OSDN Git Service

PR fortran/35037
[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   /* There is no need to mark used sections when not debugging.  */
2733   if (cold_text_section != NULL)
2734     dwarf2out_note_section_used ();
2735 }
2736 #endif
2737 \f
2738 /* And now, the subset of the debugging information support code necessary
2739    for emitting location expressions.  */
2740
2741 /* Data about a single source file.  */
2742 struct dwarf_file_data GTY(())
2743 {
2744   const char * filename;
2745   int emitted_number;
2746 };
2747
2748 /* We need some way to distinguish DW_OP_addr with a direct symbol
2749    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2750 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2751
2752
2753 typedef struct dw_val_struct *dw_val_ref;
2754 typedef struct die_struct *dw_die_ref;
2755 typedef const struct die_struct *const_dw_die_ref;
2756 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2757 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2758
2759 /* Each DIE may have a series of attribute/value pairs.  Values
2760    can take on several forms.  The forms that are used in this
2761    implementation are listed below.  */
2762
2763 enum dw_val_class
2764 {
2765   dw_val_class_addr,
2766   dw_val_class_offset,
2767   dw_val_class_loc,
2768   dw_val_class_loc_list,
2769   dw_val_class_range_list,
2770   dw_val_class_const,
2771   dw_val_class_unsigned_const,
2772   dw_val_class_long_long,
2773   dw_val_class_vec,
2774   dw_val_class_flag,
2775   dw_val_class_die_ref,
2776   dw_val_class_fde_ref,
2777   dw_val_class_lbl_id,
2778   dw_val_class_lineptr,
2779   dw_val_class_str,
2780   dw_val_class_macptr,
2781   dw_val_class_file
2782 };
2783
2784 /* Describe a double word constant value.  */
2785 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2786
2787 typedef struct dw_long_long_struct GTY(())
2788 {
2789   unsigned long hi;
2790   unsigned long low;
2791 }
2792 dw_long_long_const;
2793
2794 /* Describe a floating point constant value, or a vector constant value.  */
2795
2796 typedef struct dw_vec_struct GTY(())
2797 {
2798   unsigned char * GTY((length ("%h.length"))) array;
2799   unsigned length;
2800   unsigned elt_size;
2801 }
2802 dw_vec_const;
2803
2804 /* The dw_val_node describes an attribute's value, as it is
2805    represented internally.  */
2806
2807 typedef struct dw_val_struct GTY(())
2808 {
2809   enum dw_val_class val_class;
2810   union dw_val_struct_union
2811     {
2812       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2813       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2814       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2815       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2816       HOST_WIDE_INT GTY ((default)) val_int;
2817       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2818       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2819       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2820       struct dw_val_die_union
2821         {
2822           dw_die_ref die;
2823           int external;
2824         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2825       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2826       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2827       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2828       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2829       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2830     }
2831   GTY ((desc ("%1.val_class"))) v;
2832 }
2833 dw_val_node;
2834
2835 /* Locations in memory are described using a sequence of stack machine
2836    operations.  */
2837
2838 typedef struct dw_loc_descr_struct GTY(())
2839 {
2840   dw_loc_descr_ref dw_loc_next;
2841   enum dwarf_location_atom dw_loc_opc;
2842   dw_val_node dw_loc_oprnd1;
2843   dw_val_node dw_loc_oprnd2;
2844   int dw_loc_addr;
2845 }
2846 dw_loc_descr_node;
2847
2848 /* Location lists are ranges + location descriptions for that range,
2849    so you can track variables that are in different places over
2850    their entire life.  */
2851 typedef struct dw_loc_list_struct GTY(())
2852 {
2853   dw_loc_list_ref dw_loc_next;
2854   const char *begin; /* Label for begin address of range */
2855   const char *end;  /* Label for end address of range */
2856   char *ll_symbol; /* Label for beginning of location list.
2857                       Only on head of list */
2858   const char *section; /* Section this loclist is relative to */
2859   dw_loc_descr_ref expr;
2860 } dw_loc_list_node;
2861
2862 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2863
2864 static const char *dwarf_stack_op_name (unsigned);
2865 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2866                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2867 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2868 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2869 static unsigned long size_of_locs (dw_loc_descr_ref);
2870 static void output_loc_operands (dw_loc_descr_ref);
2871 static void output_loc_sequence (dw_loc_descr_ref);
2872
2873 /* Convert a DWARF stack opcode into its string name.  */
2874
2875 static const char *
2876 dwarf_stack_op_name (unsigned int op)
2877 {
2878   switch (op)
2879     {
2880     case DW_OP_addr:
2881     case INTERNAL_DW_OP_tls_addr:
2882       return "DW_OP_addr";
2883     case DW_OP_deref:
2884       return "DW_OP_deref";
2885     case DW_OP_const1u:
2886       return "DW_OP_const1u";
2887     case DW_OP_const1s:
2888       return "DW_OP_const1s";
2889     case DW_OP_const2u:
2890       return "DW_OP_const2u";
2891     case DW_OP_const2s:
2892       return "DW_OP_const2s";
2893     case DW_OP_const4u:
2894       return "DW_OP_const4u";
2895     case DW_OP_const4s:
2896       return "DW_OP_const4s";
2897     case DW_OP_const8u:
2898       return "DW_OP_const8u";
2899     case DW_OP_const8s:
2900       return "DW_OP_const8s";
2901     case DW_OP_constu:
2902       return "DW_OP_constu";
2903     case DW_OP_consts:
2904       return "DW_OP_consts";
2905     case DW_OP_dup:
2906       return "DW_OP_dup";
2907     case DW_OP_drop:
2908       return "DW_OP_drop";
2909     case DW_OP_over:
2910       return "DW_OP_over";
2911     case DW_OP_pick:
2912       return "DW_OP_pick";
2913     case DW_OP_swap:
2914       return "DW_OP_swap";
2915     case DW_OP_rot:
2916       return "DW_OP_rot";
2917     case DW_OP_xderef:
2918       return "DW_OP_xderef";
2919     case DW_OP_abs:
2920       return "DW_OP_abs";
2921     case DW_OP_and:
2922       return "DW_OP_and";
2923     case DW_OP_div:
2924       return "DW_OP_div";
2925     case DW_OP_minus:
2926       return "DW_OP_minus";
2927     case DW_OP_mod:
2928       return "DW_OP_mod";
2929     case DW_OP_mul:
2930       return "DW_OP_mul";
2931     case DW_OP_neg:
2932       return "DW_OP_neg";
2933     case DW_OP_not:
2934       return "DW_OP_not";
2935     case DW_OP_or:
2936       return "DW_OP_or";
2937     case DW_OP_plus:
2938       return "DW_OP_plus";
2939     case DW_OP_plus_uconst:
2940       return "DW_OP_plus_uconst";
2941     case DW_OP_shl:
2942       return "DW_OP_shl";
2943     case DW_OP_shr:
2944       return "DW_OP_shr";
2945     case DW_OP_shra:
2946       return "DW_OP_shra";
2947     case DW_OP_xor:
2948       return "DW_OP_xor";
2949     case DW_OP_bra:
2950       return "DW_OP_bra";
2951     case DW_OP_eq:
2952       return "DW_OP_eq";
2953     case DW_OP_ge:
2954       return "DW_OP_ge";
2955     case DW_OP_gt:
2956       return "DW_OP_gt";
2957     case DW_OP_le:
2958       return "DW_OP_le";
2959     case DW_OP_lt:
2960       return "DW_OP_lt";
2961     case DW_OP_ne:
2962       return "DW_OP_ne";
2963     case DW_OP_skip:
2964       return "DW_OP_skip";
2965     case DW_OP_lit0:
2966       return "DW_OP_lit0";
2967     case DW_OP_lit1:
2968       return "DW_OP_lit1";
2969     case DW_OP_lit2:
2970       return "DW_OP_lit2";
2971     case DW_OP_lit3:
2972       return "DW_OP_lit3";
2973     case DW_OP_lit4:
2974       return "DW_OP_lit4";
2975     case DW_OP_lit5:
2976       return "DW_OP_lit5";
2977     case DW_OP_lit6:
2978       return "DW_OP_lit6";
2979     case DW_OP_lit7:
2980       return "DW_OP_lit7";
2981     case DW_OP_lit8:
2982       return "DW_OP_lit8";
2983     case DW_OP_lit9:
2984       return "DW_OP_lit9";
2985     case DW_OP_lit10:
2986       return "DW_OP_lit10";
2987     case DW_OP_lit11:
2988       return "DW_OP_lit11";
2989     case DW_OP_lit12:
2990       return "DW_OP_lit12";
2991     case DW_OP_lit13:
2992       return "DW_OP_lit13";
2993     case DW_OP_lit14:
2994       return "DW_OP_lit14";
2995     case DW_OP_lit15:
2996       return "DW_OP_lit15";
2997     case DW_OP_lit16:
2998       return "DW_OP_lit16";
2999     case DW_OP_lit17:
3000       return "DW_OP_lit17";
3001     case DW_OP_lit18:
3002       return "DW_OP_lit18";
3003     case DW_OP_lit19:
3004       return "DW_OP_lit19";
3005     case DW_OP_lit20:
3006       return "DW_OP_lit20";
3007     case DW_OP_lit21:
3008       return "DW_OP_lit21";
3009     case DW_OP_lit22:
3010       return "DW_OP_lit22";
3011     case DW_OP_lit23:
3012       return "DW_OP_lit23";
3013     case DW_OP_lit24:
3014       return "DW_OP_lit24";
3015     case DW_OP_lit25:
3016       return "DW_OP_lit25";
3017     case DW_OP_lit26:
3018       return "DW_OP_lit26";
3019     case DW_OP_lit27:
3020       return "DW_OP_lit27";
3021     case DW_OP_lit28:
3022       return "DW_OP_lit28";
3023     case DW_OP_lit29:
3024       return "DW_OP_lit29";
3025     case DW_OP_lit30:
3026       return "DW_OP_lit30";
3027     case DW_OP_lit31:
3028       return "DW_OP_lit31";
3029     case DW_OP_reg0:
3030       return "DW_OP_reg0";
3031     case DW_OP_reg1:
3032       return "DW_OP_reg1";
3033     case DW_OP_reg2:
3034       return "DW_OP_reg2";
3035     case DW_OP_reg3:
3036       return "DW_OP_reg3";
3037     case DW_OP_reg4:
3038       return "DW_OP_reg4";
3039     case DW_OP_reg5:
3040       return "DW_OP_reg5";
3041     case DW_OP_reg6:
3042       return "DW_OP_reg6";
3043     case DW_OP_reg7:
3044       return "DW_OP_reg7";
3045     case DW_OP_reg8:
3046       return "DW_OP_reg8";
3047     case DW_OP_reg9:
3048       return "DW_OP_reg9";
3049     case DW_OP_reg10:
3050       return "DW_OP_reg10";
3051     case DW_OP_reg11:
3052       return "DW_OP_reg11";
3053     case DW_OP_reg12:
3054       return "DW_OP_reg12";
3055     case DW_OP_reg13:
3056       return "DW_OP_reg13";
3057     case DW_OP_reg14:
3058       return "DW_OP_reg14";
3059     case DW_OP_reg15:
3060       return "DW_OP_reg15";
3061     case DW_OP_reg16:
3062       return "DW_OP_reg16";
3063     case DW_OP_reg17:
3064       return "DW_OP_reg17";
3065     case DW_OP_reg18:
3066       return "DW_OP_reg18";
3067     case DW_OP_reg19:
3068       return "DW_OP_reg19";
3069     case DW_OP_reg20:
3070       return "DW_OP_reg20";
3071     case DW_OP_reg21:
3072       return "DW_OP_reg21";
3073     case DW_OP_reg22:
3074       return "DW_OP_reg22";
3075     case DW_OP_reg23:
3076       return "DW_OP_reg23";
3077     case DW_OP_reg24:
3078       return "DW_OP_reg24";
3079     case DW_OP_reg25:
3080       return "DW_OP_reg25";
3081     case DW_OP_reg26:
3082       return "DW_OP_reg26";
3083     case DW_OP_reg27:
3084       return "DW_OP_reg27";
3085     case DW_OP_reg28:
3086       return "DW_OP_reg28";
3087     case DW_OP_reg29:
3088       return "DW_OP_reg29";
3089     case DW_OP_reg30:
3090       return "DW_OP_reg30";
3091     case DW_OP_reg31:
3092       return "DW_OP_reg31";
3093     case DW_OP_breg0:
3094       return "DW_OP_breg0";
3095     case DW_OP_breg1:
3096       return "DW_OP_breg1";
3097     case DW_OP_breg2:
3098       return "DW_OP_breg2";
3099     case DW_OP_breg3:
3100       return "DW_OP_breg3";
3101     case DW_OP_breg4:
3102       return "DW_OP_breg4";
3103     case DW_OP_breg5:
3104       return "DW_OP_breg5";
3105     case DW_OP_breg6:
3106       return "DW_OP_breg6";
3107     case DW_OP_breg7:
3108       return "DW_OP_breg7";
3109     case DW_OP_breg8:
3110       return "DW_OP_breg8";
3111     case DW_OP_breg9:
3112       return "DW_OP_breg9";
3113     case DW_OP_breg10:
3114       return "DW_OP_breg10";
3115     case DW_OP_breg11:
3116       return "DW_OP_breg11";
3117     case DW_OP_breg12:
3118       return "DW_OP_breg12";
3119     case DW_OP_breg13:
3120       return "DW_OP_breg13";
3121     case DW_OP_breg14:
3122       return "DW_OP_breg14";
3123     case DW_OP_breg15:
3124       return "DW_OP_breg15";
3125     case DW_OP_breg16:
3126       return "DW_OP_breg16";
3127     case DW_OP_breg17:
3128       return "DW_OP_breg17";
3129     case DW_OP_breg18:
3130       return "DW_OP_breg18";
3131     case DW_OP_breg19:
3132       return "DW_OP_breg19";
3133     case DW_OP_breg20:
3134       return "DW_OP_breg20";
3135     case DW_OP_breg21:
3136       return "DW_OP_breg21";
3137     case DW_OP_breg22:
3138       return "DW_OP_breg22";
3139     case DW_OP_breg23:
3140       return "DW_OP_breg23";
3141     case DW_OP_breg24:
3142       return "DW_OP_breg24";
3143     case DW_OP_breg25:
3144       return "DW_OP_breg25";
3145     case DW_OP_breg26:
3146       return "DW_OP_breg26";
3147     case DW_OP_breg27:
3148       return "DW_OP_breg27";
3149     case DW_OP_breg28:
3150       return "DW_OP_breg28";
3151     case DW_OP_breg29:
3152       return "DW_OP_breg29";
3153     case DW_OP_breg30:
3154       return "DW_OP_breg30";
3155     case DW_OP_breg31:
3156       return "DW_OP_breg31";
3157     case DW_OP_regx:
3158       return "DW_OP_regx";
3159     case DW_OP_fbreg:
3160       return "DW_OP_fbreg";
3161     case DW_OP_bregx:
3162       return "DW_OP_bregx";
3163     case DW_OP_piece:
3164       return "DW_OP_piece";
3165     case DW_OP_deref_size:
3166       return "DW_OP_deref_size";
3167     case DW_OP_xderef_size:
3168       return "DW_OP_xderef_size";
3169     case DW_OP_nop:
3170       return "DW_OP_nop";
3171     case DW_OP_push_object_address:
3172       return "DW_OP_push_object_address";
3173     case DW_OP_call2:
3174       return "DW_OP_call2";
3175     case DW_OP_call4:
3176       return "DW_OP_call4";
3177     case DW_OP_call_ref:
3178       return "DW_OP_call_ref";
3179     case DW_OP_GNU_push_tls_address:
3180       return "DW_OP_GNU_push_tls_address";
3181     case DW_OP_GNU_uninit:
3182       return "DW_OP_GNU_uninit";
3183     default:
3184       return "OP_<unknown>";
3185     }
3186 }
3187
3188 /* Return a pointer to a newly allocated location description.  Location
3189    descriptions are simple expression terms that can be strung
3190    together to form more complicated location (address) descriptions.  */
3191
3192 static inline dw_loc_descr_ref
3193 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3194                unsigned HOST_WIDE_INT oprnd2)
3195 {
3196   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3197
3198   descr->dw_loc_opc = op;
3199   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3200   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3201   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3202   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3203
3204   return descr;
3205 }
3206
3207 /* Add a location description term to a location description expression.  */
3208
3209 static inline void
3210 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3211 {
3212   dw_loc_descr_ref *d;
3213
3214   /* Find the end of the chain.  */
3215   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3216     ;
3217
3218   *d = descr;
3219 }
3220
3221 /* Return the size of a location descriptor.  */
3222
3223 static unsigned long
3224 size_of_loc_descr (dw_loc_descr_ref loc)
3225 {
3226   unsigned long size = 1;
3227
3228   switch (loc->dw_loc_opc)
3229     {
3230     case DW_OP_addr:
3231     case INTERNAL_DW_OP_tls_addr:
3232       size += DWARF2_ADDR_SIZE;
3233       break;
3234     case DW_OP_const1u:
3235     case DW_OP_const1s:
3236       size += 1;
3237       break;
3238     case DW_OP_const2u:
3239     case DW_OP_const2s:
3240       size += 2;
3241       break;
3242     case DW_OP_const4u:
3243     case DW_OP_const4s:
3244       size += 4;
3245       break;
3246     case DW_OP_const8u:
3247     case DW_OP_const8s:
3248       size += 8;
3249       break;
3250     case DW_OP_constu:
3251       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3252       break;
3253     case DW_OP_consts:
3254       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3255       break;
3256     case DW_OP_pick:
3257       size += 1;
3258       break;
3259     case DW_OP_plus_uconst:
3260       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3261       break;
3262     case DW_OP_skip:
3263     case DW_OP_bra:
3264       size += 2;
3265       break;
3266     case DW_OP_breg0:
3267     case DW_OP_breg1:
3268     case DW_OP_breg2:
3269     case DW_OP_breg3:
3270     case DW_OP_breg4:
3271     case DW_OP_breg5:
3272     case DW_OP_breg6:
3273     case DW_OP_breg7:
3274     case DW_OP_breg8:
3275     case DW_OP_breg9:
3276     case DW_OP_breg10:
3277     case DW_OP_breg11:
3278     case DW_OP_breg12:
3279     case DW_OP_breg13:
3280     case DW_OP_breg14:
3281     case DW_OP_breg15:
3282     case DW_OP_breg16:
3283     case DW_OP_breg17:
3284     case DW_OP_breg18:
3285     case DW_OP_breg19:
3286     case DW_OP_breg20:
3287     case DW_OP_breg21:
3288     case DW_OP_breg22:
3289     case DW_OP_breg23:
3290     case DW_OP_breg24:
3291     case DW_OP_breg25:
3292     case DW_OP_breg26:
3293     case DW_OP_breg27:
3294     case DW_OP_breg28:
3295     case DW_OP_breg29:
3296     case DW_OP_breg30:
3297     case DW_OP_breg31:
3298       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3299       break;
3300     case DW_OP_regx:
3301       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3302       break;
3303     case DW_OP_fbreg:
3304       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3305       break;
3306     case DW_OP_bregx:
3307       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3308       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3309       break;
3310     case DW_OP_piece:
3311       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3312       break;
3313     case DW_OP_deref_size:
3314     case DW_OP_xderef_size:
3315       size += 1;
3316       break;
3317     case DW_OP_call2:
3318       size += 2;
3319       break;
3320     case DW_OP_call4:
3321       size += 4;
3322       break;
3323     case DW_OP_call_ref:
3324       size += DWARF2_ADDR_SIZE;
3325       break;
3326     default:
3327       break;
3328     }
3329
3330   return size;
3331 }
3332
3333 /* Return the size of a series of location descriptors.  */
3334
3335 static unsigned long
3336 size_of_locs (dw_loc_descr_ref loc)
3337 {
3338   dw_loc_descr_ref l;
3339   unsigned long size;
3340
3341   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3342      field, to avoid writing to a PCH file.  */
3343   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3344     {
3345       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3346         break;
3347       size += size_of_loc_descr (l);
3348     }
3349   if (! l)
3350     return size;
3351
3352   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3353     {
3354       l->dw_loc_addr = size;
3355       size += size_of_loc_descr (l);
3356     }
3357
3358   return size;
3359 }
3360
3361 /* Output location description stack opcode's operands (if any).  */
3362
3363 static void
3364 output_loc_operands (dw_loc_descr_ref loc)
3365 {
3366   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3367   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3368
3369   switch (loc->dw_loc_opc)
3370     {
3371 #ifdef DWARF2_DEBUGGING_INFO
3372     case DW_OP_addr:
3373       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3374       break;
3375     case DW_OP_const2u:
3376     case DW_OP_const2s:
3377       dw2_asm_output_data (2, val1->v.val_int, NULL);
3378       break;
3379     case DW_OP_const4u:
3380     case DW_OP_const4s:
3381       dw2_asm_output_data (4, val1->v.val_int, NULL);
3382       break;
3383     case DW_OP_const8u:
3384     case DW_OP_const8s:
3385       gcc_assert (HOST_BITS_PER_LONG >= 64);
3386       dw2_asm_output_data (8, val1->v.val_int, NULL);
3387       break;
3388     case DW_OP_skip:
3389     case DW_OP_bra:
3390       {
3391         int offset;
3392
3393         gcc_assert (val1->val_class == dw_val_class_loc);
3394         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3395
3396         dw2_asm_output_data (2, offset, NULL);
3397       }
3398       break;
3399 #else
3400     case DW_OP_addr:
3401     case DW_OP_const2u:
3402     case DW_OP_const2s:
3403     case DW_OP_const4u:
3404     case DW_OP_const4s:
3405     case DW_OP_const8u:
3406     case DW_OP_const8s:
3407     case DW_OP_skip:
3408     case DW_OP_bra:
3409       /* We currently don't make any attempt to make sure these are
3410          aligned properly like we do for the main unwind info, so
3411          don't support emitting things larger than a byte if we're
3412          only doing unwinding.  */
3413       gcc_unreachable ();
3414 #endif
3415     case DW_OP_const1u:
3416     case DW_OP_const1s:
3417       dw2_asm_output_data (1, val1->v.val_int, NULL);
3418       break;
3419     case DW_OP_constu:
3420       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3421       break;
3422     case DW_OP_consts:
3423       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3424       break;
3425     case DW_OP_pick:
3426       dw2_asm_output_data (1, val1->v.val_int, NULL);
3427       break;
3428     case DW_OP_plus_uconst:
3429       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3430       break;
3431     case DW_OP_breg0:
3432     case DW_OP_breg1:
3433     case DW_OP_breg2:
3434     case DW_OP_breg3:
3435     case DW_OP_breg4:
3436     case DW_OP_breg5:
3437     case DW_OP_breg6:
3438     case DW_OP_breg7:
3439     case DW_OP_breg8:
3440     case DW_OP_breg9:
3441     case DW_OP_breg10:
3442     case DW_OP_breg11:
3443     case DW_OP_breg12:
3444     case DW_OP_breg13:
3445     case DW_OP_breg14:
3446     case DW_OP_breg15:
3447     case DW_OP_breg16:
3448     case DW_OP_breg17:
3449     case DW_OP_breg18:
3450     case DW_OP_breg19:
3451     case DW_OP_breg20:
3452     case DW_OP_breg21:
3453     case DW_OP_breg22:
3454     case DW_OP_breg23:
3455     case DW_OP_breg24:
3456     case DW_OP_breg25:
3457     case DW_OP_breg26:
3458     case DW_OP_breg27:
3459     case DW_OP_breg28:
3460     case DW_OP_breg29:
3461     case DW_OP_breg30:
3462     case DW_OP_breg31:
3463       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3464       break;
3465     case DW_OP_regx:
3466       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3467       break;
3468     case DW_OP_fbreg:
3469       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3470       break;
3471     case DW_OP_bregx:
3472       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3473       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3474       break;
3475     case DW_OP_piece:
3476       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3477       break;
3478     case DW_OP_deref_size:
3479     case DW_OP_xderef_size:
3480       dw2_asm_output_data (1, val1->v.val_int, NULL);
3481       break;
3482
3483     case INTERNAL_DW_OP_tls_addr:
3484       if (targetm.asm_out.output_dwarf_dtprel)
3485         {
3486           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3487                                                DWARF2_ADDR_SIZE,
3488                                                val1->v.val_addr);
3489           fputc ('\n', asm_out_file);
3490         }
3491       else
3492         gcc_unreachable ();
3493       break;
3494
3495     default:
3496       /* Other codes have no operands.  */
3497       break;
3498     }
3499 }
3500
3501 /* Output a sequence of location operations.  */
3502
3503 static void
3504 output_loc_sequence (dw_loc_descr_ref loc)
3505 {
3506   for (; loc != NULL; loc = loc->dw_loc_next)
3507     {
3508       /* Output the opcode.  */
3509       dw2_asm_output_data (1, loc->dw_loc_opc,
3510                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3511
3512       /* Output the operand(s) (if any).  */
3513       output_loc_operands (loc);
3514     }
3515 }
3516
3517 /* This routine will generate the correct assembly data for a location
3518    description based on a cfi entry with a complex address.  */
3519
3520 static void
3521 output_cfa_loc (dw_cfi_ref cfi)
3522 {
3523   dw_loc_descr_ref loc;
3524   unsigned long size;
3525
3526   /* Output the size of the block.  */
3527   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3528   size = size_of_locs (loc);
3529   dw2_asm_output_data_uleb128 (size, NULL);
3530
3531   /* Now output the operations themselves.  */
3532   output_loc_sequence (loc);
3533 }
3534
3535 /* This function builds a dwarf location descriptor sequence from a
3536    dw_cfa_location, adding the given OFFSET to the result of the
3537    expression.  */
3538
3539 static struct dw_loc_descr_struct *
3540 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3541 {
3542   struct dw_loc_descr_struct *head, *tmp;
3543
3544   offset += cfa->offset;
3545
3546   if (cfa->indirect)
3547     {
3548       if (cfa->base_offset)
3549         {
3550           if (cfa->reg <= 31)
3551             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3552           else
3553             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3554         }
3555       else if (cfa->reg <= 31)
3556         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3557       else
3558         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3559
3560       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3561       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3562       add_loc_descr (&head, tmp);
3563       if (offset != 0)
3564         {
3565           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3566           add_loc_descr (&head, tmp);
3567         }
3568     }
3569   else
3570     {
3571       if (offset == 0)
3572         if (cfa->reg <= 31)
3573           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3574         else
3575           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3576       else if (cfa->reg <= 31)
3577         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3578       else
3579         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3580     }
3581
3582   return head;
3583 }
3584
3585 /* This function fills in aa dw_cfa_location structure from a dwarf location
3586    descriptor sequence.  */
3587
3588 static void
3589 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3590 {
3591   struct dw_loc_descr_struct *ptr;
3592   cfa->offset = 0;
3593   cfa->base_offset = 0;
3594   cfa->indirect = 0;
3595   cfa->reg = -1;
3596
3597   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3598     {
3599       enum dwarf_location_atom op = ptr->dw_loc_opc;
3600
3601       switch (op)
3602         {
3603         case DW_OP_reg0:
3604         case DW_OP_reg1:
3605         case DW_OP_reg2:
3606         case DW_OP_reg3:
3607         case DW_OP_reg4:
3608         case DW_OP_reg5:
3609         case DW_OP_reg6:
3610         case DW_OP_reg7:
3611         case DW_OP_reg8:
3612         case DW_OP_reg9:
3613         case DW_OP_reg10:
3614         case DW_OP_reg11:
3615         case DW_OP_reg12:
3616         case DW_OP_reg13:
3617         case DW_OP_reg14:
3618         case DW_OP_reg15:
3619         case DW_OP_reg16:
3620         case DW_OP_reg17:
3621         case DW_OP_reg18:
3622         case DW_OP_reg19:
3623         case DW_OP_reg20:
3624         case DW_OP_reg21:
3625         case DW_OP_reg22:
3626         case DW_OP_reg23:
3627         case DW_OP_reg24:
3628         case DW_OP_reg25:
3629         case DW_OP_reg26:
3630         case DW_OP_reg27:
3631         case DW_OP_reg28:
3632         case DW_OP_reg29:
3633         case DW_OP_reg30:
3634         case DW_OP_reg31:
3635           cfa->reg = op - DW_OP_reg0;
3636           break;
3637         case DW_OP_regx:
3638           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3639           break;
3640         case DW_OP_breg0:
3641         case DW_OP_breg1:
3642         case DW_OP_breg2:
3643         case DW_OP_breg3:
3644         case DW_OP_breg4:
3645         case DW_OP_breg5:
3646         case DW_OP_breg6:
3647         case DW_OP_breg7:
3648         case DW_OP_breg8:
3649         case DW_OP_breg9:
3650         case DW_OP_breg10:
3651         case DW_OP_breg11:
3652         case DW_OP_breg12:
3653         case DW_OP_breg13:
3654         case DW_OP_breg14:
3655         case DW_OP_breg15:
3656         case DW_OP_breg16:
3657         case DW_OP_breg17:
3658         case DW_OP_breg18:
3659         case DW_OP_breg19:
3660         case DW_OP_breg20:
3661         case DW_OP_breg21:
3662         case DW_OP_breg22:
3663         case DW_OP_breg23:
3664         case DW_OP_breg24:
3665         case DW_OP_breg25:
3666         case DW_OP_breg26:
3667         case DW_OP_breg27:
3668         case DW_OP_breg28:
3669         case DW_OP_breg29:
3670         case DW_OP_breg30:
3671         case DW_OP_breg31:
3672           cfa->reg = op - DW_OP_breg0;
3673           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3674           break;
3675         case DW_OP_bregx:
3676           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3677           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3678           break;
3679         case DW_OP_deref:
3680           cfa->indirect = 1;
3681           break;
3682         case DW_OP_plus_uconst:
3683           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3684           break;
3685         default:
3686           internal_error ("DW_LOC_OP %s not implemented",
3687                           dwarf_stack_op_name (ptr->dw_loc_opc));
3688         }
3689     }
3690 }
3691 #endif /* .debug_frame support */
3692 \f
3693 /* And now, the support for symbolic debugging information.  */
3694 #ifdef DWARF2_DEBUGGING_INFO
3695
3696 /* .debug_str support.  */
3697 static int output_indirect_string (void **, void *);
3698
3699 static void dwarf2out_init (const char *);
3700 static void dwarf2out_finish (const char *);
3701 static void dwarf2out_define (unsigned int, const char *);
3702 static void dwarf2out_undef (unsigned int, const char *);
3703 static void dwarf2out_start_source_file (unsigned, const char *);
3704 static void dwarf2out_end_source_file (unsigned);
3705 static void dwarf2out_begin_block (unsigned, unsigned);
3706 static void dwarf2out_end_block (unsigned, unsigned);
3707 static bool dwarf2out_ignore_block (const_tree);
3708 static void dwarf2out_global_decl (tree);
3709 static void dwarf2out_type_decl (tree, int);
3710 static void dwarf2out_imported_module_or_decl (tree, tree);
3711 static void dwarf2out_abstract_function (tree);
3712 static void dwarf2out_var_location (rtx);
3713 static void dwarf2out_begin_function (tree);
3714
3715 /* The debug hooks structure.  */
3716
3717 const struct gcc_debug_hooks dwarf2_debug_hooks =
3718 {
3719   dwarf2out_init,
3720   dwarf2out_finish,
3721   dwarf2out_define,
3722   dwarf2out_undef,
3723   dwarf2out_start_source_file,
3724   dwarf2out_end_source_file,
3725   dwarf2out_begin_block,
3726   dwarf2out_end_block,
3727   dwarf2out_ignore_block,
3728   dwarf2out_source_line,
3729   dwarf2out_begin_prologue,
3730   debug_nothing_int_charstar,   /* end_prologue */
3731   dwarf2out_end_epilogue,
3732   dwarf2out_begin_function,
3733   debug_nothing_int,            /* end_function */
3734   dwarf2out_decl,               /* function_decl */
3735   dwarf2out_global_decl,
3736   dwarf2out_type_decl,          /* type_decl */
3737   dwarf2out_imported_module_or_decl,
3738   debug_nothing_tree,           /* deferred_inline_function */
3739   /* The DWARF 2 backend tries to reduce debugging bloat by not
3740      emitting the abstract description of inline functions until
3741      something tries to reference them.  */
3742   dwarf2out_abstract_function,  /* outlining_inline_function */
3743   debug_nothing_rtx,            /* label */
3744   debug_nothing_int,            /* handle_pch */
3745   dwarf2out_var_location,
3746   dwarf2out_switch_text_section,
3747   1                             /* start_end_main_source_file */
3748 };
3749 #endif
3750 \f
3751 /* NOTE: In the comments in this file, many references are made to
3752    "Debugging Information Entries".  This term is abbreviated as `DIE'
3753    throughout the remainder of this file.  */
3754
3755 /* An internal representation of the DWARF output is built, and then
3756    walked to generate the DWARF debugging info.  The walk of the internal
3757    representation is done after the entire program has been compiled.
3758    The types below are used to describe the internal representation.  */
3759
3760 /* Various DIE's use offsets relative to the beginning of the
3761    .debug_info section to refer to each other.  */
3762
3763 typedef long int dw_offset;
3764
3765 /* Define typedefs here to avoid circular dependencies.  */
3766
3767 typedef struct dw_attr_struct *dw_attr_ref;
3768 typedef struct dw_line_info_struct *dw_line_info_ref;
3769 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3770 typedef struct pubname_struct *pubname_ref;
3771 typedef struct dw_ranges_struct *dw_ranges_ref;
3772 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3773
3774 /* Each entry in the line_info_table maintains the file and
3775    line number associated with the label generated for that
3776    entry.  The label gives the PC value associated with
3777    the line number entry.  */
3778
3779 typedef struct dw_line_info_struct GTY(())
3780 {
3781   unsigned long dw_file_num;
3782   unsigned long dw_line_num;
3783 }
3784 dw_line_info_entry;
3785
3786 /* Line information for functions in separate sections; each one gets its
3787    own sequence.  */
3788 typedef struct dw_separate_line_info_struct GTY(())
3789 {
3790   unsigned long dw_file_num;
3791   unsigned long dw_line_num;
3792   unsigned long function;
3793 }
3794 dw_separate_line_info_entry;
3795
3796 /* Each DIE attribute has a field specifying the attribute kind,
3797    a link to the next attribute in the chain, and an attribute value.
3798    Attributes are typically linked below the DIE they modify.  */
3799
3800 typedef struct dw_attr_struct GTY(())
3801 {
3802   enum dwarf_attribute dw_attr;
3803   dw_val_node dw_attr_val;
3804 }
3805 dw_attr_node;
3806
3807 DEF_VEC_O(dw_attr_node);
3808 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3809
3810 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3811    The children of each node form a circular list linked by
3812    die_sib.  die_child points to the node *before* the "first" child node.  */
3813
3814 typedef struct die_struct GTY(())
3815 {
3816   enum dwarf_tag die_tag;
3817   char *die_symbol;
3818   VEC(dw_attr_node,gc) * die_attr;
3819   dw_die_ref die_parent;
3820   dw_die_ref die_child;
3821   dw_die_ref die_sib;
3822   dw_die_ref die_definition; /* ref from a specification to its definition */
3823   dw_offset die_offset;
3824   unsigned long die_abbrev;
3825   int die_mark;
3826   /* Die is used and must not be pruned as unused.  */
3827   int die_perennial_p;
3828   unsigned int decl_id;
3829 }
3830 die_node;
3831
3832 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3833 #define FOR_EACH_CHILD(die, c, expr) do {       \
3834   c = die->die_child;                           \
3835   if (c) do {                                   \
3836     c = c->die_sib;                             \
3837     expr;                                       \
3838   } while (c != die->die_child);                \
3839 } while (0)
3840
3841 /* The pubname structure */
3842
3843 typedef struct pubname_struct GTY(())
3844 {
3845   dw_die_ref die;
3846   const char *name;
3847 }
3848 pubname_entry;
3849
3850 DEF_VEC_O(pubname_entry);
3851 DEF_VEC_ALLOC_O(pubname_entry, gc);
3852
3853 struct dw_ranges_struct GTY(())
3854 {
3855   /* If this is positive, it's a block number, otherwise it's a
3856      bitwise-negated index into dw_ranges_by_label.  */
3857   int num;
3858 };
3859
3860 struct dw_ranges_by_label_struct GTY(())
3861 {
3862   const char *begin;
3863   const char *end;
3864 };
3865
3866 /* The limbo die list structure.  */
3867 typedef struct limbo_die_struct GTY(())
3868 {
3869   dw_die_ref die;
3870   tree created_for;
3871   struct limbo_die_struct *next;
3872 }
3873 limbo_die_node;
3874
3875 /* How to start an assembler comment.  */
3876 #ifndef ASM_COMMENT_START
3877 #define ASM_COMMENT_START ";#"
3878 #endif
3879
3880 /* Define a macro which returns nonzero for a TYPE_DECL which was
3881    implicitly generated for a tagged type.
3882
3883    Note that unlike the gcc front end (which generates a NULL named
3884    TYPE_DECL node for each complete tagged type, each array type, and
3885    each function type node created) the g++ front end generates a
3886    _named_ TYPE_DECL node for each tagged type node created.
3887    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3888    generate a DW_TAG_typedef DIE for them.  */
3889
3890 #define TYPE_DECL_IS_STUB(decl)                         \
3891   (DECL_NAME (decl) == NULL_TREE                        \
3892    || (DECL_ARTIFICIAL (decl)                           \
3893        && is_tagged_type (TREE_TYPE (decl))             \
3894        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3895            /* This is necessary for stub decls that     \
3896               appear in nested inline functions.  */    \
3897            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3898                && (decl_ultimate_origin (decl)          \
3899                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3900
3901 /* Information concerning the compilation unit's programming
3902    language, and compiler version.  */
3903
3904 /* Fixed size portion of the DWARF compilation unit header.  */
3905 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3906   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3907
3908 /* Fixed size portion of public names info.  */
3909 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3910
3911 /* Fixed size portion of the address range info.  */
3912 #define DWARF_ARANGES_HEADER_SIZE                                       \
3913   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3914                 DWARF2_ADDR_SIZE * 2)                                   \
3915    - DWARF_INITIAL_LENGTH_SIZE)
3916
3917 /* Size of padding portion in the address range info.  It must be
3918    aligned to twice the pointer size.  */
3919 #define DWARF_ARANGES_PAD_SIZE \
3920   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3921                 DWARF2_ADDR_SIZE * 2)                              \
3922    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3923
3924 /* Use assembler line directives if available.  */
3925 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3926 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3927 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3928 #else
3929 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3930 #endif
3931 #endif
3932
3933 /* Minimum line offset in a special line info. opcode.
3934    This value was chosen to give a reasonable range of values.  */
3935 #define DWARF_LINE_BASE  -10
3936
3937 /* First special line opcode - leave room for the standard opcodes.  */
3938 #define DWARF_LINE_OPCODE_BASE  10
3939
3940 /* Range of line offsets in a special line info. opcode.  */
3941 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3942
3943 /* Flag that indicates the initial value of the is_stmt_start flag.
3944    In the present implementation, we do not mark any lines as
3945    the beginning of a source statement, because that information
3946    is not made available by the GCC front-end.  */
3947 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3948
3949 #ifdef DWARF2_DEBUGGING_INFO
3950 /* This location is used by calc_die_sizes() to keep track
3951    the offset of each DIE within the .debug_info section.  */
3952 static unsigned long next_die_offset;
3953 #endif
3954
3955 /* Record the root of the DIE's built for the current compilation unit.  */
3956 static GTY(()) dw_die_ref comp_unit_die;
3957
3958 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3959 static GTY(()) limbo_die_node *limbo_die_list;
3960
3961 /* Filenames referenced by this compilation unit.  */
3962 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3963
3964 /* A hash table of references to DIE's that describe declarations.
3965    The key is a DECL_UID() which is a unique number identifying each decl.  */
3966 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3967
3968 /* Node of the variable location list.  */
3969 struct var_loc_node GTY ((chain_next ("%h.next")))
3970 {
3971   rtx GTY (()) var_loc_note;
3972   const char * GTY (()) label;
3973   const char * GTY (()) section_label;
3974   struct var_loc_node * GTY (()) next;
3975 };
3976
3977 /* Variable location list.  */
3978 struct var_loc_list_def GTY (())
3979 {
3980   struct var_loc_node * GTY (()) first;
3981
3982   /* Do not mark the last element of the chained list because
3983      it is marked through the chain.  */
3984   struct var_loc_node * GTY ((skip ("%h"))) last;
3985
3986   /* DECL_UID of the variable decl.  */
3987   unsigned int decl_id;
3988 };
3989 typedef struct var_loc_list_def var_loc_list;
3990
3991
3992 /* Table of decl location linked lists.  */
3993 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3994
3995 /* A pointer to the base of a list of references to DIE's that
3996    are uniquely identified by their tag, presence/absence of
3997    children DIE's, and list of attribute/value pairs.  */
3998 static GTY((length ("abbrev_die_table_allocated")))
3999   dw_die_ref *abbrev_die_table;
4000
4001 /* Number of elements currently allocated for abbrev_die_table.  */
4002 static GTY(()) unsigned abbrev_die_table_allocated;
4003
4004 /* Number of elements in type_die_table currently in use.  */
4005 static GTY(()) unsigned abbrev_die_table_in_use;
4006
4007 /* Size (in elements) of increments by which we may expand the
4008    abbrev_die_table.  */
4009 #define ABBREV_DIE_TABLE_INCREMENT 256
4010
4011 /* A pointer to the base of a table that contains line information
4012    for each source code line in .text in the compilation unit.  */
4013 static GTY((length ("line_info_table_allocated")))
4014      dw_line_info_ref line_info_table;
4015
4016 /* Number of elements currently allocated for line_info_table.  */
4017 static GTY(()) unsigned line_info_table_allocated;
4018
4019 /* Number of elements in line_info_table currently in use.  */
4020 static GTY(()) unsigned line_info_table_in_use;
4021
4022 /* A pointer to the base of a table that contains line information
4023    for each source code line outside of .text in the compilation unit.  */
4024 static GTY ((length ("separate_line_info_table_allocated")))
4025      dw_separate_line_info_ref separate_line_info_table;
4026
4027 /* Number of elements currently allocated for separate_line_info_table.  */
4028 static GTY(()) unsigned separate_line_info_table_allocated;
4029
4030 /* Number of elements in separate_line_info_table currently in use.  */
4031 static GTY(()) unsigned separate_line_info_table_in_use;
4032
4033 /* Size (in elements) of increments by which we may expand the
4034    line_info_table.  */
4035 #define LINE_INFO_TABLE_INCREMENT 1024
4036
4037 /* A pointer to the base of a table that contains a list of publicly
4038    accessible names.  */
4039 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4040
4041 /* A pointer to the base of a table that contains a list of publicly
4042    accessible types.  */
4043 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4044
4045 /* Array of dies for which we should generate .debug_arange info.  */
4046 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4047
4048 /* Number of elements currently allocated for arange_table.  */
4049 static GTY(()) unsigned arange_table_allocated;
4050
4051 /* Number of elements in arange_table currently in use.  */
4052 static GTY(()) unsigned arange_table_in_use;
4053
4054 /* Size (in elements) of increments by which we may expand the
4055    arange_table.  */
4056 #define ARANGE_TABLE_INCREMENT 64
4057
4058 /* Array of dies for which we should generate .debug_ranges info.  */
4059 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4060
4061 /* Number of elements currently allocated for ranges_table.  */
4062 static GTY(()) unsigned ranges_table_allocated;
4063
4064 /* Number of elements in ranges_table currently in use.  */
4065 static GTY(()) unsigned ranges_table_in_use;
4066
4067 /* Array of pairs of labels referenced in ranges_table.  */
4068 static GTY ((length ("ranges_by_label_allocated")))
4069      dw_ranges_by_label_ref ranges_by_label;
4070
4071 /* Number of elements currently allocated for ranges_by_label.  */
4072 static GTY(()) unsigned ranges_by_label_allocated;
4073
4074 /* Number of elements in ranges_by_label currently in use.  */
4075 static GTY(()) unsigned ranges_by_label_in_use;
4076
4077 /* Size (in elements) of increments by which we may expand the
4078    ranges_table.  */
4079 #define RANGES_TABLE_INCREMENT 64
4080
4081 /* Whether we have location lists that need outputting */
4082 static GTY(()) bool have_location_lists;
4083
4084 /* Unique label counter.  */
4085 static GTY(()) unsigned int loclabel_num;
4086
4087 #ifdef DWARF2_DEBUGGING_INFO
4088 /* Record whether the function being analyzed contains inlined functions.  */
4089 static int current_function_has_inlines;
4090 #endif
4091 #if 0 && defined (MIPS_DEBUGGING_INFO)
4092 static int comp_unit_has_inlines;
4093 #endif
4094
4095 /* The last file entry emitted by maybe_emit_file().  */
4096 static GTY(()) struct dwarf_file_data * last_emitted_file;
4097
4098 /* Number of internal labels generated by gen_internal_sym().  */
4099 static GTY(()) int label_num;
4100
4101 /* Cached result of previous call to lookup_filename.  */
4102 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4103
4104 #ifdef DWARF2_DEBUGGING_INFO
4105
4106 /* Offset from the "steady-state frame pointer" to the frame base,
4107    within the current function.  */
4108 static HOST_WIDE_INT frame_pointer_fb_offset;
4109
4110 /* Forward declarations for functions defined in this file.  */
4111
4112 static int is_pseudo_reg (const_rtx);
4113 static tree type_main_variant (tree);
4114 static int is_tagged_type (const_tree);
4115 static const char *dwarf_tag_name (unsigned);
4116 static const char *dwarf_attr_name (unsigned);
4117 static const char *dwarf_form_name (unsigned);
4118 static tree decl_ultimate_origin (const_tree);
4119 static tree block_ultimate_origin (const_tree);
4120 static tree decl_class_context (tree);
4121 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4122 static inline enum dw_val_class AT_class (dw_attr_ref);
4123 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4124 static inline unsigned AT_flag (dw_attr_ref);
4125 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4126 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4127 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4128 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4129 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4130                               unsigned long);
4131 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4132                                unsigned int, unsigned char *);
4133 static hashval_t debug_str_do_hash (const void *);
4134 static int debug_str_eq (const void *, const void *);
4135 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4136 static inline const char *AT_string (dw_attr_ref);
4137 static int AT_string_form (dw_attr_ref);
4138 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4139 static void add_AT_specification (dw_die_ref, dw_die_ref);
4140 static inline dw_die_ref AT_ref (dw_attr_ref);
4141 static inline int AT_ref_external (dw_attr_ref);
4142 static inline void set_AT_ref_external (dw_attr_ref, int);
4143 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4144 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4145 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4146 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4147                              dw_loc_list_ref);
4148 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4149 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4150 static inline rtx AT_addr (dw_attr_ref);
4151 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4152 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4153 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4154 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4155                            unsigned HOST_WIDE_INT);
4156 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4157                                unsigned long);
4158 static inline const char *AT_lbl (dw_attr_ref);
4159 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4160 static const char *get_AT_low_pc (dw_die_ref);
4161 static const char *get_AT_hi_pc (dw_die_ref);
4162 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4163 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4164 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4165 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4166 static bool is_c_family (void);
4167 static bool is_cxx (void);
4168 static bool is_java (void);
4169 static bool is_fortran (void);
4170 static bool is_ada (void);
4171 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4172 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4173 static void add_child_die (dw_die_ref, dw_die_ref);
4174 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4175 static dw_die_ref lookup_type_die (tree);
4176 static void equate_type_number_to_die (tree, dw_die_ref);
4177 static hashval_t decl_die_table_hash (const void *);
4178 static int decl_die_table_eq (const void *, const void *);
4179 static dw_die_ref lookup_decl_die (tree);
4180 static hashval_t decl_loc_table_hash (const void *);
4181 static int decl_loc_table_eq (const void *, const void *);
4182 static var_loc_list *lookup_decl_loc (const_tree);
4183 static void equate_decl_number_to_die (tree, dw_die_ref);
4184 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4185 static void print_spaces (FILE *);
4186 static void print_die (dw_die_ref, FILE *);
4187 static void print_dwarf_line_table (FILE *);
4188 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4189 static dw_die_ref pop_compile_unit (dw_die_ref);
4190 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4191 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4192 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4193 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4194 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4195 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4196 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4197 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4198 static void compute_section_prefix (dw_die_ref);
4199 static int is_type_die (dw_die_ref);
4200 static int is_comdat_die (dw_die_ref);
4201 static int is_symbol_die (dw_die_ref);
4202 static void assign_symbol_names (dw_die_ref);
4203 static void break_out_includes (dw_die_ref);
4204 static hashval_t htab_cu_hash (const void *);
4205 static int htab_cu_eq (const void *, const void *);
4206 static void htab_cu_del (void *);
4207 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4208 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4209 static void add_sibling_attributes (dw_die_ref);
4210 static void build_abbrev_table (dw_die_ref);
4211 static void output_location_lists (dw_die_ref);
4212 static int constant_size (long unsigned);
4213 static unsigned long size_of_die (dw_die_ref);
4214 static void calc_die_sizes (dw_die_ref);
4215 static void mark_dies (dw_die_ref);
4216 static void unmark_dies (dw_die_ref);
4217 static void unmark_all_dies (dw_die_ref);
4218 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4219 static unsigned long size_of_aranges (void);
4220 static enum dwarf_form value_format (dw_attr_ref);
4221 static void output_value_format (dw_attr_ref);
4222 static void output_abbrev_section (void);
4223 static void output_die_symbol (dw_die_ref);
4224 static void output_die (dw_die_ref);
4225 static void output_compilation_unit_header (void);
4226 static void output_comp_unit (dw_die_ref, int);
4227 static const char *dwarf2_name (tree, int);
4228 static void add_pubname (tree, dw_die_ref);
4229 static void add_pubtype (tree, dw_die_ref);
4230 static void output_pubnames (VEC (pubname_entry,gc) *);
4231 static void add_arange (tree, dw_die_ref);
4232 static void output_aranges (void);
4233 static unsigned int add_ranges_num (int);
4234 static unsigned int add_ranges (const_tree);
4235 static unsigned int add_ranges_by_labels (const char *, const char *);
4236 static void output_ranges (void);
4237 static void output_line_info (void);
4238 static void output_file_names (void);
4239 static dw_die_ref base_type_die (tree);
4240 static int is_base_type (tree);
4241 static bool is_subrange_type (const_tree);
4242 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4243 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4244 static int type_is_enum (const_tree);
4245 static unsigned int dbx_reg_number (const_rtx);
4246 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4247 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4248 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int, 
4249                                                 enum var_init_status);
4250 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4251                                                      enum var_init_status);
4252 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4253 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4254                                          enum var_init_status);
4255 static int is_based_loc (const_rtx);
4256 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4257                                             enum var_init_status);
4258 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4259                                                enum var_init_status);
4260 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4261 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4262 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4263 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4264 static tree field_type (const_tree);
4265 static unsigned int simple_type_align_in_bits (const_tree);
4266 static unsigned int simple_decl_align_in_bits (const_tree);
4267 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4268 static HOST_WIDE_INT field_byte_offset (const_tree);
4269 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4270                                          dw_loc_descr_ref);
4271 static void add_data_member_location_attribute (dw_die_ref, tree);
4272 static void add_const_value_attribute (dw_die_ref, rtx);
4273 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4274 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4275 static void insert_float (const_rtx, unsigned char *);
4276 static rtx rtl_for_decl_location (tree);
4277 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4278                                                    enum dwarf_attribute);
4279 static void tree_add_const_value_attribute (dw_die_ref, tree);
4280 static void add_name_attribute (dw_die_ref, const char *);
4281 static void add_comp_dir_attribute (dw_die_ref);
4282 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4283 static void add_subscript_info (dw_die_ref, tree);
4284 static void add_byte_size_attribute (dw_die_ref, tree);
4285 static void add_bit_offset_attribute (dw_die_ref, tree);
4286 static void add_bit_size_attribute (dw_die_ref, tree);
4287 static void add_prototyped_attribute (dw_die_ref, tree);
4288 static void add_abstract_origin_attribute (dw_die_ref, tree);
4289 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4290 static void add_src_coords_attributes (dw_die_ref, tree);
4291 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4292 static void push_decl_scope (tree);
4293 static void pop_decl_scope (void);
4294 static dw_die_ref scope_die_for (tree, dw_die_ref);
4295 static inline int local_scope_p (dw_die_ref);
4296 static inline int class_or_namespace_scope_p (dw_die_ref);
4297 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4298 static void add_calling_convention_attribute (dw_die_ref, tree);
4299 static const char *type_tag (const_tree);
4300 static tree member_declared_type (const_tree);
4301 #if 0
4302 static const char *decl_start_label (tree);
4303 #endif
4304 static void gen_array_type_die (tree, dw_die_ref);
4305 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4306 #if 0
4307 static void gen_entry_point_die (tree, dw_die_ref);
4308 #endif
4309 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4310 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4311 static void gen_inlined_union_type_die (tree, dw_die_ref);
4312 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4313 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4314 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4315 static void gen_formal_types_die (tree, dw_die_ref);
4316 static void gen_subprogram_die (tree, dw_die_ref);
4317 static void gen_variable_die (tree, dw_die_ref);
4318 static void gen_label_die (tree, dw_die_ref);
4319 static void gen_lexical_block_die (tree, dw_die_ref, int);
4320 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4321 static void gen_field_die (tree, dw_die_ref);
4322 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4323 static dw_die_ref gen_compile_unit_die (const char *);
4324 static void gen_inheritance_die (tree, tree, dw_die_ref);
4325 static void gen_member_die (tree, dw_die_ref);
4326 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4327                                                 enum debug_info_usage);
4328 static void gen_subroutine_type_die (tree, dw_die_ref);
4329 static void gen_typedef_die (tree, dw_die_ref);
4330 static void gen_type_die (tree, dw_die_ref);
4331 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4332 static void gen_block_die (tree, dw_die_ref, int);
4333 static void decls_for_scope (tree, dw_die_ref, int);
4334 static int is_redundant_typedef (const_tree);
4335 static void gen_namespace_die (tree);
4336 static void gen_decl_die (tree, dw_die_ref);
4337 static dw_die_ref force_decl_die (tree);
4338 static dw_die_ref force_type_die (tree);
4339 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4340 static void declare_in_namespace (tree, dw_die_ref);
4341 static struct dwarf_file_data * lookup_filename (const char *);
4342 static void retry_incomplete_types (void);
4343 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4344 static void splice_child_die (dw_die_ref, dw_die_ref);
4345 static int file_info_cmp (const void *, const void *);
4346 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4347                                      const char *, const char *, unsigned);
4348 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4349                                        const char *, const char *,
4350                                        const char *);
4351 static void output_loc_list (dw_loc_list_ref);
4352 static char *gen_internal_sym (const char *);
4353
4354 static void prune_unmark_dies (dw_die_ref);
4355 static void prune_unused_types_mark (dw_die_ref, int);
4356 static void prune_unused_types_walk (dw_die_ref);
4357 static void prune_unused_types_walk_attribs (dw_die_ref);
4358 static void prune_unused_types_prune (dw_die_ref);
4359 static void prune_unused_types (void);
4360 static int maybe_emit_file (struct dwarf_file_data *fd);
4361
4362 /* Section names used to hold DWARF debugging information.  */
4363 #ifndef DEBUG_INFO_SECTION
4364 #define DEBUG_INFO_SECTION      ".debug_info"
4365 #endif
4366 #ifndef DEBUG_ABBREV_SECTION
4367 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4368 #endif
4369 #ifndef DEBUG_ARANGES_SECTION
4370 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4371 #endif
4372 #ifndef DEBUG_MACINFO_SECTION
4373 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4374 #endif
4375 #ifndef DEBUG_LINE_SECTION
4376 #define DEBUG_LINE_SECTION      ".debug_line"
4377 #endif
4378 #ifndef DEBUG_LOC_SECTION
4379 #define DEBUG_LOC_SECTION       ".debug_loc"
4380 #endif
4381 #ifndef DEBUG_PUBNAMES_SECTION
4382 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4383 #endif
4384 #ifndef DEBUG_STR_SECTION
4385 #define DEBUG_STR_SECTION       ".debug_str"
4386 #endif
4387 #ifndef DEBUG_RANGES_SECTION
4388 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4389 #endif
4390
4391 /* Standard ELF section names for compiled code and data.  */
4392 #ifndef TEXT_SECTION_NAME
4393 #define TEXT_SECTION_NAME       ".text"
4394 #endif
4395
4396 /* Section flags for .debug_str section.  */
4397 #define DEBUG_STR_SECTION_FLAGS \
4398   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4399    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4400    : SECTION_DEBUG)
4401
4402 /* Labels we insert at beginning sections we can reference instead of
4403    the section names themselves.  */
4404
4405 #ifndef TEXT_SECTION_LABEL
4406 #define TEXT_SECTION_LABEL              "Ltext"
4407 #endif
4408 #ifndef COLD_TEXT_SECTION_LABEL
4409 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4410 #endif
4411 #ifndef DEBUG_LINE_SECTION_LABEL
4412 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4413 #endif
4414 #ifndef DEBUG_INFO_SECTION_LABEL
4415 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4416 #endif
4417 #ifndef DEBUG_ABBREV_SECTION_LABEL
4418 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4419 #endif
4420 #ifndef DEBUG_LOC_SECTION_LABEL
4421 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4422 #endif
4423 #ifndef DEBUG_RANGES_SECTION_LABEL
4424 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4425 #endif
4426 #ifndef DEBUG_MACINFO_SECTION_LABEL
4427 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4428 #endif
4429
4430 /* Definitions of defaults for formats and names of various special
4431    (artificial) labels which may be generated within this file (when the -g
4432    options is used and DWARF2_DEBUGGING_INFO is in effect.
4433    If necessary, these may be overridden from within the tm.h file, but
4434    typically, overriding these defaults is unnecessary.  */
4435
4436 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4437 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4438 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4439 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4440 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4441 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4442 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4443 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4444 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4445 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4446
4447 #ifndef TEXT_END_LABEL
4448 #define TEXT_END_LABEL          "Letext"
4449 #endif
4450 #ifndef COLD_END_LABEL
4451 #define COLD_END_LABEL          "Letext_cold"
4452 #endif
4453 #ifndef BLOCK_BEGIN_LABEL
4454 #define BLOCK_BEGIN_LABEL       "LBB"
4455 #endif
4456 #ifndef BLOCK_END_LABEL
4457 #define BLOCK_END_LABEL         "LBE"
4458 #endif
4459 #ifndef LINE_CODE_LABEL
4460 #define LINE_CODE_LABEL         "LM"
4461 #endif
4462 #ifndef SEPARATE_LINE_CODE_LABEL
4463 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4464 #endif
4465
4466 \f
4467 /* We allow a language front-end to designate a function that is to be
4468    called to "demangle" any name before it is put into a DIE.  */
4469
4470 static const char *(*demangle_name_func) (const char *);
4471
4472 void
4473 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4474 {
4475   demangle_name_func = func;
4476 }
4477
4478 /* Test if rtl node points to a pseudo register.  */
4479
4480 static inline int
4481 is_pseudo_reg (const_rtx rtl)
4482 {
4483   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4484           || (GET_CODE (rtl) == SUBREG
4485               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4486 }
4487
4488 /* Return a reference to a type, with its const and volatile qualifiers
4489    removed.  */
4490
4491 static inline tree
4492 type_main_variant (tree type)
4493 {
4494   type = TYPE_MAIN_VARIANT (type);
4495
4496   /* ??? There really should be only one main variant among any group of
4497      variants of a given type (and all of the MAIN_VARIANT values for all
4498      members of the group should point to that one type) but sometimes the C
4499      front-end messes this up for array types, so we work around that bug
4500      here.  */
4501   if (TREE_CODE (type) == ARRAY_TYPE)
4502     while (type != TYPE_MAIN_VARIANT (type))
4503       type = TYPE_MAIN_VARIANT (type);
4504
4505   return type;
4506 }
4507
4508 /* Return nonzero if the given type node represents a tagged type.  */
4509
4510 static inline int
4511 is_tagged_type (const_tree type)
4512 {
4513   enum tree_code code = TREE_CODE (type);
4514
4515   return (code == RECORD_TYPE || code == UNION_TYPE
4516           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4517 }
4518
4519 /* Convert a DIE tag into its string name.  */
4520
4521 static const char *
4522 dwarf_tag_name (unsigned int tag)
4523 {
4524   switch (tag)
4525     {
4526     case DW_TAG_padding:
4527       return "DW_TAG_padding";
4528     case DW_TAG_array_type:
4529       return "DW_TAG_array_type";
4530     case DW_TAG_class_type:
4531       return "DW_TAG_class_type";
4532     case DW_TAG_entry_point:
4533       return "DW_TAG_entry_point";
4534     case DW_TAG_enumeration_type:
4535       return "DW_TAG_enumeration_type";
4536     case DW_TAG_formal_parameter:
4537       return "DW_TAG_formal_parameter";
4538     case DW_TAG_imported_declaration:
4539       return "DW_TAG_imported_declaration";
4540     case DW_TAG_label:
4541       return "DW_TAG_label";
4542     case DW_TAG_lexical_block:
4543       return "DW_TAG_lexical_block";
4544     case DW_TAG_member:
4545       return "DW_TAG_member";
4546     case DW_TAG_pointer_type:
4547       return "DW_TAG_pointer_type";
4548     case DW_TAG_reference_type:
4549       return "DW_TAG_reference_type";
4550     case DW_TAG_compile_unit:
4551       return "DW_TAG_compile_unit";
4552     case DW_TAG_string_type:
4553       return "DW_TAG_string_type";
4554     case DW_TAG_structure_type:
4555       return "DW_TAG_structure_type";
4556     case DW_TAG_subroutine_type:
4557       return "DW_TAG_subroutine_type";
4558     case DW_TAG_typedef:
4559       return "DW_TAG_typedef";
4560     case DW_TAG_union_type:
4561       return "DW_TAG_union_type";
4562     case DW_TAG_unspecified_parameters:
4563       return "DW_TAG_unspecified_parameters";
4564     case DW_TAG_variant:
4565       return "DW_TAG_variant";
4566     case DW_TAG_common_block:
4567       return "DW_TAG_common_block";
4568     case DW_TAG_common_inclusion:
4569       return "DW_TAG_common_inclusion";
4570     case DW_TAG_inheritance:
4571       return "DW_TAG_inheritance";
4572     case DW_TAG_inlined_subroutine:
4573       return "DW_TAG_inlined_subroutine";
4574     case DW_TAG_module:
4575       return "DW_TAG_module";
4576     case DW_TAG_ptr_to_member_type:
4577       return "DW_TAG_ptr_to_member_type";
4578     case DW_TAG_set_type:
4579       return "DW_TAG_set_type";
4580     case DW_TAG_subrange_type:
4581       return "DW_TAG_subrange_type";
4582     case DW_TAG_with_stmt:
4583       return "DW_TAG_with_stmt";
4584     case DW_TAG_access_declaration:
4585       return "DW_TAG_access_declaration";
4586     case DW_TAG_base_type:
4587       return "DW_TAG_base_type";
4588     case DW_TAG_catch_block:
4589       return "DW_TAG_catch_block";
4590     case DW_TAG_const_type:
4591       return "DW_TAG_const_type";
4592     case DW_TAG_constant:
4593       return "DW_TAG_constant";
4594     case DW_TAG_enumerator:
4595       return "DW_TAG_enumerator";
4596     case DW_TAG_file_type:
4597       return "DW_TAG_file_type";
4598     case DW_TAG_friend:
4599       return "DW_TAG_friend";
4600     case DW_TAG_namelist:
4601       return "DW_TAG_namelist";
4602     case DW_TAG_namelist_item:
4603       return "DW_TAG_namelist_item";
4604     case DW_TAG_packed_type:
4605       return "DW_TAG_packed_type";
4606     case DW_TAG_subprogram:
4607       return "DW_TAG_subprogram";
4608     case DW_TAG_template_type_param:
4609       return "DW_TAG_template_type_param";
4610     case DW_TAG_template_value_param:
4611       return "DW_TAG_template_value_param";
4612     case DW_TAG_thrown_type:
4613       return "DW_TAG_thrown_type";
4614     case DW_TAG_try_block:
4615       return "DW_TAG_try_block";
4616     case DW_TAG_variant_part:
4617       return "DW_TAG_variant_part";
4618     case DW_TAG_variable:
4619       return "DW_TAG_variable";
4620     case DW_TAG_volatile_type:
4621       return "DW_TAG_volatile_type";
4622     case DW_TAG_dwarf_procedure:
4623       return "DW_TAG_dwarf_procedure";
4624     case DW_TAG_restrict_type:
4625       return "DW_TAG_restrict_type";
4626     case DW_TAG_interface_type:
4627       return "DW_TAG_interface_type";
4628     case DW_TAG_namespace:
4629       return "DW_TAG_namespace";
4630     case DW_TAG_imported_module:
4631       return "DW_TAG_imported_module";
4632     case DW_TAG_unspecified_type:
4633       return "DW_TAG_unspecified_type";
4634     case DW_TAG_partial_unit:
4635       return "DW_TAG_partial_unit";
4636     case DW_TAG_imported_unit:
4637       return "DW_TAG_imported_unit";
4638     case DW_TAG_condition:
4639       return "DW_TAG_condition";
4640     case DW_TAG_shared_type:
4641       return "DW_TAG_shared_type";
4642     case DW_TAG_MIPS_loop:
4643       return "DW_TAG_MIPS_loop";
4644     case DW_TAG_format_label:
4645       return "DW_TAG_format_label";
4646     case DW_TAG_function_template:
4647       return "DW_TAG_function_template";
4648     case DW_TAG_class_template:
4649       return "DW_TAG_class_template";
4650     case DW_TAG_GNU_BINCL:
4651       return "DW_TAG_GNU_BINCL";
4652     case DW_TAG_GNU_EINCL:
4653       return "DW_TAG_GNU_EINCL";
4654     default:
4655       return "DW_TAG_<unknown>";
4656     }
4657 }
4658
4659 /* Convert a DWARF attribute code into its string name.  */
4660
4661 static const char *
4662 dwarf_attr_name (unsigned int attr)
4663 {
4664   switch (attr)
4665     {
4666     case DW_AT_sibling:
4667       return "DW_AT_sibling";
4668     case DW_AT_location:
4669       return "DW_AT_location";
4670     case DW_AT_name:
4671       return "DW_AT_name";
4672     case DW_AT_ordering:
4673       return "DW_AT_ordering";
4674     case DW_AT_subscr_data:
4675       return "DW_AT_subscr_data";
4676     case DW_AT_byte_size:
4677       return "DW_AT_byte_size";
4678     case DW_AT_bit_offset:
4679       return "DW_AT_bit_offset";
4680     case DW_AT_bit_size:
4681       return "DW_AT_bit_size";
4682     case DW_AT_element_list:
4683       return "DW_AT_element_list";
4684     case DW_AT_stmt_list:
4685       return "DW_AT_stmt_list";
4686     case DW_AT_low_pc:
4687       return "DW_AT_low_pc";
4688     case DW_AT_high_pc:
4689       return "DW_AT_high_pc";
4690     case DW_AT_language:
4691       return "DW_AT_language";
4692     case DW_AT_member:
4693       return "DW_AT_member";
4694     case DW_AT_discr:
4695       return "DW_AT_discr";
4696     case DW_AT_discr_value:
4697       return "DW_AT_discr_value";
4698     case DW_AT_visibility:
4699       return "DW_AT_visibility";
4700     case DW_AT_import:
4701       return "DW_AT_import";
4702     case DW_AT_string_length:
4703       return "DW_AT_string_length";
4704     case DW_AT_common_reference:
4705       return "DW_AT_common_reference";
4706     case DW_AT_comp_dir:
4707       return "DW_AT_comp_dir";
4708     case DW_AT_const_value:
4709       return "DW_AT_const_value";
4710     case DW_AT_containing_type:
4711       return "DW_AT_containing_type";
4712     case DW_AT_default_value:
4713       return "DW_AT_default_value";
4714     case DW_AT_inline:
4715       return "DW_AT_inline";
4716     case DW_AT_is_optional:
4717       return "DW_AT_is_optional";
4718     case DW_AT_lower_bound:
4719       return "DW_AT_lower_bound";
4720     case DW_AT_producer:
4721       return "DW_AT_producer";
4722     case DW_AT_prototyped:
4723       return "DW_AT_prototyped";
4724     case DW_AT_return_addr:
4725       return "DW_AT_return_addr";
4726     case DW_AT_start_scope:
4727       return "DW_AT_start_scope";
4728     case DW_AT_bit_stride:
4729       return "DW_AT_bit_stride";
4730     case DW_AT_upper_bound:
4731       return "DW_AT_upper_bound";
4732     case DW_AT_abstract_origin:
4733       return "DW_AT_abstract_origin";
4734     case DW_AT_accessibility:
4735       return "DW_AT_accessibility";
4736     case DW_AT_address_class:
4737       return "DW_AT_address_class";
4738     case DW_AT_artificial:
4739       return "DW_AT_artificial";
4740     case DW_AT_base_types:
4741       return "DW_AT_base_types";
4742     case DW_AT_calling_convention:
4743       return "DW_AT_calling_convention";
4744     case DW_AT_count:
4745       return "DW_AT_count";
4746     case DW_AT_data_member_location:
4747       return "DW_AT_data_member_location";
4748     case DW_AT_decl_column:
4749       return "DW_AT_decl_column";
4750     case DW_AT_decl_file:
4751       return "DW_AT_decl_file";
4752     case DW_AT_decl_line:
4753       return "DW_AT_decl_line";
4754     case DW_AT_declaration:
4755       return "DW_AT_declaration";
4756     case DW_AT_discr_list:
4757       return "DW_AT_discr_list";
4758     case DW_AT_encoding:
4759       return "DW_AT_encoding";
4760     case DW_AT_external:
4761       return "DW_AT_external";
4762     case DW_AT_frame_base:
4763       return "DW_AT_frame_base";
4764     case DW_AT_friend:
4765       return "DW_AT_friend";
4766     case DW_AT_identifier_case:
4767       return "DW_AT_identifier_case";
4768     case DW_AT_macro_info:
4769       return "DW_AT_macro_info";
4770     case DW_AT_namelist_items:
4771       return "DW_AT_namelist_items";
4772     case DW_AT_priority:
4773       return "DW_AT_priority";
4774     case DW_AT_segment:
4775       return "DW_AT_segment";
4776     case DW_AT_specification:
4777       return "DW_AT_specification";
4778     case DW_AT_static_link:
4779       return "DW_AT_static_link";
4780     case DW_AT_type:
4781       return "DW_AT_type";
4782     case DW_AT_use_location:
4783       return "DW_AT_use_location";
4784     case DW_AT_variable_parameter:
4785       return "DW_AT_variable_parameter";
4786     case DW_AT_virtuality:
4787       return "DW_AT_virtuality";
4788     case DW_AT_vtable_elem_location:
4789       return "DW_AT_vtable_elem_location";
4790
4791     case DW_AT_allocated:
4792       return "DW_AT_allocated";
4793     case DW_AT_associated:
4794       return "DW_AT_associated";
4795     case DW_AT_data_location:
4796       return "DW_AT_data_location";
4797     case DW_AT_byte_stride:
4798       return "DW_AT_byte_stride";
4799     case DW_AT_entry_pc:
4800       return "DW_AT_entry_pc";
4801     case DW_AT_use_UTF8:
4802       return "DW_AT_use_UTF8";
4803     case DW_AT_extension:
4804       return "DW_AT_extension";
4805     case DW_AT_ranges:
4806       return "DW_AT_ranges";
4807     case DW_AT_trampoline:
4808       return "DW_AT_trampoline";
4809     case DW_AT_call_column:
4810       return "DW_AT_call_column";
4811     case DW_AT_call_file:
4812       return "DW_AT_call_file";
4813     case DW_AT_call_line:
4814       return "DW_AT_call_line";
4815
4816     case DW_AT_MIPS_fde:
4817       return "DW_AT_MIPS_fde";
4818     case DW_AT_MIPS_loop_begin:
4819       return "DW_AT_MIPS_loop_begin";
4820     case DW_AT_MIPS_tail_loop_begin:
4821       return "DW_AT_MIPS_tail_loop_begin";
4822     case DW_AT_MIPS_epilog_begin:
4823       return "DW_AT_MIPS_epilog_begin";
4824     case DW_AT_MIPS_loop_unroll_factor:
4825       return "DW_AT_MIPS_loop_unroll_factor";
4826     case DW_AT_MIPS_software_pipeline_depth:
4827       return "DW_AT_MIPS_software_pipeline_depth";
4828     case DW_AT_MIPS_linkage_name:
4829       return "DW_AT_MIPS_linkage_name";
4830     case DW_AT_MIPS_stride:
4831       return "DW_AT_MIPS_stride";
4832     case DW_AT_MIPS_abstract_name:
4833       return "DW_AT_MIPS_abstract_name";
4834     case DW_AT_MIPS_clone_origin:
4835       return "DW_AT_MIPS_clone_origin";
4836     case DW_AT_MIPS_has_inlines:
4837       return "DW_AT_MIPS_has_inlines";
4838
4839     case DW_AT_sf_names:
4840       return "DW_AT_sf_names";
4841     case DW_AT_src_info:
4842       return "DW_AT_src_info";
4843     case DW_AT_mac_info:
4844       return "DW_AT_mac_info";
4845     case DW_AT_src_coords:
4846       return "DW_AT_src_coords";
4847     case DW_AT_body_begin:
4848       return "DW_AT_body_begin";
4849     case DW_AT_body_end:
4850       return "DW_AT_body_end";
4851     case DW_AT_GNU_vector:
4852       return "DW_AT_GNU_vector";
4853
4854     case DW_AT_VMS_rtnbeg_pd_address:
4855       return "DW_AT_VMS_rtnbeg_pd_address";
4856
4857     default:
4858       return "DW_AT_<unknown>";
4859     }
4860 }
4861
4862 /* Convert a DWARF value form code into its string name.  */
4863
4864 static const char *
4865 dwarf_form_name (unsigned int form)
4866 {
4867   switch (form)
4868     {
4869     case DW_FORM_addr:
4870       return "DW_FORM_addr";
4871     case DW_FORM_block2:
4872       return "DW_FORM_block2";
4873     case DW_FORM_block4:
4874       return "DW_FORM_block4";
4875     case DW_FORM_data2:
4876       return "DW_FORM_data2";
4877     case DW_FORM_data4:
4878       return "DW_FORM_data4";
4879     case DW_FORM_data8:
4880       return "DW_FORM_data8";
4881     case DW_FORM_string:
4882       return "DW_FORM_string";
4883     case DW_FORM_block:
4884       return "DW_FORM_block";
4885     case DW_FORM_block1:
4886       return "DW_FORM_block1";
4887     case DW_FORM_data1:
4888       return "DW_FORM_data1";
4889     case DW_FORM_flag:
4890       return "DW_FORM_flag";
4891     case DW_FORM_sdata:
4892       return "DW_FORM_sdata";
4893     case DW_FORM_strp:
4894       return "DW_FORM_strp";
4895     case DW_FORM_udata:
4896       return "DW_FORM_udata";
4897     case DW_FORM_ref_addr:
4898       return "DW_FORM_ref_addr";
4899     case DW_FORM_ref1:
4900       return "DW_FORM_ref1";
4901     case DW_FORM_ref2:
4902       return "DW_FORM_ref2";
4903     case DW_FORM_ref4:
4904       return "DW_FORM_ref4";
4905     case DW_FORM_ref8:
4906       return "DW_FORM_ref8";
4907     case DW_FORM_ref_udata:
4908       return "DW_FORM_ref_udata";
4909     case DW_FORM_indirect:
4910       return "DW_FORM_indirect";
4911     default:
4912       return "DW_FORM_<unknown>";
4913     }
4914 }
4915 \f
4916 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4917    instance of an inlined instance of a decl which is local to an inline
4918    function, so we have to trace all of the way back through the origin chain
4919    to find out what sort of node actually served as the original seed for the
4920    given block.  */
4921
4922 static tree
4923 decl_ultimate_origin (const_tree decl)
4924 {
4925   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4926     return NULL_TREE;
4927
4928   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4929      nodes in the function to point to themselves; ignore that if
4930      we're trying to output the abstract instance of this function.  */
4931   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4932     return NULL_TREE;
4933
4934   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4935      most distant ancestor, this should never happen.  */
4936   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4937
4938   return DECL_ABSTRACT_ORIGIN (decl);
4939 }
4940
4941 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4942    instance of an inlined instance of a block which is local to an inline
4943    function, so we have to trace all of the way back through the origin chain
4944    to find out what sort of node actually served as the original seed for the
4945    given block.  */
4946
4947 static tree
4948 block_ultimate_origin (const_tree block)
4949 {
4950   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4951
4952   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4953      nodes in the function to point to themselves; ignore that if
4954      we're trying to output the abstract instance of this function.  */
4955   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4956     return NULL_TREE;
4957
4958   if (immediate_origin == NULL_TREE)
4959     return NULL_TREE;
4960   else
4961     {
4962       tree ret_val;
4963       tree lookahead = immediate_origin;
4964
4965       do
4966         {
4967           ret_val = lookahead;
4968           lookahead = (TREE_CODE (ret_val) == BLOCK
4969                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4970         }
4971       while (lookahead != NULL && lookahead != ret_val);
4972
4973       /* The block's abstract origin chain may not be the *ultimate* origin of
4974          the block. It could lead to a DECL that has an abstract origin set.
4975          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4976          will give us if it has one).  Note that DECL's abstract origins are
4977          supposed to be the most distant ancestor (or so decl_ultimate_origin
4978          claims), so we don't need to loop following the DECL origins.  */
4979       if (DECL_P (ret_val))
4980         return DECL_ORIGIN (ret_val);
4981
4982       return ret_val;
4983     }
4984 }
4985
4986 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4987    of a virtual function may refer to a base class, so we check the 'this'
4988    parameter.  */
4989
4990 static tree
4991 decl_class_context (tree decl)
4992 {
4993   tree context = NULL_TREE;
4994
4995   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4996     context = DECL_CONTEXT (decl);
4997   else
4998     context = TYPE_MAIN_VARIANT
4999       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5000
5001   if (context && !TYPE_P (context))
5002     context = NULL_TREE;
5003
5004   return context;
5005 }
5006 \f
5007 /* Add an attribute/value pair to a DIE.  */
5008
5009 static inline void
5010 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5011 {
5012   /* Maybe this should be an assert?  */
5013   if (die == NULL)
5014     return;
5015
5016   if (die->die_attr == NULL)
5017     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5018   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5019 }
5020
5021 static inline enum dw_val_class
5022 AT_class (dw_attr_ref a)
5023 {
5024   return a->dw_attr_val.val_class;
5025 }
5026
5027 /* Add a flag value attribute to a DIE.  */
5028
5029 static inline void
5030 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5031 {
5032   dw_attr_node attr;
5033
5034   attr.dw_attr = attr_kind;
5035   attr.dw_attr_val.val_class = dw_val_class_flag;
5036   attr.dw_attr_val.v.val_flag = flag;
5037   add_dwarf_attr (die, &attr);
5038 }
5039
5040 static inline unsigned
5041 AT_flag (dw_attr_ref a)
5042 {
5043   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5044   return a->dw_attr_val.v.val_flag;
5045 }
5046
5047 /* Add a signed integer attribute value to a DIE.  */
5048
5049 static inline void
5050 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5051 {
5052   dw_attr_node attr;
5053
5054   attr.dw_attr = attr_kind;
5055   attr.dw_attr_val.val_class = dw_val_class_const;
5056   attr.dw_attr_val.v.val_int = int_val;
5057   add_dwarf_attr (die, &attr);
5058 }
5059
5060 static inline HOST_WIDE_INT
5061 AT_int (dw_attr_ref a)
5062 {
5063   gcc_assert (a && AT_class (a) == dw_val_class_const);
5064   return a->dw_attr_val.v.val_int;
5065 }
5066
5067 /* Add an unsigned integer attribute value to a DIE.  */
5068
5069 static inline void
5070 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5071                  unsigned HOST_WIDE_INT unsigned_val)
5072 {
5073   dw_attr_node attr;
5074
5075   attr.dw_attr = attr_kind;
5076   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5077   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5078   add_dwarf_attr (die, &attr);
5079 }
5080
5081 static inline unsigned HOST_WIDE_INT
5082 AT_unsigned (dw_attr_ref a)
5083 {
5084   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5085   return a->dw_attr_val.v.val_unsigned;
5086 }
5087
5088 /* Add an unsigned double integer attribute value to a DIE.  */
5089
5090 static inline void
5091 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5092                   long unsigned int val_hi, long unsigned int val_low)
5093 {
5094   dw_attr_node attr;
5095
5096   attr.dw_attr = attr_kind;
5097   attr.dw_attr_val.val_class = dw_val_class_long_long;
5098   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5099   attr.dw_attr_val.v.val_long_long.low = val_low;
5100   add_dwarf_attr (die, &attr);
5101 }
5102
5103 /* Add a floating point attribute value to a DIE and return it.  */
5104
5105 static inline void
5106 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5107             unsigned int length, unsigned int elt_size, unsigned char *array)
5108 {
5109   dw_attr_node attr;
5110
5111   attr.dw_attr = attr_kind;
5112   attr.dw_attr_val.val_class = dw_val_class_vec;
5113   attr.dw_attr_val.v.val_vec.length = length;
5114   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5115   attr.dw_attr_val.v.val_vec.array = array;
5116   add_dwarf_attr (die, &attr);
5117 }
5118
5119 /* Hash and equality functions for debug_str_hash.  */
5120
5121 static hashval_t
5122 debug_str_do_hash (const void *x)
5123 {
5124   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5125 }
5126
5127 static int
5128 debug_str_eq (const void *x1, const void *x2)
5129 {
5130   return strcmp ((((const struct indirect_string_node *)x1)->str),
5131                  (const char *)x2) == 0;
5132 }
5133
5134 /* Add a string attribute value to a DIE.  */
5135
5136 static inline void
5137 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5138 {
5139   dw_attr_node attr;
5140   struct indirect_string_node *node;
5141   void **slot;
5142
5143   if (! debug_str_hash)
5144     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5145                                       debug_str_eq, NULL);
5146
5147   slot = htab_find_slot_with_hash (debug_str_hash, str,
5148                                    htab_hash_string (str), INSERT);
5149   if (*slot == NULL)
5150     {
5151       node = (struct indirect_string_node *)
5152                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5153       node->str = ggc_strdup (str);
5154       *slot = node;
5155     }
5156   else
5157     node = (struct indirect_string_node *) *slot;
5158
5159   node->refcount++;
5160
5161   attr.dw_attr = attr_kind;
5162   attr.dw_attr_val.val_class = dw_val_class_str;
5163   attr.dw_attr_val.v.val_str = node;
5164   add_dwarf_attr (die, &attr);
5165 }
5166
5167 static inline const char *
5168 AT_string (dw_attr_ref a)
5169 {
5170   gcc_assert (a && AT_class (a) == dw_val_class_str);
5171   return a->dw_attr_val.v.val_str->str;
5172 }
5173
5174 /* Find out whether a string should be output inline in DIE
5175    or out-of-line in .debug_str section.  */
5176
5177 static int
5178 AT_string_form (dw_attr_ref a)
5179 {
5180   struct indirect_string_node *node;
5181   unsigned int len;
5182   char label[32];
5183
5184   gcc_assert (a && AT_class (a) == dw_val_class_str);
5185
5186   node = a->dw_attr_val.v.val_str;
5187   if (node->form)
5188     return node->form;
5189
5190   len = strlen (node->str) + 1;
5191
5192   /* If the string is shorter or equal to the size of the reference, it is
5193      always better to put it inline.  */
5194   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5195     return node->form = DW_FORM_string;
5196
5197   /* If we cannot expect the linker to merge strings in .debug_str
5198      section, only put it into .debug_str if it is worth even in this
5199      single module.  */
5200   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5201       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5202     return node->form = DW_FORM_string;
5203
5204   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5205   ++dw2_string_counter;
5206   node->label = xstrdup (label);
5207
5208   return node->form = DW_FORM_strp;
5209 }
5210
5211 /* Add a DIE reference attribute value to a DIE.  */
5212
5213 static inline void
5214 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5215 {
5216   dw_attr_node attr;
5217
5218   attr.dw_attr = attr_kind;
5219   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5220   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5221   attr.dw_attr_val.v.val_die_ref.external = 0;
5222   add_dwarf_attr (die, &attr);
5223 }
5224
5225 /* Add an AT_specification attribute to a DIE, and also make the back
5226    pointer from the specification to the definition.  */
5227
5228 static inline void
5229 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5230 {
5231   add_AT_die_ref (die, DW_AT_specification, targ_die);
5232   gcc_assert (!targ_die->die_definition);
5233   targ_die->die_definition = die;
5234 }
5235
5236 static inline dw_die_ref
5237 AT_ref (dw_attr_ref a)
5238 {
5239   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5240   return a->dw_attr_val.v.val_die_ref.die;
5241 }
5242
5243 static inline int
5244 AT_ref_external (dw_attr_ref a)
5245 {
5246   if (a && AT_class (a) == dw_val_class_die_ref)
5247     return a->dw_attr_val.v.val_die_ref.external;
5248
5249   return 0;
5250 }
5251
5252 static inline void
5253 set_AT_ref_external (dw_attr_ref a, int i)
5254 {
5255   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5256   a->dw_attr_val.v.val_die_ref.external = i;
5257 }
5258
5259 /* Add an FDE reference attribute value to a DIE.  */
5260
5261 static inline void
5262 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5263 {
5264   dw_attr_node attr;
5265
5266   attr.dw_attr = attr_kind;
5267   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5268   attr.dw_attr_val.v.val_fde_index = targ_fde;
5269   add_dwarf_attr (die, &attr);
5270 }
5271
5272 /* Add a location description attribute value to a DIE.  */
5273
5274 static inline void
5275 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5276 {
5277   dw_attr_node attr;
5278
5279   attr.dw_attr = attr_kind;
5280   attr.dw_attr_val.val_class = dw_val_class_loc;
5281   attr.dw_attr_val.v.val_loc = loc;
5282   add_dwarf_attr (die, &attr);
5283 }
5284
5285 static inline dw_loc_descr_ref
5286 AT_loc (dw_attr_ref a)
5287 {
5288   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5289   return a->dw_attr_val.v.val_loc;
5290 }
5291
5292 static inline void
5293 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5294 {
5295   dw_attr_node attr;
5296
5297   attr.dw_attr = attr_kind;
5298   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5299   attr.dw_attr_val.v.val_loc_list = loc_list;
5300   add_dwarf_attr (die, &attr);
5301   have_location_lists = true;
5302 }
5303
5304 static inline dw_loc_list_ref
5305 AT_loc_list (dw_attr_ref a)
5306 {
5307   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5308   return a->dw_attr_val.v.val_loc_list;
5309 }
5310
5311 /* Add an address constant attribute value to a DIE.  */
5312
5313 static inline void
5314 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5315 {
5316   dw_attr_node attr;
5317
5318   attr.dw_attr = attr_kind;
5319   attr.dw_attr_val.val_class = dw_val_class_addr;
5320   attr.dw_attr_val.v.val_addr = addr;
5321   add_dwarf_attr (die, &attr);
5322 }
5323
5324 /* Get the RTX from to an address DIE attribute.  */
5325
5326 static inline rtx
5327 AT_addr (dw_attr_ref a)
5328 {
5329   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5330   return a->dw_attr_val.v.val_addr;
5331 }
5332
5333 /* Add a file attribute value to a DIE.  */
5334
5335 static inline void
5336 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5337              struct dwarf_file_data *fd)
5338 {
5339   dw_attr_node attr;
5340
5341   attr.dw_attr = attr_kind;
5342   attr.dw_attr_val.val_class = dw_val_class_file;
5343   attr.dw_attr_val.v.val_file = fd;
5344   add_dwarf_attr (die, &attr);
5345 }
5346
5347 /* Get the dwarf_file_data from a file DIE attribute.  */
5348
5349 static inline struct dwarf_file_data *
5350 AT_file (dw_attr_ref a)
5351 {
5352   gcc_assert (a && AT_class (a) == dw_val_class_file);
5353   return a->dw_attr_val.v.val_file;
5354 }
5355
5356 /* Add a label identifier attribute value to a DIE.  */
5357
5358 static inline void
5359 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5360 {
5361   dw_attr_node attr;
5362
5363   attr.dw_attr = attr_kind;
5364   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5365   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5366   add_dwarf_attr (die, &attr);
5367 }
5368
5369 /* Add a section offset attribute value to a DIE, an offset into the
5370    debug_line section.  */
5371
5372 static inline void
5373 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5374                 const char *label)
5375 {
5376   dw_attr_node attr;
5377
5378   attr.dw_attr = attr_kind;
5379   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5380   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5381   add_dwarf_attr (die, &attr);
5382 }
5383
5384 /* Add a section offset attribute value to a DIE, an offset into the
5385    debug_macinfo section.  */
5386
5387 static inline void
5388 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5389                const char *label)
5390 {
5391   dw_attr_node attr;
5392
5393   attr.dw_attr = attr_kind;
5394   attr.dw_attr_val.val_class = dw_val_class_macptr;
5395   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5396   add_dwarf_attr (die, &attr);
5397 }
5398
5399 /* Add an offset attribute value to a DIE.  */
5400
5401 static inline void
5402 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5403                unsigned HOST_WIDE_INT offset)
5404 {
5405   dw_attr_node attr;
5406
5407   attr.dw_attr = attr_kind;
5408   attr.dw_attr_val.val_class = dw_val_class_offset;
5409   attr.dw_attr_val.v.val_offset = offset;
5410   add_dwarf_attr (die, &attr);
5411 }
5412
5413 /* Add an range_list attribute value to a DIE.  */
5414
5415 static void
5416 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5417                    long unsigned int offset)
5418 {
5419   dw_attr_node attr;
5420
5421   attr.dw_attr = attr_kind;
5422   attr.dw_attr_val.val_class = dw_val_class_range_list;
5423   attr.dw_attr_val.v.val_offset = offset;
5424   add_dwarf_attr (die, &attr);
5425 }
5426
5427 static inline const char *
5428 AT_lbl (dw_attr_ref a)
5429 {
5430   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5431                     || AT_class (a) == dw_val_class_lineptr
5432                     || AT_class (a) == dw_val_class_macptr));
5433   return a->dw_attr_val.v.val_lbl_id;
5434 }
5435
5436 /* Get the attribute of type attr_kind.  */
5437
5438 static dw_attr_ref
5439 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5440 {
5441   dw_attr_ref a;
5442   unsigned ix;
5443   dw_die_ref spec = NULL;
5444
5445   if (! die)
5446     return NULL;
5447
5448   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5449     if (a->dw_attr == attr_kind)
5450       return a;
5451     else if (a->dw_attr == DW_AT_specification
5452              || a->dw_attr == DW_AT_abstract_origin)
5453       spec = AT_ref (a);
5454
5455   if (spec)
5456     return get_AT (spec, attr_kind);
5457
5458   return NULL;
5459 }
5460
5461 /* Return the "low pc" attribute value, typically associated with a subprogram
5462    DIE.  Return null if the "low pc" attribute is either not present, or if it
5463    cannot be represented as an assembler label identifier.  */
5464
5465 static inline const char *
5466 get_AT_low_pc (dw_die_ref die)
5467 {
5468   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5469
5470   return a ? AT_lbl (a) : NULL;
5471 }
5472
5473 /* Return the "high pc" attribute value, typically associated with a subprogram
5474    DIE.  Return null if the "high pc" attribute is either not present, or if it
5475    cannot be represented as an assembler label identifier.  */
5476
5477 static inline const char *
5478 get_AT_hi_pc (dw_die_ref die)
5479 {
5480   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5481
5482   return a ? AT_lbl (a) : NULL;
5483 }
5484
5485 /* Return the value of the string attribute designated by ATTR_KIND, or
5486    NULL if it is not present.  */
5487
5488 static inline const char *
5489 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5490 {
5491   dw_attr_ref a = get_AT (die, attr_kind);
5492
5493   return a ? AT_string (a) : NULL;
5494 }
5495
5496 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5497    if it is not present.  */
5498
5499 static inline int
5500 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5501 {
5502   dw_attr_ref a = get_AT (die, attr_kind);
5503
5504   return a ? AT_flag (a) : 0;
5505 }
5506
5507 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5508    if it is not present.  */
5509
5510 static inline unsigned
5511 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5512 {
5513   dw_attr_ref a = get_AT (die, attr_kind);
5514
5515   return a ? AT_unsigned (a) : 0;
5516 }
5517
5518 static inline dw_die_ref
5519 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5520 {
5521   dw_attr_ref a = get_AT (die, attr_kind);
5522
5523   return a ? AT_ref (a) : NULL;
5524 }
5525
5526 static inline struct dwarf_file_data *
5527 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5528 {
5529   dw_attr_ref a = get_AT (die, attr_kind);
5530
5531   return a ? AT_file (a) : NULL;
5532 }
5533
5534 /* Return TRUE if the language is C or C++.  */
5535
5536 static inline bool
5537 is_c_family (void)
5538 {
5539   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5540
5541   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5542           || lang == DW_LANG_C99
5543           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5544 }
5545
5546 /* Return TRUE if the language is C++.  */
5547
5548 static inline bool
5549 is_cxx (void)
5550 {
5551   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5552
5553   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5554 }
5555
5556 /* Return TRUE if the language is Fortran.  */
5557
5558 static inline bool
5559 is_fortran (void)
5560 {
5561   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5562
5563   return (lang == DW_LANG_Fortran77
5564           || lang == DW_LANG_Fortran90
5565           || lang == DW_LANG_Fortran95);
5566 }
5567
5568 /* Return TRUE if the language is Java.  */
5569
5570 static inline bool
5571 is_java (void)
5572 {
5573   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5574
5575   return lang == DW_LANG_Java;
5576 }
5577
5578 /* Return TRUE if the language is Ada.  */
5579
5580 static inline bool
5581 is_ada (void)
5582 {
5583   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5584
5585   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5586 }
5587
5588 /* Remove the specified attribute if present.  */
5589
5590 static void
5591 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5592 {
5593   dw_attr_ref a;
5594   unsigned ix;
5595
5596   if (! die)
5597     return;
5598
5599   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5600     if (a->dw_attr == attr_kind)
5601       {
5602         if (AT_class (a) == dw_val_class_str)
5603           if (a->dw_attr_val.v.val_str->refcount)
5604             a->dw_attr_val.v.val_str->refcount--;
5605
5606         /* VEC_ordered_remove should help reduce the number of abbrevs
5607            that are needed.  */
5608         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5609         return;
5610       }
5611 }
5612
5613 /* Remove CHILD from its parent.  PREV must have the property that
5614    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5615
5616 static void
5617 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5618 {
5619   gcc_assert (child->die_parent == prev->die_parent);
5620   gcc_assert (prev->die_sib == child);
5621   if (prev == child)
5622     {
5623       gcc_assert (child->die_parent->die_child == child);
5624       prev = NULL;
5625     }
5626   else
5627     prev->die_sib = child->die_sib;
5628   if (child->die_parent->die_child == child)
5629     child->die_parent->die_child = prev;
5630 }
5631
5632 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5633    matches TAG.  */
5634
5635 static void
5636 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5637 {
5638   dw_die_ref c;
5639
5640   c = die->die_child;
5641   if (c) do {
5642     dw_die_ref prev = c;
5643     c = c->die_sib;
5644     while (c->die_tag == tag)
5645       {
5646         remove_child_with_prev (c, prev);
5647         /* Might have removed every child.  */
5648         if (c == c->die_sib)
5649           return;
5650         c = c->die_sib;
5651       }
5652   } while (c != die->die_child);
5653 }
5654
5655 /* Add a CHILD_DIE as the last child of DIE.  */
5656
5657 static void
5658 add_child_die (dw_die_ref die, dw_die_ref child_die)
5659 {
5660   /* FIXME this should probably be an assert.  */
5661   if (! die || ! child_die)
5662     return;
5663   gcc_assert (die != child_die);
5664
5665   child_die->die_parent = die;
5666   if (die->die_child)
5667     {
5668       child_die->die_sib = die->die_child->die_sib;
5669       die->die_child->die_sib = child_die;
5670     }
5671   else
5672     child_die->die_sib = child_die;
5673   die->die_child = child_die;
5674 }
5675
5676 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5677    is the specification, to the end of PARENT's list of children.
5678    This is done by removing and re-adding it.  */
5679
5680 static void
5681 splice_child_die (dw_die_ref parent, dw_die_ref child)
5682 {
5683   dw_die_ref p;
5684
5685   /* We want the declaration DIE from inside the class, not the
5686      specification DIE at toplevel.  */
5687   if (child->die_parent != parent)
5688     {
5689       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5690
5691       if (tmp)
5692         child = tmp;
5693     }
5694
5695   gcc_assert (child->die_parent == parent
5696               || (child->die_parent
5697                   == get_AT_ref (parent, DW_AT_specification)));
5698
5699   for (p = child->die_parent->die_child; ; p = p->die_sib)
5700     if (p->die_sib == child)
5701       {
5702         remove_child_with_prev (child, p);
5703         break;
5704       }
5705
5706   add_child_die (parent, child);
5707 }
5708
5709 /* Return a pointer to a newly created DIE node.  */
5710
5711 static inline dw_die_ref
5712 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5713 {
5714   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5715
5716   die->die_tag = tag_value;
5717
5718   if (parent_die != NULL)
5719     add_child_die (parent_die, die);
5720   else
5721     {
5722       limbo_die_node *limbo_node;
5723
5724       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5725       limbo_node->die = die;
5726       limbo_node->created_for = t;
5727       limbo_node->next = limbo_die_list;
5728       limbo_die_list = limbo_node;
5729     }
5730
5731   return die;
5732 }
5733
5734 /* Return the DIE associated with the given type specifier.  */
5735
5736 static inline dw_die_ref
5737 lookup_type_die (tree type)
5738 {
5739   return TYPE_SYMTAB_DIE (type);
5740 }
5741
5742 /* Equate a DIE to a given type specifier.  */
5743
5744 static inline void
5745 equate_type_number_to_die (tree type, dw_die_ref type_die)
5746 {
5747   TYPE_SYMTAB_DIE (type) = type_die;
5748 }
5749
5750 /* Returns a hash value for X (which really is a die_struct).  */
5751
5752 static hashval_t
5753 decl_die_table_hash (const void *x)
5754 {
5755   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5756 }
5757
5758 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5759
5760 static int
5761 decl_die_table_eq (const void *x, const void *y)
5762 {
5763   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5764 }
5765
5766 /* Return the DIE associated with a given declaration.  */
5767
5768 static inline dw_die_ref
5769 lookup_decl_die (tree decl)
5770 {
5771   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5772 }
5773
5774 /* Returns a hash value for X (which really is a var_loc_list).  */
5775
5776 static hashval_t
5777 decl_loc_table_hash (const void *x)
5778 {
5779   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5780 }
5781
5782 /* Return nonzero if decl_id of var_loc_list X is the same as
5783    UID of decl *Y.  */
5784
5785 static int
5786 decl_loc_table_eq (const void *x, const void *y)
5787 {
5788   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5789 }
5790
5791 /* Return the var_loc list associated with a given declaration.  */
5792
5793 static inline var_loc_list *
5794 lookup_decl_loc (const_tree decl)
5795 {
5796   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5797 }
5798
5799 /* Equate a DIE to a particular declaration.  */
5800
5801 static void
5802 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5803 {
5804   unsigned int decl_id = DECL_UID (decl);
5805   void **slot;
5806
5807   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5808   *slot = decl_die;
5809   decl_die->decl_id = decl_id;
5810 }
5811
5812 /* Add a variable location node to the linked list for DECL.  */
5813
5814 static void
5815 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5816 {
5817   unsigned int decl_id = DECL_UID (decl);
5818   var_loc_list *temp;
5819   void **slot;
5820
5821   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5822   if (*slot == NULL)
5823     {
5824       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5825       temp->decl_id = decl_id;
5826       *slot = temp;
5827     }
5828   else
5829     temp = *slot;
5830
5831   if (temp->last)
5832     {
5833       /* If the current location is the same as the end of the list,
5834          and either both or neither of the locations is uninitialized,
5835          we have nothing to do.  */
5836       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5837                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5838           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5839                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5840               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5841                    == VAR_INIT_STATUS_UNINITIALIZED)
5842                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5843                       == VAR_INIT_STATUS_UNINITIALIZED))))
5844         {
5845           /* Add LOC to the end of list and update LAST.  */
5846           temp->last->next = loc;
5847           temp->last = loc;
5848         }
5849     }
5850   /* Do not add empty location to the beginning of the list.  */
5851   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5852     {
5853       temp->first = loc;
5854       temp->last = loc;
5855     }
5856 }
5857 \f
5858 /* Keep track of the number of spaces used to indent the
5859    output of the debugging routines that print the structure of
5860    the DIE internal representation.  */
5861 static int print_indent;
5862
5863 /* Indent the line the number of spaces given by print_indent.  */
5864
5865 static inline void
5866 print_spaces (FILE *outfile)
5867 {
5868   fprintf (outfile, "%*s", print_indent, "");
5869 }
5870
5871 /* Print the information associated with a given DIE, and its children.
5872    This routine is a debugging aid only.  */
5873
5874 static void
5875 print_die (dw_die_ref die, FILE *outfile)
5876 {
5877   dw_attr_ref a;
5878   dw_die_ref c;
5879   unsigned ix;
5880
5881   print_spaces (outfile);
5882   fprintf (outfile, "DIE %4ld: %s\n",
5883            die->die_offset, dwarf_tag_name (die->die_tag));
5884   print_spaces (outfile);
5885   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5886   fprintf (outfile, " offset: %ld\n", die->die_offset);
5887
5888   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5889     {
5890       print_spaces (outfile);
5891       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5892
5893       switch (AT_class (a))
5894         {
5895         case dw_val_class_addr:
5896           fprintf (outfile, "address");
5897           break;
5898         case dw_val_class_offset:
5899           fprintf (outfile, "offset");
5900           break;
5901         case dw_val_class_loc:
5902           fprintf (outfile, "location descriptor");
5903           break;
5904         case dw_val_class_loc_list:
5905           fprintf (outfile, "location list -> label:%s",
5906                    AT_loc_list (a)->ll_symbol);
5907           break;
5908         case dw_val_class_range_list:
5909           fprintf (outfile, "range list");
5910           break;
5911         case dw_val_class_const:
5912           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5913           break;
5914         case dw_val_class_unsigned_const:
5915           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5916           break;
5917         case dw_val_class_long_long:
5918           fprintf (outfile, "constant (%lu,%lu)",
5919                    a->dw_attr_val.v.val_long_long.hi,
5920                    a->dw_attr_val.v.val_long_long.low);
5921           break;
5922         case dw_val_class_vec:
5923           fprintf (outfile, "floating-point or vector constant");
5924           break;
5925         case dw_val_class_flag:
5926           fprintf (outfile, "%u", AT_flag (a));
5927           break;
5928         case dw_val_class_die_ref:
5929           if (AT_ref (a) != NULL)
5930             {
5931               if (AT_ref (a)->die_symbol)
5932                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5933               else
5934                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5935             }
5936           else
5937             fprintf (outfile, "die -> <null>");
5938           break;
5939         case dw_val_class_lbl_id:
5940         case dw_val_class_lineptr:
5941         case dw_val_class_macptr:
5942           fprintf (outfile, "label: %s", AT_lbl (a));
5943           break;
5944         case dw_val_class_str:
5945           if (AT_string (a) != NULL)
5946             fprintf (outfile, "\"%s\"", AT_string (a));
5947           else
5948             fprintf (outfile, "<null>");
5949           break;
5950         case dw_val_class_file:
5951           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5952                    AT_file (a)->emitted_number);
5953           break;
5954         default:
5955           break;
5956         }
5957
5958       fprintf (outfile, "\n");
5959     }
5960
5961   if (die->die_child != NULL)
5962     {
5963       print_indent += 4;
5964       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5965       print_indent -= 4;
5966     }
5967   if (print_indent == 0)
5968     fprintf (outfile, "\n");
5969 }
5970
5971 /* Print the contents of the source code line number correspondence table.
5972    This routine is a debugging aid only.  */
5973
5974 static void
5975 print_dwarf_line_table (FILE *outfile)
5976 {
5977   unsigned i;
5978   dw_line_info_ref line_info;
5979
5980   fprintf (outfile, "\n\nDWARF source line information\n");
5981   for (i = 1; i < line_info_table_in_use; i++)
5982     {
5983       line_info = &line_info_table[i];
5984       fprintf (outfile, "%5d: %4ld %6ld\n", i,
5985                line_info->dw_file_num,
5986                line_info->dw_line_num);
5987     }
5988
5989   fprintf (outfile, "\n\n");
5990 }
5991
5992 /* Print the information collected for a given DIE.  */
5993
5994 void
5995 debug_dwarf_die (dw_die_ref die)
5996 {
5997   print_die (die, stderr);
5998 }
5999
6000 /* Print all DWARF information collected for the compilation unit.
6001    This routine is a debugging aid only.  */
6002
6003 void
6004 debug_dwarf (void)
6005 {
6006   print_indent = 0;
6007   print_die (comp_unit_die, stderr);
6008   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6009     print_dwarf_line_table (stderr);
6010 }
6011 \f
6012 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6013    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6014    DIE that marks the start of the DIEs for this include file.  */
6015
6016 static dw_die_ref
6017 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6018 {
6019   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6020   dw_die_ref new_unit = gen_compile_unit_die (filename);
6021
6022   new_unit->die_sib = old_unit;
6023   return new_unit;
6024 }
6025
6026 /* Close an include-file CU and reopen the enclosing one.  */
6027
6028 static dw_die_ref
6029 pop_compile_unit (dw_die_ref old_unit)
6030 {
6031   dw_die_ref new_unit = old_unit->die_sib;
6032
6033   old_unit->die_sib = NULL;
6034   return new_unit;
6035 }
6036
6037 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6038 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6039
6040 /* Calculate the checksum of a location expression.  */
6041
6042 static inline void
6043 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6044 {
6045   CHECKSUM (loc->dw_loc_opc);
6046   CHECKSUM (loc->dw_loc_oprnd1);
6047   CHECKSUM (loc->dw_loc_oprnd2);
6048 }
6049
6050 /* Calculate the checksum of an attribute.  */
6051
6052 static void
6053 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6054 {
6055   dw_loc_descr_ref loc;
6056   rtx r;
6057
6058   CHECKSUM (at->dw_attr);
6059
6060   /* We don't care that this was compiled with a different compiler
6061      snapshot; if the output is the same, that's what matters.  */
6062   if (at->dw_attr == DW_AT_producer)
6063     return;
6064
6065   switch (AT_class (at))
6066     {
6067     case dw_val_class_const:
6068       CHECKSUM (at->dw_attr_val.v.val_int);
6069       break;
6070     case dw_val_class_unsigned_const:
6071       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6072       break;
6073     case dw_val_class_long_long:
6074       CHECKSUM (at->dw_attr_val.v.val_long_long);
6075       break;
6076     case dw_val_class_vec:
6077       CHECKSUM (at->dw_attr_val.v.val_vec);
6078       break;
6079     case dw_val_class_flag:
6080       CHECKSUM (at->dw_attr_val.v.val_flag);
6081       break;
6082     case dw_val_class_str:
6083       CHECKSUM_STRING (AT_string (at));
6084       break;
6085
6086     case dw_val_class_addr:
6087       r = AT_addr (at);
6088       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6089       CHECKSUM_STRING (XSTR (r, 0));
6090       break;
6091
6092     case dw_val_class_offset:
6093       CHECKSUM (at->dw_attr_val.v.val_offset);
6094       break;
6095
6096     case dw_val_class_loc:
6097       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6098         loc_checksum (loc, ctx);
6099       break;
6100
6101     case dw_val_class_die_ref:
6102       die_checksum (AT_ref (at), ctx, mark);
6103       break;
6104
6105     case dw_val_class_fde_ref:
6106     case dw_val_class_lbl_id:
6107     case dw_val_class_lineptr:
6108     case dw_val_class_macptr:
6109       break;
6110
6111     case dw_val_class_file:
6112       CHECKSUM_STRING (AT_file (at)->filename);
6113       break;
6114
6115     default:
6116       break;
6117     }
6118 }
6119
6120 /* Calculate the checksum of a DIE.  */
6121
6122 static void
6123 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6124 {
6125   dw_die_ref c;
6126   dw_attr_ref a;
6127   unsigned ix;
6128
6129   /* To avoid infinite recursion.  */
6130   if (die->die_mark)
6131     {
6132       CHECKSUM (die->die_mark);
6133       return;
6134     }
6135   die->die_mark = ++(*mark);
6136
6137   CHECKSUM (die->die_tag);
6138
6139   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6140     attr_checksum (a, ctx, mark);
6141
6142   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6143 }
6144
6145 #undef CHECKSUM
6146 #undef CHECKSUM_STRING
6147
6148 /* Do the location expressions look same?  */
6149 static inline int
6150 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6151 {
6152   return loc1->dw_loc_opc == loc2->dw_loc_opc
6153          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6154          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6155 }
6156
6157 /* Do the values look the same?  */
6158 static int
6159 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6160 {
6161   dw_loc_descr_ref loc1, loc2;
6162   rtx r1, r2;
6163
6164   if (v1->val_class != v2->val_class)
6165     return 0;
6166
6167   switch (v1->val_class)
6168     {
6169     case dw_val_class_const:
6170       return v1->v.val_int == v2->v.val_int;
6171     case dw_val_class_unsigned_const:
6172       return v1->v.val_unsigned == v2->v.val_unsigned;
6173     case dw_val_class_long_long:
6174       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6175              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6176     case dw_val_class_vec:
6177       if (v1->v.val_vec.length != v2->v.val_vec.length
6178           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6179         return 0;
6180       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6181                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6182         return 0;
6183       return 1;
6184     case dw_val_class_flag:
6185       return v1->v.val_flag == v2->v.val_flag;
6186     case dw_val_class_str:
6187       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6188
6189     case dw_val_class_addr:
6190       r1 = v1->v.val_addr;
6191       r2 = v2->v.val_addr;
6192       if (GET_CODE (r1) != GET_CODE (r2))
6193         return 0;
6194       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6195       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6196
6197     case dw_val_class_offset:
6198       return v1->v.val_offset == v2->v.val_offset;
6199
6200     case dw_val_class_loc:
6201       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6202            loc1 && loc2;
6203            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6204         if (!same_loc_p (loc1, loc2, mark))
6205           return 0;
6206       return !loc1 && !loc2;
6207
6208     case dw_val_class_die_ref:
6209       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6210
6211     case dw_val_class_fde_ref:
6212     case dw_val_class_lbl_id:
6213     case dw_val_class_lineptr:
6214     case dw_val_class_macptr:
6215       return 1;
6216
6217     case dw_val_class_file:
6218       return v1->v.val_file == v2->v.val_file;
6219
6220     default:
6221       return 1;
6222     }
6223 }
6224
6225 /* Do the attributes look the same?  */
6226
6227 static int
6228 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6229 {
6230   if (at1->dw_attr != at2->dw_attr)
6231     return 0;
6232
6233   /* We don't care that this was compiled with a different compiler
6234      snapshot; if the output is the same, that's what matters. */
6235   if (at1->dw_attr == DW_AT_producer)
6236     return 1;
6237
6238   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6239 }
6240
6241 /* Do the dies look the same?  */
6242
6243 static int
6244 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6245 {
6246   dw_die_ref c1, c2;
6247   dw_attr_ref a1;
6248   unsigned ix;
6249
6250   /* To avoid infinite recursion.  */
6251   if (die1->die_mark)
6252     return die1->die_mark == die2->die_mark;
6253   die1->die_mark = die2->die_mark = ++(*mark);
6254
6255   if (die1->die_tag != die2->die_tag)
6256     return 0;
6257
6258   if (VEC_length (dw_attr_node, die1->die_attr)
6259       != VEC_length (dw_attr_node, die2->die_attr))
6260     return 0;
6261
6262   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6263     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6264       return 0;
6265
6266   c1 = die1->die_child;
6267   c2 = die2->die_child;
6268   if (! c1)
6269     {
6270       if (c2)
6271         return 0;
6272     }
6273   else
6274     for (;;)
6275       {
6276         if (!same_die_p (c1, c2, mark))
6277           return 0;
6278         c1 = c1->die_sib;
6279         c2 = c2->die_sib;
6280         if (c1 == die1->die_child)
6281           {
6282             if (c2 == die2->die_child)
6283               break;
6284             else
6285               return 0;
6286           }
6287     }
6288
6289   return 1;
6290 }
6291
6292 /* Do the dies look the same?  Wrapper around same_die_p.  */
6293
6294 static int
6295 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6296 {
6297   int mark = 0;
6298   int ret = same_die_p (die1, die2, &mark);
6299
6300   unmark_all_dies (die1);
6301   unmark_all_dies (die2);
6302
6303   return ret;
6304 }
6305
6306 /* The prefix to attach to symbols on DIEs in the current comdat debug
6307    info section.  */
6308 static char *comdat_symbol_id;
6309
6310 /* The index of the current symbol within the current comdat CU.  */
6311 static unsigned int comdat_symbol_number;
6312
6313 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6314    children, and set comdat_symbol_id accordingly.  */
6315
6316 static void
6317 compute_section_prefix (dw_die_ref unit_die)
6318 {
6319   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6320   const char *base = die_name ? lbasename (die_name) : "anonymous";
6321   char *name = alloca (strlen (base) + 64);
6322   char *p;
6323   int i, mark;
6324   unsigned char checksum[16];
6325   struct md5_ctx ctx;
6326
6327   /* Compute the checksum of the DIE, then append part of it as hex digits to
6328      the name filename of the unit.  */
6329
6330   md5_init_ctx (&ctx);
6331   mark = 0;
6332   die_checksum (unit_die, &ctx, &mark);
6333   unmark_all_dies (unit_die);
6334   md5_finish_ctx (&ctx, checksum);
6335
6336   sprintf (name, "%s.", base);
6337   clean_symbol_name (name);
6338
6339   p = name + strlen (name);
6340   for (i = 0; i < 4; i++)
6341     {
6342       sprintf (p, "%.2x", checksum[i]);
6343       p += 2;
6344     }
6345
6346   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6347   comdat_symbol_number = 0;
6348 }
6349
6350 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6351
6352 static int
6353 is_type_die (dw_die_ref die)
6354 {
6355   switch (die->die_tag)
6356     {
6357     case DW_TAG_array_type:
6358     case DW_TAG_class_type:
6359     case DW_TAG_interface_type:
6360     case DW_TAG_enumeration_type:
6361     case DW_TAG_pointer_type:
6362     case DW_TAG_reference_type:
6363     case DW_TAG_string_type:
6364     case DW_TAG_structure_type:
6365     case DW_TAG_subroutine_type:
6366     case DW_TAG_union_type:
6367     case DW_TAG_ptr_to_member_type:
6368     case DW_TAG_set_type:
6369     case DW_TAG_subrange_type:
6370     case DW_TAG_base_type:
6371     case DW_TAG_const_type:
6372     case DW_TAG_file_type:
6373     case DW_TAG_packed_type:
6374     case DW_TAG_volatile_type:
6375     case DW_TAG_typedef:
6376       return 1;
6377     default:
6378       return 0;
6379     }
6380 }
6381
6382 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6383    Basically, we want to choose the bits that are likely to be shared between
6384    compilations (types) and leave out the bits that are specific to individual
6385    compilations (functions).  */
6386
6387 static int
6388 is_comdat_die (dw_die_ref c)
6389 {
6390   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6391      we do for stabs.  The advantage is a greater likelihood of sharing between
6392      objects that don't include headers in the same order (and therefore would
6393      put the base types in a different comdat).  jason 8/28/00 */
6394
6395   if (c->die_tag == DW_TAG_base_type)
6396     return 0;
6397
6398   if (c->die_tag == DW_TAG_pointer_type
6399       || c->die_tag == DW_TAG_reference_type
6400       || c->die_tag == DW_TAG_const_type
6401       || c->die_tag == DW_TAG_volatile_type)
6402     {
6403       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6404
6405       return t ? is_comdat_die (t) : 0;
6406     }
6407
6408   return is_type_die (c);
6409 }
6410
6411 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6412    compilation unit.  */
6413
6414 static int
6415 is_symbol_die (dw_die_ref c)
6416 {
6417   return (is_type_die (c)
6418           || (get_AT (c, DW_AT_declaration)
6419               && !get_AT (c, DW_AT_specification))
6420           || c->die_tag == DW_TAG_namespace);
6421 }
6422
6423 static char *
6424 gen_internal_sym (const char *prefix)
6425 {
6426   char buf[256];
6427
6428   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6429   return xstrdup (buf);
6430 }
6431
6432 /* Assign symbols to all worthy DIEs under DIE.  */
6433
6434 static void
6435 assign_symbol_names (dw_die_ref die)
6436 {
6437   dw_die_ref c;
6438
6439   if (is_symbol_die (die))
6440     {
6441       if (comdat_symbol_id)
6442         {
6443           char *p = alloca (strlen (comdat_symbol_id) + 64);
6444
6445           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6446                    comdat_symbol_id, comdat_symbol_number++);
6447           die->die_symbol = xstrdup (p);
6448         }
6449       else
6450         die->die_symbol = gen_internal_sym ("LDIE");
6451     }
6452
6453   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6454 }
6455
6456 struct cu_hash_table_entry
6457 {
6458   dw_die_ref cu;
6459   unsigned min_comdat_num, max_comdat_num;
6460   struct cu_hash_table_entry *next;
6461 };
6462
6463 /* Routines to manipulate hash table of CUs.  */
6464 static hashval_t
6465 htab_cu_hash (const void *of)
6466 {
6467   const struct cu_hash_table_entry *entry = of;
6468
6469   return htab_hash_string (entry->cu->die_symbol);
6470 }
6471
6472 static int
6473 htab_cu_eq (const void *of1, const void *of2)
6474 {
6475   const struct cu_hash_table_entry *entry1 = of1;
6476   const struct die_struct *entry2 = of2;
6477
6478   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6479 }
6480
6481 static void
6482 htab_cu_del (void *what)
6483 {
6484   struct cu_hash_table_entry *next, *entry = what;
6485
6486   while (entry)
6487     {
6488       next = entry->next;
6489       free (entry);
6490       entry = next;
6491     }
6492 }
6493
6494 /* Check whether we have already seen this CU and set up SYM_NUM
6495    accordingly.  */
6496 static int
6497 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6498 {
6499   struct cu_hash_table_entry dummy;
6500   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6501
6502   dummy.max_comdat_num = 0;
6503
6504   slot = (struct cu_hash_table_entry **)
6505     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6506         INSERT);
6507   entry = *slot;
6508
6509   for (; entry; last = entry, entry = entry->next)
6510     {
6511       if (same_die_p_wrap (cu, entry->cu))
6512         break;
6513     }
6514
6515   if (entry)
6516     {
6517       *sym_num = entry->min_comdat_num;
6518       return 1;
6519     }
6520
6521   entry = XCNEW (struct cu_hash_table_entry);
6522   entry->cu = cu;
6523   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6524   entry->next = *slot;
6525   *slot = entry;
6526
6527   return 0;
6528 }
6529
6530 /* Record SYM_NUM to record of CU in HTABLE.  */
6531 static void
6532 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6533 {
6534   struct cu_hash_table_entry **slot, *entry;
6535
6536   slot = (struct cu_hash_table_entry **)
6537     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6538         NO_INSERT);
6539   entry = *slot;
6540
6541   entry->max_comdat_num = sym_num;
6542 }
6543
6544 /* Traverse the DIE (which is always comp_unit_die), and set up
6545    additional compilation units for each of the include files we see
6546    bracketed by BINCL/EINCL.  */
6547
6548 static void
6549 break_out_includes (dw_die_ref die)
6550 {
6551   dw_die_ref c;
6552   dw_die_ref unit = NULL;
6553   limbo_die_node *node, **pnode;
6554   htab_t cu_hash_table;
6555
6556   c = die->die_child;
6557   if (c) do {
6558     dw_die_ref prev = c;
6559     c = c->die_sib;
6560     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6561            || (unit && is_comdat_die (c)))
6562       {
6563         dw_die_ref next = c->die_sib;
6564
6565         /* This DIE is for a secondary CU; remove it from the main one.  */
6566         remove_child_with_prev (c, prev);
6567
6568         if (c->die_tag == DW_TAG_GNU_BINCL)
6569           unit = push_new_compile_unit (unit, c);
6570         else if (c->die_tag == DW_TAG_GNU_EINCL)
6571           unit = pop_compile_unit (unit);
6572         else
6573           add_child_die (unit, c);
6574         c = next;
6575         if (c == die->die_child)
6576           break;
6577       }
6578   } while (c != die->die_child);
6579
6580 #if 0
6581   /* We can only use this in debugging, since the frontend doesn't check
6582      to make sure that we leave every include file we enter.  */
6583   gcc_assert (!unit);
6584 #endif
6585
6586   assign_symbol_names (die);
6587   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6588   for (node = limbo_die_list, pnode = &limbo_die_list;
6589        node;
6590        node = node->next)
6591     {
6592       int is_dupl;
6593
6594       compute_section_prefix (node->die);
6595       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6596                         &comdat_symbol_number);
6597       assign_symbol_names (node->die);
6598       if (is_dupl)
6599         *pnode = node->next;
6600       else
6601         {
6602           pnode = &node->next;
6603           record_comdat_symbol_number (node->die, cu_hash_table,
6604                 comdat_symbol_number);
6605         }
6606     }
6607   htab_delete (cu_hash_table);
6608 }
6609
6610 /* Traverse the DIE and add a sibling attribute if it may have the
6611    effect of speeding up access to siblings.  To save some space,
6612    avoid generating sibling attributes for DIE's without children.  */
6613
6614 static void
6615 add_sibling_attributes (dw_die_ref die)
6616 {
6617   dw_die_ref c;
6618
6619   if (! die->die_child)
6620     return;
6621
6622   if (die->die_parent && die != die->die_parent->die_child)
6623     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6624
6625   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6626 }
6627
6628 /* Output all location lists for the DIE and its children.  */
6629
6630 static void
6631 output_location_lists (dw_die_ref die)
6632 {
6633   dw_die_ref c;
6634   dw_attr_ref a;
6635   unsigned ix;
6636
6637   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6638     if (AT_class (a) == dw_val_class_loc_list)
6639       output_loc_list (AT_loc_list (a));
6640
6641   FOR_EACH_CHILD (die, c, output_location_lists (c));
6642 }
6643
6644 /* The format of each DIE (and its attribute value pairs) is encoded in an
6645    abbreviation table.  This routine builds the abbreviation table and assigns
6646    a unique abbreviation id for each abbreviation entry.  The children of each
6647    die are visited recursively.  */
6648
6649 static void
6650 build_abbrev_table (dw_die_ref die)
6651 {
6652   unsigned long abbrev_id;
6653   unsigned int n_alloc;
6654   dw_die_ref c;
6655   dw_attr_ref a;
6656   unsigned ix;
6657
6658   /* Scan the DIE references, and mark as external any that refer to
6659      DIEs from other CUs (i.e. those which are not marked).  */
6660   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6661     if (AT_class (a) == dw_val_class_die_ref
6662         && AT_ref (a)->die_mark == 0)
6663       {
6664         gcc_assert (AT_ref (a)->die_symbol);
6665
6666         set_AT_ref_external (a, 1);
6667       }
6668
6669   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6670     {
6671       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6672       dw_attr_ref die_a, abbrev_a;
6673       unsigned ix;
6674       bool ok = true;
6675
6676       if (abbrev->die_tag != die->die_tag)
6677         continue;
6678       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6679         continue;
6680
6681       if (VEC_length (dw_attr_node, abbrev->die_attr)
6682           != VEC_length (dw_attr_node, die->die_attr))
6683         continue;
6684
6685       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6686         {
6687           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6688           if ((abbrev_a->dw_attr != die_a->dw_attr)
6689               || (value_format (abbrev_a) != value_format (die_a)))
6690             {
6691               ok = false;
6692               break;
6693             }
6694         }
6695       if (ok)
6696         break;
6697     }
6698
6699   if (abbrev_id >= abbrev_die_table_in_use)
6700     {
6701       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6702         {
6703           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6704           abbrev_die_table = ggc_realloc (abbrev_die_table,
6705                                           sizeof (dw_die_ref) * n_alloc);
6706
6707           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6708                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6709           abbrev_die_table_allocated = n_alloc;
6710         }
6711
6712       ++abbrev_die_table_in_use;
6713       abbrev_die_table[abbrev_id] = die;
6714     }
6715
6716   die->die_abbrev = abbrev_id;
6717   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6718 }
6719 \f
6720 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6721
6722 static int
6723 constant_size (long unsigned int value)
6724 {
6725   int log;
6726
6727   if (value == 0)
6728     log = 0;
6729   else
6730     log = floor_log2 (value);
6731
6732   log = log / 8;
6733   log = 1 << (floor_log2 (log) + 1);
6734
6735   return log;
6736 }
6737
6738 /* Return the size of a DIE as it is represented in the
6739    .debug_info section.  */
6740
6741 static unsigned long
6742 size_of_die (dw_die_ref die)
6743 {
6744   unsigned long size = 0;
6745   dw_attr_ref a;
6746   unsigned ix;
6747
6748   size += size_of_uleb128 (die->die_abbrev);
6749   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6750     {
6751       switch (AT_class (a))
6752         {
6753         case dw_val_class_addr:
6754           size += DWARF2_ADDR_SIZE;
6755           break;
6756         case dw_val_class_offset:
6757           size += DWARF_OFFSET_SIZE;
6758           break;
6759         case dw_val_class_loc:
6760           {
6761             unsigned long lsize = size_of_locs (AT_loc (a));
6762
6763             /* Block length.  */
6764             size += constant_size (lsize);
6765             size += lsize;
6766           }
6767           break;
6768         case dw_val_class_loc_list:
6769           size += DWARF_OFFSET_SIZE;
6770           break;
6771         case dw_val_class_range_list:
6772           size += DWARF_OFFSET_SIZE;
6773           break;
6774         case dw_val_class_const:
6775           size += size_of_sleb128 (AT_int (a));
6776           break;
6777         case dw_val_class_unsigned_const:
6778           size += constant_size (AT_unsigned (a));
6779           break;
6780         case dw_val_class_long_long:
6781           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6782           break;
6783         case dw_val_class_vec:
6784           size += 1 + (a->dw_attr_val.v.val_vec.length
6785                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6786           break;
6787         case dw_val_class_flag:
6788           size += 1;
6789           break;
6790         case dw_val_class_die_ref:
6791           if (AT_ref_external (a))
6792             size += DWARF2_ADDR_SIZE;
6793           else
6794             size += DWARF_OFFSET_SIZE;
6795           break;
6796         case dw_val_class_fde_ref:
6797           size += DWARF_OFFSET_SIZE;
6798           break;
6799         case dw_val_class_lbl_id:
6800           size += DWARF2_ADDR_SIZE;
6801           break;
6802         case dw_val_class_lineptr:
6803         case dw_val_class_macptr:
6804           size += DWARF_OFFSET_SIZE;
6805           break;
6806         case dw_val_class_str:
6807           if (AT_string_form (a) == DW_FORM_strp)
6808             size += DWARF_OFFSET_SIZE;
6809           else
6810             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6811           break;
6812         case dw_val_class_file:
6813           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6814           break;
6815         default:
6816           gcc_unreachable ();
6817         }
6818     }
6819
6820   return size;
6821 }
6822
6823 /* Size the debugging information associated with a given DIE.  Visits the
6824    DIE's children recursively.  Updates the global variable next_die_offset, on
6825    each time through.  Uses the current value of next_die_offset to update the
6826    die_offset field in each DIE.  */
6827
6828 static void
6829 calc_die_sizes (dw_die_ref die)
6830 {
6831   dw_die_ref c;
6832
6833   die->die_offset = next_die_offset;
6834   next_die_offset += size_of_die (die);
6835
6836   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6837
6838   if (die->die_child != NULL)
6839     /* Count the null byte used to terminate sibling lists.  */
6840     next_die_offset += 1;
6841 }
6842
6843 /* Set the marks for a die and its children.  We do this so
6844    that we know whether or not a reference needs to use FORM_ref_addr; only
6845    DIEs in the same CU will be marked.  We used to clear out the offset
6846    and use that as the flag, but ran into ordering problems.  */
6847
6848 static void
6849 mark_dies (dw_die_ref die)
6850 {
6851   dw_die_ref c;
6852
6853   gcc_assert (!die->die_mark);
6854
6855   die->die_mark = 1;
6856   FOR_EACH_CHILD (die, c, mark_dies (c));
6857 }
6858
6859 /* Clear the marks for a die and its children.  */
6860
6861 static void
6862 unmark_dies (dw_die_ref die)
6863 {
6864   dw_die_ref c;
6865
6866   gcc_assert (die->die_mark);
6867
6868   die->die_mark = 0;
6869   FOR_EACH_CHILD (die, c, unmark_dies (c));
6870 }
6871
6872 /* Clear the marks for a die, its children and referred dies.  */
6873
6874 static void
6875 unmark_all_dies (dw_die_ref die)
6876 {
6877   dw_die_ref c;
6878   dw_attr_ref a;
6879   unsigned ix;
6880
6881   if (!die->die_mark)
6882     return;
6883   die->die_mark = 0;
6884
6885   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6886
6887   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6888     if (AT_class (a) == dw_val_class_die_ref)
6889       unmark_all_dies (AT_ref (a));
6890 }
6891
6892 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6893    generated for the compilation unit.  */
6894
6895 static unsigned long
6896 size_of_pubnames (VEC (pubname_entry, gc) * names)
6897 {
6898   unsigned long size;
6899   unsigned i;
6900   pubname_ref p;
6901
6902   size = DWARF_PUBNAMES_HEADER_SIZE;
6903   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6904     if (names != pubtype_table
6905         || p->die->die_offset != 0
6906         || !flag_eliminate_unused_debug_types)
6907       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6908
6909   size += DWARF_OFFSET_SIZE;
6910   return size;
6911 }
6912
6913 /* Return the size of the information in the .debug_aranges section.  */
6914
6915 static unsigned long
6916 size_of_aranges (void)
6917 {
6918   unsigned long size;
6919
6920   size = DWARF_ARANGES_HEADER_SIZE;
6921
6922   /* Count the address/length pair for this compilation unit.  */
6923   if (text_section_used)
6924     size += 2 * DWARF2_ADDR_SIZE;
6925   if (cold_text_section_used)
6926     size += 2 * DWARF2_ADDR_SIZE;
6927   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6928
6929   /* Count the two zero words used to terminated the address range table.  */
6930   size += 2 * DWARF2_ADDR_SIZE;
6931   return size;
6932 }
6933 \f
6934 /* Select the encoding of an attribute value.  */
6935
6936 static enum dwarf_form
6937 value_format (dw_attr_ref a)
6938 {
6939   switch (a->dw_attr_val.val_class)
6940     {
6941     case dw_val_class_addr:
6942       return DW_FORM_addr;
6943     case dw_val_class_range_list:
6944     case dw_val_class_offset:
6945     case dw_val_class_loc_list:
6946       switch (DWARF_OFFSET_SIZE)
6947         {
6948         case 4:
6949           return DW_FORM_data4;
6950         case 8:
6951           return DW_FORM_data8;
6952         default:
6953           gcc_unreachable ();
6954         }
6955     case dw_val_class_loc:
6956       switch (constant_size (size_of_locs (AT_loc (a))))
6957         {
6958         case 1:
6959           return DW_FORM_block1;
6960         case 2:
6961           return DW_FORM_block2;
6962         default:
6963           gcc_unreachable ();
6964         }
6965     case dw_val_class_const:
6966       return DW_FORM_sdata;
6967     case dw_val_class_unsigned_const:
6968       switch (constant_size (AT_unsigned (a)))
6969         {
6970         case 1:
6971           return DW_FORM_data1;
6972         case 2:
6973           return DW_FORM_data2;
6974         case 4:
6975           return DW_FORM_data4;
6976         case 8:
6977           return DW_FORM_data8;
6978         default:
6979           gcc_unreachable ();
6980         }
6981     case dw_val_class_long_long:
6982       return DW_FORM_block1;
6983     case dw_val_class_vec:
6984       return DW_FORM_block1;
6985     case dw_val_class_flag:
6986       return DW_FORM_flag;
6987     case dw_val_class_die_ref:
6988       if (AT_ref_external (a))
6989         return DW_FORM_ref_addr;
6990       else
6991         return DW_FORM_ref;
6992     case dw_val_class_fde_ref:
6993       return DW_FORM_data;
6994     case dw_val_class_lbl_id:
6995       return DW_FORM_addr;
6996     case dw_val_class_lineptr:
6997     case dw_val_class_macptr:
6998       return DW_FORM_data;
6999     case dw_val_class_str:
7000       return AT_string_form (a);
7001     case dw_val_class_file:
7002       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7003         {
7004         case 1:
7005           return DW_FORM_data1;
7006         case 2:
7007           return DW_FORM_data2;
7008         case 4:
7009           return DW_FORM_data4;
7010         default:
7011           gcc_unreachable ();
7012         }
7013
7014     default:
7015       gcc_unreachable ();
7016     }
7017 }
7018
7019 /* Output the encoding of an attribute value.  */
7020
7021 static void
7022 output_value_format (dw_attr_ref a)
7023 {
7024   enum dwarf_form form = value_format (a);
7025
7026   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7027 }
7028
7029 /* Output the .debug_abbrev section which defines the DIE abbreviation
7030    table.  */
7031
7032 static void
7033 output_abbrev_section (void)
7034 {
7035   unsigned long abbrev_id;
7036
7037   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7038     {
7039       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7040       unsigned ix;
7041       dw_attr_ref a_attr;
7042
7043       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7044       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7045                                    dwarf_tag_name (abbrev->die_tag));
7046
7047       if (abbrev->die_child != NULL)
7048         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7049       else
7050         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7051
7052       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7053            ix++)
7054         {
7055           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7056                                        dwarf_attr_name (a_attr->dw_attr));
7057           output_value_format (a_attr);
7058         }
7059
7060       dw2_asm_output_data (1, 0, NULL);
7061       dw2_asm_output_data (1, 0, NULL);
7062     }
7063
7064   /* Terminate the table.  */
7065   dw2_asm_output_data (1, 0, NULL);
7066 }
7067
7068 /* Output a symbol we can use to refer to this DIE from another CU.  */
7069
7070 static inline void
7071 output_die_symbol (dw_die_ref die)
7072 {
7073   char *sym = die->die_symbol;
7074
7075   if (sym == 0)
7076     return;
7077
7078   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7079     /* We make these global, not weak; if the target doesn't support
7080        .linkonce, it doesn't support combining the sections, so debugging
7081        will break.  */
7082     targetm.asm_out.globalize_label (asm_out_file, sym);
7083
7084   ASM_OUTPUT_LABEL (asm_out_file, sym);
7085 }
7086
7087 /* Return a new location list, given the begin and end range, and the
7088    expression. gensym tells us whether to generate a new internal symbol for
7089    this location list node, which is done for the head of the list only.  */
7090
7091 static inline dw_loc_list_ref
7092 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7093               const char *section, unsigned int gensym)
7094 {
7095   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7096
7097   retlist->begin = begin;
7098   retlist->end = end;
7099   retlist->expr = expr;
7100   retlist->section = section;
7101   if (gensym)
7102     retlist->ll_symbol = gen_internal_sym ("LLST");
7103
7104   return retlist;
7105 }
7106
7107 /* Add a location description expression to a location list.  */
7108
7109 static inline void
7110 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7111                            const char *begin, const char *end,
7112                            const char *section)
7113 {
7114   dw_loc_list_ref *d;
7115
7116   /* Find the end of the chain.  */
7117   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7118     ;
7119
7120   /* Add a new location list node to the list.  */
7121   *d = new_loc_list (descr, begin, end, section, 0);
7122 }
7123
7124 /* Output the location list given to us.  */
7125
7126 static void
7127 output_loc_list (dw_loc_list_ref list_head)
7128 {
7129   dw_loc_list_ref curr = list_head;
7130
7131   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7132
7133   /* Walk the location list, and output each range + expression.  */
7134   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7135     {
7136       unsigned long size;
7137       /* Don't output an entry that starts and ends at the same address.  */
7138       if (strcmp (curr->begin, curr->end) == 0)
7139         continue;
7140       if (!have_multiple_function_sections)
7141         {
7142           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7143                                 "Location list begin address (%s)",
7144                                 list_head->ll_symbol);
7145           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7146                                 "Location list end address (%s)",
7147                                 list_head->ll_symbol);
7148         }
7149       else
7150         {
7151           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7152                                "Location list begin address (%s)",
7153                                list_head->ll_symbol);
7154           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7155                                "Location list end address (%s)",
7156                                list_head->ll_symbol);
7157         }
7158       size = size_of_locs (curr->expr);
7159
7160       /* Output the block length for this list of location operations.  */
7161       gcc_assert (size <= 0xffff);
7162       dw2_asm_output_data (2, size, "%s", "Location expression size");
7163
7164       output_loc_sequence (curr->expr);
7165     }
7166
7167   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7168                        "Location list terminator begin (%s)",
7169                        list_head->ll_symbol);
7170   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7171                        "Location list terminator end (%s)",
7172                        list_head->ll_symbol);
7173 }
7174
7175 /* Output the DIE and its attributes.  Called recursively to generate
7176    the definitions of each child DIE.  */
7177
7178 static void
7179 output_die (dw_die_ref die)
7180 {
7181   dw_attr_ref a;
7182   dw_die_ref c;
7183   unsigned long size;
7184   unsigned ix;
7185
7186   /* If someone in another CU might refer to us, set up a symbol for
7187      them to point to.  */
7188   if (die->die_symbol)
7189     output_die_symbol (die);
7190
7191   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7192                                (unsigned long)die->die_offset,
7193                                dwarf_tag_name (die->die_tag));
7194
7195   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7196     {
7197       const char *name = dwarf_attr_name (a->dw_attr);
7198
7199       switch (AT_class (a))
7200         {
7201         case dw_val_class_addr:
7202           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7203           break;
7204
7205         case dw_val_class_offset:
7206           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7207                                "%s", name);
7208           break;
7209
7210         case dw_val_class_range_list:
7211           {
7212             char *p = strchr (ranges_section_label, '\0');
7213
7214             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7215                      a->dw_attr_val.v.val_offset);
7216             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7217                                    debug_ranges_section, "%s", name);
7218             *p = '\0';
7219           }
7220           break;
7221
7222         case dw_val_class_loc:
7223           size = size_of_locs (AT_loc (a));
7224
7225           /* Output the block length for this list of location operations.  */
7226           dw2_asm_output_data (constant_size (size), size, "%s", name);
7227
7228           output_loc_sequence (AT_loc (a));
7229           break;
7230
7231         case dw_val_class_const:
7232           /* ??? It would be slightly more efficient to use a scheme like is
7233              used for unsigned constants below, but gdb 4.x does not sign
7234              extend.  Gdb 5.x does sign extend.  */
7235           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7236           break;
7237
7238         case dw_val_class_unsigned_const:
7239           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7240                                AT_unsigned (a), "%s", name);
7241           break;
7242
7243         case dw_val_class_long_long:
7244           {
7245             unsigned HOST_WIDE_INT first, second;
7246
7247             dw2_asm_output_data (1,
7248                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7249                                  "%s", name);
7250
7251             if (WORDS_BIG_ENDIAN)
7252               {
7253                 first = a->dw_attr_val.v.val_long_long.hi;
7254                 second = a->dw_attr_val.v.val_long_long.low;
7255               }
7256             else
7257               {
7258                 first = a->dw_attr_val.v.val_long_long.low;
7259                 second = a->dw_attr_val.v.val_long_long.hi;
7260               }
7261
7262             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7263                                  first, "long long constant");
7264             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7265                                  second, NULL);
7266           }
7267           break;
7268
7269         case dw_val_class_vec:
7270           {
7271             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7272             unsigned int len = a->dw_attr_val.v.val_vec.length;
7273             unsigned int i;
7274             unsigned char *p;
7275
7276             dw2_asm_output_data (1, len * elt_size, "%s", name);
7277             if (elt_size > sizeof (HOST_WIDE_INT))
7278               {
7279                 elt_size /= 2;
7280                 len *= 2;
7281               }
7282             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7283                  i < len;
7284                  i++, p += elt_size)
7285               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7286                                    "fp or vector constant word %u", i);
7287             break;
7288           }
7289
7290         case dw_val_class_flag:
7291           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7292           break;
7293
7294         case dw_val_class_loc_list:
7295           {
7296             char *sym = AT_loc_list (a)->ll_symbol;
7297
7298             gcc_assert (sym);
7299             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7300                                    "%s", name);
7301           }
7302           break;
7303
7304         case dw_val_class_die_ref:
7305           if (AT_ref_external (a))
7306             {
7307               char *sym = AT_ref (a)->die_symbol;
7308
7309               gcc_assert (sym);
7310               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7311                                      "%s", name);
7312             }
7313           else
7314             {
7315               gcc_assert (AT_ref (a)->die_offset);
7316               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7317                                    "%s", name);
7318             }
7319           break;
7320
7321         case dw_val_class_fde_ref:
7322           {
7323             char l1[20];
7324
7325             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7326                                          a->dw_attr_val.v.val_fde_index * 2);
7327             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7328                                    "%s", name);
7329           }
7330           break;
7331
7332         case dw_val_class_lbl_id:
7333           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7334           break;
7335
7336         case dw_val_class_lineptr:
7337           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7338                                  debug_line_section, "%s", name);
7339           break;
7340
7341         case dw_val_class_macptr:
7342           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7343                                  debug_macinfo_section, "%s", name);
7344           break;
7345
7346         case dw_val_class_str:
7347           if (AT_string_form (a) == DW_FORM_strp)
7348             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7349                                    a->dw_attr_val.v.val_str->label,
7350                                    debug_str_section,
7351                                    "%s: \"%s\"", name, AT_string (a));
7352           else
7353             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7354           break;
7355
7356         case dw_val_class_file:
7357           {
7358             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7359
7360             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7361                                  a->dw_attr_val.v.val_file->filename);
7362             break;
7363           }
7364
7365         default:
7366           gcc_unreachable ();
7367         }
7368     }
7369
7370   FOR_EACH_CHILD (die, c, output_die (c));
7371
7372   /* Add null byte to terminate sibling list.  */
7373   if (die->die_child != NULL)
7374     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7375                          (unsigned long) die->die_offset);
7376 }
7377
7378 /* Output the compilation unit that appears at the beginning of the
7379    .debug_info section, and precedes the DIE descriptions.  */
7380
7381 static void
7382 output_compilation_unit_header (void)
7383 {
7384   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7385     dw2_asm_output_data (4, 0xffffffff,
7386       "Initial length escape value indicating 64-bit DWARF extension");
7387   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7388                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7389                        "Length of Compilation Unit Info");
7390   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7391   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7392                          debug_abbrev_section,
7393                          "Offset Into Abbrev. Section");
7394   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7395 }
7396
7397 /* Output the compilation unit DIE and its children.  */
7398
7399 static void
7400 output_comp_unit (dw_die_ref die, int output_if_empty)
7401 {
7402   const char *secname;
7403   char *oldsym, *tmp;
7404
7405   /* Unless we are outputting main CU, we may throw away empty ones.  */
7406   if (!output_if_empty && die->die_child == NULL)
7407     return;
7408
7409   /* Even if there are no children of this DIE, we must output the information
7410      about the compilation unit.  Otherwise, on an empty translation unit, we
7411      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7412      will then complain when examining the file.  First mark all the DIEs in
7413      this CU so we know which get local refs.  */
7414   mark_dies (die);
7415
7416   build_abbrev_table (die);
7417
7418   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7419   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7420   calc_die_sizes (die);
7421
7422   oldsym = die->die_symbol;
7423   if (oldsym)
7424     {
7425       tmp = alloca (strlen (oldsym) + 24);
7426
7427       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7428       secname = tmp;
7429       die->die_symbol = NULL;
7430       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7431     }
7432   else
7433     switch_to_section (debug_info_section);
7434
7435   /* Output debugging information.  */
7436   output_compilation_unit_header ();
7437   output_die (die);
7438
7439   /* Leave the marks on the main CU, so we can check them in
7440      output_pubnames.  */
7441   if (oldsym)
7442     {
7443       unmark_dies (die);
7444       die->die_symbol = oldsym;
7445     }
7446 }
7447
7448 /* Return the DWARF2/3 pubname associated with a decl.  */
7449
7450 static const char *
7451 dwarf2_name (tree decl, int scope)
7452 {
7453   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7454 }
7455
7456 /* Add a new entry to .debug_pubnames if appropriate.  */
7457
7458 static void
7459 add_pubname (tree decl, dw_die_ref die)
7460 {
7461   pubname_entry e;
7462
7463   if (! TREE_PUBLIC (decl))
7464     return;
7465
7466   e.die = die;
7467   e.name = xstrdup (dwarf2_name (decl, 1));
7468   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7469 }
7470
7471 /* Add a new entry to .debug_pubtypes if appropriate.  */
7472
7473 static void
7474 add_pubtype (tree decl, dw_die_ref die)
7475 {
7476   pubname_entry e;
7477
7478   e.name = NULL;
7479   if ((TREE_PUBLIC (decl)
7480        || die->die_parent == comp_unit_die)
7481       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7482     {
7483       e.die = die;
7484       if (TYPE_P (decl))
7485         {
7486           if (TYPE_NAME (decl))
7487             {
7488               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7489                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7490               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7491                        && DECL_NAME (TYPE_NAME (decl)))
7492                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7493               else
7494                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7495             }
7496         }
7497       else
7498         e.name = xstrdup (dwarf2_name (decl, 1));
7499
7500       /* If we don't have a name for the type, there's no point in adding
7501          it to the table.  */
7502       if (e.name && e.name[0] != '\0')
7503         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7504     }
7505 }
7506
7507 /* Output the public names table used to speed up access to externally
7508    visible names; or the public types table used to find type definitions.  */
7509
7510 static void
7511 output_pubnames (VEC (pubname_entry, gc) * names)
7512 {
7513   unsigned i;
7514   unsigned long pubnames_length = size_of_pubnames (names);
7515   pubname_ref pub;
7516
7517   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7518     dw2_asm_output_data (4, 0xffffffff,
7519       "Initial length escape value indicating 64-bit DWARF extension");
7520   if (names == pubname_table)
7521     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7522                          "Length of Public Names Info");
7523   else
7524     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7525                          "Length of Public Type Names Info");
7526   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7527   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7528                          debug_info_section,
7529                          "Offset of Compilation Unit Info");
7530   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7531                        "Compilation Unit Length");
7532
7533   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7534     {
7535       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7536       if (names == pubname_table)
7537         gcc_assert (pub->die->die_mark);
7538
7539       if (names != pubtype_table
7540           || pub->die->die_offset != 0
7541           || !flag_eliminate_unused_debug_types)
7542         {
7543           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7544                                "DIE offset");
7545
7546           dw2_asm_output_nstring (pub->name, -1, "external name");
7547         }
7548     }
7549
7550   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7551 }
7552
7553 /* Add a new entry to .debug_aranges if appropriate.  */
7554
7555 static void
7556 add_arange (tree decl, dw_die_ref die)
7557 {
7558   if (! DECL_SECTION_NAME (decl))
7559     return;
7560
7561   if (arange_table_in_use == arange_table_allocated)
7562     {
7563       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7564       arange_table = ggc_realloc (arange_table,
7565                                   (arange_table_allocated
7566                                    * sizeof (dw_die_ref)));
7567       memset (arange_table + arange_table_in_use, 0,
7568               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7569     }
7570
7571   arange_table[arange_table_in_use++] = die;
7572 }
7573
7574 /* Output the information that goes into the .debug_aranges table.
7575    Namely, define the beginning and ending address range of the
7576    text section generated for this compilation unit.  */
7577
7578 static void
7579 output_aranges (void)
7580 {
7581   unsigned i;
7582   unsigned long aranges_length = size_of_aranges ();
7583
7584   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7585     dw2_asm_output_data (4, 0xffffffff,
7586       "Initial length escape value indicating 64-bit DWARF extension");
7587   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7588                        "Length of Address Ranges Info");
7589   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7590   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7591                          debug_info_section,
7592                          "Offset of Compilation Unit Info");
7593   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7594   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7595
7596   /* We need to align to twice the pointer size here.  */
7597   if (DWARF_ARANGES_PAD_SIZE)
7598     {
7599       /* Pad using a 2 byte words so that padding is correct for any
7600          pointer size.  */
7601       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7602                            2 * DWARF2_ADDR_SIZE);
7603       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7604         dw2_asm_output_data (2, 0, NULL);
7605     }
7606
7607   /* It is necessary not to output these entries if the sections were
7608      not used; if the sections were not used, the length will be 0 and
7609      the address may end up as 0 if the section is discarded by ld
7610      --gc-sections, leaving an invalid (0, 0) entry that can be
7611      confused with the terminator.  */
7612   if (text_section_used)
7613     {
7614       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7615       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7616                             text_section_label, "Length");
7617     }
7618   if (cold_text_section_used)
7619     {
7620       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7621                            "Address");
7622       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7623                             cold_text_section_label, "Length");
7624     }
7625
7626   for (i = 0; i < arange_table_in_use; i++)
7627     {
7628       dw_die_ref die = arange_table[i];
7629
7630       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7631       gcc_assert (die->die_mark);
7632
7633       if (die->die_tag == DW_TAG_subprogram)
7634         {
7635           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7636                                "Address");
7637           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7638                                 get_AT_low_pc (die), "Length");
7639         }
7640       else
7641         {
7642           /* A static variable; extract the symbol from DW_AT_location.
7643              Note that this code isn't currently hit, as we only emit
7644              aranges for functions (jason 9/23/99).  */
7645           dw_attr_ref a = get_AT (die, DW_AT_location);
7646           dw_loc_descr_ref loc;
7647
7648           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7649
7650           loc = AT_loc (a);
7651           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7652
7653           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7654                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7655           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7656                                get_AT_unsigned (die, DW_AT_byte_size),
7657                                "Length");
7658         }
7659     }
7660
7661   /* Output the terminator words.  */
7662   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7663   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7664 }
7665
7666 /* Add a new entry to .debug_ranges.  Return the offset at which it
7667    was placed.  */
7668
7669 static unsigned int
7670 add_ranges_num (int num)
7671 {
7672   unsigned int in_use = ranges_table_in_use;
7673
7674   if (in_use == ranges_table_allocated)
7675     {
7676       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7677       ranges_table
7678         = ggc_realloc (ranges_table, (ranges_table_allocated
7679                                       * sizeof (struct dw_ranges_struct)));
7680       memset (ranges_table + ranges_table_in_use, 0,
7681               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7682     }
7683
7684   ranges_table[in_use].num = num;
7685   ranges_table_in_use = in_use + 1;
7686
7687   return in_use * 2 * DWARF2_ADDR_SIZE;
7688 }
7689
7690 /* Add a new entry to .debug_ranges corresponding to a block, or a
7691    range terminator if BLOCK is NULL.  */
7692
7693 static unsigned int
7694 add_ranges (const_tree block)
7695 {
7696   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7697 }
7698
7699 /* Add a new entry to .debug_ranges corresponding to a pair of
7700    labels.  */
7701
7702 static unsigned int
7703 add_ranges_by_labels (const char *begin, const char *end)
7704 {
7705   unsigned int in_use = ranges_by_label_in_use;
7706
7707   if (in_use == ranges_by_label_allocated)
7708     {
7709       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7710       ranges_by_label
7711         = ggc_realloc (ranges_by_label,
7712                        (ranges_by_label_allocated
7713                         * sizeof (struct dw_ranges_by_label_struct)));
7714       memset (ranges_by_label + ranges_by_label_in_use, 0,
7715               RANGES_TABLE_INCREMENT
7716               * sizeof (struct dw_ranges_by_label_struct));
7717     }
7718
7719   ranges_by_label[in_use].begin = begin;
7720   ranges_by_label[in_use].end = end;
7721   ranges_by_label_in_use = in_use + 1;
7722
7723   return add_ranges_num (-(int)in_use - 1);
7724 }
7725
7726 static void
7727 output_ranges (void)
7728 {
7729   unsigned i;
7730   static const char *const start_fmt = "Offset 0x%x";
7731   const char *fmt = start_fmt;
7732
7733   for (i = 0; i < ranges_table_in_use; i++)
7734     {
7735       int block_num = ranges_table[i].num;
7736
7737       if (block_num > 0)
7738         {
7739           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7740           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7741
7742           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7743           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7744
7745           /* If all code is in the text section, then the compilation
7746              unit base address defaults to DW_AT_low_pc, which is the
7747              base of the text section.  */
7748           if (!have_multiple_function_sections)
7749             {
7750               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7751                                     text_section_label,
7752                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7753               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7754                                     text_section_label, NULL);
7755             }
7756
7757           /* Otherwise, the compilation unit base address is zero,
7758              which allows us to use absolute addresses, and not worry
7759              about whether the target supports cross-section
7760              arithmetic.  */
7761           else
7762             {
7763               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7764                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7765               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7766             }
7767
7768           fmt = NULL;
7769         }
7770
7771       /* Negative block_num stands for an index into ranges_by_label.  */
7772       else if (block_num < 0)
7773         {
7774           int lab_idx = - block_num - 1;
7775
7776           if (!have_multiple_function_sections)
7777             {
7778               gcc_unreachable ();
7779 #if 0
7780               /* If we ever use add_ranges_by_labels () for a single
7781                  function section, all we have to do is to take out
7782                  the #if 0 above.  */
7783               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7784                                     ranges_by_label[lab_idx].begin,
7785                                     text_section_label,
7786                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7787               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7788                                     ranges_by_label[lab_idx].end,
7789                                     text_section_label, NULL);
7790 #endif
7791             }
7792           else
7793             {
7794               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7795                                    ranges_by_label[lab_idx].begin,
7796                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7797               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7798                                    ranges_by_label[lab_idx].end,
7799                                    NULL);
7800             }
7801         }
7802       else
7803         {
7804           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7805           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7806           fmt = start_fmt;
7807         }
7808     }
7809 }
7810
7811 /* Data structure containing information about input files.  */
7812 struct file_info
7813 {
7814   const char *path;     /* Complete file name.  */
7815   const char *fname;    /* File name part.  */
7816   int length;           /* Length of entire string.  */
7817   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7818   int dir_idx;          /* Index in directory table.  */
7819 };
7820
7821 /* Data structure containing information about directories with source
7822    files.  */
7823 struct dir_info
7824 {
7825   const char *path;     /* Path including directory name.  */
7826   int length;           /* Path length.  */
7827   int prefix;           /* Index of directory entry which is a prefix.  */
7828   int count;            /* Number of files in this directory.  */
7829   int dir_idx;          /* Index of directory used as base.  */
7830 };
7831
7832 /* Callback function for file_info comparison.  We sort by looking at
7833    the directories in the path.  */
7834
7835 static int
7836 file_info_cmp (const void *p1, const void *p2)
7837 {
7838   const struct file_info *s1 = p1;
7839   const struct file_info *s2 = p2;
7840   const unsigned char *cp1;
7841   const unsigned char *cp2;
7842
7843   /* Take care of file names without directories.  We need to make sure that
7844      we return consistent values to qsort since some will get confused if
7845      we return the same value when identical operands are passed in opposite
7846      orders.  So if neither has a directory, return 0 and otherwise return
7847      1 or -1 depending on which one has the directory.  */
7848   if ((s1->path == s1->fname || s2->path == s2->fname))
7849     return (s2->path == s2->fname) - (s1->path == s1->fname);
7850
7851   cp1 = (const unsigned char *) s1->path;
7852   cp2 = (const unsigned char *) s2->path;
7853
7854   while (1)
7855     {
7856       ++cp1;
7857       ++cp2;
7858       /* Reached the end of the first path?  If so, handle like above.  */
7859       if ((cp1 == (const unsigned char *) s1->fname)
7860           || (cp2 == (const unsigned char *) s2->fname))
7861         return ((cp2 == (const unsigned char *) s2->fname)
7862                 - (cp1 == (const unsigned char *) s1->fname));
7863
7864       /* Character of current path component the same?  */
7865       else if (*cp1 != *cp2)
7866         return *cp1 - *cp2;
7867     }
7868 }
7869
7870 struct file_name_acquire_data
7871 {
7872   struct file_info *files;
7873   int used_files;
7874   int max_files;
7875 };
7876
7877 /* Traversal function for the hash table.  */
7878
7879 static int
7880 file_name_acquire (void ** slot, void *data)
7881 {
7882   struct file_name_acquire_data *fnad = data;
7883   struct dwarf_file_data *d = *slot;
7884   struct file_info *fi;
7885   const char *f;
7886
7887   gcc_assert (fnad->max_files >= d->emitted_number);
7888
7889   if (! d->emitted_number)
7890     return 1;
7891
7892   gcc_assert (fnad->max_files != fnad->used_files);
7893
7894   fi = fnad->files + fnad->used_files++;
7895
7896   /* Skip all leading "./".  */
7897   f = d->filename;
7898   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7899     f += 2;
7900
7901   /* Create a new array entry.  */
7902   fi->path = f;
7903   fi->length = strlen (f);
7904   fi->file_idx = d;
7905
7906   /* Search for the file name part.  */
7907   f = strrchr (f, DIR_SEPARATOR);
7908 #if defined (DIR_SEPARATOR_2)
7909   {
7910     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7911
7912     if (g != NULL)
7913       {
7914         if (f == NULL || f < g)
7915           f = g;
7916       }
7917   }
7918 #endif
7919
7920   fi->fname = f == NULL ? fi->path : f + 1;
7921   return 1;
7922 }
7923
7924 /* Output the directory table and the file name table.  We try to minimize
7925    the total amount of memory needed.  A heuristic is used to avoid large
7926    slowdowns with many input files.  */
7927
7928 static void
7929 output_file_names (void)
7930 {
7931   struct file_name_acquire_data fnad;
7932   int numfiles;
7933   struct file_info *files;
7934   struct dir_info *dirs;
7935   int *saved;
7936   int *savehere;
7937   int *backmap;
7938   int ndirs;
7939   int idx_offset;
7940   int i;
7941   int idx;
7942
7943   if (!last_emitted_file)
7944     {
7945       dw2_asm_output_data (1, 0, "End directory table");
7946       dw2_asm_output_data (1, 0, "End file name table");
7947       return;
7948     }
7949
7950   numfiles = last_emitted_file->emitted_number;
7951
7952   /* Allocate the various arrays we need.  */
7953   files = alloca (numfiles * sizeof (struct file_info));
7954   dirs = alloca (numfiles * sizeof (struct dir_info));
7955
7956   fnad.files = files;
7957   fnad.used_files = 0;
7958   fnad.max_files = numfiles;
7959   htab_traverse (file_table, file_name_acquire, &fnad);
7960   gcc_assert (fnad.used_files == fnad.max_files);
7961
7962   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7963
7964   /* Find all the different directories used.  */
7965   dirs[0].path = files[0].path;
7966   dirs[0].length = files[0].fname - files[0].path;
7967   dirs[0].prefix = -1;
7968   dirs[0].count = 1;
7969   dirs[0].dir_idx = 0;
7970   files[0].dir_idx = 0;
7971   ndirs = 1;
7972
7973   for (i = 1; i < numfiles; i++)
7974     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7975         && memcmp (dirs[ndirs - 1].path, files[i].path,
7976                    dirs[ndirs - 1].length) == 0)
7977       {
7978         /* Same directory as last entry.  */
7979         files[i].dir_idx = ndirs - 1;
7980         ++dirs[ndirs - 1].count;
7981       }
7982     else
7983       {
7984         int j;
7985
7986         /* This is a new directory.  */
7987         dirs[ndirs].path = files[i].path;
7988         dirs[ndirs].length = files[i].fname - files[i].path;
7989         dirs[ndirs].count = 1;
7990         dirs[ndirs].dir_idx = ndirs;
7991         files[i].dir_idx = ndirs;
7992
7993         /* Search for a prefix.  */
7994         dirs[ndirs].prefix = -1;
7995         for (j = 0; j < ndirs; j++)
7996           if (dirs[j].length < dirs[ndirs].length
7997               && dirs[j].length > 1
7998               && (dirs[ndirs].prefix == -1
7999                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8000               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8001             dirs[ndirs].prefix = j;
8002
8003         ++ndirs;
8004       }
8005
8006   /* Now to the actual work.  We have to find a subset of the directories which
8007      allow expressing the file name using references to the directory table
8008      with the least amount of characters.  We do not do an exhaustive search
8009      where we would have to check out every combination of every single
8010      possible prefix.  Instead we use a heuristic which provides nearly optimal
8011      results in most cases and never is much off.  */
8012   saved = alloca (ndirs * sizeof (int));
8013   savehere = alloca (ndirs * sizeof (int));
8014
8015   memset (saved, '\0', ndirs * sizeof (saved[0]));
8016   for (i = 0; i < ndirs; i++)
8017     {
8018       int j;
8019       int total;
8020
8021       /* We can always save some space for the current directory.  But this
8022          does not mean it will be enough to justify adding the directory.  */
8023       savehere[i] = dirs[i].length;
8024       total = (savehere[i] - saved[i]) * dirs[i].count;
8025
8026       for (j = i + 1; j < ndirs; j++)
8027         {
8028           savehere[j] = 0;
8029           if (saved[j] < dirs[i].length)
8030             {
8031               /* Determine whether the dirs[i] path is a prefix of the
8032                  dirs[j] path.  */
8033               int k;
8034
8035               k = dirs[j].prefix;
8036               while (k != -1 && k != (int) i)
8037                 k = dirs[k].prefix;
8038
8039               if (k == (int) i)
8040                 {
8041                   /* Yes it is.  We can possibly save some memory by
8042                      writing the filenames in dirs[j] relative to
8043                      dirs[i].  */
8044                   savehere[j] = dirs[i].length;
8045                   total += (savehere[j] - saved[j]) * dirs[j].count;
8046                 }
8047             }
8048         }
8049
8050       /* Check whether we can save enough to justify adding the dirs[i]
8051          directory.  */
8052       if (total > dirs[i].length + 1)
8053         {
8054           /* It's worthwhile adding.  */
8055           for (j = i; j < ndirs; j++)
8056             if (savehere[j] > 0)
8057               {
8058                 /* Remember how much we saved for this directory so far.  */
8059                 saved[j] = savehere[j];
8060
8061                 /* Remember the prefix directory.  */
8062                 dirs[j].dir_idx = i;
8063               }
8064         }
8065     }
8066
8067   /* Emit the directory name table.  */
8068   idx = 1;
8069   idx_offset = dirs[0].length > 0 ? 1 : 0;
8070   for (i = 1 - idx_offset; i < ndirs; i++)
8071     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8072                             "Directory Entry: 0x%x", i + idx_offset);
8073
8074   dw2_asm_output_data (1, 0, "End directory table");
8075
8076   /* We have to emit them in the order of emitted_number since that's
8077      used in the debug info generation.  To do this efficiently we
8078      generate a back-mapping of the indices first.  */
8079   backmap = alloca (numfiles * sizeof (int));
8080   for (i = 0; i < numfiles; i++)
8081     backmap[files[i].file_idx->emitted_number - 1] = i;
8082
8083   /* Now write all the file names.  */
8084   for (i = 0; i < numfiles; i++)
8085     {
8086       int file_idx = backmap[i];
8087       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8088
8089       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8090                               "File Entry: 0x%x", (unsigned) i + 1);
8091
8092       /* Include directory index.  */
8093       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8094
8095       /* Modification time.  */
8096       dw2_asm_output_data_uleb128 (0, NULL);
8097
8098       /* File length in bytes.  */
8099       dw2_asm_output_data_uleb128 (0, NULL);
8100     }
8101
8102   dw2_asm_output_data (1, 0, "End file name table");
8103 }
8104
8105
8106 /* Output the source line number correspondence information.  This
8107    information goes into the .debug_line section.  */
8108
8109 static void
8110 output_line_info (void)
8111 {
8112   char l1[20], l2[20], p1[20], p2[20];
8113   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8114   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8115   unsigned opc;
8116   unsigned n_op_args;
8117   unsigned long lt_index;
8118   unsigned long current_line;
8119   long line_offset;
8120   long line_delta;
8121   unsigned long current_file;
8122   unsigned long function;
8123
8124   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8125   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8126   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8127   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8128
8129   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8130     dw2_asm_output_data (4, 0xffffffff,
8131       "Initial length escape value indicating 64-bit DWARF extension");
8132   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8133                         "Length of Source Line Info");
8134   ASM_OUTPUT_LABEL (asm_out_file, l1);
8135
8136   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8137   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8138   ASM_OUTPUT_LABEL (asm_out_file, p1);
8139
8140   /* Define the architecture-dependent minimum instruction length (in
8141    bytes).  In this implementation of DWARF, this field is used for
8142    information purposes only.  Since GCC generates assembly language,
8143    we have no a priori knowledge of how many instruction bytes are
8144    generated for each source line, and therefore can use only the
8145    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8146    commands.  Accordingly, we fix this as `1', which is "correct
8147    enough" for all architectures, and don't let the target override.  */
8148   dw2_asm_output_data (1, 1,
8149                        "Minimum Instruction Length");
8150
8151   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8152                        "Default is_stmt_start flag");
8153   dw2_asm_output_data (1, DWARF_LINE_BASE,
8154                        "Line Base Value (Special Opcodes)");
8155   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8156                        "Line Range Value (Special Opcodes)");
8157   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8158                        "Special Opcode Base");
8159
8160   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8161     {
8162       switch (opc)
8163         {
8164         case DW_LNS_advance_pc:
8165         case DW_LNS_advance_line:
8166         case DW_LNS_set_file:
8167         case DW_LNS_set_column:
8168         case DW_LNS_fixed_advance_pc:
8169           n_op_args = 1;
8170           break;
8171         default:
8172           n_op_args = 0;
8173           break;
8174         }
8175
8176       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8177                            opc, n_op_args);
8178     }
8179
8180   /* Write out the information about the files we use.  */
8181   output_file_names ();
8182   ASM_OUTPUT_LABEL (asm_out_file, p2);
8183
8184   /* We used to set the address register to the first location in the text
8185      section here, but that didn't accomplish anything since we already
8186      have a line note for the opening brace of the first function.  */
8187
8188   /* Generate the line number to PC correspondence table, encoded as
8189      a series of state machine operations.  */
8190   current_file = 1;
8191   current_line = 1;
8192
8193   if (cfun && in_cold_section_p)
8194     strcpy (prev_line_label, cfun->cold_section_label);
8195   else
8196     strcpy (prev_line_label, text_section_label);
8197   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8198     {
8199       dw_line_info_ref line_info = &line_info_table[lt_index];
8200
8201 #if 0
8202       /* Disable this optimization for now; GDB wants to see two line notes
8203          at the beginning of a function so it can find the end of the
8204          prologue.  */
8205
8206       /* Don't emit anything for redundant notes.  Just updating the
8207          address doesn't accomplish anything, because we already assume
8208          that anything after the last address is this line.  */
8209       if (line_info->dw_line_num == current_line
8210           && line_info->dw_file_num == current_file)
8211         continue;
8212 #endif
8213
8214       /* Emit debug info for the address of the current line.
8215
8216          Unfortunately, we have little choice here currently, and must always
8217          use the most general form.  GCC does not know the address delta
8218          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8219          attributes which will give an upper bound on the address range.  We
8220          could perhaps use length attributes to determine when it is safe to
8221          use DW_LNS_fixed_advance_pc.  */
8222
8223       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8224       if (0)
8225         {
8226           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8227           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8228                                "DW_LNS_fixed_advance_pc");
8229           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8230         }
8231       else
8232         {
8233           /* This can handle any delta.  This takes
8234              4+DWARF2_ADDR_SIZE bytes.  */
8235           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8236           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8237           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8238           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8239         }
8240
8241       strcpy (prev_line_label, line_label);
8242
8243       /* Emit debug info for the source file of the current line, if
8244          different from the previous line.  */
8245       if (line_info->dw_file_num != current_file)
8246         {
8247           current_file = line_info->dw_file_num;
8248           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8249           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8250         }
8251
8252       /* Emit debug info for the current line number, choosing the encoding
8253          that uses the least amount of space.  */
8254       if (line_info->dw_line_num != current_line)
8255         {
8256           line_offset = line_info->dw_line_num - current_line;
8257           line_delta = line_offset - DWARF_LINE_BASE;
8258           current_line = line_info->dw_line_num;
8259           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8260             /* This can handle deltas from -10 to 234, using the current
8261                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8262                takes 1 byte.  */
8263             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8264                                  "line %lu", current_line);
8265           else
8266             {
8267               /* This can handle any delta.  This takes at least 4 bytes,
8268                  depending on the value being encoded.  */
8269               dw2_asm_output_data (1, DW_LNS_advance_line,
8270                                    "advance to line %lu", current_line);
8271               dw2_asm_output_data_sleb128 (line_offset, NULL);
8272               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8273             }
8274         }
8275       else
8276         /* We still need to start a new row, so output a copy insn.  */
8277         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8278     }
8279
8280   /* Emit debug info for the address of the end of the function.  */
8281   if (0)
8282     {
8283       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8284                            "DW_LNS_fixed_advance_pc");
8285       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8286     }
8287   else
8288     {
8289       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8290       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8291       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8292       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8293     }
8294
8295   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8296   dw2_asm_output_data_uleb128 (1, NULL);
8297   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8298
8299   function = 0;
8300   current_file = 1;
8301   current_line = 1;
8302   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8303     {
8304       dw_separate_line_info_ref line_info
8305         = &separate_line_info_table[lt_index];
8306
8307 #if 0
8308       /* Don't emit anything for redundant notes.  */
8309       if (line_info->dw_line_num == current_line
8310           && line_info->dw_file_num == current_file
8311           && line_info->function == function)
8312         goto cont;
8313 #endif
8314
8315       /* Emit debug info for the address of the current line.  If this is
8316          a new function, or the first line of a function, then we need
8317          to handle it differently.  */
8318       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8319                                    lt_index);
8320       if (function != line_info->function)
8321         {
8322           function = line_info->function;
8323
8324           /* Set the address register to the first line in the function.  */
8325           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8326           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8327           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8328           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8329         }
8330       else
8331         {
8332           /* ??? See the DW_LNS_advance_pc comment above.  */
8333           if (0)
8334             {
8335               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8336                                    "DW_LNS_fixed_advance_pc");
8337               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8338             }
8339           else
8340             {
8341               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8342               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8343               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8344               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8345             }
8346         }
8347
8348       strcpy (prev_line_label, line_label);
8349
8350       /* Emit debug info for the source file of the current line, if
8351          different from the previous line.  */
8352       if (line_info->dw_file_num != current_file)
8353         {
8354           current_file = line_info->dw_file_num;
8355           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8356           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8357         }
8358
8359       /* Emit debug info for the current line number, choosing the encoding
8360          that uses the least amount of space.  */
8361       if (line_info->dw_line_num != current_line)
8362         {
8363           line_offset = line_info->dw_line_num - current_line;
8364           line_delta = line_offset - DWARF_LINE_BASE;
8365           current_line = line_info->dw_line_num;
8366           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8367             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8368                                  "line %lu", current_line);
8369           else
8370             {
8371               dw2_asm_output_data (1, DW_LNS_advance_line,
8372                                    "advance to line %lu", current_line);
8373               dw2_asm_output_data_sleb128 (line_offset, NULL);
8374               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8375             }
8376         }
8377       else
8378         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8379
8380 #if 0
8381     cont:
8382 #endif
8383
8384       lt_index++;
8385
8386       /* If we're done with a function, end its sequence.  */
8387       if (lt_index == separate_line_info_table_in_use
8388           || separate_line_info_table[lt_index].function != function)
8389         {
8390           current_file = 1;
8391           current_line = 1;
8392
8393           /* Emit debug info for the address of the end of the function.  */
8394           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8395           if (0)
8396             {
8397               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8398                                    "DW_LNS_fixed_advance_pc");
8399               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8400             }
8401           else
8402             {
8403               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8404               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8405               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8406               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8407             }
8408
8409           /* Output the marker for the end of this sequence.  */
8410           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8411           dw2_asm_output_data_uleb128 (1, NULL);
8412           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8413         }
8414     }
8415
8416   /* Output the marker for the end of the line number info.  */
8417   ASM_OUTPUT_LABEL (asm_out_file, l2);
8418 }
8419 \f
8420 /* Given a pointer to a tree node for some base type, return a pointer to
8421    a DIE that describes the given type.
8422
8423    This routine must only be called for GCC type nodes that correspond to
8424    Dwarf base (fundamental) types.  */
8425
8426 static dw_die_ref
8427 base_type_die (tree type)
8428 {
8429   dw_die_ref base_type_result;
8430   enum dwarf_type encoding;
8431
8432   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8433     return 0;
8434
8435   switch (TREE_CODE (type))
8436     {
8437     case INTEGER_TYPE:
8438       if (TYPE_STRING_FLAG (type))
8439         {
8440           if (TYPE_UNSIGNED (type))
8441             encoding = DW_ATE_unsigned_char;
8442           else
8443             encoding = DW_ATE_signed_char;
8444         }
8445       else if (TYPE_UNSIGNED (type))
8446         encoding = DW_ATE_unsigned;
8447       else
8448         encoding = DW_ATE_signed;
8449       break;
8450
8451     case REAL_TYPE:
8452       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8453         encoding = DW_ATE_decimal_float;
8454       else
8455         encoding = DW_ATE_float;
8456       break;
8457
8458     case FIXED_POINT_TYPE:
8459       if (TYPE_UNSIGNED (type))
8460         encoding = DW_ATE_unsigned_fixed;
8461       else
8462         encoding = DW_ATE_signed_fixed;
8463       break;
8464
8465       /* Dwarf2 doesn't know anything about complex ints, so use
8466          a user defined type for it.  */
8467     case COMPLEX_TYPE:
8468       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8469         encoding = DW_ATE_complex_float;
8470       else
8471         encoding = DW_ATE_lo_user;
8472       break;
8473
8474     case BOOLEAN_TYPE:
8475       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8476       encoding = DW_ATE_boolean;
8477       break;
8478
8479     default:
8480       /* No other TREE_CODEs are Dwarf fundamental types.  */
8481       gcc_unreachable ();
8482     }
8483
8484   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8485
8486   /* This probably indicates a bug.  */
8487   if (! TYPE_NAME (type))
8488     add_name_attribute (base_type_result, "__unknown__");
8489
8490   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8491                    int_size_in_bytes (type));
8492   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8493
8494   return base_type_result;
8495 }
8496
8497 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8498    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8499
8500 static inline int
8501 is_base_type (tree type)
8502 {
8503   switch (TREE_CODE (type))
8504     {
8505     case ERROR_MARK:
8506     case VOID_TYPE:
8507     case INTEGER_TYPE:
8508     case REAL_TYPE:
8509     case FIXED_POINT_TYPE:
8510     case COMPLEX_TYPE:
8511     case BOOLEAN_TYPE:
8512       return 1;
8513
8514     case ARRAY_TYPE:
8515     case RECORD_TYPE:
8516     case UNION_TYPE:
8517     case QUAL_UNION_TYPE:
8518     case ENUMERAL_TYPE:
8519     case FUNCTION_TYPE:
8520     case METHOD_TYPE:
8521     case POINTER_TYPE:
8522     case REFERENCE_TYPE:
8523     case OFFSET_TYPE:
8524     case LANG_TYPE:
8525     case VECTOR_TYPE:
8526       return 0;
8527
8528     default:
8529       gcc_unreachable ();
8530     }
8531
8532   return 0;
8533 }
8534
8535 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8536    node, return the size in bits for the type if it is a constant, or else
8537    return the alignment for the type if the type's size is not constant, or
8538    else return BITS_PER_WORD if the type actually turns out to be an
8539    ERROR_MARK node.  */
8540
8541 static inline unsigned HOST_WIDE_INT
8542 simple_type_size_in_bits (const_tree type)
8543 {
8544   if (TREE_CODE (type) == ERROR_MARK)
8545     return BITS_PER_WORD;
8546   else if (TYPE_SIZE (type) == NULL_TREE)
8547     return 0;
8548   else if (host_integerp (TYPE_SIZE (type), 1))
8549     return tree_low_cst (TYPE_SIZE (type), 1);
8550   else
8551     return TYPE_ALIGN (type);
8552 }
8553
8554 /* Return true if the debug information for the given type should be
8555    emitted as a subrange type.  */
8556
8557 static inline bool
8558 is_subrange_type (const_tree type)
8559 {
8560   tree subtype = TREE_TYPE (type);
8561
8562   /* Subrange types are identified by the fact that they are integer
8563      types, and that they have a subtype which is either an integer type
8564      or an enumeral type.  */
8565
8566   if (TREE_CODE (type) != INTEGER_TYPE
8567       || subtype == NULL_TREE)
8568     return false;
8569
8570   if (TREE_CODE (subtype) != INTEGER_TYPE
8571       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8572     return false;
8573
8574   if (TREE_CODE (type) == TREE_CODE (subtype)
8575       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8576       && TYPE_MIN_VALUE (type) != NULL
8577       && TYPE_MIN_VALUE (subtype) != NULL
8578       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8579       && TYPE_MAX_VALUE (type) != NULL
8580       && TYPE_MAX_VALUE (subtype) != NULL
8581       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8582     {
8583       /* The type and its subtype have the same representation.  If in
8584          addition the two types also have the same name, then the given
8585          type is not a subrange type, but rather a plain base type.  */
8586       /* FIXME: brobecker/2004-03-22:
8587          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8588          therefore be sufficient to check the TYPE_SIZE node pointers
8589          rather than checking the actual size.  Unfortunately, we have
8590          found some cases, such as in the Ada "integer" type, where
8591          this is not the case.  Until this problem is solved, we need to
8592          keep checking the actual size.  */
8593       tree type_name = TYPE_NAME (type);
8594       tree subtype_name = TYPE_NAME (subtype);
8595
8596       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8597         type_name = DECL_NAME (type_name);
8598
8599       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8600         subtype_name = DECL_NAME (subtype_name);
8601
8602       if (type_name == subtype_name)
8603         return false;
8604     }
8605
8606   return true;
8607 }
8608
8609 /*  Given a pointer to a tree node for a subrange type, return a pointer
8610     to a DIE that describes the given type.  */
8611
8612 static dw_die_ref
8613 subrange_type_die (tree type, dw_die_ref context_die)
8614 {
8615   dw_die_ref subrange_die;
8616   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8617
8618   if (context_die == NULL)
8619     context_die = comp_unit_die;
8620
8621   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8622
8623   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8624     {
8625       /* The size of the subrange type and its base type do not match,
8626          so we need to generate a size attribute for the subrange type.  */
8627       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8628     }
8629
8630   if (TYPE_MIN_VALUE (type) != NULL)
8631     add_bound_info (subrange_die, DW_AT_lower_bound,
8632                     TYPE_MIN_VALUE (type));
8633   if (TYPE_MAX_VALUE (type) != NULL)
8634     add_bound_info (subrange_die, DW_AT_upper_bound,
8635                     TYPE_MAX_VALUE (type));
8636
8637   return subrange_die;
8638 }
8639
8640 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8641    entry that chains various modifiers in front of the given type.  */
8642
8643 static dw_die_ref
8644 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8645                    dw_die_ref context_die)
8646 {
8647   enum tree_code code = TREE_CODE (type);
8648   dw_die_ref mod_type_die;
8649   dw_die_ref sub_die = NULL;
8650   tree item_type = NULL;
8651   tree qualified_type;
8652   tree name;
8653
8654   if (code == ERROR_MARK)
8655     return NULL;
8656
8657   /* See if we already have the appropriately qualified variant of
8658      this type.  */
8659   qualified_type
8660     = get_qualified_type (type,
8661                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8662                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8663
8664   /* If we do, then we can just use its DIE, if it exists.  */
8665   if (qualified_type)
8666     {
8667       mod_type_die = lookup_type_die (qualified_type);
8668       if (mod_type_die)
8669         return mod_type_die;
8670     }
8671
8672   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8673
8674   /* Handle C typedef types.  */
8675   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8676     {
8677       tree dtype = TREE_TYPE (name);
8678
8679       if (qualified_type == dtype)
8680         {
8681           /* For a named type, use the typedef.  */
8682           gen_type_die (qualified_type, context_die);
8683           return lookup_type_die (qualified_type);
8684         }
8685       else if (is_const_type < TYPE_READONLY (dtype)
8686                || is_volatile_type < TYPE_VOLATILE (dtype)
8687                || (is_const_type <= TYPE_READONLY (dtype)
8688                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8689                    && DECL_ORIGINAL_TYPE (name) != type))
8690         /* cv-unqualified version of named type.  Just use the unnamed
8691            type to which it refers.  */
8692         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8693                                   is_const_type, is_volatile_type,
8694                                   context_die);
8695       /* Else cv-qualified version of named type; fall through.  */
8696     }
8697
8698   if (is_const_type)
8699     {
8700       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8701       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8702     }
8703   else if (is_volatile_type)
8704     {
8705       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8706       sub_die = modified_type_die (type, 0, 0, context_die);
8707     }
8708   else if (code == POINTER_TYPE)
8709     {
8710       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8711       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8712                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8713       item_type = TREE_TYPE (type);
8714     }
8715   else if (code == REFERENCE_TYPE)
8716     {
8717       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8718       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8719                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8720       item_type = TREE_TYPE (type);
8721     }
8722   else if (is_subrange_type (type))
8723     {
8724       mod_type_die = subrange_type_die (type, context_die);
8725       item_type = TREE_TYPE (type);
8726     }
8727   else if (is_base_type (type))
8728     mod_type_die = base_type_die (type);
8729   else
8730     {
8731       gen_type_die (type, context_die);
8732
8733       /* We have to get the type_main_variant here (and pass that to the
8734          `lookup_type_die' routine) because the ..._TYPE node we have
8735          might simply be a *copy* of some original type node (where the
8736          copy was created to help us keep track of typedef names) and
8737          that copy might have a different TYPE_UID from the original
8738          ..._TYPE node.  */
8739       if (TREE_CODE (type) != VECTOR_TYPE)
8740         return lookup_type_die (type_main_variant (type));
8741       else
8742         /* Vectors have the debugging information in the type,
8743            not the main variant.  */
8744         return lookup_type_die (type);
8745     }
8746
8747   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8748      don't output a DW_TAG_typedef, since there isn't one in the
8749      user's program; just attach a DW_AT_name to the type.  */
8750   if (name
8751       && (TREE_CODE (name) != TYPE_DECL
8752           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8753     {
8754       if (TREE_CODE (name) == TYPE_DECL)
8755         /* Could just call add_name_and_src_coords_attributes here,
8756            but since this is a builtin type it doesn't have any
8757            useful source coordinates anyway.  */
8758         name = DECL_NAME (name);
8759       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8760     }
8761
8762   if (qualified_type)
8763     equate_type_number_to_die (qualified_type, mod_type_die);
8764
8765   if (item_type)
8766     /* We must do this after the equate_type_number_to_die call, in case
8767        this is a recursive type.  This ensures that the modified_type_die
8768        recursion will terminate even if the type is recursive.  Recursive
8769        types are possible in Ada.  */
8770     sub_die = modified_type_die (item_type,
8771                                  TYPE_READONLY (item_type),
8772                                  TYPE_VOLATILE (item_type),
8773                                  context_die);
8774
8775   if (sub_die != NULL)
8776     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8777
8778   return mod_type_die;
8779 }
8780
8781 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8782    an enumerated type.  */
8783
8784 static inline int
8785 type_is_enum (const_tree type)
8786 {
8787   return TREE_CODE (type) == ENUMERAL_TYPE;
8788 }
8789
8790 /* Return the DBX register number described by a given RTL node.  */
8791
8792 static unsigned int
8793 dbx_reg_number (const_rtx rtl)
8794 {
8795   unsigned regno = REGNO (rtl);
8796
8797   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8798
8799 #ifdef LEAF_REG_REMAP
8800   if (current_function_uses_only_leaf_regs)
8801     {
8802       int leaf_reg = LEAF_REG_REMAP (regno);
8803       if (leaf_reg != -1)
8804         regno = (unsigned) leaf_reg;
8805     }
8806 #endif
8807
8808   return DBX_REGISTER_NUMBER (regno);
8809 }
8810
8811 /* Optionally add a DW_OP_piece term to a location description expression.
8812    DW_OP_piece is only added if the location description expression already
8813    doesn't end with DW_OP_piece.  */
8814
8815 static void
8816 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8817 {
8818   dw_loc_descr_ref loc;
8819
8820   if (*list_head != NULL)
8821     {
8822       /* Find the end of the chain.  */
8823       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8824         ;
8825
8826       if (loc->dw_loc_opc != DW_OP_piece)
8827         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8828     }
8829 }
8830
8831 /* Return a location descriptor that designates a machine register or
8832    zero if there is none.  */
8833
8834 static dw_loc_descr_ref
8835 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8836 {
8837   rtx regs;
8838
8839   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8840     return 0;
8841
8842   regs = targetm.dwarf_register_span (rtl);
8843
8844   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8845     return multiple_reg_loc_descriptor (rtl, regs, initialized);
8846   else
8847     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8848 }
8849
8850 /* Return a location descriptor that designates a machine register for
8851    a given hard register number.  */
8852
8853 static dw_loc_descr_ref
8854 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8855 {
8856   dw_loc_descr_ref reg_loc_descr;
8857   if (regno <= 31)
8858     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8859   else
8860     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8861
8862   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8863     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8864
8865   return reg_loc_descr;
8866 }
8867
8868 /* Given an RTL of a register, return a location descriptor that
8869    designates a value that spans more than one register.  */
8870
8871 static dw_loc_descr_ref
8872 multiple_reg_loc_descriptor (rtx rtl, rtx regs, 
8873                              enum var_init_status initialized)
8874 {
8875   int nregs, size, i;
8876   unsigned reg;
8877   dw_loc_descr_ref loc_result = NULL;
8878
8879   reg = REGNO (rtl);
8880 #ifdef LEAF_REG_REMAP
8881   if (current_function_uses_only_leaf_regs)
8882     {
8883       int leaf_reg = LEAF_REG_REMAP (reg);
8884       if (leaf_reg != -1)
8885         reg = (unsigned) leaf_reg;
8886     }
8887 #endif
8888   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8889   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8890
8891   /* Simple, contiguous registers.  */
8892   if (regs == NULL_RTX)
8893     {
8894       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8895
8896       loc_result = NULL;
8897       while (nregs--)
8898         {
8899           dw_loc_descr_ref t;
8900
8901           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8902                                       VAR_INIT_STATUS_INITIALIZED);
8903           add_loc_descr (&loc_result, t);
8904           add_loc_descr_op_piece (&loc_result, size);
8905           ++reg;
8906         }
8907       return loc_result;
8908     }
8909
8910   /* Now onto stupid register sets in non contiguous locations.  */
8911
8912   gcc_assert (GET_CODE (regs) == PARALLEL);
8913
8914   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8915   loc_result = NULL;
8916
8917   for (i = 0; i < XVECLEN (regs, 0); ++i)
8918     {
8919       dw_loc_descr_ref t;
8920
8921       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8922                                   VAR_INIT_STATUS_INITIALIZED);
8923       add_loc_descr (&loc_result, t);
8924       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8925       add_loc_descr_op_piece (&loc_result, size);
8926     }
8927
8928   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8929     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8930   return loc_result;
8931 }
8932
8933 /* Return a location descriptor that designates a constant.  */
8934
8935 static dw_loc_descr_ref
8936 int_loc_descriptor (HOST_WIDE_INT i)
8937 {
8938   enum dwarf_location_atom op;
8939
8940   /* Pick the smallest representation of a constant, rather than just
8941      defaulting to the LEB encoding.  */
8942   if (i >= 0)
8943     {
8944       if (i <= 31)
8945         op = DW_OP_lit0 + i;
8946       else if (i <= 0xff)
8947         op = DW_OP_const1u;
8948       else if (i <= 0xffff)
8949         op = DW_OP_const2u;
8950       else if (HOST_BITS_PER_WIDE_INT == 32
8951                || i <= 0xffffffff)
8952         op = DW_OP_const4u;
8953       else
8954         op = DW_OP_constu;
8955     }
8956   else
8957     {
8958       if (i >= -0x80)
8959         op = DW_OP_const1s;
8960       else if (i >= -0x8000)
8961         op = DW_OP_const2s;
8962       else if (HOST_BITS_PER_WIDE_INT == 32
8963                || i >= -0x80000000)
8964         op = DW_OP_const4s;
8965       else
8966         op = DW_OP_consts;
8967     }
8968
8969   return new_loc_descr (op, i, 0);
8970 }
8971
8972 /* Return a location descriptor that designates a base+offset location.  */
8973
8974 static dw_loc_descr_ref
8975 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
8976                  enum var_init_status initialized)
8977 {
8978   unsigned int regno;
8979   dw_loc_descr_ref result;
8980
8981   /* We only use "frame base" when we're sure we're talking about the
8982      post-prologue local stack frame.  We do this by *not* running
8983      register elimination until this point, and recognizing the special
8984      argument pointer and soft frame pointer rtx's.  */
8985   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8986     {
8987       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8988
8989       if (elim != reg)
8990         {
8991           if (GET_CODE (elim) == PLUS)
8992             {
8993               offset += INTVAL (XEXP (elim, 1));
8994               elim = XEXP (elim, 0);
8995             }
8996           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8997                       : stack_pointer_rtx));
8998           offset += frame_pointer_fb_offset;
8999
9000           return new_loc_descr (DW_OP_fbreg, offset, 0);
9001         }
9002     }
9003
9004   regno = dbx_reg_number (reg);
9005   if (regno <= 31)
9006     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9007   else
9008     result = new_loc_descr (DW_OP_bregx, regno, offset);
9009
9010   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9011     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9012
9013   return result;
9014 }
9015
9016 /* Return true if this RTL expression describes a base+offset calculation.  */
9017
9018 static inline int
9019 is_based_loc (const_rtx rtl)
9020 {
9021   return (GET_CODE (rtl) == PLUS
9022           && ((REG_P (XEXP (rtl, 0))
9023                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9024                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9025 }
9026
9027 /* Return a descriptor that describes the concatenation of N locations
9028    used to form the address of a memory location.  */
9029
9030 static dw_loc_descr_ref
9031 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9032                             enum var_init_status initialized)
9033 {
9034   unsigned int i;
9035   dw_loc_descr_ref cc_loc_result = NULL;
9036   unsigned int n = XVECLEN (concatn, 0);
9037
9038   for (i = 0; i < n; ++i)
9039     {
9040       dw_loc_descr_ref ref;
9041       rtx x = XVECEXP (concatn, 0, i);
9042
9043       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9044       if (ref == NULL)
9045         return NULL;
9046
9047       add_loc_descr (&cc_loc_result, ref);
9048       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9049     }
9050
9051   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9052     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9053
9054   return cc_loc_result;
9055 }
9056
9057 /* The following routine converts the RTL for a variable or parameter
9058    (resident in memory) into an equivalent Dwarf representation of a
9059    mechanism for getting the address of that same variable onto the top of a
9060    hypothetical "address evaluation" stack.
9061
9062    When creating memory location descriptors, we are effectively transforming
9063    the RTL for a memory-resident object into its Dwarf postfix expression
9064    equivalent.  This routine recursively descends an RTL tree, turning
9065    it into Dwarf postfix code as it goes.
9066
9067    MODE is the mode of the memory reference, needed to handle some
9068    autoincrement addressing modes.
9069
9070    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9071    location list for RTL.
9072
9073    Return 0 if we can't represent the location.  */
9074
9075 static dw_loc_descr_ref
9076 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9077                     enum var_init_status initialized)
9078 {
9079   dw_loc_descr_ref mem_loc_result = NULL;
9080   enum dwarf_location_atom op;
9081
9082   /* Note that for a dynamically sized array, the location we will generate a
9083      description of here will be the lowest numbered location which is
9084      actually within the array.  That's *not* necessarily the same as the
9085      zeroth element of the array.  */
9086
9087   rtl = targetm.delegitimize_address (rtl);
9088
9089   switch (GET_CODE (rtl))
9090     {
9091     case POST_INC:
9092     case POST_DEC:
9093     case POST_MODIFY:
9094       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9095          just fall into the SUBREG code.  */
9096
9097       /* ... fall through ...  */
9098
9099     case SUBREG:
9100       /* The case of a subreg may arise when we have a local (register)
9101          variable or a formal (register) parameter which doesn't quite fill
9102          up an entire register.  For now, just assume that it is
9103          legitimate to make the Dwarf info refer to the whole register which
9104          contains the given subreg.  */
9105       rtl = XEXP (rtl, 0);
9106
9107       /* ... fall through ...  */
9108
9109     case REG:
9110       /* Whenever a register number forms a part of the description of the
9111          method for calculating the (dynamic) address of a memory resident
9112          object, DWARF rules require the register number be referred to as
9113          a "base register".  This distinction is not based in any way upon
9114          what category of register the hardware believes the given register
9115          belongs to.  This is strictly DWARF terminology we're dealing with
9116          here. Note that in cases where the location of a memory-resident
9117          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9118          OP_CONST (0)) the actual DWARF location descriptor that we generate
9119          may just be OP_BASEREG (basereg).  This may look deceptively like
9120          the object in question was allocated to a register (rather than in
9121          memory) so DWARF consumers need to be aware of the subtle
9122          distinction between OP_REG and OP_BASEREG.  */
9123       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9124         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9125       break;
9126
9127     case MEM:
9128       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9129                                            VAR_INIT_STATUS_INITIALIZED);
9130       if (mem_loc_result != 0)
9131         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9132       break;
9133
9134     case LO_SUM:
9135          rtl = XEXP (rtl, 1);
9136
9137       /* ... fall through ...  */
9138
9139     case LABEL_REF:
9140       /* Some ports can transform a symbol ref into a label ref, because
9141          the symbol ref is too far away and has to be dumped into a constant
9142          pool.  */
9143     case CONST:
9144     case SYMBOL_REF:
9145       /* Alternatively, the symbol in the constant pool might be referenced
9146          by a different symbol.  */
9147       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9148         {
9149           bool marked;
9150           rtx tmp = get_pool_constant_mark (rtl, &marked);
9151
9152           if (GET_CODE (tmp) == SYMBOL_REF)
9153             {
9154               rtl = tmp;
9155               if (CONSTANT_POOL_ADDRESS_P (tmp))
9156                 get_pool_constant_mark (tmp, &marked);
9157               else
9158                 marked = true;
9159             }
9160
9161           /* If all references to this pool constant were optimized away,
9162              it was not output and thus we can't represent it.
9163              FIXME: might try to use DW_OP_const_value here, though
9164              DW_OP_piece complicates it.  */
9165           if (!marked)
9166             return 0;
9167         }
9168
9169       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9170       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9171       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9172       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9173       break;
9174
9175     case PRE_MODIFY:
9176       /* Extract the PLUS expression nested inside and fall into
9177          PLUS code below.  */
9178       rtl = XEXP (rtl, 1);
9179       goto plus;
9180
9181     case PRE_INC:
9182     case PRE_DEC:
9183       /* Turn these into a PLUS expression and fall into the PLUS code
9184          below.  */
9185       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9186                           GEN_INT (GET_CODE (rtl) == PRE_INC
9187                                    ? GET_MODE_UNIT_SIZE (mode)
9188                                    : -GET_MODE_UNIT_SIZE (mode)));
9189
9190       /* ... fall through ...  */
9191
9192     case PLUS:
9193     plus:
9194       if (is_based_loc (rtl))
9195         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9196                                           INTVAL (XEXP (rtl, 1)),
9197                                           VAR_INIT_STATUS_INITIALIZED);
9198       else
9199         {
9200           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9201                                                VAR_INIT_STATUS_INITIALIZED);
9202           if (mem_loc_result == 0)
9203             break;
9204
9205           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9206               && INTVAL (XEXP (rtl, 1)) >= 0)
9207             add_loc_descr (&mem_loc_result,
9208                            new_loc_descr (DW_OP_plus_uconst,
9209                                           INTVAL (XEXP (rtl, 1)), 0));
9210           else
9211             {
9212               add_loc_descr (&mem_loc_result,
9213                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9214                                                  VAR_INIT_STATUS_INITIALIZED));
9215               add_loc_descr (&mem_loc_result,
9216                              new_loc_descr (DW_OP_plus, 0, 0));
9217             }
9218         }
9219       break;
9220
9221     /* If a pseudo-reg is optimized away, it is possible for it to
9222        be replaced with a MEM containing a multiply or shift.  */
9223     case MULT:
9224       op = DW_OP_mul;
9225       goto do_binop;
9226
9227     case ASHIFT:
9228       op = DW_OP_shl;
9229       goto do_binop;
9230
9231     case ASHIFTRT:
9232       op = DW_OP_shra;
9233       goto do_binop;
9234
9235     case LSHIFTRT:
9236       op = DW_OP_shr;
9237       goto do_binop;
9238
9239     do_binop:
9240       {
9241         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9242                                                    VAR_INIT_STATUS_INITIALIZED);
9243         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9244                                                    VAR_INIT_STATUS_INITIALIZED);
9245
9246         if (op0 == 0 || op1 == 0)
9247           break;
9248
9249         mem_loc_result = op0;
9250         add_loc_descr (&mem_loc_result, op1);
9251         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9252         break;
9253       }
9254
9255     case CONST_INT:
9256       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9257       break;
9258
9259     case CONCATN:
9260       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode, 
9261                                                    VAR_INIT_STATUS_INITIALIZED);
9262       break;
9263
9264     default:
9265       gcc_unreachable ();
9266     }
9267
9268   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9269     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9270
9271   return mem_loc_result;
9272 }
9273
9274 /* Return a descriptor that describes the concatenation of two locations.
9275    This is typically a complex variable.  */
9276
9277 static dw_loc_descr_ref
9278 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9279 {
9280   dw_loc_descr_ref cc_loc_result = NULL;
9281   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9282   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9283
9284   if (x0_ref == 0 || x1_ref == 0)
9285     return 0;
9286
9287   cc_loc_result = x0_ref;
9288   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9289
9290   add_loc_descr (&cc_loc_result, x1_ref);
9291   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9292
9293   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9294     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9295
9296   return cc_loc_result;
9297 }
9298
9299 /* Return a descriptor that describes the concatenation of N
9300    locations.  */
9301
9302 static dw_loc_descr_ref
9303 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9304 {
9305   unsigned int i;
9306   dw_loc_descr_ref cc_loc_result = NULL;
9307   unsigned int n = XVECLEN (concatn, 0);
9308
9309   for (i = 0; i < n; ++i)
9310     {
9311       dw_loc_descr_ref ref;
9312       rtx x = XVECEXP (concatn, 0, i);
9313
9314       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9315       if (ref == NULL)
9316         return NULL;
9317
9318       add_loc_descr (&cc_loc_result, ref);
9319       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9320     }
9321
9322   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9323     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9324
9325   return cc_loc_result;
9326 }
9327
9328 /* Output a proper Dwarf location descriptor for a variable or parameter
9329    which is either allocated in a register or in a memory location.  For a
9330    register, we just generate an OP_REG and the register number.  For a
9331    memory location we provide a Dwarf postfix expression describing how to
9332    generate the (dynamic) address of the object onto the address stack.
9333
9334    If we don't know how to describe it, return 0.  */
9335
9336 static dw_loc_descr_ref
9337 loc_descriptor (rtx rtl, enum var_init_status initialized)
9338 {
9339   dw_loc_descr_ref loc_result = NULL;
9340
9341   switch (GET_CODE (rtl))
9342     {
9343     case SUBREG:
9344       /* The case of a subreg may arise when we have a local (register)
9345          variable or a formal (register) parameter which doesn't quite fill
9346          up an entire register.  For now, just assume that it is
9347          legitimate to make the Dwarf info refer to the whole register which
9348          contains the given subreg.  */
9349       rtl = SUBREG_REG (rtl);
9350
9351       /* ... fall through ...  */
9352
9353     case REG:
9354       loc_result = reg_loc_descriptor (rtl, initialized);
9355       break;
9356
9357     case MEM:
9358       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9359                                        initialized);
9360       break;
9361
9362     case CONCAT:
9363       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9364                                           initialized);
9365       break;
9366
9367     case CONCATN:
9368       loc_result = concatn_loc_descriptor (rtl, initialized);
9369       break;
9370
9371     case VAR_LOCATION:
9372       /* Single part.  */
9373       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9374         {
9375           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9376           break;
9377         }
9378
9379       rtl = XEXP (rtl, 1);
9380       /* FALLTHRU */
9381
9382     case PARALLEL:
9383       {
9384         rtvec par_elems = XVEC (rtl, 0);
9385         int num_elem = GET_NUM_ELEM (par_elems);
9386         enum machine_mode mode;
9387         int i;
9388
9389         /* Create the first one, so we have something to add to.  */
9390         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9391                                      initialized);
9392         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9393         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9394         for (i = 1; i < num_elem; i++)
9395           {
9396             dw_loc_descr_ref temp;
9397
9398             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9399                                    initialized);
9400             add_loc_descr (&loc_result, temp);
9401             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9402             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9403           }
9404       }
9405       break;
9406
9407     default:
9408       gcc_unreachable ();
9409     }
9410
9411   return loc_result;
9412 }
9413
9414 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9415    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9416    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9417    top-level invocation, and we require the address of LOC; is 0 if we require
9418    the value of LOC.  */
9419
9420 static dw_loc_descr_ref
9421 loc_descriptor_from_tree_1 (tree loc, int want_address)
9422 {
9423   dw_loc_descr_ref ret, ret1;
9424   int have_address = 0;
9425   enum dwarf_location_atom op;
9426
9427   /* ??? Most of the time we do not take proper care for sign/zero
9428      extending the values properly.  Hopefully this won't be a real
9429      problem...  */
9430
9431   switch (TREE_CODE (loc))
9432     {
9433     case ERROR_MARK:
9434       return 0;
9435
9436     case PLACEHOLDER_EXPR:
9437       /* This case involves extracting fields from an object to determine the
9438          position of other fields.  We don't try to encode this here.  The
9439          only user of this is Ada, which encodes the needed information using
9440          the names of types.  */
9441       return 0;
9442
9443     case CALL_EXPR:
9444       return 0;
9445
9446     case PREINCREMENT_EXPR:
9447     case PREDECREMENT_EXPR:
9448     case POSTINCREMENT_EXPR:
9449     case POSTDECREMENT_EXPR:
9450       /* There are no opcodes for these operations.  */
9451       return 0;
9452
9453     case ADDR_EXPR:
9454       /* If we already want an address, there's nothing we can do.  */
9455       if (want_address)
9456         return 0;
9457
9458       /* Otherwise, process the argument and look for the address.  */
9459       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9460
9461     case VAR_DECL:
9462       if (DECL_THREAD_LOCAL_P (loc))
9463         {
9464           rtx rtl;
9465
9466           /* If this is not defined, we have no way to emit the data.  */
9467           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9468             return 0;
9469
9470           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9471              look up addresses of objects in the current module.  */
9472           if (DECL_EXTERNAL (loc))
9473             return 0;
9474
9475           rtl = rtl_for_decl_location (loc);
9476           if (rtl == NULL_RTX)
9477             return 0;
9478
9479           if (!MEM_P (rtl))
9480             return 0;
9481           rtl = XEXP (rtl, 0);
9482           if (! CONSTANT_P (rtl))
9483             return 0;
9484
9485           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9486           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9487           ret->dw_loc_oprnd1.v.val_addr = rtl;
9488
9489           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9490           add_loc_descr (&ret, ret1);
9491
9492           have_address = 1;
9493           break;
9494         }
9495       /* FALLTHRU */
9496
9497     case PARM_DECL:
9498       if (DECL_HAS_VALUE_EXPR_P (loc))
9499         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9500                                            want_address);
9501       /* FALLTHRU */
9502
9503     case RESULT_DECL:
9504     case FUNCTION_DECL:
9505       {
9506         rtx rtl = rtl_for_decl_location (loc);
9507
9508         if (rtl == NULL_RTX)
9509           return 0;
9510         else if (GET_CODE (rtl) == CONST_INT)
9511           {
9512             HOST_WIDE_INT val = INTVAL (rtl);
9513             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9514               val &= GET_MODE_MASK (DECL_MODE (loc));
9515             ret = int_loc_descriptor (val);
9516           }
9517         else if (GET_CODE (rtl) == CONST_STRING)
9518           return 0;
9519         else if (CONSTANT_P (rtl))
9520           {
9521             ret = new_loc_descr (DW_OP_addr, 0, 0);
9522             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9523             ret->dw_loc_oprnd1.v.val_addr = rtl;
9524           }
9525         else
9526           {
9527             enum machine_mode mode;
9528
9529             /* Certain constructs can only be represented at top-level.  */
9530             if (want_address == 2)
9531               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9532
9533             mode = GET_MODE (rtl);
9534             if (MEM_P (rtl))
9535               {
9536                 rtl = XEXP (rtl, 0);
9537                 have_address = 1;
9538               }
9539             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9540           }
9541       }
9542       break;
9543
9544     case INDIRECT_REF:
9545       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9546       have_address = 1;
9547       break;
9548
9549     case COMPOUND_EXPR:
9550       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9551
9552     case NOP_EXPR:
9553     case CONVERT_EXPR:
9554     case NON_LVALUE_EXPR:
9555     case VIEW_CONVERT_EXPR:
9556     case SAVE_EXPR:
9557     case GIMPLE_MODIFY_STMT:
9558       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9559                                          want_address);
9560
9561     case COMPONENT_REF:
9562     case BIT_FIELD_REF:
9563     case ARRAY_REF:
9564     case ARRAY_RANGE_REF:
9565       {
9566         tree obj, offset;
9567         HOST_WIDE_INT bitsize, bitpos, bytepos;
9568         enum machine_mode mode;
9569         int volatilep;
9570         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9571
9572         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9573                                    &unsignedp, &volatilep, false);
9574
9575         if (obj == loc)
9576           return 0;
9577
9578         ret = loc_descriptor_from_tree_1 (obj, 1);
9579         if (ret == 0
9580             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9581           return 0;
9582
9583         if (offset != NULL_TREE)
9584           {
9585             /* Variable offset.  */
9586             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9587             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9588           }
9589
9590         bytepos = bitpos / BITS_PER_UNIT;
9591         if (bytepos > 0)
9592           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9593         else if (bytepos < 0)
9594           {
9595             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9596             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9597           }
9598
9599         have_address = 1;
9600         break;
9601       }
9602
9603     case INTEGER_CST:
9604       if (host_integerp (loc, 0))
9605         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9606       else
9607         return 0;
9608       break;
9609
9610     case CONSTRUCTOR:
9611       {
9612         /* Get an RTL for this, if something has been emitted.  */
9613         rtx rtl = lookup_constant_def (loc);
9614         enum machine_mode mode;
9615
9616         if (!rtl || !MEM_P (rtl))
9617           return 0;
9618         mode = GET_MODE (rtl);
9619         rtl = XEXP (rtl, 0);
9620         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9621         have_address = 1;
9622         break;
9623       }
9624
9625     case TRUTH_AND_EXPR:
9626     case TRUTH_ANDIF_EXPR:
9627     case BIT_AND_EXPR:
9628       op = DW_OP_and;
9629       goto do_binop;
9630
9631     case TRUTH_XOR_EXPR:
9632     case BIT_XOR_EXPR:
9633       op = DW_OP_xor;
9634       goto do_binop;
9635
9636     case TRUTH_OR_EXPR:
9637     case TRUTH_ORIF_EXPR:
9638     case BIT_IOR_EXPR:
9639       op = DW_OP_or;
9640       goto do_binop;
9641
9642     case FLOOR_DIV_EXPR:
9643     case CEIL_DIV_EXPR:
9644     case ROUND_DIV_EXPR:
9645     case TRUNC_DIV_EXPR:
9646       op = DW_OP_div;
9647       goto do_binop;
9648
9649     case MINUS_EXPR:
9650       op = DW_OP_minus;
9651       goto do_binop;
9652
9653     case FLOOR_MOD_EXPR:
9654     case CEIL_MOD_EXPR:
9655     case ROUND_MOD_EXPR:
9656     case TRUNC_MOD_EXPR:
9657       op = DW_OP_mod;
9658       goto do_binop;
9659
9660     case MULT_EXPR:
9661       op = DW_OP_mul;
9662       goto do_binop;
9663
9664     case LSHIFT_EXPR:
9665       op = DW_OP_shl;
9666       goto do_binop;
9667
9668     case RSHIFT_EXPR:
9669       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9670       goto do_binop;
9671
9672     case POINTER_PLUS_EXPR:
9673     case PLUS_EXPR:
9674       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9675           && host_integerp (TREE_OPERAND (loc, 1), 0))
9676         {
9677           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9678           if (ret == 0)
9679             return 0;
9680
9681           add_loc_descr (&ret,
9682                          new_loc_descr (DW_OP_plus_uconst,
9683                                         tree_low_cst (TREE_OPERAND (loc, 1),
9684                                                       0),
9685                                         0));
9686           break;
9687         }
9688
9689       op = DW_OP_plus;
9690       goto do_binop;
9691
9692     case LE_EXPR:
9693       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9694         return 0;
9695
9696       op = DW_OP_le;
9697       goto do_binop;
9698
9699     case GE_EXPR:
9700       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9701         return 0;
9702
9703       op = DW_OP_ge;
9704       goto do_binop;
9705
9706     case LT_EXPR:
9707       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9708         return 0;
9709
9710       op = DW_OP_lt;
9711       goto do_binop;
9712
9713     case GT_EXPR:
9714       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9715         return 0;
9716
9717       op = DW_OP_gt;
9718       goto do_binop;
9719
9720     case EQ_EXPR:
9721       op = DW_OP_eq;
9722       goto do_binop;
9723
9724     case NE_EXPR:
9725       op = DW_OP_ne;
9726       goto do_binop;
9727
9728     do_binop:
9729       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9730       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9731       if (ret == 0 || ret1 == 0)
9732         return 0;
9733
9734       add_loc_descr (&ret, ret1);
9735       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9736       break;
9737
9738     case TRUTH_NOT_EXPR:
9739     case BIT_NOT_EXPR:
9740       op = DW_OP_not;
9741       goto do_unop;
9742
9743     case ABS_EXPR:
9744       op = DW_OP_abs;
9745       goto do_unop;
9746
9747     case NEGATE_EXPR:
9748       op = DW_OP_neg;
9749       goto do_unop;
9750
9751     do_unop:
9752       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9753       if (ret == 0)
9754         return 0;
9755
9756       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9757       break;
9758
9759     case MIN_EXPR:
9760     case MAX_EXPR:
9761       {
9762         const enum tree_code code =
9763           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9764
9765         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9766                       build2 (code, integer_type_node,
9767                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9768                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9769       }
9770
9771       /* ... fall through ...  */
9772
9773     case COND_EXPR:
9774       {
9775         dw_loc_descr_ref lhs
9776           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9777         dw_loc_descr_ref rhs
9778           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9779         dw_loc_descr_ref bra_node, jump_node, tmp;
9780
9781         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9782         if (ret == 0 || lhs == 0 || rhs == 0)
9783           return 0;
9784
9785         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9786         add_loc_descr (&ret, bra_node);
9787
9788         add_loc_descr (&ret, rhs);
9789         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9790         add_loc_descr (&ret, jump_node);
9791
9792         add_loc_descr (&ret, lhs);
9793         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9794         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9795
9796         /* ??? Need a node to point the skip at.  Use a nop.  */
9797         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9798         add_loc_descr (&ret, tmp);
9799         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9800         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9801       }
9802       break;
9803
9804     case FIX_TRUNC_EXPR:
9805       return 0;
9806
9807     default:
9808       /* Leave front-end specific codes as simply unknown.  This comes
9809          up, for instance, with the C STMT_EXPR.  */
9810       if ((unsigned int) TREE_CODE (loc)
9811           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9812         return 0;
9813
9814 #ifdef ENABLE_CHECKING
9815       /* Otherwise this is a generic code; we should just lists all of
9816          these explicitly.  We forgot one.  */
9817       gcc_unreachable ();
9818 #else
9819       /* In a release build, we want to degrade gracefully: better to
9820          generate incomplete debugging information than to crash.  */
9821       return NULL;
9822 #endif
9823     }
9824
9825   /* Show if we can't fill the request for an address.  */
9826   if (want_address && !have_address)
9827     return 0;
9828
9829   /* If we've got an address and don't want one, dereference.  */
9830   if (!want_address && have_address && ret)
9831     {
9832       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9833
9834       if (size > DWARF2_ADDR_SIZE || size == -1)
9835         return 0;
9836       else if (size == DWARF2_ADDR_SIZE)
9837         op = DW_OP_deref;
9838       else
9839         op = DW_OP_deref_size;
9840
9841       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9842     }
9843
9844   return ret;
9845 }
9846
9847 static inline dw_loc_descr_ref
9848 loc_descriptor_from_tree (tree loc)
9849 {
9850   return loc_descriptor_from_tree_1 (loc, 2);
9851 }
9852
9853 /* Given a value, round it up to the lowest multiple of `boundary'
9854    which is not less than the value itself.  */
9855
9856 static inline HOST_WIDE_INT
9857 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9858 {
9859   return (((value + boundary - 1) / boundary) * boundary);
9860 }
9861
9862 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9863    pointer to the declared type for the relevant field variable, or return
9864    `integer_type_node' if the given node turns out to be an
9865    ERROR_MARK node.  */
9866
9867 static inline tree
9868 field_type (const_tree decl)
9869 {
9870   tree type;
9871
9872   if (TREE_CODE (decl) == ERROR_MARK)
9873     return integer_type_node;
9874
9875   type = DECL_BIT_FIELD_TYPE (decl);
9876   if (type == NULL_TREE)
9877     type = TREE_TYPE (decl);
9878
9879   return type;
9880 }
9881
9882 /* Given a pointer to a tree node, return the alignment in bits for
9883    it, or else return BITS_PER_WORD if the node actually turns out to
9884    be an ERROR_MARK node.  */
9885
9886 static inline unsigned
9887 simple_type_align_in_bits (const_tree type)
9888 {
9889   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9890 }
9891
9892 static inline unsigned
9893 simple_decl_align_in_bits (const_tree decl)
9894 {
9895   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9896 }
9897
9898 /* Return the result of rounding T up to ALIGN.  */
9899
9900 static inline HOST_WIDE_INT
9901 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9902 {
9903   /* We must be careful if T is negative because HOST_WIDE_INT can be
9904      either "above" or "below" unsigned int as per the C promotion
9905      rules, depending on the host, thus making the signedness of the
9906      direct multiplication and division unpredictable.  */
9907   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9908
9909   u += align - 1;
9910   u /= align;
9911   u *= align;
9912
9913   return (HOST_WIDE_INT) u;
9914 }
9915
9916 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9917    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9918    or return 0 if we are unable to determine what that offset is, either
9919    because the argument turns out to be a pointer to an ERROR_MARK node, or
9920    because the offset is actually variable.  (We can't handle the latter case
9921    just yet).  */
9922
9923 static HOST_WIDE_INT
9924 field_byte_offset (const_tree decl)
9925 {
9926   HOST_WIDE_INT object_offset_in_bits;
9927   HOST_WIDE_INT bitpos_int;
9928
9929   if (TREE_CODE (decl) == ERROR_MARK)
9930     return 0;
9931
9932   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9933
9934   /* We cannot yet cope with fields whose positions are variable, so
9935      for now, when we see such things, we simply return 0.  Someday, we may
9936      be able to handle such cases, but it will be damn difficult.  */
9937   if (! host_integerp (bit_position (decl), 0))
9938     return 0;
9939
9940   bitpos_int = int_bit_position (decl);
9941
9942 #ifdef PCC_BITFIELD_TYPE_MATTERS
9943   if (PCC_BITFIELD_TYPE_MATTERS)
9944     {
9945       tree type;
9946       tree field_size_tree;
9947       HOST_WIDE_INT deepest_bitpos;
9948       unsigned HOST_WIDE_INT field_size_in_bits;
9949       unsigned int type_align_in_bits;
9950       unsigned int decl_align_in_bits;
9951       unsigned HOST_WIDE_INT type_size_in_bits;
9952
9953       type = field_type (decl);
9954       field_size_tree = DECL_SIZE (decl);
9955
9956       /* The size could be unspecified if there was an error, or for
9957          a flexible array member.  */
9958       if (! field_size_tree)
9959         field_size_tree = bitsize_zero_node;
9960
9961       /* If we don't know the size of the field, pretend it's a full word.  */
9962       if (host_integerp (field_size_tree, 1))
9963         field_size_in_bits = tree_low_cst (field_size_tree, 1);
9964       else
9965         field_size_in_bits = BITS_PER_WORD;
9966
9967       type_size_in_bits = simple_type_size_in_bits (type);
9968       type_align_in_bits = simple_type_align_in_bits (type);
9969       decl_align_in_bits = simple_decl_align_in_bits (decl);
9970
9971       /* The GCC front-end doesn't make any attempt to keep track of the
9972          starting bit offset (relative to the start of the containing
9973          structure type) of the hypothetical "containing object" for a
9974          bit-field.  Thus, when computing the byte offset value for the
9975          start of the "containing object" of a bit-field, we must deduce
9976          this information on our own. This can be rather tricky to do in
9977          some cases.  For example, handling the following structure type
9978          definition when compiling for an i386/i486 target (which only
9979          aligns long long's to 32-bit boundaries) can be very tricky:
9980
9981          struct S { int field1; long long field2:31; };
9982
9983          Fortunately, there is a simple rule-of-thumb which can be used
9984          in such cases.  When compiling for an i386/i486, GCC will
9985          allocate 8 bytes for the structure shown above.  It decides to
9986          do this based upon one simple rule for bit-field allocation.
9987          GCC allocates each "containing object" for each bit-field at
9988          the first (i.e. lowest addressed) legitimate alignment boundary
9989          (based upon the required minimum alignment for the declared
9990          type of the field) which it can possibly use, subject to the
9991          condition that there is still enough available space remaining
9992          in the containing object (when allocated at the selected point)
9993          to fully accommodate all of the bits of the bit-field itself.
9994
9995          This simple rule makes it obvious why GCC allocates 8 bytes for
9996          each object of the structure type shown above.  When looking
9997          for a place to allocate the "containing object" for `field2',
9998          the compiler simply tries to allocate a 64-bit "containing
9999          object" at each successive 32-bit boundary (starting at zero)
10000          until it finds a place to allocate that 64- bit field such that
10001          at least 31 contiguous (and previously unallocated) bits remain
10002          within that selected 64 bit field.  (As it turns out, for the
10003          example above, the compiler finds it is OK to allocate the
10004          "containing object" 64-bit field at bit-offset zero within the
10005          structure type.)
10006
10007          Here we attempt to work backwards from the limited set of facts
10008          we're given, and we try to deduce from those facts, where GCC
10009          must have believed that the containing object started (within
10010          the structure type). The value we deduce is then used (by the
10011          callers of this routine) to generate DW_AT_location and
10012          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10013          the case of DW_AT_location, regular fields as well).  */
10014
10015       /* Figure out the bit-distance from the start of the structure to
10016          the "deepest" bit of the bit-field.  */
10017       deepest_bitpos = bitpos_int + field_size_in_bits;
10018
10019       /* This is the tricky part.  Use some fancy footwork to deduce
10020          where the lowest addressed bit of the containing object must
10021          be.  */
10022       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10023
10024       /* Round up to type_align by default.  This works best for
10025          bitfields.  */
10026       object_offset_in_bits
10027         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10028
10029       if (object_offset_in_bits > bitpos_int)
10030         {
10031           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10032
10033           /* Round up to decl_align instead.  */
10034           object_offset_in_bits
10035             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10036         }
10037     }
10038   else
10039 #endif
10040     object_offset_in_bits = bitpos_int;
10041
10042   return object_offset_in_bits / BITS_PER_UNIT;
10043 }
10044 \f
10045 /* The following routines define various Dwarf attributes and any data
10046    associated with them.  */
10047
10048 /* Add a location description attribute value to a DIE.
10049
10050    This emits location attributes suitable for whole variables and
10051    whole parameters.  Note that the location attributes for struct fields are
10052    generated by the routine `data_member_location_attribute' below.  */
10053
10054 static inline void
10055 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10056                              dw_loc_descr_ref descr)
10057 {
10058   if (descr != 0)
10059     add_AT_loc (die, attr_kind, descr);
10060 }
10061
10062 /* Attach the specialized form of location attribute used for data members of
10063    struct and union types.  In the special case of a FIELD_DECL node which
10064    represents a bit-field, the "offset" part of this special location
10065    descriptor must indicate the distance in bytes from the lowest-addressed
10066    byte of the containing struct or union type to the lowest-addressed byte of
10067    the "containing object" for the bit-field.  (See the `field_byte_offset'
10068    function above).
10069
10070    For any given bit-field, the "containing object" is a hypothetical object
10071    (of some integral or enum type) within which the given bit-field lives.  The
10072    type of this hypothetical "containing object" is always the same as the
10073    declared type of the individual bit-field itself (for GCC anyway... the
10074    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10075    bytes) of the hypothetical "containing object" which will be given in the
10076    DW_AT_byte_size attribute for this bit-field.  (See the
10077    `byte_size_attribute' function below.)  It is also used when calculating the
10078    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10079    function below.)  */
10080
10081 static void
10082 add_data_member_location_attribute (dw_die_ref die, tree decl)
10083 {
10084   HOST_WIDE_INT offset;
10085   dw_loc_descr_ref loc_descr = 0;
10086
10087   if (TREE_CODE (decl) == TREE_BINFO)
10088     {
10089       /* We're working on the TAG_inheritance for a base class.  */
10090       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10091         {
10092           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10093              aren't at a fixed offset from all (sub)objects of the same
10094              type.  We need to extract the appropriate offset from our
10095              vtable.  The following dwarf expression means
10096
10097                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10098
10099              This is specific to the V3 ABI, of course.  */
10100
10101           dw_loc_descr_ref tmp;
10102
10103           /* Make a copy of the object address.  */
10104           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10105           add_loc_descr (&loc_descr, tmp);
10106
10107           /* Extract the vtable address.  */
10108           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10109           add_loc_descr (&loc_descr, tmp);
10110
10111           /* Calculate the address of the offset.  */
10112           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10113           gcc_assert (offset < 0);
10114
10115           tmp = int_loc_descriptor (-offset);
10116           add_loc_descr (&loc_descr, tmp);
10117           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10118           add_loc_descr (&loc_descr, tmp);
10119
10120           /* Extract the offset.  */
10121           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10122           add_loc_descr (&loc_descr, tmp);
10123
10124           /* Add it to the object address.  */
10125           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10126           add_loc_descr (&loc_descr, tmp);
10127         }
10128       else
10129         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10130     }
10131   else
10132     offset = field_byte_offset (decl);
10133
10134   if (! loc_descr)
10135     {
10136       enum dwarf_location_atom op;
10137
10138       /* The DWARF2 standard says that we should assume that the structure
10139          address is already on the stack, so we can specify a structure field
10140          address by using DW_OP_plus_uconst.  */
10141
10142 #ifdef MIPS_DEBUGGING_INFO
10143       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10144          operator correctly.  It works only if we leave the offset on the
10145          stack.  */
10146       op = DW_OP_constu;
10147 #else
10148       op = DW_OP_plus_uconst;
10149 #endif
10150
10151       loc_descr = new_loc_descr (op, offset, 0);
10152     }
10153
10154   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10155 }
10156
10157 /* Writes integer values to dw_vec_const array.  */
10158
10159 static void
10160 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10161 {
10162   while (size != 0)
10163     {
10164       *dest++ = val & 0xff;
10165       val >>= 8;
10166       --size;
10167     }
10168 }
10169
10170 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10171
10172 static HOST_WIDE_INT
10173 extract_int (const unsigned char *src, unsigned int size)
10174 {
10175   HOST_WIDE_INT val = 0;
10176
10177   src += size;
10178   while (size != 0)
10179     {
10180       val <<= 8;
10181       val |= *--src & 0xff;
10182       --size;
10183     }
10184   return val;
10185 }
10186
10187 /* Writes floating point values to dw_vec_const array.  */
10188
10189 static void
10190 insert_float (const_rtx rtl, unsigned char *array)
10191 {
10192   REAL_VALUE_TYPE rv;
10193   long val[4];
10194   int i;
10195
10196   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10197   real_to_target (val, &rv, GET_MODE (rtl));
10198
10199   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10200   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10201     {
10202       insert_int (val[i], 4, array);
10203       array += 4;
10204     }
10205 }
10206
10207 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10208    does not have a "location" either in memory or in a register.  These
10209    things can arise in GNU C when a constant is passed as an actual parameter
10210    to an inlined function.  They can also arise in C++ where declared
10211    constants do not necessarily get memory "homes".  */
10212
10213 static void
10214 add_const_value_attribute (dw_die_ref die, rtx rtl)
10215 {
10216   switch (GET_CODE (rtl))
10217     {
10218     case CONST_INT:
10219       {
10220         HOST_WIDE_INT val = INTVAL (rtl);
10221
10222         if (val < 0)
10223           add_AT_int (die, DW_AT_const_value, val);
10224         else
10225           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10226       }
10227       break;
10228
10229     case CONST_DOUBLE:
10230       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10231          floating-point constant.  A CONST_DOUBLE is used whenever the
10232          constant requires more than one word in order to be adequately
10233          represented.  We output CONST_DOUBLEs as blocks.  */
10234       {
10235         enum machine_mode mode = GET_MODE (rtl);
10236
10237         if (SCALAR_FLOAT_MODE_P (mode))
10238           {
10239             unsigned int length = GET_MODE_SIZE (mode);
10240             unsigned char *array = ggc_alloc (length);
10241
10242             insert_float (rtl, array);
10243             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10244           }
10245         else
10246           {
10247             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10248             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10249
10250             add_AT_long_long (die, DW_AT_const_value,
10251                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10252           }
10253       }
10254       break;
10255
10256     case CONST_VECTOR:
10257       {
10258         enum machine_mode mode = GET_MODE (rtl);
10259         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10260         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10261         unsigned char *array = ggc_alloc (length * elt_size);
10262         unsigned int i;
10263         unsigned char *p;
10264
10265         switch (GET_MODE_CLASS (mode))
10266           {
10267           case MODE_VECTOR_INT:
10268             for (i = 0, p = array; i < length; i++, p += elt_size)
10269               {
10270                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10271                 HOST_WIDE_INT lo, hi;
10272
10273                 switch (GET_CODE (elt))
10274                   {
10275                   case CONST_INT:
10276                     lo = INTVAL (elt);
10277                     hi = -(lo < 0);
10278                     break;
10279
10280                   case CONST_DOUBLE:
10281                     lo = CONST_DOUBLE_LOW (elt);
10282                     hi = CONST_DOUBLE_HIGH (elt);
10283                     break;
10284
10285                   default:
10286                     gcc_unreachable ();
10287                   }
10288
10289                 if (elt_size <= sizeof (HOST_WIDE_INT))
10290                   insert_int (lo, elt_size, p);
10291                 else
10292                   {
10293                     unsigned char *p0 = p;
10294                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10295
10296                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10297                     if (WORDS_BIG_ENDIAN)
10298                       {
10299                         p0 = p1;
10300                         p1 = p;
10301                       }
10302                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10303                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10304                   }
10305               }
10306             break;
10307
10308           case MODE_VECTOR_FLOAT:
10309             for (i = 0, p = array; i < length; i++, p += elt_size)
10310               {
10311                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10312                 insert_float (elt, p);
10313               }
10314             break;
10315
10316           default:
10317             gcc_unreachable ();
10318           }
10319
10320         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10321       }
10322       break;
10323
10324     case CONST_STRING:
10325       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10326       break;
10327
10328     case SYMBOL_REF:
10329     case LABEL_REF:
10330     case CONST:
10331       add_AT_addr (die, DW_AT_const_value, rtl);
10332       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10333       break;
10334
10335     case PLUS:
10336       /* In cases where an inlined instance of an inline function is passed
10337          the address of an `auto' variable (which is local to the caller) we
10338          can get a situation where the DECL_RTL of the artificial local
10339          variable (for the inlining) which acts as a stand-in for the
10340          corresponding formal parameter (of the inline function) will look
10341          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10342          exactly a compile-time constant expression, but it isn't the address
10343          of the (artificial) local variable either.  Rather, it represents the
10344          *value* which the artificial local variable always has during its
10345          lifetime.  We currently have no way to represent such quasi-constant
10346          values in Dwarf, so for now we just punt and generate nothing.  */
10347       break;
10348
10349     default:
10350       /* No other kinds of rtx should be possible here.  */
10351       gcc_unreachable ();
10352     }
10353
10354 }
10355
10356 /* Determine whether the evaluation of EXPR references any variables
10357    or functions which aren't otherwise used (and therefore may not be
10358    output).  */
10359 static tree
10360 reference_to_unused (tree * tp, int * walk_subtrees,
10361                      void * data ATTRIBUTE_UNUSED)
10362 {
10363   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10364     *walk_subtrees = 0;
10365
10366   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10367       && ! TREE_ASM_WRITTEN (*tp))
10368     return *tp;
10369   else if (!flag_unit_at_a_time)
10370     return NULL_TREE;
10371   /* ???  The C++ FE emits debug information for using decls, so
10372      putting gcc_unreachable here falls over.  See PR31899.  For now
10373      be conservative.  */
10374   else if (!cgraph_global_info_ready
10375            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10376     return *tp;
10377   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10378     {
10379       struct varpool_node *node = varpool_node (*tp);
10380       if (!node->needed)
10381         return *tp;
10382     }
10383   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10384            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10385     {
10386       struct cgraph_node *node = cgraph_node (*tp);
10387       if (!node->output)
10388         return *tp;
10389     }
10390   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10391     return *tp;
10392
10393   return NULL_TREE;
10394 }
10395
10396 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10397    for use in a later add_const_value_attribute call.  */
10398
10399 static rtx
10400 rtl_for_decl_init (tree init, tree type)
10401 {
10402   rtx rtl = NULL_RTX;
10403
10404   /* If a variable is initialized with a string constant without embedded
10405      zeros, build CONST_STRING.  */
10406   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10407     {
10408       tree enttype = TREE_TYPE (type);
10409       tree domain = TYPE_DOMAIN (type);
10410       enum machine_mode mode = TYPE_MODE (enttype);
10411
10412       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10413           && domain
10414           && integer_zerop (TYPE_MIN_VALUE (domain))
10415           && compare_tree_int (TYPE_MAX_VALUE (domain),
10416                                TREE_STRING_LENGTH (init) - 1) == 0
10417           && ((size_t) TREE_STRING_LENGTH (init)
10418               == strlen (TREE_STRING_POINTER (init)) + 1))
10419         rtl = gen_rtx_CONST_STRING (VOIDmode,
10420                                     ggc_strdup (TREE_STRING_POINTER (init)));
10421     }
10422   /* Other aggregates, and complex values, could be represented using
10423      CONCAT: FIXME!  */
10424   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10425     ;
10426   /* Vectors only work if their mode is supported by the target.
10427      FIXME: generic vectors ought to work too.  */
10428   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10429     ;
10430   /* If the initializer is something that we know will expand into an
10431      immediate RTL constant, expand it now.  We must be careful not to
10432      reference variables which won't be output.  */
10433   else if (initializer_constant_valid_p (init, type)
10434            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10435     {
10436       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10437          possible.  */
10438       if (TREE_CODE (type) == VECTOR_TYPE)
10439         switch (TREE_CODE (init))
10440           {
10441           case VECTOR_CST:
10442             break;
10443           case CONSTRUCTOR:
10444             if (TREE_CONSTANT (init))
10445               {
10446                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10447                 bool constant_p = true;
10448                 tree value;
10449                 unsigned HOST_WIDE_INT ix;
10450
10451                 /* Even when ctor is constant, it might contain non-*_CST
10452                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10453                    belong into VECTOR_CST nodes.  */
10454                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10455                   if (!CONSTANT_CLASS_P (value))
10456                     {
10457                       constant_p = false;
10458                       break;
10459                     }
10460
10461                 if (constant_p)
10462                   {
10463                     init = build_vector_from_ctor (type, elts);
10464                     break;
10465                   }
10466               }
10467             /* FALLTHRU */
10468
10469           default:
10470             return NULL;
10471           }
10472
10473       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10474
10475       /* If expand_expr returns a MEM, it wasn't immediate.  */
10476       gcc_assert (!rtl || !MEM_P (rtl));
10477     }
10478
10479   return rtl;
10480 }
10481
10482 /* Generate RTL for the variable DECL to represent its location.  */
10483
10484 static rtx
10485 rtl_for_decl_location (tree decl)
10486 {
10487   rtx rtl;
10488
10489   /* Here we have to decide where we are going to say the parameter "lives"
10490      (as far as the debugger is concerned).  We only have a couple of
10491      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10492
10493      DECL_RTL normally indicates where the parameter lives during most of the
10494      activation of the function.  If optimization is enabled however, this
10495      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10496      that the parameter doesn't really live anywhere (as far as the code
10497      generation parts of GCC are concerned) during most of the function's
10498      activation.  That will happen (for example) if the parameter is never
10499      referenced within the function.
10500
10501      We could just generate a location descriptor here for all non-NULL
10502      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10503      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10504      where DECL_RTL is NULL or is a pseudo-reg.
10505
10506      Note however that we can only get away with using DECL_INCOMING_RTL as
10507      a backup substitute for DECL_RTL in certain limited cases.  In cases
10508      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10509      we can be sure that the parameter was passed using the same type as it is
10510      declared to have within the function, and that its DECL_INCOMING_RTL
10511      points us to a place where a value of that type is passed.
10512
10513      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10514      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10515      because in these cases DECL_INCOMING_RTL points us to a value of some
10516      type which is *different* from the type of the parameter itself.  Thus,
10517      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10518      such cases, the debugger would end up (for example) trying to fetch a
10519      `float' from a place which actually contains the first part of a
10520      `double'.  That would lead to really incorrect and confusing
10521      output at debug-time.
10522
10523      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10524      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10525      are a couple of exceptions however.  On little-endian machines we can
10526      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10527      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10528      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10529      when (on a little-endian machine) a non-prototyped function has a
10530      parameter declared to be of type `short' or `char'.  In such cases,
10531      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10532      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10533      passed `int' value.  If the debugger then uses that address to fetch
10534      a `short' or a `char' (on a little-endian machine) the result will be
10535      the correct data, so we allow for such exceptional cases below.
10536
10537      Note that our goal here is to describe the place where the given formal
10538      parameter lives during most of the function's activation (i.e. between the
10539      end of the prologue and the start of the epilogue).  We'll do that as best
10540      as we can. Note however that if the given formal parameter is modified
10541      sometime during the execution of the function, then a stack backtrace (at
10542      debug-time) will show the function as having been called with the *new*
10543      value rather than the value which was originally passed in.  This happens
10544      rarely enough that it is not a major problem, but it *is* a problem, and
10545      I'd like to fix it.
10546
10547      A future version of dwarf2out.c may generate two additional attributes for
10548      any given DW_TAG_formal_parameter DIE which will describe the "passed
10549      type" and the "passed location" for the given formal parameter in addition
10550      to the attributes we now generate to indicate the "declared type" and the
10551      "active location" for each parameter.  This additional set of attributes
10552      could be used by debuggers for stack backtraces. Separately, note that
10553      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10554      This happens (for example) for inlined-instances of inline function formal
10555      parameters which are never referenced.  This really shouldn't be
10556      happening.  All PARM_DECL nodes should get valid non-NULL
10557      DECL_INCOMING_RTL values.  FIXME.  */
10558
10559   /* Use DECL_RTL as the "location" unless we find something better.  */
10560   rtl = DECL_RTL_IF_SET (decl);
10561
10562   /* When generating abstract instances, ignore everything except
10563      constants, symbols living in memory, and symbols living in
10564      fixed registers.  */
10565   if (! reload_completed)
10566     {
10567       if (rtl
10568           && (CONSTANT_P (rtl)
10569               || (MEM_P (rtl)
10570                   && CONSTANT_P (XEXP (rtl, 0)))
10571               || (REG_P (rtl)
10572                   && TREE_CODE (decl) == VAR_DECL
10573                   && TREE_STATIC (decl))))
10574         {
10575           rtl = targetm.delegitimize_address (rtl);
10576           return rtl;
10577         }
10578       rtl = NULL_RTX;
10579     }
10580   else if (TREE_CODE (decl) == PARM_DECL)
10581     {
10582       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10583         {
10584           tree declared_type = TREE_TYPE (decl);
10585           tree passed_type = DECL_ARG_TYPE (decl);
10586           enum machine_mode dmode = TYPE_MODE (declared_type);
10587           enum machine_mode pmode = TYPE_MODE (passed_type);
10588
10589           /* This decl represents a formal parameter which was optimized out.
10590              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10591              all cases where (rtl == NULL_RTX) just below.  */
10592           if (dmode == pmode)
10593             rtl = DECL_INCOMING_RTL (decl);
10594           else if (SCALAR_INT_MODE_P (dmode)
10595                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10596                    && DECL_INCOMING_RTL (decl))
10597             {
10598               rtx inc = DECL_INCOMING_RTL (decl);
10599               if (REG_P (inc))
10600                 rtl = inc;
10601               else if (MEM_P (inc))
10602                 {
10603                   if (BYTES_BIG_ENDIAN)
10604                     rtl = adjust_address_nv (inc, dmode,
10605                                              GET_MODE_SIZE (pmode)
10606                                              - GET_MODE_SIZE (dmode));
10607                   else
10608                     rtl = inc;
10609                 }
10610             }
10611         }
10612
10613       /* If the parm was passed in registers, but lives on the stack, then
10614          make a big endian correction if the mode of the type of the
10615          parameter is not the same as the mode of the rtl.  */
10616       /* ??? This is the same series of checks that are made in dbxout.c before
10617          we reach the big endian correction code there.  It isn't clear if all
10618          of these checks are necessary here, but keeping them all is the safe
10619          thing to do.  */
10620       else if (MEM_P (rtl)
10621                && XEXP (rtl, 0) != const0_rtx
10622                && ! CONSTANT_P (XEXP (rtl, 0))
10623                /* Not passed in memory.  */
10624                && !MEM_P (DECL_INCOMING_RTL (decl))
10625                /* Not passed by invisible reference.  */
10626                && (!REG_P (XEXP (rtl, 0))
10627                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10628                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10629 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10630                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10631 #endif
10632                      )
10633                /* Big endian correction check.  */
10634                && BYTES_BIG_ENDIAN
10635                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10636                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10637                    < UNITS_PER_WORD))
10638         {
10639           int offset = (UNITS_PER_WORD
10640                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10641
10642           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10643                              plus_constant (XEXP (rtl, 0), offset));
10644         }
10645     }
10646   else if (TREE_CODE (decl) == VAR_DECL
10647            && rtl
10648            && MEM_P (rtl)
10649            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10650            && BYTES_BIG_ENDIAN)
10651     {
10652       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10653       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10654
10655       /* If a variable is declared "register" yet is smaller than
10656          a register, then if we store the variable to memory, it
10657          looks like we're storing a register-sized value, when in
10658          fact we are not.  We need to adjust the offset of the
10659          storage location to reflect the actual value's bytes,
10660          else gdb will not be able to display it.  */
10661       if (rsize > dsize)
10662         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10663                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10664     }
10665
10666   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10667      and will have been substituted directly into all expressions that use it.
10668      C does not have such a concept, but C++ and other languages do.  */
10669   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10670     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10671
10672   if (rtl)
10673     rtl = targetm.delegitimize_address (rtl);
10674
10675   /* If we don't look past the constant pool, we risk emitting a
10676      reference to a constant pool entry that isn't referenced from
10677      code, and thus is not emitted.  */
10678   if (rtl)
10679     rtl = avoid_constant_pool_reference (rtl);
10680
10681   return rtl;
10682 }
10683
10684 /* We need to figure out what section we should use as the base for the
10685    address ranges where a given location is valid.
10686    1. If this particular DECL has a section associated with it, use that.
10687    2. If this function has a section associated with it, use that.
10688    3. Otherwise, use the text section.
10689    XXX: If you split a variable across multiple sections, we won't notice.  */
10690
10691 static const char *
10692 secname_for_decl (const_tree decl)
10693 {
10694   const char *secname;
10695
10696   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10697     {
10698       tree sectree = DECL_SECTION_NAME (decl);
10699       secname = TREE_STRING_POINTER (sectree);
10700     }
10701   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10702     {
10703       tree sectree = DECL_SECTION_NAME (current_function_decl);
10704       secname = TREE_STRING_POINTER (sectree);
10705     }
10706   else if (cfun && in_cold_section_p)
10707     secname = cfun->cold_section_label;
10708   else
10709     secname = text_section_label;
10710
10711   return secname;
10712 }
10713
10714 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10715    data attribute for a variable or a parameter.  We generate the
10716    DW_AT_const_value attribute only in those cases where the given variable
10717    or parameter does not have a true "location" either in memory or in a
10718    register.  This can happen (for example) when a constant is passed as an
10719    actual argument in a call to an inline function.  (It's possible that
10720    these things can crop up in other ways also.)  Note that one type of
10721    constant value which can be passed into an inlined function is a constant
10722    pointer.  This can happen for example if an actual argument in an inlined
10723    function call evaluates to a compile-time constant address.  */
10724
10725 static void
10726 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10727                                        enum dwarf_attribute attr)
10728 {
10729   rtx rtl;
10730   dw_loc_descr_ref descr;
10731   var_loc_list *loc_list;
10732   struct var_loc_node *node;
10733   if (TREE_CODE (decl) == ERROR_MARK)
10734     return;
10735
10736   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10737               || TREE_CODE (decl) == RESULT_DECL);
10738
10739   /* See if we possibly have multiple locations for this variable.  */
10740   loc_list = lookup_decl_loc (decl);
10741
10742   /* If it truly has multiple locations, the first and last node will
10743      differ.  */
10744   if (loc_list && loc_list->first != loc_list->last)
10745     {
10746       const char *endname, *secname;
10747       dw_loc_list_ref list;
10748       rtx varloc;
10749       enum var_init_status initialized;
10750
10751       /* Now that we know what section we are using for a base,
10752          actually construct the list of locations.
10753          The first location information is what is passed to the
10754          function that creates the location list, and the remaining
10755          locations just get added on to that list.
10756          Note that we only know the start address for a location
10757          (IE location changes), so to build the range, we use
10758          the range [current location start, next location start].
10759          This means we have to special case the last node, and generate
10760          a range of [last location start, end of function label].  */
10761
10762       node = loc_list->first;
10763       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10764       secname = secname_for_decl (decl);
10765
10766       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10767         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10768       else
10769         initialized = VAR_INIT_STATUS_INITIALIZED;
10770
10771       list = new_loc_list (loc_descriptor (varloc, initialized),
10772                            node->label, node->next->label, secname, 1);
10773       node = node->next;
10774
10775       for (; node->next; node = node->next)
10776         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10777           {
10778             /* The variable has a location between NODE->LABEL and
10779                NODE->NEXT->LABEL.  */
10780             enum var_init_status initialized =
10781               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10782             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10783             add_loc_descr_to_loc_list (&list, 
10784                                        loc_descriptor (varloc, initialized),
10785                                        node->label, node->next->label, secname);
10786           }
10787
10788       /* If the variable has a location at the last label
10789          it keeps its location until the end of function.  */
10790       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10791         {
10792           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10793           enum var_init_status initialized =
10794             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10795
10796           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10797           if (!current_function_decl)
10798             endname = text_end_label;
10799           else
10800             {
10801               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10802                                            current_function_funcdef_no);
10803               endname = ggc_strdup (label_id);
10804             }
10805           add_loc_descr_to_loc_list (&list, 
10806                                      loc_descriptor (varloc, initialized),
10807                                      node->label, endname, secname);
10808         }
10809
10810       /* Finally, add the location list to the DIE, and we are done.  */
10811       add_AT_loc_list (die, attr, list);
10812       return;
10813     }
10814
10815   /* Try to get some constant RTL for this decl, and use that as the value of
10816      the location.  */
10817
10818   rtl = rtl_for_decl_location (decl);
10819   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10820     {
10821       add_const_value_attribute (die, rtl);
10822       return;
10823     }
10824
10825   /* If we have tried to generate the location otherwise, and it
10826      didn't work out (we wouldn't be here if we did), and we have a one entry
10827      location list, try generating a location from that.  */
10828   if (loc_list && loc_list->first)
10829     {
10830       enum var_init_status status;
10831       node = loc_list->first;
10832       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10833       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10834       if (descr)
10835         {
10836           add_AT_location_description (die, attr, descr);
10837           return;
10838         }
10839     }
10840
10841   /* We couldn't get any rtl, so try directly generating the location
10842      description from the tree.  */
10843   descr = loc_descriptor_from_tree (decl);
10844   if (descr)
10845     {
10846       add_AT_location_description (die, attr, descr);
10847       return;
10848     }
10849   /* None of that worked, so it must not really have a location;
10850      try adding a constant value attribute from the DECL_INITIAL.  */
10851   tree_add_const_value_attribute (die, decl);
10852 }
10853
10854 /* If we don't have a copy of this variable in memory for some reason (such
10855    as a C++ member constant that doesn't have an out-of-line definition),
10856    we should tell the debugger about the constant value.  */
10857
10858 static void
10859 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10860 {
10861   tree init = DECL_INITIAL (decl);
10862   tree type = TREE_TYPE (decl);
10863   rtx rtl;
10864
10865   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10866     /* OK */;
10867   else
10868     return;
10869
10870   rtl = rtl_for_decl_init (init, type);
10871   if (rtl)
10872     add_const_value_attribute (var_die, rtl);
10873 }
10874
10875 /* Convert the CFI instructions for the current function into a
10876    location list.  This is used for DW_AT_frame_base when we targeting
10877    a dwarf2 consumer that does not support the dwarf3
10878    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10879    expressions.  */
10880
10881 static dw_loc_list_ref
10882 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10883 {
10884   dw_fde_ref fde;
10885   dw_loc_list_ref list, *list_tail;
10886   dw_cfi_ref cfi;
10887   dw_cfa_location last_cfa, next_cfa;
10888   const char *start_label, *last_label, *section;
10889
10890   fde = &fde_table[fde_table_in_use - 1];
10891
10892   section = secname_for_decl (current_function_decl);
10893   list_tail = &list;
10894   list = NULL;
10895
10896   next_cfa.reg = INVALID_REGNUM;
10897   next_cfa.offset = 0;
10898   next_cfa.indirect = 0;
10899   next_cfa.base_offset = 0;
10900
10901   start_label = fde->dw_fde_begin;
10902
10903   /* ??? Bald assumption that the CIE opcode list does not contain
10904      advance opcodes.  */
10905   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10906     lookup_cfa_1 (cfi, &next_cfa);
10907
10908   last_cfa = next_cfa;
10909   last_label = start_label;
10910
10911   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10912     switch (cfi->dw_cfi_opc)
10913       {
10914       case DW_CFA_set_loc:
10915       case DW_CFA_advance_loc1:
10916       case DW_CFA_advance_loc2:
10917       case DW_CFA_advance_loc4:
10918         if (!cfa_equal_p (&last_cfa, &next_cfa))
10919           {
10920             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10921                                        start_label, last_label, section,
10922                                        list == NULL);
10923
10924             list_tail = &(*list_tail)->dw_loc_next;
10925             last_cfa = next_cfa;
10926             start_label = last_label;
10927           }
10928         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10929         break;
10930
10931       case DW_CFA_advance_loc:
10932         /* The encoding is complex enough that we should never emit this.  */
10933       case DW_CFA_remember_state:
10934       case DW_CFA_restore_state:
10935         /* We don't handle these two in this function.  It would be possible
10936            if it were to be required.  */
10937         gcc_unreachable ();
10938
10939       default:
10940         lookup_cfa_1 (cfi, &next_cfa);
10941         break;
10942       }
10943
10944   if (!cfa_equal_p (&last_cfa, &next_cfa))
10945     {
10946       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10947                                  start_label, last_label, section,
10948                                  list == NULL);
10949       list_tail = &(*list_tail)->dw_loc_next;
10950       start_label = last_label;
10951     }
10952   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10953                              start_label, fde->dw_fde_end, section,
10954                              list == NULL);
10955
10956   return list;
10957 }
10958
10959 /* Compute a displacement from the "steady-state frame pointer" to the
10960    frame base (often the same as the CFA), and store it in
10961    frame_pointer_fb_offset.  OFFSET is added to the displacement
10962    before the latter is negated.  */
10963
10964 static void
10965 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10966 {
10967   rtx reg, elim;
10968
10969 #ifdef FRAME_POINTER_CFA_OFFSET
10970   reg = frame_pointer_rtx;
10971   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10972 #else
10973   reg = arg_pointer_rtx;
10974   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10975 #endif
10976
10977   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10978   if (GET_CODE (elim) == PLUS)
10979     {
10980       offset += INTVAL (XEXP (elim, 1));
10981       elim = XEXP (elim, 0);
10982     }
10983   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10984                        : stack_pointer_rtx));
10985
10986   frame_pointer_fb_offset = -offset;
10987 }
10988
10989 /* Generate a DW_AT_name attribute given some string value to be included as
10990    the value of the attribute.  */
10991
10992 static void
10993 add_name_attribute (dw_die_ref die, const char *name_string)
10994 {
10995   if (name_string != NULL && *name_string != 0)
10996     {
10997       if (demangle_name_func)
10998         name_string = (*demangle_name_func) (name_string);
10999
11000       add_AT_string (die, DW_AT_name, name_string);
11001     }
11002 }
11003
11004 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11005
11006 static void
11007 add_comp_dir_attribute (dw_die_ref die)
11008 {
11009   const char *wd = get_src_pwd ();
11010   if (wd != NULL)
11011     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11012 }
11013
11014 /* Given a tree node describing an array bound (either lower or upper) output
11015    a representation for that bound.  */
11016
11017 static void
11018 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11019 {
11020   switch (TREE_CODE (bound))
11021     {
11022     case ERROR_MARK:
11023       return;
11024
11025     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11026     case INTEGER_CST:
11027       if (! host_integerp (bound, 0)
11028           || (bound_attr == DW_AT_lower_bound
11029               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11030                   || (is_fortran () && integer_onep (bound)))))
11031         /* Use the default.  */
11032         ;
11033       else
11034         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11035       break;
11036
11037     case CONVERT_EXPR:
11038     case NOP_EXPR:
11039     case NON_LVALUE_EXPR:
11040     case VIEW_CONVERT_EXPR:
11041       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11042       break;
11043
11044     case SAVE_EXPR:
11045       break;
11046
11047     case VAR_DECL:
11048     case PARM_DECL:
11049     case RESULT_DECL:
11050       {
11051         dw_die_ref decl_die = lookup_decl_die (bound);
11052
11053         /* ??? Can this happen, or should the variable have been bound
11054            first?  Probably it can, since I imagine that we try to create
11055            the types of parameters in the order in which they exist in
11056            the list, and won't have created a forward reference to a
11057            later parameter.  */
11058         if (decl_die != NULL)
11059           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11060         break;
11061       }
11062
11063     default:
11064       {
11065         /* Otherwise try to create a stack operation procedure to
11066            evaluate the value of the array bound.  */
11067
11068         dw_die_ref ctx, decl_die;
11069         dw_loc_descr_ref loc;
11070
11071         loc = loc_descriptor_from_tree (bound);
11072         if (loc == NULL)
11073           break;
11074
11075         if (current_function_decl == 0)
11076           ctx = comp_unit_die;
11077         else
11078           ctx = lookup_decl_die (current_function_decl);
11079
11080         decl_die = new_die (DW_TAG_variable, ctx, bound);
11081         add_AT_flag (decl_die, DW_AT_artificial, 1);
11082         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11083         add_AT_loc (decl_die, DW_AT_location, loc);
11084
11085         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11086         break;
11087       }
11088     }
11089 }
11090
11091 /* Note that the block of subscript information for an array type also
11092    includes information about the element type of type given array type.  */
11093
11094 static void
11095 add_subscript_info (dw_die_ref type_die, tree type)
11096 {
11097 #ifndef MIPS_DEBUGGING_INFO
11098   unsigned dimension_number;
11099 #endif
11100   tree lower, upper;
11101   dw_die_ref subrange_die;
11102
11103   /* The GNU compilers represent multidimensional array types as sequences of
11104      one dimensional array types whose element types are themselves array
11105      types.  Here we squish that down, so that each multidimensional array
11106      type gets only one array_type DIE in the Dwarf debugging info. The draft
11107      Dwarf specification say that we are allowed to do this kind of
11108      compression in C (because there is no difference between an array or
11109      arrays and a multidimensional array in C) but for other source languages
11110      (e.g. Ada) we probably shouldn't do this.  */
11111
11112   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11113      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11114      We work around this by disabling this feature.  See also
11115      gen_array_type_die.  */
11116 #ifndef MIPS_DEBUGGING_INFO
11117   for (dimension_number = 0;
11118        TREE_CODE (type) == ARRAY_TYPE;
11119        type = TREE_TYPE (type), dimension_number++)
11120 #endif
11121     {
11122       tree domain = TYPE_DOMAIN (type);
11123
11124       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11125          and (in GNU C only) variable bounds.  Handle all three forms
11126          here.  */
11127       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11128       if (domain)
11129         {
11130           /* We have an array type with specified bounds.  */
11131           lower = TYPE_MIN_VALUE (domain);
11132           upper = TYPE_MAX_VALUE (domain);
11133
11134           /* Define the index type.  */
11135           if (TREE_TYPE (domain))
11136             {
11137               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11138                  TREE_TYPE field.  We can't emit debug info for this
11139                  because it is an unnamed integral type.  */
11140               if (TREE_CODE (domain) == INTEGER_TYPE
11141                   && TYPE_NAME (domain) == NULL_TREE
11142                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11143                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11144                 ;
11145               else
11146                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11147                                     type_die);
11148             }
11149
11150           /* ??? If upper is NULL, the array has unspecified length,
11151              but it does have a lower bound.  This happens with Fortran
11152                dimension arr(N:*)
11153              Since the debugger is definitely going to need to know N
11154              to produce useful results, go ahead and output the lower
11155              bound solo, and hope the debugger can cope.  */
11156
11157           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11158           if (upper)
11159             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11160         }
11161
11162       /* Otherwise we have an array type with an unspecified length.  The
11163          DWARF-2 spec does not say how to handle this; let's just leave out the
11164          bounds.  */
11165     }
11166 }
11167
11168 static void
11169 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11170 {
11171   unsigned size;
11172
11173   switch (TREE_CODE (tree_node))
11174     {
11175     case ERROR_MARK:
11176       size = 0;
11177       break;
11178     case ENUMERAL_TYPE:
11179     case RECORD_TYPE:
11180     case UNION_TYPE:
11181     case QUAL_UNION_TYPE:
11182       size = int_size_in_bytes (tree_node);
11183       break;
11184     case FIELD_DECL:
11185       /* For a data member of a struct or union, the DW_AT_byte_size is
11186          generally given as the number of bytes normally allocated for an
11187          object of the *declared* type of the member itself.  This is true
11188          even for bit-fields.  */
11189       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11190       break;
11191     default:
11192       gcc_unreachable ();
11193     }
11194
11195   /* Note that `size' might be -1 when we get to this point.  If it is, that
11196      indicates that the byte size of the entity in question is variable.  We
11197      have no good way of expressing this fact in Dwarf at the present time,
11198      so just let the -1 pass on through.  */
11199   add_AT_unsigned (die, DW_AT_byte_size, size);
11200 }
11201
11202 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11203    which specifies the distance in bits from the highest order bit of the
11204    "containing object" for the bit-field to the highest order bit of the
11205    bit-field itself.
11206
11207    For any given bit-field, the "containing object" is a hypothetical object
11208    (of some integral or enum type) within which the given bit-field lives.  The
11209    type of this hypothetical "containing object" is always the same as the
11210    declared type of the individual bit-field itself.  The determination of the
11211    exact location of the "containing object" for a bit-field is rather
11212    complicated.  It's handled by the `field_byte_offset' function (above).
11213
11214    Note that it is the size (in bytes) of the hypothetical "containing object"
11215    which will be given in the DW_AT_byte_size attribute for this bit-field.
11216    (See `byte_size_attribute' above).  */
11217
11218 static inline void
11219 add_bit_offset_attribute (dw_die_ref die, tree decl)
11220 {
11221   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11222   tree type = DECL_BIT_FIELD_TYPE (decl);
11223   HOST_WIDE_INT bitpos_int;
11224   HOST_WIDE_INT highest_order_object_bit_offset;
11225   HOST_WIDE_INT highest_order_field_bit_offset;
11226   HOST_WIDE_INT unsigned bit_offset;
11227
11228   /* Must be a field and a bit field.  */
11229   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11230
11231   /* We can't yet handle bit-fields whose offsets are variable, so if we
11232      encounter such things, just return without generating any attribute
11233      whatsoever.  Likewise for variable or too large size.  */
11234   if (! host_integerp (bit_position (decl), 0)
11235       || ! host_integerp (DECL_SIZE (decl), 1))
11236     return;
11237
11238   bitpos_int = int_bit_position (decl);
11239
11240   /* Note that the bit offset is always the distance (in bits) from the
11241      highest-order bit of the "containing object" to the highest-order bit of
11242      the bit-field itself.  Since the "high-order end" of any object or field
11243      is different on big-endian and little-endian machines, the computation
11244      below must take account of these differences.  */
11245   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11246   highest_order_field_bit_offset = bitpos_int;
11247
11248   if (! BYTES_BIG_ENDIAN)
11249     {
11250       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11251       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11252     }
11253
11254   bit_offset
11255     = (! BYTES_BIG_ENDIAN
11256        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11257        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11258
11259   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11260 }
11261
11262 /* For a FIELD_DECL node which represents a bit field, output an attribute
11263    which specifies the length in bits of the given field.  */
11264
11265 static inline void
11266 add_bit_size_attribute (dw_die_ref die, tree decl)
11267 {
11268   /* Must be a field and a bit field.  */
11269   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11270               && DECL_BIT_FIELD_TYPE (decl));
11271
11272   if (host_integerp (DECL_SIZE (decl), 1))
11273     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11274 }
11275
11276 /* If the compiled language is ANSI C, then add a 'prototyped'
11277    attribute, if arg types are given for the parameters of a function.  */
11278
11279 static inline void
11280 add_prototyped_attribute (dw_die_ref die, tree func_type)
11281 {
11282   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11283       && TYPE_ARG_TYPES (func_type) != NULL)
11284     add_AT_flag (die, DW_AT_prototyped, 1);
11285 }
11286
11287 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11288    by looking in either the type declaration or object declaration
11289    equate table.  */
11290
11291 static inline void
11292 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11293 {
11294   dw_die_ref origin_die = NULL;
11295
11296   if (TREE_CODE (origin) != FUNCTION_DECL)
11297     {
11298       /* We may have gotten separated from the block for the inlined
11299          function, if we're in an exception handler or some such; make
11300          sure that the abstract function has been written out.
11301
11302          Doing this for nested functions is wrong, however; functions are
11303          distinct units, and our context might not even be inline.  */
11304       tree fn = origin;
11305
11306       if (TYPE_P (fn))
11307         fn = TYPE_STUB_DECL (fn);
11308
11309       fn = decl_function_context (fn);
11310       if (fn)
11311         dwarf2out_abstract_function (fn);
11312     }
11313
11314   if (DECL_P (origin))
11315     origin_die = lookup_decl_die (origin);
11316   else if (TYPE_P (origin))
11317     origin_die = lookup_type_die (origin);
11318
11319   /* XXX: Functions that are never lowered don't always have correct block
11320      trees (in the case of java, they simply have no block tree, in some other
11321      languages).  For these functions, there is nothing we can really do to
11322      output correct debug info for inlined functions in all cases.  Rather
11323      than die, we'll just produce deficient debug info now, in that we will
11324      have variables without a proper abstract origin.  In the future, when all
11325      functions are lowered, we should re-add a gcc_assert (origin_die)
11326      here.  */
11327
11328   if (origin_die)
11329       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11330 }
11331
11332 /* We do not currently support the pure_virtual attribute.  */
11333
11334 static inline void
11335 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11336 {
11337   if (DECL_VINDEX (func_decl))
11338     {
11339       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11340
11341       if (host_integerp (DECL_VINDEX (func_decl), 0))
11342         add_AT_loc (die, DW_AT_vtable_elem_location,
11343                     new_loc_descr (DW_OP_constu,
11344                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11345                                    0));
11346
11347       /* GNU extension: Record what type this method came from originally.  */
11348       if (debug_info_level > DINFO_LEVEL_TERSE)
11349         add_AT_die_ref (die, DW_AT_containing_type,
11350                         lookup_type_die (DECL_CONTEXT (func_decl)));
11351     }
11352 }
11353 \f
11354 /* Add source coordinate attributes for the given decl.  */
11355
11356 static void
11357 add_src_coords_attributes (dw_die_ref die, tree decl)
11358 {
11359   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11360
11361   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11362   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11363 }
11364
11365 /* Add a DW_AT_name attribute and source coordinate attribute for the
11366    given decl, but only if it actually has a name.  */
11367
11368 static void
11369 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11370 {
11371   tree decl_name;
11372
11373   decl_name = DECL_NAME (decl);
11374   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11375     {
11376       add_name_attribute (die, dwarf2_name (decl, 0));
11377       if (! DECL_ARTIFICIAL (decl))
11378         add_src_coords_attributes (die, decl);
11379
11380       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11381           && TREE_PUBLIC (decl)
11382           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11383           && !DECL_ABSTRACT (decl)
11384           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11385           && !is_fortran ())
11386         add_AT_string (die, DW_AT_MIPS_linkage_name,
11387                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11388     }
11389
11390 #ifdef VMS_DEBUGGING_INFO
11391   /* Get the function's name, as described by its RTL.  This may be different
11392      from the DECL_NAME name used in the source file.  */
11393   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11394     {
11395       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11396                    XEXP (DECL_RTL (decl), 0));
11397       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11398     }
11399 #endif
11400 }
11401
11402 /* Push a new declaration scope.  */
11403
11404 static void
11405 push_decl_scope (tree scope)
11406 {
11407   VEC_safe_push (tree, gc, decl_scope_table, scope);
11408 }
11409
11410 /* Pop a declaration scope.  */
11411
11412 static inline void
11413 pop_decl_scope (void)
11414 {
11415   VEC_pop (tree, decl_scope_table);
11416 }
11417
11418 /* Return the DIE for the scope that immediately contains this type.
11419    Non-named types get global scope.  Named types nested in other
11420    types get their containing scope if it's open, or global scope
11421    otherwise.  All other types (i.e. function-local named types) get
11422    the current active scope.  */
11423
11424 static dw_die_ref
11425 scope_die_for (tree t, dw_die_ref context_die)
11426 {
11427   dw_die_ref scope_die = NULL;
11428   tree containing_scope;
11429   int i;
11430
11431   /* Non-types always go in the current scope.  */
11432   gcc_assert (TYPE_P (t));
11433
11434   containing_scope = TYPE_CONTEXT (t);
11435
11436   /* Use the containing namespace if it was passed in (for a declaration).  */
11437   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11438     {
11439       if (context_die == lookup_decl_die (containing_scope))
11440         /* OK */;
11441       else
11442         containing_scope = NULL_TREE;
11443     }
11444
11445   /* Ignore function type "scopes" from the C frontend.  They mean that
11446      a tagged type is local to a parmlist of a function declarator, but
11447      that isn't useful to DWARF.  */
11448   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11449     containing_scope = NULL_TREE;
11450
11451   if (containing_scope == NULL_TREE)
11452     scope_die = comp_unit_die;
11453   else if (TYPE_P (containing_scope))
11454     {
11455       /* For types, we can just look up the appropriate DIE.  But
11456          first we check to see if we're in the middle of emitting it
11457          so we know where the new DIE should go.  */
11458       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11459         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11460           break;
11461
11462       if (i < 0)
11463         {
11464           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11465                       || TREE_ASM_WRITTEN (containing_scope));
11466
11467           /* If none of the current dies are suitable, we get file scope.  */
11468           scope_die = comp_unit_die;
11469         }
11470       else
11471         scope_die = lookup_type_die (containing_scope);
11472     }
11473   else
11474     scope_die = context_die;
11475
11476   return scope_die;
11477 }
11478
11479 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11480
11481 static inline int
11482 local_scope_p (dw_die_ref context_die)
11483 {
11484   for (; context_die; context_die = context_die->die_parent)
11485     if (context_die->die_tag == DW_TAG_inlined_subroutine
11486         || context_die->die_tag == DW_TAG_subprogram)
11487       return 1;
11488
11489   return 0;
11490 }
11491
11492 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11493    whether or not to treat a DIE in this context as a declaration.  */
11494
11495 static inline int
11496 class_or_namespace_scope_p (dw_die_ref context_die)
11497 {
11498   return (context_die
11499           && (context_die->die_tag == DW_TAG_structure_type
11500               || context_die->die_tag == DW_TAG_class_type
11501               || context_die->die_tag == DW_TAG_interface_type
11502               || context_die->die_tag == DW_TAG_union_type
11503               || context_die->die_tag == DW_TAG_namespace));
11504 }
11505
11506 /* Many forms of DIEs require a "type description" attribute.  This
11507    routine locates the proper "type descriptor" die for the type given
11508    by 'type', and adds a DW_AT_type attribute below the given die.  */
11509
11510 static void
11511 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11512                     int decl_volatile, dw_die_ref context_die)
11513 {
11514   enum tree_code code  = TREE_CODE (type);
11515   dw_die_ref type_die  = NULL;
11516
11517   /* ??? If this type is an unnamed subrange type of an integral, floating-point
11518      or fixed-point type, use the inner type.  This is because we have no
11519      support for unnamed types in base_type_die.  This can happen if this is
11520      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11521   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11522       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11523     type = TREE_TYPE (type), code = TREE_CODE (type);
11524
11525   if (code == ERROR_MARK
11526       /* Handle a special case.  For functions whose return type is void, we
11527          generate *no* type attribute.  (Note that no object may have type
11528          `void', so this only applies to function return types).  */
11529       || code == VOID_TYPE)
11530     return;
11531
11532   type_die = modified_type_die (type,
11533                                 decl_const || TYPE_READONLY (type),
11534                                 decl_volatile || TYPE_VOLATILE (type),
11535                                 context_die);
11536
11537   if (type_die != NULL)
11538     add_AT_die_ref (object_die, DW_AT_type, type_die);
11539 }
11540
11541 /* Given an object die, add the calling convention attribute for the
11542    function call type.  */
11543 static void
11544 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11545 {
11546   enum dwarf_calling_convention value = DW_CC_normal;
11547
11548   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11549
11550   /* DWARF doesn't provide a way to identify a program's source-level
11551      entry point.  DW_AT_calling_convention attributes are only meant
11552      to describe functions' calling conventions.  However, lacking a
11553      better way to signal the Fortran main program, we use this for the
11554      time being, following existing custom.  */
11555   if (is_fortran ()
11556       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11557     value = DW_CC_program;
11558
11559   /* Only add the attribute if the backend requests it, and
11560      is not DW_CC_normal.  */
11561   if (value && (value != DW_CC_normal))
11562     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11563 }
11564
11565 /* Given a tree pointer to a struct, class, union, or enum type node, return
11566    a pointer to the (string) tag name for the given type, or zero if the type
11567    was declared without a tag.  */
11568
11569 static const char *
11570 type_tag (const_tree type)
11571 {
11572   const char *name = 0;
11573
11574   if (TYPE_NAME (type) != 0)
11575     {
11576       tree t = 0;
11577
11578       /* Find the IDENTIFIER_NODE for the type name.  */
11579       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11580         t = TYPE_NAME (type);
11581
11582       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11583          a TYPE_DECL node, regardless of whether or not a `typedef' was
11584          involved.  */
11585       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11586                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11587         {
11588           /* We want to be extra verbose.  Don't call dwarf_name if
11589              DECL_NAME isn't set.  The default hook for decl_printable_name
11590              doesn't like that, and in this context it's correct to return
11591              0, instead of "<anonymous>" or the like.  */
11592           if (DECL_NAME (TYPE_NAME (type)))
11593             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11594         }
11595
11596       /* Now get the name as a string, or invent one.  */
11597       if (!name && t != 0)
11598         name = IDENTIFIER_POINTER (t);
11599     }
11600
11601   return (name == 0 || *name == '\0') ? 0 : name;
11602 }
11603
11604 /* Return the type associated with a data member, make a special check
11605    for bit field types.  */
11606
11607 static inline tree
11608 member_declared_type (const_tree member)
11609 {
11610   return (DECL_BIT_FIELD_TYPE (member)
11611           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11612 }
11613
11614 /* Get the decl's label, as described by its RTL. This may be different
11615    from the DECL_NAME name used in the source file.  */
11616
11617 #if 0
11618 static const char *
11619 decl_start_label (tree decl)
11620 {
11621   rtx x;
11622   const char *fnname;
11623
11624   x = DECL_RTL (decl);
11625   gcc_assert (MEM_P (x));
11626
11627   x = XEXP (x, 0);
11628   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11629
11630   fnname = XSTR (x, 0);
11631   return fnname;
11632 }
11633 #endif
11634 \f
11635 /* These routines generate the internal representation of the DIE's for
11636    the compilation unit.  Debugging information is collected by walking
11637    the declaration trees passed in from dwarf2out_decl().  */
11638
11639 static void
11640 gen_array_type_die (tree type, dw_die_ref context_die)
11641 {
11642   dw_die_ref scope_die = scope_die_for (type, context_die);
11643   dw_die_ref array_die;
11644   tree element_type;
11645
11646   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11647      the inner array type comes before the outer array type.  Thus we must
11648      call gen_type_die before we call new_die.  See below also.  */
11649 #ifdef MIPS_DEBUGGING_INFO
11650   gen_type_die (TREE_TYPE (type), context_die);
11651 #endif
11652
11653   array_die = new_die (DW_TAG_array_type, scope_die, type);
11654   add_name_attribute (array_die, type_tag (type));
11655   equate_type_number_to_die (type, array_die);
11656
11657   if (TREE_CODE (type) == VECTOR_TYPE)
11658     {
11659       /* The frontend feeds us a representation for the vector as a struct
11660          containing an array.  Pull out the array type.  */
11661       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11662       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11663     }
11664
11665   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11666   if (is_fortran ()
11667       && TREE_CODE (type) == ARRAY_TYPE
11668       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11669     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11670
11671 #if 0
11672   /* We default the array ordering.  SDB will probably do
11673      the right things even if DW_AT_ordering is not present.  It's not even
11674      an issue until we start to get into multidimensional arrays anyway.  If
11675      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11676      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11677      and when we find out that we need to put these in, we will only do so
11678      for multidimensional arrays.  */
11679   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11680 #endif
11681
11682 #ifdef MIPS_DEBUGGING_INFO
11683   /* The SGI compilers handle arrays of unknown bound by setting
11684      AT_declaration and not emitting any subrange DIEs.  */
11685   if (! TYPE_DOMAIN (type))
11686     add_AT_flag (array_die, DW_AT_declaration, 1);
11687   else
11688 #endif
11689     add_subscript_info (array_die, type);
11690
11691   /* Add representation of the type of the elements of this array type.  */
11692   element_type = TREE_TYPE (type);
11693
11694   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11695      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11696      We work around this by disabling this feature.  See also
11697      add_subscript_info.  */
11698 #ifndef MIPS_DEBUGGING_INFO
11699   while (TREE_CODE (element_type) == ARRAY_TYPE)
11700     element_type = TREE_TYPE (element_type);
11701
11702   gen_type_die (element_type, context_die);
11703 #endif
11704
11705   add_type_attribute (array_die, element_type, 0, 0, context_die);
11706
11707   if (get_AT (array_die, DW_AT_name))
11708     add_pubtype (type, array_die);
11709 }
11710
11711 static dw_loc_descr_ref
11712 descr_info_loc (tree val, tree base_decl)
11713 {
11714   HOST_WIDE_INT size;
11715   dw_loc_descr_ref loc, loc2;
11716   enum dwarf_location_atom op;
11717
11718   if (val == base_decl)
11719     return new_loc_descr (DW_OP_push_object_address, 0, 0);
11720
11721   switch (TREE_CODE (val))
11722     {
11723     case NOP_EXPR:
11724     case CONVERT_EXPR:
11725       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11726     case INTEGER_CST:
11727       if (host_integerp (val, 0))
11728         return int_loc_descriptor (tree_low_cst (val, 0));
11729       break;
11730     case INDIRECT_REF:
11731       size = int_size_in_bytes (TREE_TYPE (val));
11732       if (size < 0)
11733         break;
11734       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11735       if (!loc)
11736         break;
11737       if (size == DWARF2_ADDR_SIZE)
11738         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11739       else
11740         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11741       return loc;
11742     case POINTER_PLUS_EXPR:
11743     case PLUS_EXPR:
11744       if (host_integerp (TREE_OPERAND (val, 1), 1)
11745           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11746              < 16384)
11747         {
11748           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11749           if (!loc)
11750             break;
11751           add_loc_descr (&loc,
11752                          new_loc_descr (DW_OP_plus_uconst,
11753                                         tree_low_cst (TREE_OPERAND (val, 1),
11754                                                       1), 0));
11755         }
11756       else
11757         {
11758           op = DW_OP_plus;
11759         do_binop:
11760           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11761           if (!loc)
11762             break;
11763           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11764           if (!loc2)
11765             break;
11766           add_loc_descr (&loc, loc2);
11767           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11768         }
11769       return loc;
11770     case MINUS_EXPR:
11771       op = DW_OP_minus;
11772       goto do_binop;
11773     case MULT_EXPR:
11774       op = DW_OP_mul;
11775       goto do_binop;
11776     case EQ_EXPR:
11777       op = DW_OP_eq;
11778       goto do_binop;
11779     case NE_EXPR:
11780       op = DW_OP_ne;
11781       goto do_binop;
11782     default:
11783       break;
11784     }
11785   return NULL;
11786 }
11787
11788 static void
11789 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11790                       tree val, tree base_decl)
11791 {
11792   dw_loc_descr_ref loc;
11793
11794   if (host_integerp (val, 0))
11795     {
11796       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11797       return;
11798     }
11799
11800   loc = descr_info_loc (val, base_decl);
11801   if (!loc)
11802     return;
11803
11804   add_AT_loc (die, attr, loc);
11805 }
11806
11807 /* This routine generates DIE for array with hidden descriptor, details
11808    are filled into *info by a langhook.  */
11809
11810 static void
11811 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11812                           dw_die_ref context_die)
11813 {
11814   dw_die_ref scope_die = scope_die_for (type, context_die);
11815   dw_die_ref array_die;
11816   int dim;
11817
11818   array_die = new_die (DW_TAG_array_type, scope_die, type);
11819   add_name_attribute (array_die, type_tag (type));
11820   equate_type_number_to_die (type, array_die);
11821
11822   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11823   if (is_fortran ()
11824       && info->ndimensions >= 2)
11825     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11826
11827   if (info->data_location)
11828     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11829                           info->base_decl);
11830   if (info->associated)
11831     add_descr_info_field (array_die, DW_AT_associated, info->associated,
11832                           info->base_decl);
11833   if (info->allocated)
11834     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11835                           info->base_decl);
11836
11837   for (dim = 0; dim < info->ndimensions; dim++)
11838     {
11839       dw_die_ref subrange_die
11840         = new_die (DW_TAG_subrange_type, array_die, NULL);
11841
11842       if (info->dimen[dim].lower_bound)
11843         {
11844           /* If it is the default value, omit it.  */
11845           if ((is_c_family () || is_java ())
11846               && integer_zerop (info->dimen[dim].lower_bound))
11847             ;
11848           else if (is_fortran ()
11849                    && integer_onep (info->dimen[dim].lower_bound))
11850             ;
11851           else
11852             add_descr_info_field (subrange_die, DW_AT_lower_bound,
11853                                   info->dimen[dim].lower_bound,
11854                                   info->base_decl);
11855         }
11856       if (info->dimen[dim].upper_bound)
11857         add_descr_info_field (subrange_die, DW_AT_upper_bound,
11858                               info->dimen[dim].upper_bound,
11859                               info->base_decl);
11860       if (info->dimen[dim].stride)
11861         add_descr_info_field (subrange_die, DW_AT_byte_stride,
11862                               info->dimen[dim].stride,
11863                               info->base_decl);
11864     }
11865
11866   gen_type_die (info->element_type, context_die);
11867   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11868
11869   if (get_AT (array_die, DW_AT_name))
11870     add_pubtype (type, array_die);
11871 }
11872
11873 #if 0
11874 static void
11875 gen_entry_point_die (tree decl, dw_die_ref context_die)
11876 {
11877   tree origin = decl_ultimate_origin (decl);
11878   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11879
11880   if (origin != NULL)
11881     add_abstract_origin_attribute (decl_die, origin);
11882   else
11883     {
11884       add_name_and_src_coords_attributes (decl_die, decl);
11885       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11886                           0, 0, context_die);
11887     }
11888
11889   if (DECL_ABSTRACT (decl))
11890     equate_decl_number_to_die (decl, decl_die);
11891   else
11892     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11893 }
11894 #endif
11895
11896 /* Walk through the list of incomplete types again, trying once more to
11897    emit full debugging info for them.  */
11898
11899 static void
11900 retry_incomplete_types (void)
11901 {
11902   int i;
11903
11904   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11905     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11906 }
11907
11908 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11909
11910 static void
11911 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11912 {
11913   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11914
11915   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11916      be incomplete and such types are not marked.  */
11917   add_abstract_origin_attribute (type_die, type);
11918 }
11919
11920 /* Determine what tag to use for a record type.  */
11921
11922 static enum dwarf_tag
11923 record_type_tag (tree type)
11924 {
11925   if (! lang_hooks.types.classify_record)
11926     return DW_TAG_structure_type;
11927
11928   switch (lang_hooks.types.classify_record (type))
11929     {
11930     case RECORD_IS_STRUCT:
11931       return DW_TAG_structure_type;
11932
11933     case RECORD_IS_CLASS:
11934       return DW_TAG_class_type;
11935
11936     case RECORD_IS_INTERFACE:
11937       return DW_TAG_interface_type;
11938
11939     default:
11940       gcc_unreachable ();
11941     }
11942 }
11943
11944 /* Generate a DIE to represent an inlined instance of a structure type.  */
11945
11946 static void
11947 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11948 {
11949   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
11950
11951   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11952      be incomplete and such types are not marked.  */
11953   add_abstract_origin_attribute (type_die, type);
11954 }
11955
11956 /* Generate a DIE to represent an inlined instance of a union type.  */
11957
11958 static void
11959 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11960 {
11961   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11962
11963   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11964      be incomplete and such types are not marked.  */
11965   add_abstract_origin_attribute (type_die, type);
11966 }
11967
11968 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11969    include all of the information about the enumeration values also. Each
11970    enumerated type name/value is listed as a child of the enumerated type
11971    DIE.  */
11972
11973 static dw_die_ref
11974 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11975 {
11976   dw_die_ref type_die = lookup_type_die (type);
11977
11978   if (type_die == NULL)
11979     {
11980       type_die = new_die (DW_TAG_enumeration_type,
11981                           scope_die_for (type, context_die), type);
11982       equate_type_number_to_die (type, type_die);
11983       add_name_attribute (type_die, type_tag (type));
11984     }
11985   else if (! TYPE_SIZE (type))
11986     return type_die;
11987   else
11988     remove_AT (type_die, DW_AT_declaration);
11989
11990   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11991      given enum type is incomplete, do not generate the DW_AT_byte_size
11992      attribute or the DW_AT_element_list attribute.  */
11993   if (TYPE_SIZE (type))
11994     {
11995       tree link;
11996
11997       TREE_ASM_WRITTEN (type) = 1;
11998       add_byte_size_attribute (type_die, type);
11999       if (TYPE_STUB_DECL (type) != NULL_TREE)
12000         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12001
12002       /* If the first reference to this type was as the return type of an
12003          inline function, then it may not have a parent.  Fix this now.  */
12004       if (type_die->die_parent == NULL)
12005         add_child_die (scope_die_for (type, context_die), type_die);
12006
12007       for (link = TYPE_VALUES (type);
12008            link != NULL; link = TREE_CHAIN (link))
12009         {
12010           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12011           tree value = TREE_VALUE (link);
12012
12013           add_name_attribute (enum_die,
12014                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12015
12016           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12017             /* DWARF2 does not provide a way of indicating whether or
12018                not enumeration constants are signed or unsigned.  GDB
12019                always assumes the values are signed, so we output all
12020                values as if they were signed.  That means that
12021                enumeration constants with very large unsigned values
12022                will appear to have negative values in the debugger.  */
12023             add_AT_int (enum_die, DW_AT_const_value,
12024                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12025         }
12026     }
12027   else
12028     add_AT_flag (type_die, DW_AT_declaration, 1);
12029
12030   if (get_AT (type_die, DW_AT_name))
12031     add_pubtype (type, type_die);
12032
12033   return type_die;
12034 }
12035
12036 /* Generate a DIE to represent either a real live formal parameter decl or to
12037    represent just the type of some formal parameter position in some function
12038    type.
12039
12040    Note that this routine is a bit unusual because its argument may be a
12041    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12042    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12043    node.  If it's the former then this function is being called to output a
12044    DIE to represent a formal parameter object (or some inlining thereof).  If
12045    it's the latter, then this function is only being called to output a
12046    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12047    argument type of some subprogram type.  */
12048
12049 static dw_die_ref
12050 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12051 {
12052   dw_die_ref parm_die
12053     = new_die (DW_TAG_formal_parameter, context_die, node);
12054   tree origin;
12055
12056   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12057     {
12058     case tcc_declaration:
12059       origin = decl_ultimate_origin (node);
12060       if (origin != NULL)
12061         add_abstract_origin_attribute (parm_die, origin);
12062       else
12063         {
12064           tree type = TREE_TYPE (node);
12065           add_name_and_src_coords_attributes (parm_die, node);
12066           if (DECL_BY_REFERENCE (node))
12067             type = TREE_TYPE (type);
12068           add_type_attribute (parm_die, type,
12069                               TREE_READONLY (node),
12070                               TREE_THIS_VOLATILE (node),
12071                               context_die);
12072           if (DECL_ARTIFICIAL (node))
12073             add_AT_flag (parm_die, DW_AT_artificial, 1);
12074         }
12075
12076       equate_decl_number_to_die (node, parm_die);
12077       if (! DECL_ABSTRACT (node))
12078         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12079
12080       break;
12081
12082     case tcc_type:
12083       /* We were called with some kind of a ..._TYPE node.  */
12084       add_type_attribute (parm_die, node, 0, 0, context_die);
12085       break;
12086
12087     default:
12088       gcc_unreachable ();
12089     }
12090
12091   return parm_die;
12092 }
12093
12094 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12095    at the end of an (ANSI prototyped) formal parameters list.  */
12096
12097 static void
12098 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12099 {
12100   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12101 }
12102
12103 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12104    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12105    parameters as specified in some function type specification (except for
12106    those which appear as part of a function *definition*).  */
12107
12108 static void
12109 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12110 {
12111   tree link;
12112   tree formal_type = NULL;
12113   tree first_parm_type;
12114   tree arg;
12115
12116   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12117     {
12118       arg = DECL_ARGUMENTS (function_or_method_type);
12119       function_or_method_type = TREE_TYPE (function_or_method_type);
12120     }
12121   else
12122     arg = NULL_TREE;
12123
12124   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12125
12126   /* Make our first pass over the list of formal parameter types and output a
12127      DW_TAG_formal_parameter DIE for each one.  */
12128   for (link = first_parm_type; link; )
12129     {
12130       dw_die_ref parm_die;
12131
12132       formal_type = TREE_VALUE (link);
12133       if (formal_type == void_type_node)
12134         break;
12135
12136       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12137       parm_die = gen_formal_parameter_die (formal_type, context_die);
12138       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12139            && link == first_parm_type)
12140           || (arg && DECL_ARTIFICIAL (arg)))
12141         add_AT_flag (parm_die, DW_AT_artificial, 1);
12142
12143       link = TREE_CHAIN (link);
12144       if (arg)
12145         arg = TREE_CHAIN (arg);
12146     }
12147
12148   /* If this function type has an ellipsis, add a
12149      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12150   if (formal_type != void_type_node)
12151     gen_unspecified_parameters_die (function_or_method_type, context_die);
12152
12153   /* Make our second (and final) pass over the list of formal parameter types
12154      and output DIEs to represent those types (as necessary).  */
12155   for (link = TYPE_ARG_TYPES (function_or_method_type);
12156        link && TREE_VALUE (link);
12157        link = TREE_CHAIN (link))
12158     gen_type_die (TREE_VALUE (link), context_die);
12159 }
12160
12161 /* We want to generate the DIE for TYPE so that we can generate the
12162    die for MEMBER, which has been defined; we will need to refer back
12163    to the member declaration nested within TYPE.  If we're trying to
12164    generate minimal debug info for TYPE, processing TYPE won't do the
12165    trick; we need to attach the member declaration by hand.  */
12166
12167 static void
12168 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12169 {
12170   gen_type_die (type, context_die);
12171
12172   /* If we're trying to avoid duplicate debug info, we may not have
12173      emitted the member decl for this function.  Emit it now.  */
12174   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12175       && ! lookup_decl_die (member))
12176     {
12177       dw_die_ref type_die;
12178       gcc_assert (!decl_ultimate_origin (member));
12179
12180       push_decl_scope (type);
12181       type_die = lookup_type_die (type);
12182       if (TREE_CODE (member) == FUNCTION_DECL)
12183         gen_subprogram_die (member, type_die);
12184       else if (TREE_CODE (member) == FIELD_DECL)
12185         {
12186           /* Ignore the nameless fields that are used to skip bits but handle
12187              C++ anonymous unions and structs.  */
12188           if (DECL_NAME (member) != NULL_TREE
12189               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12190               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12191             {
12192               gen_type_die (member_declared_type (member), type_die);
12193               gen_field_die (member, type_die);
12194             }
12195         }
12196       else
12197         gen_variable_die (member, type_die);
12198
12199       pop_decl_scope ();
12200     }
12201 }
12202
12203 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12204    may later generate inlined and/or out-of-line instances of.  */
12205
12206 static void
12207 dwarf2out_abstract_function (tree decl)
12208 {
12209   dw_die_ref old_die;
12210   tree save_fn;
12211   tree context;
12212   int was_abstract = DECL_ABSTRACT (decl);
12213
12214   /* Make sure we have the actual abstract inline, not a clone.  */
12215   decl = DECL_ORIGIN (decl);
12216
12217   old_die = lookup_decl_die (decl);
12218   if (old_die && get_AT (old_die, DW_AT_inline))
12219     /* We've already generated the abstract instance.  */
12220     return;
12221
12222   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12223      we don't get confused by DECL_ABSTRACT.  */
12224   if (debug_info_level > DINFO_LEVEL_TERSE)
12225     {
12226       context = decl_class_context (decl);
12227       if (context)
12228         gen_type_die_for_member
12229           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12230     }
12231
12232   /* Pretend we've just finished compiling this function.  */
12233   save_fn = current_function_decl;
12234   current_function_decl = decl;
12235   push_cfun (DECL_STRUCT_FUNCTION (decl));
12236
12237   set_decl_abstract_flags (decl, 1);
12238   dwarf2out_decl (decl);
12239   if (! was_abstract)
12240     set_decl_abstract_flags (decl, 0);
12241
12242   current_function_decl = save_fn;
12243   pop_cfun ();
12244 }
12245
12246 /* Helper function of premark_used_types() which gets called through
12247    htab_traverse_resize().
12248
12249    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12250    marked as unused by prune_unused_types.  */
12251 static int
12252 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12253 {
12254   tree type;
12255   dw_die_ref die;
12256
12257   type = *slot;
12258   die = lookup_type_die (type);
12259   if (die != NULL)
12260     die->die_perennial_p = 1;
12261   return 1;
12262 }
12263
12264 /* Mark all members of used_types_hash as perennial.  */
12265 static void
12266 premark_used_types (void)
12267 {
12268   if (cfun && cfun->used_types_hash)
12269     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12270 }
12271
12272 /* Generate a DIE to represent a declared function (either file-scope or
12273    block-local).  */
12274
12275 static void
12276 gen_subprogram_die (tree decl, dw_die_ref context_die)
12277 {
12278   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12279   tree origin = decl_ultimate_origin (decl);
12280   dw_die_ref subr_die;
12281   tree fn_arg_types;
12282   tree outer_scope;
12283   dw_die_ref old_die = lookup_decl_die (decl);
12284   int declaration = (current_function_decl != decl
12285                      || class_or_namespace_scope_p (context_die));
12286
12287   premark_used_types ();
12288
12289   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12290      started to generate the abstract instance of an inline, decided to output
12291      its containing class, and proceeded to emit the declaration of the inline
12292      from the member list for the class.  If so, DECLARATION takes priority;
12293      we'll get back to the abstract instance when done with the class.  */
12294
12295   /* The class-scope declaration DIE must be the primary DIE.  */
12296   if (origin && declaration && class_or_namespace_scope_p (context_die))
12297     {
12298       origin = NULL;
12299       gcc_assert (!old_die);
12300     }
12301
12302   /* Now that the C++ front end lazily declares artificial member fns, we
12303      might need to retrofit the declaration into its class.  */
12304   if (!declaration && !origin && !old_die
12305       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12306       && !class_or_namespace_scope_p (context_die)
12307       && debug_info_level > DINFO_LEVEL_TERSE)
12308     old_die = force_decl_die (decl);
12309
12310   if (origin != NULL)
12311     {
12312       gcc_assert (!declaration || local_scope_p (context_die));
12313
12314       /* Fixup die_parent for the abstract instance of a nested
12315          inline function.  */
12316       if (old_die && old_die->die_parent == NULL)
12317         add_child_die (context_die, old_die);
12318
12319       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12320       add_abstract_origin_attribute (subr_die, origin);
12321     }
12322   else if (old_die)
12323     {
12324       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12325       struct dwarf_file_data * file_index = lookup_filename (s.file);
12326
12327       if (!get_AT_flag (old_die, DW_AT_declaration)
12328           /* We can have a normal definition following an inline one in the
12329              case of redefinition of GNU C extern inlines.
12330              It seems reasonable to use AT_specification in this case.  */
12331           && !get_AT (old_die, DW_AT_inline))
12332         {
12333           /* Detect and ignore this case, where we are trying to output
12334              something we have already output.  */
12335           return;
12336         }
12337
12338       /* If the definition comes from the same place as the declaration,
12339          maybe use the old DIE.  We always want the DIE for this function
12340          that has the *_pc attributes to be under comp_unit_die so the
12341          debugger can find it.  We also need to do this for abstract
12342          instances of inlines, since the spec requires the out-of-line copy
12343          to have the same parent.  For local class methods, this doesn't
12344          apply; we just use the old DIE.  */
12345       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12346           && (DECL_ARTIFICIAL (decl)
12347               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12348                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12349                       == (unsigned) s.line))))
12350         {
12351           subr_die = old_die;
12352
12353           /* Clear out the declaration attribute and the formal parameters.
12354              Do not remove all children, because it is possible that this
12355              declaration die was forced using force_decl_die(). In such
12356              cases die that forced declaration die (e.g. TAG_imported_module)
12357              is one of the children that we do not want to remove.  */
12358           remove_AT (subr_die, DW_AT_declaration);
12359           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12360         }
12361       else
12362         {
12363           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12364           add_AT_specification (subr_die, old_die);
12365           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12366             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12367           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12368             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12369         }
12370     }
12371   else
12372     {
12373       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12374
12375       if (TREE_PUBLIC (decl))
12376         add_AT_flag (subr_die, DW_AT_external, 1);
12377
12378       add_name_and_src_coords_attributes (subr_die, decl);
12379       if (debug_info_level > DINFO_LEVEL_TERSE)
12380         {
12381           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12382           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12383                               0, 0, context_die);
12384         }
12385
12386       add_pure_or_virtual_attribute (subr_die, decl);
12387       if (DECL_ARTIFICIAL (decl))
12388         add_AT_flag (subr_die, DW_AT_artificial, 1);
12389
12390       if (TREE_PROTECTED (decl))
12391         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12392       else if (TREE_PRIVATE (decl))
12393         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12394     }
12395
12396   if (declaration)
12397     {
12398       if (!old_die || !get_AT (old_die, DW_AT_inline))
12399         {
12400           add_AT_flag (subr_die, DW_AT_declaration, 1);
12401
12402           /* The first time we see a member function, it is in the context of
12403              the class to which it belongs.  We make sure of this by emitting
12404              the class first.  The next time is the definition, which is
12405              handled above.  The two may come from the same source text.
12406
12407              Note that force_decl_die() forces function declaration die. It is
12408              later reused to represent definition.  */
12409           equate_decl_number_to_die (decl, subr_die);
12410         }
12411     }
12412   else if (DECL_ABSTRACT (decl))
12413     {
12414       if (DECL_DECLARED_INLINE_P (decl))
12415         {
12416           if (cgraph_function_possibly_inlined_p (decl))
12417             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12418           else
12419             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12420         }
12421       else
12422         {
12423           if (cgraph_function_possibly_inlined_p (decl))
12424             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12425           else
12426             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12427         }
12428
12429       if (DECL_DECLARED_INLINE_P (decl)
12430           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12431         add_AT_flag (subr_die, DW_AT_artificial, 1);
12432
12433       equate_decl_number_to_die (decl, subr_die);
12434     }
12435   else if (!DECL_EXTERNAL (decl))
12436     {
12437       HOST_WIDE_INT cfa_fb_offset;
12438
12439       if (!old_die || !get_AT (old_die, DW_AT_inline))
12440         equate_decl_number_to_die (decl, subr_die);
12441
12442       if (!flag_reorder_blocks_and_partition)
12443         {
12444           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12445                                        current_function_funcdef_no);
12446           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12447           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12448                                        current_function_funcdef_no);
12449           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12450
12451           add_pubname (decl, subr_die);
12452           add_arange (decl, subr_die);
12453         }
12454       else
12455         {  /* Do nothing for now; maybe need to duplicate die, one for
12456               hot section and ond for cold section, then use the hot/cold
12457               section begin/end labels to generate the aranges...  */
12458           /*
12459             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12460             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12461             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12462             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12463
12464             add_pubname (decl, subr_die);
12465             add_arange (decl, subr_die);
12466             add_arange (decl, subr_die);
12467            */
12468         }
12469
12470 #ifdef MIPS_DEBUGGING_INFO
12471       /* Add a reference to the FDE for this routine.  */
12472       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12473 #endif
12474
12475       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12476
12477       /* We define the "frame base" as the function's CFA.  This is more
12478          convenient for several reasons: (1) It's stable across the prologue
12479          and epilogue, which makes it better than just a frame pointer,
12480          (2) With dwarf3, there exists a one-byte encoding that allows us
12481          to reference the .debug_frame data by proxy, but failing that,
12482          (3) We can at least reuse the code inspection and interpretation
12483          code that determines the CFA position at various points in the
12484          function.  */
12485       /* ??? Use some command-line or configury switch to enable the use
12486          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
12487          consumers that understand it; fall back to "pure" dwarf2 and
12488          convert the CFA data into a location list.  */
12489       {
12490         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12491         if (list->dw_loc_next)
12492           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12493         else
12494           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12495       }
12496
12497       /* Compute a displacement from the "steady-state frame pointer" to
12498          the CFA.  The former is what all stack slots and argument slots
12499          will reference in the rtl; the later is what we've told the
12500          debugger about.  We'll need to adjust all frame_base references
12501          by this displacement.  */
12502       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12503
12504       if (cfun->static_chain_decl)
12505         add_AT_location_description (subr_die, DW_AT_static_link,
12506                  loc_descriptor_from_tree (cfun->static_chain_decl));
12507     }
12508
12509   /* Now output descriptions of the arguments for this function. This gets
12510      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12511      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12512      `...' at the end of the formal parameter list.  In order to find out if
12513      there was a trailing ellipsis or not, we must instead look at the type
12514      associated with the FUNCTION_DECL.  This will be a node of type
12515      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12516      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12517      an ellipsis at the end.  */
12518
12519   /* In the case where we are describing a mere function declaration, all we
12520      need to do here (and all we *can* do here) is to describe the *types* of
12521      its formal parameters.  */
12522   if (debug_info_level <= DINFO_LEVEL_TERSE)
12523     ;
12524   else if (declaration)
12525     gen_formal_types_die (decl, subr_die);
12526   else
12527     {
12528       /* Generate DIEs to represent all known formal parameters.  */
12529       tree arg_decls = DECL_ARGUMENTS (decl);
12530       tree parm;
12531
12532       /* When generating DIEs, generate the unspecified_parameters DIE
12533          instead if we come across the arg "__builtin_va_alist" */
12534       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12535         if (TREE_CODE (parm) == PARM_DECL)
12536           {
12537             if (DECL_NAME (parm)
12538                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12539                             "__builtin_va_alist"))
12540               gen_unspecified_parameters_die (parm, subr_die);
12541             else
12542               gen_decl_die (parm, subr_die);
12543           }
12544
12545       /* Decide whether we need an unspecified_parameters DIE at the end.
12546          There are 2 more cases to do this for: 1) the ansi ... declaration -
12547          this is detectable when the end of the arg list is not a
12548          void_type_node 2) an unprototyped function declaration (not a
12549          definition).  This just means that we have no info about the
12550          parameters at all.  */
12551       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12552       if (fn_arg_types != NULL)
12553         {
12554           /* This is the prototyped case, check for....  */
12555           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12556             gen_unspecified_parameters_die (decl, subr_die);
12557         }
12558       else if (DECL_INITIAL (decl) == NULL_TREE)
12559         gen_unspecified_parameters_die (decl, subr_die);
12560     }
12561
12562   /* Output Dwarf info for all of the stuff within the body of the function
12563      (if it has one - it may be just a declaration).  */
12564   outer_scope = DECL_INITIAL (decl);
12565
12566   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12567      a function.  This BLOCK actually represents the outermost binding contour
12568      for the function, i.e. the contour in which the function's formal
12569      parameters and labels get declared. Curiously, it appears that the front
12570      end doesn't actually put the PARM_DECL nodes for the current function onto
12571      the BLOCK_VARS list for this outer scope, but are strung off of the
12572      DECL_ARGUMENTS list for the function instead.
12573
12574      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12575      the LABEL_DECL nodes for the function however, and we output DWARF info
12576      for those in decls_for_scope.  Just within the `outer_scope' there will be
12577      a BLOCK node representing the function's outermost pair of curly braces,
12578      and any blocks used for the base and member initializers of a C++
12579      constructor function.  */
12580   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12581     {
12582       /* Emit a DW_TAG_variable DIE for a named return value.  */
12583       if (DECL_NAME (DECL_RESULT (decl)))
12584         gen_decl_die (DECL_RESULT (decl), subr_die);
12585
12586       current_function_has_inlines = 0;
12587       decls_for_scope (outer_scope, subr_die, 0);
12588
12589 #if 0 && defined (MIPS_DEBUGGING_INFO)
12590       if (current_function_has_inlines)
12591         {
12592           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12593           if (! comp_unit_has_inlines)
12594             {
12595               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12596               comp_unit_has_inlines = 1;
12597             }
12598         }
12599 #endif
12600     }
12601   /* Add the calling convention attribute if requested.  */
12602   add_calling_convention_attribute (subr_die, decl);
12603
12604 }
12605
12606 /* Generate a DIE to represent a declared data object.  */
12607
12608 static void
12609 gen_variable_die (tree decl, dw_die_ref context_die)
12610 {
12611   tree origin = decl_ultimate_origin (decl);
12612   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12613
12614   dw_die_ref old_die = lookup_decl_die (decl);
12615   int declaration = (DECL_EXTERNAL (decl)
12616                      /* If DECL is COMDAT and has not actually been
12617                         emitted, we cannot take its address; there
12618                         might end up being no definition anywhere in
12619                         the program.  For example, consider the C++
12620                         test case:
12621
12622                           template <class T>
12623                           struct S { static const int i = 7; };
12624
12625                           template <class T>
12626                           const int S<T>::i;
12627
12628                           int f() { return S<int>::i; }
12629
12630                         Here, S<int>::i is not DECL_EXTERNAL, but no
12631                         definition is required, so the compiler will
12632                         not emit a definition.  */
12633                      || (TREE_CODE (decl) == VAR_DECL
12634                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12635                      || class_or_namespace_scope_p (context_die));
12636
12637   if (origin != NULL)
12638     add_abstract_origin_attribute (var_die, origin);
12639
12640   /* Loop unrolling can create multiple blocks that refer to the same
12641      static variable, so we must test for the DW_AT_declaration flag.
12642
12643      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12644      copy decls and set the DECL_ABSTRACT flag on them instead of
12645      sharing them.
12646
12647      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12648
12649      ??? The declare_in_namespace support causes us to get two DIEs for one
12650      variable, both of which are declarations.  We want to avoid considering
12651      one to be a specification, so we must test that this DIE is not a
12652      declaration.  */
12653   else if (old_die && TREE_STATIC (decl) && ! declaration
12654            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12655     {
12656       /* This is a definition of a C++ class level static.  */
12657       add_AT_specification (var_die, old_die);
12658       if (DECL_NAME (decl))
12659         {
12660           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12661           struct dwarf_file_data * file_index = lookup_filename (s.file);
12662
12663           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12664             add_AT_file (var_die, DW_AT_decl_file, file_index);
12665
12666           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12667             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12668         }
12669     }
12670   else
12671     {
12672       tree type = TREE_TYPE (decl);
12673       if ((TREE_CODE (decl) == PARM_DECL
12674            || TREE_CODE (decl) == RESULT_DECL)
12675           && DECL_BY_REFERENCE (decl))
12676         type = TREE_TYPE (type);
12677
12678       add_name_and_src_coords_attributes (var_die, decl);
12679       add_type_attribute (var_die, type, TREE_READONLY (decl),
12680                           TREE_THIS_VOLATILE (decl), context_die);
12681
12682       if (TREE_PUBLIC (decl))
12683         add_AT_flag (var_die, DW_AT_external, 1);
12684
12685       if (DECL_ARTIFICIAL (decl))
12686         add_AT_flag (var_die, DW_AT_artificial, 1);
12687
12688       if (TREE_PROTECTED (decl))
12689         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12690       else if (TREE_PRIVATE (decl))
12691         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12692     }
12693
12694   if (declaration)
12695     add_AT_flag (var_die, DW_AT_declaration, 1);
12696
12697   if (DECL_ABSTRACT (decl) || declaration)
12698     equate_decl_number_to_die (decl, var_die);
12699
12700   if (! declaration && ! DECL_ABSTRACT (decl))
12701     {
12702       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12703       add_pubname (decl, var_die);
12704     }
12705   else
12706     tree_add_const_value_attribute (var_die, decl);
12707 }
12708
12709 /* Generate a DIE to represent a label identifier.  */
12710
12711 static void
12712 gen_label_die (tree decl, dw_die_ref context_die)
12713 {
12714   tree origin = decl_ultimate_origin (decl);
12715   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12716   rtx insn;
12717   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12718
12719   if (origin != NULL)
12720     add_abstract_origin_attribute (lbl_die, origin);
12721   else
12722     add_name_and_src_coords_attributes (lbl_die, decl);
12723
12724   if (DECL_ABSTRACT (decl))
12725     equate_decl_number_to_die (decl, lbl_die);
12726   else
12727     {
12728       insn = DECL_RTL_IF_SET (decl);
12729
12730       /* Deleted labels are programmer specified labels which have been
12731          eliminated because of various optimizations.  We still emit them
12732          here so that it is possible to put breakpoints on them.  */
12733       if (insn
12734           && (LABEL_P (insn)
12735               || ((NOTE_P (insn)
12736                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12737         {
12738           /* When optimization is enabled (via -O) some parts of the compiler
12739              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12740              represent source-level labels which were explicitly declared by
12741              the user.  This really shouldn't be happening though, so catch
12742              it if it ever does happen.  */
12743           gcc_assert (!INSN_DELETED_P (insn));
12744
12745           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12746           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12747         }
12748     }
12749 }
12750
12751 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12752    attributes to the DIE for a block STMT, to describe where the inlined
12753    function was called from.  This is similar to add_src_coords_attributes.  */
12754
12755 static inline void
12756 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12757 {
12758   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12759
12760   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12761   add_AT_unsigned (die, DW_AT_call_line, s.line);
12762 }
12763
12764
12765 /* If STMT's abstract origin is a function declaration and STMT's
12766    first subblock's abstract origin is the function's outermost block,
12767    then we're looking at the main entry point.  */
12768 static bool
12769 is_inlined_entry_point (const_tree stmt)
12770 {
12771   tree decl, block;
12772
12773   if (!stmt || TREE_CODE (stmt) != BLOCK)
12774     return false;
12775
12776   decl = block_ultimate_origin (stmt);
12777
12778   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12779     return false;
12780
12781   block = BLOCK_SUBBLOCKS (stmt);
12782
12783   if (block)
12784     {
12785       if (TREE_CODE (block) != BLOCK)
12786         return false;
12787
12788       block = block_ultimate_origin (block);
12789     }
12790
12791   return block == DECL_INITIAL (decl);
12792 }
12793
12794 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12795    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12796
12797 static inline void
12798 add_high_low_attributes (tree stmt, dw_die_ref die)
12799 {
12800   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12801
12802   if (BLOCK_FRAGMENT_CHAIN (stmt))
12803     {
12804       tree chain;
12805
12806       if (is_inlined_entry_point (stmt))
12807         {
12808           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12809                                        BLOCK_NUMBER (stmt));
12810           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12811         }
12812
12813       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12814
12815       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12816       do
12817         {
12818           add_ranges (chain);
12819           chain = BLOCK_FRAGMENT_CHAIN (chain);
12820         }
12821       while (chain);
12822       add_ranges (NULL);
12823     }
12824   else
12825     {
12826       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12827                                    BLOCK_NUMBER (stmt));
12828       add_AT_lbl_id (die, DW_AT_low_pc, label);
12829       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12830                                    BLOCK_NUMBER (stmt));
12831       add_AT_lbl_id (die, DW_AT_high_pc, label);
12832     }
12833 }
12834
12835 /* Generate a DIE for a lexical block.  */
12836
12837 static void
12838 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12839 {
12840   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12841
12842   if (! BLOCK_ABSTRACT (stmt))
12843     add_high_low_attributes (stmt, stmt_die);
12844
12845   decls_for_scope (stmt, stmt_die, depth);
12846 }
12847
12848 /* Generate a DIE for an inlined subprogram.  */
12849
12850 static void
12851 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12852 {
12853   tree decl = block_ultimate_origin (stmt);
12854
12855   /* Emit info for the abstract instance first, if we haven't yet.  We
12856      must emit this even if the block is abstract, otherwise when we
12857      emit the block below (or elsewhere), we may end up trying to emit
12858      a die whose origin die hasn't been emitted, and crashing.  */
12859   dwarf2out_abstract_function (decl);
12860
12861   if (! BLOCK_ABSTRACT (stmt))
12862     {
12863       dw_die_ref subr_die
12864         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12865
12866       add_abstract_origin_attribute (subr_die, decl);
12867       add_high_low_attributes (stmt, subr_die);
12868       add_call_src_coords_attributes (stmt, subr_die);
12869
12870       decls_for_scope (stmt, subr_die, depth);
12871       current_function_has_inlines = 1;
12872     }
12873   else
12874     /* We may get here if we're the outer block of function A that was
12875        inlined into function B that was inlined into function C.  When
12876        generating debugging info for C, dwarf2out_abstract_function(B)
12877        would mark all inlined blocks as abstract, including this one.
12878        So, we wouldn't (and shouldn't) expect labels to be generated
12879        for this one.  Instead, just emit debugging info for
12880        declarations within the block.  This is particularly important
12881        in the case of initializers of arguments passed from B to us:
12882        if they're statement expressions containing declarations, we
12883        wouldn't generate dies for their abstract variables, and then,
12884        when generating dies for the real variables, we'd die (pun
12885        intended :-)  */
12886     gen_lexical_block_die (stmt, context_die, depth);
12887 }
12888
12889 /* Generate a DIE for a field in a record, or structure.  */
12890
12891 static void
12892 gen_field_die (tree decl, dw_die_ref context_die)
12893 {
12894   dw_die_ref decl_die;
12895
12896   if (TREE_TYPE (decl) == error_mark_node)
12897     return;
12898
12899   decl_die = new_die (DW_TAG_member, context_die, decl);
12900   add_name_and_src_coords_attributes (decl_die, decl);
12901   add_type_attribute (decl_die, member_declared_type (decl),
12902                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12903                       context_die);
12904
12905   if (DECL_BIT_FIELD_TYPE (decl))
12906     {
12907       add_byte_size_attribute (decl_die, decl);
12908       add_bit_size_attribute (decl_die, decl);
12909       add_bit_offset_attribute (decl_die, decl);
12910     }
12911
12912   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12913     add_data_member_location_attribute (decl_die, decl);
12914
12915   if (DECL_ARTIFICIAL (decl))
12916     add_AT_flag (decl_die, DW_AT_artificial, 1);
12917
12918   if (TREE_PROTECTED (decl))
12919     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12920   else if (TREE_PRIVATE (decl))
12921     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12922
12923   /* Equate decl number to die, so that we can look up this decl later on.  */
12924   equate_decl_number_to_die (decl, decl_die);
12925 }
12926
12927 #if 0
12928 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12929    Use modified_type_die instead.
12930    We keep this code here just in case these types of DIEs may be needed to
12931    represent certain things in other languages (e.g. Pascal) someday.  */
12932
12933 static void
12934 gen_pointer_type_die (tree type, dw_die_ref context_die)
12935 {
12936   dw_die_ref ptr_die
12937     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12938
12939   equate_type_number_to_die (type, ptr_die);
12940   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12941   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12942 }
12943
12944 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12945    Use modified_type_die instead.
12946    We keep this code here just in case these types of DIEs may be needed to
12947    represent certain things in other languages (e.g. Pascal) someday.  */
12948
12949 static void
12950 gen_reference_type_die (tree type, dw_die_ref context_die)
12951 {
12952   dw_die_ref ref_die
12953     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12954
12955   equate_type_number_to_die (type, ref_die);
12956   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12957   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12958 }
12959 #endif
12960
12961 /* Generate a DIE for a pointer to a member type.  */
12962
12963 static void
12964 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12965 {
12966   dw_die_ref ptr_die
12967     = new_die (DW_TAG_ptr_to_member_type,
12968                scope_die_for (type, context_die), type);
12969
12970   equate_type_number_to_die (type, ptr_die);
12971   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12972                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12973   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12974 }
12975
12976 /* Generate the DIE for the compilation unit.  */
12977
12978 static dw_die_ref
12979 gen_compile_unit_die (const char *filename)
12980 {
12981   dw_die_ref die;
12982   char producer[250];
12983   const char *language_string = lang_hooks.name;
12984   int language;
12985
12986   die = new_die (DW_TAG_compile_unit, NULL, NULL);
12987
12988   if (filename)
12989     {
12990       add_name_attribute (die, filename);
12991       /* Don't add cwd for <built-in>.  */
12992       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12993         add_comp_dir_attribute (die);
12994     }
12995
12996   sprintf (producer, "%s %s", language_string, version_string);
12997
12998 #ifdef MIPS_DEBUGGING_INFO
12999   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13000      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13001      not appear in the producer string, the debugger reaches the conclusion
13002      that the object file is stripped and has no debugging information.
13003      To get the MIPS/SGI debugger to believe that there is debugging
13004      information in the object file, we add a -g to the producer string.  */
13005   if (debug_info_level > DINFO_LEVEL_TERSE)
13006     strcat (producer, " -g");
13007 #endif
13008
13009   add_AT_string (die, DW_AT_producer, producer);
13010
13011   if (strcmp (language_string, "GNU C++") == 0)
13012     language = DW_LANG_C_plus_plus;
13013   else if (strcmp (language_string, "GNU Ada") == 0)
13014     language = DW_LANG_Ada95;
13015   else if (strcmp (language_string, "GNU F77") == 0)
13016     language = DW_LANG_Fortran77;
13017   else if (strcmp (language_string, "GNU F95") == 0)
13018     language = DW_LANG_Fortran95;
13019   else if (strcmp (language_string, "GNU Pascal") == 0)
13020     language = DW_LANG_Pascal83;
13021   else if (strcmp (language_string, "GNU Java") == 0)
13022     language = DW_LANG_Java;
13023   else if (strcmp (language_string, "GNU Objective-C") == 0)
13024     language = DW_LANG_ObjC;
13025   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13026     language = DW_LANG_ObjC_plus_plus;
13027   else
13028     language = DW_LANG_C89;
13029
13030   add_AT_unsigned (die, DW_AT_language, language);
13031   return die;
13032 }
13033
13034 /* Generate the DIE for a base class.  */
13035
13036 static void
13037 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13038 {
13039   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13040
13041   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13042   add_data_member_location_attribute (die, binfo);
13043
13044   if (BINFO_VIRTUAL_P (binfo))
13045     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13046
13047   if (access == access_public_node)
13048     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13049   else if (access == access_protected_node)
13050     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13051 }
13052
13053 /* Generate a DIE for a class member.  */
13054
13055 static void
13056 gen_member_die (tree type, dw_die_ref context_die)
13057 {
13058   tree member;
13059   tree binfo = TYPE_BINFO (type);
13060   dw_die_ref child;
13061
13062   /* If this is not an incomplete type, output descriptions of each of its
13063      members. Note that as we output the DIEs necessary to represent the
13064      members of this record or union type, we will also be trying to output
13065      DIEs to represent the *types* of those members. However the `type'
13066      function (above) will specifically avoid generating type DIEs for member
13067      types *within* the list of member DIEs for this (containing) type except
13068      for those types (of members) which are explicitly marked as also being
13069      members of this (containing) type themselves.  The g++ front- end can
13070      force any given type to be treated as a member of some other (containing)
13071      type by setting the TYPE_CONTEXT of the given (member) type to point to
13072      the TREE node representing the appropriate (containing) type.  */
13073
13074   /* First output info about the base classes.  */
13075   if (binfo)
13076     {
13077       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13078       int i;
13079       tree base;
13080
13081       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13082         gen_inheritance_die (base,
13083                              (accesses ? VEC_index (tree, accesses, i)
13084                               : access_public_node), context_die);
13085     }
13086
13087   /* Now output info about the data members and type members.  */
13088   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13089     {
13090       /* If we thought we were generating minimal debug info for TYPE
13091          and then changed our minds, some of the member declarations
13092          may have already been defined.  Don't define them again, but
13093          do put them in the right order.  */
13094
13095       child = lookup_decl_die (member);
13096       if (child)
13097         splice_child_die (context_die, child);
13098       else
13099         gen_decl_die (member, context_die);
13100     }
13101
13102   /* Now output info about the function members (if any).  */
13103   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13104     {
13105       /* Don't include clones in the member list.  */
13106       if (DECL_ABSTRACT_ORIGIN (member))
13107         continue;
13108
13109       child = lookup_decl_die (member);
13110       if (child)
13111         splice_child_die (context_die, child);
13112       else
13113         gen_decl_die (member, context_die);
13114     }
13115 }
13116
13117 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13118    is set, we pretend that the type was never defined, so we only get the
13119    member DIEs needed by later specification DIEs.  */
13120
13121 static void
13122 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13123                                 enum debug_info_usage usage)
13124 {
13125   dw_die_ref type_die = lookup_type_die (type);
13126   dw_die_ref scope_die = 0;
13127   int nested = 0;
13128   int complete = (TYPE_SIZE (type)
13129                   && (! TYPE_STUB_DECL (type)
13130                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13131   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13132   complete = complete && should_emit_struct_debug (type, usage);
13133
13134   if (type_die && ! complete)
13135     return;
13136
13137   if (TYPE_CONTEXT (type) != NULL_TREE
13138       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13139           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13140     nested = 1;
13141
13142   scope_die = scope_die_for (type, context_die);
13143
13144   if (! type_die || (nested && scope_die == comp_unit_die))
13145     /* First occurrence of type or toplevel definition of nested class.  */
13146     {
13147       dw_die_ref old_die = type_die;
13148
13149       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13150                           ? record_type_tag (type) : DW_TAG_union_type,
13151                           scope_die, type);
13152       equate_type_number_to_die (type, type_die);
13153       if (old_die)
13154         add_AT_specification (type_die, old_die);
13155       else
13156         add_name_attribute (type_die, type_tag (type));
13157     }
13158   else
13159     remove_AT (type_die, DW_AT_declaration);
13160
13161   /* If this type has been completed, then give it a byte_size attribute and
13162      then give a list of members.  */
13163   if (complete && !ns_decl)
13164     {
13165       /* Prevent infinite recursion in cases where the type of some member of
13166          this type is expressed in terms of this type itself.  */
13167       TREE_ASM_WRITTEN (type) = 1;
13168       add_byte_size_attribute (type_die, type);
13169       if (TYPE_STUB_DECL (type) != NULL_TREE)
13170         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13171
13172       /* If the first reference to this type was as the return type of an
13173          inline function, then it may not have a parent.  Fix this now.  */
13174       if (type_die->die_parent == NULL)
13175         add_child_die (scope_die, type_die);
13176
13177       push_decl_scope (type);
13178       gen_member_die (type, type_die);
13179       pop_decl_scope ();
13180
13181       /* GNU extension: Record what type our vtable lives in.  */
13182       if (TYPE_VFIELD (type))
13183         {
13184           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13185
13186           gen_type_die (vtype, context_die);
13187           add_AT_die_ref (type_die, DW_AT_containing_type,
13188                           lookup_type_die (vtype));
13189         }
13190     }
13191   else
13192     {
13193       add_AT_flag (type_die, DW_AT_declaration, 1);
13194
13195       /* We don't need to do this for function-local types.  */
13196       if (TYPE_STUB_DECL (type)
13197           && ! decl_function_context (TYPE_STUB_DECL (type)))
13198         VEC_safe_push (tree, gc, incomplete_types, type);
13199     }
13200
13201   if (get_AT (type_die, DW_AT_name))
13202     add_pubtype (type, type_die);
13203 }
13204
13205 /* Generate a DIE for a subroutine _type_.  */
13206
13207 static void
13208 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13209 {
13210   tree return_type = TREE_TYPE (type);
13211   dw_die_ref subr_die
13212     = new_die (DW_TAG_subroutine_type,
13213                scope_die_for (type, context_die), type);
13214
13215   equate_type_number_to_die (type, subr_die);
13216   add_prototyped_attribute (subr_die, type);
13217   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13218   gen_formal_types_die (type, subr_die);
13219
13220   if (get_AT (subr_die, DW_AT_name))
13221     add_pubtype (type, subr_die);
13222 }
13223
13224 /* Generate a DIE for a type definition.  */
13225
13226 static void
13227 gen_typedef_die (tree decl, dw_die_ref context_die)
13228 {
13229   dw_die_ref type_die;
13230   tree origin;
13231
13232   if (TREE_ASM_WRITTEN (decl))
13233     return;
13234
13235   TREE_ASM_WRITTEN (decl) = 1;
13236   type_die = new_die (DW_TAG_typedef, context_die, decl);
13237   origin = decl_ultimate_origin (decl);
13238   if (origin != NULL)
13239     add_abstract_origin_attribute (type_die, origin);
13240   else
13241     {
13242       tree type;
13243
13244       add_name_and_src_coords_attributes (type_die, decl);
13245       if (DECL_ORIGINAL_TYPE (decl))
13246         {
13247           type = DECL_ORIGINAL_TYPE (decl);
13248
13249           gcc_assert (type != TREE_TYPE (decl));
13250           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13251         }
13252       else
13253         type = TREE_TYPE (decl);
13254
13255       add_type_attribute (type_die, type, TREE_READONLY (decl),
13256                           TREE_THIS_VOLATILE (decl), context_die);
13257     }
13258
13259   if (DECL_ABSTRACT (decl))
13260     equate_decl_number_to_die (decl, type_die);
13261
13262   if (get_AT (type_die, DW_AT_name))
13263     add_pubtype (decl, type_die);
13264 }
13265
13266 /* Generate a type description DIE.  */
13267
13268 static void
13269 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13270                                 enum debug_info_usage usage)
13271 {
13272   int need_pop;
13273   struct array_descr_info info;
13274
13275   if (type == NULL_TREE || type == error_mark_node)
13276     return;
13277
13278   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13279       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13280     {
13281       if (TREE_ASM_WRITTEN (type))
13282         return;
13283
13284       /* Prevent broken recursion; we can't hand off to the same type.  */
13285       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13286
13287       TREE_ASM_WRITTEN (type) = 1;
13288       gen_decl_die (TYPE_NAME (type), context_die);
13289       return;
13290     }
13291
13292   /* If this is an array type with hidden descriptor, handle it first.  */
13293   if (!TREE_ASM_WRITTEN (type)
13294       && lang_hooks.types.get_array_descr_info
13295       && lang_hooks.types.get_array_descr_info (type, &info))
13296     {
13297       gen_descr_array_type_die (type, &info, context_die);
13298       TREE_ASM_WRITTEN (type) = 1;
13299       return;
13300     }
13301
13302   /* We are going to output a DIE to represent the unqualified version
13303      of this type (i.e. without any const or volatile qualifiers) so
13304      get the main variant (i.e. the unqualified version) of this type
13305      now.  (Vectors are special because the debugging info is in the
13306      cloned type itself).  */
13307   if (TREE_CODE (type) != VECTOR_TYPE)
13308     type = type_main_variant (type);
13309
13310   if (TREE_ASM_WRITTEN (type))
13311     return;
13312
13313   switch (TREE_CODE (type))
13314     {
13315     case ERROR_MARK:
13316       break;
13317
13318     case POINTER_TYPE:
13319     case REFERENCE_TYPE:
13320       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13321          ensures that the gen_type_die recursion will terminate even if the
13322          type is recursive.  Recursive types are possible in Ada.  */
13323       /* ??? We could perhaps do this for all types before the switch
13324          statement.  */
13325       TREE_ASM_WRITTEN (type) = 1;
13326
13327       /* For these types, all that is required is that we output a DIE (or a
13328          set of DIEs) to represent the "basis" type.  */
13329       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13330                                 DINFO_USAGE_IND_USE);
13331       break;
13332
13333     case OFFSET_TYPE:
13334       /* This code is used for C++ pointer-to-data-member types.
13335          Output a description of the relevant class type.  */
13336       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13337                                         DINFO_USAGE_IND_USE);
13338
13339       /* Output a description of the type of the object pointed to.  */
13340       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13341                                         DINFO_USAGE_IND_USE);
13342
13343       /* Now output a DIE to represent this pointer-to-data-member type
13344          itself.  */
13345       gen_ptr_to_mbr_type_die (type, context_die);
13346       break;
13347
13348     case FUNCTION_TYPE:
13349       /* Force out return type (in case it wasn't forced out already).  */
13350       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13351                                         DINFO_USAGE_DIR_USE);
13352       gen_subroutine_type_die (type, context_die);
13353       break;
13354
13355     case METHOD_TYPE:
13356       /* Force out return type (in case it wasn't forced out already).  */
13357       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13358                                         DINFO_USAGE_DIR_USE);
13359       gen_subroutine_type_die (type, context_die);
13360       break;
13361
13362     case ARRAY_TYPE:
13363       gen_array_type_die (type, context_die);
13364       break;
13365
13366     case VECTOR_TYPE:
13367       gen_array_type_die (type, context_die);
13368       break;
13369
13370     case ENUMERAL_TYPE:
13371     case RECORD_TYPE:
13372     case UNION_TYPE:
13373     case QUAL_UNION_TYPE:
13374       /* If this is a nested type whose containing class hasn't been written
13375          out yet, writing it out will cover this one, too.  This does not apply
13376          to instantiations of member class templates; they need to be added to
13377          the containing class as they are generated.  FIXME: This hurts the
13378          idea of combining type decls from multiple TUs, since we can't predict
13379          what set of template instantiations we'll get.  */
13380       if (TYPE_CONTEXT (type)
13381           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13382           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13383         {
13384           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13385
13386           if (TREE_ASM_WRITTEN (type))
13387             return;
13388
13389           /* If that failed, attach ourselves to the stub.  */
13390           push_decl_scope (TYPE_CONTEXT (type));
13391           context_die = lookup_type_die (TYPE_CONTEXT (type));
13392           need_pop = 1;
13393         }
13394       else
13395         {
13396           declare_in_namespace (type, context_die);
13397           need_pop = 0;
13398         }
13399
13400       if (TREE_CODE (type) == ENUMERAL_TYPE)
13401         {
13402           /* This might have been written out by the call to
13403              declare_in_namespace.  */
13404           if (!TREE_ASM_WRITTEN (type))
13405             gen_enumeration_type_die (type, context_die);
13406         }
13407       else
13408         gen_struct_or_union_type_die (type, context_die, usage);
13409
13410       if (need_pop)
13411         pop_decl_scope ();
13412
13413       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13414          it up if it is ever completed.  gen_*_type_die will set it for us
13415          when appropriate.  */
13416       return;
13417
13418     case VOID_TYPE:
13419     case INTEGER_TYPE:
13420     case REAL_TYPE:
13421     case FIXED_POINT_TYPE:
13422     case COMPLEX_TYPE:
13423     case BOOLEAN_TYPE:
13424       /* No DIEs needed for fundamental types.  */
13425       break;
13426
13427     case LANG_TYPE:
13428       /* No Dwarf representation currently defined.  */
13429       break;
13430
13431     default:
13432       gcc_unreachable ();
13433     }
13434
13435   TREE_ASM_WRITTEN (type) = 1;
13436 }
13437
13438 static void
13439 gen_type_die (tree type, dw_die_ref context_die)
13440 {
13441   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13442 }
13443
13444 /* Generate a DIE for a tagged type instantiation.  */
13445
13446 static void
13447 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13448 {
13449   if (type == NULL_TREE || type == error_mark_node)
13450     return;
13451
13452   /* We are going to output a DIE to represent the unqualified version of
13453      this type (i.e. without any const or volatile qualifiers) so make sure
13454      that we have the main variant (i.e. the unqualified version) of this
13455      type now.  */
13456   gcc_assert (type == type_main_variant (type));
13457
13458   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13459      an instance of an unresolved type.  */
13460
13461   switch (TREE_CODE (type))
13462     {
13463     case ERROR_MARK:
13464       break;
13465
13466     case ENUMERAL_TYPE:
13467       gen_inlined_enumeration_type_die (type, context_die);
13468       break;
13469
13470     case RECORD_TYPE:
13471       gen_inlined_structure_type_die (type, context_die);
13472       break;
13473
13474     case UNION_TYPE:
13475     case QUAL_UNION_TYPE:
13476       gen_inlined_union_type_die (type, context_die);
13477       break;
13478
13479     default:
13480       gcc_unreachable ();
13481     }
13482 }
13483
13484 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13485    things which are local to the given block.  */
13486
13487 static void
13488 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13489 {
13490   int must_output_die = 0;
13491   tree origin;
13492   tree decl;
13493   enum tree_code origin_code;
13494
13495   /* Ignore blocks that are NULL.  */
13496   if (stmt == NULL_TREE)
13497     return;
13498
13499   /* If the block is one fragment of a non-contiguous block, do not
13500      process the variables, since they will have been done by the
13501      origin block.  Do process subblocks.  */
13502   if (BLOCK_FRAGMENT_ORIGIN (stmt))
13503     {
13504       tree sub;
13505
13506       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13507         gen_block_die (sub, context_die, depth + 1);
13508
13509       return;
13510     }
13511
13512   /* Determine the "ultimate origin" of this block.  This block may be an
13513      inlined instance of an inlined instance of inline function, so we have
13514      to trace all of the way back through the origin chain to find out what
13515      sort of node actually served as the original seed for the creation of
13516      the current block.  */
13517   origin = block_ultimate_origin (stmt);
13518   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13519
13520   /* Determine if we need to output any Dwarf DIEs at all to represent this
13521      block.  */
13522   if (origin_code == FUNCTION_DECL)
13523     /* The outer scopes for inlinings *must* always be represented.  We
13524        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13525     must_output_die = 1;
13526   else
13527     {
13528       /* In the case where the current block represents an inlining of the
13529          "body block" of an inline function, we must *NOT* output any DIE for
13530          this block because we have already output a DIE to represent the whole
13531          inlined function scope and the "body block" of any function doesn't
13532          really represent a different scope according to ANSI C rules.  So we
13533          check here to make sure that this block does not represent a "body
13534          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13535       if (! is_body_block (origin ? origin : stmt))
13536         {
13537           /* Determine if this block directly contains any "significant"
13538              local declarations which we will need to output DIEs for.  */
13539           if (debug_info_level > DINFO_LEVEL_TERSE)
13540             /* We are not in terse mode so *any* local declaration counts
13541                as being a "significant" one.  */
13542             must_output_die = (BLOCK_VARS (stmt) != NULL
13543                                && (TREE_USED (stmt)
13544                                    || TREE_ASM_WRITTEN (stmt)
13545                                    || BLOCK_ABSTRACT (stmt)));
13546           else
13547             /* We are in terse mode, so only local (nested) function
13548                definitions count as "significant" local declarations.  */
13549             for (decl = BLOCK_VARS (stmt);
13550                  decl != NULL; decl = TREE_CHAIN (decl))
13551               if (TREE_CODE (decl) == FUNCTION_DECL
13552                   && DECL_INITIAL (decl))
13553                 {
13554                   must_output_die = 1;
13555                   break;
13556                 }
13557         }
13558     }
13559
13560   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13561      DIE for any block which contains no significant local declarations at
13562      all.  Rather, in such cases we just call `decls_for_scope' so that any
13563      needed Dwarf info for any sub-blocks will get properly generated. Note
13564      that in terse mode, our definition of what constitutes a "significant"
13565      local declaration gets restricted to include only inlined function
13566      instances and local (nested) function definitions.  */
13567   if (must_output_die)
13568     {
13569       if (origin_code == FUNCTION_DECL)
13570         gen_inlined_subroutine_die (stmt, context_die, depth);
13571       else
13572         gen_lexical_block_die (stmt, context_die, depth);
13573     }
13574   else
13575     decls_for_scope (stmt, context_die, depth);
13576 }
13577
13578 /* Generate all of the decls declared within a given scope and (recursively)
13579    all of its sub-blocks.  */
13580
13581 static void
13582 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13583 {
13584   tree decl;
13585   tree subblocks;
13586
13587   /* Ignore NULL blocks.  */
13588   if (stmt == NULL_TREE)
13589     return;
13590
13591   if (TREE_USED (stmt))
13592     {
13593       /* Output the DIEs to represent all of the data objects and typedefs
13594          declared directly within this block but not within any nested
13595          sub-blocks.  Also, nested function and tag DIEs have been
13596          generated with a parent of NULL; fix that up now.  */
13597       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13598         {
13599           dw_die_ref die;
13600
13601           if (TREE_CODE (decl) == FUNCTION_DECL)
13602             die = lookup_decl_die (decl);
13603           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13604             die = lookup_type_die (TREE_TYPE (decl));
13605           else
13606             die = NULL;
13607
13608           if (die != NULL && die->die_parent == NULL)
13609             add_child_die (context_die, die);
13610           /* Do not produce debug information for static variables since
13611              these might be optimized out.  We are called for these later
13612              in varpool_analyze_pending_decls. */
13613           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13614             ;
13615           else
13616             gen_decl_die (decl, context_die);
13617         }
13618     }
13619
13620   /* If we're at -g1, we're not interested in subblocks.  */
13621   if (debug_info_level <= DINFO_LEVEL_TERSE)
13622     return;
13623
13624   /* Output the DIEs to represent all sub-blocks (and the items declared
13625      therein) of this block.  */
13626   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13627        subblocks != NULL;
13628        subblocks = BLOCK_CHAIN (subblocks))
13629     gen_block_die (subblocks, context_die, depth + 1);
13630 }
13631
13632 /* Is this a typedef we can avoid emitting?  */
13633
13634 static inline int
13635 is_redundant_typedef (const_tree decl)
13636 {
13637   if (TYPE_DECL_IS_STUB (decl))
13638     return 1;
13639
13640   if (DECL_ARTIFICIAL (decl)
13641       && DECL_CONTEXT (decl)
13642       && is_tagged_type (DECL_CONTEXT (decl))
13643       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13644       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13645     /* Also ignore the artificial member typedef for the class name.  */
13646     return 1;
13647
13648   return 0;
13649 }
13650
13651 /* Returns the DIE for decl.  A DIE will always be returned.  */
13652
13653 static dw_die_ref
13654 force_decl_die (tree decl)
13655 {
13656   dw_die_ref decl_die;
13657   unsigned saved_external_flag;
13658   tree save_fn = NULL_TREE;
13659   decl_die = lookup_decl_die (decl);
13660   if (!decl_die)
13661     {
13662       dw_die_ref context_die;
13663       tree decl_context = DECL_CONTEXT (decl);
13664       if (decl_context)
13665         {
13666           /* Find die that represents this context.  */
13667           if (TYPE_P (decl_context))
13668             context_die = force_type_die (decl_context);
13669           else
13670             context_die = force_decl_die (decl_context);
13671         }
13672       else
13673         context_die = comp_unit_die;
13674
13675       decl_die = lookup_decl_die (decl);
13676       if (decl_die)
13677         return decl_die;
13678
13679       switch (TREE_CODE (decl))
13680         {
13681         case FUNCTION_DECL:
13682           /* Clear current_function_decl, so that gen_subprogram_die thinks
13683              that this is a declaration. At this point, we just want to force
13684              declaration die.  */
13685           save_fn = current_function_decl;
13686           current_function_decl = NULL_TREE;
13687           gen_subprogram_die (decl, context_die);
13688           current_function_decl = save_fn;
13689           break;
13690
13691         case VAR_DECL:
13692           /* Set external flag to force declaration die. Restore it after
13693            gen_decl_die() call.  */
13694           saved_external_flag = DECL_EXTERNAL (decl);
13695           DECL_EXTERNAL (decl) = 1;
13696           gen_decl_die (decl, context_die);
13697           DECL_EXTERNAL (decl) = saved_external_flag;
13698           break;
13699
13700         case NAMESPACE_DECL:
13701           dwarf2out_decl (decl);
13702           break;
13703
13704         default:
13705           gcc_unreachable ();
13706         }
13707
13708       /* We should be able to find the DIE now.  */
13709       if (!decl_die)
13710         decl_die = lookup_decl_die (decl);
13711       gcc_assert (decl_die);
13712     }
13713
13714   return decl_die;
13715 }
13716
13717 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13718    always returned.  */
13719
13720 static dw_die_ref
13721 force_type_die (tree type)
13722 {
13723   dw_die_ref type_die;
13724
13725   type_die = lookup_type_die (type);
13726   if (!type_die)
13727     {
13728       dw_die_ref context_die;
13729       if (TYPE_CONTEXT (type))
13730         {
13731           if (TYPE_P (TYPE_CONTEXT (type)))
13732             context_die = force_type_die (TYPE_CONTEXT (type));
13733           else
13734             context_die = force_decl_die (TYPE_CONTEXT (type));
13735         }
13736       else
13737         context_die = comp_unit_die;
13738
13739       type_die = modified_type_die (type, TYPE_READONLY (type),
13740                                     TYPE_VOLATILE (type), context_die);
13741       gcc_assert (type_die);
13742     }
13743   return type_die;
13744 }
13745
13746 /* Force out any required namespaces to be able to output DECL,
13747    and return the new context_die for it, if it's changed.  */
13748
13749 static dw_die_ref
13750 setup_namespace_context (tree thing, dw_die_ref context_die)
13751 {
13752   tree context = (DECL_P (thing)
13753                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13754   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13755     /* Force out the namespace.  */
13756     context_die = force_decl_die (context);
13757
13758   return context_die;
13759 }
13760
13761 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13762    type) within its namespace, if appropriate.
13763
13764    For compatibility with older debuggers, namespace DIEs only contain
13765    declarations; all definitions are emitted at CU scope.  */
13766
13767 static void
13768 declare_in_namespace (tree thing, dw_die_ref context_die)
13769 {
13770   dw_die_ref ns_context;
13771
13772   if (debug_info_level <= DINFO_LEVEL_TERSE)
13773     return;
13774
13775   /* If this decl is from an inlined function, then don't try to emit it in its
13776      namespace, as we will get confused.  It would have already been emitted
13777      when the abstract instance of the inline function was emitted anyways.  */
13778   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13779     return;
13780
13781   ns_context = setup_namespace_context (thing, context_die);
13782
13783   if (ns_context != context_die)
13784     {
13785       if (DECL_P (thing))
13786         gen_decl_die (thing, ns_context);
13787       else
13788         gen_type_die (thing, ns_context);
13789     }
13790 }
13791
13792 /* Generate a DIE for a namespace or namespace alias.  */
13793
13794 static void
13795 gen_namespace_die (tree decl)
13796 {
13797   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13798
13799   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13800      they are an alias of.  */
13801   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13802     {
13803       /* Output a real namespace.  */
13804       dw_die_ref namespace_die
13805         = new_die (DW_TAG_namespace, context_die, decl);
13806       add_name_and_src_coords_attributes (namespace_die, decl);
13807       equate_decl_number_to_die (decl, namespace_die);
13808     }
13809   else
13810     {
13811       /* Output a namespace alias.  */
13812
13813       /* Force out the namespace we are an alias of, if necessary.  */
13814       dw_die_ref origin_die
13815         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13816
13817       /* Now create the namespace alias DIE.  */
13818       dw_die_ref namespace_die
13819         = new_die (DW_TAG_imported_declaration, context_die, decl);
13820       add_name_and_src_coords_attributes (namespace_die, decl);
13821       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13822       equate_decl_number_to_die (decl, namespace_die);
13823     }
13824 }
13825
13826 /* Generate Dwarf debug information for a decl described by DECL.  */
13827
13828 static void
13829 gen_decl_die (tree decl, dw_die_ref context_die)
13830 {
13831   tree origin;
13832
13833   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13834     return;
13835
13836   switch (TREE_CODE (decl))
13837     {
13838     case ERROR_MARK:
13839       break;
13840
13841     case CONST_DECL:
13842       /* The individual enumerators of an enum type get output when we output
13843          the Dwarf representation of the relevant enum type itself.  */
13844       break;
13845
13846     case FUNCTION_DECL:
13847       /* Don't output any DIEs to represent mere function declarations,
13848          unless they are class members or explicit block externs.  */
13849       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13850           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13851         break;
13852
13853 #if 0
13854       /* FIXME */
13855       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13856          on local redeclarations of global functions.  That seems broken.  */
13857       if (current_function_decl != decl)
13858         /* This is only a declaration.  */;
13859 #endif
13860
13861       /* If we're emitting a clone, emit info for the abstract instance.  */
13862       if (DECL_ORIGIN (decl) != decl)
13863         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13864
13865       /* If we're emitting an out-of-line copy of an inline function,
13866          emit info for the abstract instance and set up to refer to it.  */
13867       else if (cgraph_function_possibly_inlined_p (decl)
13868                && ! DECL_ABSTRACT (decl)
13869                && ! class_or_namespace_scope_p (context_die)
13870                /* dwarf2out_abstract_function won't emit a die if this is just
13871                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13872                   that case, because that works only if we have a die.  */
13873                && DECL_INITIAL (decl) != NULL_TREE)
13874         {
13875           dwarf2out_abstract_function (decl);
13876           set_decl_origin_self (decl);
13877         }
13878
13879       /* Otherwise we're emitting the primary DIE for this decl.  */
13880       else if (debug_info_level > DINFO_LEVEL_TERSE)
13881         {
13882           /* Before we describe the FUNCTION_DECL itself, make sure that we
13883              have described its return type.  */
13884           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13885
13886           /* And its virtual context.  */
13887           if (DECL_VINDEX (decl) != NULL_TREE)
13888             gen_type_die (DECL_CONTEXT (decl), context_die);
13889
13890           /* And its containing type.  */
13891           origin = decl_class_context (decl);
13892           if (origin != NULL_TREE)
13893             gen_type_die_for_member (origin, decl, context_die);
13894
13895           /* And its containing namespace.  */
13896           declare_in_namespace (decl, context_die);
13897         }
13898
13899       /* Now output a DIE to represent the function itself.  */
13900       gen_subprogram_die (decl, context_die);
13901       break;
13902
13903     case TYPE_DECL:
13904       /* If we are in terse mode, don't generate any DIEs to represent any
13905          actual typedefs.  */
13906       if (debug_info_level <= DINFO_LEVEL_TERSE)
13907         break;
13908
13909       /* In the special case of a TYPE_DECL node representing the declaration
13910          of some type tag, if the given TYPE_DECL is marked as having been
13911          instantiated from some other (original) TYPE_DECL node (e.g. one which
13912          was generated within the original definition of an inline function) we
13913          have to generate a special (abbreviated) DW_TAG_structure_type,
13914          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13915       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13916           && is_tagged_type (TREE_TYPE (decl)))
13917         {
13918           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13919           break;
13920         }
13921
13922       if (is_redundant_typedef (decl))
13923         gen_type_die (TREE_TYPE (decl), context_die);
13924       else
13925         /* Output a DIE to represent the typedef itself.  */
13926         gen_typedef_die (decl, context_die);
13927       break;
13928
13929     case LABEL_DECL:
13930       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13931         gen_label_die (decl, context_die);
13932       break;
13933
13934     case VAR_DECL:
13935     case RESULT_DECL:
13936       /* If we are in terse mode, don't generate any DIEs to represent any
13937          variable declarations or definitions.  */
13938       if (debug_info_level <= DINFO_LEVEL_TERSE)
13939         break;
13940
13941       /* Output any DIEs that are needed to specify the type of this data
13942          object.  */
13943       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
13944         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13945       else
13946         gen_type_die (TREE_TYPE (decl), context_die);
13947
13948       /* And its containing type.  */
13949       origin = decl_class_context (decl);
13950       if (origin != NULL_TREE)
13951         gen_type_die_for_member (origin, decl, context_die);
13952
13953       /* And its containing namespace.  */
13954       declare_in_namespace (decl, context_die);
13955
13956       /* Now output the DIE to represent the data object itself.  This gets
13957          complicated because of the possibility that the VAR_DECL really
13958          represents an inlined instance of a formal parameter for an inline
13959          function.  */
13960       origin = decl_ultimate_origin (decl);
13961       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13962         gen_formal_parameter_die (decl, context_die);
13963       else
13964         gen_variable_die (decl, context_die);
13965       break;
13966
13967     case FIELD_DECL:
13968       /* Ignore the nameless fields that are used to skip bits but handle C++
13969          anonymous unions and structs.  */
13970       if (DECL_NAME (decl) != NULL_TREE
13971           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13972           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13973         {
13974           gen_type_die (member_declared_type (decl), context_die);
13975           gen_field_die (decl, context_die);
13976         }
13977       break;
13978
13979     case PARM_DECL:
13980       if (DECL_BY_REFERENCE (decl))
13981         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13982       else
13983         gen_type_die (TREE_TYPE (decl), context_die);
13984       gen_formal_parameter_die (decl, context_die);
13985       break;
13986
13987     case NAMESPACE_DECL:
13988       gen_namespace_die (decl);
13989       break;
13990
13991     default:
13992       /* Probably some frontend-internal decl.  Assume we don't care.  */
13993       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13994       break;
13995     }
13996 }
13997 \f
13998 /* Output debug information for global decl DECL.  Called from toplev.c after
13999    compilation proper has finished.  */
14000
14001 static void
14002 dwarf2out_global_decl (tree decl)
14003 {
14004   /* Output DWARF2 information for file-scope tentative data object
14005      declarations, file-scope (extern) function declarations (which had no
14006      corresponding body) and file-scope tagged type declarations and
14007      definitions which have not yet been forced out.  */
14008   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14009     dwarf2out_decl (decl);
14010 }
14011
14012 /* Output debug information for type decl DECL.  Called from toplev.c
14013    and from language front ends (to record built-in types).  */
14014 static void
14015 dwarf2out_type_decl (tree decl, int local)
14016 {
14017   if (!local)
14018     dwarf2out_decl (decl);
14019 }
14020
14021 /* Output debug information for imported module or decl.  */
14022
14023 static void
14024 dwarf2out_imported_module_or_decl (tree decl, tree context)
14025 {
14026   dw_die_ref imported_die, at_import_die;
14027   dw_die_ref scope_die;
14028   expanded_location xloc;
14029
14030   if (debug_info_level <= DINFO_LEVEL_TERSE)
14031     return;
14032
14033   gcc_assert (decl);
14034
14035   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14036      We need decl DIE for reference and scope die. First, get DIE for the decl
14037      itself.  */
14038
14039   /* Get the scope die for decl context. Use comp_unit_die for global module
14040      or decl. If die is not found for non globals, force new die.  */
14041   if (!context)
14042     scope_die = comp_unit_die;
14043   else if (TYPE_P (context))
14044     {
14045       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14046         return;
14047     scope_die = force_type_die (context);
14048     }
14049   else
14050     scope_die = force_decl_die (context);
14051
14052   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14053   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14054     {
14055       if (is_base_type (TREE_TYPE (decl)))
14056         at_import_die = base_type_die (TREE_TYPE (decl));
14057       else
14058         at_import_die = force_type_die (TREE_TYPE (decl));
14059     }
14060   else
14061     {
14062       at_import_die = lookup_decl_die (decl);
14063       if (!at_import_die)
14064         {
14065           /* If we're trying to avoid duplicate debug info, we may not have
14066              emitted the member decl for this field.  Emit it now.  */
14067           if (TREE_CODE (decl) == FIELD_DECL)
14068             {
14069               tree type = DECL_CONTEXT (decl);
14070               dw_die_ref type_context_die;
14071
14072               if (TYPE_CONTEXT (type))
14073                 if (TYPE_P (TYPE_CONTEXT (type)))
14074                   {
14075                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14076                                                    DINFO_USAGE_DIR_USE))
14077                       return;
14078                   type_context_die = force_type_die (TYPE_CONTEXT (type));
14079                   }
14080               else
14081                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14082               else
14083                 type_context_die = comp_unit_die;
14084               gen_type_die_for_member (type, decl, type_context_die);
14085             }
14086           at_import_die = force_decl_die (decl);
14087         }
14088     }
14089
14090   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14091   if (TREE_CODE (decl) == NAMESPACE_DECL)
14092     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14093   else
14094     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14095
14096   xloc = expand_location (input_location);
14097   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14098   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14099   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14100 }
14101
14102 /* Write the debugging output for DECL.  */
14103
14104 void
14105 dwarf2out_decl (tree decl)
14106 {
14107   dw_die_ref context_die = comp_unit_die;
14108
14109   switch (TREE_CODE (decl))
14110     {
14111     case ERROR_MARK:
14112       return;
14113
14114     case FUNCTION_DECL:
14115       /* What we would really like to do here is to filter out all mere
14116          file-scope declarations of file-scope functions which are never
14117          referenced later within this translation unit (and keep all of ones
14118          that *are* referenced later on) but we aren't clairvoyant, so we have
14119          no idea which functions will be referenced in the future (i.e. later
14120          on within the current translation unit). So here we just ignore all
14121          file-scope function declarations which are not also definitions.  If
14122          and when the debugger needs to know something about these functions,
14123          it will have to hunt around and find the DWARF information associated
14124          with the definition of the function.
14125
14126          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14127          nodes represent definitions and which ones represent mere
14128          declarations.  We have to check DECL_INITIAL instead. That's because
14129          the C front-end supports some weird semantics for "extern inline"
14130          function definitions.  These can get inlined within the current
14131          translation unit (and thus, we need to generate Dwarf info for their
14132          abstract instances so that the Dwarf info for the concrete inlined
14133          instances can have something to refer to) but the compiler never
14134          generates any out-of-lines instances of such things (despite the fact
14135          that they *are* definitions).
14136
14137          The important point is that the C front-end marks these "extern
14138          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14139          them anyway. Note that the C++ front-end also plays some similar games
14140          for inline function definitions appearing within include files which
14141          also contain `#pragma interface' pragmas.  */
14142       if (DECL_INITIAL (decl) == NULL_TREE)
14143         return;
14144
14145       /* If we're a nested function, initially use a parent of NULL; if we're
14146          a plain function, this will be fixed up in decls_for_scope.  If
14147          we're a method, it will be ignored, since we already have a DIE.  */
14148       if (decl_function_context (decl)
14149           /* But if we're in terse mode, we don't care about scope.  */
14150           && debug_info_level > DINFO_LEVEL_TERSE)
14151         context_die = NULL;
14152       break;
14153
14154     case VAR_DECL:
14155       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14156          declaration and if the declaration was never even referenced from
14157          within this entire compilation unit.  We suppress these DIEs in
14158          order to save space in the .debug section (by eliminating entries
14159          which are probably useless).  Note that we must not suppress
14160          block-local extern declarations (whether used or not) because that
14161          would screw-up the debugger's name lookup mechanism and cause it to
14162          miss things which really ought to be in scope at a given point.  */
14163       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14164         return;
14165
14166       /* For local statics lookup proper context die.  */
14167       if (TREE_STATIC (decl) && decl_function_context (decl))
14168         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14169
14170       /* If we are in terse mode, don't generate any DIEs to represent any
14171          variable declarations or definitions.  */
14172       if (debug_info_level <= DINFO_LEVEL_TERSE)
14173         return;
14174       break;
14175
14176     case NAMESPACE_DECL:
14177       if (debug_info_level <= DINFO_LEVEL_TERSE)
14178         return;
14179       if (lookup_decl_die (decl) != NULL)
14180         return;
14181       break;
14182
14183     case TYPE_DECL:
14184       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14185       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14186         return;
14187
14188       /* Don't bother trying to generate any DIEs to represent any of the
14189          normal built-in types for the language we are compiling.  */
14190       if (DECL_IS_BUILTIN (decl))
14191         {
14192           /* OK, we need to generate one for `bool' so GDB knows what type
14193              comparisons have.  */
14194           if (is_cxx ()
14195               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14196               && ! DECL_IGNORED_P (decl))
14197             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14198
14199           return;
14200         }
14201
14202       /* If we are in terse mode, don't generate any DIEs for types.  */
14203       if (debug_info_level <= DINFO_LEVEL_TERSE)
14204         return;
14205
14206       /* If we're a function-scope tag, initially use a parent of NULL;
14207          this will be fixed up in decls_for_scope.  */
14208       if (decl_function_context (decl))
14209         context_die = NULL;
14210
14211       break;
14212
14213     default:
14214       return;
14215     }
14216
14217   gen_decl_die (decl, context_die);
14218 }
14219
14220 /* Output a marker (i.e. a label) for the beginning of the generated code for
14221    a lexical block.  */
14222
14223 static void
14224 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14225                        unsigned int blocknum)
14226 {
14227   switch_to_section (current_function_section ());
14228   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14229 }
14230
14231 /* Output a marker (i.e. a label) for the end of the generated code for a
14232    lexical block.  */
14233
14234 static void
14235 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14236 {
14237   switch_to_section (current_function_section ());
14238   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14239 }
14240
14241 /* Returns nonzero if it is appropriate not to emit any debugging
14242    information for BLOCK, because it doesn't contain any instructions.
14243
14244    Don't allow this for blocks with nested functions or local classes
14245    as we would end up with orphans, and in the presence of scheduling
14246    we may end up calling them anyway.  */
14247
14248 static bool
14249 dwarf2out_ignore_block (const_tree block)
14250 {
14251   tree decl;
14252
14253   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14254     if (TREE_CODE (decl) == FUNCTION_DECL
14255         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14256       return 0;
14257
14258   return 1;
14259 }
14260
14261 /* Hash table routines for file_hash.  */
14262
14263 static int
14264 file_table_eq (const void *p1_p, const void *p2_p)
14265 {
14266   const struct dwarf_file_data * p1 = p1_p;
14267   const char * p2 = p2_p;
14268   return strcmp (p1->filename, p2) == 0;
14269 }
14270
14271 static hashval_t
14272 file_table_hash (const void *p_p)
14273 {
14274   const struct dwarf_file_data * p = p_p;
14275   return htab_hash_string (p->filename);
14276 }
14277
14278 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14279    dwarf2out.c) and return its "index".  The index of each (known) filename is
14280    just a unique number which is associated with only that one filename.  We
14281    need such numbers for the sake of generating labels (in the .debug_sfnames
14282    section) and references to those files numbers (in the .debug_srcinfo
14283    and.debug_macinfo sections).  If the filename given as an argument is not
14284    found in our current list, add it to the list and assign it the next
14285    available unique index number.  In order to speed up searches, we remember
14286    the index of the filename was looked up last.  This handles the majority of
14287    all searches.  */
14288
14289 static struct dwarf_file_data *
14290 lookup_filename (const char *file_name)
14291 {
14292   void ** slot;
14293   struct dwarf_file_data * created;
14294
14295   /* Check to see if the file name that was searched on the previous
14296      call matches this file name.  If so, return the index.  */
14297   if (file_table_last_lookup
14298       && (file_name == file_table_last_lookup->filename
14299           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14300     return file_table_last_lookup;
14301
14302   /* Didn't match the previous lookup, search the table.  */
14303   slot = htab_find_slot_with_hash (file_table, file_name,
14304                                    htab_hash_string (file_name), INSERT);
14305   if (*slot)
14306     return *slot;
14307
14308   created = ggc_alloc (sizeof (struct dwarf_file_data));
14309   created->filename = file_name;
14310   created->emitted_number = 0;
14311   *slot = created;
14312   return created;
14313 }
14314
14315 /* If the assembler will construct the file table, then translate the compiler
14316    internal file table number into the assembler file table number, and emit
14317    a .file directive if we haven't already emitted one yet.  The file table
14318    numbers are different because we prune debug info for unused variables and
14319    types, which may include filenames.  */
14320
14321 static int
14322 maybe_emit_file (struct dwarf_file_data * fd)
14323 {
14324   if (! fd->emitted_number)
14325     {
14326       if (last_emitted_file)
14327         fd->emitted_number = last_emitted_file->emitted_number + 1;
14328       else
14329         fd->emitted_number = 1;
14330       last_emitted_file = fd;
14331
14332       if (DWARF2_ASM_LINE_DEBUG_INFO)
14333         {
14334           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14335           output_quoted_string (asm_out_file,
14336                                 remap_debug_filename (fd->filename));
14337           fputc ('\n', asm_out_file);
14338         }
14339     }
14340
14341   return fd->emitted_number;
14342 }
14343
14344 /* Called by the final INSN scan whenever we see a var location.  We
14345    use it to drop labels in the right places, and throw the location in
14346    our lookup table.  */
14347
14348 static void
14349 dwarf2out_var_location (rtx loc_note)
14350 {
14351   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14352   struct var_loc_node *newloc;
14353   rtx prev_insn;
14354   static rtx last_insn;
14355   static const char *last_label;
14356   tree decl;
14357
14358   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14359     return;
14360   prev_insn = PREV_INSN (loc_note);
14361
14362   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14363   /* If the insn we processed last time is the previous insn
14364      and it is also a var location note, use the label we emitted
14365      last time.  */
14366   if (last_insn != NULL_RTX
14367       && last_insn == prev_insn
14368       && NOTE_P (prev_insn)
14369       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14370     {
14371       newloc->label = last_label;
14372     }
14373   else
14374     {
14375       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14376       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14377       loclabel_num++;
14378       newloc->label = ggc_strdup (loclabel);
14379     }
14380   newloc->var_loc_note = loc_note;
14381   newloc->next = NULL;
14382
14383   if (cfun && in_cold_section_p)
14384     newloc->section_label = cfun->cold_section_label;
14385   else
14386     newloc->section_label = text_section_label;
14387
14388   last_insn = loc_note;
14389   last_label = newloc->label;
14390   decl = NOTE_VAR_LOCATION_DECL (loc_note);
14391   add_var_loc_to_decl (decl, newloc);
14392 }
14393
14394 /* We need to reset the locations at the beginning of each
14395    function. We can't do this in the end_function hook, because the
14396    declarations that use the locations won't have been output when
14397    that hook is called.  Also compute have_multiple_function_sections here.  */
14398
14399 static void
14400 dwarf2out_begin_function (tree fun)
14401 {
14402   htab_empty (decl_loc_table);
14403
14404   if (function_section (fun) != text_section)
14405     have_multiple_function_sections = true;
14406
14407   dwarf2out_note_section_used ();
14408 }
14409
14410 /* Output a label to mark the beginning of a source code line entry
14411    and record information relating to this source line, in
14412    'line_info_table' for later output of the .debug_line section.  */
14413
14414 static void
14415 dwarf2out_source_line (unsigned int line, const char *filename)
14416 {
14417   if (debug_info_level >= DINFO_LEVEL_NORMAL
14418       && line != 0)
14419     {
14420       int file_num = maybe_emit_file (lookup_filename (filename));
14421
14422       switch_to_section (current_function_section ());
14423
14424       /* If requested, emit something human-readable.  */
14425       if (flag_debug_asm)
14426         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14427                  filename, line);
14428
14429       if (DWARF2_ASM_LINE_DEBUG_INFO)
14430         {
14431           /* Emit the .loc directive understood by GNU as.  */
14432           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14433
14434           /* Indicate that line number info exists.  */
14435           line_info_table_in_use++;
14436         }
14437       else if (function_section (current_function_decl) != text_section)
14438         {
14439           dw_separate_line_info_ref line_info;
14440           targetm.asm_out.internal_label (asm_out_file,
14441                                           SEPARATE_LINE_CODE_LABEL,
14442                                           separate_line_info_table_in_use);
14443
14444           /* Expand the line info table if necessary.  */
14445           if (separate_line_info_table_in_use
14446               == separate_line_info_table_allocated)
14447             {
14448               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14449               separate_line_info_table
14450                 = ggc_realloc (separate_line_info_table,
14451                                separate_line_info_table_allocated
14452                                * sizeof (dw_separate_line_info_entry));
14453               memset (separate_line_info_table
14454                        + separate_line_info_table_in_use,
14455                       0,
14456                       (LINE_INFO_TABLE_INCREMENT
14457                        * sizeof (dw_separate_line_info_entry)));
14458             }
14459
14460           /* Add the new entry at the end of the line_info_table.  */
14461           line_info
14462             = &separate_line_info_table[separate_line_info_table_in_use++];
14463           line_info->dw_file_num = file_num;
14464           line_info->dw_line_num = line;
14465           line_info->function = current_function_funcdef_no;
14466         }
14467       else
14468         {
14469           dw_line_info_ref line_info;
14470
14471           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14472                                      line_info_table_in_use);
14473
14474           /* Expand the line info table if necessary.  */
14475           if (line_info_table_in_use == line_info_table_allocated)
14476             {
14477               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14478               line_info_table
14479                 = ggc_realloc (line_info_table,
14480                                (line_info_table_allocated
14481                                 * sizeof (dw_line_info_entry)));
14482               memset (line_info_table + line_info_table_in_use, 0,
14483                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14484             }
14485
14486           /* Add the new entry at the end of the line_info_table.  */
14487           line_info = &line_info_table[line_info_table_in_use++];
14488           line_info->dw_file_num = file_num;
14489           line_info->dw_line_num = line;
14490         }
14491     }
14492 }
14493
14494 /* Record the beginning of a new source file.  */
14495
14496 static void
14497 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14498 {
14499   if (flag_eliminate_dwarf2_dups)
14500     {
14501       /* Record the beginning of the file for break_out_includes.  */
14502       dw_die_ref bincl_die;
14503
14504       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14505       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14506     }
14507
14508   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14509     {
14510       int file_num = maybe_emit_file (lookup_filename (filename));
14511
14512       switch_to_section (debug_macinfo_section);
14513       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14514       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14515                                    lineno);
14516
14517       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14518     }
14519 }
14520
14521 /* Record the end of a source file.  */
14522
14523 static void
14524 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14525 {
14526   if (flag_eliminate_dwarf2_dups)
14527     /* Record the end of the file for break_out_includes.  */
14528     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14529
14530   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14531     {
14532       switch_to_section (debug_macinfo_section);
14533       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14534     }
14535 }
14536
14537 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14538    the tail part of the directive line, i.e. the part which is past the
14539    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14540
14541 static void
14542 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14543                   const char *buffer ATTRIBUTE_UNUSED)
14544 {
14545   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14546     {
14547       switch_to_section (debug_macinfo_section);
14548       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14549       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14550       dw2_asm_output_nstring (buffer, -1, "The macro");
14551     }
14552 }
14553
14554 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14555    the tail part of the directive line, i.e. the part which is past the
14556    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14557
14558 static void
14559 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14560                  const char *buffer ATTRIBUTE_UNUSED)
14561 {
14562   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14563     {
14564       switch_to_section (debug_macinfo_section);
14565       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14566       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14567       dw2_asm_output_nstring (buffer, -1, "The macro");
14568     }
14569 }
14570
14571 /* Set up for Dwarf output at the start of compilation.  */
14572
14573 static void
14574 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14575 {
14576   /* Allocate the file_table.  */
14577   file_table = htab_create_ggc (50, file_table_hash,
14578                                 file_table_eq, NULL);
14579
14580   /* Allocate the decl_die_table.  */
14581   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14582                                     decl_die_table_eq, NULL);
14583
14584   /* Allocate the decl_loc_table.  */
14585   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14586                                     decl_loc_table_eq, NULL);
14587
14588   /* Allocate the initial hunk of the decl_scope_table.  */
14589   decl_scope_table = VEC_alloc (tree, gc, 256);
14590
14591   /* Allocate the initial hunk of the abbrev_die_table.  */
14592   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14593                                         * sizeof (dw_die_ref));
14594   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14595   /* Zero-th entry is allocated, but unused.  */
14596   abbrev_die_table_in_use = 1;
14597
14598   /* Allocate the initial hunk of the line_info_table.  */
14599   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14600                                        * sizeof (dw_line_info_entry));
14601   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14602
14603   /* Zero-th entry is allocated, but unused.  */
14604   line_info_table_in_use = 1;
14605
14606   /* Allocate the pubtypes and pubnames vectors.  */
14607   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14608   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14609
14610   /* Generate the initial DIE for the .debug section.  Note that the (string)
14611      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14612      will (typically) be a relative pathname and that this pathname should be
14613      taken as being relative to the directory from which the compiler was
14614      invoked when the given (base) source file was compiled.  We will fill
14615      in this value in dwarf2out_finish.  */
14616   comp_unit_die = gen_compile_unit_die (NULL);
14617
14618   incomplete_types = VEC_alloc (tree, gc, 64);
14619
14620   used_rtx_array = VEC_alloc (rtx, gc, 32);
14621
14622   debug_info_section = get_section (DEBUG_INFO_SECTION,
14623                                     SECTION_DEBUG, NULL);
14624   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14625                                       SECTION_DEBUG, NULL);
14626   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14627                                        SECTION_DEBUG, NULL);
14628   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14629                                        SECTION_DEBUG, NULL);
14630   debug_line_section = get_section (DEBUG_LINE_SECTION,
14631                                     SECTION_DEBUG, NULL);
14632   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14633                                    SECTION_DEBUG, NULL);
14634   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14635                                         SECTION_DEBUG, NULL);
14636 #ifdef DEBUG_PUBTYPES_SECTION
14637   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14638                                         SECTION_DEBUG, NULL);
14639 #endif
14640   debug_str_section = get_section (DEBUG_STR_SECTION,
14641                                    DEBUG_STR_SECTION_FLAGS, NULL);
14642   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14643                                       SECTION_DEBUG, NULL);
14644   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14645                                      SECTION_DEBUG, NULL);
14646
14647   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14648   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14649                                DEBUG_ABBREV_SECTION_LABEL, 0);
14650   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14651   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14652                                COLD_TEXT_SECTION_LABEL, 0);
14653   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14654
14655   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14656                                DEBUG_INFO_SECTION_LABEL, 0);
14657   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14658                                DEBUG_LINE_SECTION_LABEL, 0);
14659   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14660                                DEBUG_RANGES_SECTION_LABEL, 0);
14661   switch_to_section (debug_abbrev_section);
14662   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14663   switch_to_section (debug_info_section);
14664   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14665   switch_to_section (debug_line_section);
14666   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14667
14668   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14669     {
14670       switch_to_section (debug_macinfo_section);
14671       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14672                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14673       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14674     }
14675
14676   switch_to_section (text_section);
14677   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14678   if (flag_reorder_blocks_and_partition)
14679     {
14680       cold_text_section = unlikely_text_section ();
14681       switch_to_section (cold_text_section);
14682       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14683     }
14684 }
14685
14686 /* A helper function for dwarf2out_finish called through
14687    ht_forall.  Emit one queued .debug_str string.  */
14688
14689 static int
14690 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14691 {
14692   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14693
14694   if (node->form == DW_FORM_strp)
14695     {
14696       switch_to_section (debug_str_section);
14697       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14698       assemble_string (node->str, strlen (node->str) + 1);
14699     }
14700
14701   return 1;
14702 }
14703
14704 #if ENABLE_ASSERT_CHECKING
14705 /* Verify that all marks are clear.  */
14706
14707 static void
14708 verify_marks_clear (dw_die_ref die)
14709 {
14710   dw_die_ref c;
14711
14712   gcc_assert (! die->die_mark);
14713   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14714 }
14715 #endif /* ENABLE_ASSERT_CHECKING */
14716
14717 /* Clear the marks for a die and its children.
14718    Be cool if the mark isn't set.  */
14719
14720 static void
14721 prune_unmark_dies (dw_die_ref die)
14722 {
14723   dw_die_ref c;
14724
14725   if (die->die_mark)
14726     die->die_mark = 0;
14727   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14728 }
14729
14730 /* Given DIE that we're marking as used, find any other dies
14731    it references as attributes and mark them as used.  */
14732
14733 static void
14734 prune_unused_types_walk_attribs (dw_die_ref die)
14735 {
14736   dw_attr_ref a;
14737   unsigned ix;
14738
14739   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14740     {
14741       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14742         {
14743           /* A reference to another DIE.
14744              Make sure that it will get emitted.  */
14745           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14746         }
14747       /* Set the string's refcount to 0 so that prune_unused_types_mark
14748          accounts properly for it.  */
14749       if (AT_class (a) == dw_val_class_str)
14750         a->dw_attr_val.v.val_str->refcount = 0;
14751     }
14752 }
14753
14754
14755 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14756    to DIE's children.  */
14757
14758 static void
14759 prune_unused_types_mark (dw_die_ref die, int dokids)
14760 {
14761   dw_die_ref c;
14762
14763   if (die->die_mark == 0)
14764     {
14765       /* We haven't done this node yet.  Mark it as used.  */
14766       die->die_mark = 1;
14767
14768       /* We also have to mark its parents as used.
14769          (But we don't want to mark our parents' kids due to this.)  */
14770       if (die->die_parent)
14771         prune_unused_types_mark (die->die_parent, 0);
14772
14773       /* Mark any referenced nodes.  */
14774       prune_unused_types_walk_attribs (die);
14775
14776       /* If this node is a specification,
14777          also mark the definition, if it exists.  */
14778       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14779         prune_unused_types_mark (die->die_definition, 1);
14780     }
14781
14782   if (dokids && die->die_mark != 2)
14783     {
14784       /* We need to walk the children, but haven't done so yet.
14785          Remember that we've walked the kids.  */
14786       die->die_mark = 2;
14787
14788       /* If this is an array type, we need to make sure our
14789          kids get marked, even if they're types.  */
14790       if (die->die_tag == DW_TAG_array_type)
14791         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14792       else
14793         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14794     }
14795 }
14796
14797
14798 /* Walk the tree DIE and mark types that we actually use.  */
14799
14800 static void
14801 prune_unused_types_walk (dw_die_ref die)
14802 {
14803   dw_die_ref c;
14804
14805   /* Don't do anything if this node is already marked.  */
14806   if (die->die_mark)
14807     return;
14808
14809   switch (die->die_tag)
14810     {
14811     case DW_TAG_const_type:
14812     case DW_TAG_packed_type:
14813     case DW_TAG_pointer_type:
14814     case DW_TAG_reference_type:
14815     case DW_TAG_volatile_type:
14816     case DW_TAG_typedef:
14817     case DW_TAG_array_type:
14818     case DW_TAG_structure_type:
14819     case DW_TAG_union_type:
14820     case DW_TAG_class_type:
14821     case DW_TAG_interface_type:
14822     case DW_TAG_friend:
14823     case DW_TAG_variant_part:
14824     case DW_TAG_enumeration_type:
14825     case DW_TAG_subroutine_type:
14826     case DW_TAG_string_type:
14827     case DW_TAG_set_type:
14828     case DW_TAG_subrange_type:
14829     case DW_TAG_ptr_to_member_type:
14830     case DW_TAG_file_type:
14831       if (die->die_perennial_p)
14832         break;
14833
14834       /* It's a type node --- don't mark it.  */
14835       return;
14836
14837     default:
14838       /* Mark everything else.  */
14839       break;
14840   }
14841
14842   die->die_mark = 1;
14843
14844   /* Now, mark any dies referenced from here.  */
14845   prune_unused_types_walk_attribs (die);
14846
14847   /* Mark children.  */
14848   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14849 }
14850
14851 /* Increment the string counts on strings referred to from DIE's
14852    attributes.  */
14853
14854 static void
14855 prune_unused_types_update_strings (dw_die_ref die)
14856 {
14857   dw_attr_ref a;
14858   unsigned ix;
14859
14860   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14861     if (AT_class (a) == dw_val_class_str)
14862       {
14863         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14864         s->refcount++;
14865         /* Avoid unnecessarily putting strings that are used less than
14866            twice in the hash table.  */
14867         if (s->refcount
14868             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14869           {
14870             void ** slot;
14871             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14872                                              htab_hash_string (s->str),
14873                                              INSERT);
14874             gcc_assert (*slot == NULL);
14875             *slot = s;
14876           }
14877       }
14878 }
14879
14880 /* Remove from the tree DIE any dies that aren't marked.  */
14881
14882 static void
14883 prune_unused_types_prune (dw_die_ref die)
14884 {
14885   dw_die_ref c;
14886
14887   gcc_assert (die->die_mark);
14888   prune_unused_types_update_strings (die);
14889
14890   if (! die->die_child)
14891     return;
14892
14893   c = die->die_child;
14894   do {
14895     dw_die_ref prev = c;
14896     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14897       if (c == die->die_child)
14898         {
14899           /* No marked children between 'prev' and the end of the list.  */
14900           if (prev == c)
14901             /* No marked children at all.  */
14902             die->die_child = NULL;
14903           else
14904             {
14905               prev->die_sib = c->die_sib;
14906               die->die_child = prev;
14907             }
14908           return;
14909         }
14910
14911     if (c != prev->die_sib)
14912       prev->die_sib = c;
14913     prune_unused_types_prune (c);
14914   } while (c != die->die_child);
14915 }
14916
14917
14918 /* Remove dies representing declarations that we never use.  */
14919
14920 static void
14921 prune_unused_types (void)
14922 {
14923   unsigned int i;
14924   limbo_die_node *node;
14925   pubname_ref pub;
14926
14927 #if ENABLE_ASSERT_CHECKING
14928   /* All the marks should already be clear.  */
14929   verify_marks_clear (comp_unit_die);
14930   for (node = limbo_die_list; node; node = node->next)
14931     verify_marks_clear (node->die);
14932 #endif /* ENABLE_ASSERT_CHECKING */
14933
14934   /* Set the mark on nodes that are actually used.  */
14935   prune_unused_types_walk (comp_unit_die);
14936   for (node = limbo_die_list; node; node = node->next)
14937     prune_unused_types_walk (node->die);
14938
14939   /* Also set the mark on nodes referenced from the
14940      pubname_table or arange_table.  */
14941   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14942     prune_unused_types_mark (pub->die, 1);
14943   for (i = 0; i < arange_table_in_use; i++)
14944     prune_unused_types_mark (arange_table[i], 1);
14945
14946   /* Get rid of nodes that aren't marked; and update the string counts.  */
14947   if (debug_str_hash)
14948     htab_empty (debug_str_hash);
14949   prune_unused_types_prune (comp_unit_die);
14950   for (node = limbo_die_list; node; node = node->next)
14951     prune_unused_types_prune (node->die);
14952
14953   /* Leave the marks clear.  */
14954   prune_unmark_dies (comp_unit_die);
14955   for (node = limbo_die_list; node; node = node->next)
14956     prune_unmark_dies (node->die);
14957 }
14958
14959 /* Set the parameter to true if there are any relative pathnames in
14960    the file table.  */
14961 static int
14962 file_table_relative_p (void ** slot, void *param)
14963 {
14964   bool *p = param;
14965   struct dwarf_file_data *d = *slot;
14966   if (!IS_ABSOLUTE_PATH (d->filename))
14967     {
14968       *p = true;
14969       return 0;
14970     }
14971   return 1;
14972 }
14973
14974 /* Output stuff that dwarf requires at the end of every file,
14975    and generate the DWARF-2 debugging info.  */
14976
14977 static void
14978 dwarf2out_finish (const char *filename)
14979 {
14980   limbo_die_node *node, *next_node;
14981   dw_die_ref die = 0;
14982
14983   /* Add the name for the main input file now.  We delayed this from
14984      dwarf2out_init to avoid complications with PCH.  */
14985   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
14986   if (!IS_ABSOLUTE_PATH (filename))
14987     add_comp_dir_attribute (comp_unit_die);
14988   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14989     {
14990       bool p = false;
14991       htab_traverse (file_table, file_table_relative_p, &p);
14992       if (p)
14993         add_comp_dir_attribute (comp_unit_die);
14994     }
14995
14996   /* Traverse the limbo die list, and add parent/child links.  The only
14997      dies without parents that should be here are concrete instances of
14998      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
14999      For concrete instances, we can get the parent die from the abstract
15000      instance.  */
15001   for (node = limbo_die_list; node; node = next_node)
15002     {
15003       next_node = node->next;
15004       die = node->die;
15005
15006       if (die->die_parent == NULL)
15007         {
15008           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15009
15010           if (origin)
15011             add_child_die (origin->die_parent, die);
15012           else if (die == comp_unit_die)
15013             ;
15014           else if (errorcount > 0 || sorrycount > 0)
15015             /* It's OK to be confused by errors in the input.  */
15016             add_child_die (comp_unit_die, die);
15017           else
15018             {
15019               /* In certain situations, the lexical block containing a
15020                  nested function can be optimized away, which results
15021                  in the nested function die being orphaned.  Likewise
15022                  with the return type of that nested function.  Force
15023                  this to be a child of the containing function.
15024
15025                  It may happen that even the containing function got fully
15026                  inlined and optimized out.  In that case we are lost and
15027                  assign the empty child.  This should not be big issue as
15028                  the function is likely unreachable too.  */
15029               tree context = NULL_TREE;
15030
15031               gcc_assert (node->created_for);
15032
15033               if (DECL_P (node->created_for))
15034                 context = DECL_CONTEXT (node->created_for);
15035               else if (TYPE_P (node->created_for))
15036                 context = TYPE_CONTEXT (node->created_for);
15037
15038               gcc_assert (context
15039                           && (TREE_CODE (context) == FUNCTION_DECL
15040                               || TREE_CODE (context) == NAMESPACE_DECL));
15041
15042               origin = lookup_decl_die (context);
15043               if (origin)
15044                 add_child_die (origin, die);
15045               else
15046                 add_child_die (comp_unit_die, die);
15047             }
15048         }
15049     }
15050
15051   limbo_die_list = NULL;
15052
15053   /* Walk through the list of incomplete types again, trying once more to
15054      emit full debugging info for them.  */
15055   retry_incomplete_types ();
15056
15057   if (flag_eliminate_unused_debug_types)
15058     prune_unused_types ();
15059
15060   /* Generate separate CUs for each of the include files we've seen.
15061      They will go into limbo_die_list.  */
15062   if (flag_eliminate_dwarf2_dups)
15063     break_out_includes (comp_unit_die);
15064
15065   /* Traverse the DIE's and add add sibling attributes to those DIE's
15066      that have children.  */
15067   add_sibling_attributes (comp_unit_die);
15068   for (node = limbo_die_list; node; node = node->next)
15069     add_sibling_attributes (node->die);
15070
15071   /* Output a terminator label for the .text section.  */
15072   switch_to_section (text_section);
15073   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15074   if (flag_reorder_blocks_and_partition)
15075     {
15076       switch_to_section (unlikely_text_section ());
15077       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15078     }
15079
15080   /* We can only use the low/high_pc attributes if all of the code was
15081      in .text.  */
15082   if (!have_multiple_function_sections)
15083     {
15084       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15085       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15086     }
15087
15088   else
15089     {
15090       unsigned fde_idx = 0;
15091
15092       /* We need to give .debug_loc and .debug_ranges an appropriate
15093          "base address".  Use zero so that these addresses become
15094          absolute.  Historically, we've emitted the unexpected
15095          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15096          Emit both to give time for other tools to adapt.  */
15097       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15098       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15099
15100       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15101                          add_ranges_by_labels (text_section_label,
15102                                                text_end_label));
15103       if (flag_reorder_blocks_and_partition)
15104         add_ranges_by_labels (cold_text_section_label,
15105                               cold_end_label);
15106
15107       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15108         {
15109           dw_fde_ref fde = &fde_table[fde_idx];
15110
15111           if (fde->dw_fde_switched_sections)
15112             {
15113               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15114                                     fde->dw_fde_hot_section_end_label);
15115               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15116                                     fde->dw_fde_unlikely_section_end_label);
15117             }
15118           else
15119             add_ranges_by_labels (fde->dw_fde_begin,
15120                                   fde->dw_fde_end);
15121         }
15122
15123       add_ranges (NULL);
15124     }
15125
15126   /* Output location list section if necessary.  */
15127   if (have_location_lists)
15128     {
15129       /* Output the location lists info.  */
15130       switch_to_section (debug_loc_section);
15131       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15132                                    DEBUG_LOC_SECTION_LABEL, 0);
15133       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15134       output_location_lists (die);
15135     }
15136
15137   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15138     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15139                     debug_line_section_label);
15140
15141   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15142     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15143
15144   /* Output all of the compilation units.  We put the main one last so that
15145      the offsets are available to output_pubnames.  */
15146   for (node = limbo_die_list; node; node = node->next)
15147     output_comp_unit (node->die, 0);
15148
15149   output_comp_unit (comp_unit_die, 0);
15150
15151   /* Output the abbreviation table.  */
15152   switch_to_section (debug_abbrev_section);
15153   output_abbrev_section ();
15154
15155   /* Output public names table if necessary.  */
15156   if (!VEC_empty (pubname_entry, pubname_table))
15157     {
15158       switch_to_section (debug_pubnames_section);
15159       output_pubnames (pubname_table);
15160     }
15161
15162 #ifdef DEBUG_PUBTYPES_SECTION
15163   /* Output public types table if necessary.  */
15164   if (!VEC_empty (pubname_entry, pubtype_table))
15165     {
15166       switch_to_section (debug_pubtypes_section);
15167       output_pubnames (pubtype_table);
15168     }
15169 #endif
15170
15171   /* Output the address range information.  We only put functions in the arange
15172      table, so don't write it out if we don't have any.  */
15173   if (fde_table_in_use)
15174     {
15175       switch_to_section (debug_aranges_section);
15176       output_aranges ();
15177     }
15178
15179   /* Output ranges section if necessary.  */
15180   if (ranges_table_in_use)
15181     {
15182       switch_to_section (debug_ranges_section);
15183       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15184       output_ranges ();
15185     }
15186
15187   /* Output the source line correspondence table.  We must do this
15188      even if there is no line information.  Otherwise, on an empty
15189      translation unit, we will generate a present, but empty,
15190      .debug_info section.  IRIX 6.5 `nm' will then complain when
15191      examining the file.  This is done late so that any filenames
15192      used by the debug_info section are marked as 'used'.  */
15193   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15194     {
15195       switch_to_section (debug_line_section);
15196       output_line_info ();
15197     }
15198
15199   /* Have to end the macro section.  */
15200   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15201     {
15202       switch_to_section (debug_macinfo_section);
15203       dw2_asm_output_data (1, 0, "End compilation unit");
15204     }
15205
15206   /* If we emitted any DW_FORM_strp form attribute, output the string
15207      table too.  */
15208   if (debug_str_hash)
15209     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15210 }
15211 #else
15212
15213 /* This should never be used, but its address is needed for comparisons.  */
15214 const struct gcc_debug_hooks dwarf2_debug_hooks;
15215
15216 #endif /* DWARF2_DEBUGGING_INFO */
15217
15218 #include "gt-dwarf2out.h"