OSDN Git Service

./:
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 #  define DWARF2_FRAME_INFO \
100   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 #  define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107    collected using DWARF_FRAME_REGNUM to those that should be output in
108    .debug_frame and .eh_frame.  */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Decide whether we want to emit frame unwind information for the current
114    translation unit.  */
115
116 int
117 dwarf2out_do_frame (void)
118 {
119   /* We want to emit correct CFA location expressions or lists, so we
120      have to return true if we're going to output debug info, even if
121      we're not going to output frame or unwind info.  */
122   return (write_symbols == DWARF2_DEBUG
123           || write_symbols == VMS_AND_DWARF2_DEBUG
124           || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126           || (DWARF2_UNWIND_INFO
127               && (flag_unwind_tables
128                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
130           );
131 }
132
133 /* The size of the target's pointer type.  */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
137
138 /* Array of RTXes referenced by the debugging information, which therefore
139    must be kept around forever.  */
140 static GTY(()) VEC(rtx,gc) *used_rtx_array;
141
142 /* A pointer to the base of a list of incomplete types which might be
143    completed at some later time.  incomplete_types_list needs to be a
144    VEC(tree,gc) because we want to tell the garbage collector about
145    it.  */
146 static GTY(()) VEC(tree,gc) *incomplete_types;
147
148 /* A pointer to the base of a table of references to declaration
149    scopes.  This table is a display which tracks the nesting
150    of declaration scopes at the current scope and containing
151    scopes.  This table is used to find the proper place to
152    define type declaration DIE's.  */
153 static GTY(()) VEC(tree,gc) *decl_scope_table;
154
155 /* Pointers to various DWARF2 sections.  */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_ranges_section;
166 static GTY(()) section *debug_frame_section;
167
168 /* How to start an assembler comment.  */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
172
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
176
177 /* Call frames are described using a sequence of Call Frame
178    Information instructions.  The register number, offset
179    and address fields are provided as possible operands;
180    their use is selected by the opcode field.  */
181
182 enum dw_cfi_oprnd_type {
183   dw_cfi_oprnd_unused,
184   dw_cfi_oprnd_reg_num,
185   dw_cfi_oprnd_offset,
186   dw_cfi_oprnd_addr,
187   dw_cfi_oprnd_loc
188 };
189
190 typedef union dw_cfi_oprnd_struct GTY(())
191 {
192   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
193   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
194   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
196 }
197 dw_cfi_oprnd;
198
199 typedef struct dw_cfi_struct GTY(())
200 {
201   dw_cfi_ref dw_cfi_next;
202   enum dwarf_call_frame_info dw_cfi_opc;
203   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
204     dw_cfi_oprnd1;
205   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
206     dw_cfi_oprnd2;
207 }
208 dw_cfi_node;
209
210 /* This is how we define the location of the CFA. We use to handle it
211    as REG + OFFSET all the time,  but now it can be more complex.
212    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
213    Instead of passing around REG and OFFSET, we pass a copy
214    of this structure.  */
215 typedef struct cfa_loc GTY(())
216 {
217   HOST_WIDE_INT offset;
218   HOST_WIDE_INT base_offset;
219   unsigned int reg;
220   int indirect;            /* 1 if CFA is accessed via a dereference.  */
221 } dw_cfa_location;
222
223 /* All call frame descriptions (FDE's) in the GCC generated DWARF
224    refer to a single Common Information Entry (CIE), defined at
225    the beginning of the .debug_frame section.  This use of a single
226    CIE obviates the need to keep track of multiple CIE's
227    in the DWARF generation routines below.  */
228
229 typedef struct dw_fde_struct GTY(())
230 {
231   tree decl;
232   const char *dw_fde_begin;
233   const char *dw_fde_current_label;
234   const char *dw_fde_end;
235   const char *dw_fde_hot_section_label;
236   const char *dw_fde_hot_section_end_label;
237   const char *dw_fde_unlikely_section_label;
238   const char *dw_fde_unlikely_section_end_label;
239   bool dw_fde_switched_sections;
240   dw_cfi_ref dw_fde_cfi;
241   unsigned funcdef_number;
242   unsigned all_throwers_are_sibcalls : 1;
243   unsigned nothrow : 1;
244   unsigned uses_eh_lsda : 1;
245 }
246 dw_fde_node;
247
248 /* Maximum size (in bytes) of an artificially generated label.  */
249 #define MAX_ARTIFICIAL_LABEL_BYTES      30
250
251 /* The size of addresses as they appear in the Dwarf 2 data.
252    Some architectures use word addresses to refer to code locations,
253    but Dwarf 2 info always uses byte addresses.  On such machines,
254    Dwarf 2 addresses need to be larger than the architecture's
255    pointers.  */
256 #ifndef DWARF2_ADDR_SIZE
257 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
258 #endif
259
260 /* The size in bytes of a DWARF field indicating an offset or length
261    relative to a debug info section, specified to be 4 bytes in the
262    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
263    as PTR_SIZE.  */
264
265 #ifndef DWARF_OFFSET_SIZE
266 #define DWARF_OFFSET_SIZE 4
267 #endif
268
269 /* According to the (draft) DWARF 3 specification, the initial length
270    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
271    bytes are 0xffffffff, followed by the length stored in the next 8
272    bytes.
273
274    However, the SGI/MIPS ABI uses an initial length which is equal to
275    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
276
277 #ifndef DWARF_INITIAL_LENGTH_SIZE
278 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
279 #endif
280
281 #define DWARF_VERSION 2
282
283 /* Round SIZE up to the nearest BOUNDARY.  */
284 #define DWARF_ROUND(SIZE,BOUNDARY) \
285   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
286
287 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
288 #ifndef DWARF_CIE_DATA_ALIGNMENT
289 #ifdef STACK_GROWS_DOWNWARD
290 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
291 #else
292 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
293 #endif
294 #endif
295
296 /* CIE identifier.  */
297 #if HOST_BITS_PER_WIDE_INT >= 64
298 #define DWARF_CIE_ID \
299   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
300 #else
301 #define DWARF_CIE_ID DW_CIE_ID
302 #endif
303
304 /* A pointer to the base of a table that contains frame description
305    information for each routine.  */
306 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
307
308 /* Number of elements currently allocated for fde_table.  */
309 static GTY(()) unsigned fde_table_allocated;
310
311 /* Number of elements in fde_table currently in use.  */
312 static GTY(()) unsigned fde_table_in_use;
313
314 /* Size (in elements) of increments by which we may expand the
315    fde_table.  */
316 #define FDE_TABLE_INCREMENT 256
317
318 /* A list of call frame insns for the CIE.  */
319 static GTY(()) dw_cfi_ref cie_cfi_head;
320
321 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
322 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
323    attribute that accelerates the lookup of the FDE associated
324    with the subprogram.  This variable holds the table index of the FDE
325    associated with the current function (body) definition.  */
326 static unsigned current_funcdef_fde;
327 #endif
328
329 struct indirect_string_node GTY(())
330 {
331   const char *str;
332   unsigned int refcount;
333   unsigned int form;
334   char *label;
335 };
336
337 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
338
339 static GTY(()) int dw2_string_counter;
340 static GTY(()) unsigned long dwarf2out_cfi_label_num;
341
342 /* True if the compilation unit places functions in more than one section.  */
343 static GTY(()) bool have_multiple_function_sections = false;
344
345 /* Whether the default text and cold text sections have been used at all.  */
346
347 static GTY(()) bool text_section_used = false;
348 static GTY(()) bool cold_text_section_used = false;
349
350 /* The default cold text section.  */
351 static GTY(()) section *cold_text_section;
352
353 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
354
355 /* Forward declarations for functions defined in this file.  */
356
357 static char *stripattributes (const char *);
358 static const char *dwarf_cfi_name (unsigned);
359 static dw_cfi_ref new_cfi (void);
360 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
361 static void add_fde_cfi (const char *, dw_cfi_ref);
362 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
363 static void lookup_cfa (dw_cfa_location *);
364 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
365 #ifdef DWARF2_UNWIND_INFO
366 static void initial_return_save (rtx);
367 #endif
368 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
369 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
370 static void output_call_frame_info (int);
371 static void dwarf2out_note_section_used (void);
372 static void dwarf2out_stack_adjust (rtx, bool);
373 static void flush_queued_reg_saves (void);
374 static bool clobbers_queued_reg_save (const_rtx);
375 static void dwarf2out_frame_debug_expr (rtx, const char *);
376
377 /* Support for complex CFA locations.  */
378 static void output_cfa_loc (dw_cfi_ref);
379 static void get_cfa_from_loc_descr (dw_cfa_location *,
380                                     struct dw_loc_descr_struct *);
381 static struct dw_loc_descr_struct *build_cfa_loc
382   (dw_cfa_location *, HOST_WIDE_INT);
383 static void def_cfa_1 (const char *, dw_cfa_location *);
384
385 /* How to start an assembler comment.  */
386 #ifndef ASM_COMMENT_START
387 #define ASM_COMMENT_START ";#"
388 #endif
389
390 /* Data and reference forms for relocatable data.  */
391 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
392 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
393
394 #ifndef DEBUG_FRAME_SECTION
395 #define DEBUG_FRAME_SECTION     ".debug_frame"
396 #endif
397
398 #ifndef FUNC_BEGIN_LABEL
399 #define FUNC_BEGIN_LABEL        "LFB"
400 #endif
401
402 #ifndef FUNC_END_LABEL
403 #define FUNC_END_LABEL          "LFE"
404 #endif
405
406 #ifndef FRAME_BEGIN_LABEL
407 #define FRAME_BEGIN_LABEL       "Lframe"
408 #endif
409 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
410 #define CIE_END_LABEL           "LECIE"
411 #define FDE_LABEL               "LSFDE"
412 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
413 #define FDE_END_LABEL           "LEFDE"
414 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
415 #define LINE_NUMBER_END_LABEL   "LELT"
416 #define LN_PROLOG_AS_LABEL      "LASLTP"
417 #define LN_PROLOG_END_LABEL     "LELTP"
418 #define DIE_LABEL_PREFIX        "DW"
419
420 /* The DWARF 2 CFA column which tracks the return address.  Normally this
421    is the column for PC, or the first column after all of the hard
422    registers.  */
423 #ifndef DWARF_FRAME_RETURN_COLUMN
424 #ifdef PC_REGNUM
425 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
426 #else
427 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
428 #endif
429 #endif
430
431 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
432    default, we just provide columns for all registers.  */
433 #ifndef DWARF_FRAME_REGNUM
434 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
435 #endif
436 \f
437 /* Hook used by __throw.  */
438
439 rtx
440 expand_builtin_dwarf_sp_column (void)
441 {
442   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
443   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
444 }
445
446 /* Return a pointer to a copy of the section string name S with all
447    attributes stripped off, and an asterisk prepended (for assemble_name).  */
448
449 static inline char *
450 stripattributes (const char *s)
451 {
452   char *stripped = XNEWVEC (char, strlen (s) + 2);
453   char *p = stripped;
454
455   *p++ = '*';
456
457   while (*s && *s != ',')
458     *p++ = *s++;
459
460   *p = '\0';
461   return stripped;
462 }
463
464 /* MEM is a memory reference for the register size table, each element of
465    which has mode MODE.  Initialize column C as a return address column.  */
466
467 static void
468 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
469 {
470   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
471   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
472   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
473 }
474
475 /* Generate code to initialize the register size table.  */
476
477 void
478 expand_builtin_init_dwarf_reg_sizes (tree address)
479 {
480   unsigned int i;
481   enum machine_mode mode = TYPE_MODE (char_type_node);
482   rtx addr = expand_normal (address);
483   rtx mem = gen_rtx_MEM (BLKmode, addr);
484   bool wrote_return_column = false;
485
486   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
487     {
488       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
489
490       if (rnum < DWARF_FRAME_REGISTERS)
491         {
492           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
493           enum machine_mode save_mode = reg_raw_mode[i];
494           HOST_WIDE_INT size;
495
496           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
497             save_mode = choose_hard_reg_mode (i, 1, true);
498           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
499             {
500               if (save_mode == VOIDmode)
501                 continue;
502               wrote_return_column = true;
503             }
504           size = GET_MODE_SIZE (save_mode);
505           if (offset < 0)
506             continue;
507
508           emit_move_insn (adjust_address (mem, mode, offset),
509                           gen_int_mode (size, mode));
510         }
511     }
512
513   if (!wrote_return_column)
514     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
515
516 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
517   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
518 #endif
519
520   targetm.init_dwarf_reg_sizes_extra (address);
521 }
522
523 /* Convert a DWARF call frame info. operation to its string name */
524
525 static const char *
526 dwarf_cfi_name (unsigned int cfi_opc)
527 {
528   switch (cfi_opc)
529     {
530     case DW_CFA_advance_loc:
531       return "DW_CFA_advance_loc";
532     case DW_CFA_offset:
533       return "DW_CFA_offset";
534     case DW_CFA_restore:
535       return "DW_CFA_restore";
536     case DW_CFA_nop:
537       return "DW_CFA_nop";
538     case DW_CFA_set_loc:
539       return "DW_CFA_set_loc";
540     case DW_CFA_advance_loc1:
541       return "DW_CFA_advance_loc1";
542     case DW_CFA_advance_loc2:
543       return "DW_CFA_advance_loc2";
544     case DW_CFA_advance_loc4:
545       return "DW_CFA_advance_loc4";
546     case DW_CFA_offset_extended:
547       return "DW_CFA_offset_extended";
548     case DW_CFA_restore_extended:
549       return "DW_CFA_restore_extended";
550     case DW_CFA_undefined:
551       return "DW_CFA_undefined";
552     case DW_CFA_same_value:
553       return "DW_CFA_same_value";
554     case DW_CFA_register:
555       return "DW_CFA_register";
556     case DW_CFA_remember_state:
557       return "DW_CFA_remember_state";
558     case DW_CFA_restore_state:
559       return "DW_CFA_restore_state";
560     case DW_CFA_def_cfa:
561       return "DW_CFA_def_cfa";
562     case DW_CFA_def_cfa_register:
563       return "DW_CFA_def_cfa_register";
564     case DW_CFA_def_cfa_offset:
565       return "DW_CFA_def_cfa_offset";
566
567     /* DWARF 3 */
568     case DW_CFA_def_cfa_expression:
569       return "DW_CFA_def_cfa_expression";
570     case DW_CFA_expression:
571       return "DW_CFA_expression";
572     case DW_CFA_offset_extended_sf:
573       return "DW_CFA_offset_extended_sf";
574     case DW_CFA_def_cfa_sf:
575       return "DW_CFA_def_cfa_sf";
576     case DW_CFA_def_cfa_offset_sf:
577       return "DW_CFA_def_cfa_offset_sf";
578
579     /* SGI/MIPS specific */
580     case DW_CFA_MIPS_advance_loc8:
581       return "DW_CFA_MIPS_advance_loc8";
582
583     /* GNU extensions */
584     case DW_CFA_GNU_window_save:
585       return "DW_CFA_GNU_window_save";
586     case DW_CFA_GNU_args_size:
587       return "DW_CFA_GNU_args_size";
588     case DW_CFA_GNU_negative_offset_extended:
589       return "DW_CFA_GNU_negative_offset_extended";
590
591     default:
592       return "DW_CFA_<unknown>";
593     }
594 }
595
596 /* Return a pointer to a newly allocated Call Frame Instruction.  */
597
598 static inline dw_cfi_ref
599 new_cfi (void)
600 {
601   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
602
603   cfi->dw_cfi_next = NULL;
604   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
605   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
606
607   return cfi;
608 }
609
610 /* Add a Call Frame Instruction to list of instructions.  */
611
612 static inline void
613 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
614 {
615   dw_cfi_ref *p;
616
617   /* Find the end of the chain.  */
618   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
619     ;
620
621   *p = cfi;
622 }
623
624 /* Generate a new label for the CFI info to refer to.  */
625
626 char *
627 dwarf2out_cfi_label (void)
628 {
629   static char label[20];
630
631   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
632   ASM_OUTPUT_LABEL (asm_out_file, label);
633   return label;
634 }
635
636 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
637    or to the CIE if LABEL is NULL.  */
638
639 static void
640 add_fde_cfi (const char *label, dw_cfi_ref cfi)
641 {
642   if (label)
643     {
644       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
645
646       if (*label == 0)
647         label = dwarf2out_cfi_label ();
648
649       if (fde->dw_fde_current_label == NULL
650           || strcmp (label, fde->dw_fde_current_label) != 0)
651         {
652           dw_cfi_ref xcfi;
653
654           label = xstrdup (label);
655
656           /* Set the location counter to the new label.  */
657           xcfi = new_cfi ();
658           /* If we have a current label, advance from there, otherwise
659              set the location directly using set_loc.  */
660           xcfi->dw_cfi_opc = fde->dw_fde_current_label
661                              ? DW_CFA_advance_loc4
662                              : DW_CFA_set_loc;
663           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
664           add_cfi (&fde->dw_fde_cfi, xcfi);
665
666           fde->dw_fde_current_label = label;
667         }
668
669       add_cfi (&fde->dw_fde_cfi, cfi);
670     }
671
672   else
673     add_cfi (&cie_cfi_head, cfi);
674 }
675
676 /* Subroutine of lookup_cfa.  */
677
678 static void
679 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
680 {
681   switch (cfi->dw_cfi_opc)
682     {
683     case DW_CFA_def_cfa_offset:
684       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
685       break;
686     case DW_CFA_def_cfa_offset_sf:
687       loc->offset
688         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689       break;
690     case DW_CFA_def_cfa_register:
691       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
692       break;
693     case DW_CFA_def_cfa:
694       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
695       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
696       break;
697     case DW_CFA_def_cfa_sf:
698       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
699       loc->offset
700         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
701       break;
702     case DW_CFA_def_cfa_expression:
703       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
704       break;
705     default:
706       break;
707     }
708 }
709
710 /* Find the previous value for the CFA.  */
711
712 static void
713 lookup_cfa (dw_cfa_location *loc)
714 {
715   dw_cfi_ref cfi;
716
717   loc->reg = INVALID_REGNUM;
718   loc->offset = 0;
719   loc->indirect = 0;
720   loc->base_offset = 0;
721
722   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
723     lookup_cfa_1 (cfi, loc);
724
725   if (fde_table_in_use)
726     {
727       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
728       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
729         lookup_cfa_1 (cfi, loc);
730     }
731 }
732
733 /* The current rule for calculating the DWARF2 canonical frame address.  */
734 static dw_cfa_location cfa;
735
736 /* The register used for saving registers to the stack, and its offset
737    from the CFA.  */
738 static dw_cfa_location cfa_store;
739
740 /* The running total of the size of arguments pushed onto the stack.  */
741 static HOST_WIDE_INT args_size;
742
743 /* The last args_size we actually output.  */
744 static HOST_WIDE_INT old_args_size;
745
746 /* Entry point to update the canonical frame address (CFA).
747    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
748    calculated from REG+OFFSET.  */
749
750 void
751 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
752 {
753   dw_cfa_location loc;
754   loc.indirect = 0;
755   loc.base_offset = 0;
756   loc.reg = reg;
757   loc.offset = offset;
758   def_cfa_1 (label, &loc);
759 }
760
761 /* Determine if two dw_cfa_location structures define the same data.  */
762
763 static bool
764 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
765 {
766   return (loc1->reg == loc2->reg
767           && loc1->offset == loc2->offset
768           && loc1->indirect == loc2->indirect
769           && (loc1->indirect == 0
770               || loc1->base_offset == loc2->base_offset));
771 }
772
773 /* This routine does the actual work.  The CFA is now calculated from
774    the dw_cfa_location structure.  */
775
776 static void
777 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
778 {
779   dw_cfi_ref cfi;
780   dw_cfa_location old_cfa, loc;
781
782   cfa = *loc_p;
783   loc = *loc_p;
784
785   if (cfa_store.reg == loc.reg && loc.indirect == 0)
786     cfa_store.offset = loc.offset;
787
788   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
789   lookup_cfa (&old_cfa);
790
791   /* If nothing changed, no need to issue any call frame instructions.  */
792   if (cfa_equal_p (&loc, &old_cfa))
793     return;
794
795   cfi = new_cfi ();
796
797   if (loc.reg == old_cfa.reg && !loc.indirect)
798     {
799       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
800          the CFA register did not change but the offset did.  */
801       if (loc.offset < 0)
802         {
803           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
804           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
805
806           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
807           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
808         }
809       else
810         {
811           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
812           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
813         }
814     }
815
816 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
817   else if (loc.offset == old_cfa.offset
818            && old_cfa.reg != INVALID_REGNUM
819            && !loc.indirect)
820     {
821       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
822          indicating the CFA register has changed to <register> but the
823          offset has not changed.  */
824       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
825       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
826     }
827 #endif
828
829   else if (loc.indirect == 0)
830     {
831       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
832          indicating the CFA register has changed to <register> with
833          the specified offset.  */
834       if (loc.offset < 0)
835         {
836           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
837           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
838
839           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
840           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
841           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
842         }
843       else
844         {
845           cfi->dw_cfi_opc = DW_CFA_def_cfa;
846           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
847           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
848         }
849     }
850   else
851     {
852       /* Construct a DW_CFA_def_cfa_expression instruction to
853          calculate the CFA using a full location expression since no
854          register-offset pair is available.  */
855       struct dw_loc_descr_struct *loc_list;
856
857       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
858       loc_list = build_cfa_loc (&loc, 0);
859       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
860     }
861
862   add_fde_cfi (label, cfi);
863 }
864
865 /* Add the CFI for saving a register.  REG is the CFA column number.
866    LABEL is passed to add_fde_cfi.
867    If SREG is -1, the register is saved at OFFSET from the CFA;
868    otherwise it is saved in SREG.  */
869
870 static void
871 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
872 {
873   dw_cfi_ref cfi = new_cfi ();
874
875   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
876
877   if (sreg == INVALID_REGNUM)
878     {
879       if (reg & ~0x3f)
880         /* The register number won't fit in 6 bits, so we have to use
881            the long form.  */
882         cfi->dw_cfi_opc = DW_CFA_offset_extended;
883       else
884         cfi->dw_cfi_opc = DW_CFA_offset;
885
886 #ifdef ENABLE_CHECKING
887       {
888         /* If we get an offset that is not a multiple of
889            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
890            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
891            description.  */
892         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
893
894         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
895       }
896 #endif
897       offset /= DWARF_CIE_DATA_ALIGNMENT;
898       if (offset < 0)
899         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
900
901       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
902     }
903   else if (sreg == reg)
904     cfi->dw_cfi_opc = DW_CFA_same_value;
905   else
906     {
907       cfi->dw_cfi_opc = DW_CFA_register;
908       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
909     }
910
911   add_fde_cfi (label, cfi);
912 }
913
914 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
915    This CFI tells the unwinder that it needs to restore the window registers
916    from the previous frame's window save area.
917
918    ??? Perhaps we should note in the CIE where windows are saved (instead of
919    assuming 0(cfa)) and what registers are in the window.  */
920
921 void
922 dwarf2out_window_save (const char *label)
923 {
924   dw_cfi_ref cfi = new_cfi ();
925
926   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
927   add_fde_cfi (label, cfi);
928 }
929
930 /* Add a CFI to update the running total of the size of arguments
931    pushed onto the stack.  */
932
933 void
934 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
935 {
936   dw_cfi_ref cfi;
937
938   if (size == old_args_size)
939     return;
940
941   old_args_size = size;
942
943   cfi = new_cfi ();
944   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
945   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
946   add_fde_cfi (label, cfi);
947 }
948
949 /* Entry point for saving a register to the stack.  REG is the GCC register
950    number.  LABEL and OFFSET are passed to reg_save.  */
951
952 void
953 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
954 {
955   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
956 }
957
958 /* Entry point for saving the return address in the stack.
959    LABEL and OFFSET are passed to reg_save.  */
960
961 void
962 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
963 {
964   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
965 }
966
967 /* Entry point for saving the return address in a register.
968    LABEL and SREG are passed to reg_save.  */
969
970 void
971 dwarf2out_return_reg (const char *label, unsigned int sreg)
972 {
973   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
974 }
975
976 #ifdef DWARF2_UNWIND_INFO
977 /* Record the initial position of the return address.  RTL is
978    INCOMING_RETURN_ADDR_RTX.  */
979
980 static void
981 initial_return_save (rtx rtl)
982 {
983   unsigned int reg = INVALID_REGNUM;
984   HOST_WIDE_INT offset = 0;
985
986   switch (GET_CODE (rtl))
987     {
988     case REG:
989       /* RA is in a register.  */
990       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
991       break;
992
993     case MEM:
994       /* RA is on the stack.  */
995       rtl = XEXP (rtl, 0);
996       switch (GET_CODE (rtl))
997         {
998         case REG:
999           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1000           offset = 0;
1001           break;
1002
1003         case PLUS:
1004           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1005           offset = INTVAL (XEXP (rtl, 1));
1006           break;
1007
1008         case MINUS:
1009           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1010           offset = -INTVAL (XEXP (rtl, 1));
1011           break;
1012
1013         default:
1014           gcc_unreachable ();
1015         }
1016
1017       break;
1018
1019     case PLUS:
1020       /* The return address is at some offset from any value we can
1021          actually load.  For instance, on the SPARC it is in %i7+8. Just
1022          ignore the offset for now; it doesn't matter for unwinding frames.  */
1023       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1024       initial_return_save (XEXP (rtl, 0));
1025       return;
1026
1027     default:
1028       gcc_unreachable ();
1029     }
1030
1031   if (reg != DWARF_FRAME_RETURN_COLUMN)
1032     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1033 }
1034 #endif
1035
1036 /* Given a SET, calculate the amount of stack adjustment it
1037    contains.  */
1038
1039 static HOST_WIDE_INT
1040 stack_adjust_offset (const_rtx pattern)
1041 {
1042   const_rtx src = SET_SRC (pattern);
1043   const_rtx dest = SET_DEST (pattern);
1044   HOST_WIDE_INT offset = 0;
1045   enum rtx_code code;
1046
1047   if (dest == stack_pointer_rtx)
1048     {
1049       /* (set (reg sp) (plus (reg sp) (const_int))) */
1050       code = GET_CODE (src);
1051       if (! (code == PLUS || code == MINUS)
1052           || XEXP (src, 0) != stack_pointer_rtx
1053           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1054         return 0;
1055
1056       offset = INTVAL (XEXP (src, 1));
1057       if (code == PLUS)
1058         offset = -offset;
1059     }
1060   else if (MEM_P (dest))
1061     {
1062       /* (set (mem (pre_dec (reg sp))) (foo)) */
1063       src = XEXP (dest, 0);
1064       code = GET_CODE (src);
1065
1066       switch (code)
1067         {
1068         case PRE_MODIFY:
1069         case POST_MODIFY:
1070           if (XEXP (src, 0) == stack_pointer_rtx)
1071             {
1072               rtx val = XEXP (XEXP (src, 1), 1);
1073               /* We handle only adjustments by constant amount.  */
1074               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1075                           && GET_CODE (val) == CONST_INT);
1076               offset = -INTVAL (val);
1077               break;
1078             }
1079           return 0;
1080
1081         case PRE_DEC:
1082         case POST_DEC:
1083           if (XEXP (src, 0) == stack_pointer_rtx)
1084             {
1085               offset = GET_MODE_SIZE (GET_MODE (dest));
1086               break;
1087             }
1088           return 0;
1089
1090         case PRE_INC:
1091         case POST_INC:
1092           if (XEXP (src, 0) == stack_pointer_rtx)
1093             {
1094               offset = -GET_MODE_SIZE (GET_MODE (dest));
1095               break;
1096             }
1097           return 0;
1098
1099         default:
1100           return 0;
1101         }
1102     }
1103   else
1104     return 0;
1105
1106   return offset;
1107 }
1108
1109 /* Check INSN to see if it looks like a push or a stack adjustment, and
1110    make a note of it if it does.  EH uses this information to find out how
1111    much extra space it needs to pop off the stack.  */
1112
1113 static void
1114 dwarf2out_stack_adjust (rtx insn, bool after_p)
1115 {
1116   HOST_WIDE_INT offset;
1117   const char *label;
1118   int i;
1119
1120   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1121      with this function.  Proper support would require all frame-related
1122      insns to be marked, and to be able to handle saving state around
1123      epilogues textually in the middle of the function.  */
1124   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1125     return;
1126
1127   /* If only calls can throw, and we have a frame pointer,
1128      save up adjustments until we see the CALL_INSN.  */
1129   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1130     {
1131       if (CALL_P (insn) && !after_p)
1132         {
1133           /* Extract the size of the args from the CALL rtx itself.  */
1134           insn = PATTERN (insn);
1135           if (GET_CODE (insn) == PARALLEL)
1136             insn = XVECEXP (insn, 0, 0);
1137           if (GET_CODE (insn) == SET)
1138             insn = SET_SRC (insn);
1139           gcc_assert (GET_CODE (insn) == CALL);
1140           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1141         }
1142       return;
1143     }
1144
1145   if (CALL_P (insn) && !after_p)
1146     {
1147       if (!flag_asynchronous_unwind_tables)
1148         dwarf2out_args_size ("", args_size);
1149       return;
1150     }
1151   else if (BARRIER_P (insn))
1152     {
1153       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1154          the compiler will have already emitted a stack adjustment, but
1155          doesn't bother for calls to noreturn functions.  */
1156 #ifdef STACK_GROWS_DOWNWARD
1157       offset = -args_size;
1158 #else
1159       offset = args_size;
1160 #endif
1161     }
1162   else if (GET_CODE (PATTERN (insn)) == SET)
1163     offset = stack_adjust_offset (PATTERN (insn));
1164   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1165            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1166     {
1167       /* There may be stack adjustments inside compound insns.  Search
1168          for them.  */
1169       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1170         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1171           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1172     }
1173   else
1174     return;
1175
1176   if (offset == 0)
1177     return;
1178
1179   if (cfa.reg == STACK_POINTER_REGNUM)
1180     cfa.offset += offset;
1181
1182 #ifndef STACK_GROWS_DOWNWARD
1183   offset = -offset;
1184 #endif
1185
1186   args_size += offset;
1187   if (args_size < 0)
1188     args_size = 0;
1189
1190   label = dwarf2out_cfi_label ();
1191   def_cfa_1 (label, &cfa);
1192   if (flag_asynchronous_unwind_tables)
1193     dwarf2out_args_size (label, args_size);
1194 }
1195
1196 #endif
1197
1198 /* We delay emitting a register save until either (a) we reach the end
1199    of the prologue or (b) the register is clobbered.  This clusters
1200    register saves so that there are fewer pc advances.  */
1201
1202 struct queued_reg_save GTY(())
1203 {
1204   struct queued_reg_save *next;
1205   rtx reg;
1206   HOST_WIDE_INT cfa_offset;
1207   rtx saved_reg;
1208 };
1209
1210 static GTY(()) struct queued_reg_save *queued_reg_saves;
1211
1212 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1213 struct reg_saved_in_data GTY(()) {
1214   rtx orig_reg;
1215   rtx saved_in_reg;
1216 };
1217
1218 /* A list of registers saved in other registers.
1219    The list intentionally has a small maximum capacity of 4; if your
1220    port needs more than that, you might consider implementing a
1221    more efficient data structure.  */
1222 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1223 static GTY(()) size_t num_regs_saved_in_regs;
1224
1225 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1226 static const char *last_reg_save_label;
1227
1228 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1229    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1230
1231 static void
1232 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1233 {
1234   struct queued_reg_save *q;
1235
1236   /* Duplicates waste space, but it's also necessary to remove them
1237      for correctness, since the queue gets output in reverse
1238      order.  */
1239   for (q = queued_reg_saves; q != NULL; q = q->next)
1240     if (REGNO (q->reg) == REGNO (reg))
1241       break;
1242
1243   if (q == NULL)
1244     {
1245       q = ggc_alloc (sizeof (*q));
1246       q->next = queued_reg_saves;
1247       queued_reg_saves = q;
1248     }
1249
1250   q->reg = reg;
1251   q->cfa_offset = offset;
1252   q->saved_reg = sreg;
1253
1254   last_reg_save_label = label;
1255 }
1256
1257 /* Output all the entries in QUEUED_REG_SAVES.  */
1258
1259 static void
1260 flush_queued_reg_saves (void)
1261 {
1262   struct queued_reg_save *q;
1263
1264   for (q = queued_reg_saves; q; q = q->next)
1265     {
1266       size_t i;
1267       unsigned int reg, sreg;
1268
1269       for (i = 0; i < num_regs_saved_in_regs; i++)
1270         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1271           break;
1272       if (q->saved_reg && i == num_regs_saved_in_regs)
1273         {
1274           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1275           num_regs_saved_in_regs++;
1276         }
1277       if (i != num_regs_saved_in_regs)
1278         {
1279           regs_saved_in_regs[i].orig_reg = q->reg;
1280           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1281         }
1282
1283       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1284       if (q->saved_reg)
1285         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1286       else
1287         sreg = INVALID_REGNUM;
1288       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1289     }
1290
1291   queued_reg_saves = NULL;
1292   last_reg_save_label = NULL;
1293 }
1294
1295 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1296    location for?  Or, does it clobber a register which we've previously
1297    said that some other register is saved in, and for which we now
1298    have a new location for?  */
1299
1300 static bool
1301 clobbers_queued_reg_save (const_rtx insn)
1302 {
1303   struct queued_reg_save *q;
1304
1305   for (q = queued_reg_saves; q; q = q->next)
1306     {
1307       size_t i;
1308       if (modified_in_p (q->reg, insn))
1309         return true;
1310       for (i = 0; i < num_regs_saved_in_regs; i++)
1311         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1312             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1313           return true;
1314     }
1315
1316   return false;
1317 }
1318
1319 /* Entry point for saving the first register into the second.  */
1320
1321 void
1322 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1323 {
1324   size_t i;
1325   unsigned int regno, sregno;
1326
1327   for (i = 0; i < num_regs_saved_in_regs; i++)
1328     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1329       break;
1330   if (i == num_regs_saved_in_regs)
1331     {
1332       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1333       num_regs_saved_in_regs++;
1334     }
1335   regs_saved_in_regs[i].orig_reg = reg;
1336   regs_saved_in_regs[i].saved_in_reg = sreg;
1337
1338   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1339   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1340   reg_save (label, regno, sregno, 0);
1341 }
1342
1343 /* What register, if any, is currently saved in REG?  */
1344
1345 static rtx
1346 reg_saved_in (rtx reg)
1347 {
1348   unsigned int regn = REGNO (reg);
1349   size_t i;
1350   struct queued_reg_save *q;
1351
1352   for (q = queued_reg_saves; q; q = q->next)
1353     if (q->saved_reg && regn == REGNO (q->saved_reg))
1354       return q->reg;
1355
1356   for (i = 0; i < num_regs_saved_in_regs; i++)
1357     if (regs_saved_in_regs[i].saved_in_reg
1358         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1359       return regs_saved_in_regs[i].orig_reg;
1360
1361   return NULL_RTX;
1362 }
1363
1364
1365 /* A temporary register holding an integral value used in adjusting SP
1366    or setting up the store_reg.  The "offset" field holds the integer
1367    value, not an offset.  */
1368 static dw_cfa_location cfa_temp;
1369
1370 /* Record call frame debugging information for an expression EXPR,
1371    which either sets SP or FP (adjusting how we calculate the frame
1372    address) or saves a register to the stack or another register.
1373    LABEL indicates the address of EXPR.
1374
1375    This function encodes a state machine mapping rtxes to actions on
1376    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1377    users need not read the source code.
1378
1379   The High-Level Picture
1380
1381   Changes in the register we use to calculate the CFA: Currently we
1382   assume that if you copy the CFA register into another register, we
1383   should take the other one as the new CFA register; this seems to
1384   work pretty well.  If it's wrong for some target, it's simple
1385   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1386
1387   Changes in the register we use for saving registers to the stack:
1388   This is usually SP, but not always.  Again, we deduce that if you
1389   copy SP into another register (and SP is not the CFA register),
1390   then the new register is the one we will be using for register
1391   saves.  This also seems to work.
1392
1393   Register saves: There's not much guesswork about this one; if
1394   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1395   register save, and the register used to calculate the destination
1396   had better be the one we think we're using for this purpose.
1397   It's also assumed that a copy from a call-saved register to another
1398   register is saving that register if RTX_FRAME_RELATED_P is set on
1399   that instruction.  If the copy is from a call-saved register to
1400   the *same* register, that means that the register is now the same
1401   value as in the caller.
1402
1403   Except: If the register being saved is the CFA register, and the
1404   offset is nonzero, we are saving the CFA, so we assume we have to
1405   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1406   the intent is to save the value of SP from the previous frame.
1407
1408   In addition, if a register has previously been saved to a different
1409   register,
1410
1411   Invariants / Summaries of Rules
1412
1413   cfa          current rule for calculating the CFA.  It usually
1414                consists of a register and an offset.
1415   cfa_store    register used by prologue code to save things to the stack
1416                cfa_store.offset is the offset from the value of
1417                cfa_store.reg to the actual CFA
1418   cfa_temp     register holding an integral value.  cfa_temp.offset
1419                stores the value, which will be used to adjust the
1420                stack pointer.  cfa_temp is also used like cfa_store,
1421                to track stores to the stack via fp or a temp reg.
1422
1423   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1424                with cfa.reg as the first operand changes the cfa.reg and its
1425                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1426                cfa_temp.offset.
1427
1428   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1429                expression yielding a constant.  This sets cfa_temp.reg
1430                and cfa_temp.offset.
1431
1432   Rule 5:      Create a new register cfa_store used to save items to the
1433                stack.
1434
1435   Rules 10-14: Save a register to the stack.  Define offset as the
1436                difference of the original location and cfa_store's
1437                location (or cfa_temp's location if cfa_temp is used).
1438
1439   The Rules
1440
1441   "{a,b}" indicates a choice of a xor b.
1442   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1443
1444   Rule 1:
1445   (set <reg1> <reg2>:cfa.reg)
1446   effects: cfa.reg = <reg1>
1447            cfa.offset unchanged
1448            cfa_temp.reg = <reg1>
1449            cfa_temp.offset = cfa.offset
1450
1451   Rule 2:
1452   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1453                               {<const_int>,<reg>:cfa_temp.reg}))
1454   effects: cfa.reg = sp if fp used
1455            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1456            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1457              if cfa_store.reg==sp
1458
1459   Rule 3:
1460   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1461   effects: cfa.reg = fp
1462            cfa_offset += +/- <const_int>
1463
1464   Rule 4:
1465   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1466   constraints: <reg1> != fp
1467                <reg1> != sp
1468   effects: cfa.reg = <reg1>
1469            cfa_temp.reg = <reg1>
1470            cfa_temp.offset = cfa.offset
1471
1472   Rule 5:
1473   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1474   constraints: <reg1> != fp
1475                <reg1> != sp
1476   effects: cfa_store.reg = <reg1>
1477            cfa_store.offset = cfa.offset - cfa_temp.offset
1478
1479   Rule 6:
1480   (set <reg> <const_int>)
1481   effects: cfa_temp.reg = <reg>
1482            cfa_temp.offset = <const_int>
1483
1484   Rule 7:
1485   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1486   effects: cfa_temp.reg = <reg1>
1487            cfa_temp.offset |= <const_int>
1488
1489   Rule 8:
1490   (set <reg> (high <exp>))
1491   effects: none
1492
1493   Rule 9:
1494   (set <reg> (lo_sum <exp> <const_int>))
1495   effects: cfa_temp.reg = <reg>
1496            cfa_temp.offset = <const_int>
1497
1498   Rule 10:
1499   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1500   effects: cfa_store.offset -= <const_int>
1501            cfa.offset = cfa_store.offset if cfa.reg == sp
1502            cfa.reg = sp
1503            cfa.base_offset = -cfa_store.offset
1504
1505   Rule 11:
1506   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1507   effects: cfa_store.offset += -/+ mode_size(mem)
1508            cfa.offset = cfa_store.offset if cfa.reg == sp
1509            cfa.reg = sp
1510            cfa.base_offset = -cfa_store.offset
1511
1512   Rule 12:
1513   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1514
1515        <reg2>)
1516   effects: cfa.reg = <reg1>
1517            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1518
1519   Rule 13:
1520   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1521   effects: cfa.reg = <reg1>
1522            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1523
1524   Rule 14:
1525   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1526   effects: cfa.reg = <reg1>
1527            cfa.base_offset = -cfa_temp.offset
1528            cfa_temp.offset -= mode_size(mem)
1529
1530   Rule 15:
1531   (set <reg> {unspec, unspec_volatile})
1532   effects: target-dependent  */
1533
1534 static void
1535 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1536 {
1537   rtx src, dest;
1538   HOST_WIDE_INT offset;
1539
1540   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1541      the PARALLEL independently. The first element is always processed if
1542      it is a SET. This is for backward compatibility.   Other elements
1543      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1544      flag is set in them.  */
1545   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1546     {
1547       int par_index;
1548       int limit = XVECLEN (expr, 0);
1549       rtx elem;
1550
1551       /* PARALLELs have strict read-modify-write semantics, so we
1552          ought to evaluate every rvalue before changing any lvalue.
1553          It's cumbersome to do that in general, but there's an
1554          easy approximation that is enough for all current users:
1555          handle register saves before register assignments.  */
1556       if (GET_CODE (expr) == PARALLEL)
1557         for (par_index = 0; par_index < limit; par_index++)
1558           {
1559             elem = XVECEXP (expr, 0, par_index);
1560             if (GET_CODE (elem) == SET
1561                 && MEM_P (SET_DEST (elem))
1562                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1563               dwarf2out_frame_debug_expr (elem, label);
1564           }
1565
1566       for (par_index = 0; par_index < limit; par_index++)
1567         {
1568           elem = XVECEXP (expr, 0, par_index);
1569           if (GET_CODE (elem) == SET
1570               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1571               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1572             dwarf2out_frame_debug_expr (elem, label);
1573         }
1574       return;
1575     }
1576
1577   gcc_assert (GET_CODE (expr) == SET);
1578
1579   src = SET_SRC (expr);
1580   dest = SET_DEST (expr);
1581
1582   if (REG_P (src))
1583     {
1584       rtx rsi = reg_saved_in (src);
1585       if (rsi)
1586         src = rsi;
1587     }
1588
1589   switch (GET_CODE (dest))
1590     {
1591     case REG:
1592       switch (GET_CODE (src))
1593         {
1594           /* Setting FP from SP.  */
1595         case REG:
1596           if (cfa.reg == (unsigned) REGNO (src))
1597             {
1598               /* Rule 1 */
1599               /* Update the CFA rule wrt SP or FP.  Make sure src is
1600                  relative to the current CFA register.
1601
1602                  We used to require that dest be either SP or FP, but the
1603                  ARM copies SP to a temporary register, and from there to
1604                  FP.  So we just rely on the backends to only set
1605                  RTX_FRAME_RELATED_P on appropriate insns.  */
1606               cfa.reg = REGNO (dest);
1607               cfa_temp.reg = cfa.reg;
1608               cfa_temp.offset = cfa.offset;
1609             }
1610           else
1611             {
1612               /* Saving a register in a register.  */
1613               gcc_assert (!fixed_regs [REGNO (dest)]
1614                           /* For the SPARC and its register window.  */
1615                           || (DWARF_FRAME_REGNUM (REGNO (src))
1616                               == DWARF_FRAME_RETURN_COLUMN));
1617               queue_reg_save (label, src, dest, 0);
1618             }
1619           break;
1620
1621         case PLUS:
1622         case MINUS:
1623         case LO_SUM:
1624           if (dest == stack_pointer_rtx)
1625             {
1626               /* Rule 2 */
1627               /* Adjusting SP.  */
1628               switch (GET_CODE (XEXP (src, 1)))
1629                 {
1630                 case CONST_INT:
1631                   offset = INTVAL (XEXP (src, 1));
1632                   break;
1633                 case REG:
1634                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1635                               == cfa_temp.reg);
1636                   offset = cfa_temp.offset;
1637                   break;
1638                 default:
1639                   gcc_unreachable ();
1640                 }
1641
1642               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1643                 {
1644                   /* Restoring SP from FP in the epilogue.  */
1645                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1646                   cfa.reg = STACK_POINTER_REGNUM;
1647                 }
1648               else if (GET_CODE (src) == LO_SUM)
1649                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1650                 ;
1651               else
1652                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1653
1654               if (GET_CODE (src) != MINUS)
1655                 offset = -offset;
1656               if (cfa.reg == STACK_POINTER_REGNUM)
1657                 cfa.offset += offset;
1658               if (cfa_store.reg == STACK_POINTER_REGNUM)
1659                 cfa_store.offset += offset;
1660             }
1661           else if (dest == hard_frame_pointer_rtx)
1662             {
1663               /* Rule 3 */
1664               /* Either setting the FP from an offset of the SP,
1665                  or adjusting the FP */
1666               gcc_assert (frame_pointer_needed);
1667
1668               gcc_assert (REG_P (XEXP (src, 0))
1669                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1670                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1671               offset = INTVAL (XEXP (src, 1));
1672               if (GET_CODE (src) != MINUS)
1673                 offset = -offset;
1674               cfa.offset += offset;
1675               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1676             }
1677           else
1678             {
1679               gcc_assert (GET_CODE (src) != MINUS);
1680
1681               /* Rule 4 */
1682               if (REG_P (XEXP (src, 0))
1683                   && REGNO (XEXP (src, 0)) == cfa.reg
1684                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1685                 {
1686                   /* Setting a temporary CFA register that will be copied
1687                      into the FP later on.  */
1688                   offset = - INTVAL (XEXP (src, 1));
1689                   cfa.offset += offset;
1690                   cfa.reg = REGNO (dest);
1691                   /* Or used to save regs to the stack.  */
1692                   cfa_temp.reg = cfa.reg;
1693                   cfa_temp.offset = cfa.offset;
1694                 }
1695
1696               /* Rule 5 */
1697               else if (REG_P (XEXP (src, 0))
1698                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1699                        && XEXP (src, 1) == stack_pointer_rtx)
1700                 {
1701                   /* Setting a scratch register that we will use instead
1702                      of SP for saving registers to the stack.  */
1703                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1704                   cfa_store.reg = REGNO (dest);
1705                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1706                 }
1707
1708               /* Rule 9 */
1709               else if (GET_CODE (src) == LO_SUM
1710                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1711                 {
1712                   cfa_temp.reg = REGNO (dest);
1713                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1714                 }
1715               else
1716                 gcc_unreachable ();
1717             }
1718           break;
1719
1720           /* Rule 6 */
1721         case CONST_INT:
1722           cfa_temp.reg = REGNO (dest);
1723           cfa_temp.offset = INTVAL (src);
1724           break;
1725
1726           /* Rule 7 */
1727         case IOR:
1728           gcc_assert (REG_P (XEXP (src, 0))
1729                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1730                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1731
1732           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1733             cfa_temp.reg = REGNO (dest);
1734           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1735           break;
1736
1737           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1738              which will fill in all of the bits.  */
1739           /* Rule 8 */
1740         case HIGH:
1741           break;
1742
1743           /* Rule 15 */
1744         case UNSPEC:
1745         case UNSPEC_VOLATILE:
1746           gcc_assert (targetm.dwarf_handle_frame_unspec);
1747           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1748           return;
1749
1750         default:
1751           gcc_unreachable ();
1752         }
1753
1754       def_cfa_1 (label, &cfa);
1755       break;
1756
1757     case MEM:
1758       gcc_assert (REG_P (src));
1759
1760       /* Saving a register to the stack.  Make sure dest is relative to the
1761          CFA register.  */
1762       switch (GET_CODE (XEXP (dest, 0)))
1763         {
1764           /* Rule 10 */
1765           /* With a push.  */
1766         case PRE_MODIFY:
1767           /* We can't handle variable size modifications.  */
1768           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1769                       == CONST_INT);
1770           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1771
1772           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1773                       && cfa_store.reg == STACK_POINTER_REGNUM);
1774
1775           cfa_store.offset += offset;
1776           if (cfa.reg == STACK_POINTER_REGNUM)
1777             cfa.offset = cfa_store.offset;
1778
1779           offset = -cfa_store.offset;
1780           break;
1781
1782           /* Rule 11 */
1783         case PRE_INC:
1784         case PRE_DEC:
1785           offset = GET_MODE_SIZE (GET_MODE (dest));
1786           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1787             offset = -offset;
1788
1789           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1790                       && cfa_store.reg == STACK_POINTER_REGNUM);
1791
1792           cfa_store.offset += offset;
1793           if (cfa.reg == STACK_POINTER_REGNUM)
1794             cfa.offset = cfa_store.offset;
1795
1796           offset = -cfa_store.offset;
1797           break;
1798
1799           /* Rule 12 */
1800           /* With an offset.  */
1801         case PLUS:
1802         case MINUS:
1803         case LO_SUM:
1804           {
1805             int regno;
1806
1807             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1808                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1809             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1810             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1811               offset = -offset;
1812
1813             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1814
1815             if (cfa_store.reg == (unsigned) regno)
1816               offset -= cfa_store.offset;
1817             else
1818               {
1819                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1820                 offset -= cfa_temp.offset;
1821               }
1822           }
1823           break;
1824
1825           /* Rule 13 */
1826           /* Without an offset.  */
1827         case REG:
1828           {
1829             int regno = REGNO (XEXP (dest, 0));
1830
1831             if (cfa_store.reg == (unsigned) regno)
1832               offset = -cfa_store.offset;
1833             else
1834               {
1835                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1836                 offset = -cfa_temp.offset;
1837               }
1838           }
1839           break;
1840
1841           /* Rule 14 */
1842         case POST_INC:
1843           gcc_assert (cfa_temp.reg
1844                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1845           offset = -cfa_temp.offset;
1846           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1847           break;
1848
1849         default:
1850           gcc_unreachable ();
1851         }
1852
1853       if (REGNO (src) != STACK_POINTER_REGNUM
1854           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1855           && (unsigned) REGNO (src) == cfa.reg)
1856         {
1857           /* We're storing the current CFA reg into the stack.  */
1858
1859           if (cfa.offset == 0)
1860             {
1861               /* If the source register is exactly the CFA, assume
1862                  we're saving SP like any other register; this happens
1863                  on the ARM.  */
1864               def_cfa_1 (label, &cfa);
1865               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1866               break;
1867             }
1868           else
1869             {
1870               /* Otherwise, we'll need to look in the stack to
1871                  calculate the CFA.  */
1872               rtx x = XEXP (dest, 0);
1873
1874               if (!REG_P (x))
1875                 x = XEXP (x, 0);
1876               gcc_assert (REG_P (x));
1877
1878               cfa.reg = REGNO (x);
1879               cfa.base_offset = offset;
1880               cfa.indirect = 1;
1881               def_cfa_1 (label, &cfa);
1882               break;
1883             }
1884         }
1885
1886       def_cfa_1 (label, &cfa);
1887       queue_reg_save (label, src, NULL_RTX, offset);
1888       break;
1889
1890     default:
1891       gcc_unreachable ();
1892     }
1893 }
1894
1895 /* Record call frame debugging information for INSN, which either
1896    sets SP or FP (adjusting how we calculate the frame address) or saves a
1897    register to the stack.  If INSN is NULL_RTX, initialize our state.
1898
1899    If AFTER_P is false, we're being called before the insn is emitted,
1900    otherwise after.  Call instructions get invoked twice.  */
1901
1902 void
1903 dwarf2out_frame_debug (rtx insn, bool after_p)
1904 {
1905   const char *label;
1906   rtx src;
1907
1908   if (insn == NULL_RTX)
1909     {
1910       size_t i;
1911
1912       /* Flush any queued register saves.  */
1913       flush_queued_reg_saves ();
1914
1915       /* Set up state for generating call frame debug info.  */
1916       lookup_cfa (&cfa);
1917       gcc_assert (cfa.reg
1918                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1919
1920       cfa.reg = STACK_POINTER_REGNUM;
1921       cfa_store = cfa;
1922       cfa_temp.reg = -1;
1923       cfa_temp.offset = 0;
1924
1925       for (i = 0; i < num_regs_saved_in_regs; i++)
1926         {
1927           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1928           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1929         }
1930       num_regs_saved_in_regs = 0;
1931       return;
1932     }
1933
1934   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1935     flush_queued_reg_saves ();
1936
1937   if (! RTX_FRAME_RELATED_P (insn))
1938     {
1939       if (!ACCUMULATE_OUTGOING_ARGS)
1940         dwarf2out_stack_adjust (insn, after_p);
1941       return;
1942     }
1943
1944   label = dwarf2out_cfi_label ();
1945   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1946   if (src)
1947     insn = XEXP (src, 0);
1948   else
1949     insn = PATTERN (insn);
1950
1951   dwarf2out_frame_debug_expr (insn, label);
1952 }
1953
1954 #endif
1955
1956 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1957 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1958  (enum dwarf_call_frame_info cfi);
1959
1960 static enum dw_cfi_oprnd_type
1961 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1962 {
1963   switch (cfi)
1964     {
1965     case DW_CFA_nop:
1966     case DW_CFA_GNU_window_save:
1967       return dw_cfi_oprnd_unused;
1968
1969     case DW_CFA_set_loc:
1970     case DW_CFA_advance_loc1:
1971     case DW_CFA_advance_loc2:
1972     case DW_CFA_advance_loc4:
1973     case DW_CFA_MIPS_advance_loc8:
1974       return dw_cfi_oprnd_addr;
1975
1976     case DW_CFA_offset:
1977     case DW_CFA_offset_extended:
1978     case DW_CFA_def_cfa:
1979     case DW_CFA_offset_extended_sf:
1980     case DW_CFA_def_cfa_sf:
1981     case DW_CFA_restore_extended:
1982     case DW_CFA_undefined:
1983     case DW_CFA_same_value:
1984     case DW_CFA_def_cfa_register:
1985     case DW_CFA_register:
1986       return dw_cfi_oprnd_reg_num;
1987
1988     case DW_CFA_def_cfa_offset:
1989     case DW_CFA_GNU_args_size:
1990     case DW_CFA_def_cfa_offset_sf:
1991       return dw_cfi_oprnd_offset;
1992
1993     case DW_CFA_def_cfa_expression:
1994     case DW_CFA_expression:
1995       return dw_cfi_oprnd_loc;
1996
1997     default:
1998       gcc_unreachable ();
1999     }
2000 }
2001
2002 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2003 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2004  (enum dwarf_call_frame_info cfi);
2005
2006 static enum dw_cfi_oprnd_type
2007 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2008 {
2009   switch (cfi)
2010     {
2011     case DW_CFA_def_cfa:
2012     case DW_CFA_def_cfa_sf:
2013     case DW_CFA_offset:
2014     case DW_CFA_offset_extended_sf:
2015     case DW_CFA_offset_extended:
2016       return dw_cfi_oprnd_offset;
2017
2018     case DW_CFA_register:
2019       return dw_cfi_oprnd_reg_num;
2020
2021     default:
2022       return dw_cfi_oprnd_unused;
2023     }
2024 }
2025
2026 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2027
2028 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2029    switch to the data section instead, and write out a synthetic label
2030    for collect2.  */
2031
2032 static void
2033 switch_to_eh_frame_section (void)
2034 {
2035   tree label;
2036
2037 #ifdef EH_FRAME_SECTION_NAME
2038   if (eh_frame_section == 0)
2039     {
2040       int flags;
2041
2042       if (EH_TABLES_CAN_BE_READ_ONLY)
2043         {
2044           int fde_encoding;
2045           int per_encoding;
2046           int lsda_encoding;
2047
2048           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2049                                                        /*global=*/0);
2050           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2051                                                        /*global=*/1);
2052           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2053                                                         /*global=*/0);
2054           flags = ((! flag_pic
2055                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2056                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2057                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2058                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2059                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2060                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2061                    ? 0 : SECTION_WRITE);
2062         }
2063       else
2064         flags = SECTION_WRITE;
2065       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2066     }
2067 #endif
2068
2069   if (eh_frame_section)
2070     switch_to_section (eh_frame_section);
2071   else
2072     {
2073       /* We have no special eh_frame section.  Put the information in
2074          the data section and emit special labels to guide collect2.  */
2075       switch_to_section (data_section);
2076       label = get_file_function_name ("F");
2077       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2078       targetm.asm_out.globalize_label (asm_out_file,
2079                                        IDENTIFIER_POINTER (label));
2080       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2081     }
2082 }
2083
2084 /* Output a Call Frame Information opcode and its operand(s).  */
2085
2086 static void
2087 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2088 {
2089   unsigned long r;
2090   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2091     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2092                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2093                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2094                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
2095   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2096     {
2097       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2098       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2099                            "DW_CFA_offset, column 0x%lx", r);
2100       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2101     }
2102   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2103     {
2104       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2105       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2106                            "DW_CFA_restore, column 0x%lx", r);
2107     }
2108   else
2109     {
2110       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2111                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2112
2113       switch (cfi->dw_cfi_opc)
2114         {
2115         case DW_CFA_set_loc:
2116           if (for_eh)
2117             dw2_asm_output_encoded_addr_rtx (
2118                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2119                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2120                 false, NULL);
2121           else
2122             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2123                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2124           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2125           break;
2126
2127         case DW_CFA_advance_loc1:
2128           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2129                                 fde->dw_fde_current_label, NULL);
2130           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2131           break;
2132
2133         case DW_CFA_advance_loc2:
2134           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2135                                 fde->dw_fde_current_label, NULL);
2136           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2137           break;
2138
2139         case DW_CFA_advance_loc4:
2140           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2141                                 fde->dw_fde_current_label, NULL);
2142           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2143           break;
2144
2145         case DW_CFA_MIPS_advance_loc8:
2146           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2147                                 fde->dw_fde_current_label, NULL);
2148           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2149           break;
2150
2151         case DW_CFA_offset_extended:
2152         case DW_CFA_def_cfa:
2153           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2154           dw2_asm_output_data_uleb128 (r, NULL);
2155           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2156           break;
2157
2158         case DW_CFA_offset_extended_sf:
2159         case DW_CFA_def_cfa_sf:
2160           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2161           dw2_asm_output_data_uleb128 (r, NULL);
2162           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2163           break;
2164
2165         case DW_CFA_restore_extended:
2166         case DW_CFA_undefined:
2167         case DW_CFA_same_value:
2168         case DW_CFA_def_cfa_register:
2169           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2170           dw2_asm_output_data_uleb128 (r, NULL);
2171           break;
2172
2173         case DW_CFA_register:
2174           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2175           dw2_asm_output_data_uleb128 (r, NULL);
2176           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2177           dw2_asm_output_data_uleb128 (r, NULL);
2178           break;
2179
2180         case DW_CFA_def_cfa_offset:
2181         case DW_CFA_GNU_args_size:
2182           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2183           break;
2184
2185         case DW_CFA_def_cfa_offset_sf:
2186           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2187           break;
2188
2189         case DW_CFA_GNU_window_save:
2190           break;
2191
2192         case DW_CFA_def_cfa_expression:
2193         case DW_CFA_expression:
2194           output_cfa_loc (cfi);
2195           break;
2196
2197         case DW_CFA_GNU_negative_offset_extended:
2198           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2199           gcc_unreachable ();
2200
2201         default:
2202           break;
2203         }
2204     }
2205 }
2206
2207 /* Output the call frame information used to record information
2208    that relates to calculating the frame pointer, and records the
2209    location of saved registers.  */
2210
2211 static void
2212 output_call_frame_info (int for_eh)
2213 {
2214   unsigned int i;
2215   dw_fde_ref fde;
2216   dw_cfi_ref cfi;
2217   char l1[20], l2[20], section_start_label[20];
2218   bool any_lsda_needed = false;
2219   char augmentation[6];
2220   int augmentation_size;
2221   int fde_encoding = DW_EH_PE_absptr;
2222   int per_encoding = DW_EH_PE_absptr;
2223   int lsda_encoding = DW_EH_PE_absptr;
2224   int return_reg;
2225
2226   /* Don't emit a CIE if there won't be any FDEs.  */
2227   if (fde_table_in_use == 0)
2228     return;
2229
2230   /* If we make FDEs linkonce, we may have to emit an empty label for
2231      an FDE that wouldn't otherwise be emitted.  We want to avoid
2232      having an FDE kept around when the function it refers to is
2233      discarded.  Example where this matters: a primary function
2234      template in C++ requires EH information, but an explicit
2235      specialization doesn't.  */
2236   if (TARGET_USES_WEAK_UNWIND_INFO
2237       && ! flag_asynchronous_unwind_tables
2238       && flag_exceptions
2239       && for_eh)
2240     for (i = 0; i < fde_table_in_use; i++)
2241       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2242           && !fde_table[i].uses_eh_lsda
2243           && ! DECL_WEAK (fde_table[i].decl))
2244         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2245                                       for_eh, /* empty */ 1);
2246
2247   /* If we don't have any functions we'll want to unwind out of, don't
2248      emit any EH unwind information.  Note that if exceptions aren't
2249      enabled, we won't have collected nothrow information, and if we
2250      asked for asynchronous tables, we always want this info.  */
2251   if (for_eh)
2252     {
2253       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2254
2255       for (i = 0; i < fde_table_in_use; i++)
2256         if (fde_table[i].uses_eh_lsda)
2257           any_eh_needed = any_lsda_needed = true;
2258         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2259           any_eh_needed = true;
2260         else if (! fde_table[i].nothrow
2261                  && ! fde_table[i].all_throwers_are_sibcalls)
2262           any_eh_needed = true;
2263
2264       if (! any_eh_needed)
2265         return;
2266     }
2267
2268   /* We're going to be generating comments, so turn on app.  */
2269   if (flag_debug_asm)
2270     app_enable ();
2271
2272   if (for_eh)
2273     switch_to_eh_frame_section ();
2274   else
2275     {
2276       if (!debug_frame_section)
2277         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2278                                            SECTION_DEBUG, NULL);
2279       switch_to_section (debug_frame_section);
2280     }
2281
2282   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2283   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2284
2285   /* Output the CIE.  */
2286   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2287   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2288   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2289     dw2_asm_output_data (4, 0xffffffff,
2290       "Initial length escape value indicating 64-bit DWARF extension");
2291   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2292                         "Length of Common Information Entry");
2293   ASM_OUTPUT_LABEL (asm_out_file, l1);
2294
2295   /* Now that the CIE pointer is PC-relative for EH,
2296      use 0 to identify the CIE.  */
2297   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2298                        (for_eh ? 0 : DWARF_CIE_ID),
2299                        "CIE Identifier Tag");
2300
2301   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2302
2303   augmentation[0] = 0;
2304   augmentation_size = 0;
2305   if (for_eh)
2306     {
2307       char *p;
2308
2309       /* Augmentation:
2310          z      Indicates that a uleb128 is present to size the
2311                 augmentation section.
2312          L      Indicates the encoding (and thus presence) of
2313                 an LSDA pointer in the FDE augmentation.
2314          R      Indicates a non-default pointer encoding for
2315                 FDE code pointers.
2316          P      Indicates the presence of an encoding + language
2317                 personality routine in the CIE augmentation.  */
2318
2319       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2320       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2321       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2322
2323       p = augmentation + 1;
2324       if (eh_personality_libfunc)
2325         {
2326           *p++ = 'P';
2327           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2328           assemble_external_libcall (eh_personality_libfunc);
2329         }
2330       if (any_lsda_needed)
2331         {
2332           *p++ = 'L';
2333           augmentation_size += 1;
2334         }
2335       if (fde_encoding != DW_EH_PE_absptr)
2336         {
2337           *p++ = 'R';
2338           augmentation_size += 1;
2339         }
2340       if (p > augmentation + 1)
2341         {
2342           augmentation[0] = 'z';
2343           *p = '\0';
2344         }
2345
2346       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2347       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2348         {
2349           int offset = (  4             /* Length */
2350                         + 4             /* CIE Id */
2351                         + 1             /* CIE version */
2352                         + strlen (augmentation) + 1     /* Augmentation */
2353                         + size_of_uleb128 (1)           /* Code alignment */
2354                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2355                         + 1             /* RA column */
2356                         + 1             /* Augmentation size */
2357                         + 1             /* Personality encoding */ );
2358           int pad = -offset & (PTR_SIZE - 1);
2359
2360           augmentation_size += pad;
2361
2362           /* Augmentations should be small, so there's scarce need to
2363              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2364           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2365         }
2366     }
2367
2368   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2369   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2370   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2371                                "CIE Data Alignment Factor");
2372
2373   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2374   if (DW_CIE_VERSION == 1)
2375     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2376   else
2377     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2378
2379   if (augmentation[0])
2380     {
2381       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2382       if (eh_personality_libfunc)
2383         {
2384           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2385                                eh_data_format_name (per_encoding));
2386           dw2_asm_output_encoded_addr_rtx (per_encoding,
2387                                            eh_personality_libfunc,
2388                                            true, NULL);
2389         }
2390
2391       if (any_lsda_needed)
2392         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2393                              eh_data_format_name (lsda_encoding));
2394
2395       if (fde_encoding != DW_EH_PE_absptr)
2396         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2397                              eh_data_format_name (fde_encoding));
2398     }
2399
2400   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2401     output_cfi (cfi, NULL, for_eh);
2402
2403   /* Pad the CIE out to an address sized boundary.  */
2404   ASM_OUTPUT_ALIGN (asm_out_file,
2405                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2406   ASM_OUTPUT_LABEL (asm_out_file, l2);
2407
2408   /* Loop through all of the FDE's.  */
2409   for (i = 0; i < fde_table_in_use; i++)
2410     {
2411       fde = &fde_table[i];
2412
2413       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2414       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2415           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2416           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2417           && !fde->uses_eh_lsda)
2418         continue;
2419
2420       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2421       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2422       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2423       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2424       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2425         dw2_asm_output_data (4, 0xffffffff,
2426                              "Initial length escape value indicating 64-bit DWARF extension");
2427       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2428                             "FDE Length");
2429       ASM_OUTPUT_LABEL (asm_out_file, l1);
2430
2431       if (for_eh)
2432         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2433       else
2434         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2435                                debug_frame_section, "FDE CIE offset");
2436
2437       if (for_eh)
2438         {
2439           if (fde->dw_fde_switched_sections)
2440             {
2441               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2442                                       fde->dw_fde_unlikely_section_label);
2443               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2444                                       fde->dw_fde_hot_section_label);
2445               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2446               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2447               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2448                                                "FDE initial location");
2449               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2450                                     fde->dw_fde_hot_section_end_label,
2451                                     fde->dw_fde_hot_section_label,
2452                                     "FDE address range");
2453               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2454                                                "FDE initial location");
2455               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2456                                     fde->dw_fde_unlikely_section_end_label,
2457                                     fde->dw_fde_unlikely_section_label,
2458                                     "FDE address range");
2459             }
2460           else
2461             {
2462               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2463               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2464               dw2_asm_output_encoded_addr_rtx (fde_encoding,
2465                                                sym_ref,
2466                                                false,
2467                                                "FDE initial location");
2468               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2469                                     fde->dw_fde_end, fde->dw_fde_begin,
2470                                     "FDE address range");
2471             }
2472         }
2473       else
2474         {
2475           if (fde->dw_fde_switched_sections)
2476             {
2477               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2478                                    fde->dw_fde_hot_section_label,
2479                                    "FDE initial location");
2480               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2481                                     fde->dw_fde_hot_section_end_label,
2482                                     fde->dw_fde_hot_section_label,
2483                                     "FDE address range");
2484               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2485                                    fde->dw_fde_unlikely_section_label,
2486                                    "FDE initial location");
2487               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2488                                     fde->dw_fde_unlikely_section_end_label,
2489                                     fde->dw_fde_unlikely_section_label,
2490                                     "FDE address range");
2491             }
2492           else
2493             {
2494               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2495                                    "FDE initial location");
2496               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2497                                     fde->dw_fde_end, fde->dw_fde_begin,
2498                                     "FDE address range");
2499             }
2500         }
2501
2502       if (augmentation[0])
2503         {
2504           if (any_lsda_needed)
2505             {
2506               int size = size_of_encoded_value (lsda_encoding);
2507
2508               if (lsda_encoding == DW_EH_PE_aligned)
2509                 {
2510                   int offset = (  4             /* Length */
2511                                 + 4             /* CIE offset */
2512                                 + 2 * size_of_encoded_value (fde_encoding)
2513                                 + 1             /* Augmentation size */ );
2514                   int pad = -offset & (PTR_SIZE - 1);
2515
2516                   size += pad;
2517                   gcc_assert (size_of_uleb128 (size) == 1);
2518                 }
2519
2520               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2521
2522               if (fde->uses_eh_lsda)
2523                 {
2524                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2525                                                fde->funcdef_number);
2526                   dw2_asm_output_encoded_addr_rtx (
2527                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2528                         false, "Language Specific Data Area");
2529                 }
2530               else
2531                 {
2532                   if (lsda_encoding == DW_EH_PE_aligned)
2533                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2534                   dw2_asm_output_data
2535                     (size_of_encoded_value (lsda_encoding), 0,
2536                      "Language Specific Data Area (none)");
2537                 }
2538             }
2539           else
2540             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2541         }
2542
2543       /* Loop through the Call Frame Instructions associated with
2544          this FDE.  */
2545       fde->dw_fde_current_label = fde->dw_fde_begin;
2546       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2547         output_cfi (cfi, fde, for_eh);
2548
2549       /* Pad the FDE out to an address sized boundary.  */
2550       ASM_OUTPUT_ALIGN (asm_out_file,
2551                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2552       ASM_OUTPUT_LABEL (asm_out_file, l2);
2553     }
2554
2555   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2556     dw2_asm_output_data (4, 0, "End of Table");
2557 #ifdef MIPS_DEBUGGING_INFO
2558   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2559      get a value of 0.  Putting .align 0 after the label fixes it.  */
2560   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2561 #endif
2562
2563   /* Turn off app to make assembly quicker.  */
2564   if (flag_debug_asm)
2565     app_disable ();
2566 }
2567
2568 /* Output a marker (i.e. a label) for the beginning of a function, before
2569    the prologue.  */
2570
2571 void
2572 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2573                           const char *file ATTRIBUTE_UNUSED)
2574 {
2575   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2576   char * dup_label;
2577   dw_fde_ref fde;
2578
2579   current_function_func_begin_label = NULL;
2580
2581 #ifdef TARGET_UNWIND_INFO
2582   /* ??? current_function_func_begin_label is also used by except.c
2583      for call-site information.  We must emit this label if it might
2584      be used.  */
2585   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2586       && ! dwarf2out_do_frame ())
2587     return;
2588 #else
2589   if (! dwarf2out_do_frame ())
2590     return;
2591 #endif
2592
2593   switch_to_section (function_section (current_function_decl));
2594   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2595                                current_function_funcdef_no);
2596   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2597                           current_function_funcdef_no);
2598   dup_label = xstrdup (label);
2599   current_function_func_begin_label = dup_label;
2600
2601 #ifdef TARGET_UNWIND_INFO
2602   /* We can elide the fde allocation if we're not emitting debug info.  */
2603   if (! dwarf2out_do_frame ())
2604     return;
2605 #endif
2606
2607   /* Expand the fde table if necessary.  */
2608   if (fde_table_in_use == fde_table_allocated)
2609     {
2610       fde_table_allocated += FDE_TABLE_INCREMENT;
2611       fde_table = ggc_realloc (fde_table,
2612                                fde_table_allocated * sizeof (dw_fde_node));
2613       memset (fde_table + fde_table_in_use, 0,
2614               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2615     }
2616
2617   /* Record the FDE associated with this function.  */
2618   current_funcdef_fde = fde_table_in_use;
2619
2620   /* Add the new FDE at the end of the fde_table.  */
2621   fde = &fde_table[fde_table_in_use++];
2622   fde->decl = current_function_decl;
2623   fde->dw_fde_begin = dup_label;
2624   fde->dw_fde_current_label = dup_label;
2625   fde->dw_fde_hot_section_label = NULL;
2626   fde->dw_fde_hot_section_end_label = NULL;
2627   fde->dw_fde_unlikely_section_label = NULL;
2628   fde->dw_fde_unlikely_section_end_label = NULL;
2629   fde->dw_fde_switched_sections = false;
2630   fde->dw_fde_end = NULL;
2631   fde->dw_fde_cfi = NULL;
2632   fde->funcdef_number = current_function_funcdef_no;
2633   fde->nothrow = TREE_NOTHROW (current_function_decl);
2634   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2635   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2636
2637   args_size = old_args_size = 0;
2638
2639   /* We only want to output line number information for the genuine dwarf2
2640      prologue case, not the eh frame case.  */
2641 #ifdef DWARF2_DEBUGGING_INFO
2642   if (file)
2643     dwarf2out_source_line (line, file);
2644 #endif
2645 }
2646
2647 /* Output a marker (i.e. a label) for the absolute end of the generated code
2648    for a function definition.  This gets called *after* the epilogue code has
2649    been generated.  */
2650
2651 void
2652 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2653                         const char *file ATTRIBUTE_UNUSED)
2654 {
2655   dw_fde_ref fde;
2656   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2657
2658   /* Output a label to mark the endpoint of the code generated for this
2659      function.  */
2660   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2661                                current_function_funcdef_no);
2662   ASM_OUTPUT_LABEL (asm_out_file, label);
2663   fde = &fde_table[fde_table_in_use - 1];
2664   fde->dw_fde_end = xstrdup (label);
2665 }
2666
2667 void
2668 dwarf2out_frame_init (void)
2669 {
2670   /* Allocate the initial hunk of the fde_table.  */
2671   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2672   fde_table_allocated = FDE_TABLE_INCREMENT;
2673   fde_table_in_use = 0;
2674
2675   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2676      sake of lookup_cfa.  */
2677
2678   /* On entry, the Canonical Frame Address is at SP.  */
2679   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2680
2681 #ifdef DWARF2_UNWIND_INFO
2682   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2683     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2684 #endif
2685 }
2686
2687 void
2688 dwarf2out_frame_finish (void)
2689 {
2690   /* Output call frame information.  */
2691   if (DWARF2_FRAME_INFO)
2692     output_call_frame_info (0);
2693
2694 #ifndef TARGET_UNWIND_INFO
2695   /* Output another copy for the unwinder.  */
2696   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2697     output_call_frame_info (1);
2698 #endif
2699 }
2700
2701 /* Note that the current function section is being used for code.  */
2702
2703 static void
2704 dwarf2out_note_section_used (void)
2705 {
2706   section *sec = current_function_section ();
2707   if (sec == text_section)
2708     text_section_used = true;
2709   else if (sec == cold_text_section)
2710     cold_text_section_used = true;
2711 }
2712
2713 void
2714 dwarf2out_switch_text_section (void)
2715 {
2716   dw_fde_ref fde;
2717
2718   gcc_assert (cfun);
2719
2720   fde = &fde_table[fde_table_in_use - 1];
2721   fde->dw_fde_switched_sections = true;
2722   fde->dw_fde_hot_section_label = cfun->hot_section_label;
2723   fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
2724   fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
2725   fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
2726   have_multiple_function_sections = true;
2727
2728   /* Reset the current label on switching text sections, so that we
2729      don't attempt to advance_loc4 between labels in different sections.  */
2730   fde->dw_fde_current_label = NULL;
2731
2732   dwarf2out_note_section_used ();
2733 }
2734 #endif
2735 \f
2736 /* And now, the subset of the debugging information support code necessary
2737    for emitting location expressions.  */
2738
2739 /* Data about a single source file.  */
2740 struct dwarf_file_data GTY(())
2741 {
2742   const char * filename;
2743   int emitted_number;
2744 };
2745
2746 /* We need some way to distinguish DW_OP_addr with a direct symbol
2747    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2748 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2749
2750
2751 typedef struct dw_val_struct *dw_val_ref;
2752 typedef struct die_struct *dw_die_ref;
2753 typedef const struct die_struct *const_dw_die_ref;
2754 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2755 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2756
2757 /* Each DIE may have a series of attribute/value pairs.  Values
2758    can take on several forms.  The forms that are used in this
2759    implementation are listed below.  */
2760
2761 enum dw_val_class
2762 {
2763   dw_val_class_addr,
2764   dw_val_class_offset,
2765   dw_val_class_loc,
2766   dw_val_class_loc_list,
2767   dw_val_class_range_list,
2768   dw_val_class_const,
2769   dw_val_class_unsigned_const,
2770   dw_val_class_long_long,
2771   dw_val_class_vec,
2772   dw_val_class_flag,
2773   dw_val_class_die_ref,
2774   dw_val_class_fde_ref,
2775   dw_val_class_lbl_id,
2776   dw_val_class_lineptr,
2777   dw_val_class_str,
2778   dw_val_class_macptr,
2779   dw_val_class_file
2780 };
2781
2782 /* Describe a double word constant value.  */
2783 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2784
2785 typedef struct dw_long_long_struct GTY(())
2786 {
2787   unsigned long hi;
2788   unsigned long low;
2789 }
2790 dw_long_long_const;
2791
2792 /* Describe a floating point constant value, or a vector constant value.  */
2793
2794 typedef struct dw_vec_struct GTY(())
2795 {
2796   unsigned char * GTY((length ("%h.length"))) array;
2797   unsigned length;
2798   unsigned elt_size;
2799 }
2800 dw_vec_const;
2801
2802 /* The dw_val_node describes an attribute's value, as it is
2803    represented internally.  */
2804
2805 typedef struct dw_val_struct GTY(())
2806 {
2807   enum dw_val_class val_class;
2808   union dw_val_struct_union
2809     {
2810       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2811       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2812       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2813       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2814       HOST_WIDE_INT GTY ((default)) val_int;
2815       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2816       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2817       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2818       struct dw_val_die_union
2819         {
2820           dw_die_ref die;
2821           int external;
2822         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2823       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2824       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2825       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2826       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2827       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2828     }
2829   GTY ((desc ("%1.val_class"))) v;
2830 }
2831 dw_val_node;
2832
2833 /* Locations in memory are described using a sequence of stack machine
2834    operations.  */
2835
2836 typedef struct dw_loc_descr_struct GTY(())
2837 {
2838   dw_loc_descr_ref dw_loc_next;
2839   enum dwarf_location_atom dw_loc_opc;
2840   dw_val_node dw_loc_oprnd1;
2841   dw_val_node dw_loc_oprnd2;
2842   int dw_loc_addr;
2843 }
2844 dw_loc_descr_node;
2845
2846 /* Location lists are ranges + location descriptions for that range,
2847    so you can track variables that are in different places over
2848    their entire life.  */
2849 typedef struct dw_loc_list_struct GTY(())
2850 {
2851   dw_loc_list_ref dw_loc_next;
2852   const char *begin; /* Label for begin address of range */
2853   const char *end;  /* Label for end address of range */
2854   char *ll_symbol; /* Label for beginning of location list.
2855                       Only on head of list */
2856   const char *section; /* Section this loclist is relative to */
2857   dw_loc_descr_ref expr;
2858 } dw_loc_list_node;
2859
2860 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2861
2862 static const char *dwarf_stack_op_name (unsigned);
2863 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2864                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2865 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2866 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2867 static unsigned long size_of_locs (dw_loc_descr_ref);
2868 static void output_loc_operands (dw_loc_descr_ref);
2869 static void output_loc_sequence (dw_loc_descr_ref);
2870
2871 /* Convert a DWARF stack opcode into its string name.  */
2872
2873 static const char *
2874 dwarf_stack_op_name (unsigned int op)
2875 {
2876   switch (op)
2877     {
2878     case DW_OP_addr:
2879     case INTERNAL_DW_OP_tls_addr:
2880       return "DW_OP_addr";
2881     case DW_OP_deref:
2882       return "DW_OP_deref";
2883     case DW_OP_const1u:
2884       return "DW_OP_const1u";
2885     case DW_OP_const1s:
2886       return "DW_OP_const1s";
2887     case DW_OP_const2u:
2888       return "DW_OP_const2u";
2889     case DW_OP_const2s:
2890       return "DW_OP_const2s";
2891     case DW_OP_const4u:
2892       return "DW_OP_const4u";
2893     case DW_OP_const4s:
2894       return "DW_OP_const4s";
2895     case DW_OP_const8u:
2896       return "DW_OP_const8u";
2897     case DW_OP_const8s:
2898       return "DW_OP_const8s";
2899     case DW_OP_constu:
2900       return "DW_OP_constu";
2901     case DW_OP_consts:
2902       return "DW_OP_consts";
2903     case DW_OP_dup:
2904       return "DW_OP_dup";
2905     case DW_OP_drop:
2906       return "DW_OP_drop";
2907     case DW_OP_over:
2908       return "DW_OP_over";
2909     case DW_OP_pick:
2910       return "DW_OP_pick";
2911     case DW_OP_swap:
2912       return "DW_OP_swap";
2913     case DW_OP_rot:
2914       return "DW_OP_rot";
2915     case DW_OP_xderef:
2916       return "DW_OP_xderef";
2917     case DW_OP_abs:
2918       return "DW_OP_abs";
2919     case DW_OP_and:
2920       return "DW_OP_and";
2921     case DW_OP_div:
2922       return "DW_OP_div";
2923     case DW_OP_minus:
2924       return "DW_OP_minus";
2925     case DW_OP_mod:
2926       return "DW_OP_mod";
2927     case DW_OP_mul:
2928       return "DW_OP_mul";
2929     case DW_OP_neg:
2930       return "DW_OP_neg";
2931     case DW_OP_not:
2932       return "DW_OP_not";
2933     case DW_OP_or:
2934       return "DW_OP_or";
2935     case DW_OP_plus:
2936       return "DW_OP_plus";
2937     case DW_OP_plus_uconst:
2938       return "DW_OP_plus_uconst";
2939     case DW_OP_shl:
2940       return "DW_OP_shl";
2941     case DW_OP_shr:
2942       return "DW_OP_shr";
2943     case DW_OP_shra:
2944       return "DW_OP_shra";
2945     case DW_OP_xor:
2946       return "DW_OP_xor";
2947     case DW_OP_bra:
2948       return "DW_OP_bra";
2949     case DW_OP_eq:
2950       return "DW_OP_eq";
2951     case DW_OP_ge:
2952       return "DW_OP_ge";
2953     case DW_OP_gt:
2954       return "DW_OP_gt";
2955     case DW_OP_le:
2956       return "DW_OP_le";
2957     case DW_OP_lt:
2958       return "DW_OP_lt";
2959     case DW_OP_ne:
2960       return "DW_OP_ne";
2961     case DW_OP_skip:
2962       return "DW_OP_skip";
2963     case DW_OP_lit0:
2964       return "DW_OP_lit0";
2965     case DW_OP_lit1:
2966       return "DW_OP_lit1";
2967     case DW_OP_lit2:
2968       return "DW_OP_lit2";
2969     case DW_OP_lit3:
2970       return "DW_OP_lit3";
2971     case DW_OP_lit4:
2972       return "DW_OP_lit4";
2973     case DW_OP_lit5:
2974       return "DW_OP_lit5";
2975     case DW_OP_lit6:
2976       return "DW_OP_lit6";
2977     case DW_OP_lit7:
2978       return "DW_OP_lit7";
2979     case DW_OP_lit8:
2980       return "DW_OP_lit8";
2981     case DW_OP_lit9:
2982       return "DW_OP_lit9";
2983     case DW_OP_lit10:
2984       return "DW_OP_lit10";
2985     case DW_OP_lit11:
2986       return "DW_OP_lit11";
2987     case DW_OP_lit12:
2988       return "DW_OP_lit12";
2989     case DW_OP_lit13:
2990       return "DW_OP_lit13";
2991     case DW_OP_lit14:
2992       return "DW_OP_lit14";
2993     case DW_OP_lit15:
2994       return "DW_OP_lit15";
2995     case DW_OP_lit16:
2996       return "DW_OP_lit16";
2997     case DW_OP_lit17:
2998       return "DW_OP_lit17";
2999     case DW_OP_lit18:
3000       return "DW_OP_lit18";
3001     case DW_OP_lit19:
3002       return "DW_OP_lit19";
3003     case DW_OP_lit20:
3004       return "DW_OP_lit20";
3005     case DW_OP_lit21:
3006       return "DW_OP_lit21";
3007     case DW_OP_lit22:
3008       return "DW_OP_lit22";
3009     case DW_OP_lit23:
3010       return "DW_OP_lit23";
3011     case DW_OP_lit24:
3012       return "DW_OP_lit24";
3013     case DW_OP_lit25:
3014       return "DW_OP_lit25";
3015     case DW_OP_lit26:
3016       return "DW_OP_lit26";
3017     case DW_OP_lit27:
3018       return "DW_OP_lit27";
3019     case DW_OP_lit28:
3020       return "DW_OP_lit28";
3021     case DW_OP_lit29:
3022       return "DW_OP_lit29";
3023     case DW_OP_lit30:
3024       return "DW_OP_lit30";
3025     case DW_OP_lit31:
3026       return "DW_OP_lit31";
3027     case DW_OP_reg0:
3028       return "DW_OP_reg0";
3029     case DW_OP_reg1:
3030       return "DW_OP_reg1";
3031     case DW_OP_reg2:
3032       return "DW_OP_reg2";
3033     case DW_OP_reg3:
3034       return "DW_OP_reg3";
3035     case DW_OP_reg4:
3036       return "DW_OP_reg4";
3037     case DW_OP_reg5:
3038       return "DW_OP_reg5";
3039     case DW_OP_reg6:
3040       return "DW_OP_reg6";
3041     case DW_OP_reg7:
3042       return "DW_OP_reg7";
3043     case DW_OP_reg8:
3044       return "DW_OP_reg8";
3045     case DW_OP_reg9:
3046       return "DW_OP_reg9";
3047     case DW_OP_reg10:
3048       return "DW_OP_reg10";
3049     case DW_OP_reg11:
3050       return "DW_OP_reg11";
3051     case DW_OP_reg12:
3052       return "DW_OP_reg12";
3053     case DW_OP_reg13:
3054       return "DW_OP_reg13";
3055     case DW_OP_reg14:
3056       return "DW_OP_reg14";
3057     case DW_OP_reg15:
3058       return "DW_OP_reg15";
3059     case DW_OP_reg16:
3060       return "DW_OP_reg16";
3061     case DW_OP_reg17:
3062       return "DW_OP_reg17";
3063     case DW_OP_reg18:
3064       return "DW_OP_reg18";
3065     case DW_OP_reg19:
3066       return "DW_OP_reg19";
3067     case DW_OP_reg20:
3068       return "DW_OP_reg20";
3069     case DW_OP_reg21:
3070       return "DW_OP_reg21";
3071     case DW_OP_reg22:
3072       return "DW_OP_reg22";
3073     case DW_OP_reg23:
3074       return "DW_OP_reg23";
3075     case DW_OP_reg24:
3076       return "DW_OP_reg24";
3077     case DW_OP_reg25:
3078       return "DW_OP_reg25";
3079     case DW_OP_reg26:
3080       return "DW_OP_reg26";
3081     case DW_OP_reg27:
3082       return "DW_OP_reg27";
3083     case DW_OP_reg28:
3084       return "DW_OP_reg28";
3085     case DW_OP_reg29:
3086       return "DW_OP_reg29";
3087     case DW_OP_reg30:
3088       return "DW_OP_reg30";
3089     case DW_OP_reg31:
3090       return "DW_OP_reg31";
3091     case DW_OP_breg0:
3092       return "DW_OP_breg0";
3093     case DW_OP_breg1:
3094       return "DW_OP_breg1";
3095     case DW_OP_breg2:
3096       return "DW_OP_breg2";
3097     case DW_OP_breg3:
3098       return "DW_OP_breg3";
3099     case DW_OP_breg4:
3100       return "DW_OP_breg4";
3101     case DW_OP_breg5:
3102       return "DW_OP_breg5";
3103     case DW_OP_breg6:
3104       return "DW_OP_breg6";
3105     case DW_OP_breg7:
3106       return "DW_OP_breg7";
3107     case DW_OP_breg8:
3108       return "DW_OP_breg8";
3109     case DW_OP_breg9:
3110       return "DW_OP_breg9";
3111     case DW_OP_breg10:
3112       return "DW_OP_breg10";
3113     case DW_OP_breg11:
3114       return "DW_OP_breg11";
3115     case DW_OP_breg12:
3116       return "DW_OP_breg12";
3117     case DW_OP_breg13:
3118       return "DW_OP_breg13";
3119     case DW_OP_breg14:
3120       return "DW_OP_breg14";
3121     case DW_OP_breg15:
3122       return "DW_OP_breg15";
3123     case DW_OP_breg16:
3124       return "DW_OP_breg16";
3125     case DW_OP_breg17:
3126       return "DW_OP_breg17";
3127     case DW_OP_breg18:
3128       return "DW_OP_breg18";
3129     case DW_OP_breg19:
3130       return "DW_OP_breg19";
3131     case DW_OP_breg20:
3132       return "DW_OP_breg20";
3133     case DW_OP_breg21:
3134       return "DW_OP_breg21";
3135     case DW_OP_breg22:
3136       return "DW_OP_breg22";
3137     case DW_OP_breg23:
3138       return "DW_OP_breg23";
3139     case DW_OP_breg24:
3140       return "DW_OP_breg24";
3141     case DW_OP_breg25:
3142       return "DW_OP_breg25";
3143     case DW_OP_breg26:
3144       return "DW_OP_breg26";
3145     case DW_OP_breg27:
3146       return "DW_OP_breg27";
3147     case DW_OP_breg28:
3148       return "DW_OP_breg28";
3149     case DW_OP_breg29:
3150       return "DW_OP_breg29";
3151     case DW_OP_breg30:
3152       return "DW_OP_breg30";
3153     case DW_OP_breg31:
3154       return "DW_OP_breg31";
3155     case DW_OP_regx:
3156       return "DW_OP_regx";
3157     case DW_OP_fbreg:
3158       return "DW_OP_fbreg";
3159     case DW_OP_bregx:
3160       return "DW_OP_bregx";
3161     case DW_OP_piece:
3162       return "DW_OP_piece";
3163     case DW_OP_deref_size:
3164       return "DW_OP_deref_size";
3165     case DW_OP_xderef_size:
3166       return "DW_OP_xderef_size";
3167     case DW_OP_nop:
3168       return "DW_OP_nop";
3169     case DW_OP_push_object_address:
3170       return "DW_OP_push_object_address";
3171     case DW_OP_call2:
3172       return "DW_OP_call2";
3173     case DW_OP_call4:
3174       return "DW_OP_call4";
3175     case DW_OP_call_ref:
3176       return "DW_OP_call_ref";
3177     case DW_OP_GNU_push_tls_address:
3178       return "DW_OP_GNU_push_tls_address";
3179     case DW_OP_GNU_uninit:
3180       return "DW_OP_GNU_uninit";
3181     default:
3182       return "OP_<unknown>";
3183     }
3184 }
3185
3186 /* Return a pointer to a newly allocated location description.  Location
3187    descriptions are simple expression terms that can be strung
3188    together to form more complicated location (address) descriptions.  */
3189
3190 static inline dw_loc_descr_ref
3191 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3192                unsigned HOST_WIDE_INT oprnd2)
3193 {
3194   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3195
3196   descr->dw_loc_opc = op;
3197   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3198   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3199   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3200   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3201
3202   return descr;
3203 }
3204
3205 /* Add a location description term to a location description expression.  */
3206
3207 static inline void
3208 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3209 {
3210   dw_loc_descr_ref *d;
3211
3212   /* Find the end of the chain.  */
3213   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3214     ;
3215
3216   *d = descr;
3217 }
3218
3219 /* Return the size of a location descriptor.  */
3220
3221 static unsigned long
3222 size_of_loc_descr (dw_loc_descr_ref loc)
3223 {
3224   unsigned long size = 1;
3225
3226   switch (loc->dw_loc_opc)
3227     {
3228     case DW_OP_addr:
3229     case INTERNAL_DW_OP_tls_addr:
3230       size += DWARF2_ADDR_SIZE;
3231       break;
3232     case DW_OP_const1u:
3233     case DW_OP_const1s:
3234       size += 1;
3235       break;
3236     case DW_OP_const2u:
3237     case DW_OP_const2s:
3238       size += 2;
3239       break;
3240     case DW_OP_const4u:
3241     case DW_OP_const4s:
3242       size += 4;
3243       break;
3244     case DW_OP_const8u:
3245     case DW_OP_const8s:
3246       size += 8;
3247       break;
3248     case DW_OP_constu:
3249       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3250       break;
3251     case DW_OP_consts:
3252       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3253       break;
3254     case DW_OP_pick:
3255       size += 1;
3256       break;
3257     case DW_OP_plus_uconst:
3258       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3259       break;
3260     case DW_OP_skip:
3261     case DW_OP_bra:
3262       size += 2;
3263       break;
3264     case DW_OP_breg0:
3265     case DW_OP_breg1:
3266     case DW_OP_breg2:
3267     case DW_OP_breg3:
3268     case DW_OP_breg4:
3269     case DW_OP_breg5:
3270     case DW_OP_breg6:
3271     case DW_OP_breg7:
3272     case DW_OP_breg8:
3273     case DW_OP_breg9:
3274     case DW_OP_breg10:
3275     case DW_OP_breg11:
3276     case DW_OP_breg12:
3277     case DW_OP_breg13:
3278     case DW_OP_breg14:
3279     case DW_OP_breg15:
3280     case DW_OP_breg16:
3281     case DW_OP_breg17:
3282     case DW_OP_breg18:
3283     case DW_OP_breg19:
3284     case DW_OP_breg20:
3285     case DW_OP_breg21:
3286     case DW_OP_breg22:
3287     case DW_OP_breg23:
3288     case DW_OP_breg24:
3289     case DW_OP_breg25:
3290     case DW_OP_breg26:
3291     case DW_OP_breg27:
3292     case DW_OP_breg28:
3293     case DW_OP_breg29:
3294     case DW_OP_breg30:
3295     case DW_OP_breg31:
3296       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3297       break;
3298     case DW_OP_regx:
3299       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3300       break;
3301     case DW_OP_fbreg:
3302       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3303       break;
3304     case DW_OP_bregx:
3305       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3306       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3307       break;
3308     case DW_OP_piece:
3309       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3310       break;
3311     case DW_OP_deref_size:
3312     case DW_OP_xderef_size:
3313       size += 1;
3314       break;
3315     case DW_OP_call2:
3316       size += 2;
3317       break;
3318     case DW_OP_call4:
3319       size += 4;
3320       break;
3321     case DW_OP_call_ref:
3322       size += DWARF2_ADDR_SIZE;
3323       break;
3324     default:
3325       break;
3326     }
3327
3328   return size;
3329 }
3330
3331 /* Return the size of a series of location descriptors.  */
3332
3333 static unsigned long
3334 size_of_locs (dw_loc_descr_ref loc)
3335 {
3336   dw_loc_descr_ref l;
3337   unsigned long size;
3338
3339   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3340      field, to avoid writing to a PCH file.  */
3341   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3342     {
3343       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3344         break;
3345       size += size_of_loc_descr (l);
3346     }
3347   if (! l)
3348     return size;
3349
3350   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3351     {
3352       l->dw_loc_addr = size;
3353       size += size_of_loc_descr (l);
3354     }
3355
3356   return size;
3357 }
3358
3359 /* Output location description stack opcode's operands (if any).  */
3360
3361 static void
3362 output_loc_operands (dw_loc_descr_ref loc)
3363 {
3364   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3365   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3366
3367   switch (loc->dw_loc_opc)
3368     {
3369 #ifdef DWARF2_DEBUGGING_INFO
3370     case DW_OP_addr:
3371       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3372       break;
3373     case DW_OP_const2u:
3374     case DW_OP_const2s:
3375       dw2_asm_output_data (2, val1->v.val_int, NULL);
3376       break;
3377     case DW_OP_const4u:
3378     case DW_OP_const4s:
3379       dw2_asm_output_data (4, val1->v.val_int, NULL);
3380       break;
3381     case DW_OP_const8u:
3382     case DW_OP_const8s:
3383       gcc_assert (HOST_BITS_PER_LONG >= 64);
3384       dw2_asm_output_data (8, val1->v.val_int, NULL);
3385       break;
3386     case DW_OP_skip:
3387     case DW_OP_bra:
3388       {
3389         int offset;
3390
3391         gcc_assert (val1->val_class == dw_val_class_loc);
3392         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3393
3394         dw2_asm_output_data (2, offset, NULL);
3395       }
3396       break;
3397 #else
3398     case DW_OP_addr:
3399     case DW_OP_const2u:
3400     case DW_OP_const2s:
3401     case DW_OP_const4u:
3402     case DW_OP_const4s:
3403     case DW_OP_const8u:
3404     case DW_OP_const8s:
3405     case DW_OP_skip:
3406     case DW_OP_bra:
3407       /* We currently don't make any attempt to make sure these are
3408          aligned properly like we do for the main unwind info, so
3409          don't support emitting things larger than a byte if we're
3410          only doing unwinding.  */
3411       gcc_unreachable ();
3412 #endif
3413     case DW_OP_const1u:
3414     case DW_OP_const1s:
3415       dw2_asm_output_data (1, val1->v.val_int, NULL);
3416       break;
3417     case DW_OP_constu:
3418       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3419       break;
3420     case DW_OP_consts:
3421       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3422       break;
3423     case DW_OP_pick:
3424       dw2_asm_output_data (1, val1->v.val_int, NULL);
3425       break;
3426     case DW_OP_plus_uconst:
3427       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3428       break;
3429     case DW_OP_breg0:
3430     case DW_OP_breg1:
3431     case DW_OP_breg2:
3432     case DW_OP_breg3:
3433     case DW_OP_breg4:
3434     case DW_OP_breg5:
3435     case DW_OP_breg6:
3436     case DW_OP_breg7:
3437     case DW_OP_breg8:
3438     case DW_OP_breg9:
3439     case DW_OP_breg10:
3440     case DW_OP_breg11:
3441     case DW_OP_breg12:
3442     case DW_OP_breg13:
3443     case DW_OP_breg14:
3444     case DW_OP_breg15:
3445     case DW_OP_breg16:
3446     case DW_OP_breg17:
3447     case DW_OP_breg18:
3448     case DW_OP_breg19:
3449     case DW_OP_breg20:
3450     case DW_OP_breg21:
3451     case DW_OP_breg22:
3452     case DW_OP_breg23:
3453     case DW_OP_breg24:
3454     case DW_OP_breg25:
3455     case DW_OP_breg26:
3456     case DW_OP_breg27:
3457     case DW_OP_breg28:
3458     case DW_OP_breg29:
3459     case DW_OP_breg30:
3460     case DW_OP_breg31:
3461       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3462       break;
3463     case DW_OP_regx:
3464       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3465       break;
3466     case DW_OP_fbreg:
3467       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3468       break;
3469     case DW_OP_bregx:
3470       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3471       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3472       break;
3473     case DW_OP_piece:
3474       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3475       break;
3476     case DW_OP_deref_size:
3477     case DW_OP_xderef_size:
3478       dw2_asm_output_data (1, val1->v.val_int, NULL);
3479       break;
3480
3481     case INTERNAL_DW_OP_tls_addr:
3482       if (targetm.asm_out.output_dwarf_dtprel)
3483         {
3484           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3485                                                DWARF2_ADDR_SIZE,
3486                                                val1->v.val_addr);
3487           fputc ('\n', asm_out_file);
3488         }
3489       else
3490         gcc_unreachable ();
3491       break;
3492
3493     default:
3494       /* Other codes have no operands.  */
3495       break;
3496     }
3497 }
3498
3499 /* Output a sequence of location operations.  */
3500
3501 static void
3502 output_loc_sequence (dw_loc_descr_ref loc)
3503 {
3504   for (; loc != NULL; loc = loc->dw_loc_next)
3505     {
3506       /* Output the opcode.  */
3507       dw2_asm_output_data (1, loc->dw_loc_opc,
3508                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3509
3510       /* Output the operand(s) (if any).  */
3511       output_loc_operands (loc);
3512     }
3513 }
3514
3515 /* This routine will generate the correct assembly data for a location
3516    description based on a cfi entry with a complex address.  */
3517
3518 static void
3519 output_cfa_loc (dw_cfi_ref cfi)
3520 {
3521   dw_loc_descr_ref loc;
3522   unsigned long size;
3523
3524   /* Output the size of the block.  */
3525   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3526   size = size_of_locs (loc);
3527   dw2_asm_output_data_uleb128 (size, NULL);
3528
3529   /* Now output the operations themselves.  */
3530   output_loc_sequence (loc);
3531 }
3532
3533 /* This function builds a dwarf location descriptor sequence from a
3534    dw_cfa_location, adding the given OFFSET to the result of the
3535    expression.  */
3536
3537 static struct dw_loc_descr_struct *
3538 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3539 {
3540   struct dw_loc_descr_struct *head, *tmp;
3541
3542   offset += cfa->offset;
3543
3544   if (cfa->indirect)
3545     {
3546       if (cfa->base_offset)
3547         {
3548           if (cfa->reg <= 31)
3549             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3550           else
3551             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3552         }
3553       else if (cfa->reg <= 31)
3554         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3555       else
3556         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3557
3558       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3559       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3560       add_loc_descr (&head, tmp);
3561       if (offset != 0)
3562         {
3563           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3564           add_loc_descr (&head, tmp);
3565         }
3566     }
3567   else
3568     {
3569       if (offset == 0)
3570         if (cfa->reg <= 31)
3571           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3572         else
3573           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3574       else if (cfa->reg <= 31)
3575         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3576       else
3577         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3578     }
3579
3580   return head;
3581 }
3582
3583 /* This function fills in aa dw_cfa_location structure from a dwarf location
3584    descriptor sequence.  */
3585
3586 static void
3587 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3588 {
3589   struct dw_loc_descr_struct *ptr;
3590   cfa->offset = 0;
3591   cfa->base_offset = 0;
3592   cfa->indirect = 0;
3593   cfa->reg = -1;
3594
3595   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3596     {
3597       enum dwarf_location_atom op = ptr->dw_loc_opc;
3598
3599       switch (op)
3600         {
3601         case DW_OP_reg0:
3602         case DW_OP_reg1:
3603         case DW_OP_reg2:
3604         case DW_OP_reg3:
3605         case DW_OP_reg4:
3606         case DW_OP_reg5:
3607         case DW_OP_reg6:
3608         case DW_OP_reg7:
3609         case DW_OP_reg8:
3610         case DW_OP_reg9:
3611         case DW_OP_reg10:
3612         case DW_OP_reg11:
3613         case DW_OP_reg12:
3614         case DW_OP_reg13:
3615         case DW_OP_reg14:
3616         case DW_OP_reg15:
3617         case DW_OP_reg16:
3618         case DW_OP_reg17:
3619         case DW_OP_reg18:
3620         case DW_OP_reg19:
3621         case DW_OP_reg20:
3622         case DW_OP_reg21:
3623         case DW_OP_reg22:
3624         case DW_OP_reg23:
3625         case DW_OP_reg24:
3626         case DW_OP_reg25:
3627         case DW_OP_reg26:
3628         case DW_OP_reg27:
3629         case DW_OP_reg28:
3630         case DW_OP_reg29:
3631         case DW_OP_reg30:
3632         case DW_OP_reg31:
3633           cfa->reg = op - DW_OP_reg0;
3634           break;
3635         case DW_OP_regx:
3636           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3637           break;
3638         case DW_OP_breg0:
3639         case DW_OP_breg1:
3640         case DW_OP_breg2:
3641         case DW_OP_breg3:
3642         case DW_OP_breg4:
3643         case DW_OP_breg5:
3644         case DW_OP_breg6:
3645         case DW_OP_breg7:
3646         case DW_OP_breg8:
3647         case DW_OP_breg9:
3648         case DW_OP_breg10:
3649         case DW_OP_breg11:
3650         case DW_OP_breg12:
3651         case DW_OP_breg13:
3652         case DW_OP_breg14:
3653         case DW_OP_breg15:
3654         case DW_OP_breg16:
3655         case DW_OP_breg17:
3656         case DW_OP_breg18:
3657         case DW_OP_breg19:
3658         case DW_OP_breg20:
3659         case DW_OP_breg21:
3660         case DW_OP_breg22:
3661         case DW_OP_breg23:
3662         case DW_OP_breg24:
3663         case DW_OP_breg25:
3664         case DW_OP_breg26:
3665         case DW_OP_breg27:
3666         case DW_OP_breg28:
3667         case DW_OP_breg29:
3668         case DW_OP_breg30:
3669         case DW_OP_breg31:
3670           cfa->reg = op - DW_OP_breg0;
3671           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3672           break;
3673         case DW_OP_bregx:
3674           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3675           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3676           break;
3677         case DW_OP_deref:
3678           cfa->indirect = 1;
3679           break;
3680         case DW_OP_plus_uconst:
3681           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3682           break;
3683         default:
3684           internal_error ("DW_LOC_OP %s not implemented",
3685                           dwarf_stack_op_name (ptr->dw_loc_opc));
3686         }
3687     }
3688 }
3689 #endif /* .debug_frame support */
3690 \f
3691 /* And now, the support for symbolic debugging information.  */
3692 #ifdef DWARF2_DEBUGGING_INFO
3693
3694 /* .debug_str support.  */
3695 static int output_indirect_string (void **, void *);
3696
3697 static void dwarf2out_init (const char *);
3698 static void dwarf2out_finish (const char *);
3699 static void dwarf2out_define (unsigned int, const char *);
3700 static void dwarf2out_undef (unsigned int, const char *);
3701 static void dwarf2out_start_source_file (unsigned, const char *);
3702 static void dwarf2out_end_source_file (unsigned);
3703 static void dwarf2out_begin_block (unsigned, unsigned);
3704 static void dwarf2out_end_block (unsigned, unsigned);
3705 static bool dwarf2out_ignore_block (const_tree);
3706 static void dwarf2out_global_decl (tree);
3707 static void dwarf2out_type_decl (tree, int);
3708 static void dwarf2out_imported_module_or_decl (tree, tree);
3709 static void dwarf2out_abstract_function (tree);
3710 static void dwarf2out_var_location (rtx);
3711 static void dwarf2out_begin_function (tree);
3712
3713 /* The debug hooks structure.  */
3714
3715 const struct gcc_debug_hooks dwarf2_debug_hooks =
3716 {
3717   dwarf2out_init,
3718   dwarf2out_finish,
3719   dwarf2out_define,
3720   dwarf2out_undef,
3721   dwarf2out_start_source_file,
3722   dwarf2out_end_source_file,
3723   dwarf2out_begin_block,
3724   dwarf2out_end_block,
3725   dwarf2out_ignore_block,
3726   dwarf2out_source_line,
3727   dwarf2out_begin_prologue,
3728   debug_nothing_int_charstar,   /* end_prologue */
3729   dwarf2out_end_epilogue,
3730   dwarf2out_begin_function,
3731   debug_nothing_int,            /* end_function */
3732   dwarf2out_decl,               /* function_decl */
3733   dwarf2out_global_decl,
3734   dwarf2out_type_decl,          /* type_decl */
3735   dwarf2out_imported_module_or_decl,
3736   debug_nothing_tree,           /* deferred_inline_function */
3737   /* The DWARF 2 backend tries to reduce debugging bloat by not
3738      emitting the abstract description of inline functions until
3739      something tries to reference them.  */
3740   dwarf2out_abstract_function,  /* outlining_inline_function */
3741   debug_nothing_rtx,            /* label */
3742   debug_nothing_int,            /* handle_pch */
3743   dwarf2out_var_location,
3744   dwarf2out_switch_text_section,
3745   1                             /* start_end_main_source_file */
3746 };
3747 #endif
3748 \f
3749 /* NOTE: In the comments in this file, many references are made to
3750    "Debugging Information Entries".  This term is abbreviated as `DIE'
3751    throughout the remainder of this file.  */
3752
3753 /* An internal representation of the DWARF output is built, and then
3754    walked to generate the DWARF debugging info.  The walk of the internal
3755    representation is done after the entire program has been compiled.
3756    The types below are used to describe the internal representation.  */
3757
3758 /* Various DIE's use offsets relative to the beginning of the
3759    .debug_info section to refer to each other.  */
3760
3761 typedef long int dw_offset;
3762
3763 /* Define typedefs here to avoid circular dependencies.  */
3764
3765 typedef struct dw_attr_struct *dw_attr_ref;
3766 typedef struct dw_line_info_struct *dw_line_info_ref;
3767 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3768 typedef struct pubname_struct *pubname_ref;
3769 typedef struct dw_ranges_struct *dw_ranges_ref;
3770 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3771
3772 /* Each entry in the line_info_table maintains the file and
3773    line number associated with the label generated for that
3774    entry.  The label gives the PC value associated with
3775    the line number entry.  */
3776
3777 typedef struct dw_line_info_struct GTY(())
3778 {
3779   unsigned long dw_file_num;
3780   unsigned long dw_line_num;
3781 }
3782 dw_line_info_entry;
3783
3784 /* Line information for functions in separate sections; each one gets its
3785    own sequence.  */
3786 typedef struct dw_separate_line_info_struct GTY(())
3787 {
3788   unsigned long dw_file_num;
3789   unsigned long dw_line_num;
3790   unsigned long function;
3791 }
3792 dw_separate_line_info_entry;
3793
3794 /* Each DIE attribute has a field specifying the attribute kind,
3795    a link to the next attribute in the chain, and an attribute value.
3796    Attributes are typically linked below the DIE they modify.  */
3797
3798 typedef struct dw_attr_struct GTY(())
3799 {
3800   enum dwarf_attribute dw_attr;
3801   dw_val_node dw_attr_val;
3802 }
3803 dw_attr_node;
3804
3805 DEF_VEC_O(dw_attr_node);
3806 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3807
3808 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3809    The children of each node form a circular list linked by
3810    die_sib.  die_child points to the node *before* the "first" child node.  */
3811
3812 typedef struct die_struct GTY(())
3813 {
3814   enum dwarf_tag die_tag;
3815   char *die_symbol;
3816   VEC(dw_attr_node,gc) * die_attr;
3817   dw_die_ref die_parent;
3818   dw_die_ref die_child;
3819   dw_die_ref die_sib;
3820   dw_die_ref die_definition; /* ref from a specification to its definition */
3821   dw_offset die_offset;
3822   unsigned long die_abbrev;
3823   int die_mark;
3824   /* Die is used and must not be pruned as unused.  */
3825   int die_perennial_p;
3826   unsigned int decl_id;
3827 }
3828 die_node;
3829
3830 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3831 #define FOR_EACH_CHILD(die, c, expr) do {       \
3832   c = die->die_child;                           \
3833   if (c) do {                                   \
3834     c = c->die_sib;                             \
3835     expr;                                       \
3836   } while (c != die->die_child);                \
3837 } while (0)
3838
3839 /* The pubname structure */
3840
3841 typedef struct pubname_struct GTY(())
3842 {
3843   dw_die_ref die;
3844   const char *name;
3845 }
3846 pubname_entry;
3847
3848 DEF_VEC_O(pubname_entry);
3849 DEF_VEC_ALLOC_O(pubname_entry, gc);
3850
3851 struct dw_ranges_struct GTY(())
3852 {
3853   /* If this is positive, it's a block number, otherwise it's a
3854      bitwise-negated index into dw_ranges_by_label.  */
3855   int num;
3856 };
3857
3858 struct dw_ranges_by_label_struct GTY(())
3859 {
3860   const char *begin;
3861   const char *end;
3862 };
3863
3864 /* The limbo die list structure.  */
3865 typedef struct limbo_die_struct GTY(())
3866 {
3867   dw_die_ref die;
3868   tree created_for;
3869   struct limbo_die_struct *next;
3870 }
3871 limbo_die_node;
3872
3873 /* How to start an assembler comment.  */
3874 #ifndef ASM_COMMENT_START
3875 #define ASM_COMMENT_START ";#"
3876 #endif
3877
3878 /* Define a macro which returns nonzero for a TYPE_DECL which was
3879    implicitly generated for a tagged type.
3880
3881    Note that unlike the gcc front end (which generates a NULL named
3882    TYPE_DECL node for each complete tagged type, each array type, and
3883    each function type node created) the g++ front end generates a
3884    _named_ TYPE_DECL node for each tagged type node created.
3885    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3886    generate a DW_TAG_typedef DIE for them.  */
3887
3888 #define TYPE_DECL_IS_STUB(decl)                         \
3889   (DECL_NAME (decl) == NULL_TREE                        \
3890    || (DECL_ARTIFICIAL (decl)                           \
3891        && is_tagged_type (TREE_TYPE (decl))             \
3892        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3893            /* This is necessary for stub decls that     \
3894               appear in nested inline functions.  */    \
3895            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3896                && (decl_ultimate_origin (decl)          \
3897                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3898
3899 /* Information concerning the compilation unit's programming
3900    language, and compiler version.  */
3901
3902 /* Fixed size portion of the DWARF compilation unit header.  */
3903 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3904   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3905
3906 /* Fixed size portion of public names info.  */
3907 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3908
3909 /* Fixed size portion of the address range info.  */
3910 #define DWARF_ARANGES_HEADER_SIZE                                       \
3911   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3912                 DWARF2_ADDR_SIZE * 2)                                   \
3913    - DWARF_INITIAL_LENGTH_SIZE)
3914
3915 /* Size of padding portion in the address range info.  It must be
3916    aligned to twice the pointer size.  */
3917 #define DWARF_ARANGES_PAD_SIZE \
3918   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3919                 DWARF2_ADDR_SIZE * 2)                              \
3920    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3921
3922 /* Use assembler line directives if available.  */
3923 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3924 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3925 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3926 #else
3927 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3928 #endif
3929 #endif
3930
3931 /* Minimum line offset in a special line info. opcode.
3932    This value was chosen to give a reasonable range of values.  */
3933 #define DWARF_LINE_BASE  -10
3934
3935 /* First special line opcode - leave room for the standard opcodes.  */
3936 #define DWARF_LINE_OPCODE_BASE  10
3937
3938 /* Range of line offsets in a special line info. opcode.  */
3939 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3940
3941 /* Flag that indicates the initial value of the is_stmt_start flag.
3942    In the present implementation, we do not mark any lines as
3943    the beginning of a source statement, because that information
3944    is not made available by the GCC front-end.  */
3945 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3946
3947 #ifdef DWARF2_DEBUGGING_INFO
3948 /* This location is used by calc_die_sizes() to keep track
3949    the offset of each DIE within the .debug_info section.  */
3950 static unsigned long next_die_offset;
3951 #endif
3952
3953 /* Record the root of the DIE's built for the current compilation unit.  */
3954 static GTY(()) dw_die_ref comp_unit_die;
3955
3956 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3957 static GTY(()) limbo_die_node *limbo_die_list;
3958
3959 /* Filenames referenced by this compilation unit.  */
3960 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3961
3962 /* A hash table of references to DIE's that describe declarations.
3963    The key is a DECL_UID() which is a unique number identifying each decl.  */
3964 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3965
3966 /* Node of the variable location list.  */
3967 struct var_loc_node GTY ((chain_next ("%h.next")))
3968 {
3969   rtx GTY (()) var_loc_note;
3970   const char * GTY (()) label;
3971   const char * GTY (()) section_label;
3972   struct var_loc_node * GTY (()) next;
3973 };
3974
3975 /* Variable location list.  */
3976 struct var_loc_list_def GTY (())
3977 {
3978   struct var_loc_node * GTY (()) first;
3979
3980   /* Do not mark the last element of the chained list because
3981      it is marked through the chain.  */
3982   struct var_loc_node * GTY ((skip ("%h"))) last;
3983
3984   /* DECL_UID of the variable decl.  */
3985   unsigned int decl_id;
3986 };
3987 typedef struct var_loc_list_def var_loc_list;
3988
3989
3990 /* Table of decl location linked lists.  */
3991 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3992
3993 /* A pointer to the base of a list of references to DIE's that
3994    are uniquely identified by their tag, presence/absence of
3995    children DIE's, and list of attribute/value pairs.  */
3996 static GTY((length ("abbrev_die_table_allocated")))
3997   dw_die_ref *abbrev_die_table;
3998
3999 /* Number of elements currently allocated for abbrev_die_table.  */
4000 static GTY(()) unsigned abbrev_die_table_allocated;
4001
4002 /* Number of elements in type_die_table currently in use.  */
4003 static GTY(()) unsigned abbrev_die_table_in_use;
4004
4005 /* Size (in elements) of increments by which we may expand the
4006    abbrev_die_table.  */
4007 #define ABBREV_DIE_TABLE_INCREMENT 256
4008
4009 /* A pointer to the base of a table that contains line information
4010    for each source code line in .text in the compilation unit.  */
4011 static GTY((length ("line_info_table_allocated")))
4012      dw_line_info_ref line_info_table;
4013
4014 /* Number of elements currently allocated for line_info_table.  */
4015 static GTY(()) unsigned line_info_table_allocated;
4016
4017 /* Number of elements in line_info_table currently in use.  */
4018 static GTY(()) unsigned line_info_table_in_use;
4019
4020 /* A pointer to the base of a table that contains line information
4021    for each source code line outside of .text in the compilation unit.  */
4022 static GTY ((length ("separate_line_info_table_allocated")))
4023      dw_separate_line_info_ref separate_line_info_table;
4024
4025 /* Number of elements currently allocated for separate_line_info_table.  */
4026 static GTY(()) unsigned separate_line_info_table_allocated;
4027
4028 /* Number of elements in separate_line_info_table currently in use.  */
4029 static GTY(()) unsigned separate_line_info_table_in_use;
4030
4031 /* Size (in elements) of increments by which we may expand the
4032    line_info_table.  */
4033 #define LINE_INFO_TABLE_INCREMENT 1024
4034
4035 /* A pointer to the base of a table that contains a list of publicly
4036    accessible names.  */
4037 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4038
4039 /* A pointer to the base of a table that contains a list of publicly
4040    accessible types.  */
4041 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4042
4043 /* Array of dies for which we should generate .debug_arange info.  */
4044 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4045
4046 /* Number of elements currently allocated for arange_table.  */
4047 static GTY(()) unsigned arange_table_allocated;
4048
4049 /* Number of elements in arange_table currently in use.  */
4050 static GTY(()) unsigned arange_table_in_use;
4051
4052 /* Size (in elements) of increments by which we may expand the
4053    arange_table.  */
4054 #define ARANGE_TABLE_INCREMENT 64
4055
4056 /* Array of dies for which we should generate .debug_ranges info.  */
4057 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4058
4059 /* Number of elements currently allocated for ranges_table.  */
4060 static GTY(()) unsigned ranges_table_allocated;
4061
4062 /* Number of elements in ranges_table currently in use.  */
4063 static GTY(()) unsigned ranges_table_in_use;
4064
4065 /* Array of pairs of labels referenced in ranges_table.  */
4066 static GTY ((length ("ranges_by_label_allocated")))
4067      dw_ranges_by_label_ref ranges_by_label;
4068
4069 /* Number of elements currently allocated for ranges_by_label.  */
4070 static GTY(()) unsigned ranges_by_label_allocated;
4071
4072 /* Number of elements in ranges_by_label currently in use.  */
4073 static GTY(()) unsigned ranges_by_label_in_use;
4074
4075 /* Size (in elements) of increments by which we may expand the
4076    ranges_table.  */
4077 #define RANGES_TABLE_INCREMENT 64
4078
4079 /* Whether we have location lists that need outputting */
4080 static GTY(()) bool have_location_lists;
4081
4082 /* Unique label counter.  */
4083 static GTY(()) unsigned int loclabel_num;
4084
4085 #ifdef DWARF2_DEBUGGING_INFO
4086 /* Record whether the function being analyzed contains inlined functions.  */
4087 static int current_function_has_inlines;
4088 #endif
4089 #if 0 && defined (MIPS_DEBUGGING_INFO)
4090 static int comp_unit_has_inlines;
4091 #endif
4092
4093 /* The last file entry emitted by maybe_emit_file().  */
4094 static GTY(()) struct dwarf_file_data * last_emitted_file;
4095
4096 /* Number of internal labels generated by gen_internal_sym().  */
4097 static GTY(()) int label_num;
4098
4099 /* Cached result of previous call to lookup_filename.  */
4100 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4101
4102 #ifdef DWARF2_DEBUGGING_INFO
4103
4104 /* Offset from the "steady-state frame pointer" to the frame base,
4105    within the current function.  */
4106 static HOST_WIDE_INT frame_pointer_fb_offset;
4107
4108 /* Forward declarations for functions defined in this file.  */
4109
4110 static int is_pseudo_reg (const_rtx);
4111 static tree type_main_variant (tree);
4112 static int is_tagged_type (const_tree);
4113 static const char *dwarf_tag_name (unsigned);
4114 static const char *dwarf_attr_name (unsigned);
4115 static const char *dwarf_form_name (unsigned);
4116 static tree decl_ultimate_origin (const_tree);
4117 static tree block_ultimate_origin (const_tree);
4118 static tree decl_class_context (tree);
4119 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4120 static inline enum dw_val_class AT_class (dw_attr_ref);
4121 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4122 static inline unsigned AT_flag (dw_attr_ref);
4123 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4124 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4125 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4126 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4127 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4128                               unsigned long);
4129 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4130                                unsigned int, unsigned char *);
4131 static hashval_t debug_str_do_hash (const void *);
4132 static int debug_str_eq (const void *, const void *);
4133 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4134 static inline const char *AT_string (dw_attr_ref);
4135 static int AT_string_form (dw_attr_ref);
4136 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4137 static void add_AT_specification (dw_die_ref, dw_die_ref);
4138 static inline dw_die_ref AT_ref (dw_attr_ref);
4139 static inline int AT_ref_external (dw_attr_ref);
4140 static inline void set_AT_ref_external (dw_attr_ref, int);
4141 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4142 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4143 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4144 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4145                              dw_loc_list_ref);
4146 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4147 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4148 static inline rtx AT_addr (dw_attr_ref);
4149 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4150 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4151 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4152 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4153                            unsigned HOST_WIDE_INT);
4154 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4155                                unsigned long);
4156 static inline const char *AT_lbl (dw_attr_ref);
4157 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4158 static const char *get_AT_low_pc (dw_die_ref);
4159 static const char *get_AT_hi_pc (dw_die_ref);
4160 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4161 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4162 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4163 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4164 static bool is_c_family (void);
4165 static bool is_cxx (void);
4166 static bool is_java (void);
4167 static bool is_fortran (void);
4168 static bool is_ada (void);
4169 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4170 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4171 static void add_child_die (dw_die_ref, dw_die_ref);
4172 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4173 static dw_die_ref lookup_type_die (tree);
4174 static void equate_type_number_to_die (tree, dw_die_ref);
4175 static hashval_t decl_die_table_hash (const void *);
4176 static int decl_die_table_eq (const void *, const void *);
4177 static dw_die_ref lookup_decl_die (tree);
4178 static hashval_t decl_loc_table_hash (const void *);
4179 static int decl_loc_table_eq (const void *, const void *);
4180 static var_loc_list *lookup_decl_loc (const_tree);
4181 static void equate_decl_number_to_die (tree, dw_die_ref);
4182 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4183 static void print_spaces (FILE *);
4184 static void print_die (dw_die_ref, FILE *);
4185 static void print_dwarf_line_table (FILE *);
4186 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4187 static dw_die_ref pop_compile_unit (dw_die_ref);
4188 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4189 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4190 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4191 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4192 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4193 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4194 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4195 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4196 static void compute_section_prefix (dw_die_ref);
4197 static int is_type_die (dw_die_ref);
4198 static int is_comdat_die (dw_die_ref);
4199 static int is_symbol_die (dw_die_ref);
4200 static void assign_symbol_names (dw_die_ref);
4201 static void break_out_includes (dw_die_ref);
4202 static hashval_t htab_cu_hash (const void *);
4203 static int htab_cu_eq (const void *, const void *);
4204 static void htab_cu_del (void *);
4205 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4206 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4207 static void add_sibling_attributes (dw_die_ref);
4208 static void build_abbrev_table (dw_die_ref);
4209 static void output_location_lists (dw_die_ref);
4210 static int constant_size (long unsigned);
4211 static unsigned long size_of_die (dw_die_ref);
4212 static void calc_die_sizes (dw_die_ref);
4213 static void mark_dies (dw_die_ref);
4214 static void unmark_dies (dw_die_ref);
4215 static void unmark_all_dies (dw_die_ref);
4216 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4217 static unsigned long size_of_aranges (void);
4218 static enum dwarf_form value_format (dw_attr_ref);
4219 static void output_value_format (dw_attr_ref);
4220 static void output_abbrev_section (void);
4221 static void output_die_symbol (dw_die_ref);
4222 static void output_die (dw_die_ref);
4223 static void output_compilation_unit_header (void);
4224 static void output_comp_unit (dw_die_ref, int);
4225 static const char *dwarf2_name (tree, int);
4226 static void add_pubname (tree, dw_die_ref);
4227 static void add_pubtype (tree, dw_die_ref);
4228 static void output_pubnames (VEC (pubname_entry,gc) *);
4229 static void add_arange (tree, dw_die_ref);
4230 static void output_aranges (void);
4231 static unsigned int add_ranges_num (int);
4232 static unsigned int add_ranges (const_tree);
4233 static unsigned int add_ranges_by_labels (const char *, const char *);
4234 static void output_ranges (void);
4235 static void output_line_info (void);
4236 static void output_file_names (void);
4237 static dw_die_ref base_type_die (tree);
4238 static int is_base_type (tree);
4239 static bool is_subrange_type (const_tree);
4240 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4241 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4242 static int type_is_enum (const_tree);
4243 static unsigned int dbx_reg_number (const_rtx);
4244 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4245 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4246 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int, 
4247                                                 enum var_init_status);
4248 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4249                                                      enum var_init_status);
4250 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4251 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4252                                          enum var_init_status);
4253 static int is_based_loc (const_rtx);
4254 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4255                                             enum var_init_status);
4256 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4257                                                enum var_init_status);
4258 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4259 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4260 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4261 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4262 static tree field_type (const_tree);
4263 static unsigned int simple_type_align_in_bits (const_tree);
4264 static unsigned int simple_decl_align_in_bits (const_tree);
4265 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4266 static HOST_WIDE_INT field_byte_offset (const_tree);
4267 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4268                                          dw_loc_descr_ref);
4269 static void add_data_member_location_attribute (dw_die_ref, tree);
4270 static void add_const_value_attribute (dw_die_ref, rtx);
4271 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4272 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4273 static void insert_float (const_rtx, unsigned char *);
4274 static rtx rtl_for_decl_location (tree);
4275 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4276                                                    enum dwarf_attribute);
4277 static void tree_add_const_value_attribute (dw_die_ref, tree);
4278 static void add_name_attribute (dw_die_ref, const char *);
4279 static void add_comp_dir_attribute (dw_die_ref);
4280 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4281 static void add_subscript_info (dw_die_ref, tree);
4282 static void add_byte_size_attribute (dw_die_ref, tree);
4283 static void add_bit_offset_attribute (dw_die_ref, tree);
4284 static void add_bit_size_attribute (dw_die_ref, tree);
4285 static void add_prototyped_attribute (dw_die_ref, tree);
4286 static void add_abstract_origin_attribute (dw_die_ref, tree);
4287 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4288 static void add_src_coords_attributes (dw_die_ref, tree);
4289 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4290 static void push_decl_scope (tree);
4291 static void pop_decl_scope (void);
4292 static dw_die_ref scope_die_for (tree, dw_die_ref);
4293 static inline int local_scope_p (dw_die_ref);
4294 static inline int class_or_namespace_scope_p (dw_die_ref);
4295 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4296 static void add_calling_convention_attribute (dw_die_ref, tree);
4297 static const char *type_tag (const_tree);
4298 static tree member_declared_type (const_tree);
4299 #if 0
4300 static const char *decl_start_label (tree);
4301 #endif
4302 static void gen_array_type_die (tree, dw_die_ref);
4303 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4304 #if 0
4305 static void gen_entry_point_die (tree, dw_die_ref);
4306 #endif
4307 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4308 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4309 static void gen_inlined_union_type_die (tree, dw_die_ref);
4310 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4311 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4312 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4313 static void gen_formal_types_die (tree, dw_die_ref);
4314 static void gen_subprogram_die (tree, dw_die_ref);
4315 static void gen_variable_die (tree, dw_die_ref);
4316 static void gen_label_die (tree, dw_die_ref);
4317 static void gen_lexical_block_die (tree, dw_die_ref, int);
4318 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4319 static void gen_field_die (tree, dw_die_ref);
4320 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4321 static dw_die_ref gen_compile_unit_die (const char *);
4322 static void gen_inheritance_die (tree, tree, dw_die_ref);
4323 static void gen_member_die (tree, dw_die_ref);
4324 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4325                                                 enum debug_info_usage);
4326 static void gen_subroutine_type_die (tree, dw_die_ref);
4327 static void gen_typedef_die (tree, dw_die_ref);
4328 static void gen_type_die (tree, dw_die_ref);
4329 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4330 static void gen_block_die (tree, dw_die_ref, int);
4331 static void decls_for_scope (tree, dw_die_ref, int);
4332 static int is_redundant_typedef (const_tree);
4333 static void gen_namespace_die (tree);
4334 static void gen_decl_die (tree, dw_die_ref);
4335 static dw_die_ref force_decl_die (tree);
4336 static dw_die_ref force_type_die (tree);
4337 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4338 static void declare_in_namespace (tree, dw_die_ref);
4339 static struct dwarf_file_data * lookup_filename (const char *);
4340 static void retry_incomplete_types (void);
4341 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4342 static void splice_child_die (dw_die_ref, dw_die_ref);
4343 static int file_info_cmp (const void *, const void *);
4344 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4345                                      const char *, const char *, unsigned);
4346 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4347                                        const char *, const char *,
4348                                        const char *);
4349 static void output_loc_list (dw_loc_list_ref);
4350 static char *gen_internal_sym (const char *);
4351
4352 static void prune_unmark_dies (dw_die_ref);
4353 static void prune_unused_types_mark (dw_die_ref, int);
4354 static void prune_unused_types_walk (dw_die_ref);
4355 static void prune_unused_types_walk_attribs (dw_die_ref);
4356 static void prune_unused_types_prune (dw_die_ref);
4357 static void prune_unused_types (void);
4358 static int maybe_emit_file (struct dwarf_file_data *fd);
4359
4360 /* Section names used to hold DWARF debugging information.  */
4361 #ifndef DEBUG_INFO_SECTION
4362 #define DEBUG_INFO_SECTION      ".debug_info"
4363 #endif
4364 #ifndef DEBUG_ABBREV_SECTION
4365 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4366 #endif
4367 #ifndef DEBUG_ARANGES_SECTION
4368 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4369 #endif
4370 #ifndef DEBUG_MACINFO_SECTION
4371 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4372 #endif
4373 #ifndef DEBUG_LINE_SECTION
4374 #define DEBUG_LINE_SECTION      ".debug_line"
4375 #endif
4376 #ifndef DEBUG_LOC_SECTION
4377 #define DEBUG_LOC_SECTION       ".debug_loc"
4378 #endif
4379 #ifndef DEBUG_PUBNAMES_SECTION
4380 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4381 #endif
4382 #ifndef DEBUG_STR_SECTION
4383 #define DEBUG_STR_SECTION       ".debug_str"
4384 #endif
4385 #ifndef DEBUG_RANGES_SECTION
4386 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4387 #endif
4388
4389 /* Standard ELF section names for compiled code and data.  */
4390 #ifndef TEXT_SECTION_NAME
4391 #define TEXT_SECTION_NAME       ".text"
4392 #endif
4393
4394 /* Section flags for .debug_str section.  */
4395 #define DEBUG_STR_SECTION_FLAGS \
4396   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4397    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4398    : SECTION_DEBUG)
4399
4400 /* Labels we insert at beginning sections we can reference instead of
4401    the section names themselves.  */
4402
4403 #ifndef TEXT_SECTION_LABEL
4404 #define TEXT_SECTION_LABEL              "Ltext"
4405 #endif
4406 #ifndef COLD_TEXT_SECTION_LABEL
4407 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4408 #endif
4409 #ifndef DEBUG_LINE_SECTION_LABEL
4410 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4411 #endif
4412 #ifndef DEBUG_INFO_SECTION_LABEL
4413 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4414 #endif
4415 #ifndef DEBUG_ABBREV_SECTION_LABEL
4416 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4417 #endif
4418 #ifndef DEBUG_LOC_SECTION_LABEL
4419 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4420 #endif
4421 #ifndef DEBUG_RANGES_SECTION_LABEL
4422 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4423 #endif
4424 #ifndef DEBUG_MACINFO_SECTION_LABEL
4425 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4426 #endif
4427
4428 /* Definitions of defaults for formats and names of various special
4429    (artificial) labels which may be generated within this file (when the -g
4430    options is used and DWARF2_DEBUGGING_INFO is in effect.
4431    If necessary, these may be overridden from within the tm.h file, but
4432    typically, overriding these defaults is unnecessary.  */
4433
4434 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4435 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4436 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4437 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4438 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4439 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4440 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4441 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4442 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4443 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4444
4445 #ifndef TEXT_END_LABEL
4446 #define TEXT_END_LABEL          "Letext"
4447 #endif
4448 #ifndef COLD_END_LABEL
4449 #define COLD_END_LABEL          "Letext_cold"
4450 #endif
4451 #ifndef BLOCK_BEGIN_LABEL
4452 #define BLOCK_BEGIN_LABEL       "LBB"
4453 #endif
4454 #ifndef BLOCK_END_LABEL
4455 #define BLOCK_END_LABEL         "LBE"
4456 #endif
4457 #ifndef LINE_CODE_LABEL
4458 #define LINE_CODE_LABEL         "LM"
4459 #endif
4460 #ifndef SEPARATE_LINE_CODE_LABEL
4461 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4462 #endif
4463
4464 \f
4465 /* We allow a language front-end to designate a function that is to be
4466    called to "demangle" any name before it is put into a DIE.  */
4467
4468 static const char *(*demangle_name_func) (const char *);
4469
4470 void
4471 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4472 {
4473   demangle_name_func = func;
4474 }
4475
4476 /* Test if rtl node points to a pseudo register.  */
4477
4478 static inline int
4479 is_pseudo_reg (const_rtx rtl)
4480 {
4481   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4482           || (GET_CODE (rtl) == SUBREG
4483               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4484 }
4485
4486 /* Return a reference to a type, with its const and volatile qualifiers
4487    removed.  */
4488
4489 static inline tree
4490 type_main_variant (tree type)
4491 {
4492   type = TYPE_MAIN_VARIANT (type);
4493
4494   /* ??? There really should be only one main variant among any group of
4495      variants of a given type (and all of the MAIN_VARIANT values for all
4496      members of the group should point to that one type) but sometimes the C
4497      front-end messes this up for array types, so we work around that bug
4498      here.  */
4499   if (TREE_CODE (type) == ARRAY_TYPE)
4500     while (type != TYPE_MAIN_VARIANT (type))
4501       type = TYPE_MAIN_VARIANT (type);
4502
4503   return type;
4504 }
4505
4506 /* Return nonzero if the given type node represents a tagged type.  */
4507
4508 static inline int
4509 is_tagged_type (const_tree type)
4510 {
4511   enum tree_code code = TREE_CODE (type);
4512
4513   return (code == RECORD_TYPE || code == UNION_TYPE
4514           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4515 }
4516
4517 /* Convert a DIE tag into its string name.  */
4518
4519 static const char *
4520 dwarf_tag_name (unsigned int tag)
4521 {
4522   switch (tag)
4523     {
4524     case DW_TAG_padding:
4525       return "DW_TAG_padding";
4526     case DW_TAG_array_type:
4527       return "DW_TAG_array_type";
4528     case DW_TAG_class_type:
4529       return "DW_TAG_class_type";
4530     case DW_TAG_entry_point:
4531       return "DW_TAG_entry_point";
4532     case DW_TAG_enumeration_type:
4533       return "DW_TAG_enumeration_type";
4534     case DW_TAG_formal_parameter:
4535       return "DW_TAG_formal_parameter";
4536     case DW_TAG_imported_declaration:
4537       return "DW_TAG_imported_declaration";
4538     case DW_TAG_label:
4539       return "DW_TAG_label";
4540     case DW_TAG_lexical_block:
4541       return "DW_TAG_lexical_block";
4542     case DW_TAG_member:
4543       return "DW_TAG_member";
4544     case DW_TAG_pointer_type:
4545       return "DW_TAG_pointer_type";
4546     case DW_TAG_reference_type:
4547       return "DW_TAG_reference_type";
4548     case DW_TAG_compile_unit:
4549       return "DW_TAG_compile_unit";
4550     case DW_TAG_string_type:
4551       return "DW_TAG_string_type";
4552     case DW_TAG_structure_type:
4553       return "DW_TAG_structure_type";
4554     case DW_TAG_subroutine_type:
4555       return "DW_TAG_subroutine_type";
4556     case DW_TAG_typedef:
4557       return "DW_TAG_typedef";
4558     case DW_TAG_union_type:
4559       return "DW_TAG_union_type";
4560     case DW_TAG_unspecified_parameters:
4561       return "DW_TAG_unspecified_parameters";
4562     case DW_TAG_variant:
4563       return "DW_TAG_variant";
4564     case DW_TAG_common_block:
4565       return "DW_TAG_common_block";
4566     case DW_TAG_common_inclusion:
4567       return "DW_TAG_common_inclusion";
4568     case DW_TAG_inheritance:
4569       return "DW_TAG_inheritance";
4570     case DW_TAG_inlined_subroutine:
4571       return "DW_TAG_inlined_subroutine";
4572     case DW_TAG_module:
4573       return "DW_TAG_module";
4574     case DW_TAG_ptr_to_member_type:
4575       return "DW_TAG_ptr_to_member_type";
4576     case DW_TAG_set_type:
4577       return "DW_TAG_set_type";
4578     case DW_TAG_subrange_type:
4579       return "DW_TAG_subrange_type";
4580     case DW_TAG_with_stmt:
4581       return "DW_TAG_with_stmt";
4582     case DW_TAG_access_declaration:
4583       return "DW_TAG_access_declaration";
4584     case DW_TAG_base_type:
4585       return "DW_TAG_base_type";
4586     case DW_TAG_catch_block:
4587       return "DW_TAG_catch_block";
4588     case DW_TAG_const_type:
4589       return "DW_TAG_const_type";
4590     case DW_TAG_constant:
4591       return "DW_TAG_constant";
4592     case DW_TAG_enumerator:
4593       return "DW_TAG_enumerator";
4594     case DW_TAG_file_type:
4595       return "DW_TAG_file_type";
4596     case DW_TAG_friend:
4597       return "DW_TAG_friend";
4598     case DW_TAG_namelist:
4599       return "DW_TAG_namelist";
4600     case DW_TAG_namelist_item:
4601       return "DW_TAG_namelist_item";
4602     case DW_TAG_packed_type:
4603       return "DW_TAG_packed_type";
4604     case DW_TAG_subprogram:
4605       return "DW_TAG_subprogram";
4606     case DW_TAG_template_type_param:
4607       return "DW_TAG_template_type_param";
4608     case DW_TAG_template_value_param:
4609       return "DW_TAG_template_value_param";
4610     case DW_TAG_thrown_type:
4611       return "DW_TAG_thrown_type";
4612     case DW_TAG_try_block:
4613       return "DW_TAG_try_block";
4614     case DW_TAG_variant_part:
4615       return "DW_TAG_variant_part";
4616     case DW_TAG_variable:
4617       return "DW_TAG_variable";
4618     case DW_TAG_volatile_type:
4619       return "DW_TAG_volatile_type";
4620     case DW_TAG_dwarf_procedure:
4621       return "DW_TAG_dwarf_procedure";
4622     case DW_TAG_restrict_type:
4623       return "DW_TAG_restrict_type";
4624     case DW_TAG_interface_type:
4625       return "DW_TAG_interface_type";
4626     case DW_TAG_namespace:
4627       return "DW_TAG_namespace";
4628     case DW_TAG_imported_module:
4629       return "DW_TAG_imported_module";
4630     case DW_TAG_unspecified_type:
4631       return "DW_TAG_unspecified_type";
4632     case DW_TAG_partial_unit:
4633       return "DW_TAG_partial_unit";
4634     case DW_TAG_imported_unit:
4635       return "DW_TAG_imported_unit";
4636     case DW_TAG_condition:
4637       return "DW_TAG_condition";
4638     case DW_TAG_shared_type:
4639       return "DW_TAG_shared_type";
4640     case DW_TAG_MIPS_loop:
4641       return "DW_TAG_MIPS_loop";
4642     case DW_TAG_format_label:
4643       return "DW_TAG_format_label";
4644     case DW_TAG_function_template:
4645       return "DW_TAG_function_template";
4646     case DW_TAG_class_template:
4647       return "DW_TAG_class_template";
4648     case DW_TAG_GNU_BINCL:
4649       return "DW_TAG_GNU_BINCL";
4650     case DW_TAG_GNU_EINCL:
4651       return "DW_TAG_GNU_EINCL";
4652     default:
4653       return "DW_TAG_<unknown>";
4654     }
4655 }
4656
4657 /* Convert a DWARF attribute code into its string name.  */
4658
4659 static const char *
4660 dwarf_attr_name (unsigned int attr)
4661 {
4662   switch (attr)
4663     {
4664     case DW_AT_sibling:
4665       return "DW_AT_sibling";
4666     case DW_AT_location:
4667       return "DW_AT_location";
4668     case DW_AT_name:
4669       return "DW_AT_name";
4670     case DW_AT_ordering:
4671       return "DW_AT_ordering";
4672     case DW_AT_subscr_data:
4673       return "DW_AT_subscr_data";
4674     case DW_AT_byte_size:
4675       return "DW_AT_byte_size";
4676     case DW_AT_bit_offset:
4677       return "DW_AT_bit_offset";
4678     case DW_AT_bit_size:
4679       return "DW_AT_bit_size";
4680     case DW_AT_element_list:
4681       return "DW_AT_element_list";
4682     case DW_AT_stmt_list:
4683       return "DW_AT_stmt_list";
4684     case DW_AT_low_pc:
4685       return "DW_AT_low_pc";
4686     case DW_AT_high_pc:
4687       return "DW_AT_high_pc";
4688     case DW_AT_language:
4689       return "DW_AT_language";
4690     case DW_AT_member:
4691       return "DW_AT_member";
4692     case DW_AT_discr:
4693       return "DW_AT_discr";
4694     case DW_AT_discr_value:
4695       return "DW_AT_discr_value";
4696     case DW_AT_visibility:
4697       return "DW_AT_visibility";
4698     case DW_AT_import:
4699       return "DW_AT_import";
4700     case DW_AT_string_length:
4701       return "DW_AT_string_length";
4702     case DW_AT_common_reference:
4703       return "DW_AT_common_reference";
4704     case DW_AT_comp_dir:
4705       return "DW_AT_comp_dir";
4706     case DW_AT_const_value:
4707       return "DW_AT_const_value";
4708     case DW_AT_containing_type:
4709       return "DW_AT_containing_type";
4710     case DW_AT_default_value:
4711       return "DW_AT_default_value";
4712     case DW_AT_inline:
4713       return "DW_AT_inline";
4714     case DW_AT_is_optional:
4715       return "DW_AT_is_optional";
4716     case DW_AT_lower_bound:
4717       return "DW_AT_lower_bound";
4718     case DW_AT_producer:
4719       return "DW_AT_producer";
4720     case DW_AT_prototyped:
4721       return "DW_AT_prototyped";
4722     case DW_AT_return_addr:
4723       return "DW_AT_return_addr";
4724     case DW_AT_start_scope:
4725       return "DW_AT_start_scope";
4726     case DW_AT_bit_stride:
4727       return "DW_AT_bit_stride";
4728     case DW_AT_upper_bound:
4729       return "DW_AT_upper_bound";
4730     case DW_AT_abstract_origin:
4731       return "DW_AT_abstract_origin";
4732     case DW_AT_accessibility:
4733       return "DW_AT_accessibility";
4734     case DW_AT_address_class:
4735       return "DW_AT_address_class";
4736     case DW_AT_artificial:
4737       return "DW_AT_artificial";
4738     case DW_AT_base_types:
4739       return "DW_AT_base_types";
4740     case DW_AT_calling_convention:
4741       return "DW_AT_calling_convention";
4742     case DW_AT_count:
4743       return "DW_AT_count";
4744     case DW_AT_data_member_location:
4745       return "DW_AT_data_member_location";
4746     case DW_AT_decl_column:
4747       return "DW_AT_decl_column";
4748     case DW_AT_decl_file:
4749       return "DW_AT_decl_file";
4750     case DW_AT_decl_line:
4751       return "DW_AT_decl_line";
4752     case DW_AT_declaration:
4753       return "DW_AT_declaration";
4754     case DW_AT_discr_list:
4755       return "DW_AT_discr_list";
4756     case DW_AT_encoding:
4757       return "DW_AT_encoding";
4758     case DW_AT_external:
4759       return "DW_AT_external";
4760     case DW_AT_frame_base:
4761       return "DW_AT_frame_base";
4762     case DW_AT_friend:
4763       return "DW_AT_friend";
4764     case DW_AT_identifier_case:
4765       return "DW_AT_identifier_case";
4766     case DW_AT_macro_info:
4767       return "DW_AT_macro_info";
4768     case DW_AT_namelist_items:
4769       return "DW_AT_namelist_items";
4770     case DW_AT_priority:
4771       return "DW_AT_priority";
4772     case DW_AT_segment:
4773       return "DW_AT_segment";
4774     case DW_AT_specification:
4775       return "DW_AT_specification";
4776     case DW_AT_static_link:
4777       return "DW_AT_static_link";
4778     case DW_AT_type:
4779       return "DW_AT_type";
4780     case DW_AT_use_location:
4781       return "DW_AT_use_location";
4782     case DW_AT_variable_parameter:
4783       return "DW_AT_variable_parameter";
4784     case DW_AT_virtuality:
4785       return "DW_AT_virtuality";
4786     case DW_AT_vtable_elem_location:
4787       return "DW_AT_vtable_elem_location";
4788
4789     case DW_AT_allocated:
4790       return "DW_AT_allocated";
4791     case DW_AT_associated:
4792       return "DW_AT_associated";
4793     case DW_AT_data_location:
4794       return "DW_AT_data_location";
4795     case DW_AT_byte_stride:
4796       return "DW_AT_byte_stride";
4797     case DW_AT_entry_pc:
4798       return "DW_AT_entry_pc";
4799     case DW_AT_use_UTF8:
4800       return "DW_AT_use_UTF8";
4801     case DW_AT_extension:
4802       return "DW_AT_extension";
4803     case DW_AT_ranges:
4804       return "DW_AT_ranges";
4805     case DW_AT_trampoline:
4806       return "DW_AT_trampoline";
4807     case DW_AT_call_column:
4808       return "DW_AT_call_column";
4809     case DW_AT_call_file:
4810       return "DW_AT_call_file";
4811     case DW_AT_call_line:
4812       return "DW_AT_call_line";
4813
4814     case DW_AT_MIPS_fde:
4815       return "DW_AT_MIPS_fde";
4816     case DW_AT_MIPS_loop_begin:
4817       return "DW_AT_MIPS_loop_begin";
4818     case DW_AT_MIPS_tail_loop_begin:
4819       return "DW_AT_MIPS_tail_loop_begin";
4820     case DW_AT_MIPS_epilog_begin:
4821       return "DW_AT_MIPS_epilog_begin";
4822     case DW_AT_MIPS_loop_unroll_factor:
4823       return "DW_AT_MIPS_loop_unroll_factor";
4824     case DW_AT_MIPS_software_pipeline_depth:
4825       return "DW_AT_MIPS_software_pipeline_depth";
4826     case DW_AT_MIPS_linkage_name:
4827       return "DW_AT_MIPS_linkage_name";
4828     case DW_AT_MIPS_stride:
4829       return "DW_AT_MIPS_stride";
4830     case DW_AT_MIPS_abstract_name:
4831       return "DW_AT_MIPS_abstract_name";
4832     case DW_AT_MIPS_clone_origin:
4833       return "DW_AT_MIPS_clone_origin";
4834     case DW_AT_MIPS_has_inlines:
4835       return "DW_AT_MIPS_has_inlines";
4836
4837     case DW_AT_sf_names:
4838       return "DW_AT_sf_names";
4839     case DW_AT_src_info:
4840       return "DW_AT_src_info";
4841     case DW_AT_mac_info:
4842       return "DW_AT_mac_info";
4843     case DW_AT_src_coords:
4844       return "DW_AT_src_coords";
4845     case DW_AT_body_begin:
4846       return "DW_AT_body_begin";
4847     case DW_AT_body_end:
4848       return "DW_AT_body_end";
4849     case DW_AT_GNU_vector:
4850       return "DW_AT_GNU_vector";
4851
4852     case DW_AT_VMS_rtnbeg_pd_address:
4853       return "DW_AT_VMS_rtnbeg_pd_address";
4854
4855     default:
4856       return "DW_AT_<unknown>";
4857     }
4858 }
4859
4860 /* Convert a DWARF value form code into its string name.  */
4861
4862 static const char *
4863 dwarf_form_name (unsigned int form)
4864 {
4865   switch (form)
4866     {
4867     case DW_FORM_addr:
4868       return "DW_FORM_addr";
4869     case DW_FORM_block2:
4870       return "DW_FORM_block2";
4871     case DW_FORM_block4:
4872       return "DW_FORM_block4";
4873     case DW_FORM_data2:
4874       return "DW_FORM_data2";
4875     case DW_FORM_data4:
4876       return "DW_FORM_data4";
4877     case DW_FORM_data8:
4878       return "DW_FORM_data8";
4879     case DW_FORM_string:
4880       return "DW_FORM_string";
4881     case DW_FORM_block:
4882       return "DW_FORM_block";
4883     case DW_FORM_block1:
4884       return "DW_FORM_block1";
4885     case DW_FORM_data1:
4886       return "DW_FORM_data1";
4887     case DW_FORM_flag:
4888       return "DW_FORM_flag";
4889     case DW_FORM_sdata:
4890       return "DW_FORM_sdata";
4891     case DW_FORM_strp:
4892       return "DW_FORM_strp";
4893     case DW_FORM_udata:
4894       return "DW_FORM_udata";
4895     case DW_FORM_ref_addr:
4896       return "DW_FORM_ref_addr";
4897     case DW_FORM_ref1:
4898       return "DW_FORM_ref1";
4899     case DW_FORM_ref2:
4900       return "DW_FORM_ref2";
4901     case DW_FORM_ref4:
4902       return "DW_FORM_ref4";
4903     case DW_FORM_ref8:
4904       return "DW_FORM_ref8";
4905     case DW_FORM_ref_udata:
4906       return "DW_FORM_ref_udata";
4907     case DW_FORM_indirect:
4908       return "DW_FORM_indirect";
4909     default:
4910       return "DW_FORM_<unknown>";
4911     }
4912 }
4913 \f
4914 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4915    instance of an inlined instance of a decl which is local to an inline
4916    function, so we have to trace all of the way back through the origin chain
4917    to find out what sort of node actually served as the original seed for the
4918    given block.  */
4919
4920 static tree
4921 decl_ultimate_origin (const_tree decl)
4922 {
4923   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4924     return NULL_TREE;
4925
4926   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4927      nodes in the function to point to themselves; ignore that if
4928      we're trying to output the abstract instance of this function.  */
4929   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4930     return NULL_TREE;
4931
4932   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4933      most distant ancestor, this should never happen.  */
4934   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4935
4936   return DECL_ABSTRACT_ORIGIN (decl);
4937 }
4938
4939 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4940    instance of an inlined instance of a block which is local to an inline
4941    function, so we have to trace all of the way back through the origin chain
4942    to find out what sort of node actually served as the original seed for the
4943    given block.  */
4944
4945 static tree
4946 block_ultimate_origin (const_tree block)
4947 {
4948   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4949
4950   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4951      nodes in the function to point to themselves; ignore that if
4952      we're trying to output the abstract instance of this function.  */
4953   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4954     return NULL_TREE;
4955
4956   if (immediate_origin == NULL_TREE)
4957     return NULL_TREE;
4958   else
4959     {
4960       tree ret_val;
4961       tree lookahead = immediate_origin;
4962
4963       do
4964         {
4965           ret_val = lookahead;
4966           lookahead = (TREE_CODE (ret_val) == BLOCK
4967                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4968         }
4969       while (lookahead != NULL && lookahead != ret_val);
4970
4971       /* The block's abstract origin chain may not be the *ultimate* origin of
4972          the block. It could lead to a DECL that has an abstract origin set.
4973          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4974          will give us if it has one).  Note that DECL's abstract origins are
4975          supposed to be the most distant ancestor (or so decl_ultimate_origin
4976          claims), so we don't need to loop following the DECL origins.  */
4977       if (DECL_P (ret_val))
4978         return DECL_ORIGIN (ret_val);
4979
4980       return ret_val;
4981     }
4982 }
4983
4984 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4985    of a virtual function may refer to a base class, so we check the 'this'
4986    parameter.  */
4987
4988 static tree
4989 decl_class_context (tree decl)
4990 {
4991   tree context = NULL_TREE;
4992
4993   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4994     context = DECL_CONTEXT (decl);
4995   else
4996     context = TYPE_MAIN_VARIANT
4997       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4998
4999   if (context && !TYPE_P (context))
5000     context = NULL_TREE;
5001
5002   return context;
5003 }
5004 \f
5005 /* Add an attribute/value pair to a DIE.  */
5006
5007 static inline void
5008 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5009 {
5010   /* Maybe this should be an assert?  */
5011   if (die == NULL)
5012     return;
5013
5014   if (die->die_attr == NULL)
5015     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5016   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5017 }
5018
5019 static inline enum dw_val_class
5020 AT_class (dw_attr_ref a)
5021 {
5022   return a->dw_attr_val.val_class;
5023 }
5024
5025 /* Add a flag value attribute to a DIE.  */
5026
5027 static inline void
5028 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5029 {
5030   dw_attr_node attr;
5031
5032   attr.dw_attr = attr_kind;
5033   attr.dw_attr_val.val_class = dw_val_class_flag;
5034   attr.dw_attr_val.v.val_flag = flag;
5035   add_dwarf_attr (die, &attr);
5036 }
5037
5038 static inline unsigned
5039 AT_flag (dw_attr_ref a)
5040 {
5041   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5042   return a->dw_attr_val.v.val_flag;
5043 }
5044
5045 /* Add a signed integer attribute value to a DIE.  */
5046
5047 static inline void
5048 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5049 {
5050   dw_attr_node attr;
5051
5052   attr.dw_attr = attr_kind;
5053   attr.dw_attr_val.val_class = dw_val_class_const;
5054   attr.dw_attr_val.v.val_int = int_val;
5055   add_dwarf_attr (die, &attr);
5056 }
5057
5058 static inline HOST_WIDE_INT
5059 AT_int (dw_attr_ref a)
5060 {
5061   gcc_assert (a && AT_class (a) == dw_val_class_const);
5062   return a->dw_attr_val.v.val_int;
5063 }
5064
5065 /* Add an unsigned integer attribute value to a DIE.  */
5066
5067 static inline void
5068 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5069                  unsigned HOST_WIDE_INT unsigned_val)
5070 {
5071   dw_attr_node attr;
5072
5073   attr.dw_attr = attr_kind;
5074   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5075   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5076   add_dwarf_attr (die, &attr);
5077 }
5078
5079 static inline unsigned HOST_WIDE_INT
5080 AT_unsigned (dw_attr_ref a)
5081 {
5082   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5083   return a->dw_attr_val.v.val_unsigned;
5084 }
5085
5086 /* Add an unsigned double integer attribute value to a DIE.  */
5087
5088 static inline void
5089 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5090                   long unsigned int val_hi, long unsigned int val_low)
5091 {
5092   dw_attr_node attr;
5093
5094   attr.dw_attr = attr_kind;
5095   attr.dw_attr_val.val_class = dw_val_class_long_long;
5096   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5097   attr.dw_attr_val.v.val_long_long.low = val_low;
5098   add_dwarf_attr (die, &attr);
5099 }
5100
5101 /* Add a floating point attribute value to a DIE and return it.  */
5102
5103 static inline void
5104 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5105             unsigned int length, unsigned int elt_size, unsigned char *array)
5106 {
5107   dw_attr_node attr;
5108
5109   attr.dw_attr = attr_kind;
5110   attr.dw_attr_val.val_class = dw_val_class_vec;
5111   attr.dw_attr_val.v.val_vec.length = length;
5112   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5113   attr.dw_attr_val.v.val_vec.array = array;
5114   add_dwarf_attr (die, &attr);
5115 }
5116
5117 /* Hash and equality functions for debug_str_hash.  */
5118
5119 static hashval_t
5120 debug_str_do_hash (const void *x)
5121 {
5122   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5123 }
5124
5125 static int
5126 debug_str_eq (const void *x1, const void *x2)
5127 {
5128   return strcmp ((((const struct indirect_string_node *)x1)->str),
5129                  (const char *)x2) == 0;
5130 }
5131
5132 /* Add a string attribute value to a DIE.  */
5133
5134 static inline void
5135 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5136 {
5137   dw_attr_node attr;
5138   struct indirect_string_node *node;
5139   void **slot;
5140
5141   if (! debug_str_hash)
5142     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5143                                       debug_str_eq, NULL);
5144
5145   slot = htab_find_slot_with_hash (debug_str_hash, str,
5146                                    htab_hash_string (str), INSERT);
5147   if (*slot == NULL)
5148     {
5149       node = (struct indirect_string_node *)
5150                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5151       node->str = ggc_strdup (str);
5152       *slot = node;
5153     }
5154   else
5155     node = (struct indirect_string_node *) *slot;
5156
5157   node->refcount++;
5158
5159   attr.dw_attr = attr_kind;
5160   attr.dw_attr_val.val_class = dw_val_class_str;
5161   attr.dw_attr_val.v.val_str = node;
5162   add_dwarf_attr (die, &attr);
5163 }
5164
5165 static inline const char *
5166 AT_string (dw_attr_ref a)
5167 {
5168   gcc_assert (a && AT_class (a) == dw_val_class_str);
5169   return a->dw_attr_val.v.val_str->str;
5170 }
5171
5172 /* Find out whether a string should be output inline in DIE
5173    or out-of-line in .debug_str section.  */
5174
5175 static int
5176 AT_string_form (dw_attr_ref a)
5177 {
5178   struct indirect_string_node *node;
5179   unsigned int len;
5180   char label[32];
5181
5182   gcc_assert (a && AT_class (a) == dw_val_class_str);
5183
5184   node = a->dw_attr_val.v.val_str;
5185   if (node->form)
5186     return node->form;
5187
5188   len = strlen (node->str) + 1;
5189
5190   /* If the string is shorter or equal to the size of the reference, it is
5191      always better to put it inline.  */
5192   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5193     return node->form = DW_FORM_string;
5194
5195   /* If we cannot expect the linker to merge strings in .debug_str
5196      section, only put it into .debug_str if it is worth even in this
5197      single module.  */
5198   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5199       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5200     return node->form = DW_FORM_string;
5201
5202   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5203   ++dw2_string_counter;
5204   node->label = xstrdup (label);
5205
5206   return node->form = DW_FORM_strp;
5207 }
5208
5209 /* Add a DIE reference attribute value to a DIE.  */
5210
5211 static inline void
5212 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5213 {
5214   dw_attr_node attr;
5215
5216   attr.dw_attr = attr_kind;
5217   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5218   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5219   attr.dw_attr_val.v.val_die_ref.external = 0;
5220   add_dwarf_attr (die, &attr);
5221 }
5222
5223 /* Add an AT_specification attribute to a DIE, and also make the back
5224    pointer from the specification to the definition.  */
5225
5226 static inline void
5227 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5228 {
5229   add_AT_die_ref (die, DW_AT_specification, targ_die);
5230   gcc_assert (!targ_die->die_definition);
5231   targ_die->die_definition = die;
5232 }
5233
5234 static inline dw_die_ref
5235 AT_ref (dw_attr_ref a)
5236 {
5237   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5238   return a->dw_attr_val.v.val_die_ref.die;
5239 }
5240
5241 static inline int
5242 AT_ref_external (dw_attr_ref a)
5243 {
5244   if (a && AT_class (a) == dw_val_class_die_ref)
5245     return a->dw_attr_val.v.val_die_ref.external;
5246
5247   return 0;
5248 }
5249
5250 static inline void
5251 set_AT_ref_external (dw_attr_ref a, int i)
5252 {
5253   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5254   a->dw_attr_val.v.val_die_ref.external = i;
5255 }
5256
5257 /* Add an FDE reference attribute value to a DIE.  */
5258
5259 static inline void
5260 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5261 {
5262   dw_attr_node attr;
5263
5264   attr.dw_attr = attr_kind;
5265   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5266   attr.dw_attr_val.v.val_fde_index = targ_fde;
5267   add_dwarf_attr (die, &attr);
5268 }
5269
5270 /* Add a location description attribute value to a DIE.  */
5271
5272 static inline void
5273 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5274 {
5275   dw_attr_node attr;
5276
5277   attr.dw_attr = attr_kind;
5278   attr.dw_attr_val.val_class = dw_val_class_loc;
5279   attr.dw_attr_val.v.val_loc = loc;
5280   add_dwarf_attr (die, &attr);
5281 }
5282
5283 static inline dw_loc_descr_ref
5284 AT_loc (dw_attr_ref a)
5285 {
5286   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5287   return a->dw_attr_val.v.val_loc;
5288 }
5289
5290 static inline void
5291 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5292 {
5293   dw_attr_node attr;
5294
5295   attr.dw_attr = attr_kind;
5296   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5297   attr.dw_attr_val.v.val_loc_list = loc_list;
5298   add_dwarf_attr (die, &attr);
5299   have_location_lists = true;
5300 }
5301
5302 static inline dw_loc_list_ref
5303 AT_loc_list (dw_attr_ref a)
5304 {
5305   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5306   return a->dw_attr_val.v.val_loc_list;
5307 }
5308
5309 /* Add an address constant attribute value to a DIE.  */
5310
5311 static inline void
5312 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5313 {
5314   dw_attr_node attr;
5315
5316   attr.dw_attr = attr_kind;
5317   attr.dw_attr_val.val_class = dw_val_class_addr;
5318   attr.dw_attr_val.v.val_addr = addr;
5319   add_dwarf_attr (die, &attr);
5320 }
5321
5322 /* Get the RTX from to an address DIE attribute.  */
5323
5324 static inline rtx
5325 AT_addr (dw_attr_ref a)
5326 {
5327   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5328   return a->dw_attr_val.v.val_addr;
5329 }
5330
5331 /* Add a file attribute value to a DIE.  */
5332
5333 static inline void
5334 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5335              struct dwarf_file_data *fd)
5336 {
5337   dw_attr_node attr;
5338
5339   attr.dw_attr = attr_kind;
5340   attr.dw_attr_val.val_class = dw_val_class_file;
5341   attr.dw_attr_val.v.val_file = fd;
5342   add_dwarf_attr (die, &attr);
5343 }
5344
5345 /* Get the dwarf_file_data from a file DIE attribute.  */
5346
5347 static inline struct dwarf_file_data *
5348 AT_file (dw_attr_ref a)
5349 {
5350   gcc_assert (a && AT_class (a) == dw_val_class_file);
5351   return a->dw_attr_val.v.val_file;
5352 }
5353
5354 /* Add a label identifier attribute value to a DIE.  */
5355
5356 static inline void
5357 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5358 {
5359   dw_attr_node attr;
5360
5361   attr.dw_attr = attr_kind;
5362   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5363   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5364   add_dwarf_attr (die, &attr);
5365 }
5366
5367 /* Add a section offset attribute value to a DIE, an offset into the
5368    debug_line section.  */
5369
5370 static inline void
5371 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5372                 const char *label)
5373 {
5374   dw_attr_node attr;
5375
5376   attr.dw_attr = attr_kind;
5377   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5378   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5379   add_dwarf_attr (die, &attr);
5380 }
5381
5382 /* Add a section offset attribute value to a DIE, an offset into the
5383    debug_macinfo section.  */
5384
5385 static inline void
5386 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5387                const char *label)
5388 {
5389   dw_attr_node attr;
5390
5391   attr.dw_attr = attr_kind;
5392   attr.dw_attr_val.val_class = dw_val_class_macptr;
5393   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5394   add_dwarf_attr (die, &attr);
5395 }
5396
5397 /* Add an offset attribute value to a DIE.  */
5398
5399 static inline void
5400 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5401                unsigned HOST_WIDE_INT offset)
5402 {
5403   dw_attr_node attr;
5404
5405   attr.dw_attr = attr_kind;
5406   attr.dw_attr_val.val_class = dw_val_class_offset;
5407   attr.dw_attr_val.v.val_offset = offset;
5408   add_dwarf_attr (die, &attr);
5409 }
5410
5411 /* Add an range_list attribute value to a DIE.  */
5412
5413 static void
5414 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5415                    long unsigned int offset)
5416 {
5417   dw_attr_node attr;
5418
5419   attr.dw_attr = attr_kind;
5420   attr.dw_attr_val.val_class = dw_val_class_range_list;
5421   attr.dw_attr_val.v.val_offset = offset;
5422   add_dwarf_attr (die, &attr);
5423 }
5424
5425 static inline const char *
5426 AT_lbl (dw_attr_ref a)
5427 {
5428   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5429                     || AT_class (a) == dw_val_class_lineptr
5430                     || AT_class (a) == dw_val_class_macptr));
5431   return a->dw_attr_val.v.val_lbl_id;
5432 }
5433
5434 /* Get the attribute of type attr_kind.  */
5435
5436 static dw_attr_ref
5437 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5438 {
5439   dw_attr_ref a;
5440   unsigned ix;
5441   dw_die_ref spec = NULL;
5442
5443   if (! die)
5444     return NULL;
5445
5446   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5447     if (a->dw_attr == attr_kind)
5448       return a;
5449     else if (a->dw_attr == DW_AT_specification
5450              || a->dw_attr == DW_AT_abstract_origin)
5451       spec = AT_ref (a);
5452
5453   if (spec)
5454     return get_AT (spec, attr_kind);
5455
5456   return NULL;
5457 }
5458
5459 /* Return the "low pc" attribute value, typically associated with a subprogram
5460    DIE.  Return null if the "low pc" attribute is either not present, or if it
5461    cannot be represented as an assembler label identifier.  */
5462
5463 static inline const char *
5464 get_AT_low_pc (dw_die_ref die)
5465 {
5466   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5467
5468   return a ? AT_lbl (a) : NULL;
5469 }
5470
5471 /* Return the "high pc" attribute value, typically associated with a subprogram
5472    DIE.  Return null if the "high pc" attribute is either not present, or if it
5473    cannot be represented as an assembler label identifier.  */
5474
5475 static inline const char *
5476 get_AT_hi_pc (dw_die_ref die)
5477 {
5478   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5479
5480   return a ? AT_lbl (a) : NULL;
5481 }
5482
5483 /* Return the value of the string attribute designated by ATTR_KIND, or
5484    NULL if it is not present.  */
5485
5486 static inline const char *
5487 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5488 {
5489   dw_attr_ref a = get_AT (die, attr_kind);
5490
5491   return a ? AT_string (a) : NULL;
5492 }
5493
5494 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5495    if it is not present.  */
5496
5497 static inline int
5498 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5499 {
5500   dw_attr_ref a = get_AT (die, attr_kind);
5501
5502   return a ? AT_flag (a) : 0;
5503 }
5504
5505 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5506    if it is not present.  */
5507
5508 static inline unsigned
5509 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5510 {
5511   dw_attr_ref a = get_AT (die, attr_kind);
5512
5513   return a ? AT_unsigned (a) : 0;
5514 }
5515
5516 static inline dw_die_ref
5517 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5518 {
5519   dw_attr_ref a = get_AT (die, attr_kind);
5520
5521   return a ? AT_ref (a) : NULL;
5522 }
5523
5524 static inline struct dwarf_file_data *
5525 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5526 {
5527   dw_attr_ref a = get_AT (die, attr_kind);
5528
5529   return a ? AT_file (a) : NULL;
5530 }
5531
5532 /* Return TRUE if the language is C or C++.  */
5533
5534 static inline bool
5535 is_c_family (void)
5536 {
5537   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5538
5539   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5540           || lang == DW_LANG_C99
5541           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5542 }
5543
5544 /* Return TRUE if the language is C++.  */
5545
5546 static inline bool
5547 is_cxx (void)
5548 {
5549   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5550
5551   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5552 }
5553
5554 /* Return TRUE if the language is Fortran.  */
5555
5556 static inline bool
5557 is_fortran (void)
5558 {
5559   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5560
5561   return (lang == DW_LANG_Fortran77
5562           || lang == DW_LANG_Fortran90
5563           || lang == DW_LANG_Fortran95);
5564 }
5565
5566 /* Return TRUE if the language is Java.  */
5567
5568 static inline bool
5569 is_java (void)
5570 {
5571   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5572
5573   return lang == DW_LANG_Java;
5574 }
5575
5576 /* Return TRUE if the language is Ada.  */
5577
5578 static inline bool
5579 is_ada (void)
5580 {
5581   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5582
5583   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5584 }
5585
5586 /* Remove the specified attribute if present.  */
5587
5588 static void
5589 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5590 {
5591   dw_attr_ref a;
5592   unsigned ix;
5593
5594   if (! die)
5595     return;
5596
5597   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5598     if (a->dw_attr == attr_kind)
5599       {
5600         if (AT_class (a) == dw_val_class_str)
5601           if (a->dw_attr_val.v.val_str->refcount)
5602             a->dw_attr_val.v.val_str->refcount--;
5603
5604         /* VEC_ordered_remove should help reduce the number of abbrevs
5605            that are needed.  */
5606         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5607         return;
5608       }
5609 }
5610
5611 /* Remove CHILD from its parent.  PREV must have the property that
5612    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5613
5614 static void
5615 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5616 {
5617   gcc_assert (child->die_parent == prev->die_parent);
5618   gcc_assert (prev->die_sib == child);
5619   if (prev == child)
5620     {
5621       gcc_assert (child->die_parent->die_child == child);
5622       prev = NULL;
5623     }
5624   else
5625     prev->die_sib = child->die_sib;
5626   if (child->die_parent->die_child == child)
5627     child->die_parent->die_child = prev;
5628 }
5629
5630 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5631    matches TAG.  */
5632
5633 static void
5634 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5635 {
5636   dw_die_ref c;
5637
5638   c = die->die_child;
5639   if (c) do {
5640     dw_die_ref prev = c;
5641     c = c->die_sib;
5642     while (c->die_tag == tag)
5643       {
5644         remove_child_with_prev (c, prev);
5645         /* Might have removed every child.  */
5646         if (c == c->die_sib)
5647           return;
5648         c = c->die_sib;
5649       }
5650   } while (c != die->die_child);
5651 }
5652
5653 /* Add a CHILD_DIE as the last child of DIE.  */
5654
5655 static void
5656 add_child_die (dw_die_ref die, dw_die_ref child_die)
5657 {
5658   /* FIXME this should probably be an assert.  */
5659   if (! die || ! child_die)
5660     return;
5661   gcc_assert (die != child_die);
5662
5663   child_die->die_parent = die;
5664   if (die->die_child)
5665     {
5666       child_die->die_sib = die->die_child->die_sib;
5667       die->die_child->die_sib = child_die;
5668     }
5669   else
5670     child_die->die_sib = child_die;
5671   die->die_child = child_die;
5672 }
5673
5674 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5675    is the specification, to the end of PARENT's list of children.
5676    This is done by removing and re-adding it.  */
5677
5678 static void
5679 splice_child_die (dw_die_ref parent, dw_die_ref child)
5680 {
5681   dw_die_ref p;
5682
5683   /* We want the declaration DIE from inside the class, not the
5684      specification DIE at toplevel.  */
5685   if (child->die_parent != parent)
5686     {
5687       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5688
5689       if (tmp)
5690         child = tmp;
5691     }
5692
5693   gcc_assert (child->die_parent == parent
5694               || (child->die_parent
5695                   == get_AT_ref (parent, DW_AT_specification)));
5696
5697   for (p = child->die_parent->die_child; ; p = p->die_sib)
5698     if (p->die_sib == child)
5699       {
5700         remove_child_with_prev (child, p);
5701         break;
5702       }
5703
5704   add_child_die (parent, child);
5705 }
5706
5707 /* Return a pointer to a newly created DIE node.  */
5708
5709 static inline dw_die_ref
5710 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5711 {
5712   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5713
5714   die->die_tag = tag_value;
5715
5716   if (parent_die != NULL)
5717     add_child_die (parent_die, die);
5718   else
5719     {
5720       limbo_die_node *limbo_node;
5721
5722       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5723       limbo_node->die = die;
5724       limbo_node->created_for = t;
5725       limbo_node->next = limbo_die_list;
5726       limbo_die_list = limbo_node;
5727     }
5728
5729   return die;
5730 }
5731
5732 /* Return the DIE associated with the given type specifier.  */
5733
5734 static inline dw_die_ref
5735 lookup_type_die (tree type)
5736 {
5737   return TYPE_SYMTAB_DIE (type);
5738 }
5739
5740 /* Equate a DIE to a given type specifier.  */
5741
5742 static inline void
5743 equate_type_number_to_die (tree type, dw_die_ref type_die)
5744 {
5745   TYPE_SYMTAB_DIE (type) = type_die;
5746 }
5747
5748 /* Returns a hash value for X (which really is a die_struct).  */
5749
5750 static hashval_t
5751 decl_die_table_hash (const void *x)
5752 {
5753   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5754 }
5755
5756 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5757
5758 static int
5759 decl_die_table_eq (const void *x, const void *y)
5760 {
5761   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5762 }
5763
5764 /* Return the DIE associated with a given declaration.  */
5765
5766 static inline dw_die_ref
5767 lookup_decl_die (tree decl)
5768 {
5769   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5770 }
5771
5772 /* Returns a hash value for X (which really is a var_loc_list).  */
5773
5774 static hashval_t
5775 decl_loc_table_hash (const void *x)
5776 {
5777   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5778 }
5779
5780 /* Return nonzero if decl_id of var_loc_list X is the same as
5781    UID of decl *Y.  */
5782
5783 static int
5784 decl_loc_table_eq (const void *x, const void *y)
5785 {
5786   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5787 }
5788
5789 /* Return the var_loc list associated with a given declaration.  */
5790
5791 static inline var_loc_list *
5792 lookup_decl_loc (const_tree decl)
5793 {
5794   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5795 }
5796
5797 /* Equate a DIE to a particular declaration.  */
5798
5799 static void
5800 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5801 {
5802   unsigned int decl_id = DECL_UID (decl);
5803   void **slot;
5804
5805   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5806   *slot = decl_die;
5807   decl_die->decl_id = decl_id;
5808 }
5809
5810 /* Add a variable location node to the linked list for DECL.  */
5811
5812 static void
5813 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5814 {
5815   unsigned int decl_id = DECL_UID (decl);
5816   var_loc_list *temp;
5817   void **slot;
5818
5819   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5820   if (*slot == NULL)
5821     {
5822       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5823       temp->decl_id = decl_id;
5824       *slot = temp;
5825     }
5826   else
5827     temp = *slot;
5828
5829   if (temp->last)
5830     {
5831       /* If the current location is the same as the end of the list,
5832          and either both or neither of the locations is uninitialized,
5833          we have nothing to do.  */
5834       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5835                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5836           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5837                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5838               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5839                    == VAR_INIT_STATUS_UNINITIALIZED)
5840                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5841                       == VAR_INIT_STATUS_UNINITIALIZED))))
5842         {
5843           /* Add LOC to the end of list and update LAST.  */
5844           temp->last->next = loc;
5845           temp->last = loc;
5846         }
5847     }
5848   /* Do not add empty location to the beginning of the list.  */
5849   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5850     {
5851       temp->first = loc;
5852       temp->last = loc;
5853     }
5854 }
5855 \f
5856 /* Keep track of the number of spaces used to indent the
5857    output of the debugging routines that print the structure of
5858    the DIE internal representation.  */
5859 static int print_indent;
5860
5861 /* Indent the line the number of spaces given by print_indent.  */
5862
5863 static inline void
5864 print_spaces (FILE *outfile)
5865 {
5866   fprintf (outfile, "%*s", print_indent, "");
5867 }
5868
5869 /* Print the information associated with a given DIE, and its children.
5870    This routine is a debugging aid only.  */
5871
5872 static void
5873 print_die (dw_die_ref die, FILE *outfile)
5874 {
5875   dw_attr_ref a;
5876   dw_die_ref c;
5877   unsigned ix;
5878
5879   print_spaces (outfile);
5880   fprintf (outfile, "DIE %4ld: %s\n",
5881            die->die_offset, dwarf_tag_name (die->die_tag));
5882   print_spaces (outfile);
5883   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5884   fprintf (outfile, " offset: %ld\n", die->die_offset);
5885
5886   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5887     {
5888       print_spaces (outfile);
5889       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5890
5891       switch (AT_class (a))
5892         {
5893         case dw_val_class_addr:
5894           fprintf (outfile, "address");
5895           break;
5896         case dw_val_class_offset:
5897           fprintf (outfile, "offset");
5898           break;
5899         case dw_val_class_loc:
5900           fprintf (outfile, "location descriptor");
5901           break;
5902         case dw_val_class_loc_list:
5903           fprintf (outfile, "location list -> label:%s",
5904                    AT_loc_list (a)->ll_symbol);
5905           break;
5906         case dw_val_class_range_list:
5907           fprintf (outfile, "range list");
5908           break;
5909         case dw_val_class_const:
5910           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5911           break;
5912         case dw_val_class_unsigned_const:
5913           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5914           break;
5915         case dw_val_class_long_long:
5916           fprintf (outfile, "constant (%lu,%lu)",
5917                    a->dw_attr_val.v.val_long_long.hi,
5918                    a->dw_attr_val.v.val_long_long.low);
5919           break;
5920         case dw_val_class_vec:
5921           fprintf (outfile, "floating-point or vector constant");
5922           break;
5923         case dw_val_class_flag:
5924           fprintf (outfile, "%u", AT_flag (a));
5925           break;
5926         case dw_val_class_die_ref:
5927           if (AT_ref (a) != NULL)
5928             {
5929               if (AT_ref (a)->die_symbol)
5930                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5931               else
5932                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5933             }
5934           else
5935             fprintf (outfile, "die -> <null>");
5936           break;
5937         case dw_val_class_lbl_id:
5938         case dw_val_class_lineptr:
5939         case dw_val_class_macptr:
5940           fprintf (outfile, "label: %s", AT_lbl (a));
5941           break;
5942         case dw_val_class_str:
5943           if (AT_string (a) != NULL)
5944             fprintf (outfile, "\"%s\"", AT_string (a));
5945           else
5946             fprintf (outfile, "<null>");
5947           break;
5948         case dw_val_class_file:
5949           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5950                    AT_file (a)->emitted_number);
5951           break;
5952         default:
5953           break;
5954         }
5955
5956       fprintf (outfile, "\n");
5957     }
5958
5959   if (die->die_child != NULL)
5960     {
5961       print_indent += 4;
5962       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5963       print_indent -= 4;
5964     }
5965   if (print_indent == 0)
5966     fprintf (outfile, "\n");
5967 }
5968
5969 /* Print the contents of the source code line number correspondence table.
5970    This routine is a debugging aid only.  */
5971
5972 static void
5973 print_dwarf_line_table (FILE *outfile)
5974 {
5975   unsigned i;
5976   dw_line_info_ref line_info;
5977
5978   fprintf (outfile, "\n\nDWARF source line information\n");
5979   for (i = 1; i < line_info_table_in_use; i++)
5980     {
5981       line_info = &line_info_table[i];
5982       fprintf (outfile, "%5d: %4ld %6ld\n", i,
5983                line_info->dw_file_num,
5984                line_info->dw_line_num);
5985     }
5986
5987   fprintf (outfile, "\n\n");
5988 }
5989
5990 /* Print the information collected for a given DIE.  */
5991
5992 void
5993 debug_dwarf_die (dw_die_ref die)
5994 {
5995   print_die (die, stderr);
5996 }
5997
5998 /* Print all DWARF information collected for the compilation unit.
5999    This routine is a debugging aid only.  */
6000
6001 void
6002 debug_dwarf (void)
6003 {
6004   print_indent = 0;
6005   print_die (comp_unit_die, stderr);
6006   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6007     print_dwarf_line_table (stderr);
6008 }
6009 \f
6010 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6011    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6012    DIE that marks the start of the DIEs for this include file.  */
6013
6014 static dw_die_ref
6015 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6016 {
6017   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6018   dw_die_ref new_unit = gen_compile_unit_die (filename);
6019
6020   new_unit->die_sib = old_unit;
6021   return new_unit;
6022 }
6023
6024 /* Close an include-file CU and reopen the enclosing one.  */
6025
6026 static dw_die_ref
6027 pop_compile_unit (dw_die_ref old_unit)
6028 {
6029   dw_die_ref new_unit = old_unit->die_sib;
6030
6031   old_unit->die_sib = NULL;
6032   return new_unit;
6033 }
6034
6035 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6036 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6037
6038 /* Calculate the checksum of a location expression.  */
6039
6040 static inline void
6041 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6042 {
6043   CHECKSUM (loc->dw_loc_opc);
6044   CHECKSUM (loc->dw_loc_oprnd1);
6045   CHECKSUM (loc->dw_loc_oprnd2);
6046 }
6047
6048 /* Calculate the checksum of an attribute.  */
6049
6050 static void
6051 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6052 {
6053   dw_loc_descr_ref loc;
6054   rtx r;
6055
6056   CHECKSUM (at->dw_attr);
6057
6058   /* We don't care that this was compiled with a different compiler
6059      snapshot; if the output is the same, that's what matters.  */
6060   if (at->dw_attr == DW_AT_producer)
6061     return;
6062
6063   switch (AT_class (at))
6064     {
6065     case dw_val_class_const:
6066       CHECKSUM (at->dw_attr_val.v.val_int);
6067       break;
6068     case dw_val_class_unsigned_const:
6069       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6070       break;
6071     case dw_val_class_long_long:
6072       CHECKSUM (at->dw_attr_val.v.val_long_long);
6073       break;
6074     case dw_val_class_vec:
6075       CHECKSUM (at->dw_attr_val.v.val_vec);
6076       break;
6077     case dw_val_class_flag:
6078       CHECKSUM (at->dw_attr_val.v.val_flag);
6079       break;
6080     case dw_val_class_str:
6081       CHECKSUM_STRING (AT_string (at));
6082       break;
6083
6084     case dw_val_class_addr:
6085       r = AT_addr (at);
6086       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6087       CHECKSUM_STRING (XSTR (r, 0));
6088       break;
6089
6090     case dw_val_class_offset:
6091       CHECKSUM (at->dw_attr_val.v.val_offset);
6092       break;
6093
6094     case dw_val_class_loc:
6095       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6096         loc_checksum (loc, ctx);
6097       break;
6098
6099     case dw_val_class_die_ref:
6100       die_checksum (AT_ref (at), ctx, mark);
6101       break;
6102
6103     case dw_val_class_fde_ref:
6104     case dw_val_class_lbl_id:
6105     case dw_val_class_lineptr:
6106     case dw_val_class_macptr:
6107       break;
6108
6109     case dw_val_class_file:
6110       CHECKSUM_STRING (AT_file (at)->filename);
6111       break;
6112
6113     default:
6114       break;
6115     }
6116 }
6117
6118 /* Calculate the checksum of a DIE.  */
6119
6120 static void
6121 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6122 {
6123   dw_die_ref c;
6124   dw_attr_ref a;
6125   unsigned ix;
6126
6127   /* To avoid infinite recursion.  */
6128   if (die->die_mark)
6129     {
6130       CHECKSUM (die->die_mark);
6131       return;
6132     }
6133   die->die_mark = ++(*mark);
6134
6135   CHECKSUM (die->die_tag);
6136
6137   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6138     attr_checksum (a, ctx, mark);
6139
6140   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6141 }
6142
6143 #undef CHECKSUM
6144 #undef CHECKSUM_STRING
6145
6146 /* Do the location expressions look same?  */
6147 static inline int
6148 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6149 {
6150   return loc1->dw_loc_opc == loc2->dw_loc_opc
6151          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6152          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6153 }
6154
6155 /* Do the values look the same?  */
6156 static int
6157 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6158 {
6159   dw_loc_descr_ref loc1, loc2;
6160   rtx r1, r2;
6161
6162   if (v1->val_class != v2->val_class)
6163     return 0;
6164
6165   switch (v1->val_class)
6166     {
6167     case dw_val_class_const:
6168       return v1->v.val_int == v2->v.val_int;
6169     case dw_val_class_unsigned_const:
6170       return v1->v.val_unsigned == v2->v.val_unsigned;
6171     case dw_val_class_long_long:
6172       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6173              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6174     case dw_val_class_vec:
6175       if (v1->v.val_vec.length != v2->v.val_vec.length
6176           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6177         return 0;
6178       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6179                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6180         return 0;
6181       return 1;
6182     case dw_val_class_flag:
6183       return v1->v.val_flag == v2->v.val_flag;
6184     case dw_val_class_str:
6185       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6186
6187     case dw_val_class_addr:
6188       r1 = v1->v.val_addr;
6189       r2 = v2->v.val_addr;
6190       if (GET_CODE (r1) != GET_CODE (r2))
6191         return 0;
6192       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6193       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6194
6195     case dw_val_class_offset:
6196       return v1->v.val_offset == v2->v.val_offset;
6197
6198     case dw_val_class_loc:
6199       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6200            loc1 && loc2;
6201            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6202         if (!same_loc_p (loc1, loc2, mark))
6203           return 0;
6204       return !loc1 && !loc2;
6205
6206     case dw_val_class_die_ref:
6207       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6208
6209     case dw_val_class_fde_ref:
6210     case dw_val_class_lbl_id:
6211     case dw_val_class_lineptr:
6212     case dw_val_class_macptr:
6213       return 1;
6214
6215     case dw_val_class_file:
6216       return v1->v.val_file == v2->v.val_file;
6217
6218     default:
6219       return 1;
6220     }
6221 }
6222
6223 /* Do the attributes look the same?  */
6224
6225 static int
6226 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6227 {
6228   if (at1->dw_attr != at2->dw_attr)
6229     return 0;
6230
6231   /* We don't care that this was compiled with a different compiler
6232      snapshot; if the output is the same, that's what matters. */
6233   if (at1->dw_attr == DW_AT_producer)
6234     return 1;
6235
6236   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6237 }
6238
6239 /* Do the dies look the same?  */
6240
6241 static int
6242 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6243 {
6244   dw_die_ref c1, c2;
6245   dw_attr_ref a1;
6246   unsigned ix;
6247
6248   /* To avoid infinite recursion.  */
6249   if (die1->die_mark)
6250     return die1->die_mark == die2->die_mark;
6251   die1->die_mark = die2->die_mark = ++(*mark);
6252
6253   if (die1->die_tag != die2->die_tag)
6254     return 0;
6255
6256   if (VEC_length (dw_attr_node, die1->die_attr)
6257       != VEC_length (dw_attr_node, die2->die_attr))
6258     return 0;
6259
6260   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6261     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6262       return 0;
6263
6264   c1 = die1->die_child;
6265   c2 = die2->die_child;
6266   if (! c1)
6267     {
6268       if (c2)
6269         return 0;
6270     }
6271   else
6272     for (;;)
6273       {
6274         if (!same_die_p (c1, c2, mark))
6275           return 0;
6276         c1 = c1->die_sib;
6277         c2 = c2->die_sib;
6278         if (c1 == die1->die_child)
6279           {
6280             if (c2 == die2->die_child)
6281               break;
6282             else
6283               return 0;
6284           }
6285     }
6286
6287   return 1;
6288 }
6289
6290 /* Do the dies look the same?  Wrapper around same_die_p.  */
6291
6292 static int
6293 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6294 {
6295   int mark = 0;
6296   int ret = same_die_p (die1, die2, &mark);
6297
6298   unmark_all_dies (die1);
6299   unmark_all_dies (die2);
6300
6301   return ret;
6302 }
6303
6304 /* The prefix to attach to symbols on DIEs in the current comdat debug
6305    info section.  */
6306 static char *comdat_symbol_id;
6307
6308 /* The index of the current symbol within the current comdat CU.  */
6309 static unsigned int comdat_symbol_number;
6310
6311 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6312    children, and set comdat_symbol_id accordingly.  */
6313
6314 static void
6315 compute_section_prefix (dw_die_ref unit_die)
6316 {
6317   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6318   const char *base = die_name ? lbasename (die_name) : "anonymous";
6319   char *name = alloca (strlen (base) + 64);
6320   char *p;
6321   int i, mark;
6322   unsigned char checksum[16];
6323   struct md5_ctx ctx;
6324
6325   /* Compute the checksum of the DIE, then append part of it as hex digits to
6326      the name filename of the unit.  */
6327
6328   md5_init_ctx (&ctx);
6329   mark = 0;
6330   die_checksum (unit_die, &ctx, &mark);
6331   unmark_all_dies (unit_die);
6332   md5_finish_ctx (&ctx, checksum);
6333
6334   sprintf (name, "%s.", base);
6335   clean_symbol_name (name);
6336
6337   p = name + strlen (name);
6338   for (i = 0; i < 4; i++)
6339     {
6340       sprintf (p, "%.2x", checksum[i]);
6341       p += 2;
6342     }
6343
6344   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6345   comdat_symbol_number = 0;
6346 }
6347
6348 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6349
6350 static int
6351 is_type_die (dw_die_ref die)
6352 {
6353   switch (die->die_tag)
6354     {
6355     case DW_TAG_array_type:
6356     case DW_TAG_class_type:
6357     case DW_TAG_interface_type:
6358     case DW_TAG_enumeration_type:
6359     case DW_TAG_pointer_type:
6360     case DW_TAG_reference_type:
6361     case DW_TAG_string_type:
6362     case DW_TAG_structure_type:
6363     case DW_TAG_subroutine_type:
6364     case DW_TAG_union_type:
6365     case DW_TAG_ptr_to_member_type:
6366     case DW_TAG_set_type:
6367     case DW_TAG_subrange_type:
6368     case DW_TAG_base_type:
6369     case DW_TAG_const_type:
6370     case DW_TAG_file_type:
6371     case DW_TAG_packed_type:
6372     case DW_TAG_volatile_type:
6373     case DW_TAG_typedef:
6374       return 1;
6375     default:
6376       return 0;
6377     }
6378 }
6379
6380 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6381    Basically, we want to choose the bits that are likely to be shared between
6382    compilations (types) and leave out the bits that are specific to individual
6383    compilations (functions).  */
6384
6385 static int
6386 is_comdat_die (dw_die_ref c)
6387 {
6388   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6389      we do for stabs.  The advantage is a greater likelihood of sharing between
6390      objects that don't include headers in the same order (and therefore would
6391      put the base types in a different comdat).  jason 8/28/00 */
6392
6393   if (c->die_tag == DW_TAG_base_type)
6394     return 0;
6395
6396   if (c->die_tag == DW_TAG_pointer_type
6397       || c->die_tag == DW_TAG_reference_type
6398       || c->die_tag == DW_TAG_const_type
6399       || c->die_tag == DW_TAG_volatile_type)
6400     {
6401       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6402
6403       return t ? is_comdat_die (t) : 0;
6404     }
6405
6406   return is_type_die (c);
6407 }
6408
6409 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6410    compilation unit.  */
6411
6412 static int
6413 is_symbol_die (dw_die_ref c)
6414 {
6415   return (is_type_die (c)
6416           || (get_AT (c, DW_AT_declaration)
6417               && !get_AT (c, DW_AT_specification))
6418           || c->die_tag == DW_TAG_namespace);
6419 }
6420
6421 static char *
6422 gen_internal_sym (const char *prefix)
6423 {
6424   char buf[256];
6425
6426   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6427   return xstrdup (buf);
6428 }
6429
6430 /* Assign symbols to all worthy DIEs under DIE.  */
6431
6432 static void
6433 assign_symbol_names (dw_die_ref die)
6434 {
6435   dw_die_ref c;
6436
6437   if (is_symbol_die (die))
6438     {
6439       if (comdat_symbol_id)
6440         {
6441           char *p = alloca (strlen (comdat_symbol_id) + 64);
6442
6443           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6444                    comdat_symbol_id, comdat_symbol_number++);
6445           die->die_symbol = xstrdup (p);
6446         }
6447       else
6448         die->die_symbol = gen_internal_sym ("LDIE");
6449     }
6450
6451   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6452 }
6453
6454 struct cu_hash_table_entry
6455 {
6456   dw_die_ref cu;
6457   unsigned min_comdat_num, max_comdat_num;
6458   struct cu_hash_table_entry *next;
6459 };
6460
6461 /* Routines to manipulate hash table of CUs.  */
6462 static hashval_t
6463 htab_cu_hash (const void *of)
6464 {
6465   const struct cu_hash_table_entry *entry = of;
6466
6467   return htab_hash_string (entry->cu->die_symbol);
6468 }
6469
6470 static int
6471 htab_cu_eq (const void *of1, const void *of2)
6472 {
6473   const struct cu_hash_table_entry *entry1 = of1;
6474   const struct die_struct *entry2 = of2;
6475
6476   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6477 }
6478
6479 static void
6480 htab_cu_del (void *what)
6481 {
6482   struct cu_hash_table_entry *next, *entry = what;
6483
6484   while (entry)
6485     {
6486       next = entry->next;
6487       free (entry);
6488       entry = next;
6489     }
6490 }
6491
6492 /* Check whether we have already seen this CU and set up SYM_NUM
6493    accordingly.  */
6494 static int
6495 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6496 {
6497   struct cu_hash_table_entry dummy;
6498   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6499
6500   dummy.max_comdat_num = 0;
6501
6502   slot = (struct cu_hash_table_entry **)
6503     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6504         INSERT);
6505   entry = *slot;
6506
6507   for (; entry; last = entry, entry = entry->next)
6508     {
6509       if (same_die_p_wrap (cu, entry->cu))
6510         break;
6511     }
6512
6513   if (entry)
6514     {
6515       *sym_num = entry->min_comdat_num;
6516       return 1;
6517     }
6518
6519   entry = XCNEW (struct cu_hash_table_entry);
6520   entry->cu = cu;
6521   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6522   entry->next = *slot;
6523   *slot = entry;
6524
6525   return 0;
6526 }
6527
6528 /* Record SYM_NUM to record of CU in HTABLE.  */
6529 static void
6530 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6531 {
6532   struct cu_hash_table_entry **slot, *entry;
6533
6534   slot = (struct cu_hash_table_entry **)
6535     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6536         NO_INSERT);
6537   entry = *slot;
6538
6539   entry->max_comdat_num = sym_num;
6540 }
6541
6542 /* Traverse the DIE (which is always comp_unit_die), and set up
6543    additional compilation units for each of the include files we see
6544    bracketed by BINCL/EINCL.  */
6545
6546 static void
6547 break_out_includes (dw_die_ref die)
6548 {
6549   dw_die_ref c;
6550   dw_die_ref unit = NULL;
6551   limbo_die_node *node, **pnode;
6552   htab_t cu_hash_table;
6553
6554   c = die->die_child;
6555   if (c) do {
6556     dw_die_ref prev = c;
6557     c = c->die_sib;
6558     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6559            || (unit && is_comdat_die (c)))
6560       {
6561         dw_die_ref next = c->die_sib;
6562
6563         /* This DIE is for a secondary CU; remove it from the main one.  */
6564         remove_child_with_prev (c, prev);
6565
6566         if (c->die_tag == DW_TAG_GNU_BINCL)
6567           unit = push_new_compile_unit (unit, c);
6568         else if (c->die_tag == DW_TAG_GNU_EINCL)
6569           unit = pop_compile_unit (unit);
6570         else
6571           add_child_die (unit, c);
6572         c = next;
6573         if (c == die->die_child)
6574           break;
6575       }
6576   } while (c != die->die_child);
6577
6578 #if 0
6579   /* We can only use this in debugging, since the frontend doesn't check
6580      to make sure that we leave every include file we enter.  */
6581   gcc_assert (!unit);
6582 #endif
6583
6584   assign_symbol_names (die);
6585   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6586   for (node = limbo_die_list, pnode = &limbo_die_list;
6587        node;
6588        node = node->next)
6589     {
6590       int is_dupl;
6591
6592       compute_section_prefix (node->die);
6593       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6594                         &comdat_symbol_number);
6595       assign_symbol_names (node->die);
6596       if (is_dupl)
6597         *pnode = node->next;
6598       else
6599         {
6600           pnode = &node->next;
6601           record_comdat_symbol_number (node->die, cu_hash_table,
6602                 comdat_symbol_number);
6603         }
6604     }
6605   htab_delete (cu_hash_table);
6606 }
6607
6608 /* Traverse the DIE and add a sibling attribute if it may have the
6609    effect of speeding up access to siblings.  To save some space,
6610    avoid generating sibling attributes for DIE's without children.  */
6611
6612 static void
6613 add_sibling_attributes (dw_die_ref die)
6614 {
6615   dw_die_ref c;
6616
6617   if (! die->die_child)
6618     return;
6619
6620   if (die->die_parent && die != die->die_parent->die_child)
6621     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6622
6623   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6624 }
6625
6626 /* Output all location lists for the DIE and its children.  */
6627
6628 static void
6629 output_location_lists (dw_die_ref die)
6630 {
6631   dw_die_ref c;
6632   dw_attr_ref a;
6633   unsigned ix;
6634
6635   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6636     if (AT_class (a) == dw_val_class_loc_list)
6637       output_loc_list (AT_loc_list (a));
6638
6639   FOR_EACH_CHILD (die, c, output_location_lists (c));
6640 }
6641
6642 /* The format of each DIE (and its attribute value pairs) is encoded in an
6643    abbreviation table.  This routine builds the abbreviation table and assigns
6644    a unique abbreviation id for each abbreviation entry.  The children of each
6645    die are visited recursively.  */
6646
6647 static void
6648 build_abbrev_table (dw_die_ref die)
6649 {
6650   unsigned long abbrev_id;
6651   unsigned int n_alloc;
6652   dw_die_ref c;
6653   dw_attr_ref a;
6654   unsigned ix;
6655
6656   /* Scan the DIE references, and mark as external any that refer to
6657      DIEs from other CUs (i.e. those which are not marked).  */
6658   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6659     if (AT_class (a) == dw_val_class_die_ref
6660         && AT_ref (a)->die_mark == 0)
6661       {
6662         gcc_assert (AT_ref (a)->die_symbol);
6663
6664         set_AT_ref_external (a, 1);
6665       }
6666
6667   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6668     {
6669       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6670       dw_attr_ref die_a, abbrev_a;
6671       unsigned ix;
6672       bool ok = true;
6673
6674       if (abbrev->die_tag != die->die_tag)
6675         continue;
6676       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6677         continue;
6678
6679       if (VEC_length (dw_attr_node, abbrev->die_attr)
6680           != VEC_length (dw_attr_node, die->die_attr))
6681         continue;
6682
6683       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6684         {
6685           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6686           if ((abbrev_a->dw_attr != die_a->dw_attr)
6687               || (value_format (abbrev_a) != value_format (die_a)))
6688             {
6689               ok = false;
6690               break;
6691             }
6692         }
6693       if (ok)
6694         break;
6695     }
6696
6697   if (abbrev_id >= abbrev_die_table_in_use)
6698     {
6699       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6700         {
6701           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6702           abbrev_die_table = ggc_realloc (abbrev_die_table,
6703                                           sizeof (dw_die_ref) * n_alloc);
6704
6705           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6706                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6707           abbrev_die_table_allocated = n_alloc;
6708         }
6709
6710       ++abbrev_die_table_in_use;
6711       abbrev_die_table[abbrev_id] = die;
6712     }
6713
6714   die->die_abbrev = abbrev_id;
6715   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6716 }
6717 \f
6718 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6719
6720 static int
6721 constant_size (long unsigned int value)
6722 {
6723   int log;
6724
6725   if (value == 0)
6726     log = 0;
6727   else
6728     log = floor_log2 (value);
6729
6730   log = log / 8;
6731   log = 1 << (floor_log2 (log) + 1);
6732
6733   return log;
6734 }
6735
6736 /* Return the size of a DIE as it is represented in the
6737    .debug_info section.  */
6738
6739 static unsigned long
6740 size_of_die (dw_die_ref die)
6741 {
6742   unsigned long size = 0;
6743   dw_attr_ref a;
6744   unsigned ix;
6745
6746   size += size_of_uleb128 (die->die_abbrev);
6747   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6748     {
6749       switch (AT_class (a))
6750         {
6751         case dw_val_class_addr:
6752           size += DWARF2_ADDR_SIZE;
6753           break;
6754         case dw_val_class_offset:
6755           size += DWARF_OFFSET_SIZE;
6756           break;
6757         case dw_val_class_loc:
6758           {
6759             unsigned long lsize = size_of_locs (AT_loc (a));
6760
6761             /* Block length.  */
6762             size += constant_size (lsize);
6763             size += lsize;
6764           }
6765           break;
6766         case dw_val_class_loc_list:
6767           size += DWARF_OFFSET_SIZE;
6768           break;
6769         case dw_val_class_range_list:
6770           size += DWARF_OFFSET_SIZE;
6771           break;
6772         case dw_val_class_const:
6773           size += size_of_sleb128 (AT_int (a));
6774           break;
6775         case dw_val_class_unsigned_const:
6776           size += constant_size (AT_unsigned (a));
6777           break;
6778         case dw_val_class_long_long:
6779           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6780           break;
6781         case dw_val_class_vec:
6782           size += 1 + (a->dw_attr_val.v.val_vec.length
6783                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6784           break;
6785         case dw_val_class_flag:
6786           size += 1;
6787           break;
6788         case dw_val_class_die_ref:
6789           if (AT_ref_external (a))
6790             size += DWARF2_ADDR_SIZE;
6791           else
6792             size += DWARF_OFFSET_SIZE;
6793           break;
6794         case dw_val_class_fde_ref:
6795           size += DWARF_OFFSET_SIZE;
6796           break;
6797         case dw_val_class_lbl_id:
6798           size += DWARF2_ADDR_SIZE;
6799           break;
6800         case dw_val_class_lineptr:
6801         case dw_val_class_macptr:
6802           size += DWARF_OFFSET_SIZE;
6803           break;
6804         case dw_val_class_str:
6805           if (AT_string_form (a) == DW_FORM_strp)
6806             size += DWARF_OFFSET_SIZE;
6807           else
6808             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6809           break;
6810         case dw_val_class_file:
6811           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6812           break;
6813         default:
6814           gcc_unreachable ();
6815         }
6816     }
6817
6818   return size;
6819 }
6820
6821 /* Size the debugging information associated with a given DIE.  Visits the
6822    DIE's children recursively.  Updates the global variable next_die_offset, on
6823    each time through.  Uses the current value of next_die_offset to update the
6824    die_offset field in each DIE.  */
6825
6826 static void
6827 calc_die_sizes (dw_die_ref die)
6828 {
6829   dw_die_ref c;
6830
6831   die->die_offset = next_die_offset;
6832   next_die_offset += size_of_die (die);
6833
6834   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6835
6836   if (die->die_child != NULL)
6837     /* Count the null byte used to terminate sibling lists.  */
6838     next_die_offset += 1;
6839 }
6840
6841 /* Set the marks for a die and its children.  We do this so
6842    that we know whether or not a reference needs to use FORM_ref_addr; only
6843    DIEs in the same CU will be marked.  We used to clear out the offset
6844    and use that as the flag, but ran into ordering problems.  */
6845
6846 static void
6847 mark_dies (dw_die_ref die)
6848 {
6849   dw_die_ref c;
6850
6851   gcc_assert (!die->die_mark);
6852
6853   die->die_mark = 1;
6854   FOR_EACH_CHILD (die, c, mark_dies (c));
6855 }
6856
6857 /* Clear the marks for a die and its children.  */
6858
6859 static void
6860 unmark_dies (dw_die_ref die)
6861 {
6862   dw_die_ref c;
6863
6864   gcc_assert (die->die_mark);
6865
6866   die->die_mark = 0;
6867   FOR_EACH_CHILD (die, c, unmark_dies (c));
6868 }
6869
6870 /* Clear the marks for a die, its children and referred dies.  */
6871
6872 static void
6873 unmark_all_dies (dw_die_ref die)
6874 {
6875   dw_die_ref c;
6876   dw_attr_ref a;
6877   unsigned ix;
6878
6879   if (!die->die_mark)
6880     return;
6881   die->die_mark = 0;
6882
6883   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6884
6885   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6886     if (AT_class (a) == dw_val_class_die_ref)
6887       unmark_all_dies (AT_ref (a));
6888 }
6889
6890 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6891    generated for the compilation unit.  */
6892
6893 static unsigned long
6894 size_of_pubnames (VEC (pubname_entry, gc) * names)
6895 {
6896   unsigned long size;
6897   unsigned i;
6898   pubname_ref p;
6899
6900   size = DWARF_PUBNAMES_HEADER_SIZE;
6901   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6902     if (names != pubtype_table
6903         || p->die->die_offset != 0
6904         || !flag_eliminate_unused_debug_types)
6905       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6906
6907   size += DWARF_OFFSET_SIZE;
6908   return size;
6909 }
6910
6911 /* Return the size of the information in the .debug_aranges section.  */
6912
6913 static unsigned long
6914 size_of_aranges (void)
6915 {
6916   unsigned long size;
6917
6918   size = DWARF_ARANGES_HEADER_SIZE;
6919
6920   /* Count the address/length pair for this compilation unit.  */
6921   if (text_section_used)
6922     size += 2 * DWARF2_ADDR_SIZE;
6923   if (cold_text_section_used)
6924     size += 2 * DWARF2_ADDR_SIZE;
6925   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6926
6927   /* Count the two zero words used to terminated the address range table.  */
6928   size += 2 * DWARF2_ADDR_SIZE;
6929   return size;
6930 }
6931 \f
6932 /* Select the encoding of an attribute value.  */
6933
6934 static enum dwarf_form
6935 value_format (dw_attr_ref a)
6936 {
6937   switch (a->dw_attr_val.val_class)
6938     {
6939     case dw_val_class_addr:
6940       return DW_FORM_addr;
6941     case dw_val_class_range_list:
6942     case dw_val_class_offset:
6943     case dw_val_class_loc_list:
6944       switch (DWARF_OFFSET_SIZE)
6945         {
6946         case 4:
6947           return DW_FORM_data4;
6948         case 8:
6949           return DW_FORM_data8;
6950         default:
6951           gcc_unreachable ();
6952         }
6953     case dw_val_class_loc:
6954       switch (constant_size (size_of_locs (AT_loc (a))))
6955         {
6956         case 1:
6957           return DW_FORM_block1;
6958         case 2:
6959           return DW_FORM_block2;
6960         default:
6961           gcc_unreachable ();
6962         }
6963     case dw_val_class_const:
6964       return DW_FORM_sdata;
6965     case dw_val_class_unsigned_const:
6966       switch (constant_size (AT_unsigned (a)))
6967         {
6968         case 1:
6969           return DW_FORM_data1;
6970         case 2:
6971           return DW_FORM_data2;
6972         case 4:
6973           return DW_FORM_data4;
6974         case 8:
6975           return DW_FORM_data8;
6976         default:
6977           gcc_unreachable ();
6978         }
6979     case dw_val_class_long_long:
6980       return DW_FORM_block1;
6981     case dw_val_class_vec:
6982       return DW_FORM_block1;
6983     case dw_val_class_flag:
6984       return DW_FORM_flag;
6985     case dw_val_class_die_ref:
6986       if (AT_ref_external (a))
6987         return DW_FORM_ref_addr;
6988       else
6989         return DW_FORM_ref;
6990     case dw_val_class_fde_ref:
6991       return DW_FORM_data;
6992     case dw_val_class_lbl_id:
6993       return DW_FORM_addr;
6994     case dw_val_class_lineptr:
6995     case dw_val_class_macptr:
6996       return DW_FORM_data;
6997     case dw_val_class_str:
6998       return AT_string_form (a);
6999     case dw_val_class_file:
7000       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7001         {
7002         case 1:
7003           return DW_FORM_data1;
7004         case 2:
7005           return DW_FORM_data2;
7006         case 4:
7007           return DW_FORM_data4;
7008         default:
7009           gcc_unreachable ();
7010         }
7011
7012     default:
7013       gcc_unreachable ();
7014     }
7015 }
7016
7017 /* Output the encoding of an attribute value.  */
7018
7019 static void
7020 output_value_format (dw_attr_ref a)
7021 {
7022   enum dwarf_form form = value_format (a);
7023
7024   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7025 }
7026
7027 /* Output the .debug_abbrev section which defines the DIE abbreviation
7028    table.  */
7029
7030 static void
7031 output_abbrev_section (void)
7032 {
7033   unsigned long abbrev_id;
7034
7035   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7036     {
7037       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7038       unsigned ix;
7039       dw_attr_ref a_attr;
7040
7041       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7042       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7043                                    dwarf_tag_name (abbrev->die_tag));
7044
7045       if (abbrev->die_child != NULL)
7046         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7047       else
7048         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7049
7050       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7051            ix++)
7052         {
7053           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7054                                        dwarf_attr_name (a_attr->dw_attr));
7055           output_value_format (a_attr);
7056         }
7057
7058       dw2_asm_output_data (1, 0, NULL);
7059       dw2_asm_output_data (1, 0, NULL);
7060     }
7061
7062   /* Terminate the table.  */
7063   dw2_asm_output_data (1, 0, NULL);
7064 }
7065
7066 /* Output a symbol we can use to refer to this DIE from another CU.  */
7067
7068 static inline void
7069 output_die_symbol (dw_die_ref die)
7070 {
7071   char *sym = die->die_symbol;
7072
7073   if (sym == 0)
7074     return;
7075
7076   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7077     /* We make these global, not weak; if the target doesn't support
7078        .linkonce, it doesn't support combining the sections, so debugging
7079        will break.  */
7080     targetm.asm_out.globalize_label (asm_out_file, sym);
7081
7082   ASM_OUTPUT_LABEL (asm_out_file, sym);
7083 }
7084
7085 /* Return a new location list, given the begin and end range, and the
7086    expression. gensym tells us whether to generate a new internal symbol for
7087    this location list node, which is done for the head of the list only.  */
7088
7089 static inline dw_loc_list_ref
7090 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7091               const char *section, unsigned int gensym)
7092 {
7093   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7094
7095   retlist->begin = begin;
7096   retlist->end = end;
7097   retlist->expr = expr;
7098   retlist->section = section;
7099   if (gensym)
7100     retlist->ll_symbol = gen_internal_sym ("LLST");
7101
7102   return retlist;
7103 }
7104
7105 /* Add a location description expression to a location list.  */
7106
7107 static inline void
7108 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7109                            const char *begin, const char *end,
7110                            const char *section)
7111 {
7112   dw_loc_list_ref *d;
7113
7114   /* Find the end of the chain.  */
7115   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7116     ;
7117
7118   /* Add a new location list node to the list.  */
7119   *d = new_loc_list (descr, begin, end, section, 0);
7120 }
7121
7122 /* Output the location list given to us.  */
7123
7124 static void
7125 output_loc_list (dw_loc_list_ref list_head)
7126 {
7127   dw_loc_list_ref curr = list_head;
7128
7129   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7130
7131   /* Walk the location list, and output each range + expression.  */
7132   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7133     {
7134       unsigned long size;
7135       /* Don't output an entry that starts and ends at the same address.  */
7136       if (strcmp (curr->begin, curr->end) == 0)
7137         continue;
7138       if (!have_multiple_function_sections)
7139         {
7140           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7141                                 "Location list begin address (%s)",
7142                                 list_head->ll_symbol);
7143           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7144                                 "Location list end address (%s)",
7145                                 list_head->ll_symbol);
7146         }
7147       else
7148         {
7149           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7150                                "Location list begin address (%s)",
7151                                list_head->ll_symbol);
7152           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7153                                "Location list end address (%s)",
7154                                list_head->ll_symbol);
7155         }
7156       size = size_of_locs (curr->expr);
7157
7158       /* Output the block length for this list of location operations.  */
7159       gcc_assert (size <= 0xffff);
7160       dw2_asm_output_data (2, size, "%s", "Location expression size");
7161
7162       output_loc_sequence (curr->expr);
7163     }
7164
7165   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7166                        "Location list terminator begin (%s)",
7167                        list_head->ll_symbol);
7168   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7169                        "Location list terminator end (%s)",
7170                        list_head->ll_symbol);
7171 }
7172
7173 /* Output the DIE and its attributes.  Called recursively to generate
7174    the definitions of each child DIE.  */
7175
7176 static void
7177 output_die (dw_die_ref die)
7178 {
7179   dw_attr_ref a;
7180   dw_die_ref c;
7181   unsigned long size;
7182   unsigned ix;
7183
7184   /* If someone in another CU might refer to us, set up a symbol for
7185      them to point to.  */
7186   if (die->die_symbol)
7187     output_die_symbol (die);
7188
7189   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7190                                (unsigned long)die->die_offset,
7191                                dwarf_tag_name (die->die_tag));
7192
7193   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7194     {
7195       const char *name = dwarf_attr_name (a->dw_attr);
7196
7197       switch (AT_class (a))
7198         {
7199         case dw_val_class_addr:
7200           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7201           break;
7202
7203         case dw_val_class_offset:
7204           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7205                                "%s", name);
7206           break;
7207
7208         case dw_val_class_range_list:
7209           {
7210             char *p = strchr (ranges_section_label, '\0');
7211
7212             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7213                      a->dw_attr_val.v.val_offset);
7214             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7215                                    debug_ranges_section, "%s", name);
7216             *p = '\0';
7217           }
7218           break;
7219
7220         case dw_val_class_loc:
7221           size = size_of_locs (AT_loc (a));
7222
7223           /* Output the block length for this list of location operations.  */
7224           dw2_asm_output_data (constant_size (size), size, "%s", name);
7225
7226           output_loc_sequence (AT_loc (a));
7227           break;
7228
7229         case dw_val_class_const:
7230           /* ??? It would be slightly more efficient to use a scheme like is
7231              used for unsigned constants below, but gdb 4.x does not sign
7232              extend.  Gdb 5.x does sign extend.  */
7233           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7234           break;
7235
7236         case dw_val_class_unsigned_const:
7237           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7238                                AT_unsigned (a), "%s", name);
7239           break;
7240
7241         case dw_val_class_long_long:
7242           {
7243             unsigned HOST_WIDE_INT first, second;
7244
7245             dw2_asm_output_data (1,
7246                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7247                                  "%s", name);
7248
7249             if (WORDS_BIG_ENDIAN)
7250               {
7251                 first = a->dw_attr_val.v.val_long_long.hi;
7252                 second = a->dw_attr_val.v.val_long_long.low;
7253               }
7254             else
7255               {
7256                 first = a->dw_attr_val.v.val_long_long.low;
7257                 second = a->dw_attr_val.v.val_long_long.hi;
7258               }
7259
7260             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7261                                  first, "long long constant");
7262             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7263                                  second, NULL);
7264           }
7265           break;
7266
7267         case dw_val_class_vec:
7268           {
7269             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7270             unsigned int len = a->dw_attr_val.v.val_vec.length;
7271             unsigned int i;
7272             unsigned char *p;
7273
7274             dw2_asm_output_data (1, len * elt_size, "%s", name);
7275             if (elt_size > sizeof (HOST_WIDE_INT))
7276               {
7277                 elt_size /= 2;
7278                 len *= 2;
7279               }
7280             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7281                  i < len;
7282                  i++, p += elt_size)
7283               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7284                                    "fp or vector constant word %u", i);
7285             break;
7286           }
7287
7288         case dw_val_class_flag:
7289           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7290           break;
7291
7292         case dw_val_class_loc_list:
7293           {
7294             char *sym = AT_loc_list (a)->ll_symbol;
7295
7296             gcc_assert (sym);
7297             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7298                                    "%s", name);
7299           }
7300           break;
7301
7302         case dw_val_class_die_ref:
7303           if (AT_ref_external (a))
7304             {
7305               char *sym = AT_ref (a)->die_symbol;
7306
7307               gcc_assert (sym);
7308               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7309                                      "%s", name);
7310             }
7311           else
7312             {
7313               gcc_assert (AT_ref (a)->die_offset);
7314               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7315                                    "%s", name);
7316             }
7317           break;
7318
7319         case dw_val_class_fde_ref:
7320           {
7321             char l1[20];
7322
7323             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7324                                          a->dw_attr_val.v.val_fde_index * 2);
7325             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7326                                    "%s", name);
7327           }
7328           break;
7329
7330         case dw_val_class_lbl_id:
7331           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7332           break;
7333
7334         case dw_val_class_lineptr:
7335           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7336                                  debug_line_section, "%s", name);
7337           break;
7338
7339         case dw_val_class_macptr:
7340           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7341                                  debug_macinfo_section, "%s", name);
7342           break;
7343
7344         case dw_val_class_str:
7345           if (AT_string_form (a) == DW_FORM_strp)
7346             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7347                                    a->dw_attr_val.v.val_str->label,
7348                                    debug_str_section,
7349                                    "%s: \"%s\"", name, AT_string (a));
7350           else
7351             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7352           break;
7353
7354         case dw_val_class_file:
7355           {
7356             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7357
7358             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7359                                  a->dw_attr_val.v.val_file->filename);
7360             break;
7361           }
7362
7363         default:
7364           gcc_unreachable ();
7365         }
7366     }
7367
7368   FOR_EACH_CHILD (die, c, output_die (c));
7369
7370   /* Add null byte to terminate sibling list.  */
7371   if (die->die_child != NULL)
7372     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7373                          (unsigned long) die->die_offset);
7374 }
7375
7376 /* Output the compilation unit that appears at the beginning of the
7377    .debug_info section, and precedes the DIE descriptions.  */
7378
7379 static void
7380 output_compilation_unit_header (void)
7381 {
7382   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7383     dw2_asm_output_data (4, 0xffffffff,
7384       "Initial length escape value indicating 64-bit DWARF extension");
7385   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7386                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7387                        "Length of Compilation Unit Info");
7388   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7389   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7390                          debug_abbrev_section,
7391                          "Offset Into Abbrev. Section");
7392   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7393 }
7394
7395 /* Output the compilation unit DIE and its children.  */
7396
7397 static void
7398 output_comp_unit (dw_die_ref die, int output_if_empty)
7399 {
7400   const char *secname;
7401   char *oldsym, *tmp;
7402
7403   /* Unless we are outputting main CU, we may throw away empty ones.  */
7404   if (!output_if_empty && die->die_child == NULL)
7405     return;
7406
7407   /* Even if there are no children of this DIE, we must output the information
7408      about the compilation unit.  Otherwise, on an empty translation unit, we
7409      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7410      will then complain when examining the file.  First mark all the DIEs in
7411      this CU so we know which get local refs.  */
7412   mark_dies (die);
7413
7414   build_abbrev_table (die);
7415
7416   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7417   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7418   calc_die_sizes (die);
7419
7420   oldsym = die->die_symbol;
7421   if (oldsym)
7422     {
7423       tmp = alloca (strlen (oldsym) + 24);
7424
7425       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7426       secname = tmp;
7427       die->die_symbol = NULL;
7428       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7429     }
7430   else
7431     switch_to_section (debug_info_section);
7432
7433   /* Output debugging information.  */
7434   output_compilation_unit_header ();
7435   output_die (die);
7436
7437   /* Leave the marks on the main CU, so we can check them in
7438      output_pubnames.  */
7439   if (oldsym)
7440     {
7441       unmark_dies (die);
7442       die->die_symbol = oldsym;
7443     }
7444 }
7445
7446 /* Return the DWARF2/3 pubname associated with a decl.  */
7447
7448 static const char *
7449 dwarf2_name (tree decl, int scope)
7450 {
7451   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7452 }
7453
7454 /* Add a new entry to .debug_pubnames if appropriate.  */
7455
7456 static void
7457 add_pubname (tree decl, dw_die_ref die)
7458 {
7459   pubname_entry e;
7460
7461   if (! TREE_PUBLIC (decl))
7462     return;
7463
7464   e.die = die;
7465   e.name = xstrdup (dwarf2_name (decl, 1));
7466   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7467 }
7468
7469 /* Add a new entry to .debug_pubtypes if appropriate.  */
7470
7471 static void
7472 add_pubtype (tree decl, dw_die_ref die)
7473 {
7474   pubname_entry e;
7475
7476   e.name = NULL;
7477   if ((TREE_PUBLIC (decl)
7478        || die->die_parent == comp_unit_die)
7479       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7480     {
7481       e.die = die;
7482       if (TYPE_P (decl))
7483         {
7484           if (TYPE_NAME (decl))
7485             {
7486               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7487                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7488               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7489                        && DECL_NAME (TYPE_NAME (decl)))
7490                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7491               else
7492                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7493             }
7494         }
7495       else
7496         e.name = xstrdup (dwarf2_name (decl, 1));
7497
7498       /* If we don't have a name for the type, there's no point in adding
7499          it to the table.  */
7500       if (e.name && e.name[0] != '\0')
7501         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7502     }
7503 }
7504
7505 /* Output the public names table used to speed up access to externally
7506    visible names; or the public types table used to find type definitions.  */
7507
7508 static void
7509 output_pubnames (VEC (pubname_entry, gc) * names)
7510 {
7511   unsigned i;
7512   unsigned long pubnames_length = size_of_pubnames (names);
7513   pubname_ref pub;
7514
7515   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7516     dw2_asm_output_data (4, 0xffffffff,
7517       "Initial length escape value indicating 64-bit DWARF extension");
7518   if (names == pubname_table)
7519     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7520                          "Length of Public Names Info");
7521   else
7522     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7523                          "Length of Public Type Names Info");
7524   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7525   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7526                          debug_info_section,
7527                          "Offset of Compilation Unit Info");
7528   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7529                        "Compilation Unit Length");
7530
7531   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7532     {
7533       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7534       if (names == pubname_table)
7535         gcc_assert (pub->die->die_mark);
7536
7537       if (names != pubtype_table
7538           || pub->die->die_offset != 0
7539           || !flag_eliminate_unused_debug_types)
7540         {
7541           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7542                                "DIE offset");
7543
7544           dw2_asm_output_nstring (pub->name, -1, "external name");
7545         }
7546     }
7547
7548   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7549 }
7550
7551 /* Add a new entry to .debug_aranges if appropriate.  */
7552
7553 static void
7554 add_arange (tree decl, dw_die_ref die)
7555 {
7556   if (! DECL_SECTION_NAME (decl))
7557     return;
7558
7559   if (arange_table_in_use == arange_table_allocated)
7560     {
7561       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7562       arange_table = ggc_realloc (arange_table,
7563                                   (arange_table_allocated
7564                                    * sizeof (dw_die_ref)));
7565       memset (arange_table + arange_table_in_use, 0,
7566               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7567     }
7568
7569   arange_table[arange_table_in_use++] = die;
7570 }
7571
7572 /* Output the information that goes into the .debug_aranges table.
7573    Namely, define the beginning and ending address range of the
7574    text section generated for this compilation unit.  */
7575
7576 static void
7577 output_aranges (void)
7578 {
7579   unsigned i;
7580   unsigned long aranges_length = size_of_aranges ();
7581
7582   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7583     dw2_asm_output_data (4, 0xffffffff,
7584       "Initial length escape value indicating 64-bit DWARF extension");
7585   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7586                        "Length of Address Ranges Info");
7587   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7588   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7589                          debug_info_section,
7590                          "Offset of Compilation Unit Info");
7591   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7592   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7593
7594   /* We need to align to twice the pointer size here.  */
7595   if (DWARF_ARANGES_PAD_SIZE)
7596     {
7597       /* Pad using a 2 byte words so that padding is correct for any
7598          pointer size.  */
7599       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7600                            2 * DWARF2_ADDR_SIZE);
7601       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7602         dw2_asm_output_data (2, 0, NULL);
7603     }
7604
7605   /* It is necessary not to output these entries if the sections were
7606      not used; if the sections were not used, the length will be 0 and
7607      the address may end up as 0 if the section is discarded by ld
7608      --gc-sections, leaving an invalid (0, 0) entry that can be
7609      confused with the terminator.  */
7610   if (text_section_used)
7611     {
7612       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7613       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7614                             text_section_label, "Length");
7615     }
7616   if (cold_text_section_used)
7617     {
7618       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7619                            "Address");
7620       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7621                             cold_text_section_label, "Length");
7622     }
7623
7624   for (i = 0; i < arange_table_in_use; i++)
7625     {
7626       dw_die_ref die = arange_table[i];
7627
7628       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7629       gcc_assert (die->die_mark);
7630
7631       if (die->die_tag == DW_TAG_subprogram)
7632         {
7633           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7634                                "Address");
7635           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7636                                 get_AT_low_pc (die), "Length");
7637         }
7638       else
7639         {
7640           /* A static variable; extract the symbol from DW_AT_location.
7641              Note that this code isn't currently hit, as we only emit
7642              aranges for functions (jason 9/23/99).  */
7643           dw_attr_ref a = get_AT (die, DW_AT_location);
7644           dw_loc_descr_ref loc;
7645
7646           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7647
7648           loc = AT_loc (a);
7649           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7650
7651           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7652                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7653           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7654                                get_AT_unsigned (die, DW_AT_byte_size),
7655                                "Length");
7656         }
7657     }
7658
7659   /* Output the terminator words.  */
7660   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7661   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7662 }
7663
7664 /* Add a new entry to .debug_ranges.  Return the offset at which it
7665    was placed.  */
7666
7667 static unsigned int
7668 add_ranges_num (int num)
7669 {
7670   unsigned int in_use = ranges_table_in_use;
7671
7672   if (in_use == ranges_table_allocated)
7673     {
7674       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7675       ranges_table
7676         = ggc_realloc (ranges_table, (ranges_table_allocated
7677                                       * sizeof (struct dw_ranges_struct)));
7678       memset (ranges_table + ranges_table_in_use, 0,
7679               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7680     }
7681
7682   ranges_table[in_use].num = num;
7683   ranges_table_in_use = in_use + 1;
7684
7685   return in_use * 2 * DWARF2_ADDR_SIZE;
7686 }
7687
7688 /* Add a new entry to .debug_ranges corresponding to a block, or a
7689    range terminator if BLOCK is NULL.  */
7690
7691 static unsigned int
7692 add_ranges (const_tree block)
7693 {
7694   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7695 }
7696
7697 /* Add a new entry to .debug_ranges corresponding to a pair of
7698    labels.  */
7699
7700 static unsigned int
7701 add_ranges_by_labels (const char *begin, const char *end)
7702 {
7703   unsigned int in_use = ranges_by_label_in_use;
7704
7705   if (in_use == ranges_by_label_allocated)
7706     {
7707       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7708       ranges_by_label
7709         = ggc_realloc (ranges_by_label,
7710                        (ranges_by_label_allocated
7711                         * sizeof (struct dw_ranges_by_label_struct)));
7712       memset (ranges_by_label + ranges_by_label_in_use, 0,
7713               RANGES_TABLE_INCREMENT
7714               * sizeof (struct dw_ranges_by_label_struct));
7715     }
7716
7717   ranges_by_label[in_use].begin = begin;
7718   ranges_by_label[in_use].end = end;
7719   ranges_by_label_in_use = in_use + 1;
7720
7721   return add_ranges_num (-(int)in_use - 1);
7722 }
7723
7724 static void
7725 output_ranges (void)
7726 {
7727   unsigned i;
7728   static const char *const start_fmt = "Offset 0x%x";
7729   const char *fmt = start_fmt;
7730
7731   for (i = 0; i < ranges_table_in_use; i++)
7732     {
7733       int block_num = ranges_table[i].num;
7734
7735       if (block_num > 0)
7736         {
7737           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7738           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7739
7740           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7741           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7742
7743           /* If all code is in the text section, then the compilation
7744              unit base address defaults to DW_AT_low_pc, which is the
7745              base of the text section.  */
7746           if (!have_multiple_function_sections)
7747             {
7748               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7749                                     text_section_label,
7750                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7751               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7752                                     text_section_label, NULL);
7753             }
7754
7755           /* Otherwise, the compilation unit base address is zero,
7756              which allows us to use absolute addresses, and not worry
7757              about whether the target supports cross-section
7758              arithmetic.  */
7759           else
7760             {
7761               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7762                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7763               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7764             }
7765
7766           fmt = NULL;
7767         }
7768
7769       /* Negative block_num stands for an index into ranges_by_label.  */
7770       else if (block_num < 0)
7771         {
7772           int lab_idx = - block_num - 1;
7773
7774           if (!have_multiple_function_sections)
7775             {
7776               gcc_unreachable ();
7777 #if 0
7778               /* If we ever use add_ranges_by_labels () for a single
7779                  function section, all we have to do is to take out
7780                  the #if 0 above.  */
7781               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7782                                     ranges_by_label[lab_idx].begin,
7783                                     text_section_label,
7784                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7785               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7786                                     ranges_by_label[lab_idx].end,
7787                                     text_section_label, NULL);
7788 #endif
7789             }
7790           else
7791             {
7792               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7793                                    ranges_by_label[lab_idx].begin,
7794                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7795               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7796                                    ranges_by_label[lab_idx].end,
7797                                    NULL);
7798             }
7799         }
7800       else
7801         {
7802           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7803           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7804           fmt = start_fmt;
7805         }
7806     }
7807 }
7808
7809 /* Data structure containing information about input files.  */
7810 struct file_info
7811 {
7812   const char *path;     /* Complete file name.  */
7813   const char *fname;    /* File name part.  */
7814   int length;           /* Length of entire string.  */
7815   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7816   int dir_idx;          /* Index in directory table.  */
7817 };
7818
7819 /* Data structure containing information about directories with source
7820    files.  */
7821 struct dir_info
7822 {
7823   const char *path;     /* Path including directory name.  */
7824   int length;           /* Path length.  */
7825   int prefix;           /* Index of directory entry which is a prefix.  */
7826   int count;            /* Number of files in this directory.  */
7827   int dir_idx;          /* Index of directory used as base.  */
7828 };
7829
7830 /* Callback function for file_info comparison.  We sort by looking at
7831    the directories in the path.  */
7832
7833 static int
7834 file_info_cmp (const void *p1, const void *p2)
7835 {
7836   const struct file_info *s1 = p1;
7837   const struct file_info *s2 = p2;
7838   const unsigned char *cp1;
7839   const unsigned char *cp2;
7840
7841   /* Take care of file names without directories.  We need to make sure that
7842      we return consistent values to qsort since some will get confused if
7843      we return the same value when identical operands are passed in opposite
7844      orders.  So if neither has a directory, return 0 and otherwise return
7845      1 or -1 depending on which one has the directory.  */
7846   if ((s1->path == s1->fname || s2->path == s2->fname))
7847     return (s2->path == s2->fname) - (s1->path == s1->fname);
7848
7849   cp1 = (const unsigned char *) s1->path;
7850   cp2 = (const unsigned char *) s2->path;
7851
7852   while (1)
7853     {
7854       ++cp1;
7855       ++cp2;
7856       /* Reached the end of the first path?  If so, handle like above.  */
7857       if ((cp1 == (const unsigned char *) s1->fname)
7858           || (cp2 == (const unsigned char *) s2->fname))
7859         return ((cp2 == (const unsigned char *) s2->fname)
7860                 - (cp1 == (const unsigned char *) s1->fname));
7861
7862       /* Character of current path component the same?  */
7863       else if (*cp1 != *cp2)
7864         return *cp1 - *cp2;
7865     }
7866 }
7867
7868 struct file_name_acquire_data
7869 {
7870   struct file_info *files;
7871   int used_files;
7872   int max_files;
7873 };
7874
7875 /* Traversal function for the hash table.  */
7876
7877 static int
7878 file_name_acquire (void ** slot, void *data)
7879 {
7880   struct file_name_acquire_data *fnad = data;
7881   struct dwarf_file_data *d = *slot;
7882   struct file_info *fi;
7883   const char *f;
7884
7885   gcc_assert (fnad->max_files >= d->emitted_number);
7886
7887   if (! d->emitted_number)
7888     return 1;
7889
7890   gcc_assert (fnad->max_files != fnad->used_files);
7891
7892   fi = fnad->files + fnad->used_files++;
7893
7894   /* Skip all leading "./".  */
7895   f = d->filename;
7896   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7897     f += 2;
7898
7899   /* Create a new array entry.  */
7900   fi->path = f;
7901   fi->length = strlen (f);
7902   fi->file_idx = d;
7903
7904   /* Search for the file name part.  */
7905   f = strrchr (f, DIR_SEPARATOR);
7906 #if defined (DIR_SEPARATOR_2)
7907   {
7908     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7909
7910     if (g != NULL)
7911       {
7912         if (f == NULL || f < g)
7913           f = g;
7914       }
7915   }
7916 #endif
7917
7918   fi->fname = f == NULL ? fi->path : f + 1;
7919   return 1;
7920 }
7921
7922 /* Output the directory table and the file name table.  We try to minimize
7923    the total amount of memory needed.  A heuristic is used to avoid large
7924    slowdowns with many input files.  */
7925
7926 static void
7927 output_file_names (void)
7928 {
7929   struct file_name_acquire_data fnad;
7930   int numfiles;
7931   struct file_info *files;
7932   struct dir_info *dirs;
7933   int *saved;
7934   int *savehere;
7935   int *backmap;
7936   int ndirs;
7937   int idx_offset;
7938   int i;
7939   int idx;
7940
7941   if (!last_emitted_file)
7942     {
7943       dw2_asm_output_data (1, 0, "End directory table");
7944       dw2_asm_output_data (1, 0, "End file name table");
7945       return;
7946     }
7947
7948   numfiles = last_emitted_file->emitted_number;
7949
7950   /* Allocate the various arrays we need.  */
7951   files = alloca (numfiles * sizeof (struct file_info));
7952   dirs = alloca (numfiles * sizeof (struct dir_info));
7953
7954   fnad.files = files;
7955   fnad.used_files = 0;
7956   fnad.max_files = numfiles;
7957   htab_traverse (file_table, file_name_acquire, &fnad);
7958   gcc_assert (fnad.used_files == fnad.max_files);
7959
7960   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7961
7962   /* Find all the different directories used.  */
7963   dirs[0].path = files[0].path;
7964   dirs[0].length = files[0].fname - files[0].path;
7965   dirs[0].prefix = -1;
7966   dirs[0].count = 1;
7967   dirs[0].dir_idx = 0;
7968   files[0].dir_idx = 0;
7969   ndirs = 1;
7970
7971   for (i = 1; i < numfiles; i++)
7972     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7973         && memcmp (dirs[ndirs - 1].path, files[i].path,
7974                    dirs[ndirs - 1].length) == 0)
7975       {
7976         /* Same directory as last entry.  */
7977         files[i].dir_idx = ndirs - 1;
7978         ++dirs[ndirs - 1].count;
7979       }
7980     else
7981       {
7982         int j;
7983
7984         /* This is a new directory.  */
7985         dirs[ndirs].path = files[i].path;
7986         dirs[ndirs].length = files[i].fname - files[i].path;
7987         dirs[ndirs].count = 1;
7988         dirs[ndirs].dir_idx = ndirs;
7989         files[i].dir_idx = ndirs;
7990
7991         /* Search for a prefix.  */
7992         dirs[ndirs].prefix = -1;
7993         for (j = 0; j < ndirs; j++)
7994           if (dirs[j].length < dirs[ndirs].length
7995               && dirs[j].length > 1
7996               && (dirs[ndirs].prefix == -1
7997                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7998               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7999             dirs[ndirs].prefix = j;
8000
8001         ++ndirs;
8002       }
8003
8004   /* Now to the actual work.  We have to find a subset of the directories which
8005      allow expressing the file name using references to the directory table
8006      with the least amount of characters.  We do not do an exhaustive search
8007      where we would have to check out every combination of every single
8008      possible prefix.  Instead we use a heuristic which provides nearly optimal
8009      results in most cases and never is much off.  */
8010   saved = alloca (ndirs * sizeof (int));
8011   savehere = alloca (ndirs * sizeof (int));
8012
8013   memset (saved, '\0', ndirs * sizeof (saved[0]));
8014   for (i = 0; i < ndirs; i++)
8015     {
8016       int j;
8017       int total;
8018
8019       /* We can always save some space for the current directory.  But this
8020          does not mean it will be enough to justify adding the directory.  */
8021       savehere[i] = dirs[i].length;
8022       total = (savehere[i] - saved[i]) * dirs[i].count;
8023
8024       for (j = i + 1; j < ndirs; j++)
8025         {
8026           savehere[j] = 0;
8027           if (saved[j] < dirs[i].length)
8028             {
8029               /* Determine whether the dirs[i] path is a prefix of the
8030                  dirs[j] path.  */
8031               int k;
8032
8033               k = dirs[j].prefix;
8034               while (k != -1 && k != (int) i)
8035                 k = dirs[k].prefix;
8036
8037               if (k == (int) i)
8038                 {
8039                   /* Yes it is.  We can possibly save some memory by
8040                      writing the filenames in dirs[j] relative to
8041                      dirs[i].  */
8042                   savehere[j] = dirs[i].length;
8043                   total += (savehere[j] - saved[j]) * dirs[j].count;
8044                 }
8045             }
8046         }
8047
8048       /* Check whether we can save enough to justify adding the dirs[i]
8049          directory.  */
8050       if (total > dirs[i].length + 1)
8051         {
8052           /* It's worthwhile adding.  */
8053           for (j = i; j < ndirs; j++)
8054             if (savehere[j] > 0)
8055               {
8056                 /* Remember how much we saved for this directory so far.  */
8057                 saved[j] = savehere[j];
8058
8059                 /* Remember the prefix directory.  */
8060                 dirs[j].dir_idx = i;
8061               }
8062         }
8063     }
8064
8065   /* Emit the directory name table.  */
8066   idx = 1;
8067   idx_offset = dirs[0].length > 0 ? 1 : 0;
8068   for (i = 1 - idx_offset; i < ndirs; i++)
8069     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8070                             "Directory Entry: 0x%x", i + idx_offset);
8071
8072   dw2_asm_output_data (1, 0, "End directory table");
8073
8074   /* We have to emit them in the order of emitted_number since that's
8075      used in the debug info generation.  To do this efficiently we
8076      generate a back-mapping of the indices first.  */
8077   backmap = alloca (numfiles * sizeof (int));
8078   for (i = 0; i < numfiles; i++)
8079     backmap[files[i].file_idx->emitted_number - 1] = i;
8080
8081   /* Now write all the file names.  */
8082   for (i = 0; i < numfiles; i++)
8083     {
8084       int file_idx = backmap[i];
8085       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8086
8087       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8088                               "File Entry: 0x%x", (unsigned) i + 1);
8089
8090       /* Include directory index.  */
8091       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8092
8093       /* Modification time.  */
8094       dw2_asm_output_data_uleb128 (0, NULL);
8095
8096       /* File length in bytes.  */
8097       dw2_asm_output_data_uleb128 (0, NULL);
8098     }
8099
8100   dw2_asm_output_data (1, 0, "End file name table");
8101 }
8102
8103
8104 /* Output the source line number correspondence information.  This
8105    information goes into the .debug_line section.  */
8106
8107 static void
8108 output_line_info (void)
8109 {
8110   char l1[20], l2[20], p1[20], p2[20];
8111   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8112   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8113   unsigned opc;
8114   unsigned n_op_args;
8115   unsigned long lt_index;
8116   unsigned long current_line;
8117   long line_offset;
8118   long line_delta;
8119   unsigned long current_file;
8120   unsigned long function;
8121
8122   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8123   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8124   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8125   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8126
8127   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8128     dw2_asm_output_data (4, 0xffffffff,
8129       "Initial length escape value indicating 64-bit DWARF extension");
8130   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8131                         "Length of Source Line Info");
8132   ASM_OUTPUT_LABEL (asm_out_file, l1);
8133
8134   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8135   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8136   ASM_OUTPUT_LABEL (asm_out_file, p1);
8137
8138   /* Define the architecture-dependent minimum instruction length (in
8139    bytes).  In this implementation of DWARF, this field is used for
8140    information purposes only.  Since GCC generates assembly language,
8141    we have no a priori knowledge of how many instruction bytes are
8142    generated for each source line, and therefore can use only the
8143    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8144    commands.  Accordingly, we fix this as `1', which is "correct
8145    enough" for all architectures, and don't let the target override.  */
8146   dw2_asm_output_data (1, 1,
8147                        "Minimum Instruction Length");
8148
8149   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8150                        "Default is_stmt_start flag");
8151   dw2_asm_output_data (1, DWARF_LINE_BASE,
8152                        "Line Base Value (Special Opcodes)");
8153   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8154                        "Line Range Value (Special Opcodes)");
8155   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8156                        "Special Opcode Base");
8157
8158   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8159     {
8160       switch (opc)
8161         {
8162         case DW_LNS_advance_pc:
8163         case DW_LNS_advance_line:
8164         case DW_LNS_set_file:
8165         case DW_LNS_set_column:
8166         case DW_LNS_fixed_advance_pc:
8167           n_op_args = 1;
8168           break;
8169         default:
8170           n_op_args = 0;
8171           break;
8172         }
8173
8174       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8175                            opc, n_op_args);
8176     }
8177
8178   /* Write out the information about the files we use.  */
8179   output_file_names ();
8180   ASM_OUTPUT_LABEL (asm_out_file, p2);
8181
8182   /* We used to set the address register to the first location in the text
8183      section here, but that didn't accomplish anything since we already
8184      have a line note for the opening brace of the first function.  */
8185
8186   /* Generate the line number to PC correspondence table, encoded as
8187      a series of state machine operations.  */
8188   current_file = 1;
8189   current_line = 1;
8190
8191   if (cfun && in_cold_section_p)
8192     strcpy (prev_line_label, cfun->cold_section_label);
8193   else
8194     strcpy (prev_line_label, text_section_label);
8195   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8196     {
8197       dw_line_info_ref line_info = &line_info_table[lt_index];
8198
8199 #if 0
8200       /* Disable this optimization for now; GDB wants to see two line notes
8201          at the beginning of a function so it can find the end of the
8202          prologue.  */
8203
8204       /* Don't emit anything for redundant notes.  Just updating the
8205          address doesn't accomplish anything, because we already assume
8206          that anything after the last address is this line.  */
8207       if (line_info->dw_line_num == current_line
8208           && line_info->dw_file_num == current_file)
8209         continue;
8210 #endif
8211
8212       /* Emit debug info for the address of the current line.
8213
8214          Unfortunately, we have little choice here currently, and must always
8215          use the most general form.  GCC does not know the address delta
8216          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8217          attributes which will give an upper bound on the address range.  We
8218          could perhaps use length attributes to determine when it is safe to
8219          use DW_LNS_fixed_advance_pc.  */
8220
8221       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8222       if (0)
8223         {
8224           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8225           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8226                                "DW_LNS_fixed_advance_pc");
8227           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8228         }
8229       else
8230         {
8231           /* This can handle any delta.  This takes
8232              4+DWARF2_ADDR_SIZE bytes.  */
8233           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8234           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8235           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8236           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8237         }
8238
8239       strcpy (prev_line_label, line_label);
8240
8241       /* Emit debug info for the source file of the current line, if
8242          different from the previous line.  */
8243       if (line_info->dw_file_num != current_file)
8244         {
8245           current_file = line_info->dw_file_num;
8246           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8247           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8248         }
8249
8250       /* Emit debug info for the current line number, choosing the encoding
8251          that uses the least amount of space.  */
8252       if (line_info->dw_line_num != current_line)
8253         {
8254           line_offset = line_info->dw_line_num - current_line;
8255           line_delta = line_offset - DWARF_LINE_BASE;
8256           current_line = line_info->dw_line_num;
8257           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8258             /* This can handle deltas from -10 to 234, using the current
8259                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8260                takes 1 byte.  */
8261             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8262                                  "line %lu", current_line);
8263           else
8264             {
8265               /* This can handle any delta.  This takes at least 4 bytes,
8266                  depending on the value being encoded.  */
8267               dw2_asm_output_data (1, DW_LNS_advance_line,
8268                                    "advance to line %lu", current_line);
8269               dw2_asm_output_data_sleb128 (line_offset, NULL);
8270               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8271             }
8272         }
8273       else
8274         /* We still need to start a new row, so output a copy insn.  */
8275         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8276     }
8277
8278   /* Emit debug info for the address of the end of the function.  */
8279   if (0)
8280     {
8281       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8282                            "DW_LNS_fixed_advance_pc");
8283       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8284     }
8285   else
8286     {
8287       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8288       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8289       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8290       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8291     }
8292
8293   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8294   dw2_asm_output_data_uleb128 (1, NULL);
8295   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8296
8297   function = 0;
8298   current_file = 1;
8299   current_line = 1;
8300   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8301     {
8302       dw_separate_line_info_ref line_info
8303         = &separate_line_info_table[lt_index];
8304
8305 #if 0
8306       /* Don't emit anything for redundant notes.  */
8307       if (line_info->dw_line_num == current_line
8308           && line_info->dw_file_num == current_file
8309           && line_info->function == function)
8310         goto cont;
8311 #endif
8312
8313       /* Emit debug info for the address of the current line.  If this is
8314          a new function, or the first line of a function, then we need
8315          to handle it differently.  */
8316       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8317                                    lt_index);
8318       if (function != line_info->function)
8319         {
8320           function = line_info->function;
8321
8322           /* Set the address register to the first line in the function.  */
8323           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8324           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8325           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8326           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8327         }
8328       else
8329         {
8330           /* ??? See the DW_LNS_advance_pc comment above.  */
8331           if (0)
8332             {
8333               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8334                                    "DW_LNS_fixed_advance_pc");
8335               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8336             }
8337           else
8338             {
8339               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8340               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8341               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8342               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8343             }
8344         }
8345
8346       strcpy (prev_line_label, line_label);
8347
8348       /* Emit debug info for the source file of the current line, if
8349          different from the previous line.  */
8350       if (line_info->dw_file_num != current_file)
8351         {
8352           current_file = line_info->dw_file_num;
8353           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8354           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8355         }
8356
8357       /* Emit debug info for the current line number, choosing the encoding
8358          that uses the least amount of space.  */
8359       if (line_info->dw_line_num != current_line)
8360         {
8361           line_offset = line_info->dw_line_num - current_line;
8362           line_delta = line_offset - DWARF_LINE_BASE;
8363           current_line = line_info->dw_line_num;
8364           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8365             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8366                                  "line %lu", current_line);
8367           else
8368             {
8369               dw2_asm_output_data (1, DW_LNS_advance_line,
8370                                    "advance to line %lu", current_line);
8371               dw2_asm_output_data_sleb128 (line_offset, NULL);
8372               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8373             }
8374         }
8375       else
8376         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8377
8378 #if 0
8379     cont:
8380 #endif
8381
8382       lt_index++;
8383
8384       /* If we're done with a function, end its sequence.  */
8385       if (lt_index == separate_line_info_table_in_use
8386           || separate_line_info_table[lt_index].function != function)
8387         {
8388           current_file = 1;
8389           current_line = 1;
8390
8391           /* Emit debug info for the address of the end of the function.  */
8392           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8393           if (0)
8394             {
8395               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8396                                    "DW_LNS_fixed_advance_pc");
8397               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8398             }
8399           else
8400             {
8401               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8402               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8403               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8404               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8405             }
8406
8407           /* Output the marker for the end of this sequence.  */
8408           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8409           dw2_asm_output_data_uleb128 (1, NULL);
8410           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8411         }
8412     }
8413
8414   /* Output the marker for the end of the line number info.  */
8415   ASM_OUTPUT_LABEL (asm_out_file, l2);
8416 }
8417 \f
8418 /* Given a pointer to a tree node for some base type, return a pointer to
8419    a DIE that describes the given type.
8420
8421    This routine must only be called for GCC type nodes that correspond to
8422    Dwarf base (fundamental) types.  */
8423
8424 static dw_die_ref
8425 base_type_die (tree type)
8426 {
8427   dw_die_ref base_type_result;
8428   enum dwarf_type encoding;
8429
8430   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8431     return 0;
8432
8433   switch (TREE_CODE (type))
8434     {
8435     case INTEGER_TYPE:
8436       if (TYPE_STRING_FLAG (type))
8437         {
8438           if (TYPE_UNSIGNED (type))
8439             encoding = DW_ATE_unsigned_char;
8440           else
8441             encoding = DW_ATE_signed_char;
8442         }
8443       else if (TYPE_UNSIGNED (type))
8444         encoding = DW_ATE_unsigned;
8445       else
8446         encoding = DW_ATE_signed;
8447       break;
8448
8449     case REAL_TYPE:
8450       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8451         encoding = DW_ATE_decimal_float;
8452       else
8453         encoding = DW_ATE_float;
8454       break;
8455
8456     case FIXED_POINT_TYPE:
8457       if (TYPE_UNSIGNED (type))
8458         encoding = DW_ATE_unsigned_fixed;
8459       else
8460         encoding = DW_ATE_signed_fixed;
8461       break;
8462
8463       /* Dwarf2 doesn't know anything about complex ints, so use
8464          a user defined type for it.  */
8465     case COMPLEX_TYPE:
8466       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8467         encoding = DW_ATE_complex_float;
8468       else
8469         encoding = DW_ATE_lo_user;
8470       break;
8471
8472     case BOOLEAN_TYPE:
8473       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8474       encoding = DW_ATE_boolean;
8475       break;
8476
8477     default:
8478       /* No other TREE_CODEs are Dwarf fundamental types.  */
8479       gcc_unreachable ();
8480     }
8481
8482   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8483
8484   /* This probably indicates a bug.  */
8485   if (! TYPE_NAME (type))
8486     add_name_attribute (base_type_result, "__unknown__");
8487
8488   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8489                    int_size_in_bytes (type));
8490   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8491
8492   return base_type_result;
8493 }
8494
8495 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8496    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8497
8498 static inline int
8499 is_base_type (tree type)
8500 {
8501   switch (TREE_CODE (type))
8502     {
8503     case ERROR_MARK:
8504     case VOID_TYPE:
8505     case INTEGER_TYPE:
8506     case REAL_TYPE:
8507     case FIXED_POINT_TYPE:
8508     case COMPLEX_TYPE:
8509     case BOOLEAN_TYPE:
8510       return 1;
8511
8512     case ARRAY_TYPE:
8513     case RECORD_TYPE:
8514     case UNION_TYPE:
8515     case QUAL_UNION_TYPE:
8516     case ENUMERAL_TYPE:
8517     case FUNCTION_TYPE:
8518     case METHOD_TYPE:
8519     case POINTER_TYPE:
8520     case REFERENCE_TYPE:
8521     case OFFSET_TYPE:
8522     case LANG_TYPE:
8523     case VECTOR_TYPE:
8524       return 0;
8525
8526     default:
8527       gcc_unreachable ();
8528     }
8529
8530   return 0;
8531 }
8532
8533 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8534    node, return the size in bits for the type if it is a constant, or else
8535    return the alignment for the type if the type's size is not constant, or
8536    else return BITS_PER_WORD if the type actually turns out to be an
8537    ERROR_MARK node.  */
8538
8539 static inline unsigned HOST_WIDE_INT
8540 simple_type_size_in_bits (const_tree type)
8541 {
8542   if (TREE_CODE (type) == ERROR_MARK)
8543     return BITS_PER_WORD;
8544   else if (TYPE_SIZE (type) == NULL_TREE)
8545     return 0;
8546   else if (host_integerp (TYPE_SIZE (type), 1))
8547     return tree_low_cst (TYPE_SIZE (type), 1);
8548   else
8549     return TYPE_ALIGN (type);
8550 }
8551
8552 /* Return true if the debug information for the given type should be
8553    emitted as a subrange type.  */
8554
8555 static inline bool
8556 is_subrange_type (const_tree type)
8557 {
8558   tree subtype = TREE_TYPE (type);
8559
8560   /* Subrange types are identified by the fact that they are integer
8561      types, and that they have a subtype which is either an integer type
8562      or an enumeral type.  */
8563
8564   if (TREE_CODE (type) != INTEGER_TYPE
8565       || subtype == NULL_TREE)
8566     return false;
8567
8568   if (TREE_CODE (subtype) != INTEGER_TYPE
8569       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8570     return false;
8571
8572   if (TREE_CODE (type) == TREE_CODE (subtype)
8573       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8574       && TYPE_MIN_VALUE (type) != NULL
8575       && TYPE_MIN_VALUE (subtype) != NULL
8576       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8577       && TYPE_MAX_VALUE (type) != NULL
8578       && TYPE_MAX_VALUE (subtype) != NULL
8579       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8580     {
8581       /* The type and its subtype have the same representation.  If in
8582          addition the two types also have the same name, then the given
8583          type is not a subrange type, but rather a plain base type.  */
8584       /* FIXME: brobecker/2004-03-22:
8585          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8586          therefore be sufficient to check the TYPE_SIZE node pointers
8587          rather than checking the actual size.  Unfortunately, we have
8588          found some cases, such as in the Ada "integer" type, where
8589          this is not the case.  Until this problem is solved, we need to
8590          keep checking the actual size.  */
8591       tree type_name = TYPE_NAME (type);
8592       tree subtype_name = TYPE_NAME (subtype);
8593
8594       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8595         type_name = DECL_NAME (type_name);
8596
8597       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8598         subtype_name = DECL_NAME (subtype_name);
8599
8600       if (type_name == subtype_name)
8601         return false;
8602     }
8603
8604   return true;
8605 }
8606
8607 /*  Given a pointer to a tree node for a subrange type, return a pointer
8608     to a DIE that describes the given type.  */
8609
8610 static dw_die_ref
8611 subrange_type_die (tree type, dw_die_ref context_die)
8612 {
8613   dw_die_ref subrange_die;
8614   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8615
8616   if (context_die == NULL)
8617     context_die = comp_unit_die;
8618
8619   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8620
8621   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8622     {
8623       /* The size of the subrange type and its base type do not match,
8624          so we need to generate a size attribute for the subrange type.  */
8625       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8626     }
8627
8628   if (TYPE_MIN_VALUE (type) != NULL)
8629     add_bound_info (subrange_die, DW_AT_lower_bound,
8630                     TYPE_MIN_VALUE (type));
8631   if (TYPE_MAX_VALUE (type) != NULL)
8632     add_bound_info (subrange_die, DW_AT_upper_bound,
8633                     TYPE_MAX_VALUE (type));
8634
8635   return subrange_die;
8636 }
8637
8638 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8639    entry that chains various modifiers in front of the given type.  */
8640
8641 static dw_die_ref
8642 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8643                    dw_die_ref context_die)
8644 {
8645   enum tree_code code = TREE_CODE (type);
8646   dw_die_ref mod_type_die;
8647   dw_die_ref sub_die = NULL;
8648   tree item_type = NULL;
8649   tree qualified_type;
8650   tree name;
8651
8652   if (code == ERROR_MARK)
8653     return NULL;
8654
8655   /* See if we already have the appropriately qualified variant of
8656      this type.  */
8657   qualified_type
8658     = get_qualified_type (type,
8659                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8660                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8661
8662   /* If we do, then we can just use its DIE, if it exists.  */
8663   if (qualified_type)
8664     {
8665       mod_type_die = lookup_type_die (qualified_type);
8666       if (mod_type_die)
8667         return mod_type_die;
8668     }
8669
8670   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8671
8672   /* Handle C typedef types.  */
8673   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8674     {
8675       tree dtype = TREE_TYPE (name);
8676
8677       if (qualified_type == dtype)
8678         {
8679           /* For a named type, use the typedef.  */
8680           gen_type_die (qualified_type, context_die);
8681           return lookup_type_die (qualified_type);
8682         }
8683       else if (is_const_type < TYPE_READONLY (dtype)
8684                || is_volatile_type < TYPE_VOLATILE (dtype)
8685                || (is_const_type <= TYPE_READONLY (dtype)
8686                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8687                    && DECL_ORIGINAL_TYPE (name) != type))
8688         /* cv-unqualified version of named type.  Just use the unnamed
8689            type to which it refers.  */
8690         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8691                                   is_const_type, is_volatile_type,
8692                                   context_die);
8693       /* Else cv-qualified version of named type; fall through.  */
8694     }
8695
8696   if (is_const_type)
8697     {
8698       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8699       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8700     }
8701   else if (is_volatile_type)
8702     {
8703       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8704       sub_die = modified_type_die (type, 0, 0, context_die);
8705     }
8706   else if (code == POINTER_TYPE)
8707     {
8708       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8709       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8710                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8711       item_type = TREE_TYPE (type);
8712     }
8713   else if (code == REFERENCE_TYPE)
8714     {
8715       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8716       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8717                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8718       item_type = TREE_TYPE (type);
8719     }
8720   else if (is_subrange_type (type))
8721     {
8722       mod_type_die = subrange_type_die (type, context_die);
8723       item_type = TREE_TYPE (type);
8724     }
8725   else if (is_base_type (type))
8726     mod_type_die = base_type_die (type);
8727   else
8728     {
8729       gen_type_die (type, context_die);
8730
8731       /* We have to get the type_main_variant here (and pass that to the
8732          `lookup_type_die' routine) because the ..._TYPE node we have
8733          might simply be a *copy* of some original type node (where the
8734          copy was created to help us keep track of typedef names) and
8735          that copy might have a different TYPE_UID from the original
8736          ..._TYPE node.  */
8737       if (TREE_CODE (type) != VECTOR_TYPE)
8738         return lookup_type_die (type_main_variant (type));
8739       else
8740         /* Vectors have the debugging information in the type,
8741            not the main variant.  */
8742         return lookup_type_die (type);
8743     }
8744
8745   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8746      don't output a DW_TAG_typedef, since there isn't one in the
8747      user's program; just attach a DW_AT_name to the type.  */
8748   if (name
8749       && (TREE_CODE (name) != TYPE_DECL
8750           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8751     {
8752       if (TREE_CODE (name) == TYPE_DECL)
8753         /* Could just call add_name_and_src_coords_attributes here,
8754            but since this is a builtin type it doesn't have any
8755            useful source coordinates anyway.  */
8756         name = DECL_NAME (name);
8757       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8758     }
8759
8760   if (qualified_type)
8761     equate_type_number_to_die (qualified_type, mod_type_die);
8762
8763   if (item_type)
8764     /* We must do this after the equate_type_number_to_die call, in case
8765        this is a recursive type.  This ensures that the modified_type_die
8766        recursion will terminate even if the type is recursive.  Recursive
8767        types are possible in Ada.  */
8768     sub_die = modified_type_die (item_type,
8769                                  TYPE_READONLY (item_type),
8770                                  TYPE_VOLATILE (item_type),
8771                                  context_die);
8772
8773   if (sub_die != NULL)
8774     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8775
8776   return mod_type_die;
8777 }
8778
8779 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8780    an enumerated type.  */
8781
8782 static inline int
8783 type_is_enum (const_tree type)
8784 {
8785   return TREE_CODE (type) == ENUMERAL_TYPE;
8786 }
8787
8788 /* Return the DBX register number described by a given RTL node.  */
8789
8790 static unsigned int
8791 dbx_reg_number (const_rtx rtl)
8792 {
8793   unsigned regno = REGNO (rtl);
8794
8795   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8796
8797 #ifdef LEAF_REG_REMAP
8798   if (current_function_uses_only_leaf_regs)
8799     {
8800       int leaf_reg = LEAF_REG_REMAP (regno);
8801       if (leaf_reg != -1)
8802         regno = (unsigned) leaf_reg;
8803     }
8804 #endif
8805
8806   return DBX_REGISTER_NUMBER (regno);
8807 }
8808
8809 /* Optionally add a DW_OP_piece term to a location description expression.
8810    DW_OP_piece is only added if the location description expression already
8811    doesn't end with DW_OP_piece.  */
8812
8813 static void
8814 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8815 {
8816   dw_loc_descr_ref loc;
8817
8818   if (*list_head != NULL)
8819     {
8820       /* Find the end of the chain.  */
8821       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8822         ;
8823
8824       if (loc->dw_loc_opc != DW_OP_piece)
8825         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8826     }
8827 }
8828
8829 /* Return a location descriptor that designates a machine register or
8830    zero if there is none.  */
8831
8832 static dw_loc_descr_ref
8833 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8834 {
8835   rtx regs;
8836
8837   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8838     return 0;
8839
8840   regs = targetm.dwarf_register_span (rtl);
8841
8842   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8843     return multiple_reg_loc_descriptor (rtl, regs, initialized);
8844   else
8845     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8846 }
8847
8848 /* Return a location descriptor that designates a machine register for
8849    a given hard register number.  */
8850
8851 static dw_loc_descr_ref
8852 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8853 {
8854   dw_loc_descr_ref reg_loc_descr;
8855   if (regno <= 31)
8856     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8857   else
8858     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8859
8860   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8861     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8862
8863   return reg_loc_descr;
8864 }
8865
8866 /* Given an RTL of a register, return a location descriptor that
8867    designates a value that spans more than one register.  */
8868
8869 static dw_loc_descr_ref
8870 multiple_reg_loc_descriptor (rtx rtl, rtx regs, 
8871                              enum var_init_status initialized)
8872 {
8873   int nregs, size, i;
8874   unsigned reg;
8875   dw_loc_descr_ref loc_result = NULL;
8876
8877   reg = REGNO (rtl);
8878 #ifdef LEAF_REG_REMAP
8879   if (current_function_uses_only_leaf_regs)
8880     {
8881       int leaf_reg = LEAF_REG_REMAP (reg);
8882       if (leaf_reg != -1)
8883         reg = (unsigned) leaf_reg;
8884     }
8885 #endif
8886   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8887   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8888
8889   /* Simple, contiguous registers.  */
8890   if (regs == NULL_RTX)
8891     {
8892       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8893
8894       loc_result = NULL;
8895       while (nregs--)
8896         {
8897           dw_loc_descr_ref t;
8898
8899           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8900                                       VAR_INIT_STATUS_INITIALIZED);
8901           add_loc_descr (&loc_result, t);
8902           add_loc_descr_op_piece (&loc_result, size);
8903           ++reg;
8904         }
8905       return loc_result;
8906     }
8907
8908   /* Now onto stupid register sets in non contiguous locations.  */
8909
8910   gcc_assert (GET_CODE (regs) == PARALLEL);
8911
8912   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8913   loc_result = NULL;
8914
8915   for (i = 0; i < XVECLEN (regs, 0); ++i)
8916     {
8917       dw_loc_descr_ref t;
8918
8919       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8920                                   VAR_INIT_STATUS_INITIALIZED);
8921       add_loc_descr (&loc_result, t);
8922       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8923       add_loc_descr_op_piece (&loc_result, size);
8924     }
8925
8926   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8927     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8928   return loc_result;
8929 }
8930
8931 /* Return a location descriptor that designates a constant.  */
8932
8933 static dw_loc_descr_ref
8934 int_loc_descriptor (HOST_WIDE_INT i)
8935 {
8936   enum dwarf_location_atom op;
8937
8938   /* Pick the smallest representation of a constant, rather than just
8939      defaulting to the LEB encoding.  */
8940   if (i >= 0)
8941     {
8942       if (i <= 31)
8943         op = DW_OP_lit0 + i;
8944       else if (i <= 0xff)
8945         op = DW_OP_const1u;
8946       else if (i <= 0xffff)
8947         op = DW_OP_const2u;
8948       else if (HOST_BITS_PER_WIDE_INT == 32
8949                || i <= 0xffffffff)
8950         op = DW_OP_const4u;
8951       else
8952         op = DW_OP_constu;
8953     }
8954   else
8955     {
8956       if (i >= -0x80)
8957         op = DW_OP_const1s;
8958       else if (i >= -0x8000)
8959         op = DW_OP_const2s;
8960       else if (HOST_BITS_PER_WIDE_INT == 32
8961                || i >= -0x80000000)
8962         op = DW_OP_const4s;
8963       else
8964         op = DW_OP_consts;
8965     }
8966
8967   return new_loc_descr (op, i, 0);
8968 }
8969
8970 /* Return a location descriptor that designates a base+offset location.  */
8971
8972 static dw_loc_descr_ref
8973 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
8974                  enum var_init_status initialized)
8975 {
8976   unsigned int regno;
8977   dw_loc_descr_ref result;
8978
8979   /* We only use "frame base" when we're sure we're talking about the
8980      post-prologue local stack frame.  We do this by *not* running
8981      register elimination until this point, and recognizing the special
8982      argument pointer and soft frame pointer rtx's.  */
8983   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8984     {
8985       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8986
8987       if (elim != reg)
8988         {
8989           if (GET_CODE (elim) == PLUS)
8990             {
8991               offset += INTVAL (XEXP (elim, 1));
8992               elim = XEXP (elim, 0);
8993             }
8994           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8995                       : stack_pointer_rtx));
8996           offset += frame_pointer_fb_offset;
8997
8998           return new_loc_descr (DW_OP_fbreg, offset, 0);
8999         }
9000     }
9001
9002   regno = dbx_reg_number (reg);
9003   if (regno <= 31)
9004     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9005   else
9006     result = new_loc_descr (DW_OP_bregx, regno, offset);
9007
9008   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9009     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9010
9011   return result;
9012 }
9013
9014 /* Return true if this RTL expression describes a base+offset calculation.  */
9015
9016 static inline int
9017 is_based_loc (const_rtx rtl)
9018 {
9019   return (GET_CODE (rtl) == PLUS
9020           && ((REG_P (XEXP (rtl, 0))
9021                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9022                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9023 }
9024
9025 /* Return a descriptor that describes the concatenation of N locations
9026    used to form the address of a memory location.  */
9027
9028 static dw_loc_descr_ref
9029 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9030                             enum var_init_status initialized)
9031 {
9032   unsigned int i;
9033   dw_loc_descr_ref cc_loc_result = NULL;
9034   unsigned int n = XVECLEN (concatn, 0);
9035
9036   for (i = 0; i < n; ++i)
9037     {
9038       dw_loc_descr_ref ref;
9039       rtx x = XVECEXP (concatn, 0, i);
9040
9041       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9042       if (ref == NULL)
9043         return NULL;
9044
9045       add_loc_descr (&cc_loc_result, ref);
9046       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9047     }
9048
9049   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9050     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9051
9052   return cc_loc_result;
9053 }
9054
9055 /* The following routine converts the RTL for a variable or parameter
9056    (resident in memory) into an equivalent Dwarf representation of a
9057    mechanism for getting the address of that same variable onto the top of a
9058    hypothetical "address evaluation" stack.
9059
9060    When creating memory location descriptors, we are effectively transforming
9061    the RTL for a memory-resident object into its Dwarf postfix expression
9062    equivalent.  This routine recursively descends an RTL tree, turning
9063    it into Dwarf postfix code as it goes.
9064
9065    MODE is the mode of the memory reference, needed to handle some
9066    autoincrement addressing modes.
9067
9068    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9069    location list for RTL.
9070
9071    Return 0 if we can't represent the location.  */
9072
9073 static dw_loc_descr_ref
9074 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9075                     enum var_init_status initialized)
9076 {
9077   dw_loc_descr_ref mem_loc_result = NULL;
9078   enum dwarf_location_atom op;
9079
9080   /* Note that for a dynamically sized array, the location we will generate a
9081      description of here will be the lowest numbered location which is
9082      actually within the array.  That's *not* necessarily the same as the
9083      zeroth element of the array.  */
9084
9085   rtl = targetm.delegitimize_address (rtl);
9086
9087   switch (GET_CODE (rtl))
9088     {
9089     case POST_INC:
9090     case POST_DEC:
9091     case POST_MODIFY:
9092       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9093          just fall into the SUBREG code.  */
9094
9095       /* ... fall through ...  */
9096
9097     case SUBREG:
9098       /* The case of a subreg may arise when we have a local (register)
9099          variable or a formal (register) parameter which doesn't quite fill
9100          up an entire register.  For now, just assume that it is
9101          legitimate to make the Dwarf info refer to the whole register which
9102          contains the given subreg.  */
9103       rtl = XEXP (rtl, 0);
9104
9105       /* ... fall through ...  */
9106
9107     case REG:
9108       /* Whenever a register number forms a part of the description of the
9109          method for calculating the (dynamic) address of a memory resident
9110          object, DWARF rules require the register number be referred to as
9111          a "base register".  This distinction is not based in any way upon
9112          what category of register the hardware believes the given register
9113          belongs to.  This is strictly DWARF terminology we're dealing with
9114          here. Note that in cases where the location of a memory-resident
9115          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9116          OP_CONST (0)) the actual DWARF location descriptor that we generate
9117          may just be OP_BASEREG (basereg).  This may look deceptively like
9118          the object in question was allocated to a register (rather than in
9119          memory) so DWARF consumers need to be aware of the subtle
9120          distinction between OP_REG and OP_BASEREG.  */
9121       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9122         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9123       break;
9124
9125     case MEM:
9126       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9127                                            VAR_INIT_STATUS_INITIALIZED);
9128       if (mem_loc_result != 0)
9129         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9130       break;
9131
9132     case LO_SUM:
9133          rtl = XEXP (rtl, 1);
9134
9135       /* ... fall through ...  */
9136
9137     case LABEL_REF:
9138       /* Some ports can transform a symbol ref into a label ref, because
9139          the symbol ref is too far away and has to be dumped into a constant
9140          pool.  */
9141     case CONST:
9142     case SYMBOL_REF:
9143       /* Alternatively, the symbol in the constant pool might be referenced
9144          by a different symbol.  */
9145       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9146         {
9147           bool marked;
9148           rtx tmp = get_pool_constant_mark (rtl, &marked);
9149
9150           if (GET_CODE (tmp) == SYMBOL_REF)
9151             {
9152               rtl = tmp;
9153               if (CONSTANT_POOL_ADDRESS_P (tmp))
9154                 get_pool_constant_mark (tmp, &marked);
9155               else
9156                 marked = true;
9157             }
9158
9159           /* If all references to this pool constant were optimized away,
9160              it was not output and thus we can't represent it.
9161              FIXME: might try to use DW_OP_const_value here, though
9162              DW_OP_piece complicates it.  */
9163           if (!marked)
9164             return 0;
9165         }
9166
9167       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9168       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9169       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9170       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9171       break;
9172
9173     case PRE_MODIFY:
9174       /* Extract the PLUS expression nested inside and fall into
9175          PLUS code below.  */
9176       rtl = XEXP (rtl, 1);
9177       goto plus;
9178
9179     case PRE_INC:
9180     case PRE_DEC:
9181       /* Turn these into a PLUS expression and fall into the PLUS code
9182          below.  */
9183       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9184                           GEN_INT (GET_CODE (rtl) == PRE_INC
9185                                    ? GET_MODE_UNIT_SIZE (mode)
9186                                    : -GET_MODE_UNIT_SIZE (mode)));
9187
9188       /* ... fall through ...  */
9189
9190     case PLUS:
9191     plus:
9192       if (is_based_loc (rtl))
9193         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9194                                           INTVAL (XEXP (rtl, 1)),
9195                                           VAR_INIT_STATUS_INITIALIZED);
9196       else
9197         {
9198           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9199                                                VAR_INIT_STATUS_INITIALIZED);
9200           if (mem_loc_result == 0)
9201             break;
9202
9203           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9204               && INTVAL (XEXP (rtl, 1)) >= 0)
9205             add_loc_descr (&mem_loc_result,
9206                            new_loc_descr (DW_OP_plus_uconst,
9207                                           INTVAL (XEXP (rtl, 1)), 0));
9208           else
9209             {
9210               add_loc_descr (&mem_loc_result,
9211                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9212                                                  VAR_INIT_STATUS_INITIALIZED));
9213               add_loc_descr (&mem_loc_result,
9214                              new_loc_descr (DW_OP_plus, 0, 0));
9215             }
9216         }
9217       break;
9218
9219     /* If a pseudo-reg is optimized away, it is possible for it to
9220        be replaced with a MEM containing a multiply or shift.  */
9221     case MULT:
9222       op = DW_OP_mul;
9223       goto do_binop;
9224
9225     case ASHIFT:
9226       op = DW_OP_shl;
9227       goto do_binop;
9228
9229     case ASHIFTRT:
9230       op = DW_OP_shra;
9231       goto do_binop;
9232
9233     case LSHIFTRT:
9234       op = DW_OP_shr;
9235       goto do_binop;
9236
9237     do_binop:
9238       {
9239         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9240                                                    VAR_INIT_STATUS_INITIALIZED);
9241         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9242                                                    VAR_INIT_STATUS_INITIALIZED);
9243
9244         if (op0 == 0 || op1 == 0)
9245           break;
9246
9247         mem_loc_result = op0;
9248         add_loc_descr (&mem_loc_result, op1);
9249         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9250         break;
9251       }
9252
9253     case CONST_INT:
9254       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9255       break;
9256
9257     case CONCATN:
9258       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode, 
9259                                                    VAR_INIT_STATUS_INITIALIZED);
9260       break;
9261
9262     default:
9263       gcc_unreachable ();
9264     }
9265
9266   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9267     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9268
9269   return mem_loc_result;
9270 }
9271
9272 /* Return a descriptor that describes the concatenation of two locations.
9273    This is typically a complex variable.  */
9274
9275 static dw_loc_descr_ref
9276 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9277 {
9278   dw_loc_descr_ref cc_loc_result = NULL;
9279   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9280   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9281
9282   if (x0_ref == 0 || x1_ref == 0)
9283     return 0;
9284
9285   cc_loc_result = x0_ref;
9286   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9287
9288   add_loc_descr (&cc_loc_result, x1_ref);
9289   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9290
9291   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9292     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9293
9294   return cc_loc_result;
9295 }
9296
9297 /* Return a descriptor that describes the concatenation of N
9298    locations.  */
9299
9300 static dw_loc_descr_ref
9301 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9302 {
9303   unsigned int i;
9304   dw_loc_descr_ref cc_loc_result = NULL;
9305   unsigned int n = XVECLEN (concatn, 0);
9306
9307   for (i = 0; i < n; ++i)
9308     {
9309       dw_loc_descr_ref ref;
9310       rtx x = XVECEXP (concatn, 0, i);
9311
9312       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9313       if (ref == NULL)
9314         return NULL;
9315
9316       add_loc_descr (&cc_loc_result, ref);
9317       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9318     }
9319
9320   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9321     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9322
9323   return cc_loc_result;
9324 }
9325
9326 /* Output a proper Dwarf location descriptor for a variable or parameter
9327    which is either allocated in a register or in a memory location.  For a
9328    register, we just generate an OP_REG and the register number.  For a
9329    memory location we provide a Dwarf postfix expression describing how to
9330    generate the (dynamic) address of the object onto the address stack.
9331
9332    If we don't know how to describe it, return 0.  */
9333
9334 static dw_loc_descr_ref
9335 loc_descriptor (rtx rtl, enum var_init_status initialized)
9336 {
9337   dw_loc_descr_ref loc_result = NULL;
9338
9339   switch (GET_CODE (rtl))
9340     {
9341     case SUBREG:
9342       /* The case of a subreg may arise when we have a local (register)
9343          variable or a formal (register) parameter which doesn't quite fill
9344          up an entire register.  For now, just assume that it is
9345          legitimate to make the Dwarf info refer to the whole register which
9346          contains the given subreg.  */
9347       rtl = SUBREG_REG (rtl);
9348
9349       /* ... fall through ...  */
9350
9351     case REG:
9352       loc_result = reg_loc_descriptor (rtl, initialized);
9353       break;
9354
9355     case MEM:
9356       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9357                                        initialized);
9358       break;
9359
9360     case CONCAT:
9361       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9362                                           initialized);
9363       break;
9364
9365     case CONCATN:
9366       loc_result = concatn_loc_descriptor (rtl, initialized);
9367       break;
9368
9369     case VAR_LOCATION:
9370       /* Single part.  */
9371       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9372         {
9373           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9374           break;
9375         }
9376
9377       rtl = XEXP (rtl, 1);
9378       /* FALLTHRU */
9379
9380     case PARALLEL:
9381       {
9382         rtvec par_elems = XVEC (rtl, 0);
9383         int num_elem = GET_NUM_ELEM (par_elems);
9384         enum machine_mode mode;
9385         int i;
9386
9387         /* Create the first one, so we have something to add to.  */
9388         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9389                                      initialized);
9390         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9391         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9392         for (i = 1; i < num_elem; i++)
9393           {
9394             dw_loc_descr_ref temp;
9395
9396             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9397                                    initialized);
9398             add_loc_descr (&loc_result, temp);
9399             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9400             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9401           }
9402       }
9403       break;
9404
9405     default:
9406       gcc_unreachable ();
9407     }
9408
9409   return loc_result;
9410 }
9411
9412 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9413    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9414    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9415    top-level invocation, and we require the address of LOC; is 0 if we require
9416    the value of LOC.  */
9417
9418 static dw_loc_descr_ref
9419 loc_descriptor_from_tree_1 (tree loc, int want_address)
9420 {
9421   dw_loc_descr_ref ret, ret1;
9422   int have_address = 0;
9423   enum dwarf_location_atom op;
9424
9425   /* ??? Most of the time we do not take proper care for sign/zero
9426      extending the values properly.  Hopefully this won't be a real
9427      problem...  */
9428
9429   switch (TREE_CODE (loc))
9430     {
9431     case ERROR_MARK:
9432       return 0;
9433
9434     case PLACEHOLDER_EXPR:
9435       /* This case involves extracting fields from an object to determine the
9436          position of other fields.  We don't try to encode this here.  The
9437          only user of this is Ada, which encodes the needed information using
9438          the names of types.  */
9439       return 0;
9440
9441     case CALL_EXPR:
9442       return 0;
9443
9444     case PREINCREMENT_EXPR:
9445     case PREDECREMENT_EXPR:
9446     case POSTINCREMENT_EXPR:
9447     case POSTDECREMENT_EXPR:
9448       /* There are no opcodes for these operations.  */
9449       return 0;
9450
9451     case ADDR_EXPR:
9452       /* If we already want an address, there's nothing we can do.  */
9453       if (want_address)
9454         return 0;
9455
9456       /* Otherwise, process the argument and look for the address.  */
9457       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9458
9459     case VAR_DECL:
9460       if (DECL_THREAD_LOCAL_P (loc))
9461         {
9462           rtx rtl;
9463
9464           /* If this is not defined, we have no way to emit the data.  */
9465           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9466             return 0;
9467
9468           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9469              look up addresses of objects in the current module.  */
9470           if (DECL_EXTERNAL (loc))
9471             return 0;
9472
9473           rtl = rtl_for_decl_location (loc);
9474           if (rtl == NULL_RTX)
9475             return 0;
9476
9477           if (!MEM_P (rtl))
9478             return 0;
9479           rtl = XEXP (rtl, 0);
9480           if (! CONSTANT_P (rtl))
9481             return 0;
9482
9483           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9484           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9485           ret->dw_loc_oprnd1.v.val_addr = rtl;
9486
9487           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9488           add_loc_descr (&ret, ret1);
9489
9490           have_address = 1;
9491           break;
9492         }
9493       /* FALLTHRU */
9494
9495     case PARM_DECL:
9496       if (DECL_HAS_VALUE_EXPR_P (loc))
9497         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9498                                            want_address);
9499       /* FALLTHRU */
9500
9501     case RESULT_DECL:
9502     case FUNCTION_DECL:
9503       {
9504         rtx rtl = rtl_for_decl_location (loc);
9505
9506         if (rtl == NULL_RTX)
9507           return 0;
9508         else if (GET_CODE (rtl) == CONST_INT)
9509           {
9510             HOST_WIDE_INT val = INTVAL (rtl);
9511             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9512               val &= GET_MODE_MASK (DECL_MODE (loc));
9513             ret = int_loc_descriptor (val);
9514           }
9515         else if (GET_CODE (rtl) == CONST_STRING)
9516           return 0;
9517         else if (CONSTANT_P (rtl))
9518           {
9519             ret = new_loc_descr (DW_OP_addr, 0, 0);
9520             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9521             ret->dw_loc_oprnd1.v.val_addr = rtl;
9522           }
9523         else
9524           {
9525             enum machine_mode mode;
9526
9527             /* Certain constructs can only be represented at top-level.  */
9528             if (want_address == 2)
9529               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9530
9531             mode = GET_MODE (rtl);
9532             if (MEM_P (rtl))
9533               {
9534                 rtl = XEXP (rtl, 0);
9535                 have_address = 1;
9536               }
9537             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9538           }
9539       }
9540       break;
9541
9542     case INDIRECT_REF:
9543       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9544       have_address = 1;
9545       break;
9546
9547     case COMPOUND_EXPR:
9548       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9549
9550     case NOP_EXPR:
9551     case CONVERT_EXPR:
9552     case NON_LVALUE_EXPR:
9553     case VIEW_CONVERT_EXPR:
9554     case SAVE_EXPR:
9555     case GIMPLE_MODIFY_STMT:
9556       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9557                                          want_address);
9558
9559     case COMPONENT_REF:
9560     case BIT_FIELD_REF:
9561     case ARRAY_REF:
9562     case ARRAY_RANGE_REF:
9563       {
9564         tree obj, offset;
9565         HOST_WIDE_INT bitsize, bitpos, bytepos;
9566         enum machine_mode mode;
9567         int volatilep;
9568         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9569
9570         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9571                                    &unsignedp, &volatilep, false);
9572
9573         if (obj == loc)
9574           return 0;
9575
9576         ret = loc_descriptor_from_tree_1 (obj, 1);
9577         if (ret == 0
9578             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9579           return 0;
9580
9581         if (offset != NULL_TREE)
9582           {
9583             /* Variable offset.  */
9584             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9585             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9586           }
9587
9588         bytepos = bitpos / BITS_PER_UNIT;
9589         if (bytepos > 0)
9590           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9591         else if (bytepos < 0)
9592           {
9593             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9594             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9595           }
9596
9597         have_address = 1;
9598         break;
9599       }
9600
9601     case INTEGER_CST:
9602       if (host_integerp (loc, 0))
9603         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9604       else
9605         return 0;
9606       break;
9607
9608     case CONSTRUCTOR:
9609       {
9610         /* Get an RTL for this, if something has been emitted.  */
9611         rtx rtl = lookup_constant_def (loc);
9612         enum machine_mode mode;
9613
9614         if (!rtl || !MEM_P (rtl))
9615           return 0;
9616         mode = GET_MODE (rtl);
9617         rtl = XEXP (rtl, 0);
9618         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9619         have_address = 1;
9620         break;
9621       }
9622
9623     case TRUTH_AND_EXPR:
9624     case TRUTH_ANDIF_EXPR:
9625     case BIT_AND_EXPR:
9626       op = DW_OP_and;
9627       goto do_binop;
9628
9629     case TRUTH_XOR_EXPR:
9630     case BIT_XOR_EXPR:
9631       op = DW_OP_xor;
9632       goto do_binop;
9633
9634     case TRUTH_OR_EXPR:
9635     case TRUTH_ORIF_EXPR:
9636     case BIT_IOR_EXPR:
9637       op = DW_OP_or;
9638       goto do_binop;
9639
9640     case FLOOR_DIV_EXPR:
9641     case CEIL_DIV_EXPR:
9642     case ROUND_DIV_EXPR:
9643     case TRUNC_DIV_EXPR:
9644       op = DW_OP_div;
9645       goto do_binop;
9646
9647     case MINUS_EXPR:
9648       op = DW_OP_minus;
9649       goto do_binop;
9650
9651     case FLOOR_MOD_EXPR:
9652     case CEIL_MOD_EXPR:
9653     case ROUND_MOD_EXPR:
9654     case TRUNC_MOD_EXPR:
9655       op = DW_OP_mod;
9656       goto do_binop;
9657
9658     case MULT_EXPR:
9659       op = DW_OP_mul;
9660       goto do_binop;
9661
9662     case LSHIFT_EXPR:
9663       op = DW_OP_shl;
9664       goto do_binop;
9665
9666     case RSHIFT_EXPR:
9667       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9668       goto do_binop;
9669
9670     case POINTER_PLUS_EXPR:
9671     case PLUS_EXPR:
9672       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9673           && host_integerp (TREE_OPERAND (loc, 1), 0))
9674         {
9675           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9676           if (ret == 0)
9677             return 0;
9678
9679           add_loc_descr (&ret,
9680                          new_loc_descr (DW_OP_plus_uconst,
9681                                         tree_low_cst (TREE_OPERAND (loc, 1),
9682                                                       0),
9683                                         0));
9684           break;
9685         }
9686
9687       op = DW_OP_plus;
9688       goto do_binop;
9689
9690     case LE_EXPR:
9691       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9692         return 0;
9693
9694       op = DW_OP_le;
9695       goto do_binop;
9696
9697     case GE_EXPR:
9698       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9699         return 0;
9700
9701       op = DW_OP_ge;
9702       goto do_binop;
9703
9704     case LT_EXPR:
9705       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9706         return 0;
9707
9708       op = DW_OP_lt;
9709       goto do_binop;
9710
9711     case GT_EXPR:
9712       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9713         return 0;
9714
9715       op = DW_OP_gt;
9716       goto do_binop;
9717
9718     case EQ_EXPR:
9719       op = DW_OP_eq;
9720       goto do_binop;
9721
9722     case NE_EXPR:
9723       op = DW_OP_ne;
9724       goto do_binop;
9725
9726     do_binop:
9727       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9728       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9729       if (ret == 0 || ret1 == 0)
9730         return 0;
9731
9732       add_loc_descr (&ret, ret1);
9733       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9734       break;
9735
9736     case TRUTH_NOT_EXPR:
9737     case BIT_NOT_EXPR:
9738       op = DW_OP_not;
9739       goto do_unop;
9740
9741     case ABS_EXPR:
9742       op = DW_OP_abs;
9743       goto do_unop;
9744
9745     case NEGATE_EXPR:
9746       op = DW_OP_neg;
9747       goto do_unop;
9748
9749     do_unop:
9750       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9751       if (ret == 0)
9752         return 0;
9753
9754       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9755       break;
9756
9757     case MIN_EXPR:
9758     case MAX_EXPR:
9759       {
9760         const enum tree_code code =
9761           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9762
9763         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9764                       build2 (code, integer_type_node,
9765                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9766                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9767       }
9768
9769       /* ... fall through ...  */
9770
9771     case COND_EXPR:
9772       {
9773         dw_loc_descr_ref lhs
9774           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9775         dw_loc_descr_ref rhs
9776           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9777         dw_loc_descr_ref bra_node, jump_node, tmp;
9778
9779         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9780         if (ret == 0 || lhs == 0 || rhs == 0)
9781           return 0;
9782
9783         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9784         add_loc_descr (&ret, bra_node);
9785
9786         add_loc_descr (&ret, rhs);
9787         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9788         add_loc_descr (&ret, jump_node);
9789
9790         add_loc_descr (&ret, lhs);
9791         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9792         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9793
9794         /* ??? Need a node to point the skip at.  Use a nop.  */
9795         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9796         add_loc_descr (&ret, tmp);
9797         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9798         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9799       }
9800       break;
9801
9802     case FIX_TRUNC_EXPR:
9803       return 0;
9804
9805     default:
9806       /* Leave front-end specific codes as simply unknown.  This comes
9807          up, for instance, with the C STMT_EXPR.  */
9808       if ((unsigned int) TREE_CODE (loc)
9809           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9810         return 0;
9811
9812 #ifdef ENABLE_CHECKING
9813       /* Otherwise this is a generic code; we should just lists all of
9814          these explicitly.  We forgot one.  */
9815       gcc_unreachable ();
9816 #else
9817       /* In a release build, we want to degrade gracefully: better to
9818          generate incomplete debugging information than to crash.  */
9819       return NULL;
9820 #endif
9821     }
9822
9823   /* Show if we can't fill the request for an address.  */
9824   if (want_address && !have_address)
9825     return 0;
9826
9827   /* If we've got an address and don't want one, dereference.  */
9828   if (!want_address && have_address && ret)
9829     {
9830       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9831
9832       if (size > DWARF2_ADDR_SIZE || size == -1)
9833         return 0;
9834       else if (size == DWARF2_ADDR_SIZE)
9835         op = DW_OP_deref;
9836       else
9837         op = DW_OP_deref_size;
9838
9839       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9840     }
9841
9842   return ret;
9843 }
9844
9845 static inline dw_loc_descr_ref
9846 loc_descriptor_from_tree (tree loc)
9847 {
9848   return loc_descriptor_from_tree_1 (loc, 2);
9849 }
9850
9851 /* Given a value, round it up to the lowest multiple of `boundary'
9852    which is not less than the value itself.  */
9853
9854 static inline HOST_WIDE_INT
9855 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9856 {
9857   return (((value + boundary - 1) / boundary) * boundary);
9858 }
9859
9860 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9861    pointer to the declared type for the relevant field variable, or return
9862    `integer_type_node' if the given node turns out to be an
9863    ERROR_MARK node.  */
9864
9865 static inline tree
9866 field_type (const_tree decl)
9867 {
9868   tree type;
9869
9870   if (TREE_CODE (decl) == ERROR_MARK)
9871     return integer_type_node;
9872
9873   type = DECL_BIT_FIELD_TYPE (decl);
9874   if (type == NULL_TREE)
9875     type = TREE_TYPE (decl);
9876
9877   return type;
9878 }
9879
9880 /* Given a pointer to a tree node, return the alignment in bits for
9881    it, or else return BITS_PER_WORD if the node actually turns out to
9882    be an ERROR_MARK node.  */
9883
9884 static inline unsigned
9885 simple_type_align_in_bits (const_tree type)
9886 {
9887   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9888 }
9889
9890 static inline unsigned
9891 simple_decl_align_in_bits (const_tree decl)
9892 {
9893   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9894 }
9895
9896 /* Return the result of rounding T up to ALIGN.  */
9897
9898 static inline HOST_WIDE_INT
9899 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9900 {
9901   /* We must be careful if T is negative because HOST_WIDE_INT can be
9902      either "above" or "below" unsigned int as per the C promotion
9903      rules, depending on the host, thus making the signedness of the
9904      direct multiplication and division unpredictable.  */
9905   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9906
9907   u += align - 1;
9908   u /= align;
9909   u *= align;
9910
9911   return (HOST_WIDE_INT) u;
9912 }
9913
9914 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9915    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9916    or return 0 if we are unable to determine what that offset is, either
9917    because the argument turns out to be a pointer to an ERROR_MARK node, or
9918    because the offset is actually variable.  (We can't handle the latter case
9919    just yet).  */
9920
9921 static HOST_WIDE_INT
9922 field_byte_offset (const_tree decl)
9923 {
9924   HOST_WIDE_INT object_offset_in_bits;
9925   HOST_WIDE_INT bitpos_int;
9926
9927   if (TREE_CODE (decl) == ERROR_MARK)
9928     return 0;
9929
9930   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9931
9932   /* We cannot yet cope with fields whose positions are variable, so
9933      for now, when we see such things, we simply return 0.  Someday, we may
9934      be able to handle such cases, but it will be damn difficult.  */
9935   if (! host_integerp (bit_position (decl), 0))
9936     return 0;
9937
9938   bitpos_int = int_bit_position (decl);
9939
9940 #ifdef PCC_BITFIELD_TYPE_MATTERS
9941   if (PCC_BITFIELD_TYPE_MATTERS)
9942     {
9943       tree type;
9944       tree field_size_tree;
9945       HOST_WIDE_INT deepest_bitpos;
9946       unsigned HOST_WIDE_INT field_size_in_bits;
9947       unsigned int type_align_in_bits;
9948       unsigned int decl_align_in_bits;
9949       unsigned HOST_WIDE_INT type_size_in_bits;
9950
9951       type = field_type (decl);
9952       field_size_tree = DECL_SIZE (decl);
9953
9954       /* The size could be unspecified if there was an error, or for
9955          a flexible array member.  */
9956       if (! field_size_tree)
9957         field_size_tree = bitsize_zero_node;
9958
9959       /* If we don't know the size of the field, pretend it's a full word.  */
9960       if (host_integerp (field_size_tree, 1))
9961         field_size_in_bits = tree_low_cst (field_size_tree, 1);
9962       else
9963         field_size_in_bits = BITS_PER_WORD;
9964
9965       type_size_in_bits = simple_type_size_in_bits (type);
9966       type_align_in_bits = simple_type_align_in_bits (type);
9967       decl_align_in_bits = simple_decl_align_in_bits (decl);
9968
9969       /* The GCC front-end doesn't make any attempt to keep track of the
9970          starting bit offset (relative to the start of the containing
9971          structure type) of the hypothetical "containing object" for a
9972          bit-field.  Thus, when computing the byte offset value for the
9973          start of the "containing object" of a bit-field, we must deduce
9974          this information on our own. This can be rather tricky to do in
9975          some cases.  For example, handling the following structure type
9976          definition when compiling for an i386/i486 target (which only
9977          aligns long long's to 32-bit boundaries) can be very tricky:
9978
9979          struct S { int field1; long long field2:31; };
9980
9981          Fortunately, there is a simple rule-of-thumb which can be used
9982          in such cases.  When compiling for an i386/i486, GCC will
9983          allocate 8 bytes for the structure shown above.  It decides to
9984          do this based upon one simple rule for bit-field allocation.
9985          GCC allocates each "containing object" for each bit-field at
9986          the first (i.e. lowest addressed) legitimate alignment boundary
9987          (based upon the required minimum alignment for the declared
9988          type of the field) which it can possibly use, subject to the
9989          condition that there is still enough available space remaining
9990          in the containing object (when allocated at the selected point)
9991          to fully accommodate all of the bits of the bit-field itself.
9992
9993          This simple rule makes it obvious why GCC allocates 8 bytes for
9994          each object of the structure type shown above.  When looking
9995          for a place to allocate the "containing object" for `field2',
9996          the compiler simply tries to allocate a 64-bit "containing
9997          object" at each successive 32-bit boundary (starting at zero)
9998          until it finds a place to allocate that 64- bit field such that
9999          at least 31 contiguous (and previously unallocated) bits remain
10000          within that selected 64 bit field.  (As it turns out, for the
10001          example above, the compiler finds it is OK to allocate the
10002          "containing object" 64-bit field at bit-offset zero within the
10003          structure type.)
10004
10005          Here we attempt to work backwards from the limited set of facts
10006          we're given, and we try to deduce from those facts, where GCC
10007          must have believed that the containing object started (within
10008          the structure type). The value we deduce is then used (by the
10009          callers of this routine) to generate DW_AT_location and
10010          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10011          the case of DW_AT_location, regular fields as well).  */
10012
10013       /* Figure out the bit-distance from the start of the structure to
10014          the "deepest" bit of the bit-field.  */
10015       deepest_bitpos = bitpos_int + field_size_in_bits;
10016
10017       /* This is the tricky part.  Use some fancy footwork to deduce
10018          where the lowest addressed bit of the containing object must
10019          be.  */
10020       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10021
10022       /* Round up to type_align by default.  This works best for
10023          bitfields.  */
10024       object_offset_in_bits
10025         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10026
10027       if (object_offset_in_bits > bitpos_int)
10028         {
10029           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10030
10031           /* Round up to decl_align instead.  */
10032           object_offset_in_bits
10033             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10034         }
10035     }
10036   else
10037 #endif
10038     object_offset_in_bits = bitpos_int;
10039
10040   return object_offset_in_bits / BITS_PER_UNIT;
10041 }
10042 \f
10043 /* The following routines define various Dwarf attributes and any data
10044    associated with them.  */
10045
10046 /* Add a location description attribute value to a DIE.
10047
10048    This emits location attributes suitable for whole variables and
10049    whole parameters.  Note that the location attributes for struct fields are
10050    generated by the routine `data_member_location_attribute' below.  */
10051
10052 static inline void
10053 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10054                              dw_loc_descr_ref descr)
10055 {
10056   if (descr != 0)
10057     add_AT_loc (die, attr_kind, descr);
10058 }
10059
10060 /* Attach the specialized form of location attribute used for data members of
10061    struct and union types.  In the special case of a FIELD_DECL node which
10062    represents a bit-field, the "offset" part of this special location
10063    descriptor must indicate the distance in bytes from the lowest-addressed
10064    byte of the containing struct or union type to the lowest-addressed byte of
10065    the "containing object" for the bit-field.  (See the `field_byte_offset'
10066    function above).
10067
10068    For any given bit-field, the "containing object" is a hypothetical object
10069    (of some integral or enum type) within which the given bit-field lives.  The
10070    type of this hypothetical "containing object" is always the same as the
10071    declared type of the individual bit-field itself (for GCC anyway... the
10072    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10073    bytes) of the hypothetical "containing object" which will be given in the
10074    DW_AT_byte_size attribute for this bit-field.  (See the
10075    `byte_size_attribute' function below.)  It is also used when calculating the
10076    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10077    function below.)  */
10078
10079 static void
10080 add_data_member_location_attribute (dw_die_ref die, tree decl)
10081 {
10082   HOST_WIDE_INT offset;
10083   dw_loc_descr_ref loc_descr = 0;
10084
10085   if (TREE_CODE (decl) == TREE_BINFO)
10086     {
10087       /* We're working on the TAG_inheritance for a base class.  */
10088       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10089         {
10090           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10091              aren't at a fixed offset from all (sub)objects of the same
10092              type.  We need to extract the appropriate offset from our
10093              vtable.  The following dwarf expression means
10094
10095                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10096
10097              This is specific to the V3 ABI, of course.  */
10098
10099           dw_loc_descr_ref tmp;
10100
10101           /* Make a copy of the object address.  */
10102           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10103           add_loc_descr (&loc_descr, tmp);
10104
10105           /* Extract the vtable address.  */
10106           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10107           add_loc_descr (&loc_descr, tmp);
10108
10109           /* Calculate the address of the offset.  */
10110           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10111           gcc_assert (offset < 0);
10112
10113           tmp = int_loc_descriptor (-offset);
10114           add_loc_descr (&loc_descr, tmp);
10115           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10116           add_loc_descr (&loc_descr, tmp);
10117
10118           /* Extract the offset.  */
10119           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10120           add_loc_descr (&loc_descr, tmp);
10121
10122           /* Add it to the object address.  */
10123           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10124           add_loc_descr (&loc_descr, tmp);
10125         }
10126       else
10127         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10128     }
10129   else
10130     offset = field_byte_offset (decl);
10131
10132   if (! loc_descr)
10133     {
10134       enum dwarf_location_atom op;
10135
10136       /* The DWARF2 standard says that we should assume that the structure
10137          address is already on the stack, so we can specify a structure field
10138          address by using DW_OP_plus_uconst.  */
10139
10140 #ifdef MIPS_DEBUGGING_INFO
10141       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10142          operator correctly.  It works only if we leave the offset on the
10143          stack.  */
10144       op = DW_OP_constu;
10145 #else
10146       op = DW_OP_plus_uconst;
10147 #endif
10148
10149       loc_descr = new_loc_descr (op, offset, 0);
10150     }
10151
10152   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10153 }
10154
10155 /* Writes integer values to dw_vec_const array.  */
10156
10157 static void
10158 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10159 {
10160   while (size != 0)
10161     {
10162       *dest++ = val & 0xff;
10163       val >>= 8;
10164       --size;
10165     }
10166 }
10167
10168 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10169
10170 static HOST_WIDE_INT
10171 extract_int (const unsigned char *src, unsigned int size)
10172 {
10173   HOST_WIDE_INT val = 0;
10174
10175   src += size;
10176   while (size != 0)
10177     {
10178       val <<= 8;
10179       val |= *--src & 0xff;
10180       --size;
10181     }
10182   return val;
10183 }
10184
10185 /* Writes floating point values to dw_vec_const array.  */
10186
10187 static void
10188 insert_float (const_rtx rtl, unsigned char *array)
10189 {
10190   REAL_VALUE_TYPE rv;
10191   long val[4];
10192   int i;
10193
10194   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10195   real_to_target (val, &rv, GET_MODE (rtl));
10196
10197   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10198   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10199     {
10200       insert_int (val[i], 4, array);
10201       array += 4;
10202     }
10203 }
10204
10205 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10206    does not have a "location" either in memory or in a register.  These
10207    things can arise in GNU C when a constant is passed as an actual parameter
10208    to an inlined function.  They can also arise in C++ where declared
10209    constants do not necessarily get memory "homes".  */
10210
10211 static void
10212 add_const_value_attribute (dw_die_ref die, rtx rtl)
10213 {
10214   switch (GET_CODE (rtl))
10215     {
10216     case CONST_INT:
10217       {
10218         HOST_WIDE_INT val = INTVAL (rtl);
10219
10220         if (val < 0)
10221           add_AT_int (die, DW_AT_const_value, val);
10222         else
10223           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10224       }
10225       break;
10226
10227     case CONST_DOUBLE:
10228       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10229          floating-point constant.  A CONST_DOUBLE is used whenever the
10230          constant requires more than one word in order to be adequately
10231          represented.  We output CONST_DOUBLEs as blocks.  */
10232       {
10233         enum machine_mode mode = GET_MODE (rtl);
10234
10235         if (SCALAR_FLOAT_MODE_P (mode))
10236           {
10237             unsigned int length = GET_MODE_SIZE (mode);
10238             unsigned char *array = ggc_alloc (length);
10239
10240             insert_float (rtl, array);
10241             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10242           }
10243         else
10244           {
10245             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10246             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10247
10248             add_AT_long_long (die, DW_AT_const_value,
10249                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10250           }
10251       }
10252       break;
10253
10254     case CONST_VECTOR:
10255       {
10256         enum machine_mode mode = GET_MODE (rtl);
10257         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10258         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10259         unsigned char *array = ggc_alloc (length * elt_size);
10260         unsigned int i;
10261         unsigned char *p;
10262
10263         switch (GET_MODE_CLASS (mode))
10264           {
10265           case MODE_VECTOR_INT:
10266             for (i = 0, p = array; i < length; i++, p += elt_size)
10267               {
10268                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10269                 HOST_WIDE_INT lo, hi;
10270
10271                 switch (GET_CODE (elt))
10272                   {
10273                   case CONST_INT:
10274                     lo = INTVAL (elt);
10275                     hi = -(lo < 0);
10276                     break;
10277
10278                   case CONST_DOUBLE:
10279                     lo = CONST_DOUBLE_LOW (elt);
10280                     hi = CONST_DOUBLE_HIGH (elt);
10281                     break;
10282
10283                   default:
10284                     gcc_unreachable ();
10285                   }
10286
10287                 if (elt_size <= sizeof (HOST_WIDE_INT))
10288                   insert_int (lo, elt_size, p);
10289                 else
10290                   {
10291                     unsigned char *p0 = p;
10292                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10293
10294                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10295                     if (WORDS_BIG_ENDIAN)
10296                       {
10297                         p0 = p1;
10298                         p1 = p;
10299                       }
10300                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10301                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10302                   }
10303               }
10304             break;
10305
10306           case MODE_VECTOR_FLOAT:
10307             for (i = 0, p = array; i < length; i++, p += elt_size)
10308               {
10309                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10310                 insert_float (elt, p);
10311               }
10312             break;
10313
10314           default:
10315             gcc_unreachable ();
10316           }
10317
10318         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10319       }
10320       break;
10321
10322     case CONST_STRING:
10323       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10324       break;
10325
10326     case SYMBOL_REF:
10327     case LABEL_REF:
10328     case CONST:
10329       add_AT_addr (die, DW_AT_const_value, rtl);
10330       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10331       break;
10332
10333     case PLUS:
10334       /* In cases where an inlined instance of an inline function is passed
10335          the address of an `auto' variable (which is local to the caller) we
10336          can get a situation where the DECL_RTL of the artificial local
10337          variable (for the inlining) which acts as a stand-in for the
10338          corresponding formal parameter (of the inline function) will look
10339          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10340          exactly a compile-time constant expression, but it isn't the address
10341          of the (artificial) local variable either.  Rather, it represents the
10342          *value* which the artificial local variable always has during its
10343          lifetime.  We currently have no way to represent such quasi-constant
10344          values in Dwarf, so for now we just punt and generate nothing.  */
10345       break;
10346
10347     default:
10348       /* No other kinds of rtx should be possible here.  */
10349       gcc_unreachable ();
10350     }
10351
10352 }
10353
10354 /* Determine whether the evaluation of EXPR references any variables
10355    or functions which aren't otherwise used (and therefore may not be
10356    output).  */
10357 static tree
10358 reference_to_unused (tree * tp, int * walk_subtrees,
10359                      void * data ATTRIBUTE_UNUSED)
10360 {
10361   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10362     *walk_subtrees = 0;
10363
10364   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10365       && ! TREE_ASM_WRITTEN (*tp))
10366     return *tp;
10367   else if (!flag_unit_at_a_time)
10368     return NULL_TREE;
10369   /* ???  The C++ FE emits debug information for using decls, so
10370      putting gcc_unreachable here falls over.  See PR31899.  For now
10371      be conservative.  */
10372   else if (!cgraph_global_info_ready
10373            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10374     return *tp;
10375   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10376     {
10377       struct varpool_node *node = varpool_node (*tp);
10378       if (!node->needed)
10379         return *tp;
10380     }
10381   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10382            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10383     {
10384       struct cgraph_node *node = cgraph_node (*tp);
10385       if (!node->output)
10386         return *tp;
10387     }
10388   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10389     return *tp;
10390
10391   return NULL_TREE;
10392 }
10393
10394 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10395    for use in a later add_const_value_attribute call.  */
10396
10397 static rtx
10398 rtl_for_decl_init (tree init, tree type)
10399 {
10400   rtx rtl = NULL_RTX;
10401
10402   /* If a variable is initialized with a string constant without embedded
10403      zeros, build CONST_STRING.  */
10404   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10405     {
10406       tree enttype = TREE_TYPE (type);
10407       tree domain = TYPE_DOMAIN (type);
10408       enum machine_mode mode = TYPE_MODE (enttype);
10409
10410       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10411           && domain
10412           && integer_zerop (TYPE_MIN_VALUE (domain))
10413           && compare_tree_int (TYPE_MAX_VALUE (domain),
10414                                TREE_STRING_LENGTH (init) - 1) == 0
10415           && ((size_t) TREE_STRING_LENGTH (init)
10416               == strlen (TREE_STRING_POINTER (init)) + 1))
10417         rtl = gen_rtx_CONST_STRING (VOIDmode,
10418                                     ggc_strdup (TREE_STRING_POINTER (init)));
10419     }
10420   /* Other aggregates, and complex values, could be represented using
10421      CONCAT: FIXME!  */
10422   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10423     ;
10424   /* Vectors only work if their mode is supported by the target.
10425      FIXME: generic vectors ought to work too.  */
10426   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10427     ;
10428   /* If the initializer is something that we know will expand into an
10429      immediate RTL constant, expand it now.  We must be careful not to
10430      reference variables which won't be output.  */
10431   else if (initializer_constant_valid_p (init, type)
10432            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10433     {
10434       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10435          possible.  */
10436       if (TREE_CODE (type) == VECTOR_TYPE)
10437         switch (TREE_CODE (init))
10438           {
10439           case VECTOR_CST:
10440             break;
10441           case CONSTRUCTOR:
10442             if (TREE_CONSTANT (init))
10443               {
10444                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10445                 bool constant_p = true;
10446                 tree value;
10447                 unsigned HOST_WIDE_INT ix;
10448
10449                 /* Even when ctor is constant, it might contain non-*_CST
10450                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10451                    belong into VECTOR_CST nodes.  */
10452                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10453                   if (!CONSTANT_CLASS_P (value))
10454                     {
10455                       constant_p = false;
10456                       break;
10457                     }
10458
10459                 if (constant_p)
10460                   {
10461                     init = build_vector_from_ctor (type, elts);
10462                     break;
10463                   }
10464               }
10465             /* FALLTHRU */
10466
10467           default:
10468             return NULL;
10469           }
10470
10471       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10472
10473       /* If expand_expr returns a MEM, it wasn't immediate.  */
10474       gcc_assert (!rtl || !MEM_P (rtl));
10475     }
10476
10477   return rtl;
10478 }
10479
10480 /* Generate RTL for the variable DECL to represent its location.  */
10481
10482 static rtx
10483 rtl_for_decl_location (tree decl)
10484 {
10485   rtx rtl;
10486
10487   /* Here we have to decide where we are going to say the parameter "lives"
10488      (as far as the debugger is concerned).  We only have a couple of
10489      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10490
10491      DECL_RTL normally indicates where the parameter lives during most of the
10492      activation of the function.  If optimization is enabled however, this
10493      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10494      that the parameter doesn't really live anywhere (as far as the code
10495      generation parts of GCC are concerned) during most of the function's
10496      activation.  That will happen (for example) if the parameter is never
10497      referenced within the function.
10498
10499      We could just generate a location descriptor here for all non-NULL
10500      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10501      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10502      where DECL_RTL is NULL or is a pseudo-reg.
10503
10504      Note however that we can only get away with using DECL_INCOMING_RTL as
10505      a backup substitute for DECL_RTL in certain limited cases.  In cases
10506      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10507      we can be sure that the parameter was passed using the same type as it is
10508      declared to have within the function, and that its DECL_INCOMING_RTL
10509      points us to a place where a value of that type is passed.
10510
10511      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10512      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10513      because in these cases DECL_INCOMING_RTL points us to a value of some
10514      type which is *different* from the type of the parameter itself.  Thus,
10515      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10516      such cases, the debugger would end up (for example) trying to fetch a
10517      `float' from a place which actually contains the first part of a
10518      `double'.  That would lead to really incorrect and confusing
10519      output at debug-time.
10520
10521      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10522      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10523      are a couple of exceptions however.  On little-endian machines we can
10524      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10525      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10526      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10527      when (on a little-endian machine) a non-prototyped function has a
10528      parameter declared to be of type `short' or `char'.  In such cases,
10529      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10530      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10531      passed `int' value.  If the debugger then uses that address to fetch
10532      a `short' or a `char' (on a little-endian machine) the result will be
10533      the correct data, so we allow for such exceptional cases below.
10534
10535      Note that our goal here is to describe the place where the given formal
10536      parameter lives during most of the function's activation (i.e. between the
10537      end of the prologue and the start of the epilogue).  We'll do that as best
10538      as we can. Note however that if the given formal parameter is modified
10539      sometime during the execution of the function, then a stack backtrace (at
10540      debug-time) will show the function as having been called with the *new*
10541      value rather than the value which was originally passed in.  This happens
10542      rarely enough that it is not a major problem, but it *is* a problem, and
10543      I'd like to fix it.
10544
10545      A future version of dwarf2out.c may generate two additional attributes for
10546      any given DW_TAG_formal_parameter DIE which will describe the "passed
10547      type" and the "passed location" for the given formal parameter in addition
10548      to the attributes we now generate to indicate the "declared type" and the
10549      "active location" for each parameter.  This additional set of attributes
10550      could be used by debuggers for stack backtraces. Separately, note that
10551      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10552      This happens (for example) for inlined-instances of inline function formal
10553      parameters which are never referenced.  This really shouldn't be
10554      happening.  All PARM_DECL nodes should get valid non-NULL
10555      DECL_INCOMING_RTL values.  FIXME.  */
10556
10557   /* Use DECL_RTL as the "location" unless we find something better.  */
10558   rtl = DECL_RTL_IF_SET (decl);
10559
10560   /* When generating abstract instances, ignore everything except
10561      constants, symbols living in memory, and symbols living in
10562      fixed registers.  */
10563   if (! reload_completed)
10564     {
10565       if (rtl
10566           && (CONSTANT_P (rtl)
10567               || (MEM_P (rtl)
10568                   && CONSTANT_P (XEXP (rtl, 0)))
10569               || (REG_P (rtl)
10570                   && TREE_CODE (decl) == VAR_DECL
10571                   && TREE_STATIC (decl))))
10572         {
10573           rtl = targetm.delegitimize_address (rtl);
10574           return rtl;
10575         }
10576       rtl = NULL_RTX;
10577     }
10578   else if (TREE_CODE (decl) == PARM_DECL)
10579     {
10580       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10581         {
10582           tree declared_type = TREE_TYPE (decl);
10583           tree passed_type = DECL_ARG_TYPE (decl);
10584           enum machine_mode dmode = TYPE_MODE (declared_type);
10585           enum machine_mode pmode = TYPE_MODE (passed_type);
10586
10587           /* This decl represents a formal parameter which was optimized out.
10588              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10589              all cases where (rtl == NULL_RTX) just below.  */
10590           if (dmode == pmode)
10591             rtl = DECL_INCOMING_RTL (decl);
10592           else if (SCALAR_INT_MODE_P (dmode)
10593                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10594                    && DECL_INCOMING_RTL (decl))
10595             {
10596               rtx inc = DECL_INCOMING_RTL (decl);
10597               if (REG_P (inc))
10598                 rtl = inc;
10599               else if (MEM_P (inc))
10600                 {
10601                   if (BYTES_BIG_ENDIAN)
10602                     rtl = adjust_address_nv (inc, dmode,
10603                                              GET_MODE_SIZE (pmode)
10604                                              - GET_MODE_SIZE (dmode));
10605                   else
10606                     rtl = inc;
10607                 }
10608             }
10609         }
10610
10611       /* If the parm was passed in registers, but lives on the stack, then
10612          make a big endian correction if the mode of the type of the
10613          parameter is not the same as the mode of the rtl.  */
10614       /* ??? This is the same series of checks that are made in dbxout.c before
10615          we reach the big endian correction code there.  It isn't clear if all
10616          of these checks are necessary here, but keeping them all is the safe
10617          thing to do.  */
10618       else if (MEM_P (rtl)
10619                && XEXP (rtl, 0) != const0_rtx
10620                && ! CONSTANT_P (XEXP (rtl, 0))
10621                /* Not passed in memory.  */
10622                && !MEM_P (DECL_INCOMING_RTL (decl))
10623                /* Not passed by invisible reference.  */
10624                && (!REG_P (XEXP (rtl, 0))
10625                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10626                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10627 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10628                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10629 #endif
10630                      )
10631                /* Big endian correction check.  */
10632                && BYTES_BIG_ENDIAN
10633                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10634                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10635                    < UNITS_PER_WORD))
10636         {
10637           int offset = (UNITS_PER_WORD
10638                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10639
10640           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10641                              plus_constant (XEXP (rtl, 0), offset));
10642         }
10643     }
10644   else if (TREE_CODE (decl) == VAR_DECL
10645            && rtl
10646            && MEM_P (rtl)
10647            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10648            && BYTES_BIG_ENDIAN)
10649     {
10650       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10651       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10652
10653       /* If a variable is declared "register" yet is smaller than
10654          a register, then if we store the variable to memory, it
10655          looks like we're storing a register-sized value, when in
10656          fact we are not.  We need to adjust the offset of the
10657          storage location to reflect the actual value's bytes,
10658          else gdb will not be able to display it.  */
10659       if (rsize > dsize)
10660         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10661                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10662     }
10663
10664   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10665      and will have been substituted directly into all expressions that use it.
10666      C does not have such a concept, but C++ and other languages do.  */
10667   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10668     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10669
10670   if (rtl)
10671     rtl = targetm.delegitimize_address (rtl);
10672
10673   /* If we don't look past the constant pool, we risk emitting a
10674      reference to a constant pool entry that isn't referenced from
10675      code, and thus is not emitted.  */
10676   if (rtl)
10677     rtl = avoid_constant_pool_reference (rtl);
10678
10679   return rtl;
10680 }
10681
10682 /* We need to figure out what section we should use as the base for the
10683    address ranges where a given location is valid.
10684    1. If this particular DECL has a section associated with it, use that.
10685    2. If this function has a section associated with it, use that.
10686    3. Otherwise, use the text section.
10687    XXX: If you split a variable across multiple sections, we won't notice.  */
10688
10689 static const char *
10690 secname_for_decl (const_tree decl)
10691 {
10692   const char *secname;
10693
10694   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10695     {
10696       tree sectree = DECL_SECTION_NAME (decl);
10697       secname = TREE_STRING_POINTER (sectree);
10698     }
10699   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10700     {
10701       tree sectree = DECL_SECTION_NAME (current_function_decl);
10702       secname = TREE_STRING_POINTER (sectree);
10703     }
10704   else if (cfun && in_cold_section_p)
10705     secname = cfun->cold_section_label;
10706   else
10707     secname = text_section_label;
10708
10709   return secname;
10710 }
10711
10712 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10713    data attribute for a variable or a parameter.  We generate the
10714    DW_AT_const_value attribute only in those cases where the given variable
10715    or parameter does not have a true "location" either in memory or in a
10716    register.  This can happen (for example) when a constant is passed as an
10717    actual argument in a call to an inline function.  (It's possible that
10718    these things can crop up in other ways also.)  Note that one type of
10719    constant value which can be passed into an inlined function is a constant
10720    pointer.  This can happen for example if an actual argument in an inlined
10721    function call evaluates to a compile-time constant address.  */
10722
10723 static void
10724 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10725                                        enum dwarf_attribute attr)
10726 {
10727   rtx rtl;
10728   dw_loc_descr_ref descr;
10729   var_loc_list *loc_list;
10730   struct var_loc_node *node;
10731   if (TREE_CODE (decl) == ERROR_MARK)
10732     return;
10733
10734   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10735               || TREE_CODE (decl) == RESULT_DECL);
10736
10737   /* See if we possibly have multiple locations for this variable.  */
10738   loc_list = lookup_decl_loc (decl);
10739
10740   /* If it truly has multiple locations, the first and last node will
10741      differ.  */
10742   if (loc_list && loc_list->first != loc_list->last)
10743     {
10744       const char *endname, *secname;
10745       dw_loc_list_ref list;
10746       rtx varloc;
10747       enum var_init_status initialized;
10748
10749       /* Now that we know what section we are using for a base,
10750          actually construct the list of locations.
10751          The first location information is what is passed to the
10752          function that creates the location list, and the remaining
10753          locations just get added on to that list.
10754          Note that we only know the start address for a location
10755          (IE location changes), so to build the range, we use
10756          the range [current location start, next location start].
10757          This means we have to special case the last node, and generate
10758          a range of [last location start, end of function label].  */
10759
10760       node = loc_list->first;
10761       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10762       secname = secname_for_decl (decl);
10763
10764       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10765         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10766       else
10767         initialized = VAR_INIT_STATUS_INITIALIZED;
10768
10769       list = new_loc_list (loc_descriptor (varloc, initialized),
10770                            node->label, node->next->label, secname, 1);
10771       node = node->next;
10772
10773       for (; node->next; node = node->next)
10774         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10775           {
10776             /* The variable has a location between NODE->LABEL and
10777                NODE->NEXT->LABEL.  */
10778             enum var_init_status initialized =
10779               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10780             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10781             add_loc_descr_to_loc_list (&list, 
10782                                        loc_descriptor (varloc, initialized),
10783                                        node->label, node->next->label, secname);
10784           }
10785
10786       /* If the variable has a location at the last label
10787          it keeps its location until the end of function.  */
10788       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10789         {
10790           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10791           enum var_init_status initialized =
10792             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10793
10794           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10795           if (!current_function_decl)
10796             endname = text_end_label;
10797           else
10798             {
10799               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10800                                            current_function_funcdef_no);
10801               endname = ggc_strdup (label_id);
10802             }
10803           add_loc_descr_to_loc_list (&list, 
10804                                      loc_descriptor (varloc, initialized),
10805                                      node->label, endname, secname);
10806         }
10807
10808       /* Finally, add the location list to the DIE, and we are done.  */
10809       add_AT_loc_list (die, attr, list);
10810       return;
10811     }
10812
10813   /* Try to get some constant RTL for this decl, and use that as the value of
10814      the location.  */
10815
10816   rtl = rtl_for_decl_location (decl);
10817   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10818     {
10819       add_const_value_attribute (die, rtl);
10820       return;
10821     }
10822
10823   /* If we have tried to generate the location otherwise, and it
10824      didn't work out (we wouldn't be here if we did), and we have a one entry
10825      location list, try generating a location from that.  */
10826   if (loc_list && loc_list->first)
10827     {
10828       enum var_init_status status;
10829       node = loc_list->first;
10830       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10831       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10832       if (descr)
10833         {
10834           add_AT_location_description (die, attr, descr);
10835           return;
10836         }
10837     }
10838
10839   /* We couldn't get any rtl, so try directly generating the location
10840      description from the tree.  */
10841   descr = loc_descriptor_from_tree (decl);
10842   if (descr)
10843     {
10844       add_AT_location_description (die, attr, descr);
10845       return;
10846     }
10847   /* None of that worked, so it must not really have a location;
10848      try adding a constant value attribute from the DECL_INITIAL.  */
10849   tree_add_const_value_attribute (die, decl);
10850 }
10851
10852 /* If we don't have a copy of this variable in memory for some reason (such
10853    as a C++ member constant that doesn't have an out-of-line definition),
10854    we should tell the debugger about the constant value.  */
10855
10856 static void
10857 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10858 {
10859   tree init = DECL_INITIAL (decl);
10860   tree type = TREE_TYPE (decl);
10861   rtx rtl;
10862
10863   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10864     /* OK */;
10865   else
10866     return;
10867
10868   rtl = rtl_for_decl_init (init, type);
10869   if (rtl)
10870     add_const_value_attribute (var_die, rtl);
10871 }
10872
10873 /* Convert the CFI instructions for the current function into a
10874    location list.  This is used for DW_AT_frame_base when we targeting
10875    a dwarf2 consumer that does not support the dwarf3
10876    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10877    expressions.  */
10878
10879 static dw_loc_list_ref
10880 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10881 {
10882   dw_fde_ref fde;
10883   dw_loc_list_ref list, *list_tail;
10884   dw_cfi_ref cfi;
10885   dw_cfa_location last_cfa, next_cfa;
10886   const char *start_label, *last_label, *section;
10887
10888   fde = &fde_table[fde_table_in_use - 1];
10889
10890   section = secname_for_decl (current_function_decl);
10891   list_tail = &list;
10892   list = NULL;
10893
10894   next_cfa.reg = INVALID_REGNUM;
10895   next_cfa.offset = 0;
10896   next_cfa.indirect = 0;
10897   next_cfa.base_offset = 0;
10898
10899   start_label = fde->dw_fde_begin;
10900
10901   /* ??? Bald assumption that the CIE opcode list does not contain
10902      advance opcodes.  */
10903   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10904     lookup_cfa_1 (cfi, &next_cfa);
10905
10906   last_cfa = next_cfa;
10907   last_label = start_label;
10908
10909   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10910     switch (cfi->dw_cfi_opc)
10911       {
10912       case DW_CFA_set_loc:
10913       case DW_CFA_advance_loc1:
10914       case DW_CFA_advance_loc2:
10915       case DW_CFA_advance_loc4:
10916         if (!cfa_equal_p (&last_cfa, &next_cfa))
10917           {
10918             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10919                                        start_label, last_label, section,
10920                                        list == NULL);
10921
10922             list_tail = &(*list_tail)->dw_loc_next;
10923             last_cfa = next_cfa;
10924             start_label = last_label;
10925           }
10926         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10927         break;
10928
10929       case DW_CFA_advance_loc:
10930         /* The encoding is complex enough that we should never emit this.  */
10931       case DW_CFA_remember_state:
10932       case DW_CFA_restore_state:
10933         /* We don't handle these two in this function.  It would be possible
10934            if it were to be required.  */
10935         gcc_unreachable ();
10936
10937       default:
10938         lookup_cfa_1 (cfi, &next_cfa);
10939         break;
10940       }
10941
10942   if (!cfa_equal_p (&last_cfa, &next_cfa))
10943     {
10944       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10945                                  start_label, last_label, section,
10946                                  list == NULL);
10947       list_tail = &(*list_tail)->dw_loc_next;
10948       start_label = last_label;
10949     }
10950   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10951                              start_label, fde->dw_fde_end, section,
10952                              list == NULL);
10953
10954   return list;
10955 }
10956
10957 /* Compute a displacement from the "steady-state frame pointer" to the
10958    frame base (often the same as the CFA), and store it in
10959    frame_pointer_fb_offset.  OFFSET is added to the displacement
10960    before the latter is negated.  */
10961
10962 static void
10963 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10964 {
10965   rtx reg, elim;
10966
10967 #ifdef FRAME_POINTER_CFA_OFFSET
10968   reg = frame_pointer_rtx;
10969   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10970 #else
10971   reg = arg_pointer_rtx;
10972   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10973 #endif
10974
10975   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10976   if (GET_CODE (elim) == PLUS)
10977     {
10978       offset += INTVAL (XEXP (elim, 1));
10979       elim = XEXP (elim, 0);
10980     }
10981   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10982                        : stack_pointer_rtx));
10983
10984   frame_pointer_fb_offset = -offset;
10985 }
10986
10987 /* Generate a DW_AT_name attribute given some string value to be included as
10988    the value of the attribute.  */
10989
10990 static void
10991 add_name_attribute (dw_die_ref die, const char *name_string)
10992 {
10993   if (name_string != NULL && *name_string != 0)
10994     {
10995       if (demangle_name_func)
10996         name_string = (*demangle_name_func) (name_string);
10997
10998       add_AT_string (die, DW_AT_name, name_string);
10999     }
11000 }
11001
11002 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11003
11004 static void
11005 add_comp_dir_attribute (dw_die_ref die)
11006 {
11007   const char *wd = get_src_pwd ();
11008   if (wd != NULL)
11009     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11010 }
11011
11012 /* Given a tree node describing an array bound (either lower or upper) output
11013    a representation for that bound.  */
11014
11015 static void
11016 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11017 {
11018   switch (TREE_CODE (bound))
11019     {
11020     case ERROR_MARK:
11021       return;
11022
11023     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11024     case INTEGER_CST:
11025       if (! host_integerp (bound, 0)
11026           || (bound_attr == DW_AT_lower_bound
11027               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11028                   || (is_fortran () && integer_onep (bound)))))
11029         /* Use the default.  */
11030         ;
11031       else
11032         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11033       break;
11034
11035     case CONVERT_EXPR:
11036     case NOP_EXPR:
11037     case NON_LVALUE_EXPR:
11038     case VIEW_CONVERT_EXPR:
11039       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11040       break;
11041
11042     case SAVE_EXPR:
11043       break;
11044
11045     case VAR_DECL:
11046     case PARM_DECL:
11047     case RESULT_DECL:
11048       {
11049         dw_die_ref decl_die = lookup_decl_die (bound);
11050
11051         /* ??? Can this happen, or should the variable have been bound
11052            first?  Probably it can, since I imagine that we try to create
11053            the types of parameters in the order in which they exist in
11054            the list, and won't have created a forward reference to a
11055            later parameter.  */
11056         if (decl_die != NULL)
11057           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11058         break;
11059       }
11060
11061     default:
11062       {
11063         /* Otherwise try to create a stack operation procedure to
11064            evaluate the value of the array bound.  */
11065
11066         dw_die_ref ctx, decl_die;
11067         dw_loc_descr_ref loc;
11068
11069         loc = loc_descriptor_from_tree (bound);
11070         if (loc == NULL)
11071           break;
11072
11073         if (current_function_decl == 0)
11074           ctx = comp_unit_die;
11075         else
11076           ctx = lookup_decl_die (current_function_decl);
11077
11078         decl_die = new_die (DW_TAG_variable, ctx, bound);
11079         add_AT_flag (decl_die, DW_AT_artificial, 1);
11080         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11081         add_AT_loc (decl_die, DW_AT_location, loc);
11082
11083         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11084         break;
11085       }
11086     }
11087 }
11088
11089 /* Note that the block of subscript information for an array type also
11090    includes information about the element type of type given array type.  */
11091
11092 static void
11093 add_subscript_info (dw_die_ref type_die, tree type)
11094 {
11095 #ifndef MIPS_DEBUGGING_INFO
11096   unsigned dimension_number;
11097 #endif
11098   tree lower, upper;
11099   dw_die_ref subrange_die;
11100
11101   /* The GNU compilers represent multidimensional array types as sequences of
11102      one dimensional array types whose element types are themselves array
11103      types.  Here we squish that down, so that each multidimensional array
11104      type gets only one array_type DIE in the Dwarf debugging info. The draft
11105      Dwarf specification say that we are allowed to do this kind of
11106      compression in C (because there is no difference between an array or
11107      arrays and a multidimensional array in C) but for other source languages
11108      (e.g. Ada) we probably shouldn't do this.  */
11109
11110   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11111      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11112      We work around this by disabling this feature.  See also
11113      gen_array_type_die.  */
11114 #ifndef MIPS_DEBUGGING_INFO
11115   for (dimension_number = 0;
11116        TREE_CODE (type) == ARRAY_TYPE;
11117        type = TREE_TYPE (type), dimension_number++)
11118 #endif
11119     {
11120       tree domain = TYPE_DOMAIN (type);
11121
11122       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11123          and (in GNU C only) variable bounds.  Handle all three forms
11124          here.  */
11125       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11126       if (domain)
11127         {
11128           /* We have an array type with specified bounds.  */
11129           lower = TYPE_MIN_VALUE (domain);
11130           upper = TYPE_MAX_VALUE (domain);
11131
11132           /* Define the index type.  */
11133           if (TREE_TYPE (domain))
11134             {
11135               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11136                  TREE_TYPE field.  We can't emit debug info for this
11137                  because it is an unnamed integral type.  */
11138               if (TREE_CODE (domain) == INTEGER_TYPE
11139                   && TYPE_NAME (domain) == NULL_TREE
11140                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11141                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11142                 ;
11143               else
11144                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11145                                     type_die);
11146             }
11147
11148           /* ??? If upper is NULL, the array has unspecified length,
11149              but it does have a lower bound.  This happens with Fortran
11150                dimension arr(N:*)
11151              Since the debugger is definitely going to need to know N
11152              to produce useful results, go ahead and output the lower
11153              bound solo, and hope the debugger can cope.  */
11154
11155           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11156           if (upper)
11157             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11158         }
11159
11160       /* Otherwise we have an array type with an unspecified length.  The
11161          DWARF-2 spec does not say how to handle this; let's just leave out the
11162          bounds.  */
11163     }
11164 }
11165
11166 static void
11167 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11168 {
11169   unsigned size;
11170
11171   switch (TREE_CODE (tree_node))
11172     {
11173     case ERROR_MARK:
11174       size = 0;
11175       break;
11176     case ENUMERAL_TYPE:
11177     case RECORD_TYPE:
11178     case UNION_TYPE:
11179     case QUAL_UNION_TYPE:
11180       size = int_size_in_bytes (tree_node);
11181       break;
11182     case FIELD_DECL:
11183       /* For a data member of a struct or union, the DW_AT_byte_size is
11184          generally given as the number of bytes normally allocated for an
11185          object of the *declared* type of the member itself.  This is true
11186          even for bit-fields.  */
11187       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11188       break;
11189     default:
11190       gcc_unreachable ();
11191     }
11192
11193   /* Note that `size' might be -1 when we get to this point.  If it is, that
11194      indicates that the byte size of the entity in question is variable.  We
11195      have no good way of expressing this fact in Dwarf at the present time,
11196      so just let the -1 pass on through.  */
11197   add_AT_unsigned (die, DW_AT_byte_size, size);
11198 }
11199
11200 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11201    which specifies the distance in bits from the highest order bit of the
11202    "containing object" for the bit-field to the highest order bit of the
11203    bit-field itself.
11204
11205    For any given bit-field, the "containing object" is a hypothetical object
11206    (of some integral or enum type) within which the given bit-field lives.  The
11207    type of this hypothetical "containing object" is always the same as the
11208    declared type of the individual bit-field itself.  The determination of the
11209    exact location of the "containing object" for a bit-field is rather
11210    complicated.  It's handled by the `field_byte_offset' function (above).
11211
11212    Note that it is the size (in bytes) of the hypothetical "containing object"
11213    which will be given in the DW_AT_byte_size attribute for this bit-field.
11214    (See `byte_size_attribute' above).  */
11215
11216 static inline void
11217 add_bit_offset_attribute (dw_die_ref die, tree decl)
11218 {
11219   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11220   tree type = DECL_BIT_FIELD_TYPE (decl);
11221   HOST_WIDE_INT bitpos_int;
11222   HOST_WIDE_INT highest_order_object_bit_offset;
11223   HOST_WIDE_INT highest_order_field_bit_offset;
11224   HOST_WIDE_INT unsigned bit_offset;
11225
11226   /* Must be a field and a bit field.  */
11227   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11228
11229   /* We can't yet handle bit-fields whose offsets are variable, so if we
11230      encounter such things, just return without generating any attribute
11231      whatsoever.  Likewise for variable or too large size.  */
11232   if (! host_integerp (bit_position (decl), 0)
11233       || ! host_integerp (DECL_SIZE (decl), 1))
11234     return;
11235
11236   bitpos_int = int_bit_position (decl);
11237
11238   /* Note that the bit offset is always the distance (in bits) from the
11239      highest-order bit of the "containing object" to the highest-order bit of
11240      the bit-field itself.  Since the "high-order end" of any object or field
11241      is different on big-endian and little-endian machines, the computation
11242      below must take account of these differences.  */
11243   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11244   highest_order_field_bit_offset = bitpos_int;
11245
11246   if (! BYTES_BIG_ENDIAN)
11247     {
11248       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11249       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11250     }
11251
11252   bit_offset
11253     = (! BYTES_BIG_ENDIAN
11254        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11255        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11256
11257   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11258 }
11259
11260 /* For a FIELD_DECL node which represents a bit field, output an attribute
11261    which specifies the length in bits of the given field.  */
11262
11263 static inline void
11264 add_bit_size_attribute (dw_die_ref die, tree decl)
11265 {
11266   /* Must be a field and a bit field.  */
11267   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11268               && DECL_BIT_FIELD_TYPE (decl));
11269
11270   if (host_integerp (DECL_SIZE (decl), 1))
11271     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11272 }
11273
11274 /* If the compiled language is ANSI C, then add a 'prototyped'
11275    attribute, if arg types are given for the parameters of a function.  */
11276
11277 static inline void
11278 add_prototyped_attribute (dw_die_ref die, tree func_type)
11279 {
11280   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11281       && TYPE_ARG_TYPES (func_type) != NULL)
11282     add_AT_flag (die, DW_AT_prototyped, 1);
11283 }
11284
11285 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11286    by looking in either the type declaration or object declaration
11287    equate table.  */
11288
11289 static inline void
11290 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11291 {
11292   dw_die_ref origin_die = NULL;
11293
11294   if (TREE_CODE (origin) != FUNCTION_DECL)
11295     {
11296       /* We may have gotten separated from the block for the inlined
11297          function, if we're in an exception handler or some such; make
11298          sure that the abstract function has been written out.
11299
11300          Doing this for nested functions is wrong, however; functions are
11301          distinct units, and our context might not even be inline.  */
11302       tree fn = origin;
11303
11304       if (TYPE_P (fn))
11305         fn = TYPE_STUB_DECL (fn);
11306
11307       fn = decl_function_context (fn);
11308       if (fn)
11309         dwarf2out_abstract_function (fn);
11310     }
11311
11312   if (DECL_P (origin))
11313     origin_die = lookup_decl_die (origin);
11314   else if (TYPE_P (origin))
11315     origin_die = lookup_type_die (origin);
11316
11317   /* XXX: Functions that are never lowered don't always have correct block
11318      trees (in the case of java, they simply have no block tree, in some other
11319      languages).  For these functions, there is nothing we can really do to
11320      output correct debug info for inlined functions in all cases.  Rather
11321      than die, we'll just produce deficient debug info now, in that we will
11322      have variables without a proper abstract origin.  In the future, when all
11323      functions are lowered, we should re-add a gcc_assert (origin_die)
11324      here.  */
11325
11326   if (origin_die)
11327       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11328 }
11329
11330 /* We do not currently support the pure_virtual attribute.  */
11331
11332 static inline void
11333 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11334 {
11335   if (DECL_VINDEX (func_decl))
11336     {
11337       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11338
11339       if (host_integerp (DECL_VINDEX (func_decl), 0))
11340         add_AT_loc (die, DW_AT_vtable_elem_location,
11341                     new_loc_descr (DW_OP_constu,
11342                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11343                                    0));
11344
11345       /* GNU extension: Record what type this method came from originally.  */
11346       if (debug_info_level > DINFO_LEVEL_TERSE)
11347         add_AT_die_ref (die, DW_AT_containing_type,
11348                         lookup_type_die (DECL_CONTEXT (func_decl)));
11349     }
11350 }
11351 \f
11352 /* Add source coordinate attributes for the given decl.  */
11353
11354 static void
11355 add_src_coords_attributes (dw_die_ref die, tree decl)
11356 {
11357   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11358
11359   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11360   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11361 }
11362
11363 /* Add a DW_AT_name attribute and source coordinate attribute for the
11364    given decl, but only if it actually has a name.  */
11365
11366 static void
11367 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11368 {
11369   tree decl_name;
11370
11371   decl_name = DECL_NAME (decl);
11372   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11373     {
11374       add_name_attribute (die, dwarf2_name (decl, 0));
11375       if (! DECL_ARTIFICIAL (decl))
11376         add_src_coords_attributes (die, decl);
11377
11378       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11379           && TREE_PUBLIC (decl)
11380           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11381           && !DECL_ABSTRACT (decl)
11382           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11383           && !is_fortran ())
11384         add_AT_string (die, DW_AT_MIPS_linkage_name,
11385                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11386     }
11387
11388 #ifdef VMS_DEBUGGING_INFO
11389   /* Get the function's name, as described by its RTL.  This may be different
11390      from the DECL_NAME name used in the source file.  */
11391   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11392     {
11393       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11394                    XEXP (DECL_RTL (decl), 0));
11395       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11396     }
11397 #endif
11398 }
11399
11400 /* Push a new declaration scope.  */
11401
11402 static void
11403 push_decl_scope (tree scope)
11404 {
11405   VEC_safe_push (tree, gc, decl_scope_table, scope);
11406 }
11407
11408 /* Pop a declaration scope.  */
11409
11410 static inline void
11411 pop_decl_scope (void)
11412 {
11413   VEC_pop (tree, decl_scope_table);
11414 }
11415
11416 /* Return the DIE for the scope that immediately contains this type.
11417    Non-named types get global scope.  Named types nested in other
11418    types get their containing scope if it's open, or global scope
11419    otherwise.  All other types (i.e. function-local named types) get
11420    the current active scope.  */
11421
11422 static dw_die_ref
11423 scope_die_for (tree t, dw_die_ref context_die)
11424 {
11425   dw_die_ref scope_die = NULL;
11426   tree containing_scope;
11427   int i;
11428
11429   /* Non-types always go in the current scope.  */
11430   gcc_assert (TYPE_P (t));
11431
11432   containing_scope = TYPE_CONTEXT (t);
11433
11434   /* Use the containing namespace if it was passed in (for a declaration).  */
11435   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11436     {
11437       if (context_die == lookup_decl_die (containing_scope))
11438         /* OK */;
11439       else
11440         containing_scope = NULL_TREE;
11441     }
11442
11443   /* Ignore function type "scopes" from the C frontend.  They mean that
11444      a tagged type is local to a parmlist of a function declarator, but
11445      that isn't useful to DWARF.  */
11446   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11447     containing_scope = NULL_TREE;
11448
11449   if (containing_scope == NULL_TREE)
11450     scope_die = comp_unit_die;
11451   else if (TYPE_P (containing_scope))
11452     {
11453       /* For types, we can just look up the appropriate DIE.  But
11454          first we check to see if we're in the middle of emitting it
11455          so we know where the new DIE should go.  */
11456       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11457         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11458           break;
11459
11460       if (i < 0)
11461         {
11462           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11463                       || TREE_ASM_WRITTEN (containing_scope));
11464
11465           /* If none of the current dies are suitable, we get file scope.  */
11466           scope_die = comp_unit_die;
11467         }
11468       else
11469         scope_die = lookup_type_die (containing_scope);
11470     }
11471   else
11472     scope_die = context_die;
11473
11474   return scope_die;
11475 }
11476
11477 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11478
11479 static inline int
11480 local_scope_p (dw_die_ref context_die)
11481 {
11482   for (; context_die; context_die = context_die->die_parent)
11483     if (context_die->die_tag == DW_TAG_inlined_subroutine
11484         || context_die->die_tag == DW_TAG_subprogram)
11485       return 1;
11486
11487   return 0;
11488 }
11489
11490 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11491    whether or not to treat a DIE in this context as a declaration.  */
11492
11493 static inline int
11494 class_or_namespace_scope_p (dw_die_ref context_die)
11495 {
11496   return (context_die
11497           && (context_die->die_tag == DW_TAG_structure_type
11498               || context_die->die_tag == DW_TAG_class_type
11499               || context_die->die_tag == DW_TAG_interface_type
11500               || context_die->die_tag == DW_TAG_union_type
11501               || context_die->die_tag == DW_TAG_namespace));
11502 }
11503
11504 /* Many forms of DIEs require a "type description" attribute.  This
11505    routine locates the proper "type descriptor" die for the type given
11506    by 'type', and adds a DW_AT_type attribute below the given die.  */
11507
11508 static void
11509 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11510                     int decl_volatile, dw_die_ref context_die)
11511 {
11512   enum tree_code code  = TREE_CODE (type);
11513   dw_die_ref type_die  = NULL;
11514
11515   /* ??? If this type is an unnamed subrange type of an integral, floating-point
11516      or fixed-point type, use the inner type.  This is because we have no
11517      support for unnamed types in base_type_die.  This can happen if this is
11518      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11519   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11520       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11521     type = TREE_TYPE (type), code = TREE_CODE (type);
11522
11523   if (code == ERROR_MARK
11524       /* Handle a special case.  For functions whose return type is void, we
11525          generate *no* type attribute.  (Note that no object may have type
11526          `void', so this only applies to function return types).  */
11527       || code == VOID_TYPE)
11528     return;
11529
11530   type_die = modified_type_die (type,
11531                                 decl_const || TYPE_READONLY (type),
11532                                 decl_volatile || TYPE_VOLATILE (type),
11533                                 context_die);
11534
11535   if (type_die != NULL)
11536     add_AT_die_ref (object_die, DW_AT_type, type_die);
11537 }
11538
11539 /* Given an object die, add the calling convention attribute for the
11540    function call type.  */
11541 static void
11542 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11543 {
11544   enum dwarf_calling_convention value = DW_CC_normal;
11545
11546   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11547
11548   /* DWARF doesn't provide a way to identify a program's source-level
11549      entry point.  DW_AT_calling_convention attributes are only meant
11550      to describe functions' calling conventions.  However, lacking a
11551      better way to signal the Fortran main program, we use this for the
11552      time being, following existing custom.  */
11553   if (is_fortran ()
11554       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11555     value = DW_CC_program;
11556
11557   /* Only add the attribute if the backend requests it, and
11558      is not DW_CC_normal.  */
11559   if (value && (value != DW_CC_normal))
11560     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11561 }
11562
11563 /* Given a tree pointer to a struct, class, union, or enum type node, return
11564    a pointer to the (string) tag name for the given type, or zero if the type
11565    was declared without a tag.  */
11566
11567 static const char *
11568 type_tag (const_tree type)
11569 {
11570   const char *name = 0;
11571
11572   if (TYPE_NAME (type) != 0)
11573     {
11574       tree t = 0;
11575
11576       /* Find the IDENTIFIER_NODE for the type name.  */
11577       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11578         t = TYPE_NAME (type);
11579
11580       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11581          a TYPE_DECL node, regardless of whether or not a `typedef' was
11582          involved.  */
11583       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11584                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11585         {
11586           /* We want to be extra verbose.  Don't call dwarf_name if
11587              DECL_NAME isn't set.  The default hook for decl_printable_name
11588              doesn't like that, and in this context it's correct to return
11589              0, instead of "<anonymous>" or the like.  */
11590           if (DECL_NAME (TYPE_NAME (type)))
11591             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11592         }
11593
11594       /* Now get the name as a string, or invent one.  */
11595       if (!name && t != 0)
11596         name = IDENTIFIER_POINTER (t);
11597     }
11598
11599   return (name == 0 || *name == '\0') ? 0 : name;
11600 }
11601
11602 /* Return the type associated with a data member, make a special check
11603    for bit field types.  */
11604
11605 static inline tree
11606 member_declared_type (const_tree member)
11607 {
11608   return (DECL_BIT_FIELD_TYPE (member)
11609           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11610 }
11611
11612 /* Get the decl's label, as described by its RTL. This may be different
11613    from the DECL_NAME name used in the source file.  */
11614
11615 #if 0
11616 static const char *
11617 decl_start_label (tree decl)
11618 {
11619   rtx x;
11620   const char *fnname;
11621
11622   x = DECL_RTL (decl);
11623   gcc_assert (MEM_P (x));
11624
11625   x = XEXP (x, 0);
11626   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11627
11628   fnname = XSTR (x, 0);
11629   return fnname;
11630 }
11631 #endif
11632 \f
11633 /* These routines generate the internal representation of the DIE's for
11634    the compilation unit.  Debugging information is collected by walking
11635    the declaration trees passed in from dwarf2out_decl().  */
11636
11637 static void
11638 gen_array_type_die (tree type, dw_die_ref context_die)
11639 {
11640   dw_die_ref scope_die = scope_die_for (type, context_die);
11641   dw_die_ref array_die;
11642   tree element_type;
11643
11644   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11645      the inner array type comes before the outer array type.  Thus we must
11646      call gen_type_die before we call new_die.  See below also.  */
11647 #ifdef MIPS_DEBUGGING_INFO
11648   gen_type_die (TREE_TYPE (type), context_die);
11649 #endif
11650
11651   array_die = new_die (DW_TAG_array_type, scope_die, type);
11652   add_name_attribute (array_die, type_tag (type));
11653   equate_type_number_to_die (type, array_die);
11654
11655   if (TREE_CODE (type) == VECTOR_TYPE)
11656     {
11657       /* The frontend feeds us a representation for the vector as a struct
11658          containing an array.  Pull out the array type.  */
11659       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11660       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11661     }
11662
11663   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11664   if (is_fortran ()
11665       && TREE_CODE (type) == ARRAY_TYPE
11666       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11667     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11668
11669 #if 0
11670   /* We default the array ordering.  SDB will probably do
11671      the right things even if DW_AT_ordering is not present.  It's not even
11672      an issue until we start to get into multidimensional arrays anyway.  If
11673      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11674      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11675      and when we find out that we need to put these in, we will only do so
11676      for multidimensional arrays.  */
11677   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11678 #endif
11679
11680 #ifdef MIPS_DEBUGGING_INFO
11681   /* The SGI compilers handle arrays of unknown bound by setting
11682      AT_declaration and not emitting any subrange DIEs.  */
11683   if (! TYPE_DOMAIN (type))
11684     add_AT_flag (array_die, DW_AT_declaration, 1);
11685   else
11686 #endif
11687     add_subscript_info (array_die, type);
11688
11689   /* Add representation of the type of the elements of this array type.  */
11690   element_type = TREE_TYPE (type);
11691
11692   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11693      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11694      We work around this by disabling this feature.  See also
11695      add_subscript_info.  */
11696 #ifndef MIPS_DEBUGGING_INFO
11697   while (TREE_CODE (element_type) == ARRAY_TYPE)
11698     element_type = TREE_TYPE (element_type);
11699
11700   gen_type_die (element_type, context_die);
11701 #endif
11702
11703   add_type_attribute (array_die, element_type, 0, 0, context_die);
11704
11705   if (get_AT (array_die, DW_AT_name))
11706     add_pubtype (type, array_die);
11707 }
11708
11709 static dw_loc_descr_ref
11710 descr_info_loc (tree val, tree base_decl)
11711 {
11712   HOST_WIDE_INT size;
11713   dw_loc_descr_ref loc, loc2;
11714   enum dwarf_location_atom op;
11715
11716   if (val == base_decl)
11717     return new_loc_descr (DW_OP_push_object_address, 0, 0);
11718
11719   switch (TREE_CODE (val))
11720     {
11721     case NOP_EXPR:
11722     case CONVERT_EXPR:
11723       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11724     case INTEGER_CST:
11725       if (host_integerp (val, 0))
11726         return int_loc_descriptor (tree_low_cst (val, 0));
11727       break;
11728     case INDIRECT_REF:
11729       size = int_size_in_bytes (TREE_TYPE (val));
11730       if (size < 0)
11731         break;
11732       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11733       if (!loc)
11734         break;
11735       if (size == DWARF2_ADDR_SIZE)
11736         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11737       else
11738         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11739       return loc;
11740     case POINTER_PLUS_EXPR:
11741     case PLUS_EXPR:
11742       if (host_integerp (TREE_OPERAND (val, 1), 1)
11743           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11744              < 16384)
11745         {
11746           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11747           if (!loc)
11748             break;
11749           add_loc_descr (&loc,
11750                          new_loc_descr (DW_OP_plus_uconst,
11751                                         tree_low_cst (TREE_OPERAND (val, 1),
11752                                                       1), 0));
11753         }
11754       else
11755         {
11756           op = DW_OP_plus;
11757         do_binop:
11758           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11759           if (!loc)
11760             break;
11761           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11762           if (!loc2)
11763             break;
11764           add_loc_descr (&loc, loc2);
11765           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11766         }
11767       return loc;
11768     case MINUS_EXPR:
11769       op = DW_OP_minus;
11770       goto do_binop;
11771     case MULT_EXPR:
11772       op = DW_OP_mul;
11773       goto do_binop;
11774     case EQ_EXPR:
11775       op = DW_OP_eq;
11776       goto do_binop;
11777     case NE_EXPR:
11778       op = DW_OP_ne;
11779       goto do_binop;
11780     default:
11781       break;
11782     }
11783   return NULL;
11784 }
11785
11786 static void
11787 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11788                       tree val, tree base_decl)
11789 {
11790   dw_loc_descr_ref loc;
11791
11792   if (host_integerp (val, 0))
11793     {
11794       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11795       return;
11796     }
11797
11798   loc = descr_info_loc (val, base_decl);
11799   if (!loc)
11800     return;
11801
11802   add_AT_loc (die, attr, loc);
11803 }
11804
11805 /* This routine generates DIE for array with hidden descriptor, details
11806    are filled into *info by a langhook.  */
11807
11808 static void
11809 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11810                           dw_die_ref context_die)
11811 {
11812   dw_die_ref scope_die = scope_die_for (type, context_die);
11813   dw_die_ref array_die;
11814   int dim;
11815
11816   array_die = new_die (DW_TAG_array_type, scope_die, type);
11817   add_name_attribute (array_die, type_tag (type));
11818   equate_type_number_to_die (type, array_die);
11819
11820   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11821   if (is_fortran ()
11822       && info->ndimensions >= 2)
11823     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11824
11825   if (info->data_location)
11826     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11827                           info->base_decl);
11828   if (info->associated)
11829     add_descr_info_field (array_die, DW_AT_associated, info->associated,
11830                           info->base_decl);
11831   if (info->allocated)
11832     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11833                           info->base_decl);
11834
11835   for (dim = 0; dim < info->ndimensions; dim++)
11836     {
11837       dw_die_ref subrange_die
11838         = new_die (DW_TAG_subrange_type, array_die, NULL);
11839
11840       if (info->dimen[dim].lower_bound)
11841         {
11842           /* If it is the default value, omit it.  */
11843           if ((is_c_family () || is_java ())
11844               && integer_zerop (info->dimen[dim].lower_bound))
11845             ;
11846           else if (is_fortran ()
11847                    && integer_onep (info->dimen[dim].lower_bound))
11848             ;
11849           else
11850             add_descr_info_field (subrange_die, DW_AT_lower_bound,
11851                                   info->dimen[dim].lower_bound,
11852                                   info->base_decl);
11853         }
11854       if (info->dimen[dim].upper_bound)
11855         add_descr_info_field (subrange_die, DW_AT_upper_bound,
11856                               info->dimen[dim].upper_bound,
11857                               info->base_decl);
11858       if (info->dimen[dim].stride)
11859         add_descr_info_field (subrange_die, DW_AT_byte_stride,
11860                               info->dimen[dim].stride,
11861                               info->base_decl);
11862     }
11863
11864   gen_type_die (info->element_type, context_die);
11865   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11866
11867   if (get_AT (array_die, DW_AT_name))
11868     add_pubtype (type, array_die);
11869 }
11870
11871 #if 0
11872 static void
11873 gen_entry_point_die (tree decl, dw_die_ref context_die)
11874 {
11875   tree origin = decl_ultimate_origin (decl);
11876   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11877
11878   if (origin != NULL)
11879     add_abstract_origin_attribute (decl_die, origin);
11880   else
11881     {
11882       add_name_and_src_coords_attributes (decl_die, decl);
11883       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11884                           0, 0, context_die);
11885     }
11886
11887   if (DECL_ABSTRACT (decl))
11888     equate_decl_number_to_die (decl, decl_die);
11889   else
11890     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11891 }
11892 #endif
11893
11894 /* Walk through the list of incomplete types again, trying once more to
11895    emit full debugging info for them.  */
11896
11897 static void
11898 retry_incomplete_types (void)
11899 {
11900   int i;
11901
11902   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11903     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11904 }
11905
11906 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11907
11908 static void
11909 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11910 {
11911   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11912
11913   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11914      be incomplete and such types are not marked.  */
11915   add_abstract_origin_attribute (type_die, type);
11916 }
11917
11918 /* Determine what tag to use for a record type.  */
11919
11920 static enum dwarf_tag
11921 record_type_tag (tree type)
11922 {
11923   if (! lang_hooks.types.classify_record)
11924     return DW_TAG_structure_type;
11925
11926   switch (lang_hooks.types.classify_record (type))
11927     {
11928     case RECORD_IS_STRUCT:
11929       return DW_TAG_structure_type;
11930
11931     case RECORD_IS_CLASS:
11932       return DW_TAG_class_type;
11933
11934     case RECORD_IS_INTERFACE:
11935       return DW_TAG_interface_type;
11936
11937     default:
11938       gcc_unreachable ();
11939     }
11940 }
11941
11942 /* Generate a DIE to represent an inlined instance of a structure type.  */
11943
11944 static void
11945 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11946 {
11947   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
11948
11949   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11950      be incomplete and such types are not marked.  */
11951   add_abstract_origin_attribute (type_die, type);
11952 }
11953
11954 /* Generate a DIE to represent an inlined instance of a union type.  */
11955
11956 static void
11957 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11958 {
11959   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11960
11961   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11962      be incomplete and such types are not marked.  */
11963   add_abstract_origin_attribute (type_die, type);
11964 }
11965
11966 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11967    include all of the information about the enumeration values also. Each
11968    enumerated type name/value is listed as a child of the enumerated type
11969    DIE.  */
11970
11971 static dw_die_ref
11972 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11973 {
11974   dw_die_ref type_die = lookup_type_die (type);
11975
11976   if (type_die == NULL)
11977     {
11978       type_die = new_die (DW_TAG_enumeration_type,
11979                           scope_die_for (type, context_die), type);
11980       equate_type_number_to_die (type, type_die);
11981       add_name_attribute (type_die, type_tag (type));
11982     }
11983   else if (! TYPE_SIZE (type))
11984     return type_die;
11985   else
11986     remove_AT (type_die, DW_AT_declaration);
11987
11988   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11989      given enum type is incomplete, do not generate the DW_AT_byte_size
11990      attribute or the DW_AT_element_list attribute.  */
11991   if (TYPE_SIZE (type))
11992     {
11993       tree link;
11994
11995       TREE_ASM_WRITTEN (type) = 1;
11996       add_byte_size_attribute (type_die, type);
11997       if (TYPE_STUB_DECL (type) != NULL_TREE)
11998         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11999
12000       /* If the first reference to this type was as the return type of an
12001          inline function, then it may not have a parent.  Fix this now.  */
12002       if (type_die->die_parent == NULL)
12003         add_child_die (scope_die_for (type, context_die), type_die);
12004
12005       for (link = TYPE_VALUES (type);
12006            link != NULL; link = TREE_CHAIN (link))
12007         {
12008           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12009           tree value = TREE_VALUE (link);
12010
12011           add_name_attribute (enum_die,
12012                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12013
12014           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12015             /* DWARF2 does not provide a way of indicating whether or
12016                not enumeration constants are signed or unsigned.  GDB
12017                always assumes the values are signed, so we output all
12018                values as if they were signed.  That means that
12019                enumeration constants with very large unsigned values
12020                will appear to have negative values in the debugger.  */
12021             add_AT_int (enum_die, DW_AT_const_value,
12022                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12023         }
12024     }
12025   else
12026     add_AT_flag (type_die, DW_AT_declaration, 1);
12027
12028   if (get_AT (type_die, DW_AT_name))
12029     add_pubtype (type, type_die);
12030
12031   return type_die;
12032 }
12033
12034 /* Generate a DIE to represent either a real live formal parameter decl or to
12035    represent just the type of some formal parameter position in some function
12036    type.
12037
12038    Note that this routine is a bit unusual because its argument may be a
12039    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12040    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12041    node.  If it's the former then this function is being called to output a
12042    DIE to represent a formal parameter object (or some inlining thereof).  If
12043    it's the latter, then this function is only being called to output a
12044    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12045    argument type of some subprogram type.  */
12046
12047 static dw_die_ref
12048 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12049 {
12050   dw_die_ref parm_die
12051     = new_die (DW_TAG_formal_parameter, context_die, node);
12052   tree origin;
12053
12054   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12055     {
12056     case tcc_declaration:
12057       origin = decl_ultimate_origin (node);
12058       if (origin != NULL)
12059         add_abstract_origin_attribute (parm_die, origin);
12060       else
12061         {
12062           tree type = TREE_TYPE (node);
12063           add_name_and_src_coords_attributes (parm_die, node);
12064           if (DECL_BY_REFERENCE (node))
12065             type = TREE_TYPE (type);
12066           add_type_attribute (parm_die, type,
12067                               TREE_READONLY (node),
12068                               TREE_THIS_VOLATILE (node),
12069                               context_die);
12070           if (DECL_ARTIFICIAL (node))
12071             add_AT_flag (parm_die, DW_AT_artificial, 1);
12072         }
12073
12074       equate_decl_number_to_die (node, parm_die);
12075       if (! DECL_ABSTRACT (node))
12076         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12077
12078       break;
12079
12080     case tcc_type:
12081       /* We were called with some kind of a ..._TYPE node.  */
12082       add_type_attribute (parm_die, node, 0, 0, context_die);
12083       break;
12084
12085     default:
12086       gcc_unreachable ();
12087     }
12088
12089   return parm_die;
12090 }
12091
12092 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12093    at the end of an (ANSI prototyped) formal parameters list.  */
12094
12095 static void
12096 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12097 {
12098   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12099 }
12100
12101 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12102    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12103    parameters as specified in some function type specification (except for
12104    those which appear as part of a function *definition*).  */
12105
12106 static void
12107 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12108 {
12109   tree link;
12110   tree formal_type = NULL;
12111   tree first_parm_type;
12112   tree arg;
12113
12114   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12115     {
12116       arg = DECL_ARGUMENTS (function_or_method_type);
12117       function_or_method_type = TREE_TYPE (function_or_method_type);
12118     }
12119   else
12120     arg = NULL_TREE;
12121
12122   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12123
12124   /* Make our first pass over the list of formal parameter types and output a
12125      DW_TAG_formal_parameter DIE for each one.  */
12126   for (link = first_parm_type; link; )
12127     {
12128       dw_die_ref parm_die;
12129
12130       formal_type = TREE_VALUE (link);
12131       if (formal_type == void_type_node)
12132         break;
12133
12134       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12135       parm_die = gen_formal_parameter_die (formal_type, context_die);
12136       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12137            && link == first_parm_type)
12138           || (arg && DECL_ARTIFICIAL (arg)))
12139         add_AT_flag (parm_die, DW_AT_artificial, 1);
12140
12141       link = TREE_CHAIN (link);
12142       if (arg)
12143         arg = TREE_CHAIN (arg);
12144     }
12145
12146   /* If this function type has an ellipsis, add a
12147      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12148   if (formal_type != void_type_node)
12149     gen_unspecified_parameters_die (function_or_method_type, context_die);
12150
12151   /* Make our second (and final) pass over the list of formal parameter types
12152      and output DIEs to represent those types (as necessary).  */
12153   for (link = TYPE_ARG_TYPES (function_or_method_type);
12154        link && TREE_VALUE (link);
12155        link = TREE_CHAIN (link))
12156     gen_type_die (TREE_VALUE (link), context_die);
12157 }
12158
12159 /* We want to generate the DIE for TYPE so that we can generate the
12160    die for MEMBER, which has been defined; we will need to refer back
12161    to the member declaration nested within TYPE.  If we're trying to
12162    generate minimal debug info for TYPE, processing TYPE won't do the
12163    trick; we need to attach the member declaration by hand.  */
12164
12165 static void
12166 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12167 {
12168   gen_type_die (type, context_die);
12169
12170   /* If we're trying to avoid duplicate debug info, we may not have
12171      emitted the member decl for this function.  Emit it now.  */
12172   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12173       && ! lookup_decl_die (member))
12174     {
12175       dw_die_ref type_die;
12176       gcc_assert (!decl_ultimate_origin (member));
12177
12178       push_decl_scope (type);
12179       type_die = lookup_type_die (type);
12180       if (TREE_CODE (member) == FUNCTION_DECL)
12181         gen_subprogram_die (member, type_die);
12182       else if (TREE_CODE (member) == FIELD_DECL)
12183         {
12184           /* Ignore the nameless fields that are used to skip bits but handle
12185              C++ anonymous unions and structs.  */
12186           if (DECL_NAME (member) != NULL_TREE
12187               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12188               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12189             {
12190               gen_type_die (member_declared_type (member), type_die);
12191               gen_field_die (member, type_die);
12192             }
12193         }
12194       else
12195         gen_variable_die (member, type_die);
12196
12197       pop_decl_scope ();
12198     }
12199 }
12200
12201 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12202    may later generate inlined and/or out-of-line instances of.  */
12203
12204 static void
12205 dwarf2out_abstract_function (tree decl)
12206 {
12207   dw_die_ref old_die;
12208   tree save_fn;
12209   tree context;
12210   int was_abstract = DECL_ABSTRACT (decl);
12211
12212   /* Make sure we have the actual abstract inline, not a clone.  */
12213   decl = DECL_ORIGIN (decl);
12214
12215   old_die = lookup_decl_die (decl);
12216   if (old_die && get_AT (old_die, DW_AT_inline))
12217     /* We've already generated the abstract instance.  */
12218     return;
12219
12220   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12221      we don't get confused by DECL_ABSTRACT.  */
12222   if (debug_info_level > DINFO_LEVEL_TERSE)
12223     {
12224       context = decl_class_context (decl);
12225       if (context)
12226         gen_type_die_for_member
12227           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12228     }
12229
12230   /* Pretend we've just finished compiling this function.  */
12231   save_fn = current_function_decl;
12232   current_function_decl = decl;
12233   push_cfun (DECL_STRUCT_FUNCTION (decl));
12234
12235   set_decl_abstract_flags (decl, 1);
12236   dwarf2out_decl (decl);
12237   if (! was_abstract)
12238     set_decl_abstract_flags (decl, 0);
12239
12240   current_function_decl = save_fn;
12241   pop_cfun ();
12242 }
12243
12244 /* Helper function of premark_used_types() which gets called through
12245    htab_traverse_resize().
12246
12247    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12248    marked as unused by prune_unused_types.  */
12249 static int
12250 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12251 {
12252   tree type;
12253   dw_die_ref die;
12254
12255   type = *slot;
12256   die = lookup_type_die (type);
12257   if (die != NULL)
12258     die->die_perennial_p = 1;
12259   return 1;
12260 }
12261
12262 /* Mark all members of used_types_hash as perennial.  */
12263 static void
12264 premark_used_types (void)
12265 {
12266   if (cfun && cfun->used_types_hash)
12267     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12268 }
12269
12270 /* Generate a DIE to represent a declared function (either file-scope or
12271    block-local).  */
12272
12273 static void
12274 gen_subprogram_die (tree decl, dw_die_ref context_die)
12275 {
12276   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12277   tree origin = decl_ultimate_origin (decl);
12278   dw_die_ref subr_die;
12279   tree fn_arg_types;
12280   tree outer_scope;
12281   dw_die_ref old_die = lookup_decl_die (decl);
12282   int declaration = (current_function_decl != decl
12283                      || class_or_namespace_scope_p (context_die));
12284
12285   premark_used_types ();
12286
12287   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12288      started to generate the abstract instance of an inline, decided to output
12289      its containing class, and proceeded to emit the declaration of the inline
12290      from the member list for the class.  If so, DECLARATION takes priority;
12291      we'll get back to the abstract instance when done with the class.  */
12292
12293   /* The class-scope declaration DIE must be the primary DIE.  */
12294   if (origin && declaration && class_or_namespace_scope_p (context_die))
12295     {
12296       origin = NULL;
12297       gcc_assert (!old_die);
12298     }
12299
12300   /* Now that the C++ front end lazily declares artificial member fns, we
12301      might need to retrofit the declaration into its class.  */
12302   if (!declaration && !origin && !old_die
12303       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12304       && !class_or_namespace_scope_p (context_die)
12305       && debug_info_level > DINFO_LEVEL_TERSE)
12306     old_die = force_decl_die (decl);
12307
12308   if (origin != NULL)
12309     {
12310       gcc_assert (!declaration || local_scope_p (context_die));
12311
12312       /* Fixup die_parent for the abstract instance of a nested
12313          inline function.  */
12314       if (old_die && old_die->die_parent == NULL)
12315         add_child_die (context_die, old_die);
12316
12317       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12318       add_abstract_origin_attribute (subr_die, origin);
12319     }
12320   else if (old_die)
12321     {
12322       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12323       struct dwarf_file_data * file_index = lookup_filename (s.file);
12324
12325       if (!get_AT_flag (old_die, DW_AT_declaration)
12326           /* We can have a normal definition following an inline one in the
12327              case of redefinition of GNU C extern inlines.
12328              It seems reasonable to use AT_specification in this case.  */
12329           && !get_AT (old_die, DW_AT_inline))
12330         {
12331           /* Detect and ignore this case, where we are trying to output
12332              something we have already output.  */
12333           return;
12334         }
12335
12336       /* If the definition comes from the same place as the declaration,
12337          maybe use the old DIE.  We always want the DIE for this function
12338          that has the *_pc attributes to be under comp_unit_die so the
12339          debugger can find it.  We also need to do this for abstract
12340          instances of inlines, since the spec requires the out-of-line copy
12341          to have the same parent.  For local class methods, this doesn't
12342          apply; we just use the old DIE.  */
12343       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12344           && (DECL_ARTIFICIAL (decl)
12345               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12346                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12347                       == (unsigned) s.line))))
12348         {
12349           subr_die = old_die;
12350
12351           /* Clear out the declaration attribute and the formal parameters.
12352              Do not remove all children, because it is possible that this
12353              declaration die was forced using force_decl_die(). In such
12354              cases die that forced declaration die (e.g. TAG_imported_module)
12355              is one of the children that we do not want to remove.  */
12356           remove_AT (subr_die, DW_AT_declaration);
12357           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12358         }
12359       else
12360         {
12361           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12362           add_AT_specification (subr_die, old_die);
12363           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12364             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12365           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12366             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12367         }
12368     }
12369   else
12370     {
12371       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12372
12373       if (TREE_PUBLIC (decl))
12374         add_AT_flag (subr_die, DW_AT_external, 1);
12375
12376       add_name_and_src_coords_attributes (subr_die, decl);
12377       if (debug_info_level > DINFO_LEVEL_TERSE)
12378         {
12379           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12380           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12381                               0, 0, context_die);
12382         }
12383
12384       add_pure_or_virtual_attribute (subr_die, decl);
12385       if (DECL_ARTIFICIAL (decl))
12386         add_AT_flag (subr_die, DW_AT_artificial, 1);
12387
12388       if (TREE_PROTECTED (decl))
12389         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12390       else if (TREE_PRIVATE (decl))
12391         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12392     }
12393
12394   if (declaration)
12395     {
12396       if (!old_die || !get_AT (old_die, DW_AT_inline))
12397         {
12398           add_AT_flag (subr_die, DW_AT_declaration, 1);
12399
12400           /* The first time we see a member function, it is in the context of
12401              the class to which it belongs.  We make sure of this by emitting
12402              the class first.  The next time is the definition, which is
12403              handled above.  The two may come from the same source text.
12404
12405              Note that force_decl_die() forces function declaration die. It is
12406              later reused to represent definition.  */
12407           equate_decl_number_to_die (decl, subr_die);
12408         }
12409     }
12410   else if (DECL_ABSTRACT (decl))
12411     {
12412       if (DECL_DECLARED_INLINE_P (decl))
12413         {
12414           if (cgraph_function_possibly_inlined_p (decl))
12415             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12416           else
12417             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12418         }
12419       else
12420         {
12421           if (cgraph_function_possibly_inlined_p (decl))
12422             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12423           else
12424             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12425         }
12426
12427       if (DECL_DECLARED_INLINE_P (decl)
12428           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12429         add_AT_flag (subr_die, DW_AT_artificial, 1);
12430
12431       equate_decl_number_to_die (decl, subr_die);
12432     }
12433   else if (!DECL_EXTERNAL (decl))
12434     {
12435       HOST_WIDE_INT cfa_fb_offset;
12436
12437       if (!old_die || !get_AT (old_die, DW_AT_inline))
12438         equate_decl_number_to_die (decl, subr_die);
12439
12440       if (!flag_reorder_blocks_and_partition)
12441         {
12442           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12443                                        current_function_funcdef_no);
12444           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12445           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12446                                        current_function_funcdef_no);
12447           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12448
12449           add_pubname (decl, subr_die);
12450           add_arange (decl, subr_die);
12451         }
12452       else
12453         {  /* Do nothing for now; maybe need to duplicate die, one for
12454               hot section and ond for cold section, then use the hot/cold
12455               section begin/end labels to generate the aranges...  */
12456           /*
12457             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12458             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12459             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12460             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12461
12462             add_pubname (decl, subr_die);
12463             add_arange (decl, subr_die);
12464             add_arange (decl, subr_die);
12465            */
12466         }
12467
12468 #ifdef MIPS_DEBUGGING_INFO
12469       /* Add a reference to the FDE for this routine.  */
12470       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12471 #endif
12472
12473       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12474
12475       /* We define the "frame base" as the function's CFA.  This is more
12476          convenient for several reasons: (1) It's stable across the prologue
12477          and epilogue, which makes it better than just a frame pointer,
12478          (2) With dwarf3, there exists a one-byte encoding that allows us
12479          to reference the .debug_frame data by proxy, but failing that,
12480          (3) We can at least reuse the code inspection and interpretation
12481          code that determines the CFA position at various points in the
12482          function.  */
12483       /* ??? Use some command-line or configury switch to enable the use
12484          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
12485          consumers that understand it; fall back to "pure" dwarf2 and
12486          convert the CFA data into a location list.  */
12487       {
12488         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12489         if (list->dw_loc_next)
12490           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12491         else
12492           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12493       }
12494
12495       /* Compute a displacement from the "steady-state frame pointer" to
12496          the CFA.  The former is what all stack slots and argument slots
12497          will reference in the rtl; the later is what we've told the
12498          debugger about.  We'll need to adjust all frame_base references
12499          by this displacement.  */
12500       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12501
12502       if (cfun->static_chain_decl)
12503         add_AT_location_description (subr_die, DW_AT_static_link,
12504                  loc_descriptor_from_tree (cfun->static_chain_decl));
12505     }
12506
12507   /* Now output descriptions of the arguments for this function. This gets
12508      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12509      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12510      `...' at the end of the formal parameter list.  In order to find out if
12511      there was a trailing ellipsis or not, we must instead look at the type
12512      associated with the FUNCTION_DECL.  This will be a node of type
12513      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12514      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12515      an ellipsis at the end.  */
12516
12517   /* In the case where we are describing a mere function declaration, all we
12518      need to do here (and all we *can* do here) is to describe the *types* of
12519      its formal parameters.  */
12520   if (debug_info_level <= DINFO_LEVEL_TERSE)
12521     ;
12522   else if (declaration)
12523     gen_formal_types_die (decl, subr_die);
12524   else
12525     {
12526       /* Generate DIEs to represent all known formal parameters.  */
12527       tree arg_decls = DECL_ARGUMENTS (decl);
12528       tree parm;
12529
12530       /* When generating DIEs, generate the unspecified_parameters DIE
12531          instead if we come across the arg "__builtin_va_alist" */
12532       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12533         if (TREE_CODE (parm) == PARM_DECL)
12534           {
12535             if (DECL_NAME (parm)
12536                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12537                             "__builtin_va_alist"))
12538               gen_unspecified_parameters_die (parm, subr_die);
12539             else
12540               gen_decl_die (parm, subr_die);
12541           }
12542
12543       /* Decide whether we need an unspecified_parameters DIE at the end.
12544          There are 2 more cases to do this for: 1) the ansi ... declaration -
12545          this is detectable when the end of the arg list is not a
12546          void_type_node 2) an unprototyped function declaration (not a
12547          definition).  This just means that we have no info about the
12548          parameters at all.  */
12549       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12550       if (fn_arg_types != NULL)
12551         {
12552           /* This is the prototyped case, check for....  */
12553           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12554             gen_unspecified_parameters_die (decl, subr_die);
12555         }
12556       else if (DECL_INITIAL (decl) == NULL_TREE)
12557         gen_unspecified_parameters_die (decl, subr_die);
12558     }
12559
12560   /* Output Dwarf info for all of the stuff within the body of the function
12561      (if it has one - it may be just a declaration).  */
12562   outer_scope = DECL_INITIAL (decl);
12563
12564   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12565      a function.  This BLOCK actually represents the outermost binding contour
12566      for the function, i.e. the contour in which the function's formal
12567      parameters and labels get declared. Curiously, it appears that the front
12568      end doesn't actually put the PARM_DECL nodes for the current function onto
12569      the BLOCK_VARS list for this outer scope, but are strung off of the
12570      DECL_ARGUMENTS list for the function instead.
12571
12572      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12573      the LABEL_DECL nodes for the function however, and we output DWARF info
12574      for those in decls_for_scope.  Just within the `outer_scope' there will be
12575      a BLOCK node representing the function's outermost pair of curly braces,
12576      and any blocks used for the base and member initializers of a C++
12577      constructor function.  */
12578   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12579     {
12580       /* Emit a DW_TAG_variable DIE for a named return value.  */
12581       if (DECL_NAME (DECL_RESULT (decl)))
12582         gen_decl_die (DECL_RESULT (decl), subr_die);
12583
12584       current_function_has_inlines = 0;
12585       decls_for_scope (outer_scope, subr_die, 0);
12586
12587 #if 0 && defined (MIPS_DEBUGGING_INFO)
12588       if (current_function_has_inlines)
12589         {
12590           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12591           if (! comp_unit_has_inlines)
12592             {
12593               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12594               comp_unit_has_inlines = 1;
12595             }
12596         }
12597 #endif
12598     }
12599   /* Add the calling convention attribute if requested.  */
12600   add_calling_convention_attribute (subr_die, decl);
12601
12602 }
12603
12604 /* Generate a DIE to represent a declared data object.  */
12605
12606 static void
12607 gen_variable_die (tree decl, dw_die_ref context_die)
12608 {
12609   tree origin = decl_ultimate_origin (decl);
12610   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12611
12612   dw_die_ref old_die = lookup_decl_die (decl);
12613   int declaration = (DECL_EXTERNAL (decl)
12614                      /* If DECL is COMDAT and has not actually been
12615                         emitted, we cannot take its address; there
12616                         might end up being no definition anywhere in
12617                         the program.  For example, consider the C++
12618                         test case:
12619
12620                           template <class T>
12621                           struct S { static const int i = 7; };
12622
12623                           template <class T>
12624                           const int S<T>::i;
12625
12626                           int f() { return S<int>::i; }
12627
12628                         Here, S<int>::i is not DECL_EXTERNAL, but no
12629                         definition is required, so the compiler will
12630                         not emit a definition.  */
12631                      || (TREE_CODE (decl) == VAR_DECL
12632                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12633                      || class_or_namespace_scope_p (context_die));
12634
12635   if (origin != NULL)
12636     add_abstract_origin_attribute (var_die, origin);
12637
12638   /* Loop unrolling can create multiple blocks that refer to the same
12639      static variable, so we must test for the DW_AT_declaration flag.
12640
12641      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12642      copy decls and set the DECL_ABSTRACT flag on them instead of
12643      sharing them.
12644
12645      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12646
12647      ??? The declare_in_namespace support causes us to get two DIEs for one
12648      variable, both of which are declarations.  We want to avoid considering
12649      one to be a specification, so we must test that this DIE is not a
12650      declaration.  */
12651   else if (old_die && TREE_STATIC (decl) && ! declaration
12652            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12653     {
12654       /* This is a definition of a C++ class level static.  */
12655       add_AT_specification (var_die, old_die);
12656       if (DECL_NAME (decl))
12657         {
12658           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12659           struct dwarf_file_data * file_index = lookup_filename (s.file);
12660
12661           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12662             add_AT_file (var_die, DW_AT_decl_file, file_index);
12663
12664           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12665             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12666         }
12667     }
12668   else
12669     {
12670       tree type = TREE_TYPE (decl);
12671       if ((TREE_CODE (decl) == PARM_DECL
12672            || TREE_CODE (decl) == RESULT_DECL)
12673           && DECL_BY_REFERENCE (decl))
12674         type = TREE_TYPE (type);
12675
12676       add_name_and_src_coords_attributes (var_die, decl);
12677       add_type_attribute (var_die, type, TREE_READONLY (decl),
12678                           TREE_THIS_VOLATILE (decl), context_die);
12679
12680       if (TREE_PUBLIC (decl))
12681         add_AT_flag (var_die, DW_AT_external, 1);
12682
12683       if (DECL_ARTIFICIAL (decl))
12684         add_AT_flag (var_die, DW_AT_artificial, 1);
12685
12686       if (TREE_PROTECTED (decl))
12687         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12688       else if (TREE_PRIVATE (decl))
12689         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12690     }
12691
12692   if (declaration)
12693     add_AT_flag (var_die, DW_AT_declaration, 1);
12694
12695   if (DECL_ABSTRACT (decl) || declaration)
12696     equate_decl_number_to_die (decl, var_die);
12697
12698   if (! declaration && ! DECL_ABSTRACT (decl))
12699     {
12700       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12701       add_pubname (decl, var_die);
12702     }
12703   else
12704     tree_add_const_value_attribute (var_die, decl);
12705 }
12706
12707 /* Generate a DIE to represent a label identifier.  */
12708
12709 static void
12710 gen_label_die (tree decl, dw_die_ref context_die)
12711 {
12712   tree origin = decl_ultimate_origin (decl);
12713   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12714   rtx insn;
12715   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12716
12717   if (origin != NULL)
12718     add_abstract_origin_attribute (lbl_die, origin);
12719   else
12720     add_name_and_src_coords_attributes (lbl_die, decl);
12721
12722   if (DECL_ABSTRACT (decl))
12723     equate_decl_number_to_die (decl, lbl_die);
12724   else
12725     {
12726       insn = DECL_RTL_IF_SET (decl);
12727
12728       /* Deleted labels are programmer specified labels which have been
12729          eliminated because of various optimizations.  We still emit them
12730          here so that it is possible to put breakpoints on them.  */
12731       if (insn
12732           && (LABEL_P (insn)
12733               || ((NOTE_P (insn)
12734                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12735         {
12736           /* When optimization is enabled (via -O) some parts of the compiler
12737              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12738              represent source-level labels which were explicitly declared by
12739              the user.  This really shouldn't be happening though, so catch
12740              it if it ever does happen.  */
12741           gcc_assert (!INSN_DELETED_P (insn));
12742
12743           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12744           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12745         }
12746     }
12747 }
12748
12749 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12750    attributes to the DIE for a block STMT, to describe where the inlined
12751    function was called from.  This is similar to add_src_coords_attributes.  */
12752
12753 static inline void
12754 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12755 {
12756   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12757
12758   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12759   add_AT_unsigned (die, DW_AT_call_line, s.line);
12760 }
12761
12762
12763 /* If STMT's abstract origin is a function declaration and STMT's
12764    first subblock's abstract origin is the function's outermost block,
12765    then we're looking at the main entry point.  */
12766 static bool
12767 is_inlined_entry_point (const_tree stmt)
12768 {
12769   tree decl, block;
12770
12771   if (!stmt || TREE_CODE (stmt) != BLOCK)
12772     return false;
12773
12774   decl = block_ultimate_origin (stmt);
12775
12776   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12777     return false;
12778
12779   block = BLOCK_SUBBLOCKS (stmt);
12780
12781   if (block)
12782     {
12783       if (TREE_CODE (block) != BLOCK)
12784         return false;
12785
12786       block = block_ultimate_origin (block);
12787     }
12788
12789   return block == DECL_INITIAL (decl);
12790 }
12791
12792 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12793    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12794
12795 static inline void
12796 add_high_low_attributes (tree stmt, dw_die_ref die)
12797 {
12798   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12799
12800   if (BLOCK_FRAGMENT_CHAIN (stmt))
12801     {
12802       tree chain;
12803
12804       if (is_inlined_entry_point (stmt))
12805         {
12806           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12807                                        BLOCK_NUMBER (stmt));
12808           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12809         }
12810
12811       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12812
12813       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12814       do
12815         {
12816           add_ranges (chain);
12817           chain = BLOCK_FRAGMENT_CHAIN (chain);
12818         }
12819       while (chain);
12820       add_ranges (NULL);
12821     }
12822   else
12823     {
12824       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12825                                    BLOCK_NUMBER (stmt));
12826       add_AT_lbl_id (die, DW_AT_low_pc, label);
12827       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12828                                    BLOCK_NUMBER (stmt));
12829       add_AT_lbl_id (die, DW_AT_high_pc, label);
12830     }
12831 }
12832
12833 /* Generate a DIE for a lexical block.  */
12834
12835 static void
12836 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12837 {
12838   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12839
12840   if (! BLOCK_ABSTRACT (stmt))
12841     add_high_low_attributes (stmt, stmt_die);
12842
12843   decls_for_scope (stmt, stmt_die, depth);
12844 }
12845
12846 /* Generate a DIE for an inlined subprogram.  */
12847
12848 static void
12849 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12850 {
12851   tree decl = block_ultimate_origin (stmt);
12852
12853   /* Emit info for the abstract instance first, if we haven't yet.  We
12854      must emit this even if the block is abstract, otherwise when we
12855      emit the block below (or elsewhere), we may end up trying to emit
12856      a die whose origin die hasn't been emitted, and crashing.  */
12857   dwarf2out_abstract_function (decl);
12858
12859   if (! BLOCK_ABSTRACT (stmt))
12860     {
12861       dw_die_ref subr_die
12862         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12863
12864       add_abstract_origin_attribute (subr_die, decl);
12865       add_high_low_attributes (stmt, subr_die);
12866       add_call_src_coords_attributes (stmt, subr_die);
12867
12868       decls_for_scope (stmt, subr_die, depth);
12869       current_function_has_inlines = 1;
12870     }
12871   else
12872     /* We may get here if we're the outer block of function A that was
12873        inlined into function B that was inlined into function C.  When
12874        generating debugging info for C, dwarf2out_abstract_function(B)
12875        would mark all inlined blocks as abstract, including this one.
12876        So, we wouldn't (and shouldn't) expect labels to be generated
12877        for this one.  Instead, just emit debugging info for
12878        declarations within the block.  This is particularly important
12879        in the case of initializers of arguments passed from B to us:
12880        if they're statement expressions containing declarations, we
12881        wouldn't generate dies for their abstract variables, and then,
12882        when generating dies for the real variables, we'd die (pun
12883        intended :-)  */
12884     gen_lexical_block_die (stmt, context_die, depth);
12885 }
12886
12887 /* Generate a DIE for a field in a record, or structure.  */
12888
12889 static void
12890 gen_field_die (tree decl, dw_die_ref context_die)
12891 {
12892   dw_die_ref decl_die;
12893
12894   if (TREE_TYPE (decl) == error_mark_node)
12895     return;
12896
12897   decl_die = new_die (DW_TAG_member, context_die, decl);
12898   add_name_and_src_coords_attributes (decl_die, decl);
12899   add_type_attribute (decl_die, member_declared_type (decl),
12900                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12901                       context_die);
12902
12903   if (DECL_BIT_FIELD_TYPE (decl))
12904     {
12905       add_byte_size_attribute (decl_die, decl);
12906       add_bit_size_attribute (decl_die, decl);
12907       add_bit_offset_attribute (decl_die, decl);
12908     }
12909
12910   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12911     add_data_member_location_attribute (decl_die, decl);
12912
12913   if (DECL_ARTIFICIAL (decl))
12914     add_AT_flag (decl_die, DW_AT_artificial, 1);
12915
12916   if (TREE_PROTECTED (decl))
12917     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12918   else if (TREE_PRIVATE (decl))
12919     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12920
12921   /* Equate decl number to die, so that we can look up this decl later on.  */
12922   equate_decl_number_to_die (decl, decl_die);
12923 }
12924
12925 #if 0
12926 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12927    Use modified_type_die instead.
12928    We keep this code here just in case these types of DIEs may be needed to
12929    represent certain things in other languages (e.g. Pascal) someday.  */
12930
12931 static void
12932 gen_pointer_type_die (tree type, dw_die_ref context_die)
12933 {
12934   dw_die_ref ptr_die
12935     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12936
12937   equate_type_number_to_die (type, ptr_die);
12938   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12939   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12940 }
12941
12942 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12943    Use modified_type_die instead.
12944    We keep this code here just in case these types of DIEs may be needed to
12945    represent certain things in other languages (e.g. Pascal) someday.  */
12946
12947 static void
12948 gen_reference_type_die (tree type, dw_die_ref context_die)
12949 {
12950   dw_die_ref ref_die
12951     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12952
12953   equate_type_number_to_die (type, ref_die);
12954   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12955   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12956 }
12957 #endif
12958
12959 /* Generate a DIE for a pointer to a member type.  */
12960
12961 static void
12962 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12963 {
12964   dw_die_ref ptr_die
12965     = new_die (DW_TAG_ptr_to_member_type,
12966                scope_die_for (type, context_die), type);
12967
12968   equate_type_number_to_die (type, ptr_die);
12969   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12970                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12971   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12972 }
12973
12974 /* Generate the DIE for the compilation unit.  */
12975
12976 static dw_die_ref
12977 gen_compile_unit_die (const char *filename)
12978 {
12979   dw_die_ref die;
12980   char producer[250];
12981   const char *language_string = lang_hooks.name;
12982   int language;
12983
12984   die = new_die (DW_TAG_compile_unit, NULL, NULL);
12985
12986   if (filename)
12987     {
12988       add_name_attribute (die, filename);
12989       /* Don't add cwd for <built-in>.  */
12990       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12991         add_comp_dir_attribute (die);
12992     }
12993
12994   sprintf (producer, "%s %s", language_string, version_string);
12995
12996 #ifdef MIPS_DEBUGGING_INFO
12997   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12998      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12999      not appear in the producer string, the debugger reaches the conclusion
13000      that the object file is stripped and has no debugging information.
13001      To get the MIPS/SGI debugger to believe that there is debugging
13002      information in the object file, we add a -g to the producer string.  */
13003   if (debug_info_level > DINFO_LEVEL_TERSE)
13004     strcat (producer, " -g");
13005 #endif
13006
13007   add_AT_string (die, DW_AT_producer, producer);
13008
13009   if (strcmp (language_string, "GNU C++") == 0)
13010     language = DW_LANG_C_plus_plus;
13011   else if (strcmp (language_string, "GNU Ada") == 0)
13012     language = DW_LANG_Ada95;
13013   else if (strcmp (language_string, "GNU F77") == 0)
13014     language = DW_LANG_Fortran77;
13015   else if (strcmp (language_string, "GNU F95") == 0)
13016     language = DW_LANG_Fortran95;
13017   else if (strcmp (language_string, "GNU Pascal") == 0)
13018     language = DW_LANG_Pascal83;
13019   else if (strcmp (language_string, "GNU Java") == 0)
13020     language = DW_LANG_Java;
13021   else if (strcmp (language_string, "GNU Objective-C") == 0)
13022     language = DW_LANG_ObjC;
13023   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13024     language = DW_LANG_ObjC_plus_plus;
13025   else
13026     language = DW_LANG_C89;
13027
13028   add_AT_unsigned (die, DW_AT_language, language);
13029   return die;
13030 }
13031
13032 /* Generate the DIE for a base class.  */
13033
13034 static void
13035 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13036 {
13037   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13038
13039   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13040   add_data_member_location_attribute (die, binfo);
13041
13042   if (BINFO_VIRTUAL_P (binfo))
13043     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13044
13045   if (access == access_public_node)
13046     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13047   else if (access == access_protected_node)
13048     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13049 }
13050
13051 /* Generate a DIE for a class member.  */
13052
13053 static void
13054 gen_member_die (tree type, dw_die_ref context_die)
13055 {
13056   tree member;
13057   tree binfo = TYPE_BINFO (type);
13058   dw_die_ref child;
13059
13060   /* If this is not an incomplete type, output descriptions of each of its
13061      members. Note that as we output the DIEs necessary to represent the
13062      members of this record or union type, we will also be trying to output
13063      DIEs to represent the *types* of those members. However the `type'
13064      function (above) will specifically avoid generating type DIEs for member
13065      types *within* the list of member DIEs for this (containing) type except
13066      for those types (of members) which are explicitly marked as also being
13067      members of this (containing) type themselves.  The g++ front- end can
13068      force any given type to be treated as a member of some other (containing)
13069      type by setting the TYPE_CONTEXT of the given (member) type to point to
13070      the TREE node representing the appropriate (containing) type.  */
13071
13072   /* First output info about the base classes.  */
13073   if (binfo)
13074     {
13075       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13076       int i;
13077       tree base;
13078
13079       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13080         gen_inheritance_die (base,
13081                              (accesses ? VEC_index (tree, accesses, i)
13082                               : access_public_node), context_die);
13083     }
13084
13085   /* Now output info about the data members and type members.  */
13086   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13087     {
13088       /* If we thought we were generating minimal debug info for TYPE
13089          and then changed our minds, some of the member declarations
13090          may have already been defined.  Don't define them again, but
13091          do put them in the right order.  */
13092
13093       child = lookup_decl_die (member);
13094       if (child)
13095         splice_child_die (context_die, child);
13096       else
13097         gen_decl_die (member, context_die);
13098     }
13099
13100   /* Now output info about the function members (if any).  */
13101   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13102     {
13103       /* Don't include clones in the member list.  */
13104       if (DECL_ABSTRACT_ORIGIN (member))
13105         continue;
13106
13107       child = lookup_decl_die (member);
13108       if (child)
13109         splice_child_die (context_die, child);
13110       else
13111         gen_decl_die (member, context_die);
13112     }
13113 }
13114
13115 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13116    is set, we pretend that the type was never defined, so we only get the
13117    member DIEs needed by later specification DIEs.  */
13118
13119 static void
13120 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13121                                 enum debug_info_usage usage)
13122 {
13123   dw_die_ref type_die = lookup_type_die (type);
13124   dw_die_ref scope_die = 0;
13125   int nested = 0;
13126   int complete = (TYPE_SIZE (type)
13127                   && (! TYPE_STUB_DECL (type)
13128                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13129   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13130   complete = complete && should_emit_struct_debug (type, usage);
13131
13132   if (type_die && ! complete)
13133     return;
13134
13135   if (TYPE_CONTEXT (type) != NULL_TREE
13136       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13137           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13138     nested = 1;
13139
13140   scope_die = scope_die_for (type, context_die);
13141
13142   if (! type_die || (nested && scope_die == comp_unit_die))
13143     /* First occurrence of type or toplevel definition of nested class.  */
13144     {
13145       dw_die_ref old_die = type_die;
13146
13147       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13148                           ? record_type_tag (type) : DW_TAG_union_type,
13149                           scope_die, type);
13150       equate_type_number_to_die (type, type_die);
13151       if (old_die)
13152         add_AT_specification (type_die, old_die);
13153       else
13154         add_name_attribute (type_die, type_tag (type));
13155     }
13156   else
13157     remove_AT (type_die, DW_AT_declaration);
13158
13159   /* If this type has been completed, then give it a byte_size attribute and
13160      then give a list of members.  */
13161   if (complete && !ns_decl)
13162     {
13163       /* Prevent infinite recursion in cases where the type of some member of
13164          this type is expressed in terms of this type itself.  */
13165       TREE_ASM_WRITTEN (type) = 1;
13166       add_byte_size_attribute (type_die, type);
13167       if (TYPE_STUB_DECL (type) != NULL_TREE)
13168         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13169
13170       /* If the first reference to this type was as the return type of an
13171          inline function, then it may not have a parent.  Fix this now.  */
13172       if (type_die->die_parent == NULL)
13173         add_child_die (scope_die, type_die);
13174
13175       push_decl_scope (type);
13176       gen_member_die (type, type_die);
13177       pop_decl_scope ();
13178
13179       /* GNU extension: Record what type our vtable lives in.  */
13180       if (TYPE_VFIELD (type))
13181         {
13182           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13183
13184           gen_type_die (vtype, context_die);
13185           add_AT_die_ref (type_die, DW_AT_containing_type,
13186                           lookup_type_die (vtype));
13187         }
13188     }
13189   else
13190     {
13191       add_AT_flag (type_die, DW_AT_declaration, 1);
13192
13193       /* We don't need to do this for function-local types.  */
13194       if (TYPE_STUB_DECL (type)
13195           && ! decl_function_context (TYPE_STUB_DECL (type)))
13196         VEC_safe_push (tree, gc, incomplete_types, type);
13197     }
13198
13199   if (get_AT (type_die, DW_AT_name))
13200     add_pubtype (type, type_die);
13201 }
13202
13203 /* Generate a DIE for a subroutine _type_.  */
13204
13205 static void
13206 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13207 {
13208   tree return_type = TREE_TYPE (type);
13209   dw_die_ref subr_die
13210     = new_die (DW_TAG_subroutine_type,
13211                scope_die_for (type, context_die), type);
13212
13213   equate_type_number_to_die (type, subr_die);
13214   add_prototyped_attribute (subr_die, type);
13215   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13216   gen_formal_types_die (type, subr_die);
13217
13218   if (get_AT (subr_die, DW_AT_name))
13219     add_pubtype (type, subr_die);
13220 }
13221
13222 /* Generate a DIE for a type definition.  */
13223
13224 static void
13225 gen_typedef_die (tree decl, dw_die_ref context_die)
13226 {
13227   dw_die_ref type_die;
13228   tree origin;
13229
13230   if (TREE_ASM_WRITTEN (decl))
13231     return;
13232
13233   TREE_ASM_WRITTEN (decl) = 1;
13234   type_die = new_die (DW_TAG_typedef, context_die, decl);
13235   origin = decl_ultimate_origin (decl);
13236   if (origin != NULL)
13237     add_abstract_origin_attribute (type_die, origin);
13238   else
13239     {
13240       tree type;
13241
13242       add_name_and_src_coords_attributes (type_die, decl);
13243       if (DECL_ORIGINAL_TYPE (decl))
13244         {
13245           type = DECL_ORIGINAL_TYPE (decl);
13246
13247           gcc_assert (type != TREE_TYPE (decl));
13248           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13249         }
13250       else
13251         type = TREE_TYPE (decl);
13252
13253       add_type_attribute (type_die, type, TREE_READONLY (decl),
13254                           TREE_THIS_VOLATILE (decl), context_die);
13255     }
13256
13257   if (DECL_ABSTRACT (decl))
13258     equate_decl_number_to_die (decl, type_die);
13259
13260   if (get_AT (type_die, DW_AT_name))
13261     add_pubtype (decl, type_die);
13262 }
13263
13264 /* Generate a type description DIE.  */
13265
13266 static void
13267 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13268                                 enum debug_info_usage usage)
13269 {
13270   int need_pop;
13271   struct array_descr_info info;
13272
13273   if (type == NULL_TREE || type == error_mark_node)
13274     return;
13275
13276   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13277       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13278     {
13279       if (TREE_ASM_WRITTEN (type))
13280         return;
13281
13282       /* Prevent broken recursion; we can't hand off to the same type.  */
13283       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13284
13285       TREE_ASM_WRITTEN (type) = 1;
13286       gen_decl_die (TYPE_NAME (type), context_die);
13287       return;
13288     }
13289
13290   /* If this is an array type with hidden descriptor, handle it first.  */
13291   if (!TREE_ASM_WRITTEN (type)
13292       && lang_hooks.types.get_array_descr_info
13293       && lang_hooks.types.get_array_descr_info (type, &info))
13294     {
13295       gen_descr_array_type_die (type, &info, context_die);
13296       TREE_ASM_WRITTEN (type) = 1;
13297       return;
13298     }
13299
13300   /* We are going to output a DIE to represent the unqualified version
13301      of this type (i.e. without any const or volatile qualifiers) so
13302      get the main variant (i.e. the unqualified version) of this type
13303      now.  (Vectors are special because the debugging info is in the
13304      cloned type itself).  */
13305   if (TREE_CODE (type) != VECTOR_TYPE)
13306     type = type_main_variant (type);
13307
13308   if (TREE_ASM_WRITTEN (type))
13309     return;
13310
13311   switch (TREE_CODE (type))
13312     {
13313     case ERROR_MARK:
13314       break;
13315
13316     case POINTER_TYPE:
13317     case REFERENCE_TYPE:
13318       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13319          ensures that the gen_type_die recursion will terminate even if the
13320          type is recursive.  Recursive types are possible in Ada.  */
13321       /* ??? We could perhaps do this for all types before the switch
13322          statement.  */
13323       TREE_ASM_WRITTEN (type) = 1;
13324
13325       /* For these types, all that is required is that we output a DIE (or a
13326          set of DIEs) to represent the "basis" type.  */
13327       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13328                                 DINFO_USAGE_IND_USE);
13329       break;
13330
13331     case OFFSET_TYPE:
13332       /* This code is used for C++ pointer-to-data-member types.
13333          Output a description of the relevant class type.  */
13334       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13335                                         DINFO_USAGE_IND_USE);
13336
13337       /* Output a description of the type of the object pointed to.  */
13338       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13339                                         DINFO_USAGE_IND_USE);
13340
13341       /* Now output a DIE to represent this pointer-to-data-member type
13342          itself.  */
13343       gen_ptr_to_mbr_type_die (type, context_die);
13344       break;
13345
13346     case FUNCTION_TYPE:
13347       /* Force out return type (in case it wasn't forced out already).  */
13348       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13349                                         DINFO_USAGE_DIR_USE);
13350       gen_subroutine_type_die (type, context_die);
13351       break;
13352
13353     case METHOD_TYPE:
13354       /* Force out return type (in case it wasn't forced out already).  */
13355       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13356                                         DINFO_USAGE_DIR_USE);
13357       gen_subroutine_type_die (type, context_die);
13358       break;
13359
13360     case ARRAY_TYPE:
13361       gen_array_type_die (type, context_die);
13362       break;
13363
13364     case VECTOR_TYPE:
13365       gen_array_type_die (type, context_die);
13366       break;
13367
13368     case ENUMERAL_TYPE:
13369     case RECORD_TYPE:
13370     case UNION_TYPE:
13371     case QUAL_UNION_TYPE:
13372       /* If this is a nested type whose containing class hasn't been written
13373          out yet, writing it out will cover this one, too.  This does not apply
13374          to instantiations of member class templates; they need to be added to
13375          the containing class as they are generated.  FIXME: This hurts the
13376          idea of combining type decls from multiple TUs, since we can't predict
13377          what set of template instantiations we'll get.  */
13378       if (TYPE_CONTEXT (type)
13379           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13380           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13381         {
13382           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13383
13384           if (TREE_ASM_WRITTEN (type))
13385             return;
13386
13387           /* If that failed, attach ourselves to the stub.  */
13388           push_decl_scope (TYPE_CONTEXT (type));
13389           context_die = lookup_type_die (TYPE_CONTEXT (type));
13390           need_pop = 1;
13391         }
13392       else
13393         {
13394           declare_in_namespace (type, context_die);
13395           need_pop = 0;
13396         }
13397
13398       if (TREE_CODE (type) == ENUMERAL_TYPE)
13399         {
13400           /* This might have been written out by the call to
13401              declare_in_namespace.  */
13402           if (!TREE_ASM_WRITTEN (type))
13403             gen_enumeration_type_die (type, context_die);
13404         }
13405       else
13406         gen_struct_or_union_type_die (type, context_die, usage);
13407
13408       if (need_pop)
13409         pop_decl_scope ();
13410
13411       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13412          it up if it is ever completed.  gen_*_type_die will set it for us
13413          when appropriate.  */
13414       return;
13415
13416     case VOID_TYPE:
13417     case INTEGER_TYPE:
13418     case REAL_TYPE:
13419     case FIXED_POINT_TYPE:
13420     case COMPLEX_TYPE:
13421     case BOOLEAN_TYPE:
13422       /* No DIEs needed for fundamental types.  */
13423       break;
13424
13425     case LANG_TYPE:
13426       /* No Dwarf representation currently defined.  */
13427       break;
13428
13429     default:
13430       gcc_unreachable ();
13431     }
13432
13433   TREE_ASM_WRITTEN (type) = 1;
13434 }
13435
13436 static void
13437 gen_type_die (tree type, dw_die_ref context_die)
13438 {
13439   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13440 }
13441
13442 /* Generate a DIE for a tagged type instantiation.  */
13443
13444 static void
13445 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13446 {
13447   if (type == NULL_TREE || type == error_mark_node)
13448     return;
13449
13450   /* We are going to output a DIE to represent the unqualified version of
13451      this type (i.e. without any const or volatile qualifiers) so make sure
13452      that we have the main variant (i.e. the unqualified version) of this
13453      type now.  */
13454   gcc_assert (type == type_main_variant (type));
13455
13456   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13457      an instance of an unresolved type.  */
13458
13459   switch (TREE_CODE (type))
13460     {
13461     case ERROR_MARK:
13462       break;
13463
13464     case ENUMERAL_TYPE:
13465       gen_inlined_enumeration_type_die (type, context_die);
13466       break;
13467
13468     case RECORD_TYPE:
13469       gen_inlined_structure_type_die (type, context_die);
13470       break;
13471
13472     case UNION_TYPE:
13473     case QUAL_UNION_TYPE:
13474       gen_inlined_union_type_die (type, context_die);
13475       break;
13476
13477     default:
13478       gcc_unreachable ();
13479     }
13480 }
13481
13482 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13483    things which are local to the given block.  */
13484
13485 static void
13486 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13487 {
13488   int must_output_die = 0;
13489   tree origin;
13490   tree decl;
13491   enum tree_code origin_code;
13492
13493   /* Ignore blocks that are NULL.  */
13494   if (stmt == NULL_TREE)
13495     return;
13496
13497   /* If the block is one fragment of a non-contiguous block, do not
13498      process the variables, since they will have been done by the
13499      origin block.  Do process subblocks.  */
13500   if (BLOCK_FRAGMENT_ORIGIN (stmt))
13501     {
13502       tree sub;
13503
13504       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13505         gen_block_die (sub, context_die, depth + 1);
13506
13507       return;
13508     }
13509
13510   /* Determine the "ultimate origin" of this block.  This block may be an
13511      inlined instance of an inlined instance of inline function, so we have
13512      to trace all of the way back through the origin chain to find out what
13513      sort of node actually served as the original seed for the creation of
13514      the current block.  */
13515   origin = block_ultimate_origin (stmt);
13516   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13517
13518   /* Determine if we need to output any Dwarf DIEs at all to represent this
13519      block.  */
13520   if (origin_code == FUNCTION_DECL)
13521     /* The outer scopes for inlinings *must* always be represented.  We
13522        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13523     must_output_die = 1;
13524   else
13525     {
13526       /* In the case where the current block represents an inlining of the
13527          "body block" of an inline function, we must *NOT* output any DIE for
13528          this block because we have already output a DIE to represent the whole
13529          inlined function scope and the "body block" of any function doesn't
13530          really represent a different scope according to ANSI C rules.  So we
13531          check here to make sure that this block does not represent a "body
13532          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13533       if (! is_body_block (origin ? origin : stmt))
13534         {
13535           /* Determine if this block directly contains any "significant"
13536              local declarations which we will need to output DIEs for.  */
13537           if (debug_info_level > DINFO_LEVEL_TERSE)
13538             /* We are not in terse mode so *any* local declaration counts
13539                as being a "significant" one.  */
13540             must_output_die = (BLOCK_VARS (stmt) != NULL
13541                                && (TREE_USED (stmt)
13542                                    || TREE_ASM_WRITTEN (stmt)
13543                                    || BLOCK_ABSTRACT (stmt)));
13544           else
13545             /* We are in terse mode, so only local (nested) function
13546                definitions count as "significant" local declarations.  */
13547             for (decl = BLOCK_VARS (stmt);
13548                  decl != NULL; decl = TREE_CHAIN (decl))
13549               if (TREE_CODE (decl) == FUNCTION_DECL
13550                   && DECL_INITIAL (decl))
13551                 {
13552                   must_output_die = 1;
13553                   break;
13554                 }
13555         }
13556     }
13557
13558   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13559      DIE for any block which contains no significant local declarations at
13560      all.  Rather, in such cases we just call `decls_for_scope' so that any
13561      needed Dwarf info for any sub-blocks will get properly generated. Note
13562      that in terse mode, our definition of what constitutes a "significant"
13563      local declaration gets restricted to include only inlined function
13564      instances and local (nested) function definitions.  */
13565   if (must_output_die)
13566     {
13567       if (origin_code == FUNCTION_DECL)
13568         gen_inlined_subroutine_die (stmt, context_die, depth);
13569       else
13570         gen_lexical_block_die (stmt, context_die, depth);
13571     }
13572   else
13573     decls_for_scope (stmt, context_die, depth);
13574 }
13575
13576 /* Generate all of the decls declared within a given scope and (recursively)
13577    all of its sub-blocks.  */
13578
13579 static void
13580 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13581 {
13582   tree decl;
13583   tree subblocks;
13584
13585   /* Ignore NULL blocks.  */
13586   if (stmt == NULL_TREE)
13587     return;
13588
13589   if (TREE_USED (stmt))
13590     {
13591       /* Output the DIEs to represent all of the data objects and typedefs
13592          declared directly within this block but not within any nested
13593          sub-blocks.  Also, nested function and tag DIEs have been
13594          generated with a parent of NULL; fix that up now.  */
13595       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13596         {
13597           dw_die_ref die;
13598
13599           if (TREE_CODE (decl) == FUNCTION_DECL)
13600             die = lookup_decl_die (decl);
13601           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13602             die = lookup_type_die (TREE_TYPE (decl));
13603           else
13604             die = NULL;
13605
13606           if (die != NULL && die->die_parent == NULL)
13607             add_child_die (context_die, die);
13608           /* Do not produce debug information for static variables since
13609              these might be optimized out.  We are called for these later
13610              in varpool_analyze_pending_decls. */
13611           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13612             ;
13613           else
13614             gen_decl_die (decl, context_die);
13615         }
13616     }
13617
13618   /* If we're at -g1, we're not interested in subblocks.  */
13619   if (debug_info_level <= DINFO_LEVEL_TERSE)
13620     return;
13621
13622   /* Output the DIEs to represent all sub-blocks (and the items declared
13623      therein) of this block.  */
13624   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13625        subblocks != NULL;
13626        subblocks = BLOCK_CHAIN (subblocks))
13627     gen_block_die (subblocks, context_die, depth + 1);
13628 }
13629
13630 /* Is this a typedef we can avoid emitting?  */
13631
13632 static inline int
13633 is_redundant_typedef (const_tree decl)
13634 {
13635   if (TYPE_DECL_IS_STUB (decl))
13636     return 1;
13637
13638   if (DECL_ARTIFICIAL (decl)
13639       && DECL_CONTEXT (decl)
13640       && is_tagged_type (DECL_CONTEXT (decl))
13641       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13642       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13643     /* Also ignore the artificial member typedef for the class name.  */
13644     return 1;
13645
13646   return 0;
13647 }
13648
13649 /* Returns the DIE for decl.  A DIE will always be returned.  */
13650
13651 static dw_die_ref
13652 force_decl_die (tree decl)
13653 {
13654   dw_die_ref decl_die;
13655   unsigned saved_external_flag;
13656   tree save_fn = NULL_TREE;
13657   decl_die = lookup_decl_die (decl);
13658   if (!decl_die)
13659     {
13660       dw_die_ref context_die;
13661       tree decl_context = DECL_CONTEXT (decl);
13662       if (decl_context)
13663         {
13664           /* Find die that represents this context.  */
13665           if (TYPE_P (decl_context))
13666             context_die = force_type_die (decl_context);
13667           else
13668             context_die = force_decl_die (decl_context);
13669         }
13670       else
13671         context_die = comp_unit_die;
13672
13673       decl_die = lookup_decl_die (decl);
13674       if (decl_die)
13675         return decl_die;
13676
13677       switch (TREE_CODE (decl))
13678         {
13679         case FUNCTION_DECL:
13680           /* Clear current_function_decl, so that gen_subprogram_die thinks
13681              that this is a declaration. At this point, we just want to force
13682              declaration die.  */
13683           save_fn = current_function_decl;
13684           current_function_decl = NULL_TREE;
13685           gen_subprogram_die (decl, context_die);
13686           current_function_decl = save_fn;
13687           break;
13688
13689         case VAR_DECL:
13690           /* Set external flag to force declaration die. Restore it after
13691            gen_decl_die() call.  */
13692           saved_external_flag = DECL_EXTERNAL (decl);
13693           DECL_EXTERNAL (decl) = 1;
13694           gen_decl_die (decl, context_die);
13695           DECL_EXTERNAL (decl) = saved_external_flag;
13696           break;
13697
13698         case NAMESPACE_DECL:
13699           dwarf2out_decl (decl);
13700           break;
13701
13702         default:
13703           gcc_unreachable ();
13704         }
13705
13706       /* We should be able to find the DIE now.  */
13707       if (!decl_die)
13708         decl_die = lookup_decl_die (decl);
13709       gcc_assert (decl_die);
13710     }
13711
13712   return decl_die;
13713 }
13714
13715 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13716    always returned.  */
13717
13718 static dw_die_ref
13719 force_type_die (tree type)
13720 {
13721   dw_die_ref type_die;
13722
13723   type_die = lookup_type_die (type);
13724   if (!type_die)
13725     {
13726       dw_die_ref context_die;
13727       if (TYPE_CONTEXT (type))
13728         {
13729           if (TYPE_P (TYPE_CONTEXT (type)))
13730             context_die = force_type_die (TYPE_CONTEXT (type));
13731           else
13732             context_die = force_decl_die (TYPE_CONTEXT (type));
13733         }
13734       else
13735         context_die = comp_unit_die;
13736
13737       type_die = lookup_type_die (type);
13738       if (type_die)
13739         return type_die;
13740       gen_type_die (type, context_die);
13741       type_die = lookup_type_die (type);
13742       gcc_assert (type_die);
13743     }
13744   return type_die;
13745 }
13746
13747 /* Force out any required namespaces to be able to output DECL,
13748    and return the new context_die for it, if it's changed.  */
13749
13750 static dw_die_ref
13751 setup_namespace_context (tree thing, dw_die_ref context_die)
13752 {
13753   tree context = (DECL_P (thing)
13754                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13755   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13756     /* Force out the namespace.  */
13757     context_die = force_decl_die (context);
13758
13759   return context_die;
13760 }
13761
13762 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13763    type) within its namespace, if appropriate.
13764
13765    For compatibility with older debuggers, namespace DIEs only contain
13766    declarations; all definitions are emitted at CU scope.  */
13767
13768 static void
13769 declare_in_namespace (tree thing, dw_die_ref context_die)
13770 {
13771   dw_die_ref ns_context;
13772
13773   if (debug_info_level <= DINFO_LEVEL_TERSE)
13774     return;
13775
13776   /* If this decl is from an inlined function, then don't try to emit it in its
13777      namespace, as we will get confused.  It would have already been emitted
13778      when the abstract instance of the inline function was emitted anyways.  */
13779   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13780     return;
13781
13782   ns_context = setup_namespace_context (thing, context_die);
13783
13784   if (ns_context != context_die)
13785     {
13786       if (DECL_P (thing))
13787         gen_decl_die (thing, ns_context);
13788       else
13789         gen_type_die (thing, ns_context);
13790     }
13791 }
13792
13793 /* Generate a DIE for a namespace or namespace alias.  */
13794
13795 static void
13796 gen_namespace_die (tree decl)
13797 {
13798   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13799
13800   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13801      they are an alias of.  */
13802   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13803     {
13804       /* Output a real namespace.  */
13805       dw_die_ref namespace_die
13806         = new_die (DW_TAG_namespace, context_die, decl);
13807       add_name_and_src_coords_attributes (namespace_die, decl);
13808       equate_decl_number_to_die (decl, namespace_die);
13809     }
13810   else
13811     {
13812       /* Output a namespace alias.  */
13813
13814       /* Force out the namespace we are an alias of, if necessary.  */
13815       dw_die_ref origin_die
13816         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13817
13818       /* Now create the namespace alias DIE.  */
13819       dw_die_ref namespace_die
13820         = new_die (DW_TAG_imported_declaration, context_die, decl);
13821       add_name_and_src_coords_attributes (namespace_die, decl);
13822       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13823       equate_decl_number_to_die (decl, namespace_die);
13824     }
13825 }
13826
13827 /* Generate Dwarf debug information for a decl described by DECL.  */
13828
13829 static void
13830 gen_decl_die (tree decl, dw_die_ref context_die)
13831 {
13832   tree origin;
13833
13834   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13835     return;
13836
13837   switch (TREE_CODE (decl))
13838     {
13839     case ERROR_MARK:
13840       break;
13841
13842     case CONST_DECL:
13843       /* The individual enumerators of an enum type get output when we output
13844          the Dwarf representation of the relevant enum type itself.  */
13845       break;
13846
13847     case FUNCTION_DECL:
13848       /* Don't output any DIEs to represent mere function declarations,
13849          unless they are class members or explicit block externs.  */
13850       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13851           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13852         break;
13853
13854 #if 0
13855       /* FIXME */
13856       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13857          on local redeclarations of global functions.  That seems broken.  */
13858       if (current_function_decl != decl)
13859         /* This is only a declaration.  */;
13860 #endif
13861
13862       /* If we're emitting a clone, emit info for the abstract instance.  */
13863       if (DECL_ORIGIN (decl) != decl)
13864         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13865
13866       /* If we're emitting an out-of-line copy of an inline function,
13867          emit info for the abstract instance and set up to refer to it.  */
13868       else if (cgraph_function_possibly_inlined_p (decl)
13869                && ! DECL_ABSTRACT (decl)
13870                && ! class_or_namespace_scope_p (context_die)
13871                /* dwarf2out_abstract_function won't emit a die if this is just
13872                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13873                   that case, because that works only if we have a die.  */
13874                && DECL_INITIAL (decl) != NULL_TREE)
13875         {
13876           dwarf2out_abstract_function (decl);
13877           set_decl_origin_self (decl);
13878         }
13879
13880       /* Otherwise we're emitting the primary DIE for this decl.  */
13881       else if (debug_info_level > DINFO_LEVEL_TERSE)
13882         {
13883           /* Before we describe the FUNCTION_DECL itself, make sure that we
13884              have described its return type.  */
13885           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13886
13887           /* And its virtual context.  */
13888           if (DECL_VINDEX (decl) != NULL_TREE)
13889             gen_type_die (DECL_CONTEXT (decl), context_die);
13890
13891           /* And its containing type.  */
13892           origin = decl_class_context (decl);
13893           if (origin != NULL_TREE)
13894             gen_type_die_for_member (origin, decl, context_die);
13895
13896           /* And its containing namespace.  */
13897           declare_in_namespace (decl, context_die);
13898         }
13899
13900       /* Now output a DIE to represent the function itself.  */
13901       gen_subprogram_die (decl, context_die);
13902       break;
13903
13904     case TYPE_DECL:
13905       /* If we are in terse mode, don't generate any DIEs to represent any
13906          actual typedefs.  */
13907       if (debug_info_level <= DINFO_LEVEL_TERSE)
13908         break;
13909
13910       /* In the special case of a TYPE_DECL node representing the declaration
13911          of some type tag, if the given TYPE_DECL is marked as having been
13912          instantiated from some other (original) TYPE_DECL node (e.g. one which
13913          was generated within the original definition of an inline function) we
13914          have to generate a special (abbreviated) DW_TAG_structure_type,
13915          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13916       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13917           && is_tagged_type (TREE_TYPE (decl)))
13918         {
13919           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13920           break;
13921         }
13922
13923       if (is_redundant_typedef (decl))
13924         gen_type_die (TREE_TYPE (decl), context_die);
13925       else
13926         /* Output a DIE to represent the typedef itself.  */
13927         gen_typedef_die (decl, context_die);
13928       break;
13929
13930     case LABEL_DECL:
13931       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13932         gen_label_die (decl, context_die);
13933       break;
13934
13935     case VAR_DECL:
13936     case RESULT_DECL:
13937       /* If we are in terse mode, don't generate any DIEs to represent any
13938          variable declarations or definitions.  */
13939       if (debug_info_level <= DINFO_LEVEL_TERSE)
13940         break;
13941
13942       /* Output any DIEs that are needed to specify the type of this data
13943          object.  */
13944       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
13945         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13946       else
13947         gen_type_die (TREE_TYPE (decl), context_die);
13948
13949       /* And its containing type.  */
13950       origin = decl_class_context (decl);
13951       if (origin != NULL_TREE)
13952         gen_type_die_for_member (origin, decl, context_die);
13953
13954       /* And its containing namespace.  */
13955       declare_in_namespace (decl, context_die);
13956
13957       /* Now output the DIE to represent the data object itself.  This gets
13958          complicated because of the possibility that the VAR_DECL really
13959          represents an inlined instance of a formal parameter for an inline
13960          function.  */
13961       origin = decl_ultimate_origin (decl);
13962       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13963         gen_formal_parameter_die (decl, context_die);
13964       else
13965         gen_variable_die (decl, context_die);
13966       break;
13967
13968     case FIELD_DECL:
13969       /* Ignore the nameless fields that are used to skip bits but handle C++
13970          anonymous unions and structs.  */
13971       if (DECL_NAME (decl) != NULL_TREE
13972           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13973           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13974         {
13975           gen_type_die (member_declared_type (decl), context_die);
13976           gen_field_die (decl, context_die);
13977         }
13978       break;
13979
13980     case PARM_DECL:
13981       if (DECL_BY_REFERENCE (decl))
13982         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13983       else
13984         gen_type_die (TREE_TYPE (decl), context_die);
13985       gen_formal_parameter_die (decl, context_die);
13986       break;
13987
13988     case NAMESPACE_DECL:
13989       gen_namespace_die (decl);
13990       break;
13991
13992     default:
13993       /* Probably some frontend-internal decl.  Assume we don't care.  */
13994       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13995       break;
13996     }
13997 }
13998 \f
13999 /* Output debug information for global decl DECL.  Called from toplev.c after
14000    compilation proper has finished.  */
14001
14002 static void
14003 dwarf2out_global_decl (tree decl)
14004 {
14005   /* Output DWARF2 information for file-scope tentative data object
14006      declarations, file-scope (extern) function declarations (which had no
14007      corresponding body) and file-scope tagged type declarations and
14008      definitions which have not yet been forced out.  */
14009   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14010     dwarf2out_decl (decl);
14011 }
14012
14013 /* Output debug information for type decl DECL.  Called from toplev.c
14014    and from language front ends (to record built-in types).  */
14015 static void
14016 dwarf2out_type_decl (tree decl, int local)
14017 {
14018   if (!local)
14019     dwarf2out_decl (decl);
14020 }
14021
14022 /* Output debug information for imported module or decl.  */
14023
14024 static void
14025 dwarf2out_imported_module_or_decl (tree decl, tree context)
14026 {
14027   dw_die_ref imported_die, at_import_die;
14028   dw_die_ref scope_die;
14029   expanded_location xloc;
14030
14031   if (debug_info_level <= DINFO_LEVEL_TERSE)
14032     return;
14033
14034   gcc_assert (decl);
14035
14036   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14037      We need decl DIE for reference and scope die. First, get DIE for the decl
14038      itself.  */
14039
14040   /* Get the scope die for decl context. Use comp_unit_die for global module
14041      or decl. If die is not found for non globals, force new die.  */
14042   if (!context)
14043     scope_die = comp_unit_die;
14044   else if (TYPE_P (context))
14045     {
14046       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14047         return;
14048     scope_die = force_type_die (context);
14049     }
14050   else
14051     scope_die = force_decl_die (context);
14052
14053   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14054   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14055     {
14056       if (is_base_type (TREE_TYPE (decl)))
14057         at_import_die = base_type_die (TREE_TYPE (decl));
14058       else
14059         at_import_die = force_type_die (TREE_TYPE (decl));
14060     }
14061   else
14062     {
14063       at_import_die = lookup_decl_die (decl);
14064       if (!at_import_die)
14065         {
14066           /* If we're trying to avoid duplicate debug info, we may not have
14067              emitted the member decl for this field.  Emit it now.  */
14068           if (TREE_CODE (decl) == FIELD_DECL)
14069             {
14070               tree type = DECL_CONTEXT (decl);
14071               dw_die_ref type_context_die;
14072
14073               if (TYPE_CONTEXT (type))
14074                 if (TYPE_P (TYPE_CONTEXT (type)))
14075                   {
14076                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14077                                                    DINFO_USAGE_DIR_USE))
14078                       return;
14079                   type_context_die = force_type_die (TYPE_CONTEXT (type));
14080                   }
14081               else
14082                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14083               else
14084                 type_context_die = comp_unit_die;
14085               gen_type_die_for_member (type, decl, type_context_die);
14086             }
14087           at_import_die = force_decl_die (decl);
14088         }
14089     }
14090
14091   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14092   if (TREE_CODE (decl) == NAMESPACE_DECL)
14093     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14094   else
14095     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14096
14097   xloc = expand_location (input_location);
14098   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14099   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14100   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14101 }
14102
14103 /* Write the debugging output for DECL.  */
14104
14105 void
14106 dwarf2out_decl (tree decl)
14107 {
14108   dw_die_ref context_die = comp_unit_die;
14109
14110   switch (TREE_CODE (decl))
14111     {
14112     case ERROR_MARK:
14113       return;
14114
14115     case FUNCTION_DECL:
14116       /* What we would really like to do here is to filter out all mere
14117          file-scope declarations of file-scope functions which are never
14118          referenced later within this translation unit (and keep all of ones
14119          that *are* referenced later on) but we aren't clairvoyant, so we have
14120          no idea which functions will be referenced in the future (i.e. later
14121          on within the current translation unit). So here we just ignore all
14122          file-scope function declarations which are not also definitions.  If
14123          and when the debugger needs to know something about these functions,
14124          it will have to hunt around and find the DWARF information associated
14125          with the definition of the function.
14126
14127          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14128          nodes represent definitions and which ones represent mere
14129          declarations.  We have to check DECL_INITIAL instead. That's because
14130          the C front-end supports some weird semantics for "extern inline"
14131          function definitions.  These can get inlined within the current
14132          translation unit (and thus, we need to generate Dwarf info for their
14133          abstract instances so that the Dwarf info for the concrete inlined
14134          instances can have something to refer to) but the compiler never
14135          generates any out-of-lines instances of such things (despite the fact
14136          that they *are* definitions).
14137
14138          The important point is that the C front-end marks these "extern
14139          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14140          them anyway. Note that the C++ front-end also plays some similar games
14141          for inline function definitions appearing within include files which
14142          also contain `#pragma interface' pragmas.  */
14143       if (DECL_INITIAL (decl) == NULL_TREE)
14144         return;
14145
14146       /* If we're a nested function, initially use a parent of NULL; if we're
14147          a plain function, this will be fixed up in decls_for_scope.  If
14148          we're a method, it will be ignored, since we already have a DIE.  */
14149       if (decl_function_context (decl)
14150           /* But if we're in terse mode, we don't care about scope.  */
14151           && debug_info_level > DINFO_LEVEL_TERSE)
14152         context_die = NULL;
14153       break;
14154
14155     case VAR_DECL:
14156       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14157          declaration and if the declaration was never even referenced from
14158          within this entire compilation unit.  We suppress these DIEs in
14159          order to save space in the .debug section (by eliminating entries
14160          which are probably useless).  Note that we must not suppress
14161          block-local extern declarations (whether used or not) because that
14162          would screw-up the debugger's name lookup mechanism and cause it to
14163          miss things which really ought to be in scope at a given point.  */
14164       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14165         return;
14166
14167       /* For local statics lookup proper context die.  */
14168       if (TREE_STATIC (decl) && decl_function_context (decl))
14169         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14170
14171       /* If we are in terse mode, don't generate any DIEs to represent any
14172          variable declarations or definitions.  */
14173       if (debug_info_level <= DINFO_LEVEL_TERSE)
14174         return;
14175       break;
14176
14177     case NAMESPACE_DECL:
14178       if (debug_info_level <= DINFO_LEVEL_TERSE)
14179         return;
14180       if (lookup_decl_die (decl) != NULL)
14181         return;
14182       break;
14183
14184     case TYPE_DECL:
14185       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14186       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14187         return;
14188
14189       /* Don't bother trying to generate any DIEs to represent any of the
14190          normal built-in types for the language we are compiling.  */
14191       if (DECL_IS_BUILTIN (decl))
14192         {
14193           /* OK, we need to generate one for `bool' so GDB knows what type
14194              comparisons have.  */
14195           if (is_cxx ()
14196               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14197               && ! DECL_IGNORED_P (decl))
14198             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14199
14200           return;
14201         }
14202
14203       /* If we are in terse mode, don't generate any DIEs for types.  */
14204       if (debug_info_level <= DINFO_LEVEL_TERSE)
14205         return;
14206
14207       /* If we're a function-scope tag, initially use a parent of NULL;
14208          this will be fixed up in decls_for_scope.  */
14209       if (decl_function_context (decl))
14210         context_die = NULL;
14211
14212       break;
14213
14214     default:
14215       return;
14216     }
14217
14218   gen_decl_die (decl, context_die);
14219 }
14220
14221 /* Output a marker (i.e. a label) for the beginning of the generated code for
14222    a lexical block.  */
14223
14224 static void
14225 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14226                        unsigned int blocknum)
14227 {
14228   switch_to_section (current_function_section ());
14229   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14230 }
14231
14232 /* Output a marker (i.e. a label) for the end of the generated code for a
14233    lexical block.  */
14234
14235 static void
14236 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14237 {
14238   switch_to_section (current_function_section ());
14239   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14240 }
14241
14242 /* Returns nonzero if it is appropriate not to emit any debugging
14243    information for BLOCK, because it doesn't contain any instructions.
14244
14245    Don't allow this for blocks with nested functions or local classes
14246    as we would end up with orphans, and in the presence of scheduling
14247    we may end up calling them anyway.  */
14248
14249 static bool
14250 dwarf2out_ignore_block (const_tree block)
14251 {
14252   tree decl;
14253
14254   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14255     if (TREE_CODE (decl) == FUNCTION_DECL
14256         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14257       return 0;
14258
14259   return 1;
14260 }
14261
14262 /* Hash table routines for file_hash.  */
14263
14264 static int
14265 file_table_eq (const void *p1_p, const void *p2_p)
14266 {
14267   const struct dwarf_file_data * p1 = p1_p;
14268   const char * p2 = p2_p;
14269   return strcmp (p1->filename, p2) == 0;
14270 }
14271
14272 static hashval_t
14273 file_table_hash (const void *p_p)
14274 {
14275   const struct dwarf_file_data * p = p_p;
14276   return htab_hash_string (p->filename);
14277 }
14278
14279 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14280    dwarf2out.c) and return its "index".  The index of each (known) filename is
14281    just a unique number which is associated with only that one filename.  We
14282    need such numbers for the sake of generating labels (in the .debug_sfnames
14283    section) and references to those files numbers (in the .debug_srcinfo
14284    and.debug_macinfo sections).  If the filename given as an argument is not
14285    found in our current list, add it to the list and assign it the next
14286    available unique index number.  In order to speed up searches, we remember
14287    the index of the filename was looked up last.  This handles the majority of
14288    all searches.  */
14289
14290 static struct dwarf_file_data *
14291 lookup_filename (const char *file_name)
14292 {
14293   void ** slot;
14294   struct dwarf_file_data * created;
14295
14296   /* Check to see if the file name that was searched on the previous
14297      call matches this file name.  If so, return the index.  */
14298   if (file_table_last_lookup
14299       && (file_name == file_table_last_lookup->filename
14300           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14301     return file_table_last_lookup;
14302
14303   /* Didn't match the previous lookup, search the table.  */
14304   slot = htab_find_slot_with_hash (file_table, file_name,
14305                                    htab_hash_string (file_name), INSERT);
14306   if (*slot)
14307     return *slot;
14308
14309   created = ggc_alloc (sizeof (struct dwarf_file_data));
14310   created->filename = file_name;
14311   created->emitted_number = 0;
14312   *slot = created;
14313   return created;
14314 }
14315
14316 /* If the assembler will construct the file table, then translate the compiler
14317    internal file table number into the assembler file table number, and emit
14318    a .file directive if we haven't already emitted one yet.  The file table
14319    numbers are different because we prune debug info for unused variables and
14320    types, which may include filenames.  */
14321
14322 static int
14323 maybe_emit_file (struct dwarf_file_data * fd)
14324 {
14325   if (! fd->emitted_number)
14326     {
14327       if (last_emitted_file)
14328         fd->emitted_number = last_emitted_file->emitted_number + 1;
14329       else
14330         fd->emitted_number = 1;
14331       last_emitted_file = fd;
14332
14333       if (DWARF2_ASM_LINE_DEBUG_INFO)
14334         {
14335           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14336           output_quoted_string (asm_out_file,
14337                                 remap_debug_filename (fd->filename));
14338           fputc ('\n', asm_out_file);
14339         }
14340     }
14341
14342   return fd->emitted_number;
14343 }
14344
14345 /* Called by the final INSN scan whenever we see a var location.  We
14346    use it to drop labels in the right places, and throw the location in
14347    our lookup table.  */
14348
14349 static void
14350 dwarf2out_var_location (rtx loc_note)
14351 {
14352   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14353   struct var_loc_node *newloc;
14354   rtx prev_insn;
14355   static rtx last_insn;
14356   static const char *last_label;
14357   tree decl;
14358
14359   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14360     return;
14361   prev_insn = PREV_INSN (loc_note);
14362
14363   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14364   /* If the insn we processed last time is the previous insn
14365      and it is also a var location note, use the label we emitted
14366      last time.  */
14367   if (last_insn != NULL_RTX
14368       && last_insn == prev_insn
14369       && NOTE_P (prev_insn)
14370       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14371     {
14372       newloc->label = last_label;
14373     }
14374   else
14375     {
14376       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14377       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14378       loclabel_num++;
14379       newloc->label = ggc_strdup (loclabel);
14380     }
14381   newloc->var_loc_note = loc_note;
14382   newloc->next = NULL;
14383
14384   if (cfun && in_cold_section_p)
14385     newloc->section_label = cfun->cold_section_label;
14386   else
14387     newloc->section_label = text_section_label;
14388
14389   last_insn = loc_note;
14390   last_label = newloc->label;
14391   decl = NOTE_VAR_LOCATION_DECL (loc_note);
14392   add_var_loc_to_decl (decl, newloc);
14393 }
14394
14395 /* We need to reset the locations at the beginning of each
14396    function. We can't do this in the end_function hook, because the
14397    declarations that use the locations won't have been output when
14398    that hook is called.  Also compute have_multiple_function_sections here.  */
14399
14400 static void
14401 dwarf2out_begin_function (tree fun)
14402 {
14403   htab_empty (decl_loc_table);
14404
14405   if (function_section (fun) != text_section)
14406     have_multiple_function_sections = true;
14407
14408   dwarf2out_note_section_used ();
14409 }
14410
14411 /* Output a label to mark the beginning of a source code line entry
14412    and record information relating to this source line, in
14413    'line_info_table' for later output of the .debug_line section.  */
14414
14415 static void
14416 dwarf2out_source_line (unsigned int line, const char *filename)
14417 {
14418   if (debug_info_level >= DINFO_LEVEL_NORMAL
14419       && line != 0)
14420     {
14421       int file_num = maybe_emit_file (lookup_filename (filename));
14422
14423       switch_to_section (current_function_section ());
14424
14425       /* If requested, emit something human-readable.  */
14426       if (flag_debug_asm)
14427         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14428                  filename, line);
14429
14430       if (DWARF2_ASM_LINE_DEBUG_INFO)
14431         {
14432           /* Emit the .loc directive understood by GNU as.  */
14433           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14434
14435           /* Indicate that line number info exists.  */
14436           line_info_table_in_use++;
14437         }
14438       else if (function_section (current_function_decl) != text_section)
14439         {
14440           dw_separate_line_info_ref line_info;
14441           targetm.asm_out.internal_label (asm_out_file,
14442                                           SEPARATE_LINE_CODE_LABEL,
14443                                           separate_line_info_table_in_use);
14444
14445           /* Expand the line info table if necessary.  */
14446           if (separate_line_info_table_in_use
14447               == separate_line_info_table_allocated)
14448             {
14449               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14450               separate_line_info_table
14451                 = ggc_realloc (separate_line_info_table,
14452                                separate_line_info_table_allocated
14453                                * sizeof (dw_separate_line_info_entry));
14454               memset (separate_line_info_table
14455                        + separate_line_info_table_in_use,
14456                       0,
14457                       (LINE_INFO_TABLE_INCREMENT
14458                        * sizeof (dw_separate_line_info_entry)));
14459             }
14460
14461           /* Add the new entry at the end of the line_info_table.  */
14462           line_info
14463             = &separate_line_info_table[separate_line_info_table_in_use++];
14464           line_info->dw_file_num = file_num;
14465           line_info->dw_line_num = line;
14466           line_info->function = current_function_funcdef_no;
14467         }
14468       else
14469         {
14470           dw_line_info_ref line_info;
14471
14472           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14473                                      line_info_table_in_use);
14474
14475           /* Expand the line info table if necessary.  */
14476           if (line_info_table_in_use == line_info_table_allocated)
14477             {
14478               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14479               line_info_table
14480                 = ggc_realloc (line_info_table,
14481                                (line_info_table_allocated
14482                                 * sizeof (dw_line_info_entry)));
14483               memset (line_info_table + line_info_table_in_use, 0,
14484                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14485             }
14486
14487           /* Add the new entry at the end of the line_info_table.  */
14488           line_info = &line_info_table[line_info_table_in_use++];
14489           line_info->dw_file_num = file_num;
14490           line_info->dw_line_num = line;
14491         }
14492     }
14493 }
14494
14495 /* Record the beginning of a new source file.  */
14496
14497 static void
14498 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14499 {
14500   if (flag_eliminate_dwarf2_dups)
14501     {
14502       /* Record the beginning of the file for break_out_includes.  */
14503       dw_die_ref bincl_die;
14504
14505       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14506       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14507     }
14508
14509   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14510     {
14511       int file_num = maybe_emit_file (lookup_filename (filename));
14512
14513       switch_to_section (debug_macinfo_section);
14514       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14515       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14516                                    lineno);
14517
14518       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14519     }
14520 }
14521
14522 /* Record the end of a source file.  */
14523
14524 static void
14525 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14526 {
14527   if (flag_eliminate_dwarf2_dups)
14528     /* Record the end of the file for break_out_includes.  */
14529     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14530
14531   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14532     {
14533       switch_to_section (debug_macinfo_section);
14534       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14535     }
14536 }
14537
14538 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14539    the tail part of the directive line, i.e. the part which is past the
14540    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14541
14542 static void
14543 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14544                   const char *buffer ATTRIBUTE_UNUSED)
14545 {
14546   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14547     {
14548       switch_to_section (debug_macinfo_section);
14549       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14550       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14551       dw2_asm_output_nstring (buffer, -1, "The macro");
14552     }
14553 }
14554
14555 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14556    the tail part of the directive line, i.e. the part which is past the
14557    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14558
14559 static void
14560 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14561                  const char *buffer ATTRIBUTE_UNUSED)
14562 {
14563   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14564     {
14565       switch_to_section (debug_macinfo_section);
14566       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14567       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14568       dw2_asm_output_nstring (buffer, -1, "The macro");
14569     }
14570 }
14571
14572 /* Set up for Dwarf output at the start of compilation.  */
14573
14574 static void
14575 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14576 {
14577   /* Allocate the file_table.  */
14578   file_table = htab_create_ggc (50, file_table_hash,
14579                                 file_table_eq, NULL);
14580
14581   /* Allocate the decl_die_table.  */
14582   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14583                                     decl_die_table_eq, NULL);
14584
14585   /* Allocate the decl_loc_table.  */
14586   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14587                                     decl_loc_table_eq, NULL);
14588
14589   /* Allocate the initial hunk of the decl_scope_table.  */
14590   decl_scope_table = VEC_alloc (tree, gc, 256);
14591
14592   /* Allocate the initial hunk of the abbrev_die_table.  */
14593   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14594                                         * sizeof (dw_die_ref));
14595   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14596   /* Zero-th entry is allocated, but unused.  */
14597   abbrev_die_table_in_use = 1;
14598
14599   /* Allocate the initial hunk of the line_info_table.  */
14600   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14601                                        * sizeof (dw_line_info_entry));
14602   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14603
14604   /* Zero-th entry is allocated, but unused.  */
14605   line_info_table_in_use = 1;
14606
14607   /* Allocate the pubtypes and pubnames vectors.  */
14608   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14609   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14610
14611   /* Generate the initial DIE for the .debug section.  Note that the (string)
14612      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14613      will (typically) be a relative pathname and that this pathname should be
14614      taken as being relative to the directory from which the compiler was
14615      invoked when the given (base) source file was compiled.  We will fill
14616      in this value in dwarf2out_finish.  */
14617   comp_unit_die = gen_compile_unit_die (NULL);
14618
14619   incomplete_types = VEC_alloc (tree, gc, 64);
14620
14621   used_rtx_array = VEC_alloc (rtx, gc, 32);
14622
14623   debug_info_section = get_section (DEBUG_INFO_SECTION,
14624                                     SECTION_DEBUG, NULL);
14625   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14626                                       SECTION_DEBUG, NULL);
14627   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14628                                        SECTION_DEBUG, NULL);
14629   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14630                                        SECTION_DEBUG, NULL);
14631   debug_line_section = get_section (DEBUG_LINE_SECTION,
14632                                     SECTION_DEBUG, NULL);
14633   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14634                                    SECTION_DEBUG, NULL);
14635   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14636                                         SECTION_DEBUG, NULL);
14637 #ifdef DEBUG_PUBTYPES_SECTION
14638   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14639                                         SECTION_DEBUG, NULL);
14640 #endif
14641   debug_str_section = get_section (DEBUG_STR_SECTION,
14642                                    DEBUG_STR_SECTION_FLAGS, NULL);
14643   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14644                                       SECTION_DEBUG, NULL);
14645   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14646                                      SECTION_DEBUG, NULL);
14647
14648   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14649   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14650                                DEBUG_ABBREV_SECTION_LABEL, 0);
14651   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14652   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14653                                COLD_TEXT_SECTION_LABEL, 0);
14654   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14655
14656   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14657                                DEBUG_INFO_SECTION_LABEL, 0);
14658   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14659                                DEBUG_LINE_SECTION_LABEL, 0);
14660   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14661                                DEBUG_RANGES_SECTION_LABEL, 0);
14662   switch_to_section (debug_abbrev_section);
14663   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14664   switch_to_section (debug_info_section);
14665   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14666   switch_to_section (debug_line_section);
14667   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14668
14669   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14670     {
14671       switch_to_section (debug_macinfo_section);
14672       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14673                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14674       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14675     }
14676
14677   switch_to_section (text_section);
14678   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14679   if (flag_reorder_blocks_and_partition)
14680     {
14681       cold_text_section = unlikely_text_section ();
14682       switch_to_section (cold_text_section);
14683       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14684     }
14685 }
14686
14687 /* A helper function for dwarf2out_finish called through
14688    ht_forall.  Emit one queued .debug_str string.  */
14689
14690 static int
14691 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14692 {
14693   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14694
14695   if (node->form == DW_FORM_strp)
14696     {
14697       switch_to_section (debug_str_section);
14698       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14699       assemble_string (node->str, strlen (node->str) + 1);
14700     }
14701
14702   return 1;
14703 }
14704
14705 #if ENABLE_ASSERT_CHECKING
14706 /* Verify that all marks are clear.  */
14707
14708 static void
14709 verify_marks_clear (dw_die_ref die)
14710 {
14711   dw_die_ref c;
14712
14713   gcc_assert (! die->die_mark);
14714   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14715 }
14716 #endif /* ENABLE_ASSERT_CHECKING */
14717
14718 /* Clear the marks for a die and its children.
14719    Be cool if the mark isn't set.  */
14720
14721 static void
14722 prune_unmark_dies (dw_die_ref die)
14723 {
14724   dw_die_ref c;
14725
14726   if (die->die_mark)
14727     die->die_mark = 0;
14728   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14729 }
14730
14731 /* Given DIE that we're marking as used, find any other dies
14732    it references as attributes and mark them as used.  */
14733
14734 static void
14735 prune_unused_types_walk_attribs (dw_die_ref die)
14736 {
14737   dw_attr_ref a;
14738   unsigned ix;
14739
14740   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14741     {
14742       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14743         {
14744           /* A reference to another DIE.
14745              Make sure that it will get emitted.  */
14746           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14747         }
14748       /* Set the string's refcount to 0 so that prune_unused_types_mark
14749          accounts properly for it.  */
14750       if (AT_class (a) == dw_val_class_str)
14751         a->dw_attr_val.v.val_str->refcount = 0;
14752     }
14753 }
14754
14755
14756 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14757    to DIE's children.  */
14758
14759 static void
14760 prune_unused_types_mark (dw_die_ref die, int dokids)
14761 {
14762   dw_die_ref c;
14763
14764   if (die->die_mark == 0)
14765     {
14766       /* We haven't done this node yet.  Mark it as used.  */
14767       die->die_mark = 1;
14768
14769       /* We also have to mark its parents as used.
14770          (But we don't want to mark our parents' kids due to this.)  */
14771       if (die->die_parent)
14772         prune_unused_types_mark (die->die_parent, 0);
14773
14774       /* Mark any referenced nodes.  */
14775       prune_unused_types_walk_attribs (die);
14776
14777       /* If this node is a specification,
14778          also mark the definition, if it exists.  */
14779       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14780         prune_unused_types_mark (die->die_definition, 1);
14781     }
14782
14783   if (dokids && die->die_mark != 2)
14784     {
14785       /* We need to walk the children, but haven't done so yet.
14786          Remember that we've walked the kids.  */
14787       die->die_mark = 2;
14788
14789       /* If this is an array type, we need to make sure our
14790          kids get marked, even if they're types.  */
14791       if (die->die_tag == DW_TAG_array_type)
14792         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14793       else
14794         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14795     }
14796 }
14797
14798
14799 /* Walk the tree DIE and mark types that we actually use.  */
14800
14801 static void
14802 prune_unused_types_walk (dw_die_ref die)
14803 {
14804   dw_die_ref c;
14805
14806   /* Don't do anything if this node is already marked.  */
14807   if (die->die_mark)
14808     return;
14809
14810   switch (die->die_tag)
14811     {
14812     case DW_TAG_const_type:
14813     case DW_TAG_packed_type:
14814     case DW_TAG_pointer_type:
14815     case DW_TAG_reference_type:
14816     case DW_TAG_volatile_type:
14817     case DW_TAG_typedef:
14818     case DW_TAG_array_type:
14819     case DW_TAG_structure_type:
14820     case DW_TAG_union_type:
14821     case DW_TAG_class_type:
14822     case DW_TAG_interface_type:
14823     case DW_TAG_friend:
14824     case DW_TAG_variant_part:
14825     case DW_TAG_enumeration_type:
14826     case DW_TAG_subroutine_type:
14827     case DW_TAG_string_type:
14828     case DW_TAG_set_type:
14829     case DW_TAG_subrange_type:
14830     case DW_TAG_ptr_to_member_type:
14831     case DW_TAG_file_type:
14832       if (die->die_perennial_p)
14833         break;
14834
14835       /* It's a type node --- don't mark it.  */
14836       return;
14837
14838     default:
14839       /* Mark everything else.  */
14840       break;
14841   }
14842
14843   die->die_mark = 1;
14844
14845   /* Now, mark any dies referenced from here.  */
14846   prune_unused_types_walk_attribs (die);
14847
14848   /* Mark children.  */
14849   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14850 }
14851
14852 /* Increment the string counts on strings referred to from DIE's
14853    attributes.  */
14854
14855 static void
14856 prune_unused_types_update_strings (dw_die_ref die)
14857 {
14858   dw_attr_ref a;
14859   unsigned ix;
14860
14861   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14862     if (AT_class (a) == dw_val_class_str)
14863       {
14864         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14865         s->refcount++;
14866         /* Avoid unnecessarily putting strings that are used less than
14867            twice in the hash table.  */
14868         if (s->refcount
14869             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14870           {
14871             void ** slot;
14872             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14873                                              htab_hash_string (s->str),
14874                                              INSERT);
14875             gcc_assert (*slot == NULL);
14876             *slot = s;
14877           }
14878       }
14879 }
14880
14881 /* Remove from the tree DIE any dies that aren't marked.  */
14882
14883 static void
14884 prune_unused_types_prune (dw_die_ref die)
14885 {
14886   dw_die_ref c;
14887
14888   gcc_assert (die->die_mark);
14889   prune_unused_types_update_strings (die);
14890
14891   if (! die->die_child)
14892     return;
14893
14894   c = die->die_child;
14895   do {
14896     dw_die_ref prev = c;
14897     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14898       if (c == die->die_child)
14899         {
14900           /* No marked children between 'prev' and the end of the list.  */
14901           if (prev == c)
14902             /* No marked children at all.  */
14903             die->die_child = NULL;
14904           else
14905             {
14906               prev->die_sib = c->die_sib;
14907               die->die_child = prev;
14908             }
14909           return;
14910         }
14911
14912     if (c != prev->die_sib)
14913       prev->die_sib = c;
14914     prune_unused_types_prune (c);
14915   } while (c != die->die_child);
14916 }
14917
14918
14919 /* Remove dies representing declarations that we never use.  */
14920
14921 static void
14922 prune_unused_types (void)
14923 {
14924   unsigned int i;
14925   limbo_die_node *node;
14926   pubname_ref pub;
14927
14928 #if ENABLE_ASSERT_CHECKING
14929   /* All the marks should already be clear.  */
14930   verify_marks_clear (comp_unit_die);
14931   for (node = limbo_die_list; node; node = node->next)
14932     verify_marks_clear (node->die);
14933 #endif /* ENABLE_ASSERT_CHECKING */
14934
14935   /* Set the mark on nodes that are actually used.  */
14936   prune_unused_types_walk (comp_unit_die);
14937   for (node = limbo_die_list; node; node = node->next)
14938     prune_unused_types_walk (node->die);
14939
14940   /* Also set the mark on nodes referenced from the
14941      pubname_table or arange_table.  */
14942   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14943     prune_unused_types_mark (pub->die, 1);
14944   for (i = 0; i < arange_table_in_use; i++)
14945     prune_unused_types_mark (arange_table[i], 1);
14946
14947   /* Get rid of nodes that aren't marked; and update the string counts.  */
14948   if (debug_str_hash)
14949     htab_empty (debug_str_hash);
14950   prune_unused_types_prune (comp_unit_die);
14951   for (node = limbo_die_list; node; node = node->next)
14952     prune_unused_types_prune (node->die);
14953
14954   /* Leave the marks clear.  */
14955   prune_unmark_dies (comp_unit_die);
14956   for (node = limbo_die_list; node; node = node->next)
14957     prune_unmark_dies (node->die);
14958 }
14959
14960 /* Set the parameter to true if there are any relative pathnames in
14961    the file table.  */
14962 static int
14963 file_table_relative_p (void ** slot, void *param)
14964 {
14965   bool *p = param;
14966   struct dwarf_file_data *d = *slot;
14967   if (!IS_ABSOLUTE_PATH (d->filename))
14968     {
14969       *p = true;
14970       return 0;
14971     }
14972   return 1;
14973 }
14974
14975 /* Output stuff that dwarf requires at the end of every file,
14976    and generate the DWARF-2 debugging info.  */
14977
14978 static void
14979 dwarf2out_finish (const char *filename)
14980 {
14981   limbo_die_node *node, *next_node;
14982   dw_die_ref die = 0;
14983
14984   /* Add the name for the main input file now.  We delayed this from
14985      dwarf2out_init to avoid complications with PCH.  */
14986   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
14987   if (!IS_ABSOLUTE_PATH (filename))
14988     add_comp_dir_attribute (comp_unit_die);
14989   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14990     {
14991       bool p = false;
14992       htab_traverse (file_table, file_table_relative_p, &p);
14993       if (p)
14994         add_comp_dir_attribute (comp_unit_die);
14995     }
14996
14997   /* Traverse the limbo die list, and add parent/child links.  The only
14998      dies without parents that should be here are concrete instances of
14999      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15000      For concrete instances, we can get the parent die from the abstract
15001      instance.  */
15002   for (node = limbo_die_list; node; node = next_node)
15003     {
15004       next_node = node->next;
15005       die = node->die;
15006
15007       if (die->die_parent == NULL)
15008         {
15009           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15010
15011           if (origin)
15012             add_child_die (origin->die_parent, die);
15013           else if (die == comp_unit_die)
15014             ;
15015           else if (errorcount > 0 || sorrycount > 0)
15016             /* It's OK to be confused by errors in the input.  */
15017             add_child_die (comp_unit_die, die);
15018           else
15019             {
15020               /* In certain situations, the lexical block containing a
15021                  nested function can be optimized away, which results
15022                  in the nested function die being orphaned.  Likewise
15023                  with the return type of that nested function.  Force
15024                  this to be a child of the containing function.
15025
15026                  It may happen that even the containing function got fully
15027                  inlined and optimized out.  In that case we are lost and
15028                  assign the empty child.  This should not be big issue as
15029                  the function is likely unreachable too.  */
15030               tree context = NULL_TREE;
15031
15032               gcc_assert (node->created_for);
15033
15034               if (DECL_P (node->created_for))
15035                 context = DECL_CONTEXT (node->created_for);
15036               else if (TYPE_P (node->created_for))
15037                 context = TYPE_CONTEXT (node->created_for);
15038
15039               gcc_assert (context
15040                           && (TREE_CODE (context) == FUNCTION_DECL
15041                               || TREE_CODE (context) == NAMESPACE_DECL));
15042
15043               origin = lookup_decl_die (context);
15044               if (origin)
15045                 add_child_die (origin, die);
15046               else
15047                 add_child_die (comp_unit_die, die);
15048             }
15049         }
15050     }
15051
15052   limbo_die_list = NULL;
15053
15054   /* Walk through the list of incomplete types again, trying once more to
15055      emit full debugging info for them.  */
15056   retry_incomplete_types ();
15057
15058   if (flag_eliminate_unused_debug_types)
15059     prune_unused_types ();
15060
15061   /* Generate separate CUs for each of the include files we've seen.
15062      They will go into limbo_die_list.  */
15063   if (flag_eliminate_dwarf2_dups)
15064     break_out_includes (comp_unit_die);
15065
15066   /* Traverse the DIE's and add add sibling attributes to those DIE's
15067      that have children.  */
15068   add_sibling_attributes (comp_unit_die);
15069   for (node = limbo_die_list; node; node = node->next)
15070     add_sibling_attributes (node->die);
15071
15072   /* Output a terminator label for the .text section.  */
15073   switch_to_section (text_section);
15074   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15075   if (flag_reorder_blocks_and_partition)
15076     {
15077       switch_to_section (unlikely_text_section ());
15078       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15079     }
15080
15081   /* We can only use the low/high_pc attributes if all of the code was
15082      in .text.  */
15083   if (!have_multiple_function_sections)
15084     {
15085       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15086       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15087     }
15088
15089   else
15090     {
15091       unsigned fde_idx = 0;
15092
15093       /* We need to give .debug_loc and .debug_ranges an appropriate
15094          "base address".  Use zero so that these addresses become
15095          absolute.  Historically, we've emitted the unexpected
15096          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15097          Emit both to give time for other tools to adapt.  */
15098       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15099       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15100
15101       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15102                          add_ranges_by_labels (text_section_label,
15103                                                text_end_label));
15104       if (flag_reorder_blocks_and_partition)
15105         add_ranges_by_labels (cold_text_section_label,
15106                               cold_end_label);
15107
15108       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15109         {
15110           dw_fde_ref fde = &fde_table[fde_idx];
15111
15112           if (fde->dw_fde_switched_sections)
15113             {
15114               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15115                                     fde->dw_fde_hot_section_end_label);
15116               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15117                                     fde->dw_fde_unlikely_section_end_label);
15118             }
15119           else
15120             add_ranges_by_labels (fde->dw_fde_begin,
15121                                   fde->dw_fde_end);
15122         }
15123
15124       add_ranges (NULL);
15125     }
15126
15127   /* Output location list section if necessary.  */
15128   if (have_location_lists)
15129     {
15130       /* Output the location lists info.  */
15131       switch_to_section (debug_loc_section);
15132       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15133                                    DEBUG_LOC_SECTION_LABEL, 0);
15134       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15135       output_location_lists (die);
15136     }
15137
15138   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15139     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15140                     debug_line_section_label);
15141
15142   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15143     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15144
15145   /* Output all of the compilation units.  We put the main one last so that
15146      the offsets are available to output_pubnames.  */
15147   for (node = limbo_die_list; node; node = node->next)
15148     output_comp_unit (node->die, 0);
15149
15150   output_comp_unit (comp_unit_die, 0);
15151
15152   /* Output the abbreviation table.  */
15153   switch_to_section (debug_abbrev_section);
15154   output_abbrev_section ();
15155
15156   /* Output public names table if necessary.  */
15157   if (!VEC_empty (pubname_entry, pubname_table))
15158     {
15159       switch_to_section (debug_pubnames_section);
15160       output_pubnames (pubname_table);
15161     }
15162
15163 #ifdef DEBUG_PUBTYPES_SECTION
15164   /* Output public types table if necessary.  */
15165   if (!VEC_empty (pubname_entry, pubtype_table))
15166     {
15167       switch_to_section (debug_pubtypes_section);
15168       output_pubnames (pubtype_table);
15169     }
15170 #endif
15171
15172   /* Output the address range information.  We only put functions in the arange
15173      table, so don't write it out if we don't have any.  */
15174   if (fde_table_in_use)
15175     {
15176       switch_to_section (debug_aranges_section);
15177       output_aranges ();
15178     }
15179
15180   /* Output ranges section if necessary.  */
15181   if (ranges_table_in_use)
15182     {
15183       switch_to_section (debug_ranges_section);
15184       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15185       output_ranges ();
15186     }
15187
15188   /* Output the source line correspondence table.  We must do this
15189      even if there is no line information.  Otherwise, on an empty
15190      translation unit, we will generate a present, but empty,
15191      .debug_info section.  IRIX 6.5 `nm' will then complain when
15192      examining the file.  This is done late so that any filenames
15193      used by the debug_info section are marked as 'used'.  */
15194   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15195     {
15196       switch_to_section (debug_line_section);
15197       output_line_info ();
15198     }
15199
15200   /* Have to end the macro section.  */
15201   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15202     {
15203       switch_to_section (debug_macinfo_section);
15204       dw2_asm_output_data (1, 0, "End compilation unit");
15205     }
15206
15207   /* If we emitted any DW_FORM_strp form attribute, output the string
15208      table too.  */
15209   if (debug_str_hash)
15210     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15211 }
15212 #else
15213
15214 /* This should never be used, but its address is needed for comparisons.  */
15215 const struct gcc_debug_hooks dwarf2_debug_hooks;
15216
15217 #endif /* DWARF2_DEBUGGING_INFO */
15218
15219 #include "gt-dwarf2out.h"