OSDN Git Service

* config/xtensa/xtensa.c (xtensa_va_start): Unshare valist.
[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   HOST_WIDE_INT stack_realignment;
243   /* Dynamic realign argument pointer register.  */
244   unsigned int drap_reg;
245   /* Virtual dynamic realign argument pointer register.  */
246   unsigned int vdrap_reg;
247   unsigned all_throwers_are_sibcalls : 1;
248   unsigned nothrow : 1;
249   unsigned uses_eh_lsda : 1;
250   /* Whether we did stack realign in this call frame.  */
251   unsigned stack_realign : 1;
252   /* Whether dynamic realign argument pointer register has been saved.  */
253   unsigned drap_reg_saved: 1;
254 }
255 dw_fde_node;
256
257 /* Maximum size (in bytes) of an artificially generated label.  */
258 #define MAX_ARTIFICIAL_LABEL_BYTES      30
259
260 /* The size of addresses as they appear in the Dwarf 2 data.
261    Some architectures use word addresses to refer to code locations,
262    but Dwarf 2 info always uses byte addresses.  On such machines,
263    Dwarf 2 addresses need to be larger than the architecture's
264    pointers.  */
265 #ifndef DWARF2_ADDR_SIZE
266 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
267 #endif
268
269 /* The size in bytes of a DWARF field indicating an offset or length
270    relative to a debug info section, specified to be 4 bytes in the
271    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
272    as PTR_SIZE.  */
273
274 #ifndef DWARF_OFFSET_SIZE
275 #define DWARF_OFFSET_SIZE 4
276 #endif
277
278 /* According to the (draft) DWARF 3 specification, the initial length
279    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
280    bytes are 0xffffffff, followed by the length stored in the next 8
281    bytes.
282
283    However, the SGI/MIPS ABI uses an initial length which is equal to
284    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
285
286 #ifndef DWARF_INITIAL_LENGTH_SIZE
287 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
288 #endif
289
290 #define DWARF_VERSION 2
291
292 /* Round SIZE up to the nearest BOUNDARY.  */
293 #define DWARF_ROUND(SIZE,BOUNDARY) \
294   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
295
296 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
297 #ifndef DWARF_CIE_DATA_ALIGNMENT
298 #ifdef STACK_GROWS_DOWNWARD
299 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
300 #else
301 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
302 #endif
303 #endif
304
305 /* CIE identifier.  */
306 #if HOST_BITS_PER_WIDE_INT >= 64
307 #define DWARF_CIE_ID \
308   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
309 #else
310 #define DWARF_CIE_ID DW_CIE_ID
311 #endif
312
313 /* A pointer to the base of a table that contains frame description
314    information for each routine.  */
315 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
316
317 /* Number of elements currently allocated for fde_table.  */
318 static GTY(()) unsigned fde_table_allocated;
319
320 /* Number of elements in fde_table currently in use.  */
321 static GTY(()) unsigned fde_table_in_use;
322
323 /* Size (in elements) of increments by which we may expand the
324    fde_table.  */
325 #define FDE_TABLE_INCREMENT 256
326
327 /* Get the current fde_table entry we should use.  */
328
329 static inline dw_fde_ref
330 current_fde (void)
331 {
332   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
333 }
334
335 /* A list of call frame insns for the CIE.  */
336 static GTY(()) dw_cfi_ref cie_cfi_head;
337
338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
340    attribute that accelerates the lookup of the FDE associated
341    with the subprogram.  This variable holds the table index of the FDE
342    associated with the current function (body) definition.  */
343 static unsigned current_funcdef_fde;
344 #endif
345
346 struct indirect_string_node GTY(())
347 {
348   const char *str;
349   unsigned int refcount;
350   unsigned int form;
351   char *label;
352 };
353
354 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
355
356 static GTY(()) int dw2_string_counter;
357 static GTY(()) unsigned long dwarf2out_cfi_label_num;
358
359 /* True if the compilation unit places functions in more than one section.  */
360 static GTY(()) bool have_multiple_function_sections = false;
361
362 /* Whether the default text and cold text sections have been used at all.  */
363
364 static GTY(()) bool text_section_used = false;
365 static GTY(()) bool cold_text_section_used = false;
366
367 /* The default cold text section.  */
368 static GTY(()) section *cold_text_section;
369
370 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
371
372 /* Forward declarations for functions defined in this file.  */
373
374 static char *stripattributes (const char *);
375 static const char *dwarf_cfi_name (unsigned);
376 static dw_cfi_ref new_cfi (void);
377 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
378 static void add_fde_cfi (const char *, dw_cfi_ref);
379 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
380 static void lookup_cfa (dw_cfa_location *);
381 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
382 #ifdef DWARF2_UNWIND_INFO
383 static void initial_return_save (rtx);
384 #endif
385 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
386 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
387 static void output_cfi_directive (dw_cfi_ref);
388 static void output_call_frame_info (int);
389 static void dwarf2out_note_section_used (void);
390 static void dwarf2out_stack_adjust (rtx, bool);
391 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
392 static void flush_queued_reg_saves (void);
393 static bool clobbers_queued_reg_save (const_rtx);
394 static void dwarf2out_frame_debug_expr (rtx, const char *);
395
396 /* Support for complex CFA locations.  */
397 static void output_cfa_loc (dw_cfi_ref);
398 static void output_cfa_loc_raw (dw_cfi_ref);
399 static void get_cfa_from_loc_descr (dw_cfa_location *,
400                                     struct dw_loc_descr_struct *);
401 static struct dw_loc_descr_struct *build_cfa_loc
402   (dw_cfa_location *, HOST_WIDE_INT);
403 static struct dw_loc_descr_struct *build_cfa_aligned_loc
404   (HOST_WIDE_INT, HOST_WIDE_INT);
405 static void def_cfa_1 (const char *, dw_cfa_location *);
406
407 /* How to start an assembler comment.  */
408 #ifndef ASM_COMMENT_START
409 #define ASM_COMMENT_START ";#"
410 #endif
411
412 /* Data and reference forms for relocatable data.  */
413 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
414 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
415
416 #ifndef DEBUG_FRAME_SECTION
417 #define DEBUG_FRAME_SECTION     ".debug_frame"
418 #endif
419
420 #ifndef FUNC_BEGIN_LABEL
421 #define FUNC_BEGIN_LABEL        "LFB"
422 #endif
423
424 #ifndef FUNC_END_LABEL
425 #define FUNC_END_LABEL          "LFE"
426 #endif
427
428 #ifndef FRAME_BEGIN_LABEL
429 #define FRAME_BEGIN_LABEL       "Lframe"
430 #endif
431 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
432 #define CIE_END_LABEL           "LECIE"
433 #define FDE_LABEL               "LSFDE"
434 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
435 #define FDE_END_LABEL           "LEFDE"
436 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
437 #define LINE_NUMBER_END_LABEL   "LELT"
438 #define LN_PROLOG_AS_LABEL      "LASLTP"
439 #define LN_PROLOG_END_LABEL     "LELTP"
440 #define DIE_LABEL_PREFIX        "DW"
441
442 /* The DWARF 2 CFA column which tracks the return address.  Normally this
443    is the column for PC, or the first column after all of the hard
444    registers.  */
445 #ifndef DWARF_FRAME_RETURN_COLUMN
446 #ifdef PC_REGNUM
447 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
448 #else
449 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
450 #endif
451 #endif
452
453 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
454    default, we just provide columns for all registers.  */
455 #ifndef DWARF_FRAME_REGNUM
456 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
457 #endif
458 \f
459 /* Hook used by __throw.  */
460
461 rtx
462 expand_builtin_dwarf_sp_column (void)
463 {
464   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
465   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
466 }
467
468 /* Return a pointer to a copy of the section string name S with all
469    attributes stripped off, and an asterisk prepended (for assemble_name).  */
470
471 static inline char *
472 stripattributes (const char *s)
473 {
474   char *stripped = XNEWVEC (char, strlen (s) + 2);
475   char *p = stripped;
476
477   *p++ = '*';
478
479   while (*s && *s != ',')
480     *p++ = *s++;
481
482   *p = '\0';
483   return stripped;
484 }
485
486 /* MEM is a memory reference for the register size table, each element of
487    which has mode MODE.  Initialize column C as a return address column.  */
488
489 static void
490 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
491 {
492   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
493   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
494   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
495 }
496
497 /* Generate code to initialize the register size table.  */
498
499 void
500 expand_builtin_init_dwarf_reg_sizes (tree address)
501 {
502   unsigned int i;
503   enum machine_mode mode = TYPE_MODE (char_type_node);
504   rtx addr = expand_normal (address);
505   rtx mem = gen_rtx_MEM (BLKmode, addr);
506   bool wrote_return_column = false;
507
508   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
509     {
510       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
511
512       if (rnum < DWARF_FRAME_REGISTERS)
513         {
514           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
515           enum machine_mode save_mode = reg_raw_mode[i];
516           HOST_WIDE_INT size;
517
518           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
519             save_mode = choose_hard_reg_mode (i, 1, true);
520           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
521             {
522               if (save_mode == VOIDmode)
523                 continue;
524               wrote_return_column = true;
525             }
526           size = GET_MODE_SIZE (save_mode);
527           if (offset < 0)
528             continue;
529
530           emit_move_insn (adjust_address (mem, mode, offset),
531                           gen_int_mode (size, mode));
532         }
533     }
534
535   if (!wrote_return_column)
536     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
537
538 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
539   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
540 #endif
541
542   targetm.init_dwarf_reg_sizes_extra (address);
543 }
544
545 /* Convert a DWARF call frame info. operation to its string name */
546
547 static const char *
548 dwarf_cfi_name (unsigned int cfi_opc)
549 {
550   switch (cfi_opc)
551     {
552     case DW_CFA_advance_loc:
553       return "DW_CFA_advance_loc";
554     case DW_CFA_offset:
555       return "DW_CFA_offset";
556     case DW_CFA_restore:
557       return "DW_CFA_restore";
558     case DW_CFA_nop:
559       return "DW_CFA_nop";
560     case DW_CFA_set_loc:
561       return "DW_CFA_set_loc";
562     case DW_CFA_advance_loc1:
563       return "DW_CFA_advance_loc1";
564     case DW_CFA_advance_loc2:
565       return "DW_CFA_advance_loc2";
566     case DW_CFA_advance_loc4:
567       return "DW_CFA_advance_loc4";
568     case DW_CFA_offset_extended:
569       return "DW_CFA_offset_extended";
570     case DW_CFA_restore_extended:
571       return "DW_CFA_restore_extended";
572     case DW_CFA_undefined:
573       return "DW_CFA_undefined";
574     case DW_CFA_same_value:
575       return "DW_CFA_same_value";
576     case DW_CFA_register:
577       return "DW_CFA_register";
578     case DW_CFA_remember_state:
579       return "DW_CFA_remember_state";
580     case DW_CFA_restore_state:
581       return "DW_CFA_restore_state";
582     case DW_CFA_def_cfa:
583       return "DW_CFA_def_cfa";
584     case DW_CFA_def_cfa_register:
585       return "DW_CFA_def_cfa_register";
586     case DW_CFA_def_cfa_offset:
587       return "DW_CFA_def_cfa_offset";
588
589     /* DWARF 3 */
590     case DW_CFA_def_cfa_expression:
591       return "DW_CFA_def_cfa_expression";
592     case DW_CFA_expression:
593       return "DW_CFA_expression";
594     case DW_CFA_offset_extended_sf:
595       return "DW_CFA_offset_extended_sf";
596     case DW_CFA_def_cfa_sf:
597       return "DW_CFA_def_cfa_sf";
598     case DW_CFA_def_cfa_offset_sf:
599       return "DW_CFA_def_cfa_offset_sf";
600
601     /* SGI/MIPS specific */
602     case DW_CFA_MIPS_advance_loc8:
603       return "DW_CFA_MIPS_advance_loc8";
604
605     /* GNU extensions */
606     case DW_CFA_GNU_window_save:
607       return "DW_CFA_GNU_window_save";
608     case DW_CFA_GNU_args_size:
609       return "DW_CFA_GNU_args_size";
610     case DW_CFA_GNU_negative_offset_extended:
611       return "DW_CFA_GNU_negative_offset_extended";
612
613     default:
614       return "DW_CFA_<unknown>";
615     }
616 }
617
618 /* Return a pointer to a newly allocated Call Frame Instruction.  */
619
620 static inline dw_cfi_ref
621 new_cfi (void)
622 {
623   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
624
625   cfi->dw_cfi_next = NULL;
626   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
627   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
628
629   return cfi;
630 }
631
632 /* Add a Call Frame Instruction to list of instructions.  */
633
634 static inline void
635 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
636 {
637   dw_cfi_ref *p;
638   dw_fde_ref fde = current_fde ();
639
640   /* When DRAP is used, CFA is defined with an expression.  Redefine
641      CFA may lead to a different CFA value.   */
642   if (fde && fde->drap_reg != INVALID_REGNUM)
643     switch (cfi->dw_cfi_opc)
644       {
645         case DW_CFA_def_cfa_register:
646         case DW_CFA_def_cfa_offset:
647         case DW_CFA_def_cfa_offset_sf:
648         case DW_CFA_def_cfa:
649         case DW_CFA_def_cfa_sf:
650           gcc_unreachable ();
651
652         default:
653           break;
654       }
655
656   /* Find the end of the chain.  */
657   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
658     ;
659
660   *p = cfi;
661 }
662
663 /* Generate a new label for the CFI info to refer to.  */
664
665 char *
666 dwarf2out_cfi_label (void)
667 {
668   static char label[20];
669
670   if (flag_dwarf2_cfi_asm)
671     {
672       /* In this case, we will be emitting the asm directive instead of
673          the label, so just return a placeholder to keep the rest of the
674          interfaces happy.  */
675       strcpy (label, "<do not output>");
676     }
677   else
678     {
679       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
680       ASM_OUTPUT_LABEL (asm_out_file, label);
681     }
682
683   return label;
684 }
685
686 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
687    or to the CIE if LABEL is NULL.  */
688
689 static void
690 add_fde_cfi (const char *label, dw_cfi_ref cfi)
691 {
692   dw_cfi_ref *list_head = &cie_cfi_head;
693
694   if (flag_dwarf2_cfi_asm)
695     {
696       if (label)
697         {
698           output_cfi_directive (cfi);
699
700           /* We still have to add the cfi to the list so that
701              lookup_cfa works later on.  */
702           list_head = &current_fde ()->dw_fde_cfi;
703         }
704       /* ??? If this is a CFI for the CIE, we don't emit.  This
705          assumes that the standard CIE contents that the assembler
706          uses matches the standard CIE contents that the compiler
707          uses.  This is probably a bad assumption.  I'm not quite
708          sure how to address this for now.  */
709     }
710   else if (label)
711     {
712       dw_fde_ref fde = current_fde ();
713
714       gcc_assert (fde != NULL);
715
716       if (*label == 0)
717         label = dwarf2out_cfi_label ();
718
719       if (fde->dw_fde_current_label == NULL
720           || strcmp (label, fde->dw_fde_current_label) != 0)
721         {
722           dw_cfi_ref xcfi;
723
724           label = xstrdup (label);
725
726           /* Set the location counter to the new label.  */
727           xcfi = new_cfi ();
728           /* If we have a current label, advance from there, otherwise
729              set the location directly using set_loc.  */
730           xcfi->dw_cfi_opc = fde->dw_fde_current_label
731                              ? DW_CFA_advance_loc4
732                              : DW_CFA_set_loc;
733           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
734           add_cfi (&fde->dw_fde_cfi, xcfi);
735
736           fde->dw_fde_current_label = label;
737         }
738
739       list_head = &fde->dw_fde_cfi;
740     }
741
742   add_cfi (list_head, cfi);
743 }
744
745 /* Subroutine of lookup_cfa.  */
746
747 static void
748 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
749 {
750   switch (cfi->dw_cfi_opc)
751     {
752     case DW_CFA_def_cfa_offset:
753       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
754       break;
755     case DW_CFA_def_cfa_offset_sf:
756       loc->offset
757         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
758       break;
759     case DW_CFA_def_cfa_register:
760       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
761       break;
762     case DW_CFA_def_cfa:
763       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
764       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
765       break;
766     case DW_CFA_def_cfa_sf:
767       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
768       loc->offset
769         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
770       break;
771     case DW_CFA_def_cfa_expression:
772       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
773       break;
774     default:
775       break;
776     }
777 }
778
779 /* Find the previous value for the CFA.  */
780
781 static void
782 lookup_cfa (dw_cfa_location *loc)
783 {
784   dw_cfi_ref cfi;
785   dw_fde_ref fde;
786
787   loc->reg = INVALID_REGNUM;
788   loc->offset = 0;
789   loc->indirect = 0;
790   loc->base_offset = 0;
791
792   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
793     lookup_cfa_1 (cfi, loc);
794
795   fde = current_fde ();
796   if (fde)
797     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
798       lookup_cfa_1 (cfi, loc);
799 }
800
801 /* The current rule for calculating the DWARF2 canonical frame address.  */
802 static dw_cfa_location cfa;
803
804 /* The register used for saving registers to the stack, and its offset
805    from the CFA.  */
806 static dw_cfa_location cfa_store;
807
808 /* The running total of the size of arguments pushed onto the stack.  */
809 static HOST_WIDE_INT args_size;
810
811 /* The last args_size we actually output.  */
812 static HOST_WIDE_INT old_args_size;
813
814 /* Entry point to update the canonical frame address (CFA).
815    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
816    calculated from REG+OFFSET.  */
817
818 void
819 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
820 {
821   dw_cfa_location loc;
822   loc.indirect = 0;
823   loc.base_offset = 0;
824   loc.reg = reg;
825   loc.offset = offset;
826   def_cfa_1 (label, &loc);
827 }
828
829 /* Determine if two dw_cfa_location structures define the same data.  */
830
831 static bool
832 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
833 {
834   return (loc1->reg == loc2->reg
835           && loc1->offset == loc2->offset
836           && loc1->indirect == loc2->indirect
837           && (loc1->indirect == 0
838               || loc1->base_offset == loc2->base_offset));
839 }
840
841 /* This routine does the actual work.  The CFA is now calculated from
842    the dw_cfa_location structure.  */
843
844 static void
845 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
846 {
847   dw_cfi_ref cfi;
848   dw_cfa_location old_cfa, loc;
849
850   cfa = *loc_p;
851   loc = *loc_p;
852
853   if (cfa_store.reg == loc.reg && loc.indirect == 0)
854     cfa_store.offset = loc.offset;
855
856   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
857   lookup_cfa (&old_cfa);
858
859   /* If nothing changed, no need to issue any call frame instructions.  */
860   if (cfa_equal_p (&loc, &old_cfa))
861     return;
862
863   cfi = new_cfi ();
864
865   if (loc.reg == old_cfa.reg && !loc.indirect)
866     {
867       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
868          the CFA register did not change but the offset did.  */
869       if (loc.offset < 0)
870         {
871           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
872           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
873
874           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
875           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
876         }
877       else
878         {
879           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
880           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
881         }
882     }
883
884 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
885   else if (loc.offset == old_cfa.offset
886            && old_cfa.reg != INVALID_REGNUM
887            && !loc.indirect)
888     {
889       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
890          indicating the CFA register has changed to <register> but the
891          offset has not changed.  */
892       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
893       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
894     }
895 #endif
896
897   else if (loc.indirect == 0)
898     {
899       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
900          indicating the CFA register has changed to <register> with
901          the specified offset.  */
902       if (loc.offset < 0)
903         {
904           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
905           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
906
907           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
908           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
909           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
910         }
911       else
912         {
913           cfi->dw_cfi_opc = DW_CFA_def_cfa;
914           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
915           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
916         }
917     }
918   else
919     {
920       /* Construct a DW_CFA_def_cfa_expression instruction to
921          calculate the CFA using a full location expression since no
922          register-offset pair is available.  */
923       struct dw_loc_descr_struct *loc_list;
924
925       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
926       loc_list = build_cfa_loc (&loc, 0);
927       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
928     }
929
930   add_fde_cfi (label, cfi);
931 }
932
933 /* Add the CFI for saving a register.  REG is the CFA column number.
934    LABEL is passed to add_fde_cfi.
935    If SREG is -1, the register is saved at OFFSET from the CFA;
936    otherwise it is saved in SREG.  */
937
938 static void
939 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
940 {
941   dw_cfi_ref cfi = new_cfi ();
942   dw_fde_ref fde = current_fde ();
943
944   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
945
946   /* When stack is aligned, store REG using DW_CFA_expression with
947      FP.  */
948   if (fde
949       && fde->stack_realign
950       && sreg == INVALID_REGNUM)
951     {
952       cfi->dw_cfi_opc = DW_CFA_expression;
953       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
954       cfi->dw_cfi_oprnd1.dw_cfi_loc
955         = build_cfa_aligned_loc (offset, fde->stack_realignment);
956     }
957   else if (sreg == INVALID_REGNUM)
958     {
959       if (reg & ~0x3f)
960         /* The register number won't fit in 6 bits, so we have to use
961            the long form.  */
962         cfi->dw_cfi_opc = DW_CFA_offset_extended;
963       else
964         cfi->dw_cfi_opc = DW_CFA_offset;
965
966 #ifdef ENABLE_CHECKING
967       {
968         /* If we get an offset that is not a multiple of
969            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
970            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
971            description.  */
972         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
973
974         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
975       }
976 #endif
977       offset /= DWARF_CIE_DATA_ALIGNMENT;
978       if (offset < 0)
979         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
980
981       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
982     }
983   else if (sreg == reg)
984     cfi->dw_cfi_opc = DW_CFA_same_value;
985   else
986     {
987       cfi->dw_cfi_opc = DW_CFA_register;
988       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
989     }
990
991   add_fde_cfi (label, cfi);
992 }
993
994 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
995    This CFI tells the unwinder that it needs to restore the window registers
996    from the previous frame's window save area.
997
998    ??? Perhaps we should note in the CIE where windows are saved (instead of
999    assuming 0(cfa)) and what registers are in the window.  */
1000
1001 void
1002 dwarf2out_window_save (const char *label)
1003 {
1004   dw_cfi_ref cfi = new_cfi ();
1005
1006   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1007   add_fde_cfi (label, cfi);
1008 }
1009
1010 /* Add a CFI to update the running total of the size of arguments
1011    pushed onto the stack.  */
1012
1013 void
1014 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1015 {
1016   dw_cfi_ref cfi;
1017
1018   if (size == old_args_size)
1019     return;
1020
1021   old_args_size = size;
1022
1023   cfi = new_cfi ();
1024   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1025   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1026   add_fde_cfi (label, cfi);
1027 }
1028
1029 /* Entry point for saving a register to the stack.  REG is the GCC register
1030    number.  LABEL and OFFSET are passed to reg_save.  */
1031
1032 void
1033 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1034 {
1035   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1036 }
1037
1038 /* Entry point for saving the return address in the stack.
1039    LABEL and OFFSET are passed to reg_save.  */
1040
1041 void
1042 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1043 {
1044   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1045 }
1046
1047 /* Entry point for saving the return address in a register.
1048    LABEL and SREG are passed to reg_save.  */
1049
1050 void
1051 dwarf2out_return_reg (const char *label, unsigned int sreg)
1052 {
1053   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1054 }
1055
1056 #ifdef DWARF2_UNWIND_INFO
1057 /* Record the initial position of the return address.  RTL is
1058    INCOMING_RETURN_ADDR_RTX.  */
1059
1060 static void
1061 initial_return_save (rtx rtl)
1062 {
1063   unsigned int reg = INVALID_REGNUM;
1064   HOST_WIDE_INT offset = 0;
1065
1066   switch (GET_CODE (rtl))
1067     {
1068     case REG:
1069       /* RA is in a register.  */
1070       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1071       break;
1072
1073     case MEM:
1074       /* RA is on the stack.  */
1075       rtl = XEXP (rtl, 0);
1076       switch (GET_CODE (rtl))
1077         {
1078         case REG:
1079           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1080           offset = 0;
1081           break;
1082
1083         case PLUS:
1084           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1085           offset = INTVAL (XEXP (rtl, 1));
1086           break;
1087
1088         case MINUS:
1089           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1090           offset = -INTVAL (XEXP (rtl, 1));
1091           break;
1092
1093         default:
1094           gcc_unreachable ();
1095         }
1096
1097       break;
1098
1099     case PLUS:
1100       /* The return address is at some offset from any value we can
1101          actually load.  For instance, on the SPARC it is in %i7+8. Just
1102          ignore the offset for now; it doesn't matter for unwinding frames.  */
1103       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1104       initial_return_save (XEXP (rtl, 0));
1105       return;
1106
1107     default:
1108       gcc_unreachable ();
1109     }
1110
1111   if (reg != DWARF_FRAME_RETURN_COLUMN)
1112     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1113 }
1114 #endif
1115
1116 /* Given a SET, calculate the amount of stack adjustment it
1117    contains.  */
1118
1119 static HOST_WIDE_INT
1120 stack_adjust_offset (const_rtx pattern)
1121 {
1122   const_rtx src = SET_SRC (pattern);
1123   const_rtx dest = SET_DEST (pattern);
1124   HOST_WIDE_INT offset = 0;
1125   enum rtx_code code;
1126
1127   if (dest == stack_pointer_rtx)
1128     {
1129       /* (set (reg sp) (plus (reg sp) (const_int))) */
1130       code = GET_CODE (src);
1131       if (! (code == PLUS || code == MINUS)
1132           || XEXP (src, 0) != stack_pointer_rtx
1133           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1134         return 0;
1135
1136       offset = INTVAL (XEXP (src, 1));
1137       if (code == PLUS)
1138         offset = -offset;
1139     }
1140   else if (MEM_P (dest))
1141     {
1142       /* (set (mem (pre_dec (reg sp))) (foo)) */
1143       src = XEXP (dest, 0);
1144       code = GET_CODE (src);
1145
1146       switch (code)
1147         {
1148         case PRE_MODIFY:
1149         case POST_MODIFY:
1150           if (XEXP (src, 0) == stack_pointer_rtx)
1151             {
1152               rtx val = XEXP (XEXP (src, 1), 1);
1153               /* We handle only adjustments by constant amount.  */
1154               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1155                           && GET_CODE (val) == CONST_INT);
1156               offset = -INTVAL (val);
1157               break;
1158             }
1159           return 0;
1160
1161         case PRE_DEC:
1162         case POST_DEC:
1163           if (XEXP (src, 0) == stack_pointer_rtx)
1164             {
1165               offset = GET_MODE_SIZE (GET_MODE (dest));
1166               break;
1167             }
1168           return 0;
1169
1170         case PRE_INC:
1171         case POST_INC:
1172           if (XEXP (src, 0) == stack_pointer_rtx)
1173             {
1174               offset = -GET_MODE_SIZE (GET_MODE (dest));
1175               break;
1176             }
1177           return 0;
1178
1179         default:
1180           return 0;
1181         }
1182     }
1183   else
1184     return 0;
1185
1186   return offset;
1187 }
1188
1189 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1190    indexed by INSN_UID.  */
1191
1192 static HOST_WIDE_INT *barrier_args_size;
1193
1194 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1195
1196 static HOST_WIDE_INT
1197 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1198                              VEC (rtx, heap) **next)
1199 {
1200   HOST_WIDE_INT offset = 0;
1201   int i;
1202
1203   if (! RTX_FRAME_RELATED_P (insn))
1204     {
1205       if (prologue_epilogue_contains (insn)
1206           || sibcall_epilogue_contains (insn))
1207         /* Nothing */;
1208       else if (GET_CODE (PATTERN (insn)) == SET)
1209         offset = stack_adjust_offset (PATTERN (insn));
1210       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1211                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1212         {
1213           /* There may be stack adjustments inside compound insns.  Search
1214              for them.  */
1215           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1216             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1217               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1218         }
1219     }
1220   else
1221     {
1222       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1223
1224       if (expr)
1225         {
1226           expr = XEXP (expr, 0);
1227           if (GET_CODE (expr) == PARALLEL
1228               || GET_CODE (expr) == SEQUENCE)
1229             for (i = 1; i < XVECLEN (expr, 0); i++)
1230               {
1231                 rtx elem = XVECEXP (expr, 0, i);
1232
1233                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1234                   offset += stack_adjust_offset (elem);
1235               }
1236         }
1237     }
1238
1239 #ifndef STACK_GROWS_DOWNWARD
1240   offset = -offset;
1241 #endif
1242
1243   cur_args_size += offset;
1244   if (cur_args_size < 0)
1245     cur_args_size = 0;
1246
1247   if (JUMP_P (insn))
1248     {
1249       rtx dest = JUMP_LABEL (insn);
1250
1251       if (dest)
1252         {
1253           if (barrier_args_size [INSN_UID (dest)] < 0)
1254             {
1255               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1256               VEC_safe_push (rtx, heap, *next, dest);
1257             }
1258           else
1259             gcc_assert (barrier_args_size[INSN_UID (dest)]
1260                         == cur_args_size);
1261         }
1262     }
1263
1264   return cur_args_size;
1265 }
1266
1267 /* Walk the whole function and compute args_size on BARRIERs.  */
1268
1269 static void
1270 compute_barrier_args_size (void)
1271 {
1272   int max_uid = get_max_uid (), i;
1273   rtx insn;
1274   VEC (rtx, heap) *worklist, *next, *tmp;
1275
1276   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1277   for (i = 0; i < max_uid; i++)
1278     barrier_args_size[i] = -1;
1279
1280   worklist = VEC_alloc (rtx, heap, 20);
1281   next = VEC_alloc (rtx, heap, 20);
1282   insn = get_insns ();
1283   barrier_args_size[INSN_UID (insn)] = 0;
1284   VEC_quick_push (rtx, worklist, insn);
1285   for (;;)
1286     {
1287       while (!VEC_empty (rtx, worklist))
1288         {
1289           rtx prev, body, first_insn;
1290           HOST_WIDE_INT cur_args_size;
1291
1292           first_insn = insn = VEC_pop (rtx, worklist);
1293           cur_args_size = barrier_args_size[INSN_UID (insn)];
1294           prev = prev_nonnote_insn (insn);
1295           if (prev && BARRIER_P (prev))
1296             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1297
1298           for (; insn; insn = NEXT_INSN (insn))
1299             {
1300               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1301                 continue;
1302               if (BARRIER_P (insn))
1303                 break;
1304
1305               if (LABEL_P (insn))
1306                 {
1307                   if (insn == first_insn)
1308                     continue;
1309                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1310                     {
1311                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1312                       continue;
1313                     }
1314                   else
1315                     {
1316                       /* The insns starting with this label have been
1317                          already scanned or are in the worklist.  */
1318                       gcc_assert (barrier_args_size[INSN_UID (insn)]
1319                                   == cur_args_size);
1320                       break;
1321                     }
1322                 }
1323
1324               body = PATTERN (insn);
1325               if (GET_CODE (body) == SEQUENCE)
1326                 {
1327                   for (i = 1; i < XVECLEN (body, 0); i++)
1328                     cur_args_size
1329                       = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1330                                                      cur_args_size, &next);
1331                   cur_args_size
1332                     = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1333                                                    cur_args_size, &next);
1334                 }
1335               else
1336                 cur_args_size
1337                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1338             }
1339         }
1340
1341       if (VEC_empty (rtx, next))
1342         break;
1343
1344       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1345       tmp = next;
1346       next = worklist;
1347       worklist = tmp;
1348       VEC_truncate (rtx, next, 0);
1349     }
1350
1351   VEC_free (rtx, heap, worklist);
1352   VEC_free (rtx, heap, next);
1353 }
1354
1355
1356 /* Check INSN to see if it looks like a push or a stack adjustment, and
1357    make a note of it if it does.  EH uses this information to find out how
1358    much extra space it needs to pop off the stack.  */
1359
1360 static void
1361 dwarf2out_stack_adjust (rtx insn, bool after_p)
1362 {
1363   HOST_WIDE_INT offset;
1364   const char *label;
1365   int i;
1366
1367   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1368      with this function.  Proper support would require all frame-related
1369      insns to be marked, and to be able to handle saving state around
1370      epilogues textually in the middle of the function.  */
1371   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1372     return;
1373
1374   /* If only calls can throw, and we have a frame pointer,
1375      save up adjustments until we see the CALL_INSN.  */
1376   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1377     {
1378       if (CALL_P (insn) && !after_p)
1379         {
1380           /* Extract the size of the args from the CALL rtx itself.  */
1381           insn = PATTERN (insn);
1382           if (GET_CODE (insn) == PARALLEL)
1383             insn = XVECEXP (insn, 0, 0);
1384           if (GET_CODE (insn) == SET)
1385             insn = SET_SRC (insn);
1386           gcc_assert (GET_CODE (insn) == CALL);
1387           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1388         }
1389       return;
1390     }
1391
1392   if (CALL_P (insn) && !after_p)
1393     {
1394       if (!flag_asynchronous_unwind_tables)
1395         dwarf2out_args_size ("", args_size);
1396       return;
1397     }
1398   else if (BARRIER_P (insn))
1399     {
1400       /* Don't call compute_barrier_args_size () if the only
1401          BARRIER is at the end of function.  */
1402       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1403         compute_barrier_args_size ();
1404       if (barrier_args_size == NULL)
1405         offset = 0;
1406       else
1407         {
1408           offset = barrier_args_size[INSN_UID (insn)];
1409           if (offset < 0)
1410             offset = 0;
1411         }
1412
1413       offset -= args_size;
1414 #ifndef STACK_GROWS_DOWNWARD
1415       offset = -offset;
1416 #endif
1417     }
1418   else if (GET_CODE (PATTERN (insn)) == SET)
1419     offset = stack_adjust_offset (PATTERN (insn));
1420   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1421            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1422     {
1423       /* There may be stack adjustments inside compound insns.  Search
1424          for them.  */
1425       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1426         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1427           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1428     }
1429   else
1430     return;
1431
1432   if (offset == 0)
1433     return;
1434
1435   label = dwarf2out_cfi_label ();
1436   dwarf2out_args_size_adjust (offset, label);
1437 }
1438
1439 /* Adjust args_size based on stack adjustment OFFSET.  */
1440
1441 static void
1442 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1443 {
1444   if (cfa.reg == STACK_POINTER_REGNUM)
1445     cfa.offset += offset;
1446
1447   if (cfa_store.reg == STACK_POINTER_REGNUM)
1448     cfa_store.offset += offset;
1449
1450 #ifndef STACK_GROWS_DOWNWARD
1451   offset = -offset;
1452 #endif
1453
1454   args_size += offset;
1455   if (args_size < 0)
1456     args_size = 0;
1457
1458   def_cfa_1 (label, &cfa);
1459   if (flag_asynchronous_unwind_tables)
1460     dwarf2out_args_size (label, args_size);
1461 }
1462
1463 #endif
1464
1465 /* We delay emitting a register save until either (a) we reach the end
1466    of the prologue or (b) the register is clobbered.  This clusters
1467    register saves so that there are fewer pc advances.  */
1468
1469 struct queued_reg_save GTY(())
1470 {
1471   struct queued_reg_save *next;
1472   rtx reg;
1473   HOST_WIDE_INT cfa_offset;
1474   rtx saved_reg;
1475 };
1476
1477 static GTY(()) struct queued_reg_save *queued_reg_saves;
1478
1479 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1480 struct reg_saved_in_data GTY(()) {
1481   rtx orig_reg;
1482   rtx saved_in_reg;
1483 };
1484
1485 /* A list of registers saved in other registers.
1486    The list intentionally has a small maximum capacity of 4; if your
1487    port needs more than that, you might consider implementing a
1488    more efficient data structure.  */
1489 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1490 static GTY(()) size_t num_regs_saved_in_regs;
1491
1492 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1493 static const char *last_reg_save_label;
1494
1495 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1496    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1497
1498 static void
1499 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1500 {
1501   struct queued_reg_save *q;
1502
1503   /* Duplicates waste space, but it's also necessary to remove them
1504      for correctness, since the queue gets output in reverse
1505      order.  */
1506   for (q = queued_reg_saves; q != NULL; q = q->next)
1507     if (REGNO (q->reg) == REGNO (reg))
1508       break;
1509
1510   if (q == NULL)
1511     {
1512       q = GGC_NEW (struct queued_reg_save);
1513       q->next = queued_reg_saves;
1514       queued_reg_saves = q;
1515     }
1516
1517   q->reg = reg;
1518   q->cfa_offset = offset;
1519   q->saved_reg = sreg;
1520
1521   last_reg_save_label = label;
1522 }
1523
1524 /* Output all the entries in QUEUED_REG_SAVES.  */
1525
1526 static void
1527 flush_queued_reg_saves (void)
1528 {
1529   struct queued_reg_save *q;
1530
1531   for (q = queued_reg_saves; q; q = q->next)
1532     {
1533       size_t i;
1534       unsigned int reg, sreg;
1535
1536       for (i = 0; i < num_regs_saved_in_regs; i++)
1537         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1538           break;
1539       if (q->saved_reg && i == num_regs_saved_in_regs)
1540         {
1541           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1542           num_regs_saved_in_regs++;
1543         }
1544       if (i != num_regs_saved_in_regs)
1545         {
1546           regs_saved_in_regs[i].orig_reg = q->reg;
1547           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1548         }
1549
1550       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1551       if (q->saved_reg)
1552         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1553       else
1554         sreg = INVALID_REGNUM;
1555       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1556     }
1557
1558   queued_reg_saves = NULL;
1559   last_reg_save_label = NULL;
1560 }
1561
1562 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1563    location for?  Or, does it clobber a register which we've previously
1564    said that some other register is saved in, and for which we now
1565    have a new location for?  */
1566
1567 static bool
1568 clobbers_queued_reg_save (const_rtx insn)
1569 {
1570   struct queued_reg_save *q;
1571
1572   for (q = queued_reg_saves; q; q = q->next)
1573     {
1574       size_t i;
1575       if (modified_in_p (q->reg, insn))
1576         return true;
1577       for (i = 0; i < num_regs_saved_in_regs; i++)
1578         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1579             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1580           return true;
1581     }
1582
1583   return false;
1584 }
1585
1586 /* Entry point for saving the first register into the second.  */
1587
1588 void
1589 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1590 {
1591   size_t i;
1592   unsigned int regno, sregno;
1593
1594   for (i = 0; i < num_regs_saved_in_regs; i++)
1595     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1596       break;
1597   if (i == num_regs_saved_in_regs)
1598     {
1599       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1600       num_regs_saved_in_regs++;
1601     }
1602   regs_saved_in_regs[i].orig_reg = reg;
1603   regs_saved_in_regs[i].saved_in_reg = sreg;
1604
1605   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1606   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1607   reg_save (label, regno, sregno, 0);
1608 }
1609
1610 /* What register, if any, is currently saved in REG?  */
1611
1612 static rtx
1613 reg_saved_in (rtx reg)
1614 {
1615   unsigned int regn = REGNO (reg);
1616   size_t i;
1617   struct queued_reg_save *q;
1618
1619   for (q = queued_reg_saves; q; q = q->next)
1620     if (q->saved_reg && regn == REGNO (q->saved_reg))
1621       return q->reg;
1622
1623   for (i = 0; i < num_regs_saved_in_regs; i++)
1624     if (regs_saved_in_regs[i].saved_in_reg
1625         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1626       return regs_saved_in_regs[i].orig_reg;
1627
1628   return NULL_RTX;
1629 }
1630
1631
1632 /* A temporary register holding an integral value used in adjusting SP
1633    or setting up the store_reg.  The "offset" field holds the integer
1634    value, not an offset.  */
1635 static dw_cfa_location cfa_temp;
1636
1637 /* Record call frame debugging information for an expression EXPR,
1638    which either sets SP or FP (adjusting how we calculate the frame
1639    address) or saves a register to the stack or another register.
1640    LABEL indicates the address of EXPR.
1641
1642    This function encodes a state machine mapping rtxes to actions on
1643    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1644    users need not read the source code.
1645
1646   The High-Level Picture
1647
1648   Changes in the register we use to calculate the CFA: Currently we
1649   assume that if you copy the CFA register into another register, we
1650   should take the other one as the new CFA register; this seems to
1651   work pretty well.  If it's wrong for some target, it's simple
1652   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1653
1654   Changes in the register we use for saving registers to the stack:
1655   This is usually SP, but not always.  Again, we deduce that if you
1656   copy SP into another register (and SP is not the CFA register),
1657   then the new register is the one we will be using for register
1658   saves.  This also seems to work.
1659
1660   Register saves: There's not much guesswork about this one; if
1661   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1662   register save, and the register used to calculate the destination
1663   had better be the one we think we're using for this purpose.
1664   It's also assumed that a copy from a call-saved register to another
1665   register is saving that register if RTX_FRAME_RELATED_P is set on
1666   that instruction.  If the copy is from a call-saved register to
1667   the *same* register, that means that the register is now the same
1668   value as in the caller.
1669
1670   Except: If the register being saved is the CFA register, and the
1671   offset is nonzero, we are saving the CFA, so we assume we have to
1672   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1673   the intent is to save the value of SP from the previous frame.
1674
1675   In addition, if a register has previously been saved to a different
1676   register,
1677
1678   Invariants / Summaries of Rules
1679
1680   cfa          current rule for calculating the CFA.  It usually
1681                consists of a register and an offset.
1682   cfa_store    register used by prologue code to save things to the stack
1683                cfa_store.offset is the offset from the value of
1684                cfa_store.reg to the actual CFA
1685   cfa_temp     register holding an integral value.  cfa_temp.offset
1686                stores the value, which will be used to adjust the
1687                stack pointer.  cfa_temp is also used like cfa_store,
1688                to track stores to the stack via fp or a temp reg.
1689
1690   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1691                with cfa.reg as the first operand changes the cfa.reg and its
1692                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1693                cfa_temp.offset.
1694
1695   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1696                expression yielding a constant.  This sets cfa_temp.reg
1697                and cfa_temp.offset.
1698
1699   Rule 5:      Create a new register cfa_store used to save items to the
1700                stack.
1701
1702   Rules 10-14: Save a register to the stack.  Define offset as the
1703                difference of the original location and cfa_store's
1704                location (or cfa_temp's location if cfa_temp is used).
1705
1706   Rules 16-20: If AND operation happens on sp in prologue, we assume
1707                stack is realigned.  We will use a group of DW_OP_XXX
1708                expressions to represent the location of the stored
1709                register instead of CFA+offset.
1710
1711   The Rules
1712
1713   "{a,b}" indicates a choice of a xor b.
1714   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1715
1716   Rule 1:
1717   (set <reg1> <reg2>:cfa.reg)
1718   effects: cfa.reg = <reg1>
1719            cfa.offset unchanged
1720            cfa_temp.reg = <reg1>
1721            cfa_temp.offset = cfa.offset
1722
1723   Rule 2:
1724   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1725                               {<const_int>,<reg>:cfa_temp.reg}))
1726   effects: cfa.reg = sp if fp used
1727            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1728            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1729              if cfa_store.reg==sp
1730
1731   Rule 3:
1732   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1733   effects: cfa.reg = fp
1734            cfa_offset += +/- <const_int>
1735
1736   Rule 4:
1737   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1738   constraints: <reg1> != fp
1739                <reg1> != sp
1740   effects: cfa.reg = <reg1>
1741            cfa_temp.reg = <reg1>
1742            cfa_temp.offset = cfa.offset
1743
1744   Rule 5:
1745   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1746   constraints: <reg1> != fp
1747                <reg1> != sp
1748   effects: cfa_store.reg = <reg1>
1749            cfa_store.offset = cfa.offset - cfa_temp.offset
1750
1751   Rule 6:
1752   (set <reg> <const_int>)
1753   effects: cfa_temp.reg = <reg>
1754            cfa_temp.offset = <const_int>
1755
1756   Rule 7:
1757   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1758   effects: cfa_temp.reg = <reg1>
1759            cfa_temp.offset |= <const_int>
1760
1761   Rule 8:
1762   (set <reg> (high <exp>))
1763   effects: none
1764
1765   Rule 9:
1766   (set <reg> (lo_sum <exp> <const_int>))
1767   effects: cfa_temp.reg = <reg>
1768            cfa_temp.offset = <const_int>
1769
1770   Rule 10:
1771   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1772   effects: cfa_store.offset -= <const_int>
1773            cfa.offset = cfa_store.offset if cfa.reg == sp
1774            cfa.reg = sp
1775            cfa.base_offset = -cfa_store.offset
1776
1777   Rule 11:
1778   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1779   effects: cfa_store.offset += -/+ mode_size(mem)
1780            cfa.offset = cfa_store.offset if cfa.reg == sp
1781            cfa.reg = sp
1782            cfa.base_offset = -cfa_store.offset
1783
1784   Rule 12:
1785   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1786
1787        <reg2>)
1788   effects: cfa.reg = <reg1>
1789            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1790
1791   Rule 13:
1792   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1793   effects: cfa.reg = <reg1>
1794            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1795
1796   Rule 14:
1797   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1798   effects: cfa.reg = <reg1>
1799            cfa.base_offset = -cfa_temp.offset
1800            cfa_temp.offset -= mode_size(mem)
1801
1802   Rule 15:
1803   (set <reg> {unspec, unspec_volatile})
1804   effects: target-dependent
1805
1806   Rule 16:
1807   (set sp (and: sp <const_int>))
1808   constraints: cfa_store.reg == sp
1809   effects: current_fde.stack_realign = 1
1810            cfa_store.offset = 0
1811            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1812
1813   Rule 17:
1814   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1815   effects: cfa_store.offset += -/+ mode_size(mem)
1816
1817   Rule 18:
1818   (set (mem ({pre_inc, pre_dec} sp)) fp)
1819   constraints: fde->stack_realign == 1
1820   effects: cfa_store.offset = 0
1821            cfa.reg != HARD_FRAME_POINTER_REGNUM
1822
1823   Rule 19:
1824   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1825   constraints: fde->stack_realign == 1
1826                && cfa.offset == 0
1827                && cfa.indirect == 0
1828                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1829   effects: Use DW_CFA_def_cfa_expression to define cfa
1830            cfa.reg == fde->drap_reg
1831
1832   Rule 20:
1833   (set reg fde->drap_reg)
1834   constraints: fde->vdrap_reg == INVALID_REGNUM
1835   effects: fde->vdrap_reg = reg.
1836   (set mem fde->drap_reg)
1837   constraints: fde->drap_reg_saved == 1
1838   effects: none.  */
1839
1840 static void
1841 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1842 {
1843   rtx src, dest, span;
1844   HOST_WIDE_INT offset;
1845   dw_fde_ref fde;
1846
1847   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1848      the PARALLEL independently. The first element is always processed if
1849      it is a SET. This is for backward compatibility.   Other elements
1850      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1851      flag is set in them.  */
1852   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1853     {
1854       int par_index;
1855       int limit = XVECLEN (expr, 0);
1856       rtx elem;
1857
1858       /* PARALLELs have strict read-modify-write semantics, so we
1859          ought to evaluate every rvalue before changing any lvalue.
1860          It's cumbersome to do that in general, but there's an
1861          easy approximation that is enough for all current users:
1862          handle register saves before register assignments.  */
1863       if (GET_CODE (expr) == PARALLEL)
1864         for (par_index = 0; par_index < limit; par_index++)
1865           {
1866             elem = XVECEXP (expr, 0, par_index);
1867             if (GET_CODE (elem) == SET
1868                 && MEM_P (SET_DEST (elem))
1869                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1870               dwarf2out_frame_debug_expr (elem, label);
1871           }
1872
1873       for (par_index = 0; par_index < limit; par_index++)
1874         {
1875           elem = XVECEXP (expr, 0, par_index);
1876           if (GET_CODE (elem) == SET
1877               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1878               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1879             dwarf2out_frame_debug_expr (elem, label);
1880           else if (GET_CODE (elem) == SET
1881                    && par_index != 0
1882                    && !RTX_FRAME_RELATED_P (elem))
1883             {
1884               /* Stack adjustment combining might combine some post-prologue
1885                  stack adjustment into a prologue stack adjustment.  */
1886               HOST_WIDE_INT offset = stack_adjust_offset (elem);
1887
1888               if (offset != 0)
1889                 dwarf2out_args_size_adjust (offset, label);
1890             }
1891         }
1892       return;
1893     }
1894
1895   gcc_assert (GET_CODE (expr) == SET);
1896
1897   src = SET_SRC (expr);
1898   dest = SET_DEST (expr);
1899
1900   if (REG_P (src))
1901     {
1902       rtx rsi = reg_saved_in (src);
1903       if (rsi)
1904         src = rsi;
1905     }
1906
1907   fde = current_fde ();
1908
1909   if (GET_CODE (src) == REG
1910       && fde
1911       && fde->drap_reg == REGNO (src)
1912       && (fde->drap_reg_saved
1913           || GET_CODE (dest) == REG))
1914     {
1915       /* Rule 20 */
1916       /* If we are saving dynamic realign argument pointer to a
1917          register, the destination is virtual dynamic realign
1918          argument pointer.  It may be used to access argument.  */
1919       if (GET_CODE (dest) == REG)
1920         {
1921           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1922           fde->vdrap_reg = REGNO (dest);
1923         }
1924       return;
1925     }
1926
1927   switch (GET_CODE (dest))
1928     {
1929     case REG:
1930       switch (GET_CODE (src))
1931         {
1932           /* Setting FP from SP.  */
1933         case REG:
1934           if (cfa.reg == (unsigned) REGNO (src))
1935             {
1936               /* Rule 1 */
1937               /* Update the CFA rule wrt SP or FP.  Make sure src is
1938                  relative to the current CFA register.
1939
1940                  We used to require that dest be either SP or FP, but the
1941                  ARM copies SP to a temporary register, and from there to
1942                  FP.  So we just rely on the backends to only set
1943                  RTX_FRAME_RELATED_P on appropriate insns.  */
1944               cfa.reg = REGNO (dest);
1945               cfa_temp.reg = cfa.reg;
1946               cfa_temp.offset = cfa.offset;
1947             }
1948           else
1949             {
1950               /* Saving a register in a register.  */
1951               gcc_assert (!fixed_regs [REGNO (dest)]
1952                           /* For the SPARC and its register window.  */
1953                           || (DWARF_FRAME_REGNUM (REGNO (src))
1954                               == DWARF_FRAME_RETURN_COLUMN));
1955
1956               /* After stack is aligned, we can only save SP in FP
1957                  if drap register is used.  In this case, we have
1958                  to restore stack pointer with the CFA value and we
1959                  don't generate this DWARF information.  */
1960               if (fde
1961                   && fde->stack_realign
1962                   && REGNO (src) == STACK_POINTER_REGNUM)
1963                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1964                             && fde->drap_reg != INVALID_REGNUM
1965                             && cfa.reg != REGNO (src));
1966               else
1967                 queue_reg_save (label, src, dest, 0);
1968             }
1969           break;
1970
1971         case PLUS:
1972         case MINUS:
1973         case LO_SUM:
1974           if (dest == stack_pointer_rtx)
1975             {
1976               /* Rule 2 */
1977               /* Adjusting SP.  */
1978               switch (GET_CODE (XEXP (src, 1)))
1979                 {
1980                 case CONST_INT:
1981                   offset = INTVAL (XEXP (src, 1));
1982                   break;
1983                 case REG:
1984                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1985                               == cfa_temp.reg);
1986                   offset = cfa_temp.offset;
1987                   break;
1988                 default:
1989                   gcc_unreachable ();
1990                 }
1991
1992               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1993                 {
1994                   /* Restoring SP from FP in the epilogue.  */
1995                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1996                   cfa.reg = STACK_POINTER_REGNUM;
1997                 }
1998               else if (GET_CODE (src) == LO_SUM)
1999                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2000                 ;
2001               else
2002                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2003
2004               if (GET_CODE (src) != MINUS)
2005                 offset = -offset;
2006               if (cfa.reg == STACK_POINTER_REGNUM)
2007                 cfa.offset += offset;
2008               if (cfa_store.reg == STACK_POINTER_REGNUM)
2009                 cfa_store.offset += offset;
2010             }
2011           else if (dest == hard_frame_pointer_rtx)
2012             {
2013               /* Rule 3 */
2014               /* Either setting the FP from an offset of the SP,
2015                  or adjusting the FP */
2016               gcc_assert (frame_pointer_needed);
2017
2018               gcc_assert (REG_P (XEXP (src, 0))
2019                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2020                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2021               offset = INTVAL (XEXP (src, 1));
2022               if (GET_CODE (src) != MINUS)
2023                 offset = -offset;
2024               cfa.offset += offset;
2025               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2026             }
2027           else
2028             {
2029               gcc_assert (GET_CODE (src) != MINUS);
2030
2031               /* Rule 4 */
2032               if (REG_P (XEXP (src, 0))
2033                   && REGNO (XEXP (src, 0)) == cfa.reg
2034                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2035                 {
2036                   /* Setting a temporary CFA register that will be copied
2037                      into the FP later on.  */
2038                   offset = - INTVAL (XEXP (src, 1));
2039                   cfa.offset += offset;
2040                   cfa.reg = REGNO (dest);
2041                   /* Or used to save regs to the stack.  */
2042                   cfa_temp.reg = cfa.reg;
2043                   cfa_temp.offset = cfa.offset;
2044                 }
2045
2046               /* Rule 5 */
2047               else if (REG_P (XEXP (src, 0))
2048                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2049                        && XEXP (src, 1) == stack_pointer_rtx)
2050                 {
2051                   /* Setting a scratch register that we will use instead
2052                      of SP for saving registers to the stack.  */
2053                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2054                   cfa_store.reg = REGNO (dest);
2055                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2056                 }
2057
2058               /* Rule 9 */
2059               else if (GET_CODE (src) == LO_SUM
2060                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2061                 {
2062                   cfa_temp.reg = REGNO (dest);
2063                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2064                 }
2065               else
2066                 gcc_unreachable ();
2067             }
2068           break;
2069
2070           /* Rule 6 */
2071         case CONST_INT:
2072           cfa_temp.reg = REGNO (dest);
2073           cfa_temp.offset = INTVAL (src);
2074           break;
2075
2076           /* Rule 7 */
2077         case IOR:
2078           gcc_assert (REG_P (XEXP (src, 0))
2079                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2080                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2081
2082           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2083             cfa_temp.reg = REGNO (dest);
2084           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2085           break;
2086
2087           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2088              which will fill in all of the bits.  */
2089           /* Rule 8 */
2090         case HIGH:
2091           break;
2092
2093           /* Rule 15 */
2094         case UNSPEC:
2095         case UNSPEC_VOLATILE:
2096           gcc_assert (targetm.dwarf_handle_frame_unspec);
2097           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2098           return;
2099
2100           /* Rule 16 */
2101         case AND:
2102           /* If this AND operation happens on stack pointer in prologue,
2103              we assume the stack is realigned and we extract the
2104              alignment.  */
2105           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2106             {
2107               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2108               fde->stack_realign = 1;
2109               fde->stack_realignment = INTVAL (XEXP (src, 1));
2110               cfa_store.offset = 0;
2111
2112               if (cfa.reg != STACK_POINTER_REGNUM
2113                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2114                 fde->drap_reg = cfa.reg;
2115             }
2116           return;
2117
2118         default:
2119           gcc_unreachable ();
2120         }
2121
2122       def_cfa_1 (label, &cfa);
2123       break;
2124
2125     case MEM:
2126
2127       /* Saving a register to the stack.  Make sure dest is relative to the
2128          CFA register.  */
2129       switch (GET_CODE (XEXP (dest, 0)))
2130         {
2131           /* Rule 10 */
2132           /* With a push.  */
2133         case PRE_MODIFY:
2134           /* We can't handle variable size modifications.  */
2135           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2136                       == CONST_INT);
2137           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2138
2139           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2140                       && cfa_store.reg == STACK_POINTER_REGNUM);
2141
2142           cfa_store.offset += offset;
2143           if (cfa.reg == STACK_POINTER_REGNUM)
2144             cfa.offset = cfa_store.offset;
2145
2146           offset = -cfa_store.offset;
2147           break;
2148
2149           /* Rule 11 */
2150         case PRE_INC:
2151         case PRE_DEC:
2152           offset = GET_MODE_SIZE (GET_MODE (dest));
2153           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2154             offset = -offset;
2155
2156           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2157                        == STACK_POINTER_REGNUM)
2158                       && cfa_store.reg == STACK_POINTER_REGNUM);
2159
2160           cfa_store.offset += offset;
2161
2162           /* Rule 18: If stack is aligned, we will use FP as a
2163              reference to represent the address of the stored
2164              regiser.  */
2165           if (fde
2166               && fde->stack_realign
2167               && src == hard_frame_pointer_rtx)
2168             {
2169               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2170               cfa_store.offset = 0;
2171             }
2172
2173           if (cfa.reg == STACK_POINTER_REGNUM)
2174             cfa.offset = cfa_store.offset;
2175
2176           offset = -cfa_store.offset;
2177           break;
2178
2179           /* Rule 12 */
2180           /* With an offset.  */
2181         case PLUS:
2182         case MINUS:
2183         case LO_SUM:
2184           {
2185             int regno;
2186
2187             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2188                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2189             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2190             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2191               offset = -offset;
2192
2193             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2194
2195             if (cfa_store.reg == (unsigned) regno)
2196               offset -= cfa_store.offset;
2197             else
2198               {
2199                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2200                 offset -= cfa_temp.offset;
2201               }
2202           }
2203           break;
2204
2205           /* Rule 13 */
2206           /* Without an offset.  */
2207         case REG:
2208           {
2209             int regno = REGNO (XEXP (dest, 0));
2210
2211             if (cfa_store.reg == (unsigned) regno)
2212               offset = -cfa_store.offset;
2213             else
2214               {
2215                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2216                 offset = -cfa_temp.offset;
2217               }
2218           }
2219           break;
2220
2221           /* Rule 14 */
2222         case POST_INC:
2223           gcc_assert (cfa_temp.reg
2224                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2225           offset = -cfa_temp.offset;
2226           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2227           break;
2228
2229         default:
2230           gcc_unreachable ();
2231         }
2232
2233         /* Rule 17 */
2234         /* If the source operand of this MEM operation is not a
2235            register, basically the source is return address.  Here
2236            we only care how much stack grew and we don't save it.  */
2237       if (!REG_P (src))
2238         break;
2239
2240       if (REGNO (src) != STACK_POINTER_REGNUM
2241           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2242           && (unsigned) REGNO (src) == cfa.reg)
2243         {
2244           /* We're storing the current CFA reg into the stack.  */
2245
2246           if (cfa.offset == 0)
2247             {
2248               /* Rule 19 */
2249               /* If stack is aligned, putting CFA reg into stack means
2250                  we can no longer use reg + offset to represent CFA.
2251                  Here we use DW_CFA_def_cfa_expression instead.  The
2252                  result of this expression equals to the original CFA
2253                  value.  */
2254               if (fde
2255                   && fde->stack_realign
2256                   && cfa.indirect == 0
2257                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2258                 {
2259                   dw_cfa_location cfa_exp;
2260
2261                   gcc_assert (fde->drap_reg == cfa.reg);
2262
2263                   cfa_exp.indirect = 1;
2264                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2265                   cfa_exp.base_offset = offset;
2266                   cfa_exp.offset = 0;
2267
2268                   fde->drap_reg_saved = 1;
2269
2270                   def_cfa_1 (label, &cfa_exp);
2271                   break;
2272                 }
2273
2274               /* If the source register is exactly the CFA, assume
2275                  we're saving SP like any other register; this happens
2276                  on the ARM.  */
2277               def_cfa_1 (label, &cfa);
2278               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2279               break;
2280             }
2281           else
2282             {
2283               /* Otherwise, we'll need to look in the stack to
2284                  calculate the CFA.  */
2285               rtx x = XEXP (dest, 0);
2286
2287               if (!REG_P (x))
2288                 x = XEXP (x, 0);
2289               gcc_assert (REG_P (x));
2290
2291               cfa.reg = REGNO (x);
2292               cfa.base_offset = offset;
2293               cfa.indirect = 1;
2294               def_cfa_1 (label, &cfa);
2295               break;
2296             }
2297         }
2298
2299       def_cfa_1 (label, &cfa);
2300       {
2301         span = targetm.dwarf_register_span (src);
2302
2303         if (!span)
2304           queue_reg_save (label, src, NULL_RTX, offset);
2305         else
2306           {
2307             /* We have a PARALLEL describing where the contents of SRC
2308                live.  Queue register saves for each piece of the
2309                PARALLEL.  */
2310             int par_index;
2311             int limit;
2312             HOST_WIDE_INT span_offset = offset;
2313
2314             gcc_assert (GET_CODE (span) == PARALLEL);
2315
2316             limit = XVECLEN (span, 0);
2317             for (par_index = 0; par_index < limit; par_index++)
2318               {
2319                 rtx elem = XVECEXP (span, 0, par_index);
2320
2321                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2322                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2323               }
2324           }
2325       }
2326       break;
2327
2328     default:
2329       gcc_unreachable ();
2330     }
2331 }
2332
2333 /* Record call frame debugging information for INSN, which either
2334    sets SP or FP (adjusting how we calculate the frame address) or saves a
2335    register to the stack.  If INSN is NULL_RTX, initialize our state.
2336
2337    If AFTER_P is false, we're being called before the insn is emitted,
2338    otherwise after.  Call instructions get invoked twice.  */
2339
2340 void
2341 dwarf2out_frame_debug (rtx insn, bool after_p)
2342 {
2343   const char *label;
2344   rtx src;
2345
2346   if (insn == NULL_RTX)
2347     {
2348       size_t i;
2349
2350       /* Flush any queued register saves.  */
2351       flush_queued_reg_saves ();
2352
2353       /* Set up state for generating call frame debug info.  */
2354       lookup_cfa (&cfa);
2355       gcc_assert (cfa.reg
2356                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2357
2358       cfa.reg = STACK_POINTER_REGNUM;
2359       cfa_store = cfa;
2360       cfa_temp.reg = -1;
2361       cfa_temp.offset = 0;
2362
2363       for (i = 0; i < num_regs_saved_in_regs; i++)
2364         {
2365           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2366           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2367         }
2368       num_regs_saved_in_regs = 0;
2369
2370       if (barrier_args_size)
2371         {
2372           XDELETEVEC (barrier_args_size);
2373           barrier_args_size = NULL;
2374         }
2375       return;
2376     }
2377
2378   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2379     flush_queued_reg_saves ();
2380
2381   if (! RTX_FRAME_RELATED_P (insn))
2382     {
2383       if (!ACCUMULATE_OUTGOING_ARGS)
2384         dwarf2out_stack_adjust (insn, after_p);
2385       return;
2386     }
2387
2388   label = dwarf2out_cfi_label ();
2389   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2390   if (src)
2391     insn = XEXP (src, 0);
2392   else
2393     insn = PATTERN (insn);
2394
2395   dwarf2out_frame_debug_expr (insn, label);
2396 }
2397
2398 #endif
2399
2400 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2401 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2402  (enum dwarf_call_frame_info cfi);
2403
2404 static enum dw_cfi_oprnd_type
2405 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2406 {
2407   switch (cfi)
2408     {
2409     case DW_CFA_nop:
2410     case DW_CFA_GNU_window_save:
2411       return dw_cfi_oprnd_unused;
2412
2413     case DW_CFA_set_loc:
2414     case DW_CFA_advance_loc1:
2415     case DW_CFA_advance_loc2:
2416     case DW_CFA_advance_loc4:
2417     case DW_CFA_MIPS_advance_loc8:
2418       return dw_cfi_oprnd_addr;
2419
2420     case DW_CFA_offset:
2421     case DW_CFA_offset_extended:
2422     case DW_CFA_def_cfa:
2423     case DW_CFA_offset_extended_sf:
2424     case DW_CFA_def_cfa_sf:
2425     case DW_CFA_restore_extended:
2426     case DW_CFA_undefined:
2427     case DW_CFA_same_value:
2428     case DW_CFA_def_cfa_register:
2429     case DW_CFA_register:
2430       return dw_cfi_oprnd_reg_num;
2431
2432     case DW_CFA_def_cfa_offset:
2433     case DW_CFA_GNU_args_size:
2434     case DW_CFA_def_cfa_offset_sf:
2435       return dw_cfi_oprnd_offset;
2436
2437     case DW_CFA_def_cfa_expression:
2438     case DW_CFA_expression:
2439       return dw_cfi_oprnd_loc;
2440
2441     default:
2442       gcc_unreachable ();
2443     }
2444 }
2445
2446 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2447 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2448  (enum dwarf_call_frame_info cfi);
2449
2450 static enum dw_cfi_oprnd_type
2451 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2452 {
2453   switch (cfi)
2454     {
2455     case DW_CFA_def_cfa:
2456     case DW_CFA_def_cfa_sf:
2457     case DW_CFA_offset:
2458     case DW_CFA_offset_extended_sf:
2459     case DW_CFA_offset_extended:
2460       return dw_cfi_oprnd_offset;
2461
2462     case DW_CFA_register:
2463       return dw_cfi_oprnd_reg_num;
2464
2465     default:
2466       return dw_cfi_oprnd_unused;
2467     }
2468 }
2469
2470 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2471
2472 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2473    switch to the data section instead, and write out a synthetic label
2474    for collect2.  */
2475
2476 static void
2477 switch_to_eh_frame_section (void)
2478 {
2479   tree label;
2480
2481 #ifdef EH_FRAME_SECTION_NAME
2482   if (eh_frame_section == 0)
2483     {
2484       int flags;
2485
2486       if (EH_TABLES_CAN_BE_READ_ONLY)
2487         {
2488           int fde_encoding;
2489           int per_encoding;
2490           int lsda_encoding;
2491
2492           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2493                                                        /*global=*/0);
2494           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2495                                                        /*global=*/1);
2496           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2497                                                         /*global=*/0);
2498           flags = ((! flag_pic
2499                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2500                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2501                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2502                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2503                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2504                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2505                    ? 0 : SECTION_WRITE);
2506         }
2507       else
2508         flags = SECTION_WRITE;
2509       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2510     }
2511 #endif
2512
2513   if (eh_frame_section)
2514     switch_to_section (eh_frame_section);
2515   else
2516     {
2517       /* We have no special eh_frame section.  Put the information in
2518          the data section and emit special labels to guide collect2.  */
2519       switch_to_section (data_section);
2520       label = get_file_function_name ("F");
2521       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2522       targetm.asm_out.globalize_label (asm_out_file,
2523                                        IDENTIFIER_POINTER (label));
2524       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2525     }
2526 }
2527
2528 /* Output a Call Frame Information opcode and its operand(s).  */
2529
2530 static void
2531 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2532 {
2533   unsigned long r;
2534   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2535     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2536                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2537                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2538                          ((unsigned HOST_WIDE_INT)
2539                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2540   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2541     {
2542       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2543       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2544                            "DW_CFA_offset, column 0x%lx", r);
2545       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2546     }
2547   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2548     {
2549       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2550       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2551                            "DW_CFA_restore, column 0x%lx", r);
2552     }
2553   else
2554     {
2555       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2556                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2557
2558       switch (cfi->dw_cfi_opc)
2559         {
2560         case DW_CFA_set_loc:
2561           if (for_eh)
2562             dw2_asm_output_encoded_addr_rtx (
2563                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2564                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2565                 false, NULL);
2566           else
2567             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2568                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2569           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2570           break;
2571
2572         case DW_CFA_advance_loc1:
2573           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2574                                 fde->dw_fde_current_label, NULL);
2575           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2576           break;
2577
2578         case DW_CFA_advance_loc2:
2579           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2580                                 fde->dw_fde_current_label, NULL);
2581           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2582           break;
2583
2584         case DW_CFA_advance_loc4:
2585           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2586                                 fde->dw_fde_current_label, NULL);
2587           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2588           break;
2589
2590         case DW_CFA_MIPS_advance_loc8:
2591           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2592                                 fde->dw_fde_current_label, NULL);
2593           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2594           break;
2595
2596         case DW_CFA_offset_extended:
2597         case DW_CFA_def_cfa:
2598           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2599           dw2_asm_output_data_uleb128 (r, NULL);
2600           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2601           break;
2602
2603         case DW_CFA_offset_extended_sf:
2604         case DW_CFA_def_cfa_sf:
2605           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2606           dw2_asm_output_data_uleb128 (r, NULL);
2607           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2608           break;
2609
2610         case DW_CFA_restore_extended:
2611         case DW_CFA_undefined:
2612         case DW_CFA_same_value:
2613         case DW_CFA_def_cfa_register:
2614           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2615           dw2_asm_output_data_uleb128 (r, NULL);
2616           break;
2617
2618         case DW_CFA_register:
2619           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2620           dw2_asm_output_data_uleb128 (r, NULL);
2621           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2622           dw2_asm_output_data_uleb128 (r, NULL);
2623           break;
2624
2625         case DW_CFA_def_cfa_offset:
2626         case DW_CFA_GNU_args_size:
2627           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2628           break;
2629
2630         case DW_CFA_def_cfa_offset_sf:
2631           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2632           break;
2633
2634         case DW_CFA_GNU_window_save:
2635           break;
2636
2637         case DW_CFA_def_cfa_expression:
2638         case DW_CFA_expression:
2639           output_cfa_loc (cfi);
2640           break;
2641
2642         case DW_CFA_GNU_negative_offset_extended:
2643           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2644           gcc_unreachable ();
2645
2646         default:
2647           break;
2648         }
2649     }
2650 }
2651
2652 /* Similar, but do it via assembler directives instead.  */
2653
2654 static void
2655 output_cfi_directive (dw_cfi_ref cfi)
2656 {
2657   unsigned long r, r2;
2658
2659   switch (cfi->dw_cfi_opc)
2660     {
2661     case DW_CFA_advance_loc:
2662     case DW_CFA_advance_loc1:
2663     case DW_CFA_advance_loc2:
2664     case DW_CFA_advance_loc4:
2665     case DW_CFA_MIPS_advance_loc8:
2666     case DW_CFA_set_loc:
2667       /* Should only be created by add_fde_cfi in a code path not
2668          followed when emitting via directives.  The assembler is
2669          going to take care of this for us.  */
2670       gcc_unreachable ();
2671
2672     case DW_CFA_offset:
2673     case DW_CFA_offset_extended:
2674     case DW_CFA_offset_extended_sf:
2675       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2676       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2677                r, cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT);
2678       break;
2679
2680     case DW_CFA_restore:
2681     case DW_CFA_restore_extended:
2682       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2683       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2684       break;
2685
2686     case DW_CFA_undefined:
2687       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2688       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2689       break;
2690
2691     case DW_CFA_same_value:
2692       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2693       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2694       break;
2695
2696     case DW_CFA_def_cfa:
2697     case DW_CFA_def_cfa_sf:
2698       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2699       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2700                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2701       break;
2702
2703     case DW_CFA_def_cfa_register:
2704       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2705       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2706       break;
2707
2708     case DW_CFA_register:
2709       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2710       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2711       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2712       break;
2713
2714     case DW_CFA_def_cfa_offset:
2715     case DW_CFA_def_cfa_offset_sf:
2716       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2717                HOST_WIDE_INT_PRINT_DEC"\n",
2718                cfi->dw_cfi_oprnd1.dw_cfi_offset);
2719       break;
2720
2721     case DW_CFA_GNU_args_size:
2722       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2723       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2724       if (flag_debug_asm)
2725         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2726                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2727       fputc ('\n', asm_out_file);
2728       break;
2729
2730     case DW_CFA_GNU_window_save:
2731       fprintf (asm_out_file, "\t.cfi_window_save\n");
2732       break;
2733
2734     case DW_CFA_def_cfa_expression:
2735     case DW_CFA_expression:
2736       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2737       output_cfa_loc_raw (cfi);
2738       fputc ('\n', asm_out_file);
2739       break;
2740
2741     default:
2742       gcc_unreachable ();
2743     }
2744 }
2745
2746 /* Output the call frame information used to record information
2747    that relates to calculating the frame pointer, and records the
2748    location of saved registers.  */
2749
2750 static void
2751 output_call_frame_info (int for_eh)
2752 {
2753   unsigned int i;
2754   dw_fde_ref fde;
2755   dw_cfi_ref cfi;
2756   char l1[20], l2[20], section_start_label[20];
2757   bool any_lsda_needed = false;
2758   char augmentation[6];
2759   int augmentation_size;
2760   int fde_encoding = DW_EH_PE_absptr;
2761   int per_encoding = DW_EH_PE_absptr;
2762   int lsda_encoding = DW_EH_PE_absptr;
2763   int return_reg;
2764
2765   /* Don't emit a CIE if there won't be any FDEs.  */
2766   if (fde_table_in_use == 0)
2767     return;
2768
2769   /* Nothing to do if the assembler's doing it all.  */
2770   if (flag_dwarf2_cfi_asm)
2771     return;
2772
2773   /* If we make FDEs linkonce, we may have to emit an empty label for
2774      an FDE that wouldn't otherwise be emitted.  We want to avoid
2775      having an FDE kept around when the function it refers to is
2776      discarded.  Example where this matters: a primary function
2777      template in C++ requires EH information, but an explicit
2778      specialization doesn't.  */
2779   if (TARGET_USES_WEAK_UNWIND_INFO
2780       && ! flag_asynchronous_unwind_tables
2781       && flag_exceptions
2782       && for_eh)
2783     for (i = 0; i < fde_table_in_use; i++)
2784       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2785           && !fde_table[i].uses_eh_lsda
2786           && ! DECL_WEAK (fde_table[i].decl))
2787         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2788                                       for_eh, /* empty */ 1);
2789
2790   /* If we don't have any functions we'll want to unwind out of, don't
2791      emit any EH unwind information.  Note that if exceptions aren't
2792      enabled, we won't have collected nothrow information, and if we
2793      asked for asynchronous tables, we always want this info.  */
2794   if (for_eh)
2795     {
2796       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2797
2798       for (i = 0; i < fde_table_in_use; i++)
2799         if (fde_table[i].uses_eh_lsda)
2800           any_eh_needed = any_lsda_needed = true;
2801         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2802           any_eh_needed = true;
2803         else if (! fde_table[i].nothrow
2804                  && ! fde_table[i].all_throwers_are_sibcalls)
2805           any_eh_needed = true;
2806
2807       if (! any_eh_needed)
2808         return;
2809     }
2810
2811   /* We're going to be generating comments, so turn on app.  */
2812   if (flag_debug_asm)
2813     app_enable ();
2814
2815   if (for_eh)
2816     switch_to_eh_frame_section ();
2817   else
2818     {
2819       if (!debug_frame_section)
2820         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2821                                            SECTION_DEBUG, NULL);
2822       switch_to_section (debug_frame_section);
2823     }
2824
2825   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2826   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2827
2828   /* Output the CIE.  */
2829   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2830   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2831   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2832     dw2_asm_output_data (4, 0xffffffff,
2833       "Initial length escape value indicating 64-bit DWARF extension");
2834   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2835                         "Length of Common Information Entry");
2836   ASM_OUTPUT_LABEL (asm_out_file, l1);
2837
2838   /* Now that the CIE pointer is PC-relative for EH,
2839      use 0 to identify the CIE.  */
2840   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2841                        (for_eh ? 0 : DWARF_CIE_ID),
2842                        "CIE Identifier Tag");
2843
2844   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2845
2846   augmentation[0] = 0;
2847   augmentation_size = 0;
2848   if (for_eh)
2849     {
2850       char *p;
2851
2852       /* Augmentation:
2853          z      Indicates that a uleb128 is present to size the
2854                 augmentation section.
2855          L      Indicates the encoding (and thus presence) of
2856                 an LSDA pointer in the FDE augmentation.
2857          R      Indicates a non-default pointer encoding for
2858                 FDE code pointers.
2859          P      Indicates the presence of an encoding + language
2860                 personality routine in the CIE augmentation.  */
2861
2862       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2863       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2864       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2865
2866       p = augmentation + 1;
2867       if (eh_personality_libfunc)
2868         {
2869           *p++ = 'P';
2870           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2871           assemble_external_libcall (eh_personality_libfunc);
2872         }
2873       if (any_lsda_needed)
2874         {
2875           *p++ = 'L';
2876           augmentation_size += 1;
2877         }
2878       if (fde_encoding != DW_EH_PE_absptr)
2879         {
2880           *p++ = 'R';
2881           augmentation_size += 1;
2882         }
2883       if (p > augmentation + 1)
2884         {
2885           augmentation[0] = 'z';
2886           *p = '\0';
2887         }
2888
2889       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2890       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2891         {
2892           int offset = (  4             /* Length */
2893                         + 4             /* CIE Id */
2894                         + 1             /* CIE version */
2895                         + strlen (augmentation) + 1     /* Augmentation */
2896                         + size_of_uleb128 (1)           /* Code alignment */
2897                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2898                         + 1             /* RA column */
2899                         + 1             /* Augmentation size */
2900                         + 1             /* Personality encoding */ );
2901           int pad = -offset & (PTR_SIZE - 1);
2902
2903           augmentation_size += pad;
2904
2905           /* Augmentations should be small, so there's scarce need to
2906              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2907           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2908         }
2909     }
2910
2911   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2912   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2913   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2914                                "CIE Data Alignment Factor");
2915
2916   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2917   if (DW_CIE_VERSION == 1)
2918     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2919   else
2920     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2921
2922   if (augmentation[0])
2923     {
2924       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2925       if (eh_personality_libfunc)
2926         {
2927           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2928                                eh_data_format_name (per_encoding));
2929           dw2_asm_output_encoded_addr_rtx (per_encoding,
2930                                            eh_personality_libfunc,
2931                                            true, NULL);
2932         }
2933
2934       if (any_lsda_needed)
2935         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2936                              eh_data_format_name (lsda_encoding));
2937
2938       if (fde_encoding != DW_EH_PE_absptr)
2939         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2940                              eh_data_format_name (fde_encoding));
2941     }
2942
2943   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2944     output_cfi (cfi, NULL, for_eh);
2945
2946   /* Pad the CIE out to an address sized boundary.  */
2947   ASM_OUTPUT_ALIGN (asm_out_file,
2948                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2949   ASM_OUTPUT_LABEL (asm_out_file, l2);
2950
2951   /* Loop through all of the FDE's.  */
2952   for (i = 0; i < fde_table_in_use; i++)
2953     {
2954       fde = &fde_table[i];
2955
2956       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2957       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2958           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2959           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2960           && !fde->uses_eh_lsda)
2961         continue;
2962
2963       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2964       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2965       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2966       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2967       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2968         dw2_asm_output_data (4, 0xffffffff,
2969                              "Initial length escape value indicating 64-bit DWARF extension");
2970       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2971                             "FDE Length");
2972       ASM_OUTPUT_LABEL (asm_out_file, l1);
2973
2974       if (for_eh)
2975         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2976       else
2977         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2978                                debug_frame_section, "FDE CIE offset");
2979
2980       if (for_eh)
2981         {
2982           if (fde->dw_fde_switched_sections)
2983             {
2984               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2985                                       fde->dw_fde_unlikely_section_label);
2986               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2987                                       fde->dw_fde_hot_section_label);
2988               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2989               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2990               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2991                                                "FDE initial location");
2992               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2993                                     fde->dw_fde_hot_section_end_label,
2994                                     fde->dw_fde_hot_section_label,
2995                                     "FDE address range");
2996               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2997                                                "FDE initial location");
2998               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2999                                     fde->dw_fde_unlikely_section_end_label,
3000                                     fde->dw_fde_unlikely_section_label,
3001                                     "FDE address range");
3002             }
3003           else
3004             {
3005               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3006               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3007               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3008                                                sym_ref,
3009                                                false,
3010                                                "FDE initial location");
3011               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3012                                     fde->dw_fde_end, fde->dw_fde_begin,
3013                                     "FDE address range");
3014             }
3015         }
3016       else
3017         {
3018           if (fde->dw_fde_switched_sections)
3019             {
3020               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3021                                    fde->dw_fde_hot_section_label,
3022                                    "FDE initial location");
3023               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3024                                     fde->dw_fde_hot_section_end_label,
3025                                     fde->dw_fde_hot_section_label,
3026                                     "FDE address range");
3027               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3028                                    fde->dw_fde_unlikely_section_label,
3029                                    "FDE initial location");
3030               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3031                                     fde->dw_fde_unlikely_section_end_label,
3032                                     fde->dw_fde_unlikely_section_label,
3033                                     "FDE address range");
3034             }
3035           else
3036             {
3037               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3038                                    "FDE initial location");
3039               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3040                                     fde->dw_fde_end, fde->dw_fde_begin,
3041                                     "FDE address range");
3042             }
3043         }
3044
3045       if (augmentation[0])
3046         {
3047           if (any_lsda_needed)
3048             {
3049               int size = size_of_encoded_value (lsda_encoding);
3050
3051               if (lsda_encoding == DW_EH_PE_aligned)
3052                 {
3053                   int offset = (  4             /* Length */
3054                                 + 4             /* CIE offset */
3055                                 + 2 * size_of_encoded_value (fde_encoding)
3056                                 + 1             /* Augmentation size */ );
3057                   int pad = -offset & (PTR_SIZE - 1);
3058
3059                   size += pad;
3060                   gcc_assert (size_of_uleb128 (size) == 1);
3061                 }
3062
3063               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3064
3065               if (fde->uses_eh_lsda)
3066                 {
3067                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3068                                                fde->funcdef_number);
3069                   dw2_asm_output_encoded_addr_rtx (
3070                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3071                         false, "Language Specific Data Area");
3072                 }
3073               else
3074                 {
3075                   if (lsda_encoding == DW_EH_PE_aligned)
3076                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3077                   dw2_asm_output_data
3078                     (size_of_encoded_value (lsda_encoding), 0,
3079                      "Language Specific Data Area (none)");
3080                 }
3081             }
3082           else
3083             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3084         }
3085
3086       /* Loop through the Call Frame Instructions associated with
3087          this FDE.  */
3088       fde->dw_fde_current_label = fde->dw_fde_begin;
3089       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3090         output_cfi (cfi, fde, for_eh);
3091
3092       /* Pad the FDE out to an address sized boundary.  */
3093       ASM_OUTPUT_ALIGN (asm_out_file,
3094                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3095       ASM_OUTPUT_LABEL (asm_out_file, l2);
3096     }
3097
3098   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3099     dw2_asm_output_data (4, 0, "End of Table");
3100 #ifdef MIPS_DEBUGGING_INFO
3101   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3102      get a value of 0.  Putting .align 0 after the label fixes it.  */
3103   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3104 #endif
3105
3106   /* Turn off app to make assembly quicker.  */
3107   if (flag_debug_asm)
3108     app_disable ();
3109 }
3110
3111 /* Output a marker (i.e. a label) for the beginning of a function, before
3112    the prologue.  */
3113
3114 void
3115 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3116                           const char *file ATTRIBUTE_UNUSED)
3117 {
3118   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3119   char * dup_label;
3120   dw_fde_ref fde;
3121
3122   current_function_func_begin_label = NULL;
3123
3124 #ifdef TARGET_UNWIND_INFO
3125   /* ??? current_function_func_begin_label is also used by except.c
3126      for call-site information.  We must emit this label if it might
3127      be used.  */
3128   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3129       && ! dwarf2out_do_frame ())
3130     return;
3131 #else
3132   if (! dwarf2out_do_frame ())
3133     return;
3134 #endif
3135
3136   switch_to_section (function_section (current_function_decl));
3137   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3138                                current_function_funcdef_no);
3139   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3140                           current_function_funcdef_no);
3141   dup_label = xstrdup (label);
3142   current_function_func_begin_label = dup_label;
3143
3144 #ifdef TARGET_UNWIND_INFO
3145   /* We can elide the fde allocation if we're not emitting debug info.  */
3146   if (! dwarf2out_do_frame ())
3147     return;
3148 #endif
3149
3150   /* Expand the fde table if necessary.  */
3151   if (fde_table_in_use == fde_table_allocated)
3152     {
3153       fde_table_allocated += FDE_TABLE_INCREMENT;
3154       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3155       memset (fde_table + fde_table_in_use, 0,
3156               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3157     }
3158
3159   /* Record the FDE associated with this function.  */
3160   current_funcdef_fde = fde_table_in_use;
3161
3162   /* Add the new FDE at the end of the fde_table.  */
3163   fde = &fde_table[fde_table_in_use++];
3164   fde->decl = current_function_decl;
3165   fde->dw_fde_begin = dup_label;
3166   fde->dw_fde_current_label = dup_label;
3167   fde->dw_fde_hot_section_label = NULL;
3168   fde->dw_fde_hot_section_end_label = NULL;
3169   fde->dw_fde_unlikely_section_label = NULL;
3170   fde->dw_fde_unlikely_section_end_label = NULL;
3171   fde->dw_fde_switched_sections = false;
3172   fde->dw_fde_end = NULL;
3173   fde->dw_fde_cfi = NULL;
3174   fde->funcdef_number = current_function_funcdef_no;
3175   fde->nothrow = TREE_NOTHROW (current_function_decl);
3176   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3177   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3178   fde->drap_reg = INVALID_REGNUM;
3179   fde->vdrap_reg = INVALID_REGNUM;
3180
3181   args_size = old_args_size = 0;
3182
3183   /* We only want to output line number information for the genuine dwarf2
3184      prologue case, not the eh frame case.  */
3185 #ifdef DWARF2_DEBUGGING_INFO
3186   if (file)
3187     dwarf2out_source_line (line, file);
3188 #endif
3189
3190   if (flag_dwarf2_cfi_asm)
3191     {
3192       int enc;
3193       rtx ref;
3194
3195       fprintf (asm_out_file, "\t.cfi_startproc\n");
3196
3197       if (eh_personality_libfunc)
3198         {
3199           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3200           ref = eh_personality_libfunc;
3201
3202           /* ??? The GAS support isn't entirely consistent.  We have to
3203              handle indirect support ourselves, but PC-relative is done
3204              in the assembler.  Further, the assembler can't handle any
3205              of the weirder relocation types.  */
3206           if (enc & DW_EH_PE_indirect)
3207             ref = dw2_force_const_mem (ref, true);
3208
3209           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3210           output_addr_const (asm_out_file, ref);
3211           fputc ('\n', asm_out_file);
3212         }
3213
3214       if (crtl->uses_eh_lsda)
3215         {
3216           char lab[20];
3217
3218           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3219           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3220                                        current_function_funcdef_no);
3221           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3222           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3223
3224           if (enc & DW_EH_PE_indirect)
3225             ref = dw2_force_const_mem (ref, true);
3226
3227           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3228           output_addr_const (asm_out_file, ref);
3229           fputc ('\n', asm_out_file);
3230         }
3231     }
3232 }
3233
3234 /* Output a marker (i.e. a label) for the absolute end of the generated code
3235    for a function definition.  This gets called *after* the epilogue code has
3236    been generated.  */
3237
3238 void
3239 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3240                         const char *file ATTRIBUTE_UNUSED)
3241 {
3242   dw_fde_ref fde;
3243   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3244
3245   if (flag_dwarf2_cfi_asm)
3246     fprintf (asm_out_file, "\t.cfi_endproc\n");
3247
3248   /* Output a label to mark the endpoint of the code generated for this
3249      function.  */
3250   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3251                                current_function_funcdef_no);
3252   ASM_OUTPUT_LABEL (asm_out_file, label);
3253   fde = current_fde ();
3254   gcc_assert (fde != NULL);
3255   fde->dw_fde_end = xstrdup (label);
3256 }
3257
3258 void
3259 dwarf2out_frame_init (void)
3260 {
3261   /* Allocate the initial hunk of the fde_table.  */
3262   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3263   fde_table_allocated = FDE_TABLE_INCREMENT;
3264   fde_table_in_use = 0;
3265
3266   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3267      sake of lookup_cfa.  */
3268
3269   /* On entry, the Canonical Frame Address is at SP.  */
3270   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3271
3272 #ifdef DWARF2_UNWIND_INFO
3273   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3274     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3275 #endif
3276 }
3277
3278 void
3279 dwarf2out_frame_finish (void)
3280 {
3281   /* Output call frame information.  */
3282   if (DWARF2_FRAME_INFO)
3283     output_call_frame_info (0);
3284
3285 #ifndef TARGET_UNWIND_INFO
3286   /* Output another copy for the unwinder.  */
3287   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3288     output_call_frame_info (1);
3289 #endif
3290 }
3291
3292 /* Note that the current function section is being used for code.  */
3293
3294 static void
3295 dwarf2out_note_section_used (void)
3296 {
3297   section *sec = current_function_section ();
3298   if (sec == text_section)
3299     text_section_used = true;
3300   else if (sec == cold_text_section)
3301     cold_text_section_used = true;
3302 }
3303
3304 void
3305 dwarf2out_switch_text_section (void)
3306 {
3307   dw_fde_ref fde = current_fde ();
3308
3309   gcc_assert (cfun && fde);
3310
3311   fde->dw_fde_switched_sections = true;
3312   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3313   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3314   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3315   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3316   have_multiple_function_sections = true;
3317
3318   /* Reset the current label on switching text sections, so that we
3319      don't attempt to advance_loc4 between labels in different sections.  */
3320   fde->dw_fde_current_label = NULL;
3321
3322   /* There is no need to mark used sections when not debugging.  */
3323   if (cold_text_section != NULL)
3324     dwarf2out_note_section_used ();
3325 }
3326 #endif
3327 \f
3328 /* And now, the subset of the debugging information support code necessary
3329    for emitting location expressions.  */
3330
3331 /* Data about a single source file.  */
3332 struct dwarf_file_data GTY(())
3333 {
3334   const char * filename;
3335   int emitted_number;
3336 };
3337
3338 /* We need some way to distinguish DW_OP_addr with a direct symbol
3339    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3340 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3341
3342
3343 typedef struct dw_val_struct *dw_val_ref;
3344 typedef struct die_struct *dw_die_ref;
3345 typedef const struct die_struct *const_dw_die_ref;
3346 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3347 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3348
3349 /* Each DIE may have a series of attribute/value pairs.  Values
3350    can take on several forms.  The forms that are used in this
3351    implementation are listed below.  */
3352
3353 enum dw_val_class
3354 {
3355   dw_val_class_addr,
3356   dw_val_class_offset,
3357   dw_val_class_loc,
3358   dw_val_class_loc_list,
3359   dw_val_class_range_list,
3360   dw_val_class_const,
3361   dw_val_class_unsigned_const,
3362   dw_val_class_long_long,
3363   dw_val_class_vec,
3364   dw_val_class_flag,
3365   dw_val_class_die_ref,
3366   dw_val_class_fde_ref,
3367   dw_val_class_lbl_id,
3368   dw_val_class_lineptr,
3369   dw_val_class_str,
3370   dw_val_class_macptr,
3371   dw_val_class_file
3372 };
3373
3374 /* Describe a double word constant value.  */
3375 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3376
3377 typedef struct dw_long_long_struct GTY(())
3378 {
3379   unsigned long hi;
3380   unsigned long low;
3381 }
3382 dw_long_long_const;
3383
3384 /* Describe a floating point constant value, or a vector constant value.  */
3385
3386 typedef struct dw_vec_struct GTY(())
3387 {
3388   unsigned char * GTY((length ("%h.length"))) array;
3389   unsigned length;
3390   unsigned elt_size;
3391 }
3392 dw_vec_const;
3393
3394 /* The dw_val_node describes an attribute's value, as it is
3395    represented internally.  */
3396
3397 typedef struct dw_val_struct GTY(())
3398 {
3399   enum dw_val_class val_class;
3400   union dw_val_struct_union
3401     {
3402       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3403       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3404       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3405       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3406       HOST_WIDE_INT GTY ((default)) val_int;
3407       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3408       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3409       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3410       struct dw_val_die_union
3411         {
3412           dw_die_ref die;
3413           int external;
3414         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3415       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3416       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3417       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3418       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3419       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3420     }
3421   GTY ((desc ("%1.val_class"))) v;
3422 }
3423 dw_val_node;
3424
3425 /* Locations in memory are described using a sequence of stack machine
3426    operations.  */
3427
3428 typedef struct dw_loc_descr_struct GTY(())
3429 {
3430   dw_loc_descr_ref dw_loc_next;
3431   enum dwarf_location_atom dw_loc_opc;
3432   dw_val_node dw_loc_oprnd1;
3433   dw_val_node dw_loc_oprnd2;
3434   int dw_loc_addr;
3435 }
3436 dw_loc_descr_node;
3437
3438 /* Location lists are ranges + location descriptions for that range,
3439    so you can track variables that are in different places over
3440    their entire life.  */
3441 typedef struct dw_loc_list_struct GTY(())
3442 {
3443   dw_loc_list_ref dw_loc_next;
3444   const char *begin; /* Label for begin address of range */
3445   const char *end;  /* Label for end address of range */
3446   char *ll_symbol; /* Label for beginning of location list.
3447                       Only on head of list */
3448   const char *section; /* Section this loclist is relative to */
3449   dw_loc_descr_ref expr;
3450 } dw_loc_list_node;
3451
3452 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3453
3454 static const char *dwarf_stack_op_name (unsigned);
3455 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
3456                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3457 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3458 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
3459 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
3460 static unsigned long size_of_locs (dw_loc_descr_ref);
3461 static void output_loc_operands (dw_loc_descr_ref);
3462 static void output_loc_sequence (dw_loc_descr_ref);
3463
3464 /* Convert a DWARF stack opcode into its string name.  */
3465
3466 static const char *
3467 dwarf_stack_op_name (unsigned int op)
3468 {
3469   switch (op)
3470     {
3471     case DW_OP_addr:
3472     case INTERNAL_DW_OP_tls_addr:
3473       return "DW_OP_addr";
3474     case DW_OP_deref:
3475       return "DW_OP_deref";
3476     case DW_OP_const1u:
3477       return "DW_OP_const1u";
3478     case DW_OP_const1s:
3479       return "DW_OP_const1s";
3480     case DW_OP_const2u:
3481       return "DW_OP_const2u";
3482     case DW_OP_const2s:
3483       return "DW_OP_const2s";
3484     case DW_OP_const4u:
3485       return "DW_OP_const4u";
3486     case DW_OP_const4s:
3487       return "DW_OP_const4s";
3488     case DW_OP_const8u:
3489       return "DW_OP_const8u";
3490     case DW_OP_const8s:
3491       return "DW_OP_const8s";
3492     case DW_OP_constu:
3493       return "DW_OP_constu";
3494     case DW_OP_consts:
3495       return "DW_OP_consts";
3496     case DW_OP_dup:
3497       return "DW_OP_dup";
3498     case DW_OP_drop:
3499       return "DW_OP_drop";
3500     case DW_OP_over:
3501       return "DW_OP_over";
3502     case DW_OP_pick:
3503       return "DW_OP_pick";
3504     case DW_OP_swap:
3505       return "DW_OP_swap";
3506     case DW_OP_rot:
3507       return "DW_OP_rot";
3508     case DW_OP_xderef:
3509       return "DW_OP_xderef";
3510     case DW_OP_abs:
3511       return "DW_OP_abs";
3512     case DW_OP_and:
3513       return "DW_OP_and";
3514     case DW_OP_div:
3515       return "DW_OP_div";
3516     case DW_OP_minus:
3517       return "DW_OP_minus";
3518     case DW_OP_mod:
3519       return "DW_OP_mod";
3520     case DW_OP_mul:
3521       return "DW_OP_mul";
3522     case DW_OP_neg:
3523       return "DW_OP_neg";
3524     case DW_OP_not:
3525       return "DW_OP_not";
3526     case DW_OP_or:
3527       return "DW_OP_or";
3528     case DW_OP_plus:
3529       return "DW_OP_plus";
3530     case DW_OP_plus_uconst:
3531       return "DW_OP_plus_uconst";
3532     case DW_OP_shl:
3533       return "DW_OP_shl";
3534     case DW_OP_shr:
3535       return "DW_OP_shr";
3536     case DW_OP_shra:
3537       return "DW_OP_shra";
3538     case DW_OP_xor:
3539       return "DW_OP_xor";
3540     case DW_OP_bra:
3541       return "DW_OP_bra";
3542     case DW_OP_eq:
3543       return "DW_OP_eq";
3544     case DW_OP_ge:
3545       return "DW_OP_ge";
3546     case DW_OP_gt:
3547       return "DW_OP_gt";
3548     case DW_OP_le:
3549       return "DW_OP_le";
3550     case DW_OP_lt:
3551       return "DW_OP_lt";
3552     case DW_OP_ne:
3553       return "DW_OP_ne";
3554     case DW_OP_skip:
3555       return "DW_OP_skip";
3556     case DW_OP_lit0:
3557       return "DW_OP_lit0";
3558     case DW_OP_lit1:
3559       return "DW_OP_lit1";
3560     case DW_OP_lit2:
3561       return "DW_OP_lit2";
3562     case DW_OP_lit3:
3563       return "DW_OP_lit3";
3564     case DW_OP_lit4:
3565       return "DW_OP_lit4";
3566     case DW_OP_lit5:
3567       return "DW_OP_lit5";
3568     case DW_OP_lit6:
3569       return "DW_OP_lit6";
3570     case DW_OP_lit7:
3571       return "DW_OP_lit7";
3572     case DW_OP_lit8:
3573       return "DW_OP_lit8";
3574     case DW_OP_lit9:
3575       return "DW_OP_lit9";
3576     case DW_OP_lit10:
3577       return "DW_OP_lit10";
3578     case DW_OP_lit11:
3579       return "DW_OP_lit11";
3580     case DW_OP_lit12:
3581       return "DW_OP_lit12";
3582     case DW_OP_lit13:
3583       return "DW_OP_lit13";
3584     case DW_OP_lit14:
3585       return "DW_OP_lit14";
3586     case DW_OP_lit15:
3587       return "DW_OP_lit15";
3588     case DW_OP_lit16:
3589       return "DW_OP_lit16";
3590     case DW_OP_lit17:
3591       return "DW_OP_lit17";
3592     case DW_OP_lit18:
3593       return "DW_OP_lit18";
3594     case DW_OP_lit19:
3595       return "DW_OP_lit19";
3596     case DW_OP_lit20:
3597       return "DW_OP_lit20";
3598     case DW_OP_lit21:
3599       return "DW_OP_lit21";
3600     case DW_OP_lit22:
3601       return "DW_OP_lit22";
3602     case DW_OP_lit23:
3603       return "DW_OP_lit23";
3604     case DW_OP_lit24:
3605       return "DW_OP_lit24";
3606     case DW_OP_lit25:
3607       return "DW_OP_lit25";
3608     case DW_OP_lit26:
3609       return "DW_OP_lit26";
3610     case DW_OP_lit27:
3611       return "DW_OP_lit27";
3612     case DW_OP_lit28:
3613       return "DW_OP_lit28";
3614     case DW_OP_lit29:
3615       return "DW_OP_lit29";
3616     case DW_OP_lit30:
3617       return "DW_OP_lit30";
3618     case DW_OP_lit31:
3619       return "DW_OP_lit31";
3620     case DW_OP_reg0:
3621       return "DW_OP_reg0";
3622     case DW_OP_reg1:
3623       return "DW_OP_reg1";
3624     case DW_OP_reg2:
3625       return "DW_OP_reg2";
3626     case DW_OP_reg3:
3627       return "DW_OP_reg3";
3628     case DW_OP_reg4:
3629       return "DW_OP_reg4";
3630     case DW_OP_reg5:
3631       return "DW_OP_reg5";
3632     case DW_OP_reg6:
3633       return "DW_OP_reg6";
3634     case DW_OP_reg7:
3635       return "DW_OP_reg7";
3636     case DW_OP_reg8:
3637       return "DW_OP_reg8";
3638     case DW_OP_reg9:
3639       return "DW_OP_reg9";
3640     case DW_OP_reg10:
3641       return "DW_OP_reg10";
3642     case DW_OP_reg11:
3643       return "DW_OP_reg11";
3644     case DW_OP_reg12:
3645       return "DW_OP_reg12";
3646     case DW_OP_reg13:
3647       return "DW_OP_reg13";
3648     case DW_OP_reg14:
3649       return "DW_OP_reg14";
3650     case DW_OP_reg15:
3651       return "DW_OP_reg15";
3652     case DW_OP_reg16:
3653       return "DW_OP_reg16";
3654     case DW_OP_reg17:
3655       return "DW_OP_reg17";
3656     case DW_OP_reg18:
3657       return "DW_OP_reg18";
3658     case DW_OP_reg19:
3659       return "DW_OP_reg19";
3660     case DW_OP_reg20:
3661       return "DW_OP_reg20";
3662     case DW_OP_reg21:
3663       return "DW_OP_reg21";
3664     case DW_OP_reg22:
3665       return "DW_OP_reg22";
3666     case DW_OP_reg23:
3667       return "DW_OP_reg23";
3668     case DW_OP_reg24:
3669       return "DW_OP_reg24";
3670     case DW_OP_reg25:
3671       return "DW_OP_reg25";
3672     case DW_OP_reg26:
3673       return "DW_OP_reg26";
3674     case DW_OP_reg27:
3675       return "DW_OP_reg27";
3676     case DW_OP_reg28:
3677       return "DW_OP_reg28";
3678     case DW_OP_reg29:
3679       return "DW_OP_reg29";
3680     case DW_OP_reg30:
3681       return "DW_OP_reg30";
3682     case DW_OP_reg31:
3683       return "DW_OP_reg31";
3684     case DW_OP_breg0:
3685       return "DW_OP_breg0";
3686     case DW_OP_breg1:
3687       return "DW_OP_breg1";
3688     case DW_OP_breg2:
3689       return "DW_OP_breg2";
3690     case DW_OP_breg3:
3691       return "DW_OP_breg3";
3692     case DW_OP_breg4:
3693       return "DW_OP_breg4";
3694     case DW_OP_breg5:
3695       return "DW_OP_breg5";
3696     case DW_OP_breg6:
3697       return "DW_OP_breg6";
3698     case DW_OP_breg7:
3699       return "DW_OP_breg7";
3700     case DW_OP_breg8:
3701       return "DW_OP_breg8";
3702     case DW_OP_breg9:
3703       return "DW_OP_breg9";
3704     case DW_OP_breg10:
3705       return "DW_OP_breg10";
3706     case DW_OP_breg11:
3707       return "DW_OP_breg11";
3708     case DW_OP_breg12:
3709       return "DW_OP_breg12";
3710     case DW_OP_breg13:
3711       return "DW_OP_breg13";
3712     case DW_OP_breg14:
3713       return "DW_OP_breg14";
3714     case DW_OP_breg15:
3715       return "DW_OP_breg15";
3716     case DW_OP_breg16:
3717       return "DW_OP_breg16";
3718     case DW_OP_breg17:
3719       return "DW_OP_breg17";
3720     case DW_OP_breg18:
3721       return "DW_OP_breg18";
3722     case DW_OP_breg19:
3723       return "DW_OP_breg19";
3724     case DW_OP_breg20:
3725       return "DW_OP_breg20";
3726     case DW_OP_breg21:
3727       return "DW_OP_breg21";
3728     case DW_OP_breg22:
3729       return "DW_OP_breg22";
3730     case DW_OP_breg23:
3731       return "DW_OP_breg23";
3732     case DW_OP_breg24:
3733       return "DW_OP_breg24";
3734     case DW_OP_breg25:
3735       return "DW_OP_breg25";
3736     case DW_OP_breg26:
3737       return "DW_OP_breg26";
3738     case DW_OP_breg27:
3739       return "DW_OP_breg27";
3740     case DW_OP_breg28:
3741       return "DW_OP_breg28";
3742     case DW_OP_breg29:
3743       return "DW_OP_breg29";
3744     case DW_OP_breg30:
3745       return "DW_OP_breg30";
3746     case DW_OP_breg31:
3747       return "DW_OP_breg31";
3748     case DW_OP_regx:
3749       return "DW_OP_regx";
3750     case DW_OP_fbreg:
3751       return "DW_OP_fbreg";
3752     case DW_OP_bregx:
3753       return "DW_OP_bregx";
3754     case DW_OP_piece:
3755       return "DW_OP_piece";
3756     case DW_OP_deref_size:
3757       return "DW_OP_deref_size";
3758     case DW_OP_xderef_size:
3759       return "DW_OP_xderef_size";
3760     case DW_OP_nop:
3761       return "DW_OP_nop";
3762     case DW_OP_push_object_address:
3763       return "DW_OP_push_object_address";
3764     case DW_OP_call2:
3765       return "DW_OP_call2";
3766     case DW_OP_call4:
3767       return "DW_OP_call4";
3768     case DW_OP_call_ref:
3769       return "DW_OP_call_ref";
3770     case DW_OP_GNU_push_tls_address:
3771       return "DW_OP_GNU_push_tls_address";
3772     case DW_OP_GNU_uninit:
3773       return "DW_OP_GNU_uninit";
3774     default:
3775       return "OP_<unknown>";
3776     }
3777 }
3778
3779 /* Return a pointer to a newly allocated location description.  Location
3780    descriptions are simple expression terms that can be strung
3781    together to form more complicated location (address) descriptions.  */
3782
3783 static inline dw_loc_descr_ref
3784 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3785                unsigned HOST_WIDE_INT oprnd2)
3786 {
3787   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3788
3789   descr->dw_loc_opc = op;
3790   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3791   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3792   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3793   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3794
3795   return descr;
3796 }
3797
3798 /* Add a location description term to a location description expression.  */
3799
3800 static inline void
3801 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3802 {
3803   dw_loc_descr_ref *d;
3804
3805   /* Find the end of the chain.  */
3806   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3807     ;
3808
3809   *d = descr;
3810 }
3811
3812 /* Return the size of a location descriptor.  */
3813
3814 static unsigned long
3815 size_of_loc_descr (dw_loc_descr_ref loc)
3816 {
3817   unsigned long size = 1;
3818
3819   switch (loc->dw_loc_opc)
3820     {
3821     case DW_OP_addr:
3822     case INTERNAL_DW_OP_tls_addr:
3823       size += DWARF2_ADDR_SIZE;
3824       break;
3825     case DW_OP_const1u:
3826     case DW_OP_const1s:
3827       size += 1;
3828       break;
3829     case DW_OP_const2u:
3830     case DW_OP_const2s:
3831       size += 2;
3832       break;
3833     case DW_OP_const4u:
3834     case DW_OP_const4s:
3835       size += 4;
3836       break;
3837     case DW_OP_const8u:
3838     case DW_OP_const8s:
3839       size += 8;
3840       break;
3841     case DW_OP_constu:
3842       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3843       break;
3844     case DW_OP_consts:
3845       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3846       break;
3847     case DW_OP_pick:
3848       size += 1;
3849       break;
3850     case DW_OP_plus_uconst:
3851       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3852       break;
3853     case DW_OP_skip:
3854     case DW_OP_bra:
3855       size += 2;
3856       break;
3857     case DW_OP_breg0:
3858     case DW_OP_breg1:
3859     case DW_OP_breg2:
3860     case DW_OP_breg3:
3861     case DW_OP_breg4:
3862     case DW_OP_breg5:
3863     case DW_OP_breg6:
3864     case DW_OP_breg7:
3865     case DW_OP_breg8:
3866     case DW_OP_breg9:
3867     case DW_OP_breg10:
3868     case DW_OP_breg11:
3869     case DW_OP_breg12:
3870     case DW_OP_breg13:
3871     case DW_OP_breg14:
3872     case DW_OP_breg15:
3873     case DW_OP_breg16:
3874     case DW_OP_breg17:
3875     case DW_OP_breg18:
3876     case DW_OP_breg19:
3877     case DW_OP_breg20:
3878     case DW_OP_breg21:
3879     case DW_OP_breg22:
3880     case DW_OP_breg23:
3881     case DW_OP_breg24:
3882     case DW_OP_breg25:
3883     case DW_OP_breg26:
3884     case DW_OP_breg27:
3885     case DW_OP_breg28:
3886     case DW_OP_breg29:
3887     case DW_OP_breg30:
3888     case DW_OP_breg31:
3889       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3890       break;
3891     case DW_OP_regx:
3892       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3893       break;
3894     case DW_OP_fbreg:
3895       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3896       break;
3897     case DW_OP_bregx:
3898       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3899       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3900       break;
3901     case DW_OP_piece:
3902       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3903       break;
3904     case DW_OP_deref_size:
3905     case DW_OP_xderef_size:
3906       size += 1;
3907       break;
3908     case DW_OP_call2:
3909       size += 2;
3910       break;
3911     case DW_OP_call4:
3912       size += 4;
3913       break;
3914     case DW_OP_call_ref:
3915       size += DWARF2_ADDR_SIZE;
3916       break;
3917     default:
3918       break;
3919     }
3920
3921   return size;
3922 }
3923
3924 /* Return the size of a series of location descriptors.  */
3925
3926 static unsigned long
3927 size_of_locs (dw_loc_descr_ref loc)
3928 {
3929   dw_loc_descr_ref l;
3930   unsigned long size;
3931
3932   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3933      field, to avoid writing to a PCH file.  */
3934   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3935     {
3936       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3937         break;
3938       size += size_of_loc_descr (l);
3939     }
3940   if (! l)
3941     return size;
3942
3943   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3944     {
3945       l->dw_loc_addr = size;
3946       size += size_of_loc_descr (l);
3947     }
3948
3949   return size;
3950 }
3951
3952 /* Output location description stack opcode's operands (if any).  */
3953
3954 static void
3955 output_loc_operands (dw_loc_descr_ref loc)
3956 {
3957   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3958   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3959
3960   switch (loc->dw_loc_opc)
3961     {
3962 #ifdef DWARF2_DEBUGGING_INFO
3963     case DW_OP_addr:
3964       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3965       break;
3966     case DW_OP_const2u:
3967     case DW_OP_const2s:
3968       dw2_asm_output_data (2, val1->v.val_int, NULL);
3969       break;
3970     case DW_OP_const4u:
3971     case DW_OP_const4s:
3972       dw2_asm_output_data (4, val1->v.val_int, NULL);
3973       break;
3974     case DW_OP_const8u:
3975     case DW_OP_const8s:
3976       gcc_assert (HOST_BITS_PER_LONG >= 64);
3977       dw2_asm_output_data (8, val1->v.val_int, NULL);
3978       break;
3979     case DW_OP_skip:
3980     case DW_OP_bra:
3981       {
3982         int offset;
3983
3984         gcc_assert (val1->val_class == dw_val_class_loc);
3985         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3986
3987         dw2_asm_output_data (2, offset, NULL);
3988       }
3989       break;
3990 #else
3991     case DW_OP_addr:
3992     case DW_OP_const2u:
3993     case DW_OP_const2s:
3994     case DW_OP_const4u:
3995     case DW_OP_const4s:
3996     case DW_OP_const8u:
3997     case DW_OP_const8s:
3998     case DW_OP_skip:
3999     case DW_OP_bra:
4000       /* We currently don't make any attempt to make sure these are
4001          aligned properly like we do for the main unwind info, so
4002          don't support emitting things larger than a byte if we're
4003          only doing unwinding.  */
4004       gcc_unreachable ();
4005 #endif
4006     case DW_OP_const1u:
4007     case DW_OP_const1s:
4008       dw2_asm_output_data (1, val1->v.val_int, NULL);
4009       break;
4010     case DW_OP_constu:
4011       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4012       break;
4013     case DW_OP_consts:
4014       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4015       break;
4016     case DW_OP_pick:
4017       dw2_asm_output_data (1, val1->v.val_int, NULL);
4018       break;
4019     case DW_OP_plus_uconst:
4020       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4021       break;
4022     case DW_OP_breg0:
4023     case DW_OP_breg1:
4024     case DW_OP_breg2:
4025     case DW_OP_breg3:
4026     case DW_OP_breg4:
4027     case DW_OP_breg5:
4028     case DW_OP_breg6:
4029     case DW_OP_breg7:
4030     case DW_OP_breg8:
4031     case DW_OP_breg9:
4032     case DW_OP_breg10:
4033     case DW_OP_breg11:
4034     case DW_OP_breg12:
4035     case DW_OP_breg13:
4036     case DW_OP_breg14:
4037     case DW_OP_breg15:
4038     case DW_OP_breg16:
4039     case DW_OP_breg17:
4040     case DW_OP_breg18:
4041     case DW_OP_breg19:
4042     case DW_OP_breg20:
4043     case DW_OP_breg21:
4044     case DW_OP_breg22:
4045     case DW_OP_breg23:
4046     case DW_OP_breg24:
4047     case DW_OP_breg25:
4048     case DW_OP_breg26:
4049     case DW_OP_breg27:
4050     case DW_OP_breg28:
4051     case DW_OP_breg29:
4052     case DW_OP_breg30:
4053     case DW_OP_breg31:
4054       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4055       break;
4056     case DW_OP_regx:
4057       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4058       break;
4059     case DW_OP_fbreg:
4060       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4061       break;
4062     case DW_OP_bregx:
4063       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4064       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4065       break;
4066     case DW_OP_piece:
4067       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4068       break;
4069     case DW_OP_deref_size:
4070     case DW_OP_xderef_size:
4071       dw2_asm_output_data (1, val1->v.val_int, NULL);
4072       break;
4073
4074     case INTERNAL_DW_OP_tls_addr:
4075       if (targetm.asm_out.output_dwarf_dtprel)
4076         {
4077           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4078                                                DWARF2_ADDR_SIZE,
4079                                                val1->v.val_addr);
4080           fputc ('\n', asm_out_file);
4081         }
4082       else
4083         gcc_unreachable ();
4084       break;
4085
4086     default:
4087       /* Other codes have no operands.  */
4088       break;
4089     }
4090 }
4091
4092 /* Output a sequence of location operations.  */
4093
4094 static void
4095 output_loc_sequence (dw_loc_descr_ref loc)
4096 {
4097   for (; loc != NULL; loc = loc->dw_loc_next)
4098     {
4099       /* Output the opcode.  */
4100       dw2_asm_output_data (1, loc->dw_loc_opc,
4101                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4102
4103       /* Output the operand(s) (if any).  */
4104       output_loc_operands (loc);
4105     }
4106 }
4107
4108 /* Output location description stack opcode's operands (if any).
4109    The output is single bytes on a line, suitable for .cfi_escape.  */
4110
4111 static void
4112 output_loc_operands_raw (dw_loc_descr_ref loc)
4113 {
4114   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4115   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4116
4117   switch (loc->dw_loc_opc)
4118     {
4119     case DW_OP_addr:
4120       /* We cannot output addresses in .cfi_escape, only bytes.  */
4121       gcc_unreachable ();
4122
4123     case DW_OP_const1u:
4124     case DW_OP_const1s:
4125     case DW_OP_pick:
4126     case DW_OP_deref_size:
4127     case DW_OP_xderef_size:
4128       fputc (',', asm_out_file);
4129       dw2_asm_output_data_raw (1, val1->v.val_int);
4130       break;
4131
4132     case DW_OP_const2u:
4133     case DW_OP_const2s:
4134       fputc (',', asm_out_file);
4135       dw2_asm_output_data_raw (2, val1->v.val_int);
4136       break;
4137
4138     case DW_OP_const4u:
4139     case DW_OP_const4s:
4140       fputc (',', asm_out_file);
4141       dw2_asm_output_data_raw (4, val1->v.val_int);
4142       break;
4143
4144     case DW_OP_const8u:
4145     case DW_OP_const8s:
4146       gcc_assert (HOST_BITS_PER_LONG >= 64);
4147       fputc (',', asm_out_file);
4148       dw2_asm_output_data_raw (8, val1->v.val_int);
4149       break;
4150
4151     case DW_OP_skip:
4152     case DW_OP_bra:
4153       {
4154         int offset;
4155
4156         gcc_assert (val1->val_class == dw_val_class_loc);
4157         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4158
4159         fputc (',', asm_out_file);
4160         dw2_asm_output_data_raw (2, offset);
4161       }
4162       break;
4163
4164     case DW_OP_constu:
4165     case DW_OP_plus_uconst:
4166     case DW_OP_regx:
4167     case DW_OP_piece:
4168       fputc (',', asm_out_file);
4169       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4170       break;
4171
4172     case DW_OP_consts:
4173     case DW_OP_breg0:
4174     case DW_OP_breg1:
4175     case DW_OP_breg2:
4176     case DW_OP_breg3:
4177     case DW_OP_breg4:
4178     case DW_OP_breg5:
4179     case DW_OP_breg6:
4180     case DW_OP_breg7:
4181     case DW_OP_breg8:
4182     case DW_OP_breg9:
4183     case DW_OP_breg10:
4184     case DW_OP_breg11:
4185     case DW_OP_breg12:
4186     case DW_OP_breg13:
4187     case DW_OP_breg14:
4188     case DW_OP_breg15:
4189     case DW_OP_breg16:
4190     case DW_OP_breg17:
4191     case DW_OP_breg18:
4192     case DW_OP_breg19:
4193     case DW_OP_breg20:
4194     case DW_OP_breg21:
4195     case DW_OP_breg22:
4196     case DW_OP_breg23:
4197     case DW_OP_breg24:
4198     case DW_OP_breg25:
4199     case DW_OP_breg26:
4200     case DW_OP_breg27:
4201     case DW_OP_breg28:
4202     case DW_OP_breg29:
4203     case DW_OP_breg30:
4204     case DW_OP_breg31:
4205     case DW_OP_fbreg:
4206       fputc (',', asm_out_file);
4207       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4208       break;
4209
4210     case DW_OP_bregx:
4211       fputc (',', asm_out_file);
4212       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4213       fputc (',', asm_out_file);
4214       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4215       break;
4216
4217     case INTERNAL_DW_OP_tls_addr:
4218       gcc_unreachable ();
4219
4220     default:
4221       /* Other codes have no operands.  */
4222       break;
4223     }
4224 }
4225
4226 static void
4227 output_loc_sequence_raw (dw_loc_descr_ref loc)
4228 {
4229   while (1)
4230     {
4231       /* Output the opcode.  */
4232       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4233       output_loc_operands_raw (loc);
4234
4235       if (!loc->dw_loc_next)
4236         break;
4237       loc = loc->dw_loc_next;
4238
4239       fputc (',', asm_out_file);
4240     }
4241 }
4242
4243 /* This routine will generate the correct assembly data for a location
4244    description based on a cfi entry with a complex address.  */
4245
4246 static void
4247 output_cfa_loc (dw_cfi_ref cfi)
4248 {
4249   dw_loc_descr_ref loc;
4250   unsigned long size;
4251
4252   if (cfi->dw_cfi_opc == DW_CFA_expression)
4253     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4254
4255   /* Output the size of the block.  */
4256   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4257   size = size_of_locs (loc);
4258   dw2_asm_output_data_uleb128 (size, NULL);
4259
4260   /* Now output the operations themselves.  */
4261   output_loc_sequence (loc);
4262 }
4263
4264 /* Similar, but used for .cfi_escape.  */
4265
4266 static void
4267 output_cfa_loc_raw (dw_cfi_ref cfi)
4268 {
4269   dw_loc_descr_ref loc;
4270   unsigned long size;
4271
4272   if (cfi->dw_cfi_opc == DW_CFA_expression)
4273     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4274
4275   /* Output the size of the block.  */
4276   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4277   size = size_of_locs (loc);
4278   dw2_asm_output_data_uleb128_raw (size);
4279   fputc (',', asm_out_file);
4280
4281   /* Now output the operations themselves.  */
4282   output_loc_sequence_raw (loc);
4283 }
4284
4285 /* This function builds a dwarf location descriptor sequence from a
4286    dw_cfa_location, adding the given OFFSET to the result of the
4287    expression.  */
4288
4289 static struct dw_loc_descr_struct *
4290 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4291 {
4292   struct dw_loc_descr_struct *head, *tmp;
4293
4294   offset += cfa->offset;
4295
4296   if (cfa->indirect)
4297     {
4298       if (cfa->base_offset)
4299         {
4300           if (cfa->reg <= 31)
4301             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
4302           else
4303             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
4304         }
4305       else if (cfa->reg <= 31)
4306         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
4307       else
4308         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
4309
4310       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4311       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4312       add_loc_descr (&head, tmp);
4313       if (offset != 0)
4314         {
4315           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4316           add_loc_descr (&head, tmp);
4317         }
4318     }
4319   else
4320     {
4321       if (offset == 0)
4322         if (cfa->reg <= 31)
4323           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
4324         else
4325           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
4326       else if (cfa->reg <= 31)
4327         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
4328       else
4329         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
4330     }
4331
4332   return head;
4333 }
4334
4335 /* This function builds a dwarf location descriptor sequence for
4336    the address at OFFSET from the CFA when stack is aligned to
4337    ALIGNMENT byte.  */
4338
4339 static struct dw_loc_descr_struct *
4340 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4341 {
4342   struct dw_loc_descr_struct *head;
4343   unsigned int dwarf_fp
4344     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4345
4346  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4347   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4348     {
4349       if (dwarf_fp <= 31)
4350         head = new_loc_descr (DW_OP_breg0 + dwarf_fp, 0, 0);
4351       else
4352         head = new_loc_descr (DW_OP_bregx, dwarf_fp, 0);
4353
4354       add_loc_descr (&head, int_loc_descriptor (alignment));
4355       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4356
4357       add_loc_descr (&head, int_loc_descriptor (offset));
4358       add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4359     }
4360   else if (dwarf_fp <= 31)
4361     head = new_loc_descr (DW_OP_breg0 + dwarf_fp, offset, 0);
4362   else
4363     head = new_loc_descr (DW_OP_bregx, dwarf_fp, offset);
4364   return head;
4365 }
4366
4367 /* This function fills in aa dw_cfa_location structure from a dwarf location
4368    descriptor sequence.  */
4369
4370 static void
4371 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4372 {
4373   struct dw_loc_descr_struct *ptr;
4374   cfa->offset = 0;
4375   cfa->base_offset = 0;
4376   cfa->indirect = 0;
4377   cfa->reg = -1;
4378
4379   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4380     {
4381       enum dwarf_location_atom op = ptr->dw_loc_opc;
4382
4383       switch (op)
4384         {
4385         case DW_OP_reg0:
4386         case DW_OP_reg1:
4387         case DW_OP_reg2:
4388         case DW_OP_reg3:
4389         case DW_OP_reg4:
4390         case DW_OP_reg5:
4391         case DW_OP_reg6:
4392         case DW_OP_reg7:
4393         case DW_OP_reg8:
4394         case DW_OP_reg9:
4395         case DW_OP_reg10:
4396         case DW_OP_reg11:
4397         case DW_OP_reg12:
4398         case DW_OP_reg13:
4399         case DW_OP_reg14:
4400         case DW_OP_reg15:
4401         case DW_OP_reg16:
4402         case DW_OP_reg17:
4403         case DW_OP_reg18:
4404         case DW_OP_reg19:
4405         case DW_OP_reg20:
4406         case DW_OP_reg21:
4407         case DW_OP_reg22:
4408         case DW_OP_reg23:
4409         case DW_OP_reg24:
4410         case DW_OP_reg25:
4411         case DW_OP_reg26:
4412         case DW_OP_reg27:
4413         case DW_OP_reg28:
4414         case DW_OP_reg29:
4415         case DW_OP_reg30:
4416         case DW_OP_reg31:
4417           cfa->reg = op - DW_OP_reg0;
4418           break;
4419         case DW_OP_regx:
4420           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4421           break;
4422         case DW_OP_breg0:
4423         case DW_OP_breg1:
4424         case DW_OP_breg2:
4425         case DW_OP_breg3:
4426         case DW_OP_breg4:
4427         case DW_OP_breg5:
4428         case DW_OP_breg6:
4429         case DW_OP_breg7:
4430         case DW_OP_breg8:
4431         case DW_OP_breg9:
4432         case DW_OP_breg10:
4433         case DW_OP_breg11:
4434         case DW_OP_breg12:
4435         case DW_OP_breg13:
4436         case DW_OP_breg14:
4437         case DW_OP_breg15:
4438         case DW_OP_breg16:
4439         case DW_OP_breg17:
4440         case DW_OP_breg18:
4441         case DW_OP_breg19:
4442         case DW_OP_breg20:
4443         case DW_OP_breg21:
4444         case DW_OP_breg22:
4445         case DW_OP_breg23:
4446         case DW_OP_breg24:
4447         case DW_OP_breg25:
4448         case DW_OP_breg26:
4449         case DW_OP_breg27:
4450         case DW_OP_breg28:
4451         case DW_OP_breg29:
4452         case DW_OP_breg30:
4453         case DW_OP_breg31:
4454           cfa->reg = op - DW_OP_breg0;
4455           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4456           break;
4457         case DW_OP_bregx:
4458           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4459           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4460           break;
4461         case DW_OP_deref:
4462           cfa->indirect = 1;
4463           break;
4464         case DW_OP_plus_uconst:
4465           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4466           break;
4467         default:
4468           internal_error ("DW_LOC_OP %s not implemented",
4469                           dwarf_stack_op_name (ptr->dw_loc_opc));
4470         }
4471     }
4472 }
4473 #endif /* .debug_frame support */
4474 \f
4475 /* And now, the support for symbolic debugging information.  */
4476 #ifdef DWARF2_DEBUGGING_INFO
4477
4478 /* .debug_str support.  */
4479 static int output_indirect_string (void **, void *);
4480
4481 static void dwarf2out_init (const char *);
4482 static void dwarf2out_finish (const char *);
4483 static void dwarf2out_define (unsigned int, const char *);
4484 static void dwarf2out_undef (unsigned int, const char *);
4485 static void dwarf2out_start_source_file (unsigned, const char *);
4486 static void dwarf2out_end_source_file (unsigned);
4487 static void dwarf2out_begin_block (unsigned, unsigned);
4488 static void dwarf2out_end_block (unsigned, unsigned);
4489 static bool dwarf2out_ignore_block (const_tree);
4490 static void dwarf2out_global_decl (tree);
4491 static void dwarf2out_type_decl (tree, int);
4492 static void dwarf2out_imported_module_or_decl (tree, tree);
4493 static void dwarf2out_abstract_function (tree);
4494 static void dwarf2out_var_location (rtx);
4495 static void dwarf2out_begin_function (tree);
4496
4497 /* The debug hooks structure.  */
4498
4499 const struct gcc_debug_hooks dwarf2_debug_hooks =
4500 {
4501   dwarf2out_init,
4502   dwarf2out_finish,
4503   dwarf2out_define,
4504   dwarf2out_undef,
4505   dwarf2out_start_source_file,
4506   dwarf2out_end_source_file,
4507   dwarf2out_begin_block,
4508   dwarf2out_end_block,
4509   dwarf2out_ignore_block,
4510   dwarf2out_source_line,
4511   dwarf2out_begin_prologue,
4512   debug_nothing_int_charstar,   /* end_prologue */
4513   dwarf2out_end_epilogue,
4514   dwarf2out_begin_function,
4515   debug_nothing_int,            /* end_function */
4516   dwarf2out_decl,               /* function_decl */
4517   dwarf2out_global_decl,
4518   dwarf2out_type_decl,          /* type_decl */
4519   dwarf2out_imported_module_or_decl,
4520   debug_nothing_tree,           /* deferred_inline_function */
4521   /* The DWARF 2 backend tries to reduce debugging bloat by not
4522      emitting the abstract description of inline functions until
4523      something tries to reference them.  */
4524   dwarf2out_abstract_function,  /* outlining_inline_function */
4525   debug_nothing_rtx,            /* label */
4526   debug_nothing_int,            /* handle_pch */
4527   dwarf2out_var_location,
4528   dwarf2out_switch_text_section,
4529   1                             /* start_end_main_source_file */
4530 };
4531 #endif
4532 \f
4533 /* NOTE: In the comments in this file, many references are made to
4534    "Debugging Information Entries".  This term is abbreviated as `DIE'
4535    throughout the remainder of this file.  */
4536
4537 /* An internal representation of the DWARF output is built, and then
4538    walked to generate the DWARF debugging info.  The walk of the internal
4539    representation is done after the entire program has been compiled.
4540    The types below are used to describe the internal representation.  */
4541
4542 /* Various DIE's use offsets relative to the beginning of the
4543    .debug_info section to refer to each other.  */
4544
4545 typedef long int dw_offset;
4546
4547 /* Define typedefs here to avoid circular dependencies.  */
4548
4549 typedef struct dw_attr_struct *dw_attr_ref;
4550 typedef struct dw_line_info_struct *dw_line_info_ref;
4551 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4552 typedef struct pubname_struct *pubname_ref;
4553 typedef struct dw_ranges_struct *dw_ranges_ref;
4554 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4555
4556 /* Each entry in the line_info_table maintains the file and
4557    line number associated with the label generated for that
4558    entry.  The label gives the PC value associated with
4559    the line number entry.  */
4560
4561 typedef struct dw_line_info_struct GTY(())
4562 {
4563   unsigned long dw_file_num;
4564   unsigned long dw_line_num;
4565 }
4566 dw_line_info_entry;
4567
4568 /* Line information for functions in separate sections; each one gets its
4569    own sequence.  */
4570 typedef struct dw_separate_line_info_struct GTY(())
4571 {
4572   unsigned long dw_file_num;
4573   unsigned long dw_line_num;
4574   unsigned long function;
4575 }
4576 dw_separate_line_info_entry;
4577
4578 /* Each DIE attribute has a field specifying the attribute kind,
4579    a link to the next attribute in the chain, and an attribute value.
4580    Attributes are typically linked below the DIE they modify.  */
4581
4582 typedef struct dw_attr_struct GTY(())
4583 {
4584   enum dwarf_attribute dw_attr;
4585   dw_val_node dw_attr_val;
4586 }
4587 dw_attr_node;
4588
4589 DEF_VEC_O(dw_attr_node);
4590 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4591
4592 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4593    The children of each node form a circular list linked by
4594    die_sib.  die_child points to the node *before* the "first" child node.  */
4595
4596 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4597 {
4598   enum dwarf_tag die_tag;
4599   char *die_symbol;
4600   VEC(dw_attr_node,gc) * die_attr;
4601   dw_die_ref die_parent;
4602   dw_die_ref die_child;
4603   dw_die_ref die_sib;
4604   dw_die_ref die_definition; /* ref from a specification to its definition */
4605   dw_offset die_offset;
4606   unsigned long die_abbrev;
4607   int die_mark;
4608   /* Die is used and must not be pruned as unused.  */
4609   int die_perennial_p;
4610   unsigned int decl_id;
4611 }
4612 die_node;
4613
4614 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4615 #define FOR_EACH_CHILD(die, c, expr) do {       \
4616   c = die->die_child;                           \
4617   if (c) do {                                   \
4618     c = c->die_sib;                             \
4619     expr;                                       \
4620   } while (c != die->die_child);                \
4621 } while (0)
4622
4623 /* The pubname structure */
4624
4625 typedef struct pubname_struct GTY(())
4626 {
4627   dw_die_ref die;
4628   const char *name;
4629 }
4630 pubname_entry;
4631
4632 DEF_VEC_O(pubname_entry);
4633 DEF_VEC_ALLOC_O(pubname_entry, gc);
4634
4635 struct dw_ranges_struct GTY(())
4636 {
4637   /* If this is positive, it's a block number, otherwise it's a
4638      bitwise-negated index into dw_ranges_by_label.  */
4639   int num;
4640 };
4641
4642 struct dw_ranges_by_label_struct GTY(())
4643 {
4644   const char *begin;
4645   const char *end;
4646 };
4647
4648 /* The limbo die list structure.  */
4649 typedef struct limbo_die_struct GTY(())
4650 {
4651   dw_die_ref die;
4652   tree created_for;
4653   struct limbo_die_struct *next;
4654 }
4655 limbo_die_node;
4656
4657 /* How to start an assembler comment.  */
4658 #ifndef ASM_COMMENT_START
4659 #define ASM_COMMENT_START ";#"
4660 #endif
4661
4662 /* Define a macro which returns nonzero for a TYPE_DECL which was
4663    implicitly generated for a tagged type.
4664
4665    Note that unlike the gcc front end (which generates a NULL named
4666    TYPE_DECL node for each complete tagged type, each array type, and
4667    each function type node created) the g++ front end generates a
4668    _named_ TYPE_DECL node for each tagged type node created.
4669    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4670    generate a DW_TAG_typedef DIE for them.  */
4671
4672 #define TYPE_DECL_IS_STUB(decl)                         \
4673   (DECL_NAME (decl) == NULL_TREE                        \
4674    || (DECL_ARTIFICIAL (decl)                           \
4675        && is_tagged_type (TREE_TYPE (decl))             \
4676        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4677            /* This is necessary for stub decls that     \
4678               appear in nested inline functions.  */    \
4679            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4680                && (decl_ultimate_origin (decl)          \
4681                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4682
4683 /* Information concerning the compilation unit's programming
4684    language, and compiler version.  */
4685
4686 /* Fixed size portion of the DWARF compilation unit header.  */
4687 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4688   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4689
4690 /* Fixed size portion of public names info.  */
4691 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4692
4693 /* Fixed size portion of the address range info.  */
4694 #define DWARF_ARANGES_HEADER_SIZE                                       \
4695   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4696                 DWARF2_ADDR_SIZE * 2)                                   \
4697    - DWARF_INITIAL_LENGTH_SIZE)
4698
4699 /* Size of padding portion in the address range info.  It must be
4700    aligned to twice the pointer size.  */
4701 #define DWARF_ARANGES_PAD_SIZE \
4702   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4703                 DWARF2_ADDR_SIZE * 2)                              \
4704    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4705
4706 /* Use assembler line directives if available.  */
4707 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4708 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4709 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4710 #else
4711 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4712 #endif
4713 #endif
4714
4715 /* Minimum line offset in a special line info. opcode.
4716    This value was chosen to give a reasonable range of values.  */
4717 #define DWARF_LINE_BASE  -10
4718
4719 /* First special line opcode - leave room for the standard opcodes.  */
4720 #define DWARF_LINE_OPCODE_BASE  10
4721
4722 /* Range of line offsets in a special line info. opcode.  */
4723 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4724
4725 /* Flag that indicates the initial value of the is_stmt_start flag.
4726    In the present implementation, we do not mark any lines as
4727    the beginning of a source statement, because that information
4728    is not made available by the GCC front-end.  */
4729 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4730
4731 #ifdef DWARF2_DEBUGGING_INFO
4732 /* This location is used by calc_die_sizes() to keep track
4733    the offset of each DIE within the .debug_info section.  */
4734 static unsigned long next_die_offset;
4735 #endif
4736
4737 /* Record the root of the DIE's built for the current compilation unit.  */
4738 static GTY(()) dw_die_ref comp_unit_die;
4739
4740 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4741 static GTY(()) limbo_die_node *limbo_die_list;
4742
4743 /* Filenames referenced by this compilation unit.  */
4744 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4745
4746 /* A hash table of references to DIE's that describe declarations.
4747    The key is a DECL_UID() which is a unique number identifying each decl.  */
4748 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4749
4750 /* Node of the variable location list.  */
4751 struct var_loc_node GTY ((chain_next ("%h.next")))
4752 {
4753   rtx GTY (()) var_loc_note;
4754   const char * GTY (()) label;
4755   const char * GTY (()) section_label;
4756   struct var_loc_node * GTY (()) next;
4757 };
4758
4759 /* Variable location list.  */
4760 struct var_loc_list_def GTY (())
4761 {
4762   struct var_loc_node * GTY (()) first;
4763
4764   /* Do not mark the last element of the chained list because
4765      it is marked through the chain.  */
4766   struct var_loc_node * GTY ((skip ("%h"))) last;
4767
4768   /* DECL_UID of the variable decl.  */
4769   unsigned int decl_id;
4770 };
4771 typedef struct var_loc_list_def var_loc_list;
4772
4773
4774 /* Table of decl location linked lists.  */
4775 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4776
4777 /* A pointer to the base of a list of references to DIE's that
4778    are uniquely identified by their tag, presence/absence of
4779    children DIE's, and list of attribute/value pairs.  */
4780 static GTY((length ("abbrev_die_table_allocated")))
4781   dw_die_ref *abbrev_die_table;
4782
4783 /* Number of elements currently allocated for abbrev_die_table.  */
4784 static GTY(()) unsigned abbrev_die_table_allocated;
4785
4786 /* Number of elements in type_die_table currently in use.  */
4787 static GTY(()) unsigned abbrev_die_table_in_use;
4788
4789 /* Size (in elements) of increments by which we may expand the
4790    abbrev_die_table.  */
4791 #define ABBREV_DIE_TABLE_INCREMENT 256
4792
4793 /* A pointer to the base of a table that contains line information
4794    for each source code line in .text in the compilation unit.  */
4795 static GTY((length ("line_info_table_allocated")))
4796      dw_line_info_ref line_info_table;
4797
4798 /* Number of elements currently allocated for line_info_table.  */
4799 static GTY(()) unsigned line_info_table_allocated;
4800
4801 /* Number of elements in line_info_table currently in use.  */
4802 static GTY(()) unsigned line_info_table_in_use;
4803
4804 /* A pointer to the base of a table that contains line information
4805    for each source code line outside of .text in the compilation unit.  */
4806 static GTY ((length ("separate_line_info_table_allocated")))
4807      dw_separate_line_info_ref separate_line_info_table;
4808
4809 /* Number of elements currently allocated for separate_line_info_table.  */
4810 static GTY(()) unsigned separate_line_info_table_allocated;
4811
4812 /* Number of elements in separate_line_info_table currently in use.  */
4813 static GTY(()) unsigned separate_line_info_table_in_use;
4814
4815 /* Size (in elements) of increments by which we may expand the
4816    line_info_table.  */
4817 #define LINE_INFO_TABLE_INCREMENT 1024
4818
4819 /* A pointer to the base of a table that contains a list of publicly
4820    accessible names.  */
4821 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4822
4823 /* A pointer to the base of a table that contains a list of publicly
4824    accessible types.  */
4825 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4826
4827 /* Array of dies for which we should generate .debug_arange info.  */
4828 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4829
4830 /* Number of elements currently allocated for arange_table.  */
4831 static GTY(()) unsigned arange_table_allocated;
4832
4833 /* Number of elements in arange_table currently in use.  */
4834 static GTY(()) unsigned arange_table_in_use;
4835
4836 /* Size (in elements) of increments by which we may expand the
4837    arange_table.  */
4838 #define ARANGE_TABLE_INCREMENT 64
4839
4840 /* Array of dies for which we should generate .debug_ranges info.  */
4841 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4842
4843 /* Number of elements currently allocated for ranges_table.  */
4844 static GTY(()) unsigned ranges_table_allocated;
4845
4846 /* Number of elements in ranges_table currently in use.  */
4847 static GTY(()) unsigned ranges_table_in_use;
4848
4849 /* Array of pairs of labels referenced in ranges_table.  */
4850 static GTY ((length ("ranges_by_label_allocated")))
4851      dw_ranges_by_label_ref ranges_by_label;
4852
4853 /* Number of elements currently allocated for ranges_by_label.  */
4854 static GTY(()) unsigned ranges_by_label_allocated;
4855
4856 /* Number of elements in ranges_by_label currently in use.  */
4857 static GTY(()) unsigned ranges_by_label_in_use;
4858
4859 /* Size (in elements) of increments by which we may expand the
4860    ranges_table.  */
4861 #define RANGES_TABLE_INCREMENT 64
4862
4863 /* Whether we have location lists that need outputting */
4864 static GTY(()) bool have_location_lists;
4865
4866 /* Unique label counter.  */
4867 static GTY(()) unsigned int loclabel_num;
4868
4869 #ifdef DWARF2_DEBUGGING_INFO
4870 /* Record whether the function being analyzed contains inlined functions.  */
4871 static int current_function_has_inlines;
4872 #endif
4873 #if 0 && defined (MIPS_DEBUGGING_INFO)
4874 static int comp_unit_has_inlines;
4875 #endif
4876
4877 /* The last file entry emitted by maybe_emit_file().  */
4878 static GTY(()) struct dwarf_file_data * last_emitted_file;
4879
4880 /* Number of internal labels generated by gen_internal_sym().  */
4881 static GTY(()) int label_num;
4882
4883 /* Cached result of previous call to lookup_filename.  */
4884 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4885
4886 #ifdef DWARF2_DEBUGGING_INFO
4887
4888 /* Offset from the "steady-state frame pointer" to the frame base,
4889    within the current function.  */
4890 static HOST_WIDE_INT frame_pointer_fb_offset;
4891
4892 /* Forward declarations for functions defined in this file.  */
4893
4894 static int is_pseudo_reg (const_rtx);
4895 static tree type_main_variant (tree);
4896 static int is_tagged_type (const_tree);
4897 static const char *dwarf_tag_name (unsigned);
4898 static const char *dwarf_attr_name (unsigned);
4899 static const char *dwarf_form_name (unsigned);
4900 static tree decl_ultimate_origin (const_tree);
4901 static tree block_ultimate_origin (const_tree);
4902 static tree decl_class_context (tree);
4903 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4904 static inline enum dw_val_class AT_class (dw_attr_ref);
4905 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4906 static inline unsigned AT_flag (dw_attr_ref);
4907 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4908 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4909 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4910 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4911 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4912                               unsigned long);
4913 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4914                                unsigned int, unsigned char *);
4915 static hashval_t debug_str_do_hash (const void *);
4916 static int debug_str_eq (const void *, const void *);
4917 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4918 static inline const char *AT_string (dw_attr_ref);
4919 static int AT_string_form (dw_attr_ref);
4920 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4921 static void add_AT_specification (dw_die_ref, dw_die_ref);
4922 static inline dw_die_ref AT_ref (dw_attr_ref);
4923 static inline int AT_ref_external (dw_attr_ref);
4924 static inline void set_AT_ref_external (dw_attr_ref, int);
4925 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4926 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4927 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4928 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4929                              dw_loc_list_ref);
4930 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4931 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4932 static inline rtx AT_addr (dw_attr_ref);
4933 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4934 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4935 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4936 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4937                            unsigned HOST_WIDE_INT);
4938 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4939                                unsigned long);
4940 static inline const char *AT_lbl (dw_attr_ref);
4941 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4942 static const char *get_AT_low_pc (dw_die_ref);
4943 static const char *get_AT_hi_pc (dw_die_ref);
4944 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4945 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4946 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4947 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4948 static bool is_c_family (void);
4949 static bool is_cxx (void);
4950 static bool is_java (void);
4951 static bool is_fortran (void);
4952 static bool is_ada (void);
4953 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4954 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4955 static void add_child_die (dw_die_ref, dw_die_ref);
4956 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4957 static dw_die_ref lookup_type_die (tree);
4958 static void equate_type_number_to_die (tree, dw_die_ref);
4959 static hashval_t decl_die_table_hash (const void *);
4960 static int decl_die_table_eq (const void *, const void *);
4961 static dw_die_ref lookup_decl_die (tree);
4962 static hashval_t decl_loc_table_hash (const void *);
4963 static int decl_loc_table_eq (const void *, const void *);
4964 static var_loc_list *lookup_decl_loc (const_tree);
4965 static void equate_decl_number_to_die (tree, dw_die_ref);
4966 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4967 static void print_spaces (FILE *);
4968 static void print_die (dw_die_ref, FILE *);
4969 static void print_dwarf_line_table (FILE *);
4970 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4971 static dw_die_ref pop_compile_unit (dw_die_ref);
4972 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4973 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4974 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4975 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4976 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4977 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4978 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4979 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4980 static void compute_section_prefix (dw_die_ref);
4981 static int is_type_die (dw_die_ref);
4982 static int is_comdat_die (dw_die_ref);
4983 static int is_symbol_die (dw_die_ref);
4984 static void assign_symbol_names (dw_die_ref);
4985 static void break_out_includes (dw_die_ref);
4986 static hashval_t htab_cu_hash (const void *);
4987 static int htab_cu_eq (const void *, const void *);
4988 static void htab_cu_del (void *);
4989 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4990 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4991 static void add_sibling_attributes (dw_die_ref);
4992 static void build_abbrev_table (dw_die_ref);
4993 static void output_location_lists (dw_die_ref);
4994 static int constant_size (long unsigned);
4995 static unsigned long size_of_die (dw_die_ref);
4996 static void calc_die_sizes (dw_die_ref);
4997 static void mark_dies (dw_die_ref);
4998 static void unmark_dies (dw_die_ref);
4999 static void unmark_all_dies (dw_die_ref);
5000 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5001 static unsigned long size_of_aranges (void);
5002 static enum dwarf_form value_format (dw_attr_ref);
5003 static void output_value_format (dw_attr_ref);
5004 static void output_abbrev_section (void);
5005 static void output_die_symbol (dw_die_ref);
5006 static void output_die (dw_die_ref);
5007 static void output_compilation_unit_header (void);
5008 static void output_comp_unit (dw_die_ref, int);
5009 static const char *dwarf2_name (tree, int);
5010 static void add_pubname (tree, dw_die_ref);
5011 static void add_pubname_string (const char *, dw_die_ref);
5012 static void add_pubtype (tree, dw_die_ref);
5013 static void output_pubnames (VEC (pubname_entry,gc) *);
5014 static void add_arange (tree, dw_die_ref);
5015 static void output_aranges (void);
5016 static unsigned int add_ranges_num (int);
5017 static unsigned int add_ranges (const_tree);
5018 static unsigned int add_ranges_by_labels (const char *, const char *);
5019 static void output_ranges (void);
5020 static void output_line_info (void);
5021 static void output_file_names (void);
5022 static dw_die_ref base_type_die (tree);
5023 static int is_base_type (tree);
5024 static bool is_subrange_type (const_tree);
5025 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5026 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5027 static int type_is_enum (const_tree);
5028 static unsigned int dbx_reg_number (const_rtx);
5029 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5030 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5031 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5032                                                 enum var_init_status);
5033 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5034                                                      enum var_init_status);
5035 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5036                                          enum var_init_status);
5037 static int is_based_loc (const_rtx);
5038 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5039                                             enum var_init_status);
5040 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5041                                                enum var_init_status);
5042 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5043 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5044 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5045 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5046 static tree field_type (const_tree);
5047 static unsigned int simple_type_align_in_bits (const_tree);
5048 static unsigned int simple_decl_align_in_bits (const_tree);
5049 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5050 static HOST_WIDE_INT field_byte_offset (const_tree);
5051 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5052                                          dw_loc_descr_ref);
5053 static void add_data_member_location_attribute (dw_die_ref, tree);
5054 static void add_const_value_attribute (dw_die_ref, rtx);
5055 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5056 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5057 static void insert_float (const_rtx, unsigned char *);
5058 static rtx rtl_for_decl_location (tree);
5059 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5060                                                    enum dwarf_attribute);
5061 static void tree_add_const_value_attribute (dw_die_ref, tree);
5062 static void add_name_attribute (dw_die_ref, const char *);
5063 static void add_comp_dir_attribute (dw_die_ref);
5064 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5065 static void add_subscript_info (dw_die_ref, tree, bool);
5066 static void add_byte_size_attribute (dw_die_ref, tree);
5067 static void add_bit_offset_attribute (dw_die_ref, tree);
5068 static void add_bit_size_attribute (dw_die_ref, tree);
5069 static void add_prototyped_attribute (dw_die_ref, tree);
5070 static void add_abstract_origin_attribute (dw_die_ref, tree);
5071 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5072 static void add_src_coords_attributes (dw_die_ref, tree);
5073 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5074 static void push_decl_scope (tree);
5075 static void pop_decl_scope (void);
5076 static dw_die_ref scope_die_for (tree, dw_die_ref);
5077 static inline int local_scope_p (dw_die_ref);
5078 static inline int class_or_namespace_scope_p (dw_die_ref);
5079 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5080 static void add_calling_convention_attribute (dw_die_ref, tree);
5081 static const char *type_tag (const_tree);
5082 static tree member_declared_type (const_tree);
5083 #if 0
5084 static const char *decl_start_label (tree);
5085 #endif
5086 static void gen_array_type_die (tree, dw_die_ref);
5087 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5088 #if 0
5089 static void gen_entry_point_die (tree, dw_die_ref);
5090 #endif
5091 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
5092 static void gen_inlined_structure_type_die (tree, dw_die_ref);
5093 static void gen_inlined_union_type_die (tree, dw_die_ref);
5094 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5095 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
5096 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5097 static void gen_formal_types_die (tree, dw_die_ref);
5098 static void gen_subprogram_die (tree, dw_die_ref);
5099 static void gen_variable_die (tree, dw_die_ref);
5100 static void gen_label_die (tree, dw_die_ref);
5101 static void gen_lexical_block_die (tree, dw_die_ref, int);
5102 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5103 static void gen_field_die (tree, dw_die_ref);
5104 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5105 static dw_die_ref gen_compile_unit_die (const char *);
5106 static void gen_inheritance_die (tree, tree, dw_die_ref);
5107 static void gen_member_die (tree, dw_die_ref);
5108 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5109                                                 enum debug_info_usage);
5110 static void gen_subroutine_type_die (tree, dw_die_ref);
5111 static void gen_typedef_die (tree, dw_die_ref);
5112 static void gen_type_die (tree, dw_die_ref);
5113 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
5114 static void gen_block_die (tree, dw_die_ref, int);
5115 static void decls_for_scope (tree, dw_die_ref, int);
5116 static int is_redundant_typedef (const_tree);
5117 static void gen_namespace_die (tree);
5118 static void gen_decl_die (tree, dw_die_ref);
5119 static dw_die_ref force_decl_die (tree);
5120 static dw_die_ref force_type_die (tree);
5121 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5122 static void declare_in_namespace (tree, dw_die_ref);
5123 static struct dwarf_file_data * lookup_filename (const char *);
5124 static void retry_incomplete_types (void);
5125 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5126 static void splice_child_die (dw_die_ref, dw_die_ref);
5127 static int file_info_cmp (const void *, const void *);
5128 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5129                                      const char *, const char *, unsigned);
5130 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5131                                        const char *, const char *,
5132                                        const char *);
5133 static void output_loc_list (dw_loc_list_ref);
5134 static char *gen_internal_sym (const char *);
5135
5136 static void prune_unmark_dies (dw_die_ref);
5137 static void prune_unused_types_mark (dw_die_ref, int);
5138 static void prune_unused_types_walk (dw_die_ref);
5139 static void prune_unused_types_walk_attribs (dw_die_ref);
5140 static void prune_unused_types_prune (dw_die_ref);
5141 static void prune_unused_types (void);
5142 static int maybe_emit_file (struct dwarf_file_data *fd);
5143
5144 /* Section names used to hold DWARF debugging information.  */
5145 #ifndef DEBUG_INFO_SECTION
5146 #define DEBUG_INFO_SECTION      ".debug_info"
5147 #endif
5148 #ifndef DEBUG_ABBREV_SECTION
5149 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5150 #endif
5151 #ifndef DEBUG_ARANGES_SECTION
5152 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5153 #endif
5154 #ifndef DEBUG_MACINFO_SECTION
5155 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5156 #endif
5157 #ifndef DEBUG_LINE_SECTION
5158 #define DEBUG_LINE_SECTION      ".debug_line"
5159 #endif
5160 #ifndef DEBUG_LOC_SECTION
5161 #define DEBUG_LOC_SECTION       ".debug_loc"
5162 #endif
5163 #ifndef DEBUG_PUBNAMES_SECTION
5164 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5165 #endif
5166 #ifndef DEBUG_STR_SECTION
5167 #define DEBUG_STR_SECTION       ".debug_str"
5168 #endif
5169 #ifndef DEBUG_RANGES_SECTION
5170 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5171 #endif
5172
5173 /* Standard ELF section names for compiled code and data.  */
5174 #ifndef TEXT_SECTION_NAME
5175 #define TEXT_SECTION_NAME       ".text"
5176 #endif
5177
5178 /* Section flags for .debug_str section.  */
5179 #define DEBUG_STR_SECTION_FLAGS \
5180   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5181    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5182    : SECTION_DEBUG)
5183
5184 /* Labels we insert at beginning sections we can reference instead of
5185    the section names themselves.  */
5186
5187 #ifndef TEXT_SECTION_LABEL
5188 #define TEXT_SECTION_LABEL              "Ltext"
5189 #endif
5190 #ifndef COLD_TEXT_SECTION_LABEL
5191 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5192 #endif
5193 #ifndef DEBUG_LINE_SECTION_LABEL
5194 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5195 #endif
5196 #ifndef DEBUG_INFO_SECTION_LABEL
5197 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5198 #endif
5199 #ifndef DEBUG_ABBREV_SECTION_LABEL
5200 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5201 #endif
5202 #ifndef DEBUG_LOC_SECTION_LABEL
5203 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5204 #endif
5205 #ifndef DEBUG_RANGES_SECTION_LABEL
5206 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5207 #endif
5208 #ifndef DEBUG_MACINFO_SECTION_LABEL
5209 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5210 #endif
5211
5212 /* Definitions of defaults for formats and names of various special
5213    (artificial) labels which may be generated within this file (when the -g
5214    options is used and DWARF2_DEBUGGING_INFO is in effect.
5215    If necessary, these may be overridden from within the tm.h file, but
5216    typically, overriding these defaults is unnecessary.  */
5217
5218 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5219 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5220 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5221 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5222 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5223 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5224 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5225 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5226 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5227 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5228
5229 #ifndef TEXT_END_LABEL
5230 #define TEXT_END_LABEL          "Letext"
5231 #endif
5232 #ifndef COLD_END_LABEL
5233 #define COLD_END_LABEL          "Letext_cold"
5234 #endif
5235 #ifndef BLOCK_BEGIN_LABEL
5236 #define BLOCK_BEGIN_LABEL       "LBB"
5237 #endif
5238 #ifndef BLOCK_END_LABEL
5239 #define BLOCK_END_LABEL         "LBE"
5240 #endif
5241 #ifndef LINE_CODE_LABEL
5242 #define LINE_CODE_LABEL         "LM"
5243 #endif
5244 #ifndef SEPARATE_LINE_CODE_LABEL
5245 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5246 #endif
5247
5248 \f
5249 /* We allow a language front-end to designate a function that is to be
5250    called to "demangle" any name before it is put into a DIE.  */
5251
5252 static const char *(*demangle_name_func) (const char *);
5253
5254 void
5255 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5256 {
5257   demangle_name_func = func;
5258 }
5259
5260 /* Test if rtl node points to a pseudo register.  */
5261
5262 static inline int
5263 is_pseudo_reg (const_rtx rtl)
5264 {
5265   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5266           || (GET_CODE (rtl) == SUBREG
5267               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5268 }
5269
5270 /* Return a reference to a type, with its const and volatile qualifiers
5271    removed.  */
5272
5273 static inline tree
5274 type_main_variant (tree type)
5275 {
5276   type = TYPE_MAIN_VARIANT (type);
5277
5278   /* ??? There really should be only one main variant among any group of
5279      variants of a given type (and all of the MAIN_VARIANT values for all
5280      members of the group should point to that one type) but sometimes the C
5281      front-end messes this up for array types, so we work around that bug
5282      here.  */
5283   if (TREE_CODE (type) == ARRAY_TYPE)
5284     while (type != TYPE_MAIN_VARIANT (type))
5285       type = TYPE_MAIN_VARIANT (type);
5286
5287   return type;
5288 }
5289
5290 /* Return nonzero if the given type node represents a tagged type.  */
5291
5292 static inline int
5293 is_tagged_type (const_tree type)
5294 {
5295   enum tree_code code = TREE_CODE (type);
5296
5297   return (code == RECORD_TYPE || code == UNION_TYPE
5298           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5299 }
5300
5301 /* Convert a DIE tag into its string name.  */
5302
5303 static const char *
5304 dwarf_tag_name (unsigned int tag)
5305 {
5306   switch (tag)
5307     {
5308     case DW_TAG_padding:
5309       return "DW_TAG_padding";
5310     case DW_TAG_array_type:
5311       return "DW_TAG_array_type";
5312     case DW_TAG_class_type:
5313       return "DW_TAG_class_type";
5314     case DW_TAG_entry_point:
5315       return "DW_TAG_entry_point";
5316     case DW_TAG_enumeration_type:
5317       return "DW_TAG_enumeration_type";
5318     case DW_TAG_formal_parameter:
5319       return "DW_TAG_formal_parameter";
5320     case DW_TAG_imported_declaration:
5321       return "DW_TAG_imported_declaration";
5322     case DW_TAG_label:
5323       return "DW_TAG_label";
5324     case DW_TAG_lexical_block:
5325       return "DW_TAG_lexical_block";
5326     case DW_TAG_member:
5327       return "DW_TAG_member";
5328     case DW_TAG_pointer_type:
5329       return "DW_TAG_pointer_type";
5330     case DW_TAG_reference_type:
5331       return "DW_TAG_reference_type";
5332     case DW_TAG_compile_unit:
5333       return "DW_TAG_compile_unit";
5334     case DW_TAG_string_type:
5335       return "DW_TAG_string_type";
5336     case DW_TAG_structure_type:
5337       return "DW_TAG_structure_type";
5338     case DW_TAG_subroutine_type:
5339       return "DW_TAG_subroutine_type";
5340     case DW_TAG_typedef:
5341       return "DW_TAG_typedef";
5342     case DW_TAG_union_type:
5343       return "DW_TAG_union_type";
5344     case DW_TAG_unspecified_parameters:
5345       return "DW_TAG_unspecified_parameters";
5346     case DW_TAG_variant:
5347       return "DW_TAG_variant";
5348     case DW_TAG_common_block:
5349       return "DW_TAG_common_block";
5350     case DW_TAG_common_inclusion:
5351       return "DW_TAG_common_inclusion";
5352     case DW_TAG_inheritance:
5353       return "DW_TAG_inheritance";
5354     case DW_TAG_inlined_subroutine:
5355       return "DW_TAG_inlined_subroutine";
5356     case DW_TAG_module:
5357       return "DW_TAG_module";
5358     case DW_TAG_ptr_to_member_type:
5359       return "DW_TAG_ptr_to_member_type";
5360     case DW_TAG_set_type:
5361       return "DW_TAG_set_type";
5362     case DW_TAG_subrange_type:
5363       return "DW_TAG_subrange_type";
5364     case DW_TAG_with_stmt:
5365       return "DW_TAG_with_stmt";
5366     case DW_TAG_access_declaration:
5367       return "DW_TAG_access_declaration";
5368     case DW_TAG_base_type:
5369       return "DW_TAG_base_type";
5370     case DW_TAG_catch_block:
5371       return "DW_TAG_catch_block";
5372     case DW_TAG_const_type:
5373       return "DW_TAG_const_type";
5374     case DW_TAG_constant:
5375       return "DW_TAG_constant";
5376     case DW_TAG_enumerator:
5377       return "DW_TAG_enumerator";
5378     case DW_TAG_file_type:
5379       return "DW_TAG_file_type";
5380     case DW_TAG_friend:
5381       return "DW_TAG_friend";
5382     case DW_TAG_namelist:
5383       return "DW_TAG_namelist";
5384     case DW_TAG_namelist_item:
5385       return "DW_TAG_namelist_item";
5386     case DW_TAG_packed_type:
5387       return "DW_TAG_packed_type";
5388     case DW_TAG_subprogram:
5389       return "DW_TAG_subprogram";
5390     case DW_TAG_template_type_param:
5391       return "DW_TAG_template_type_param";
5392     case DW_TAG_template_value_param:
5393       return "DW_TAG_template_value_param";
5394     case DW_TAG_thrown_type:
5395       return "DW_TAG_thrown_type";
5396     case DW_TAG_try_block:
5397       return "DW_TAG_try_block";
5398     case DW_TAG_variant_part:
5399       return "DW_TAG_variant_part";
5400     case DW_TAG_variable:
5401       return "DW_TAG_variable";
5402     case DW_TAG_volatile_type:
5403       return "DW_TAG_volatile_type";
5404     case DW_TAG_dwarf_procedure:
5405       return "DW_TAG_dwarf_procedure";
5406     case DW_TAG_restrict_type:
5407       return "DW_TAG_restrict_type";
5408     case DW_TAG_interface_type:
5409       return "DW_TAG_interface_type";
5410     case DW_TAG_namespace:
5411       return "DW_TAG_namespace";
5412     case DW_TAG_imported_module:
5413       return "DW_TAG_imported_module";
5414     case DW_TAG_unspecified_type:
5415       return "DW_TAG_unspecified_type";
5416     case DW_TAG_partial_unit:
5417       return "DW_TAG_partial_unit";
5418     case DW_TAG_imported_unit:
5419       return "DW_TAG_imported_unit";
5420     case DW_TAG_condition:
5421       return "DW_TAG_condition";
5422     case DW_TAG_shared_type:
5423       return "DW_TAG_shared_type";
5424     case DW_TAG_MIPS_loop:
5425       return "DW_TAG_MIPS_loop";
5426     case DW_TAG_format_label:
5427       return "DW_TAG_format_label";
5428     case DW_TAG_function_template:
5429       return "DW_TAG_function_template";
5430     case DW_TAG_class_template:
5431       return "DW_TAG_class_template";
5432     case DW_TAG_GNU_BINCL:
5433       return "DW_TAG_GNU_BINCL";
5434     case DW_TAG_GNU_EINCL:
5435       return "DW_TAG_GNU_EINCL";
5436     default:
5437       return "DW_TAG_<unknown>";
5438     }
5439 }
5440
5441 /* Convert a DWARF attribute code into its string name.  */
5442
5443 static const char *
5444 dwarf_attr_name (unsigned int attr)
5445 {
5446   switch (attr)
5447     {
5448     case DW_AT_sibling:
5449       return "DW_AT_sibling";
5450     case DW_AT_location:
5451       return "DW_AT_location";
5452     case DW_AT_name:
5453       return "DW_AT_name";
5454     case DW_AT_ordering:
5455       return "DW_AT_ordering";
5456     case DW_AT_subscr_data:
5457       return "DW_AT_subscr_data";
5458     case DW_AT_byte_size:
5459       return "DW_AT_byte_size";
5460     case DW_AT_bit_offset:
5461       return "DW_AT_bit_offset";
5462     case DW_AT_bit_size:
5463       return "DW_AT_bit_size";
5464     case DW_AT_element_list:
5465       return "DW_AT_element_list";
5466     case DW_AT_stmt_list:
5467       return "DW_AT_stmt_list";
5468     case DW_AT_low_pc:
5469       return "DW_AT_low_pc";
5470     case DW_AT_high_pc:
5471       return "DW_AT_high_pc";
5472     case DW_AT_language:
5473       return "DW_AT_language";
5474     case DW_AT_member:
5475       return "DW_AT_member";
5476     case DW_AT_discr:
5477       return "DW_AT_discr";
5478     case DW_AT_discr_value:
5479       return "DW_AT_discr_value";
5480     case DW_AT_visibility:
5481       return "DW_AT_visibility";
5482     case DW_AT_import:
5483       return "DW_AT_import";
5484     case DW_AT_string_length:
5485       return "DW_AT_string_length";
5486     case DW_AT_common_reference:
5487       return "DW_AT_common_reference";
5488     case DW_AT_comp_dir:
5489       return "DW_AT_comp_dir";
5490     case DW_AT_const_value:
5491       return "DW_AT_const_value";
5492     case DW_AT_containing_type:
5493       return "DW_AT_containing_type";
5494     case DW_AT_default_value:
5495       return "DW_AT_default_value";
5496     case DW_AT_inline:
5497       return "DW_AT_inline";
5498     case DW_AT_is_optional:
5499       return "DW_AT_is_optional";
5500     case DW_AT_lower_bound:
5501       return "DW_AT_lower_bound";
5502     case DW_AT_producer:
5503       return "DW_AT_producer";
5504     case DW_AT_prototyped:
5505       return "DW_AT_prototyped";
5506     case DW_AT_return_addr:
5507       return "DW_AT_return_addr";
5508     case DW_AT_start_scope:
5509       return "DW_AT_start_scope";
5510     case DW_AT_bit_stride:
5511       return "DW_AT_bit_stride";
5512     case DW_AT_upper_bound:
5513       return "DW_AT_upper_bound";
5514     case DW_AT_abstract_origin:
5515       return "DW_AT_abstract_origin";
5516     case DW_AT_accessibility:
5517       return "DW_AT_accessibility";
5518     case DW_AT_address_class:
5519       return "DW_AT_address_class";
5520     case DW_AT_artificial:
5521       return "DW_AT_artificial";
5522     case DW_AT_base_types:
5523       return "DW_AT_base_types";
5524     case DW_AT_calling_convention:
5525       return "DW_AT_calling_convention";
5526     case DW_AT_count:
5527       return "DW_AT_count";
5528     case DW_AT_data_member_location:
5529       return "DW_AT_data_member_location";
5530     case DW_AT_decl_column:
5531       return "DW_AT_decl_column";
5532     case DW_AT_decl_file:
5533       return "DW_AT_decl_file";
5534     case DW_AT_decl_line:
5535       return "DW_AT_decl_line";
5536     case DW_AT_declaration:
5537       return "DW_AT_declaration";
5538     case DW_AT_discr_list:
5539       return "DW_AT_discr_list";
5540     case DW_AT_encoding:
5541       return "DW_AT_encoding";
5542     case DW_AT_external:
5543       return "DW_AT_external";
5544     case DW_AT_frame_base:
5545       return "DW_AT_frame_base";
5546     case DW_AT_friend:
5547       return "DW_AT_friend";
5548     case DW_AT_identifier_case:
5549       return "DW_AT_identifier_case";
5550     case DW_AT_macro_info:
5551       return "DW_AT_macro_info";
5552     case DW_AT_namelist_items:
5553       return "DW_AT_namelist_items";
5554     case DW_AT_priority:
5555       return "DW_AT_priority";
5556     case DW_AT_segment:
5557       return "DW_AT_segment";
5558     case DW_AT_specification:
5559       return "DW_AT_specification";
5560     case DW_AT_static_link:
5561       return "DW_AT_static_link";
5562     case DW_AT_type:
5563       return "DW_AT_type";
5564     case DW_AT_use_location:
5565       return "DW_AT_use_location";
5566     case DW_AT_variable_parameter:
5567       return "DW_AT_variable_parameter";
5568     case DW_AT_virtuality:
5569       return "DW_AT_virtuality";
5570     case DW_AT_vtable_elem_location:
5571       return "DW_AT_vtable_elem_location";
5572
5573     case DW_AT_allocated:
5574       return "DW_AT_allocated";
5575     case DW_AT_associated:
5576       return "DW_AT_associated";
5577     case DW_AT_data_location:
5578       return "DW_AT_data_location";
5579     case DW_AT_byte_stride:
5580       return "DW_AT_byte_stride";
5581     case DW_AT_entry_pc:
5582       return "DW_AT_entry_pc";
5583     case DW_AT_use_UTF8:
5584       return "DW_AT_use_UTF8";
5585     case DW_AT_extension:
5586       return "DW_AT_extension";
5587     case DW_AT_ranges:
5588       return "DW_AT_ranges";
5589     case DW_AT_trampoline:
5590       return "DW_AT_trampoline";
5591     case DW_AT_call_column:
5592       return "DW_AT_call_column";
5593     case DW_AT_call_file:
5594       return "DW_AT_call_file";
5595     case DW_AT_call_line:
5596       return "DW_AT_call_line";
5597
5598     case DW_AT_MIPS_fde:
5599       return "DW_AT_MIPS_fde";
5600     case DW_AT_MIPS_loop_begin:
5601       return "DW_AT_MIPS_loop_begin";
5602     case DW_AT_MIPS_tail_loop_begin:
5603       return "DW_AT_MIPS_tail_loop_begin";
5604     case DW_AT_MIPS_epilog_begin:
5605       return "DW_AT_MIPS_epilog_begin";
5606     case DW_AT_MIPS_loop_unroll_factor:
5607       return "DW_AT_MIPS_loop_unroll_factor";
5608     case DW_AT_MIPS_software_pipeline_depth:
5609       return "DW_AT_MIPS_software_pipeline_depth";
5610     case DW_AT_MIPS_linkage_name:
5611       return "DW_AT_MIPS_linkage_name";
5612     case DW_AT_MIPS_stride:
5613       return "DW_AT_MIPS_stride";
5614     case DW_AT_MIPS_abstract_name:
5615       return "DW_AT_MIPS_abstract_name";
5616     case DW_AT_MIPS_clone_origin:
5617       return "DW_AT_MIPS_clone_origin";
5618     case DW_AT_MIPS_has_inlines:
5619       return "DW_AT_MIPS_has_inlines";
5620
5621     case DW_AT_sf_names:
5622       return "DW_AT_sf_names";
5623     case DW_AT_src_info:
5624       return "DW_AT_src_info";
5625     case DW_AT_mac_info:
5626       return "DW_AT_mac_info";
5627     case DW_AT_src_coords:
5628       return "DW_AT_src_coords";
5629     case DW_AT_body_begin:
5630       return "DW_AT_body_begin";
5631     case DW_AT_body_end:
5632       return "DW_AT_body_end";
5633     case DW_AT_GNU_vector:
5634       return "DW_AT_GNU_vector";
5635
5636     case DW_AT_VMS_rtnbeg_pd_address:
5637       return "DW_AT_VMS_rtnbeg_pd_address";
5638
5639     default:
5640       return "DW_AT_<unknown>";
5641     }
5642 }
5643
5644 /* Convert a DWARF value form code into its string name.  */
5645
5646 static const char *
5647 dwarf_form_name (unsigned int form)
5648 {
5649   switch (form)
5650     {
5651     case DW_FORM_addr:
5652       return "DW_FORM_addr";
5653     case DW_FORM_block2:
5654       return "DW_FORM_block2";
5655     case DW_FORM_block4:
5656       return "DW_FORM_block4";
5657     case DW_FORM_data2:
5658       return "DW_FORM_data2";
5659     case DW_FORM_data4:
5660       return "DW_FORM_data4";
5661     case DW_FORM_data8:
5662       return "DW_FORM_data8";
5663     case DW_FORM_string:
5664       return "DW_FORM_string";
5665     case DW_FORM_block:
5666       return "DW_FORM_block";
5667     case DW_FORM_block1:
5668       return "DW_FORM_block1";
5669     case DW_FORM_data1:
5670       return "DW_FORM_data1";
5671     case DW_FORM_flag:
5672       return "DW_FORM_flag";
5673     case DW_FORM_sdata:
5674       return "DW_FORM_sdata";
5675     case DW_FORM_strp:
5676       return "DW_FORM_strp";
5677     case DW_FORM_udata:
5678       return "DW_FORM_udata";
5679     case DW_FORM_ref_addr:
5680       return "DW_FORM_ref_addr";
5681     case DW_FORM_ref1:
5682       return "DW_FORM_ref1";
5683     case DW_FORM_ref2:
5684       return "DW_FORM_ref2";
5685     case DW_FORM_ref4:
5686       return "DW_FORM_ref4";
5687     case DW_FORM_ref8:
5688       return "DW_FORM_ref8";
5689     case DW_FORM_ref_udata:
5690       return "DW_FORM_ref_udata";
5691     case DW_FORM_indirect:
5692       return "DW_FORM_indirect";
5693     default:
5694       return "DW_FORM_<unknown>";
5695     }
5696 }
5697 \f
5698 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5699    instance of an inlined instance of a decl which is local to an inline
5700    function, so we have to trace all of the way back through the origin chain
5701    to find out what sort of node actually served as the original seed for the
5702    given block.  */
5703
5704 static tree
5705 decl_ultimate_origin (const_tree decl)
5706 {
5707   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5708     return NULL_TREE;
5709
5710   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5711      nodes in the function to point to themselves; ignore that if
5712      we're trying to output the abstract instance of this function.  */
5713   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5714     return NULL_TREE;
5715
5716   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5717      most distant ancestor, this should never happen.  */
5718   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5719
5720   return DECL_ABSTRACT_ORIGIN (decl);
5721 }
5722
5723 /* Determine the "ultimate origin" of a block.  The block may be an inlined
5724    instance of an inlined instance of a block which is local to an inline
5725    function, so we have to trace all of the way back through the origin chain
5726    to find out what sort of node actually served as the original seed for the
5727    given block.  */
5728
5729 static tree
5730 block_ultimate_origin (const_tree block)
5731 {
5732   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
5733
5734   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
5735      nodes in the function to point to themselves; ignore that if
5736      we're trying to output the abstract instance of this function.  */
5737   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
5738     return NULL_TREE;
5739
5740   if (immediate_origin == NULL_TREE)
5741     return NULL_TREE;
5742   else
5743     {
5744       tree ret_val;
5745       tree lookahead = immediate_origin;
5746
5747       do
5748         {
5749           ret_val = lookahead;
5750           lookahead = (TREE_CODE (ret_val) == BLOCK
5751                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5752         }
5753       while (lookahead != NULL && lookahead != ret_val);
5754
5755       /* The block's abstract origin chain may not be the *ultimate* origin of
5756          the block. It could lead to a DECL that has an abstract origin set.
5757          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5758          will give us if it has one).  Note that DECL's abstract origins are
5759          supposed to be the most distant ancestor (or so decl_ultimate_origin
5760          claims), so we don't need to loop following the DECL origins.  */
5761       if (DECL_P (ret_val))
5762         return DECL_ORIGIN (ret_val);
5763
5764       return ret_val;
5765     }
5766 }
5767
5768 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5769    of a virtual function may refer to a base class, so we check the 'this'
5770    parameter.  */
5771
5772 static tree
5773 decl_class_context (tree decl)
5774 {
5775   tree context = NULL_TREE;
5776
5777   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5778     context = DECL_CONTEXT (decl);
5779   else
5780     context = TYPE_MAIN_VARIANT
5781       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5782
5783   if (context && !TYPE_P (context))
5784     context = NULL_TREE;
5785
5786   return context;
5787 }
5788 \f
5789 /* Add an attribute/value pair to a DIE.  */
5790
5791 static inline void
5792 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5793 {
5794   /* Maybe this should be an assert?  */
5795   if (die == NULL)
5796     return;
5797
5798   if (die->die_attr == NULL)
5799     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5800   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5801 }
5802
5803 static inline enum dw_val_class
5804 AT_class (dw_attr_ref a)
5805 {
5806   return a->dw_attr_val.val_class;
5807 }
5808
5809 /* Add a flag value attribute to a DIE.  */
5810
5811 static inline void
5812 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5813 {
5814   dw_attr_node attr;
5815
5816   attr.dw_attr = attr_kind;
5817   attr.dw_attr_val.val_class = dw_val_class_flag;
5818   attr.dw_attr_val.v.val_flag = flag;
5819   add_dwarf_attr (die, &attr);
5820 }
5821
5822 static inline unsigned
5823 AT_flag (dw_attr_ref a)
5824 {
5825   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5826   return a->dw_attr_val.v.val_flag;
5827 }
5828
5829 /* Add a signed integer attribute value to a DIE.  */
5830
5831 static inline void
5832 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5833 {
5834   dw_attr_node attr;
5835
5836   attr.dw_attr = attr_kind;
5837   attr.dw_attr_val.val_class = dw_val_class_const;
5838   attr.dw_attr_val.v.val_int = int_val;
5839   add_dwarf_attr (die, &attr);
5840 }
5841
5842 static inline HOST_WIDE_INT
5843 AT_int (dw_attr_ref a)
5844 {
5845   gcc_assert (a && AT_class (a) == dw_val_class_const);
5846   return a->dw_attr_val.v.val_int;
5847 }
5848
5849 /* Add an unsigned integer attribute value to a DIE.  */
5850
5851 static inline void
5852 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5853                  unsigned HOST_WIDE_INT unsigned_val)
5854 {
5855   dw_attr_node attr;
5856
5857   attr.dw_attr = attr_kind;
5858   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5859   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5860   add_dwarf_attr (die, &attr);
5861 }
5862
5863 static inline unsigned HOST_WIDE_INT
5864 AT_unsigned (dw_attr_ref a)
5865 {
5866   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5867   return a->dw_attr_val.v.val_unsigned;
5868 }
5869
5870 /* Add an unsigned double integer attribute value to a DIE.  */
5871
5872 static inline void
5873 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5874                   long unsigned int val_hi, long unsigned int val_low)
5875 {
5876   dw_attr_node attr;
5877
5878   attr.dw_attr = attr_kind;
5879   attr.dw_attr_val.val_class = dw_val_class_long_long;
5880   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5881   attr.dw_attr_val.v.val_long_long.low = val_low;
5882   add_dwarf_attr (die, &attr);
5883 }
5884
5885 /* Add a floating point attribute value to a DIE and return it.  */
5886
5887 static inline void
5888 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5889             unsigned int length, unsigned int elt_size, unsigned char *array)
5890 {
5891   dw_attr_node attr;
5892
5893   attr.dw_attr = attr_kind;
5894   attr.dw_attr_val.val_class = dw_val_class_vec;
5895   attr.dw_attr_val.v.val_vec.length = length;
5896   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5897   attr.dw_attr_val.v.val_vec.array = array;
5898   add_dwarf_attr (die, &attr);
5899 }
5900
5901 /* Hash and equality functions for debug_str_hash.  */
5902
5903 static hashval_t
5904 debug_str_do_hash (const void *x)
5905 {
5906   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5907 }
5908
5909 static int
5910 debug_str_eq (const void *x1, const void *x2)
5911 {
5912   return strcmp ((((const struct indirect_string_node *)x1)->str),
5913                  (const char *)x2) == 0;
5914 }
5915
5916 /* Add a string attribute value to a DIE.  */
5917
5918 static inline void
5919 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5920 {
5921   dw_attr_node attr;
5922   struct indirect_string_node *node;
5923   void **slot;
5924
5925   if (! debug_str_hash)
5926     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5927                                       debug_str_eq, NULL);
5928
5929   slot = htab_find_slot_with_hash (debug_str_hash, str,
5930                                    htab_hash_string (str), INSERT);
5931   if (*slot == NULL)
5932     {
5933       node = (struct indirect_string_node *)
5934                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5935       node->str = ggc_strdup (str);
5936       *slot = node;
5937     }
5938   else
5939     node = (struct indirect_string_node *) *slot;
5940
5941   node->refcount++;
5942
5943   attr.dw_attr = attr_kind;
5944   attr.dw_attr_val.val_class = dw_val_class_str;
5945   attr.dw_attr_val.v.val_str = node;
5946   add_dwarf_attr (die, &attr);
5947 }
5948
5949 static inline const char *
5950 AT_string (dw_attr_ref a)
5951 {
5952   gcc_assert (a && AT_class (a) == dw_val_class_str);
5953   return a->dw_attr_val.v.val_str->str;
5954 }
5955
5956 /* Find out whether a string should be output inline in DIE
5957    or out-of-line in .debug_str section.  */
5958
5959 static int
5960 AT_string_form (dw_attr_ref a)
5961 {
5962   struct indirect_string_node *node;
5963   unsigned int len;
5964   char label[32];
5965
5966   gcc_assert (a && AT_class (a) == dw_val_class_str);
5967
5968   node = a->dw_attr_val.v.val_str;
5969   if (node->form)
5970     return node->form;
5971
5972   len = strlen (node->str) + 1;
5973
5974   /* If the string is shorter or equal to the size of the reference, it is
5975      always better to put it inline.  */
5976   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5977     return node->form = DW_FORM_string;
5978
5979   /* If we cannot expect the linker to merge strings in .debug_str
5980      section, only put it into .debug_str if it is worth even in this
5981      single module.  */
5982   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5983       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5984     return node->form = DW_FORM_string;
5985
5986   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5987   ++dw2_string_counter;
5988   node->label = xstrdup (label);
5989
5990   return node->form = DW_FORM_strp;
5991 }
5992
5993 /* Add a DIE reference attribute value to a DIE.  */
5994
5995 static inline void
5996 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5997 {
5998   dw_attr_node attr;
5999
6000   attr.dw_attr = attr_kind;
6001   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6002   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6003   attr.dw_attr_val.v.val_die_ref.external = 0;
6004   add_dwarf_attr (die, &attr);
6005 }
6006
6007 /* Add an AT_specification attribute to a DIE, and also make the back
6008    pointer from the specification to the definition.  */
6009
6010 static inline void
6011 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6012 {
6013   add_AT_die_ref (die, DW_AT_specification, targ_die);
6014   gcc_assert (!targ_die->die_definition);
6015   targ_die->die_definition = die;
6016 }
6017
6018 static inline dw_die_ref
6019 AT_ref (dw_attr_ref a)
6020 {
6021   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6022   return a->dw_attr_val.v.val_die_ref.die;
6023 }
6024
6025 static inline int
6026 AT_ref_external (dw_attr_ref a)
6027 {
6028   if (a && AT_class (a) == dw_val_class_die_ref)
6029     return a->dw_attr_val.v.val_die_ref.external;
6030
6031   return 0;
6032 }
6033
6034 static inline void
6035 set_AT_ref_external (dw_attr_ref a, int i)
6036 {
6037   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6038   a->dw_attr_val.v.val_die_ref.external = i;
6039 }
6040
6041 /* Add an FDE reference attribute value to a DIE.  */
6042
6043 static inline void
6044 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6045 {
6046   dw_attr_node attr;
6047
6048   attr.dw_attr = attr_kind;
6049   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6050   attr.dw_attr_val.v.val_fde_index = targ_fde;
6051   add_dwarf_attr (die, &attr);
6052 }
6053
6054 /* Add a location description attribute value to a DIE.  */
6055
6056 static inline void
6057 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6058 {
6059   dw_attr_node attr;
6060
6061   attr.dw_attr = attr_kind;
6062   attr.dw_attr_val.val_class = dw_val_class_loc;
6063   attr.dw_attr_val.v.val_loc = loc;
6064   add_dwarf_attr (die, &attr);
6065 }
6066
6067 static inline dw_loc_descr_ref
6068 AT_loc (dw_attr_ref a)
6069 {
6070   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6071   return a->dw_attr_val.v.val_loc;
6072 }
6073
6074 static inline void
6075 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6076 {
6077   dw_attr_node attr;
6078
6079   attr.dw_attr = attr_kind;
6080   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6081   attr.dw_attr_val.v.val_loc_list = loc_list;
6082   add_dwarf_attr (die, &attr);
6083   have_location_lists = true;
6084 }
6085
6086 static inline dw_loc_list_ref
6087 AT_loc_list (dw_attr_ref a)
6088 {
6089   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6090   return a->dw_attr_val.v.val_loc_list;
6091 }
6092
6093 /* Add an address constant attribute value to a DIE.  */
6094
6095 static inline void
6096 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6097 {
6098   dw_attr_node attr;
6099
6100   attr.dw_attr = attr_kind;
6101   attr.dw_attr_val.val_class = dw_val_class_addr;
6102   attr.dw_attr_val.v.val_addr = addr;
6103   add_dwarf_attr (die, &attr);
6104 }
6105
6106 /* Get the RTX from to an address DIE attribute.  */
6107
6108 static inline rtx
6109 AT_addr (dw_attr_ref a)
6110 {
6111   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6112   return a->dw_attr_val.v.val_addr;
6113 }
6114
6115 /* Add a file attribute value to a DIE.  */
6116
6117 static inline void
6118 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6119              struct dwarf_file_data *fd)
6120 {
6121   dw_attr_node attr;
6122
6123   attr.dw_attr = attr_kind;
6124   attr.dw_attr_val.val_class = dw_val_class_file;
6125   attr.dw_attr_val.v.val_file = fd;
6126   add_dwarf_attr (die, &attr);
6127 }
6128
6129 /* Get the dwarf_file_data from a file DIE attribute.  */
6130
6131 static inline struct dwarf_file_data *
6132 AT_file (dw_attr_ref a)
6133 {
6134   gcc_assert (a && AT_class (a) == dw_val_class_file);
6135   return a->dw_attr_val.v.val_file;
6136 }
6137
6138 /* Add a label identifier attribute value to a DIE.  */
6139
6140 static inline void
6141 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6142 {
6143   dw_attr_node attr;
6144
6145   attr.dw_attr = attr_kind;
6146   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6147   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6148   add_dwarf_attr (die, &attr);
6149 }
6150
6151 /* Add a section offset attribute value to a DIE, an offset into the
6152    debug_line section.  */
6153
6154 static inline void
6155 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6156                 const char *label)
6157 {
6158   dw_attr_node attr;
6159
6160   attr.dw_attr = attr_kind;
6161   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6162   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6163   add_dwarf_attr (die, &attr);
6164 }
6165
6166 /* Add a section offset attribute value to a DIE, an offset into the
6167    debug_macinfo section.  */
6168
6169 static inline void
6170 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6171                const char *label)
6172 {
6173   dw_attr_node attr;
6174
6175   attr.dw_attr = attr_kind;
6176   attr.dw_attr_val.val_class = dw_val_class_macptr;
6177   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6178   add_dwarf_attr (die, &attr);
6179 }
6180
6181 /* Add an offset attribute value to a DIE.  */
6182
6183 static inline void
6184 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6185                unsigned HOST_WIDE_INT offset)
6186 {
6187   dw_attr_node attr;
6188
6189   attr.dw_attr = attr_kind;
6190   attr.dw_attr_val.val_class = dw_val_class_offset;
6191   attr.dw_attr_val.v.val_offset = offset;
6192   add_dwarf_attr (die, &attr);
6193 }
6194
6195 /* Add an range_list attribute value to a DIE.  */
6196
6197 static void
6198 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6199                    long unsigned int offset)
6200 {
6201   dw_attr_node attr;
6202
6203   attr.dw_attr = attr_kind;
6204   attr.dw_attr_val.val_class = dw_val_class_range_list;
6205   attr.dw_attr_val.v.val_offset = offset;
6206   add_dwarf_attr (die, &attr);
6207 }
6208
6209 static inline const char *
6210 AT_lbl (dw_attr_ref a)
6211 {
6212   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6213                     || AT_class (a) == dw_val_class_lineptr
6214                     || AT_class (a) == dw_val_class_macptr));
6215   return a->dw_attr_val.v.val_lbl_id;
6216 }
6217
6218 /* Get the attribute of type attr_kind.  */
6219
6220 static dw_attr_ref
6221 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6222 {
6223   dw_attr_ref a;
6224   unsigned ix;
6225   dw_die_ref spec = NULL;
6226
6227   if (! die)
6228     return NULL;
6229
6230   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6231     if (a->dw_attr == attr_kind)
6232       return a;
6233     else if (a->dw_attr == DW_AT_specification
6234              || a->dw_attr == DW_AT_abstract_origin)
6235       spec = AT_ref (a);
6236
6237   if (spec)
6238     return get_AT (spec, attr_kind);
6239
6240   return NULL;
6241 }
6242
6243 /* Return the "low pc" attribute value, typically associated with a subprogram
6244    DIE.  Return null if the "low pc" attribute is either not present, or if it
6245    cannot be represented as an assembler label identifier.  */
6246
6247 static inline const char *
6248 get_AT_low_pc (dw_die_ref die)
6249 {
6250   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6251
6252   return a ? AT_lbl (a) : NULL;
6253 }
6254
6255 /* Return the "high pc" attribute value, typically associated with a subprogram
6256    DIE.  Return null if the "high pc" attribute is either not present, or if it
6257    cannot be represented as an assembler label identifier.  */
6258
6259 static inline const char *
6260 get_AT_hi_pc (dw_die_ref die)
6261 {
6262   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6263
6264   return a ? AT_lbl (a) : NULL;
6265 }
6266
6267 /* Return the value of the string attribute designated by ATTR_KIND, or
6268    NULL if it is not present.  */
6269
6270 static inline const char *
6271 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6272 {
6273   dw_attr_ref a = get_AT (die, attr_kind);
6274
6275   return a ? AT_string (a) : NULL;
6276 }
6277
6278 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6279    if it is not present.  */
6280
6281 static inline int
6282 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6283 {
6284   dw_attr_ref a = get_AT (die, attr_kind);
6285
6286   return a ? AT_flag (a) : 0;
6287 }
6288
6289 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6290    if it is not present.  */
6291
6292 static inline unsigned
6293 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6294 {
6295   dw_attr_ref a = get_AT (die, attr_kind);
6296
6297   return a ? AT_unsigned (a) : 0;
6298 }
6299
6300 static inline dw_die_ref
6301 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6302 {
6303   dw_attr_ref a = get_AT (die, attr_kind);
6304
6305   return a ? AT_ref (a) : NULL;
6306 }
6307
6308 static inline struct dwarf_file_data *
6309 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6310 {
6311   dw_attr_ref a = get_AT (die, attr_kind);
6312
6313   return a ? AT_file (a) : NULL;
6314 }
6315
6316 /* Return TRUE if the language is C or C++.  */
6317
6318 static inline bool
6319 is_c_family (void)
6320 {
6321   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6322
6323   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6324           || lang == DW_LANG_C99
6325           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6326 }
6327
6328 /* Return TRUE if the language is C++.  */
6329
6330 static inline bool
6331 is_cxx (void)
6332 {
6333   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6334
6335   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6336 }
6337
6338 /* Return TRUE if the language is Fortran.  */
6339
6340 static inline bool
6341 is_fortran (void)
6342 {
6343   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6344
6345   return (lang == DW_LANG_Fortran77
6346           || lang == DW_LANG_Fortran90
6347           || lang == DW_LANG_Fortran95);
6348 }
6349
6350 /* Return TRUE if the language is Java.  */
6351
6352 static inline bool
6353 is_java (void)
6354 {
6355   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6356
6357   return lang == DW_LANG_Java;
6358 }
6359
6360 /* Return TRUE if the language is Ada.  */
6361
6362 static inline bool
6363 is_ada (void)
6364 {
6365   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6366
6367   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6368 }
6369
6370 /* Remove the specified attribute if present.  */
6371
6372 static void
6373 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6374 {
6375   dw_attr_ref a;
6376   unsigned ix;
6377
6378   if (! die)
6379     return;
6380
6381   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6382     if (a->dw_attr == attr_kind)
6383       {
6384         if (AT_class (a) == dw_val_class_str)
6385           if (a->dw_attr_val.v.val_str->refcount)
6386             a->dw_attr_val.v.val_str->refcount--;
6387
6388         /* VEC_ordered_remove should help reduce the number of abbrevs
6389            that are needed.  */
6390         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6391         return;
6392       }
6393 }
6394
6395 /* Remove CHILD from its parent.  PREV must have the property that
6396    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6397
6398 static void
6399 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6400 {
6401   gcc_assert (child->die_parent == prev->die_parent);
6402   gcc_assert (prev->die_sib == child);
6403   if (prev == child)
6404     {
6405       gcc_assert (child->die_parent->die_child == child);
6406       prev = NULL;
6407     }
6408   else
6409     prev->die_sib = child->die_sib;
6410   if (child->die_parent->die_child == child)
6411     child->die_parent->die_child = prev;
6412 }
6413
6414 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6415    matches TAG.  */
6416
6417 static void
6418 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6419 {
6420   dw_die_ref c;
6421
6422   c = die->die_child;
6423   if (c) do {
6424     dw_die_ref prev = c;
6425     c = c->die_sib;
6426     while (c->die_tag == tag)
6427       {
6428         remove_child_with_prev (c, prev);
6429         /* Might have removed every child.  */
6430         if (c == c->die_sib)
6431           return;
6432         c = c->die_sib;
6433       }
6434   } while (c != die->die_child);
6435 }
6436
6437 /* Add a CHILD_DIE as the last child of DIE.  */
6438
6439 static void
6440 add_child_die (dw_die_ref die, dw_die_ref child_die)
6441 {
6442   /* FIXME this should probably be an assert.  */
6443   if (! die || ! child_die)
6444     return;
6445   gcc_assert (die != child_die);
6446
6447   child_die->die_parent = die;
6448   if (die->die_child)
6449     {
6450       child_die->die_sib = die->die_child->die_sib;
6451       die->die_child->die_sib = child_die;
6452     }
6453   else
6454     child_die->die_sib = child_die;
6455   die->die_child = child_die;
6456 }
6457
6458 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6459    is the specification, to the end of PARENT's list of children.
6460    This is done by removing and re-adding it.  */
6461
6462 static void
6463 splice_child_die (dw_die_ref parent, dw_die_ref child)
6464 {
6465   dw_die_ref p;
6466
6467   /* We want the declaration DIE from inside the class, not the
6468      specification DIE at toplevel.  */
6469   if (child->die_parent != parent)
6470     {
6471       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6472
6473       if (tmp)
6474         child = tmp;
6475     }
6476
6477   gcc_assert (child->die_parent == parent
6478               || (child->die_parent
6479                   == get_AT_ref (parent, DW_AT_specification)));
6480
6481   for (p = child->die_parent->die_child; ; p = p->die_sib)
6482     if (p->die_sib == child)
6483       {
6484         remove_child_with_prev (child, p);
6485         break;
6486       }
6487
6488   add_child_die (parent, child);
6489 }
6490
6491 /* Return a pointer to a newly created DIE node.  */
6492
6493 static inline dw_die_ref
6494 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6495 {
6496   dw_die_ref die = GGC_CNEW (die_node);
6497
6498   die->die_tag = tag_value;
6499
6500   if (parent_die != NULL)
6501     add_child_die (parent_die, die);
6502   else
6503     {
6504       limbo_die_node *limbo_node;
6505
6506       limbo_node = GGC_CNEW (limbo_die_node);
6507       limbo_node->die = die;
6508       limbo_node->created_for = t;
6509       limbo_node->next = limbo_die_list;
6510       limbo_die_list = limbo_node;
6511     }
6512
6513   return die;
6514 }
6515
6516 /* Return the DIE associated with the given type specifier.  */
6517
6518 static inline dw_die_ref
6519 lookup_type_die (tree type)
6520 {
6521   return TYPE_SYMTAB_DIE (type);
6522 }
6523
6524 /* Equate a DIE to a given type specifier.  */
6525
6526 static inline void
6527 equate_type_number_to_die (tree type, dw_die_ref type_die)
6528 {
6529   TYPE_SYMTAB_DIE (type) = type_die;
6530 }
6531
6532 /* Returns a hash value for X (which really is a die_struct).  */
6533
6534 static hashval_t
6535 decl_die_table_hash (const void *x)
6536 {
6537   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6538 }
6539
6540 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6541
6542 static int
6543 decl_die_table_eq (const void *x, const void *y)
6544 {
6545   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6546 }
6547
6548 /* Return the DIE associated with a given declaration.  */
6549
6550 static inline dw_die_ref
6551 lookup_decl_die (tree decl)
6552 {
6553   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6554 }
6555
6556 /* Returns a hash value for X (which really is a var_loc_list).  */
6557
6558 static hashval_t
6559 decl_loc_table_hash (const void *x)
6560 {
6561   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6562 }
6563
6564 /* Return nonzero if decl_id of var_loc_list X is the same as
6565    UID of decl *Y.  */
6566
6567 static int
6568 decl_loc_table_eq (const void *x, const void *y)
6569 {
6570   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6571 }
6572
6573 /* Return the var_loc list associated with a given declaration.  */
6574
6575 static inline var_loc_list *
6576 lookup_decl_loc (const_tree decl)
6577 {
6578   return (var_loc_list *)
6579     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6580 }
6581
6582 /* Equate a DIE to a particular declaration.  */
6583
6584 static void
6585 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6586 {
6587   unsigned int decl_id = DECL_UID (decl);
6588   void **slot;
6589
6590   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6591   *slot = decl_die;
6592   decl_die->decl_id = decl_id;
6593 }
6594
6595 /* Add a variable location node to the linked list for DECL.  */
6596
6597 static void
6598 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6599 {
6600   unsigned int decl_id = DECL_UID (decl);
6601   var_loc_list *temp;
6602   void **slot;
6603
6604   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6605   if (*slot == NULL)
6606     {
6607       temp = GGC_CNEW (var_loc_list);
6608       temp->decl_id = decl_id;
6609       *slot = temp;
6610     }
6611   else
6612     temp = (var_loc_list *) *slot;
6613
6614   if (temp->last)
6615     {
6616       /* If the current location is the same as the end of the list,
6617          and either both or neither of the locations is uninitialized,
6618          we have nothing to do.  */
6619       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6620                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6621           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6622                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6623               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6624                    == VAR_INIT_STATUS_UNINITIALIZED)
6625                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6626                       == VAR_INIT_STATUS_UNINITIALIZED))))
6627         {
6628           /* Add LOC to the end of list and update LAST.  */
6629           temp->last->next = loc;
6630           temp->last = loc;
6631         }
6632     }
6633   /* Do not add empty location to the beginning of the list.  */
6634   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6635     {
6636       temp->first = loc;
6637       temp->last = loc;
6638     }
6639 }
6640 \f
6641 /* Keep track of the number of spaces used to indent the
6642    output of the debugging routines that print the structure of
6643    the DIE internal representation.  */
6644 static int print_indent;
6645
6646 /* Indent the line the number of spaces given by print_indent.  */
6647
6648 static inline void
6649 print_spaces (FILE *outfile)
6650 {
6651   fprintf (outfile, "%*s", print_indent, "");
6652 }
6653
6654 /* Print the information associated with a given DIE, and its children.
6655    This routine is a debugging aid only.  */
6656
6657 static void
6658 print_die (dw_die_ref die, FILE *outfile)
6659 {
6660   dw_attr_ref a;
6661   dw_die_ref c;
6662   unsigned ix;
6663
6664   print_spaces (outfile);
6665   fprintf (outfile, "DIE %4ld: %s\n",
6666            die->die_offset, dwarf_tag_name (die->die_tag));
6667   print_spaces (outfile);
6668   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6669   fprintf (outfile, " offset: %ld\n", die->die_offset);
6670
6671   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6672     {
6673       print_spaces (outfile);
6674       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6675
6676       switch (AT_class (a))
6677         {
6678         case dw_val_class_addr:
6679           fprintf (outfile, "address");
6680           break;
6681         case dw_val_class_offset:
6682           fprintf (outfile, "offset");
6683           break;
6684         case dw_val_class_loc:
6685           fprintf (outfile, "location descriptor");
6686           break;
6687         case dw_val_class_loc_list:
6688           fprintf (outfile, "location list -> label:%s",
6689                    AT_loc_list (a)->ll_symbol);
6690           break;
6691         case dw_val_class_range_list:
6692           fprintf (outfile, "range list");
6693           break;
6694         case dw_val_class_const:
6695           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6696           break;
6697         case dw_val_class_unsigned_const:
6698           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6699           break;
6700         case dw_val_class_long_long:
6701           fprintf (outfile, "constant (%lu,%lu)",
6702                    a->dw_attr_val.v.val_long_long.hi,
6703                    a->dw_attr_val.v.val_long_long.low);
6704           break;
6705         case dw_val_class_vec:
6706           fprintf (outfile, "floating-point or vector constant");
6707           break;
6708         case dw_val_class_flag:
6709           fprintf (outfile, "%u", AT_flag (a));
6710           break;
6711         case dw_val_class_die_ref:
6712           if (AT_ref (a) != NULL)
6713             {
6714               if (AT_ref (a)->die_symbol)
6715                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6716               else
6717                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6718             }
6719           else
6720             fprintf (outfile, "die -> <null>");
6721           break;
6722         case dw_val_class_lbl_id:
6723         case dw_val_class_lineptr:
6724         case dw_val_class_macptr:
6725           fprintf (outfile, "label: %s", AT_lbl (a));
6726           break;
6727         case dw_val_class_str:
6728           if (AT_string (a) != NULL)
6729             fprintf (outfile, "\"%s\"", AT_string (a));
6730           else
6731             fprintf (outfile, "<null>");
6732           break;
6733         case dw_val_class_file:
6734           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6735                    AT_file (a)->emitted_number);
6736           break;
6737         default:
6738           break;
6739         }
6740
6741       fprintf (outfile, "\n");
6742     }
6743
6744   if (die->die_child != NULL)
6745     {
6746       print_indent += 4;
6747       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6748       print_indent -= 4;
6749     }
6750   if (print_indent == 0)
6751     fprintf (outfile, "\n");
6752 }
6753
6754 /* Print the contents of the source code line number correspondence table.
6755    This routine is a debugging aid only.  */
6756
6757 static void
6758 print_dwarf_line_table (FILE *outfile)
6759 {
6760   unsigned i;
6761   dw_line_info_ref line_info;
6762
6763   fprintf (outfile, "\n\nDWARF source line information\n");
6764   for (i = 1; i < line_info_table_in_use; i++)
6765     {
6766       line_info = &line_info_table[i];
6767       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6768                line_info->dw_file_num,
6769                line_info->dw_line_num);
6770     }
6771
6772   fprintf (outfile, "\n\n");
6773 }
6774
6775 /* Print the information collected for a given DIE.  */
6776
6777 void
6778 debug_dwarf_die (dw_die_ref die)
6779 {
6780   print_die (die, stderr);
6781 }
6782
6783 /* Print all DWARF information collected for the compilation unit.
6784    This routine is a debugging aid only.  */
6785
6786 void
6787 debug_dwarf (void)
6788 {
6789   print_indent = 0;
6790   print_die (comp_unit_die, stderr);
6791   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6792     print_dwarf_line_table (stderr);
6793 }
6794 \f
6795 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6796    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6797    DIE that marks the start of the DIEs for this include file.  */
6798
6799 static dw_die_ref
6800 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6801 {
6802   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6803   dw_die_ref new_unit = gen_compile_unit_die (filename);
6804
6805   new_unit->die_sib = old_unit;
6806   return new_unit;
6807 }
6808
6809 /* Close an include-file CU and reopen the enclosing one.  */
6810
6811 static dw_die_ref
6812 pop_compile_unit (dw_die_ref old_unit)
6813 {
6814   dw_die_ref new_unit = old_unit->die_sib;
6815
6816   old_unit->die_sib = NULL;
6817   return new_unit;
6818 }
6819
6820 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6821 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6822
6823 /* Calculate the checksum of a location expression.  */
6824
6825 static inline void
6826 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6827 {
6828   CHECKSUM (loc->dw_loc_opc);
6829   CHECKSUM (loc->dw_loc_oprnd1);
6830   CHECKSUM (loc->dw_loc_oprnd2);
6831 }
6832
6833 /* Calculate the checksum of an attribute.  */
6834
6835 static void
6836 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6837 {
6838   dw_loc_descr_ref loc;
6839   rtx r;
6840
6841   CHECKSUM (at->dw_attr);
6842
6843   /* We don't care that this was compiled with a different compiler
6844      snapshot; if the output is the same, that's what matters.  */
6845   if (at->dw_attr == DW_AT_producer)
6846     return;
6847
6848   switch (AT_class (at))
6849     {
6850     case dw_val_class_const:
6851       CHECKSUM (at->dw_attr_val.v.val_int);
6852       break;
6853     case dw_val_class_unsigned_const:
6854       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6855       break;
6856     case dw_val_class_long_long:
6857       CHECKSUM (at->dw_attr_val.v.val_long_long);
6858       break;
6859     case dw_val_class_vec:
6860       CHECKSUM (at->dw_attr_val.v.val_vec);
6861       break;
6862     case dw_val_class_flag:
6863       CHECKSUM (at->dw_attr_val.v.val_flag);
6864       break;
6865     case dw_val_class_str:
6866       CHECKSUM_STRING (AT_string (at));
6867       break;
6868
6869     case dw_val_class_addr:
6870       r = AT_addr (at);
6871       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6872       CHECKSUM_STRING (XSTR (r, 0));
6873       break;
6874
6875     case dw_val_class_offset:
6876       CHECKSUM (at->dw_attr_val.v.val_offset);
6877       break;
6878
6879     case dw_val_class_loc:
6880       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6881         loc_checksum (loc, ctx);
6882       break;
6883
6884     case dw_val_class_die_ref:
6885       die_checksum (AT_ref (at), ctx, mark);
6886       break;
6887
6888     case dw_val_class_fde_ref:
6889     case dw_val_class_lbl_id:
6890     case dw_val_class_lineptr:
6891     case dw_val_class_macptr:
6892       break;
6893
6894     case dw_val_class_file:
6895       CHECKSUM_STRING (AT_file (at)->filename);
6896       break;
6897
6898     default:
6899       break;
6900     }
6901 }
6902
6903 /* Calculate the checksum of a DIE.  */
6904
6905 static void
6906 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6907 {
6908   dw_die_ref c;
6909   dw_attr_ref a;
6910   unsigned ix;
6911
6912   /* To avoid infinite recursion.  */
6913   if (die->die_mark)
6914     {
6915       CHECKSUM (die->die_mark);
6916       return;
6917     }
6918   die->die_mark = ++(*mark);
6919
6920   CHECKSUM (die->die_tag);
6921
6922   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6923     attr_checksum (a, ctx, mark);
6924
6925   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6926 }
6927
6928 #undef CHECKSUM
6929 #undef CHECKSUM_STRING
6930
6931 /* Do the location expressions look same?  */
6932 static inline int
6933 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6934 {
6935   return loc1->dw_loc_opc == loc2->dw_loc_opc
6936          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6937          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6938 }
6939
6940 /* Do the values look the same?  */
6941 static int
6942 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6943 {
6944   dw_loc_descr_ref loc1, loc2;
6945   rtx r1, r2;
6946
6947   if (v1->val_class != v2->val_class)
6948     return 0;
6949
6950   switch (v1->val_class)
6951     {
6952     case dw_val_class_const:
6953       return v1->v.val_int == v2->v.val_int;
6954     case dw_val_class_unsigned_const:
6955       return v1->v.val_unsigned == v2->v.val_unsigned;
6956     case dw_val_class_long_long:
6957       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6958              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6959     case dw_val_class_vec:
6960       if (v1->v.val_vec.length != v2->v.val_vec.length
6961           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6962         return 0;
6963       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6964                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6965         return 0;
6966       return 1;
6967     case dw_val_class_flag:
6968       return v1->v.val_flag == v2->v.val_flag;
6969     case dw_val_class_str:
6970       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6971
6972     case dw_val_class_addr:
6973       r1 = v1->v.val_addr;
6974       r2 = v2->v.val_addr;
6975       if (GET_CODE (r1) != GET_CODE (r2))
6976         return 0;
6977       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6978       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6979
6980     case dw_val_class_offset:
6981       return v1->v.val_offset == v2->v.val_offset;
6982
6983     case dw_val_class_loc:
6984       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6985            loc1 && loc2;
6986            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6987         if (!same_loc_p (loc1, loc2, mark))
6988           return 0;
6989       return !loc1 && !loc2;
6990
6991     case dw_val_class_die_ref:
6992       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6993
6994     case dw_val_class_fde_ref:
6995     case dw_val_class_lbl_id:
6996     case dw_val_class_lineptr:
6997     case dw_val_class_macptr:
6998       return 1;
6999
7000     case dw_val_class_file:
7001       return v1->v.val_file == v2->v.val_file;
7002
7003     default:
7004       return 1;
7005     }
7006 }
7007
7008 /* Do the attributes look the same?  */
7009
7010 static int
7011 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7012 {
7013   if (at1->dw_attr != at2->dw_attr)
7014     return 0;
7015
7016   /* We don't care that this was compiled with a different compiler
7017      snapshot; if the output is the same, that's what matters. */
7018   if (at1->dw_attr == DW_AT_producer)
7019     return 1;
7020
7021   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7022 }
7023
7024 /* Do the dies look the same?  */
7025
7026 static int
7027 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7028 {
7029   dw_die_ref c1, c2;
7030   dw_attr_ref a1;
7031   unsigned ix;
7032
7033   /* To avoid infinite recursion.  */
7034   if (die1->die_mark)
7035     return die1->die_mark == die2->die_mark;
7036   die1->die_mark = die2->die_mark = ++(*mark);
7037
7038   if (die1->die_tag != die2->die_tag)
7039     return 0;
7040
7041   if (VEC_length (dw_attr_node, die1->die_attr)
7042       != VEC_length (dw_attr_node, die2->die_attr))
7043     return 0;
7044
7045   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7046     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7047       return 0;
7048
7049   c1 = die1->die_child;
7050   c2 = die2->die_child;
7051   if (! c1)
7052     {
7053       if (c2)
7054         return 0;
7055     }
7056   else
7057     for (;;)
7058       {
7059         if (!same_die_p (c1, c2, mark))
7060           return 0;
7061         c1 = c1->die_sib;
7062         c2 = c2->die_sib;
7063         if (c1 == die1->die_child)
7064           {
7065             if (c2 == die2->die_child)
7066               break;
7067             else
7068               return 0;
7069           }
7070     }
7071
7072   return 1;
7073 }
7074
7075 /* Do the dies look the same?  Wrapper around same_die_p.  */
7076
7077 static int
7078 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7079 {
7080   int mark = 0;
7081   int ret = same_die_p (die1, die2, &mark);
7082
7083   unmark_all_dies (die1);
7084   unmark_all_dies (die2);
7085
7086   return ret;
7087 }
7088
7089 /* The prefix to attach to symbols on DIEs in the current comdat debug
7090    info section.  */
7091 static char *comdat_symbol_id;
7092
7093 /* The index of the current symbol within the current comdat CU.  */
7094 static unsigned int comdat_symbol_number;
7095
7096 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7097    children, and set comdat_symbol_id accordingly.  */
7098
7099 static void
7100 compute_section_prefix (dw_die_ref unit_die)
7101 {
7102   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7103   const char *base = die_name ? lbasename (die_name) : "anonymous";
7104   char *name = XALLOCAVEC (char, strlen (base) + 64);
7105   char *p;
7106   int i, mark;
7107   unsigned char checksum[16];
7108   struct md5_ctx ctx;
7109
7110   /* Compute the checksum of the DIE, then append part of it as hex digits to
7111      the name filename of the unit.  */
7112
7113   md5_init_ctx (&ctx);
7114   mark = 0;
7115   die_checksum (unit_die, &ctx, &mark);
7116   unmark_all_dies (unit_die);
7117   md5_finish_ctx (&ctx, checksum);
7118
7119   sprintf (name, "%s.", base);
7120   clean_symbol_name (name);
7121
7122   p = name + strlen (name);
7123   for (i = 0; i < 4; i++)
7124     {
7125       sprintf (p, "%.2x", checksum[i]);
7126       p += 2;
7127     }
7128
7129   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7130   comdat_symbol_number = 0;
7131 }
7132
7133 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7134
7135 static int
7136 is_type_die (dw_die_ref die)
7137 {
7138   switch (die->die_tag)
7139     {
7140     case DW_TAG_array_type:
7141     case DW_TAG_class_type:
7142     case DW_TAG_interface_type:
7143     case DW_TAG_enumeration_type:
7144     case DW_TAG_pointer_type:
7145     case DW_TAG_reference_type:
7146     case DW_TAG_string_type:
7147     case DW_TAG_structure_type:
7148     case DW_TAG_subroutine_type:
7149     case DW_TAG_union_type:
7150     case DW_TAG_ptr_to_member_type:
7151     case DW_TAG_set_type:
7152     case DW_TAG_subrange_type:
7153     case DW_TAG_base_type:
7154     case DW_TAG_const_type:
7155     case DW_TAG_file_type:
7156     case DW_TAG_packed_type:
7157     case DW_TAG_volatile_type:
7158     case DW_TAG_typedef:
7159       return 1;
7160     default:
7161       return 0;
7162     }
7163 }
7164
7165 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7166    Basically, we want to choose the bits that are likely to be shared between
7167    compilations (types) and leave out the bits that are specific to individual
7168    compilations (functions).  */
7169
7170 static int
7171 is_comdat_die (dw_die_ref c)
7172 {
7173   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7174      we do for stabs.  The advantage is a greater likelihood of sharing between
7175      objects that don't include headers in the same order (and therefore would
7176      put the base types in a different comdat).  jason 8/28/00 */
7177
7178   if (c->die_tag == DW_TAG_base_type)
7179     return 0;
7180
7181   if (c->die_tag == DW_TAG_pointer_type
7182       || c->die_tag == DW_TAG_reference_type
7183       || c->die_tag == DW_TAG_const_type
7184       || c->die_tag == DW_TAG_volatile_type)
7185     {
7186       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7187
7188       return t ? is_comdat_die (t) : 0;
7189     }
7190
7191   return is_type_die (c);
7192 }
7193
7194 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7195    compilation unit.  */
7196
7197 static int
7198 is_symbol_die (dw_die_ref c)
7199 {
7200   return (is_type_die (c)
7201           || (get_AT (c, DW_AT_declaration)
7202               && !get_AT (c, DW_AT_specification))
7203           || c->die_tag == DW_TAG_namespace);
7204 }
7205
7206 static char *
7207 gen_internal_sym (const char *prefix)
7208 {
7209   char buf[256];
7210
7211   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7212   return xstrdup (buf);
7213 }
7214
7215 /* Assign symbols to all worthy DIEs under DIE.  */
7216
7217 static void
7218 assign_symbol_names (dw_die_ref die)
7219 {
7220   dw_die_ref c;
7221
7222   if (is_symbol_die (die))
7223     {
7224       if (comdat_symbol_id)
7225         {
7226           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7227
7228           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7229                    comdat_symbol_id, comdat_symbol_number++);
7230           die->die_symbol = xstrdup (p);
7231         }
7232       else
7233         die->die_symbol = gen_internal_sym ("LDIE");
7234     }
7235
7236   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7237 }
7238
7239 struct cu_hash_table_entry
7240 {
7241   dw_die_ref cu;
7242   unsigned min_comdat_num, max_comdat_num;
7243   struct cu_hash_table_entry *next;
7244 };
7245
7246 /* Routines to manipulate hash table of CUs.  */
7247 static hashval_t
7248 htab_cu_hash (const void *of)
7249 {
7250   const struct cu_hash_table_entry *const entry =
7251     (const struct cu_hash_table_entry *) of;
7252
7253   return htab_hash_string (entry->cu->die_symbol);
7254 }
7255
7256 static int
7257 htab_cu_eq (const void *of1, const void *of2)
7258 {
7259   const struct cu_hash_table_entry *const entry1 =
7260     (const struct cu_hash_table_entry *) of1;
7261   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7262
7263   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7264 }
7265
7266 static void
7267 htab_cu_del (void *what)
7268 {
7269   struct cu_hash_table_entry *next,
7270     *entry = (struct cu_hash_table_entry *) what;
7271
7272   while (entry)
7273     {
7274       next = entry->next;
7275       free (entry);
7276       entry = next;
7277     }
7278 }
7279
7280 /* Check whether we have already seen this CU and set up SYM_NUM
7281    accordingly.  */
7282 static int
7283 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7284 {
7285   struct cu_hash_table_entry dummy;
7286   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7287
7288   dummy.max_comdat_num = 0;
7289
7290   slot = (struct cu_hash_table_entry **)
7291     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7292         INSERT);
7293   entry = *slot;
7294
7295   for (; entry; last = entry, entry = entry->next)
7296     {
7297       if (same_die_p_wrap (cu, entry->cu))
7298         break;
7299     }
7300
7301   if (entry)
7302     {
7303       *sym_num = entry->min_comdat_num;
7304       return 1;
7305     }
7306
7307   entry = XCNEW (struct cu_hash_table_entry);
7308   entry->cu = cu;
7309   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7310   entry->next = *slot;
7311   *slot = entry;
7312
7313   return 0;
7314 }
7315
7316 /* Record SYM_NUM to record of CU in HTABLE.  */
7317 static void
7318 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7319 {
7320   struct cu_hash_table_entry **slot, *entry;
7321
7322   slot = (struct cu_hash_table_entry **)
7323     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7324         NO_INSERT);
7325   entry = *slot;
7326
7327   entry->max_comdat_num = sym_num;
7328 }
7329
7330 /* Traverse the DIE (which is always comp_unit_die), and set up
7331    additional compilation units for each of the include files we see
7332    bracketed by BINCL/EINCL.  */
7333
7334 static void
7335 break_out_includes (dw_die_ref die)
7336 {
7337   dw_die_ref c;
7338   dw_die_ref unit = NULL;
7339   limbo_die_node *node, **pnode;
7340   htab_t cu_hash_table;
7341
7342   c = die->die_child;
7343   if (c) do {
7344     dw_die_ref prev = c;
7345     c = c->die_sib;
7346     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7347            || (unit && is_comdat_die (c)))
7348       {
7349         dw_die_ref next = c->die_sib;
7350
7351         /* This DIE is for a secondary CU; remove it from the main one.  */
7352         remove_child_with_prev (c, prev);
7353
7354         if (c->die_tag == DW_TAG_GNU_BINCL)
7355           unit = push_new_compile_unit (unit, c);
7356         else if (c->die_tag == DW_TAG_GNU_EINCL)
7357           unit = pop_compile_unit (unit);
7358         else
7359           add_child_die (unit, c);
7360         c = next;
7361         if (c == die->die_child)
7362           break;
7363       }
7364   } while (c != die->die_child);
7365
7366 #if 0
7367   /* We can only use this in debugging, since the frontend doesn't check
7368      to make sure that we leave every include file we enter.  */
7369   gcc_assert (!unit);
7370 #endif
7371
7372   assign_symbol_names (die);
7373   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7374   for (node = limbo_die_list, pnode = &limbo_die_list;
7375        node;
7376        node = node->next)
7377     {
7378       int is_dupl;
7379
7380       compute_section_prefix (node->die);
7381       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7382                         &comdat_symbol_number);
7383       assign_symbol_names (node->die);
7384       if (is_dupl)
7385         *pnode = node->next;
7386       else
7387         {
7388           pnode = &node->next;
7389           record_comdat_symbol_number (node->die, cu_hash_table,
7390                 comdat_symbol_number);
7391         }
7392     }
7393   htab_delete (cu_hash_table);
7394 }
7395
7396 /* Traverse the DIE and add a sibling attribute if it may have the
7397    effect of speeding up access to siblings.  To save some space,
7398    avoid generating sibling attributes for DIE's without children.  */
7399
7400 static void
7401 add_sibling_attributes (dw_die_ref die)
7402 {
7403   dw_die_ref c;
7404
7405   if (! die->die_child)
7406     return;
7407
7408   if (die->die_parent && die != die->die_parent->die_child)
7409     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7410
7411   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7412 }
7413
7414 /* Output all location lists for the DIE and its children.  */
7415
7416 static void
7417 output_location_lists (dw_die_ref die)
7418 {
7419   dw_die_ref c;
7420   dw_attr_ref a;
7421   unsigned ix;
7422
7423   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7424     if (AT_class (a) == dw_val_class_loc_list)
7425       output_loc_list (AT_loc_list (a));
7426
7427   FOR_EACH_CHILD (die, c, output_location_lists (c));
7428 }
7429
7430 /* The format of each DIE (and its attribute value pairs) is encoded in an
7431    abbreviation table.  This routine builds the abbreviation table and assigns
7432    a unique abbreviation id for each abbreviation entry.  The children of each
7433    die are visited recursively.  */
7434
7435 static void
7436 build_abbrev_table (dw_die_ref die)
7437 {
7438   unsigned long abbrev_id;
7439   unsigned int n_alloc;
7440   dw_die_ref c;
7441   dw_attr_ref a;
7442   unsigned ix;
7443
7444   /* Scan the DIE references, and mark as external any that refer to
7445      DIEs from other CUs (i.e. those which are not marked).  */
7446   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7447     if (AT_class (a) == dw_val_class_die_ref
7448         && AT_ref (a)->die_mark == 0)
7449       {
7450         gcc_assert (AT_ref (a)->die_symbol);
7451
7452         set_AT_ref_external (a, 1);
7453       }
7454
7455   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7456     {
7457       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7458       dw_attr_ref die_a, abbrev_a;
7459       unsigned ix;
7460       bool ok = true;
7461
7462       if (abbrev->die_tag != die->die_tag)
7463         continue;
7464       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7465         continue;
7466
7467       if (VEC_length (dw_attr_node, abbrev->die_attr)
7468           != VEC_length (dw_attr_node, die->die_attr))
7469         continue;
7470
7471       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7472         {
7473           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7474           if ((abbrev_a->dw_attr != die_a->dw_attr)
7475               || (value_format (abbrev_a) != value_format (die_a)))
7476             {
7477               ok = false;
7478               break;
7479             }
7480         }
7481       if (ok)
7482         break;
7483     }
7484
7485   if (abbrev_id >= abbrev_die_table_in_use)
7486     {
7487       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7488         {
7489           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7490           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7491                                             n_alloc);
7492
7493           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7494                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7495           abbrev_die_table_allocated = n_alloc;
7496         }
7497
7498       ++abbrev_die_table_in_use;
7499       abbrev_die_table[abbrev_id] = die;
7500     }
7501
7502   die->die_abbrev = abbrev_id;
7503   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7504 }
7505 \f
7506 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7507
7508 static int
7509 constant_size (long unsigned int value)
7510 {
7511   int log;
7512
7513   if (value == 0)
7514     log = 0;
7515   else
7516     log = floor_log2 (value);
7517
7518   log = log / 8;
7519   log = 1 << (floor_log2 (log) + 1);
7520
7521   return log;
7522 }
7523
7524 /* Return the size of a DIE as it is represented in the
7525    .debug_info section.  */
7526
7527 static unsigned long
7528 size_of_die (dw_die_ref die)
7529 {
7530   unsigned long size = 0;
7531   dw_attr_ref a;
7532   unsigned ix;
7533
7534   size += size_of_uleb128 (die->die_abbrev);
7535   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7536     {
7537       switch (AT_class (a))
7538         {
7539         case dw_val_class_addr:
7540           size += DWARF2_ADDR_SIZE;
7541           break;
7542         case dw_val_class_offset:
7543           size += DWARF_OFFSET_SIZE;
7544           break;
7545         case dw_val_class_loc:
7546           {
7547             unsigned long lsize = size_of_locs (AT_loc (a));
7548
7549             /* Block length.  */
7550             size += constant_size (lsize);
7551             size += lsize;
7552           }
7553           break;
7554         case dw_val_class_loc_list:
7555           size += DWARF_OFFSET_SIZE;
7556           break;
7557         case dw_val_class_range_list:
7558           size += DWARF_OFFSET_SIZE;
7559           break;
7560         case dw_val_class_const:
7561           size += size_of_sleb128 (AT_int (a));
7562           break;
7563         case dw_val_class_unsigned_const:
7564           size += constant_size (AT_unsigned (a));
7565           break;
7566         case dw_val_class_long_long:
7567           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7568           break;
7569         case dw_val_class_vec:
7570           size += 1 + (a->dw_attr_val.v.val_vec.length
7571                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
7572           break;
7573         case dw_val_class_flag:
7574           size += 1;
7575           break;
7576         case dw_val_class_die_ref:
7577           if (AT_ref_external (a))
7578             size += DWARF2_ADDR_SIZE;
7579           else
7580             size += DWARF_OFFSET_SIZE;
7581           break;
7582         case dw_val_class_fde_ref:
7583           size += DWARF_OFFSET_SIZE;
7584           break;
7585         case dw_val_class_lbl_id:
7586           size += DWARF2_ADDR_SIZE;
7587           break;
7588         case dw_val_class_lineptr:
7589         case dw_val_class_macptr:
7590           size += DWARF_OFFSET_SIZE;
7591           break;
7592         case dw_val_class_str:
7593           if (AT_string_form (a) == DW_FORM_strp)
7594             size += DWARF_OFFSET_SIZE;
7595           else
7596             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7597           break;
7598         case dw_val_class_file:
7599           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7600           break;
7601         default:
7602           gcc_unreachable ();
7603         }
7604     }
7605
7606   return size;
7607 }
7608
7609 /* Size the debugging information associated with a given DIE.  Visits the
7610    DIE's children recursively.  Updates the global variable next_die_offset, on
7611    each time through.  Uses the current value of next_die_offset to update the
7612    die_offset field in each DIE.  */
7613
7614 static void
7615 calc_die_sizes (dw_die_ref die)
7616 {
7617   dw_die_ref c;
7618
7619   die->die_offset = next_die_offset;
7620   next_die_offset += size_of_die (die);
7621
7622   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7623
7624   if (die->die_child != NULL)
7625     /* Count the null byte used to terminate sibling lists.  */
7626     next_die_offset += 1;
7627 }
7628
7629 /* Set the marks for a die and its children.  We do this so
7630    that we know whether or not a reference needs to use FORM_ref_addr; only
7631    DIEs in the same CU will be marked.  We used to clear out the offset
7632    and use that as the flag, but ran into ordering problems.  */
7633
7634 static void
7635 mark_dies (dw_die_ref die)
7636 {
7637   dw_die_ref c;
7638
7639   gcc_assert (!die->die_mark);
7640
7641   die->die_mark = 1;
7642   FOR_EACH_CHILD (die, c, mark_dies (c));
7643 }
7644
7645 /* Clear the marks for a die and its children.  */
7646
7647 static void
7648 unmark_dies (dw_die_ref die)
7649 {
7650   dw_die_ref c;
7651
7652   gcc_assert (die->die_mark);
7653
7654   die->die_mark = 0;
7655   FOR_EACH_CHILD (die, c, unmark_dies (c));
7656 }
7657
7658 /* Clear the marks for a die, its children and referred dies.  */
7659
7660 static void
7661 unmark_all_dies (dw_die_ref die)
7662 {
7663   dw_die_ref c;
7664   dw_attr_ref a;
7665   unsigned ix;
7666
7667   if (!die->die_mark)
7668     return;
7669   die->die_mark = 0;
7670
7671   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7672
7673   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7674     if (AT_class (a) == dw_val_class_die_ref)
7675       unmark_all_dies (AT_ref (a));
7676 }
7677
7678 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7679    generated for the compilation unit.  */
7680
7681 static unsigned long
7682 size_of_pubnames (VEC (pubname_entry, gc) * names)
7683 {
7684   unsigned long size;
7685   unsigned i;
7686   pubname_ref p;
7687
7688   size = DWARF_PUBNAMES_HEADER_SIZE;
7689   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7690     if (names != pubtype_table
7691         || p->die->die_offset != 0
7692         || !flag_eliminate_unused_debug_types)
7693       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7694
7695   size += DWARF_OFFSET_SIZE;
7696   return size;
7697 }
7698
7699 /* Return the size of the information in the .debug_aranges section.  */
7700
7701 static unsigned long
7702 size_of_aranges (void)
7703 {
7704   unsigned long size;
7705
7706   size = DWARF_ARANGES_HEADER_SIZE;
7707
7708   /* Count the address/length pair for this compilation unit.  */
7709   if (text_section_used)
7710     size += 2 * DWARF2_ADDR_SIZE;
7711   if (cold_text_section_used)
7712     size += 2 * DWARF2_ADDR_SIZE;
7713   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7714
7715   /* Count the two zero words used to terminated the address range table.  */
7716   size += 2 * DWARF2_ADDR_SIZE;
7717   return size;
7718 }
7719 \f
7720 /* Select the encoding of an attribute value.  */
7721
7722 static enum dwarf_form
7723 value_format (dw_attr_ref a)
7724 {
7725   switch (a->dw_attr_val.val_class)
7726     {
7727     case dw_val_class_addr:
7728       return DW_FORM_addr;
7729     case dw_val_class_range_list:
7730     case dw_val_class_offset:
7731     case dw_val_class_loc_list:
7732       switch (DWARF_OFFSET_SIZE)
7733         {
7734         case 4:
7735           return DW_FORM_data4;
7736         case 8:
7737           return DW_FORM_data8;
7738         default:
7739           gcc_unreachable ();
7740         }
7741     case dw_val_class_loc:
7742       switch (constant_size (size_of_locs (AT_loc (a))))
7743         {
7744         case 1:
7745           return DW_FORM_block1;
7746         case 2:
7747           return DW_FORM_block2;
7748         default:
7749           gcc_unreachable ();
7750         }
7751     case dw_val_class_const:
7752       return DW_FORM_sdata;
7753     case dw_val_class_unsigned_const:
7754       switch (constant_size (AT_unsigned (a)))
7755         {
7756         case 1:
7757           return DW_FORM_data1;
7758         case 2:
7759           return DW_FORM_data2;
7760         case 4:
7761           return DW_FORM_data4;
7762         case 8:
7763           return DW_FORM_data8;
7764         default:
7765           gcc_unreachable ();
7766         }
7767     case dw_val_class_long_long:
7768       return DW_FORM_block1;
7769     case dw_val_class_vec:
7770       return DW_FORM_block1;
7771     case dw_val_class_flag:
7772       return DW_FORM_flag;
7773     case dw_val_class_die_ref:
7774       if (AT_ref_external (a))
7775         return DW_FORM_ref_addr;
7776       else
7777         return DW_FORM_ref;
7778     case dw_val_class_fde_ref:
7779       return DW_FORM_data;
7780     case dw_val_class_lbl_id:
7781       return DW_FORM_addr;
7782     case dw_val_class_lineptr:
7783     case dw_val_class_macptr:
7784       return DW_FORM_data;
7785     case dw_val_class_str:
7786       return AT_string_form (a);
7787     case dw_val_class_file:
7788       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7789         {
7790         case 1:
7791           return DW_FORM_data1;
7792         case 2:
7793           return DW_FORM_data2;
7794         case 4:
7795           return DW_FORM_data4;
7796         default:
7797           gcc_unreachable ();
7798         }
7799
7800     default:
7801       gcc_unreachable ();
7802     }
7803 }
7804
7805 /* Output the encoding of an attribute value.  */
7806
7807 static void
7808 output_value_format (dw_attr_ref a)
7809 {
7810   enum dwarf_form form = value_format (a);
7811
7812   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7813 }
7814
7815 /* Output the .debug_abbrev section which defines the DIE abbreviation
7816    table.  */
7817
7818 static void
7819 output_abbrev_section (void)
7820 {
7821   unsigned long abbrev_id;
7822
7823   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7824     {
7825       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7826       unsigned ix;
7827       dw_attr_ref a_attr;
7828
7829       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7830       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7831                                    dwarf_tag_name (abbrev->die_tag));
7832
7833       if (abbrev->die_child != NULL)
7834         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7835       else
7836         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7837
7838       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7839            ix++)
7840         {
7841           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7842                                        dwarf_attr_name (a_attr->dw_attr));
7843           output_value_format (a_attr);
7844         }
7845
7846       dw2_asm_output_data (1, 0, NULL);
7847       dw2_asm_output_data (1, 0, NULL);
7848     }
7849
7850   /* Terminate the table.  */
7851   dw2_asm_output_data (1, 0, NULL);
7852 }
7853
7854 /* Output a symbol we can use to refer to this DIE from another CU.  */
7855
7856 static inline void
7857 output_die_symbol (dw_die_ref die)
7858 {
7859   char *sym = die->die_symbol;
7860
7861   if (sym == 0)
7862     return;
7863
7864   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7865     /* We make these global, not weak; if the target doesn't support
7866        .linkonce, it doesn't support combining the sections, so debugging
7867        will break.  */
7868     targetm.asm_out.globalize_label (asm_out_file, sym);
7869
7870   ASM_OUTPUT_LABEL (asm_out_file, sym);
7871 }
7872
7873 /* Return a new location list, given the begin and end range, and the
7874    expression. gensym tells us whether to generate a new internal symbol for
7875    this location list node, which is done for the head of the list only.  */
7876
7877 static inline dw_loc_list_ref
7878 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7879               const char *section, unsigned int gensym)
7880 {
7881   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7882
7883   retlist->begin = begin;
7884   retlist->end = end;
7885   retlist->expr = expr;
7886   retlist->section = section;
7887   if (gensym)
7888     retlist->ll_symbol = gen_internal_sym ("LLST");
7889
7890   return retlist;
7891 }
7892
7893 /* Add a location description expression to a location list.  */
7894
7895 static inline void
7896 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7897                            const char *begin, const char *end,
7898                            const char *section)
7899 {
7900   dw_loc_list_ref *d;
7901
7902   /* Find the end of the chain.  */
7903   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7904     ;
7905
7906   /* Add a new location list node to the list.  */
7907   *d = new_loc_list (descr, begin, end, section, 0);
7908 }
7909
7910 /* Output the location list given to us.  */
7911
7912 static void
7913 output_loc_list (dw_loc_list_ref list_head)
7914 {
7915   dw_loc_list_ref curr = list_head;
7916
7917   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7918
7919   /* Walk the location list, and output each range + expression.  */
7920   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7921     {
7922       unsigned long size;
7923       /* Don't output an entry that starts and ends at the same address.  */
7924       if (strcmp (curr->begin, curr->end) == 0)
7925         continue;
7926       if (!have_multiple_function_sections)
7927         {
7928           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7929                                 "Location list begin address (%s)",
7930                                 list_head->ll_symbol);
7931           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7932                                 "Location list end address (%s)",
7933                                 list_head->ll_symbol);
7934         }
7935       else
7936         {
7937           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7938                                "Location list begin address (%s)",
7939                                list_head->ll_symbol);
7940           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7941                                "Location list end address (%s)",
7942                                list_head->ll_symbol);
7943         }
7944       size = size_of_locs (curr->expr);
7945
7946       /* Output the block length for this list of location operations.  */
7947       gcc_assert (size <= 0xffff);
7948       dw2_asm_output_data (2, size, "%s", "Location expression size");
7949
7950       output_loc_sequence (curr->expr);
7951     }
7952
7953   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7954                        "Location list terminator begin (%s)",
7955                        list_head->ll_symbol);
7956   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7957                        "Location list terminator end (%s)",
7958                        list_head->ll_symbol);
7959 }
7960
7961 /* Output the DIE and its attributes.  Called recursively to generate
7962    the definitions of each child DIE.  */
7963
7964 static void
7965 output_die (dw_die_ref die)
7966 {
7967   dw_attr_ref a;
7968   dw_die_ref c;
7969   unsigned long size;
7970   unsigned ix;
7971
7972   /* If someone in another CU might refer to us, set up a symbol for
7973      them to point to.  */
7974   if (die->die_symbol)
7975     output_die_symbol (die);
7976
7977   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7978                                (unsigned long)die->die_offset,
7979                                dwarf_tag_name (die->die_tag));
7980
7981   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7982     {
7983       const char *name = dwarf_attr_name (a->dw_attr);
7984
7985       switch (AT_class (a))
7986         {
7987         case dw_val_class_addr:
7988           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7989           break;
7990
7991         case dw_val_class_offset:
7992           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7993                                "%s", name);
7994           break;
7995
7996         case dw_val_class_range_list:
7997           {
7998             char *p = strchr (ranges_section_label, '\0');
7999
8000             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8001                      a->dw_attr_val.v.val_offset);
8002             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8003                                    debug_ranges_section, "%s", name);
8004             *p = '\0';
8005           }
8006           break;
8007
8008         case dw_val_class_loc:
8009           size = size_of_locs (AT_loc (a));
8010
8011           /* Output the block length for this list of location operations.  */
8012           dw2_asm_output_data (constant_size (size), size, "%s", name);
8013
8014           output_loc_sequence (AT_loc (a));
8015           break;
8016
8017         case dw_val_class_const:
8018           /* ??? It would be slightly more efficient to use a scheme like is
8019              used for unsigned constants below, but gdb 4.x does not sign
8020              extend.  Gdb 5.x does sign extend.  */
8021           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8022           break;
8023
8024         case dw_val_class_unsigned_const:
8025           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8026                                AT_unsigned (a), "%s", name);
8027           break;
8028
8029         case dw_val_class_long_long:
8030           {
8031             unsigned HOST_WIDE_INT first, second;
8032
8033             dw2_asm_output_data (1,
8034                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8035                                  "%s", name);
8036
8037             if (WORDS_BIG_ENDIAN)
8038               {
8039                 first = a->dw_attr_val.v.val_long_long.hi;
8040                 second = a->dw_attr_val.v.val_long_long.low;
8041               }
8042             else
8043               {
8044                 first = a->dw_attr_val.v.val_long_long.low;
8045                 second = a->dw_attr_val.v.val_long_long.hi;
8046               }
8047
8048             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8049                                  first, "long long constant");
8050             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8051                                  second, NULL);
8052           }
8053           break;
8054
8055         case dw_val_class_vec:
8056           {
8057             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8058             unsigned int len = a->dw_attr_val.v.val_vec.length;
8059             unsigned int i;
8060             unsigned char *p;
8061
8062             dw2_asm_output_data (1, len * elt_size, "%s", name);
8063             if (elt_size > sizeof (HOST_WIDE_INT))
8064               {
8065                 elt_size /= 2;
8066                 len *= 2;
8067               }
8068             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8069                  i < len;
8070                  i++, p += elt_size)
8071               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8072                                    "fp or vector constant word %u", i);
8073             break;
8074           }
8075
8076         case dw_val_class_flag:
8077           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8078           break;
8079
8080         case dw_val_class_loc_list:
8081           {
8082             char *sym = AT_loc_list (a)->ll_symbol;
8083
8084             gcc_assert (sym);
8085             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8086                                    "%s", name);
8087           }
8088           break;
8089
8090         case dw_val_class_die_ref:
8091           if (AT_ref_external (a))
8092             {
8093               char *sym = AT_ref (a)->die_symbol;
8094
8095               gcc_assert (sym);
8096               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8097                                      "%s", name);
8098             }
8099           else
8100             {
8101               gcc_assert (AT_ref (a)->die_offset);
8102               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8103                                    "%s", name);
8104             }
8105           break;
8106
8107         case dw_val_class_fde_ref:
8108           {
8109             char l1[20];
8110
8111             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8112                                          a->dw_attr_val.v.val_fde_index * 2);
8113             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8114                                    "%s", name);
8115           }
8116           break;
8117
8118         case dw_val_class_lbl_id:
8119           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8120           break;
8121
8122         case dw_val_class_lineptr:
8123           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8124                                  debug_line_section, "%s", name);
8125           break;
8126
8127         case dw_val_class_macptr:
8128           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8129                                  debug_macinfo_section, "%s", name);
8130           break;
8131
8132         case dw_val_class_str:
8133           if (AT_string_form (a) == DW_FORM_strp)
8134             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8135                                    a->dw_attr_val.v.val_str->label,
8136                                    debug_str_section,
8137                                    "%s: \"%s\"", name, AT_string (a));
8138           else
8139             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8140           break;
8141
8142         case dw_val_class_file:
8143           {
8144             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8145
8146             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8147                                  a->dw_attr_val.v.val_file->filename);
8148             break;
8149           }
8150
8151         default:
8152           gcc_unreachable ();
8153         }
8154     }
8155
8156   FOR_EACH_CHILD (die, c, output_die (c));
8157
8158   /* Add null byte to terminate sibling list.  */
8159   if (die->die_child != NULL)
8160     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8161                          (unsigned long) die->die_offset);
8162 }
8163
8164 /* Output the compilation unit that appears at the beginning of the
8165    .debug_info section, and precedes the DIE descriptions.  */
8166
8167 static void
8168 output_compilation_unit_header (void)
8169 {
8170   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8171     dw2_asm_output_data (4, 0xffffffff,
8172       "Initial length escape value indicating 64-bit DWARF extension");
8173   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8174                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8175                        "Length of Compilation Unit Info");
8176   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8177   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8178                          debug_abbrev_section,
8179                          "Offset Into Abbrev. Section");
8180   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8181 }
8182
8183 /* Output the compilation unit DIE and its children.  */
8184
8185 static void
8186 output_comp_unit (dw_die_ref die, int output_if_empty)
8187 {
8188   const char *secname;
8189   char *oldsym, *tmp;
8190
8191   /* Unless we are outputting main CU, we may throw away empty ones.  */
8192   if (!output_if_empty && die->die_child == NULL)
8193     return;
8194
8195   /* Even if there are no children of this DIE, we must output the information
8196      about the compilation unit.  Otherwise, on an empty translation unit, we
8197      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8198      will then complain when examining the file.  First mark all the DIEs in
8199      this CU so we know which get local refs.  */
8200   mark_dies (die);
8201
8202   build_abbrev_table (die);
8203
8204   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8205   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8206   calc_die_sizes (die);
8207
8208   oldsym = die->die_symbol;
8209   if (oldsym)
8210     {
8211       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8212
8213       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8214       secname = tmp;
8215       die->die_symbol = NULL;
8216       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8217     }
8218   else
8219     switch_to_section (debug_info_section);
8220
8221   /* Output debugging information.  */
8222   output_compilation_unit_header ();
8223   output_die (die);
8224
8225   /* Leave the marks on the main CU, so we can check them in
8226      output_pubnames.  */
8227   if (oldsym)
8228     {
8229       unmark_dies (die);
8230       die->die_symbol = oldsym;
8231     }
8232 }
8233
8234 /* Return the DWARF2/3 pubname associated with a decl.  */
8235
8236 static const char *
8237 dwarf2_name (tree decl, int scope)
8238 {
8239   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8240 }
8241
8242 /* Add a new entry to .debug_pubnames if appropriate.  */
8243
8244 static void
8245 add_pubname_string (const char *str, dw_die_ref die)
8246 {
8247   pubname_entry e;
8248
8249   e.die = die;
8250   e.name = xstrdup (str);
8251   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8252 }
8253
8254 static void
8255 add_pubname (tree decl, dw_die_ref die)
8256 {
8257
8258   if (TREE_PUBLIC (decl))
8259     add_pubname_string (dwarf2_name (decl, 1), die);
8260 }
8261
8262 /* Add a new entry to .debug_pubtypes if appropriate.  */
8263
8264 static void
8265 add_pubtype (tree decl, dw_die_ref die)
8266 {
8267   pubname_entry e;
8268
8269   e.name = NULL;
8270   if ((TREE_PUBLIC (decl)
8271        || die->die_parent == comp_unit_die)
8272       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8273     {
8274       e.die = die;
8275       if (TYPE_P (decl))
8276         {
8277           if (TYPE_NAME (decl))
8278             {
8279               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8280                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8281               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8282                        && DECL_NAME (TYPE_NAME (decl)))
8283                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8284               else
8285                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8286             }
8287         }
8288       else
8289         e.name = xstrdup (dwarf2_name (decl, 1));
8290
8291       /* If we don't have a name for the type, there's no point in adding
8292          it to the table.  */
8293       if (e.name && e.name[0] != '\0')
8294         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8295     }
8296 }
8297
8298 /* Output the public names table used to speed up access to externally
8299    visible names; or the public types table used to find type definitions.  */
8300
8301 static void
8302 output_pubnames (VEC (pubname_entry, gc) * names)
8303 {
8304   unsigned i;
8305   unsigned long pubnames_length = size_of_pubnames (names);
8306   pubname_ref pub;
8307
8308   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8309     dw2_asm_output_data (4, 0xffffffff,
8310       "Initial length escape value indicating 64-bit DWARF extension");
8311   if (names == pubname_table)
8312     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8313                          "Length of Public Names Info");
8314   else
8315     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8316                          "Length of Public Type Names Info");
8317   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8318   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8319                          debug_info_section,
8320                          "Offset of Compilation Unit Info");
8321   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8322                        "Compilation Unit Length");
8323
8324   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8325     {
8326       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8327       if (names == pubname_table)
8328         gcc_assert (pub->die->die_mark);
8329
8330       if (names != pubtype_table
8331           || pub->die->die_offset != 0
8332           || !flag_eliminate_unused_debug_types)
8333         {
8334           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8335                                "DIE offset");
8336
8337           dw2_asm_output_nstring (pub->name, -1, "external name");
8338         }
8339     }
8340
8341   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8342 }
8343
8344 /* Add a new entry to .debug_aranges if appropriate.  */
8345
8346 static void
8347 add_arange (tree decl, dw_die_ref die)
8348 {
8349   if (! DECL_SECTION_NAME (decl))
8350     return;
8351
8352   if (arange_table_in_use == arange_table_allocated)
8353     {
8354       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8355       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8356                                     arange_table_allocated);
8357       memset (arange_table + arange_table_in_use, 0,
8358               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8359     }
8360
8361   arange_table[arange_table_in_use++] = die;
8362 }
8363
8364 /* Output the information that goes into the .debug_aranges table.
8365    Namely, define the beginning and ending address range of the
8366    text section generated for this compilation unit.  */
8367
8368 static void
8369 output_aranges (void)
8370 {
8371   unsigned i;
8372   unsigned long aranges_length = size_of_aranges ();
8373
8374   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8375     dw2_asm_output_data (4, 0xffffffff,
8376       "Initial length escape value indicating 64-bit DWARF extension");
8377   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8378                        "Length of Address Ranges Info");
8379   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8380   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8381                          debug_info_section,
8382                          "Offset of Compilation Unit Info");
8383   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8384   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8385
8386   /* We need to align to twice the pointer size here.  */
8387   if (DWARF_ARANGES_PAD_SIZE)
8388     {
8389       /* Pad using a 2 byte words so that padding is correct for any
8390          pointer size.  */
8391       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8392                            2 * DWARF2_ADDR_SIZE);
8393       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8394         dw2_asm_output_data (2, 0, NULL);
8395     }
8396
8397   /* It is necessary not to output these entries if the sections were
8398      not used; if the sections were not used, the length will be 0 and
8399      the address may end up as 0 if the section is discarded by ld
8400      --gc-sections, leaving an invalid (0, 0) entry that can be
8401      confused with the terminator.  */
8402   if (text_section_used)
8403     {
8404       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8405       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8406                             text_section_label, "Length");
8407     }
8408   if (cold_text_section_used)
8409     {
8410       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8411                            "Address");
8412       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8413                             cold_text_section_label, "Length");
8414     }
8415
8416   for (i = 0; i < arange_table_in_use; i++)
8417     {
8418       dw_die_ref die = arange_table[i];
8419
8420       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8421       gcc_assert (die->die_mark);
8422
8423       if (die->die_tag == DW_TAG_subprogram)
8424         {
8425           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8426                                "Address");
8427           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8428                                 get_AT_low_pc (die), "Length");
8429         }
8430       else
8431         {
8432           /* A static variable; extract the symbol from DW_AT_location.
8433              Note that this code isn't currently hit, as we only emit
8434              aranges for functions (jason 9/23/99).  */
8435           dw_attr_ref a = get_AT (die, DW_AT_location);
8436           dw_loc_descr_ref loc;
8437
8438           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8439
8440           loc = AT_loc (a);
8441           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8442
8443           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8444                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8445           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8446                                get_AT_unsigned (die, DW_AT_byte_size),
8447                                "Length");
8448         }
8449     }
8450
8451   /* Output the terminator words.  */
8452   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8453   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8454 }
8455
8456 /* Add a new entry to .debug_ranges.  Return the offset at which it
8457    was placed.  */
8458
8459 static unsigned int
8460 add_ranges_num (int num)
8461 {
8462   unsigned int in_use = ranges_table_in_use;
8463
8464   if (in_use == ranges_table_allocated)
8465     {
8466       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8467       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8468                                     ranges_table_allocated);
8469       memset (ranges_table + ranges_table_in_use, 0,
8470               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8471     }
8472
8473   ranges_table[in_use].num = num;
8474   ranges_table_in_use = in_use + 1;
8475
8476   return in_use * 2 * DWARF2_ADDR_SIZE;
8477 }
8478
8479 /* Add a new entry to .debug_ranges corresponding to a block, or a
8480    range terminator if BLOCK is NULL.  */
8481
8482 static unsigned int
8483 add_ranges (const_tree block)
8484 {
8485   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8486 }
8487
8488 /* Add a new entry to .debug_ranges corresponding to a pair of
8489    labels.  */
8490
8491 static unsigned int
8492 add_ranges_by_labels (const char *begin, const char *end)
8493 {
8494   unsigned int in_use = ranges_by_label_in_use;
8495
8496   if (in_use == ranges_by_label_allocated)
8497     {
8498       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8499       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8500                                        ranges_by_label,
8501                                        ranges_by_label_allocated);
8502       memset (ranges_by_label + ranges_by_label_in_use, 0,
8503               RANGES_TABLE_INCREMENT
8504               * sizeof (struct dw_ranges_by_label_struct));
8505     }
8506
8507   ranges_by_label[in_use].begin = begin;
8508   ranges_by_label[in_use].end = end;
8509   ranges_by_label_in_use = in_use + 1;
8510
8511   return add_ranges_num (-(int)in_use - 1);
8512 }
8513
8514 static void
8515 output_ranges (void)
8516 {
8517   unsigned i;
8518   static const char *const start_fmt = "Offset 0x%x";
8519   const char *fmt = start_fmt;
8520
8521   for (i = 0; i < ranges_table_in_use; i++)
8522     {
8523       int block_num = ranges_table[i].num;
8524
8525       if (block_num > 0)
8526         {
8527           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8528           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8529
8530           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8531           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8532
8533           /* If all code is in the text section, then the compilation
8534              unit base address defaults to DW_AT_low_pc, which is the
8535              base of the text section.  */
8536           if (!have_multiple_function_sections)
8537             {
8538               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8539                                     text_section_label,
8540                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8541               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8542                                     text_section_label, NULL);
8543             }
8544
8545           /* Otherwise, the compilation unit base address is zero,
8546              which allows us to use absolute addresses, and not worry
8547              about whether the target supports cross-section
8548              arithmetic.  */
8549           else
8550             {
8551               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8552                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8553               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8554             }
8555
8556           fmt = NULL;
8557         }
8558
8559       /* Negative block_num stands for an index into ranges_by_label.  */
8560       else if (block_num < 0)
8561         {
8562           int lab_idx = - block_num - 1;
8563
8564           if (!have_multiple_function_sections)
8565             {
8566               gcc_unreachable ();
8567 #if 0
8568               /* If we ever use add_ranges_by_labels () for a single
8569                  function section, all we have to do is to take out
8570                  the #if 0 above.  */
8571               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8572                                     ranges_by_label[lab_idx].begin,
8573                                     text_section_label,
8574                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8575               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8576                                     ranges_by_label[lab_idx].end,
8577                                     text_section_label, NULL);
8578 #endif
8579             }
8580           else
8581             {
8582               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8583                                    ranges_by_label[lab_idx].begin,
8584                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8585               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8586                                    ranges_by_label[lab_idx].end,
8587                                    NULL);
8588             }
8589         }
8590       else
8591         {
8592           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8593           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8594           fmt = start_fmt;
8595         }
8596     }
8597 }
8598
8599 /* Data structure containing information about input files.  */
8600 struct file_info
8601 {
8602   const char *path;     /* Complete file name.  */
8603   const char *fname;    /* File name part.  */
8604   int length;           /* Length of entire string.  */
8605   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8606   int dir_idx;          /* Index in directory table.  */
8607 };
8608
8609 /* Data structure containing information about directories with source
8610    files.  */
8611 struct dir_info
8612 {
8613   const char *path;     /* Path including directory name.  */
8614   int length;           /* Path length.  */
8615   int prefix;           /* Index of directory entry which is a prefix.  */
8616   int count;            /* Number of files in this directory.  */
8617   int dir_idx;          /* Index of directory used as base.  */
8618 };
8619
8620 /* Callback function for file_info comparison.  We sort by looking at
8621    the directories in the path.  */
8622
8623 static int
8624 file_info_cmp (const void *p1, const void *p2)
8625 {
8626   const struct file_info *const s1 = (const struct file_info *) p1;
8627   const struct file_info *const s2 = (const struct file_info *) p2;
8628   const unsigned char *cp1;
8629   const unsigned char *cp2;
8630
8631   /* Take care of file names without directories.  We need to make sure that
8632      we return consistent values to qsort since some will get confused if
8633      we return the same value when identical operands are passed in opposite
8634      orders.  So if neither has a directory, return 0 and otherwise return
8635      1 or -1 depending on which one has the directory.  */
8636   if ((s1->path == s1->fname || s2->path == s2->fname))
8637     return (s2->path == s2->fname) - (s1->path == s1->fname);
8638
8639   cp1 = (const unsigned char *) s1->path;
8640   cp2 = (const unsigned char *) s2->path;
8641
8642   while (1)
8643     {
8644       ++cp1;
8645       ++cp2;
8646       /* Reached the end of the first path?  If so, handle like above.  */
8647       if ((cp1 == (const unsigned char *) s1->fname)
8648           || (cp2 == (const unsigned char *) s2->fname))
8649         return ((cp2 == (const unsigned char *) s2->fname)
8650                 - (cp1 == (const unsigned char *) s1->fname));
8651
8652       /* Character of current path component the same?  */
8653       else if (*cp1 != *cp2)
8654         return *cp1 - *cp2;
8655     }
8656 }
8657
8658 struct file_name_acquire_data
8659 {
8660   struct file_info *files;
8661   int used_files;
8662   int max_files;
8663 };
8664
8665 /* Traversal function for the hash table.  */
8666
8667 static int
8668 file_name_acquire (void ** slot, void *data)
8669 {
8670   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8671   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8672   struct file_info *fi;
8673   const char *f;
8674
8675   gcc_assert (fnad->max_files >= d->emitted_number);
8676
8677   if (! d->emitted_number)
8678     return 1;
8679
8680   gcc_assert (fnad->max_files != fnad->used_files);
8681
8682   fi = fnad->files + fnad->used_files++;
8683
8684   /* Skip all leading "./".  */
8685   f = d->filename;
8686   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8687     f += 2;
8688
8689   /* Create a new array entry.  */
8690   fi->path = f;
8691   fi->length = strlen (f);
8692   fi->file_idx = d;
8693
8694   /* Search for the file name part.  */
8695   f = strrchr (f, DIR_SEPARATOR);
8696 #if defined (DIR_SEPARATOR_2)
8697   {
8698     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8699
8700     if (g != NULL)
8701       {
8702         if (f == NULL || f < g)
8703           f = g;
8704       }
8705   }
8706 #endif
8707
8708   fi->fname = f == NULL ? fi->path : f + 1;
8709   return 1;
8710 }
8711
8712 /* Output the directory table and the file name table.  We try to minimize
8713    the total amount of memory needed.  A heuristic is used to avoid large
8714    slowdowns with many input files.  */
8715
8716 static void
8717 output_file_names (void)
8718 {
8719   struct file_name_acquire_data fnad;
8720   int numfiles;
8721   struct file_info *files;
8722   struct dir_info *dirs;
8723   int *saved;
8724   int *savehere;
8725   int *backmap;
8726   int ndirs;
8727   int idx_offset;
8728   int i;
8729   int idx;
8730
8731   if (!last_emitted_file)
8732     {
8733       dw2_asm_output_data (1, 0, "End directory table");
8734       dw2_asm_output_data (1, 0, "End file name table");
8735       return;
8736     }
8737
8738   numfiles = last_emitted_file->emitted_number;
8739
8740   /* Allocate the various arrays we need.  */
8741   files = XALLOCAVEC (struct file_info, numfiles);
8742   dirs = XALLOCAVEC (struct dir_info, numfiles);
8743
8744   fnad.files = files;
8745   fnad.used_files = 0;
8746   fnad.max_files = numfiles;
8747   htab_traverse (file_table, file_name_acquire, &fnad);
8748   gcc_assert (fnad.used_files == fnad.max_files);
8749
8750   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8751
8752   /* Find all the different directories used.  */
8753   dirs[0].path = files[0].path;
8754   dirs[0].length = files[0].fname - files[0].path;
8755   dirs[0].prefix = -1;
8756   dirs[0].count = 1;
8757   dirs[0].dir_idx = 0;
8758   files[0].dir_idx = 0;
8759   ndirs = 1;
8760
8761   for (i = 1; i < numfiles; i++)
8762     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8763         && memcmp (dirs[ndirs - 1].path, files[i].path,
8764                    dirs[ndirs - 1].length) == 0)
8765       {
8766         /* Same directory as last entry.  */
8767         files[i].dir_idx = ndirs - 1;
8768         ++dirs[ndirs - 1].count;
8769       }
8770     else
8771       {
8772         int j;
8773
8774         /* This is a new directory.  */
8775         dirs[ndirs].path = files[i].path;
8776         dirs[ndirs].length = files[i].fname - files[i].path;
8777         dirs[ndirs].count = 1;
8778         dirs[ndirs].dir_idx = ndirs;
8779         files[i].dir_idx = ndirs;
8780
8781         /* Search for a prefix.  */
8782         dirs[ndirs].prefix = -1;
8783         for (j = 0; j < ndirs; j++)
8784           if (dirs[j].length < dirs[ndirs].length
8785               && dirs[j].length > 1
8786               && (dirs[ndirs].prefix == -1
8787                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8788               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8789             dirs[ndirs].prefix = j;
8790
8791         ++ndirs;
8792       }
8793
8794   /* Now to the actual work.  We have to find a subset of the directories which
8795      allow expressing the file name using references to the directory table
8796      with the least amount of characters.  We do not do an exhaustive search
8797      where we would have to check out every combination of every single
8798      possible prefix.  Instead we use a heuristic which provides nearly optimal
8799      results in most cases and never is much off.  */
8800   saved = XALLOCAVEC (int, ndirs);
8801   savehere = XALLOCAVEC (int, ndirs);
8802
8803   memset (saved, '\0', ndirs * sizeof (saved[0]));
8804   for (i = 0; i < ndirs; i++)
8805     {
8806       int j;
8807       int total;
8808
8809       /* We can always save some space for the current directory.  But this
8810          does not mean it will be enough to justify adding the directory.  */
8811       savehere[i] = dirs[i].length;
8812       total = (savehere[i] - saved[i]) * dirs[i].count;
8813
8814       for (j = i + 1; j < ndirs; j++)
8815         {
8816           savehere[j] = 0;
8817           if (saved[j] < dirs[i].length)
8818             {
8819               /* Determine whether the dirs[i] path is a prefix of the
8820                  dirs[j] path.  */
8821               int k;
8822
8823               k = dirs[j].prefix;
8824               while (k != -1 && k != (int) i)
8825                 k = dirs[k].prefix;
8826
8827               if (k == (int) i)
8828                 {
8829                   /* Yes it is.  We can possibly save some memory by
8830                      writing the filenames in dirs[j] relative to
8831                      dirs[i].  */
8832                   savehere[j] = dirs[i].length;
8833                   total += (savehere[j] - saved[j]) * dirs[j].count;
8834                 }
8835             }
8836         }
8837
8838       /* Check whether we can save enough to justify adding the dirs[i]
8839          directory.  */
8840       if (total > dirs[i].length + 1)
8841         {
8842           /* It's worthwhile adding.  */
8843           for (j = i; j < ndirs; j++)
8844             if (savehere[j] > 0)
8845               {
8846                 /* Remember how much we saved for this directory so far.  */
8847                 saved[j] = savehere[j];
8848
8849                 /* Remember the prefix directory.  */
8850                 dirs[j].dir_idx = i;
8851               }
8852         }
8853     }
8854
8855   /* Emit the directory name table.  */
8856   idx = 1;
8857   idx_offset = dirs[0].length > 0 ? 1 : 0;
8858   for (i = 1 - idx_offset; i < ndirs; i++)
8859     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8860                             "Directory Entry: 0x%x", i + idx_offset);
8861
8862   dw2_asm_output_data (1, 0, "End directory table");
8863
8864   /* We have to emit them in the order of emitted_number since that's
8865      used in the debug info generation.  To do this efficiently we
8866      generate a back-mapping of the indices first.  */
8867   backmap = XALLOCAVEC (int, numfiles);
8868   for (i = 0; i < numfiles; i++)
8869     backmap[files[i].file_idx->emitted_number - 1] = i;
8870
8871   /* Now write all the file names.  */
8872   for (i = 0; i < numfiles; i++)
8873     {
8874       int file_idx = backmap[i];
8875       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8876
8877       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8878                               "File Entry: 0x%x", (unsigned) i + 1);
8879
8880       /* Include directory index.  */
8881       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8882
8883       /* Modification time.  */
8884       dw2_asm_output_data_uleb128 (0, NULL);
8885
8886       /* File length in bytes.  */
8887       dw2_asm_output_data_uleb128 (0, NULL);
8888     }
8889
8890   dw2_asm_output_data (1, 0, "End file name table");
8891 }
8892
8893
8894 /* Output the source line number correspondence information.  This
8895    information goes into the .debug_line section.  */
8896
8897 static void
8898 output_line_info (void)
8899 {
8900   char l1[20], l2[20], p1[20], p2[20];
8901   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8902   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8903   unsigned opc;
8904   unsigned n_op_args;
8905   unsigned long lt_index;
8906   unsigned long current_line;
8907   long line_offset;
8908   long line_delta;
8909   unsigned long current_file;
8910   unsigned long function;
8911
8912   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8913   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8914   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8915   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8916
8917   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8918     dw2_asm_output_data (4, 0xffffffff,
8919       "Initial length escape value indicating 64-bit DWARF extension");
8920   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8921                         "Length of Source Line Info");
8922   ASM_OUTPUT_LABEL (asm_out_file, l1);
8923
8924   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8925   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8926   ASM_OUTPUT_LABEL (asm_out_file, p1);
8927
8928   /* Define the architecture-dependent minimum instruction length (in
8929    bytes).  In this implementation of DWARF, this field is used for
8930    information purposes only.  Since GCC generates assembly language,
8931    we have no a priori knowledge of how many instruction bytes are
8932    generated for each source line, and therefore can use only the
8933    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8934    commands.  Accordingly, we fix this as `1', which is "correct
8935    enough" for all architectures, and don't let the target override.  */
8936   dw2_asm_output_data (1, 1,
8937                        "Minimum Instruction Length");
8938
8939   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8940                        "Default is_stmt_start flag");
8941   dw2_asm_output_data (1, DWARF_LINE_BASE,
8942                        "Line Base Value (Special Opcodes)");
8943   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8944                        "Line Range Value (Special Opcodes)");
8945   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8946                        "Special Opcode Base");
8947
8948   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8949     {
8950       switch (opc)
8951         {
8952         case DW_LNS_advance_pc:
8953         case DW_LNS_advance_line:
8954         case DW_LNS_set_file:
8955         case DW_LNS_set_column:
8956         case DW_LNS_fixed_advance_pc:
8957           n_op_args = 1;
8958           break;
8959         default:
8960           n_op_args = 0;
8961           break;
8962         }
8963
8964       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8965                            opc, n_op_args);
8966     }
8967
8968   /* Write out the information about the files we use.  */
8969   output_file_names ();
8970   ASM_OUTPUT_LABEL (asm_out_file, p2);
8971
8972   /* We used to set the address register to the first location in the text
8973      section here, but that didn't accomplish anything since we already
8974      have a line note for the opening brace of the first function.  */
8975
8976   /* Generate the line number to PC correspondence table, encoded as
8977      a series of state machine operations.  */
8978   current_file = 1;
8979   current_line = 1;
8980
8981   if (cfun && in_cold_section_p)
8982     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8983   else
8984     strcpy (prev_line_label, text_section_label);
8985   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8986     {
8987       dw_line_info_ref line_info = &line_info_table[lt_index];
8988
8989 #if 0
8990       /* Disable this optimization for now; GDB wants to see two line notes
8991          at the beginning of a function so it can find the end of the
8992          prologue.  */
8993
8994       /* Don't emit anything for redundant notes.  Just updating the
8995          address doesn't accomplish anything, because we already assume
8996          that anything after the last address is this line.  */
8997       if (line_info->dw_line_num == current_line
8998           && line_info->dw_file_num == current_file)
8999         continue;
9000 #endif
9001
9002       /* Emit debug info for the address of the current line.
9003
9004          Unfortunately, we have little choice here currently, and must always
9005          use the most general form.  GCC does not know the address delta
9006          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9007          attributes which will give an upper bound on the address range.  We
9008          could perhaps use length attributes to determine when it is safe to
9009          use DW_LNS_fixed_advance_pc.  */
9010
9011       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9012       if (0)
9013         {
9014           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9015           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9016                                "DW_LNS_fixed_advance_pc");
9017           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9018         }
9019       else
9020         {
9021           /* This can handle any delta.  This takes
9022              4+DWARF2_ADDR_SIZE bytes.  */
9023           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9024           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9025           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9026           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9027         }
9028
9029       strcpy (prev_line_label, line_label);
9030
9031       /* Emit debug info for the source file of the current line, if
9032          different from the previous line.  */
9033       if (line_info->dw_file_num != current_file)
9034         {
9035           current_file = line_info->dw_file_num;
9036           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9037           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9038         }
9039
9040       /* Emit debug info for the current line number, choosing the encoding
9041          that uses the least amount of space.  */
9042       if (line_info->dw_line_num != current_line)
9043         {
9044           line_offset = line_info->dw_line_num - current_line;
9045           line_delta = line_offset - DWARF_LINE_BASE;
9046           current_line = line_info->dw_line_num;
9047           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9048             /* This can handle deltas from -10 to 234, using the current
9049                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9050                takes 1 byte.  */
9051             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9052                                  "line %lu", current_line);
9053           else
9054             {
9055               /* This can handle any delta.  This takes at least 4 bytes,
9056                  depending on the value being encoded.  */
9057               dw2_asm_output_data (1, DW_LNS_advance_line,
9058                                    "advance to line %lu", current_line);
9059               dw2_asm_output_data_sleb128 (line_offset, NULL);
9060               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9061             }
9062         }
9063       else
9064         /* We still need to start a new row, so output a copy insn.  */
9065         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9066     }
9067
9068   /* Emit debug info for the address of the end of the function.  */
9069   if (0)
9070     {
9071       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9072                            "DW_LNS_fixed_advance_pc");
9073       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9074     }
9075   else
9076     {
9077       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9078       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9079       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9080       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9081     }
9082
9083   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9084   dw2_asm_output_data_uleb128 (1, NULL);
9085   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9086
9087   function = 0;
9088   current_file = 1;
9089   current_line = 1;
9090   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9091     {
9092       dw_separate_line_info_ref line_info
9093         = &separate_line_info_table[lt_index];
9094
9095 #if 0
9096       /* Don't emit anything for redundant notes.  */
9097       if (line_info->dw_line_num == current_line
9098           && line_info->dw_file_num == current_file
9099           && line_info->function == function)
9100         goto cont;
9101 #endif
9102
9103       /* Emit debug info for the address of the current line.  If this is
9104          a new function, or the first line of a function, then we need
9105          to handle it differently.  */
9106       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9107                                    lt_index);
9108       if (function != line_info->function)
9109         {
9110           function = line_info->function;
9111
9112           /* Set the address register to the first line in the function.  */
9113           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9114           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9115           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9116           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9117         }
9118       else
9119         {
9120           /* ??? See the DW_LNS_advance_pc comment above.  */
9121           if (0)
9122             {
9123               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9124                                    "DW_LNS_fixed_advance_pc");
9125               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9126             }
9127           else
9128             {
9129               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9130               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9131               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9132               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9133             }
9134         }
9135
9136       strcpy (prev_line_label, line_label);
9137
9138       /* Emit debug info for the source file of the current line, if
9139          different from the previous line.  */
9140       if (line_info->dw_file_num != current_file)
9141         {
9142           current_file = line_info->dw_file_num;
9143           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9144           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9145         }
9146
9147       /* Emit debug info for the current line number, choosing the encoding
9148          that uses the least amount of space.  */
9149       if (line_info->dw_line_num != current_line)
9150         {
9151           line_offset = line_info->dw_line_num - current_line;
9152           line_delta = line_offset - DWARF_LINE_BASE;
9153           current_line = line_info->dw_line_num;
9154           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9155             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9156                                  "line %lu", current_line);
9157           else
9158             {
9159               dw2_asm_output_data (1, DW_LNS_advance_line,
9160                                    "advance to line %lu", current_line);
9161               dw2_asm_output_data_sleb128 (line_offset, NULL);
9162               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9163             }
9164         }
9165       else
9166         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9167
9168 #if 0
9169     cont:
9170 #endif
9171
9172       lt_index++;
9173
9174       /* If we're done with a function, end its sequence.  */
9175       if (lt_index == separate_line_info_table_in_use
9176           || separate_line_info_table[lt_index].function != function)
9177         {
9178           current_file = 1;
9179           current_line = 1;
9180
9181           /* Emit debug info for the address of the end of the function.  */
9182           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9183           if (0)
9184             {
9185               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9186                                    "DW_LNS_fixed_advance_pc");
9187               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9188             }
9189           else
9190             {
9191               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9192               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9193               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9194               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9195             }
9196
9197           /* Output the marker for the end of this sequence.  */
9198           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9199           dw2_asm_output_data_uleb128 (1, NULL);
9200           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9201         }
9202     }
9203
9204   /* Output the marker for the end of the line number info.  */
9205   ASM_OUTPUT_LABEL (asm_out_file, l2);
9206 }
9207 \f
9208 /* Given a pointer to a tree node for some base type, return a pointer to
9209    a DIE that describes the given type.
9210
9211    This routine must only be called for GCC type nodes that correspond to
9212    Dwarf base (fundamental) types.  */
9213
9214 static dw_die_ref
9215 base_type_die (tree type)
9216 {
9217   dw_die_ref base_type_result;
9218   enum dwarf_type encoding;
9219
9220   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9221     return 0;
9222
9223   switch (TREE_CODE (type))
9224     {
9225     case INTEGER_TYPE:
9226       if (TYPE_STRING_FLAG (type))
9227         {
9228           if (TYPE_UNSIGNED (type))
9229             encoding = DW_ATE_unsigned_char;
9230           else
9231             encoding = DW_ATE_signed_char;
9232         }
9233       else if (TYPE_UNSIGNED (type))
9234         encoding = DW_ATE_unsigned;
9235       else
9236         encoding = DW_ATE_signed;
9237       break;
9238
9239     case REAL_TYPE:
9240       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9241         encoding = DW_ATE_decimal_float;
9242       else
9243         encoding = DW_ATE_float;
9244       break;
9245
9246     case FIXED_POINT_TYPE:
9247       if (TYPE_UNSIGNED (type))
9248         encoding = DW_ATE_unsigned_fixed;
9249       else
9250         encoding = DW_ATE_signed_fixed;
9251       break;
9252
9253       /* Dwarf2 doesn't know anything about complex ints, so use
9254          a user defined type for it.  */
9255     case COMPLEX_TYPE:
9256       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9257         encoding = DW_ATE_complex_float;
9258       else
9259         encoding = DW_ATE_lo_user;
9260       break;
9261
9262     case BOOLEAN_TYPE:
9263       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9264       encoding = DW_ATE_boolean;
9265       break;
9266
9267     default:
9268       /* No other TREE_CODEs are Dwarf fundamental types.  */
9269       gcc_unreachable ();
9270     }
9271
9272   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9273
9274   /* This probably indicates a bug.  */
9275   if (! TYPE_NAME (type))
9276     add_name_attribute (base_type_result, "__unknown__");
9277
9278   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9279                    int_size_in_bytes (type));
9280   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9281
9282   return base_type_result;
9283 }
9284
9285 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9286    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9287
9288 static inline int
9289 is_base_type (tree type)
9290 {
9291   switch (TREE_CODE (type))
9292     {
9293     case ERROR_MARK:
9294     case VOID_TYPE:
9295     case INTEGER_TYPE:
9296     case REAL_TYPE:
9297     case FIXED_POINT_TYPE:
9298     case COMPLEX_TYPE:
9299     case BOOLEAN_TYPE:
9300       return 1;
9301
9302     case ARRAY_TYPE:
9303     case RECORD_TYPE:
9304     case UNION_TYPE:
9305     case QUAL_UNION_TYPE:
9306     case ENUMERAL_TYPE:
9307     case FUNCTION_TYPE:
9308     case METHOD_TYPE:
9309     case POINTER_TYPE:
9310     case REFERENCE_TYPE:
9311     case OFFSET_TYPE:
9312     case LANG_TYPE:
9313     case VECTOR_TYPE:
9314       return 0;
9315
9316     default:
9317       gcc_unreachable ();
9318     }
9319
9320   return 0;
9321 }
9322
9323 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9324    node, return the size in bits for the type if it is a constant, or else
9325    return the alignment for the type if the type's size is not constant, or
9326    else return BITS_PER_WORD if the type actually turns out to be an
9327    ERROR_MARK node.  */
9328
9329 static inline unsigned HOST_WIDE_INT
9330 simple_type_size_in_bits (const_tree type)
9331 {
9332   if (TREE_CODE (type) == ERROR_MARK)
9333     return BITS_PER_WORD;
9334   else if (TYPE_SIZE (type) == NULL_TREE)
9335     return 0;
9336   else if (host_integerp (TYPE_SIZE (type), 1))
9337     return tree_low_cst (TYPE_SIZE (type), 1);
9338   else
9339     return TYPE_ALIGN (type);
9340 }
9341
9342 /* Return true if the debug information for the given type should be
9343    emitted as a subrange type.  */
9344
9345 static inline bool
9346 is_subrange_type (const_tree type)
9347 {
9348   tree subtype = TREE_TYPE (type);
9349
9350   /* Subrange types are identified by the fact that they are integer
9351      types, and that they have a subtype which is either an integer type
9352      or an enumeral type.  */
9353
9354   if (TREE_CODE (type) != INTEGER_TYPE
9355       || subtype == NULL_TREE)
9356     return false;
9357
9358   if (TREE_CODE (subtype) != INTEGER_TYPE
9359       && TREE_CODE (subtype) != ENUMERAL_TYPE
9360       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9361     return false;
9362
9363   if (TREE_CODE (type) == TREE_CODE (subtype)
9364       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9365       && TYPE_MIN_VALUE (type) != NULL
9366       && TYPE_MIN_VALUE (subtype) != NULL
9367       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9368       && TYPE_MAX_VALUE (type) != NULL
9369       && TYPE_MAX_VALUE (subtype) != NULL
9370       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9371     {
9372       /* The type and its subtype have the same representation.  If in
9373          addition the two types also have the same name, then the given
9374          type is not a subrange type, but rather a plain base type.  */
9375       /* FIXME: brobecker/2004-03-22:
9376          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9377          therefore be sufficient to check the TYPE_SIZE node pointers
9378          rather than checking the actual size.  Unfortunately, we have
9379          found some cases, such as in the Ada "integer" type, where
9380          this is not the case.  Until this problem is solved, we need to
9381          keep checking the actual size.  */
9382       tree type_name = TYPE_NAME (type);
9383       tree subtype_name = TYPE_NAME (subtype);
9384
9385       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9386         type_name = DECL_NAME (type_name);
9387
9388       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9389         subtype_name = DECL_NAME (subtype_name);
9390
9391       if (type_name == subtype_name)
9392         return false;
9393     }
9394
9395   return true;
9396 }
9397
9398 /*  Given a pointer to a tree node for a subrange type, return a pointer
9399     to a DIE that describes the given type.  */
9400
9401 static dw_die_ref
9402 subrange_type_die (tree type, dw_die_ref context_die)
9403 {
9404   dw_die_ref subrange_die;
9405   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9406
9407   if (context_die == NULL)
9408     context_die = comp_unit_die;
9409
9410   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9411
9412   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9413     {
9414       /* The size of the subrange type and its base type do not match,
9415          so we need to generate a size attribute for the subrange type.  */
9416       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9417     }
9418
9419   if (TYPE_MIN_VALUE (type) != NULL)
9420     add_bound_info (subrange_die, DW_AT_lower_bound,
9421                     TYPE_MIN_VALUE (type));
9422   if (TYPE_MAX_VALUE (type) != NULL)
9423     add_bound_info (subrange_die, DW_AT_upper_bound,
9424                     TYPE_MAX_VALUE (type));
9425
9426   return subrange_die;
9427 }
9428
9429 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9430    entry that chains various modifiers in front of the given type.  */
9431
9432 static dw_die_ref
9433 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9434                    dw_die_ref context_die)
9435 {
9436   enum tree_code code = TREE_CODE (type);
9437   dw_die_ref mod_type_die;
9438   dw_die_ref sub_die = NULL;
9439   tree item_type = NULL;
9440   tree qualified_type;
9441   tree name;
9442
9443   if (code == ERROR_MARK)
9444     return NULL;
9445
9446   /* See if we already have the appropriately qualified variant of
9447      this type.  */
9448   qualified_type
9449     = get_qualified_type (type,
9450                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9451                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9452
9453   /* If we do, then we can just use its DIE, if it exists.  */
9454   if (qualified_type)
9455     {
9456       mod_type_die = lookup_type_die (qualified_type);
9457       if (mod_type_die)
9458         return mod_type_die;
9459     }
9460
9461   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9462
9463   /* Handle C typedef types.  */
9464   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9465     {
9466       tree dtype = TREE_TYPE (name);
9467
9468       if (qualified_type == dtype)
9469         {
9470           /* For a named type, use the typedef.  */
9471           gen_type_die (qualified_type, context_die);
9472           return lookup_type_die (qualified_type);
9473         }
9474       else if (is_const_type < TYPE_READONLY (dtype)
9475                || is_volatile_type < TYPE_VOLATILE (dtype)
9476                || (is_const_type <= TYPE_READONLY (dtype)
9477                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9478                    && DECL_ORIGINAL_TYPE (name) != type))
9479         /* cv-unqualified version of named type.  Just use the unnamed
9480            type to which it refers.  */
9481         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9482                                   is_const_type, is_volatile_type,
9483                                   context_die);
9484       /* Else cv-qualified version of named type; fall through.  */
9485     }
9486
9487   if (is_const_type)
9488     {
9489       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9490       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9491     }
9492   else if (is_volatile_type)
9493     {
9494       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9495       sub_die = modified_type_die (type, 0, 0, context_die);
9496     }
9497   else if (code == POINTER_TYPE)
9498     {
9499       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9500       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9501                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9502       item_type = TREE_TYPE (type);
9503     }
9504   else if (code == REFERENCE_TYPE)
9505     {
9506       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9507       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9508                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9509       item_type = TREE_TYPE (type);
9510     }
9511   else if (is_subrange_type (type))
9512     {
9513       mod_type_die = subrange_type_die (type, context_die);
9514       item_type = TREE_TYPE (type);
9515     }
9516   else if (is_base_type (type))
9517     mod_type_die = base_type_die (type);
9518   else
9519     {
9520       gen_type_die (type, context_die);
9521
9522       /* We have to get the type_main_variant here (and pass that to the
9523          `lookup_type_die' routine) because the ..._TYPE node we have
9524          might simply be a *copy* of some original type node (where the
9525          copy was created to help us keep track of typedef names) and
9526          that copy might have a different TYPE_UID from the original
9527          ..._TYPE node.  */
9528       if (TREE_CODE (type) != VECTOR_TYPE)
9529         return lookup_type_die (type_main_variant (type));
9530       else
9531         /* Vectors have the debugging information in the type,
9532            not the main variant.  */
9533         return lookup_type_die (type);
9534     }
9535
9536   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9537      don't output a DW_TAG_typedef, since there isn't one in the
9538      user's program; just attach a DW_AT_name to the type.  */
9539   if (name
9540       && (TREE_CODE (name) != TYPE_DECL
9541           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9542     {
9543       if (TREE_CODE (name) == TYPE_DECL)
9544         /* Could just call add_name_and_src_coords_attributes here,
9545            but since this is a builtin type it doesn't have any
9546            useful source coordinates anyway.  */
9547         name = DECL_NAME (name);
9548       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9549     }
9550
9551   if (qualified_type)
9552     equate_type_number_to_die (qualified_type, mod_type_die);
9553
9554   if (item_type)
9555     /* We must do this after the equate_type_number_to_die call, in case
9556        this is a recursive type.  This ensures that the modified_type_die
9557        recursion will terminate even if the type is recursive.  Recursive
9558        types are possible in Ada.  */
9559     sub_die = modified_type_die (item_type,
9560                                  TYPE_READONLY (item_type),
9561                                  TYPE_VOLATILE (item_type),
9562                                  context_die);
9563
9564   if (sub_die != NULL)
9565     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9566
9567   return mod_type_die;
9568 }
9569
9570 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9571    an enumerated type.  */
9572
9573 static inline int
9574 type_is_enum (const_tree type)
9575 {
9576   return TREE_CODE (type) == ENUMERAL_TYPE;
9577 }
9578
9579 /* Return the DBX register number described by a given RTL node.  */
9580
9581 static unsigned int
9582 dbx_reg_number (const_rtx rtl)
9583 {
9584   unsigned regno = REGNO (rtl);
9585
9586   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9587
9588 #ifdef LEAF_REG_REMAP
9589   if (current_function_uses_only_leaf_regs)
9590     {
9591       int leaf_reg = LEAF_REG_REMAP (regno);
9592       if (leaf_reg != -1)
9593         regno = (unsigned) leaf_reg;
9594     }
9595 #endif
9596
9597   return DBX_REGISTER_NUMBER (regno);
9598 }
9599
9600 /* Optionally add a DW_OP_piece term to a location description expression.
9601    DW_OP_piece is only added if the location description expression already
9602    doesn't end with DW_OP_piece.  */
9603
9604 static void
9605 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9606 {
9607   dw_loc_descr_ref loc;
9608
9609   if (*list_head != NULL)
9610     {
9611       /* Find the end of the chain.  */
9612       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9613         ;
9614
9615       if (loc->dw_loc_opc != DW_OP_piece)
9616         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9617     }
9618 }
9619
9620 /* Return a location descriptor that designates a machine register or
9621    zero if there is none.  */
9622
9623 static dw_loc_descr_ref
9624 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9625 {
9626   rtx regs;
9627
9628   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9629     return 0;
9630
9631   regs = targetm.dwarf_register_span (rtl);
9632
9633   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9634     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9635   else
9636     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9637 }
9638
9639 /* Return a location descriptor that designates a machine register for
9640    a given hard register number.  */
9641
9642 static dw_loc_descr_ref
9643 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9644 {
9645   dw_loc_descr_ref reg_loc_descr;
9646   if (regno <= 31)
9647     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
9648   else
9649     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
9650
9651   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9652     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9653
9654   return reg_loc_descr;
9655 }
9656
9657 /* Given an RTL of a register, return a location descriptor that
9658    designates a value that spans more than one register.  */
9659
9660 static dw_loc_descr_ref
9661 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9662                              enum var_init_status initialized)
9663 {
9664   int nregs, size, i;
9665   unsigned reg;
9666   dw_loc_descr_ref loc_result = NULL;
9667
9668   reg = REGNO (rtl);
9669 #ifdef LEAF_REG_REMAP
9670   if (current_function_uses_only_leaf_regs)
9671     {
9672       int leaf_reg = LEAF_REG_REMAP (reg);
9673       if (leaf_reg != -1)
9674         reg = (unsigned) leaf_reg;
9675     }
9676 #endif
9677   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9678   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9679
9680   /* Simple, contiguous registers.  */
9681   if (regs == NULL_RTX)
9682     {
9683       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9684
9685       loc_result = NULL;
9686       while (nregs--)
9687         {
9688           dw_loc_descr_ref t;
9689
9690           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9691                                       VAR_INIT_STATUS_INITIALIZED);
9692           add_loc_descr (&loc_result, t);
9693           add_loc_descr_op_piece (&loc_result, size);
9694           ++reg;
9695         }
9696       return loc_result;
9697     }
9698
9699   /* Now onto stupid register sets in non contiguous locations.  */
9700
9701   gcc_assert (GET_CODE (regs) == PARALLEL);
9702
9703   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9704   loc_result = NULL;
9705
9706   for (i = 0; i < XVECLEN (regs, 0); ++i)
9707     {
9708       dw_loc_descr_ref t;
9709
9710       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9711                                   VAR_INIT_STATUS_INITIALIZED);
9712       add_loc_descr (&loc_result, t);
9713       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9714       add_loc_descr_op_piece (&loc_result, size);
9715     }
9716
9717   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9718     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9719   return loc_result;
9720 }
9721
9722 #endif /* DWARF2_DEBUGGING_INFO */
9723
9724 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9725
9726 /* Return a location descriptor that designates a constant.  */
9727
9728 static dw_loc_descr_ref
9729 int_loc_descriptor (HOST_WIDE_INT i)
9730 {
9731   enum dwarf_location_atom op;
9732
9733   /* Pick the smallest representation of a constant, rather than just
9734      defaulting to the LEB encoding.  */
9735   if (i >= 0)
9736     {
9737       if (i <= 31)
9738         op = DW_OP_lit0 + i;
9739       else if (i <= 0xff)
9740         op = DW_OP_const1u;
9741       else if (i <= 0xffff)
9742         op = DW_OP_const2u;
9743       else if (HOST_BITS_PER_WIDE_INT == 32
9744                || i <= 0xffffffff)
9745         op = DW_OP_const4u;
9746       else
9747         op = DW_OP_constu;
9748     }
9749   else
9750     {
9751       if (i >= -0x80)
9752         op = DW_OP_const1s;
9753       else if (i >= -0x8000)
9754         op = DW_OP_const2s;
9755       else if (HOST_BITS_PER_WIDE_INT == 32
9756                || i >= -0x80000000)
9757         op = DW_OP_const4s;
9758       else
9759         op = DW_OP_consts;
9760     }
9761
9762   return new_loc_descr (op, i, 0);
9763 }
9764 #endif
9765
9766 #ifdef DWARF2_DEBUGGING_INFO
9767
9768 /* Return a location descriptor that designates a base+offset location.  */
9769
9770 static dw_loc_descr_ref
9771 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9772                  enum var_init_status initialized)
9773 {
9774   unsigned int regno;
9775   dw_loc_descr_ref result;
9776   dw_fde_ref fde = current_fde ();
9777
9778   /* We only use "frame base" when we're sure we're talking about the
9779      post-prologue local stack frame.  We do this by *not* running
9780      register elimination until this point, and recognizing the special
9781      argument pointer and soft frame pointer rtx's.  */
9782   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9783     {
9784       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9785
9786       if (elim != reg)
9787         {
9788           if (GET_CODE (elim) == PLUS)
9789             {
9790               offset += INTVAL (XEXP (elim, 1));
9791               elim = XEXP (elim, 0);
9792             }
9793           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9794                        && (elim == hard_frame_pointer_rtx
9795                            || elim == stack_pointer_rtx))
9796                       || elim == (frame_pointer_needed
9797                                   ? hard_frame_pointer_rtx
9798                                   : stack_pointer_rtx));
9799
9800           /* If drap register is used to align stack, use frame
9801              pointer + offset to access stack variables.  If stack
9802              is aligned without drap, use stack pointer + offset to
9803              access stack variables.  */
9804           if (crtl->stack_realign_tried
9805               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9806               && reg == frame_pointer_rtx)
9807             {
9808               int base_reg
9809                 = DWARF_FRAME_REGNUM (cfa.indirect
9810                                       ? HARD_FRAME_POINTER_REGNUM
9811                                       : STACK_POINTER_REGNUM);
9812               if (base_reg <= 31)
9813                 return new_loc_descr (DW_OP_breg0 + base_reg, offset, 0);
9814               else
9815                 return new_loc_descr (DW_OP_bregx, base_reg, offset);
9816             }
9817
9818           offset += frame_pointer_fb_offset;
9819           return new_loc_descr (DW_OP_fbreg, offset, 0);
9820         }
9821     }
9822   else if (fde
9823            && fde->drap_reg != INVALID_REGNUM
9824            && (fde->drap_reg == REGNO (reg)
9825                || fde->vdrap_reg == REGNO (reg)))
9826     {
9827       /* Use cfa+offset to represent the location of arguments passed
9828          on stack when drap is used to align stack.  */
9829       return new_loc_descr (DW_OP_fbreg, offset, 0);
9830     }
9831
9832   regno = dbx_reg_number (reg);
9833   if (regno <= 31)
9834     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9835   else
9836     result = new_loc_descr (DW_OP_bregx, regno, offset);
9837
9838   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9839     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9840
9841   return result;
9842 }
9843
9844 /* Return true if this RTL expression describes a base+offset calculation.  */
9845
9846 static inline int
9847 is_based_loc (const_rtx rtl)
9848 {
9849   return (GET_CODE (rtl) == PLUS
9850           && ((REG_P (XEXP (rtl, 0))
9851                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9852                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9853 }
9854
9855 /* Return a descriptor that describes the concatenation of N locations
9856    used to form the address of a memory location.  */
9857
9858 static dw_loc_descr_ref
9859 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9860                             enum var_init_status initialized)
9861 {
9862   unsigned int i;
9863   dw_loc_descr_ref cc_loc_result = NULL;
9864   unsigned int n = XVECLEN (concatn, 0);
9865
9866   for (i = 0; i < n; ++i)
9867     {
9868       dw_loc_descr_ref ref;
9869       rtx x = XVECEXP (concatn, 0, i);
9870
9871       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9872       if (ref == NULL)
9873         return NULL;
9874
9875       add_loc_descr (&cc_loc_result, ref);
9876       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9877     }
9878
9879   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9880     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9881
9882   return cc_loc_result;
9883 }
9884
9885 /* The following routine converts the RTL for a variable or parameter
9886    (resident in memory) into an equivalent Dwarf representation of a
9887    mechanism for getting the address of that same variable onto the top of a
9888    hypothetical "address evaluation" stack.
9889
9890    When creating memory location descriptors, we are effectively transforming
9891    the RTL for a memory-resident object into its Dwarf postfix expression
9892    equivalent.  This routine recursively descends an RTL tree, turning
9893    it into Dwarf postfix code as it goes.
9894
9895    MODE is the mode of the memory reference, needed to handle some
9896    autoincrement addressing modes.
9897
9898    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9899    location list for RTL.
9900
9901    Return 0 if we can't represent the location.  */
9902
9903 static dw_loc_descr_ref
9904 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9905                     enum var_init_status initialized)
9906 {
9907   dw_loc_descr_ref mem_loc_result = NULL;
9908   enum dwarf_location_atom op;
9909
9910   /* Note that for a dynamically sized array, the location we will generate a
9911      description of here will be the lowest numbered location which is
9912      actually within the array.  That's *not* necessarily the same as the
9913      zeroth element of the array.  */
9914
9915   rtl = targetm.delegitimize_address (rtl);
9916
9917   switch (GET_CODE (rtl))
9918     {
9919     case POST_INC:
9920     case POST_DEC:
9921     case POST_MODIFY:
9922       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9923          just fall into the SUBREG code.  */
9924
9925       /* ... fall through ...  */
9926
9927     case SUBREG:
9928       /* The case of a subreg may arise when we have a local (register)
9929          variable or a formal (register) parameter which doesn't quite fill
9930          up an entire register.  For now, just assume that it is
9931          legitimate to make the Dwarf info refer to the whole register which
9932          contains the given subreg.  */
9933       rtl = XEXP (rtl, 0);
9934
9935       /* ... fall through ...  */
9936
9937     case REG:
9938       /* Whenever a register number forms a part of the description of the
9939          method for calculating the (dynamic) address of a memory resident
9940          object, DWARF rules require the register number be referred to as
9941          a "base register".  This distinction is not based in any way upon
9942          what category of register the hardware believes the given register
9943          belongs to.  This is strictly DWARF terminology we're dealing with
9944          here. Note that in cases where the location of a memory-resident
9945          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9946          OP_CONST (0)) the actual DWARF location descriptor that we generate
9947          may just be OP_BASEREG (basereg).  This may look deceptively like
9948          the object in question was allocated to a register (rather than in
9949          memory) so DWARF consumers need to be aware of the subtle
9950          distinction between OP_REG and OP_BASEREG.  */
9951       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9952         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9953       break;
9954
9955     case MEM:
9956       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9957                                            VAR_INIT_STATUS_INITIALIZED);
9958       if (mem_loc_result != 0)
9959         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9960       break;
9961
9962     case LO_SUM:
9963          rtl = XEXP (rtl, 1);
9964
9965       /* ... fall through ...  */
9966
9967     case LABEL_REF:
9968       /* Some ports can transform a symbol ref into a label ref, because
9969          the symbol ref is too far away and has to be dumped into a constant
9970          pool.  */
9971     case CONST:
9972     case SYMBOL_REF:
9973       /* Alternatively, the symbol in the constant pool might be referenced
9974          by a different symbol.  */
9975       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9976         {
9977           bool marked;
9978           rtx tmp = get_pool_constant_mark (rtl, &marked);
9979
9980           if (GET_CODE (tmp) == SYMBOL_REF)
9981             {
9982               rtl = tmp;
9983               if (CONSTANT_POOL_ADDRESS_P (tmp))
9984                 get_pool_constant_mark (tmp, &marked);
9985               else
9986                 marked = true;
9987             }
9988
9989           /* If all references to this pool constant were optimized away,
9990              it was not output and thus we can't represent it.
9991              FIXME: might try to use DW_OP_const_value here, though
9992              DW_OP_piece complicates it.  */
9993           if (!marked)
9994             return 0;
9995         }
9996
9997       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9998       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9999       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10000       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10001       break;
10002
10003     case PRE_MODIFY:
10004       /* Extract the PLUS expression nested inside and fall into
10005          PLUS code below.  */
10006       rtl = XEXP (rtl, 1);
10007       goto plus;
10008
10009     case PRE_INC:
10010     case PRE_DEC:
10011       /* Turn these into a PLUS expression and fall into the PLUS code
10012          below.  */
10013       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10014                           GEN_INT (GET_CODE (rtl) == PRE_INC
10015                                    ? GET_MODE_UNIT_SIZE (mode)
10016                                    : -GET_MODE_UNIT_SIZE (mode)));
10017
10018       /* ... fall through ...  */
10019
10020     case PLUS:
10021     plus:
10022       if (is_based_loc (rtl))
10023         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10024                                           INTVAL (XEXP (rtl, 1)),
10025                                           VAR_INIT_STATUS_INITIALIZED);
10026       else
10027         {
10028           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10029                                                VAR_INIT_STATUS_INITIALIZED);
10030           if (mem_loc_result == 0)
10031             break;
10032
10033           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10034               && INTVAL (XEXP (rtl, 1)) >= 0)
10035             add_loc_descr (&mem_loc_result,
10036                            new_loc_descr (DW_OP_plus_uconst,
10037                                           INTVAL (XEXP (rtl, 1)), 0));
10038           else
10039             {
10040               add_loc_descr (&mem_loc_result,
10041                              mem_loc_descriptor (XEXP (rtl, 1), mode,
10042                                                  VAR_INIT_STATUS_INITIALIZED));
10043               add_loc_descr (&mem_loc_result,
10044                              new_loc_descr (DW_OP_plus, 0, 0));
10045             }
10046         }
10047       break;
10048
10049     /* If a pseudo-reg is optimized away, it is possible for it to
10050        be replaced with a MEM containing a multiply or shift.  */
10051     case MULT:
10052       op = DW_OP_mul;
10053       goto do_binop;
10054
10055     case ASHIFT:
10056       op = DW_OP_shl;
10057       goto do_binop;
10058
10059     case ASHIFTRT:
10060       op = DW_OP_shra;
10061       goto do_binop;
10062
10063     case LSHIFTRT:
10064       op = DW_OP_shr;
10065       goto do_binop;
10066
10067     do_binop:
10068       {
10069         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10070                                                    VAR_INIT_STATUS_INITIALIZED);
10071         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10072                                                    VAR_INIT_STATUS_INITIALIZED);
10073
10074         if (op0 == 0 || op1 == 0)
10075           break;
10076
10077         mem_loc_result = op0;
10078         add_loc_descr (&mem_loc_result, op1);
10079         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10080         break;
10081       }
10082
10083     case CONST_INT:
10084       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10085       break;
10086
10087     case CONCATN:
10088       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10089                                                    VAR_INIT_STATUS_INITIALIZED);
10090       break;
10091
10092     default:
10093       gcc_unreachable ();
10094     }
10095
10096   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10097     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10098
10099   return mem_loc_result;
10100 }
10101
10102 /* Return a descriptor that describes the concatenation of two locations.
10103    This is typically a complex variable.  */
10104
10105 static dw_loc_descr_ref
10106 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10107 {
10108   dw_loc_descr_ref cc_loc_result = NULL;
10109   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10110   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10111
10112   if (x0_ref == 0 || x1_ref == 0)
10113     return 0;
10114
10115   cc_loc_result = x0_ref;
10116   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10117
10118   add_loc_descr (&cc_loc_result, x1_ref);
10119   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10120
10121   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10122     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10123
10124   return cc_loc_result;
10125 }
10126
10127 /* Return a descriptor that describes the concatenation of N
10128    locations.  */
10129
10130 static dw_loc_descr_ref
10131 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10132 {
10133   unsigned int i;
10134   dw_loc_descr_ref cc_loc_result = NULL;
10135   unsigned int n = XVECLEN (concatn, 0);
10136
10137   for (i = 0; i < n; ++i)
10138     {
10139       dw_loc_descr_ref ref;
10140       rtx x = XVECEXP (concatn, 0, i);
10141
10142       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10143       if (ref == NULL)
10144         return NULL;
10145
10146       add_loc_descr (&cc_loc_result, ref);
10147       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10148     }
10149
10150   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10151     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10152
10153   return cc_loc_result;
10154 }
10155
10156 /* Output a proper Dwarf location descriptor for a variable or parameter
10157    which is either allocated in a register or in a memory location.  For a
10158    register, we just generate an OP_REG and the register number.  For a
10159    memory location we provide a Dwarf postfix expression describing how to
10160    generate the (dynamic) address of the object onto the address stack.
10161
10162    If we don't know how to describe it, return 0.  */
10163
10164 static dw_loc_descr_ref
10165 loc_descriptor (rtx rtl, enum var_init_status initialized)
10166 {
10167   dw_loc_descr_ref loc_result = NULL;
10168
10169   switch (GET_CODE (rtl))
10170     {
10171     case SUBREG:
10172       /* The case of a subreg may arise when we have a local (register)
10173          variable or a formal (register) parameter which doesn't quite fill
10174          up an entire register.  For now, just assume that it is
10175          legitimate to make the Dwarf info refer to the whole register which
10176          contains the given subreg.  */
10177       rtl = SUBREG_REG (rtl);
10178
10179       /* ... fall through ...  */
10180
10181     case REG:
10182       loc_result = reg_loc_descriptor (rtl, initialized);
10183       break;
10184
10185     case MEM:
10186       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10187                                        initialized);
10188       break;
10189
10190     case CONCAT:
10191       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10192                                           initialized);
10193       break;
10194
10195     case CONCATN:
10196       loc_result = concatn_loc_descriptor (rtl, initialized);
10197       break;
10198
10199     case VAR_LOCATION:
10200       /* Single part.  */
10201       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10202         {
10203           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10204           break;
10205         }
10206
10207       rtl = XEXP (rtl, 1);
10208       /* FALLTHRU */
10209
10210     case PARALLEL:
10211       {
10212         rtvec par_elems = XVEC (rtl, 0);
10213         int num_elem = GET_NUM_ELEM (par_elems);
10214         enum machine_mode mode;
10215         int i;
10216
10217         /* Create the first one, so we have something to add to.  */
10218         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10219                                      initialized);
10220         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10221         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10222         for (i = 1; i < num_elem; i++)
10223           {
10224             dw_loc_descr_ref temp;
10225
10226             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10227                                    initialized);
10228             add_loc_descr (&loc_result, temp);
10229             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10230             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10231           }
10232       }
10233       break;
10234
10235     default:
10236       gcc_unreachable ();
10237     }
10238
10239   return loc_result;
10240 }
10241
10242 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10243    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10244    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10245    top-level invocation, and we require the address of LOC; is 0 if we require
10246    the value of LOC.  */
10247
10248 static dw_loc_descr_ref
10249 loc_descriptor_from_tree_1 (tree loc, int want_address)
10250 {
10251   dw_loc_descr_ref ret, ret1;
10252   int have_address = 0;
10253   enum dwarf_location_atom op;
10254
10255   /* ??? Most of the time we do not take proper care for sign/zero
10256      extending the values properly.  Hopefully this won't be a real
10257      problem...  */
10258
10259   switch (TREE_CODE (loc))
10260     {
10261     case ERROR_MARK:
10262       return 0;
10263
10264     case PLACEHOLDER_EXPR:
10265       /* This case involves extracting fields from an object to determine the
10266          position of other fields.  We don't try to encode this here.  The
10267          only user of this is Ada, which encodes the needed information using
10268          the names of types.  */
10269       return 0;
10270
10271     case CALL_EXPR:
10272       return 0;
10273
10274     case PREINCREMENT_EXPR:
10275     case PREDECREMENT_EXPR:
10276     case POSTINCREMENT_EXPR:
10277     case POSTDECREMENT_EXPR:
10278       /* There are no opcodes for these operations.  */
10279       return 0;
10280
10281     case ADDR_EXPR:
10282       /* If we already want an address, there's nothing we can do.  */
10283       if (want_address)
10284         return 0;
10285
10286       /* Otherwise, process the argument and look for the address.  */
10287       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10288
10289     case VAR_DECL:
10290       if (DECL_THREAD_LOCAL_P (loc))
10291         {
10292           rtx rtl;
10293           unsigned first_op;
10294           unsigned second_op;
10295
10296           if (targetm.have_tls)
10297             {
10298               /* If this is not defined, we have no way to emit the
10299                  data.  */
10300               if (!targetm.asm_out.output_dwarf_dtprel)
10301                 return 0;
10302
10303                /* The way DW_OP_GNU_push_tls_address is specified, we
10304                   can only look up addresses of objects in the current
10305                   module.  */
10306               if (DECL_EXTERNAL (loc))
10307                 return 0;
10308               first_op = INTERNAL_DW_OP_tls_addr;
10309               second_op = DW_OP_GNU_push_tls_address;
10310             }
10311           else
10312             {
10313               if (!targetm.emutls.debug_form_tls_address)
10314                 return 0;
10315               loc = emutls_decl (loc);
10316               first_op = DW_OP_addr;
10317               second_op = DW_OP_form_tls_address;
10318             }
10319
10320           rtl = rtl_for_decl_location (loc);
10321           if (rtl == NULL_RTX)
10322             return 0;
10323
10324           if (!MEM_P (rtl))
10325             return 0;
10326           rtl = XEXP (rtl, 0);
10327           if (! CONSTANT_P (rtl))
10328             return 0;
10329
10330           ret = new_loc_descr (first_op, 0, 0);
10331           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10332           ret->dw_loc_oprnd1.v.val_addr = rtl;
10333
10334           ret1 = new_loc_descr (second_op, 0, 0);
10335           add_loc_descr (&ret, ret1);
10336
10337           have_address = 1;
10338           break;
10339         }
10340       /* FALLTHRU */
10341
10342     case PARM_DECL:
10343       if (DECL_HAS_VALUE_EXPR_P (loc))
10344         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10345                                            want_address);
10346       /* FALLTHRU */
10347
10348     case RESULT_DECL:
10349     case FUNCTION_DECL:
10350       {
10351         rtx rtl = rtl_for_decl_location (loc);
10352
10353         if (rtl == NULL_RTX)
10354           return 0;
10355         else if (GET_CODE (rtl) == CONST_INT)
10356           {
10357             HOST_WIDE_INT val = INTVAL (rtl);
10358             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10359               val &= GET_MODE_MASK (DECL_MODE (loc));
10360             ret = int_loc_descriptor (val);
10361           }
10362         else if (GET_CODE (rtl) == CONST_STRING)
10363           return 0;
10364         else if (CONSTANT_P (rtl))
10365           {
10366             ret = new_loc_descr (DW_OP_addr, 0, 0);
10367             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10368             ret->dw_loc_oprnd1.v.val_addr = rtl;
10369           }
10370         else
10371           {
10372             enum machine_mode mode;
10373
10374             /* Certain constructs can only be represented at top-level.  */
10375             if (want_address == 2)
10376               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10377
10378             mode = GET_MODE (rtl);
10379             if (MEM_P (rtl))
10380               {
10381                 rtl = XEXP (rtl, 0);
10382                 have_address = 1;
10383               }
10384             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10385           }
10386       }
10387       break;
10388
10389     case INDIRECT_REF:
10390       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10391       have_address = 1;
10392       break;
10393
10394     case COMPOUND_EXPR:
10395       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10396
10397     CASE_CONVERT:
10398     case VIEW_CONVERT_EXPR:
10399     case SAVE_EXPR:
10400     case MODIFY_EXPR:
10401       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10402
10403     case COMPONENT_REF:
10404     case BIT_FIELD_REF:
10405     case ARRAY_REF:
10406     case ARRAY_RANGE_REF:
10407       {
10408         tree obj, offset;
10409         HOST_WIDE_INT bitsize, bitpos, bytepos;
10410         enum machine_mode mode;
10411         int volatilep;
10412         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10413
10414         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10415                                    &unsignedp, &volatilep, false);
10416
10417         if (obj == loc)
10418           return 0;
10419
10420         ret = loc_descriptor_from_tree_1 (obj, 1);
10421         if (ret == 0
10422             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10423           return 0;
10424
10425         if (offset != NULL_TREE)
10426           {
10427             /* Variable offset.  */
10428             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
10429             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10430           }
10431
10432         bytepos = bitpos / BITS_PER_UNIT;
10433         if (bytepos > 0)
10434           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10435         else if (bytepos < 0)
10436           {
10437             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10438             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10439           }
10440
10441         have_address = 1;
10442         break;
10443       }
10444
10445     case INTEGER_CST:
10446       if (host_integerp (loc, 0))
10447         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10448       else
10449         return 0;
10450       break;
10451
10452     case CONSTRUCTOR:
10453       {
10454         /* Get an RTL for this, if something has been emitted.  */
10455         rtx rtl = lookup_constant_def (loc);
10456         enum machine_mode mode;
10457
10458         if (!rtl || !MEM_P (rtl))
10459           return 0;
10460         mode = GET_MODE (rtl);
10461         rtl = XEXP (rtl, 0);
10462         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10463         have_address = 1;
10464         break;
10465       }
10466
10467     case TRUTH_AND_EXPR:
10468     case TRUTH_ANDIF_EXPR:
10469     case BIT_AND_EXPR:
10470       op = DW_OP_and;
10471       goto do_binop;
10472
10473     case TRUTH_XOR_EXPR:
10474     case BIT_XOR_EXPR:
10475       op = DW_OP_xor;
10476       goto do_binop;
10477
10478     case TRUTH_OR_EXPR:
10479     case TRUTH_ORIF_EXPR:
10480     case BIT_IOR_EXPR:
10481       op = DW_OP_or;
10482       goto do_binop;
10483
10484     case FLOOR_DIV_EXPR:
10485     case CEIL_DIV_EXPR:
10486     case ROUND_DIV_EXPR:
10487     case TRUNC_DIV_EXPR:
10488       op = DW_OP_div;
10489       goto do_binop;
10490
10491     case MINUS_EXPR:
10492       op = DW_OP_minus;
10493       goto do_binop;
10494
10495     case FLOOR_MOD_EXPR:
10496     case CEIL_MOD_EXPR:
10497     case ROUND_MOD_EXPR:
10498     case TRUNC_MOD_EXPR:
10499       op = DW_OP_mod;
10500       goto do_binop;
10501
10502     case MULT_EXPR:
10503       op = DW_OP_mul;
10504       goto do_binop;
10505
10506     case LSHIFT_EXPR:
10507       op = DW_OP_shl;
10508       goto do_binop;
10509
10510     case RSHIFT_EXPR:
10511       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10512       goto do_binop;
10513
10514     case POINTER_PLUS_EXPR:
10515     case PLUS_EXPR:
10516       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10517           && host_integerp (TREE_OPERAND (loc, 1), 0))
10518         {
10519           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10520           if (ret == 0)
10521             return 0;
10522
10523           add_loc_descr (&ret,
10524                          new_loc_descr (DW_OP_plus_uconst,
10525                                         tree_low_cst (TREE_OPERAND (loc, 1),
10526                                                       0),
10527                                         0));
10528           break;
10529         }
10530
10531       op = DW_OP_plus;
10532       goto do_binop;
10533
10534     case LE_EXPR:
10535       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10536         return 0;
10537
10538       op = DW_OP_le;
10539       goto do_binop;
10540
10541     case GE_EXPR:
10542       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10543         return 0;
10544
10545       op = DW_OP_ge;
10546       goto do_binop;
10547
10548     case LT_EXPR:
10549       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10550         return 0;
10551
10552       op = DW_OP_lt;
10553       goto do_binop;
10554
10555     case GT_EXPR:
10556       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10557         return 0;
10558
10559       op = DW_OP_gt;
10560       goto do_binop;
10561
10562     case EQ_EXPR:
10563       op = DW_OP_eq;
10564       goto do_binop;
10565
10566     case NE_EXPR:
10567       op = DW_OP_ne;
10568       goto do_binop;
10569
10570     do_binop:
10571       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10572       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10573       if (ret == 0 || ret1 == 0)
10574         return 0;
10575
10576       add_loc_descr (&ret, ret1);
10577       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10578       break;
10579
10580     case TRUTH_NOT_EXPR:
10581     case BIT_NOT_EXPR:
10582       op = DW_OP_not;
10583       goto do_unop;
10584
10585     case ABS_EXPR:
10586       op = DW_OP_abs;
10587       goto do_unop;
10588
10589     case NEGATE_EXPR:
10590       op = DW_OP_neg;
10591       goto do_unop;
10592
10593     do_unop:
10594       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10595       if (ret == 0)
10596         return 0;
10597
10598       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10599       break;
10600
10601     case MIN_EXPR:
10602     case MAX_EXPR:
10603       {
10604         const enum tree_code code =
10605           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10606
10607         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10608                       build2 (code, integer_type_node,
10609                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10610                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10611       }
10612
10613       /* ... fall through ...  */
10614
10615     case COND_EXPR:
10616       {
10617         dw_loc_descr_ref lhs
10618           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10619         dw_loc_descr_ref rhs
10620           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10621         dw_loc_descr_ref bra_node, jump_node, tmp;
10622
10623         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10624         if (ret == 0 || lhs == 0 || rhs == 0)
10625           return 0;
10626
10627         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10628         add_loc_descr (&ret, bra_node);
10629
10630         add_loc_descr (&ret, rhs);
10631         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10632         add_loc_descr (&ret, jump_node);
10633
10634         add_loc_descr (&ret, lhs);
10635         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10636         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10637
10638         /* ??? Need a node to point the skip at.  Use a nop.  */
10639         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10640         add_loc_descr (&ret, tmp);
10641         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10642         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10643       }
10644       break;
10645
10646     case FIX_TRUNC_EXPR:
10647       return 0;
10648
10649     default:
10650       /* Leave front-end specific codes as simply unknown.  This comes
10651          up, for instance, with the C STMT_EXPR.  */
10652       if ((unsigned int) TREE_CODE (loc)
10653           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10654         return 0;
10655
10656 #ifdef ENABLE_CHECKING
10657       /* Otherwise this is a generic code; we should just lists all of
10658          these explicitly.  We forgot one.  */
10659       gcc_unreachable ();
10660 #else
10661       /* In a release build, we want to degrade gracefully: better to
10662          generate incomplete debugging information than to crash.  */
10663       return NULL;
10664 #endif
10665     }
10666
10667   /* Show if we can't fill the request for an address.  */
10668   if (want_address && !have_address)
10669     return 0;
10670
10671   /* If we've got an address and don't want one, dereference.  */
10672   if (!want_address && have_address && ret)
10673     {
10674       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10675
10676       if (size > DWARF2_ADDR_SIZE || size == -1)
10677         return 0;
10678       else if (size == DWARF2_ADDR_SIZE)
10679         op = DW_OP_deref;
10680       else
10681         op = DW_OP_deref_size;
10682
10683       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10684     }
10685
10686   return ret;
10687 }
10688
10689 static inline dw_loc_descr_ref
10690 loc_descriptor_from_tree (tree loc)
10691 {
10692   return loc_descriptor_from_tree_1 (loc, 2);
10693 }
10694
10695 /* Given a value, round it up to the lowest multiple of `boundary'
10696    which is not less than the value itself.  */
10697
10698 static inline HOST_WIDE_INT
10699 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10700 {
10701   return (((value + boundary - 1) / boundary) * boundary);
10702 }
10703
10704 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10705    pointer to the declared type for the relevant field variable, or return
10706    `integer_type_node' if the given node turns out to be an
10707    ERROR_MARK node.  */
10708
10709 static inline tree
10710 field_type (const_tree decl)
10711 {
10712   tree type;
10713
10714   if (TREE_CODE (decl) == ERROR_MARK)
10715     return integer_type_node;
10716
10717   type = DECL_BIT_FIELD_TYPE (decl);
10718   if (type == NULL_TREE)
10719     type = TREE_TYPE (decl);
10720
10721   return type;
10722 }
10723
10724 /* Given a pointer to a tree node, return the alignment in bits for
10725    it, or else return BITS_PER_WORD if the node actually turns out to
10726    be an ERROR_MARK node.  */
10727
10728 static inline unsigned
10729 simple_type_align_in_bits (const_tree type)
10730 {
10731   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10732 }
10733
10734 static inline unsigned
10735 simple_decl_align_in_bits (const_tree decl)
10736 {
10737   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10738 }
10739
10740 /* Return the result of rounding T up to ALIGN.  */
10741
10742 static inline HOST_WIDE_INT
10743 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10744 {
10745   /* We must be careful if T is negative because HOST_WIDE_INT can be
10746      either "above" or "below" unsigned int as per the C promotion
10747      rules, depending on the host, thus making the signedness of the
10748      direct multiplication and division unpredictable.  */
10749   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10750
10751   u += align - 1;
10752   u /= align;
10753   u *= align;
10754
10755   return (HOST_WIDE_INT) u;
10756 }
10757
10758 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10759    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10760    or return 0 if we are unable to determine what that offset is, either
10761    because the argument turns out to be a pointer to an ERROR_MARK node, or
10762    because the offset is actually variable.  (We can't handle the latter case
10763    just yet).  */
10764
10765 static HOST_WIDE_INT
10766 field_byte_offset (const_tree decl)
10767 {
10768   HOST_WIDE_INT object_offset_in_bits;
10769   HOST_WIDE_INT bitpos_int;
10770
10771   if (TREE_CODE (decl) == ERROR_MARK)
10772     return 0;
10773
10774   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10775
10776   /* We cannot yet cope with fields whose positions are variable, so
10777      for now, when we see such things, we simply return 0.  Someday, we may
10778      be able to handle such cases, but it will be damn difficult.  */
10779   if (! host_integerp (bit_position (decl), 0))
10780     return 0;
10781
10782   bitpos_int = int_bit_position (decl);
10783
10784 #ifdef PCC_BITFIELD_TYPE_MATTERS
10785   if (PCC_BITFIELD_TYPE_MATTERS)
10786     {
10787       tree type;
10788       tree field_size_tree;
10789       HOST_WIDE_INT deepest_bitpos;
10790       unsigned HOST_WIDE_INT field_size_in_bits;
10791       unsigned int type_align_in_bits;
10792       unsigned int decl_align_in_bits;
10793       unsigned HOST_WIDE_INT type_size_in_bits;
10794
10795       type = field_type (decl);
10796       field_size_tree = DECL_SIZE (decl);
10797
10798       /* The size could be unspecified if there was an error, or for
10799          a flexible array member.  */
10800       if (! field_size_tree)
10801         field_size_tree = bitsize_zero_node;
10802
10803       /* If we don't know the size of the field, pretend it's a full word.  */
10804       if (host_integerp (field_size_tree, 1))
10805         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10806       else
10807         field_size_in_bits = BITS_PER_WORD;
10808
10809       type_size_in_bits = simple_type_size_in_bits (type);
10810       type_align_in_bits = simple_type_align_in_bits (type);
10811       decl_align_in_bits = simple_decl_align_in_bits (decl);
10812
10813       /* The GCC front-end doesn't make any attempt to keep track of the
10814          starting bit offset (relative to the start of the containing
10815          structure type) of the hypothetical "containing object" for a
10816          bit-field.  Thus, when computing the byte offset value for the
10817          start of the "containing object" of a bit-field, we must deduce
10818          this information on our own. This can be rather tricky to do in
10819          some cases.  For example, handling the following structure type
10820          definition when compiling for an i386/i486 target (which only
10821          aligns long long's to 32-bit boundaries) can be very tricky:
10822
10823          struct S { int field1; long long field2:31; };
10824
10825          Fortunately, there is a simple rule-of-thumb which can be used
10826          in such cases.  When compiling for an i386/i486, GCC will
10827          allocate 8 bytes for the structure shown above.  It decides to
10828          do this based upon one simple rule for bit-field allocation.
10829          GCC allocates each "containing object" for each bit-field at
10830          the first (i.e. lowest addressed) legitimate alignment boundary
10831          (based upon the required minimum alignment for the declared
10832          type of the field) which it can possibly use, subject to the
10833          condition that there is still enough available space remaining
10834          in the containing object (when allocated at the selected point)
10835          to fully accommodate all of the bits of the bit-field itself.
10836
10837          This simple rule makes it obvious why GCC allocates 8 bytes for
10838          each object of the structure type shown above.  When looking
10839          for a place to allocate the "containing object" for `field2',
10840          the compiler simply tries to allocate a 64-bit "containing
10841          object" at each successive 32-bit boundary (starting at zero)
10842          until it finds a place to allocate that 64- bit field such that
10843          at least 31 contiguous (and previously unallocated) bits remain
10844          within that selected 64 bit field.  (As it turns out, for the
10845          example above, the compiler finds it is OK to allocate the
10846          "containing object" 64-bit field at bit-offset zero within the
10847          structure type.)
10848
10849          Here we attempt to work backwards from the limited set of facts
10850          we're given, and we try to deduce from those facts, where GCC
10851          must have believed that the containing object started (within
10852          the structure type). The value we deduce is then used (by the
10853          callers of this routine) to generate DW_AT_location and
10854          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10855          the case of DW_AT_location, regular fields as well).  */
10856
10857       /* Figure out the bit-distance from the start of the structure to
10858          the "deepest" bit of the bit-field.  */
10859       deepest_bitpos = bitpos_int + field_size_in_bits;
10860
10861       /* This is the tricky part.  Use some fancy footwork to deduce
10862          where the lowest addressed bit of the containing object must
10863          be.  */
10864       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10865
10866       /* Round up to type_align by default.  This works best for
10867          bitfields.  */
10868       object_offset_in_bits
10869         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10870
10871       if (object_offset_in_bits > bitpos_int)
10872         {
10873           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10874
10875           /* Round up to decl_align instead.  */
10876           object_offset_in_bits
10877             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10878         }
10879     }
10880   else
10881 #endif
10882     object_offset_in_bits = bitpos_int;
10883
10884   return object_offset_in_bits / BITS_PER_UNIT;
10885 }
10886 \f
10887 /* The following routines define various Dwarf attributes and any data
10888    associated with them.  */
10889
10890 /* Add a location description attribute value to a DIE.
10891
10892    This emits location attributes suitable for whole variables and
10893    whole parameters.  Note that the location attributes for struct fields are
10894    generated by the routine `data_member_location_attribute' below.  */
10895
10896 static inline void
10897 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10898                              dw_loc_descr_ref descr)
10899 {
10900   if (descr != 0)
10901     add_AT_loc (die, attr_kind, descr);
10902 }
10903
10904 /* Attach the specialized form of location attribute used for data members of
10905    struct and union types.  In the special case of a FIELD_DECL node which
10906    represents a bit-field, the "offset" part of this special location
10907    descriptor must indicate the distance in bytes from the lowest-addressed
10908    byte of the containing struct or union type to the lowest-addressed byte of
10909    the "containing object" for the bit-field.  (See the `field_byte_offset'
10910    function above).
10911
10912    For any given bit-field, the "containing object" is a hypothetical object
10913    (of some integral or enum type) within which the given bit-field lives.  The
10914    type of this hypothetical "containing object" is always the same as the
10915    declared type of the individual bit-field itself (for GCC anyway... the
10916    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10917    bytes) of the hypothetical "containing object" which will be given in the
10918    DW_AT_byte_size attribute for this bit-field.  (See the
10919    `byte_size_attribute' function below.)  It is also used when calculating the
10920    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10921    function below.)  */
10922
10923 static void
10924 add_data_member_location_attribute (dw_die_ref die, tree decl)
10925 {
10926   HOST_WIDE_INT offset;
10927   dw_loc_descr_ref loc_descr = 0;
10928
10929   if (TREE_CODE (decl) == TREE_BINFO)
10930     {
10931       /* We're working on the TAG_inheritance for a base class.  */
10932       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10933         {
10934           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10935              aren't at a fixed offset from all (sub)objects of the same
10936              type.  We need to extract the appropriate offset from our
10937              vtable.  The following dwarf expression means
10938
10939                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10940
10941              This is specific to the V3 ABI, of course.  */
10942
10943           dw_loc_descr_ref tmp;
10944
10945           /* Make a copy of the object address.  */
10946           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10947           add_loc_descr (&loc_descr, tmp);
10948
10949           /* Extract the vtable address.  */
10950           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10951           add_loc_descr (&loc_descr, tmp);
10952
10953           /* Calculate the address of the offset.  */
10954           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10955           gcc_assert (offset < 0);
10956
10957           tmp = int_loc_descriptor (-offset);
10958           add_loc_descr (&loc_descr, tmp);
10959           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10960           add_loc_descr (&loc_descr, tmp);
10961
10962           /* Extract the offset.  */
10963           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10964           add_loc_descr (&loc_descr, tmp);
10965
10966           /* Add it to the object address.  */
10967           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10968           add_loc_descr (&loc_descr, tmp);
10969         }
10970       else
10971         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10972     }
10973   else
10974     offset = field_byte_offset (decl);
10975
10976   if (! loc_descr)
10977     {
10978       enum dwarf_location_atom op;
10979
10980       /* The DWARF2 standard says that we should assume that the structure
10981          address is already on the stack, so we can specify a structure field
10982          address by using DW_OP_plus_uconst.  */
10983
10984 #ifdef MIPS_DEBUGGING_INFO
10985       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10986          operator correctly.  It works only if we leave the offset on the
10987          stack.  */
10988       op = DW_OP_constu;
10989 #else
10990       op = DW_OP_plus_uconst;
10991 #endif
10992
10993       loc_descr = new_loc_descr (op, offset, 0);
10994     }
10995
10996   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10997 }
10998
10999 /* Writes integer values to dw_vec_const array.  */
11000
11001 static void
11002 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11003 {
11004   while (size != 0)
11005     {
11006       *dest++ = val & 0xff;
11007       val >>= 8;
11008       --size;
11009     }
11010 }
11011
11012 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11013
11014 static HOST_WIDE_INT
11015 extract_int (const unsigned char *src, unsigned int size)
11016 {
11017   HOST_WIDE_INT val = 0;
11018
11019   src += size;
11020   while (size != 0)
11021     {
11022       val <<= 8;
11023       val |= *--src & 0xff;
11024       --size;
11025     }
11026   return val;
11027 }
11028
11029 /* Writes floating point values to dw_vec_const array.  */
11030
11031 static void
11032 insert_float (const_rtx rtl, unsigned char *array)
11033 {
11034   REAL_VALUE_TYPE rv;
11035   long val[4];
11036   int i;
11037
11038   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11039   real_to_target (val, &rv, GET_MODE (rtl));
11040
11041   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11042   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11043     {
11044       insert_int (val[i], 4, array);
11045       array += 4;
11046     }
11047 }
11048
11049 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11050    does not have a "location" either in memory or in a register.  These
11051    things can arise in GNU C when a constant is passed as an actual parameter
11052    to an inlined function.  They can also arise in C++ where declared
11053    constants do not necessarily get memory "homes".  */
11054
11055 static void
11056 add_const_value_attribute (dw_die_ref die, rtx rtl)
11057 {
11058   switch (GET_CODE (rtl))
11059     {
11060     case CONST_INT:
11061       {
11062         HOST_WIDE_INT val = INTVAL (rtl);
11063
11064         if (val < 0)
11065           add_AT_int (die, DW_AT_const_value, val);
11066         else
11067           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11068       }
11069       break;
11070
11071     case CONST_DOUBLE:
11072       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11073          floating-point constant.  A CONST_DOUBLE is used whenever the
11074          constant requires more than one word in order to be adequately
11075          represented.  We output CONST_DOUBLEs as blocks.  */
11076       {
11077         enum machine_mode mode = GET_MODE (rtl);
11078
11079         if (SCALAR_FLOAT_MODE_P (mode))
11080           {
11081             unsigned int length = GET_MODE_SIZE (mode);
11082             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11083
11084             insert_float (rtl, array);
11085             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11086           }
11087         else
11088           {
11089             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11090             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11091
11092             add_AT_long_long (die, DW_AT_const_value,
11093                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11094           }
11095       }
11096       break;
11097
11098     case CONST_VECTOR:
11099       {
11100         enum machine_mode mode = GET_MODE (rtl);
11101         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11102         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11103         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11104         unsigned int i;
11105         unsigned char *p;
11106
11107         switch (GET_MODE_CLASS (mode))
11108           {
11109           case MODE_VECTOR_INT:
11110             for (i = 0, p = array; i < length; i++, p += elt_size)
11111               {
11112                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11113                 HOST_WIDE_INT lo, hi;
11114
11115                 switch (GET_CODE (elt))
11116                   {
11117                   case CONST_INT:
11118                     lo = INTVAL (elt);
11119                     hi = -(lo < 0);
11120                     break;
11121
11122                   case CONST_DOUBLE:
11123                     lo = CONST_DOUBLE_LOW (elt);
11124                     hi = CONST_DOUBLE_HIGH (elt);
11125                     break;
11126
11127                   default:
11128                     gcc_unreachable ();
11129                   }
11130
11131                 if (elt_size <= sizeof (HOST_WIDE_INT))
11132                   insert_int (lo, elt_size, p);
11133                 else
11134                   {
11135                     unsigned char *p0 = p;
11136                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11137
11138                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11139                     if (WORDS_BIG_ENDIAN)
11140                       {
11141                         p0 = p1;
11142                         p1 = p;
11143                       }
11144                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11145                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11146                   }
11147               }
11148             break;
11149
11150           case MODE_VECTOR_FLOAT:
11151             for (i = 0, p = array; i < length; i++, p += elt_size)
11152               {
11153                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11154                 insert_float (elt, p);
11155               }
11156             break;
11157
11158           default:
11159             gcc_unreachable ();
11160           }
11161
11162         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11163       }
11164       break;
11165
11166     case CONST_STRING:
11167       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11168       break;
11169
11170     case SYMBOL_REF:
11171     case LABEL_REF:
11172     case CONST:
11173       add_AT_addr (die, DW_AT_const_value, rtl);
11174       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11175       break;
11176
11177     case PLUS:
11178       /* In cases where an inlined instance of an inline function is passed
11179          the address of an `auto' variable (which is local to the caller) we
11180          can get a situation where the DECL_RTL of the artificial local
11181          variable (for the inlining) which acts as a stand-in for the
11182          corresponding formal parameter (of the inline function) will look
11183          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11184          exactly a compile-time constant expression, but it isn't the address
11185          of the (artificial) local variable either.  Rather, it represents the
11186          *value* which the artificial local variable always has during its
11187          lifetime.  We currently have no way to represent such quasi-constant
11188          values in Dwarf, so for now we just punt and generate nothing.  */
11189       break;
11190
11191     default:
11192       /* No other kinds of rtx should be possible here.  */
11193       gcc_unreachable ();
11194     }
11195
11196 }
11197
11198 /* Determine whether the evaluation of EXPR references any variables
11199    or functions which aren't otherwise used (and therefore may not be
11200    output).  */
11201 static tree
11202 reference_to_unused (tree * tp, int * walk_subtrees,
11203                      void * data ATTRIBUTE_UNUSED)
11204 {
11205   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11206     *walk_subtrees = 0;
11207
11208   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11209       && ! TREE_ASM_WRITTEN (*tp))
11210     return *tp;
11211   /* ???  The C++ FE emits debug information for using decls, so
11212      putting gcc_unreachable here falls over.  See PR31899.  For now
11213      be conservative.  */
11214   else if (!cgraph_global_info_ready
11215            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11216     return *tp;
11217   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11218     {
11219       struct varpool_node *node = varpool_node (*tp);
11220       if (!node->needed)
11221         return *tp;
11222     }
11223   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11224            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11225     {
11226       struct cgraph_node *node = cgraph_node (*tp);
11227       if (!node->output)
11228         return *tp;
11229     }
11230   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11231     return *tp;
11232
11233   return NULL_TREE;
11234 }
11235
11236 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11237    for use in a later add_const_value_attribute call.  */
11238
11239 static rtx
11240 rtl_for_decl_init (tree init, tree type)
11241 {
11242   rtx rtl = NULL_RTX;
11243
11244   /* If a variable is initialized with a string constant without embedded
11245      zeros, build CONST_STRING.  */
11246   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11247     {
11248       tree enttype = TREE_TYPE (type);
11249       tree domain = TYPE_DOMAIN (type);
11250       enum machine_mode mode = TYPE_MODE (enttype);
11251
11252       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11253           && domain
11254           && integer_zerop (TYPE_MIN_VALUE (domain))
11255           && compare_tree_int (TYPE_MAX_VALUE (domain),
11256                                TREE_STRING_LENGTH (init) - 1) == 0
11257           && ((size_t) TREE_STRING_LENGTH (init)
11258               == strlen (TREE_STRING_POINTER (init)) + 1))
11259         rtl = gen_rtx_CONST_STRING (VOIDmode,
11260                                     ggc_strdup (TREE_STRING_POINTER (init)));
11261     }
11262   /* Other aggregates, and complex values, could be represented using
11263      CONCAT: FIXME!  */
11264   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11265     ;
11266   /* Vectors only work if their mode is supported by the target.
11267      FIXME: generic vectors ought to work too.  */
11268   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11269     ;
11270   /* If the initializer is something that we know will expand into an
11271      immediate RTL constant, expand it now.  We must be careful not to
11272      reference variables which won't be output.  */
11273   else if (initializer_constant_valid_p (init, type)
11274            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11275     {
11276       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11277          possible.  */
11278       if (TREE_CODE (type) == VECTOR_TYPE)
11279         switch (TREE_CODE (init))
11280           {
11281           case VECTOR_CST:
11282             break;
11283           case CONSTRUCTOR:
11284             if (TREE_CONSTANT (init))
11285               {
11286                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11287                 bool constant_p = true;
11288                 tree value;
11289                 unsigned HOST_WIDE_INT ix;
11290
11291                 /* Even when ctor is constant, it might contain non-*_CST
11292                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11293                    belong into VECTOR_CST nodes.  */
11294                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11295                   if (!CONSTANT_CLASS_P (value))
11296                     {
11297                       constant_p = false;
11298                       break;
11299                     }
11300
11301                 if (constant_p)
11302                   {
11303                     init = build_vector_from_ctor (type, elts);
11304                     break;
11305                   }
11306               }
11307             /* FALLTHRU */
11308
11309           default:
11310             return NULL;
11311           }
11312
11313       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11314
11315       /* If expand_expr returns a MEM, it wasn't immediate.  */
11316       gcc_assert (!rtl || !MEM_P (rtl));
11317     }
11318
11319   return rtl;
11320 }
11321
11322 /* Generate RTL for the variable DECL to represent its location.  */
11323
11324 static rtx
11325 rtl_for_decl_location (tree decl)
11326 {
11327   rtx rtl;
11328
11329   /* Here we have to decide where we are going to say the parameter "lives"
11330      (as far as the debugger is concerned).  We only have a couple of
11331      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11332
11333      DECL_RTL normally indicates where the parameter lives during most of the
11334      activation of the function.  If optimization is enabled however, this
11335      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11336      that the parameter doesn't really live anywhere (as far as the code
11337      generation parts of GCC are concerned) during most of the function's
11338      activation.  That will happen (for example) if the parameter is never
11339      referenced within the function.
11340
11341      We could just generate a location descriptor here for all non-NULL
11342      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11343      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11344      where DECL_RTL is NULL or is a pseudo-reg.
11345
11346      Note however that we can only get away with using DECL_INCOMING_RTL as
11347      a backup substitute for DECL_RTL in certain limited cases.  In cases
11348      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11349      we can be sure that the parameter was passed using the same type as it is
11350      declared to have within the function, and that its DECL_INCOMING_RTL
11351      points us to a place where a value of that type is passed.
11352
11353      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11354      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11355      because in these cases DECL_INCOMING_RTL points us to a value of some
11356      type which is *different* from the type of the parameter itself.  Thus,
11357      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11358      such cases, the debugger would end up (for example) trying to fetch a
11359      `float' from a place which actually contains the first part of a
11360      `double'.  That would lead to really incorrect and confusing
11361      output at debug-time.
11362
11363      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11364      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11365      are a couple of exceptions however.  On little-endian machines we can
11366      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11367      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11368      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11369      when (on a little-endian machine) a non-prototyped function has a
11370      parameter declared to be of type `short' or `char'.  In such cases,
11371      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11372      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11373      passed `int' value.  If the debugger then uses that address to fetch
11374      a `short' or a `char' (on a little-endian machine) the result will be
11375      the correct data, so we allow for such exceptional cases below.
11376
11377      Note that our goal here is to describe the place where the given formal
11378      parameter lives during most of the function's activation (i.e. between the
11379      end of the prologue and the start of the epilogue).  We'll do that as best
11380      as we can. Note however that if the given formal parameter is modified
11381      sometime during the execution of the function, then a stack backtrace (at
11382      debug-time) will show the function as having been called with the *new*
11383      value rather than the value which was originally passed in.  This happens
11384      rarely enough that it is not a major problem, but it *is* a problem, and
11385      I'd like to fix it.
11386
11387      A future version of dwarf2out.c may generate two additional attributes for
11388      any given DW_TAG_formal_parameter DIE which will describe the "passed
11389      type" and the "passed location" for the given formal parameter in addition
11390      to the attributes we now generate to indicate the "declared type" and the
11391      "active location" for each parameter.  This additional set of attributes
11392      could be used by debuggers for stack backtraces. Separately, note that
11393      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11394      This happens (for example) for inlined-instances of inline function formal
11395      parameters which are never referenced.  This really shouldn't be
11396      happening.  All PARM_DECL nodes should get valid non-NULL
11397      DECL_INCOMING_RTL values.  FIXME.  */
11398
11399   /* Use DECL_RTL as the "location" unless we find something better.  */
11400   rtl = DECL_RTL_IF_SET (decl);
11401
11402   /* When generating abstract instances, ignore everything except
11403      constants, symbols living in memory, and symbols living in
11404      fixed registers.  */
11405   if (! reload_completed)
11406     {
11407       if (rtl
11408           && (CONSTANT_P (rtl)
11409               || (MEM_P (rtl)
11410                   && CONSTANT_P (XEXP (rtl, 0)))
11411               || (REG_P (rtl)
11412                   && TREE_CODE (decl) == VAR_DECL
11413                   && TREE_STATIC (decl))))
11414         {
11415           rtl = targetm.delegitimize_address (rtl);
11416           return rtl;
11417         }
11418       rtl = NULL_RTX;
11419     }
11420   else if (TREE_CODE (decl) == PARM_DECL)
11421     {
11422       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11423         {
11424           tree declared_type = TREE_TYPE (decl);
11425           tree passed_type = DECL_ARG_TYPE (decl);
11426           enum machine_mode dmode = TYPE_MODE (declared_type);
11427           enum machine_mode pmode = TYPE_MODE (passed_type);
11428
11429           /* This decl represents a formal parameter which was optimized out.
11430              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11431              all cases where (rtl == NULL_RTX) just below.  */
11432           if (dmode == pmode)
11433             rtl = DECL_INCOMING_RTL (decl);
11434           else if (SCALAR_INT_MODE_P (dmode)
11435                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11436                    && DECL_INCOMING_RTL (decl))
11437             {
11438               rtx inc = DECL_INCOMING_RTL (decl);
11439               if (REG_P (inc))
11440                 rtl = inc;
11441               else if (MEM_P (inc))
11442                 {
11443                   if (BYTES_BIG_ENDIAN)
11444                     rtl = adjust_address_nv (inc, dmode,
11445                                              GET_MODE_SIZE (pmode)
11446                                              - GET_MODE_SIZE (dmode));
11447                   else
11448                     rtl = inc;
11449                 }
11450             }
11451         }
11452
11453       /* If the parm was passed in registers, but lives on the stack, then
11454          make a big endian correction if the mode of the type of the
11455          parameter is not the same as the mode of the rtl.  */
11456       /* ??? This is the same series of checks that are made in dbxout.c before
11457          we reach the big endian correction code there.  It isn't clear if all
11458          of these checks are necessary here, but keeping them all is the safe
11459          thing to do.  */
11460       else if (MEM_P (rtl)
11461                && XEXP (rtl, 0) != const0_rtx
11462                && ! CONSTANT_P (XEXP (rtl, 0))
11463                /* Not passed in memory.  */
11464                && !MEM_P (DECL_INCOMING_RTL (decl))
11465                /* Not passed by invisible reference.  */
11466                && (!REG_P (XEXP (rtl, 0))
11467                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11468                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11469 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11470                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11471 #endif
11472                      )
11473                /* Big endian correction check.  */
11474                && BYTES_BIG_ENDIAN
11475                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11476                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11477                    < UNITS_PER_WORD))
11478         {
11479           int offset = (UNITS_PER_WORD
11480                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11481
11482           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11483                              plus_constant (XEXP (rtl, 0), offset));
11484         }
11485     }
11486   else if (TREE_CODE (decl) == VAR_DECL
11487            && rtl
11488            && MEM_P (rtl)
11489            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11490            && BYTES_BIG_ENDIAN)
11491     {
11492       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11493       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11494
11495       /* If a variable is declared "register" yet is smaller than
11496          a register, then if we store the variable to memory, it
11497          looks like we're storing a register-sized value, when in
11498          fact we are not.  We need to adjust the offset of the
11499          storage location to reflect the actual value's bytes,
11500          else gdb will not be able to display it.  */
11501       if (rsize > dsize)
11502         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11503                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11504     }
11505
11506   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11507      and will have been substituted directly into all expressions that use it.
11508      C does not have such a concept, but C++ and other languages do.  */
11509   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11510     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11511
11512   if (rtl)
11513     rtl = targetm.delegitimize_address (rtl);
11514
11515   /* If we don't look past the constant pool, we risk emitting a
11516      reference to a constant pool entry that isn't referenced from
11517      code, and thus is not emitted.  */
11518   if (rtl)
11519     rtl = avoid_constant_pool_reference (rtl);
11520
11521   return rtl;
11522 }
11523
11524 /* We need to figure out what section we should use as the base for the
11525    address ranges where a given location is valid.
11526    1. If this particular DECL has a section associated with it, use that.
11527    2. If this function has a section associated with it, use that.
11528    3. Otherwise, use the text section.
11529    XXX: If you split a variable across multiple sections, we won't notice.  */
11530
11531 static const char *
11532 secname_for_decl (const_tree decl)
11533 {
11534   const char *secname;
11535
11536   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11537     {
11538       tree sectree = DECL_SECTION_NAME (decl);
11539       secname = TREE_STRING_POINTER (sectree);
11540     }
11541   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11542     {
11543       tree sectree = DECL_SECTION_NAME (current_function_decl);
11544       secname = TREE_STRING_POINTER (sectree);
11545     }
11546   else if (cfun && in_cold_section_p)
11547     secname = crtl->subsections.cold_section_label;
11548   else
11549     secname = text_section_label;
11550
11551   return secname;
11552 }
11553
11554 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_RTX is returned.
11555    If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
11556    value is the offset into the common block for the symbol.  */
11557
11558 static tree
11559 fortran_common (tree decl, HOST_WIDE_INT *value)
11560 {
11561   tree val_expr, cvar;
11562   enum machine_mode mode;
11563   HOST_WIDE_INT bitsize, bitpos;
11564   tree offset;
11565   int volatilep = 0, unsignedp = 0;
11566
11567   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11568      it does not have a value (the offset into the common area), or if it
11569      is thread local (as opposed to global) then it isn't common, and shouldn't
11570      be handled as such.  */
11571   if (TREE_CODE (decl) != VAR_DECL
11572       || !TREE_PUBLIC (decl)
11573       || !TREE_STATIC (decl)
11574       || !DECL_HAS_VALUE_EXPR_P (decl)
11575       || !is_fortran ())
11576     return NULL_TREE;
11577
11578   val_expr = DECL_VALUE_EXPR (decl);
11579   if (TREE_CODE (val_expr) != COMPONENT_REF)
11580     return NULL_TREE;
11581
11582   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11583                               &mode, &unsignedp, &volatilep, true);
11584
11585   if (cvar == NULL_TREE
11586       || TREE_CODE (cvar) != VAR_DECL
11587       || DECL_ARTIFICIAL (cvar)
11588       || !TREE_PUBLIC (cvar))
11589     return NULL_TREE;
11590
11591   *value = 0;
11592   if (offset != NULL)
11593     {
11594       if (!host_integerp (offset, 0))
11595         return NULL_TREE;
11596       *value = tree_low_cst (offset, 0);
11597     }
11598   if (bitpos != 0)
11599     *value += bitpos / BITS_PER_UNIT;
11600
11601   return cvar;
11602 }
11603
11604
11605 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11606    data attribute for a variable or a parameter.  We generate the
11607    DW_AT_const_value attribute only in those cases where the given variable
11608    or parameter does not have a true "location" either in memory or in a
11609    register.  This can happen (for example) when a constant is passed as an
11610    actual argument in a call to an inline function.  (It's possible that
11611    these things can crop up in other ways also.)  Note that one type of
11612    constant value which can be passed into an inlined function is a constant
11613    pointer.  This can happen for example if an actual argument in an inlined
11614    function call evaluates to a compile-time constant address.  */
11615
11616 static void
11617 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11618                                        enum dwarf_attribute attr)
11619 {
11620   rtx rtl;
11621   dw_loc_descr_ref descr;
11622   var_loc_list *loc_list;
11623   struct var_loc_node *node;
11624   if (TREE_CODE (decl) == ERROR_MARK)
11625     return;
11626
11627   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11628               || TREE_CODE (decl) == RESULT_DECL);
11629
11630   /* See if we possibly have multiple locations for this variable.  */
11631   loc_list = lookup_decl_loc (decl);
11632
11633   /* If it truly has multiple locations, the first and last node will
11634      differ.  */
11635   if (loc_list && loc_list->first != loc_list->last)
11636     {
11637       const char *endname, *secname;
11638       dw_loc_list_ref list;
11639       rtx varloc;
11640       enum var_init_status initialized;
11641
11642       /* Now that we know what section we are using for a base,
11643          actually construct the list of locations.
11644          The first location information is what is passed to the
11645          function that creates the location list, and the remaining
11646          locations just get added on to that list.
11647          Note that we only know the start address for a location
11648          (IE location changes), so to build the range, we use
11649          the range [current location start, next location start].
11650          This means we have to special case the last node, and generate
11651          a range of [last location start, end of function label].  */
11652
11653       node = loc_list->first;
11654       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11655       secname = secname_for_decl (decl);
11656
11657       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11658         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11659       else
11660         initialized = VAR_INIT_STATUS_INITIALIZED;
11661
11662       list = new_loc_list (loc_descriptor (varloc, initialized),
11663                            node->label, node->next->label, secname, 1);
11664       node = node->next;
11665
11666       for (; node->next; node = node->next)
11667         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11668           {
11669             /* The variable has a location between NODE->LABEL and
11670                NODE->NEXT->LABEL.  */
11671             enum var_init_status initialized =
11672               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11673             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11674             add_loc_descr_to_loc_list (&list,
11675                                        loc_descriptor (varloc, initialized),
11676                                        node->label, node->next->label, secname);
11677           }
11678
11679       /* If the variable has a location at the last label
11680          it keeps its location until the end of function.  */
11681       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11682         {
11683           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11684           enum var_init_status initialized =
11685             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11686
11687           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11688           if (!current_function_decl)
11689             endname = text_end_label;
11690           else
11691             {
11692               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11693                                            current_function_funcdef_no);
11694               endname = ggc_strdup (label_id);
11695             }
11696           add_loc_descr_to_loc_list (&list,
11697                                      loc_descriptor (varloc, initialized),
11698                                      node->label, endname, secname);
11699         }
11700
11701       /* Finally, add the location list to the DIE, and we are done.  */
11702       add_AT_loc_list (die, attr, list);
11703       return;
11704     }
11705
11706   /* Try to get some constant RTL for this decl, and use that as the value of
11707      the location.  */
11708
11709   rtl = rtl_for_decl_location (decl);
11710   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11711     {
11712       add_const_value_attribute (die, rtl);
11713       return;
11714     }
11715
11716   /* If we have tried to generate the location otherwise, and it
11717      didn't work out (we wouldn't be here if we did), and we have a one entry
11718      location list, try generating a location from that.  */
11719   if (loc_list && loc_list->first)
11720     {
11721       enum var_init_status status;
11722       node = loc_list->first;
11723       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11724       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11725       if (descr)
11726         {
11727           add_AT_location_description (die, attr, descr);
11728           return;
11729         }
11730     }
11731
11732   /* We couldn't get any rtl, so try directly generating the location
11733      description from the tree.  */
11734   descr = loc_descriptor_from_tree (decl);
11735   if (descr)
11736     {
11737       add_AT_location_description (die, attr, descr);
11738       return;
11739     }
11740   /* None of that worked, so it must not really have a location;
11741      try adding a constant value attribute from the DECL_INITIAL.  */
11742   tree_add_const_value_attribute (die, decl);
11743 }
11744
11745 /* If we don't have a copy of this variable in memory for some reason (such
11746    as a C++ member constant that doesn't have an out-of-line definition),
11747    we should tell the debugger about the constant value.  */
11748
11749 static void
11750 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
11751 {
11752   tree init = DECL_INITIAL (decl);
11753   tree type = TREE_TYPE (decl);
11754   rtx rtl;
11755
11756   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
11757     /* OK */;
11758   else
11759     return;
11760
11761   rtl = rtl_for_decl_init (init, type);
11762   if (rtl)
11763     add_const_value_attribute (var_die, rtl);
11764 }
11765
11766 /* Convert the CFI instructions for the current function into a
11767    location list.  This is used for DW_AT_frame_base when we targeting
11768    a dwarf2 consumer that does not support the dwarf3
11769    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
11770    expressions.  */
11771
11772 static dw_loc_list_ref
11773 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
11774 {
11775   dw_fde_ref fde;
11776   dw_loc_list_ref list, *list_tail;
11777   dw_cfi_ref cfi;
11778   dw_cfa_location last_cfa, next_cfa;
11779   const char *start_label, *last_label, *section;
11780
11781   fde = current_fde ();
11782   gcc_assert (fde != NULL);
11783
11784   section = secname_for_decl (current_function_decl);
11785   list_tail = &list;
11786   list = NULL;
11787
11788   next_cfa.reg = INVALID_REGNUM;
11789   next_cfa.offset = 0;
11790   next_cfa.indirect = 0;
11791   next_cfa.base_offset = 0;
11792
11793   start_label = fde->dw_fde_begin;
11794
11795   /* ??? Bald assumption that the CIE opcode list does not contain
11796      advance opcodes.  */
11797   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11798     lookup_cfa_1 (cfi, &next_cfa);
11799
11800   last_cfa = next_cfa;
11801   last_label = start_label;
11802
11803   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11804     switch (cfi->dw_cfi_opc)
11805       {
11806       case DW_CFA_set_loc:
11807       case DW_CFA_advance_loc1:
11808       case DW_CFA_advance_loc2:
11809       case DW_CFA_advance_loc4:
11810         if (!cfa_equal_p (&last_cfa, &next_cfa))
11811           {
11812             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11813                                        start_label, last_label, section,
11814                                        list == NULL);
11815
11816             list_tail = &(*list_tail)->dw_loc_next;
11817             last_cfa = next_cfa;
11818             start_label = last_label;
11819           }
11820         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11821         break;
11822
11823       case DW_CFA_advance_loc:
11824         /* The encoding is complex enough that we should never emit this.  */
11825       case DW_CFA_remember_state:
11826       case DW_CFA_restore_state:
11827         /* We don't handle these two in this function.  It would be possible
11828            if it were to be required.  */
11829         gcc_unreachable ();
11830
11831       default:
11832         lookup_cfa_1 (cfi, &next_cfa);
11833         break;
11834       }
11835
11836   if (!cfa_equal_p (&last_cfa, &next_cfa))
11837     {
11838       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11839                                  start_label, last_label, section,
11840                                  list == NULL);
11841       list_tail = &(*list_tail)->dw_loc_next;
11842       start_label = last_label;
11843     }
11844   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11845                              start_label, fde->dw_fde_end, section,
11846                              list == NULL);
11847
11848   return list;
11849 }
11850
11851 /* Compute a displacement from the "steady-state frame pointer" to the
11852    frame base (often the same as the CFA), and store it in
11853    frame_pointer_fb_offset.  OFFSET is added to the displacement
11854    before the latter is negated.  */
11855
11856 static void
11857 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11858 {
11859   rtx reg, elim;
11860
11861 #ifdef FRAME_POINTER_CFA_OFFSET
11862   reg = frame_pointer_rtx;
11863   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11864 #else
11865   reg = arg_pointer_rtx;
11866   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11867 #endif
11868
11869   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11870   if (GET_CODE (elim) == PLUS)
11871     {
11872       offset += INTVAL (XEXP (elim, 1));
11873       elim = XEXP (elim, 0);
11874     }
11875
11876   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11877                && (elim == hard_frame_pointer_rtx
11878                    || elim == stack_pointer_rtx))
11879               || elim == (frame_pointer_needed
11880                           ? hard_frame_pointer_rtx
11881                           : stack_pointer_rtx));
11882
11883   frame_pointer_fb_offset = -offset;
11884 }
11885
11886 /* Generate a DW_AT_name attribute given some string value to be included as
11887    the value of the attribute.  */
11888
11889 static void
11890 add_name_attribute (dw_die_ref die, const char *name_string)
11891 {
11892   if (name_string != NULL && *name_string != 0)
11893     {
11894       if (demangle_name_func)
11895         name_string = (*demangle_name_func) (name_string);
11896
11897       add_AT_string (die, DW_AT_name, name_string);
11898     }
11899 }
11900
11901 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11902
11903 static void
11904 add_comp_dir_attribute (dw_die_ref die)
11905 {
11906   const char *wd = get_src_pwd ();
11907   if (wd != NULL)
11908     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11909 }
11910
11911 /* Given a tree node describing an array bound (either lower or upper) output
11912    a representation for that bound.  */
11913
11914 static void
11915 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11916 {
11917   switch (TREE_CODE (bound))
11918     {
11919     case ERROR_MARK:
11920       return;
11921
11922     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11923     case INTEGER_CST:
11924       if (! host_integerp (bound, 0)
11925           || (bound_attr == DW_AT_lower_bound
11926               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11927                   || (is_fortran () && integer_onep (bound)))))
11928         /* Use the default.  */
11929         ;
11930       else
11931         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11932       break;
11933
11934     CASE_CONVERT:
11935     case VIEW_CONVERT_EXPR:
11936       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11937       break;
11938
11939     case SAVE_EXPR:
11940       break;
11941
11942     case VAR_DECL:
11943     case PARM_DECL:
11944     case RESULT_DECL:
11945       {
11946         dw_die_ref decl_die = lookup_decl_die (bound);
11947
11948         /* ??? Can this happen, or should the variable have been bound
11949            first?  Probably it can, since I imagine that we try to create
11950            the types of parameters in the order in which they exist in
11951            the list, and won't have created a forward reference to a
11952            later parameter.  */
11953         if (decl_die != NULL)
11954           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11955         break;
11956       }
11957
11958     default:
11959       {
11960         /* Otherwise try to create a stack operation procedure to
11961            evaluate the value of the array bound.  */
11962
11963         dw_die_ref ctx, decl_die;
11964         dw_loc_descr_ref loc;
11965
11966         loc = loc_descriptor_from_tree (bound);
11967         if (loc == NULL)
11968           break;
11969
11970         if (current_function_decl == 0)
11971           ctx = comp_unit_die;
11972         else
11973           ctx = lookup_decl_die (current_function_decl);
11974
11975         decl_die = new_die (DW_TAG_variable, ctx, bound);
11976         add_AT_flag (decl_die, DW_AT_artificial, 1);
11977         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11978         add_AT_loc (decl_die, DW_AT_location, loc);
11979
11980         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11981         break;
11982       }
11983     }
11984 }
11985
11986 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
11987    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
11988    Note that the block of subscript information for an array type also
11989    includes information about the element type of the given array type.  */
11990
11991 static void
11992 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
11993 {
11994   unsigned dimension_number;
11995   tree lower, upper;
11996   dw_die_ref subrange_die;
11997
11998   for (dimension_number = 0;
11999        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12000        type = TREE_TYPE (type), dimension_number++)
12001     {
12002       tree domain = TYPE_DOMAIN (type);
12003
12004       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12005          and (in GNU C only) variable bounds.  Handle all three forms
12006          here.  */
12007       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12008       if (domain)
12009         {
12010           /* We have an array type with specified bounds.  */
12011           lower = TYPE_MIN_VALUE (domain);
12012           upper = TYPE_MAX_VALUE (domain);
12013
12014           /* Define the index type.  */
12015           if (TREE_TYPE (domain))
12016             {
12017               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12018                  TREE_TYPE field.  We can't emit debug info for this
12019                  because it is an unnamed integral type.  */
12020               if (TREE_CODE (domain) == INTEGER_TYPE
12021                   && TYPE_NAME (domain) == NULL_TREE
12022                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12023                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12024                 ;
12025               else
12026                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12027                                     type_die);
12028             }
12029
12030           /* ??? If upper is NULL, the array has unspecified length,
12031              but it does have a lower bound.  This happens with Fortran
12032                dimension arr(N:*)
12033              Since the debugger is definitely going to need to know N
12034              to produce useful results, go ahead and output the lower
12035              bound solo, and hope the debugger can cope.  */
12036
12037           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12038           if (upper)
12039             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12040         }
12041
12042       /* Otherwise we have an array type with an unspecified length.  The
12043          DWARF-2 spec does not say how to handle this; let's just leave out the
12044          bounds.  */
12045     }
12046 }
12047
12048 static void
12049 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12050 {
12051   unsigned size;
12052
12053   switch (TREE_CODE (tree_node))
12054     {
12055     case ERROR_MARK:
12056       size = 0;
12057       break;
12058     case ENUMERAL_TYPE:
12059     case RECORD_TYPE:
12060     case UNION_TYPE:
12061     case QUAL_UNION_TYPE:
12062       size = int_size_in_bytes (tree_node);
12063       break;
12064     case FIELD_DECL:
12065       /* For a data member of a struct or union, the DW_AT_byte_size is
12066          generally given as the number of bytes normally allocated for an
12067          object of the *declared* type of the member itself.  This is true
12068          even for bit-fields.  */
12069       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12070       break;
12071     default:
12072       gcc_unreachable ();
12073     }
12074
12075   /* Note that `size' might be -1 when we get to this point.  If it is, that
12076      indicates that the byte size of the entity in question is variable.  We
12077      have no good way of expressing this fact in Dwarf at the present time,
12078      so just let the -1 pass on through.  */
12079   add_AT_unsigned (die, DW_AT_byte_size, size);
12080 }
12081
12082 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12083    which specifies the distance in bits from the highest order bit of the
12084    "containing object" for the bit-field to the highest order bit of the
12085    bit-field itself.
12086
12087    For any given bit-field, the "containing object" is a hypothetical object
12088    (of some integral or enum type) within which the given bit-field lives.  The
12089    type of this hypothetical "containing object" is always the same as the
12090    declared type of the individual bit-field itself.  The determination of the
12091    exact location of the "containing object" for a bit-field is rather
12092    complicated.  It's handled by the `field_byte_offset' function (above).
12093
12094    Note that it is the size (in bytes) of the hypothetical "containing object"
12095    which will be given in the DW_AT_byte_size attribute for this bit-field.
12096    (See `byte_size_attribute' above).  */
12097
12098 static inline void
12099 add_bit_offset_attribute (dw_die_ref die, tree decl)
12100 {
12101   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12102   tree type = DECL_BIT_FIELD_TYPE (decl);
12103   HOST_WIDE_INT bitpos_int;
12104   HOST_WIDE_INT highest_order_object_bit_offset;
12105   HOST_WIDE_INT highest_order_field_bit_offset;
12106   HOST_WIDE_INT unsigned bit_offset;
12107
12108   /* Must be a field and a bit field.  */
12109   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12110
12111   /* We can't yet handle bit-fields whose offsets are variable, so if we
12112      encounter such things, just return without generating any attribute
12113      whatsoever.  Likewise for variable or too large size.  */
12114   if (! host_integerp (bit_position (decl), 0)
12115       || ! host_integerp (DECL_SIZE (decl), 1))
12116     return;
12117
12118   bitpos_int = int_bit_position (decl);
12119
12120   /* Note that the bit offset is always the distance (in bits) from the
12121      highest-order bit of the "containing object" to the highest-order bit of
12122      the bit-field itself.  Since the "high-order end" of any object or field
12123      is different on big-endian and little-endian machines, the computation
12124      below must take account of these differences.  */
12125   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12126   highest_order_field_bit_offset = bitpos_int;
12127
12128   if (! BYTES_BIG_ENDIAN)
12129     {
12130       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12131       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12132     }
12133
12134   bit_offset
12135     = (! BYTES_BIG_ENDIAN
12136        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12137        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12138
12139   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12140 }
12141
12142 /* For a FIELD_DECL node which represents a bit field, output an attribute
12143    which specifies the length in bits of the given field.  */
12144
12145 static inline void
12146 add_bit_size_attribute (dw_die_ref die, tree decl)
12147 {
12148   /* Must be a field and a bit field.  */
12149   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12150               && DECL_BIT_FIELD_TYPE (decl));
12151
12152   if (host_integerp (DECL_SIZE (decl), 1))
12153     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12154 }
12155
12156 /* If the compiled language is ANSI C, then add a 'prototyped'
12157    attribute, if arg types are given for the parameters of a function.  */
12158
12159 static inline void
12160 add_prototyped_attribute (dw_die_ref die, tree func_type)
12161 {
12162   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12163       && TYPE_ARG_TYPES (func_type) != NULL)
12164     add_AT_flag (die, DW_AT_prototyped, 1);
12165 }
12166
12167 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12168    by looking in either the type declaration or object declaration
12169    equate table.  */
12170
12171 static inline void
12172 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12173 {
12174   dw_die_ref origin_die = NULL;
12175
12176   if (TREE_CODE (origin) != FUNCTION_DECL)
12177     {
12178       /* We may have gotten separated from the block for the inlined
12179          function, if we're in an exception handler or some such; make
12180          sure that the abstract function has been written out.
12181
12182          Doing this for nested functions is wrong, however; functions are
12183          distinct units, and our context might not even be inline.  */
12184       tree fn = origin;
12185
12186       if (TYPE_P (fn))
12187         fn = TYPE_STUB_DECL (fn);
12188
12189       fn = decl_function_context (fn);
12190       if (fn)
12191         dwarf2out_abstract_function (fn);
12192     }
12193
12194   if (DECL_P (origin))
12195     origin_die = lookup_decl_die (origin);
12196   else if (TYPE_P (origin))
12197     origin_die = lookup_type_die (origin);
12198
12199   /* XXX: Functions that are never lowered don't always have correct block
12200      trees (in the case of java, they simply have no block tree, in some other
12201      languages).  For these functions, there is nothing we can really do to
12202      output correct debug info for inlined functions in all cases.  Rather
12203      than die, we'll just produce deficient debug info now, in that we will
12204      have variables without a proper abstract origin.  In the future, when all
12205      functions are lowered, we should re-add a gcc_assert (origin_die)
12206      here.  */
12207
12208   if (origin_die)
12209       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12210 }
12211
12212 /* We do not currently support the pure_virtual attribute.  */
12213
12214 static inline void
12215 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12216 {
12217   if (DECL_VINDEX (func_decl))
12218     {
12219       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12220
12221       if (host_integerp (DECL_VINDEX (func_decl), 0))
12222         add_AT_loc (die, DW_AT_vtable_elem_location,
12223                     new_loc_descr (DW_OP_constu,
12224                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12225                                    0));
12226
12227       /* GNU extension: Record what type this method came from originally.  */
12228       if (debug_info_level > DINFO_LEVEL_TERSE)
12229         add_AT_die_ref (die, DW_AT_containing_type,
12230                         lookup_type_die (DECL_CONTEXT (func_decl)));
12231     }
12232 }
12233 \f
12234 /* Add source coordinate attributes for the given decl.  */
12235
12236 static void
12237 add_src_coords_attributes (dw_die_ref die, tree decl)
12238 {
12239   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12240
12241   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12242   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12243 }
12244
12245 /* Add a DW_AT_name attribute and source coordinate attribute for the
12246    given decl, but only if it actually has a name.  */
12247
12248 static void
12249 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12250 {
12251   tree decl_name;
12252
12253   decl_name = DECL_NAME (decl);
12254   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12255     {
12256       add_name_attribute (die, dwarf2_name (decl, 0));
12257       if (! DECL_ARTIFICIAL (decl))
12258         add_src_coords_attributes (die, decl);
12259
12260       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12261           && TREE_PUBLIC (decl)
12262           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12263           && !DECL_ABSTRACT (decl)
12264           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12265           && !is_fortran ())
12266         add_AT_string (die, DW_AT_MIPS_linkage_name,
12267                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12268     }
12269
12270 #ifdef VMS_DEBUGGING_INFO
12271   /* Get the function's name, as described by its RTL.  This may be different
12272      from the DECL_NAME name used in the source file.  */
12273   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12274     {
12275       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12276                    XEXP (DECL_RTL (decl), 0));
12277       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12278     }
12279 #endif
12280 }
12281
12282 /* Push a new declaration scope.  */
12283
12284 static void
12285 push_decl_scope (tree scope)
12286 {
12287   VEC_safe_push (tree, gc, decl_scope_table, scope);
12288 }
12289
12290 /* Pop a declaration scope.  */
12291
12292 static inline void
12293 pop_decl_scope (void)
12294 {
12295   VEC_pop (tree, decl_scope_table);
12296 }
12297
12298 /* Return the DIE for the scope that immediately contains this type.
12299    Non-named types get global scope.  Named types nested in other
12300    types get their containing scope if it's open, or global scope
12301    otherwise.  All other types (i.e. function-local named types) get
12302    the current active scope.  */
12303
12304 static dw_die_ref
12305 scope_die_for (tree t, dw_die_ref context_die)
12306 {
12307   dw_die_ref scope_die = NULL;
12308   tree containing_scope;
12309   int i;
12310
12311   /* Non-types always go in the current scope.  */
12312   gcc_assert (TYPE_P (t));
12313
12314   containing_scope = TYPE_CONTEXT (t);
12315
12316   /* Use the containing namespace if it was passed in (for a declaration).  */
12317   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12318     {
12319       if (context_die == lookup_decl_die (containing_scope))
12320         /* OK */;
12321       else
12322         containing_scope = NULL_TREE;
12323     }
12324
12325   /* Ignore function type "scopes" from the C frontend.  They mean that
12326      a tagged type is local to a parmlist of a function declarator, but
12327      that isn't useful to DWARF.  */
12328   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12329     containing_scope = NULL_TREE;
12330
12331   if (containing_scope == NULL_TREE)
12332     scope_die = comp_unit_die;
12333   else if (TYPE_P (containing_scope))
12334     {
12335       /* For types, we can just look up the appropriate DIE.  But
12336          first we check to see if we're in the middle of emitting it
12337          so we know where the new DIE should go.  */
12338       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12339         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12340           break;
12341
12342       if (i < 0)
12343         {
12344           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12345                       || TREE_ASM_WRITTEN (containing_scope));
12346
12347           /* If none of the current dies are suitable, we get file scope.  */
12348           scope_die = comp_unit_die;
12349         }
12350       else
12351         scope_die = lookup_type_die (containing_scope);
12352     }
12353   else
12354     scope_die = context_die;
12355
12356   return scope_die;
12357 }
12358
12359 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12360
12361 static inline int
12362 local_scope_p (dw_die_ref context_die)
12363 {
12364   for (; context_die; context_die = context_die->die_parent)
12365     if (context_die->die_tag == DW_TAG_inlined_subroutine
12366         || context_die->die_tag == DW_TAG_subprogram)
12367       return 1;
12368
12369   return 0;
12370 }
12371
12372 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12373    whether or not to treat a DIE in this context as a declaration.  */
12374
12375 static inline int
12376 class_or_namespace_scope_p (dw_die_ref context_die)
12377 {
12378   return (context_die
12379           && (context_die->die_tag == DW_TAG_structure_type
12380               || context_die->die_tag == DW_TAG_class_type
12381               || context_die->die_tag == DW_TAG_interface_type
12382               || context_die->die_tag == DW_TAG_union_type
12383               || context_die->die_tag == DW_TAG_namespace));
12384 }
12385
12386 /* Many forms of DIEs require a "type description" attribute.  This
12387    routine locates the proper "type descriptor" die for the type given
12388    by 'type', and adds a DW_AT_type attribute below the given die.  */
12389
12390 static void
12391 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12392                     int decl_volatile, dw_die_ref context_die)
12393 {
12394   enum tree_code code  = TREE_CODE (type);
12395   dw_die_ref type_die  = NULL;
12396
12397   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12398      or fixed-point type, use the inner type.  This is because we have no
12399      support for unnamed types in base_type_die.  This can happen if this is
12400      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12401   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12402       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12403     type = TREE_TYPE (type), code = TREE_CODE (type);
12404
12405   if (code == ERROR_MARK
12406       /* Handle a special case.  For functions whose return type is void, we
12407          generate *no* type attribute.  (Note that no object may have type
12408          `void', so this only applies to function return types).  */
12409       || code == VOID_TYPE)
12410     return;
12411
12412   type_die = modified_type_die (type,
12413                                 decl_const || TYPE_READONLY (type),
12414                                 decl_volatile || TYPE_VOLATILE (type),
12415                                 context_die);
12416
12417   if (type_die != NULL)
12418     add_AT_die_ref (object_die, DW_AT_type, type_die);
12419 }
12420
12421 /* Given an object die, add the calling convention attribute for the
12422    function call type.  */
12423 static void
12424 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12425 {
12426   enum dwarf_calling_convention value = DW_CC_normal;
12427
12428   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12429
12430   /* DWARF doesn't provide a way to identify a program's source-level
12431      entry point.  DW_AT_calling_convention attributes are only meant
12432      to describe functions' calling conventions.  However, lacking a
12433      better way to signal the Fortran main program, we use this for the
12434      time being, following existing custom.  */
12435   if (is_fortran ()
12436       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12437     value = DW_CC_program;
12438
12439   /* Only add the attribute if the backend requests it, and
12440      is not DW_CC_normal.  */
12441   if (value && (value != DW_CC_normal))
12442     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12443 }
12444
12445 /* Given a tree pointer to a struct, class, union, or enum type node, return
12446    a pointer to the (string) tag name for the given type, or zero if the type
12447    was declared without a tag.  */
12448
12449 static const char *
12450 type_tag (const_tree type)
12451 {
12452   const char *name = 0;
12453
12454   if (TYPE_NAME (type) != 0)
12455     {
12456       tree t = 0;
12457
12458       /* Find the IDENTIFIER_NODE for the type name.  */
12459       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12460         t = TYPE_NAME (type);
12461
12462       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12463          a TYPE_DECL node, regardless of whether or not a `typedef' was
12464          involved.  */
12465       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12466                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12467         {
12468           /* We want to be extra verbose.  Don't call dwarf_name if
12469              DECL_NAME isn't set.  The default hook for decl_printable_name
12470              doesn't like that, and in this context it's correct to return
12471              0, instead of "<anonymous>" or the like.  */
12472           if (DECL_NAME (TYPE_NAME (type)))
12473             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12474         }
12475
12476       /* Now get the name as a string, or invent one.  */
12477       if (!name && t != 0)
12478         name = IDENTIFIER_POINTER (t);
12479     }
12480
12481   return (name == 0 || *name == '\0') ? 0 : name;
12482 }
12483
12484 /* Return the type associated with a data member, make a special check
12485    for bit field types.  */
12486
12487 static inline tree
12488 member_declared_type (const_tree member)
12489 {
12490   return (DECL_BIT_FIELD_TYPE (member)
12491           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12492 }
12493
12494 /* Get the decl's label, as described by its RTL. This may be different
12495    from the DECL_NAME name used in the source file.  */
12496
12497 #if 0
12498 static const char *
12499 decl_start_label (tree decl)
12500 {
12501   rtx x;
12502   const char *fnname;
12503
12504   x = DECL_RTL (decl);
12505   gcc_assert (MEM_P (x));
12506
12507   x = XEXP (x, 0);
12508   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12509
12510   fnname = XSTR (x, 0);
12511   return fnname;
12512 }
12513 #endif
12514 \f
12515 /* These routines generate the internal representation of the DIE's for
12516    the compilation unit.  Debugging information is collected by walking
12517    the declaration trees passed in from dwarf2out_decl().  */
12518
12519 static void
12520 gen_array_type_die (tree type, dw_die_ref context_die)
12521 {
12522   dw_die_ref scope_die = scope_die_for (type, context_die);
12523   dw_die_ref array_die;
12524
12525   /* GNU compilers represent multidimensional array types as sequences of one
12526      dimensional array types whose element types are themselves array types.
12527      We sometimes squish that down to a single array_type DIE with multiple
12528      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12529      say that we are allowed to do this kind of compression in C, because
12530      there is no difference between an array of arrays and a multidimensional
12531      array.  We don't do this for Ada to remain as close as possible to the
12532      actual representation, which is especially important against the language
12533      flexibilty wrt arrays of variable size.  */
12534
12535   bool collapse_nested_arrays = !is_ada ();
12536   tree element_type;
12537   
12538   /* ??? The SGI dwarf reader fails for array of array of enum types
12539      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12540      array type comes before the outer array type.  We thus call gen_type_die
12541      before we new_die and must prevent nested array types collapsing for this
12542      target.  */
12543
12544 #ifdef MIPS_DEBUGGING_INFO
12545   gen_type_die (TREE_TYPE (type), context_die);
12546   collapse_nested_arrays = false;
12547 #endif
12548
12549   array_die = new_die (DW_TAG_array_type, scope_die, type);
12550   add_name_attribute (array_die, type_tag (type));
12551   equate_type_number_to_die (type, array_die);
12552
12553   if (TREE_CODE (type) == VECTOR_TYPE)
12554     {
12555       /* The frontend feeds us a representation for the vector as a struct
12556          containing an array.  Pull out the array type.  */
12557       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12558       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12559     }
12560
12561   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12562   if (is_fortran ()
12563       && TREE_CODE (type) == ARRAY_TYPE
12564       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
12565     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12566
12567 #if 0
12568   /* We default the array ordering.  SDB will probably do
12569      the right things even if DW_AT_ordering is not present.  It's not even
12570      an issue until we start to get into multidimensional arrays anyway.  If
12571      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12572      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12573      and when we find out that we need to put these in, we will only do so
12574      for multidimensional arrays.  */
12575   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12576 #endif
12577
12578 #ifdef MIPS_DEBUGGING_INFO
12579   /* The SGI compilers handle arrays of unknown bound by setting
12580      AT_declaration and not emitting any subrange DIEs.  */
12581   if (! TYPE_DOMAIN (type))
12582     add_AT_flag (array_die, DW_AT_declaration, 1);
12583   else
12584 #endif
12585     add_subscript_info (array_die, type, collapse_nested_arrays);
12586
12587   /* Add representation of the type of the elements of this array type and
12588      emit the corresponding DIE if we haven't done it already.  */  
12589   element_type = TREE_TYPE (type);
12590   if (collapse_nested_arrays)
12591     while (TREE_CODE (element_type) == ARRAY_TYPE)
12592       element_type = TREE_TYPE (element_type);
12593   
12594 #ifndef MIPS_DEBUGGING_INFO
12595   gen_type_die (element_type, context_die);
12596 #endif
12597
12598   add_type_attribute (array_die, element_type, 0, 0, context_die);
12599
12600   if (get_AT (array_die, DW_AT_name))
12601     add_pubtype (type, array_die);
12602 }
12603
12604 static dw_loc_descr_ref
12605 descr_info_loc (tree val, tree base_decl)
12606 {
12607   HOST_WIDE_INT size;
12608   dw_loc_descr_ref loc, loc2;
12609   enum dwarf_location_atom op;
12610
12611   if (val == base_decl)
12612     return new_loc_descr (DW_OP_push_object_address, 0, 0);
12613
12614   switch (TREE_CODE (val))
12615     {
12616     CASE_CONVERT:
12617       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12618     case INTEGER_CST:
12619       if (host_integerp (val, 0))
12620         return int_loc_descriptor (tree_low_cst (val, 0));
12621       break;
12622     case INDIRECT_REF:
12623       size = int_size_in_bytes (TREE_TYPE (val));
12624       if (size < 0)
12625         break;
12626       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12627       if (!loc)
12628         break;
12629       if (size == DWARF2_ADDR_SIZE)
12630         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
12631       else
12632         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
12633       return loc;
12634     case POINTER_PLUS_EXPR:
12635     case PLUS_EXPR:
12636       if (host_integerp (TREE_OPERAND (val, 1), 1)
12637           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
12638              < 16384)
12639         {
12640           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12641           if (!loc)
12642             break;
12643           add_loc_descr (&loc,
12644                          new_loc_descr (DW_OP_plus_uconst,
12645                                         tree_low_cst (TREE_OPERAND (val, 1),
12646                                                       1), 0));
12647         }
12648       else
12649         {
12650           op = DW_OP_plus;
12651         do_binop:
12652           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12653           if (!loc)
12654             break;
12655           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
12656           if (!loc2)
12657             break;
12658           add_loc_descr (&loc, loc2);
12659           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
12660         }
12661       return loc;
12662     case MINUS_EXPR:
12663       op = DW_OP_minus;
12664       goto do_binop;
12665     case MULT_EXPR:
12666       op = DW_OP_mul;
12667       goto do_binop;
12668     case EQ_EXPR:
12669       op = DW_OP_eq;
12670       goto do_binop;
12671     case NE_EXPR:
12672       op = DW_OP_ne;
12673       goto do_binop;
12674     default:
12675       break;
12676     }
12677   return NULL;
12678 }
12679
12680 static void
12681 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
12682                       tree val, tree base_decl)
12683 {
12684   dw_loc_descr_ref loc;
12685
12686   if (host_integerp (val, 0))
12687     {
12688       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
12689       return;
12690     }
12691
12692   loc = descr_info_loc (val, base_decl);
12693   if (!loc)
12694     return;
12695
12696   add_AT_loc (die, attr, loc);
12697 }
12698
12699 /* This routine generates DIE for array with hidden descriptor, details
12700    are filled into *info by a langhook.  */
12701
12702 static void
12703 gen_descr_array_type_die (tree type, struct array_descr_info *info,
12704                           dw_die_ref context_die)
12705 {
12706   dw_die_ref scope_die = scope_die_for (type, context_die);
12707   dw_die_ref array_die;
12708   int dim;
12709
12710   array_die = new_die (DW_TAG_array_type, scope_die, type);
12711   add_name_attribute (array_die, type_tag (type));
12712   equate_type_number_to_die (type, array_die);
12713
12714   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12715   if (is_fortran ()
12716       && info->ndimensions >= 2)
12717     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12718
12719   if (info->data_location)
12720     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
12721                           info->base_decl);
12722   if (info->associated)
12723     add_descr_info_field (array_die, DW_AT_associated, info->associated,
12724                           info->base_decl);
12725   if (info->allocated)
12726     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
12727                           info->base_decl);
12728
12729   for (dim = 0; dim < info->ndimensions; dim++)
12730     {
12731       dw_die_ref subrange_die
12732         = new_die (DW_TAG_subrange_type, array_die, NULL);
12733
12734       if (info->dimen[dim].lower_bound)
12735         {
12736           /* If it is the default value, omit it.  */
12737           if ((is_c_family () || is_java ())
12738               && integer_zerop (info->dimen[dim].lower_bound))
12739             ;
12740           else if (is_fortran ()
12741                    && integer_onep (info->dimen[dim].lower_bound))
12742             ;
12743           else
12744             add_descr_info_field (subrange_die, DW_AT_lower_bound,
12745                                   info->dimen[dim].lower_bound,
12746                                   info->base_decl);
12747         }
12748       if (info->dimen[dim].upper_bound)
12749         add_descr_info_field (subrange_die, DW_AT_upper_bound,
12750                               info->dimen[dim].upper_bound,
12751                               info->base_decl);
12752       if (info->dimen[dim].stride)
12753         add_descr_info_field (subrange_die, DW_AT_byte_stride,
12754                               info->dimen[dim].stride,
12755                               info->base_decl);
12756     }
12757
12758   gen_type_die (info->element_type, context_die);
12759   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
12760
12761   if (get_AT (array_die, DW_AT_name))
12762     add_pubtype (type, array_die);
12763 }
12764
12765 #if 0
12766 static void
12767 gen_entry_point_die (tree decl, dw_die_ref context_die)
12768 {
12769   tree origin = decl_ultimate_origin (decl);
12770   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
12771
12772   if (origin != NULL)
12773     add_abstract_origin_attribute (decl_die, origin);
12774   else
12775     {
12776       add_name_and_src_coords_attributes (decl_die, decl);
12777       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
12778                           0, 0, context_die);
12779     }
12780
12781   if (DECL_ABSTRACT (decl))
12782     equate_decl_number_to_die (decl, decl_die);
12783   else
12784     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
12785 }
12786 #endif
12787
12788 /* Walk through the list of incomplete types again, trying once more to
12789    emit full debugging info for them.  */
12790
12791 static void
12792 retry_incomplete_types (void)
12793 {
12794   int i;
12795
12796   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12797     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12798 }
12799
12800 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
12801
12802 static void
12803 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12804 {
12805   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12806
12807   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12808      be incomplete and such types are not marked.  */
12809   add_abstract_origin_attribute (type_die, type);
12810 }
12811
12812 /* Determine what tag to use for a record type.  */
12813
12814 static enum dwarf_tag
12815 record_type_tag (tree type)
12816 {
12817   if (! lang_hooks.types.classify_record)
12818     return DW_TAG_structure_type;
12819
12820   switch (lang_hooks.types.classify_record (type))
12821     {
12822     case RECORD_IS_STRUCT:
12823       return DW_TAG_structure_type;
12824
12825     case RECORD_IS_CLASS:
12826       return DW_TAG_class_type;
12827
12828     case RECORD_IS_INTERFACE:
12829       return DW_TAG_interface_type;
12830
12831     default:
12832       gcc_unreachable ();
12833     }
12834 }
12835
12836 /* Generate a DIE to represent an inlined instance of a structure type.  */
12837
12838 static void
12839 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12840 {
12841   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12842
12843   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12844      be incomplete and such types are not marked.  */
12845   add_abstract_origin_attribute (type_die, type);
12846 }
12847
12848 /* Generate a DIE to represent an inlined instance of a union type.  */
12849
12850 static void
12851 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12852 {
12853   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12854
12855   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12856      be incomplete and such types are not marked.  */
12857   add_abstract_origin_attribute (type_die, type);
12858 }
12859
12860 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
12861    include all of the information about the enumeration values also. Each
12862    enumerated type name/value is listed as a child of the enumerated type
12863    DIE.  */
12864
12865 static dw_die_ref
12866 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12867 {
12868   dw_die_ref type_die = lookup_type_die (type);
12869
12870   if (type_die == NULL)
12871     {
12872       type_die = new_die (DW_TAG_enumeration_type,
12873                           scope_die_for (type, context_die), type);
12874       equate_type_number_to_die (type, type_die);
12875       add_name_attribute (type_die, type_tag (type));
12876     }
12877   else if (! TYPE_SIZE (type))
12878     return type_die;
12879   else
12880     remove_AT (type_die, DW_AT_declaration);
12881
12882   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12883      given enum type is incomplete, do not generate the DW_AT_byte_size
12884      attribute or the DW_AT_element_list attribute.  */
12885   if (TYPE_SIZE (type))
12886     {
12887       tree link;
12888
12889       TREE_ASM_WRITTEN (type) = 1;
12890       add_byte_size_attribute (type_die, type);
12891       if (TYPE_STUB_DECL (type) != NULL_TREE)
12892         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12893
12894       /* If the first reference to this type was as the return type of an
12895          inline function, then it may not have a parent.  Fix this now.  */
12896       if (type_die->die_parent == NULL)
12897         add_child_die (scope_die_for (type, context_die), type_die);
12898
12899       for (link = TYPE_VALUES (type);
12900            link != NULL; link = TREE_CHAIN (link))
12901         {
12902           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12903           tree value = TREE_VALUE (link);
12904
12905           add_name_attribute (enum_die,
12906                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12907
12908           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12909             /* DWARF2 does not provide a way of indicating whether or
12910                not enumeration constants are signed or unsigned.  GDB
12911                always assumes the values are signed, so we output all
12912                values as if they were signed.  That means that
12913                enumeration constants with very large unsigned values
12914                will appear to have negative values in the debugger.  */
12915             add_AT_int (enum_die, DW_AT_const_value,
12916                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12917         }
12918     }
12919   else
12920     add_AT_flag (type_die, DW_AT_declaration, 1);
12921
12922   if (get_AT (type_die, DW_AT_name))
12923     add_pubtype (type, type_die);
12924
12925   return type_die;
12926 }
12927
12928 /* Generate a DIE to represent either a real live formal parameter decl or to
12929    represent just the type of some formal parameter position in some function
12930    type.
12931
12932    Note that this routine is a bit unusual because its argument may be a
12933    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12934    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12935    node.  If it's the former then this function is being called to output a
12936    DIE to represent a formal parameter object (or some inlining thereof).  If
12937    it's the latter, then this function is only being called to output a
12938    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12939    argument type of some subprogram type.  */
12940
12941 static dw_die_ref
12942 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12943 {
12944   dw_die_ref parm_die
12945     = new_die (DW_TAG_formal_parameter, context_die, node);
12946   tree origin;
12947
12948   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12949     {
12950     case tcc_declaration:
12951       origin = decl_ultimate_origin (node);
12952       if (origin != NULL)
12953         add_abstract_origin_attribute (parm_die, origin);
12954       else
12955         {
12956           tree type = TREE_TYPE (node);
12957           add_name_and_src_coords_attributes (parm_die, node);
12958           if (DECL_BY_REFERENCE (node))
12959             type = TREE_TYPE (type);
12960           add_type_attribute (parm_die, type,
12961                               TREE_READONLY (node),
12962                               TREE_THIS_VOLATILE (node),
12963                               context_die);
12964           if (DECL_ARTIFICIAL (node))
12965             add_AT_flag (parm_die, DW_AT_artificial, 1);
12966         }
12967
12968       equate_decl_number_to_die (node, parm_die);
12969       if (! DECL_ABSTRACT (node))
12970         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12971
12972       break;
12973
12974     case tcc_type:
12975       /* We were called with some kind of a ..._TYPE node.  */
12976       add_type_attribute (parm_die, node, 0, 0, context_die);
12977       break;
12978
12979     default:
12980       gcc_unreachable ();
12981     }
12982
12983   return parm_die;
12984 }
12985
12986 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12987    at the end of an (ANSI prototyped) formal parameters list.  */
12988
12989 static void
12990 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12991 {
12992   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12993 }
12994
12995 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12996    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12997    parameters as specified in some function type specification (except for
12998    those which appear as part of a function *definition*).  */
12999
13000 static void
13001 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13002 {
13003   tree link;
13004   tree formal_type = NULL;
13005   tree first_parm_type;
13006   tree arg;
13007
13008   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13009     {
13010       arg = DECL_ARGUMENTS (function_or_method_type);
13011       function_or_method_type = TREE_TYPE (function_or_method_type);
13012     }
13013   else
13014     arg = NULL_TREE;
13015
13016   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13017
13018   /* Make our first pass over the list of formal parameter types and output a
13019      DW_TAG_formal_parameter DIE for each one.  */
13020   for (link = first_parm_type; link; )
13021     {
13022       dw_die_ref parm_die;
13023
13024       formal_type = TREE_VALUE (link);
13025       if (formal_type == void_type_node)
13026         break;
13027
13028       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13029       parm_die = gen_formal_parameter_die (formal_type, context_die);
13030       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13031            && link == first_parm_type)
13032           || (arg && DECL_ARTIFICIAL (arg)))
13033         add_AT_flag (parm_die, DW_AT_artificial, 1);
13034
13035       link = TREE_CHAIN (link);
13036       if (arg)
13037         arg = TREE_CHAIN (arg);
13038     }
13039
13040   /* If this function type has an ellipsis, add a
13041      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13042   if (formal_type != void_type_node)
13043     gen_unspecified_parameters_die (function_or_method_type, context_die);
13044
13045   /* Make our second (and final) pass over the list of formal parameter types
13046      and output DIEs to represent those types (as necessary).  */
13047   for (link = TYPE_ARG_TYPES (function_or_method_type);
13048        link && TREE_VALUE (link);
13049        link = TREE_CHAIN (link))
13050     gen_type_die (TREE_VALUE (link), context_die);
13051 }
13052
13053 /* We want to generate the DIE for TYPE so that we can generate the
13054    die for MEMBER, which has been defined; we will need to refer back
13055    to the member declaration nested within TYPE.  If we're trying to
13056    generate minimal debug info for TYPE, processing TYPE won't do the
13057    trick; we need to attach the member declaration by hand.  */
13058
13059 static void
13060 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13061 {
13062   gen_type_die (type, context_die);
13063
13064   /* If we're trying to avoid duplicate debug info, we may not have
13065      emitted the member decl for this function.  Emit it now.  */
13066   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13067       && ! lookup_decl_die (member))
13068     {
13069       dw_die_ref type_die;
13070       gcc_assert (!decl_ultimate_origin (member));
13071
13072       push_decl_scope (type);
13073       type_die = lookup_type_die (type);
13074       if (TREE_CODE (member) == FUNCTION_DECL)
13075         gen_subprogram_die (member, type_die);
13076       else if (TREE_CODE (member) == FIELD_DECL)
13077         {
13078           /* Ignore the nameless fields that are used to skip bits but handle
13079              C++ anonymous unions and structs.  */
13080           if (DECL_NAME (member) != NULL_TREE
13081               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13082               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13083             {
13084               gen_type_die (member_declared_type (member), type_die);
13085               gen_field_die (member, type_die);
13086             }
13087         }
13088       else
13089         gen_variable_die (member, type_die);
13090
13091       pop_decl_scope ();
13092     }
13093 }
13094
13095 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13096    may later generate inlined and/or out-of-line instances of.  */
13097
13098 static void
13099 dwarf2out_abstract_function (tree decl)
13100 {
13101   dw_die_ref old_die;
13102   tree save_fn;
13103   tree context;
13104   int was_abstract = DECL_ABSTRACT (decl);
13105
13106   /* Make sure we have the actual abstract inline, not a clone.  */
13107   decl = DECL_ORIGIN (decl);
13108
13109   old_die = lookup_decl_die (decl);
13110   if (old_die && get_AT (old_die, DW_AT_inline))
13111     /* We've already generated the abstract instance.  */
13112     return;
13113
13114   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13115      we don't get confused by DECL_ABSTRACT.  */
13116   if (debug_info_level > DINFO_LEVEL_TERSE)
13117     {
13118       context = decl_class_context (decl);
13119       if (context)
13120         gen_type_die_for_member
13121           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13122     }
13123
13124   /* Pretend we've just finished compiling this function.  */
13125   save_fn = current_function_decl;
13126   current_function_decl = decl;
13127   push_cfun (DECL_STRUCT_FUNCTION (decl));
13128
13129   set_decl_abstract_flags (decl, 1);
13130   dwarf2out_decl (decl);
13131   if (! was_abstract)
13132     set_decl_abstract_flags (decl, 0);
13133
13134   current_function_decl = save_fn;
13135   pop_cfun ();
13136 }
13137
13138 /* Helper function of premark_used_types() which gets called through
13139    htab_traverse_resize().
13140
13141    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13142    marked as unused by prune_unused_types.  */
13143 static int
13144 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13145 {
13146   tree type;
13147   dw_die_ref die;
13148
13149   type = (tree) *slot;
13150   die = lookup_type_die (type);
13151   if (die != NULL)
13152     die->die_perennial_p = 1;
13153   return 1;
13154 }
13155
13156 /* Mark all members of used_types_hash as perennial.  */
13157 static void
13158 premark_used_types (void)
13159 {
13160   if (cfun && cfun->used_types_hash)
13161     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13162 }
13163
13164 /* Generate a DIE to represent a declared function (either file-scope or
13165    block-local).  */
13166
13167 static void
13168 gen_subprogram_die (tree decl, dw_die_ref context_die)
13169 {
13170   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13171   tree origin = decl_ultimate_origin (decl);
13172   dw_die_ref subr_die;
13173   tree fn_arg_types;
13174   tree outer_scope;
13175   dw_die_ref old_die = lookup_decl_die (decl);
13176   int declaration = (current_function_decl != decl
13177                      || class_or_namespace_scope_p (context_die));
13178
13179   premark_used_types ();
13180
13181   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13182      started to generate the abstract instance of an inline, decided to output
13183      its containing class, and proceeded to emit the declaration of the inline
13184      from the member list for the class.  If so, DECLARATION takes priority;
13185      we'll get back to the abstract instance when done with the class.  */
13186
13187   /* The class-scope declaration DIE must be the primary DIE.  */
13188   if (origin && declaration && class_or_namespace_scope_p (context_die))
13189     {
13190       origin = NULL;
13191       gcc_assert (!old_die);
13192     }
13193
13194   /* Now that the C++ front end lazily declares artificial member fns, we
13195      might need to retrofit the declaration into its class.  */
13196   if (!declaration && !origin && !old_die
13197       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13198       && !class_or_namespace_scope_p (context_die)
13199       && debug_info_level > DINFO_LEVEL_TERSE)
13200     old_die = force_decl_die (decl);
13201
13202   if (origin != NULL)
13203     {
13204       gcc_assert (!declaration || local_scope_p (context_die));
13205
13206       /* Fixup die_parent for the abstract instance of a nested
13207          inline function.  */
13208       if (old_die && old_die->die_parent == NULL)
13209         add_child_die (context_die, old_die);
13210
13211       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13212       add_abstract_origin_attribute (subr_die, origin);
13213     }
13214   else if (old_die)
13215     {
13216       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13217       struct dwarf_file_data * file_index = lookup_filename (s.file);
13218
13219       if (!get_AT_flag (old_die, DW_AT_declaration)
13220           /* We can have a normal definition following an inline one in the
13221              case of redefinition of GNU C extern inlines.
13222              It seems reasonable to use AT_specification in this case.  */
13223           && !get_AT (old_die, DW_AT_inline))
13224         {
13225           /* Detect and ignore this case, where we are trying to output
13226              something we have already output.  */
13227           return;
13228         }
13229
13230       /* If the definition comes from the same place as the declaration,
13231          maybe use the old DIE.  We always want the DIE for this function
13232          that has the *_pc attributes to be under comp_unit_die so the
13233          debugger can find it.  We also need to do this for abstract
13234          instances of inlines, since the spec requires the out-of-line copy
13235          to have the same parent.  For local class methods, this doesn't
13236          apply; we just use the old DIE.  */
13237       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13238           && (DECL_ARTIFICIAL (decl)
13239               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13240                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13241                       == (unsigned) s.line))))
13242         {
13243           subr_die = old_die;
13244
13245           /* Clear out the declaration attribute and the formal parameters.
13246              Do not remove all children, because it is possible that this
13247              declaration die was forced using force_decl_die(). In such
13248              cases die that forced declaration die (e.g. TAG_imported_module)
13249              is one of the children that we do not want to remove.  */
13250           remove_AT (subr_die, DW_AT_declaration);
13251           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13252         }
13253       else
13254         {
13255           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13256           add_AT_specification (subr_die, old_die);
13257           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13258             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13259           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13260             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13261         }
13262     }
13263   else
13264     {
13265       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13266
13267       if (TREE_PUBLIC (decl))
13268         add_AT_flag (subr_die, DW_AT_external, 1);
13269
13270       add_name_and_src_coords_attributes (subr_die, decl);
13271       if (debug_info_level > DINFO_LEVEL_TERSE)
13272         {
13273           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13274           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13275                               0, 0, context_die);
13276         }
13277
13278       add_pure_or_virtual_attribute (subr_die, decl);
13279       if (DECL_ARTIFICIAL (decl))
13280         add_AT_flag (subr_die, DW_AT_artificial, 1);
13281
13282       if (TREE_PROTECTED (decl))
13283         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13284       else if (TREE_PRIVATE (decl))
13285         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13286     }
13287
13288   if (declaration)
13289     {
13290       if (!old_die || !get_AT (old_die, DW_AT_inline))
13291         {
13292           add_AT_flag (subr_die, DW_AT_declaration, 1);
13293
13294           /* The first time we see a member function, it is in the context of
13295              the class to which it belongs.  We make sure of this by emitting
13296              the class first.  The next time is the definition, which is
13297              handled above.  The two may come from the same source text.
13298
13299              Note that force_decl_die() forces function declaration die. It is
13300              later reused to represent definition.  */
13301           equate_decl_number_to_die (decl, subr_die);
13302         }
13303     }
13304   else if (DECL_ABSTRACT (decl))
13305     {
13306       if (DECL_DECLARED_INLINE_P (decl))
13307         {
13308           if (cgraph_function_possibly_inlined_p (decl))
13309             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13310           else
13311             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13312         }
13313       else
13314         {
13315           if (cgraph_function_possibly_inlined_p (decl))
13316             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13317           else
13318             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13319         }
13320
13321       if (DECL_DECLARED_INLINE_P (decl)
13322           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13323         add_AT_flag (subr_die, DW_AT_artificial, 1);
13324
13325       equate_decl_number_to_die (decl, subr_die);
13326     }
13327   else if (!DECL_EXTERNAL (decl))
13328     {
13329       HOST_WIDE_INT cfa_fb_offset;
13330
13331       if (!old_die || !get_AT (old_die, DW_AT_inline))
13332         equate_decl_number_to_die (decl, subr_die);
13333
13334       if (!flag_reorder_blocks_and_partition)
13335         {
13336           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13337                                        current_function_funcdef_no);
13338           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13339           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13340                                        current_function_funcdef_no);
13341           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13342
13343           add_pubname (decl, subr_die);
13344           add_arange (decl, subr_die);
13345         }
13346       else
13347         {  /* Do nothing for now; maybe need to duplicate die, one for
13348               hot section and one for cold section, then use the hot/cold
13349               section begin/end labels to generate the aranges...  */
13350           /*
13351             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13352             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13353             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13354             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13355
13356             add_pubname (decl, subr_die);
13357             add_arange (decl, subr_die);
13358             add_arange (decl, subr_die);
13359            */
13360         }
13361
13362 #ifdef MIPS_DEBUGGING_INFO
13363       /* Add a reference to the FDE for this routine.  */
13364       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13365 #endif
13366
13367       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13368
13369       /* We define the "frame base" as the function's CFA.  This is more
13370          convenient for several reasons: (1) It's stable across the prologue
13371          and epilogue, which makes it better than just a frame pointer,
13372          (2) With dwarf3, there exists a one-byte encoding that allows us
13373          to reference the .debug_frame data by proxy, but failing that,
13374          (3) We can at least reuse the code inspection and interpretation
13375          code that determines the CFA position at various points in the
13376          function.  */
13377       /* ??? Use some command-line or configury switch to enable the use
13378          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13379          consumers that understand it; fall back to "pure" dwarf2 and
13380          convert the CFA data into a location list.  */
13381       {
13382         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13383         if (list->dw_loc_next)
13384           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13385         else
13386           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13387       }
13388
13389       /* Compute a displacement from the "steady-state frame pointer" to
13390          the CFA.  The former is what all stack slots and argument slots
13391          will reference in the rtl; the later is what we've told the
13392          debugger about.  We'll need to adjust all frame_base references
13393          by this displacement.  */
13394       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13395
13396       if (cfun->static_chain_decl)
13397         add_AT_location_description (subr_die, DW_AT_static_link,
13398                  loc_descriptor_from_tree (cfun->static_chain_decl));
13399     }
13400
13401   /* Now output descriptions of the arguments for this function. This gets
13402      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13403      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13404      `...' at the end of the formal parameter list.  In order to find out if
13405      there was a trailing ellipsis or not, we must instead look at the type
13406      associated with the FUNCTION_DECL.  This will be a node of type
13407      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13408      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13409      an ellipsis at the end.  */
13410
13411   /* In the case where we are describing a mere function declaration, all we
13412      need to do here (and all we *can* do here) is to describe the *types* of
13413      its formal parameters.  */
13414   if (debug_info_level <= DINFO_LEVEL_TERSE)
13415     ;
13416   else if (declaration)
13417     gen_formal_types_die (decl, subr_die);
13418   else
13419     {
13420       /* Generate DIEs to represent all known formal parameters.  */
13421       tree arg_decls = DECL_ARGUMENTS (decl);
13422       tree parm;
13423
13424       /* When generating DIEs, generate the unspecified_parameters DIE
13425          instead if we come across the arg "__builtin_va_alist" */
13426       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13427         if (TREE_CODE (parm) == PARM_DECL)
13428           {
13429             if (DECL_NAME (parm)
13430                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13431                             "__builtin_va_alist"))
13432               gen_unspecified_parameters_die (parm, subr_die);
13433             else
13434               gen_decl_die (parm, subr_die);
13435           }
13436
13437       /* Decide whether we need an unspecified_parameters DIE at the end.
13438          There are 2 more cases to do this for: 1) the ansi ... declaration -
13439          this is detectable when the end of the arg list is not a
13440          void_type_node 2) an unprototyped function declaration (not a
13441          definition).  This just means that we have no info about the
13442          parameters at all.  */
13443       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13444       if (fn_arg_types != NULL)
13445         {
13446           /* This is the prototyped case, check for....  */
13447           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13448             gen_unspecified_parameters_die (decl, subr_die);
13449         }
13450       else if (DECL_INITIAL (decl) == NULL_TREE)
13451         gen_unspecified_parameters_die (decl, subr_die);
13452     }
13453
13454   /* Output Dwarf info for all of the stuff within the body of the function
13455      (if it has one - it may be just a declaration).  */
13456   outer_scope = DECL_INITIAL (decl);
13457
13458   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13459      a function.  This BLOCK actually represents the outermost binding contour
13460      for the function, i.e. the contour in which the function's formal
13461      parameters and labels get declared. Curiously, it appears that the front
13462      end doesn't actually put the PARM_DECL nodes for the current function onto
13463      the BLOCK_VARS list for this outer scope, but are strung off of the
13464      DECL_ARGUMENTS list for the function instead.
13465
13466      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13467      the LABEL_DECL nodes for the function however, and we output DWARF info
13468      for those in decls_for_scope.  Just within the `outer_scope' there will be
13469      a BLOCK node representing the function's outermost pair of curly braces,
13470      and any blocks used for the base and member initializers of a C++
13471      constructor function.  */
13472   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13473     {
13474       /* Emit a DW_TAG_variable DIE for a named return value.  */
13475       if (DECL_NAME (DECL_RESULT (decl)))
13476         gen_decl_die (DECL_RESULT (decl), subr_die);
13477
13478       current_function_has_inlines = 0;
13479       decls_for_scope (outer_scope, subr_die, 0);
13480
13481 #if 0 && defined (MIPS_DEBUGGING_INFO)
13482       if (current_function_has_inlines)
13483         {
13484           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13485           if (! comp_unit_has_inlines)
13486             {
13487               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13488               comp_unit_has_inlines = 1;
13489             }
13490         }
13491 #endif
13492     }
13493   /* Add the calling convention attribute if requested.  */
13494   add_calling_convention_attribute (subr_die, decl);
13495
13496 }
13497
13498 /* Generate a DIE to represent a declared data object.  */
13499
13500 static void
13501 gen_variable_die (tree decl, dw_die_ref context_die)
13502 {
13503   HOST_WIDE_INT off;
13504   tree com_decl;
13505   dw_die_ref var_die;
13506   tree origin = decl_ultimate_origin (decl);
13507   dw_die_ref old_die = lookup_decl_die (decl);
13508   int declaration = (DECL_EXTERNAL (decl)
13509                      /* If DECL is COMDAT and has not actually been
13510                         emitted, we cannot take its address; there
13511                         might end up being no definition anywhere in
13512                         the program.  For example, consider the C++
13513                         test case:
13514
13515                           template <class T>
13516                           struct S { static const int i = 7; };
13517
13518                           template <class T>
13519                           const int S<T>::i;
13520
13521                           int f() { return S<int>::i; }
13522
13523                         Here, S<int>::i is not DECL_EXTERNAL, but no
13524                         definition is required, so the compiler will
13525                         not emit a definition.  */
13526                      || (TREE_CODE (decl) == VAR_DECL
13527                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
13528                      || class_or_namespace_scope_p (context_die));
13529
13530   com_decl = fortran_common (decl, &off);
13531
13532   /* Symbol in common gets emitted as a child of the common block, in the form
13533      of a data member.
13534
13535      ??? This creates a new common block die for every common block symbol.
13536      Better to share same common block die for all symbols in that block.  */
13537   if (com_decl)
13538     {
13539       tree field;
13540       dw_die_ref com_die;
13541       const char *cnam = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13542       dw_loc_descr_ref loc = loc_descriptor_from_tree (com_decl);
13543
13544       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13545       var_die = new_die (DW_TAG_common_block, context_die, decl);
13546       add_name_and_src_coords_attributes (var_die, field);
13547       add_AT_flag (var_die, DW_AT_external, 1);
13548       add_AT_loc (var_die, DW_AT_location, loc);
13549       com_die = new_die (DW_TAG_member, var_die, decl);
13550       add_name_and_src_coords_attributes (com_die, decl);
13551       add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
13552                           TREE_THIS_VOLATILE (decl), context_die);
13553       add_AT_loc (com_die, DW_AT_data_member_location,
13554                   int_loc_descriptor (off));
13555       add_pubname_string (cnam, var_die); /* ??? needed? */
13556       return;
13557     }
13558
13559   var_die = new_die (DW_TAG_variable, context_die, decl);
13560
13561   if (origin != NULL)
13562     add_abstract_origin_attribute (var_die, origin);
13563
13564   /* Loop unrolling can create multiple blocks that refer to the same
13565      static variable, so we must test for the DW_AT_declaration flag.
13566
13567      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
13568      copy decls and set the DECL_ABSTRACT flag on them instead of
13569      sharing them.
13570
13571      ??? Duplicated blocks have been rewritten to use .debug_ranges.
13572
13573      ??? The declare_in_namespace support causes us to get two DIEs for one
13574      variable, both of which are declarations.  We want to avoid considering
13575      one to be a specification, so we must test that this DIE is not a
13576      declaration.  */
13577   else if (old_die && TREE_STATIC (decl) && ! declaration
13578            && get_AT_flag (old_die, DW_AT_declaration) == 1)
13579     {
13580       /* This is a definition of a C++ class level static.  */
13581       add_AT_specification (var_die, old_die);
13582       if (DECL_NAME (decl))
13583         {
13584           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13585           struct dwarf_file_data * file_index = lookup_filename (s.file);
13586
13587           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13588             add_AT_file (var_die, DW_AT_decl_file, file_index);
13589
13590           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13591             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
13592         }
13593     }
13594   else
13595     {
13596       tree type = TREE_TYPE (decl);
13597       if ((TREE_CODE (decl) == PARM_DECL
13598            || TREE_CODE (decl) == RESULT_DECL)
13599           && DECL_BY_REFERENCE (decl))
13600         type = TREE_TYPE (type);
13601
13602       add_name_and_src_coords_attributes (var_die, decl);
13603       add_type_attribute (var_die, type, TREE_READONLY (decl),
13604                           TREE_THIS_VOLATILE (decl), context_die);
13605
13606       if (TREE_PUBLIC (decl))
13607         add_AT_flag (var_die, DW_AT_external, 1);
13608
13609       if (DECL_ARTIFICIAL (decl))
13610         add_AT_flag (var_die, DW_AT_artificial, 1);
13611
13612       if (TREE_PROTECTED (decl))
13613         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
13614       else if (TREE_PRIVATE (decl))
13615         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
13616     }
13617
13618   if (declaration)
13619     add_AT_flag (var_die, DW_AT_declaration, 1);
13620
13621   if (DECL_ABSTRACT (decl) || declaration)
13622     equate_decl_number_to_die (decl, var_die);
13623
13624   if (! declaration && ! DECL_ABSTRACT (decl))
13625     {
13626       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
13627       add_pubname (decl, var_die);
13628     }
13629   else
13630     tree_add_const_value_attribute (var_die, decl);
13631 }
13632
13633 /* Generate a DIE to represent a label identifier.  */
13634
13635 static void
13636 gen_label_die (tree decl, dw_die_ref context_die)
13637 {
13638   tree origin = decl_ultimate_origin (decl);
13639   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
13640   rtx insn;
13641   char label[MAX_ARTIFICIAL_LABEL_BYTES];
13642
13643   if (origin != NULL)
13644     add_abstract_origin_attribute (lbl_die, origin);
13645   else
13646     add_name_and_src_coords_attributes (lbl_die, decl);
13647
13648   if (DECL_ABSTRACT (decl))
13649     equate_decl_number_to_die (decl, lbl_die);
13650   else
13651     {
13652       insn = DECL_RTL_IF_SET (decl);
13653
13654       /* Deleted labels are programmer specified labels which have been
13655          eliminated because of various optimizations.  We still emit them
13656          here so that it is possible to put breakpoints on them.  */
13657       if (insn
13658           && (LABEL_P (insn)
13659               || ((NOTE_P (insn)
13660                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
13661         {
13662           /* When optimization is enabled (via -O) some parts of the compiler
13663              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
13664              represent source-level labels which were explicitly declared by
13665              the user.  This really shouldn't be happening though, so catch
13666              it if it ever does happen.  */
13667           gcc_assert (!INSN_DELETED_P (insn));
13668
13669           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
13670           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
13671         }
13672     }
13673 }
13674
13675 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
13676    attributes to the DIE for a block STMT, to describe where the inlined
13677    function was called from.  This is similar to add_src_coords_attributes.  */
13678
13679 static inline void
13680 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
13681 {
13682   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
13683
13684   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
13685   add_AT_unsigned (die, DW_AT_call_line, s.line);
13686 }
13687
13688
13689 /* If STMT's abstract origin is a function declaration and STMT's
13690    first subblock's abstract origin is the function's outermost block,
13691    then we're looking at the main entry point.  */
13692 static bool
13693 is_inlined_entry_point (const_tree stmt)
13694 {
13695   tree decl, block;
13696
13697   if (!stmt || TREE_CODE (stmt) != BLOCK)
13698     return false;
13699
13700   decl = block_ultimate_origin (stmt);
13701
13702   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
13703     return false;
13704
13705   block = BLOCK_SUBBLOCKS (stmt);
13706
13707   if (block)
13708     {
13709       if (TREE_CODE (block) != BLOCK)
13710         return false;
13711
13712       block = block_ultimate_origin (block);
13713     }
13714
13715   return block == DECL_INITIAL (decl);
13716 }
13717
13718 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
13719    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
13720
13721 static inline void
13722 add_high_low_attributes (tree stmt, dw_die_ref die)
13723 {
13724   char label[MAX_ARTIFICIAL_LABEL_BYTES];
13725
13726   if (BLOCK_FRAGMENT_CHAIN (stmt))
13727     {
13728       tree chain;
13729
13730       if (is_inlined_entry_point (stmt))
13731         {
13732           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13733                                        BLOCK_NUMBER (stmt));
13734           add_AT_lbl_id (die, DW_AT_entry_pc, label);
13735         }
13736
13737       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
13738
13739       chain = BLOCK_FRAGMENT_CHAIN (stmt);
13740       do
13741         {
13742           add_ranges (chain);
13743           chain = BLOCK_FRAGMENT_CHAIN (chain);
13744         }
13745       while (chain);
13746       add_ranges (NULL);
13747     }
13748   else
13749     {
13750       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13751                                    BLOCK_NUMBER (stmt));
13752       add_AT_lbl_id (die, DW_AT_low_pc, label);
13753       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
13754                                    BLOCK_NUMBER (stmt));
13755       add_AT_lbl_id (die, DW_AT_high_pc, label);
13756     }
13757 }
13758
13759 /* Generate a DIE for a lexical block.  */
13760
13761 static void
13762 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
13763 {
13764   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
13765
13766   if (! BLOCK_ABSTRACT (stmt))
13767     add_high_low_attributes (stmt, stmt_die);
13768
13769   decls_for_scope (stmt, stmt_die, depth);
13770 }
13771
13772 /* Generate a DIE for an inlined subprogram.  */
13773
13774 static void
13775 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
13776 {
13777   tree decl = block_ultimate_origin (stmt);
13778
13779   /* Emit info for the abstract instance first, if we haven't yet.  We
13780      must emit this even if the block is abstract, otherwise when we
13781      emit the block below (or elsewhere), we may end up trying to emit
13782      a die whose origin die hasn't been emitted, and crashing.  */
13783   dwarf2out_abstract_function (decl);
13784
13785   if (! BLOCK_ABSTRACT (stmt))
13786     {
13787       dw_die_ref subr_die
13788         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13789
13790       add_abstract_origin_attribute (subr_die, decl);
13791       add_high_low_attributes (stmt, subr_die);
13792       add_call_src_coords_attributes (stmt, subr_die);
13793
13794       decls_for_scope (stmt, subr_die, depth);
13795       current_function_has_inlines = 1;
13796     }
13797   else
13798     /* We may get here if we're the outer block of function A that was
13799        inlined into function B that was inlined into function C.  When
13800        generating debugging info for C, dwarf2out_abstract_function(B)
13801        would mark all inlined blocks as abstract, including this one.
13802        So, we wouldn't (and shouldn't) expect labels to be generated
13803        for this one.  Instead, just emit debugging info for
13804        declarations within the block.  This is particularly important
13805        in the case of initializers of arguments passed from B to us:
13806        if they're statement expressions containing declarations, we
13807        wouldn't generate dies for their abstract variables, and then,
13808        when generating dies for the real variables, we'd die (pun
13809        intended :-)  */
13810     gen_lexical_block_die (stmt, context_die, depth);
13811 }
13812
13813 /* Generate a DIE for a field in a record, or structure.  */
13814
13815 static void
13816 gen_field_die (tree decl, dw_die_ref context_die)
13817 {
13818   dw_die_ref decl_die;
13819
13820   if (TREE_TYPE (decl) == error_mark_node)
13821     return;
13822
13823   decl_die = new_die (DW_TAG_member, context_die, decl);
13824   add_name_and_src_coords_attributes (decl_die, decl);
13825   add_type_attribute (decl_die, member_declared_type (decl),
13826                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13827                       context_die);
13828
13829   if (DECL_BIT_FIELD_TYPE (decl))
13830     {
13831       add_byte_size_attribute (decl_die, decl);
13832       add_bit_size_attribute (decl_die, decl);
13833       add_bit_offset_attribute (decl_die, decl);
13834     }
13835
13836   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13837     add_data_member_location_attribute (decl_die, decl);
13838
13839   if (DECL_ARTIFICIAL (decl))
13840     add_AT_flag (decl_die, DW_AT_artificial, 1);
13841
13842   if (TREE_PROTECTED (decl))
13843     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13844   else if (TREE_PRIVATE (decl))
13845     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13846
13847   /* Equate decl number to die, so that we can look up this decl later on.  */
13848   equate_decl_number_to_die (decl, decl_die);
13849 }
13850
13851 #if 0
13852 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13853    Use modified_type_die instead.
13854    We keep this code here just in case these types of DIEs may be needed to
13855    represent certain things in other languages (e.g. Pascal) someday.  */
13856
13857 static void
13858 gen_pointer_type_die (tree type, dw_die_ref context_die)
13859 {
13860   dw_die_ref ptr_die
13861     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13862
13863   equate_type_number_to_die (type, ptr_die);
13864   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13865   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13866 }
13867
13868 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13869    Use modified_type_die instead.
13870    We keep this code here just in case these types of DIEs may be needed to
13871    represent certain things in other languages (e.g. Pascal) someday.  */
13872
13873 static void
13874 gen_reference_type_die (tree type, dw_die_ref context_die)
13875 {
13876   dw_die_ref ref_die
13877     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13878
13879   equate_type_number_to_die (type, ref_die);
13880   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13881   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13882 }
13883 #endif
13884
13885 /* Generate a DIE for a pointer to a member type.  */
13886
13887 static void
13888 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13889 {
13890   dw_die_ref ptr_die
13891     = new_die (DW_TAG_ptr_to_member_type,
13892                scope_die_for (type, context_die), type);
13893
13894   equate_type_number_to_die (type, ptr_die);
13895   add_AT_die_ref (ptr_die, DW_AT_containing_type,
13896                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13897   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13898 }
13899
13900 /* Generate the DIE for the compilation unit.  */
13901
13902 static dw_die_ref
13903 gen_compile_unit_die (const char *filename)
13904 {
13905   dw_die_ref die;
13906   char producer[250];
13907   const char *language_string = lang_hooks.name;
13908   int language;
13909
13910   die = new_die (DW_TAG_compile_unit, NULL, NULL);
13911
13912   if (filename)
13913     {
13914       add_name_attribute (die, filename);
13915       /* Don't add cwd for <built-in>.  */
13916       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13917         add_comp_dir_attribute (die);
13918     }
13919
13920   sprintf (producer, "%s %s", language_string, version_string);
13921
13922 #ifdef MIPS_DEBUGGING_INFO
13923   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13924      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13925      not appear in the producer string, the debugger reaches the conclusion
13926      that the object file is stripped and has no debugging information.
13927      To get the MIPS/SGI debugger to believe that there is debugging
13928      information in the object file, we add a -g to the producer string.  */
13929   if (debug_info_level > DINFO_LEVEL_TERSE)
13930     strcat (producer, " -g");
13931 #endif
13932
13933   add_AT_string (die, DW_AT_producer, producer);
13934
13935   if (strcmp (language_string, "GNU C++") == 0)
13936     language = DW_LANG_C_plus_plus;
13937   else if (strcmp (language_string, "GNU Ada") == 0)
13938     language = DW_LANG_Ada95;
13939   else if (strcmp (language_string, "GNU F77") == 0)
13940     language = DW_LANG_Fortran77;
13941   else if (strcmp (language_string, "GNU Fortran") == 0)
13942     language = DW_LANG_Fortran95;
13943   else if (strcmp (language_string, "GNU Pascal") == 0)
13944     language = DW_LANG_Pascal83;
13945   else if (strcmp (language_string, "GNU Java") == 0)
13946     language = DW_LANG_Java;
13947   else if (strcmp (language_string, "GNU Objective-C") == 0)
13948     language = DW_LANG_ObjC;
13949   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13950     language = DW_LANG_ObjC_plus_plus;
13951   else
13952     language = DW_LANG_C89;
13953
13954   add_AT_unsigned (die, DW_AT_language, language);
13955   return die;
13956 }
13957
13958 /* Generate the DIE for a base class.  */
13959
13960 static void
13961 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13962 {
13963   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13964
13965   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13966   add_data_member_location_attribute (die, binfo);
13967
13968   if (BINFO_VIRTUAL_P (binfo))
13969     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13970
13971   if (access == access_public_node)
13972     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13973   else if (access == access_protected_node)
13974     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13975 }
13976
13977 /* Generate a DIE for a class member.  */
13978
13979 static void
13980 gen_member_die (tree type, dw_die_ref context_die)
13981 {
13982   tree member;
13983   tree binfo = TYPE_BINFO (type);
13984   dw_die_ref child;
13985
13986   /* If this is not an incomplete type, output descriptions of each of its
13987      members. Note that as we output the DIEs necessary to represent the
13988      members of this record or union type, we will also be trying to output
13989      DIEs to represent the *types* of those members. However the `type'
13990      function (above) will specifically avoid generating type DIEs for member
13991      types *within* the list of member DIEs for this (containing) type except
13992      for those types (of members) which are explicitly marked as also being
13993      members of this (containing) type themselves.  The g++ front- end can
13994      force any given type to be treated as a member of some other (containing)
13995      type by setting the TYPE_CONTEXT of the given (member) type to point to
13996      the TREE node representing the appropriate (containing) type.  */
13997
13998   /* First output info about the base classes.  */
13999   if (binfo)
14000     {
14001       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14002       int i;
14003       tree base;
14004
14005       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14006         gen_inheritance_die (base,
14007                              (accesses ? VEC_index (tree, accesses, i)
14008                               : access_public_node), context_die);
14009     }
14010
14011   /* Now output info about the data members and type members.  */
14012   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14013     {
14014       /* If we thought we were generating minimal debug info for TYPE
14015          and then changed our minds, some of the member declarations
14016          may have already been defined.  Don't define them again, but
14017          do put them in the right order.  */
14018
14019       child = lookup_decl_die (member);
14020       if (child)
14021         splice_child_die (context_die, child);
14022       else
14023         gen_decl_die (member, context_die);
14024     }
14025
14026   /* Now output info about the function members (if any).  */
14027   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14028     {
14029       /* Don't include clones in the member list.  */
14030       if (DECL_ABSTRACT_ORIGIN (member))
14031         continue;
14032
14033       child = lookup_decl_die (member);
14034       if (child)
14035         splice_child_die (context_die, child);
14036       else
14037         gen_decl_die (member, context_die);
14038     }
14039 }
14040
14041 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14042    is set, we pretend that the type was never defined, so we only get the
14043    member DIEs needed by later specification DIEs.  */
14044
14045 static void
14046 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14047                                 enum debug_info_usage usage)
14048 {
14049   dw_die_ref type_die = lookup_type_die (type);
14050   dw_die_ref scope_die = 0;
14051   int nested = 0;
14052   int complete = (TYPE_SIZE (type)
14053                   && (! TYPE_STUB_DECL (type)
14054                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14055   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14056   complete = complete && should_emit_struct_debug (type, usage);
14057
14058   if (type_die && ! complete)
14059     return;
14060
14061   if (TYPE_CONTEXT (type) != NULL_TREE
14062       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14063           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14064     nested = 1;
14065
14066   scope_die = scope_die_for (type, context_die);
14067
14068   if (! type_die || (nested && scope_die == comp_unit_die))
14069     /* First occurrence of type or toplevel definition of nested class.  */
14070     {
14071       dw_die_ref old_die = type_die;
14072
14073       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14074                           ? record_type_tag (type) : DW_TAG_union_type,
14075                           scope_die, type);
14076       equate_type_number_to_die (type, type_die);
14077       if (old_die)
14078         add_AT_specification (type_die, old_die);
14079       else
14080         add_name_attribute (type_die, type_tag (type));
14081     }
14082   else
14083     remove_AT (type_die, DW_AT_declaration);
14084
14085   /* If this type has been completed, then give it a byte_size attribute and
14086      then give a list of members.  */
14087   if (complete && !ns_decl)
14088     {
14089       /* Prevent infinite recursion in cases where the type of some member of
14090          this type is expressed in terms of this type itself.  */
14091       TREE_ASM_WRITTEN (type) = 1;
14092       add_byte_size_attribute (type_die, type);
14093       if (TYPE_STUB_DECL (type) != NULL_TREE)
14094         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14095
14096       /* If the first reference to this type was as the return type of an
14097          inline function, then it may not have a parent.  Fix this now.  */
14098       if (type_die->die_parent == NULL)
14099         add_child_die (scope_die, type_die);
14100
14101       push_decl_scope (type);
14102       gen_member_die (type, type_die);
14103       pop_decl_scope ();
14104
14105       /* GNU extension: Record what type our vtable lives in.  */
14106       if (TYPE_VFIELD (type))
14107         {
14108           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14109
14110           gen_type_die (vtype, context_die);
14111           add_AT_die_ref (type_die, DW_AT_containing_type,
14112                           lookup_type_die (vtype));
14113         }
14114     }
14115   else
14116     {
14117       add_AT_flag (type_die, DW_AT_declaration, 1);
14118
14119       /* We don't need to do this for function-local types.  */
14120       if (TYPE_STUB_DECL (type)
14121           && ! decl_function_context (TYPE_STUB_DECL (type)))
14122         VEC_safe_push (tree, gc, incomplete_types, type);
14123     }
14124
14125   if (get_AT (type_die, DW_AT_name))
14126     add_pubtype (type, type_die);
14127 }
14128
14129 /* Generate a DIE for a subroutine _type_.  */
14130
14131 static void
14132 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14133 {
14134   tree return_type = TREE_TYPE (type);
14135   dw_die_ref subr_die
14136     = new_die (DW_TAG_subroutine_type,
14137                scope_die_for (type, context_die), type);
14138
14139   equate_type_number_to_die (type, subr_die);
14140   add_prototyped_attribute (subr_die, type);
14141   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14142   gen_formal_types_die (type, subr_die);
14143
14144   if (get_AT (subr_die, DW_AT_name))
14145     add_pubtype (type, subr_die);
14146 }
14147
14148 /* Generate a DIE for a type definition.  */
14149
14150 static void
14151 gen_typedef_die (tree decl, dw_die_ref context_die)
14152 {
14153   dw_die_ref type_die;
14154   tree origin;
14155
14156   if (TREE_ASM_WRITTEN (decl))
14157     return;
14158
14159   TREE_ASM_WRITTEN (decl) = 1;
14160   type_die = new_die (DW_TAG_typedef, context_die, decl);
14161   origin = decl_ultimate_origin (decl);
14162   if (origin != NULL)
14163     add_abstract_origin_attribute (type_die, origin);
14164   else
14165     {
14166       tree type;
14167
14168       add_name_and_src_coords_attributes (type_die, decl);
14169       if (DECL_ORIGINAL_TYPE (decl))
14170         {
14171           type = DECL_ORIGINAL_TYPE (decl);
14172
14173           gcc_assert (type != TREE_TYPE (decl));
14174           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14175         }
14176       else
14177         type = TREE_TYPE (decl);
14178
14179       add_type_attribute (type_die, type, TREE_READONLY (decl),
14180                           TREE_THIS_VOLATILE (decl), context_die);
14181     }
14182
14183   if (DECL_ABSTRACT (decl))
14184     equate_decl_number_to_die (decl, type_die);
14185
14186   if (get_AT (type_die, DW_AT_name))
14187     add_pubtype (decl, type_die);
14188 }
14189
14190 /* Generate a type description DIE.  */
14191
14192 static void
14193 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14194                                 enum debug_info_usage usage)
14195 {
14196   int need_pop;
14197   struct array_descr_info info;
14198
14199   if (type == NULL_TREE || type == error_mark_node)
14200     return;
14201
14202   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14203       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14204     {
14205       if (TREE_ASM_WRITTEN (type))
14206         return;
14207
14208       /* Prevent broken recursion; we can't hand off to the same type.  */
14209       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14210
14211       TREE_ASM_WRITTEN (type) = 1;
14212       gen_decl_die (TYPE_NAME (type), context_die);
14213       return;
14214     }
14215
14216   /* If this is an array type with hidden descriptor, handle it first.  */
14217   if (!TREE_ASM_WRITTEN (type)
14218       && lang_hooks.types.get_array_descr_info
14219       && lang_hooks.types.get_array_descr_info (type, &info))
14220     {
14221       gen_descr_array_type_die (type, &info, context_die);
14222       TREE_ASM_WRITTEN (type) = 1;
14223       return;
14224     }
14225
14226   /* We are going to output a DIE to represent the unqualified version
14227      of this type (i.e. without any const or volatile qualifiers) so
14228      get the main variant (i.e. the unqualified version) of this type
14229      now.  (Vectors are special because the debugging info is in the
14230      cloned type itself).  */
14231   if (TREE_CODE (type) != VECTOR_TYPE)
14232     type = type_main_variant (type);
14233
14234   if (TREE_ASM_WRITTEN (type))
14235     return;
14236
14237   switch (TREE_CODE (type))
14238     {
14239     case ERROR_MARK:
14240       break;
14241
14242     case POINTER_TYPE:
14243     case REFERENCE_TYPE:
14244       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14245          ensures that the gen_type_die recursion will terminate even if the
14246          type is recursive.  Recursive types are possible in Ada.  */
14247       /* ??? We could perhaps do this for all types before the switch
14248          statement.  */
14249       TREE_ASM_WRITTEN (type) = 1;
14250
14251       /* For these types, all that is required is that we output a DIE (or a
14252          set of DIEs) to represent the "basis" type.  */
14253       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14254                                 DINFO_USAGE_IND_USE);
14255       break;
14256
14257     case OFFSET_TYPE:
14258       /* This code is used for C++ pointer-to-data-member types.
14259          Output a description of the relevant class type.  */
14260       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14261                                         DINFO_USAGE_IND_USE);
14262
14263       /* Output a description of the type of the object pointed to.  */
14264       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14265                                         DINFO_USAGE_IND_USE);
14266
14267       /* Now output a DIE to represent this pointer-to-data-member type
14268          itself.  */
14269       gen_ptr_to_mbr_type_die (type, context_die);
14270       break;
14271
14272     case FUNCTION_TYPE:
14273       /* Force out return type (in case it wasn't forced out already).  */
14274       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14275                                         DINFO_USAGE_DIR_USE);
14276       gen_subroutine_type_die (type, context_die);
14277       break;
14278
14279     case METHOD_TYPE:
14280       /* Force out return type (in case it wasn't forced out already).  */
14281       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14282                                         DINFO_USAGE_DIR_USE);
14283       gen_subroutine_type_die (type, context_die);
14284       break;
14285
14286     case ARRAY_TYPE:
14287       gen_array_type_die (type, context_die);
14288       break;
14289
14290     case VECTOR_TYPE:
14291       gen_array_type_die (type, context_die);
14292       break;
14293
14294     case ENUMERAL_TYPE:
14295     case RECORD_TYPE:
14296     case UNION_TYPE:
14297     case QUAL_UNION_TYPE:
14298       /* If this is a nested type whose containing class hasn't been written
14299          out yet, writing it out will cover this one, too.  This does not apply
14300          to instantiations of member class templates; they need to be added to
14301          the containing class as they are generated.  FIXME: This hurts the
14302          idea of combining type decls from multiple TUs, since we can't predict
14303          what set of template instantiations we'll get.  */
14304       if (TYPE_CONTEXT (type)
14305           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14306           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14307         {
14308           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14309
14310           if (TREE_ASM_WRITTEN (type))
14311             return;
14312
14313           /* If that failed, attach ourselves to the stub.  */
14314           push_decl_scope (TYPE_CONTEXT (type));
14315           context_die = lookup_type_die (TYPE_CONTEXT (type));
14316           need_pop = 1;
14317         }
14318       else
14319         {
14320           declare_in_namespace (type, context_die);
14321           need_pop = 0;
14322         }
14323
14324       if (TREE_CODE (type) == ENUMERAL_TYPE)
14325         {
14326           /* This might have been written out by the call to
14327              declare_in_namespace.  */
14328           if (!TREE_ASM_WRITTEN (type))
14329             gen_enumeration_type_die (type, context_die);
14330         }
14331       else
14332         gen_struct_or_union_type_die (type, context_die, usage);
14333
14334       if (need_pop)
14335         pop_decl_scope ();
14336
14337       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14338          it up if it is ever completed.  gen_*_type_die will set it for us
14339          when appropriate.  */
14340       return;
14341
14342     case VOID_TYPE:
14343     case INTEGER_TYPE:
14344     case REAL_TYPE:
14345     case FIXED_POINT_TYPE:
14346     case COMPLEX_TYPE:
14347     case BOOLEAN_TYPE:
14348       /* No DIEs needed for fundamental types.  */
14349       break;
14350
14351     case LANG_TYPE:
14352       /* No Dwarf representation currently defined.  */
14353       break;
14354
14355     default:
14356       gcc_unreachable ();
14357     }
14358
14359   TREE_ASM_WRITTEN (type) = 1;
14360 }
14361
14362 static void
14363 gen_type_die (tree type, dw_die_ref context_die)
14364 {
14365   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14366 }
14367
14368 /* Generate a DIE for a tagged type instantiation.  */
14369
14370 static void
14371 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
14372 {
14373   if (type == NULL_TREE || type == error_mark_node)
14374     return;
14375
14376   /* We are going to output a DIE to represent the unqualified version of
14377      this type (i.e. without any const or volatile qualifiers) so make sure
14378      that we have the main variant (i.e. the unqualified version) of this
14379      type now.  */
14380   gcc_assert (type == type_main_variant (type));
14381
14382   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
14383      an instance of an unresolved type.  */
14384
14385   switch (TREE_CODE (type))
14386     {
14387     case ERROR_MARK:
14388       break;
14389
14390     case ENUMERAL_TYPE:
14391       gen_inlined_enumeration_type_die (type, context_die);
14392       break;
14393
14394     case RECORD_TYPE:
14395       gen_inlined_structure_type_die (type, context_die);
14396       break;
14397
14398     case UNION_TYPE:
14399     case QUAL_UNION_TYPE:
14400       gen_inlined_union_type_die (type, context_die);
14401       break;
14402
14403     default:
14404       gcc_unreachable ();
14405     }
14406 }
14407
14408 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14409    things which are local to the given block.  */
14410
14411 static void
14412 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14413 {
14414   int must_output_die = 0;
14415   tree origin;
14416   tree decl;
14417   enum tree_code origin_code;
14418
14419   /* Ignore blocks that are NULL.  */
14420   if (stmt == NULL_TREE)
14421     return;
14422
14423   /* If the block is one fragment of a non-contiguous block, do not
14424      process the variables, since they will have been done by the
14425      origin block.  Do process subblocks.  */
14426   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14427     {
14428       tree sub;
14429
14430       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14431         gen_block_die (sub, context_die, depth + 1);
14432
14433       return;
14434     }
14435
14436   /* Determine the "ultimate origin" of this block.  This block may be an
14437      inlined instance of an inlined instance of inline function, so we have
14438      to trace all of the way back through the origin chain to find out what
14439      sort of node actually served as the original seed for the creation of
14440      the current block.  */
14441   origin = block_ultimate_origin (stmt);
14442   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
14443
14444   /* Determine if we need to output any Dwarf DIEs at all to represent this
14445      block.  */
14446   if (origin_code == FUNCTION_DECL)
14447     /* The outer scopes for inlinings *must* always be represented.  We
14448        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14449     must_output_die = 1;
14450   else
14451     {
14452       /* In the case where the current block represents an inlining of the
14453          "body block" of an inline function, we must *NOT* output any DIE for
14454          this block because we have already output a DIE to represent the whole
14455          inlined function scope and the "body block" of any function doesn't
14456          really represent a different scope according to ANSI C rules.  So we
14457          check here to make sure that this block does not represent a "body
14458          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
14459       if (! is_body_block (origin ? origin : stmt))
14460         {
14461           /* Determine if this block directly contains any "significant"
14462              local declarations which we will need to output DIEs for.  */
14463           if (debug_info_level > DINFO_LEVEL_TERSE)
14464             /* We are not in terse mode so *any* local declaration counts
14465                as being a "significant" one.  */
14466             must_output_die = (BLOCK_VARS (stmt) != NULL
14467                                && (TREE_USED (stmt)
14468                                    || TREE_ASM_WRITTEN (stmt)
14469                                    || BLOCK_ABSTRACT (stmt)));
14470           else
14471             /* We are in terse mode, so only local (nested) function
14472                definitions count as "significant" local declarations.  */
14473             for (decl = BLOCK_VARS (stmt);
14474                  decl != NULL; decl = TREE_CHAIN (decl))
14475               if (TREE_CODE (decl) == FUNCTION_DECL
14476                   && DECL_INITIAL (decl))
14477                 {
14478                   must_output_die = 1;
14479                   break;
14480                 }
14481         }
14482     }
14483
14484   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14485      DIE for any block which contains no significant local declarations at
14486      all.  Rather, in such cases we just call `decls_for_scope' so that any
14487      needed Dwarf info for any sub-blocks will get properly generated. Note
14488      that in terse mode, our definition of what constitutes a "significant"
14489      local declaration gets restricted to include only inlined function
14490      instances and local (nested) function definitions.  */
14491   if (must_output_die)
14492     {
14493       if (origin_code == FUNCTION_DECL)
14494         gen_inlined_subroutine_die (stmt, context_die, depth);
14495       else
14496         gen_lexical_block_die (stmt, context_die, depth);
14497     }
14498   else
14499     decls_for_scope (stmt, context_die, depth);
14500 }
14501
14502 /* Generate all of the decls declared within a given scope and (recursively)
14503    all of its sub-blocks.  */
14504
14505 static void
14506 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14507 {
14508   tree decl;
14509   tree subblocks;
14510
14511   /* Ignore NULL blocks.  */
14512   if (stmt == NULL_TREE)
14513     return;
14514
14515   if (TREE_USED (stmt))
14516     {
14517       /* Output the DIEs to represent all of the data objects and typedefs
14518          declared directly within this block but not within any nested
14519          sub-blocks.  Also, nested function and tag DIEs have been
14520          generated with a parent of NULL; fix that up now.  */
14521       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14522         {
14523           dw_die_ref die;
14524
14525           if (TREE_CODE (decl) == FUNCTION_DECL)
14526             die = lookup_decl_die (decl);
14527           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
14528             die = lookup_type_die (TREE_TYPE (decl));
14529           else
14530             die = NULL;
14531
14532           if (die != NULL && die->die_parent == NULL)
14533             add_child_die (context_die, die);
14534           /* Do not produce debug information for static variables since
14535              these might be optimized out.  We are called for these later
14536              in varpool_analyze_pending_decls.
14537
14538              But *do* produce it for Fortran COMMON variables because,
14539              even though they are static, their names can differ depending
14540              on the scope, which we need to preserve.  */
14541           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
14542               && !(is_fortran () && TREE_PUBLIC (decl)))
14543             ;
14544           else
14545             gen_decl_die (decl, context_die);
14546         }
14547     }
14548
14549   /* If we're at -g1, we're not interested in subblocks.  */
14550   if (debug_info_level <= DINFO_LEVEL_TERSE)
14551     return;
14552
14553   /* Output the DIEs to represent all sub-blocks (and the items declared
14554      therein) of this block.  */
14555   for (subblocks = BLOCK_SUBBLOCKS (stmt);
14556        subblocks != NULL;
14557        subblocks = BLOCK_CHAIN (subblocks))
14558     gen_block_die (subblocks, context_die, depth + 1);
14559 }
14560
14561 /* Is this a typedef we can avoid emitting?  */
14562
14563 static inline int
14564 is_redundant_typedef (const_tree decl)
14565 {
14566   if (TYPE_DECL_IS_STUB (decl))
14567     return 1;
14568
14569   if (DECL_ARTIFICIAL (decl)
14570       && DECL_CONTEXT (decl)
14571       && is_tagged_type (DECL_CONTEXT (decl))
14572       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
14573       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
14574     /* Also ignore the artificial member typedef for the class name.  */
14575     return 1;
14576
14577   return 0;
14578 }
14579
14580 /* Returns the DIE for a context.  */
14581
14582 static inline dw_die_ref
14583 get_context_die (tree context)
14584 {
14585   if (context)
14586     {
14587       /* Find die that represents this context.  */
14588       if (TYPE_P (context))
14589         return force_type_die (context);
14590       else
14591         return force_decl_die (context);
14592     }
14593   return comp_unit_die;
14594 }
14595
14596 /* Returns the DIE for decl.  A DIE will always be returned.  */
14597
14598 static dw_die_ref
14599 force_decl_die (tree decl)
14600 {
14601   dw_die_ref decl_die;
14602   unsigned saved_external_flag;
14603   tree save_fn = NULL_TREE;
14604   decl_die = lookup_decl_die (decl);
14605   if (!decl_die)
14606     {
14607       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
14608
14609       decl_die = lookup_decl_die (decl);
14610       if (decl_die)
14611         return decl_die;
14612
14613       switch (TREE_CODE (decl))
14614         {
14615         case FUNCTION_DECL:
14616           /* Clear current_function_decl, so that gen_subprogram_die thinks
14617              that this is a declaration. At this point, we just want to force
14618              declaration die.  */
14619           save_fn = current_function_decl;
14620           current_function_decl = NULL_TREE;
14621           gen_subprogram_die (decl, context_die);
14622           current_function_decl = save_fn;
14623           break;
14624
14625         case VAR_DECL:
14626           /* Set external flag to force declaration die. Restore it after
14627            gen_decl_die() call.  */
14628           saved_external_flag = DECL_EXTERNAL (decl);
14629           DECL_EXTERNAL (decl) = 1;
14630           gen_decl_die (decl, context_die);
14631           DECL_EXTERNAL (decl) = saved_external_flag;
14632           break;
14633
14634         case NAMESPACE_DECL:
14635           dwarf2out_decl (decl);
14636           break;
14637
14638         default:
14639           gcc_unreachable ();
14640         }
14641
14642       /* We should be able to find the DIE now.  */
14643       if (!decl_die)
14644         decl_die = lookup_decl_die (decl);
14645       gcc_assert (decl_die);
14646     }
14647
14648   return decl_die;
14649 }
14650
14651 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
14652    always returned.  */
14653
14654 static dw_die_ref
14655 force_type_die (tree type)
14656 {
14657   dw_die_ref type_die;
14658
14659   type_die = lookup_type_die (type);
14660   if (!type_die)
14661     {
14662       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
14663
14664       type_die = modified_type_die (type, TYPE_READONLY (type),
14665                                     TYPE_VOLATILE (type), context_die);
14666       gcc_assert (type_die);
14667     }
14668   return type_die;
14669 }
14670
14671 /* Force out any required namespaces to be able to output DECL,
14672    and return the new context_die for it, if it's changed.  */
14673
14674 static dw_die_ref
14675 setup_namespace_context (tree thing, dw_die_ref context_die)
14676 {
14677   tree context = (DECL_P (thing)
14678                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
14679   if (context && TREE_CODE (context) == NAMESPACE_DECL)
14680     /* Force out the namespace.  */
14681     context_die = force_decl_die (context);
14682
14683   return context_die;
14684 }
14685
14686 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
14687    type) within its namespace, if appropriate.
14688
14689    For compatibility with older debuggers, namespace DIEs only contain
14690    declarations; all definitions are emitted at CU scope.  */
14691
14692 static void
14693 declare_in_namespace (tree thing, dw_die_ref context_die)
14694 {
14695   dw_die_ref ns_context;
14696
14697   if (debug_info_level <= DINFO_LEVEL_TERSE)
14698     return;
14699
14700   /* If this decl is from an inlined function, then don't try to emit it in its
14701      namespace, as we will get confused.  It would have already been emitted
14702      when the abstract instance of the inline function was emitted anyways.  */
14703   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
14704     return;
14705
14706   ns_context = setup_namespace_context (thing, context_die);
14707
14708   if (ns_context != context_die)
14709     {
14710       if (DECL_P (thing))
14711         gen_decl_die (thing, ns_context);
14712       else
14713         gen_type_die (thing, ns_context);
14714     }
14715 }
14716
14717 /* Generate a DIE for a namespace or namespace alias.  */
14718
14719 static void
14720 gen_namespace_die (tree decl)
14721 {
14722   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
14723
14724   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
14725      they are an alias of.  */
14726   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
14727     {
14728       /* Output a real namespace.  */
14729       dw_die_ref namespace_die
14730         = new_die (DW_TAG_namespace, context_die, decl);
14731       add_name_and_src_coords_attributes (namespace_die, decl);
14732       equate_decl_number_to_die (decl, namespace_die);
14733     }
14734   else
14735     {
14736       /* Output a namespace alias.  */
14737
14738       /* Force out the namespace we are an alias of, if necessary.  */
14739       dw_die_ref origin_die
14740         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
14741
14742       /* Now create the namespace alias DIE.  */
14743       dw_die_ref namespace_die
14744         = new_die (DW_TAG_imported_declaration, context_die, decl);
14745       add_name_and_src_coords_attributes (namespace_die, decl);
14746       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
14747       equate_decl_number_to_die (decl, namespace_die);
14748     }
14749 }
14750
14751 /* Generate Dwarf debug information for a decl described by DECL.  */
14752
14753 static void
14754 gen_decl_die (tree decl, dw_die_ref context_die)
14755 {
14756   tree origin;
14757
14758   if (DECL_P (decl) && DECL_IGNORED_P (decl))
14759     return;
14760
14761   switch (TREE_CODE (decl))
14762     {
14763     case ERROR_MARK:
14764       break;
14765
14766     case CONST_DECL:
14767       /* The individual enumerators of an enum type get output when we output
14768          the Dwarf representation of the relevant enum type itself.  */
14769       break;
14770
14771     case FUNCTION_DECL:
14772       /* Don't output any DIEs to represent mere function declarations,
14773          unless they are class members or explicit block externs.  */
14774       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
14775           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
14776         break;
14777
14778 #if 0
14779       /* FIXME */
14780       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
14781          on local redeclarations of global functions.  That seems broken.  */
14782       if (current_function_decl != decl)
14783         /* This is only a declaration.  */;
14784 #endif
14785
14786       /* If we're emitting a clone, emit info for the abstract instance.  */
14787       if (DECL_ORIGIN (decl) != decl)
14788         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14789
14790       /* If we're emitting an out-of-line copy of an inline function,
14791          emit info for the abstract instance and set up to refer to it.  */
14792       else if (cgraph_function_possibly_inlined_p (decl)
14793                && ! DECL_ABSTRACT (decl)
14794                && ! class_or_namespace_scope_p (context_die)
14795                /* dwarf2out_abstract_function won't emit a die if this is just
14796                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
14797                   that case, because that works only if we have a die.  */
14798                && DECL_INITIAL (decl) != NULL_TREE)
14799         {
14800           dwarf2out_abstract_function (decl);
14801           set_decl_origin_self (decl);
14802         }
14803
14804       /* Otherwise we're emitting the primary DIE for this decl.  */
14805       else if (debug_info_level > DINFO_LEVEL_TERSE)
14806         {
14807           /* Before we describe the FUNCTION_DECL itself, make sure that we
14808              have described its return type.  */
14809           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14810
14811           /* And its virtual context.  */
14812           if (DECL_VINDEX (decl) != NULL_TREE)
14813             gen_type_die (DECL_CONTEXT (decl), context_die);
14814
14815           /* And its containing type.  */
14816           origin = decl_class_context (decl);
14817           if (origin != NULL_TREE)
14818             gen_type_die_for_member (origin, decl, context_die);
14819
14820           /* And its containing namespace.  */
14821           declare_in_namespace (decl, context_die);
14822         }
14823
14824       /* Now output a DIE to represent the function itself.  */
14825       gen_subprogram_die (decl, context_die);
14826       break;
14827
14828     case TYPE_DECL:
14829       /* If we are in terse mode, don't generate any DIEs to represent any
14830          actual typedefs.  */
14831       if (debug_info_level <= DINFO_LEVEL_TERSE)
14832         break;
14833
14834       /* In the special case of a TYPE_DECL node representing the declaration
14835          of some type tag, if the given TYPE_DECL is marked as having been
14836          instantiated from some other (original) TYPE_DECL node (e.g. one which
14837          was generated within the original definition of an inline function) we
14838          have to generate a special (abbreviated) DW_TAG_structure_type,
14839          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
14840       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14841           && is_tagged_type (TREE_TYPE (decl)))
14842         {
14843           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14844           break;
14845         }
14846
14847       if (is_redundant_typedef (decl))
14848         gen_type_die (TREE_TYPE (decl), context_die);
14849       else
14850         /* Output a DIE to represent the typedef itself.  */
14851         gen_typedef_die (decl, context_die);
14852       break;
14853
14854     case LABEL_DECL:
14855       if (debug_info_level >= DINFO_LEVEL_NORMAL)
14856         gen_label_die (decl, context_die);
14857       break;
14858
14859     case VAR_DECL:
14860     case RESULT_DECL:
14861       /* If we are in terse mode, don't generate any DIEs to represent any
14862          variable declarations or definitions.  */
14863       if (debug_info_level <= DINFO_LEVEL_TERSE)
14864         break;
14865
14866       /* If this is the global definition of the Fortran COMMON block, we don't
14867          need to do anything.  Syntactically, the block itself has no identity,
14868          just its constituent identifiers.  */
14869       if (TREE_CODE (decl) == VAR_DECL
14870           && TREE_PUBLIC (decl)
14871           && TREE_STATIC (decl)
14872           && is_fortran ()
14873           && !DECL_HAS_VALUE_EXPR_P (decl))
14874         break;
14875
14876       /* Output any DIEs that are needed to specify the type of this data
14877          object.  */
14878       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14879         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14880       else
14881         gen_type_die (TREE_TYPE (decl), context_die);
14882
14883       /* And its containing type.  */
14884       origin = decl_class_context (decl);
14885       if (origin != NULL_TREE)
14886         gen_type_die_for_member (origin, decl, context_die);
14887
14888       /* And its containing namespace.  */
14889       declare_in_namespace (decl, context_die);
14890
14891       /* Now output the DIE to represent the data object itself.  This gets
14892          complicated because of the possibility that the VAR_DECL really
14893          represents an inlined instance of a formal parameter for an inline
14894          function.  */
14895       origin = decl_ultimate_origin (decl);
14896       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14897         gen_formal_parameter_die (decl, context_die);
14898       else
14899         gen_variable_die (decl, context_die);
14900       break;
14901
14902     case FIELD_DECL:
14903       /* Ignore the nameless fields that are used to skip bits but handle C++
14904          anonymous unions and structs.  */
14905       if (DECL_NAME (decl) != NULL_TREE
14906           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14907           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14908         {
14909           gen_type_die (member_declared_type (decl), context_die);
14910           gen_field_die (decl, context_die);
14911         }
14912       break;
14913
14914     case PARM_DECL:
14915       if (DECL_BY_REFERENCE (decl))
14916         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14917       else
14918         gen_type_die (TREE_TYPE (decl), context_die);
14919       gen_formal_parameter_die (decl, context_die);
14920       break;
14921
14922     case NAMESPACE_DECL:
14923       gen_namespace_die (decl);
14924       break;
14925
14926     default:
14927       /* Probably some frontend-internal decl.  Assume we don't care.  */
14928       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14929       break;
14930     }
14931 }
14932 \f
14933 /* Output debug information for global decl DECL.  Called from toplev.c after
14934    compilation proper has finished.  */
14935
14936 static void
14937 dwarf2out_global_decl (tree decl)
14938 {
14939   /* Output DWARF2 information for file-scope tentative data object
14940      declarations, file-scope (extern) function declarations (which
14941      had no corresponding body) and file-scope tagged type declarations
14942      and definitions which have not yet been forced out.
14943
14944      Ignore the global decl of any Fortran COMMON blocks which also
14945      wind up here though they have already been described in the local
14946      scope for the procedures using them.  */
14947   if (TREE_CODE (decl) == VAR_DECL
14948       && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14949     return;
14950
14951   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14952     dwarf2out_decl (decl);
14953 }
14954
14955 /* Output debug information for type decl DECL.  Called from toplev.c
14956    and from language front ends (to record built-in types).  */
14957 static void
14958 dwarf2out_type_decl (tree decl, int local)
14959 {
14960   if (!local)
14961     dwarf2out_decl (decl);
14962 }
14963
14964 /* Output debug information for imported module or decl.  */
14965
14966 static void
14967 dwarf2out_imported_module_or_decl (tree decl, tree context)
14968 {
14969   dw_die_ref imported_die, at_import_die;
14970   dw_die_ref scope_die;
14971   expanded_location xloc;
14972
14973   if (debug_info_level <= DINFO_LEVEL_TERSE)
14974     return;
14975
14976   gcc_assert (decl);
14977
14978   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14979      We need decl DIE for reference and scope die. First, get DIE for the decl
14980      itself.  */
14981
14982   /* Get the scope die for decl context. Use comp_unit_die for global module
14983      or decl. If die is not found for non globals, force new die.  */
14984   if (context
14985       && TYPE_P (context)
14986       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14987     return;
14988   scope_die = get_context_die (context);
14989
14990   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14991   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14992     {
14993       if (is_base_type (TREE_TYPE (decl)))
14994         at_import_die = base_type_die (TREE_TYPE (decl));
14995       else
14996         at_import_die = force_type_die (TREE_TYPE (decl));
14997       /* For namespace N { typedef void T; } using N::T; base_type_die
14998          returns NULL, but DW_TAG_imported_declaration requires
14999          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15000       if (!at_import_die)
15001         {
15002           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15003           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15004           at_import_die = lookup_type_die (TREE_TYPE (decl));
15005           gcc_assert (at_import_die);
15006         }
15007     }
15008   else
15009     {
15010       at_import_die = lookup_decl_die (decl);
15011       if (!at_import_die)
15012         {
15013           /* If we're trying to avoid duplicate debug info, we may not have
15014              emitted the member decl for this field.  Emit it now.  */
15015           if (TREE_CODE (decl) == FIELD_DECL)
15016             {
15017               tree type = DECL_CONTEXT (decl);
15018
15019               if (TYPE_CONTEXT (type)
15020                   && TYPE_P (TYPE_CONTEXT (type))
15021                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15022                                                 DINFO_USAGE_DIR_USE))
15023                 return;
15024               gen_type_die_for_member (type, decl,
15025                                        get_context_die (TYPE_CONTEXT (type)));
15026             }
15027           at_import_die = force_decl_die (decl);
15028         }
15029     }
15030
15031   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15032   if (TREE_CODE (decl) == NAMESPACE_DECL)
15033     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
15034   else
15035     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
15036
15037   xloc = expand_location (input_location);
15038   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15039   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15040   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15041 }
15042
15043 /* Write the debugging output for DECL.  */
15044
15045 void
15046 dwarf2out_decl (tree decl)
15047 {
15048   dw_die_ref context_die = comp_unit_die;
15049
15050   switch (TREE_CODE (decl))
15051     {
15052     case ERROR_MARK:
15053       return;
15054
15055     case FUNCTION_DECL:
15056       /* What we would really like to do here is to filter out all mere
15057          file-scope declarations of file-scope functions which are never
15058          referenced later within this translation unit (and keep all of ones
15059          that *are* referenced later on) but we aren't clairvoyant, so we have
15060          no idea which functions will be referenced in the future (i.e. later
15061          on within the current translation unit). So here we just ignore all
15062          file-scope function declarations which are not also definitions.  If
15063          and when the debugger needs to know something about these functions,
15064          it will have to hunt around and find the DWARF information associated
15065          with the definition of the function.
15066
15067          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15068          nodes represent definitions and which ones represent mere
15069          declarations.  We have to check DECL_INITIAL instead. That's because
15070          the C front-end supports some weird semantics for "extern inline"
15071          function definitions.  These can get inlined within the current
15072          translation unit (and thus, we need to generate Dwarf info for their
15073          abstract instances so that the Dwarf info for the concrete inlined
15074          instances can have something to refer to) but the compiler never
15075          generates any out-of-lines instances of such things (despite the fact
15076          that they *are* definitions).
15077
15078          The important point is that the C front-end marks these "extern
15079          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15080          them anyway. Note that the C++ front-end also plays some similar games
15081          for inline function definitions appearing within include files which
15082          also contain `#pragma interface' pragmas.  */
15083       if (DECL_INITIAL (decl) == NULL_TREE)
15084         return;
15085
15086       /* If we're a nested function, initially use a parent of NULL; if we're
15087          a plain function, this will be fixed up in decls_for_scope.  If
15088          we're a method, it will be ignored, since we already have a DIE.  */
15089       if (decl_function_context (decl)
15090           /* But if we're in terse mode, we don't care about scope.  */
15091           && debug_info_level > DINFO_LEVEL_TERSE)
15092         context_die = NULL;
15093       break;
15094
15095     case VAR_DECL:
15096       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15097          declaration and if the declaration was never even referenced from
15098          within this entire compilation unit.  We suppress these DIEs in
15099          order to save space in the .debug section (by eliminating entries
15100          which are probably useless).  Note that we must not suppress
15101          block-local extern declarations (whether used or not) because that
15102          would screw-up the debugger's name lookup mechanism and cause it to
15103          miss things which really ought to be in scope at a given point.  */
15104       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15105         return;
15106
15107       /* For local statics lookup proper context die.  */
15108       if (TREE_STATIC (decl) && decl_function_context (decl))
15109         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15110
15111       /* If we are in terse mode, don't generate any DIEs to represent any
15112          variable declarations or definitions.  */
15113       if (debug_info_level <= DINFO_LEVEL_TERSE)
15114         return;
15115       break;
15116
15117     case NAMESPACE_DECL:
15118       if (debug_info_level <= DINFO_LEVEL_TERSE)
15119         return;
15120       if (lookup_decl_die (decl) != NULL)
15121         return;
15122       break;
15123
15124     case TYPE_DECL:
15125       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15126       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15127         return;
15128
15129       /* Don't bother trying to generate any DIEs to represent any of the
15130          normal built-in types for the language we are compiling.  */
15131       if (DECL_IS_BUILTIN (decl))
15132         {
15133           /* OK, we need to generate one for `bool' so GDB knows what type
15134              comparisons have.  */
15135           if (is_cxx ()
15136               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15137               && ! DECL_IGNORED_P (decl))
15138             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15139
15140           return;
15141         }
15142
15143       /* If we are in terse mode, don't generate any DIEs for types.  */
15144       if (debug_info_level <= DINFO_LEVEL_TERSE)
15145         return;
15146
15147       /* If we're a function-scope tag, initially use a parent of NULL;
15148          this will be fixed up in decls_for_scope.  */
15149       if (decl_function_context (decl))
15150         context_die = NULL;
15151
15152       break;
15153
15154     default:
15155       return;
15156     }
15157
15158   gen_decl_die (decl, context_die);
15159 }
15160
15161 /* Output a marker (i.e. a label) for the beginning of the generated code for
15162    a lexical block.  */
15163
15164 static void
15165 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15166                        unsigned int blocknum)
15167 {
15168   switch_to_section (current_function_section ());
15169   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15170 }
15171
15172 /* Output a marker (i.e. a label) for the end of the generated code for a
15173    lexical block.  */
15174
15175 static void
15176 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15177 {
15178   switch_to_section (current_function_section ());
15179   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15180 }
15181
15182 /* Returns nonzero if it is appropriate not to emit any debugging
15183    information for BLOCK, because it doesn't contain any instructions.
15184
15185    Don't allow this for blocks with nested functions or local classes
15186    as we would end up with orphans, and in the presence of scheduling
15187    we may end up calling them anyway.  */
15188
15189 static bool
15190 dwarf2out_ignore_block (const_tree block)
15191 {
15192   tree decl;
15193
15194   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15195     if (TREE_CODE (decl) == FUNCTION_DECL
15196         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15197       return 0;
15198
15199   return 1;
15200 }
15201
15202 /* Hash table routines for file_hash.  */
15203
15204 static int
15205 file_table_eq (const void *p1_p, const void *p2_p)
15206 {
15207   const struct dwarf_file_data *const p1 =
15208     (const struct dwarf_file_data *) p1_p;
15209   const char *const p2 = (const char *) p2_p;
15210   return strcmp (p1->filename, p2) == 0;
15211 }
15212
15213 static hashval_t
15214 file_table_hash (const void *p_p)
15215 {
15216   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15217   return htab_hash_string (p->filename);
15218 }
15219
15220 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15221    dwarf2out.c) and return its "index".  The index of each (known) filename is
15222    just a unique number which is associated with only that one filename.  We
15223    need such numbers for the sake of generating labels (in the .debug_sfnames
15224    section) and references to those files numbers (in the .debug_srcinfo
15225    and.debug_macinfo sections).  If the filename given as an argument is not
15226    found in our current list, add it to the list and assign it the next
15227    available unique index number.  In order to speed up searches, we remember
15228    the index of the filename was looked up last.  This handles the majority of
15229    all searches.  */
15230
15231 static struct dwarf_file_data *
15232 lookup_filename (const char *file_name)
15233 {
15234   void ** slot;
15235   struct dwarf_file_data * created;
15236
15237   /* Check to see if the file name that was searched on the previous
15238      call matches this file name.  If so, return the index.  */
15239   if (file_table_last_lookup
15240       && (file_name == file_table_last_lookup->filename
15241           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15242     return file_table_last_lookup;
15243
15244   /* Didn't match the previous lookup, search the table.  */
15245   slot = htab_find_slot_with_hash (file_table, file_name,
15246                                    htab_hash_string (file_name), INSERT);
15247   if (*slot)
15248     return (struct dwarf_file_data *) *slot;
15249
15250   created = GGC_NEW (struct dwarf_file_data);
15251   created->filename = file_name;
15252   created->emitted_number = 0;
15253   *slot = created;
15254   return created;
15255 }
15256
15257 /* If the assembler will construct the file table, then translate the compiler
15258    internal file table number into the assembler file table number, and emit
15259    a .file directive if we haven't already emitted one yet.  The file table
15260    numbers are different because we prune debug info for unused variables and
15261    types, which may include filenames.  */
15262
15263 static int
15264 maybe_emit_file (struct dwarf_file_data * fd)
15265 {
15266   if (! fd->emitted_number)
15267     {
15268       if (last_emitted_file)
15269         fd->emitted_number = last_emitted_file->emitted_number + 1;
15270       else
15271         fd->emitted_number = 1;
15272       last_emitted_file = fd;
15273
15274       if (DWARF2_ASM_LINE_DEBUG_INFO)
15275         {
15276           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15277           output_quoted_string (asm_out_file,
15278                                 remap_debug_filename (fd->filename));
15279           fputc ('\n', asm_out_file);
15280         }
15281     }
15282
15283   return fd->emitted_number;
15284 }
15285
15286 /* Called by the final INSN scan whenever we see a var location.  We
15287    use it to drop labels in the right places, and throw the location in
15288    our lookup table.  */
15289
15290 static void
15291 dwarf2out_var_location (rtx loc_note)
15292 {
15293   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15294   struct var_loc_node *newloc;
15295   rtx prev_insn;
15296   static rtx last_insn;
15297   static const char *last_label;
15298   tree decl;
15299
15300   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15301     return;
15302   prev_insn = PREV_INSN (loc_note);
15303
15304   newloc = GGC_CNEW (struct var_loc_node);
15305   /* If the insn we processed last time is the previous insn
15306      and it is also a var location note, use the label we emitted
15307      last time.  */
15308   if (last_insn != NULL_RTX
15309       && last_insn == prev_insn
15310       && NOTE_P (prev_insn)
15311       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15312     {
15313       newloc->label = last_label;
15314     }
15315   else
15316     {
15317       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15318       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15319       loclabel_num++;
15320       newloc->label = ggc_strdup (loclabel);
15321     }
15322   newloc->var_loc_note = loc_note;
15323   newloc->next = NULL;
15324
15325   if (cfun && in_cold_section_p)
15326     newloc->section_label = crtl->subsections.cold_section_label;
15327   else
15328     newloc->section_label = text_section_label;
15329
15330   last_insn = loc_note;
15331   last_label = newloc->label;
15332   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15333   add_var_loc_to_decl (decl, newloc);
15334 }
15335
15336 /* We need to reset the locations at the beginning of each
15337    function. We can't do this in the end_function hook, because the
15338    declarations that use the locations won't have been output when
15339    that hook is called.  Also compute have_multiple_function_sections here.  */
15340
15341 static void
15342 dwarf2out_begin_function (tree fun)
15343 {
15344   htab_empty (decl_loc_table);
15345
15346   if (function_section (fun) != text_section)
15347     have_multiple_function_sections = true;
15348
15349   dwarf2out_note_section_used ();
15350 }
15351
15352 /* Output a label to mark the beginning of a source code line entry
15353    and record information relating to this source line, in
15354    'line_info_table' for later output of the .debug_line section.  */
15355
15356 static void
15357 dwarf2out_source_line (unsigned int line, const char *filename)
15358 {
15359   if (debug_info_level >= DINFO_LEVEL_NORMAL
15360       && line != 0)
15361     {
15362       int file_num = maybe_emit_file (lookup_filename (filename));
15363
15364       switch_to_section (current_function_section ());
15365
15366       /* If requested, emit something human-readable.  */
15367       if (flag_debug_asm)
15368         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15369                  filename, line);
15370
15371       if (DWARF2_ASM_LINE_DEBUG_INFO)
15372         {
15373           /* Emit the .loc directive understood by GNU as.  */
15374           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15375
15376           /* Indicate that line number info exists.  */
15377           line_info_table_in_use++;
15378         }
15379       else if (function_section (current_function_decl) != text_section)
15380         {
15381           dw_separate_line_info_ref line_info;
15382           targetm.asm_out.internal_label (asm_out_file,
15383                                           SEPARATE_LINE_CODE_LABEL,
15384                                           separate_line_info_table_in_use);
15385
15386           /* Expand the line info table if necessary.  */
15387           if (separate_line_info_table_in_use
15388               == separate_line_info_table_allocated)
15389             {
15390               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15391               separate_line_info_table
15392                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15393                                  separate_line_info_table,
15394                                  separate_line_info_table_allocated);
15395               memset (separate_line_info_table
15396                        + separate_line_info_table_in_use,
15397                       0,
15398                       (LINE_INFO_TABLE_INCREMENT
15399                        * sizeof (dw_separate_line_info_entry)));
15400             }
15401
15402           /* Add the new entry at the end of the line_info_table.  */
15403           line_info
15404             = &separate_line_info_table[separate_line_info_table_in_use++];
15405           line_info->dw_file_num = file_num;
15406           line_info->dw_line_num = line;
15407           line_info->function = current_function_funcdef_no;
15408         }
15409       else
15410         {
15411           dw_line_info_ref line_info;
15412
15413           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15414                                      line_info_table_in_use);
15415
15416           /* Expand the line info table if necessary.  */
15417           if (line_info_table_in_use == line_info_table_allocated)
15418             {
15419               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15420               line_info_table
15421                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15422                                  line_info_table_allocated);
15423               memset (line_info_table + line_info_table_in_use, 0,
15424                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15425             }
15426
15427           /* Add the new entry at the end of the line_info_table.  */
15428           line_info = &line_info_table[line_info_table_in_use++];
15429           line_info->dw_file_num = file_num;
15430           line_info->dw_line_num = line;
15431         }
15432     }
15433 }
15434
15435 /* Record the beginning of a new source file.  */
15436
15437 static void
15438 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15439 {
15440   if (flag_eliminate_dwarf2_dups)
15441     {
15442       /* Record the beginning of the file for break_out_includes.  */
15443       dw_die_ref bincl_die;
15444
15445       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15446       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15447     }
15448
15449   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15450     {
15451       int file_num = maybe_emit_file (lookup_filename (filename));
15452
15453       switch_to_section (debug_macinfo_section);
15454       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15455       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15456                                    lineno);
15457
15458       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15459     }
15460 }
15461
15462 /* Record the end of a source file.  */
15463
15464 static void
15465 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15466 {
15467   if (flag_eliminate_dwarf2_dups)
15468     /* Record the end of the file for break_out_includes.  */
15469     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15470
15471   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15472     {
15473       switch_to_section (debug_macinfo_section);
15474       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15475     }
15476 }
15477
15478 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
15479    the tail part of the directive line, i.e. the part which is past the
15480    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15481
15482 static void
15483 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
15484                   const char *buffer ATTRIBUTE_UNUSED)
15485 {
15486   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15487     {
15488       switch_to_section (debug_macinfo_section);
15489       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
15490       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15491       dw2_asm_output_nstring (buffer, -1, "The macro");
15492     }
15493 }
15494
15495 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
15496    the tail part of the directive line, i.e. the part which is past the
15497    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15498
15499 static void
15500 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
15501                  const char *buffer ATTRIBUTE_UNUSED)
15502 {
15503   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15504     {
15505       switch_to_section (debug_macinfo_section);
15506       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
15507       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15508       dw2_asm_output_nstring (buffer, -1, "The macro");
15509     }
15510 }
15511
15512 /* Set up for Dwarf output at the start of compilation.  */
15513
15514 static void
15515 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
15516 {
15517   /* Allocate the file_table.  */
15518   file_table = htab_create_ggc (50, file_table_hash,
15519                                 file_table_eq, NULL);
15520
15521   /* Allocate the decl_die_table.  */
15522   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
15523                                     decl_die_table_eq, NULL);
15524
15525   /* Allocate the decl_loc_table.  */
15526   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
15527                                     decl_loc_table_eq, NULL);
15528
15529   /* Allocate the initial hunk of the decl_scope_table.  */
15530   decl_scope_table = VEC_alloc (tree, gc, 256);
15531
15532   /* Allocate the initial hunk of the abbrev_die_table.  */
15533   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
15534   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
15535   /* Zero-th entry is allocated, but unused.  */
15536   abbrev_die_table_in_use = 1;
15537
15538   /* Allocate the initial hunk of the line_info_table.  */
15539   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
15540   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
15541
15542   /* Zero-th entry is allocated, but unused.  */
15543   line_info_table_in_use = 1;
15544
15545   /* Allocate the pubtypes and pubnames vectors.  */
15546   pubname_table = VEC_alloc (pubname_entry, gc, 32);
15547   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
15548
15549   /* Generate the initial DIE for the .debug section.  Note that the (string)
15550      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
15551      will (typically) be a relative pathname and that this pathname should be
15552      taken as being relative to the directory from which the compiler was
15553      invoked when the given (base) source file was compiled.  We will fill
15554      in this value in dwarf2out_finish.  */
15555   comp_unit_die = gen_compile_unit_die (NULL);
15556
15557   incomplete_types = VEC_alloc (tree, gc, 64);
15558
15559   used_rtx_array = VEC_alloc (rtx, gc, 32);
15560
15561   debug_info_section = get_section (DEBUG_INFO_SECTION,
15562                                     SECTION_DEBUG, NULL);
15563   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
15564                                       SECTION_DEBUG, NULL);
15565   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
15566                                        SECTION_DEBUG, NULL);
15567   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
15568                                        SECTION_DEBUG, NULL);
15569   debug_line_section = get_section (DEBUG_LINE_SECTION,
15570                                     SECTION_DEBUG, NULL);
15571   debug_loc_section = get_section (DEBUG_LOC_SECTION,
15572                                    SECTION_DEBUG, NULL);
15573   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
15574                                         SECTION_DEBUG, NULL);
15575 #ifdef DEBUG_PUBTYPES_SECTION
15576   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
15577                                         SECTION_DEBUG, NULL);
15578 #endif
15579   debug_str_section = get_section (DEBUG_STR_SECTION,
15580                                    DEBUG_STR_SECTION_FLAGS, NULL);
15581   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
15582                                       SECTION_DEBUG, NULL);
15583   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
15584                                      SECTION_DEBUG, NULL);
15585
15586   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
15587   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
15588                                DEBUG_ABBREV_SECTION_LABEL, 0);
15589   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
15590   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
15591                                COLD_TEXT_SECTION_LABEL, 0);
15592   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
15593
15594   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
15595                                DEBUG_INFO_SECTION_LABEL, 0);
15596   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
15597                                DEBUG_LINE_SECTION_LABEL, 0);
15598   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
15599                                DEBUG_RANGES_SECTION_LABEL, 0);
15600   switch_to_section (debug_abbrev_section);
15601   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
15602   switch_to_section (debug_info_section);
15603   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
15604   switch_to_section (debug_line_section);
15605   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
15606
15607   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15608     {
15609       switch_to_section (debug_macinfo_section);
15610       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
15611                                    DEBUG_MACINFO_SECTION_LABEL, 0);
15612       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
15613     }
15614
15615   switch_to_section (text_section);
15616   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
15617   if (flag_reorder_blocks_and_partition)
15618     {
15619       cold_text_section = unlikely_text_section ();
15620       switch_to_section (cold_text_section);
15621       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
15622     }
15623 }
15624
15625 /* A helper function for dwarf2out_finish called through
15626    ht_forall.  Emit one queued .debug_str string.  */
15627
15628 static int
15629 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
15630 {
15631   struct indirect_string_node *node = (struct indirect_string_node *) *h;
15632
15633   if (node->form == DW_FORM_strp)
15634     {
15635       switch_to_section (debug_str_section);
15636       ASM_OUTPUT_LABEL (asm_out_file, node->label);
15637       assemble_string (node->str, strlen (node->str) + 1);
15638     }
15639
15640   return 1;
15641 }
15642
15643 #if ENABLE_ASSERT_CHECKING
15644 /* Verify that all marks are clear.  */
15645
15646 static void
15647 verify_marks_clear (dw_die_ref die)
15648 {
15649   dw_die_ref c;
15650
15651   gcc_assert (! die->die_mark);
15652   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
15653 }
15654 #endif /* ENABLE_ASSERT_CHECKING */
15655
15656 /* Clear the marks for a die and its children.
15657    Be cool if the mark isn't set.  */
15658
15659 static void
15660 prune_unmark_dies (dw_die_ref die)
15661 {
15662   dw_die_ref c;
15663
15664   if (die->die_mark)
15665     die->die_mark = 0;
15666   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
15667 }
15668
15669 /* Given DIE that we're marking as used, find any other dies
15670    it references as attributes and mark them as used.  */
15671
15672 static void
15673 prune_unused_types_walk_attribs (dw_die_ref die)
15674 {
15675   dw_attr_ref a;
15676   unsigned ix;
15677
15678   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15679     {
15680       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
15681         {
15682           /* A reference to another DIE.
15683              Make sure that it will get emitted.  */
15684           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
15685         }
15686       /* Set the string's refcount to 0 so that prune_unused_types_mark
15687          accounts properly for it.  */
15688       if (AT_class (a) == dw_val_class_str)
15689         a->dw_attr_val.v.val_str->refcount = 0;
15690     }
15691 }
15692
15693
15694 /* Mark DIE as being used.  If DOKIDS is true, then walk down
15695    to DIE's children.  */
15696
15697 static void
15698 prune_unused_types_mark (dw_die_ref die, int dokids)
15699 {
15700   dw_die_ref c;
15701
15702   if (die->die_mark == 0)
15703     {
15704       /* We haven't done this node yet.  Mark it as used.  */
15705       die->die_mark = 1;
15706
15707       /* We also have to mark its parents as used.
15708          (But we don't want to mark our parents' kids due to this.)  */
15709       if (die->die_parent)
15710         prune_unused_types_mark (die->die_parent, 0);
15711
15712       /* Mark any referenced nodes.  */
15713       prune_unused_types_walk_attribs (die);
15714
15715       /* If this node is a specification,
15716          also mark the definition, if it exists.  */
15717       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
15718         prune_unused_types_mark (die->die_definition, 1);
15719     }
15720
15721   if (dokids && die->die_mark != 2)
15722     {
15723       /* We need to walk the children, but haven't done so yet.
15724          Remember that we've walked the kids.  */
15725       die->die_mark = 2;
15726
15727       /* If this is an array type, we need to make sure our
15728          kids get marked, even if they're types.  */
15729       if (die->die_tag == DW_TAG_array_type)
15730         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
15731       else
15732         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15733     }
15734 }
15735
15736
15737 /* Walk the tree DIE and mark types that we actually use.  */
15738
15739 static void
15740 prune_unused_types_walk (dw_die_ref die)
15741 {
15742   dw_die_ref c;
15743
15744   /* Don't do anything if this node is already marked.  */
15745   if (die->die_mark)
15746     return;
15747
15748   switch (die->die_tag)
15749     {
15750     case DW_TAG_const_type:
15751     case DW_TAG_packed_type:
15752     case DW_TAG_pointer_type:
15753     case DW_TAG_reference_type:
15754     case DW_TAG_volatile_type:
15755     case DW_TAG_typedef:
15756     case DW_TAG_array_type:
15757     case DW_TAG_structure_type:
15758     case DW_TAG_union_type:
15759     case DW_TAG_class_type:
15760     case DW_TAG_interface_type:
15761     case DW_TAG_friend:
15762     case DW_TAG_variant_part:
15763     case DW_TAG_enumeration_type:
15764     case DW_TAG_subroutine_type:
15765     case DW_TAG_string_type:
15766     case DW_TAG_set_type:
15767     case DW_TAG_subrange_type:
15768     case DW_TAG_ptr_to_member_type:
15769     case DW_TAG_file_type:
15770       if (die->die_perennial_p)
15771         break;
15772
15773       /* It's a type node --- don't mark it.  */
15774       return;
15775
15776     default:
15777       /* Mark everything else.  */
15778       break;
15779   }
15780
15781   die->die_mark = 1;
15782
15783   /* Now, mark any dies referenced from here.  */
15784   prune_unused_types_walk_attribs (die);
15785
15786   /* Mark children.  */
15787   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15788 }
15789
15790 /* Increment the string counts on strings referred to from DIE's
15791    attributes.  */
15792
15793 static void
15794 prune_unused_types_update_strings (dw_die_ref die)
15795 {
15796   dw_attr_ref a;
15797   unsigned ix;
15798
15799   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15800     if (AT_class (a) == dw_val_class_str)
15801       {
15802         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15803         s->refcount++;
15804         /* Avoid unnecessarily putting strings that are used less than
15805            twice in the hash table.  */
15806         if (s->refcount
15807             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15808           {
15809             void ** slot;
15810             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15811                                              htab_hash_string (s->str),
15812                                              INSERT);
15813             gcc_assert (*slot == NULL);
15814             *slot = s;
15815           }
15816       }
15817 }
15818
15819 /* Remove from the tree DIE any dies that aren't marked.  */
15820
15821 static void
15822 prune_unused_types_prune (dw_die_ref die)
15823 {
15824   dw_die_ref c;
15825
15826   gcc_assert (die->die_mark);
15827   prune_unused_types_update_strings (die);
15828
15829   if (! die->die_child)
15830     return;
15831
15832   c = die->die_child;
15833   do {
15834     dw_die_ref prev = c;
15835     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15836       if (c == die->die_child)
15837         {
15838           /* No marked children between 'prev' and the end of the list.  */
15839           if (prev == c)
15840             /* No marked children at all.  */
15841             die->die_child = NULL;
15842           else
15843             {
15844               prev->die_sib = c->die_sib;
15845               die->die_child = prev;
15846             }
15847           return;
15848         }
15849
15850     if (c != prev->die_sib)
15851       prev->die_sib = c;
15852     prune_unused_types_prune (c);
15853   } while (c != die->die_child);
15854 }
15855
15856
15857 /* Remove dies representing declarations that we never use.  */
15858
15859 static void
15860 prune_unused_types (void)
15861 {
15862   unsigned int i;
15863   limbo_die_node *node;
15864   pubname_ref pub;
15865
15866 #if ENABLE_ASSERT_CHECKING
15867   /* All the marks should already be clear.  */
15868   verify_marks_clear (comp_unit_die);
15869   for (node = limbo_die_list; node; node = node->next)
15870     verify_marks_clear (node->die);
15871 #endif /* ENABLE_ASSERT_CHECKING */
15872
15873   /* Set the mark on nodes that are actually used.  */
15874   prune_unused_types_walk (comp_unit_die);
15875   for (node = limbo_die_list; node; node = node->next)
15876     prune_unused_types_walk (node->die);
15877
15878   /* Also set the mark on nodes referenced from the
15879      pubname_table or arange_table.  */
15880   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15881     prune_unused_types_mark (pub->die, 1);
15882   for (i = 0; i < arange_table_in_use; i++)
15883     prune_unused_types_mark (arange_table[i], 1);
15884
15885   /* Get rid of nodes that aren't marked; and update the string counts.  */
15886   if (debug_str_hash)
15887     htab_empty (debug_str_hash);
15888   prune_unused_types_prune (comp_unit_die);
15889   for (node = limbo_die_list; node; node = node->next)
15890     prune_unused_types_prune (node->die);
15891
15892   /* Leave the marks clear.  */
15893   prune_unmark_dies (comp_unit_die);
15894   for (node = limbo_die_list; node; node = node->next)
15895     prune_unmark_dies (node->die);
15896 }
15897
15898 /* Set the parameter to true if there are any relative pathnames in
15899    the file table.  */
15900 static int
15901 file_table_relative_p (void ** slot, void *param)
15902 {
15903   bool *p = (bool *) param;
15904   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
15905   if (!IS_ABSOLUTE_PATH (d->filename))
15906     {
15907       *p = true;
15908       return 0;
15909     }
15910   return 1;
15911 }
15912
15913 /* Output stuff that dwarf requires at the end of every file,
15914    and generate the DWARF-2 debugging info.  */
15915
15916 static void
15917 dwarf2out_finish (const char *filename)
15918 {
15919   limbo_die_node *node, *next_node;
15920   dw_die_ref die = 0;
15921
15922   /* Add the name for the main input file now.  We delayed this from
15923      dwarf2out_init to avoid complications with PCH.  */
15924   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15925   if (!IS_ABSOLUTE_PATH (filename))
15926     add_comp_dir_attribute (comp_unit_die);
15927   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15928     {
15929       bool p = false;
15930       htab_traverse (file_table, file_table_relative_p, &p);
15931       if (p)
15932         add_comp_dir_attribute (comp_unit_die);
15933     }
15934
15935   /* Traverse the limbo die list, and add parent/child links.  The only
15936      dies without parents that should be here are concrete instances of
15937      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15938      For concrete instances, we can get the parent die from the abstract
15939      instance.  */
15940   for (node = limbo_die_list; node; node = next_node)
15941     {
15942       next_node = node->next;
15943       die = node->die;
15944
15945       if (die->die_parent == NULL)
15946         {
15947           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15948
15949           if (origin)
15950             add_child_die (origin->die_parent, die);
15951           else if (die == comp_unit_die)
15952             ;
15953           else if (errorcount > 0 || sorrycount > 0)
15954             /* It's OK to be confused by errors in the input.  */
15955             add_child_die (comp_unit_die, die);
15956           else
15957             {
15958               /* In certain situations, the lexical block containing a
15959                  nested function can be optimized away, which results
15960                  in the nested function die being orphaned.  Likewise
15961                  with the return type of that nested function.  Force
15962                  this to be a child of the containing function.
15963
15964                  It may happen that even the containing function got fully
15965                  inlined and optimized out.  In that case we are lost and
15966                  assign the empty child.  This should not be big issue as
15967                  the function is likely unreachable too.  */
15968               tree context = NULL_TREE;
15969
15970               gcc_assert (node->created_for);
15971
15972               if (DECL_P (node->created_for))
15973                 context = DECL_CONTEXT (node->created_for);
15974               else if (TYPE_P (node->created_for))
15975                 context = TYPE_CONTEXT (node->created_for);
15976
15977               gcc_assert (context
15978                           && (TREE_CODE (context) == FUNCTION_DECL
15979                               || TREE_CODE (context) == NAMESPACE_DECL));
15980
15981               origin = lookup_decl_die (context);
15982               if (origin)
15983                 add_child_die (origin, die);
15984               else
15985                 add_child_die (comp_unit_die, die);
15986             }
15987         }
15988     }
15989
15990   limbo_die_list = NULL;
15991
15992   /* Walk through the list of incomplete types again, trying once more to
15993      emit full debugging info for them.  */
15994   retry_incomplete_types ();
15995
15996   if (flag_eliminate_unused_debug_types)
15997     prune_unused_types ();
15998
15999   /* Generate separate CUs for each of the include files we've seen.
16000      They will go into limbo_die_list.  */
16001   if (flag_eliminate_dwarf2_dups)
16002     break_out_includes (comp_unit_die);
16003
16004   /* Traverse the DIE's and add add sibling attributes to those DIE's
16005      that have children.  */
16006   add_sibling_attributes (comp_unit_die);
16007   for (node = limbo_die_list; node; node = node->next)
16008     add_sibling_attributes (node->die);
16009
16010   /* Output a terminator label for the .text section.  */
16011   switch_to_section (text_section);
16012   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16013   if (flag_reorder_blocks_and_partition)
16014     {
16015       switch_to_section (unlikely_text_section ());
16016       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16017     }
16018
16019   /* We can only use the low/high_pc attributes if all of the code was
16020      in .text.  */
16021   if (!have_multiple_function_sections)
16022     {
16023       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16024       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16025     }
16026
16027   else
16028     {
16029       unsigned fde_idx = 0;
16030
16031       /* We need to give .debug_loc and .debug_ranges an appropriate
16032          "base address".  Use zero so that these addresses become
16033          absolute.  Historically, we've emitted the unexpected
16034          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16035          Emit both to give time for other tools to adapt.  */
16036       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16037       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16038
16039       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16040                          add_ranges_by_labels (text_section_label,
16041                                                text_end_label));
16042       if (flag_reorder_blocks_and_partition)
16043         add_ranges_by_labels (cold_text_section_label,
16044                               cold_end_label);
16045
16046       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16047         {
16048           dw_fde_ref fde = &fde_table[fde_idx];
16049
16050           if (fde->dw_fde_switched_sections)
16051             {
16052               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16053                                     fde->dw_fde_hot_section_end_label);
16054               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16055                                     fde->dw_fde_unlikely_section_end_label);
16056             }
16057           else
16058             add_ranges_by_labels (fde->dw_fde_begin,
16059                                   fde->dw_fde_end);
16060         }
16061
16062       add_ranges (NULL);
16063     }
16064
16065   /* Output location list section if necessary.  */
16066   if (have_location_lists)
16067     {
16068       /* Output the location lists info.  */
16069       switch_to_section (debug_loc_section);
16070       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16071                                    DEBUG_LOC_SECTION_LABEL, 0);
16072       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16073       output_location_lists (die);
16074     }
16075
16076   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16077     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16078                     debug_line_section_label);
16079
16080   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16081     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16082
16083   /* Output all of the compilation units.  We put the main one last so that
16084      the offsets are available to output_pubnames.  */
16085   for (node = limbo_die_list; node; node = node->next)
16086     output_comp_unit (node->die, 0);
16087
16088   output_comp_unit (comp_unit_die, 0);
16089
16090   /* Output the abbreviation table.  */
16091   switch_to_section (debug_abbrev_section);
16092   output_abbrev_section ();
16093
16094   /* Output public names table if necessary.  */
16095   if (!VEC_empty (pubname_entry, pubname_table))
16096     {
16097       switch_to_section (debug_pubnames_section);
16098       output_pubnames (pubname_table);
16099     }
16100
16101 #ifdef DEBUG_PUBTYPES_SECTION
16102   /* Output public types table if necessary.  */
16103   if (!VEC_empty (pubname_entry, pubtype_table))
16104     {
16105       switch_to_section (debug_pubtypes_section);
16106       output_pubnames (pubtype_table);
16107     }
16108 #endif
16109
16110   /* Output the address range information.  We only put functions in the arange
16111      table, so don't write it out if we don't have any.  */
16112   if (fde_table_in_use)
16113     {
16114       switch_to_section (debug_aranges_section);
16115       output_aranges ();
16116     }
16117
16118   /* Output ranges section if necessary.  */
16119   if (ranges_table_in_use)
16120     {
16121       switch_to_section (debug_ranges_section);
16122       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16123       output_ranges ();
16124     }
16125
16126   /* Output the source line correspondence table.  We must do this
16127      even if there is no line information.  Otherwise, on an empty
16128      translation unit, we will generate a present, but empty,
16129      .debug_info section.  IRIX 6.5 `nm' will then complain when
16130      examining the file.  This is done late so that any filenames
16131      used by the debug_info section are marked as 'used'.  */
16132   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16133     {
16134       switch_to_section (debug_line_section);
16135       output_line_info ();
16136     }
16137
16138   /* Have to end the macro section.  */
16139   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16140     {
16141       switch_to_section (debug_macinfo_section);
16142       dw2_asm_output_data (1, 0, "End compilation unit");
16143     }
16144
16145   /* If we emitted any DW_FORM_strp form attribute, output the string
16146      table too.  */
16147   if (debug_str_hash)
16148     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16149 }
16150 #else
16151
16152 /* This should never be used, but its address is needed for comparisons.  */
16153 const struct gcc_debug_hooks dwarf2_debug_hooks;
16154
16155 #endif /* DWARF2_DEBUGGING_INFO */
16156
16157 #include "gt-dwarf2out.h"