OSDN Git Service

* doc/tm.texi (SDB and DWARF): Add extra parameter to
[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 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_str_section;
160 static GTY(()) section *debug_ranges_section;
161 static GTY(()) section *debug_frame_section;
162
163 /* How to start an assembler comment.  */
164 #ifndef ASM_COMMENT_START
165 #define ASM_COMMENT_START ";#"
166 #endif
167
168 typedef struct dw_cfi_struct *dw_cfi_ref;
169 typedef struct dw_fde_struct *dw_fde_ref;
170 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
171
172 /* Call frames are described using a sequence of Call Frame
173    Information instructions.  The register number, offset
174    and address fields are provided as possible operands;
175    their use is selected by the opcode field.  */
176
177 enum dw_cfi_oprnd_type {
178   dw_cfi_oprnd_unused,
179   dw_cfi_oprnd_reg_num,
180   dw_cfi_oprnd_offset,
181   dw_cfi_oprnd_addr,
182   dw_cfi_oprnd_loc
183 };
184
185 typedef union dw_cfi_oprnd_struct GTY(())
186 {
187   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
188   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
189   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
190   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
191 }
192 dw_cfi_oprnd;
193
194 typedef struct dw_cfi_struct GTY(())
195 {
196   dw_cfi_ref dw_cfi_next;
197   enum dwarf_call_frame_info dw_cfi_opc;
198   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
199     dw_cfi_oprnd1;
200   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
201     dw_cfi_oprnd2;
202 }
203 dw_cfi_node;
204
205 /* This is how we define the location of the CFA. We use to handle it
206    as REG + OFFSET all the time,  but now it can be more complex.
207    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
208    Instead of passing around REG and OFFSET, we pass a copy
209    of this structure.  */
210 typedef struct cfa_loc GTY(())
211 {
212   HOST_WIDE_INT offset;
213   HOST_WIDE_INT base_offset;
214   unsigned int reg;
215   int indirect;            /* 1 if CFA is accessed via a dereference.  */
216 } dw_cfa_location;
217
218 /* All call frame descriptions (FDE's) in the GCC generated DWARF
219    refer to a single Common Information Entry (CIE), defined at
220    the beginning of the .debug_frame section.  This use of a single
221    CIE obviates the need to keep track of multiple CIE's
222    in the DWARF generation routines below.  */
223
224 typedef struct dw_fde_struct GTY(())
225 {
226   tree decl;
227   const char *dw_fde_begin;
228   const char *dw_fde_current_label;
229   const char *dw_fde_end;
230   const char *dw_fde_hot_section_label;
231   const char *dw_fde_hot_section_end_label;
232   const char *dw_fde_unlikely_section_label;
233   const char *dw_fde_unlikely_section_end_label;
234   bool dw_fde_switched_sections;
235   dw_cfi_ref dw_fde_cfi;
236   unsigned funcdef_number;
237   unsigned all_throwers_are_sibcalls : 1;
238   unsigned nothrow : 1;
239   unsigned uses_eh_lsda : 1;
240 }
241 dw_fde_node;
242
243 /* Maximum size (in bytes) of an artificially generated label.  */
244 #define MAX_ARTIFICIAL_LABEL_BYTES      30
245
246 /* The size of addresses as they appear in the Dwarf 2 data.
247    Some architectures use word addresses to refer to code locations,
248    but Dwarf 2 info always uses byte addresses.  On such machines,
249    Dwarf 2 addresses need to be larger than the architecture's
250    pointers.  */
251 #ifndef DWARF2_ADDR_SIZE
252 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
253 #endif
254
255 /* The size in bytes of a DWARF field indicating an offset or length
256    relative to a debug info section, specified to be 4 bytes in the
257    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
258    as PTR_SIZE.  */
259
260 #ifndef DWARF_OFFSET_SIZE
261 #define DWARF_OFFSET_SIZE 4
262 #endif
263
264 /* According to the (draft) DWARF 3 specification, the initial length
265    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
266    bytes are 0xffffffff, followed by the length stored in the next 8
267    bytes.
268
269    However, the SGI/MIPS ABI uses an initial length which is equal to
270    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
271
272 #ifndef DWARF_INITIAL_LENGTH_SIZE
273 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
274 #endif
275
276 #define DWARF_VERSION 2
277
278 /* Round SIZE up to the nearest BOUNDARY.  */
279 #define DWARF_ROUND(SIZE,BOUNDARY) \
280   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
281
282 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
283 #ifndef DWARF_CIE_DATA_ALIGNMENT
284 #ifdef STACK_GROWS_DOWNWARD
285 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
286 #else
287 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
288 #endif
289 #endif
290
291 /* A pointer to the base of a table that contains frame description
292    information for each routine.  */
293 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
294
295 /* Number of elements currently allocated for fde_table.  */
296 static GTY(()) unsigned fde_table_allocated;
297
298 /* Number of elements in fde_table currently in use.  */
299 static GTY(()) unsigned fde_table_in_use;
300
301 /* Size (in elements) of increments by which we may expand the
302    fde_table.  */
303 #define FDE_TABLE_INCREMENT 256
304
305 /* A list of call frame insns for the CIE.  */
306 static GTY(()) dw_cfi_ref cie_cfi_head;
307
308 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
309 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
310    attribute that accelerates the lookup of the FDE associated
311    with the subprogram.  This variable holds the table index of the FDE
312    associated with the current function (body) definition.  */
313 static unsigned current_funcdef_fde;
314 #endif
315
316 struct indirect_string_node GTY(())
317 {
318   const char *str;
319   unsigned int refcount;
320   unsigned int form;
321   char *label;
322 };
323
324 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
325
326 static GTY(()) int dw2_string_counter;
327 static GTY(()) unsigned long dwarf2out_cfi_label_num;
328
329 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
330
331 /* Forward declarations for functions defined in this file.  */
332
333 static char *stripattributes (const char *);
334 static const char *dwarf_cfi_name (unsigned);
335 static dw_cfi_ref new_cfi (void);
336 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
337 static void add_fde_cfi (const char *, dw_cfi_ref);
338 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
339 static void lookup_cfa (dw_cfa_location *);
340 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
341 static void initial_return_save (rtx);
342 static HOST_WIDE_INT stack_adjust_offset (rtx);
343 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
344 static void output_call_frame_info (int);
345 static void dwarf2out_stack_adjust (rtx, bool);
346 static void flush_queued_reg_saves (void);
347 static bool clobbers_queued_reg_save (rtx);
348 static void dwarf2out_frame_debug_expr (rtx, const char *);
349
350 /* Support for complex CFA locations.  */
351 static void output_cfa_loc (dw_cfi_ref);
352 static void get_cfa_from_loc_descr (dw_cfa_location *,
353                                     struct dw_loc_descr_struct *);
354 static struct dw_loc_descr_struct *build_cfa_loc
355   (dw_cfa_location *, HOST_WIDE_INT);
356 static void def_cfa_1 (const char *, dw_cfa_location *);
357
358 /* How to start an assembler comment.  */
359 #ifndef ASM_COMMENT_START
360 #define ASM_COMMENT_START ";#"
361 #endif
362
363 /* Data and reference forms for relocatable data.  */
364 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
365 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
366
367 #ifndef DEBUG_FRAME_SECTION
368 #define DEBUG_FRAME_SECTION     ".debug_frame"
369 #endif
370
371 #ifndef FUNC_BEGIN_LABEL
372 #define FUNC_BEGIN_LABEL        "LFB"
373 #endif
374
375 #ifndef FUNC_END_LABEL
376 #define FUNC_END_LABEL          "LFE"
377 #endif
378
379 #ifndef FRAME_BEGIN_LABEL
380 #define FRAME_BEGIN_LABEL       "Lframe"
381 #endif
382 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
383 #define CIE_END_LABEL           "LECIE"
384 #define FDE_LABEL               "LSFDE"
385 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
386 #define FDE_END_LABEL           "LEFDE"
387 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
388 #define LINE_NUMBER_END_LABEL   "LELT"
389 #define LN_PROLOG_AS_LABEL      "LASLTP"
390 #define LN_PROLOG_END_LABEL     "LELTP"
391 #define DIE_LABEL_PREFIX        "DW"
392
393 /* The DWARF 2 CFA column which tracks the return address.  Normally this
394    is the column for PC, or the first column after all of the hard
395    registers.  */
396 #ifndef DWARF_FRAME_RETURN_COLUMN
397 #ifdef PC_REGNUM
398 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
399 #else
400 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
401 #endif
402 #endif
403
404 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
405    default, we just provide columns for all registers.  */
406 #ifndef DWARF_FRAME_REGNUM
407 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
408 #endif
409 \f
410 /* Hook used by __throw.  */
411
412 rtx
413 expand_builtin_dwarf_sp_column (void)
414 {
415   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
416   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
417 }
418
419 /* Return a pointer to a copy of the section string name S with all
420    attributes stripped off, and an asterisk prepended (for assemble_name).  */
421
422 static inline char *
423 stripattributes (const char *s)
424 {
425   char *stripped = XNEWVEC (char, strlen (s) + 2);
426   char *p = stripped;
427
428   *p++ = '*';
429
430   while (*s && *s != ',')
431     *p++ = *s++;
432
433   *p = '\0';
434   return stripped;
435 }
436
437 /* Generate code to initialize the register size table.  */
438
439 void
440 expand_builtin_init_dwarf_reg_sizes (tree address)
441 {
442   unsigned int i;
443   enum machine_mode mode = TYPE_MODE (char_type_node);
444   rtx addr = expand_normal (address);
445   rtx mem = gen_rtx_MEM (BLKmode, addr);
446   bool wrote_return_column = false;
447
448   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
449     {
450       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
451       
452       if (rnum < DWARF_FRAME_REGISTERS)
453         {
454           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
455           enum machine_mode save_mode = reg_raw_mode[i];
456           HOST_WIDE_INT size;
457           
458           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
459             save_mode = choose_hard_reg_mode (i, 1, true);
460           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
461             {
462               if (save_mode == VOIDmode)
463                 continue;
464               wrote_return_column = true;
465             }
466           size = GET_MODE_SIZE (save_mode);
467           if (offset < 0)
468             continue;
469           
470           emit_move_insn (adjust_address (mem, mode, offset),
471                           gen_int_mode (size, mode));
472         }
473     }
474
475 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
476   gcc_assert (wrote_return_column);
477   i = DWARF_ALT_FRAME_RETURN_COLUMN;
478   wrote_return_column = false;
479 #else
480   i = DWARF_FRAME_RETURN_COLUMN;
481 #endif
482
483   if (! wrote_return_column)
484     {
485       enum machine_mode save_mode = Pmode;
486       HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode);
487       HOST_WIDE_INT size = GET_MODE_SIZE (save_mode);
488       emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
489     }
490 }
491
492 /* Convert a DWARF call frame info. operation to its string name */
493
494 static const char *
495 dwarf_cfi_name (unsigned int cfi_opc)
496 {
497   switch (cfi_opc)
498     {
499     case DW_CFA_advance_loc:
500       return "DW_CFA_advance_loc";
501     case DW_CFA_offset:
502       return "DW_CFA_offset";
503     case DW_CFA_restore:
504       return "DW_CFA_restore";
505     case DW_CFA_nop:
506       return "DW_CFA_nop";
507     case DW_CFA_set_loc:
508       return "DW_CFA_set_loc";
509     case DW_CFA_advance_loc1:
510       return "DW_CFA_advance_loc1";
511     case DW_CFA_advance_loc2:
512       return "DW_CFA_advance_loc2";
513     case DW_CFA_advance_loc4:
514       return "DW_CFA_advance_loc4";
515     case DW_CFA_offset_extended:
516       return "DW_CFA_offset_extended";
517     case DW_CFA_restore_extended:
518       return "DW_CFA_restore_extended";
519     case DW_CFA_undefined:
520       return "DW_CFA_undefined";
521     case DW_CFA_same_value:
522       return "DW_CFA_same_value";
523     case DW_CFA_register:
524       return "DW_CFA_register";
525     case DW_CFA_remember_state:
526       return "DW_CFA_remember_state";
527     case DW_CFA_restore_state:
528       return "DW_CFA_restore_state";
529     case DW_CFA_def_cfa:
530       return "DW_CFA_def_cfa";
531     case DW_CFA_def_cfa_register:
532       return "DW_CFA_def_cfa_register";
533     case DW_CFA_def_cfa_offset:
534       return "DW_CFA_def_cfa_offset";
535
536     /* DWARF 3 */
537     case DW_CFA_def_cfa_expression:
538       return "DW_CFA_def_cfa_expression";
539     case DW_CFA_expression:
540       return "DW_CFA_expression";
541     case DW_CFA_offset_extended_sf:
542       return "DW_CFA_offset_extended_sf";
543     case DW_CFA_def_cfa_sf:
544       return "DW_CFA_def_cfa_sf";
545     case DW_CFA_def_cfa_offset_sf:
546       return "DW_CFA_def_cfa_offset_sf";
547
548     /* SGI/MIPS specific */
549     case DW_CFA_MIPS_advance_loc8:
550       return "DW_CFA_MIPS_advance_loc8";
551
552     /* GNU extensions */
553     case DW_CFA_GNU_window_save:
554       return "DW_CFA_GNU_window_save";
555     case DW_CFA_GNU_args_size:
556       return "DW_CFA_GNU_args_size";
557     case DW_CFA_GNU_negative_offset_extended:
558       return "DW_CFA_GNU_negative_offset_extended";
559
560     default:
561       return "DW_CFA_<unknown>";
562     }
563 }
564
565 /* Return a pointer to a newly allocated Call Frame Instruction.  */
566
567 static inline dw_cfi_ref
568 new_cfi (void)
569 {
570   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
571
572   cfi->dw_cfi_next = NULL;
573   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
574   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
575
576   return cfi;
577 }
578
579 /* Add a Call Frame Instruction to list of instructions.  */
580
581 static inline void
582 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
583 {
584   dw_cfi_ref *p;
585
586   /* Find the end of the chain.  */
587   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
588     ;
589
590   *p = cfi;
591 }
592
593 /* Generate a new label for the CFI info to refer to.  */
594
595 char *
596 dwarf2out_cfi_label (void)
597 {
598   static char label[20];
599
600   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
601   ASM_OUTPUT_LABEL (asm_out_file, label);
602   return label;
603 }
604
605 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
606    or to the CIE if LABEL is NULL.  */
607
608 static void
609 add_fde_cfi (const char *label, dw_cfi_ref cfi)
610 {
611   if (label)
612     {
613       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
614
615       if (*label == 0)
616         label = dwarf2out_cfi_label ();
617
618       if (fde->dw_fde_current_label == NULL
619           || strcmp (label, fde->dw_fde_current_label) != 0)
620         {
621           dw_cfi_ref xcfi;
622
623           fde->dw_fde_current_label = label = xstrdup (label);
624
625           /* Set the location counter to the new label.  */
626           xcfi = new_cfi ();
627           xcfi->dw_cfi_opc = DW_CFA_advance_loc4;
628           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
629           add_cfi (&fde->dw_fde_cfi, xcfi);
630         }
631
632       add_cfi (&fde->dw_fde_cfi, cfi);
633     }
634
635   else
636     add_cfi (&cie_cfi_head, cfi);
637 }
638
639 /* Subroutine of lookup_cfa.  */
640
641 static void
642 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
643 {
644   switch (cfi->dw_cfi_opc)
645     {
646     case DW_CFA_def_cfa_offset:
647       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
648       break;
649     case DW_CFA_def_cfa_offset_sf:
650       loc->offset
651         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
652       break;
653     case DW_CFA_def_cfa_register:
654       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
655       break;
656     case DW_CFA_def_cfa:
657       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
658       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
659       break;
660     case DW_CFA_def_cfa_sf:
661       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
662       loc->offset
663         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
664       break;
665     case DW_CFA_def_cfa_expression:
666       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
667       break;
668     default:
669       break;
670     }
671 }
672
673 /* Find the previous value for the CFA.  */
674
675 static void
676 lookup_cfa (dw_cfa_location *loc)
677 {
678   dw_cfi_ref cfi;
679
680   loc->reg = INVALID_REGNUM;
681   loc->offset = 0;
682   loc->indirect = 0;
683   loc->base_offset = 0;
684
685   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
686     lookup_cfa_1 (cfi, loc);
687
688   if (fde_table_in_use)
689     {
690       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
691       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
692         lookup_cfa_1 (cfi, loc);
693     }
694 }
695
696 /* The current rule for calculating the DWARF2 canonical frame address.  */
697 static dw_cfa_location cfa;
698
699 /* The register used for saving registers to the stack, and its offset
700    from the CFA.  */
701 static dw_cfa_location cfa_store;
702
703 /* The running total of the size of arguments pushed onto the stack.  */
704 static HOST_WIDE_INT args_size;
705
706 /* The last args_size we actually output.  */
707 static HOST_WIDE_INT old_args_size;
708
709 /* Entry point to update the canonical frame address (CFA).
710    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
711    calculated from REG+OFFSET.  */
712
713 void
714 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
715 {
716   dw_cfa_location loc;
717   loc.indirect = 0;
718   loc.base_offset = 0;
719   loc.reg = reg;
720   loc.offset = offset;
721   def_cfa_1 (label, &loc);
722 }
723
724 /* Determine if two dw_cfa_location structures define the same data.  */
725
726 static bool
727 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
728 {
729   return (loc1->reg == loc2->reg
730           && loc1->offset == loc2->offset
731           && loc1->indirect == loc2->indirect
732           && (loc1->indirect == 0
733               || loc1->base_offset == loc2->base_offset));
734 }
735
736 /* This routine does the actual work.  The CFA is now calculated from
737    the dw_cfa_location structure.  */
738
739 static void
740 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
741 {
742   dw_cfi_ref cfi;
743   dw_cfa_location old_cfa, loc;
744
745   cfa = *loc_p;
746   loc = *loc_p;
747
748   if (cfa_store.reg == loc.reg && loc.indirect == 0)
749     cfa_store.offset = loc.offset;
750
751   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
752   lookup_cfa (&old_cfa);
753
754   /* If nothing changed, no need to issue any call frame instructions.  */
755   if (cfa_equal_p (&loc, &old_cfa))
756     return;
757
758   cfi = new_cfi ();
759
760   if (loc.reg == old_cfa.reg && !loc.indirect)
761     {
762       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
763          the CFA register did not change but the offset did.  */
764       if (loc.offset < 0)
765         {
766           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
767           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
768
769           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
770           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
771         }
772       else
773         {
774           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
775           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
776         }
777     }
778
779 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
780   else if (loc.offset == old_cfa.offset
781            && old_cfa.reg != INVALID_REGNUM
782            && !loc.indirect)
783     {
784       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
785          indicating the CFA register has changed to <register> but the
786          offset has not changed.  */
787       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
788       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
789     }
790 #endif
791
792   else if (loc.indirect == 0)
793     {
794       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
795          indicating the CFA register has changed to <register> with
796          the specified offset.  */
797       if (loc.offset < 0)
798         {
799           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
800           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
801
802           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
803           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
804           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
805         }
806       else
807         {
808           cfi->dw_cfi_opc = DW_CFA_def_cfa;
809           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
810           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
811         }
812     }
813   else
814     {
815       /* Construct a DW_CFA_def_cfa_expression instruction to
816          calculate the CFA using a full location expression since no
817          register-offset pair is available.  */
818       struct dw_loc_descr_struct *loc_list;
819
820       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
821       loc_list = build_cfa_loc (&loc, 0);
822       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
823     }
824
825   add_fde_cfi (label, cfi);
826 }
827
828 /* Add the CFI for saving a register.  REG is the CFA column number.
829    LABEL is passed to add_fde_cfi.
830    If SREG is -1, the register is saved at OFFSET from the CFA;
831    otherwise it is saved in SREG.  */
832
833 static void
834 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
835 {
836   dw_cfi_ref cfi = new_cfi ();
837
838   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
839
840   if (sreg == INVALID_REGNUM)
841     {
842       if (reg & ~0x3f)
843         /* The register number won't fit in 6 bits, so we have to use
844            the long form.  */
845         cfi->dw_cfi_opc = DW_CFA_offset_extended;
846       else
847         cfi->dw_cfi_opc = DW_CFA_offset;
848
849 #ifdef ENABLE_CHECKING
850       {
851         /* If we get an offset that is not a multiple of
852            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
853            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
854            description.  */
855         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
856
857         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
858       }
859 #endif
860       offset /= DWARF_CIE_DATA_ALIGNMENT;
861       if (offset < 0)
862         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
863
864       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
865     }
866   else if (sreg == reg)
867     cfi->dw_cfi_opc = DW_CFA_same_value;
868   else
869     {
870       cfi->dw_cfi_opc = DW_CFA_register;
871       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
872     }
873
874   add_fde_cfi (label, cfi);
875 }
876
877 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
878    This CFI tells the unwinder that it needs to restore the window registers
879    from the previous frame's window save area.
880
881    ??? Perhaps we should note in the CIE where windows are saved (instead of
882    assuming 0(cfa)) and what registers are in the window.  */
883
884 void
885 dwarf2out_window_save (const char *label)
886 {
887   dw_cfi_ref cfi = new_cfi ();
888
889   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
890   add_fde_cfi (label, cfi);
891 }
892
893 /* Add a CFI to update the running total of the size of arguments
894    pushed onto the stack.  */
895
896 void
897 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
898 {
899   dw_cfi_ref cfi;
900
901   if (size == old_args_size)
902     return;
903
904   old_args_size = size;
905
906   cfi = new_cfi ();
907   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
908   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
909   add_fde_cfi (label, cfi);
910 }
911
912 /* Entry point for saving a register to the stack.  REG is the GCC register
913    number.  LABEL and OFFSET are passed to reg_save.  */
914
915 void
916 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
917 {
918   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
919 }
920
921 /* Entry point for saving the return address in the stack.
922    LABEL and OFFSET are passed to reg_save.  */
923
924 void
925 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
926 {
927   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
928 }
929
930 /* Entry point for saving the return address in a register.
931    LABEL and SREG are passed to reg_save.  */
932
933 void
934 dwarf2out_return_reg (const char *label, unsigned int sreg)
935 {
936   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
937 }
938
939 /* Record the initial position of the return address.  RTL is
940    INCOMING_RETURN_ADDR_RTX.  */
941
942 static void
943 initial_return_save (rtx rtl)
944 {
945   unsigned int reg = INVALID_REGNUM;
946   HOST_WIDE_INT offset = 0;
947
948   switch (GET_CODE (rtl))
949     {
950     case REG:
951       /* RA is in a register.  */
952       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
953       break;
954
955     case MEM:
956       /* RA is on the stack.  */
957       rtl = XEXP (rtl, 0);
958       switch (GET_CODE (rtl))
959         {
960         case REG:
961           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
962           offset = 0;
963           break;
964
965         case PLUS:
966           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
967           offset = INTVAL (XEXP (rtl, 1));
968           break;
969
970         case MINUS:
971           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
972           offset = -INTVAL (XEXP (rtl, 1));
973           break;
974
975         default:
976           gcc_unreachable ();
977         }
978
979       break;
980
981     case PLUS:
982       /* The return address is at some offset from any value we can
983          actually load.  For instance, on the SPARC it is in %i7+8. Just
984          ignore the offset for now; it doesn't matter for unwinding frames.  */
985       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
986       initial_return_save (XEXP (rtl, 0));
987       return;
988
989     default:
990       gcc_unreachable ();
991     }
992
993   if (reg != DWARF_FRAME_RETURN_COLUMN)
994     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
995 }
996
997 /* Given a SET, calculate the amount of stack adjustment it
998    contains.  */
999
1000 static HOST_WIDE_INT
1001 stack_adjust_offset (rtx pattern)
1002 {
1003   rtx src = SET_SRC (pattern);
1004   rtx dest = SET_DEST (pattern);
1005   HOST_WIDE_INT offset = 0;
1006   enum rtx_code code;
1007
1008   if (dest == stack_pointer_rtx)
1009     {
1010       /* (set (reg sp) (plus (reg sp) (const_int))) */
1011       code = GET_CODE (src);
1012       if (! (code == PLUS || code == MINUS)
1013           || XEXP (src, 0) != stack_pointer_rtx
1014           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1015         return 0;
1016
1017       offset = INTVAL (XEXP (src, 1));
1018       if (code == PLUS)
1019         offset = -offset;
1020     }
1021   else if (MEM_P (dest))
1022     {
1023       /* (set (mem (pre_dec (reg sp))) (foo)) */
1024       src = XEXP (dest, 0);
1025       code = GET_CODE (src);
1026
1027       switch (code)
1028         {
1029         case PRE_MODIFY:
1030         case POST_MODIFY:
1031           if (XEXP (src, 0) == stack_pointer_rtx)
1032             {
1033               rtx val = XEXP (XEXP (src, 1), 1);
1034               /* We handle only adjustments by constant amount.  */
1035               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1036                           && GET_CODE (val) == CONST_INT);
1037               offset = -INTVAL (val);
1038               break;
1039             }
1040           return 0;
1041
1042         case PRE_DEC:
1043         case POST_DEC:
1044           if (XEXP (src, 0) == stack_pointer_rtx)
1045             {
1046               offset = GET_MODE_SIZE (GET_MODE (dest));
1047               break;
1048             }
1049           return 0;
1050
1051         case PRE_INC:
1052         case POST_INC:
1053           if (XEXP (src, 0) == stack_pointer_rtx)
1054             {
1055               offset = -GET_MODE_SIZE (GET_MODE (dest));
1056               break;
1057             }
1058           return 0;
1059
1060         default:
1061           return 0;
1062         }
1063     }
1064   else
1065     return 0;
1066
1067   return offset;
1068 }
1069
1070 /* Check INSN to see if it looks like a push or a stack adjustment, and
1071    make a note of it if it does.  EH uses this information to find out how
1072    much extra space it needs to pop off the stack.  */
1073
1074 static void
1075 dwarf2out_stack_adjust (rtx insn, bool after_p)
1076 {
1077   HOST_WIDE_INT offset;
1078   const char *label;
1079   int i;
1080
1081   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1082      with this function.  Proper support would require all frame-related
1083      insns to be marked, and to be able to handle saving state around
1084      epilogues textually in the middle of the function.  */
1085   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1086     return;
1087
1088   /* If only calls can throw, and we have a frame pointer,
1089      save up adjustments until we see the CALL_INSN.  */
1090   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1091     {
1092       if (CALL_P (insn) && !after_p)
1093         {
1094           /* Extract the size of the args from the CALL rtx itself.  */
1095           insn = PATTERN (insn);
1096           if (GET_CODE (insn) == PARALLEL)
1097             insn = XVECEXP (insn, 0, 0);
1098           if (GET_CODE (insn) == SET)
1099             insn = SET_SRC (insn);
1100           gcc_assert (GET_CODE (insn) == CALL);
1101           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1102         }
1103       return;
1104     }
1105
1106   if (CALL_P (insn) && !after_p)
1107     {
1108       if (!flag_asynchronous_unwind_tables)
1109         dwarf2out_args_size ("", args_size);
1110       return;
1111     }
1112   else if (BARRIER_P (insn))
1113     {
1114       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1115          the compiler will have already emitted a stack adjustment, but
1116          doesn't bother for calls to noreturn functions.  */
1117 #ifdef STACK_GROWS_DOWNWARD
1118       offset = -args_size;
1119 #else
1120       offset = args_size;
1121 #endif
1122     }
1123   else if (GET_CODE (PATTERN (insn)) == SET)
1124     offset = stack_adjust_offset (PATTERN (insn));
1125   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1126            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1127     {
1128       /* There may be stack adjustments inside compound insns.  Search
1129          for them.  */
1130       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1131         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1132           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1133     }
1134   else
1135     return;
1136
1137   if (offset == 0)
1138     return;
1139
1140   if (cfa.reg == STACK_POINTER_REGNUM)
1141     cfa.offset += offset;
1142
1143 #ifndef STACK_GROWS_DOWNWARD
1144   offset = -offset;
1145 #endif
1146
1147   args_size += offset;
1148   if (args_size < 0)
1149     args_size = 0;
1150
1151   label = dwarf2out_cfi_label ();
1152   def_cfa_1 (label, &cfa);
1153   if (flag_asynchronous_unwind_tables)
1154     dwarf2out_args_size (label, args_size);
1155 }
1156
1157 #endif
1158
1159 /* We delay emitting a register save until either (a) we reach the end
1160    of the prologue or (b) the register is clobbered.  This clusters
1161    register saves so that there are fewer pc advances.  */
1162
1163 struct queued_reg_save GTY(())
1164 {
1165   struct queued_reg_save *next;
1166   rtx reg;
1167   HOST_WIDE_INT cfa_offset;
1168   rtx saved_reg;
1169 };
1170
1171 static GTY(()) struct queued_reg_save *queued_reg_saves;
1172
1173 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1174 struct reg_saved_in_data GTY(()) {
1175   rtx orig_reg;
1176   rtx saved_in_reg;
1177 };
1178
1179 /* A list of registers saved in other registers.
1180    The list intentionally has a small maximum capacity of 4; if your
1181    port needs more than that, you might consider implementing a
1182    more efficient data structure.  */
1183 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1184 static GTY(()) size_t num_regs_saved_in_regs;
1185
1186 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1187 static const char *last_reg_save_label;
1188
1189 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1190    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1191
1192 static void
1193 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1194 {
1195   struct queued_reg_save *q;
1196
1197   /* Duplicates waste space, but it's also necessary to remove them
1198      for correctness, since the queue gets output in reverse
1199      order.  */
1200   for (q = queued_reg_saves; q != NULL; q = q->next)
1201     if (REGNO (q->reg) == REGNO (reg))
1202       break;
1203
1204   if (q == NULL)
1205     {
1206       q = ggc_alloc (sizeof (*q));
1207       q->next = queued_reg_saves;
1208       queued_reg_saves = q;
1209     }
1210
1211   q->reg = reg;
1212   q->cfa_offset = offset;
1213   q->saved_reg = sreg;
1214
1215   last_reg_save_label = label;
1216 }
1217
1218 /* Output all the entries in QUEUED_REG_SAVES.  */
1219
1220 static void
1221 flush_queued_reg_saves (void)
1222 {
1223   struct queued_reg_save *q;
1224
1225   for (q = queued_reg_saves; q; q = q->next)
1226     {
1227       size_t i;
1228       unsigned int reg, sreg;
1229
1230       for (i = 0; i < num_regs_saved_in_regs; i++)
1231         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1232           break;
1233       if (q->saved_reg && i == num_regs_saved_in_regs)
1234         {
1235           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1236           num_regs_saved_in_regs++;
1237         }
1238       if (i != num_regs_saved_in_regs)
1239         {
1240           regs_saved_in_regs[i].orig_reg = q->reg;
1241           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1242         }
1243
1244       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1245       if (q->saved_reg)
1246         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1247       else
1248         sreg = INVALID_REGNUM;
1249       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1250     }
1251
1252   queued_reg_saves = NULL;
1253   last_reg_save_label = NULL;
1254 }
1255
1256 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1257    location for?  Or, does it clobber a register which we've previously
1258    said that some other register is saved in, and for which we now
1259    have a new location for?  */
1260
1261 static bool
1262 clobbers_queued_reg_save (rtx insn)
1263 {
1264   struct queued_reg_save *q;
1265
1266   for (q = queued_reg_saves; q; q = q->next)
1267     {
1268       size_t i;
1269       if (modified_in_p (q->reg, insn))
1270         return true;
1271       for (i = 0; i < num_regs_saved_in_regs; i++)
1272         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1273             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1274           return true;
1275     }
1276
1277   return false;
1278 }
1279
1280 /* Entry point for saving the first register into the second.  */
1281
1282 void
1283 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1284 {
1285   size_t i;
1286   unsigned int regno, sregno;
1287
1288   for (i = 0; i < num_regs_saved_in_regs; i++)
1289     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1290       break;
1291   if (i == num_regs_saved_in_regs)
1292     {
1293       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1294       num_regs_saved_in_regs++;
1295     }
1296   regs_saved_in_regs[i].orig_reg = reg;
1297   regs_saved_in_regs[i].saved_in_reg = sreg;
1298
1299   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1300   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1301   reg_save (label, regno, sregno, 0);
1302 }
1303
1304 /* What register, if any, is currently saved in REG?  */
1305
1306 static rtx
1307 reg_saved_in (rtx reg)
1308 {
1309   unsigned int regn = REGNO (reg);
1310   size_t i;
1311   struct queued_reg_save *q;
1312
1313   for (q = queued_reg_saves; q; q = q->next)
1314     if (q->saved_reg && regn == REGNO (q->saved_reg))
1315       return q->reg;
1316
1317   for (i = 0; i < num_regs_saved_in_regs; i++)
1318     if (regs_saved_in_regs[i].saved_in_reg
1319         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1320       return regs_saved_in_regs[i].orig_reg;
1321
1322   return NULL_RTX;
1323 }
1324
1325
1326 /* A temporary register holding an integral value used in adjusting SP
1327    or setting up the store_reg.  The "offset" field holds the integer
1328    value, not an offset.  */
1329 static dw_cfa_location cfa_temp;
1330
1331 /* Record call frame debugging information for an expression EXPR,
1332    which either sets SP or FP (adjusting how we calculate the frame
1333    address) or saves a register to the stack or another register.
1334    LABEL indicates the address of EXPR.
1335
1336    This function encodes a state machine mapping rtxes to actions on
1337    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1338    users need not read the source code.
1339
1340   The High-Level Picture
1341
1342   Changes in the register we use to calculate the CFA: Currently we
1343   assume that if you copy the CFA register into another register, we
1344   should take the other one as the new CFA register; this seems to
1345   work pretty well.  If it's wrong for some target, it's simple
1346   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1347
1348   Changes in the register we use for saving registers to the stack:
1349   This is usually SP, but not always.  Again, we deduce that if you
1350   copy SP into another register (and SP is not the CFA register),
1351   then the new register is the one we will be using for register
1352   saves.  This also seems to work.
1353
1354   Register saves: There's not much guesswork about this one; if
1355   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1356   register save, and the register used to calculate the destination
1357   had better be the one we think we're using for this purpose.
1358   It's also assumed that a copy from a call-saved register to another
1359   register is saving that register if RTX_FRAME_RELATED_P is set on
1360   that instruction.  If the copy is from a call-saved register to
1361   the *same* register, that means that the register is now the same
1362   value as in the caller.
1363
1364   Except: If the register being saved is the CFA register, and the
1365   offset is nonzero, we are saving the CFA, so we assume we have to
1366   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1367   the intent is to save the value of SP from the previous frame.
1368
1369   In addition, if a register has previously been saved to a different
1370   register,
1371
1372   Invariants / Summaries of Rules
1373
1374   cfa          current rule for calculating the CFA.  It usually
1375                consists of a register and an offset.
1376   cfa_store    register used by prologue code to save things to the stack
1377                cfa_store.offset is the offset from the value of
1378                cfa_store.reg to the actual CFA
1379   cfa_temp     register holding an integral value.  cfa_temp.offset
1380                stores the value, which will be used to adjust the
1381                stack pointer.  cfa_temp is also used like cfa_store,
1382                to track stores to the stack via fp or a temp reg.
1383
1384   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1385                with cfa.reg as the first operand changes the cfa.reg and its
1386                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1387                cfa_temp.offset.
1388
1389   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1390                expression yielding a constant.  This sets cfa_temp.reg
1391                and cfa_temp.offset.
1392
1393   Rule 5:      Create a new register cfa_store used to save items to the
1394                stack.
1395
1396   Rules 10-14: Save a register to the stack.  Define offset as the
1397                difference of the original location and cfa_store's
1398                location (or cfa_temp's location if cfa_temp is used).
1399
1400   The Rules
1401
1402   "{a,b}" indicates a choice of a xor b.
1403   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1404
1405   Rule 1:
1406   (set <reg1> <reg2>:cfa.reg)
1407   effects: cfa.reg = <reg1>
1408            cfa.offset unchanged
1409            cfa_temp.reg = <reg1>
1410            cfa_temp.offset = cfa.offset
1411
1412   Rule 2:
1413   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1414                               {<const_int>,<reg>:cfa_temp.reg}))
1415   effects: cfa.reg = sp if fp used
1416            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1417            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1418              if cfa_store.reg==sp
1419
1420   Rule 3:
1421   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1422   effects: cfa.reg = fp
1423            cfa_offset += +/- <const_int>
1424
1425   Rule 4:
1426   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1427   constraints: <reg1> != fp
1428                <reg1> != sp
1429   effects: cfa.reg = <reg1>
1430            cfa_temp.reg = <reg1>
1431            cfa_temp.offset = cfa.offset
1432
1433   Rule 5:
1434   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1435   constraints: <reg1> != fp
1436                <reg1> != sp
1437   effects: cfa_store.reg = <reg1>
1438            cfa_store.offset = cfa.offset - cfa_temp.offset
1439
1440   Rule 6:
1441   (set <reg> <const_int>)
1442   effects: cfa_temp.reg = <reg>
1443            cfa_temp.offset = <const_int>
1444
1445   Rule 7:
1446   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1447   effects: cfa_temp.reg = <reg1>
1448            cfa_temp.offset |= <const_int>
1449
1450   Rule 8:
1451   (set <reg> (high <exp>))
1452   effects: none
1453
1454   Rule 9:
1455   (set <reg> (lo_sum <exp> <const_int>))
1456   effects: cfa_temp.reg = <reg>
1457            cfa_temp.offset = <const_int>
1458
1459   Rule 10:
1460   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1461   effects: cfa_store.offset -= <const_int>
1462            cfa.offset = cfa_store.offset if cfa.reg == sp
1463            cfa.reg = sp
1464            cfa.base_offset = -cfa_store.offset
1465
1466   Rule 11:
1467   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1468   effects: cfa_store.offset += -/+ mode_size(mem)
1469            cfa.offset = cfa_store.offset if cfa.reg == sp
1470            cfa.reg = sp
1471            cfa.base_offset = -cfa_store.offset
1472
1473   Rule 12:
1474   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1475
1476        <reg2>)
1477   effects: cfa.reg = <reg1>
1478            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1479
1480   Rule 13:
1481   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1482   effects: cfa.reg = <reg1>
1483            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1484
1485   Rule 14:
1486   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1487   effects: cfa.reg = <reg1>
1488            cfa.base_offset = -cfa_temp.offset
1489            cfa_temp.offset -= mode_size(mem)
1490
1491   Rule 15:
1492   (set <reg> {unspec, unspec_volatile})
1493   effects: target-dependent  */
1494
1495 static void
1496 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1497 {
1498   rtx src, dest;
1499   HOST_WIDE_INT offset;
1500
1501   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1502      the PARALLEL independently. The first element is always processed if
1503      it is a SET. This is for backward compatibility.   Other elements
1504      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1505      flag is set in them.  */
1506   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1507     {
1508       int par_index;
1509       int limit = XVECLEN (expr, 0);
1510
1511       for (par_index = 0; par_index < limit; par_index++)
1512         if (GET_CODE (XVECEXP (expr, 0, par_index)) == SET
1513             && (RTX_FRAME_RELATED_P (XVECEXP (expr, 0, par_index))
1514                 || par_index == 0))
1515           dwarf2out_frame_debug_expr (XVECEXP (expr, 0, par_index), label);
1516
1517       return;
1518     }
1519
1520   gcc_assert (GET_CODE (expr) == SET);
1521
1522   src = SET_SRC (expr);
1523   dest = SET_DEST (expr);
1524
1525   if (REG_P (src))
1526     {
1527       rtx rsi = reg_saved_in (src);
1528       if (rsi)
1529         src = rsi;
1530     }
1531
1532   switch (GET_CODE (dest))
1533     {
1534     case REG:
1535       switch (GET_CODE (src))
1536         {
1537           /* Setting FP from SP.  */
1538         case REG:
1539           if (cfa.reg == (unsigned) REGNO (src))
1540             {
1541               /* Rule 1 */
1542               /* Update the CFA rule wrt SP or FP.  Make sure src is
1543                  relative to the current CFA register.
1544
1545                  We used to require that dest be either SP or FP, but the
1546                  ARM copies SP to a temporary register, and from there to
1547                  FP.  So we just rely on the backends to only set
1548                  RTX_FRAME_RELATED_P on appropriate insns.  */
1549               cfa.reg = REGNO (dest);
1550               cfa_temp.reg = cfa.reg;
1551               cfa_temp.offset = cfa.offset;
1552             }
1553           else
1554             {
1555               /* Saving a register in a register.  */
1556               gcc_assert (!fixed_regs [REGNO (dest)]
1557                           /* For the SPARC and its register window.  */
1558                           || (DWARF_FRAME_REGNUM (REGNO (src))
1559                               == DWARF_FRAME_RETURN_COLUMN));
1560               queue_reg_save (label, src, dest, 0);
1561             }
1562           break;
1563
1564         case PLUS:
1565         case MINUS:
1566         case LO_SUM:
1567           if (dest == stack_pointer_rtx)
1568             {
1569               /* Rule 2 */
1570               /* Adjusting SP.  */
1571               switch (GET_CODE (XEXP (src, 1)))
1572                 {
1573                 case CONST_INT:
1574                   offset = INTVAL (XEXP (src, 1));
1575                   break;
1576                 case REG:
1577                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1578                               == cfa_temp.reg);
1579                   offset = cfa_temp.offset;
1580                   break;
1581                 default:
1582                   gcc_unreachable ();
1583                 }
1584
1585               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1586                 {
1587                   /* Restoring SP from FP in the epilogue.  */
1588                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1589                   cfa.reg = STACK_POINTER_REGNUM;
1590                 }
1591               else if (GET_CODE (src) == LO_SUM)
1592                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1593                 ;
1594               else
1595                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1596
1597               if (GET_CODE (src) != MINUS)
1598                 offset = -offset;
1599               if (cfa.reg == STACK_POINTER_REGNUM)
1600                 cfa.offset += offset;
1601               if (cfa_store.reg == STACK_POINTER_REGNUM)
1602                 cfa_store.offset += offset;
1603             }
1604           else if (dest == hard_frame_pointer_rtx)
1605             {
1606               /* Rule 3 */
1607               /* Either setting the FP from an offset of the SP,
1608                  or adjusting the FP */
1609               gcc_assert (frame_pointer_needed);
1610
1611               gcc_assert (REG_P (XEXP (src, 0))
1612                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1613                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1614               offset = INTVAL (XEXP (src, 1));
1615               if (GET_CODE (src) != MINUS)
1616                 offset = -offset;
1617               cfa.offset += offset;
1618               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1619             }
1620           else
1621             {
1622               gcc_assert (GET_CODE (src) != MINUS);
1623
1624               /* Rule 4 */
1625               if (REG_P (XEXP (src, 0))
1626                   && REGNO (XEXP (src, 0)) == cfa.reg
1627                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1628                 {
1629                   /* Setting a temporary CFA register that will be copied
1630                      into the FP later on.  */
1631                   offset = - INTVAL (XEXP (src, 1));
1632                   cfa.offset += offset;
1633                   cfa.reg = REGNO (dest);
1634                   /* Or used to save regs to the stack.  */
1635                   cfa_temp.reg = cfa.reg;
1636                   cfa_temp.offset = cfa.offset;
1637                 }
1638
1639               /* Rule 5 */
1640               else if (REG_P (XEXP (src, 0))
1641                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1642                        && XEXP (src, 1) == stack_pointer_rtx)
1643                 {
1644                   /* Setting a scratch register that we will use instead
1645                      of SP for saving registers to the stack.  */
1646                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1647                   cfa_store.reg = REGNO (dest);
1648                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1649                 }
1650
1651               /* Rule 9 */
1652               else if (GET_CODE (src) == LO_SUM
1653                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1654                 {
1655                   cfa_temp.reg = REGNO (dest);
1656                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1657                 }
1658               else
1659                 gcc_unreachable ();
1660             }
1661           break;
1662
1663           /* Rule 6 */
1664         case CONST_INT:
1665           cfa_temp.reg = REGNO (dest);
1666           cfa_temp.offset = INTVAL (src);
1667           break;
1668
1669           /* Rule 7 */
1670         case IOR:
1671           gcc_assert (REG_P (XEXP (src, 0))
1672                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1673                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1674
1675           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1676             cfa_temp.reg = REGNO (dest);
1677           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1678           break;
1679
1680           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1681              which will fill in all of the bits.  */
1682           /* Rule 8 */
1683         case HIGH:
1684           break;
1685
1686           /* Rule 15 */
1687         case UNSPEC:
1688         case UNSPEC_VOLATILE:
1689           gcc_assert (targetm.dwarf_handle_frame_unspec);
1690           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1691           return;
1692
1693         default:
1694           gcc_unreachable ();
1695         }
1696
1697       def_cfa_1 (label, &cfa);
1698       break;
1699
1700     case MEM:
1701       gcc_assert (REG_P (src));
1702
1703       /* Saving a register to the stack.  Make sure dest is relative to the
1704          CFA register.  */
1705       switch (GET_CODE (XEXP (dest, 0)))
1706         {
1707           /* Rule 10 */
1708           /* With a push.  */
1709         case PRE_MODIFY:
1710           /* We can't handle variable size modifications.  */
1711           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1712                       == CONST_INT);
1713           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1714
1715           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1716                       && cfa_store.reg == STACK_POINTER_REGNUM);
1717
1718           cfa_store.offset += offset;
1719           if (cfa.reg == STACK_POINTER_REGNUM)
1720             cfa.offset = cfa_store.offset;
1721
1722           offset = -cfa_store.offset;
1723           break;
1724
1725           /* Rule 11 */
1726         case PRE_INC:
1727         case PRE_DEC:
1728           offset = GET_MODE_SIZE (GET_MODE (dest));
1729           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1730             offset = -offset;
1731
1732           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1733                       && cfa_store.reg == STACK_POINTER_REGNUM);
1734
1735           cfa_store.offset += offset;
1736           if (cfa.reg == STACK_POINTER_REGNUM)
1737             cfa.offset = cfa_store.offset;
1738
1739           offset = -cfa_store.offset;
1740           break;
1741
1742           /* Rule 12 */
1743           /* With an offset.  */
1744         case PLUS:
1745         case MINUS:
1746         case LO_SUM:
1747           {
1748             int regno;
1749
1750             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1751                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1752             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1753             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1754               offset = -offset;
1755
1756             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1757
1758             if (cfa_store.reg == (unsigned) regno)
1759               offset -= cfa_store.offset;
1760             else
1761               {
1762                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1763                 offset -= cfa_temp.offset;
1764               }
1765           }
1766           break;
1767
1768           /* Rule 13 */
1769           /* Without an offset.  */
1770         case REG:
1771           {
1772             int regno = REGNO (XEXP (dest, 0));
1773
1774             if (cfa_store.reg == (unsigned) regno)
1775               offset = -cfa_store.offset;
1776             else
1777               {
1778                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1779                 offset = -cfa_temp.offset;
1780               }
1781           }
1782           break;
1783
1784           /* Rule 14 */
1785         case POST_INC:
1786           gcc_assert (cfa_temp.reg
1787                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1788           offset = -cfa_temp.offset;
1789           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1790           break;
1791
1792         default:
1793           gcc_unreachable ();
1794         }
1795
1796       if (REGNO (src) != STACK_POINTER_REGNUM
1797           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1798           && (unsigned) REGNO (src) == cfa.reg)
1799         {
1800           /* We're storing the current CFA reg into the stack.  */
1801
1802           if (cfa.offset == 0)
1803             {
1804               /* If the source register is exactly the CFA, assume
1805                  we're saving SP like any other register; this happens
1806                  on the ARM.  */
1807               def_cfa_1 (label, &cfa);
1808               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1809               break;
1810             }
1811           else
1812             {
1813               /* Otherwise, we'll need to look in the stack to
1814                  calculate the CFA.  */
1815               rtx x = XEXP (dest, 0);
1816
1817               if (!REG_P (x))
1818                 x = XEXP (x, 0);
1819               gcc_assert (REG_P (x));
1820
1821               cfa.reg = REGNO (x);
1822               cfa.base_offset = offset;
1823               cfa.indirect = 1;
1824               def_cfa_1 (label, &cfa);
1825               break;
1826             }
1827         }
1828
1829       def_cfa_1 (label, &cfa);
1830       queue_reg_save (label, src, NULL_RTX, offset);
1831       break;
1832
1833     default:
1834       gcc_unreachable ();
1835     }
1836 }
1837
1838 /* Record call frame debugging information for INSN, which either
1839    sets SP or FP (adjusting how we calculate the frame address) or saves a
1840    register to the stack.  If INSN is NULL_RTX, initialize our state.
1841
1842    If AFTER_P is false, we're being called before the insn is emitted,
1843    otherwise after.  Call instructions get invoked twice.  */
1844
1845 void
1846 dwarf2out_frame_debug (rtx insn, bool after_p)
1847 {
1848   const char *label;
1849   rtx src;
1850
1851   if (insn == NULL_RTX)
1852     {
1853       size_t i;
1854
1855       /* Flush any queued register saves.  */
1856       flush_queued_reg_saves ();
1857
1858       /* Set up state for generating call frame debug info.  */
1859       lookup_cfa (&cfa);
1860       gcc_assert (cfa.reg
1861                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1862
1863       cfa.reg = STACK_POINTER_REGNUM;
1864       cfa_store = cfa;
1865       cfa_temp.reg = -1;
1866       cfa_temp.offset = 0;
1867
1868       for (i = 0; i < num_regs_saved_in_regs; i++)
1869         {
1870           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1871           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1872         }
1873       num_regs_saved_in_regs = 0;
1874       return;
1875     }
1876
1877   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1878     flush_queued_reg_saves ();
1879
1880   if (! RTX_FRAME_RELATED_P (insn))
1881     {
1882       if (!ACCUMULATE_OUTGOING_ARGS)
1883         dwarf2out_stack_adjust (insn, after_p);
1884       return;
1885     }
1886
1887   label = dwarf2out_cfi_label ();
1888   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1889   if (src)
1890     insn = XEXP (src, 0);
1891   else
1892     insn = PATTERN (insn);
1893
1894   dwarf2out_frame_debug_expr (insn, label);
1895 }
1896
1897 #endif
1898
1899 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1900 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1901  (enum dwarf_call_frame_info cfi);
1902
1903 static enum dw_cfi_oprnd_type
1904 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1905 {
1906   switch (cfi)
1907     {
1908     case DW_CFA_nop:
1909     case DW_CFA_GNU_window_save:
1910       return dw_cfi_oprnd_unused;
1911
1912     case DW_CFA_set_loc:
1913     case DW_CFA_advance_loc1:
1914     case DW_CFA_advance_loc2:
1915     case DW_CFA_advance_loc4:
1916     case DW_CFA_MIPS_advance_loc8:
1917       return dw_cfi_oprnd_addr;
1918
1919     case DW_CFA_offset:
1920     case DW_CFA_offset_extended:
1921     case DW_CFA_def_cfa:
1922     case DW_CFA_offset_extended_sf:
1923     case DW_CFA_def_cfa_sf:
1924     case DW_CFA_restore_extended:
1925     case DW_CFA_undefined:
1926     case DW_CFA_same_value:
1927     case DW_CFA_def_cfa_register:
1928     case DW_CFA_register:
1929       return dw_cfi_oprnd_reg_num;
1930
1931     case DW_CFA_def_cfa_offset:
1932     case DW_CFA_GNU_args_size:
1933     case DW_CFA_def_cfa_offset_sf:
1934       return dw_cfi_oprnd_offset;
1935
1936     case DW_CFA_def_cfa_expression:
1937     case DW_CFA_expression:
1938       return dw_cfi_oprnd_loc;
1939
1940     default:
1941       gcc_unreachable ();
1942     }
1943 }
1944
1945 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1946 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1947  (enum dwarf_call_frame_info cfi);
1948
1949 static enum dw_cfi_oprnd_type
1950 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1951 {
1952   switch (cfi)
1953     {
1954     case DW_CFA_def_cfa:
1955     case DW_CFA_def_cfa_sf:
1956     case DW_CFA_offset:
1957     case DW_CFA_offset_extended_sf:
1958     case DW_CFA_offset_extended:
1959       return dw_cfi_oprnd_offset;
1960
1961     case DW_CFA_register:
1962       return dw_cfi_oprnd_reg_num;
1963
1964     default:
1965       return dw_cfi_oprnd_unused;
1966     }
1967 }
1968
1969 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1970
1971 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
1972    switch to the data section instead, and write out a synthetic label
1973    for collect2.  */
1974
1975 static void
1976 switch_to_eh_frame_section (void)
1977 {
1978   tree label;
1979
1980 #ifdef EH_FRAME_SECTION_NAME
1981   if (eh_frame_section == 0)
1982     {
1983       int flags;
1984
1985       if (EH_TABLES_CAN_BE_READ_ONLY)
1986         {
1987           int fde_encoding;
1988           int per_encoding;
1989           int lsda_encoding;
1990
1991           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
1992                                                        /*global=*/0);
1993           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
1994                                                        /*global=*/1);
1995           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
1996                                                         /*global=*/0);
1997           flags = ((! flag_pic
1998                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
1999                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2000                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2001                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2002                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2003                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2004                    ? 0 : SECTION_WRITE);
2005         }
2006       else
2007         flags = SECTION_WRITE;
2008       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2009     }
2010 #endif
2011
2012   if (eh_frame_section)
2013     switch_to_section (eh_frame_section);
2014   else
2015     {
2016       /* We have no special eh_frame section.  Put the information in
2017          the data section and emit special labels to guide collect2.  */
2018       switch_to_section (data_section);
2019       label = get_file_function_name ('F');
2020       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2021       targetm.asm_out.globalize_label (asm_out_file,
2022                                        IDENTIFIER_POINTER (label));
2023       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2024     }
2025 }
2026
2027 /* Output a Call Frame Information opcode and its operand(s).  */
2028
2029 static void
2030 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2031 {
2032   unsigned long r;
2033   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2034     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2035                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2036                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2037                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
2038   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2039     {
2040       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2041       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2042                            "DW_CFA_offset, column 0x%lx", r);
2043       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2044     }
2045   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2046     {
2047       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2048       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2049                            "DW_CFA_restore, column 0x%lx", r);
2050     }
2051   else
2052     {
2053       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2054                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2055
2056       switch (cfi->dw_cfi_opc)
2057         {
2058         case DW_CFA_set_loc:
2059           if (for_eh)
2060             dw2_asm_output_encoded_addr_rtx (
2061                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2062                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2063                 false, NULL);
2064           else
2065             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2066                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2067           break;
2068
2069         case DW_CFA_advance_loc1:
2070           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2071                                 fde->dw_fde_current_label, NULL);
2072           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2073           break;
2074
2075         case DW_CFA_advance_loc2:
2076           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2077                                 fde->dw_fde_current_label, NULL);
2078           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2079           break;
2080
2081         case DW_CFA_advance_loc4:
2082           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2083                                 fde->dw_fde_current_label, NULL);
2084           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2085           break;
2086
2087         case DW_CFA_MIPS_advance_loc8:
2088           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2089                                 fde->dw_fde_current_label, NULL);
2090           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2091           break;
2092
2093         case DW_CFA_offset_extended:
2094         case DW_CFA_def_cfa:
2095           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2096           dw2_asm_output_data_uleb128 (r, NULL);
2097           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2098           break;
2099
2100         case DW_CFA_offset_extended_sf:
2101         case DW_CFA_def_cfa_sf:
2102           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2103           dw2_asm_output_data_uleb128 (r, NULL);
2104           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2105           break;
2106
2107         case DW_CFA_restore_extended:
2108         case DW_CFA_undefined:
2109         case DW_CFA_same_value:
2110         case DW_CFA_def_cfa_register:
2111           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2112           dw2_asm_output_data_uleb128 (r, NULL);
2113           break;
2114
2115         case DW_CFA_register:
2116           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2117           dw2_asm_output_data_uleb128 (r, NULL);
2118           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2119           dw2_asm_output_data_uleb128 (r, NULL);
2120           break;
2121
2122         case DW_CFA_def_cfa_offset:
2123         case DW_CFA_GNU_args_size:
2124           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2125           break;
2126
2127         case DW_CFA_def_cfa_offset_sf:
2128           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2129           break;
2130
2131         case DW_CFA_GNU_window_save:
2132           break;
2133
2134         case DW_CFA_def_cfa_expression:
2135         case DW_CFA_expression:
2136           output_cfa_loc (cfi);
2137           break;
2138
2139         case DW_CFA_GNU_negative_offset_extended:
2140           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2141           gcc_unreachable ();
2142
2143         default:
2144           break;
2145         }
2146     }
2147 }
2148
2149 /* Output the call frame information used to record information
2150    that relates to calculating the frame pointer, and records the
2151    location of saved registers.  */
2152
2153 static void
2154 output_call_frame_info (int for_eh)
2155 {
2156   unsigned int i;
2157   dw_fde_ref fde;
2158   dw_cfi_ref cfi;
2159   char l1[20], l2[20], section_start_label[20];
2160   bool any_lsda_needed = false;
2161   char augmentation[6];
2162   int augmentation_size;
2163   int fde_encoding = DW_EH_PE_absptr;
2164   int per_encoding = DW_EH_PE_absptr;
2165   int lsda_encoding = DW_EH_PE_absptr;
2166   int return_reg;
2167
2168   /* Don't emit a CIE if there won't be any FDEs.  */
2169   if (fde_table_in_use == 0)
2170     return;
2171
2172   /* If we make FDEs linkonce, we may have to emit an empty label for
2173      an FDE that wouldn't otherwise be emitted.  We want to avoid
2174      having an FDE kept around when the function it refers to is
2175      discarded.  Example where this matters: a primary function
2176      template in C++ requires EH information, but an explicit
2177      specialization doesn't.  */
2178   if (TARGET_USES_WEAK_UNWIND_INFO
2179       && ! flag_asynchronous_unwind_tables
2180       && for_eh)
2181     for (i = 0; i < fde_table_in_use; i++)
2182       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2183           && !fde_table[i].uses_eh_lsda
2184           && ! DECL_WEAK (fde_table[i].decl))
2185         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2186                                       for_eh, /* empty */ 1);
2187
2188   /* If we don't have any functions we'll want to unwind out of, don't
2189      emit any EH unwind information.  Note that if exceptions aren't
2190      enabled, we won't have collected nothrow information, and if we
2191      asked for asynchronous tables, we always want this info.  */
2192   if (for_eh)
2193     {
2194       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2195
2196       for (i = 0; i < fde_table_in_use; i++)
2197         if (fde_table[i].uses_eh_lsda)
2198           any_eh_needed = any_lsda_needed = true;
2199         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2200           any_eh_needed = true;
2201         else if (! fde_table[i].nothrow
2202                  && ! fde_table[i].all_throwers_are_sibcalls)
2203           any_eh_needed = true;
2204
2205       if (! any_eh_needed)
2206         return;
2207     }
2208
2209   /* We're going to be generating comments, so turn on app.  */
2210   if (flag_debug_asm)
2211     app_enable ();
2212
2213   if (for_eh)
2214     switch_to_eh_frame_section ();
2215   else
2216     switch_to_section (debug_frame_section);
2217
2218   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2219   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2220
2221   /* Output the CIE.  */
2222   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2223   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2224   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2225                         "Length of Common Information Entry");
2226   ASM_OUTPUT_LABEL (asm_out_file, l1);
2227
2228   /* Now that the CIE pointer is PC-relative for EH,
2229      use 0 to identify the CIE.  */
2230   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2231                        (for_eh ? 0 : DW_CIE_ID),
2232                        "CIE Identifier Tag");
2233
2234   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2235
2236   augmentation[0] = 0;
2237   augmentation_size = 0;
2238   if (for_eh)
2239     {
2240       char *p;
2241
2242       /* Augmentation:
2243          z      Indicates that a uleb128 is present to size the
2244                 augmentation section.
2245          L      Indicates the encoding (and thus presence) of
2246                 an LSDA pointer in the FDE augmentation.
2247          R      Indicates a non-default pointer encoding for
2248                 FDE code pointers.
2249          P      Indicates the presence of an encoding + language
2250                 personality routine in the CIE augmentation.  */
2251
2252       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2253       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2254       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2255
2256       p = augmentation + 1;
2257       if (eh_personality_libfunc)
2258         {
2259           *p++ = 'P';
2260           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2261         }
2262       if (any_lsda_needed)
2263         {
2264           *p++ = 'L';
2265           augmentation_size += 1;
2266         }
2267       if (fde_encoding != DW_EH_PE_absptr)
2268         {
2269           *p++ = 'R';
2270           augmentation_size += 1;
2271         }
2272       if (p > augmentation + 1)
2273         {
2274           augmentation[0] = 'z';
2275           *p = '\0';
2276         }
2277
2278       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2279       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2280         {
2281           int offset = (  4             /* Length */
2282                         + 4             /* CIE Id */
2283                         + 1             /* CIE version */
2284                         + strlen (augmentation) + 1     /* Augmentation */
2285                         + size_of_uleb128 (1)           /* Code alignment */
2286                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2287                         + 1             /* RA column */
2288                         + 1             /* Augmentation size */
2289                         + 1             /* Personality encoding */ );
2290           int pad = -offset & (PTR_SIZE - 1);
2291
2292           augmentation_size += pad;
2293
2294           /* Augmentations should be small, so there's scarce need to
2295              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2296           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2297         }
2298     }
2299
2300   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2301   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2302   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2303                                "CIE Data Alignment Factor");
2304
2305   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2306   if (DW_CIE_VERSION == 1)
2307     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2308   else
2309     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2310
2311   if (augmentation[0])
2312     {
2313       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2314       if (eh_personality_libfunc)
2315         {
2316           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2317                                eh_data_format_name (per_encoding));
2318           dw2_asm_output_encoded_addr_rtx (per_encoding,
2319                                            eh_personality_libfunc,
2320                                            true, NULL);
2321         }
2322
2323       if (any_lsda_needed)
2324         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2325                              eh_data_format_name (lsda_encoding));
2326
2327       if (fde_encoding != DW_EH_PE_absptr)
2328         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2329                              eh_data_format_name (fde_encoding));
2330     }
2331
2332   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2333     output_cfi (cfi, NULL, for_eh);
2334
2335   /* Pad the CIE out to an address sized boundary.  */
2336   ASM_OUTPUT_ALIGN (asm_out_file,
2337                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2338   ASM_OUTPUT_LABEL (asm_out_file, l2);
2339
2340   /* Loop through all of the FDE's.  */
2341   for (i = 0; i < fde_table_in_use; i++)
2342     {
2343       fde = &fde_table[i];
2344
2345       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2346       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2347           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2348           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2349           && !fde->uses_eh_lsda)
2350         continue;
2351
2352       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2353       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2354       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2355       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2356       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2357                             "FDE Length");
2358       ASM_OUTPUT_LABEL (asm_out_file, l1);
2359
2360       if (for_eh)
2361         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2362       else
2363         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2364                                debug_frame_section, "FDE CIE offset");
2365
2366       if (for_eh)
2367         {
2368           rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2369           SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2370           dw2_asm_output_encoded_addr_rtx (fde_encoding,
2371                                            sym_ref,
2372                                            false,
2373                                            "FDE initial location");
2374           if (fde->dw_fde_switched_sections)
2375             {
2376               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode, 
2377                                       fde->dw_fde_unlikely_section_label);
2378               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode, 
2379                                       fde->dw_fde_hot_section_label);
2380               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2381               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2382               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2383                                                "FDE initial location");
2384               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2385                                     fde->dw_fde_hot_section_end_label,
2386                                     fde->dw_fde_hot_section_label,
2387                                     "FDE address range");
2388               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2389                                                "FDE initial location");
2390               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2391                                     fde->dw_fde_unlikely_section_end_label,
2392                                     fde->dw_fde_unlikely_section_label,
2393                                     "FDE address range");
2394             }
2395           else
2396             dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2397                                   fde->dw_fde_end, fde->dw_fde_begin,
2398                                   "FDE address range");
2399         }
2400       else
2401         {
2402           dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2403                                "FDE initial location");
2404           if (fde->dw_fde_switched_sections)
2405             {
2406               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2407                                    fde->dw_fde_hot_section_label,
2408                                    "FDE initial location");
2409               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2410                                     fde->dw_fde_hot_section_end_label,
2411                                     fde->dw_fde_hot_section_label,
2412                                     "FDE address range");
2413               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2414                                    fde->dw_fde_unlikely_section_label,
2415                                    "FDE initial location");
2416               dw2_asm_output_delta (DWARF2_ADDR_SIZE, 
2417                                     fde->dw_fde_unlikely_section_end_label,
2418                                     fde->dw_fde_unlikely_section_label,
2419                                     "FDE address range");
2420             }
2421           else
2422             dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2423                                   fde->dw_fde_end, fde->dw_fde_begin,
2424                                   "FDE address range");
2425         }
2426
2427       if (augmentation[0])
2428         {
2429           if (any_lsda_needed)
2430             {
2431               int size = size_of_encoded_value (lsda_encoding);
2432
2433               if (lsda_encoding == DW_EH_PE_aligned)
2434                 {
2435                   int offset = (  4             /* Length */
2436                                 + 4             /* CIE offset */
2437                                 + 2 * size_of_encoded_value (fde_encoding)
2438                                 + 1             /* Augmentation size */ );
2439                   int pad = -offset & (PTR_SIZE - 1);
2440
2441                   size += pad;
2442                   gcc_assert (size_of_uleb128 (size) == 1);
2443                 }
2444
2445               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2446
2447               if (fde->uses_eh_lsda)
2448                 {
2449                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2450                                                fde->funcdef_number);
2451                   dw2_asm_output_encoded_addr_rtx (
2452                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2453                         false, "Language Specific Data Area");
2454                 }
2455               else
2456                 {
2457                   if (lsda_encoding == DW_EH_PE_aligned)
2458                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2459                   dw2_asm_output_data
2460                     (size_of_encoded_value (lsda_encoding), 0,
2461                      "Language Specific Data Area (none)");
2462                 }
2463             }
2464           else
2465             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2466         }
2467
2468       /* Loop through the Call Frame Instructions associated with
2469          this FDE.  */
2470       fde->dw_fde_current_label = fde->dw_fde_begin;
2471       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2472         output_cfi (cfi, fde, for_eh);
2473
2474       /* Pad the FDE out to an address sized boundary.  */
2475       ASM_OUTPUT_ALIGN (asm_out_file,
2476                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2477       ASM_OUTPUT_LABEL (asm_out_file, l2);
2478     }
2479
2480   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2481     dw2_asm_output_data (4, 0, "End of Table");
2482 #ifdef MIPS_DEBUGGING_INFO
2483   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2484      get a value of 0.  Putting .align 0 after the label fixes it.  */
2485   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2486 #endif
2487
2488   /* Turn off app to make assembly quicker.  */
2489   if (flag_debug_asm)
2490     app_disable ();
2491 }
2492
2493 /* Output a marker (i.e. a label) for the beginning of a function, before
2494    the prologue.  */
2495
2496 void
2497 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2498                           const char *file ATTRIBUTE_UNUSED)
2499 {
2500   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2501   char * dup_label;
2502   dw_fde_ref fde;
2503
2504   current_function_func_begin_label = NULL;
2505
2506 #ifdef TARGET_UNWIND_INFO
2507   /* ??? current_function_func_begin_label is also used by except.c
2508      for call-site information.  We must emit this label if it might
2509      be used.  */
2510   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2511       && ! dwarf2out_do_frame ())
2512     return;
2513 #else
2514   if (! dwarf2out_do_frame ())
2515     return;
2516 #endif
2517
2518   switch_to_section (function_section (current_function_decl));
2519   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2520                                current_function_funcdef_no);
2521   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2522                           current_function_funcdef_no);
2523   dup_label = xstrdup (label);
2524   current_function_func_begin_label = dup_label;
2525
2526 #ifdef TARGET_UNWIND_INFO
2527   /* We can elide the fde allocation if we're not emitting debug info.  */
2528   if (! dwarf2out_do_frame ())
2529     return;
2530 #endif
2531
2532   /* Expand the fde table if necessary.  */
2533   if (fde_table_in_use == fde_table_allocated)
2534     {
2535       fde_table_allocated += FDE_TABLE_INCREMENT;
2536       fde_table = ggc_realloc (fde_table,
2537                                fde_table_allocated * sizeof (dw_fde_node));
2538       memset (fde_table + fde_table_in_use, 0,
2539               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2540     }
2541
2542   /* Record the FDE associated with this function.  */
2543   current_funcdef_fde = fde_table_in_use;
2544
2545   /* Add the new FDE at the end of the fde_table.  */
2546   fde = &fde_table[fde_table_in_use++];
2547   fde->decl = current_function_decl;
2548   fde->dw_fde_begin = dup_label;
2549   fde->dw_fde_current_label = NULL;
2550   fde->dw_fde_hot_section_label = NULL;
2551   fde->dw_fde_hot_section_end_label = NULL;
2552   fde->dw_fde_unlikely_section_label = NULL;
2553   fde->dw_fde_unlikely_section_end_label = NULL;
2554   fde->dw_fde_switched_sections = false;
2555   fde->dw_fde_end = NULL;
2556   fde->dw_fde_cfi = NULL;
2557   fde->funcdef_number = current_function_funcdef_no;
2558   fde->nothrow = TREE_NOTHROW (current_function_decl);
2559   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2560   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2561
2562   args_size = old_args_size = 0;
2563
2564   /* We only want to output line number information for the genuine dwarf2
2565      prologue case, not the eh frame case.  */
2566 #ifdef DWARF2_DEBUGGING_INFO
2567   if (file)
2568     dwarf2out_source_line (line, file);
2569 #endif
2570 }
2571
2572 /* Output a marker (i.e. a label) for the absolute end of the generated code
2573    for a function definition.  This gets called *after* the epilogue code has
2574    been generated.  */
2575
2576 void
2577 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2578                         const char *file ATTRIBUTE_UNUSED)
2579 {
2580   dw_fde_ref fde;
2581   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2582
2583   /* Output a label to mark the endpoint of the code generated for this
2584      function.  */
2585   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2586                                current_function_funcdef_no);
2587   ASM_OUTPUT_LABEL (asm_out_file, label);
2588   fde = &fde_table[fde_table_in_use - 1];
2589   fde->dw_fde_end = xstrdup (label);
2590 }
2591
2592 void
2593 dwarf2out_frame_init (void)
2594 {
2595   /* Allocate the initial hunk of the fde_table.  */
2596   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2597   fde_table_allocated = FDE_TABLE_INCREMENT;
2598   fde_table_in_use = 0;
2599
2600   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2601      sake of lookup_cfa.  */
2602
2603   /* On entry, the Canonical Frame Address is at SP.  */
2604   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2605
2606 #ifdef DWARF2_UNWIND_INFO
2607   if (DWARF2_UNWIND_INFO)
2608     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2609 #endif
2610 }
2611
2612 void
2613 dwarf2out_frame_finish (void)
2614 {
2615   /* Output call frame information.  */
2616   if (DWARF2_FRAME_INFO)
2617     output_call_frame_info (0);
2618
2619 #ifndef TARGET_UNWIND_INFO
2620   /* Output another copy for the unwinder.  */
2621   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2622     output_call_frame_info (1);
2623 #endif
2624 }
2625 #endif
2626 \f
2627 /* And now, the subset of the debugging information support code necessary
2628    for emitting location expressions.  */
2629
2630 /* We need some way to distinguish DW_OP_addr with a direct symbol
2631    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2632 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2633
2634
2635 typedef struct dw_val_struct *dw_val_ref;
2636 typedef struct die_struct *dw_die_ref;
2637 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2638 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2639
2640 /* Each DIE may have a series of attribute/value pairs.  Values
2641    can take on several forms.  The forms that are used in this
2642    implementation are listed below.  */
2643
2644 enum dw_val_class
2645 {
2646   dw_val_class_addr,
2647   dw_val_class_offset,
2648   dw_val_class_loc,
2649   dw_val_class_loc_list,
2650   dw_val_class_range_list,
2651   dw_val_class_const,
2652   dw_val_class_unsigned_const,
2653   dw_val_class_long_long,
2654   dw_val_class_vec,
2655   dw_val_class_flag,
2656   dw_val_class_die_ref,
2657   dw_val_class_fde_ref,
2658   dw_val_class_lbl_id,
2659   dw_val_class_lineptr,
2660   dw_val_class_str,
2661   dw_val_class_macptr
2662 };
2663
2664 /* Describe a double word constant value.  */
2665 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2666
2667 typedef struct dw_long_long_struct GTY(())
2668 {
2669   unsigned long hi;
2670   unsigned long low;
2671 }
2672 dw_long_long_const;
2673
2674 /* Describe a floating point constant value, or a vector constant value.  */
2675
2676 typedef struct dw_vec_struct GTY(())
2677 {
2678   unsigned char * GTY((length ("%h.length"))) array;
2679   unsigned length;
2680   unsigned elt_size;
2681 }
2682 dw_vec_const;
2683
2684 /* The dw_val_node describes an attribute's value, as it is
2685    represented internally.  */
2686
2687 typedef struct dw_val_struct GTY(())
2688 {
2689   enum dw_val_class val_class;
2690   union dw_val_struct_union
2691     {
2692       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2693       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2694       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2695       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2696       HOST_WIDE_INT GTY ((default)) val_int;
2697       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2698       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2699       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2700       struct dw_val_die_union
2701         {
2702           dw_die_ref die;
2703           int external;
2704         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2705       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2706       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2707       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2708       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2709     }
2710   GTY ((desc ("%1.val_class"))) v;
2711 }
2712 dw_val_node;
2713
2714 /* Locations in memory are described using a sequence of stack machine
2715    operations.  */
2716
2717 typedef struct dw_loc_descr_struct GTY(())
2718 {
2719   dw_loc_descr_ref dw_loc_next;
2720   enum dwarf_location_atom dw_loc_opc;
2721   dw_val_node dw_loc_oprnd1;
2722   dw_val_node dw_loc_oprnd2;
2723   int dw_loc_addr;
2724 }
2725 dw_loc_descr_node;
2726
2727 /* Location lists are ranges + location descriptions for that range,
2728    so you can track variables that are in different places over
2729    their entire life.  */
2730 typedef struct dw_loc_list_struct GTY(())
2731 {
2732   dw_loc_list_ref dw_loc_next;
2733   const char *begin; /* Label for begin address of range */
2734   const char *end;  /* Label for end address of range */
2735   char *ll_symbol; /* Label for beginning of location list.
2736                       Only on head of list */
2737   const char *section; /* Section this loclist is relative to */
2738   dw_loc_descr_ref expr;
2739 } dw_loc_list_node;
2740
2741 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2742
2743 static const char *dwarf_stack_op_name (unsigned);
2744 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2745                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2746 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2747 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2748 static unsigned long size_of_locs (dw_loc_descr_ref);
2749 static void output_loc_operands (dw_loc_descr_ref);
2750 static void output_loc_sequence (dw_loc_descr_ref);
2751
2752 /* Convert a DWARF stack opcode into its string name.  */
2753
2754 static const char *
2755 dwarf_stack_op_name (unsigned int op)
2756 {
2757   switch (op)
2758     {
2759     case DW_OP_addr:
2760     case INTERNAL_DW_OP_tls_addr:
2761       return "DW_OP_addr";
2762     case DW_OP_deref:
2763       return "DW_OP_deref";
2764     case DW_OP_const1u:
2765       return "DW_OP_const1u";
2766     case DW_OP_const1s:
2767       return "DW_OP_const1s";
2768     case DW_OP_const2u:
2769       return "DW_OP_const2u";
2770     case DW_OP_const2s:
2771       return "DW_OP_const2s";
2772     case DW_OP_const4u:
2773       return "DW_OP_const4u";
2774     case DW_OP_const4s:
2775       return "DW_OP_const4s";
2776     case DW_OP_const8u:
2777       return "DW_OP_const8u";
2778     case DW_OP_const8s:
2779       return "DW_OP_const8s";
2780     case DW_OP_constu:
2781       return "DW_OP_constu";
2782     case DW_OP_consts:
2783       return "DW_OP_consts";
2784     case DW_OP_dup:
2785       return "DW_OP_dup";
2786     case DW_OP_drop:
2787       return "DW_OP_drop";
2788     case DW_OP_over:
2789       return "DW_OP_over";
2790     case DW_OP_pick:
2791       return "DW_OP_pick";
2792     case DW_OP_swap:
2793       return "DW_OP_swap";
2794     case DW_OP_rot:
2795       return "DW_OP_rot";
2796     case DW_OP_xderef:
2797       return "DW_OP_xderef";
2798     case DW_OP_abs:
2799       return "DW_OP_abs";
2800     case DW_OP_and:
2801       return "DW_OP_and";
2802     case DW_OP_div:
2803       return "DW_OP_div";
2804     case DW_OP_minus:
2805       return "DW_OP_minus";
2806     case DW_OP_mod:
2807       return "DW_OP_mod";
2808     case DW_OP_mul:
2809       return "DW_OP_mul";
2810     case DW_OP_neg:
2811       return "DW_OP_neg";
2812     case DW_OP_not:
2813       return "DW_OP_not";
2814     case DW_OP_or:
2815       return "DW_OP_or";
2816     case DW_OP_plus:
2817       return "DW_OP_plus";
2818     case DW_OP_plus_uconst:
2819       return "DW_OP_plus_uconst";
2820     case DW_OP_shl:
2821       return "DW_OP_shl";
2822     case DW_OP_shr:
2823       return "DW_OP_shr";
2824     case DW_OP_shra:
2825       return "DW_OP_shra";
2826     case DW_OP_xor:
2827       return "DW_OP_xor";
2828     case DW_OP_bra:
2829       return "DW_OP_bra";
2830     case DW_OP_eq:
2831       return "DW_OP_eq";
2832     case DW_OP_ge:
2833       return "DW_OP_ge";
2834     case DW_OP_gt:
2835       return "DW_OP_gt";
2836     case DW_OP_le:
2837       return "DW_OP_le";
2838     case DW_OP_lt:
2839       return "DW_OP_lt";
2840     case DW_OP_ne:
2841       return "DW_OP_ne";
2842     case DW_OP_skip:
2843       return "DW_OP_skip";
2844     case DW_OP_lit0:
2845       return "DW_OP_lit0";
2846     case DW_OP_lit1:
2847       return "DW_OP_lit1";
2848     case DW_OP_lit2:
2849       return "DW_OP_lit2";
2850     case DW_OP_lit3:
2851       return "DW_OP_lit3";
2852     case DW_OP_lit4:
2853       return "DW_OP_lit4";
2854     case DW_OP_lit5:
2855       return "DW_OP_lit5";
2856     case DW_OP_lit6:
2857       return "DW_OP_lit6";
2858     case DW_OP_lit7:
2859       return "DW_OP_lit7";
2860     case DW_OP_lit8:
2861       return "DW_OP_lit8";
2862     case DW_OP_lit9:
2863       return "DW_OP_lit9";
2864     case DW_OP_lit10:
2865       return "DW_OP_lit10";
2866     case DW_OP_lit11:
2867       return "DW_OP_lit11";
2868     case DW_OP_lit12:
2869       return "DW_OP_lit12";
2870     case DW_OP_lit13:
2871       return "DW_OP_lit13";
2872     case DW_OP_lit14:
2873       return "DW_OP_lit14";
2874     case DW_OP_lit15:
2875       return "DW_OP_lit15";
2876     case DW_OP_lit16:
2877       return "DW_OP_lit16";
2878     case DW_OP_lit17:
2879       return "DW_OP_lit17";
2880     case DW_OP_lit18:
2881       return "DW_OP_lit18";
2882     case DW_OP_lit19:
2883       return "DW_OP_lit19";
2884     case DW_OP_lit20:
2885       return "DW_OP_lit20";
2886     case DW_OP_lit21:
2887       return "DW_OP_lit21";
2888     case DW_OP_lit22:
2889       return "DW_OP_lit22";
2890     case DW_OP_lit23:
2891       return "DW_OP_lit23";
2892     case DW_OP_lit24:
2893       return "DW_OP_lit24";
2894     case DW_OP_lit25:
2895       return "DW_OP_lit25";
2896     case DW_OP_lit26:
2897       return "DW_OP_lit26";
2898     case DW_OP_lit27:
2899       return "DW_OP_lit27";
2900     case DW_OP_lit28:
2901       return "DW_OP_lit28";
2902     case DW_OP_lit29:
2903       return "DW_OP_lit29";
2904     case DW_OP_lit30:
2905       return "DW_OP_lit30";
2906     case DW_OP_lit31:
2907       return "DW_OP_lit31";
2908     case DW_OP_reg0:
2909       return "DW_OP_reg0";
2910     case DW_OP_reg1:
2911       return "DW_OP_reg1";
2912     case DW_OP_reg2:
2913       return "DW_OP_reg2";
2914     case DW_OP_reg3:
2915       return "DW_OP_reg3";
2916     case DW_OP_reg4:
2917       return "DW_OP_reg4";
2918     case DW_OP_reg5:
2919       return "DW_OP_reg5";
2920     case DW_OP_reg6:
2921       return "DW_OP_reg6";
2922     case DW_OP_reg7:
2923       return "DW_OP_reg7";
2924     case DW_OP_reg8:
2925       return "DW_OP_reg8";
2926     case DW_OP_reg9:
2927       return "DW_OP_reg9";
2928     case DW_OP_reg10:
2929       return "DW_OP_reg10";
2930     case DW_OP_reg11:
2931       return "DW_OP_reg11";
2932     case DW_OP_reg12:
2933       return "DW_OP_reg12";
2934     case DW_OP_reg13:
2935       return "DW_OP_reg13";
2936     case DW_OP_reg14:
2937       return "DW_OP_reg14";
2938     case DW_OP_reg15:
2939       return "DW_OP_reg15";
2940     case DW_OP_reg16:
2941       return "DW_OP_reg16";
2942     case DW_OP_reg17:
2943       return "DW_OP_reg17";
2944     case DW_OP_reg18:
2945       return "DW_OP_reg18";
2946     case DW_OP_reg19:
2947       return "DW_OP_reg19";
2948     case DW_OP_reg20:
2949       return "DW_OP_reg20";
2950     case DW_OP_reg21:
2951       return "DW_OP_reg21";
2952     case DW_OP_reg22:
2953       return "DW_OP_reg22";
2954     case DW_OP_reg23:
2955       return "DW_OP_reg23";
2956     case DW_OP_reg24:
2957       return "DW_OP_reg24";
2958     case DW_OP_reg25:
2959       return "DW_OP_reg25";
2960     case DW_OP_reg26:
2961       return "DW_OP_reg26";
2962     case DW_OP_reg27:
2963       return "DW_OP_reg27";
2964     case DW_OP_reg28:
2965       return "DW_OP_reg28";
2966     case DW_OP_reg29:
2967       return "DW_OP_reg29";
2968     case DW_OP_reg30:
2969       return "DW_OP_reg30";
2970     case DW_OP_reg31:
2971       return "DW_OP_reg31";
2972     case DW_OP_breg0:
2973       return "DW_OP_breg0";
2974     case DW_OP_breg1:
2975       return "DW_OP_breg1";
2976     case DW_OP_breg2:
2977       return "DW_OP_breg2";
2978     case DW_OP_breg3:
2979       return "DW_OP_breg3";
2980     case DW_OP_breg4:
2981       return "DW_OP_breg4";
2982     case DW_OP_breg5:
2983       return "DW_OP_breg5";
2984     case DW_OP_breg6:
2985       return "DW_OP_breg6";
2986     case DW_OP_breg7:
2987       return "DW_OP_breg7";
2988     case DW_OP_breg8:
2989       return "DW_OP_breg8";
2990     case DW_OP_breg9:
2991       return "DW_OP_breg9";
2992     case DW_OP_breg10:
2993       return "DW_OP_breg10";
2994     case DW_OP_breg11:
2995       return "DW_OP_breg11";
2996     case DW_OP_breg12:
2997       return "DW_OP_breg12";
2998     case DW_OP_breg13:
2999       return "DW_OP_breg13";
3000     case DW_OP_breg14:
3001       return "DW_OP_breg14";
3002     case DW_OP_breg15:
3003       return "DW_OP_breg15";
3004     case DW_OP_breg16:
3005       return "DW_OP_breg16";
3006     case DW_OP_breg17:
3007       return "DW_OP_breg17";
3008     case DW_OP_breg18:
3009       return "DW_OP_breg18";
3010     case DW_OP_breg19:
3011       return "DW_OP_breg19";
3012     case DW_OP_breg20:
3013       return "DW_OP_breg20";
3014     case DW_OP_breg21:
3015       return "DW_OP_breg21";
3016     case DW_OP_breg22:
3017       return "DW_OP_breg22";
3018     case DW_OP_breg23:
3019       return "DW_OP_breg23";
3020     case DW_OP_breg24:
3021       return "DW_OP_breg24";
3022     case DW_OP_breg25:
3023       return "DW_OP_breg25";
3024     case DW_OP_breg26:
3025       return "DW_OP_breg26";
3026     case DW_OP_breg27:
3027       return "DW_OP_breg27";
3028     case DW_OP_breg28:
3029       return "DW_OP_breg28";
3030     case DW_OP_breg29:
3031       return "DW_OP_breg29";
3032     case DW_OP_breg30:
3033       return "DW_OP_breg30";
3034     case DW_OP_breg31:
3035       return "DW_OP_breg31";
3036     case DW_OP_regx:
3037       return "DW_OP_regx";
3038     case DW_OP_fbreg:
3039       return "DW_OP_fbreg";
3040     case DW_OP_bregx:
3041       return "DW_OP_bregx";
3042     case DW_OP_piece:
3043       return "DW_OP_piece";
3044     case DW_OP_deref_size:
3045       return "DW_OP_deref_size";
3046     case DW_OP_xderef_size:
3047       return "DW_OP_xderef_size";
3048     case DW_OP_nop:
3049       return "DW_OP_nop";
3050     case DW_OP_push_object_address:
3051       return "DW_OP_push_object_address";
3052     case DW_OP_call2:
3053       return "DW_OP_call2";
3054     case DW_OP_call4:
3055       return "DW_OP_call4";
3056     case DW_OP_call_ref:
3057       return "DW_OP_call_ref";
3058     case DW_OP_GNU_push_tls_address:
3059       return "DW_OP_GNU_push_tls_address";
3060     default:
3061       return "OP_<unknown>";
3062     }
3063 }
3064
3065 /* Return a pointer to a newly allocated location description.  Location
3066    descriptions are simple expression terms that can be strung
3067    together to form more complicated location (address) descriptions.  */
3068
3069 static inline dw_loc_descr_ref
3070 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3071                unsigned HOST_WIDE_INT oprnd2)
3072 {
3073   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3074
3075   descr->dw_loc_opc = op;
3076   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3077   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3078   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3079   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3080
3081   return descr;
3082 }
3083
3084 /* Add a location description term to a location description expression.  */
3085
3086 static inline void
3087 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3088 {
3089   dw_loc_descr_ref *d;
3090
3091   /* Find the end of the chain.  */
3092   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3093     ;
3094
3095   *d = descr;
3096 }
3097
3098 /* Return the size of a location descriptor.  */
3099
3100 static unsigned long
3101 size_of_loc_descr (dw_loc_descr_ref loc)
3102 {
3103   unsigned long size = 1;
3104
3105   switch (loc->dw_loc_opc)
3106     {
3107     case DW_OP_addr:
3108     case INTERNAL_DW_OP_tls_addr:
3109       size += DWARF2_ADDR_SIZE;
3110       break;
3111     case DW_OP_const1u:
3112     case DW_OP_const1s:
3113       size += 1;
3114       break;
3115     case DW_OP_const2u:
3116     case DW_OP_const2s:
3117       size += 2;
3118       break;
3119     case DW_OP_const4u:
3120     case DW_OP_const4s:
3121       size += 4;
3122       break;
3123     case DW_OP_const8u:
3124     case DW_OP_const8s:
3125       size += 8;
3126       break;
3127     case DW_OP_constu:
3128       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3129       break;
3130     case DW_OP_consts:
3131       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3132       break;
3133     case DW_OP_pick:
3134       size += 1;
3135       break;
3136     case DW_OP_plus_uconst:
3137       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3138       break;
3139     case DW_OP_skip:
3140     case DW_OP_bra:
3141       size += 2;
3142       break;
3143     case DW_OP_breg0:
3144     case DW_OP_breg1:
3145     case DW_OP_breg2:
3146     case DW_OP_breg3:
3147     case DW_OP_breg4:
3148     case DW_OP_breg5:
3149     case DW_OP_breg6:
3150     case DW_OP_breg7:
3151     case DW_OP_breg8:
3152     case DW_OP_breg9:
3153     case DW_OP_breg10:
3154     case DW_OP_breg11:
3155     case DW_OP_breg12:
3156     case DW_OP_breg13:
3157     case DW_OP_breg14:
3158     case DW_OP_breg15:
3159     case DW_OP_breg16:
3160     case DW_OP_breg17:
3161     case DW_OP_breg18:
3162     case DW_OP_breg19:
3163     case DW_OP_breg20:
3164     case DW_OP_breg21:
3165     case DW_OP_breg22:
3166     case DW_OP_breg23:
3167     case DW_OP_breg24:
3168     case DW_OP_breg25:
3169     case DW_OP_breg26:
3170     case DW_OP_breg27:
3171     case DW_OP_breg28:
3172     case DW_OP_breg29:
3173     case DW_OP_breg30:
3174     case DW_OP_breg31:
3175       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3176       break;
3177     case DW_OP_regx:
3178       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3179       break;
3180     case DW_OP_fbreg:
3181       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3182       break;
3183     case DW_OP_bregx:
3184       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3185       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3186       break;
3187     case DW_OP_piece:
3188       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3189       break;
3190     case DW_OP_deref_size:
3191     case DW_OP_xderef_size:
3192       size += 1;
3193       break;
3194     case DW_OP_call2:
3195       size += 2;
3196       break;
3197     case DW_OP_call4:
3198       size += 4;
3199       break;
3200     case DW_OP_call_ref:
3201       size += DWARF2_ADDR_SIZE;
3202       break;
3203     default:
3204       break;
3205     }
3206
3207   return size;
3208 }
3209
3210 /* Return the size of a series of location descriptors.  */
3211
3212 static unsigned long
3213 size_of_locs (dw_loc_descr_ref loc)
3214 {
3215   unsigned long size;
3216
3217   for (size = 0; loc != NULL; loc = loc->dw_loc_next)
3218     {
3219       loc->dw_loc_addr = size;
3220       size += size_of_loc_descr (loc);
3221     }
3222
3223   return size;
3224 }
3225
3226 /* Output location description stack opcode's operands (if any).  */
3227
3228 static void
3229 output_loc_operands (dw_loc_descr_ref loc)
3230 {
3231   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3232   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3233
3234   switch (loc->dw_loc_opc)
3235     {
3236 #ifdef DWARF2_DEBUGGING_INFO
3237     case DW_OP_addr:
3238       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3239       break;
3240     case DW_OP_const2u:
3241     case DW_OP_const2s:
3242       dw2_asm_output_data (2, val1->v.val_int, NULL);
3243       break;
3244     case DW_OP_const4u:
3245     case DW_OP_const4s:
3246       dw2_asm_output_data (4, val1->v.val_int, NULL);
3247       break;
3248     case DW_OP_const8u:
3249     case DW_OP_const8s:
3250       gcc_assert (HOST_BITS_PER_LONG >= 64);
3251       dw2_asm_output_data (8, val1->v.val_int, NULL);
3252       break;
3253     case DW_OP_skip:
3254     case DW_OP_bra:
3255       {
3256         int offset;
3257
3258         gcc_assert (val1->val_class == dw_val_class_loc);
3259         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3260
3261         dw2_asm_output_data (2, offset, NULL);
3262       }
3263       break;
3264 #else
3265     case DW_OP_addr:
3266     case DW_OP_const2u:
3267     case DW_OP_const2s:
3268     case DW_OP_const4u:
3269     case DW_OP_const4s:
3270     case DW_OP_const8u:
3271     case DW_OP_const8s:
3272     case DW_OP_skip:
3273     case DW_OP_bra:
3274       /* We currently don't make any attempt to make sure these are
3275          aligned properly like we do for the main unwind info, so
3276          don't support emitting things larger than a byte if we're
3277          only doing unwinding.  */
3278       gcc_unreachable ();
3279 #endif
3280     case DW_OP_const1u:
3281     case DW_OP_const1s:
3282       dw2_asm_output_data (1, val1->v.val_int, NULL);
3283       break;
3284     case DW_OP_constu:
3285       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3286       break;
3287     case DW_OP_consts:
3288       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3289       break;
3290     case DW_OP_pick:
3291       dw2_asm_output_data (1, val1->v.val_int, NULL);
3292       break;
3293     case DW_OP_plus_uconst:
3294       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3295       break;
3296     case DW_OP_breg0:
3297     case DW_OP_breg1:
3298     case DW_OP_breg2:
3299     case DW_OP_breg3:
3300     case DW_OP_breg4:
3301     case DW_OP_breg5:
3302     case DW_OP_breg6:
3303     case DW_OP_breg7:
3304     case DW_OP_breg8:
3305     case DW_OP_breg9:
3306     case DW_OP_breg10:
3307     case DW_OP_breg11:
3308     case DW_OP_breg12:
3309     case DW_OP_breg13:
3310     case DW_OP_breg14:
3311     case DW_OP_breg15:
3312     case DW_OP_breg16:
3313     case DW_OP_breg17:
3314     case DW_OP_breg18:
3315     case DW_OP_breg19:
3316     case DW_OP_breg20:
3317     case DW_OP_breg21:
3318     case DW_OP_breg22:
3319     case DW_OP_breg23:
3320     case DW_OP_breg24:
3321     case DW_OP_breg25:
3322     case DW_OP_breg26:
3323     case DW_OP_breg27:
3324     case DW_OP_breg28:
3325     case DW_OP_breg29:
3326     case DW_OP_breg30:
3327     case DW_OP_breg31:
3328       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3329       break;
3330     case DW_OP_regx:
3331       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3332       break;
3333     case DW_OP_fbreg:
3334       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3335       break;
3336     case DW_OP_bregx:
3337       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3338       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3339       break;
3340     case DW_OP_piece:
3341       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3342       break;
3343     case DW_OP_deref_size:
3344     case DW_OP_xderef_size:
3345       dw2_asm_output_data (1, val1->v.val_int, NULL);
3346       break;
3347
3348     case INTERNAL_DW_OP_tls_addr:
3349       if (targetm.asm_out.output_dwarf_dtprel)
3350         {
3351           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3352                                                DWARF2_ADDR_SIZE,
3353                                                val1->v.val_addr);
3354           fputc ('\n', asm_out_file);
3355         }
3356       else
3357         gcc_unreachable ();
3358       break;
3359
3360     default:
3361       /* Other codes have no operands.  */
3362       break;
3363     }
3364 }
3365
3366 /* Output a sequence of location operations.  */
3367
3368 static void
3369 output_loc_sequence (dw_loc_descr_ref loc)
3370 {
3371   for (; loc != NULL; loc = loc->dw_loc_next)
3372     {
3373       /* Output the opcode.  */
3374       dw2_asm_output_data (1, loc->dw_loc_opc,
3375                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3376
3377       /* Output the operand(s) (if any).  */
3378       output_loc_operands (loc);
3379     }
3380 }
3381
3382 /* This routine will generate the correct assembly data for a location
3383    description based on a cfi entry with a complex address.  */
3384
3385 static void
3386 output_cfa_loc (dw_cfi_ref cfi)
3387 {
3388   dw_loc_descr_ref loc;
3389   unsigned long size;
3390
3391   /* Output the size of the block.  */
3392   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3393   size = size_of_locs (loc);
3394   dw2_asm_output_data_uleb128 (size, NULL);
3395
3396   /* Now output the operations themselves.  */
3397   output_loc_sequence (loc);
3398 }
3399
3400 /* This function builds a dwarf location descriptor sequence from a
3401    dw_cfa_location, adding the given OFFSET to the result of the
3402    expression.  */
3403
3404 static struct dw_loc_descr_struct *
3405 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3406 {
3407   struct dw_loc_descr_struct *head, *tmp;
3408
3409   offset += cfa->offset;
3410
3411   if (cfa->indirect)
3412     {
3413       if (cfa->base_offset)
3414         {
3415           if (cfa->reg <= 31)
3416             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3417           else
3418             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3419         }
3420       else if (cfa->reg <= 31)
3421         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3422       else
3423         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3424
3425       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3426       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3427       add_loc_descr (&head, tmp);
3428       if (offset != 0)
3429         {
3430           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3431           add_loc_descr (&head, tmp);
3432         }
3433     }
3434   else
3435     {
3436       if (offset == 0)
3437         if (cfa->reg <= 31)
3438           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3439         else
3440           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3441       else if (cfa->reg <= 31)
3442         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3443       else
3444         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3445     }
3446
3447   return head;
3448 }
3449
3450 /* This function fills in aa dw_cfa_location structure from a dwarf location
3451    descriptor sequence.  */
3452
3453 static void
3454 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3455 {
3456   struct dw_loc_descr_struct *ptr;
3457   cfa->offset = 0;
3458   cfa->base_offset = 0;
3459   cfa->indirect = 0;
3460   cfa->reg = -1;
3461
3462   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3463     {
3464       enum dwarf_location_atom op = ptr->dw_loc_opc;
3465
3466       switch (op)
3467         {
3468         case DW_OP_reg0:
3469         case DW_OP_reg1:
3470         case DW_OP_reg2:
3471         case DW_OP_reg3:
3472         case DW_OP_reg4:
3473         case DW_OP_reg5:
3474         case DW_OP_reg6:
3475         case DW_OP_reg7:
3476         case DW_OP_reg8:
3477         case DW_OP_reg9:
3478         case DW_OP_reg10:
3479         case DW_OP_reg11:
3480         case DW_OP_reg12:
3481         case DW_OP_reg13:
3482         case DW_OP_reg14:
3483         case DW_OP_reg15:
3484         case DW_OP_reg16:
3485         case DW_OP_reg17:
3486         case DW_OP_reg18:
3487         case DW_OP_reg19:
3488         case DW_OP_reg20:
3489         case DW_OP_reg21:
3490         case DW_OP_reg22:
3491         case DW_OP_reg23:
3492         case DW_OP_reg24:
3493         case DW_OP_reg25:
3494         case DW_OP_reg26:
3495         case DW_OP_reg27:
3496         case DW_OP_reg28:
3497         case DW_OP_reg29:
3498         case DW_OP_reg30:
3499         case DW_OP_reg31:
3500           cfa->reg = op - DW_OP_reg0;
3501           break;
3502         case DW_OP_regx:
3503           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3504           break;
3505         case DW_OP_breg0:
3506         case DW_OP_breg1:
3507         case DW_OP_breg2:
3508         case DW_OP_breg3:
3509         case DW_OP_breg4:
3510         case DW_OP_breg5:
3511         case DW_OP_breg6:
3512         case DW_OP_breg7:
3513         case DW_OP_breg8:
3514         case DW_OP_breg9:
3515         case DW_OP_breg10:
3516         case DW_OP_breg11:
3517         case DW_OP_breg12:
3518         case DW_OP_breg13:
3519         case DW_OP_breg14:
3520         case DW_OP_breg15:
3521         case DW_OP_breg16:
3522         case DW_OP_breg17:
3523         case DW_OP_breg18:
3524         case DW_OP_breg19:
3525         case DW_OP_breg20:
3526         case DW_OP_breg21:
3527         case DW_OP_breg22:
3528         case DW_OP_breg23:
3529         case DW_OP_breg24:
3530         case DW_OP_breg25:
3531         case DW_OP_breg26:
3532         case DW_OP_breg27:
3533         case DW_OP_breg28:
3534         case DW_OP_breg29:
3535         case DW_OP_breg30:
3536         case DW_OP_breg31:
3537           cfa->reg = op - DW_OP_breg0;
3538           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3539           break;
3540         case DW_OP_bregx:
3541           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3542           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3543           break;
3544         case DW_OP_deref:
3545           cfa->indirect = 1;
3546           break;
3547         case DW_OP_plus_uconst:
3548           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3549           break;
3550         default:
3551           internal_error ("DW_LOC_OP %s not implemented",
3552                           dwarf_stack_op_name (ptr->dw_loc_opc));
3553         }
3554     }
3555 }
3556 #endif /* .debug_frame support */
3557 \f
3558 /* And now, the support for symbolic debugging information.  */
3559 #ifdef DWARF2_DEBUGGING_INFO
3560
3561 /* .debug_str support.  */
3562 static int output_indirect_string (void **, void *);
3563
3564 static void dwarf2out_init (const char *);
3565 static void dwarf2out_finish (const char *);
3566 static void dwarf2out_define (unsigned int, const char *);
3567 static void dwarf2out_undef (unsigned int, const char *);
3568 static void dwarf2out_start_source_file (unsigned, const char *);
3569 static void dwarf2out_end_source_file (unsigned);
3570 static void dwarf2out_begin_block (unsigned, unsigned);
3571 static void dwarf2out_end_block (unsigned, unsigned);
3572 static bool dwarf2out_ignore_block (tree);
3573 static void dwarf2out_global_decl (tree);
3574 static void dwarf2out_type_decl (tree, int);
3575 static void dwarf2out_imported_module_or_decl (tree, tree);
3576 static void dwarf2out_abstract_function (tree);
3577 static void dwarf2out_var_location (rtx);
3578 static void dwarf2out_begin_function (tree);
3579 static void dwarf2out_switch_text_section (void);
3580
3581 /* The debug hooks structure.  */
3582
3583 const struct gcc_debug_hooks dwarf2_debug_hooks =
3584 {
3585   dwarf2out_init,
3586   dwarf2out_finish,
3587   dwarf2out_define,
3588   dwarf2out_undef,
3589   dwarf2out_start_source_file,
3590   dwarf2out_end_source_file,
3591   dwarf2out_begin_block,
3592   dwarf2out_end_block,
3593   dwarf2out_ignore_block,
3594   dwarf2out_source_line,
3595   dwarf2out_begin_prologue,
3596   debug_nothing_int_charstar,   /* end_prologue */
3597   dwarf2out_end_epilogue,
3598   dwarf2out_begin_function,
3599   debug_nothing_int,            /* end_function */
3600   dwarf2out_decl,               /* function_decl */
3601   dwarf2out_global_decl,
3602   dwarf2out_type_decl,          /* type_decl */
3603   dwarf2out_imported_module_or_decl,
3604   debug_nothing_tree,           /* deferred_inline_function */
3605   /* The DWARF 2 backend tries to reduce debugging bloat by not
3606      emitting the abstract description of inline functions until
3607      something tries to reference them.  */
3608   dwarf2out_abstract_function,  /* outlining_inline_function */
3609   debug_nothing_rtx,            /* label */
3610   debug_nothing_int,            /* handle_pch */
3611   dwarf2out_var_location,
3612   dwarf2out_switch_text_section,
3613   1                             /* start_end_main_source_file */
3614 };
3615 #endif
3616 \f
3617 /* NOTE: In the comments in this file, many references are made to
3618    "Debugging Information Entries".  This term is abbreviated as `DIE'
3619    throughout the remainder of this file.  */
3620
3621 /* An internal representation of the DWARF output is built, and then
3622    walked to generate the DWARF debugging info.  The walk of the internal
3623    representation is done after the entire program has been compiled.
3624    The types below are used to describe the internal representation.  */
3625
3626 /* Various DIE's use offsets relative to the beginning of the
3627    .debug_info section to refer to each other.  */
3628
3629 typedef long int dw_offset;
3630
3631 /* Define typedefs here to avoid circular dependencies.  */
3632
3633 typedef struct dw_attr_struct *dw_attr_ref;
3634 typedef struct dw_line_info_struct *dw_line_info_ref;
3635 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3636 typedef struct pubname_struct *pubname_ref;
3637 typedef struct dw_ranges_struct *dw_ranges_ref;
3638
3639 /* Each entry in the line_info_table maintains the file and
3640    line number associated with the label generated for that
3641    entry.  The label gives the PC value associated with
3642    the line number entry.  */
3643
3644 typedef struct dw_line_info_struct GTY(())
3645 {
3646   unsigned long dw_file_num;
3647   unsigned long dw_line_num;
3648 }
3649 dw_line_info_entry;
3650
3651 /* Line information for functions in separate sections; each one gets its
3652    own sequence.  */
3653 typedef struct dw_separate_line_info_struct GTY(())
3654 {
3655   unsigned long dw_file_num;
3656   unsigned long dw_line_num;
3657   unsigned long function;
3658 }
3659 dw_separate_line_info_entry;
3660
3661 /* Each DIE attribute has a field specifying the attribute kind,
3662    a link to the next attribute in the chain, and an attribute value.
3663    Attributes are typically linked below the DIE they modify.  */
3664
3665 typedef struct dw_attr_struct GTY(())
3666 {
3667   enum dwarf_attribute dw_attr;
3668   dw_attr_ref dw_attr_next;
3669   dw_val_node dw_attr_val;
3670 }
3671 dw_attr_node;
3672
3673 /* The Debugging Information Entry (DIE) structure */
3674
3675 typedef struct die_struct GTY(())
3676 {
3677   enum dwarf_tag die_tag;
3678   char *die_symbol;
3679   dw_attr_ref die_attr;
3680   dw_die_ref die_parent;
3681   dw_die_ref die_child;
3682   dw_die_ref die_sib;
3683   dw_die_ref die_definition; /* ref from a specification to its definition */
3684   dw_offset die_offset;
3685   unsigned long die_abbrev;
3686   int die_mark;
3687   unsigned int decl_id;
3688 }
3689 die_node;
3690
3691 /* The pubname structure */
3692
3693 typedef struct pubname_struct GTY(())
3694 {
3695   dw_die_ref die;
3696   char *name;
3697 }
3698 pubname_entry;
3699
3700 struct dw_ranges_struct GTY(())
3701 {
3702   int block_num;
3703 };
3704
3705 /* The limbo die list structure.  */
3706 typedef struct limbo_die_struct GTY(())
3707 {
3708   dw_die_ref die;
3709   tree created_for;
3710   struct limbo_die_struct *next;
3711 }
3712 limbo_die_node;
3713
3714 /* How to start an assembler comment.  */
3715 #ifndef ASM_COMMENT_START
3716 #define ASM_COMMENT_START ";#"
3717 #endif
3718
3719 /* Define a macro which returns nonzero for a TYPE_DECL which was
3720    implicitly generated for a tagged type.
3721
3722    Note that unlike the gcc front end (which generates a NULL named
3723    TYPE_DECL node for each complete tagged type, each array type, and
3724    each function type node created) the g++ front end generates a
3725    _named_ TYPE_DECL node for each tagged type node created.
3726    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3727    generate a DW_TAG_typedef DIE for them.  */
3728
3729 #define TYPE_DECL_IS_STUB(decl)                         \
3730   (DECL_NAME (decl) == NULL_TREE                        \
3731    || (DECL_ARTIFICIAL (decl)                           \
3732        && is_tagged_type (TREE_TYPE (decl))             \
3733        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3734            /* This is necessary for stub decls that     \
3735               appear in nested inline functions.  */    \
3736            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3737                && (decl_ultimate_origin (decl)          \
3738                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3739
3740 /* Information concerning the compilation unit's programming
3741    language, and compiler version.  */
3742
3743 /* Fixed size portion of the DWARF compilation unit header.  */
3744 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3745   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3746
3747 /* Fixed size portion of public names info.  */
3748 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3749
3750 /* Fixed size portion of the address range info.  */
3751 #define DWARF_ARANGES_HEADER_SIZE                                       \
3752   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3753                 DWARF2_ADDR_SIZE * 2)                                   \
3754    - DWARF_INITIAL_LENGTH_SIZE)
3755
3756 /* Size of padding portion in the address range info.  It must be
3757    aligned to twice the pointer size.  */
3758 #define DWARF_ARANGES_PAD_SIZE \
3759   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3760                 DWARF2_ADDR_SIZE * 2) \
3761    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3762
3763 /* Use assembler line directives if available.  */
3764 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3765 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3766 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3767 #else
3768 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3769 #endif
3770 #endif
3771
3772 /* Minimum line offset in a special line info. opcode.
3773    This value was chosen to give a reasonable range of values.  */
3774 #define DWARF_LINE_BASE  -10
3775
3776 /* First special line opcode - leave room for the standard opcodes.  */
3777 #define DWARF_LINE_OPCODE_BASE  10
3778
3779 /* Range of line offsets in a special line info. opcode.  */
3780 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3781
3782 /* Flag that indicates the initial value of the is_stmt_start flag.
3783    In the present implementation, we do not mark any lines as
3784    the beginning of a source statement, because that information
3785    is not made available by the GCC front-end.  */
3786 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3787
3788 #ifdef DWARF2_DEBUGGING_INFO
3789 /* This location is used by calc_die_sizes() to keep track
3790    the offset of each DIE within the .debug_info section.  */
3791 static unsigned long next_die_offset;
3792 #endif
3793
3794 /* Record the root of the DIE's built for the current compilation unit.  */
3795 static GTY(()) dw_die_ref comp_unit_die;
3796
3797 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3798 static GTY(()) limbo_die_node *limbo_die_list;
3799
3800 /* Filenames referenced by this compilation unit.  */
3801 static GTY(()) varray_type file_table;
3802 static GTY(()) varray_type file_table_emitted;
3803 static GTY(()) size_t file_table_last_lookup_index;
3804
3805 /* A hash table of references to DIE's that describe declarations.
3806    The key is a DECL_UID() which is a unique number identifying each decl.  */
3807 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3808
3809 /* Node of the variable location list.  */
3810 struct var_loc_node GTY ((chain_next ("%h.next")))
3811 {
3812   rtx GTY (()) var_loc_note;
3813   const char * GTY (()) label;
3814   const char * GTY (()) section_label;
3815   struct var_loc_node * GTY (()) next;
3816 };
3817
3818 /* Variable location list.  */
3819 struct var_loc_list_def GTY (())
3820 {
3821   struct var_loc_node * GTY (()) first;
3822
3823   /* Do not mark the last element of the chained list because
3824      it is marked through the chain.  */
3825   struct var_loc_node * GTY ((skip ("%h"))) last;
3826
3827   /* DECL_UID of the variable decl.  */
3828   unsigned int decl_id;
3829 };
3830 typedef struct var_loc_list_def var_loc_list;
3831
3832
3833 /* Table of decl location linked lists.  */
3834 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3835
3836 /* A pointer to the base of a list of references to DIE's that
3837    are uniquely identified by their tag, presence/absence of
3838    children DIE's, and list of attribute/value pairs.  */
3839 static GTY((length ("abbrev_die_table_allocated")))
3840   dw_die_ref *abbrev_die_table;
3841
3842 /* Number of elements currently allocated for abbrev_die_table.  */
3843 static GTY(()) unsigned abbrev_die_table_allocated;
3844
3845 /* Number of elements in type_die_table currently in use.  */
3846 static GTY(()) unsigned abbrev_die_table_in_use;
3847
3848 /* Size (in elements) of increments by which we may expand the
3849    abbrev_die_table.  */
3850 #define ABBREV_DIE_TABLE_INCREMENT 256
3851
3852 /* A pointer to the base of a table that contains line information
3853    for each source code line in .text in the compilation unit.  */
3854 static GTY((length ("line_info_table_allocated")))
3855      dw_line_info_ref line_info_table;
3856
3857 /* Number of elements currently allocated for line_info_table.  */
3858 static GTY(()) unsigned line_info_table_allocated;
3859
3860 /* Number of elements in line_info_table currently in use.  */
3861 static GTY(()) unsigned line_info_table_in_use;
3862
3863 /* True if the compilation unit places functions in more than one section.  */
3864 static GTY(()) bool have_multiple_function_sections = false;
3865
3866 /* A pointer to the base of a table that contains line information
3867    for each source code line outside of .text in the compilation unit.  */
3868 static GTY ((length ("separate_line_info_table_allocated")))
3869      dw_separate_line_info_ref separate_line_info_table;
3870
3871 /* Number of elements currently allocated for separate_line_info_table.  */
3872 static GTY(()) unsigned separate_line_info_table_allocated;
3873
3874 /* Number of elements in separate_line_info_table currently in use.  */
3875 static GTY(()) unsigned separate_line_info_table_in_use;
3876
3877 /* Size (in elements) of increments by which we may expand the
3878    line_info_table.  */
3879 #define LINE_INFO_TABLE_INCREMENT 1024
3880
3881 /* A pointer to the base of a table that contains a list of publicly
3882    accessible names.  */
3883 static GTY ((length ("pubname_table_allocated"))) pubname_ref pubname_table;
3884
3885 /* Number of elements currently allocated for pubname_table.  */
3886 static GTY(()) unsigned pubname_table_allocated;
3887
3888 /* Number of elements in pubname_table currently in use.  */
3889 static GTY(()) unsigned pubname_table_in_use;
3890
3891 /* Size (in elements) of increments by which we may expand the
3892    pubname_table.  */
3893 #define PUBNAME_TABLE_INCREMENT 64
3894
3895 /* Array of dies for which we should generate .debug_arange info.  */
3896 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3897
3898 /* Number of elements currently allocated for arange_table.  */
3899 static GTY(()) unsigned arange_table_allocated;
3900
3901 /* Number of elements in arange_table currently in use.  */
3902 static GTY(()) unsigned arange_table_in_use;
3903
3904 /* Size (in elements) of increments by which we may expand the
3905    arange_table.  */
3906 #define ARANGE_TABLE_INCREMENT 64
3907
3908 /* Array of dies for which we should generate .debug_ranges info.  */
3909 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3910
3911 /* Number of elements currently allocated for ranges_table.  */
3912 static GTY(()) unsigned ranges_table_allocated;
3913
3914 /* Number of elements in ranges_table currently in use.  */
3915 static GTY(()) unsigned ranges_table_in_use;
3916
3917 /* Size (in elements) of increments by which we may expand the
3918    ranges_table.  */
3919 #define RANGES_TABLE_INCREMENT 64
3920
3921 /* Whether we have location lists that need outputting */
3922 static GTY(()) bool have_location_lists;
3923
3924 /* Unique label counter.  */
3925 static GTY(()) unsigned int loclabel_num;
3926
3927 #ifdef DWARF2_DEBUGGING_INFO
3928 /* Record whether the function being analyzed contains inlined functions.  */
3929 static int current_function_has_inlines;
3930 #endif
3931 #if 0 && defined (MIPS_DEBUGGING_INFO)
3932 static int comp_unit_has_inlines;
3933 #endif
3934
3935 /* Number of file tables emitted in maybe_emit_file().  */
3936 static GTY(()) int emitcount = 0;
3937
3938 /* Number of internal labels generated by gen_internal_sym().  */
3939 static GTY(()) int label_num;
3940
3941 #ifdef DWARF2_DEBUGGING_INFO
3942
3943 /* Offset from the "steady-state frame pointer" to the frame base,
3944    within the current function.  */
3945 static HOST_WIDE_INT frame_pointer_fb_offset;
3946
3947 /* Forward declarations for functions defined in this file.  */
3948
3949 static int is_pseudo_reg (rtx);
3950 static tree type_main_variant (tree);
3951 static int is_tagged_type (tree);
3952 static const char *dwarf_tag_name (unsigned);
3953 static const char *dwarf_attr_name (unsigned);
3954 static const char *dwarf_form_name (unsigned);
3955 static tree decl_ultimate_origin (tree);
3956 static tree block_ultimate_origin (tree);
3957 static tree decl_class_context (tree);
3958 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3959 static inline enum dw_val_class AT_class (dw_attr_ref);
3960 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3961 static inline unsigned AT_flag (dw_attr_ref);
3962 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3963 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3964 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3965 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3966 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
3967                               unsigned long);
3968 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3969                                unsigned int, unsigned char *);
3970 static hashval_t debug_str_do_hash (const void *);
3971 static int debug_str_eq (const void *, const void *);
3972 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3973 static inline const char *AT_string (dw_attr_ref);
3974 static int AT_string_form (dw_attr_ref);
3975 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3976 static void add_AT_specification (dw_die_ref, dw_die_ref);
3977 static inline dw_die_ref AT_ref (dw_attr_ref);
3978 static inline int AT_ref_external (dw_attr_ref);
3979 static inline void set_AT_ref_external (dw_attr_ref, int);
3980 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3981 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3982 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3983 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3984                              dw_loc_list_ref);
3985 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3986 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3987 static inline rtx AT_addr (dw_attr_ref);
3988 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3989 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3990 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3991 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3992                            unsigned HOST_WIDE_INT);
3993 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3994                                unsigned long);
3995 static inline const char *AT_lbl (dw_attr_ref);
3996 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3997 static const char *get_AT_low_pc (dw_die_ref);
3998 static const char *get_AT_hi_pc (dw_die_ref);
3999 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4000 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4001 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4002 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4003 static bool is_c_family (void);
4004 static bool is_cxx (void);
4005 static bool is_java (void);
4006 static bool is_fortran (void);
4007 static bool is_ada (void);
4008 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4009 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4010 static inline void free_die (dw_die_ref);
4011 static void remove_children (dw_die_ref);
4012 static void add_child_die (dw_die_ref, dw_die_ref);
4013 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4014 static dw_die_ref lookup_type_die (tree);
4015 static void equate_type_number_to_die (tree, dw_die_ref);
4016 static hashval_t decl_die_table_hash (const void *);
4017 static int decl_die_table_eq (const void *, const void *);
4018 static dw_die_ref lookup_decl_die (tree);
4019 static hashval_t decl_loc_table_hash (const void *);
4020 static int decl_loc_table_eq (const void *, const void *);
4021 static var_loc_list *lookup_decl_loc (tree);
4022 static void equate_decl_number_to_die (tree, dw_die_ref);
4023 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4024 static void print_spaces (FILE *);
4025 static void print_die (dw_die_ref, FILE *);
4026 static void print_dwarf_line_table (FILE *);
4027 static void reverse_die_lists (dw_die_ref);
4028 static void reverse_all_dies (dw_die_ref);
4029 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4030 static dw_die_ref pop_compile_unit (dw_die_ref);
4031 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4032 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4033 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4034 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4035 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4036 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4037 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4038 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4039 static void compute_section_prefix (dw_die_ref);
4040 static int is_type_die (dw_die_ref);
4041 static int is_comdat_die (dw_die_ref);
4042 static int is_symbol_die (dw_die_ref);
4043 static void assign_symbol_names (dw_die_ref);
4044 static void break_out_includes (dw_die_ref);
4045 static hashval_t htab_cu_hash (const void *);
4046 static int htab_cu_eq (const void *, const void *);
4047 static void htab_cu_del (void *);
4048 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4049 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4050 static void add_sibling_attributes (dw_die_ref);
4051 static void build_abbrev_table (dw_die_ref);
4052 static void output_location_lists (dw_die_ref);
4053 static int constant_size (long unsigned);
4054 static unsigned long size_of_die (dw_die_ref);
4055 static void calc_die_sizes (dw_die_ref);
4056 static void mark_dies (dw_die_ref);
4057 static void unmark_dies (dw_die_ref);
4058 static void unmark_all_dies (dw_die_ref);
4059 static unsigned long size_of_pubnames (void);
4060 static unsigned long size_of_aranges (void);
4061 static enum dwarf_form value_format (dw_attr_ref);
4062 static void output_value_format (dw_attr_ref);
4063 static void output_abbrev_section (void);
4064 static void output_die_symbol (dw_die_ref);
4065 static void output_die (dw_die_ref);
4066 static void output_compilation_unit_header (void);
4067 static void output_comp_unit (dw_die_ref, int);
4068 static const char *dwarf2_name (tree, int);
4069 static void add_pubname (tree, dw_die_ref);
4070 static void output_pubnames (void);
4071 static void add_arange (tree, dw_die_ref);
4072 static void output_aranges (void);
4073 static unsigned int add_ranges (tree);
4074 static void output_ranges (void);
4075 static void output_line_info (void);
4076 static void output_file_names (void);
4077 static dw_die_ref base_type_die (tree);
4078 static tree root_type (tree);
4079 static int is_base_type (tree);
4080 static bool is_subrange_type (tree);
4081 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4082 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4083 static int type_is_enum (tree);
4084 static unsigned int dbx_reg_number (rtx);
4085 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4086 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4087 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4088 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4089 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4090 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4091 static int is_based_loc (rtx);
4092 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4093 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4094 static dw_loc_descr_ref loc_descriptor (rtx);
4095 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4096 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4097 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4098 static tree field_type (tree);
4099 static unsigned int simple_type_align_in_bits (tree);
4100 static unsigned int simple_decl_align_in_bits (tree);
4101 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4102 static HOST_WIDE_INT field_byte_offset (tree);
4103 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4104                                          dw_loc_descr_ref);
4105 static void add_data_member_location_attribute (dw_die_ref, tree);
4106 static void add_const_value_attribute (dw_die_ref, rtx);
4107 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4108 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4109 static void insert_float (rtx, unsigned char *);
4110 static rtx rtl_for_decl_location (tree);
4111 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4112                                                    enum dwarf_attribute);
4113 static void tree_add_const_value_attribute (dw_die_ref, tree);
4114 static void add_name_attribute (dw_die_ref, const char *);
4115 static void add_comp_dir_attribute (dw_die_ref);
4116 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4117 static void add_subscript_info (dw_die_ref, tree);
4118 static void add_byte_size_attribute (dw_die_ref, tree);
4119 static void add_bit_offset_attribute (dw_die_ref, tree);
4120 static void add_bit_size_attribute (dw_die_ref, tree);
4121 static void add_prototyped_attribute (dw_die_ref, tree);
4122 static void add_abstract_origin_attribute (dw_die_ref, tree);
4123 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4124 static void add_src_coords_attributes (dw_die_ref, tree);
4125 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4126 static void push_decl_scope (tree);
4127 static void pop_decl_scope (void);
4128 static dw_die_ref scope_die_for (tree, dw_die_ref);
4129 static inline int local_scope_p (dw_die_ref);
4130 static inline int class_or_namespace_scope_p (dw_die_ref);
4131 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4132 static void add_calling_convention_attribute (dw_die_ref, tree);
4133 static const char *type_tag (tree);
4134 static tree member_declared_type (tree);
4135 #if 0
4136 static const char *decl_start_label (tree);
4137 #endif
4138 static void gen_array_type_die (tree, dw_die_ref);
4139 #if 0
4140 static void gen_entry_point_die (tree, dw_die_ref);
4141 #endif
4142 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4143 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4144 static void gen_inlined_union_type_die (tree, dw_die_ref);
4145 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4146 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4147 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4148 static void gen_formal_types_die (tree, dw_die_ref);
4149 static void gen_subprogram_die (tree, dw_die_ref);
4150 static void gen_variable_die (tree, dw_die_ref);
4151 static void gen_label_die (tree, dw_die_ref);
4152 static void gen_lexical_block_die (tree, dw_die_ref, int);
4153 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4154 static void gen_field_die (tree, dw_die_ref);
4155 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4156 static dw_die_ref gen_compile_unit_die (const char *);
4157 static void gen_inheritance_die (tree, tree, dw_die_ref);
4158 static void gen_member_die (tree, dw_die_ref);
4159 static void gen_struct_or_union_type_die (tree, dw_die_ref);
4160 static void gen_subroutine_type_die (tree, dw_die_ref);
4161 static void gen_typedef_die (tree, dw_die_ref);
4162 static void gen_type_die (tree, dw_die_ref);
4163 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4164 static void gen_block_die (tree, dw_die_ref, int);
4165 static void decls_for_scope (tree, dw_die_ref, int);
4166 static int is_redundant_typedef (tree);
4167 static void gen_namespace_die (tree);
4168 static void gen_decl_die (tree, dw_die_ref);
4169 static dw_die_ref force_decl_die (tree);
4170 static dw_die_ref force_type_die (tree);
4171 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4172 static void declare_in_namespace (tree, dw_die_ref);
4173 static unsigned lookup_filename (const char *);
4174 static void init_file_table (void);
4175 static void retry_incomplete_types (void);
4176 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4177 static void splice_child_die (dw_die_ref, dw_die_ref);
4178 static int file_info_cmp (const void *, const void *);
4179 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4180                                      const char *, const char *, unsigned);
4181 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4182                                        const char *, const char *,
4183                                        const char *);
4184 static void output_loc_list (dw_loc_list_ref);
4185 static char *gen_internal_sym (const char *);
4186
4187 static void prune_unmark_dies (dw_die_ref);
4188 static void prune_unused_types_mark (dw_die_ref, int);
4189 static void prune_unused_types_walk (dw_die_ref);
4190 static void prune_unused_types_walk_attribs (dw_die_ref);
4191 static void prune_unused_types_prune (dw_die_ref);
4192 static void prune_unused_types (void);
4193 static int maybe_emit_file (int);
4194
4195 /* Section names used to hold DWARF debugging information.  */
4196 #ifndef DEBUG_INFO_SECTION
4197 #define DEBUG_INFO_SECTION      ".debug_info"
4198 #endif
4199 #ifndef DEBUG_ABBREV_SECTION
4200 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4201 #endif
4202 #ifndef DEBUG_ARANGES_SECTION
4203 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4204 #endif
4205 #ifndef DEBUG_MACINFO_SECTION
4206 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4207 #endif
4208 #ifndef DEBUG_LINE_SECTION
4209 #define DEBUG_LINE_SECTION      ".debug_line"
4210 #endif
4211 #ifndef DEBUG_LOC_SECTION
4212 #define DEBUG_LOC_SECTION       ".debug_loc"
4213 #endif
4214 #ifndef DEBUG_PUBNAMES_SECTION
4215 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4216 #endif
4217 #ifndef DEBUG_STR_SECTION
4218 #define DEBUG_STR_SECTION       ".debug_str"
4219 #endif
4220 #ifndef DEBUG_RANGES_SECTION
4221 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4222 #endif
4223
4224 /* Standard ELF section names for compiled code and data.  */
4225 #ifndef TEXT_SECTION_NAME
4226 #define TEXT_SECTION_NAME       ".text"
4227 #endif
4228
4229 /* Section flags for .debug_str section.  */
4230 #define DEBUG_STR_SECTION_FLAGS \
4231   (HAVE_GAS_SHF_MERGE && flag_merge_constants                   \
4232    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4233    : SECTION_DEBUG)
4234
4235 /* Labels we insert at beginning sections we can reference instead of
4236    the section names themselves.  */
4237
4238 #ifndef TEXT_SECTION_LABEL
4239 #define TEXT_SECTION_LABEL              "Ltext"
4240 #endif
4241 #ifndef COLD_TEXT_SECTION_LABEL
4242 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4243 #endif
4244 #ifndef DEBUG_LINE_SECTION_LABEL
4245 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4246 #endif
4247 #ifndef DEBUG_INFO_SECTION_LABEL
4248 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4249 #endif
4250 #ifndef DEBUG_ABBREV_SECTION_LABEL
4251 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4252 #endif
4253 #ifndef DEBUG_LOC_SECTION_LABEL
4254 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4255 #endif
4256 #ifndef DEBUG_RANGES_SECTION_LABEL
4257 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4258 #endif
4259 #ifndef DEBUG_MACINFO_SECTION_LABEL
4260 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4261 #endif
4262
4263 /* Definitions of defaults for formats and names of various special
4264    (artificial) labels which may be generated within this file (when the -g
4265    options is used and DWARF2_DEBUGGING_INFO is in effect.
4266    If necessary, these may be overridden from within the tm.h file, but
4267    typically, overriding these defaults is unnecessary.  */
4268
4269 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4270 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4271 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4272 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES]; 
4273 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4274 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4275 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4276 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4277 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4278 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4279
4280 #ifndef TEXT_END_LABEL
4281 #define TEXT_END_LABEL          "Letext"
4282 #endif
4283 #ifndef COLD_END_LABEL
4284 #define COLD_END_LABEL          "Letext_cold"
4285 #endif
4286 #ifndef BLOCK_BEGIN_LABEL
4287 #define BLOCK_BEGIN_LABEL       "LBB"
4288 #endif
4289 #ifndef BLOCK_END_LABEL
4290 #define BLOCK_END_LABEL         "LBE"
4291 #endif
4292 #ifndef LINE_CODE_LABEL
4293 #define LINE_CODE_LABEL         "LM"
4294 #endif
4295 #ifndef SEPARATE_LINE_CODE_LABEL
4296 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4297 #endif
4298 \f
4299 /* We allow a language front-end to designate a function that is to be
4300    called to "demangle" any name before it is put into a DIE.  */
4301
4302 static const char *(*demangle_name_func) (const char *);
4303
4304 void
4305 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4306 {
4307   demangle_name_func = func;
4308 }
4309
4310 /* Test if rtl node points to a pseudo register.  */
4311
4312 static inline int
4313 is_pseudo_reg (rtx rtl)
4314 {
4315   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4316           || (GET_CODE (rtl) == SUBREG
4317               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4318 }
4319
4320 /* Return a reference to a type, with its const and volatile qualifiers
4321    removed.  */
4322
4323 static inline tree
4324 type_main_variant (tree type)
4325 {
4326   type = TYPE_MAIN_VARIANT (type);
4327
4328   /* ??? There really should be only one main variant among any group of
4329      variants of a given type (and all of the MAIN_VARIANT values for all
4330      members of the group should point to that one type) but sometimes the C
4331      front-end messes this up for array types, so we work around that bug
4332      here.  */
4333   if (TREE_CODE (type) == ARRAY_TYPE)
4334     while (type != TYPE_MAIN_VARIANT (type))
4335       type = TYPE_MAIN_VARIANT (type);
4336
4337   return type;
4338 }
4339
4340 /* Return nonzero if the given type node represents a tagged type.  */
4341
4342 static inline int
4343 is_tagged_type (tree type)
4344 {
4345   enum tree_code code = TREE_CODE (type);
4346
4347   return (code == RECORD_TYPE || code == UNION_TYPE
4348           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4349 }
4350
4351 /* Convert a DIE tag into its string name.  */
4352
4353 static const char *
4354 dwarf_tag_name (unsigned int tag)
4355 {
4356   switch (tag)
4357     {
4358     case DW_TAG_padding:
4359       return "DW_TAG_padding";
4360     case DW_TAG_array_type:
4361       return "DW_TAG_array_type";
4362     case DW_TAG_class_type:
4363       return "DW_TAG_class_type";
4364     case DW_TAG_entry_point:
4365       return "DW_TAG_entry_point";
4366     case DW_TAG_enumeration_type:
4367       return "DW_TAG_enumeration_type";
4368     case DW_TAG_formal_parameter:
4369       return "DW_TAG_formal_parameter";
4370     case DW_TAG_imported_declaration:
4371       return "DW_TAG_imported_declaration";
4372     case DW_TAG_label:
4373       return "DW_TAG_label";
4374     case DW_TAG_lexical_block:
4375       return "DW_TAG_lexical_block";
4376     case DW_TAG_member:
4377       return "DW_TAG_member";
4378     case DW_TAG_pointer_type:
4379       return "DW_TAG_pointer_type";
4380     case DW_TAG_reference_type:
4381       return "DW_TAG_reference_type";
4382     case DW_TAG_compile_unit:
4383       return "DW_TAG_compile_unit";
4384     case DW_TAG_string_type:
4385       return "DW_TAG_string_type";
4386     case DW_TAG_structure_type:
4387       return "DW_TAG_structure_type";
4388     case DW_TAG_subroutine_type:
4389       return "DW_TAG_subroutine_type";
4390     case DW_TAG_typedef:
4391       return "DW_TAG_typedef";
4392     case DW_TAG_union_type:
4393       return "DW_TAG_union_type";
4394     case DW_TAG_unspecified_parameters:
4395       return "DW_TAG_unspecified_parameters";
4396     case DW_TAG_variant:
4397       return "DW_TAG_variant";
4398     case DW_TAG_common_block:
4399       return "DW_TAG_common_block";
4400     case DW_TAG_common_inclusion:
4401       return "DW_TAG_common_inclusion";
4402     case DW_TAG_inheritance:
4403       return "DW_TAG_inheritance";
4404     case DW_TAG_inlined_subroutine:
4405       return "DW_TAG_inlined_subroutine";
4406     case DW_TAG_module:
4407       return "DW_TAG_module";
4408     case DW_TAG_ptr_to_member_type:
4409       return "DW_TAG_ptr_to_member_type";
4410     case DW_TAG_set_type:
4411       return "DW_TAG_set_type";
4412     case DW_TAG_subrange_type:
4413       return "DW_TAG_subrange_type";
4414     case DW_TAG_with_stmt:
4415       return "DW_TAG_with_stmt";
4416     case DW_TAG_access_declaration:
4417       return "DW_TAG_access_declaration";
4418     case DW_TAG_base_type:
4419       return "DW_TAG_base_type";
4420     case DW_TAG_catch_block:
4421       return "DW_TAG_catch_block";
4422     case DW_TAG_const_type:
4423       return "DW_TAG_const_type";
4424     case DW_TAG_constant:
4425       return "DW_TAG_constant";
4426     case DW_TAG_enumerator:
4427       return "DW_TAG_enumerator";
4428     case DW_TAG_file_type:
4429       return "DW_TAG_file_type";
4430     case DW_TAG_friend:
4431       return "DW_TAG_friend";
4432     case DW_TAG_namelist:
4433       return "DW_TAG_namelist";
4434     case DW_TAG_namelist_item:
4435       return "DW_TAG_namelist_item";
4436     case DW_TAG_namespace:
4437       return "DW_TAG_namespace";
4438     case DW_TAG_packed_type:
4439       return "DW_TAG_packed_type";
4440     case DW_TAG_subprogram:
4441       return "DW_TAG_subprogram";
4442     case DW_TAG_template_type_param:
4443       return "DW_TAG_template_type_param";
4444     case DW_TAG_template_value_param:
4445       return "DW_TAG_template_value_param";
4446     case DW_TAG_thrown_type:
4447       return "DW_TAG_thrown_type";
4448     case DW_TAG_try_block:
4449       return "DW_TAG_try_block";
4450     case DW_TAG_variant_part:
4451       return "DW_TAG_variant_part";
4452     case DW_TAG_variable:
4453       return "DW_TAG_variable";
4454     case DW_TAG_volatile_type:
4455       return "DW_TAG_volatile_type";
4456     case DW_TAG_imported_module:
4457       return "DW_TAG_imported_module";
4458     case DW_TAG_MIPS_loop:
4459       return "DW_TAG_MIPS_loop";
4460     case DW_TAG_format_label:
4461       return "DW_TAG_format_label";
4462     case DW_TAG_function_template:
4463       return "DW_TAG_function_template";
4464     case DW_TAG_class_template:
4465       return "DW_TAG_class_template";
4466     case DW_TAG_GNU_BINCL:
4467       return "DW_TAG_GNU_BINCL";
4468     case DW_TAG_GNU_EINCL:
4469       return "DW_TAG_GNU_EINCL";
4470     default:
4471       return "DW_TAG_<unknown>";
4472     }
4473 }
4474
4475 /* Convert a DWARF attribute code into its string name.  */
4476
4477 static const char *
4478 dwarf_attr_name (unsigned int attr)
4479 {
4480   switch (attr)
4481     {
4482     case DW_AT_sibling:
4483       return "DW_AT_sibling";
4484     case DW_AT_location:
4485       return "DW_AT_location";
4486     case DW_AT_name:
4487       return "DW_AT_name";
4488     case DW_AT_ordering:
4489       return "DW_AT_ordering";
4490     case DW_AT_subscr_data:
4491       return "DW_AT_subscr_data";
4492     case DW_AT_byte_size:
4493       return "DW_AT_byte_size";
4494     case DW_AT_bit_offset:
4495       return "DW_AT_bit_offset";
4496     case DW_AT_bit_size:
4497       return "DW_AT_bit_size";
4498     case DW_AT_element_list:
4499       return "DW_AT_element_list";
4500     case DW_AT_stmt_list:
4501       return "DW_AT_stmt_list";
4502     case DW_AT_low_pc:
4503       return "DW_AT_low_pc";
4504     case DW_AT_high_pc:
4505       return "DW_AT_high_pc";
4506     case DW_AT_language:
4507       return "DW_AT_language";
4508     case DW_AT_member:
4509       return "DW_AT_member";
4510     case DW_AT_discr:
4511       return "DW_AT_discr";
4512     case DW_AT_discr_value:
4513       return "DW_AT_discr_value";
4514     case DW_AT_visibility:
4515       return "DW_AT_visibility";
4516     case DW_AT_import:
4517       return "DW_AT_import";
4518     case DW_AT_string_length:
4519       return "DW_AT_string_length";
4520     case DW_AT_common_reference:
4521       return "DW_AT_common_reference";
4522     case DW_AT_comp_dir:
4523       return "DW_AT_comp_dir";
4524     case DW_AT_const_value:
4525       return "DW_AT_const_value";
4526     case DW_AT_containing_type:
4527       return "DW_AT_containing_type";
4528     case DW_AT_default_value:
4529       return "DW_AT_default_value";
4530     case DW_AT_inline:
4531       return "DW_AT_inline";
4532     case DW_AT_is_optional:
4533       return "DW_AT_is_optional";
4534     case DW_AT_lower_bound:
4535       return "DW_AT_lower_bound";
4536     case DW_AT_producer:
4537       return "DW_AT_producer";
4538     case DW_AT_prototyped:
4539       return "DW_AT_prototyped";
4540     case DW_AT_return_addr:
4541       return "DW_AT_return_addr";
4542     case DW_AT_start_scope:
4543       return "DW_AT_start_scope";
4544     case DW_AT_stride_size:
4545       return "DW_AT_stride_size";
4546     case DW_AT_upper_bound:
4547       return "DW_AT_upper_bound";
4548     case DW_AT_abstract_origin:
4549       return "DW_AT_abstract_origin";
4550     case DW_AT_accessibility:
4551       return "DW_AT_accessibility";
4552     case DW_AT_address_class:
4553       return "DW_AT_address_class";
4554     case DW_AT_artificial:
4555       return "DW_AT_artificial";
4556     case DW_AT_base_types:
4557       return "DW_AT_base_types";
4558     case DW_AT_calling_convention:
4559       return "DW_AT_calling_convention";
4560     case DW_AT_count:
4561       return "DW_AT_count";
4562     case DW_AT_data_member_location:
4563       return "DW_AT_data_member_location";
4564     case DW_AT_decl_column:
4565       return "DW_AT_decl_column";
4566     case DW_AT_decl_file:
4567       return "DW_AT_decl_file";
4568     case DW_AT_decl_line:
4569       return "DW_AT_decl_line";
4570     case DW_AT_declaration:
4571       return "DW_AT_declaration";
4572     case DW_AT_discr_list:
4573       return "DW_AT_discr_list";
4574     case DW_AT_encoding:
4575       return "DW_AT_encoding";
4576     case DW_AT_external:
4577       return "DW_AT_external";
4578     case DW_AT_frame_base:
4579       return "DW_AT_frame_base";
4580     case DW_AT_friend:
4581       return "DW_AT_friend";
4582     case DW_AT_identifier_case:
4583       return "DW_AT_identifier_case";
4584     case DW_AT_macro_info:
4585       return "DW_AT_macro_info";
4586     case DW_AT_namelist_items:
4587       return "DW_AT_namelist_items";
4588     case DW_AT_priority:
4589       return "DW_AT_priority";
4590     case DW_AT_segment:
4591       return "DW_AT_segment";
4592     case DW_AT_specification:
4593       return "DW_AT_specification";
4594     case DW_AT_static_link:
4595       return "DW_AT_static_link";
4596     case DW_AT_type:
4597       return "DW_AT_type";
4598     case DW_AT_use_location:
4599       return "DW_AT_use_location";
4600     case DW_AT_variable_parameter:
4601       return "DW_AT_variable_parameter";
4602     case DW_AT_virtuality:
4603       return "DW_AT_virtuality";
4604     case DW_AT_vtable_elem_location:
4605       return "DW_AT_vtable_elem_location";
4606
4607     case DW_AT_allocated:
4608       return "DW_AT_allocated";
4609     case DW_AT_associated:
4610       return "DW_AT_associated";
4611     case DW_AT_data_location:
4612       return "DW_AT_data_location";
4613     case DW_AT_stride:
4614       return "DW_AT_stride";
4615     case DW_AT_entry_pc:
4616       return "DW_AT_entry_pc";
4617     case DW_AT_use_UTF8:
4618       return "DW_AT_use_UTF8";
4619     case DW_AT_extension:
4620       return "DW_AT_extension";
4621     case DW_AT_ranges:
4622       return "DW_AT_ranges";
4623     case DW_AT_trampoline:
4624       return "DW_AT_trampoline";
4625     case DW_AT_call_column:
4626       return "DW_AT_call_column";
4627     case DW_AT_call_file:
4628       return "DW_AT_call_file";
4629     case DW_AT_call_line:
4630       return "DW_AT_call_line";
4631
4632     case DW_AT_MIPS_fde:
4633       return "DW_AT_MIPS_fde";
4634     case DW_AT_MIPS_loop_begin:
4635       return "DW_AT_MIPS_loop_begin";
4636     case DW_AT_MIPS_tail_loop_begin:
4637       return "DW_AT_MIPS_tail_loop_begin";
4638     case DW_AT_MIPS_epilog_begin:
4639       return "DW_AT_MIPS_epilog_begin";
4640     case DW_AT_MIPS_loop_unroll_factor:
4641       return "DW_AT_MIPS_loop_unroll_factor";
4642     case DW_AT_MIPS_software_pipeline_depth:
4643       return "DW_AT_MIPS_software_pipeline_depth";
4644     case DW_AT_MIPS_linkage_name:
4645       return "DW_AT_MIPS_linkage_name";
4646     case DW_AT_MIPS_stride:
4647       return "DW_AT_MIPS_stride";
4648     case DW_AT_MIPS_abstract_name:
4649       return "DW_AT_MIPS_abstract_name";
4650     case DW_AT_MIPS_clone_origin:
4651       return "DW_AT_MIPS_clone_origin";
4652     case DW_AT_MIPS_has_inlines:
4653       return "DW_AT_MIPS_has_inlines";
4654
4655     case DW_AT_sf_names:
4656       return "DW_AT_sf_names";
4657     case DW_AT_src_info:
4658       return "DW_AT_src_info";
4659     case DW_AT_mac_info:
4660       return "DW_AT_mac_info";
4661     case DW_AT_src_coords:
4662       return "DW_AT_src_coords";
4663     case DW_AT_body_begin:
4664       return "DW_AT_body_begin";
4665     case DW_AT_body_end:
4666       return "DW_AT_body_end";
4667     case DW_AT_GNU_vector:
4668       return "DW_AT_GNU_vector";
4669
4670     case DW_AT_VMS_rtnbeg_pd_address:
4671       return "DW_AT_VMS_rtnbeg_pd_address";
4672
4673     default:
4674       return "DW_AT_<unknown>";
4675     }
4676 }
4677
4678 /* Convert a DWARF value form code into its string name.  */
4679
4680 static const char *
4681 dwarf_form_name (unsigned int form)
4682 {
4683   switch (form)
4684     {
4685     case DW_FORM_addr:
4686       return "DW_FORM_addr";
4687     case DW_FORM_block2:
4688       return "DW_FORM_block2";
4689     case DW_FORM_block4:
4690       return "DW_FORM_block4";
4691     case DW_FORM_data2:
4692       return "DW_FORM_data2";
4693     case DW_FORM_data4:
4694       return "DW_FORM_data4";
4695     case DW_FORM_data8:
4696       return "DW_FORM_data8";
4697     case DW_FORM_string:
4698       return "DW_FORM_string";
4699     case DW_FORM_block:
4700       return "DW_FORM_block";
4701     case DW_FORM_block1:
4702       return "DW_FORM_block1";
4703     case DW_FORM_data1:
4704       return "DW_FORM_data1";
4705     case DW_FORM_flag:
4706       return "DW_FORM_flag";
4707     case DW_FORM_sdata:
4708       return "DW_FORM_sdata";
4709     case DW_FORM_strp:
4710       return "DW_FORM_strp";
4711     case DW_FORM_udata:
4712       return "DW_FORM_udata";
4713     case DW_FORM_ref_addr:
4714       return "DW_FORM_ref_addr";
4715     case DW_FORM_ref1:
4716       return "DW_FORM_ref1";
4717     case DW_FORM_ref2:
4718       return "DW_FORM_ref2";
4719     case DW_FORM_ref4:
4720       return "DW_FORM_ref4";
4721     case DW_FORM_ref8:
4722       return "DW_FORM_ref8";
4723     case DW_FORM_ref_udata:
4724       return "DW_FORM_ref_udata";
4725     case DW_FORM_indirect:
4726       return "DW_FORM_indirect";
4727     default:
4728       return "DW_FORM_<unknown>";
4729     }
4730 }
4731 \f
4732 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4733    instance of an inlined instance of a decl which is local to an inline
4734    function, so we have to trace all of the way back through the origin chain
4735    to find out what sort of node actually served as the original seed for the
4736    given block.  */
4737
4738 static tree
4739 decl_ultimate_origin (tree decl)
4740 {
4741   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4742     return NULL_TREE;
4743
4744   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4745      nodes in the function to point to themselves; ignore that if
4746      we're trying to output the abstract instance of this function.  */
4747   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4748     return NULL_TREE;
4749
4750   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4751      most distant ancestor, this should never happen.  */
4752   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4753
4754   return DECL_ABSTRACT_ORIGIN (decl);
4755 }
4756
4757 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4758    instance of an inlined instance of a block which is local to an inline
4759    function, so we have to trace all of the way back through the origin chain
4760    to find out what sort of node actually served as the original seed for the
4761    given block.  */
4762
4763 static tree
4764 block_ultimate_origin (tree block)
4765 {
4766   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4767
4768   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4769      nodes in the function to point to themselves; ignore that if
4770      we're trying to output the abstract instance of this function.  */
4771   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4772     return NULL_TREE;
4773
4774   if (immediate_origin == NULL_TREE)
4775     return NULL_TREE;
4776   else
4777     {
4778       tree ret_val;
4779       tree lookahead = immediate_origin;
4780
4781       do
4782         {
4783           ret_val = lookahead;
4784           lookahead = (TREE_CODE (ret_val) == BLOCK
4785                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4786         }
4787       while (lookahead != NULL && lookahead != ret_val);
4788       
4789       /* The block's abstract origin chain may not be the *ultimate* origin of
4790          the block. It could lead to a DECL that has an abstract origin set.
4791          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4792          will give us if it has one).  Note that DECL's abstract origins are
4793          supposed to be the most distant ancestor (or so decl_ultimate_origin
4794          claims), so we don't need to loop following the DECL origins.  */
4795       if (DECL_P (ret_val))
4796         return DECL_ORIGIN (ret_val);
4797
4798       return ret_val;
4799     }
4800 }
4801
4802 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4803    of a virtual function may refer to a base class, so we check the 'this'
4804    parameter.  */
4805
4806 static tree
4807 decl_class_context (tree decl)
4808 {
4809   tree context = NULL_TREE;
4810
4811   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4812     context = DECL_CONTEXT (decl);
4813   else
4814     context = TYPE_MAIN_VARIANT
4815       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4816
4817   if (context && !TYPE_P (context))
4818     context = NULL_TREE;
4819
4820   return context;
4821 }
4822 \f
4823 /* Add an attribute/value pair to a DIE.  We build the lists up in reverse
4824    addition order, and correct that in reverse_all_dies.  */
4825
4826 static inline void
4827 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4828 {
4829   if (die != NULL && attr != NULL)
4830     {
4831       attr->dw_attr_next = die->die_attr;
4832       die->die_attr = attr;
4833     }
4834 }
4835
4836 static inline enum dw_val_class
4837 AT_class (dw_attr_ref a)
4838 {
4839   return a->dw_attr_val.val_class;
4840 }
4841
4842 /* Add a flag value attribute to a DIE.  */
4843
4844 static inline void
4845 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4846 {
4847   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4848
4849   attr->dw_attr_next = NULL;
4850   attr->dw_attr = attr_kind;
4851   attr->dw_attr_val.val_class = dw_val_class_flag;
4852   attr->dw_attr_val.v.val_flag = flag;
4853   add_dwarf_attr (die, attr);
4854 }
4855
4856 static inline unsigned
4857 AT_flag (dw_attr_ref a)
4858 {
4859   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4860   return a->dw_attr_val.v.val_flag;
4861 }
4862
4863 /* Add a signed integer attribute value to a DIE.  */
4864
4865 static inline void
4866 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4867 {
4868   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4869
4870   attr->dw_attr_next = NULL;
4871   attr->dw_attr = attr_kind;
4872   attr->dw_attr_val.val_class = dw_val_class_const;
4873   attr->dw_attr_val.v.val_int = int_val;
4874   add_dwarf_attr (die, attr);
4875 }
4876
4877 static inline HOST_WIDE_INT
4878 AT_int (dw_attr_ref a)
4879 {
4880   gcc_assert (a && AT_class (a) == dw_val_class_const);
4881   return a->dw_attr_val.v.val_int;
4882 }
4883
4884 /* Add an unsigned integer attribute value to a DIE.  */
4885
4886 static inline void
4887 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4888                  unsigned HOST_WIDE_INT unsigned_val)
4889 {
4890   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4891
4892   attr->dw_attr_next = NULL;
4893   attr->dw_attr = attr_kind;
4894   attr->dw_attr_val.val_class = dw_val_class_unsigned_const;
4895   attr->dw_attr_val.v.val_unsigned = unsigned_val;
4896   add_dwarf_attr (die, attr);
4897 }
4898
4899 static inline unsigned HOST_WIDE_INT
4900 AT_unsigned (dw_attr_ref a)
4901 {
4902   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4903   return a->dw_attr_val.v.val_unsigned;
4904 }
4905
4906 /* Add an unsigned double integer attribute value to a DIE.  */
4907
4908 static inline void
4909 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4910                   long unsigned int val_hi, long unsigned int val_low)
4911 {
4912   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4913
4914   attr->dw_attr_next = NULL;
4915   attr->dw_attr = attr_kind;
4916   attr->dw_attr_val.val_class = dw_val_class_long_long;
4917   attr->dw_attr_val.v.val_long_long.hi = val_hi;
4918   attr->dw_attr_val.v.val_long_long.low = val_low;
4919   add_dwarf_attr (die, attr);
4920 }
4921
4922 /* Add a floating point attribute value to a DIE and return it.  */
4923
4924 static inline void
4925 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4926             unsigned int length, unsigned int elt_size, unsigned char *array)
4927 {
4928   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4929
4930   attr->dw_attr_next = NULL;
4931   attr->dw_attr = attr_kind;
4932   attr->dw_attr_val.val_class = dw_val_class_vec;
4933   attr->dw_attr_val.v.val_vec.length = length;
4934   attr->dw_attr_val.v.val_vec.elt_size = elt_size;
4935   attr->dw_attr_val.v.val_vec.array = array;
4936   add_dwarf_attr (die, attr);
4937 }
4938
4939 /* Hash and equality functions for debug_str_hash.  */
4940
4941 static hashval_t
4942 debug_str_do_hash (const void *x)
4943 {
4944   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4945 }
4946
4947 static int
4948 debug_str_eq (const void *x1, const void *x2)
4949 {
4950   return strcmp ((((const struct indirect_string_node *)x1)->str),
4951                  (const char *)x2) == 0;
4952 }
4953
4954 /* Add a string attribute value to a DIE.  */
4955
4956 static inline void
4957 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4958 {
4959   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
4960   struct indirect_string_node *node;
4961   void **slot;
4962
4963   if (! debug_str_hash)
4964     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4965                                       debug_str_eq, NULL);
4966
4967   slot = htab_find_slot_with_hash (debug_str_hash, str,
4968                                    htab_hash_string (str), INSERT);
4969   if (*slot == NULL)
4970     *slot = ggc_alloc_cleared (sizeof (struct indirect_string_node));
4971   node = (struct indirect_string_node *) *slot;
4972   node->str = ggc_strdup (str);
4973   node->refcount++;
4974
4975   attr->dw_attr_next = NULL;
4976   attr->dw_attr = attr_kind;
4977   attr->dw_attr_val.val_class = dw_val_class_str;
4978   attr->dw_attr_val.v.val_str = node;
4979   add_dwarf_attr (die, attr);
4980 }
4981
4982 static inline const char *
4983 AT_string (dw_attr_ref a)
4984 {
4985   gcc_assert (a && AT_class (a) == dw_val_class_str);
4986   return a->dw_attr_val.v.val_str->str;
4987 }
4988
4989 /* Find out whether a string should be output inline in DIE
4990    or out-of-line in .debug_str section.  */
4991
4992 static int
4993 AT_string_form (dw_attr_ref a)
4994 {
4995   struct indirect_string_node *node;
4996   unsigned int len;
4997   char label[32];
4998
4999   gcc_assert (a && AT_class (a) == dw_val_class_str);
5000
5001   node = a->dw_attr_val.v.val_str;
5002   if (node->form)
5003     return node->form;
5004
5005   len = strlen (node->str) + 1;
5006
5007   /* If the string is shorter or equal to the size of the reference, it is
5008      always better to put it inline.  */
5009   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5010     return node->form = DW_FORM_string;
5011
5012   /* If we cannot expect the linker to merge strings in .debug_str
5013      section, only put it into .debug_str if it is worth even in this
5014      single module.  */
5015   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5016       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5017     return node->form = DW_FORM_string;
5018
5019   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5020   ++dw2_string_counter;
5021   node->label = xstrdup (label);
5022
5023   return node->form = DW_FORM_strp;
5024 }
5025
5026 /* Add a DIE reference attribute value to a DIE.  */
5027
5028 static inline void
5029 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5030 {
5031   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5032
5033   attr->dw_attr_next = NULL;
5034   attr->dw_attr = attr_kind;
5035   attr->dw_attr_val.val_class = dw_val_class_die_ref;
5036   attr->dw_attr_val.v.val_die_ref.die = targ_die;
5037   attr->dw_attr_val.v.val_die_ref.external = 0;
5038   add_dwarf_attr (die, attr);
5039 }
5040
5041 /* Add an AT_specification attribute to a DIE, and also make the back
5042    pointer from the specification to the definition.  */
5043
5044 static inline void
5045 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5046 {
5047   add_AT_die_ref (die, DW_AT_specification, targ_die);
5048   gcc_assert (!targ_die->die_definition);
5049   targ_die->die_definition = die;
5050 }
5051
5052 static inline dw_die_ref
5053 AT_ref (dw_attr_ref a)
5054 {
5055   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5056   return a->dw_attr_val.v.val_die_ref.die;
5057 }
5058
5059 static inline int
5060 AT_ref_external (dw_attr_ref a)
5061 {
5062   if (a && AT_class (a) == dw_val_class_die_ref)
5063     return a->dw_attr_val.v.val_die_ref.external;
5064
5065   return 0;
5066 }
5067
5068 static inline void
5069 set_AT_ref_external (dw_attr_ref a, int i)
5070 {
5071   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5072   a->dw_attr_val.v.val_die_ref.external = i;
5073 }
5074
5075 /* Add an FDE reference attribute value to a DIE.  */
5076
5077 static inline void
5078 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5079 {
5080   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5081
5082   attr->dw_attr_next = NULL;
5083   attr->dw_attr = attr_kind;
5084   attr->dw_attr_val.val_class = dw_val_class_fde_ref;
5085   attr->dw_attr_val.v.val_fde_index = targ_fde;
5086   add_dwarf_attr (die, attr);
5087 }
5088
5089 /* Add a location description attribute value to a DIE.  */
5090
5091 static inline void
5092 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5093 {
5094   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5095
5096   attr->dw_attr_next = NULL;
5097   attr->dw_attr = attr_kind;
5098   attr->dw_attr_val.val_class = dw_val_class_loc;
5099   attr->dw_attr_val.v.val_loc = loc;
5100   add_dwarf_attr (die, attr);
5101 }
5102
5103 static inline dw_loc_descr_ref
5104 AT_loc (dw_attr_ref a)
5105 {
5106   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5107   return a->dw_attr_val.v.val_loc;
5108 }
5109
5110 static inline void
5111 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5112 {
5113   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5114
5115   attr->dw_attr_next = NULL;
5116   attr->dw_attr = attr_kind;
5117   attr->dw_attr_val.val_class = dw_val_class_loc_list;
5118   attr->dw_attr_val.v.val_loc_list = loc_list;
5119   add_dwarf_attr (die, attr);
5120   have_location_lists = true;
5121 }
5122
5123 static inline dw_loc_list_ref
5124 AT_loc_list (dw_attr_ref a)
5125 {
5126   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5127   return a->dw_attr_val.v.val_loc_list;
5128 }
5129
5130 /* Add an address constant attribute value to a DIE.  */
5131
5132 static inline void
5133 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5134 {
5135   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5136
5137   attr->dw_attr_next = NULL;
5138   attr->dw_attr = attr_kind;
5139   attr->dw_attr_val.val_class = dw_val_class_addr;
5140   attr->dw_attr_val.v.val_addr = addr;
5141   add_dwarf_attr (die, attr);
5142 }
5143
5144 static inline rtx
5145 AT_addr (dw_attr_ref a)
5146 {
5147   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5148   return a->dw_attr_val.v.val_addr;
5149 }
5150
5151 /* Add a label identifier attribute value to a DIE.  */
5152
5153 static inline void
5154 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5155 {
5156   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5157
5158   attr->dw_attr_next = NULL;
5159   attr->dw_attr = attr_kind;
5160   attr->dw_attr_val.val_class = dw_val_class_lbl_id;
5161   attr->dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5162   add_dwarf_attr (die, attr);
5163 }
5164
5165 /* Add a section offset attribute value to a DIE, an offset into the
5166    debug_line section.  */
5167
5168 static inline void
5169 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5170                 const char *label)
5171 {
5172   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5173
5174   attr->dw_attr_next = NULL;
5175   attr->dw_attr = attr_kind;
5176   attr->dw_attr_val.val_class = dw_val_class_lineptr;
5177   attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5178   add_dwarf_attr (die, attr);
5179 }
5180
5181 /* Add a section offset attribute value to a DIE, an offset into the
5182    debug_macinfo section.  */
5183
5184 static inline void
5185 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5186                const char *label)
5187 {
5188   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5189
5190   attr->dw_attr_next = NULL;
5191   attr->dw_attr = attr_kind;
5192   attr->dw_attr_val.val_class = dw_val_class_macptr;
5193   attr->dw_attr_val.v.val_lbl_id = xstrdup (label);
5194   add_dwarf_attr (die, attr);
5195 }
5196
5197 /* Add an offset attribute value to a DIE.  */
5198
5199 static inline void
5200 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5201                unsigned HOST_WIDE_INT offset)
5202 {
5203   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5204
5205   attr->dw_attr_next = NULL;
5206   attr->dw_attr = attr_kind;
5207   attr->dw_attr_val.val_class = dw_val_class_offset;
5208   attr->dw_attr_val.v.val_offset = offset;
5209   add_dwarf_attr (die, attr);
5210 }
5211
5212 /* Add an range_list attribute value to a DIE.  */
5213
5214 static void
5215 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5216                    long unsigned int offset)
5217 {
5218   dw_attr_ref attr = ggc_alloc (sizeof (dw_attr_node));
5219
5220   attr->dw_attr_next = NULL;
5221   attr->dw_attr = attr_kind;
5222   attr->dw_attr_val.val_class = dw_val_class_range_list;
5223   attr->dw_attr_val.v.val_offset = offset;
5224   add_dwarf_attr (die, attr);
5225 }
5226
5227 static inline const char *
5228 AT_lbl (dw_attr_ref a)
5229 {
5230   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5231                     || AT_class (a) == dw_val_class_lineptr
5232                     || AT_class (a) == dw_val_class_macptr));
5233   return a->dw_attr_val.v.val_lbl_id;
5234 }
5235
5236 /* Get the attribute of type attr_kind.  */
5237
5238 static dw_attr_ref
5239 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5240 {
5241   dw_attr_ref a;
5242   dw_die_ref spec = NULL;
5243
5244   if (die != NULL)
5245     {
5246       for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5247         if (a->dw_attr == attr_kind)
5248           return a;
5249         else if (a->dw_attr == DW_AT_specification
5250                  || a->dw_attr == DW_AT_abstract_origin)
5251           spec = AT_ref (a);
5252
5253       if (spec)
5254         return get_AT (spec, attr_kind);
5255     }
5256
5257   return NULL;
5258 }
5259
5260 /* Return the "low pc" attribute value, typically associated with a subprogram
5261    DIE.  Return null if the "low pc" attribute is either not present, or if it
5262    cannot be represented as an assembler label identifier.  */
5263
5264 static inline const char *
5265 get_AT_low_pc (dw_die_ref die)
5266 {
5267   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5268
5269   return a ? AT_lbl (a) : NULL;
5270 }
5271
5272 /* Return the "high pc" attribute value, typically associated with a subprogram
5273    DIE.  Return null if the "high pc" attribute is either not present, or if it
5274    cannot be represented as an assembler label identifier.  */
5275
5276 static inline const char *
5277 get_AT_hi_pc (dw_die_ref die)
5278 {
5279   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5280
5281   return a ? AT_lbl (a) : NULL;
5282 }
5283
5284 /* Return the value of the string attribute designated by ATTR_KIND, or
5285    NULL if it is not present.  */
5286
5287 static inline const char *
5288 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5289 {
5290   dw_attr_ref a = get_AT (die, attr_kind);
5291
5292   return a ? AT_string (a) : NULL;
5293 }
5294
5295 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5296    if it is not present.  */
5297
5298 static inline int
5299 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5300 {
5301   dw_attr_ref a = get_AT (die, attr_kind);
5302
5303   return a ? AT_flag (a) : 0;
5304 }
5305
5306 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5307    if it is not present.  */
5308
5309 static inline unsigned
5310 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5311 {
5312   dw_attr_ref a = get_AT (die, attr_kind);
5313
5314   return a ? AT_unsigned (a) : 0;
5315 }
5316
5317 static inline dw_die_ref
5318 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5319 {
5320   dw_attr_ref a = get_AT (die, attr_kind);
5321
5322   return a ? AT_ref (a) : NULL;
5323 }
5324
5325 /* Return TRUE if the language is C or C++.  */
5326
5327 static inline bool
5328 is_c_family (void)
5329 {
5330   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5331
5332   return (lang == DW_LANG_C || lang == DW_LANG_C89
5333           || lang == DW_LANG_C_plus_plus);
5334 }
5335
5336 /* Return TRUE if the language is C++.  */
5337
5338 static inline bool
5339 is_cxx (void)
5340 {
5341   return (get_AT_unsigned (comp_unit_die, DW_AT_language)
5342           == DW_LANG_C_plus_plus);
5343 }
5344
5345 /* Return TRUE if the language is Fortran.  */
5346
5347 static inline bool
5348 is_fortran (void)
5349 {
5350   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5351
5352   return (lang == DW_LANG_Fortran77
5353           || lang == DW_LANG_Fortran90
5354           || lang == DW_LANG_Fortran95);
5355 }
5356
5357 /* Return TRUE if the language is Java.  */
5358
5359 static inline bool
5360 is_java (void)
5361 {
5362   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5363
5364   return lang == DW_LANG_Java;
5365 }
5366
5367 /* Return TRUE if the language is Ada.  */
5368
5369 static inline bool
5370 is_ada (void)
5371 {
5372   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5373
5374   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5375 }
5376
5377 /* Free up the memory used by A.  */
5378
5379 static inline void free_AT (dw_attr_ref);
5380 static inline void
5381 free_AT (dw_attr_ref a)
5382 {
5383   if (AT_class (a) == dw_val_class_str)
5384     if (a->dw_attr_val.v.val_str->refcount)
5385       a->dw_attr_val.v.val_str->refcount--;
5386 }
5387
5388 /* Remove the specified attribute if present.  */
5389
5390 static void
5391 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5392 {
5393   dw_attr_ref *p;
5394   dw_attr_ref removed = NULL;
5395
5396   if (die != NULL)
5397     {
5398       for (p = &(die->die_attr); *p; p = &((*p)->dw_attr_next))
5399         if ((*p)->dw_attr == attr_kind)
5400           {
5401             removed = *p;
5402             *p = (*p)->dw_attr_next;
5403             break;
5404           }
5405
5406       if (removed != 0)
5407         free_AT (removed);
5408     }
5409 }
5410
5411 /* Remove child die whose die_tag is specified tag.  */
5412
5413 static void
5414 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5415 {
5416   dw_die_ref current, prev, next;
5417   current = die->die_child;
5418   prev = NULL;
5419   while (current != NULL)
5420     {
5421       if (current->die_tag == tag)
5422         {
5423           next = current->die_sib;
5424           if (prev == NULL)
5425             die->die_child = next;
5426           else
5427             prev->die_sib = next;
5428           free_die (current);
5429           current = next;
5430         }
5431       else
5432         {
5433           prev = current;
5434           current = current->die_sib;
5435         }
5436     }
5437 }
5438
5439 /* Free up the memory used by DIE.  */
5440
5441 static inline void
5442 free_die (dw_die_ref die)
5443 {
5444   remove_children (die);
5445 }
5446
5447 /* Discard the children of this DIE.  */
5448
5449 static void
5450 remove_children (dw_die_ref die)
5451 {
5452   dw_die_ref child_die = die->die_child;
5453
5454   die->die_child = NULL;
5455
5456   while (child_die != NULL)
5457     {
5458       dw_die_ref tmp_die = child_die;
5459       dw_attr_ref a;
5460
5461       child_die = child_die->die_sib;
5462
5463       for (a = tmp_die->die_attr; a != NULL;)
5464         {
5465           dw_attr_ref tmp_a = a;
5466
5467           a = a->dw_attr_next;
5468           free_AT (tmp_a);
5469         }
5470
5471       free_die (tmp_die);
5472     }
5473 }
5474
5475 /* Add a child DIE below its parent.  We build the lists up in reverse
5476    addition order, and correct that in reverse_all_dies.  */
5477
5478 static inline void
5479 add_child_die (dw_die_ref die, dw_die_ref child_die)
5480 {
5481   if (die != NULL && child_die != NULL)
5482     {
5483       gcc_assert (die != child_die);
5484
5485       child_die->die_parent = die;
5486       child_die->die_sib = die->die_child;
5487       die->die_child = child_die;
5488     }
5489 }
5490
5491 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5492    is the specification, to the front of PARENT's list of children.  */
5493
5494 static void
5495 splice_child_die (dw_die_ref parent, dw_die_ref child)
5496 {
5497   dw_die_ref *p;
5498
5499   /* We want the declaration DIE from inside the class, not the
5500      specification DIE at toplevel.  */
5501   if (child->die_parent != parent)
5502     {
5503       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5504
5505       if (tmp)
5506         child = tmp;
5507     }
5508
5509   gcc_assert (child->die_parent == parent
5510               || (child->die_parent
5511                   == get_AT_ref (parent, DW_AT_specification)));
5512
5513   for (p = &(child->die_parent->die_child); *p; p = &((*p)->die_sib))
5514     if (*p == child)
5515       {
5516         *p = child->die_sib;
5517         break;
5518       }
5519
5520   child->die_parent = parent;
5521   child->die_sib = parent->die_child;
5522   parent->die_child = child;
5523 }
5524
5525 /* Return a pointer to a newly created DIE node.  */
5526
5527 static inline dw_die_ref
5528 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5529 {
5530   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5531
5532   die->die_tag = tag_value;
5533
5534   if (parent_die != NULL)
5535     add_child_die (parent_die, die);
5536   else
5537     {
5538       limbo_die_node *limbo_node;
5539
5540       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5541       limbo_node->die = die;
5542       limbo_node->created_for = t;
5543       limbo_node->next = limbo_die_list;
5544       limbo_die_list = limbo_node;
5545     }
5546
5547   return die;
5548 }
5549
5550 /* Return the DIE associated with the given type specifier.  */
5551
5552 static inline dw_die_ref
5553 lookup_type_die (tree type)
5554 {
5555   return TYPE_SYMTAB_DIE (type);
5556 }
5557
5558 /* Equate a DIE to a given type specifier.  */
5559
5560 static inline void
5561 equate_type_number_to_die (tree type, dw_die_ref type_die)
5562 {
5563   TYPE_SYMTAB_DIE (type) = type_die;
5564 }
5565
5566 /* Returns a hash value for X (which really is a die_struct).  */
5567
5568 static hashval_t
5569 decl_die_table_hash (const void *x)
5570 {
5571   return (hashval_t) ((const dw_die_ref) x)->decl_id;
5572 }
5573
5574 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5575
5576 static int
5577 decl_die_table_eq (const void *x, const void *y)
5578 {
5579   return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5580 }
5581
5582 /* Return the DIE associated with a given declaration.  */
5583
5584 static inline dw_die_ref
5585 lookup_decl_die (tree decl)
5586 {
5587   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5588 }
5589
5590 /* Returns a hash value for X (which really is a var_loc_list).  */
5591
5592 static hashval_t
5593 decl_loc_table_hash (const void *x)
5594 {
5595   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5596 }
5597
5598 /* Return nonzero if decl_id of var_loc_list X is the same as
5599    UID of decl *Y.  */
5600
5601 static int
5602 decl_loc_table_eq (const void *x, const void *y)
5603 {
5604   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5605 }
5606
5607 /* Return the var_loc list associated with a given declaration.  */
5608
5609 static inline var_loc_list *
5610 lookup_decl_loc (tree decl)
5611 {
5612   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5613 }
5614
5615 /* Equate a DIE to a particular declaration.  */
5616
5617 static void
5618 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5619 {
5620   unsigned int decl_id = DECL_UID (decl);
5621   void **slot;
5622
5623   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5624   *slot = decl_die;
5625   decl_die->decl_id = decl_id;
5626 }
5627
5628 /* Add a variable location node to the linked list for DECL.  */
5629
5630 static void
5631 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5632 {
5633   unsigned int decl_id = DECL_UID (decl);
5634   var_loc_list *temp;
5635   void **slot;
5636
5637   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5638   if (*slot == NULL)
5639     {
5640       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5641       temp->decl_id = decl_id;
5642       *slot = temp;
5643     }
5644   else
5645     temp = *slot;
5646
5647   if (temp->last)
5648     {
5649       /* If the current location is the same as the end of the list,
5650          we have nothing to do.  */
5651       if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5652                         NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5653         {
5654           /* Add LOC to the end of list and update LAST.  */
5655           temp->last->next = loc;
5656           temp->last = loc;
5657         }
5658     }
5659   /* Do not add empty location to the beginning of the list.  */
5660   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5661     {
5662       temp->first = loc;
5663       temp->last = loc;
5664     }
5665 }
5666 \f
5667 /* Keep track of the number of spaces used to indent the
5668    output of the debugging routines that print the structure of
5669    the DIE internal representation.  */
5670 static int print_indent;
5671
5672 /* Indent the line the number of spaces given by print_indent.  */
5673
5674 static inline void
5675 print_spaces (FILE *outfile)
5676 {
5677   fprintf (outfile, "%*s", print_indent, "");
5678 }
5679
5680 /* Print the information associated with a given DIE, and its children.
5681    This routine is a debugging aid only.  */
5682
5683 static void
5684 print_die (dw_die_ref die, FILE *outfile)
5685 {
5686   dw_attr_ref a;
5687   dw_die_ref c;
5688
5689   print_spaces (outfile);
5690   fprintf (outfile, "DIE %4lu: %s\n",
5691            die->die_offset, dwarf_tag_name (die->die_tag));
5692   print_spaces (outfile);
5693   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5694   fprintf (outfile, " offset: %lu\n", die->die_offset);
5695
5696   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
5697     {
5698       print_spaces (outfile);
5699       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5700
5701       switch (AT_class (a))
5702         {
5703         case dw_val_class_addr:
5704           fprintf (outfile, "address");
5705           break;
5706         case dw_val_class_offset:
5707           fprintf (outfile, "offset");
5708           break;
5709         case dw_val_class_loc:
5710           fprintf (outfile, "location descriptor");
5711           break;
5712         case dw_val_class_loc_list:
5713           fprintf (outfile, "location list -> label:%s",
5714                    AT_loc_list (a)->ll_symbol);
5715           break;
5716         case dw_val_class_range_list:
5717           fprintf (outfile, "range list");
5718           break;
5719         case dw_val_class_const:
5720           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5721           break;
5722         case dw_val_class_unsigned_const:
5723           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5724           break;
5725         case dw_val_class_long_long:
5726           fprintf (outfile, "constant (%lu,%lu)",
5727                    a->dw_attr_val.v.val_long_long.hi,
5728                    a->dw_attr_val.v.val_long_long.low);
5729           break;
5730         case dw_val_class_vec:
5731           fprintf (outfile, "floating-point or vector constant");
5732           break;
5733         case dw_val_class_flag:
5734           fprintf (outfile, "%u", AT_flag (a));
5735           break;
5736         case dw_val_class_die_ref:
5737           if (AT_ref (a) != NULL)
5738             {
5739               if (AT_ref (a)->die_symbol)
5740                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5741               else
5742                 fprintf (outfile, "die -> %lu", AT_ref (a)->die_offset);
5743             }
5744           else
5745             fprintf (outfile, "die -> <null>");
5746           break;
5747         case dw_val_class_lbl_id:
5748         case dw_val_class_lineptr:
5749         case dw_val_class_macptr:
5750           fprintf (outfile, "label: %s", AT_lbl (a));
5751           break;
5752         case dw_val_class_str:
5753           if (AT_string (a) != NULL)
5754             fprintf (outfile, "\"%s\"", AT_string (a));
5755           else
5756             fprintf (outfile, "<null>");
5757           break;
5758         default:
5759           break;
5760         }
5761
5762       fprintf (outfile, "\n");
5763     }
5764
5765   if (die->die_child != NULL)
5766     {
5767       print_indent += 4;
5768       for (c = die->die_child; c != NULL; c = c->die_sib)
5769         print_die (c, outfile);
5770
5771       print_indent -= 4;
5772     }
5773   if (print_indent == 0)
5774     fprintf (outfile, "\n");
5775 }
5776
5777 /* Print the contents of the source code line number correspondence table.
5778    This routine is a debugging aid only.  */
5779
5780 static void
5781 print_dwarf_line_table (FILE *outfile)
5782 {
5783   unsigned i;
5784   dw_line_info_ref line_info;
5785
5786   fprintf (outfile, "\n\nDWARF source line information\n");
5787   for (i = 1; i < line_info_table_in_use; i++)
5788     {
5789       line_info = &line_info_table[i];
5790       fprintf (outfile, "%5d: ", i);
5791       fprintf (outfile, "%-20s",
5792                VARRAY_CHAR_PTR (file_table, line_info->dw_file_num));
5793       fprintf (outfile, "%6ld", line_info->dw_line_num);
5794       fprintf (outfile, "\n");
5795     }
5796
5797   fprintf (outfile, "\n\n");
5798 }
5799
5800 /* Print the information collected for a given DIE.  */
5801
5802 void
5803 debug_dwarf_die (dw_die_ref die)
5804 {
5805   print_die (die, stderr);
5806 }
5807
5808 /* Print all DWARF information collected for the compilation unit.
5809    This routine is a debugging aid only.  */
5810
5811 void
5812 debug_dwarf (void)
5813 {
5814   print_indent = 0;
5815   print_die (comp_unit_die, stderr);
5816   if (! DWARF2_ASM_LINE_DEBUG_INFO)
5817     print_dwarf_line_table (stderr);
5818 }
5819 \f
5820 /* We build up the lists of children and attributes by pushing new ones
5821    onto the beginning of the list.  Reverse the lists for DIE so that
5822    they are in order of addition.  */
5823
5824 static void
5825 reverse_die_lists (dw_die_ref die)
5826 {
5827   dw_die_ref c, cp, cn;
5828   dw_attr_ref a, ap, an;
5829
5830   for (a = die->die_attr, ap = 0; a; a = an)
5831     {
5832       an = a->dw_attr_next;
5833       a->dw_attr_next = ap;
5834       ap = a;
5835     }
5836
5837   die->die_attr = ap;
5838
5839   for (c = die->die_child, cp = 0; c; c = cn)
5840     {
5841       cn = c->die_sib;
5842       c->die_sib = cp;
5843       cp = c;
5844     }
5845
5846   die->die_child = cp;
5847 }
5848
5849 /* reverse_die_lists only reverses the single die you pass it. Since we used to
5850    reverse all dies in add_sibling_attributes, which runs through all the dies,
5851    it would reverse all the dies.  Now, however, since we don't call
5852    reverse_die_lists in add_sibling_attributes, we need a routine to
5853    recursively reverse all the dies. This is that routine.  */
5854
5855 static void
5856 reverse_all_dies (dw_die_ref die)
5857 {
5858   dw_die_ref c;
5859
5860   reverse_die_lists (die);
5861
5862   for (c = die->die_child; c; c = c->die_sib)
5863     reverse_all_dies (c);
5864 }
5865
5866 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5867    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5868    DIE that marks the start of the DIEs for this include file.  */
5869
5870 static dw_die_ref
5871 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5872 {
5873   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5874   dw_die_ref new_unit = gen_compile_unit_die (filename);
5875
5876   new_unit->die_sib = old_unit;
5877   return new_unit;
5878 }
5879
5880 /* Close an include-file CU and reopen the enclosing one.  */
5881
5882 static dw_die_ref
5883 pop_compile_unit (dw_die_ref old_unit)
5884 {
5885   dw_die_ref new_unit = old_unit->die_sib;
5886
5887   old_unit->die_sib = NULL;
5888   return new_unit;
5889 }
5890
5891 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5892 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5893
5894 /* Calculate the checksum of a location expression.  */
5895
5896 static inline void
5897 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5898 {
5899   CHECKSUM (loc->dw_loc_opc);
5900   CHECKSUM (loc->dw_loc_oprnd1);
5901   CHECKSUM (loc->dw_loc_oprnd2);
5902 }
5903
5904 /* Calculate the checksum of an attribute.  */
5905
5906 static void
5907 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5908 {
5909   dw_loc_descr_ref loc;
5910   rtx r;
5911
5912   CHECKSUM (at->dw_attr);
5913
5914   /* We don't care about differences in file numbering.  */
5915   if (at->dw_attr == DW_AT_decl_file
5916       /* Or that this was compiled with a different compiler snapshot; if
5917          the output is the same, that's what matters.  */
5918       || at->dw_attr == DW_AT_producer)
5919     return;
5920
5921   switch (AT_class (at))
5922     {
5923     case dw_val_class_const:
5924       CHECKSUM (at->dw_attr_val.v.val_int);
5925       break;
5926     case dw_val_class_unsigned_const:
5927       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5928       break;
5929     case dw_val_class_long_long:
5930       CHECKSUM (at->dw_attr_val.v.val_long_long);
5931       break;
5932     case dw_val_class_vec:
5933       CHECKSUM (at->dw_attr_val.v.val_vec);
5934       break;
5935     case dw_val_class_flag:
5936       CHECKSUM (at->dw_attr_val.v.val_flag);
5937       break;
5938     case dw_val_class_str:
5939       CHECKSUM_STRING (AT_string (at));
5940       break;
5941
5942     case dw_val_class_addr:
5943       r = AT_addr (at);
5944       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5945       CHECKSUM_STRING (XSTR (r, 0));
5946       break;
5947
5948     case dw_val_class_offset:
5949       CHECKSUM (at->dw_attr_val.v.val_offset);
5950       break;
5951
5952     case dw_val_class_loc:
5953       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5954         loc_checksum (loc, ctx);
5955       break;
5956
5957     case dw_val_class_die_ref:
5958       die_checksum (AT_ref (at), ctx, mark);
5959       break;
5960
5961     case dw_val_class_fde_ref:
5962     case dw_val_class_lbl_id:
5963     case dw_val_class_lineptr:
5964     case dw_val_class_macptr:
5965       break;
5966
5967     default:
5968       break;
5969     }
5970 }
5971
5972 /* Calculate the checksum of a DIE.  */
5973
5974 static void
5975 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5976 {
5977   dw_die_ref c;
5978   dw_attr_ref a;
5979
5980   /* To avoid infinite recursion.  */
5981   if (die->die_mark)
5982     {
5983       CHECKSUM (die->die_mark);
5984       return;
5985     }
5986   die->die_mark = ++(*mark);
5987
5988   CHECKSUM (die->die_tag);
5989
5990   for (a = die->die_attr; a; a = a->dw_attr_next)
5991     attr_checksum (a, ctx, mark);
5992
5993   for (c = die->die_child; c; c = c->die_sib)
5994     die_checksum (c, ctx, mark);
5995 }
5996
5997 #undef CHECKSUM
5998 #undef CHECKSUM_STRING
5999
6000 /* Do the location expressions look same?  */
6001 static inline int
6002 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6003 {
6004   return loc1->dw_loc_opc == loc2->dw_loc_opc
6005          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6006          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6007 }
6008
6009 /* Do the values look the same?  */
6010 static int
6011 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6012 {
6013   dw_loc_descr_ref loc1, loc2;
6014   rtx r1, r2;
6015
6016   if (v1->val_class != v2->val_class)
6017     return 0;
6018
6019   switch (v1->val_class)
6020     {
6021     case dw_val_class_const:
6022       return v1->v.val_int == v2->v.val_int;
6023     case dw_val_class_unsigned_const:
6024       return v1->v.val_unsigned == v2->v.val_unsigned;
6025     case dw_val_class_long_long:
6026       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6027              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6028     case dw_val_class_vec:
6029       if (v1->v.val_vec.length != v2->v.val_vec.length
6030           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6031         return 0;
6032       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6033                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6034         return 0;
6035       return 1;
6036     case dw_val_class_flag:
6037       return v1->v.val_flag == v2->v.val_flag;
6038     case dw_val_class_str:
6039       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6040
6041     case dw_val_class_addr:
6042       r1 = v1->v.val_addr;
6043       r2 = v2->v.val_addr;
6044       if (GET_CODE (r1) != GET_CODE (r2))
6045         return 0;
6046       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6047       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6048
6049     case dw_val_class_offset:
6050       return v1->v.val_offset == v2->v.val_offset;
6051
6052     case dw_val_class_loc:
6053       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6054            loc1 && loc2;
6055            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6056         if (!same_loc_p (loc1, loc2, mark))
6057           return 0;
6058       return !loc1 && !loc2;
6059
6060     case dw_val_class_die_ref:
6061       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6062
6063     case dw_val_class_fde_ref:
6064     case dw_val_class_lbl_id:
6065     case dw_val_class_lineptr:
6066     case dw_val_class_macptr:
6067       return 1;
6068
6069     default:
6070       return 1;
6071     }
6072 }
6073
6074 /* Do the attributes look the same?  */
6075
6076 static int
6077 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6078 {
6079   if (at1->dw_attr != at2->dw_attr)
6080     return 0;
6081
6082   /* We don't care about differences in file numbering.  */
6083   if (at1->dw_attr == DW_AT_decl_file
6084       /* Or that this was compiled with a different compiler snapshot; if
6085          the output is the same, that's what matters.  */
6086       || at1->dw_attr == DW_AT_producer)
6087     return 1;
6088
6089   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6090 }
6091
6092 /* Do the dies look the same?  */
6093
6094 static int
6095 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6096 {
6097   dw_die_ref c1, c2;
6098   dw_attr_ref a1, a2;
6099
6100   /* To avoid infinite recursion.  */
6101   if (die1->die_mark)
6102     return die1->die_mark == die2->die_mark;
6103   die1->die_mark = die2->die_mark = ++(*mark);
6104
6105   if (die1->die_tag != die2->die_tag)
6106     return 0;
6107
6108   for (a1 = die1->die_attr, a2 = die2->die_attr;
6109        a1 && a2;
6110        a1 = a1->dw_attr_next, a2 = a2->dw_attr_next)
6111     if (!same_attr_p (a1, a2, mark))
6112       return 0;
6113   if (a1 || a2)
6114     return 0;
6115
6116   for (c1 = die1->die_child, c2 = die2->die_child;
6117        c1 && c2;
6118        c1 = c1->die_sib, c2 = c2->die_sib)
6119     if (!same_die_p (c1, c2, mark))
6120       return 0;
6121   if (c1 || c2)
6122     return 0;
6123
6124   return 1;
6125 }
6126
6127 /* Do the dies look the same?  Wrapper around same_die_p.  */
6128
6129 static int
6130 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6131 {
6132   int mark = 0;
6133   int ret = same_die_p (die1, die2, &mark);
6134
6135   unmark_all_dies (die1);
6136   unmark_all_dies (die2);
6137
6138   return ret;
6139 }
6140
6141 /* The prefix to attach to symbols on DIEs in the current comdat debug
6142    info section.  */
6143 static char *comdat_symbol_id;
6144
6145 /* The index of the current symbol within the current comdat CU.  */
6146 static unsigned int comdat_symbol_number;
6147
6148 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6149    children, and set comdat_symbol_id accordingly.  */
6150
6151 static void
6152 compute_section_prefix (dw_die_ref unit_die)
6153 {
6154   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6155   const char *base = die_name ? lbasename (die_name) : "anonymous";
6156   char *name = alloca (strlen (base) + 64);
6157   char *p;
6158   int i, mark;
6159   unsigned char checksum[16];
6160   struct md5_ctx ctx;
6161
6162   /* Compute the checksum of the DIE, then append part of it as hex digits to
6163      the name filename of the unit.  */
6164
6165   md5_init_ctx (&ctx);
6166   mark = 0;
6167   die_checksum (unit_die, &ctx, &mark);
6168   unmark_all_dies (unit_die);
6169   md5_finish_ctx (&ctx, checksum);
6170
6171   sprintf (name, "%s.", base);
6172   clean_symbol_name (name);
6173
6174   p = name + strlen (name);
6175   for (i = 0; i < 4; i++)
6176     {
6177       sprintf (p, "%.2x", checksum[i]);
6178       p += 2;
6179     }
6180
6181   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6182   comdat_symbol_number = 0;
6183 }
6184
6185 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6186
6187 static int
6188 is_type_die (dw_die_ref die)
6189 {
6190   switch (die->die_tag)
6191     {
6192     case DW_TAG_array_type:
6193     case DW_TAG_class_type:
6194     case DW_TAG_enumeration_type:
6195     case DW_TAG_pointer_type:
6196     case DW_TAG_reference_type:
6197     case DW_TAG_string_type:
6198     case DW_TAG_structure_type:
6199     case DW_TAG_subroutine_type:
6200     case DW_TAG_union_type:
6201     case DW_TAG_ptr_to_member_type:
6202     case DW_TAG_set_type:
6203     case DW_TAG_subrange_type:
6204     case DW_TAG_base_type:
6205     case DW_TAG_const_type:
6206     case DW_TAG_file_type:
6207     case DW_TAG_packed_type:
6208     case DW_TAG_volatile_type:
6209     case DW_TAG_typedef:
6210       return 1;
6211     default:
6212       return 0;
6213     }
6214 }
6215
6216 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6217    Basically, we want to choose the bits that are likely to be shared between
6218    compilations (types) and leave out the bits that are specific to individual
6219    compilations (functions).  */
6220
6221 static int
6222 is_comdat_die (dw_die_ref c)
6223 {
6224   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6225      we do for stabs.  The advantage is a greater likelihood of sharing between
6226      objects that don't include headers in the same order (and therefore would
6227      put the base types in a different comdat).  jason 8/28/00 */
6228
6229   if (c->die_tag == DW_TAG_base_type)
6230     return 0;
6231
6232   if (c->die_tag == DW_TAG_pointer_type
6233       || c->die_tag == DW_TAG_reference_type
6234       || c->die_tag == DW_TAG_const_type
6235       || c->die_tag == DW_TAG_volatile_type)
6236     {
6237       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6238
6239       return t ? is_comdat_die (t) : 0;
6240     }
6241
6242   return is_type_die (c);
6243 }
6244
6245 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6246    compilation unit.  */
6247
6248 static int
6249 is_symbol_die (dw_die_ref c)
6250 {
6251   return (is_type_die (c)
6252           || (get_AT (c, DW_AT_declaration)
6253               && !get_AT (c, DW_AT_specification)));
6254 }
6255
6256 static char *
6257 gen_internal_sym (const char *prefix)
6258 {
6259   char buf[256];
6260
6261   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6262   return xstrdup (buf);
6263 }
6264
6265 /* Assign symbols to all worthy DIEs under DIE.  */
6266
6267 static void
6268 assign_symbol_names (dw_die_ref die)
6269 {
6270   dw_die_ref c;
6271
6272   if (is_symbol_die (die))
6273     {
6274       if (comdat_symbol_id)
6275         {
6276           char *p = alloca (strlen (comdat_symbol_id) + 64);
6277
6278           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6279                    comdat_symbol_id, comdat_symbol_number++);
6280           die->die_symbol = xstrdup (p);
6281         }
6282       else
6283         die->die_symbol = gen_internal_sym ("LDIE");
6284     }
6285
6286   for (c = die->die_child; c != NULL; c = c->die_sib)
6287     assign_symbol_names (c);
6288 }
6289
6290 struct cu_hash_table_entry
6291 {
6292   dw_die_ref cu;
6293   unsigned min_comdat_num, max_comdat_num;
6294   struct cu_hash_table_entry *next;
6295 };
6296
6297 /* Routines to manipulate hash table of CUs.  */
6298 static hashval_t
6299 htab_cu_hash (const void *of)
6300 {
6301   const struct cu_hash_table_entry *entry = of;
6302
6303   return htab_hash_string (entry->cu->die_symbol);
6304 }
6305
6306 static int
6307 htab_cu_eq (const void *of1, const void *of2)
6308 {
6309   const struct cu_hash_table_entry *entry1 = of1;
6310   const struct die_struct *entry2 = of2;
6311
6312   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6313 }
6314
6315 static void
6316 htab_cu_del (void *what)
6317 {
6318   struct cu_hash_table_entry *next, *entry = what;
6319
6320   while (entry)
6321     {
6322       next = entry->next;
6323       free (entry);
6324       entry = next;
6325     }
6326 }
6327
6328 /* Check whether we have already seen this CU and set up SYM_NUM
6329    accordingly.  */
6330 static int
6331 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6332 {
6333   struct cu_hash_table_entry dummy;
6334   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6335
6336   dummy.max_comdat_num = 0;
6337
6338   slot = (struct cu_hash_table_entry **)
6339     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6340         INSERT);
6341   entry = *slot;
6342
6343   for (; entry; last = entry, entry = entry->next)
6344     {
6345       if (same_die_p_wrap (cu, entry->cu))
6346         break;
6347     }
6348
6349   if (entry)
6350     {
6351       *sym_num = entry->min_comdat_num;
6352       return 1;
6353     }
6354
6355   entry = XCNEW (struct cu_hash_table_entry);
6356   entry->cu = cu;
6357   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6358   entry->next = *slot;
6359   *slot = entry;
6360
6361   return 0;
6362 }
6363
6364 /* Record SYM_NUM to record of CU in HTABLE.  */
6365 static void
6366 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6367 {
6368   struct cu_hash_table_entry **slot, *entry;
6369
6370   slot = (struct cu_hash_table_entry **)
6371     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6372         NO_INSERT);
6373   entry = *slot;
6374
6375   entry->max_comdat_num = sym_num;
6376 }
6377
6378 /* Traverse the DIE (which is always comp_unit_die), and set up
6379    additional compilation units for each of the include files we see
6380    bracketed by BINCL/EINCL.  */
6381
6382 static void
6383 break_out_includes (dw_die_ref die)
6384 {
6385   dw_die_ref *ptr;
6386   dw_die_ref unit = NULL;
6387   limbo_die_node *node, **pnode;
6388   htab_t cu_hash_table;
6389
6390   for (ptr = &(die->die_child); *ptr;)
6391     {
6392       dw_die_ref c = *ptr;
6393
6394       if (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6395           || (unit && is_comdat_die (c)))
6396         {
6397           /* This DIE is for a secondary CU; remove it from the main one.  */
6398           *ptr = c->die_sib;
6399
6400           if (c->die_tag == DW_TAG_GNU_BINCL)
6401             {
6402               unit = push_new_compile_unit (unit, c);
6403               free_die (c);
6404             }
6405           else if (c->die_tag == DW_TAG_GNU_EINCL)
6406             {
6407               unit = pop_compile_unit (unit);
6408               free_die (c);
6409             }
6410           else
6411             add_child_die (unit, c);
6412         }
6413       else
6414         {
6415           /* Leave this DIE in the main CU.  */
6416           ptr = &(c->die_sib);
6417           continue;
6418         }
6419     }
6420
6421 #if 0
6422   /* We can only use this in debugging, since the frontend doesn't check
6423      to make sure that we leave every include file we enter.  */
6424   gcc_assert (!unit);
6425 #endif
6426
6427   assign_symbol_names (die);
6428   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6429   for (node = limbo_die_list, pnode = &limbo_die_list;
6430        node;
6431        node = node->next)
6432     {
6433       int is_dupl;
6434
6435       compute_section_prefix (node->die);
6436       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6437                         &comdat_symbol_number);
6438       assign_symbol_names (node->die);
6439       if (is_dupl)
6440         *pnode = node->next;
6441       else
6442         {
6443           pnode = &node->next;
6444           record_comdat_symbol_number (node->die, cu_hash_table,
6445                 comdat_symbol_number);
6446         }
6447     }
6448   htab_delete (cu_hash_table);
6449 }
6450
6451 /* Traverse the DIE and add a sibling attribute if it may have the
6452    effect of speeding up access to siblings.  To save some space,
6453    avoid generating sibling attributes for DIE's without children.  */
6454
6455 static void
6456 add_sibling_attributes (dw_die_ref die)
6457 {
6458   dw_die_ref c;
6459
6460   if (die->die_tag != DW_TAG_compile_unit
6461       && die->die_sib && die->die_child != NULL)
6462     /* Add the sibling link to the front of the attribute list.  */
6463     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6464
6465   for (c = die->die_child; c != NULL; c = c->die_sib)
6466     add_sibling_attributes (c);
6467 }
6468
6469 /* Output all location lists for the DIE and its children.  */
6470
6471 static void
6472 output_location_lists (dw_die_ref die)
6473 {
6474   dw_die_ref c;
6475   dw_attr_ref d_attr;
6476
6477   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6478     if (AT_class (d_attr) == dw_val_class_loc_list)
6479       output_loc_list (AT_loc_list (d_attr));
6480
6481   for (c = die->die_child; c != NULL; c = c->die_sib)
6482     output_location_lists (c);
6483
6484 }
6485
6486 /* The format of each DIE (and its attribute value pairs) is encoded in an
6487    abbreviation table.  This routine builds the abbreviation table and assigns
6488    a unique abbreviation id for each abbreviation entry.  The children of each
6489    die are visited recursively.  */
6490
6491 static void
6492 build_abbrev_table (dw_die_ref die)
6493 {
6494   unsigned long abbrev_id;
6495   unsigned int n_alloc;
6496   dw_die_ref c;
6497   dw_attr_ref d_attr, a_attr;
6498
6499   /* Scan the DIE references, and mark as external any that refer to
6500      DIEs from other CUs (i.e. those which are not marked).  */
6501   for (d_attr = die->die_attr; d_attr; d_attr = d_attr->dw_attr_next)
6502     if (AT_class (d_attr) == dw_val_class_die_ref
6503         && AT_ref (d_attr)->die_mark == 0)
6504       {
6505         gcc_assert (AT_ref (d_attr)->die_symbol);
6506
6507         set_AT_ref_external (d_attr, 1);
6508       }
6509
6510   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6511     {
6512       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6513
6514       if (abbrev->die_tag == die->die_tag)
6515         {
6516           if ((abbrev->die_child != NULL) == (die->die_child != NULL))
6517             {
6518               a_attr = abbrev->die_attr;
6519               d_attr = die->die_attr;
6520
6521               while (a_attr != NULL && d_attr != NULL)
6522                 {
6523                   if ((a_attr->dw_attr != d_attr->dw_attr)
6524                       || (value_format (a_attr) != value_format (d_attr)))
6525                     break;
6526
6527                   a_attr = a_attr->dw_attr_next;
6528                   d_attr = d_attr->dw_attr_next;
6529                 }
6530
6531               if (a_attr == NULL && d_attr == NULL)
6532                 break;
6533             }
6534         }
6535     }
6536
6537   if (abbrev_id >= abbrev_die_table_in_use)
6538     {
6539       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6540         {
6541           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6542           abbrev_die_table = ggc_realloc (abbrev_die_table,
6543                                           sizeof (dw_die_ref) * n_alloc);
6544
6545           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6546                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6547           abbrev_die_table_allocated = n_alloc;
6548         }
6549
6550       ++abbrev_die_table_in_use;
6551       abbrev_die_table[abbrev_id] = die;
6552     }
6553
6554   die->die_abbrev = abbrev_id;
6555   for (c = die->die_child; c != NULL; c = c->die_sib)
6556     build_abbrev_table (c);
6557 }
6558 \f
6559 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6560
6561 static int
6562 constant_size (long unsigned int value)
6563 {
6564   int log;
6565
6566   if (value == 0)
6567     log = 0;
6568   else
6569     log = floor_log2 (value);
6570
6571   log = log / 8;
6572   log = 1 << (floor_log2 (log) + 1);
6573
6574   return log;
6575 }
6576
6577 /* Return the size of a DIE as it is represented in the
6578    .debug_info section.  */
6579
6580 static unsigned long
6581 size_of_die (dw_die_ref die)
6582 {
6583   unsigned long size = 0;
6584   dw_attr_ref a;
6585
6586   size += size_of_uleb128 (die->die_abbrev);
6587   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
6588     {
6589       switch (AT_class (a))
6590         {
6591         case dw_val_class_addr:
6592           size += DWARF2_ADDR_SIZE;
6593           break;
6594         case dw_val_class_offset:
6595           size += DWARF_OFFSET_SIZE;
6596           break;
6597         case dw_val_class_loc:
6598           {
6599             unsigned long lsize = size_of_locs (AT_loc (a));
6600
6601             /* Block length.  */
6602             size += constant_size (lsize);
6603             size += lsize;
6604           }
6605           break;
6606         case dw_val_class_loc_list:
6607           size += DWARF_OFFSET_SIZE;
6608           break;
6609         case dw_val_class_range_list:
6610           size += DWARF_OFFSET_SIZE;
6611           break;
6612         case dw_val_class_const:
6613           size += size_of_sleb128 (AT_int (a));
6614           break;
6615         case dw_val_class_unsigned_const:
6616           size += constant_size (AT_unsigned (a));
6617           break;
6618         case dw_val_class_long_long:
6619           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6620           break;
6621         case dw_val_class_vec:
6622           size += 1 + (a->dw_attr_val.v.val_vec.length
6623                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6624           break;
6625         case dw_val_class_flag:
6626           size += 1;
6627           break;
6628         case dw_val_class_die_ref:
6629           if (AT_ref_external (a))
6630             size += DWARF2_ADDR_SIZE;
6631           else
6632             size += DWARF_OFFSET_SIZE;
6633           break;
6634         case dw_val_class_fde_ref:
6635           size += DWARF_OFFSET_SIZE;
6636           break;
6637         case dw_val_class_lbl_id:
6638           size += DWARF2_ADDR_SIZE;
6639           break;
6640         case dw_val_class_lineptr:
6641         case dw_val_class_macptr:
6642           size += DWARF_OFFSET_SIZE;
6643           break;
6644         case dw_val_class_str:
6645           if (AT_string_form (a) == DW_FORM_strp)
6646             size += DWARF_OFFSET_SIZE;
6647           else
6648             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6649           break;
6650         default:
6651           gcc_unreachable ();
6652         }
6653     }
6654
6655   return size;
6656 }
6657
6658 /* Size the debugging information associated with a given DIE.  Visits the
6659    DIE's children recursively.  Updates the global variable next_die_offset, on
6660    each time through.  Uses the current value of next_die_offset to update the
6661    die_offset field in each DIE.  */
6662
6663 static void
6664 calc_die_sizes (dw_die_ref die)
6665 {
6666   dw_die_ref c;
6667
6668   die->die_offset = next_die_offset;
6669   next_die_offset += size_of_die (die);
6670
6671   for (c = die->die_child; c != NULL; c = c->die_sib)
6672     calc_die_sizes (c);
6673
6674   if (die->die_child != NULL)
6675     /* Count the null byte used to terminate sibling lists.  */
6676     next_die_offset += 1;
6677 }
6678
6679 /* Set the marks for a die and its children.  We do this so
6680    that we know whether or not a reference needs to use FORM_ref_addr; only
6681    DIEs in the same CU will be marked.  We used to clear out the offset
6682    and use that as the flag, but ran into ordering problems.  */
6683
6684 static void
6685 mark_dies (dw_die_ref die)
6686 {
6687   dw_die_ref c;
6688
6689   gcc_assert (!die->die_mark);
6690
6691   die->die_mark = 1;
6692   for (c = die->die_child; c; c = c->die_sib)
6693     mark_dies (c);
6694 }
6695
6696 /* Clear the marks for a die and its children.  */
6697
6698 static void
6699 unmark_dies (dw_die_ref die)
6700 {
6701   dw_die_ref c;
6702
6703   gcc_assert (die->die_mark);
6704
6705   die->die_mark = 0;
6706   for (c = die->die_child; c; c = c->die_sib)
6707     unmark_dies (c);
6708 }
6709
6710 /* Clear the marks for a die, its children and referred dies.  */
6711
6712 static void
6713 unmark_all_dies (dw_die_ref die)
6714 {
6715   dw_die_ref c;
6716   dw_attr_ref a;
6717
6718   if (!die->die_mark)
6719     return;
6720   die->die_mark = 0;
6721
6722   for (c = die->die_child; c; c = c->die_sib)
6723     unmark_all_dies (c);
6724
6725   for (a = die->die_attr; a; a = a->dw_attr_next)
6726     if (AT_class (a) == dw_val_class_die_ref)
6727       unmark_all_dies (AT_ref (a));
6728 }
6729
6730 /* Return the size of the .debug_pubnames table  generated for the
6731    compilation unit.  */
6732
6733 static unsigned long
6734 size_of_pubnames (void)
6735 {
6736   unsigned long size;
6737   unsigned i;
6738
6739   size = DWARF_PUBNAMES_HEADER_SIZE;
6740   for (i = 0; i < pubname_table_in_use; i++)
6741     {
6742       pubname_ref p = &pubname_table[i];
6743       size += DWARF_OFFSET_SIZE + strlen (p->name) + 1;
6744     }
6745
6746   size += DWARF_OFFSET_SIZE;
6747   return size;
6748 }
6749
6750 /* Return the size of the information in the .debug_aranges section.  */
6751
6752 static unsigned long
6753 size_of_aranges (void)
6754 {
6755   unsigned long size;
6756
6757   size = DWARF_ARANGES_HEADER_SIZE;
6758
6759   /* Count the address/length pair for this compilation unit.  */
6760   size += 2 * DWARF2_ADDR_SIZE;
6761   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6762
6763   /* Count the two zero words used to terminated the address range table.  */
6764   size += 2 * DWARF2_ADDR_SIZE;
6765   return size;
6766 }
6767 \f
6768 /* Select the encoding of an attribute value.  */
6769
6770 static enum dwarf_form
6771 value_format (dw_attr_ref a)
6772 {
6773   switch (a->dw_attr_val.val_class)
6774     {
6775     case dw_val_class_addr:
6776       return DW_FORM_addr;
6777     case dw_val_class_range_list:
6778     case dw_val_class_offset:
6779       switch (DWARF_OFFSET_SIZE)
6780         {
6781         case 4:
6782           return DW_FORM_data4;
6783         case 8:
6784           return DW_FORM_data8;
6785         default:
6786           gcc_unreachable ();
6787         }
6788     case dw_val_class_loc_list:
6789       /* FIXME: Could be DW_FORM_data8, with a > 32 bit size
6790          .debug_loc section */
6791       return DW_FORM_data4;
6792     case dw_val_class_loc:
6793       switch (constant_size (size_of_locs (AT_loc (a))))
6794         {
6795         case 1:
6796           return DW_FORM_block1;
6797         case 2:
6798           return DW_FORM_block2;
6799         default:
6800           gcc_unreachable ();
6801         }
6802     case dw_val_class_const:
6803       return DW_FORM_sdata;
6804     case dw_val_class_unsigned_const:
6805       switch (constant_size (AT_unsigned (a)))
6806         {
6807         case 1:
6808           return DW_FORM_data1;
6809         case 2:
6810           return DW_FORM_data2;
6811         case 4:
6812           return DW_FORM_data4;
6813         case 8:
6814           return DW_FORM_data8;
6815         default:
6816           gcc_unreachable ();
6817         }
6818     case dw_val_class_long_long:
6819       return DW_FORM_block1;
6820     case dw_val_class_vec:
6821       return DW_FORM_block1;
6822     case dw_val_class_flag:
6823       return DW_FORM_flag;
6824     case dw_val_class_die_ref:
6825       if (AT_ref_external (a))
6826         return DW_FORM_ref_addr;
6827       else
6828         return DW_FORM_ref;
6829     case dw_val_class_fde_ref:
6830       return DW_FORM_data;
6831     case dw_val_class_lbl_id:
6832       return DW_FORM_addr;
6833     case dw_val_class_lineptr:
6834     case dw_val_class_macptr:
6835       return DW_FORM_data;
6836     case dw_val_class_str:
6837       return AT_string_form (a);
6838
6839     default:
6840       gcc_unreachable ();
6841     }
6842 }
6843
6844 /* Output the encoding of an attribute value.  */
6845
6846 static void
6847 output_value_format (dw_attr_ref a)
6848 {
6849   enum dwarf_form form = value_format (a);
6850
6851   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6852 }
6853
6854 /* Output the .debug_abbrev section which defines the DIE abbreviation
6855    table.  */
6856
6857 static void
6858 output_abbrev_section (void)
6859 {
6860   unsigned long abbrev_id;
6861
6862   dw_attr_ref a_attr;
6863
6864   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6865     {
6866       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6867
6868       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6869       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6870                                    dwarf_tag_name (abbrev->die_tag));
6871
6872       if (abbrev->die_child != NULL)
6873         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6874       else
6875         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6876
6877       for (a_attr = abbrev->die_attr; a_attr != NULL;
6878            a_attr = a_attr->dw_attr_next)
6879         {
6880           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6881                                        dwarf_attr_name (a_attr->dw_attr));
6882           output_value_format (a_attr);
6883         }
6884
6885       dw2_asm_output_data (1, 0, NULL);
6886       dw2_asm_output_data (1, 0, NULL);
6887     }
6888
6889   /* Terminate the table.  */
6890   dw2_asm_output_data (1, 0, NULL);
6891 }
6892
6893 /* Output a symbol we can use to refer to this DIE from another CU.  */
6894
6895 static inline void
6896 output_die_symbol (dw_die_ref die)
6897 {
6898   char *sym = die->die_symbol;
6899
6900   if (sym == 0)
6901     return;
6902
6903   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6904     /* We make these global, not weak; if the target doesn't support
6905        .linkonce, it doesn't support combining the sections, so debugging
6906        will break.  */
6907     targetm.asm_out.globalize_label (asm_out_file, sym);
6908
6909   ASM_OUTPUT_LABEL (asm_out_file, sym);
6910 }
6911
6912 /* Return a new location list, given the begin and end range, and the
6913    expression. gensym tells us whether to generate a new internal symbol for
6914    this location list node, which is done for the head of the list only.  */
6915
6916 static inline dw_loc_list_ref
6917 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6918               const char *section, unsigned int gensym)
6919 {
6920   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6921
6922   retlist->begin = begin;
6923   retlist->end = end;
6924   retlist->expr = expr;
6925   retlist->section = section;
6926   if (gensym)
6927     retlist->ll_symbol = gen_internal_sym ("LLST");
6928
6929   return retlist;
6930 }
6931
6932 /* Add a location description expression to a location list.  */
6933
6934 static inline void
6935 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6936                            const char *begin, const char *end,
6937                            const char *section)
6938 {
6939   dw_loc_list_ref *d;
6940
6941   /* Find the end of the chain.  */
6942   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
6943     ;
6944
6945   /* Add a new location list node to the list.  */
6946   *d = new_loc_list (descr, begin, end, section, 0);
6947 }
6948
6949 static void
6950 dwarf2out_switch_text_section (void)
6951 {
6952   dw_fde_ref fde;
6953
6954   gcc_assert (cfun);
6955
6956   fde = &fde_table[fde_table_in_use - 1];
6957   fde->dw_fde_switched_sections = true;
6958   fde->dw_fde_hot_section_label = cfun->hot_section_label;
6959   fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
6960   fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
6961   fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
6962   have_multiple_function_sections = true;
6963 }
6964
6965 /* Output the location list given to us.  */
6966
6967 static void
6968 output_loc_list (dw_loc_list_ref list_head)
6969 {
6970   dw_loc_list_ref curr = list_head;
6971
6972   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
6973
6974   /* Walk the location list, and output each range + expression.  */
6975   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
6976     {
6977       unsigned long size;
6978       if (!have_multiple_function_sections)
6979         {
6980           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
6981                                 "Location list begin address (%s)",
6982                                 list_head->ll_symbol);
6983           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
6984                                 "Location list end address (%s)",
6985                                 list_head->ll_symbol);
6986         }
6987       else
6988         {
6989           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
6990                                "Location list begin address (%s)",
6991                                list_head->ll_symbol);
6992           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
6993                                "Location list end address (%s)",
6994                                list_head->ll_symbol);
6995         }
6996       size = size_of_locs (curr->expr);
6997
6998       /* Output the block length for this list of location operations.  */
6999       gcc_assert (size <= 0xffff);
7000       dw2_asm_output_data (2, size, "%s", "Location expression size");
7001
7002       output_loc_sequence (curr->expr);
7003     }
7004
7005   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7006                        "Location list terminator begin (%s)",
7007                        list_head->ll_symbol);
7008   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7009                        "Location list terminator end (%s)",
7010                        list_head->ll_symbol);
7011 }
7012
7013 /* Output the DIE and its attributes.  Called recursively to generate
7014    the definitions of each child DIE.  */
7015
7016 static void
7017 output_die (dw_die_ref die)
7018 {
7019   dw_attr_ref a;
7020   dw_die_ref c;
7021   unsigned long size;
7022
7023   /* If someone in another CU might refer to us, set up a symbol for
7024      them to point to.  */
7025   if (die->die_symbol)
7026     output_die_symbol (die);
7027
7028   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7029                                die->die_offset, dwarf_tag_name (die->die_tag));
7030
7031   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
7032     {
7033       const char *name = dwarf_attr_name (a->dw_attr);
7034
7035       switch (AT_class (a))
7036         {
7037         case dw_val_class_addr:
7038           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7039           break;
7040
7041         case dw_val_class_offset:
7042           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7043                                "%s", name);
7044           break;
7045
7046         case dw_val_class_range_list:
7047           {
7048             char *p = strchr (ranges_section_label, '\0');
7049
7050             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7051                      a->dw_attr_val.v.val_offset);
7052             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7053                                    debug_ranges_section, "%s", name);
7054             *p = '\0';
7055           }
7056           break;
7057
7058         case dw_val_class_loc:
7059           size = size_of_locs (AT_loc (a));
7060
7061           /* Output the block length for this list of location operations.  */
7062           dw2_asm_output_data (constant_size (size), size, "%s", name);
7063
7064           output_loc_sequence (AT_loc (a));
7065           break;
7066
7067         case dw_val_class_const:
7068           /* ??? It would be slightly more efficient to use a scheme like is
7069              used for unsigned constants below, but gdb 4.x does not sign
7070              extend.  Gdb 5.x does sign extend.  */
7071           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7072           break;
7073
7074         case dw_val_class_unsigned_const:
7075           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7076                                AT_unsigned (a), "%s", name);
7077           break;
7078
7079         case dw_val_class_long_long:
7080           {
7081             unsigned HOST_WIDE_INT first, second;
7082
7083             dw2_asm_output_data (1,
7084                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7085                                  "%s", name);
7086
7087             if (WORDS_BIG_ENDIAN)
7088               {
7089                 first = a->dw_attr_val.v.val_long_long.hi;
7090                 second = a->dw_attr_val.v.val_long_long.low;
7091               }
7092             else
7093               {
7094                 first = a->dw_attr_val.v.val_long_long.low;
7095                 second = a->dw_attr_val.v.val_long_long.hi;
7096               }
7097
7098             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7099                                  first, "long long constant");
7100             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7101                                  second, NULL);
7102           }
7103           break;
7104
7105         case dw_val_class_vec:
7106           {
7107             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7108             unsigned int len = a->dw_attr_val.v.val_vec.length;
7109             unsigned int i;
7110             unsigned char *p;
7111
7112             dw2_asm_output_data (1, len * elt_size, "%s", name);
7113             if (elt_size > sizeof (HOST_WIDE_INT))
7114               {
7115                 elt_size /= 2;
7116                 len *= 2;
7117               }
7118             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7119                  i < len;
7120                  i++, p += elt_size)
7121               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7122                                    "fp or vector constant word %u", i);
7123             break;
7124           }
7125
7126         case dw_val_class_flag:
7127           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7128           break;
7129
7130         case dw_val_class_loc_list:
7131           {
7132             char *sym = AT_loc_list (a)->ll_symbol;
7133
7134             gcc_assert (sym);
7135             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7136                                    "%s", name);
7137           }
7138           break;
7139
7140         case dw_val_class_die_ref:
7141           if (AT_ref_external (a))
7142             {
7143               char *sym = AT_ref (a)->die_symbol;
7144
7145               gcc_assert (sym);
7146               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7147                                      "%s", name);
7148             }
7149           else
7150             {
7151               gcc_assert (AT_ref (a)->die_offset);
7152               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7153                                    "%s", name);
7154             }
7155           break;
7156
7157         case dw_val_class_fde_ref:
7158           {
7159             char l1[20];
7160
7161             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7162                                          a->dw_attr_val.v.val_fde_index * 2);
7163             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7164                                    "%s", name);
7165           }
7166           break;
7167
7168         case dw_val_class_lbl_id:
7169           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7170           break;
7171
7172         case dw_val_class_lineptr:
7173           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7174                                  debug_line_section, "%s", name);
7175           break;
7176
7177         case dw_val_class_macptr:
7178           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7179                                  debug_macinfo_section, "%s", name);
7180           break;
7181
7182         case dw_val_class_str:
7183           if (AT_string_form (a) == DW_FORM_strp)
7184             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7185                                    a->dw_attr_val.v.val_str->label,
7186                                    debug_str_section,
7187                                    "%s: \"%s\"", name, AT_string (a));
7188           else
7189             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7190           break;
7191
7192         default:
7193           gcc_unreachable ();
7194         }
7195     }
7196
7197   for (c = die->die_child; c != NULL; c = c->die_sib)
7198     output_die (c);
7199
7200   /* Add null byte to terminate sibling list.  */
7201   if (die->die_child != NULL)
7202     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7203                          die->die_offset);
7204 }
7205
7206 /* Output the compilation unit that appears at the beginning of the
7207    .debug_info section, and precedes the DIE descriptions.  */
7208
7209 static void
7210 output_compilation_unit_header (void)
7211 {
7212   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7213     dw2_asm_output_data (4, 0xffffffff,
7214       "Initial length escape value indicating 64-bit DWARF extension");
7215   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7216                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7217                        "Length of Compilation Unit Info");
7218   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7219   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7220                          debug_abbrev_section,
7221                          "Offset Into Abbrev. Section");
7222   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7223 }
7224
7225 /* Output the compilation unit DIE and its children.  */
7226
7227 static void
7228 output_comp_unit (dw_die_ref die, int output_if_empty)
7229 {
7230   const char *secname;
7231   char *oldsym, *tmp;
7232
7233   /* Unless we are outputting main CU, we may throw away empty ones.  */
7234   if (!output_if_empty && die->die_child == NULL)
7235     return;
7236
7237   /* Even if there are no children of this DIE, we must output the information
7238      about the compilation unit.  Otherwise, on an empty translation unit, we
7239      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7240      will then complain when examining the file.  First mark all the DIEs in
7241      this CU so we know which get local refs.  */
7242   mark_dies (die);
7243
7244   build_abbrev_table (die);
7245
7246   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7247   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7248   calc_die_sizes (die);
7249
7250   oldsym = die->die_symbol;
7251   if (oldsym)
7252     {
7253       tmp = alloca (strlen (oldsym) + 24);
7254
7255       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7256       secname = tmp;
7257       die->die_symbol = NULL;
7258       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7259     }
7260   else
7261     switch_to_section (debug_info_section);
7262
7263   /* Output debugging information.  */
7264   output_compilation_unit_header ();
7265   output_die (die);
7266
7267   /* Leave the marks on the main CU, so we can check them in
7268      output_pubnames.  */
7269   if (oldsym)
7270     {
7271       unmark_dies (die);
7272       die->die_symbol = oldsym;
7273     }
7274 }
7275
7276 /* The DWARF2 pubname for a nested thingy looks like "A::f".  The
7277    output of lang_hooks.decl_printable_name for C++ looks like
7278    "A::f(int)".  Let's drop the argument list, and maybe the scope.  */
7279
7280 static const char *
7281 dwarf2_name (tree decl, int scope)
7282 {
7283   return lang_hooks.decl_printable_name (decl, scope ? 1 : 0);
7284 }
7285
7286 /* Add a new entry to .debug_pubnames if appropriate.  */
7287
7288 static void
7289 add_pubname (tree decl, dw_die_ref die)
7290 {
7291   pubname_ref p;
7292
7293   if (! TREE_PUBLIC (decl))
7294     return;
7295
7296   if (pubname_table_in_use == pubname_table_allocated)
7297     {
7298       pubname_table_allocated += PUBNAME_TABLE_INCREMENT;
7299       pubname_table
7300         = ggc_realloc (pubname_table,
7301                        (pubname_table_allocated * sizeof (pubname_entry)));
7302       memset (pubname_table + pubname_table_in_use, 0,
7303               PUBNAME_TABLE_INCREMENT * sizeof (pubname_entry));
7304     }
7305
7306   p = &pubname_table[pubname_table_in_use++];
7307   p->die = die;
7308   p->name = xstrdup (dwarf2_name (decl, 1));
7309 }
7310
7311 /* Output the public names table used to speed up access to externally
7312    visible names.  For now, only generate entries for externally
7313    visible procedures.  */
7314
7315 static void
7316 output_pubnames (void)
7317 {
7318   unsigned i;
7319   unsigned long pubnames_length = size_of_pubnames ();
7320
7321   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7322     dw2_asm_output_data (4, 0xffffffff,
7323       "Initial length escape value indicating 64-bit DWARF extension");
7324   dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7325                        "Length of Public Names Info");
7326   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7327   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7328                          debug_info_section,
7329                          "Offset of Compilation Unit Info");
7330   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7331                        "Compilation Unit Length");
7332
7333   for (i = 0; i < pubname_table_in_use; i++)
7334     {
7335       pubname_ref pub = &pubname_table[i];
7336
7337       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7338       gcc_assert (pub->die->die_mark);
7339
7340       dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7341                            "DIE offset");
7342
7343       dw2_asm_output_nstring (pub->name, -1, "external name");
7344     }
7345
7346   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7347 }
7348
7349 /* Add a new entry to .debug_aranges if appropriate.  */
7350
7351 static void
7352 add_arange (tree decl, dw_die_ref die)
7353 {
7354   if (! DECL_SECTION_NAME (decl))
7355     return;
7356
7357   if (arange_table_in_use == arange_table_allocated)
7358     {
7359       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7360       arange_table = ggc_realloc (arange_table,
7361                                   (arange_table_allocated
7362                                    * sizeof (dw_die_ref)));
7363       memset (arange_table + arange_table_in_use, 0,
7364               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7365     }
7366
7367   arange_table[arange_table_in_use++] = die;
7368 }
7369
7370 /* Output the information that goes into the .debug_aranges table.
7371    Namely, define the beginning and ending address range of the
7372    text section generated for this compilation unit.  */
7373
7374 static void
7375 output_aranges (void)
7376 {
7377   unsigned i;
7378   unsigned long aranges_length = size_of_aranges ();
7379
7380   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7381     dw2_asm_output_data (4, 0xffffffff,
7382       "Initial length escape value indicating 64-bit DWARF extension");
7383   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7384                        "Length of Address Ranges Info");
7385   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7386   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7387                          debug_info_section,
7388                          "Offset of Compilation Unit Info");
7389   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7390   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7391
7392   /* We need to align to twice the pointer size here.  */
7393   if (DWARF_ARANGES_PAD_SIZE)
7394     {
7395       /* Pad using a 2 byte words so that padding is correct for any
7396          pointer size.  */
7397       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7398                            2 * DWARF2_ADDR_SIZE);
7399       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7400         dw2_asm_output_data (2, 0, NULL);
7401     }
7402
7403   dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7404   dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7405                         text_section_label, "Length");
7406   if (flag_reorder_blocks_and_partition)
7407     {
7408       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label, 
7409                            "Address");
7410       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7411                             cold_text_section_label, "Length");
7412     }
7413
7414   for (i = 0; i < arange_table_in_use; i++)
7415     {
7416       dw_die_ref die = arange_table[i];
7417
7418       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7419       gcc_assert (die->die_mark);
7420
7421       if (die->die_tag == DW_TAG_subprogram)
7422         {
7423           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7424                                "Address");
7425           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7426                                 get_AT_low_pc (die), "Length");
7427         }
7428       else
7429         {
7430           /* A static variable; extract the symbol from DW_AT_location.
7431              Note that this code isn't currently hit, as we only emit
7432              aranges for functions (jason 9/23/99).  */
7433           dw_attr_ref a = get_AT (die, DW_AT_location);
7434           dw_loc_descr_ref loc;
7435
7436           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7437
7438           loc = AT_loc (a);
7439           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7440
7441           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7442                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7443           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7444                                get_AT_unsigned (die, DW_AT_byte_size),
7445                                "Length");
7446         }
7447     }
7448
7449   /* Output the terminator words.  */
7450   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7451   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7452 }
7453
7454 /* Add a new entry to .debug_ranges.  Return the offset at which it
7455    was placed.  */
7456
7457 static unsigned int
7458 add_ranges (tree block)
7459 {
7460   unsigned int in_use = ranges_table_in_use;
7461
7462   if (in_use == ranges_table_allocated)
7463     {
7464       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7465       ranges_table
7466         = ggc_realloc (ranges_table, (ranges_table_allocated
7467                                       * sizeof (struct dw_ranges_struct)));
7468       memset (ranges_table + ranges_table_in_use, 0,
7469               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7470     }
7471
7472   ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7473   ranges_table_in_use = in_use + 1;
7474
7475   return in_use * 2 * DWARF2_ADDR_SIZE;
7476 }
7477
7478 static void
7479 output_ranges (void)
7480 {
7481   unsigned i;
7482   static const char *const start_fmt = "Offset 0x%x";
7483   const char *fmt = start_fmt;
7484
7485   for (i = 0; i < ranges_table_in_use; i++)
7486     {
7487       int block_num = ranges_table[i].block_num;
7488
7489       if (block_num)
7490         {
7491           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7492           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7493
7494           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7495           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7496
7497           /* If all code is in the text section, then the compilation
7498              unit base address defaults to DW_AT_low_pc, which is the
7499              base of the text section.  */
7500           if (!have_multiple_function_sections)
7501             {
7502               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7503                                     text_section_label,
7504                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7505               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7506                                     text_section_label, NULL);
7507             }
7508
7509           /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7510              compilation unit base address to zero, which allows us to
7511              use absolute addresses, and not worry about whether the
7512              target supports cross-section arithmetic.  */
7513           else
7514             {
7515               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7516                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7517               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7518             }
7519
7520           fmt = NULL;
7521         }
7522       else
7523         {
7524           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7525           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7526           fmt = start_fmt;
7527         }
7528     }
7529 }
7530
7531 /* Data structure containing information about input files.  */
7532 struct file_info
7533 {
7534   char *path;           /* Complete file name.  */
7535   char *fname;          /* File name part.  */
7536   int length;           /* Length of entire string.  */
7537   int file_idx;         /* Index in input file table.  */
7538   int dir_idx;          /* Index in directory table.  */
7539 };
7540
7541 /* Data structure containing information about directories with source
7542    files.  */
7543 struct dir_info
7544 {
7545   char *path;           /* Path including directory name.  */
7546   int length;           /* Path length.  */
7547   int prefix;           /* Index of directory entry which is a prefix.  */
7548   int count;            /* Number of files in this directory.  */
7549   int dir_idx;          /* Index of directory used as base.  */
7550   int used;             /* Used in the end?  */
7551 };
7552
7553 /* Callback function for file_info comparison.  We sort by looking at
7554    the directories in the path.  */
7555
7556 static int
7557 file_info_cmp (const void *p1, const void *p2)
7558 {
7559   const struct file_info *s1 = p1;
7560   const struct file_info *s2 = p2;
7561   unsigned char *cp1;
7562   unsigned char *cp2;
7563
7564   /* Take care of file names without directories.  We need to make sure that
7565      we return consistent values to qsort since some will get confused if
7566      we return the same value when identical operands are passed in opposite
7567      orders.  So if neither has a directory, return 0 and otherwise return
7568      1 or -1 depending on which one has the directory.  */
7569   if ((s1->path == s1->fname || s2->path == s2->fname))
7570     return (s2->path == s2->fname) - (s1->path == s1->fname);
7571
7572   cp1 = (unsigned char *) s1->path;
7573   cp2 = (unsigned char *) s2->path;
7574
7575   while (1)
7576     {
7577       ++cp1;
7578       ++cp2;
7579       /* Reached the end of the first path?  If so, handle like above.  */
7580       if ((cp1 == (unsigned char *) s1->fname)
7581           || (cp2 == (unsigned char *) s2->fname))
7582         return ((cp2 == (unsigned char *) s2->fname)
7583                 - (cp1 == (unsigned char *) s1->fname));
7584
7585       /* Character of current path component the same?  */
7586       else if (*cp1 != *cp2)
7587         return *cp1 - *cp2;
7588     }
7589 }
7590
7591 /* Output the directory table and the file name table.  We try to minimize
7592    the total amount of memory needed.  A heuristic is used to avoid large
7593    slowdowns with many input files.  */
7594
7595 static void
7596 output_file_names (void)
7597 {
7598   struct file_info *files;
7599   struct dir_info *dirs;
7600   int *saved;
7601   int *savehere;
7602   int *backmap;
7603   size_t ndirs;
7604   int idx_offset;
7605   size_t i;
7606   int idx;
7607
7608   /* Handle the case where file_table is empty.  */
7609   if (VARRAY_ACTIVE_SIZE (file_table) <= 1)
7610     {
7611       dw2_asm_output_data (1, 0, "End directory table");
7612       dw2_asm_output_data (1, 0, "End file name table");
7613       return;
7614     }
7615
7616   /* Allocate the various arrays we need.  */
7617   files = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct file_info));
7618   dirs = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (struct dir_info));
7619
7620   /* Sort the file names.  */
7621   for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7622     {
7623       char *f;
7624
7625       /* Skip all leading "./".  */
7626       f = VARRAY_CHAR_PTR (file_table, i);
7627       while (f[0] == '.' && f[1] == '/')
7628         f += 2;
7629
7630       /* Create a new array entry.  */
7631       files[i].path = f;
7632       files[i].length = strlen (f);
7633       files[i].file_idx = i;
7634
7635       /* Search for the file name part.  */
7636       f = strrchr (f, '/');
7637       files[i].fname = f == NULL ? files[i].path : f + 1;
7638     }
7639
7640   qsort (files + 1, VARRAY_ACTIVE_SIZE (file_table) - 1,
7641          sizeof (files[0]), file_info_cmp);
7642
7643   /* Find all the different directories used.  */
7644   dirs[0].path = files[1].path;
7645   dirs[0].length = files[1].fname - files[1].path;
7646   dirs[0].prefix = -1;
7647   dirs[0].count = 1;
7648   dirs[0].dir_idx = 0;
7649   dirs[0].used = 0;
7650   files[1].dir_idx = 0;
7651   ndirs = 1;
7652
7653   for (i = 2; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7654     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7655         && memcmp (dirs[ndirs - 1].path, files[i].path,
7656                    dirs[ndirs - 1].length) == 0)
7657       {
7658         /* Same directory as last entry.  */
7659         files[i].dir_idx = ndirs - 1;
7660         ++dirs[ndirs - 1].count;
7661       }
7662     else
7663       {
7664         size_t j;
7665
7666         /* This is a new directory.  */
7667         dirs[ndirs].path = files[i].path;
7668         dirs[ndirs].length = files[i].fname - files[i].path;
7669         dirs[ndirs].count = 1;
7670         dirs[ndirs].dir_idx = ndirs;
7671         dirs[ndirs].used = 0;
7672         files[i].dir_idx = ndirs;
7673
7674         /* Search for a prefix.  */
7675         dirs[ndirs].prefix = -1;
7676         for (j = 0; j < ndirs; j++)
7677           if (dirs[j].length < dirs[ndirs].length
7678               && dirs[j].length > 1
7679               && (dirs[ndirs].prefix == -1
7680                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7681               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7682             dirs[ndirs].prefix = j;
7683
7684         ++ndirs;
7685       }
7686
7687   /* Now to the actual work.  We have to find a subset of the directories which
7688      allow expressing the file name using references to the directory table
7689      with the least amount of characters.  We do not do an exhaustive search
7690      where we would have to check out every combination of every single
7691      possible prefix.  Instead we use a heuristic which provides nearly optimal
7692      results in most cases and never is much off.  */
7693   saved = alloca (ndirs * sizeof (int));
7694   savehere = alloca (ndirs * sizeof (int));
7695
7696   memset (saved, '\0', ndirs * sizeof (saved[0]));
7697   for (i = 0; i < ndirs; i++)
7698     {
7699       size_t j;
7700       int total;
7701
7702       /* We can always save some space for the current directory.  But this
7703          does not mean it will be enough to justify adding the directory.  */
7704       savehere[i] = dirs[i].length;
7705       total = (savehere[i] - saved[i]) * dirs[i].count;
7706
7707       for (j = i + 1; j < ndirs; j++)
7708         {
7709           savehere[j] = 0;
7710           if (saved[j] < dirs[i].length)
7711             {
7712               /* Determine whether the dirs[i] path is a prefix of the
7713                  dirs[j] path.  */
7714               int k;
7715
7716               k = dirs[j].prefix;
7717               while (k != -1 && k != (int) i)
7718                 k = dirs[k].prefix;
7719
7720               if (k == (int) i)
7721                 {
7722                   /* Yes it is.  We can possibly safe some memory but
7723                      writing the filenames in dirs[j] relative to
7724                      dirs[i].  */
7725                   savehere[j] = dirs[i].length;
7726                   total += (savehere[j] - saved[j]) * dirs[j].count;
7727                 }
7728             }
7729         }
7730
7731       /* Check whether we can safe enough to justify adding the dirs[i]
7732          directory.  */
7733       if (total > dirs[i].length + 1)
7734         {
7735           /* It's worthwhile adding.  */
7736           for (j = i; j < ndirs; j++)
7737             if (savehere[j] > 0)
7738               {
7739                 /* Remember how much we saved for this directory so far.  */
7740                 saved[j] = savehere[j];
7741
7742                 /* Remember the prefix directory.  */
7743                 dirs[j].dir_idx = i;
7744               }
7745         }
7746     }
7747
7748   /* We have to emit them in the order they appear in the file_table array
7749      since the index is used in the debug info generation.  To do this
7750      efficiently we generate a back-mapping of the indices first.  */
7751   backmap = alloca (VARRAY_ACTIVE_SIZE (file_table) * sizeof (int));
7752   for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7753     {
7754       backmap[files[i].file_idx] = i;
7755
7756       /* Mark this directory as used.  */
7757       dirs[dirs[files[i].dir_idx].dir_idx].used = 1;
7758     }
7759
7760   /* That was it.  We are ready to emit the information.  First emit the
7761      directory name table.  We have to make sure the first actually emitted
7762      directory name has index one; zero is reserved for the current working
7763      directory.  Make sure we do not confuse these indices with the one for the
7764      constructed table (even though most of the time they are identical).  */
7765   idx = 1;
7766   idx_offset = dirs[0].length > 0 ? 1 : 0;
7767   for (i = 1 - idx_offset; i < ndirs; i++)
7768     if (dirs[i].used != 0)
7769       {
7770         dirs[i].used = idx++;
7771         dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7772                                 "Directory Entry: 0x%x", dirs[i].used);
7773       }
7774
7775   dw2_asm_output_data (1, 0, "End directory table");
7776
7777   /* Correct the index for the current working directory entry if it
7778      exists.  */
7779   if (idx_offset == 0)
7780     dirs[0].used = 0;
7781
7782   /* Now write all the file names.  */
7783   for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
7784     {
7785       int file_idx = backmap[i];
7786       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7787
7788       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7789                               "File Entry: 0x%lx", (unsigned long) i);
7790
7791       /* Include directory index.  */
7792       dw2_asm_output_data_uleb128 (dirs[dir_idx].used, NULL);
7793
7794       /* Modification time.  */
7795       dw2_asm_output_data_uleb128 (0, NULL);
7796
7797       /* File length in bytes.  */
7798       dw2_asm_output_data_uleb128 (0, NULL);
7799     }
7800
7801   dw2_asm_output_data (1, 0, "End file name table");
7802 }
7803
7804
7805 /* Output the source line number correspondence information.  This
7806    information goes into the .debug_line section.  */
7807
7808 static void
7809 output_line_info (void)
7810 {
7811   char l1[20], l2[20], p1[20], p2[20];
7812   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7813   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7814   unsigned opc;
7815   unsigned n_op_args;
7816   unsigned long lt_index;
7817   unsigned long current_line;
7818   long line_offset;
7819   long line_delta;
7820   unsigned long current_file;
7821   unsigned long function;
7822
7823   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7824   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7825   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7826   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7827
7828   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7829     dw2_asm_output_data (4, 0xffffffff,
7830       "Initial length escape value indicating 64-bit DWARF extension");
7831   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7832                         "Length of Source Line Info");
7833   ASM_OUTPUT_LABEL (asm_out_file, l1);
7834
7835   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7836   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7837   ASM_OUTPUT_LABEL (asm_out_file, p1);
7838
7839   /* Define the architecture-dependent minimum instruction length (in
7840    bytes).  In this implementation of DWARF, this field is used for
7841    information purposes only.  Since GCC generates assembly language,
7842    we have no a priori knowledge of how many instruction bytes are
7843    generated for each source line, and therefore can use only the
7844    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7845    commands.  Accordingly, we fix this as `1', which is "correct
7846    enough" for all architectures, and don't let the target override.  */
7847   dw2_asm_output_data (1, 1,
7848                        "Minimum Instruction Length");
7849
7850   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7851                        "Default is_stmt_start flag");
7852   dw2_asm_output_data (1, DWARF_LINE_BASE,
7853                        "Line Base Value (Special Opcodes)");
7854   dw2_asm_output_data (1, DWARF_LINE_RANGE,
7855                        "Line Range Value (Special Opcodes)");
7856   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7857                        "Special Opcode Base");
7858
7859   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7860     {
7861       switch (opc)
7862         {
7863         case DW_LNS_advance_pc:
7864         case DW_LNS_advance_line:
7865         case DW_LNS_set_file:
7866         case DW_LNS_set_column:
7867         case DW_LNS_fixed_advance_pc:
7868           n_op_args = 1;
7869           break;
7870         default:
7871           n_op_args = 0;
7872           break;
7873         }
7874
7875       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
7876                            opc, n_op_args);
7877     }
7878
7879   /* Write out the information about the files we use.  */
7880   output_file_names ();
7881   ASM_OUTPUT_LABEL (asm_out_file, p2);
7882
7883   /* We used to set the address register to the first location in the text
7884      section here, but that didn't accomplish anything since we already
7885      have a line note for the opening brace of the first function.  */
7886
7887   /* Generate the line number to PC correspondence table, encoded as
7888      a series of state machine operations.  */
7889   current_file = 1;
7890   current_line = 1;
7891
7892   if (cfun && in_cold_section_p)
7893     strcpy (prev_line_label, cfun->cold_section_label);
7894   else
7895     strcpy (prev_line_label, text_section_label);
7896   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
7897     {
7898       dw_line_info_ref line_info = &line_info_table[lt_index];
7899
7900 #if 0
7901       /* Disable this optimization for now; GDB wants to see two line notes
7902          at the beginning of a function so it can find the end of the
7903          prologue.  */
7904
7905       /* Don't emit anything for redundant notes.  Just updating the
7906          address doesn't accomplish anything, because we already assume
7907          that anything after the last address is this line.  */
7908       if (line_info->dw_line_num == current_line
7909           && line_info->dw_file_num == current_file)
7910         continue;
7911 #endif
7912
7913       /* Emit debug info for the address of the current line.
7914
7915          Unfortunately, we have little choice here currently, and must always
7916          use the most general form.  GCC does not know the address delta
7917          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
7918          attributes which will give an upper bound on the address range.  We
7919          could perhaps use length attributes to determine when it is safe to
7920          use DW_LNS_fixed_advance_pc.  */
7921
7922       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
7923       if (0)
7924         {
7925           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
7926           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7927                                "DW_LNS_fixed_advance_pc");
7928           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
7929         }
7930       else
7931         {
7932           /* This can handle any delta.  This takes
7933              4+DWARF2_ADDR_SIZE bytes.  */
7934           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7935           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7936           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7937           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
7938         }
7939
7940       strcpy (prev_line_label, line_label);
7941
7942       /* Emit debug info for the source file of the current line, if
7943          different from the previous line.  */
7944       if (line_info->dw_file_num != current_file)
7945         {
7946           current_file = line_info->dw_file_num;
7947           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
7948           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
7949                                        VARRAY_CHAR_PTR (file_table,
7950                                                         current_file));
7951         }
7952
7953       /* Emit debug info for the current line number, choosing the encoding
7954          that uses the least amount of space.  */
7955       if (line_info->dw_line_num != current_line)
7956         {
7957           line_offset = line_info->dw_line_num - current_line;
7958           line_delta = line_offset - DWARF_LINE_BASE;
7959           current_line = line_info->dw_line_num;
7960           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
7961             /* This can handle deltas from -10 to 234, using the current
7962                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
7963                takes 1 byte.  */
7964             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
7965                                  "line %lu", current_line);
7966           else
7967             {
7968               /* This can handle any delta.  This takes at least 4 bytes,
7969                  depending on the value being encoded.  */
7970               dw2_asm_output_data (1, DW_LNS_advance_line,
7971                                    "advance to line %lu", current_line);
7972               dw2_asm_output_data_sleb128 (line_offset, NULL);
7973               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7974             }
7975         }
7976       else
7977         /* We still need to start a new row, so output a copy insn.  */
7978         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
7979     }
7980
7981   /* Emit debug info for the address of the end of the function.  */
7982   if (0)
7983     {
7984       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
7985                            "DW_LNS_fixed_advance_pc");
7986       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
7987     }
7988   else
7989     {
7990       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
7991       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
7992       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
7993       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
7994     }
7995
7996   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
7997   dw2_asm_output_data_uleb128 (1, NULL);
7998   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
7999
8000   function = 0;
8001   current_file = 1;
8002   current_line = 1;
8003   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8004     {
8005       dw_separate_line_info_ref line_info
8006         = &separate_line_info_table[lt_index];
8007
8008 #if 0
8009       /* Don't emit anything for redundant notes.  */
8010       if (line_info->dw_line_num == current_line
8011           && line_info->dw_file_num == current_file
8012           && line_info->function == function)
8013         goto cont;
8014 #endif
8015
8016       /* Emit debug info for the address of the current line.  If this is
8017          a new function, or the first line of a function, then we need
8018          to handle it differently.  */
8019       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8020                                    lt_index);
8021       if (function != line_info->function)
8022         {
8023           function = line_info->function;
8024
8025           /* Set the address register to the first line in the function.  */
8026           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8027           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8028           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8029           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8030         }
8031       else
8032         {
8033           /* ??? See the DW_LNS_advance_pc comment above.  */
8034           if (0)
8035             {
8036               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8037                                    "DW_LNS_fixed_advance_pc");
8038               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8039             }
8040           else
8041             {
8042               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8043               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8044               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8045               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8046             }
8047         }
8048
8049       strcpy (prev_line_label, line_label);
8050
8051       /* Emit debug info for the source file of the current line, if
8052          different from the previous line.  */
8053       if (line_info->dw_file_num != current_file)
8054         {
8055           current_file = line_info->dw_file_num;
8056           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8057           dw2_asm_output_data_uleb128 (current_file, "(\"%s\")",
8058                                        VARRAY_CHAR_PTR (file_table,
8059                                                         current_file));
8060         }
8061
8062       /* Emit debug info for the current line number, choosing the encoding
8063          that uses the least amount of space.  */
8064       if (line_info->dw_line_num != current_line)
8065         {
8066           line_offset = line_info->dw_line_num - current_line;
8067           line_delta = line_offset - DWARF_LINE_BASE;
8068           current_line = line_info->dw_line_num;
8069           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8070             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8071                                  "line %lu", current_line);
8072           else
8073             {
8074               dw2_asm_output_data (1, DW_LNS_advance_line,
8075                                    "advance to line %lu", current_line);
8076               dw2_asm_output_data_sleb128 (line_offset, NULL);
8077               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8078             }
8079         }
8080       else
8081         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8082
8083 #if 0
8084     cont:
8085 #endif
8086
8087       lt_index++;
8088
8089       /* If we're done with a function, end its sequence.  */
8090       if (lt_index == separate_line_info_table_in_use
8091           || separate_line_info_table[lt_index].function != function)
8092         {
8093           current_file = 1;
8094           current_line = 1;
8095
8096           /* Emit debug info for the address of the end of the function.  */
8097           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8098           if (0)
8099             {
8100               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8101                                    "DW_LNS_fixed_advance_pc");
8102               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8103             }
8104           else
8105             {
8106               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8107               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8108               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8109               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8110             }
8111
8112           /* Output the marker for the end of this sequence.  */
8113           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8114           dw2_asm_output_data_uleb128 (1, NULL);
8115           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8116         }
8117     }
8118
8119   /* Output the marker for the end of the line number info.  */
8120   ASM_OUTPUT_LABEL (asm_out_file, l2);
8121 }
8122 \f
8123 /* Given a pointer to a tree node for some base type, return a pointer to
8124    a DIE that describes the given type.
8125
8126    This routine must only be called for GCC type nodes that correspond to
8127    Dwarf base (fundamental) types.  */
8128
8129 static dw_die_ref
8130 base_type_die (tree type)
8131 {
8132   dw_die_ref base_type_result;
8133   enum dwarf_type encoding;
8134
8135   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8136     return 0;
8137
8138   switch (TREE_CODE (type))
8139     {
8140     case INTEGER_TYPE:
8141       if (TYPE_STRING_FLAG (type))
8142         {
8143           if (TYPE_UNSIGNED (type))
8144             encoding = DW_ATE_unsigned_char;
8145           else
8146             encoding = DW_ATE_signed_char;
8147         }
8148       else if (TYPE_UNSIGNED (type))
8149         encoding = DW_ATE_unsigned;
8150       else
8151         encoding = DW_ATE_signed;
8152       break;
8153
8154     case REAL_TYPE:
8155       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8156         encoding = DW_ATE_decimal_float;
8157       else
8158         encoding = DW_ATE_float;
8159       break;
8160
8161       /* Dwarf2 doesn't know anything about complex ints, so use
8162          a user defined type for it.  */
8163     case COMPLEX_TYPE:
8164       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8165         encoding = DW_ATE_complex_float;
8166       else
8167         encoding = DW_ATE_lo_user;
8168       break;
8169
8170     case BOOLEAN_TYPE:
8171       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8172       encoding = DW_ATE_boolean;
8173       break;
8174
8175     default:
8176       /* No other TREE_CODEs are Dwarf fundamental types.  */
8177       gcc_unreachable ();
8178     }
8179
8180   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8181
8182   /* This probably indicates a bug.  */
8183   if (! TYPE_NAME (type))
8184     add_name_attribute (base_type_result, "__unknown__");
8185
8186   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8187                    int_size_in_bytes (type));
8188   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8189
8190   return base_type_result;
8191 }
8192
8193 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
8194    the Dwarf "root" type for the given input type.  The Dwarf "root" type of
8195    a given type is generally the same as the given type, except that if the
8196    given type is a pointer or reference type, then the root type of the given
8197    type is the root type of the "basis" type for the pointer or reference
8198    type.  (This definition of the "root" type is recursive.) Also, the root
8199    type of a `const' qualified type or a `volatile' qualified type is the
8200    root type of the given type without the qualifiers.  */
8201
8202 static tree
8203 root_type (tree type)
8204 {
8205   if (TREE_CODE (type) == ERROR_MARK)
8206     return error_mark_node;
8207
8208   switch (TREE_CODE (type))
8209     {
8210     case ERROR_MARK:
8211       return error_mark_node;
8212
8213     case POINTER_TYPE:
8214     case REFERENCE_TYPE:
8215       return type_main_variant (root_type (TREE_TYPE (type)));
8216
8217     default:
8218       return type_main_variant (type);
8219     }
8220 }
8221
8222 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8223    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8224
8225 static inline int
8226 is_base_type (tree type)
8227 {
8228   switch (TREE_CODE (type))
8229     {
8230     case ERROR_MARK:
8231     case VOID_TYPE:
8232     case INTEGER_TYPE:
8233     case REAL_TYPE:
8234     case COMPLEX_TYPE:
8235     case BOOLEAN_TYPE:
8236       return 1;
8237
8238     case ARRAY_TYPE:
8239     case RECORD_TYPE:
8240     case UNION_TYPE:
8241     case QUAL_UNION_TYPE:
8242     case ENUMERAL_TYPE:
8243     case FUNCTION_TYPE:
8244     case METHOD_TYPE:
8245     case POINTER_TYPE:
8246     case REFERENCE_TYPE:
8247     case OFFSET_TYPE:
8248     case LANG_TYPE:
8249     case VECTOR_TYPE:
8250       return 0;
8251
8252     default:
8253       gcc_unreachable ();
8254     }
8255
8256   return 0;
8257 }
8258
8259 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8260    node, return the size in bits for the type if it is a constant, or else
8261    return the alignment for the type if the type's size is not constant, or
8262    else return BITS_PER_WORD if the type actually turns out to be an
8263    ERROR_MARK node.  */
8264
8265 static inline unsigned HOST_WIDE_INT
8266 simple_type_size_in_bits (tree type)
8267 {
8268   if (TREE_CODE (type) == ERROR_MARK)
8269     return BITS_PER_WORD;
8270   else if (TYPE_SIZE (type) == NULL_TREE)
8271     return 0;
8272   else if (host_integerp (TYPE_SIZE (type), 1))
8273     return tree_low_cst (TYPE_SIZE (type), 1);
8274   else
8275     return TYPE_ALIGN (type);
8276 }
8277
8278 /* Return true if the debug information for the given type should be
8279    emitted as a subrange type.  */
8280
8281 static inline bool
8282 is_subrange_type (tree type)
8283 {
8284   tree subtype = TREE_TYPE (type);
8285
8286   /* Subrange types are identified by the fact that they are integer
8287      types, and that they have a subtype which is either an integer type
8288      or an enumeral type.  */
8289
8290   if (TREE_CODE (type) != INTEGER_TYPE
8291       || subtype == NULL_TREE)
8292     return false;
8293
8294   if (TREE_CODE (subtype) != INTEGER_TYPE
8295       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8296     return false;
8297
8298   if (TREE_CODE (type) == TREE_CODE (subtype)
8299       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8300       && TYPE_MIN_VALUE (type) != NULL
8301       && TYPE_MIN_VALUE (subtype) != NULL
8302       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8303       && TYPE_MAX_VALUE (type) != NULL
8304       && TYPE_MAX_VALUE (subtype) != NULL
8305       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8306     {
8307       /* The type and its subtype have the same representation.  If in
8308          addition the two types also have the same name, then the given
8309          type is not a subrange type, but rather a plain base type.  */
8310       /* FIXME: brobecker/2004-03-22:
8311          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8312          therefore be sufficient to check the TYPE_SIZE node pointers
8313          rather than checking the actual size.  Unfortunately, we have
8314          found some cases, such as in the Ada "integer" type, where
8315          this is not the case.  Until this problem is solved, we need to
8316          keep checking the actual size.  */
8317       tree type_name = TYPE_NAME (type);
8318       tree subtype_name = TYPE_NAME (subtype);
8319
8320       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8321         type_name = DECL_NAME (type_name);
8322
8323       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8324         subtype_name = DECL_NAME (subtype_name);
8325
8326       if (type_name == subtype_name)
8327         return false;
8328     }
8329
8330   return true;
8331 }
8332
8333 /*  Given a pointer to a tree node for a subrange type, return a pointer
8334     to a DIE that describes the given type.  */
8335
8336 static dw_die_ref
8337 subrange_type_die (tree type, dw_die_ref context_die)
8338 {
8339   dw_die_ref subrange_die;
8340   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8341
8342   if (context_die == NULL)
8343     context_die = comp_unit_die;
8344
8345   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8346
8347   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8348     {
8349       /* The size of the subrange type and its base type do not match,
8350          so we need to generate a size attribute for the subrange type.  */
8351       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8352     }
8353
8354   if (TYPE_MIN_VALUE (type) != NULL)
8355     add_bound_info (subrange_die, DW_AT_lower_bound,
8356                     TYPE_MIN_VALUE (type));
8357   if (TYPE_MAX_VALUE (type) != NULL)
8358     add_bound_info (subrange_die, DW_AT_upper_bound,
8359                     TYPE_MAX_VALUE (type));
8360
8361   return subrange_die;
8362 }
8363
8364 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8365    entry that chains various modifiers in front of the given type.  */
8366
8367 static dw_die_ref
8368 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8369                    dw_die_ref context_die)
8370 {
8371   enum tree_code code = TREE_CODE (type);
8372   dw_die_ref mod_type_die;
8373   dw_die_ref sub_die = NULL;
8374   tree item_type = NULL;
8375   tree qualified_type;
8376   tree name;
8377
8378   if (code == ERROR_MARK)
8379     return NULL;
8380
8381   /* See if we already have the appropriately qualified variant of
8382      this type.  */
8383   qualified_type
8384     = get_qualified_type (type,
8385                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8386                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8387   
8388   /* If we do, then we can just use its DIE, if it exists.  */
8389   if (qualified_type)
8390     {
8391       mod_type_die = lookup_type_die (qualified_type);
8392       if (mod_type_die)
8393         return mod_type_die;
8394     }
8395   
8396   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8397   
8398   /* Handle C typedef types.  */
8399   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8400     {
8401       tree dtype = TREE_TYPE (name);
8402       
8403       if (qualified_type == dtype)
8404         {
8405           /* For a named type, use the typedef.  */
8406           gen_type_die (qualified_type, context_die);
8407           return lookup_type_die (qualified_type);
8408         }
8409       else if (DECL_ORIGINAL_TYPE (name)
8410                && (is_const_type < TYPE_READONLY (dtype)
8411                    || is_volatile_type < TYPE_VOLATILE (dtype)))
8412         /* cv-unqualified version of named type.  Just use the unnamed
8413            type to which it refers.  */
8414         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8415                                   is_const_type, is_volatile_type,
8416                                   context_die);
8417       /* Else cv-qualified version of named type; fall through.  */
8418     }
8419   
8420   if (is_const_type)
8421     {
8422       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8423       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8424     }
8425   else if (is_volatile_type)
8426     {
8427       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8428       sub_die = modified_type_die (type, 0, 0, context_die);
8429     }
8430   else if (code == POINTER_TYPE)
8431     {
8432       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8433       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8434                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8435       item_type = TREE_TYPE (type);
8436     }
8437   else if (code == REFERENCE_TYPE)
8438     {
8439       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8440       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8441                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8442       item_type = TREE_TYPE (type);
8443     }
8444   else if (is_subrange_type (type))
8445     {
8446       mod_type_die = subrange_type_die (type, context_die);
8447       item_type = TREE_TYPE (type);
8448     }
8449   else if (is_base_type (type))
8450     mod_type_die = base_type_die (type);
8451   else
8452     {
8453       gen_type_die (type, context_die);
8454       
8455       /* We have to get the type_main_variant here (and pass that to the
8456          `lookup_type_die' routine) because the ..._TYPE node we have
8457          might simply be a *copy* of some original type node (where the
8458          copy was created to help us keep track of typedef names) and
8459          that copy might have a different TYPE_UID from the original
8460          ..._TYPE node.  */
8461       if (TREE_CODE (type) != VECTOR_TYPE)
8462         return lookup_type_die (type_main_variant (type));
8463       else
8464         /* Vectors have the debugging information in the type,
8465            not the main variant.  */
8466         return lookup_type_die (type);
8467     }
8468   
8469   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8470      don't output a DW_TAG_typedef, since there isn't one in the
8471      user's program; just attach a DW_AT_name to the type.  */
8472   if (name
8473       && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8474     {
8475       if (TREE_CODE (name) == TYPE_DECL)
8476         /* Could just call add_name_and_src_coords_attributes here,
8477            but since this is a builtin type it doesn't have any
8478            useful source coordinates anyway.  */
8479         name = DECL_NAME (name);
8480       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8481     }
8482   
8483   if (qualified_type)
8484     equate_type_number_to_die (qualified_type, mod_type_die);
8485
8486   if (item_type)
8487     /* We must do this after the equate_type_number_to_die call, in case
8488        this is a recursive type.  This ensures that the modified_type_die
8489        recursion will terminate even if the type is recursive.  Recursive
8490        types are possible in Ada.  */
8491     sub_die = modified_type_die (item_type,
8492                                  TYPE_READONLY (item_type),
8493                                  TYPE_VOLATILE (item_type),
8494                                  context_die);
8495
8496   if (sub_die != NULL)
8497     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8498
8499   return mod_type_die;
8500 }
8501
8502 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8503    an enumerated type.  */
8504
8505 static inline int
8506 type_is_enum (tree type)
8507 {
8508   return TREE_CODE (type) == ENUMERAL_TYPE;
8509 }
8510
8511 /* Return the DBX register number described by a given RTL node.  */
8512
8513 static unsigned int
8514 dbx_reg_number (rtx rtl)
8515 {
8516   unsigned regno = REGNO (rtl);
8517
8518   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8519
8520 #ifdef LEAF_REG_REMAP
8521   regno = LEAF_REG_REMAP (regno);
8522 #endif
8523
8524   return DBX_REGISTER_NUMBER (regno);
8525 }
8526
8527 /* Optionally add a DW_OP_piece term to a location description expression.
8528    DW_OP_piece is only added if the location description expression already
8529    doesn't end with DW_OP_piece.  */
8530
8531 static void
8532 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8533 {
8534   dw_loc_descr_ref loc;
8535
8536   if (*list_head != NULL)
8537     {
8538       /* Find the end of the chain.  */
8539       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8540         ;
8541
8542       if (loc->dw_loc_opc != DW_OP_piece)
8543         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8544     }
8545 }
8546
8547 /* Return a location descriptor that designates a machine register or
8548    zero if there is none.  */
8549
8550 static dw_loc_descr_ref
8551 reg_loc_descriptor (rtx rtl)
8552 {
8553   rtx regs;
8554
8555   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8556     return 0;
8557
8558   regs = targetm.dwarf_register_span (rtl);
8559
8560   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8561     return multiple_reg_loc_descriptor (rtl, regs);
8562   else
8563     return one_reg_loc_descriptor (dbx_reg_number (rtl));
8564 }
8565
8566 /* Return a location descriptor that designates a machine register for
8567    a given hard register number.  */
8568
8569 static dw_loc_descr_ref
8570 one_reg_loc_descriptor (unsigned int regno)
8571 {
8572   if (regno <= 31)
8573     return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8574   else
8575     return new_loc_descr (DW_OP_regx, regno, 0);
8576 }
8577
8578 /* Given an RTL of a register, return a location descriptor that
8579    designates a value that spans more than one register.  */
8580
8581 static dw_loc_descr_ref
8582 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8583 {
8584   int nregs, size, i;
8585   unsigned reg;
8586   dw_loc_descr_ref loc_result = NULL;
8587
8588   reg = REGNO (rtl);
8589 #ifdef LEAF_REG_REMAP
8590   reg = LEAF_REG_REMAP (reg);
8591 #endif
8592   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8593   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8594
8595   /* Simple, contiguous registers.  */
8596   if (regs == NULL_RTX)
8597     {
8598       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8599
8600       loc_result = NULL;
8601       while (nregs--)
8602         {
8603           dw_loc_descr_ref t;
8604
8605           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8606           add_loc_descr (&loc_result, t);
8607           add_loc_descr_op_piece (&loc_result, size);
8608           ++reg;
8609         }
8610       return loc_result;
8611     }
8612
8613   /* Now onto stupid register sets in non contiguous locations.  */
8614
8615   gcc_assert (GET_CODE (regs) == PARALLEL);
8616
8617   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8618   loc_result = NULL;
8619
8620   for (i = 0; i < XVECLEN (regs, 0); ++i)
8621     {
8622       dw_loc_descr_ref t;
8623
8624       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8625       add_loc_descr (&loc_result, t);
8626       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8627       add_loc_descr_op_piece (&loc_result, size);
8628     }
8629   return loc_result;
8630 }
8631
8632 /* Return a location descriptor that designates a constant.  */
8633
8634 static dw_loc_descr_ref
8635 int_loc_descriptor (HOST_WIDE_INT i)
8636 {
8637   enum dwarf_location_atom op;
8638
8639   /* Pick the smallest representation of a constant, rather than just
8640      defaulting to the LEB encoding.  */
8641   if (i >= 0)
8642     {
8643       if (i <= 31)
8644         op = DW_OP_lit0 + i;
8645       else if (i <= 0xff)
8646         op = DW_OP_const1u;
8647       else if (i <= 0xffff)
8648         op = DW_OP_const2u;
8649       else if (HOST_BITS_PER_WIDE_INT == 32
8650                || i <= 0xffffffff)
8651         op = DW_OP_const4u;
8652       else
8653         op = DW_OP_constu;
8654     }
8655   else
8656     {
8657       if (i >= -0x80)
8658         op = DW_OP_const1s;
8659       else if (i >= -0x8000)
8660         op = DW_OP_const2s;
8661       else if (HOST_BITS_PER_WIDE_INT == 32
8662                || i >= -0x80000000)
8663         op = DW_OP_const4s;
8664       else
8665         op = DW_OP_consts;
8666     }
8667
8668   return new_loc_descr (op, i, 0);
8669 }
8670
8671 /* Return a location descriptor that designates a base+offset location.  */
8672
8673 static dw_loc_descr_ref
8674 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8675 {
8676   unsigned int regno;
8677
8678   /* We only use "frame base" when we're sure we're talking about the
8679      post-prologue local stack frame.  We do this by *not* running
8680      register elimination until this point, and recognizing the special
8681      argument pointer and soft frame pointer rtx's.  */
8682   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8683     {
8684       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8685
8686       if (elim != reg)
8687         {
8688           if (GET_CODE (elim) == PLUS)
8689             {
8690               offset += INTVAL (XEXP (elim, 1));
8691               elim = XEXP (elim, 0);
8692             }
8693           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8694                       : stack_pointer_rtx));
8695           offset += frame_pointer_fb_offset;
8696
8697           return new_loc_descr (DW_OP_fbreg, offset, 0);
8698         }
8699     }
8700
8701   regno = dbx_reg_number (reg);
8702   if (regno <= 31)
8703     return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8704   else
8705     return new_loc_descr (DW_OP_bregx, regno, offset);
8706 }
8707
8708 /* Return true if this RTL expression describes a base+offset calculation.  */
8709
8710 static inline int
8711 is_based_loc (rtx rtl)
8712 {
8713   return (GET_CODE (rtl) == PLUS
8714           && ((REG_P (XEXP (rtl, 0))
8715                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8716                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8717 }
8718
8719 /* The following routine converts the RTL for a variable or parameter
8720    (resident in memory) into an equivalent Dwarf representation of a
8721    mechanism for getting the address of that same variable onto the top of a
8722    hypothetical "address evaluation" stack.
8723
8724    When creating memory location descriptors, we are effectively transforming
8725    the RTL for a memory-resident object into its Dwarf postfix expression
8726    equivalent.  This routine recursively descends an RTL tree, turning
8727    it into Dwarf postfix code as it goes.
8728
8729    MODE is the mode of the memory reference, needed to handle some
8730    autoincrement addressing modes.
8731
8732    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8733    location list for RTL.
8734
8735    Return 0 if we can't represent the location.  */
8736
8737 static dw_loc_descr_ref
8738 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8739 {
8740   dw_loc_descr_ref mem_loc_result = NULL;
8741   enum dwarf_location_atom op;
8742
8743   /* Note that for a dynamically sized array, the location we will generate a
8744      description of here will be the lowest numbered location which is
8745      actually within the array.  That's *not* necessarily the same as the
8746      zeroth element of the array.  */
8747
8748   rtl = targetm.delegitimize_address (rtl);
8749
8750   switch (GET_CODE (rtl))
8751     {
8752     case POST_INC:
8753     case POST_DEC:
8754     case POST_MODIFY:
8755       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
8756          just fall into the SUBREG code.  */
8757
8758       /* ... fall through ...  */
8759
8760     case SUBREG:
8761       /* The case of a subreg may arise when we have a local (register)
8762          variable or a formal (register) parameter which doesn't quite fill
8763          up an entire register.  For now, just assume that it is
8764          legitimate to make the Dwarf info refer to the whole register which
8765          contains the given subreg.  */
8766       rtl = XEXP (rtl, 0);
8767
8768       /* ... fall through ...  */
8769
8770     case REG:
8771       /* Whenever a register number forms a part of the description of the
8772          method for calculating the (dynamic) address of a memory resident
8773          object, DWARF rules require the register number be referred to as
8774          a "base register".  This distinction is not based in any way upon
8775          what category of register the hardware believes the given register
8776          belongs to.  This is strictly DWARF terminology we're dealing with
8777          here. Note that in cases where the location of a memory-resident
8778          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8779          OP_CONST (0)) the actual DWARF location descriptor that we generate
8780          may just be OP_BASEREG (basereg).  This may look deceptively like
8781          the object in question was allocated to a register (rather than in
8782          memory) so DWARF consumers need to be aware of the subtle
8783          distinction between OP_REG and OP_BASEREG.  */
8784       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8785         mem_loc_result = based_loc_descr (rtl, 0);
8786       break;
8787
8788     case MEM:
8789       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8790       if (mem_loc_result != 0)
8791         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8792       break;
8793
8794     case LO_SUM:
8795          rtl = XEXP (rtl, 1);
8796
8797       /* ... fall through ...  */
8798
8799     case LABEL_REF:
8800       /* Some ports can transform a symbol ref into a label ref, because
8801          the symbol ref is too far away and has to be dumped into a constant
8802          pool.  */
8803     case CONST:
8804     case SYMBOL_REF:
8805       /* Alternatively, the symbol in the constant pool might be referenced
8806          by a different symbol.  */
8807       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8808         {
8809           bool marked;
8810           rtx tmp = get_pool_constant_mark (rtl, &marked);
8811
8812           if (GET_CODE (tmp) == SYMBOL_REF)
8813             {
8814               rtl = tmp;
8815               if (CONSTANT_POOL_ADDRESS_P (tmp))
8816                 get_pool_constant_mark (tmp, &marked);
8817               else
8818                 marked = true;
8819             }
8820
8821           /* If all references to this pool constant were optimized away,
8822              it was not output and thus we can't represent it.
8823              FIXME: might try to use DW_OP_const_value here, though
8824              DW_OP_piece complicates it.  */
8825           if (!marked)
8826             return 0;
8827         }
8828
8829       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8830       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8831       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8832       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8833       break;
8834
8835     case PRE_MODIFY:
8836       /* Extract the PLUS expression nested inside and fall into
8837          PLUS code below.  */
8838       rtl = XEXP (rtl, 1);
8839       goto plus;
8840
8841     case PRE_INC:
8842     case PRE_DEC:
8843       /* Turn these into a PLUS expression and fall into the PLUS code
8844          below.  */
8845       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8846                           GEN_INT (GET_CODE (rtl) == PRE_INC
8847                                    ? GET_MODE_UNIT_SIZE (mode)
8848                                    : -GET_MODE_UNIT_SIZE (mode)));
8849
8850       /* ... fall through ...  */
8851
8852     case PLUS:
8853     plus:
8854       if (is_based_loc (rtl))
8855         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8856                                           INTVAL (XEXP (rtl, 1)));
8857       else
8858         {
8859           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8860           if (mem_loc_result == 0)
8861             break;
8862
8863           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8864               && INTVAL (XEXP (rtl, 1)) >= 0)
8865             add_loc_descr (&mem_loc_result,
8866                            new_loc_descr (DW_OP_plus_uconst,
8867                                           INTVAL (XEXP (rtl, 1)), 0));
8868           else
8869             {
8870               add_loc_descr (&mem_loc_result,
8871                              mem_loc_descriptor (XEXP (rtl, 1), mode));
8872               add_loc_descr (&mem_loc_result,
8873                              new_loc_descr (DW_OP_plus, 0, 0));
8874             }
8875         }
8876       break;
8877
8878     /* If a pseudo-reg is optimized away, it is possible for it to
8879        be replaced with a MEM containing a multiply or shift.  */
8880     case MULT:
8881       op = DW_OP_mul;
8882       goto do_binop;
8883
8884     case ASHIFT:
8885       op = DW_OP_shl;
8886       goto do_binop;
8887
8888     case ASHIFTRT:
8889       op = DW_OP_shra;
8890       goto do_binop;
8891
8892     case LSHIFTRT:
8893       op = DW_OP_shr;
8894       goto do_binop;
8895
8896     do_binop:
8897       {
8898         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
8899         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
8900
8901         if (op0 == 0 || op1 == 0)
8902           break;
8903
8904         mem_loc_result = op0;
8905         add_loc_descr (&mem_loc_result, op1);
8906         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
8907         break;
8908       }
8909
8910     case CONST_INT:
8911       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
8912       break;
8913
8914     default:
8915       gcc_unreachable ();
8916     }
8917
8918   return mem_loc_result;
8919 }
8920
8921 /* Return a descriptor that describes the concatenation of two locations.
8922    This is typically a complex variable.  */
8923
8924 static dw_loc_descr_ref
8925 concat_loc_descriptor (rtx x0, rtx x1)
8926 {
8927   dw_loc_descr_ref cc_loc_result = NULL;
8928   dw_loc_descr_ref x0_ref = loc_descriptor (x0);
8929   dw_loc_descr_ref x1_ref = loc_descriptor (x1);
8930
8931   if (x0_ref == 0 || x1_ref == 0)
8932     return 0;
8933
8934   cc_loc_result = x0_ref;
8935   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
8936
8937   add_loc_descr (&cc_loc_result, x1_ref);
8938   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
8939
8940   return cc_loc_result;
8941 }
8942
8943 /* Output a proper Dwarf location descriptor for a variable or parameter
8944    which is either allocated in a register or in a memory location.  For a
8945    register, we just generate an OP_REG and the register number.  For a
8946    memory location we provide a Dwarf postfix expression describing how to
8947    generate the (dynamic) address of the object onto the address stack.
8948
8949    If we don't know how to describe it, return 0.  */
8950
8951 static dw_loc_descr_ref
8952 loc_descriptor (rtx rtl)
8953 {
8954   dw_loc_descr_ref loc_result = NULL;
8955
8956   switch (GET_CODE (rtl))
8957     {
8958     case SUBREG:
8959       /* The case of a subreg may arise when we have a local (register)
8960          variable or a formal (register) parameter which doesn't quite fill
8961          up an entire register.  For now, just assume that it is
8962          legitimate to make the Dwarf info refer to the whole register which
8963          contains the given subreg.  */
8964       rtl = SUBREG_REG (rtl);
8965
8966       /* ... fall through ...  */
8967
8968     case REG:
8969       loc_result = reg_loc_descriptor (rtl);
8970       break;
8971
8972     case MEM:
8973       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8974       break;
8975
8976     case CONCAT:
8977       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
8978       break;
8979
8980     case VAR_LOCATION:
8981       /* Single part.  */
8982       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
8983         {
8984           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
8985           break;
8986         }
8987
8988       rtl = XEXP (rtl, 1);
8989       /* FALLTHRU */
8990
8991     case PARALLEL:
8992       {
8993         rtvec par_elems = XVEC (rtl, 0);
8994         int num_elem = GET_NUM_ELEM (par_elems);
8995         enum machine_mode mode;
8996         int i;
8997
8998         /* Create the first one, so we have something to add to.  */
8999         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9000         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9001         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9002         for (i = 1; i < num_elem; i++)
9003           {
9004             dw_loc_descr_ref temp;
9005
9006             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9007             add_loc_descr (&loc_result, temp);
9008             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9009             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9010           }
9011       }
9012       break;
9013
9014     default:
9015       gcc_unreachable ();
9016     }
9017
9018   return loc_result;
9019 }
9020
9021 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9022    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9023    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9024    top-level invocation, and we require the address of LOC; is 0 if we require
9025    the value of LOC.  */
9026
9027 static dw_loc_descr_ref
9028 loc_descriptor_from_tree_1 (tree loc, int want_address)
9029 {
9030   dw_loc_descr_ref ret, ret1;
9031   int have_address = 0;
9032   enum dwarf_location_atom op;
9033
9034   /* ??? Most of the time we do not take proper care for sign/zero
9035      extending the values properly.  Hopefully this won't be a real
9036      problem...  */
9037
9038   switch (TREE_CODE (loc))
9039     {
9040     case ERROR_MARK:
9041       return 0;
9042
9043     case PLACEHOLDER_EXPR:
9044       /* This case involves extracting fields from an object to determine the
9045          position of other fields.  We don't try to encode this here.  The
9046          only user of this is Ada, which encodes the needed information using
9047          the names of types.  */
9048       return 0;
9049
9050     case CALL_EXPR:
9051       return 0;
9052
9053     case PREINCREMENT_EXPR:
9054     case PREDECREMENT_EXPR:
9055     case POSTINCREMENT_EXPR:
9056     case POSTDECREMENT_EXPR:
9057       /* There are no opcodes for these operations.  */
9058       return 0;
9059
9060     case ADDR_EXPR:
9061       /* If we already want an address, there's nothing we can do.  */
9062       if (want_address)
9063         return 0;
9064
9065       /* Otherwise, process the argument and look for the address.  */
9066       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9067
9068     case VAR_DECL:
9069       if (DECL_THREAD_LOCAL_P (loc))
9070         {
9071           rtx rtl;
9072
9073           /* If this is not defined, we have no way to emit the data.  */
9074           if (!targetm.asm_out.output_dwarf_dtprel)
9075             return 0;
9076
9077           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9078              look up addresses of objects in the current module.  */
9079           if (DECL_EXTERNAL (loc))
9080             return 0;
9081
9082           rtl = rtl_for_decl_location (loc);
9083           if (rtl == NULL_RTX)
9084             return 0;
9085
9086           if (!MEM_P (rtl))
9087             return 0;
9088           rtl = XEXP (rtl, 0);
9089           if (! CONSTANT_P (rtl))
9090             return 0;
9091
9092           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9093           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9094           ret->dw_loc_oprnd1.v.val_addr = rtl;
9095
9096           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9097           add_loc_descr (&ret, ret1);
9098
9099           have_address = 1;
9100           break;
9101         }
9102       /* FALLTHRU */
9103
9104     case PARM_DECL:
9105       if (DECL_HAS_VALUE_EXPR_P (loc))
9106         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9107                                            want_address);
9108       /* FALLTHRU */
9109
9110     case RESULT_DECL:
9111       {
9112         rtx rtl = rtl_for_decl_location (loc);
9113
9114         if (rtl == NULL_RTX)
9115           return 0;
9116         else if (GET_CODE (rtl) == CONST_INT)
9117           {
9118             HOST_WIDE_INT val = INTVAL (rtl);
9119             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9120               val &= GET_MODE_MASK (DECL_MODE (loc));
9121             ret = int_loc_descriptor (val);
9122           }
9123         else if (GET_CODE (rtl) == CONST_STRING)
9124           return 0;
9125         else if (CONSTANT_P (rtl))
9126           {
9127             ret = new_loc_descr (DW_OP_addr, 0, 0);
9128             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9129             ret->dw_loc_oprnd1.v.val_addr = rtl;
9130           }
9131         else
9132           {
9133             enum machine_mode mode;
9134
9135             /* Certain constructs can only be represented at top-level.  */
9136             if (want_address == 2)
9137               return loc_descriptor (rtl);
9138
9139             mode = GET_MODE (rtl);
9140             if (MEM_P (rtl))
9141               {
9142                 rtl = XEXP (rtl, 0);
9143                 have_address = 1;
9144               }
9145             ret = mem_loc_descriptor (rtl, mode);
9146           }
9147       }
9148       break;
9149
9150     case INDIRECT_REF:
9151       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9152       have_address = 1;
9153       break;
9154
9155     case COMPOUND_EXPR:
9156       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9157
9158     case NOP_EXPR:
9159     case CONVERT_EXPR:
9160     case NON_LVALUE_EXPR:
9161     case VIEW_CONVERT_EXPR:
9162     case SAVE_EXPR:
9163     case MODIFY_EXPR:
9164       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9165
9166     case COMPONENT_REF:
9167     case BIT_FIELD_REF:
9168     case ARRAY_REF:
9169     case ARRAY_RANGE_REF:
9170       {
9171         tree obj, offset;
9172         HOST_WIDE_INT bitsize, bitpos, bytepos;
9173         enum machine_mode mode;
9174         int volatilep;
9175         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9176
9177         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9178                                    &unsignedp, &volatilep, false);
9179
9180         if (obj == loc)
9181           return 0;
9182
9183         ret = loc_descriptor_from_tree_1 (obj, 1);
9184         if (ret == 0
9185             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9186           return 0;
9187
9188         if (offset != NULL_TREE)
9189           {
9190             /* Variable offset.  */
9191             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9192             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9193           }
9194
9195         bytepos = bitpos / BITS_PER_UNIT;
9196         if (bytepos > 0)
9197           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9198         else if (bytepos < 0)
9199           {
9200             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9201             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9202           }
9203
9204         have_address = 1;
9205         break;
9206       }
9207
9208     case INTEGER_CST:
9209       if (host_integerp (loc, 0))
9210         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9211       else
9212         return 0;
9213       break;
9214
9215     case CONSTRUCTOR:
9216       {
9217         /* Get an RTL for this, if something has been emitted.  */
9218         rtx rtl = lookup_constant_def (loc);
9219         enum machine_mode mode;
9220
9221         if (!rtl || !MEM_P (rtl))
9222           return 0;
9223         mode = GET_MODE (rtl);
9224         rtl = XEXP (rtl, 0);
9225         ret = mem_loc_descriptor (rtl, mode);
9226         have_address = 1;
9227         break;
9228       }
9229
9230     case TRUTH_AND_EXPR:
9231     case TRUTH_ANDIF_EXPR:
9232     case BIT_AND_EXPR:
9233       op = DW_OP_and;
9234       goto do_binop;
9235
9236     case TRUTH_XOR_EXPR:
9237     case BIT_XOR_EXPR:
9238       op = DW_OP_xor;
9239       goto do_binop;
9240
9241     case TRUTH_OR_EXPR:
9242     case TRUTH_ORIF_EXPR:
9243     case BIT_IOR_EXPR:
9244       op = DW_OP_or;
9245       goto do_binop;
9246
9247     case FLOOR_DIV_EXPR:
9248     case CEIL_DIV_EXPR:
9249     case ROUND_DIV_EXPR:
9250     case TRUNC_DIV_EXPR:
9251       op = DW_OP_div;
9252       goto do_binop;
9253
9254     case MINUS_EXPR:
9255       op = DW_OP_minus;
9256       goto do_binop;
9257
9258     case FLOOR_MOD_EXPR:
9259     case CEIL_MOD_EXPR:
9260     case ROUND_MOD_EXPR:
9261     case TRUNC_MOD_EXPR:
9262       op = DW_OP_mod;
9263       goto do_binop;
9264
9265     case MULT_EXPR:
9266       op = DW_OP_mul;
9267       goto do_binop;
9268
9269     case LSHIFT_EXPR:
9270       op = DW_OP_shl;
9271       goto do_binop;
9272
9273     case RSHIFT_EXPR:
9274       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9275       goto do_binop;
9276
9277     case PLUS_EXPR:
9278       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9279           && host_integerp (TREE_OPERAND (loc, 1), 0))
9280         {
9281           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9282           if (ret == 0)
9283             return 0;
9284
9285           add_loc_descr (&ret,
9286                          new_loc_descr (DW_OP_plus_uconst,
9287                                         tree_low_cst (TREE_OPERAND (loc, 1),
9288                                                       0),
9289                                         0));
9290           break;
9291         }
9292
9293       op = DW_OP_plus;
9294       goto do_binop;
9295
9296     case LE_EXPR:
9297       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9298         return 0;
9299
9300       op = DW_OP_le;
9301       goto do_binop;
9302
9303     case GE_EXPR:
9304       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9305         return 0;
9306
9307       op = DW_OP_ge;
9308       goto do_binop;
9309
9310     case LT_EXPR:
9311       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9312         return 0;
9313
9314       op = DW_OP_lt;
9315       goto do_binop;
9316
9317     case GT_EXPR:
9318       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9319         return 0;
9320
9321       op = DW_OP_gt;
9322       goto do_binop;
9323
9324     case EQ_EXPR:
9325       op = DW_OP_eq;
9326       goto do_binop;
9327
9328     case NE_EXPR:
9329       op = DW_OP_ne;
9330       goto do_binop;
9331
9332     do_binop:
9333       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9334       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9335       if (ret == 0 || ret1 == 0)
9336         return 0;
9337
9338       add_loc_descr (&ret, ret1);
9339       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9340       break;
9341
9342     case TRUTH_NOT_EXPR:
9343     case BIT_NOT_EXPR:
9344       op = DW_OP_not;
9345       goto do_unop;
9346
9347     case ABS_EXPR:
9348       op = DW_OP_abs;
9349       goto do_unop;
9350
9351     case NEGATE_EXPR:
9352       op = DW_OP_neg;
9353       goto do_unop;
9354
9355     do_unop:
9356       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9357       if (ret == 0)
9358         return 0;
9359
9360       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9361       break;
9362
9363     case MIN_EXPR:
9364     case MAX_EXPR:
9365       {
9366         const enum tree_code code =
9367           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9368
9369         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9370                       build2 (code, integer_type_node,
9371                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9372                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9373       }
9374
9375       /* ... fall through ...  */
9376
9377     case COND_EXPR:
9378       {
9379         dw_loc_descr_ref lhs
9380           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9381         dw_loc_descr_ref rhs
9382           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9383         dw_loc_descr_ref bra_node, jump_node, tmp;
9384
9385         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9386         if (ret == 0 || lhs == 0 || rhs == 0)
9387           return 0;
9388
9389         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9390         add_loc_descr (&ret, bra_node);
9391
9392         add_loc_descr (&ret, rhs);
9393         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9394         add_loc_descr (&ret, jump_node);
9395
9396         add_loc_descr (&ret, lhs);
9397         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9398         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9399
9400         /* ??? Need a node to point the skip at.  Use a nop.  */
9401         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9402         add_loc_descr (&ret, tmp);
9403         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9404         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9405       }
9406       break;
9407
9408     case FIX_TRUNC_EXPR:
9409     case FIX_CEIL_EXPR:
9410     case FIX_FLOOR_EXPR:
9411     case FIX_ROUND_EXPR:
9412       return 0;
9413
9414     default:
9415       /* Leave front-end specific codes as simply unknown.  This comes
9416          up, for instance, with the C STMT_EXPR.  */
9417       if ((unsigned int) TREE_CODE (loc)
9418           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9419         return 0;
9420
9421 #ifdef ENABLE_CHECKING
9422       /* Otherwise this is a generic code; we should just lists all of
9423          these explicitly.  We forgot one.  */
9424       gcc_unreachable ();
9425 #else
9426       /* In a release build, we want to degrade gracefully: better to
9427          generate incomplete debugging information than to crash.  */
9428       return NULL;
9429 #endif
9430     }
9431
9432   /* Show if we can't fill the request for an address.  */
9433   if (want_address && !have_address)
9434     return 0;
9435
9436   /* If we've got an address and don't want one, dereference.  */
9437   if (!want_address && have_address && ret)
9438     {
9439       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9440
9441       if (size > DWARF2_ADDR_SIZE || size == -1)
9442         return 0;
9443       else if (size == DWARF2_ADDR_SIZE)
9444         op = DW_OP_deref;
9445       else
9446         op = DW_OP_deref_size;
9447
9448       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9449     }
9450
9451   return ret;
9452 }
9453
9454 static inline dw_loc_descr_ref
9455 loc_descriptor_from_tree (tree loc)
9456 {
9457   return loc_descriptor_from_tree_1 (loc, 2);
9458 }
9459
9460 /* Given a value, round it up to the lowest multiple of `boundary'
9461    which is not less than the value itself.  */
9462
9463 static inline HOST_WIDE_INT
9464 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9465 {
9466   return (((value + boundary - 1) / boundary) * boundary);
9467 }
9468
9469 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9470    pointer to the declared type for the relevant field variable, or return
9471    `integer_type_node' if the given node turns out to be an
9472    ERROR_MARK node.  */
9473
9474 static inline tree
9475 field_type (tree decl)
9476 {
9477   tree type;
9478
9479   if (TREE_CODE (decl) == ERROR_MARK)
9480     return integer_type_node;
9481
9482   type = DECL_BIT_FIELD_TYPE (decl);
9483   if (type == NULL_TREE)
9484     type = TREE_TYPE (decl);
9485
9486   return type;
9487 }
9488
9489 /* Given a pointer to a tree node, return the alignment in bits for
9490    it, or else return BITS_PER_WORD if the node actually turns out to
9491    be an ERROR_MARK node.  */
9492
9493 static inline unsigned
9494 simple_type_align_in_bits (tree type)
9495 {
9496   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9497 }
9498
9499 static inline unsigned
9500 simple_decl_align_in_bits (tree decl)
9501 {
9502   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9503 }
9504
9505 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9506    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9507    or return 0 if we are unable to determine what that offset is, either
9508    because the argument turns out to be a pointer to an ERROR_MARK node, or
9509    because the offset is actually variable.  (We can't handle the latter case
9510    just yet).  */
9511
9512 static HOST_WIDE_INT
9513 field_byte_offset (tree decl)
9514 {
9515   unsigned int type_align_in_bits;
9516   unsigned int decl_align_in_bits;
9517   unsigned HOST_WIDE_INT type_size_in_bits;
9518   HOST_WIDE_INT object_offset_in_bits;
9519   tree type;
9520   tree field_size_tree;
9521   HOST_WIDE_INT bitpos_int;
9522   HOST_WIDE_INT deepest_bitpos;
9523   unsigned HOST_WIDE_INT field_size_in_bits;
9524
9525   if (TREE_CODE (decl) == ERROR_MARK)
9526     return 0;
9527
9528   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9529
9530   type = field_type (decl);
9531   field_size_tree = DECL_SIZE (decl);
9532
9533   /* The size could be unspecified if there was an error, or for
9534      a flexible array member.  */
9535   if (! field_size_tree)
9536     field_size_tree = bitsize_zero_node;
9537
9538   /* We cannot yet cope with fields whose positions are variable, so
9539      for now, when we see such things, we simply return 0.  Someday, we may
9540      be able to handle such cases, but it will be damn difficult.  */
9541   if (! host_integerp (bit_position (decl), 0))
9542     return 0;
9543
9544   bitpos_int = int_bit_position (decl);
9545
9546   /* If we don't know the size of the field, pretend it's a full word.  */
9547   if (host_integerp (field_size_tree, 1))
9548     field_size_in_bits = tree_low_cst (field_size_tree, 1);
9549   else
9550     field_size_in_bits = BITS_PER_WORD;
9551
9552   type_size_in_bits = simple_type_size_in_bits (type);
9553   type_align_in_bits = simple_type_align_in_bits (type);
9554   decl_align_in_bits = simple_decl_align_in_bits (decl);
9555
9556   /* The GCC front-end doesn't make any attempt to keep track of the starting
9557      bit offset (relative to the start of the containing structure type) of the
9558      hypothetical "containing object" for a bit-field.  Thus, when computing
9559      the byte offset value for the start of the "containing object" of a
9560      bit-field, we must deduce this information on our own. This can be rather
9561      tricky to do in some cases.  For example, handling the following structure
9562      type definition when compiling for an i386/i486 target (which only aligns
9563      long long's to 32-bit boundaries) can be very tricky:
9564
9565          struct S { int field1; long long field2:31; };
9566
9567      Fortunately, there is a simple rule-of-thumb which can be used in such
9568      cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for the
9569      structure shown above.  It decides to do this based upon one simple rule
9570      for bit-field allocation.  GCC allocates each "containing object" for each
9571      bit-field at the first (i.e. lowest addressed) legitimate alignment
9572      boundary (based upon the required minimum alignment for the declared type
9573      of the field) which it can possibly use, subject to the condition that
9574      there is still enough available space remaining in the containing object
9575      (when allocated at the selected point) to fully accommodate all of the
9576      bits of the bit-field itself.
9577
9578      This simple rule makes it obvious why GCC allocates 8 bytes for each
9579      object of the structure type shown above.  When looking for a place to
9580      allocate the "containing object" for `field2', the compiler simply tries
9581      to allocate a 64-bit "containing object" at each successive 32-bit
9582      boundary (starting at zero) until it finds a place to allocate that 64-
9583      bit field such that at least 31 contiguous (and previously unallocated)
9584      bits remain within that selected 64 bit field.  (As it turns out, for the
9585      example above, the compiler finds it is OK to allocate the "containing
9586      object" 64-bit field at bit-offset zero within the structure type.)
9587
9588      Here we attempt to work backwards from the limited set of facts we're
9589      given, and we try to deduce from those facts, where GCC must have believed
9590      that the containing object started (within the structure type). The value
9591      we deduce is then used (by the callers of this routine) to generate
9592      DW_AT_location and DW_AT_bit_offset attributes for fields (both bit-fields
9593      and, in the case of DW_AT_location, regular fields as well).  */
9594
9595   /* Figure out the bit-distance from the start of the structure to the
9596      "deepest" bit of the bit-field.  */
9597   deepest_bitpos = bitpos_int + field_size_in_bits;
9598
9599   /* This is the tricky part.  Use some fancy footwork to deduce where the
9600      lowest addressed bit of the containing object must be.  */
9601   object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9602
9603   /* Round up to type_align by default.  This works best for bitfields.  */
9604   object_offset_in_bits += type_align_in_bits - 1;
9605   object_offset_in_bits /= type_align_in_bits;
9606   object_offset_in_bits *= type_align_in_bits;
9607
9608   if (object_offset_in_bits > bitpos_int)
9609     {
9610       /* Sigh, the decl must be packed.  */
9611       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9612
9613       /* Round up to decl_align instead.  */
9614       object_offset_in_bits += decl_align_in_bits - 1;
9615       object_offset_in_bits /= decl_align_in_bits;
9616       object_offset_in_bits *= decl_align_in_bits;
9617     }
9618
9619   return object_offset_in_bits / BITS_PER_UNIT;
9620 }
9621 \f
9622 /* The following routines define various Dwarf attributes and any data
9623    associated with them.  */
9624
9625 /* Add a location description attribute value to a DIE.
9626
9627    This emits location attributes suitable for whole variables and
9628    whole parameters.  Note that the location attributes for struct fields are
9629    generated by the routine `data_member_location_attribute' below.  */
9630
9631 static inline void
9632 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9633                              dw_loc_descr_ref descr)
9634 {
9635   if (descr != 0)
9636     add_AT_loc (die, attr_kind, descr);
9637 }
9638
9639 /* Attach the specialized form of location attribute used for data members of
9640    struct and union types.  In the special case of a FIELD_DECL node which
9641    represents a bit-field, the "offset" part of this special location
9642    descriptor must indicate the distance in bytes from the lowest-addressed
9643    byte of the containing struct or union type to the lowest-addressed byte of
9644    the "containing object" for the bit-field.  (See the `field_byte_offset'
9645    function above).
9646
9647    For any given bit-field, the "containing object" is a hypothetical object
9648    (of some integral or enum type) within which the given bit-field lives.  The
9649    type of this hypothetical "containing object" is always the same as the
9650    declared type of the individual bit-field itself (for GCC anyway... the
9651    DWARF spec doesn't actually mandate this).  Note that it is the size (in
9652    bytes) of the hypothetical "containing object" which will be given in the
9653    DW_AT_byte_size attribute for this bit-field.  (See the
9654    `byte_size_attribute' function below.)  It is also used when calculating the
9655    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
9656    function below.)  */
9657
9658 static void
9659 add_data_member_location_attribute (dw_die_ref die, tree decl)
9660 {
9661   HOST_WIDE_INT offset;
9662   dw_loc_descr_ref loc_descr = 0;
9663
9664   if (TREE_CODE (decl) == TREE_BINFO)
9665     {
9666       /* We're working on the TAG_inheritance for a base class.  */
9667       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9668         {
9669           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9670              aren't at a fixed offset from all (sub)objects of the same
9671              type.  We need to extract the appropriate offset from our
9672              vtable.  The following dwarf expression means
9673
9674                BaseAddr = ObAddr + *((*ObAddr) - Offset)
9675
9676              This is specific to the V3 ABI, of course.  */
9677
9678           dw_loc_descr_ref tmp;
9679
9680           /* Make a copy of the object address.  */
9681           tmp = new_loc_descr (DW_OP_dup, 0, 0);
9682           add_loc_descr (&loc_descr, tmp);
9683
9684           /* Extract the vtable address.  */
9685           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9686           add_loc_descr (&loc_descr, tmp);
9687
9688           /* Calculate the address of the offset.  */
9689           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9690           gcc_assert (offset < 0);
9691
9692           tmp = int_loc_descriptor (-offset);
9693           add_loc_descr (&loc_descr, tmp);
9694           tmp = new_loc_descr (DW_OP_minus, 0, 0);
9695           add_loc_descr (&loc_descr, tmp);
9696
9697           /* Extract the offset.  */
9698           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9699           add_loc_descr (&loc_descr, tmp);
9700
9701           /* Add it to the object address.  */
9702           tmp = new_loc_descr (DW_OP_plus, 0, 0);
9703           add_loc_descr (&loc_descr, tmp);
9704         }
9705       else
9706         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9707     }
9708   else
9709     offset = field_byte_offset (decl);
9710
9711   if (! loc_descr)
9712     {
9713       enum dwarf_location_atom op;
9714
9715       /* The DWARF2 standard says that we should assume that the structure
9716          address is already on the stack, so we can specify a structure field
9717          address by using DW_OP_plus_uconst.  */
9718
9719 #ifdef MIPS_DEBUGGING_INFO
9720       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9721          operator correctly.  It works only if we leave the offset on the
9722          stack.  */
9723       op = DW_OP_constu;
9724 #else
9725       op = DW_OP_plus_uconst;
9726 #endif
9727
9728       loc_descr = new_loc_descr (op, offset, 0);
9729     }
9730
9731   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9732 }
9733
9734 /* Writes integer values to dw_vec_const array.  */
9735
9736 static void
9737 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9738 {
9739   while (size != 0)
9740     {
9741       *dest++ = val & 0xff;
9742       val >>= 8;
9743       --size;
9744     }
9745 }
9746
9747 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9748
9749 static HOST_WIDE_INT
9750 extract_int (const unsigned char *src, unsigned int size)
9751 {
9752   HOST_WIDE_INT val = 0;
9753
9754   src += size;
9755   while (size != 0)
9756     {
9757       val <<= 8;
9758       val |= *--src & 0xff;
9759       --size;
9760     }
9761   return val;
9762 }
9763
9764 /* Writes floating point values to dw_vec_const array.  */
9765
9766 static void
9767 insert_float (rtx rtl, unsigned char *array)
9768 {
9769   REAL_VALUE_TYPE rv;
9770   long val[4];
9771   int i;
9772
9773   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9774   real_to_target (val, &rv, GET_MODE (rtl));
9775
9776   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9777   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9778     {
9779       insert_int (val[i], 4, array);
9780       array += 4;
9781     }
9782 }
9783
9784 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9785    does not have a "location" either in memory or in a register.  These
9786    things can arise in GNU C when a constant is passed as an actual parameter
9787    to an inlined function.  They can also arise in C++ where declared
9788    constants do not necessarily get memory "homes".  */
9789
9790 static void
9791 add_const_value_attribute (dw_die_ref die, rtx rtl)
9792 {
9793   switch (GET_CODE (rtl))
9794     {
9795     case CONST_INT:
9796       {
9797         HOST_WIDE_INT val = INTVAL (rtl);
9798
9799         if (val < 0)
9800           add_AT_int (die, DW_AT_const_value, val);
9801         else
9802           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9803       }
9804       break;
9805
9806     case CONST_DOUBLE:
9807       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
9808          floating-point constant.  A CONST_DOUBLE is used whenever the
9809          constant requires more than one word in order to be adequately
9810          represented.  We output CONST_DOUBLEs as blocks.  */
9811       {
9812         enum machine_mode mode = GET_MODE (rtl);
9813
9814         if (SCALAR_FLOAT_MODE_P (mode))
9815           {
9816             unsigned int length = GET_MODE_SIZE (mode);
9817             unsigned char *array = ggc_alloc (length);
9818
9819             insert_float (rtl, array);
9820             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
9821           }
9822         else
9823           {
9824             /* ??? We really should be using HOST_WIDE_INT throughout.  */
9825             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
9826
9827             add_AT_long_long (die, DW_AT_const_value,
9828                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
9829           }
9830       }
9831       break;
9832
9833     case CONST_VECTOR:
9834       {
9835         enum machine_mode mode = GET_MODE (rtl);
9836         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
9837         unsigned int length = CONST_VECTOR_NUNITS (rtl);
9838         unsigned char *array = ggc_alloc (length * elt_size);
9839         unsigned int i;
9840         unsigned char *p;
9841
9842         switch (GET_MODE_CLASS (mode))
9843           {
9844           case MODE_VECTOR_INT:
9845             for (i = 0, p = array; i < length; i++, p += elt_size)
9846               {
9847                 rtx elt = CONST_VECTOR_ELT (rtl, i);
9848                 HOST_WIDE_INT lo, hi;
9849
9850                 switch (GET_CODE (elt))
9851                   {
9852                   case CONST_INT:
9853                     lo = INTVAL (elt);
9854                     hi = -(lo < 0);
9855                     break;
9856
9857                   case CONST_DOUBLE:
9858                     lo = CONST_DOUBLE_LOW (elt);
9859                     hi = CONST_DOUBLE_HIGH (elt);
9860                     break;
9861
9862                   default:
9863                     gcc_unreachable ();
9864                   }
9865
9866                 if (elt_size <= sizeof (HOST_WIDE_INT))
9867                   insert_int (lo, elt_size, p);
9868                 else
9869                   {
9870                     unsigned char *p0 = p;
9871                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
9872
9873                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
9874                     if (WORDS_BIG_ENDIAN)
9875                       {
9876                         p0 = p1;
9877                         p1 = p;
9878                       }
9879                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
9880                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
9881                   }
9882               }
9883             break;
9884
9885           case MODE_VECTOR_FLOAT:
9886             for (i = 0, p = array; i < length; i++, p += elt_size)
9887               {
9888                 rtx elt = CONST_VECTOR_ELT (rtl, i);
9889                 insert_float (elt, p);
9890               }
9891             break;
9892
9893           default:
9894             gcc_unreachable ();
9895           }
9896
9897         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
9898       }
9899       break;
9900
9901     case CONST_STRING:
9902       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
9903       break;
9904
9905     case SYMBOL_REF:
9906     case LABEL_REF:
9907     case CONST:
9908       add_AT_addr (die, DW_AT_const_value, rtl);
9909       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9910       break;
9911
9912     case PLUS:
9913       /* In cases where an inlined instance of an inline function is passed
9914          the address of an `auto' variable (which is local to the caller) we
9915          can get a situation where the DECL_RTL of the artificial local
9916          variable (for the inlining) which acts as a stand-in for the
9917          corresponding formal parameter (of the inline function) will look
9918          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
9919          exactly a compile-time constant expression, but it isn't the address
9920          of the (artificial) local variable either.  Rather, it represents the
9921          *value* which the artificial local variable always has during its
9922          lifetime.  We currently have no way to represent such quasi-constant
9923          values in Dwarf, so for now we just punt and generate nothing.  */
9924       break;
9925
9926     default:
9927       /* No other kinds of rtx should be possible here.  */
9928       gcc_unreachable ();
9929     }
9930
9931 }
9932
9933 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
9934    for use in a later add_const_value_attribute call.  */
9935
9936 static rtx
9937 rtl_for_decl_init (tree init, tree type)
9938 {
9939   rtx rtl = NULL_RTX;
9940
9941   /* If a variable is initialized with a string constant without embedded
9942      zeros, build CONST_STRING.  */
9943   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
9944     {
9945       tree enttype = TREE_TYPE (type);
9946       tree domain = TYPE_DOMAIN (type);
9947       enum machine_mode mode = TYPE_MODE (enttype);
9948
9949       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
9950           && domain
9951           && integer_zerop (TYPE_MIN_VALUE (domain))
9952           && compare_tree_int (TYPE_MAX_VALUE (domain),
9953                                TREE_STRING_LENGTH (init) - 1) == 0
9954           && ((size_t) TREE_STRING_LENGTH (init)
9955               == strlen (TREE_STRING_POINTER (init)) + 1))
9956         rtl = gen_rtx_CONST_STRING (VOIDmode,
9957                                     ggc_strdup (TREE_STRING_POINTER (init)));
9958     }
9959   /* If the initializer is something that we know will expand into an
9960      immediate RTL constant, expand it now.  Expanding anything else
9961      tends to produce unresolved symbols; see debug/5770 and c++/6381.  */
9962   /* Aggregate, vector, and complex types may contain constructors that may
9963      result in code being generated when expand_expr is called, so we can't
9964      handle them here.  Integer and float are useful and safe types to handle
9965      here.  */
9966   else if ((INTEGRAL_TYPE_P (type) || SCALAR_FLOAT_TYPE_P (type))
9967            && initializer_constant_valid_p (init, type) == null_pointer_node)
9968     {
9969       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
9970
9971       /* If expand_expr returns a MEM, it wasn't immediate.  */
9972       gcc_assert (!rtl || !MEM_P (rtl));
9973     }
9974
9975   return rtl;
9976 }
9977
9978 /* Generate RTL for the variable DECL to represent its location.  */
9979
9980 static rtx
9981 rtl_for_decl_location (tree decl)
9982 {
9983   rtx rtl;
9984
9985   /* Here we have to decide where we are going to say the parameter "lives"
9986      (as far as the debugger is concerned).  We only have a couple of
9987      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
9988
9989      DECL_RTL normally indicates where the parameter lives during most of the
9990      activation of the function.  If optimization is enabled however, this
9991      could be either NULL or else a pseudo-reg.  Both of those cases indicate
9992      that the parameter doesn't really live anywhere (as far as the code
9993      generation parts of GCC are concerned) during most of the function's
9994      activation.  That will happen (for example) if the parameter is never
9995      referenced within the function.
9996
9997      We could just generate a location descriptor here for all non-NULL
9998      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
9999      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10000      where DECL_RTL is NULL or is a pseudo-reg.
10001
10002      Note however that we can only get away with using DECL_INCOMING_RTL as
10003      a backup substitute for DECL_RTL in certain limited cases.  In cases
10004      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10005      we can be sure that the parameter was passed using the same type as it is
10006      declared to have within the function, and that its DECL_INCOMING_RTL
10007      points us to a place where a value of that type is passed.
10008
10009      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10010      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10011      because in these cases DECL_INCOMING_RTL points us to a value of some
10012      type which is *different* from the type of the parameter itself.  Thus,
10013      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10014      such cases, the debugger would end up (for example) trying to fetch a
10015      `float' from a place which actually contains the first part of a
10016      `double'.  That would lead to really incorrect and confusing
10017      output at debug-time.
10018
10019      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10020      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10021      are a couple of exceptions however.  On little-endian machines we can
10022      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10023      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10024      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10025      when (on a little-endian machine) a non-prototyped function has a
10026      parameter declared to be of type `short' or `char'.  In such cases,
10027      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10028      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10029      passed `int' value.  If the debugger then uses that address to fetch
10030      a `short' or a `char' (on a little-endian machine) the result will be
10031      the correct data, so we allow for such exceptional cases below.
10032
10033      Note that our goal here is to describe the place where the given formal
10034      parameter lives during most of the function's activation (i.e. between the
10035      end of the prologue and the start of the epilogue).  We'll do that as best
10036      as we can. Note however that if the given formal parameter is modified
10037      sometime during the execution of the function, then a stack backtrace (at
10038      debug-time) will show the function as having been called with the *new*
10039      value rather than the value which was originally passed in.  This happens
10040      rarely enough that it is not a major problem, but it *is* a problem, and
10041      I'd like to fix it.
10042
10043      A future version of dwarf2out.c may generate two additional attributes for
10044      any given DW_TAG_formal_parameter DIE which will describe the "passed
10045      type" and the "passed location" for the given formal parameter in addition
10046      to the attributes we now generate to indicate the "declared type" and the
10047      "active location" for each parameter.  This additional set of attributes
10048      could be used by debuggers for stack backtraces. Separately, note that
10049      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10050      This happens (for example) for inlined-instances of inline function formal
10051      parameters which are never referenced.  This really shouldn't be
10052      happening.  All PARM_DECL nodes should get valid non-NULL
10053      DECL_INCOMING_RTL values.  FIXME.  */
10054
10055   /* Use DECL_RTL as the "location" unless we find something better.  */
10056   rtl = DECL_RTL_IF_SET (decl);
10057
10058   /* When generating abstract instances, ignore everything except
10059      constants, symbols living in memory, and symbols living in
10060      fixed registers.  */
10061   if (! reload_completed)
10062     {
10063       if (rtl
10064           && (CONSTANT_P (rtl)
10065               || (MEM_P (rtl)
10066                   && CONSTANT_P (XEXP (rtl, 0)))
10067               || (REG_P (rtl)
10068                   && TREE_CODE (decl) == VAR_DECL
10069                   && TREE_STATIC (decl))))
10070         {
10071           rtl = targetm.delegitimize_address (rtl);
10072           return rtl;
10073         }
10074       rtl = NULL_RTX;
10075     }
10076   else if (TREE_CODE (decl) == PARM_DECL)
10077     {
10078       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10079         {
10080           tree declared_type = TREE_TYPE (decl);
10081           tree passed_type = DECL_ARG_TYPE (decl);
10082           enum machine_mode dmode = TYPE_MODE (declared_type);
10083           enum machine_mode pmode = TYPE_MODE (passed_type);
10084
10085           /* This decl represents a formal parameter which was optimized out.
10086              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10087              all cases where (rtl == NULL_RTX) just below.  */
10088           if (dmode == pmode)
10089             rtl = DECL_INCOMING_RTL (decl);
10090           else if (SCALAR_INT_MODE_P (dmode)
10091                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10092                    && DECL_INCOMING_RTL (decl))
10093             {
10094               rtx inc = DECL_INCOMING_RTL (decl);
10095               if (REG_P (inc))
10096                 rtl = inc;
10097               else if (MEM_P (inc))
10098                 {
10099                   if (BYTES_BIG_ENDIAN)
10100                     rtl = adjust_address_nv (inc, dmode,
10101                                              GET_MODE_SIZE (pmode)
10102                                              - GET_MODE_SIZE (dmode));
10103                   else
10104                     rtl = inc;
10105                 }
10106             }
10107         }
10108
10109       /* If the parm was passed in registers, but lives on the stack, then
10110          make a big endian correction if the mode of the type of the
10111          parameter is not the same as the mode of the rtl.  */
10112       /* ??? This is the same series of checks that are made in dbxout.c before
10113          we reach the big endian correction code there.  It isn't clear if all
10114          of these checks are necessary here, but keeping them all is the safe
10115          thing to do.  */
10116       else if (MEM_P (rtl)
10117                && XEXP (rtl, 0) != const0_rtx
10118                && ! CONSTANT_P (XEXP (rtl, 0))
10119                /* Not passed in memory.  */
10120                && !MEM_P (DECL_INCOMING_RTL (decl))
10121                /* Not passed by invisible reference.  */
10122                && (!REG_P (XEXP (rtl, 0))
10123                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10124                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10125 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10126                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10127 #endif
10128                      )
10129                /* Big endian correction check.  */
10130                && BYTES_BIG_ENDIAN
10131                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10132                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10133                    < UNITS_PER_WORD))
10134         {
10135           int offset = (UNITS_PER_WORD
10136                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10137
10138           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10139                              plus_constant (XEXP (rtl, 0), offset));
10140         }
10141     }
10142   else if (TREE_CODE (decl) == VAR_DECL
10143            && rtl
10144            && MEM_P (rtl)
10145            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10146            && BYTES_BIG_ENDIAN)
10147     {
10148       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10149       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10150
10151       /* If a variable is declared "register" yet is smaller than
10152          a register, then if we store the variable to memory, it
10153          looks like we're storing a register-sized value, when in
10154          fact we are not.  We need to adjust the offset of the
10155          storage location to reflect the actual value's bytes,
10156          else gdb will not be able to display it.  */
10157       if (rsize > dsize)
10158         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10159                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10160     }
10161
10162   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10163      and will have been substituted directly into all expressions that use it.
10164      C does not have such a concept, but C++ and other languages do.  */
10165   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10166     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10167
10168   if (rtl)
10169     rtl = targetm.delegitimize_address (rtl);
10170
10171   /* If we don't look past the constant pool, we risk emitting a
10172      reference to a constant pool entry that isn't referenced from
10173      code, and thus is not emitted.  */
10174   if (rtl)
10175     rtl = avoid_constant_pool_reference (rtl);
10176
10177   return rtl;
10178 }
10179
10180 /* We need to figure out what section we should use as the base for the
10181    address ranges where a given location is valid.
10182    1. If this particular DECL has a section associated with it, use that.
10183    2. If this function has a section associated with it, use that.
10184    3. Otherwise, use the text section.
10185    XXX: If you split a variable across multiple sections, we won't notice.  */
10186
10187 static const char *
10188 secname_for_decl (tree decl)
10189 {
10190   const char *secname;
10191
10192   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10193     {
10194       tree sectree = DECL_SECTION_NAME (decl);
10195       secname = TREE_STRING_POINTER (sectree);
10196     }
10197   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10198     {
10199       tree sectree = DECL_SECTION_NAME (current_function_decl);
10200       secname = TREE_STRING_POINTER (sectree);
10201     }
10202   else if (cfun && in_cold_section_p)
10203     secname = cfun->cold_section_label;
10204   else
10205     secname = text_section_label;
10206
10207   return secname;
10208 }
10209
10210 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10211    data attribute for a variable or a parameter.  We generate the
10212    DW_AT_const_value attribute only in those cases where the given variable
10213    or parameter does not have a true "location" either in memory or in a
10214    register.  This can happen (for example) when a constant is passed as an
10215    actual argument in a call to an inline function.  (It's possible that
10216    these things can crop up in other ways also.)  Note that one type of
10217    constant value which can be passed into an inlined function is a constant
10218    pointer.  This can happen for example if an actual argument in an inlined
10219    function call evaluates to a compile-time constant address.  */
10220
10221 static void
10222 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10223                                        enum dwarf_attribute attr)
10224 {
10225   rtx rtl;
10226   dw_loc_descr_ref descr;
10227   var_loc_list *loc_list;
10228   struct var_loc_node *node;
10229   if (TREE_CODE (decl) == ERROR_MARK)
10230     return;
10231
10232   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10233               || TREE_CODE (decl) == RESULT_DECL);
10234              
10235   /* See if we possibly have multiple locations for this variable.  */
10236   loc_list = lookup_decl_loc (decl);
10237
10238   /* If it truly has multiple locations, the first and last node will
10239      differ.  */
10240   if (loc_list && loc_list->first != loc_list->last)
10241     {
10242       const char *endname, *secname;
10243       dw_loc_list_ref list;
10244       rtx varloc;
10245
10246       /* Now that we know what section we are using for a base,
10247          actually construct the list of locations.
10248          The first location information is what is passed to the
10249          function that creates the location list, and the remaining
10250          locations just get added on to that list.
10251          Note that we only know the start address for a location
10252          (IE location changes), so to build the range, we use
10253          the range [current location start, next location start].
10254          This means we have to special case the last node, and generate
10255          a range of [last location start, end of function label].  */
10256
10257       node = loc_list->first;
10258       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10259       secname = secname_for_decl (decl);
10260
10261       list = new_loc_list (loc_descriptor (varloc),
10262                            node->label, node->next->label, secname, 1);
10263       node = node->next;
10264
10265       for (; node->next; node = node->next)
10266         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10267           {
10268             /* The variable has a location between NODE->LABEL and
10269                NODE->NEXT->LABEL.  */
10270             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10271             add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10272                                        node->label, node->next->label, secname);
10273           }
10274
10275       /* If the variable has a location at the last label
10276          it keeps its location until the end of function.  */
10277       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10278         {
10279           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10280
10281           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10282           if (!current_function_decl)
10283             endname = text_end_label;
10284           else
10285             {
10286               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10287                                            current_function_funcdef_no);
10288               endname = ggc_strdup (label_id);
10289             }
10290           add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10291                                      node->label, endname, secname);
10292         }
10293
10294       /* Finally, add the location list to the DIE, and we are done.  */
10295       add_AT_loc_list (die, attr, list);
10296       return;
10297     }
10298
10299   /* Try to get some constant RTL for this decl, and use that as the value of
10300      the location.  */
10301   
10302   rtl = rtl_for_decl_location (decl);
10303   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10304     {
10305       add_const_value_attribute (die, rtl);
10306       return;
10307     }
10308   
10309   /* If we have tried to generate the location otherwise, and it
10310      didn't work out (we wouldn't be here if we did), and we have a one entry
10311      location list, try generating a location from that.  */
10312   if (loc_list && loc_list->first)
10313     {
10314       node = loc_list->first;
10315       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10316       if (descr)
10317         {
10318           add_AT_location_description (die, attr, descr);
10319           return;
10320         }
10321     }
10322
10323   /* We couldn't get any rtl, so try directly generating the location
10324      description from the tree.  */
10325   descr = loc_descriptor_from_tree (decl);
10326   if (descr)
10327     {
10328       add_AT_location_description (die, attr, descr);
10329       return;
10330     }
10331 }
10332
10333 /* If we don't have a copy of this variable in memory for some reason (such
10334    as a C++ member constant that doesn't have an out-of-line definition),
10335    we should tell the debugger about the constant value.  */
10336
10337 static void
10338 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10339 {
10340   tree init = DECL_INITIAL (decl);
10341   tree type = TREE_TYPE (decl);
10342   rtx rtl;
10343
10344   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10345     /* OK */;
10346   else
10347     return;
10348
10349   rtl = rtl_for_decl_init (init, type);
10350   if (rtl)
10351     add_const_value_attribute (var_die, rtl);
10352 }
10353
10354 /* Convert the CFI instructions for the current function into a
10355    location list.  This is used for DW_AT_frame_base when we targeting
10356    a dwarf2 consumer that does not support the dwarf3
10357    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10358    expressions.  */
10359
10360 static dw_loc_list_ref
10361 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10362 {
10363   dw_fde_ref fde;
10364   dw_loc_list_ref list, *list_tail;
10365   dw_cfi_ref cfi;
10366   dw_cfa_location last_cfa, next_cfa;
10367   const char *start_label, *last_label, *section;
10368
10369   fde = &fde_table[fde_table_in_use - 1];
10370
10371   section = secname_for_decl (current_function_decl);
10372   list_tail = &list;
10373   list = NULL;
10374
10375   next_cfa.reg = INVALID_REGNUM;
10376   next_cfa.offset = 0;
10377   next_cfa.indirect = 0;
10378   next_cfa.base_offset = 0;
10379
10380   start_label = fde->dw_fde_begin;
10381
10382   /* ??? Bald assumption that the CIE opcode list does not contain
10383      advance opcodes.  */
10384   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10385     lookup_cfa_1 (cfi, &next_cfa);
10386
10387   last_cfa = next_cfa;
10388   last_label = start_label;
10389
10390   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10391     switch (cfi->dw_cfi_opc)
10392       {
10393       case DW_CFA_advance_loc1:
10394       case DW_CFA_advance_loc2:
10395       case DW_CFA_advance_loc4:
10396         if (!cfa_equal_p (&last_cfa, &next_cfa))
10397           {
10398             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10399                                        start_label, last_label, section,
10400                                        list == NULL);
10401
10402             list_tail = &(*list_tail)->dw_loc_next;
10403             last_cfa = next_cfa;
10404             start_label = last_label;
10405           }
10406         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10407         break;
10408
10409       case DW_CFA_advance_loc:
10410         /* The encoding is complex enough that we should never emit this.  */
10411       case DW_CFA_remember_state:
10412       case DW_CFA_restore_state:
10413         /* We don't handle these two in this function.  It would be possible
10414            if it were to be required.  */
10415         gcc_unreachable ();
10416
10417       default:
10418         lookup_cfa_1 (cfi, &next_cfa);
10419         break;
10420       }
10421
10422   if (!cfa_equal_p (&last_cfa, &next_cfa))
10423     {
10424       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10425                                  start_label, last_label, section,
10426                                  list == NULL);
10427       list_tail = &(*list_tail)->dw_loc_next;
10428       start_label = last_label;
10429     }
10430   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10431                              start_label, fde->dw_fde_end, section,
10432                              list == NULL);
10433
10434   return list;
10435 }
10436
10437 /* Compute a displacement from the "steady-state frame pointer" to the
10438    frame base (often the same as the CFA), and store it in
10439    frame_pointer_fb_offset.  OFFSET is added to the displacement
10440    before the latter is negated.  */
10441
10442 static void
10443 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10444 {
10445   rtx reg, elim;
10446
10447 #ifdef FRAME_POINTER_CFA_OFFSET
10448   reg = frame_pointer_rtx;
10449   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10450 #else
10451   reg = arg_pointer_rtx;
10452   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10453 #endif
10454
10455   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10456   if (GET_CODE (elim) == PLUS)
10457     {
10458       offset += INTVAL (XEXP (elim, 1));
10459       elim = XEXP (elim, 0);
10460     }
10461   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10462                        : stack_pointer_rtx));
10463
10464   frame_pointer_fb_offset = -offset;
10465 }
10466
10467 /* Generate a DW_AT_name attribute given some string value to be included as
10468    the value of the attribute.  */
10469
10470 static void
10471 add_name_attribute (dw_die_ref die, const char *name_string)
10472 {
10473   if (name_string != NULL && *name_string != 0)
10474     {
10475       if (demangle_name_func)
10476         name_string = (*demangle_name_func) (name_string);
10477
10478       add_AT_string (die, DW_AT_name, name_string);
10479     }
10480 }
10481
10482 /* Generate a DW_AT_comp_dir attribute for DIE.  */
10483
10484 static void
10485 add_comp_dir_attribute (dw_die_ref die)
10486 {
10487   const char *wd = get_src_pwd ();
10488   if (wd != NULL)
10489     add_AT_string (die, DW_AT_comp_dir, wd);
10490 }
10491
10492 /* Given a tree node describing an array bound (either lower or upper) output
10493    a representation for that bound.  */
10494
10495 static void
10496 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10497 {
10498   switch (TREE_CODE (bound))
10499     {
10500     case ERROR_MARK:
10501       return;
10502
10503     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
10504     case INTEGER_CST:
10505       if (! host_integerp (bound, 0)
10506           || (bound_attr == DW_AT_lower_bound
10507               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
10508                   || (is_fortran () && integer_onep (bound)))))
10509         /* Use the default.  */
10510         ;
10511       else
10512         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10513       break;
10514
10515     case CONVERT_EXPR:
10516     case NOP_EXPR:
10517     case NON_LVALUE_EXPR:
10518     case VIEW_CONVERT_EXPR:
10519       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10520       break;
10521
10522     case SAVE_EXPR:
10523       break;
10524
10525     case VAR_DECL:
10526     case PARM_DECL:
10527     case RESULT_DECL:
10528       {
10529         dw_die_ref decl_die = lookup_decl_die (bound);
10530
10531         /* ??? Can this happen, or should the variable have been bound
10532            first?  Probably it can, since I imagine that we try to create
10533            the types of parameters in the order in which they exist in
10534            the list, and won't have created a forward reference to a
10535            later parameter.  */
10536         if (decl_die != NULL)
10537           add_AT_die_ref (subrange_die, bound_attr, decl_die);
10538         break;
10539       }
10540
10541     default:
10542       {
10543         /* Otherwise try to create a stack operation procedure to
10544            evaluate the value of the array bound.  */
10545
10546         dw_die_ref ctx, decl_die;
10547         dw_loc_descr_ref loc;
10548
10549         loc = loc_descriptor_from_tree (bound);
10550         if (loc == NULL)
10551           break;
10552
10553         if (current_function_decl == 0)
10554           ctx = comp_unit_die;
10555         else
10556           ctx = lookup_decl_die (current_function_decl);
10557
10558         decl_die = new_die (DW_TAG_variable, ctx, bound);
10559         add_AT_flag (decl_die, DW_AT_artificial, 1);
10560         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10561         add_AT_loc (decl_die, DW_AT_location, loc);
10562
10563         add_AT_die_ref (subrange_die, bound_attr, decl_die);
10564         break;
10565       }
10566     }
10567 }
10568
10569 /* Note that the block of subscript information for an array type also
10570    includes information about the element type of type given array type.  */
10571
10572 static void
10573 add_subscript_info (dw_die_ref type_die, tree type)
10574 {
10575 #ifndef MIPS_DEBUGGING_INFO
10576   unsigned dimension_number;
10577 #endif
10578   tree lower, upper;
10579   dw_die_ref subrange_die;
10580
10581   /* The GNU compilers represent multidimensional array types as sequences of
10582      one dimensional array types whose element types are themselves array
10583      types.  Here we squish that down, so that each multidimensional array
10584      type gets only one array_type DIE in the Dwarf debugging info. The draft
10585      Dwarf specification say that we are allowed to do this kind of
10586      compression in C (because there is no difference between an array or
10587      arrays and a multidimensional array in C) but for other source languages
10588      (e.g. Ada) we probably shouldn't do this.  */
10589
10590   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10591      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
10592      We work around this by disabling this feature.  See also
10593      gen_array_type_die.  */
10594 #ifndef MIPS_DEBUGGING_INFO
10595   for (dimension_number = 0;
10596        TREE_CODE (type) == ARRAY_TYPE;
10597        type = TREE_TYPE (type), dimension_number++)
10598 #endif
10599     {
10600       tree domain = TYPE_DOMAIN (type);
10601
10602       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10603          and (in GNU C only) variable bounds.  Handle all three forms
10604          here.  */
10605       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10606       if (domain)
10607         {
10608           /* We have an array type with specified bounds.  */
10609           lower = TYPE_MIN_VALUE (domain);
10610           upper = TYPE_MAX_VALUE (domain);
10611
10612           /* Define the index type.  */
10613           if (TREE_TYPE (domain))
10614             {
10615               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
10616                  TREE_TYPE field.  We can't emit debug info for this
10617                  because it is an unnamed integral type.  */
10618               if (TREE_CODE (domain) == INTEGER_TYPE
10619                   && TYPE_NAME (domain) == NULL_TREE
10620                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10621                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10622                 ;
10623               else
10624                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10625                                     type_die);
10626             }
10627
10628           /* ??? If upper is NULL, the array has unspecified length,
10629              but it does have a lower bound.  This happens with Fortran
10630                dimension arr(N:*)
10631              Since the debugger is definitely going to need to know N
10632              to produce useful results, go ahead and output the lower
10633              bound solo, and hope the debugger can cope.  */
10634
10635           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10636           if (upper)
10637             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10638         }
10639
10640       /* Otherwise we have an array type with an unspecified length.  The
10641          DWARF-2 spec does not say how to handle this; let's just leave out the
10642          bounds.  */
10643     }
10644 }
10645
10646 static void
10647 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10648 {
10649   unsigned size;
10650
10651   switch (TREE_CODE (tree_node))
10652     {
10653     case ERROR_MARK:
10654       size = 0;
10655       break;
10656     case ENUMERAL_TYPE:
10657     case RECORD_TYPE:
10658     case UNION_TYPE:
10659     case QUAL_UNION_TYPE:
10660       size = int_size_in_bytes (tree_node);
10661       break;
10662     case FIELD_DECL:
10663       /* For a data member of a struct or union, the DW_AT_byte_size is
10664          generally given as the number of bytes normally allocated for an
10665          object of the *declared* type of the member itself.  This is true
10666          even for bit-fields.  */
10667       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10668       break;
10669     default:
10670       gcc_unreachable ();
10671     }
10672
10673   /* Note that `size' might be -1 when we get to this point.  If it is, that
10674      indicates that the byte size of the entity in question is variable.  We
10675      have no good way of expressing this fact in Dwarf at the present time,
10676      so just let the -1 pass on through.  */
10677   add_AT_unsigned (die, DW_AT_byte_size, size);
10678 }
10679
10680 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10681    which specifies the distance in bits from the highest order bit of the
10682    "containing object" for the bit-field to the highest order bit of the
10683    bit-field itself.
10684
10685    For any given bit-field, the "containing object" is a hypothetical object
10686    (of some integral or enum type) within which the given bit-field lives.  The
10687    type of this hypothetical "containing object" is always the same as the
10688    declared type of the individual bit-field itself.  The determination of the
10689    exact location of the "containing object" for a bit-field is rather
10690    complicated.  It's handled by the `field_byte_offset' function (above).
10691
10692    Note that it is the size (in bytes) of the hypothetical "containing object"
10693    which will be given in the DW_AT_byte_size attribute for this bit-field.
10694    (See `byte_size_attribute' above).  */
10695
10696 static inline void
10697 add_bit_offset_attribute (dw_die_ref die, tree decl)
10698 {
10699   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10700   tree type = DECL_BIT_FIELD_TYPE (decl);
10701   HOST_WIDE_INT bitpos_int;
10702   HOST_WIDE_INT highest_order_object_bit_offset;
10703   HOST_WIDE_INT highest_order_field_bit_offset;
10704   HOST_WIDE_INT unsigned bit_offset;
10705
10706   /* Must be a field and a bit field.  */
10707   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10708
10709   /* We can't yet handle bit-fields whose offsets are variable, so if we
10710      encounter such things, just return without generating any attribute
10711      whatsoever.  Likewise for variable or too large size.  */
10712   if (! host_integerp (bit_position (decl), 0)
10713       || ! host_integerp (DECL_SIZE (decl), 1))
10714     return;
10715
10716   bitpos_int = int_bit_position (decl);
10717
10718   /* Note that the bit offset is always the distance (in bits) from the
10719      highest-order bit of the "containing object" to the highest-order bit of
10720      the bit-field itself.  Since the "high-order end" of any object or field
10721      is different on big-endian and little-endian machines, the computation
10722      below must take account of these differences.  */
10723   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10724   highest_order_field_bit_offset = bitpos_int;
10725
10726   if (! BYTES_BIG_ENDIAN)
10727     {
10728       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10729       highest_order_object_bit_offset += simple_type_size_in_bits (type);
10730     }
10731
10732   bit_offset
10733     = (! BYTES_BIG_ENDIAN
10734        ? highest_order_object_bit_offset - highest_order_field_bit_offset
10735        : highest_order_field_bit_offset - highest_order_object_bit_offset);
10736
10737   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10738 }
10739
10740 /* For a FIELD_DECL node which represents a bit field, output an attribute
10741    which specifies the length in bits of the given field.  */
10742
10743 static inline void
10744 add_bit_size_attribute (dw_die_ref die, tree decl)
10745 {
10746   /* Must be a field and a bit field.  */
10747   gcc_assert (TREE_CODE (decl) == FIELD_DECL
10748               && DECL_BIT_FIELD_TYPE (decl));
10749
10750   if (host_integerp (DECL_SIZE (decl), 1))
10751     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10752 }
10753
10754 /* If the compiled language is ANSI C, then add a 'prototyped'
10755    attribute, if arg types are given for the parameters of a function.  */
10756
10757 static inline void
10758 add_prototyped_attribute (dw_die_ref die, tree func_type)
10759 {
10760   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10761       && TYPE_ARG_TYPES (func_type) != NULL)
10762     add_AT_flag (die, DW_AT_prototyped, 1);
10763 }
10764
10765 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
10766    by looking in either the type declaration or object declaration
10767    equate table.  */
10768
10769 static inline void
10770 add_abstract_origin_attribute (dw_die_ref die, tree origin)
10771 {
10772   dw_die_ref origin_die = NULL;
10773
10774   if (TREE_CODE (origin) != FUNCTION_DECL)
10775     {
10776       /* We may have gotten separated from the block for the inlined
10777          function, if we're in an exception handler or some such; make
10778          sure that the abstract function has been written out.
10779
10780          Doing this for nested functions is wrong, however; functions are
10781          distinct units, and our context might not even be inline.  */
10782       tree fn = origin;
10783
10784       if (TYPE_P (fn))
10785         fn = TYPE_STUB_DECL (fn);
10786       
10787       fn = decl_function_context (fn);
10788       if (fn)
10789         dwarf2out_abstract_function (fn);
10790     }
10791
10792   if (DECL_P (origin))
10793     origin_die = lookup_decl_die (origin);
10794   else if (TYPE_P (origin))
10795     origin_die = lookup_type_die (origin);
10796
10797   /* XXX: Functions that are never lowered don't always have correct block
10798      trees (in the case of java, they simply have no block tree, in some other
10799      languages).  For these functions, there is nothing we can really do to
10800      output correct debug info for inlined functions in all cases.  Rather
10801      than die, we'll just produce deficient debug info now, in that we will
10802      have variables without a proper abstract origin.  In the future, when all
10803      functions are lowered, we should re-add a gcc_assert (origin_die)
10804      here.  */
10805
10806   if (origin_die)
10807       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
10808 }
10809
10810 /* We do not currently support the pure_virtual attribute.  */
10811
10812 static inline void
10813 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
10814 {
10815   if (DECL_VINDEX (func_decl))
10816     {
10817       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
10818
10819       if (host_integerp (DECL_VINDEX (func_decl), 0))
10820         add_AT_loc (die, DW_AT_vtable_elem_location,
10821                     new_loc_descr (DW_OP_constu,
10822                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
10823                                    0));
10824
10825       /* GNU extension: Record what type this method came from originally.  */
10826       if (debug_info_level > DINFO_LEVEL_TERSE)
10827         add_AT_die_ref (die, DW_AT_containing_type,
10828                         lookup_type_die (DECL_CONTEXT (func_decl)));
10829     }
10830 }
10831 \f
10832 /* Add source coordinate attributes for the given decl.  */
10833
10834 static void
10835 add_src_coords_attributes (dw_die_ref die, tree decl)
10836 {
10837   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
10838   unsigned file_index = lookup_filename (s.file);
10839
10840   add_AT_unsigned (die, DW_AT_decl_file, file_index);
10841   add_AT_unsigned (die, DW_AT_decl_line, s.line);
10842 }
10843
10844 /* Add a DW_AT_name attribute and source coordinate attribute for the
10845    given decl, but only if it actually has a name.  */
10846
10847 static void
10848 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
10849 {
10850   tree decl_name;
10851
10852   decl_name = DECL_NAME (decl);
10853   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
10854     {
10855       add_name_attribute (die, dwarf2_name (decl, 0));
10856       if (! DECL_ARTIFICIAL (decl))
10857         add_src_coords_attributes (die, decl);
10858
10859       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
10860           && TREE_PUBLIC (decl)
10861           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
10862           && !DECL_ABSTRACT (decl)
10863           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
10864         add_AT_string (die, DW_AT_MIPS_linkage_name,
10865                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
10866     }
10867
10868 #ifdef VMS_DEBUGGING_INFO
10869   /* Get the function's name, as described by its RTL.  This may be different
10870      from the DECL_NAME name used in the source file.  */
10871   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
10872     {
10873       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
10874                    XEXP (DECL_RTL (decl), 0));
10875       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
10876     }
10877 #endif
10878 }
10879
10880 /* Push a new declaration scope.  */
10881
10882 static void
10883 push_decl_scope (tree scope)
10884 {
10885   VEC_safe_push (tree, gc, decl_scope_table, scope);
10886 }
10887
10888 /* Pop a declaration scope.  */
10889
10890 static inline void
10891 pop_decl_scope (void)
10892 {
10893   VEC_pop (tree, decl_scope_table);
10894 }
10895
10896 /* Return the DIE for the scope that immediately contains this type.
10897    Non-named types get global scope.  Named types nested in other
10898    types get their containing scope if it's open, or global scope
10899    otherwise.  All other types (i.e. function-local named types) get
10900    the current active scope.  */
10901
10902 static dw_die_ref
10903 scope_die_for (tree t, dw_die_ref context_die)
10904 {
10905   dw_die_ref scope_die = NULL;
10906   tree containing_scope;
10907   int i;
10908
10909   /* Non-types always go in the current scope.  */
10910   gcc_assert (TYPE_P (t));
10911
10912   containing_scope = TYPE_CONTEXT (t);
10913
10914   /* Use the containing namespace if it was passed in (for a declaration).  */
10915   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
10916     {
10917       if (context_die == lookup_decl_die (containing_scope))
10918         /* OK */;
10919       else
10920         containing_scope = NULL_TREE;
10921     }
10922
10923   /* Ignore function type "scopes" from the C frontend.  They mean that
10924      a tagged type is local to a parmlist of a function declarator, but
10925      that isn't useful to DWARF.  */
10926   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
10927     containing_scope = NULL_TREE;
10928
10929   if (containing_scope == NULL_TREE)
10930     scope_die = comp_unit_die;
10931   else if (TYPE_P (containing_scope))
10932     {
10933       /* For types, we can just look up the appropriate DIE.  But
10934          first we check to see if we're in the middle of emitting it
10935          so we know where the new DIE should go.  */
10936       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
10937         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
10938           break;
10939
10940       if (i < 0)
10941         {
10942           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
10943                       || TREE_ASM_WRITTEN (containing_scope));
10944
10945           /* If none of the current dies are suitable, we get file scope.  */
10946           scope_die = comp_unit_die;
10947         }
10948       else
10949         scope_die = lookup_type_die (containing_scope);
10950     }
10951   else
10952     scope_die = context_die;
10953
10954   return scope_die;
10955 }
10956
10957 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
10958
10959 static inline int
10960 local_scope_p (dw_die_ref context_die)
10961 {
10962   for (; context_die; context_die = context_die->die_parent)
10963     if (context_die->die_tag == DW_TAG_inlined_subroutine
10964         || context_die->die_tag == DW_TAG_subprogram)
10965       return 1;
10966
10967   return 0;
10968 }
10969
10970 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
10971    whether or not to treat a DIE in this context as a declaration.  */
10972
10973 static inline int
10974 class_or_namespace_scope_p (dw_die_ref context_die)
10975 {
10976   return (context_die
10977           && (context_die->die_tag == DW_TAG_structure_type
10978               || context_die->die_tag == DW_TAG_union_type
10979               || context_die->die_tag == DW_TAG_namespace));
10980 }
10981
10982 /* Many forms of DIEs require a "type description" attribute.  This
10983    routine locates the proper "type descriptor" die for the type given
10984    by 'type', and adds a DW_AT_type attribute below the given die.  */
10985
10986 static void
10987 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
10988                     int decl_volatile, dw_die_ref context_die)
10989 {
10990   enum tree_code code  = TREE_CODE (type);
10991   dw_die_ref type_die  = NULL;
10992
10993   /* ??? If this type is an unnamed subrange type of an integral or
10994      floating-point type, use the inner type.  This is because we have no
10995      support for unnamed types in base_type_die.  This can happen if this is
10996      an Ada subrange type.  Correct solution is emit a subrange type die.  */
10997   if ((code == INTEGER_TYPE || code == REAL_TYPE)
10998       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
10999     type = TREE_TYPE (type), code = TREE_CODE (type);
11000
11001   if (code == ERROR_MARK
11002       /* Handle a special case.  For functions whose return type is void, we
11003          generate *no* type attribute.  (Note that no object may have type
11004          `void', so this only applies to function return types).  */
11005       || code == VOID_TYPE)
11006     return;
11007
11008   type_die = modified_type_die (type,
11009                                 decl_const || TYPE_READONLY (type),
11010                                 decl_volatile || TYPE_VOLATILE (type),
11011                                 context_die);
11012
11013   if (type_die != NULL)
11014     add_AT_die_ref (object_die, DW_AT_type, type_die);
11015 }
11016
11017 /* Given an object die, add the calling convention attribute for the
11018    function call type.  */
11019 static void
11020 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11021 {
11022   enum dwarf_calling_convention value = DW_CC_normal;
11023
11024   value = targetm.dwarf_calling_convention (type);
11025
11026   /* Only add the attribute if the backend requests it, and
11027      is not DW_CC_normal.  */
11028   if (value && (value != DW_CC_normal))
11029     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11030 }
11031
11032 /* Given a tree pointer to a struct, class, union, or enum type node, return
11033    a pointer to the (string) tag name for the given type, or zero if the type
11034    was declared without a tag.  */
11035
11036 static const char *
11037 type_tag (tree type)
11038 {
11039   const char *name = 0;
11040
11041   if (TYPE_NAME (type) != 0)
11042     {
11043       tree t = 0;
11044
11045       /* Find the IDENTIFIER_NODE for the type name.  */
11046       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11047         t = TYPE_NAME (type);
11048
11049       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11050          a TYPE_DECL node, regardless of whether or not a `typedef' was
11051          involved.  */
11052       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11053                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11054         t = DECL_NAME (TYPE_NAME (type));
11055
11056       /* Now get the name as a string, or invent one.  */
11057       if (t != 0)
11058         name = IDENTIFIER_POINTER (t);
11059     }
11060
11061   return (name == 0 || *name == '\0') ? 0 : name;
11062 }
11063
11064 /* Return the type associated with a data member, make a special check
11065    for bit field types.  */
11066
11067 static inline tree
11068 member_declared_type (tree member)
11069 {
11070   return (DECL_BIT_FIELD_TYPE (member)
11071           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11072 }
11073
11074 /* Get the decl's label, as described by its RTL. This may be different
11075    from the DECL_NAME name used in the source file.  */
11076
11077 #if 0
11078 static const char *
11079 decl_start_label (tree decl)
11080 {
11081   rtx x;
11082   const char *fnname;
11083
11084   x = DECL_RTL (decl);
11085   gcc_assert (MEM_P (x));
11086
11087   x = XEXP (x, 0);
11088   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11089
11090   fnname = XSTR (x, 0);
11091   return fnname;
11092 }
11093 #endif
11094 \f
11095 /* These routines generate the internal representation of the DIE's for
11096    the compilation unit.  Debugging information is collected by walking
11097    the declaration trees passed in from dwarf2out_decl().  */
11098
11099 static void
11100 gen_array_type_die (tree type, dw_die_ref context_die)
11101 {
11102   dw_die_ref scope_die = scope_die_for (type, context_die);
11103   dw_die_ref array_die;
11104   tree element_type;
11105
11106   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11107      the inner array type comes before the outer array type.  Thus we must
11108      call gen_type_die before we call new_die.  See below also.  */
11109 #ifdef MIPS_DEBUGGING_INFO
11110   gen_type_die (TREE_TYPE (type), context_die);
11111 #endif
11112
11113   array_die = new_die (DW_TAG_array_type, scope_die, type);
11114   add_name_attribute (array_die, type_tag (type));
11115   equate_type_number_to_die (type, array_die);
11116
11117   if (TREE_CODE (type) == VECTOR_TYPE)
11118     {
11119       /* The frontend feeds us a representation for the vector as a struct
11120          containing an array.  Pull out the array type.  */
11121       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11122       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11123     }
11124
11125 #if 0
11126   /* We default the array ordering.  SDB will probably do
11127      the right things even if DW_AT_ordering is not present.  It's not even
11128      an issue until we start to get into multidimensional arrays anyway.  If
11129      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11130      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11131      and when we find out that we need to put these in, we will only do so
11132      for multidimensional arrays.  */
11133   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11134 #endif
11135
11136 #ifdef MIPS_DEBUGGING_INFO
11137   /* The SGI compilers handle arrays of unknown bound by setting
11138      AT_declaration and not emitting any subrange DIEs.  */
11139   if (! TYPE_DOMAIN (type))
11140     add_AT_flag (array_die, DW_AT_declaration, 1);
11141   else
11142 #endif
11143     add_subscript_info (array_die, type);
11144
11145   /* Add representation of the type of the elements of this array type.  */
11146   element_type = TREE_TYPE (type);
11147
11148   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11149      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11150      We work around this by disabling this feature.  See also
11151      add_subscript_info.  */
11152 #ifndef MIPS_DEBUGGING_INFO
11153   while (TREE_CODE (element_type) == ARRAY_TYPE)
11154     element_type = TREE_TYPE (element_type);
11155
11156   gen_type_die (element_type, context_die);
11157 #endif
11158
11159   add_type_attribute (array_die, element_type, 0, 0, context_die);
11160 }
11161
11162 #if 0
11163 static void
11164 gen_entry_point_die (tree decl, dw_die_ref context_die)
11165 {
11166   tree origin = decl_ultimate_origin (decl);
11167   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11168
11169   if (origin != NULL)
11170     add_abstract_origin_attribute (decl_die, origin);
11171   else
11172     {
11173       add_name_and_src_coords_attributes (decl_die, decl);
11174       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11175                           0, 0, context_die);
11176     }
11177
11178   if (DECL_ABSTRACT (decl))
11179     equate_decl_number_to_die (decl, decl_die);
11180   else
11181     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11182 }
11183 #endif
11184
11185 /* Walk through the list of incomplete types again, trying once more to
11186    emit full debugging info for them.  */
11187
11188 static void
11189 retry_incomplete_types (void)
11190 {
11191   int i;
11192
11193   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11194     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11195 }
11196
11197 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11198
11199 static void
11200 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11201 {
11202   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11203
11204   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11205      be incomplete and such types are not marked.  */
11206   add_abstract_origin_attribute (type_die, type);
11207 }
11208
11209 /* Generate a DIE to represent an inlined instance of a structure type.  */
11210
11211 static void
11212 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11213 {
11214   dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11215
11216   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11217      be incomplete and such types are not marked.  */
11218   add_abstract_origin_attribute (type_die, type);
11219 }
11220
11221 /* Generate a DIE to represent an inlined instance of a union type.  */
11222
11223 static void
11224 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11225 {
11226   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11227
11228   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11229      be incomplete and such types are not marked.  */
11230   add_abstract_origin_attribute (type_die, type);
11231 }
11232
11233 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11234    include all of the information about the enumeration values also. Each
11235    enumerated type name/value is listed as a child of the enumerated type
11236    DIE.  */
11237
11238 static dw_die_ref
11239 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11240 {
11241   dw_die_ref type_die = lookup_type_die (type);
11242
11243   if (type_die == NULL)
11244     {
11245       type_die = new_die (DW_TAG_enumeration_type,
11246                           scope_die_for (type, context_die), type);
11247       equate_type_number_to_die (type, type_die);
11248       add_name_attribute (type_die, type_tag (type));
11249     }
11250   else if (! TYPE_SIZE (type))
11251     return type_die;
11252   else
11253     remove_AT (type_die, DW_AT_declaration);
11254
11255   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11256      given enum type is incomplete, do not generate the DW_AT_byte_size
11257      attribute or the DW_AT_element_list attribute.  */
11258   if (TYPE_SIZE (type))
11259     {
11260       tree link;
11261
11262       TREE_ASM_WRITTEN (type) = 1;
11263       add_byte_size_attribute (type_die, type);
11264       if (TYPE_STUB_DECL (type) != NULL_TREE)
11265         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11266
11267       /* If the first reference to this type was as the return type of an
11268          inline function, then it may not have a parent.  Fix this now.  */
11269       if (type_die->die_parent == NULL)
11270         add_child_die (scope_die_for (type, context_die), type_die);
11271
11272       for (link = TYPE_VALUES (type);
11273            link != NULL; link = TREE_CHAIN (link))
11274         {
11275           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11276           tree value = TREE_VALUE (link);
11277
11278           add_name_attribute (enum_die,
11279                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11280
11281           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11282             /* DWARF2 does not provide a way of indicating whether or
11283                not enumeration constants are signed or unsigned.  GDB
11284                always assumes the values are signed, so we output all
11285                values as if they were signed.  That means that
11286                enumeration constants with very large unsigned values
11287                will appear to have negative values in the debugger.  */
11288             add_AT_int (enum_die, DW_AT_const_value,
11289                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11290         }
11291     }
11292   else
11293     add_AT_flag (type_die, DW_AT_declaration, 1);
11294
11295   return type_die;
11296 }
11297
11298 /* Generate a DIE to represent either a real live formal parameter decl or to
11299    represent just the type of some formal parameter position in some function
11300    type.
11301
11302    Note that this routine is a bit unusual because its argument may be a
11303    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11304    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11305    node.  If it's the former then this function is being called to output a
11306    DIE to represent a formal parameter object (or some inlining thereof).  If
11307    it's the latter, then this function is only being called to output a
11308    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11309    argument type of some subprogram type.  */
11310
11311 static dw_die_ref
11312 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11313 {
11314   dw_die_ref parm_die
11315     = new_die (DW_TAG_formal_parameter, context_die, node);
11316   tree origin;
11317
11318   switch (TREE_CODE_CLASS (TREE_CODE (node)))
11319     {
11320     case tcc_declaration:
11321       origin = decl_ultimate_origin (node);
11322       if (origin != NULL)
11323         add_abstract_origin_attribute (parm_die, origin);
11324       else
11325         {
11326           add_name_and_src_coords_attributes (parm_die, node);
11327           add_type_attribute (parm_die, TREE_TYPE (node),
11328                               TREE_READONLY (node),
11329                               TREE_THIS_VOLATILE (node),
11330                               context_die);
11331           if (DECL_ARTIFICIAL (node))
11332             add_AT_flag (parm_die, DW_AT_artificial, 1);
11333         }
11334
11335       equate_decl_number_to_die (node, parm_die);
11336       if (! DECL_ABSTRACT (node))
11337         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11338
11339       break;
11340
11341     case tcc_type:
11342       /* We were called with some kind of a ..._TYPE node.  */
11343       add_type_attribute (parm_die, node, 0, 0, context_die);
11344       break;
11345
11346     default:
11347       gcc_unreachable ();
11348     }
11349
11350   return parm_die;
11351 }
11352
11353 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11354    at the end of an (ANSI prototyped) formal parameters list.  */
11355
11356 static void
11357 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11358 {
11359   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11360 }
11361
11362 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11363    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11364    parameters as specified in some function type specification (except for
11365    those which appear as part of a function *definition*).  */
11366
11367 static void
11368 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11369 {
11370   tree link;
11371   tree formal_type = NULL;
11372   tree first_parm_type;
11373   tree arg;
11374
11375   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11376     {
11377       arg = DECL_ARGUMENTS (function_or_method_type);
11378       function_or_method_type = TREE_TYPE (function_or_method_type);
11379     }
11380   else
11381     arg = NULL_TREE;
11382
11383   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11384
11385   /* Make our first pass over the list of formal parameter types and output a
11386      DW_TAG_formal_parameter DIE for each one.  */
11387   for (link = first_parm_type; link; )
11388     {
11389       dw_die_ref parm_die;
11390
11391       formal_type = TREE_VALUE (link);
11392       if (formal_type == void_type_node)
11393         break;
11394
11395       /* Output a (nameless) DIE to represent the formal parameter itself.  */
11396       parm_die = gen_formal_parameter_die (formal_type, context_die);
11397       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11398            && link == first_parm_type)
11399           || (arg && DECL_ARTIFICIAL (arg)))
11400         add_AT_flag (parm_die, DW_AT_artificial, 1);
11401
11402       link = TREE_CHAIN (link);
11403       if (arg)
11404         arg = TREE_CHAIN (arg);
11405     }
11406
11407   /* If this function type has an ellipsis, add a
11408      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
11409   if (formal_type != void_type_node)
11410     gen_unspecified_parameters_die (function_or_method_type, context_die);
11411
11412   /* Make our second (and final) pass over the list of formal parameter types
11413      and output DIEs to represent those types (as necessary).  */
11414   for (link = TYPE_ARG_TYPES (function_or_method_type);
11415        link && TREE_VALUE (link);
11416        link = TREE_CHAIN (link))
11417     gen_type_die (TREE_VALUE (link), context_die);
11418 }
11419
11420 /* We want to generate the DIE for TYPE so that we can generate the
11421    die for MEMBER, which has been defined; we will need to refer back
11422    to the member declaration nested within TYPE.  If we're trying to
11423    generate minimal debug info for TYPE, processing TYPE won't do the
11424    trick; we need to attach the member declaration by hand.  */
11425
11426 static void
11427 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11428 {
11429   gen_type_die (type, context_die);
11430
11431   /* If we're trying to avoid duplicate debug info, we may not have
11432      emitted the member decl for this function.  Emit it now.  */
11433   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11434       && ! lookup_decl_die (member))
11435     {
11436       dw_die_ref type_die;
11437       gcc_assert (!decl_ultimate_origin (member));
11438
11439       push_decl_scope (type);
11440       type_die = lookup_type_die (type);
11441       if (TREE_CODE (member) == FUNCTION_DECL)
11442         gen_subprogram_die (member, type_die);
11443       else if (TREE_CODE (member) == FIELD_DECL)
11444         {
11445           /* Ignore the nameless fields that are used to skip bits but handle
11446              C++ anonymous unions and structs.  */
11447           if (DECL_NAME (member) != NULL_TREE
11448               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11449               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11450             {
11451               gen_type_die (member_declared_type (member), type_die);
11452               gen_field_die (member, type_die);
11453             }
11454         }
11455       else
11456         gen_variable_die (member, type_die);
11457
11458       pop_decl_scope ();
11459     }
11460 }
11461
11462 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11463    may later generate inlined and/or out-of-line instances of.  */
11464
11465 static void
11466 dwarf2out_abstract_function (tree decl)
11467 {
11468   dw_die_ref old_die;
11469   tree save_fn;
11470   tree context;
11471   int was_abstract = DECL_ABSTRACT (decl);
11472
11473   /* Make sure we have the actual abstract inline, not a clone.  */
11474   decl = DECL_ORIGIN (decl);
11475
11476   old_die = lookup_decl_die (decl);
11477   if (old_die && get_AT (old_die, DW_AT_inline))
11478     /* We've already generated the abstract instance.  */
11479     return;
11480
11481   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11482      we don't get confused by DECL_ABSTRACT.  */
11483   if (debug_info_level > DINFO_LEVEL_TERSE)
11484     {
11485       context = decl_class_context (decl);
11486       if (context)
11487         gen_type_die_for_member
11488           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11489     }
11490
11491   /* Pretend we've just finished compiling this function.  */
11492   save_fn = current_function_decl;
11493   current_function_decl = decl;
11494
11495   set_decl_abstract_flags (decl, 1);
11496   dwarf2out_decl (decl);
11497   if (! was_abstract)
11498     set_decl_abstract_flags (decl, 0);
11499
11500   current_function_decl = save_fn;
11501 }
11502
11503 /* Generate a DIE to represent a declared function (either file-scope or
11504    block-local).  */
11505
11506 static void
11507 gen_subprogram_die (tree decl, dw_die_ref context_die)
11508 {
11509   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11510   tree origin = decl_ultimate_origin (decl);
11511   dw_die_ref subr_die;
11512   tree fn_arg_types;
11513   tree outer_scope;
11514   dw_die_ref old_die = lookup_decl_die (decl);
11515   int declaration = (current_function_decl != decl
11516                      || class_or_namespace_scope_p (context_die));
11517
11518   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11519      started to generate the abstract instance of an inline, decided to output
11520      its containing class, and proceeded to emit the declaration of the inline
11521      from the member list for the class.  If so, DECLARATION takes priority;
11522      we'll get back to the abstract instance when done with the class.  */
11523
11524   /* The class-scope declaration DIE must be the primary DIE.  */
11525   if (origin && declaration && class_or_namespace_scope_p (context_die))
11526     {
11527       origin = NULL;
11528       gcc_assert (!old_die);
11529     }
11530
11531   /* Now that the C++ front end lazily declares artificial member fns, we
11532      might need to retrofit the declaration into its class.  */
11533   if (!declaration && !origin && !old_die
11534       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11535       && !class_or_namespace_scope_p (context_die)
11536       && debug_info_level > DINFO_LEVEL_TERSE)
11537     old_die = force_decl_die (decl);
11538
11539   if (origin != NULL)
11540     {
11541       gcc_assert (!declaration || local_scope_p (context_die));
11542
11543       /* Fixup die_parent for the abstract instance of a nested
11544          inline function.  */
11545       if (old_die && old_die->die_parent == NULL)
11546         add_child_die (context_die, old_die);
11547
11548       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11549       add_abstract_origin_attribute (subr_die, origin);
11550     }
11551   else if (old_die)
11552     {
11553       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11554       unsigned file_index = lookup_filename (s.file);
11555
11556       if (!get_AT_flag (old_die, DW_AT_declaration)
11557           /* We can have a normal definition following an inline one in the
11558              case of redefinition of GNU C extern inlines.
11559              It seems reasonable to use AT_specification in this case.  */
11560           && !get_AT (old_die, DW_AT_inline))
11561         {
11562           /* Detect and ignore this case, where we are trying to output
11563              something we have already output.  */
11564           return;
11565         }
11566
11567       /* If the definition comes from the same place as the declaration,
11568          maybe use the old DIE.  We always want the DIE for this function
11569          that has the *_pc attributes to be under comp_unit_die so the
11570          debugger can find it.  We also need to do this for abstract
11571          instances of inlines, since the spec requires the out-of-line copy
11572          to have the same parent.  For local class methods, this doesn't
11573          apply; we just use the old DIE.  */
11574       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11575           && (DECL_ARTIFICIAL (decl)
11576               || (get_AT_unsigned (old_die, DW_AT_decl_file) == file_index
11577                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
11578                       == (unsigned) s.line))))
11579         {
11580           subr_die = old_die;
11581
11582           /* Clear out the declaration attribute and the formal parameters.
11583              Do not remove all children, because it is possible that this
11584              declaration die was forced using force_decl_die(). In such
11585              cases die that forced declaration die (e.g. TAG_imported_module)
11586              is one of the children that we do not want to remove.  */
11587           remove_AT (subr_die, DW_AT_declaration);
11588           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11589         }
11590       else
11591         {
11592           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11593           add_AT_specification (subr_die, old_die);
11594           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11595             add_AT_unsigned (subr_die, DW_AT_decl_file, file_index);
11596           if (get_AT_unsigned (old_die, DW_AT_decl_line)
11597               != (unsigned) s.line)
11598             add_AT_unsigned
11599               (subr_die, DW_AT_decl_line, s.line);
11600         }
11601     }
11602   else
11603     {
11604       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11605
11606       if (TREE_PUBLIC (decl))
11607         add_AT_flag (subr_die, DW_AT_external, 1);
11608
11609       add_name_and_src_coords_attributes (subr_die, decl);
11610       if (debug_info_level > DINFO_LEVEL_TERSE)
11611         {
11612           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11613           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11614                               0, 0, context_die);
11615         }
11616
11617       add_pure_or_virtual_attribute (subr_die, decl);
11618       if (DECL_ARTIFICIAL (decl))
11619         add_AT_flag (subr_die, DW_AT_artificial, 1);
11620
11621       if (TREE_PROTECTED (decl))
11622         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11623       else if (TREE_PRIVATE (decl))
11624         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11625     }
11626
11627   if (declaration)
11628     {
11629       if (!old_die || !get_AT (old_die, DW_AT_inline))
11630         {
11631           add_AT_flag (subr_die, DW_AT_declaration, 1);
11632
11633           /* The first time we see a member function, it is in the context of
11634              the class to which it belongs.  We make sure of this by emitting
11635              the class first.  The next time is the definition, which is
11636              handled above.  The two may come from the same source text.
11637
11638              Note that force_decl_die() forces function declaration die. It is
11639              later reused to represent definition.  */
11640           equate_decl_number_to_die (decl, subr_die);
11641         }
11642     }
11643   else if (DECL_ABSTRACT (decl))
11644     {
11645       if (DECL_DECLARED_INLINE_P (decl))
11646         {
11647           if (cgraph_function_possibly_inlined_p (decl))
11648             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11649           else
11650             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11651         }
11652       else
11653         {
11654           if (cgraph_function_possibly_inlined_p (decl))
11655             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11656           else
11657             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11658         }
11659
11660       equate_decl_number_to_die (decl, subr_die);
11661     }
11662   else if (!DECL_EXTERNAL (decl))
11663     {
11664       HOST_WIDE_INT cfa_fb_offset;
11665
11666       if (!old_die || !get_AT (old_die, DW_AT_inline))
11667         equate_decl_number_to_die (decl, subr_die);
11668
11669       if (!flag_reorder_blocks_and_partition)
11670         {
11671           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11672                                        current_function_funcdef_no);
11673           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11674           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11675                                        current_function_funcdef_no);
11676           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11677           
11678           add_pubname (decl, subr_die);
11679           add_arange (decl, subr_die);
11680         }
11681       else
11682         {  /* Do nothing for now; maybe need to duplicate die, one for
11683               hot section and ond for cold section, then use the hot/cold
11684               section begin/end labels to generate the aranges...  */
11685           /*
11686             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11687             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11688             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11689             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11690
11691             add_pubname (decl, subr_die);
11692             add_arange (decl, subr_die);
11693             add_arange (decl, subr_die);
11694            */
11695         }
11696
11697 #ifdef MIPS_DEBUGGING_INFO
11698       /* Add a reference to the FDE for this routine.  */
11699       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11700 #endif
11701
11702       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
11703
11704       /* We define the "frame base" as the function's CFA.  This is more
11705          convenient for several reasons: (1) It's stable across the prologue
11706          and epilogue, which makes it better than just a frame pointer,
11707          (2) With dwarf3, there exists a one-byte encoding that allows us
11708          to reference the .debug_frame data by proxy, but failing that,
11709          (3) We can at least reuse the code inspection and interpretation
11710          code that determines the CFA position at various points in the
11711          function.  */
11712       /* ??? Use some command-line or configury switch to enable the use
11713          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11714          consumers that understand it; fall back to "pure" dwarf2 and
11715          convert the CFA data into a location list.  */
11716       {
11717         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11718         if (list->dw_loc_next)
11719           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
11720         else
11721           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
11722       }
11723
11724       /* Compute a displacement from the "steady-state frame pointer" to
11725          the CFA.  The former is what all stack slots and argument slots
11726          will reference in the rtl; the later is what we've told the 
11727          debugger about.  We'll need to adjust all frame_base references
11728          by this displacement.  */
11729       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
11730
11731       if (cfun->static_chain_decl)
11732         add_AT_location_description (subr_die, DW_AT_static_link,
11733                  loc_descriptor_from_tree (cfun->static_chain_decl));
11734     }
11735
11736   /* Now output descriptions of the arguments for this function. This gets
11737      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
11738      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
11739      `...' at the end of the formal parameter list.  In order to find out if
11740      there was a trailing ellipsis or not, we must instead look at the type
11741      associated with the FUNCTION_DECL.  This will be a node of type
11742      FUNCTION_TYPE. If the chain of type nodes hanging off of this
11743      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
11744      an ellipsis at the end.  */
11745
11746   /* In the case where we are describing a mere function declaration, all we
11747      need to do here (and all we *can* do here) is to describe the *types* of
11748      its formal parameters.  */
11749   if (debug_info_level <= DINFO_LEVEL_TERSE)
11750     ;
11751   else if (declaration)
11752     gen_formal_types_die (decl, subr_die);
11753   else
11754     {
11755       /* Generate DIEs to represent all known formal parameters.  */
11756       tree arg_decls = DECL_ARGUMENTS (decl);
11757       tree parm;
11758
11759       /* When generating DIEs, generate the unspecified_parameters DIE
11760          instead if we come across the arg "__builtin_va_alist" */
11761       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
11762         if (TREE_CODE (parm) == PARM_DECL)
11763           {
11764             if (DECL_NAME (parm)
11765                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
11766                             "__builtin_va_alist"))
11767               gen_unspecified_parameters_die (parm, subr_die);
11768             else
11769               gen_decl_die (parm, subr_die);
11770           }
11771
11772       /* Decide whether we need an unspecified_parameters DIE at the end.
11773          There are 2 more cases to do this for: 1) the ansi ... declaration -
11774          this is detectable when the end of the arg list is not a
11775          void_type_node 2) an unprototyped function declaration (not a
11776          definition).  This just means that we have no info about the
11777          parameters at all.  */
11778       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
11779       if (fn_arg_types != NULL)
11780         {
11781           /* This is the prototyped case, check for....  */
11782           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
11783             gen_unspecified_parameters_die (decl, subr_die);
11784         }
11785       else if (DECL_INITIAL (decl) == NULL_TREE)
11786         gen_unspecified_parameters_die (decl, subr_die);
11787     }
11788
11789   /* Output Dwarf info for all of the stuff within the body of the function
11790      (if it has one - it may be just a declaration).  */
11791   outer_scope = DECL_INITIAL (decl);
11792
11793   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
11794      a function.  This BLOCK actually represents the outermost binding contour
11795      for the function, i.e. the contour in which the function's formal
11796      parameters and labels get declared. Curiously, it appears that the front
11797      end doesn't actually put the PARM_DECL nodes for the current function onto
11798      the BLOCK_VARS list for this outer scope, but are strung off of the
11799      DECL_ARGUMENTS list for the function instead.
11800
11801      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
11802      the LABEL_DECL nodes for the function however, and we output DWARF info
11803      for those in decls_for_scope.  Just within the `outer_scope' there will be
11804      a BLOCK node representing the function's outermost pair of curly braces,
11805      and any blocks used for the base and member initializers of a C++
11806      constructor function.  */
11807   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
11808     {
11809       /* Emit a DW_TAG_variable DIE for a named return value.  */
11810       if (DECL_NAME (DECL_RESULT (decl)))
11811         gen_decl_die (DECL_RESULT (decl), subr_die);
11812
11813       current_function_has_inlines = 0;
11814       decls_for_scope (outer_scope, subr_die, 0);
11815
11816 #if 0 && defined (MIPS_DEBUGGING_INFO)
11817       if (current_function_has_inlines)
11818         {
11819           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
11820           if (! comp_unit_has_inlines)
11821             {
11822               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
11823               comp_unit_has_inlines = 1;
11824             }
11825         }
11826 #endif
11827     }
11828   /* Add the calling convention attribute if requested.  */
11829   add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
11830
11831 }
11832
11833 /* Generate a DIE to represent a declared data object.  */
11834
11835 static void
11836 gen_variable_die (tree decl, dw_die_ref context_die)
11837 {
11838   tree origin = decl_ultimate_origin (decl);
11839   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
11840
11841   dw_die_ref old_die = lookup_decl_die (decl);
11842   int declaration = (DECL_EXTERNAL (decl)
11843                      /* If DECL is COMDAT and has not actually been
11844                         emitted, we cannot take its address; there
11845                         might end up being no definition anywhere in
11846                         the program.  For example, consider the C++
11847                         test case:
11848
11849                           template <class T>
11850                           struct S { static const int i = 7; };
11851
11852                           template <class T>
11853                           const int S<T>::i;
11854
11855                           int f() { return S<int>::i; }
11856                           
11857                         Here, S<int>::i is not DECL_EXTERNAL, but no
11858                         definition is required, so the compiler will
11859                         not emit a definition.  */  
11860                      || (TREE_CODE (decl) == VAR_DECL
11861                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
11862                      || class_or_namespace_scope_p (context_die));
11863
11864   if (origin != NULL)
11865     add_abstract_origin_attribute (var_die, origin);
11866
11867   /* Loop unrolling can create multiple blocks that refer to the same
11868      static variable, so we must test for the DW_AT_declaration flag.
11869
11870      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
11871      copy decls and set the DECL_ABSTRACT flag on them instead of
11872      sharing them.
11873
11874      ??? Duplicated blocks have been rewritten to use .debug_ranges.
11875
11876      ??? The declare_in_namespace support causes us to get two DIEs for one
11877      variable, both of which are declarations.  We want to avoid considering
11878      one to be a specification, so we must test that this DIE is not a
11879      declaration.  */
11880   else if (old_die && TREE_STATIC (decl) && ! declaration
11881            && get_AT_flag (old_die, DW_AT_declaration) == 1)
11882     {
11883       /* This is a definition of a C++ class level static.  */
11884       add_AT_specification (var_die, old_die);
11885       if (DECL_NAME (decl))
11886         {
11887           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11888           unsigned file_index = lookup_filename (s.file);
11889
11890           if (get_AT_unsigned (old_die, DW_AT_decl_file) != file_index)
11891             add_AT_unsigned (var_die, DW_AT_decl_file, file_index);
11892
11893           if (get_AT_unsigned (old_die, DW_AT_decl_line)
11894               != (unsigned) s.line)
11895
11896             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
11897         }
11898     }
11899   else
11900     {
11901       add_name_and_src_coords_attributes (var_die, decl);
11902       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
11903                           TREE_THIS_VOLATILE (decl), context_die);
11904
11905       if (TREE_PUBLIC (decl))
11906         add_AT_flag (var_die, DW_AT_external, 1);
11907
11908       if (DECL_ARTIFICIAL (decl))
11909         add_AT_flag (var_die, DW_AT_artificial, 1);
11910
11911       if (TREE_PROTECTED (decl))
11912         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
11913       else if (TREE_PRIVATE (decl))
11914         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
11915     }
11916
11917   if (declaration)
11918     add_AT_flag (var_die, DW_AT_declaration, 1);
11919
11920   if (DECL_ABSTRACT (decl) || declaration)
11921     equate_decl_number_to_die (decl, var_die);
11922
11923   if (! declaration && ! DECL_ABSTRACT (decl))
11924     {
11925       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
11926       add_pubname (decl, var_die);
11927     }
11928   else
11929     tree_add_const_value_attribute (var_die, decl);
11930 }
11931
11932 /* Generate a DIE to represent a label identifier.  */
11933
11934 static void
11935 gen_label_die (tree decl, dw_die_ref context_die)
11936 {
11937   tree origin = decl_ultimate_origin (decl);
11938   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
11939   rtx insn;
11940   char label[MAX_ARTIFICIAL_LABEL_BYTES];
11941
11942   if (origin != NULL)
11943     add_abstract_origin_attribute (lbl_die, origin);
11944   else
11945     add_name_and_src_coords_attributes (lbl_die, decl);
11946
11947   if (DECL_ABSTRACT (decl))
11948     equate_decl_number_to_die (decl, lbl_die);
11949   else
11950     {
11951       insn = DECL_RTL_IF_SET (decl);
11952
11953       /* Deleted labels are programmer specified labels which have been
11954          eliminated because of various optimizations.  We still emit them
11955          here so that it is possible to put breakpoints on them.  */
11956       if (insn
11957           && (LABEL_P (insn)
11958               || ((NOTE_P (insn)
11959                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))))
11960         {
11961           /* When optimization is enabled (via -O) some parts of the compiler
11962              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
11963              represent source-level labels which were explicitly declared by
11964              the user.  This really shouldn't be happening though, so catch
11965              it if it ever does happen.  */
11966           gcc_assert (!INSN_DELETED_P (insn));
11967
11968           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
11969           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
11970         }
11971     }
11972 }
11973
11974 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
11975    attributes to the DIE for a block STMT, to describe where the inlined
11976    function was called from.  This is similar to add_src_coords_attributes.  */
11977
11978 static inline void
11979 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
11980 {
11981   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
11982   unsigned file_index = lookup_filename (s.file);
11983
11984   add_AT_unsigned (die, DW_AT_call_file, file_index);
11985   add_AT_unsigned (die, DW_AT_call_line, s.line);
11986 }
11987
11988 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
11989    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
11990
11991 static inline void
11992 add_high_low_attributes (tree stmt, dw_die_ref die)
11993 {
11994   char label[MAX_ARTIFICIAL_LABEL_BYTES];
11995
11996   if (BLOCK_FRAGMENT_CHAIN (stmt))
11997     {
11998       tree chain;
11999
12000       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12001
12002       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12003       do
12004         {
12005           add_ranges (chain);
12006           chain = BLOCK_FRAGMENT_CHAIN (chain);
12007         }
12008       while (chain);
12009       add_ranges (NULL);
12010     }
12011   else
12012     {
12013       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12014                                    BLOCK_NUMBER (stmt));
12015       add_AT_lbl_id (die, DW_AT_low_pc, label);
12016       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12017                                    BLOCK_NUMBER (stmt));
12018       add_AT_lbl_id (die, DW_AT_high_pc, label);
12019     }
12020 }
12021
12022 /* Generate a DIE for a lexical block.  */
12023
12024 static void
12025 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12026 {
12027   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12028
12029   if (! BLOCK_ABSTRACT (stmt))
12030     add_high_low_attributes (stmt, stmt_die);
12031
12032   decls_for_scope (stmt, stmt_die, depth);
12033 }
12034
12035 /* Generate a DIE for an inlined subprogram.  */
12036
12037 static void
12038 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12039 {
12040   tree decl = block_ultimate_origin (stmt);
12041
12042   /* Emit info for the abstract instance first, if we haven't yet.  We
12043      must emit this even if the block is abstract, otherwise when we
12044      emit the block below (or elsewhere), we may end up trying to emit
12045      a die whose origin die hasn't been emitted, and crashing.  */
12046   dwarf2out_abstract_function (decl);
12047
12048   if (! BLOCK_ABSTRACT (stmt))
12049     {
12050       dw_die_ref subr_die
12051         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12052
12053       add_abstract_origin_attribute (subr_die, decl);
12054       add_high_low_attributes (stmt, subr_die);
12055       add_call_src_coords_attributes (stmt, subr_die);
12056
12057       decls_for_scope (stmt, subr_die, depth);
12058       current_function_has_inlines = 1;
12059     }
12060   else
12061     /* We may get here if we're the outer block of function A that was
12062        inlined into function B that was inlined into function C.  When
12063        generating debugging info for C, dwarf2out_abstract_function(B)
12064        would mark all inlined blocks as abstract, including this one.
12065        So, we wouldn't (and shouldn't) expect labels to be generated
12066        for this one.  Instead, just emit debugging info for
12067        declarations within the block.  This is particularly important
12068        in the case of initializers of arguments passed from B to us:
12069        if they're statement expressions containing declarations, we
12070        wouldn't generate dies for their abstract variables, and then,
12071        when generating dies for the real variables, we'd die (pun
12072        intended :-)  */
12073     gen_lexical_block_die (stmt, context_die, depth);
12074 }
12075
12076 /* Generate a DIE for a field in a record, or structure.  */
12077
12078 static void
12079 gen_field_die (tree decl, dw_die_ref context_die)
12080 {
12081   dw_die_ref decl_die;
12082
12083   if (TREE_TYPE (decl) == error_mark_node)
12084     return;
12085
12086   decl_die = new_die (DW_TAG_member, context_die, decl);
12087   add_name_and_src_coords_attributes (decl_die, decl);
12088   add_type_attribute (decl_die, member_declared_type (decl),
12089                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12090                       context_die);
12091
12092   if (DECL_BIT_FIELD_TYPE (decl))
12093     {
12094       add_byte_size_attribute (decl_die, decl);
12095       add_bit_size_attribute (decl_die, decl);
12096       add_bit_offset_attribute (decl_die, decl);
12097     }
12098
12099   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12100     add_data_member_location_attribute (decl_die, decl);
12101
12102   if (DECL_ARTIFICIAL (decl))
12103     add_AT_flag (decl_die, DW_AT_artificial, 1);
12104
12105   if (TREE_PROTECTED (decl))
12106     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12107   else if (TREE_PRIVATE (decl))
12108     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12109
12110   /* Equate decl number to die, so that we can look up this decl later on.  */
12111   equate_decl_number_to_die (decl, decl_die);
12112 }
12113
12114 #if 0
12115 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12116    Use modified_type_die instead.
12117    We keep this code here just in case these types of DIEs may be needed to
12118    represent certain things in other languages (e.g. Pascal) someday.  */
12119
12120 static void
12121 gen_pointer_type_die (tree type, dw_die_ref context_die)
12122 {
12123   dw_die_ref ptr_die
12124     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12125
12126   equate_type_number_to_die (type, ptr_die);
12127   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12128   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12129 }
12130
12131 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12132    Use modified_type_die instead.
12133    We keep this code here just in case these types of DIEs may be needed to
12134    represent certain things in other languages (e.g. Pascal) someday.  */
12135
12136 static void
12137 gen_reference_type_die (tree type, dw_die_ref context_die)
12138 {
12139   dw_die_ref ref_die
12140     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12141
12142   equate_type_number_to_die (type, ref_die);
12143   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12144   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12145 }
12146 #endif
12147
12148 /* Generate a DIE for a pointer to a member type.  */
12149
12150 static void
12151 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12152 {
12153   dw_die_ref ptr_die
12154     = new_die (DW_TAG_ptr_to_member_type,
12155                scope_die_for (type, context_die), type);
12156
12157   equate_type_number_to_die (type, ptr_die);
12158   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12159                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12160   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12161 }
12162
12163 /* Generate the DIE for the compilation unit.  */
12164
12165 static dw_die_ref
12166 gen_compile_unit_die (const char *filename)
12167 {
12168   dw_die_ref die;
12169   char producer[250];
12170   const char *language_string = lang_hooks.name;
12171   int language;
12172
12173   die = new_die (DW_TAG_compile_unit, NULL, NULL);
12174
12175   if (filename)
12176     {
12177       add_name_attribute (die, filename);
12178       /* Don't add cwd for <built-in>.  */
12179       if (filename[0] != DIR_SEPARATOR && filename[0] != '<')
12180         add_comp_dir_attribute (die);
12181     }
12182
12183   sprintf (producer, "%s %s", language_string, version_string);
12184
12185 #ifdef MIPS_DEBUGGING_INFO
12186   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12187      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12188      not appear in the producer string, the debugger reaches the conclusion
12189      that the object file is stripped and has no debugging information.
12190      To get the MIPS/SGI debugger to believe that there is debugging
12191      information in the object file, we add a -g to the producer string.  */
12192   if (debug_info_level > DINFO_LEVEL_TERSE)
12193     strcat (producer, " -g");
12194 #endif
12195
12196   add_AT_string (die, DW_AT_producer, producer);
12197
12198   if (strcmp (language_string, "GNU C++") == 0)
12199     language = DW_LANG_C_plus_plus;
12200   else if (strcmp (language_string, "GNU Ada") == 0)
12201     language = DW_LANG_Ada95;
12202   else if (strcmp (language_string, "GNU F77") == 0)
12203     language = DW_LANG_Fortran77;
12204   else if (strcmp (language_string, "GNU F95") == 0)
12205     language = DW_LANG_Fortran95;
12206   else if (strcmp (language_string, "GNU Pascal") == 0)
12207     language = DW_LANG_Pascal83;
12208   else if (strcmp (language_string, "GNU Java") == 0)
12209     language = DW_LANG_Java;
12210   else
12211     language = DW_LANG_C89;
12212
12213   add_AT_unsigned (die, DW_AT_language, language);
12214   return die;
12215 }
12216
12217 /* Generate the DIE for a base class.  */
12218
12219 static void
12220 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12221 {
12222   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12223
12224   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12225   add_data_member_location_attribute (die, binfo);
12226
12227   if (BINFO_VIRTUAL_P (binfo))
12228     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12229
12230   if (access == access_public_node)
12231     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12232   else if (access == access_protected_node)
12233     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12234 }
12235
12236 /* Generate a DIE for a class member.  */
12237
12238 static void
12239 gen_member_die (tree type, dw_die_ref context_die)
12240 {
12241   tree member;
12242   tree binfo = TYPE_BINFO (type);
12243   dw_die_ref child;
12244
12245   /* If this is not an incomplete type, output descriptions of each of its
12246      members. Note that as we output the DIEs necessary to represent the
12247      members of this record or union type, we will also be trying to output
12248      DIEs to represent the *types* of those members. However the `type'
12249      function (above) will specifically avoid generating type DIEs for member
12250      types *within* the list of member DIEs for this (containing) type except
12251      for those types (of members) which are explicitly marked as also being
12252      members of this (containing) type themselves.  The g++ front- end can
12253      force any given type to be treated as a member of some other (containing)
12254      type by setting the TYPE_CONTEXT of the given (member) type to point to
12255      the TREE node representing the appropriate (containing) type.  */
12256
12257   /* First output info about the base classes.  */
12258   if (binfo)
12259     {
12260       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12261       int i;
12262       tree base;
12263
12264       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12265         gen_inheritance_die (base,
12266                              (accesses ? VEC_index (tree, accesses, i)
12267                               : access_public_node), context_die);
12268     }
12269
12270   /* Now output info about the data members and type members.  */
12271   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12272     {
12273       /* If we thought we were generating minimal debug info for TYPE
12274          and then changed our minds, some of the member declarations
12275          may have already been defined.  Don't define them again, but
12276          do put them in the right order.  */
12277
12278       child = lookup_decl_die (member);
12279       if (child)
12280         splice_child_die (context_die, child);
12281       else
12282         gen_decl_die (member, context_die);
12283     }
12284
12285   /* Now output info about the function members (if any).  */
12286   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12287     {
12288       /* Don't include clones in the member list.  */
12289       if (DECL_ABSTRACT_ORIGIN (member))
12290         continue;
12291
12292       child = lookup_decl_die (member);
12293       if (child)
12294         splice_child_die (context_die, child);
12295       else
12296         gen_decl_die (member, context_die);
12297     }
12298 }
12299
12300 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
12301    is set, we pretend that the type was never defined, so we only get the
12302    member DIEs needed by later specification DIEs.  */
12303
12304 static void
12305 gen_struct_or_union_type_die (tree type, dw_die_ref context_die)
12306 {
12307   dw_die_ref type_die = lookup_type_die (type);
12308   dw_die_ref scope_die = 0;
12309   int nested = 0;
12310   int complete = (TYPE_SIZE (type)
12311                   && (! TYPE_STUB_DECL (type)
12312                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12313   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12314
12315   if (type_die && ! complete)
12316     return;
12317
12318   if (TYPE_CONTEXT (type) != NULL_TREE
12319       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12320           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12321     nested = 1;
12322
12323   scope_die = scope_die_for (type, context_die);
12324
12325   if (! type_die || (nested && scope_die == comp_unit_die))
12326     /* First occurrence of type or toplevel definition of nested class.  */
12327     {
12328       dw_die_ref old_die = type_die;
12329
12330       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12331                           ? DW_TAG_structure_type : DW_TAG_union_type,
12332                           scope_die, type);
12333       equate_type_number_to_die (type, type_die);
12334       if (old_die)
12335         add_AT_specification (type_die, old_die);
12336       else
12337         add_name_attribute (type_die, type_tag (type));
12338     }
12339   else
12340     remove_AT (type_die, DW_AT_declaration);
12341
12342   /* If this type has been completed, then give it a byte_size attribute and
12343      then give a list of members.  */
12344   if (complete && !ns_decl)
12345     {
12346       /* Prevent infinite recursion in cases where the type of some member of
12347          this type is expressed in terms of this type itself.  */
12348       TREE_ASM_WRITTEN (type) = 1;
12349       add_byte_size_attribute (type_die, type);
12350       if (TYPE_STUB_DECL (type) != NULL_TREE)
12351         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12352
12353       /* If the first reference to this type was as the return type of an
12354          inline function, then it may not have a parent.  Fix this now.  */
12355       if (type_die->die_parent == NULL)
12356         add_child_die (scope_die, type_die);
12357
12358       push_decl_scope (type);
12359       gen_member_die (type, type_die);
12360       pop_decl_scope ();
12361
12362       /* GNU extension: Record what type our vtable lives in.  */
12363       if (TYPE_VFIELD (type))
12364         {
12365           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12366
12367           gen_type_die (vtype, context_die);
12368           add_AT_die_ref (type_die, DW_AT_containing_type,
12369                           lookup_type_die (vtype));
12370         }
12371     }
12372   else
12373     {
12374       add_AT_flag (type_die, DW_AT_declaration, 1);
12375
12376       /* We don't need to do this for function-local types.  */
12377       if (TYPE_STUB_DECL (type)
12378           && ! decl_function_context (TYPE_STUB_DECL (type)))
12379         VEC_safe_push (tree, gc, incomplete_types, type);
12380     }
12381 }
12382
12383 /* Generate a DIE for a subroutine _type_.  */
12384
12385 static void
12386 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12387 {
12388   tree return_type = TREE_TYPE (type);
12389   dw_die_ref subr_die
12390     = new_die (DW_TAG_subroutine_type,
12391                scope_die_for (type, context_die), type);
12392
12393   equate_type_number_to_die (type, subr_die);
12394   add_prototyped_attribute (subr_die, type);
12395   add_type_attribute (subr_die, return_type, 0, 0, context_die);
12396   gen_formal_types_die (type, subr_die);
12397 }
12398
12399 /* Generate a DIE for a type definition.  */
12400
12401 static void
12402 gen_typedef_die (tree decl, dw_die_ref context_die)
12403 {
12404   dw_die_ref type_die;
12405   tree origin;
12406
12407   if (TREE_ASM_WRITTEN (decl))
12408     return;
12409
12410   TREE_ASM_WRITTEN (decl) = 1;
12411   type_die = new_die (DW_TAG_typedef, context_die, decl);
12412   origin = decl_ultimate_origin (decl);
12413   if (origin != NULL)
12414     add_abstract_origin_attribute (type_die, origin);
12415   else
12416     {
12417       tree type;
12418
12419       add_name_and_src_coords_attributes (type_die, decl);
12420       if (DECL_ORIGINAL_TYPE (decl))
12421         {
12422           type = DECL_ORIGINAL_TYPE (decl);
12423
12424           gcc_assert (type != TREE_TYPE (decl));
12425           equate_type_number_to_die (TREE_TYPE (decl), type_die);
12426         }
12427       else
12428         type = TREE_TYPE (decl);
12429
12430       add_type_attribute (type_die, type, TREE_READONLY (decl),
12431                           TREE_THIS_VOLATILE (decl), context_die);
12432     }
12433
12434   if (DECL_ABSTRACT (decl))
12435     equate_decl_number_to_die (decl, type_die);
12436 }
12437
12438 /* Generate a type description DIE.  */
12439
12440 static void
12441 gen_type_die (tree type, dw_die_ref context_die)
12442 {
12443   int need_pop;
12444
12445   if (type == NULL_TREE || type == error_mark_node)
12446     return;
12447
12448   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12449       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12450     {
12451       if (TREE_ASM_WRITTEN (type))
12452         return;
12453
12454       /* Prevent broken recursion; we can't hand off to the same type.  */
12455       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12456
12457       TREE_ASM_WRITTEN (type) = 1;
12458       gen_decl_die (TYPE_NAME (type), context_die);
12459       return;
12460     }
12461
12462   /* We are going to output a DIE to represent the unqualified version
12463      of this type (i.e. without any const or volatile qualifiers) so
12464      get the main variant (i.e. the unqualified version) of this type
12465      now.  (Vectors are special because the debugging info is in the
12466      cloned type itself).  */
12467   if (TREE_CODE (type) != VECTOR_TYPE)
12468     type = type_main_variant (type);
12469
12470   if (TREE_ASM_WRITTEN (type))
12471     return;
12472
12473   switch (TREE_CODE (type))
12474     {
12475     case ERROR_MARK:
12476       break;
12477
12478     case POINTER_TYPE:
12479     case REFERENCE_TYPE:
12480       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
12481          ensures that the gen_type_die recursion will terminate even if the
12482          type is recursive.  Recursive types are possible in Ada.  */
12483       /* ??? We could perhaps do this for all types before the switch
12484          statement.  */
12485       TREE_ASM_WRITTEN (type) = 1;
12486
12487       /* For these types, all that is required is that we output a DIE (or a
12488          set of DIEs) to represent the "basis" type.  */
12489       gen_type_die (TREE_TYPE (type), context_die);
12490       break;
12491
12492     case OFFSET_TYPE:
12493       /* This code is used for C++ pointer-to-data-member types.
12494          Output a description of the relevant class type.  */
12495       gen_type_die (TYPE_OFFSET_BASETYPE (type), context_die);
12496
12497       /* Output a description of the type of the object pointed to.  */
12498       gen_type_die (TREE_TYPE (type), context_die);
12499
12500       /* Now output a DIE to represent this pointer-to-data-member type
12501          itself.  */
12502       gen_ptr_to_mbr_type_die (type, context_die);
12503       break;
12504
12505     case FUNCTION_TYPE:
12506       /* Force out return type (in case it wasn't forced out already).  */
12507       gen_type_die (TREE_TYPE (type), context_die);
12508       gen_subroutine_type_die (type, context_die);
12509       break;
12510
12511     case METHOD_TYPE:
12512       /* Force out return type (in case it wasn't forced out already).  */
12513       gen_type_die (TREE_TYPE (type), context_die);
12514       gen_subroutine_type_die (type, context_die);
12515       break;
12516
12517     case ARRAY_TYPE:
12518       gen_array_type_die (type, context_die);
12519       break;
12520
12521     case VECTOR_TYPE:
12522       gen_array_type_die (type, context_die);
12523       break;
12524
12525     case ENUMERAL_TYPE:
12526     case RECORD_TYPE:
12527     case UNION_TYPE:
12528     case QUAL_UNION_TYPE:
12529       /* If this is a nested type whose containing class hasn't been written
12530          out yet, writing it out will cover this one, too.  This does not apply
12531          to instantiations of member class templates; they need to be added to
12532          the containing class as they are generated.  FIXME: This hurts the
12533          idea of combining type decls from multiple TUs, since we can't predict
12534          what set of template instantiations we'll get.  */
12535       if (TYPE_CONTEXT (type)
12536           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12537           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12538         {
12539           gen_type_die (TYPE_CONTEXT (type), context_die);
12540
12541           if (TREE_ASM_WRITTEN (type))
12542             return;
12543
12544           /* If that failed, attach ourselves to the stub.  */
12545           push_decl_scope (TYPE_CONTEXT (type));
12546           context_die = lookup_type_die (TYPE_CONTEXT (type));
12547           need_pop = 1;
12548         }
12549       else
12550         {
12551           declare_in_namespace (type, context_die);
12552           need_pop = 0;
12553         }
12554
12555       if (TREE_CODE (type) == ENUMERAL_TYPE)
12556         gen_enumeration_type_die (type, context_die);
12557       else
12558         gen_struct_or_union_type_die (type, context_die);
12559
12560       if (need_pop)
12561         pop_decl_scope ();
12562
12563       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12564          it up if it is ever completed.  gen_*_type_die will set it for us
12565          when appropriate.  */
12566       return;
12567
12568     case VOID_TYPE:
12569     case INTEGER_TYPE:
12570     case REAL_TYPE:
12571     case COMPLEX_TYPE:
12572     case BOOLEAN_TYPE:
12573       /* No DIEs needed for fundamental types.  */
12574       break;
12575
12576     case LANG_TYPE:
12577       /* No Dwarf representation currently defined.  */
12578       break;
12579
12580     default:
12581       gcc_unreachable ();
12582     }
12583
12584   TREE_ASM_WRITTEN (type) = 1;
12585 }
12586
12587 /* Generate a DIE for a tagged type instantiation.  */
12588
12589 static void
12590 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12591 {
12592   if (type == NULL_TREE || type == error_mark_node)
12593     return;
12594
12595   /* We are going to output a DIE to represent the unqualified version of
12596      this type (i.e. without any const or volatile qualifiers) so make sure
12597      that we have the main variant (i.e. the unqualified version) of this
12598      type now.  */
12599   gcc_assert (type == type_main_variant (type));
12600
12601   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12602      an instance of an unresolved type.  */
12603
12604   switch (TREE_CODE (type))
12605     {
12606     case ERROR_MARK:
12607       break;
12608
12609     case ENUMERAL_TYPE:
12610       gen_inlined_enumeration_type_die (type, context_die);
12611       break;
12612
12613     case RECORD_TYPE:
12614       gen_inlined_structure_type_die (type, context_die);
12615       break;
12616
12617     case UNION_TYPE:
12618     case QUAL_UNION_TYPE:
12619       gen_inlined_union_type_die (type, context_die);
12620       break;
12621
12622     default:
12623       gcc_unreachable ();
12624     }
12625 }
12626
12627 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12628    things which are local to the given block.  */
12629
12630 static void
12631 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12632 {
12633   int must_output_die = 0;
12634   tree origin;
12635   tree decl;
12636   enum tree_code origin_code;
12637
12638   /* Ignore blocks that are NULL.  */
12639   if (stmt == NULL_TREE)
12640     return;
12641
12642   /* If the block is one fragment of a non-contiguous block, do not
12643      process the variables, since they will have been done by the
12644      origin block.  Do process subblocks.  */
12645   if (BLOCK_FRAGMENT_ORIGIN (stmt))
12646     {
12647       tree sub;
12648
12649       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12650         gen_block_die (sub, context_die, depth + 1);
12651
12652       return;
12653     }
12654
12655   /* Determine the "ultimate origin" of this block.  This block may be an
12656      inlined instance of an inlined instance of inline function, so we have
12657      to trace all of the way back through the origin chain to find out what
12658      sort of node actually served as the original seed for the creation of
12659      the current block.  */
12660   origin = block_ultimate_origin (stmt);
12661   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
12662
12663   /* Determine if we need to output any Dwarf DIEs at all to represent this
12664      block.  */
12665   if (origin_code == FUNCTION_DECL)
12666     /* The outer scopes for inlinings *must* always be represented.  We
12667        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
12668     must_output_die = 1;
12669   else
12670     {
12671       /* In the case where the current block represents an inlining of the
12672          "body block" of an inline function, we must *NOT* output any DIE for
12673          this block because we have already output a DIE to represent the whole
12674          inlined function scope and the "body block" of any function doesn't
12675          really represent a different scope according to ANSI C rules.  So we
12676          check here to make sure that this block does not represent a "body
12677          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
12678       if (! is_body_block (origin ? origin : stmt))
12679         {
12680           /* Determine if this block directly contains any "significant"
12681              local declarations which we will need to output DIEs for.  */
12682           if (debug_info_level > DINFO_LEVEL_TERSE)
12683             /* We are not in terse mode so *any* local declaration counts
12684                as being a "significant" one.  */
12685             must_output_die = (BLOCK_VARS (stmt) != NULL 
12686                                && (TREE_USED (stmt) 
12687                                    || TREE_ASM_WRITTEN (stmt)
12688                                    || BLOCK_ABSTRACT (stmt)));
12689           else
12690             /* We are in terse mode, so only local (nested) function
12691                definitions count as "significant" local declarations.  */
12692             for (decl = BLOCK_VARS (stmt);
12693                  decl != NULL; decl = TREE_CHAIN (decl))
12694               if (TREE_CODE (decl) == FUNCTION_DECL
12695                   && DECL_INITIAL (decl))
12696                 {
12697                   must_output_die = 1;
12698                   break;
12699                 }
12700         }
12701     }
12702
12703   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
12704      DIE for any block which contains no significant local declarations at
12705      all.  Rather, in such cases we just call `decls_for_scope' so that any
12706      needed Dwarf info for any sub-blocks will get properly generated. Note
12707      that in terse mode, our definition of what constitutes a "significant"
12708      local declaration gets restricted to include only inlined function
12709      instances and local (nested) function definitions.  */
12710   if (must_output_die)
12711     {
12712       if (origin_code == FUNCTION_DECL)
12713         gen_inlined_subroutine_die (stmt, context_die, depth);
12714       else
12715         gen_lexical_block_die (stmt, context_die, depth);
12716     }
12717   else
12718     decls_for_scope (stmt, context_die, depth);
12719 }
12720
12721 /* Generate all of the decls declared within a given scope and (recursively)
12722    all of its sub-blocks.  */
12723
12724 static void
12725 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
12726 {
12727   tree decl;
12728   tree subblocks;
12729
12730   /* Ignore NULL blocks.  */
12731   if (stmt == NULL_TREE)
12732     return;
12733
12734   if (TREE_USED (stmt))
12735     {
12736       /* Output the DIEs to represent all of the data objects and typedefs
12737          declared directly within this block but not within any nested
12738          sub-blocks.  Also, nested function and tag DIEs have been
12739          generated with a parent of NULL; fix that up now.  */
12740       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
12741         {
12742           dw_die_ref die;
12743           
12744           if (TREE_CODE (decl) == FUNCTION_DECL)
12745             die = lookup_decl_die (decl);
12746           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
12747             die = lookup_type_die (TREE_TYPE (decl));
12748           else
12749             die = NULL;
12750           
12751           if (die != NULL && die->die_parent == NULL)
12752             add_child_die (context_die, die);
12753           /* Do not produce debug information for static variables since
12754              these might be optimized out.  We are called for these later
12755              in cgraph_varpool_analyze_pending_decls. */
12756           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
12757             ;
12758           else
12759             gen_decl_die (decl, context_die);
12760         }
12761     }
12762
12763   /* If we're at -g1, we're not interested in subblocks.  */
12764   if (debug_info_level <= DINFO_LEVEL_TERSE)
12765     return;
12766
12767   /* Output the DIEs to represent all sub-blocks (and the items declared
12768      therein) of this block.  */
12769   for (subblocks = BLOCK_SUBBLOCKS (stmt);
12770        subblocks != NULL;
12771        subblocks = BLOCK_CHAIN (subblocks))
12772     gen_block_die (subblocks, context_die, depth + 1);
12773 }
12774
12775 /* Is this a typedef we can avoid emitting?  */
12776
12777 static inline int
12778 is_redundant_typedef (tree decl)
12779 {
12780   if (TYPE_DECL_IS_STUB (decl))
12781     return 1;
12782
12783   if (DECL_ARTIFICIAL (decl)
12784       && DECL_CONTEXT (decl)
12785       && is_tagged_type (DECL_CONTEXT (decl))
12786       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
12787       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
12788     /* Also ignore the artificial member typedef for the class name.  */
12789     return 1;
12790
12791   return 0;
12792 }
12793
12794 /* Returns the DIE for decl.  A DIE will always be returned.  */
12795
12796 static dw_die_ref
12797 force_decl_die (tree decl)
12798 {
12799   dw_die_ref decl_die;
12800   unsigned saved_external_flag;
12801   tree save_fn = NULL_TREE;
12802   decl_die = lookup_decl_die (decl);
12803   if (!decl_die)
12804     {
12805       dw_die_ref context_die;
12806       tree decl_context = DECL_CONTEXT (decl);
12807       if (decl_context)
12808         {
12809           /* Find die that represents this context.  */
12810           if (TYPE_P (decl_context))
12811             context_die = force_type_die (decl_context);
12812           else
12813             context_die = force_decl_die (decl_context);
12814         }
12815       else
12816         context_die = comp_unit_die;
12817
12818       decl_die = lookup_decl_die (decl);
12819       if (decl_die)
12820         return decl_die;
12821
12822       switch (TREE_CODE (decl))
12823         {
12824         case FUNCTION_DECL:
12825           /* Clear current_function_decl, so that gen_subprogram_die thinks
12826              that this is a declaration. At this point, we just want to force
12827              declaration die.  */
12828           save_fn = current_function_decl;
12829           current_function_decl = NULL_TREE;
12830           gen_subprogram_die (decl, context_die);
12831           current_function_decl = save_fn;
12832           break;
12833
12834         case VAR_DECL:
12835           /* Set external flag to force declaration die. Restore it after
12836            gen_decl_die() call.  */
12837           saved_external_flag = DECL_EXTERNAL (decl);
12838           DECL_EXTERNAL (decl) = 1;
12839           gen_decl_die (decl, context_die);
12840           DECL_EXTERNAL (decl) = saved_external_flag;
12841           break;
12842
12843         case NAMESPACE_DECL:
12844           dwarf2out_decl (decl);
12845           break;
12846
12847         default:
12848           gcc_unreachable ();
12849         }
12850
12851       /* We should be able to find the DIE now.  */
12852       if (!decl_die)
12853         decl_die = lookup_decl_die (decl);
12854       gcc_assert (decl_die);
12855     }
12856
12857   return decl_die;
12858 }
12859
12860 /* Returns the DIE for TYPE.  A DIE is always returned.  */
12861
12862 static dw_die_ref
12863 force_type_die (tree type)
12864 {
12865   dw_die_ref type_die;
12866
12867   type_die = lookup_type_die (type);
12868   if (!type_die)
12869     {
12870       dw_die_ref context_die;
12871       if (TYPE_CONTEXT (type))
12872         {
12873           if (TYPE_P (TYPE_CONTEXT (type)))
12874             context_die = force_type_die (TYPE_CONTEXT (type));
12875           else
12876             context_die = force_decl_die (TYPE_CONTEXT (type));
12877         }
12878       else
12879         context_die = comp_unit_die;
12880
12881       type_die = lookup_type_die (type);
12882       if (type_die)
12883         return type_die;
12884       gen_type_die (type, context_die);
12885       type_die = lookup_type_die (type);
12886       gcc_assert (type_die);
12887     }
12888   return type_die;
12889 }
12890
12891 /* Force out any required namespaces to be able to output DECL,
12892    and return the new context_die for it, if it's changed.  */
12893
12894 static dw_die_ref
12895 setup_namespace_context (tree thing, dw_die_ref context_die)
12896 {
12897   tree context = (DECL_P (thing)
12898                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
12899   if (context && TREE_CODE (context) == NAMESPACE_DECL)
12900     /* Force out the namespace.  */
12901     context_die = force_decl_die (context);
12902
12903   return context_die;
12904 }
12905
12906 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
12907    type) within its namespace, if appropriate.
12908
12909    For compatibility with older debuggers, namespace DIEs only contain
12910    declarations; all definitions are emitted at CU scope.  */
12911
12912 static void
12913 declare_in_namespace (tree thing, dw_die_ref context_die)
12914 {
12915   dw_die_ref ns_context;
12916
12917   if (debug_info_level <= DINFO_LEVEL_TERSE)
12918     return;
12919
12920   /* If this decl is from an inlined function, then don't try to emit it in its
12921      namespace, as we will get confused.  It would have already been emitted
12922      when the abstract instance of the inline function was emitted anyways.  */
12923   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
12924     return;
12925
12926   ns_context = setup_namespace_context (thing, context_die);
12927
12928   if (ns_context != context_die)
12929     {
12930       if (DECL_P (thing))
12931         gen_decl_die (thing, ns_context);
12932       else
12933         gen_type_die (thing, ns_context);
12934     }
12935 }
12936
12937 /* Generate a DIE for a namespace or namespace alias.  */
12938
12939 static void
12940 gen_namespace_die (tree decl)
12941 {
12942   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
12943
12944   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
12945      they are an alias of.  */
12946   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
12947     {
12948       /* Output a real namespace.  */
12949       dw_die_ref namespace_die
12950         = new_die (DW_TAG_namespace, context_die, decl);
12951       add_name_and_src_coords_attributes (namespace_die, decl);
12952       equate_decl_number_to_die (decl, namespace_die);
12953     }
12954   else
12955     {
12956       /* Output a namespace alias.  */
12957
12958       /* Force out the namespace we are an alias of, if necessary.  */
12959       dw_die_ref origin_die
12960         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
12961
12962       /* Now create the namespace alias DIE.  */
12963       dw_die_ref namespace_die
12964         = new_die (DW_TAG_imported_declaration, context_die, decl);
12965       add_name_and_src_coords_attributes (namespace_die, decl);
12966       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
12967       equate_decl_number_to_die (decl, namespace_die);
12968     }
12969 }
12970
12971 /* Generate Dwarf debug information for a decl described by DECL.  */
12972
12973 static void
12974 gen_decl_die (tree decl, dw_die_ref context_die)
12975 {
12976   tree origin;
12977
12978   if (DECL_P (decl) && DECL_IGNORED_P (decl))
12979     return;
12980
12981   switch (TREE_CODE (decl))
12982     {
12983     case ERROR_MARK:
12984       break;
12985
12986     case CONST_DECL:
12987       /* The individual enumerators of an enum type get output when we output
12988          the Dwarf representation of the relevant enum type itself.  */
12989       break;
12990
12991     case FUNCTION_DECL:
12992       /* Don't output any DIEs to represent mere function declarations,
12993          unless they are class members or explicit block externs.  */
12994       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
12995           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
12996         break;
12997
12998 #if 0
12999       /* FIXME */
13000       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13001          on local redeclarations of global functions.  That seems broken.  */
13002       if (current_function_decl != decl)
13003         /* This is only a declaration.  */;
13004 #endif
13005
13006       /* If we're emitting a clone, emit info for the abstract instance.  */
13007       if (DECL_ORIGIN (decl) != decl)
13008         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13009
13010       /* If we're emitting an out-of-line copy of an inline function,
13011          emit info for the abstract instance and set up to refer to it.  */
13012       else if (cgraph_function_possibly_inlined_p (decl)
13013                && ! DECL_ABSTRACT (decl)
13014                && ! class_or_namespace_scope_p (context_die)
13015                /* dwarf2out_abstract_function won't emit a die if this is just
13016                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13017                   that case, because that works only if we have a die.  */
13018                && DECL_INITIAL (decl) != NULL_TREE)
13019         {
13020           dwarf2out_abstract_function (decl);
13021           set_decl_origin_self (decl);
13022         }
13023
13024       /* Otherwise we're emitting the primary DIE for this decl.  */
13025       else if (debug_info_level > DINFO_LEVEL_TERSE)
13026         {
13027           /* Before we describe the FUNCTION_DECL itself, make sure that we
13028              have described its return type.  */
13029           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13030
13031           /* And its virtual context.  */
13032           if (DECL_VINDEX (decl) != NULL_TREE)
13033             gen_type_die (DECL_CONTEXT (decl), context_die);
13034
13035           /* And its containing type.  */
13036           origin = decl_class_context (decl);
13037           if (origin != NULL_TREE)
13038             gen_type_die_for_member (origin, decl, context_die);
13039
13040           /* And its containing namespace.  */
13041           declare_in_namespace (decl, context_die);
13042         }
13043
13044       /* Now output a DIE to represent the function itself.  */
13045       gen_subprogram_die (decl, context_die);
13046       break;
13047
13048     case TYPE_DECL:
13049       /* If we are in terse mode, don't generate any DIEs to represent any
13050          actual typedefs.  */
13051       if (debug_info_level <= DINFO_LEVEL_TERSE)
13052         break;
13053
13054       /* In the special case of a TYPE_DECL node representing the declaration
13055          of some type tag, if the given TYPE_DECL is marked as having been
13056          instantiated from some other (original) TYPE_DECL node (e.g. one which
13057          was generated within the original definition of an inline function) we
13058          have to generate a special (abbreviated) DW_TAG_structure_type,
13059          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13060       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13061         {
13062           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13063           break;
13064         }
13065
13066       if (is_redundant_typedef (decl))
13067         gen_type_die (TREE_TYPE (decl), context_die);
13068       else
13069         /* Output a DIE to represent the typedef itself.  */
13070         gen_typedef_die (decl, context_die);
13071       break;
13072
13073     case LABEL_DECL:
13074       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13075         gen_label_die (decl, context_die);
13076       break;
13077
13078     case VAR_DECL:
13079     case RESULT_DECL:
13080       /* If we are in terse mode, don't generate any DIEs to represent any
13081          variable declarations or definitions.  */
13082       if (debug_info_level <= DINFO_LEVEL_TERSE)
13083         break;
13084
13085       /* Output any DIEs that are needed to specify the type of this data
13086          object.  */
13087       gen_type_die (TREE_TYPE (decl), context_die);
13088
13089       /* And its containing type.  */
13090       origin = decl_class_context (decl);
13091       if (origin != NULL_TREE)
13092         gen_type_die_for_member (origin, decl, context_die);
13093
13094       /* And its containing namespace.  */
13095       declare_in_namespace (decl, context_die);
13096
13097       /* Now output the DIE to represent the data object itself.  This gets
13098          complicated because of the possibility that the VAR_DECL really
13099          represents an inlined instance of a formal parameter for an inline
13100          function.  */
13101       origin = decl_ultimate_origin (decl);
13102       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13103         gen_formal_parameter_die (decl, context_die);
13104       else
13105         gen_variable_die (decl, context_die);
13106       break;
13107
13108     case FIELD_DECL:
13109       /* Ignore the nameless fields that are used to skip bits but handle C++
13110          anonymous unions and structs.  */
13111       if (DECL_NAME (decl) != NULL_TREE
13112           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13113           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13114         {
13115           gen_type_die (member_declared_type (decl), context_die);
13116           gen_field_die (decl, context_die);
13117         }
13118       break;
13119
13120     case PARM_DECL:
13121       gen_type_die (TREE_TYPE (decl), context_die);
13122       gen_formal_parameter_die (decl, context_die);
13123       break;
13124
13125     case NAMESPACE_DECL:
13126       gen_namespace_die (decl);
13127       break;
13128
13129     default:
13130       /* Probably some frontend-internal decl.  Assume we don't care.  */
13131       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13132       break;
13133     }
13134 }
13135 \f
13136 /* Output debug information for global decl DECL.  Called from toplev.c after
13137    compilation proper has finished.  */
13138
13139 static void
13140 dwarf2out_global_decl (tree decl)
13141 {
13142   /* Output DWARF2 information for file-scope tentative data object
13143      declarations, file-scope (extern) function declarations (which had no
13144      corresponding body) and file-scope tagged type declarations and
13145      definitions which have not yet been forced out.  */
13146   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13147     dwarf2out_decl (decl);
13148 }
13149
13150 /* Output debug information for type decl DECL.  Called from toplev.c
13151    and from language front ends (to record built-in types).  */
13152 static void
13153 dwarf2out_type_decl (tree decl, int local)
13154 {
13155   if (!local)
13156     dwarf2out_decl (decl);
13157 }
13158
13159 /* Output debug information for imported module or decl.  */
13160
13161 static void
13162 dwarf2out_imported_module_or_decl (tree decl, tree context)
13163 {
13164   dw_die_ref imported_die, at_import_die;
13165   dw_die_ref scope_die;
13166   unsigned file_index;
13167   expanded_location xloc;
13168
13169   if (debug_info_level <= DINFO_LEVEL_TERSE)
13170     return;
13171
13172   gcc_assert (decl);
13173
13174   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13175      We need decl DIE for reference and scope die. First, get DIE for the decl
13176      itself.  */
13177
13178   /* Get the scope die for decl context. Use comp_unit_die for global module
13179      or decl. If die is not found for non globals, force new die.  */
13180   if (!context)
13181     scope_die = comp_unit_die;
13182   else if (TYPE_P (context))
13183     scope_die = force_type_die (context);
13184   else
13185     scope_die = force_decl_die (context);
13186
13187   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13188   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13189     at_import_die = force_type_die (TREE_TYPE (decl));
13190   else
13191     {
13192       at_import_die = lookup_decl_die (decl);
13193       if (!at_import_die)
13194         {
13195           /* If we're trying to avoid duplicate debug info, we may not have
13196              emitted the member decl for this field.  Emit it now.  */
13197           if (TREE_CODE (decl) == FIELD_DECL)
13198             {
13199               tree type = DECL_CONTEXT (decl);
13200               dw_die_ref type_context_die;
13201
13202               if (TYPE_CONTEXT (type))
13203                 if (TYPE_P (TYPE_CONTEXT (type)))
13204                   type_context_die = force_type_die (TYPE_CONTEXT (type));
13205               else
13206                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13207               else
13208                 type_context_die = comp_unit_die;
13209               gen_type_die_for_member (type, decl, type_context_die);
13210             }
13211           at_import_die = force_decl_die (decl);
13212         }
13213     }
13214
13215   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13216   if (TREE_CODE (decl) == NAMESPACE_DECL)
13217     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13218   else
13219     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13220
13221   xloc = expand_location (input_location);
13222   file_index = lookup_filename (xloc.file);
13223   add_AT_unsigned (imported_die, DW_AT_decl_file, file_index);
13224   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13225   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13226 }
13227
13228 /* Write the debugging output for DECL.  */
13229
13230 void
13231 dwarf2out_decl (tree decl)
13232 {
13233   dw_die_ref context_die = comp_unit_die;
13234
13235   switch (TREE_CODE (decl))
13236     {
13237     case ERROR_MARK:
13238       return;
13239
13240     case FUNCTION_DECL:
13241       /* What we would really like to do here is to filter out all mere
13242          file-scope declarations of file-scope functions which are never
13243          referenced later within this translation unit (and keep all of ones
13244          that *are* referenced later on) but we aren't clairvoyant, so we have
13245          no idea which functions will be referenced in the future (i.e. later
13246          on within the current translation unit). So here we just ignore all
13247          file-scope function declarations which are not also definitions.  If
13248          and when the debugger needs to know something about these functions,
13249          it will have to hunt around and find the DWARF information associated
13250          with the definition of the function.
13251
13252          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13253          nodes represent definitions and which ones represent mere
13254          declarations.  We have to check DECL_INITIAL instead. That's because
13255          the C front-end supports some weird semantics for "extern inline"
13256          function definitions.  These can get inlined within the current
13257          translation unit (and thus, we need to generate Dwarf info for their
13258          abstract instances so that the Dwarf info for the concrete inlined
13259          instances can have something to refer to) but the compiler never
13260          generates any out-of-lines instances of such things (despite the fact
13261          that they *are* definitions).
13262
13263          The important point is that the C front-end marks these "extern
13264          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13265          them anyway. Note that the C++ front-end also plays some similar games
13266          for inline function definitions appearing within include files which
13267          also contain `#pragma interface' pragmas.  */
13268       if (DECL_INITIAL (decl) == NULL_TREE)
13269         return;
13270
13271       /* If we're a nested function, initially use a parent of NULL; if we're
13272          a plain function, this will be fixed up in decls_for_scope.  If
13273          we're a method, it will be ignored, since we already have a DIE.  */
13274       if (decl_function_context (decl)
13275           /* But if we're in terse mode, we don't care about scope.  */
13276           && debug_info_level > DINFO_LEVEL_TERSE)
13277         context_die = NULL;
13278       break;
13279
13280     case VAR_DECL:
13281       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13282          declaration and if the declaration was never even referenced from
13283          within this entire compilation unit.  We suppress these DIEs in
13284          order to save space in the .debug section (by eliminating entries
13285          which are probably useless).  Note that we must not suppress
13286          block-local extern declarations (whether used or not) because that
13287          would screw-up the debugger's name lookup mechanism and cause it to
13288          miss things which really ought to be in scope at a given point.  */
13289       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13290         return;
13291
13292       /* For local statics lookup proper context die.  */
13293       if (TREE_STATIC (decl) && decl_function_context (decl))
13294         context_die = lookup_decl_die (DECL_CONTEXT (decl));
13295
13296       /* If we are in terse mode, don't generate any DIEs to represent any
13297          variable declarations or definitions.  */
13298       if (debug_info_level <= DINFO_LEVEL_TERSE)
13299         return;
13300       break;
13301
13302     case NAMESPACE_DECL:
13303       if (debug_info_level <= DINFO_LEVEL_TERSE)
13304         return;
13305       if (lookup_decl_die (decl) != NULL)
13306         return;
13307       break;
13308
13309     case TYPE_DECL:
13310       /* Don't emit stubs for types unless they are needed by other DIEs.  */
13311       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13312         return;
13313
13314       /* Don't bother trying to generate any DIEs to represent any of the
13315          normal built-in types for the language we are compiling.  */
13316       if (DECL_IS_BUILTIN (decl))
13317         {
13318           /* OK, we need to generate one for `bool' so GDB knows what type
13319              comparisons have.  */
13320           if ((get_AT_unsigned (comp_unit_die, DW_AT_language)
13321                == DW_LANG_C_plus_plus)
13322               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13323               && ! DECL_IGNORED_P (decl))
13324             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13325
13326           return;
13327         }
13328
13329       /* If we are in terse mode, don't generate any DIEs for types.  */
13330       if (debug_info_level <= DINFO_LEVEL_TERSE)
13331         return;
13332
13333       /* If we're a function-scope tag, initially use a parent of NULL;
13334          this will be fixed up in decls_for_scope.  */
13335       if (decl_function_context (decl))
13336         context_die = NULL;
13337
13338       break;
13339
13340     default:
13341       return;
13342     }
13343
13344   gen_decl_die (decl, context_die);
13345 }
13346
13347 /* Output a marker (i.e. a label) for the beginning of the generated code for
13348    a lexical block.  */
13349
13350 static void
13351 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13352                        unsigned int blocknum)
13353 {
13354   switch_to_section (current_function_section ());
13355   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13356 }
13357
13358 /* Output a marker (i.e. a label) for the end of the generated code for a
13359    lexical block.  */
13360
13361 static void
13362 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13363 {
13364   switch_to_section (current_function_section ());
13365   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13366 }
13367
13368 /* Returns nonzero if it is appropriate not to emit any debugging
13369    information for BLOCK, because it doesn't contain any instructions.
13370
13371    Don't allow this for blocks with nested functions or local classes
13372    as we would end up with orphans, and in the presence of scheduling
13373    we may end up calling them anyway.  */
13374
13375 static bool
13376 dwarf2out_ignore_block (tree block)
13377 {
13378   tree decl;
13379
13380   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13381     if (TREE_CODE (decl) == FUNCTION_DECL
13382         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13383       return 0;
13384
13385   return 1;
13386 }
13387
13388 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13389    dwarf2out.c) and return its "index".  The index of each (known) filename is
13390    just a unique number which is associated with only that one filename.  We
13391    need such numbers for the sake of generating labels (in the .debug_sfnames
13392    section) and references to those files numbers (in the .debug_srcinfo
13393    and.debug_macinfo sections).  If the filename given as an argument is not
13394    found in our current list, add it to the list and assign it the next
13395    available unique index number.  In order to speed up searches, we remember
13396    the index of the filename was looked up last.  This handles the majority of
13397    all searches.  */
13398
13399 static unsigned
13400 lookup_filename (const char *file_name)
13401 {
13402   size_t i, n;
13403   char *save_file_name;
13404
13405   /* Check to see if the file name that was searched on the previous
13406      call matches this file name.  If so, return the index.  */
13407   if (file_table_last_lookup_index != 0)
13408     {
13409       const char *last
13410         = VARRAY_CHAR_PTR (file_table, file_table_last_lookup_index);
13411       if (strcmp (file_name, last) == 0)
13412         return file_table_last_lookup_index;
13413     }
13414
13415   /* Didn't match the previous lookup, search the table.  */
13416   n = VARRAY_ACTIVE_SIZE (file_table);
13417   for (i = 1; i < n; i++)
13418     if (strcmp (file_name, VARRAY_CHAR_PTR (file_table, i)) == 0)
13419       {
13420         file_table_last_lookup_index = i;
13421         return i;
13422       }
13423
13424   /* Add the new entry to the end of the filename table.  */
13425   file_table_last_lookup_index = n;
13426   save_file_name = (char *) ggc_strdup (file_name);
13427   VARRAY_PUSH_CHAR_PTR (file_table, save_file_name);
13428   VARRAY_PUSH_UINT (file_table_emitted, 0);
13429
13430   /* If the assembler is emitting the file table, and we aren't eliminating
13431      unused debug types, then we must emit .file here.  If we are eliminating
13432      unused debug types, then this will be done by the maybe_emit_file call in
13433      prune_unused_types_walk_attribs.  */
13434
13435   if (DWARF2_ASM_LINE_DEBUG_INFO && ! flag_eliminate_unused_debug_types)
13436     return maybe_emit_file (i);
13437
13438   return i;
13439 }
13440
13441 /* If the assembler will construct the file table, then translate the compiler
13442    internal file table number into the assembler file table number, and emit
13443    a .file directive if we haven't already emitted one yet.  The file table
13444    numbers are different because we prune debug info for unused variables and
13445    types, which may include filenames.  */
13446
13447 static int
13448 maybe_emit_file (int fileno)
13449 {
13450   if (DWARF2_ASM_LINE_DEBUG_INFO && fileno > 0)
13451     {
13452       if (!VARRAY_UINT (file_table_emitted, fileno))
13453         {
13454           VARRAY_UINT (file_table_emitted, fileno) = ++emitcount;
13455           fprintf (asm_out_file, "\t.file %u ",
13456                    VARRAY_UINT (file_table_emitted, fileno));
13457           output_quoted_string (asm_out_file,
13458                                 VARRAY_CHAR_PTR (file_table, fileno));
13459           fputc ('\n', asm_out_file);
13460         }
13461       return VARRAY_UINT (file_table_emitted, fileno);
13462     }
13463   else
13464     return fileno;
13465 }
13466
13467 /* Initialize the compiler internal file table.  */
13468
13469 static void
13470 init_file_table (void)
13471 {
13472   /* Allocate the initial hunk of the file_table.  */
13473   VARRAY_CHAR_PTR_INIT (file_table, 64, "file_table");
13474   VARRAY_UINT_INIT (file_table_emitted, 64, "file_table_emitted");
13475
13476   /* Skip the first entry - file numbers begin at 1.  */
13477   VARRAY_PUSH_CHAR_PTR (file_table, NULL);
13478   VARRAY_PUSH_UINT (file_table_emitted, 0);
13479   file_table_last_lookup_index = 0;
13480 }
13481
13482 /* Called by the final INSN scan whenever we see a var location.  We
13483    use it to drop labels in the right places, and throw the location in
13484    our lookup table.  */
13485
13486 static void
13487 dwarf2out_var_location (rtx loc_note)
13488 {
13489   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13490   struct var_loc_node *newloc;
13491   rtx prev_insn;
13492   static rtx last_insn;
13493   static const char *last_label;
13494   tree decl;
13495
13496   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13497     return;
13498   prev_insn = PREV_INSN (loc_note);
13499
13500   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13501   /* If the insn we processed last time is the previous insn
13502      and it is also a var location note, use the label we emitted
13503      last time.  */
13504   if (last_insn != NULL_RTX
13505       && last_insn == prev_insn
13506       && NOTE_P (prev_insn)
13507       && NOTE_LINE_NUMBER (prev_insn) == NOTE_INSN_VAR_LOCATION)
13508     {
13509       newloc->label = last_label;
13510     }
13511   else
13512     {
13513       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13514       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13515       loclabel_num++;
13516       newloc->label = ggc_strdup (loclabel);
13517     }
13518   newloc->var_loc_note = loc_note;
13519   newloc->next = NULL;
13520
13521   if (cfun && in_cold_section_p)
13522     newloc->section_label = cfun->cold_section_label;
13523   else
13524     newloc->section_label = text_section_label;
13525
13526   last_insn = loc_note;
13527   last_label = newloc->label;
13528   decl = NOTE_VAR_LOCATION_DECL (loc_note);
13529   if (DECL_DEBUG_EXPR_IS_FROM (decl) && DECL_DEBUG_EXPR (decl) 
13530       && DECL_P (DECL_DEBUG_EXPR (decl)))
13531     decl = DECL_DEBUG_EXPR (decl); 
13532   add_var_loc_to_decl (decl, newloc);
13533 }
13534
13535 /* We need to reset the locations at the beginning of each
13536    function. We can't do this in the end_function hook, because the
13537    declarations that use the locations won't have been output when
13538    that hook is called.  Also compute have_multiple_function_sections here.  */
13539
13540 static void
13541 dwarf2out_begin_function (tree fun)
13542 {
13543   htab_empty (decl_loc_table);
13544   
13545   if (function_section (fun) != text_section)
13546     have_multiple_function_sections = true;
13547 }
13548
13549 /* Output a label to mark the beginning of a source code line entry
13550    and record information relating to this source line, in
13551    'line_info_table' for later output of the .debug_line section.  */
13552
13553 static void
13554 dwarf2out_source_line (unsigned int line, const char *filename)
13555 {
13556   if (debug_info_level >= DINFO_LEVEL_NORMAL
13557       && line != 0)
13558     {
13559       switch_to_section (current_function_section ());
13560
13561       /* If requested, emit something human-readable.  */
13562       if (flag_debug_asm)
13563         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13564                  filename, line);
13565
13566       if (DWARF2_ASM_LINE_DEBUG_INFO)
13567         {
13568           unsigned file_num = lookup_filename (filename);
13569
13570           file_num = maybe_emit_file (file_num);
13571
13572           /* Emit the .loc directive understood by GNU as.  */
13573           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13574
13575           /* Indicate that line number info exists.  */
13576           line_info_table_in_use++;
13577         }
13578       else if (function_section (current_function_decl) != text_section)
13579         {
13580           dw_separate_line_info_ref line_info;
13581           targetm.asm_out.internal_label (asm_out_file, SEPARATE_LINE_CODE_LABEL,
13582                                      separate_line_info_table_in_use);
13583
13584           /* Expand the line info table if necessary.  */
13585           if (separate_line_info_table_in_use
13586               == separate_line_info_table_allocated)
13587             {
13588               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13589               separate_line_info_table
13590                 = ggc_realloc (separate_line_info_table,
13591                                separate_line_info_table_allocated
13592                                * sizeof (dw_separate_line_info_entry));
13593               memset (separate_line_info_table
13594                        + separate_line_info_table_in_use,
13595                       0,
13596                       (LINE_INFO_TABLE_INCREMENT
13597                        * sizeof (dw_separate_line_info_entry)));
13598             }
13599
13600           /* Add the new entry at the end of the line_info_table.  */
13601           line_info
13602             = &separate_line_info_table[separate_line_info_table_in_use++];
13603           line_info->dw_file_num = lookup_filename (filename);
13604           line_info->dw_line_num = line;
13605           line_info->function = current_function_funcdef_no;
13606         }
13607       else
13608         {
13609           dw_line_info_ref line_info;
13610
13611           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13612                                      line_info_table_in_use);
13613
13614           /* Expand the line info table if necessary.  */
13615           if (line_info_table_in_use == line_info_table_allocated)
13616             {
13617               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13618               line_info_table
13619                 = ggc_realloc (line_info_table,
13620                                (line_info_table_allocated
13621                                 * sizeof (dw_line_info_entry)));
13622               memset (line_info_table + line_info_table_in_use, 0,
13623                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13624             }
13625
13626           /* Add the new entry at the end of the line_info_table.  */
13627           line_info = &line_info_table[line_info_table_in_use++];
13628           line_info->dw_file_num = lookup_filename (filename);
13629           line_info->dw_line_num = line;
13630         }
13631     }
13632 }
13633
13634 /* Record the beginning of a new source file.  */
13635
13636 static void
13637 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13638 {
13639   if (flag_eliminate_dwarf2_dups)
13640     {
13641       /* Record the beginning of the file for break_out_includes.  */
13642       dw_die_ref bincl_die;
13643
13644       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13645       add_AT_string (bincl_die, DW_AT_name, filename);
13646     }
13647
13648   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13649     {
13650       int fileno;
13651
13652       switch_to_section (debug_macinfo_section);
13653       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13654       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13655                                    lineno);
13656
13657       fileno = maybe_emit_file (lookup_filename (filename));
13658       dw2_asm_output_data_uleb128 (fileno, "Filename we just started");
13659     }
13660 }
13661
13662 /* Record the end of a source file.  */
13663
13664 static void
13665 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
13666 {
13667   if (flag_eliminate_dwarf2_dups)
13668     /* Record the end of the file for break_out_includes.  */
13669     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
13670
13671   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13672     {
13673       switch_to_section (debug_macinfo_section);
13674       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
13675     }
13676 }
13677
13678 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
13679    the tail part of the directive line, i.e. the part which is past the
13680    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13681
13682 static void
13683 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
13684                   const char *buffer ATTRIBUTE_UNUSED)
13685 {
13686   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13687     {
13688       switch_to_section (debug_macinfo_section);
13689       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
13690       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13691       dw2_asm_output_nstring (buffer, -1, "The macro");
13692     }
13693 }
13694
13695 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
13696    the tail part of the directive line, i.e. the part which is past the
13697    initial whitespace, #, whitespace, directive-name, whitespace part.  */
13698
13699 static void
13700 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
13701                  const char *buffer ATTRIBUTE_UNUSED)
13702 {
13703   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13704     {
13705       switch_to_section (debug_macinfo_section);
13706       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
13707       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
13708       dw2_asm_output_nstring (buffer, -1, "The macro");
13709     }
13710 }
13711
13712 /* Set up for Dwarf output at the start of compilation.  */
13713
13714 static void
13715 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
13716 {
13717   init_file_table ();
13718
13719   /* Allocate the decl_die_table.  */
13720   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
13721                                     decl_die_table_eq, NULL);
13722
13723   /* Allocate the decl_loc_table.  */
13724   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
13725                                     decl_loc_table_eq, NULL);
13726
13727   /* Allocate the initial hunk of the decl_scope_table.  */
13728   decl_scope_table = VEC_alloc (tree, gc, 256);
13729
13730   /* Allocate the initial hunk of the abbrev_die_table.  */
13731   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
13732                                         * sizeof (dw_die_ref));
13733   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
13734   /* Zero-th entry is allocated, but unused.  */
13735   abbrev_die_table_in_use = 1;
13736
13737   /* Allocate the initial hunk of the line_info_table.  */
13738   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
13739                                        * sizeof (dw_line_info_entry));
13740   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
13741
13742   /* Zero-th entry is allocated, but unused.  */
13743   line_info_table_in_use = 1;
13744
13745   /* Generate the initial DIE for the .debug section.  Note that the (string)
13746      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
13747      will (typically) be a relative pathname and that this pathname should be
13748      taken as being relative to the directory from which the compiler was
13749      invoked when the given (base) source file was compiled.  We will fill
13750      in this value in dwarf2out_finish.  */
13751   comp_unit_die = gen_compile_unit_die (NULL);
13752
13753   incomplete_types = VEC_alloc (tree, gc, 64);
13754
13755   used_rtx_array = VEC_alloc (rtx, gc, 32);
13756
13757   debug_info_section = get_section (DEBUG_INFO_SECTION,
13758                                     SECTION_DEBUG, NULL);
13759   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
13760                                       SECTION_DEBUG, NULL);
13761   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
13762                                        SECTION_DEBUG, NULL);
13763   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
13764                                        SECTION_DEBUG, NULL);
13765   debug_line_section = get_section (DEBUG_LINE_SECTION,
13766                                     SECTION_DEBUG, NULL);
13767   debug_loc_section = get_section (DEBUG_LOC_SECTION,
13768                                    SECTION_DEBUG, NULL);
13769   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
13770                                         SECTION_DEBUG, NULL);
13771   debug_str_section = get_section (DEBUG_STR_SECTION,
13772                                    DEBUG_STR_SECTION_FLAGS, NULL);
13773   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
13774                                       SECTION_DEBUG, NULL);
13775   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
13776                                      SECTION_DEBUG, NULL);
13777
13778   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
13779   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
13780                                DEBUG_ABBREV_SECTION_LABEL, 0);
13781   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
13782   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label, 
13783                                COLD_TEXT_SECTION_LABEL, 0);
13784   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
13785
13786   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
13787                                DEBUG_INFO_SECTION_LABEL, 0);
13788   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
13789                                DEBUG_LINE_SECTION_LABEL, 0);
13790   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
13791                                DEBUG_RANGES_SECTION_LABEL, 0);
13792   switch_to_section (debug_abbrev_section);
13793   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
13794   switch_to_section (debug_info_section);
13795   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
13796   switch_to_section (debug_line_section);
13797   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
13798
13799   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13800     {
13801       switch_to_section (debug_macinfo_section);
13802       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
13803                                    DEBUG_MACINFO_SECTION_LABEL, 0);
13804       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
13805     }
13806
13807   switch_to_section (text_section);
13808   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
13809   if (flag_reorder_blocks_and_partition)
13810     {
13811       switch_to_section (unlikely_text_section ());
13812       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
13813     }
13814 }
13815
13816 /* A helper function for dwarf2out_finish called through
13817    ht_forall.  Emit one queued .debug_str string.  */
13818
13819 static int
13820 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
13821 {
13822   struct indirect_string_node *node = (struct indirect_string_node *) *h;
13823
13824   if (node->form == DW_FORM_strp)
13825     {
13826       switch_to_section (debug_str_section);
13827       ASM_OUTPUT_LABEL (asm_out_file, node->label);
13828       assemble_string (node->str, strlen (node->str) + 1);
13829     }
13830
13831   return 1;
13832 }
13833
13834
13835
13836 /* Clear the marks for a die and its children.
13837    Be cool if the mark isn't set.  */
13838
13839 static void
13840 prune_unmark_dies (dw_die_ref die)
13841 {
13842   dw_die_ref c;
13843   die->die_mark = 0;
13844   for (c = die->die_child; c; c = c->die_sib)
13845     prune_unmark_dies (c);
13846 }
13847
13848
13849 /* Given DIE that we're marking as used, find any other dies
13850    it references as attributes and mark them as used.  */
13851
13852 static void
13853 prune_unused_types_walk_attribs (dw_die_ref die)
13854 {
13855   dw_attr_ref a;
13856
13857   for (a = die->die_attr; a != NULL; a = a->dw_attr_next)
13858     {
13859       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
13860         {
13861           /* A reference to another DIE.
13862              Make sure that it will get emitted.  */
13863           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
13864         }
13865       else if (a->dw_attr == DW_AT_decl_file || a->dw_attr == DW_AT_call_file)
13866         {
13867           /* A reference to a file.  Make sure the file name is emitted.  */
13868           a->dw_attr_val.v.val_unsigned =
13869             maybe_emit_file (a->dw_attr_val.v.val_unsigned);
13870         }
13871     }
13872 }
13873
13874
13875 /* Mark DIE as being used.  If DOKIDS is true, then walk down
13876    to DIE's children.  */
13877
13878 static void
13879 prune_unused_types_mark (dw_die_ref die, int dokids)
13880 {
13881   dw_die_ref c;
13882
13883   if (die->die_mark == 0)
13884     {
13885       /* We haven't done this node yet.  Mark it as used.  */
13886       die->die_mark = 1;
13887
13888       /* We also have to mark its parents as used.
13889          (But we don't want to mark our parents' kids due to this.)  */
13890       if (die->die_parent)
13891         prune_unused_types_mark (die->die_parent, 0);
13892
13893       /* Mark any referenced nodes.  */
13894       prune_unused_types_walk_attribs (die);
13895
13896       /* If this node is a specification,
13897          also mark the definition, if it exists.  */
13898       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
13899         prune_unused_types_mark (die->die_definition, 1);
13900     }
13901
13902   if (dokids && die->die_mark != 2)
13903     {
13904       /* We need to walk the children, but haven't done so yet.
13905          Remember that we've walked the kids.  */
13906       die->die_mark = 2;
13907
13908       /* Walk them.  */
13909       for (c = die->die_child; c; c = c->die_sib)
13910         {
13911           /* If this is an array type, we need to make sure our
13912              kids get marked, even if they're types.  */
13913           if (die->die_tag == DW_TAG_array_type)
13914             prune_unused_types_mark (c, 1);
13915           else
13916             prune_unused_types_walk (c);
13917         }
13918     }
13919 }
13920
13921
13922 /* Walk the tree DIE and mark types that we actually use.  */
13923
13924 static void
13925 prune_unused_types_walk (dw_die_ref die)
13926 {
13927   dw_die_ref c;
13928
13929   /* Don't do anything if this node is already marked.  */
13930   if (die->die_mark)
13931     return;
13932
13933   switch (die->die_tag) {
13934   case DW_TAG_const_type:
13935   case DW_TAG_packed_type:
13936   case DW_TAG_pointer_type:
13937   case DW_TAG_reference_type:
13938   case DW_TAG_volatile_type:
13939   case DW_TAG_typedef:
13940   case DW_TAG_array_type:
13941   case DW_TAG_structure_type:
13942   case DW_TAG_union_type:
13943   case DW_TAG_class_type:
13944   case DW_TAG_friend:
13945   case DW_TAG_variant_part:
13946   case DW_TAG_enumeration_type:
13947   case DW_TAG_subroutine_type:
13948   case DW_TAG_string_type:
13949   case DW_TAG_set_type:
13950   case DW_TAG_subrange_type:
13951   case DW_TAG_ptr_to_member_type:
13952   case DW_TAG_file_type:
13953     /* It's a type node --- don't mark it.  */
13954     return;
13955
13956   default:
13957     /* Mark everything else.  */
13958     break;
13959   }
13960
13961   die->die_mark = 1;
13962
13963   /* Now, mark any dies referenced from here.  */
13964   prune_unused_types_walk_attribs (die);
13965
13966   /* Mark children.  */
13967   for (c = die->die_child; c; c = c->die_sib)
13968     prune_unused_types_walk (c);
13969 }
13970
13971
13972 /* Remove from the tree DIE any dies that aren't marked.  */
13973
13974 static void
13975 prune_unused_types_prune (dw_die_ref die)
13976 {
13977   dw_die_ref c, p, n;
13978
13979   gcc_assert (die->die_mark);
13980
13981   p = NULL;
13982   for (c = die->die_child; c; c = n)
13983     {
13984       n = c->die_sib;
13985       if (c->die_mark)
13986         {
13987           prune_unused_types_prune (c);
13988           p = c;
13989         }
13990       else
13991         {
13992           if (p)
13993             p->die_sib = n;
13994           else
13995             die->die_child = n;
13996           free_die (c);
13997         }
13998     }
13999 }
14000
14001
14002 /* Remove dies representing declarations that we never use.  */
14003
14004 static void
14005 prune_unused_types (void)
14006 {
14007   unsigned int i;
14008   limbo_die_node *node;
14009
14010   /* Clear all the marks.  */
14011   prune_unmark_dies (comp_unit_die);
14012   for (node = limbo_die_list; node; node = node->next)
14013     prune_unmark_dies (node->die);
14014
14015   /* Set the mark on nodes that are actually used.  */
14016   prune_unused_types_walk (comp_unit_die);
14017   for (node = limbo_die_list; node; node = node->next)
14018     prune_unused_types_walk (node->die);
14019
14020   /* Also set the mark on nodes referenced from the
14021      pubname_table or arange_table.  */
14022   for (i = 0; i < pubname_table_in_use; i++)
14023     prune_unused_types_mark (pubname_table[i].die, 1);
14024   for (i = 0; i < arange_table_in_use; i++)
14025     prune_unused_types_mark (arange_table[i], 1);
14026
14027   /* Get rid of nodes that aren't marked.  */
14028   prune_unused_types_prune (comp_unit_die);
14029   for (node = limbo_die_list; node; node = node->next)
14030     prune_unused_types_prune (node->die);
14031
14032   /* Leave the marks clear.  */
14033   prune_unmark_dies (comp_unit_die);
14034   for (node = limbo_die_list; node; node = node->next)
14035     prune_unmark_dies (node->die);
14036 }
14037
14038 /* Output stuff that dwarf requires at the end of every file,
14039    and generate the DWARF-2 debugging info.  */
14040
14041 static void
14042 dwarf2out_finish (const char *filename)
14043 {
14044   limbo_die_node *node, *next_node;
14045   dw_die_ref die = 0;
14046
14047   /* Add the name for the main input file now.  We delayed this from
14048      dwarf2out_init to avoid complications with PCH.  */
14049   add_name_attribute (comp_unit_die, filename);
14050   if (filename[0] != DIR_SEPARATOR)
14051     add_comp_dir_attribute (comp_unit_die);
14052   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14053     {
14054       size_t i;
14055       for (i = 1; i < VARRAY_ACTIVE_SIZE (file_table); i++)
14056         if (VARRAY_CHAR_PTR (file_table, i)[0] != DIR_SEPARATOR
14057             /* Don't add cwd for <built-in>.  */
14058             && VARRAY_CHAR_PTR (file_table, i)[0] != '<')
14059           {
14060             add_comp_dir_attribute (comp_unit_die);
14061             break;
14062           }
14063     }
14064
14065   /* Traverse the limbo die list, and add parent/child links.  The only
14066      dies without parents that should be here are concrete instances of
14067      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
14068      For concrete instances, we can get the parent die from the abstract
14069      instance.  */
14070   for (node = limbo_die_list; node; node = next_node)
14071     {
14072       next_node = node->next;
14073       die = node->die;
14074
14075       if (die->die_parent == NULL)
14076         {
14077           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14078
14079           if (origin)
14080             add_child_die (origin->die_parent, die);
14081           else if (die == comp_unit_die)
14082             ;
14083           else if (errorcount > 0 || sorrycount > 0)
14084             /* It's OK to be confused by errors in the input.  */
14085             add_child_die (comp_unit_die, die);
14086           else
14087             {
14088               /* In certain situations, the lexical block containing a
14089                  nested function can be optimized away, which results
14090                  in the nested function die being orphaned.  Likewise
14091                  with the return type of that nested function.  Force
14092                  this to be a child of the containing function.
14093
14094                  It may happen that even the containing function got fully
14095                  inlined and optimized out.  In that case we are lost and
14096                  assign the empty child.  This should not be big issue as
14097                  the function is likely unreachable too.  */
14098               tree context = NULL_TREE;
14099
14100               gcc_assert (node->created_for);
14101
14102               if (DECL_P (node->created_for))
14103                 context = DECL_CONTEXT (node->created_for);
14104               else if (TYPE_P (node->created_for))
14105                 context = TYPE_CONTEXT (node->created_for);
14106
14107               gcc_assert (context && TREE_CODE (context) == FUNCTION_DECL);
14108
14109               origin = lookup_decl_die (context);
14110               if (origin)
14111                 add_child_die (origin, die);
14112               else
14113                 add_child_die (comp_unit_die, die);
14114             }
14115         }
14116     }
14117
14118   limbo_die_list = NULL;
14119
14120   /* Walk through the list of incomplete types again, trying once more to
14121      emit full debugging info for them.  */
14122   retry_incomplete_types ();
14123
14124   /* We need to reverse all the dies before break_out_includes, or
14125      we'll see the end of an include file before the beginning.  */
14126   reverse_all_dies (comp_unit_die);
14127
14128   if (flag_eliminate_unused_debug_types)
14129     prune_unused_types ();
14130
14131   /* Generate separate CUs for each of the include files we've seen.
14132      They will go into limbo_die_list.  */
14133   if (flag_eliminate_dwarf2_dups)
14134     break_out_includes (comp_unit_die);
14135
14136   /* Traverse the DIE's and add add sibling attributes to those DIE's
14137      that have children.  */
14138   add_sibling_attributes (comp_unit_die);
14139   for (node = limbo_die_list; node; node = node->next)
14140     add_sibling_attributes (node->die);
14141
14142   /* Output a terminator label for the .text section.  */
14143   switch_to_section (text_section);
14144   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14145   if (flag_reorder_blocks_and_partition)
14146     {
14147       switch_to_section (unlikely_text_section ());
14148       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14149     }
14150
14151   /* Output the source line correspondence table.  We must do this
14152      even if there is no line information.  Otherwise, on an empty
14153      translation unit, we will generate a present, but empty,
14154      .debug_info section.  IRIX 6.5 `nm' will then complain when
14155      examining the file.  */
14156   if (! DWARF2_ASM_LINE_DEBUG_INFO)
14157     {
14158       switch_to_section (debug_line_section);
14159       output_line_info ();
14160     }
14161
14162   /* We can only use the low/high_pc attributes if all of the code was
14163      in .text.  */
14164   if (!have_multiple_function_sections)
14165     {
14166       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14167       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14168     }
14169
14170   /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14171      "base address".  Use zero so that these addresses become absolute.  */
14172   else if (have_location_lists || ranges_table_in_use)
14173     add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14174
14175   /* Output location list section if necessary.  */
14176   if (have_location_lists)
14177     {
14178       /* Output the location lists info.  */
14179       switch_to_section (debug_loc_section);
14180       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14181                                    DEBUG_LOC_SECTION_LABEL, 0);
14182       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14183       output_location_lists (die);
14184     }
14185
14186   if (debug_info_level >= DINFO_LEVEL_NORMAL)
14187     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14188                     debug_line_section_label);
14189
14190   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14191     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14192
14193   /* Output all of the compilation units.  We put the main one last so that
14194      the offsets are available to output_pubnames.  */
14195   for (node = limbo_die_list; node; node = node->next)
14196     output_comp_unit (node->die, 0);
14197
14198   output_comp_unit (comp_unit_die, 0);
14199
14200   /* Output the abbreviation table.  */
14201   switch_to_section (debug_abbrev_section);
14202   output_abbrev_section ();
14203
14204   /* Output public names table if necessary.  */
14205   if (pubname_table_in_use)
14206     {
14207       switch_to_section (debug_pubnames_section);
14208       output_pubnames ();
14209     }
14210
14211   /* Output the address range information.  We only put functions in the arange
14212      table, so don't write it out if we don't have any.  */
14213   if (fde_table_in_use)
14214     {
14215       switch_to_section (debug_aranges_section);
14216       output_aranges ();
14217     }
14218
14219   /* Output ranges section if necessary.  */
14220   if (ranges_table_in_use)
14221     {
14222       switch_to_section (debug_ranges_section);
14223       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14224       output_ranges ();
14225     }
14226
14227   /* Have to end the macro section.  */
14228   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14229     {
14230       switch_to_section (debug_macinfo_section);
14231       dw2_asm_output_data (1, 0, "End compilation unit");
14232     }
14233
14234   /* If we emitted any DW_FORM_strp form attribute, output the string
14235      table too.  */
14236   if (debug_str_hash)
14237     htab_traverse (debug_str_hash, output_indirect_string, NULL);
14238 }
14239 #else
14240
14241 /* This should never be used, but its address is needed for comparisons.  */
14242 const struct gcc_debug_hooks dwarf2_debug_hooks;
14243
14244 #endif /* DWARF2_DEBUGGING_INFO */
14245
14246 #include "gt-dwarf2out.h"