OSDN Git Service

2007-03-28 Tobias Schlter <tobi@gcc.gnu.org>
[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 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 2, 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 COPYING.  If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "version.h"
43 #include "flags.h"
44 #include "real.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "reload.h"
50 #include "function.h"
51 #include "output.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "dwarf2.h"
56 #include "dwarf2out.h"
57 #include "dwarf2asm.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "ggc.h"
61 #include "md5.h"
62 #include "tm_p.h"
63 #include "diagnostic.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "hashtab.h"
68 #include "cgraph.h"
69 #include "input.h"
70
71 #ifdef DWARF2_DEBUGGING_INFO
72 static void dwarf2out_source_line (unsigned int, const char *);
73 #endif
74
75 /* DWARF2 Abbreviation Glossary:
76    CFA = Canonical Frame Address
77            a fixed address on the stack which identifies a call frame.
78            We define it to be the value of SP just before the call insn.
79            The CFA register and offset, which may change during the course
80            of the function, are used to calculate its value at runtime.
81    CFI = Call Frame Instruction
82            an instruction for the DWARF2 abstract machine
83    CIE = Common Information Entry
84            information describing information common to one or more FDEs
85    DIE = Debugging Information Entry
86    FDE = Frame Description Entry
87            information describing the stack call frame, in particular,
88            how to restore registers
89
90    DW_CFA_... = DWARF2 CFA call frame instruction
91    DW_TAG_... = DWARF2 DIE tag */
92
93 #ifndef DWARF2_FRAME_INFO
94 # ifdef DWARF2_DEBUGGING_INFO
95 #  define DWARF2_FRAME_INFO \
96   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
97 # else
98 #  define DWARF2_FRAME_INFO 0
99 # endif
100 #endif
101
102 /* Map register numbers held in the call frame info that gcc has
103    collected using DWARF_FRAME_REGNUM to those that should be output in
104    .debug_frame and .eh_frame.  */
105 #ifndef DWARF2_FRAME_REG_OUT
106 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
107 #endif
108
109 /* Decide whether we want to emit frame unwind information for the current
110    translation unit.  */
111
112 int
113 dwarf2out_do_frame (void)
114 {
115   /* We want to emit correct CFA location expressions or lists, so we
116      have to return true if we're going to output debug info, even if
117      we're not going to output frame or unwind info.  */
118   return (write_symbols == DWARF2_DEBUG
119           || write_symbols == VMS_AND_DWARF2_DEBUG
120           || DWARF2_FRAME_INFO
121 #ifdef DWARF2_UNWIND_INFO
122           || (DWARF2_UNWIND_INFO
123               && (flag_unwind_tables
124                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
125 #endif
126           );
127 }
128
129 /* The size of the target's pointer type.  */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
133
134 /* Array of RTXes referenced by the debugging information, which therefore
135    must be kept around forever.  */
136 static GTY(()) VEC(rtx,gc) *used_rtx_array;
137
138 /* A pointer to the base of a list of incomplete types which might be
139    completed at some later time.  incomplete_types_list needs to be a
140    VEC(tree,gc) because we want to tell the garbage collector about
141    it.  */
142 static GTY(()) VEC(tree,gc) *incomplete_types;
143
144 /* A pointer to the base of a table of references to declaration
145    scopes.  This table is a display which tracks the nesting
146    of declaration scopes at the current scope and containing
147    scopes.  This table is used to find the proper place to
148    define type declaration DIE's.  */
149 static GTY(()) VEC(tree,gc) *decl_scope_table;
150
151 /* Pointers to various DWARF2 sections.  */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_abbrev_section;
154 static GTY(()) section *debug_aranges_section;
155 static GTY(()) section *debug_macinfo_section;
156 static GTY(()) section *debug_line_section;
157 static GTY(()) section *debug_loc_section;
158 static GTY(()) section *debug_pubnames_section;
159 static GTY(()) section *debug_pubtypes_section;
160 static GTY(()) section *debug_str_section;
161 static GTY(()) section *debug_ranges_section;
162 static GTY(()) section *debug_frame_section;
163
164 /* How to start an assembler comment.  */
165 #ifndef ASM_COMMENT_START
166 #define ASM_COMMENT_START ";#"
167 #endif
168
169 typedef struct dw_cfi_struct *dw_cfi_ref;
170 typedef struct dw_fde_struct *dw_fde_ref;
171 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
172
173 /* Call frames are described using a sequence of Call Frame
174    Information instructions.  The register number, offset
175    and address fields are provided as possible operands;
176    their use is selected by the opcode field.  */
177
178 enum dw_cfi_oprnd_type {
179   dw_cfi_oprnd_unused,
180   dw_cfi_oprnd_reg_num,
181   dw_cfi_oprnd_offset,
182   dw_cfi_oprnd_addr,
183   dw_cfi_oprnd_loc
184 };
185
186 typedef union dw_cfi_oprnd_struct GTY(())
187 {
188   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
189   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
190   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
191   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
192 }
193 dw_cfi_oprnd;
194
195 typedef struct dw_cfi_struct GTY(())
196 {
197   dw_cfi_ref dw_cfi_next;
198   enum dwarf_call_frame_info dw_cfi_opc;
199   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
200     dw_cfi_oprnd1;
201   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
202     dw_cfi_oprnd2;
203 }
204 dw_cfi_node;
205
206 /* This is how we define the location of the CFA. We use to handle it
207    as REG + OFFSET all the time,  but now it can be more complex.
208    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
209    Instead of passing around REG and OFFSET, we pass a copy
210    of this structure.  */
211 typedef struct cfa_loc GTY(())
212 {
213   HOST_WIDE_INT offset;
214   HOST_WIDE_INT base_offset;
215   unsigned int reg;
216   int indirect;            /* 1 if CFA is accessed via a dereference.  */
217 } dw_cfa_location;
218
219 /* All call frame descriptions (FDE's) in the GCC generated DWARF
220    refer to a single Common Information Entry (CIE), defined at
221    the beginning of the .debug_frame section.  This use of a single
222    CIE obviates the need to keep track of multiple CIE's
223    in the DWARF generation routines below.  */
224
225 typedef struct dw_fde_struct GTY(())
226 {
227   tree decl;
228   const char *dw_fde_begin;
229   const char *dw_fde_current_label;
230   const char *dw_fde_end;
231   const char *dw_fde_hot_section_label;
232   const char *dw_fde_hot_section_end_label;
233   const char *dw_fde_unlikely_section_label;
234   const char *dw_fde_unlikely_section_end_label;
235   bool dw_fde_switched_sections;
236   dw_cfi_ref dw_fde_cfi;
237   unsigned funcdef_number;
238   unsigned all_throwers_are_sibcalls : 1;
239   unsigned nothrow : 1;
240   unsigned uses_eh_lsda : 1;
241 }
242 dw_fde_node;
243
244 /* Maximum size (in bytes) of an artificially generated label.  */
245 #define MAX_ARTIFICIAL_LABEL_BYTES      30
246
247 /* The size of addresses as they appear in the Dwarf 2 data.
248    Some architectures use word addresses to refer to code locations,
249    but Dwarf 2 info always uses byte addresses.  On such machines,
250    Dwarf 2 addresses need to be larger than the architecture's
251    pointers.  */
252 #ifndef DWARF2_ADDR_SIZE
253 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
254 #endif
255
256 /* The size in bytes of a DWARF field indicating an offset or length
257    relative to a debug info section, specified to be 4 bytes in the
258    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
259    as PTR_SIZE.  */
260
261 #ifndef DWARF_OFFSET_SIZE
262 #define DWARF_OFFSET_SIZE 4
263 #endif
264
265 /* According to the (draft) DWARF 3 specification, the initial length
266    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
267    bytes are 0xffffffff, followed by the length stored in the next 8
268    bytes.
269
270    However, the SGI/MIPS ABI uses an initial length which is equal to
271    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
272
273 #ifndef DWARF_INITIAL_LENGTH_SIZE
274 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
275 #endif
276
277 #define DWARF_VERSION 2
278
279 /* Round SIZE up to the nearest BOUNDARY.  */
280 #define DWARF_ROUND(SIZE,BOUNDARY) \
281   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
282
283 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
284 #ifndef DWARF_CIE_DATA_ALIGNMENT
285 #ifdef STACK_GROWS_DOWNWARD
286 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
287 #else
288 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
289 #endif
290 #endif
291
292 /* CIE identifier.  */
293 #if HOST_BITS_PER_WIDE_INT >= 64
294 #define DWARF_CIE_ID \
295   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
296 #else
297 #define DWARF_CIE_ID DW_CIE_ID
298 #endif
299
300 /* A pointer to the base of a table that contains frame description
301    information for each routine.  */
302 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
303
304 /* Number of elements currently allocated for fde_table.  */
305 static GTY(()) unsigned fde_table_allocated;
306
307 /* Number of elements in fde_table currently in use.  */
308 static GTY(()) unsigned fde_table_in_use;
309
310 /* Size (in elements) of increments by which we may expand the
311    fde_table.  */
312 #define FDE_TABLE_INCREMENT 256
313
314 /* A list of call frame insns for the CIE.  */
315 static GTY(()) dw_cfi_ref cie_cfi_head;
316
317 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
318 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
319    attribute that accelerates the lookup of the FDE associated
320    with the subprogram.  This variable holds the table index of the FDE
321    associated with the current function (body) definition.  */
322 static unsigned current_funcdef_fde;
323 #endif
324
325 struct indirect_string_node GTY(())
326 {
327   const char *str;
328   unsigned int refcount;
329   unsigned int form;
330   char *label;
331 };
332
333 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
334
335 static GTY(()) int dw2_string_counter;
336 static GTY(()) unsigned long dwarf2out_cfi_label_num;
337
338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339
340 /* Forward declarations for functions defined in this file.  */
341
342 static char *stripattributes (const char *);
343 static const char *dwarf_cfi_name (unsigned);
344 static dw_cfi_ref new_cfi (void);
345 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
346 static void add_fde_cfi (const char *, dw_cfi_ref);
347 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
348 static void lookup_cfa (dw_cfa_location *);
349 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
350 static void initial_return_save (rtx);
351 static HOST_WIDE_INT stack_adjust_offset (rtx);
352 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
353 static void output_call_frame_info (int);
354 static void dwarf2out_stack_adjust (rtx, bool);
355 static void flush_queued_reg_saves (void);
356 static bool clobbers_queued_reg_save (rtx);
357 static void dwarf2out_frame_debug_expr (rtx, const char *);
358
359 /* Support for complex CFA locations.  */
360 static void output_cfa_loc (dw_cfi_ref);
361 static void get_cfa_from_loc_descr (dw_cfa_location *,
362                                     struct dw_loc_descr_struct *);
363 static struct dw_loc_descr_struct *build_cfa_loc
364   (dw_cfa_location *, HOST_WIDE_INT);
365 static void def_cfa_1 (const char *, dw_cfa_location *);
366
367 /* How to start an assembler comment.  */
368 #ifndef ASM_COMMENT_START
369 #define ASM_COMMENT_START ";#"
370 #endif
371
372 /* Data and reference forms for relocatable data.  */
373 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
374 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
375
376 #ifndef DEBUG_FRAME_SECTION
377 #define DEBUG_FRAME_SECTION     ".debug_frame"
378 #endif
379
380 #ifndef FUNC_BEGIN_LABEL
381 #define FUNC_BEGIN_LABEL        "LFB"
382 #endif
383
384 #ifndef FUNC_END_LABEL
385 #define FUNC_END_LABEL          "LFE"
386 #endif
387
388 #ifndef FRAME_BEGIN_LABEL
389 #define FRAME_BEGIN_LABEL       "Lframe"
390 #endif
391 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
392 #define CIE_END_LABEL           "LECIE"
393 #define FDE_LABEL               "LSFDE"
394 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
395 #define FDE_END_LABEL           "LEFDE"
396 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
397 #define LINE_NUMBER_END_LABEL   "LELT"
398 #define LN_PROLOG_AS_LABEL      "LASLTP"
399 #define LN_PROLOG_END_LABEL     "LELTP"
400 #define DIE_LABEL_PREFIX        "DW"
401
402 /* The DWARF 2 CFA column which tracks the return address.  Normally this
403    is the column for PC, or the first column after all of the hard
404    registers.  */
405 #ifndef DWARF_FRAME_RETURN_COLUMN
406 #ifdef PC_REGNUM
407 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
408 #else
409 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
410 #endif
411 #endif
412
413 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
414    default, we just provide columns for all registers.  */
415 #ifndef DWARF_FRAME_REGNUM
416 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
417 #endif
418 \f
419 /* Hook used by __throw.  */
420
421 rtx
422 expand_builtin_dwarf_sp_column (void)
423 {
424   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
425   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
426 }
427
428 /* Return a pointer to a copy of the section string name S with all
429    attributes stripped off, and an asterisk prepended (for assemble_name).  */
430
431 static inline char *
432 stripattributes (const char *s)
433 {
434   char *stripped = XNEWVEC (char, strlen (s) + 2);
435   char *p = stripped;
436
437   *p++ = '*';
438
439   while (*s && *s != ',')
440     *p++ = *s++;
441
442   *p = '\0';
443   return stripped;
444 }
445
446 /* MEM is a memory reference for the register size table, each element of
447    which has mode MODE.  Initialize column C as a return address column.  */
448
449 static void
450 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
451 {
452   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
453   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
454   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
455 }
456
457 /* Generate code to initialize the register size table.  */
458
459 void
460 expand_builtin_init_dwarf_reg_sizes (tree address)
461 {
462   unsigned int i;
463   enum machine_mode mode = TYPE_MODE (char_type_node);
464   rtx addr = expand_normal (address);
465   rtx mem = gen_rtx_MEM (BLKmode, addr);
466   bool wrote_return_column = false;
467
468   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
469     {
470       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
471       
472       if (rnum < DWARF_FRAME_REGISTERS)
473         {
474           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
475           enum machine_mode save_mode = reg_raw_mode[i];
476           HOST_WIDE_INT size;
477           
478           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
479             save_mode = choose_hard_reg_mode (i, 1, true);
480           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
481             {
482               if (save_mode == VOIDmode)
483                 continue;
484               wrote_return_column = true;
485             }
486           size = GET_MODE_SIZE (save_mode);
487           if (offset < 0)
488             continue;
489           
490           emit_move_insn (adjust_address (mem, mode, offset),
491                           gen_int_mode (size, mode));
492         }
493     }
494
495   if (!wrote_return_column)
496     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
497
498 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
499   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
500 #endif
501
502   targetm.init_dwarf_reg_sizes_extra (address);
503 }
504
505 /* Convert a DWARF call frame info. operation to its string name */
506
507 static const char *
508 dwarf_cfi_name (unsigned int cfi_opc)
509 {
510   switch (cfi_opc)
511     {
512     case DW_CFA_advance_loc:
513       return "DW_CFA_advance_loc";
514     case DW_CFA_offset:
515       return "DW_CFA_offset";
516     case DW_CFA_restore:
517       return "DW_CFA_restore";
518     case DW_CFA_nop:
519       return "DW_CFA_nop";
520     case DW_CFA_set_loc:
521       return "DW_CFA_set_loc";
522     case DW_CFA_advance_loc1:
523       return "DW_CFA_advance_loc1";
524     case DW_CFA_advance_loc2:
525       return "DW_CFA_advance_loc2";
526     case DW_CFA_advance_loc4:
527       return "DW_CFA_advance_loc4";
528     case DW_CFA_offset_extended:
529       return "DW_CFA_offset_extended";
530     case DW_CFA_restore_extended:
531       return "DW_CFA_restore_extended";
532     case DW_CFA_undefined:
533       return "DW_CFA_undefined";
534     case DW_CFA_same_value:
535       return "DW_CFA_same_value";
536     case DW_CFA_register:
537       return "DW_CFA_register";
538     case DW_CFA_remember_state:
539       return "DW_CFA_remember_state";
540     case DW_CFA_restore_state:
541       return "DW_CFA_restore_state";
542     case DW_CFA_def_cfa:
543       return "DW_CFA_def_cfa";
544     case DW_CFA_def_cfa_register:
545       return "DW_CFA_def_cfa_register";
546     case DW_CFA_def_cfa_offset:
547       return "DW_CFA_def_cfa_offset";
548
549     /* DWARF 3 */
550     case DW_CFA_def_cfa_expression:
551       return "DW_CFA_def_cfa_expression";
552     case DW_CFA_expression:
553       return "DW_CFA_expression";
554     case DW_CFA_offset_extended_sf:
555       return "DW_CFA_offset_extended_sf";
556     case DW_CFA_def_cfa_sf:
557       return "DW_CFA_def_cfa_sf";
558     case DW_CFA_def_cfa_offset_sf:
559       return "DW_CFA_def_cfa_offset_sf";
560
561     /* SGI/MIPS specific */
562     case DW_CFA_MIPS_advance_loc8:
563       return "DW_CFA_MIPS_advance_loc8";
564
565     /* GNU extensions */
566     case DW_CFA_GNU_window_save:
567       return "DW_CFA_GNU_window_save";
568     case DW_CFA_GNU_args_size:
569       return "DW_CFA_GNU_args_size";
570     case DW_CFA_GNU_negative_offset_extended:
571       return "DW_CFA_GNU_negative_offset_extended";
572
573     default:
574       return "DW_CFA_<unknown>";
575     }
576 }
577
578 /* Return a pointer to a newly allocated Call Frame Instruction.  */
579
580 static inline dw_cfi_ref
581 new_cfi (void)
582 {
583   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
584
585   cfi->dw_cfi_next = NULL;
586   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
587   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
588
589   return cfi;
590 }
591
592 /* Add a Call Frame Instruction to list of instructions.  */
593
594 static inline void
595 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
596 {
597   dw_cfi_ref *p;
598
599   /* Find the end of the chain.  */
600   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
601     ;
602
603   *p = cfi;
604 }
605
606 /* Generate a new label for the CFI info to refer to.  */
607
608 char *
609 dwarf2out_cfi_label (void)
610 {
611   static char label[20];
612
613   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
614   ASM_OUTPUT_LABEL (asm_out_file, label);
615   return label;
616 }
617
618 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
619    or to the CIE if LABEL is NULL.  */
620
621 static void
622 add_fde_cfi (const char *label, dw_cfi_ref cfi)
623 {
624   if (label)
625     {
626       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
627
628       if (*label == 0)
629         label = dwarf2out_cfi_label ();
630
631       if (fde->dw_fde_current_label == NULL
632           || strcmp (label, fde->dw_fde_current_label) != 0)
633         {
634           dw_cfi_ref xcfi;
635
636           label = xstrdup (label);
637
638           /* Set the location counter to the new label.  */
639           xcfi = new_cfi ();
640           /* If we have a current label, advance from there, otherwise
641              set the location directly using set_loc.  */
642           xcfi->dw_cfi_opc = fde->dw_fde_current_label
643                              ? DW_CFA_advance_loc4
644                              : DW_CFA_set_loc;
645           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
646           add_cfi (&fde->dw_fde_cfi, xcfi);
647
648           fde->dw_fde_current_label = label;
649         }
650
651       add_cfi (&fde->dw_fde_cfi, cfi);
652     }
653
654   else
655     add_cfi (&cie_cfi_head, cfi);
656 }
657
658 /* Subroutine of lookup_cfa.  */
659
660 static void
661 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
662 {
663   switch (cfi->dw_cfi_opc)
664     {
665     case DW_CFA_def_cfa_offset:
666       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
667       break;
668     case DW_CFA_def_cfa_offset_sf:
669       loc->offset
670         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
671       break;
672     case DW_CFA_def_cfa_register:
673       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
674       break;
675     case DW_CFA_def_cfa:
676       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
677       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
678       break;
679     case DW_CFA_def_cfa_sf:
680       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
681       loc->offset
682         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
683       break;
684     case DW_CFA_def_cfa_expression:
685       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
686       break;
687     default:
688       break;
689     }
690 }
691
692 /* Find the previous value for the CFA.  */
693
694 static void
695 lookup_cfa (dw_cfa_location *loc)
696 {
697   dw_cfi_ref cfi;
698
699   loc->reg = INVALID_REGNUM;
700   loc->offset = 0;
701   loc->indirect = 0;
702   loc->base_offset = 0;
703
704   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
705     lookup_cfa_1 (cfi, loc);
706
707   if (fde_table_in_use)
708     {
709       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
710       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
711         lookup_cfa_1 (cfi, loc);
712     }
713 }
714
715 /* The current rule for calculating the DWARF2 canonical frame address.  */
716 static dw_cfa_location cfa;
717
718 /* The register used for saving registers to the stack, and its offset
719    from the CFA.  */
720 static dw_cfa_location cfa_store;
721
722 /* The running total of the size of arguments pushed onto the stack.  */
723 static HOST_WIDE_INT args_size;
724
725 /* The last args_size we actually output.  */
726 static HOST_WIDE_INT old_args_size;
727
728 /* Entry point to update the canonical frame address (CFA).
729    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
730    calculated from REG+OFFSET.  */
731
732 void
733 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
734 {
735   dw_cfa_location loc;
736   loc.indirect = 0;
737   loc.base_offset = 0;
738   loc.reg = reg;
739   loc.offset = offset;
740   def_cfa_1 (label, &loc);
741 }
742
743 /* Determine if two dw_cfa_location structures define the same data.  */
744
745 static bool
746 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
747 {
748   return (loc1->reg == loc2->reg
749           && loc1->offset == loc2->offset
750           && loc1->indirect == loc2->indirect
751           && (loc1->indirect == 0
752               || loc1->base_offset == loc2->base_offset));
753 }
754
755 /* This routine does the actual work.  The CFA is now calculated from
756    the dw_cfa_location structure.  */
757
758 static void
759 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
760 {
761   dw_cfi_ref cfi;
762   dw_cfa_location old_cfa, loc;
763
764   cfa = *loc_p;
765   loc = *loc_p;
766
767   if (cfa_store.reg == loc.reg && loc.indirect == 0)
768     cfa_store.offset = loc.offset;
769
770   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
771   lookup_cfa (&old_cfa);
772
773   /* If nothing changed, no need to issue any call frame instructions.  */
774   if (cfa_equal_p (&loc, &old_cfa))
775     return;
776
777   cfi = new_cfi ();
778
779   if (loc.reg == old_cfa.reg && !loc.indirect)
780     {
781       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
782          the CFA register did not change but the offset did.  */
783       if (loc.offset < 0)
784         {
785           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
786           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
787
788           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
789           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
790         }
791       else
792         {
793           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
794           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
795         }
796     }
797
798 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
799   else if (loc.offset == old_cfa.offset
800            && old_cfa.reg != INVALID_REGNUM
801            && !loc.indirect)
802     {
803       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
804          indicating the CFA register has changed to <register> but the
805          offset has not changed.  */
806       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
807       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
808     }
809 #endif
810
811   else if (loc.indirect == 0)
812     {
813       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
814          indicating the CFA register has changed to <register> with
815          the specified offset.  */
816       if (loc.offset < 0)
817         {
818           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
819           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
820
821           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
822           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
823           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
824         }
825       else
826         {
827           cfi->dw_cfi_opc = DW_CFA_def_cfa;
828           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
830         }
831     }
832   else
833     {
834       /* Construct a DW_CFA_def_cfa_expression instruction to
835          calculate the CFA using a full location expression since no
836          register-offset pair is available.  */
837       struct dw_loc_descr_struct *loc_list;
838
839       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
840       loc_list = build_cfa_loc (&loc, 0);
841       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
842     }
843
844   add_fde_cfi (label, cfi);
845 }
846
847 /* Add the CFI for saving a register.  REG is the CFA column number.
848    LABEL is passed to add_fde_cfi.
849    If SREG is -1, the register is saved at OFFSET from the CFA;
850    otherwise it is saved in SREG.  */
851
852 static void
853 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
854 {
855   dw_cfi_ref cfi = new_cfi ();
856
857   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
858
859   if (sreg == INVALID_REGNUM)
860     {
861       if (reg & ~0x3f)
862         /* The register number won't fit in 6 bits, so we have to use
863            the long form.  */
864         cfi->dw_cfi_opc = DW_CFA_offset_extended;
865       else
866         cfi->dw_cfi_opc = DW_CFA_offset;
867
868 #ifdef ENABLE_CHECKING
869       {
870         /* If we get an offset that is not a multiple of
871            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
872            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
873            description.  */
874         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
875
876         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
877       }
878 #endif
879       offset /= DWARF_CIE_DATA_ALIGNMENT;
880       if (offset < 0)
881         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
882
883       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
884     }
885   else if (sreg == reg)
886     cfi->dw_cfi_opc = DW_CFA_same_value;
887   else
888     {
889       cfi->dw_cfi_opc = DW_CFA_register;
890       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
891     }
892
893   add_fde_cfi (label, cfi);
894 }
895
896 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
897    This CFI tells the unwinder that it needs to restore the window registers
898    from the previous frame's window save area.
899
900    ??? Perhaps we should note in the CIE where windows are saved (instead of
901    assuming 0(cfa)) and what registers are in the window.  */
902
903 void
904 dwarf2out_window_save (const char *label)
905 {
906   dw_cfi_ref cfi = new_cfi ();
907
908   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
909   add_fde_cfi (label, cfi);
910 }
911
912 /* Add a CFI to update the running total of the size of arguments
913    pushed onto the stack.  */
914
915 void
916 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
917 {
918   dw_cfi_ref cfi;
919
920   if (size == old_args_size)
921     return;
922
923   old_args_size = size;
924
925   cfi = new_cfi ();
926   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
927   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
928   add_fde_cfi (label, cfi);
929 }
930
931 /* Entry point for saving a register to the stack.  REG is the GCC register
932    number.  LABEL and OFFSET are passed to reg_save.  */
933
934 void
935 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
936 {
937   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
938 }
939
940 /* Entry point for saving the return address in the stack.
941    LABEL and OFFSET are passed to reg_save.  */
942
943 void
944 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
945 {
946   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
947 }
948
949 /* Entry point for saving the return address in a register.
950    LABEL and SREG are passed to reg_save.  */
951
952 void
953 dwarf2out_return_reg (const char *label, unsigned int sreg)
954 {
955   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
956 }
957
958 /* Record the initial position of the return address.  RTL is
959    INCOMING_RETURN_ADDR_RTX.  */
960
961 static void
962 initial_return_save (rtx rtl)
963 {
964   unsigned int reg = INVALID_REGNUM;
965   HOST_WIDE_INT offset = 0;
966
967   switch (GET_CODE (rtl))
968     {
969     case REG:
970       /* RA is in a register.  */
971       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
972       break;
973
974     case MEM:
975       /* RA is on the stack.  */
976       rtl = XEXP (rtl, 0);
977       switch (GET_CODE (rtl))
978         {
979         case REG:
980           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
981           offset = 0;
982           break;
983
984         case PLUS:
985           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
986           offset = INTVAL (XEXP (rtl, 1));
987           break;
988
989         case MINUS:
990           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
991           offset = -INTVAL (XEXP (rtl, 1));
992           break;
993
994         default:
995           gcc_unreachable ();
996         }
997
998       break;
999
1000     case PLUS:
1001       /* The return address is at some offset from any value we can
1002          actually load.  For instance, on the SPARC it is in %i7+8. Just
1003          ignore the offset for now; it doesn't matter for unwinding frames.  */
1004       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1005       initial_return_save (XEXP (rtl, 0));
1006       return;
1007
1008     default:
1009       gcc_unreachable ();
1010     }
1011
1012   if (reg != DWARF_FRAME_RETURN_COLUMN)
1013     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1014 }
1015
1016 /* Given a SET, calculate the amount of stack adjustment it
1017    contains.  */
1018
1019 static HOST_WIDE_INT
1020 stack_adjust_offset (rtx pattern)
1021 {
1022   rtx src = SET_SRC (pattern);
1023   rtx dest = SET_DEST (pattern);
1024   HOST_WIDE_INT offset = 0;
1025   enum rtx_code code;
1026
1027   if (dest == stack_pointer_rtx)
1028     {
1029       /* (set (reg sp) (plus (reg sp) (const_int))) */
1030       code = GET_CODE (src);
1031       if (! (code == PLUS || code == MINUS)
1032           || XEXP (src, 0) != stack_pointer_rtx
1033           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1034         return 0;
1035
1036       offset = INTVAL (XEXP (src, 1));
1037       if (code == PLUS)
1038         offset = -offset;
1039     }
1040   else if (MEM_P (dest))
1041     {
1042       /* (set (mem (pre_dec (reg sp))) (foo)) */
1043       src = XEXP (dest, 0);
1044       code = GET_CODE (src);
1045
1046       switch (code)
1047         {
1048         case PRE_MODIFY:
1049         case POST_MODIFY:
1050           if (XEXP (src, 0) == stack_pointer_rtx)
1051             {
1052               rtx val = XEXP (XEXP (src, 1), 1);
1053               /* We handle only adjustments by constant amount.  */
1054               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1055                           && GET_CODE (val) == CONST_INT);
1056               offset = -INTVAL (val);
1057               break;
1058             }
1059           return 0;
1060
1061         case PRE_DEC:
1062         case POST_DEC:
1063           if (XEXP (src, 0) == stack_pointer_rtx)
1064             {
1065               offset = GET_MODE_SIZE (GET_MODE (dest));
1066               break;
1067             }
1068           return 0;
1069
1070         case PRE_INC:
1071         case POST_INC:
1072           if (XEXP (src, 0) == stack_pointer_rtx)
1073             {
1074               offset = -GET_MODE_SIZE (GET_MODE (dest));
1075               break;
1076             }
1077           return 0;
1078
1079         default:
1080           return 0;
1081         }
1082     }
1083   else
1084     return 0;
1085
1086   return offset;
1087 }
1088
1089 /* Check INSN to see if it looks like a push or a stack adjustment, and
1090    make a note of it if it does.  EH uses this information to find out how
1091    much extra space it needs to pop off the stack.  */
1092
1093 static void
1094 dwarf2out_stack_adjust (rtx insn, bool after_p)
1095 {
1096   HOST_WIDE_INT offset;
1097   const char *label;
1098   int i;
1099
1100   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1101      with this function.  Proper support would require all frame-related
1102      insns to be marked, and to be able to handle saving state around
1103      epilogues textually in the middle of the function.  */
1104   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1105     return;
1106
1107   /* If only calls can throw, and we have a frame pointer,
1108      save up adjustments until we see the CALL_INSN.  */
1109   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1110     {
1111       if (CALL_P (insn) && !after_p)
1112         {
1113           /* Extract the size of the args from the CALL rtx itself.  */
1114           insn = PATTERN (insn);
1115           if (GET_CODE (insn) == PARALLEL)
1116             insn = XVECEXP (insn, 0, 0);
1117           if (GET_CODE (insn) == SET)
1118             insn = SET_SRC (insn);
1119           gcc_assert (GET_CODE (insn) == CALL);
1120           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1121         }
1122       return;
1123     }
1124
1125   if (CALL_P (insn) && !after_p)
1126     {
1127       if (!flag_asynchronous_unwind_tables)
1128         dwarf2out_args_size ("", args_size);
1129       return;
1130     }
1131   else if (BARRIER_P (insn))
1132     {
1133       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1134          the compiler will have already emitted a stack adjustment, but
1135          doesn't bother for calls to noreturn functions.  */
1136 #ifdef STACK_GROWS_DOWNWARD
1137       offset = -args_size;
1138 #else
1139       offset = args_size;
1140 #endif
1141     }
1142   else if (GET_CODE (PATTERN (insn)) == SET)
1143     offset = stack_adjust_offset (PATTERN (insn));
1144   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1145            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1146     {
1147       /* There may be stack adjustments inside compound insns.  Search
1148          for them.  */
1149       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1150         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1151           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1152     }
1153   else
1154     return;
1155
1156   if (offset == 0)
1157     return;
1158
1159   if (cfa.reg == STACK_POINTER_REGNUM)
1160     cfa.offset += offset;
1161
1162 #ifndef STACK_GROWS_DOWNWARD
1163   offset = -offset;
1164 #endif
1165
1166   args_size += offset;
1167   if (args_size < 0)
1168     args_size = 0;
1169
1170   label = dwarf2out_cfi_label ();
1171   def_cfa_1 (label, &cfa);
1172   if (flag_asynchronous_unwind_tables)
1173     dwarf2out_args_size (label, args_size);
1174 }
1175
1176 #endif
1177
1178 /* We delay emitting a register save until either (a) we reach the end
1179    of the prologue or (b) the register is clobbered.  This clusters
1180    register saves so that there are fewer pc advances.  */
1181
1182 struct queued_reg_save GTY(())
1183 {
1184   struct queued_reg_save *next;
1185   rtx reg;
1186   HOST_WIDE_INT cfa_offset;
1187   rtx saved_reg;
1188 };
1189
1190 static GTY(()) struct queued_reg_save *queued_reg_saves;
1191
1192 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1193 struct reg_saved_in_data GTY(()) {
1194   rtx orig_reg;
1195   rtx saved_in_reg;
1196 };
1197
1198 /* A list of registers saved in other registers.
1199    The list intentionally has a small maximum capacity of 4; if your
1200    port needs more than that, you might consider implementing a
1201    more efficient data structure.  */
1202 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1203 static GTY(()) size_t num_regs_saved_in_regs;
1204
1205 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1206 static const char *last_reg_save_label;
1207
1208 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1209    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1210
1211 static void
1212 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1213 {
1214   struct queued_reg_save *q;
1215
1216   /* Duplicates waste space, but it's also necessary to remove them
1217      for correctness, since the queue gets output in reverse
1218      order.  */
1219   for (q = queued_reg_saves; q != NULL; q = q->next)
1220     if (REGNO (q->reg) == REGNO (reg))
1221       break;
1222
1223   if (q == NULL)
1224     {
1225       q = ggc_alloc (sizeof (*q));
1226       q->next = queued_reg_saves;
1227       queued_reg_saves = q;
1228     }
1229
1230   q->reg = reg;
1231   q->cfa_offset = offset;
1232   q->saved_reg = sreg;
1233
1234   last_reg_save_label = label;
1235 }
1236
1237 /* Output all the entries in QUEUED_REG_SAVES.  */
1238
1239 static void
1240 flush_queued_reg_saves (void)
1241 {
1242   struct queued_reg_save *q;
1243
1244   for (q = queued_reg_saves; q; q = q->next)
1245     {
1246       size_t i;
1247       unsigned int reg, sreg;
1248
1249       for (i = 0; i < num_regs_saved_in_regs; i++)
1250         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1251           break;
1252       if (q->saved_reg && i == num_regs_saved_in_regs)
1253         {
1254           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1255           num_regs_saved_in_regs++;
1256         }
1257       if (i != num_regs_saved_in_regs)
1258         {
1259           regs_saved_in_regs[i].orig_reg = q->reg;
1260           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1261         }
1262
1263       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1264       if (q->saved_reg)
1265         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1266       else
1267         sreg = INVALID_REGNUM;
1268       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1269     }
1270
1271   queued_reg_saves = NULL;
1272   last_reg_save_label = NULL;
1273 }
1274
1275 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1276    location for?  Or, does it clobber a register which we've previously
1277    said that some other register is saved in, and for which we now
1278    have a new location for?  */
1279
1280 static bool
1281 clobbers_queued_reg_save (rtx insn)
1282 {
1283   struct queued_reg_save *q;
1284
1285   for (q = queued_reg_saves; q; q = q->next)
1286     {
1287       size_t i;
1288       if (modified_in_p (q->reg, insn))
1289         return true;
1290       for (i = 0; i < num_regs_saved_in_regs; i++)
1291         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1292             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1293           return true;
1294     }
1295
1296   return false;
1297 }
1298
1299 /* Entry point for saving the first register into the second.  */
1300
1301 void
1302 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1303 {
1304   size_t i;
1305   unsigned int regno, sregno;
1306
1307   for (i = 0; i < num_regs_saved_in_regs; i++)
1308     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1309       break;
1310   if (i == num_regs_saved_in_regs)
1311     {
1312       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1313       num_regs_saved_in_regs++;
1314     }
1315   regs_saved_in_regs[i].orig_reg = reg;
1316   regs_saved_in_regs[i].saved_in_reg = sreg;
1317
1318   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1319   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1320   reg_save (label, regno, sregno, 0);
1321 }
1322
1323 /* What register, if any, is currently saved in REG?  */
1324
1325 static rtx
1326 reg_saved_in (rtx reg)
1327 {
1328   unsigned int regn = REGNO (reg);
1329   size_t i;
1330   struct queued_reg_save *q;
1331
1332   for (q = queued_reg_saves; q; q = q->next)
1333     if (q->saved_reg && regn == REGNO (q->saved_reg))
1334       return q->reg;
1335
1336   for (i = 0; i < num_regs_saved_in_regs; i++)
1337     if (regs_saved_in_regs[i].saved_in_reg
1338         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1339       return regs_saved_in_regs[i].orig_reg;
1340
1341   return NULL_RTX;
1342 }
1343
1344
1345 /* A temporary register holding an integral value used in adjusting SP
1346    or setting up the store_reg.  The "offset" field holds the integer
1347    value, not an offset.  */
1348 static dw_cfa_location cfa_temp;
1349
1350 /* Record call frame debugging information for an expression EXPR,
1351    which either sets SP or FP (adjusting how we calculate the frame
1352    address) or saves a register to the stack or another register.
1353    LABEL indicates the address of EXPR.
1354
1355    This function encodes a state machine mapping rtxes to actions on
1356    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1357    users need not read the source code.
1358
1359   The High-Level Picture
1360
1361   Changes in the register we use to calculate the CFA: Currently we
1362   assume that if you copy the CFA register into another register, we
1363   should take the other one as the new CFA register; this seems to
1364   work pretty well.  If it's wrong for some target, it's simple
1365   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1366
1367   Changes in the register we use for saving registers to the stack:
1368   This is usually SP, but not always.  Again, we deduce that if you
1369   copy SP into another register (and SP is not the CFA register),
1370   then the new register is the one we will be using for register
1371   saves.  This also seems to work.
1372
1373   Register saves: There's not much guesswork about this one; if
1374   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1375   register save, and the register used to calculate the destination
1376   had better be the one we think we're using for this purpose.
1377   It's also assumed that a copy from a call-saved register to another
1378   register is saving that register if RTX_FRAME_RELATED_P is set on
1379   that instruction.  If the copy is from a call-saved register to
1380   the *same* register, that means that the register is now the same
1381   value as in the caller.
1382
1383   Except: If the register being saved is the CFA register, and the
1384   offset is nonzero, we are saving the CFA, so we assume we have to
1385   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1386   the intent is to save the value of SP from the previous frame.
1387
1388   In addition, if a register has previously been saved to a different
1389   register,
1390
1391   Invariants / Summaries of Rules
1392
1393   cfa          current rule for calculating the CFA.  It usually
1394                consists of a register and an offset.
1395   cfa_store    register used by prologue code to save things to the stack
1396                cfa_store.offset is the offset from the value of
1397                cfa_store.reg to the actual CFA
1398   cfa_temp     register holding an integral value.  cfa_temp.offset
1399                stores the value, which will be used to adjust the
1400                stack pointer.  cfa_temp is also used like cfa_store,
1401                to track stores to the stack via fp or a temp reg.
1402
1403   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1404                with cfa.reg as the first operand changes the cfa.reg and its
1405                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1406                cfa_temp.offset.
1407
1408   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1409                expression yielding a constant.  This sets cfa_temp.reg
1410                and cfa_temp.offset.
1411
1412   Rule 5:      Create a new register cfa_store used to save items to the
1413                stack.
1414
1415   Rules 10-14: Save a register to the stack.  Define offset as the
1416                difference of the original location and cfa_store's
1417                location (or cfa_temp's location if cfa_temp is used).
1418
1419   The Rules
1420
1421   "{a,b}" indicates a choice of a xor b.
1422   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1423
1424   Rule 1:
1425   (set <reg1> <reg2>:cfa.reg)
1426   effects: cfa.reg = <reg1>
1427            cfa.offset unchanged
1428            cfa_temp.reg = <reg1>
1429            cfa_temp.offset = cfa.offset
1430
1431   Rule 2:
1432   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1433                               {<const_int>,<reg>:cfa_temp.reg}))
1434   effects: cfa.reg = sp if fp used
1435            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1436            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1437              if cfa_store.reg==sp
1438
1439   Rule 3:
1440   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1441   effects: cfa.reg = fp
1442            cfa_offset += +/- <const_int>
1443
1444   Rule 4:
1445   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1446   constraints: <reg1> != fp
1447                <reg1> != sp
1448   effects: cfa.reg = <reg1>
1449            cfa_temp.reg = <reg1>
1450            cfa_temp.offset = cfa.offset
1451
1452   Rule 5:
1453   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1454   constraints: <reg1> != fp
1455                <reg1> != sp
1456   effects: cfa_store.reg = <reg1>
1457            cfa_store.offset = cfa.offset - cfa_temp.offset
1458
1459   Rule 6:
1460   (set <reg> <const_int>)
1461   effects: cfa_temp.reg = <reg>
1462            cfa_temp.offset = <const_int>
1463
1464   Rule 7:
1465   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1466   effects: cfa_temp.reg = <reg1>
1467            cfa_temp.offset |= <const_int>
1468
1469   Rule 8:
1470   (set <reg> (high <exp>))
1471   effects: none
1472
1473   Rule 9:
1474   (set <reg> (lo_sum <exp> <const_int>))
1475   effects: cfa_temp.reg = <reg>
1476            cfa_temp.offset = <const_int>
1477
1478   Rule 10:
1479   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1480   effects: cfa_store.offset -= <const_int>
1481            cfa.offset = cfa_store.offset if cfa.reg == sp
1482            cfa.reg = sp
1483            cfa.base_offset = -cfa_store.offset
1484
1485   Rule 11:
1486   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1487   effects: cfa_store.offset += -/+ mode_size(mem)
1488            cfa.offset = cfa_store.offset if cfa.reg == sp
1489            cfa.reg = sp
1490            cfa.base_offset = -cfa_store.offset
1491
1492   Rule 12:
1493   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1494
1495        <reg2>)
1496   effects: cfa.reg = <reg1>
1497            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1498
1499   Rule 13:
1500   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1501   effects: cfa.reg = <reg1>
1502            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1503
1504   Rule 14:
1505   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1506   effects: cfa.reg = <reg1>
1507            cfa.base_offset = -cfa_temp.offset
1508            cfa_temp.offset -= mode_size(mem)
1509
1510   Rule 15:
1511   (set <reg> {unspec, unspec_volatile})
1512   effects: target-dependent  */
1513
1514 static void
1515 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1516 {
1517   rtx src, dest;
1518   HOST_WIDE_INT offset;
1519
1520   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1521      the PARALLEL independently. The first element is always processed if
1522      it is a SET. This is for backward compatibility.   Other elements
1523      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1524      flag is set in them.  */
1525   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1526     {
1527       int par_index;
1528       int limit = XVECLEN (expr, 0);
1529       rtx elem;
1530
1531       /* PARALLELs have strict read-modify-write semantics, so we
1532          ought to evaluate every rvalue before changing any lvalue.
1533          It's cumbersome to do that in general, but there's an
1534          easy approximation that is enough for all current users:
1535          handle register saves before register assignments.  */
1536       if (GET_CODE (expr) == PARALLEL)
1537         for (par_index = 0; par_index < limit; par_index++)
1538           {
1539             elem = XVECEXP (expr, 0, par_index);
1540             if (GET_CODE (elem) == SET
1541                 && MEM_P (SET_DEST (elem))
1542                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1543               dwarf2out_frame_debug_expr (elem, label);
1544           }
1545
1546       for (par_index = 0; par_index < limit; par_index++)
1547         {
1548           elem = XVECEXP (expr, 0, par_index);
1549           if (GET_CODE (elem) == SET
1550               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1551               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1552             dwarf2out_frame_debug_expr (elem, label);
1553         }
1554       return;
1555     }
1556
1557   gcc_assert (GET_CODE (expr) == SET);
1558
1559   src = SET_SRC (expr);
1560   dest = SET_DEST (expr);
1561
1562   if (REG_P (src))
1563     {
1564       rtx rsi = reg_saved_in (src);
1565       if (rsi)
1566         src = rsi;
1567     }
1568
1569   switch (GET_CODE (dest))
1570     {
1571     case REG:
1572       switch (GET_CODE (src))
1573         {
1574           /* Setting FP from SP.  */
1575         case REG:
1576           if (cfa.reg == (unsigned) REGNO (src))
1577             {
1578               /* Rule 1 */
1579               /* Update the CFA rule wrt SP or FP.  Make sure src is
1580                  relative to the current CFA register.
1581
1582                  We used to require that dest be either SP or FP, but the
1583                  ARM copies SP to a temporary register, and from there to
1584                  FP.  So we just rely on the backends to only set
1585                  RTX_FRAME_RELATED_P on appropriate insns.  */
1586               cfa.reg = REGNO (dest);
1587               cfa_temp.reg = cfa.reg;
1588               cfa_temp.offset = cfa.offset;
1589             }
1590           else
1591             {
1592               /* Saving a register in a register.  */
1593               gcc_assert (!fixed_regs [REGNO (dest)]
1594                           /* For the SPARC and its register window.  */
1595                           || (DWARF_FRAME_REGNUM (REGNO (src))
1596                               == DWARF_FRAME_RETURN_COLUMN));
1597               queue_reg_save (label, src, dest, 0);
1598             }
1599           break;
1600
1601         case PLUS:
1602         case MINUS:
1603         case LO_SUM:
1604           if (dest == stack_pointer_rtx)
1605             {
1606               /* Rule 2 */
1607               /* Adjusting SP.  */
1608               switch (GET_CODE (XEXP (src, 1)))
1609                 {
1610                 case CONST_INT:
1611                   offset = INTVAL (XEXP (src, 1));
1612                   break;
1613                 case REG:
1614                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1615                               == cfa_temp.reg);
1616                   offset = cfa_temp.offset;
1617                   break;
1618                 default:
1619                   gcc_unreachable ();
1620                 }
1621
1622               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1623                 {
1624                   /* Restoring SP from FP in the epilogue.  */
1625                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1626                   cfa.reg = STACK_POINTER_REGNUM;
1627                 }
1628               else if (GET_CODE (src) == LO_SUM)
1629                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1630                 ;
1631               else
1632                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1633
1634               if (GET_CODE (src) != MINUS)
1635                 offset = -offset;
1636               if (cfa.reg == STACK_POINTER_REGNUM)
1637                 cfa.offset += offset;
1638               if (cfa_store.reg == STACK_POINTER_REGNUM)
1639                 cfa_store.offset += offset;
1640             }
1641           else if (dest == hard_frame_pointer_rtx)
1642             {
1643               /* Rule 3 */
1644               /* Either setting the FP from an offset of the SP,
1645                  or adjusting the FP */
1646               gcc_assert (frame_pointer_needed);
1647
1648               gcc_assert (REG_P (XEXP (src, 0))
1649                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1650                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1651               offset = INTVAL (XEXP (src, 1));
1652               if (GET_CODE (src) != MINUS)
1653                 offset = -offset;
1654               cfa.offset += offset;
1655               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1656             }
1657           else
1658             {
1659               gcc_assert (GET_CODE (src) != MINUS);
1660
1661               /* Rule 4 */
1662               if (REG_P (XEXP (src, 0))
1663                   && REGNO (XEXP (src, 0)) == cfa.reg
1664                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1665                 {
1666                   /* Setting a temporary CFA register that will be copied
1667                      into the FP later on.  */
1668                   offset = - INTVAL (XEXP (src, 1));
1669                   cfa.offset += offset;
1670                   cfa.reg = REGNO (dest);
1671                   /* Or used to save regs to the stack.  */
1672                   cfa_temp.reg = cfa.reg;
1673                   cfa_temp.offset = cfa.offset;
1674                 }
1675
1676               /* Rule 5 */
1677               else if (REG_P (XEXP (src, 0))
1678                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1679                        && XEXP (src, 1) == stack_pointer_rtx)
1680                 {
1681                   /* Setting a scratch register that we will use instead
1682                      of SP for saving registers to the stack.  */
1683                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1684                   cfa_store.reg = REGNO (dest);
1685                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1686                 }
1687
1688               /* Rule 9 */
1689               else if (GET_CODE (src) == LO_SUM
1690                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1691                 {
1692                   cfa_temp.reg = REGNO (dest);
1693                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1694                 }
1695               else
1696                 gcc_unreachable ();
1697             }
1698           break;
1699
1700           /* Rule 6 */
1701         case CONST_INT:
1702           cfa_temp.reg = REGNO (dest);
1703           cfa_temp.offset = INTVAL (src);
1704           break;
1705
1706           /* Rule 7 */
1707         case IOR:
1708           gcc_assert (REG_P (XEXP (src, 0))
1709                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1710                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1711
1712           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1713             cfa_temp.reg = REGNO (dest);
1714           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1715           break;
1716
1717           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1718              which will fill in all of the bits.  */
1719           /* Rule 8 */
1720         case HIGH:
1721           break;
1722
1723           /* Rule 15 */
1724         case UNSPEC:
1725         case UNSPEC_VOLATILE:
1726           gcc_assert (targetm.dwarf_handle_frame_unspec);
1727           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1728           return;
1729
1730         default:
1731           gcc_unreachable ();
1732         }
1733
1734       def_cfa_1 (label, &cfa);
1735       break;
1736
1737     case MEM:
1738       gcc_assert (REG_P (src));
1739
1740       /* Saving a register to the stack.  Make sure dest is relative to the
1741          CFA register.  */
1742       switch (GET_CODE (XEXP (dest, 0)))
1743         {
1744           /* Rule 10 */
1745           /* With a push.  */
1746         case PRE_MODIFY:
1747           /* We can't handle variable size modifications.  */
1748           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1749                       == CONST_INT);
1750           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1751
1752           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1753                       && cfa_store.reg == STACK_POINTER_REGNUM);
1754
1755           cfa_store.offset += offset;
1756           if (cfa.reg == STACK_POINTER_REGNUM)
1757             cfa.offset = cfa_store.offset;
1758
1759           offset = -cfa_store.offset;
1760           break;
1761
1762           /* Rule 11 */
1763         case PRE_INC:
1764         case PRE_DEC:
1765           offset = GET_MODE_SIZE (GET_MODE (dest));
1766           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1767             offset = -offset;
1768
1769           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1770                       && cfa_store.reg == STACK_POINTER_REGNUM);
1771
1772           cfa_store.offset += offset;
1773           if (cfa.reg == STACK_POINTER_REGNUM)
1774             cfa.offset = cfa_store.offset;
1775
1776           offset = -cfa_store.offset;
1777           break;
1778
1779           /* Rule 12 */
1780           /* With an offset.  */
1781         case PLUS:
1782         case MINUS:
1783         case LO_SUM:
1784           {
1785             int regno;
1786
1787             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1788                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1789             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1790             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1791               offset = -offset;
1792
1793             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1794
1795             if (cfa_store.reg == (unsigned) regno)
1796               offset -= cfa_store.offset;
1797             else
1798               {
1799                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1800                 offset -= cfa_temp.offset;
1801               }
1802           }
1803           break;
1804
1805           /* Rule 13 */
1806           /* Without an offset.  */
1807         case REG:
1808           {
1809             int regno = REGNO (XEXP (dest, 0));
1810
1811             if (cfa_store.reg == (unsigned) regno)
1812               offset = -cfa_store.offset;
1813             else
1814               {
1815                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1816                 offset = -cfa_temp.offset;
1817               }
1818           }
1819           break;
1820
1821           /* Rule 14 */
1822         case POST_INC:
1823           gcc_assert (cfa_temp.reg
1824                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1825           offset = -cfa_temp.offset;
1826           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1827           break;
1828
1829         default:
1830           gcc_unreachable ();
1831         }
1832
1833       if (REGNO (src) != STACK_POINTER_REGNUM
1834           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1835           && (unsigned) REGNO (src) == cfa.reg)
1836         {
1837           /* We're storing the current CFA reg into the stack.  */
1838
1839           if (cfa.offset == 0)
1840             {
1841               /* If the source register is exactly the CFA, assume
1842                  we're saving SP like any other register; this happens
1843                  on the ARM.  */
1844               def_cfa_1 (label, &cfa);
1845               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1846               break;
1847             }
1848           else
1849             {
1850               /* Otherwise, we'll need to look in the stack to
1851                  calculate the CFA.  */
1852               rtx x = XEXP (dest, 0);
1853
1854               if (!REG_P (x))
1855                 x = XEXP (x, 0);
1856               gcc_assert (REG_P (x));
1857
1858               cfa.reg = REGNO (x);
1859               cfa.base_offset = offset;
1860               cfa.indirect = 1;
1861               def_cfa_1 (label, &cfa);
1862               break;
1863             }
1864         }
1865
1866       def_cfa_1 (label, &cfa);
1867       queue_reg_save (label, src, NULL_RTX, offset);
1868       break;
1869
1870     default:
1871       gcc_unreachable ();
1872     }
1873 }
1874
1875 /* Record call frame debugging information for INSN, which either
1876    sets SP or FP (adjusting how we calculate the frame address) or saves a
1877    register to the stack.  If INSN is NULL_RTX, initialize our state.
1878
1879    If AFTER_P is false, we're being called before the insn is emitted,
1880    otherwise after.  Call instructions get invoked twice.  */
1881
1882 void
1883 dwarf2out_frame_debug (rtx insn, bool after_p)
1884 {
1885   const char *label;
1886   rtx src;
1887
1888   if (insn == NULL_RTX)
1889     {
1890       size_t i;
1891
1892       /* Flush any queued register saves.  */
1893       flush_queued_reg_saves ();
1894
1895       /* Set up state for generating call frame debug info.  */
1896       lookup_cfa (&cfa);
1897       gcc_assert (cfa.reg
1898                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1899
1900       cfa.reg = STACK_POINTER_REGNUM;
1901       cfa_store = cfa;
1902       cfa_temp.reg = -1;
1903       cfa_temp.offset = 0;
1904
1905       for (i = 0; i < num_regs_saved_in_regs; i++)
1906         {
1907           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1908           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1909         }
1910       num_regs_saved_in_regs = 0;
1911       return;
1912     }
1913
1914   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1915     flush_queued_reg_saves ();
1916
1917   if (! RTX_FRAME_RELATED_P (insn))
1918     {
1919       if (!ACCUMULATE_OUTGOING_ARGS)
1920         dwarf2out_stack_adjust (insn, after_p);
1921       return;
1922     }
1923
1924   label = dwarf2out_cfi_label ();
1925   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1926   if (src)
1927     insn = XEXP (src, 0);
1928   else
1929     insn = PATTERN (insn);
1930
1931   dwarf2out_frame_debug_expr (insn, label);
1932 }
1933
1934 #endif
1935
1936 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1937 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1938  (enum dwarf_call_frame_info cfi);
1939
1940 static enum dw_cfi_oprnd_type
1941 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1942 {
1943   switch (cfi)
1944     {
1945     case DW_CFA_nop:
1946     case DW_CFA_GNU_window_save:
1947       return dw_cfi_oprnd_unused;
1948
1949     case DW_CFA_set_loc:
1950     case DW_CFA_advance_loc1:
1951     case DW_CFA_advance_loc2:
1952     case DW_CFA_advance_loc4:
1953     case DW_CFA_MIPS_advance_loc8:
1954       return dw_cfi_oprnd_addr;
1955
1956     case DW_CFA_offset:
1957     case DW_CFA_offset_extended:
1958     case DW_CFA_def_cfa:
1959     case DW_CFA_offset_extended_sf:
1960     case DW_CFA_def_cfa_sf:
1961     case DW_CFA_restore_extended:
1962     case DW_CFA_undefined:
1963     case DW_CFA_same_value:
1964     case DW_CFA_def_cfa_register:
1965     case DW_CFA_register:
1966       return dw_cfi_oprnd_reg_num;
1967
1968     case DW_CFA_def_cfa_offset:
1969     case DW_CFA_GNU_args_size:
1970     case DW_CFA_def_cfa_offset_sf:
1971       return dw_cfi_oprnd_offset;
1972
1973     case DW_CFA_def_cfa_expression:
1974     case DW_CFA_expression:
1975       return dw_cfi_oprnd_loc;
1976
1977     default:
1978       gcc_unreachable ();
1979     }
1980 }
1981
1982 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1983 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1984  (enum dwarf_call_frame_info cfi);
1985
1986 static enum dw_cfi_oprnd_type
1987 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1988 {
1989   switch (cfi)
1990     {
1991     case DW_CFA_def_cfa:
1992     case DW_CFA_def_cfa_sf:
1993     case DW_CFA_offset:
1994     case DW_CFA_offset_extended_sf:
1995     case DW_CFA_offset_extended:
1996       return dw_cfi_oprnd_offset;
1997
1998     case DW_CFA_register:
1999       return dw_cfi_oprnd_reg_num;
2000
2001     default:
2002       return dw_cfi_oprnd_unused;
2003     }
2004 }
2005
2006 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2007
2008 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2009    switch to the data section instead, and write out a synthetic label
2010    for collect2.  */
2011
2012 static void
2013 switch_to_eh_frame_section (void)
2014 {
2015   tree label;
2016
2017 #ifdef EH_FRAME_SECTION_NAME
2018   if (eh_frame_section == 0)
2019     {
2020       int flags;
2021
2022       if (EH_TABLES_CAN_BE_READ_ONLY)
2023         {
2024           int fde_encoding;
2025           int per_encoding;
2026           int lsda_encoding;
2027
2028           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2029                                                        /*global=*/0);
2030           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2031                                                        /*global=*/1);
2032           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2033                                                         /*global=*/0);
2034           flags = ((! flag_pic
2035                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2036                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2037                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2038                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2039                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2040                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2041                    ? 0 : SECTION_WRITE);
2042         }
2043       else
2044         flags = SECTION_WRITE;
2045       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2046     }
2047 #endif
2048
2049   if (eh_frame_section)
2050     switch_to_section (eh_frame_section);
2051   else
2052     {
2053       /* We have no special eh_frame section.  Put the information in
2054          the data section and emit special labels to guide collect2.  */
2055       switch_to_section (data_section);
2056       label = get_file_function_name ("F");
2057       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2058       targetm.asm_out.globalize_label (asm_out_file,
2059                                        IDENTIFIER_POINTER (label));
2060       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2061     }
2062 }
2063
2064 /* Output a Call Frame Information opcode and its operand(s).  */
2065
2066 static void
2067 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2068 {
2069   unsigned long r;
2070   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2071     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2072                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2073                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2074                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
2075   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2076     {
2077       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2078       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2079                            "DW_CFA_offset, column 0x%lx", r);
2080       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2081     }
2082   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2083     {
2084       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2085       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2086                            "DW_CFA_restore, column 0x%lx", r);
2087     }
2088   else
2089     {
2090       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2091                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2092
2093       switch (cfi->dw_cfi_opc)
2094         {
2095         case DW_CFA_set_loc:
2096           if (for_eh)
2097             dw2_asm_output_encoded_addr_rtx (
2098                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2099                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2100                 false, NULL);
2101           else
2102             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2103                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2104           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2105           break;
2106
2107         case DW_CFA_advance_loc1:
2108           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2109                                 fde->dw_fde_current_label, NULL);
2110           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2111           break;
2112
2113         case DW_CFA_advance_loc2:
2114           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2115                                 fde->dw_fde_current_label, NULL);
2116           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2117           break;
2118
2119         case DW_CFA_advance_loc4:
2120           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2121                                 fde->dw_fde_current_label, NULL);
2122           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2123           break;
2124
2125         case DW_CFA_MIPS_advance_loc8:
2126           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2127                                 fde->dw_fde_current_label, NULL);
2128           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2129           break;
2130
2131         case DW_CFA_offset_extended:
2132         case DW_CFA_def_cfa:
2133           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2134           dw2_asm_output_data_uleb128 (r, NULL);
2135           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2136           break;
2137
2138         case DW_CFA_offset_extended_sf:
2139         case DW_CFA_def_cfa_sf:
2140           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2141           dw2_asm_output_data_uleb128 (r, NULL);
2142           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2143           break;
2144
2145         case DW_CFA_restore_extended:
2146         case DW_CFA_undefined:
2147         case DW_CFA_same_value:
2148         case DW_CFA_def_cfa_register:
2149           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2150           dw2_asm_output_data_uleb128 (r, NULL);
2151           break;
2152
2153         case DW_CFA_register:
2154           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2155           dw2_asm_output_data_uleb128 (r, NULL);
2156           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2157           dw2_asm_output_data_uleb128 (r, NULL);
2158           break;
2159
2160         case DW_CFA_def_cfa_offset:
2161         case DW_CFA_GNU_args_size:
2162           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2163           break;
2164
2165         case DW_CFA_def_cfa_offset_sf:
2166           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2167           break;
2168
2169         case DW_CFA_GNU_window_save:
2170           break;
2171
2172         case DW_CFA_def_cfa_expression:
2173         case DW_CFA_expression:
2174           output_cfa_loc (cfi);
2175           break;
2176
2177         case DW_CFA_GNU_negative_offset_extended:
2178           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2179           gcc_unreachable ();
2180
2181         default:
2182           break;
2183         }
2184     }
2185 }
2186
2187 /* Output the call frame information used to record information
2188    that relates to calculating the frame pointer, and records the
2189    location of saved registers.  */
2190
2191 static void
2192 output_call_frame_info (int for_eh)
2193 {
2194   unsigned int i;
2195   dw_fde_ref fde;
2196   dw_cfi_ref cfi;
2197   char l1[20], l2[20], section_start_label[20];
2198   bool any_lsda_needed = false;
2199   char augmentation[6];
2200   int augmentation_size;
2201   int fde_encoding = DW_EH_PE_absptr;
2202   int per_encoding = DW_EH_PE_absptr;
2203   int lsda_encoding = DW_EH_PE_absptr;
2204   int return_reg;
2205
2206   /* Don't emit a CIE if there won't be any FDEs.  */
2207   if (fde_table_in_use == 0)
2208     return;
2209
2210   /* If we make FDEs linkonce, we may have to emit an empty label for
2211      an FDE that wouldn't otherwise be emitted.  We want to avoid
2212      having an FDE kept around when the function it refers to is
2213      discarded.  Example where this matters: a primary function
2214      template in C++ requires EH information, but an explicit
2215      specialization doesn't.  */
2216   if (TARGET_USES_WEAK_UNWIND_INFO
2217       && ! flag_asynchronous_unwind_tables
2218       && for_eh)
2219     for (i = 0; i < fde_table_in_use; i++)
2220       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2221           && !fde_table[i].uses_eh_lsda
2222           && ! DECL_WEAK (fde_table[i].decl))
2223         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2224                                       for_eh, /* empty */ 1);
2225
2226   /* If we don't have any functions we'll want to unwind out of, don't
2227      emit any EH unwind information.  Note that if exceptions aren't
2228      enabled, we won't have collected nothrow information, and if we
2229      asked for asynchronous tables, we always want this info.  */
2230   if (for_eh)
2231     {
2232       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2233
2234       for (i = 0; i < fde_table_in_use; i++)
2235         if (fde_table[i].uses_eh_lsda)
2236           any_eh_needed = any_lsda_needed = true;
2237         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2238           any_eh_needed = true;
2239         else if (! fde_table[i].nothrow
2240                  && ! fde_table[i].all_throwers_are_sibcalls)
2241           any_eh_needed = true;
2242
2243       if (! any_eh_needed)
2244         return;
2245     }
2246
2247   /* We're going to be generating comments, so turn on app.  */
2248   if (flag_debug_asm)
2249     app_enable ();
2250
2251   if (for_eh)
2252     switch_to_eh_frame_section ();
2253   else
2254     {
2255       if (!debug_frame_section)
2256         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2257                                            SECTION_DEBUG, NULL);
2258       switch_to_section (debug_frame_section);
2259     }
2260
2261   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2262   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2263
2264   /* Output the CIE.  */
2265   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2266   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2267   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2268     dw2_asm_output_data (4, 0xffffffff,
2269       "Initial length escape value indicating 64-bit DWARF extension");
2270   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2271                         "Length of Common Information Entry");
2272   ASM_OUTPUT_LABEL (asm_out_file, l1);
2273
2274   /* Now that the CIE pointer is PC-relative for EH,
2275      use 0 to identify the CIE.  */
2276   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2277                        (for_eh ? 0 : DWARF_CIE_ID),
2278                        "CIE Identifier Tag");
2279
2280   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2281
2282   augmentation[0] = 0;
2283   augmentation_size = 0;
2284   if (for_eh)
2285     {
2286       char *p;
2287
2288       /* Augmentation:
2289          z      Indicates that a uleb128 is present to size the
2290                 augmentation section.
2291          L      Indicates the encoding (and thus presence) of
2292                 an LSDA pointer in the FDE augmentation.
2293          R      Indicates a non-default pointer encoding for
2294                 FDE code pointers.
2295          P      Indicates the presence of an encoding + language
2296                 personality routine in the CIE augmentation.  */
2297
2298       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2299       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2300       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2301
2302       p = augmentation + 1;
2303       if (eh_personality_libfunc)
2304         {
2305           *p++ = 'P';
2306           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2307         }
2308       if (any_lsda_needed)
2309         {
2310           *p++ = 'L';
2311           augmentation_size += 1;
2312         }
2313       if (fde_encoding != DW_EH_PE_absptr)
2314         {
2315           *p++ = 'R';
2316           augmentation_size += 1;
2317         }
2318       if (p > augmentation + 1)
2319         {
2320           augmentation[0] = 'z';
2321           *p = '\0';
2322         }
2323
2324       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2325       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2326         {
2327           int offset = (  4             /* Length */
2328                         + 4             /* CIE Id */
2329                         + 1             /* CIE version */
2330                         + strlen (augmentation) + 1     /* Augmentation */
2331                         + size_of_uleb128 (1)           /* Code alignment */
2332                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2333                         + 1             /* RA column */
2334                         + 1             /* Augmentation size */
2335                         + 1             /* Personality encoding */ );
2336           int pad = -offset & (PTR_SIZE - 1);
2337
2338           augmentation_size += pad;
2339
2340           /* Augmentations should be small, so there's scarce need to
2341              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2342           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2343         }
2344     }
2345
2346   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2347   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2348   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2349                                "CIE Data Alignment Factor");
2350
2351   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2352   if (DW_CIE_VERSION == 1)
2353     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2354   else
2355     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2356
2357   if (augmentation[0])
2358     {
2359       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2360       if (eh_personality_libfunc)
2361         {
2362           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2363                                eh_data_format_name (per_encoding));
2364           dw2_asm_output_encoded_addr_rtx (per_encoding,
2365                                            eh_personality_libfunc,
2366                                            true, NULL);
2367         }
2368
2369       if (any_lsda_needed)
2370         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2371                              eh_data_format_name (lsda_encoding));
2372
2373       if (fde_encoding != DW_EH_PE_absptr)
2374         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2375                              eh_data_format_name (fde_encoding));
2376     }
2377
2378   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2379     output_cfi (cfi, NULL, for_eh);
2380
2381   /* Pad the CIE out to an address sized boundary.  */
2382   ASM_OUTPUT_ALIGN (asm_out_file,
2383                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2384   ASM_OUTPUT_LABEL (asm_out_file, l2);
2385
2386   /* Loop through all of the FDE's.  */
2387   for (i = 0; i < fde_table_in_use; i++)
2388     {
2389       fde = &fde_table[i];
2390
2391       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2392       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2393           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2394           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2395           && !fde->uses_eh_lsda)
2396         continue;
2397
2398       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2399       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2400       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2401       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2402       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2403         dw2_asm_output_data (4, 0xffffffff,
2404                              "Initial length escape value indicating 64-bit DWARF extension");
2405       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2406                             "FDE Length");
2407       ASM_OUTPUT_LABEL (asm_out_file, l1);
2408
2409       if (for_eh)
2410         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2411       else
2412         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2413                                debug_frame_section, "FDE CIE offset");
2414
2415       if (for_eh)
2416         {
2417           rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2418           SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2419           dw2_asm_output_encoded_addr_rtx (fde_encoding,
2420                                            sym_ref,
2421                                            false,
2422                                            "FDE initial location");
2423           if (fde->dw_fde_switched_sections)
2424             {
2425               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode, 
2426                                       fde->dw_fde_unlikely_section_label);
2427               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode, 
2428                                       fde->dw_fde_hot_section_label);
2429               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2430               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2431               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2432                                                "FDE initial location");
2433               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2434                                     fde->dw_fde_hot_section_end_label,
2435                                     fde->dw_fde_hot_section_label,
2436                                     "FDE address range");
2437               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2438                                                "FDE initial location");
2439               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2440                                     fde->dw_fde_unlikely_section_end_label,
2441                                     fde->dw_fde_unlikely_section_label,
2442                                     "FDE address range");
2443             }
2444           else
2445             dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2446                                   fde->dw_fde_end, fde->dw_fde_begin,
2447                                   "FDE address range");
2448         }
2449       else
2450         {
2451           dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2452                                "FDE initial location");
2453           if (fde->dw_fde_switched_sections)
2454             {
2455               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2456                                    fde->dw_fde_hot_section_label,
2457                                    "FDE initial location");
2458               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2459                                     fde->dw_fde_hot_section_end_label,
2460                                     fde->dw_fde_hot_section_label,
2461                                     "FDE address range");
2462               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2463                                    fde->dw_fde_unlikely_section_label,
2464                                    "FDE initial location");
2465               dw2_asm_output_delta (DWARF2_ADDR_SIZE, 
2466                                     fde->dw_fde_unlikely_section_end_label,
2467                                     fde->dw_fde_unlikely_section_label,
2468                                     "FDE address range");
2469             }
2470           else
2471             dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2472                                   fde->dw_fde_end, fde->dw_fde_begin,
2473                                   "FDE address range");
2474         }
2475
2476       if (augmentation[0])
2477         {
2478           if (any_lsda_needed)
2479             {
2480               int size = size_of_encoded_value (lsda_encoding);
2481
2482               if (lsda_encoding == DW_EH_PE_aligned)
2483                 {
2484                   int offset = (  4             /* Length */
2485                                 + 4             /* CIE offset */
2486                                 + 2 * size_of_encoded_value (fde_encoding)
2487                                 + 1             /* Augmentation size */ );
2488                   int pad = -offset & (PTR_SIZE - 1);
2489
2490                   size += pad;
2491                   gcc_assert (size_of_uleb128 (size) == 1);
2492                 }
2493
2494               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2495
2496               if (fde->uses_eh_lsda)
2497                 {
2498                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2499                                                fde->funcdef_number);
2500                   dw2_asm_output_encoded_addr_rtx (
2501                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2502                         false, "Language Specific Data Area");
2503                 }
2504               else
2505                 {
2506                   if (lsda_encoding == DW_EH_PE_aligned)
2507                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2508                   dw2_asm_output_data
2509                     (size_of_encoded_value (lsda_encoding), 0,
2510                      "Language Specific Data Area (none)");
2511                 }
2512             }
2513           else
2514             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2515         }
2516
2517       /* Loop through the Call Frame Instructions associated with
2518          this FDE.  */
2519       fde->dw_fde_current_label = fde->dw_fde_begin;
2520       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2521         output_cfi (cfi, fde, for_eh);
2522
2523       /* Pad the FDE out to an address sized boundary.  */
2524       ASM_OUTPUT_ALIGN (asm_out_file,
2525                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2526       ASM_OUTPUT_LABEL (asm_out_file, l2);
2527     }
2528
2529   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2530     dw2_asm_output_data (4, 0, "End of Table");
2531 #ifdef MIPS_DEBUGGING_INFO
2532   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2533      get a value of 0.  Putting .align 0 after the label fixes it.  */
2534   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2535 #endif
2536
2537   /* Turn off app to make assembly quicker.  */
2538   if (flag_debug_asm)
2539     app_disable ();
2540 }
2541
2542 /* Output a marker (i.e. a label) for the beginning of a function, before
2543    the prologue.  */
2544
2545 void
2546 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2547                           const char *file ATTRIBUTE_UNUSED)
2548 {
2549   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2550   char * dup_label;
2551   dw_fde_ref fde;
2552
2553   current_function_func_begin_label = NULL;
2554
2555 #ifdef TARGET_UNWIND_INFO
2556   /* ??? current_function_func_begin_label is also used by except.c
2557      for call-site information.  We must emit this label if it might
2558      be used.  */
2559   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2560       && ! dwarf2out_do_frame ())
2561     return;
2562 #else
2563   if (! dwarf2out_do_frame ())
2564     return;
2565 #endif
2566
2567   switch_to_section (function_section (current_function_decl));
2568   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2569                                current_function_funcdef_no);
2570   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2571                           current_function_funcdef_no);
2572   dup_label = xstrdup (label);
2573   current_function_func_begin_label = dup_label;
2574
2575 #ifdef TARGET_UNWIND_INFO
2576   /* We can elide the fde allocation if we're not emitting debug info.  */
2577   if (! dwarf2out_do_frame ())
2578     return;
2579 #endif
2580
2581   /* Expand the fde table if necessary.  */
2582   if (fde_table_in_use == fde_table_allocated)
2583     {
2584       fde_table_allocated += FDE_TABLE_INCREMENT;
2585       fde_table = ggc_realloc (fde_table,
2586                                fde_table_allocated * sizeof (dw_fde_node));
2587       memset (fde_table + fde_table_in_use, 0,
2588               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2589     }
2590
2591   /* Record the FDE associated with this function.  */
2592   current_funcdef_fde = fde_table_in_use;
2593
2594   /* Add the new FDE at the end of the fde_table.  */
2595   fde = &fde_table[fde_table_in_use++];
2596   fde->decl = current_function_decl;
2597   fde->dw_fde_begin = dup_label;
2598   fde->dw_fde_current_label = dup_label;
2599   fde->dw_fde_hot_section_label = NULL;
2600   fde->dw_fde_hot_section_end_label = NULL;
2601   fde->dw_fde_unlikely_section_label = NULL;
2602   fde->dw_fde_unlikely_section_end_label = NULL;
2603   fde->dw_fde_switched_sections = false;
2604   fde->dw_fde_end = NULL;
2605   fde->dw_fde_cfi = NULL;
2606   fde->funcdef_number = current_function_funcdef_no;
2607   fde->nothrow = TREE_NOTHROW (current_function_decl);
2608   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2609   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2610
2611   args_size = old_args_size = 0;
2612
2613   /* We only want to output line number information for the genuine dwarf2
2614      prologue case, not the eh frame case.  */
2615 #ifdef DWARF2_DEBUGGING_INFO
2616   if (file)
2617     dwarf2out_source_line (line, file);
2618 #endif
2619 }
2620
2621 /* Output a marker (i.e. a label) for the absolute end of the generated code
2622    for a function definition.  This gets called *after* the epilogue code has
2623    been generated.  */
2624
2625 void
2626 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2627                         const char *file ATTRIBUTE_UNUSED)
2628 {
2629   dw_fde_ref fde;
2630   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2631
2632   /* Output a label to mark the endpoint of the code generated for this
2633      function.  */
2634   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2635                                current_function_funcdef_no);
2636   ASM_OUTPUT_LABEL (asm_out_file, label);
2637   fde = &fde_table[fde_table_in_use - 1];
2638   fde->dw_fde_end = xstrdup (label);
2639 }
2640
2641 void
2642 dwarf2out_frame_init (void)
2643 {
2644   /* Allocate the initial hunk of the fde_table.  */
2645   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2646   fde_table_allocated = FDE_TABLE_INCREMENT;
2647   fde_table_in_use = 0;
2648
2649   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2650      sake of lookup_cfa.  */
2651
2652   /* On entry, the Canonical Frame Address is at SP.  */
2653   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2654
2655 #ifdef DWARF2_UNWIND_INFO
2656   if (DWARF2_UNWIND_INFO)
2657     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2658 #endif
2659 }
2660
2661 void
2662 dwarf2out_frame_finish (void)
2663 {
2664   /* Output call frame information.  */
2665   if (DWARF2_FRAME_INFO)
2666     output_call_frame_info (0);
2667
2668 #ifndef TARGET_UNWIND_INFO
2669   /* Output another copy for the unwinder.  */
2670   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2671     output_call_frame_info (1);
2672 #endif
2673 }
2674 #endif
2675 \f
2676 /* And now, the subset of the debugging information support code necessary
2677    for emitting location expressions.  */
2678
2679 /* Data about a single source file.  */
2680 struct dwarf_file_data GTY(())
2681 {
2682   const char * filename;
2683   int emitted_number;
2684 };
2685
2686 /* We need some way to distinguish DW_OP_addr with a direct symbol
2687    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2688 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2689
2690
2691 typedef struct dw_val_struct *dw_val_ref;
2692 typedef struct die_struct *dw_die_ref;
2693 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2694 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2695
2696 /* Each DIE may have a series of attribute/value pairs.  Values
2697    can take on several forms.  The forms that are used in this
2698    implementation are listed below.  */
2699
2700 enum dw_val_class
2701 {
2702   dw_val_class_addr,
2703   dw_val_class_offset,
2704   dw_val_class_loc,
2705   dw_val_class_loc_list,
2706   dw_val_class_range_list,
2707   dw_val_class_const,
2708   dw_val_class_unsigned_const,
2709   dw_val_class_long_long,
2710   dw_val_class_vec,
2711   dw_val_class_flag,
2712   dw_val_class_die_ref,
2713   dw_val_class_fde_ref,
2714   dw_val_class_lbl_id,
2715   dw_val_class_lineptr,
2716   dw_val_class_str,
2717   dw_val_class_macptr,
2718   dw_val_class_file
2719 };
2720
2721 /* Describe a double word constant value.  */
2722 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2723
2724 typedef struct dw_long_long_struct GTY(())
2725 {
2726   unsigned long hi;
2727   unsigned long low;
2728 }
2729 dw_long_long_const;
2730
2731 /* Describe a floating point constant value, or a vector constant value.  */
2732
2733 typedef struct dw_vec_struct GTY(())
2734 {
2735   unsigned char * GTY((length ("%h.length"))) array;
2736   unsigned length;
2737   unsigned elt_size;
2738 }
2739 dw_vec_const;
2740
2741 /* The dw_val_node describes an attribute's value, as it is
2742    represented internally.  */
2743
2744 typedef struct dw_val_struct GTY(())
2745 {
2746   enum dw_val_class val_class;
2747   union dw_val_struct_union
2748     {
2749       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2750       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2751       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2752       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2753       HOST_WIDE_INT GTY ((default)) val_int;
2754       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2755       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2756       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2757       struct dw_val_die_union
2758         {
2759           dw_die_ref die;
2760           int external;
2761         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2762       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2763       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2764       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2765       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2766       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2767     }
2768   GTY ((desc ("%1.val_class"))) v;
2769 }
2770 dw_val_node;
2771
2772 /* Locations in memory are described using a sequence of stack machine
2773    operations.  */
2774
2775 typedef struct dw_loc_descr_struct GTY(())
2776 {
2777   dw_loc_descr_ref dw_loc_next;
2778   enum dwarf_location_atom dw_loc_opc;
2779   dw_val_node dw_loc_oprnd1;
2780   dw_val_node dw_loc_oprnd2;
2781   int dw_loc_addr;
2782 }
2783 dw_loc_descr_node;
2784
2785 /* Location lists are ranges + location descriptions for that range,
2786    so you can track variables that are in different places over
2787    their entire life.  */
2788 typedef struct dw_loc_list_struct GTY(())
2789 {
2790   dw_loc_list_ref dw_loc_next;
2791   const char *begin; /* Label for begin address of range */
2792   const char *end;  /* Label for end address of range */
2793   char *ll_symbol; /* Label for beginning of location list.
2794                       Only on head of list */
2795   const char *section; /* Section this loclist is relative to */
2796   dw_loc_descr_ref expr;
2797 } dw_loc_list_node;
2798
2799 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2800
2801 static const char *dwarf_stack_op_name (unsigned);
2802 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2803                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2804 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2805 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2806 static unsigned long size_of_locs (dw_loc_descr_ref);
2807 static void output_loc_operands (dw_loc_descr_ref);
2808 static void output_loc_sequence (dw_loc_descr_ref);
2809
2810 /* Convert a DWARF stack opcode into its string name.  */
2811
2812 static const char *
2813 dwarf_stack_op_name (unsigned int op)
2814 {
2815   switch (op)
2816     {
2817     case DW_OP_addr:
2818     case INTERNAL_DW_OP_tls_addr:
2819       return "DW_OP_addr";
2820     case DW_OP_deref:
2821       return "DW_OP_deref";
2822     case DW_OP_const1u:
2823       return "DW_OP_const1u";
2824     case DW_OP_const1s:
2825       return "DW_OP_const1s";
2826     case DW_OP_const2u:
2827       return "DW_OP_const2u";
2828     case DW_OP_const2s:
2829       return "DW_OP_const2s";
2830     case DW_OP_const4u:
2831       return "DW_OP_const4u";
2832     case DW_OP_const4s:
2833       return "DW_OP_const4s";
2834     case DW_OP_const8u:
2835       return "DW_OP_const8u";
2836     case DW_OP_const8s:
2837       return "DW_OP_const8s";
2838     case DW_OP_constu:
2839       return "DW_OP_constu";
2840     case DW_OP_consts:
2841       return "DW_OP_consts";
2842     case DW_OP_dup:
2843       return "DW_OP_dup";
2844     case DW_OP_drop:
2845       return "DW_OP_drop";
2846     case DW_OP_over:
2847       return "DW_OP_over";
2848     case DW_OP_pick:
2849       return "DW_OP_pick";
2850     case DW_OP_swap:
2851       return "DW_OP_swap";
2852     case DW_OP_rot:
2853       return "DW_OP_rot";
2854     case DW_OP_xderef:
2855       return "DW_OP_xderef";
2856     case DW_OP_abs:
2857       return "DW_OP_abs";
2858     case DW_OP_and:
2859       return "DW_OP_and";
2860     case DW_OP_div:
2861       return "DW_OP_div";
2862     case DW_OP_minus:
2863       return "DW_OP_minus";
2864     case DW_OP_mod:
2865       return "DW_OP_mod";
2866     case DW_OP_mul:
2867       return "DW_OP_mul";
2868     case DW_OP_neg:
2869       return "DW_OP_neg";
2870     case DW_OP_not:
2871       return "DW_OP_not";
2872     case DW_OP_or:
2873       return "DW_OP_or";
2874     case DW_OP_plus:
2875       return "DW_OP_plus";
2876     case DW_OP_plus_uconst:
2877       return "DW_OP_plus_uconst";
2878     case DW_OP_shl:
2879       return "DW_OP_shl";
2880     case DW_OP_shr:
2881       return "DW_OP_shr";
2882     case DW_OP_shra:
2883       return "DW_OP_shra";
2884     case DW_OP_xor:
2885       return "DW_OP_xor";
2886     case DW_OP_bra:
2887       return "DW_OP_bra";
2888     case DW_OP_eq:
2889       return "DW_OP_eq";
2890     case DW_OP_ge:
2891       return "DW_OP_ge";
2892     case DW_OP_gt:
2893       return "DW_OP_gt";
2894     case DW_OP_le:
2895       return "DW_OP_le";
2896     case DW_OP_lt:
2897       return "DW_OP_lt";
2898     case DW_OP_ne:
2899       return "DW_OP_ne";
2900     case DW_OP_skip:
2901       return "DW_OP_skip";
2902     case DW_OP_lit0:
2903       return "DW_OP_lit0";
2904     case DW_OP_lit1:
2905       return "DW_OP_lit1";
2906     case DW_OP_lit2:
2907       return "DW_OP_lit2";
2908     case DW_OP_lit3:
2909       return "DW_OP_lit3";
2910     case DW_OP_lit4:
2911       return "DW_OP_lit4";
2912     case DW_OP_lit5:
2913       return "DW_OP_lit5";
2914     case DW_OP_lit6:
2915       return "DW_OP_lit6";
2916     case DW_OP_lit7:
2917       return "DW_OP_lit7";
2918     case DW_OP_lit8:
2919       return "DW_OP_lit8";
2920     case DW_OP_lit9:
2921       return "DW_OP_lit9";
2922     case DW_OP_lit10:
2923       return "DW_OP_lit10";
2924     case DW_OP_lit11:
2925       return "DW_OP_lit11";
2926     case DW_OP_lit12:
2927       return "DW_OP_lit12";
2928     case DW_OP_lit13:
2929       return "DW_OP_lit13";
2930     case DW_OP_lit14:
2931       return "DW_OP_lit14";
2932     case DW_OP_lit15:
2933       return "DW_OP_lit15";
2934     case DW_OP_lit16:
2935       return "DW_OP_lit16";
2936     case DW_OP_lit17:
2937       return "DW_OP_lit17";
2938     case DW_OP_lit18:
2939       return "DW_OP_lit18";
2940     case DW_OP_lit19:
2941       return "DW_OP_lit19";
2942     case DW_OP_lit20:
2943       return "DW_OP_lit20";
2944     case DW_OP_lit21:
2945       return "DW_OP_lit21";
2946     case DW_OP_lit22:
2947       return "DW_OP_lit22";
2948     case DW_OP_lit23:
2949       return "DW_OP_lit23";
2950     case DW_OP_lit24:
2951       return "DW_OP_lit24";
2952     case DW_OP_lit25:
2953       return "DW_OP_lit25";
2954     case DW_OP_lit26:
2955       return "DW_OP_lit26";
2956     case DW_OP_lit27:
2957       return "DW_OP_lit27";
2958     case DW_OP_lit28:
2959       return "DW_OP_lit28";
2960     case DW_OP_lit29:
2961       return "DW_OP_lit29";
2962     case DW_OP_lit30:
2963       return "DW_OP_lit30";
2964     case DW_OP_lit31:
2965       return "DW_OP_lit31";
2966     case DW_OP_reg0:
2967       return "DW_OP_reg0";
2968     case DW_OP_reg1:
2969       return "DW_OP_reg1";
2970     case DW_OP_reg2:
2971       return "DW_OP_reg2";
2972     case DW_OP_reg3:
2973       return "DW_OP_reg3";
2974     case DW_OP_reg4:
2975       return "DW_OP_reg4";
2976     case DW_OP_reg5:
2977       return "DW_OP_reg5";
2978     case DW_OP_reg6:
2979       return "DW_OP_reg6";
2980     case DW_OP_reg7:
2981       return "DW_OP_reg7";
2982     case DW_OP_reg8:
2983       return "DW_OP_reg8";
2984     case DW_OP_reg9:
2985       return "DW_OP_reg9";
2986     case DW_OP_reg10:
2987       return "DW_OP_reg10";
2988     case DW_OP_reg11:
2989       return "DW_OP_reg11";
2990     case DW_OP_reg12:
2991       return "DW_OP_reg12";
2992     case DW_OP_reg13:
2993       return "DW_OP_reg13";
2994     case DW_OP_reg14:
2995       return "DW_OP_reg14";
2996     case DW_OP_reg15:
2997       return "DW_OP_reg15";
2998     case DW_OP_reg16:
2999       return "DW_OP_reg16";
3000     case DW_OP_reg17:
3001       return "DW_OP_reg17";
3002     case DW_OP_reg18:
3003       return "DW_OP_reg18";
3004     case DW_OP_reg19:
3005       return "DW_OP_reg19";
3006     case DW_OP_reg20:
3007       return "DW_OP_reg20";
3008     case DW_OP_reg21:
3009       return "DW_OP_reg21";
3010     case DW_OP_reg22:
3011       return "DW_OP_reg22";
3012     case DW_OP_reg23:
3013       return "DW_OP_reg23";
3014     case DW_OP_reg24:
3015       return "DW_OP_reg24";
3016     case DW_OP_reg25:
3017       return "DW_OP_reg25";
3018     case DW_OP_reg26:
3019       return "DW_OP_reg26";
3020     case DW_OP_reg27:
3021       return "DW_OP_reg27";
3022     case DW_OP_reg28:
3023       return "DW_OP_reg28";
3024     case DW_OP_reg29:
3025       return "DW_OP_reg29";
3026     case DW_OP_reg30:
3027       return "DW_OP_reg30";
3028     case DW_OP_reg31:
3029       return "DW_OP_reg31";
3030     case DW_OP_breg0:
3031       return "DW_OP_breg0";
3032     case DW_OP_breg1:
3033       return "DW_OP_breg1";
3034     case DW_OP_breg2:
3035       return "DW_OP_breg2";
3036     case DW_OP_breg3:
3037       return "DW_OP_breg3";
3038     case DW_OP_breg4:
3039       return "DW_OP_breg4";
3040     case DW_OP_breg5:
3041       return "DW_OP_breg5";
3042     case DW_OP_breg6:
3043       return "DW_OP_breg6";
3044     case DW_OP_breg7:
3045       return "DW_OP_breg7";
3046     case DW_OP_breg8:
3047       return "DW_OP_breg8";
3048     case DW_OP_breg9:
3049       return "DW_OP_breg9";
3050     case DW_OP_breg10:
3051       return "DW_OP_breg10";
3052     case DW_OP_breg11:
3053       return "DW_OP_breg11";
3054     case DW_OP_breg12:
3055       return "DW_OP_breg12";
3056     case DW_OP_breg13:
3057       return "DW_OP_breg13";
3058     case DW_OP_breg14:
3059       return "DW_OP_breg14";
3060     case DW_OP_breg15:
3061       return "DW_OP_breg15";
3062     case DW_OP_breg16:
3063       return "DW_OP_breg16";
3064     case DW_OP_breg17:
3065       return "DW_OP_breg17";
3066     case DW_OP_breg18:
3067       return "DW_OP_breg18";
3068     case DW_OP_breg19:
3069       return "DW_OP_breg19";
3070     case DW_OP_breg20:
3071       return "DW_OP_breg20";
3072     case DW_OP_breg21:
3073       return "DW_OP_breg21";
3074     case DW_OP_breg22:
3075       return "DW_OP_breg22";
3076     case DW_OP_breg23:
3077       return "DW_OP_breg23";
3078     case DW_OP_breg24:
3079       return "DW_OP_breg24";
3080     case DW_OP_breg25:
3081       return "DW_OP_breg25";
3082     case DW_OP_breg26:
3083       return "DW_OP_breg26";
3084     case DW_OP_breg27:
3085       return "DW_OP_breg27";
3086     case DW_OP_breg28:
3087       return "DW_OP_breg28";
3088     case DW_OP_breg29:
3089       return "DW_OP_breg29";
3090     case DW_OP_breg30:
3091       return "DW_OP_breg30";
3092     case DW_OP_breg31:
3093       return "DW_OP_breg31";
3094     case DW_OP_regx:
3095       return "DW_OP_regx";
3096     case DW_OP_fbreg:
3097       return "DW_OP_fbreg";
3098     case DW_OP_bregx:
3099       return "DW_OP_bregx";
3100     case DW_OP_piece:
3101       return "DW_OP_piece";
3102     case DW_OP_deref_size:
3103       return "DW_OP_deref_size";
3104     case DW_OP_xderef_size:
3105       return "DW_OP_xderef_size";
3106     case DW_OP_nop:
3107       return "DW_OP_nop";
3108     case DW_OP_push_object_address:
3109       return "DW_OP_push_object_address";
3110     case DW_OP_call2:
3111       return "DW_OP_call2";
3112     case DW_OP_call4:
3113       return "DW_OP_call4";
3114     case DW_OP_call_ref:
3115       return "DW_OP_call_ref";
3116     case DW_OP_GNU_push_tls_address:
3117       return "DW_OP_GNU_push_tls_address";
3118     default:
3119       return "OP_<unknown>";
3120     }
3121 }
3122
3123 /* Return a pointer to a newly allocated location description.  Location
3124    descriptions are simple expression terms that can be strung
3125    together to form more complicated location (address) descriptions.  */
3126
3127 static inline dw_loc_descr_ref
3128 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3129                unsigned HOST_WIDE_INT oprnd2)
3130 {
3131   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3132
3133   descr->dw_loc_opc = op;
3134   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3135   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3136   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3137   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3138
3139   return descr;
3140 }
3141
3142 /* Add a location description term to a location description expression.  */
3143
3144 static inline void
3145 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3146 {
3147   dw_loc_descr_ref *d;
3148
3149   /* Find the end of the chain.  */
3150   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3151     ;
3152
3153   *d = descr;
3154 }
3155
3156 /* Return the size of a location descriptor.  */
3157
3158 static unsigned long
3159 size_of_loc_descr (dw_loc_descr_ref loc)
3160 {
3161   unsigned long size = 1;
3162
3163   switch (loc->dw_loc_opc)
3164     {
3165     case DW_OP_addr:
3166     case INTERNAL_DW_OP_tls_addr:
3167       size += DWARF2_ADDR_SIZE;
3168       break;
3169     case DW_OP_const1u:
3170     case DW_OP_const1s:
3171       size += 1;
3172       break;
3173     case DW_OP_const2u:
3174     case DW_OP_const2s:
3175       size += 2;
3176       break;
3177     case DW_OP_const4u:
3178     case DW_OP_const4s:
3179       size += 4;
3180       break;
3181     case DW_OP_const8u:
3182     case DW_OP_const8s:
3183       size += 8;
3184       break;
3185     case DW_OP_constu:
3186       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3187       break;
3188     case DW_OP_consts:
3189       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3190       break;
3191     case DW_OP_pick:
3192       size += 1;
3193       break;
3194     case DW_OP_plus_uconst:
3195       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3196       break;
3197     case DW_OP_skip:
3198     case DW_OP_bra:
3199       size += 2;
3200       break;
3201     case DW_OP_breg0:
3202     case DW_OP_breg1:
3203     case DW_OP_breg2:
3204     case DW_OP_breg3:
3205     case DW_OP_breg4:
3206     case DW_OP_breg5:
3207     case DW_OP_breg6:
3208     case DW_OP_breg7:
3209     case DW_OP_breg8:
3210     case DW_OP_breg9:
3211     case DW_OP_breg10:
3212     case DW_OP_breg11:
3213     case DW_OP_breg12:
3214     case DW_OP_breg13:
3215     case DW_OP_breg14:
3216     case DW_OP_breg15:
3217     case DW_OP_breg16:
3218     case DW_OP_breg17:
3219     case DW_OP_breg18:
3220     case DW_OP_breg19:
3221     case DW_OP_breg20:
3222     case DW_OP_breg21:
3223     case DW_OP_breg22:
3224     case DW_OP_breg23:
3225     case DW_OP_breg24:
3226     case DW_OP_breg25:
3227     case DW_OP_breg26:
3228     case DW_OP_breg27:
3229     case DW_OP_breg28:
3230     case DW_OP_breg29:
3231     case DW_OP_breg30:
3232     case DW_OP_breg31:
3233       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3234       break;
3235     case DW_OP_regx:
3236       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3237       break;
3238     case DW_OP_fbreg:
3239       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3240       break;
3241     case DW_OP_bregx:
3242       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3243       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3244       break;
3245     case DW_OP_piece:
3246       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3247       break;
3248     case DW_OP_deref_size:
3249     case DW_OP_xderef_size:
3250       size += 1;
3251       break;
3252     case DW_OP_call2:
3253       size += 2;
3254       break;
3255     case DW_OP_call4:
3256       size += 4;
3257       break;
3258     case DW_OP_call_ref:
3259       size += DWARF2_ADDR_SIZE;
3260       break;
3261     default:
3262       break;
3263     }
3264
3265   return size;
3266 }
3267
3268 /* Return the size of a series of location descriptors.  */
3269
3270 static unsigned long
3271 size_of_locs (dw_loc_descr_ref loc)
3272 {
3273   dw_loc_descr_ref l;
3274   unsigned long size;
3275
3276   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3277      field, to avoid writing to a PCH file.  */
3278   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3279     {
3280       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3281         break;
3282       size += size_of_loc_descr (l);
3283     }
3284   if (! l)
3285     return size;
3286
3287   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3288     {
3289       l->dw_loc_addr = size;
3290       size += size_of_loc_descr (l);
3291     }
3292
3293   return size;
3294 }
3295
3296 /* Output location description stack opcode's operands (if any).  */
3297
3298 static void
3299 output_loc_operands (dw_loc_descr_ref loc)
3300 {
3301   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3302   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3303
3304   switch (loc->dw_loc_opc)
3305     {
3306 #ifdef DWARF2_DEBUGGING_INFO
3307     case DW_OP_addr:
3308       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3309       break;
3310     case DW_OP_const2u:
3311     case DW_OP_const2s:
3312       dw2_asm_output_data (2, val1->v.val_int, NULL);
3313       break;
3314     case DW_OP_const4u:
3315     case DW_OP_const4s:
3316       dw2_asm_output_data (4, val1->v.val_int, NULL);
3317       break;
3318     case DW_OP_const8u:
3319     case DW_OP_const8s:
3320       gcc_assert (HOST_BITS_PER_LONG >= 64);
3321       dw2_asm_output_data (8, val1->v.val_int, NULL);
3322       break;
3323     case DW_OP_skip:
3324     case DW_OP_bra:
3325       {
3326         int offset;
3327
3328         gcc_assert (val1->val_class == dw_val_class_loc);
3329         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3330
3331         dw2_asm_output_data (2, offset, NULL);
3332       }
3333       break;
3334 #else
3335     case DW_OP_addr:
3336     case DW_OP_const2u:
3337     case DW_OP_const2s:
3338     case DW_OP_const4u:
3339     case DW_OP_const4s:
3340     case DW_OP_const8u:
3341     case DW_OP_const8s:
3342     case DW_OP_skip:
3343     case DW_OP_bra:
3344       /* We currently don't make any attempt to make sure these are
3345          aligned properly like we do for the main unwind info, so
3346          don't support emitting things larger than a byte if we're
3347          only doing unwinding.  */
3348       gcc_unreachable ();
3349 #endif
3350     case DW_OP_const1u:
3351     case DW_OP_const1s:
3352       dw2_asm_output_data (1, val1->v.val_int, NULL);
3353       break;
3354     case DW_OP_constu:
3355       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3356       break;
3357     case DW_OP_consts:
3358       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3359       break;
3360     case DW_OP_pick:
3361       dw2_asm_output_data (1, val1->v.val_int, NULL);
3362       break;
3363     case DW_OP_plus_uconst:
3364       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3365       break;
3366     case DW_OP_breg0:
3367     case DW_OP_breg1:
3368     case DW_OP_breg2:
3369     case DW_OP_breg3:
3370     case DW_OP_breg4:
3371     case DW_OP_breg5:
3372     case DW_OP_breg6:
3373     case DW_OP_breg7:
3374     case DW_OP_breg8:
3375     case DW_OP_breg9:
3376     case DW_OP_breg10:
3377     case DW_OP_breg11:
3378     case DW_OP_breg12:
3379     case DW_OP_breg13:
3380     case DW_OP_breg14:
3381     case DW_OP_breg15:
3382     case DW_OP_breg16:
3383     case DW_OP_breg17:
3384     case DW_OP_breg18:
3385     case DW_OP_breg19:
3386     case DW_OP_breg20:
3387     case DW_OP_breg21:
3388     case DW_OP_breg22:
3389     case DW_OP_breg23:
3390     case DW_OP_breg24:
3391     case DW_OP_breg25:
3392     case DW_OP_breg26:
3393     case DW_OP_breg27:
3394     case DW_OP_breg28:
3395     case DW_OP_breg29:
3396     case DW_OP_breg30:
3397     case DW_OP_breg31:
3398       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3399       break;
3400     case DW_OP_regx:
3401       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3402       break;
3403     case DW_OP_fbreg:
3404       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3405       break;
3406     case DW_OP_bregx:
3407       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3408       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3409       break;
3410     case DW_OP_piece:
3411       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3412       break;
3413     case DW_OP_deref_size:
3414     case DW_OP_xderef_size:
3415       dw2_asm_output_data (1, val1->v.val_int, NULL);
3416       break;
3417
3418     case INTERNAL_DW_OP_tls_addr:
3419       if (targetm.asm_out.output_dwarf_dtprel)
3420         {
3421           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3422                                                DWARF2_ADDR_SIZE,
3423                                                val1->v.val_addr);
3424           fputc ('\n', asm_out_file);
3425         }
3426       else
3427         gcc_unreachable ();
3428       break;
3429
3430     default:
3431       /* Other codes have no operands.  */
3432       break;
3433     }
3434 }
3435
3436 /* Output a sequence of location operations.  */
3437
3438 static void
3439 output_loc_sequence (dw_loc_descr_ref loc)
3440 {
3441   for (; loc != NULL; loc = loc->dw_loc_next)
3442     {
3443       /* Output the opcode.  */
3444       dw2_asm_output_data (1, loc->dw_loc_opc,
3445                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3446
3447       /* Output the operand(s) (if any).  */
3448       output_loc_operands (loc);
3449     }
3450 }
3451
3452 /* This routine will generate the correct assembly data for a location
3453    description based on a cfi entry with a complex address.  */
3454
3455 static void
3456 output_cfa_loc (dw_cfi_ref cfi)
3457 {
3458   dw_loc_descr_ref loc;
3459   unsigned long size;
3460
3461   /* Output the size of the block.  */
3462   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3463   size = size_of_locs (loc);
3464   dw2_asm_output_data_uleb128 (size, NULL);
3465
3466   /* Now output the operations themselves.  */
3467   output_loc_sequence (loc);
3468 }
3469
3470 /* This function builds a dwarf location descriptor sequence from a
3471    dw_cfa_location, adding the given OFFSET to the result of the
3472    expression.  */
3473
3474 static struct dw_loc_descr_struct *
3475 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3476 {
3477   struct dw_loc_descr_struct *head, *tmp;
3478
3479   offset += cfa->offset;
3480
3481   if (cfa->indirect)
3482     {
3483       if (cfa->base_offset)
3484         {
3485           if (cfa->reg <= 31)
3486             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3487           else
3488             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3489         }
3490       else if (cfa->reg <= 31)
3491         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3492       else
3493         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3494
3495       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3496       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3497       add_loc_descr (&head, tmp);
3498       if (offset != 0)
3499         {
3500           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3501           add_loc_descr (&head, tmp);
3502         }
3503     }
3504   else
3505     {
3506       if (offset == 0)
3507         if (cfa->reg <= 31)
3508           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3509         else
3510           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3511       else if (cfa->reg <= 31)
3512         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3513       else
3514         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3515     }
3516
3517   return head;
3518 }
3519
3520 /* This function fills in aa dw_cfa_location structure from a dwarf location
3521    descriptor sequence.  */
3522
3523 static void
3524 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3525 {
3526   struct dw_loc_descr_struct *ptr;
3527   cfa->offset = 0;
3528   cfa->base_offset = 0;
3529   cfa->indirect = 0;
3530   cfa->reg = -1;
3531
3532   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3533     {
3534       enum dwarf_location_atom op = ptr->dw_loc_opc;
3535
3536       switch (op)
3537         {
3538         case DW_OP_reg0:
3539         case DW_OP_reg1:
3540         case DW_OP_reg2:
3541         case DW_OP_reg3:
3542         case DW_OP_reg4:
3543         case DW_OP_reg5:
3544         case DW_OP_reg6:
3545         case DW_OP_reg7:
3546         case DW_OP_reg8:
3547         case DW_OP_reg9:
3548         case DW_OP_reg10:
3549         case DW_OP_reg11:
3550         case DW_OP_reg12:
3551         case DW_OP_reg13:
3552         case DW_OP_reg14:
3553         case DW_OP_reg15:
3554         case DW_OP_reg16:
3555         case DW_OP_reg17:
3556         case DW_OP_reg18:
3557         case DW_OP_reg19:
3558         case DW_OP_reg20:
3559         case DW_OP_reg21:
3560         case DW_OP_reg22:
3561         case DW_OP_reg23:
3562         case DW_OP_reg24:
3563         case DW_OP_reg25:
3564         case DW_OP_reg26:
3565         case DW_OP_reg27:
3566         case DW_OP_reg28:
3567         case DW_OP_reg29:
3568         case DW_OP_reg30:
3569         case DW_OP_reg31:
3570           cfa->reg = op - DW_OP_reg0;
3571           break;
3572         case DW_OP_regx:
3573           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3574           break;
3575         case DW_OP_breg0:
3576         case DW_OP_breg1:
3577         case DW_OP_breg2:
3578         case DW_OP_breg3:
3579         case DW_OP_breg4:
3580         case DW_OP_breg5:
3581         case DW_OP_breg6:
3582         case DW_OP_breg7:
3583         case DW_OP_breg8:
3584         case DW_OP_breg9:
3585         case DW_OP_breg10:
3586         case DW_OP_breg11:
3587         case DW_OP_breg12:
3588         case DW_OP_breg13:
3589         case DW_OP_breg14:
3590         case DW_OP_breg15:
3591         case DW_OP_breg16:
3592         case DW_OP_breg17:
3593         case DW_OP_breg18:
3594         case DW_OP_breg19:
3595         case DW_OP_breg20:
3596         case DW_OP_breg21:
3597         case DW_OP_breg22:
3598         case DW_OP_breg23:
3599         case DW_OP_breg24:
3600         case DW_OP_breg25:
3601         case DW_OP_breg26:
3602         case DW_OP_breg27:
3603         case DW_OP_breg28:
3604         case DW_OP_breg29:
3605         case DW_OP_breg30:
3606         case DW_OP_breg31:
3607           cfa->reg = op - DW_OP_breg0;
3608           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3609           break;
3610         case DW_OP_bregx:
3611           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3612           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3613           break;
3614         case DW_OP_deref:
3615           cfa->indirect = 1;
3616           break;
3617         case DW_OP_plus_uconst:
3618           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3619           break;
3620         default:
3621           internal_error ("DW_LOC_OP %s not implemented",
3622                           dwarf_stack_op_name (ptr->dw_loc_opc));
3623         }
3624     }
3625 }
3626 #endif /* .debug_frame support */
3627 \f
3628 /* And now, the support for symbolic debugging information.  */
3629 #ifdef DWARF2_DEBUGGING_INFO
3630
3631 /* .debug_str support.  */
3632 static int output_indirect_string (void **, void *);
3633
3634 static void dwarf2out_init (const char *);
3635 static void dwarf2out_finish (const char *);
3636 static void dwarf2out_define (unsigned int, const char *);
3637 static void dwarf2out_undef (unsigned int, const char *);
3638 static void dwarf2out_start_source_file (unsigned, const char *);
3639 static void dwarf2out_end_source_file (unsigned);
3640 static void dwarf2out_begin_block (unsigned, unsigned);
3641 static void dwarf2out_end_block (unsigned, unsigned);
3642 static bool dwarf2out_ignore_block (tree);
3643 static void dwarf2out_global_decl (tree);
3644 static void dwarf2out_type_decl (tree, int);
3645 static void dwarf2out_imported_module_or_decl (tree, tree);
3646 static void dwarf2out_abstract_function (tree);
3647 static void dwarf2out_var_location (rtx);
3648 static void dwarf2out_begin_function (tree);
3649 static void dwarf2out_switch_text_section (void);
3650
3651 /* The debug hooks structure.  */
3652
3653 const struct gcc_debug_hooks dwarf2_debug_hooks =
3654 {
3655   dwarf2out_init,
3656   dwarf2out_finish,
3657   dwarf2out_define,
3658   dwarf2out_undef,
3659   dwarf2out_start_source_file,
3660   dwarf2out_end_source_file,
3661   dwarf2out_begin_block,
3662   dwarf2out_end_block,
3663   dwarf2out_ignore_block,
3664   dwarf2out_source_line,
3665   dwarf2out_begin_prologue,
3666   debug_nothing_int_charstar,   /* end_prologue */
3667   dwarf2out_end_epilogue,
3668   dwarf2out_begin_function,
3669   debug_nothing_int,            /* end_function */
3670   dwarf2out_decl,               /* function_decl */
3671   dwarf2out_global_decl,
3672   dwarf2out_type_decl,          /* type_decl */
3673   dwarf2out_imported_module_or_decl,
3674   debug_nothing_tree,           /* deferred_inline_function */
3675   /* The DWARF 2 backend tries to reduce debugging bloat by not
3676      emitting the abstract description of inline functions until
3677      something tries to reference them.  */
3678   dwarf2out_abstract_function,  /* outlining_inline_function */
3679   debug_nothing_rtx,            /* label */
3680   debug_nothing_int,            /* handle_pch */
3681   dwarf2out_var_location,
3682   dwarf2out_switch_text_section,
3683   1                             /* start_end_main_source_file */
3684 };
3685 #endif
3686 \f
3687 /* NOTE: In the comments in this file, many references are made to
3688    "Debugging Information Entries".  This term is abbreviated as `DIE'
3689    throughout the remainder of this file.  */
3690
3691 /* An internal representation of the DWARF output is built, and then
3692    walked to generate the DWARF debugging info.  The walk of the internal
3693    representation is done after the entire program has been compiled.
3694    The types below are used to describe the internal representation.  */
3695
3696 /* Various DIE's use offsets relative to the beginning of the
3697    .debug_info section to refer to each other.  */
3698
3699 typedef long int dw_offset;
3700
3701 /* Define typedefs here to avoid circular dependencies.  */
3702
3703 typedef struct dw_attr_struct *dw_attr_ref;
3704 typedef struct dw_line_info_struct *dw_line_info_ref;
3705 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3706 typedef struct pubname_struct *pubname_ref;
3707 typedef struct dw_ranges_struct *dw_ranges_ref;
3708
3709 /* Each entry in the line_info_table maintains the file and
3710    line number associated with the label generated for that
3711    entry.  The label gives the PC value associated with
3712    the line number entry.  */
3713
3714 typedef struct dw_line_info_struct GTY(())
3715 {
3716   unsigned long dw_file_num;
3717   unsigned long dw_line_num;
3718 }
3719 dw_line_info_entry;
3720
3721 /* Line information for functions in separate sections; each one gets its
3722    own sequence.  */
3723 typedef struct dw_separate_line_info_struct GTY(())
3724 {
3725   unsigned long dw_file_num;
3726   unsigned long dw_line_num;
3727   unsigned long function;
3728 }
3729 dw_separate_line_info_entry;
3730
3731 /* Each DIE attribute has a field specifying the attribute kind,
3732    a link to the next attribute in the chain, and an attribute value.
3733    Attributes are typically linked below the DIE they modify.  */
3734
3735 typedef struct dw_attr_struct GTY(())
3736 {
3737   enum dwarf_attribute dw_attr;
3738   dw_val_node dw_attr_val;
3739 }
3740 dw_attr_node;
3741
3742 DEF_VEC_O(dw_attr_node);
3743 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3744
3745 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3746    The children of each node form a circular list linked by
3747    die_sib.  die_child points to the node *before* the "first" child node.  */
3748
3749 typedef struct die_struct GTY(())
3750 {
3751   enum dwarf_tag die_tag;
3752   char *die_symbol;
3753   VEC(dw_attr_node,gc) * die_attr;
3754   dw_die_ref die_parent;
3755   dw_die_ref die_child;
3756   dw_die_ref die_sib;
3757   dw_die_ref die_definition; /* ref from a specification to its definition */
3758   dw_offset die_offset;
3759   unsigned long die_abbrev;
3760   int die_mark;
3761   /* Die is used and must not be pruned as unused.  */
3762   int die_perennial_p;
3763   unsigned int decl_id;
3764 }
3765 die_node;
3766
3767 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3768 #define FOR_EACH_CHILD(die, c, expr) do {       \
3769   c = die->die_child;                           \
3770   if (c) do {                                   \
3771     c = c->die_sib;                             \
3772     expr;                                       \
3773   } while (c != die->die_child);                \
3774 } while (0)
3775
3776 /* The pubname structure */
3777
3778 typedef struct pubname_struct GTY(())
3779 {
3780   dw_die_ref die;
3781   const char *name;
3782 }
3783 pubname_entry;
3784
3785 DEF_VEC_O(pubname_entry);
3786 DEF_VEC_ALLOC_O(pubname_entry, gc);
3787
3788 struct dw_ranges_struct GTY(())
3789 {
3790   int block_num;
3791 };
3792
3793 /* The limbo die list structure.  */
3794 typedef struct limbo_die_struct GTY(())
3795 {
3796   dw_die_ref die;
3797   tree created_for;
3798   struct limbo_die_struct *next;
3799 }
3800 limbo_die_node;
3801
3802 /* How to start an assembler comment.  */
3803 #ifndef ASM_COMMENT_START
3804 #define ASM_COMMENT_START ";#"
3805 #endif
3806
3807 /* Define a macro which returns nonzero for a TYPE_DECL which was
3808    implicitly generated for a tagged type.
3809
3810    Note that unlike the gcc front end (which generates a NULL named
3811    TYPE_DECL node for each complete tagged type, each array type, and
3812    each function type node created) the g++ front end generates a
3813    _named_ TYPE_DECL node for each tagged type node created.
3814    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3815    generate a DW_TAG_typedef DIE for them.  */
3816
3817 #define TYPE_DECL_IS_STUB(decl)                         \
3818   (DECL_NAME (decl) == NULL_TREE                        \
3819    || (DECL_ARTIFICIAL (decl)                           \
3820        && is_tagged_type (TREE_TYPE (decl))             \
3821        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3822            /* This is necessary for stub decls that     \
3823               appear in nested inline functions.  */    \
3824            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3825                && (decl_ultimate_origin (decl)          \
3826                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3827
3828 /* Information concerning the compilation unit's programming
3829    language, and compiler version.  */
3830
3831 /* Fixed size portion of the DWARF compilation unit header.  */
3832 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3833   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3834
3835 /* Fixed size portion of public names info.  */
3836 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3837
3838 /* Fixed size portion of the address range info.  */
3839 #define DWARF_ARANGES_HEADER_SIZE                                       \
3840   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3841                 DWARF2_ADDR_SIZE * 2)                                   \
3842    - DWARF_INITIAL_LENGTH_SIZE)
3843
3844 /* Size of padding portion in the address range info.  It must be
3845    aligned to twice the pointer size.  */
3846 #define DWARF_ARANGES_PAD_SIZE \
3847   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3848                 DWARF2_ADDR_SIZE * 2) \
3849    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3850
3851 /* Use assembler line directives if available.  */
3852 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3853 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3854 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3855 #else
3856 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3857 #endif
3858 #endif
3859
3860 /* Minimum line offset in a special line info. opcode.
3861    This value was chosen to give a reasonable range of values.  */
3862 #define DWARF_LINE_BASE  -10
3863
3864 /* First special line opcode - leave room for the standard opcodes.  */
3865 #define DWARF_LINE_OPCODE_BASE  10
3866
3867 /* Range of line offsets in a special line info. opcode.  */
3868 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3869
3870 /* Flag that indicates the initial value of the is_stmt_start flag.
3871    In the present implementation, we do not mark any lines as
3872    the beginning of a source statement, because that information
3873    is not made available by the GCC front-end.  */
3874 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3875
3876 #ifdef DWARF2_DEBUGGING_INFO
3877 /* This location is used by calc_die_sizes() to keep track
3878    the offset of each DIE within the .debug_info section.  */
3879 static unsigned long next_die_offset;
3880 #endif
3881
3882 /* Record the root of the DIE's built for the current compilation unit.  */
3883 static GTY(()) dw_die_ref comp_unit_die;
3884
3885 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3886 static GTY(()) limbo_die_node *limbo_die_list;
3887
3888 /* Filenames referenced by this compilation unit.  */
3889 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3890
3891 /* A hash table of references to DIE's that describe declarations.
3892    The key is a DECL_UID() which is a unique number identifying each decl.  */
3893 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3894
3895 /* Node of the variable location list.  */
3896 struct var_loc_node GTY ((chain_next ("%h.next")))
3897 {
3898   rtx GTY (()) var_loc_note;
3899   const char * GTY (()) label;
3900   const char * GTY (()) section_label;
3901   struct var_loc_node * GTY (()) next;
3902 };
3903
3904 /* Variable location list.  */
3905 struct var_loc_list_def GTY (())
3906 {
3907   struct var_loc_node * GTY (()) first;
3908
3909   /* Do not mark the last element of the chained list because
3910      it is marked through the chain.  */
3911   struct var_loc_node * GTY ((skip ("%h"))) last;
3912
3913   /* DECL_UID of the variable decl.  */
3914   unsigned int decl_id;
3915 };
3916 typedef struct var_loc_list_def var_loc_list;
3917
3918
3919 /* Table of decl location linked lists.  */
3920 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3921
3922 /* A pointer to the base of a list of references to DIE's that
3923    are uniquely identified by their tag, presence/absence of
3924    children DIE's, and list of attribute/value pairs.  */
3925 static GTY((length ("abbrev_die_table_allocated")))
3926   dw_die_ref *abbrev_die_table;
3927
3928 /* Number of elements currently allocated for abbrev_die_table.  */
3929 static GTY(()) unsigned abbrev_die_table_allocated;
3930
3931 /* Number of elements in type_die_table currently in use.  */
3932 static GTY(()) unsigned abbrev_die_table_in_use;
3933
3934 /* Size (in elements) of increments by which we may expand the
3935    abbrev_die_table.  */
3936 #define ABBREV_DIE_TABLE_INCREMENT 256
3937
3938 /* A pointer to the base of a table that contains line information
3939    for each source code line in .text in the compilation unit.  */
3940 static GTY((length ("line_info_table_allocated")))
3941      dw_line_info_ref line_info_table;
3942
3943 /* Number of elements currently allocated for line_info_table.  */
3944 static GTY(()) unsigned line_info_table_allocated;
3945
3946 /* Number of elements in line_info_table currently in use.  */
3947 static GTY(()) unsigned line_info_table_in_use;
3948
3949 /* True if the compilation unit places functions in more than one section.  */
3950 static GTY(()) bool have_multiple_function_sections = false;
3951
3952 /* A pointer to the base of a table that contains line information
3953    for each source code line outside of .text in the compilation unit.  */
3954 static GTY ((length ("separate_line_info_table_allocated")))
3955      dw_separate_line_info_ref separate_line_info_table;
3956
3957 /* Number of elements currently allocated for separate_line_info_table.  */
3958 static GTY(()) unsigned separate_line_info_table_allocated;
3959
3960 /* Number of elements in separate_line_info_table currently in use.  */
3961 static GTY(()) unsigned separate_line_info_table_in_use;
3962
3963 /* Size (in elements) of increments by which we may expand the
3964    line_info_table.  */
3965 #define LINE_INFO_TABLE_INCREMENT 1024
3966
3967 /* A pointer to the base of a table that contains a list of publicly
3968    accessible names.  */
3969 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3970
3971 /* A pointer to the base of a table that contains a list of publicly
3972    accessible types.  */
3973 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3974
3975 /* Array of dies for which we should generate .debug_arange info.  */
3976 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3977
3978 /* Number of elements currently allocated for arange_table.  */
3979 static GTY(()) unsigned arange_table_allocated;
3980
3981 /* Number of elements in arange_table currently in use.  */
3982 static GTY(()) unsigned arange_table_in_use;
3983
3984 /* Size (in elements) of increments by which we may expand the
3985    arange_table.  */
3986 #define ARANGE_TABLE_INCREMENT 64
3987
3988 /* Array of dies for which we should generate .debug_ranges info.  */
3989 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3990
3991 /* Number of elements currently allocated for ranges_table.  */
3992 static GTY(()) unsigned ranges_table_allocated;
3993
3994 /* Number of elements in ranges_table currently in use.  */
3995 static GTY(()) unsigned ranges_table_in_use;
3996
3997 /* Size (in elements) of increments by which we may expand the
3998    ranges_table.  */
3999 #define RANGES_TABLE_INCREMENT 64
4000
4001 /* Whether we have location lists that need outputting */
4002 static GTY(()) bool have_location_lists;
4003
4004 /* Unique label counter.  */
4005 static GTY(()) unsigned int loclabel_num;
4006
4007 #ifdef DWARF2_DEBUGGING_INFO
4008 /* Record whether the function being analyzed contains inlined functions.  */
4009 static int current_function_has_inlines;
4010 #endif
4011 #if 0 && defined (MIPS_DEBUGGING_INFO)
4012 static int comp_unit_has_inlines;
4013 #endif
4014
4015 /* The last file entry emitted by maybe_emit_file().  */
4016 static GTY(()) struct dwarf_file_data * last_emitted_file;
4017
4018 /* Number of internal labels generated by gen_internal_sym().  */
4019 static GTY(()) int label_num;
4020
4021 /* Cached result of previous call to lookup_filename.  */
4022 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4023
4024 #ifdef DWARF2_DEBUGGING_INFO
4025
4026 /* Offset from the "steady-state frame pointer" to the frame base,
4027    within the current function.  */
4028 static HOST_WIDE_INT frame_pointer_fb_offset;
4029
4030 /* Forward declarations for functions defined in this file.  */
4031
4032 static int is_pseudo_reg (rtx);
4033 static tree type_main_variant (tree);
4034 static int is_tagged_type (tree);
4035 static const char *dwarf_tag_name (unsigned);
4036 static const char *dwarf_attr_name (unsigned);
4037 static const char *dwarf_form_name (unsigned);
4038 static tree decl_ultimate_origin (tree);
4039 static tree block_ultimate_origin (tree);
4040 static tree decl_class_context (tree);
4041 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4042 static inline enum dw_val_class AT_class (dw_attr_ref);
4043 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4044 static inline unsigned AT_flag (dw_attr_ref);
4045 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4046 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4047 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4048 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4049 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4050                               unsigned long);
4051 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4052                                unsigned int, unsigned char *);
4053 static hashval_t debug_str_do_hash (const void *);
4054 static int debug_str_eq (const void *, const void *);
4055 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4056 static inline const char *AT_string (dw_attr_ref);
4057 static int AT_string_form (dw_attr_ref);
4058 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4059 static void add_AT_specification (dw_die_ref, dw_die_ref);
4060 static inline dw_die_ref AT_ref (dw_attr_ref);
4061 static inline int AT_ref_external (dw_attr_ref);
4062 static inline void set_AT_ref_external (dw_attr_ref, int);
4063 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4064 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4065 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4066 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4067                              dw_loc_list_ref);
4068 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4069 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4070 static inline rtx AT_addr (dw_attr_ref);
4071 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4072 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4073 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4074 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4075                            unsigned HOST_WIDE_INT);
4076 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4077                                unsigned long);
4078 static inline const char *AT_lbl (dw_attr_ref);
4079 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4080 static const char *get_AT_low_pc (dw_die_ref);
4081 static const char *get_AT_hi_pc (dw_die_ref);
4082 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4083 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4084 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4085 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4086 static bool is_c_family (void);
4087 static bool is_cxx (void);
4088 static bool is_java (void);
4089 static bool is_fortran (void);
4090 static bool is_ada (void);
4091 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4092 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4093 static void add_child_die (dw_die_ref, dw_die_ref);
4094 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4095 static dw_die_ref lookup_type_die (tree);
4096 static void equate_type_number_to_die (tree, dw_die_ref);
4097 static hashval_t decl_die_table_hash (const void *);
4098 static int decl_die_table_eq (const void *, const void *);
4099 static dw_die_ref lookup_decl_die (tree);
4100 static hashval_t decl_loc_table_hash (const void *);
4101 static int decl_loc_table_eq (const void *, const void *);
4102 static var_loc_list *lookup_decl_loc (tree);
4103 static void equate_decl_number_to_die (tree, dw_die_ref);
4104 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4105 static void print_spaces (FILE *);
4106 static void print_die (dw_die_ref, FILE *);
4107 static void print_dwarf_line_table (FILE *);
4108 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4109 static dw_die_ref pop_compile_unit (dw_die_ref);
4110 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4111 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4112 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4113 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4114 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4115 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4116 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4117 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4118 static void compute_section_prefix (dw_die_ref);
4119 static int is_type_die (dw_die_ref);
4120 static int is_comdat_die (dw_die_ref);
4121 static int is_symbol_die (dw_die_ref);
4122 static void assign_symbol_names (dw_die_ref);
4123 static void break_out_includes (dw_die_ref);
4124 static hashval_t htab_cu_hash (const void *);
4125 static int htab_cu_eq (const void *, const void *);
4126 static void htab_cu_del (void *);
4127 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4128 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4129 static void add_sibling_attributes (dw_die_ref);
4130 static void build_abbrev_table (dw_die_ref);
4131 static void output_location_lists (dw_die_ref);
4132 static int constant_size (long unsigned);
4133 static unsigned long size_of_die (dw_die_ref);
4134 static void calc_die_sizes (dw_die_ref);
4135 static void mark_dies (dw_die_ref);
4136 static void unmark_dies (dw_die_ref);
4137 static void unmark_all_dies (dw_die_ref);
4138 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4139 static unsigned long size_of_aranges (void);
4140 static enum dwarf_form value_format (dw_attr_ref);
4141 static void output_value_format (dw_attr_ref);
4142 static void output_abbrev_section (void);
4143 static void output_die_symbol (dw_die_ref);
4144 static void output_die (dw_die_ref);
4145 static void output_compilation_unit_header (void);
4146 static void output_comp_unit (dw_die_ref, int);
4147 static const char *dwarf2_name (tree, int);
4148 static void add_pubname (tree, dw_die_ref);
4149 static void add_pubtype (tree, dw_die_ref);
4150 static void output_pubnames (VEC (pubname_entry,gc) *);
4151 static void add_arange (tree, dw_die_ref);
4152 static void output_aranges (void);
4153 static unsigned int add_ranges (tree);
4154 static void output_ranges (void);
4155 static void output_line_info (void);
4156 static void output_file_names (void);
4157 static dw_die_ref base_type_die (tree);
4158 static int is_base_type (tree);
4159 static bool is_subrange_type (tree);
4160 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4161 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4162 static int type_is_enum (tree);
4163 static unsigned int dbx_reg_number (rtx);
4164 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4165 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4166 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4167 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4168 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4169 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4170 static int is_based_loc (rtx);
4171 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4172 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4173 static dw_loc_descr_ref loc_descriptor (rtx);
4174 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4175 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4176 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4177 static tree field_type (tree);
4178 static unsigned int simple_type_align_in_bits (tree);
4179 static unsigned int simple_decl_align_in_bits (tree);
4180 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4181 static HOST_WIDE_INT field_byte_offset (tree);
4182 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4183                                          dw_loc_descr_ref);
4184 static void add_data_member_location_attribute (dw_die_ref, tree);
4185 static void add_const_value_attribute (dw_die_ref, rtx);
4186 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4187 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4188 static void insert_float (rtx, unsigned char *);
4189 static rtx rtl_for_decl_location (tree);
4190 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4191                                                    enum dwarf_attribute);
4192 static void tree_add_const_value_attribute (dw_die_ref, tree);
4193 static void add_name_attribute (dw_die_ref, const char *);
4194 static void add_comp_dir_attribute (dw_die_ref);
4195 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4196 static void add_subscript_info (dw_die_ref, tree);
4197 static void add_byte_size_attribute (dw_die_ref, tree);
4198 static void add_bit_offset_attribute (dw_die_ref, tree);
4199 static void add_bit_size_attribute (dw_die_ref, tree);
4200 static void add_prototyped_attribute (dw_die_ref, tree);
4201 static void add_abstract_origin_attribute (dw_die_ref, tree);
4202 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4203 static void add_src_coords_attributes (dw_die_ref, tree);
4204 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4205 static void push_decl_scope (tree);
4206 static void pop_decl_scope (void);
4207 static dw_die_ref scope_die_for (tree, dw_die_ref);
4208 static inline int local_scope_p (dw_die_ref);
4209 static inline int class_or_namespace_scope_p (dw_die_ref);
4210 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4211 static void add_calling_convention_attribute (dw_die_ref, tree);
4212 static const char *type_tag (tree);
4213 static tree member_declared_type (tree);
4214 #if 0
4215 static const char *decl_start_label (tree);
4216 #endif
4217 static void gen_array_type_die (tree, dw_die_ref);
4218 #if 0
4219 static void gen_entry_point_die (tree, dw_die_ref);
4220 #endif
4221 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4222 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4223 static void gen_inlined_union_type_die (tree, dw_die_ref);
4224 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4225 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4226 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4227 static void gen_formal_types_die (tree, dw_die_ref);
4228 static void gen_subprogram_die (tree, dw_die_ref);
4229 static void gen_variable_die (tree, dw_die_ref);
4230 static void gen_label_die (tree, dw_die_ref);
4231 static void gen_lexical_block_die (tree, dw_die_ref, int);
4232 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4233 static void gen_field_die (tree, dw_die_ref);
4234 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4235 static dw_die_ref gen_compile_unit_die (const char *);
4236 static void gen_inheritance_die (tree, tree, dw_die_ref);
4237 static void gen_member_die (tree, dw_die_ref);
4238 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4239 static void gen_subroutine_type_die (tree, dw_die_ref);
4240 static void gen_typedef_die (tree, dw_die_ref);
4241 static void gen_type_die (tree, dw_die_ref);
4242 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4243 static void gen_block_die (tree, dw_die_ref, int);
4244 static void decls_for_scope (tree, dw_die_ref, int);
4245 static int is_redundant_typedef (tree);
4246 static void gen_namespace_die (tree);
4247 static void gen_decl_die (tree, dw_die_ref);
4248 static dw_die_ref force_decl_die (tree);
4249 static dw_die_ref force_type_die (tree);
4250 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4251 static void declare_in_namespace (tree, dw_die_ref);
4252 static struct dwarf_file_data * lookup_filename (const char *);
4253 static void retry_incomplete_types (void);
4254 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4255 static void splice_child_die (dw_die_ref, dw_die_ref);
4256 static int file_info_cmp (const void *, const void *);
4257 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4258                                      const char *, const char *, unsigned);
4259 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4260                                        const char *, const char *,
4261                                        const char *);
4262 static void output_loc_list (dw_loc_list_ref);
4263 static char *gen_internal_sym (const char *);
4264
4265 static void prune_unmark_dies (dw_die_ref);
4266 static void prune_unused_types_mark (dw_die_ref, int);
4267 static void prune_unused_types_walk (dw_die_ref);
4268 static void prune_unused_types_walk_attribs (dw_die_ref);
4269 static void prune_unused_types_prune (dw_die_ref);
4270 static void prune_unused_types (void);
4271 static int maybe_emit_file (struct dwarf_file_data *fd);
4272
4273 /* Section names used to hold DWARF debugging information.  */
4274 #ifndef DEBUG_INFO_SECTION
4275 #define DEBUG_INFO_SECTION      ".debug_info"
4276 #endif
4277 #ifndef DEBUG_ABBREV_SECTION
4278 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4279 #endif
4280 #ifndef DEBUG_ARANGES_SECTION
4281 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4282 #endif
4283 #ifndef DEBUG_MACINFO_SECTION
4284 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4285 #endif
4286 #ifndef DEBUG_LINE_SECTION
4287 #define DEBUG_LINE_SECTION      ".debug_line"
4288 #endif
4289 #ifndef DEBUG_LOC_SECTION
4290 #define DEBUG_LOC_SECTION       ".debug_loc"
4291 #endif
4292 #ifndef DEBUG_PUBNAMES_SECTION
4293 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4294 #endif
4295 #ifndef DEBUG_STR_SECTION
4296 #define DEBUG_STR_SECTION       ".debug_str"
4297 #endif
4298 #ifndef DEBUG_RANGES_SECTION
4299 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4300 #endif
4301
4302 /* Standard ELF section names for compiled code and data.  */
4303 #ifndef TEXT_SECTION_NAME
4304 #define TEXT_SECTION_NAME       ".text"
4305 #endif
4306
4307 /* Section flags for .debug_str section.  */
4308 #define DEBUG_STR_SECTION_FLAGS \
4309   (HAVE_GAS_SHF_MERGE && flag_merge_constants                   \
4310    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4311    : SECTION_DEBUG)
4312
4313 /* Labels we insert at beginning sections we can reference instead of
4314    the section names themselves.  */
4315
4316 #ifndef TEXT_SECTION_LABEL
4317 #define TEXT_SECTION_LABEL              "Ltext"
4318 #endif
4319 #ifndef COLD_TEXT_SECTION_LABEL
4320 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4321 #endif
4322 #ifndef DEBUG_LINE_SECTION_LABEL
4323 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4324 #endif
4325 #ifndef DEBUG_INFO_SECTION_LABEL
4326 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4327 #endif
4328 #ifndef DEBUG_ABBREV_SECTION_LABEL
4329 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4330 #endif
4331 #ifndef DEBUG_LOC_SECTION_LABEL
4332 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4333 #endif
4334 #ifndef DEBUG_RANGES_SECTION_LABEL
4335 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4336 #endif
4337 #ifndef DEBUG_MACINFO_SECTION_LABEL
4338 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4339 #endif
4340
4341 /* Definitions of defaults for formats and names of various special
4342    (artificial) labels which may be generated within this file (when the -g
4343    options is used and DWARF2_DEBUGGING_INFO is in effect.
4344    If necessary, these may be overridden from within the tm.h file, but
4345    typically, overriding these defaults is unnecessary.  */
4346
4347 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4348 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4349 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4350 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES]; 
4351 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4352 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4353 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4354 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4355 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4356 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4357
4358 #ifndef TEXT_END_LABEL
4359 #define TEXT_END_LABEL          "Letext"
4360 #endif
4361 #ifndef COLD_END_LABEL
4362 #define COLD_END_LABEL          "Letext_cold"
4363 #endif
4364 #ifndef BLOCK_BEGIN_LABEL
4365 #define BLOCK_BEGIN_LABEL       "LBB"
4366 #endif
4367 #ifndef BLOCK_END_LABEL
4368 #define BLOCK_END_LABEL         "LBE"
4369 #endif
4370 #ifndef LINE_CODE_LABEL
4371 #define LINE_CODE_LABEL         "LM"
4372 #endif
4373 #ifndef SEPARATE_LINE_CODE_LABEL
4374 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4375 #endif
4376 \f
4377 /* We allow a language front-end to designate a function that is to be
4378    called to "demangle" any name before it is put into a DIE.  */
4379
4380 static const char *(*demangle_name_func) (const char *);
4381
4382 void
4383 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4384 {
4385   demangle_name_func = func;
4386 }
4387
4388 /* Test if rtl node points to a pseudo register.  */
4389
4390 static inline int
4391 is_pseudo_reg (rtx rtl)
4392 {
4393   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4394           || (GET_CODE (rtl) == SUBREG
4395               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4396 }
4397
4398 /* Return a reference to a type, with its const and volatile qualifiers
4399    removed.  */
4400
4401 static inline tree
4402 type_main_variant (tree type)
4403 {
4404   type = TYPE_MAIN_VARIANT (type);
4405
4406   /* ??? There really should be only one main variant among any group of
4407      variants of a given type (and all of the MAIN_VARIANT values for all
4408      members of the group should point to that one type) but sometimes the C
4409      front-end messes this up for array types, so we work around that bug
4410      here.  */
4411   if (TREE_CODE (type) == ARRAY_TYPE)
4412     while (type != TYPE_MAIN_VARIANT (type))
4413       type = TYPE_MAIN_VARIANT (type);
4414
4415   return type;
4416 }
4417
4418 /* Return nonzero if the given type node represents a tagged type.  */
4419
4420 static inline int
4421 is_tagged_type (tree type)
4422 {
4423   enum tree_code code = TREE_CODE (type);
4424
4425   return (code == RECORD_TYPE || code == UNION_TYPE
4426           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4427 }
4428
4429 /* Convert a DIE tag into its string name.  */
4430
4431 static const char *
4432 dwarf_tag_name (unsigned int tag)
4433 {
4434   switch (tag)
4435     {
4436     case DW_TAG_padding:
4437       return "DW_TAG_padding";
4438     case DW_TAG_array_type:
4439       return "DW_TAG_array_type";
4440     case DW_TAG_class_type:
4441       return "DW_TAG_class_type";
4442     case DW_TAG_entry_point:
4443       return "DW_TAG_entry_point";
4444     case DW_TAG_enumeration_type:
4445       return "DW_TAG_enumeration_type";
4446     case DW_TAG_formal_parameter:
4447       return "DW_TAG_formal_parameter";
4448     case DW_TAG_imported_declaration:
4449       return "DW_TAG_imported_declaration";
4450     case DW_TAG_label:
4451       return "DW_TAG_label";
4452     case DW_TAG_lexical_block:
4453       return "DW_TAG_lexical_block";
4454     case DW_TAG_member:
4455       return "DW_TAG_member";
4456     case DW_TAG_pointer_type:
4457       return "DW_TAG_pointer_type";
4458     case DW_TAG_reference_type:
4459       return "DW_TAG_reference_type";
4460     case DW_TAG_compile_unit:
4461       return "DW_TAG_compile_unit";
4462     case DW_TAG_string_type:
4463       return "DW_TAG_string_type";
4464     case DW_TAG_structure_type:
4465       return "DW_TAG_structure_type";
4466     case DW_TAG_subroutine_type:
4467       return "DW_TAG_subroutine_type";
4468     case DW_TAG_typedef:
4469       return "DW_TAG_typedef";
4470     case DW_TAG_union_type:
4471       return "DW_TAG_union_type";
4472     case DW_TAG_unspecified_parameters:
4473       return "DW_TAG_unspecified_parameters";
4474     case DW_TAG_variant:
4475       return "DW_TAG_variant";
4476     case DW_TAG_common_block:
4477       return "DW_TAG_common_block";
4478     case DW_TAG_common_inclusion:
4479       return "DW_TAG_common_inclusion";
4480     case DW_TAG_inheritance:
4481       return "DW_TAG_inheritance";
4482     case DW_TAG_inlined_subroutine:
4483       return "DW_TAG_inlined_subroutine";
4484     case DW_TAG_module:
4485       return "DW_TAG_module";
4486     case DW_TAG_ptr_to_member_type:
4487       return "DW_TAG_ptr_to_member_type";
4488     case DW_TAG_set_type:
4489       return "DW_TAG_set_type";
4490     case DW_TAG_subrange_type:
4491       return "DW_TAG_subrange_type";
4492     case DW_TAG_with_stmt:
4493       return "DW_TAG_with_stmt";
4494     case DW_TAG_access_declaration:
4495       return "DW_TAG_access_declaration";
4496     case DW_TAG_base_type:
4497       return "DW_TAG_base_type";
4498     case DW_TAG_catch_block:
4499       return "DW_TAG_catch_block";
4500     case DW_TAG_const_type:
4501       return "DW_TAG_const_type";
4502     case DW_TAG_constant:
4503       return "DW_TAG_constant";
4504     case DW_TAG_enumerator:
4505       return "DW_TAG_enumerator";
4506     case DW_TAG_file_type:
4507       return "DW_TAG_file_type";
4508     case DW_TAG_friend:
4509       return "DW_TAG_friend";
4510     case DW_TAG_namelist:
4511       return "DW_TAG_namelist";
4512     case DW_TAG_namelist_item:
4513       return "DW_TAG_namelist_item";
4514     case DW_TAG_namespace:
4515       return "DW_TAG_namespace";
4516     case DW_TAG_packed_type:
4517       return "DW_TAG_packed_type";
4518     case DW_TAG_subprogram:
4519       return "DW_TAG_subprogram";
4520     case DW_TAG_template_type_param:
4521       return "DW_TAG_template_type_param";
4522     case DW_TAG_template_value_param:
4523       return "DW_TAG_template_value_param";
4524     case DW_TAG_thrown_type:
4525       return "DW_TAG_thrown_type";
4526     case DW_TAG_try_block:
4527       return "DW_TAG_try_block";
4528     case DW_TAG_variant_part:
4529       return "DW_TAG_variant_part";
4530     case DW_TAG_variable:
4531       return "DW_TAG_variable";
4532     case DW_TAG_volatile_type:
4533       return "DW_TAG_volatile_type";
4534     case DW_TAG_imported_module:
4535       return "DW_TAG_imported_module";
4536     case DW_TAG_MIPS_loop:
4537       return "DW_TAG_MIPS_loop";
4538     case DW_TAG_format_label:
4539       return "DW_TAG_format_label";
4540     case DW_TAG_function_template:
4541       return "DW_TAG_function_template";
4542     case DW_TAG_class_template:
4543       return "DW_TAG_class_template";
4544     case DW_TAG_GNU_BINCL:
4545       return "DW_TAG_GNU_BINCL";
4546     case DW_TAG_GNU_EINCL:
4547       return "DW_TAG_GNU_EINCL";
4548     default:
4549       return "DW_TAG_<unknown>";
4550     }
4551 }
4552
4553 /* Convert a DWARF attribute code into its string name.  */
4554
4555 static const char *
4556 dwarf_attr_name (unsigned int attr)
4557 {
4558   switch (attr)
4559     {
4560     case DW_AT_sibling:
4561       return "DW_AT_sibling";
4562     case DW_AT_location:
4563       return "DW_AT_location";
4564     case DW_AT_name:
4565       return "DW_AT_name";
4566     case DW_AT_ordering:
4567       return "DW_AT_ordering";
4568     case DW_AT_subscr_data:
4569       return "DW_AT_subscr_data";
4570     case DW_AT_byte_size:
4571       return "DW_AT_byte_size";
4572     case DW_AT_bit_offset:
4573       return "DW_AT_bit_offset";
4574     case DW_AT_bit_size:
4575       return "DW_AT_bit_size";
4576     case DW_AT_element_list:
4577       return "DW_AT_element_list";
4578     case DW_AT_stmt_list:
4579       return "DW_AT_stmt_list";
4580     case DW_AT_low_pc:
4581       return "DW_AT_low_pc";
4582     case DW_AT_high_pc:
4583       return "DW_AT_high_pc";
4584     case DW_AT_language:
4585       return "DW_AT_language";
4586     case DW_AT_member:
4587       return "DW_AT_member";
4588     case DW_AT_discr:
4589       return "DW_AT_discr";
4590     case DW_AT_discr_value:
4591       return "DW_AT_discr_value";
4592     case DW_AT_visibility:
4593       return "DW_AT_visibility";
4594     case DW_AT_import:
4595       return "DW_AT_import";
4596     case DW_AT_string_length:
4597       return "DW_AT_string_length";
4598     case DW_AT_common_reference:
4599       return "DW_AT_common_reference";
4600     case DW_AT_comp_dir:
4601       return "DW_AT_comp_dir";
4602     case DW_AT_const_value:
4603       return "DW_AT_const_value";
4604     case DW_AT_containing_type:
4605       return "DW_AT_containing_type";
4606     case DW_AT_default_value:
4607       return "DW_AT_default_value";
4608     case DW_AT_inline:
4609       return "DW_AT_inline";
4610     case DW_AT_is_optional:
4611       return "DW_AT_is_optional";
4612     case DW_AT_lower_bound:
4613       return "DW_AT_lower_bound";
4614     case DW_AT_producer:
4615       return "DW_AT_producer";
4616     case DW_AT_prototyped:
4617       return "DW_AT_prototyped";
4618     case DW_AT_return_addr:
4619       return "DW_AT_return_addr";
4620     case DW_AT_start_scope:
4621       return "DW_AT_start_scope";
4622     case DW_AT_stride_size:
4623       return "DW_AT_stride_size";
4624     case DW_AT_upper_bound:
4625       return "DW_AT_upper_bound";
4626     case DW_AT_abstract_origin:
4627       return "DW_AT_abstract_origin";
4628     case DW_AT_accessibility:
4629       return "DW_AT_accessibility";
4630     case DW_AT_address_class:
4631       return "DW_AT_address_class";
4632     case DW_AT_artificial:
4633       return "DW_AT_artificial";
4634     case DW_AT_base_types:
4635       return "DW_AT_base_types";
4636     case DW_AT_calling_convention:
4637       return "DW_AT_calling_convention";
4638     case DW_AT_count:
4639       return "DW_AT_count";
4640     case DW_AT_data_member_location:
4641       return "DW_AT_data_member_location";
4642     case DW_AT_decl_column:
4643       return "DW_AT_decl_column";
4644     case DW_AT_decl_file:
4645       return "DW_AT_decl_file";
4646     case DW_AT_decl_line:
4647       return "DW_AT_decl_line";
4648     case DW_AT_declaration:
4649       return "DW_AT_declaration";
4650     case DW_AT_discr_list:
4651       return "DW_AT_discr_list";
4652     case DW_AT_encoding:
4653       return "DW_AT_encoding";
4654     case DW_AT_external:
4655       return "DW_AT_external";
4656     case DW_AT_frame_base:
4657       return "DW_AT_frame_base";
4658     case DW_AT_friend:
4659       return "DW_AT_friend";
4660     case DW_AT_identifier_case:
4661       return "DW_AT_identifier_case";
4662     case DW_AT_macro_info:
4663       return "DW_AT_macro_info";
4664     case DW_AT_namelist_items:
4665       return "DW_AT_namelist_items";
4666     case DW_AT_priority:
4667       return "DW_AT_priority";
4668     case DW_AT_segment:
4669       return "DW_AT_segment";
4670     case DW_AT_specification:
4671       return "DW_AT_specification";
4672     case DW_AT_static_link:
4673       return "DW_AT_static_link";
4674     case DW_AT_type:
4675       return "DW_AT_type";
4676     case DW_AT_use_location:
4677       return "DW_AT_use_location";
4678     case DW_AT_variable_parameter:
4679       return "DW_AT_variable_parameter";
4680     case DW_AT_virtuality:
4681       return "DW_AT_virtuality";
4682     case DW_AT_vtable_elem_location:
4683       return "DW_AT_vtable_elem_location";
4684
4685     case DW_AT_allocated:
4686       return "DW_AT_allocated";
4687     case DW_AT_associated:
4688       return "DW_AT_associated";
4689     case DW_AT_data_location:
4690       return "DW_AT_data_location";
4691     case DW_AT_stride:
4692       return "DW_AT_stride";
4693     case DW_AT_entry_pc:
4694       return "DW_AT_entry_pc";
4695     case DW_AT_use_UTF8:
4696       return "DW_AT_use_UTF8";
4697     case DW_AT_extension:
4698       return "DW_AT_extension";
4699     case DW_AT_ranges:
4700       return "DW_AT_ranges";
4701     case DW_AT_trampoline:
4702       return "DW_AT_trampoline";
4703     case DW_AT_call_column:
4704       return "DW_AT_call_column";
4705     case DW_AT_call_file:
4706       return "DW_AT_call_file";
4707     case DW_AT_call_line:
4708       return "DW_AT_call_line";
4709
4710     case DW_AT_MIPS_fde:
4711       return "DW_AT_MIPS_fde";
4712     case DW_AT_MIPS_loop_begin:
4713       return "DW_AT_MIPS_loop_begin";
4714     case DW_AT_MIPS_tail_loop_begin:
4715       return "DW_AT_MIPS_tail_loop_begin";
4716     case DW_AT_MIPS_epilog_begin:
4717       return "DW_AT_MIPS_epilog_begin";
4718     case DW_AT_MIPS_loop_unroll_factor:
4719       return "DW_AT_MIPS_loop_unroll_factor";
4720     case DW_AT_MIPS_software_pipeline_depth:
4721       return "DW_AT_MIPS_software_pipeline_depth";
4722     case DW_AT_MIPS_linkage_name:
4723       return "DW_AT_MIPS_linkage_name";
4724     case DW_AT_MIPS_stride:
4725       return "DW_AT_MIPS_stride";
4726     case DW_AT_MIPS_abstract_name:
4727       return "DW_AT_MIPS_abstract_name";
4728     case DW_AT_MIPS_clone_origin:
4729       return "DW_AT_MIPS_clone_origin";
4730     case DW_AT_MIPS_has_inlines:
4731       return "DW_AT_MIPS_has_inlines";
4732
4733     case DW_AT_sf_names:
4734       return "DW_AT_sf_names";
4735     case DW_AT_src_info:
4736       return "DW_AT_src_info";
4737     case DW_AT_mac_info:
4738       return "DW_AT_mac_info";
4739     case DW_AT_src_coords:
4740       return "DW_AT_src_coords";
4741     case DW_AT_body_begin:
4742       return "DW_AT_body_begin";
4743     case DW_AT_body_end:
4744       return "DW_AT_body_end";
4745     case DW_AT_GNU_vector:
4746       return "DW_AT_GNU_vector";
4747
4748     case DW_AT_VMS_rtnbeg_pd_address:
4749       return "DW_AT_VMS_rtnbeg_pd_address";
4750
4751     default:
4752       return "DW_AT_<unknown>";
4753     }
4754 }
4755
4756 /* Convert a DWARF value form code into its string name.  */
4757
4758 static const char *
4759 dwarf_form_name (unsigned int form)
4760 {
4761   switch (form)
4762     {
4763     case DW_FORM_addr:
4764       return "DW_FORM_addr";
4765     case DW_FORM_block2:
4766       return "DW_FORM_block2";
4767     case DW_FORM_block4:
4768       return "DW_FORM_block4";
4769     case DW_FORM_data2:
4770       return "DW_FORM_data2";
4771     case DW_FORM_data4:
4772       return "DW_FORM_data4";
4773     case DW_FORM_data8:
4774       return "DW_FORM_data8";
4775     case DW_FORM_string:
4776       return "DW_FORM_string";
4777     case DW_FORM_block:
4778       return "DW_FORM_block";
4779     case DW_FORM_block1:
4780       return "DW_FORM_block1";
4781     case DW_FORM_data1:
4782       return "DW_FORM_data1";
4783     case DW_FORM_flag:
4784       return "DW_FORM_flag";
4785     case DW_FORM_sdata:
4786       return "DW_FORM_sdata";
4787     case DW_FORM_strp:
4788       return "DW_FORM_strp";
4789     case DW_FORM_udata:
4790       return "DW_FORM_udata";
4791     case DW_FORM_ref_addr:
4792       return "DW_FORM_ref_addr";
4793     case DW_FORM_ref1:
4794       return "DW_FORM_ref1";
4795     case DW_FORM_ref2:
4796       return "DW_FORM_ref2";
4797     case DW_FORM_ref4:
4798       return "DW_FORM_ref4";
4799     case DW_FORM_ref8:
4800       return "DW_FORM_ref8";
4801     case DW_FORM_ref_udata:
4802       return "DW_FORM_ref_udata";
4803     case DW_FORM_indirect:
4804       return "DW_FORM_indirect";
4805     default:
4806       return "DW_FORM_<unknown>";
4807     }
4808 }
4809 \f
4810 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4811    instance of an inlined instance of a decl which is local to an inline
4812    function, so we have to trace all of the way back through the origin chain
4813    to find out what sort of node actually served as the original seed for the
4814    given block.  */
4815
4816 static tree
4817 decl_ultimate_origin (tree decl)
4818 {
4819   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4820     return NULL_TREE;
4821
4822   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4823      nodes in the function to point to themselves; ignore that if
4824      we're trying to output the abstract instance of this function.  */
4825   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4826     return NULL_TREE;
4827
4828   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4829      most distant ancestor, this should never happen.  */
4830   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4831
4832   return DECL_ABSTRACT_ORIGIN (decl);
4833 }
4834
4835 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4836    instance of an inlined instance of a block which is local to an inline
4837    function, so we have to trace all of the way back through the origin chain
4838    to find out what sort of node actually served as the original seed for the
4839    given block.  */
4840
4841 static tree
4842 block_ultimate_origin (tree block)
4843 {
4844   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4845
4846   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4847      nodes in the function to point to themselves; ignore that if
4848      we're trying to output the abstract instance of this function.  */
4849   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4850     return NULL_TREE;
4851
4852   if (immediate_origin == NULL_TREE)
4853     return NULL_TREE;
4854   else
4855     {
4856       tree ret_val;
4857       tree lookahead = immediate_origin;
4858
4859       do
4860         {
4861           ret_val = lookahead;
4862           lookahead = (TREE_CODE (ret_val) == BLOCK
4863                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4864         }
4865       while (lookahead != NULL && lookahead != ret_val);
4866       
4867       /* The block's abstract origin chain may not be the *ultimate* origin of
4868          the block. It could lead to a DECL that has an abstract origin set.
4869          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4870          will give us if it has one).  Note that DECL's abstract origins are
4871          supposed to be the most distant ancestor (or so decl_ultimate_origin
4872          claims), so we don't need to loop following the DECL origins.  */
4873       if (DECL_P (ret_val))
4874         return DECL_ORIGIN (ret_val);
4875
4876       return ret_val;
4877     }
4878 }
4879
4880 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4881    of a virtual function may refer to a base class, so we check the 'this'
4882    parameter.  */
4883
4884 static tree
4885 decl_class_context (tree decl)
4886 {
4887   tree context = NULL_TREE;
4888
4889   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4890     context = DECL_CONTEXT (decl);
4891   else
4892     context = TYPE_MAIN_VARIANT
4893       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4894
4895   if (context && !TYPE_P (context))
4896     context = NULL_TREE;
4897
4898   return context;
4899 }
4900 \f
4901 /* Add an attribute/value pair to a DIE.  */
4902
4903 static inline void
4904 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4905 {
4906   /* Maybe this should be an assert?  */
4907   if (die == NULL)
4908     return;
4909   
4910   if (die->die_attr == NULL)
4911     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4912   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4913 }
4914
4915 static inline enum dw_val_class
4916 AT_class (dw_attr_ref a)
4917 {
4918   return a->dw_attr_val.val_class;
4919 }
4920
4921 /* Add a flag value attribute to a DIE.  */
4922
4923 static inline void
4924 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4925 {
4926   dw_attr_node attr;
4927
4928   attr.dw_attr = attr_kind;
4929   attr.dw_attr_val.val_class = dw_val_class_flag;
4930   attr.dw_attr_val.v.val_flag = flag;
4931   add_dwarf_attr (die, &attr);
4932 }
4933
4934 static inline unsigned
4935 AT_flag (dw_attr_ref a)
4936 {
4937   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4938   return a->dw_attr_val.v.val_flag;
4939 }
4940
4941 /* Add a signed integer attribute value to a DIE.  */
4942
4943 static inline void
4944 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4945 {
4946   dw_attr_node attr;
4947
4948   attr.dw_attr = attr_kind;
4949   attr.dw_attr_val.val_class = dw_val_class_const;
4950   attr.dw_attr_val.v.val_int = int_val;
4951   add_dwarf_attr (die, &attr);
4952 }
4953
4954 static inline HOST_WIDE_INT
4955 AT_int (dw_attr_ref a)
4956 {
4957   gcc_assert (a && AT_class (a) == dw_val_class_const);
4958   return a->dw_attr_val.v.val_int;
4959 }
4960
4961 /* Add an unsigned integer attribute value to a DIE.  */
4962
4963 static inline void
4964 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4965                  unsigned HOST_WIDE_INT unsigned_val)
4966 {
4967   dw_attr_node attr;
4968
4969   attr.dw_attr = attr_kind;
4970   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4971   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4972   add_dwarf_attr (die, &attr);
4973 }
4974
4975 static inline unsigned HOST_WIDE_INT
4976 AT_unsigned (dw_attr_ref a)
4977 {
4978   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4979   return a->dw_attr_val.v.val_unsigned;
4980 }
4981
4982 /* Add an unsigned double integer attribute value to a DIE.  */
4983
4984 static inline void
4985 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4986                   long unsigned int val_hi, long unsigned int val_low)
4987 {
4988   dw_attr_node attr;
4989
4990   attr.dw_attr = attr_kind;
4991   attr.dw_attr_val.val_class = dw_val_class_long_long;
4992   attr.dw_attr_val.v.val_long_long.hi = val_hi;
4993   attr.dw_attr_val.v.val_long_long.low = val_low;
4994   add_dwarf_attr (die, &attr);
4995 }
4996
4997 /* Add a floating point attribute value to a DIE and return it.  */
4998
4999 static inline void
5000 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5001             unsigned int length, unsigned int elt_size, unsigned char *array)
5002 {
5003   dw_attr_node attr;
5004
5005   attr.dw_attr = attr_kind;
5006   attr.dw_attr_val.val_class = dw_val_class_vec;
5007   attr.dw_attr_val.v.val_vec.length = length;
5008   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5009   attr.dw_attr_val.v.val_vec.array = array;
5010   add_dwarf_attr (die, &attr);
5011 }
5012
5013 /* Hash and equality functions for debug_str_hash.  */
5014
5015 static hashval_t
5016 debug_str_do_hash (const void *x)
5017 {
5018   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5019 }
5020
5021 static int
5022 debug_str_eq (const void *x1, const void *x2)
5023 {
5024   return strcmp ((((const struct indirect_string_node *)x1)->str),
5025                  (const char *)x2) == 0;
5026 }
5027
5028 /* Add a string attribute value to a DIE.  */
5029
5030 static inline void
5031 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5032 {
5033   dw_attr_node attr;
5034   struct indirect_string_node *node;
5035   void **slot;
5036
5037   if (! debug_str_hash)
5038     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5039                                       debug_str_eq, NULL);
5040
5041   slot = htab_find_slot_with_hash (debug_str_hash, str,
5042                                    htab_hash_string (str), INSERT);
5043   if (*slot == NULL)
5044     {
5045       node = (struct indirect_string_node *)
5046                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5047       node->str = ggc_strdup (str);
5048       *slot = node;
5049     }
5050   else
5051     node = (struct indirect_string_node *) *slot;
5052
5053   node->refcount++;
5054
5055   attr.dw_attr = attr_kind;
5056   attr.dw_attr_val.val_class = dw_val_class_str;
5057   attr.dw_attr_val.v.val_str = node;
5058   add_dwarf_attr (die, &attr);
5059 }
5060
5061 static inline const char *
5062 AT_string (dw_attr_ref a)
5063 {
5064   gcc_assert (a && AT_class (a) == dw_val_class_str);
5065   return a->dw_attr_val.v.val_str->str;
5066 }
5067
5068 /* Find out whether a string should be output inline in DIE
5069    or out-of-line in .debug_str section.  */
5070
5071 static int
5072 AT_string_form (dw_attr_ref a)
5073 {
5074   struct indirect_string_node *node;
5075   unsigned int len;
5076   char label[32];
5077
5078   gcc_assert (a && AT_class (a) == dw_val_class_str);
5079
5080   node = a->dw_attr_val.v.val_str;
5081   if (node->form)
5082     return node->form;
5083
5084   len = strlen (node->str) + 1;
5085
5086   /* If the string is shorter or equal to the size of the reference, it is
5087      always better to put it inline.  */
5088   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5089     return node->form = DW_FORM_string;
5090
5091   /* If we cannot expect the linker to merge strings in .debug_str
5092      section, only put it into .debug_str if it is worth even in this
5093      single module.  */
5094   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5095       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5096     return node->form = DW_FORM_string;
5097
5098   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5099   ++dw2_string_counter;
5100   node->label = xstrdup (label);
5101
5102   return node->form = DW_FORM_strp;
5103 }
5104
5105 /* Add a DIE reference attribute value to a DIE.  */
5106
5107 static inline void
5108 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5109 {
5110   dw_attr_node attr;
5111
5112   attr.dw_attr = attr_kind;
5113   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5114   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5115   attr.dw_attr_val.v.val_die_ref.external = 0;
5116   add_dwarf_attr (die, &attr);
5117 }
5118
5119 /* Add an AT_specification attribute to a DIE, and also make the back
5120    pointer from the specification to the definition.  */
5121
5122 static inline void
5123 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5124 {
5125   add_AT_die_ref (die, DW_AT_specification, targ_die);
5126   gcc_assert (!targ_die->die_definition);
5127   targ_die->die_definition = die;
5128 }
5129
5130 static inline dw_die_ref
5131 AT_ref (dw_attr_ref a)
5132 {
5133   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5134   return a->dw_attr_val.v.val_die_ref.die;
5135 }
5136
5137 static inline int
5138 AT_ref_external (dw_attr_ref a)
5139 {
5140   if (a && AT_class (a) == dw_val_class_die_ref)
5141     return a->dw_attr_val.v.val_die_ref.external;
5142
5143   return 0;
5144 }
5145
5146 static inline void
5147 set_AT_ref_external (dw_attr_ref a, int i)
5148 {
5149   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5150   a->dw_attr_val.v.val_die_ref.external = i;
5151 }
5152
5153 /* Add an FDE reference attribute value to a DIE.  */
5154
5155 static inline void
5156 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5157 {
5158   dw_attr_node attr;
5159
5160   attr.dw_attr = attr_kind;
5161   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5162   attr.dw_attr_val.v.val_fde_index = targ_fde;
5163   add_dwarf_attr (die, &attr);
5164 }
5165
5166 /* Add a location description attribute value to a DIE.  */
5167
5168 static inline void
5169 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5170 {
5171   dw_attr_node attr;
5172
5173   attr.dw_attr = attr_kind;
5174   attr.dw_attr_val.val_class = dw_val_class_loc;
5175   attr.dw_attr_val.v.val_loc = loc;
5176   add_dwarf_attr (die, &attr);
5177 }
5178
5179 static inline dw_loc_descr_ref
5180 AT_loc (dw_attr_ref a)
5181 {
5182   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5183   return a->dw_attr_val.v.val_loc;
5184 }
5185
5186 static inline void
5187 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5188 {
5189   dw_attr_node attr;
5190
5191   attr.dw_attr = attr_kind;
5192   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5193   attr.dw_attr_val.v.val_loc_list = loc_list;
5194   add_dwarf_attr (die, &attr);
5195   have_location_lists = true;
5196 }
5197
5198 static inline dw_loc_list_ref
5199 AT_loc_list (dw_attr_ref a)
5200 {
5201   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5202   return a->dw_attr_val.v.val_loc_list;
5203 }
5204
5205 /* Add an address constant attribute value to a DIE.  */
5206
5207 static inline void
5208 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5209 {
5210   dw_attr_node attr;
5211
5212   attr.dw_attr = attr_kind;
5213   attr.dw_attr_val.val_class = dw_val_class_addr;
5214   attr.dw_attr_val.v.val_addr = addr;
5215   add_dwarf_attr (die, &attr);
5216 }
5217
5218 /* Get the RTX from to an address DIE attribute.  */
5219
5220 static inline rtx
5221 AT_addr (dw_attr_ref a)
5222 {
5223   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5224   return a->dw_attr_val.v.val_addr;
5225 }
5226
5227 /* Add a file attribute value to a DIE.  */
5228
5229 static inline void
5230 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5231              struct dwarf_file_data *fd)
5232 {
5233   dw_attr_node attr;
5234
5235   attr.dw_attr = attr_kind;
5236   attr.dw_attr_val.val_class = dw_val_class_file;
5237   attr.dw_attr_val.v.val_file = fd;
5238   add_dwarf_attr (die, &attr);
5239 }
5240
5241 /* Get the dwarf_file_data from a file DIE attribute.  */
5242
5243 static inline struct dwarf_file_data *
5244 AT_file (dw_attr_ref a)
5245 {
5246   gcc_assert (a && AT_class (a) == dw_val_class_file);
5247   return a->dw_attr_val.v.val_file;
5248 }
5249
5250 /* Add a label identifier attribute value to a DIE.  */
5251
5252 static inline void
5253 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5254 {
5255   dw_attr_node attr;
5256
5257   attr.dw_attr = attr_kind;
5258   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5259   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5260   add_dwarf_attr (die, &attr);
5261 }
5262
5263 /* Add a section offset attribute value to a DIE, an offset into the
5264    debug_line section.  */
5265
5266 static inline void
5267 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5268                 const char *label)
5269 {
5270   dw_attr_node attr;
5271
5272   attr.dw_attr = attr_kind;
5273   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5274   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5275   add_dwarf_attr (die, &attr);
5276 }
5277
5278 /* Add a section offset attribute value to a DIE, an offset into the
5279    debug_macinfo section.  */
5280
5281 static inline void
5282 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5283                const char *label)
5284 {
5285   dw_attr_node attr;
5286
5287   attr.dw_attr = attr_kind;
5288   attr.dw_attr_val.val_class = dw_val_class_macptr;
5289   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5290   add_dwarf_attr (die, &attr);
5291 }
5292
5293 /* Add an offset attribute value to a DIE.  */
5294
5295 static inline void
5296 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5297                unsigned HOST_WIDE_INT offset)
5298 {
5299   dw_attr_node attr;
5300
5301   attr.dw_attr = attr_kind;
5302   attr.dw_attr_val.val_class = dw_val_class_offset;
5303   attr.dw_attr_val.v.val_offset = offset;
5304   add_dwarf_attr (die, &attr);
5305 }
5306
5307 /* Add an range_list attribute value to a DIE.  */
5308
5309 static void
5310 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5311                    long unsigned int offset)
5312 {
5313   dw_attr_node attr;
5314
5315   attr.dw_attr = attr_kind;
5316   attr.dw_attr_val.val_class = dw_val_class_range_list;
5317   attr.dw_attr_val.v.val_offset = offset;
5318   add_dwarf_attr (die, &attr);
5319 }
5320
5321 static inline const char *
5322 AT_lbl (dw_attr_ref a)
5323 {
5324   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5325                     || AT_class (a) == dw_val_class_lineptr
5326                     || AT_class (a) == dw_val_class_macptr));
5327   return a->dw_attr_val.v.val_lbl_id;
5328 }
5329
5330 /* Get the attribute of type attr_kind.  */
5331
5332 static dw_attr_ref
5333 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5334 {
5335   dw_attr_ref a;
5336   unsigned ix;
5337   dw_die_ref spec = NULL;
5338
5339   if (! die)
5340     return NULL;
5341
5342   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5343     if (a->dw_attr == attr_kind)
5344       return a;
5345     else if (a->dw_attr == DW_AT_specification
5346              || a->dw_attr == DW_AT_abstract_origin)
5347       spec = AT_ref (a);
5348   
5349   if (spec)
5350     return get_AT (spec, attr_kind);
5351
5352   return NULL;
5353 }
5354
5355 /* Return the "low pc" attribute value, typically associated with a subprogram
5356    DIE.  Return null if the "low pc" attribute is either not present, or if it
5357    cannot be represented as an assembler label identifier.  */
5358
5359 static inline const char *
5360 get_AT_low_pc (dw_die_ref die)
5361 {
5362   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5363
5364   return a ? AT_lbl (a) : NULL;
5365 }
5366
5367 /* Return the "high pc" attribute value, typically associated with a subprogram
5368    DIE.  Return null if the "high pc" attribute is either not present, or if it
5369    cannot be represented as an assembler label identifier.  */
5370
5371 static inline const char *
5372 get_AT_hi_pc (dw_die_ref die)
5373 {
5374   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5375
5376   return a ? AT_lbl (a) : NULL;
5377 }
5378
5379 /* Return the value of the string attribute designated by ATTR_KIND, or
5380    NULL if it is not present.  */
5381
5382 static inline const char *
5383 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5384 {
5385   dw_attr_ref a = get_AT (die, attr_kind);
5386
5387   return a ? AT_string (a) : NULL;
5388 }
5389
5390 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5391    if it is not present.  */
5392
5393 static inline int
5394 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5395 {
5396   dw_attr_ref a = get_AT (die, attr_kind);
5397
5398   return a ? AT_flag (a) : 0;
5399 }
5400
5401 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5402    if it is not present.  */
5403
5404 static inline unsigned
5405 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5406 {
5407   dw_attr_ref a = get_AT (die, attr_kind);
5408
5409   return a ? AT_unsigned (a) : 0;
5410 }
5411
5412 static inline dw_die_ref
5413 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5414 {
5415   dw_attr_ref a = get_AT (die, attr_kind);
5416
5417   return a ? AT_ref (a) : NULL;
5418 }
5419
5420 static inline struct dwarf_file_data *
5421 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5422 {
5423   dw_attr_ref a = get_AT (die, attr_kind);
5424
5425   return a ? AT_file (a) : NULL;
5426 }
5427
5428 /* Return TRUE if the language is C or C++.  */
5429
5430 static inline bool
5431 is_c_family (void)
5432 {
5433   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5434
5435   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5436           || lang == DW_LANG_C99
5437           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5438 }
5439
5440 /* Return TRUE if the language is C++.  */
5441
5442 static inline bool
5443 is_cxx (void)
5444 {
5445   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5446   
5447   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5448 }
5449
5450 /* Return TRUE if the language is Fortran.  */
5451
5452 static inline bool
5453 is_fortran (void)
5454 {
5455   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5456
5457   return (lang == DW_LANG_Fortran77
5458           || lang == DW_LANG_Fortran90
5459           || lang == DW_LANG_Fortran95);
5460 }
5461
5462 /* Return TRUE if the language is Java.  */
5463
5464 static inline bool
5465 is_java (void)
5466 {
5467   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5468
5469   return lang == DW_LANG_Java;
5470 }
5471
5472 /* Return TRUE if the language is Ada.  */
5473
5474 static inline bool
5475 is_ada (void)
5476 {
5477   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5478
5479   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5480 }
5481
5482 /* Remove the specified attribute if present.  */
5483
5484 static void
5485 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5486 {
5487   dw_attr_ref a;
5488   unsigned ix;
5489
5490   if (! die)
5491     return;
5492
5493   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5494     if (a->dw_attr == attr_kind)
5495       {
5496         if (AT_class (a) == dw_val_class_str)
5497           if (a->dw_attr_val.v.val_str->refcount)
5498             a->dw_attr_val.v.val_str->refcount--;
5499
5500         /* VEC_ordered_remove should help reduce the number of abbrevs
5501            that are needed.  */
5502         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5503         return;
5504       }
5505 }
5506
5507 /* Remove CHILD from its parent.  PREV must have the property that
5508    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5509
5510 static void
5511 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5512 {
5513   gcc_assert (child->die_parent == prev->die_parent);
5514   gcc_assert (prev->die_sib == child);
5515   if (prev == child)
5516     {
5517       gcc_assert (child->die_parent->die_child == child);
5518       prev = NULL;
5519     }
5520   else
5521     prev->die_sib = child->die_sib;
5522   if (child->die_parent->die_child == child)
5523     child->die_parent->die_child = prev;
5524 }
5525
5526 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5527    matches TAG.  */
5528
5529 static void
5530 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5531 {
5532   dw_die_ref c;
5533   
5534   c = die->die_child;
5535   if (c) do {
5536     dw_die_ref prev = c;
5537     c = c->die_sib;
5538     while (c->die_tag == tag)
5539       {
5540         remove_child_with_prev (c, prev);
5541         /* Might have removed every child.  */
5542         if (c == c->die_sib)
5543           return;
5544         c = c->die_sib;
5545       }
5546   } while (c != die->die_child);
5547 }
5548
5549 /* Add a CHILD_DIE as the last child of DIE.  */
5550
5551 static void
5552 add_child_die (dw_die_ref die, dw_die_ref child_die)
5553 {
5554   /* FIXME this should probably be an assert.  */
5555   if (! die || ! child_die)
5556     return;
5557   gcc_assert (die != child_die);
5558
5559   child_die->die_parent = die;
5560   if (die->die_child)
5561     {
5562       child_die->die_sib = die->die_child->die_sib;
5563       die->die_child->die_sib = child_die;
5564     }
5565   else
5566     child_die->die_sib = child_die;
5567   die->die_child = child_die;
5568 }
5569
5570 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5571    is the specification, to the end of PARENT's list of children.  
5572    This is done by removing and re-adding it.  */
5573
5574 static void
5575 splice_child_die (dw_die_ref parent, dw_die_ref child)
5576 {
5577   dw_die_ref p;
5578
5579   /* We want the declaration DIE from inside the class, not the
5580      specification DIE at toplevel.  */
5581   if (child->die_parent != parent)
5582     {
5583       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5584
5585       if (tmp)
5586         child = tmp;
5587     }
5588
5589   gcc_assert (child->die_parent == parent
5590               || (child->die_parent
5591                   == get_AT_ref (parent, DW_AT_specification)));
5592   
5593   for (p = child->die_parent->die_child; ; p = p->die_sib)
5594     if (p->die_sib == child)
5595       {
5596         remove_child_with_prev (child, p);
5597         break;
5598       }
5599
5600   add_child_die (parent, child);
5601 }
5602
5603 /* Return a pointer to a newly created DIE node.  */
5604
5605 static inline dw_die_ref
5606 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5607 {
5608   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5609
5610   die->die_tag = tag_value;
5611
5612   if (parent_die != NULL)
5613     add_child_die (parent_die, die);
5614   else
5615     {
5616       limbo_die_node *limbo_node;
5617
5618       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5619       limbo_node->die = die;
5620       limbo_node->created_for = t;
5621       limbo_node->next = limbo_die_list;
5622       limbo_die_list = limbo_node;
5623     }
5624
5625   return die;
5626 }
5627
5628 /* Return the DIE associated with the given type specifier.  */
5629
5630 static inline dw_die_ref
5631 lookup_type_die (tree type)
5632 {
5633   return TYPE_SYMTAB_DIE (type);
5634 }
5635
5636 /* Equate a DIE to a given type specifier.  */
5637
5638 static inline void
5639 equate_type_number_to_die (tree type, dw_die_ref type_die)
5640 {
5641   TYPE_SYMTAB_DIE (type) = type_die;
5642 }
5643
5644 /* Returns a hash value for X (which really is a die_struct).  */
5645
5646 static hashval_t
5647 decl_die_table_hash (const void *x)
5648 {
5649   return (hashval_t) ((const dw_die_ref) x)->decl_id;
5650 }
5651
5652 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5653
5654 static int
5655 decl_die_table_eq (const void *x, const void *y)
5656 {
5657   return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5658 }
5659
5660 /* Return the DIE associated with a given declaration.  */
5661
5662 static inline dw_die_ref
5663 lookup_decl_die (tree decl)
5664 {
5665   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5666 }
5667
5668 /* Returns a hash value for X (which really is a var_loc_list).  */
5669
5670 static hashval_t
5671 decl_loc_table_hash (const void *x)
5672 {
5673   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5674 }
5675
5676 /* Return nonzero if decl_id of var_loc_list X is the same as
5677    UID of decl *Y.  */
5678
5679 static int
5680 decl_loc_table_eq (const void *x, const void *y)
5681 {
5682   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5683 }
5684
5685 /* Return the var_loc list associated with a given declaration.  */
5686
5687 static inline var_loc_list *
5688 lookup_decl_loc (tree decl)
5689 {
5690   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5691 }
5692
5693 /* Equate a DIE to a particular declaration.  */
5694
5695 static void
5696 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5697 {
5698   unsigned int decl_id = DECL_UID (decl);
5699   void **slot;
5700
5701   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5702   *slot = decl_die;
5703   decl_die->decl_id = decl_id;
5704 }
5705
5706 /* Add a variable location node to the linked list for DECL.  */
5707
5708 static void
5709 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5710 {
5711   unsigned int decl_id = DECL_UID (decl);
5712   var_loc_list *temp;
5713   void **slot;
5714
5715   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5716   if (*slot == NULL)
5717     {
5718       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5719       temp->decl_id = decl_id;
5720       *slot = temp;
5721     }
5722   else
5723     temp = *slot;
5724
5725   if (temp->last)
5726     {
5727       /* If the current location is the same as the end of the list,
5728          we have nothing to do.  */
5729       if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5730                         NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5731         {
5732           /* Add LOC to the end of list and update LAST.  */
5733           temp->last->next = loc;
5734           temp->last = loc;
5735         }
5736     }
5737   /* Do not add empty location to the beginning of the list.  */
5738   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5739     {
5740       temp->first = loc;
5741       temp->last = loc;
5742     }
5743 }
5744 \f
5745 /* Keep track of the number of spaces used to indent the
5746    output of the debugging routines that print the structure of
5747    the DIE internal representation.  */
5748 static int print_indent;
5749
5750 /* Indent the line the number of spaces given by print_indent.  */
5751
5752 static inline void
5753 print_spaces (FILE *outfile)
5754 {
5755   fprintf (outfile, "%*s", print_indent, "");
5756 }
5757
5758 /* Print the information associated with a given DIE, and its children.
5759    This routine is a debugging aid only.  */
5760
5761 static void
5762 print_die (dw_die_ref die, FILE *outfile)
5763 {
5764   dw_attr_ref a;
5765   dw_die_ref c;
5766   unsigned ix;
5767
5768   print_spaces (outfile);
5769   fprintf (outfile, "DIE %4lu: %s\n",
5770            die->die_offset, dwarf_tag_name (die->die_tag));
5771   print_spaces (outfile);
5772   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5773   fprintf (outfile, " offset: %lu\n", die->die_offset);
5774
5775   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5776     {
5777       print_spaces (outfile);
5778       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5779
5780       switch (AT_class (a))
5781         {
5782         case dw_val_class_addr:
5783           fprintf (outfile, "address");
5784           break;
5785         case dw_val_class_offset:
5786           fprintf (outfile, "offset");
5787           break;
5788         case dw_val_class_loc:
5789           fprintf (outfile, "location descriptor");
5790           break;
5791         case dw_val_class_loc_list:
5792           fprintf (outfile, "location list -> label:%s",
5793                    AT_loc_list (a)->ll_symbol);
5794           break;
5795         case dw_val_class_range_list:
5796           fprintf (outfile, "range list");
5797           break;
5798         case dw_val_class_const:
5799           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5800           break;
5801         case dw_val_class_unsigned_const:
5802           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5803           break;
5804         case dw_val_class_long_long:
5805           fprintf (outfile, "constant (%lu,%lu)",
5806                    a->dw_attr_val.v.val_long_long.hi,
5807                    a->dw_attr_val.v.val_long_long.low);
5808           break;
5809         case dw_val_class_vec:
5810           fprintf (outfile, "floating-point or vector constant");
5811           break;
5812         case dw_val_class_flag:
5813           fprintf (outfile, "%u", AT_flag (a));
5814           break;
5815         case dw_val_class_die_ref:
5816           if (AT_ref (a) != NULL)
5817             {
5818               if (AT_ref (a)->die_symbol)
5819                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5820               else
5821                 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5822             }
5823           else
5824             fprintf (outfile, "die -> <null>");
5825           break;
5826         case dw_val_class_lbl_id:
5827         case dw_val_class_lineptr:
5828         case dw_val_class_macptr:
5829           fprintf (outfile, "label: %s", AT_lbl (a));
5830           break;
5831         case dw_val_class_str:
5832           if (AT_string (a) != NULL)
5833             fprintf (outfile, "\"%s\"", AT_string (a));
5834           else
5835             fprintf (outfile, "<null>");
5836           break;
5837         case dw_val_class_file:
5838           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5839                    AT_file (a)->emitted_number);
5840           break;
5841         default:
5842           break;
5843         }
5844
5845       fprintf (outfile, "\n");
5846     }
5847
5848   if (die->die_child != NULL)
5849     {
5850       print_indent += 4;
5851       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5852       print_indent -= 4;
5853     }
5854   if (print_indent == 0)
5855     fprintf (outfile, "\n");
5856 }
5857
5858 /* Print the contents of the source code line number correspondence table.
5859    This routine is a debugging aid only.  */
5860
5861 static void
5862 print_dwarf_line_table (FILE *outfile)
5863 {
5864   unsigned i;
5865   dw_line_info_ref line_info;
5866
5867   fprintf (outfile, "\n\nDWARF source line information\n");
5868   for (i = 1; i < line_info_table_in_use; i++)
5869     {
5870       line_info = &line_info_table[i];
5871       fprintf (outfile, "%5d: %4ld %6ld\n", i,
5872                line_info->dw_file_num,
5873                line_info->dw_line_num);
5874     }
5875
5876   fprintf (outfile, "\n\n");
5877 }
5878
5879 /* Print the information collected for a given DIE.  */
5880
5881 void
5882 debug_dwarf_die (dw_die_ref die)
5883 {
5884   print_die (die, stderr);
5885 }
5886
5887 /* Print all DWARF information collected for the compilation unit.
5888    This routine is a debugging aid only.  */
5889
5890 void
5891 debug_dwarf (void)
5892 {
5893   print_indent = 0;
5894   print_die (comp_unit_die, stderr);
5895   if (! DWARF2_ASM_LINE_DEBUG_INFO)
5896     print_dwarf_line_table (stderr);
5897 }
5898 \f
5899 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5900    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5901    DIE that marks the start of the DIEs for this include file.  */
5902
5903 static dw_die_ref
5904 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5905 {
5906   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5907   dw_die_ref new_unit = gen_compile_unit_die (filename);
5908
5909   new_unit->die_sib = old_unit;
5910   return new_unit;
5911 }
5912
5913 /* Close an include-file CU and reopen the enclosing one.  */
5914
5915 static dw_die_ref
5916 pop_compile_unit (dw_die_ref old_unit)
5917 {
5918   dw_die_ref new_unit = old_unit->die_sib;
5919
5920   old_unit->die_sib = NULL;
5921   return new_unit;
5922 }
5923
5924 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5925 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5926
5927 /* Calculate the checksum of a location expression.  */
5928
5929 static inline void
5930 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5931 {
5932   CHECKSUM (loc->dw_loc_opc);
5933   CHECKSUM (loc->dw_loc_oprnd1);
5934   CHECKSUM (loc->dw_loc_oprnd2);
5935 }
5936
5937 /* Calculate the checksum of an attribute.  */
5938
5939 static void
5940 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5941 {
5942   dw_loc_descr_ref loc;
5943   rtx r;
5944
5945   CHECKSUM (at->dw_attr);
5946
5947   /* We don't care that this was compiled with a different compiler
5948      snapshot; if the output is the same, that's what matters.  */
5949   if (at->dw_attr == DW_AT_producer)
5950     return;
5951
5952   switch (AT_class (at))
5953     {
5954     case dw_val_class_const:
5955       CHECKSUM (at->dw_attr_val.v.val_int);
5956       break;
5957     case dw_val_class_unsigned_const:
5958       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5959       break;
5960     case dw_val_class_long_long:
5961       CHECKSUM (at->dw_attr_val.v.val_long_long);
5962       break;
5963     case dw_val_class_vec:
5964       CHECKSUM (at->dw_attr_val.v.val_vec);
5965       break;
5966     case dw_val_class_flag:
5967       CHECKSUM (at->dw_attr_val.v.val_flag);
5968       break;
5969     case dw_val_class_str:
5970       CHECKSUM_STRING (AT_string (at));
5971       break;
5972
5973     case dw_val_class_addr:
5974       r = AT_addr (at);
5975       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5976       CHECKSUM_STRING (XSTR (r, 0));
5977       break;
5978
5979     case dw_val_class_offset:
5980       CHECKSUM (at->dw_attr_val.v.val_offset);
5981       break;
5982
5983     case dw_val_class_loc:
5984       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5985         loc_checksum (loc, ctx);
5986       break;
5987
5988     case dw_val_class_die_ref:
5989       die_checksum (AT_ref (at), ctx, mark);
5990       break;
5991
5992     case dw_val_class_fde_ref:
5993     case dw_val_class_lbl_id:
5994     case dw_val_class_lineptr:
5995     case dw_val_class_macptr:
5996       break;
5997
5998     case dw_val_class_file:
5999       CHECKSUM_STRING (AT_file (at)->filename);
6000       break;
6001
6002     default:
6003       break;
6004     }
6005 }
6006
6007 /* Calculate the checksum of a DIE.  */
6008
6009 static void
6010 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6011 {
6012   dw_die_ref c;
6013   dw_attr_ref a;
6014   unsigned ix;
6015
6016   /* To avoid infinite recursion.  */
6017   if (die->die_mark)
6018     {
6019       CHECKSUM (die->die_mark);
6020       return;
6021     }
6022   die->die_mark = ++(*mark);
6023
6024   CHECKSUM (die->die_tag);
6025
6026   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6027     attr_checksum (a, ctx, mark);
6028
6029   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6030 }
6031
6032 #undef CHECKSUM
6033 #undef CHECKSUM_STRING
6034
6035 /* Do the location expressions look same?  */
6036 static inline int
6037 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6038 {
6039   return loc1->dw_loc_opc == loc2->dw_loc_opc
6040          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6041          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6042 }
6043
6044 /* Do the values look the same?  */
6045 static int
6046 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6047 {
6048   dw_loc_descr_ref loc1, loc2;
6049   rtx r1, r2;
6050
6051   if (v1->val_class != v2->val_class)
6052     return 0;
6053
6054   switch (v1->val_class)
6055     {
6056     case dw_val_class_const:
6057       return v1->v.val_int == v2->v.val_int;
6058     case dw_val_class_unsigned_const:
6059       return v1->v.val_unsigned == v2->v.val_unsigned;
6060     case dw_val_class_long_long:
6061       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6062              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6063     case dw_val_class_vec:
6064       if (v1->v.val_vec.length != v2->v.val_vec.length
6065           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6066         return 0;
6067       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6068                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6069         return 0;
6070       return 1;
6071     case dw_val_class_flag:
6072       return v1->v.val_flag == v2->v.val_flag;
6073     case dw_val_class_str:
6074       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6075
6076     case dw_val_class_addr:
6077       r1 = v1->v.val_addr;
6078       r2 = v2->v.val_addr;
6079       if (GET_CODE (r1) != GET_CODE (r2))
6080         return 0;
6081       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6082       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6083
6084     case dw_val_class_offset:
6085       return v1->v.val_offset == v2->v.val_offset;
6086
6087     case dw_val_class_loc:
6088       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6089            loc1 && loc2;
6090            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6091         if (!same_loc_p (loc1, loc2, mark))
6092           return 0;
6093       return !loc1 && !loc2;
6094
6095     case dw_val_class_die_ref:
6096       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6097
6098     case dw_val_class_fde_ref:
6099     case dw_val_class_lbl_id:
6100     case dw_val_class_lineptr:
6101     case dw_val_class_macptr:
6102       return 1;
6103
6104     case dw_val_class_file:
6105       return v1->v.val_file == v2->v.val_file;
6106
6107     default:
6108       return 1;
6109     }
6110 }
6111
6112 /* Do the attributes look the same?  */
6113
6114 static int
6115 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6116 {
6117   if (at1->dw_attr != at2->dw_attr)
6118     return 0;
6119
6120   /* We don't care that this was compiled with a different compiler
6121      snapshot; if the output is the same, that's what matters. */
6122   if (at1->dw_attr == DW_AT_producer)
6123     return 1;
6124
6125   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6126 }
6127
6128 /* Do the dies look the same?  */
6129
6130 static int
6131 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6132 {
6133   dw_die_ref c1, c2;
6134   dw_attr_ref a1;
6135   unsigned ix;
6136
6137   /* To avoid infinite recursion.  */
6138   if (die1->die_mark)
6139     return die1->die_mark == die2->die_mark;
6140   die1->die_mark = die2->die_mark = ++(*mark);
6141
6142   if (die1->die_tag != die2->die_tag)
6143     return 0;
6144
6145   if (VEC_length (dw_attr_node, die1->die_attr)
6146       != VEC_length (dw_attr_node, die2->die_attr))
6147     return 0;
6148   
6149   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6150     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6151       return 0;
6152
6153   c1 = die1->die_child;
6154   c2 = die2->die_child;
6155   if (! c1)
6156     {
6157       if (c2)
6158         return 0;
6159     }
6160   else
6161     for (;;)
6162       {
6163         if (!same_die_p (c1, c2, mark))
6164           return 0;
6165         c1 = c1->die_sib;
6166         c2 = c2->die_sib;
6167         if (c1 == die1->die_child)
6168           {
6169             if (c2 == die2->die_child)
6170               break;
6171             else
6172               return 0;
6173           }
6174     }
6175
6176   return 1;
6177 }
6178
6179 /* Do the dies look the same?  Wrapper around same_die_p.  */
6180
6181 static int
6182 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6183 {
6184   int mark = 0;
6185   int ret = same_die_p (die1, die2, &mark);
6186
6187   unmark_all_dies (die1);
6188   unmark_all_dies (die2);
6189
6190   return ret;
6191 }
6192
6193 /* The prefix to attach to symbols on DIEs in the current comdat debug
6194    info section.  */
6195 static char *comdat_symbol_id;
6196
6197 /* The index of the current symbol within the current comdat CU.  */
6198 static unsigned int comdat_symbol_number;
6199
6200 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6201    children, and set comdat_symbol_id accordingly.  */
6202
6203 static void
6204 compute_section_prefix (dw_die_ref unit_die)
6205 {
6206   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6207   const char *base = die_name ? lbasename (die_name) : "anonymous";
6208   char *name = alloca (strlen (base) + 64);
6209   char *p;
6210   int i, mark;
6211   unsigned char checksum[16];
6212   struct md5_ctx ctx;
6213
6214   /* Compute the checksum of the DIE, then append part of it as hex digits to
6215      the name filename of the unit.  */
6216
6217   md5_init_ctx (&ctx);
6218   mark = 0;
6219   die_checksum (unit_die, &ctx, &mark);
6220   unmark_all_dies (unit_die);
6221   md5_finish_ctx (&ctx, checksum);
6222
6223   sprintf (name, "%s.", base);
6224   clean_symbol_name (name);
6225
6226   p = name + strlen (name);
6227   for (i = 0; i < 4; i++)
6228     {
6229       sprintf (p, "%.2x", checksum[i]);
6230       p += 2;
6231     }
6232
6233   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6234   comdat_symbol_number = 0;
6235 }
6236
6237 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6238
6239 static int
6240 is_type_die (dw_die_ref die)
6241 {
6242   switch (die->die_tag)
6243     {
6244     case DW_TAG_array_type:
6245     case DW_TAG_class_type:
6246     case DW_TAG_enumeration_type:
6247     case DW_TAG_pointer_type:
6248     case DW_TAG_reference_type:
6249     case DW_TAG_string_type:
6250     case DW_TAG_structure_type:
6251     case DW_TAG_subroutine_type:
6252     case DW_TAG_union_type:
6253     case DW_TAG_ptr_to_member_type:
6254     case DW_TAG_set_type:
6255     case DW_TAG_subrange_type:
6256     case DW_TAG_base_type:
6257     case DW_TAG_const_type:
6258     case DW_TAG_file_type:
6259     case DW_TAG_packed_type:
6260     case DW_TAG_volatile_type:
6261     case DW_TAG_typedef:
6262       return 1;
6263     default:
6264       return 0;
6265     }
6266 }
6267
6268 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6269    Basically, we want to choose the bits that are likely to be shared between
6270    compilations (types) and leave out the bits that are specific to individual
6271    compilations (functions).  */
6272
6273 static int
6274 is_comdat_die (dw_die_ref c)
6275 {
6276   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6277      we do for stabs.  The advantage is a greater likelihood of sharing between
6278      objects that don't include headers in the same order (and therefore would
6279      put the base types in a different comdat).  jason 8/28/00 */
6280
6281   if (c->die_tag == DW_TAG_base_type)
6282     return 0;
6283
6284   if (c->die_tag == DW_TAG_pointer_type
6285       || c->die_tag == DW_TAG_reference_type
6286       || c->die_tag == DW_TAG_const_type
6287       || c->die_tag == DW_TAG_volatile_type)
6288     {
6289       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6290
6291       return t ? is_comdat_die (t) : 0;
6292     }
6293
6294   return is_type_die (c);
6295 }
6296
6297 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6298    compilation unit.  */
6299
6300 static int
6301 is_symbol_die (dw_die_ref c)
6302 {
6303   return (is_type_die (c)
6304           || (get_AT (c, DW_AT_declaration)
6305               && !get_AT (c, DW_AT_specification))
6306           || c->die_tag == DW_TAG_namespace);
6307 }
6308
6309 static char *
6310 gen_internal_sym (const char *prefix)
6311 {
6312   char buf[256];
6313
6314   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6315   return xstrdup (buf);
6316 }
6317
6318 /* Assign symbols to all worthy DIEs under DIE.  */
6319
6320 static void
6321 assign_symbol_names (dw_die_ref die)
6322 {
6323   dw_die_ref c;
6324
6325   if (is_symbol_die (die))
6326     {
6327       if (comdat_symbol_id)
6328         {
6329           char *p = alloca (strlen (comdat_symbol_id) + 64);
6330
6331           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6332                    comdat_symbol_id, comdat_symbol_number++);
6333           die->die_symbol = xstrdup (p);
6334         }
6335       else
6336         die->die_symbol = gen_internal_sym ("LDIE");
6337     }
6338
6339   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6340 }
6341
6342 struct cu_hash_table_entry
6343 {
6344   dw_die_ref cu;
6345   unsigned min_comdat_num, max_comdat_num;
6346   struct cu_hash_table_entry *next;
6347 };
6348
6349 /* Routines to manipulate hash table of CUs.  */
6350 static hashval_t
6351 htab_cu_hash (const void *of)
6352 {
6353   const struct cu_hash_table_entry *entry = of;
6354
6355   return htab_hash_string (entry->cu->die_symbol);
6356 }
6357
6358 static int
6359 htab_cu_eq (const void *of1, const void *of2)
6360 {
6361   const struct cu_hash_table_entry *entry1 = of1;
6362   const struct die_struct *entry2 = of2;
6363
6364   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6365 }
6366
6367 static void
6368 htab_cu_del (void *what)
6369 {
6370   struct cu_hash_table_entry *next, *entry = what;
6371
6372   while (entry)
6373     {
6374       next = entry->next;
6375       free (entry);
6376       entry = next;
6377     }
6378 }
6379
6380 /* Check whether we have already seen this CU and set up SYM_NUM
6381    accordingly.  */
6382 static int
6383 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6384 {
6385   struct cu_hash_table_entry dummy;
6386   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6387
6388   dummy.max_comdat_num = 0;
6389
6390   slot = (struct cu_hash_table_entry **)
6391     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6392         INSERT);
6393   entry = *slot;
6394
6395   for (; entry; last = entry, entry = entry->next)
6396     {
6397       if (same_die_p_wrap (cu, entry->cu))
6398         break;
6399     }
6400
6401   if (entry)
6402     {
6403       *sym_num = entry->min_comdat_num;
6404       return 1;
6405     }
6406
6407   entry = XCNEW (struct cu_hash_table_entry);
6408   entry->cu = cu;
6409   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6410   entry->next = *slot;
6411   *slot = entry;
6412
6413   return 0;
6414 }
6415
6416 /* Record SYM_NUM to record of CU in HTABLE.  */
6417 static void
6418 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6419 {
6420   struct cu_hash_table_entry **slot, *entry;
6421
6422   slot = (struct cu_hash_table_entry **)
6423     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6424         NO_INSERT);
6425   entry = *slot;
6426
6427   entry->max_comdat_num = sym_num;
6428 }
6429
6430 /* Traverse the DIE (which is always comp_unit_die), and set up
6431    additional compilation units for each of the include files we see
6432    bracketed by BINCL/EINCL.  */
6433
6434 static void
6435 break_out_includes (dw_die_ref die)
6436 {
6437   dw_die_ref c;
6438   dw_die_ref unit = NULL;
6439   limbo_die_node *node, **pnode;
6440   htab_t cu_hash_table;
6441
6442   c = die->die_child;
6443   if (c) do {
6444     dw_die_ref prev = c;
6445     c = c->die_sib;
6446     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6447            || (unit && is_comdat_die (c)))
6448       {
6449         dw_die_ref next = c->die_sib;
6450
6451         /* This DIE is for a secondary CU; remove it from the main one.  */
6452         remove_child_with_prev (c, prev);
6453         
6454         if (c->die_tag == DW_TAG_GNU_BINCL)
6455           unit = push_new_compile_unit (unit, c);
6456         else if (c->die_tag == DW_TAG_GNU_EINCL)
6457           unit = pop_compile_unit (unit);
6458         else
6459           add_child_die (unit, c);
6460         c = next;
6461         if (c == die->die_child)
6462           break;
6463       }
6464   } while (c != die->die_child);
6465
6466 #if 0
6467   /* We can only use this in debugging, since the frontend doesn't check
6468      to make sure that we leave every include file we enter.  */
6469   gcc_assert (!unit);
6470 #endif
6471
6472   assign_symbol_names (die);
6473   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6474   for (node = limbo_die_list, pnode = &limbo_die_list;
6475        node;
6476        node = node->next)
6477     {
6478       int is_dupl;
6479
6480       compute_section_prefix (node->die);
6481       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6482                         &comdat_symbol_number);
6483       assign_symbol_names (node->die);
6484       if (is_dupl)
6485         *pnode = node->next;
6486       else
6487         {
6488           pnode = &node->next;
6489           record_comdat_symbol_number (node->die, cu_hash_table,
6490                 comdat_symbol_number);
6491         }
6492     }
6493   htab_delete (cu_hash_table);
6494 }
6495
6496 /* Traverse the DIE and add a sibling attribute if it may have the
6497    effect of speeding up access to siblings.  To save some space,
6498    avoid generating sibling attributes for DIE's without children.  */
6499
6500 static void
6501 add_sibling_attributes (dw_die_ref die)
6502 {
6503   dw_die_ref c;
6504
6505   if (! die->die_child)
6506     return;
6507
6508   if (die->die_parent && die != die->die_parent->die_child)
6509     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6510
6511   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6512 }
6513
6514 /* Output all location lists for the DIE and its children.  */
6515
6516 static void
6517 output_location_lists (dw_die_ref die)
6518 {
6519   dw_die_ref c;
6520   dw_attr_ref a;
6521   unsigned ix;
6522
6523   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6524     if (AT_class (a) == dw_val_class_loc_list)
6525       output_loc_list (AT_loc_list (a));
6526
6527   FOR_EACH_CHILD (die, c, output_location_lists (c));
6528 }
6529
6530 /* The format of each DIE (and its attribute value pairs) is encoded in an
6531    abbreviation table.  This routine builds the abbreviation table and assigns
6532    a unique abbreviation id for each abbreviation entry.  The children of each
6533    die are visited recursively.  */
6534
6535 static void
6536 build_abbrev_table (dw_die_ref die)
6537 {
6538   unsigned long abbrev_id;
6539   unsigned int n_alloc;
6540   dw_die_ref c;
6541   dw_attr_ref a;
6542   unsigned ix;
6543
6544   /* Scan the DIE references, and mark as external any that refer to
6545      DIEs from other CUs (i.e. those which are not marked).  */
6546   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6547     if (AT_class (a) == dw_val_class_die_ref
6548         && AT_ref (a)->die_mark == 0)
6549       {
6550         gcc_assert (AT_ref (a)->die_symbol);
6551
6552         set_AT_ref_external (a, 1);
6553       }
6554
6555   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6556     {
6557       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6558       dw_attr_ref die_a, abbrev_a;
6559       unsigned ix;
6560       bool ok = true;
6561       
6562       if (abbrev->die_tag != die->die_tag)
6563         continue;
6564       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6565         continue;
6566       
6567       if (VEC_length (dw_attr_node, abbrev->die_attr)
6568           != VEC_length (dw_attr_node, die->die_attr))
6569         continue;
6570   
6571       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6572         {
6573           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6574           if ((abbrev_a->dw_attr != die_a->dw_attr)
6575               || (value_format (abbrev_a) != value_format (die_a)))
6576             {
6577               ok = false;
6578               break;
6579             }
6580         }
6581       if (ok)
6582         break;
6583     }
6584
6585   if (abbrev_id >= abbrev_die_table_in_use)
6586     {
6587       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6588         {
6589           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6590           abbrev_die_table = ggc_realloc (abbrev_die_table,
6591                                           sizeof (dw_die_ref) * n_alloc);
6592
6593           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6594                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6595           abbrev_die_table_allocated = n_alloc;
6596         }
6597
6598       ++abbrev_die_table_in_use;
6599       abbrev_die_table[abbrev_id] = die;
6600     }
6601
6602   die->die_abbrev = abbrev_id;
6603   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6604 }
6605 \f
6606 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6607
6608 static int
6609 constant_size (long unsigned int value)
6610 {
6611   int log;
6612
6613   if (value == 0)
6614     log = 0;
6615   else
6616     log = floor_log2 (value);
6617
6618   log = log / 8;
6619   log = 1 << (floor_log2 (log) + 1);
6620
6621   return log;
6622 }
6623
6624 /* Return the size of a DIE as it is represented in the
6625    .debug_info section.  */
6626
6627 static unsigned long
6628 size_of_die (dw_die_ref die)
6629 {
6630   unsigned long size = 0;
6631   dw_attr_ref a;
6632   unsigned ix;
6633
6634   size += size_of_uleb128 (die->die_abbrev);
6635   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6636     {
6637       switch (AT_class (a))
6638         {
6639         case dw_val_class_addr:
6640           size += DWARF2_ADDR_SIZE;
6641           break;
6642         case dw_val_class_offset:
6643           size += DWARF_OFFSET_SIZE;
6644           break;
6645         case dw_val_class_loc:
6646           {
6647             unsigned long lsize = size_of_locs (AT_loc (a));
6648
6649             /* Block length.  */
6650             size += constant_size (lsize);
6651             size += lsize;
6652           }
6653           break;
6654         case dw_val_class_loc_list:
6655           size += DWARF_OFFSET_SIZE;
6656           break;
6657         case dw_val_class_range_list:
6658           size += DWARF_OFFSET_SIZE;
6659           break;
6660         case dw_val_class_const:
6661           size += size_of_sleb128 (AT_int (a));
6662           break;
6663         case dw_val_class_unsigned_const:
6664           size += constant_size (AT_unsigned (a));
6665           break;
6666         case dw_val_class_long_long:
6667           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6668           break;
6669         case dw_val_class_vec:
6670           size += 1 + (a->dw_attr_val.v.val_vec.length
6671                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6672           break;
6673         case dw_val_class_flag:
6674           size += 1;
6675           break;
6676         case dw_val_class_die_ref:
6677           if (AT_ref_external (a))
6678             size += DWARF2_ADDR_SIZE;
6679           else
6680             size += DWARF_OFFSET_SIZE;
6681           break;
6682         case dw_val_class_fde_ref:
6683           size += DWARF_OFFSET_SIZE;
6684           break;
6685         case dw_val_class_lbl_id:
6686           size += DWARF2_ADDR_SIZE;
6687           break;
6688         case dw_val_class_lineptr:
6689         case dw_val_class_macptr:
6690           size += DWARF_OFFSET_SIZE;
6691           break;
6692         case dw_val_class_str:
6693           if (AT_string_form (a) == DW_FORM_strp)
6694             size += DWARF_OFFSET_SIZE;
6695           else
6696             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6697           break;
6698         case dw_val_class_file:
6699           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6700           break;
6701         default:
6702           gcc_unreachable ();
6703         }
6704     }
6705
6706   return size;
6707 }
6708
6709 /* Size the debugging information associated with a given DIE.  Visits the
6710    DIE's children recursively.  Updates the global variable next_die_offset, on
6711    each time through.  Uses the current value of next_die_offset to update the
6712    die_offset field in each DIE.  */
6713
6714 static void
6715 calc_die_sizes (dw_die_ref die)
6716 {
6717   dw_die_ref c;
6718
6719   die->die_offset = next_die_offset;
6720   next_die_offset += size_of_die (die);
6721
6722   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6723
6724   if (die->die_child != NULL)
6725     /* Count the null byte used to terminate sibling lists.  */
6726     next_die_offset += 1;
6727 }
6728
6729 /* Set the marks for a die and its children.  We do this so
6730    that we know whether or not a reference needs to use FORM_ref_addr; only
6731    DIEs in the same CU will be marked.  We used to clear out the offset
6732    and use that as the flag, but ran into ordering problems.  */
6733
6734 static void
6735 mark_dies (dw_die_ref die)
6736 {
6737   dw_die_ref c;
6738
6739   gcc_assert (!die->die_mark);
6740
6741   die->die_mark = 1;
6742   FOR_EACH_CHILD (die, c, mark_dies (c));
6743 }
6744
6745 /* Clear the marks for a die and its children.  */
6746
6747 static void
6748 unmark_dies (dw_die_ref die)
6749 {
6750   dw_die_ref c;
6751
6752   gcc_assert (die->die_mark);
6753
6754   die->die_mark = 0;
6755   FOR_EACH_CHILD (die, c, unmark_dies (c));
6756 }
6757
6758 /* Clear the marks for a die, its children and referred dies.  */
6759
6760 static void
6761 unmark_all_dies (dw_die_ref die)
6762 {
6763   dw_die_ref c;
6764   dw_attr_ref a;
6765   unsigned ix;
6766
6767   if (!die->die_mark)
6768     return;
6769   die->die_mark = 0;
6770
6771   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6772
6773   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6774     if (AT_class (a) == dw_val_class_die_ref)
6775       unmark_all_dies (AT_ref (a));
6776 }
6777
6778 /* Return the size of the .debug_pubnames or .debug_pubtypes table  
6779    generated for the compilation unit.  */
6780
6781 static unsigned long
6782 size_of_pubnames (VEC (pubname_entry, gc) * names)
6783 {
6784   unsigned long size;
6785   unsigned i;
6786   pubname_ref p;
6787
6788   size = DWARF_PUBNAMES_HEADER_SIZE;
6789   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6790     if (names != pubtype_table
6791         || p->die->die_offset != 0
6792         || !flag_eliminate_unused_debug_types)
6793       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6794
6795   size += DWARF_OFFSET_SIZE;
6796   return size;
6797 }
6798
6799 /* Return the size of the information in the .debug_aranges section.  */
6800
6801 static unsigned long
6802 size_of_aranges (void)
6803 {
6804   unsigned long size;
6805
6806   size = DWARF_ARANGES_HEADER_SIZE;
6807
6808   /* Count the address/length pair for this compilation unit.  */
6809   size += 2 * DWARF2_ADDR_SIZE;
6810   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6811
6812   /* Count the two zero words used to terminated the address range table.  */
6813   size += 2 * DWARF2_ADDR_SIZE;
6814   return size;
6815 }
6816 \f
6817 /* Select the encoding of an attribute value.  */
6818
6819 static enum dwarf_form
6820 value_format (dw_attr_ref a)
6821 {
6822   switch (a->dw_attr_val.val_class)
6823     {
6824     case dw_val_class_addr:
6825       return DW_FORM_addr;
6826     case dw_val_class_range_list:
6827     case dw_val_class_offset:
6828     case dw_val_class_loc_list:
6829       switch (DWARF_OFFSET_SIZE)
6830         {
6831         case 4:
6832           return DW_FORM_data4;
6833         case 8:
6834           return DW_FORM_data8;
6835         default:
6836           gcc_unreachable ();
6837         }
6838     case dw_val_class_loc:
6839       switch (constant_size (size_of_locs (AT_loc (a))))
6840         {
6841         case 1:
6842           return DW_FORM_block1;
6843         case 2:
6844           return DW_FORM_block2;
6845         default:
6846           gcc_unreachable ();
6847         }
6848     case dw_val_class_const:
6849       return DW_FORM_sdata;
6850     case dw_val_class_unsigned_const:
6851       switch (constant_size (AT_unsigned (a)))
6852         {
6853         case 1:
6854           return DW_FORM_data1;
6855         case 2:
6856           return DW_FORM_data2;
6857         case 4:
6858           return DW_FORM_data4;
6859         case 8:
6860           return DW_FORM_data8;
6861         default:
6862           gcc_unreachable ();
6863         }
6864     case dw_val_class_long_long:
6865       return DW_FORM_block1;
6866     case dw_val_class_vec:
6867       return DW_FORM_block1;
6868     case dw_val_class_flag:
6869       return DW_FORM_flag;
6870     case dw_val_class_die_ref:
6871       if (AT_ref_external (a))
6872         return DW_FORM_ref_addr;
6873       else
6874         return DW_FORM_ref;
6875     case dw_val_class_fde_ref:
6876       return DW_FORM_data;
6877     case dw_val_class_lbl_id:
6878       return DW_FORM_addr;
6879     case dw_val_class_lineptr:
6880     case dw_val_class_macptr:
6881       return DW_FORM_data;
6882     case dw_val_class_str:
6883       return AT_string_form (a);
6884     case dw_val_class_file:
6885       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6886         {
6887         case 1:
6888           return DW_FORM_data1;
6889         case 2:
6890           return DW_FORM_data2;
6891         case 4:
6892           return DW_FORM_data4;
6893         default:
6894           gcc_unreachable ();
6895         }
6896
6897     default:
6898       gcc_unreachable ();
6899     }
6900 }
6901
6902 /* Output the encoding of an attribute value.  */
6903
6904 static void
6905 output_value_format (dw_attr_ref a)
6906 {
6907   enum dwarf_form form = value_format (a);
6908
6909   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6910 }
6911
6912 /* Output the .debug_abbrev section which defines the DIE abbreviation
6913    table.  */
6914
6915 static void
6916 output_abbrev_section (void)
6917 {
6918   unsigned long abbrev_id;
6919
6920   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6921     {
6922       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6923       unsigned ix;
6924       dw_attr_ref a_attr;
6925
6926       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6927       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6928                                    dwarf_tag_name (abbrev->die_tag));
6929
6930       if (abbrev->die_child != NULL)
6931         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6932       else
6933         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6934
6935       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6936            ix++)
6937         {
6938           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6939                                        dwarf_attr_name (a_attr->dw_attr));
6940           output_value_format (a_attr);
6941         }
6942
6943       dw2_asm_output_data (1, 0, NULL);
6944       dw2_asm_output_data (1, 0, NULL);
6945     }
6946
6947   /* Terminate the table.  */
6948   dw2_asm_output_data (1, 0, NULL);
6949 }
6950
6951 /* Output a symbol we can use to refer to this DIE from another CU.  */
6952
6953 static inline void
6954 output_die_symbol (dw_die_ref die)
6955 {
6956   char *sym = die->die_symbol;
6957
6958   if (sym == 0)
6959     return;
6960
6961   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6962     /* We make these global, not weak; if the target doesn't support
6963        .linkonce, it doesn't support combining the sections, so debugging
6964        will break.  */
6965     targetm.asm_out.globalize_label (asm_out_file, sym);
6966
6967   ASM_OUTPUT_LABEL (asm_out_file, sym);
6968 }
6969
6970 /* Return a new location list, given the begin and end range, and the
6971    expression. gensym tells us whether to generate a new internal symbol for
6972    this location list node, which is done for the head of the list only.  */
6973
6974 static inline dw_loc_list_ref
6975 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6976               const char *section, unsigned int gensym)
6977 {
6978   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6979
6980   retlist->begin = begin;
6981   retlist->end = end;
6982   retlist->expr = expr;
6983   retlist->section = section;
6984   if (gensym)
6985     retlist->ll_symbol = gen_internal_sym ("LLST");
6986
6987   return retlist;
6988 }
6989
6990 /* Add a location description expression to a location list.  */
6991
6992 static inline void
6993 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6994                            const char *begin, const char *end,
6995                            const char *section)
6996 {
6997   dw_loc_list_ref *d;
6998
6999   /* Find the end of the chain.  */
7000   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7001     ;
7002
7003   /* Add a new location list node to the list.  */
7004   *d = new_loc_list (descr, begin, end, section, 0);
7005 }
7006
7007 static void
7008 dwarf2out_switch_text_section (void)
7009 {
7010   dw_fde_ref fde;
7011
7012   gcc_assert (cfun);
7013
7014   fde = &fde_table[fde_table_in_use - 1];
7015   fde->dw_fde_switched_sections = true;
7016   fde->dw_fde_hot_section_label = cfun->hot_section_label;
7017   fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7018   fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7019   fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
7020   have_multiple_function_sections = true;
7021
7022   /* Reset the current label on switching text sections, so that we
7023      don't attempt to advance_loc4 between labels in different sections.  */
7024   fde->dw_fde_current_label = NULL;
7025 }
7026
7027 /* Output the location list given to us.  */
7028
7029 static void
7030 output_loc_list (dw_loc_list_ref list_head)
7031 {
7032   dw_loc_list_ref curr = list_head;
7033
7034   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7035
7036   /* Walk the location list, and output each range + expression.  */
7037   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7038     {
7039       unsigned long size;
7040       if (!have_multiple_function_sections)
7041         {
7042           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7043                                 "Location list begin address (%s)",
7044                                 list_head->ll_symbol);
7045           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7046                                 "Location list end address (%s)",
7047                                 list_head->ll_symbol);
7048         }
7049       else
7050         {
7051           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7052                                "Location list begin address (%s)",
7053                                list_head->ll_symbol);
7054           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7055                                "Location list end address (%s)",
7056                                list_head->ll_symbol);
7057         }
7058       size = size_of_locs (curr->expr);
7059
7060       /* Output the block length for this list of location operations.  */
7061       gcc_assert (size <= 0xffff);
7062       dw2_asm_output_data (2, size, "%s", "Location expression size");
7063
7064       output_loc_sequence (curr->expr);
7065     }
7066
7067   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7068                        "Location list terminator begin (%s)",
7069                        list_head->ll_symbol);
7070   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7071                        "Location list terminator end (%s)",
7072                        list_head->ll_symbol);
7073 }
7074
7075 /* Output the DIE and its attributes.  Called recursively to generate
7076    the definitions of each child DIE.  */
7077
7078 static void
7079 output_die (dw_die_ref die)
7080 {
7081   dw_attr_ref a;
7082   dw_die_ref c;
7083   unsigned long size;
7084   unsigned ix;
7085
7086   /* If someone in another CU might refer to us, set up a symbol for
7087      them to point to.  */
7088   if (die->die_symbol)
7089     output_die_symbol (die);
7090
7091   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7092                                die->die_offset, dwarf_tag_name (die->die_tag));
7093
7094   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7095     {
7096       const char *name = dwarf_attr_name (a->dw_attr);
7097
7098       switch (AT_class (a))
7099         {
7100         case dw_val_class_addr:
7101           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7102           break;
7103
7104         case dw_val_class_offset:
7105           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7106                                "%s", name);
7107           break;
7108
7109         case dw_val_class_range_list:
7110           {
7111             char *p = strchr (ranges_section_label, '\0');
7112
7113             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7114                      a->dw_attr_val.v.val_offset);
7115             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7116                                    debug_ranges_section, "%s", name);
7117             *p = '\0';
7118           }
7119           break;
7120
7121         case dw_val_class_loc:
7122           size = size_of_locs (AT_loc (a));
7123
7124           /* Output the block length for this list of location operations.  */
7125           dw2_asm_output_data (constant_size (size), size, "%s", name);
7126
7127           output_loc_sequence (AT_loc (a));
7128           break;
7129
7130         case dw_val_class_const:
7131           /* ??? It would be slightly more efficient to use a scheme like is
7132              used for unsigned constants below, but gdb 4.x does not sign
7133              extend.  Gdb 5.x does sign extend.  */
7134           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7135           break;
7136
7137         case dw_val_class_unsigned_const:
7138           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7139                                AT_unsigned (a), "%s", name);
7140           break;
7141
7142         case dw_val_class_long_long:
7143           {
7144             unsigned HOST_WIDE_INT first, second;
7145
7146             dw2_asm_output_data (1,
7147                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7148                                  "%s", name);
7149
7150             if (WORDS_BIG_ENDIAN)
7151               {
7152                 first = a->dw_attr_val.v.val_long_long.hi;
7153                 second = a->dw_attr_val.v.val_long_long.low;
7154               }
7155             else
7156               {
7157                 first = a->dw_attr_val.v.val_long_long.low;
7158                 second = a->dw_attr_val.v.val_long_long.hi;
7159               }
7160
7161             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7162                                  first, "long long constant");
7163             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7164                                  second, NULL);
7165           }
7166           break;
7167
7168         case dw_val_class_vec:
7169           {
7170             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7171             unsigned int len = a->dw_attr_val.v.val_vec.length;
7172             unsigned int i;
7173             unsigned char *p;
7174
7175             dw2_asm_output_data (1, len * elt_size, "%s", name);
7176             if (elt_size > sizeof (HOST_WIDE_INT))
7177               {
7178                 elt_size /= 2;
7179                 len *= 2;
7180               }
7181             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7182                  i < len;
7183                  i++, p += elt_size)
7184               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7185                                    "fp or vector constant word %u", i);
7186             break;
7187           }
7188
7189         case dw_val_class_flag:
7190           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7191           break;
7192
7193         case dw_val_class_loc_list:
7194           {
7195             char *sym = AT_loc_list (a)->ll_symbol;
7196
7197             gcc_assert (sym);
7198             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7199                                    "%s", name);
7200           }
7201           break;
7202
7203         case dw_val_class_die_ref:
7204           if (AT_ref_external (a))
7205             {
7206               char *sym = AT_ref (a)->die_symbol;
7207
7208               gcc_assert (sym);
7209               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7210                                      "%s", name);
7211             }
7212           else
7213             {
7214               gcc_assert (AT_ref (a)->die_offset);
7215               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7216                                    "%s", name);
7217             }
7218           break;
7219
7220         case dw_val_class_fde_ref:
7221           {
7222             char l1[20];
7223
7224             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7225                                          a->dw_attr_val.v.val_fde_index * 2);
7226             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7227                                    "%s", name);
7228           }
7229           break;
7230
7231         case dw_val_class_lbl_id:
7232           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7233           break;
7234
7235         case dw_val_class_lineptr:
7236           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7237                                  debug_line_section, "%s", name);
7238           break;
7239
7240         case dw_val_class_macptr:
7241           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7242                                  debug_macinfo_section, "%s", name);
7243           break;
7244
7245         case dw_val_class_str:
7246           if (AT_string_form (a) == DW_FORM_strp)
7247             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7248                                    a->dw_attr_val.v.val_str->label,
7249                                    debug_str_section,
7250                                    "%s: \"%s\"", name, AT_string (a));
7251           else
7252             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7253           break;
7254
7255         case dw_val_class_file:
7256           {
7257             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7258             
7259             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7260                                  a->dw_attr_val.v.val_file->filename);
7261             break;
7262           }
7263
7264         default:
7265           gcc_unreachable ();
7266         }
7267     }
7268
7269   FOR_EACH_CHILD (die, c, output_die (c));
7270
7271   /* Add null byte to terminate sibling list.  */
7272   if (die->die_child != NULL)
7273     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7274                          die->die_offset);
7275 }
7276
7277 /* Output the compilation unit that appears at the beginning of the
7278    .debug_info section, and precedes the DIE descriptions.  */
7279
7280 static void
7281 output_compilation_unit_header (void)
7282 {
7283   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7284     dw2_asm_output_data (4, 0xffffffff,
7285       "Initial length escape value indicating 64-bit DWARF extension");
7286   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7287                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7288                        "Length of Compilation Unit Info");
7289   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7290   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7291                          debug_abbrev_section,
7292                          "Offset Into Abbrev. Section");
7293   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7294 }
7295
7296 /* Output the compilation unit DIE and its children.  */
7297
7298 static void
7299 output_comp_unit (dw_die_ref die, int output_if_empty)
7300 {
7301   const char *secname;
7302   char *oldsym, *tmp;
7303
7304   /* Unless we are outputting main CU, we may throw away empty ones.  */
7305   if (!output_if_empty && die->die_child == NULL)
7306     return;
7307
7308   /* Even if there are no children of this DIE, we must output the information
7309      about the compilation unit.  Otherwise, on an empty translation unit, we
7310      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7311      will then complain when examining the file.  First mark all the DIEs in
7312      this CU so we know which get local refs.  */
7313   mark_dies (die);
7314
7315   build_abbrev_table (die);
7316
7317   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7318   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7319   calc_die_sizes (die);
7320
7321   oldsym = die->die_symbol;
7322   if (oldsym)
7323     {
7324       tmp = alloca (strlen (oldsym) + 24);
7325
7326       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7327       secname = tmp;
7328       die->die_symbol = NULL;
7329       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7330     }
7331   else
7332     switch_to_section (debug_info_section);
7333
7334   /* Output debugging information.  */
7335   output_compilation_unit_header ();
7336   output_die (die);
7337
7338   /* Leave the marks on the main CU, so we can check them in
7339      output_pubnames.  */
7340   if (oldsym)
7341     {
7342       unmark_dies (die);
7343       die->die_symbol = oldsym;
7344     }
7345 }
7346
7347 /* Return the DWARF2/3 pubname associated with a decl.  */
7348
7349 static const char *
7350 dwarf2_name (tree decl, int scope)
7351 {
7352   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7353 }
7354
7355 /* Add a new entry to .debug_pubnames if appropriate.  */
7356
7357 static void
7358 add_pubname (tree decl, dw_die_ref die)
7359 {
7360   pubname_entry e;
7361
7362   if (! TREE_PUBLIC (decl))
7363     return;
7364
7365   e.die = die;
7366   e.name = xstrdup (dwarf2_name (decl, 1));
7367   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7368 }
7369
7370 /* Add a new entry to .debug_pubtypes if appropriate.  */
7371
7372 static void
7373 add_pubtype (tree decl, dw_die_ref die)
7374 {
7375   pubname_entry e;
7376
7377   e.name = NULL;
7378   if ((TREE_PUBLIC (decl)
7379        || die->die_parent == comp_unit_die)
7380       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7381     {
7382       e.die = die;
7383       if (TYPE_P (decl))
7384         {
7385           if (TYPE_NAME (decl))
7386             {
7387               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7388                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7389               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7390                        && DECL_NAME (TYPE_NAME (decl)))
7391                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7392              else
7393                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7394             }
7395         }
7396       else 
7397         e.name = xstrdup (dwarf2_name (decl, 1));
7398
7399       /* If we don't have a name for the type, there's no point in adding
7400          it to the table.  */
7401       if (e.name && e.name[0] != '\0')
7402         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7403     }
7404 }
7405
7406 /* Output the public names table used to speed up access to externally
7407    visible names; or the public types table used to find type definitions.  */
7408
7409 static void
7410 output_pubnames (VEC (pubname_entry, gc) * names)
7411 {
7412   unsigned i;
7413   unsigned long pubnames_length = size_of_pubnames (names);
7414   pubname_ref pub;
7415
7416   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7417     dw2_asm_output_data (4, 0xffffffff,
7418       "Initial length escape value indicating 64-bit DWARF extension");
7419   if (names == pubname_table)
7420     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7421                          "Length of Public Names Info");
7422   else
7423     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7424                          "Length of Public Type Names Info");
7425   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7426   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7427                          debug_info_section,
7428                          "Offset of Compilation Unit Info");
7429   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7430                        "Compilation Unit Length");
7431
7432   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7433     {
7434       /* We shouldn't see pubnames for DIEs outside of the main CU.  */      
7435       if (names == pubname_table)
7436         gcc_assert (pub->die->die_mark);
7437
7438       if (names != pubtype_table
7439           || pub->die->die_offset != 0
7440           || !flag_eliminate_unused_debug_types)
7441         {
7442           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7443                                "DIE offset");
7444
7445           dw2_asm_output_nstring (pub->name, -1, "external name");
7446         }
7447     }
7448
7449   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7450 }
7451
7452 /* Add a new entry to .debug_aranges if appropriate.  */
7453
7454 static void
7455 add_arange (tree decl, dw_die_ref die)
7456 {
7457   if (! DECL_SECTION_NAME (decl))
7458     return;
7459
7460   if (arange_table_in_use == arange_table_allocated)
7461     {
7462       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7463       arange_table = ggc_realloc (arange_table,
7464                                   (arange_table_allocated
7465                                    * sizeof (dw_die_ref)));
7466       memset (arange_table + arange_table_in_use, 0,
7467               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7468     }
7469
7470   arange_table[arange_table_in_use++] = die;
7471 }
7472
7473 /* Output the information that goes into the .debug_aranges table.
7474    Namely, define the beginning and ending address range of the
7475    text section generated for this compilation unit.  */
7476
7477 static void
7478 output_aranges (void)
7479 {
7480   unsigned i;
7481   unsigned long aranges_length = size_of_aranges ();
7482
7483   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7484     dw2_asm_output_data (4, 0xffffffff,
7485       "Initial length escape value indicating 64-bit DWARF extension");
7486   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7487                        "Length of Address Ranges Info");
7488   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7489   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7490                          debug_info_section,
7491                          "Offset of Compilation Unit Info");
7492   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7493   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7494
7495   /* We need to align to twice the pointer size here.  */
7496   if (DWARF_ARANGES_PAD_SIZE)
7497     {
7498       /* Pad using a 2 byte words so that padding is correct for any
7499          pointer size.  */
7500       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7501                            2 * DWARF2_ADDR_SIZE);
7502       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7503         dw2_asm_output_data (2, 0, NULL);
7504     }
7505
7506   dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7507   dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7508                         text_section_label, "Length");
7509   if (flag_reorder_blocks_and_partition)
7510     {
7511       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label, 
7512                            "Address");
7513       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7514                             cold_text_section_label, "Length");
7515     }
7516
7517   for (i = 0; i < arange_table_in_use; i++)
7518     {
7519       dw_die_ref die = arange_table[i];
7520
7521       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7522       gcc_assert (die->die_mark);
7523
7524       if (die->die_tag == DW_TAG_subprogram)
7525         {
7526           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7527                                "Address");
7528           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7529                                 get_AT_low_pc (die), "Length");
7530         }
7531       else
7532         {
7533           /* A static variable; extract the symbol from DW_AT_location.
7534              Note that this code isn't currently hit, as we only emit
7535              aranges for functions (jason 9/23/99).  */
7536           dw_attr_ref a = get_AT (die, DW_AT_location);
7537           dw_loc_descr_ref loc;
7538
7539           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7540
7541           loc = AT_loc (a);
7542           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7543
7544           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7545                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7546           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7547                                get_AT_unsigned (die, DW_AT_byte_size),
7548                                "Length");
7549         }
7550     }
7551
7552   /* Output the terminator words.  */
7553   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7554   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7555 }
7556
7557 /* Add a new entry to .debug_ranges.  Return the offset at which it
7558    was placed.  */
7559
7560 static unsigned int
7561 add_ranges (tree block)
7562 {
7563   unsigned int in_use = ranges_table_in_use;
7564
7565   if (in_use == ranges_table_allocated)
7566     {
7567       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7568       ranges_table
7569         = ggc_realloc (ranges_table, (ranges_table_allocated
7570                                       * sizeof (struct dw_ranges_struct)));
7571       memset (ranges_table + ranges_table_in_use, 0,
7572               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7573     }
7574
7575   ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7576   ranges_table_in_use = in_use + 1;
7577
7578   return in_use * 2 * DWARF2_ADDR_SIZE;
7579 }
7580
7581 static void
7582 output_ranges (void)
7583 {
7584   unsigned i;
7585   static const char *const start_fmt = "Offset 0x%x";
7586   const char *fmt = start_fmt;
7587
7588   for (i = 0; i < ranges_table_in_use; i++)
7589     {
7590       int block_num = ranges_table[i].block_num;
7591
7592       if (block_num)
7593         {
7594           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7595           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7596
7597           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7598           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7599
7600           /* If all code is in the text section, then the compilation
7601              unit base address defaults to DW_AT_low_pc, which is the
7602              base of the text section.  */
7603           if (!have_multiple_function_sections)
7604             {
7605               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7606                                     text_section_label,
7607                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7608               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7609                                     text_section_label, NULL);
7610             }
7611
7612           /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7613              compilation unit base address to zero, which allows us to
7614              use absolute addresses, and not worry about whether the
7615              target supports cross-section arithmetic.  */
7616           else
7617             {
7618               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7619                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7620               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7621             }
7622
7623           fmt = NULL;
7624         }
7625       else
7626         {
7627           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7628           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7629           fmt = start_fmt;
7630         }
7631     }
7632 }
7633
7634 /* Data structure containing information about input files.  */
7635 struct file_info
7636 {
7637   const char *path;     /* Complete file name.  */
7638   const char *fname;    /* File name part.  */
7639   int length;           /* Length of entire string.  */
7640   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7641   int dir_idx;          /* Index in directory table.  */
7642 };
7643
7644 /* Data structure containing information about directories with source
7645    files.  */
7646 struct dir_info
7647 {
7648   const char *path;     /* Path including directory name.  */
7649   int length;           /* Path length.  */
7650   int prefix;           /* Index of directory entry which is a prefix.  */
7651   int count;            /* Number of files in this directory.  */
7652   int dir_idx;          /* Index of directory used as base.  */
7653 };
7654
7655 /* Callback function for file_info comparison.  We sort by looking at
7656    the directories in the path.  */
7657
7658 static int
7659 file_info_cmp (const void *p1, const void *p2)
7660 {
7661   const struct file_info *s1 = p1;
7662   const struct file_info *s2 = p2;
7663   unsigned char *cp1;
7664   unsigned char *cp2;
7665
7666   /* Take care of file names without directories.  We need to make sure that
7667      we return consistent values to qsort since some will get confused if
7668      we return the same value when identical operands are passed in opposite
7669      orders.  So if neither has a directory, return 0 and otherwise return
7670      1 or -1 depending on which one has the directory.  */
7671   if ((s1->path == s1->fname || s2->path == s2->fname))
7672     return (s2->path == s2->fname) - (s1->path == s1->fname);
7673
7674   cp1 = (unsigned char *) s1->path;
7675   cp2 = (unsigned char *) s2->path;
7676
7677   while (1)
7678     {
7679       ++cp1;
7680       ++cp2;
7681       /* Reached the end of the first path?  If so, handle like above.  */
7682       if ((cp1 == (unsigned char *) s1->fname)
7683           || (cp2 == (unsigned char *) s2->fname))
7684         return ((cp2 == (unsigned char *) s2->fname)
7685                 - (cp1 == (unsigned char *) s1->fname));
7686
7687       /* Character of current path component the same?  */
7688       else if (*cp1 != *cp2)
7689         return *cp1 - *cp2;
7690     }
7691 }
7692
7693 struct file_name_acquire_data 
7694 {
7695   struct file_info *files;
7696   int used_files;
7697   int max_files;
7698 };
7699
7700 /* Traversal function for the hash table.  */
7701
7702 static int
7703 file_name_acquire (void ** slot, void *data)
7704 {
7705   struct file_name_acquire_data *fnad = data;
7706   struct dwarf_file_data *d = *slot;
7707   struct file_info *fi;
7708   const char *f;
7709
7710   gcc_assert (fnad->max_files >= d->emitted_number);
7711
7712   if (! d->emitted_number)
7713     return 1;
7714
7715   gcc_assert (fnad->max_files != fnad->used_files);
7716
7717   fi = fnad->files + fnad->used_files++;
7718
7719   /* Skip all leading "./".  */
7720   f = d->filename;
7721   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7722     f += 2;
7723   
7724   /* Create a new array entry.  */
7725   fi->path = f;
7726   fi->length = strlen (f);
7727   fi->file_idx = d;
7728   
7729   /* Search for the file name part.  */
7730   f = strrchr (f, DIR_SEPARATOR);
7731 #if defined (DIR_SEPARATOR_2)
7732   {
7733     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7734
7735     if (g != NULL)
7736       {
7737         if (f == NULL || f < g)
7738           f = g;
7739       }
7740   }
7741 #endif
7742
7743   fi->fname = f == NULL ? fi->path : f + 1;
7744   return 1;
7745 }
7746
7747 /* Output the directory table and the file name table.  We try to minimize
7748    the total amount of memory needed.  A heuristic is used to avoid large
7749    slowdowns with many input files.  */
7750
7751 static void
7752 output_file_names (void)
7753 {
7754   struct file_name_acquire_data fnad;
7755   int numfiles;
7756   struct file_info *files;
7757   struct dir_info *dirs;
7758   int *saved;
7759   int *savehere;
7760   int *backmap;
7761   int ndirs;
7762   int idx_offset;
7763   int i;
7764   int idx;
7765
7766   if (!last_emitted_file)
7767     {
7768       dw2_asm_output_data (1, 0, "End directory table");
7769       dw2_asm_output_data (1, 0, "End file name table");
7770       return;
7771     }
7772
7773   numfiles = last_emitted_file->emitted_number;
7774
7775   /* Allocate the various arrays we need.  */
7776   files = alloca (numfiles * sizeof (struct file_info));
7777   dirs = alloca (numfiles * sizeof (struct dir_info));
7778
7779   fnad.files = files;
7780   fnad.used_files = 0;
7781   fnad.max_files = numfiles;
7782   htab_traverse (file_table, file_name_acquire, &fnad);
7783   gcc_assert (fnad.used_files == fnad.max_files);
7784
7785   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7786
7787   /* Find all the different directories used.  */
7788   dirs[0].path = files[0].path;
7789   dirs[0].length = files[0].fname - files[0].path;
7790   dirs[0].prefix = -1;
7791   dirs[0].count = 1;
7792   dirs[0].dir_idx = 0;
7793   files[0].dir_idx = 0;
7794   ndirs = 1;
7795
7796   for (i = 1; i < numfiles; i++)
7797     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7798         && memcmp (dirs[ndirs - 1].path, files[i].path,
7799                    dirs[ndirs - 1].length) == 0)
7800       {
7801         /* Same directory as last entry.  */
7802         files[i].dir_idx = ndirs - 1;
7803         ++dirs[ndirs - 1].count;
7804       }
7805     else
7806       {
7807         int j;
7808
7809         /* This is a new directory.  */
7810         dirs[ndirs].path = files[i].path;
7811         dirs[ndirs].length = files[i].fname - files[i].path;
7812         dirs[ndirs].count = 1;
7813         dirs[ndirs].dir_idx = ndirs;
7814         files[i].dir_idx = ndirs;
7815
7816         /* Search for a prefix.  */
7817         dirs[ndirs].prefix = -1;
7818         for (j = 0; j < ndirs; j++)
7819           if (dirs[j].length < dirs[ndirs].length
7820               && dirs[j].length > 1
7821               && (dirs[ndirs].prefix == -1
7822                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7823               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7824             dirs[ndirs].prefix = j;
7825
7826         ++ndirs;
7827       }
7828
7829   /* Now to the actual work.  We have to find a subset of the directories which
7830      allow expressing the file name using references to the directory table
7831      with the least amount of characters.  We do not do an exhaustive search
7832      where we would have to check out every combination of every single
7833      possible prefix.  Instead we use a heuristic which provides nearly optimal
7834      results in most cases and never is much off.  */
7835   saved = alloca (ndirs * sizeof (int));
7836   savehere = alloca (ndirs * sizeof (int));
7837
7838   memset (saved, '\0', ndirs * sizeof (saved[0]));
7839   for (i = 0; i < ndirs; i++)
7840     {
7841       int j;
7842       int total;
7843
7844       /* We can always save some space for the current directory.  But this
7845          does not mean it will be enough to justify adding the directory.  */
7846       savehere[i] = dirs[i].length;
7847       total = (savehere[i] - saved[i]) * dirs[i].count;
7848
7849       for (j = i + 1; j < ndirs; j++)
7850         {
7851           savehere[j] = 0;
7852           if (saved[j] < dirs[i].length)
7853             {
7854               /* Determine whether the dirs[i] path is a prefix of the
7855                  dirs[j] path.  */
7856               int k;
7857
7858               k = dirs[j].prefix;
7859               while (k != -1 && k != (int) i)
7860                 k = dirs[k].prefix;
7861
7862               if (k == (int) i)
7863                 {
7864                   /* Yes it is.  We can possibly save some memory by
7865                      writing the filenames in dirs[j] relative to
7866                      dirs[i].  */
7867                   savehere[j] = dirs[i].length;
7868                   total += (savehere[j] - saved[j]) * dirs[j].count;
7869                 }
7870             }
7871         }
7872
7873       /* Check whether we can save enough to justify adding the dirs[i]
7874          directory.  */
7875       if (total > dirs[i].length + 1)
7876         {
7877           /* It's worthwhile adding.  */
7878           for (j = i; j < ndirs; j++)
7879             if (savehere[j] > 0)
7880               {
7881                 /* Remember how much we saved for this directory so far.  */
7882                 saved[j] = savehere[j];
7883
7884                 /* Remember the prefix directory.  */
7885                 dirs[j].dir_idx = i;
7886               }
7887         }
7888     }
7889
7890   /* Emit the directory name table.  */
7891   idx = 1;
7892   idx_offset = dirs[0].length > 0 ? 1 : 0;
7893   for (i = 1 - idx_offset; i < ndirs; i++)
7894     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7895                             "Directory Entry: 0x%x", i + idx_offset);
7896
7897   dw2_asm_output_data (1, 0, "End directory table");
7898
7899   /* We have to emit them in the order of emitted_number since that's
7900      used in the debug info generation.  To do this efficiently we
7901      generate a back-mapping of the indices first.  */
7902   backmap = alloca (numfiles * sizeof (int));
7903   for (i = 0; i < numfiles; i++)
7904     backmap[files[i].file_idx->emitted_number - 1] = i;
7905
7906   /* Now write all the file names.  */
7907   for (i = 0; i < numfiles; i++)
7908     {
7909       int file_idx = backmap[i];
7910       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7911
7912       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7913                               "File Entry: 0x%x", (unsigned) i + 1);
7914
7915       /* Include directory index.  */
7916       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
7917
7918       /* Modification time.  */
7919       dw2_asm_output_data_uleb128 (0, NULL);
7920
7921       /* File length in bytes.  */
7922       dw2_asm_output_data_uleb128 (0, NULL);
7923     }
7924
7925   dw2_asm_output_data (1, 0, "End file name table");
7926 }
7927
7928
7929 /* Output the source line number correspondence information.  This
7930    information goes into the .debug_line section.  */
7931
7932 static void
7933 output_line_info (void)
7934 {
7935   char l1[20], l2[20], p1[20], p2[20];
7936   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7937   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7938   unsigned opc;
7939   unsigned n_op_args;
7940   unsigned long lt_index;
7941   unsigned long current_line;
7942   long line_offset;
7943   long line_delta;
7944   unsigned long current_file;
7945   unsigned long function;
7946
7947   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7948   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7949   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7950   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7951
7952   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7953     dw2_asm_output_data (4, 0xffffffff,
7954       "Initial length escape value indicating 64-bit DWARF extension");
7955   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7956                         "Length of Source Line Info");
7957   ASM_OUTPUT_LABEL (asm_out_file, l1);
7958
7959   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7960   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7961   ASM_OUTPUT_LABEL (asm_out_file, p1);
7962
7963   /* Define the architecture-dependent minimum instruction length (in
7964    bytes).  In this implementation of DWARF, this field is used for
7965    information purposes only.  Since GCC generates assembly language,
7966    we have no a priori knowledge of how many instruction bytes are
7967    generated for each source line, and therefore can use only the
7968    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7969    commands.  Accordingly, we fix this as `1', which is "correct
7970    enough" for all architectures, and don't let the target override.  */
7971   dw2_asm_output_data (1, 1,
7972                        "Minimum Instruction Length");
7973
7974   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7975                        "Default is_stmt_start flag");
7976   dw2_asm_output_data (1, DWARF_LINE_BASE,
7977                        "Line Base Value (Special Opcodes)");
7978   dw2_asm_output_data (1, DWARF_LINE_RANGE,
7979                        "Line Range Value (Special Opcodes)");
7980   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7981                        "Special Opcode Base");
7982
7983   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7984     {
7985       switch (opc)
7986         {
7987         case DW_LNS_advance_pc:
7988         case DW_LNS_advance_line:
7989         case DW_LNS_set_file:
7990         case DW_LNS_set_column:
7991         case DW_LNS_fixed_advance_pc:
7992           n_op_args = 1;
7993           break;
7994         default:
7995           n_op_args = 0;
7996           break;
7997         }
7998
7999       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8000                            opc, n_op_args);
8001     }
8002
8003   /* Write out the information about the files we use.  */
8004   output_file_names ();
8005   ASM_OUTPUT_LABEL (asm_out_file, p2);
8006
8007   /* We used to set the address register to the first location in the text
8008      section here, but that didn't accomplish anything since we already
8009      have a line note for the opening brace of the first function.  */
8010
8011   /* Generate the line number to PC correspondence table, encoded as
8012      a series of state machine operations.  */
8013   current_file = 1;
8014   current_line = 1;
8015
8016   if (cfun && in_cold_section_p)
8017     strcpy (prev_line_label, cfun->cold_section_label);
8018   else
8019     strcpy (prev_line_label, text_section_label);
8020   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8021     {
8022       dw_line_info_ref line_info = &line_info_table[lt_index];
8023
8024 #if 0
8025       /* Disable this optimization for now; GDB wants to see two line notes
8026          at the beginning of a function so it can find the end of the
8027          prologue.  */
8028
8029       /* Don't emit anything for redundant notes.  Just updating the
8030          address doesn't accomplish anything, because we already assume
8031          that anything after the last address is this line.  */
8032       if (line_info->dw_line_num == current_line
8033           && line_info->dw_file_num == current_file)
8034         continue;
8035 #endif
8036
8037       /* Emit debug info for the address of the current line.
8038
8039          Unfortunately, we have little choice here currently, and must always
8040          use the most general form.  GCC does not know the address delta
8041          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8042          attributes which will give an upper bound on the address range.  We
8043          could perhaps use length attributes to determine when it is safe to
8044          use DW_LNS_fixed_advance_pc.  */
8045
8046       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8047       if (0)
8048         {
8049           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8050           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8051                                "DW_LNS_fixed_advance_pc");
8052           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8053         }
8054       else
8055         {
8056           /* This can handle any delta.  This takes
8057              4+DWARF2_ADDR_SIZE bytes.  */
8058           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8059           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8060           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8061           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8062         }
8063
8064       strcpy (prev_line_label, line_label);
8065
8066       /* Emit debug info for the source file of the current line, if
8067          different from the previous line.  */
8068       if (line_info->dw_file_num != current_file)
8069         {
8070           current_file = line_info->dw_file_num;
8071           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8072           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8073         }
8074
8075       /* Emit debug info for the current line number, choosing the encoding
8076          that uses the least amount of space.  */
8077       if (line_info->dw_line_num != current_line)
8078         {
8079           line_offset = line_info->dw_line_num - current_line;
8080           line_delta = line_offset - DWARF_LINE_BASE;
8081           current_line = line_info->dw_line_num;
8082           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8083             /* This can handle deltas from -10 to 234, using the current
8084                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8085                takes 1 byte.  */
8086             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8087                                  "line %lu", current_line);
8088           else
8089             {
8090               /* This can handle any delta.  This takes at least 4 bytes,
8091                  depending on the value being encoded.  */
8092               dw2_asm_output_data (1, DW_LNS_advance_line,
8093                                    "advance to line %lu", current_line);
8094               dw2_asm_output_data_sleb128 (line_offset, NULL);
8095               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8096             }
8097         }
8098       else
8099         /* We still need to start a new row, so output a copy insn.  */
8100         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8101     }
8102
8103   /* Emit debug info for the address of the end of the function.  */
8104   if (0)
8105     {
8106       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8107                            "DW_LNS_fixed_advance_pc");
8108       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8109     }
8110   else
8111     {
8112       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8113       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8114       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8115       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8116     }
8117
8118   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8119   dw2_asm_output_data_uleb128 (1, NULL);
8120   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8121
8122   function = 0;
8123   current_file = 1;
8124   current_line = 1;
8125   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8126     {
8127       dw_separate_line_info_ref line_info
8128         = &separate_line_info_table[lt_index];
8129
8130 #if 0
8131       /* Don't emit anything for redundant notes.  */
8132       if (line_info->dw_line_num == current_line
8133           && line_info->dw_file_num == current_file
8134           && line_info->function == function)
8135         goto cont;
8136 #endif
8137
8138       /* Emit debug info for the address of the current line.  If this is
8139          a new function, or the first line of a function, then we need
8140          to handle it differently.  */
8141       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8142                                    lt_index);
8143       if (function != line_info->function)
8144         {
8145           function = line_info->function;
8146
8147           /* Set the address register to the first line in the function.  */
8148           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8149           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8150           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8151           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8152         }
8153       else
8154         {
8155           /* ??? See the DW_LNS_advance_pc comment above.  */
8156           if (0)
8157             {
8158               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8159                                    "DW_LNS_fixed_advance_pc");
8160               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8161             }
8162           else
8163             {
8164               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8165               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8166               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8167               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8168             }
8169         }
8170
8171       strcpy (prev_line_label, line_label);
8172
8173       /* Emit debug info for the source file of the current line, if
8174          different from the previous line.  */
8175       if (line_info->dw_file_num != current_file)
8176         {
8177           current_file = line_info->dw_file_num;
8178           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8179           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8180         }
8181
8182       /* Emit debug info for the current line number, choosing the encoding
8183          that uses the least amount of space.  */
8184       if (line_info->dw_line_num != current_line)
8185         {
8186           line_offset = line_info->dw_line_num - current_line;
8187           line_delta = line_offset - DWARF_LINE_BASE;
8188           current_line = line_info->dw_line_num;
8189           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8190             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8191                                  "line %lu", current_line);
8192           else
8193             {
8194               dw2_asm_output_data (1, DW_LNS_advance_line,
8195                                    "advance to line %lu", current_line);
8196               dw2_asm_output_data_sleb128 (line_offset, NULL);
8197               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8198             }
8199         }
8200       else
8201         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8202
8203 #if 0
8204     cont:
8205 #endif
8206
8207       lt_index++;
8208
8209       /* If we're done with a function, end its sequence.  */
8210       if (lt_index == separate_line_info_table_in_use
8211           || separate_line_info_table[lt_index].function != function)
8212         {
8213           current_file = 1;
8214           current_line = 1;
8215
8216           /* Emit debug info for the address of the end of the function.  */
8217           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8218           if (0)
8219             {
8220               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8221                                    "DW_LNS_fixed_advance_pc");
8222               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8223             }
8224           else
8225             {
8226               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8227               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8228               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8229               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8230             }
8231
8232           /* Output the marker for the end of this sequence.  */
8233           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8234           dw2_asm_output_data_uleb128 (1, NULL);
8235           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8236         }
8237     }
8238
8239   /* Output the marker for the end of the line number info.  */
8240   ASM_OUTPUT_LABEL (asm_out_file, l2);
8241 }
8242 \f
8243 /* Given a pointer to a tree node for some base type, return a pointer to
8244    a DIE that describes the given type.
8245
8246    This routine must only be called for GCC type nodes that correspond to
8247    Dwarf base (fundamental) types.  */
8248
8249 static dw_die_ref
8250 base_type_die (tree type)
8251 {
8252   dw_die_ref base_type_result;
8253   enum dwarf_type encoding;
8254
8255   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8256     return 0;
8257
8258   switch (TREE_CODE (type))
8259     {
8260     case INTEGER_TYPE:
8261       if (TYPE_STRING_FLAG (type))
8262         {
8263           if (TYPE_UNSIGNED (type))
8264             encoding = DW_ATE_unsigned_char;
8265           else
8266             encoding = DW_ATE_signed_char;
8267         }
8268       else if (TYPE_UNSIGNED (type))
8269         encoding = DW_ATE_unsigned;
8270       else
8271         encoding = DW_ATE_signed;
8272       break;
8273
8274     case REAL_TYPE:
8275       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8276         encoding = DW_ATE_decimal_float;
8277       else
8278         encoding = DW_ATE_float;
8279       break;
8280
8281       /* Dwarf2 doesn't know anything about complex ints, so use
8282          a user defined type for it.  */
8283     case COMPLEX_TYPE:
8284       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8285         encoding = DW_ATE_complex_float;
8286       else
8287         encoding = DW_ATE_lo_user;
8288       break;
8289
8290     case BOOLEAN_TYPE:
8291       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8292       encoding = DW_ATE_boolean;
8293       break;
8294
8295     default:
8296       /* No other TREE_CODEs are Dwarf fundamental types.  */
8297       gcc_unreachable ();
8298     }
8299
8300   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8301
8302   /* This probably indicates a bug.  */
8303   if (! TYPE_NAME (type))
8304     add_name_attribute (base_type_result, "__unknown__");
8305
8306   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8307                    int_size_in_bytes (type));
8308   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8309
8310   return base_type_result;
8311 }
8312
8313 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8314    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8315
8316 static inline int
8317 is_base_type (tree type)
8318 {
8319   switch (TREE_CODE (type))
8320     {
8321     case ERROR_MARK:
8322     case VOID_TYPE:
8323     case INTEGER_TYPE:
8324     case REAL_TYPE:
8325     case COMPLEX_TYPE:
8326     case BOOLEAN_TYPE:
8327       return 1;
8328
8329     case ARRAY_TYPE:
8330     case RECORD_TYPE:
8331     case UNION_TYPE:
8332     case QUAL_UNION_TYPE:
8333     case ENUMERAL_TYPE:
8334     case FUNCTION_TYPE:
8335     case METHOD_TYPE:
8336     case POINTER_TYPE:
8337     case REFERENCE_TYPE:
8338     case OFFSET_TYPE:
8339     case LANG_TYPE:
8340     case VECTOR_TYPE:
8341       return 0;
8342
8343     default:
8344       gcc_unreachable ();
8345     }
8346
8347   return 0;
8348 }
8349
8350 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8351    node, return the size in bits for the type if it is a constant, or else
8352    return the alignment for the type if the type's size is not constant, or
8353    else return BITS_PER_WORD if the type actually turns out to be an
8354    ERROR_MARK node.  */
8355
8356 static inline unsigned HOST_WIDE_INT
8357 simple_type_size_in_bits (tree type)
8358 {
8359   if (TREE_CODE (type) == ERROR_MARK)
8360     return BITS_PER_WORD;
8361   else if (TYPE_SIZE (type) == NULL_TREE)
8362     return 0;
8363   else if (host_integerp (TYPE_SIZE (type), 1))
8364     return tree_low_cst (TYPE_SIZE (type), 1);
8365   else
8366     return TYPE_ALIGN (type);
8367 }
8368
8369 /* Return true if the debug information for the given type should be
8370    emitted as a subrange type.  */
8371
8372 static inline bool
8373 is_subrange_type (tree type)
8374 {
8375   tree subtype = TREE_TYPE (type);
8376
8377   /* Subrange types are identified by the fact that they are integer
8378      types, and that they have a subtype which is either an integer type
8379      or an enumeral type.  */
8380
8381   if (TREE_CODE (type) != INTEGER_TYPE
8382       || subtype == NULL_TREE)
8383     return false;
8384
8385   if (TREE_CODE (subtype) != INTEGER_TYPE
8386       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8387     return false;
8388
8389   if (TREE_CODE (type) == TREE_CODE (subtype)
8390       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8391       && TYPE_MIN_VALUE (type) != NULL
8392       && TYPE_MIN_VALUE (subtype) != NULL
8393       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8394       && TYPE_MAX_VALUE (type) != NULL
8395       && TYPE_MAX_VALUE (subtype) != NULL
8396       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8397     {
8398       /* The type and its subtype have the same representation.  If in
8399          addition the two types also have the same name, then the given
8400          type is not a subrange type, but rather a plain base type.  */
8401       /* FIXME: brobecker/2004-03-22:
8402          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8403          therefore be sufficient to check the TYPE_SIZE node pointers
8404          rather than checking the actual size.  Unfortunately, we have
8405          found some cases, such as in the Ada "integer" type, where
8406          this is not the case.  Until this problem is solved, we need to
8407          keep checking the actual size.  */
8408       tree type_name = TYPE_NAME (type);
8409       tree subtype_name = TYPE_NAME (subtype);
8410
8411       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8412         type_name = DECL_NAME (type_name);
8413
8414       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8415         subtype_name = DECL_NAME (subtype_name);
8416
8417       if (type_name == subtype_name)
8418         return false;
8419     }
8420
8421   return true;
8422 }
8423
8424 /*  Given a pointer to a tree node for a subrange type, return a pointer
8425     to a DIE that describes the given type.  */
8426
8427 static dw_die_ref
8428 subrange_type_die (tree type, dw_die_ref context_die)
8429 {
8430   dw_die_ref subrange_die;
8431   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8432
8433   if (context_die == NULL)
8434     context_die = comp_unit_die;
8435
8436   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8437
8438   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8439     {
8440       /* The size of the subrange type and its base type do not match,
8441          so we need to generate a size attribute for the subrange type.  */
8442       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8443     }
8444
8445   if (TYPE_MIN_VALUE (type) != NULL)
8446     add_bound_info (subrange_die, DW_AT_lower_bound,
8447                     TYPE_MIN_VALUE (type));
8448   if (TYPE_MAX_VALUE (type) != NULL)
8449     add_bound_info (subrange_die, DW_AT_upper_bound,
8450                     TYPE_MAX_VALUE (type));
8451
8452   return subrange_die;
8453 }
8454
8455 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8456    entry that chains various modifiers in front of the given type.  */
8457
8458 static dw_die_ref
8459 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8460                    dw_die_ref context_die)
8461 {
8462   enum tree_code code = TREE_CODE (type);
8463   dw_die_ref mod_type_die;
8464   dw_die_ref sub_die = NULL;
8465   tree item_type = NULL;
8466   tree qualified_type;
8467   tree name;
8468
8469   if (code == ERROR_MARK)
8470     return NULL;
8471
8472   /* See if we already have the appropriately qualified variant of
8473      this type.  */
8474   qualified_type
8475     = get_qualified_type (type,
8476                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8477                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8478   
8479   /* If we do, then we can just use its DIE, if it exists.  */
8480   if (qualified_type)
8481     {
8482       mod_type_die = lookup_type_die (qualified_type);
8483       if (mod_type_die)
8484         return mod_type_die;
8485     }
8486   
8487   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8488   
8489   /* Handle C typedef types.  */
8490   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8491     {
8492       tree dtype = TREE_TYPE (name);
8493       
8494       if (qualified_type == dtype)
8495         {
8496           /* For a named type, use the typedef.  */
8497           gen_type_die (qualified_type, context_die);
8498           return lookup_type_die (qualified_type);
8499         }
8500       else if (is_const_type < TYPE_READONLY (dtype)
8501                || is_volatile_type < TYPE_VOLATILE (dtype)
8502                || (is_const_type <= TYPE_READONLY (dtype)
8503                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8504                    && DECL_ORIGINAL_TYPE (name) != type))
8505         /* cv-unqualified version of named type.  Just use the unnamed
8506            type to which it refers.  */
8507         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8508                                   is_const_type, is_volatile_type,
8509                                   context_die);
8510       /* Else cv-qualified version of named type; fall through.  */
8511     }
8512   
8513   if (is_const_type)
8514     {
8515       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8516       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8517     }
8518   else if (is_volatile_type)
8519     {
8520       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8521       sub_die = modified_type_die (type, 0, 0, context_die);
8522     }
8523   else if (code == POINTER_TYPE)
8524     {
8525       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8526       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8527                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8528       item_type = TREE_TYPE (type);
8529     }
8530   else if (code == REFERENCE_TYPE)
8531     {
8532       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8533       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8534                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8535       item_type = TREE_TYPE (type);
8536     }
8537   else if (is_subrange_type (type))
8538     {
8539       mod_type_die = subrange_type_die (type, context_die);
8540       item_type = TREE_TYPE (type);
8541     }
8542   else if (is_base_type (type))
8543     mod_type_die = base_type_die (type);
8544   else
8545     {
8546       gen_type_die (type, context_die);
8547       
8548       /* We have to get the type_main_variant here (and pass that to the
8549          `lookup_type_die' routine) because the ..._TYPE node we have
8550          might simply be a *copy* of some original type node (where the
8551          copy was created to help us keep track of typedef names) and
8552          that copy might have a different TYPE_UID from the original
8553          ..._TYPE node.  */
8554       if (TREE_CODE (type) != VECTOR_TYPE)
8555         return lookup_type_die (type_main_variant (type));
8556       else
8557         /* Vectors have the debugging information in the type,
8558            not the main variant.  */
8559         return lookup_type_die (type);
8560     }
8561   
8562   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8563      don't output a DW_TAG_typedef, since there isn't one in the
8564      user's program; just attach a DW_AT_name to the type.  */
8565   if (name
8566       && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8567     {
8568       if (TREE_CODE (name) == TYPE_DECL)
8569         /* Could just call add_name_and_src_coords_attributes here,
8570            but since this is a builtin type it doesn't have any
8571            useful source coordinates anyway.  */
8572         name = DECL_NAME (name);
8573       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8574     }
8575   
8576   if (qualified_type)
8577     equate_type_number_to_die (qualified_type, mod_type_die);
8578
8579   if (item_type)
8580     /* We must do this after the equate_type_number_to_die call, in case
8581        this is a recursive type.  This ensures that the modified_type_die
8582        recursion will terminate even if the type is recursive.  Recursive
8583        types are possible in Ada.  */
8584     sub_die = modified_type_die (item_type,
8585                                  TYPE_READONLY (item_type),
8586                                  TYPE_VOLATILE (item_type),
8587                                  context_die);
8588
8589   if (sub_die != NULL)
8590     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8591
8592   return mod_type_die;
8593 }
8594
8595 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8596    an enumerated type.  */
8597
8598 static inline int
8599 type_is_enum (tree type)
8600 {
8601   return TREE_CODE (type) == ENUMERAL_TYPE;
8602 }
8603
8604 /* Return the DBX register number described by a given RTL node.  */
8605
8606 static unsigned int
8607 dbx_reg_number (rtx rtl)
8608 {
8609   unsigned regno = REGNO (rtl);
8610
8611   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8612
8613 #ifdef LEAF_REG_REMAP
8614   if (current_function_uses_only_leaf_regs)
8615     {
8616       int leaf_reg = LEAF_REG_REMAP (regno);
8617       if (leaf_reg != -1)
8618         regno = (unsigned) leaf_reg;
8619     }
8620 #endif
8621
8622   return DBX_REGISTER_NUMBER (regno);
8623 }
8624
8625 /* Optionally add a DW_OP_piece term to a location description expression.
8626    DW_OP_piece is only added if the location description expression already
8627    doesn't end with DW_OP_piece.  */
8628
8629 static void
8630 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8631 {
8632   dw_loc_descr_ref loc;
8633
8634   if (*list_head != NULL)
8635     {
8636       /* Find the end of the chain.  */
8637       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8638         ;
8639
8640       if (loc->dw_loc_opc != DW_OP_piece)
8641         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8642     }
8643 }
8644
8645 /* Return a location descriptor that designates a machine register or
8646    zero if there is none.  */
8647
8648 static dw_loc_descr_ref
8649 reg_loc_descriptor (rtx rtl)
8650 {
8651   rtx regs;
8652
8653   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8654     return 0;
8655
8656   regs = targetm.dwarf_register_span (rtl);
8657
8658   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8659     return multiple_reg_loc_descriptor (rtl, regs);
8660   else
8661     return one_reg_loc_descriptor (dbx_reg_number (rtl));
8662 }
8663
8664 /* Return a location descriptor that designates a machine register for
8665    a given hard register number.  */
8666
8667 static dw_loc_descr_ref
8668 one_reg_loc_descriptor (unsigned int regno)
8669 {
8670   if (regno <= 31)
8671     return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8672   else
8673     return new_loc_descr (DW_OP_regx, regno, 0);
8674 }
8675
8676 /* Given an RTL of a register, return a location descriptor that
8677    designates a value that spans more than one register.  */
8678
8679 static dw_loc_descr_ref
8680 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8681 {
8682   int nregs, size, i;
8683   unsigned reg;
8684   dw_loc_descr_ref loc_result = NULL;
8685
8686   reg = REGNO (rtl);
8687 #ifdef LEAF_REG_REMAP
8688   if (current_function_uses_only_leaf_regs)
8689     {
8690       int leaf_reg = LEAF_REG_REMAP (reg);
8691       if (leaf_reg != -1)
8692         reg = (unsigned) leaf_reg;
8693     }
8694 #endif
8695   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8696   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8697
8698   /* Simple, contiguous registers.  */
8699   if (regs == NULL_RTX)
8700     {
8701       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8702
8703       loc_result = NULL;
8704       while (nregs--)
8705         {
8706           dw_loc_descr_ref t;
8707
8708           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8709           add_loc_descr (&loc_result, t);
8710           add_loc_descr_op_piece (&loc_result, size);
8711           ++reg;
8712         }
8713       return loc_result;
8714     }
8715
8716   /* Now onto stupid register sets in non contiguous locations.  */
8717
8718   gcc_assert (GET_CODE (regs) == PARALLEL);
8719
8720   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8721   loc_result = NULL;
8722
8723   for (i = 0; i < XVECLEN (regs, 0); ++i)
8724     {
8725       dw_loc_descr_ref t;
8726
8727       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8728       add_loc_descr (&loc_result, t);
8729       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8730       add_loc_descr_op_piece (&loc_result, size);
8731     }
8732   return loc_result;
8733 }
8734
8735 /* Return a location descriptor that designates a constant.  */
8736
8737 static dw_loc_descr_ref
8738 int_loc_descriptor (HOST_WIDE_INT i)
8739 {
8740   enum dwarf_location_atom op;
8741
8742   /* Pick the smallest representation of a constant, rather than just
8743      defaulting to the LEB encoding.  */
8744   if (i >= 0)
8745     {
8746       if (i <= 31)
8747         op = DW_OP_lit0 + i;
8748       else if (i <= 0xff)
8749         op = DW_OP_const1u;
8750       else if (i <= 0xffff)
8751         op = DW_OP_const2u;
8752       else if (HOST_BITS_PER_WIDE_INT == 32
8753                || i <= 0xffffffff)
8754         op = DW_OP_const4u;
8755       else
8756         op = DW_OP_constu;
8757     }
8758   else
8759     {
8760       if (i >= -0x80)
8761         op = DW_OP_const1s;
8762       else if (i >= -0x8000)
8763         op = DW_OP_const2s;
8764       else if (HOST_BITS_PER_WIDE_INT == 32
8765                || i >= -0x80000000)
8766         op = DW_OP_const4s;
8767       else
8768         op = DW_OP_consts;
8769     }
8770
8771   return new_loc_descr (op, i, 0);
8772 }
8773
8774 /* Return a location descriptor that designates a base+offset location.  */
8775
8776 static dw_loc_descr_ref
8777 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8778 {
8779   unsigned int regno;
8780
8781   /* We only use "frame base" when we're sure we're talking about the
8782      post-prologue local stack frame.  We do this by *not* running
8783      register elimination until this point, and recognizing the special
8784      argument pointer and soft frame pointer rtx's.  */
8785   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8786     {
8787       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8788
8789       if (elim != reg)
8790         {
8791           if (GET_CODE (elim) == PLUS)
8792             {
8793               offset += INTVAL (XEXP (elim, 1));
8794               elim = XEXP (elim, 0);
8795             }
8796           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8797                       : stack_pointer_rtx));
8798           offset += frame_pointer_fb_offset;
8799
8800           return new_loc_descr (DW_OP_fbreg, offset, 0);
8801         }
8802     }
8803
8804   regno = dbx_reg_number (reg);
8805   if (regno <= 31)
8806     return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8807   else
8808     return new_loc_descr (DW_OP_bregx, regno, offset);
8809 }
8810
8811 /* Return true if this RTL expression describes a base+offset calculation.  */
8812
8813 static inline int
8814 is_based_loc (rtx rtl)
8815 {
8816   return (GET_CODE (rtl) == PLUS
8817           && ((REG_P (XEXP (rtl, 0))
8818                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8819                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8820 }
8821
8822 /* Return a descriptor that describes the concatenation of N locations
8823    used to form the address of a memory location.  */
8824
8825 static dw_loc_descr_ref
8826 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode)
8827 {
8828   unsigned int i;
8829   dw_loc_descr_ref cc_loc_result = NULL;
8830   unsigned int n = XVECLEN (concatn, 0);
8831
8832   for (i = 0; i < n; ++i)
8833     {
8834       dw_loc_descr_ref ref;
8835       rtx x = XVECEXP (concatn, 0, i);
8836
8837       ref = mem_loc_descriptor (x, mode);
8838       if (ref == NULL)
8839         return NULL;
8840
8841       add_loc_descr (&cc_loc_result, ref);
8842       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
8843     }
8844
8845   return cc_loc_result;
8846 }
8847
8848 /* The following routine converts the RTL for a variable or parameter
8849    (resident in memory) into an equivalent Dwarf representation of a
8850    mechanism for getting the address of that same variable onto the top of a
8851    hypothetical "address evaluation" stack.
8852
8853    When creating memory location descriptors, we are effectively transforming
8854    the RTL for a memory-resident object into its Dwarf postfix expression
8855    equivalent.  This routine recursively descends an RTL tree, turning
8856    it into Dwarf postfix code as it goes.
8857
8858    MODE is the mode of the memory reference, needed to handle some
8859    autoincrement addressing modes.
8860
8861    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8862    location list for RTL.
8863
8864    Return 0 if we can't represent the location.  */
8865
8866 static dw_loc_descr_ref
8867 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8868 {
8869   dw_loc_descr_ref mem_loc_result = NULL;
8870   enum dwarf_location_atom op;
8871
8872   /* Note that for a dynamically sized array, the location we will generate a
8873      description of here will be the lowest numbered location which is
8874      actually within the array.  That's *not* necessarily the same as the
8875      zeroth element of the array.  */
8876
8877   rtl = targetm.delegitimize_address (rtl);
8878
8879   switch (GET_CODE (rtl))
8880     {
8881     case POST_INC:
8882     case POST_DEC:
8883     case POST_MODIFY:
8884       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
8885          just fall into the SUBREG code.  */
8886
8887       /* ... fall through ...  */
8888
8889     case SUBREG:
8890       /* The case of a subreg may arise when we have a local (register)
8891          variable or a formal (register) parameter which doesn't quite fill
8892          up an entire register.  For now, just assume that it is
8893          legitimate to make the Dwarf info refer to the whole register which
8894          contains the given subreg.  */
8895       rtl = XEXP (rtl, 0);
8896
8897       /* ... fall through ...  */
8898
8899     case REG:
8900       /* Whenever a register number forms a part of the description of the
8901          method for calculating the (dynamic) address of a memory resident
8902          object, DWARF rules require the register number be referred to as
8903          a "base register".  This distinction is not based in any way upon
8904          what category of register the hardware believes the given register
8905          belongs to.  This is strictly DWARF terminology we're dealing with
8906          here. Note that in cases where the location of a memory-resident
8907          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8908          OP_CONST (0)) the actual DWARF location descriptor that we generate
8909          may just be OP_BASEREG (basereg).  This may look deceptively like
8910          the object in question was allocated to a register (rather than in
8911          memory) so DWARF consumers need to be aware of the subtle
8912          distinction between OP_REG and OP_BASEREG.  */
8913       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8914         mem_loc_result = based_loc_descr (rtl, 0);
8915       break;
8916
8917     case MEM:
8918       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8919       if (mem_loc_result != 0)
8920         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8921       break;
8922
8923     case LO_SUM:
8924          rtl = XEXP (rtl, 1);
8925
8926       /* ... fall through ...  */
8927
8928     case LABEL_REF:
8929       /* Some ports can transform a symbol ref into a label ref, because
8930          the symbol ref is too far away and has to be dumped into a constant
8931          pool.  */
8932     case CONST:
8933     case SYMBOL_REF:
8934       /* Alternatively, the symbol in the constant pool might be referenced
8935          by a different symbol.  */
8936       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8937         {
8938           bool marked;
8939           rtx tmp = get_pool_constant_mark (rtl, &marked);
8940
8941           if (GET_CODE (tmp) == SYMBOL_REF)
8942             {
8943               rtl = tmp;
8944               if (CONSTANT_POOL_ADDRESS_P (tmp))
8945                 get_pool_constant_mark (tmp, &marked);
8946               else
8947                 marked = true;
8948             }
8949
8950           /* If all references to this pool constant were optimized away,
8951              it was not output and thus we can't represent it.
8952              FIXME: might try to use DW_OP_const_value here, though
8953              DW_OP_piece complicates it.  */
8954           if (!marked)
8955             return 0;
8956         }
8957
8958       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8959       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8960       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8961       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8962       break;
8963
8964     case PRE_MODIFY:
8965       /* Extract the PLUS expression nested inside and fall into
8966          PLUS code below.  */
8967       rtl = XEXP (rtl, 1);
8968       goto plus;
8969
8970     case PRE_INC:
8971     case PRE_DEC:
8972       /* Turn these into a PLUS expression and fall into the PLUS code
8973          below.  */
8974       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8975                           GEN_INT (GET_CODE (rtl) == PRE_INC
8976                                    ? GET_MODE_UNIT_SIZE (mode)
8977                                    : -GET_MODE_UNIT_SIZE (mode)));
8978
8979       /* ... fall through ...  */
8980
8981     case PLUS:
8982     plus:
8983       if (is_based_loc (rtl))
8984         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8985                                           INTVAL (XEXP (rtl, 1)));
8986       else
8987         {
8988           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8989           if (mem_loc_result == 0)
8990             break;
8991
8992           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8993               && INTVAL (XEXP (rtl, 1)) >= 0)
8994             add_loc_descr (&mem_loc_result,
8995                            new_loc_descr (DW_OP_plus_uconst,
8996                                           INTVAL (XEXP (rtl, 1)), 0));
8997           else
8998             {
8999               add_loc_descr (&mem_loc_result,
9000                              mem_loc_descriptor (XEXP (rtl, 1), mode));
9001               add_loc_descr (&mem_loc_result,
9002                              new_loc_descr (DW_OP_plus, 0, 0));
9003             }
9004         }
9005       break;
9006
9007     /* If a pseudo-reg is optimized away, it is possible for it to
9008        be replaced with a MEM containing a multiply or shift.  */
9009     case MULT:
9010       op = DW_OP_mul;
9011       goto do_binop;
9012
9013     case ASHIFT:
9014       op = DW_OP_shl;
9015       goto do_binop;
9016
9017     case ASHIFTRT:
9018       op = DW_OP_shra;
9019       goto do_binop;
9020
9021     case LSHIFTRT:
9022       op = DW_OP_shr;
9023       goto do_binop;
9024
9025     do_binop:
9026       {
9027         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
9028         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
9029
9030         if (op0 == 0 || op1 == 0)
9031           break;
9032
9033         mem_loc_result = op0;
9034         add_loc_descr (&mem_loc_result, op1);
9035         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9036         break;
9037       }
9038
9039     case CONST_INT:
9040       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9041       break;
9042
9043     case CONCATN:
9044       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode);
9045       break;
9046
9047     default:
9048       gcc_unreachable ();
9049     }
9050
9051   return mem_loc_result;
9052 }
9053
9054 /* Return a descriptor that describes the concatenation of two locations.
9055    This is typically a complex variable.  */
9056
9057 static dw_loc_descr_ref
9058 concat_loc_descriptor (rtx x0, rtx x1)
9059 {
9060   dw_loc_descr_ref cc_loc_result = NULL;
9061   dw_loc_descr_ref x0_ref = loc_descriptor (x0);
9062   dw_loc_descr_ref x1_ref = loc_descriptor (x1);
9063
9064   if (x0_ref == 0 || x1_ref == 0)
9065     return 0;
9066
9067   cc_loc_result = x0_ref;
9068   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9069
9070   add_loc_descr (&cc_loc_result, x1_ref);
9071   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9072
9073   return cc_loc_result;
9074 }
9075
9076 /* Return a descriptor that describes the concatenation of N
9077    locations.  */
9078
9079 static dw_loc_descr_ref
9080 concatn_loc_descriptor (rtx concatn)
9081 {
9082   unsigned int i;
9083   dw_loc_descr_ref cc_loc_result = NULL;
9084   unsigned int n = XVECLEN (concatn, 0);
9085
9086   for (i = 0; i < n; ++i)
9087     {
9088       dw_loc_descr_ref ref;
9089       rtx x = XVECEXP (concatn, 0, i);
9090
9091       ref = loc_descriptor (x);
9092       if (ref == NULL)
9093         return NULL;
9094
9095       add_loc_descr (&cc_loc_result, ref);
9096       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9097     }
9098
9099   return cc_loc_result;
9100 }
9101
9102 /* Output a proper Dwarf location descriptor for a variable or parameter
9103    which is either allocated in a register or in a memory location.  For a
9104    register, we just generate an OP_REG and the register number.  For a
9105    memory location we provide a Dwarf postfix expression describing how to
9106    generate the (dynamic) address of the object onto the address stack.
9107
9108    If we don't know how to describe it, return 0.  */
9109
9110 static dw_loc_descr_ref
9111 loc_descriptor (rtx rtl)
9112 {
9113   dw_loc_descr_ref loc_result = NULL;
9114
9115   switch (GET_CODE (rtl))
9116     {
9117     case SUBREG:
9118       /* The case of a subreg may arise when we have a local (register)
9119          variable or a formal (register) parameter which doesn't quite fill
9120          up an entire register.  For now, just assume that it is
9121          legitimate to make the Dwarf info refer to the whole register which
9122          contains the given subreg.  */
9123       rtl = SUBREG_REG (rtl);
9124
9125       /* ... fall through ...  */
9126
9127     case REG:
9128       loc_result = reg_loc_descriptor (rtl);
9129       break;
9130
9131     case MEM:
9132       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
9133       break;
9134
9135     case CONCAT:
9136       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
9137       break;
9138
9139     case CONCATN:
9140       loc_result = concatn_loc_descriptor (rtl);
9141       break;
9142
9143     case VAR_LOCATION:
9144       /* Single part.  */
9145       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9146         {
9147           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
9148           break;
9149         }
9150
9151       rtl = XEXP (rtl, 1);
9152       /* FALLTHRU */
9153
9154     case PARALLEL:
9155       {
9156         rtvec par_elems = XVEC (rtl, 0);
9157         int num_elem = GET_NUM_ELEM (par_elems);
9158         enum machine_mode mode;
9159         int i;
9160
9161         /* Create the first one, so we have something to add to.  */
9162         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9163         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9164         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9165         for (i = 1; i < num_elem; i++)
9166           {
9167             dw_loc_descr_ref temp;
9168
9169             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9170             add_loc_descr (&loc_result, temp);
9171             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9172             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9173           }
9174       }
9175       break;
9176
9177     default:
9178       gcc_unreachable ();
9179     }
9180
9181   return loc_result;
9182 }
9183
9184 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9185    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9186    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9187    top-level invocation, and we require the address of LOC; is 0 if we require
9188    the value of LOC.  */
9189
9190 static dw_loc_descr_ref
9191 loc_descriptor_from_tree_1 (tree loc, int want_address)
9192 {
9193   dw_loc_descr_ref ret, ret1;
9194   int have_address = 0;
9195   enum dwarf_location_atom op;
9196
9197   /* ??? Most of the time we do not take proper care for sign/zero
9198      extending the values properly.  Hopefully this won't be a real
9199      problem...  */
9200
9201   switch (TREE_CODE (loc))
9202     {
9203     case ERROR_MARK:
9204       return 0;
9205
9206     case PLACEHOLDER_EXPR:
9207       /* This case involves extracting fields from an object to determine the
9208          position of other fields.  We don't try to encode this here.  The
9209          only user of this is Ada, which encodes the needed information using
9210          the names of types.  */
9211       return 0;
9212
9213     case CALL_EXPR:
9214       return 0;
9215
9216     case PREINCREMENT_EXPR:
9217     case PREDECREMENT_EXPR:
9218     case POSTINCREMENT_EXPR:
9219     case POSTDECREMENT_EXPR:
9220       /* There are no opcodes for these operations.  */
9221       return 0;
9222
9223     case ADDR_EXPR:
9224       /* If we already want an address, there's nothing we can do.  */
9225       if (want_address)
9226         return 0;
9227
9228       /* Otherwise, process the argument and look for the address.  */
9229       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9230
9231     case VAR_DECL:
9232       if (DECL_THREAD_LOCAL_P (loc))
9233         {
9234           rtx rtl;
9235
9236           /* If this is not defined, we have no way to emit the data.  */
9237           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9238             return 0;
9239
9240           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9241              look up addresses of objects in the current module.  */
9242           if (DECL_EXTERNAL (loc))
9243             return 0;
9244
9245           rtl = rtl_for_decl_location (loc);
9246           if (rtl == NULL_RTX)
9247             return 0;
9248
9249           if (!MEM_P (rtl))
9250             return 0;
9251           rtl = XEXP (rtl, 0);
9252           if (! CONSTANT_P (rtl))
9253             return 0;
9254
9255           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9256           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9257           ret->dw_loc_oprnd1.v.val_addr = rtl;
9258
9259           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9260           add_loc_descr (&ret, ret1);
9261
9262           have_address = 1;
9263           break;
9264         }
9265       /* FALLTHRU */
9266
9267     case PARM_DECL:
9268       if (DECL_HAS_VALUE_EXPR_P (loc))
9269         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9270                                            want_address);
9271       /* FALLTHRU */
9272
9273     case RESULT_DECL:
9274     case FUNCTION_DECL:
9275       {
9276         rtx rtl = rtl_for_decl_location (loc);
9277
9278         if (rtl == NULL_RTX)
9279           return 0;
9280         else if (GET_CODE (rtl) == CONST_INT)
9281           {
9282             HOST_WIDE_INT val = INTVAL (rtl);
9283             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9284               val &= GET_MODE_MASK (DECL_MODE (loc));
9285             ret = int_loc_descriptor (val);
9286           }
9287         else if (GET_CODE (rtl) == CONST_STRING)
9288           return 0;
9289         else if (CONSTANT_P (rtl))
9290           {
9291             ret = new_loc_descr (DW_OP_addr, 0, 0);
9292             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9293             ret->dw_loc_oprnd1.v.val_addr = rtl;
9294           }
9295         else
9296           {
9297             enum machine_mode mode;
9298
9299             /* Certain constructs can only be represented at top-level.  */
9300             if (want_address == 2)
9301               return loc_descriptor (rtl);
9302
9303             mode = GET_MODE (rtl);
9304             if (MEM_P (rtl))
9305               {
9306                 rtl = XEXP (rtl, 0);
9307                 have_address = 1;
9308               }
9309             ret = mem_loc_descriptor (rtl, mode);
9310           }
9311       }
9312       break;
9313
9314     case INDIRECT_REF:
9315       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9316       have_address = 1;
9317       break;
9318
9319     case COMPOUND_EXPR:
9320       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9321
9322     case NOP_EXPR:
9323     case CONVERT_EXPR:
9324     case NON_LVALUE_EXPR:
9325     case VIEW_CONVERT_EXPR:
9326     case SAVE_EXPR:
9327     case GIMPLE_MODIFY_STMT:
9328       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9329                                          want_address);
9330
9331     case COMPONENT_REF:
9332     case BIT_FIELD_REF:
9333     case ARRAY_REF:
9334     case ARRAY_RANGE_REF:
9335       {
9336         tree obj, offset;
9337         HOST_WIDE_INT bitsize, bitpos, bytepos;
9338         enum machine_mode mode;
9339         int volatilep;
9340         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9341
9342         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9343                                    &unsignedp, &volatilep, false);
9344
9345         if (obj == loc)
9346           return 0;
9347
9348         ret = loc_descriptor_from_tree_1 (obj, 1);
9349         if (ret == 0
9350             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9351           return 0;
9352
9353         if (offset != NULL_TREE)
9354           {
9355             /* Variable offset.  */
9356             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9357             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9358           }
9359
9360         bytepos = bitpos / BITS_PER_UNIT;
9361         if (bytepos > 0)
9362           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9363         else if (bytepos < 0)
9364           {
9365             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9366             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9367           }
9368
9369         have_address = 1;
9370         break;
9371       }
9372
9373     case INTEGER_CST:
9374       if (host_integerp (loc, 0))
9375         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9376       else
9377         return 0;
9378       break;
9379
9380     case CONSTRUCTOR:
9381       {
9382         /* Get an RTL for this, if something has been emitted.  */
9383         rtx rtl = lookup_constant_def (loc);
9384         enum machine_mode mode;
9385
9386         if (!rtl || !MEM_P (rtl))
9387           return 0;
9388         mode = GET_MODE (rtl);
9389         rtl = XEXP (rtl, 0);
9390         ret = mem_loc_descriptor (rtl, mode);
9391         have_address = 1;
9392         break;
9393       }
9394
9395     case TRUTH_AND_EXPR:
9396     case TRUTH_ANDIF_EXPR:
9397     case BIT_AND_EXPR:
9398       op = DW_OP_and;
9399       goto do_binop;
9400
9401     case TRUTH_XOR_EXPR:
9402     case BIT_XOR_EXPR:
9403       op = DW_OP_xor;
9404       goto do_binop;
9405
9406     case TRUTH_OR_EXPR:
9407     case TRUTH_ORIF_EXPR:
9408     case BIT_IOR_EXPR:
9409       op = DW_OP_or;
9410       goto do_binop;
9411
9412     case FLOOR_DIV_EXPR:
9413     case CEIL_DIV_EXPR:
9414     case ROUND_DIV_EXPR:
9415     case TRUNC_DIV_EXPR:
9416       op = DW_OP_div;
9417       goto do_binop;
9418
9419     case MINUS_EXPR:
9420       op = DW_OP_minus;
9421       goto do_binop;
9422
9423     case FLOOR_MOD_EXPR:
9424     case CEIL_MOD_EXPR:
9425     case ROUND_MOD_EXPR:
9426     case TRUNC_MOD_EXPR:
9427       op = DW_OP_mod;
9428       goto do_binop;
9429
9430     case MULT_EXPR:
9431       op = DW_OP_mul;
9432       goto do_binop;
9433
9434     case LSHIFT_EXPR:
9435       op = DW_OP_shl;
9436       goto do_binop;
9437
9438     case RSHIFT_EXPR:
9439       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9440       goto do_binop;
9441
9442     case PLUS_EXPR:
9443       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9444           && host_integerp (TREE_OPERAND (loc, 1), 0))
9445         {
9446           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9447           if (ret == 0)
9448             return 0;
9449
9450           add_loc_descr (&ret,
9451                          new_loc_descr (DW_OP_plus_uconst,
9452                                         tree_low_cst (TREE_OPERAND (loc, 1),
9453                                                       0),
9454                                         0));
9455           break;
9456         }
9457
9458       op = DW_OP_plus;
9459       goto do_binop;
9460
9461     case LE_EXPR:
9462       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9463         return 0;
9464
9465       op = DW_OP_le;
9466       goto do_binop;
9467
9468     case GE_EXPR:
9469       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9470         return 0;
9471
9472       op = DW_OP_ge;
9473       goto do_binop;
9474
9475     case LT_EXPR:
9476       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9477         return 0;
9478
9479       op = DW_OP_lt;
9480       goto do_binop;
9481
9482     case GT_EXPR:
9483       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9484         return 0;
9485
9486       op = DW_OP_gt;
9487       goto do_binop;
9488
9489     case EQ_EXPR:
9490       op = DW_OP_eq;
9491       goto do_binop;
9492
9493     case NE_EXPR:
9494       op = DW_OP_ne;
9495       goto do_binop;
9496
9497     do_binop:
9498       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9499       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9500       if (ret == 0 || ret1 == 0)
9501         return 0;
9502
9503       add_loc_descr (&ret, ret1);
9504       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9505       break;
9506
9507     case TRUTH_NOT_EXPR:
9508     case BIT_NOT_EXPR:
9509       op = DW_OP_not;
9510       goto do_unop;
9511
9512     case ABS_EXPR:
9513       op = DW_OP_abs;
9514       goto do_unop;
9515
9516     case NEGATE_EXPR:
9517       op = DW_OP_neg;
9518       goto do_unop;
9519
9520     do_unop:
9521       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9522       if (ret == 0)
9523         return 0;
9524
9525       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9526       break;
9527
9528     case MIN_EXPR:
9529     case MAX_EXPR:
9530       {
9531         const enum tree_code code =
9532           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9533
9534         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9535                       build2 (code, integer_type_node,
9536                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9537                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9538       }
9539
9540       /* ... fall through ...  */
9541
9542     case COND_EXPR:
9543       {
9544         dw_loc_descr_ref lhs
9545           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9546         dw_loc_descr_ref rhs
9547           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9548         dw_loc_descr_ref bra_node, jump_node, tmp;
9549
9550         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9551         if (ret == 0 || lhs == 0 || rhs == 0)
9552           return 0;
9553
9554         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9555         add_loc_descr (&ret, bra_node);
9556
9557         add_loc_descr (&ret, rhs);
9558         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9559         add_loc_descr (&ret, jump_node);
9560
9561         add_loc_descr (&ret, lhs);
9562         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9563         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9564
9565         /* ??? Need a node to point the skip at.  Use a nop.  */
9566         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9567         add_loc_descr (&ret, tmp);
9568         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9569         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9570       }
9571       break;
9572
9573     case FIX_TRUNC_EXPR:
9574       return 0;
9575
9576     default:
9577       /* Leave front-end specific codes as simply unknown.  This comes
9578          up, for instance, with the C STMT_EXPR.  */
9579       if ((unsigned int) TREE_CODE (loc)
9580           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9581         return 0;
9582
9583 #ifdef ENABLE_CHECKING
9584       /* Otherwise this is a generic code; we should just lists all of
9585          these explicitly.  We forgot one.  */
9586       gcc_unreachable ();
9587 #else
9588       /* In a release build, we want to degrade gracefully: better to
9589          generate incomplete debugging information than to crash.  */
9590       return NULL;
9591 #endif
9592     }
9593
9594   /* Show if we can't fill the request for an address.  */
9595   if (want_address && !have_address)
9596     return 0;
9597
9598   /* If we've got an address and don't want one, dereference.  */
9599   if (!want_address && have_address && ret)
9600     {
9601       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9602
9603       if (size > DWARF2_ADDR_SIZE || size == -1)
9604         return 0;
9605       else if (size == DWARF2_ADDR_SIZE)
9606         op = DW_OP_deref;
9607       else
9608         op = DW_OP_deref_size;
9609
9610       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9611     }
9612
9613   return ret;
9614 }
9615
9616 static inline dw_loc_descr_ref
9617 loc_descriptor_from_tree (tree loc)
9618 {
9619   return loc_descriptor_from_tree_1 (loc, 2);
9620 }
9621
9622 /* Given a value, round it up to the lowest multiple of `boundary'
9623    which is not less than the value itself.  */
9624
9625 static inline HOST_WIDE_INT
9626 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9627 {
9628   return (((value + boundary - 1) / boundary) * boundary);
9629 }
9630
9631 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9632    pointer to the declared type for the relevant field variable, or return
9633    `integer_type_node' if the given node turns out to be an
9634    ERROR_MARK node.  */
9635
9636 static inline tree
9637 field_type (tree decl)
9638 {
9639   tree type;
9640
9641   if (TREE_CODE (decl) == ERROR_MARK)
9642     return integer_type_node;
9643
9644   type = DECL_BIT_FIELD_TYPE (decl);
9645   if (type == NULL_TREE)
9646     type = TREE_TYPE (decl);
9647
9648   return type;
9649 }
9650
9651 /* Given a pointer to a tree node, return the alignment in bits for
9652    it, or else return BITS_PER_WORD if the node actually turns out to
9653    be an ERROR_MARK node.  */
9654
9655 static inline unsigned
9656 simple_type_align_in_bits (tree type)
9657 {
9658   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9659 }
9660
9661 static inline unsigned
9662 simple_decl_align_in_bits (tree decl)
9663 {
9664   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9665 }
9666
9667 /* Return the result of rounding T up to ALIGN.  */
9668
9669 static inline HOST_WIDE_INT
9670 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9671 {
9672   /* We must be careful if T is negative because HOST_WIDE_INT can be
9673      either "above" or "below" unsigned int as per the C promotion
9674      rules, depending on the host, thus making the signedness of the
9675      direct multiplication and division unpredictable.  */
9676   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9677
9678   u += align - 1;
9679   u /= align;
9680   u *= align;
9681
9682   return (HOST_WIDE_INT) u;
9683 }
9684
9685 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9686    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9687    or return 0 if we are unable to determine what that offset is, either
9688    because the argument turns out to be a pointer to an ERROR_MARK node, or
9689    because the offset is actually variable.  (We can't handle the latter case
9690    just yet).  */
9691
9692 static HOST_WIDE_INT
9693 field_byte_offset (tree decl)
9694 {
9695   unsigned int type_align_in_bits;
9696   unsigned int decl_align_in_bits;
9697   unsigned HOST_WIDE_INT type_size_in_bits;
9698   HOST_WIDE_INT object_offset_in_bits;
9699   tree type;
9700   tree field_size_tree;
9701   HOST_WIDE_INT bitpos_int;
9702   HOST_WIDE_INT deepest_bitpos;
9703   unsigned HOST_WIDE_INT field_size_in_bits;
9704
9705   if (TREE_CODE (decl) == ERROR_MARK)
9706     return 0;
9707
9708   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9709
9710   type = field_type (decl);
9711   field_size_tree = DECL_SIZE (decl);
9712
9713   /* The size could be unspecified if there was an error, or for
9714      a flexible array member.  */
9715   if (! field_size_tree)
9716     field_size_tree = bitsize_zero_node;
9717
9718   /* We cannot yet cope with fields whose positions are variable, so
9719      for now, when we see such things, we simply return 0.  Someday, we may
9720      be able to handle such cases, but it will be damn difficult.  */
9721   if (! host_integerp (bit_position (decl), 0))
9722     return 0;
9723
9724   bitpos_int = int_bit_position (decl);
9725
9726   /* If we don't know the size of the field, pretend it's a full word.  */
9727   if (host_integerp (field_size_tree, 1))
9728     field_size_in_bits = tree_low_cst (field_size_tree, 1);
9729   else
9730     field_size_in_bits = BITS_PER_WORD;
9731
9732   type_size_in_bits = simple_type_size_in_bits (type);
9733   type_align_in_bits = simple_type_align_in_bits (type);
9734   decl_align_in_bits = simple_decl_align_in_bits (decl);
9735
9736   /* The GCC front-end doesn't make any attempt to keep track of the starting
9737      bit offset (relative to the start of the containing structure type) of the
9738      hypothetical "containing object" for a bit-field.  Thus, when computing
9739      the byte offset value for the start of the "containing object" of a
9740      bit-field, we must deduce this information on our own. This can be rather
9741      tricky to do in some cases.  For example, handling the following structure
9742      type definition when compiling for an i386/i486 target (which only aligns
9743      long long's to 32-bit boundaries) can be very tricky:
9744
9745          struct S { int field1; long long field2:31; };
9746
9747      Fortunately, there is a simple rule-of-thumb which can be used in such
9748      cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
9749      structure shown above.  It decides to do this based upon one simple rule
9750      for bit-field allocation.  GCC allocates each "containing object" for each
9751      bit-field at the first (i.e. lowest addressed) legitimate alignment
9752      boundary (based upon the required minimum alignment for the declared type
9753      of the field) which it can possibly use, subject to the condition that
9754      there is still enough available space remaining in the containing object
9755      (when allocated at the selected point) to fully accommodate all of the
9756      bits of the bit-field itself.
9757
9758      This simple rule makes it obvious why GCC allocates 8 bytes for each
9759      object of the structure type shown above.  When looking for a place to
9760      allocate the "containing object" for `field2', the compiler simply tries
9761      to allocate a 64-bit "containing object" at each successive 32-bit
9762      boundary (starting at zero) until it finds a place to allocate that 64-
9763      bit field such that at least 31 contiguous (and previously unallocated)
9764      bits remain within that selected 64 bit field.  (As it turns out, for the
9765      example above, the compiler finds it is OK to allocate the "containing
9766      object" 64-bit field at bit-offset zero within the structure type.)
9767
9768      Here we attempt to work backwards from the limited set of facts we're
9769      given, and we try to deduce from those facts, where GCC must have believed
9770      that the containing object started (within the structure type). The value
9771      we deduce is then used (by the callers of this routine) to generate
9772      DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9773      and, in the case of DW_AT_location, regular fields as well).  */
9774
9775   /* Figure out the bit-distance from the start of the structure to the
9776      "deepest" bit of the bit-field.  */
9777   deepest_bitpos = bitpos_int + field_size_in_bits;
9778
9779   /* This is the tricky part.  Use some fancy footwork to deduce where the
9780      lowest addressed bit of the containing object must be.  */
9781   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9782
9783   /* Round up to type_align by default.  This works best for bitfields.  */
9784   object_offset_in_bits
9785     = round_up_to_align (object_offset_in_bits, type_align_in_bits);
9786
9787   if (object_offset_in_bits > bitpos_int)
9788     {
9789       /* Sigh, the decl must be packed.  */
9790       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9791
9792       /* Round up to decl_align instead.  */
9793       object_offset_in_bits
9794         = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
9795     }
9796
9797   return object_offset_in_bits / BITS_PER_UNIT;
9798 }
9799 \f
9800 /* The following routines define various Dwarf attributes and any data
9801    associated with them.  */
9802
9803 /* Add a location description attribute value to a DIE.
9804
9805    This emits location attributes suitable for whole variables and
9806    whole parameters.  Note that the location attributes for struct fields are
9807    generated by the routine `data_member_location_attribute' below.  */
9808
9809 static inline void
9810 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9811                              dw_loc_descr_ref descr)
9812 {
9813   if (descr != 0)
9814     add_AT_loc (die, attr_kind, descr);
9815 }
9816
9817 /* Attach the specialized form of location attribute used for data members of
9818    struct and union types.  In the special case of a FIELD_DECL node which
9819    represents a bit-field, the "offset" part of this special location
9820    descriptor must indicate the distance in bytes from the lowest-addressed
9821    byte of the containing struct or union type to the lowest-addressed byte of
9822    the "containing object" for the bit-field.  (See the `field_byte_offset'
9823    function above).
9824
9825    For any given bit-field, the "containing object" is a hypothetical object
9826    (of some integral or enum type) within which the given bit-field lives.  The
9827    type of this hypothetical "containing object" is always the same as the
9828    declared type of the individual bit-field itself (for GCC anyway... the
9829    DWARF spec doesn't actually mandate this).  Note that it is the size (in
9830    bytes) of the hypothetical "containing object" which will be given in the
9831    DW_AT_byte_size attribute for this bit-field.  (See the
9832    `byte_size_attribute' function below.)  It is also used when calculating the
9833    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
9834    function below.)  */
9835
9836 static void
9837 add_data_member_location_attribute (dw_die_ref die, tree decl)
9838 {
9839   HOST_WIDE_INT offset;
9840   dw_loc_descr_ref loc_descr = 0;
9841
9842   if (TREE_CODE (decl) == TREE_BINFO)
9843     {
9844       /* We're working on the TAG_inheritance for a base class.  */
9845       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9846         {
9847           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9848              aren't at a fixed offset from all (sub)objects of the same
9849              type.  We need to extract the appropriate offset from our
9850              vtable.  The following dwarf expression means
9851
9852                BaseAddr = ObAddr + *((*ObAddr) - Offset)
9853
9854              This is specific to the V3 ABI, of course.  */
9855
9856           dw_loc_descr_ref tmp;
9857
9858           /* Make a copy of the object address.  */
9859           tmp = new_loc_descr (DW_OP_dup, 0, 0);
9860           add_loc_descr (&loc_descr, tmp);
9861
9862           /* Extract the vtable address.  */
9863           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9864           add_loc_descr (&loc_descr, tmp);
9865
9866           /* Calculate the address of the offset.  */
9867           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9868           gcc_assert (offset < 0);
9869
9870           tmp = int_loc_descriptor (-offset);
9871           add_loc_descr (&loc_descr, tmp);
9872           tmp = new_loc_descr (DW_OP_minus, 0, 0);
9873           add_loc_descr (&loc_descr, tmp);
9874
9875           /* Extract the offset.  */
9876           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9877           add_loc_descr (&loc_descr, tmp);
9878
9879           /* Add it to the object address.  */
9880           tmp = new_loc_descr (DW_OP_plus, 0, 0);
9881           add_loc_descr (&loc_descr, tmp);
9882         }
9883       else
9884         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9885     }
9886   else
9887     offset = field_byte_offset (decl);
9888
9889   if (! loc_descr)
9890     {
9891       enum dwarf_location_atom op;
9892
9893       /* The DWARF2 standard says that we should assume that the structure
9894          address is already on the stack, so we can specify a structure field
9895          address by using DW_OP_plus_uconst.  */
9896
9897 #ifdef MIPS_DEBUGGING_INFO
9898       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9899          operator correctly.  It works only if we leave the offset on the
9900          stack.  */
9901       op = DW_OP_constu;
9902 #else
9903       op = DW_OP_plus_uconst;
9904 #endif
9905
9906       loc_descr = new_loc_descr (op, offset, 0);
9907     }
9908
9909   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9910 }
9911
9912 /* Writes integer values to dw_vec_const array.  */
9913
9914 static void
9915 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9916 {
9917   while (size != 0)
9918     {
9919       *dest++ = val & 0xff;
9920       val >>= 8;
9921       --size;
9922     }
9923 }
9924
9925 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9926
9927 static HOST_WIDE_INT
9928 extract_int (const unsigned char *src, unsigned int size)
9929 {
9930   HOST_WIDE_INT val = 0;
9931
9932   src += size;
9933   while (size != 0)
9934     {
9935       val <<= 8;
9936       val |= *--src & 0xff;
9937       --size;
9938     }
9939   return val;
9940 }
9941
9942 /* Writes floating point values to dw_vec_const array.  */
9943
9944 static void
9945 insert_float (rtx rtl, unsigned char *array)
9946 {
9947   REAL_VALUE_TYPE rv;
9948   long val[4];
9949   int i;
9950
9951   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9952   real_to_target (val, &rv, GET_MODE (rtl));
9953
9954   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9955   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9956     {
9957       insert_int (val[i], 4, array);
9958       array += 4;
9959     }
9960 }
9961
9962 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9963    does not have a "location" either in memory or in a register.  These
9964    things can arise in GNU C when a constant is passed as an actual parameter
9965    to an inlined function.  They can also arise in C++ where declared
9966    constants do not necessarily get memory "homes".  */
9967
9968 static void
9969 add_const_value_attribute (dw_die_ref die, rtx rtl)
9970 {
9971   switch (GET_CODE (rtl))
9972     {
9973     case CONST_INT:
9974       {
9975         HOST_WIDE_INT val = INTVAL (rtl);
9976
9977         if (val < 0)
9978           add_AT_int (die, DW_AT_const_value, val);
9979         else
9980           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9981       }
9982       break;
9983
9984     case CONST_DOUBLE:
9985       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9986          floating-point constant.  A CONST_DOUBLE is used whenever the
9987          constant requires more than one word in order to be adequately
9988          represented.  We output CONST_DOUBLEs as blocks.  */
9989       {
9990         enum machine_mode mode = GET_MODE (rtl);
9991
9992         if (SCALAR_FLOAT_MODE_P (mode))
9993           {
9994             unsigned int length = GET_MODE_SIZE (mode);
9995             unsigned char *array = ggc_alloc (length);
9996
9997             insert_float (rtl, array);
9998             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9999           }
10000         else
10001           {
10002             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10003             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10004
10005             add_AT_long_long (die, DW_AT_const_value,
10006                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10007           }
10008       }
10009       break;
10010
10011     case CONST_VECTOR:
10012       {
10013         enum machine_mode mode = GET_MODE (rtl);
10014         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10015         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10016         unsigned char *array = ggc_alloc (length * elt_size);
10017         unsigned int i;
10018         unsigned char *p;
10019
10020         switch (GET_MODE_CLASS (mode))
10021           {
10022           case MODE_VECTOR_INT:
10023             for (i = 0, p = array; i < length; i++, p += elt_size)
10024               {
10025                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10026                 HOST_WIDE_INT lo, hi;
10027
10028                 switch (GET_CODE (elt))
10029                   {
10030                   case CONST_INT:
10031                     lo = INTVAL (elt);
10032                     hi = -(lo < 0);
10033                     break;
10034
10035                   case CONST_DOUBLE:
10036                     lo = CONST_DOUBLE_LOW (elt);
10037                     hi = CONST_DOUBLE_HIGH (elt);
10038                     break;
10039
10040                   default:
10041                     gcc_unreachable ();
10042                   }
10043
10044                 if (elt_size <= sizeof (HOST_WIDE_INT))
10045                   insert_int (lo, elt_size, p);
10046                 else
10047                   {
10048                     unsigned char *p0 = p;
10049                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10050
10051                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10052                     if (WORDS_BIG_ENDIAN)
10053                       {
10054                         p0 = p1;
10055                         p1 = p;
10056                       }
10057                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10058                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10059                   }
10060               }
10061             break;
10062
10063           case MODE_VECTOR_FLOAT:
10064             for (i = 0, p = array; i < length; i++, p += elt_size)
10065               {
10066                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10067                 insert_float (elt, p);
10068               }
10069             break;
10070
10071           default:
10072             gcc_unreachable ();
10073           }
10074
10075         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10076       }
10077       break;
10078
10079     case CONST_STRING:
10080       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10081       break;
10082
10083     case SYMBOL_REF:
10084     case LABEL_REF:
10085     case CONST:
10086       add_AT_addr (die, DW_AT_const_value, rtl);
10087       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10088       break;
10089
10090     case PLUS:
10091       /* In cases where an inlined instance of an inline function is passed
10092          the address of an `auto' variable (which is local to the caller) we
10093          can get a situation where the DECL_RTL of the artificial local
10094          variable (for the inlining) which acts as a stand-in for the
10095          corresponding formal parameter (of the inline function) will look
10096          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10097          exactly a compile-time constant expression, but it isn't the address
10098          of the (artificial) local variable either.  Rather, it represents the
10099          *value* which the artificial local variable always has during its
10100          lifetime.  We currently have no way to represent such quasi-constant
10101          values in Dwarf, so for now we just punt and generate nothing.  */
10102       break;
10103
10104     default:
10105       /* No other kinds of rtx should be possible here.  */
10106       gcc_unreachable ();
10107     }
10108
10109 }
10110
10111 /* Determine whether the evaluation of EXPR references any variables
10112    or functions which aren't otherwise used (and therefore may not be
10113    output).  */
10114 static tree
10115 reference_to_unused (tree * tp, int * walk_subtrees,
10116                      void * data ATTRIBUTE_UNUSED)
10117 {
10118   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10119     *walk_subtrees = 0;
10120   
10121   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10122       && ! TREE_ASM_WRITTEN (*tp))
10123     return *tp;
10124   else if (DECL_P (*tp) && TREE_CODE (*tp) != FUNCTION_DECL)
10125     {
10126       struct varpool_node *node = varpool_node (*tp);
10127       if (!node->needed)
10128         return *tp;
10129     }
10130
10131   return NULL_TREE;
10132 }
10133
10134 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10135    for use in a later add_const_value_attribute call.  */
10136
10137 static rtx
10138 rtl_for_decl_init (tree init, tree type)
10139 {
10140   rtx rtl = NULL_RTX;
10141
10142   /* If a variable is initialized with a string constant without embedded
10143      zeros, build CONST_STRING.  */
10144   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10145     {
10146       tree enttype = TREE_TYPE (type);
10147       tree domain = TYPE_DOMAIN (type);
10148       enum machine_mode mode = TYPE_MODE (enttype);
10149
10150       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10151           && domain
10152           && integer_zerop (TYPE_MIN_VALUE (domain))
10153           && compare_tree_int (TYPE_MAX_VALUE (domain),
10154                                TREE_STRING_LENGTH (init) - 1) == 0
10155           && ((size_t) TREE_STRING_LENGTH (init)
10156               == strlen (TREE_STRING_POINTER (init)) + 1))
10157         rtl = gen_rtx_CONST_STRING (VOIDmode,
10158                                     ggc_strdup (TREE_STRING_POINTER (init)));
10159     }
10160   /* Other aggregates, and complex values, could be represented using
10161      CONCAT: FIXME!  */
10162   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10163     ;
10164   /* Vectors only work if their mode is supported by the target.  
10165      FIXME: generic vectors ought to work too.  */
10166   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10167     ;
10168   /* If the initializer is something that we know will expand into an
10169      immediate RTL constant, expand it now.  We must be careful not to
10170      reference variables which won't be output.  */
10171   else if (initializer_constant_valid_p (init, type)
10172            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10173     {
10174       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10175
10176       /* If expand_expr returns a MEM, it wasn't immediate.  */
10177       gcc_assert (!rtl || !MEM_P (rtl));
10178     }
10179
10180   return rtl;
10181 }
10182
10183 /* Generate RTL for the variable DECL to represent its location.  */
10184
10185 static rtx
10186 rtl_for_decl_location (tree decl)
10187 {
10188   rtx rtl;
10189
10190   /* Here we have to decide where we are going to say the parameter "lives"
10191      (as far as the debugger is concerned).  We only have a couple of
10192      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10193
10194      DECL_RTL normally indicates where the parameter lives during most of the
10195      activation of the function.  If optimization is enabled however, this
10196      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10197      that the parameter doesn't really live anywhere (as far as the code
10198      generation parts of GCC are concerned) during most of the function's
10199      activation.  That will happen (for example) if the parameter is never
10200      referenced within the function.
10201
10202      We could just generate a location descriptor here for all non-NULL
10203      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10204      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10205      where DECL_RTL is NULL or is a pseudo-reg.
10206
10207      Note however that we can only get away with using DECL_INCOMING_RTL as
10208      a backup substitute for DECL_RTL in certain limited cases.  In cases
10209      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10210      we can be sure that the parameter was passed using the same type as it is
10211      declared to have within the function, and that its DECL_INCOMING_RTL
10212      points us to a place where a value of that type is passed.
10213
10214      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10215      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10216      because in these cases DECL_INCOMING_RTL points us to a value of some
10217      type which is *different* from the type of the parameter itself.  Thus,
10218      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10219      such cases, the debugger would end up (for example) trying to fetch a
10220      `float' from a place which actually contains the first part of a
10221      `double'.  That would lead to really incorrect and confusing
10222      output at debug-time.
10223
10224      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10225      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10226      are a couple of exceptions however.  On little-endian machines we can
10227      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10228      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10229      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10230      when (on a little-endian machine) a non-prototyped function has a
10231      parameter declared to be of type `short' or `char'.  In such cases,
10232      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10233      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10234      passed `int' value.  If the debugger then uses that address to fetch
10235      a `short' or a `char' (on a little-endian machine) the result will be
10236      the correct data, so we allow for such exceptional cases below.
10237
10238      Note that our goal here is to describe the place where the given formal
10239      parameter lives during most of the function's activation (i.e. between the
10240      end of the prologue and the start of the epilogue).  We'll do that as best
10241      as we can. Note however that if the given formal parameter is modified
10242      sometime during the execution of the function, then a stack backtrace (at
10243      debug-time) will show the function as having been called with the *new*
10244      value rather than the value which was originally passed in.  This happens
10245      rarely enough that it is not a major problem, but it *is* a problem, and
10246      I'd like to fix it.
10247
10248      A future version of dwarf2out.c may generate two additional attributes for
10249      any given DW_TAG_formal_parameter DIE which will describe the "passed
10250      type" and the "passed location" for the given formal parameter in addition
10251      to the attributes we now generate to indicate the "declared type" and the
10252      "active location" for each parameter.  This additional set of attributes
10253      could be used by debuggers for stack backtraces. Separately, note that
10254      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10255      This happens (for example) for inlined-instances of inline function formal
10256      parameters which are never referenced.  This really shouldn't be
10257      happening.  All PARM_DECL nodes should get valid non-NULL
10258      DECL_INCOMING_RTL values.  FIXME.  */
10259
10260   /* Use DECL_RTL as the "location" unless we find something better.  */
10261   rtl = DECL_RTL_IF_SET (decl);
10262
10263   /* When generating abstract instances, ignore everything except
10264      constants, symbols living in memory, and symbols living in
10265      fixed registers.  */
10266   if (! reload_completed)
10267     {
10268       if (rtl
10269           && (CONSTANT_P (rtl)
10270               || (MEM_P (rtl)
10271                   && CONSTANT_P (XEXP (rtl, 0)))
10272               || (REG_P (rtl)
10273                   && TREE_CODE (decl) == VAR_DECL
10274                   && TREE_STATIC (decl))))
10275         {
10276           rtl = targetm.delegitimize_address (rtl);
10277           return rtl;
10278         }
10279       rtl = NULL_RTX;
10280     }
10281   else if (TREE_CODE (decl) == PARM_DECL)
10282     {
10283       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10284         {
10285           tree declared_type = TREE_TYPE (decl);
10286           tree passed_type = DECL_ARG_TYPE (decl);
10287           enum machine_mode dmode = TYPE_MODE (declared_type);
10288           enum machine_mode pmode = TYPE_MODE (passed_type);
10289
10290           /* This decl represents a formal parameter which was optimized out.
10291              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10292              all cases where (rtl == NULL_RTX) just below.  */
10293           if (dmode == pmode)
10294             rtl = DECL_INCOMING_RTL (decl);
10295           else if (SCALAR_INT_MODE_P (dmode)
10296                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10297                    && DECL_INCOMING_RTL (decl))
10298             {
10299               rtx inc = DECL_INCOMING_RTL (decl);
10300               if (REG_P (inc))
10301                 rtl = inc;
10302               else if (MEM_P (inc))
10303                 {
10304                   if (BYTES_BIG_ENDIAN)
10305                     rtl = adjust_address_nv (inc, dmode,
10306                                              GET_MODE_SIZE (pmode)
10307                                              - GET_MODE_SIZE (dmode));
10308                   else
10309                     rtl = inc;
10310                 }
10311             }
10312         }
10313
10314       /* If the parm was passed in registers, but lives on the stack, then
10315          make a big endian correction if the mode of the type of the
10316          parameter is not the same as the mode of the rtl.  */
10317       /* ??? This is the same series of checks that are made in dbxout.c before
10318          we reach the big endian correction code there.  It isn't clear if all
10319          of these checks are necessary here, but keeping them all is the safe
10320          thing to do.  */
10321       else if (MEM_P (rtl)
10322                && XEXP (rtl, 0) != const0_rtx
10323                && ! CONSTANT_P (XEXP (rtl, 0))
10324                /* Not passed in memory.  */
10325                && !MEM_P (DECL_INCOMING_RTL (decl))
10326                /* Not passed by invisible reference.  */
10327                && (!REG_P (XEXP (rtl, 0))
10328                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10329                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10330 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10331                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10332 #endif
10333                      )
10334                /* Big endian correction check.  */
10335                && BYTES_BIG_ENDIAN
10336                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10337                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10338                    < UNITS_PER_WORD))
10339         {
10340           int offset = (UNITS_PER_WORD
10341                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10342
10343           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10344                              plus_constant (XEXP (rtl, 0), offset));
10345         }
10346     }
10347   else if (TREE_CODE (decl) == VAR_DECL
10348            && rtl
10349            && MEM_P (rtl)
10350            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10351            && BYTES_BIG_ENDIAN)
10352     {
10353       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10354       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10355
10356       /* If a variable is declared "register" yet is smaller than
10357          a register, then if we store the variable to memory, it
10358          looks like we're storing a register-sized value, when in
10359          fact we are not.  We need to adjust the offset of the
10360          storage location to reflect the actual value's bytes,
10361          else gdb will not be able to display it.  */
10362       if (rsize > dsize)
10363         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10364                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10365     }
10366
10367   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10368      and will have been substituted directly into all expressions that use it.
10369      C does not have such a concept, but C++ and other languages do.  */
10370   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10371     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10372
10373   if (rtl)
10374     rtl = targetm.delegitimize_address (rtl);
10375
10376   /* If we don't look past the constant pool, we risk emitting a
10377      reference to a constant pool entry that isn't referenced from
10378      code, and thus is not emitted.  */
10379   if (rtl)
10380     rtl = avoid_constant_pool_reference (rtl);
10381
10382   return rtl;
10383 }
10384
10385 /* We need to figure out what section we should use as the base for the
10386    address ranges where a given location is valid.
10387    1. If this particular DECL has a section associated with it, use that.
10388    2. If this function has a section associated with it, use that.
10389    3. Otherwise, use the text section.
10390    XXX: If you split a variable across multiple sections, we won't notice.  */
10391
10392 static const char *
10393 secname_for_decl (tree decl)
10394 {
10395   const char *secname;
10396
10397   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10398     {
10399       tree sectree = DECL_SECTION_NAME (decl);
10400       secname = TREE_STRING_POINTER (sectree);
10401     }
10402   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10403     {
10404       tree sectree = DECL_SECTION_NAME (current_function_decl);
10405       secname = TREE_STRING_POINTER (sectree);
10406     }
10407   else if (cfun && in_cold_section_p)
10408     secname = cfun->cold_section_label;
10409   else
10410     secname = text_section_label;
10411
10412   return secname;
10413 }
10414
10415 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10416    data attribute for a variable or a parameter.  We generate the
10417    DW_AT_const_value attribute only in those cases where the given variable
10418    or parameter does not have a true "location" either in memory or in a
10419    register.  This can happen (for example) when a constant is passed as an
10420    actual argument in a call to an inline function.  (It's possible that
10421    these things can crop up in other ways also.)  Note that one type of
10422    constant value which can be passed into an inlined function is a constant
10423    pointer.  This can happen for example if an actual argument in an inlined
10424    function call evaluates to a compile-time constant address.  */
10425
10426 static void
10427 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10428                                        enum dwarf_attribute attr)
10429 {
10430   rtx rtl;
10431   dw_loc_descr_ref descr;
10432   var_loc_list *loc_list;
10433   struct var_loc_node *node;
10434   if (TREE_CODE (decl) == ERROR_MARK)
10435     return;
10436
10437   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10438               || TREE_CODE (decl) == RESULT_DECL);
10439              
10440   /* See if we possibly have multiple locations for this variable.  */
10441   loc_list = lookup_decl_loc (decl);
10442
10443   /* If it truly has multiple locations, the first and last node will
10444      differ.  */
10445   if (loc_list && loc_list->first != loc_list->last)
10446     {
10447       const char *endname, *secname;
10448       dw_loc_list_ref list;
10449       rtx varloc;
10450
10451       /* Now that we know what section we are using for a base,
10452          actually construct the list of locations.
10453          The first location information is what is passed to the
10454          function that creates the location list, and the remaining
10455          locations just get added on to that list.
10456          Note that we only know the start address for a location
10457          (IE location changes), so to build the range, we use
10458          the range [current location start, next location start].
10459          This means we have to special case the last node, and generate
10460          a range of [last location start, end of function label].  */
10461
10462       node = loc_list->first;
10463       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10464       secname = secname_for_decl (decl);
10465
10466       list = new_loc_list (loc_descriptor (varloc),
10467                            node->label, node->next->label, secname, 1);
10468       node = node->next;
10469
10470       for (; node->next; node = node->next)
10471         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10472           {
10473             /* The variable has a location between NODE->LABEL and
10474                NODE->NEXT->LABEL.  */
10475             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10476             add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10477                                        node->label, node->next->label, secname);
10478           }
10479
10480       /* If the variable has a location at the last label
10481          it keeps its location until the end of function.  */
10482       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10483         {
10484           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10485
10486           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10487           if (!current_function_decl)
10488             endname = text_end_label;
10489           else
10490             {
10491               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10492                                            current_function_funcdef_no);
10493               endname = ggc_strdup (label_id);
10494             }
10495           add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10496                                      node->label, endname, secname);
10497         }
10498
10499       /* Finally, add the location list to the DIE, and we are done.  */
10500       add_AT_loc_list (die, attr, list);
10501       return;
10502     }
10503
10504   /* Try to get some constant RTL for this decl, and use that as the value of
10505      the location.  */
10506   
10507   rtl = rtl_for_decl_location (decl);
10508   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10509     {
10510       add_const_value_attribute (die, rtl);
10511       return;
10512     }
10513   
10514   /* If we have tried to generate the location otherwise, and it
10515      didn't work out (we wouldn't be here if we did), and we have a one entry
10516      location list, try generating a location from that.  */
10517   if (loc_list && loc_list->first)
10518     {
10519       node = loc_list->first;
10520       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10521       if (descr)
10522         {
10523           add_AT_location_description (die, attr, descr);
10524           return;
10525         }
10526     }
10527
10528   /* We couldn't get any rtl, so try directly generating the location
10529      description from the tree.  */
10530   descr = loc_descriptor_from_tree (decl);
10531   if (descr)
10532     {
10533       add_AT_location_description (die, attr, descr);
10534       return;
10535     }
10536   /* None of that worked, so it must not really have a location;
10537      try adding a constant value attribute from the DECL_INITIAL.  */
10538   tree_add_const_value_attribute (die, decl);
10539 }
10540
10541 /* If we don't have a copy of this variable in memory for some reason (such
10542    as a C++ member constant that doesn't have an out-of-line definition),
10543    we should tell the debugger about the constant value.  */
10544
10545 static void
10546 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10547 {
10548   tree init = DECL_INITIAL (decl);
10549   tree type = TREE_TYPE (decl);
10550   rtx rtl;
10551
10552   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10553     /* OK */;
10554   else
10555     return;
10556
10557   rtl = rtl_for_decl_init (init, type);
10558   if (rtl)
10559     add_const_value_attribute (var_die, rtl);
10560 }
10561
10562 /* Convert the CFI instructions for the current function into a
10563    location list.  This is used for DW_AT_frame_base when we targeting
10564    a dwarf2 consumer that does not support the dwarf3
10565    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10566    expressions.  */
10567
10568 static dw_loc_list_ref
10569 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10570 {
10571   dw_fde_ref fde;
10572   dw_loc_list_ref list, *list_tail;
10573   dw_cfi_ref cfi;
10574   dw_cfa_location last_cfa, next_cfa;
10575   const char *start_label, *last_label, *section;
10576
10577   fde = &fde_table[fde_table_in_use - 1];
10578
10579   section = secname_for_decl (current_function_decl);
10580   list_tail = &list;
10581   list = NULL;
10582
10583   next_cfa.reg = INVALID_REGNUM;
10584   next_cfa.offset = 0;
10585   next_cfa.indirect = 0;
10586   next_cfa.base_offset = 0;
10587
10588   start_label = fde->dw_fde_begin;
10589
10590   /* ??? Bald assumption that the CIE opcode list does not contain
10591      advance opcodes.  */
10592   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10593     lookup_cfa_1 (cfi, &next_cfa);
10594
10595   last_cfa = next_cfa;
10596   last_label = start_label;
10597
10598   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10599     switch (cfi->dw_cfi_opc)
10600       {
10601       case DW_CFA_set_loc:
10602       case DW_CFA_advance_loc1:
10603       case DW_CFA_advance_loc2:
10604       case DW_CFA_advance_loc4:
10605         if (!cfa_equal_p (&last_cfa, &next_cfa))
10606           {
10607             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10608                                        start_label, last_label, section,
10609                                        list == NULL);
10610
10611             list_tail = &(*list_tail)->dw_loc_next;
10612             last_cfa = next_cfa;
10613             start_label = last_label;
10614           }
10615         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10616         break;
10617
10618       case DW_CFA_advance_loc:
10619         /* The encoding is complex enough that we should never emit this.  */
10620       case DW_CFA_remember_state:
10621       case DW_CFA_restore_state:
10622         /* We don't handle these two in this function.  It would be possible
10623            if it were to be required.  */
10624         gcc_unreachable ();
10625
10626       default:
10627         lookup_cfa_1 (cfi, &next_cfa);
10628         break;
10629       }
10630
10631   if (!cfa_equal_p (&last_cfa, &next_cfa))
10632     {
10633       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10634                                  start_label, last_label, section,
10635                                  list == NULL);
10636       list_tail = &(*list_tail)->dw_loc_next;
10637       start_label = last_label;
10638     }
10639   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10640                              start_label, fde->dw_fde_end, section,
10641                              list == NULL);
10642
10643   return list;
10644 }
10645
10646 /* Compute a displacement from the "steady-state frame pointer" to the
10647    frame base (often the same as the CFA), and store it in
10648    frame_pointer_fb_offset.  OFFSET is added to the displacement
10649    before the latter is negated.  */
10650
10651 static void
10652 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10653 {
10654   rtx reg, elim;
10655
10656 #ifdef FRAME_POINTER_CFA_OFFSET
10657   reg = frame_pointer_rtx;
10658   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10659 #else
10660   reg = arg_pointer_rtx;
10661   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10662 #endif
10663
10664   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10665   if (GET_CODE (elim) == PLUS)
10666     {
10667       offset += INTVAL (XEXP (elim, 1));
10668       elim = XEXP (elim, 0);
10669     }
10670   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10671                        : stack_pointer_rtx));
10672
10673   frame_pointer_fb_offset = -offset;
10674 }
10675
10676 /* Generate a DW_AT_name attribute given some string value to be included as
10677    the value of the attribute.  */
10678
10679 static void
10680 add_name_attribute (dw_die_ref die, const char *name_string)
10681 {
10682   if (name_string != NULL && *name_string != 0)
10683     {
10684       if (demangle_name_func)
10685         name_string = (*demangle_name_func) (name_string);
10686
10687       add_AT_string (die, DW_AT_name, name_string);
10688     }
10689 }
10690
10691 /* Generate a DW_AT_comp_dir attribute for DIE.  */
10692
10693 static void
10694 add_comp_dir_attribute (dw_die_ref die)
10695 {
10696   const char *wd = get_src_pwd ();
10697   if (wd != NULL)
10698     add_AT_string (die, DW_AT_comp_dir, wd);
10699 }
10700
10701 /* Given a tree node describing an array bound (either lower or upper) output
10702    a representation for that bound.  */
10703
10704 static void
10705 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10706 {
10707   switch (TREE_CODE (bound))
10708     {
10709     case ERROR_MARK:
10710       return;
10711
10712     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
10713     case INTEGER_CST:
10714       if (! host_integerp (bound, 0)
10715           || (bound_attr == DW_AT_lower_bound
10716               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
10717                   || (is_fortran () && integer_onep (bound)))))
10718         /* Use the default.  */
10719         ;
10720       else
10721         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10722       break;
10723
10724     case CONVERT_EXPR:
10725     case NOP_EXPR:
10726     case NON_LVALUE_EXPR:
10727     case VIEW_CONVERT_EXPR:
10728       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10729       break;
10730
10731     case SAVE_EXPR:
10732       break;
10733
10734     case VAR_DECL:
10735     case PARM_DECL:
10736     case RESULT_DECL:
10737       {
10738         dw_die_ref decl_die = lookup_decl_die (bound);
10739
10740         /* ??? Can this happen, or should the variable have been bound
10741            first?  Probably it can, since I imagine that we try to create
10742            the types of parameters in the order in which they exist in
10743            the list, and won't have created a forward reference to a
10744            later parameter.  */
10745         if (decl_die != NULL)
10746           add_AT_die_ref (subrange_die, bound_attr, decl_die);
10747         break;
10748       }
10749
10750     default:
10751       {
10752         /* Otherwise try to create a stack operation procedure to
10753            evaluate the value of the array bound.  */
10754
10755         dw_die_ref ctx, decl_die;
10756         dw_loc_descr_ref loc;
10757
10758         loc = loc_descriptor_from_tree (bound);
10759         if (loc == NULL)
10760           break;
10761
10762         if (current_function_decl == 0)
10763           ctx = comp_unit_die;
10764         else
10765           ctx = lookup_decl_die (current_function_decl);
10766
10767         decl_die = new_die (DW_TAG_variable, ctx, bound);
10768         add_AT_flag (decl_die, DW_AT_artificial, 1);
10769         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10770         add_AT_loc (decl_die, DW_AT_location, loc);
10771
10772         add_AT_die_ref (subrange_die, bound_attr, decl_die);
10773         break;
10774       }
10775     }
10776 }
10777
10778 /* Note that the block of subscript information for an array type also
10779    includes information about the element type of type given array type.  */
10780
10781 static void
10782 add_subscript_info (dw_die_ref type_die, tree type)
10783 {
10784 #ifndef MIPS_DEBUGGING_INFO
10785   unsigned dimension_number;
10786 #endif
10787   tree lower, upper;
10788   dw_die_ref subrange_die;
10789
10790   /* The GNU compilers represent multidimensional array types as sequences of
10791      one dimensional array types whose element types are themselves array
10792      types.  Here we squish that down, so that each multidimensional array
10793      type gets only one array_type DIE in the Dwarf debugging info. The draft
10794      Dwarf specification say that we are allowed to do this kind of
10795      compression in C (because there is no difference between an array or
10796      arrays and a multidimensional array in C) but for other source languages
10797      (e.g. Ada) we probably shouldn't do this.  */
10798
10799   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10800      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
10801      We work around this by disabling this feature.  See also
10802      gen_array_type_die.  */
10803 #ifndef MIPS_DEBUGGING_INFO
10804   for (dimension_number = 0;
10805        TREE_CODE (type) == ARRAY_TYPE;
10806        type = TREE_TYPE (type), dimension_number++)
10807 #endif
10808     {
10809       tree domain = TYPE_DOMAIN (type);
10810
10811       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10812          and (in GNU C only) variable bounds.  Handle all three forms
10813          here.  */
10814       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10815       if (domain)
10816         {
10817           /* We have an array type with specified bounds.  */
10818           lower = TYPE_MIN_VALUE (domain);
10819           upper = TYPE_MAX_VALUE (domain);
10820
10821           /* Define the index type.  */
10822           if (TREE_TYPE (domain))
10823             {
10824               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
10825                  TREE_TYPE field.  We can't emit debug info for this
10826                  because it is an unnamed integral type.  */
10827               if (TREE_CODE (domain) == INTEGER_TYPE
10828                   && TYPE_NAME (domain) == NULL_TREE
10829                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10830                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10831                 ;
10832               else
10833                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10834                                     type_die);
10835             }
10836
10837           /* ??? If upper is NULL, the array has unspecified length,
10838              but it does have a lower bound.  This happens with Fortran
10839                dimension arr(N:*)
10840              Since the debugger is definitely going to need to know N
10841              to produce useful results, go ahead and output the lower
10842              bound solo, and hope the debugger can cope.  */
10843
10844           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10845           if (upper)
10846             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10847         }
10848
10849       /* Otherwise we have an array type with an unspecified length.  The
10850          DWARF-2 spec does not say how to handle this; let's just leave out the
10851          bounds.  */
10852     }
10853 }
10854
10855 static void
10856 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10857 {
10858   unsigned size;
10859
10860   switch (TREE_CODE (tree_node))
10861     {
10862     case ERROR_MARK:
10863       size = 0;
10864       break;
10865     case ENUMERAL_TYPE:
10866     case RECORD_TYPE:
10867     case UNION_TYPE:
10868     case QUAL_UNION_TYPE:
10869       size = int_size_in_bytes (tree_node);
10870       break;
10871     case FIELD_DECL:
10872       /* For a data member of a struct or union, the DW_AT_byte_size is
10873          generally given as the number of bytes normally allocated for an
10874          object of the *declared* type of the member itself.  This is true
10875          even for bit-fields.  */
10876       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10877       break;
10878     default:
10879       gcc_unreachable ();
10880     }
10881
10882   /* Note that `size' might be -1 when we get to this point.  If it is, that
10883      indicates that the byte size of the entity in question is variable.  We
10884      have no good way of expressing this fact in Dwarf at the present time,
10885      so just let the -1 pass on through.  */
10886   add_AT_unsigned (die, DW_AT_byte_size, size);
10887 }
10888
10889 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10890    which specifies the distance in bits from the highest order bit of the
10891    "containing object" for the bit-field to the highest order bit of the
10892    bit-field itself.
10893
10894    For any given bit-field, the "containing object" is a hypothetical object
10895    (of some integral or enum type) within which the given bit-field lives.  The
10896    type of this hypothetical "containing object" is always the same as the
10897    declared type of the individual bit-field itself.  The determination of the
10898    exact location of the "containing object" for a bit-field is rather
10899    complicated.  It's handled by the `field_byte_offset' function (above).
10900
10901    Note that it is the size (in bytes) of the hypothetical "containing object"
10902    which will be given in the DW_AT_byte_size attribute for this bit-field.
10903    (See `byte_size_attribute' above).  */
10904
10905 static inline void
10906 add_bit_offset_attribute (dw_die_ref die, tree decl)
10907 {
10908   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10909   tree type = DECL_BIT_FIELD_TYPE (decl);
10910   HOST_WIDE_INT bitpos_int;
10911   HOST_WIDE_INT highest_order_object_bit_offset;
10912   HOST_WIDE_INT highest_order_field_bit_offset;
10913   HOST_WIDE_INT unsigned bit_offset;
10914
10915   /* Must be a field and a bit field.  */
10916   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10917
10918   /* We can't yet handle bit-fields whose offsets are variable, so if we
10919      encounter such things, just return without generating any attribute
10920      whatsoever.  Likewise for variable or too large size.  */
10921   if (! host_integerp (bit_position (decl), 0)
10922       || ! host_integerp (DECL_SIZE (decl), 1))
10923     return;
10924
10925   bitpos_int = int_bit_position (decl);
10926
10927   /* Note that the bit offset is always the distance (in bits) from the
10928      highest-order bit of the "containing object" to the highest-order bit of
10929      the bit-field itself.  Since the "high-order end" of any object or field
10930      is different on big-endian and little-endian machines, the computation
10931      below must take account of these differences.  */
10932   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10933   highest_order_field_bit_offset = bitpos_int;
10934
10935   if (! BYTES_BIG_ENDIAN)
10936     {
10937       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10938       highest_order_object_bit_offset += simple_type_size_in_bits (type);
10939     }
10940
10941   bit_offset
10942     = (! BYTES_BIG_ENDIAN
10943        ? highest_order_object_bit_offset - highest_order_field_bit_offset
10944        : highest_order_field_bit_offset - highest_order_object_bit_offset);
10945
10946   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10947 }
10948
10949 /* For a FIELD_DECL node which represents a bit field, output an attribute
10950    which specifies the length in bits of the given field.  */
10951
10952 static inline void
10953 add_bit_size_attribute (dw_die_ref die, tree decl)
10954 {
10955   /* Must be a field and a bit field.  */
10956   gcc_assert (TREE_CODE (decl) == FIELD_DECL
10957               && DECL_BIT_FIELD_TYPE (decl));
10958
10959   if (host_integerp (DECL_SIZE (decl), 1))
10960     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10961 }
10962
10963 /* If the compiled language is ANSI C, then add a 'prototyped'
10964    attribute, if arg types are given for the parameters of a function.  */
10965
10966 static inline void
10967 add_prototyped_attribute (dw_die_ref die, tree func_type)
10968 {
10969   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10970       && TYPE_ARG_TYPES (func_type) != NULL)
10971     add_AT_flag (die, DW_AT_prototyped, 1);
10972 }
10973
10974 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
10975    by looking in either the type declaration or object declaration
10976    equate table.  */
10977
10978 static inline void
10979 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10980 {
10981   dw_die_ref origin_die = NULL;
10982
10983   if (TREE_CODE (origin) != FUNCTION_DECL)
10984     {
10985       /* We may have gotten separated from the block for the inlined
10986          function, if we're in an exception handler or some such; make
10987          sure that the abstract function has been written out.
10988
10989          Doing this for nested functions is wrong, however; functions are
10990          distinct units, and our context might not even be inline.  */
10991       tree fn = origin;
10992
10993       if (TYPE_P (fn))
10994         fn = TYPE_STUB_DECL (fn);
10995       
10996       fn = decl_function_context (fn);
10997       if (fn)
10998         dwarf2out_abstract_function (fn);
10999     }
11000
11001   if (DECL_P (origin))
11002     origin_die = lookup_decl_die (origin);
11003   else if (TYPE_P (origin))
11004     origin_die = lookup_type_die (origin);
11005
11006   /* XXX: Functions that are never lowered don't always have correct block
11007      trees (in the case of java, they simply have no block tree, in some other
11008      languages).  For these functions, there is nothing we can really do to
11009      output correct debug info for inlined functions in all cases.  Rather
11010      than die, we'll just produce deficient debug info now, in that we will
11011      have variables without a proper abstract origin.  In the future, when all
11012      functions are lowered, we should re-add a gcc_assert (origin_die)
11013      here.  */
11014
11015   if (origin_die)
11016       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11017 }
11018
11019 /* We do not currently support the pure_virtual attribute.  */
11020
11021 static inline void
11022 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11023 {
11024   if (DECL_VINDEX (func_decl))
11025     {
11026       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11027
11028       if (host_integerp (DECL_VINDEX (func_decl), 0))
11029         add_AT_loc (die, DW_AT_vtable_elem_location,
11030                     new_loc_descr (DW_OP_constu,
11031                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11032                                    0));
11033
11034       /* GNU extension: Record what type this method came from originally.  */
11035       if (debug_info_level > DINFO_LEVEL_TERSE)
11036         add_AT_die_ref (die, DW_AT_containing_type,
11037                         lookup_type_die (DECL_CONTEXT (func_decl)));
11038     }
11039 }
11040 \f
11041 /* Add source coordinate attributes for the given decl.  */
11042
11043 static void
11044 add_src_coords_attributes (dw_die_ref die, tree decl)
11045 {
11046   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11047
11048   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11049   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11050 }
11051
11052 /* Add a DW_AT_name attribute and source coordinate attribute for the
11053    given decl, but only if it actually has a name.  */
11054
11055 static void
11056 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11057 {
11058   tree decl_name;
11059
11060   decl_name = DECL_NAME (decl);
11061   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11062     {
11063       add_name_attribute (die, dwarf2_name (decl, 0));
11064       if (! DECL_ARTIFICIAL (decl))
11065         add_src_coords_attributes (die, decl);
11066
11067       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11068           && TREE_PUBLIC (decl)
11069           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11070           && !DECL_ABSTRACT (decl)
11071           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
11072         add_AT_string (die, DW_AT_MIPS_linkage_name,
11073                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11074     }
11075
11076 #ifdef VMS_DEBUGGING_INFO
11077   /* Get the function's name, as described by its RTL.  This may be different
11078      from the DECL_NAME name used in the source file.  */
11079   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11080     {
11081       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11082                    XEXP (DECL_RTL (decl), 0));
11083       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11084     }
11085 #endif
11086 }
11087
11088 /* Push a new declaration scope.  */
11089
11090 static void
11091 push_decl_scope (tree scope)
11092 {
11093   VEC_safe_push (tree, gc, decl_scope_table, scope);
11094 }
11095
11096 /* Pop a declaration scope.  */
11097
11098 static inline void
11099 pop_decl_scope (void)
11100 {
11101   VEC_pop (tree, decl_scope_table);
11102 }
11103
11104 /* Return the DIE for the scope that immediately contains this type.
11105    Non-named types get global scope.  Named types nested in other
11106    types get their containing scope if it's open, or global scope
11107    otherwise.  All other types (i.e. function-local named types) get
11108    the current active scope.  */
11109
11110 static dw_die_ref
11111 scope_die_for (tree t, dw_die_ref context_die)
11112 {
11113   dw_die_ref scope_die = NULL;
11114   tree containing_scope;
11115   int i;
11116
11117   /* Non-types always go in the current scope.  */
11118   gcc_assert (TYPE_P (t));
11119
11120   containing_scope = TYPE_CONTEXT (t);
11121
11122   /* Use the containing namespace if it was passed in (for a declaration).  */
11123   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11124     {
11125       if (context_die == lookup_decl_die (containing_scope))
11126         /* OK */;
11127       else
11128         containing_scope = NULL_TREE;
11129     }
11130
11131   /* Ignore function type "scopes" from the C frontend.  They mean that
11132      a tagged type is local to a parmlist of a function declarator, but
11133      that isn't useful to DWARF.  */
11134   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11135     containing_scope = NULL_TREE;
11136
11137   if (containing_scope == NULL_TREE)
11138     scope_die = comp_unit_die;
11139   else if (TYPE_P (containing_scope))
11140     {
11141       /* For types, we can just look up the appropriate DIE.  But
11142          first we check to see if we're in the middle of emitting it
11143          so we know where the new DIE should go.  */
11144       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11145         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11146           break;
11147
11148       if (i < 0)
11149         {
11150           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11151                       || TREE_ASM_WRITTEN (containing_scope));
11152
11153           /* If none of the current dies are suitable, we get file scope.  */
11154           scope_die = comp_unit_die;
11155         }
11156       else
11157         scope_die = lookup_type_die (containing_scope);
11158     }
11159   else
11160     scope_die = context_die;
11161
11162   return scope_die;
11163 }
11164
11165 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11166
11167 static inline int
11168 local_scope_p (dw_die_ref context_die)
11169 {
11170   for (; context_die; context_die = context_die->die_parent)
11171     if (context_die->die_tag == DW_TAG_inlined_subroutine
11172         || context_die->die_tag == DW_TAG_subprogram)
11173       return 1;
11174
11175   return 0;
11176 }
11177
11178 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11179    whether or not to treat a DIE in this context as a declaration.  */
11180
11181 static inline int
11182 class_or_namespace_scope_p (dw_die_ref context_die)
11183 {
11184   return (context_die
11185           && (context_die->die_tag == DW_TAG_structure_type
11186               || context_die->die_tag == DW_TAG_union_type
11187               || context_die->die_tag == DW_TAG_namespace));
11188 }
11189
11190 /* Many forms of DIEs require a "type description" attribute.  This
11191    routine locates the proper "type descriptor" die for the type given
11192    by 'type', and adds a DW_AT_type attribute below the given die.  */
11193
11194 static void
11195 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11196                     int decl_volatile, dw_die_ref context_die)
11197 {
11198   enum tree_code code  = TREE_CODE (type);
11199   dw_die_ref type_die  = NULL;
11200
11201   /* ??? If this type is an unnamed subrange type of an integral or
11202      floating-point type, use the inner type.  This is because we have no
11203      support for unnamed types in base_type_die.  This can happen if this is
11204      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11205   if ((code == INTEGER_TYPE || code == REAL_TYPE)
11206       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11207     type = TREE_TYPE (type), code = TREE_CODE (type);
11208
11209   if (code == ERROR_MARK
11210       /* Handle a special case.  For functions whose return type is void, we
11211          generate *no* type attribute.  (Note that no object may have type
11212          `void', so this only applies to function return types).  */
11213       || code == VOID_TYPE)
11214     return;
11215
11216   type_die = modified_type_die (type,
11217                                 decl_const || TYPE_READONLY (type),
11218                                 decl_volatile || TYPE_VOLATILE (type),
11219                                 context_die);
11220
11221   if (type_die != NULL)
11222     add_AT_die_ref (object_die, DW_AT_type, type_die);
11223 }
11224
11225 /* Given an object die, add the calling convention attribute for the
11226    function call type.  */
11227 static void
11228 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11229 {
11230   enum dwarf_calling_convention value = DW_CC_normal;
11231
11232   value = targetm.dwarf_calling_convention (type);
11233
11234   /* Only add the attribute if the backend requests it, and
11235      is not DW_CC_normal.  */
11236   if (value && (value != DW_CC_normal))
11237     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11238 }
11239
11240 /* Given a tree pointer to a struct, class, union, or enum type node, return
11241    a pointer to the (string) tag name for the given type, or zero if the type
11242    was declared without a tag.  */
11243
11244 static const char *
11245 type_tag (tree type)
11246 {
11247   const char *name = 0;
11248
11249   if (TYPE_NAME (type) != 0)
11250     {
11251       tree t = 0;
11252
11253       /* Find the IDENTIFIER_NODE for the type name.  */
11254       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11255         t = TYPE_NAME (type);
11256
11257       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11258          a TYPE_DECL node, regardless of whether or not a `typedef' was
11259          involved.  */
11260       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11261                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11262         {
11263           /* We want to be extra verbose.  Don't call dwarf_name if
11264              DECL_NAME isn't set.  The default hook for decl_printable_name
11265              doesn't like that, and in this context it's correct to return
11266              0, instead of "<anonymous>" or the like.  */
11267           if (DECL_NAME (TYPE_NAME (type)))
11268             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11269         }
11270
11271       /* Now get the name as a string, or invent one.  */
11272       if (!name && t != 0)
11273         name = IDENTIFIER_POINTER (t);
11274     }
11275
11276   return (name == 0 || *name == '\0') ? 0 : name;
11277 }
11278
11279 /* Return the type associated with a data member, make a special check
11280    for bit field types.  */
11281
11282 static inline tree
11283 member_declared_type (tree member)
11284 {
11285   return (DECL_BIT_FIELD_TYPE (member)
11286           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11287 }
11288
11289 /* Get the decl's label, as described by its RTL. This may be different
11290    from the DECL_NAME name used in the source file.  */
11291
11292 #if 0
11293 static const char *
11294 decl_start_label (tree decl)
11295 {
11296   rtx x;
11297   const char *fnname;
11298
11299   x = DECL_RTL (decl);
11300   gcc_assert (MEM_P (x));
11301
11302   x = XEXP (x, 0);
11303   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11304
11305   fnname = XSTR (x, 0);
11306   return fnname;
11307 }
11308 #endif
11309 \f
11310 /* These routines generate the internal representation of the DIE's for
11311    the compilation unit.  Debugging information is collected by walking
11312    the declaration trees passed in from dwarf2out_decl().  */
11313
11314 static void
11315 gen_array_type_die (tree type, dw_die_ref context_die)
11316 {
11317   dw_die_ref scope_die = scope_die_for (type, context_die);
11318   dw_die_ref array_die;
11319   tree element_type;
11320
11321   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11322      the inner array type comes before the outer array type.  Thus we must
11323      call gen_type_die before we call new_die.  See below also.  */
11324 #ifdef MIPS_DEBUGGING_INFO
11325   gen_type_die (TREE_TYPE (type), context_die);
11326 #endif
11327
11328   array_die = new_die (DW_TAG_array_type, scope_die, type);
11329   add_name_attribute (array_die, type_tag (type));
11330   equate_type_number_to_die (type, array_die);
11331
11332   if (TREE_CODE (type) == VECTOR_TYPE)
11333     {
11334       /* The frontend feeds us a representation for the vector as a struct
11335          containing an array.  Pull out the array type.  */
11336       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11337       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11338     }
11339
11340 #if 0
11341   /* We default the array ordering.  SDB will probably do
11342      the right things even if DW_AT_ordering is not present.  It's not even
11343      an issue until we start to get into multidimensional arrays anyway.  If
11344      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11345      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11346      and when we find out that we need to put these in, we will only do so
11347      for multidimensional arrays.  */
11348   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11349 #endif
11350
11351 #ifdef MIPS_DEBUGGING_INFO
11352   /* The SGI compilers handle arrays of unknown bound by setting
11353      AT_declaration and not emitting any subrange DIEs.  */
11354   if (! TYPE_DOMAIN (type))
11355     add_AT_flag (array_die, DW_AT_declaration, 1);
11356   else
11357 #endif
11358     add_subscript_info (array_die, type);
11359
11360   /* Add representation of the type of the elements of this array type.  */
11361   element_type = TREE_TYPE (type);
11362
11363   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11364      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11365      We work around this by disabling this feature.  See also
11366      add_subscript_info.  */
11367 #ifndef MIPS_DEBUGGING_INFO
11368   while (TREE_CODE (element_type) == ARRAY_TYPE)
11369     element_type = TREE_TYPE (element_type);
11370
11371   gen_type_die (element_type, context_die);
11372 #endif
11373
11374   add_type_attribute (array_die, element_type, 0, 0, context_die);
11375
11376   if (get_AT (array_die, DW_AT_name))
11377     add_pubtype (type, array_die);
11378 }
11379
11380 #if 0
11381 static void
11382 gen_entry_point_die (tree decl, dw_die_ref context_die)
11383 {
11384   tree origin = decl_ultimate_origin (decl);
11385   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11386
11387   if (origin != NULL)
11388     add_abstract_origin_attribute (decl_die, origin);
11389   else
11390     {
11391       add_name_and_src_coords_attributes (decl_die, decl);
11392       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11393                           0, 0, context_die);
11394     }
11395
11396   if (DECL_ABSTRACT (decl))
11397     equate_decl_number_to_die (decl, decl_die);
11398   else
11399     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11400 }
11401 #endif
11402
11403 /* Walk through the list of incomplete types again, trying once more to
11404    emit full debugging info for them.  */
11405
11406 static void
11407 retry_incomplete_types (void)
11408 {
11409   int i;
11410
11411   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11412     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11413 }
11414
11415 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11416
11417 static void
11418 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11419 {
11420   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11421
11422   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11423      be incomplete and such types are not marked.  */
11424   add_abstract_origin_attribute (type_die, type);
11425 }
11426
11427 /* Generate a DIE to represent an inlined instance of a structure type.  */
11428
11429 static void
11430 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11431 {
11432   dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11433
11434   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11435      be incomplete and such types are not marked.  */
11436   add_abstract_origin_attribute (type_die, type);
11437 }
11438
11439 /* Generate a DIE to represent an inlined instance of a union type.  */
11440
11441 static void
11442 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11443 {
11444   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11445
11446   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11447      be incomplete and such types are not marked.  */
11448   add_abstract_origin_attribute (type_die, type);
11449 }
11450
11451 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11452    include all of the information about the enumeration values also. Each
11453    enumerated type name/value is listed as a child of the enumerated type
11454    DIE.  */
11455
11456 static dw_die_ref
11457 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11458 {
11459   dw_die_ref type_die = lookup_type_die (type);
11460
11461   if (type_die == NULL)
11462     {
11463       type_die = new_die (DW_TAG_enumeration_type,
11464                           scope_die_for (type, context_die), type);
11465       equate_type_number_to_die (type, type_die);
11466       add_name_attribute (type_die, type_tag (type));
11467     }
11468   else if (! TYPE_SIZE (type))
11469     return type_die;
11470   else
11471     remove_AT (type_die, DW_AT_declaration);
11472
11473   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11474      given enum type is incomplete, do not generate the DW_AT_byte_size
11475      attribute or the DW_AT_element_list attribute.  */
11476   if (TYPE_SIZE (type))
11477     {
11478       tree link;
11479
11480       TREE_ASM_WRITTEN (type) = 1;
11481       add_byte_size_attribute (type_die, type);
11482       if (TYPE_STUB_DECL (type) != NULL_TREE)
11483         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11484
11485       /* If the first reference to this type was as the return type of an
11486          inline function, then it may not have a parent.  Fix this now.  */
11487       if (type_die->die_parent == NULL)
11488         add_child_die (scope_die_for (type, context_die), type_die);
11489
11490       for (link = TYPE_VALUES (type);
11491            link != NULL; link = TREE_CHAIN (link))
11492         {
11493           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11494           tree value = TREE_VALUE (link);
11495
11496           add_name_attribute (enum_die,
11497                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11498
11499           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11500             /* DWARF2 does not provide a way of indicating whether or
11501                not enumeration constants are signed or unsigned.  GDB
11502                always assumes the values are signed, so we output all
11503                values as if they were signed.  That means that
11504                enumeration constants with very large unsigned values
11505                will appear to have negative values in the debugger.  */
11506             add_AT_int (enum_die, DW_AT_const_value,
11507                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11508         }
11509     }
11510   else
11511     add_AT_flag (type_die, DW_AT_declaration, 1);
11512
11513   if (get_AT (type_die, DW_AT_name))
11514     add_pubtype (type, type_die);
11515
11516   return type_die;
11517 }
11518
11519 /* Generate a DIE to represent either a real live formal parameter decl or to
11520    represent just the type of some formal parameter position in some function
11521    type.
11522
11523    Note that this routine is a bit unusual because its argument may be a
11524    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11525    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11526    node.  If it's the former then this function is being called to output a
11527    DIE to represent a formal parameter object (or some inlining thereof).  If
11528    it's the latter, then this function is only being called to output a
11529    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11530    argument type of some subprogram type.  */
11531
11532 static dw_die_ref
11533 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11534 {
11535   dw_die_ref parm_die
11536     = new_die (DW_TAG_formal_parameter, context_die, node);
11537   tree origin;
11538
11539   switch (TREE_CODE_CLASS (TREE_CODE (node)))
11540     {
11541     case tcc_declaration:
11542       origin = decl_ultimate_origin (node);
11543       if (origin != NULL)
11544         add_abstract_origin_attribute (parm_die, origin);
11545       else
11546         {
11547           add_name_and_src_coords_attributes (parm_die, node);
11548           add_type_attribute (parm_die, TREE_TYPE (node),
11549                               TREE_READONLY (node),
11550                               TREE_THIS_VOLATILE (node),
11551                               context_die);
11552           if (DECL_ARTIFICIAL (node))
11553             add_AT_flag (parm_die, DW_AT_artificial, 1);
11554         }
11555
11556       equate_decl_number_to_die (node, parm_die);
11557       if (! DECL_ABSTRACT (node))
11558         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11559
11560       break;
11561
11562     case tcc_type:
11563       /* We were called with some kind of a ..._TYPE node.  */
11564       add_type_attribute (parm_die, node, 0, 0, context_die);
11565       break;
11566
11567     default:
11568       gcc_unreachable ();
11569     }
11570
11571   return parm_die;
11572 }
11573
11574 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11575    at the end of an (ANSI prototyped) formal parameters list.  */
11576
11577 static void
11578 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11579 {
11580   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11581 }
11582
11583 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11584    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11585    parameters as specified in some function type specification (except for
11586    those which appear as part of a function *definition*).  */
11587
11588 static void
11589 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11590 {
11591   tree link;
11592   tree formal_type = NULL;
11593   tree first_parm_type;
11594   tree arg;
11595
11596   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11597     {
11598       arg = DECL_ARGUMENTS (function_or_method_type);
11599       function_or_method_type = TREE_TYPE (function_or_method_type);
11600     }
11601   else
11602     arg = NULL_TREE;
11603
11604   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11605
11606   /* Make our first pass over the list of formal parameter types and output a
11607      DW_TAG_formal_parameter DIE for each one.  */
11608   for (link = first_parm_type; link; )
11609     {
11610       dw_die_ref parm_die;
11611
11612       formal_type = TREE_VALUE (link);
11613       if (formal_type == void_type_node)
11614         break;
11615
11616       /* Output a (nameless) DIE to represent the formal parameter itself.  */
11617       parm_die = gen_formal_parameter_die (formal_type, context_die);
11618       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11619            && link == first_parm_type)
11620           || (arg && DECL_ARTIFICIAL (arg)))
11621         add_AT_flag (parm_die, DW_AT_artificial, 1);
11622
11623       link = TREE_CHAIN (link);
11624       if (arg)
11625         arg = TREE_CHAIN (arg);
11626     }
11627
11628   /* If this function type has an ellipsis, add a
11629      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
11630   if (formal_type != void_type_node)
11631     gen_unspecified_parameters_die (function_or_method_type, context_die);
11632
11633   /* Make our second (and final) pass over the list of formal parameter types
11634      and output DIEs to represent those types (as necessary).  */
11635   for (link = TYPE_ARG_TYPES (function_or_method_type);
11636        link && TREE_VALUE (link);
11637        link = TREE_CHAIN (link))
11638     gen_type_die (TREE_VALUE (link), context_die);
11639 }
11640
11641 /* We want to generate the DIE for TYPE so that we can generate the
11642    die for MEMBER, which has been defined; we will need to refer back
11643    to the member declaration nested within TYPE.  If we're trying to
11644    generate minimal debug info for TYPE, processing TYPE won't do the
11645    trick; we need to attach the member declaration by hand.  */
11646
11647 static void
11648 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11649 {
11650   gen_type_die (type, context_die);
11651
11652   /* If we're trying to avoid duplicate debug info, we may not have
11653      emitted the member decl for this function.  Emit it now.  */
11654   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11655       && ! lookup_decl_die (member))
11656     {
11657       dw_die_ref type_die;
11658       gcc_assert (!decl_ultimate_origin (member));
11659
11660       push_decl_scope (type);
11661       type_die = lookup_type_die (type);
11662       if (TREE_CODE (member) == FUNCTION_DECL)
11663         gen_subprogram_die (member, type_die);
11664       else if (TREE_CODE (member) == FIELD_DECL)
11665         {
11666           /* Ignore the nameless fields that are used to skip bits but handle
11667              C++ anonymous unions and structs.  */
11668           if (DECL_NAME (member) != NULL_TREE
11669               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11670               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11671             {
11672               gen_type_die (member_declared_type (member), type_die);
11673               gen_field_die (member, type_die);
11674             }
11675         }
11676       else
11677         gen_variable_die (member, type_die);
11678
11679       pop_decl_scope ();
11680     }
11681 }
11682
11683 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11684    may later generate inlined and/or out-of-line instances of.  */
11685
11686 static void
11687 dwarf2out_abstract_function (tree decl)
11688 {
11689   dw_die_ref old_die;
11690   tree save_fn;
11691   struct function *save_cfun;
11692   tree context;
11693   int was_abstract = DECL_ABSTRACT (decl);
11694
11695   /* Make sure we have the actual abstract inline, not a clone.  */
11696   decl = DECL_ORIGIN (decl);
11697
11698   old_die = lookup_decl_die (decl);
11699   if (old_die && get_AT (old_die, DW_AT_inline))
11700     /* We've already generated the abstract instance.  */
11701     return;
11702
11703   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11704      we don't get confused by DECL_ABSTRACT.  */
11705   if (debug_info_level > DINFO_LEVEL_TERSE)
11706     {
11707       context = decl_class_context (decl);
11708       if (context)
11709         gen_type_die_for_member
11710           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11711     }
11712
11713   /* Pretend we've just finished compiling this function.  */
11714   save_fn = current_function_decl;
11715   save_cfun = cfun;
11716   current_function_decl = decl;
11717   cfun = DECL_STRUCT_FUNCTION (decl);
11718
11719   set_decl_abstract_flags (decl, 1);
11720   dwarf2out_decl (decl);
11721   if (! was_abstract)
11722     set_decl_abstract_flags (decl, 0);
11723
11724   current_function_decl = save_fn;
11725   cfun = save_cfun;
11726 }
11727
11728 /* Helper function of premark_used_types() which gets called through
11729    htab_traverse_resize().
11730
11731    Marks the DIE of a given type in *SLOT as perennial, so it never gets
11732    marked as unused by prune_unused_types.  */
11733 static int
11734 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11735 {
11736   tree type;
11737   dw_die_ref die;
11738
11739   type = *slot;
11740   die = lookup_type_die (type);
11741   if (die != NULL)
11742     die->die_perennial_p = 1;
11743   return 1;
11744 }
11745
11746 /* Mark all members of used_types_hash as perennial.  */
11747 static void
11748 premark_used_types (void)
11749 {
11750   if (cfun && cfun->used_types_hash)
11751     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11752 }
11753
11754 /* Generate a DIE to represent a declared function (either file-scope or
11755    block-local).  */
11756
11757 static void
11758 gen_subprogram_die (tree decl, dw_die_ref context_die)
11759 {
11760   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11761   tree origin = decl_ultimate_origin (decl);
11762   dw_die_ref subr_die;
11763   tree fn_arg_types;
11764   tree outer_scope;
11765   dw_die_ref old_die = lookup_decl_die (decl);
11766   int declaration = (current_function_decl != decl
11767                      || class_or_namespace_scope_p (context_die));
11768
11769   premark_used_types ();
11770
11771   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11772      started to generate the abstract instance of an inline, decided to output
11773      its containing class, and proceeded to emit the declaration of the inline
11774      from the member list for the class.  If so, DECLARATION takes priority;
11775      we'll get back to the abstract instance when done with the class.  */
11776
11777   /* The class-scope declaration DIE must be the primary DIE.  */
11778   if (origin && declaration && class_or_namespace_scope_p (context_die))
11779     {
11780       origin = NULL;
11781       gcc_assert (!old_die);
11782     }
11783
11784   /* Now that the C++ front end lazily declares artificial member fns, we
11785      might need to retrofit the declaration into its class.  */
11786   if (!declaration && !origin && !old_die
11787       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11788       && !class_or_namespace_scope_p (context_die)
11789       && debug_info_level > DINFO_LEVEL_TERSE)
11790     old_die = force_decl_die (decl);
11791
11792   if (origin != NULL)
11793     {
11794       gcc_assert (!declaration || local_scope_p (context_die));
11795
11796       /* Fixup die_parent for the abstract instance of a nested
11797          inline function.  */
11798       if (old_die && old_die->die_parent == NULL)
11799         add_child_die (context_die, old_die);
11800
11801       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11802       add_abstract_origin_attribute (subr_die, origin);
11803     }
11804   else if (old_die)
11805     {
11806       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11807       struct dwarf_file_data * file_index = lookup_filename (s.file);
11808
11809       if (!get_AT_flag (old_die, DW_AT_declaration)
11810           /* We can have a normal definition following an inline one in the
11811              case of redefinition of GNU C extern inlines.
11812              It seems reasonable to use AT_specification in this case.  */
11813           && !get_AT (old_die, DW_AT_inline))
11814         {
11815           /* Detect and ignore this case, where we are trying to output
11816              something we have already output.  */
11817           return;
11818         }
11819
11820       /* If the definition comes from the same place as the declaration,
11821          maybe use the old DIE.  We always want the DIE for this function
11822          that has the *_pc attributes to be under comp_unit_die so the
11823          debugger can find it.  We also need to do this for abstract
11824          instances of inlines, since the spec requires the out-of-line copy
11825          to have the same parent.  For local class methods, this doesn't
11826          apply; we just use the old DIE.  */
11827       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11828           && (DECL_ARTIFICIAL (decl)
11829               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
11830                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
11831                       == (unsigned) s.line))))
11832         {
11833           subr_die = old_die;
11834
11835           /* Clear out the declaration attribute and the formal parameters.
11836              Do not remove all children, because it is possible that this
11837              declaration die was forced using force_decl_die(). In such
11838              cases die that forced declaration die (e.g. TAG_imported_module)
11839              is one of the children that we do not want to remove.  */
11840           remove_AT (subr_die, DW_AT_declaration);
11841           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11842         }
11843       else
11844         {
11845           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11846           add_AT_specification (subr_die, old_die);
11847           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
11848             add_AT_file (subr_die, DW_AT_decl_file, file_index);
11849           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
11850             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
11851         }
11852     }
11853   else
11854     {
11855       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11856
11857       if (TREE_PUBLIC (decl))
11858         add_AT_flag (subr_die, DW_AT_external, 1);
11859
11860       add_name_and_src_coords_attributes (subr_die, decl);
11861       if (debug_info_level > DINFO_LEVEL_TERSE)
11862         {
11863           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11864           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11865                               0, 0, context_die);
11866         }
11867
11868       add_pure_or_virtual_attribute (subr_die, decl);
11869       if (DECL_ARTIFICIAL (decl))
11870         add_AT_flag (subr_die, DW_AT_artificial, 1);
11871
11872       if (TREE_PROTECTED (decl))
11873         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11874       else if (TREE_PRIVATE (decl))
11875         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11876     }
11877
11878   if (declaration)
11879     {
11880       if (!old_die || !get_AT (old_die, DW_AT_inline))
11881         {
11882           add_AT_flag (subr_die, DW_AT_declaration, 1);
11883
11884           /* The first time we see a member function, it is in the context of
11885              the class to which it belongs.  We make sure of this by emitting
11886              the class first.  The next time is the definition, which is
11887              handled above.  The two may come from the same source text.
11888
11889              Note that force_decl_die() forces function declaration die. It is
11890              later reused to represent definition.  */
11891           equate_decl_number_to_die (decl, subr_die);
11892         }
11893     }
11894   else if (DECL_ABSTRACT (decl))
11895     {
11896       if (DECL_DECLARED_INLINE_P (decl))
11897         {
11898           if (cgraph_function_possibly_inlined_p (decl))
11899             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11900           else
11901             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11902         }
11903       else
11904         {
11905           if (cgraph_function_possibly_inlined_p (decl))
11906             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11907           else
11908             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11909         }
11910
11911       equate_decl_number_to_die (decl, subr_die);
11912     }
11913   else if (!DECL_EXTERNAL (decl))
11914     {
11915       HOST_WIDE_INT cfa_fb_offset;
11916
11917       if (!old_die || !get_AT (old_die, DW_AT_inline))
11918         equate_decl_number_to_die (decl, subr_die);
11919
11920       if (!flag_reorder_blocks_and_partition)
11921         {
11922           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11923                                        current_function_funcdef_no);
11924           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11925           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11926                                        current_function_funcdef_no);
11927           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11928           
11929           add_pubname (decl, subr_die);
11930           add_arange (decl, subr_die);
11931         }
11932       else
11933         {  /* Do nothing for now; maybe need to duplicate die, one for
11934               hot section and ond for cold section, then use the hot/cold
11935               section begin/end labels to generate the aranges...  */
11936           /*
11937             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11938             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11939             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11940             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11941
11942             add_pubname (decl, subr_die);
11943             add_arange (decl, subr_die);
11944             add_arange (decl, subr_die);
11945            */
11946         }
11947
11948 #ifdef MIPS_DEBUGGING_INFO
11949       /* Add a reference to the FDE for this routine.  */
11950       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11951 #endif
11952
11953       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
11954
11955       /* We define the "frame base" as the function's CFA.  This is more
11956          convenient for several reasons: (1) It's stable across the prologue
11957          and epilogue, which makes it better than just a frame pointer,
11958          (2) With dwarf3, there exists a one-byte encoding that allows us
11959          to reference the .debug_frame data by proxy, but failing that,
11960          (3) We can at least reuse the code inspection and interpretation
11961          code that determines the CFA position at various points in the
11962          function.  */
11963       /* ??? Use some command-line or configury switch to enable the use
11964          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11965          consumers that understand it; fall back to "pure" dwarf2 and
11966          convert the CFA data into a location list.  */
11967       {
11968         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11969         if (list->dw_loc_next)
11970           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11971         else
11972           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11973       }
11974
11975       /* Compute a displacement from the "steady-state frame pointer" to
11976          the CFA.  The former is what all stack slots and argument slots
11977          will reference in the rtl; the later is what we've told the 
11978          debugger about.  We'll need to adjust all frame_base references
11979          by this displacement.  */
11980       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11981
11982       if (cfun->static_chain_decl)
11983         add_AT_location_description (subr_die, DW_AT_static_link,
11984                  loc_descriptor_from_tree (cfun->static_chain_decl));
11985     }
11986
11987   /* Now output descriptions of the arguments for this function. This gets
11988      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11989      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11990      `...' at the end of the formal parameter list.  In order to find out if
11991      there was a trailing ellipsis or not, we must instead look at the type
11992      associated with the FUNCTION_DECL.  This will be a node of type
11993      FUNCTION_TYPE. If the chain of type nodes hanging off of this
11994      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11995      an ellipsis at the end.  */
11996
11997   /* In the case where we are describing a mere function declaration, all we
11998      need to do here (and all we *can* do here) is to describe the *types* of
11999      its formal parameters.  */
12000   if (debug_info_level <= DINFO_LEVEL_TERSE)
12001     ;
12002   else if (declaration)
12003     gen_formal_types_die (decl, subr_die);
12004   else
12005     {
12006       /* Generate DIEs to represent all known formal parameters.  */
12007       tree arg_decls = DECL_ARGUMENTS (decl);
12008       tree parm;
12009
12010       /* When generating DIEs, generate the unspecified_parameters DIE
12011          instead if we come across the arg "__builtin_va_alist" */
12012       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12013         if (TREE_CODE (parm) == PARM_DECL)
12014           {
12015             if (DECL_NAME (parm)
12016                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12017                             "__builtin_va_alist"))
12018               gen_unspecified_parameters_die (parm, subr_die);
12019             else
12020               gen_decl_die (parm, subr_die);
12021           }
12022
12023       /* Decide whether we need an unspecified_parameters DIE at the end.
12024          There are 2 more cases to do this for: 1) the ansi ... declaration -
12025          this is detectable when the end of the arg list is not a
12026          void_type_node 2) an unprototyped function declaration (not a
12027          definition).  This just means that we have no info about the
12028          parameters at all.  */
12029       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12030       if (fn_arg_types != NULL)
12031         {
12032           /* This is the prototyped case, check for....  */
12033           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12034             gen_unspecified_parameters_die (decl, subr_die);
12035         }
12036       else if (DECL_INITIAL (decl) == NULL_TREE)
12037         gen_unspecified_parameters_die (decl, subr_die);
12038     }
12039
12040   /* Output Dwarf info for all of the stuff within the body of the function
12041      (if it has one - it may be just a declaration).  */
12042   outer_scope = DECL_INITIAL (decl);
12043
12044   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12045      a function.  This BLOCK actually represents the outermost binding contour
12046      for the function, i.e. the contour in which the function's formal
12047      parameters and labels get declared. Curiously, it appears that the front
12048      end doesn't actually put the PARM_DECL nodes for the current function onto
12049      the BLOCK_VARS list for this outer scope, but are strung off of the
12050      DECL_ARGUMENTS list for the function instead.
12051
12052      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12053      the LABEL_DECL nodes for the function however, and we output DWARF info
12054      for those in decls_for_scope.  Just within the `outer_scope' there will be
12055      a BLOCK node representing the function's outermost pair of curly braces,
12056      and any blocks used for the base and member initializers of a C++
12057      constructor function.  */
12058   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12059     {
12060       /* Emit a DW_TAG_variable DIE for a named return value.  */
12061       if (DECL_NAME (DECL_RESULT (decl)))
12062         gen_decl_die (DECL_RESULT (decl), subr_die);
12063
12064       current_function_has_inlines = 0;
12065       decls_for_scope (outer_scope, subr_die, 0);
12066
12067 #if 0 && defined (MIPS_DEBUGGING_INFO)
12068       if (current_function_has_inlines)
12069         {
12070           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12071           if (! comp_unit_has_inlines)
12072             {
12073               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12074               comp_unit_has_inlines = 1;
12075             }
12076         }
12077 #endif
12078     }
12079   /* Add the calling convention attribute if requested.  */
12080   add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12081
12082 }
12083
12084 /* Generate a DIE to represent a declared data object.  */
12085
12086 static void
12087 gen_variable_die (tree decl, dw_die_ref context_die)
12088 {
12089   tree origin = decl_ultimate_origin (decl);
12090   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12091
12092   dw_die_ref old_die = lookup_decl_die (decl);
12093   int declaration = (DECL_EXTERNAL (decl)
12094                      /* If DECL is COMDAT and has not actually been
12095                         emitted, we cannot take its address; there
12096                         might end up being no definition anywhere in
12097                         the program.  For example, consider the C++
12098                         test case:
12099
12100                           template <class T>
12101                           struct S { static const int i = 7; };
12102
12103                           template <class T>
12104                           const int S<T>::i;
12105
12106                           int f() { return S<int>::i; }
12107                           
12108                         Here, S<int>::i is not DECL_EXTERNAL, but no
12109                         definition is required, so the compiler will
12110                         not emit a definition.  */  
12111                      || (TREE_CODE (decl) == VAR_DECL
12112                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12113                      || class_or_namespace_scope_p (context_die));
12114
12115   if (origin != NULL)
12116     add_abstract_origin_attribute (var_die, origin);
12117
12118   /* Loop unrolling can create multiple blocks that refer to the same
12119      static variable, so we must test for the DW_AT_declaration flag.
12120
12121      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12122      copy decls and set the DECL_ABSTRACT flag on them instead of
12123      sharing them.
12124
12125      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12126
12127      ??? The declare_in_namespace support causes us to get two DIEs for one
12128      variable, both of which are declarations.  We want to avoid considering
12129      one to be a specification, so we must test that this DIE is not a
12130      declaration.  */
12131   else if (old_die && TREE_STATIC (decl) && ! declaration
12132            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12133     {
12134       /* This is a definition of a C++ class level static.  */
12135       add_AT_specification (var_die, old_die);
12136       if (DECL_NAME (decl))
12137         {
12138           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12139           struct dwarf_file_data * file_index = lookup_filename (s.file);
12140
12141           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12142             add_AT_file (var_die, DW_AT_decl_file, file_index);
12143
12144           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12145             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12146         }
12147     }
12148   else
12149     {
12150       add_name_and_src_coords_attributes (var_die, decl);
12151       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
12152                           TREE_THIS_VOLATILE (decl), context_die);
12153
12154       if (TREE_PUBLIC (decl))
12155         add_AT_flag (var_die, DW_AT_external, 1);
12156
12157       if (DECL_ARTIFICIAL (decl))
12158         add_AT_flag (var_die, DW_AT_artificial, 1);
12159
12160       if (TREE_PROTECTED (decl))
12161         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12162       else if (TREE_PRIVATE (decl))
12163         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12164     }
12165
12166   if (declaration)
12167     add_AT_flag (var_die, DW_AT_declaration, 1);
12168
12169   if (DECL_ABSTRACT (decl) || declaration)
12170     equate_decl_number_to_die (decl, var_die);
12171
12172   if (! declaration && ! DECL_ABSTRACT (decl))
12173     {
12174       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12175       add_pubname (decl, var_die);
12176     }
12177   else
12178     tree_add_const_value_attribute (var_die, decl);
12179 }
12180
12181 /* Generate a DIE to represent a label identifier.  */
12182
12183 static void
12184 gen_label_die (tree decl, dw_die_ref context_die)
12185 {
12186   tree origin = decl_ultimate_origin (decl);
12187   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12188   rtx insn;
12189   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12190
12191   if (origin != NULL)
12192     add_abstract_origin_attribute (lbl_die, origin);
12193   else
12194     add_name_and_src_coords_attributes (lbl_die, decl);
12195
12196   if (DECL_ABSTRACT (decl))
12197     equate_decl_number_to_die (decl, lbl_die);
12198   else
12199     {
12200       insn = DECL_RTL_IF_SET (decl);
12201
12202       /* Deleted labels are programmer specified labels which have been
12203          eliminated because of various optimizations.  We still emit them
12204          here so that it is possible to put breakpoints on them.  */
12205       if (insn
12206           && (LABEL_P (insn)
12207               || ((NOTE_P (insn)
12208                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
12209         {
12210           /* When optimization is enabled (via -O) some parts of the compiler
12211              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12212              represent source-level labels which were explicitly declared by
12213              the user.  This really shouldn't be happening though, so catch
12214              it if it ever does happen.  */
12215           gcc_assert (!INSN_DELETED_P (insn));
12216
12217           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12218           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12219         }
12220     }
12221 }
12222
12223 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12224    attributes to the DIE for a block STMT, to describe where the inlined
12225    function was called from.  This is similar to add_src_coords_attributes.  */
12226
12227 static inline void
12228 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12229 {
12230   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12231
12232   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12233   add_AT_unsigned (die, DW_AT_call_line, s.line);
12234 }
12235
12236
12237 /* If STMT's abstract origin is a function declaration and STMT's
12238    first subblock's abstract origin is the function's outermost block,
12239    then we're looking at the main entry point.  */
12240 static bool
12241 is_inlined_entry_point (tree stmt)
12242 {
12243   tree decl, block;
12244
12245   if (!stmt || TREE_CODE (stmt) != BLOCK)
12246     return false;
12247
12248   decl = block_ultimate_origin (stmt);
12249
12250   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12251     return false;
12252
12253   block = BLOCK_SUBBLOCKS (stmt);
12254
12255   if (block)
12256     {
12257       if (TREE_CODE (block) != BLOCK)
12258         return false;
12259
12260       block = block_ultimate_origin (block);
12261     }
12262
12263   return block == DECL_INITIAL (decl);
12264 }
12265
12266 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12267    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12268
12269 static inline void
12270 add_high_low_attributes (tree stmt, dw_die_ref die)
12271 {
12272   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12273
12274   if (BLOCK_FRAGMENT_CHAIN (stmt))
12275     {
12276       tree chain;
12277
12278       if (is_inlined_entry_point (stmt))
12279         {
12280           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12281                                        BLOCK_NUMBER (stmt));
12282           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12283         }
12284
12285       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12286
12287       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12288       do
12289         {
12290           add_ranges (chain);
12291           chain = BLOCK_FRAGMENT_CHAIN (chain);
12292         }
12293       while (chain);
12294       add_ranges (NULL);
12295     }
12296   else
12297     {
12298       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12299                                    BLOCK_NUMBER (stmt));
12300       add_AT_lbl_id (die, DW_AT_low_pc, label);
12301       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12302                                    BLOCK_NUMBER (stmt));
12303       add_AT_lbl_id (die, DW_AT_high_pc, label);
12304     }
12305 }
12306
12307 /* Generate a DIE for a lexical block.  */
12308
12309 static void
12310 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12311 {
12312   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12313
12314   if (! BLOCK_ABSTRACT (stmt))
12315     add_high_low_attributes (stmt, stmt_die);
12316
12317   decls_for_scope (stmt, stmt_die, depth);
12318 }
12319
12320 /* Generate a DIE for an inlined subprogram.  */
12321
12322 static void
12323 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12324 {
12325   tree decl = block_ultimate_origin (stmt);
12326
12327   /* Emit info for the abstract instance first, if we haven't yet.  We
12328      must emit this even if the block is abstract, otherwise when we
12329      emit the block below (or elsewhere), we may end up trying to emit
12330      a die whose origin die hasn't been emitted, and crashing.  */
12331   dwarf2out_abstract_function (decl);
12332
12333   if (! BLOCK_ABSTRACT (stmt))
12334     {
12335       dw_die_ref subr_die
12336         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12337
12338       add_abstract_origin_attribute (subr_die, decl);
12339       add_high_low_attributes (stmt, subr_die);
12340       add_call_src_coords_attributes (stmt, subr_die);
12341
12342       decls_for_scope (stmt, subr_die, depth);
12343       current_function_has_inlines = 1;
12344     }
12345   else
12346     /* We may get here if we're the outer block of function A that was
12347        inlined into function B that was inlined into function C.  When
12348        generating debugging info for C, dwarf2out_abstract_function(B)
12349        would mark all inlined blocks as abstract, including this one.
12350        So, we wouldn't (and shouldn't) expect labels to be generated
12351        for this one.  Instead, just emit debugging info for
12352        declarations within the block.  This is particularly important
12353        in the case of initializers of arguments passed from B to us:
12354        if they're statement expressions containing declarations, we
12355        wouldn't generate dies for their abstract variables, and then,
12356        when generating dies for the real variables, we'd die (pun
12357        intended :-)  */
12358     gen_lexical_block_die (stmt, context_die, depth);
12359 }
12360
12361 /* Generate a DIE for a field in a record, or structure.  */
12362
12363 static void
12364 gen_field_die (tree decl, dw_die_ref context_die)
12365 {
12366   dw_die_ref decl_die;
12367
12368   if (TREE_TYPE (decl) == error_mark_node)
12369     return;
12370
12371   decl_die = new_die (DW_TAG_member, context_die, decl);
12372   add_name_and_src_coords_attributes (decl_die, decl);
12373   add_type_attribute (decl_die, member_declared_type (decl),
12374                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12375                       context_die);
12376
12377   if (DECL_BIT_FIELD_TYPE (decl))
12378     {
12379       add_byte_size_attribute (decl_die, decl);
12380       add_bit_size_attribute (decl_die, decl);
12381       add_bit_offset_attribute (decl_die, decl);
12382     }
12383
12384   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12385     add_data_member_location_attribute (decl_die, decl);
12386
12387   if (DECL_ARTIFICIAL (decl))
12388     add_AT_flag (decl_die, DW_AT_artificial, 1);
12389
12390   if (TREE_PROTECTED (decl))
12391     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12392   else if (TREE_PRIVATE (decl))
12393     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12394
12395   /* Equate decl number to die, so that we can look up this decl later on.  */
12396   equate_decl_number_to_die (decl, decl_die);
12397 }
12398
12399 #if 0
12400 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12401    Use modified_type_die instead.
12402    We keep this code here just in case these types of DIEs may be needed to
12403    represent certain things in other languages (e.g. Pascal) someday.  */
12404
12405 static void
12406 gen_pointer_type_die (tree type, dw_die_ref context_die)
12407 {
12408   dw_die_ref ptr_die
12409     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12410
12411   equate_type_number_to_die (type, ptr_die);
12412   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12413   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12414 }
12415
12416 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12417    Use modified_type_die instead.
12418    We keep this code here just in case these types of DIEs may be needed to
12419    represent certain things in other languages (e.g. Pascal) someday.  */
12420
12421 static void
12422 gen_reference_type_die (tree type, dw_die_ref context_die)
12423 {
12424   dw_die_ref ref_die
12425     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12426
12427   equate_type_number_to_die (type, ref_die);
12428   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12429   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12430 }
12431 #endif
12432
12433 /* Generate a DIE for a pointer to a member type.  */
12434
12435 static void
12436 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12437 {
12438   dw_die_ref ptr_die
12439     = new_die (DW_TAG_ptr_to_member_type,
12440                scope_die_for (type, context_die), type);
12441
12442   equate_type_number_to_die (type, ptr_die);
12443   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12444                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12445   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12446 }
12447
12448 /* Generate the DIE for the compilation unit.  */
12449
12450 static dw_die_ref
12451 gen_compile_unit_die (const char *filename)
12452 {
12453   dw_die_ref die;
12454   char producer[250];
12455   const char *language_string = lang_hooks.name;
12456   int language;
12457
12458   die = new_die (DW_TAG_compile_unit, NULL, NULL);
12459
12460   if (filename)
12461     {
12462       add_name_attribute (die, filename);
12463       /* Don't add cwd for <built-in>.  */
12464       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12465         add_comp_dir_attribute (die);
12466     }
12467
12468   sprintf (producer, "%s %s", language_string, version_string);
12469
12470 #ifdef MIPS_DEBUGGING_INFO
12471   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12472      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12473      not appear in the producer string, the debugger reaches the conclusion
12474      that the object file is stripped and has no debugging information.
12475      To get the MIPS/SGI debugger to believe that there is debugging
12476      information in the object file, we add a -g to the producer string.  */
12477   if (debug_info_level > DINFO_LEVEL_TERSE)
12478     strcat (producer, " -g");
12479 #endif
12480
12481   add_AT_string (die, DW_AT_producer, producer);
12482
12483   if (strcmp (language_string, "GNU C++") == 0)
12484     language = DW_LANG_C_plus_plus;
12485   else if (strcmp (language_string, "GNU Ada") == 0)
12486     language = DW_LANG_Ada95;
12487   else if (strcmp (language_string, "GNU F77") == 0)
12488     language = DW_LANG_Fortran77;
12489   else if (strcmp (language_string, "GNU F95") == 0)
12490     language = DW_LANG_Fortran95;
12491   else if (strcmp (language_string, "GNU Pascal") == 0)
12492     language = DW_LANG_Pascal83;
12493   else if (strcmp (language_string, "GNU Java") == 0)
12494     language = DW_LANG_Java;
12495   else if (strcmp (language_string, "GNU Objective-C") == 0)
12496     language = DW_LANG_ObjC;
12497   else if (strcmp (language_string, "GNU Objective-C++") == 0)
12498     language = DW_LANG_ObjC_plus_plus;
12499   else
12500     language = DW_LANG_C89;
12501
12502   add_AT_unsigned (die, DW_AT_language, language);
12503   return die;
12504 }
12505
12506 /* Generate the DIE for a base class.  */
12507
12508 static void
12509 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12510 {
12511   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12512
12513   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12514   add_data_member_location_attribute (die, binfo);
12515
12516   if (BINFO_VIRTUAL_P (binfo))
12517     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12518
12519   if (access == access_public_node)
12520     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12521   else if (access == access_protected_node)
12522     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12523 }
12524
12525 /* Generate a DIE for a class member.  */
12526
12527 static void
12528 gen_member_die (tree type, dw_die_ref context_die)
12529 {
12530   tree member;
12531   tree binfo = TYPE_BINFO (type);
12532   dw_die_ref child;
12533
12534   /* If this is not an incomplete type, output descriptions of each of its
12535      members. Note that as we output the DIEs necessary to represent the
12536      members of this record or union type, we will also be trying to output
12537      DIEs to represent the *types* of those members. However the `type'
12538      function (above) will specifically avoid generating type DIEs for member
12539      types *within* the list of member DIEs for this (containing) type except
12540      for those types (of members) which are explicitly marked as also being
12541      members of this (containing) type themselves.  The g++ front- end can
12542      force any given type to be treated as a member of some other (containing)
12543      type by setting the TYPE_CONTEXT of the given (member) type to point to
12544      the TREE node representing the appropriate (containing) type.  */
12545
12546   /* First output info about the base classes.  */
12547   if (binfo)
12548     {
12549       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12550       int i;
12551       tree base;
12552
12553       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12554         gen_inheritance_die (base,
12555                              (accesses ? VEC_index (tree, accesses, i)
12556                               : access_public_node), context_die);
12557     }
12558
12559   /* Now output info about the data members and type members.  */
12560   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12561     {
12562       /* If we thought we were generating minimal debug info for TYPE
12563          and then changed our minds, some of the member declarations
12564          may have already been defined.  Don't define them again, but
12565          do put them in the right order.  */
12566
12567       child = lookup_decl_die (member);
12568       if (child)
12569         splice_child_die (context_die, child);
12570       else
12571         gen_decl_die (member, context_die);
12572     }
12573
12574   /* Now output info about the function members (if any).  */
12575   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12576     {
12577       /* Don't include clones in the member list.  */
12578       if (DECL_ABSTRACT_ORIGIN (member))
12579         continue;
12580
12581       child = lookup_decl_die (member);
12582       if (child)
12583         splice_child_die (context_die, child);
12584       else
12585         gen_decl_die (member, context_die);
12586     }
12587 }
12588
12589 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
12590    is set, we pretend that the type was never defined, so we only get the
12591    member DIEs needed by later specification DIEs.  */
12592
12593 static void
12594 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12595 {
12596   dw_die_ref type_die = lookup_type_die (type);
12597   dw_die_ref scope_die = 0;
12598   int nested = 0;
12599   int complete = (TYPE_SIZE (type)
12600                   && (! TYPE_STUB_DECL (type)
12601                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12602   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12603
12604   if (type_die && ! complete)
12605     return;
12606
12607   if (TYPE_CONTEXT (type) != NULL_TREE
12608       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12609           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12610     nested = 1;
12611
12612   scope_die = scope_die_for (type, context_die);
12613
12614   if (! type_die || (nested && scope_die == comp_unit_die))
12615     /* First occurrence of type or toplevel definition of nested class.  */
12616     {
12617       dw_die_ref old_die = type_die;
12618
12619       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12620                           ? DW_TAG_structure_type : DW_TAG_union_type,
12621                           scope_die, type);
12622       equate_type_number_to_die (type, type_die);
12623       if (old_die)
12624         add_AT_specification (type_die, old_die);
12625       else
12626         add_name_attribute (type_die, type_tag (type));
12627     }
12628   else
12629     remove_AT (type_die, DW_AT_declaration);
12630
12631   /* If this type has been completed, then give it a byte_size attribute and
12632      then give a list of members.  */
12633   if (complete && !ns_decl)
12634     {
12635       /* Prevent infinite recursion in cases where the type of some member of
12636          this type is expressed in terms of this type itself.  */
12637       TREE_ASM_WRITTEN (type) = 1;
12638       add_byte_size_attribute (type_die, type);
12639       if (TYPE_STUB_DECL (type) != NULL_TREE)
12640         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12641
12642       /* If the first reference to this type was as the return type of an
12643          inline function, then it may not have a parent.  Fix this now.  */
12644       if (type_die->die_parent == NULL)
12645         add_child_die (scope_die, type_die);
12646
12647       push_decl_scope (type);
12648       gen_member_die (type, type_die);
12649       pop_decl_scope ();
12650
12651       /* GNU extension: Record what type our vtable lives in.  */
12652       if (TYPE_VFIELD (type))
12653         {
12654           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12655
12656           gen_type_die (vtype, context_die);
12657           add_AT_die_ref (type_die, DW_AT_containing_type,
12658                           lookup_type_die (vtype));
12659         }
12660     }
12661   else
12662     {
12663       add_AT_flag (type_die, DW_AT_declaration, 1);
12664
12665       /* We don't need to do this for function-local types.  */
12666       if (TYPE_STUB_DECL (type)
12667           && ! decl_function_context (TYPE_STUB_DECL (type)))
12668         VEC_safe_push (tree, gc, incomplete_types, type);
12669     }
12670
12671   if (get_AT (type_die, DW_AT_name))
12672     add_pubtype (type, type_die);
12673 }
12674
12675 /* Generate a DIE for a subroutine _type_.  */
12676
12677 static void
12678 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12679 {
12680   tree return_type = TREE_TYPE (type);
12681   dw_die_ref subr_die
12682     = new_die (DW_TAG_subroutine_type,
12683                scope_die_for (type, context_die), type);
12684
12685   equate_type_number_to_die (type, subr_die);
12686   add_prototyped_attribute (subr_die, type);
12687   add_type_attribute (subr_die, return_type, 0, 0, context_die);
12688   gen_formal_types_die (type, subr_die);
12689
12690   if (get_AT (subr_die, DW_AT_name))
12691     add_pubtype (type, subr_die);
12692 }
12693
12694 /* Generate a DIE for a type definition.  */
12695
12696 static void
12697 gen_typedef_die (tree decl, dw_die_ref context_die)
12698 {
12699   dw_die_ref type_die;
12700   tree origin;
12701
12702   if (TREE_ASM_WRITTEN (decl))
12703     return;
12704
12705   TREE_ASM_WRITTEN (decl) = 1;
12706   type_die = new_die (DW_TAG_typedef, context_die, decl);
12707   origin = decl_ultimate_origin (decl);
12708   if (origin != NULL)
12709     add_abstract_origin_attribute (type_die, origin);
12710   else
12711     {
12712       tree type;
12713
12714       add_name_and_src_coords_attributes (type_die, decl);
12715       if (DECL_ORIGINAL_TYPE (decl))
12716         {
12717           type = DECL_ORIGINAL_TYPE (decl);
12718
12719           gcc_assert (type != TREE_TYPE (decl));
12720           equate_type_number_to_die (TREE_TYPE (decl), type_die);
12721         }
12722       else
12723         type = TREE_TYPE (decl);
12724
12725       add_type_attribute (type_die, type, TREE_READONLY (decl),
12726                           TREE_THIS_VOLATILE (decl), context_die);
12727     }
12728
12729   if (DECL_ABSTRACT (decl))
12730     equate_decl_number_to_die (decl, type_die);
12731
12732   if (get_AT (type_die, DW_AT_name))
12733     add_pubtype (decl, type_die);
12734 }
12735
12736 /* Generate a type description DIE.  */
12737
12738 static void
12739 gen_type_die (tree type, dw_die_ref context_die)
12740 {
12741   int need_pop;
12742
12743   if (type == NULL_TREE || type == error_mark_node)
12744     return;
12745
12746   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12747       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12748     {
12749       if (TREE_ASM_WRITTEN (type))
12750         return;
12751
12752       /* Prevent broken recursion; we can't hand off to the same type.  */
12753       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12754
12755       TREE_ASM_WRITTEN (type) = 1;
12756       gen_decl_die (TYPE_NAME (type), context_die);
12757       return;
12758     }
12759
12760   /* We are going to output a DIE to represent the unqualified version
12761      of this type (i.e. without any const or volatile qualifiers) so
12762      get the main variant (i.e. the unqualified version) of this type
12763      now.  (Vectors are special because the debugging info is in the
12764      cloned type itself).  */
12765   if (TREE_CODE (type) != VECTOR_TYPE)
12766     type = type_main_variant (type);
12767
12768   if (TREE_ASM_WRITTEN (type))
12769     return;
12770
12771   switch (TREE_CODE (type))
12772     {
12773     case ERROR_MARK:
12774       break;
12775
12776     case POINTER_TYPE:
12777     case REFERENCE_TYPE:
12778       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
12779          ensures that the gen_type_die recursion will terminate even if the
12780          type is recursive.  Recursive types are possible in Ada.  */
12781       /* ??? We could perhaps do this for all types before the switch
12782          statement.  */
12783       TREE_ASM_WRITTEN (type) = 1;
12784
12785       /* For these types, all that is required is that we output a DIE (or a
12786          set of DIEs) to represent the "basis" type.  */
12787       gen_type_die (TREE_TYPE (type), context_die);
12788       break;
12789
12790     case OFFSET_TYPE:
12791       /* This code is used for C++ pointer-to-data-member types.
12792          Output a description of the relevant class type.  */
12793       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12794
12795       /* Output a description of the type of the object pointed to.  */
12796       gen_type_die (TREE_TYPE (type), context_die);
12797
12798       /* Now output a DIE to represent this pointer-to-data-member type
12799          itself.  */
12800       gen_ptr_to_mbr_type_die (type, context_die);
12801       break;
12802
12803     case FUNCTION_TYPE:
12804       /* Force out return type (in case it wasn't forced out already).  */
12805       gen_type_die (TREE_TYPE (type), context_die);
12806       gen_subroutine_type_die (type, context_die);
12807       break;
12808
12809     case METHOD_TYPE:
12810       /* Force out return type (in case it wasn't forced out already).  */
12811       gen_type_die (TREE_TYPE (type), context_die);
12812       gen_subroutine_type_die (type, context_die);
12813       break;
12814
12815     case ARRAY_TYPE:
12816       gen_array_type_die (type, context_die);
12817       break;
12818
12819     case VECTOR_TYPE:
12820       gen_array_type_die (type, context_die);
12821       break;
12822
12823     case ENUMERAL_TYPE:
12824     case RECORD_TYPE:
12825     case UNION_TYPE:
12826     case QUAL_UNION_TYPE:
12827       /* If this is a nested type whose containing class hasn't been written
12828          out yet, writing it out will cover this one, too.  This does not apply
12829          to instantiations of member class templates; they need to be added to
12830          the containing class as they are generated.  FIXME: This hurts the
12831          idea of combining type decls from multiple TUs, since we can't predict
12832          what set of template instantiations we'll get.  */
12833       if (TYPE_CONTEXT (type)
12834           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12835           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12836         {
12837           gen_type_die (TYPE_CONTEXT (type), context_die);
12838
12839           if (TREE_ASM_WRITTEN (type))
12840             return;
12841
12842           /* If that failed, attach ourselves to the stub.  */
12843           push_decl_scope (TYPE_CONTEXT (type));
12844           context_die = lookup_type_die (TYPE_CONTEXT (type));
12845           need_pop = 1;
12846         }
12847       else
12848         {
12849           declare_in_namespace (type, context_die);
12850           need_pop = 0;
12851         }
12852
12853       if (TREE_CODE (type) == ENUMERAL_TYPE)
12854         {
12855           /* This might have been written out by the call to
12856              declare_in_namespace.  */
12857           if (!TREE_ASM_WRITTEN (type))
12858             gen_enumeration_type_die (type, context_die);
12859         }
12860       else
12861         gen_struct_or_union_type_die (type, context_die);
12862
12863       if (need_pop)
12864         pop_decl_scope ();
12865
12866       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12867          it up if it is ever completed.  gen_*_type_die will set it for us
12868          when appropriate.  */
12869       return;
12870
12871     case VOID_TYPE:
12872     case INTEGER_TYPE:
12873     case REAL_TYPE:
12874     case COMPLEX_TYPE:
12875     case BOOLEAN_TYPE:
12876       /* No DIEs needed for fundamental types.  */
12877       break;
12878
12879     case LANG_TYPE:
12880       /* No Dwarf representation currently defined.  */
12881       break;
12882
12883     default:
12884       gcc_unreachable ();
12885     }
12886
12887   TREE_ASM_WRITTEN (type) = 1;
12888 }
12889
12890 /* Generate a DIE for a tagged type instantiation.  */
12891
12892 static void
12893 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12894 {
12895   if (type == NULL_TREE || type == error_mark_node)
12896     return;
12897
12898   /* We are going to output a DIE to represent the unqualified version of
12899      this type (i.e. without any const or volatile qualifiers) so make sure
12900      that we have the main variant (i.e. the unqualified version) of this
12901      type now.  */
12902   gcc_assert (type == type_main_variant (type));
12903
12904   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12905      an instance of an unresolved type.  */
12906
12907   switch (TREE_CODE (type))
12908     {
12909     case ERROR_MARK:
12910       break;
12911
12912     case ENUMERAL_TYPE:
12913       gen_inlined_enumeration_type_die (type, context_die);
12914       break;
12915
12916     case RECORD_TYPE:
12917       gen_inlined_structure_type_die (type, context_die);
12918       break;
12919
12920     case UNION_TYPE:
12921     case QUAL_UNION_TYPE:
12922       gen_inlined_union_type_die (type, context_die);
12923       break;
12924
12925     default:
12926       gcc_unreachable ();
12927     }
12928 }
12929
12930 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12931    things which are local to the given block.  */
12932
12933 static void
12934 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12935 {
12936   int must_output_die = 0;
12937   tree origin;
12938   tree decl;
12939   enum tree_code origin_code;
12940
12941   /* Ignore blocks that are NULL.  */
12942   if (stmt == NULL_TREE)
12943     return;
12944
12945   /* If the block is one fragment of a non-contiguous block, do not
12946      process the variables, since they will have been done by the
12947      origin block.  Do process subblocks.  */
12948   if (BLOCK_FRAGMENT_ORIGIN (stmt))
12949     {
12950       tree sub;
12951
12952       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12953         gen_block_die (sub, context_die, depth + 1);
12954
12955       return;
12956     }
12957
12958   /* Determine the "ultimate origin" of this block.  This block may be an
12959      inlined instance of an inlined instance of inline function, so we have
12960      to trace all of the way back through the origin chain to find out what
12961      sort of node actually served as the original seed for the creation of
12962      the current block.  */
12963   origin = block_ultimate_origin (stmt);
12964   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12965
12966   /* Determine if we need to output any Dwarf DIEs at all to represent this
12967      block.  */
12968   if (origin_code == FUNCTION_DECL)
12969     /* The outer scopes for inlinings *must* always be represented.  We
12970        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
12971     must_output_die = 1;
12972   else
12973     {
12974       /* In the case where the current block represents an inlining of the
12975          "body block" of an inline function, we must *NOT* output any DIE for
12976          this block because we have already output a DIE to represent the whole
12977          inlined function scope and the "body block" of any function doesn't
12978          really represent a different scope according to ANSI C rules.  So we
12979          check here to make sure that this block does not represent a "body
12980          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
12981       if (! is_body_block (origin ? origin : stmt))
12982         {
12983           /* Determine if this block directly contains any "significant"
12984              local declarations which we will need to output DIEs for.  */
12985           if (debug_info_level > DINFO_LEVEL_TERSE)
12986             /* We are not in terse mode so *any* local declaration counts
12987                as being a "significant" one.  */
12988             must_output_die = (BLOCK_VARS (stmt) != NULL 
12989                                && (TREE_USED (stmt) 
12990                                    || TREE_ASM_WRITTEN (stmt)
12991                                    || BLOCK_ABSTRACT (stmt)));
12992           else
12993             /* We are in terse mode, so only local (nested) function
12994                definitions count as "significant" local declarations.  */
12995             for (decl = BLOCK_VARS (stmt);
12996                  decl != NULL; decl = TREE_CHAIN (decl))
12997               if (TREE_CODE (decl) == FUNCTION_DECL
12998                   && DECL_INITIAL (decl))
12999                 {
13000                   must_output_die = 1;
13001                   break;
13002                 }
13003         }
13004     }
13005
13006   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13007      DIE for any block which contains no significant local declarations at
13008      all.  Rather, in such cases we just call `decls_for_scope' so that any
13009      needed Dwarf info for any sub-blocks will get properly generated. Note
13010      that in terse mode, our definition of what constitutes a "significant"
13011      local declaration gets restricted to include only inlined function
13012      instances and local (nested) function definitions.  */
13013   if (must_output_die)
13014     {
13015       if (origin_code == FUNCTION_DECL)
13016         gen_inlined_subroutine_die (stmt, context_die, depth);
13017       else
13018         gen_lexical_block_die (stmt, context_die, depth);
13019     }
13020   else
13021     decls_for_scope (stmt, context_die, depth);
13022 }
13023
13024 /* Generate all of the decls declared within a given scope and (recursively)
13025    all of its sub-blocks.  */
13026
13027 static void
13028 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13029 {
13030   tree decl;
13031   tree subblocks;
13032
13033   /* Ignore NULL blocks.  */
13034   if (stmt == NULL_TREE)
13035     return;
13036
13037   if (TREE_USED (stmt))
13038     {
13039       /* Output the DIEs to represent all of the data objects and typedefs
13040          declared directly within this block but not within any nested
13041          sub-blocks.  Also, nested function and tag DIEs have been
13042          generated with a parent of NULL; fix that up now.  */
13043       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13044         {
13045           dw_die_ref die;
13046           
13047           if (TREE_CODE (decl) == FUNCTION_DECL)
13048             die = lookup_decl_die (decl);
13049           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13050             die = lookup_type_die (TREE_TYPE (decl));
13051           else
13052             die = NULL;
13053           
13054           if (die != NULL && die->die_parent == NULL)
13055             add_child_die (context_die, die);
13056           /* Do not produce debug information for static variables since
13057              these might be optimized out.  We are called for these later
13058              in varpool_analyze_pending_decls. */
13059           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13060             ;
13061           else
13062             gen_decl_die (decl, context_die);
13063         }
13064     }
13065
13066   /* If we're at -g1, we're not interested in subblocks.  */
13067   if (debug_info_level <= DINFO_LEVEL_TERSE)
13068     return;
13069
13070   /* Output the DIEs to represent all sub-blocks (and the items declared
13071      therein) of this block.  */
13072   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13073        subblocks != NULL;
13074        subblocks = BLOCK_CHAIN (subblocks))
13075     gen_block_die (subblocks, context_die, depth + 1);
13076 }
13077
13078 /* Is this a typedef we can avoid emitting?  */
13079
13080 static inline int
13081 is_redundant_typedef (tree decl)
13082 {
13083   if (TYPE_DECL_IS_STUB (decl))
13084     return 1;
13085
13086   if (DECL_ARTIFICIAL (decl)
13087       && DECL_CONTEXT (decl)
13088       && is_tagged_type (DECL_CONTEXT (decl))
13089       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13090       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13091     /* Also ignore the artificial member typedef for the class name.  */
13092     return 1;
13093
13094   return 0;
13095 }
13096
13097 /* Returns the DIE for decl.  A DIE will always be returned.  */
13098
13099 static dw_die_ref
13100 force_decl_die (tree decl)
13101 {
13102   dw_die_ref decl_die;
13103   unsigned saved_external_flag;
13104   tree save_fn = NULL_TREE;
13105   decl_die = lookup_decl_die (decl);
13106   if (!decl_die)
13107     {
13108       dw_die_ref context_die;
13109       tree decl_context = DECL_CONTEXT (decl);
13110       if (decl_context)
13111         {
13112           /* Find die that represents this context.  */
13113           if (TYPE_P (decl_context))
13114             context_die = force_type_die (decl_context);
13115           else
13116             context_die = force_decl_die (decl_context);
13117         }
13118       else
13119         context_die = comp_unit_die;
13120
13121       decl_die = lookup_decl_die (decl);
13122       if (decl_die)
13123         return decl_die;
13124
13125       switch (TREE_CODE (decl))
13126         {
13127         case FUNCTION_DECL:
13128           /* Clear current_function_decl, so that gen_subprogram_die thinks
13129              that this is a declaration. At this point, we just want to force
13130              declaration die.  */
13131           save_fn = current_function_decl;
13132           current_function_decl = NULL_TREE;
13133           gen_subprogram_die (decl, context_die);
13134           current_function_decl = save_fn;
13135           break;
13136
13137         case VAR_DECL:
13138           /* Set external flag to force declaration die. Restore it after
13139            gen_decl_die() call.  */
13140           saved_external_flag = DECL_EXTERNAL (decl);
13141           DECL_EXTERNAL (decl) = 1;
13142           gen_decl_die (decl, context_die);
13143           DECL_EXTERNAL (decl) = saved_external_flag;
13144           break;
13145
13146         case NAMESPACE_DECL:
13147           dwarf2out_decl (decl);
13148           break;
13149
13150         default:
13151           gcc_unreachable ();
13152         }
13153
13154       /* We should be able to find the DIE now.  */
13155       if (!decl_die)
13156         decl_die = lookup_decl_die (decl);
13157       gcc_assert (decl_die);
13158     }
13159
13160   return decl_die;
13161 }
13162
13163 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13164    always returned.  */
13165
13166 static dw_die_ref
13167 force_type_die (tree type)
13168 {
13169   dw_die_ref type_die;
13170
13171   type_die = lookup_type_die (type);
13172   if (!type_die)
13173     {
13174       dw_die_ref context_die;
13175       if (TYPE_CONTEXT (type))
13176         {
13177           if (TYPE_P (TYPE_CONTEXT (type)))
13178             context_die = force_type_die (TYPE_CONTEXT (type));
13179           else
13180             context_die = force_decl_die (TYPE_CONTEXT (type));
13181         }
13182       else
13183         context_die = comp_unit_die;
13184
13185       type_die = lookup_type_die (type);
13186       if (type_die)
13187         return type_die;
13188       gen_type_die (type, context_die);
13189       type_die = lookup_type_die (type);
13190       gcc_assert (type_die);
13191     }
13192   return type_die;
13193 }
13194
13195 /* Force out any required namespaces to be able to output DECL,
13196    and return the new context_die for it, if it's changed.  */
13197
13198 static dw_die_ref
13199 setup_namespace_context (tree thing, dw_die_ref context_die)
13200 {
13201   tree context = (DECL_P (thing)
13202                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13203   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13204     /* Force out the namespace.  */
13205     context_die = force_decl_die (context);
13206
13207   return context_die;
13208 }
13209
13210 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13211    type) within its namespace, if appropriate.
13212
13213    For compatibility with older debuggers, namespace DIEs only contain
13214    declarations; all definitions are emitted at CU scope.  */
13215
13216 static void
13217 declare_in_namespace (tree thing, dw_die_ref context_die)
13218 {
13219   dw_die_ref ns_context;
13220
13221   if (debug_info_level <= DINFO_LEVEL_TERSE)
13222     return;
13223
13224   /* If this decl is from an inlined function, then don't try to emit it in its
13225      namespace, as we will get confused.  It would have already been emitted
13226      when the abstract instance of the inline function was emitted anyways.  */
13227   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13228     return;
13229
13230   ns_context = setup_namespace_context (thing, context_die);
13231
13232   if (ns_context != context_die)
13233     {
13234       if (DECL_P (thing))
13235         gen_decl_die (thing, ns_context);
13236       else
13237         gen_type_die (thing, ns_context);
13238     }
13239 }
13240
13241 /* Generate a DIE for a namespace or namespace alias.  */
13242
13243 static void
13244 gen_namespace_die (tree decl)
13245 {
13246   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13247
13248   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13249      they are an alias of.  */
13250   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13251     {
13252       /* Output a real namespace.  */
13253       dw_die_ref namespace_die
13254         = new_die (DW_TAG_namespace, context_die, decl);
13255       add_name_and_src_coords_attributes (namespace_die, decl);
13256       equate_decl_number_to_die (decl, namespace_die);
13257     }
13258   else
13259     {
13260       /* Output a namespace alias.  */
13261
13262       /* Force out the namespace we are an alias of, if necessary.  */
13263       dw_die_ref origin_die
13264         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13265
13266       /* Now create the namespace alias DIE.  */
13267       dw_die_ref namespace_die
13268         = new_die (DW_TAG_imported_declaration, context_die, decl);
13269       add_name_and_src_coords_attributes (namespace_die, decl);
13270       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13271       equate_decl_number_to_die (decl, namespace_die);
13272     }
13273 }
13274
13275 /* Generate Dwarf debug information for a decl described by DECL.  */
13276
13277 static void
13278 gen_decl_die (tree decl, dw_die_ref context_die)
13279 {
13280   tree origin;
13281
13282   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13283     return;
13284
13285   switch (TREE_CODE (decl))
13286     {
13287     case ERROR_MARK:
13288       break;
13289
13290     case CONST_DECL:
13291       /* The individual enumerators of an enum type get output when we output
13292          the Dwarf representation of the relevant enum type itself.  */
13293       break;
13294
13295     case FUNCTION_DECL:
13296       /* Don't output any DIEs to represent mere function declarations,
13297          unless they are class members or explicit block externs.  */
13298       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13299           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13300         break;
13301
13302 #if 0
13303       /* FIXME */
13304       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13305          on local redeclarations of global functions.  That seems broken.  */
13306       if (current_function_decl != decl)
13307         /* This is only a declaration.  */;
13308 #endif
13309
13310       /* If we're emitting a clone, emit info for the abstract instance.  */
13311       if (DECL_ORIGIN (decl) != decl)
13312         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13313
13314       /* If we're emitting an out-of-line copy of an inline function,
13315          emit info for the abstract instance and set up to refer to it.  */
13316       else if (cgraph_function_possibly_inlined_p (decl)
13317                && ! DECL_ABSTRACT (decl)
13318                && ! class_or_namespace_scope_p (context_die)
13319                /* dwarf2out_abstract_function won't emit a die if this is just
13320                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13321                   that case, because that works only if we have a die.  */
13322                && DECL_INITIAL (decl) != NULL_TREE)
13323         {
13324           dwarf2out_abstract_function (decl);
13325           set_decl_origin_self (decl);
13326         }
13327
13328       /* Otherwise we're emitting the primary DIE for this decl.  */
13329       else if (debug_info_level > DINFO_LEVEL_TERSE)
13330         {
13331           /* Before we describe the FUNCTION_DECL itself, make sure that we
13332              have described its return type.  */
13333           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13334
13335           /* And its virtual context.  */
13336           if (DECL_VINDEX (decl) != NULL_TREE)
13337             gen_type_die (DECL_CONTEXT (decl), context_die);
13338
13339           /* And its containing type.  */
13340           origin = decl_class_context (decl);
13341           if (origin != NULL_TREE)
13342             gen_type_die_for_member (origin, decl, context_die);
13343
13344           /* And its containing namespace.  */
13345           declare_in_namespace (decl, context_die);
13346         }
13347
13348       /* Now output a DIE to represent the function itself.  */
13349       gen_subprogram_die (decl, context_die);
13350       break;
13351
13352     case TYPE_DECL:
13353       /* If we are in terse mode, don't generate any DIEs to represent any
13354          actual typedefs.  */
13355       if (debug_info_level <= DINFO_LEVEL_TERSE)
13356         break;
13357
13358       /* In the special case of a TYPE_DECL node representing the declaration
13359          of some type tag, if the given TYPE_DECL is marked as having been
13360          instantiated from some other (original) TYPE_DECL node (e.g. one which
13361          was generated within the original definition of an inline function) we
13362          have to generate a special (abbreviated) DW_TAG_structure_type,
13363          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13364       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13365         {
13366           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13367           break;
13368         }
13369
13370       if (is_redundant_typedef (decl))
13371         gen_type_die (TREE_TYPE (decl), context_die);
13372       else
13373         /* Output a DIE to represent the typedef itself.  */
13374         gen_typedef_die (decl, context_die);
13375       break;
13376
13377     case LABEL_DECL:
13378       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13379         gen_label_die (decl, context_die);
13380       break;
13381
13382     case VAR_DECL:
13383     case RESULT_DECL:
13384       /* If we are in terse mode, don't generate any DIEs to represent any
13385          variable declarations or definitions.  */
13386       if (debug_info_level <= DINFO_LEVEL_TERSE)
13387         break;
13388
13389       /* Output any DIEs that are needed to specify the type of this data
13390          object.  */
13391       gen_type_die (TREE_TYPE (decl), context_die);
13392
13393       /* And its containing type.  */
13394       origin = decl_class_context (decl);
13395       if (origin != NULL_TREE)
13396         gen_type_die_for_member (origin, decl, context_die);
13397
13398       /* And its containing namespace.  */
13399       declare_in_namespace (decl, context_die);
13400
13401       /* Now output the DIE to represent the data object itself.  This gets
13402          complicated because of the possibility that the VAR_DECL really
13403          represents an inlined instance of a formal parameter for an inline
13404          function.  */
13405       origin = decl_ultimate_origin (decl);
13406       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13407         gen_formal_parameter_die (decl, context_die);
13408       else
13409         gen_variable_die (decl, context_die);
13410       break;
13411
13412     case FIELD_DECL:
13413       /* Ignore the nameless fields that are used to skip bits but handle C++
13414          anonymous unions and structs.  */
13415       if (DECL_NAME (decl) != NULL_TREE
13416           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13417           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13418         {
13419           gen_type_die (member_declared_type (decl), context_die);
13420           gen_field_die (decl, context_die);
13421         }
13422       break;
13423
13424     case PARM_DECL:
13425       gen_type_die (TREE_TYPE (decl), context_die);
13426       gen_formal_parameter_die (decl, context_die);
13427       break;
13428
13429     case NAMESPACE_DECL:
13430       gen_namespace_die (decl);
13431       break;
13432
13433     default:
13434       /* Probably some frontend-internal decl.  Assume we don't care.  */
13435       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13436       break;
13437     }
13438 }
13439 \f
13440 /* Output debug information for global decl DECL.  Called from toplev.c after
13441    compilation proper has finished.  */
13442
13443 static void
13444 dwarf2out_global_decl (tree decl)
13445 {
13446   /* Output DWARF2 information for file-scope tentative data object
13447      declarations, file-scope (extern) function declarations (which had no
13448      corresponding body) and file-scope tagged type declarations and
13449      definitions which have not yet been forced out.  */
13450   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13451     dwarf2out_decl (decl);
13452 }
13453
13454 /* Output debug information for type decl DECL.  Called from toplev.c
13455    and from language front ends (to record built-in types).  */
13456 static void
13457 dwarf2out_type_decl (tree decl, int local)
13458 {
13459   if (!local)
13460     dwarf2out_decl (decl);
13461 }
13462
13463 /* Output debug information for imported module or decl.  */
13464
13465 static void
13466 dwarf2out_imported_module_or_decl (tree decl, tree context)
13467 {
13468   dw_die_ref imported_die, at_import_die;
13469   dw_die_ref scope_die;
13470   expanded_location xloc;
13471
13472   if (debug_info_level <= DINFO_LEVEL_TERSE)
13473     return;
13474
13475   gcc_assert (decl);
13476
13477   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13478      We need decl DIE for reference and scope die. First, get DIE for the decl
13479      itself.  */
13480
13481   /* Get the scope die for decl context. Use comp_unit_die for global module
13482      or decl. If die is not found for non globals, force new die.  */
13483   if (!context)
13484     scope_die = comp_unit_die;
13485   else if (TYPE_P (context))
13486     scope_die = force_type_die (context);
13487   else
13488     scope_die = force_decl_die (context);
13489
13490   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13491   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13492     {
13493       if (is_base_type (TREE_TYPE (decl)))
13494         at_import_die = base_type_die (TREE_TYPE (decl));
13495       else
13496         at_import_die = force_type_die (TREE_TYPE (decl));
13497     }
13498   else
13499     {
13500       at_import_die = lookup_decl_die (decl);
13501       if (!at_import_die)
13502         {
13503           /* If we're trying to avoid duplicate debug info, we may not have
13504              emitted the member decl for this field.  Emit it now.  */
13505           if (TREE_CODE (decl) == FIELD_DECL)
13506             {
13507               tree type = DECL_CONTEXT (decl);
13508               dw_die_ref type_context_die;
13509
13510               if (TYPE_CONTEXT (type))
13511                 if (TYPE_P (TYPE_CONTEXT (type)))
13512                   type_context_die = force_type_die (TYPE_CONTEXT (type));
13513               else
13514                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13515               else
13516                 type_context_die = comp_unit_die;
13517               gen_type_die_for_member (type, decl, type_context_die);
13518             }
13519           at_import_die = force_decl_die (decl);
13520         }
13521     }
13522
13523   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13524   if (TREE_CODE (decl) == NAMESPACE_DECL)
13525     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13526   else
13527     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13528
13529   xloc = expand_location (input_location);
13530   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13531   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13532   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13533 }
13534
13535 /* Write the debugging output for DECL.  */
13536
13537 void
13538 dwarf2out_decl (tree decl)
13539 {
13540   dw_die_ref context_die = comp_unit_die;
13541
13542   switch (TREE_CODE (decl))
13543     {
13544     case ERROR_MARK:
13545       return;
13546
13547     case FUNCTION_DECL:
13548       /* What we would really like to do here is to filter out all mere
13549          file-scope declarations of file-scope functions which are never
13550          referenced later within this translation unit (and keep all of ones
13551          that *are* referenced later on) but we aren't clairvoyant, so we have
13552          no idea which functions will be referenced in the future (i.e. later
13553          on within the current translation unit). So here we just ignore all
13554          file-scope function declarations which are not also definitions.  If
13555          and when the debugger needs to know something about these functions,
13556          it will have to hunt around and find the DWARF information associated
13557          with the definition of the function.
13558
13559          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13560          nodes represent definitions and which ones represent mere
13561          declarations.  We have to check DECL_INITIAL instead. That's because
13562          the C front-end supports some weird semantics for "extern inline"
13563          function definitions.  These can get inlined within the current
13564          translation unit (and thus, we need to generate Dwarf info for their
13565          abstract instances so that the Dwarf info for the concrete inlined
13566          instances can have something to refer to) but the compiler never
13567          generates any out-of-lines instances of such things (despite the fact
13568          that they *are* definitions).
13569
13570          The important point is that the C front-end marks these "extern
13571          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13572          them anyway. Note that the C++ front-end also plays some similar games
13573          for inline function definitions appearing within include files which
13574          also contain `#pragma interface' pragmas.  */
13575       if (DECL_INITIAL (decl) == NULL_TREE)
13576         return;
13577
13578       /* If we're a nested function, initially use a parent of NULL; if we're
13579          a plain function, this will be fixed up in decls_for_scope.  If
13580          we're a method, it will be ignored, since we already have a DIE.  */
13581       if (decl_function_context (decl)
13582           /* But if we're in terse mode, we don't care about scope.  */
13583           && debug_info_level > DINFO_LEVEL_TERSE)
13584         context_die = NULL;
13585       break;
13586
13587     case VAR_DECL:
13588       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13589          declaration and if the declaration was never even referenced from
13590          within this entire compilation unit.  We suppress these DIEs in
13591          order to save space in the .debug section (by eliminating entries
13592          which are probably useless).  Note that we must not suppress
13593          block-local extern declarations (whether used or not) because that
13594          would screw-up the debugger's name lookup mechanism and cause it to
13595          miss things which really ought to be in scope at a given point.  */
13596       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13597         return;
13598
13599       /* For local statics lookup proper context die.  */
13600       if (TREE_STATIC (decl) && decl_function_context (decl))
13601         context_die = lookup_decl_die (DECL_CONTEXT (decl));
13602
13603       /* If we are in terse mode, don't generate any DIEs to represent any
13604          variable declarations or definitions.  */
13605       if (debug_info_level <= DINFO_LEVEL_TERSE)
13606         return;
13607       break;
13608
13609     case NAMESPACE_DECL:
13610       if (debug_info_level <= DINFO_LEVEL_TERSE)
13611         return;
13612       if (lookup_decl_die (decl) != NULL)
13613         return;
13614       break;
13615
13616     case TYPE_DECL:
13617       /* Don't emit stubs for types unless they are needed by other DIEs.  */
13618       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13619         return;
13620
13621       /* Don't bother trying to generate any DIEs to represent any of the
13622          normal built-in types for the language we are compiling.  */
13623       if (DECL_IS_BUILTIN (decl))
13624         {
13625           /* OK, we need to generate one for `bool' so GDB knows what type
13626              comparisons have.  */
13627           if (is_cxx ()
13628               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13629               && ! DECL_IGNORED_P (decl))
13630             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13631
13632           return;
13633         }
13634
13635       /* If we are in terse mode, don't generate any DIEs for types.  */
13636       if (debug_info_level <= DINFO_LEVEL_TERSE)
13637         return;
13638
13639       /* If we're a function-scope tag, initially use a parent of NULL;
13640          this will be fixed up in decls_for_scope.  */
13641       if (decl_function_context (decl))
13642         context_die = NULL;
13643
13644       break;
13645
13646     default:
13647       return;
13648     }
13649
13650   gen_decl_die (decl, context_die);
13651 }
13652
13653 /* Output a marker (i.e. a label) for the beginning of the generated code for
13654    a lexical block.  */
13655
13656 static void
13657 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13658                        unsigned int blocknum)
13659 {
13660   switch_to_section (current_function_section ());
13661   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13662 }
13663
13664 /* Output a marker (i.e. a label) for the end of the generated code for a
13665    lexical block.  */
13666
13667 static void
13668 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13669 {
13670   switch_to_section (current_function_section ());
13671   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13672 }
13673
13674 /* Returns nonzero if it is appropriate not to emit any debugging
13675    information for BLOCK, because it doesn't contain any instructions.
13676
13677    Don't allow this for blocks with nested functions or local classes
13678    as we would end up with orphans, and in the presence of scheduling
13679    we may end up calling them anyway.  */
13680
13681 static bool
13682 dwarf2out_ignore_block (tree block)
13683 {
13684   tree decl;
13685
13686   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13687     if (TREE_CODE (decl) == FUNCTION_DECL
13688         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13689       return 0;
13690
13691   return 1;
13692 }
13693
13694 /* Hash table routines for file_hash.  */
13695
13696 static int
13697 file_table_eq (const void *p1_p, const void *p2_p)
13698 {
13699   const struct dwarf_file_data * p1 = p1_p;
13700   const char * p2 = p2_p;
13701   return strcmp (p1->filename, p2) == 0;
13702 }
13703
13704 static hashval_t
13705 file_table_hash (const void *p_p)
13706 {
13707   const struct dwarf_file_data * p = p_p;
13708   return htab_hash_string (p->filename);
13709 }
13710
13711 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13712    dwarf2out.c) and return its "index".  The index of each (known) filename is
13713    just a unique number which is associated with only that one filename.  We
13714    need such numbers for the sake of generating labels (in the .debug_sfnames
13715    section) and references to those files numbers (in the .debug_srcinfo
13716    and.debug_macinfo sections).  If the filename given as an argument is not
13717    found in our current list, add it to the list and assign it the next
13718    available unique index number.  In order to speed up searches, we remember
13719    the index of the filename was looked up last.  This handles the majority of
13720    all searches.  */
13721
13722 static struct dwarf_file_data *
13723 lookup_filename (const char *file_name)
13724 {
13725   void ** slot;
13726   struct dwarf_file_data * created;
13727
13728   /* Check to see if the file name that was searched on the previous
13729      call matches this file name.  If so, return the index.  */
13730   if (file_table_last_lookup
13731       && (file_name == file_table_last_lookup->filename
13732           || strcmp (file_table_last_lookup->filename, file_name) == 0))
13733     return file_table_last_lookup;
13734
13735   /* Didn't match the previous lookup, search the table.  */
13736   slot = htab_find_slot_with_hash (file_table, file_name,
13737                                    htab_hash_string (file_name), INSERT);
13738   if (*slot)
13739     return *slot;
13740
13741   created = ggc_alloc (sizeof (struct dwarf_file_data));
13742   created->filename = file_name;
13743   created->emitted_number = 0;
13744   *slot = created;
13745   return created;
13746 }
13747
13748 /* If the assembler will construct the file table, then translate the compiler
13749    internal file table number into the assembler file table number, and emit
13750    a .file directive if we haven't already emitted one yet.  The file table
13751    numbers are different because we prune debug info for unused variables and
13752    types, which may include filenames.  */
13753
13754 static int
13755 maybe_emit_file (struct dwarf_file_data * fd)
13756 {
13757   if (! fd->emitted_number)
13758     {
13759       if (last_emitted_file)
13760         fd->emitted_number = last_emitted_file->emitted_number + 1;
13761       else
13762         fd->emitted_number = 1;
13763       last_emitted_file = fd;
13764       
13765       if (DWARF2_ASM_LINE_DEBUG_INFO)
13766         {
13767           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13768           output_quoted_string (asm_out_file, fd->filename);
13769           fputc ('\n', asm_out_file);
13770         }
13771     }
13772   
13773   return fd->emitted_number;
13774 }
13775
13776 /* Called by the final INSN scan whenever we see a var location.  We
13777    use it to drop labels in the right places, and throw the location in
13778    our lookup table.  */
13779
13780 static void
13781 dwarf2out_var_location (rtx loc_note)
13782 {
13783   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13784   struct var_loc_node *newloc;
13785   rtx prev_insn;
13786   static rtx last_insn;
13787   static const char *last_label;
13788   tree decl;
13789
13790   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13791     return;
13792   prev_insn = PREV_INSN (loc_note);
13793
13794   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13795   /* If the insn we processed last time is the previous insn
13796      and it is also a var location note, use the label we emitted
13797      last time.  */
13798   if (last_insn != NULL_RTX
13799       && last_insn == prev_insn
13800       && NOTE_P (prev_insn)
13801       && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13802     {
13803       newloc->label = last_label;
13804     }
13805   else
13806     {
13807       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13808       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13809       loclabel_num++;
13810       newloc->label = ggc_strdup (loclabel);
13811     }
13812   newloc->var_loc_note = loc_note;
13813   newloc->next = NULL;
13814
13815   if (cfun && in_cold_section_p)
13816     newloc->section_label = cfun->cold_section_label;
13817   else
13818     newloc->section_label = text_section_label;
13819
13820   last_insn = loc_note;
13821   last_label = newloc->label;
13822   decl = NOTE_VAR_LOCATION_DECL (loc_note);
13823   add_var_loc_to_decl (decl, newloc);
13824 }
13825
13826 /* We need to reset the locations at the beginning of each
13827    function. We can't do this in the end_function hook, because the
13828    declarations that use the locations won't have been output when
13829    that hook is called.  Also compute have_multiple_function_sections here.  */
13830
13831 static void
13832 dwarf2out_begin_function (tree fun)
13833 {
13834   htab_empty (decl_loc_table);
13835   
13836   if (function_section (fun) != text_section)
13837     have_multiple_function_sections = true;
13838 }
13839
13840 /* Output a label to mark the beginning of a source code line entry
13841    and record information relating to this source line, in
13842    'line_info_table' for later output of the .debug_line section.  */
13843
13844 static void
13845 dwarf2out_source_line (unsigned int line, const char *filename)
13846 {
13847   if (debug_info_level >= DINFO_LEVEL_NORMAL
13848       && line != 0)
13849     {
13850       int file_num = maybe_emit_file (lookup_filename (filename));
13851       
13852       switch_to_section (current_function_section ());
13853
13854       /* If requested, emit something human-readable.  */
13855       if (flag_debug_asm)
13856         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13857                  filename, line);
13858
13859       if (DWARF2_ASM_LINE_DEBUG_INFO)
13860         {
13861           /* Emit the .loc directive understood by GNU as.  */
13862           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13863
13864           /* Indicate that line number info exists.  */
13865           line_info_table_in_use++;
13866         }
13867       else if (function_section (current_function_decl) != text_section)
13868         {
13869           dw_separate_line_info_ref line_info;
13870           targetm.asm_out.internal_label (asm_out_file, 
13871                                           SEPARATE_LINE_CODE_LABEL,
13872                                           separate_line_info_table_in_use);
13873
13874           /* Expand the line info table if necessary.  */
13875           if (separate_line_info_table_in_use
13876               == separate_line_info_table_allocated)
13877             {
13878               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13879               separate_line_info_table
13880                 = ggc_realloc (separate_line_info_table,
13881                                separate_line_info_table_allocated
13882                                * sizeof (dw_separate_line_info_entry));
13883               memset (separate_line_info_table
13884                        + separate_line_info_table_in_use,
13885                       0,
13886                       (LINE_INFO_TABLE_INCREMENT
13887                        * sizeof (dw_separate_line_info_entry)));
13888             }
13889
13890           /* Add the new entry at the end of the line_info_table.  */
13891           line_info
13892             = &separate_line_info_table[separate_line_info_table_in_use++];
13893           line_info->dw_file_num = file_num;
13894           line_info->dw_line_num = line;
13895           line_info->function = current_function_funcdef_no;
13896         }
13897       else
13898         {
13899           dw_line_info_ref line_info;
13900
13901           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13902                                      line_info_table_in_use);
13903
13904           /* Expand the line info table if necessary.  */
13905           if (line_info_table_in_use == line_info_table_allocated)
13906             {
13907               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13908               line_info_table
13909                 = ggc_realloc (line_info_table,
13910                                (line_info_table_allocated
13911                                 * sizeof (dw_line_info_entry)));
13912               memset (line_info_table + line_info_table_in_use, 0,
13913                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13914             }
13915
13916           /* Add the new entry at the end of the line_info_table.  */
13917           line_info = &line_info_table[line_info_table_in_use++];
13918           line_info->dw_file_num = file_num;
13919           line_info->dw_line_num = line;
13920         }
13921     }
13922 }
13923
13924 /* Record the beginning of a new source file.  */
13925
13926 static void
13927 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13928 {
13929   if (flag_eliminate_dwarf2_dups)
13930     {
13931       /* Record the beginning of the file for break_out_includes.  */
13932       dw_die_ref bincl_die;
13933
13934       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13935       add_AT_string (bincl_die, DW_AT_name, filename);
13936     }
13937
13938   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13939     {
13940       int file_num = maybe_emit_file (lookup_filename (filename));
13941
13942       switch_to_section (debug_macinfo_section);
13943       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13944       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13945                                    lineno);
13946
13947       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
13948     }
13949 }
13950
13951 /* Record the end of a source file.  */
13952
13953 static void
13954 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13955 {
13956   if (flag_eliminate_dwarf2_dups)
13957     /* Record the end of the file for break_out_includes.  */
13958     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13959
13960   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13961     {
13962       switch_to_section (debug_macinfo_section);
13963       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13964     }
13965 }
13966
13967 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
13968    the tail part of the directive line, i.e. the part which is past the
13969    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13970
13971 static void
13972 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13973                   const char *buffer ATTRIBUTE_UNUSED)
13974 {
13975   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13976     {
13977       switch_to_section (debug_macinfo_section);
13978       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13979       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13980       dw2_asm_output_nstring (buffer, -1, "The macro");
13981     }
13982 }
13983
13984 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
13985    the tail part of the directive line, i.e. the part which is past the
13986    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13987
13988 static void
13989 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13990                  const char *buffer ATTRIBUTE_UNUSED)
13991 {
13992   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13993     {
13994       switch_to_section (debug_macinfo_section);
13995       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13996       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13997       dw2_asm_output_nstring (buffer, -1, "The macro");
13998     }
13999 }
14000
14001 /* Set up for Dwarf output at the start of compilation.  */
14002
14003 static void
14004 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14005 {
14006   /* Allocate the file_table.  */
14007   file_table = htab_create_ggc (50, file_table_hash,
14008                                 file_table_eq, NULL);
14009
14010   /* Allocate the decl_die_table.  */
14011   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14012                                     decl_die_table_eq, NULL);
14013
14014   /* Allocate the decl_loc_table.  */
14015   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14016                                     decl_loc_table_eq, NULL);
14017
14018   /* Allocate the initial hunk of the decl_scope_table.  */
14019   decl_scope_table = VEC_alloc (tree, gc, 256);
14020
14021   /* Allocate the initial hunk of the abbrev_die_table.  */
14022   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14023                                         * sizeof (dw_die_ref));
14024   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14025   /* Zero-th entry is allocated, but unused.  */
14026   abbrev_die_table_in_use = 1;
14027
14028   /* Allocate the initial hunk of the line_info_table.  */
14029   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14030                                        * sizeof (dw_line_info_entry));
14031   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14032
14033   /* Zero-th entry is allocated, but unused.  */
14034   line_info_table_in_use = 1;
14035
14036   /* Allocate the pubtypes and pubnames vectors.  */
14037   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14038   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14039
14040   /* Generate the initial DIE for the .debug section.  Note that the (string)
14041      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14042      will (typically) be a relative pathname and that this pathname should be
14043      taken as being relative to the directory from which the compiler was
14044      invoked when the given (base) source file was compiled.  We will fill
14045      in this value in dwarf2out_finish.  */
14046   comp_unit_die = gen_compile_unit_die (NULL);
14047
14048   incomplete_types = VEC_alloc (tree, gc, 64);
14049
14050   used_rtx_array = VEC_alloc (rtx, gc, 32);
14051
14052   debug_info_section = get_section (DEBUG_INFO_SECTION,
14053                                     SECTION_DEBUG, NULL);
14054   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14055                                       SECTION_DEBUG, NULL);
14056   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14057                                        SECTION_DEBUG, NULL);
14058   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14059                                        SECTION_DEBUG, NULL);
14060   debug_line_section = get_section (DEBUG_LINE_SECTION,
14061                                     SECTION_DEBUG, NULL);
14062   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14063                                    SECTION_DEBUG, NULL);
14064   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14065                                         SECTION_DEBUG, NULL);
14066 #ifdef DEBUG_PUBTYPES_SECTION
14067   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14068                                         SECTION_DEBUG, NULL);
14069 #endif
14070   debug_str_section = get_section (DEBUG_STR_SECTION,
14071                                    DEBUG_STR_SECTION_FLAGS, NULL);
14072   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14073                                       SECTION_DEBUG, NULL);
14074   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14075                                      SECTION_DEBUG, NULL);
14076
14077   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14078   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14079                                DEBUG_ABBREV_SECTION_LABEL, 0);
14080   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14081   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label, 
14082                                COLD_TEXT_SECTION_LABEL, 0);
14083   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14084
14085   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14086                                DEBUG_INFO_SECTION_LABEL, 0);
14087   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14088                                DEBUG_LINE_SECTION_LABEL, 0);
14089   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14090                                DEBUG_RANGES_SECTION_LABEL, 0);
14091   switch_to_section (debug_abbrev_section);
14092   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14093   switch_to_section (debug_info_section);
14094   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14095   switch_to_section (debug_line_section);
14096   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14097
14098   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14099     {
14100       switch_to_section (debug_macinfo_section);
14101       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14102                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14103       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14104     }
14105
14106   switch_to_section (text_section);
14107   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14108   if (flag_reorder_blocks_and_partition)
14109     {
14110       switch_to_section (unlikely_text_section ());
14111       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14112     }
14113 }
14114
14115 /* A helper function for dwarf2out_finish called through
14116    ht_forall.  Emit one queued .debug_str string.  */
14117
14118 static int
14119 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14120 {
14121   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14122
14123   if (node->form == DW_FORM_strp)
14124     {
14125       switch_to_section (debug_str_section);
14126       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14127       assemble_string (node->str, strlen (node->str) + 1);
14128     }
14129
14130   return 1;
14131 }
14132
14133 #if ENABLE_ASSERT_CHECKING
14134 /* Verify that all marks are clear.  */
14135
14136 static void
14137 verify_marks_clear (dw_die_ref die)
14138 {
14139   dw_die_ref c;
14140   
14141   gcc_assert (! die->die_mark);
14142   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14143 }
14144 #endif /* ENABLE_ASSERT_CHECKING */
14145
14146 /* Clear the marks for a die and its children.
14147    Be cool if the mark isn't set.  */
14148
14149 static void
14150 prune_unmark_dies (dw_die_ref die)
14151 {
14152   dw_die_ref c;
14153   
14154   if (die->die_mark)
14155     die->die_mark = 0;
14156   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14157 }
14158
14159 /* Given DIE that we're marking as used, find any other dies
14160    it references as attributes and mark them as used.  */
14161
14162 static void
14163 prune_unused_types_walk_attribs (dw_die_ref die)
14164 {
14165   dw_attr_ref a;
14166   unsigned ix;
14167
14168   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14169     {
14170       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14171         {
14172           /* A reference to another DIE.
14173              Make sure that it will get emitted.  */
14174           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14175         }
14176       /* Set the string's refcount to 0 so that prune_unused_types_mark
14177          accounts properly for it.  */
14178       if (AT_class (a) == dw_val_class_str)
14179         a->dw_attr_val.v.val_str->refcount = 0;
14180     }
14181 }
14182
14183
14184 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14185    to DIE's children.  */
14186
14187 static void
14188 prune_unused_types_mark (dw_die_ref die, int dokids)
14189 {
14190   dw_die_ref c;
14191
14192   if (die->die_mark == 0)
14193     {
14194       /* We haven't done this node yet.  Mark it as used.  */
14195       die->die_mark = 1;
14196
14197       /* We also have to mark its parents as used.
14198          (But we don't want to mark our parents' kids due to this.)  */
14199       if (die->die_parent)
14200         prune_unused_types_mark (die->die_parent, 0);
14201
14202       /* Mark any referenced nodes.  */
14203       prune_unused_types_walk_attribs (die);
14204
14205       /* If this node is a specification,
14206          also mark the definition, if it exists.  */
14207       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14208         prune_unused_types_mark (die->die_definition, 1);
14209     }
14210
14211   if (dokids && die->die_mark != 2)
14212     {
14213       /* We need to walk the children, but haven't done so yet.
14214          Remember that we've walked the kids.  */
14215       die->die_mark = 2;
14216
14217       /* If this is an array type, we need to make sure our
14218          kids get marked, even if they're types.  */
14219       if (die->die_tag == DW_TAG_array_type)
14220         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14221       else
14222         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14223     }
14224 }
14225
14226
14227 /* Walk the tree DIE and mark types that we actually use.  */
14228
14229 static void
14230 prune_unused_types_walk (dw_die_ref die)
14231 {
14232   dw_die_ref c;
14233
14234   /* Don't do anything if this node is already marked.  */
14235   if (die->die_mark)
14236     return;
14237
14238   switch (die->die_tag)
14239     {
14240     case DW_TAG_const_type:
14241     case DW_TAG_packed_type:
14242     case DW_TAG_pointer_type:
14243     case DW_TAG_reference_type:
14244     case DW_TAG_volatile_type:
14245     case DW_TAG_typedef:
14246     case DW_TAG_array_type:
14247     case DW_TAG_structure_type:
14248     case DW_TAG_union_type:
14249     case DW_TAG_class_type:
14250     case DW_TAG_friend:
14251     case DW_TAG_variant_part:
14252     case DW_TAG_enumeration_type:
14253     case DW_TAG_subroutine_type:
14254     case DW_TAG_string_type:
14255     case DW_TAG_set_type:
14256     case DW_TAG_subrange_type:
14257     case DW_TAG_ptr_to_member_type:
14258     case DW_TAG_file_type:
14259       if (die->die_perennial_p)
14260         break;
14261
14262       /* It's a type node --- don't mark it.  */
14263       return;
14264
14265     default:
14266       /* Mark everything else.  */
14267       break;
14268   }
14269
14270   die->die_mark = 1;
14271
14272   /* Now, mark any dies referenced from here.  */
14273   prune_unused_types_walk_attribs (die);
14274
14275   /* Mark children.  */
14276   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14277 }
14278
14279 /* Increment the string counts on strings referred to from DIE's
14280    attributes.  */
14281
14282 static void
14283 prune_unused_types_update_strings (dw_die_ref die)
14284 {
14285   dw_attr_ref a;
14286   unsigned ix;
14287
14288   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14289     if (AT_class (a) == dw_val_class_str)
14290       {
14291         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14292         s->refcount++;
14293         /* Avoid unnecessarily putting strings that are used less than
14294            twice in the hash table.  */
14295         if (s->refcount
14296             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14297           {
14298             void ** slot;
14299             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14300                                              htab_hash_string (s->str),
14301                                              INSERT);
14302             gcc_assert (*slot == NULL);
14303             *slot = s;
14304           }
14305       }
14306 }
14307
14308 /* Remove from the tree DIE any dies that aren't marked.  */
14309
14310 static void
14311 prune_unused_types_prune (dw_die_ref die)
14312 {
14313   dw_die_ref c;
14314
14315   gcc_assert (die->die_mark);
14316   prune_unused_types_update_strings (die);
14317
14318   if (! die->die_child)
14319     return;
14320   
14321   c = die->die_child;
14322   do {
14323     dw_die_ref prev = c;
14324     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14325       if (c == die->die_child)
14326         {
14327           /* No marked children between 'prev' and the end of the list.  */
14328           if (prev == c)
14329             /* No marked children at all.  */
14330             die->die_child = NULL;
14331           else
14332             {
14333               prev->die_sib = c->die_sib;
14334               die->die_child = prev;
14335             }
14336           return;
14337         }
14338
14339     if (c != prev->die_sib)
14340       prev->die_sib = c;
14341     prune_unused_types_prune (c);
14342   } while (c != die->die_child);
14343 }
14344
14345
14346 /* Remove dies representing declarations that we never use.  */
14347
14348 static void
14349 prune_unused_types (void)
14350 {
14351   unsigned int i;
14352   limbo_die_node *node;
14353   pubname_ref pub;
14354
14355 #if ENABLE_ASSERT_CHECKING
14356   /* All the marks should already be clear.  */
14357   verify_marks_clear (comp_unit_die);
14358   for (node = limbo_die_list; node; node = node->next)
14359     verify_marks_clear (node->die);
14360 #endif /* ENABLE_ASSERT_CHECKING */
14361
14362   /* Set the mark on nodes that are actually used.  */
14363   prune_unused_types_walk (comp_unit_die);
14364   for (node = limbo_die_list; node; node = node->next)
14365     prune_unused_types_walk (node->die);
14366
14367   /* Also set the mark on nodes referenced from the
14368      pubname_table or arange_table.  */
14369   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14370     prune_unused_types_mark (pub->die, 1);
14371   for (i = 0; i < arange_table_in_use; i++)
14372     prune_unused_types_mark (arange_table[i], 1);
14373
14374   /* Get rid of nodes that aren't marked; and update the string counts.  */
14375   if (debug_str_hash)
14376     htab_empty (debug_str_hash);
14377   prune_unused_types_prune (comp_unit_die);
14378   for (node = limbo_die_list; node; node = node->next)
14379     prune_unused_types_prune (node->die);
14380
14381   /* Leave the marks clear.  */
14382   prune_unmark_dies (comp_unit_die);
14383   for (node = limbo_die_list; node; node = node->next)
14384     prune_unmark_dies (node->die);
14385 }
14386
14387 /* Set the parameter to true if there are any relative pathnames in
14388    the file table.  */
14389 static int
14390 file_table_relative_p (void ** slot, void *param)
14391 {
14392   bool *p = param;
14393   struct dwarf_file_data *d = *slot;
14394   if (d->emitted_number && !IS_ABSOLUTE_PATH (d->filename))
14395     {
14396       *p = true;
14397       return 0;
14398     }
14399   return 1;
14400 }
14401
14402 /* Output stuff that dwarf requires at the end of every file,
14403    and generate the DWARF-2 debugging info.  */
14404
14405 static void
14406 dwarf2out_finish (const char *filename)
14407 {
14408   limbo_die_node *node, *next_node;
14409   dw_die_ref die = 0;
14410
14411   /* Add the name for the main input file now.  We delayed this from
14412      dwarf2out_init to avoid complications with PCH.  */
14413   add_name_attribute (comp_unit_die, filename);
14414   if (!IS_ABSOLUTE_PATH (filename))
14415     add_comp_dir_attribute (comp_unit_die);
14416   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14417     {
14418       bool p = false;
14419       htab_traverse (file_table, file_table_relative_p, &p);
14420       if (p)
14421         add_comp_dir_attribute (comp_unit_die);
14422     }
14423
14424   /* Traverse the limbo die list, and add parent/child links.  The only
14425      dies without parents that should be here are concrete instances of
14426      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
14427      For concrete instances, we can get the parent die from the abstract
14428      instance.  */
14429   for (node = limbo_die_list; node; node = next_node)
14430     {
14431       next_node = node->next;
14432       die = node->die;
14433
14434       if (die->die_parent == NULL)
14435         {
14436           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14437
14438           if (origin)
14439             add_child_die (origin->die_parent, die);
14440           else if (die == comp_unit_die)
14441             ;
14442           else if (errorcount > 0 || sorrycount > 0)
14443             /* It's OK to be confused by errors in the input.  */
14444             add_child_die (comp_unit_die, die);
14445           else
14446             {
14447               /* In certain situations, the lexical block containing a
14448                  nested function can be optimized away, which results
14449                  in the nested function die being orphaned.  Likewise
14450                  with the return type of that nested function.  Force
14451                  this to be a child of the containing function.
14452
14453                  It may happen that even the containing function got fully
14454                  inlined and optimized out.  In that case we are lost and
14455                  assign the empty child.  This should not be big issue as
14456                  the function is likely unreachable too.  */
14457               tree context = NULL_TREE;
14458
14459               gcc_assert (node->created_for);
14460
14461               if (DECL_P (node->created_for))
14462                 context = DECL_CONTEXT (node->created_for);
14463               else if (TYPE_P (node->created_for))
14464                 context = TYPE_CONTEXT (node->created_for);
14465
14466               gcc_assert (context
14467                           && (TREE_CODE (context) == FUNCTION_DECL
14468                               || TREE_CODE (context) == NAMESPACE_DECL));
14469
14470               origin = lookup_decl_die (context);
14471               if (origin)
14472                 add_child_die (origin, die);
14473               else
14474                 add_child_die (comp_unit_die, die);
14475             }
14476         }
14477     }
14478
14479   limbo_die_list = NULL;
14480
14481   /* Walk through the list of incomplete types again, trying once more to
14482      emit full debugging info for them.  */
14483   retry_incomplete_types ();
14484
14485   if (flag_eliminate_unused_debug_types)
14486     prune_unused_types ();
14487
14488   /* Generate separate CUs for each of the include files we've seen.
14489      They will go into limbo_die_list.  */
14490   if (flag_eliminate_dwarf2_dups)
14491     break_out_includes (comp_unit_die);
14492
14493   /* Traverse the DIE's and add add sibling attributes to those DIE's
14494      that have children.  */
14495   add_sibling_attributes (comp_unit_die);
14496   for (node = limbo_die_list; node; node = node->next)
14497     add_sibling_attributes (node->die);
14498
14499   /* Output a terminator label for the .text section.  */
14500   switch_to_section (text_section);
14501   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14502   if (flag_reorder_blocks_and_partition)
14503     {
14504       switch_to_section (unlikely_text_section ());
14505       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14506     }
14507
14508   /* We can only use the low/high_pc attributes if all of the code was
14509      in .text.  */
14510   if (!have_multiple_function_sections)
14511     {
14512       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14513       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14514     }
14515
14516   /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14517      "base address".  Use zero so that these addresses become absolute.  */
14518   else if (have_location_lists || ranges_table_in_use)
14519     add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14520
14521   /* Output location list section if necessary.  */
14522   if (have_location_lists)
14523     {
14524       /* Output the location lists info.  */
14525       switch_to_section (debug_loc_section);
14526       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14527                                    DEBUG_LOC_SECTION_LABEL, 0);
14528       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14529       output_location_lists (die);
14530     }
14531
14532   if (debug_info_level >= DINFO_LEVEL_NORMAL)
14533     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14534                     debug_line_section_label);
14535
14536   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14537     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14538
14539   /* Output all of the compilation units.  We put the main one last so that
14540      the offsets are available to output_pubnames.  */
14541   for (node = limbo_die_list; node; node = node->next)
14542     output_comp_unit (node->die, 0);
14543
14544   output_comp_unit (comp_unit_die, 0);
14545
14546   /* Output the abbreviation table.  */
14547   switch_to_section (debug_abbrev_section);
14548   output_abbrev_section ();
14549
14550   /* Output public names table if necessary.  */
14551   if (!VEC_empty (pubname_entry, pubname_table))
14552     {
14553       switch_to_section (debug_pubnames_section);
14554       output_pubnames (pubname_table);
14555     }
14556
14557 #ifdef DEBUG_PUBTYPES_SECTION
14558   /* Output public types table if necessary.  */
14559   if (!VEC_empty (pubname_entry, pubtype_table))
14560     {
14561       switch_to_section (debug_pubtypes_section);
14562       output_pubnames (pubtype_table);
14563     }
14564 #endif
14565   
14566   /* Output the address range information.  We only put functions in the arange
14567      table, so don't write it out if we don't have any.  */
14568   if (fde_table_in_use)
14569     {
14570       switch_to_section (debug_aranges_section);
14571       output_aranges ();
14572     }
14573
14574   /* Output ranges section if necessary.  */
14575   if (ranges_table_in_use)
14576     {
14577       switch_to_section (debug_ranges_section);
14578       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14579       output_ranges ();
14580     }
14581
14582   /* Output the source line correspondence table.  We must do this
14583      even if there is no line information.  Otherwise, on an empty
14584      translation unit, we will generate a present, but empty,
14585      .debug_info section.  IRIX 6.5 `nm' will then complain when
14586      examining the file.  This is done late so that any filenames
14587      used by the debug_info section are marked as 'used'.  */
14588   if (! DWARF2_ASM_LINE_DEBUG_INFO)
14589     {
14590       switch_to_section (debug_line_section);
14591       output_line_info ();
14592     }
14593
14594   /* Have to end the macro section.  */
14595   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14596     {
14597       switch_to_section (debug_macinfo_section);
14598       dw2_asm_output_data (1, 0, "End compilation unit");
14599     }
14600
14601   /* If we emitted any DW_FORM_strp form attribute, output the string
14602      table too.  */
14603   if (debug_str_hash)
14604     htab_traverse (debug_str_hash, output_indirect_string, NULL);
14605 }
14606 #else
14607
14608 /* This should never be used, but its address is needed for comparisons.  */
14609 const struct gcc_debug_hooks dwarf2_debug_hooks;
14610
14611 #endif /* DWARF2_DEBUGGING_INFO */
14612
14613 #include "gt-dwarf2out.h"