OSDN Git Service

2008-06-26 H.J. Lu <hongjiu.lu@intel.com>
[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 /* Get the current fde_table entry we should use.  */
319
320 static inline dw_fde_ref
321 current_fde (void)
322 {
323   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
324 }
325
326 /* A list of call frame insns for the CIE.  */
327 static GTY(()) dw_cfi_ref cie_cfi_head;
328
329 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
330 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
331    attribute that accelerates the lookup of the FDE associated
332    with the subprogram.  This variable holds the table index of the FDE
333    associated with the current function (body) definition.  */
334 static unsigned current_funcdef_fde;
335 #endif
336
337 struct indirect_string_node GTY(())
338 {
339   const char *str;
340   unsigned int refcount;
341   unsigned int form;
342   char *label;
343 };
344
345 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
346
347 static GTY(()) int dw2_string_counter;
348 static GTY(()) unsigned long dwarf2out_cfi_label_num;
349
350 /* True if the compilation unit places functions in more than one section.  */
351 static GTY(()) bool have_multiple_function_sections = false;
352
353 /* Whether the default text and cold text sections have been used at all.  */
354
355 static GTY(()) bool text_section_used = false;
356 static GTY(()) bool cold_text_section_used = false;
357
358 /* The default cold text section.  */
359 static GTY(()) section *cold_text_section;
360
361 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
362
363 /* Forward declarations for functions defined in this file.  */
364
365 static char *stripattributes (const char *);
366 static const char *dwarf_cfi_name (unsigned);
367 static dw_cfi_ref new_cfi (void);
368 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
369 static void add_fde_cfi (const char *, dw_cfi_ref);
370 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
371 static void lookup_cfa (dw_cfa_location *);
372 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
373 #ifdef DWARF2_UNWIND_INFO
374 static void initial_return_save (rtx);
375 #endif
376 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
377 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
378 static void output_call_frame_info (int);
379 static void dwarf2out_note_section_used (void);
380 static void dwarf2out_stack_adjust (rtx, bool);
381 static void flush_queued_reg_saves (void);
382 static bool clobbers_queued_reg_save (const_rtx);
383 static void dwarf2out_frame_debug_expr (rtx, const char *);
384
385 /* Support for complex CFA locations.  */
386 static void output_cfa_loc (dw_cfi_ref);
387 static void get_cfa_from_loc_descr (dw_cfa_location *,
388                                     struct dw_loc_descr_struct *);
389 static struct dw_loc_descr_struct *build_cfa_loc
390   (dw_cfa_location *, HOST_WIDE_INT);
391 static void def_cfa_1 (const char *, dw_cfa_location *);
392
393 /* How to start an assembler comment.  */
394 #ifndef ASM_COMMENT_START
395 #define ASM_COMMENT_START ";#"
396 #endif
397
398 /* Data and reference forms for relocatable data.  */
399 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
400 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
401
402 #ifndef DEBUG_FRAME_SECTION
403 #define DEBUG_FRAME_SECTION     ".debug_frame"
404 #endif
405
406 #ifndef FUNC_BEGIN_LABEL
407 #define FUNC_BEGIN_LABEL        "LFB"
408 #endif
409
410 #ifndef FUNC_END_LABEL
411 #define FUNC_END_LABEL          "LFE"
412 #endif
413
414 #ifndef FRAME_BEGIN_LABEL
415 #define FRAME_BEGIN_LABEL       "Lframe"
416 #endif
417 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
418 #define CIE_END_LABEL           "LECIE"
419 #define FDE_LABEL               "LSFDE"
420 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
421 #define FDE_END_LABEL           "LEFDE"
422 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
423 #define LINE_NUMBER_END_LABEL   "LELT"
424 #define LN_PROLOG_AS_LABEL      "LASLTP"
425 #define LN_PROLOG_END_LABEL     "LELTP"
426 #define DIE_LABEL_PREFIX        "DW"
427
428 /* The DWARF 2 CFA column which tracks the return address.  Normally this
429    is the column for PC, or the first column after all of the hard
430    registers.  */
431 #ifndef DWARF_FRAME_RETURN_COLUMN
432 #ifdef PC_REGNUM
433 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
434 #else
435 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
436 #endif
437 #endif
438
439 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
440    default, we just provide columns for all registers.  */
441 #ifndef DWARF_FRAME_REGNUM
442 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
443 #endif
444 \f
445 /* Hook used by __throw.  */
446
447 rtx
448 expand_builtin_dwarf_sp_column (void)
449 {
450   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
451   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
452 }
453
454 /* Return a pointer to a copy of the section string name S with all
455    attributes stripped off, and an asterisk prepended (for assemble_name).  */
456
457 static inline char *
458 stripattributes (const char *s)
459 {
460   char *stripped = XNEWVEC (char, strlen (s) + 2);
461   char *p = stripped;
462
463   *p++ = '*';
464
465   while (*s && *s != ',')
466     *p++ = *s++;
467
468   *p = '\0';
469   return stripped;
470 }
471
472 /* MEM is a memory reference for the register size table, each element of
473    which has mode MODE.  Initialize column C as a return address column.  */
474
475 static void
476 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
477 {
478   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
479   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
480   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
481 }
482
483 /* Generate code to initialize the register size table.  */
484
485 void
486 expand_builtin_init_dwarf_reg_sizes (tree address)
487 {
488   unsigned int i;
489   enum machine_mode mode = TYPE_MODE (char_type_node);
490   rtx addr = expand_normal (address);
491   rtx mem = gen_rtx_MEM (BLKmode, addr);
492   bool wrote_return_column = false;
493
494   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
495     {
496       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
497
498       if (rnum < DWARF_FRAME_REGISTERS)
499         {
500           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
501           enum machine_mode save_mode = reg_raw_mode[i];
502           HOST_WIDE_INT size;
503
504           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
505             save_mode = choose_hard_reg_mode (i, 1, true);
506           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
507             {
508               if (save_mode == VOIDmode)
509                 continue;
510               wrote_return_column = true;
511             }
512           size = GET_MODE_SIZE (save_mode);
513           if (offset < 0)
514             continue;
515
516           emit_move_insn (adjust_address (mem, mode, offset),
517                           gen_int_mode (size, mode));
518         }
519     }
520
521   if (!wrote_return_column)
522     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
523
524 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
525   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
526 #endif
527
528   targetm.init_dwarf_reg_sizes_extra (address);
529 }
530
531 /* Convert a DWARF call frame info. operation to its string name */
532
533 static const char *
534 dwarf_cfi_name (unsigned int cfi_opc)
535 {
536   switch (cfi_opc)
537     {
538     case DW_CFA_advance_loc:
539       return "DW_CFA_advance_loc";
540     case DW_CFA_offset:
541       return "DW_CFA_offset";
542     case DW_CFA_restore:
543       return "DW_CFA_restore";
544     case DW_CFA_nop:
545       return "DW_CFA_nop";
546     case DW_CFA_set_loc:
547       return "DW_CFA_set_loc";
548     case DW_CFA_advance_loc1:
549       return "DW_CFA_advance_loc1";
550     case DW_CFA_advance_loc2:
551       return "DW_CFA_advance_loc2";
552     case DW_CFA_advance_loc4:
553       return "DW_CFA_advance_loc4";
554     case DW_CFA_offset_extended:
555       return "DW_CFA_offset_extended";
556     case DW_CFA_restore_extended:
557       return "DW_CFA_restore_extended";
558     case DW_CFA_undefined:
559       return "DW_CFA_undefined";
560     case DW_CFA_same_value:
561       return "DW_CFA_same_value";
562     case DW_CFA_register:
563       return "DW_CFA_register";
564     case DW_CFA_remember_state:
565       return "DW_CFA_remember_state";
566     case DW_CFA_restore_state:
567       return "DW_CFA_restore_state";
568     case DW_CFA_def_cfa:
569       return "DW_CFA_def_cfa";
570     case DW_CFA_def_cfa_register:
571       return "DW_CFA_def_cfa_register";
572     case DW_CFA_def_cfa_offset:
573       return "DW_CFA_def_cfa_offset";
574
575     /* DWARF 3 */
576     case DW_CFA_def_cfa_expression:
577       return "DW_CFA_def_cfa_expression";
578     case DW_CFA_expression:
579       return "DW_CFA_expression";
580     case DW_CFA_offset_extended_sf:
581       return "DW_CFA_offset_extended_sf";
582     case DW_CFA_def_cfa_sf:
583       return "DW_CFA_def_cfa_sf";
584     case DW_CFA_def_cfa_offset_sf:
585       return "DW_CFA_def_cfa_offset_sf";
586
587     /* SGI/MIPS specific */
588     case DW_CFA_MIPS_advance_loc8:
589       return "DW_CFA_MIPS_advance_loc8";
590
591     /* GNU extensions */
592     case DW_CFA_GNU_window_save:
593       return "DW_CFA_GNU_window_save";
594     case DW_CFA_GNU_args_size:
595       return "DW_CFA_GNU_args_size";
596     case DW_CFA_GNU_negative_offset_extended:
597       return "DW_CFA_GNU_negative_offset_extended";
598
599     default:
600       return "DW_CFA_<unknown>";
601     }
602 }
603
604 /* Return a pointer to a newly allocated Call Frame Instruction.  */
605
606 static inline dw_cfi_ref
607 new_cfi (void)
608 {
609   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
610
611   cfi->dw_cfi_next = NULL;
612   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
613   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
614
615   return cfi;
616 }
617
618 /* Add a Call Frame Instruction to list of instructions.  */
619
620 static inline void
621 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
622 {
623   dw_cfi_ref *p;
624
625   /* Find the end of the chain.  */
626   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
627     ;
628
629   *p = cfi;
630 }
631
632 /* Generate a new label for the CFI info to refer to.  */
633
634 char *
635 dwarf2out_cfi_label (void)
636 {
637   static char label[20];
638
639   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
640   ASM_OUTPUT_LABEL (asm_out_file, label);
641   return label;
642 }
643
644 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
645    or to the CIE if LABEL is NULL.  */
646
647 static void
648 add_fde_cfi (const char *label, dw_cfi_ref cfi)
649 {
650   if (label)
651     {
652       dw_fde_ref fde = current_fde ();
653
654       gcc_assert (fde != NULL);
655
656       if (*label == 0)
657         label = dwarf2out_cfi_label ();
658
659       if (fde->dw_fde_current_label == NULL
660           || strcmp (label, fde->dw_fde_current_label) != 0)
661         {
662           dw_cfi_ref xcfi;
663
664           label = xstrdup (label);
665
666           /* Set the location counter to the new label.  */
667           xcfi = new_cfi ();
668           /* If we have a current label, advance from there, otherwise
669              set the location directly using set_loc.  */
670           xcfi->dw_cfi_opc = fde->dw_fde_current_label
671                              ? DW_CFA_advance_loc4
672                              : DW_CFA_set_loc;
673           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
674           add_cfi (&fde->dw_fde_cfi, xcfi);
675
676           fde->dw_fde_current_label = label;
677         }
678
679       add_cfi (&fde->dw_fde_cfi, cfi);
680     }
681
682   else
683     add_cfi (&cie_cfi_head, cfi);
684 }
685
686 /* Subroutine of lookup_cfa.  */
687
688 static void
689 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
690 {
691   switch (cfi->dw_cfi_opc)
692     {
693     case DW_CFA_def_cfa_offset:
694       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
695       break;
696     case DW_CFA_def_cfa_offset_sf:
697       loc->offset
698         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
699       break;
700     case DW_CFA_def_cfa_register:
701       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
702       break;
703     case DW_CFA_def_cfa:
704       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
705       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
706       break;
707     case DW_CFA_def_cfa_sf:
708       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
709       loc->offset
710         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
711       break;
712     case DW_CFA_def_cfa_expression:
713       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
714       break;
715     default:
716       break;
717     }
718 }
719
720 /* Find the previous value for the CFA.  */
721
722 static void
723 lookup_cfa (dw_cfa_location *loc)
724 {
725   dw_cfi_ref cfi;
726   dw_fde_ref fde;
727
728   loc->reg = INVALID_REGNUM;
729   loc->offset = 0;
730   loc->indirect = 0;
731   loc->base_offset = 0;
732
733   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
734     lookup_cfa_1 (cfi, loc);
735
736   fde = current_fde ();
737   if (fde)
738     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
739       lookup_cfa_1 (cfi, loc);
740 }
741
742 /* The current rule for calculating the DWARF2 canonical frame address.  */
743 static dw_cfa_location cfa;
744
745 /* The register used for saving registers to the stack, and its offset
746    from the CFA.  */
747 static dw_cfa_location cfa_store;
748
749 /* The running total of the size of arguments pushed onto the stack.  */
750 static HOST_WIDE_INT args_size;
751
752 /* The last args_size we actually output.  */
753 static HOST_WIDE_INT old_args_size;
754
755 /* Entry point to update the canonical frame address (CFA).
756    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
757    calculated from REG+OFFSET.  */
758
759 void
760 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
761 {
762   dw_cfa_location loc;
763   loc.indirect = 0;
764   loc.base_offset = 0;
765   loc.reg = reg;
766   loc.offset = offset;
767   def_cfa_1 (label, &loc);
768 }
769
770 /* Determine if two dw_cfa_location structures define the same data.  */
771
772 static bool
773 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
774 {
775   return (loc1->reg == loc2->reg
776           && loc1->offset == loc2->offset
777           && loc1->indirect == loc2->indirect
778           && (loc1->indirect == 0
779               || loc1->base_offset == loc2->base_offset));
780 }
781
782 /* This routine does the actual work.  The CFA is now calculated from
783    the dw_cfa_location structure.  */
784
785 static void
786 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
787 {
788   dw_cfi_ref cfi;
789   dw_cfa_location old_cfa, loc;
790
791   cfa = *loc_p;
792   loc = *loc_p;
793
794   if (cfa_store.reg == loc.reg && loc.indirect == 0)
795     cfa_store.offset = loc.offset;
796
797   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
798   lookup_cfa (&old_cfa);
799
800   /* If nothing changed, no need to issue any call frame instructions.  */
801   if (cfa_equal_p (&loc, &old_cfa))
802     return;
803
804   cfi = new_cfi ();
805
806   if (loc.reg == old_cfa.reg && !loc.indirect)
807     {
808       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
809          the CFA register did not change but the offset did.  */
810       if (loc.offset < 0)
811         {
812           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
813           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
814
815           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
816           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
817         }
818       else
819         {
820           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
821           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
822         }
823     }
824
825 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
826   else if (loc.offset == old_cfa.offset
827            && old_cfa.reg != INVALID_REGNUM
828            && !loc.indirect)
829     {
830       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
831          indicating the CFA register has changed to <register> but the
832          offset has not changed.  */
833       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
834       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
835     }
836 #endif
837
838   else if (loc.indirect == 0)
839     {
840       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
841          indicating the CFA register has changed to <register> with
842          the specified offset.  */
843       if (loc.offset < 0)
844         {
845           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
846           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
847
848           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
849           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
850           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
851         }
852       else
853         {
854           cfi->dw_cfi_opc = DW_CFA_def_cfa;
855           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
856           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
857         }
858     }
859   else
860     {
861       /* Construct a DW_CFA_def_cfa_expression instruction to
862          calculate the CFA using a full location expression since no
863          register-offset pair is available.  */
864       struct dw_loc_descr_struct *loc_list;
865
866       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
867       loc_list = build_cfa_loc (&loc, 0);
868       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
869     }
870
871   add_fde_cfi (label, cfi);
872 }
873
874 /* Add the CFI for saving a register.  REG is the CFA column number.
875    LABEL is passed to add_fde_cfi.
876    If SREG is -1, the register is saved at OFFSET from the CFA;
877    otherwise it is saved in SREG.  */
878
879 static void
880 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
881 {
882   dw_cfi_ref cfi = new_cfi ();
883
884   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
885
886   if (sreg == INVALID_REGNUM)
887     {
888       if (reg & ~0x3f)
889         /* The register number won't fit in 6 bits, so we have to use
890            the long form.  */
891         cfi->dw_cfi_opc = DW_CFA_offset_extended;
892       else
893         cfi->dw_cfi_opc = DW_CFA_offset;
894
895 #ifdef ENABLE_CHECKING
896       {
897         /* If we get an offset that is not a multiple of
898            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
899            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
900            description.  */
901         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
902
903         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
904       }
905 #endif
906       offset /= DWARF_CIE_DATA_ALIGNMENT;
907       if (offset < 0)
908         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
909
910       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
911     }
912   else if (sreg == reg)
913     cfi->dw_cfi_opc = DW_CFA_same_value;
914   else
915     {
916       cfi->dw_cfi_opc = DW_CFA_register;
917       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
918     }
919
920   add_fde_cfi (label, cfi);
921 }
922
923 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
924    This CFI tells the unwinder that it needs to restore the window registers
925    from the previous frame's window save area.
926
927    ??? Perhaps we should note in the CIE where windows are saved (instead of
928    assuming 0(cfa)) and what registers are in the window.  */
929
930 void
931 dwarf2out_window_save (const char *label)
932 {
933   dw_cfi_ref cfi = new_cfi ();
934
935   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
936   add_fde_cfi (label, cfi);
937 }
938
939 /* Add a CFI to update the running total of the size of arguments
940    pushed onto the stack.  */
941
942 void
943 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
944 {
945   dw_cfi_ref cfi;
946
947   if (size == old_args_size)
948     return;
949
950   old_args_size = size;
951
952   cfi = new_cfi ();
953   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
954   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
955   add_fde_cfi (label, cfi);
956 }
957
958 /* Entry point for saving a register to the stack.  REG is the GCC register
959    number.  LABEL and OFFSET are passed to reg_save.  */
960
961 void
962 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
963 {
964   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
965 }
966
967 /* Entry point for saving the return address in the stack.
968    LABEL and OFFSET are passed to reg_save.  */
969
970 void
971 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
972 {
973   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
974 }
975
976 /* Entry point for saving the return address in a register.
977    LABEL and SREG are passed to reg_save.  */
978
979 void
980 dwarf2out_return_reg (const char *label, unsigned int sreg)
981 {
982   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
983 }
984
985 #ifdef DWARF2_UNWIND_INFO
986 /* Record the initial position of the return address.  RTL is
987    INCOMING_RETURN_ADDR_RTX.  */
988
989 static void
990 initial_return_save (rtx rtl)
991 {
992   unsigned int reg = INVALID_REGNUM;
993   HOST_WIDE_INT offset = 0;
994
995   switch (GET_CODE (rtl))
996     {
997     case REG:
998       /* RA is in a register.  */
999       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1000       break;
1001
1002     case MEM:
1003       /* RA is on the stack.  */
1004       rtl = XEXP (rtl, 0);
1005       switch (GET_CODE (rtl))
1006         {
1007         case REG:
1008           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1009           offset = 0;
1010           break;
1011
1012         case PLUS:
1013           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1014           offset = INTVAL (XEXP (rtl, 1));
1015           break;
1016
1017         case MINUS:
1018           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1019           offset = -INTVAL (XEXP (rtl, 1));
1020           break;
1021
1022         default:
1023           gcc_unreachable ();
1024         }
1025
1026       break;
1027
1028     case PLUS:
1029       /* The return address is at some offset from any value we can
1030          actually load.  For instance, on the SPARC it is in %i7+8. Just
1031          ignore the offset for now; it doesn't matter for unwinding frames.  */
1032       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1033       initial_return_save (XEXP (rtl, 0));
1034       return;
1035
1036     default:
1037       gcc_unreachable ();
1038     }
1039
1040   if (reg != DWARF_FRAME_RETURN_COLUMN)
1041     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1042 }
1043 #endif
1044
1045 /* Given a SET, calculate the amount of stack adjustment it
1046    contains.  */
1047
1048 static HOST_WIDE_INT
1049 stack_adjust_offset (const_rtx pattern)
1050 {
1051   const_rtx src = SET_SRC (pattern);
1052   const_rtx dest = SET_DEST (pattern);
1053   HOST_WIDE_INT offset = 0;
1054   enum rtx_code code;
1055
1056   if (dest == stack_pointer_rtx)
1057     {
1058       /* (set (reg sp) (plus (reg sp) (const_int))) */
1059       code = GET_CODE (src);
1060       if (! (code == PLUS || code == MINUS)
1061           || XEXP (src, 0) != stack_pointer_rtx
1062           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1063         return 0;
1064
1065       offset = INTVAL (XEXP (src, 1));
1066       if (code == PLUS)
1067         offset = -offset;
1068     }
1069   else if (MEM_P (dest))
1070     {
1071       /* (set (mem (pre_dec (reg sp))) (foo)) */
1072       src = XEXP (dest, 0);
1073       code = GET_CODE (src);
1074
1075       switch (code)
1076         {
1077         case PRE_MODIFY:
1078         case POST_MODIFY:
1079           if (XEXP (src, 0) == stack_pointer_rtx)
1080             {
1081               rtx val = XEXP (XEXP (src, 1), 1);
1082               /* We handle only adjustments by constant amount.  */
1083               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1084                           && GET_CODE (val) == CONST_INT);
1085               offset = -INTVAL (val);
1086               break;
1087             }
1088           return 0;
1089
1090         case PRE_DEC:
1091         case POST_DEC:
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         case PRE_INC:
1100         case POST_INC:
1101           if (XEXP (src, 0) == stack_pointer_rtx)
1102             {
1103               offset = -GET_MODE_SIZE (GET_MODE (dest));
1104               break;
1105             }
1106           return 0;
1107
1108         default:
1109           return 0;
1110         }
1111     }
1112   else
1113     return 0;
1114
1115   return offset;
1116 }
1117
1118 /* Check INSN to see if it looks like a push or a stack adjustment, and
1119    make a note of it if it does.  EH uses this information to find out how
1120    much extra space it needs to pop off the stack.  */
1121
1122 static void
1123 dwarf2out_stack_adjust (rtx insn, bool after_p)
1124 {
1125   HOST_WIDE_INT offset;
1126   const char *label;
1127   int i;
1128
1129   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1130      with this function.  Proper support would require all frame-related
1131      insns to be marked, and to be able to handle saving state around
1132      epilogues textually in the middle of the function.  */
1133   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1134     return;
1135
1136   /* If only calls can throw, and we have a frame pointer,
1137      save up adjustments until we see the CALL_INSN.  */
1138   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1139     {
1140       if (CALL_P (insn) && !after_p)
1141         {
1142           /* Extract the size of the args from the CALL rtx itself.  */
1143           insn = PATTERN (insn);
1144           if (GET_CODE (insn) == PARALLEL)
1145             insn = XVECEXP (insn, 0, 0);
1146           if (GET_CODE (insn) == SET)
1147             insn = SET_SRC (insn);
1148           gcc_assert (GET_CODE (insn) == CALL);
1149           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1150         }
1151       return;
1152     }
1153
1154   if (CALL_P (insn) && !after_p)
1155     {
1156       if (!flag_asynchronous_unwind_tables)
1157         dwarf2out_args_size ("", args_size);
1158       return;
1159     }
1160   else if (BARRIER_P (insn))
1161     {
1162       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1163          the compiler will have already emitted a stack adjustment, but
1164          doesn't bother for calls to noreturn functions.  */
1165 #ifdef STACK_GROWS_DOWNWARD
1166       offset = -args_size;
1167 #else
1168       offset = args_size;
1169 #endif
1170     }
1171   else if (GET_CODE (PATTERN (insn)) == SET)
1172     offset = stack_adjust_offset (PATTERN (insn));
1173   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1174            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1175     {
1176       /* There may be stack adjustments inside compound insns.  Search
1177          for them.  */
1178       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1179         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1180           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1181     }
1182   else
1183     return;
1184
1185   if (offset == 0)
1186     return;
1187
1188   if (cfa.reg == STACK_POINTER_REGNUM)
1189     cfa.offset += offset;
1190
1191 #ifndef STACK_GROWS_DOWNWARD
1192   offset = -offset;
1193 #endif
1194
1195   args_size += offset;
1196   if (args_size < 0)
1197     args_size = 0;
1198
1199   label = dwarf2out_cfi_label ();
1200   def_cfa_1 (label, &cfa);
1201   if (flag_asynchronous_unwind_tables)
1202     dwarf2out_args_size (label, args_size);
1203 }
1204
1205 #endif
1206
1207 /* We delay emitting a register save until either (a) we reach the end
1208    of the prologue or (b) the register is clobbered.  This clusters
1209    register saves so that there are fewer pc advances.  */
1210
1211 struct queued_reg_save GTY(())
1212 {
1213   struct queued_reg_save *next;
1214   rtx reg;
1215   HOST_WIDE_INT cfa_offset;
1216   rtx saved_reg;
1217 };
1218
1219 static GTY(()) struct queued_reg_save *queued_reg_saves;
1220
1221 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1222 struct reg_saved_in_data GTY(()) {
1223   rtx orig_reg;
1224   rtx saved_in_reg;
1225 };
1226
1227 /* A list of registers saved in other registers.
1228    The list intentionally has a small maximum capacity of 4; if your
1229    port needs more than that, you might consider implementing a
1230    more efficient data structure.  */
1231 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1232 static GTY(()) size_t num_regs_saved_in_regs;
1233
1234 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1235 static const char *last_reg_save_label;
1236
1237 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1238    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1239
1240 static void
1241 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1242 {
1243   struct queued_reg_save *q;
1244
1245   /* Duplicates waste space, but it's also necessary to remove them
1246      for correctness, since the queue gets output in reverse
1247      order.  */
1248   for (q = queued_reg_saves; q != NULL; q = q->next)
1249     if (REGNO (q->reg) == REGNO (reg))
1250       break;
1251
1252   if (q == NULL)
1253     {
1254       q = GGC_NEW (struct queued_reg_save);
1255       q->next = queued_reg_saves;
1256       queued_reg_saves = q;
1257     }
1258
1259   q->reg = reg;
1260   q->cfa_offset = offset;
1261   q->saved_reg = sreg;
1262
1263   last_reg_save_label = label;
1264 }
1265
1266 /* Output all the entries in QUEUED_REG_SAVES.  */
1267
1268 static void
1269 flush_queued_reg_saves (void)
1270 {
1271   struct queued_reg_save *q;
1272
1273   for (q = queued_reg_saves; q; q = q->next)
1274     {
1275       size_t i;
1276       unsigned int reg, sreg;
1277
1278       for (i = 0; i < num_regs_saved_in_regs; i++)
1279         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1280           break;
1281       if (q->saved_reg && i == num_regs_saved_in_regs)
1282         {
1283           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1284           num_regs_saved_in_regs++;
1285         }
1286       if (i != num_regs_saved_in_regs)
1287         {
1288           regs_saved_in_regs[i].orig_reg = q->reg;
1289           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1290         }
1291
1292       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1293       if (q->saved_reg)
1294         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1295       else
1296         sreg = INVALID_REGNUM;
1297       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1298     }
1299
1300   queued_reg_saves = NULL;
1301   last_reg_save_label = NULL;
1302 }
1303
1304 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1305    location for?  Or, does it clobber a register which we've previously
1306    said that some other register is saved in, and for which we now
1307    have a new location for?  */
1308
1309 static bool
1310 clobbers_queued_reg_save (const_rtx insn)
1311 {
1312   struct queued_reg_save *q;
1313
1314   for (q = queued_reg_saves; q; q = q->next)
1315     {
1316       size_t i;
1317       if (modified_in_p (q->reg, insn))
1318         return true;
1319       for (i = 0; i < num_regs_saved_in_regs; i++)
1320         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1321             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1322           return true;
1323     }
1324
1325   return false;
1326 }
1327
1328 /* Entry point for saving the first register into the second.  */
1329
1330 void
1331 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1332 {
1333   size_t i;
1334   unsigned int regno, sregno;
1335
1336   for (i = 0; i < num_regs_saved_in_regs; i++)
1337     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1338       break;
1339   if (i == num_regs_saved_in_regs)
1340     {
1341       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1342       num_regs_saved_in_regs++;
1343     }
1344   regs_saved_in_regs[i].orig_reg = reg;
1345   regs_saved_in_regs[i].saved_in_reg = sreg;
1346
1347   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1348   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1349   reg_save (label, regno, sregno, 0);
1350 }
1351
1352 /* What register, if any, is currently saved in REG?  */
1353
1354 static rtx
1355 reg_saved_in (rtx reg)
1356 {
1357   unsigned int regn = REGNO (reg);
1358   size_t i;
1359   struct queued_reg_save *q;
1360
1361   for (q = queued_reg_saves; q; q = q->next)
1362     if (q->saved_reg && regn == REGNO (q->saved_reg))
1363       return q->reg;
1364
1365   for (i = 0; i < num_regs_saved_in_regs; i++)
1366     if (regs_saved_in_regs[i].saved_in_reg
1367         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1368       return regs_saved_in_regs[i].orig_reg;
1369
1370   return NULL_RTX;
1371 }
1372
1373
1374 /* A temporary register holding an integral value used in adjusting SP
1375    or setting up the store_reg.  The "offset" field holds the integer
1376    value, not an offset.  */
1377 static dw_cfa_location cfa_temp;
1378
1379 /* Record call frame debugging information for an expression EXPR,
1380    which either sets SP or FP (adjusting how we calculate the frame
1381    address) or saves a register to the stack or another register.
1382    LABEL indicates the address of EXPR.
1383
1384    This function encodes a state machine mapping rtxes to actions on
1385    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1386    users need not read the source code.
1387
1388   The High-Level Picture
1389
1390   Changes in the register we use to calculate the CFA: Currently we
1391   assume that if you copy the CFA register into another register, we
1392   should take the other one as the new CFA register; this seems to
1393   work pretty well.  If it's wrong for some target, it's simple
1394   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1395
1396   Changes in the register we use for saving registers to the stack:
1397   This is usually SP, but not always.  Again, we deduce that if you
1398   copy SP into another register (and SP is not the CFA register),
1399   then the new register is the one we will be using for register
1400   saves.  This also seems to work.
1401
1402   Register saves: There's not much guesswork about this one; if
1403   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1404   register save, and the register used to calculate the destination
1405   had better be the one we think we're using for this purpose.
1406   It's also assumed that a copy from a call-saved register to another
1407   register is saving that register if RTX_FRAME_RELATED_P is set on
1408   that instruction.  If the copy is from a call-saved register to
1409   the *same* register, that means that the register is now the same
1410   value as in the caller.
1411
1412   Except: If the register being saved is the CFA register, and the
1413   offset is nonzero, we are saving the CFA, so we assume we have to
1414   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1415   the intent is to save the value of SP from the previous frame.
1416
1417   In addition, if a register has previously been saved to a different
1418   register,
1419
1420   Invariants / Summaries of Rules
1421
1422   cfa          current rule for calculating the CFA.  It usually
1423                consists of a register and an offset.
1424   cfa_store    register used by prologue code to save things to the stack
1425                cfa_store.offset is the offset from the value of
1426                cfa_store.reg to the actual CFA
1427   cfa_temp     register holding an integral value.  cfa_temp.offset
1428                stores the value, which will be used to adjust the
1429                stack pointer.  cfa_temp is also used like cfa_store,
1430                to track stores to the stack via fp or a temp reg.
1431
1432   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1433                with cfa.reg as the first operand changes the cfa.reg and its
1434                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1435                cfa_temp.offset.
1436
1437   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1438                expression yielding a constant.  This sets cfa_temp.reg
1439                and cfa_temp.offset.
1440
1441   Rule 5:      Create a new register cfa_store used to save items to the
1442                stack.
1443
1444   Rules 10-14: Save a register to the stack.  Define offset as the
1445                difference of the original location and cfa_store's
1446                location (or cfa_temp's location if cfa_temp is used).
1447
1448   The Rules
1449
1450   "{a,b}" indicates a choice of a xor b.
1451   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1452
1453   Rule 1:
1454   (set <reg1> <reg2>:cfa.reg)
1455   effects: cfa.reg = <reg1>
1456            cfa.offset unchanged
1457            cfa_temp.reg = <reg1>
1458            cfa_temp.offset = cfa.offset
1459
1460   Rule 2:
1461   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1462                               {<const_int>,<reg>:cfa_temp.reg}))
1463   effects: cfa.reg = sp if fp used
1464            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1465            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1466              if cfa_store.reg==sp
1467
1468   Rule 3:
1469   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1470   effects: cfa.reg = fp
1471            cfa_offset += +/- <const_int>
1472
1473   Rule 4:
1474   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1475   constraints: <reg1> != fp
1476                <reg1> != sp
1477   effects: cfa.reg = <reg1>
1478            cfa_temp.reg = <reg1>
1479            cfa_temp.offset = cfa.offset
1480
1481   Rule 5:
1482   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1483   constraints: <reg1> != fp
1484                <reg1> != sp
1485   effects: cfa_store.reg = <reg1>
1486            cfa_store.offset = cfa.offset - cfa_temp.offset
1487
1488   Rule 6:
1489   (set <reg> <const_int>)
1490   effects: cfa_temp.reg = <reg>
1491            cfa_temp.offset = <const_int>
1492
1493   Rule 7:
1494   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1495   effects: cfa_temp.reg = <reg1>
1496            cfa_temp.offset |= <const_int>
1497
1498   Rule 8:
1499   (set <reg> (high <exp>))
1500   effects: none
1501
1502   Rule 9:
1503   (set <reg> (lo_sum <exp> <const_int>))
1504   effects: cfa_temp.reg = <reg>
1505            cfa_temp.offset = <const_int>
1506
1507   Rule 10:
1508   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1509   effects: cfa_store.offset -= <const_int>
1510            cfa.offset = cfa_store.offset if cfa.reg == sp
1511            cfa.reg = sp
1512            cfa.base_offset = -cfa_store.offset
1513
1514   Rule 11:
1515   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1516   effects: cfa_store.offset += -/+ mode_size(mem)
1517            cfa.offset = cfa_store.offset if cfa.reg == sp
1518            cfa.reg = sp
1519            cfa.base_offset = -cfa_store.offset
1520
1521   Rule 12:
1522   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1523
1524        <reg2>)
1525   effects: cfa.reg = <reg1>
1526            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1527
1528   Rule 13:
1529   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1530   effects: cfa.reg = <reg1>
1531            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1532
1533   Rule 14:
1534   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1535   effects: cfa.reg = <reg1>
1536            cfa.base_offset = -cfa_temp.offset
1537            cfa_temp.offset -= mode_size(mem)
1538
1539   Rule 15:
1540   (set <reg> {unspec, unspec_volatile})
1541   effects: target-dependent  */
1542
1543 static void
1544 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1545 {
1546   rtx src, dest, span;
1547   HOST_WIDE_INT offset;
1548
1549   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1550      the PARALLEL independently. The first element is always processed if
1551      it is a SET. This is for backward compatibility.   Other elements
1552      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1553      flag is set in them.  */
1554   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1555     {
1556       int par_index;
1557       int limit = XVECLEN (expr, 0);
1558       rtx elem;
1559
1560       /* PARALLELs have strict read-modify-write semantics, so we
1561          ought to evaluate every rvalue before changing any lvalue.
1562          It's cumbersome to do that in general, but there's an
1563          easy approximation that is enough for all current users:
1564          handle register saves before register assignments.  */
1565       if (GET_CODE (expr) == PARALLEL)
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))
1571                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1572               dwarf2out_frame_debug_expr (elem, label);
1573           }
1574
1575       for (par_index = 0; par_index < limit; par_index++)
1576         {
1577           elem = XVECEXP (expr, 0, par_index);
1578           if (GET_CODE (elem) == SET
1579               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1580               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1581             dwarf2out_frame_debug_expr (elem, label);
1582         }
1583       return;
1584     }
1585
1586   gcc_assert (GET_CODE (expr) == SET);
1587
1588   src = SET_SRC (expr);
1589   dest = SET_DEST (expr);
1590
1591   if (REG_P (src))
1592     {
1593       rtx rsi = reg_saved_in (src);
1594       if (rsi)
1595         src = rsi;
1596     }
1597
1598   switch (GET_CODE (dest))
1599     {
1600     case REG:
1601       switch (GET_CODE (src))
1602         {
1603           /* Setting FP from SP.  */
1604         case REG:
1605           if (cfa.reg == (unsigned) REGNO (src))
1606             {
1607               /* Rule 1 */
1608               /* Update the CFA rule wrt SP or FP.  Make sure src is
1609                  relative to the current CFA register.
1610
1611                  We used to require that dest be either SP or FP, but the
1612                  ARM copies SP to a temporary register, and from there to
1613                  FP.  So we just rely on the backends to only set
1614                  RTX_FRAME_RELATED_P on appropriate insns.  */
1615               cfa.reg = REGNO (dest);
1616               cfa_temp.reg = cfa.reg;
1617               cfa_temp.offset = cfa.offset;
1618             }
1619           else
1620             {
1621               /* Saving a register in a register.  */
1622               gcc_assert (!fixed_regs [REGNO (dest)]
1623                           /* For the SPARC and its register window.  */
1624                           || (DWARF_FRAME_REGNUM (REGNO (src))
1625                               == DWARF_FRAME_RETURN_COLUMN));
1626               queue_reg_save (label, src, dest, 0);
1627             }
1628           break;
1629
1630         case PLUS:
1631         case MINUS:
1632         case LO_SUM:
1633           if (dest == stack_pointer_rtx)
1634             {
1635               /* Rule 2 */
1636               /* Adjusting SP.  */
1637               switch (GET_CODE (XEXP (src, 1)))
1638                 {
1639                 case CONST_INT:
1640                   offset = INTVAL (XEXP (src, 1));
1641                   break;
1642                 case REG:
1643                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1644                               == cfa_temp.reg);
1645                   offset = cfa_temp.offset;
1646                   break;
1647                 default:
1648                   gcc_unreachable ();
1649                 }
1650
1651               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1652                 {
1653                   /* Restoring SP from FP in the epilogue.  */
1654                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1655                   cfa.reg = STACK_POINTER_REGNUM;
1656                 }
1657               else if (GET_CODE (src) == LO_SUM)
1658                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1659                 ;
1660               else
1661                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1662
1663               if (GET_CODE (src) != MINUS)
1664                 offset = -offset;
1665               if (cfa.reg == STACK_POINTER_REGNUM)
1666                 cfa.offset += offset;
1667               if (cfa_store.reg == STACK_POINTER_REGNUM)
1668                 cfa_store.offset += offset;
1669             }
1670           else if (dest == hard_frame_pointer_rtx)
1671             {
1672               /* Rule 3 */
1673               /* Either setting the FP from an offset of the SP,
1674                  or adjusting the FP */
1675               gcc_assert (frame_pointer_needed);
1676
1677               gcc_assert (REG_P (XEXP (src, 0))
1678                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1679                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1680               offset = INTVAL (XEXP (src, 1));
1681               if (GET_CODE (src) != MINUS)
1682                 offset = -offset;
1683               cfa.offset += offset;
1684               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1685             }
1686           else
1687             {
1688               gcc_assert (GET_CODE (src) != MINUS);
1689
1690               /* Rule 4 */
1691               if (REG_P (XEXP (src, 0))
1692                   && REGNO (XEXP (src, 0)) == cfa.reg
1693                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1694                 {
1695                   /* Setting a temporary CFA register that will be copied
1696                      into the FP later on.  */
1697                   offset = - INTVAL (XEXP (src, 1));
1698                   cfa.offset += offset;
1699                   cfa.reg = REGNO (dest);
1700                   /* Or used to save regs to the stack.  */
1701                   cfa_temp.reg = cfa.reg;
1702                   cfa_temp.offset = cfa.offset;
1703                 }
1704
1705               /* Rule 5 */
1706               else if (REG_P (XEXP (src, 0))
1707                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1708                        && XEXP (src, 1) == stack_pointer_rtx)
1709                 {
1710                   /* Setting a scratch register that we will use instead
1711                      of SP for saving registers to the stack.  */
1712                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1713                   cfa_store.reg = REGNO (dest);
1714                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1715                 }
1716
1717               /* Rule 9 */
1718               else if (GET_CODE (src) == LO_SUM
1719                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1720                 {
1721                   cfa_temp.reg = REGNO (dest);
1722                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1723                 }
1724               else
1725                 gcc_unreachable ();
1726             }
1727           break;
1728
1729           /* Rule 6 */
1730         case CONST_INT:
1731           cfa_temp.reg = REGNO (dest);
1732           cfa_temp.offset = INTVAL (src);
1733           break;
1734
1735           /* Rule 7 */
1736         case IOR:
1737           gcc_assert (REG_P (XEXP (src, 0))
1738                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1739                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1740
1741           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1742             cfa_temp.reg = REGNO (dest);
1743           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1744           break;
1745
1746           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1747              which will fill in all of the bits.  */
1748           /* Rule 8 */
1749         case HIGH:
1750           break;
1751
1752           /* Rule 15 */
1753         case UNSPEC:
1754         case UNSPEC_VOLATILE:
1755           gcc_assert (targetm.dwarf_handle_frame_unspec);
1756           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1757           return;
1758
1759         default:
1760           gcc_unreachable ();
1761         }
1762
1763       def_cfa_1 (label, &cfa);
1764       break;
1765
1766     case MEM:
1767       gcc_assert (REG_P (src));
1768
1769       /* Saving a register to the stack.  Make sure dest is relative to the
1770          CFA register.  */
1771       switch (GET_CODE (XEXP (dest, 0)))
1772         {
1773           /* Rule 10 */
1774           /* With a push.  */
1775         case PRE_MODIFY:
1776           /* We can't handle variable size modifications.  */
1777           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1778                       == CONST_INT);
1779           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1780
1781           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1782                       && cfa_store.reg == STACK_POINTER_REGNUM);
1783
1784           cfa_store.offset += offset;
1785           if (cfa.reg == STACK_POINTER_REGNUM)
1786             cfa.offset = cfa_store.offset;
1787
1788           offset = -cfa_store.offset;
1789           break;
1790
1791           /* Rule 11 */
1792         case PRE_INC:
1793         case PRE_DEC:
1794           offset = GET_MODE_SIZE (GET_MODE (dest));
1795           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1796             offset = -offset;
1797
1798           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1799                       && cfa_store.reg == STACK_POINTER_REGNUM);
1800
1801           cfa_store.offset += offset;
1802           if (cfa.reg == STACK_POINTER_REGNUM)
1803             cfa.offset = cfa_store.offset;
1804
1805           offset = -cfa_store.offset;
1806           break;
1807
1808           /* Rule 12 */
1809           /* With an offset.  */
1810         case PLUS:
1811         case MINUS:
1812         case LO_SUM:
1813           {
1814             int regno;
1815
1816             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1817                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1818             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1819             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1820               offset = -offset;
1821
1822             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1823
1824             if (cfa_store.reg == (unsigned) regno)
1825               offset -= cfa_store.offset;
1826             else
1827               {
1828                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1829                 offset -= cfa_temp.offset;
1830               }
1831           }
1832           break;
1833
1834           /* Rule 13 */
1835           /* Without an offset.  */
1836         case REG:
1837           {
1838             int regno = REGNO (XEXP (dest, 0));
1839
1840             if (cfa_store.reg == (unsigned) regno)
1841               offset = -cfa_store.offset;
1842             else
1843               {
1844                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1845                 offset = -cfa_temp.offset;
1846               }
1847           }
1848           break;
1849
1850           /* Rule 14 */
1851         case POST_INC:
1852           gcc_assert (cfa_temp.reg
1853                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1854           offset = -cfa_temp.offset;
1855           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1856           break;
1857
1858         default:
1859           gcc_unreachable ();
1860         }
1861
1862       if (REGNO (src) != STACK_POINTER_REGNUM
1863           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1864           && (unsigned) REGNO (src) == cfa.reg)
1865         {
1866           /* We're storing the current CFA reg into the stack.  */
1867
1868           if (cfa.offset == 0)
1869             {
1870               /* If the source register is exactly the CFA, assume
1871                  we're saving SP like any other register; this happens
1872                  on the ARM.  */
1873               def_cfa_1 (label, &cfa);
1874               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1875               break;
1876             }
1877           else
1878             {
1879               /* Otherwise, we'll need to look in the stack to
1880                  calculate the CFA.  */
1881               rtx x = XEXP (dest, 0);
1882
1883               if (!REG_P (x))
1884                 x = XEXP (x, 0);
1885               gcc_assert (REG_P (x));
1886
1887               cfa.reg = REGNO (x);
1888               cfa.base_offset = offset;
1889               cfa.indirect = 1;
1890               def_cfa_1 (label, &cfa);
1891               break;
1892             }
1893         }
1894
1895       def_cfa_1 (label, &cfa);
1896       {
1897         span = targetm.dwarf_register_span (src);
1898
1899         if (!span)
1900           queue_reg_save (label, src, NULL_RTX, offset);
1901         else
1902           {
1903             /* We have a PARALLEL describing where the contents of SRC
1904                live.  Queue register saves for each piece of the
1905                PARALLEL.  */
1906             int par_index;
1907             int limit;
1908             HOST_WIDE_INT span_offset = offset;
1909
1910             gcc_assert (GET_CODE (span) == PARALLEL);
1911
1912             limit = XVECLEN (span, 0);
1913             for (par_index = 0; par_index < limit; par_index++)
1914               {
1915                 rtx elem = XVECEXP (span, 0, par_index);
1916
1917                 queue_reg_save (label, elem, NULL_RTX, span_offset);
1918                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1919               }
1920           }
1921       }
1922       break;
1923
1924     default:
1925       gcc_unreachable ();
1926     }
1927 }
1928
1929 /* Record call frame debugging information for INSN, which either
1930    sets SP or FP (adjusting how we calculate the frame address) or saves a
1931    register to the stack.  If INSN is NULL_RTX, initialize our state.
1932
1933    If AFTER_P is false, we're being called before the insn is emitted,
1934    otherwise after.  Call instructions get invoked twice.  */
1935
1936 void
1937 dwarf2out_frame_debug (rtx insn, bool after_p)
1938 {
1939   const char *label;
1940   rtx src;
1941
1942   if (insn == NULL_RTX)
1943     {
1944       size_t i;
1945
1946       /* Flush any queued register saves.  */
1947       flush_queued_reg_saves ();
1948
1949       /* Set up state for generating call frame debug info.  */
1950       lookup_cfa (&cfa);
1951       gcc_assert (cfa.reg
1952                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1953
1954       cfa.reg = STACK_POINTER_REGNUM;
1955       cfa_store = cfa;
1956       cfa_temp.reg = -1;
1957       cfa_temp.offset = 0;
1958
1959       for (i = 0; i < num_regs_saved_in_regs; i++)
1960         {
1961           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1962           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1963         }
1964       num_regs_saved_in_regs = 0;
1965       return;
1966     }
1967
1968   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1969     flush_queued_reg_saves ();
1970
1971   if (! RTX_FRAME_RELATED_P (insn))
1972     {
1973       if (!ACCUMULATE_OUTGOING_ARGS)
1974         dwarf2out_stack_adjust (insn, after_p);
1975       return;
1976     }
1977
1978   label = dwarf2out_cfi_label ();
1979   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1980   if (src)
1981     insn = XEXP (src, 0);
1982   else
1983     insn = PATTERN (insn);
1984
1985   dwarf2out_frame_debug_expr (insn, label);
1986 }
1987
1988 #endif
1989
1990 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1991 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1992  (enum dwarf_call_frame_info cfi);
1993
1994 static enum dw_cfi_oprnd_type
1995 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1996 {
1997   switch (cfi)
1998     {
1999     case DW_CFA_nop:
2000     case DW_CFA_GNU_window_save:
2001       return dw_cfi_oprnd_unused;
2002
2003     case DW_CFA_set_loc:
2004     case DW_CFA_advance_loc1:
2005     case DW_CFA_advance_loc2:
2006     case DW_CFA_advance_loc4:
2007     case DW_CFA_MIPS_advance_loc8:
2008       return dw_cfi_oprnd_addr;
2009
2010     case DW_CFA_offset:
2011     case DW_CFA_offset_extended:
2012     case DW_CFA_def_cfa:
2013     case DW_CFA_offset_extended_sf:
2014     case DW_CFA_def_cfa_sf:
2015     case DW_CFA_restore_extended:
2016     case DW_CFA_undefined:
2017     case DW_CFA_same_value:
2018     case DW_CFA_def_cfa_register:
2019     case DW_CFA_register:
2020       return dw_cfi_oprnd_reg_num;
2021
2022     case DW_CFA_def_cfa_offset:
2023     case DW_CFA_GNU_args_size:
2024     case DW_CFA_def_cfa_offset_sf:
2025       return dw_cfi_oprnd_offset;
2026
2027     case DW_CFA_def_cfa_expression:
2028     case DW_CFA_expression:
2029       return dw_cfi_oprnd_loc;
2030
2031     default:
2032       gcc_unreachable ();
2033     }
2034 }
2035
2036 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2037 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2038  (enum dwarf_call_frame_info cfi);
2039
2040 static enum dw_cfi_oprnd_type
2041 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2042 {
2043   switch (cfi)
2044     {
2045     case DW_CFA_def_cfa:
2046     case DW_CFA_def_cfa_sf:
2047     case DW_CFA_offset:
2048     case DW_CFA_offset_extended_sf:
2049     case DW_CFA_offset_extended:
2050       return dw_cfi_oprnd_offset;
2051
2052     case DW_CFA_register:
2053       return dw_cfi_oprnd_reg_num;
2054
2055     default:
2056       return dw_cfi_oprnd_unused;
2057     }
2058 }
2059
2060 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2061
2062 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2063    switch to the data section instead, and write out a synthetic label
2064    for collect2.  */
2065
2066 static void
2067 switch_to_eh_frame_section (void)
2068 {
2069   tree label;
2070
2071 #ifdef EH_FRAME_SECTION_NAME
2072   if (eh_frame_section == 0)
2073     {
2074       int flags;
2075
2076       if (EH_TABLES_CAN_BE_READ_ONLY)
2077         {
2078           int fde_encoding;
2079           int per_encoding;
2080           int lsda_encoding;
2081
2082           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2083                                                        /*global=*/0);
2084           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2085                                                        /*global=*/1);
2086           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2087                                                         /*global=*/0);
2088           flags = ((! flag_pic
2089                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2090                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2091                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2092                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2093                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2094                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2095                    ? 0 : SECTION_WRITE);
2096         }
2097       else
2098         flags = SECTION_WRITE;
2099       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2100     }
2101 #endif
2102
2103   if (eh_frame_section)
2104     switch_to_section (eh_frame_section);
2105   else
2106     {
2107       /* We have no special eh_frame section.  Put the information in
2108          the data section and emit special labels to guide collect2.  */
2109       switch_to_section (data_section);
2110       label = get_file_function_name ("F");
2111       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2112       targetm.asm_out.globalize_label (asm_out_file,
2113                                        IDENTIFIER_POINTER (label));
2114       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2115     }
2116 }
2117
2118 /* Output a Call Frame Information opcode and its operand(s).  */
2119
2120 static void
2121 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2122 {
2123   unsigned long r;
2124   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2125     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2126                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2127                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2128                          ((unsigned HOST_WIDE_INT)
2129                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2130   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2131     {
2132       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2133       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2134                            "DW_CFA_offset, column 0x%lx", r);
2135       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2136     }
2137   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2138     {
2139       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2140       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2141                            "DW_CFA_restore, column 0x%lx", r);
2142     }
2143   else
2144     {
2145       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2146                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2147
2148       switch (cfi->dw_cfi_opc)
2149         {
2150         case DW_CFA_set_loc:
2151           if (for_eh)
2152             dw2_asm_output_encoded_addr_rtx (
2153                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2154                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2155                 false, NULL);
2156           else
2157             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2158                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2159           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2160           break;
2161
2162         case DW_CFA_advance_loc1:
2163           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2164                                 fde->dw_fde_current_label, NULL);
2165           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2166           break;
2167
2168         case DW_CFA_advance_loc2:
2169           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2170                                 fde->dw_fde_current_label, NULL);
2171           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2172           break;
2173
2174         case DW_CFA_advance_loc4:
2175           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2176                                 fde->dw_fde_current_label, NULL);
2177           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2178           break;
2179
2180         case DW_CFA_MIPS_advance_loc8:
2181           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2182                                 fde->dw_fde_current_label, NULL);
2183           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2184           break;
2185
2186         case DW_CFA_offset_extended:
2187         case DW_CFA_def_cfa:
2188           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2189           dw2_asm_output_data_uleb128 (r, NULL);
2190           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2191           break;
2192
2193         case DW_CFA_offset_extended_sf:
2194         case DW_CFA_def_cfa_sf:
2195           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2196           dw2_asm_output_data_uleb128 (r, NULL);
2197           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2198           break;
2199
2200         case DW_CFA_restore_extended:
2201         case DW_CFA_undefined:
2202         case DW_CFA_same_value:
2203         case DW_CFA_def_cfa_register:
2204           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2205           dw2_asm_output_data_uleb128 (r, NULL);
2206           break;
2207
2208         case DW_CFA_register:
2209           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2210           dw2_asm_output_data_uleb128 (r, NULL);
2211           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2212           dw2_asm_output_data_uleb128 (r, NULL);
2213           break;
2214
2215         case DW_CFA_def_cfa_offset:
2216         case DW_CFA_GNU_args_size:
2217           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2218           break;
2219
2220         case DW_CFA_def_cfa_offset_sf:
2221           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2222           break;
2223
2224         case DW_CFA_GNU_window_save:
2225           break;
2226
2227         case DW_CFA_def_cfa_expression:
2228         case DW_CFA_expression:
2229           output_cfa_loc (cfi);
2230           break;
2231
2232         case DW_CFA_GNU_negative_offset_extended:
2233           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2234           gcc_unreachable ();
2235
2236         default:
2237           break;
2238         }
2239     }
2240 }
2241
2242 /* Output the call frame information used to record information
2243    that relates to calculating the frame pointer, and records the
2244    location of saved registers.  */
2245
2246 static void
2247 output_call_frame_info (int for_eh)
2248 {
2249   unsigned int i;
2250   dw_fde_ref fde;
2251   dw_cfi_ref cfi;
2252   char l1[20], l2[20], section_start_label[20];
2253   bool any_lsda_needed = false;
2254   char augmentation[6];
2255   int augmentation_size;
2256   int fde_encoding = DW_EH_PE_absptr;
2257   int per_encoding = DW_EH_PE_absptr;
2258   int lsda_encoding = DW_EH_PE_absptr;
2259   int return_reg;
2260
2261   /* Don't emit a CIE if there won't be any FDEs.  */
2262   if (fde_table_in_use == 0)
2263     return;
2264
2265   /* If we make FDEs linkonce, we may have to emit an empty label for
2266      an FDE that wouldn't otherwise be emitted.  We want to avoid
2267      having an FDE kept around when the function it refers to is
2268      discarded.  Example where this matters: a primary function
2269      template in C++ requires EH information, but an explicit
2270      specialization doesn't.  */
2271   if (TARGET_USES_WEAK_UNWIND_INFO
2272       && ! flag_asynchronous_unwind_tables
2273       && flag_exceptions
2274       && for_eh)
2275     for (i = 0; i < fde_table_in_use; i++)
2276       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2277           && !fde_table[i].uses_eh_lsda
2278           && ! DECL_WEAK (fde_table[i].decl))
2279         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2280                                       for_eh, /* empty */ 1);
2281
2282   /* If we don't have any functions we'll want to unwind out of, don't
2283      emit any EH unwind information.  Note that if exceptions aren't
2284      enabled, we won't have collected nothrow information, and if we
2285      asked for asynchronous tables, we always want this info.  */
2286   if (for_eh)
2287     {
2288       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2289
2290       for (i = 0; i < fde_table_in_use; i++)
2291         if (fde_table[i].uses_eh_lsda)
2292           any_eh_needed = any_lsda_needed = true;
2293         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2294           any_eh_needed = true;
2295         else if (! fde_table[i].nothrow
2296                  && ! fde_table[i].all_throwers_are_sibcalls)
2297           any_eh_needed = true;
2298
2299       if (! any_eh_needed)
2300         return;
2301     }
2302
2303   /* We're going to be generating comments, so turn on app.  */
2304   if (flag_debug_asm)
2305     app_enable ();
2306
2307   if (for_eh)
2308     switch_to_eh_frame_section ();
2309   else
2310     {
2311       if (!debug_frame_section)
2312         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2313                                            SECTION_DEBUG, NULL);
2314       switch_to_section (debug_frame_section);
2315     }
2316
2317   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2318   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2319
2320   /* Output the CIE.  */
2321   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2322   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2323   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2324     dw2_asm_output_data (4, 0xffffffff,
2325       "Initial length escape value indicating 64-bit DWARF extension");
2326   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2327                         "Length of Common Information Entry");
2328   ASM_OUTPUT_LABEL (asm_out_file, l1);
2329
2330   /* Now that the CIE pointer is PC-relative for EH,
2331      use 0 to identify the CIE.  */
2332   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2333                        (for_eh ? 0 : DWARF_CIE_ID),
2334                        "CIE Identifier Tag");
2335
2336   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2337
2338   augmentation[0] = 0;
2339   augmentation_size = 0;
2340   if (for_eh)
2341     {
2342       char *p;
2343
2344       /* Augmentation:
2345          z      Indicates that a uleb128 is present to size the
2346                 augmentation section.
2347          L      Indicates the encoding (and thus presence) of
2348                 an LSDA pointer in the FDE augmentation.
2349          R      Indicates a non-default pointer encoding for
2350                 FDE code pointers.
2351          P      Indicates the presence of an encoding + language
2352                 personality routine in the CIE augmentation.  */
2353
2354       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2355       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2356       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2357
2358       p = augmentation + 1;
2359       if (eh_personality_libfunc)
2360         {
2361           *p++ = 'P';
2362           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2363           assemble_external_libcall (eh_personality_libfunc);
2364         }
2365       if (any_lsda_needed)
2366         {
2367           *p++ = 'L';
2368           augmentation_size += 1;
2369         }
2370       if (fde_encoding != DW_EH_PE_absptr)
2371         {
2372           *p++ = 'R';
2373           augmentation_size += 1;
2374         }
2375       if (p > augmentation + 1)
2376         {
2377           augmentation[0] = 'z';
2378           *p = '\0';
2379         }
2380
2381       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2382       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2383         {
2384           int offset = (  4             /* Length */
2385                         + 4             /* CIE Id */
2386                         + 1             /* CIE version */
2387                         + strlen (augmentation) + 1     /* Augmentation */
2388                         + size_of_uleb128 (1)           /* Code alignment */
2389                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2390                         + 1             /* RA column */
2391                         + 1             /* Augmentation size */
2392                         + 1             /* Personality encoding */ );
2393           int pad = -offset & (PTR_SIZE - 1);
2394
2395           augmentation_size += pad;
2396
2397           /* Augmentations should be small, so there's scarce need to
2398              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2399           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2400         }
2401     }
2402
2403   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2404   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2405   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2406                                "CIE Data Alignment Factor");
2407
2408   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2409   if (DW_CIE_VERSION == 1)
2410     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2411   else
2412     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2413
2414   if (augmentation[0])
2415     {
2416       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2417       if (eh_personality_libfunc)
2418         {
2419           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2420                                eh_data_format_name (per_encoding));
2421           dw2_asm_output_encoded_addr_rtx (per_encoding,
2422                                            eh_personality_libfunc,
2423                                            true, NULL);
2424         }
2425
2426       if (any_lsda_needed)
2427         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2428                              eh_data_format_name (lsda_encoding));
2429
2430       if (fde_encoding != DW_EH_PE_absptr)
2431         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2432                              eh_data_format_name (fde_encoding));
2433     }
2434
2435   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2436     output_cfi (cfi, NULL, for_eh);
2437
2438   /* Pad the CIE out to an address sized boundary.  */
2439   ASM_OUTPUT_ALIGN (asm_out_file,
2440                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2441   ASM_OUTPUT_LABEL (asm_out_file, l2);
2442
2443   /* Loop through all of the FDE's.  */
2444   for (i = 0; i < fde_table_in_use; i++)
2445     {
2446       fde = &fde_table[i];
2447
2448       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2449       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2450           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2451           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2452           && !fde->uses_eh_lsda)
2453         continue;
2454
2455       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2456       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2457       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2458       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2459       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2460         dw2_asm_output_data (4, 0xffffffff,
2461                              "Initial length escape value indicating 64-bit DWARF extension");
2462       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2463                             "FDE Length");
2464       ASM_OUTPUT_LABEL (asm_out_file, l1);
2465
2466       if (for_eh)
2467         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2468       else
2469         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2470                                debug_frame_section, "FDE CIE offset");
2471
2472       if (for_eh)
2473         {
2474           if (fde->dw_fde_switched_sections)
2475             {
2476               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2477                                       fde->dw_fde_unlikely_section_label);
2478               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2479                                       fde->dw_fde_hot_section_label);
2480               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2481               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2482               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2483                                                "FDE initial location");
2484               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2485                                     fde->dw_fde_hot_section_end_label,
2486                                     fde->dw_fde_hot_section_label,
2487                                     "FDE address range");
2488               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2489                                                "FDE initial location");
2490               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2491                                     fde->dw_fde_unlikely_section_end_label,
2492                                     fde->dw_fde_unlikely_section_label,
2493                                     "FDE address range");
2494             }
2495           else
2496             {
2497               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2498               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2499               dw2_asm_output_encoded_addr_rtx (fde_encoding,
2500                                                sym_ref,
2501                                                false,
2502                                                "FDE initial location");
2503               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2504                                     fde->dw_fde_end, fde->dw_fde_begin,
2505                                     "FDE address range");
2506             }
2507         }
2508       else
2509         {
2510           if (fde->dw_fde_switched_sections)
2511             {
2512               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2513                                    fde->dw_fde_hot_section_label,
2514                                    "FDE initial location");
2515               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2516                                     fde->dw_fde_hot_section_end_label,
2517                                     fde->dw_fde_hot_section_label,
2518                                     "FDE address range");
2519               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2520                                    fde->dw_fde_unlikely_section_label,
2521                                    "FDE initial location");
2522               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2523                                     fde->dw_fde_unlikely_section_end_label,
2524                                     fde->dw_fde_unlikely_section_label,
2525                                     "FDE address range");
2526             }
2527           else
2528             {
2529               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2530                                    "FDE initial location");
2531               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2532                                     fde->dw_fde_end, fde->dw_fde_begin,
2533                                     "FDE address range");
2534             }
2535         }
2536
2537       if (augmentation[0])
2538         {
2539           if (any_lsda_needed)
2540             {
2541               int size = size_of_encoded_value (lsda_encoding);
2542
2543               if (lsda_encoding == DW_EH_PE_aligned)
2544                 {
2545                   int offset = (  4             /* Length */
2546                                 + 4             /* CIE offset */
2547                                 + 2 * size_of_encoded_value (fde_encoding)
2548                                 + 1             /* Augmentation size */ );
2549                   int pad = -offset & (PTR_SIZE - 1);
2550
2551                   size += pad;
2552                   gcc_assert (size_of_uleb128 (size) == 1);
2553                 }
2554
2555               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2556
2557               if (fde->uses_eh_lsda)
2558                 {
2559                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2560                                                fde->funcdef_number);
2561                   dw2_asm_output_encoded_addr_rtx (
2562                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2563                         false, "Language Specific Data Area");
2564                 }
2565               else
2566                 {
2567                   if (lsda_encoding == DW_EH_PE_aligned)
2568                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2569                   dw2_asm_output_data
2570                     (size_of_encoded_value (lsda_encoding), 0,
2571                      "Language Specific Data Area (none)");
2572                 }
2573             }
2574           else
2575             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2576         }
2577
2578       /* Loop through the Call Frame Instructions associated with
2579          this FDE.  */
2580       fde->dw_fde_current_label = fde->dw_fde_begin;
2581       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2582         output_cfi (cfi, fde, for_eh);
2583
2584       /* Pad the FDE out to an address sized boundary.  */
2585       ASM_OUTPUT_ALIGN (asm_out_file,
2586                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2587       ASM_OUTPUT_LABEL (asm_out_file, l2);
2588     }
2589
2590   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2591     dw2_asm_output_data (4, 0, "End of Table");
2592 #ifdef MIPS_DEBUGGING_INFO
2593   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2594      get a value of 0.  Putting .align 0 after the label fixes it.  */
2595   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2596 #endif
2597
2598   /* Turn off app to make assembly quicker.  */
2599   if (flag_debug_asm)
2600     app_disable ();
2601 }
2602
2603 /* Output a marker (i.e. a label) for the beginning of a function, before
2604    the prologue.  */
2605
2606 void
2607 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2608                           const char *file ATTRIBUTE_UNUSED)
2609 {
2610   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2611   char * dup_label;
2612   dw_fde_ref fde;
2613
2614   current_function_func_begin_label = NULL;
2615
2616 #ifdef TARGET_UNWIND_INFO
2617   /* ??? current_function_func_begin_label is also used by except.c
2618      for call-site information.  We must emit this label if it might
2619      be used.  */
2620   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2621       && ! dwarf2out_do_frame ())
2622     return;
2623 #else
2624   if (! dwarf2out_do_frame ())
2625     return;
2626 #endif
2627
2628   switch_to_section (function_section (current_function_decl));
2629   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2630                                current_function_funcdef_no);
2631   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2632                           current_function_funcdef_no);
2633   dup_label = xstrdup (label);
2634   current_function_func_begin_label = dup_label;
2635
2636 #ifdef TARGET_UNWIND_INFO
2637   /* We can elide the fde allocation if we're not emitting debug info.  */
2638   if (! dwarf2out_do_frame ())
2639     return;
2640 #endif
2641
2642   /* Expand the fde table if necessary.  */
2643   if (fde_table_in_use == fde_table_allocated)
2644     {
2645       fde_table_allocated += FDE_TABLE_INCREMENT;
2646       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
2647       memset (fde_table + fde_table_in_use, 0,
2648               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2649     }
2650
2651   /* Record the FDE associated with this function.  */
2652   current_funcdef_fde = fde_table_in_use;
2653
2654   /* Add the new FDE at the end of the fde_table.  */
2655   fde = &fde_table[fde_table_in_use++];
2656   fde->decl = current_function_decl;
2657   fde->dw_fde_begin = dup_label;
2658   fde->dw_fde_current_label = dup_label;
2659   fde->dw_fde_hot_section_label = NULL;
2660   fde->dw_fde_hot_section_end_label = NULL;
2661   fde->dw_fde_unlikely_section_label = NULL;
2662   fde->dw_fde_unlikely_section_end_label = NULL;
2663   fde->dw_fde_switched_sections = false;
2664   fde->dw_fde_end = NULL;
2665   fde->dw_fde_cfi = NULL;
2666   fde->funcdef_number = current_function_funcdef_no;
2667   fde->nothrow = TREE_NOTHROW (current_function_decl);
2668   fde->uses_eh_lsda = crtl->uses_eh_lsda;
2669   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
2670
2671   args_size = old_args_size = 0;
2672
2673   /* We only want to output line number information for the genuine dwarf2
2674      prologue case, not the eh frame case.  */
2675 #ifdef DWARF2_DEBUGGING_INFO
2676   if (file)
2677     dwarf2out_source_line (line, file);
2678 #endif
2679 }
2680
2681 /* Output a marker (i.e. a label) for the absolute end of the generated code
2682    for a function definition.  This gets called *after* the epilogue code has
2683    been generated.  */
2684
2685 void
2686 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2687                         const char *file ATTRIBUTE_UNUSED)
2688 {
2689   dw_fde_ref fde;
2690   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2691
2692   /* Output a label to mark the endpoint of the code generated for this
2693      function.  */
2694   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2695                                current_function_funcdef_no);
2696   ASM_OUTPUT_LABEL (asm_out_file, label);
2697   fde = current_fde ();
2698   gcc_assert (fde != NULL);
2699   fde->dw_fde_end = xstrdup (label);
2700 }
2701
2702 void
2703 dwarf2out_frame_init (void)
2704 {
2705   /* Allocate the initial hunk of the fde_table.  */
2706   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
2707   fde_table_allocated = FDE_TABLE_INCREMENT;
2708   fde_table_in_use = 0;
2709
2710   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2711      sake of lookup_cfa.  */
2712
2713   /* On entry, the Canonical Frame Address is at SP.  */
2714   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2715
2716 #ifdef DWARF2_UNWIND_INFO
2717   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2718     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2719 #endif
2720 }
2721
2722 void
2723 dwarf2out_frame_finish (void)
2724 {
2725   /* Output call frame information.  */
2726   if (DWARF2_FRAME_INFO)
2727     output_call_frame_info (0);
2728
2729 #ifndef TARGET_UNWIND_INFO
2730   /* Output another copy for the unwinder.  */
2731   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2732     output_call_frame_info (1);
2733 #endif
2734 }
2735
2736 /* Note that the current function section is being used for code.  */
2737
2738 static void
2739 dwarf2out_note_section_used (void)
2740 {
2741   section *sec = current_function_section ();
2742   if (sec == text_section)
2743     text_section_used = true;
2744   else if (sec == cold_text_section)
2745     cold_text_section_used = true;
2746 }
2747
2748 void
2749 dwarf2out_switch_text_section (void)
2750 {
2751   dw_fde_ref fde = current_fde ();
2752
2753   gcc_assert (cfun && fde);
2754
2755   fde->dw_fde_switched_sections = true;
2756   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
2757   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
2758   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
2759   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
2760   have_multiple_function_sections = true;
2761
2762   /* Reset the current label on switching text sections, so that we
2763      don't attempt to advance_loc4 between labels in different sections.  */
2764   fde->dw_fde_current_label = NULL;
2765
2766   /* There is no need to mark used sections when not debugging.  */
2767   if (cold_text_section != NULL)
2768     dwarf2out_note_section_used ();
2769 }
2770 #endif
2771 \f
2772 /* And now, the subset of the debugging information support code necessary
2773    for emitting location expressions.  */
2774
2775 /* Data about a single source file.  */
2776 struct dwarf_file_data GTY(())
2777 {
2778   const char * filename;
2779   int emitted_number;
2780 };
2781
2782 /* We need some way to distinguish DW_OP_addr with a direct symbol
2783    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2784 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2785
2786
2787 typedef struct dw_val_struct *dw_val_ref;
2788 typedef struct die_struct *dw_die_ref;
2789 typedef const struct die_struct *const_dw_die_ref;
2790 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2791 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2792
2793 /* Each DIE may have a series of attribute/value pairs.  Values
2794    can take on several forms.  The forms that are used in this
2795    implementation are listed below.  */
2796
2797 enum dw_val_class
2798 {
2799   dw_val_class_addr,
2800   dw_val_class_offset,
2801   dw_val_class_loc,
2802   dw_val_class_loc_list,
2803   dw_val_class_range_list,
2804   dw_val_class_const,
2805   dw_val_class_unsigned_const,
2806   dw_val_class_long_long,
2807   dw_val_class_vec,
2808   dw_val_class_flag,
2809   dw_val_class_die_ref,
2810   dw_val_class_fde_ref,
2811   dw_val_class_lbl_id,
2812   dw_val_class_lineptr,
2813   dw_val_class_str,
2814   dw_val_class_macptr,
2815   dw_val_class_file
2816 };
2817
2818 /* Describe a double word constant value.  */
2819 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2820
2821 typedef struct dw_long_long_struct GTY(())
2822 {
2823   unsigned long hi;
2824   unsigned long low;
2825 }
2826 dw_long_long_const;
2827
2828 /* Describe a floating point constant value, or a vector constant value.  */
2829
2830 typedef struct dw_vec_struct GTY(())
2831 {
2832   unsigned char * GTY((length ("%h.length"))) array;
2833   unsigned length;
2834   unsigned elt_size;
2835 }
2836 dw_vec_const;
2837
2838 /* The dw_val_node describes an attribute's value, as it is
2839    represented internally.  */
2840
2841 typedef struct dw_val_struct GTY(())
2842 {
2843   enum dw_val_class val_class;
2844   union dw_val_struct_union
2845     {
2846       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2847       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2848       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2849       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2850       HOST_WIDE_INT GTY ((default)) val_int;
2851       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2852       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2853       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2854       struct dw_val_die_union
2855         {
2856           dw_die_ref die;
2857           int external;
2858         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2859       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2860       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2861       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2862       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2863       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2864     }
2865   GTY ((desc ("%1.val_class"))) v;
2866 }
2867 dw_val_node;
2868
2869 /* Locations in memory are described using a sequence of stack machine
2870    operations.  */
2871
2872 typedef struct dw_loc_descr_struct GTY(())
2873 {
2874   dw_loc_descr_ref dw_loc_next;
2875   enum dwarf_location_atom dw_loc_opc;
2876   dw_val_node dw_loc_oprnd1;
2877   dw_val_node dw_loc_oprnd2;
2878   int dw_loc_addr;
2879 }
2880 dw_loc_descr_node;
2881
2882 /* Location lists are ranges + location descriptions for that range,
2883    so you can track variables that are in different places over
2884    their entire life.  */
2885 typedef struct dw_loc_list_struct GTY(())
2886 {
2887   dw_loc_list_ref dw_loc_next;
2888   const char *begin; /* Label for begin address of range */
2889   const char *end;  /* Label for end address of range */
2890   char *ll_symbol; /* Label for beginning of location list.
2891                       Only on head of list */
2892   const char *section; /* Section this loclist is relative to */
2893   dw_loc_descr_ref expr;
2894 } dw_loc_list_node;
2895
2896 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2897
2898 static const char *dwarf_stack_op_name (unsigned);
2899 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2900                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2901 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2902 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2903 static unsigned long size_of_locs (dw_loc_descr_ref);
2904 static void output_loc_operands (dw_loc_descr_ref);
2905 static void output_loc_sequence (dw_loc_descr_ref);
2906
2907 /* Convert a DWARF stack opcode into its string name.  */
2908
2909 static const char *
2910 dwarf_stack_op_name (unsigned int op)
2911 {
2912   switch (op)
2913     {
2914     case DW_OP_addr:
2915     case INTERNAL_DW_OP_tls_addr:
2916       return "DW_OP_addr";
2917     case DW_OP_deref:
2918       return "DW_OP_deref";
2919     case DW_OP_const1u:
2920       return "DW_OP_const1u";
2921     case DW_OP_const1s:
2922       return "DW_OP_const1s";
2923     case DW_OP_const2u:
2924       return "DW_OP_const2u";
2925     case DW_OP_const2s:
2926       return "DW_OP_const2s";
2927     case DW_OP_const4u:
2928       return "DW_OP_const4u";
2929     case DW_OP_const4s:
2930       return "DW_OP_const4s";
2931     case DW_OP_const8u:
2932       return "DW_OP_const8u";
2933     case DW_OP_const8s:
2934       return "DW_OP_const8s";
2935     case DW_OP_constu:
2936       return "DW_OP_constu";
2937     case DW_OP_consts:
2938       return "DW_OP_consts";
2939     case DW_OP_dup:
2940       return "DW_OP_dup";
2941     case DW_OP_drop:
2942       return "DW_OP_drop";
2943     case DW_OP_over:
2944       return "DW_OP_over";
2945     case DW_OP_pick:
2946       return "DW_OP_pick";
2947     case DW_OP_swap:
2948       return "DW_OP_swap";
2949     case DW_OP_rot:
2950       return "DW_OP_rot";
2951     case DW_OP_xderef:
2952       return "DW_OP_xderef";
2953     case DW_OP_abs:
2954       return "DW_OP_abs";
2955     case DW_OP_and:
2956       return "DW_OP_and";
2957     case DW_OP_div:
2958       return "DW_OP_div";
2959     case DW_OP_minus:
2960       return "DW_OP_minus";
2961     case DW_OP_mod:
2962       return "DW_OP_mod";
2963     case DW_OP_mul:
2964       return "DW_OP_mul";
2965     case DW_OP_neg:
2966       return "DW_OP_neg";
2967     case DW_OP_not:
2968       return "DW_OP_not";
2969     case DW_OP_or:
2970       return "DW_OP_or";
2971     case DW_OP_plus:
2972       return "DW_OP_plus";
2973     case DW_OP_plus_uconst:
2974       return "DW_OP_plus_uconst";
2975     case DW_OP_shl:
2976       return "DW_OP_shl";
2977     case DW_OP_shr:
2978       return "DW_OP_shr";
2979     case DW_OP_shra:
2980       return "DW_OP_shra";
2981     case DW_OP_xor:
2982       return "DW_OP_xor";
2983     case DW_OP_bra:
2984       return "DW_OP_bra";
2985     case DW_OP_eq:
2986       return "DW_OP_eq";
2987     case DW_OP_ge:
2988       return "DW_OP_ge";
2989     case DW_OP_gt:
2990       return "DW_OP_gt";
2991     case DW_OP_le:
2992       return "DW_OP_le";
2993     case DW_OP_lt:
2994       return "DW_OP_lt";
2995     case DW_OP_ne:
2996       return "DW_OP_ne";
2997     case DW_OP_skip:
2998       return "DW_OP_skip";
2999     case DW_OP_lit0:
3000       return "DW_OP_lit0";
3001     case DW_OP_lit1:
3002       return "DW_OP_lit1";
3003     case DW_OP_lit2:
3004       return "DW_OP_lit2";
3005     case DW_OP_lit3:
3006       return "DW_OP_lit3";
3007     case DW_OP_lit4:
3008       return "DW_OP_lit4";
3009     case DW_OP_lit5:
3010       return "DW_OP_lit5";
3011     case DW_OP_lit6:
3012       return "DW_OP_lit6";
3013     case DW_OP_lit7:
3014       return "DW_OP_lit7";
3015     case DW_OP_lit8:
3016       return "DW_OP_lit8";
3017     case DW_OP_lit9:
3018       return "DW_OP_lit9";
3019     case DW_OP_lit10:
3020       return "DW_OP_lit10";
3021     case DW_OP_lit11:
3022       return "DW_OP_lit11";
3023     case DW_OP_lit12:
3024       return "DW_OP_lit12";
3025     case DW_OP_lit13:
3026       return "DW_OP_lit13";
3027     case DW_OP_lit14:
3028       return "DW_OP_lit14";
3029     case DW_OP_lit15:
3030       return "DW_OP_lit15";
3031     case DW_OP_lit16:
3032       return "DW_OP_lit16";
3033     case DW_OP_lit17:
3034       return "DW_OP_lit17";
3035     case DW_OP_lit18:
3036       return "DW_OP_lit18";
3037     case DW_OP_lit19:
3038       return "DW_OP_lit19";
3039     case DW_OP_lit20:
3040       return "DW_OP_lit20";
3041     case DW_OP_lit21:
3042       return "DW_OP_lit21";
3043     case DW_OP_lit22:
3044       return "DW_OP_lit22";
3045     case DW_OP_lit23:
3046       return "DW_OP_lit23";
3047     case DW_OP_lit24:
3048       return "DW_OP_lit24";
3049     case DW_OP_lit25:
3050       return "DW_OP_lit25";
3051     case DW_OP_lit26:
3052       return "DW_OP_lit26";
3053     case DW_OP_lit27:
3054       return "DW_OP_lit27";
3055     case DW_OP_lit28:
3056       return "DW_OP_lit28";
3057     case DW_OP_lit29:
3058       return "DW_OP_lit29";
3059     case DW_OP_lit30:
3060       return "DW_OP_lit30";
3061     case DW_OP_lit31:
3062       return "DW_OP_lit31";
3063     case DW_OP_reg0:
3064       return "DW_OP_reg0";
3065     case DW_OP_reg1:
3066       return "DW_OP_reg1";
3067     case DW_OP_reg2:
3068       return "DW_OP_reg2";
3069     case DW_OP_reg3:
3070       return "DW_OP_reg3";
3071     case DW_OP_reg4:
3072       return "DW_OP_reg4";
3073     case DW_OP_reg5:
3074       return "DW_OP_reg5";
3075     case DW_OP_reg6:
3076       return "DW_OP_reg6";
3077     case DW_OP_reg7:
3078       return "DW_OP_reg7";
3079     case DW_OP_reg8:
3080       return "DW_OP_reg8";
3081     case DW_OP_reg9:
3082       return "DW_OP_reg9";
3083     case DW_OP_reg10:
3084       return "DW_OP_reg10";
3085     case DW_OP_reg11:
3086       return "DW_OP_reg11";
3087     case DW_OP_reg12:
3088       return "DW_OP_reg12";
3089     case DW_OP_reg13:
3090       return "DW_OP_reg13";
3091     case DW_OP_reg14:
3092       return "DW_OP_reg14";
3093     case DW_OP_reg15:
3094       return "DW_OP_reg15";
3095     case DW_OP_reg16:
3096       return "DW_OP_reg16";
3097     case DW_OP_reg17:
3098       return "DW_OP_reg17";
3099     case DW_OP_reg18:
3100       return "DW_OP_reg18";
3101     case DW_OP_reg19:
3102       return "DW_OP_reg19";
3103     case DW_OP_reg20:
3104       return "DW_OP_reg20";
3105     case DW_OP_reg21:
3106       return "DW_OP_reg21";
3107     case DW_OP_reg22:
3108       return "DW_OP_reg22";
3109     case DW_OP_reg23:
3110       return "DW_OP_reg23";
3111     case DW_OP_reg24:
3112       return "DW_OP_reg24";
3113     case DW_OP_reg25:
3114       return "DW_OP_reg25";
3115     case DW_OP_reg26:
3116       return "DW_OP_reg26";
3117     case DW_OP_reg27:
3118       return "DW_OP_reg27";
3119     case DW_OP_reg28:
3120       return "DW_OP_reg28";
3121     case DW_OP_reg29:
3122       return "DW_OP_reg29";
3123     case DW_OP_reg30:
3124       return "DW_OP_reg30";
3125     case DW_OP_reg31:
3126       return "DW_OP_reg31";
3127     case DW_OP_breg0:
3128       return "DW_OP_breg0";
3129     case DW_OP_breg1:
3130       return "DW_OP_breg1";
3131     case DW_OP_breg2:
3132       return "DW_OP_breg2";
3133     case DW_OP_breg3:
3134       return "DW_OP_breg3";
3135     case DW_OP_breg4:
3136       return "DW_OP_breg4";
3137     case DW_OP_breg5:
3138       return "DW_OP_breg5";
3139     case DW_OP_breg6:
3140       return "DW_OP_breg6";
3141     case DW_OP_breg7:
3142       return "DW_OP_breg7";
3143     case DW_OP_breg8:
3144       return "DW_OP_breg8";
3145     case DW_OP_breg9:
3146       return "DW_OP_breg9";
3147     case DW_OP_breg10:
3148       return "DW_OP_breg10";
3149     case DW_OP_breg11:
3150       return "DW_OP_breg11";
3151     case DW_OP_breg12:
3152       return "DW_OP_breg12";
3153     case DW_OP_breg13:
3154       return "DW_OP_breg13";
3155     case DW_OP_breg14:
3156       return "DW_OP_breg14";
3157     case DW_OP_breg15:
3158       return "DW_OP_breg15";
3159     case DW_OP_breg16:
3160       return "DW_OP_breg16";
3161     case DW_OP_breg17:
3162       return "DW_OP_breg17";
3163     case DW_OP_breg18:
3164       return "DW_OP_breg18";
3165     case DW_OP_breg19:
3166       return "DW_OP_breg19";
3167     case DW_OP_breg20:
3168       return "DW_OP_breg20";
3169     case DW_OP_breg21:
3170       return "DW_OP_breg21";
3171     case DW_OP_breg22:
3172       return "DW_OP_breg22";
3173     case DW_OP_breg23:
3174       return "DW_OP_breg23";
3175     case DW_OP_breg24:
3176       return "DW_OP_breg24";
3177     case DW_OP_breg25:
3178       return "DW_OP_breg25";
3179     case DW_OP_breg26:
3180       return "DW_OP_breg26";
3181     case DW_OP_breg27:
3182       return "DW_OP_breg27";
3183     case DW_OP_breg28:
3184       return "DW_OP_breg28";
3185     case DW_OP_breg29:
3186       return "DW_OP_breg29";
3187     case DW_OP_breg30:
3188       return "DW_OP_breg30";
3189     case DW_OP_breg31:
3190       return "DW_OP_breg31";
3191     case DW_OP_regx:
3192       return "DW_OP_regx";
3193     case DW_OP_fbreg:
3194       return "DW_OP_fbreg";
3195     case DW_OP_bregx:
3196       return "DW_OP_bregx";
3197     case DW_OP_piece:
3198       return "DW_OP_piece";
3199     case DW_OP_deref_size:
3200       return "DW_OP_deref_size";
3201     case DW_OP_xderef_size:
3202       return "DW_OP_xderef_size";
3203     case DW_OP_nop:
3204       return "DW_OP_nop";
3205     case DW_OP_push_object_address:
3206       return "DW_OP_push_object_address";
3207     case DW_OP_call2:
3208       return "DW_OP_call2";
3209     case DW_OP_call4:
3210       return "DW_OP_call4";
3211     case DW_OP_call_ref:
3212       return "DW_OP_call_ref";
3213     case DW_OP_GNU_push_tls_address:
3214       return "DW_OP_GNU_push_tls_address";
3215     case DW_OP_GNU_uninit:
3216       return "DW_OP_GNU_uninit";
3217     default:
3218       return "OP_<unknown>";
3219     }
3220 }
3221
3222 /* Return a pointer to a newly allocated location description.  Location
3223    descriptions are simple expression terms that can be strung
3224    together to form more complicated location (address) descriptions.  */
3225
3226 static inline dw_loc_descr_ref
3227 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3228                unsigned HOST_WIDE_INT oprnd2)
3229 {
3230   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3231
3232   descr->dw_loc_opc = op;
3233   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3234   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3235   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3236   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3237
3238   return descr;
3239 }
3240
3241 /* Add a location description term to a location description expression.  */
3242
3243 static inline void
3244 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3245 {
3246   dw_loc_descr_ref *d;
3247
3248   /* Find the end of the chain.  */
3249   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3250     ;
3251
3252   *d = descr;
3253 }
3254
3255 /* Return the size of a location descriptor.  */
3256
3257 static unsigned long
3258 size_of_loc_descr (dw_loc_descr_ref loc)
3259 {
3260   unsigned long size = 1;
3261
3262   switch (loc->dw_loc_opc)
3263     {
3264     case DW_OP_addr:
3265     case INTERNAL_DW_OP_tls_addr:
3266       size += DWARF2_ADDR_SIZE;
3267       break;
3268     case DW_OP_const1u:
3269     case DW_OP_const1s:
3270       size += 1;
3271       break;
3272     case DW_OP_const2u:
3273     case DW_OP_const2s:
3274       size += 2;
3275       break;
3276     case DW_OP_const4u:
3277     case DW_OP_const4s:
3278       size += 4;
3279       break;
3280     case DW_OP_const8u:
3281     case DW_OP_const8s:
3282       size += 8;
3283       break;
3284     case DW_OP_constu:
3285       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3286       break;
3287     case DW_OP_consts:
3288       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3289       break;
3290     case DW_OP_pick:
3291       size += 1;
3292       break;
3293     case DW_OP_plus_uconst:
3294       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3295       break;
3296     case DW_OP_skip:
3297     case DW_OP_bra:
3298       size += 2;
3299       break;
3300     case DW_OP_breg0:
3301     case DW_OP_breg1:
3302     case DW_OP_breg2:
3303     case DW_OP_breg3:
3304     case DW_OP_breg4:
3305     case DW_OP_breg5:
3306     case DW_OP_breg6:
3307     case DW_OP_breg7:
3308     case DW_OP_breg8:
3309     case DW_OP_breg9:
3310     case DW_OP_breg10:
3311     case DW_OP_breg11:
3312     case DW_OP_breg12:
3313     case DW_OP_breg13:
3314     case DW_OP_breg14:
3315     case DW_OP_breg15:
3316     case DW_OP_breg16:
3317     case DW_OP_breg17:
3318     case DW_OP_breg18:
3319     case DW_OP_breg19:
3320     case DW_OP_breg20:
3321     case DW_OP_breg21:
3322     case DW_OP_breg22:
3323     case DW_OP_breg23:
3324     case DW_OP_breg24:
3325     case DW_OP_breg25:
3326     case DW_OP_breg26:
3327     case DW_OP_breg27:
3328     case DW_OP_breg28:
3329     case DW_OP_breg29:
3330     case DW_OP_breg30:
3331     case DW_OP_breg31:
3332       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3333       break;
3334     case DW_OP_regx:
3335       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3336       break;
3337     case DW_OP_fbreg:
3338       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3339       break;
3340     case DW_OP_bregx:
3341       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3342       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3343       break;
3344     case DW_OP_piece:
3345       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3346       break;
3347     case DW_OP_deref_size:
3348     case DW_OP_xderef_size:
3349       size += 1;
3350       break;
3351     case DW_OP_call2:
3352       size += 2;
3353       break;
3354     case DW_OP_call4:
3355       size += 4;
3356       break;
3357     case DW_OP_call_ref:
3358       size += DWARF2_ADDR_SIZE;
3359       break;
3360     default:
3361       break;
3362     }
3363
3364   return size;
3365 }
3366
3367 /* Return the size of a series of location descriptors.  */
3368
3369 static unsigned long
3370 size_of_locs (dw_loc_descr_ref loc)
3371 {
3372   dw_loc_descr_ref l;
3373   unsigned long size;
3374
3375   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3376      field, to avoid writing to a PCH file.  */
3377   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3378     {
3379       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3380         break;
3381       size += size_of_loc_descr (l);
3382     }
3383   if (! l)
3384     return size;
3385
3386   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3387     {
3388       l->dw_loc_addr = size;
3389       size += size_of_loc_descr (l);
3390     }
3391
3392   return size;
3393 }
3394
3395 /* Output location description stack opcode's operands (if any).  */
3396
3397 static void
3398 output_loc_operands (dw_loc_descr_ref loc)
3399 {
3400   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3401   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3402
3403   switch (loc->dw_loc_opc)
3404     {
3405 #ifdef DWARF2_DEBUGGING_INFO
3406     case DW_OP_addr:
3407       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3408       break;
3409     case DW_OP_const2u:
3410     case DW_OP_const2s:
3411       dw2_asm_output_data (2, val1->v.val_int, NULL);
3412       break;
3413     case DW_OP_const4u:
3414     case DW_OP_const4s:
3415       dw2_asm_output_data (4, val1->v.val_int, NULL);
3416       break;
3417     case DW_OP_const8u:
3418     case DW_OP_const8s:
3419       gcc_assert (HOST_BITS_PER_LONG >= 64);
3420       dw2_asm_output_data (8, val1->v.val_int, NULL);
3421       break;
3422     case DW_OP_skip:
3423     case DW_OP_bra:
3424       {
3425         int offset;
3426
3427         gcc_assert (val1->val_class == dw_val_class_loc);
3428         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3429
3430         dw2_asm_output_data (2, offset, NULL);
3431       }
3432       break;
3433 #else
3434     case DW_OP_addr:
3435     case DW_OP_const2u:
3436     case DW_OP_const2s:
3437     case DW_OP_const4u:
3438     case DW_OP_const4s:
3439     case DW_OP_const8u:
3440     case DW_OP_const8s:
3441     case DW_OP_skip:
3442     case DW_OP_bra:
3443       /* We currently don't make any attempt to make sure these are
3444          aligned properly like we do for the main unwind info, so
3445          don't support emitting things larger than a byte if we're
3446          only doing unwinding.  */
3447       gcc_unreachable ();
3448 #endif
3449     case DW_OP_const1u:
3450     case DW_OP_const1s:
3451       dw2_asm_output_data (1, val1->v.val_int, NULL);
3452       break;
3453     case DW_OP_constu:
3454       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3455       break;
3456     case DW_OP_consts:
3457       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3458       break;
3459     case DW_OP_pick:
3460       dw2_asm_output_data (1, val1->v.val_int, NULL);
3461       break;
3462     case DW_OP_plus_uconst:
3463       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3464       break;
3465     case DW_OP_breg0:
3466     case DW_OP_breg1:
3467     case DW_OP_breg2:
3468     case DW_OP_breg3:
3469     case DW_OP_breg4:
3470     case DW_OP_breg5:
3471     case DW_OP_breg6:
3472     case DW_OP_breg7:
3473     case DW_OP_breg8:
3474     case DW_OP_breg9:
3475     case DW_OP_breg10:
3476     case DW_OP_breg11:
3477     case DW_OP_breg12:
3478     case DW_OP_breg13:
3479     case DW_OP_breg14:
3480     case DW_OP_breg15:
3481     case DW_OP_breg16:
3482     case DW_OP_breg17:
3483     case DW_OP_breg18:
3484     case DW_OP_breg19:
3485     case DW_OP_breg20:
3486     case DW_OP_breg21:
3487     case DW_OP_breg22:
3488     case DW_OP_breg23:
3489     case DW_OP_breg24:
3490     case DW_OP_breg25:
3491     case DW_OP_breg26:
3492     case DW_OP_breg27:
3493     case DW_OP_breg28:
3494     case DW_OP_breg29:
3495     case DW_OP_breg30:
3496     case DW_OP_breg31:
3497       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3498       break;
3499     case DW_OP_regx:
3500       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3501       break;
3502     case DW_OP_fbreg:
3503       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3504       break;
3505     case DW_OP_bregx:
3506       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3507       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3508       break;
3509     case DW_OP_piece:
3510       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3511       break;
3512     case DW_OP_deref_size:
3513     case DW_OP_xderef_size:
3514       dw2_asm_output_data (1, val1->v.val_int, NULL);
3515       break;
3516
3517     case INTERNAL_DW_OP_tls_addr:
3518       if (targetm.asm_out.output_dwarf_dtprel)
3519         {
3520           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3521                                                DWARF2_ADDR_SIZE,
3522                                                val1->v.val_addr);
3523           fputc ('\n', asm_out_file);
3524         }
3525       else
3526         gcc_unreachable ();
3527       break;
3528
3529     default:
3530       /* Other codes have no operands.  */
3531       break;
3532     }
3533 }
3534
3535 /* Output a sequence of location operations.  */
3536
3537 static void
3538 output_loc_sequence (dw_loc_descr_ref loc)
3539 {
3540   for (; loc != NULL; loc = loc->dw_loc_next)
3541     {
3542       /* Output the opcode.  */
3543       dw2_asm_output_data (1, loc->dw_loc_opc,
3544                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3545
3546       /* Output the operand(s) (if any).  */
3547       output_loc_operands (loc);
3548     }
3549 }
3550
3551 /* This routine will generate the correct assembly data for a location
3552    description based on a cfi entry with a complex address.  */
3553
3554 static void
3555 output_cfa_loc (dw_cfi_ref cfi)
3556 {
3557   dw_loc_descr_ref loc;
3558   unsigned long size;
3559
3560   /* Output the size of the block.  */
3561   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3562   size = size_of_locs (loc);
3563   dw2_asm_output_data_uleb128 (size, NULL);
3564
3565   /* Now output the operations themselves.  */
3566   output_loc_sequence (loc);
3567 }
3568
3569 /* This function builds a dwarf location descriptor sequence from a
3570    dw_cfa_location, adding the given OFFSET to the result of the
3571    expression.  */
3572
3573 static struct dw_loc_descr_struct *
3574 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3575 {
3576   struct dw_loc_descr_struct *head, *tmp;
3577
3578   offset += cfa->offset;
3579
3580   if (cfa->indirect)
3581     {
3582       if (cfa->base_offset)
3583         {
3584           if (cfa->reg <= 31)
3585             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3586           else
3587             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3588         }
3589       else if (cfa->reg <= 31)
3590         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3591       else
3592         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3593
3594       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3595       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3596       add_loc_descr (&head, tmp);
3597       if (offset != 0)
3598         {
3599           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3600           add_loc_descr (&head, tmp);
3601         }
3602     }
3603   else
3604     {
3605       if (offset == 0)
3606         if (cfa->reg <= 31)
3607           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3608         else
3609           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3610       else if (cfa->reg <= 31)
3611         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3612       else
3613         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3614     }
3615
3616   return head;
3617 }
3618
3619 /* This function fills in aa dw_cfa_location structure from a dwarf location
3620    descriptor sequence.  */
3621
3622 static void
3623 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3624 {
3625   struct dw_loc_descr_struct *ptr;
3626   cfa->offset = 0;
3627   cfa->base_offset = 0;
3628   cfa->indirect = 0;
3629   cfa->reg = -1;
3630
3631   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3632     {
3633       enum dwarf_location_atom op = ptr->dw_loc_opc;
3634
3635       switch (op)
3636         {
3637         case DW_OP_reg0:
3638         case DW_OP_reg1:
3639         case DW_OP_reg2:
3640         case DW_OP_reg3:
3641         case DW_OP_reg4:
3642         case DW_OP_reg5:
3643         case DW_OP_reg6:
3644         case DW_OP_reg7:
3645         case DW_OP_reg8:
3646         case DW_OP_reg9:
3647         case DW_OP_reg10:
3648         case DW_OP_reg11:
3649         case DW_OP_reg12:
3650         case DW_OP_reg13:
3651         case DW_OP_reg14:
3652         case DW_OP_reg15:
3653         case DW_OP_reg16:
3654         case DW_OP_reg17:
3655         case DW_OP_reg18:
3656         case DW_OP_reg19:
3657         case DW_OP_reg20:
3658         case DW_OP_reg21:
3659         case DW_OP_reg22:
3660         case DW_OP_reg23:
3661         case DW_OP_reg24:
3662         case DW_OP_reg25:
3663         case DW_OP_reg26:
3664         case DW_OP_reg27:
3665         case DW_OP_reg28:
3666         case DW_OP_reg29:
3667         case DW_OP_reg30:
3668         case DW_OP_reg31:
3669           cfa->reg = op - DW_OP_reg0;
3670           break;
3671         case DW_OP_regx:
3672           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3673           break;
3674         case DW_OP_breg0:
3675         case DW_OP_breg1:
3676         case DW_OP_breg2:
3677         case DW_OP_breg3:
3678         case DW_OP_breg4:
3679         case DW_OP_breg5:
3680         case DW_OP_breg6:
3681         case DW_OP_breg7:
3682         case DW_OP_breg8:
3683         case DW_OP_breg9:
3684         case DW_OP_breg10:
3685         case DW_OP_breg11:
3686         case DW_OP_breg12:
3687         case DW_OP_breg13:
3688         case DW_OP_breg14:
3689         case DW_OP_breg15:
3690         case DW_OP_breg16:
3691         case DW_OP_breg17:
3692         case DW_OP_breg18:
3693         case DW_OP_breg19:
3694         case DW_OP_breg20:
3695         case DW_OP_breg21:
3696         case DW_OP_breg22:
3697         case DW_OP_breg23:
3698         case DW_OP_breg24:
3699         case DW_OP_breg25:
3700         case DW_OP_breg26:
3701         case DW_OP_breg27:
3702         case DW_OP_breg28:
3703         case DW_OP_breg29:
3704         case DW_OP_breg30:
3705         case DW_OP_breg31:
3706           cfa->reg = op - DW_OP_breg0;
3707           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3708           break;
3709         case DW_OP_bregx:
3710           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3711           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3712           break;
3713         case DW_OP_deref:
3714           cfa->indirect = 1;
3715           break;
3716         case DW_OP_plus_uconst:
3717           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3718           break;
3719         default:
3720           internal_error ("DW_LOC_OP %s not implemented",
3721                           dwarf_stack_op_name (ptr->dw_loc_opc));
3722         }
3723     }
3724 }
3725 #endif /* .debug_frame support */
3726 \f
3727 /* And now, the support for symbolic debugging information.  */
3728 #ifdef DWARF2_DEBUGGING_INFO
3729
3730 /* .debug_str support.  */
3731 static int output_indirect_string (void **, void *);
3732
3733 static void dwarf2out_init (const char *);
3734 static void dwarf2out_finish (const char *);
3735 static void dwarf2out_define (unsigned int, const char *);
3736 static void dwarf2out_undef (unsigned int, const char *);
3737 static void dwarf2out_start_source_file (unsigned, const char *);
3738 static void dwarf2out_end_source_file (unsigned);
3739 static void dwarf2out_begin_block (unsigned, unsigned);
3740 static void dwarf2out_end_block (unsigned, unsigned);
3741 static bool dwarf2out_ignore_block (const_tree);
3742 static void dwarf2out_global_decl (tree);
3743 static void dwarf2out_type_decl (tree, int);
3744 static void dwarf2out_imported_module_or_decl (tree, tree);
3745 static void dwarf2out_abstract_function (tree);
3746 static void dwarf2out_var_location (rtx);
3747 static void dwarf2out_begin_function (tree);
3748
3749 /* The debug hooks structure.  */
3750
3751 const struct gcc_debug_hooks dwarf2_debug_hooks =
3752 {
3753   dwarf2out_init,
3754   dwarf2out_finish,
3755   dwarf2out_define,
3756   dwarf2out_undef,
3757   dwarf2out_start_source_file,
3758   dwarf2out_end_source_file,
3759   dwarf2out_begin_block,
3760   dwarf2out_end_block,
3761   dwarf2out_ignore_block,
3762   dwarf2out_source_line,
3763   dwarf2out_begin_prologue,
3764   debug_nothing_int_charstar,   /* end_prologue */
3765   dwarf2out_end_epilogue,
3766   dwarf2out_begin_function,
3767   debug_nothing_int,            /* end_function */
3768   dwarf2out_decl,               /* function_decl */
3769   dwarf2out_global_decl,
3770   dwarf2out_type_decl,          /* type_decl */
3771   dwarf2out_imported_module_or_decl,
3772   debug_nothing_tree,           /* deferred_inline_function */
3773   /* The DWARF 2 backend tries to reduce debugging bloat by not
3774      emitting the abstract description of inline functions until
3775      something tries to reference them.  */
3776   dwarf2out_abstract_function,  /* outlining_inline_function */
3777   debug_nothing_rtx,            /* label */
3778   debug_nothing_int,            /* handle_pch */
3779   dwarf2out_var_location,
3780   dwarf2out_switch_text_section,
3781   1                             /* start_end_main_source_file */
3782 };
3783 #endif
3784 \f
3785 /* NOTE: In the comments in this file, many references are made to
3786    "Debugging Information Entries".  This term is abbreviated as `DIE'
3787    throughout the remainder of this file.  */
3788
3789 /* An internal representation of the DWARF output is built, and then
3790    walked to generate the DWARF debugging info.  The walk of the internal
3791    representation is done after the entire program has been compiled.
3792    The types below are used to describe the internal representation.  */
3793
3794 /* Various DIE's use offsets relative to the beginning of the
3795    .debug_info section to refer to each other.  */
3796
3797 typedef long int dw_offset;
3798
3799 /* Define typedefs here to avoid circular dependencies.  */
3800
3801 typedef struct dw_attr_struct *dw_attr_ref;
3802 typedef struct dw_line_info_struct *dw_line_info_ref;
3803 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3804 typedef struct pubname_struct *pubname_ref;
3805 typedef struct dw_ranges_struct *dw_ranges_ref;
3806 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3807
3808 /* Each entry in the line_info_table maintains the file and
3809    line number associated with the label generated for that
3810    entry.  The label gives the PC value associated with
3811    the line number entry.  */
3812
3813 typedef struct dw_line_info_struct GTY(())
3814 {
3815   unsigned long dw_file_num;
3816   unsigned long dw_line_num;
3817 }
3818 dw_line_info_entry;
3819
3820 /* Line information for functions in separate sections; each one gets its
3821    own sequence.  */
3822 typedef struct dw_separate_line_info_struct GTY(())
3823 {
3824   unsigned long dw_file_num;
3825   unsigned long dw_line_num;
3826   unsigned long function;
3827 }
3828 dw_separate_line_info_entry;
3829
3830 /* Each DIE attribute has a field specifying the attribute kind,
3831    a link to the next attribute in the chain, and an attribute value.
3832    Attributes are typically linked below the DIE they modify.  */
3833
3834 typedef struct dw_attr_struct GTY(())
3835 {
3836   enum dwarf_attribute dw_attr;
3837   dw_val_node dw_attr_val;
3838 }
3839 dw_attr_node;
3840
3841 DEF_VEC_O(dw_attr_node);
3842 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3843
3844 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3845    The children of each node form a circular list linked by
3846    die_sib.  die_child points to the node *before* the "first" child node.  */
3847
3848 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
3849 {
3850   enum dwarf_tag die_tag;
3851   char *die_symbol;
3852   VEC(dw_attr_node,gc) * die_attr;
3853   dw_die_ref die_parent;
3854   dw_die_ref die_child;
3855   dw_die_ref die_sib;
3856   dw_die_ref die_definition; /* ref from a specification to its definition */
3857   dw_offset die_offset;
3858   unsigned long die_abbrev;
3859   int die_mark;
3860   /* Die is used and must not be pruned as unused.  */
3861   int die_perennial_p;
3862   unsigned int decl_id;
3863 }
3864 die_node;
3865
3866 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3867 #define FOR_EACH_CHILD(die, c, expr) do {       \
3868   c = die->die_child;                           \
3869   if (c) do {                                   \
3870     c = c->die_sib;                             \
3871     expr;                                       \
3872   } while (c != die->die_child);                \
3873 } while (0)
3874
3875 /* The pubname structure */
3876
3877 typedef struct pubname_struct GTY(())
3878 {
3879   dw_die_ref die;
3880   const char *name;
3881 }
3882 pubname_entry;
3883
3884 DEF_VEC_O(pubname_entry);
3885 DEF_VEC_ALLOC_O(pubname_entry, gc);
3886
3887 struct dw_ranges_struct GTY(())
3888 {
3889   /* If this is positive, it's a block number, otherwise it's a
3890      bitwise-negated index into dw_ranges_by_label.  */
3891   int num;
3892 };
3893
3894 struct dw_ranges_by_label_struct GTY(())
3895 {
3896   const char *begin;
3897   const char *end;
3898 };
3899
3900 /* The limbo die list structure.  */
3901 typedef struct limbo_die_struct GTY(())
3902 {
3903   dw_die_ref die;
3904   tree created_for;
3905   struct limbo_die_struct *next;
3906 }
3907 limbo_die_node;
3908
3909 /* How to start an assembler comment.  */
3910 #ifndef ASM_COMMENT_START
3911 #define ASM_COMMENT_START ";#"
3912 #endif
3913
3914 /* Define a macro which returns nonzero for a TYPE_DECL which was
3915    implicitly generated for a tagged type.
3916
3917    Note that unlike the gcc front end (which generates a NULL named
3918    TYPE_DECL node for each complete tagged type, each array type, and
3919    each function type node created) the g++ front end generates a
3920    _named_ TYPE_DECL node for each tagged type node created.
3921    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3922    generate a DW_TAG_typedef DIE for them.  */
3923
3924 #define TYPE_DECL_IS_STUB(decl)                         \
3925   (DECL_NAME (decl) == NULL_TREE                        \
3926    || (DECL_ARTIFICIAL (decl)                           \
3927        && is_tagged_type (TREE_TYPE (decl))             \
3928        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3929            /* This is necessary for stub decls that     \
3930               appear in nested inline functions.  */    \
3931            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3932                && (decl_ultimate_origin (decl)          \
3933                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3934
3935 /* Information concerning the compilation unit's programming
3936    language, and compiler version.  */
3937
3938 /* Fixed size portion of the DWARF compilation unit header.  */
3939 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3940   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3941
3942 /* Fixed size portion of public names info.  */
3943 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3944
3945 /* Fixed size portion of the address range info.  */
3946 #define DWARF_ARANGES_HEADER_SIZE                                       \
3947   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3948                 DWARF2_ADDR_SIZE * 2)                                   \
3949    - DWARF_INITIAL_LENGTH_SIZE)
3950
3951 /* Size of padding portion in the address range info.  It must be
3952    aligned to twice the pointer size.  */
3953 #define DWARF_ARANGES_PAD_SIZE \
3954   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3955                 DWARF2_ADDR_SIZE * 2)                              \
3956    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3957
3958 /* Use assembler line directives if available.  */
3959 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3960 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3961 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3962 #else
3963 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3964 #endif
3965 #endif
3966
3967 /* Minimum line offset in a special line info. opcode.
3968    This value was chosen to give a reasonable range of values.  */
3969 #define DWARF_LINE_BASE  -10
3970
3971 /* First special line opcode - leave room for the standard opcodes.  */
3972 #define DWARF_LINE_OPCODE_BASE  10
3973
3974 /* Range of line offsets in a special line info. opcode.  */
3975 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3976
3977 /* Flag that indicates the initial value of the is_stmt_start flag.
3978    In the present implementation, we do not mark any lines as
3979    the beginning of a source statement, because that information
3980    is not made available by the GCC front-end.  */
3981 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3982
3983 #ifdef DWARF2_DEBUGGING_INFO
3984 /* This location is used by calc_die_sizes() to keep track
3985    the offset of each DIE within the .debug_info section.  */
3986 static unsigned long next_die_offset;
3987 #endif
3988
3989 /* Record the root of the DIE's built for the current compilation unit.  */
3990 static GTY(()) dw_die_ref comp_unit_die;
3991
3992 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3993 static GTY(()) limbo_die_node *limbo_die_list;
3994
3995 /* Filenames referenced by this compilation unit.  */
3996 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3997
3998 /* A hash table of references to DIE's that describe declarations.
3999    The key is a DECL_UID() which is a unique number identifying each decl.  */
4000 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4001
4002 /* Node of the variable location list.  */
4003 struct var_loc_node GTY ((chain_next ("%h.next")))
4004 {
4005   rtx GTY (()) var_loc_note;
4006   const char * GTY (()) label;
4007   const char * GTY (()) section_label;
4008   struct var_loc_node * GTY (()) next;
4009 };
4010
4011 /* Variable location list.  */
4012 struct var_loc_list_def GTY (())
4013 {
4014   struct var_loc_node * GTY (()) first;
4015
4016   /* Do not mark the last element of the chained list because
4017      it is marked through the chain.  */
4018   struct var_loc_node * GTY ((skip ("%h"))) last;
4019
4020   /* DECL_UID of the variable decl.  */
4021   unsigned int decl_id;
4022 };
4023 typedef struct var_loc_list_def var_loc_list;
4024
4025
4026 /* Table of decl location linked lists.  */
4027 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4028
4029 /* A pointer to the base of a list of references to DIE's that
4030    are uniquely identified by their tag, presence/absence of
4031    children DIE's, and list of attribute/value pairs.  */
4032 static GTY((length ("abbrev_die_table_allocated")))
4033   dw_die_ref *abbrev_die_table;
4034
4035 /* Number of elements currently allocated for abbrev_die_table.  */
4036 static GTY(()) unsigned abbrev_die_table_allocated;
4037
4038 /* Number of elements in type_die_table currently in use.  */
4039 static GTY(()) unsigned abbrev_die_table_in_use;
4040
4041 /* Size (in elements) of increments by which we may expand the
4042    abbrev_die_table.  */
4043 #define ABBREV_DIE_TABLE_INCREMENT 256
4044
4045 /* A pointer to the base of a table that contains line information
4046    for each source code line in .text in the compilation unit.  */
4047 static GTY((length ("line_info_table_allocated")))
4048      dw_line_info_ref line_info_table;
4049
4050 /* Number of elements currently allocated for line_info_table.  */
4051 static GTY(()) unsigned line_info_table_allocated;
4052
4053 /* Number of elements in line_info_table currently in use.  */
4054 static GTY(()) unsigned line_info_table_in_use;
4055
4056 /* A pointer to the base of a table that contains line information
4057    for each source code line outside of .text in the compilation unit.  */
4058 static GTY ((length ("separate_line_info_table_allocated")))
4059      dw_separate_line_info_ref separate_line_info_table;
4060
4061 /* Number of elements currently allocated for separate_line_info_table.  */
4062 static GTY(()) unsigned separate_line_info_table_allocated;
4063
4064 /* Number of elements in separate_line_info_table currently in use.  */
4065 static GTY(()) unsigned separate_line_info_table_in_use;
4066
4067 /* Size (in elements) of increments by which we may expand the
4068    line_info_table.  */
4069 #define LINE_INFO_TABLE_INCREMENT 1024
4070
4071 /* A pointer to the base of a table that contains a list of publicly
4072    accessible names.  */
4073 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4074
4075 /* A pointer to the base of a table that contains a list of publicly
4076    accessible types.  */
4077 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4078
4079 /* Array of dies for which we should generate .debug_arange info.  */
4080 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4081
4082 /* Number of elements currently allocated for arange_table.  */
4083 static GTY(()) unsigned arange_table_allocated;
4084
4085 /* Number of elements in arange_table currently in use.  */
4086 static GTY(()) unsigned arange_table_in_use;
4087
4088 /* Size (in elements) of increments by which we may expand the
4089    arange_table.  */
4090 #define ARANGE_TABLE_INCREMENT 64
4091
4092 /* Array of dies for which we should generate .debug_ranges info.  */
4093 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4094
4095 /* Number of elements currently allocated for ranges_table.  */
4096 static GTY(()) unsigned ranges_table_allocated;
4097
4098 /* Number of elements in ranges_table currently in use.  */
4099 static GTY(()) unsigned ranges_table_in_use;
4100
4101 /* Array of pairs of labels referenced in ranges_table.  */
4102 static GTY ((length ("ranges_by_label_allocated")))
4103      dw_ranges_by_label_ref ranges_by_label;
4104
4105 /* Number of elements currently allocated for ranges_by_label.  */
4106 static GTY(()) unsigned ranges_by_label_allocated;
4107
4108 /* Number of elements in ranges_by_label currently in use.  */
4109 static GTY(()) unsigned ranges_by_label_in_use;
4110
4111 /* Size (in elements) of increments by which we may expand the
4112    ranges_table.  */
4113 #define RANGES_TABLE_INCREMENT 64
4114
4115 /* Whether we have location lists that need outputting */
4116 static GTY(()) bool have_location_lists;
4117
4118 /* Unique label counter.  */
4119 static GTY(()) unsigned int loclabel_num;
4120
4121 #ifdef DWARF2_DEBUGGING_INFO
4122 /* Record whether the function being analyzed contains inlined functions.  */
4123 static int current_function_has_inlines;
4124 #endif
4125 #if 0 && defined (MIPS_DEBUGGING_INFO)
4126 static int comp_unit_has_inlines;
4127 #endif
4128
4129 /* The last file entry emitted by maybe_emit_file().  */
4130 static GTY(()) struct dwarf_file_data * last_emitted_file;
4131
4132 /* Number of internal labels generated by gen_internal_sym().  */
4133 static GTY(()) int label_num;
4134
4135 /* Cached result of previous call to lookup_filename.  */
4136 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4137
4138 #ifdef DWARF2_DEBUGGING_INFO
4139
4140 /* Offset from the "steady-state frame pointer" to the frame base,
4141    within the current function.  */
4142 static HOST_WIDE_INT frame_pointer_fb_offset;
4143
4144 /* Forward declarations for functions defined in this file.  */
4145
4146 static int is_pseudo_reg (const_rtx);
4147 static tree type_main_variant (tree);
4148 static int is_tagged_type (const_tree);
4149 static const char *dwarf_tag_name (unsigned);
4150 static const char *dwarf_attr_name (unsigned);
4151 static const char *dwarf_form_name (unsigned);
4152 static tree decl_ultimate_origin (const_tree);
4153 static tree block_ultimate_origin (const_tree);
4154 static tree decl_class_context (tree);
4155 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4156 static inline enum dw_val_class AT_class (dw_attr_ref);
4157 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4158 static inline unsigned AT_flag (dw_attr_ref);
4159 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4160 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4161 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4162 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4163 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4164                               unsigned long);
4165 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4166                                unsigned int, unsigned char *);
4167 static hashval_t debug_str_do_hash (const void *);
4168 static int debug_str_eq (const void *, const void *);
4169 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4170 static inline const char *AT_string (dw_attr_ref);
4171 static int AT_string_form (dw_attr_ref);
4172 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4173 static void add_AT_specification (dw_die_ref, dw_die_ref);
4174 static inline dw_die_ref AT_ref (dw_attr_ref);
4175 static inline int AT_ref_external (dw_attr_ref);
4176 static inline void set_AT_ref_external (dw_attr_ref, int);
4177 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4178 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4179 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4180 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4181                              dw_loc_list_ref);
4182 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4183 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4184 static inline rtx AT_addr (dw_attr_ref);
4185 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4186 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4187 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4188 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4189                            unsigned HOST_WIDE_INT);
4190 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4191                                unsigned long);
4192 static inline const char *AT_lbl (dw_attr_ref);
4193 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4194 static const char *get_AT_low_pc (dw_die_ref);
4195 static const char *get_AT_hi_pc (dw_die_ref);
4196 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4197 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4198 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4199 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4200 static bool is_c_family (void);
4201 static bool is_cxx (void);
4202 static bool is_java (void);
4203 static bool is_fortran (void);
4204 static bool is_ada (void);
4205 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4206 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4207 static void add_child_die (dw_die_ref, dw_die_ref);
4208 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4209 static dw_die_ref lookup_type_die (tree);
4210 static void equate_type_number_to_die (tree, dw_die_ref);
4211 static hashval_t decl_die_table_hash (const void *);
4212 static int decl_die_table_eq (const void *, const void *);
4213 static dw_die_ref lookup_decl_die (tree);
4214 static hashval_t decl_loc_table_hash (const void *);
4215 static int decl_loc_table_eq (const void *, const void *);
4216 static var_loc_list *lookup_decl_loc (const_tree);
4217 static void equate_decl_number_to_die (tree, dw_die_ref);
4218 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4219 static void print_spaces (FILE *);
4220 static void print_die (dw_die_ref, FILE *);
4221 static void print_dwarf_line_table (FILE *);
4222 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4223 static dw_die_ref pop_compile_unit (dw_die_ref);
4224 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4225 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4226 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4227 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4228 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4229 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4230 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4231 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4232 static void compute_section_prefix (dw_die_ref);
4233 static int is_type_die (dw_die_ref);
4234 static int is_comdat_die (dw_die_ref);
4235 static int is_symbol_die (dw_die_ref);
4236 static void assign_symbol_names (dw_die_ref);
4237 static void break_out_includes (dw_die_ref);
4238 static hashval_t htab_cu_hash (const void *);
4239 static int htab_cu_eq (const void *, const void *);
4240 static void htab_cu_del (void *);
4241 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4242 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4243 static void add_sibling_attributes (dw_die_ref);
4244 static void build_abbrev_table (dw_die_ref);
4245 static void output_location_lists (dw_die_ref);
4246 static int constant_size (long unsigned);
4247 static unsigned long size_of_die (dw_die_ref);
4248 static void calc_die_sizes (dw_die_ref);
4249 static void mark_dies (dw_die_ref);
4250 static void unmark_dies (dw_die_ref);
4251 static void unmark_all_dies (dw_die_ref);
4252 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4253 static unsigned long size_of_aranges (void);
4254 static enum dwarf_form value_format (dw_attr_ref);
4255 static void output_value_format (dw_attr_ref);
4256 static void output_abbrev_section (void);
4257 static void output_die_symbol (dw_die_ref);
4258 static void output_die (dw_die_ref);
4259 static void output_compilation_unit_header (void);
4260 static void output_comp_unit (dw_die_ref, int);
4261 static const char *dwarf2_name (tree, int);
4262 static void add_pubname (tree, dw_die_ref);
4263 static void add_pubname_string (const char *, dw_die_ref);
4264 static void add_pubtype (tree, dw_die_ref);
4265 static void output_pubnames (VEC (pubname_entry,gc) *);
4266 static void add_arange (tree, dw_die_ref);
4267 static void output_aranges (void);
4268 static unsigned int add_ranges_num (int);
4269 static unsigned int add_ranges (const_tree);
4270 static unsigned int add_ranges_by_labels (const char *, const char *);
4271 static void output_ranges (void);
4272 static void output_line_info (void);
4273 static void output_file_names (void);
4274 static dw_die_ref base_type_die (tree);
4275 static int is_base_type (tree);
4276 static bool is_subrange_type (const_tree);
4277 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4278 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4279 static int type_is_enum (const_tree);
4280 static unsigned int dbx_reg_number (const_rtx);
4281 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4282 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4283 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4284                                                 enum var_init_status);
4285 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4286                                                      enum var_init_status);
4287 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4288 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4289                                          enum var_init_status);
4290 static int is_based_loc (const_rtx);
4291 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4292                                             enum var_init_status);
4293 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4294                                                enum var_init_status);
4295 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4296 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4297 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4298 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4299 static tree field_type (const_tree);
4300 static unsigned int simple_type_align_in_bits (const_tree);
4301 static unsigned int simple_decl_align_in_bits (const_tree);
4302 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4303 static HOST_WIDE_INT field_byte_offset (const_tree);
4304 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4305                                          dw_loc_descr_ref);
4306 static void add_data_member_location_attribute (dw_die_ref, tree);
4307 static void add_const_value_attribute (dw_die_ref, rtx);
4308 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4309 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4310 static void insert_float (const_rtx, unsigned char *);
4311 static rtx rtl_for_decl_location (tree);
4312 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4313                                                    enum dwarf_attribute);
4314 static void tree_add_const_value_attribute (dw_die_ref, tree);
4315 static void add_name_attribute (dw_die_ref, const char *);
4316 static void add_comp_dir_attribute (dw_die_ref);
4317 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4318 static void add_subscript_info (dw_die_ref, tree);
4319 static void add_byte_size_attribute (dw_die_ref, tree);
4320 static void add_bit_offset_attribute (dw_die_ref, tree);
4321 static void add_bit_size_attribute (dw_die_ref, tree);
4322 static void add_prototyped_attribute (dw_die_ref, tree);
4323 static void add_abstract_origin_attribute (dw_die_ref, tree);
4324 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4325 static void add_src_coords_attributes (dw_die_ref, tree);
4326 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4327 static void push_decl_scope (tree);
4328 static void pop_decl_scope (void);
4329 static dw_die_ref scope_die_for (tree, dw_die_ref);
4330 static inline int local_scope_p (dw_die_ref);
4331 static inline int class_or_namespace_scope_p (dw_die_ref);
4332 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4333 static void add_calling_convention_attribute (dw_die_ref, tree);
4334 static const char *type_tag (const_tree);
4335 static tree member_declared_type (const_tree);
4336 #if 0
4337 static const char *decl_start_label (tree);
4338 #endif
4339 static void gen_array_type_die (tree, dw_die_ref);
4340 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4341 #if 0
4342 static void gen_entry_point_die (tree, dw_die_ref);
4343 #endif
4344 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4345 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4346 static void gen_inlined_union_type_die (tree, dw_die_ref);
4347 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4348 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4349 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4350 static void gen_formal_types_die (tree, dw_die_ref);
4351 static void gen_subprogram_die (tree, dw_die_ref);
4352 static void gen_variable_die (tree, dw_die_ref);
4353 static void gen_label_die (tree, dw_die_ref);
4354 static void gen_lexical_block_die (tree, dw_die_ref, int);
4355 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4356 static void gen_field_die (tree, dw_die_ref);
4357 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4358 static dw_die_ref gen_compile_unit_die (const char *);
4359 static void gen_inheritance_die (tree, tree, dw_die_ref);
4360 static void gen_member_die (tree, dw_die_ref);
4361 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4362                                                 enum debug_info_usage);
4363 static void gen_subroutine_type_die (tree, dw_die_ref);
4364 static void gen_typedef_die (tree, dw_die_ref);
4365 static void gen_type_die (tree, dw_die_ref);
4366 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4367 static void gen_block_die (tree, dw_die_ref, int);
4368 static void decls_for_scope (tree, dw_die_ref, int);
4369 static int is_redundant_typedef (const_tree);
4370 static void gen_namespace_die (tree);
4371 static void gen_decl_die (tree, dw_die_ref);
4372 static dw_die_ref force_decl_die (tree);
4373 static dw_die_ref force_type_die (tree);
4374 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4375 static void declare_in_namespace (tree, dw_die_ref);
4376 static struct dwarf_file_data * lookup_filename (const char *);
4377 static void retry_incomplete_types (void);
4378 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4379 static void splice_child_die (dw_die_ref, dw_die_ref);
4380 static int file_info_cmp (const void *, const void *);
4381 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4382                                      const char *, const char *, unsigned);
4383 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4384                                        const char *, const char *,
4385                                        const char *);
4386 static void output_loc_list (dw_loc_list_ref);
4387 static char *gen_internal_sym (const char *);
4388
4389 static void prune_unmark_dies (dw_die_ref);
4390 static void prune_unused_types_mark (dw_die_ref, int);
4391 static void prune_unused_types_walk (dw_die_ref);
4392 static void prune_unused_types_walk_attribs (dw_die_ref);
4393 static void prune_unused_types_prune (dw_die_ref);
4394 static void prune_unused_types (void);
4395 static int maybe_emit_file (struct dwarf_file_data *fd);
4396
4397 /* Section names used to hold DWARF debugging information.  */
4398 #ifndef DEBUG_INFO_SECTION
4399 #define DEBUG_INFO_SECTION      ".debug_info"
4400 #endif
4401 #ifndef DEBUG_ABBREV_SECTION
4402 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4403 #endif
4404 #ifndef DEBUG_ARANGES_SECTION
4405 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4406 #endif
4407 #ifndef DEBUG_MACINFO_SECTION
4408 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4409 #endif
4410 #ifndef DEBUG_LINE_SECTION
4411 #define DEBUG_LINE_SECTION      ".debug_line"
4412 #endif
4413 #ifndef DEBUG_LOC_SECTION
4414 #define DEBUG_LOC_SECTION       ".debug_loc"
4415 #endif
4416 #ifndef DEBUG_PUBNAMES_SECTION
4417 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4418 #endif
4419 #ifndef DEBUG_STR_SECTION
4420 #define DEBUG_STR_SECTION       ".debug_str"
4421 #endif
4422 #ifndef DEBUG_RANGES_SECTION
4423 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4424 #endif
4425
4426 /* Standard ELF section names for compiled code and data.  */
4427 #ifndef TEXT_SECTION_NAME
4428 #define TEXT_SECTION_NAME       ".text"
4429 #endif
4430
4431 /* Section flags for .debug_str section.  */
4432 #define DEBUG_STR_SECTION_FLAGS \
4433   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4434    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4435    : SECTION_DEBUG)
4436
4437 /* Labels we insert at beginning sections we can reference instead of
4438    the section names themselves.  */
4439
4440 #ifndef TEXT_SECTION_LABEL
4441 #define TEXT_SECTION_LABEL              "Ltext"
4442 #endif
4443 #ifndef COLD_TEXT_SECTION_LABEL
4444 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4445 #endif
4446 #ifndef DEBUG_LINE_SECTION_LABEL
4447 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4448 #endif
4449 #ifndef DEBUG_INFO_SECTION_LABEL
4450 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4451 #endif
4452 #ifndef DEBUG_ABBREV_SECTION_LABEL
4453 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4454 #endif
4455 #ifndef DEBUG_LOC_SECTION_LABEL
4456 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4457 #endif
4458 #ifndef DEBUG_RANGES_SECTION_LABEL
4459 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4460 #endif
4461 #ifndef DEBUG_MACINFO_SECTION_LABEL
4462 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4463 #endif
4464
4465 /* Definitions of defaults for formats and names of various special
4466    (artificial) labels which may be generated within this file (when the -g
4467    options is used and DWARF2_DEBUGGING_INFO is in effect.
4468    If necessary, these may be overridden from within the tm.h file, but
4469    typically, overriding these defaults is unnecessary.  */
4470
4471 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4472 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4473 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4474 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4475 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4476 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4477 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4478 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4479 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4480 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4481
4482 #ifndef TEXT_END_LABEL
4483 #define TEXT_END_LABEL          "Letext"
4484 #endif
4485 #ifndef COLD_END_LABEL
4486 #define COLD_END_LABEL          "Letext_cold"
4487 #endif
4488 #ifndef BLOCK_BEGIN_LABEL
4489 #define BLOCK_BEGIN_LABEL       "LBB"
4490 #endif
4491 #ifndef BLOCK_END_LABEL
4492 #define BLOCK_END_LABEL         "LBE"
4493 #endif
4494 #ifndef LINE_CODE_LABEL
4495 #define LINE_CODE_LABEL         "LM"
4496 #endif
4497 #ifndef SEPARATE_LINE_CODE_LABEL
4498 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4499 #endif
4500
4501 \f
4502 /* We allow a language front-end to designate a function that is to be
4503    called to "demangle" any name before it is put into a DIE.  */
4504
4505 static const char *(*demangle_name_func) (const char *);
4506
4507 void
4508 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4509 {
4510   demangle_name_func = func;
4511 }
4512
4513 /* Test if rtl node points to a pseudo register.  */
4514
4515 static inline int
4516 is_pseudo_reg (const_rtx rtl)
4517 {
4518   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4519           || (GET_CODE (rtl) == SUBREG
4520               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4521 }
4522
4523 /* Return a reference to a type, with its const and volatile qualifiers
4524    removed.  */
4525
4526 static inline tree
4527 type_main_variant (tree type)
4528 {
4529   type = TYPE_MAIN_VARIANT (type);
4530
4531   /* ??? There really should be only one main variant among any group of
4532      variants of a given type (and all of the MAIN_VARIANT values for all
4533      members of the group should point to that one type) but sometimes the C
4534      front-end messes this up for array types, so we work around that bug
4535      here.  */
4536   if (TREE_CODE (type) == ARRAY_TYPE)
4537     while (type != TYPE_MAIN_VARIANT (type))
4538       type = TYPE_MAIN_VARIANT (type);
4539
4540   return type;
4541 }
4542
4543 /* Return nonzero if the given type node represents a tagged type.  */
4544
4545 static inline int
4546 is_tagged_type (const_tree type)
4547 {
4548   enum tree_code code = TREE_CODE (type);
4549
4550   return (code == RECORD_TYPE || code == UNION_TYPE
4551           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4552 }
4553
4554 /* Convert a DIE tag into its string name.  */
4555
4556 static const char *
4557 dwarf_tag_name (unsigned int tag)
4558 {
4559   switch (tag)
4560     {
4561     case DW_TAG_padding:
4562       return "DW_TAG_padding";
4563     case DW_TAG_array_type:
4564       return "DW_TAG_array_type";
4565     case DW_TAG_class_type:
4566       return "DW_TAG_class_type";
4567     case DW_TAG_entry_point:
4568       return "DW_TAG_entry_point";
4569     case DW_TAG_enumeration_type:
4570       return "DW_TAG_enumeration_type";
4571     case DW_TAG_formal_parameter:
4572       return "DW_TAG_formal_parameter";
4573     case DW_TAG_imported_declaration:
4574       return "DW_TAG_imported_declaration";
4575     case DW_TAG_label:
4576       return "DW_TAG_label";
4577     case DW_TAG_lexical_block:
4578       return "DW_TAG_lexical_block";
4579     case DW_TAG_member:
4580       return "DW_TAG_member";
4581     case DW_TAG_pointer_type:
4582       return "DW_TAG_pointer_type";
4583     case DW_TAG_reference_type:
4584       return "DW_TAG_reference_type";
4585     case DW_TAG_compile_unit:
4586       return "DW_TAG_compile_unit";
4587     case DW_TAG_string_type:
4588       return "DW_TAG_string_type";
4589     case DW_TAG_structure_type:
4590       return "DW_TAG_structure_type";
4591     case DW_TAG_subroutine_type:
4592       return "DW_TAG_subroutine_type";
4593     case DW_TAG_typedef:
4594       return "DW_TAG_typedef";
4595     case DW_TAG_union_type:
4596       return "DW_TAG_union_type";
4597     case DW_TAG_unspecified_parameters:
4598       return "DW_TAG_unspecified_parameters";
4599     case DW_TAG_variant:
4600       return "DW_TAG_variant";
4601     case DW_TAG_common_block:
4602       return "DW_TAG_common_block";
4603     case DW_TAG_common_inclusion:
4604       return "DW_TAG_common_inclusion";
4605     case DW_TAG_inheritance:
4606       return "DW_TAG_inheritance";
4607     case DW_TAG_inlined_subroutine:
4608       return "DW_TAG_inlined_subroutine";
4609     case DW_TAG_module:
4610       return "DW_TAG_module";
4611     case DW_TAG_ptr_to_member_type:
4612       return "DW_TAG_ptr_to_member_type";
4613     case DW_TAG_set_type:
4614       return "DW_TAG_set_type";
4615     case DW_TAG_subrange_type:
4616       return "DW_TAG_subrange_type";
4617     case DW_TAG_with_stmt:
4618       return "DW_TAG_with_stmt";
4619     case DW_TAG_access_declaration:
4620       return "DW_TAG_access_declaration";
4621     case DW_TAG_base_type:
4622       return "DW_TAG_base_type";
4623     case DW_TAG_catch_block:
4624       return "DW_TAG_catch_block";
4625     case DW_TAG_const_type:
4626       return "DW_TAG_const_type";
4627     case DW_TAG_constant:
4628       return "DW_TAG_constant";
4629     case DW_TAG_enumerator:
4630       return "DW_TAG_enumerator";
4631     case DW_TAG_file_type:
4632       return "DW_TAG_file_type";
4633     case DW_TAG_friend:
4634       return "DW_TAG_friend";
4635     case DW_TAG_namelist:
4636       return "DW_TAG_namelist";
4637     case DW_TAG_namelist_item:
4638       return "DW_TAG_namelist_item";
4639     case DW_TAG_packed_type:
4640       return "DW_TAG_packed_type";
4641     case DW_TAG_subprogram:
4642       return "DW_TAG_subprogram";
4643     case DW_TAG_template_type_param:
4644       return "DW_TAG_template_type_param";
4645     case DW_TAG_template_value_param:
4646       return "DW_TAG_template_value_param";
4647     case DW_TAG_thrown_type:
4648       return "DW_TAG_thrown_type";
4649     case DW_TAG_try_block:
4650       return "DW_TAG_try_block";
4651     case DW_TAG_variant_part:
4652       return "DW_TAG_variant_part";
4653     case DW_TAG_variable:
4654       return "DW_TAG_variable";
4655     case DW_TAG_volatile_type:
4656       return "DW_TAG_volatile_type";
4657     case DW_TAG_dwarf_procedure:
4658       return "DW_TAG_dwarf_procedure";
4659     case DW_TAG_restrict_type:
4660       return "DW_TAG_restrict_type";
4661     case DW_TAG_interface_type:
4662       return "DW_TAG_interface_type";
4663     case DW_TAG_namespace:
4664       return "DW_TAG_namespace";
4665     case DW_TAG_imported_module:
4666       return "DW_TAG_imported_module";
4667     case DW_TAG_unspecified_type:
4668       return "DW_TAG_unspecified_type";
4669     case DW_TAG_partial_unit:
4670       return "DW_TAG_partial_unit";
4671     case DW_TAG_imported_unit:
4672       return "DW_TAG_imported_unit";
4673     case DW_TAG_condition:
4674       return "DW_TAG_condition";
4675     case DW_TAG_shared_type:
4676       return "DW_TAG_shared_type";
4677     case DW_TAG_MIPS_loop:
4678       return "DW_TAG_MIPS_loop";
4679     case DW_TAG_format_label:
4680       return "DW_TAG_format_label";
4681     case DW_TAG_function_template:
4682       return "DW_TAG_function_template";
4683     case DW_TAG_class_template:
4684       return "DW_TAG_class_template";
4685     case DW_TAG_GNU_BINCL:
4686       return "DW_TAG_GNU_BINCL";
4687     case DW_TAG_GNU_EINCL:
4688       return "DW_TAG_GNU_EINCL";
4689     default:
4690       return "DW_TAG_<unknown>";
4691     }
4692 }
4693
4694 /* Convert a DWARF attribute code into its string name.  */
4695
4696 static const char *
4697 dwarf_attr_name (unsigned int attr)
4698 {
4699   switch (attr)
4700     {
4701     case DW_AT_sibling:
4702       return "DW_AT_sibling";
4703     case DW_AT_location:
4704       return "DW_AT_location";
4705     case DW_AT_name:
4706       return "DW_AT_name";
4707     case DW_AT_ordering:
4708       return "DW_AT_ordering";
4709     case DW_AT_subscr_data:
4710       return "DW_AT_subscr_data";
4711     case DW_AT_byte_size:
4712       return "DW_AT_byte_size";
4713     case DW_AT_bit_offset:
4714       return "DW_AT_bit_offset";
4715     case DW_AT_bit_size:
4716       return "DW_AT_bit_size";
4717     case DW_AT_element_list:
4718       return "DW_AT_element_list";
4719     case DW_AT_stmt_list:
4720       return "DW_AT_stmt_list";
4721     case DW_AT_low_pc:
4722       return "DW_AT_low_pc";
4723     case DW_AT_high_pc:
4724       return "DW_AT_high_pc";
4725     case DW_AT_language:
4726       return "DW_AT_language";
4727     case DW_AT_member:
4728       return "DW_AT_member";
4729     case DW_AT_discr:
4730       return "DW_AT_discr";
4731     case DW_AT_discr_value:
4732       return "DW_AT_discr_value";
4733     case DW_AT_visibility:
4734       return "DW_AT_visibility";
4735     case DW_AT_import:
4736       return "DW_AT_import";
4737     case DW_AT_string_length:
4738       return "DW_AT_string_length";
4739     case DW_AT_common_reference:
4740       return "DW_AT_common_reference";
4741     case DW_AT_comp_dir:
4742       return "DW_AT_comp_dir";
4743     case DW_AT_const_value:
4744       return "DW_AT_const_value";
4745     case DW_AT_containing_type:
4746       return "DW_AT_containing_type";
4747     case DW_AT_default_value:
4748       return "DW_AT_default_value";
4749     case DW_AT_inline:
4750       return "DW_AT_inline";
4751     case DW_AT_is_optional:
4752       return "DW_AT_is_optional";
4753     case DW_AT_lower_bound:
4754       return "DW_AT_lower_bound";
4755     case DW_AT_producer:
4756       return "DW_AT_producer";
4757     case DW_AT_prototyped:
4758       return "DW_AT_prototyped";
4759     case DW_AT_return_addr:
4760       return "DW_AT_return_addr";
4761     case DW_AT_start_scope:
4762       return "DW_AT_start_scope";
4763     case DW_AT_bit_stride:
4764       return "DW_AT_bit_stride";
4765     case DW_AT_upper_bound:
4766       return "DW_AT_upper_bound";
4767     case DW_AT_abstract_origin:
4768       return "DW_AT_abstract_origin";
4769     case DW_AT_accessibility:
4770       return "DW_AT_accessibility";
4771     case DW_AT_address_class:
4772       return "DW_AT_address_class";
4773     case DW_AT_artificial:
4774       return "DW_AT_artificial";
4775     case DW_AT_base_types:
4776       return "DW_AT_base_types";
4777     case DW_AT_calling_convention:
4778       return "DW_AT_calling_convention";
4779     case DW_AT_count:
4780       return "DW_AT_count";
4781     case DW_AT_data_member_location:
4782       return "DW_AT_data_member_location";
4783     case DW_AT_decl_column:
4784       return "DW_AT_decl_column";
4785     case DW_AT_decl_file:
4786       return "DW_AT_decl_file";
4787     case DW_AT_decl_line:
4788       return "DW_AT_decl_line";
4789     case DW_AT_declaration:
4790       return "DW_AT_declaration";
4791     case DW_AT_discr_list:
4792       return "DW_AT_discr_list";
4793     case DW_AT_encoding:
4794       return "DW_AT_encoding";
4795     case DW_AT_external:
4796       return "DW_AT_external";
4797     case DW_AT_frame_base:
4798       return "DW_AT_frame_base";
4799     case DW_AT_friend:
4800       return "DW_AT_friend";
4801     case DW_AT_identifier_case:
4802       return "DW_AT_identifier_case";
4803     case DW_AT_macro_info:
4804       return "DW_AT_macro_info";
4805     case DW_AT_namelist_items:
4806       return "DW_AT_namelist_items";
4807     case DW_AT_priority:
4808       return "DW_AT_priority";
4809     case DW_AT_segment:
4810       return "DW_AT_segment";
4811     case DW_AT_specification:
4812       return "DW_AT_specification";
4813     case DW_AT_static_link:
4814       return "DW_AT_static_link";
4815     case DW_AT_type:
4816       return "DW_AT_type";
4817     case DW_AT_use_location:
4818       return "DW_AT_use_location";
4819     case DW_AT_variable_parameter:
4820       return "DW_AT_variable_parameter";
4821     case DW_AT_virtuality:
4822       return "DW_AT_virtuality";
4823     case DW_AT_vtable_elem_location:
4824       return "DW_AT_vtable_elem_location";
4825
4826     case DW_AT_allocated:
4827       return "DW_AT_allocated";
4828     case DW_AT_associated:
4829       return "DW_AT_associated";
4830     case DW_AT_data_location:
4831       return "DW_AT_data_location";
4832     case DW_AT_byte_stride:
4833       return "DW_AT_byte_stride";
4834     case DW_AT_entry_pc:
4835       return "DW_AT_entry_pc";
4836     case DW_AT_use_UTF8:
4837       return "DW_AT_use_UTF8";
4838     case DW_AT_extension:
4839       return "DW_AT_extension";
4840     case DW_AT_ranges:
4841       return "DW_AT_ranges";
4842     case DW_AT_trampoline:
4843       return "DW_AT_trampoline";
4844     case DW_AT_call_column:
4845       return "DW_AT_call_column";
4846     case DW_AT_call_file:
4847       return "DW_AT_call_file";
4848     case DW_AT_call_line:
4849       return "DW_AT_call_line";
4850
4851     case DW_AT_MIPS_fde:
4852       return "DW_AT_MIPS_fde";
4853     case DW_AT_MIPS_loop_begin:
4854       return "DW_AT_MIPS_loop_begin";
4855     case DW_AT_MIPS_tail_loop_begin:
4856       return "DW_AT_MIPS_tail_loop_begin";
4857     case DW_AT_MIPS_epilog_begin:
4858       return "DW_AT_MIPS_epilog_begin";
4859     case DW_AT_MIPS_loop_unroll_factor:
4860       return "DW_AT_MIPS_loop_unroll_factor";
4861     case DW_AT_MIPS_software_pipeline_depth:
4862       return "DW_AT_MIPS_software_pipeline_depth";
4863     case DW_AT_MIPS_linkage_name:
4864       return "DW_AT_MIPS_linkage_name";
4865     case DW_AT_MIPS_stride:
4866       return "DW_AT_MIPS_stride";
4867     case DW_AT_MIPS_abstract_name:
4868       return "DW_AT_MIPS_abstract_name";
4869     case DW_AT_MIPS_clone_origin:
4870       return "DW_AT_MIPS_clone_origin";
4871     case DW_AT_MIPS_has_inlines:
4872       return "DW_AT_MIPS_has_inlines";
4873
4874     case DW_AT_sf_names:
4875       return "DW_AT_sf_names";
4876     case DW_AT_src_info:
4877       return "DW_AT_src_info";
4878     case DW_AT_mac_info:
4879       return "DW_AT_mac_info";
4880     case DW_AT_src_coords:
4881       return "DW_AT_src_coords";
4882     case DW_AT_body_begin:
4883       return "DW_AT_body_begin";
4884     case DW_AT_body_end:
4885       return "DW_AT_body_end";
4886     case DW_AT_GNU_vector:
4887       return "DW_AT_GNU_vector";
4888
4889     case DW_AT_VMS_rtnbeg_pd_address:
4890       return "DW_AT_VMS_rtnbeg_pd_address";
4891
4892     default:
4893       return "DW_AT_<unknown>";
4894     }
4895 }
4896
4897 /* Convert a DWARF value form code into its string name.  */
4898
4899 static const char *
4900 dwarf_form_name (unsigned int form)
4901 {
4902   switch (form)
4903     {
4904     case DW_FORM_addr:
4905       return "DW_FORM_addr";
4906     case DW_FORM_block2:
4907       return "DW_FORM_block2";
4908     case DW_FORM_block4:
4909       return "DW_FORM_block4";
4910     case DW_FORM_data2:
4911       return "DW_FORM_data2";
4912     case DW_FORM_data4:
4913       return "DW_FORM_data4";
4914     case DW_FORM_data8:
4915       return "DW_FORM_data8";
4916     case DW_FORM_string:
4917       return "DW_FORM_string";
4918     case DW_FORM_block:
4919       return "DW_FORM_block";
4920     case DW_FORM_block1:
4921       return "DW_FORM_block1";
4922     case DW_FORM_data1:
4923       return "DW_FORM_data1";
4924     case DW_FORM_flag:
4925       return "DW_FORM_flag";
4926     case DW_FORM_sdata:
4927       return "DW_FORM_sdata";
4928     case DW_FORM_strp:
4929       return "DW_FORM_strp";
4930     case DW_FORM_udata:
4931       return "DW_FORM_udata";
4932     case DW_FORM_ref_addr:
4933       return "DW_FORM_ref_addr";
4934     case DW_FORM_ref1:
4935       return "DW_FORM_ref1";
4936     case DW_FORM_ref2:
4937       return "DW_FORM_ref2";
4938     case DW_FORM_ref4:
4939       return "DW_FORM_ref4";
4940     case DW_FORM_ref8:
4941       return "DW_FORM_ref8";
4942     case DW_FORM_ref_udata:
4943       return "DW_FORM_ref_udata";
4944     case DW_FORM_indirect:
4945       return "DW_FORM_indirect";
4946     default:
4947       return "DW_FORM_<unknown>";
4948     }
4949 }
4950 \f
4951 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4952    instance of an inlined instance of a decl which is local to an inline
4953    function, so we have to trace all of the way back through the origin chain
4954    to find out what sort of node actually served as the original seed for the
4955    given block.  */
4956
4957 static tree
4958 decl_ultimate_origin (const_tree decl)
4959 {
4960   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4961     return NULL_TREE;
4962
4963   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4964      nodes in the function to point to themselves; ignore that if
4965      we're trying to output the abstract instance of this function.  */
4966   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4967     return NULL_TREE;
4968
4969   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4970      most distant ancestor, this should never happen.  */
4971   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4972
4973   return DECL_ABSTRACT_ORIGIN (decl);
4974 }
4975
4976 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4977    instance of an inlined instance of a block which is local to an inline
4978    function, so we have to trace all of the way back through the origin chain
4979    to find out what sort of node actually served as the original seed for the
4980    given block.  */
4981
4982 static tree
4983 block_ultimate_origin (const_tree block)
4984 {
4985   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4986
4987   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4988      nodes in the function to point to themselves; ignore that if
4989      we're trying to output the abstract instance of this function.  */
4990   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4991     return NULL_TREE;
4992
4993   if (immediate_origin == NULL_TREE)
4994     return NULL_TREE;
4995   else
4996     {
4997       tree ret_val;
4998       tree lookahead = immediate_origin;
4999
5000       do
5001         {
5002           ret_val = lookahead;
5003           lookahead = (TREE_CODE (ret_val) == BLOCK
5004                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5005         }
5006       while (lookahead != NULL && lookahead != ret_val);
5007
5008       /* The block's abstract origin chain may not be the *ultimate* origin of
5009          the block. It could lead to a DECL that has an abstract origin set.
5010          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5011          will give us if it has one).  Note that DECL's abstract origins are
5012          supposed to be the most distant ancestor (or so decl_ultimate_origin
5013          claims), so we don't need to loop following the DECL origins.  */
5014       if (DECL_P (ret_val))
5015         return DECL_ORIGIN (ret_val);
5016
5017       return ret_val;
5018     }
5019 }
5020
5021 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5022    of a virtual function may refer to a base class, so we check the 'this'
5023    parameter.  */
5024
5025 static tree
5026 decl_class_context (tree decl)
5027 {
5028   tree context = NULL_TREE;
5029
5030   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5031     context = DECL_CONTEXT (decl);
5032   else
5033     context = TYPE_MAIN_VARIANT
5034       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5035
5036   if (context && !TYPE_P (context))
5037     context = NULL_TREE;
5038
5039   return context;
5040 }
5041 \f
5042 /* Add an attribute/value pair to a DIE.  */
5043
5044 static inline void
5045 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5046 {
5047   /* Maybe this should be an assert?  */
5048   if (die == NULL)
5049     return;
5050
5051   if (die->die_attr == NULL)
5052     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5053   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5054 }
5055
5056 static inline enum dw_val_class
5057 AT_class (dw_attr_ref a)
5058 {
5059   return a->dw_attr_val.val_class;
5060 }
5061
5062 /* Add a flag value attribute to a DIE.  */
5063
5064 static inline void
5065 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5066 {
5067   dw_attr_node attr;
5068
5069   attr.dw_attr = attr_kind;
5070   attr.dw_attr_val.val_class = dw_val_class_flag;
5071   attr.dw_attr_val.v.val_flag = flag;
5072   add_dwarf_attr (die, &attr);
5073 }
5074
5075 static inline unsigned
5076 AT_flag (dw_attr_ref a)
5077 {
5078   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5079   return a->dw_attr_val.v.val_flag;
5080 }
5081
5082 /* Add a signed integer attribute value to a DIE.  */
5083
5084 static inline void
5085 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5086 {
5087   dw_attr_node attr;
5088
5089   attr.dw_attr = attr_kind;
5090   attr.dw_attr_val.val_class = dw_val_class_const;
5091   attr.dw_attr_val.v.val_int = int_val;
5092   add_dwarf_attr (die, &attr);
5093 }
5094
5095 static inline HOST_WIDE_INT
5096 AT_int (dw_attr_ref a)
5097 {
5098   gcc_assert (a && AT_class (a) == dw_val_class_const);
5099   return a->dw_attr_val.v.val_int;
5100 }
5101
5102 /* Add an unsigned integer attribute value to a DIE.  */
5103
5104 static inline void
5105 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5106                  unsigned HOST_WIDE_INT unsigned_val)
5107 {
5108   dw_attr_node attr;
5109
5110   attr.dw_attr = attr_kind;
5111   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5112   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5113   add_dwarf_attr (die, &attr);
5114 }
5115
5116 static inline unsigned HOST_WIDE_INT
5117 AT_unsigned (dw_attr_ref a)
5118 {
5119   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5120   return a->dw_attr_val.v.val_unsigned;
5121 }
5122
5123 /* Add an unsigned double integer attribute value to a DIE.  */
5124
5125 static inline void
5126 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5127                   long unsigned int val_hi, long unsigned int val_low)
5128 {
5129   dw_attr_node attr;
5130
5131   attr.dw_attr = attr_kind;
5132   attr.dw_attr_val.val_class = dw_val_class_long_long;
5133   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5134   attr.dw_attr_val.v.val_long_long.low = val_low;
5135   add_dwarf_attr (die, &attr);
5136 }
5137
5138 /* Add a floating point attribute value to a DIE and return it.  */
5139
5140 static inline void
5141 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5142             unsigned int length, unsigned int elt_size, unsigned char *array)
5143 {
5144   dw_attr_node attr;
5145
5146   attr.dw_attr = attr_kind;
5147   attr.dw_attr_val.val_class = dw_val_class_vec;
5148   attr.dw_attr_val.v.val_vec.length = length;
5149   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5150   attr.dw_attr_val.v.val_vec.array = array;
5151   add_dwarf_attr (die, &attr);
5152 }
5153
5154 /* Hash and equality functions for debug_str_hash.  */
5155
5156 static hashval_t
5157 debug_str_do_hash (const void *x)
5158 {
5159   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5160 }
5161
5162 static int
5163 debug_str_eq (const void *x1, const void *x2)
5164 {
5165   return strcmp ((((const struct indirect_string_node *)x1)->str),
5166                  (const char *)x2) == 0;
5167 }
5168
5169 /* Add a string attribute value to a DIE.  */
5170
5171 static inline void
5172 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5173 {
5174   dw_attr_node attr;
5175   struct indirect_string_node *node;
5176   void **slot;
5177
5178   if (! debug_str_hash)
5179     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5180                                       debug_str_eq, NULL);
5181
5182   slot = htab_find_slot_with_hash (debug_str_hash, str,
5183                                    htab_hash_string (str), INSERT);
5184   if (*slot == NULL)
5185     {
5186       node = (struct indirect_string_node *)
5187                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5188       node->str = ggc_strdup (str);
5189       *slot = node;
5190     }
5191   else
5192     node = (struct indirect_string_node *) *slot;
5193
5194   node->refcount++;
5195
5196   attr.dw_attr = attr_kind;
5197   attr.dw_attr_val.val_class = dw_val_class_str;
5198   attr.dw_attr_val.v.val_str = node;
5199   add_dwarf_attr (die, &attr);
5200 }
5201
5202 static inline const char *
5203 AT_string (dw_attr_ref a)
5204 {
5205   gcc_assert (a && AT_class (a) == dw_val_class_str);
5206   return a->dw_attr_val.v.val_str->str;
5207 }
5208
5209 /* Find out whether a string should be output inline in DIE
5210    or out-of-line in .debug_str section.  */
5211
5212 static int
5213 AT_string_form (dw_attr_ref a)
5214 {
5215   struct indirect_string_node *node;
5216   unsigned int len;
5217   char label[32];
5218
5219   gcc_assert (a && AT_class (a) == dw_val_class_str);
5220
5221   node = a->dw_attr_val.v.val_str;
5222   if (node->form)
5223     return node->form;
5224
5225   len = strlen (node->str) + 1;
5226
5227   /* If the string is shorter or equal to the size of the reference, it is
5228      always better to put it inline.  */
5229   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5230     return node->form = DW_FORM_string;
5231
5232   /* If we cannot expect the linker to merge strings in .debug_str
5233      section, only put it into .debug_str if it is worth even in this
5234      single module.  */
5235   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5236       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5237     return node->form = DW_FORM_string;
5238
5239   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5240   ++dw2_string_counter;
5241   node->label = xstrdup (label);
5242
5243   return node->form = DW_FORM_strp;
5244 }
5245
5246 /* Add a DIE reference attribute value to a DIE.  */
5247
5248 static inline void
5249 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5250 {
5251   dw_attr_node attr;
5252
5253   attr.dw_attr = attr_kind;
5254   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5255   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5256   attr.dw_attr_val.v.val_die_ref.external = 0;
5257   add_dwarf_attr (die, &attr);
5258 }
5259
5260 /* Add an AT_specification attribute to a DIE, and also make the back
5261    pointer from the specification to the definition.  */
5262
5263 static inline void
5264 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5265 {
5266   add_AT_die_ref (die, DW_AT_specification, targ_die);
5267   gcc_assert (!targ_die->die_definition);
5268   targ_die->die_definition = die;
5269 }
5270
5271 static inline dw_die_ref
5272 AT_ref (dw_attr_ref a)
5273 {
5274   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5275   return a->dw_attr_val.v.val_die_ref.die;
5276 }
5277
5278 static inline int
5279 AT_ref_external (dw_attr_ref a)
5280 {
5281   if (a && AT_class (a) == dw_val_class_die_ref)
5282     return a->dw_attr_val.v.val_die_ref.external;
5283
5284   return 0;
5285 }
5286
5287 static inline void
5288 set_AT_ref_external (dw_attr_ref a, int i)
5289 {
5290   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5291   a->dw_attr_val.v.val_die_ref.external = i;
5292 }
5293
5294 /* Add an FDE reference attribute value to a DIE.  */
5295
5296 static inline void
5297 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5298 {
5299   dw_attr_node attr;
5300
5301   attr.dw_attr = attr_kind;
5302   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5303   attr.dw_attr_val.v.val_fde_index = targ_fde;
5304   add_dwarf_attr (die, &attr);
5305 }
5306
5307 /* Add a location description attribute value to a DIE.  */
5308
5309 static inline void
5310 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5311 {
5312   dw_attr_node attr;
5313
5314   attr.dw_attr = attr_kind;
5315   attr.dw_attr_val.val_class = dw_val_class_loc;
5316   attr.dw_attr_val.v.val_loc = loc;
5317   add_dwarf_attr (die, &attr);
5318 }
5319
5320 static inline dw_loc_descr_ref
5321 AT_loc (dw_attr_ref a)
5322 {
5323   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5324   return a->dw_attr_val.v.val_loc;
5325 }
5326
5327 static inline void
5328 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5329 {
5330   dw_attr_node attr;
5331
5332   attr.dw_attr = attr_kind;
5333   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5334   attr.dw_attr_val.v.val_loc_list = loc_list;
5335   add_dwarf_attr (die, &attr);
5336   have_location_lists = true;
5337 }
5338
5339 static inline dw_loc_list_ref
5340 AT_loc_list (dw_attr_ref a)
5341 {
5342   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5343   return a->dw_attr_val.v.val_loc_list;
5344 }
5345
5346 /* Add an address constant attribute value to a DIE.  */
5347
5348 static inline void
5349 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5350 {
5351   dw_attr_node attr;
5352
5353   attr.dw_attr = attr_kind;
5354   attr.dw_attr_val.val_class = dw_val_class_addr;
5355   attr.dw_attr_val.v.val_addr = addr;
5356   add_dwarf_attr (die, &attr);
5357 }
5358
5359 /* Get the RTX from to an address DIE attribute.  */
5360
5361 static inline rtx
5362 AT_addr (dw_attr_ref a)
5363 {
5364   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5365   return a->dw_attr_val.v.val_addr;
5366 }
5367
5368 /* Add a file attribute value to a DIE.  */
5369
5370 static inline void
5371 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5372              struct dwarf_file_data *fd)
5373 {
5374   dw_attr_node attr;
5375
5376   attr.dw_attr = attr_kind;
5377   attr.dw_attr_val.val_class = dw_val_class_file;
5378   attr.dw_attr_val.v.val_file = fd;
5379   add_dwarf_attr (die, &attr);
5380 }
5381
5382 /* Get the dwarf_file_data from a file DIE attribute.  */
5383
5384 static inline struct dwarf_file_data *
5385 AT_file (dw_attr_ref a)
5386 {
5387   gcc_assert (a && AT_class (a) == dw_val_class_file);
5388   return a->dw_attr_val.v.val_file;
5389 }
5390
5391 /* Add a label identifier attribute value to a DIE.  */
5392
5393 static inline void
5394 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5395 {
5396   dw_attr_node attr;
5397
5398   attr.dw_attr = attr_kind;
5399   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5400   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5401   add_dwarf_attr (die, &attr);
5402 }
5403
5404 /* Add a section offset attribute value to a DIE, an offset into the
5405    debug_line section.  */
5406
5407 static inline void
5408 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5409                 const char *label)
5410 {
5411   dw_attr_node attr;
5412
5413   attr.dw_attr = attr_kind;
5414   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5415   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5416   add_dwarf_attr (die, &attr);
5417 }
5418
5419 /* Add a section offset attribute value to a DIE, an offset into the
5420    debug_macinfo section.  */
5421
5422 static inline void
5423 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5424                const char *label)
5425 {
5426   dw_attr_node attr;
5427
5428   attr.dw_attr = attr_kind;
5429   attr.dw_attr_val.val_class = dw_val_class_macptr;
5430   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5431   add_dwarf_attr (die, &attr);
5432 }
5433
5434 /* Add an offset attribute value to a DIE.  */
5435
5436 static inline void
5437 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5438                unsigned HOST_WIDE_INT offset)
5439 {
5440   dw_attr_node attr;
5441
5442   attr.dw_attr = attr_kind;
5443   attr.dw_attr_val.val_class = dw_val_class_offset;
5444   attr.dw_attr_val.v.val_offset = offset;
5445   add_dwarf_attr (die, &attr);
5446 }
5447
5448 /* Add an range_list attribute value to a DIE.  */
5449
5450 static void
5451 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5452                    long unsigned int offset)
5453 {
5454   dw_attr_node attr;
5455
5456   attr.dw_attr = attr_kind;
5457   attr.dw_attr_val.val_class = dw_val_class_range_list;
5458   attr.dw_attr_val.v.val_offset = offset;
5459   add_dwarf_attr (die, &attr);
5460 }
5461
5462 static inline const char *
5463 AT_lbl (dw_attr_ref a)
5464 {
5465   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5466                     || AT_class (a) == dw_val_class_lineptr
5467                     || AT_class (a) == dw_val_class_macptr));
5468   return a->dw_attr_val.v.val_lbl_id;
5469 }
5470
5471 /* Get the attribute of type attr_kind.  */
5472
5473 static dw_attr_ref
5474 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5475 {
5476   dw_attr_ref a;
5477   unsigned ix;
5478   dw_die_ref spec = NULL;
5479
5480   if (! die)
5481     return NULL;
5482
5483   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5484     if (a->dw_attr == attr_kind)
5485       return a;
5486     else if (a->dw_attr == DW_AT_specification
5487              || a->dw_attr == DW_AT_abstract_origin)
5488       spec = AT_ref (a);
5489
5490   if (spec)
5491     return get_AT (spec, attr_kind);
5492
5493   return NULL;
5494 }
5495
5496 /* Return the "low pc" attribute value, typically associated with a subprogram
5497    DIE.  Return null if the "low pc" attribute is either not present, or if it
5498    cannot be represented as an assembler label identifier.  */
5499
5500 static inline const char *
5501 get_AT_low_pc (dw_die_ref die)
5502 {
5503   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5504
5505   return a ? AT_lbl (a) : NULL;
5506 }
5507
5508 /* Return the "high pc" attribute value, typically associated with a subprogram
5509    DIE.  Return null if the "high pc" attribute is either not present, or if it
5510    cannot be represented as an assembler label identifier.  */
5511
5512 static inline const char *
5513 get_AT_hi_pc (dw_die_ref die)
5514 {
5515   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5516
5517   return a ? AT_lbl (a) : NULL;
5518 }
5519
5520 /* Return the value of the string attribute designated by ATTR_KIND, or
5521    NULL if it is not present.  */
5522
5523 static inline const char *
5524 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5525 {
5526   dw_attr_ref a = get_AT (die, attr_kind);
5527
5528   return a ? AT_string (a) : NULL;
5529 }
5530
5531 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5532    if it is not present.  */
5533
5534 static inline int
5535 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5536 {
5537   dw_attr_ref a = get_AT (die, attr_kind);
5538
5539   return a ? AT_flag (a) : 0;
5540 }
5541
5542 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5543    if it is not present.  */
5544
5545 static inline unsigned
5546 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5547 {
5548   dw_attr_ref a = get_AT (die, attr_kind);
5549
5550   return a ? AT_unsigned (a) : 0;
5551 }
5552
5553 static inline dw_die_ref
5554 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5555 {
5556   dw_attr_ref a = get_AT (die, attr_kind);
5557
5558   return a ? AT_ref (a) : NULL;
5559 }
5560
5561 static inline struct dwarf_file_data *
5562 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5563 {
5564   dw_attr_ref a = get_AT (die, attr_kind);
5565
5566   return a ? AT_file (a) : NULL;
5567 }
5568
5569 /* Return TRUE if the language is C or C++.  */
5570
5571 static inline bool
5572 is_c_family (void)
5573 {
5574   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5575
5576   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5577           || lang == DW_LANG_C99
5578           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5579 }
5580
5581 /* Return TRUE if the language is C++.  */
5582
5583 static inline bool
5584 is_cxx (void)
5585 {
5586   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5587
5588   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5589 }
5590
5591 /* Return TRUE if the language is Fortran.  */
5592
5593 static inline bool
5594 is_fortran (void)
5595 {
5596   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5597
5598   return (lang == DW_LANG_Fortran77
5599           || lang == DW_LANG_Fortran90
5600           || lang == DW_LANG_Fortran95);
5601 }
5602
5603 /* Return TRUE if the language is Java.  */
5604
5605 static inline bool
5606 is_java (void)
5607 {
5608   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5609
5610   return lang == DW_LANG_Java;
5611 }
5612
5613 /* Return TRUE if the language is Ada.  */
5614
5615 static inline bool
5616 is_ada (void)
5617 {
5618   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5619
5620   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5621 }
5622
5623 /* Remove the specified attribute if present.  */
5624
5625 static void
5626 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5627 {
5628   dw_attr_ref a;
5629   unsigned ix;
5630
5631   if (! die)
5632     return;
5633
5634   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5635     if (a->dw_attr == attr_kind)
5636       {
5637         if (AT_class (a) == dw_val_class_str)
5638           if (a->dw_attr_val.v.val_str->refcount)
5639             a->dw_attr_val.v.val_str->refcount--;
5640
5641         /* VEC_ordered_remove should help reduce the number of abbrevs
5642            that are needed.  */
5643         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5644         return;
5645       }
5646 }
5647
5648 /* Remove CHILD from its parent.  PREV must have the property that
5649    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5650
5651 static void
5652 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5653 {
5654   gcc_assert (child->die_parent == prev->die_parent);
5655   gcc_assert (prev->die_sib == child);
5656   if (prev == child)
5657     {
5658       gcc_assert (child->die_parent->die_child == child);
5659       prev = NULL;
5660     }
5661   else
5662     prev->die_sib = child->die_sib;
5663   if (child->die_parent->die_child == child)
5664     child->die_parent->die_child = prev;
5665 }
5666
5667 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5668    matches TAG.  */
5669
5670 static void
5671 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5672 {
5673   dw_die_ref c;
5674
5675   c = die->die_child;
5676   if (c) do {
5677     dw_die_ref prev = c;
5678     c = c->die_sib;
5679     while (c->die_tag == tag)
5680       {
5681         remove_child_with_prev (c, prev);
5682         /* Might have removed every child.  */
5683         if (c == c->die_sib)
5684           return;
5685         c = c->die_sib;
5686       }
5687   } while (c != die->die_child);
5688 }
5689
5690 /* Add a CHILD_DIE as the last child of DIE.  */
5691
5692 static void
5693 add_child_die (dw_die_ref die, dw_die_ref child_die)
5694 {
5695   /* FIXME this should probably be an assert.  */
5696   if (! die || ! child_die)
5697     return;
5698   gcc_assert (die != child_die);
5699
5700   child_die->die_parent = die;
5701   if (die->die_child)
5702     {
5703       child_die->die_sib = die->die_child->die_sib;
5704       die->die_child->die_sib = child_die;
5705     }
5706   else
5707     child_die->die_sib = child_die;
5708   die->die_child = child_die;
5709 }
5710
5711 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5712    is the specification, to the end of PARENT's list of children.
5713    This is done by removing and re-adding it.  */
5714
5715 static void
5716 splice_child_die (dw_die_ref parent, dw_die_ref child)
5717 {
5718   dw_die_ref p;
5719
5720   /* We want the declaration DIE from inside the class, not the
5721      specification DIE at toplevel.  */
5722   if (child->die_parent != parent)
5723     {
5724       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5725
5726       if (tmp)
5727         child = tmp;
5728     }
5729
5730   gcc_assert (child->die_parent == parent
5731               || (child->die_parent
5732                   == get_AT_ref (parent, DW_AT_specification)));
5733
5734   for (p = child->die_parent->die_child; ; p = p->die_sib)
5735     if (p->die_sib == child)
5736       {
5737         remove_child_with_prev (child, p);
5738         break;
5739       }
5740
5741   add_child_die (parent, child);
5742 }
5743
5744 /* Return a pointer to a newly created DIE node.  */
5745
5746 static inline dw_die_ref
5747 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5748 {
5749   dw_die_ref die = GGC_CNEW (die_node);
5750
5751   die->die_tag = tag_value;
5752
5753   if (parent_die != NULL)
5754     add_child_die (parent_die, die);
5755   else
5756     {
5757       limbo_die_node *limbo_node;
5758
5759       limbo_node = GGC_CNEW (limbo_die_node);
5760       limbo_node->die = die;
5761       limbo_node->created_for = t;
5762       limbo_node->next = limbo_die_list;
5763       limbo_die_list = limbo_node;
5764     }
5765
5766   return die;
5767 }
5768
5769 /* Return the DIE associated with the given type specifier.  */
5770
5771 static inline dw_die_ref
5772 lookup_type_die (tree type)
5773 {
5774   return TYPE_SYMTAB_DIE (type);
5775 }
5776
5777 /* Equate a DIE to a given type specifier.  */
5778
5779 static inline void
5780 equate_type_number_to_die (tree type, dw_die_ref type_die)
5781 {
5782   TYPE_SYMTAB_DIE (type) = type_die;
5783 }
5784
5785 /* Returns a hash value for X (which really is a die_struct).  */
5786
5787 static hashval_t
5788 decl_die_table_hash (const void *x)
5789 {
5790   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5791 }
5792
5793 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5794
5795 static int
5796 decl_die_table_eq (const void *x, const void *y)
5797 {
5798   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5799 }
5800
5801 /* Return the DIE associated with a given declaration.  */
5802
5803 static inline dw_die_ref
5804 lookup_decl_die (tree decl)
5805 {
5806   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5807 }
5808
5809 /* Returns a hash value for X (which really is a var_loc_list).  */
5810
5811 static hashval_t
5812 decl_loc_table_hash (const void *x)
5813 {
5814   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5815 }
5816
5817 /* Return nonzero if decl_id of var_loc_list X is the same as
5818    UID of decl *Y.  */
5819
5820 static int
5821 decl_loc_table_eq (const void *x, const void *y)
5822 {
5823   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5824 }
5825
5826 /* Return the var_loc list associated with a given declaration.  */
5827
5828 static inline var_loc_list *
5829 lookup_decl_loc (const_tree decl)
5830 {
5831   return (var_loc_list *)
5832     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5833 }
5834
5835 /* Equate a DIE to a particular declaration.  */
5836
5837 static void
5838 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5839 {
5840   unsigned int decl_id = DECL_UID (decl);
5841   void **slot;
5842
5843   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5844   *slot = decl_die;
5845   decl_die->decl_id = decl_id;
5846 }
5847
5848 /* Add a variable location node to the linked list for DECL.  */
5849
5850 static void
5851 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5852 {
5853   unsigned int decl_id = DECL_UID (decl);
5854   var_loc_list *temp;
5855   void **slot;
5856
5857   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5858   if (*slot == NULL)
5859     {
5860       temp = GGC_CNEW (var_loc_list);
5861       temp->decl_id = decl_id;
5862       *slot = temp;
5863     }
5864   else
5865     temp = (var_loc_list *) *slot;
5866
5867   if (temp->last)
5868     {
5869       /* If the current location is the same as the end of the list,
5870          and either both or neither of the locations is uninitialized,
5871          we have nothing to do.  */
5872       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5873                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5874           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5875                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5876               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5877                    == VAR_INIT_STATUS_UNINITIALIZED)
5878                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5879                       == VAR_INIT_STATUS_UNINITIALIZED))))
5880         {
5881           /* Add LOC to the end of list and update LAST.  */
5882           temp->last->next = loc;
5883           temp->last = loc;
5884         }
5885     }
5886   /* Do not add empty location to the beginning of the list.  */
5887   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5888     {
5889       temp->first = loc;
5890       temp->last = loc;
5891     }
5892 }
5893 \f
5894 /* Keep track of the number of spaces used to indent the
5895    output of the debugging routines that print the structure of
5896    the DIE internal representation.  */
5897 static int print_indent;
5898
5899 /* Indent the line the number of spaces given by print_indent.  */
5900
5901 static inline void
5902 print_spaces (FILE *outfile)
5903 {
5904   fprintf (outfile, "%*s", print_indent, "");
5905 }
5906
5907 /* Print the information associated with a given DIE, and its children.
5908    This routine is a debugging aid only.  */
5909
5910 static void
5911 print_die (dw_die_ref die, FILE *outfile)
5912 {
5913   dw_attr_ref a;
5914   dw_die_ref c;
5915   unsigned ix;
5916
5917   print_spaces (outfile);
5918   fprintf (outfile, "DIE %4ld: %s\n",
5919            die->die_offset, dwarf_tag_name (die->die_tag));
5920   print_spaces (outfile);
5921   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5922   fprintf (outfile, " offset: %ld\n", die->die_offset);
5923
5924   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5925     {
5926       print_spaces (outfile);
5927       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5928
5929       switch (AT_class (a))
5930         {
5931         case dw_val_class_addr:
5932           fprintf (outfile, "address");
5933           break;
5934         case dw_val_class_offset:
5935           fprintf (outfile, "offset");
5936           break;
5937         case dw_val_class_loc:
5938           fprintf (outfile, "location descriptor");
5939           break;
5940         case dw_val_class_loc_list:
5941           fprintf (outfile, "location list -> label:%s",
5942                    AT_loc_list (a)->ll_symbol);
5943           break;
5944         case dw_val_class_range_list:
5945           fprintf (outfile, "range list");
5946           break;
5947         case dw_val_class_const:
5948           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5949           break;
5950         case dw_val_class_unsigned_const:
5951           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5952           break;
5953         case dw_val_class_long_long:
5954           fprintf (outfile, "constant (%lu,%lu)",
5955                    a->dw_attr_val.v.val_long_long.hi,
5956                    a->dw_attr_val.v.val_long_long.low);
5957           break;
5958         case dw_val_class_vec:
5959           fprintf (outfile, "floating-point or vector constant");
5960           break;
5961         case dw_val_class_flag:
5962           fprintf (outfile, "%u", AT_flag (a));
5963           break;
5964         case dw_val_class_die_ref:
5965           if (AT_ref (a) != NULL)
5966             {
5967               if (AT_ref (a)->die_symbol)
5968                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5969               else
5970                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5971             }
5972           else
5973             fprintf (outfile, "die -> <null>");
5974           break;
5975         case dw_val_class_lbl_id:
5976         case dw_val_class_lineptr:
5977         case dw_val_class_macptr:
5978           fprintf (outfile, "label: %s", AT_lbl (a));
5979           break;
5980         case dw_val_class_str:
5981           if (AT_string (a) != NULL)
5982             fprintf (outfile, "\"%s\"", AT_string (a));
5983           else
5984             fprintf (outfile, "<null>");
5985           break;
5986         case dw_val_class_file:
5987           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5988                    AT_file (a)->emitted_number);
5989           break;
5990         default:
5991           break;
5992         }
5993
5994       fprintf (outfile, "\n");
5995     }
5996
5997   if (die->die_child != NULL)
5998     {
5999       print_indent += 4;
6000       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6001       print_indent -= 4;
6002     }
6003   if (print_indent == 0)
6004     fprintf (outfile, "\n");
6005 }
6006
6007 /* Print the contents of the source code line number correspondence table.
6008    This routine is a debugging aid only.  */
6009
6010 static void
6011 print_dwarf_line_table (FILE *outfile)
6012 {
6013   unsigned i;
6014   dw_line_info_ref line_info;
6015
6016   fprintf (outfile, "\n\nDWARF source line information\n");
6017   for (i = 1; i < line_info_table_in_use; i++)
6018     {
6019       line_info = &line_info_table[i];
6020       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6021                line_info->dw_file_num,
6022                line_info->dw_line_num);
6023     }
6024
6025   fprintf (outfile, "\n\n");
6026 }
6027
6028 /* Print the information collected for a given DIE.  */
6029
6030 void
6031 debug_dwarf_die (dw_die_ref die)
6032 {
6033   print_die (die, stderr);
6034 }
6035
6036 /* Print all DWARF information collected for the compilation unit.
6037    This routine is a debugging aid only.  */
6038
6039 void
6040 debug_dwarf (void)
6041 {
6042   print_indent = 0;
6043   print_die (comp_unit_die, stderr);
6044   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6045     print_dwarf_line_table (stderr);
6046 }
6047 \f
6048 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6049    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6050    DIE that marks the start of the DIEs for this include file.  */
6051
6052 static dw_die_ref
6053 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6054 {
6055   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6056   dw_die_ref new_unit = gen_compile_unit_die (filename);
6057
6058   new_unit->die_sib = old_unit;
6059   return new_unit;
6060 }
6061
6062 /* Close an include-file CU and reopen the enclosing one.  */
6063
6064 static dw_die_ref
6065 pop_compile_unit (dw_die_ref old_unit)
6066 {
6067   dw_die_ref new_unit = old_unit->die_sib;
6068
6069   old_unit->die_sib = NULL;
6070   return new_unit;
6071 }
6072
6073 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6074 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6075
6076 /* Calculate the checksum of a location expression.  */
6077
6078 static inline void
6079 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6080 {
6081   CHECKSUM (loc->dw_loc_opc);
6082   CHECKSUM (loc->dw_loc_oprnd1);
6083   CHECKSUM (loc->dw_loc_oprnd2);
6084 }
6085
6086 /* Calculate the checksum of an attribute.  */
6087
6088 static void
6089 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6090 {
6091   dw_loc_descr_ref loc;
6092   rtx r;
6093
6094   CHECKSUM (at->dw_attr);
6095
6096   /* We don't care that this was compiled with a different compiler
6097      snapshot; if the output is the same, that's what matters.  */
6098   if (at->dw_attr == DW_AT_producer)
6099     return;
6100
6101   switch (AT_class (at))
6102     {
6103     case dw_val_class_const:
6104       CHECKSUM (at->dw_attr_val.v.val_int);
6105       break;
6106     case dw_val_class_unsigned_const:
6107       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6108       break;
6109     case dw_val_class_long_long:
6110       CHECKSUM (at->dw_attr_val.v.val_long_long);
6111       break;
6112     case dw_val_class_vec:
6113       CHECKSUM (at->dw_attr_val.v.val_vec);
6114       break;
6115     case dw_val_class_flag:
6116       CHECKSUM (at->dw_attr_val.v.val_flag);
6117       break;
6118     case dw_val_class_str:
6119       CHECKSUM_STRING (AT_string (at));
6120       break;
6121
6122     case dw_val_class_addr:
6123       r = AT_addr (at);
6124       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6125       CHECKSUM_STRING (XSTR (r, 0));
6126       break;
6127
6128     case dw_val_class_offset:
6129       CHECKSUM (at->dw_attr_val.v.val_offset);
6130       break;
6131
6132     case dw_val_class_loc:
6133       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6134         loc_checksum (loc, ctx);
6135       break;
6136
6137     case dw_val_class_die_ref:
6138       die_checksum (AT_ref (at), ctx, mark);
6139       break;
6140
6141     case dw_val_class_fde_ref:
6142     case dw_val_class_lbl_id:
6143     case dw_val_class_lineptr:
6144     case dw_val_class_macptr:
6145       break;
6146
6147     case dw_val_class_file:
6148       CHECKSUM_STRING (AT_file (at)->filename);
6149       break;
6150
6151     default:
6152       break;
6153     }
6154 }
6155
6156 /* Calculate the checksum of a DIE.  */
6157
6158 static void
6159 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6160 {
6161   dw_die_ref c;
6162   dw_attr_ref a;
6163   unsigned ix;
6164
6165   /* To avoid infinite recursion.  */
6166   if (die->die_mark)
6167     {
6168       CHECKSUM (die->die_mark);
6169       return;
6170     }
6171   die->die_mark = ++(*mark);
6172
6173   CHECKSUM (die->die_tag);
6174
6175   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6176     attr_checksum (a, ctx, mark);
6177
6178   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6179 }
6180
6181 #undef CHECKSUM
6182 #undef CHECKSUM_STRING
6183
6184 /* Do the location expressions look same?  */
6185 static inline int
6186 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6187 {
6188   return loc1->dw_loc_opc == loc2->dw_loc_opc
6189          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6190          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6191 }
6192
6193 /* Do the values look the same?  */
6194 static int
6195 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6196 {
6197   dw_loc_descr_ref loc1, loc2;
6198   rtx r1, r2;
6199
6200   if (v1->val_class != v2->val_class)
6201     return 0;
6202
6203   switch (v1->val_class)
6204     {
6205     case dw_val_class_const:
6206       return v1->v.val_int == v2->v.val_int;
6207     case dw_val_class_unsigned_const:
6208       return v1->v.val_unsigned == v2->v.val_unsigned;
6209     case dw_val_class_long_long:
6210       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6211              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6212     case dw_val_class_vec:
6213       if (v1->v.val_vec.length != v2->v.val_vec.length
6214           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6215         return 0;
6216       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6217                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6218         return 0;
6219       return 1;
6220     case dw_val_class_flag:
6221       return v1->v.val_flag == v2->v.val_flag;
6222     case dw_val_class_str:
6223       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6224
6225     case dw_val_class_addr:
6226       r1 = v1->v.val_addr;
6227       r2 = v2->v.val_addr;
6228       if (GET_CODE (r1) != GET_CODE (r2))
6229         return 0;
6230       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6231       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6232
6233     case dw_val_class_offset:
6234       return v1->v.val_offset == v2->v.val_offset;
6235
6236     case dw_val_class_loc:
6237       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6238            loc1 && loc2;
6239            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6240         if (!same_loc_p (loc1, loc2, mark))
6241           return 0;
6242       return !loc1 && !loc2;
6243
6244     case dw_val_class_die_ref:
6245       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6246
6247     case dw_val_class_fde_ref:
6248     case dw_val_class_lbl_id:
6249     case dw_val_class_lineptr:
6250     case dw_val_class_macptr:
6251       return 1;
6252
6253     case dw_val_class_file:
6254       return v1->v.val_file == v2->v.val_file;
6255
6256     default:
6257       return 1;
6258     }
6259 }
6260
6261 /* Do the attributes look the same?  */
6262
6263 static int
6264 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6265 {
6266   if (at1->dw_attr != at2->dw_attr)
6267     return 0;
6268
6269   /* We don't care that this was compiled with a different compiler
6270      snapshot; if the output is the same, that's what matters. */
6271   if (at1->dw_attr == DW_AT_producer)
6272     return 1;
6273
6274   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6275 }
6276
6277 /* Do the dies look the same?  */
6278
6279 static int
6280 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6281 {
6282   dw_die_ref c1, c2;
6283   dw_attr_ref a1;
6284   unsigned ix;
6285
6286   /* To avoid infinite recursion.  */
6287   if (die1->die_mark)
6288     return die1->die_mark == die2->die_mark;
6289   die1->die_mark = die2->die_mark = ++(*mark);
6290
6291   if (die1->die_tag != die2->die_tag)
6292     return 0;
6293
6294   if (VEC_length (dw_attr_node, die1->die_attr)
6295       != VEC_length (dw_attr_node, die2->die_attr))
6296     return 0;
6297
6298   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6299     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6300       return 0;
6301
6302   c1 = die1->die_child;
6303   c2 = die2->die_child;
6304   if (! c1)
6305     {
6306       if (c2)
6307         return 0;
6308     }
6309   else
6310     for (;;)
6311       {
6312         if (!same_die_p (c1, c2, mark))
6313           return 0;
6314         c1 = c1->die_sib;
6315         c2 = c2->die_sib;
6316         if (c1 == die1->die_child)
6317           {
6318             if (c2 == die2->die_child)
6319               break;
6320             else
6321               return 0;
6322           }
6323     }
6324
6325   return 1;
6326 }
6327
6328 /* Do the dies look the same?  Wrapper around same_die_p.  */
6329
6330 static int
6331 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6332 {
6333   int mark = 0;
6334   int ret = same_die_p (die1, die2, &mark);
6335
6336   unmark_all_dies (die1);
6337   unmark_all_dies (die2);
6338
6339   return ret;
6340 }
6341
6342 /* The prefix to attach to symbols on DIEs in the current comdat debug
6343    info section.  */
6344 static char *comdat_symbol_id;
6345
6346 /* The index of the current symbol within the current comdat CU.  */
6347 static unsigned int comdat_symbol_number;
6348
6349 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6350    children, and set comdat_symbol_id accordingly.  */
6351
6352 static void
6353 compute_section_prefix (dw_die_ref unit_die)
6354 {
6355   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6356   const char *base = die_name ? lbasename (die_name) : "anonymous";
6357   char *name = XALLOCAVEC (char, strlen (base) + 64);
6358   char *p;
6359   int i, mark;
6360   unsigned char checksum[16];
6361   struct md5_ctx ctx;
6362
6363   /* Compute the checksum of the DIE, then append part of it as hex digits to
6364      the name filename of the unit.  */
6365
6366   md5_init_ctx (&ctx);
6367   mark = 0;
6368   die_checksum (unit_die, &ctx, &mark);
6369   unmark_all_dies (unit_die);
6370   md5_finish_ctx (&ctx, checksum);
6371
6372   sprintf (name, "%s.", base);
6373   clean_symbol_name (name);
6374
6375   p = name + strlen (name);
6376   for (i = 0; i < 4; i++)
6377     {
6378       sprintf (p, "%.2x", checksum[i]);
6379       p += 2;
6380     }
6381
6382   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6383   comdat_symbol_number = 0;
6384 }
6385
6386 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6387
6388 static int
6389 is_type_die (dw_die_ref die)
6390 {
6391   switch (die->die_tag)
6392     {
6393     case DW_TAG_array_type:
6394     case DW_TAG_class_type:
6395     case DW_TAG_interface_type:
6396     case DW_TAG_enumeration_type:
6397     case DW_TAG_pointer_type:
6398     case DW_TAG_reference_type:
6399     case DW_TAG_string_type:
6400     case DW_TAG_structure_type:
6401     case DW_TAG_subroutine_type:
6402     case DW_TAG_union_type:
6403     case DW_TAG_ptr_to_member_type:
6404     case DW_TAG_set_type:
6405     case DW_TAG_subrange_type:
6406     case DW_TAG_base_type:
6407     case DW_TAG_const_type:
6408     case DW_TAG_file_type:
6409     case DW_TAG_packed_type:
6410     case DW_TAG_volatile_type:
6411     case DW_TAG_typedef:
6412       return 1;
6413     default:
6414       return 0;
6415     }
6416 }
6417
6418 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6419    Basically, we want to choose the bits that are likely to be shared between
6420    compilations (types) and leave out the bits that are specific to individual
6421    compilations (functions).  */
6422
6423 static int
6424 is_comdat_die (dw_die_ref c)
6425 {
6426   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6427      we do for stabs.  The advantage is a greater likelihood of sharing between
6428      objects that don't include headers in the same order (and therefore would
6429      put the base types in a different comdat).  jason 8/28/00 */
6430
6431   if (c->die_tag == DW_TAG_base_type)
6432     return 0;
6433
6434   if (c->die_tag == DW_TAG_pointer_type
6435       || c->die_tag == DW_TAG_reference_type
6436       || c->die_tag == DW_TAG_const_type
6437       || c->die_tag == DW_TAG_volatile_type)
6438     {
6439       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6440
6441       return t ? is_comdat_die (t) : 0;
6442     }
6443
6444   return is_type_die (c);
6445 }
6446
6447 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6448    compilation unit.  */
6449
6450 static int
6451 is_symbol_die (dw_die_ref c)
6452 {
6453   return (is_type_die (c)
6454           || (get_AT (c, DW_AT_declaration)
6455               && !get_AT (c, DW_AT_specification))
6456           || c->die_tag == DW_TAG_namespace);
6457 }
6458
6459 static char *
6460 gen_internal_sym (const char *prefix)
6461 {
6462   char buf[256];
6463
6464   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6465   return xstrdup (buf);
6466 }
6467
6468 /* Assign symbols to all worthy DIEs under DIE.  */
6469
6470 static void
6471 assign_symbol_names (dw_die_ref die)
6472 {
6473   dw_die_ref c;
6474
6475   if (is_symbol_die (die))
6476     {
6477       if (comdat_symbol_id)
6478         {
6479           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6480
6481           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6482                    comdat_symbol_id, comdat_symbol_number++);
6483           die->die_symbol = xstrdup (p);
6484         }
6485       else
6486         die->die_symbol = gen_internal_sym ("LDIE");
6487     }
6488
6489   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6490 }
6491
6492 struct cu_hash_table_entry
6493 {
6494   dw_die_ref cu;
6495   unsigned min_comdat_num, max_comdat_num;
6496   struct cu_hash_table_entry *next;
6497 };
6498
6499 /* Routines to manipulate hash table of CUs.  */
6500 static hashval_t
6501 htab_cu_hash (const void *of)
6502 {
6503   const struct cu_hash_table_entry *const entry =
6504     (const struct cu_hash_table_entry *) of;
6505
6506   return htab_hash_string (entry->cu->die_symbol);
6507 }
6508
6509 static int
6510 htab_cu_eq (const void *of1, const void *of2)
6511 {
6512   const struct cu_hash_table_entry *const entry1 =
6513     (const struct cu_hash_table_entry *) of1;
6514   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6515
6516   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6517 }
6518
6519 static void
6520 htab_cu_del (void *what)
6521 {
6522   struct cu_hash_table_entry *next,
6523     *entry = (struct cu_hash_table_entry *) what;
6524
6525   while (entry)
6526     {
6527       next = entry->next;
6528       free (entry);
6529       entry = next;
6530     }
6531 }
6532
6533 /* Check whether we have already seen this CU and set up SYM_NUM
6534    accordingly.  */
6535 static int
6536 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6537 {
6538   struct cu_hash_table_entry dummy;
6539   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6540
6541   dummy.max_comdat_num = 0;
6542
6543   slot = (struct cu_hash_table_entry **)
6544     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6545         INSERT);
6546   entry = *slot;
6547
6548   for (; entry; last = entry, entry = entry->next)
6549     {
6550       if (same_die_p_wrap (cu, entry->cu))
6551         break;
6552     }
6553
6554   if (entry)
6555     {
6556       *sym_num = entry->min_comdat_num;
6557       return 1;
6558     }
6559
6560   entry = XCNEW (struct cu_hash_table_entry);
6561   entry->cu = cu;
6562   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6563   entry->next = *slot;
6564   *slot = entry;
6565
6566   return 0;
6567 }
6568
6569 /* Record SYM_NUM to record of CU in HTABLE.  */
6570 static void
6571 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6572 {
6573   struct cu_hash_table_entry **slot, *entry;
6574
6575   slot = (struct cu_hash_table_entry **)
6576     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6577         NO_INSERT);
6578   entry = *slot;
6579
6580   entry->max_comdat_num = sym_num;
6581 }
6582
6583 /* Traverse the DIE (which is always comp_unit_die), and set up
6584    additional compilation units for each of the include files we see
6585    bracketed by BINCL/EINCL.  */
6586
6587 static void
6588 break_out_includes (dw_die_ref die)
6589 {
6590   dw_die_ref c;
6591   dw_die_ref unit = NULL;
6592   limbo_die_node *node, **pnode;
6593   htab_t cu_hash_table;
6594
6595   c = die->die_child;
6596   if (c) do {
6597     dw_die_ref prev = c;
6598     c = c->die_sib;
6599     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6600            || (unit && is_comdat_die (c)))
6601       {
6602         dw_die_ref next = c->die_sib;
6603
6604         /* This DIE is for a secondary CU; remove it from the main one.  */
6605         remove_child_with_prev (c, prev);
6606
6607         if (c->die_tag == DW_TAG_GNU_BINCL)
6608           unit = push_new_compile_unit (unit, c);
6609         else if (c->die_tag == DW_TAG_GNU_EINCL)
6610           unit = pop_compile_unit (unit);
6611         else
6612           add_child_die (unit, c);
6613         c = next;
6614         if (c == die->die_child)
6615           break;
6616       }
6617   } while (c != die->die_child);
6618
6619 #if 0
6620   /* We can only use this in debugging, since the frontend doesn't check
6621      to make sure that we leave every include file we enter.  */
6622   gcc_assert (!unit);
6623 #endif
6624
6625   assign_symbol_names (die);
6626   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6627   for (node = limbo_die_list, pnode = &limbo_die_list;
6628        node;
6629        node = node->next)
6630     {
6631       int is_dupl;
6632
6633       compute_section_prefix (node->die);
6634       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6635                         &comdat_symbol_number);
6636       assign_symbol_names (node->die);
6637       if (is_dupl)
6638         *pnode = node->next;
6639       else
6640         {
6641           pnode = &node->next;
6642           record_comdat_symbol_number (node->die, cu_hash_table,
6643                 comdat_symbol_number);
6644         }
6645     }
6646   htab_delete (cu_hash_table);
6647 }
6648
6649 /* Traverse the DIE and add a sibling attribute if it may have the
6650    effect of speeding up access to siblings.  To save some space,
6651    avoid generating sibling attributes for DIE's without children.  */
6652
6653 static void
6654 add_sibling_attributes (dw_die_ref die)
6655 {
6656   dw_die_ref c;
6657
6658   if (! die->die_child)
6659     return;
6660
6661   if (die->die_parent && die != die->die_parent->die_child)
6662     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6663
6664   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6665 }
6666
6667 /* Output all location lists for the DIE and its children.  */
6668
6669 static void
6670 output_location_lists (dw_die_ref die)
6671 {
6672   dw_die_ref c;
6673   dw_attr_ref a;
6674   unsigned ix;
6675
6676   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6677     if (AT_class (a) == dw_val_class_loc_list)
6678       output_loc_list (AT_loc_list (a));
6679
6680   FOR_EACH_CHILD (die, c, output_location_lists (c));
6681 }
6682
6683 /* The format of each DIE (and its attribute value pairs) is encoded in an
6684    abbreviation table.  This routine builds the abbreviation table and assigns
6685    a unique abbreviation id for each abbreviation entry.  The children of each
6686    die are visited recursively.  */
6687
6688 static void
6689 build_abbrev_table (dw_die_ref die)
6690 {
6691   unsigned long abbrev_id;
6692   unsigned int n_alloc;
6693   dw_die_ref c;
6694   dw_attr_ref a;
6695   unsigned ix;
6696
6697   /* Scan the DIE references, and mark as external any that refer to
6698      DIEs from other CUs (i.e. those which are not marked).  */
6699   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6700     if (AT_class (a) == dw_val_class_die_ref
6701         && AT_ref (a)->die_mark == 0)
6702       {
6703         gcc_assert (AT_ref (a)->die_symbol);
6704
6705         set_AT_ref_external (a, 1);
6706       }
6707
6708   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6709     {
6710       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6711       dw_attr_ref die_a, abbrev_a;
6712       unsigned ix;
6713       bool ok = true;
6714
6715       if (abbrev->die_tag != die->die_tag)
6716         continue;
6717       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6718         continue;
6719
6720       if (VEC_length (dw_attr_node, abbrev->die_attr)
6721           != VEC_length (dw_attr_node, die->die_attr))
6722         continue;
6723
6724       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6725         {
6726           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6727           if ((abbrev_a->dw_attr != die_a->dw_attr)
6728               || (value_format (abbrev_a) != value_format (die_a)))
6729             {
6730               ok = false;
6731               break;
6732             }
6733         }
6734       if (ok)
6735         break;
6736     }
6737
6738   if (abbrev_id >= abbrev_die_table_in_use)
6739     {
6740       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6741         {
6742           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6743           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
6744                                             n_alloc);
6745
6746           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6747                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6748           abbrev_die_table_allocated = n_alloc;
6749         }
6750
6751       ++abbrev_die_table_in_use;
6752       abbrev_die_table[abbrev_id] = die;
6753     }
6754
6755   die->die_abbrev = abbrev_id;
6756   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6757 }
6758 \f
6759 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6760
6761 static int
6762 constant_size (long unsigned int value)
6763 {
6764   int log;
6765
6766   if (value == 0)
6767     log = 0;
6768   else
6769     log = floor_log2 (value);
6770
6771   log = log / 8;
6772   log = 1 << (floor_log2 (log) + 1);
6773
6774   return log;
6775 }
6776
6777 /* Return the size of a DIE as it is represented in the
6778    .debug_info section.  */
6779
6780 static unsigned long
6781 size_of_die (dw_die_ref die)
6782 {
6783   unsigned long size = 0;
6784   dw_attr_ref a;
6785   unsigned ix;
6786
6787   size += size_of_uleb128 (die->die_abbrev);
6788   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6789     {
6790       switch (AT_class (a))
6791         {
6792         case dw_val_class_addr:
6793           size += DWARF2_ADDR_SIZE;
6794           break;
6795         case dw_val_class_offset:
6796           size += DWARF_OFFSET_SIZE;
6797           break;
6798         case dw_val_class_loc:
6799           {
6800             unsigned long lsize = size_of_locs (AT_loc (a));
6801
6802             /* Block length.  */
6803             size += constant_size (lsize);
6804             size += lsize;
6805           }
6806           break;
6807         case dw_val_class_loc_list:
6808           size += DWARF_OFFSET_SIZE;
6809           break;
6810         case dw_val_class_range_list:
6811           size += DWARF_OFFSET_SIZE;
6812           break;
6813         case dw_val_class_const:
6814           size += size_of_sleb128 (AT_int (a));
6815           break;
6816         case dw_val_class_unsigned_const:
6817           size += constant_size (AT_unsigned (a));
6818           break;
6819         case dw_val_class_long_long:
6820           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6821           break;
6822         case dw_val_class_vec:
6823           size += 1 + (a->dw_attr_val.v.val_vec.length
6824                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6825           break;
6826         case dw_val_class_flag:
6827           size += 1;
6828           break;
6829         case dw_val_class_die_ref:
6830           if (AT_ref_external (a))
6831             size += DWARF2_ADDR_SIZE;
6832           else
6833             size += DWARF_OFFSET_SIZE;
6834           break;
6835         case dw_val_class_fde_ref:
6836           size += DWARF_OFFSET_SIZE;
6837           break;
6838         case dw_val_class_lbl_id:
6839           size += DWARF2_ADDR_SIZE;
6840           break;
6841         case dw_val_class_lineptr:
6842         case dw_val_class_macptr:
6843           size += DWARF_OFFSET_SIZE;
6844           break;
6845         case dw_val_class_str:
6846           if (AT_string_form (a) == DW_FORM_strp)
6847             size += DWARF_OFFSET_SIZE;
6848           else
6849             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6850           break;
6851         case dw_val_class_file:
6852           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6853           break;
6854         default:
6855           gcc_unreachable ();
6856         }
6857     }
6858
6859   return size;
6860 }
6861
6862 /* Size the debugging information associated with a given DIE.  Visits the
6863    DIE's children recursively.  Updates the global variable next_die_offset, on
6864    each time through.  Uses the current value of next_die_offset to update the
6865    die_offset field in each DIE.  */
6866
6867 static void
6868 calc_die_sizes (dw_die_ref die)
6869 {
6870   dw_die_ref c;
6871
6872   die->die_offset = next_die_offset;
6873   next_die_offset += size_of_die (die);
6874
6875   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6876
6877   if (die->die_child != NULL)
6878     /* Count the null byte used to terminate sibling lists.  */
6879     next_die_offset += 1;
6880 }
6881
6882 /* Set the marks for a die and its children.  We do this so
6883    that we know whether or not a reference needs to use FORM_ref_addr; only
6884    DIEs in the same CU will be marked.  We used to clear out the offset
6885    and use that as the flag, but ran into ordering problems.  */
6886
6887 static void
6888 mark_dies (dw_die_ref die)
6889 {
6890   dw_die_ref c;
6891
6892   gcc_assert (!die->die_mark);
6893
6894   die->die_mark = 1;
6895   FOR_EACH_CHILD (die, c, mark_dies (c));
6896 }
6897
6898 /* Clear the marks for a die and its children.  */
6899
6900 static void
6901 unmark_dies (dw_die_ref die)
6902 {
6903   dw_die_ref c;
6904
6905   gcc_assert (die->die_mark);
6906
6907   die->die_mark = 0;
6908   FOR_EACH_CHILD (die, c, unmark_dies (c));
6909 }
6910
6911 /* Clear the marks for a die, its children and referred dies.  */
6912
6913 static void
6914 unmark_all_dies (dw_die_ref die)
6915 {
6916   dw_die_ref c;
6917   dw_attr_ref a;
6918   unsigned ix;
6919
6920   if (!die->die_mark)
6921     return;
6922   die->die_mark = 0;
6923
6924   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6925
6926   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6927     if (AT_class (a) == dw_val_class_die_ref)
6928       unmark_all_dies (AT_ref (a));
6929 }
6930
6931 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6932    generated for the compilation unit.  */
6933
6934 static unsigned long
6935 size_of_pubnames (VEC (pubname_entry, gc) * names)
6936 {
6937   unsigned long size;
6938   unsigned i;
6939   pubname_ref p;
6940
6941   size = DWARF_PUBNAMES_HEADER_SIZE;
6942   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6943     if (names != pubtype_table
6944         || p->die->die_offset != 0
6945         || !flag_eliminate_unused_debug_types)
6946       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6947
6948   size += DWARF_OFFSET_SIZE;
6949   return size;
6950 }
6951
6952 /* Return the size of the information in the .debug_aranges section.  */
6953
6954 static unsigned long
6955 size_of_aranges (void)
6956 {
6957   unsigned long size;
6958
6959   size = DWARF_ARANGES_HEADER_SIZE;
6960
6961   /* Count the address/length pair for this compilation unit.  */
6962   if (text_section_used)
6963     size += 2 * DWARF2_ADDR_SIZE;
6964   if (cold_text_section_used)
6965     size += 2 * DWARF2_ADDR_SIZE;
6966   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6967
6968   /* Count the two zero words used to terminated the address range table.  */
6969   size += 2 * DWARF2_ADDR_SIZE;
6970   return size;
6971 }
6972 \f
6973 /* Select the encoding of an attribute value.  */
6974
6975 static enum dwarf_form
6976 value_format (dw_attr_ref a)
6977 {
6978   switch (a->dw_attr_val.val_class)
6979     {
6980     case dw_val_class_addr:
6981       return DW_FORM_addr;
6982     case dw_val_class_range_list:
6983     case dw_val_class_offset:
6984     case dw_val_class_loc_list:
6985       switch (DWARF_OFFSET_SIZE)
6986         {
6987         case 4:
6988           return DW_FORM_data4;
6989         case 8:
6990           return DW_FORM_data8;
6991         default:
6992           gcc_unreachable ();
6993         }
6994     case dw_val_class_loc:
6995       switch (constant_size (size_of_locs (AT_loc (a))))
6996         {
6997         case 1:
6998           return DW_FORM_block1;
6999         case 2:
7000           return DW_FORM_block2;
7001         default:
7002           gcc_unreachable ();
7003         }
7004     case dw_val_class_const:
7005       return DW_FORM_sdata;
7006     case dw_val_class_unsigned_const:
7007       switch (constant_size (AT_unsigned (a)))
7008         {
7009         case 1:
7010           return DW_FORM_data1;
7011         case 2:
7012           return DW_FORM_data2;
7013         case 4:
7014           return DW_FORM_data4;
7015         case 8:
7016           return DW_FORM_data8;
7017         default:
7018           gcc_unreachable ();
7019         }
7020     case dw_val_class_long_long:
7021       return DW_FORM_block1;
7022     case dw_val_class_vec:
7023       return DW_FORM_block1;
7024     case dw_val_class_flag:
7025       return DW_FORM_flag;
7026     case dw_val_class_die_ref:
7027       if (AT_ref_external (a))
7028         return DW_FORM_ref_addr;
7029       else
7030         return DW_FORM_ref;
7031     case dw_val_class_fde_ref:
7032       return DW_FORM_data;
7033     case dw_val_class_lbl_id:
7034       return DW_FORM_addr;
7035     case dw_val_class_lineptr:
7036     case dw_val_class_macptr:
7037       return DW_FORM_data;
7038     case dw_val_class_str:
7039       return AT_string_form (a);
7040     case dw_val_class_file:
7041       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7042         {
7043         case 1:
7044           return DW_FORM_data1;
7045         case 2:
7046           return DW_FORM_data2;
7047         case 4:
7048           return DW_FORM_data4;
7049         default:
7050           gcc_unreachable ();
7051         }
7052
7053     default:
7054       gcc_unreachable ();
7055     }
7056 }
7057
7058 /* Output the encoding of an attribute value.  */
7059
7060 static void
7061 output_value_format (dw_attr_ref a)
7062 {
7063   enum dwarf_form form = value_format (a);
7064
7065   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7066 }
7067
7068 /* Output the .debug_abbrev section which defines the DIE abbreviation
7069    table.  */
7070
7071 static void
7072 output_abbrev_section (void)
7073 {
7074   unsigned long abbrev_id;
7075
7076   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7077     {
7078       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7079       unsigned ix;
7080       dw_attr_ref a_attr;
7081
7082       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7083       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7084                                    dwarf_tag_name (abbrev->die_tag));
7085
7086       if (abbrev->die_child != NULL)
7087         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7088       else
7089         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7090
7091       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7092            ix++)
7093         {
7094           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7095                                        dwarf_attr_name (a_attr->dw_attr));
7096           output_value_format (a_attr);
7097         }
7098
7099       dw2_asm_output_data (1, 0, NULL);
7100       dw2_asm_output_data (1, 0, NULL);
7101     }
7102
7103   /* Terminate the table.  */
7104   dw2_asm_output_data (1, 0, NULL);
7105 }
7106
7107 /* Output a symbol we can use to refer to this DIE from another CU.  */
7108
7109 static inline void
7110 output_die_symbol (dw_die_ref die)
7111 {
7112   char *sym = die->die_symbol;
7113
7114   if (sym == 0)
7115     return;
7116
7117   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7118     /* We make these global, not weak; if the target doesn't support
7119        .linkonce, it doesn't support combining the sections, so debugging
7120        will break.  */
7121     targetm.asm_out.globalize_label (asm_out_file, sym);
7122
7123   ASM_OUTPUT_LABEL (asm_out_file, sym);
7124 }
7125
7126 /* Return a new location list, given the begin and end range, and the
7127    expression. gensym tells us whether to generate a new internal symbol for
7128    this location list node, which is done for the head of the list only.  */
7129
7130 static inline dw_loc_list_ref
7131 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7132               const char *section, unsigned int gensym)
7133 {
7134   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7135
7136   retlist->begin = begin;
7137   retlist->end = end;
7138   retlist->expr = expr;
7139   retlist->section = section;
7140   if (gensym)
7141     retlist->ll_symbol = gen_internal_sym ("LLST");
7142
7143   return retlist;
7144 }
7145
7146 /* Add a location description expression to a location list.  */
7147
7148 static inline void
7149 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7150                            const char *begin, const char *end,
7151                            const char *section)
7152 {
7153   dw_loc_list_ref *d;
7154
7155   /* Find the end of the chain.  */
7156   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7157     ;
7158
7159   /* Add a new location list node to the list.  */
7160   *d = new_loc_list (descr, begin, end, section, 0);
7161 }
7162
7163 /* Output the location list given to us.  */
7164
7165 static void
7166 output_loc_list (dw_loc_list_ref list_head)
7167 {
7168   dw_loc_list_ref curr = list_head;
7169
7170   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7171
7172   /* Walk the location list, and output each range + expression.  */
7173   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7174     {
7175       unsigned long size;
7176       /* Don't output an entry that starts and ends at the same address.  */
7177       if (strcmp (curr->begin, curr->end) == 0)
7178         continue;
7179       if (!have_multiple_function_sections)
7180         {
7181           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7182                                 "Location list begin address (%s)",
7183                                 list_head->ll_symbol);
7184           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7185                                 "Location list end address (%s)",
7186                                 list_head->ll_symbol);
7187         }
7188       else
7189         {
7190           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7191                                "Location list begin address (%s)",
7192                                list_head->ll_symbol);
7193           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7194                                "Location list end address (%s)",
7195                                list_head->ll_symbol);
7196         }
7197       size = size_of_locs (curr->expr);
7198
7199       /* Output the block length for this list of location operations.  */
7200       gcc_assert (size <= 0xffff);
7201       dw2_asm_output_data (2, size, "%s", "Location expression size");
7202
7203       output_loc_sequence (curr->expr);
7204     }
7205
7206   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7207                        "Location list terminator begin (%s)",
7208                        list_head->ll_symbol);
7209   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7210                        "Location list terminator end (%s)",
7211                        list_head->ll_symbol);
7212 }
7213
7214 /* Output the DIE and its attributes.  Called recursively to generate
7215    the definitions of each child DIE.  */
7216
7217 static void
7218 output_die (dw_die_ref die)
7219 {
7220   dw_attr_ref a;
7221   dw_die_ref c;
7222   unsigned long size;
7223   unsigned ix;
7224
7225   /* If someone in another CU might refer to us, set up a symbol for
7226      them to point to.  */
7227   if (die->die_symbol)
7228     output_die_symbol (die);
7229
7230   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7231                                (unsigned long)die->die_offset,
7232                                dwarf_tag_name (die->die_tag));
7233
7234   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7235     {
7236       const char *name = dwarf_attr_name (a->dw_attr);
7237
7238       switch (AT_class (a))
7239         {
7240         case dw_val_class_addr:
7241           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7242           break;
7243
7244         case dw_val_class_offset:
7245           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7246                                "%s", name);
7247           break;
7248
7249         case dw_val_class_range_list:
7250           {
7251             char *p = strchr (ranges_section_label, '\0');
7252
7253             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7254                      a->dw_attr_val.v.val_offset);
7255             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7256                                    debug_ranges_section, "%s", name);
7257             *p = '\0';
7258           }
7259           break;
7260
7261         case dw_val_class_loc:
7262           size = size_of_locs (AT_loc (a));
7263
7264           /* Output the block length for this list of location operations.  */
7265           dw2_asm_output_data (constant_size (size), size, "%s", name);
7266
7267           output_loc_sequence (AT_loc (a));
7268           break;
7269
7270         case dw_val_class_const:
7271           /* ??? It would be slightly more efficient to use a scheme like is
7272              used for unsigned constants below, but gdb 4.x does not sign
7273              extend.  Gdb 5.x does sign extend.  */
7274           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7275           break;
7276
7277         case dw_val_class_unsigned_const:
7278           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7279                                AT_unsigned (a), "%s", name);
7280           break;
7281
7282         case dw_val_class_long_long:
7283           {
7284             unsigned HOST_WIDE_INT first, second;
7285
7286             dw2_asm_output_data (1,
7287                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7288                                  "%s", name);
7289
7290             if (WORDS_BIG_ENDIAN)
7291               {
7292                 first = a->dw_attr_val.v.val_long_long.hi;
7293                 second = a->dw_attr_val.v.val_long_long.low;
7294               }
7295             else
7296               {
7297                 first = a->dw_attr_val.v.val_long_long.low;
7298                 second = a->dw_attr_val.v.val_long_long.hi;
7299               }
7300
7301             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7302                                  first, "long long constant");
7303             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7304                                  second, NULL);
7305           }
7306           break;
7307
7308         case dw_val_class_vec:
7309           {
7310             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7311             unsigned int len = a->dw_attr_val.v.val_vec.length;
7312             unsigned int i;
7313             unsigned char *p;
7314
7315             dw2_asm_output_data (1, len * elt_size, "%s", name);
7316             if (elt_size > sizeof (HOST_WIDE_INT))
7317               {
7318                 elt_size /= 2;
7319                 len *= 2;
7320               }
7321             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7322                  i < len;
7323                  i++, p += elt_size)
7324               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7325                                    "fp or vector constant word %u", i);
7326             break;
7327           }
7328
7329         case dw_val_class_flag:
7330           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7331           break;
7332
7333         case dw_val_class_loc_list:
7334           {
7335             char *sym = AT_loc_list (a)->ll_symbol;
7336
7337             gcc_assert (sym);
7338             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7339                                    "%s", name);
7340           }
7341           break;
7342
7343         case dw_val_class_die_ref:
7344           if (AT_ref_external (a))
7345             {
7346               char *sym = AT_ref (a)->die_symbol;
7347
7348               gcc_assert (sym);
7349               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7350                                      "%s", name);
7351             }
7352           else
7353             {
7354               gcc_assert (AT_ref (a)->die_offset);
7355               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7356                                    "%s", name);
7357             }
7358           break;
7359
7360         case dw_val_class_fde_ref:
7361           {
7362             char l1[20];
7363
7364             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7365                                          a->dw_attr_val.v.val_fde_index * 2);
7366             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7367                                    "%s", name);
7368           }
7369           break;
7370
7371         case dw_val_class_lbl_id:
7372           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7373           break;
7374
7375         case dw_val_class_lineptr:
7376           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7377                                  debug_line_section, "%s", name);
7378           break;
7379
7380         case dw_val_class_macptr:
7381           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7382                                  debug_macinfo_section, "%s", name);
7383           break;
7384
7385         case dw_val_class_str:
7386           if (AT_string_form (a) == DW_FORM_strp)
7387             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7388                                    a->dw_attr_val.v.val_str->label,
7389                                    debug_str_section,
7390                                    "%s: \"%s\"", name, AT_string (a));
7391           else
7392             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7393           break;
7394
7395         case dw_val_class_file:
7396           {
7397             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7398
7399             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7400                                  a->dw_attr_val.v.val_file->filename);
7401             break;
7402           }
7403
7404         default:
7405           gcc_unreachable ();
7406         }
7407     }
7408
7409   FOR_EACH_CHILD (die, c, output_die (c));
7410
7411   /* Add null byte to terminate sibling list.  */
7412   if (die->die_child != NULL)
7413     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7414                          (unsigned long) die->die_offset);
7415 }
7416
7417 /* Output the compilation unit that appears at the beginning of the
7418    .debug_info section, and precedes the DIE descriptions.  */
7419
7420 static void
7421 output_compilation_unit_header (void)
7422 {
7423   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7424     dw2_asm_output_data (4, 0xffffffff,
7425       "Initial length escape value indicating 64-bit DWARF extension");
7426   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7427                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7428                        "Length of Compilation Unit Info");
7429   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7430   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7431                          debug_abbrev_section,
7432                          "Offset Into Abbrev. Section");
7433   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7434 }
7435
7436 /* Output the compilation unit DIE and its children.  */
7437
7438 static void
7439 output_comp_unit (dw_die_ref die, int output_if_empty)
7440 {
7441   const char *secname;
7442   char *oldsym, *tmp;
7443
7444   /* Unless we are outputting main CU, we may throw away empty ones.  */
7445   if (!output_if_empty && die->die_child == NULL)
7446     return;
7447
7448   /* Even if there are no children of this DIE, we must output the information
7449      about the compilation unit.  Otherwise, on an empty translation unit, we
7450      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7451      will then complain when examining the file.  First mark all the DIEs in
7452      this CU so we know which get local refs.  */
7453   mark_dies (die);
7454
7455   build_abbrev_table (die);
7456
7457   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7458   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7459   calc_die_sizes (die);
7460
7461   oldsym = die->die_symbol;
7462   if (oldsym)
7463     {
7464       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
7465
7466       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7467       secname = tmp;
7468       die->die_symbol = NULL;
7469       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7470     }
7471   else
7472     switch_to_section (debug_info_section);
7473
7474   /* Output debugging information.  */
7475   output_compilation_unit_header ();
7476   output_die (die);
7477
7478   /* Leave the marks on the main CU, so we can check them in
7479      output_pubnames.  */
7480   if (oldsym)
7481     {
7482       unmark_dies (die);
7483       die->die_symbol = oldsym;
7484     }
7485 }
7486
7487 /* Return the DWARF2/3 pubname associated with a decl.  */
7488
7489 static const char *
7490 dwarf2_name (tree decl, int scope)
7491 {
7492   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7493 }
7494
7495 /* Add a new entry to .debug_pubnames if appropriate.  */
7496
7497 static void
7498 add_pubname_string (const char *str, dw_die_ref die)
7499 {
7500   pubname_entry e;
7501
7502   e.die = die;
7503   e.name = xstrdup (str);
7504   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7505 }
7506
7507 static void
7508 add_pubname (tree decl, dw_die_ref die)
7509 {
7510
7511   if (TREE_PUBLIC (decl))
7512     add_pubname_string (dwarf2_name (decl, 1), die);
7513 }
7514
7515 /* Add a new entry to .debug_pubtypes if appropriate.  */
7516
7517 static void
7518 add_pubtype (tree decl, dw_die_ref die)
7519 {
7520   pubname_entry e;
7521
7522   e.name = NULL;
7523   if ((TREE_PUBLIC (decl)
7524        || die->die_parent == comp_unit_die)
7525       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7526     {
7527       e.die = die;
7528       if (TYPE_P (decl))
7529         {
7530           if (TYPE_NAME (decl))
7531             {
7532               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7533                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7534               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7535                        && DECL_NAME (TYPE_NAME (decl)))
7536                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7537               else
7538                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7539             }
7540         }
7541       else
7542         e.name = xstrdup (dwarf2_name (decl, 1));
7543
7544       /* If we don't have a name for the type, there's no point in adding
7545          it to the table.  */
7546       if (e.name && e.name[0] != '\0')
7547         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7548     }
7549 }
7550
7551 /* Output the public names table used to speed up access to externally
7552    visible names; or the public types table used to find type definitions.  */
7553
7554 static void
7555 output_pubnames (VEC (pubname_entry, gc) * names)
7556 {
7557   unsigned i;
7558   unsigned long pubnames_length = size_of_pubnames (names);
7559   pubname_ref pub;
7560
7561   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7562     dw2_asm_output_data (4, 0xffffffff,
7563       "Initial length escape value indicating 64-bit DWARF extension");
7564   if (names == pubname_table)
7565     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7566                          "Length of Public Names Info");
7567   else
7568     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7569                          "Length of Public Type Names Info");
7570   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7571   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7572                          debug_info_section,
7573                          "Offset of Compilation Unit Info");
7574   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7575                        "Compilation Unit Length");
7576
7577   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7578     {
7579       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7580       if (names == pubname_table)
7581         gcc_assert (pub->die->die_mark);
7582
7583       if (names != pubtype_table
7584           || pub->die->die_offset != 0
7585           || !flag_eliminate_unused_debug_types)
7586         {
7587           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7588                                "DIE offset");
7589
7590           dw2_asm_output_nstring (pub->name, -1, "external name");
7591         }
7592     }
7593
7594   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7595 }
7596
7597 /* Add a new entry to .debug_aranges if appropriate.  */
7598
7599 static void
7600 add_arange (tree decl, dw_die_ref die)
7601 {
7602   if (! DECL_SECTION_NAME (decl))
7603     return;
7604
7605   if (arange_table_in_use == arange_table_allocated)
7606     {
7607       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7608       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
7609                                     arange_table_allocated);
7610       memset (arange_table + arange_table_in_use, 0,
7611               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7612     }
7613
7614   arange_table[arange_table_in_use++] = die;
7615 }
7616
7617 /* Output the information that goes into the .debug_aranges table.
7618    Namely, define the beginning and ending address range of the
7619    text section generated for this compilation unit.  */
7620
7621 static void
7622 output_aranges (void)
7623 {
7624   unsigned i;
7625   unsigned long aranges_length = size_of_aranges ();
7626
7627   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7628     dw2_asm_output_data (4, 0xffffffff,
7629       "Initial length escape value indicating 64-bit DWARF extension");
7630   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7631                        "Length of Address Ranges Info");
7632   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7633   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7634                          debug_info_section,
7635                          "Offset of Compilation Unit Info");
7636   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7637   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7638
7639   /* We need to align to twice the pointer size here.  */
7640   if (DWARF_ARANGES_PAD_SIZE)
7641     {
7642       /* Pad using a 2 byte words so that padding is correct for any
7643          pointer size.  */
7644       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7645                            2 * DWARF2_ADDR_SIZE);
7646       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7647         dw2_asm_output_data (2, 0, NULL);
7648     }
7649
7650   /* It is necessary not to output these entries if the sections were
7651      not used; if the sections were not used, the length will be 0 and
7652      the address may end up as 0 if the section is discarded by ld
7653      --gc-sections, leaving an invalid (0, 0) entry that can be
7654      confused with the terminator.  */
7655   if (text_section_used)
7656     {
7657       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7658       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7659                             text_section_label, "Length");
7660     }
7661   if (cold_text_section_used)
7662     {
7663       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7664                            "Address");
7665       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7666                             cold_text_section_label, "Length");
7667     }
7668
7669   for (i = 0; i < arange_table_in_use; i++)
7670     {
7671       dw_die_ref die = arange_table[i];
7672
7673       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7674       gcc_assert (die->die_mark);
7675
7676       if (die->die_tag == DW_TAG_subprogram)
7677         {
7678           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7679                                "Address");
7680           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7681                                 get_AT_low_pc (die), "Length");
7682         }
7683       else
7684         {
7685           /* A static variable; extract the symbol from DW_AT_location.
7686              Note that this code isn't currently hit, as we only emit
7687              aranges for functions (jason 9/23/99).  */
7688           dw_attr_ref a = get_AT (die, DW_AT_location);
7689           dw_loc_descr_ref loc;
7690
7691           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7692
7693           loc = AT_loc (a);
7694           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7695
7696           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7697                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7698           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7699                                get_AT_unsigned (die, DW_AT_byte_size),
7700                                "Length");
7701         }
7702     }
7703
7704   /* Output the terminator words.  */
7705   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7706   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7707 }
7708
7709 /* Add a new entry to .debug_ranges.  Return the offset at which it
7710    was placed.  */
7711
7712 static unsigned int
7713 add_ranges_num (int num)
7714 {
7715   unsigned int in_use = ranges_table_in_use;
7716
7717   if (in_use == ranges_table_allocated)
7718     {
7719       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7720       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
7721                                     ranges_table_allocated);
7722       memset (ranges_table + ranges_table_in_use, 0,
7723               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7724     }
7725
7726   ranges_table[in_use].num = num;
7727   ranges_table_in_use = in_use + 1;
7728
7729   return in_use * 2 * DWARF2_ADDR_SIZE;
7730 }
7731
7732 /* Add a new entry to .debug_ranges corresponding to a block, or a
7733    range terminator if BLOCK is NULL.  */
7734
7735 static unsigned int
7736 add_ranges (const_tree block)
7737 {
7738   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7739 }
7740
7741 /* Add a new entry to .debug_ranges corresponding to a pair of
7742    labels.  */
7743
7744 static unsigned int
7745 add_ranges_by_labels (const char *begin, const char *end)
7746 {
7747   unsigned int in_use = ranges_by_label_in_use;
7748
7749   if (in_use == ranges_by_label_allocated)
7750     {
7751       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7752       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
7753                                        ranges_by_label,
7754                                        ranges_by_label_allocated);
7755       memset (ranges_by_label + ranges_by_label_in_use, 0,
7756               RANGES_TABLE_INCREMENT
7757               * sizeof (struct dw_ranges_by_label_struct));
7758     }
7759
7760   ranges_by_label[in_use].begin = begin;
7761   ranges_by_label[in_use].end = end;
7762   ranges_by_label_in_use = in_use + 1;
7763
7764   return add_ranges_num (-(int)in_use - 1);
7765 }
7766
7767 static void
7768 output_ranges (void)
7769 {
7770   unsigned i;
7771   static const char *const start_fmt = "Offset 0x%x";
7772   const char *fmt = start_fmt;
7773
7774   for (i = 0; i < ranges_table_in_use; i++)
7775     {
7776       int block_num = ranges_table[i].num;
7777
7778       if (block_num > 0)
7779         {
7780           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7781           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7782
7783           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7784           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7785
7786           /* If all code is in the text section, then the compilation
7787              unit base address defaults to DW_AT_low_pc, which is the
7788              base of the text section.  */
7789           if (!have_multiple_function_sections)
7790             {
7791               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7792                                     text_section_label,
7793                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7794               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7795                                     text_section_label, NULL);
7796             }
7797
7798           /* Otherwise, the compilation unit base address is zero,
7799              which allows us to use absolute addresses, and not worry
7800              about whether the target supports cross-section
7801              arithmetic.  */
7802           else
7803             {
7804               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7805                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7806               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7807             }
7808
7809           fmt = NULL;
7810         }
7811
7812       /* Negative block_num stands for an index into ranges_by_label.  */
7813       else if (block_num < 0)
7814         {
7815           int lab_idx = - block_num - 1;
7816
7817           if (!have_multiple_function_sections)
7818             {
7819               gcc_unreachable ();
7820 #if 0
7821               /* If we ever use add_ranges_by_labels () for a single
7822                  function section, all we have to do is to take out
7823                  the #if 0 above.  */
7824               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7825                                     ranges_by_label[lab_idx].begin,
7826                                     text_section_label,
7827                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7828               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7829                                     ranges_by_label[lab_idx].end,
7830                                     text_section_label, NULL);
7831 #endif
7832             }
7833           else
7834             {
7835               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7836                                    ranges_by_label[lab_idx].begin,
7837                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7838               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7839                                    ranges_by_label[lab_idx].end,
7840                                    NULL);
7841             }
7842         }
7843       else
7844         {
7845           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7846           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7847           fmt = start_fmt;
7848         }
7849     }
7850 }
7851
7852 /* Data structure containing information about input files.  */
7853 struct file_info
7854 {
7855   const char *path;     /* Complete file name.  */
7856   const char *fname;    /* File name part.  */
7857   int length;           /* Length of entire string.  */
7858   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7859   int dir_idx;          /* Index in directory table.  */
7860 };
7861
7862 /* Data structure containing information about directories with source
7863    files.  */
7864 struct dir_info
7865 {
7866   const char *path;     /* Path including directory name.  */
7867   int length;           /* Path length.  */
7868   int prefix;           /* Index of directory entry which is a prefix.  */
7869   int count;            /* Number of files in this directory.  */
7870   int dir_idx;          /* Index of directory used as base.  */
7871 };
7872
7873 /* Callback function for file_info comparison.  We sort by looking at
7874    the directories in the path.  */
7875
7876 static int
7877 file_info_cmp (const void *p1, const void *p2)
7878 {
7879   const struct file_info *const s1 = (const struct file_info *) p1;
7880   const struct file_info *const s2 = (const struct file_info *) p2;
7881   const unsigned char *cp1;
7882   const unsigned char *cp2;
7883
7884   /* Take care of file names without directories.  We need to make sure that
7885      we return consistent values to qsort since some will get confused if
7886      we return the same value when identical operands are passed in opposite
7887      orders.  So if neither has a directory, return 0 and otherwise return
7888      1 or -1 depending on which one has the directory.  */
7889   if ((s1->path == s1->fname || s2->path == s2->fname))
7890     return (s2->path == s2->fname) - (s1->path == s1->fname);
7891
7892   cp1 = (const unsigned char *) s1->path;
7893   cp2 = (const unsigned char *) s2->path;
7894
7895   while (1)
7896     {
7897       ++cp1;
7898       ++cp2;
7899       /* Reached the end of the first path?  If so, handle like above.  */
7900       if ((cp1 == (const unsigned char *) s1->fname)
7901           || (cp2 == (const unsigned char *) s2->fname))
7902         return ((cp2 == (const unsigned char *) s2->fname)
7903                 - (cp1 == (const unsigned char *) s1->fname));
7904
7905       /* Character of current path component the same?  */
7906       else if (*cp1 != *cp2)
7907         return *cp1 - *cp2;
7908     }
7909 }
7910
7911 struct file_name_acquire_data
7912 {
7913   struct file_info *files;
7914   int used_files;
7915   int max_files;
7916 };
7917
7918 /* Traversal function for the hash table.  */
7919
7920 static int
7921 file_name_acquire (void ** slot, void *data)
7922 {
7923   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
7924   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
7925   struct file_info *fi;
7926   const char *f;
7927
7928   gcc_assert (fnad->max_files >= d->emitted_number);
7929
7930   if (! d->emitted_number)
7931     return 1;
7932
7933   gcc_assert (fnad->max_files != fnad->used_files);
7934
7935   fi = fnad->files + fnad->used_files++;
7936
7937   /* Skip all leading "./".  */
7938   f = d->filename;
7939   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7940     f += 2;
7941
7942   /* Create a new array entry.  */
7943   fi->path = f;
7944   fi->length = strlen (f);
7945   fi->file_idx = d;
7946
7947   /* Search for the file name part.  */
7948   f = strrchr (f, DIR_SEPARATOR);
7949 #if defined (DIR_SEPARATOR_2)
7950   {
7951     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7952
7953     if (g != NULL)
7954       {
7955         if (f == NULL || f < g)
7956           f = g;
7957       }
7958   }
7959 #endif
7960
7961   fi->fname = f == NULL ? fi->path : f + 1;
7962   return 1;
7963 }
7964
7965 /* Output the directory table and the file name table.  We try to minimize
7966    the total amount of memory needed.  A heuristic is used to avoid large
7967    slowdowns with many input files.  */
7968
7969 static void
7970 output_file_names (void)
7971 {
7972   struct file_name_acquire_data fnad;
7973   int numfiles;
7974   struct file_info *files;
7975   struct dir_info *dirs;
7976   int *saved;
7977   int *savehere;
7978   int *backmap;
7979   int ndirs;
7980   int idx_offset;
7981   int i;
7982   int idx;
7983
7984   if (!last_emitted_file)
7985     {
7986       dw2_asm_output_data (1, 0, "End directory table");
7987       dw2_asm_output_data (1, 0, "End file name table");
7988       return;
7989     }
7990
7991   numfiles = last_emitted_file->emitted_number;
7992
7993   /* Allocate the various arrays we need.  */
7994   files = XALLOCAVEC (struct file_info, numfiles);
7995   dirs = XALLOCAVEC (struct dir_info, numfiles);
7996
7997   fnad.files = files;
7998   fnad.used_files = 0;
7999   fnad.max_files = numfiles;
8000   htab_traverse (file_table, file_name_acquire, &fnad);
8001   gcc_assert (fnad.used_files == fnad.max_files);
8002
8003   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8004
8005   /* Find all the different directories used.  */
8006   dirs[0].path = files[0].path;
8007   dirs[0].length = files[0].fname - files[0].path;
8008   dirs[0].prefix = -1;
8009   dirs[0].count = 1;
8010   dirs[0].dir_idx = 0;
8011   files[0].dir_idx = 0;
8012   ndirs = 1;
8013
8014   for (i = 1; i < numfiles; i++)
8015     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8016         && memcmp (dirs[ndirs - 1].path, files[i].path,
8017                    dirs[ndirs - 1].length) == 0)
8018       {
8019         /* Same directory as last entry.  */
8020         files[i].dir_idx = ndirs - 1;
8021         ++dirs[ndirs - 1].count;
8022       }
8023     else
8024       {
8025         int j;
8026
8027         /* This is a new directory.  */
8028         dirs[ndirs].path = files[i].path;
8029         dirs[ndirs].length = files[i].fname - files[i].path;
8030         dirs[ndirs].count = 1;
8031         dirs[ndirs].dir_idx = ndirs;
8032         files[i].dir_idx = ndirs;
8033
8034         /* Search for a prefix.  */
8035         dirs[ndirs].prefix = -1;
8036         for (j = 0; j < ndirs; j++)
8037           if (dirs[j].length < dirs[ndirs].length
8038               && dirs[j].length > 1
8039               && (dirs[ndirs].prefix == -1
8040                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8041               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8042             dirs[ndirs].prefix = j;
8043
8044         ++ndirs;
8045       }
8046
8047   /* Now to the actual work.  We have to find a subset of the directories which
8048      allow expressing the file name using references to the directory table
8049      with the least amount of characters.  We do not do an exhaustive search
8050      where we would have to check out every combination of every single
8051      possible prefix.  Instead we use a heuristic which provides nearly optimal
8052      results in most cases and never is much off.  */
8053   saved = XALLOCAVEC (int, ndirs);
8054   savehere = XALLOCAVEC (int, ndirs);
8055
8056   memset (saved, '\0', ndirs * sizeof (saved[0]));
8057   for (i = 0; i < ndirs; i++)
8058     {
8059       int j;
8060       int total;
8061
8062       /* We can always save some space for the current directory.  But this
8063          does not mean it will be enough to justify adding the directory.  */
8064       savehere[i] = dirs[i].length;
8065       total = (savehere[i] - saved[i]) * dirs[i].count;
8066
8067       for (j = i + 1; j < ndirs; j++)
8068         {
8069           savehere[j] = 0;
8070           if (saved[j] < dirs[i].length)
8071             {
8072               /* Determine whether the dirs[i] path is a prefix of the
8073                  dirs[j] path.  */
8074               int k;
8075
8076               k = dirs[j].prefix;
8077               while (k != -1 && k != (int) i)
8078                 k = dirs[k].prefix;
8079
8080               if (k == (int) i)
8081                 {
8082                   /* Yes it is.  We can possibly save some memory by
8083                      writing the filenames in dirs[j] relative to
8084                      dirs[i].  */
8085                   savehere[j] = dirs[i].length;
8086                   total += (savehere[j] - saved[j]) * dirs[j].count;
8087                 }
8088             }
8089         }
8090
8091       /* Check whether we can save enough to justify adding the dirs[i]
8092          directory.  */
8093       if (total > dirs[i].length + 1)
8094         {
8095           /* It's worthwhile adding.  */
8096           for (j = i; j < ndirs; j++)
8097             if (savehere[j] > 0)
8098               {
8099                 /* Remember how much we saved for this directory so far.  */
8100                 saved[j] = savehere[j];
8101
8102                 /* Remember the prefix directory.  */
8103                 dirs[j].dir_idx = i;
8104               }
8105         }
8106     }
8107
8108   /* Emit the directory name table.  */
8109   idx = 1;
8110   idx_offset = dirs[0].length > 0 ? 1 : 0;
8111   for (i = 1 - idx_offset; i < ndirs; i++)
8112     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8113                             "Directory Entry: 0x%x", i + idx_offset);
8114
8115   dw2_asm_output_data (1, 0, "End directory table");
8116
8117   /* We have to emit them in the order of emitted_number since that's
8118      used in the debug info generation.  To do this efficiently we
8119      generate a back-mapping of the indices first.  */
8120   backmap = XALLOCAVEC (int, numfiles);
8121   for (i = 0; i < numfiles; i++)
8122     backmap[files[i].file_idx->emitted_number - 1] = i;
8123
8124   /* Now write all the file names.  */
8125   for (i = 0; i < numfiles; i++)
8126     {
8127       int file_idx = backmap[i];
8128       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8129
8130       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8131                               "File Entry: 0x%x", (unsigned) i + 1);
8132
8133       /* Include directory index.  */
8134       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8135
8136       /* Modification time.  */
8137       dw2_asm_output_data_uleb128 (0, NULL);
8138
8139       /* File length in bytes.  */
8140       dw2_asm_output_data_uleb128 (0, NULL);
8141     }
8142
8143   dw2_asm_output_data (1, 0, "End file name table");
8144 }
8145
8146
8147 /* Output the source line number correspondence information.  This
8148    information goes into the .debug_line section.  */
8149
8150 static void
8151 output_line_info (void)
8152 {
8153   char l1[20], l2[20], p1[20], p2[20];
8154   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8155   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8156   unsigned opc;
8157   unsigned n_op_args;
8158   unsigned long lt_index;
8159   unsigned long current_line;
8160   long line_offset;
8161   long line_delta;
8162   unsigned long current_file;
8163   unsigned long function;
8164
8165   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8166   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8167   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8168   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8169
8170   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8171     dw2_asm_output_data (4, 0xffffffff,
8172       "Initial length escape value indicating 64-bit DWARF extension");
8173   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8174                         "Length of Source Line Info");
8175   ASM_OUTPUT_LABEL (asm_out_file, l1);
8176
8177   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8178   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8179   ASM_OUTPUT_LABEL (asm_out_file, p1);
8180
8181   /* Define the architecture-dependent minimum instruction length (in
8182    bytes).  In this implementation of DWARF, this field is used for
8183    information purposes only.  Since GCC generates assembly language,
8184    we have no a priori knowledge of how many instruction bytes are
8185    generated for each source line, and therefore can use only the
8186    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8187    commands.  Accordingly, we fix this as `1', which is "correct
8188    enough" for all architectures, and don't let the target override.  */
8189   dw2_asm_output_data (1, 1,
8190                        "Minimum Instruction Length");
8191
8192   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8193                        "Default is_stmt_start flag");
8194   dw2_asm_output_data (1, DWARF_LINE_BASE,
8195                        "Line Base Value (Special Opcodes)");
8196   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8197                        "Line Range Value (Special Opcodes)");
8198   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8199                        "Special Opcode Base");
8200
8201   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8202     {
8203       switch (opc)
8204         {
8205         case DW_LNS_advance_pc:
8206         case DW_LNS_advance_line:
8207         case DW_LNS_set_file:
8208         case DW_LNS_set_column:
8209         case DW_LNS_fixed_advance_pc:
8210           n_op_args = 1;
8211           break;
8212         default:
8213           n_op_args = 0;
8214           break;
8215         }
8216
8217       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8218                            opc, n_op_args);
8219     }
8220
8221   /* Write out the information about the files we use.  */
8222   output_file_names ();
8223   ASM_OUTPUT_LABEL (asm_out_file, p2);
8224
8225   /* We used to set the address register to the first location in the text
8226      section here, but that didn't accomplish anything since we already
8227      have a line note for the opening brace of the first function.  */
8228
8229   /* Generate the line number to PC correspondence table, encoded as
8230      a series of state machine operations.  */
8231   current_file = 1;
8232   current_line = 1;
8233
8234   if (cfun && in_cold_section_p)
8235     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8236   else
8237     strcpy (prev_line_label, text_section_label);
8238   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8239     {
8240       dw_line_info_ref line_info = &line_info_table[lt_index];
8241
8242 #if 0
8243       /* Disable this optimization for now; GDB wants to see two line notes
8244          at the beginning of a function so it can find the end of the
8245          prologue.  */
8246
8247       /* Don't emit anything for redundant notes.  Just updating the
8248          address doesn't accomplish anything, because we already assume
8249          that anything after the last address is this line.  */
8250       if (line_info->dw_line_num == current_line
8251           && line_info->dw_file_num == current_file)
8252         continue;
8253 #endif
8254
8255       /* Emit debug info for the address of the current line.
8256
8257          Unfortunately, we have little choice here currently, and must always
8258          use the most general form.  GCC does not know the address delta
8259          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8260          attributes which will give an upper bound on the address range.  We
8261          could perhaps use length attributes to determine when it is safe to
8262          use DW_LNS_fixed_advance_pc.  */
8263
8264       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8265       if (0)
8266         {
8267           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8268           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8269                                "DW_LNS_fixed_advance_pc");
8270           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8271         }
8272       else
8273         {
8274           /* This can handle any delta.  This takes
8275              4+DWARF2_ADDR_SIZE bytes.  */
8276           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8277           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8278           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8279           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8280         }
8281
8282       strcpy (prev_line_label, line_label);
8283
8284       /* Emit debug info for the source file of the current line, if
8285          different from the previous line.  */
8286       if (line_info->dw_file_num != current_file)
8287         {
8288           current_file = line_info->dw_file_num;
8289           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8290           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8291         }
8292
8293       /* Emit debug info for the current line number, choosing the encoding
8294          that uses the least amount of space.  */
8295       if (line_info->dw_line_num != current_line)
8296         {
8297           line_offset = line_info->dw_line_num - current_line;
8298           line_delta = line_offset - DWARF_LINE_BASE;
8299           current_line = line_info->dw_line_num;
8300           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8301             /* This can handle deltas from -10 to 234, using the current
8302                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8303                takes 1 byte.  */
8304             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8305                                  "line %lu", current_line);
8306           else
8307             {
8308               /* This can handle any delta.  This takes at least 4 bytes,
8309                  depending on the value being encoded.  */
8310               dw2_asm_output_data (1, DW_LNS_advance_line,
8311                                    "advance to line %lu", current_line);
8312               dw2_asm_output_data_sleb128 (line_offset, NULL);
8313               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8314             }
8315         }
8316       else
8317         /* We still need to start a new row, so output a copy insn.  */
8318         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8319     }
8320
8321   /* Emit debug info for the address of the end of the function.  */
8322   if (0)
8323     {
8324       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8325                            "DW_LNS_fixed_advance_pc");
8326       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8327     }
8328   else
8329     {
8330       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8331       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8332       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8333       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8334     }
8335
8336   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8337   dw2_asm_output_data_uleb128 (1, NULL);
8338   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8339
8340   function = 0;
8341   current_file = 1;
8342   current_line = 1;
8343   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8344     {
8345       dw_separate_line_info_ref line_info
8346         = &separate_line_info_table[lt_index];
8347
8348 #if 0
8349       /* Don't emit anything for redundant notes.  */
8350       if (line_info->dw_line_num == current_line
8351           && line_info->dw_file_num == current_file
8352           && line_info->function == function)
8353         goto cont;
8354 #endif
8355
8356       /* Emit debug info for the address of the current line.  If this is
8357          a new function, or the first line of a function, then we need
8358          to handle it differently.  */
8359       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8360                                    lt_index);
8361       if (function != line_info->function)
8362         {
8363           function = line_info->function;
8364
8365           /* Set the address register to the first line in the function.  */
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       else
8372         {
8373           /* ??? See the DW_LNS_advance_pc comment above.  */
8374           if (0)
8375             {
8376               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8377                                    "DW_LNS_fixed_advance_pc");
8378               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8379             }
8380           else
8381             {
8382               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8383               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8384               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8385               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8386             }
8387         }
8388
8389       strcpy (prev_line_label, line_label);
8390
8391       /* Emit debug info for the source file of the current line, if
8392          different from the previous line.  */
8393       if (line_info->dw_file_num != current_file)
8394         {
8395           current_file = line_info->dw_file_num;
8396           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8397           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8398         }
8399
8400       /* Emit debug info for the current line number, choosing the encoding
8401          that uses the least amount of space.  */
8402       if (line_info->dw_line_num != current_line)
8403         {
8404           line_offset = line_info->dw_line_num - current_line;
8405           line_delta = line_offset - DWARF_LINE_BASE;
8406           current_line = line_info->dw_line_num;
8407           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8408             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8409                                  "line %lu", current_line);
8410           else
8411             {
8412               dw2_asm_output_data (1, DW_LNS_advance_line,
8413                                    "advance to line %lu", current_line);
8414               dw2_asm_output_data_sleb128 (line_offset, NULL);
8415               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8416             }
8417         }
8418       else
8419         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8420
8421 #if 0
8422     cont:
8423 #endif
8424
8425       lt_index++;
8426
8427       /* If we're done with a function, end its sequence.  */
8428       if (lt_index == separate_line_info_table_in_use
8429           || separate_line_info_table[lt_index].function != function)
8430         {
8431           current_file = 1;
8432           current_line = 1;
8433
8434           /* Emit debug info for the address of the end of the function.  */
8435           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8436           if (0)
8437             {
8438               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8439                                    "DW_LNS_fixed_advance_pc");
8440               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8441             }
8442           else
8443             {
8444               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8445               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8446               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8447               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8448             }
8449
8450           /* Output the marker for the end of this sequence.  */
8451           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8452           dw2_asm_output_data_uleb128 (1, NULL);
8453           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8454         }
8455     }
8456
8457   /* Output the marker for the end of the line number info.  */
8458   ASM_OUTPUT_LABEL (asm_out_file, l2);
8459 }
8460 \f
8461 /* Given a pointer to a tree node for some base type, return a pointer to
8462    a DIE that describes the given type.
8463
8464    This routine must only be called for GCC type nodes that correspond to
8465    Dwarf base (fundamental) types.  */
8466
8467 static dw_die_ref
8468 base_type_die (tree type)
8469 {
8470   dw_die_ref base_type_result;
8471   enum dwarf_type encoding;
8472
8473   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8474     return 0;
8475
8476   switch (TREE_CODE (type))
8477     {
8478     case INTEGER_TYPE:
8479       if (TYPE_STRING_FLAG (type))
8480         {
8481           if (TYPE_UNSIGNED (type))
8482             encoding = DW_ATE_unsigned_char;
8483           else
8484             encoding = DW_ATE_signed_char;
8485         }
8486       else if (TYPE_UNSIGNED (type))
8487         encoding = DW_ATE_unsigned;
8488       else
8489         encoding = DW_ATE_signed;
8490       break;
8491
8492     case REAL_TYPE:
8493       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8494         encoding = DW_ATE_decimal_float;
8495       else
8496         encoding = DW_ATE_float;
8497       break;
8498
8499     case FIXED_POINT_TYPE:
8500       if (TYPE_UNSIGNED (type))
8501         encoding = DW_ATE_unsigned_fixed;
8502       else
8503         encoding = DW_ATE_signed_fixed;
8504       break;
8505
8506       /* Dwarf2 doesn't know anything about complex ints, so use
8507          a user defined type for it.  */
8508     case COMPLEX_TYPE:
8509       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8510         encoding = DW_ATE_complex_float;
8511       else
8512         encoding = DW_ATE_lo_user;
8513       break;
8514
8515     case BOOLEAN_TYPE:
8516       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8517       encoding = DW_ATE_boolean;
8518       break;
8519
8520     default:
8521       /* No other TREE_CODEs are Dwarf fundamental types.  */
8522       gcc_unreachable ();
8523     }
8524
8525   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8526
8527   /* This probably indicates a bug.  */
8528   if (! TYPE_NAME (type))
8529     add_name_attribute (base_type_result, "__unknown__");
8530
8531   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8532                    int_size_in_bytes (type));
8533   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8534
8535   return base_type_result;
8536 }
8537
8538 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8539    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8540
8541 static inline int
8542 is_base_type (tree type)
8543 {
8544   switch (TREE_CODE (type))
8545     {
8546     case ERROR_MARK:
8547     case VOID_TYPE:
8548     case INTEGER_TYPE:
8549     case REAL_TYPE:
8550     case FIXED_POINT_TYPE:
8551     case COMPLEX_TYPE:
8552     case BOOLEAN_TYPE:
8553       return 1;
8554
8555     case ARRAY_TYPE:
8556     case RECORD_TYPE:
8557     case UNION_TYPE:
8558     case QUAL_UNION_TYPE:
8559     case ENUMERAL_TYPE:
8560     case FUNCTION_TYPE:
8561     case METHOD_TYPE:
8562     case POINTER_TYPE:
8563     case REFERENCE_TYPE:
8564     case OFFSET_TYPE:
8565     case LANG_TYPE:
8566     case VECTOR_TYPE:
8567       return 0;
8568
8569     default:
8570       gcc_unreachable ();
8571     }
8572
8573   return 0;
8574 }
8575
8576 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8577    node, return the size in bits for the type if it is a constant, or else
8578    return the alignment for the type if the type's size is not constant, or
8579    else return BITS_PER_WORD if the type actually turns out to be an
8580    ERROR_MARK node.  */
8581
8582 static inline unsigned HOST_WIDE_INT
8583 simple_type_size_in_bits (const_tree type)
8584 {
8585   if (TREE_CODE (type) == ERROR_MARK)
8586     return BITS_PER_WORD;
8587   else if (TYPE_SIZE (type) == NULL_TREE)
8588     return 0;
8589   else if (host_integerp (TYPE_SIZE (type), 1))
8590     return tree_low_cst (TYPE_SIZE (type), 1);
8591   else
8592     return TYPE_ALIGN (type);
8593 }
8594
8595 /* Return true if the debug information for the given type should be
8596    emitted as a subrange type.  */
8597
8598 static inline bool
8599 is_subrange_type (const_tree type)
8600 {
8601   tree subtype = TREE_TYPE (type);
8602
8603   /* Subrange types are identified by the fact that they are integer
8604      types, and that they have a subtype which is either an integer type
8605      or an enumeral type.  */
8606
8607   if (TREE_CODE (type) != INTEGER_TYPE
8608       || subtype == NULL_TREE)
8609     return false;
8610
8611   if (TREE_CODE (subtype) != INTEGER_TYPE
8612       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8613     return false;
8614
8615   if (TREE_CODE (type) == TREE_CODE (subtype)
8616       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8617       && TYPE_MIN_VALUE (type) != NULL
8618       && TYPE_MIN_VALUE (subtype) != NULL
8619       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8620       && TYPE_MAX_VALUE (type) != NULL
8621       && TYPE_MAX_VALUE (subtype) != NULL
8622       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8623     {
8624       /* The type and its subtype have the same representation.  If in
8625          addition the two types also have the same name, then the given
8626          type is not a subrange type, but rather a plain base type.  */
8627       /* FIXME: brobecker/2004-03-22:
8628          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8629          therefore be sufficient to check the TYPE_SIZE node pointers
8630          rather than checking the actual size.  Unfortunately, we have
8631          found some cases, such as in the Ada "integer" type, where
8632          this is not the case.  Until this problem is solved, we need to
8633          keep checking the actual size.  */
8634       tree type_name = TYPE_NAME (type);
8635       tree subtype_name = TYPE_NAME (subtype);
8636
8637       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8638         type_name = DECL_NAME (type_name);
8639
8640       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8641         subtype_name = DECL_NAME (subtype_name);
8642
8643       if (type_name == subtype_name)
8644         return false;
8645     }
8646
8647   return true;
8648 }
8649
8650 /*  Given a pointer to a tree node for a subrange type, return a pointer
8651     to a DIE that describes the given type.  */
8652
8653 static dw_die_ref
8654 subrange_type_die (tree type, dw_die_ref context_die)
8655 {
8656   dw_die_ref subrange_die;
8657   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8658
8659   if (context_die == NULL)
8660     context_die = comp_unit_die;
8661
8662   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8663
8664   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8665     {
8666       /* The size of the subrange type and its base type do not match,
8667          so we need to generate a size attribute for the subrange type.  */
8668       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8669     }
8670
8671   if (TYPE_MIN_VALUE (type) != NULL)
8672     add_bound_info (subrange_die, DW_AT_lower_bound,
8673                     TYPE_MIN_VALUE (type));
8674   if (TYPE_MAX_VALUE (type) != NULL)
8675     add_bound_info (subrange_die, DW_AT_upper_bound,
8676                     TYPE_MAX_VALUE (type));
8677
8678   return subrange_die;
8679 }
8680
8681 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8682    entry that chains various modifiers in front of the given type.  */
8683
8684 static dw_die_ref
8685 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8686                    dw_die_ref context_die)
8687 {
8688   enum tree_code code = TREE_CODE (type);
8689   dw_die_ref mod_type_die;
8690   dw_die_ref sub_die = NULL;
8691   tree item_type = NULL;
8692   tree qualified_type;
8693   tree name;
8694
8695   if (code == ERROR_MARK)
8696     return NULL;
8697
8698   /* See if we already have the appropriately qualified variant of
8699      this type.  */
8700   qualified_type
8701     = get_qualified_type (type,
8702                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8703                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8704
8705   /* If we do, then we can just use its DIE, if it exists.  */
8706   if (qualified_type)
8707     {
8708       mod_type_die = lookup_type_die (qualified_type);
8709       if (mod_type_die)
8710         return mod_type_die;
8711     }
8712
8713   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8714
8715   /* Handle C typedef types.  */
8716   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8717     {
8718       tree dtype = TREE_TYPE (name);
8719
8720       if (qualified_type == dtype)
8721         {
8722           /* For a named type, use the typedef.  */
8723           gen_type_die (qualified_type, context_die);
8724           return lookup_type_die (qualified_type);
8725         }
8726       else if (is_const_type < TYPE_READONLY (dtype)
8727                || is_volatile_type < TYPE_VOLATILE (dtype)
8728                || (is_const_type <= TYPE_READONLY (dtype)
8729                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8730                    && DECL_ORIGINAL_TYPE (name) != type))
8731         /* cv-unqualified version of named type.  Just use the unnamed
8732            type to which it refers.  */
8733         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8734                                   is_const_type, is_volatile_type,
8735                                   context_die);
8736       /* Else cv-qualified version of named type; fall through.  */
8737     }
8738
8739   if (is_const_type)
8740     {
8741       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8742       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8743     }
8744   else if (is_volatile_type)
8745     {
8746       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8747       sub_die = modified_type_die (type, 0, 0, context_die);
8748     }
8749   else if (code == POINTER_TYPE)
8750     {
8751       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8752       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8753                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8754       item_type = TREE_TYPE (type);
8755     }
8756   else if (code == REFERENCE_TYPE)
8757     {
8758       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8759       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8760                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8761       item_type = TREE_TYPE (type);
8762     }
8763   else if (is_subrange_type (type))
8764     {
8765       mod_type_die = subrange_type_die (type, context_die);
8766       item_type = TREE_TYPE (type);
8767     }
8768   else if (is_base_type (type))
8769     mod_type_die = base_type_die (type);
8770   else
8771     {
8772       gen_type_die (type, context_die);
8773
8774       /* We have to get the type_main_variant here (and pass that to the
8775          `lookup_type_die' routine) because the ..._TYPE node we have
8776          might simply be a *copy* of some original type node (where the
8777          copy was created to help us keep track of typedef names) and
8778          that copy might have a different TYPE_UID from the original
8779          ..._TYPE node.  */
8780       if (TREE_CODE (type) != VECTOR_TYPE)
8781         return lookup_type_die (type_main_variant (type));
8782       else
8783         /* Vectors have the debugging information in the type,
8784            not the main variant.  */
8785         return lookup_type_die (type);
8786     }
8787
8788   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8789      don't output a DW_TAG_typedef, since there isn't one in the
8790      user's program; just attach a DW_AT_name to the type.  */
8791   if (name
8792       && (TREE_CODE (name) != TYPE_DECL
8793           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8794     {
8795       if (TREE_CODE (name) == TYPE_DECL)
8796         /* Could just call add_name_and_src_coords_attributes here,
8797            but since this is a builtin type it doesn't have any
8798            useful source coordinates anyway.  */
8799         name = DECL_NAME (name);
8800       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8801     }
8802
8803   if (qualified_type)
8804     equate_type_number_to_die (qualified_type, mod_type_die);
8805
8806   if (item_type)
8807     /* We must do this after the equate_type_number_to_die call, in case
8808        this is a recursive type.  This ensures that the modified_type_die
8809        recursion will terminate even if the type is recursive.  Recursive
8810        types are possible in Ada.  */
8811     sub_die = modified_type_die (item_type,
8812                                  TYPE_READONLY (item_type),
8813                                  TYPE_VOLATILE (item_type),
8814                                  context_die);
8815
8816   if (sub_die != NULL)
8817     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8818
8819   return mod_type_die;
8820 }
8821
8822 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8823    an enumerated type.  */
8824
8825 static inline int
8826 type_is_enum (const_tree type)
8827 {
8828   return TREE_CODE (type) == ENUMERAL_TYPE;
8829 }
8830
8831 /* Return the DBX register number described by a given RTL node.  */
8832
8833 static unsigned int
8834 dbx_reg_number (const_rtx rtl)
8835 {
8836   unsigned regno = REGNO (rtl);
8837
8838   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8839
8840 #ifdef LEAF_REG_REMAP
8841   if (current_function_uses_only_leaf_regs)
8842     {
8843       int leaf_reg = LEAF_REG_REMAP (regno);
8844       if (leaf_reg != -1)
8845         regno = (unsigned) leaf_reg;
8846     }
8847 #endif
8848
8849   return DBX_REGISTER_NUMBER (regno);
8850 }
8851
8852 /* Optionally add a DW_OP_piece term to a location description expression.
8853    DW_OP_piece is only added if the location description expression already
8854    doesn't end with DW_OP_piece.  */
8855
8856 static void
8857 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8858 {
8859   dw_loc_descr_ref loc;
8860
8861   if (*list_head != NULL)
8862     {
8863       /* Find the end of the chain.  */
8864       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8865         ;
8866
8867       if (loc->dw_loc_opc != DW_OP_piece)
8868         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8869     }
8870 }
8871
8872 /* Return a location descriptor that designates a machine register or
8873    zero if there is none.  */
8874
8875 static dw_loc_descr_ref
8876 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8877 {
8878   rtx regs;
8879
8880   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8881     return 0;
8882
8883   regs = targetm.dwarf_register_span (rtl);
8884
8885   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8886     return multiple_reg_loc_descriptor (rtl, regs, initialized);
8887   else
8888     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8889 }
8890
8891 /* Return a location descriptor that designates a machine register for
8892    a given hard register number.  */
8893
8894 static dw_loc_descr_ref
8895 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8896 {
8897   dw_loc_descr_ref reg_loc_descr;
8898   if (regno <= 31)
8899     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8900   else
8901     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8902
8903   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8904     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8905
8906   return reg_loc_descr;
8907 }
8908
8909 /* Given an RTL of a register, return a location descriptor that
8910    designates a value that spans more than one register.  */
8911
8912 static dw_loc_descr_ref
8913 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
8914                              enum var_init_status initialized)
8915 {
8916   int nregs, size, i;
8917   unsigned reg;
8918   dw_loc_descr_ref loc_result = NULL;
8919
8920   reg = REGNO (rtl);
8921 #ifdef LEAF_REG_REMAP
8922   if (current_function_uses_only_leaf_regs)
8923     {
8924       int leaf_reg = LEAF_REG_REMAP (reg);
8925       if (leaf_reg != -1)
8926         reg = (unsigned) leaf_reg;
8927     }
8928 #endif
8929   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8930   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8931
8932   /* Simple, contiguous registers.  */
8933   if (regs == NULL_RTX)
8934     {
8935       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8936
8937       loc_result = NULL;
8938       while (nregs--)
8939         {
8940           dw_loc_descr_ref t;
8941
8942           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8943                                       VAR_INIT_STATUS_INITIALIZED);
8944           add_loc_descr (&loc_result, t);
8945           add_loc_descr_op_piece (&loc_result, size);
8946           ++reg;
8947         }
8948       return loc_result;
8949     }
8950
8951   /* Now onto stupid register sets in non contiguous locations.  */
8952
8953   gcc_assert (GET_CODE (regs) == PARALLEL);
8954
8955   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8956   loc_result = NULL;
8957
8958   for (i = 0; i < XVECLEN (regs, 0); ++i)
8959     {
8960       dw_loc_descr_ref t;
8961
8962       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8963                                   VAR_INIT_STATUS_INITIALIZED);
8964       add_loc_descr (&loc_result, t);
8965       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8966       add_loc_descr_op_piece (&loc_result, size);
8967     }
8968
8969   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8970     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8971   return loc_result;
8972 }
8973
8974 /* Return a location descriptor that designates a constant.  */
8975
8976 static dw_loc_descr_ref
8977 int_loc_descriptor (HOST_WIDE_INT i)
8978 {
8979   enum dwarf_location_atom op;
8980
8981   /* Pick the smallest representation of a constant, rather than just
8982      defaulting to the LEB encoding.  */
8983   if (i >= 0)
8984     {
8985       if (i <= 31)
8986         op = DW_OP_lit0 + i;
8987       else if (i <= 0xff)
8988         op = DW_OP_const1u;
8989       else if (i <= 0xffff)
8990         op = DW_OP_const2u;
8991       else if (HOST_BITS_PER_WIDE_INT == 32
8992                || i <= 0xffffffff)
8993         op = DW_OP_const4u;
8994       else
8995         op = DW_OP_constu;
8996     }
8997   else
8998     {
8999       if (i >= -0x80)
9000         op = DW_OP_const1s;
9001       else if (i >= -0x8000)
9002         op = DW_OP_const2s;
9003       else if (HOST_BITS_PER_WIDE_INT == 32
9004                || i >= -0x80000000)
9005         op = DW_OP_const4s;
9006       else
9007         op = DW_OP_consts;
9008     }
9009
9010   return new_loc_descr (op, i, 0);
9011 }
9012
9013 /* Return a location descriptor that designates a base+offset location.  */
9014
9015 static dw_loc_descr_ref
9016 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9017                  enum var_init_status initialized)
9018 {
9019   unsigned int regno;
9020   dw_loc_descr_ref result;
9021
9022   /* We only use "frame base" when we're sure we're talking about the
9023      post-prologue local stack frame.  We do this by *not* running
9024      register elimination until this point, and recognizing the special
9025      argument pointer and soft frame pointer rtx's.  */
9026   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9027     {
9028       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9029
9030       if (elim != reg)
9031         {
9032           if (GET_CODE (elim) == PLUS)
9033             {
9034               offset += INTVAL (XEXP (elim, 1));
9035               elim = XEXP (elim, 0);
9036             }
9037           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
9038                       : stack_pointer_rtx));
9039           offset += frame_pointer_fb_offset;
9040
9041           return new_loc_descr (DW_OP_fbreg, offset, 0);
9042         }
9043     }
9044
9045   regno = dbx_reg_number (reg);
9046   if (regno <= 31)
9047     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9048   else
9049     result = new_loc_descr (DW_OP_bregx, regno, offset);
9050
9051   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9052     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9053
9054   return result;
9055 }
9056
9057 /* Return true if this RTL expression describes a base+offset calculation.  */
9058
9059 static inline int
9060 is_based_loc (const_rtx rtl)
9061 {
9062   return (GET_CODE (rtl) == PLUS
9063           && ((REG_P (XEXP (rtl, 0))
9064                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9065                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9066 }
9067
9068 /* Return a descriptor that describes the concatenation of N locations
9069    used to form the address of a memory location.  */
9070
9071 static dw_loc_descr_ref
9072 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9073                             enum var_init_status initialized)
9074 {
9075   unsigned int i;
9076   dw_loc_descr_ref cc_loc_result = NULL;
9077   unsigned int n = XVECLEN (concatn, 0);
9078
9079   for (i = 0; i < n; ++i)
9080     {
9081       dw_loc_descr_ref ref;
9082       rtx x = XVECEXP (concatn, 0, i);
9083
9084       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9085       if (ref == NULL)
9086         return NULL;
9087
9088       add_loc_descr (&cc_loc_result, ref);
9089       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9090     }
9091
9092   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9093     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9094
9095   return cc_loc_result;
9096 }
9097
9098 /* The following routine converts the RTL for a variable or parameter
9099    (resident in memory) into an equivalent Dwarf representation of a
9100    mechanism for getting the address of that same variable onto the top of a
9101    hypothetical "address evaluation" stack.
9102
9103    When creating memory location descriptors, we are effectively transforming
9104    the RTL for a memory-resident object into its Dwarf postfix expression
9105    equivalent.  This routine recursively descends an RTL tree, turning
9106    it into Dwarf postfix code as it goes.
9107
9108    MODE is the mode of the memory reference, needed to handle some
9109    autoincrement addressing modes.
9110
9111    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9112    location list for RTL.
9113
9114    Return 0 if we can't represent the location.  */
9115
9116 static dw_loc_descr_ref
9117 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9118                     enum var_init_status initialized)
9119 {
9120   dw_loc_descr_ref mem_loc_result = NULL;
9121   enum dwarf_location_atom op;
9122
9123   /* Note that for a dynamically sized array, the location we will generate a
9124      description of here will be the lowest numbered location which is
9125      actually within the array.  That's *not* necessarily the same as the
9126      zeroth element of the array.  */
9127
9128   rtl = targetm.delegitimize_address (rtl);
9129
9130   switch (GET_CODE (rtl))
9131     {
9132     case POST_INC:
9133     case POST_DEC:
9134     case POST_MODIFY:
9135       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9136          just fall into the SUBREG code.  */
9137
9138       /* ... fall through ...  */
9139
9140     case SUBREG:
9141       /* The case of a subreg may arise when we have a local (register)
9142          variable or a formal (register) parameter which doesn't quite fill
9143          up an entire register.  For now, just assume that it is
9144          legitimate to make the Dwarf info refer to the whole register which
9145          contains the given subreg.  */
9146       rtl = XEXP (rtl, 0);
9147
9148       /* ... fall through ...  */
9149
9150     case REG:
9151       /* Whenever a register number forms a part of the description of the
9152          method for calculating the (dynamic) address of a memory resident
9153          object, DWARF rules require the register number be referred to as
9154          a "base register".  This distinction is not based in any way upon
9155          what category of register the hardware believes the given register
9156          belongs to.  This is strictly DWARF terminology we're dealing with
9157          here. Note that in cases where the location of a memory-resident
9158          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9159          OP_CONST (0)) the actual DWARF location descriptor that we generate
9160          may just be OP_BASEREG (basereg).  This may look deceptively like
9161          the object in question was allocated to a register (rather than in
9162          memory) so DWARF consumers need to be aware of the subtle
9163          distinction between OP_REG and OP_BASEREG.  */
9164       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9165         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9166       break;
9167
9168     case MEM:
9169       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9170                                            VAR_INIT_STATUS_INITIALIZED);
9171       if (mem_loc_result != 0)
9172         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9173       break;
9174
9175     case LO_SUM:
9176          rtl = XEXP (rtl, 1);
9177
9178       /* ... fall through ...  */
9179
9180     case LABEL_REF:
9181       /* Some ports can transform a symbol ref into a label ref, because
9182          the symbol ref is too far away and has to be dumped into a constant
9183          pool.  */
9184     case CONST:
9185     case SYMBOL_REF:
9186       /* Alternatively, the symbol in the constant pool might be referenced
9187          by a different symbol.  */
9188       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9189         {
9190           bool marked;
9191           rtx tmp = get_pool_constant_mark (rtl, &marked);
9192
9193           if (GET_CODE (tmp) == SYMBOL_REF)
9194             {
9195               rtl = tmp;
9196               if (CONSTANT_POOL_ADDRESS_P (tmp))
9197                 get_pool_constant_mark (tmp, &marked);
9198               else
9199                 marked = true;
9200             }
9201
9202           /* If all references to this pool constant were optimized away,
9203              it was not output and thus we can't represent it.
9204              FIXME: might try to use DW_OP_const_value here, though
9205              DW_OP_piece complicates it.  */
9206           if (!marked)
9207             return 0;
9208         }
9209
9210       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9211       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9212       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9213       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9214       break;
9215
9216     case PRE_MODIFY:
9217       /* Extract the PLUS expression nested inside and fall into
9218          PLUS code below.  */
9219       rtl = XEXP (rtl, 1);
9220       goto plus;
9221
9222     case PRE_INC:
9223     case PRE_DEC:
9224       /* Turn these into a PLUS expression and fall into the PLUS code
9225          below.  */
9226       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9227                           GEN_INT (GET_CODE (rtl) == PRE_INC
9228                                    ? GET_MODE_UNIT_SIZE (mode)
9229                                    : -GET_MODE_UNIT_SIZE (mode)));
9230
9231       /* ... fall through ...  */
9232
9233     case PLUS:
9234     plus:
9235       if (is_based_loc (rtl))
9236         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9237                                           INTVAL (XEXP (rtl, 1)),
9238                                           VAR_INIT_STATUS_INITIALIZED);
9239       else
9240         {
9241           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9242                                                VAR_INIT_STATUS_INITIALIZED);
9243           if (mem_loc_result == 0)
9244             break;
9245
9246           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9247               && INTVAL (XEXP (rtl, 1)) >= 0)
9248             add_loc_descr (&mem_loc_result,
9249                            new_loc_descr (DW_OP_plus_uconst,
9250                                           INTVAL (XEXP (rtl, 1)), 0));
9251           else
9252             {
9253               add_loc_descr (&mem_loc_result,
9254                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9255                                                  VAR_INIT_STATUS_INITIALIZED));
9256               add_loc_descr (&mem_loc_result,
9257                              new_loc_descr (DW_OP_plus, 0, 0));
9258             }
9259         }
9260       break;
9261
9262     /* If a pseudo-reg is optimized away, it is possible for it to
9263        be replaced with a MEM containing a multiply or shift.  */
9264     case MULT:
9265       op = DW_OP_mul;
9266       goto do_binop;
9267
9268     case ASHIFT:
9269       op = DW_OP_shl;
9270       goto do_binop;
9271
9272     case ASHIFTRT:
9273       op = DW_OP_shra;
9274       goto do_binop;
9275
9276     case LSHIFTRT:
9277       op = DW_OP_shr;
9278       goto do_binop;
9279
9280     do_binop:
9281       {
9282         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9283                                                    VAR_INIT_STATUS_INITIALIZED);
9284         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9285                                                    VAR_INIT_STATUS_INITIALIZED);
9286
9287         if (op0 == 0 || op1 == 0)
9288           break;
9289
9290         mem_loc_result = op0;
9291         add_loc_descr (&mem_loc_result, op1);
9292         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9293         break;
9294       }
9295
9296     case CONST_INT:
9297       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9298       break;
9299
9300     case CONCATN:
9301       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9302                                                    VAR_INIT_STATUS_INITIALIZED);
9303       break;
9304
9305     default:
9306       gcc_unreachable ();
9307     }
9308
9309   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9310     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9311
9312   return mem_loc_result;
9313 }
9314
9315 /* Return a descriptor that describes the concatenation of two locations.
9316    This is typically a complex variable.  */
9317
9318 static dw_loc_descr_ref
9319 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9320 {
9321   dw_loc_descr_ref cc_loc_result = NULL;
9322   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9323   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9324
9325   if (x0_ref == 0 || x1_ref == 0)
9326     return 0;
9327
9328   cc_loc_result = x0_ref;
9329   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9330
9331   add_loc_descr (&cc_loc_result, x1_ref);
9332   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9333
9334   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9335     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9336
9337   return cc_loc_result;
9338 }
9339
9340 /* Return a descriptor that describes the concatenation of N
9341    locations.  */
9342
9343 static dw_loc_descr_ref
9344 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9345 {
9346   unsigned int i;
9347   dw_loc_descr_ref cc_loc_result = NULL;
9348   unsigned int n = XVECLEN (concatn, 0);
9349
9350   for (i = 0; i < n; ++i)
9351     {
9352       dw_loc_descr_ref ref;
9353       rtx x = XVECEXP (concatn, 0, i);
9354
9355       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9356       if (ref == NULL)
9357         return NULL;
9358
9359       add_loc_descr (&cc_loc_result, ref);
9360       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9361     }
9362
9363   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9364     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9365
9366   return cc_loc_result;
9367 }
9368
9369 /* Output a proper Dwarf location descriptor for a variable or parameter
9370    which is either allocated in a register or in a memory location.  For a
9371    register, we just generate an OP_REG and the register number.  For a
9372    memory location we provide a Dwarf postfix expression describing how to
9373    generate the (dynamic) address of the object onto the address stack.
9374
9375    If we don't know how to describe it, return 0.  */
9376
9377 static dw_loc_descr_ref
9378 loc_descriptor (rtx rtl, enum var_init_status initialized)
9379 {
9380   dw_loc_descr_ref loc_result = NULL;
9381
9382   switch (GET_CODE (rtl))
9383     {
9384     case SUBREG:
9385       /* The case of a subreg may arise when we have a local (register)
9386          variable or a formal (register) parameter which doesn't quite fill
9387          up an entire register.  For now, just assume that it is
9388          legitimate to make the Dwarf info refer to the whole register which
9389          contains the given subreg.  */
9390       rtl = SUBREG_REG (rtl);
9391
9392       /* ... fall through ...  */
9393
9394     case REG:
9395       loc_result = reg_loc_descriptor (rtl, initialized);
9396       break;
9397
9398     case MEM:
9399       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9400                                        initialized);
9401       break;
9402
9403     case CONCAT:
9404       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9405                                           initialized);
9406       break;
9407
9408     case CONCATN:
9409       loc_result = concatn_loc_descriptor (rtl, initialized);
9410       break;
9411
9412     case VAR_LOCATION:
9413       /* Single part.  */
9414       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9415         {
9416           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9417           break;
9418         }
9419
9420       rtl = XEXP (rtl, 1);
9421       /* FALLTHRU */
9422
9423     case PARALLEL:
9424       {
9425         rtvec par_elems = XVEC (rtl, 0);
9426         int num_elem = GET_NUM_ELEM (par_elems);
9427         enum machine_mode mode;
9428         int i;
9429
9430         /* Create the first one, so we have something to add to.  */
9431         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9432                                      initialized);
9433         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9434         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9435         for (i = 1; i < num_elem; i++)
9436           {
9437             dw_loc_descr_ref temp;
9438
9439             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9440                                    initialized);
9441             add_loc_descr (&loc_result, temp);
9442             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9443             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9444           }
9445       }
9446       break;
9447
9448     default:
9449       gcc_unreachable ();
9450     }
9451
9452   return loc_result;
9453 }
9454
9455 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9456    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9457    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9458    top-level invocation, and we require the address of LOC; is 0 if we require
9459    the value of LOC.  */
9460
9461 static dw_loc_descr_ref
9462 loc_descriptor_from_tree_1 (tree loc, int want_address)
9463 {
9464   dw_loc_descr_ref ret, ret1;
9465   int have_address = 0;
9466   enum dwarf_location_atom op;
9467
9468   /* ??? Most of the time we do not take proper care for sign/zero
9469      extending the values properly.  Hopefully this won't be a real
9470      problem...  */
9471
9472   switch (TREE_CODE (loc))
9473     {
9474     case ERROR_MARK:
9475       return 0;
9476
9477     case PLACEHOLDER_EXPR:
9478       /* This case involves extracting fields from an object to determine the
9479          position of other fields.  We don't try to encode this here.  The
9480          only user of this is Ada, which encodes the needed information using
9481          the names of types.  */
9482       return 0;
9483
9484     case CALL_EXPR:
9485       return 0;
9486
9487     case PREINCREMENT_EXPR:
9488     case PREDECREMENT_EXPR:
9489     case POSTINCREMENT_EXPR:
9490     case POSTDECREMENT_EXPR:
9491       /* There are no opcodes for these operations.  */
9492       return 0;
9493
9494     case ADDR_EXPR:
9495       /* If we already want an address, there's nothing we can do.  */
9496       if (want_address)
9497         return 0;
9498
9499       /* Otherwise, process the argument and look for the address.  */
9500       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9501
9502     case VAR_DECL:
9503       if (DECL_THREAD_LOCAL_P (loc))
9504         {
9505           rtx rtl;
9506           unsigned first_op;
9507           unsigned second_op;
9508
9509           if (targetm.have_tls)
9510             {
9511               /* If this is not defined, we have no way to emit the
9512                  data.  */
9513               if (!targetm.asm_out.output_dwarf_dtprel)
9514                 return 0;
9515
9516                /* The way DW_OP_GNU_push_tls_address is specified, we
9517                   can only look up addresses of objects in the current
9518                   module.  */
9519               if (DECL_EXTERNAL (loc))
9520                 return 0;
9521               first_op = INTERNAL_DW_OP_tls_addr;
9522               second_op = DW_OP_GNU_push_tls_address;
9523             }
9524           else
9525             {
9526               if (!targetm.emutls.debug_form_tls_address)
9527                 return 0;
9528               loc = emutls_decl (loc);
9529               first_op = DW_OP_addr;
9530               second_op = DW_OP_form_tls_address;
9531             }
9532
9533           rtl = rtl_for_decl_location (loc);
9534           if (rtl == NULL_RTX)
9535             return 0;
9536
9537           if (!MEM_P (rtl))
9538             return 0;
9539           rtl = XEXP (rtl, 0);
9540           if (! CONSTANT_P (rtl))
9541             return 0;
9542
9543           ret = new_loc_descr (first_op, 0, 0);
9544           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9545           ret->dw_loc_oprnd1.v.val_addr = rtl;
9546
9547           ret1 = new_loc_descr (second_op, 0, 0);
9548           add_loc_descr (&ret, ret1);
9549
9550           have_address = 1;
9551           break;
9552         }
9553       /* FALLTHRU */
9554
9555     case PARM_DECL:
9556       if (DECL_HAS_VALUE_EXPR_P (loc))
9557         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9558                                            want_address);
9559       /* FALLTHRU */
9560
9561     case RESULT_DECL:
9562     case FUNCTION_DECL:
9563       {
9564         rtx rtl = rtl_for_decl_location (loc);
9565
9566         if (rtl == NULL_RTX)
9567           return 0;
9568         else if (GET_CODE (rtl) == CONST_INT)
9569           {
9570             HOST_WIDE_INT val = INTVAL (rtl);
9571             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9572               val &= GET_MODE_MASK (DECL_MODE (loc));
9573             ret = int_loc_descriptor (val);
9574           }
9575         else if (GET_CODE (rtl) == CONST_STRING)
9576           return 0;
9577         else if (CONSTANT_P (rtl))
9578           {
9579             ret = new_loc_descr (DW_OP_addr, 0, 0);
9580             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9581             ret->dw_loc_oprnd1.v.val_addr = rtl;
9582           }
9583         else
9584           {
9585             enum machine_mode mode;
9586
9587             /* Certain constructs can only be represented at top-level.  */
9588             if (want_address == 2)
9589               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9590
9591             mode = GET_MODE (rtl);
9592             if (MEM_P (rtl))
9593               {
9594                 rtl = XEXP (rtl, 0);
9595                 have_address = 1;
9596               }
9597             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9598           }
9599       }
9600       break;
9601
9602     case INDIRECT_REF:
9603       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9604       have_address = 1;
9605       break;
9606
9607     case COMPOUND_EXPR:
9608       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9609
9610     CASE_CONVERT:
9611     case VIEW_CONVERT_EXPR:
9612     case SAVE_EXPR:
9613     case GIMPLE_MODIFY_STMT:
9614       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9615                                          want_address);
9616
9617     case COMPONENT_REF:
9618     case BIT_FIELD_REF:
9619     case ARRAY_REF:
9620     case ARRAY_RANGE_REF:
9621       {
9622         tree obj, offset;
9623         HOST_WIDE_INT bitsize, bitpos, bytepos;
9624         enum machine_mode mode;
9625         int volatilep;
9626         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9627
9628         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9629                                    &unsignedp, &volatilep, false);
9630
9631         if (obj == loc)
9632           return 0;
9633
9634         ret = loc_descriptor_from_tree_1 (obj, 1);
9635         if (ret == 0
9636             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9637           return 0;
9638
9639         if (offset != NULL_TREE)
9640           {
9641             /* Variable offset.  */
9642             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9643             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9644           }
9645
9646         bytepos = bitpos / BITS_PER_UNIT;
9647         if (bytepos > 0)
9648           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9649         else if (bytepos < 0)
9650           {
9651             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9652             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9653           }
9654
9655         have_address = 1;
9656         break;
9657       }
9658
9659     case INTEGER_CST:
9660       if (host_integerp (loc, 0))
9661         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9662       else
9663         return 0;
9664       break;
9665
9666     case CONSTRUCTOR:
9667       {
9668         /* Get an RTL for this, if something has been emitted.  */
9669         rtx rtl = lookup_constant_def (loc);
9670         enum machine_mode mode;
9671
9672         if (!rtl || !MEM_P (rtl))
9673           return 0;
9674         mode = GET_MODE (rtl);
9675         rtl = XEXP (rtl, 0);
9676         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9677         have_address = 1;
9678         break;
9679       }
9680
9681     case TRUTH_AND_EXPR:
9682     case TRUTH_ANDIF_EXPR:
9683     case BIT_AND_EXPR:
9684       op = DW_OP_and;
9685       goto do_binop;
9686
9687     case TRUTH_XOR_EXPR:
9688     case BIT_XOR_EXPR:
9689       op = DW_OP_xor;
9690       goto do_binop;
9691
9692     case TRUTH_OR_EXPR:
9693     case TRUTH_ORIF_EXPR:
9694     case BIT_IOR_EXPR:
9695       op = DW_OP_or;
9696       goto do_binop;
9697
9698     case FLOOR_DIV_EXPR:
9699     case CEIL_DIV_EXPR:
9700     case ROUND_DIV_EXPR:
9701     case TRUNC_DIV_EXPR:
9702       op = DW_OP_div;
9703       goto do_binop;
9704
9705     case MINUS_EXPR:
9706       op = DW_OP_minus;
9707       goto do_binop;
9708
9709     case FLOOR_MOD_EXPR:
9710     case CEIL_MOD_EXPR:
9711     case ROUND_MOD_EXPR:
9712     case TRUNC_MOD_EXPR:
9713       op = DW_OP_mod;
9714       goto do_binop;
9715
9716     case MULT_EXPR:
9717       op = DW_OP_mul;
9718       goto do_binop;
9719
9720     case LSHIFT_EXPR:
9721       op = DW_OP_shl;
9722       goto do_binop;
9723
9724     case RSHIFT_EXPR:
9725       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9726       goto do_binop;
9727
9728     case POINTER_PLUS_EXPR:
9729     case PLUS_EXPR:
9730       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9731           && host_integerp (TREE_OPERAND (loc, 1), 0))
9732         {
9733           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9734           if (ret == 0)
9735             return 0;
9736
9737           add_loc_descr (&ret,
9738                          new_loc_descr (DW_OP_plus_uconst,
9739                                         tree_low_cst (TREE_OPERAND (loc, 1),
9740                                                       0),
9741                                         0));
9742           break;
9743         }
9744
9745       op = DW_OP_plus;
9746       goto do_binop;
9747
9748     case LE_EXPR:
9749       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9750         return 0;
9751
9752       op = DW_OP_le;
9753       goto do_binop;
9754
9755     case GE_EXPR:
9756       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9757         return 0;
9758
9759       op = DW_OP_ge;
9760       goto do_binop;
9761
9762     case LT_EXPR:
9763       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9764         return 0;
9765
9766       op = DW_OP_lt;
9767       goto do_binop;
9768
9769     case GT_EXPR:
9770       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9771         return 0;
9772
9773       op = DW_OP_gt;
9774       goto do_binop;
9775
9776     case EQ_EXPR:
9777       op = DW_OP_eq;
9778       goto do_binop;
9779
9780     case NE_EXPR:
9781       op = DW_OP_ne;
9782       goto do_binop;
9783
9784     do_binop:
9785       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9786       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9787       if (ret == 0 || ret1 == 0)
9788         return 0;
9789
9790       add_loc_descr (&ret, ret1);
9791       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9792       break;
9793
9794     case TRUTH_NOT_EXPR:
9795     case BIT_NOT_EXPR:
9796       op = DW_OP_not;
9797       goto do_unop;
9798
9799     case ABS_EXPR:
9800       op = DW_OP_abs;
9801       goto do_unop;
9802
9803     case NEGATE_EXPR:
9804       op = DW_OP_neg;
9805       goto do_unop;
9806
9807     do_unop:
9808       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9809       if (ret == 0)
9810         return 0;
9811
9812       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9813       break;
9814
9815     case MIN_EXPR:
9816     case MAX_EXPR:
9817       {
9818         const enum tree_code code =
9819           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9820
9821         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9822                       build2 (code, integer_type_node,
9823                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9824                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9825       }
9826
9827       /* ... fall through ...  */
9828
9829     case COND_EXPR:
9830       {
9831         dw_loc_descr_ref lhs
9832           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9833         dw_loc_descr_ref rhs
9834           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9835         dw_loc_descr_ref bra_node, jump_node, tmp;
9836
9837         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9838         if (ret == 0 || lhs == 0 || rhs == 0)
9839           return 0;
9840
9841         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9842         add_loc_descr (&ret, bra_node);
9843
9844         add_loc_descr (&ret, rhs);
9845         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9846         add_loc_descr (&ret, jump_node);
9847
9848         add_loc_descr (&ret, lhs);
9849         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9850         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9851
9852         /* ??? Need a node to point the skip at.  Use a nop.  */
9853         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9854         add_loc_descr (&ret, tmp);
9855         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9856         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9857       }
9858       break;
9859
9860     case FIX_TRUNC_EXPR:
9861       return 0;
9862
9863     default:
9864       /* Leave front-end specific codes as simply unknown.  This comes
9865          up, for instance, with the C STMT_EXPR.  */
9866       if ((unsigned int) TREE_CODE (loc)
9867           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9868         return 0;
9869
9870 #ifdef ENABLE_CHECKING
9871       /* Otherwise this is a generic code; we should just lists all of
9872          these explicitly.  We forgot one.  */
9873       gcc_unreachable ();
9874 #else
9875       /* In a release build, we want to degrade gracefully: better to
9876          generate incomplete debugging information than to crash.  */
9877       return NULL;
9878 #endif
9879     }
9880
9881   /* Show if we can't fill the request for an address.  */
9882   if (want_address && !have_address)
9883     return 0;
9884
9885   /* If we've got an address and don't want one, dereference.  */
9886   if (!want_address && have_address && ret)
9887     {
9888       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9889
9890       if (size > DWARF2_ADDR_SIZE || size == -1)
9891         return 0;
9892       else if (size == DWARF2_ADDR_SIZE)
9893         op = DW_OP_deref;
9894       else
9895         op = DW_OP_deref_size;
9896
9897       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9898     }
9899
9900   return ret;
9901 }
9902
9903 static inline dw_loc_descr_ref
9904 loc_descriptor_from_tree (tree loc)
9905 {
9906   return loc_descriptor_from_tree_1 (loc, 2);
9907 }
9908
9909 /* Given a value, round it up to the lowest multiple of `boundary'
9910    which is not less than the value itself.  */
9911
9912 static inline HOST_WIDE_INT
9913 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9914 {
9915   return (((value + boundary - 1) / boundary) * boundary);
9916 }
9917
9918 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9919    pointer to the declared type for the relevant field variable, or return
9920    `integer_type_node' if the given node turns out to be an
9921    ERROR_MARK node.  */
9922
9923 static inline tree
9924 field_type (const_tree decl)
9925 {
9926   tree type;
9927
9928   if (TREE_CODE (decl) == ERROR_MARK)
9929     return integer_type_node;
9930
9931   type = DECL_BIT_FIELD_TYPE (decl);
9932   if (type == NULL_TREE)
9933     type = TREE_TYPE (decl);
9934
9935   return type;
9936 }
9937
9938 /* Given a pointer to a tree node, return the alignment in bits for
9939    it, or else return BITS_PER_WORD if the node actually turns out to
9940    be an ERROR_MARK node.  */
9941
9942 static inline unsigned
9943 simple_type_align_in_bits (const_tree type)
9944 {
9945   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9946 }
9947
9948 static inline unsigned
9949 simple_decl_align_in_bits (const_tree decl)
9950 {
9951   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9952 }
9953
9954 /* Return the result of rounding T up to ALIGN.  */
9955
9956 static inline HOST_WIDE_INT
9957 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9958 {
9959   /* We must be careful if T is negative because HOST_WIDE_INT can be
9960      either "above" or "below" unsigned int as per the C promotion
9961      rules, depending on the host, thus making the signedness of the
9962      direct multiplication and division unpredictable.  */
9963   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9964
9965   u += align - 1;
9966   u /= align;
9967   u *= align;
9968
9969   return (HOST_WIDE_INT) u;
9970 }
9971
9972 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9973    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9974    or return 0 if we are unable to determine what that offset is, either
9975    because the argument turns out to be a pointer to an ERROR_MARK node, or
9976    because the offset is actually variable.  (We can't handle the latter case
9977    just yet).  */
9978
9979 static HOST_WIDE_INT
9980 field_byte_offset (const_tree decl)
9981 {
9982   HOST_WIDE_INT object_offset_in_bits;
9983   HOST_WIDE_INT bitpos_int;
9984
9985   if (TREE_CODE (decl) == ERROR_MARK)
9986     return 0;
9987
9988   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9989
9990   /* We cannot yet cope with fields whose positions are variable, so
9991      for now, when we see such things, we simply return 0.  Someday, we may
9992      be able to handle such cases, but it will be damn difficult.  */
9993   if (! host_integerp (bit_position (decl), 0))
9994     return 0;
9995
9996   bitpos_int = int_bit_position (decl);
9997
9998 #ifdef PCC_BITFIELD_TYPE_MATTERS
9999   if (PCC_BITFIELD_TYPE_MATTERS)
10000     {
10001       tree type;
10002       tree field_size_tree;
10003       HOST_WIDE_INT deepest_bitpos;
10004       unsigned HOST_WIDE_INT field_size_in_bits;
10005       unsigned int type_align_in_bits;
10006       unsigned int decl_align_in_bits;
10007       unsigned HOST_WIDE_INT type_size_in_bits;
10008
10009       type = field_type (decl);
10010       field_size_tree = DECL_SIZE (decl);
10011
10012       /* The size could be unspecified if there was an error, or for
10013          a flexible array member.  */
10014       if (! field_size_tree)
10015         field_size_tree = bitsize_zero_node;
10016
10017       /* If we don't know the size of the field, pretend it's a full word.  */
10018       if (host_integerp (field_size_tree, 1))
10019         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10020       else
10021         field_size_in_bits = BITS_PER_WORD;
10022
10023       type_size_in_bits = simple_type_size_in_bits (type);
10024       type_align_in_bits = simple_type_align_in_bits (type);
10025       decl_align_in_bits = simple_decl_align_in_bits (decl);
10026
10027       /* The GCC front-end doesn't make any attempt to keep track of the
10028          starting bit offset (relative to the start of the containing
10029          structure type) of the hypothetical "containing object" for a
10030          bit-field.  Thus, when computing the byte offset value for the
10031          start of the "containing object" of a bit-field, we must deduce
10032          this information on our own. This can be rather tricky to do in
10033          some cases.  For example, handling the following structure type
10034          definition when compiling for an i386/i486 target (which only
10035          aligns long long's to 32-bit boundaries) can be very tricky:
10036
10037          struct S { int field1; long long field2:31; };
10038
10039          Fortunately, there is a simple rule-of-thumb which can be used
10040          in such cases.  When compiling for an i386/i486, GCC will
10041          allocate 8 bytes for the structure shown above.  It decides to
10042          do this based upon one simple rule for bit-field allocation.
10043          GCC allocates each "containing object" for each bit-field at
10044          the first (i.e. lowest addressed) legitimate alignment boundary
10045          (based upon the required minimum alignment for the declared
10046          type of the field) which it can possibly use, subject to the
10047          condition that there is still enough available space remaining
10048          in the containing object (when allocated at the selected point)
10049          to fully accommodate all of the bits of the bit-field itself.
10050
10051          This simple rule makes it obvious why GCC allocates 8 bytes for
10052          each object of the structure type shown above.  When looking
10053          for a place to allocate the "containing object" for `field2',
10054          the compiler simply tries to allocate a 64-bit "containing
10055          object" at each successive 32-bit boundary (starting at zero)
10056          until it finds a place to allocate that 64- bit field such that
10057          at least 31 contiguous (and previously unallocated) bits remain
10058          within that selected 64 bit field.  (As it turns out, for the
10059          example above, the compiler finds it is OK to allocate the
10060          "containing object" 64-bit field at bit-offset zero within the
10061          structure type.)
10062
10063          Here we attempt to work backwards from the limited set of facts
10064          we're given, and we try to deduce from those facts, where GCC
10065          must have believed that the containing object started (within
10066          the structure type). The value we deduce is then used (by the
10067          callers of this routine) to generate DW_AT_location and
10068          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10069          the case of DW_AT_location, regular fields as well).  */
10070
10071       /* Figure out the bit-distance from the start of the structure to
10072          the "deepest" bit of the bit-field.  */
10073       deepest_bitpos = bitpos_int + field_size_in_bits;
10074
10075       /* This is the tricky part.  Use some fancy footwork to deduce
10076          where the lowest addressed bit of the containing object must
10077          be.  */
10078       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10079
10080       /* Round up to type_align by default.  This works best for
10081          bitfields.  */
10082       object_offset_in_bits
10083         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10084
10085       if (object_offset_in_bits > bitpos_int)
10086         {
10087           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10088
10089           /* Round up to decl_align instead.  */
10090           object_offset_in_bits
10091             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10092         }
10093     }
10094   else
10095 #endif
10096     object_offset_in_bits = bitpos_int;
10097
10098   return object_offset_in_bits / BITS_PER_UNIT;
10099 }
10100 \f
10101 /* The following routines define various Dwarf attributes and any data
10102    associated with them.  */
10103
10104 /* Add a location description attribute value to a DIE.
10105
10106    This emits location attributes suitable for whole variables and
10107    whole parameters.  Note that the location attributes for struct fields are
10108    generated by the routine `data_member_location_attribute' below.  */
10109
10110 static inline void
10111 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10112                              dw_loc_descr_ref descr)
10113 {
10114   if (descr != 0)
10115     add_AT_loc (die, attr_kind, descr);
10116 }
10117
10118 /* Attach the specialized form of location attribute used for data members of
10119    struct and union types.  In the special case of a FIELD_DECL node which
10120    represents a bit-field, the "offset" part of this special location
10121    descriptor must indicate the distance in bytes from the lowest-addressed
10122    byte of the containing struct or union type to the lowest-addressed byte of
10123    the "containing object" for the bit-field.  (See the `field_byte_offset'
10124    function above).
10125
10126    For any given bit-field, the "containing object" is a hypothetical object
10127    (of some integral or enum type) within which the given bit-field lives.  The
10128    type of this hypothetical "containing object" is always the same as the
10129    declared type of the individual bit-field itself (for GCC anyway... the
10130    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10131    bytes) of the hypothetical "containing object" which will be given in the
10132    DW_AT_byte_size attribute for this bit-field.  (See the
10133    `byte_size_attribute' function below.)  It is also used when calculating the
10134    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10135    function below.)  */
10136
10137 static void
10138 add_data_member_location_attribute (dw_die_ref die, tree decl)
10139 {
10140   HOST_WIDE_INT offset;
10141   dw_loc_descr_ref loc_descr = 0;
10142
10143   if (TREE_CODE (decl) == TREE_BINFO)
10144     {
10145       /* We're working on the TAG_inheritance for a base class.  */
10146       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10147         {
10148           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10149              aren't at a fixed offset from all (sub)objects of the same
10150              type.  We need to extract the appropriate offset from our
10151              vtable.  The following dwarf expression means
10152
10153                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10154
10155              This is specific to the V3 ABI, of course.  */
10156
10157           dw_loc_descr_ref tmp;
10158
10159           /* Make a copy of the object address.  */
10160           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10161           add_loc_descr (&loc_descr, tmp);
10162
10163           /* Extract the vtable address.  */
10164           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10165           add_loc_descr (&loc_descr, tmp);
10166
10167           /* Calculate the address of the offset.  */
10168           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10169           gcc_assert (offset < 0);
10170
10171           tmp = int_loc_descriptor (-offset);
10172           add_loc_descr (&loc_descr, tmp);
10173           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10174           add_loc_descr (&loc_descr, tmp);
10175
10176           /* Extract the offset.  */
10177           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10178           add_loc_descr (&loc_descr, tmp);
10179
10180           /* Add it to the object address.  */
10181           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10182           add_loc_descr (&loc_descr, tmp);
10183         }
10184       else
10185         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10186     }
10187   else
10188     offset = field_byte_offset (decl);
10189
10190   if (! loc_descr)
10191     {
10192       enum dwarf_location_atom op;
10193
10194       /* The DWARF2 standard says that we should assume that the structure
10195          address is already on the stack, so we can specify a structure field
10196          address by using DW_OP_plus_uconst.  */
10197
10198 #ifdef MIPS_DEBUGGING_INFO
10199       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10200          operator correctly.  It works only if we leave the offset on the
10201          stack.  */
10202       op = DW_OP_constu;
10203 #else
10204       op = DW_OP_plus_uconst;
10205 #endif
10206
10207       loc_descr = new_loc_descr (op, offset, 0);
10208     }
10209
10210   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10211 }
10212
10213 /* Writes integer values to dw_vec_const array.  */
10214
10215 static void
10216 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10217 {
10218   while (size != 0)
10219     {
10220       *dest++ = val & 0xff;
10221       val >>= 8;
10222       --size;
10223     }
10224 }
10225
10226 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10227
10228 static HOST_WIDE_INT
10229 extract_int (const unsigned char *src, unsigned int size)
10230 {
10231   HOST_WIDE_INT val = 0;
10232
10233   src += size;
10234   while (size != 0)
10235     {
10236       val <<= 8;
10237       val |= *--src & 0xff;
10238       --size;
10239     }
10240   return val;
10241 }
10242
10243 /* Writes floating point values to dw_vec_const array.  */
10244
10245 static void
10246 insert_float (const_rtx rtl, unsigned char *array)
10247 {
10248   REAL_VALUE_TYPE rv;
10249   long val[4];
10250   int i;
10251
10252   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10253   real_to_target (val, &rv, GET_MODE (rtl));
10254
10255   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10256   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10257     {
10258       insert_int (val[i], 4, array);
10259       array += 4;
10260     }
10261 }
10262
10263 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10264    does not have a "location" either in memory or in a register.  These
10265    things can arise in GNU C when a constant is passed as an actual parameter
10266    to an inlined function.  They can also arise in C++ where declared
10267    constants do not necessarily get memory "homes".  */
10268
10269 static void
10270 add_const_value_attribute (dw_die_ref die, rtx rtl)
10271 {
10272   switch (GET_CODE (rtl))
10273     {
10274     case CONST_INT:
10275       {
10276         HOST_WIDE_INT val = INTVAL (rtl);
10277
10278         if (val < 0)
10279           add_AT_int (die, DW_AT_const_value, val);
10280         else
10281           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10282       }
10283       break;
10284
10285     case CONST_DOUBLE:
10286       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10287          floating-point constant.  A CONST_DOUBLE is used whenever the
10288          constant requires more than one word in order to be adequately
10289          represented.  We output CONST_DOUBLEs as blocks.  */
10290       {
10291         enum machine_mode mode = GET_MODE (rtl);
10292
10293         if (SCALAR_FLOAT_MODE_P (mode))
10294           {
10295             unsigned int length = GET_MODE_SIZE (mode);
10296             unsigned char *array = GGC_NEWVEC (unsigned char, length);
10297
10298             insert_float (rtl, array);
10299             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10300           }
10301         else
10302           {
10303             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10304             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10305
10306             add_AT_long_long (die, DW_AT_const_value,
10307                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10308           }
10309       }
10310       break;
10311
10312     case CONST_VECTOR:
10313       {
10314         enum machine_mode mode = GET_MODE (rtl);
10315         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10316         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10317         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
10318         unsigned int i;
10319         unsigned char *p;
10320
10321         switch (GET_MODE_CLASS (mode))
10322           {
10323           case MODE_VECTOR_INT:
10324             for (i = 0, p = array; i < length; i++, p += elt_size)
10325               {
10326                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10327                 HOST_WIDE_INT lo, hi;
10328
10329                 switch (GET_CODE (elt))
10330                   {
10331                   case CONST_INT:
10332                     lo = INTVAL (elt);
10333                     hi = -(lo < 0);
10334                     break;
10335
10336                   case CONST_DOUBLE:
10337                     lo = CONST_DOUBLE_LOW (elt);
10338                     hi = CONST_DOUBLE_HIGH (elt);
10339                     break;
10340
10341                   default:
10342                     gcc_unreachable ();
10343                   }
10344
10345                 if (elt_size <= sizeof (HOST_WIDE_INT))
10346                   insert_int (lo, elt_size, p);
10347                 else
10348                   {
10349                     unsigned char *p0 = p;
10350                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10351
10352                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10353                     if (WORDS_BIG_ENDIAN)
10354                       {
10355                         p0 = p1;
10356                         p1 = p;
10357                       }
10358                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10359                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10360                   }
10361               }
10362             break;
10363
10364           case MODE_VECTOR_FLOAT:
10365             for (i = 0, p = array; i < length; i++, p += elt_size)
10366               {
10367                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10368                 insert_float (elt, p);
10369               }
10370             break;
10371
10372           default:
10373             gcc_unreachable ();
10374           }
10375
10376         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10377       }
10378       break;
10379
10380     case CONST_STRING:
10381       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10382       break;
10383
10384     case SYMBOL_REF:
10385     case LABEL_REF:
10386     case CONST:
10387       add_AT_addr (die, DW_AT_const_value, rtl);
10388       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10389       break;
10390
10391     case PLUS:
10392       /* In cases where an inlined instance of an inline function is passed
10393          the address of an `auto' variable (which is local to the caller) we
10394          can get a situation where the DECL_RTL of the artificial local
10395          variable (for the inlining) which acts as a stand-in for the
10396          corresponding formal parameter (of the inline function) will look
10397          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10398          exactly a compile-time constant expression, but it isn't the address
10399          of the (artificial) local variable either.  Rather, it represents the
10400          *value* which the artificial local variable always has during its
10401          lifetime.  We currently have no way to represent such quasi-constant
10402          values in Dwarf, so for now we just punt and generate nothing.  */
10403       break;
10404
10405     default:
10406       /* No other kinds of rtx should be possible here.  */
10407       gcc_unreachable ();
10408     }
10409
10410 }
10411
10412 /* Determine whether the evaluation of EXPR references any variables
10413    or functions which aren't otherwise used (and therefore may not be
10414    output).  */
10415 static tree
10416 reference_to_unused (tree * tp, int * walk_subtrees,
10417                      void * data ATTRIBUTE_UNUSED)
10418 {
10419   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10420     *walk_subtrees = 0;
10421
10422   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10423       && ! TREE_ASM_WRITTEN (*tp))
10424     return *tp;
10425   else if (!flag_unit_at_a_time)
10426     return NULL_TREE;
10427   /* ???  The C++ FE emits debug information for using decls, so
10428      putting gcc_unreachable here falls over.  See PR31899.  For now
10429      be conservative.  */
10430   else if (!cgraph_global_info_ready
10431            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10432     return *tp;
10433   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10434     {
10435       struct varpool_node *node = varpool_node (*tp);
10436       if (!node->needed)
10437         return *tp;
10438     }
10439   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10440            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10441     {
10442       struct cgraph_node *node = cgraph_node (*tp);
10443       if (!node->output)
10444         return *tp;
10445     }
10446   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10447     return *tp;
10448
10449   return NULL_TREE;
10450 }
10451
10452 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10453    for use in a later add_const_value_attribute call.  */
10454
10455 static rtx
10456 rtl_for_decl_init (tree init, tree type)
10457 {
10458   rtx rtl = NULL_RTX;
10459
10460   /* If a variable is initialized with a string constant without embedded
10461      zeros, build CONST_STRING.  */
10462   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10463     {
10464       tree enttype = TREE_TYPE (type);
10465       tree domain = TYPE_DOMAIN (type);
10466       enum machine_mode mode = TYPE_MODE (enttype);
10467
10468       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10469           && domain
10470           && integer_zerop (TYPE_MIN_VALUE (domain))
10471           && compare_tree_int (TYPE_MAX_VALUE (domain),
10472                                TREE_STRING_LENGTH (init) - 1) == 0
10473           && ((size_t) TREE_STRING_LENGTH (init)
10474               == strlen (TREE_STRING_POINTER (init)) + 1))
10475         rtl = gen_rtx_CONST_STRING (VOIDmode,
10476                                     ggc_strdup (TREE_STRING_POINTER (init)));
10477     }
10478   /* Other aggregates, and complex values, could be represented using
10479      CONCAT: FIXME!  */
10480   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10481     ;
10482   /* Vectors only work if their mode is supported by the target.
10483      FIXME: generic vectors ought to work too.  */
10484   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10485     ;
10486   /* If the initializer is something that we know will expand into an
10487      immediate RTL constant, expand it now.  We must be careful not to
10488      reference variables which won't be output.  */
10489   else if (initializer_constant_valid_p (init, type)
10490            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10491     {
10492       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10493          possible.  */
10494       if (TREE_CODE (type) == VECTOR_TYPE)
10495         switch (TREE_CODE (init))
10496           {
10497           case VECTOR_CST:
10498             break;
10499           case CONSTRUCTOR:
10500             if (TREE_CONSTANT (init))
10501               {
10502                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10503                 bool constant_p = true;
10504                 tree value;
10505                 unsigned HOST_WIDE_INT ix;
10506
10507                 /* Even when ctor is constant, it might contain non-*_CST
10508                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10509                    belong into VECTOR_CST nodes.  */
10510                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10511                   if (!CONSTANT_CLASS_P (value))
10512                     {
10513                       constant_p = false;
10514                       break;
10515                     }
10516
10517                 if (constant_p)
10518                   {
10519                     init = build_vector_from_ctor (type, elts);
10520                     break;
10521                   }
10522               }
10523             /* FALLTHRU */
10524
10525           default:
10526             return NULL;
10527           }
10528
10529       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10530
10531       /* If expand_expr returns a MEM, it wasn't immediate.  */
10532       gcc_assert (!rtl || !MEM_P (rtl));
10533     }
10534
10535   return rtl;
10536 }
10537
10538 /* Generate RTL for the variable DECL to represent its location.  */
10539
10540 static rtx
10541 rtl_for_decl_location (tree decl)
10542 {
10543   rtx rtl;
10544
10545   /* Here we have to decide where we are going to say the parameter "lives"
10546      (as far as the debugger is concerned).  We only have a couple of
10547      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10548
10549      DECL_RTL normally indicates where the parameter lives during most of the
10550      activation of the function.  If optimization is enabled however, this
10551      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10552      that the parameter doesn't really live anywhere (as far as the code
10553      generation parts of GCC are concerned) during most of the function's
10554      activation.  That will happen (for example) if the parameter is never
10555      referenced within the function.
10556
10557      We could just generate a location descriptor here for all non-NULL
10558      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10559      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10560      where DECL_RTL is NULL or is a pseudo-reg.
10561
10562      Note however that we can only get away with using DECL_INCOMING_RTL as
10563      a backup substitute for DECL_RTL in certain limited cases.  In cases
10564      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10565      we can be sure that the parameter was passed using the same type as it is
10566      declared to have within the function, and that its DECL_INCOMING_RTL
10567      points us to a place where a value of that type is passed.
10568
10569      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10570      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10571      because in these cases DECL_INCOMING_RTL points us to a value of some
10572      type which is *different* from the type of the parameter itself.  Thus,
10573      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10574      such cases, the debugger would end up (for example) trying to fetch a
10575      `float' from a place which actually contains the first part of a
10576      `double'.  That would lead to really incorrect and confusing
10577      output at debug-time.
10578
10579      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10580      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10581      are a couple of exceptions however.  On little-endian machines we can
10582      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10583      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10584      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10585      when (on a little-endian machine) a non-prototyped function has a
10586      parameter declared to be of type `short' or `char'.  In such cases,
10587      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10588      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10589      passed `int' value.  If the debugger then uses that address to fetch
10590      a `short' or a `char' (on a little-endian machine) the result will be
10591      the correct data, so we allow for such exceptional cases below.
10592
10593      Note that our goal here is to describe the place where the given formal
10594      parameter lives during most of the function's activation (i.e. between the
10595      end of the prologue and the start of the epilogue).  We'll do that as best
10596      as we can. Note however that if the given formal parameter is modified
10597      sometime during the execution of the function, then a stack backtrace (at
10598      debug-time) will show the function as having been called with the *new*
10599      value rather than the value which was originally passed in.  This happens
10600      rarely enough that it is not a major problem, but it *is* a problem, and
10601      I'd like to fix it.
10602
10603      A future version of dwarf2out.c may generate two additional attributes for
10604      any given DW_TAG_formal_parameter DIE which will describe the "passed
10605      type" and the "passed location" for the given formal parameter in addition
10606      to the attributes we now generate to indicate the "declared type" and the
10607      "active location" for each parameter.  This additional set of attributes
10608      could be used by debuggers for stack backtraces. Separately, note that
10609      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10610      This happens (for example) for inlined-instances of inline function formal
10611      parameters which are never referenced.  This really shouldn't be
10612      happening.  All PARM_DECL nodes should get valid non-NULL
10613      DECL_INCOMING_RTL values.  FIXME.  */
10614
10615   /* Use DECL_RTL as the "location" unless we find something better.  */
10616   rtl = DECL_RTL_IF_SET (decl);
10617
10618   /* When generating abstract instances, ignore everything except
10619      constants, symbols living in memory, and symbols living in
10620      fixed registers.  */
10621   if (! reload_completed)
10622     {
10623       if (rtl
10624           && (CONSTANT_P (rtl)
10625               || (MEM_P (rtl)
10626                   && CONSTANT_P (XEXP (rtl, 0)))
10627               || (REG_P (rtl)
10628                   && TREE_CODE (decl) == VAR_DECL
10629                   && TREE_STATIC (decl))))
10630         {
10631           rtl = targetm.delegitimize_address (rtl);
10632           return rtl;
10633         }
10634       rtl = NULL_RTX;
10635     }
10636   else if (TREE_CODE (decl) == PARM_DECL)
10637     {
10638       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10639         {
10640           tree declared_type = TREE_TYPE (decl);
10641           tree passed_type = DECL_ARG_TYPE (decl);
10642           enum machine_mode dmode = TYPE_MODE (declared_type);
10643           enum machine_mode pmode = TYPE_MODE (passed_type);
10644
10645           /* This decl represents a formal parameter which was optimized out.
10646              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10647              all cases where (rtl == NULL_RTX) just below.  */
10648           if (dmode == pmode)
10649             rtl = DECL_INCOMING_RTL (decl);
10650           else if (SCALAR_INT_MODE_P (dmode)
10651                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10652                    && DECL_INCOMING_RTL (decl))
10653             {
10654               rtx inc = DECL_INCOMING_RTL (decl);
10655               if (REG_P (inc))
10656                 rtl = inc;
10657               else if (MEM_P (inc))
10658                 {
10659                   if (BYTES_BIG_ENDIAN)
10660                     rtl = adjust_address_nv (inc, dmode,
10661                                              GET_MODE_SIZE (pmode)
10662                                              - GET_MODE_SIZE (dmode));
10663                   else
10664                     rtl = inc;
10665                 }
10666             }
10667         }
10668
10669       /* If the parm was passed in registers, but lives on the stack, then
10670          make a big endian correction if the mode of the type of the
10671          parameter is not the same as the mode of the rtl.  */
10672       /* ??? This is the same series of checks that are made in dbxout.c before
10673          we reach the big endian correction code there.  It isn't clear if all
10674          of these checks are necessary here, but keeping them all is the safe
10675          thing to do.  */
10676       else if (MEM_P (rtl)
10677                && XEXP (rtl, 0) != const0_rtx
10678                && ! CONSTANT_P (XEXP (rtl, 0))
10679                /* Not passed in memory.  */
10680                && !MEM_P (DECL_INCOMING_RTL (decl))
10681                /* Not passed by invisible reference.  */
10682                && (!REG_P (XEXP (rtl, 0))
10683                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10684                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10685 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10686                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10687 #endif
10688                      )
10689                /* Big endian correction check.  */
10690                && BYTES_BIG_ENDIAN
10691                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10692                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10693                    < UNITS_PER_WORD))
10694         {
10695           int offset = (UNITS_PER_WORD
10696                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10697
10698           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10699                              plus_constant (XEXP (rtl, 0), offset));
10700         }
10701     }
10702   else if (TREE_CODE (decl) == VAR_DECL
10703            && rtl
10704            && MEM_P (rtl)
10705            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10706            && BYTES_BIG_ENDIAN)
10707     {
10708       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10709       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10710
10711       /* If a variable is declared "register" yet is smaller than
10712          a register, then if we store the variable to memory, it
10713          looks like we're storing a register-sized value, when in
10714          fact we are not.  We need to adjust the offset of the
10715          storage location to reflect the actual value's bytes,
10716          else gdb will not be able to display it.  */
10717       if (rsize > dsize)
10718         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10719                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10720     }
10721
10722   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10723      and will have been substituted directly into all expressions that use it.
10724      C does not have such a concept, but C++ and other languages do.  */
10725   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10726     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10727
10728   if (rtl)
10729     rtl = targetm.delegitimize_address (rtl);
10730
10731   /* If we don't look past the constant pool, we risk emitting a
10732      reference to a constant pool entry that isn't referenced from
10733      code, and thus is not emitted.  */
10734   if (rtl)
10735     rtl = avoid_constant_pool_reference (rtl);
10736
10737   return rtl;
10738 }
10739
10740 /* We need to figure out what section we should use as the base for the
10741    address ranges where a given location is valid.
10742    1. If this particular DECL has a section associated with it, use that.
10743    2. If this function has a section associated with it, use that.
10744    3. Otherwise, use the text section.
10745    XXX: If you split a variable across multiple sections, we won't notice.  */
10746
10747 static const char *
10748 secname_for_decl (const_tree decl)
10749 {
10750   const char *secname;
10751
10752   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10753     {
10754       tree sectree = DECL_SECTION_NAME (decl);
10755       secname = TREE_STRING_POINTER (sectree);
10756     }
10757   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10758     {
10759       tree sectree = DECL_SECTION_NAME (current_function_decl);
10760       secname = TREE_STRING_POINTER (sectree);
10761     }
10762   else if (cfun && in_cold_section_p)
10763     secname = crtl->subsections.cold_section_label;
10764   else
10765     secname = text_section_label;
10766
10767   return secname;
10768 }
10769
10770 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_RTX is returned.
10771    If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
10772    value is the offset into the common block for the symbol.  */
10773
10774 static tree
10775 fortran_common (tree decl, HOST_WIDE_INT *value)
10776 {
10777   tree val_expr, cvar;
10778   enum machine_mode mode;
10779   HOST_WIDE_INT bitsize, bitpos;
10780   tree offset;
10781   int volatilep = 0, unsignedp = 0;
10782
10783   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
10784      it does not have a value (the offset into the common area), or if it
10785      is thread local (as opposed to global) then it isn't common, and shouldn't
10786      be handled as such.  */
10787   if (TREE_CODE (decl) != VAR_DECL
10788       || !TREE_PUBLIC (decl)
10789       || !TREE_STATIC (decl)
10790       || !DECL_HAS_VALUE_EXPR_P (decl)
10791       || !is_fortran ())
10792     return NULL_TREE;
10793
10794   val_expr = DECL_VALUE_EXPR (decl);
10795   if (TREE_CODE (val_expr) != COMPONENT_REF)
10796     return NULL_TREE;
10797
10798   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
10799                               &mode, &unsignedp, &volatilep, true);
10800
10801   if (cvar == NULL_TREE
10802       || TREE_CODE (cvar) != VAR_DECL
10803       || DECL_ARTIFICIAL (cvar)
10804       || !TREE_PUBLIC (cvar))
10805     return NULL_TREE;
10806
10807   *value = 0;
10808   if (offset != NULL)
10809     {
10810       if (!host_integerp (offset, 0))
10811         return NULL_TREE;
10812       *value = tree_low_cst (offset, 0);
10813     }
10814   if (bitpos != 0)
10815     *value += bitpos / BITS_PER_UNIT;
10816
10817   return cvar;
10818 }
10819
10820
10821 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10822    data attribute for a variable or a parameter.  We generate the
10823    DW_AT_const_value attribute only in those cases where the given variable
10824    or parameter does not have a true "location" either in memory or in a
10825    register.  This can happen (for example) when a constant is passed as an
10826    actual argument in a call to an inline function.  (It's possible that
10827    these things can crop up in other ways also.)  Note that one type of
10828    constant value which can be passed into an inlined function is a constant
10829    pointer.  This can happen for example if an actual argument in an inlined
10830    function call evaluates to a compile-time constant address.  */
10831
10832 static void
10833 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10834                                        enum dwarf_attribute attr)
10835 {
10836   rtx rtl;
10837   dw_loc_descr_ref descr;
10838   var_loc_list *loc_list;
10839   struct var_loc_node *node;
10840   if (TREE_CODE (decl) == ERROR_MARK)
10841     return;
10842
10843   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10844               || TREE_CODE (decl) == RESULT_DECL);
10845
10846   /* See if we possibly have multiple locations for this variable.  */
10847   loc_list = lookup_decl_loc (decl);
10848
10849   /* If it truly has multiple locations, the first and last node will
10850      differ.  */
10851   if (loc_list && loc_list->first != loc_list->last)
10852     {
10853       const char *endname, *secname;
10854       dw_loc_list_ref list;
10855       rtx varloc;
10856       enum var_init_status initialized;
10857
10858       /* Now that we know what section we are using for a base,
10859          actually construct the list of locations.
10860          The first location information is what is passed to the
10861          function that creates the location list, and the remaining
10862          locations just get added on to that list.
10863          Note that we only know the start address for a location
10864          (IE location changes), so to build the range, we use
10865          the range [current location start, next location start].
10866          This means we have to special case the last node, and generate
10867          a range of [last location start, end of function label].  */
10868
10869       node = loc_list->first;
10870       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10871       secname = secname_for_decl (decl);
10872
10873       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10874         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10875       else
10876         initialized = VAR_INIT_STATUS_INITIALIZED;
10877
10878       list = new_loc_list (loc_descriptor (varloc, initialized),
10879                            node->label, node->next->label, secname, 1);
10880       node = node->next;
10881
10882       for (; node->next; node = node->next)
10883         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10884           {
10885             /* The variable has a location between NODE->LABEL and
10886                NODE->NEXT->LABEL.  */
10887             enum var_init_status initialized =
10888               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10889             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10890             add_loc_descr_to_loc_list (&list,
10891                                        loc_descriptor (varloc, initialized),
10892                                        node->label, node->next->label, secname);
10893           }
10894
10895       /* If the variable has a location at the last label
10896          it keeps its location until the end of function.  */
10897       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10898         {
10899           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10900           enum var_init_status initialized =
10901             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10902
10903           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10904           if (!current_function_decl)
10905             endname = text_end_label;
10906           else
10907             {
10908               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10909                                            current_function_funcdef_no);
10910               endname = ggc_strdup (label_id);
10911             }
10912           add_loc_descr_to_loc_list (&list,
10913                                      loc_descriptor (varloc, initialized),
10914                                      node->label, endname, secname);
10915         }
10916
10917       /* Finally, add the location list to the DIE, and we are done.  */
10918       add_AT_loc_list (die, attr, list);
10919       return;
10920     }
10921
10922   /* Try to get some constant RTL for this decl, and use that as the value of
10923      the location.  */
10924
10925   rtl = rtl_for_decl_location (decl);
10926   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10927     {
10928       add_const_value_attribute (die, rtl);
10929       return;
10930     }
10931
10932   /* If we have tried to generate the location otherwise, and it
10933      didn't work out (we wouldn't be here if we did), and we have a one entry
10934      location list, try generating a location from that.  */
10935   if (loc_list && loc_list->first)
10936     {
10937       enum var_init_status status;
10938       node = loc_list->first;
10939       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10940       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10941       if (descr)
10942         {
10943           add_AT_location_description (die, attr, descr);
10944           return;
10945         }
10946     }
10947
10948   /* We couldn't get any rtl, so try directly generating the location
10949      description from the tree.  */
10950   descr = loc_descriptor_from_tree (decl);
10951   if (descr)
10952     {
10953       add_AT_location_description (die, attr, descr);
10954       return;
10955     }
10956   /* None of that worked, so it must not really have a location;
10957      try adding a constant value attribute from the DECL_INITIAL.  */
10958   tree_add_const_value_attribute (die, decl);
10959 }
10960
10961 /* If we don't have a copy of this variable in memory for some reason (such
10962    as a C++ member constant that doesn't have an out-of-line definition),
10963    we should tell the debugger about the constant value.  */
10964
10965 static void
10966 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10967 {
10968   tree init = DECL_INITIAL (decl);
10969   tree type = TREE_TYPE (decl);
10970   rtx rtl;
10971
10972   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10973     /* OK */;
10974   else
10975     return;
10976
10977   rtl = rtl_for_decl_init (init, type);
10978   if (rtl)
10979     add_const_value_attribute (var_die, rtl);
10980 }
10981
10982 /* Convert the CFI instructions for the current function into a
10983    location list.  This is used for DW_AT_frame_base when we targeting
10984    a dwarf2 consumer that does not support the dwarf3
10985    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10986    expressions.  */
10987
10988 static dw_loc_list_ref
10989 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10990 {
10991   dw_fde_ref fde;
10992   dw_loc_list_ref list, *list_tail;
10993   dw_cfi_ref cfi;
10994   dw_cfa_location last_cfa, next_cfa;
10995   const char *start_label, *last_label, *section;
10996
10997   fde = current_fde ();
10998   gcc_assert (fde != NULL);
10999
11000   section = secname_for_decl (current_function_decl);
11001   list_tail = &list;
11002   list = NULL;
11003
11004   next_cfa.reg = INVALID_REGNUM;
11005   next_cfa.offset = 0;
11006   next_cfa.indirect = 0;
11007   next_cfa.base_offset = 0;
11008
11009   start_label = fde->dw_fde_begin;
11010
11011   /* ??? Bald assumption that the CIE opcode list does not contain
11012      advance opcodes.  */
11013   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11014     lookup_cfa_1 (cfi, &next_cfa);
11015
11016   last_cfa = next_cfa;
11017   last_label = start_label;
11018
11019   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11020     switch (cfi->dw_cfi_opc)
11021       {
11022       case DW_CFA_set_loc:
11023       case DW_CFA_advance_loc1:
11024       case DW_CFA_advance_loc2:
11025       case DW_CFA_advance_loc4:
11026         if (!cfa_equal_p (&last_cfa, &next_cfa))
11027           {
11028             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11029                                        start_label, last_label, section,
11030                                        list == NULL);
11031
11032             list_tail = &(*list_tail)->dw_loc_next;
11033             last_cfa = next_cfa;
11034             start_label = last_label;
11035           }
11036         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11037         break;
11038
11039       case DW_CFA_advance_loc:
11040         /* The encoding is complex enough that we should never emit this.  */
11041       case DW_CFA_remember_state:
11042       case DW_CFA_restore_state:
11043         /* We don't handle these two in this function.  It would be possible
11044            if it were to be required.  */
11045         gcc_unreachable ();
11046
11047       default:
11048         lookup_cfa_1 (cfi, &next_cfa);
11049         break;
11050       }
11051
11052   if (!cfa_equal_p (&last_cfa, &next_cfa))
11053     {
11054       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11055                                  start_label, last_label, section,
11056                                  list == NULL);
11057       list_tail = &(*list_tail)->dw_loc_next;
11058       start_label = last_label;
11059     }
11060   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11061                              start_label, fde->dw_fde_end, section,
11062                              list == NULL);
11063
11064   return list;
11065 }
11066
11067 /* Compute a displacement from the "steady-state frame pointer" to the
11068    frame base (often the same as the CFA), and store it in
11069    frame_pointer_fb_offset.  OFFSET is added to the displacement
11070    before the latter is negated.  */
11071
11072 static void
11073 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11074 {
11075   rtx reg, elim;
11076
11077 #ifdef FRAME_POINTER_CFA_OFFSET
11078   reg = frame_pointer_rtx;
11079   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11080 #else
11081   reg = arg_pointer_rtx;
11082   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11083 #endif
11084
11085   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11086   if (GET_CODE (elim) == PLUS)
11087     {
11088       offset += INTVAL (XEXP (elim, 1));
11089       elim = XEXP (elim, 0);
11090     }
11091   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
11092                        : stack_pointer_rtx));
11093
11094   frame_pointer_fb_offset = -offset;
11095 }
11096
11097 /* Generate a DW_AT_name attribute given some string value to be included as
11098    the value of the attribute.  */
11099
11100 static void
11101 add_name_attribute (dw_die_ref die, const char *name_string)
11102 {
11103   if (name_string != NULL && *name_string != 0)
11104     {
11105       if (demangle_name_func)
11106         name_string = (*demangle_name_func) (name_string);
11107
11108       add_AT_string (die, DW_AT_name, name_string);
11109     }
11110 }
11111
11112 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11113
11114 static void
11115 add_comp_dir_attribute (dw_die_ref die)
11116 {
11117   const char *wd = get_src_pwd ();
11118   if (wd != NULL)
11119     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11120 }
11121
11122 /* Given a tree node describing an array bound (either lower or upper) output
11123    a representation for that bound.  */
11124
11125 static void
11126 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11127 {
11128   switch (TREE_CODE (bound))
11129     {
11130     case ERROR_MARK:
11131       return;
11132
11133     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11134     case INTEGER_CST:
11135       if (! host_integerp (bound, 0)
11136           || (bound_attr == DW_AT_lower_bound
11137               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11138                   || (is_fortran () && integer_onep (bound)))))
11139         /* Use the default.  */
11140         ;
11141       else
11142         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11143       break;
11144
11145     CASE_CONVERT:
11146     case VIEW_CONVERT_EXPR:
11147       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11148       break;
11149
11150     case SAVE_EXPR:
11151       break;
11152
11153     case VAR_DECL:
11154     case PARM_DECL:
11155     case RESULT_DECL:
11156       {
11157         dw_die_ref decl_die = lookup_decl_die (bound);
11158
11159         /* ??? Can this happen, or should the variable have been bound
11160            first?  Probably it can, since I imagine that we try to create
11161            the types of parameters in the order in which they exist in
11162            the list, and won't have created a forward reference to a
11163            later parameter.  */
11164         if (decl_die != NULL)
11165           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11166         break;
11167       }
11168
11169     default:
11170       {
11171         /* Otherwise try to create a stack operation procedure to
11172            evaluate the value of the array bound.  */
11173
11174         dw_die_ref ctx, decl_die;
11175         dw_loc_descr_ref loc;
11176
11177         loc = loc_descriptor_from_tree (bound);
11178         if (loc == NULL)
11179           break;
11180
11181         if (current_function_decl == 0)
11182           ctx = comp_unit_die;
11183         else
11184           ctx = lookup_decl_die (current_function_decl);
11185
11186         decl_die = new_die (DW_TAG_variable, ctx, bound);
11187         add_AT_flag (decl_die, DW_AT_artificial, 1);
11188         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11189         add_AT_loc (decl_die, DW_AT_location, loc);
11190
11191         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11192         break;
11193       }
11194     }
11195 }
11196
11197 /* Note that the block of subscript information for an array type also
11198    includes information about the element type of type given array type.  */
11199
11200 static void
11201 add_subscript_info (dw_die_ref type_die, tree type)
11202 {
11203 #ifndef MIPS_DEBUGGING_INFO
11204   unsigned dimension_number;
11205 #endif
11206   tree lower, upper;
11207   dw_die_ref subrange_die;
11208
11209   /* The GNU compilers represent multidimensional array types as sequences of
11210      one dimensional array types whose element types are themselves array
11211      types.  Here we squish that down, so that each multidimensional array
11212      type gets only one array_type DIE in the Dwarf debugging info. The draft
11213      Dwarf specification say that we are allowed to do this kind of
11214      compression in C (because there is no difference between an array or
11215      arrays and a multidimensional array in C) but for other source languages
11216      (e.g. Ada) we probably shouldn't do this.  */
11217
11218   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11219      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11220      We work around this by disabling this feature.  See also
11221      gen_array_type_die.  */
11222 #ifndef MIPS_DEBUGGING_INFO
11223   for (dimension_number = 0;
11224        TREE_CODE (type) == ARRAY_TYPE;
11225        type = TREE_TYPE (type), dimension_number++)
11226 #endif
11227     {
11228       tree domain = TYPE_DOMAIN (type);
11229
11230       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11231          and (in GNU C only) variable bounds.  Handle all three forms
11232          here.  */
11233       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11234       if (domain)
11235         {
11236           /* We have an array type with specified bounds.  */
11237           lower = TYPE_MIN_VALUE (domain);
11238           upper = TYPE_MAX_VALUE (domain);
11239
11240           /* Define the index type.  */
11241           if (TREE_TYPE (domain))
11242             {
11243               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11244                  TREE_TYPE field.  We can't emit debug info for this
11245                  because it is an unnamed integral type.  */
11246               if (TREE_CODE (domain) == INTEGER_TYPE
11247                   && TYPE_NAME (domain) == NULL_TREE
11248                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11249                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11250                 ;
11251               else
11252                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11253                                     type_die);
11254             }
11255
11256           /* ??? If upper is NULL, the array has unspecified length,
11257              but it does have a lower bound.  This happens with Fortran
11258                dimension arr(N:*)
11259              Since the debugger is definitely going to need to know N
11260              to produce useful results, go ahead and output the lower
11261              bound solo, and hope the debugger can cope.  */
11262
11263           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11264           if (upper)
11265             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11266         }
11267
11268       /* Otherwise we have an array type with an unspecified length.  The
11269          DWARF-2 spec does not say how to handle this; let's just leave out the
11270          bounds.  */
11271     }
11272 }
11273
11274 static void
11275 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11276 {
11277   unsigned size;
11278
11279   switch (TREE_CODE (tree_node))
11280     {
11281     case ERROR_MARK:
11282       size = 0;
11283       break;
11284     case ENUMERAL_TYPE:
11285     case RECORD_TYPE:
11286     case UNION_TYPE:
11287     case QUAL_UNION_TYPE:
11288       size = int_size_in_bytes (tree_node);
11289       break;
11290     case FIELD_DECL:
11291       /* For a data member of a struct or union, the DW_AT_byte_size is
11292          generally given as the number of bytes normally allocated for an
11293          object of the *declared* type of the member itself.  This is true
11294          even for bit-fields.  */
11295       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11296       break;
11297     default:
11298       gcc_unreachable ();
11299     }
11300
11301   /* Note that `size' might be -1 when we get to this point.  If it is, that
11302      indicates that the byte size of the entity in question is variable.  We
11303      have no good way of expressing this fact in Dwarf at the present time,
11304      so just let the -1 pass on through.  */
11305   add_AT_unsigned (die, DW_AT_byte_size, size);
11306 }
11307
11308 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11309    which specifies the distance in bits from the highest order bit of the
11310    "containing object" for the bit-field to the highest order bit of the
11311    bit-field itself.
11312
11313    For any given bit-field, the "containing object" is a hypothetical object
11314    (of some integral or enum type) within which the given bit-field lives.  The
11315    type of this hypothetical "containing object" is always the same as the
11316    declared type of the individual bit-field itself.  The determination of the
11317    exact location of the "containing object" for a bit-field is rather
11318    complicated.  It's handled by the `field_byte_offset' function (above).
11319
11320    Note that it is the size (in bytes) of the hypothetical "containing object"
11321    which will be given in the DW_AT_byte_size attribute for this bit-field.
11322    (See `byte_size_attribute' above).  */
11323
11324 static inline void
11325 add_bit_offset_attribute (dw_die_ref die, tree decl)
11326 {
11327   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11328   tree type = DECL_BIT_FIELD_TYPE (decl);
11329   HOST_WIDE_INT bitpos_int;
11330   HOST_WIDE_INT highest_order_object_bit_offset;
11331   HOST_WIDE_INT highest_order_field_bit_offset;
11332   HOST_WIDE_INT unsigned bit_offset;
11333
11334   /* Must be a field and a bit field.  */
11335   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11336
11337   /* We can't yet handle bit-fields whose offsets are variable, so if we
11338      encounter such things, just return without generating any attribute
11339      whatsoever.  Likewise for variable or too large size.  */
11340   if (! host_integerp (bit_position (decl), 0)
11341       || ! host_integerp (DECL_SIZE (decl), 1))
11342     return;
11343
11344   bitpos_int = int_bit_position (decl);
11345
11346   /* Note that the bit offset is always the distance (in bits) from the
11347      highest-order bit of the "containing object" to the highest-order bit of
11348      the bit-field itself.  Since the "high-order end" of any object or field
11349      is different on big-endian and little-endian machines, the computation
11350      below must take account of these differences.  */
11351   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11352   highest_order_field_bit_offset = bitpos_int;
11353
11354   if (! BYTES_BIG_ENDIAN)
11355     {
11356       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11357       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11358     }
11359
11360   bit_offset
11361     = (! BYTES_BIG_ENDIAN
11362        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11363        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11364
11365   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11366 }
11367
11368 /* For a FIELD_DECL node which represents a bit field, output an attribute
11369    which specifies the length in bits of the given field.  */
11370
11371 static inline void
11372 add_bit_size_attribute (dw_die_ref die, tree decl)
11373 {
11374   /* Must be a field and a bit field.  */
11375   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11376               && DECL_BIT_FIELD_TYPE (decl));
11377
11378   if (host_integerp (DECL_SIZE (decl), 1))
11379     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11380 }
11381
11382 /* If the compiled language is ANSI C, then add a 'prototyped'
11383    attribute, if arg types are given for the parameters of a function.  */
11384
11385 static inline void
11386 add_prototyped_attribute (dw_die_ref die, tree func_type)
11387 {
11388   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11389       && TYPE_ARG_TYPES (func_type) != NULL)
11390     add_AT_flag (die, DW_AT_prototyped, 1);
11391 }
11392
11393 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11394    by looking in either the type declaration or object declaration
11395    equate table.  */
11396
11397 static inline void
11398 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11399 {
11400   dw_die_ref origin_die = NULL;
11401
11402   if (TREE_CODE (origin) != FUNCTION_DECL)
11403     {
11404       /* We may have gotten separated from the block for the inlined
11405          function, if we're in an exception handler or some such; make
11406          sure that the abstract function has been written out.
11407
11408          Doing this for nested functions is wrong, however; functions are
11409          distinct units, and our context might not even be inline.  */
11410       tree fn = origin;
11411
11412       if (TYPE_P (fn))
11413         fn = TYPE_STUB_DECL (fn);
11414
11415       fn = decl_function_context (fn);
11416       if (fn)
11417         dwarf2out_abstract_function (fn);
11418     }
11419
11420   if (DECL_P (origin))
11421     origin_die = lookup_decl_die (origin);
11422   else if (TYPE_P (origin))
11423     origin_die = lookup_type_die (origin);
11424
11425   /* XXX: Functions that are never lowered don't always have correct block
11426      trees (in the case of java, they simply have no block tree, in some other
11427      languages).  For these functions, there is nothing we can really do to
11428      output correct debug info for inlined functions in all cases.  Rather
11429      than die, we'll just produce deficient debug info now, in that we will
11430      have variables without a proper abstract origin.  In the future, when all
11431      functions are lowered, we should re-add a gcc_assert (origin_die)
11432      here.  */
11433
11434   if (origin_die)
11435       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11436 }
11437
11438 /* We do not currently support the pure_virtual attribute.  */
11439
11440 static inline void
11441 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11442 {
11443   if (DECL_VINDEX (func_decl))
11444     {
11445       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11446
11447       if (host_integerp (DECL_VINDEX (func_decl), 0))
11448         add_AT_loc (die, DW_AT_vtable_elem_location,
11449                     new_loc_descr (DW_OP_constu,
11450                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11451                                    0));
11452
11453       /* GNU extension: Record what type this method came from originally.  */
11454       if (debug_info_level > DINFO_LEVEL_TERSE)
11455         add_AT_die_ref (die, DW_AT_containing_type,
11456                         lookup_type_die (DECL_CONTEXT (func_decl)));
11457     }
11458 }
11459 \f
11460 /* Add source coordinate attributes for the given decl.  */
11461
11462 static void
11463 add_src_coords_attributes (dw_die_ref die, tree decl)
11464 {
11465   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11466
11467   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11468   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11469 }
11470
11471 /* Add a DW_AT_name attribute and source coordinate attribute for the
11472    given decl, but only if it actually has a name.  */
11473
11474 static void
11475 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11476 {
11477   tree decl_name;
11478
11479   decl_name = DECL_NAME (decl);
11480   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11481     {
11482       add_name_attribute (die, dwarf2_name (decl, 0));
11483       if (! DECL_ARTIFICIAL (decl))
11484         add_src_coords_attributes (die, decl);
11485
11486       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11487           && TREE_PUBLIC (decl)
11488           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11489           && !DECL_ABSTRACT (decl)
11490           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11491           && !is_fortran ())
11492         add_AT_string (die, DW_AT_MIPS_linkage_name,
11493                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11494     }
11495
11496 #ifdef VMS_DEBUGGING_INFO
11497   /* Get the function's name, as described by its RTL.  This may be different
11498      from the DECL_NAME name used in the source file.  */
11499   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11500     {
11501       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11502                    XEXP (DECL_RTL (decl), 0));
11503       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11504     }
11505 #endif
11506 }
11507
11508 /* Push a new declaration scope.  */
11509
11510 static void
11511 push_decl_scope (tree scope)
11512 {
11513   VEC_safe_push (tree, gc, decl_scope_table, scope);
11514 }
11515
11516 /* Pop a declaration scope.  */
11517
11518 static inline void
11519 pop_decl_scope (void)
11520 {
11521   VEC_pop (tree, decl_scope_table);
11522 }
11523
11524 /* Return the DIE for the scope that immediately contains this type.
11525    Non-named types get global scope.  Named types nested in other
11526    types get their containing scope if it's open, or global scope
11527    otherwise.  All other types (i.e. function-local named types) get
11528    the current active scope.  */
11529
11530 static dw_die_ref
11531 scope_die_for (tree t, dw_die_ref context_die)
11532 {
11533   dw_die_ref scope_die = NULL;
11534   tree containing_scope;
11535   int i;
11536
11537   /* Non-types always go in the current scope.  */
11538   gcc_assert (TYPE_P (t));
11539
11540   containing_scope = TYPE_CONTEXT (t);
11541
11542   /* Use the containing namespace if it was passed in (for a declaration).  */
11543   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11544     {
11545       if (context_die == lookup_decl_die (containing_scope))
11546         /* OK */;
11547       else
11548         containing_scope = NULL_TREE;
11549     }
11550
11551   /* Ignore function type "scopes" from the C frontend.  They mean that
11552      a tagged type is local to a parmlist of a function declarator, but
11553      that isn't useful to DWARF.  */
11554   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11555     containing_scope = NULL_TREE;
11556
11557   if (containing_scope == NULL_TREE)
11558     scope_die = comp_unit_die;
11559   else if (TYPE_P (containing_scope))
11560     {
11561       /* For types, we can just look up the appropriate DIE.  But
11562          first we check to see if we're in the middle of emitting it
11563          so we know where the new DIE should go.  */
11564       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11565         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11566           break;
11567
11568       if (i < 0)
11569         {
11570           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11571                       || TREE_ASM_WRITTEN (containing_scope));
11572
11573           /* If none of the current dies are suitable, we get file scope.  */
11574           scope_die = comp_unit_die;
11575         }
11576       else
11577         scope_die = lookup_type_die (containing_scope);
11578     }
11579   else
11580     scope_die = context_die;
11581
11582   return scope_die;
11583 }
11584
11585 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11586
11587 static inline int
11588 local_scope_p (dw_die_ref context_die)
11589 {
11590   for (; context_die; context_die = context_die->die_parent)
11591     if (context_die->die_tag == DW_TAG_inlined_subroutine
11592         || context_die->die_tag == DW_TAG_subprogram)
11593       return 1;
11594
11595   return 0;
11596 }
11597
11598 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11599    whether or not to treat a DIE in this context as a declaration.  */
11600
11601 static inline int
11602 class_or_namespace_scope_p (dw_die_ref context_die)
11603 {
11604   return (context_die
11605           && (context_die->die_tag == DW_TAG_structure_type
11606               || context_die->die_tag == DW_TAG_class_type
11607               || context_die->die_tag == DW_TAG_interface_type
11608               || context_die->die_tag == DW_TAG_union_type
11609               || context_die->die_tag == DW_TAG_namespace));
11610 }
11611
11612 /* Many forms of DIEs require a "type description" attribute.  This
11613    routine locates the proper "type descriptor" die for the type given
11614    by 'type', and adds a DW_AT_type attribute below the given die.  */
11615
11616 static void
11617 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11618                     int decl_volatile, dw_die_ref context_die)
11619 {
11620   enum tree_code code  = TREE_CODE (type);
11621   dw_die_ref type_die  = NULL;
11622
11623   /* ??? If this type is an unnamed subrange type of an integral, floating-point
11624      or fixed-point type, use the inner type.  This is because we have no
11625      support for unnamed types in base_type_die.  This can happen if this is
11626      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11627   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11628       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11629     type = TREE_TYPE (type), code = TREE_CODE (type);
11630
11631   if (code == ERROR_MARK
11632       /* Handle a special case.  For functions whose return type is void, we
11633          generate *no* type attribute.  (Note that no object may have type
11634          `void', so this only applies to function return types).  */
11635       || code == VOID_TYPE)
11636     return;
11637
11638   type_die = modified_type_die (type,
11639                                 decl_const || TYPE_READONLY (type),
11640                                 decl_volatile || TYPE_VOLATILE (type),
11641                                 context_die);
11642
11643   if (type_die != NULL)
11644     add_AT_die_ref (object_die, DW_AT_type, type_die);
11645 }
11646
11647 /* Given an object die, add the calling convention attribute for the
11648    function call type.  */
11649 static void
11650 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11651 {
11652   enum dwarf_calling_convention value = DW_CC_normal;
11653
11654   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11655
11656   /* DWARF doesn't provide a way to identify a program's source-level
11657      entry point.  DW_AT_calling_convention attributes are only meant
11658      to describe functions' calling conventions.  However, lacking a
11659      better way to signal the Fortran main program, we use this for the
11660      time being, following existing custom.  */
11661   if (is_fortran ()
11662       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11663     value = DW_CC_program;
11664
11665   /* Only add the attribute if the backend requests it, and
11666      is not DW_CC_normal.  */
11667   if (value && (value != DW_CC_normal))
11668     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11669 }
11670
11671 /* Given a tree pointer to a struct, class, union, or enum type node, return
11672    a pointer to the (string) tag name for the given type, or zero if the type
11673    was declared without a tag.  */
11674
11675 static const char *
11676 type_tag (const_tree type)
11677 {
11678   const char *name = 0;
11679
11680   if (TYPE_NAME (type) != 0)
11681     {
11682       tree t = 0;
11683
11684       /* Find the IDENTIFIER_NODE for the type name.  */
11685       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11686         t = TYPE_NAME (type);
11687
11688       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11689          a TYPE_DECL node, regardless of whether or not a `typedef' was
11690          involved.  */
11691       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11692                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11693         {
11694           /* We want to be extra verbose.  Don't call dwarf_name if
11695              DECL_NAME isn't set.  The default hook for decl_printable_name
11696              doesn't like that, and in this context it's correct to return
11697              0, instead of "<anonymous>" or the like.  */
11698           if (DECL_NAME (TYPE_NAME (type)))
11699             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11700         }
11701
11702       /* Now get the name as a string, or invent one.  */
11703       if (!name && t != 0)
11704         name = IDENTIFIER_POINTER (t);
11705     }
11706
11707   return (name == 0 || *name == '\0') ? 0 : name;
11708 }
11709
11710 /* Return the type associated with a data member, make a special check
11711    for bit field types.  */
11712
11713 static inline tree
11714 member_declared_type (const_tree member)
11715 {
11716   return (DECL_BIT_FIELD_TYPE (member)
11717           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11718 }
11719
11720 /* Get the decl's label, as described by its RTL. This may be different
11721    from the DECL_NAME name used in the source file.  */
11722
11723 #if 0
11724 static const char *
11725 decl_start_label (tree decl)
11726 {
11727   rtx x;
11728   const char *fnname;
11729
11730   x = DECL_RTL (decl);
11731   gcc_assert (MEM_P (x));
11732
11733   x = XEXP (x, 0);
11734   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11735
11736   fnname = XSTR (x, 0);
11737   return fnname;
11738 }
11739 #endif
11740 \f
11741 /* These routines generate the internal representation of the DIE's for
11742    the compilation unit.  Debugging information is collected by walking
11743    the declaration trees passed in from dwarf2out_decl().  */
11744
11745 static void
11746 gen_array_type_die (tree type, dw_die_ref context_die)
11747 {
11748   dw_die_ref scope_die = scope_die_for (type, context_die);
11749   dw_die_ref array_die;
11750   tree element_type;
11751
11752   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11753      the inner array type comes before the outer array type.  Thus we must
11754      call gen_type_die before we call new_die.  See below also.  */
11755 #ifdef MIPS_DEBUGGING_INFO
11756   gen_type_die (TREE_TYPE (type), context_die);
11757 #endif
11758
11759   array_die = new_die (DW_TAG_array_type, scope_die, type);
11760   add_name_attribute (array_die, type_tag (type));
11761   equate_type_number_to_die (type, array_die);
11762
11763   if (TREE_CODE (type) == VECTOR_TYPE)
11764     {
11765       /* The frontend feeds us a representation for the vector as a struct
11766          containing an array.  Pull out the array type.  */
11767       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11768       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11769     }
11770
11771   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11772   if (is_fortran ()
11773       && TREE_CODE (type) == ARRAY_TYPE
11774       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11775     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11776
11777 #if 0
11778   /* We default the array ordering.  SDB will probably do
11779      the right things even if DW_AT_ordering is not present.  It's not even
11780      an issue until we start to get into multidimensional arrays anyway.  If
11781      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11782      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11783      and when we find out that we need to put these in, we will only do so
11784      for multidimensional arrays.  */
11785   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11786 #endif
11787
11788 #ifdef MIPS_DEBUGGING_INFO
11789   /* The SGI compilers handle arrays of unknown bound by setting
11790      AT_declaration and not emitting any subrange DIEs.  */
11791   if (! TYPE_DOMAIN (type))
11792     add_AT_flag (array_die, DW_AT_declaration, 1);
11793   else
11794 #endif
11795     add_subscript_info (array_die, type);
11796
11797   /* Add representation of the type of the elements of this array type.  */
11798   element_type = TREE_TYPE (type);
11799
11800   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11801      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11802      We work around this by disabling this feature.  See also
11803      add_subscript_info.  */
11804 #ifndef MIPS_DEBUGGING_INFO
11805   while (TREE_CODE (element_type) == ARRAY_TYPE)
11806     element_type = TREE_TYPE (element_type);
11807
11808   gen_type_die (element_type, context_die);
11809 #endif
11810
11811   add_type_attribute (array_die, element_type, 0, 0, context_die);
11812
11813   if (get_AT (array_die, DW_AT_name))
11814     add_pubtype (type, array_die);
11815 }
11816
11817 static dw_loc_descr_ref
11818 descr_info_loc (tree val, tree base_decl)
11819 {
11820   HOST_WIDE_INT size;
11821   dw_loc_descr_ref loc, loc2;
11822   enum dwarf_location_atom op;
11823
11824   if (val == base_decl)
11825     return new_loc_descr (DW_OP_push_object_address, 0, 0);
11826
11827   switch (TREE_CODE (val))
11828     {
11829     CASE_CONVERT:
11830       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11831     case INTEGER_CST:
11832       if (host_integerp (val, 0))
11833         return int_loc_descriptor (tree_low_cst (val, 0));
11834       break;
11835     case INDIRECT_REF:
11836       size = int_size_in_bytes (TREE_TYPE (val));
11837       if (size < 0)
11838         break;
11839       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11840       if (!loc)
11841         break;
11842       if (size == DWARF2_ADDR_SIZE)
11843         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11844       else
11845         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11846       return loc;
11847     case POINTER_PLUS_EXPR:
11848     case PLUS_EXPR:
11849       if (host_integerp (TREE_OPERAND (val, 1), 1)
11850           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11851              < 16384)
11852         {
11853           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11854           if (!loc)
11855             break;
11856           add_loc_descr (&loc,
11857                          new_loc_descr (DW_OP_plus_uconst,
11858                                         tree_low_cst (TREE_OPERAND (val, 1),
11859                                                       1), 0));
11860         }
11861       else
11862         {
11863           op = DW_OP_plus;
11864         do_binop:
11865           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11866           if (!loc)
11867             break;
11868           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11869           if (!loc2)
11870             break;
11871           add_loc_descr (&loc, loc2);
11872           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11873         }
11874       return loc;
11875     case MINUS_EXPR:
11876       op = DW_OP_minus;
11877       goto do_binop;
11878     case MULT_EXPR:
11879       op = DW_OP_mul;
11880       goto do_binop;
11881     case EQ_EXPR:
11882       op = DW_OP_eq;
11883       goto do_binop;
11884     case NE_EXPR:
11885       op = DW_OP_ne;
11886       goto do_binop;
11887     default:
11888       break;
11889     }
11890   return NULL;
11891 }
11892
11893 static void
11894 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11895                       tree val, tree base_decl)
11896 {
11897   dw_loc_descr_ref loc;
11898
11899   if (host_integerp (val, 0))
11900     {
11901       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11902       return;
11903     }
11904
11905   loc = descr_info_loc (val, base_decl);
11906   if (!loc)
11907     return;
11908
11909   add_AT_loc (die, attr, loc);
11910 }
11911
11912 /* This routine generates DIE for array with hidden descriptor, details
11913    are filled into *info by a langhook.  */
11914
11915 static void
11916 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11917                           dw_die_ref context_die)
11918 {
11919   dw_die_ref scope_die = scope_die_for (type, context_die);
11920   dw_die_ref array_die;
11921   int dim;
11922
11923   array_die = new_die (DW_TAG_array_type, scope_die, type);
11924   add_name_attribute (array_die, type_tag (type));
11925   equate_type_number_to_die (type, array_die);
11926
11927   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11928   if (is_fortran ()
11929       && info->ndimensions >= 2)
11930     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11931
11932   if (info->data_location)
11933     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11934                           info->base_decl);
11935   if (info->associated)
11936     add_descr_info_field (array_die, DW_AT_associated, info->associated,
11937                           info->base_decl);
11938   if (info->allocated)
11939     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11940                           info->base_decl);
11941
11942   for (dim = 0; dim < info->ndimensions; dim++)
11943     {
11944       dw_die_ref subrange_die
11945         = new_die (DW_TAG_subrange_type, array_die, NULL);
11946
11947       if (info->dimen[dim].lower_bound)
11948         {
11949           /* If it is the default value, omit it.  */
11950           if ((is_c_family () || is_java ())
11951               && integer_zerop (info->dimen[dim].lower_bound))
11952             ;
11953           else if (is_fortran ()
11954                    && integer_onep (info->dimen[dim].lower_bound))
11955             ;
11956           else
11957             add_descr_info_field (subrange_die, DW_AT_lower_bound,
11958                                   info->dimen[dim].lower_bound,
11959                                   info->base_decl);
11960         }
11961       if (info->dimen[dim].upper_bound)
11962         add_descr_info_field (subrange_die, DW_AT_upper_bound,
11963                               info->dimen[dim].upper_bound,
11964                               info->base_decl);
11965       if (info->dimen[dim].stride)
11966         add_descr_info_field (subrange_die, DW_AT_byte_stride,
11967                               info->dimen[dim].stride,
11968                               info->base_decl);
11969     }
11970
11971   gen_type_die (info->element_type, context_die);
11972   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11973
11974   if (get_AT (array_die, DW_AT_name))
11975     add_pubtype (type, array_die);
11976 }
11977
11978 #if 0
11979 static void
11980 gen_entry_point_die (tree decl, dw_die_ref context_die)
11981 {
11982   tree origin = decl_ultimate_origin (decl);
11983   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11984
11985   if (origin != NULL)
11986     add_abstract_origin_attribute (decl_die, origin);
11987   else
11988     {
11989       add_name_and_src_coords_attributes (decl_die, decl);
11990       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11991                           0, 0, context_die);
11992     }
11993
11994   if (DECL_ABSTRACT (decl))
11995     equate_decl_number_to_die (decl, decl_die);
11996   else
11997     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11998 }
11999 #endif
12000
12001 /* Walk through the list of incomplete types again, trying once more to
12002    emit full debugging info for them.  */
12003
12004 static void
12005 retry_incomplete_types (void)
12006 {
12007   int i;
12008
12009   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12010     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12011 }
12012
12013 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
12014
12015 static void
12016 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12017 {
12018   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12019
12020   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12021      be incomplete and such types are not marked.  */
12022   add_abstract_origin_attribute (type_die, type);
12023 }
12024
12025 /* Determine what tag to use for a record type.  */
12026
12027 static enum dwarf_tag
12028 record_type_tag (tree type)
12029 {
12030   if (! lang_hooks.types.classify_record)
12031     return DW_TAG_structure_type;
12032
12033   switch (lang_hooks.types.classify_record (type))
12034     {
12035     case RECORD_IS_STRUCT:
12036       return DW_TAG_structure_type;
12037
12038     case RECORD_IS_CLASS:
12039       return DW_TAG_class_type;
12040
12041     case RECORD_IS_INTERFACE:
12042       return DW_TAG_interface_type;
12043
12044     default:
12045       gcc_unreachable ();
12046     }
12047 }
12048
12049 /* Generate a DIE to represent an inlined instance of a structure type.  */
12050
12051 static void
12052 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12053 {
12054   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12055
12056   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12057      be incomplete and such types are not marked.  */
12058   add_abstract_origin_attribute (type_die, type);
12059 }
12060
12061 /* Generate a DIE to represent an inlined instance of a union type.  */
12062
12063 static void
12064 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12065 {
12066   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12067
12068   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12069      be incomplete and such types are not marked.  */
12070   add_abstract_origin_attribute (type_die, type);
12071 }
12072
12073 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
12074    include all of the information about the enumeration values also. Each
12075    enumerated type name/value is listed as a child of the enumerated type
12076    DIE.  */
12077
12078 static dw_die_ref
12079 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12080 {
12081   dw_die_ref type_die = lookup_type_die (type);
12082
12083   if (type_die == NULL)
12084     {
12085       type_die = new_die (DW_TAG_enumeration_type,
12086                           scope_die_for (type, context_die), type);
12087       equate_type_number_to_die (type, type_die);
12088       add_name_attribute (type_die, type_tag (type));
12089     }
12090   else if (! TYPE_SIZE (type))
12091     return type_die;
12092   else
12093     remove_AT (type_die, DW_AT_declaration);
12094
12095   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12096      given enum type is incomplete, do not generate the DW_AT_byte_size
12097      attribute or the DW_AT_element_list attribute.  */
12098   if (TYPE_SIZE (type))
12099     {
12100       tree link;
12101
12102       TREE_ASM_WRITTEN (type) = 1;
12103       add_byte_size_attribute (type_die, type);
12104       if (TYPE_STUB_DECL (type) != NULL_TREE)
12105         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12106
12107       /* If the first reference to this type was as the return type of an
12108          inline function, then it may not have a parent.  Fix this now.  */
12109       if (type_die->die_parent == NULL)
12110         add_child_die (scope_die_for (type, context_die), type_die);
12111
12112       for (link = TYPE_VALUES (type);
12113            link != NULL; link = TREE_CHAIN (link))
12114         {
12115           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12116           tree value = TREE_VALUE (link);
12117
12118           add_name_attribute (enum_die,
12119                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12120
12121           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12122             /* DWARF2 does not provide a way of indicating whether or
12123                not enumeration constants are signed or unsigned.  GDB
12124                always assumes the values are signed, so we output all
12125                values as if they were signed.  That means that
12126                enumeration constants with very large unsigned values
12127                will appear to have negative values in the debugger.  */
12128             add_AT_int (enum_die, DW_AT_const_value,
12129                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12130         }
12131     }
12132   else
12133     add_AT_flag (type_die, DW_AT_declaration, 1);
12134
12135   if (get_AT (type_die, DW_AT_name))
12136     add_pubtype (type, type_die);
12137
12138   return type_die;
12139 }
12140
12141 /* Generate a DIE to represent either a real live formal parameter decl or to
12142    represent just the type of some formal parameter position in some function
12143    type.
12144
12145    Note that this routine is a bit unusual because its argument may be a
12146    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12147    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12148    node.  If it's the former then this function is being called to output a
12149    DIE to represent a formal parameter object (or some inlining thereof).  If
12150    it's the latter, then this function is only being called to output a
12151    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12152    argument type of some subprogram type.  */
12153
12154 static dw_die_ref
12155 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12156 {
12157   dw_die_ref parm_die
12158     = new_die (DW_TAG_formal_parameter, context_die, node);
12159   tree origin;
12160
12161   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12162     {
12163     case tcc_declaration:
12164       origin = decl_ultimate_origin (node);
12165       if (origin != NULL)
12166         add_abstract_origin_attribute (parm_die, origin);
12167       else
12168         {
12169           tree type = TREE_TYPE (node);
12170           add_name_and_src_coords_attributes (parm_die, node);
12171           if (DECL_BY_REFERENCE (node))
12172             type = TREE_TYPE (type);
12173           add_type_attribute (parm_die, type,
12174                               TREE_READONLY (node),
12175                               TREE_THIS_VOLATILE (node),
12176                               context_die);
12177           if (DECL_ARTIFICIAL (node))
12178             add_AT_flag (parm_die, DW_AT_artificial, 1);
12179         }
12180
12181       equate_decl_number_to_die (node, parm_die);
12182       if (! DECL_ABSTRACT (node))
12183         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12184
12185       break;
12186
12187     case tcc_type:
12188       /* We were called with some kind of a ..._TYPE node.  */
12189       add_type_attribute (parm_die, node, 0, 0, context_die);
12190       break;
12191
12192     default:
12193       gcc_unreachable ();
12194     }
12195
12196   return parm_die;
12197 }
12198
12199 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12200    at the end of an (ANSI prototyped) formal parameters list.  */
12201
12202 static void
12203 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12204 {
12205   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12206 }
12207
12208 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12209    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12210    parameters as specified in some function type specification (except for
12211    those which appear as part of a function *definition*).  */
12212
12213 static void
12214 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12215 {
12216   tree link;
12217   tree formal_type = NULL;
12218   tree first_parm_type;
12219   tree arg;
12220
12221   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12222     {
12223       arg = DECL_ARGUMENTS (function_or_method_type);
12224       function_or_method_type = TREE_TYPE (function_or_method_type);
12225     }
12226   else
12227     arg = NULL_TREE;
12228
12229   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12230
12231   /* Make our first pass over the list of formal parameter types and output a
12232      DW_TAG_formal_parameter DIE for each one.  */
12233   for (link = first_parm_type; link; )
12234     {
12235       dw_die_ref parm_die;
12236
12237       formal_type = TREE_VALUE (link);
12238       if (formal_type == void_type_node)
12239         break;
12240
12241       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12242       parm_die = gen_formal_parameter_die (formal_type, context_die);
12243       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12244            && link == first_parm_type)
12245           || (arg && DECL_ARTIFICIAL (arg)))
12246         add_AT_flag (parm_die, DW_AT_artificial, 1);
12247
12248       link = TREE_CHAIN (link);
12249       if (arg)
12250         arg = TREE_CHAIN (arg);
12251     }
12252
12253   /* If this function type has an ellipsis, add a
12254      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12255   if (formal_type != void_type_node)
12256     gen_unspecified_parameters_die (function_or_method_type, context_die);
12257
12258   /* Make our second (and final) pass over the list of formal parameter types
12259      and output DIEs to represent those types (as necessary).  */
12260   for (link = TYPE_ARG_TYPES (function_or_method_type);
12261        link && TREE_VALUE (link);
12262        link = TREE_CHAIN (link))
12263     gen_type_die (TREE_VALUE (link), context_die);
12264 }
12265
12266 /* We want to generate the DIE for TYPE so that we can generate the
12267    die for MEMBER, which has been defined; we will need to refer back
12268    to the member declaration nested within TYPE.  If we're trying to
12269    generate minimal debug info for TYPE, processing TYPE won't do the
12270    trick; we need to attach the member declaration by hand.  */
12271
12272 static void
12273 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12274 {
12275   gen_type_die (type, context_die);
12276
12277   /* If we're trying to avoid duplicate debug info, we may not have
12278      emitted the member decl for this function.  Emit it now.  */
12279   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12280       && ! lookup_decl_die (member))
12281     {
12282       dw_die_ref type_die;
12283       gcc_assert (!decl_ultimate_origin (member));
12284
12285       push_decl_scope (type);
12286       type_die = lookup_type_die (type);
12287       if (TREE_CODE (member) == FUNCTION_DECL)
12288         gen_subprogram_die (member, type_die);
12289       else if (TREE_CODE (member) == FIELD_DECL)
12290         {
12291           /* Ignore the nameless fields that are used to skip bits but handle
12292              C++ anonymous unions and structs.  */
12293           if (DECL_NAME (member) != NULL_TREE
12294               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12295               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12296             {
12297               gen_type_die (member_declared_type (member), type_die);
12298               gen_field_die (member, type_die);
12299             }
12300         }
12301       else
12302         gen_variable_die (member, type_die);
12303
12304       pop_decl_scope ();
12305     }
12306 }
12307
12308 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12309    may later generate inlined and/or out-of-line instances of.  */
12310
12311 static void
12312 dwarf2out_abstract_function (tree decl)
12313 {
12314   dw_die_ref old_die;
12315   tree save_fn;
12316   tree context;
12317   int was_abstract = DECL_ABSTRACT (decl);
12318
12319   /* Make sure we have the actual abstract inline, not a clone.  */
12320   decl = DECL_ORIGIN (decl);
12321
12322   old_die = lookup_decl_die (decl);
12323   if (old_die && get_AT (old_die, DW_AT_inline))
12324     /* We've already generated the abstract instance.  */
12325     return;
12326
12327   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12328      we don't get confused by DECL_ABSTRACT.  */
12329   if (debug_info_level > DINFO_LEVEL_TERSE)
12330     {
12331       context = decl_class_context (decl);
12332       if (context)
12333         gen_type_die_for_member
12334           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12335     }
12336
12337   /* Pretend we've just finished compiling this function.  */
12338   save_fn = current_function_decl;
12339   current_function_decl = decl;
12340   push_cfun (DECL_STRUCT_FUNCTION (decl));
12341
12342   set_decl_abstract_flags (decl, 1);
12343   dwarf2out_decl (decl);
12344   if (! was_abstract)
12345     set_decl_abstract_flags (decl, 0);
12346
12347   current_function_decl = save_fn;
12348   pop_cfun ();
12349 }
12350
12351 /* Helper function of premark_used_types() which gets called through
12352    htab_traverse_resize().
12353
12354    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12355    marked as unused by prune_unused_types.  */
12356 static int
12357 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12358 {
12359   tree type;
12360   dw_die_ref die;
12361
12362   type = (tree) *slot;
12363   die = lookup_type_die (type);
12364   if (die != NULL)
12365     die->die_perennial_p = 1;
12366   return 1;
12367 }
12368
12369 /* Mark all members of used_types_hash as perennial.  */
12370 static void
12371 premark_used_types (void)
12372 {
12373   if (cfun && cfun->used_types_hash)
12374     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12375 }
12376
12377 /* Generate a DIE to represent a declared function (either file-scope or
12378    block-local).  */
12379
12380 static void
12381 gen_subprogram_die (tree decl, dw_die_ref context_die)
12382 {
12383   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12384   tree origin = decl_ultimate_origin (decl);
12385   dw_die_ref subr_die;
12386   tree fn_arg_types;
12387   tree outer_scope;
12388   dw_die_ref old_die = lookup_decl_die (decl);
12389   int declaration = (current_function_decl != decl
12390                      || class_or_namespace_scope_p (context_die));
12391
12392   premark_used_types ();
12393
12394   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12395      started to generate the abstract instance of an inline, decided to output
12396      its containing class, and proceeded to emit the declaration of the inline
12397      from the member list for the class.  If so, DECLARATION takes priority;
12398      we'll get back to the abstract instance when done with the class.  */
12399
12400   /* The class-scope declaration DIE must be the primary DIE.  */
12401   if (origin && declaration && class_or_namespace_scope_p (context_die))
12402     {
12403       origin = NULL;
12404       gcc_assert (!old_die);
12405     }
12406
12407   /* Now that the C++ front end lazily declares artificial member fns, we
12408      might need to retrofit the declaration into its class.  */
12409   if (!declaration && !origin && !old_die
12410       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12411       && !class_or_namespace_scope_p (context_die)
12412       && debug_info_level > DINFO_LEVEL_TERSE)
12413     old_die = force_decl_die (decl);
12414
12415   if (origin != NULL)
12416     {
12417       gcc_assert (!declaration || local_scope_p (context_die));
12418
12419       /* Fixup die_parent for the abstract instance of a nested
12420          inline function.  */
12421       if (old_die && old_die->die_parent == NULL)
12422         add_child_die (context_die, old_die);
12423
12424       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12425       add_abstract_origin_attribute (subr_die, origin);
12426     }
12427   else if (old_die)
12428     {
12429       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12430       struct dwarf_file_data * file_index = lookup_filename (s.file);
12431
12432       if (!get_AT_flag (old_die, DW_AT_declaration)
12433           /* We can have a normal definition following an inline one in the
12434              case of redefinition of GNU C extern inlines.
12435              It seems reasonable to use AT_specification in this case.  */
12436           && !get_AT (old_die, DW_AT_inline))
12437         {
12438           /* Detect and ignore this case, where we are trying to output
12439              something we have already output.  */
12440           return;
12441         }
12442
12443       /* If the definition comes from the same place as the declaration,
12444          maybe use the old DIE.  We always want the DIE for this function
12445          that has the *_pc attributes to be under comp_unit_die so the
12446          debugger can find it.  We also need to do this for abstract
12447          instances of inlines, since the spec requires the out-of-line copy
12448          to have the same parent.  For local class methods, this doesn't
12449          apply; we just use the old DIE.  */
12450       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12451           && (DECL_ARTIFICIAL (decl)
12452               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12453                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12454                       == (unsigned) s.line))))
12455         {
12456           subr_die = old_die;
12457
12458           /* Clear out the declaration attribute and the formal parameters.
12459              Do not remove all children, because it is possible that this
12460              declaration die was forced using force_decl_die(). In such
12461              cases die that forced declaration die (e.g. TAG_imported_module)
12462              is one of the children that we do not want to remove.  */
12463           remove_AT (subr_die, DW_AT_declaration);
12464           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12465         }
12466       else
12467         {
12468           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12469           add_AT_specification (subr_die, old_die);
12470           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12471             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12472           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12473             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12474         }
12475     }
12476   else
12477     {
12478       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12479
12480       if (TREE_PUBLIC (decl))
12481         add_AT_flag (subr_die, DW_AT_external, 1);
12482
12483       add_name_and_src_coords_attributes (subr_die, decl);
12484       if (debug_info_level > DINFO_LEVEL_TERSE)
12485         {
12486           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12487           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12488                               0, 0, context_die);
12489         }
12490
12491       add_pure_or_virtual_attribute (subr_die, decl);
12492       if (DECL_ARTIFICIAL (decl))
12493         add_AT_flag (subr_die, DW_AT_artificial, 1);
12494
12495       if (TREE_PROTECTED (decl))
12496         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12497       else if (TREE_PRIVATE (decl))
12498         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12499     }
12500
12501   if (declaration)
12502     {
12503       if (!old_die || !get_AT (old_die, DW_AT_inline))
12504         {
12505           add_AT_flag (subr_die, DW_AT_declaration, 1);
12506
12507           /* The first time we see a member function, it is in the context of
12508              the class to which it belongs.  We make sure of this by emitting
12509              the class first.  The next time is the definition, which is
12510              handled above.  The two may come from the same source text.
12511
12512              Note that force_decl_die() forces function declaration die. It is
12513              later reused to represent definition.  */
12514           equate_decl_number_to_die (decl, subr_die);
12515         }
12516     }
12517   else if (DECL_ABSTRACT (decl))
12518     {
12519       if (DECL_DECLARED_INLINE_P (decl))
12520         {
12521           if (cgraph_function_possibly_inlined_p (decl))
12522             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12523           else
12524             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12525         }
12526       else
12527         {
12528           if (cgraph_function_possibly_inlined_p (decl))
12529             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12530           else
12531             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12532         }
12533
12534       if (DECL_DECLARED_INLINE_P (decl)
12535           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12536         add_AT_flag (subr_die, DW_AT_artificial, 1);
12537
12538       equate_decl_number_to_die (decl, subr_die);
12539     }
12540   else if (!DECL_EXTERNAL (decl))
12541     {
12542       HOST_WIDE_INT cfa_fb_offset;
12543
12544       if (!old_die || !get_AT (old_die, DW_AT_inline))
12545         equate_decl_number_to_die (decl, subr_die);
12546
12547       if (!flag_reorder_blocks_and_partition)
12548         {
12549           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12550                                        current_function_funcdef_no);
12551           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12552           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12553                                        current_function_funcdef_no);
12554           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12555
12556           add_pubname (decl, subr_die);
12557           add_arange (decl, subr_die);
12558         }
12559       else
12560         {  /* Do nothing for now; maybe need to duplicate die, one for
12561               hot section and one for cold section, then use the hot/cold
12562               section begin/end labels to generate the aranges...  */
12563           /*
12564             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12565             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12566             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12567             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12568
12569             add_pubname (decl, subr_die);
12570             add_arange (decl, subr_die);
12571             add_arange (decl, subr_die);
12572            */
12573         }
12574
12575 #ifdef MIPS_DEBUGGING_INFO
12576       /* Add a reference to the FDE for this routine.  */
12577       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12578 #endif
12579
12580       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12581
12582       /* We define the "frame base" as the function's CFA.  This is more
12583          convenient for several reasons: (1) It's stable across the prologue
12584          and epilogue, which makes it better than just a frame pointer,
12585          (2) With dwarf3, there exists a one-byte encoding that allows us
12586          to reference the .debug_frame data by proxy, but failing that,
12587          (3) We can at least reuse the code inspection and interpretation
12588          code that determines the CFA position at various points in the
12589          function.  */
12590       /* ??? Use some command-line or configury switch to enable the use
12591          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
12592          consumers that understand it; fall back to "pure" dwarf2 and
12593          convert the CFA data into a location list.  */
12594       {
12595         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12596         if (list->dw_loc_next)
12597           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12598         else
12599           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12600       }
12601
12602       /* Compute a displacement from the "steady-state frame pointer" to
12603          the CFA.  The former is what all stack slots and argument slots
12604          will reference in the rtl; the later is what we've told the
12605          debugger about.  We'll need to adjust all frame_base references
12606          by this displacement.  */
12607       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12608
12609       if (cfun->static_chain_decl)
12610         add_AT_location_description (subr_die, DW_AT_static_link,
12611                  loc_descriptor_from_tree (cfun->static_chain_decl));
12612     }
12613
12614   /* Now output descriptions of the arguments for this function. This gets
12615      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12616      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12617      `...' at the end of the formal parameter list.  In order to find out if
12618      there was a trailing ellipsis or not, we must instead look at the type
12619      associated with the FUNCTION_DECL.  This will be a node of type
12620      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12621      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12622      an ellipsis at the end.  */
12623
12624   /* In the case where we are describing a mere function declaration, all we
12625      need to do here (and all we *can* do here) is to describe the *types* of
12626      its formal parameters.  */
12627   if (debug_info_level <= DINFO_LEVEL_TERSE)
12628     ;
12629   else if (declaration)
12630     gen_formal_types_die (decl, subr_die);
12631   else
12632     {
12633       /* Generate DIEs to represent all known formal parameters.  */
12634       tree arg_decls = DECL_ARGUMENTS (decl);
12635       tree parm;
12636
12637       /* When generating DIEs, generate the unspecified_parameters DIE
12638          instead if we come across the arg "__builtin_va_alist" */
12639       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12640         if (TREE_CODE (parm) == PARM_DECL)
12641           {
12642             if (DECL_NAME (parm)
12643                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12644                             "__builtin_va_alist"))
12645               gen_unspecified_parameters_die (parm, subr_die);
12646             else
12647               gen_decl_die (parm, subr_die);
12648           }
12649
12650       /* Decide whether we need an unspecified_parameters DIE at the end.
12651          There are 2 more cases to do this for: 1) the ansi ... declaration -
12652          this is detectable when the end of the arg list is not a
12653          void_type_node 2) an unprototyped function declaration (not a
12654          definition).  This just means that we have no info about the
12655          parameters at all.  */
12656       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12657       if (fn_arg_types != NULL)
12658         {
12659           /* This is the prototyped case, check for....  */
12660           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12661             gen_unspecified_parameters_die (decl, subr_die);
12662         }
12663       else if (DECL_INITIAL (decl) == NULL_TREE)
12664         gen_unspecified_parameters_die (decl, subr_die);
12665     }
12666
12667   /* Output Dwarf info for all of the stuff within the body of the function
12668      (if it has one - it may be just a declaration).  */
12669   outer_scope = DECL_INITIAL (decl);
12670
12671   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12672      a function.  This BLOCK actually represents the outermost binding contour
12673      for the function, i.e. the contour in which the function's formal
12674      parameters and labels get declared. Curiously, it appears that the front
12675      end doesn't actually put the PARM_DECL nodes for the current function onto
12676      the BLOCK_VARS list for this outer scope, but are strung off of the
12677      DECL_ARGUMENTS list for the function instead.
12678
12679      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12680      the LABEL_DECL nodes for the function however, and we output DWARF info
12681      for those in decls_for_scope.  Just within the `outer_scope' there will be
12682      a BLOCK node representing the function's outermost pair of curly braces,
12683      and any blocks used for the base and member initializers of a C++
12684      constructor function.  */
12685   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12686     {
12687       /* Emit a DW_TAG_variable DIE for a named return value.  */
12688       if (DECL_NAME (DECL_RESULT (decl)))
12689         gen_decl_die (DECL_RESULT (decl), subr_die);
12690
12691       current_function_has_inlines = 0;
12692       decls_for_scope (outer_scope, subr_die, 0);
12693
12694 #if 0 && defined (MIPS_DEBUGGING_INFO)
12695       if (current_function_has_inlines)
12696         {
12697           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12698           if (! comp_unit_has_inlines)
12699             {
12700               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12701               comp_unit_has_inlines = 1;
12702             }
12703         }
12704 #endif
12705     }
12706   /* Add the calling convention attribute if requested.  */
12707   add_calling_convention_attribute (subr_die, decl);
12708
12709 }
12710
12711 /* Generate a DIE to represent a declared data object.  */
12712
12713 static void
12714 gen_variable_die (tree decl, dw_die_ref context_die)
12715 {
12716   HOST_WIDE_INT off;
12717   tree com_decl;
12718   dw_die_ref var_die;
12719   tree origin = decl_ultimate_origin (decl);
12720   dw_die_ref old_die = lookup_decl_die (decl);
12721   int declaration = (DECL_EXTERNAL (decl)
12722                      /* If DECL is COMDAT and has not actually been
12723                         emitted, we cannot take its address; there
12724                         might end up being no definition anywhere in
12725                         the program.  For example, consider the C++
12726                         test case:
12727
12728                           template <class T>
12729                           struct S { static const int i = 7; };
12730
12731                           template <class T>
12732                           const int S<T>::i;
12733
12734                           int f() { return S<int>::i; }
12735
12736                         Here, S<int>::i is not DECL_EXTERNAL, but no
12737                         definition is required, so the compiler will
12738                         not emit a definition.  */
12739                      || (TREE_CODE (decl) == VAR_DECL
12740                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12741                      || class_or_namespace_scope_p (context_die));
12742
12743   com_decl = fortran_common (decl, &off);
12744
12745   /* Symbol in common gets emitted as a child of the common block, in the form
12746      of a data member.
12747
12748      ??? This creates a new common block die for every common block symbol.
12749      Better to share same common block die for all symbols in that block.  */
12750   if (com_decl)
12751     {
12752       tree field;
12753       dw_die_ref com_die;
12754       const char *cnam = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
12755       dw_loc_descr_ref loc = loc_descriptor_from_tree (com_decl);
12756
12757       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
12758       var_die = new_die (DW_TAG_common_block, context_die, decl);
12759       add_name_and_src_coords_attributes (var_die, field);
12760       add_AT_flag (var_die, DW_AT_external, 1);
12761       add_AT_loc (var_die, DW_AT_location, loc);
12762       com_die = new_die (DW_TAG_member, var_die, decl);
12763       add_name_and_src_coords_attributes (com_die, decl);
12764       add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
12765                           TREE_THIS_VOLATILE (decl), context_die);
12766       add_AT_loc (com_die, DW_AT_data_member_location,
12767                   int_loc_descriptor (off));
12768       add_pubname_string (cnam, var_die); /* ??? needed? */
12769       return;
12770     }
12771
12772   var_die = new_die (DW_TAG_variable, context_die, decl);
12773
12774   if (origin != NULL)
12775     add_abstract_origin_attribute (var_die, origin);
12776
12777   /* Loop unrolling can create multiple blocks that refer to the same
12778      static variable, so we must test for the DW_AT_declaration flag.
12779
12780      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12781      copy decls and set the DECL_ABSTRACT flag on them instead of
12782      sharing them.
12783
12784      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12785
12786      ??? The declare_in_namespace support causes us to get two DIEs for one
12787      variable, both of which are declarations.  We want to avoid considering
12788      one to be a specification, so we must test that this DIE is not a
12789      declaration.  */
12790   else if (old_die && TREE_STATIC (decl) && ! declaration
12791            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12792     {
12793       /* This is a definition of a C++ class level static.  */
12794       add_AT_specification (var_die, old_die);
12795       if (DECL_NAME (decl))
12796         {
12797           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12798           struct dwarf_file_data * file_index = lookup_filename (s.file);
12799
12800           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12801             add_AT_file (var_die, DW_AT_decl_file, file_index);
12802
12803           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12804             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12805         }
12806     }
12807   else
12808     {
12809       tree type = TREE_TYPE (decl);
12810       if ((TREE_CODE (decl) == PARM_DECL
12811            || TREE_CODE (decl) == RESULT_DECL)
12812           && DECL_BY_REFERENCE (decl))
12813         type = TREE_TYPE (type);
12814
12815       add_name_and_src_coords_attributes (var_die, decl);
12816       add_type_attribute (var_die, type, TREE_READONLY (decl),
12817                           TREE_THIS_VOLATILE (decl), context_die);
12818
12819       if (TREE_PUBLIC (decl))
12820         add_AT_flag (var_die, DW_AT_external, 1);
12821
12822       if (DECL_ARTIFICIAL (decl))
12823         add_AT_flag (var_die, DW_AT_artificial, 1);
12824
12825       if (TREE_PROTECTED (decl))
12826         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12827       else if (TREE_PRIVATE (decl))
12828         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12829     }
12830
12831   if (declaration)
12832     add_AT_flag (var_die, DW_AT_declaration, 1);
12833
12834   if (DECL_ABSTRACT (decl) || declaration)
12835     equate_decl_number_to_die (decl, var_die);
12836
12837   if (! declaration && ! DECL_ABSTRACT (decl))
12838     {
12839       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12840       add_pubname (decl, var_die);
12841     }
12842   else
12843     tree_add_const_value_attribute (var_die, decl);
12844 }
12845
12846 /* Generate a DIE to represent a label identifier.  */
12847
12848 static void
12849 gen_label_die (tree decl, dw_die_ref context_die)
12850 {
12851   tree origin = decl_ultimate_origin (decl);
12852   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12853   rtx insn;
12854   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12855
12856   if (origin != NULL)
12857     add_abstract_origin_attribute (lbl_die, origin);
12858   else
12859     add_name_and_src_coords_attributes (lbl_die, decl);
12860
12861   if (DECL_ABSTRACT (decl))
12862     equate_decl_number_to_die (decl, lbl_die);
12863   else
12864     {
12865       insn = DECL_RTL_IF_SET (decl);
12866
12867       /* Deleted labels are programmer specified labels which have been
12868          eliminated because of various optimizations.  We still emit them
12869          here so that it is possible to put breakpoints on them.  */
12870       if (insn
12871           && (LABEL_P (insn)
12872               || ((NOTE_P (insn)
12873                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12874         {
12875           /* When optimization is enabled (via -O) some parts of the compiler
12876              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12877              represent source-level labels which were explicitly declared by
12878              the user.  This really shouldn't be happening though, so catch
12879              it if it ever does happen.  */
12880           gcc_assert (!INSN_DELETED_P (insn));
12881
12882           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12883           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12884         }
12885     }
12886 }
12887
12888 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12889    attributes to the DIE for a block STMT, to describe where the inlined
12890    function was called from.  This is similar to add_src_coords_attributes.  */
12891
12892 static inline void
12893 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12894 {
12895   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12896
12897   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12898   add_AT_unsigned (die, DW_AT_call_line, s.line);
12899 }
12900
12901
12902 /* If STMT's abstract origin is a function declaration and STMT's
12903    first subblock's abstract origin is the function's outermost block,
12904    then we're looking at the main entry point.  */
12905 static bool
12906 is_inlined_entry_point (const_tree stmt)
12907 {
12908   tree decl, block;
12909
12910   if (!stmt || TREE_CODE (stmt) != BLOCK)
12911     return false;
12912
12913   decl = block_ultimate_origin (stmt);
12914
12915   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12916     return false;
12917
12918   block = BLOCK_SUBBLOCKS (stmt);
12919
12920   if (block)
12921     {
12922       if (TREE_CODE (block) != BLOCK)
12923         return false;
12924
12925       block = block_ultimate_origin (block);
12926     }
12927
12928   return block == DECL_INITIAL (decl);
12929 }
12930
12931 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12932    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12933
12934 static inline void
12935 add_high_low_attributes (tree stmt, dw_die_ref die)
12936 {
12937   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12938
12939   if (BLOCK_FRAGMENT_CHAIN (stmt))
12940     {
12941       tree chain;
12942
12943       if (is_inlined_entry_point (stmt))
12944         {
12945           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12946                                        BLOCK_NUMBER (stmt));
12947           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12948         }
12949
12950       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12951
12952       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12953       do
12954         {
12955           add_ranges (chain);
12956           chain = BLOCK_FRAGMENT_CHAIN (chain);
12957         }
12958       while (chain);
12959       add_ranges (NULL);
12960     }
12961   else
12962     {
12963       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12964                                    BLOCK_NUMBER (stmt));
12965       add_AT_lbl_id (die, DW_AT_low_pc, label);
12966       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12967                                    BLOCK_NUMBER (stmt));
12968       add_AT_lbl_id (die, DW_AT_high_pc, label);
12969     }
12970 }
12971
12972 /* Generate a DIE for a lexical block.  */
12973
12974 static void
12975 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12976 {
12977   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12978
12979   if (! BLOCK_ABSTRACT (stmt))
12980     add_high_low_attributes (stmt, stmt_die);
12981
12982   decls_for_scope (stmt, stmt_die, depth);
12983 }
12984
12985 /* Generate a DIE for an inlined subprogram.  */
12986
12987 static void
12988 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12989 {
12990   tree decl = block_ultimate_origin (stmt);
12991
12992   /* Emit info for the abstract instance first, if we haven't yet.  We
12993      must emit this even if the block is abstract, otherwise when we
12994      emit the block below (or elsewhere), we may end up trying to emit
12995      a die whose origin die hasn't been emitted, and crashing.  */
12996   dwarf2out_abstract_function (decl);
12997
12998   if (! BLOCK_ABSTRACT (stmt))
12999     {
13000       dw_die_ref subr_die
13001         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13002
13003       add_abstract_origin_attribute (subr_die, decl);
13004       add_high_low_attributes (stmt, subr_die);
13005       add_call_src_coords_attributes (stmt, subr_die);
13006
13007       decls_for_scope (stmt, subr_die, depth);
13008       current_function_has_inlines = 1;
13009     }
13010   else
13011     /* We may get here if we're the outer block of function A that was
13012        inlined into function B that was inlined into function C.  When
13013        generating debugging info for C, dwarf2out_abstract_function(B)
13014        would mark all inlined blocks as abstract, including this one.
13015        So, we wouldn't (and shouldn't) expect labels to be generated
13016        for this one.  Instead, just emit debugging info for
13017        declarations within the block.  This is particularly important
13018        in the case of initializers of arguments passed from B to us:
13019        if they're statement expressions containing declarations, we
13020        wouldn't generate dies for their abstract variables, and then,
13021        when generating dies for the real variables, we'd die (pun
13022        intended :-)  */
13023     gen_lexical_block_die (stmt, context_die, depth);
13024 }
13025
13026 /* Generate a DIE for a field in a record, or structure.  */
13027
13028 static void
13029 gen_field_die (tree decl, dw_die_ref context_die)
13030 {
13031   dw_die_ref decl_die;
13032
13033   if (TREE_TYPE (decl) == error_mark_node)
13034     return;
13035
13036   decl_die = new_die (DW_TAG_member, context_die, decl);
13037   add_name_and_src_coords_attributes (decl_die, decl);
13038   add_type_attribute (decl_die, member_declared_type (decl),
13039                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13040                       context_die);
13041
13042   if (DECL_BIT_FIELD_TYPE (decl))
13043     {
13044       add_byte_size_attribute (decl_die, decl);
13045       add_bit_size_attribute (decl_die, decl);
13046       add_bit_offset_attribute (decl_die, decl);
13047     }
13048
13049   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13050     add_data_member_location_attribute (decl_die, decl);
13051
13052   if (DECL_ARTIFICIAL (decl))
13053     add_AT_flag (decl_die, DW_AT_artificial, 1);
13054
13055   if (TREE_PROTECTED (decl))
13056     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13057   else if (TREE_PRIVATE (decl))
13058     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13059
13060   /* Equate decl number to die, so that we can look up this decl later on.  */
13061   equate_decl_number_to_die (decl, decl_die);
13062 }
13063
13064 #if 0
13065 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13066    Use modified_type_die instead.
13067    We keep this code here just in case these types of DIEs may be needed to
13068    represent certain things in other languages (e.g. Pascal) someday.  */
13069
13070 static void
13071 gen_pointer_type_die (tree type, dw_die_ref context_die)
13072 {
13073   dw_die_ref ptr_die
13074     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13075
13076   equate_type_number_to_die (type, ptr_die);
13077   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13078   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13079 }
13080
13081 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13082    Use modified_type_die instead.
13083    We keep this code here just in case these types of DIEs may be needed to
13084    represent certain things in other languages (e.g. Pascal) someday.  */
13085
13086 static void
13087 gen_reference_type_die (tree type, dw_die_ref context_die)
13088 {
13089   dw_die_ref ref_die
13090     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13091
13092   equate_type_number_to_die (type, ref_die);
13093   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13094   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13095 }
13096 #endif
13097
13098 /* Generate a DIE for a pointer to a member type.  */
13099
13100 static void
13101 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13102 {
13103   dw_die_ref ptr_die
13104     = new_die (DW_TAG_ptr_to_member_type,
13105                scope_die_for (type, context_die), type);
13106
13107   equate_type_number_to_die (type, ptr_die);
13108   add_AT_die_ref (ptr_die, DW_AT_containing_type,
13109                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13110   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13111 }
13112
13113 /* Generate the DIE for the compilation unit.  */
13114
13115 static dw_die_ref
13116 gen_compile_unit_die (const char *filename)
13117 {
13118   dw_die_ref die;
13119   char producer[250];
13120   const char *language_string = lang_hooks.name;
13121   int language;
13122
13123   die = new_die (DW_TAG_compile_unit, NULL, NULL);
13124
13125   if (filename)
13126     {
13127       add_name_attribute (die, filename);
13128       /* Don't add cwd for <built-in>.  */
13129       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13130         add_comp_dir_attribute (die);
13131     }
13132
13133   sprintf (producer, "%s %s", language_string, version_string);
13134
13135 #ifdef MIPS_DEBUGGING_INFO
13136   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13137      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13138      not appear in the producer string, the debugger reaches the conclusion
13139      that the object file is stripped and has no debugging information.
13140      To get the MIPS/SGI debugger to believe that there is debugging
13141      information in the object file, we add a -g to the producer string.  */
13142   if (debug_info_level > DINFO_LEVEL_TERSE)
13143     strcat (producer, " -g");
13144 #endif
13145
13146   add_AT_string (die, DW_AT_producer, producer);
13147
13148   if (strcmp (language_string, "GNU C++") == 0)
13149     language = DW_LANG_C_plus_plus;
13150   else if (strcmp (language_string, "GNU Ada") == 0)
13151     language = DW_LANG_Ada95;
13152   else if (strcmp (language_string, "GNU F77") == 0)
13153     language = DW_LANG_Fortran77;
13154   else if (strcmp (language_string, "GNU Fortran") == 0)
13155     language = DW_LANG_Fortran95;
13156   else if (strcmp (language_string, "GNU Pascal") == 0)
13157     language = DW_LANG_Pascal83;
13158   else if (strcmp (language_string, "GNU Java") == 0)
13159     language = DW_LANG_Java;
13160   else if (strcmp (language_string, "GNU Objective-C") == 0)
13161     language = DW_LANG_ObjC;
13162   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13163     language = DW_LANG_ObjC_plus_plus;
13164   else
13165     language = DW_LANG_C89;
13166
13167   add_AT_unsigned (die, DW_AT_language, language);
13168   return die;
13169 }
13170
13171 /* Generate the DIE for a base class.  */
13172
13173 static void
13174 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13175 {
13176   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13177
13178   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13179   add_data_member_location_attribute (die, binfo);
13180
13181   if (BINFO_VIRTUAL_P (binfo))
13182     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13183
13184   if (access == access_public_node)
13185     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13186   else if (access == access_protected_node)
13187     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13188 }
13189
13190 /* Generate a DIE for a class member.  */
13191
13192 static void
13193 gen_member_die (tree type, dw_die_ref context_die)
13194 {
13195   tree member;
13196   tree binfo = TYPE_BINFO (type);
13197   dw_die_ref child;
13198
13199   /* If this is not an incomplete type, output descriptions of each of its
13200      members. Note that as we output the DIEs necessary to represent the
13201      members of this record or union type, we will also be trying to output
13202      DIEs to represent the *types* of those members. However the `type'
13203      function (above) will specifically avoid generating type DIEs for member
13204      types *within* the list of member DIEs for this (containing) type except
13205      for those types (of members) which are explicitly marked as also being
13206      members of this (containing) type themselves.  The g++ front- end can
13207      force any given type to be treated as a member of some other (containing)
13208      type by setting the TYPE_CONTEXT of the given (member) type to point to
13209      the TREE node representing the appropriate (containing) type.  */
13210
13211   /* First output info about the base classes.  */
13212   if (binfo)
13213     {
13214       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13215       int i;
13216       tree base;
13217
13218       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13219         gen_inheritance_die (base,
13220                              (accesses ? VEC_index (tree, accesses, i)
13221                               : access_public_node), context_die);
13222     }
13223
13224   /* Now output info about the data members and type members.  */
13225   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13226     {
13227       /* If we thought we were generating minimal debug info for TYPE
13228          and then changed our minds, some of the member declarations
13229          may have already been defined.  Don't define them again, but
13230          do put them in the right order.  */
13231
13232       child = lookup_decl_die (member);
13233       if (child)
13234         splice_child_die (context_die, child);
13235       else
13236         gen_decl_die (member, context_die);
13237     }
13238
13239   /* Now output info about the function members (if any).  */
13240   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13241     {
13242       /* Don't include clones in the member list.  */
13243       if (DECL_ABSTRACT_ORIGIN (member))
13244         continue;
13245
13246       child = lookup_decl_die (member);
13247       if (child)
13248         splice_child_die (context_die, child);
13249       else
13250         gen_decl_die (member, context_die);
13251     }
13252 }
13253
13254 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13255    is set, we pretend that the type was never defined, so we only get the
13256    member DIEs needed by later specification DIEs.  */
13257
13258 static void
13259 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13260                                 enum debug_info_usage usage)
13261 {
13262   dw_die_ref type_die = lookup_type_die (type);
13263   dw_die_ref scope_die = 0;
13264   int nested = 0;
13265   int complete = (TYPE_SIZE (type)
13266                   && (! TYPE_STUB_DECL (type)
13267                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13268   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13269   complete = complete && should_emit_struct_debug (type, usage);
13270
13271   if (type_die && ! complete)
13272     return;
13273
13274   if (TYPE_CONTEXT (type) != NULL_TREE
13275       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13276           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13277     nested = 1;
13278
13279   scope_die = scope_die_for (type, context_die);
13280
13281   if (! type_die || (nested && scope_die == comp_unit_die))
13282     /* First occurrence of type or toplevel definition of nested class.  */
13283     {
13284       dw_die_ref old_die = type_die;
13285
13286       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13287                           ? record_type_tag (type) : DW_TAG_union_type,
13288                           scope_die, type);
13289       equate_type_number_to_die (type, type_die);
13290       if (old_die)
13291         add_AT_specification (type_die, old_die);
13292       else
13293         add_name_attribute (type_die, type_tag (type));
13294     }
13295   else
13296     remove_AT (type_die, DW_AT_declaration);
13297
13298   /* If this type has been completed, then give it a byte_size attribute and
13299      then give a list of members.  */
13300   if (complete && !ns_decl)
13301     {
13302       /* Prevent infinite recursion in cases where the type of some member of
13303          this type is expressed in terms of this type itself.  */
13304       TREE_ASM_WRITTEN (type) = 1;
13305       add_byte_size_attribute (type_die, type);
13306       if (TYPE_STUB_DECL (type) != NULL_TREE)
13307         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13308
13309       /* If the first reference to this type was as the return type of an
13310          inline function, then it may not have a parent.  Fix this now.  */
13311       if (type_die->die_parent == NULL)
13312         add_child_die (scope_die, type_die);
13313
13314       push_decl_scope (type);
13315       gen_member_die (type, type_die);
13316       pop_decl_scope ();
13317
13318       /* GNU extension: Record what type our vtable lives in.  */
13319       if (TYPE_VFIELD (type))
13320         {
13321           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13322
13323           gen_type_die (vtype, context_die);
13324           add_AT_die_ref (type_die, DW_AT_containing_type,
13325                           lookup_type_die (vtype));
13326         }
13327     }
13328   else
13329     {
13330       add_AT_flag (type_die, DW_AT_declaration, 1);
13331
13332       /* We don't need to do this for function-local types.  */
13333       if (TYPE_STUB_DECL (type)
13334           && ! decl_function_context (TYPE_STUB_DECL (type)))
13335         VEC_safe_push (tree, gc, incomplete_types, type);
13336     }
13337
13338   if (get_AT (type_die, DW_AT_name))
13339     add_pubtype (type, type_die);
13340 }
13341
13342 /* Generate a DIE for a subroutine _type_.  */
13343
13344 static void
13345 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13346 {
13347   tree return_type = TREE_TYPE (type);
13348   dw_die_ref subr_die
13349     = new_die (DW_TAG_subroutine_type,
13350                scope_die_for (type, context_die), type);
13351
13352   equate_type_number_to_die (type, subr_die);
13353   add_prototyped_attribute (subr_die, type);
13354   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13355   gen_formal_types_die (type, subr_die);
13356
13357   if (get_AT (subr_die, DW_AT_name))
13358     add_pubtype (type, subr_die);
13359 }
13360
13361 /* Generate a DIE for a type definition.  */
13362
13363 static void
13364 gen_typedef_die (tree decl, dw_die_ref context_die)
13365 {
13366   dw_die_ref type_die;
13367   tree origin;
13368
13369   if (TREE_ASM_WRITTEN (decl))
13370     return;
13371
13372   TREE_ASM_WRITTEN (decl) = 1;
13373   type_die = new_die (DW_TAG_typedef, context_die, decl);
13374   origin = decl_ultimate_origin (decl);
13375   if (origin != NULL)
13376     add_abstract_origin_attribute (type_die, origin);
13377   else
13378     {
13379       tree type;
13380
13381       add_name_and_src_coords_attributes (type_die, decl);
13382       if (DECL_ORIGINAL_TYPE (decl))
13383         {
13384           type = DECL_ORIGINAL_TYPE (decl);
13385
13386           gcc_assert (type != TREE_TYPE (decl));
13387           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13388         }
13389       else
13390         type = TREE_TYPE (decl);
13391
13392       add_type_attribute (type_die, type, TREE_READONLY (decl),
13393                           TREE_THIS_VOLATILE (decl), context_die);
13394     }
13395
13396   if (DECL_ABSTRACT (decl))
13397     equate_decl_number_to_die (decl, type_die);
13398
13399   if (get_AT (type_die, DW_AT_name))
13400     add_pubtype (decl, type_die);
13401 }
13402
13403 /* Generate a type description DIE.  */
13404
13405 static void
13406 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13407                                 enum debug_info_usage usage)
13408 {
13409   int need_pop;
13410   struct array_descr_info info;
13411
13412   if (type == NULL_TREE || type == error_mark_node)
13413     return;
13414
13415   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13416       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13417     {
13418       if (TREE_ASM_WRITTEN (type))
13419         return;
13420
13421       /* Prevent broken recursion; we can't hand off to the same type.  */
13422       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13423
13424       TREE_ASM_WRITTEN (type) = 1;
13425       gen_decl_die (TYPE_NAME (type), context_die);
13426       return;
13427     }
13428
13429   /* If this is an array type with hidden descriptor, handle it first.  */
13430   if (!TREE_ASM_WRITTEN (type)
13431       && lang_hooks.types.get_array_descr_info
13432       && lang_hooks.types.get_array_descr_info (type, &info))
13433     {
13434       gen_descr_array_type_die (type, &info, context_die);
13435       TREE_ASM_WRITTEN (type) = 1;
13436       return;
13437     }
13438
13439   /* We are going to output a DIE to represent the unqualified version
13440      of this type (i.e. without any const or volatile qualifiers) so
13441      get the main variant (i.e. the unqualified version) of this type
13442      now.  (Vectors are special because the debugging info is in the
13443      cloned type itself).  */
13444   if (TREE_CODE (type) != VECTOR_TYPE)
13445     type = type_main_variant (type);
13446
13447   if (TREE_ASM_WRITTEN (type))
13448     return;
13449
13450   switch (TREE_CODE (type))
13451     {
13452     case ERROR_MARK:
13453       break;
13454
13455     case POINTER_TYPE:
13456     case REFERENCE_TYPE:
13457       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13458          ensures that the gen_type_die recursion will terminate even if the
13459          type is recursive.  Recursive types are possible in Ada.  */
13460       /* ??? We could perhaps do this for all types before the switch
13461          statement.  */
13462       TREE_ASM_WRITTEN (type) = 1;
13463
13464       /* For these types, all that is required is that we output a DIE (or a
13465          set of DIEs) to represent the "basis" type.  */
13466       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13467                                 DINFO_USAGE_IND_USE);
13468       break;
13469
13470     case OFFSET_TYPE:
13471       /* This code is used for C++ pointer-to-data-member types.
13472          Output a description of the relevant class type.  */
13473       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13474                                         DINFO_USAGE_IND_USE);
13475
13476       /* Output a description of the type of the object pointed to.  */
13477       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13478                                         DINFO_USAGE_IND_USE);
13479
13480       /* Now output a DIE to represent this pointer-to-data-member type
13481          itself.  */
13482       gen_ptr_to_mbr_type_die (type, context_die);
13483       break;
13484
13485     case FUNCTION_TYPE:
13486       /* Force out return type (in case it wasn't forced out already).  */
13487       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13488                                         DINFO_USAGE_DIR_USE);
13489       gen_subroutine_type_die (type, context_die);
13490       break;
13491
13492     case METHOD_TYPE:
13493       /* Force out return type (in case it wasn't forced out already).  */
13494       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13495                                         DINFO_USAGE_DIR_USE);
13496       gen_subroutine_type_die (type, context_die);
13497       break;
13498
13499     case ARRAY_TYPE:
13500       gen_array_type_die (type, context_die);
13501       break;
13502
13503     case VECTOR_TYPE:
13504       gen_array_type_die (type, context_die);
13505       break;
13506
13507     case ENUMERAL_TYPE:
13508     case RECORD_TYPE:
13509     case UNION_TYPE:
13510     case QUAL_UNION_TYPE:
13511       /* If this is a nested type whose containing class hasn't been written
13512          out yet, writing it out will cover this one, too.  This does not apply
13513          to instantiations of member class templates; they need to be added to
13514          the containing class as they are generated.  FIXME: This hurts the
13515          idea of combining type decls from multiple TUs, since we can't predict
13516          what set of template instantiations we'll get.  */
13517       if (TYPE_CONTEXT (type)
13518           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13519           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13520         {
13521           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13522
13523           if (TREE_ASM_WRITTEN (type))
13524             return;
13525
13526           /* If that failed, attach ourselves to the stub.  */
13527           push_decl_scope (TYPE_CONTEXT (type));
13528           context_die = lookup_type_die (TYPE_CONTEXT (type));
13529           need_pop = 1;
13530         }
13531       else
13532         {
13533           declare_in_namespace (type, context_die);
13534           need_pop = 0;
13535         }
13536
13537       if (TREE_CODE (type) == ENUMERAL_TYPE)
13538         {
13539           /* This might have been written out by the call to
13540              declare_in_namespace.  */
13541           if (!TREE_ASM_WRITTEN (type))
13542             gen_enumeration_type_die (type, context_die);
13543         }
13544       else
13545         gen_struct_or_union_type_die (type, context_die, usage);
13546
13547       if (need_pop)
13548         pop_decl_scope ();
13549
13550       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13551          it up if it is ever completed.  gen_*_type_die will set it for us
13552          when appropriate.  */
13553       return;
13554
13555     case VOID_TYPE:
13556     case INTEGER_TYPE:
13557     case REAL_TYPE:
13558     case FIXED_POINT_TYPE:
13559     case COMPLEX_TYPE:
13560     case BOOLEAN_TYPE:
13561       /* No DIEs needed for fundamental types.  */
13562       break;
13563
13564     case LANG_TYPE:
13565       /* No Dwarf representation currently defined.  */
13566       break;
13567
13568     default:
13569       gcc_unreachable ();
13570     }
13571
13572   TREE_ASM_WRITTEN (type) = 1;
13573 }
13574
13575 static void
13576 gen_type_die (tree type, dw_die_ref context_die)
13577 {
13578   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13579 }
13580
13581 /* Generate a DIE for a tagged type instantiation.  */
13582
13583 static void
13584 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13585 {
13586   if (type == NULL_TREE || type == error_mark_node)
13587     return;
13588
13589   /* We are going to output a DIE to represent the unqualified version of
13590      this type (i.e. without any const or volatile qualifiers) so make sure
13591      that we have the main variant (i.e. the unqualified version) of this
13592      type now.  */
13593   gcc_assert (type == type_main_variant (type));
13594
13595   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13596      an instance of an unresolved type.  */
13597
13598   switch (TREE_CODE (type))
13599     {
13600     case ERROR_MARK:
13601       break;
13602
13603     case ENUMERAL_TYPE:
13604       gen_inlined_enumeration_type_die (type, context_die);
13605       break;
13606
13607     case RECORD_TYPE:
13608       gen_inlined_structure_type_die (type, context_die);
13609       break;
13610
13611     case UNION_TYPE:
13612     case QUAL_UNION_TYPE:
13613       gen_inlined_union_type_die (type, context_die);
13614       break;
13615
13616     default:
13617       gcc_unreachable ();
13618     }
13619 }
13620
13621 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13622    things which are local to the given block.  */
13623
13624 static void
13625 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13626 {
13627   int must_output_die = 0;
13628   tree origin;
13629   tree decl;
13630   enum tree_code origin_code;
13631
13632   /* Ignore blocks that are NULL.  */
13633   if (stmt == NULL_TREE)
13634     return;
13635
13636   /* If the block is one fragment of a non-contiguous block, do not
13637      process the variables, since they will have been done by the
13638      origin block.  Do process subblocks.  */
13639   if (BLOCK_FRAGMENT_ORIGIN (stmt))
13640     {
13641       tree sub;
13642
13643       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13644         gen_block_die (sub, context_die, depth + 1);
13645
13646       return;
13647     }
13648
13649   /* Determine the "ultimate origin" of this block.  This block may be an
13650      inlined instance of an inlined instance of inline function, so we have
13651      to trace all of the way back through the origin chain to find out what
13652      sort of node actually served as the original seed for the creation of
13653      the current block.  */
13654   origin = block_ultimate_origin (stmt);
13655   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13656
13657   /* Determine if we need to output any Dwarf DIEs at all to represent this
13658      block.  */
13659   if (origin_code == FUNCTION_DECL)
13660     /* The outer scopes for inlinings *must* always be represented.  We
13661        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13662     must_output_die = 1;
13663   else
13664     {
13665       /* In the case where the current block represents an inlining of the
13666          "body block" of an inline function, we must *NOT* output any DIE for
13667          this block because we have already output a DIE to represent the whole
13668          inlined function scope and the "body block" of any function doesn't
13669          really represent a different scope according to ANSI C rules.  So we
13670          check here to make sure that this block does not represent a "body
13671          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13672       if (! is_body_block (origin ? origin : stmt))
13673         {
13674           /* Determine if this block directly contains any "significant"
13675              local declarations which we will need to output DIEs for.  */
13676           if (debug_info_level > DINFO_LEVEL_TERSE)
13677             /* We are not in terse mode so *any* local declaration counts
13678                as being a "significant" one.  */
13679             must_output_die = (BLOCK_VARS (stmt) != NULL
13680                                && (TREE_USED (stmt)
13681                                    || TREE_ASM_WRITTEN (stmt)
13682                                    || BLOCK_ABSTRACT (stmt)));
13683           else
13684             /* We are in terse mode, so only local (nested) function
13685                definitions count as "significant" local declarations.  */
13686             for (decl = BLOCK_VARS (stmt);
13687                  decl != NULL; decl = TREE_CHAIN (decl))
13688               if (TREE_CODE (decl) == FUNCTION_DECL
13689                   && DECL_INITIAL (decl))
13690                 {
13691                   must_output_die = 1;
13692                   break;
13693                 }
13694         }
13695     }
13696
13697   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13698      DIE for any block which contains no significant local declarations at
13699      all.  Rather, in such cases we just call `decls_for_scope' so that any
13700      needed Dwarf info for any sub-blocks will get properly generated. Note
13701      that in terse mode, our definition of what constitutes a "significant"
13702      local declaration gets restricted to include only inlined function
13703      instances and local (nested) function definitions.  */
13704   if (must_output_die)
13705     {
13706       if (origin_code == FUNCTION_DECL)
13707         gen_inlined_subroutine_die (stmt, context_die, depth);
13708       else
13709         gen_lexical_block_die (stmt, context_die, depth);
13710     }
13711   else
13712     decls_for_scope (stmt, context_die, depth);
13713 }
13714
13715 /* Generate all of the decls declared within a given scope and (recursively)
13716    all of its sub-blocks.  */
13717
13718 static void
13719 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13720 {
13721   tree decl;
13722   tree subblocks;
13723
13724   /* Ignore NULL blocks.  */
13725   if (stmt == NULL_TREE)
13726     return;
13727
13728   if (TREE_USED (stmt))
13729     {
13730       /* Output the DIEs to represent all of the data objects and typedefs
13731          declared directly within this block but not within any nested
13732          sub-blocks.  Also, nested function and tag DIEs have been
13733          generated with a parent of NULL; fix that up now.  */
13734       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13735         {
13736           dw_die_ref die;
13737
13738           if (TREE_CODE (decl) == FUNCTION_DECL)
13739             die = lookup_decl_die (decl);
13740           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13741             die = lookup_type_die (TREE_TYPE (decl));
13742           else
13743             die = NULL;
13744
13745           if (die != NULL && die->die_parent == NULL)
13746             add_child_die (context_die, die);
13747           /* Do not produce debug information for static variables since
13748              these might be optimized out.  We are called for these later
13749              in varpool_analyze_pending_decls.
13750
13751              But *do* produce it for Fortran COMMON variables because,
13752              even though they are static, their names can differ depending
13753              on the scope, which we need to preserve.  */
13754           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
13755               && !(is_fortran () && TREE_PUBLIC (decl)))
13756             ;
13757           else
13758             gen_decl_die (decl, context_die);
13759         }
13760     }
13761
13762   /* If we're at -g1, we're not interested in subblocks.  */
13763   if (debug_info_level <= DINFO_LEVEL_TERSE)
13764     return;
13765
13766   /* Output the DIEs to represent all sub-blocks (and the items declared
13767      therein) of this block.  */
13768   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13769        subblocks != NULL;
13770        subblocks = BLOCK_CHAIN (subblocks))
13771     gen_block_die (subblocks, context_die, depth + 1);
13772 }
13773
13774 /* Is this a typedef we can avoid emitting?  */
13775
13776 static inline int
13777 is_redundant_typedef (const_tree decl)
13778 {
13779   if (TYPE_DECL_IS_STUB (decl))
13780     return 1;
13781
13782   if (DECL_ARTIFICIAL (decl)
13783       && DECL_CONTEXT (decl)
13784       && is_tagged_type (DECL_CONTEXT (decl))
13785       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13786       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13787     /* Also ignore the artificial member typedef for the class name.  */
13788     return 1;
13789
13790   return 0;
13791 }
13792
13793 /* Returns the DIE for decl.  A DIE will always be returned.  */
13794
13795 static dw_die_ref
13796 force_decl_die (tree decl)
13797 {
13798   dw_die_ref decl_die;
13799   unsigned saved_external_flag;
13800   tree save_fn = NULL_TREE;
13801   decl_die = lookup_decl_die (decl);
13802   if (!decl_die)
13803     {
13804       dw_die_ref context_die;
13805       tree decl_context = DECL_CONTEXT (decl);
13806       if (decl_context)
13807         {
13808           /* Find die that represents this context.  */
13809           if (TYPE_P (decl_context))
13810             context_die = force_type_die (decl_context);
13811           else
13812             context_die = force_decl_die (decl_context);
13813         }
13814       else
13815         context_die = comp_unit_die;
13816
13817       decl_die = lookup_decl_die (decl);
13818       if (decl_die)
13819         return decl_die;
13820
13821       switch (TREE_CODE (decl))
13822         {
13823         case FUNCTION_DECL:
13824           /* Clear current_function_decl, so that gen_subprogram_die thinks
13825              that this is a declaration. At this point, we just want to force
13826              declaration die.  */
13827           save_fn = current_function_decl;
13828           current_function_decl = NULL_TREE;
13829           gen_subprogram_die (decl, context_die);
13830           current_function_decl = save_fn;
13831           break;
13832
13833         case VAR_DECL:
13834           /* Set external flag to force declaration die. Restore it after
13835            gen_decl_die() call.  */
13836           saved_external_flag = DECL_EXTERNAL (decl);
13837           DECL_EXTERNAL (decl) = 1;
13838           gen_decl_die (decl, context_die);
13839           DECL_EXTERNAL (decl) = saved_external_flag;
13840           break;
13841
13842         case NAMESPACE_DECL:
13843           dwarf2out_decl (decl);
13844           break;
13845
13846         default:
13847           gcc_unreachable ();
13848         }
13849
13850       /* We should be able to find the DIE now.  */
13851       if (!decl_die)
13852         decl_die = lookup_decl_die (decl);
13853       gcc_assert (decl_die);
13854     }
13855
13856   return decl_die;
13857 }
13858
13859 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13860    always returned.  */
13861
13862 static dw_die_ref
13863 force_type_die (tree type)
13864 {
13865   dw_die_ref type_die;
13866
13867   type_die = lookup_type_die (type);
13868   if (!type_die)
13869     {
13870       dw_die_ref context_die;
13871       if (TYPE_CONTEXT (type))
13872         {
13873           if (TYPE_P (TYPE_CONTEXT (type)))
13874             context_die = force_type_die (TYPE_CONTEXT (type));
13875           else
13876             context_die = force_decl_die (TYPE_CONTEXT (type));
13877         }
13878       else
13879         context_die = comp_unit_die;
13880
13881       type_die = modified_type_die (type, TYPE_READONLY (type),
13882                                     TYPE_VOLATILE (type), context_die);
13883       gcc_assert (type_die);
13884     }
13885   return type_die;
13886 }
13887
13888 /* Force out any required namespaces to be able to output DECL,
13889    and return the new context_die for it, if it's changed.  */
13890
13891 static dw_die_ref
13892 setup_namespace_context (tree thing, dw_die_ref context_die)
13893 {
13894   tree context = (DECL_P (thing)
13895                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13896   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13897     /* Force out the namespace.  */
13898     context_die = force_decl_die (context);
13899
13900   return context_die;
13901 }
13902
13903 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13904    type) within its namespace, if appropriate.
13905
13906    For compatibility with older debuggers, namespace DIEs only contain
13907    declarations; all definitions are emitted at CU scope.  */
13908
13909 static void
13910 declare_in_namespace (tree thing, dw_die_ref context_die)
13911 {
13912   dw_die_ref ns_context;
13913
13914   if (debug_info_level <= DINFO_LEVEL_TERSE)
13915     return;
13916
13917   /* If this decl is from an inlined function, then don't try to emit it in its
13918      namespace, as we will get confused.  It would have already been emitted
13919      when the abstract instance of the inline function was emitted anyways.  */
13920   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13921     return;
13922
13923   ns_context = setup_namespace_context (thing, context_die);
13924
13925   if (ns_context != context_die)
13926     {
13927       if (DECL_P (thing))
13928         gen_decl_die (thing, ns_context);
13929       else
13930         gen_type_die (thing, ns_context);
13931     }
13932 }
13933
13934 /* Generate a DIE for a namespace or namespace alias.  */
13935
13936 static void
13937 gen_namespace_die (tree decl)
13938 {
13939   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13940
13941   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13942      they are an alias of.  */
13943   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13944     {
13945       /* Output a real namespace.  */
13946       dw_die_ref namespace_die
13947         = new_die (DW_TAG_namespace, context_die, decl);
13948       add_name_and_src_coords_attributes (namespace_die, decl);
13949       equate_decl_number_to_die (decl, namespace_die);
13950     }
13951   else
13952     {
13953       /* Output a namespace alias.  */
13954
13955       /* Force out the namespace we are an alias of, if necessary.  */
13956       dw_die_ref origin_die
13957         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13958
13959       /* Now create the namespace alias DIE.  */
13960       dw_die_ref namespace_die
13961         = new_die (DW_TAG_imported_declaration, context_die, decl);
13962       add_name_and_src_coords_attributes (namespace_die, decl);
13963       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13964       equate_decl_number_to_die (decl, namespace_die);
13965     }
13966 }
13967
13968 /* Generate Dwarf debug information for a decl described by DECL.  */
13969
13970 static void
13971 gen_decl_die (tree decl, dw_die_ref context_die)
13972 {
13973   tree origin;
13974
13975   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13976     return;
13977
13978   switch (TREE_CODE (decl))
13979     {
13980     case ERROR_MARK:
13981       break;
13982
13983     case CONST_DECL:
13984       /* The individual enumerators of an enum type get output when we output
13985          the Dwarf representation of the relevant enum type itself.  */
13986       break;
13987
13988     case FUNCTION_DECL:
13989       /* Don't output any DIEs to represent mere function declarations,
13990          unless they are class members or explicit block externs.  */
13991       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13992           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13993         break;
13994
13995 #if 0
13996       /* FIXME */
13997       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13998          on local redeclarations of global functions.  That seems broken.  */
13999       if (current_function_decl != decl)
14000         /* This is only a declaration.  */;
14001 #endif
14002
14003       /* If we're emitting a clone, emit info for the abstract instance.  */
14004       if (DECL_ORIGIN (decl) != decl)
14005         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14006
14007       /* If we're emitting an out-of-line copy of an inline function,
14008          emit info for the abstract instance and set up to refer to it.  */
14009       else if (cgraph_function_possibly_inlined_p (decl)
14010                && ! DECL_ABSTRACT (decl)
14011                && ! class_or_namespace_scope_p (context_die)
14012                /* dwarf2out_abstract_function won't emit a die if this is just
14013                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
14014                   that case, because that works only if we have a die.  */
14015                && DECL_INITIAL (decl) != NULL_TREE)
14016         {
14017           dwarf2out_abstract_function (decl);
14018           set_decl_origin_self (decl);
14019         }
14020
14021       /* Otherwise we're emitting the primary DIE for this decl.  */
14022       else if (debug_info_level > DINFO_LEVEL_TERSE)
14023         {
14024           /* Before we describe the FUNCTION_DECL itself, make sure that we
14025              have described its return type.  */
14026           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14027
14028           /* And its virtual context.  */
14029           if (DECL_VINDEX (decl) != NULL_TREE)
14030             gen_type_die (DECL_CONTEXT (decl), context_die);
14031
14032           /* And its containing type.  */
14033           origin = decl_class_context (decl);
14034           if (origin != NULL_TREE)
14035             gen_type_die_for_member (origin, decl, context_die);
14036
14037           /* And its containing namespace.  */
14038           declare_in_namespace (decl, context_die);
14039         }
14040
14041       /* Now output a DIE to represent the function itself.  */
14042       gen_subprogram_die (decl, context_die);
14043       break;
14044
14045     case TYPE_DECL:
14046       /* If we are in terse mode, don't generate any DIEs to represent any
14047          actual typedefs.  */
14048       if (debug_info_level <= DINFO_LEVEL_TERSE)
14049         break;
14050
14051       /* In the special case of a TYPE_DECL node representing the declaration
14052          of some type tag, if the given TYPE_DECL is marked as having been
14053          instantiated from some other (original) TYPE_DECL node (e.g. one which
14054          was generated within the original definition of an inline function) we
14055          have to generate a special (abbreviated) DW_TAG_structure_type,
14056          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
14057       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14058           && is_tagged_type (TREE_TYPE (decl)))
14059         {
14060           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14061           break;
14062         }
14063
14064       if (is_redundant_typedef (decl))
14065         gen_type_die (TREE_TYPE (decl), context_die);
14066       else
14067         /* Output a DIE to represent the typedef itself.  */
14068         gen_typedef_die (decl, context_die);
14069       break;
14070
14071     case LABEL_DECL:
14072       if (debug_info_level >= DINFO_LEVEL_NORMAL)
14073         gen_label_die (decl, context_die);
14074       break;
14075
14076     case VAR_DECL:
14077     case RESULT_DECL:
14078       /* If we are in terse mode, don't generate any DIEs to represent any
14079          variable declarations or definitions.  */
14080       if (debug_info_level <= DINFO_LEVEL_TERSE)
14081         break;
14082
14083       /* If this is the global definition of the Fortran COMMON block, we don't
14084          need to do anything.  Syntactically, the block itself has no identity,
14085          just its constituent identifiers.  */
14086       if (TREE_CODE (decl) == VAR_DECL
14087           && TREE_PUBLIC (decl)
14088           && TREE_STATIC (decl)
14089           && is_fortran ()
14090           && !DECL_HAS_VALUE_EXPR_P (decl))
14091         break;
14092
14093       /* Output any DIEs that are needed to specify the type of this data
14094          object.  */
14095       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14096         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14097       else
14098         gen_type_die (TREE_TYPE (decl), context_die);
14099
14100       /* And its containing type.  */
14101       origin = decl_class_context (decl);
14102       if (origin != NULL_TREE)
14103         gen_type_die_for_member (origin, decl, context_die);
14104
14105       /* And its containing namespace.  */
14106       declare_in_namespace (decl, context_die);
14107
14108       /* Now output the DIE to represent the data object itself.  This gets
14109          complicated because of the possibility that the VAR_DECL really
14110          represents an inlined instance of a formal parameter for an inline
14111          function.  */
14112       origin = decl_ultimate_origin (decl);
14113       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14114         gen_formal_parameter_die (decl, context_die);
14115       else
14116         gen_variable_die (decl, context_die);
14117       break;
14118
14119     case FIELD_DECL:
14120       /* Ignore the nameless fields that are used to skip bits but handle C++
14121          anonymous unions and structs.  */
14122       if (DECL_NAME (decl) != NULL_TREE
14123           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14124           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14125         {
14126           gen_type_die (member_declared_type (decl), context_die);
14127           gen_field_die (decl, context_die);
14128         }
14129       break;
14130
14131     case PARM_DECL:
14132       if (DECL_BY_REFERENCE (decl))
14133         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14134       else
14135         gen_type_die (TREE_TYPE (decl), context_die);
14136       gen_formal_parameter_die (decl, context_die);
14137       break;
14138
14139     case NAMESPACE_DECL:
14140       gen_namespace_die (decl);
14141       break;
14142
14143     default:
14144       /* Probably some frontend-internal decl.  Assume we don't care.  */
14145       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14146       break;
14147     }
14148 }
14149 \f
14150 /* Output debug information for global decl DECL.  Called from toplev.c after
14151    compilation proper has finished.  */
14152
14153 static void
14154 dwarf2out_global_decl (tree decl)
14155 {
14156   /* Output DWARF2 information for file-scope tentative data object
14157      declarations, file-scope (extern) function declarations (which
14158      had no corresponding body) and file-scope tagged type declarations
14159      and definitions which have not yet been forced out.
14160
14161      Ignore the global decl of any Fortran COMMON blocks which also
14162      wind up here though they have already been described in the local
14163      scope for the procedures using them.  */
14164   if (TREE_CODE (decl) == VAR_DECL
14165       && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14166     return;
14167
14168   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14169     dwarf2out_decl (decl);
14170 }
14171
14172 /* Output debug information for type decl DECL.  Called from toplev.c
14173    and from language front ends (to record built-in types).  */
14174 static void
14175 dwarf2out_type_decl (tree decl, int local)
14176 {
14177   if (!local)
14178     dwarf2out_decl (decl);
14179 }
14180
14181 /* Output debug information for imported module or decl.  */
14182
14183 static void
14184 dwarf2out_imported_module_or_decl (tree decl, tree context)
14185 {
14186   dw_die_ref imported_die, at_import_die;
14187   dw_die_ref scope_die;
14188   expanded_location xloc;
14189
14190   if (debug_info_level <= DINFO_LEVEL_TERSE)
14191     return;
14192
14193   gcc_assert (decl);
14194
14195   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14196      We need decl DIE for reference and scope die. First, get DIE for the decl
14197      itself.  */
14198
14199   /* Get the scope die for decl context. Use comp_unit_die for global module
14200      or decl. If die is not found for non globals, force new die.  */
14201   if (!context)
14202     scope_die = comp_unit_die;
14203   else if (TYPE_P (context))
14204     {
14205       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14206         return;
14207     scope_die = force_type_die (context);
14208     }
14209   else
14210     scope_die = force_decl_die (context);
14211
14212   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14213   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14214     {
14215       if (is_base_type (TREE_TYPE (decl)))
14216         at_import_die = base_type_die (TREE_TYPE (decl));
14217       else
14218         at_import_die = force_type_die (TREE_TYPE (decl));
14219     }
14220   else
14221     {
14222       at_import_die = lookup_decl_die (decl);
14223       if (!at_import_die)
14224         {
14225           /* If we're trying to avoid duplicate debug info, we may not have
14226              emitted the member decl for this field.  Emit it now.  */
14227           if (TREE_CODE (decl) == FIELD_DECL)
14228             {
14229               tree type = DECL_CONTEXT (decl);
14230               dw_die_ref type_context_die;
14231
14232               if (TYPE_CONTEXT (type))
14233                 if (TYPE_P (TYPE_CONTEXT (type)))
14234                   {
14235                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14236                                                    DINFO_USAGE_DIR_USE))
14237                       return;
14238                   type_context_die = force_type_die (TYPE_CONTEXT (type));
14239                   }
14240               else
14241                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14242               else
14243                 type_context_die = comp_unit_die;
14244               gen_type_die_for_member (type, decl, type_context_die);
14245             }
14246           at_import_die = force_decl_die (decl);
14247         }
14248     }
14249
14250   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14251   if (TREE_CODE (decl) == NAMESPACE_DECL)
14252     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14253   else
14254     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14255
14256   xloc = expand_location (input_location);
14257   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14258   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14259   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14260 }
14261
14262 /* Write the debugging output for DECL.  */
14263
14264 void
14265 dwarf2out_decl (tree decl)
14266 {
14267   dw_die_ref context_die = comp_unit_die;
14268
14269   switch (TREE_CODE (decl))
14270     {
14271     case ERROR_MARK:
14272       return;
14273
14274     case FUNCTION_DECL:
14275       /* What we would really like to do here is to filter out all mere
14276          file-scope declarations of file-scope functions which are never
14277          referenced later within this translation unit (and keep all of ones
14278          that *are* referenced later on) but we aren't clairvoyant, so we have
14279          no idea which functions will be referenced in the future (i.e. later
14280          on within the current translation unit). So here we just ignore all
14281          file-scope function declarations which are not also definitions.  If
14282          and when the debugger needs to know something about these functions,
14283          it will have to hunt around and find the DWARF information associated
14284          with the definition of the function.
14285
14286          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14287          nodes represent definitions and which ones represent mere
14288          declarations.  We have to check DECL_INITIAL instead. That's because
14289          the C front-end supports some weird semantics for "extern inline"
14290          function definitions.  These can get inlined within the current
14291          translation unit (and thus, we need to generate Dwarf info for their
14292          abstract instances so that the Dwarf info for the concrete inlined
14293          instances can have something to refer to) but the compiler never
14294          generates any out-of-lines instances of such things (despite the fact
14295          that they *are* definitions).
14296
14297          The important point is that the C front-end marks these "extern
14298          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14299          them anyway. Note that the C++ front-end also plays some similar games
14300          for inline function definitions appearing within include files which
14301          also contain `#pragma interface' pragmas.  */
14302       if (DECL_INITIAL (decl) == NULL_TREE)
14303         return;
14304
14305       /* If we're a nested function, initially use a parent of NULL; if we're
14306          a plain function, this will be fixed up in decls_for_scope.  If
14307          we're a method, it will be ignored, since we already have a DIE.  */
14308       if (decl_function_context (decl)
14309           /* But if we're in terse mode, we don't care about scope.  */
14310           && debug_info_level > DINFO_LEVEL_TERSE)
14311         context_die = NULL;
14312       break;
14313
14314     case VAR_DECL:
14315       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14316          declaration and if the declaration was never even referenced from
14317          within this entire compilation unit.  We suppress these DIEs in
14318          order to save space in the .debug section (by eliminating entries
14319          which are probably useless).  Note that we must not suppress
14320          block-local extern declarations (whether used or not) because that
14321          would screw-up the debugger's name lookup mechanism and cause it to
14322          miss things which really ought to be in scope at a given point.  */
14323       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14324         return;
14325
14326       /* For local statics lookup proper context die.  */
14327       if (TREE_STATIC (decl) && decl_function_context (decl))
14328         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14329
14330       /* If we are in terse mode, don't generate any DIEs to represent any
14331          variable declarations or definitions.  */
14332       if (debug_info_level <= DINFO_LEVEL_TERSE)
14333         return;
14334       break;
14335
14336     case NAMESPACE_DECL:
14337       if (debug_info_level <= DINFO_LEVEL_TERSE)
14338         return;
14339       if (lookup_decl_die (decl) != NULL)
14340         return;
14341       break;
14342
14343     case TYPE_DECL:
14344       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14345       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14346         return;
14347
14348       /* Don't bother trying to generate any DIEs to represent any of the
14349          normal built-in types for the language we are compiling.  */
14350       if (DECL_IS_BUILTIN (decl))
14351         {
14352           /* OK, we need to generate one for `bool' so GDB knows what type
14353              comparisons have.  */
14354           if (is_cxx ()
14355               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14356               && ! DECL_IGNORED_P (decl))
14357             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14358
14359           return;
14360         }
14361
14362       /* If we are in terse mode, don't generate any DIEs for types.  */
14363       if (debug_info_level <= DINFO_LEVEL_TERSE)
14364         return;
14365
14366       /* If we're a function-scope tag, initially use a parent of NULL;
14367          this will be fixed up in decls_for_scope.  */
14368       if (decl_function_context (decl))
14369         context_die = NULL;
14370
14371       break;
14372
14373     default:
14374       return;
14375     }
14376
14377   gen_decl_die (decl, context_die);
14378 }
14379
14380 /* Output a marker (i.e. a label) for the beginning of the generated code for
14381    a lexical block.  */
14382
14383 static void
14384 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14385                        unsigned int blocknum)
14386 {
14387   switch_to_section (current_function_section ());
14388   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14389 }
14390
14391 /* Output a marker (i.e. a label) for the end of the generated code for a
14392    lexical block.  */
14393
14394 static void
14395 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14396 {
14397   switch_to_section (current_function_section ());
14398   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14399 }
14400
14401 /* Returns nonzero if it is appropriate not to emit any debugging
14402    information for BLOCK, because it doesn't contain any instructions.
14403
14404    Don't allow this for blocks with nested functions or local classes
14405    as we would end up with orphans, and in the presence of scheduling
14406    we may end up calling them anyway.  */
14407
14408 static bool
14409 dwarf2out_ignore_block (const_tree block)
14410 {
14411   tree decl;
14412
14413   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14414     if (TREE_CODE (decl) == FUNCTION_DECL
14415         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14416       return 0;
14417
14418   return 1;
14419 }
14420
14421 /* Hash table routines for file_hash.  */
14422
14423 static int
14424 file_table_eq (const void *p1_p, const void *p2_p)
14425 {
14426   const struct dwarf_file_data *const p1 =
14427     (const struct dwarf_file_data *) p1_p;
14428   const char *const p2 = (const char *) p2_p;
14429   return strcmp (p1->filename, p2) == 0;
14430 }
14431
14432 static hashval_t
14433 file_table_hash (const void *p_p)
14434 {
14435   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
14436   return htab_hash_string (p->filename);
14437 }
14438
14439 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14440    dwarf2out.c) and return its "index".  The index of each (known) filename is
14441    just a unique number which is associated with only that one filename.  We
14442    need such numbers for the sake of generating labels (in the .debug_sfnames
14443    section) and references to those files numbers (in the .debug_srcinfo
14444    and.debug_macinfo sections).  If the filename given as an argument is not
14445    found in our current list, add it to the list and assign it the next
14446    available unique index number.  In order to speed up searches, we remember
14447    the index of the filename was looked up last.  This handles the majority of
14448    all searches.  */
14449
14450 static struct dwarf_file_data *
14451 lookup_filename (const char *file_name)
14452 {
14453   void ** slot;
14454   struct dwarf_file_data * created;
14455
14456   /* Check to see if the file name that was searched on the previous
14457      call matches this file name.  If so, return the index.  */
14458   if (file_table_last_lookup
14459       && (file_name == file_table_last_lookup->filename
14460           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14461     return file_table_last_lookup;
14462
14463   /* Didn't match the previous lookup, search the table.  */
14464   slot = htab_find_slot_with_hash (file_table, file_name,
14465                                    htab_hash_string (file_name), INSERT);
14466   if (*slot)
14467     return (struct dwarf_file_data *) *slot;
14468
14469   created = GGC_NEW (struct dwarf_file_data);
14470   created->filename = file_name;
14471   created->emitted_number = 0;
14472   *slot = created;
14473   return created;
14474 }
14475
14476 /* If the assembler will construct the file table, then translate the compiler
14477    internal file table number into the assembler file table number, and emit
14478    a .file directive if we haven't already emitted one yet.  The file table
14479    numbers are different because we prune debug info for unused variables and
14480    types, which may include filenames.  */
14481
14482 static int
14483 maybe_emit_file (struct dwarf_file_data * fd)
14484 {
14485   if (! fd->emitted_number)
14486     {
14487       if (last_emitted_file)
14488         fd->emitted_number = last_emitted_file->emitted_number + 1;
14489       else
14490         fd->emitted_number = 1;
14491       last_emitted_file = fd;
14492
14493       if (DWARF2_ASM_LINE_DEBUG_INFO)
14494         {
14495           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14496           output_quoted_string (asm_out_file,
14497                                 remap_debug_filename (fd->filename));
14498           fputc ('\n', asm_out_file);
14499         }
14500     }
14501
14502   return fd->emitted_number;
14503 }
14504
14505 /* Called by the final INSN scan whenever we see a var location.  We
14506    use it to drop labels in the right places, and throw the location in
14507    our lookup table.  */
14508
14509 static void
14510 dwarf2out_var_location (rtx loc_note)
14511 {
14512   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14513   struct var_loc_node *newloc;
14514   rtx prev_insn;
14515   static rtx last_insn;
14516   static const char *last_label;
14517   tree decl;
14518
14519   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14520     return;
14521   prev_insn = PREV_INSN (loc_note);
14522
14523   newloc = GGC_CNEW (struct var_loc_node);
14524   /* If the insn we processed last time is the previous insn
14525      and it is also a var location note, use the label we emitted
14526      last time.  */
14527   if (last_insn != NULL_RTX
14528       && last_insn == prev_insn
14529       && NOTE_P (prev_insn)
14530       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14531     {
14532       newloc->label = last_label;
14533     }
14534   else
14535     {
14536       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14537       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14538       loclabel_num++;
14539       newloc->label = ggc_strdup (loclabel);
14540     }
14541   newloc->var_loc_note = loc_note;
14542   newloc->next = NULL;
14543
14544   if (cfun && in_cold_section_p)
14545     newloc->section_label = crtl->subsections.cold_section_label;
14546   else
14547     newloc->section_label = text_section_label;
14548
14549   last_insn = loc_note;
14550   last_label = newloc->label;
14551   decl = NOTE_VAR_LOCATION_DECL (loc_note);
14552   add_var_loc_to_decl (decl, newloc);
14553 }
14554
14555 /* We need to reset the locations at the beginning of each
14556    function. We can't do this in the end_function hook, because the
14557    declarations that use the locations won't have been output when
14558    that hook is called.  Also compute have_multiple_function_sections here.  */
14559
14560 static void
14561 dwarf2out_begin_function (tree fun)
14562 {
14563   htab_empty (decl_loc_table);
14564
14565   if (function_section (fun) != text_section)
14566     have_multiple_function_sections = true;
14567
14568   dwarf2out_note_section_used ();
14569 }
14570
14571 /* Output a label to mark the beginning of a source code line entry
14572    and record information relating to this source line, in
14573    'line_info_table' for later output of the .debug_line section.  */
14574
14575 static void
14576 dwarf2out_source_line (unsigned int line, const char *filename)
14577 {
14578   if (debug_info_level >= DINFO_LEVEL_NORMAL
14579       && line != 0)
14580     {
14581       int file_num = maybe_emit_file (lookup_filename (filename));
14582
14583       switch_to_section (current_function_section ());
14584
14585       /* If requested, emit something human-readable.  */
14586       if (flag_debug_asm)
14587         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14588                  filename, line);
14589
14590       if (DWARF2_ASM_LINE_DEBUG_INFO)
14591         {
14592           /* Emit the .loc directive understood by GNU as.  */
14593           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14594
14595           /* Indicate that line number info exists.  */
14596           line_info_table_in_use++;
14597         }
14598       else if (function_section (current_function_decl) != text_section)
14599         {
14600           dw_separate_line_info_ref line_info;
14601           targetm.asm_out.internal_label (asm_out_file,
14602                                           SEPARATE_LINE_CODE_LABEL,
14603                                           separate_line_info_table_in_use);
14604
14605           /* Expand the line info table if necessary.  */
14606           if (separate_line_info_table_in_use
14607               == separate_line_info_table_allocated)
14608             {
14609               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14610               separate_line_info_table
14611                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
14612                                  separate_line_info_table,
14613                                  separate_line_info_table_allocated);
14614               memset (separate_line_info_table
14615                        + separate_line_info_table_in_use,
14616                       0,
14617                       (LINE_INFO_TABLE_INCREMENT
14618                        * sizeof (dw_separate_line_info_entry)));
14619             }
14620
14621           /* Add the new entry at the end of the line_info_table.  */
14622           line_info
14623             = &separate_line_info_table[separate_line_info_table_in_use++];
14624           line_info->dw_file_num = file_num;
14625           line_info->dw_line_num = line;
14626           line_info->function = current_function_funcdef_no;
14627         }
14628       else
14629         {
14630           dw_line_info_ref line_info;
14631
14632           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14633                                      line_info_table_in_use);
14634
14635           /* Expand the line info table if necessary.  */
14636           if (line_info_table_in_use == line_info_table_allocated)
14637             {
14638               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14639               line_info_table
14640                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
14641                                  line_info_table_allocated);
14642               memset (line_info_table + line_info_table_in_use, 0,
14643                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14644             }
14645
14646           /* Add the new entry at the end of the line_info_table.  */
14647           line_info = &line_info_table[line_info_table_in_use++];
14648           line_info->dw_file_num = file_num;
14649           line_info->dw_line_num = line;
14650         }
14651     }
14652 }
14653
14654 /* Record the beginning of a new source file.  */
14655
14656 static void
14657 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14658 {
14659   if (flag_eliminate_dwarf2_dups)
14660     {
14661       /* Record the beginning of the file for break_out_includes.  */
14662       dw_die_ref bincl_die;
14663
14664       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14665       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14666     }
14667
14668   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14669     {
14670       int file_num = maybe_emit_file (lookup_filename (filename));
14671
14672       switch_to_section (debug_macinfo_section);
14673       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14674       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14675                                    lineno);
14676
14677       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14678     }
14679 }
14680
14681 /* Record the end of a source file.  */
14682
14683 static void
14684 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14685 {
14686   if (flag_eliminate_dwarf2_dups)
14687     /* Record the end of the file for break_out_includes.  */
14688     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14689
14690   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14691     {
14692       switch_to_section (debug_macinfo_section);
14693       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14694     }
14695 }
14696
14697 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14698    the tail part of the directive line, i.e. the part which is past the
14699    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14700
14701 static void
14702 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14703                   const char *buffer ATTRIBUTE_UNUSED)
14704 {
14705   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14706     {
14707       switch_to_section (debug_macinfo_section);
14708       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14709       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14710       dw2_asm_output_nstring (buffer, -1, "The macro");
14711     }
14712 }
14713
14714 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14715    the tail part of the directive line, i.e. the part which is past the
14716    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14717
14718 static void
14719 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14720                  const char *buffer ATTRIBUTE_UNUSED)
14721 {
14722   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14723     {
14724       switch_to_section (debug_macinfo_section);
14725       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14726       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14727       dw2_asm_output_nstring (buffer, -1, "The macro");
14728     }
14729 }
14730
14731 /* Set up for Dwarf output at the start of compilation.  */
14732
14733 static void
14734 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14735 {
14736   /* Allocate the file_table.  */
14737   file_table = htab_create_ggc (50, file_table_hash,
14738                                 file_table_eq, NULL);
14739
14740   /* Allocate the decl_die_table.  */
14741   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14742                                     decl_die_table_eq, NULL);
14743
14744   /* Allocate the decl_loc_table.  */
14745   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14746                                     decl_loc_table_eq, NULL);
14747
14748   /* Allocate the initial hunk of the decl_scope_table.  */
14749   decl_scope_table = VEC_alloc (tree, gc, 256);
14750
14751   /* Allocate the initial hunk of the abbrev_die_table.  */
14752   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
14753   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14754   /* Zero-th entry is allocated, but unused.  */
14755   abbrev_die_table_in_use = 1;
14756
14757   /* Allocate the initial hunk of the line_info_table.  */
14758   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
14759   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14760
14761   /* Zero-th entry is allocated, but unused.  */
14762   line_info_table_in_use = 1;
14763
14764   /* Allocate the pubtypes and pubnames vectors.  */
14765   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14766   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14767
14768   /* Generate the initial DIE for the .debug section.  Note that the (string)
14769      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14770      will (typically) be a relative pathname and that this pathname should be
14771      taken as being relative to the directory from which the compiler was
14772      invoked when the given (base) source file was compiled.  We will fill
14773      in this value in dwarf2out_finish.  */
14774   comp_unit_die = gen_compile_unit_die (NULL);
14775
14776   incomplete_types = VEC_alloc (tree, gc, 64);
14777
14778   used_rtx_array = VEC_alloc (rtx, gc, 32);
14779
14780   debug_info_section = get_section (DEBUG_INFO_SECTION,
14781                                     SECTION_DEBUG, NULL);
14782   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14783                                       SECTION_DEBUG, NULL);
14784   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14785                                        SECTION_DEBUG, NULL);
14786   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14787                                        SECTION_DEBUG, NULL);
14788   debug_line_section = get_section (DEBUG_LINE_SECTION,
14789                                     SECTION_DEBUG, NULL);
14790   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14791                                    SECTION_DEBUG, NULL);
14792   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14793                                         SECTION_DEBUG, NULL);
14794 #ifdef DEBUG_PUBTYPES_SECTION
14795   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14796                                         SECTION_DEBUG, NULL);
14797 #endif
14798   debug_str_section = get_section (DEBUG_STR_SECTION,
14799                                    DEBUG_STR_SECTION_FLAGS, NULL);
14800   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14801                                       SECTION_DEBUG, NULL);
14802   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14803                                      SECTION_DEBUG, NULL);
14804
14805   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14806   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14807                                DEBUG_ABBREV_SECTION_LABEL, 0);
14808   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14809   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14810                                COLD_TEXT_SECTION_LABEL, 0);
14811   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14812
14813   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14814                                DEBUG_INFO_SECTION_LABEL, 0);
14815   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14816                                DEBUG_LINE_SECTION_LABEL, 0);
14817   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14818                                DEBUG_RANGES_SECTION_LABEL, 0);
14819   switch_to_section (debug_abbrev_section);
14820   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14821   switch_to_section (debug_info_section);
14822   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14823   switch_to_section (debug_line_section);
14824   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14825
14826   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14827     {
14828       switch_to_section (debug_macinfo_section);
14829       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14830                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14831       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14832     }
14833
14834   switch_to_section (text_section);
14835   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14836   if (flag_reorder_blocks_and_partition)
14837     {
14838       cold_text_section = unlikely_text_section ();
14839       switch_to_section (cold_text_section);
14840       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14841     }
14842 }
14843
14844 /* A helper function for dwarf2out_finish called through
14845    ht_forall.  Emit one queued .debug_str string.  */
14846
14847 static int
14848 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14849 {
14850   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14851
14852   if (node->form == DW_FORM_strp)
14853     {
14854       switch_to_section (debug_str_section);
14855       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14856       assemble_string (node->str, strlen (node->str) + 1);
14857     }
14858
14859   return 1;
14860 }
14861
14862 #if ENABLE_ASSERT_CHECKING
14863 /* Verify that all marks are clear.  */
14864
14865 static void
14866 verify_marks_clear (dw_die_ref die)
14867 {
14868   dw_die_ref c;
14869
14870   gcc_assert (! die->die_mark);
14871   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14872 }
14873 #endif /* ENABLE_ASSERT_CHECKING */
14874
14875 /* Clear the marks for a die and its children.
14876    Be cool if the mark isn't set.  */
14877
14878 static void
14879 prune_unmark_dies (dw_die_ref die)
14880 {
14881   dw_die_ref c;
14882
14883   if (die->die_mark)
14884     die->die_mark = 0;
14885   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14886 }
14887
14888 /* Given DIE that we're marking as used, find any other dies
14889    it references as attributes and mark them as used.  */
14890
14891 static void
14892 prune_unused_types_walk_attribs (dw_die_ref die)
14893 {
14894   dw_attr_ref a;
14895   unsigned ix;
14896
14897   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14898     {
14899       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14900         {
14901           /* A reference to another DIE.
14902              Make sure that it will get emitted.  */
14903           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14904         }
14905       /* Set the string's refcount to 0 so that prune_unused_types_mark
14906          accounts properly for it.  */
14907       if (AT_class (a) == dw_val_class_str)
14908         a->dw_attr_val.v.val_str->refcount = 0;
14909     }
14910 }
14911
14912
14913 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14914    to DIE's children.  */
14915
14916 static void
14917 prune_unused_types_mark (dw_die_ref die, int dokids)
14918 {
14919   dw_die_ref c;
14920
14921   if (die->die_mark == 0)
14922     {
14923       /* We haven't done this node yet.  Mark it as used.  */
14924       die->die_mark = 1;
14925
14926       /* We also have to mark its parents as used.
14927          (But we don't want to mark our parents' kids due to this.)  */
14928       if (die->die_parent)
14929         prune_unused_types_mark (die->die_parent, 0);
14930
14931       /* Mark any referenced nodes.  */
14932       prune_unused_types_walk_attribs (die);
14933
14934       /* If this node is a specification,
14935          also mark the definition, if it exists.  */
14936       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14937         prune_unused_types_mark (die->die_definition, 1);
14938     }
14939
14940   if (dokids && die->die_mark != 2)
14941     {
14942       /* We need to walk the children, but haven't done so yet.
14943          Remember that we've walked the kids.  */
14944       die->die_mark = 2;
14945
14946       /* If this is an array type, we need to make sure our
14947          kids get marked, even if they're types.  */
14948       if (die->die_tag == DW_TAG_array_type)
14949         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14950       else
14951         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14952     }
14953 }
14954
14955
14956 /* Walk the tree DIE and mark types that we actually use.  */
14957
14958 static void
14959 prune_unused_types_walk (dw_die_ref die)
14960 {
14961   dw_die_ref c;
14962
14963   /* Don't do anything if this node is already marked.  */
14964   if (die->die_mark)
14965     return;
14966
14967   switch (die->die_tag)
14968     {
14969     case DW_TAG_const_type:
14970     case DW_TAG_packed_type:
14971     case DW_TAG_pointer_type:
14972     case DW_TAG_reference_type:
14973     case DW_TAG_volatile_type:
14974     case DW_TAG_typedef:
14975     case DW_TAG_array_type:
14976     case DW_TAG_structure_type:
14977     case DW_TAG_union_type:
14978     case DW_TAG_class_type:
14979     case DW_TAG_interface_type:
14980     case DW_TAG_friend:
14981     case DW_TAG_variant_part:
14982     case DW_TAG_enumeration_type:
14983     case DW_TAG_subroutine_type:
14984     case DW_TAG_string_type:
14985     case DW_TAG_set_type:
14986     case DW_TAG_subrange_type:
14987     case DW_TAG_ptr_to_member_type:
14988     case DW_TAG_file_type:
14989       if (die->die_perennial_p)
14990         break;
14991
14992       /* It's a type node --- don't mark it.  */
14993       return;
14994
14995     default:
14996       /* Mark everything else.  */
14997       break;
14998   }
14999
15000   die->die_mark = 1;
15001
15002   /* Now, mark any dies referenced from here.  */
15003   prune_unused_types_walk_attribs (die);
15004
15005   /* Mark children.  */
15006   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15007 }
15008
15009 /* Increment the string counts on strings referred to from DIE's
15010    attributes.  */
15011
15012 static void
15013 prune_unused_types_update_strings (dw_die_ref die)
15014 {
15015   dw_attr_ref a;
15016   unsigned ix;
15017
15018   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15019     if (AT_class (a) == dw_val_class_str)
15020       {
15021         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15022         s->refcount++;
15023         /* Avoid unnecessarily putting strings that are used less than
15024            twice in the hash table.  */
15025         if (s->refcount
15026             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15027           {
15028             void ** slot;
15029             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15030                                              htab_hash_string (s->str),
15031                                              INSERT);
15032             gcc_assert (*slot == NULL);
15033             *slot = s;
15034           }
15035       }
15036 }
15037
15038 /* Remove from the tree DIE any dies that aren't marked.  */
15039
15040 static void
15041 prune_unused_types_prune (dw_die_ref die)
15042 {
15043   dw_die_ref c;
15044
15045   gcc_assert (die->die_mark);
15046   prune_unused_types_update_strings (die);
15047
15048   if (! die->die_child)
15049     return;
15050
15051   c = die->die_child;
15052   do {
15053     dw_die_ref prev = c;
15054     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15055       if (c == die->die_child)
15056         {
15057           /* No marked children between 'prev' and the end of the list.  */
15058           if (prev == c)
15059             /* No marked children at all.  */
15060             die->die_child = NULL;
15061           else
15062             {
15063               prev->die_sib = c->die_sib;
15064               die->die_child = prev;
15065             }
15066           return;
15067         }
15068
15069     if (c != prev->die_sib)
15070       prev->die_sib = c;
15071     prune_unused_types_prune (c);
15072   } while (c != die->die_child);
15073 }
15074
15075
15076 /* Remove dies representing declarations that we never use.  */
15077
15078 static void
15079 prune_unused_types (void)
15080 {
15081   unsigned int i;
15082   limbo_die_node *node;
15083   pubname_ref pub;
15084
15085 #if ENABLE_ASSERT_CHECKING
15086   /* All the marks should already be clear.  */
15087   verify_marks_clear (comp_unit_die);
15088   for (node = limbo_die_list; node; node = node->next)
15089     verify_marks_clear (node->die);
15090 #endif /* ENABLE_ASSERT_CHECKING */
15091
15092   /* Set the mark on nodes that are actually used.  */
15093   prune_unused_types_walk (comp_unit_die);
15094   for (node = limbo_die_list; node; node = node->next)
15095     prune_unused_types_walk (node->die);
15096
15097   /* Also set the mark on nodes referenced from the
15098      pubname_table or arange_table.  */
15099   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15100     prune_unused_types_mark (pub->die, 1);
15101   for (i = 0; i < arange_table_in_use; i++)
15102     prune_unused_types_mark (arange_table[i], 1);
15103
15104   /* Get rid of nodes that aren't marked; and update the string counts.  */
15105   if (debug_str_hash)
15106     htab_empty (debug_str_hash);
15107   prune_unused_types_prune (comp_unit_die);
15108   for (node = limbo_die_list; node; node = node->next)
15109     prune_unused_types_prune (node->die);
15110
15111   /* Leave the marks clear.  */
15112   prune_unmark_dies (comp_unit_die);
15113   for (node = limbo_die_list; node; node = node->next)
15114     prune_unmark_dies (node->die);
15115 }
15116
15117 /* Set the parameter to true if there are any relative pathnames in
15118    the file table.  */
15119 static int
15120 file_table_relative_p (void ** slot, void *param)
15121 {
15122   bool *p = (bool *) param;
15123   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
15124   if (!IS_ABSOLUTE_PATH (d->filename))
15125     {
15126       *p = true;
15127       return 0;
15128     }
15129   return 1;
15130 }
15131
15132 /* Output stuff that dwarf requires at the end of every file,
15133    and generate the DWARF-2 debugging info.  */
15134
15135 static void
15136 dwarf2out_finish (const char *filename)
15137 {
15138   limbo_die_node *node, *next_node;
15139   dw_die_ref die = 0;
15140
15141   /* Add the name for the main input file now.  We delayed this from
15142      dwarf2out_init to avoid complications with PCH.  */
15143   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15144   if (!IS_ABSOLUTE_PATH (filename))
15145     add_comp_dir_attribute (comp_unit_die);
15146   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15147     {
15148       bool p = false;
15149       htab_traverse (file_table, file_table_relative_p, &p);
15150       if (p)
15151         add_comp_dir_attribute (comp_unit_die);
15152     }
15153
15154   /* Traverse the limbo die list, and add parent/child links.  The only
15155      dies without parents that should be here are concrete instances of
15156      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15157      For concrete instances, we can get the parent die from the abstract
15158      instance.  */
15159   for (node = limbo_die_list; node; node = next_node)
15160     {
15161       next_node = node->next;
15162       die = node->die;
15163
15164       if (die->die_parent == NULL)
15165         {
15166           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15167
15168           if (origin)
15169             add_child_die (origin->die_parent, die);
15170           else if (die == comp_unit_die)
15171             ;
15172           else if (errorcount > 0 || sorrycount > 0)
15173             /* It's OK to be confused by errors in the input.  */
15174             add_child_die (comp_unit_die, die);
15175           else
15176             {
15177               /* In certain situations, the lexical block containing a
15178                  nested function can be optimized away, which results
15179                  in the nested function die being orphaned.  Likewise
15180                  with the return type of that nested function.  Force
15181                  this to be a child of the containing function.
15182
15183                  It may happen that even the containing function got fully
15184                  inlined and optimized out.  In that case we are lost and
15185                  assign the empty child.  This should not be big issue as
15186                  the function is likely unreachable too.  */
15187               tree context = NULL_TREE;
15188
15189               gcc_assert (node->created_for);
15190
15191               if (DECL_P (node->created_for))
15192                 context = DECL_CONTEXT (node->created_for);
15193               else if (TYPE_P (node->created_for))
15194                 context = TYPE_CONTEXT (node->created_for);
15195
15196               gcc_assert (context
15197                           && (TREE_CODE (context) == FUNCTION_DECL
15198                               || TREE_CODE (context) == NAMESPACE_DECL));
15199
15200               origin = lookup_decl_die (context);
15201               if (origin)
15202                 add_child_die (origin, die);
15203               else
15204                 add_child_die (comp_unit_die, die);
15205             }
15206         }
15207     }
15208
15209   limbo_die_list = NULL;
15210
15211   /* Walk through the list of incomplete types again, trying once more to
15212      emit full debugging info for them.  */
15213   retry_incomplete_types ();
15214
15215   if (flag_eliminate_unused_debug_types)
15216     prune_unused_types ();
15217
15218   /* Generate separate CUs for each of the include files we've seen.
15219      They will go into limbo_die_list.  */
15220   if (flag_eliminate_dwarf2_dups)
15221     break_out_includes (comp_unit_die);
15222
15223   /* Traverse the DIE's and add add sibling attributes to those DIE's
15224      that have children.  */
15225   add_sibling_attributes (comp_unit_die);
15226   for (node = limbo_die_list; node; node = node->next)
15227     add_sibling_attributes (node->die);
15228
15229   /* Output a terminator label for the .text section.  */
15230   switch_to_section (text_section);
15231   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15232   if (flag_reorder_blocks_and_partition)
15233     {
15234       switch_to_section (unlikely_text_section ());
15235       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15236     }
15237
15238   /* We can only use the low/high_pc attributes if all of the code was
15239      in .text.  */
15240   if (!have_multiple_function_sections)
15241     {
15242       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15243       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15244     }
15245
15246   else
15247     {
15248       unsigned fde_idx = 0;
15249
15250       /* We need to give .debug_loc and .debug_ranges an appropriate
15251          "base address".  Use zero so that these addresses become
15252          absolute.  Historically, we've emitted the unexpected
15253          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15254          Emit both to give time for other tools to adapt.  */
15255       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15256       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15257
15258       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15259                          add_ranges_by_labels (text_section_label,
15260                                                text_end_label));
15261       if (flag_reorder_blocks_and_partition)
15262         add_ranges_by_labels (cold_text_section_label,
15263                               cold_end_label);
15264
15265       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15266         {
15267           dw_fde_ref fde = &fde_table[fde_idx];
15268
15269           if (fde->dw_fde_switched_sections)
15270             {
15271               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15272                                     fde->dw_fde_hot_section_end_label);
15273               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15274                                     fde->dw_fde_unlikely_section_end_label);
15275             }
15276           else
15277             add_ranges_by_labels (fde->dw_fde_begin,
15278                                   fde->dw_fde_end);
15279         }
15280
15281       add_ranges (NULL);
15282     }
15283
15284   /* Output location list section if necessary.  */
15285   if (have_location_lists)
15286     {
15287       /* Output the location lists info.  */
15288       switch_to_section (debug_loc_section);
15289       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15290                                    DEBUG_LOC_SECTION_LABEL, 0);
15291       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15292       output_location_lists (die);
15293     }
15294
15295   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15296     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15297                     debug_line_section_label);
15298
15299   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15300     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15301
15302   /* Output all of the compilation units.  We put the main one last so that
15303      the offsets are available to output_pubnames.  */
15304   for (node = limbo_die_list; node; node = node->next)
15305     output_comp_unit (node->die, 0);
15306
15307   output_comp_unit (comp_unit_die, 0);
15308
15309   /* Output the abbreviation table.  */
15310   switch_to_section (debug_abbrev_section);
15311   output_abbrev_section ();
15312
15313   /* Output public names table if necessary.  */
15314   if (!VEC_empty (pubname_entry, pubname_table))
15315     {
15316       switch_to_section (debug_pubnames_section);
15317       output_pubnames (pubname_table);
15318     }
15319
15320 #ifdef DEBUG_PUBTYPES_SECTION
15321   /* Output public types table if necessary.  */
15322   if (!VEC_empty (pubname_entry, pubtype_table))
15323     {
15324       switch_to_section (debug_pubtypes_section);
15325       output_pubnames (pubtype_table);
15326     }
15327 #endif
15328
15329   /* Output the address range information.  We only put functions in the arange
15330      table, so don't write it out if we don't have any.  */
15331   if (fde_table_in_use)
15332     {
15333       switch_to_section (debug_aranges_section);
15334       output_aranges ();
15335     }
15336
15337   /* Output ranges section if necessary.  */
15338   if (ranges_table_in_use)
15339     {
15340       switch_to_section (debug_ranges_section);
15341       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15342       output_ranges ();
15343     }
15344
15345   /* Output the source line correspondence table.  We must do this
15346      even if there is no line information.  Otherwise, on an empty
15347      translation unit, we will generate a present, but empty,
15348      .debug_info section.  IRIX 6.5 `nm' will then complain when
15349      examining the file.  This is done late so that any filenames
15350      used by the debug_info section are marked as 'used'.  */
15351   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15352     {
15353       switch_to_section (debug_line_section);
15354       output_line_info ();
15355     }
15356
15357   /* Have to end the macro section.  */
15358   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15359     {
15360       switch_to_section (debug_macinfo_section);
15361       dw2_asm_output_data (1, 0, "End compilation unit");
15362     }
15363
15364   /* If we emitted any DW_FORM_strp form attribute, output the string
15365      table too.  */
15366   if (debug_str_hash)
15367     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15368 }
15369 #else
15370
15371 /* This should never be used, but its address is needed for comparisons.  */
15372 const struct gcc_debug_hooks dwarf2_debug_hooks;
15373
15374 #endif /* DWARF2_DEBUGGING_INFO */
15375
15376 #include "gt-dwarf2out.h"