OSDN Git Service

* dwarf2out.c (def_cfa_1): Don't scale by DWARF_CIE_DATA_ALIGNMENT.
[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 /* Decide whether to emit frame unwind via assembler directives.  */
134
135 int
136 dwarf2out_do_cfi_asm (void)
137 {
138   int enc;
139
140   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
141     return false;
142   if (!eh_personality_libfunc)
143     return true;
144   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
145     return false;
146
147   /* Make sure the personality encoding is one the assembler can support.
148      In particular, aligned addresses can't be handled.  */
149   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
150   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
151     return false;
152   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
153   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
154     return false;
155
156   return true;
157 }
158
159 /* The size of the target's pointer type.  */
160 #ifndef PTR_SIZE
161 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
162 #endif
163
164 /* Array of RTXes referenced by the debugging information, which therefore
165    must be kept around forever.  */
166 static GTY(()) VEC(rtx,gc) *used_rtx_array;
167
168 /* A pointer to the base of a list of incomplete types which might be
169    completed at some later time.  incomplete_types_list needs to be a
170    VEC(tree,gc) because we want to tell the garbage collector about
171    it.  */
172 static GTY(()) VEC(tree,gc) *incomplete_types;
173
174 /* A pointer to the base of a table of references to declaration
175    scopes.  This table is a display which tracks the nesting
176    of declaration scopes at the current scope and containing
177    scopes.  This table is used to find the proper place to
178    define type declaration DIE's.  */
179 static GTY(()) VEC(tree,gc) *decl_scope_table;
180
181 /* Pointers to various DWARF2 sections.  */
182 static GTY(()) section *debug_info_section;
183 static GTY(()) section *debug_abbrev_section;
184 static GTY(()) section *debug_aranges_section;
185 static GTY(()) section *debug_macinfo_section;
186 static GTY(()) section *debug_line_section;
187 static GTY(()) section *debug_loc_section;
188 static GTY(()) section *debug_pubnames_section;
189 static GTY(()) section *debug_pubtypes_section;
190 static GTY(()) section *debug_str_section;
191 static GTY(()) section *debug_ranges_section;
192 static GTY(()) section *debug_frame_section;
193
194 /* How to start an assembler comment.  */
195 #ifndef ASM_COMMENT_START
196 #define ASM_COMMENT_START ";#"
197 #endif
198
199 typedef struct dw_cfi_struct *dw_cfi_ref;
200 typedef struct dw_fde_struct *dw_fde_ref;
201 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
202
203 /* Call frames are described using a sequence of Call Frame
204    Information instructions.  The register number, offset
205    and address fields are provided as possible operands;
206    their use is selected by the opcode field.  */
207
208 enum dw_cfi_oprnd_type {
209   dw_cfi_oprnd_unused,
210   dw_cfi_oprnd_reg_num,
211   dw_cfi_oprnd_offset,
212   dw_cfi_oprnd_addr,
213   dw_cfi_oprnd_loc
214 };
215
216 typedef union dw_cfi_oprnd_struct GTY(())
217 {
218   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
219   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
220   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
221   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
222 }
223 dw_cfi_oprnd;
224
225 typedef struct dw_cfi_struct GTY(())
226 {
227   dw_cfi_ref dw_cfi_next;
228   enum dwarf_call_frame_info dw_cfi_opc;
229   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
230     dw_cfi_oprnd1;
231   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
232     dw_cfi_oprnd2;
233 }
234 dw_cfi_node;
235
236 /* This is how we define the location of the CFA. We use to handle it
237    as REG + OFFSET all the time,  but now it can be more complex.
238    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
239    Instead of passing around REG and OFFSET, we pass a copy
240    of this structure.  */
241 typedef struct cfa_loc GTY(())
242 {
243   HOST_WIDE_INT offset;
244   HOST_WIDE_INT base_offset;
245   unsigned int reg;
246   int indirect;            /* 1 if CFA is accessed via a dereference.  */
247 } dw_cfa_location;
248
249 /* All call frame descriptions (FDE's) in the GCC generated DWARF
250    refer to a single Common Information Entry (CIE), defined at
251    the beginning of the .debug_frame section.  This use of a single
252    CIE obviates the need to keep track of multiple CIE's
253    in the DWARF generation routines below.  */
254
255 typedef struct dw_fde_struct GTY(())
256 {
257   tree decl;
258   const char *dw_fde_begin;
259   const char *dw_fde_current_label;
260   const char *dw_fde_end;
261   const char *dw_fde_hot_section_label;
262   const char *dw_fde_hot_section_end_label;
263   const char *dw_fde_unlikely_section_label;
264   const char *dw_fde_unlikely_section_end_label;
265   bool dw_fde_switched_sections;
266   dw_cfi_ref dw_fde_cfi;
267   unsigned funcdef_number;
268   HOST_WIDE_INT stack_realignment;
269   /* Dynamic realign argument pointer register.  */
270   unsigned int drap_reg;
271   /* Virtual dynamic realign argument pointer register.  */
272   unsigned int vdrap_reg;
273   unsigned all_throwers_are_sibcalls : 1;
274   unsigned nothrow : 1;
275   unsigned uses_eh_lsda : 1;
276   /* Whether we did stack realign in this call frame.  */
277   unsigned stack_realign : 1;
278   /* Whether dynamic realign argument pointer register has been saved.  */
279   unsigned drap_reg_saved: 1;
280 }
281 dw_fde_node;
282
283 /* Maximum size (in bytes) of an artificially generated label.  */
284 #define MAX_ARTIFICIAL_LABEL_BYTES      30
285
286 /* The size of addresses as they appear in the Dwarf 2 data.
287    Some architectures use word addresses to refer to code locations,
288    but Dwarf 2 info always uses byte addresses.  On such machines,
289    Dwarf 2 addresses need to be larger than the architecture's
290    pointers.  */
291 #ifndef DWARF2_ADDR_SIZE
292 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
293 #endif
294
295 /* The size in bytes of a DWARF field indicating an offset or length
296    relative to a debug info section, specified to be 4 bytes in the
297    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
298    as PTR_SIZE.  */
299
300 #ifndef DWARF_OFFSET_SIZE
301 #define DWARF_OFFSET_SIZE 4
302 #endif
303
304 /* According to the (draft) DWARF 3 specification, the initial length
305    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
306    bytes are 0xffffffff, followed by the length stored in the next 8
307    bytes.
308
309    However, the SGI/MIPS ABI uses an initial length which is equal to
310    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
311
312 #ifndef DWARF_INITIAL_LENGTH_SIZE
313 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
314 #endif
315
316 #define DWARF_VERSION 2
317
318 /* Round SIZE up to the nearest BOUNDARY.  */
319 #define DWARF_ROUND(SIZE,BOUNDARY) \
320   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
321
322 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
323 #ifndef DWARF_CIE_DATA_ALIGNMENT
324 #ifdef STACK_GROWS_DOWNWARD
325 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
326 #else
327 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
328 #endif
329 #endif
330
331 /* CIE identifier.  */
332 #if HOST_BITS_PER_WIDE_INT >= 64
333 #define DWARF_CIE_ID \
334   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
335 #else
336 #define DWARF_CIE_ID DW_CIE_ID
337 #endif
338
339 /* A pointer to the base of a table that contains frame description
340    information for each routine.  */
341 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
342
343 /* Number of elements currently allocated for fde_table.  */
344 static GTY(()) unsigned fde_table_allocated;
345
346 /* Number of elements in fde_table currently in use.  */
347 static GTY(()) unsigned fde_table_in_use;
348
349 /* Size (in elements) of increments by which we may expand the
350    fde_table.  */
351 #define FDE_TABLE_INCREMENT 256
352
353 /* Get the current fde_table entry we should use.  */
354
355 static inline dw_fde_ref
356 current_fde (void)
357 {
358   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
359 }
360
361 /* A list of call frame insns for the CIE.  */
362 static GTY(()) dw_cfi_ref cie_cfi_head;
363
364 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
365 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
366    attribute that accelerates the lookup of the FDE associated
367    with the subprogram.  This variable holds the table index of the FDE
368    associated with the current function (body) definition.  */
369 static unsigned current_funcdef_fde;
370 #endif
371
372 struct indirect_string_node GTY(())
373 {
374   const char *str;
375   unsigned int refcount;
376   unsigned int form;
377   char *label;
378 };
379
380 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
381
382 static GTY(()) int dw2_string_counter;
383 static GTY(()) unsigned long dwarf2out_cfi_label_num;
384
385 /* True if the compilation unit places functions in more than one section.  */
386 static GTY(()) bool have_multiple_function_sections = false;
387
388 /* Whether the default text and cold text sections have been used at all.  */
389
390 static GTY(()) bool text_section_used = false;
391 static GTY(()) bool cold_text_section_used = false;
392
393 /* The default cold text section.  */
394 static GTY(()) section *cold_text_section;
395
396 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
397
398 /* Forward declarations for functions defined in this file.  */
399
400 static char *stripattributes (const char *);
401 static const char *dwarf_cfi_name (unsigned);
402 static dw_cfi_ref new_cfi (void);
403 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
404 static void add_fde_cfi (const char *, dw_cfi_ref);
405 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
406 static void lookup_cfa (dw_cfa_location *);
407 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
408 #ifdef DWARF2_UNWIND_INFO
409 static void initial_return_save (rtx);
410 #endif
411 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
412 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
413 static void output_cfi_directive (dw_cfi_ref);
414 static void output_call_frame_info (int);
415 static void dwarf2out_note_section_used (void);
416 static void dwarf2out_stack_adjust (rtx, bool);
417 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
418 static void flush_queued_reg_saves (void);
419 static bool clobbers_queued_reg_save (const_rtx);
420 static void dwarf2out_frame_debug_expr (rtx, const char *);
421
422 /* Support for complex CFA locations.  */
423 static void output_cfa_loc (dw_cfi_ref);
424 static void output_cfa_loc_raw (dw_cfi_ref);
425 static void get_cfa_from_loc_descr (dw_cfa_location *,
426                                     struct dw_loc_descr_struct *);
427 static struct dw_loc_descr_struct *build_cfa_loc
428   (dw_cfa_location *, HOST_WIDE_INT);
429 static struct dw_loc_descr_struct *build_cfa_aligned_loc
430   (HOST_WIDE_INT, HOST_WIDE_INT);
431 static void def_cfa_1 (const char *, dw_cfa_location *);
432
433 /* How to start an assembler comment.  */
434 #ifndef ASM_COMMENT_START
435 #define ASM_COMMENT_START ";#"
436 #endif
437
438 /* Data and reference forms for relocatable data.  */
439 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
440 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
441
442 #ifndef DEBUG_FRAME_SECTION
443 #define DEBUG_FRAME_SECTION     ".debug_frame"
444 #endif
445
446 #ifndef FUNC_BEGIN_LABEL
447 #define FUNC_BEGIN_LABEL        "LFB"
448 #endif
449
450 #ifndef FUNC_END_LABEL
451 #define FUNC_END_LABEL          "LFE"
452 #endif
453
454 #ifndef FRAME_BEGIN_LABEL
455 #define FRAME_BEGIN_LABEL       "Lframe"
456 #endif
457 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
458 #define CIE_END_LABEL           "LECIE"
459 #define FDE_LABEL               "LSFDE"
460 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
461 #define FDE_END_LABEL           "LEFDE"
462 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
463 #define LINE_NUMBER_END_LABEL   "LELT"
464 #define LN_PROLOG_AS_LABEL      "LASLTP"
465 #define LN_PROLOG_END_LABEL     "LELTP"
466 #define DIE_LABEL_PREFIX        "DW"
467
468 /* The DWARF 2 CFA column which tracks the return address.  Normally this
469    is the column for PC, or the first column after all of the hard
470    registers.  */
471 #ifndef DWARF_FRAME_RETURN_COLUMN
472 #ifdef PC_REGNUM
473 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
474 #else
475 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
476 #endif
477 #endif
478
479 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
480    default, we just provide columns for all registers.  */
481 #ifndef DWARF_FRAME_REGNUM
482 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
483 #endif
484 \f
485 /* Hook used by __throw.  */
486
487 rtx
488 expand_builtin_dwarf_sp_column (void)
489 {
490   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
491   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
492 }
493
494 /* Return a pointer to a copy of the section string name S with all
495    attributes stripped off, and an asterisk prepended (for assemble_name).  */
496
497 static inline char *
498 stripattributes (const char *s)
499 {
500   char *stripped = XNEWVEC (char, strlen (s) + 2);
501   char *p = stripped;
502
503   *p++ = '*';
504
505   while (*s && *s != ',')
506     *p++ = *s++;
507
508   *p = '\0';
509   return stripped;
510 }
511
512 /* MEM is a memory reference for the register size table, each element of
513    which has mode MODE.  Initialize column C as a return address column.  */
514
515 static void
516 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
517 {
518   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
519   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
520   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
521 }
522
523 /* Generate code to initialize the register size table.  */
524
525 void
526 expand_builtin_init_dwarf_reg_sizes (tree address)
527 {
528   unsigned int i;
529   enum machine_mode mode = TYPE_MODE (char_type_node);
530   rtx addr = expand_normal (address);
531   rtx mem = gen_rtx_MEM (BLKmode, addr);
532   bool wrote_return_column = false;
533
534   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
535     {
536       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
537
538       if (rnum < DWARF_FRAME_REGISTERS)
539         {
540           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
541           enum machine_mode save_mode = reg_raw_mode[i];
542           HOST_WIDE_INT size;
543
544           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
545             save_mode = choose_hard_reg_mode (i, 1, true);
546           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
547             {
548               if (save_mode == VOIDmode)
549                 continue;
550               wrote_return_column = true;
551             }
552           size = GET_MODE_SIZE (save_mode);
553           if (offset < 0)
554             continue;
555
556           emit_move_insn (adjust_address (mem, mode, offset),
557                           gen_int_mode (size, mode));
558         }
559     }
560
561   if (!wrote_return_column)
562     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
563
564 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
565   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
566 #endif
567
568   targetm.init_dwarf_reg_sizes_extra (address);
569 }
570
571 /* Convert a DWARF call frame info. operation to its string name */
572
573 static const char *
574 dwarf_cfi_name (unsigned int cfi_opc)
575 {
576   switch (cfi_opc)
577     {
578     case DW_CFA_advance_loc:
579       return "DW_CFA_advance_loc";
580     case DW_CFA_offset:
581       return "DW_CFA_offset";
582     case DW_CFA_restore:
583       return "DW_CFA_restore";
584     case DW_CFA_nop:
585       return "DW_CFA_nop";
586     case DW_CFA_set_loc:
587       return "DW_CFA_set_loc";
588     case DW_CFA_advance_loc1:
589       return "DW_CFA_advance_loc1";
590     case DW_CFA_advance_loc2:
591       return "DW_CFA_advance_loc2";
592     case DW_CFA_advance_loc4:
593       return "DW_CFA_advance_loc4";
594     case DW_CFA_offset_extended:
595       return "DW_CFA_offset_extended";
596     case DW_CFA_restore_extended:
597       return "DW_CFA_restore_extended";
598     case DW_CFA_undefined:
599       return "DW_CFA_undefined";
600     case DW_CFA_same_value:
601       return "DW_CFA_same_value";
602     case DW_CFA_register:
603       return "DW_CFA_register";
604     case DW_CFA_remember_state:
605       return "DW_CFA_remember_state";
606     case DW_CFA_restore_state:
607       return "DW_CFA_restore_state";
608     case DW_CFA_def_cfa:
609       return "DW_CFA_def_cfa";
610     case DW_CFA_def_cfa_register:
611       return "DW_CFA_def_cfa_register";
612     case DW_CFA_def_cfa_offset:
613       return "DW_CFA_def_cfa_offset";
614
615     /* DWARF 3 */
616     case DW_CFA_def_cfa_expression:
617       return "DW_CFA_def_cfa_expression";
618     case DW_CFA_expression:
619       return "DW_CFA_expression";
620     case DW_CFA_offset_extended_sf:
621       return "DW_CFA_offset_extended_sf";
622     case DW_CFA_def_cfa_sf:
623       return "DW_CFA_def_cfa_sf";
624     case DW_CFA_def_cfa_offset_sf:
625       return "DW_CFA_def_cfa_offset_sf";
626
627     /* SGI/MIPS specific */
628     case DW_CFA_MIPS_advance_loc8:
629       return "DW_CFA_MIPS_advance_loc8";
630
631     /* GNU extensions */
632     case DW_CFA_GNU_window_save:
633       return "DW_CFA_GNU_window_save";
634     case DW_CFA_GNU_args_size:
635       return "DW_CFA_GNU_args_size";
636     case DW_CFA_GNU_negative_offset_extended:
637       return "DW_CFA_GNU_negative_offset_extended";
638
639     default:
640       return "DW_CFA_<unknown>";
641     }
642 }
643
644 /* Return a pointer to a newly allocated Call Frame Instruction.  */
645
646 static inline dw_cfi_ref
647 new_cfi (void)
648 {
649   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
650
651   cfi->dw_cfi_next = NULL;
652   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
653   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
654
655   return cfi;
656 }
657
658 /* Add a Call Frame Instruction to list of instructions.  */
659
660 static inline void
661 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
662 {
663   dw_cfi_ref *p;
664   dw_fde_ref fde = current_fde ();
665
666   /* When DRAP is used, CFA is defined with an expression.  Redefine
667      CFA may lead to a different CFA value.   */
668   if (fde && fde->drap_reg != INVALID_REGNUM)
669     switch (cfi->dw_cfi_opc)
670       {
671         case DW_CFA_def_cfa_register:
672         case DW_CFA_def_cfa_offset:
673         case DW_CFA_def_cfa_offset_sf:
674         case DW_CFA_def_cfa:
675         case DW_CFA_def_cfa_sf:
676           gcc_unreachable ();
677
678         default:
679           break;
680       }
681
682   /* Find the end of the chain.  */
683   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
684     ;
685
686   *p = cfi;
687 }
688
689 /* Generate a new label for the CFI info to refer to.  */
690
691 char *
692 dwarf2out_cfi_label (void)
693 {
694   static char label[20];
695
696   if (dwarf2out_do_cfi_asm ())
697     {
698       /* In this case, we will be emitting the asm directive instead of
699          the label, so just return a placeholder to keep the rest of the
700          interfaces happy.  */
701       strcpy (label, "<do not output>");
702     }
703   else
704     {
705       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
706       ASM_OUTPUT_LABEL (asm_out_file, label);
707     }
708
709   return label;
710 }
711
712 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
713    or to the CIE if LABEL is NULL.  */
714
715 static void
716 add_fde_cfi (const char *label, dw_cfi_ref cfi)
717 {
718   dw_cfi_ref *list_head = &cie_cfi_head;
719
720   if (dwarf2out_do_cfi_asm ())
721     {
722       if (label)
723         {
724           output_cfi_directive (cfi);
725
726           /* We still have to add the cfi to the list so that
727              lookup_cfa works later on.  */
728           list_head = &current_fde ()->dw_fde_cfi;
729         }
730       /* ??? If this is a CFI for the CIE, we don't emit.  This
731          assumes that the standard CIE contents that the assembler
732          uses matches the standard CIE contents that the compiler
733          uses.  This is probably a bad assumption.  I'm not quite
734          sure how to address this for now.  */
735     }
736   else if (label)
737     {
738       dw_fde_ref fde = current_fde ();
739
740       gcc_assert (fde != NULL);
741
742       if (*label == 0)
743         label = dwarf2out_cfi_label ();
744
745       if (fde->dw_fde_current_label == NULL
746           || strcmp (label, fde->dw_fde_current_label) != 0)
747         {
748           dw_cfi_ref xcfi;
749
750           label = xstrdup (label);
751
752           /* Set the location counter to the new label.  */
753           xcfi = new_cfi ();
754           /* If we have a current label, advance from there, otherwise
755              set the location directly using set_loc.  */
756           xcfi->dw_cfi_opc = fde->dw_fde_current_label
757                              ? DW_CFA_advance_loc4
758                              : DW_CFA_set_loc;
759           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
760           add_cfi (&fde->dw_fde_cfi, xcfi);
761
762           fde->dw_fde_current_label = label;
763         }
764
765       list_head = &fde->dw_fde_cfi;
766     }
767
768   add_cfi (list_head, cfi);
769 }
770
771 /* Subroutine of lookup_cfa.  */
772
773 static void
774 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
775 {
776   switch (cfi->dw_cfi_opc)
777     {
778     case DW_CFA_def_cfa_offset:
779     case DW_CFA_def_cfa_offset_sf:
780       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
781       break;
782     case DW_CFA_def_cfa_register:
783       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
784       break;
785     case DW_CFA_def_cfa:
786     case DW_CFA_def_cfa_sf:
787       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
788       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
789       break;
790     case DW_CFA_def_cfa_expression:
791       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
792       break;
793     default:
794       break;
795     }
796 }
797
798 /* Find the previous value for the CFA.  */
799
800 static void
801 lookup_cfa (dw_cfa_location *loc)
802 {
803   dw_cfi_ref cfi;
804   dw_fde_ref fde;
805
806   loc->reg = INVALID_REGNUM;
807   loc->offset = 0;
808   loc->indirect = 0;
809   loc->base_offset = 0;
810
811   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
812     lookup_cfa_1 (cfi, loc);
813
814   fde = current_fde ();
815   if (fde)
816     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
817       lookup_cfa_1 (cfi, loc);
818 }
819
820 /* The current rule for calculating the DWARF2 canonical frame address.  */
821 static dw_cfa_location cfa;
822
823 /* The register used for saving registers to the stack, and its offset
824    from the CFA.  */
825 static dw_cfa_location cfa_store;
826
827 /* The running total of the size of arguments pushed onto the stack.  */
828 static HOST_WIDE_INT args_size;
829
830 /* The last args_size we actually output.  */
831 static HOST_WIDE_INT old_args_size;
832
833 /* Entry point to update the canonical frame address (CFA).
834    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
835    calculated from REG+OFFSET.  */
836
837 void
838 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
839 {
840   dw_cfa_location loc;
841   loc.indirect = 0;
842   loc.base_offset = 0;
843   loc.reg = reg;
844   loc.offset = offset;
845   def_cfa_1 (label, &loc);
846 }
847
848 /* Determine if two dw_cfa_location structures define the same data.  */
849
850 static bool
851 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
852 {
853   return (loc1->reg == loc2->reg
854           && loc1->offset == loc2->offset
855           && loc1->indirect == loc2->indirect
856           && (loc1->indirect == 0
857               || loc1->base_offset == loc2->base_offset));
858 }
859
860 /* This routine does the actual work.  The CFA is now calculated from
861    the dw_cfa_location structure.  */
862
863 static void
864 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
865 {
866   dw_cfi_ref cfi;
867   dw_cfa_location old_cfa, loc;
868
869   cfa = *loc_p;
870   loc = *loc_p;
871
872   if (cfa_store.reg == loc.reg && loc.indirect == 0)
873     cfa_store.offset = loc.offset;
874
875   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
876   lookup_cfa (&old_cfa);
877
878   /* If nothing changed, no need to issue any call frame instructions.  */
879   if (cfa_equal_p (&loc, &old_cfa))
880     return;
881
882   cfi = new_cfi ();
883
884   if (loc.reg == old_cfa.reg && !loc.indirect)
885     {
886       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
887          the CFA register did not change but the offset did.  The data 
888          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
889          in the assembler via the .cfi_def_cfa_offset directive.  */
890       if (loc.offset < 0)
891         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
892       else
893         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
894       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
895     }
896
897 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
898   else if (loc.offset == old_cfa.offset
899            && old_cfa.reg != INVALID_REGNUM
900            && !loc.indirect)
901     {
902       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
903          indicating the CFA register has changed to <register> but the
904          offset has not changed.  */
905       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
906       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
907     }
908 #endif
909
910   else if (loc.indirect == 0)
911     {
912       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
913          indicating the CFA register has changed to <register> with
914          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
915          happens in output_cfi, or in the assembler via the .cfi_def_cfa
916          directive.  */
917       if (loc.offset < 0)
918         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
919       else
920         cfi->dw_cfi_opc = DW_CFA_def_cfa;
921       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
922       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
923     }
924   else
925     {
926       /* Construct a DW_CFA_def_cfa_expression instruction to
927          calculate the CFA using a full location expression since no
928          register-offset pair is available.  */
929       struct dw_loc_descr_struct *loc_list;
930
931       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
932       loc_list = build_cfa_loc (&loc, 0);
933       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
934     }
935
936   add_fde_cfi (label, cfi);
937 }
938
939 /* Add the CFI for saving a register.  REG is the CFA column number.
940    LABEL is passed to add_fde_cfi.
941    If SREG is -1, the register is saved at OFFSET from the CFA;
942    otherwise it is saved in SREG.  */
943
944 static void
945 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
946 {
947   dw_cfi_ref cfi = new_cfi ();
948   dw_fde_ref fde = current_fde ();
949
950   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
951
952   /* When stack is aligned, store REG using DW_CFA_expression with
953      FP.  */
954   if (fde
955       && fde->stack_realign
956       && sreg == INVALID_REGNUM)
957     {
958       cfi->dw_cfi_opc = DW_CFA_expression;
959       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
960       cfi->dw_cfi_oprnd1.dw_cfi_loc
961         = build_cfa_aligned_loc (offset, fde->stack_realignment);
962     }
963   else if (sreg == INVALID_REGNUM)
964     {
965       if (offset < 0)
966         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
967       else if (reg & ~0x3f)
968         cfi->dw_cfi_opc = DW_CFA_offset_extended;
969       else
970         cfi->dw_cfi_opc = DW_CFA_offset;
971       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
972     }
973   else if (sreg == reg)
974     cfi->dw_cfi_opc = DW_CFA_same_value;
975   else
976     {
977       cfi->dw_cfi_opc = DW_CFA_register;
978       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
979     }
980
981   add_fde_cfi (label, cfi);
982 }
983
984 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
985    This CFI tells the unwinder that it needs to restore the window registers
986    from the previous frame's window save area.
987
988    ??? Perhaps we should note in the CIE where windows are saved (instead of
989    assuming 0(cfa)) and what registers are in the window.  */
990
991 void
992 dwarf2out_window_save (const char *label)
993 {
994   dw_cfi_ref cfi = new_cfi ();
995
996   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
997   add_fde_cfi (label, cfi);
998 }
999
1000 /* Add a CFI to update the running total of the size of arguments
1001    pushed onto the stack.  */
1002
1003 void
1004 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1005 {
1006   dw_cfi_ref cfi;
1007
1008   if (size == old_args_size)
1009     return;
1010
1011   old_args_size = size;
1012
1013   cfi = new_cfi ();
1014   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1015   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1016   add_fde_cfi (label, cfi);
1017 }
1018
1019 /* Entry point for saving a register to the stack.  REG is the GCC register
1020    number.  LABEL and OFFSET are passed to reg_save.  */
1021
1022 void
1023 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1024 {
1025   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1026 }
1027
1028 /* Entry point for saving the return address in the stack.
1029    LABEL and OFFSET are passed to reg_save.  */
1030
1031 void
1032 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1033 {
1034   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1035 }
1036
1037 /* Entry point for saving the return address in a register.
1038    LABEL and SREG are passed to reg_save.  */
1039
1040 void
1041 dwarf2out_return_reg (const char *label, unsigned int sreg)
1042 {
1043   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1044 }
1045
1046 #ifdef DWARF2_UNWIND_INFO
1047 /* Record the initial position of the return address.  RTL is
1048    INCOMING_RETURN_ADDR_RTX.  */
1049
1050 static void
1051 initial_return_save (rtx rtl)
1052 {
1053   unsigned int reg = INVALID_REGNUM;
1054   HOST_WIDE_INT offset = 0;
1055
1056   switch (GET_CODE (rtl))
1057     {
1058     case REG:
1059       /* RA is in a register.  */
1060       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1061       break;
1062
1063     case MEM:
1064       /* RA is on the stack.  */
1065       rtl = XEXP (rtl, 0);
1066       switch (GET_CODE (rtl))
1067         {
1068         case REG:
1069           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1070           offset = 0;
1071           break;
1072
1073         case PLUS:
1074           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1075           offset = INTVAL (XEXP (rtl, 1));
1076           break;
1077
1078         case MINUS:
1079           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1080           offset = -INTVAL (XEXP (rtl, 1));
1081           break;
1082
1083         default:
1084           gcc_unreachable ();
1085         }
1086
1087       break;
1088
1089     case PLUS:
1090       /* The return address is at some offset from any value we can
1091          actually load.  For instance, on the SPARC it is in %i7+8. Just
1092          ignore the offset for now; it doesn't matter for unwinding frames.  */
1093       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1094       initial_return_save (XEXP (rtl, 0));
1095       return;
1096
1097     default:
1098       gcc_unreachable ();
1099     }
1100
1101   if (reg != DWARF_FRAME_RETURN_COLUMN)
1102     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1103 }
1104 #endif
1105
1106 /* Given a SET, calculate the amount of stack adjustment it
1107    contains.  */
1108
1109 static HOST_WIDE_INT
1110 stack_adjust_offset (const_rtx pattern)
1111 {
1112   const_rtx src = SET_SRC (pattern);
1113   const_rtx dest = SET_DEST (pattern);
1114   HOST_WIDE_INT offset = 0;
1115   enum rtx_code code;
1116
1117   if (dest == stack_pointer_rtx)
1118     {
1119       /* (set (reg sp) (plus (reg sp) (const_int))) */
1120       code = GET_CODE (src);
1121       if (! (code == PLUS || code == MINUS)
1122           || XEXP (src, 0) != stack_pointer_rtx
1123           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1124         return 0;
1125
1126       offset = INTVAL (XEXP (src, 1));
1127       if (code == PLUS)
1128         offset = -offset;
1129     }
1130   else if (MEM_P (dest))
1131     {
1132       /* (set (mem (pre_dec (reg sp))) (foo)) */
1133       src = XEXP (dest, 0);
1134       code = GET_CODE (src);
1135
1136       switch (code)
1137         {
1138         case PRE_MODIFY:
1139         case POST_MODIFY:
1140           if (XEXP (src, 0) == stack_pointer_rtx)
1141             {
1142               rtx val = XEXP (XEXP (src, 1), 1);
1143               /* We handle only adjustments by constant amount.  */
1144               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1145                           && GET_CODE (val) == CONST_INT);
1146               offset = -INTVAL (val);
1147               break;
1148             }
1149           return 0;
1150
1151         case PRE_DEC:
1152         case POST_DEC:
1153           if (XEXP (src, 0) == stack_pointer_rtx)
1154             {
1155               offset = GET_MODE_SIZE (GET_MODE (dest));
1156               break;
1157             }
1158           return 0;
1159
1160         case PRE_INC:
1161         case POST_INC:
1162           if (XEXP (src, 0) == stack_pointer_rtx)
1163             {
1164               offset = -GET_MODE_SIZE (GET_MODE (dest));
1165               break;
1166             }
1167           return 0;
1168
1169         default:
1170           return 0;
1171         }
1172     }
1173   else
1174     return 0;
1175
1176   return offset;
1177 }
1178
1179 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1180    indexed by INSN_UID.  */
1181
1182 static HOST_WIDE_INT *barrier_args_size;
1183
1184 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1185
1186 static HOST_WIDE_INT
1187 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1188                              VEC (rtx, heap) **next)
1189 {
1190   HOST_WIDE_INT offset = 0;
1191   int i;
1192
1193   if (! RTX_FRAME_RELATED_P (insn))
1194     {
1195       if (prologue_epilogue_contains (insn)
1196           || sibcall_epilogue_contains (insn))
1197         /* Nothing */;
1198       else if (GET_CODE (PATTERN (insn)) == SET)
1199         offset = stack_adjust_offset (PATTERN (insn));
1200       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1201                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1202         {
1203           /* There may be stack adjustments inside compound insns.  Search
1204              for them.  */
1205           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1206             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1207               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1208         }
1209     }
1210   else
1211     {
1212       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1213
1214       if (expr)
1215         {
1216           expr = XEXP (expr, 0);
1217           if (GET_CODE (expr) == PARALLEL
1218               || GET_CODE (expr) == SEQUENCE)
1219             for (i = 1; i < XVECLEN (expr, 0); i++)
1220               {
1221                 rtx elem = XVECEXP (expr, 0, i);
1222
1223                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1224                   offset += stack_adjust_offset (elem);
1225               }
1226         }
1227     }
1228
1229 #ifndef STACK_GROWS_DOWNWARD
1230   offset = -offset;
1231 #endif
1232
1233   cur_args_size += offset;
1234   if (cur_args_size < 0)
1235     cur_args_size = 0;
1236
1237   if (JUMP_P (insn))
1238     {
1239       rtx dest = JUMP_LABEL (insn);
1240
1241       if (dest)
1242         {
1243           if (barrier_args_size [INSN_UID (dest)] < 0)
1244             {
1245               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1246               VEC_safe_push (rtx, heap, *next, dest);
1247             }
1248         }
1249     }
1250
1251   return cur_args_size;
1252 }
1253
1254 /* Walk the whole function and compute args_size on BARRIERs.  */
1255
1256 static void
1257 compute_barrier_args_size (void)
1258 {
1259   int max_uid = get_max_uid (), i;
1260   rtx insn;
1261   VEC (rtx, heap) *worklist, *next, *tmp;
1262
1263   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1264   for (i = 0; i < max_uid; i++)
1265     barrier_args_size[i] = -1;
1266
1267   worklist = VEC_alloc (rtx, heap, 20);
1268   next = VEC_alloc (rtx, heap, 20);
1269   insn = get_insns ();
1270   barrier_args_size[INSN_UID (insn)] = 0;
1271   VEC_quick_push (rtx, worklist, insn);
1272   for (;;)
1273     {
1274       while (!VEC_empty (rtx, worklist))
1275         {
1276           rtx prev, body, first_insn;
1277           HOST_WIDE_INT cur_args_size;
1278
1279           first_insn = insn = VEC_pop (rtx, worklist);
1280           cur_args_size = barrier_args_size[INSN_UID (insn)];
1281           prev = prev_nonnote_insn (insn);
1282           if (prev && BARRIER_P (prev))
1283             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1284
1285           for (; insn; insn = NEXT_INSN (insn))
1286             {
1287               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1288                 continue;
1289               if (BARRIER_P (insn))
1290                 break;
1291
1292               if (LABEL_P (insn))
1293                 {
1294                   if (insn == first_insn)
1295                     continue;
1296                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1297                     {
1298                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1299                       continue;
1300                     }
1301                   else
1302                     {
1303                       /* The insns starting with this label have been
1304                          already scanned or are in the worklist.  */
1305                       break;
1306                     }
1307                 }
1308
1309               body = PATTERN (insn);
1310               if (GET_CODE (body) == SEQUENCE)
1311                 {
1312                   for (i = 1; i < XVECLEN (body, 0); i++)
1313                     cur_args_size
1314                       = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1315                                                      cur_args_size, &next);
1316                   cur_args_size
1317                     = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1318                                                    cur_args_size, &next);
1319                 }
1320               else
1321                 cur_args_size
1322                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1323             }
1324         }
1325
1326       if (VEC_empty (rtx, next))
1327         break;
1328
1329       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1330       tmp = next;
1331       next = worklist;
1332       worklist = tmp;
1333       VEC_truncate (rtx, next, 0);
1334     }
1335
1336   VEC_free (rtx, heap, worklist);
1337   VEC_free (rtx, heap, next);
1338 }
1339
1340
1341 /* Check INSN to see if it looks like a push or a stack adjustment, and
1342    make a note of it if it does.  EH uses this information to find out how
1343    much extra space it needs to pop off the stack.  */
1344
1345 static void
1346 dwarf2out_stack_adjust (rtx insn, bool after_p)
1347 {
1348   HOST_WIDE_INT offset;
1349   const char *label;
1350   int i;
1351
1352   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1353      with this function.  Proper support would require all frame-related
1354      insns to be marked, and to be able to handle saving state around
1355      epilogues textually in the middle of the function.  */
1356   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1357     return;
1358
1359   /* If only calls can throw, and we have a frame pointer,
1360      save up adjustments until we see the CALL_INSN.  */
1361   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1362     {
1363       if (CALL_P (insn) && !after_p)
1364         {
1365           /* Extract the size of the args from the CALL rtx itself.  */
1366           insn = PATTERN (insn);
1367           if (GET_CODE (insn) == PARALLEL)
1368             insn = XVECEXP (insn, 0, 0);
1369           if (GET_CODE (insn) == SET)
1370             insn = SET_SRC (insn);
1371           gcc_assert (GET_CODE (insn) == CALL);
1372           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1373         }
1374       return;
1375     }
1376
1377   if (CALL_P (insn) && !after_p)
1378     {
1379       if (!flag_asynchronous_unwind_tables)
1380         dwarf2out_args_size ("", args_size);
1381       return;
1382     }
1383   else if (BARRIER_P (insn))
1384     {
1385       /* Don't call compute_barrier_args_size () if the only
1386          BARRIER is at the end of function.  */
1387       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1388         compute_barrier_args_size ();
1389       if (barrier_args_size == NULL)
1390         offset = 0;
1391       else
1392         {
1393           offset = barrier_args_size[INSN_UID (insn)];
1394           if (offset < 0)
1395             offset = 0;
1396         }
1397
1398       offset -= args_size;
1399 #ifndef STACK_GROWS_DOWNWARD
1400       offset = -offset;
1401 #endif
1402     }
1403   else if (GET_CODE (PATTERN (insn)) == SET)
1404     offset = stack_adjust_offset (PATTERN (insn));
1405   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1406            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1407     {
1408       /* There may be stack adjustments inside compound insns.  Search
1409          for them.  */
1410       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1411         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1412           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1413     }
1414   else
1415     return;
1416
1417   if (offset == 0)
1418     return;
1419
1420   label = dwarf2out_cfi_label ();
1421   dwarf2out_args_size_adjust (offset, label);
1422 }
1423
1424 /* Adjust args_size based on stack adjustment OFFSET.  */
1425
1426 static void
1427 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1428 {
1429   if (cfa.reg == STACK_POINTER_REGNUM)
1430     cfa.offset += offset;
1431
1432   if (cfa_store.reg == STACK_POINTER_REGNUM)
1433     cfa_store.offset += offset;
1434
1435 #ifndef STACK_GROWS_DOWNWARD
1436   offset = -offset;
1437 #endif
1438
1439   args_size += offset;
1440   if (args_size < 0)
1441     args_size = 0;
1442
1443   def_cfa_1 (label, &cfa);
1444   if (flag_asynchronous_unwind_tables)
1445     dwarf2out_args_size (label, args_size);
1446 }
1447
1448 #endif
1449
1450 /* We delay emitting a register save until either (a) we reach the end
1451    of the prologue or (b) the register is clobbered.  This clusters
1452    register saves so that there are fewer pc advances.  */
1453
1454 struct queued_reg_save GTY(())
1455 {
1456   struct queued_reg_save *next;
1457   rtx reg;
1458   HOST_WIDE_INT cfa_offset;
1459   rtx saved_reg;
1460 };
1461
1462 static GTY(()) struct queued_reg_save *queued_reg_saves;
1463
1464 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1465 struct reg_saved_in_data GTY(()) {
1466   rtx orig_reg;
1467   rtx saved_in_reg;
1468 };
1469
1470 /* A list of registers saved in other registers.
1471    The list intentionally has a small maximum capacity of 4; if your
1472    port needs more than that, you might consider implementing a
1473    more efficient data structure.  */
1474 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1475 static GTY(()) size_t num_regs_saved_in_regs;
1476
1477 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1478 static const char *last_reg_save_label;
1479
1480 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1481    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1482
1483 static void
1484 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1485 {
1486   struct queued_reg_save *q;
1487
1488   /* Duplicates waste space, but it's also necessary to remove them
1489      for correctness, since the queue gets output in reverse
1490      order.  */
1491   for (q = queued_reg_saves; q != NULL; q = q->next)
1492     if (REGNO (q->reg) == REGNO (reg))
1493       break;
1494
1495   if (q == NULL)
1496     {
1497       q = GGC_NEW (struct queued_reg_save);
1498       q->next = queued_reg_saves;
1499       queued_reg_saves = q;
1500     }
1501
1502   q->reg = reg;
1503   q->cfa_offset = offset;
1504   q->saved_reg = sreg;
1505
1506   last_reg_save_label = label;
1507 }
1508
1509 /* Output all the entries in QUEUED_REG_SAVES.  */
1510
1511 static void
1512 flush_queued_reg_saves (void)
1513 {
1514   struct queued_reg_save *q;
1515
1516   for (q = queued_reg_saves; q; q = q->next)
1517     {
1518       size_t i;
1519       unsigned int reg, sreg;
1520
1521       for (i = 0; i < num_regs_saved_in_regs; i++)
1522         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1523           break;
1524       if (q->saved_reg && i == num_regs_saved_in_regs)
1525         {
1526           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1527           num_regs_saved_in_regs++;
1528         }
1529       if (i != num_regs_saved_in_regs)
1530         {
1531           regs_saved_in_regs[i].orig_reg = q->reg;
1532           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1533         }
1534
1535       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1536       if (q->saved_reg)
1537         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1538       else
1539         sreg = INVALID_REGNUM;
1540       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1541     }
1542
1543   queued_reg_saves = NULL;
1544   last_reg_save_label = NULL;
1545 }
1546
1547 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1548    location for?  Or, does it clobber a register which we've previously
1549    said that some other register is saved in, and for which we now
1550    have a new location for?  */
1551
1552 static bool
1553 clobbers_queued_reg_save (const_rtx insn)
1554 {
1555   struct queued_reg_save *q;
1556
1557   for (q = queued_reg_saves; q; q = q->next)
1558     {
1559       size_t i;
1560       if (modified_in_p (q->reg, insn))
1561         return true;
1562       for (i = 0; i < num_regs_saved_in_regs; i++)
1563         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1564             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1565           return true;
1566     }
1567
1568   return false;
1569 }
1570
1571 /* Entry point for saving the first register into the second.  */
1572
1573 void
1574 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1575 {
1576   size_t i;
1577   unsigned int regno, sregno;
1578
1579   for (i = 0; i < num_regs_saved_in_regs; i++)
1580     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1581       break;
1582   if (i == num_regs_saved_in_regs)
1583     {
1584       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1585       num_regs_saved_in_regs++;
1586     }
1587   regs_saved_in_regs[i].orig_reg = reg;
1588   regs_saved_in_regs[i].saved_in_reg = sreg;
1589
1590   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1591   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1592   reg_save (label, regno, sregno, 0);
1593 }
1594
1595 /* What register, if any, is currently saved in REG?  */
1596
1597 static rtx
1598 reg_saved_in (rtx reg)
1599 {
1600   unsigned int regn = REGNO (reg);
1601   size_t i;
1602   struct queued_reg_save *q;
1603
1604   for (q = queued_reg_saves; q; q = q->next)
1605     if (q->saved_reg && regn == REGNO (q->saved_reg))
1606       return q->reg;
1607
1608   for (i = 0; i < num_regs_saved_in_regs; i++)
1609     if (regs_saved_in_regs[i].saved_in_reg
1610         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1611       return regs_saved_in_regs[i].orig_reg;
1612
1613   return NULL_RTX;
1614 }
1615
1616
1617 /* A temporary register holding an integral value used in adjusting SP
1618    or setting up the store_reg.  The "offset" field holds the integer
1619    value, not an offset.  */
1620 static dw_cfa_location cfa_temp;
1621
1622 /* Record call frame debugging information for an expression EXPR,
1623    which either sets SP or FP (adjusting how we calculate the frame
1624    address) or saves a register to the stack or another register.
1625    LABEL indicates the address of EXPR.
1626
1627    This function encodes a state machine mapping rtxes to actions on
1628    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1629    users need not read the source code.
1630
1631   The High-Level Picture
1632
1633   Changes in the register we use to calculate the CFA: Currently we
1634   assume that if you copy the CFA register into another register, we
1635   should take the other one as the new CFA register; this seems to
1636   work pretty well.  If it's wrong for some target, it's simple
1637   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1638
1639   Changes in the register we use for saving registers to the stack:
1640   This is usually SP, but not always.  Again, we deduce that if you
1641   copy SP into another register (and SP is not the CFA register),
1642   then the new register is the one we will be using for register
1643   saves.  This also seems to work.
1644
1645   Register saves: There's not much guesswork about this one; if
1646   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1647   register save, and the register used to calculate the destination
1648   had better be the one we think we're using for this purpose.
1649   It's also assumed that a copy from a call-saved register to another
1650   register is saving that register if RTX_FRAME_RELATED_P is set on
1651   that instruction.  If the copy is from a call-saved register to
1652   the *same* register, that means that the register is now the same
1653   value as in the caller.
1654
1655   Except: If the register being saved is the CFA register, and the
1656   offset is nonzero, we are saving the CFA, so we assume we have to
1657   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1658   the intent is to save the value of SP from the previous frame.
1659
1660   In addition, if a register has previously been saved to a different
1661   register,
1662
1663   Invariants / Summaries of Rules
1664
1665   cfa          current rule for calculating the CFA.  It usually
1666                consists of a register and an offset.
1667   cfa_store    register used by prologue code to save things to the stack
1668                cfa_store.offset is the offset from the value of
1669                cfa_store.reg to the actual CFA
1670   cfa_temp     register holding an integral value.  cfa_temp.offset
1671                stores the value, which will be used to adjust the
1672                stack pointer.  cfa_temp is also used like cfa_store,
1673                to track stores to the stack via fp or a temp reg.
1674
1675   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1676                with cfa.reg as the first operand changes the cfa.reg and its
1677                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1678                cfa_temp.offset.
1679
1680   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1681                expression yielding a constant.  This sets cfa_temp.reg
1682                and cfa_temp.offset.
1683
1684   Rule 5:      Create a new register cfa_store used to save items to the
1685                stack.
1686
1687   Rules 10-14: Save a register to the stack.  Define offset as the
1688                difference of the original location and cfa_store's
1689                location (or cfa_temp's location if cfa_temp is used).
1690
1691   Rules 16-20: If AND operation happens on sp in prologue, we assume
1692                stack is realigned.  We will use a group of DW_OP_XXX
1693                expressions to represent the location of the stored
1694                register instead of CFA+offset.
1695
1696   The Rules
1697
1698   "{a,b}" indicates a choice of a xor b.
1699   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1700
1701   Rule 1:
1702   (set <reg1> <reg2>:cfa.reg)
1703   effects: cfa.reg = <reg1>
1704            cfa.offset unchanged
1705            cfa_temp.reg = <reg1>
1706            cfa_temp.offset = cfa.offset
1707
1708   Rule 2:
1709   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1710                               {<const_int>,<reg>:cfa_temp.reg}))
1711   effects: cfa.reg = sp if fp used
1712            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1713            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1714              if cfa_store.reg==sp
1715
1716   Rule 3:
1717   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1718   effects: cfa.reg = fp
1719            cfa_offset += +/- <const_int>
1720
1721   Rule 4:
1722   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1723   constraints: <reg1> != fp
1724                <reg1> != sp
1725   effects: cfa.reg = <reg1>
1726            cfa_temp.reg = <reg1>
1727            cfa_temp.offset = cfa.offset
1728
1729   Rule 5:
1730   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1731   constraints: <reg1> != fp
1732                <reg1> != sp
1733   effects: cfa_store.reg = <reg1>
1734            cfa_store.offset = cfa.offset - cfa_temp.offset
1735
1736   Rule 6:
1737   (set <reg> <const_int>)
1738   effects: cfa_temp.reg = <reg>
1739            cfa_temp.offset = <const_int>
1740
1741   Rule 7:
1742   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1743   effects: cfa_temp.reg = <reg1>
1744            cfa_temp.offset |= <const_int>
1745
1746   Rule 8:
1747   (set <reg> (high <exp>))
1748   effects: none
1749
1750   Rule 9:
1751   (set <reg> (lo_sum <exp> <const_int>))
1752   effects: cfa_temp.reg = <reg>
1753            cfa_temp.offset = <const_int>
1754
1755   Rule 10:
1756   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1757   effects: cfa_store.offset -= <const_int>
1758            cfa.offset = cfa_store.offset if cfa.reg == sp
1759            cfa.reg = sp
1760            cfa.base_offset = -cfa_store.offset
1761
1762   Rule 11:
1763   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1764   effects: cfa_store.offset += -/+ mode_size(mem)
1765            cfa.offset = cfa_store.offset if cfa.reg == sp
1766            cfa.reg = sp
1767            cfa.base_offset = -cfa_store.offset
1768
1769   Rule 12:
1770   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1771
1772        <reg2>)
1773   effects: cfa.reg = <reg1>
1774            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1775
1776   Rule 13:
1777   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1778   effects: cfa.reg = <reg1>
1779            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1780
1781   Rule 14:
1782   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1783   effects: cfa.reg = <reg1>
1784            cfa.base_offset = -cfa_temp.offset
1785            cfa_temp.offset -= mode_size(mem)
1786
1787   Rule 15:
1788   (set <reg> {unspec, unspec_volatile})
1789   effects: target-dependent
1790
1791   Rule 16:
1792   (set sp (and: sp <const_int>))
1793   constraints: cfa_store.reg == sp
1794   effects: current_fde.stack_realign = 1
1795            cfa_store.offset = 0
1796            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1797
1798   Rule 17:
1799   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1800   effects: cfa_store.offset += -/+ mode_size(mem)
1801
1802   Rule 18:
1803   (set (mem ({pre_inc, pre_dec} sp)) fp)
1804   constraints: fde->stack_realign == 1
1805   effects: cfa_store.offset = 0
1806            cfa.reg != HARD_FRAME_POINTER_REGNUM
1807
1808   Rule 19:
1809   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1810   constraints: fde->stack_realign == 1
1811                && cfa.offset == 0
1812                && cfa.indirect == 0
1813                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1814   effects: Use DW_CFA_def_cfa_expression to define cfa
1815            cfa.reg == fde->drap_reg
1816
1817   Rule 20:
1818   (set reg fde->drap_reg)
1819   constraints: fde->vdrap_reg == INVALID_REGNUM
1820   effects: fde->vdrap_reg = reg.
1821   (set mem fde->drap_reg)
1822   constraints: fde->drap_reg_saved == 1
1823   effects: none.  */
1824
1825 static void
1826 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1827 {
1828   rtx src, dest, span;
1829   HOST_WIDE_INT offset;
1830   dw_fde_ref fde;
1831
1832   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1833      the PARALLEL independently. The first element is always processed if
1834      it is a SET. This is for backward compatibility.   Other elements
1835      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1836      flag is set in them.  */
1837   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1838     {
1839       int par_index;
1840       int limit = XVECLEN (expr, 0);
1841       rtx elem;
1842
1843       /* PARALLELs have strict read-modify-write semantics, so we
1844          ought to evaluate every rvalue before changing any lvalue.
1845          It's cumbersome to do that in general, but there's an
1846          easy approximation that is enough for all current users:
1847          handle register saves before register assignments.  */
1848       if (GET_CODE (expr) == PARALLEL)
1849         for (par_index = 0; par_index < limit; par_index++)
1850           {
1851             elem = XVECEXP (expr, 0, par_index);
1852             if (GET_CODE (elem) == SET
1853                 && MEM_P (SET_DEST (elem))
1854                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1855               dwarf2out_frame_debug_expr (elem, label);
1856           }
1857
1858       for (par_index = 0; par_index < limit; par_index++)
1859         {
1860           elem = XVECEXP (expr, 0, par_index);
1861           if (GET_CODE (elem) == SET
1862               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1863               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1864             dwarf2out_frame_debug_expr (elem, label);
1865           else if (GET_CODE (elem) == SET
1866                    && par_index != 0
1867                    && !RTX_FRAME_RELATED_P (elem))
1868             {
1869               /* Stack adjustment combining might combine some post-prologue
1870                  stack adjustment into a prologue stack adjustment.  */
1871               HOST_WIDE_INT offset = stack_adjust_offset (elem);
1872
1873               if (offset != 0)
1874                 dwarf2out_args_size_adjust (offset, label);
1875             }
1876         }
1877       return;
1878     }
1879
1880   gcc_assert (GET_CODE (expr) == SET);
1881
1882   src = SET_SRC (expr);
1883   dest = SET_DEST (expr);
1884
1885   if (REG_P (src))
1886     {
1887       rtx rsi = reg_saved_in (src);
1888       if (rsi)
1889         src = rsi;
1890     }
1891
1892   fde = current_fde ();
1893
1894   if (GET_CODE (src) == REG
1895       && fde
1896       && fde->drap_reg == REGNO (src)
1897       && (fde->drap_reg_saved
1898           || GET_CODE (dest) == REG))
1899     {
1900       /* Rule 20 */
1901       /* If we are saving dynamic realign argument pointer to a
1902          register, the destination is virtual dynamic realign
1903          argument pointer.  It may be used to access argument.  */
1904       if (GET_CODE (dest) == REG)
1905         {
1906           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1907           fde->vdrap_reg = REGNO (dest);
1908         }
1909       return;
1910     }
1911
1912   switch (GET_CODE (dest))
1913     {
1914     case REG:
1915       switch (GET_CODE (src))
1916         {
1917           /* Setting FP from SP.  */
1918         case REG:
1919           if (cfa.reg == (unsigned) REGNO (src))
1920             {
1921               /* Rule 1 */
1922               /* Update the CFA rule wrt SP or FP.  Make sure src is
1923                  relative to the current CFA register.
1924
1925                  We used to require that dest be either SP or FP, but the
1926                  ARM copies SP to a temporary register, and from there to
1927                  FP.  So we just rely on the backends to only set
1928                  RTX_FRAME_RELATED_P on appropriate insns.  */
1929               cfa.reg = REGNO (dest);
1930               cfa_temp.reg = cfa.reg;
1931               cfa_temp.offset = cfa.offset;
1932             }
1933           else
1934             {
1935               /* Saving a register in a register.  */
1936               gcc_assert (!fixed_regs [REGNO (dest)]
1937                           /* For the SPARC and its register window.  */
1938                           || (DWARF_FRAME_REGNUM (REGNO (src))
1939                               == DWARF_FRAME_RETURN_COLUMN));
1940
1941               /* After stack is aligned, we can only save SP in FP
1942                  if drap register is used.  In this case, we have
1943                  to restore stack pointer with the CFA value and we
1944                  don't generate this DWARF information.  */
1945               if (fde
1946                   && fde->stack_realign
1947                   && REGNO (src) == STACK_POINTER_REGNUM)
1948                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1949                             && fde->drap_reg != INVALID_REGNUM
1950                             && cfa.reg != REGNO (src));
1951               else
1952                 queue_reg_save (label, src, dest, 0);
1953             }
1954           break;
1955
1956         case PLUS:
1957         case MINUS:
1958         case LO_SUM:
1959           if (dest == stack_pointer_rtx)
1960             {
1961               /* Rule 2 */
1962               /* Adjusting SP.  */
1963               switch (GET_CODE (XEXP (src, 1)))
1964                 {
1965                 case CONST_INT:
1966                   offset = INTVAL (XEXP (src, 1));
1967                   break;
1968                 case REG:
1969                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1970                               == cfa_temp.reg);
1971                   offset = cfa_temp.offset;
1972                   break;
1973                 default:
1974                   gcc_unreachable ();
1975                 }
1976
1977               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1978                 {
1979                   /* Restoring SP from FP in the epilogue.  */
1980                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1981                   cfa.reg = STACK_POINTER_REGNUM;
1982                 }
1983               else if (GET_CODE (src) == LO_SUM)
1984                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1985                 ;
1986               else
1987                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1988
1989               if (GET_CODE (src) != MINUS)
1990                 offset = -offset;
1991               if (cfa.reg == STACK_POINTER_REGNUM)
1992                 cfa.offset += offset;
1993               if (cfa_store.reg == STACK_POINTER_REGNUM)
1994                 cfa_store.offset += offset;
1995             }
1996           else if (dest == hard_frame_pointer_rtx)
1997             {
1998               /* Rule 3 */
1999               /* Either setting the FP from an offset of the SP,
2000                  or adjusting the FP */
2001               gcc_assert (frame_pointer_needed);
2002
2003               gcc_assert (REG_P (XEXP (src, 0))
2004                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2005                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2006               offset = INTVAL (XEXP (src, 1));
2007               if (GET_CODE (src) != MINUS)
2008                 offset = -offset;
2009               cfa.offset += offset;
2010               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2011             }
2012           else
2013             {
2014               gcc_assert (GET_CODE (src) != MINUS);
2015
2016               /* Rule 4 */
2017               if (REG_P (XEXP (src, 0))
2018                   && REGNO (XEXP (src, 0)) == cfa.reg
2019                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2020                 {
2021                   /* Setting a temporary CFA register that will be copied
2022                      into the FP later on.  */
2023                   offset = - INTVAL (XEXP (src, 1));
2024                   cfa.offset += offset;
2025                   cfa.reg = REGNO (dest);
2026                   /* Or used to save regs to the stack.  */
2027                   cfa_temp.reg = cfa.reg;
2028                   cfa_temp.offset = cfa.offset;
2029                 }
2030
2031               /* Rule 5 */
2032               else if (REG_P (XEXP (src, 0))
2033                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2034                        && XEXP (src, 1) == stack_pointer_rtx)
2035                 {
2036                   /* Setting a scratch register that we will use instead
2037                      of SP for saving registers to the stack.  */
2038                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2039                   cfa_store.reg = REGNO (dest);
2040                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2041                 }
2042
2043               /* Rule 9 */
2044               else if (GET_CODE (src) == LO_SUM
2045                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2046                 {
2047                   cfa_temp.reg = REGNO (dest);
2048                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2049                 }
2050               else
2051                 gcc_unreachable ();
2052             }
2053           break;
2054
2055           /* Rule 6 */
2056         case CONST_INT:
2057           cfa_temp.reg = REGNO (dest);
2058           cfa_temp.offset = INTVAL (src);
2059           break;
2060
2061           /* Rule 7 */
2062         case IOR:
2063           gcc_assert (REG_P (XEXP (src, 0))
2064                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2065                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2066
2067           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2068             cfa_temp.reg = REGNO (dest);
2069           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2070           break;
2071
2072           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2073              which will fill in all of the bits.  */
2074           /* Rule 8 */
2075         case HIGH:
2076           break;
2077
2078           /* Rule 15 */
2079         case UNSPEC:
2080         case UNSPEC_VOLATILE:
2081           gcc_assert (targetm.dwarf_handle_frame_unspec);
2082           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2083           return;
2084
2085           /* Rule 16 */
2086         case AND:
2087           /* If this AND operation happens on stack pointer in prologue,
2088              we assume the stack is realigned and we extract the
2089              alignment.  */
2090           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2091             {
2092               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2093               fde->stack_realign = 1;
2094               fde->stack_realignment = INTVAL (XEXP (src, 1));
2095               cfa_store.offset = 0;
2096
2097               if (cfa.reg != STACK_POINTER_REGNUM
2098                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2099                 fde->drap_reg = cfa.reg;
2100             }
2101           return;
2102
2103         default:
2104           gcc_unreachable ();
2105         }
2106
2107       def_cfa_1 (label, &cfa);
2108       break;
2109
2110     case MEM:
2111
2112       /* Saving a register to the stack.  Make sure dest is relative to the
2113          CFA register.  */
2114       switch (GET_CODE (XEXP (dest, 0)))
2115         {
2116           /* Rule 10 */
2117           /* With a push.  */
2118         case PRE_MODIFY:
2119           /* We can't handle variable size modifications.  */
2120           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2121                       == CONST_INT);
2122           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2123
2124           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2125                       && cfa_store.reg == STACK_POINTER_REGNUM);
2126
2127           cfa_store.offset += offset;
2128           if (cfa.reg == STACK_POINTER_REGNUM)
2129             cfa.offset = cfa_store.offset;
2130
2131           offset = -cfa_store.offset;
2132           break;
2133
2134           /* Rule 11 */
2135         case PRE_INC:
2136         case PRE_DEC:
2137           offset = GET_MODE_SIZE (GET_MODE (dest));
2138           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2139             offset = -offset;
2140
2141           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2142                        == STACK_POINTER_REGNUM)
2143                       && cfa_store.reg == STACK_POINTER_REGNUM);
2144
2145           cfa_store.offset += offset;
2146
2147           /* Rule 18: If stack is aligned, we will use FP as a
2148              reference to represent the address of the stored
2149              regiser.  */
2150           if (fde
2151               && fde->stack_realign
2152               && src == hard_frame_pointer_rtx)
2153             {
2154               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2155               cfa_store.offset = 0;
2156             }
2157
2158           if (cfa.reg == STACK_POINTER_REGNUM)
2159             cfa.offset = cfa_store.offset;
2160
2161           offset = -cfa_store.offset;
2162           break;
2163
2164           /* Rule 12 */
2165           /* With an offset.  */
2166         case PLUS:
2167         case MINUS:
2168         case LO_SUM:
2169           {
2170             int regno;
2171
2172             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2173                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2174             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2175             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2176               offset = -offset;
2177
2178             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2179
2180             if (cfa_store.reg == (unsigned) regno)
2181               offset -= cfa_store.offset;
2182             else
2183               {
2184                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2185                 offset -= cfa_temp.offset;
2186               }
2187           }
2188           break;
2189
2190           /* Rule 13 */
2191           /* Without an offset.  */
2192         case REG:
2193           {
2194             int regno = REGNO (XEXP (dest, 0));
2195
2196             if (cfa_store.reg == (unsigned) regno)
2197               offset = -cfa_store.offset;
2198             else
2199               {
2200                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2201                 offset = -cfa_temp.offset;
2202               }
2203           }
2204           break;
2205
2206           /* Rule 14 */
2207         case POST_INC:
2208           gcc_assert (cfa_temp.reg
2209                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2210           offset = -cfa_temp.offset;
2211           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2212           break;
2213
2214         default:
2215           gcc_unreachable ();
2216         }
2217
2218         /* Rule 17 */
2219         /* If the source operand of this MEM operation is not a
2220            register, basically the source is return address.  Here
2221            we only care how much stack grew and we don't save it.  */
2222       if (!REG_P (src))
2223         break;
2224
2225       if (REGNO (src) != STACK_POINTER_REGNUM
2226           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2227           && (unsigned) REGNO (src) == cfa.reg)
2228         {
2229           /* We're storing the current CFA reg into the stack.  */
2230
2231           if (cfa.offset == 0)
2232             {
2233               /* Rule 19 */
2234               /* If stack is aligned, putting CFA reg into stack means
2235                  we can no longer use reg + offset to represent CFA.
2236                  Here we use DW_CFA_def_cfa_expression instead.  The
2237                  result of this expression equals to the original CFA
2238                  value.  */
2239               if (fde
2240                   && fde->stack_realign
2241                   && cfa.indirect == 0
2242                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2243                 {
2244                   dw_cfa_location cfa_exp;
2245
2246                   gcc_assert (fde->drap_reg == cfa.reg);
2247
2248                   cfa_exp.indirect = 1;
2249                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2250                   cfa_exp.base_offset = offset;
2251                   cfa_exp.offset = 0;
2252
2253                   fde->drap_reg_saved = 1;
2254
2255                   def_cfa_1 (label, &cfa_exp);
2256                   break;
2257                 }
2258
2259               /* If the source register is exactly the CFA, assume
2260                  we're saving SP like any other register; this happens
2261                  on the ARM.  */
2262               def_cfa_1 (label, &cfa);
2263               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2264               break;
2265             }
2266           else
2267             {
2268               /* Otherwise, we'll need to look in the stack to
2269                  calculate the CFA.  */
2270               rtx x = XEXP (dest, 0);
2271
2272               if (!REG_P (x))
2273                 x = XEXP (x, 0);
2274               gcc_assert (REG_P (x));
2275
2276               cfa.reg = REGNO (x);
2277               cfa.base_offset = offset;
2278               cfa.indirect = 1;
2279               def_cfa_1 (label, &cfa);
2280               break;
2281             }
2282         }
2283
2284       def_cfa_1 (label, &cfa);
2285       {
2286         span = targetm.dwarf_register_span (src);
2287
2288         if (!span)
2289           queue_reg_save (label, src, NULL_RTX, offset);
2290         else
2291           {
2292             /* We have a PARALLEL describing where the contents of SRC
2293                live.  Queue register saves for each piece of the
2294                PARALLEL.  */
2295             int par_index;
2296             int limit;
2297             HOST_WIDE_INT span_offset = offset;
2298
2299             gcc_assert (GET_CODE (span) == PARALLEL);
2300
2301             limit = XVECLEN (span, 0);
2302             for (par_index = 0; par_index < limit; par_index++)
2303               {
2304                 rtx elem = XVECEXP (span, 0, par_index);
2305
2306                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2307                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2308               }
2309           }
2310       }
2311       break;
2312
2313     default:
2314       gcc_unreachable ();
2315     }
2316 }
2317
2318 /* Record call frame debugging information for INSN, which either
2319    sets SP or FP (adjusting how we calculate the frame address) or saves a
2320    register to the stack.  If INSN is NULL_RTX, initialize our state.
2321
2322    If AFTER_P is false, we're being called before the insn is emitted,
2323    otherwise after.  Call instructions get invoked twice.  */
2324
2325 void
2326 dwarf2out_frame_debug (rtx insn, bool after_p)
2327 {
2328   const char *label;
2329   rtx src;
2330
2331   if (insn == NULL_RTX)
2332     {
2333       size_t i;
2334
2335       /* Flush any queued register saves.  */
2336       flush_queued_reg_saves ();
2337
2338       /* Set up state for generating call frame debug info.  */
2339       lookup_cfa (&cfa);
2340       gcc_assert (cfa.reg
2341                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2342
2343       cfa.reg = STACK_POINTER_REGNUM;
2344       cfa_store = cfa;
2345       cfa_temp.reg = -1;
2346       cfa_temp.offset = 0;
2347
2348       for (i = 0; i < num_regs_saved_in_regs; i++)
2349         {
2350           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2351           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2352         }
2353       num_regs_saved_in_regs = 0;
2354
2355       if (barrier_args_size)
2356         {
2357           XDELETEVEC (barrier_args_size);
2358           barrier_args_size = NULL;
2359         }
2360       return;
2361     }
2362
2363   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2364     flush_queued_reg_saves ();
2365
2366   if (! RTX_FRAME_RELATED_P (insn))
2367     {
2368       if (!ACCUMULATE_OUTGOING_ARGS)
2369         dwarf2out_stack_adjust (insn, after_p);
2370       return;
2371     }
2372
2373   label = dwarf2out_cfi_label ();
2374   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2375   if (src)
2376     insn = XEXP (src, 0);
2377   else
2378     insn = PATTERN (insn);
2379
2380   dwarf2out_frame_debug_expr (insn, label);
2381 }
2382
2383 #endif
2384
2385 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2386 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2387  (enum dwarf_call_frame_info cfi);
2388
2389 static enum dw_cfi_oprnd_type
2390 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2391 {
2392   switch (cfi)
2393     {
2394     case DW_CFA_nop:
2395     case DW_CFA_GNU_window_save:
2396       return dw_cfi_oprnd_unused;
2397
2398     case DW_CFA_set_loc:
2399     case DW_CFA_advance_loc1:
2400     case DW_CFA_advance_loc2:
2401     case DW_CFA_advance_loc4:
2402     case DW_CFA_MIPS_advance_loc8:
2403       return dw_cfi_oprnd_addr;
2404
2405     case DW_CFA_offset:
2406     case DW_CFA_offset_extended:
2407     case DW_CFA_def_cfa:
2408     case DW_CFA_offset_extended_sf:
2409     case DW_CFA_def_cfa_sf:
2410     case DW_CFA_restore_extended:
2411     case DW_CFA_undefined:
2412     case DW_CFA_same_value:
2413     case DW_CFA_def_cfa_register:
2414     case DW_CFA_register:
2415       return dw_cfi_oprnd_reg_num;
2416
2417     case DW_CFA_def_cfa_offset:
2418     case DW_CFA_GNU_args_size:
2419     case DW_CFA_def_cfa_offset_sf:
2420       return dw_cfi_oprnd_offset;
2421
2422     case DW_CFA_def_cfa_expression:
2423     case DW_CFA_expression:
2424       return dw_cfi_oprnd_loc;
2425
2426     default:
2427       gcc_unreachable ();
2428     }
2429 }
2430
2431 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2432 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2433  (enum dwarf_call_frame_info cfi);
2434
2435 static enum dw_cfi_oprnd_type
2436 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2437 {
2438   switch (cfi)
2439     {
2440     case DW_CFA_def_cfa:
2441     case DW_CFA_def_cfa_sf:
2442     case DW_CFA_offset:
2443     case DW_CFA_offset_extended_sf:
2444     case DW_CFA_offset_extended:
2445       return dw_cfi_oprnd_offset;
2446
2447     case DW_CFA_register:
2448       return dw_cfi_oprnd_reg_num;
2449
2450     default:
2451       return dw_cfi_oprnd_unused;
2452     }
2453 }
2454
2455 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2456
2457 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2458    switch to the data section instead, and write out a synthetic label
2459    for collect2.  */
2460
2461 static void
2462 switch_to_eh_frame_section (void)
2463 {
2464   tree label;
2465
2466 #ifdef EH_FRAME_SECTION_NAME
2467   if (eh_frame_section == 0)
2468     {
2469       int flags;
2470
2471       if (EH_TABLES_CAN_BE_READ_ONLY)
2472         {
2473           int fde_encoding;
2474           int per_encoding;
2475           int lsda_encoding;
2476
2477           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2478                                                        /*global=*/0);
2479           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2480                                                        /*global=*/1);
2481           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2482                                                         /*global=*/0);
2483           flags = ((! flag_pic
2484                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2485                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2486                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2487                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2488                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2489                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2490                    ? 0 : SECTION_WRITE);
2491         }
2492       else
2493         flags = SECTION_WRITE;
2494       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2495     }
2496 #endif
2497
2498   if (eh_frame_section)
2499     switch_to_section (eh_frame_section);
2500   else
2501     {
2502       /* We have no special eh_frame section.  Put the information in
2503          the data section and emit special labels to guide collect2.  */
2504       switch_to_section (data_section);
2505       label = get_file_function_name ("F");
2506       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2507       targetm.asm_out.globalize_label (asm_out_file,
2508                                        IDENTIFIER_POINTER (label));
2509       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2510     }
2511 }
2512
2513 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2514
2515 static HOST_WIDE_INT
2516 div_data_align (HOST_WIDE_INT off)
2517 {
2518   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2519   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2520   return r;
2521 }
2522
2523 /* Output a Call Frame Information opcode and its operand(s).  */
2524
2525 static void
2526 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2527 {
2528   unsigned long r;
2529   HOST_WIDE_INT off;
2530
2531   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2532     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2533                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2534                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2535                          ((unsigned HOST_WIDE_INT)
2536                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2537   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2538     {
2539       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2540       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2541                            "DW_CFA_offset, column 0x%lx", r);
2542       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2543       dw2_asm_output_data_uleb128 (off, NULL);
2544     }
2545   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2546     {
2547       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2548       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2549                            "DW_CFA_restore, column 0x%lx", r);
2550     }
2551   else
2552     {
2553       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2554                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2555
2556       switch (cfi->dw_cfi_opc)
2557         {
2558         case DW_CFA_set_loc:
2559           if (for_eh)
2560             dw2_asm_output_encoded_addr_rtx (
2561                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2562                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2563                 false, NULL);
2564           else
2565             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2566                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2567           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2568           break;
2569
2570         case DW_CFA_advance_loc1:
2571           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2572                                 fde->dw_fde_current_label, NULL);
2573           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2574           break;
2575
2576         case DW_CFA_advance_loc2:
2577           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2578                                 fde->dw_fde_current_label, NULL);
2579           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2580           break;
2581
2582         case DW_CFA_advance_loc4:
2583           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2584                                 fde->dw_fde_current_label, NULL);
2585           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2586           break;
2587
2588         case DW_CFA_MIPS_advance_loc8:
2589           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2590                                 fde->dw_fde_current_label, NULL);
2591           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2592           break;
2593
2594         case DW_CFA_offset_extended:
2595           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2596           dw2_asm_output_data_uleb128 (r, NULL);
2597           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2598           dw2_asm_output_data_uleb128 (off, NULL);
2599           break;
2600
2601         case DW_CFA_def_cfa:
2602           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2603           dw2_asm_output_data_uleb128 (r, NULL);
2604           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2605           break;
2606
2607         case DW_CFA_offset_extended_sf:
2608           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2609           dw2_asm_output_data_uleb128 (r, NULL);
2610           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2611           dw2_asm_output_data_sleb128 (off, NULL);
2612           break;
2613
2614         case DW_CFA_def_cfa_sf:
2615           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2616           dw2_asm_output_data_uleb128 (r, NULL);
2617           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2618           dw2_asm_output_data_sleb128 (off, NULL);
2619           break;
2620
2621         case DW_CFA_restore_extended:
2622         case DW_CFA_undefined:
2623         case DW_CFA_same_value:
2624         case DW_CFA_def_cfa_register:
2625           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2626           dw2_asm_output_data_uleb128 (r, NULL);
2627           break;
2628
2629         case DW_CFA_register:
2630           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2631           dw2_asm_output_data_uleb128 (r, NULL);
2632           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2633           dw2_asm_output_data_uleb128 (r, NULL);
2634           break;
2635
2636         case DW_CFA_def_cfa_offset:
2637         case DW_CFA_GNU_args_size:
2638           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2639           break;
2640
2641         case DW_CFA_def_cfa_offset_sf:
2642           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2643           dw2_asm_output_data_sleb128 (off, NULL);
2644           break;
2645
2646         case DW_CFA_GNU_window_save:
2647           break;
2648
2649         case DW_CFA_def_cfa_expression:
2650         case DW_CFA_expression:
2651           output_cfa_loc (cfi);
2652           break;
2653
2654         case DW_CFA_GNU_negative_offset_extended:
2655           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2656           gcc_unreachable ();
2657
2658         default:
2659           break;
2660         }
2661     }
2662 }
2663
2664 /* Similar, but do it via assembler directives instead.  */
2665
2666 static void
2667 output_cfi_directive (dw_cfi_ref cfi)
2668 {
2669   unsigned long r, r2;
2670
2671   switch (cfi->dw_cfi_opc)
2672     {
2673     case DW_CFA_advance_loc:
2674     case DW_CFA_advance_loc1:
2675     case DW_CFA_advance_loc2:
2676     case DW_CFA_advance_loc4:
2677     case DW_CFA_MIPS_advance_loc8:
2678     case DW_CFA_set_loc:
2679       /* Should only be created by add_fde_cfi in a code path not
2680          followed when emitting via directives.  The assembler is
2681          going to take care of this for us.  */
2682       gcc_unreachable ();
2683
2684     case DW_CFA_offset:
2685     case DW_CFA_offset_extended:
2686     case DW_CFA_offset_extended_sf:
2687       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2688       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2689                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2690       break;
2691
2692     case DW_CFA_restore:
2693     case DW_CFA_restore_extended:
2694       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2695       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2696       break;
2697
2698     case DW_CFA_undefined:
2699       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2700       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2701       break;
2702
2703     case DW_CFA_same_value:
2704       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2705       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2706       break;
2707
2708     case DW_CFA_def_cfa:
2709     case DW_CFA_def_cfa_sf:
2710       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2711       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2712                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2713       break;
2714
2715     case DW_CFA_def_cfa_register:
2716       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2717       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2718       break;
2719
2720     case DW_CFA_register:
2721       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2722       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2723       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2724       break;
2725
2726     case DW_CFA_def_cfa_offset:
2727     case DW_CFA_def_cfa_offset_sf:
2728       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2729                HOST_WIDE_INT_PRINT_DEC"\n",
2730                cfi->dw_cfi_oprnd1.dw_cfi_offset);
2731       break;
2732
2733     case DW_CFA_GNU_args_size:
2734       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2735       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2736       if (flag_debug_asm)
2737         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2738                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2739       fputc ('\n', asm_out_file);
2740       break;
2741
2742     case DW_CFA_GNU_window_save:
2743       fprintf (asm_out_file, "\t.cfi_window_save\n");
2744       break;
2745
2746     case DW_CFA_def_cfa_expression:
2747     case DW_CFA_expression:
2748       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2749       output_cfa_loc_raw (cfi);
2750       fputc ('\n', asm_out_file);
2751       break;
2752
2753     default:
2754       gcc_unreachable ();
2755     }
2756 }
2757
2758 /* Output the call frame information used to record information
2759    that relates to calculating the frame pointer, and records the
2760    location of saved registers.  */
2761
2762 static void
2763 output_call_frame_info (int for_eh)
2764 {
2765   unsigned int i;
2766   dw_fde_ref fde;
2767   dw_cfi_ref cfi;
2768   char l1[20], l2[20], section_start_label[20];
2769   bool any_lsda_needed = false;
2770   char augmentation[6];
2771   int augmentation_size;
2772   int fde_encoding = DW_EH_PE_absptr;
2773   int per_encoding = DW_EH_PE_absptr;
2774   int lsda_encoding = DW_EH_PE_absptr;
2775   int return_reg;
2776
2777   /* Don't emit a CIE if there won't be any FDEs.  */
2778   if (fde_table_in_use == 0)
2779     return;
2780
2781   /* Nothing to do if the assembler's doing it all.  */
2782   if (dwarf2out_do_cfi_asm ())
2783     return;
2784
2785   /* If we make FDEs linkonce, we may have to emit an empty label for
2786      an FDE that wouldn't otherwise be emitted.  We want to avoid
2787      having an FDE kept around when the function it refers to is
2788      discarded.  Example where this matters: a primary function
2789      template in C++ requires EH information, but an explicit
2790      specialization doesn't.  */
2791   if (TARGET_USES_WEAK_UNWIND_INFO
2792       && ! flag_asynchronous_unwind_tables
2793       && flag_exceptions
2794       && for_eh)
2795     for (i = 0; i < fde_table_in_use; i++)
2796       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2797           && !fde_table[i].uses_eh_lsda
2798           && ! DECL_WEAK (fde_table[i].decl))
2799         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2800                                       for_eh, /* empty */ 1);
2801
2802   /* If we don't have any functions we'll want to unwind out of, don't
2803      emit any EH unwind information.  Note that if exceptions aren't
2804      enabled, we won't have collected nothrow information, and if we
2805      asked for asynchronous tables, we always want this info.  */
2806   if (for_eh)
2807     {
2808       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2809
2810       for (i = 0; i < fde_table_in_use; i++)
2811         if (fde_table[i].uses_eh_lsda)
2812           any_eh_needed = any_lsda_needed = true;
2813         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2814           any_eh_needed = true;
2815         else if (! fde_table[i].nothrow
2816                  && ! fde_table[i].all_throwers_are_sibcalls)
2817           any_eh_needed = true;
2818
2819       if (! any_eh_needed)
2820         return;
2821     }
2822
2823   /* We're going to be generating comments, so turn on app.  */
2824   if (flag_debug_asm)
2825     app_enable ();
2826
2827   if (for_eh)
2828     switch_to_eh_frame_section ();
2829   else
2830     {
2831       if (!debug_frame_section)
2832         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2833                                            SECTION_DEBUG, NULL);
2834       switch_to_section (debug_frame_section);
2835     }
2836
2837   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2838   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2839
2840   /* Output the CIE.  */
2841   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2842   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2843   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2844     dw2_asm_output_data (4, 0xffffffff,
2845       "Initial length escape value indicating 64-bit DWARF extension");
2846   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2847                         "Length of Common Information Entry");
2848   ASM_OUTPUT_LABEL (asm_out_file, l1);
2849
2850   /* Now that the CIE pointer is PC-relative for EH,
2851      use 0 to identify the CIE.  */
2852   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2853                        (for_eh ? 0 : DWARF_CIE_ID),
2854                        "CIE Identifier Tag");
2855
2856   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2857
2858   augmentation[0] = 0;
2859   augmentation_size = 0;
2860   if (for_eh)
2861     {
2862       char *p;
2863
2864       /* Augmentation:
2865          z      Indicates that a uleb128 is present to size the
2866                 augmentation section.
2867          L      Indicates the encoding (and thus presence) of
2868                 an LSDA pointer in the FDE augmentation.
2869          R      Indicates a non-default pointer encoding for
2870                 FDE code pointers.
2871          P      Indicates the presence of an encoding + language
2872                 personality routine in the CIE augmentation.  */
2873
2874       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2875       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2876       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2877
2878       p = augmentation + 1;
2879       if (eh_personality_libfunc)
2880         {
2881           *p++ = 'P';
2882           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2883           assemble_external_libcall (eh_personality_libfunc);
2884         }
2885       if (any_lsda_needed)
2886         {
2887           *p++ = 'L';
2888           augmentation_size += 1;
2889         }
2890       if (fde_encoding != DW_EH_PE_absptr)
2891         {
2892           *p++ = 'R';
2893           augmentation_size += 1;
2894         }
2895       if (p > augmentation + 1)
2896         {
2897           augmentation[0] = 'z';
2898           *p = '\0';
2899         }
2900
2901       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2902       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2903         {
2904           int offset = (  4             /* Length */
2905                         + 4             /* CIE Id */
2906                         + 1             /* CIE version */
2907                         + strlen (augmentation) + 1     /* Augmentation */
2908                         + size_of_uleb128 (1)           /* Code alignment */
2909                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2910                         + 1             /* RA column */
2911                         + 1             /* Augmentation size */
2912                         + 1             /* Personality encoding */ );
2913           int pad = -offset & (PTR_SIZE - 1);
2914
2915           augmentation_size += pad;
2916
2917           /* Augmentations should be small, so there's scarce need to
2918              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2919           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2920         }
2921     }
2922
2923   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2924   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2925   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2926                                "CIE Data Alignment Factor");
2927
2928   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2929   if (DW_CIE_VERSION == 1)
2930     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2931   else
2932     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2933
2934   if (augmentation[0])
2935     {
2936       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2937       if (eh_personality_libfunc)
2938         {
2939           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2940                                eh_data_format_name (per_encoding));
2941           dw2_asm_output_encoded_addr_rtx (per_encoding,
2942                                            eh_personality_libfunc,
2943                                            true, NULL);
2944         }
2945
2946       if (any_lsda_needed)
2947         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2948                              eh_data_format_name (lsda_encoding));
2949
2950       if (fde_encoding != DW_EH_PE_absptr)
2951         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2952                              eh_data_format_name (fde_encoding));
2953     }
2954
2955   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2956     output_cfi (cfi, NULL, for_eh);
2957
2958   /* Pad the CIE out to an address sized boundary.  */
2959   ASM_OUTPUT_ALIGN (asm_out_file,
2960                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2961   ASM_OUTPUT_LABEL (asm_out_file, l2);
2962
2963   /* Loop through all of the FDE's.  */
2964   for (i = 0; i < fde_table_in_use; i++)
2965     {
2966       fde = &fde_table[i];
2967
2968       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2969       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2970           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2971           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2972           && !fde->uses_eh_lsda)
2973         continue;
2974
2975       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2976       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2977       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2978       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2979       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2980         dw2_asm_output_data (4, 0xffffffff,
2981                              "Initial length escape value indicating 64-bit DWARF extension");
2982       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2983                             "FDE Length");
2984       ASM_OUTPUT_LABEL (asm_out_file, l1);
2985
2986       if (for_eh)
2987         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2988       else
2989         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2990                                debug_frame_section, "FDE CIE offset");
2991
2992       if (for_eh)
2993         {
2994           if (fde->dw_fde_switched_sections)
2995             {
2996               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2997                                       fde->dw_fde_unlikely_section_label);
2998               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2999                                       fde->dw_fde_hot_section_label);
3000               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3001               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3002               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3003                                                "FDE initial location");
3004               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3005                                     fde->dw_fde_hot_section_end_label,
3006                                     fde->dw_fde_hot_section_label,
3007                                     "FDE address range");
3008               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3009                                                "FDE initial location");
3010               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3011                                     fde->dw_fde_unlikely_section_end_label,
3012                                     fde->dw_fde_unlikely_section_label,
3013                                     "FDE address range");
3014             }
3015           else
3016             {
3017               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3018               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3019               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3020                                                sym_ref,
3021                                                false,
3022                                                "FDE initial location");
3023               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3024                                     fde->dw_fde_end, fde->dw_fde_begin,
3025                                     "FDE address range");
3026             }
3027         }
3028       else
3029         {
3030           if (fde->dw_fde_switched_sections)
3031             {
3032               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3033                                    fde->dw_fde_hot_section_label,
3034                                    "FDE initial location");
3035               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3036                                     fde->dw_fde_hot_section_end_label,
3037                                     fde->dw_fde_hot_section_label,
3038                                     "FDE address range");
3039               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3040                                    fde->dw_fde_unlikely_section_label,
3041                                    "FDE initial location");
3042               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3043                                     fde->dw_fde_unlikely_section_end_label,
3044                                     fde->dw_fde_unlikely_section_label,
3045                                     "FDE address range");
3046             }
3047           else
3048             {
3049               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3050                                    "FDE initial location");
3051               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3052                                     fde->dw_fde_end, fde->dw_fde_begin,
3053                                     "FDE address range");
3054             }
3055         }
3056
3057       if (augmentation[0])
3058         {
3059           if (any_lsda_needed)
3060             {
3061               int size = size_of_encoded_value (lsda_encoding);
3062
3063               if (lsda_encoding == DW_EH_PE_aligned)
3064                 {
3065                   int offset = (  4             /* Length */
3066                                 + 4             /* CIE offset */
3067                                 + 2 * size_of_encoded_value (fde_encoding)
3068                                 + 1             /* Augmentation size */ );
3069                   int pad = -offset & (PTR_SIZE - 1);
3070
3071                   size += pad;
3072                   gcc_assert (size_of_uleb128 (size) == 1);
3073                 }
3074
3075               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3076
3077               if (fde->uses_eh_lsda)
3078                 {
3079                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3080                                                fde->funcdef_number);
3081                   dw2_asm_output_encoded_addr_rtx (
3082                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3083                         false, "Language Specific Data Area");
3084                 }
3085               else
3086                 {
3087                   if (lsda_encoding == DW_EH_PE_aligned)
3088                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3089                   dw2_asm_output_data
3090                     (size_of_encoded_value (lsda_encoding), 0,
3091                      "Language Specific Data Area (none)");
3092                 }
3093             }
3094           else
3095             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3096         }
3097
3098       /* Loop through the Call Frame Instructions associated with
3099          this FDE.  */
3100       fde->dw_fde_current_label = fde->dw_fde_begin;
3101       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3102         output_cfi (cfi, fde, for_eh);
3103
3104       /* Pad the FDE out to an address sized boundary.  */
3105       ASM_OUTPUT_ALIGN (asm_out_file,
3106                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3107       ASM_OUTPUT_LABEL (asm_out_file, l2);
3108     }
3109
3110   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3111     dw2_asm_output_data (4, 0, "End of Table");
3112 #ifdef MIPS_DEBUGGING_INFO
3113   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3114      get a value of 0.  Putting .align 0 after the label fixes it.  */
3115   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3116 #endif
3117
3118   /* Turn off app to make assembly quicker.  */
3119   if (flag_debug_asm)
3120     app_disable ();
3121 }
3122
3123 /* Output a marker (i.e. a label) for the beginning of a function, before
3124    the prologue.  */
3125
3126 void
3127 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3128                           const char *file ATTRIBUTE_UNUSED)
3129 {
3130   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3131   char * dup_label;
3132   dw_fde_ref fde;
3133
3134   current_function_func_begin_label = NULL;
3135
3136 #ifdef TARGET_UNWIND_INFO
3137   /* ??? current_function_func_begin_label is also used by except.c
3138      for call-site information.  We must emit this label if it might
3139      be used.  */
3140   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3141       && ! dwarf2out_do_frame ())
3142     return;
3143 #else
3144   if (! dwarf2out_do_frame ())
3145     return;
3146 #endif
3147
3148   switch_to_section (function_section (current_function_decl));
3149   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3150                                current_function_funcdef_no);
3151   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3152                           current_function_funcdef_no);
3153   dup_label = xstrdup (label);
3154   current_function_func_begin_label = dup_label;
3155
3156 #ifdef TARGET_UNWIND_INFO
3157   /* We can elide the fde allocation if we're not emitting debug info.  */
3158   if (! dwarf2out_do_frame ())
3159     return;
3160 #endif
3161
3162   /* Expand the fde table if necessary.  */
3163   if (fde_table_in_use == fde_table_allocated)
3164     {
3165       fde_table_allocated += FDE_TABLE_INCREMENT;
3166       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3167       memset (fde_table + fde_table_in_use, 0,
3168               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3169     }
3170
3171   /* Record the FDE associated with this function.  */
3172   current_funcdef_fde = fde_table_in_use;
3173
3174   /* Add the new FDE at the end of the fde_table.  */
3175   fde = &fde_table[fde_table_in_use++];
3176   fde->decl = current_function_decl;
3177   fde->dw_fde_begin = dup_label;
3178   fde->dw_fde_current_label = dup_label;
3179   fde->dw_fde_hot_section_label = NULL;
3180   fde->dw_fde_hot_section_end_label = NULL;
3181   fde->dw_fde_unlikely_section_label = NULL;
3182   fde->dw_fde_unlikely_section_end_label = NULL;
3183   fde->dw_fde_switched_sections = false;
3184   fde->dw_fde_end = NULL;
3185   fde->dw_fde_cfi = NULL;
3186   fde->funcdef_number = current_function_funcdef_no;
3187   fde->nothrow = TREE_NOTHROW (current_function_decl);
3188   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3189   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3190   fde->drap_reg = INVALID_REGNUM;
3191   fde->vdrap_reg = INVALID_REGNUM;
3192
3193   args_size = old_args_size = 0;
3194
3195   /* We only want to output line number information for the genuine dwarf2
3196      prologue case, not the eh frame case.  */
3197 #ifdef DWARF2_DEBUGGING_INFO
3198   if (file)
3199     dwarf2out_source_line (line, file);
3200 #endif
3201
3202   if (dwarf2out_do_cfi_asm ())
3203     {
3204       int enc;
3205       rtx ref;
3206
3207       fprintf (asm_out_file, "\t.cfi_startproc\n");
3208
3209       if (eh_personality_libfunc)
3210         {
3211           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3212           ref = eh_personality_libfunc;
3213
3214           /* ??? The GAS support isn't entirely consistent.  We have to
3215              handle indirect support ourselves, but PC-relative is done
3216              in the assembler.  Further, the assembler can't handle any
3217              of the weirder relocation types.  */
3218           if (enc & DW_EH_PE_indirect)
3219             ref = dw2_force_const_mem (ref, true);
3220
3221           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3222           output_addr_const (asm_out_file, ref);
3223           fputc ('\n', asm_out_file);
3224         }
3225
3226       if (crtl->uses_eh_lsda)
3227         {
3228           char lab[20];
3229
3230           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3231           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3232                                        current_function_funcdef_no);
3233           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3234           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3235
3236           if (enc & DW_EH_PE_indirect)
3237             ref = dw2_force_const_mem (ref, true);
3238
3239           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3240           output_addr_const (asm_out_file, ref);
3241           fputc ('\n', asm_out_file);
3242         }
3243     }
3244 }
3245
3246 /* Output a marker (i.e. a label) for the absolute end of the generated code
3247    for a function definition.  This gets called *after* the epilogue code has
3248    been generated.  */
3249
3250 void
3251 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3252                         const char *file ATTRIBUTE_UNUSED)
3253 {
3254   dw_fde_ref fde;
3255   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3256
3257   if (dwarf2out_do_cfi_asm ())
3258     fprintf (asm_out_file, "\t.cfi_endproc\n");
3259
3260   /* Output a label to mark the endpoint of the code generated for this
3261      function.  */
3262   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3263                                current_function_funcdef_no);
3264   ASM_OUTPUT_LABEL (asm_out_file, label);
3265   fde = current_fde ();
3266   gcc_assert (fde != NULL);
3267   fde->dw_fde_end = xstrdup (label);
3268 }
3269
3270 void
3271 dwarf2out_frame_init (void)
3272 {
3273   /* Allocate the initial hunk of the fde_table.  */
3274   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3275   fde_table_allocated = FDE_TABLE_INCREMENT;
3276   fde_table_in_use = 0;
3277
3278   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3279      sake of lookup_cfa.  */
3280
3281   /* On entry, the Canonical Frame Address is at SP.  */
3282   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3283
3284 #ifdef DWARF2_UNWIND_INFO
3285   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3286     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3287 #endif
3288 }
3289
3290 void
3291 dwarf2out_frame_finish (void)
3292 {
3293   /* Output call frame information.  */
3294   if (DWARF2_FRAME_INFO)
3295     output_call_frame_info (0);
3296
3297 #ifndef TARGET_UNWIND_INFO
3298   /* Output another copy for the unwinder.  */
3299   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3300     output_call_frame_info (1);
3301 #endif
3302 }
3303
3304 /* Note that the current function section is being used for code.  */
3305
3306 static void
3307 dwarf2out_note_section_used (void)
3308 {
3309   section *sec = current_function_section ();
3310   if (sec == text_section)
3311     text_section_used = true;
3312   else if (sec == cold_text_section)
3313     cold_text_section_used = true;
3314 }
3315
3316 void
3317 dwarf2out_switch_text_section (void)
3318 {
3319   dw_fde_ref fde = current_fde ();
3320
3321   gcc_assert (cfun && fde);
3322
3323   fde->dw_fde_switched_sections = true;
3324   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3325   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3326   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3327   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3328   have_multiple_function_sections = true;
3329
3330   /* Reset the current label on switching text sections, so that we
3331      don't attempt to advance_loc4 between labels in different sections.  */
3332   fde->dw_fde_current_label = NULL;
3333
3334   /* There is no need to mark used sections when not debugging.  */
3335   if (cold_text_section != NULL)
3336     dwarf2out_note_section_used ();
3337 }
3338 #endif
3339 \f
3340 /* And now, the subset of the debugging information support code necessary
3341    for emitting location expressions.  */
3342
3343 /* Data about a single source file.  */
3344 struct dwarf_file_data GTY(())
3345 {
3346   const char * filename;
3347   int emitted_number;
3348 };
3349
3350 /* We need some way to distinguish DW_OP_addr with a direct symbol
3351    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3352 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3353
3354
3355 typedef struct dw_val_struct *dw_val_ref;
3356 typedef struct die_struct *dw_die_ref;
3357 typedef const struct die_struct *const_dw_die_ref;
3358 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3359 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3360
3361 /* Each DIE may have a series of attribute/value pairs.  Values
3362    can take on several forms.  The forms that are used in this
3363    implementation are listed below.  */
3364
3365 enum dw_val_class
3366 {
3367   dw_val_class_addr,
3368   dw_val_class_offset,
3369   dw_val_class_loc,
3370   dw_val_class_loc_list,
3371   dw_val_class_range_list,
3372   dw_val_class_const,
3373   dw_val_class_unsigned_const,
3374   dw_val_class_long_long,
3375   dw_val_class_vec,
3376   dw_val_class_flag,
3377   dw_val_class_die_ref,
3378   dw_val_class_fde_ref,
3379   dw_val_class_lbl_id,
3380   dw_val_class_lineptr,
3381   dw_val_class_str,
3382   dw_val_class_macptr,
3383   dw_val_class_file
3384 };
3385
3386 /* Describe a double word constant value.  */
3387 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3388
3389 typedef struct dw_long_long_struct GTY(())
3390 {
3391   unsigned long hi;
3392   unsigned long low;
3393 }
3394 dw_long_long_const;
3395
3396 /* Describe a floating point constant value, or a vector constant value.  */
3397
3398 typedef struct dw_vec_struct GTY(())
3399 {
3400   unsigned char * GTY((length ("%h.length"))) array;
3401   unsigned length;
3402   unsigned elt_size;
3403 }
3404 dw_vec_const;
3405
3406 /* The dw_val_node describes an attribute's value, as it is
3407    represented internally.  */
3408
3409 typedef struct dw_val_struct GTY(())
3410 {
3411   enum dw_val_class val_class;
3412   union dw_val_struct_union
3413     {
3414       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3415       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3416       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3417       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3418       HOST_WIDE_INT GTY ((default)) val_int;
3419       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3420       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3421       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3422       struct dw_val_die_union
3423         {
3424           dw_die_ref die;
3425           int external;
3426         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3427       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3428       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3429       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3430       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3431       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3432     }
3433   GTY ((desc ("%1.val_class"))) v;
3434 }
3435 dw_val_node;
3436
3437 /* Locations in memory are described using a sequence of stack machine
3438    operations.  */
3439
3440 typedef struct dw_loc_descr_struct GTY(())
3441 {
3442   dw_loc_descr_ref dw_loc_next;
3443   enum dwarf_location_atom dw_loc_opc;
3444   dw_val_node dw_loc_oprnd1;
3445   dw_val_node dw_loc_oprnd2;
3446   int dw_loc_addr;
3447 }
3448 dw_loc_descr_node;
3449
3450 /* Location lists are ranges + location descriptions for that range,
3451    so you can track variables that are in different places over
3452    their entire life.  */
3453 typedef struct dw_loc_list_struct GTY(())
3454 {
3455   dw_loc_list_ref dw_loc_next;
3456   const char *begin; /* Label for begin address of range */
3457   const char *end;  /* Label for end address of range */
3458   char *ll_symbol; /* Label for beginning of location list.
3459                       Only on head of list */
3460   const char *section; /* Section this loclist is relative to */
3461   dw_loc_descr_ref expr;
3462 } dw_loc_list_node;
3463
3464 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3465
3466 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3467
3468 /* Convert a DWARF stack opcode into its string name.  */
3469
3470 static const char *
3471 dwarf_stack_op_name (unsigned int op)
3472 {
3473   switch (op)
3474     {
3475     case DW_OP_addr:
3476     case INTERNAL_DW_OP_tls_addr:
3477       return "DW_OP_addr";
3478     case DW_OP_deref:
3479       return "DW_OP_deref";
3480     case DW_OP_const1u:
3481       return "DW_OP_const1u";
3482     case DW_OP_const1s:
3483       return "DW_OP_const1s";
3484     case DW_OP_const2u:
3485       return "DW_OP_const2u";
3486     case DW_OP_const2s:
3487       return "DW_OP_const2s";
3488     case DW_OP_const4u:
3489       return "DW_OP_const4u";
3490     case DW_OP_const4s:
3491       return "DW_OP_const4s";
3492     case DW_OP_const8u:
3493       return "DW_OP_const8u";
3494     case DW_OP_const8s:
3495       return "DW_OP_const8s";
3496     case DW_OP_constu:
3497       return "DW_OP_constu";
3498     case DW_OP_consts:
3499       return "DW_OP_consts";
3500     case DW_OP_dup:
3501       return "DW_OP_dup";
3502     case DW_OP_drop:
3503       return "DW_OP_drop";
3504     case DW_OP_over:
3505       return "DW_OP_over";
3506     case DW_OP_pick:
3507       return "DW_OP_pick";
3508     case DW_OP_swap:
3509       return "DW_OP_swap";
3510     case DW_OP_rot:
3511       return "DW_OP_rot";
3512     case DW_OP_xderef:
3513       return "DW_OP_xderef";
3514     case DW_OP_abs:
3515       return "DW_OP_abs";
3516     case DW_OP_and:
3517       return "DW_OP_and";
3518     case DW_OP_div:
3519       return "DW_OP_div";
3520     case DW_OP_minus:
3521       return "DW_OP_minus";
3522     case DW_OP_mod:
3523       return "DW_OP_mod";
3524     case DW_OP_mul:
3525       return "DW_OP_mul";
3526     case DW_OP_neg:
3527       return "DW_OP_neg";
3528     case DW_OP_not:
3529       return "DW_OP_not";
3530     case DW_OP_or:
3531       return "DW_OP_or";
3532     case DW_OP_plus:
3533       return "DW_OP_plus";
3534     case DW_OP_plus_uconst:
3535       return "DW_OP_plus_uconst";
3536     case DW_OP_shl:
3537       return "DW_OP_shl";
3538     case DW_OP_shr:
3539       return "DW_OP_shr";
3540     case DW_OP_shra:
3541       return "DW_OP_shra";
3542     case DW_OP_xor:
3543       return "DW_OP_xor";
3544     case DW_OP_bra:
3545       return "DW_OP_bra";
3546     case DW_OP_eq:
3547       return "DW_OP_eq";
3548     case DW_OP_ge:
3549       return "DW_OP_ge";
3550     case DW_OP_gt:
3551       return "DW_OP_gt";
3552     case DW_OP_le:
3553       return "DW_OP_le";
3554     case DW_OP_lt:
3555       return "DW_OP_lt";
3556     case DW_OP_ne:
3557       return "DW_OP_ne";
3558     case DW_OP_skip:
3559       return "DW_OP_skip";
3560     case DW_OP_lit0:
3561       return "DW_OP_lit0";
3562     case DW_OP_lit1:
3563       return "DW_OP_lit1";
3564     case DW_OP_lit2:
3565       return "DW_OP_lit2";
3566     case DW_OP_lit3:
3567       return "DW_OP_lit3";
3568     case DW_OP_lit4:
3569       return "DW_OP_lit4";
3570     case DW_OP_lit5:
3571       return "DW_OP_lit5";
3572     case DW_OP_lit6:
3573       return "DW_OP_lit6";
3574     case DW_OP_lit7:
3575       return "DW_OP_lit7";
3576     case DW_OP_lit8:
3577       return "DW_OP_lit8";
3578     case DW_OP_lit9:
3579       return "DW_OP_lit9";
3580     case DW_OP_lit10:
3581       return "DW_OP_lit10";
3582     case DW_OP_lit11:
3583       return "DW_OP_lit11";
3584     case DW_OP_lit12:
3585       return "DW_OP_lit12";
3586     case DW_OP_lit13:
3587       return "DW_OP_lit13";
3588     case DW_OP_lit14:
3589       return "DW_OP_lit14";
3590     case DW_OP_lit15:
3591       return "DW_OP_lit15";
3592     case DW_OP_lit16:
3593       return "DW_OP_lit16";
3594     case DW_OP_lit17:
3595       return "DW_OP_lit17";
3596     case DW_OP_lit18:
3597       return "DW_OP_lit18";
3598     case DW_OP_lit19:
3599       return "DW_OP_lit19";
3600     case DW_OP_lit20:
3601       return "DW_OP_lit20";
3602     case DW_OP_lit21:
3603       return "DW_OP_lit21";
3604     case DW_OP_lit22:
3605       return "DW_OP_lit22";
3606     case DW_OP_lit23:
3607       return "DW_OP_lit23";
3608     case DW_OP_lit24:
3609       return "DW_OP_lit24";
3610     case DW_OP_lit25:
3611       return "DW_OP_lit25";
3612     case DW_OP_lit26:
3613       return "DW_OP_lit26";
3614     case DW_OP_lit27:
3615       return "DW_OP_lit27";
3616     case DW_OP_lit28:
3617       return "DW_OP_lit28";
3618     case DW_OP_lit29:
3619       return "DW_OP_lit29";
3620     case DW_OP_lit30:
3621       return "DW_OP_lit30";
3622     case DW_OP_lit31:
3623       return "DW_OP_lit31";
3624     case DW_OP_reg0:
3625       return "DW_OP_reg0";
3626     case DW_OP_reg1:
3627       return "DW_OP_reg1";
3628     case DW_OP_reg2:
3629       return "DW_OP_reg2";
3630     case DW_OP_reg3:
3631       return "DW_OP_reg3";
3632     case DW_OP_reg4:
3633       return "DW_OP_reg4";
3634     case DW_OP_reg5:
3635       return "DW_OP_reg5";
3636     case DW_OP_reg6:
3637       return "DW_OP_reg6";
3638     case DW_OP_reg7:
3639       return "DW_OP_reg7";
3640     case DW_OP_reg8:
3641       return "DW_OP_reg8";
3642     case DW_OP_reg9:
3643       return "DW_OP_reg9";
3644     case DW_OP_reg10:
3645       return "DW_OP_reg10";
3646     case DW_OP_reg11:
3647       return "DW_OP_reg11";
3648     case DW_OP_reg12:
3649       return "DW_OP_reg12";
3650     case DW_OP_reg13:
3651       return "DW_OP_reg13";
3652     case DW_OP_reg14:
3653       return "DW_OP_reg14";
3654     case DW_OP_reg15:
3655       return "DW_OP_reg15";
3656     case DW_OP_reg16:
3657       return "DW_OP_reg16";
3658     case DW_OP_reg17:
3659       return "DW_OP_reg17";
3660     case DW_OP_reg18:
3661       return "DW_OP_reg18";
3662     case DW_OP_reg19:
3663       return "DW_OP_reg19";
3664     case DW_OP_reg20:
3665       return "DW_OP_reg20";
3666     case DW_OP_reg21:
3667       return "DW_OP_reg21";
3668     case DW_OP_reg22:
3669       return "DW_OP_reg22";
3670     case DW_OP_reg23:
3671       return "DW_OP_reg23";
3672     case DW_OP_reg24:
3673       return "DW_OP_reg24";
3674     case DW_OP_reg25:
3675       return "DW_OP_reg25";
3676     case DW_OP_reg26:
3677       return "DW_OP_reg26";
3678     case DW_OP_reg27:
3679       return "DW_OP_reg27";
3680     case DW_OP_reg28:
3681       return "DW_OP_reg28";
3682     case DW_OP_reg29:
3683       return "DW_OP_reg29";
3684     case DW_OP_reg30:
3685       return "DW_OP_reg30";
3686     case DW_OP_reg31:
3687       return "DW_OP_reg31";
3688     case DW_OP_breg0:
3689       return "DW_OP_breg0";
3690     case DW_OP_breg1:
3691       return "DW_OP_breg1";
3692     case DW_OP_breg2:
3693       return "DW_OP_breg2";
3694     case DW_OP_breg3:
3695       return "DW_OP_breg3";
3696     case DW_OP_breg4:
3697       return "DW_OP_breg4";
3698     case DW_OP_breg5:
3699       return "DW_OP_breg5";
3700     case DW_OP_breg6:
3701       return "DW_OP_breg6";
3702     case DW_OP_breg7:
3703       return "DW_OP_breg7";
3704     case DW_OP_breg8:
3705       return "DW_OP_breg8";
3706     case DW_OP_breg9:
3707       return "DW_OP_breg9";
3708     case DW_OP_breg10:
3709       return "DW_OP_breg10";
3710     case DW_OP_breg11:
3711       return "DW_OP_breg11";
3712     case DW_OP_breg12:
3713       return "DW_OP_breg12";
3714     case DW_OP_breg13:
3715       return "DW_OP_breg13";
3716     case DW_OP_breg14:
3717       return "DW_OP_breg14";
3718     case DW_OP_breg15:
3719       return "DW_OP_breg15";
3720     case DW_OP_breg16:
3721       return "DW_OP_breg16";
3722     case DW_OP_breg17:
3723       return "DW_OP_breg17";
3724     case DW_OP_breg18:
3725       return "DW_OP_breg18";
3726     case DW_OP_breg19:
3727       return "DW_OP_breg19";
3728     case DW_OP_breg20:
3729       return "DW_OP_breg20";
3730     case DW_OP_breg21:
3731       return "DW_OP_breg21";
3732     case DW_OP_breg22:
3733       return "DW_OP_breg22";
3734     case DW_OP_breg23:
3735       return "DW_OP_breg23";
3736     case DW_OP_breg24:
3737       return "DW_OP_breg24";
3738     case DW_OP_breg25:
3739       return "DW_OP_breg25";
3740     case DW_OP_breg26:
3741       return "DW_OP_breg26";
3742     case DW_OP_breg27:
3743       return "DW_OP_breg27";
3744     case DW_OP_breg28:
3745       return "DW_OP_breg28";
3746     case DW_OP_breg29:
3747       return "DW_OP_breg29";
3748     case DW_OP_breg30:
3749       return "DW_OP_breg30";
3750     case DW_OP_breg31:
3751       return "DW_OP_breg31";
3752     case DW_OP_regx:
3753       return "DW_OP_regx";
3754     case DW_OP_fbreg:
3755       return "DW_OP_fbreg";
3756     case DW_OP_bregx:
3757       return "DW_OP_bregx";
3758     case DW_OP_piece:
3759       return "DW_OP_piece";
3760     case DW_OP_deref_size:
3761       return "DW_OP_deref_size";
3762     case DW_OP_xderef_size:
3763       return "DW_OP_xderef_size";
3764     case DW_OP_nop:
3765       return "DW_OP_nop";
3766     case DW_OP_push_object_address:
3767       return "DW_OP_push_object_address";
3768     case DW_OP_call2:
3769       return "DW_OP_call2";
3770     case DW_OP_call4:
3771       return "DW_OP_call4";
3772     case DW_OP_call_ref:
3773       return "DW_OP_call_ref";
3774     case DW_OP_GNU_push_tls_address:
3775       return "DW_OP_GNU_push_tls_address";
3776     case DW_OP_GNU_uninit:
3777       return "DW_OP_GNU_uninit";
3778     default:
3779       return "OP_<unknown>";
3780     }
3781 }
3782
3783 /* Return a pointer to a newly allocated location description.  Location
3784    descriptions are simple expression terms that can be strung
3785    together to form more complicated location (address) descriptions.  */
3786
3787 static inline dw_loc_descr_ref
3788 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3789                unsigned HOST_WIDE_INT oprnd2)
3790 {
3791   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3792
3793   descr->dw_loc_opc = op;
3794   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3795   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3796   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3797   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3798
3799   return descr;
3800 }
3801
3802 /* Return a pointer to a newly allocated location description for
3803    REG and OFFSET.  */
3804
3805 static inline dw_loc_descr_ref
3806 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
3807 {
3808   if (offset)
3809     {
3810       if (reg <= 31)
3811         return new_loc_descr (DW_OP_breg0 + reg, offset, 0);
3812       else
3813         return new_loc_descr (DW_OP_bregx, reg, offset);
3814     }
3815   else if (reg <= 31)
3816     return new_loc_descr (DW_OP_reg0 + reg, 0, 0);
3817   else
3818    return new_loc_descr (DW_OP_regx, reg, 0);
3819 }
3820
3821 /* Add a location description term to a location description expression.  */
3822
3823 static inline void
3824 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3825 {
3826   dw_loc_descr_ref *d;
3827
3828   /* Find the end of the chain.  */
3829   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3830     ;
3831
3832   *d = descr;
3833 }
3834
3835 /* Return the size of a location descriptor.  */
3836
3837 static unsigned long
3838 size_of_loc_descr (dw_loc_descr_ref loc)
3839 {
3840   unsigned long size = 1;
3841
3842   switch (loc->dw_loc_opc)
3843     {
3844     case DW_OP_addr:
3845     case INTERNAL_DW_OP_tls_addr:
3846       size += DWARF2_ADDR_SIZE;
3847       break;
3848     case DW_OP_const1u:
3849     case DW_OP_const1s:
3850       size += 1;
3851       break;
3852     case DW_OP_const2u:
3853     case DW_OP_const2s:
3854       size += 2;
3855       break;
3856     case DW_OP_const4u:
3857     case DW_OP_const4s:
3858       size += 4;
3859       break;
3860     case DW_OP_const8u:
3861     case DW_OP_const8s:
3862       size += 8;
3863       break;
3864     case DW_OP_constu:
3865       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3866       break;
3867     case DW_OP_consts:
3868       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3869       break;
3870     case DW_OP_pick:
3871       size += 1;
3872       break;
3873     case DW_OP_plus_uconst:
3874       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3875       break;
3876     case DW_OP_skip:
3877     case DW_OP_bra:
3878       size += 2;
3879       break;
3880     case DW_OP_breg0:
3881     case DW_OP_breg1:
3882     case DW_OP_breg2:
3883     case DW_OP_breg3:
3884     case DW_OP_breg4:
3885     case DW_OP_breg5:
3886     case DW_OP_breg6:
3887     case DW_OP_breg7:
3888     case DW_OP_breg8:
3889     case DW_OP_breg9:
3890     case DW_OP_breg10:
3891     case DW_OP_breg11:
3892     case DW_OP_breg12:
3893     case DW_OP_breg13:
3894     case DW_OP_breg14:
3895     case DW_OP_breg15:
3896     case DW_OP_breg16:
3897     case DW_OP_breg17:
3898     case DW_OP_breg18:
3899     case DW_OP_breg19:
3900     case DW_OP_breg20:
3901     case DW_OP_breg21:
3902     case DW_OP_breg22:
3903     case DW_OP_breg23:
3904     case DW_OP_breg24:
3905     case DW_OP_breg25:
3906     case DW_OP_breg26:
3907     case DW_OP_breg27:
3908     case DW_OP_breg28:
3909     case DW_OP_breg29:
3910     case DW_OP_breg30:
3911     case DW_OP_breg31:
3912       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3913       break;
3914     case DW_OP_regx:
3915       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3916       break;
3917     case DW_OP_fbreg:
3918       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3919       break;
3920     case DW_OP_bregx:
3921       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3922       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3923       break;
3924     case DW_OP_piece:
3925       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3926       break;
3927     case DW_OP_deref_size:
3928     case DW_OP_xderef_size:
3929       size += 1;
3930       break;
3931     case DW_OP_call2:
3932       size += 2;
3933       break;
3934     case DW_OP_call4:
3935       size += 4;
3936       break;
3937     case DW_OP_call_ref:
3938       size += DWARF2_ADDR_SIZE;
3939       break;
3940     default:
3941       break;
3942     }
3943
3944   return size;
3945 }
3946
3947 /* Return the size of a series of location descriptors.  */
3948
3949 static unsigned long
3950 size_of_locs (dw_loc_descr_ref loc)
3951 {
3952   dw_loc_descr_ref l;
3953   unsigned long size;
3954
3955   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3956      field, to avoid writing to a PCH file.  */
3957   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3958     {
3959       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3960         break;
3961       size += size_of_loc_descr (l);
3962     }
3963   if (! l)
3964     return size;
3965
3966   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3967     {
3968       l->dw_loc_addr = size;
3969       size += size_of_loc_descr (l);
3970     }
3971
3972   return size;
3973 }
3974
3975 /* Output location description stack opcode's operands (if any).  */
3976
3977 static void
3978 output_loc_operands (dw_loc_descr_ref loc)
3979 {
3980   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3981   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3982
3983   switch (loc->dw_loc_opc)
3984     {
3985 #ifdef DWARF2_DEBUGGING_INFO
3986     case DW_OP_addr:
3987       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3988       break;
3989     case DW_OP_const2u:
3990     case DW_OP_const2s:
3991       dw2_asm_output_data (2, val1->v.val_int, NULL);
3992       break;
3993     case DW_OP_const4u:
3994     case DW_OP_const4s:
3995       dw2_asm_output_data (4, val1->v.val_int, NULL);
3996       break;
3997     case DW_OP_const8u:
3998     case DW_OP_const8s:
3999       gcc_assert (HOST_BITS_PER_LONG >= 64);
4000       dw2_asm_output_data (8, val1->v.val_int, NULL);
4001       break;
4002     case DW_OP_skip:
4003     case DW_OP_bra:
4004       {
4005         int offset;
4006
4007         gcc_assert (val1->val_class == dw_val_class_loc);
4008         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4009
4010         dw2_asm_output_data (2, offset, NULL);
4011       }
4012       break;
4013 #else
4014     case DW_OP_addr:
4015     case DW_OP_const2u:
4016     case DW_OP_const2s:
4017     case DW_OP_const4u:
4018     case DW_OP_const4s:
4019     case DW_OP_const8u:
4020     case DW_OP_const8s:
4021     case DW_OP_skip:
4022     case DW_OP_bra:
4023       /* We currently don't make any attempt to make sure these are
4024          aligned properly like we do for the main unwind info, so
4025          don't support emitting things larger than a byte if we're
4026          only doing unwinding.  */
4027       gcc_unreachable ();
4028 #endif
4029     case DW_OP_const1u:
4030     case DW_OP_const1s:
4031       dw2_asm_output_data (1, val1->v.val_int, NULL);
4032       break;
4033     case DW_OP_constu:
4034       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4035       break;
4036     case DW_OP_consts:
4037       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4038       break;
4039     case DW_OP_pick:
4040       dw2_asm_output_data (1, val1->v.val_int, NULL);
4041       break;
4042     case DW_OP_plus_uconst:
4043       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4044       break;
4045     case DW_OP_breg0:
4046     case DW_OP_breg1:
4047     case DW_OP_breg2:
4048     case DW_OP_breg3:
4049     case DW_OP_breg4:
4050     case DW_OP_breg5:
4051     case DW_OP_breg6:
4052     case DW_OP_breg7:
4053     case DW_OP_breg8:
4054     case DW_OP_breg9:
4055     case DW_OP_breg10:
4056     case DW_OP_breg11:
4057     case DW_OP_breg12:
4058     case DW_OP_breg13:
4059     case DW_OP_breg14:
4060     case DW_OP_breg15:
4061     case DW_OP_breg16:
4062     case DW_OP_breg17:
4063     case DW_OP_breg18:
4064     case DW_OP_breg19:
4065     case DW_OP_breg20:
4066     case DW_OP_breg21:
4067     case DW_OP_breg22:
4068     case DW_OP_breg23:
4069     case DW_OP_breg24:
4070     case DW_OP_breg25:
4071     case DW_OP_breg26:
4072     case DW_OP_breg27:
4073     case DW_OP_breg28:
4074     case DW_OP_breg29:
4075     case DW_OP_breg30:
4076     case DW_OP_breg31:
4077       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4078       break;
4079     case DW_OP_regx:
4080       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4081       break;
4082     case DW_OP_fbreg:
4083       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4084       break;
4085     case DW_OP_bregx:
4086       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4087       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4088       break;
4089     case DW_OP_piece:
4090       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4091       break;
4092     case DW_OP_deref_size:
4093     case DW_OP_xderef_size:
4094       dw2_asm_output_data (1, val1->v.val_int, NULL);
4095       break;
4096
4097     case INTERNAL_DW_OP_tls_addr:
4098       if (targetm.asm_out.output_dwarf_dtprel)
4099         {
4100           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4101                                                DWARF2_ADDR_SIZE,
4102                                                val1->v.val_addr);
4103           fputc ('\n', asm_out_file);
4104         }
4105       else
4106         gcc_unreachable ();
4107       break;
4108
4109     default:
4110       /* Other codes have no operands.  */
4111       break;
4112     }
4113 }
4114
4115 /* Output a sequence of location operations.  */
4116
4117 static void
4118 output_loc_sequence (dw_loc_descr_ref loc)
4119 {
4120   for (; loc != NULL; loc = loc->dw_loc_next)
4121     {
4122       /* Output the opcode.  */
4123       dw2_asm_output_data (1, loc->dw_loc_opc,
4124                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4125
4126       /* Output the operand(s) (if any).  */
4127       output_loc_operands (loc);
4128     }
4129 }
4130
4131 /* Output location description stack opcode's operands (if any).
4132    The output is single bytes on a line, suitable for .cfi_escape.  */
4133
4134 static void
4135 output_loc_operands_raw (dw_loc_descr_ref loc)
4136 {
4137   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4138   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4139
4140   switch (loc->dw_loc_opc)
4141     {
4142     case DW_OP_addr:
4143       /* We cannot output addresses in .cfi_escape, only bytes.  */
4144       gcc_unreachable ();
4145
4146     case DW_OP_const1u:
4147     case DW_OP_const1s:
4148     case DW_OP_pick:
4149     case DW_OP_deref_size:
4150     case DW_OP_xderef_size:
4151       fputc (',', asm_out_file);
4152       dw2_asm_output_data_raw (1, val1->v.val_int);
4153       break;
4154
4155     case DW_OP_const2u:
4156     case DW_OP_const2s:
4157       fputc (',', asm_out_file);
4158       dw2_asm_output_data_raw (2, val1->v.val_int);
4159       break;
4160
4161     case DW_OP_const4u:
4162     case DW_OP_const4s:
4163       fputc (',', asm_out_file);
4164       dw2_asm_output_data_raw (4, val1->v.val_int);
4165       break;
4166
4167     case DW_OP_const8u:
4168     case DW_OP_const8s:
4169       gcc_assert (HOST_BITS_PER_LONG >= 64);
4170       fputc (',', asm_out_file);
4171       dw2_asm_output_data_raw (8, val1->v.val_int);
4172       break;
4173
4174     case DW_OP_skip:
4175     case DW_OP_bra:
4176       {
4177         int offset;
4178
4179         gcc_assert (val1->val_class == dw_val_class_loc);
4180         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4181
4182         fputc (',', asm_out_file);
4183         dw2_asm_output_data_raw (2, offset);
4184       }
4185       break;
4186
4187     case DW_OP_constu:
4188     case DW_OP_plus_uconst:
4189     case DW_OP_regx:
4190     case DW_OP_piece:
4191       fputc (',', asm_out_file);
4192       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4193       break;
4194
4195     case DW_OP_consts:
4196     case DW_OP_breg0:
4197     case DW_OP_breg1:
4198     case DW_OP_breg2:
4199     case DW_OP_breg3:
4200     case DW_OP_breg4:
4201     case DW_OP_breg5:
4202     case DW_OP_breg6:
4203     case DW_OP_breg7:
4204     case DW_OP_breg8:
4205     case DW_OP_breg9:
4206     case DW_OP_breg10:
4207     case DW_OP_breg11:
4208     case DW_OP_breg12:
4209     case DW_OP_breg13:
4210     case DW_OP_breg14:
4211     case DW_OP_breg15:
4212     case DW_OP_breg16:
4213     case DW_OP_breg17:
4214     case DW_OP_breg18:
4215     case DW_OP_breg19:
4216     case DW_OP_breg20:
4217     case DW_OP_breg21:
4218     case DW_OP_breg22:
4219     case DW_OP_breg23:
4220     case DW_OP_breg24:
4221     case DW_OP_breg25:
4222     case DW_OP_breg26:
4223     case DW_OP_breg27:
4224     case DW_OP_breg28:
4225     case DW_OP_breg29:
4226     case DW_OP_breg30:
4227     case DW_OP_breg31:
4228     case DW_OP_fbreg:
4229       fputc (',', asm_out_file);
4230       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4231       break;
4232
4233     case DW_OP_bregx:
4234       fputc (',', asm_out_file);
4235       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4236       fputc (',', asm_out_file);
4237       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4238       break;
4239
4240     case INTERNAL_DW_OP_tls_addr:
4241       gcc_unreachable ();
4242
4243     default:
4244       /* Other codes have no operands.  */
4245       break;
4246     }
4247 }
4248
4249 static void
4250 output_loc_sequence_raw (dw_loc_descr_ref loc)
4251 {
4252   while (1)
4253     {
4254       /* Output the opcode.  */
4255       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4256       output_loc_operands_raw (loc);
4257
4258       if (!loc->dw_loc_next)
4259         break;
4260       loc = loc->dw_loc_next;
4261
4262       fputc (',', asm_out_file);
4263     }
4264 }
4265
4266 /* This routine will generate the correct assembly data for a location
4267    description based on a cfi entry with a complex address.  */
4268
4269 static void
4270 output_cfa_loc (dw_cfi_ref cfi)
4271 {
4272   dw_loc_descr_ref loc;
4273   unsigned long size;
4274
4275   if (cfi->dw_cfi_opc == DW_CFA_expression)
4276     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4277
4278   /* Output the size of the block.  */
4279   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4280   size = size_of_locs (loc);
4281   dw2_asm_output_data_uleb128 (size, NULL);
4282
4283   /* Now output the operations themselves.  */
4284   output_loc_sequence (loc);
4285 }
4286
4287 /* Similar, but used for .cfi_escape.  */
4288
4289 static void
4290 output_cfa_loc_raw (dw_cfi_ref cfi)
4291 {
4292   dw_loc_descr_ref loc;
4293   unsigned long size;
4294
4295   if (cfi->dw_cfi_opc == DW_CFA_expression)
4296     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4297
4298   /* Output the size of the block.  */
4299   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4300   size = size_of_locs (loc);
4301   dw2_asm_output_data_uleb128_raw (size);
4302   fputc (',', asm_out_file);
4303
4304   /* Now output the operations themselves.  */
4305   output_loc_sequence_raw (loc);
4306 }
4307
4308 /* This function builds a dwarf location descriptor sequence from a
4309    dw_cfa_location, adding the given OFFSET to the result of the
4310    expression.  */
4311
4312 static struct dw_loc_descr_struct *
4313 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4314 {
4315   struct dw_loc_descr_struct *head, *tmp;
4316
4317   offset += cfa->offset;
4318
4319   if (cfa->indirect)
4320     {
4321       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4322       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4323       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4324       add_loc_descr (&head, tmp);
4325       if (offset != 0)
4326         {
4327           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4328           add_loc_descr (&head, tmp);
4329         }
4330     }
4331   else
4332     head = new_reg_loc_descr (cfa->reg, offset);
4333
4334   return head;
4335 }
4336
4337 /* This function builds a dwarf location descriptor sequence for
4338    the address at OFFSET from the CFA when stack is aligned to
4339    ALIGNMENT byte.  */
4340
4341 static struct dw_loc_descr_struct *
4342 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4343 {
4344   struct dw_loc_descr_struct *head;
4345   unsigned int dwarf_fp
4346     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4347
4348  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4349   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4350     {
4351       head = new_reg_loc_descr (dwarf_fp, 0);
4352       add_loc_descr (&head, int_loc_descriptor (alignment));
4353       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4354
4355       add_loc_descr (&head, int_loc_descriptor (offset));
4356       add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4357     }
4358   else
4359     head = new_reg_loc_descr (dwarf_fp, offset);
4360   return head;
4361 }
4362
4363 /* This function fills in aa dw_cfa_location structure from a dwarf location
4364    descriptor sequence.  */
4365
4366 static void
4367 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4368 {
4369   struct dw_loc_descr_struct *ptr;
4370   cfa->offset = 0;
4371   cfa->base_offset = 0;
4372   cfa->indirect = 0;
4373   cfa->reg = -1;
4374
4375   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4376     {
4377       enum dwarf_location_atom op = ptr->dw_loc_opc;
4378
4379       switch (op)
4380         {
4381         case DW_OP_reg0:
4382         case DW_OP_reg1:
4383         case DW_OP_reg2:
4384         case DW_OP_reg3:
4385         case DW_OP_reg4:
4386         case DW_OP_reg5:
4387         case DW_OP_reg6:
4388         case DW_OP_reg7:
4389         case DW_OP_reg8:
4390         case DW_OP_reg9:
4391         case DW_OP_reg10:
4392         case DW_OP_reg11:
4393         case DW_OP_reg12:
4394         case DW_OP_reg13:
4395         case DW_OP_reg14:
4396         case DW_OP_reg15:
4397         case DW_OP_reg16:
4398         case DW_OP_reg17:
4399         case DW_OP_reg18:
4400         case DW_OP_reg19:
4401         case DW_OP_reg20:
4402         case DW_OP_reg21:
4403         case DW_OP_reg22:
4404         case DW_OP_reg23:
4405         case DW_OP_reg24:
4406         case DW_OP_reg25:
4407         case DW_OP_reg26:
4408         case DW_OP_reg27:
4409         case DW_OP_reg28:
4410         case DW_OP_reg29:
4411         case DW_OP_reg30:
4412         case DW_OP_reg31:
4413           cfa->reg = op - DW_OP_reg0;
4414           break;
4415         case DW_OP_regx:
4416           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4417           break;
4418         case DW_OP_breg0:
4419         case DW_OP_breg1:
4420         case DW_OP_breg2:
4421         case DW_OP_breg3:
4422         case DW_OP_breg4:
4423         case DW_OP_breg5:
4424         case DW_OP_breg6:
4425         case DW_OP_breg7:
4426         case DW_OP_breg8:
4427         case DW_OP_breg9:
4428         case DW_OP_breg10:
4429         case DW_OP_breg11:
4430         case DW_OP_breg12:
4431         case DW_OP_breg13:
4432         case DW_OP_breg14:
4433         case DW_OP_breg15:
4434         case DW_OP_breg16:
4435         case DW_OP_breg17:
4436         case DW_OP_breg18:
4437         case DW_OP_breg19:
4438         case DW_OP_breg20:
4439         case DW_OP_breg21:
4440         case DW_OP_breg22:
4441         case DW_OP_breg23:
4442         case DW_OP_breg24:
4443         case DW_OP_breg25:
4444         case DW_OP_breg26:
4445         case DW_OP_breg27:
4446         case DW_OP_breg28:
4447         case DW_OP_breg29:
4448         case DW_OP_breg30:
4449         case DW_OP_breg31:
4450           cfa->reg = op - DW_OP_breg0;
4451           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4452           break;
4453         case DW_OP_bregx:
4454           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4455           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4456           break;
4457         case DW_OP_deref:
4458           cfa->indirect = 1;
4459           break;
4460         case DW_OP_plus_uconst:
4461           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4462           break;
4463         default:
4464           internal_error ("DW_LOC_OP %s not implemented",
4465                           dwarf_stack_op_name (ptr->dw_loc_opc));
4466         }
4467     }
4468 }
4469 #endif /* .debug_frame support */
4470 \f
4471 /* And now, the support for symbolic debugging information.  */
4472 #ifdef DWARF2_DEBUGGING_INFO
4473
4474 /* .debug_str support.  */
4475 static int output_indirect_string (void **, void *);
4476
4477 static void dwarf2out_init (const char *);
4478 static void dwarf2out_finish (const char *);
4479 static void dwarf2out_define (unsigned int, const char *);
4480 static void dwarf2out_undef (unsigned int, const char *);
4481 static void dwarf2out_start_source_file (unsigned, const char *);
4482 static void dwarf2out_end_source_file (unsigned);
4483 static void dwarf2out_begin_block (unsigned, unsigned);
4484 static void dwarf2out_end_block (unsigned, unsigned);
4485 static bool dwarf2out_ignore_block (const_tree);
4486 static void dwarf2out_global_decl (tree);
4487 static void dwarf2out_type_decl (tree, int);
4488 static void dwarf2out_imported_module_or_decl (tree, tree);
4489 static void dwarf2out_abstract_function (tree);
4490 static void dwarf2out_var_location (rtx);
4491 static void dwarf2out_begin_function (tree);
4492
4493 /* The debug hooks structure.  */
4494
4495 const struct gcc_debug_hooks dwarf2_debug_hooks =
4496 {
4497   dwarf2out_init,
4498   dwarf2out_finish,
4499   dwarf2out_define,
4500   dwarf2out_undef,
4501   dwarf2out_start_source_file,
4502   dwarf2out_end_source_file,
4503   dwarf2out_begin_block,
4504   dwarf2out_end_block,
4505   dwarf2out_ignore_block,
4506   dwarf2out_source_line,
4507   dwarf2out_begin_prologue,
4508   debug_nothing_int_charstar,   /* end_prologue */
4509   dwarf2out_end_epilogue,
4510   dwarf2out_begin_function,
4511   debug_nothing_int,            /* end_function */
4512   dwarf2out_decl,               /* function_decl */
4513   dwarf2out_global_decl,
4514   dwarf2out_type_decl,          /* type_decl */
4515   dwarf2out_imported_module_or_decl,
4516   debug_nothing_tree,           /* deferred_inline_function */
4517   /* The DWARF 2 backend tries to reduce debugging bloat by not
4518      emitting the abstract description of inline functions until
4519      something tries to reference them.  */
4520   dwarf2out_abstract_function,  /* outlining_inline_function */
4521   debug_nothing_rtx,            /* label */
4522   debug_nothing_int,            /* handle_pch */
4523   dwarf2out_var_location,
4524   dwarf2out_switch_text_section,
4525   1                             /* start_end_main_source_file */
4526 };
4527 #endif
4528 \f
4529 /* NOTE: In the comments in this file, many references are made to
4530    "Debugging Information Entries".  This term is abbreviated as `DIE'
4531    throughout the remainder of this file.  */
4532
4533 /* An internal representation of the DWARF output is built, and then
4534    walked to generate the DWARF debugging info.  The walk of the internal
4535    representation is done after the entire program has been compiled.
4536    The types below are used to describe the internal representation.  */
4537
4538 /* Various DIE's use offsets relative to the beginning of the
4539    .debug_info section to refer to each other.  */
4540
4541 typedef long int dw_offset;
4542
4543 /* Define typedefs here to avoid circular dependencies.  */
4544
4545 typedef struct dw_attr_struct *dw_attr_ref;
4546 typedef struct dw_line_info_struct *dw_line_info_ref;
4547 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4548 typedef struct pubname_struct *pubname_ref;
4549 typedef struct dw_ranges_struct *dw_ranges_ref;
4550 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4551
4552 /* Each entry in the line_info_table maintains the file and
4553    line number associated with the label generated for that
4554    entry.  The label gives the PC value associated with
4555    the line number entry.  */
4556
4557 typedef struct dw_line_info_struct GTY(())
4558 {
4559   unsigned long dw_file_num;
4560   unsigned long dw_line_num;
4561 }
4562 dw_line_info_entry;
4563
4564 /* Line information for functions in separate sections; each one gets its
4565    own sequence.  */
4566 typedef struct dw_separate_line_info_struct GTY(())
4567 {
4568   unsigned long dw_file_num;
4569   unsigned long dw_line_num;
4570   unsigned long function;
4571 }
4572 dw_separate_line_info_entry;
4573
4574 /* Each DIE attribute has a field specifying the attribute kind,
4575    a link to the next attribute in the chain, and an attribute value.
4576    Attributes are typically linked below the DIE they modify.  */
4577
4578 typedef struct dw_attr_struct GTY(())
4579 {
4580   enum dwarf_attribute dw_attr;
4581   dw_val_node dw_attr_val;
4582 }
4583 dw_attr_node;
4584
4585 DEF_VEC_O(dw_attr_node);
4586 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4587
4588 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4589    The children of each node form a circular list linked by
4590    die_sib.  die_child points to the node *before* the "first" child node.  */
4591
4592 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4593 {
4594   enum dwarf_tag die_tag;
4595   char *die_symbol;
4596   VEC(dw_attr_node,gc) * die_attr;
4597   dw_die_ref die_parent;
4598   dw_die_ref die_child;
4599   dw_die_ref die_sib;
4600   dw_die_ref die_definition; /* ref from a specification to its definition */
4601   dw_offset die_offset;
4602   unsigned long die_abbrev;
4603   int die_mark;
4604   /* Die is used and must not be pruned as unused.  */
4605   int die_perennial_p;
4606   unsigned int decl_id;
4607 }
4608 die_node;
4609
4610 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4611 #define FOR_EACH_CHILD(die, c, expr) do {       \
4612   c = die->die_child;                           \
4613   if (c) do {                                   \
4614     c = c->die_sib;                             \
4615     expr;                                       \
4616   } while (c != die->die_child);                \
4617 } while (0)
4618
4619 /* The pubname structure */
4620
4621 typedef struct pubname_struct GTY(())
4622 {
4623   dw_die_ref die;
4624   const char *name;
4625 }
4626 pubname_entry;
4627
4628 DEF_VEC_O(pubname_entry);
4629 DEF_VEC_ALLOC_O(pubname_entry, gc);
4630
4631 struct dw_ranges_struct GTY(())
4632 {
4633   /* If this is positive, it's a block number, otherwise it's a
4634      bitwise-negated index into dw_ranges_by_label.  */
4635   int num;
4636 };
4637
4638 struct dw_ranges_by_label_struct GTY(())
4639 {
4640   const char *begin;
4641   const char *end;
4642 };
4643
4644 /* The limbo die list structure.  */
4645 typedef struct limbo_die_struct GTY(())
4646 {
4647   dw_die_ref die;
4648   tree created_for;
4649   struct limbo_die_struct *next;
4650 }
4651 limbo_die_node;
4652
4653 /* How to start an assembler comment.  */
4654 #ifndef ASM_COMMENT_START
4655 #define ASM_COMMENT_START ";#"
4656 #endif
4657
4658 /* Define a macro which returns nonzero for a TYPE_DECL which was
4659    implicitly generated for a tagged type.
4660
4661    Note that unlike the gcc front end (which generates a NULL named
4662    TYPE_DECL node for each complete tagged type, each array type, and
4663    each function type node created) the g++ front end generates a
4664    _named_ TYPE_DECL node for each tagged type node created.
4665    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4666    generate a DW_TAG_typedef DIE for them.  */
4667
4668 #define TYPE_DECL_IS_STUB(decl)                         \
4669   (DECL_NAME (decl) == NULL_TREE                        \
4670    || (DECL_ARTIFICIAL (decl)                           \
4671        && is_tagged_type (TREE_TYPE (decl))             \
4672        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4673            /* This is necessary for stub decls that     \
4674               appear in nested inline functions.  */    \
4675            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4676                && (decl_ultimate_origin (decl)          \
4677                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4678
4679 /* Information concerning the compilation unit's programming
4680    language, and compiler version.  */
4681
4682 /* Fixed size portion of the DWARF compilation unit header.  */
4683 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4684   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4685
4686 /* Fixed size portion of public names info.  */
4687 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4688
4689 /* Fixed size portion of the address range info.  */
4690 #define DWARF_ARANGES_HEADER_SIZE                                       \
4691   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4692                 DWARF2_ADDR_SIZE * 2)                                   \
4693    - DWARF_INITIAL_LENGTH_SIZE)
4694
4695 /* Size of padding portion in the address range info.  It must be
4696    aligned to twice the pointer size.  */
4697 #define DWARF_ARANGES_PAD_SIZE \
4698   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4699                 DWARF2_ADDR_SIZE * 2)                              \
4700    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4701
4702 /* Use assembler line directives if available.  */
4703 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4704 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4705 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4706 #else
4707 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4708 #endif
4709 #endif
4710
4711 /* Minimum line offset in a special line info. opcode.
4712    This value was chosen to give a reasonable range of values.  */
4713 #define DWARF_LINE_BASE  -10
4714
4715 /* First special line opcode - leave room for the standard opcodes.  */
4716 #define DWARF_LINE_OPCODE_BASE  10
4717
4718 /* Range of line offsets in a special line info. opcode.  */
4719 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4720
4721 /* Flag that indicates the initial value of the is_stmt_start flag.
4722    In the present implementation, we do not mark any lines as
4723    the beginning of a source statement, because that information
4724    is not made available by the GCC front-end.  */
4725 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4726
4727 #ifdef DWARF2_DEBUGGING_INFO
4728 /* This location is used by calc_die_sizes() to keep track
4729    the offset of each DIE within the .debug_info section.  */
4730 static unsigned long next_die_offset;
4731 #endif
4732
4733 /* Record the root of the DIE's built for the current compilation unit.  */
4734 static GTY(()) dw_die_ref comp_unit_die;
4735
4736 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4737 static GTY(()) limbo_die_node *limbo_die_list;
4738
4739 /* Filenames referenced by this compilation unit.  */
4740 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4741
4742 /* A hash table of references to DIE's that describe declarations.
4743    The key is a DECL_UID() which is a unique number identifying each decl.  */
4744 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4745
4746 /* Node of the variable location list.  */
4747 struct var_loc_node GTY ((chain_next ("%h.next")))
4748 {
4749   rtx GTY (()) var_loc_note;
4750   const char * GTY (()) label;
4751   const char * GTY (()) section_label;
4752   struct var_loc_node * GTY (()) next;
4753 };
4754
4755 /* Variable location list.  */
4756 struct var_loc_list_def GTY (())
4757 {
4758   struct var_loc_node * GTY (()) first;
4759
4760   /* Do not mark the last element of the chained list because
4761      it is marked through the chain.  */
4762   struct var_loc_node * GTY ((skip ("%h"))) last;
4763
4764   /* DECL_UID of the variable decl.  */
4765   unsigned int decl_id;
4766 };
4767 typedef struct var_loc_list_def var_loc_list;
4768
4769
4770 /* Table of decl location linked lists.  */
4771 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4772
4773 /* A pointer to the base of a list of references to DIE's that
4774    are uniquely identified by their tag, presence/absence of
4775    children DIE's, and list of attribute/value pairs.  */
4776 static GTY((length ("abbrev_die_table_allocated")))
4777   dw_die_ref *abbrev_die_table;
4778
4779 /* Number of elements currently allocated for abbrev_die_table.  */
4780 static GTY(()) unsigned abbrev_die_table_allocated;
4781
4782 /* Number of elements in type_die_table currently in use.  */
4783 static GTY(()) unsigned abbrev_die_table_in_use;
4784
4785 /* Size (in elements) of increments by which we may expand the
4786    abbrev_die_table.  */
4787 #define ABBREV_DIE_TABLE_INCREMENT 256
4788
4789 /* A pointer to the base of a table that contains line information
4790    for each source code line in .text in the compilation unit.  */
4791 static GTY((length ("line_info_table_allocated")))
4792      dw_line_info_ref line_info_table;
4793
4794 /* Number of elements currently allocated for line_info_table.  */
4795 static GTY(()) unsigned line_info_table_allocated;
4796
4797 /* Number of elements in line_info_table currently in use.  */
4798 static GTY(()) unsigned line_info_table_in_use;
4799
4800 /* A pointer to the base of a table that contains line information
4801    for each source code line outside of .text in the compilation unit.  */
4802 static GTY ((length ("separate_line_info_table_allocated")))
4803      dw_separate_line_info_ref separate_line_info_table;
4804
4805 /* Number of elements currently allocated for separate_line_info_table.  */
4806 static GTY(()) unsigned separate_line_info_table_allocated;
4807
4808 /* Number of elements in separate_line_info_table currently in use.  */
4809 static GTY(()) unsigned separate_line_info_table_in_use;
4810
4811 /* Size (in elements) of increments by which we may expand the
4812    line_info_table.  */
4813 #define LINE_INFO_TABLE_INCREMENT 1024
4814
4815 /* A pointer to the base of a table that contains a list of publicly
4816    accessible names.  */
4817 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4818
4819 /* A pointer to the base of a table that contains a list of publicly
4820    accessible types.  */
4821 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4822
4823 /* Array of dies for which we should generate .debug_arange info.  */
4824 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4825
4826 /* Number of elements currently allocated for arange_table.  */
4827 static GTY(()) unsigned arange_table_allocated;
4828
4829 /* Number of elements in arange_table currently in use.  */
4830 static GTY(()) unsigned arange_table_in_use;
4831
4832 /* Size (in elements) of increments by which we may expand the
4833    arange_table.  */
4834 #define ARANGE_TABLE_INCREMENT 64
4835
4836 /* Array of dies for which we should generate .debug_ranges info.  */
4837 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4838
4839 /* Number of elements currently allocated for ranges_table.  */
4840 static GTY(()) unsigned ranges_table_allocated;
4841
4842 /* Number of elements in ranges_table currently in use.  */
4843 static GTY(()) unsigned ranges_table_in_use;
4844
4845 /* Array of pairs of labels referenced in ranges_table.  */
4846 static GTY ((length ("ranges_by_label_allocated")))
4847      dw_ranges_by_label_ref ranges_by_label;
4848
4849 /* Number of elements currently allocated for ranges_by_label.  */
4850 static GTY(()) unsigned ranges_by_label_allocated;
4851
4852 /* Number of elements in ranges_by_label currently in use.  */
4853 static GTY(()) unsigned ranges_by_label_in_use;
4854
4855 /* Size (in elements) of increments by which we may expand the
4856    ranges_table.  */
4857 #define RANGES_TABLE_INCREMENT 64
4858
4859 /* Whether we have location lists that need outputting */
4860 static GTY(()) bool have_location_lists;
4861
4862 /* Unique label counter.  */
4863 static GTY(()) unsigned int loclabel_num;
4864
4865 #ifdef DWARF2_DEBUGGING_INFO
4866 /* Record whether the function being analyzed contains inlined functions.  */
4867 static int current_function_has_inlines;
4868 #endif
4869 #if 0 && defined (MIPS_DEBUGGING_INFO)
4870 static int comp_unit_has_inlines;
4871 #endif
4872
4873 /* The last file entry emitted by maybe_emit_file().  */
4874 static GTY(()) struct dwarf_file_data * last_emitted_file;
4875
4876 /* Number of internal labels generated by gen_internal_sym().  */
4877 static GTY(()) int label_num;
4878
4879 /* Cached result of previous call to lookup_filename.  */
4880 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4881
4882 #ifdef DWARF2_DEBUGGING_INFO
4883
4884 /* Offset from the "steady-state frame pointer" to the frame base,
4885    within the current function.  */
4886 static HOST_WIDE_INT frame_pointer_fb_offset;
4887
4888 /* Forward declarations for functions defined in this file.  */
4889
4890 static int is_pseudo_reg (const_rtx);
4891 static tree type_main_variant (tree);
4892 static int is_tagged_type (const_tree);
4893 static const char *dwarf_tag_name (unsigned);
4894 static const char *dwarf_attr_name (unsigned);
4895 static const char *dwarf_form_name (unsigned);
4896 static tree decl_ultimate_origin (const_tree);
4897 static tree block_ultimate_origin (const_tree);
4898 static tree decl_class_context (tree);
4899 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4900 static inline enum dw_val_class AT_class (dw_attr_ref);
4901 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4902 static inline unsigned AT_flag (dw_attr_ref);
4903 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4904 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4905 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4906 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4907 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4908                               unsigned long);
4909 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4910                                unsigned int, unsigned char *);
4911 static hashval_t debug_str_do_hash (const void *);
4912 static int debug_str_eq (const void *, const void *);
4913 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4914 static inline const char *AT_string (dw_attr_ref);
4915 static int AT_string_form (dw_attr_ref);
4916 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4917 static void add_AT_specification (dw_die_ref, dw_die_ref);
4918 static inline dw_die_ref AT_ref (dw_attr_ref);
4919 static inline int AT_ref_external (dw_attr_ref);
4920 static inline void set_AT_ref_external (dw_attr_ref, int);
4921 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4922 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4923 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4924 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4925                              dw_loc_list_ref);
4926 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4927 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4928 static inline rtx AT_addr (dw_attr_ref);
4929 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4930 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4931 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4932 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4933                            unsigned HOST_WIDE_INT);
4934 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4935                                unsigned long);
4936 static inline const char *AT_lbl (dw_attr_ref);
4937 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4938 static const char *get_AT_low_pc (dw_die_ref);
4939 static const char *get_AT_hi_pc (dw_die_ref);
4940 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4941 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4942 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4943 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4944 static bool is_c_family (void);
4945 static bool is_cxx (void);
4946 static bool is_java (void);
4947 static bool is_fortran (void);
4948 static bool is_ada (void);
4949 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4950 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4951 static void add_child_die (dw_die_ref, dw_die_ref);
4952 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4953 static dw_die_ref lookup_type_die (tree);
4954 static void equate_type_number_to_die (tree, dw_die_ref);
4955 static hashval_t decl_die_table_hash (const void *);
4956 static int decl_die_table_eq (const void *, const void *);
4957 static dw_die_ref lookup_decl_die (tree);
4958 static hashval_t decl_loc_table_hash (const void *);
4959 static int decl_loc_table_eq (const void *, const void *);
4960 static var_loc_list *lookup_decl_loc (const_tree);
4961 static void equate_decl_number_to_die (tree, dw_die_ref);
4962 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4963 static void print_spaces (FILE *);
4964 static void print_die (dw_die_ref, FILE *);
4965 static void print_dwarf_line_table (FILE *);
4966 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4967 static dw_die_ref pop_compile_unit (dw_die_ref);
4968 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4969 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4970 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4971 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4972 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4973 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4974 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4975 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4976 static void compute_section_prefix (dw_die_ref);
4977 static int is_type_die (dw_die_ref);
4978 static int is_comdat_die (dw_die_ref);
4979 static int is_symbol_die (dw_die_ref);
4980 static void assign_symbol_names (dw_die_ref);
4981 static void break_out_includes (dw_die_ref);
4982 static hashval_t htab_cu_hash (const void *);
4983 static int htab_cu_eq (const void *, const void *);
4984 static void htab_cu_del (void *);
4985 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4986 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4987 static void add_sibling_attributes (dw_die_ref);
4988 static void build_abbrev_table (dw_die_ref);
4989 static void output_location_lists (dw_die_ref);
4990 static int constant_size (long unsigned);
4991 static unsigned long size_of_die (dw_die_ref);
4992 static void calc_die_sizes (dw_die_ref);
4993 static void mark_dies (dw_die_ref);
4994 static void unmark_dies (dw_die_ref);
4995 static void unmark_all_dies (dw_die_ref);
4996 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4997 static unsigned long size_of_aranges (void);
4998 static enum dwarf_form value_format (dw_attr_ref);
4999 static void output_value_format (dw_attr_ref);
5000 static void output_abbrev_section (void);
5001 static void output_die_symbol (dw_die_ref);
5002 static void output_die (dw_die_ref);
5003 static void output_compilation_unit_header (void);
5004 static void output_comp_unit (dw_die_ref, int);
5005 static const char *dwarf2_name (tree, int);
5006 static void add_pubname (tree, dw_die_ref);
5007 static void add_pubname_string (const char *, dw_die_ref);
5008 static void add_pubtype (tree, dw_die_ref);
5009 static void output_pubnames (VEC (pubname_entry,gc) *);
5010 static void add_arange (tree, dw_die_ref);
5011 static void output_aranges (void);
5012 static unsigned int add_ranges_num (int);
5013 static unsigned int add_ranges (const_tree);
5014 static unsigned int add_ranges_by_labels (const char *, const char *);
5015 static void output_ranges (void);
5016 static void output_line_info (void);
5017 static void output_file_names (void);
5018 static dw_die_ref base_type_die (tree);
5019 static int is_base_type (tree);
5020 static bool is_subrange_type (const_tree);
5021 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5022 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5023 static int type_is_enum (const_tree);
5024 static unsigned int dbx_reg_number (const_rtx);
5025 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5026 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5027 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5028                                                 enum var_init_status);
5029 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5030                                                      enum var_init_status);
5031 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5032                                          enum var_init_status);
5033 static int is_based_loc (const_rtx);
5034 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5035                                             enum var_init_status);
5036 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5037                                                enum var_init_status);
5038 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5039 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5040 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5041 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5042 static tree field_type (const_tree);
5043 static unsigned int simple_type_align_in_bits (const_tree);
5044 static unsigned int simple_decl_align_in_bits (const_tree);
5045 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5046 static HOST_WIDE_INT field_byte_offset (const_tree);
5047 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5048                                          dw_loc_descr_ref);
5049 static void add_data_member_location_attribute (dw_die_ref, tree);
5050 static void add_const_value_attribute (dw_die_ref, rtx);
5051 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5052 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5053 static void insert_float (const_rtx, unsigned char *);
5054 static rtx rtl_for_decl_location (tree);
5055 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5056                                                    enum dwarf_attribute);
5057 static void tree_add_const_value_attribute (dw_die_ref, tree);
5058 static void add_name_attribute (dw_die_ref, const char *);
5059 static void add_comp_dir_attribute (dw_die_ref);
5060 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5061 static void add_subscript_info (dw_die_ref, tree, bool);
5062 static void add_byte_size_attribute (dw_die_ref, tree);
5063 static void add_bit_offset_attribute (dw_die_ref, tree);
5064 static void add_bit_size_attribute (dw_die_ref, tree);
5065 static void add_prototyped_attribute (dw_die_ref, tree);
5066 static void add_abstract_origin_attribute (dw_die_ref, tree);
5067 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5068 static void add_src_coords_attributes (dw_die_ref, tree);
5069 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5070 static void push_decl_scope (tree);
5071 static void pop_decl_scope (void);
5072 static dw_die_ref scope_die_for (tree, dw_die_ref);
5073 static inline int local_scope_p (dw_die_ref);
5074 static inline int class_or_namespace_scope_p (dw_die_ref);
5075 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5076 static void add_calling_convention_attribute (dw_die_ref, tree);
5077 static const char *type_tag (const_tree);
5078 static tree member_declared_type (const_tree);
5079 #if 0
5080 static const char *decl_start_label (tree);
5081 #endif
5082 static void gen_array_type_die (tree, dw_die_ref);
5083 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5084 #if 0
5085 static void gen_entry_point_die (tree, dw_die_ref);
5086 #endif
5087 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
5088 static void gen_inlined_structure_type_die (tree, dw_die_ref);
5089 static void gen_inlined_union_type_die (tree, dw_die_ref);
5090 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5091 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
5092 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5093 static void gen_formal_types_die (tree, dw_die_ref);
5094 static void gen_subprogram_die (tree, dw_die_ref);
5095 static void gen_variable_die (tree, dw_die_ref);
5096 static void gen_label_die (tree, dw_die_ref);
5097 static void gen_lexical_block_die (tree, dw_die_ref, int);
5098 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5099 static void gen_field_die (tree, dw_die_ref);
5100 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5101 static dw_die_ref gen_compile_unit_die (const char *);
5102 static void gen_inheritance_die (tree, tree, dw_die_ref);
5103 static void gen_member_die (tree, dw_die_ref);
5104 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5105                                                 enum debug_info_usage);
5106 static void gen_subroutine_type_die (tree, dw_die_ref);
5107 static void gen_typedef_die (tree, dw_die_ref);
5108 static void gen_type_die (tree, dw_die_ref);
5109 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
5110 static void gen_block_die (tree, dw_die_ref, int);
5111 static void decls_for_scope (tree, dw_die_ref, int);
5112 static int is_redundant_typedef (const_tree);
5113 static void gen_namespace_die (tree);
5114 static void gen_decl_die (tree, dw_die_ref);
5115 static dw_die_ref force_decl_die (tree);
5116 static dw_die_ref force_type_die (tree);
5117 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5118 static void declare_in_namespace (tree, dw_die_ref);
5119 static struct dwarf_file_data * lookup_filename (const char *);
5120 static void retry_incomplete_types (void);
5121 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5122 static void splice_child_die (dw_die_ref, dw_die_ref);
5123 static int file_info_cmp (const void *, const void *);
5124 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5125                                      const char *, const char *, unsigned);
5126 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5127                                        const char *, const char *,
5128                                        const char *);
5129 static void output_loc_list (dw_loc_list_ref);
5130 static char *gen_internal_sym (const char *);
5131
5132 static void prune_unmark_dies (dw_die_ref);
5133 static void prune_unused_types_mark (dw_die_ref, int);
5134 static void prune_unused_types_walk (dw_die_ref);
5135 static void prune_unused_types_walk_attribs (dw_die_ref);
5136 static void prune_unused_types_prune (dw_die_ref);
5137 static void prune_unused_types (void);
5138 static int maybe_emit_file (struct dwarf_file_data *fd);
5139
5140 /* Section names used to hold DWARF debugging information.  */
5141 #ifndef DEBUG_INFO_SECTION
5142 #define DEBUG_INFO_SECTION      ".debug_info"
5143 #endif
5144 #ifndef DEBUG_ABBREV_SECTION
5145 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5146 #endif
5147 #ifndef DEBUG_ARANGES_SECTION
5148 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5149 #endif
5150 #ifndef DEBUG_MACINFO_SECTION
5151 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5152 #endif
5153 #ifndef DEBUG_LINE_SECTION
5154 #define DEBUG_LINE_SECTION      ".debug_line"
5155 #endif
5156 #ifndef DEBUG_LOC_SECTION
5157 #define DEBUG_LOC_SECTION       ".debug_loc"
5158 #endif
5159 #ifndef DEBUG_PUBNAMES_SECTION
5160 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5161 #endif
5162 #ifndef DEBUG_STR_SECTION
5163 #define DEBUG_STR_SECTION       ".debug_str"
5164 #endif
5165 #ifndef DEBUG_RANGES_SECTION
5166 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5167 #endif
5168
5169 /* Standard ELF section names for compiled code and data.  */
5170 #ifndef TEXT_SECTION_NAME
5171 #define TEXT_SECTION_NAME       ".text"
5172 #endif
5173
5174 /* Section flags for .debug_str section.  */
5175 #define DEBUG_STR_SECTION_FLAGS \
5176   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5177    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5178    : SECTION_DEBUG)
5179
5180 /* Labels we insert at beginning sections we can reference instead of
5181    the section names themselves.  */
5182
5183 #ifndef TEXT_SECTION_LABEL
5184 #define TEXT_SECTION_LABEL              "Ltext"
5185 #endif
5186 #ifndef COLD_TEXT_SECTION_LABEL
5187 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5188 #endif
5189 #ifndef DEBUG_LINE_SECTION_LABEL
5190 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5191 #endif
5192 #ifndef DEBUG_INFO_SECTION_LABEL
5193 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5194 #endif
5195 #ifndef DEBUG_ABBREV_SECTION_LABEL
5196 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5197 #endif
5198 #ifndef DEBUG_LOC_SECTION_LABEL
5199 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5200 #endif
5201 #ifndef DEBUG_RANGES_SECTION_LABEL
5202 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5203 #endif
5204 #ifndef DEBUG_MACINFO_SECTION_LABEL
5205 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5206 #endif
5207
5208 /* Definitions of defaults for formats and names of various special
5209    (artificial) labels which may be generated within this file (when the -g
5210    options is used and DWARF2_DEBUGGING_INFO is in effect.
5211    If necessary, these may be overridden from within the tm.h file, but
5212    typically, overriding these defaults is unnecessary.  */
5213
5214 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5215 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5216 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5217 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5218 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5219 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5220 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5221 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5222 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5223 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5224
5225 #ifndef TEXT_END_LABEL
5226 #define TEXT_END_LABEL          "Letext"
5227 #endif
5228 #ifndef COLD_END_LABEL
5229 #define COLD_END_LABEL          "Letext_cold"
5230 #endif
5231 #ifndef BLOCK_BEGIN_LABEL
5232 #define BLOCK_BEGIN_LABEL       "LBB"
5233 #endif
5234 #ifndef BLOCK_END_LABEL
5235 #define BLOCK_END_LABEL         "LBE"
5236 #endif
5237 #ifndef LINE_CODE_LABEL
5238 #define LINE_CODE_LABEL         "LM"
5239 #endif
5240 #ifndef SEPARATE_LINE_CODE_LABEL
5241 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5242 #endif
5243
5244 \f
5245 /* We allow a language front-end to designate a function that is to be
5246    called to "demangle" any name before it is put into a DIE.  */
5247
5248 static const char *(*demangle_name_func) (const char *);
5249
5250 void
5251 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5252 {
5253   demangle_name_func = func;
5254 }
5255
5256 /* Test if rtl node points to a pseudo register.  */
5257
5258 static inline int
5259 is_pseudo_reg (const_rtx rtl)
5260 {
5261   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5262           || (GET_CODE (rtl) == SUBREG
5263               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5264 }
5265
5266 /* Return a reference to a type, with its const and volatile qualifiers
5267    removed.  */
5268
5269 static inline tree
5270 type_main_variant (tree type)
5271 {
5272   type = TYPE_MAIN_VARIANT (type);
5273
5274   /* ??? There really should be only one main variant among any group of
5275      variants of a given type (and all of the MAIN_VARIANT values for all
5276      members of the group should point to that one type) but sometimes the C
5277      front-end messes this up for array types, so we work around that bug
5278      here.  */
5279   if (TREE_CODE (type) == ARRAY_TYPE)
5280     while (type != TYPE_MAIN_VARIANT (type))
5281       type = TYPE_MAIN_VARIANT (type);
5282
5283   return type;
5284 }
5285
5286 /* Return nonzero if the given type node represents a tagged type.  */
5287
5288 static inline int
5289 is_tagged_type (const_tree type)
5290 {
5291   enum tree_code code = TREE_CODE (type);
5292
5293   return (code == RECORD_TYPE || code == UNION_TYPE
5294           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5295 }
5296
5297 /* Convert a DIE tag into its string name.  */
5298
5299 static const char *
5300 dwarf_tag_name (unsigned int tag)
5301 {
5302   switch (tag)
5303     {
5304     case DW_TAG_padding:
5305       return "DW_TAG_padding";
5306     case DW_TAG_array_type:
5307       return "DW_TAG_array_type";
5308     case DW_TAG_class_type:
5309       return "DW_TAG_class_type";
5310     case DW_TAG_entry_point:
5311       return "DW_TAG_entry_point";
5312     case DW_TAG_enumeration_type:
5313       return "DW_TAG_enumeration_type";
5314     case DW_TAG_formal_parameter:
5315       return "DW_TAG_formal_parameter";
5316     case DW_TAG_imported_declaration:
5317       return "DW_TAG_imported_declaration";
5318     case DW_TAG_label:
5319       return "DW_TAG_label";
5320     case DW_TAG_lexical_block:
5321       return "DW_TAG_lexical_block";
5322     case DW_TAG_member:
5323       return "DW_TAG_member";
5324     case DW_TAG_pointer_type:
5325       return "DW_TAG_pointer_type";
5326     case DW_TAG_reference_type:
5327       return "DW_TAG_reference_type";
5328     case DW_TAG_compile_unit:
5329       return "DW_TAG_compile_unit";
5330     case DW_TAG_string_type:
5331       return "DW_TAG_string_type";
5332     case DW_TAG_structure_type:
5333       return "DW_TAG_structure_type";
5334     case DW_TAG_subroutine_type:
5335       return "DW_TAG_subroutine_type";
5336     case DW_TAG_typedef:
5337       return "DW_TAG_typedef";
5338     case DW_TAG_union_type:
5339       return "DW_TAG_union_type";
5340     case DW_TAG_unspecified_parameters:
5341       return "DW_TAG_unspecified_parameters";
5342     case DW_TAG_variant:
5343       return "DW_TAG_variant";
5344     case DW_TAG_common_block:
5345       return "DW_TAG_common_block";
5346     case DW_TAG_common_inclusion:
5347       return "DW_TAG_common_inclusion";
5348     case DW_TAG_inheritance:
5349       return "DW_TAG_inheritance";
5350     case DW_TAG_inlined_subroutine:
5351       return "DW_TAG_inlined_subroutine";
5352     case DW_TAG_module:
5353       return "DW_TAG_module";
5354     case DW_TAG_ptr_to_member_type:
5355       return "DW_TAG_ptr_to_member_type";
5356     case DW_TAG_set_type:
5357       return "DW_TAG_set_type";
5358     case DW_TAG_subrange_type:
5359       return "DW_TAG_subrange_type";
5360     case DW_TAG_with_stmt:
5361       return "DW_TAG_with_stmt";
5362     case DW_TAG_access_declaration:
5363       return "DW_TAG_access_declaration";
5364     case DW_TAG_base_type:
5365       return "DW_TAG_base_type";
5366     case DW_TAG_catch_block:
5367       return "DW_TAG_catch_block";
5368     case DW_TAG_const_type:
5369       return "DW_TAG_const_type";
5370     case DW_TAG_constant:
5371       return "DW_TAG_constant";
5372     case DW_TAG_enumerator:
5373       return "DW_TAG_enumerator";
5374     case DW_TAG_file_type:
5375       return "DW_TAG_file_type";
5376     case DW_TAG_friend:
5377       return "DW_TAG_friend";
5378     case DW_TAG_namelist:
5379       return "DW_TAG_namelist";
5380     case DW_TAG_namelist_item:
5381       return "DW_TAG_namelist_item";
5382     case DW_TAG_packed_type:
5383       return "DW_TAG_packed_type";
5384     case DW_TAG_subprogram:
5385       return "DW_TAG_subprogram";
5386     case DW_TAG_template_type_param:
5387       return "DW_TAG_template_type_param";
5388     case DW_TAG_template_value_param:
5389       return "DW_TAG_template_value_param";
5390     case DW_TAG_thrown_type:
5391       return "DW_TAG_thrown_type";
5392     case DW_TAG_try_block:
5393       return "DW_TAG_try_block";
5394     case DW_TAG_variant_part:
5395       return "DW_TAG_variant_part";
5396     case DW_TAG_variable:
5397       return "DW_TAG_variable";
5398     case DW_TAG_volatile_type:
5399       return "DW_TAG_volatile_type";
5400     case DW_TAG_dwarf_procedure:
5401       return "DW_TAG_dwarf_procedure";
5402     case DW_TAG_restrict_type:
5403       return "DW_TAG_restrict_type";
5404     case DW_TAG_interface_type:
5405       return "DW_TAG_interface_type";
5406     case DW_TAG_namespace:
5407       return "DW_TAG_namespace";
5408     case DW_TAG_imported_module:
5409       return "DW_TAG_imported_module";
5410     case DW_TAG_unspecified_type:
5411       return "DW_TAG_unspecified_type";
5412     case DW_TAG_partial_unit:
5413       return "DW_TAG_partial_unit";
5414     case DW_TAG_imported_unit:
5415       return "DW_TAG_imported_unit";
5416     case DW_TAG_condition:
5417       return "DW_TAG_condition";
5418     case DW_TAG_shared_type:
5419       return "DW_TAG_shared_type";
5420     case DW_TAG_MIPS_loop:
5421       return "DW_TAG_MIPS_loop";
5422     case DW_TAG_format_label:
5423       return "DW_TAG_format_label";
5424     case DW_TAG_function_template:
5425       return "DW_TAG_function_template";
5426     case DW_TAG_class_template:
5427       return "DW_TAG_class_template";
5428     case DW_TAG_GNU_BINCL:
5429       return "DW_TAG_GNU_BINCL";
5430     case DW_TAG_GNU_EINCL:
5431       return "DW_TAG_GNU_EINCL";
5432     default:
5433       return "DW_TAG_<unknown>";
5434     }
5435 }
5436
5437 /* Convert a DWARF attribute code into its string name.  */
5438
5439 static const char *
5440 dwarf_attr_name (unsigned int attr)
5441 {
5442   switch (attr)
5443     {
5444     case DW_AT_sibling:
5445       return "DW_AT_sibling";
5446     case DW_AT_location:
5447       return "DW_AT_location";
5448     case DW_AT_name:
5449       return "DW_AT_name";
5450     case DW_AT_ordering:
5451       return "DW_AT_ordering";
5452     case DW_AT_subscr_data:
5453       return "DW_AT_subscr_data";
5454     case DW_AT_byte_size:
5455       return "DW_AT_byte_size";
5456     case DW_AT_bit_offset:
5457       return "DW_AT_bit_offset";
5458     case DW_AT_bit_size:
5459       return "DW_AT_bit_size";
5460     case DW_AT_element_list:
5461       return "DW_AT_element_list";
5462     case DW_AT_stmt_list:
5463       return "DW_AT_stmt_list";
5464     case DW_AT_low_pc:
5465       return "DW_AT_low_pc";
5466     case DW_AT_high_pc:
5467       return "DW_AT_high_pc";
5468     case DW_AT_language:
5469       return "DW_AT_language";
5470     case DW_AT_member:
5471       return "DW_AT_member";
5472     case DW_AT_discr:
5473       return "DW_AT_discr";
5474     case DW_AT_discr_value:
5475       return "DW_AT_discr_value";
5476     case DW_AT_visibility:
5477       return "DW_AT_visibility";
5478     case DW_AT_import:
5479       return "DW_AT_import";
5480     case DW_AT_string_length:
5481       return "DW_AT_string_length";
5482     case DW_AT_common_reference:
5483       return "DW_AT_common_reference";
5484     case DW_AT_comp_dir:
5485       return "DW_AT_comp_dir";
5486     case DW_AT_const_value:
5487       return "DW_AT_const_value";
5488     case DW_AT_containing_type:
5489       return "DW_AT_containing_type";
5490     case DW_AT_default_value:
5491       return "DW_AT_default_value";
5492     case DW_AT_inline:
5493       return "DW_AT_inline";
5494     case DW_AT_is_optional:
5495       return "DW_AT_is_optional";
5496     case DW_AT_lower_bound:
5497       return "DW_AT_lower_bound";
5498     case DW_AT_producer:
5499       return "DW_AT_producer";
5500     case DW_AT_prototyped:
5501       return "DW_AT_prototyped";
5502     case DW_AT_return_addr:
5503       return "DW_AT_return_addr";
5504     case DW_AT_start_scope:
5505       return "DW_AT_start_scope";
5506     case DW_AT_bit_stride:
5507       return "DW_AT_bit_stride";
5508     case DW_AT_upper_bound:
5509       return "DW_AT_upper_bound";
5510     case DW_AT_abstract_origin:
5511       return "DW_AT_abstract_origin";
5512     case DW_AT_accessibility:
5513       return "DW_AT_accessibility";
5514     case DW_AT_address_class:
5515       return "DW_AT_address_class";
5516     case DW_AT_artificial:
5517       return "DW_AT_artificial";
5518     case DW_AT_base_types:
5519       return "DW_AT_base_types";
5520     case DW_AT_calling_convention:
5521       return "DW_AT_calling_convention";
5522     case DW_AT_count:
5523       return "DW_AT_count";
5524     case DW_AT_data_member_location:
5525       return "DW_AT_data_member_location";
5526     case DW_AT_decl_column:
5527       return "DW_AT_decl_column";
5528     case DW_AT_decl_file:
5529       return "DW_AT_decl_file";
5530     case DW_AT_decl_line:
5531       return "DW_AT_decl_line";
5532     case DW_AT_declaration:
5533       return "DW_AT_declaration";
5534     case DW_AT_discr_list:
5535       return "DW_AT_discr_list";
5536     case DW_AT_encoding:
5537       return "DW_AT_encoding";
5538     case DW_AT_external:
5539       return "DW_AT_external";
5540     case DW_AT_frame_base:
5541       return "DW_AT_frame_base";
5542     case DW_AT_friend:
5543       return "DW_AT_friend";
5544     case DW_AT_identifier_case:
5545       return "DW_AT_identifier_case";
5546     case DW_AT_macro_info:
5547       return "DW_AT_macro_info";
5548     case DW_AT_namelist_items:
5549       return "DW_AT_namelist_items";
5550     case DW_AT_priority:
5551       return "DW_AT_priority";
5552     case DW_AT_segment:
5553       return "DW_AT_segment";
5554     case DW_AT_specification:
5555       return "DW_AT_specification";
5556     case DW_AT_static_link:
5557       return "DW_AT_static_link";
5558     case DW_AT_type:
5559       return "DW_AT_type";
5560     case DW_AT_use_location:
5561       return "DW_AT_use_location";
5562     case DW_AT_variable_parameter:
5563       return "DW_AT_variable_parameter";
5564     case DW_AT_virtuality:
5565       return "DW_AT_virtuality";
5566     case DW_AT_vtable_elem_location:
5567       return "DW_AT_vtable_elem_location";
5568
5569     case DW_AT_allocated:
5570       return "DW_AT_allocated";
5571     case DW_AT_associated:
5572       return "DW_AT_associated";
5573     case DW_AT_data_location:
5574       return "DW_AT_data_location";
5575     case DW_AT_byte_stride:
5576       return "DW_AT_byte_stride";
5577     case DW_AT_entry_pc:
5578       return "DW_AT_entry_pc";
5579     case DW_AT_use_UTF8:
5580       return "DW_AT_use_UTF8";
5581     case DW_AT_extension:
5582       return "DW_AT_extension";
5583     case DW_AT_ranges:
5584       return "DW_AT_ranges";
5585     case DW_AT_trampoline:
5586       return "DW_AT_trampoline";
5587     case DW_AT_call_column:
5588       return "DW_AT_call_column";
5589     case DW_AT_call_file:
5590       return "DW_AT_call_file";
5591     case DW_AT_call_line:
5592       return "DW_AT_call_line";
5593
5594     case DW_AT_MIPS_fde:
5595       return "DW_AT_MIPS_fde";
5596     case DW_AT_MIPS_loop_begin:
5597       return "DW_AT_MIPS_loop_begin";
5598     case DW_AT_MIPS_tail_loop_begin:
5599       return "DW_AT_MIPS_tail_loop_begin";
5600     case DW_AT_MIPS_epilog_begin:
5601       return "DW_AT_MIPS_epilog_begin";
5602     case DW_AT_MIPS_loop_unroll_factor:
5603       return "DW_AT_MIPS_loop_unroll_factor";
5604     case DW_AT_MIPS_software_pipeline_depth:
5605       return "DW_AT_MIPS_software_pipeline_depth";
5606     case DW_AT_MIPS_linkage_name:
5607       return "DW_AT_MIPS_linkage_name";
5608     case DW_AT_MIPS_stride:
5609       return "DW_AT_MIPS_stride";
5610     case DW_AT_MIPS_abstract_name:
5611       return "DW_AT_MIPS_abstract_name";
5612     case DW_AT_MIPS_clone_origin:
5613       return "DW_AT_MIPS_clone_origin";
5614     case DW_AT_MIPS_has_inlines:
5615       return "DW_AT_MIPS_has_inlines";
5616
5617     case DW_AT_sf_names:
5618       return "DW_AT_sf_names";
5619     case DW_AT_src_info:
5620       return "DW_AT_src_info";
5621     case DW_AT_mac_info:
5622       return "DW_AT_mac_info";
5623     case DW_AT_src_coords:
5624       return "DW_AT_src_coords";
5625     case DW_AT_body_begin:
5626       return "DW_AT_body_begin";
5627     case DW_AT_body_end:
5628       return "DW_AT_body_end";
5629     case DW_AT_GNU_vector:
5630       return "DW_AT_GNU_vector";
5631
5632     case DW_AT_VMS_rtnbeg_pd_address:
5633       return "DW_AT_VMS_rtnbeg_pd_address";
5634
5635     default:
5636       return "DW_AT_<unknown>";
5637     }
5638 }
5639
5640 /* Convert a DWARF value form code into its string name.  */
5641
5642 static const char *
5643 dwarf_form_name (unsigned int form)
5644 {
5645   switch (form)
5646     {
5647     case DW_FORM_addr:
5648       return "DW_FORM_addr";
5649     case DW_FORM_block2:
5650       return "DW_FORM_block2";
5651     case DW_FORM_block4:
5652       return "DW_FORM_block4";
5653     case DW_FORM_data2:
5654       return "DW_FORM_data2";
5655     case DW_FORM_data4:
5656       return "DW_FORM_data4";
5657     case DW_FORM_data8:
5658       return "DW_FORM_data8";
5659     case DW_FORM_string:
5660       return "DW_FORM_string";
5661     case DW_FORM_block:
5662       return "DW_FORM_block";
5663     case DW_FORM_block1:
5664       return "DW_FORM_block1";
5665     case DW_FORM_data1:
5666       return "DW_FORM_data1";
5667     case DW_FORM_flag:
5668       return "DW_FORM_flag";
5669     case DW_FORM_sdata:
5670       return "DW_FORM_sdata";
5671     case DW_FORM_strp:
5672       return "DW_FORM_strp";
5673     case DW_FORM_udata:
5674       return "DW_FORM_udata";
5675     case DW_FORM_ref_addr:
5676       return "DW_FORM_ref_addr";
5677     case DW_FORM_ref1:
5678       return "DW_FORM_ref1";
5679     case DW_FORM_ref2:
5680       return "DW_FORM_ref2";
5681     case DW_FORM_ref4:
5682       return "DW_FORM_ref4";
5683     case DW_FORM_ref8:
5684       return "DW_FORM_ref8";
5685     case DW_FORM_ref_udata:
5686       return "DW_FORM_ref_udata";
5687     case DW_FORM_indirect:
5688       return "DW_FORM_indirect";
5689     default:
5690       return "DW_FORM_<unknown>";
5691     }
5692 }
5693 \f
5694 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5695    instance of an inlined instance of a decl which is local to an inline
5696    function, so we have to trace all of the way back through the origin chain
5697    to find out what sort of node actually served as the original seed for the
5698    given block.  */
5699
5700 static tree
5701 decl_ultimate_origin (const_tree decl)
5702 {
5703   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5704     return NULL_TREE;
5705
5706   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5707      nodes in the function to point to themselves; ignore that if
5708      we're trying to output the abstract instance of this function.  */
5709   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5710     return NULL_TREE;
5711
5712   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5713      most distant ancestor, this should never happen.  */
5714   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5715
5716   return DECL_ABSTRACT_ORIGIN (decl);
5717 }
5718
5719 /* Determine the "ultimate origin" of a block.  The block may be an inlined
5720    instance of an inlined instance of a block which is local to an inline
5721    function, so we have to trace all of the way back through the origin chain
5722    to find out what sort of node actually served as the original seed for the
5723    given block.  */
5724
5725 static tree
5726 block_ultimate_origin (const_tree block)
5727 {
5728   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
5729
5730   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
5731      nodes in the function to point to themselves; ignore that if
5732      we're trying to output the abstract instance of this function.  */
5733   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
5734     return NULL_TREE;
5735
5736   if (immediate_origin == NULL_TREE)
5737     return NULL_TREE;
5738   else
5739     {
5740       tree ret_val;
5741       tree lookahead = immediate_origin;
5742
5743       do
5744         {
5745           ret_val = lookahead;
5746           lookahead = (TREE_CODE (ret_val) == BLOCK
5747                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5748         }
5749       while (lookahead != NULL && lookahead != ret_val);
5750
5751       /* The block's abstract origin chain may not be the *ultimate* origin of
5752          the block. It could lead to a DECL that has an abstract origin set.
5753          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5754          will give us if it has one).  Note that DECL's abstract origins are
5755          supposed to be the most distant ancestor (or so decl_ultimate_origin
5756          claims), so we don't need to loop following the DECL origins.  */
5757       if (DECL_P (ret_val))
5758         return DECL_ORIGIN (ret_val);
5759
5760       return ret_val;
5761     }
5762 }
5763
5764 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5765    of a virtual function may refer to a base class, so we check the 'this'
5766    parameter.  */
5767
5768 static tree
5769 decl_class_context (tree decl)
5770 {
5771   tree context = NULL_TREE;
5772
5773   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5774     context = DECL_CONTEXT (decl);
5775   else
5776     context = TYPE_MAIN_VARIANT
5777       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5778
5779   if (context && !TYPE_P (context))
5780     context = NULL_TREE;
5781
5782   return context;
5783 }
5784 \f
5785 /* Add an attribute/value pair to a DIE.  */
5786
5787 static inline void
5788 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5789 {
5790   /* Maybe this should be an assert?  */
5791   if (die == NULL)
5792     return;
5793
5794   if (die->die_attr == NULL)
5795     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5796   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5797 }
5798
5799 static inline enum dw_val_class
5800 AT_class (dw_attr_ref a)
5801 {
5802   return a->dw_attr_val.val_class;
5803 }
5804
5805 /* Add a flag value attribute to a DIE.  */
5806
5807 static inline void
5808 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5809 {
5810   dw_attr_node attr;
5811
5812   attr.dw_attr = attr_kind;
5813   attr.dw_attr_val.val_class = dw_val_class_flag;
5814   attr.dw_attr_val.v.val_flag = flag;
5815   add_dwarf_attr (die, &attr);
5816 }
5817
5818 static inline unsigned
5819 AT_flag (dw_attr_ref a)
5820 {
5821   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5822   return a->dw_attr_val.v.val_flag;
5823 }
5824
5825 /* Add a signed integer attribute value to a DIE.  */
5826
5827 static inline void
5828 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5829 {
5830   dw_attr_node attr;
5831
5832   attr.dw_attr = attr_kind;
5833   attr.dw_attr_val.val_class = dw_val_class_const;
5834   attr.dw_attr_val.v.val_int = int_val;
5835   add_dwarf_attr (die, &attr);
5836 }
5837
5838 static inline HOST_WIDE_INT
5839 AT_int (dw_attr_ref a)
5840 {
5841   gcc_assert (a && AT_class (a) == dw_val_class_const);
5842   return a->dw_attr_val.v.val_int;
5843 }
5844
5845 /* Add an unsigned integer attribute value to a DIE.  */
5846
5847 static inline void
5848 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5849                  unsigned HOST_WIDE_INT unsigned_val)
5850 {
5851   dw_attr_node attr;
5852
5853   attr.dw_attr = attr_kind;
5854   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5855   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5856   add_dwarf_attr (die, &attr);
5857 }
5858
5859 static inline unsigned HOST_WIDE_INT
5860 AT_unsigned (dw_attr_ref a)
5861 {
5862   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5863   return a->dw_attr_val.v.val_unsigned;
5864 }
5865
5866 /* Add an unsigned double integer attribute value to a DIE.  */
5867
5868 static inline void
5869 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5870                   long unsigned int val_hi, long unsigned int val_low)
5871 {
5872   dw_attr_node attr;
5873
5874   attr.dw_attr = attr_kind;
5875   attr.dw_attr_val.val_class = dw_val_class_long_long;
5876   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5877   attr.dw_attr_val.v.val_long_long.low = val_low;
5878   add_dwarf_attr (die, &attr);
5879 }
5880
5881 /* Add a floating point attribute value to a DIE and return it.  */
5882
5883 static inline void
5884 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5885             unsigned int length, unsigned int elt_size, unsigned char *array)
5886 {
5887   dw_attr_node attr;
5888
5889   attr.dw_attr = attr_kind;
5890   attr.dw_attr_val.val_class = dw_val_class_vec;
5891   attr.dw_attr_val.v.val_vec.length = length;
5892   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5893   attr.dw_attr_val.v.val_vec.array = array;
5894   add_dwarf_attr (die, &attr);
5895 }
5896
5897 /* Hash and equality functions for debug_str_hash.  */
5898
5899 static hashval_t
5900 debug_str_do_hash (const void *x)
5901 {
5902   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5903 }
5904
5905 static int
5906 debug_str_eq (const void *x1, const void *x2)
5907 {
5908   return strcmp ((((const struct indirect_string_node *)x1)->str),
5909                  (const char *)x2) == 0;
5910 }
5911
5912 /* Add a string attribute value to a DIE.  */
5913
5914 static inline void
5915 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5916 {
5917   dw_attr_node attr;
5918   struct indirect_string_node *node;
5919   void **slot;
5920
5921   if (! debug_str_hash)
5922     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5923                                       debug_str_eq, NULL);
5924
5925   slot = htab_find_slot_with_hash (debug_str_hash, str,
5926                                    htab_hash_string (str), INSERT);
5927   if (*slot == NULL)
5928     {
5929       node = (struct indirect_string_node *)
5930                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5931       node->str = ggc_strdup (str);
5932       *slot = node;
5933     }
5934   else
5935     node = (struct indirect_string_node *) *slot;
5936
5937   node->refcount++;
5938
5939   attr.dw_attr = attr_kind;
5940   attr.dw_attr_val.val_class = dw_val_class_str;
5941   attr.dw_attr_val.v.val_str = node;
5942   add_dwarf_attr (die, &attr);
5943 }
5944
5945 static inline const char *
5946 AT_string (dw_attr_ref a)
5947 {
5948   gcc_assert (a && AT_class (a) == dw_val_class_str);
5949   return a->dw_attr_val.v.val_str->str;
5950 }
5951
5952 /* Find out whether a string should be output inline in DIE
5953    or out-of-line in .debug_str section.  */
5954
5955 static int
5956 AT_string_form (dw_attr_ref a)
5957 {
5958   struct indirect_string_node *node;
5959   unsigned int len;
5960   char label[32];
5961
5962   gcc_assert (a && AT_class (a) == dw_val_class_str);
5963
5964   node = a->dw_attr_val.v.val_str;
5965   if (node->form)
5966     return node->form;
5967
5968   len = strlen (node->str) + 1;
5969
5970   /* If the string is shorter or equal to the size of the reference, it is
5971      always better to put it inline.  */
5972   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5973     return node->form = DW_FORM_string;
5974
5975   /* If we cannot expect the linker to merge strings in .debug_str
5976      section, only put it into .debug_str if it is worth even in this
5977      single module.  */
5978   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5979       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5980     return node->form = DW_FORM_string;
5981
5982   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5983   ++dw2_string_counter;
5984   node->label = xstrdup (label);
5985
5986   return node->form = DW_FORM_strp;
5987 }
5988
5989 /* Add a DIE reference attribute value to a DIE.  */
5990
5991 static inline void
5992 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5993 {
5994   dw_attr_node attr;
5995
5996   attr.dw_attr = attr_kind;
5997   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5998   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5999   attr.dw_attr_val.v.val_die_ref.external = 0;
6000   add_dwarf_attr (die, &attr);
6001 }
6002
6003 /* Add an AT_specification attribute to a DIE, and also make the back
6004    pointer from the specification to the definition.  */
6005
6006 static inline void
6007 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6008 {
6009   add_AT_die_ref (die, DW_AT_specification, targ_die);
6010   gcc_assert (!targ_die->die_definition);
6011   targ_die->die_definition = die;
6012 }
6013
6014 static inline dw_die_ref
6015 AT_ref (dw_attr_ref a)
6016 {
6017   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6018   return a->dw_attr_val.v.val_die_ref.die;
6019 }
6020
6021 static inline int
6022 AT_ref_external (dw_attr_ref a)
6023 {
6024   if (a && AT_class (a) == dw_val_class_die_ref)
6025     return a->dw_attr_val.v.val_die_ref.external;
6026
6027   return 0;
6028 }
6029
6030 static inline void
6031 set_AT_ref_external (dw_attr_ref a, int i)
6032 {
6033   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6034   a->dw_attr_val.v.val_die_ref.external = i;
6035 }
6036
6037 /* Add an FDE reference attribute value to a DIE.  */
6038
6039 static inline void
6040 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6041 {
6042   dw_attr_node attr;
6043
6044   attr.dw_attr = attr_kind;
6045   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6046   attr.dw_attr_val.v.val_fde_index = targ_fde;
6047   add_dwarf_attr (die, &attr);
6048 }
6049
6050 /* Add a location description attribute value to a DIE.  */
6051
6052 static inline void
6053 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6054 {
6055   dw_attr_node attr;
6056
6057   attr.dw_attr = attr_kind;
6058   attr.dw_attr_val.val_class = dw_val_class_loc;
6059   attr.dw_attr_val.v.val_loc = loc;
6060   add_dwarf_attr (die, &attr);
6061 }
6062
6063 static inline dw_loc_descr_ref
6064 AT_loc (dw_attr_ref a)
6065 {
6066   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6067   return a->dw_attr_val.v.val_loc;
6068 }
6069
6070 static inline void
6071 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6072 {
6073   dw_attr_node attr;
6074
6075   attr.dw_attr = attr_kind;
6076   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6077   attr.dw_attr_val.v.val_loc_list = loc_list;
6078   add_dwarf_attr (die, &attr);
6079   have_location_lists = true;
6080 }
6081
6082 static inline dw_loc_list_ref
6083 AT_loc_list (dw_attr_ref a)
6084 {
6085   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6086   return a->dw_attr_val.v.val_loc_list;
6087 }
6088
6089 /* Add an address constant attribute value to a DIE.  */
6090
6091 static inline void
6092 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6093 {
6094   dw_attr_node attr;
6095
6096   attr.dw_attr = attr_kind;
6097   attr.dw_attr_val.val_class = dw_val_class_addr;
6098   attr.dw_attr_val.v.val_addr = addr;
6099   add_dwarf_attr (die, &attr);
6100 }
6101
6102 /* Get the RTX from to an address DIE attribute.  */
6103
6104 static inline rtx
6105 AT_addr (dw_attr_ref a)
6106 {
6107   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6108   return a->dw_attr_val.v.val_addr;
6109 }
6110
6111 /* Add a file attribute value to a DIE.  */
6112
6113 static inline void
6114 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6115              struct dwarf_file_data *fd)
6116 {
6117   dw_attr_node attr;
6118
6119   attr.dw_attr = attr_kind;
6120   attr.dw_attr_val.val_class = dw_val_class_file;
6121   attr.dw_attr_val.v.val_file = fd;
6122   add_dwarf_attr (die, &attr);
6123 }
6124
6125 /* Get the dwarf_file_data from a file DIE attribute.  */
6126
6127 static inline struct dwarf_file_data *
6128 AT_file (dw_attr_ref a)
6129 {
6130   gcc_assert (a && AT_class (a) == dw_val_class_file);
6131   return a->dw_attr_val.v.val_file;
6132 }
6133
6134 /* Add a label identifier attribute value to a DIE.  */
6135
6136 static inline void
6137 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6138 {
6139   dw_attr_node attr;
6140
6141   attr.dw_attr = attr_kind;
6142   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6143   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6144   add_dwarf_attr (die, &attr);
6145 }
6146
6147 /* Add a section offset attribute value to a DIE, an offset into the
6148    debug_line section.  */
6149
6150 static inline void
6151 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6152                 const char *label)
6153 {
6154   dw_attr_node attr;
6155
6156   attr.dw_attr = attr_kind;
6157   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6158   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6159   add_dwarf_attr (die, &attr);
6160 }
6161
6162 /* Add a section offset attribute value to a DIE, an offset into the
6163    debug_macinfo section.  */
6164
6165 static inline void
6166 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6167                const char *label)
6168 {
6169   dw_attr_node attr;
6170
6171   attr.dw_attr = attr_kind;
6172   attr.dw_attr_val.val_class = dw_val_class_macptr;
6173   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6174   add_dwarf_attr (die, &attr);
6175 }
6176
6177 /* Add an offset attribute value to a DIE.  */
6178
6179 static inline void
6180 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6181                unsigned HOST_WIDE_INT offset)
6182 {
6183   dw_attr_node attr;
6184
6185   attr.dw_attr = attr_kind;
6186   attr.dw_attr_val.val_class = dw_val_class_offset;
6187   attr.dw_attr_val.v.val_offset = offset;
6188   add_dwarf_attr (die, &attr);
6189 }
6190
6191 /* Add an range_list attribute value to a DIE.  */
6192
6193 static void
6194 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6195                    long unsigned int offset)
6196 {
6197   dw_attr_node attr;
6198
6199   attr.dw_attr = attr_kind;
6200   attr.dw_attr_val.val_class = dw_val_class_range_list;
6201   attr.dw_attr_val.v.val_offset = offset;
6202   add_dwarf_attr (die, &attr);
6203 }
6204
6205 static inline const char *
6206 AT_lbl (dw_attr_ref a)
6207 {
6208   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6209                     || AT_class (a) == dw_val_class_lineptr
6210                     || AT_class (a) == dw_val_class_macptr));
6211   return a->dw_attr_val.v.val_lbl_id;
6212 }
6213
6214 /* Get the attribute of type attr_kind.  */
6215
6216 static dw_attr_ref
6217 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6218 {
6219   dw_attr_ref a;
6220   unsigned ix;
6221   dw_die_ref spec = NULL;
6222
6223   if (! die)
6224     return NULL;
6225
6226   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6227     if (a->dw_attr == attr_kind)
6228       return a;
6229     else if (a->dw_attr == DW_AT_specification
6230              || a->dw_attr == DW_AT_abstract_origin)
6231       spec = AT_ref (a);
6232
6233   if (spec)
6234     return get_AT (spec, attr_kind);
6235
6236   return NULL;
6237 }
6238
6239 /* Return the "low pc" attribute value, typically associated with a subprogram
6240    DIE.  Return null if the "low pc" attribute is either not present, or if it
6241    cannot be represented as an assembler label identifier.  */
6242
6243 static inline const char *
6244 get_AT_low_pc (dw_die_ref die)
6245 {
6246   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6247
6248   return a ? AT_lbl (a) : NULL;
6249 }
6250
6251 /* Return the "high pc" attribute value, typically associated with a subprogram
6252    DIE.  Return null if the "high pc" attribute is either not present, or if it
6253    cannot be represented as an assembler label identifier.  */
6254
6255 static inline const char *
6256 get_AT_hi_pc (dw_die_ref die)
6257 {
6258   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6259
6260   return a ? AT_lbl (a) : NULL;
6261 }
6262
6263 /* Return the value of the string attribute designated by ATTR_KIND, or
6264    NULL if it is not present.  */
6265
6266 static inline const char *
6267 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6268 {
6269   dw_attr_ref a = get_AT (die, attr_kind);
6270
6271   return a ? AT_string (a) : NULL;
6272 }
6273
6274 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6275    if it is not present.  */
6276
6277 static inline int
6278 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6279 {
6280   dw_attr_ref a = get_AT (die, attr_kind);
6281
6282   return a ? AT_flag (a) : 0;
6283 }
6284
6285 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6286    if it is not present.  */
6287
6288 static inline unsigned
6289 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6290 {
6291   dw_attr_ref a = get_AT (die, attr_kind);
6292
6293   return a ? AT_unsigned (a) : 0;
6294 }
6295
6296 static inline dw_die_ref
6297 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6298 {
6299   dw_attr_ref a = get_AT (die, attr_kind);
6300
6301   return a ? AT_ref (a) : NULL;
6302 }
6303
6304 static inline struct dwarf_file_data *
6305 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6306 {
6307   dw_attr_ref a = get_AT (die, attr_kind);
6308
6309   return a ? AT_file (a) : NULL;
6310 }
6311
6312 /* Return TRUE if the language is C or C++.  */
6313
6314 static inline bool
6315 is_c_family (void)
6316 {
6317   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6318
6319   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6320           || lang == DW_LANG_C99
6321           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6322 }
6323
6324 /* Return TRUE if the language is C++.  */
6325
6326 static inline bool
6327 is_cxx (void)
6328 {
6329   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6330
6331   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6332 }
6333
6334 /* Return TRUE if the language is Fortran.  */
6335
6336 static inline bool
6337 is_fortran (void)
6338 {
6339   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6340
6341   return (lang == DW_LANG_Fortran77
6342           || lang == DW_LANG_Fortran90
6343           || lang == DW_LANG_Fortran95);
6344 }
6345
6346 /* Return TRUE if the language is Java.  */
6347
6348 static inline bool
6349 is_java (void)
6350 {
6351   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6352
6353   return lang == DW_LANG_Java;
6354 }
6355
6356 /* Return TRUE if the language is Ada.  */
6357
6358 static inline bool
6359 is_ada (void)
6360 {
6361   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6362
6363   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6364 }
6365
6366 /* Remove the specified attribute if present.  */
6367
6368 static void
6369 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6370 {
6371   dw_attr_ref a;
6372   unsigned ix;
6373
6374   if (! die)
6375     return;
6376
6377   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6378     if (a->dw_attr == attr_kind)
6379       {
6380         if (AT_class (a) == dw_val_class_str)
6381           if (a->dw_attr_val.v.val_str->refcount)
6382             a->dw_attr_val.v.val_str->refcount--;
6383
6384         /* VEC_ordered_remove should help reduce the number of abbrevs
6385            that are needed.  */
6386         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6387         return;
6388       }
6389 }
6390
6391 /* Remove CHILD from its parent.  PREV must have the property that
6392    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6393
6394 static void
6395 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6396 {
6397   gcc_assert (child->die_parent == prev->die_parent);
6398   gcc_assert (prev->die_sib == child);
6399   if (prev == child)
6400     {
6401       gcc_assert (child->die_parent->die_child == child);
6402       prev = NULL;
6403     }
6404   else
6405     prev->die_sib = child->die_sib;
6406   if (child->die_parent->die_child == child)
6407     child->die_parent->die_child = prev;
6408 }
6409
6410 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6411    matches TAG.  */
6412
6413 static void
6414 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6415 {
6416   dw_die_ref c;
6417
6418   c = die->die_child;
6419   if (c) do {
6420     dw_die_ref prev = c;
6421     c = c->die_sib;
6422     while (c->die_tag == tag)
6423       {
6424         remove_child_with_prev (c, prev);
6425         /* Might have removed every child.  */
6426         if (c == c->die_sib)
6427           return;
6428         c = c->die_sib;
6429       }
6430   } while (c != die->die_child);
6431 }
6432
6433 /* Add a CHILD_DIE as the last child of DIE.  */
6434
6435 static void
6436 add_child_die (dw_die_ref die, dw_die_ref child_die)
6437 {
6438   /* FIXME this should probably be an assert.  */
6439   if (! die || ! child_die)
6440     return;
6441   gcc_assert (die != child_die);
6442
6443   child_die->die_parent = die;
6444   if (die->die_child)
6445     {
6446       child_die->die_sib = die->die_child->die_sib;
6447       die->die_child->die_sib = child_die;
6448     }
6449   else
6450     child_die->die_sib = child_die;
6451   die->die_child = child_die;
6452 }
6453
6454 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6455    is the specification, to the end of PARENT's list of children.
6456    This is done by removing and re-adding it.  */
6457
6458 static void
6459 splice_child_die (dw_die_ref parent, dw_die_ref child)
6460 {
6461   dw_die_ref p;
6462
6463   /* We want the declaration DIE from inside the class, not the
6464      specification DIE at toplevel.  */
6465   if (child->die_parent != parent)
6466     {
6467       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6468
6469       if (tmp)
6470         child = tmp;
6471     }
6472
6473   gcc_assert (child->die_parent == parent
6474               || (child->die_parent
6475                   == get_AT_ref (parent, DW_AT_specification)));
6476
6477   for (p = child->die_parent->die_child; ; p = p->die_sib)
6478     if (p->die_sib == child)
6479       {
6480         remove_child_with_prev (child, p);
6481         break;
6482       }
6483
6484   add_child_die (parent, child);
6485 }
6486
6487 /* Return a pointer to a newly created DIE node.  */
6488
6489 static inline dw_die_ref
6490 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6491 {
6492   dw_die_ref die = GGC_CNEW (die_node);
6493
6494   die->die_tag = tag_value;
6495
6496   if (parent_die != NULL)
6497     add_child_die (parent_die, die);
6498   else
6499     {
6500       limbo_die_node *limbo_node;
6501
6502       limbo_node = GGC_CNEW (limbo_die_node);
6503       limbo_node->die = die;
6504       limbo_node->created_for = t;
6505       limbo_node->next = limbo_die_list;
6506       limbo_die_list = limbo_node;
6507     }
6508
6509   return die;
6510 }
6511
6512 /* Return the DIE associated with the given type specifier.  */
6513
6514 static inline dw_die_ref
6515 lookup_type_die (tree type)
6516 {
6517   return TYPE_SYMTAB_DIE (type);
6518 }
6519
6520 /* Equate a DIE to a given type specifier.  */
6521
6522 static inline void
6523 equate_type_number_to_die (tree type, dw_die_ref type_die)
6524 {
6525   TYPE_SYMTAB_DIE (type) = type_die;
6526 }
6527
6528 /* Returns a hash value for X (which really is a die_struct).  */
6529
6530 static hashval_t
6531 decl_die_table_hash (const void *x)
6532 {
6533   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6534 }
6535
6536 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6537
6538 static int
6539 decl_die_table_eq (const void *x, const void *y)
6540 {
6541   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6542 }
6543
6544 /* Return the DIE associated with a given declaration.  */
6545
6546 static inline dw_die_ref
6547 lookup_decl_die (tree decl)
6548 {
6549   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6550 }
6551
6552 /* Returns a hash value for X (which really is a var_loc_list).  */
6553
6554 static hashval_t
6555 decl_loc_table_hash (const void *x)
6556 {
6557   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6558 }
6559
6560 /* Return nonzero if decl_id of var_loc_list X is the same as
6561    UID of decl *Y.  */
6562
6563 static int
6564 decl_loc_table_eq (const void *x, const void *y)
6565 {
6566   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6567 }
6568
6569 /* Return the var_loc list associated with a given declaration.  */
6570
6571 static inline var_loc_list *
6572 lookup_decl_loc (const_tree decl)
6573 {
6574   return (var_loc_list *)
6575     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6576 }
6577
6578 /* Equate a DIE to a particular declaration.  */
6579
6580 static void
6581 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6582 {
6583   unsigned int decl_id = DECL_UID (decl);
6584   void **slot;
6585
6586   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6587   *slot = decl_die;
6588   decl_die->decl_id = decl_id;
6589 }
6590
6591 /* Add a variable location node to the linked list for DECL.  */
6592
6593 static void
6594 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6595 {
6596   unsigned int decl_id = DECL_UID (decl);
6597   var_loc_list *temp;
6598   void **slot;
6599
6600   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6601   if (*slot == NULL)
6602     {
6603       temp = GGC_CNEW (var_loc_list);
6604       temp->decl_id = decl_id;
6605       *slot = temp;
6606     }
6607   else
6608     temp = (var_loc_list *) *slot;
6609
6610   if (temp->last)
6611     {
6612       /* If the current location is the same as the end of the list,
6613          and either both or neither of the locations is uninitialized,
6614          we have nothing to do.  */
6615       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6616                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6617           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6618                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6619               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6620                    == VAR_INIT_STATUS_UNINITIALIZED)
6621                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6622                       == VAR_INIT_STATUS_UNINITIALIZED))))
6623         {
6624           /* Add LOC to the end of list and update LAST.  */
6625           temp->last->next = loc;
6626           temp->last = loc;
6627         }
6628     }
6629   /* Do not add empty location to the beginning of the list.  */
6630   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6631     {
6632       temp->first = loc;
6633       temp->last = loc;
6634     }
6635 }
6636 \f
6637 /* Keep track of the number of spaces used to indent the
6638    output of the debugging routines that print the structure of
6639    the DIE internal representation.  */
6640 static int print_indent;
6641
6642 /* Indent the line the number of spaces given by print_indent.  */
6643
6644 static inline void
6645 print_spaces (FILE *outfile)
6646 {
6647   fprintf (outfile, "%*s", print_indent, "");
6648 }
6649
6650 /* Print the information associated with a given DIE, and its children.
6651    This routine is a debugging aid only.  */
6652
6653 static void
6654 print_die (dw_die_ref die, FILE *outfile)
6655 {
6656   dw_attr_ref a;
6657   dw_die_ref c;
6658   unsigned ix;
6659
6660   print_spaces (outfile);
6661   fprintf (outfile, "DIE %4ld: %s\n",
6662            die->die_offset, dwarf_tag_name (die->die_tag));
6663   print_spaces (outfile);
6664   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6665   fprintf (outfile, " offset: %ld\n", die->die_offset);
6666
6667   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6668     {
6669       print_spaces (outfile);
6670       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6671
6672       switch (AT_class (a))
6673         {
6674         case dw_val_class_addr:
6675           fprintf (outfile, "address");
6676           break;
6677         case dw_val_class_offset:
6678           fprintf (outfile, "offset");
6679           break;
6680         case dw_val_class_loc:
6681           fprintf (outfile, "location descriptor");
6682           break;
6683         case dw_val_class_loc_list:
6684           fprintf (outfile, "location list -> label:%s",
6685                    AT_loc_list (a)->ll_symbol);
6686           break;
6687         case dw_val_class_range_list:
6688           fprintf (outfile, "range list");
6689           break;
6690         case dw_val_class_const:
6691           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6692           break;
6693         case dw_val_class_unsigned_const:
6694           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6695           break;
6696         case dw_val_class_long_long:
6697           fprintf (outfile, "constant (%lu,%lu)",
6698                    a->dw_attr_val.v.val_long_long.hi,
6699                    a->dw_attr_val.v.val_long_long.low);
6700           break;
6701         case dw_val_class_vec:
6702           fprintf (outfile, "floating-point or vector constant");
6703           break;
6704         case dw_val_class_flag:
6705           fprintf (outfile, "%u", AT_flag (a));
6706           break;
6707         case dw_val_class_die_ref:
6708           if (AT_ref (a) != NULL)
6709             {
6710               if (AT_ref (a)->die_symbol)
6711                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6712               else
6713                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6714             }
6715           else
6716             fprintf (outfile, "die -> <null>");
6717           break;
6718         case dw_val_class_lbl_id:
6719         case dw_val_class_lineptr:
6720         case dw_val_class_macptr:
6721           fprintf (outfile, "label: %s", AT_lbl (a));
6722           break;
6723         case dw_val_class_str:
6724           if (AT_string (a) != NULL)
6725             fprintf (outfile, "\"%s\"", AT_string (a));
6726           else
6727             fprintf (outfile, "<null>");
6728           break;
6729         case dw_val_class_file:
6730           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6731                    AT_file (a)->emitted_number);
6732           break;
6733         default:
6734           break;
6735         }
6736
6737       fprintf (outfile, "\n");
6738     }
6739
6740   if (die->die_child != NULL)
6741     {
6742       print_indent += 4;
6743       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6744       print_indent -= 4;
6745     }
6746   if (print_indent == 0)
6747     fprintf (outfile, "\n");
6748 }
6749
6750 /* Print the contents of the source code line number correspondence table.
6751    This routine is a debugging aid only.  */
6752
6753 static void
6754 print_dwarf_line_table (FILE *outfile)
6755 {
6756   unsigned i;
6757   dw_line_info_ref line_info;
6758
6759   fprintf (outfile, "\n\nDWARF source line information\n");
6760   for (i = 1; i < line_info_table_in_use; i++)
6761     {
6762       line_info = &line_info_table[i];
6763       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6764                line_info->dw_file_num,
6765                line_info->dw_line_num);
6766     }
6767
6768   fprintf (outfile, "\n\n");
6769 }
6770
6771 /* Print the information collected for a given DIE.  */
6772
6773 void
6774 debug_dwarf_die (dw_die_ref die)
6775 {
6776   print_die (die, stderr);
6777 }
6778
6779 /* Print all DWARF information collected for the compilation unit.
6780    This routine is a debugging aid only.  */
6781
6782 void
6783 debug_dwarf (void)
6784 {
6785   print_indent = 0;
6786   print_die (comp_unit_die, stderr);
6787   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6788     print_dwarf_line_table (stderr);
6789 }
6790 \f
6791 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6792    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6793    DIE that marks the start of the DIEs for this include file.  */
6794
6795 static dw_die_ref
6796 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6797 {
6798   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6799   dw_die_ref new_unit = gen_compile_unit_die (filename);
6800
6801   new_unit->die_sib = old_unit;
6802   return new_unit;
6803 }
6804
6805 /* Close an include-file CU and reopen the enclosing one.  */
6806
6807 static dw_die_ref
6808 pop_compile_unit (dw_die_ref old_unit)
6809 {
6810   dw_die_ref new_unit = old_unit->die_sib;
6811
6812   old_unit->die_sib = NULL;
6813   return new_unit;
6814 }
6815
6816 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6817 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6818
6819 /* Calculate the checksum of a location expression.  */
6820
6821 static inline void
6822 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6823 {
6824   CHECKSUM (loc->dw_loc_opc);
6825   CHECKSUM (loc->dw_loc_oprnd1);
6826   CHECKSUM (loc->dw_loc_oprnd2);
6827 }
6828
6829 /* Calculate the checksum of an attribute.  */
6830
6831 static void
6832 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6833 {
6834   dw_loc_descr_ref loc;
6835   rtx r;
6836
6837   CHECKSUM (at->dw_attr);
6838
6839   /* We don't care that this was compiled with a different compiler
6840      snapshot; if the output is the same, that's what matters.  */
6841   if (at->dw_attr == DW_AT_producer)
6842     return;
6843
6844   switch (AT_class (at))
6845     {
6846     case dw_val_class_const:
6847       CHECKSUM (at->dw_attr_val.v.val_int);
6848       break;
6849     case dw_val_class_unsigned_const:
6850       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6851       break;
6852     case dw_val_class_long_long:
6853       CHECKSUM (at->dw_attr_val.v.val_long_long);
6854       break;
6855     case dw_val_class_vec:
6856       CHECKSUM (at->dw_attr_val.v.val_vec);
6857       break;
6858     case dw_val_class_flag:
6859       CHECKSUM (at->dw_attr_val.v.val_flag);
6860       break;
6861     case dw_val_class_str:
6862       CHECKSUM_STRING (AT_string (at));
6863       break;
6864
6865     case dw_val_class_addr:
6866       r = AT_addr (at);
6867       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6868       CHECKSUM_STRING (XSTR (r, 0));
6869       break;
6870
6871     case dw_val_class_offset:
6872       CHECKSUM (at->dw_attr_val.v.val_offset);
6873       break;
6874
6875     case dw_val_class_loc:
6876       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6877         loc_checksum (loc, ctx);
6878       break;
6879
6880     case dw_val_class_die_ref:
6881       die_checksum (AT_ref (at), ctx, mark);
6882       break;
6883
6884     case dw_val_class_fde_ref:
6885     case dw_val_class_lbl_id:
6886     case dw_val_class_lineptr:
6887     case dw_val_class_macptr:
6888       break;
6889
6890     case dw_val_class_file:
6891       CHECKSUM_STRING (AT_file (at)->filename);
6892       break;
6893
6894     default:
6895       break;
6896     }
6897 }
6898
6899 /* Calculate the checksum of a DIE.  */
6900
6901 static void
6902 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6903 {
6904   dw_die_ref c;
6905   dw_attr_ref a;
6906   unsigned ix;
6907
6908   /* To avoid infinite recursion.  */
6909   if (die->die_mark)
6910     {
6911       CHECKSUM (die->die_mark);
6912       return;
6913     }
6914   die->die_mark = ++(*mark);
6915
6916   CHECKSUM (die->die_tag);
6917
6918   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6919     attr_checksum (a, ctx, mark);
6920
6921   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6922 }
6923
6924 #undef CHECKSUM
6925 #undef CHECKSUM_STRING
6926
6927 /* Do the location expressions look same?  */
6928 static inline int
6929 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6930 {
6931   return loc1->dw_loc_opc == loc2->dw_loc_opc
6932          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6933          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6934 }
6935
6936 /* Do the values look the same?  */
6937 static int
6938 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6939 {
6940   dw_loc_descr_ref loc1, loc2;
6941   rtx r1, r2;
6942
6943   if (v1->val_class != v2->val_class)
6944     return 0;
6945
6946   switch (v1->val_class)
6947     {
6948     case dw_val_class_const:
6949       return v1->v.val_int == v2->v.val_int;
6950     case dw_val_class_unsigned_const:
6951       return v1->v.val_unsigned == v2->v.val_unsigned;
6952     case dw_val_class_long_long:
6953       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6954              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6955     case dw_val_class_vec:
6956       if (v1->v.val_vec.length != v2->v.val_vec.length
6957           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6958         return 0;
6959       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6960                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6961         return 0;
6962       return 1;
6963     case dw_val_class_flag:
6964       return v1->v.val_flag == v2->v.val_flag;
6965     case dw_val_class_str:
6966       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6967
6968     case dw_val_class_addr:
6969       r1 = v1->v.val_addr;
6970       r2 = v2->v.val_addr;
6971       if (GET_CODE (r1) != GET_CODE (r2))
6972         return 0;
6973       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6974       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6975
6976     case dw_val_class_offset:
6977       return v1->v.val_offset == v2->v.val_offset;
6978
6979     case dw_val_class_loc:
6980       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6981            loc1 && loc2;
6982            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6983         if (!same_loc_p (loc1, loc2, mark))
6984           return 0;
6985       return !loc1 && !loc2;
6986
6987     case dw_val_class_die_ref:
6988       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6989
6990     case dw_val_class_fde_ref:
6991     case dw_val_class_lbl_id:
6992     case dw_val_class_lineptr:
6993     case dw_val_class_macptr:
6994       return 1;
6995
6996     case dw_val_class_file:
6997       return v1->v.val_file == v2->v.val_file;
6998
6999     default:
7000       return 1;
7001     }
7002 }
7003
7004 /* Do the attributes look the same?  */
7005
7006 static int
7007 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7008 {
7009   if (at1->dw_attr != at2->dw_attr)
7010     return 0;
7011
7012   /* We don't care that this was compiled with a different compiler
7013      snapshot; if the output is the same, that's what matters. */
7014   if (at1->dw_attr == DW_AT_producer)
7015     return 1;
7016
7017   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7018 }
7019
7020 /* Do the dies look the same?  */
7021
7022 static int
7023 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7024 {
7025   dw_die_ref c1, c2;
7026   dw_attr_ref a1;
7027   unsigned ix;
7028
7029   /* To avoid infinite recursion.  */
7030   if (die1->die_mark)
7031     return die1->die_mark == die2->die_mark;
7032   die1->die_mark = die2->die_mark = ++(*mark);
7033
7034   if (die1->die_tag != die2->die_tag)
7035     return 0;
7036
7037   if (VEC_length (dw_attr_node, die1->die_attr)
7038       != VEC_length (dw_attr_node, die2->die_attr))
7039     return 0;
7040
7041   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7042     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7043       return 0;
7044
7045   c1 = die1->die_child;
7046   c2 = die2->die_child;
7047   if (! c1)
7048     {
7049       if (c2)
7050         return 0;
7051     }
7052   else
7053     for (;;)
7054       {
7055         if (!same_die_p (c1, c2, mark))
7056           return 0;
7057         c1 = c1->die_sib;
7058         c2 = c2->die_sib;
7059         if (c1 == die1->die_child)
7060           {
7061             if (c2 == die2->die_child)
7062               break;
7063             else
7064               return 0;
7065           }
7066     }
7067
7068   return 1;
7069 }
7070
7071 /* Do the dies look the same?  Wrapper around same_die_p.  */
7072
7073 static int
7074 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7075 {
7076   int mark = 0;
7077   int ret = same_die_p (die1, die2, &mark);
7078
7079   unmark_all_dies (die1);
7080   unmark_all_dies (die2);
7081
7082   return ret;
7083 }
7084
7085 /* The prefix to attach to symbols on DIEs in the current comdat debug
7086    info section.  */
7087 static char *comdat_symbol_id;
7088
7089 /* The index of the current symbol within the current comdat CU.  */
7090 static unsigned int comdat_symbol_number;
7091
7092 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7093    children, and set comdat_symbol_id accordingly.  */
7094
7095 static void
7096 compute_section_prefix (dw_die_ref unit_die)
7097 {
7098   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7099   const char *base = die_name ? lbasename (die_name) : "anonymous";
7100   char *name = XALLOCAVEC (char, strlen (base) + 64);
7101   char *p;
7102   int i, mark;
7103   unsigned char checksum[16];
7104   struct md5_ctx ctx;
7105
7106   /* Compute the checksum of the DIE, then append part of it as hex digits to
7107      the name filename of the unit.  */
7108
7109   md5_init_ctx (&ctx);
7110   mark = 0;
7111   die_checksum (unit_die, &ctx, &mark);
7112   unmark_all_dies (unit_die);
7113   md5_finish_ctx (&ctx, checksum);
7114
7115   sprintf (name, "%s.", base);
7116   clean_symbol_name (name);
7117
7118   p = name + strlen (name);
7119   for (i = 0; i < 4; i++)
7120     {
7121       sprintf (p, "%.2x", checksum[i]);
7122       p += 2;
7123     }
7124
7125   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7126   comdat_symbol_number = 0;
7127 }
7128
7129 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7130
7131 static int
7132 is_type_die (dw_die_ref die)
7133 {
7134   switch (die->die_tag)
7135     {
7136     case DW_TAG_array_type:
7137     case DW_TAG_class_type:
7138     case DW_TAG_interface_type:
7139     case DW_TAG_enumeration_type:
7140     case DW_TAG_pointer_type:
7141     case DW_TAG_reference_type:
7142     case DW_TAG_string_type:
7143     case DW_TAG_structure_type:
7144     case DW_TAG_subroutine_type:
7145     case DW_TAG_union_type:
7146     case DW_TAG_ptr_to_member_type:
7147     case DW_TAG_set_type:
7148     case DW_TAG_subrange_type:
7149     case DW_TAG_base_type:
7150     case DW_TAG_const_type:
7151     case DW_TAG_file_type:
7152     case DW_TAG_packed_type:
7153     case DW_TAG_volatile_type:
7154     case DW_TAG_typedef:
7155       return 1;
7156     default:
7157       return 0;
7158     }
7159 }
7160
7161 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7162    Basically, we want to choose the bits that are likely to be shared between
7163    compilations (types) and leave out the bits that are specific to individual
7164    compilations (functions).  */
7165
7166 static int
7167 is_comdat_die (dw_die_ref c)
7168 {
7169   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7170      we do for stabs.  The advantage is a greater likelihood of sharing between
7171      objects that don't include headers in the same order (and therefore would
7172      put the base types in a different comdat).  jason 8/28/00 */
7173
7174   if (c->die_tag == DW_TAG_base_type)
7175     return 0;
7176
7177   if (c->die_tag == DW_TAG_pointer_type
7178       || c->die_tag == DW_TAG_reference_type
7179       || c->die_tag == DW_TAG_const_type
7180       || c->die_tag == DW_TAG_volatile_type)
7181     {
7182       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7183
7184       return t ? is_comdat_die (t) : 0;
7185     }
7186
7187   return is_type_die (c);
7188 }
7189
7190 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7191    compilation unit.  */
7192
7193 static int
7194 is_symbol_die (dw_die_ref c)
7195 {
7196   return (is_type_die (c)
7197           || (get_AT (c, DW_AT_declaration)
7198               && !get_AT (c, DW_AT_specification))
7199           || c->die_tag == DW_TAG_namespace);
7200 }
7201
7202 static char *
7203 gen_internal_sym (const char *prefix)
7204 {
7205   char buf[256];
7206
7207   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7208   return xstrdup (buf);
7209 }
7210
7211 /* Assign symbols to all worthy DIEs under DIE.  */
7212
7213 static void
7214 assign_symbol_names (dw_die_ref die)
7215 {
7216   dw_die_ref c;
7217
7218   if (is_symbol_die (die))
7219     {
7220       if (comdat_symbol_id)
7221         {
7222           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7223
7224           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7225                    comdat_symbol_id, comdat_symbol_number++);
7226           die->die_symbol = xstrdup (p);
7227         }
7228       else
7229         die->die_symbol = gen_internal_sym ("LDIE");
7230     }
7231
7232   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7233 }
7234
7235 struct cu_hash_table_entry
7236 {
7237   dw_die_ref cu;
7238   unsigned min_comdat_num, max_comdat_num;
7239   struct cu_hash_table_entry *next;
7240 };
7241
7242 /* Routines to manipulate hash table of CUs.  */
7243 static hashval_t
7244 htab_cu_hash (const void *of)
7245 {
7246   const struct cu_hash_table_entry *const entry =
7247     (const struct cu_hash_table_entry *) of;
7248
7249   return htab_hash_string (entry->cu->die_symbol);
7250 }
7251
7252 static int
7253 htab_cu_eq (const void *of1, const void *of2)
7254 {
7255   const struct cu_hash_table_entry *const entry1 =
7256     (const struct cu_hash_table_entry *) of1;
7257   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7258
7259   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7260 }
7261
7262 static void
7263 htab_cu_del (void *what)
7264 {
7265   struct cu_hash_table_entry *next,
7266     *entry = (struct cu_hash_table_entry *) what;
7267
7268   while (entry)
7269     {
7270       next = entry->next;
7271       free (entry);
7272       entry = next;
7273     }
7274 }
7275
7276 /* Check whether we have already seen this CU and set up SYM_NUM
7277    accordingly.  */
7278 static int
7279 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7280 {
7281   struct cu_hash_table_entry dummy;
7282   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7283
7284   dummy.max_comdat_num = 0;
7285
7286   slot = (struct cu_hash_table_entry **)
7287     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7288         INSERT);
7289   entry = *slot;
7290
7291   for (; entry; last = entry, entry = entry->next)
7292     {
7293       if (same_die_p_wrap (cu, entry->cu))
7294         break;
7295     }
7296
7297   if (entry)
7298     {
7299       *sym_num = entry->min_comdat_num;
7300       return 1;
7301     }
7302
7303   entry = XCNEW (struct cu_hash_table_entry);
7304   entry->cu = cu;
7305   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7306   entry->next = *slot;
7307   *slot = entry;
7308
7309   return 0;
7310 }
7311
7312 /* Record SYM_NUM to record of CU in HTABLE.  */
7313 static void
7314 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7315 {
7316   struct cu_hash_table_entry **slot, *entry;
7317
7318   slot = (struct cu_hash_table_entry **)
7319     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7320         NO_INSERT);
7321   entry = *slot;
7322
7323   entry->max_comdat_num = sym_num;
7324 }
7325
7326 /* Traverse the DIE (which is always comp_unit_die), and set up
7327    additional compilation units for each of the include files we see
7328    bracketed by BINCL/EINCL.  */
7329
7330 static void
7331 break_out_includes (dw_die_ref die)
7332 {
7333   dw_die_ref c;
7334   dw_die_ref unit = NULL;
7335   limbo_die_node *node, **pnode;
7336   htab_t cu_hash_table;
7337
7338   c = die->die_child;
7339   if (c) do {
7340     dw_die_ref prev = c;
7341     c = c->die_sib;
7342     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7343            || (unit && is_comdat_die (c)))
7344       {
7345         dw_die_ref next = c->die_sib;
7346
7347         /* This DIE is for a secondary CU; remove it from the main one.  */
7348         remove_child_with_prev (c, prev);
7349
7350         if (c->die_tag == DW_TAG_GNU_BINCL)
7351           unit = push_new_compile_unit (unit, c);
7352         else if (c->die_tag == DW_TAG_GNU_EINCL)
7353           unit = pop_compile_unit (unit);
7354         else
7355           add_child_die (unit, c);
7356         c = next;
7357         if (c == die->die_child)
7358           break;
7359       }
7360   } while (c != die->die_child);
7361
7362 #if 0
7363   /* We can only use this in debugging, since the frontend doesn't check
7364      to make sure that we leave every include file we enter.  */
7365   gcc_assert (!unit);
7366 #endif
7367
7368   assign_symbol_names (die);
7369   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7370   for (node = limbo_die_list, pnode = &limbo_die_list;
7371        node;
7372        node = node->next)
7373     {
7374       int is_dupl;
7375
7376       compute_section_prefix (node->die);
7377       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7378                         &comdat_symbol_number);
7379       assign_symbol_names (node->die);
7380       if (is_dupl)
7381         *pnode = node->next;
7382       else
7383         {
7384           pnode = &node->next;
7385           record_comdat_symbol_number (node->die, cu_hash_table,
7386                 comdat_symbol_number);
7387         }
7388     }
7389   htab_delete (cu_hash_table);
7390 }
7391
7392 /* Traverse the DIE and add a sibling attribute if it may have the
7393    effect of speeding up access to siblings.  To save some space,
7394    avoid generating sibling attributes for DIE's without children.  */
7395
7396 static void
7397 add_sibling_attributes (dw_die_ref die)
7398 {
7399   dw_die_ref c;
7400
7401   if (! die->die_child)
7402     return;
7403
7404   if (die->die_parent && die != die->die_parent->die_child)
7405     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7406
7407   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7408 }
7409
7410 /* Output all location lists for the DIE and its children.  */
7411
7412 static void
7413 output_location_lists (dw_die_ref die)
7414 {
7415   dw_die_ref c;
7416   dw_attr_ref a;
7417   unsigned ix;
7418
7419   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7420     if (AT_class (a) == dw_val_class_loc_list)
7421       output_loc_list (AT_loc_list (a));
7422
7423   FOR_EACH_CHILD (die, c, output_location_lists (c));
7424 }
7425
7426 /* The format of each DIE (and its attribute value pairs) is encoded in an
7427    abbreviation table.  This routine builds the abbreviation table and assigns
7428    a unique abbreviation id for each abbreviation entry.  The children of each
7429    die are visited recursively.  */
7430
7431 static void
7432 build_abbrev_table (dw_die_ref die)
7433 {
7434   unsigned long abbrev_id;
7435   unsigned int n_alloc;
7436   dw_die_ref c;
7437   dw_attr_ref a;
7438   unsigned ix;
7439
7440   /* Scan the DIE references, and mark as external any that refer to
7441      DIEs from other CUs (i.e. those which are not marked).  */
7442   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7443     if (AT_class (a) == dw_val_class_die_ref
7444         && AT_ref (a)->die_mark == 0)
7445       {
7446         gcc_assert (AT_ref (a)->die_symbol);
7447
7448         set_AT_ref_external (a, 1);
7449       }
7450
7451   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7452     {
7453       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7454       dw_attr_ref die_a, abbrev_a;
7455       unsigned ix;
7456       bool ok = true;
7457
7458       if (abbrev->die_tag != die->die_tag)
7459         continue;
7460       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7461         continue;
7462
7463       if (VEC_length (dw_attr_node, abbrev->die_attr)
7464           != VEC_length (dw_attr_node, die->die_attr))
7465         continue;
7466
7467       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7468         {
7469           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7470           if ((abbrev_a->dw_attr != die_a->dw_attr)
7471               || (value_format (abbrev_a) != value_format (die_a)))
7472             {
7473               ok = false;
7474               break;
7475             }
7476         }
7477       if (ok)
7478         break;
7479     }
7480
7481   if (abbrev_id >= abbrev_die_table_in_use)
7482     {
7483       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7484         {
7485           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7486           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7487                                             n_alloc);
7488
7489           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7490                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7491           abbrev_die_table_allocated = n_alloc;
7492         }
7493
7494       ++abbrev_die_table_in_use;
7495       abbrev_die_table[abbrev_id] = die;
7496     }
7497
7498   die->die_abbrev = abbrev_id;
7499   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7500 }
7501 \f
7502 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7503
7504 static int
7505 constant_size (long unsigned int value)
7506 {
7507   int log;
7508
7509   if (value == 0)
7510     log = 0;
7511   else
7512     log = floor_log2 (value);
7513
7514   log = log / 8;
7515   log = 1 << (floor_log2 (log) + 1);
7516
7517   return log;
7518 }
7519
7520 /* Return the size of a DIE as it is represented in the
7521    .debug_info section.  */
7522
7523 static unsigned long
7524 size_of_die (dw_die_ref die)
7525 {
7526   unsigned long size = 0;
7527   dw_attr_ref a;
7528   unsigned ix;
7529
7530   size += size_of_uleb128 (die->die_abbrev);
7531   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7532     {
7533       switch (AT_class (a))
7534         {
7535         case dw_val_class_addr:
7536           size += DWARF2_ADDR_SIZE;
7537           break;
7538         case dw_val_class_offset:
7539           size += DWARF_OFFSET_SIZE;
7540           break;
7541         case dw_val_class_loc:
7542           {
7543             unsigned long lsize = size_of_locs (AT_loc (a));
7544
7545             /* Block length.  */
7546             size += constant_size (lsize);
7547             size += lsize;
7548           }
7549           break;
7550         case dw_val_class_loc_list:
7551           size += DWARF_OFFSET_SIZE;
7552           break;
7553         case dw_val_class_range_list:
7554           size += DWARF_OFFSET_SIZE;
7555           break;
7556         case dw_val_class_const:
7557           size += size_of_sleb128 (AT_int (a));
7558           break;
7559         case dw_val_class_unsigned_const:
7560           size += constant_size (AT_unsigned (a));
7561           break;
7562         case dw_val_class_long_long:
7563           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7564           break;
7565         case dw_val_class_vec:
7566           size += 1 + (a->dw_attr_val.v.val_vec.length
7567                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
7568           break;
7569         case dw_val_class_flag:
7570           size += 1;
7571           break;
7572         case dw_val_class_die_ref:
7573           if (AT_ref_external (a))
7574             size += DWARF2_ADDR_SIZE;
7575           else
7576             size += DWARF_OFFSET_SIZE;
7577           break;
7578         case dw_val_class_fde_ref:
7579           size += DWARF_OFFSET_SIZE;
7580           break;
7581         case dw_val_class_lbl_id:
7582           size += DWARF2_ADDR_SIZE;
7583           break;
7584         case dw_val_class_lineptr:
7585         case dw_val_class_macptr:
7586           size += DWARF_OFFSET_SIZE;
7587           break;
7588         case dw_val_class_str:
7589           if (AT_string_form (a) == DW_FORM_strp)
7590             size += DWARF_OFFSET_SIZE;
7591           else
7592             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7593           break;
7594         case dw_val_class_file:
7595           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7596           break;
7597         default:
7598           gcc_unreachable ();
7599         }
7600     }
7601
7602   return size;
7603 }
7604
7605 /* Size the debugging information associated with a given DIE.  Visits the
7606    DIE's children recursively.  Updates the global variable next_die_offset, on
7607    each time through.  Uses the current value of next_die_offset to update the
7608    die_offset field in each DIE.  */
7609
7610 static void
7611 calc_die_sizes (dw_die_ref die)
7612 {
7613   dw_die_ref c;
7614
7615   die->die_offset = next_die_offset;
7616   next_die_offset += size_of_die (die);
7617
7618   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7619
7620   if (die->die_child != NULL)
7621     /* Count the null byte used to terminate sibling lists.  */
7622     next_die_offset += 1;
7623 }
7624
7625 /* Set the marks for a die and its children.  We do this so
7626    that we know whether or not a reference needs to use FORM_ref_addr; only
7627    DIEs in the same CU will be marked.  We used to clear out the offset
7628    and use that as the flag, but ran into ordering problems.  */
7629
7630 static void
7631 mark_dies (dw_die_ref die)
7632 {
7633   dw_die_ref c;
7634
7635   gcc_assert (!die->die_mark);
7636
7637   die->die_mark = 1;
7638   FOR_EACH_CHILD (die, c, mark_dies (c));
7639 }
7640
7641 /* Clear the marks for a die and its children.  */
7642
7643 static void
7644 unmark_dies (dw_die_ref die)
7645 {
7646   dw_die_ref c;
7647
7648   gcc_assert (die->die_mark);
7649
7650   die->die_mark = 0;
7651   FOR_EACH_CHILD (die, c, unmark_dies (c));
7652 }
7653
7654 /* Clear the marks for a die, its children and referred dies.  */
7655
7656 static void
7657 unmark_all_dies (dw_die_ref die)
7658 {
7659   dw_die_ref c;
7660   dw_attr_ref a;
7661   unsigned ix;
7662
7663   if (!die->die_mark)
7664     return;
7665   die->die_mark = 0;
7666
7667   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7668
7669   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7670     if (AT_class (a) == dw_val_class_die_ref)
7671       unmark_all_dies (AT_ref (a));
7672 }
7673
7674 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7675    generated for the compilation unit.  */
7676
7677 static unsigned long
7678 size_of_pubnames (VEC (pubname_entry, gc) * names)
7679 {
7680   unsigned long size;
7681   unsigned i;
7682   pubname_ref p;
7683
7684   size = DWARF_PUBNAMES_HEADER_SIZE;
7685   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7686     if (names != pubtype_table
7687         || p->die->die_offset != 0
7688         || !flag_eliminate_unused_debug_types)
7689       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7690
7691   size += DWARF_OFFSET_SIZE;
7692   return size;
7693 }
7694
7695 /* Return the size of the information in the .debug_aranges section.  */
7696
7697 static unsigned long
7698 size_of_aranges (void)
7699 {
7700   unsigned long size;
7701
7702   size = DWARF_ARANGES_HEADER_SIZE;
7703
7704   /* Count the address/length pair for this compilation unit.  */
7705   if (text_section_used)
7706     size += 2 * DWARF2_ADDR_SIZE;
7707   if (cold_text_section_used)
7708     size += 2 * DWARF2_ADDR_SIZE;
7709   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7710
7711   /* Count the two zero words used to terminated the address range table.  */
7712   size += 2 * DWARF2_ADDR_SIZE;
7713   return size;
7714 }
7715 \f
7716 /* Select the encoding of an attribute value.  */
7717
7718 static enum dwarf_form
7719 value_format (dw_attr_ref a)
7720 {
7721   switch (a->dw_attr_val.val_class)
7722     {
7723     case dw_val_class_addr:
7724       return DW_FORM_addr;
7725     case dw_val_class_range_list:
7726     case dw_val_class_offset:
7727     case dw_val_class_loc_list:
7728       switch (DWARF_OFFSET_SIZE)
7729         {
7730         case 4:
7731           return DW_FORM_data4;
7732         case 8:
7733           return DW_FORM_data8;
7734         default:
7735           gcc_unreachable ();
7736         }
7737     case dw_val_class_loc:
7738       switch (constant_size (size_of_locs (AT_loc (a))))
7739         {
7740         case 1:
7741           return DW_FORM_block1;
7742         case 2:
7743           return DW_FORM_block2;
7744         default:
7745           gcc_unreachable ();
7746         }
7747     case dw_val_class_const:
7748       return DW_FORM_sdata;
7749     case dw_val_class_unsigned_const:
7750       switch (constant_size (AT_unsigned (a)))
7751         {
7752         case 1:
7753           return DW_FORM_data1;
7754         case 2:
7755           return DW_FORM_data2;
7756         case 4:
7757           return DW_FORM_data4;
7758         case 8:
7759           return DW_FORM_data8;
7760         default:
7761           gcc_unreachable ();
7762         }
7763     case dw_val_class_long_long:
7764       return DW_FORM_block1;
7765     case dw_val_class_vec:
7766       return DW_FORM_block1;
7767     case dw_val_class_flag:
7768       return DW_FORM_flag;
7769     case dw_val_class_die_ref:
7770       if (AT_ref_external (a))
7771         return DW_FORM_ref_addr;
7772       else
7773         return DW_FORM_ref;
7774     case dw_val_class_fde_ref:
7775       return DW_FORM_data;
7776     case dw_val_class_lbl_id:
7777       return DW_FORM_addr;
7778     case dw_val_class_lineptr:
7779     case dw_val_class_macptr:
7780       return DW_FORM_data;
7781     case dw_val_class_str:
7782       return AT_string_form (a);
7783     case dw_val_class_file:
7784       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7785         {
7786         case 1:
7787           return DW_FORM_data1;
7788         case 2:
7789           return DW_FORM_data2;
7790         case 4:
7791           return DW_FORM_data4;
7792         default:
7793           gcc_unreachable ();
7794         }
7795
7796     default:
7797       gcc_unreachable ();
7798     }
7799 }
7800
7801 /* Output the encoding of an attribute value.  */
7802
7803 static void
7804 output_value_format (dw_attr_ref a)
7805 {
7806   enum dwarf_form form = value_format (a);
7807
7808   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7809 }
7810
7811 /* Output the .debug_abbrev section which defines the DIE abbreviation
7812    table.  */
7813
7814 static void
7815 output_abbrev_section (void)
7816 {
7817   unsigned long abbrev_id;
7818
7819   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7820     {
7821       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7822       unsigned ix;
7823       dw_attr_ref a_attr;
7824
7825       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7826       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7827                                    dwarf_tag_name (abbrev->die_tag));
7828
7829       if (abbrev->die_child != NULL)
7830         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7831       else
7832         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7833
7834       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7835            ix++)
7836         {
7837           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7838                                        dwarf_attr_name (a_attr->dw_attr));
7839           output_value_format (a_attr);
7840         }
7841
7842       dw2_asm_output_data (1, 0, NULL);
7843       dw2_asm_output_data (1, 0, NULL);
7844     }
7845
7846   /* Terminate the table.  */
7847   dw2_asm_output_data (1, 0, NULL);
7848 }
7849
7850 /* Output a symbol we can use to refer to this DIE from another CU.  */
7851
7852 static inline void
7853 output_die_symbol (dw_die_ref die)
7854 {
7855   char *sym = die->die_symbol;
7856
7857   if (sym == 0)
7858     return;
7859
7860   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7861     /* We make these global, not weak; if the target doesn't support
7862        .linkonce, it doesn't support combining the sections, so debugging
7863        will break.  */
7864     targetm.asm_out.globalize_label (asm_out_file, sym);
7865
7866   ASM_OUTPUT_LABEL (asm_out_file, sym);
7867 }
7868
7869 /* Return a new location list, given the begin and end range, and the
7870    expression. gensym tells us whether to generate a new internal symbol for
7871    this location list node, which is done for the head of the list only.  */
7872
7873 static inline dw_loc_list_ref
7874 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7875               const char *section, unsigned int gensym)
7876 {
7877   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7878
7879   retlist->begin = begin;
7880   retlist->end = end;
7881   retlist->expr = expr;
7882   retlist->section = section;
7883   if (gensym)
7884     retlist->ll_symbol = gen_internal_sym ("LLST");
7885
7886   return retlist;
7887 }
7888
7889 /* Add a location description expression to a location list.  */
7890
7891 static inline void
7892 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7893                            const char *begin, const char *end,
7894                            const char *section)
7895 {
7896   dw_loc_list_ref *d;
7897
7898   /* Find the end of the chain.  */
7899   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7900     ;
7901
7902   /* Add a new location list node to the list.  */
7903   *d = new_loc_list (descr, begin, end, section, 0);
7904 }
7905
7906 /* Output the location list given to us.  */
7907
7908 static void
7909 output_loc_list (dw_loc_list_ref list_head)
7910 {
7911   dw_loc_list_ref curr = list_head;
7912
7913   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7914
7915   /* Walk the location list, and output each range + expression.  */
7916   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7917     {
7918       unsigned long size;
7919       /* Don't output an entry that starts and ends at the same address.  */
7920       if (strcmp (curr->begin, curr->end) == 0)
7921         continue;
7922       if (!have_multiple_function_sections)
7923         {
7924           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7925                                 "Location list begin address (%s)",
7926                                 list_head->ll_symbol);
7927           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7928                                 "Location list end address (%s)",
7929                                 list_head->ll_symbol);
7930         }
7931       else
7932         {
7933           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7934                                "Location list begin address (%s)",
7935                                list_head->ll_symbol);
7936           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7937                                "Location list end address (%s)",
7938                                list_head->ll_symbol);
7939         }
7940       size = size_of_locs (curr->expr);
7941
7942       /* Output the block length for this list of location operations.  */
7943       gcc_assert (size <= 0xffff);
7944       dw2_asm_output_data (2, size, "%s", "Location expression size");
7945
7946       output_loc_sequence (curr->expr);
7947     }
7948
7949   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7950                        "Location list terminator begin (%s)",
7951                        list_head->ll_symbol);
7952   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7953                        "Location list terminator end (%s)",
7954                        list_head->ll_symbol);
7955 }
7956
7957 /* Output the DIE and its attributes.  Called recursively to generate
7958    the definitions of each child DIE.  */
7959
7960 static void
7961 output_die (dw_die_ref die)
7962 {
7963   dw_attr_ref a;
7964   dw_die_ref c;
7965   unsigned long size;
7966   unsigned ix;
7967
7968   /* If someone in another CU might refer to us, set up a symbol for
7969      them to point to.  */
7970   if (die->die_symbol)
7971     output_die_symbol (die);
7972
7973   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7974                                (unsigned long)die->die_offset,
7975                                dwarf_tag_name (die->die_tag));
7976
7977   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7978     {
7979       const char *name = dwarf_attr_name (a->dw_attr);
7980
7981       switch (AT_class (a))
7982         {
7983         case dw_val_class_addr:
7984           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7985           break;
7986
7987         case dw_val_class_offset:
7988           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7989                                "%s", name);
7990           break;
7991
7992         case dw_val_class_range_list:
7993           {
7994             char *p = strchr (ranges_section_label, '\0');
7995
7996             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7997                      a->dw_attr_val.v.val_offset);
7998             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7999                                    debug_ranges_section, "%s", name);
8000             *p = '\0';
8001           }
8002           break;
8003
8004         case dw_val_class_loc:
8005           size = size_of_locs (AT_loc (a));
8006
8007           /* Output the block length for this list of location operations.  */
8008           dw2_asm_output_data (constant_size (size), size, "%s", name);
8009
8010           output_loc_sequence (AT_loc (a));
8011           break;
8012
8013         case dw_val_class_const:
8014           /* ??? It would be slightly more efficient to use a scheme like is
8015              used for unsigned constants below, but gdb 4.x does not sign
8016              extend.  Gdb 5.x does sign extend.  */
8017           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8018           break;
8019
8020         case dw_val_class_unsigned_const:
8021           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8022                                AT_unsigned (a), "%s", name);
8023           break;
8024
8025         case dw_val_class_long_long:
8026           {
8027             unsigned HOST_WIDE_INT first, second;
8028
8029             dw2_asm_output_data (1,
8030                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8031                                  "%s", name);
8032
8033             if (WORDS_BIG_ENDIAN)
8034               {
8035                 first = a->dw_attr_val.v.val_long_long.hi;
8036                 second = a->dw_attr_val.v.val_long_long.low;
8037               }
8038             else
8039               {
8040                 first = a->dw_attr_val.v.val_long_long.low;
8041                 second = a->dw_attr_val.v.val_long_long.hi;
8042               }
8043
8044             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8045                                  first, "long long constant");
8046             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8047                                  second, NULL);
8048           }
8049           break;
8050
8051         case dw_val_class_vec:
8052           {
8053             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8054             unsigned int len = a->dw_attr_val.v.val_vec.length;
8055             unsigned int i;
8056             unsigned char *p;
8057
8058             dw2_asm_output_data (1, len * elt_size, "%s", name);
8059             if (elt_size > sizeof (HOST_WIDE_INT))
8060               {
8061                 elt_size /= 2;
8062                 len *= 2;
8063               }
8064             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8065                  i < len;
8066                  i++, p += elt_size)
8067               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8068                                    "fp or vector constant word %u", i);
8069             break;
8070           }
8071
8072         case dw_val_class_flag:
8073           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8074           break;
8075
8076         case dw_val_class_loc_list:
8077           {
8078             char *sym = AT_loc_list (a)->ll_symbol;
8079
8080             gcc_assert (sym);
8081             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8082                                    "%s", name);
8083           }
8084           break;
8085
8086         case dw_val_class_die_ref:
8087           if (AT_ref_external (a))
8088             {
8089               char *sym = AT_ref (a)->die_symbol;
8090
8091               gcc_assert (sym);
8092               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8093                                      "%s", name);
8094             }
8095           else
8096             {
8097               gcc_assert (AT_ref (a)->die_offset);
8098               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8099                                    "%s", name);
8100             }
8101           break;
8102
8103         case dw_val_class_fde_ref:
8104           {
8105             char l1[20];
8106
8107             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8108                                          a->dw_attr_val.v.val_fde_index * 2);
8109             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8110                                    "%s", name);
8111           }
8112           break;
8113
8114         case dw_val_class_lbl_id:
8115           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8116           break;
8117
8118         case dw_val_class_lineptr:
8119           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8120                                  debug_line_section, "%s", name);
8121           break;
8122
8123         case dw_val_class_macptr:
8124           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8125                                  debug_macinfo_section, "%s", name);
8126           break;
8127
8128         case dw_val_class_str:
8129           if (AT_string_form (a) == DW_FORM_strp)
8130             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8131                                    a->dw_attr_val.v.val_str->label,
8132                                    debug_str_section,
8133                                    "%s: \"%s\"", name, AT_string (a));
8134           else
8135             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8136           break;
8137
8138         case dw_val_class_file:
8139           {
8140             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8141
8142             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8143                                  a->dw_attr_val.v.val_file->filename);
8144             break;
8145           }
8146
8147         default:
8148           gcc_unreachable ();
8149         }
8150     }
8151
8152   FOR_EACH_CHILD (die, c, output_die (c));
8153
8154   /* Add null byte to terminate sibling list.  */
8155   if (die->die_child != NULL)
8156     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8157                          (unsigned long) die->die_offset);
8158 }
8159
8160 /* Output the compilation unit that appears at the beginning of the
8161    .debug_info section, and precedes the DIE descriptions.  */
8162
8163 static void
8164 output_compilation_unit_header (void)
8165 {
8166   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8167     dw2_asm_output_data (4, 0xffffffff,
8168       "Initial length escape value indicating 64-bit DWARF extension");
8169   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8170                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8171                        "Length of Compilation Unit Info");
8172   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8173   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8174                          debug_abbrev_section,
8175                          "Offset Into Abbrev. Section");
8176   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8177 }
8178
8179 /* Output the compilation unit DIE and its children.  */
8180
8181 static void
8182 output_comp_unit (dw_die_ref die, int output_if_empty)
8183 {
8184   const char *secname;
8185   char *oldsym, *tmp;
8186
8187   /* Unless we are outputting main CU, we may throw away empty ones.  */
8188   if (!output_if_empty && die->die_child == NULL)
8189     return;
8190
8191   /* Even if there are no children of this DIE, we must output the information
8192      about the compilation unit.  Otherwise, on an empty translation unit, we
8193      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8194      will then complain when examining the file.  First mark all the DIEs in
8195      this CU so we know which get local refs.  */
8196   mark_dies (die);
8197
8198   build_abbrev_table (die);
8199
8200   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8201   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8202   calc_die_sizes (die);
8203
8204   oldsym = die->die_symbol;
8205   if (oldsym)
8206     {
8207       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8208
8209       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8210       secname = tmp;
8211       die->die_symbol = NULL;
8212       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8213     }
8214   else
8215     switch_to_section (debug_info_section);
8216
8217   /* Output debugging information.  */
8218   output_compilation_unit_header ();
8219   output_die (die);
8220
8221   /* Leave the marks on the main CU, so we can check them in
8222      output_pubnames.  */
8223   if (oldsym)
8224     {
8225       unmark_dies (die);
8226       die->die_symbol = oldsym;
8227     }
8228 }
8229
8230 /* Return the DWARF2/3 pubname associated with a decl.  */
8231
8232 static const char *
8233 dwarf2_name (tree decl, int scope)
8234 {
8235   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8236 }
8237
8238 /* Add a new entry to .debug_pubnames if appropriate.  */
8239
8240 static void
8241 add_pubname_string (const char *str, dw_die_ref die)
8242 {
8243   pubname_entry e;
8244
8245   e.die = die;
8246   e.name = xstrdup (str);
8247   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8248 }
8249
8250 static void
8251 add_pubname (tree decl, dw_die_ref die)
8252 {
8253
8254   if (TREE_PUBLIC (decl))
8255     add_pubname_string (dwarf2_name (decl, 1), die);
8256 }
8257
8258 /* Add a new entry to .debug_pubtypes if appropriate.  */
8259
8260 static void
8261 add_pubtype (tree decl, dw_die_ref die)
8262 {
8263   pubname_entry e;
8264
8265   e.name = NULL;
8266   if ((TREE_PUBLIC (decl)
8267        || die->die_parent == comp_unit_die)
8268       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8269     {
8270       e.die = die;
8271       if (TYPE_P (decl))
8272         {
8273           if (TYPE_NAME (decl))
8274             {
8275               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8276                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8277               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8278                        && DECL_NAME (TYPE_NAME (decl)))
8279                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8280               else
8281                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8282             }
8283         }
8284       else
8285         e.name = xstrdup (dwarf2_name (decl, 1));
8286
8287       /* If we don't have a name for the type, there's no point in adding
8288          it to the table.  */
8289       if (e.name && e.name[0] != '\0')
8290         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8291     }
8292 }
8293
8294 /* Output the public names table used to speed up access to externally
8295    visible names; or the public types table used to find type definitions.  */
8296
8297 static void
8298 output_pubnames (VEC (pubname_entry, gc) * names)
8299 {
8300   unsigned i;
8301   unsigned long pubnames_length = size_of_pubnames (names);
8302   pubname_ref pub;
8303
8304   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8305     dw2_asm_output_data (4, 0xffffffff,
8306       "Initial length escape value indicating 64-bit DWARF extension");
8307   if (names == pubname_table)
8308     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8309                          "Length of Public Names Info");
8310   else
8311     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8312                          "Length of Public Type Names Info");
8313   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8314   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8315                          debug_info_section,
8316                          "Offset of Compilation Unit Info");
8317   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8318                        "Compilation Unit Length");
8319
8320   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8321     {
8322       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8323       if (names == pubname_table)
8324         gcc_assert (pub->die->die_mark);
8325
8326       if (names != pubtype_table
8327           || pub->die->die_offset != 0
8328           || !flag_eliminate_unused_debug_types)
8329         {
8330           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8331                                "DIE offset");
8332
8333           dw2_asm_output_nstring (pub->name, -1, "external name");
8334         }
8335     }
8336
8337   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8338 }
8339
8340 /* Add a new entry to .debug_aranges if appropriate.  */
8341
8342 static void
8343 add_arange (tree decl, dw_die_ref die)
8344 {
8345   if (! DECL_SECTION_NAME (decl))
8346     return;
8347
8348   if (arange_table_in_use == arange_table_allocated)
8349     {
8350       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8351       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8352                                     arange_table_allocated);
8353       memset (arange_table + arange_table_in_use, 0,
8354               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8355     }
8356
8357   arange_table[arange_table_in_use++] = die;
8358 }
8359
8360 /* Output the information that goes into the .debug_aranges table.
8361    Namely, define the beginning and ending address range of the
8362    text section generated for this compilation unit.  */
8363
8364 static void
8365 output_aranges (void)
8366 {
8367   unsigned i;
8368   unsigned long aranges_length = size_of_aranges ();
8369
8370   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8371     dw2_asm_output_data (4, 0xffffffff,
8372       "Initial length escape value indicating 64-bit DWARF extension");
8373   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8374                        "Length of Address Ranges Info");
8375   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8376   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8377                          debug_info_section,
8378                          "Offset of Compilation Unit Info");
8379   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8380   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8381
8382   /* We need to align to twice the pointer size here.  */
8383   if (DWARF_ARANGES_PAD_SIZE)
8384     {
8385       /* Pad using a 2 byte words so that padding is correct for any
8386          pointer size.  */
8387       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8388                            2 * DWARF2_ADDR_SIZE);
8389       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8390         dw2_asm_output_data (2, 0, NULL);
8391     }
8392
8393   /* It is necessary not to output these entries if the sections were
8394      not used; if the sections were not used, the length will be 0 and
8395      the address may end up as 0 if the section is discarded by ld
8396      --gc-sections, leaving an invalid (0, 0) entry that can be
8397      confused with the terminator.  */
8398   if (text_section_used)
8399     {
8400       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8401       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8402                             text_section_label, "Length");
8403     }
8404   if (cold_text_section_used)
8405     {
8406       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8407                            "Address");
8408       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8409                             cold_text_section_label, "Length");
8410     }
8411
8412   for (i = 0; i < arange_table_in_use; i++)
8413     {
8414       dw_die_ref die = arange_table[i];
8415
8416       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8417       gcc_assert (die->die_mark);
8418
8419       if (die->die_tag == DW_TAG_subprogram)
8420         {
8421           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8422                                "Address");
8423           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8424                                 get_AT_low_pc (die), "Length");
8425         }
8426       else
8427         {
8428           /* A static variable; extract the symbol from DW_AT_location.
8429              Note that this code isn't currently hit, as we only emit
8430              aranges for functions (jason 9/23/99).  */
8431           dw_attr_ref a = get_AT (die, DW_AT_location);
8432           dw_loc_descr_ref loc;
8433
8434           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8435
8436           loc = AT_loc (a);
8437           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8438
8439           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8440                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8441           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8442                                get_AT_unsigned (die, DW_AT_byte_size),
8443                                "Length");
8444         }
8445     }
8446
8447   /* Output the terminator words.  */
8448   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8449   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8450 }
8451
8452 /* Add a new entry to .debug_ranges.  Return the offset at which it
8453    was placed.  */
8454
8455 static unsigned int
8456 add_ranges_num (int num)
8457 {
8458   unsigned int in_use = ranges_table_in_use;
8459
8460   if (in_use == ranges_table_allocated)
8461     {
8462       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8463       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8464                                     ranges_table_allocated);
8465       memset (ranges_table + ranges_table_in_use, 0,
8466               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8467     }
8468
8469   ranges_table[in_use].num = num;
8470   ranges_table_in_use = in_use + 1;
8471
8472   return in_use * 2 * DWARF2_ADDR_SIZE;
8473 }
8474
8475 /* Add a new entry to .debug_ranges corresponding to a block, or a
8476    range terminator if BLOCK is NULL.  */
8477
8478 static unsigned int
8479 add_ranges (const_tree block)
8480 {
8481   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8482 }
8483
8484 /* Add a new entry to .debug_ranges corresponding to a pair of
8485    labels.  */
8486
8487 static unsigned int
8488 add_ranges_by_labels (const char *begin, const char *end)
8489 {
8490   unsigned int in_use = ranges_by_label_in_use;
8491
8492   if (in_use == ranges_by_label_allocated)
8493     {
8494       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8495       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8496                                        ranges_by_label,
8497                                        ranges_by_label_allocated);
8498       memset (ranges_by_label + ranges_by_label_in_use, 0,
8499               RANGES_TABLE_INCREMENT
8500               * sizeof (struct dw_ranges_by_label_struct));
8501     }
8502
8503   ranges_by_label[in_use].begin = begin;
8504   ranges_by_label[in_use].end = end;
8505   ranges_by_label_in_use = in_use + 1;
8506
8507   return add_ranges_num (-(int)in_use - 1);
8508 }
8509
8510 static void
8511 output_ranges (void)
8512 {
8513   unsigned i;
8514   static const char *const start_fmt = "Offset 0x%x";
8515   const char *fmt = start_fmt;
8516
8517   for (i = 0; i < ranges_table_in_use; i++)
8518     {
8519       int block_num = ranges_table[i].num;
8520
8521       if (block_num > 0)
8522         {
8523           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8524           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8525
8526           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8527           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8528
8529           /* If all code is in the text section, then the compilation
8530              unit base address defaults to DW_AT_low_pc, which is the
8531              base of the text section.  */
8532           if (!have_multiple_function_sections)
8533             {
8534               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8535                                     text_section_label,
8536                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8537               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8538                                     text_section_label, NULL);
8539             }
8540
8541           /* Otherwise, the compilation unit base address is zero,
8542              which allows us to use absolute addresses, and not worry
8543              about whether the target supports cross-section
8544              arithmetic.  */
8545           else
8546             {
8547               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8548                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8549               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8550             }
8551
8552           fmt = NULL;
8553         }
8554
8555       /* Negative block_num stands for an index into ranges_by_label.  */
8556       else if (block_num < 0)
8557         {
8558           int lab_idx = - block_num - 1;
8559
8560           if (!have_multiple_function_sections)
8561             {
8562               gcc_unreachable ();
8563 #if 0
8564               /* If we ever use add_ranges_by_labels () for a single
8565                  function section, all we have to do is to take out
8566                  the #if 0 above.  */
8567               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8568                                     ranges_by_label[lab_idx].begin,
8569                                     text_section_label,
8570                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8571               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8572                                     ranges_by_label[lab_idx].end,
8573                                     text_section_label, NULL);
8574 #endif
8575             }
8576           else
8577             {
8578               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8579                                    ranges_by_label[lab_idx].begin,
8580                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8581               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8582                                    ranges_by_label[lab_idx].end,
8583                                    NULL);
8584             }
8585         }
8586       else
8587         {
8588           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8589           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8590           fmt = start_fmt;
8591         }
8592     }
8593 }
8594
8595 /* Data structure containing information about input files.  */
8596 struct file_info
8597 {
8598   const char *path;     /* Complete file name.  */
8599   const char *fname;    /* File name part.  */
8600   int length;           /* Length of entire string.  */
8601   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8602   int dir_idx;          /* Index in directory table.  */
8603 };
8604
8605 /* Data structure containing information about directories with source
8606    files.  */
8607 struct dir_info
8608 {
8609   const char *path;     /* Path including directory name.  */
8610   int length;           /* Path length.  */
8611   int prefix;           /* Index of directory entry which is a prefix.  */
8612   int count;            /* Number of files in this directory.  */
8613   int dir_idx;          /* Index of directory used as base.  */
8614 };
8615
8616 /* Callback function for file_info comparison.  We sort by looking at
8617    the directories in the path.  */
8618
8619 static int
8620 file_info_cmp (const void *p1, const void *p2)
8621 {
8622   const struct file_info *const s1 = (const struct file_info *) p1;
8623   const struct file_info *const s2 = (const struct file_info *) p2;
8624   const unsigned char *cp1;
8625   const unsigned char *cp2;
8626
8627   /* Take care of file names without directories.  We need to make sure that
8628      we return consistent values to qsort since some will get confused if
8629      we return the same value when identical operands are passed in opposite
8630      orders.  So if neither has a directory, return 0 and otherwise return
8631      1 or -1 depending on which one has the directory.  */
8632   if ((s1->path == s1->fname || s2->path == s2->fname))
8633     return (s2->path == s2->fname) - (s1->path == s1->fname);
8634
8635   cp1 = (const unsigned char *) s1->path;
8636   cp2 = (const unsigned char *) s2->path;
8637
8638   while (1)
8639     {
8640       ++cp1;
8641       ++cp2;
8642       /* Reached the end of the first path?  If so, handle like above.  */
8643       if ((cp1 == (const unsigned char *) s1->fname)
8644           || (cp2 == (const unsigned char *) s2->fname))
8645         return ((cp2 == (const unsigned char *) s2->fname)
8646                 - (cp1 == (const unsigned char *) s1->fname));
8647
8648       /* Character of current path component the same?  */
8649       else if (*cp1 != *cp2)
8650         return *cp1 - *cp2;
8651     }
8652 }
8653
8654 struct file_name_acquire_data
8655 {
8656   struct file_info *files;
8657   int used_files;
8658   int max_files;
8659 };
8660
8661 /* Traversal function for the hash table.  */
8662
8663 static int
8664 file_name_acquire (void ** slot, void *data)
8665 {
8666   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8667   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8668   struct file_info *fi;
8669   const char *f;
8670
8671   gcc_assert (fnad->max_files >= d->emitted_number);
8672
8673   if (! d->emitted_number)
8674     return 1;
8675
8676   gcc_assert (fnad->max_files != fnad->used_files);
8677
8678   fi = fnad->files + fnad->used_files++;
8679
8680   /* Skip all leading "./".  */
8681   f = d->filename;
8682   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8683     f += 2;
8684
8685   /* Create a new array entry.  */
8686   fi->path = f;
8687   fi->length = strlen (f);
8688   fi->file_idx = d;
8689
8690   /* Search for the file name part.  */
8691   f = strrchr (f, DIR_SEPARATOR);
8692 #if defined (DIR_SEPARATOR_2)
8693   {
8694     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8695
8696     if (g != NULL)
8697       {
8698         if (f == NULL || f < g)
8699           f = g;
8700       }
8701   }
8702 #endif
8703
8704   fi->fname = f == NULL ? fi->path : f + 1;
8705   return 1;
8706 }
8707
8708 /* Output the directory table and the file name table.  We try to minimize
8709    the total amount of memory needed.  A heuristic is used to avoid large
8710    slowdowns with many input files.  */
8711
8712 static void
8713 output_file_names (void)
8714 {
8715   struct file_name_acquire_data fnad;
8716   int numfiles;
8717   struct file_info *files;
8718   struct dir_info *dirs;
8719   int *saved;
8720   int *savehere;
8721   int *backmap;
8722   int ndirs;
8723   int idx_offset;
8724   int i;
8725   int idx;
8726
8727   if (!last_emitted_file)
8728     {
8729       dw2_asm_output_data (1, 0, "End directory table");
8730       dw2_asm_output_data (1, 0, "End file name table");
8731       return;
8732     }
8733
8734   numfiles = last_emitted_file->emitted_number;
8735
8736   /* Allocate the various arrays we need.  */
8737   files = XALLOCAVEC (struct file_info, numfiles);
8738   dirs = XALLOCAVEC (struct dir_info, numfiles);
8739
8740   fnad.files = files;
8741   fnad.used_files = 0;
8742   fnad.max_files = numfiles;
8743   htab_traverse (file_table, file_name_acquire, &fnad);
8744   gcc_assert (fnad.used_files == fnad.max_files);
8745
8746   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8747
8748   /* Find all the different directories used.  */
8749   dirs[0].path = files[0].path;
8750   dirs[0].length = files[0].fname - files[0].path;
8751   dirs[0].prefix = -1;
8752   dirs[0].count = 1;
8753   dirs[0].dir_idx = 0;
8754   files[0].dir_idx = 0;
8755   ndirs = 1;
8756
8757   for (i = 1; i < numfiles; i++)
8758     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8759         && memcmp (dirs[ndirs - 1].path, files[i].path,
8760                    dirs[ndirs - 1].length) == 0)
8761       {
8762         /* Same directory as last entry.  */
8763         files[i].dir_idx = ndirs - 1;
8764         ++dirs[ndirs - 1].count;
8765       }
8766     else
8767       {
8768         int j;
8769
8770         /* This is a new directory.  */
8771         dirs[ndirs].path = files[i].path;
8772         dirs[ndirs].length = files[i].fname - files[i].path;
8773         dirs[ndirs].count = 1;
8774         dirs[ndirs].dir_idx = ndirs;
8775         files[i].dir_idx = ndirs;
8776
8777         /* Search for a prefix.  */
8778         dirs[ndirs].prefix = -1;
8779         for (j = 0; j < ndirs; j++)
8780           if (dirs[j].length < dirs[ndirs].length
8781               && dirs[j].length > 1
8782               && (dirs[ndirs].prefix == -1
8783                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8784               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8785             dirs[ndirs].prefix = j;
8786
8787         ++ndirs;
8788       }
8789
8790   /* Now to the actual work.  We have to find a subset of the directories which
8791      allow expressing the file name using references to the directory table
8792      with the least amount of characters.  We do not do an exhaustive search
8793      where we would have to check out every combination of every single
8794      possible prefix.  Instead we use a heuristic which provides nearly optimal
8795      results in most cases and never is much off.  */
8796   saved = XALLOCAVEC (int, ndirs);
8797   savehere = XALLOCAVEC (int, ndirs);
8798
8799   memset (saved, '\0', ndirs * sizeof (saved[0]));
8800   for (i = 0; i < ndirs; i++)
8801     {
8802       int j;
8803       int total;
8804
8805       /* We can always save some space for the current directory.  But this
8806          does not mean it will be enough to justify adding the directory.  */
8807       savehere[i] = dirs[i].length;
8808       total = (savehere[i] - saved[i]) * dirs[i].count;
8809
8810       for (j = i + 1; j < ndirs; j++)
8811         {
8812           savehere[j] = 0;
8813           if (saved[j] < dirs[i].length)
8814             {
8815               /* Determine whether the dirs[i] path is a prefix of the
8816                  dirs[j] path.  */
8817               int k;
8818
8819               k = dirs[j].prefix;
8820               while (k != -1 && k != (int) i)
8821                 k = dirs[k].prefix;
8822
8823               if (k == (int) i)
8824                 {
8825                   /* Yes it is.  We can possibly save some memory by
8826                      writing the filenames in dirs[j] relative to
8827                      dirs[i].  */
8828                   savehere[j] = dirs[i].length;
8829                   total += (savehere[j] - saved[j]) * dirs[j].count;
8830                 }
8831             }
8832         }
8833
8834       /* Check whether we can save enough to justify adding the dirs[i]
8835          directory.  */
8836       if (total > dirs[i].length + 1)
8837         {
8838           /* It's worthwhile adding.  */
8839           for (j = i; j < ndirs; j++)
8840             if (savehere[j] > 0)
8841               {
8842                 /* Remember how much we saved for this directory so far.  */
8843                 saved[j] = savehere[j];
8844
8845                 /* Remember the prefix directory.  */
8846                 dirs[j].dir_idx = i;
8847               }
8848         }
8849     }
8850
8851   /* Emit the directory name table.  */
8852   idx = 1;
8853   idx_offset = dirs[0].length > 0 ? 1 : 0;
8854   for (i = 1 - idx_offset; i < ndirs; i++)
8855     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8856                             "Directory Entry: 0x%x", i + idx_offset);
8857
8858   dw2_asm_output_data (1, 0, "End directory table");
8859
8860   /* We have to emit them in the order of emitted_number since that's
8861      used in the debug info generation.  To do this efficiently we
8862      generate a back-mapping of the indices first.  */
8863   backmap = XALLOCAVEC (int, numfiles);
8864   for (i = 0; i < numfiles; i++)
8865     backmap[files[i].file_idx->emitted_number - 1] = i;
8866
8867   /* Now write all the file names.  */
8868   for (i = 0; i < numfiles; i++)
8869     {
8870       int file_idx = backmap[i];
8871       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8872
8873       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8874                               "File Entry: 0x%x", (unsigned) i + 1);
8875
8876       /* Include directory index.  */
8877       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8878
8879       /* Modification time.  */
8880       dw2_asm_output_data_uleb128 (0, NULL);
8881
8882       /* File length in bytes.  */
8883       dw2_asm_output_data_uleb128 (0, NULL);
8884     }
8885
8886   dw2_asm_output_data (1, 0, "End file name table");
8887 }
8888
8889
8890 /* Output the source line number correspondence information.  This
8891    information goes into the .debug_line section.  */
8892
8893 static void
8894 output_line_info (void)
8895 {
8896   char l1[20], l2[20], p1[20], p2[20];
8897   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8898   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8899   unsigned opc;
8900   unsigned n_op_args;
8901   unsigned long lt_index;
8902   unsigned long current_line;
8903   long line_offset;
8904   long line_delta;
8905   unsigned long current_file;
8906   unsigned long function;
8907
8908   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8909   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8910   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8911   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8912
8913   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8914     dw2_asm_output_data (4, 0xffffffff,
8915       "Initial length escape value indicating 64-bit DWARF extension");
8916   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8917                         "Length of Source Line Info");
8918   ASM_OUTPUT_LABEL (asm_out_file, l1);
8919
8920   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8921   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8922   ASM_OUTPUT_LABEL (asm_out_file, p1);
8923
8924   /* Define the architecture-dependent minimum instruction length (in
8925    bytes).  In this implementation of DWARF, this field is used for
8926    information purposes only.  Since GCC generates assembly language,
8927    we have no a priori knowledge of how many instruction bytes are
8928    generated for each source line, and therefore can use only the
8929    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8930    commands.  Accordingly, we fix this as `1', which is "correct
8931    enough" for all architectures, and don't let the target override.  */
8932   dw2_asm_output_data (1, 1,
8933                        "Minimum Instruction Length");
8934
8935   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8936                        "Default is_stmt_start flag");
8937   dw2_asm_output_data (1, DWARF_LINE_BASE,
8938                        "Line Base Value (Special Opcodes)");
8939   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8940                        "Line Range Value (Special Opcodes)");
8941   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8942                        "Special Opcode Base");
8943
8944   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8945     {
8946       switch (opc)
8947         {
8948         case DW_LNS_advance_pc:
8949         case DW_LNS_advance_line:
8950         case DW_LNS_set_file:
8951         case DW_LNS_set_column:
8952         case DW_LNS_fixed_advance_pc:
8953           n_op_args = 1;
8954           break;
8955         default:
8956           n_op_args = 0;
8957           break;
8958         }
8959
8960       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8961                            opc, n_op_args);
8962     }
8963
8964   /* Write out the information about the files we use.  */
8965   output_file_names ();
8966   ASM_OUTPUT_LABEL (asm_out_file, p2);
8967
8968   /* We used to set the address register to the first location in the text
8969      section here, but that didn't accomplish anything since we already
8970      have a line note for the opening brace of the first function.  */
8971
8972   /* Generate the line number to PC correspondence table, encoded as
8973      a series of state machine operations.  */
8974   current_file = 1;
8975   current_line = 1;
8976
8977   if (cfun && in_cold_section_p)
8978     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8979   else
8980     strcpy (prev_line_label, text_section_label);
8981   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8982     {
8983       dw_line_info_ref line_info = &line_info_table[lt_index];
8984
8985 #if 0
8986       /* Disable this optimization for now; GDB wants to see two line notes
8987          at the beginning of a function so it can find the end of the
8988          prologue.  */
8989
8990       /* Don't emit anything for redundant notes.  Just updating the
8991          address doesn't accomplish anything, because we already assume
8992          that anything after the last address is this line.  */
8993       if (line_info->dw_line_num == current_line
8994           && line_info->dw_file_num == current_file)
8995         continue;
8996 #endif
8997
8998       /* Emit debug info for the address of the current line.
8999
9000          Unfortunately, we have little choice here currently, and must always
9001          use the most general form.  GCC does not know the address delta
9002          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9003          attributes which will give an upper bound on the address range.  We
9004          could perhaps use length attributes to determine when it is safe to
9005          use DW_LNS_fixed_advance_pc.  */
9006
9007       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9008       if (0)
9009         {
9010           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9011           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9012                                "DW_LNS_fixed_advance_pc");
9013           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9014         }
9015       else
9016         {
9017           /* This can handle any delta.  This takes
9018              4+DWARF2_ADDR_SIZE bytes.  */
9019           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9020           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9021           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9022           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9023         }
9024
9025       strcpy (prev_line_label, line_label);
9026
9027       /* Emit debug info for the source file of the current line, if
9028          different from the previous line.  */
9029       if (line_info->dw_file_num != current_file)
9030         {
9031           current_file = line_info->dw_file_num;
9032           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9033           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9034         }
9035
9036       /* Emit debug info for the current line number, choosing the encoding
9037          that uses the least amount of space.  */
9038       if (line_info->dw_line_num != current_line)
9039         {
9040           line_offset = line_info->dw_line_num - current_line;
9041           line_delta = line_offset - DWARF_LINE_BASE;
9042           current_line = line_info->dw_line_num;
9043           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9044             /* This can handle deltas from -10 to 234, using the current
9045                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9046                takes 1 byte.  */
9047             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9048                                  "line %lu", current_line);
9049           else
9050             {
9051               /* This can handle any delta.  This takes at least 4 bytes,
9052                  depending on the value being encoded.  */
9053               dw2_asm_output_data (1, DW_LNS_advance_line,
9054                                    "advance to line %lu", current_line);
9055               dw2_asm_output_data_sleb128 (line_offset, NULL);
9056               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9057             }
9058         }
9059       else
9060         /* We still need to start a new row, so output a copy insn.  */
9061         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9062     }
9063
9064   /* Emit debug info for the address of the end of the function.  */
9065   if (0)
9066     {
9067       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9068                            "DW_LNS_fixed_advance_pc");
9069       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9070     }
9071   else
9072     {
9073       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9074       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9075       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9076       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9077     }
9078
9079   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9080   dw2_asm_output_data_uleb128 (1, NULL);
9081   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9082
9083   function = 0;
9084   current_file = 1;
9085   current_line = 1;
9086   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9087     {
9088       dw_separate_line_info_ref line_info
9089         = &separate_line_info_table[lt_index];
9090
9091 #if 0
9092       /* Don't emit anything for redundant notes.  */
9093       if (line_info->dw_line_num == current_line
9094           && line_info->dw_file_num == current_file
9095           && line_info->function == function)
9096         goto cont;
9097 #endif
9098
9099       /* Emit debug info for the address of the current line.  If this is
9100          a new function, or the first line of a function, then we need
9101          to handle it differently.  */
9102       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9103                                    lt_index);
9104       if (function != line_info->function)
9105         {
9106           function = line_info->function;
9107
9108           /* Set the address register to the first line in the function.  */
9109           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9110           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9111           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9112           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9113         }
9114       else
9115         {
9116           /* ??? See the DW_LNS_advance_pc comment above.  */
9117           if (0)
9118             {
9119               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9120                                    "DW_LNS_fixed_advance_pc");
9121               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9122             }
9123           else
9124             {
9125               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9126               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9127               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9128               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9129             }
9130         }
9131
9132       strcpy (prev_line_label, line_label);
9133
9134       /* Emit debug info for the source file of the current line, if
9135          different from the previous line.  */
9136       if (line_info->dw_file_num != current_file)
9137         {
9138           current_file = line_info->dw_file_num;
9139           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9140           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9141         }
9142
9143       /* Emit debug info for the current line number, choosing the encoding
9144          that uses the least amount of space.  */
9145       if (line_info->dw_line_num != current_line)
9146         {
9147           line_offset = line_info->dw_line_num - current_line;
9148           line_delta = line_offset - DWARF_LINE_BASE;
9149           current_line = line_info->dw_line_num;
9150           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9151             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9152                                  "line %lu", current_line);
9153           else
9154             {
9155               dw2_asm_output_data (1, DW_LNS_advance_line,
9156                                    "advance to line %lu", current_line);
9157               dw2_asm_output_data_sleb128 (line_offset, NULL);
9158               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9159             }
9160         }
9161       else
9162         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9163
9164 #if 0
9165     cont:
9166 #endif
9167
9168       lt_index++;
9169
9170       /* If we're done with a function, end its sequence.  */
9171       if (lt_index == separate_line_info_table_in_use
9172           || separate_line_info_table[lt_index].function != function)
9173         {
9174           current_file = 1;
9175           current_line = 1;
9176
9177           /* Emit debug info for the address of the end of the function.  */
9178           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9179           if (0)
9180             {
9181               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9182                                    "DW_LNS_fixed_advance_pc");
9183               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9184             }
9185           else
9186             {
9187               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9188               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9189               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9190               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9191             }
9192
9193           /* Output the marker for the end of this sequence.  */
9194           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9195           dw2_asm_output_data_uleb128 (1, NULL);
9196           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9197         }
9198     }
9199
9200   /* Output the marker for the end of the line number info.  */
9201   ASM_OUTPUT_LABEL (asm_out_file, l2);
9202 }
9203 \f
9204 /* Given a pointer to a tree node for some base type, return a pointer to
9205    a DIE that describes the given type.
9206
9207    This routine must only be called for GCC type nodes that correspond to
9208    Dwarf base (fundamental) types.  */
9209
9210 static dw_die_ref
9211 base_type_die (tree type)
9212 {
9213   dw_die_ref base_type_result;
9214   enum dwarf_type encoding;
9215
9216   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9217     return 0;
9218
9219   switch (TREE_CODE (type))
9220     {
9221     case INTEGER_TYPE:
9222       if (TYPE_STRING_FLAG (type))
9223         {
9224           if (TYPE_UNSIGNED (type))
9225             encoding = DW_ATE_unsigned_char;
9226           else
9227             encoding = DW_ATE_signed_char;
9228         }
9229       else if (TYPE_UNSIGNED (type))
9230         encoding = DW_ATE_unsigned;
9231       else
9232         encoding = DW_ATE_signed;
9233       break;
9234
9235     case REAL_TYPE:
9236       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9237         encoding = DW_ATE_decimal_float;
9238       else
9239         encoding = DW_ATE_float;
9240       break;
9241
9242     case FIXED_POINT_TYPE:
9243       if (TYPE_UNSIGNED (type))
9244         encoding = DW_ATE_unsigned_fixed;
9245       else
9246         encoding = DW_ATE_signed_fixed;
9247       break;
9248
9249       /* Dwarf2 doesn't know anything about complex ints, so use
9250          a user defined type for it.  */
9251     case COMPLEX_TYPE:
9252       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9253         encoding = DW_ATE_complex_float;
9254       else
9255         encoding = DW_ATE_lo_user;
9256       break;
9257
9258     case BOOLEAN_TYPE:
9259       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9260       encoding = DW_ATE_boolean;
9261       break;
9262
9263     default:
9264       /* No other TREE_CODEs are Dwarf fundamental types.  */
9265       gcc_unreachable ();
9266     }
9267
9268   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9269
9270   /* This probably indicates a bug.  */
9271   if (! TYPE_NAME (type))
9272     add_name_attribute (base_type_result, "__unknown__");
9273
9274   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9275                    int_size_in_bytes (type));
9276   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9277
9278   return base_type_result;
9279 }
9280
9281 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9282    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9283
9284 static inline int
9285 is_base_type (tree type)
9286 {
9287   switch (TREE_CODE (type))
9288     {
9289     case ERROR_MARK:
9290     case VOID_TYPE:
9291     case INTEGER_TYPE:
9292     case REAL_TYPE:
9293     case FIXED_POINT_TYPE:
9294     case COMPLEX_TYPE:
9295     case BOOLEAN_TYPE:
9296       return 1;
9297
9298     case ARRAY_TYPE:
9299     case RECORD_TYPE:
9300     case UNION_TYPE:
9301     case QUAL_UNION_TYPE:
9302     case ENUMERAL_TYPE:
9303     case FUNCTION_TYPE:
9304     case METHOD_TYPE:
9305     case POINTER_TYPE:
9306     case REFERENCE_TYPE:
9307     case OFFSET_TYPE:
9308     case LANG_TYPE:
9309     case VECTOR_TYPE:
9310       return 0;
9311
9312     default:
9313       gcc_unreachable ();
9314     }
9315
9316   return 0;
9317 }
9318
9319 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9320    node, return the size in bits for the type if it is a constant, or else
9321    return the alignment for the type if the type's size is not constant, or
9322    else return BITS_PER_WORD if the type actually turns out to be an
9323    ERROR_MARK node.  */
9324
9325 static inline unsigned HOST_WIDE_INT
9326 simple_type_size_in_bits (const_tree type)
9327 {
9328   if (TREE_CODE (type) == ERROR_MARK)
9329     return BITS_PER_WORD;
9330   else if (TYPE_SIZE (type) == NULL_TREE)
9331     return 0;
9332   else if (host_integerp (TYPE_SIZE (type), 1))
9333     return tree_low_cst (TYPE_SIZE (type), 1);
9334   else
9335     return TYPE_ALIGN (type);
9336 }
9337
9338 /* Return true if the debug information for the given type should be
9339    emitted as a subrange type.  */
9340
9341 static inline bool
9342 is_subrange_type (const_tree type)
9343 {
9344   tree subtype = TREE_TYPE (type);
9345
9346   /* Subrange types are identified by the fact that they are integer
9347      types, and that they have a subtype which is either an integer type
9348      or an enumeral type.  */
9349
9350   if (TREE_CODE (type) != INTEGER_TYPE
9351       || subtype == NULL_TREE)
9352     return false;
9353
9354   if (TREE_CODE (subtype) != INTEGER_TYPE
9355       && TREE_CODE (subtype) != ENUMERAL_TYPE
9356       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9357     return false;
9358
9359   if (TREE_CODE (type) == TREE_CODE (subtype)
9360       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9361       && TYPE_MIN_VALUE (type) != NULL
9362       && TYPE_MIN_VALUE (subtype) != NULL
9363       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9364       && TYPE_MAX_VALUE (type) != NULL
9365       && TYPE_MAX_VALUE (subtype) != NULL
9366       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9367     {
9368       /* The type and its subtype have the same representation.  If in
9369          addition the two types also have the same name, then the given
9370          type is not a subrange type, but rather a plain base type.  */
9371       /* FIXME: brobecker/2004-03-22:
9372          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9373          therefore be sufficient to check the TYPE_SIZE node pointers
9374          rather than checking the actual size.  Unfortunately, we have
9375          found some cases, such as in the Ada "integer" type, where
9376          this is not the case.  Until this problem is solved, we need to
9377          keep checking the actual size.  */
9378       tree type_name = TYPE_NAME (type);
9379       tree subtype_name = TYPE_NAME (subtype);
9380
9381       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9382         type_name = DECL_NAME (type_name);
9383
9384       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9385         subtype_name = DECL_NAME (subtype_name);
9386
9387       if (type_name == subtype_name)
9388         return false;
9389     }
9390
9391   return true;
9392 }
9393
9394 /*  Given a pointer to a tree node for a subrange type, return a pointer
9395     to a DIE that describes the given type.  */
9396
9397 static dw_die_ref
9398 subrange_type_die (tree type, dw_die_ref context_die)
9399 {
9400   dw_die_ref subrange_die;
9401   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9402
9403   if (context_die == NULL)
9404     context_die = comp_unit_die;
9405
9406   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9407
9408   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9409     {
9410       /* The size of the subrange type and its base type do not match,
9411          so we need to generate a size attribute for the subrange type.  */
9412       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9413     }
9414
9415   if (TYPE_MIN_VALUE (type) != NULL)
9416     add_bound_info (subrange_die, DW_AT_lower_bound,
9417                     TYPE_MIN_VALUE (type));
9418   if (TYPE_MAX_VALUE (type) != NULL)
9419     add_bound_info (subrange_die, DW_AT_upper_bound,
9420                     TYPE_MAX_VALUE (type));
9421
9422   return subrange_die;
9423 }
9424
9425 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9426    entry that chains various modifiers in front of the given type.  */
9427
9428 static dw_die_ref
9429 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9430                    dw_die_ref context_die)
9431 {
9432   enum tree_code code = TREE_CODE (type);
9433   dw_die_ref mod_type_die;
9434   dw_die_ref sub_die = NULL;
9435   tree item_type = NULL;
9436   tree qualified_type;
9437   tree name;
9438
9439   if (code == ERROR_MARK)
9440     return NULL;
9441
9442   /* See if we already have the appropriately qualified variant of
9443      this type.  */
9444   qualified_type
9445     = get_qualified_type (type,
9446                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9447                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9448
9449   /* If we do, then we can just use its DIE, if it exists.  */
9450   if (qualified_type)
9451     {
9452       mod_type_die = lookup_type_die (qualified_type);
9453       if (mod_type_die)
9454         return mod_type_die;
9455     }
9456
9457   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9458
9459   /* Handle C typedef types.  */
9460   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9461     {
9462       tree dtype = TREE_TYPE (name);
9463
9464       if (qualified_type == dtype)
9465         {
9466           /* For a named type, use the typedef.  */
9467           gen_type_die (qualified_type, context_die);
9468           return lookup_type_die (qualified_type);
9469         }
9470       else if (is_const_type < TYPE_READONLY (dtype)
9471                || is_volatile_type < TYPE_VOLATILE (dtype)
9472                || (is_const_type <= TYPE_READONLY (dtype)
9473                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9474                    && DECL_ORIGINAL_TYPE (name) != type))
9475         /* cv-unqualified version of named type.  Just use the unnamed
9476            type to which it refers.  */
9477         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9478                                   is_const_type, is_volatile_type,
9479                                   context_die);
9480       /* Else cv-qualified version of named type; fall through.  */
9481     }
9482
9483   if (is_const_type)
9484     {
9485       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9486       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9487     }
9488   else if (is_volatile_type)
9489     {
9490       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9491       sub_die = modified_type_die (type, 0, 0, context_die);
9492     }
9493   else if (code == POINTER_TYPE)
9494     {
9495       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9496       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9497                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9498       item_type = TREE_TYPE (type);
9499     }
9500   else if (code == REFERENCE_TYPE)
9501     {
9502       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9503       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9504                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9505       item_type = TREE_TYPE (type);
9506     }
9507   else if (is_subrange_type (type))
9508     {
9509       mod_type_die = subrange_type_die (type, context_die);
9510       item_type = TREE_TYPE (type);
9511     }
9512   else if (is_base_type (type))
9513     mod_type_die = base_type_die (type);
9514   else
9515     {
9516       gen_type_die (type, context_die);
9517
9518       /* We have to get the type_main_variant here (and pass that to the
9519          `lookup_type_die' routine) because the ..._TYPE node we have
9520          might simply be a *copy* of some original type node (where the
9521          copy was created to help us keep track of typedef names) and
9522          that copy might have a different TYPE_UID from the original
9523          ..._TYPE node.  */
9524       if (TREE_CODE (type) != VECTOR_TYPE)
9525         return lookup_type_die (type_main_variant (type));
9526       else
9527         /* Vectors have the debugging information in the type,
9528            not the main variant.  */
9529         return lookup_type_die (type);
9530     }
9531
9532   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9533      don't output a DW_TAG_typedef, since there isn't one in the
9534      user's program; just attach a DW_AT_name to the type.  */
9535   if (name
9536       && (TREE_CODE (name) != TYPE_DECL
9537           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9538     {
9539       if (TREE_CODE (name) == TYPE_DECL)
9540         /* Could just call add_name_and_src_coords_attributes here,
9541            but since this is a builtin type it doesn't have any
9542            useful source coordinates anyway.  */
9543         name = DECL_NAME (name);
9544       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9545     }
9546
9547   if (qualified_type)
9548     equate_type_number_to_die (qualified_type, mod_type_die);
9549
9550   if (item_type)
9551     /* We must do this after the equate_type_number_to_die call, in case
9552        this is a recursive type.  This ensures that the modified_type_die
9553        recursion will terminate even if the type is recursive.  Recursive
9554        types are possible in Ada.  */
9555     sub_die = modified_type_die (item_type,
9556                                  TYPE_READONLY (item_type),
9557                                  TYPE_VOLATILE (item_type),
9558                                  context_die);
9559
9560   if (sub_die != NULL)
9561     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9562
9563   return mod_type_die;
9564 }
9565
9566 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9567    an enumerated type.  */
9568
9569 static inline int
9570 type_is_enum (const_tree type)
9571 {
9572   return TREE_CODE (type) == ENUMERAL_TYPE;
9573 }
9574
9575 /* Return the DBX register number described by a given RTL node.  */
9576
9577 static unsigned int
9578 dbx_reg_number (const_rtx rtl)
9579 {
9580   unsigned regno = REGNO (rtl);
9581
9582   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9583
9584 #ifdef LEAF_REG_REMAP
9585   if (current_function_uses_only_leaf_regs)
9586     {
9587       int leaf_reg = LEAF_REG_REMAP (regno);
9588       if (leaf_reg != -1)
9589         regno = (unsigned) leaf_reg;
9590     }
9591 #endif
9592
9593   return DBX_REGISTER_NUMBER (regno);
9594 }
9595
9596 /* Optionally add a DW_OP_piece term to a location description expression.
9597    DW_OP_piece is only added if the location description expression already
9598    doesn't end with DW_OP_piece.  */
9599
9600 static void
9601 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9602 {
9603   dw_loc_descr_ref loc;
9604
9605   if (*list_head != NULL)
9606     {
9607       /* Find the end of the chain.  */
9608       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9609         ;
9610
9611       if (loc->dw_loc_opc != DW_OP_piece)
9612         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9613     }
9614 }
9615
9616 /* Return a location descriptor that designates a machine register or
9617    zero if there is none.  */
9618
9619 static dw_loc_descr_ref
9620 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9621 {
9622   rtx regs;
9623
9624   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9625     return 0;
9626
9627   regs = targetm.dwarf_register_span (rtl);
9628
9629   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9630     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9631   else
9632     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9633 }
9634
9635 /* Return a location descriptor that designates a machine register for
9636    a given hard register number.  */
9637
9638 static dw_loc_descr_ref
9639 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9640 {
9641   dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9642
9643   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9644     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9645
9646   return reg_loc_descr;
9647 }
9648
9649 /* Given an RTL of a register, return a location descriptor that
9650    designates a value that spans more than one register.  */
9651
9652 static dw_loc_descr_ref
9653 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9654                              enum var_init_status initialized)
9655 {
9656   int nregs, size, i;
9657   unsigned reg;
9658   dw_loc_descr_ref loc_result = NULL;
9659
9660   reg = REGNO (rtl);
9661 #ifdef LEAF_REG_REMAP
9662   if (current_function_uses_only_leaf_regs)
9663     {
9664       int leaf_reg = LEAF_REG_REMAP (reg);
9665       if (leaf_reg != -1)
9666         reg = (unsigned) leaf_reg;
9667     }
9668 #endif
9669   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9670   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9671
9672   /* Simple, contiguous registers.  */
9673   if (regs == NULL_RTX)
9674     {
9675       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9676
9677       loc_result = NULL;
9678       while (nregs--)
9679         {
9680           dw_loc_descr_ref t;
9681
9682           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9683                                       VAR_INIT_STATUS_INITIALIZED);
9684           add_loc_descr (&loc_result, t);
9685           add_loc_descr_op_piece (&loc_result, size);
9686           ++reg;
9687         }
9688       return loc_result;
9689     }
9690
9691   /* Now onto stupid register sets in non contiguous locations.  */
9692
9693   gcc_assert (GET_CODE (regs) == PARALLEL);
9694
9695   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9696   loc_result = NULL;
9697
9698   for (i = 0; i < XVECLEN (regs, 0); ++i)
9699     {
9700       dw_loc_descr_ref t;
9701
9702       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9703                                   VAR_INIT_STATUS_INITIALIZED);
9704       add_loc_descr (&loc_result, t);
9705       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9706       add_loc_descr_op_piece (&loc_result, size);
9707     }
9708
9709   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9710     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9711   return loc_result;
9712 }
9713
9714 #endif /* DWARF2_DEBUGGING_INFO */
9715
9716 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9717
9718 /* Return a location descriptor that designates a constant.  */
9719
9720 static dw_loc_descr_ref
9721 int_loc_descriptor (HOST_WIDE_INT i)
9722 {
9723   enum dwarf_location_atom op;
9724
9725   /* Pick the smallest representation of a constant, rather than just
9726      defaulting to the LEB encoding.  */
9727   if (i >= 0)
9728     {
9729       if (i <= 31)
9730         op = DW_OP_lit0 + i;
9731       else if (i <= 0xff)
9732         op = DW_OP_const1u;
9733       else if (i <= 0xffff)
9734         op = DW_OP_const2u;
9735       else if (HOST_BITS_PER_WIDE_INT == 32
9736                || i <= 0xffffffff)
9737         op = DW_OP_const4u;
9738       else
9739         op = DW_OP_constu;
9740     }
9741   else
9742     {
9743       if (i >= -0x80)
9744         op = DW_OP_const1s;
9745       else if (i >= -0x8000)
9746         op = DW_OP_const2s;
9747       else if (HOST_BITS_PER_WIDE_INT == 32
9748                || i >= -0x80000000)
9749         op = DW_OP_const4s;
9750       else
9751         op = DW_OP_consts;
9752     }
9753
9754   return new_loc_descr (op, i, 0);
9755 }
9756 #endif
9757
9758 #ifdef DWARF2_DEBUGGING_INFO
9759
9760 /* Return a location descriptor that designates a base+offset location.  */
9761
9762 static dw_loc_descr_ref
9763 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9764                  enum var_init_status initialized)
9765 {
9766   unsigned int regno;
9767   dw_loc_descr_ref result;
9768   dw_fde_ref fde = current_fde ();
9769
9770   /* We only use "frame base" when we're sure we're talking about the
9771      post-prologue local stack frame.  We do this by *not* running
9772      register elimination until this point, and recognizing the special
9773      argument pointer and soft frame pointer rtx's.  */
9774   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9775     {
9776       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9777
9778       if (elim != reg)
9779         {
9780           if (GET_CODE (elim) == PLUS)
9781             {
9782               offset += INTVAL (XEXP (elim, 1));
9783               elim = XEXP (elim, 0);
9784             }
9785           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9786                        && (elim == hard_frame_pointer_rtx
9787                            || elim == stack_pointer_rtx))
9788                       || elim == (frame_pointer_needed
9789                                   ? hard_frame_pointer_rtx
9790                                   : stack_pointer_rtx));
9791
9792           /* If drap register is used to align stack, use frame
9793              pointer + offset to access stack variables.  If stack
9794              is aligned without drap, use stack pointer + offset to
9795              access stack variables.  */
9796           if (crtl->stack_realign_tried
9797               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9798               && reg == frame_pointer_rtx)
9799             {
9800               int base_reg
9801                 = DWARF_FRAME_REGNUM (cfa.indirect
9802                                       ? HARD_FRAME_POINTER_REGNUM
9803                                       : STACK_POINTER_REGNUM);
9804               return new_reg_loc_descr (base_reg, offset);
9805             }
9806
9807           offset += frame_pointer_fb_offset;
9808           return new_loc_descr (DW_OP_fbreg, offset, 0);
9809         }
9810     }
9811   else if (fde
9812            && fde->drap_reg != INVALID_REGNUM
9813            && (fde->drap_reg == REGNO (reg)
9814                || fde->vdrap_reg == REGNO (reg)))
9815     {
9816       /* Use cfa+offset to represent the location of arguments passed
9817          on stack when drap is used to align stack.  */
9818       return new_loc_descr (DW_OP_fbreg, offset, 0);
9819     }
9820
9821   regno = dbx_reg_number (reg);
9822   if (regno <= 31)
9823     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9824   else
9825     result = new_loc_descr (DW_OP_bregx, regno, offset);
9826
9827   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9828     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9829
9830   return result;
9831 }
9832
9833 /* Return true if this RTL expression describes a base+offset calculation.  */
9834
9835 static inline int
9836 is_based_loc (const_rtx rtl)
9837 {
9838   return (GET_CODE (rtl) == PLUS
9839           && ((REG_P (XEXP (rtl, 0))
9840                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9841                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9842 }
9843
9844 /* Return a descriptor that describes the concatenation of N locations
9845    used to form the address of a memory location.  */
9846
9847 static dw_loc_descr_ref
9848 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9849                             enum var_init_status initialized)
9850 {
9851   unsigned int i;
9852   dw_loc_descr_ref cc_loc_result = NULL;
9853   unsigned int n = XVECLEN (concatn, 0);
9854
9855   for (i = 0; i < n; ++i)
9856     {
9857       dw_loc_descr_ref ref;
9858       rtx x = XVECEXP (concatn, 0, i);
9859
9860       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9861       if (ref == NULL)
9862         return NULL;
9863
9864       add_loc_descr (&cc_loc_result, ref);
9865       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9866     }
9867
9868   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9869     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9870
9871   return cc_loc_result;
9872 }
9873
9874 /* The following routine converts the RTL for a variable or parameter
9875    (resident in memory) into an equivalent Dwarf representation of a
9876    mechanism for getting the address of that same variable onto the top of a
9877    hypothetical "address evaluation" stack.
9878
9879    When creating memory location descriptors, we are effectively transforming
9880    the RTL for a memory-resident object into its Dwarf postfix expression
9881    equivalent.  This routine recursively descends an RTL tree, turning
9882    it into Dwarf postfix code as it goes.
9883
9884    MODE is the mode of the memory reference, needed to handle some
9885    autoincrement addressing modes.
9886
9887    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9888    location list for RTL.
9889
9890    Return 0 if we can't represent the location.  */
9891
9892 static dw_loc_descr_ref
9893 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9894                     enum var_init_status initialized)
9895 {
9896   dw_loc_descr_ref mem_loc_result = NULL;
9897   enum dwarf_location_atom op;
9898
9899   /* Note that for a dynamically sized array, the location we will generate a
9900      description of here will be the lowest numbered location which is
9901      actually within the array.  That's *not* necessarily the same as the
9902      zeroth element of the array.  */
9903
9904   rtl = targetm.delegitimize_address (rtl);
9905
9906   switch (GET_CODE (rtl))
9907     {
9908     case POST_INC:
9909     case POST_DEC:
9910     case POST_MODIFY:
9911       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9912          just fall into the SUBREG code.  */
9913
9914       /* ... fall through ...  */
9915
9916     case SUBREG:
9917       /* The case of a subreg may arise when we have a local (register)
9918          variable or a formal (register) parameter which doesn't quite fill
9919          up an entire register.  For now, just assume that it is
9920          legitimate to make the Dwarf info refer to the whole register which
9921          contains the given subreg.  */
9922       rtl = XEXP (rtl, 0);
9923
9924       /* ... fall through ...  */
9925
9926     case REG:
9927       /* Whenever a register number forms a part of the description of the
9928          method for calculating the (dynamic) address of a memory resident
9929          object, DWARF rules require the register number be referred to as
9930          a "base register".  This distinction is not based in any way upon
9931          what category of register the hardware believes the given register
9932          belongs to.  This is strictly DWARF terminology we're dealing with
9933          here. Note that in cases where the location of a memory-resident
9934          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9935          OP_CONST (0)) the actual DWARF location descriptor that we generate
9936          may just be OP_BASEREG (basereg).  This may look deceptively like
9937          the object in question was allocated to a register (rather than in
9938          memory) so DWARF consumers need to be aware of the subtle
9939          distinction between OP_REG and OP_BASEREG.  */
9940       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9941         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9942       break;
9943
9944     case MEM:
9945       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9946                                            VAR_INIT_STATUS_INITIALIZED);
9947       if (mem_loc_result != 0)
9948         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9949       break;
9950
9951     case LO_SUM:
9952          rtl = XEXP (rtl, 1);
9953
9954       /* ... fall through ...  */
9955
9956     case LABEL_REF:
9957       /* Some ports can transform a symbol ref into a label ref, because
9958          the symbol ref is too far away and has to be dumped into a constant
9959          pool.  */
9960     case CONST:
9961     case SYMBOL_REF:
9962       /* Alternatively, the symbol in the constant pool might be referenced
9963          by a different symbol.  */
9964       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9965         {
9966           bool marked;
9967           rtx tmp = get_pool_constant_mark (rtl, &marked);
9968
9969           if (GET_CODE (tmp) == SYMBOL_REF)
9970             {
9971               rtl = tmp;
9972               if (CONSTANT_POOL_ADDRESS_P (tmp))
9973                 get_pool_constant_mark (tmp, &marked);
9974               else
9975                 marked = true;
9976             }
9977
9978           /* If all references to this pool constant were optimized away,
9979              it was not output and thus we can't represent it.
9980              FIXME: might try to use DW_OP_const_value here, though
9981              DW_OP_piece complicates it.  */
9982           if (!marked)
9983             return 0;
9984         }
9985
9986       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9987       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9988       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9989       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9990       break;
9991
9992     case PRE_MODIFY:
9993       /* Extract the PLUS expression nested inside and fall into
9994          PLUS code below.  */
9995       rtl = XEXP (rtl, 1);
9996       goto plus;
9997
9998     case PRE_INC:
9999     case PRE_DEC:
10000       /* Turn these into a PLUS expression and fall into the PLUS code
10001          below.  */
10002       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10003                           GEN_INT (GET_CODE (rtl) == PRE_INC
10004                                    ? GET_MODE_UNIT_SIZE (mode)
10005                                    : -GET_MODE_UNIT_SIZE (mode)));
10006
10007       /* ... fall through ...  */
10008
10009     case PLUS:
10010     plus:
10011       if (is_based_loc (rtl))
10012         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10013                                           INTVAL (XEXP (rtl, 1)),
10014                                           VAR_INIT_STATUS_INITIALIZED);
10015       else
10016         {
10017           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10018                                                VAR_INIT_STATUS_INITIALIZED);
10019           if (mem_loc_result == 0)
10020             break;
10021
10022           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10023               && INTVAL (XEXP (rtl, 1)) >= 0)
10024             add_loc_descr (&mem_loc_result,
10025                            new_loc_descr (DW_OP_plus_uconst,
10026                                           INTVAL (XEXP (rtl, 1)), 0));
10027           else
10028             {
10029               add_loc_descr (&mem_loc_result,
10030                              mem_loc_descriptor (XEXP (rtl, 1), mode,
10031                                                  VAR_INIT_STATUS_INITIALIZED));
10032               add_loc_descr (&mem_loc_result,
10033                              new_loc_descr (DW_OP_plus, 0, 0));
10034             }
10035         }
10036       break;
10037
10038     /* If a pseudo-reg is optimized away, it is possible for it to
10039        be replaced with a MEM containing a multiply or shift.  */
10040     case MULT:
10041       op = DW_OP_mul;
10042       goto do_binop;
10043
10044     case ASHIFT:
10045       op = DW_OP_shl;
10046       goto do_binop;
10047
10048     case ASHIFTRT:
10049       op = DW_OP_shra;
10050       goto do_binop;
10051
10052     case LSHIFTRT:
10053       op = DW_OP_shr;
10054       goto do_binop;
10055
10056     do_binop:
10057       {
10058         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10059                                                    VAR_INIT_STATUS_INITIALIZED);
10060         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10061                                                    VAR_INIT_STATUS_INITIALIZED);
10062
10063         if (op0 == 0 || op1 == 0)
10064           break;
10065
10066         mem_loc_result = op0;
10067         add_loc_descr (&mem_loc_result, op1);
10068         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10069         break;
10070       }
10071
10072     case CONST_INT:
10073       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10074       break;
10075
10076     case CONCATN:
10077       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10078                                                    VAR_INIT_STATUS_INITIALIZED);
10079       break;
10080
10081     default:
10082       gcc_unreachable ();
10083     }
10084
10085   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10086     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10087
10088   return mem_loc_result;
10089 }
10090
10091 /* Return a descriptor that describes the concatenation of two locations.
10092    This is typically a complex variable.  */
10093
10094 static dw_loc_descr_ref
10095 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10096 {
10097   dw_loc_descr_ref cc_loc_result = NULL;
10098   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10099   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10100
10101   if (x0_ref == 0 || x1_ref == 0)
10102     return 0;
10103
10104   cc_loc_result = x0_ref;
10105   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10106
10107   add_loc_descr (&cc_loc_result, x1_ref);
10108   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10109
10110   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10111     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10112
10113   return cc_loc_result;
10114 }
10115
10116 /* Return a descriptor that describes the concatenation of N
10117    locations.  */
10118
10119 static dw_loc_descr_ref
10120 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10121 {
10122   unsigned int i;
10123   dw_loc_descr_ref cc_loc_result = NULL;
10124   unsigned int n = XVECLEN (concatn, 0);
10125
10126   for (i = 0; i < n; ++i)
10127     {
10128       dw_loc_descr_ref ref;
10129       rtx x = XVECEXP (concatn, 0, i);
10130
10131       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10132       if (ref == NULL)
10133         return NULL;
10134
10135       add_loc_descr (&cc_loc_result, ref);
10136       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10137     }
10138
10139   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10140     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10141
10142   return cc_loc_result;
10143 }
10144
10145 /* Output a proper Dwarf location descriptor for a variable or parameter
10146    which is either allocated in a register or in a memory location.  For a
10147    register, we just generate an OP_REG and the register number.  For a
10148    memory location we provide a Dwarf postfix expression describing how to
10149    generate the (dynamic) address of the object onto the address stack.
10150
10151    If we don't know how to describe it, return 0.  */
10152
10153 static dw_loc_descr_ref
10154 loc_descriptor (rtx rtl, enum var_init_status initialized)
10155 {
10156   dw_loc_descr_ref loc_result = NULL;
10157
10158   switch (GET_CODE (rtl))
10159     {
10160     case SUBREG:
10161       /* The case of a subreg may arise when we have a local (register)
10162          variable or a formal (register) parameter which doesn't quite fill
10163          up an entire register.  For now, just assume that it is
10164          legitimate to make the Dwarf info refer to the whole register which
10165          contains the given subreg.  */
10166       rtl = SUBREG_REG (rtl);
10167
10168       /* ... fall through ...  */
10169
10170     case REG:
10171       loc_result = reg_loc_descriptor (rtl, initialized);
10172       break;
10173
10174     case MEM:
10175       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10176                                        initialized);
10177       break;
10178
10179     case CONCAT:
10180       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10181                                           initialized);
10182       break;
10183
10184     case CONCATN:
10185       loc_result = concatn_loc_descriptor (rtl, initialized);
10186       break;
10187
10188     case VAR_LOCATION:
10189       /* Single part.  */
10190       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10191         {
10192           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10193           break;
10194         }
10195
10196       rtl = XEXP (rtl, 1);
10197       /* FALLTHRU */
10198
10199     case PARALLEL:
10200       {
10201         rtvec par_elems = XVEC (rtl, 0);
10202         int num_elem = GET_NUM_ELEM (par_elems);
10203         enum machine_mode mode;
10204         int i;
10205
10206         /* Create the first one, so we have something to add to.  */
10207         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10208                                      initialized);
10209         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10210         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10211         for (i = 1; i < num_elem; i++)
10212           {
10213             dw_loc_descr_ref temp;
10214
10215             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10216                                    initialized);
10217             add_loc_descr (&loc_result, temp);
10218             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10219             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10220           }
10221       }
10222       break;
10223
10224     default:
10225       gcc_unreachable ();
10226     }
10227
10228   return loc_result;
10229 }
10230
10231 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10232    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10233    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10234    top-level invocation, and we require the address of LOC; is 0 if we require
10235    the value of LOC.  */
10236
10237 static dw_loc_descr_ref
10238 loc_descriptor_from_tree_1 (tree loc, int want_address)
10239 {
10240   dw_loc_descr_ref ret, ret1;
10241   int have_address = 0;
10242   enum dwarf_location_atom op;
10243
10244   /* ??? Most of the time we do not take proper care for sign/zero
10245      extending the values properly.  Hopefully this won't be a real
10246      problem...  */
10247
10248   switch (TREE_CODE (loc))
10249     {
10250     case ERROR_MARK:
10251       return 0;
10252
10253     case PLACEHOLDER_EXPR:
10254       /* This case involves extracting fields from an object to determine the
10255          position of other fields.  We don't try to encode this here.  The
10256          only user of this is Ada, which encodes the needed information using
10257          the names of types.  */
10258       return 0;
10259
10260     case CALL_EXPR:
10261       return 0;
10262
10263     case PREINCREMENT_EXPR:
10264     case PREDECREMENT_EXPR:
10265     case POSTINCREMENT_EXPR:
10266     case POSTDECREMENT_EXPR:
10267       /* There are no opcodes for these operations.  */
10268       return 0;
10269
10270     case ADDR_EXPR:
10271       /* If we already want an address, there's nothing we can do.  */
10272       if (want_address)
10273         return 0;
10274
10275       /* Otherwise, process the argument and look for the address.  */
10276       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10277
10278     case VAR_DECL:
10279       if (DECL_THREAD_LOCAL_P (loc))
10280         {
10281           rtx rtl;
10282           unsigned first_op;
10283           unsigned second_op;
10284
10285           if (targetm.have_tls)
10286             {
10287               /* If this is not defined, we have no way to emit the
10288                  data.  */
10289               if (!targetm.asm_out.output_dwarf_dtprel)
10290                 return 0;
10291
10292                /* The way DW_OP_GNU_push_tls_address is specified, we
10293                   can only look up addresses of objects in the current
10294                   module.  */
10295               if (DECL_EXTERNAL (loc))
10296                 return 0;
10297               first_op = INTERNAL_DW_OP_tls_addr;
10298               second_op = DW_OP_GNU_push_tls_address;
10299             }
10300           else
10301             {
10302               if (!targetm.emutls.debug_form_tls_address)
10303                 return 0;
10304               loc = emutls_decl (loc);
10305               first_op = DW_OP_addr;
10306               second_op = DW_OP_form_tls_address;
10307             }
10308
10309           rtl = rtl_for_decl_location (loc);
10310           if (rtl == NULL_RTX)
10311             return 0;
10312
10313           if (!MEM_P (rtl))
10314             return 0;
10315           rtl = XEXP (rtl, 0);
10316           if (! CONSTANT_P (rtl))
10317             return 0;
10318
10319           ret = new_loc_descr (first_op, 0, 0);
10320           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10321           ret->dw_loc_oprnd1.v.val_addr = rtl;
10322
10323           ret1 = new_loc_descr (second_op, 0, 0);
10324           add_loc_descr (&ret, ret1);
10325
10326           have_address = 1;
10327           break;
10328         }
10329       /* FALLTHRU */
10330
10331     case PARM_DECL:
10332       if (DECL_HAS_VALUE_EXPR_P (loc))
10333         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10334                                            want_address);
10335       /* FALLTHRU */
10336
10337     case RESULT_DECL:
10338     case FUNCTION_DECL:
10339       {
10340         rtx rtl = rtl_for_decl_location (loc);
10341
10342         if (rtl == NULL_RTX)
10343           return 0;
10344         else if (GET_CODE (rtl) == CONST_INT)
10345           {
10346             HOST_WIDE_INT val = INTVAL (rtl);
10347             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10348               val &= GET_MODE_MASK (DECL_MODE (loc));
10349             ret = int_loc_descriptor (val);
10350           }
10351         else if (GET_CODE (rtl) == CONST_STRING)
10352           return 0;
10353         else if (CONSTANT_P (rtl))
10354           {
10355             ret = new_loc_descr (DW_OP_addr, 0, 0);
10356             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10357             ret->dw_loc_oprnd1.v.val_addr = rtl;
10358           }
10359         else
10360           {
10361             enum machine_mode mode;
10362
10363             /* Certain constructs can only be represented at top-level.  */
10364             if (want_address == 2)
10365               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10366
10367             mode = GET_MODE (rtl);
10368             if (MEM_P (rtl))
10369               {
10370                 rtl = XEXP (rtl, 0);
10371                 have_address = 1;
10372               }
10373             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10374           }
10375       }
10376       break;
10377
10378     case INDIRECT_REF:
10379       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10380       have_address = 1;
10381       break;
10382
10383     case COMPOUND_EXPR:
10384       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10385
10386     CASE_CONVERT:
10387     case VIEW_CONVERT_EXPR:
10388     case SAVE_EXPR:
10389     case MODIFY_EXPR:
10390       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10391
10392     case COMPONENT_REF:
10393     case BIT_FIELD_REF:
10394     case ARRAY_REF:
10395     case ARRAY_RANGE_REF:
10396       {
10397         tree obj, offset;
10398         HOST_WIDE_INT bitsize, bitpos, bytepos;
10399         enum machine_mode mode;
10400         int volatilep;
10401         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10402
10403         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10404                                    &unsignedp, &volatilep, false);
10405
10406         if (obj == loc)
10407           return 0;
10408
10409         ret = loc_descriptor_from_tree_1 (obj, 1);
10410         if (ret == 0
10411             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10412           return 0;
10413
10414         if (offset != NULL_TREE)
10415           {
10416             /* Variable offset.  */
10417             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
10418             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10419           }
10420
10421         bytepos = bitpos / BITS_PER_UNIT;
10422         if (bytepos > 0)
10423           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10424         else if (bytepos < 0)
10425           {
10426             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10427             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10428           }
10429
10430         have_address = 1;
10431         break;
10432       }
10433
10434     case INTEGER_CST:
10435       if (host_integerp (loc, 0))
10436         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10437       else
10438         return 0;
10439       break;
10440
10441     case CONSTRUCTOR:
10442       {
10443         /* Get an RTL for this, if something has been emitted.  */
10444         rtx rtl = lookup_constant_def (loc);
10445         enum machine_mode mode;
10446
10447         if (!rtl || !MEM_P (rtl))
10448           return 0;
10449         mode = GET_MODE (rtl);
10450         rtl = XEXP (rtl, 0);
10451         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10452         have_address = 1;
10453         break;
10454       }
10455
10456     case TRUTH_AND_EXPR:
10457     case TRUTH_ANDIF_EXPR:
10458     case BIT_AND_EXPR:
10459       op = DW_OP_and;
10460       goto do_binop;
10461
10462     case TRUTH_XOR_EXPR:
10463     case BIT_XOR_EXPR:
10464       op = DW_OP_xor;
10465       goto do_binop;
10466
10467     case TRUTH_OR_EXPR:
10468     case TRUTH_ORIF_EXPR:
10469     case BIT_IOR_EXPR:
10470       op = DW_OP_or;
10471       goto do_binop;
10472
10473     case FLOOR_DIV_EXPR:
10474     case CEIL_DIV_EXPR:
10475     case ROUND_DIV_EXPR:
10476     case TRUNC_DIV_EXPR:
10477       op = DW_OP_div;
10478       goto do_binop;
10479
10480     case MINUS_EXPR:
10481       op = DW_OP_minus;
10482       goto do_binop;
10483
10484     case FLOOR_MOD_EXPR:
10485     case CEIL_MOD_EXPR:
10486     case ROUND_MOD_EXPR:
10487     case TRUNC_MOD_EXPR:
10488       op = DW_OP_mod;
10489       goto do_binop;
10490
10491     case MULT_EXPR:
10492       op = DW_OP_mul;
10493       goto do_binop;
10494
10495     case LSHIFT_EXPR:
10496       op = DW_OP_shl;
10497       goto do_binop;
10498
10499     case RSHIFT_EXPR:
10500       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10501       goto do_binop;
10502
10503     case POINTER_PLUS_EXPR:
10504     case PLUS_EXPR:
10505       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10506           && host_integerp (TREE_OPERAND (loc, 1), 0))
10507         {
10508           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10509           if (ret == 0)
10510             return 0;
10511
10512           add_loc_descr (&ret,
10513                          new_loc_descr (DW_OP_plus_uconst,
10514                                         tree_low_cst (TREE_OPERAND (loc, 1),
10515                                                       0),
10516                                         0));
10517           break;
10518         }
10519
10520       op = DW_OP_plus;
10521       goto do_binop;
10522
10523     case LE_EXPR:
10524       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10525         return 0;
10526
10527       op = DW_OP_le;
10528       goto do_binop;
10529
10530     case GE_EXPR:
10531       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10532         return 0;
10533
10534       op = DW_OP_ge;
10535       goto do_binop;
10536
10537     case LT_EXPR:
10538       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10539         return 0;
10540
10541       op = DW_OP_lt;
10542       goto do_binop;
10543
10544     case GT_EXPR:
10545       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10546         return 0;
10547
10548       op = DW_OP_gt;
10549       goto do_binop;
10550
10551     case EQ_EXPR:
10552       op = DW_OP_eq;
10553       goto do_binop;
10554
10555     case NE_EXPR:
10556       op = DW_OP_ne;
10557       goto do_binop;
10558
10559     do_binop:
10560       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10561       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10562       if (ret == 0 || ret1 == 0)
10563         return 0;
10564
10565       add_loc_descr (&ret, ret1);
10566       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10567       break;
10568
10569     case TRUTH_NOT_EXPR:
10570     case BIT_NOT_EXPR:
10571       op = DW_OP_not;
10572       goto do_unop;
10573
10574     case ABS_EXPR:
10575       op = DW_OP_abs;
10576       goto do_unop;
10577
10578     case NEGATE_EXPR:
10579       op = DW_OP_neg;
10580       goto do_unop;
10581
10582     do_unop:
10583       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10584       if (ret == 0)
10585         return 0;
10586
10587       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10588       break;
10589
10590     case MIN_EXPR:
10591     case MAX_EXPR:
10592       {
10593         const enum tree_code code =
10594           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10595
10596         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10597                       build2 (code, integer_type_node,
10598                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10599                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10600       }
10601
10602       /* ... fall through ...  */
10603
10604     case COND_EXPR:
10605       {
10606         dw_loc_descr_ref lhs
10607           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10608         dw_loc_descr_ref rhs
10609           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10610         dw_loc_descr_ref bra_node, jump_node, tmp;
10611
10612         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10613         if (ret == 0 || lhs == 0 || rhs == 0)
10614           return 0;
10615
10616         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10617         add_loc_descr (&ret, bra_node);
10618
10619         add_loc_descr (&ret, rhs);
10620         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10621         add_loc_descr (&ret, jump_node);
10622
10623         add_loc_descr (&ret, lhs);
10624         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10625         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10626
10627         /* ??? Need a node to point the skip at.  Use a nop.  */
10628         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10629         add_loc_descr (&ret, tmp);
10630         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10631         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10632       }
10633       break;
10634
10635     case FIX_TRUNC_EXPR:
10636       return 0;
10637
10638     default:
10639       /* Leave front-end specific codes as simply unknown.  This comes
10640          up, for instance, with the C STMT_EXPR.  */
10641       if ((unsigned int) TREE_CODE (loc)
10642           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10643         return 0;
10644
10645 #ifdef ENABLE_CHECKING
10646       /* Otherwise this is a generic code; we should just lists all of
10647          these explicitly.  We forgot one.  */
10648       gcc_unreachable ();
10649 #else
10650       /* In a release build, we want to degrade gracefully: better to
10651          generate incomplete debugging information than to crash.  */
10652       return NULL;
10653 #endif
10654     }
10655
10656   /* Show if we can't fill the request for an address.  */
10657   if (want_address && !have_address)
10658     return 0;
10659
10660   /* If we've got an address and don't want one, dereference.  */
10661   if (!want_address && have_address && ret)
10662     {
10663       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10664
10665       if (size > DWARF2_ADDR_SIZE || size == -1)
10666         return 0;
10667       else if (size == DWARF2_ADDR_SIZE)
10668         op = DW_OP_deref;
10669       else
10670         op = DW_OP_deref_size;
10671
10672       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10673     }
10674
10675   return ret;
10676 }
10677
10678 static inline dw_loc_descr_ref
10679 loc_descriptor_from_tree (tree loc)
10680 {
10681   return loc_descriptor_from_tree_1 (loc, 2);
10682 }
10683
10684 /* Given a value, round it up to the lowest multiple of `boundary'
10685    which is not less than the value itself.  */
10686
10687 static inline HOST_WIDE_INT
10688 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10689 {
10690   return (((value + boundary - 1) / boundary) * boundary);
10691 }
10692
10693 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10694    pointer to the declared type for the relevant field variable, or return
10695    `integer_type_node' if the given node turns out to be an
10696    ERROR_MARK node.  */
10697
10698 static inline tree
10699 field_type (const_tree decl)
10700 {
10701   tree type;
10702
10703   if (TREE_CODE (decl) == ERROR_MARK)
10704     return integer_type_node;
10705
10706   type = DECL_BIT_FIELD_TYPE (decl);
10707   if (type == NULL_TREE)
10708     type = TREE_TYPE (decl);
10709
10710   return type;
10711 }
10712
10713 /* Given a pointer to a tree node, return the alignment in bits for
10714    it, or else return BITS_PER_WORD if the node actually turns out to
10715    be an ERROR_MARK node.  */
10716
10717 static inline unsigned
10718 simple_type_align_in_bits (const_tree type)
10719 {
10720   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10721 }
10722
10723 static inline unsigned
10724 simple_decl_align_in_bits (const_tree decl)
10725 {
10726   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10727 }
10728
10729 /* Return the result of rounding T up to ALIGN.  */
10730
10731 static inline HOST_WIDE_INT
10732 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10733 {
10734   /* We must be careful if T is negative because HOST_WIDE_INT can be
10735      either "above" or "below" unsigned int as per the C promotion
10736      rules, depending on the host, thus making the signedness of the
10737      direct multiplication and division unpredictable.  */
10738   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10739
10740   u += align - 1;
10741   u /= align;
10742   u *= align;
10743
10744   return (HOST_WIDE_INT) u;
10745 }
10746
10747 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10748    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10749    or return 0 if we are unable to determine what that offset is, either
10750    because the argument turns out to be a pointer to an ERROR_MARK node, or
10751    because the offset is actually variable.  (We can't handle the latter case
10752    just yet).  */
10753
10754 static HOST_WIDE_INT
10755 field_byte_offset (const_tree decl)
10756 {
10757   HOST_WIDE_INT object_offset_in_bits;
10758   HOST_WIDE_INT bitpos_int;
10759
10760   if (TREE_CODE (decl) == ERROR_MARK)
10761     return 0;
10762
10763   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10764
10765   /* We cannot yet cope with fields whose positions are variable, so
10766      for now, when we see such things, we simply return 0.  Someday, we may
10767      be able to handle such cases, but it will be damn difficult.  */
10768   if (! host_integerp (bit_position (decl), 0))
10769     return 0;
10770
10771   bitpos_int = int_bit_position (decl);
10772
10773 #ifdef PCC_BITFIELD_TYPE_MATTERS
10774   if (PCC_BITFIELD_TYPE_MATTERS)
10775     {
10776       tree type;
10777       tree field_size_tree;
10778       HOST_WIDE_INT deepest_bitpos;
10779       unsigned HOST_WIDE_INT field_size_in_bits;
10780       unsigned int type_align_in_bits;
10781       unsigned int decl_align_in_bits;
10782       unsigned HOST_WIDE_INT type_size_in_bits;
10783
10784       type = field_type (decl);
10785       field_size_tree = DECL_SIZE (decl);
10786
10787       /* The size could be unspecified if there was an error, or for
10788          a flexible array member.  */
10789       if (! field_size_tree)
10790         field_size_tree = bitsize_zero_node;
10791
10792       /* If we don't know the size of the field, pretend it's a full word.  */
10793       if (host_integerp (field_size_tree, 1))
10794         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10795       else
10796         field_size_in_bits = BITS_PER_WORD;
10797
10798       type_size_in_bits = simple_type_size_in_bits (type);
10799       type_align_in_bits = simple_type_align_in_bits (type);
10800       decl_align_in_bits = simple_decl_align_in_bits (decl);
10801
10802       /* The GCC front-end doesn't make any attempt to keep track of the
10803          starting bit offset (relative to the start of the containing
10804          structure type) of the hypothetical "containing object" for a
10805          bit-field.  Thus, when computing the byte offset value for the
10806          start of the "containing object" of a bit-field, we must deduce
10807          this information on our own. This can be rather tricky to do in
10808          some cases.  For example, handling the following structure type
10809          definition when compiling for an i386/i486 target (which only
10810          aligns long long's to 32-bit boundaries) can be very tricky:
10811
10812          struct S { int field1; long long field2:31; };
10813
10814          Fortunately, there is a simple rule-of-thumb which can be used
10815          in such cases.  When compiling for an i386/i486, GCC will
10816          allocate 8 bytes for the structure shown above.  It decides to
10817          do this based upon one simple rule for bit-field allocation.
10818          GCC allocates each "containing object" for each bit-field at
10819          the first (i.e. lowest addressed) legitimate alignment boundary
10820          (based upon the required minimum alignment for the declared
10821          type of the field) which it can possibly use, subject to the
10822          condition that there is still enough available space remaining
10823          in the containing object (when allocated at the selected point)
10824          to fully accommodate all of the bits of the bit-field itself.
10825
10826          This simple rule makes it obvious why GCC allocates 8 bytes for
10827          each object of the structure type shown above.  When looking
10828          for a place to allocate the "containing object" for `field2',
10829          the compiler simply tries to allocate a 64-bit "containing
10830          object" at each successive 32-bit boundary (starting at zero)
10831          until it finds a place to allocate that 64- bit field such that
10832          at least 31 contiguous (and previously unallocated) bits remain
10833          within that selected 64 bit field.  (As it turns out, for the
10834          example above, the compiler finds it is OK to allocate the
10835          "containing object" 64-bit field at bit-offset zero within the
10836          structure type.)
10837
10838          Here we attempt to work backwards from the limited set of facts
10839          we're given, and we try to deduce from those facts, where GCC
10840          must have believed that the containing object started (within
10841          the structure type). The value we deduce is then used (by the
10842          callers of this routine) to generate DW_AT_location and
10843          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10844          the case of DW_AT_location, regular fields as well).  */
10845
10846       /* Figure out the bit-distance from the start of the structure to
10847          the "deepest" bit of the bit-field.  */
10848       deepest_bitpos = bitpos_int + field_size_in_bits;
10849
10850       /* This is the tricky part.  Use some fancy footwork to deduce
10851          where the lowest addressed bit of the containing object must
10852          be.  */
10853       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10854
10855       /* Round up to type_align by default.  This works best for
10856          bitfields.  */
10857       object_offset_in_bits
10858         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10859
10860       if (object_offset_in_bits > bitpos_int)
10861         {
10862           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10863
10864           /* Round up to decl_align instead.  */
10865           object_offset_in_bits
10866             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10867         }
10868     }
10869   else
10870 #endif
10871     object_offset_in_bits = bitpos_int;
10872
10873   return object_offset_in_bits / BITS_PER_UNIT;
10874 }
10875 \f
10876 /* The following routines define various Dwarf attributes and any data
10877    associated with them.  */
10878
10879 /* Add a location description attribute value to a DIE.
10880
10881    This emits location attributes suitable for whole variables and
10882    whole parameters.  Note that the location attributes for struct fields are
10883    generated by the routine `data_member_location_attribute' below.  */
10884
10885 static inline void
10886 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10887                              dw_loc_descr_ref descr)
10888 {
10889   if (descr != 0)
10890     add_AT_loc (die, attr_kind, descr);
10891 }
10892
10893 /* Attach the specialized form of location attribute used for data members of
10894    struct and union types.  In the special case of a FIELD_DECL node which
10895    represents a bit-field, the "offset" part of this special location
10896    descriptor must indicate the distance in bytes from the lowest-addressed
10897    byte of the containing struct or union type to the lowest-addressed byte of
10898    the "containing object" for the bit-field.  (See the `field_byte_offset'
10899    function above).
10900
10901    For any given bit-field, the "containing object" is a hypothetical object
10902    (of some integral or enum type) within which the given bit-field lives.  The
10903    type of this hypothetical "containing object" is always the same as the
10904    declared type of the individual bit-field itself (for GCC anyway... the
10905    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10906    bytes) of the hypothetical "containing object" which will be given in the
10907    DW_AT_byte_size attribute for this bit-field.  (See the
10908    `byte_size_attribute' function below.)  It is also used when calculating the
10909    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10910    function below.)  */
10911
10912 static void
10913 add_data_member_location_attribute (dw_die_ref die, tree decl)
10914 {
10915   HOST_WIDE_INT offset;
10916   dw_loc_descr_ref loc_descr = 0;
10917
10918   if (TREE_CODE (decl) == TREE_BINFO)
10919     {
10920       /* We're working on the TAG_inheritance for a base class.  */
10921       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10922         {
10923           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10924              aren't at a fixed offset from all (sub)objects of the same
10925              type.  We need to extract the appropriate offset from our
10926              vtable.  The following dwarf expression means
10927
10928                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10929
10930              This is specific to the V3 ABI, of course.  */
10931
10932           dw_loc_descr_ref tmp;
10933
10934           /* Make a copy of the object address.  */
10935           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10936           add_loc_descr (&loc_descr, tmp);
10937
10938           /* Extract the vtable address.  */
10939           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10940           add_loc_descr (&loc_descr, tmp);
10941
10942           /* Calculate the address of the offset.  */
10943           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10944           gcc_assert (offset < 0);
10945
10946           tmp = int_loc_descriptor (-offset);
10947           add_loc_descr (&loc_descr, tmp);
10948           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10949           add_loc_descr (&loc_descr, tmp);
10950
10951           /* Extract the offset.  */
10952           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10953           add_loc_descr (&loc_descr, tmp);
10954
10955           /* Add it to the object address.  */
10956           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10957           add_loc_descr (&loc_descr, tmp);
10958         }
10959       else
10960         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10961     }
10962   else
10963     offset = field_byte_offset (decl);
10964
10965   if (! loc_descr)
10966     {
10967       enum dwarf_location_atom op;
10968
10969       /* The DWARF2 standard says that we should assume that the structure
10970          address is already on the stack, so we can specify a structure field
10971          address by using DW_OP_plus_uconst.  */
10972
10973 #ifdef MIPS_DEBUGGING_INFO
10974       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10975          operator correctly.  It works only if we leave the offset on the
10976          stack.  */
10977       op = DW_OP_constu;
10978 #else
10979       op = DW_OP_plus_uconst;
10980 #endif
10981
10982       loc_descr = new_loc_descr (op, offset, 0);
10983     }
10984
10985   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10986 }
10987
10988 /* Writes integer values to dw_vec_const array.  */
10989
10990 static void
10991 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10992 {
10993   while (size != 0)
10994     {
10995       *dest++ = val & 0xff;
10996       val >>= 8;
10997       --size;
10998     }
10999 }
11000
11001 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11002
11003 static HOST_WIDE_INT
11004 extract_int (const unsigned char *src, unsigned int size)
11005 {
11006   HOST_WIDE_INT val = 0;
11007
11008   src += size;
11009   while (size != 0)
11010     {
11011       val <<= 8;
11012       val |= *--src & 0xff;
11013       --size;
11014     }
11015   return val;
11016 }
11017
11018 /* Writes floating point values to dw_vec_const array.  */
11019
11020 static void
11021 insert_float (const_rtx rtl, unsigned char *array)
11022 {
11023   REAL_VALUE_TYPE rv;
11024   long val[4];
11025   int i;
11026
11027   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11028   real_to_target (val, &rv, GET_MODE (rtl));
11029
11030   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11031   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11032     {
11033       insert_int (val[i], 4, array);
11034       array += 4;
11035     }
11036 }
11037
11038 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11039    does not have a "location" either in memory or in a register.  These
11040    things can arise in GNU C when a constant is passed as an actual parameter
11041    to an inlined function.  They can also arise in C++ where declared
11042    constants do not necessarily get memory "homes".  */
11043
11044 static void
11045 add_const_value_attribute (dw_die_ref die, rtx rtl)
11046 {
11047   switch (GET_CODE (rtl))
11048     {
11049     case CONST_INT:
11050       {
11051         HOST_WIDE_INT val = INTVAL (rtl);
11052
11053         if (val < 0)
11054           add_AT_int (die, DW_AT_const_value, val);
11055         else
11056           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11057       }
11058       break;
11059
11060     case CONST_DOUBLE:
11061       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11062          floating-point constant.  A CONST_DOUBLE is used whenever the
11063          constant requires more than one word in order to be adequately
11064          represented.  We output CONST_DOUBLEs as blocks.  */
11065       {
11066         enum machine_mode mode = GET_MODE (rtl);
11067
11068         if (SCALAR_FLOAT_MODE_P (mode))
11069           {
11070             unsigned int length = GET_MODE_SIZE (mode);
11071             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11072
11073             insert_float (rtl, array);
11074             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11075           }
11076         else
11077           {
11078             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11079             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11080
11081             add_AT_long_long (die, DW_AT_const_value,
11082                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11083           }
11084       }
11085       break;
11086
11087     case CONST_VECTOR:
11088       {
11089         enum machine_mode mode = GET_MODE (rtl);
11090         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11091         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11092         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11093         unsigned int i;
11094         unsigned char *p;
11095
11096         switch (GET_MODE_CLASS (mode))
11097           {
11098           case MODE_VECTOR_INT:
11099             for (i = 0, p = array; i < length; i++, p += elt_size)
11100               {
11101                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11102                 HOST_WIDE_INT lo, hi;
11103
11104                 switch (GET_CODE (elt))
11105                   {
11106                   case CONST_INT:
11107                     lo = INTVAL (elt);
11108                     hi = -(lo < 0);
11109                     break;
11110
11111                   case CONST_DOUBLE:
11112                     lo = CONST_DOUBLE_LOW (elt);
11113                     hi = CONST_DOUBLE_HIGH (elt);
11114                     break;
11115
11116                   default:
11117                     gcc_unreachable ();
11118                   }
11119
11120                 if (elt_size <= sizeof (HOST_WIDE_INT))
11121                   insert_int (lo, elt_size, p);
11122                 else
11123                   {
11124                     unsigned char *p0 = p;
11125                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11126
11127                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11128                     if (WORDS_BIG_ENDIAN)
11129                       {
11130                         p0 = p1;
11131                         p1 = p;
11132                       }
11133                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11134                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11135                   }
11136               }
11137             break;
11138
11139           case MODE_VECTOR_FLOAT:
11140             for (i = 0, p = array; i < length; i++, p += elt_size)
11141               {
11142                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11143                 insert_float (elt, p);
11144               }
11145             break;
11146
11147           default:
11148             gcc_unreachable ();
11149           }
11150
11151         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11152       }
11153       break;
11154
11155     case CONST_STRING:
11156       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11157       break;
11158
11159     case SYMBOL_REF:
11160     case LABEL_REF:
11161     case CONST:
11162       add_AT_addr (die, DW_AT_const_value, rtl);
11163       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11164       break;
11165
11166     case PLUS:
11167       /* In cases where an inlined instance of an inline function is passed
11168          the address of an `auto' variable (which is local to the caller) we
11169          can get a situation where the DECL_RTL of the artificial local
11170          variable (for the inlining) which acts as a stand-in for the
11171          corresponding formal parameter (of the inline function) will look
11172          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11173          exactly a compile-time constant expression, but it isn't the address
11174          of the (artificial) local variable either.  Rather, it represents the
11175          *value* which the artificial local variable always has during its
11176          lifetime.  We currently have no way to represent such quasi-constant
11177          values in Dwarf, so for now we just punt and generate nothing.  */
11178       break;
11179
11180     default:
11181       /* No other kinds of rtx should be possible here.  */
11182       gcc_unreachable ();
11183     }
11184
11185 }
11186
11187 /* Determine whether the evaluation of EXPR references any variables
11188    or functions which aren't otherwise used (and therefore may not be
11189    output).  */
11190 static tree
11191 reference_to_unused (tree * tp, int * walk_subtrees,
11192                      void * data ATTRIBUTE_UNUSED)
11193 {
11194   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11195     *walk_subtrees = 0;
11196
11197   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11198       && ! TREE_ASM_WRITTEN (*tp))
11199     return *tp;
11200   /* ???  The C++ FE emits debug information for using decls, so
11201      putting gcc_unreachable here falls over.  See PR31899.  For now
11202      be conservative.  */
11203   else if (!cgraph_global_info_ready
11204            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11205     return *tp;
11206   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11207     {
11208       struct varpool_node *node = varpool_node (*tp);
11209       if (!node->needed)
11210         return *tp;
11211     }
11212   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11213            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11214     {
11215       struct cgraph_node *node = cgraph_node (*tp);
11216       if (!node->output)
11217         return *tp;
11218     }
11219   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11220     return *tp;
11221
11222   return NULL_TREE;
11223 }
11224
11225 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11226    for use in a later add_const_value_attribute call.  */
11227
11228 static rtx
11229 rtl_for_decl_init (tree init, tree type)
11230 {
11231   rtx rtl = NULL_RTX;
11232
11233   /* If a variable is initialized with a string constant without embedded
11234      zeros, build CONST_STRING.  */
11235   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11236     {
11237       tree enttype = TREE_TYPE (type);
11238       tree domain = TYPE_DOMAIN (type);
11239       enum machine_mode mode = TYPE_MODE (enttype);
11240
11241       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11242           && domain
11243           && integer_zerop (TYPE_MIN_VALUE (domain))
11244           && compare_tree_int (TYPE_MAX_VALUE (domain),
11245                                TREE_STRING_LENGTH (init) - 1) == 0
11246           && ((size_t) TREE_STRING_LENGTH (init)
11247               == strlen (TREE_STRING_POINTER (init)) + 1))
11248         rtl = gen_rtx_CONST_STRING (VOIDmode,
11249                                     ggc_strdup (TREE_STRING_POINTER (init)));
11250     }
11251   /* Other aggregates, and complex values, could be represented using
11252      CONCAT: FIXME!  */
11253   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11254     ;
11255   /* Vectors only work if their mode is supported by the target.
11256      FIXME: generic vectors ought to work too.  */
11257   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11258     ;
11259   /* If the initializer is something that we know will expand into an
11260      immediate RTL constant, expand it now.  We must be careful not to
11261      reference variables which won't be output.  */
11262   else if (initializer_constant_valid_p (init, type)
11263            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11264     {
11265       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11266          possible.  */
11267       if (TREE_CODE (type) == VECTOR_TYPE)
11268         switch (TREE_CODE (init))
11269           {
11270           case VECTOR_CST:
11271             break;
11272           case CONSTRUCTOR:
11273             if (TREE_CONSTANT (init))
11274               {
11275                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11276                 bool constant_p = true;
11277                 tree value;
11278                 unsigned HOST_WIDE_INT ix;
11279
11280                 /* Even when ctor is constant, it might contain non-*_CST
11281                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11282                    belong into VECTOR_CST nodes.  */
11283                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11284                   if (!CONSTANT_CLASS_P (value))
11285                     {
11286                       constant_p = false;
11287                       break;
11288                     }
11289
11290                 if (constant_p)
11291                   {
11292                     init = build_vector_from_ctor (type, elts);
11293                     break;
11294                   }
11295               }
11296             /* FALLTHRU */
11297
11298           default:
11299             return NULL;
11300           }
11301
11302       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11303
11304       /* If expand_expr returns a MEM, it wasn't immediate.  */
11305       gcc_assert (!rtl || !MEM_P (rtl));
11306     }
11307
11308   return rtl;
11309 }
11310
11311 /* Generate RTL for the variable DECL to represent its location.  */
11312
11313 static rtx
11314 rtl_for_decl_location (tree decl)
11315 {
11316   rtx rtl;
11317
11318   /* Here we have to decide where we are going to say the parameter "lives"
11319      (as far as the debugger is concerned).  We only have a couple of
11320      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11321
11322      DECL_RTL normally indicates where the parameter lives during most of the
11323      activation of the function.  If optimization is enabled however, this
11324      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11325      that the parameter doesn't really live anywhere (as far as the code
11326      generation parts of GCC are concerned) during most of the function's
11327      activation.  That will happen (for example) if the parameter is never
11328      referenced within the function.
11329
11330      We could just generate a location descriptor here for all non-NULL
11331      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11332      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11333      where DECL_RTL is NULL or is a pseudo-reg.
11334
11335      Note however that we can only get away with using DECL_INCOMING_RTL as
11336      a backup substitute for DECL_RTL in certain limited cases.  In cases
11337      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11338      we can be sure that the parameter was passed using the same type as it is
11339      declared to have within the function, and that its DECL_INCOMING_RTL
11340      points us to a place where a value of that type is passed.
11341
11342      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11343      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11344      because in these cases DECL_INCOMING_RTL points us to a value of some
11345      type which is *different* from the type of the parameter itself.  Thus,
11346      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11347      such cases, the debugger would end up (for example) trying to fetch a
11348      `float' from a place which actually contains the first part of a
11349      `double'.  That would lead to really incorrect and confusing
11350      output at debug-time.
11351
11352      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11353      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11354      are a couple of exceptions however.  On little-endian machines we can
11355      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11356      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11357      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11358      when (on a little-endian machine) a non-prototyped function has a
11359      parameter declared to be of type `short' or `char'.  In such cases,
11360      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11361      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11362      passed `int' value.  If the debugger then uses that address to fetch
11363      a `short' or a `char' (on a little-endian machine) the result will be
11364      the correct data, so we allow for such exceptional cases below.
11365
11366      Note that our goal here is to describe the place where the given formal
11367      parameter lives during most of the function's activation (i.e. between the
11368      end of the prologue and the start of the epilogue).  We'll do that as best
11369      as we can. Note however that if the given formal parameter is modified
11370      sometime during the execution of the function, then a stack backtrace (at
11371      debug-time) will show the function as having been called with the *new*
11372      value rather than the value which was originally passed in.  This happens
11373      rarely enough that it is not a major problem, but it *is* a problem, and
11374      I'd like to fix it.
11375
11376      A future version of dwarf2out.c may generate two additional attributes for
11377      any given DW_TAG_formal_parameter DIE which will describe the "passed
11378      type" and the "passed location" for the given formal parameter in addition
11379      to the attributes we now generate to indicate the "declared type" and the
11380      "active location" for each parameter.  This additional set of attributes
11381      could be used by debuggers for stack backtraces. Separately, note that
11382      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11383      This happens (for example) for inlined-instances of inline function formal
11384      parameters which are never referenced.  This really shouldn't be
11385      happening.  All PARM_DECL nodes should get valid non-NULL
11386      DECL_INCOMING_RTL values.  FIXME.  */
11387
11388   /* Use DECL_RTL as the "location" unless we find something better.  */
11389   rtl = DECL_RTL_IF_SET (decl);
11390
11391   /* When generating abstract instances, ignore everything except
11392      constants, symbols living in memory, and symbols living in
11393      fixed registers.  */
11394   if (! reload_completed)
11395     {
11396       if (rtl
11397           && (CONSTANT_P (rtl)
11398               || (MEM_P (rtl)
11399                   && CONSTANT_P (XEXP (rtl, 0)))
11400               || (REG_P (rtl)
11401                   && TREE_CODE (decl) == VAR_DECL
11402                   && TREE_STATIC (decl))))
11403         {
11404           rtl = targetm.delegitimize_address (rtl);
11405           return rtl;
11406         }
11407       rtl = NULL_RTX;
11408     }
11409   else if (TREE_CODE (decl) == PARM_DECL)
11410     {
11411       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11412         {
11413           tree declared_type = TREE_TYPE (decl);
11414           tree passed_type = DECL_ARG_TYPE (decl);
11415           enum machine_mode dmode = TYPE_MODE (declared_type);
11416           enum machine_mode pmode = TYPE_MODE (passed_type);
11417
11418           /* This decl represents a formal parameter which was optimized out.
11419              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11420              all cases where (rtl == NULL_RTX) just below.  */
11421           if (dmode == pmode)
11422             rtl = DECL_INCOMING_RTL (decl);
11423           else if (SCALAR_INT_MODE_P (dmode)
11424                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11425                    && DECL_INCOMING_RTL (decl))
11426             {
11427               rtx inc = DECL_INCOMING_RTL (decl);
11428               if (REG_P (inc))
11429                 rtl = inc;
11430               else if (MEM_P (inc))
11431                 {
11432                   if (BYTES_BIG_ENDIAN)
11433                     rtl = adjust_address_nv (inc, dmode,
11434                                              GET_MODE_SIZE (pmode)
11435                                              - GET_MODE_SIZE (dmode));
11436                   else
11437                     rtl = inc;
11438                 }
11439             }
11440         }
11441
11442       /* If the parm was passed in registers, but lives on the stack, then
11443          make a big endian correction if the mode of the type of the
11444          parameter is not the same as the mode of the rtl.  */
11445       /* ??? This is the same series of checks that are made in dbxout.c before
11446          we reach the big endian correction code there.  It isn't clear if all
11447          of these checks are necessary here, but keeping them all is the safe
11448          thing to do.  */
11449       else if (MEM_P (rtl)
11450                && XEXP (rtl, 0) != const0_rtx
11451                && ! CONSTANT_P (XEXP (rtl, 0))
11452                /* Not passed in memory.  */
11453                && !MEM_P (DECL_INCOMING_RTL (decl))
11454                /* Not passed by invisible reference.  */
11455                && (!REG_P (XEXP (rtl, 0))
11456                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11457                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11458 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11459                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11460 #endif
11461                      )
11462                /* Big endian correction check.  */
11463                && BYTES_BIG_ENDIAN
11464                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11465                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11466                    < UNITS_PER_WORD))
11467         {
11468           int offset = (UNITS_PER_WORD
11469                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11470
11471           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11472                              plus_constant (XEXP (rtl, 0), offset));
11473         }
11474     }
11475   else if (TREE_CODE (decl) == VAR_DECL
11476            && rtl
11477            && MEM_P (rtl)
11478            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11479            && BYTES_BIG_ENDIAN)
11480     {
11481       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11482       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11483
11484       /* If a variable is declared "register" yet is smaller than
11485          a register, then if we store the variable to memory, it
11486          looks like we're storing a register-sized value, when in
11487          fact we are not.  We need to adjust the offset of the
11488          storage location to reflect the actual value's bytes,
11489          else gdb will not be able to display it.  */
11490       if (rsize > dsize)
11491         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11492                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11493     }
11494
11495   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11496      and will have been substituted directly into all expressions that use it.
11497      C does not have such a concept, but C++ and other languages do.  */
11498   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11499     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11500
11501   if (rtl)
11502     rtl = targetm.delegitimize_address (rtl);
11503
11504   /* If we don't look past the constant pool, we risk emitting a
11505      reference to a constant pool entry that isn't referenced from
11506      code, and thus is not emitted.  */
11507   if (rtl)
11508     rtl = avoid_constant_pool_reference (rtl);
11509
11510   return rtl;
11511 }
11512
11513 /* We need to figure out what section we should use as the base for the
11514    address ranges where a given location is valid.
11515    1. If this particular DECL has a section associated with it, use that.
11516    2. If this function has a section associated with it, use that.
11517    3. Otherwise, use the text section.
11518    XXX: If you split a variable across multiple sections, we won't notice.  */
11519
11520 static const char *
11521 secname_for_decl (const_tree decl)
11522 {
11523   const char *secname;
11524
11525   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11526     {
11527       tree sectree = DECL_SECTION_NAME (decl);
11528       secname = TREE_STRING_POINTER (sectree);
11529     }
11530   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11531     {
11532       tree sectree = DECL_SECTION_NAME (current_function_decl);
11533       secname = TREE_STRING_POINTER (sectree);
11534     }
11535   else if (cfun && in_cold_section_p)
11536     secname = crtl->subsections.cold_section_label;
11537   else
11538     secname = text_section_label;
11539
11540   return secname;
11541 }
11542
11543 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_RTX is returned.
11544    If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
11545    value is the offset into the common block for the symbol.  */
11546
11547 static tree
11548 fortran_common (tree decl, HOST_WIDE_INT *value)
11549 {
11550   tree val_expr, cvar;
11551   enum machine_mode mode;
11552   HOST_WIDE_INT bitsize, bitpos;
11553   tree offset;
11554   int volatilep = 0, unsignedp = 0;
11555
11556   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11557      it does not have a value (the offset into the common area), or if it
11558      is thread local (as opposed to global) then it isn't common, and shouldn't
11559      be handled as such.  */
11560   if (TREE_CODE (decl) != VAR_DECL
11561       || !TREE_PUBLIC (decl)
11562       || !TREE_STATIC (decl)
11563       || !DECL_HAS_VALUE_EXPR_P (decl)
11564       || !is_fortran ())
11565     return NULL_TREE;
11566
11567   val_expr = DECL_VALUE_EXPR (decl);
11568   if (TREE_CODE (val_expr) != COMPONENT_REF)
11569     return NULL_TREE;
11570
11571   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11572                               &mode, &unsignedp, &volatilep, true);
11573
11574   if (cvar == NULL_TREE
11575       || TREE_CODE (cvar) != VAR_DECL
11576       || DECL_ARTIFICIAL (cvar)
11577       || !TREE_PUBLIC (cvar))
11578     return NULL_TREE;
11579
11580   *value = 0;
11581   if (offset != NULL)
11582     {
11583       if (!host_integerp (offset, 0))
11584         return NULL_TREE;
11585       *value = tree_low_cst (offset, 0);
11586     }
11587   if (bitpos != 0)
11588     *value += bitpos / BITS_PER_UNIT;
11589
11590   return cvar;
11591 }
11592
11593
11594 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11595    data attribute for a variable or a parameter.  We generate the
11596    DW_AT_const_value attribute only in those cases where the given variable
11597    or parameter does not have a true "location" either in memory or in a
11598    register.  This can happen (for example) when a constant is passed as an
11599    actual argument in a call to an inline function.  (It's possible that
11600    these things can crop up in other ways also.)  Note that one type of
11601    constant value which can be passed into an inlined function is a constant
11602    pointer.  This can happen for example if an actual argument in an inlined
11603    function call evaluates to a compile-time constant address.  */
11604
11605 static void
11606 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11607                                        enum dwarf_attribute attr)
11608 {
11609   rtx rtl;
11610   dw_loc_descr_ref descr;
11611   var_loc_list *loc_list;
11612   struct var_loc_node *node;
11613   if (TREE_CODE (decl) == ERROR_MARK)
11614     return;
11615
11616   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11617               || TREE_CODE (decl) == RESULT_DECL);
11618
11619   /* See if we possibly have multiple locations for this variable.  */
11620   loc_list = lookup_decl_loc (decl);
11621
11622   /* If it truly has multiple locations, the first and last node will
11623      differ.  */
11624   if (loc_list && loc_list->first != loc_list->last)
11625     {
11626       const char *endname, *secname;
11627       dw_loc_list_ref list;
11628       rtx varloc;
11629       enum var_init_status initialized;
11630
11631       /* Now that we know what section we are using for a base,
11632          actually construct the list of locations.
11633          The first location information is what is passed to the
11634          function that creates the location list, and the remaining
11635          locations just get added on to that list.
11636          Note that we only know the start address for a location
11637          (IE location changes), so to build the range, we use
11638          the range [current location start, next location start].
11639          This means we have to special case the last node, and generate
11640          a range of [last location start, end of function label].  */
11641
11642       node = loc_list->first;
11643       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11644       secname = secname_for_decl (decl);
11645
11646       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11647         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11648       else
11649         initialized = VAR_INIT_STATUS_INITIALIZED;
11650
11651       list = new_loc_list (loc_descriptor (varloc, initialized),
11652                            node->label, node->next->label, secname, 1);
11653       node = node->next;
11654
11655       for (; node->next; node = node->next)
11656         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11657           {
11658             /* The variable has a location between NODE->LABEL and
11659                NODE->NEXT->LABEL.  */
11660             enum var_init_status initialized =
11661               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11662             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11663             add_loc_descr_to_loc_list (&list,
11664                                        loc_descriptor (varloc, initialized),
11665                                        node->label, node->next->label, secname);
11666           }
11667
11668       /* If the variable has a location at the last label
11669          it keeps its location until the end of function.  */
11670       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11671         {
11672           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11673           enum var_init_status initialized =
11674             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11675
11676           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11677           if (!current_function_decl)
11678             endname = text_end_label;
11679           else
11680             {
11681               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11682                                            current_function_funcdef_no);
11683               endname = ggc_strdup (label_id);
11684             }
11685           add_loc_descr_to_loc_list (&list,
11686                                      loc_descriptor (varloc, initialized),
11687                                      node->label, endname, secname);
11688         }
11689
11690       /* Finally, add the location list to the DIE, and we are done.  */
11691       add_AT_loc_list (die, attr, list);
11692       return;
11693     }
11694
11695   /* Try to get some constant RTL for this decl, and use that as the value of
11696      the location.  */
11697
11698   rtl = rtl_for_decl_location (decl);
11699   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11700     {
11701       add_const_value_attribute (die, rtl);
11702       return;
11703     }
11704
11705   /* If we have tried to generate the location otherwise, and it
11706      didn't work out (we wouldn't be here if we did), and we have a one entry
11707      location list, try generating a location from that.  */
11708   if (loc_list && loc_list->first)
11709     {
11710       enum var_init_status status;
11711       node = loc_list->first;
11712       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11713       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11714       if (descr)
11715         {
11716           add_AT_location_description (die, attr, descr);
11717           return;
11718         }
11719     }
11720
11721   /* We couldn't get any rtl, so try directly generating the location
11722      description from the tree.  */
11723   descr = loc_descriptor_from_tree (decl);
11724   if (descr)
11725     {
11726       add_AT_location_description (die, attr, descr);
11727       return;
11728     }
11729   /* None of that worked, so it must not really have a location;
11730      try adding a constant value attribute from the DECL_INITIAL.  */
11731   tree_add_const_value_attribute (die, decl);
11732 }
11733
11734 /* If we don't have a copy of this variable in memory for some reason (such
11735    as a C++ member constant that doesn't have an out-of-line definition),
11736    we should tell the debugger about the constant value.  */
11737
11738 static void
11739 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
11740 {
11741   tree init = DECL_INITIAL (decl);
11742   tree type = TREE_TYPE (decl);
11743   rtx rtl;
11744
11745   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
11746     /* OK */;
11747   else
11748     return;
11749
11750   rtl = rtl_for_decl_init (init, type);
11751   if (rtl)
11752     add_const_value_attribute (var_die, rtl);
11753 }
11754
11755 /* Convert the CFI instructions for the current function into a
11756    location list.  This is used for DW_AT_frame_base when we targeting
11757    a dwarf2 consumer that does not support the dwarf3
11758    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
11759    expressions.  */
11760
11761 static dw_loc_list_ref
11762 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
11763 {
11764   dw_fde_ref fde;
11765   dw_loc_list_ref list, *list_tail;
11766   dw_cfi_ref cfi;
11767   dw_cfa_location last_cfa, next_cfa;
11768   const char *start_label, *last_label, *section;
11769
11770   fde = current_fde ();
11771   gcc_assert (fde != NULL);
11772
11773   section = secname_for_decl (current_function_decl);
11774   list_tail = &list;
11775   list = NULL;
11776
11777   next_cfa.reg = INVALID_REGNUM;
11778   next_cfa.offset = 0;
11779   next_cfa.indirect = 0;
11780   next_cfa.base_offset = 0;
11781
11782   start_label = fde->dw_fde_begin;
11783
11784   /* ??? Bald assumption that the CIE opcode list does not contain
11785      advance opcodes.  */
11786   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11787     lookup_cfa_1 (cfi, &next_cfa);
11788
11789   last_cfa = next_cfa;
11790   last_label = start_label;
11791
11792   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11793     switch (cfi->dw_cfi_opc)
11794       {
11795       case DW_CFA_set_loc:
11796       case DW_CFA_advance_loc1:
11797       case DW_CFA_advance_loc2:
11798       case DW_CFA_advance_loc4:
11799         if (!cfa_equal_p (&last_cfa, &next_cfa))
11800           {
11801             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11802                                        start_label, last_label, section,
11803                                        list == NULL);
11804
11805             list_tail = &(*list_tail)->dw_loc_next;
11806             last_cfa = next_cfa;
11807             start_label = last_label;
11808           }
11809         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11810         break;
11811
11812       case DW_CFA_advance_loc:
11813         /* The encoding is complex enough that we should never emit this.  */
11814       case DW_CFA_remember_state:
11815       case DW_CFA_restore_state:
11816         /* We don't handle these two in this function.  It would be possible
11817            if it were to be required.  */
11818         gcc_unreachable ();
11819
11820       default:
11821         lookup_cfa_1 (cfi, &next_cfa);
11822         break;
11823       }
11824
11825   if (!cfa_equal_p (&last_cfa, &next_cfa))
11826     {
11827       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11828                                  start_label, last_label, section,
11829                                  list == NULL);
11830       list_tail = &(*list_tail)->dw_loc_next;
11831       start_label = last_label;
11832     }
11833   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11834                              start_label, fde->dw_fde_end, section,
11835                              list == NULL);
11836
11837   return list;
11838 }
11839
11840 /* Compute a displacement from the "steady-state frame pointer" to the
11841    frame base (often the same as the CFA), and store it in
11842    frame_pointer_fb_offset.  OFFSET is added to the displacement
11843    before the latter is negated.  */
11844
11845 static void
11846 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11847 {
11848   rtx reg, elim;
11849
11850 #ifdef FRAME_POINTER_CFA_OFFSET
11851   reg = frame_pointer_rtx;
11852   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11853 #else
11854   reg = arg_pointer_rtx;
11855   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11856 #endif
11857
11858   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11859   if (GET_CODE (elim) == PLUS)
11860     {
11861       offset += INTVAL (XEXP (elim, 1));
11862       elim = XEXP (elim, 0);
11863     }
11864
11865   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11866                && (elim == hard_frame_pointer_rtx
11867                    || elim == stack_pointer_rtx))
11868               || elim == (frame_pointer_needed
11869                           ? hard_frame_pointer_rtx
11870                           : stack_pointer_rtx));
11871
11872   frame_pointer_fb_offset = -offset;
11873 }
11874
11875 /* Generate a DW_AT_name attribute given some string value to be included as
11876    the value of the attribute.  */
11877
11878 static void
11879 add_name_attribute (dw_die_ref die, const char *name_string)
11880 {
11881   if (name_string != NULL && *name_string != 0)
11882     {
11883       if (demangle_name_func)
11884         name_string = (*demangle_name_func) (name_string);
11885
11886       add_AT_string (die, DW_AT_name, name_string);
11887     }
11888 }
11889
11890 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11891
11892 static void
11893 add_comp_dir_attribute (dw_die_ref die)
11894 {
11895   const char *wd = get_src_pwd ();
11896   if (wd != NULL)
11897     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11898 }
11899
11900 /* Given a tree node describing an array bound (either lower or upper) output
11901    a representation for that bound.  */
11902
11903 static void
11904 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11905 {
11906   switch (TREE_CODE (bound))
11907     {
11908     case ERROR_MARK:
11909       return;
11910
11911     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11912     case INTEGER_CST:
11913       if (! host_integerp (bound, 0)
11914           || (bound_attr == DW_AT_lower_bound
11915               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11916                   || (is_fortran () && integer_onep (bound)))))
11917         /* Use the default.  */
11918         ;
11919       else
11920         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11921       break;
11922
11923     CASE_CONVERT:
11924     case VIEW_CONVERT_EXPR:
11925       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11926       break;
11927
11928     case SAVE_EXPR:
11929       break;
11930
11931     case VAR_DECL:
11932     case PARM_DECL:
11933     case RESULT_DECL:
11934       {
11935         dw_die_ref decl_die = lookup_decl_die (bound);
11936
11937         /* ??? Can this happen, or should the variable have been bound
11938            first?  Probably it can, since I imagine that we try to create
11939            the types of parameters in the order in which they exist in
11940            the list, and won't have created a forward reference to a
11941            later parameter.  */
11942         if (decl_die != NULL)
11943           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11944         break;
11945       }
11946
11947     default:
11948       {
11949         /* Otherwise try to create a stack operation procedure to
11950            evaluate the value of the array bound.  */
11951
11952         dw_die_ref ctx, decl_die;
11953         dw_loc_descr_ref loc;
11954
11955         loc = loc_descriptor_from_tree (bound);
11956         if (loc == NULL)
11957           break;
11958
11959         if (current_function_decl == 0)
11960           ctx = comp_unit_die;
11961         else
11962           ctx = lookup_decl_die (current_function_decl);
11963
11964         decl_die = new_die (DW_TAG_variable, ctx, bound);
11965         add_AT_flag (decl_die, DW_AT_artificial, 1);
11966         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11967         add_AT_loc (decl_die, DW_AT_location, loc);
11968
11969         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11970         break;
11971       }
11972     }
11973 }
11974
11975 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
11976    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
11977    Note that the block of subscript information for an array type also
11978    includes information about the element type of the given array type.  */
11979
11980 static void
11981 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
11982 {
11983   unsigned dimension_number;
11984   tree lower, upper;
11985   dw_die_ref subrange_die;
11986
11987   for (dimension_number = 0;
11988        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
11989        type = TREE_TYPE (type), dimension_number++)
11990     {
11991       tree domain = TYPE_DOMAIN (type);
11992
11993       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11994          and (in GNU C only) variable bounds.  Handle all three forms
11995          here.  */
11996       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11997       if (domain)
11998         {
11999           /* We have an array type with specified bounds.  */
12000           lower = TYPE_MIN_VALUE (domain);
12001           upper = TYPE_MAX_VALUE (domain);
12002
12003           /* Define the index type.  */
12004           if (TREE_TYPE (domain))
12005             {
12006               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12007                  TREE_TYPE field.  We can't emit debug info for this
12008                  because it is an unnamed integral type.  */
12009               if (TREE_CODE (domain) == INTEGER_TYPE
12010                   && TYPE_NAME (domain) == NULL_TREE
12011                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12012                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12013                 ;
12014               else
12015                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12016                                     type_die);
12017             }
12018
12019           /* ??? If upper is NULL, the array has unspecified length,
12020              but it does have a lower bound.  This happens with Fortran
12021                dimension arr(N:*)
12022              Since the debugger is definitely going to need to know N
12023              to produce useful results, go ahead and output the lower
12024              bound solo, and hope the debugger can cope.  */
12025
12026           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12027           if (upper)
12028             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12029         }
12030
12031       /* Otherwise we have an array type with an unspecified length.  The
12032          DWARF-2 spec does not say how to handle this; let's just leave out the
12033          bounds.  */
12034     }
12035 }
12036
12037 static void
12038 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12039 {
12040   unsigned size;
12041
12042   switch (TREE_CODE (tree_node))
12043     {
12044     case ERROR_MARK:
12045       size = 0;
12046       break;
12047     case ENUMERAL_TYPE:
12048     case RECORD_TYPE:
12049     case UNION_TYPE:
12050     case QUAL_UNION_TYPE:
12051       size = int_size_in_bytes (tree_node);
12052       break;
12053     case FIELD_DECL:
12054       /* For a data member of a struct or union, the DW_AT_byte_size is
12055          generally given as the number of bytes normally allocated for an
12056          object of the *declared* type of the member itself.  This is true
12057          even for bit-fields.  */
12058       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12059       break;
12060     default:
12061       gcc_unreachable ();
12062     }
12063
12064   /* Note that `size' might be -1 when we get to this point.  If it is, that
12065      indicates that the byte size of the entity in question is variable.  We
12066      have no good way of expressing this fact in Dwarf at the present time,
12067      so just let the -1 pass on through.  */
12068   add_AT_unsigned (die, DW_AT_byte_size, size);
12069 }
12070
12071 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12072    which specifies the distance in bits from the highest order bit of the
12073    "containing object" for the bit-field to the highest order bit of the
12074    bit-field itself.
12075
12076    For any given bit-field, the "containing object" is a hypothetical object
12077    (of some integral or enum type) within which the given bit-field lives.  The
12078    type of this hypothetical "containing object" is always the same as the
12079    declared type of the individual bit-field itself.  The determination of the
12080    exact location of the "containing object" for a bit-field is rather
12081    complicated.  It's handled by the `field_byte_offset' function (above).
12082
12083    Note that it is the size (in bytes) of the hypothetical "containing object"
12084    which will be given in the DW_AT_byte_size attribute for this bit-field.
12085    (See `byte_size_attribute' above).  */
12086
12087 static inline void
12088 add_bit_offset_attribute (dw_die_ref die, tree decl)
12089 {
12090   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12091   tree type = DECL_BIT_FIELD_TYPE (decl);
12092   HOST_WIDE_INT bitpos_int;
12093   HOST_WIDE_INT highest_order_object_bit_offset;
12094   HOST_WIDE_INT highest_order_field_bit_offset;
12095   HOST_WIDE_INT unsigned bit_offset;
12096
12097   /* Must be a field and a bit field.  */
12098   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12099
12100   /* We can't yet handle bit-fields whose offsets are variable, so if we
12101      encounter such things, just return without generating any attribute
12102      whatsoever.  Likewise for variable or too large size.  */
12103   if (! host_integerp (bit_position (decl), 0)
12104       || ! host_integerp (DECL_SIZE (decl), 1))
12105     return;
12106
12107   bitpos_int = int_bit_position (decl);
12108
12109   /* Note that the bit offset is always the distance (in bits) from the
12110      highest-order bit of the "containing object" to the highest-order bit of
12111      the bit-field itself.  Since the "high-order end" of any object or field
12112      is different on big-endian and little-endian machines, the computation
12113      below must take account of these differences.  */
12114   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12115   highest_order_field_bit_offset = bitpos_int;
12116
12117   if (! BYTES_BIG_ENDIAN)
12118     {
12119       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12120       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12121     }
12122
12123   bit_offset
12124     = (! BYTES_BIG_ENDIAN
12125        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12126        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12127
12128   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12129 }
12130
12131 /* For a FIELD_DECL node which represents a bit field, output an attribute
12132    which specifies the length in bits of the given field.  */
12133
12134 static inline void
12135 add_bit_size_attribute (dw_die_ref die, tree decl)
12136 {
12137   /* Must be a field and a bit field.  */
12138   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12139               && DECL_BIT_FIELD_TYPE (decl));
12140
12141   if (host_integerp (DECL_SIZE (decl), 1))
12142     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12143 }
12144
12145 /* If the compiled language is ANSI C, then add a 'prototyped'
12146    attribute, if arg types are given for the parameters of a function.  */
12147
12148 static inline void
12149 add_prototyped_attribute (dw_die_ref die, tree func_type)
12150 {
12151   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12152       && TYPE_ARG_TYPES (func_type) != NULL)
12153     add_AT_flag (die, DW_AT_prototyped, 1);
12154 }
12155
12156 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12157    by looking in either the type declaration or object declaration
12158    equate table.  */
12159
12160 static inline void
12161 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12162 {
12163   dw_die_ref origin_die = NULL;
12164
12165   if (TREE_CODE (origin) != FUNCTION_DECL)
12166     {
12167       /* We may have gotten separated from the block for the inlined
12168          function, if we're in an exception handler or some such; make
12169          sure that the abstract function has been written out.
12170
12171          Doing this for nested functions is wrong, however; functions are
12172          distinct units, and our context might not even be inline.  */
12173       tree fn = origin;
12174
12175       if (TYPE_P (fn))
12176         fn = TYPE_STUB_DECL (fn);
12177
12178       fn = decl_function_context (fn);
12179       if (fn)
12180         dwarf2out_abstract_function (fn);
12181     }
12182
12183   if (DECL_P (origin))
12184     origin_die = lookup_decl_die (origin);
12185   else if (TYPE_P (origin))
12186     origin_die = lookup_type_die (origin);
12187
12188   /* XXX: Functions that are never lowered don't always have correct block
12189      trees (in the case of java, they simply have no block tree, in some other
12190      languages).  For these functions, there is nothing we can really do to
12191      output correct debug info for inlined functions in all cases.  Rather
12192      than die, we'll just produce deficient debug info now, in that we will
12193      have variables without a proper abstract origin.  In the future, when all
12194      functions are lowered, we should re-add a gcc_assert (origin_die)
12195      here.  */
12196
12197   if (origin_die)
12198       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12199 }
12200
12201 /* We do not currently support the pure_virtual attribute.  */
12202
12203 static inline void
12204 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12205 {
12206   if (DECL_VINDEX (func_decl))
12207     {
12208       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12209
12210       if (host_integerp (DECL_VINDEX (func_decl), 0))
12211         add_AT_loc (die, DW_AT_vtable_elem_location,
12212                     new_loc_descr (DW_OP_constu,
12213                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12214                                    0));
12215
12216       /* GNU extension: Record what type this method came from originally.  */
12217       if (debug_info_level > DINFO_LEVEL_TERSE)
12218         add_AT_die_ref (die, DW_AT_containing_type,
12219                         lookup_type_die (DECL_CONTEXT (func_decl)));
12220     }
12221 }
12222 \f
12223 /* Add source coordinate attributes for the given decl.  */
12224
12225 static void
12226 add_src_coords_attributes (dw_die_ref die, tree decl)
12227 {
12228   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12229
12230   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12231   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12232 }
12233
12234 /* Add a DW_AT_name attribute and source coordinate attribute for the
12235    given decl, but only if it actually has a name.  */
12236
12237 static void
12238 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12239 {
12240   tree decl_name;
12241
12242   decl_name = DECL_NAME (decl);
12243   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12244     {
12245       add_name_attribute (die, dwarf2_name (decl, 0));
12246       if (! DECL_ARTIFICIAL (decl))
12247         add_src_coords_attributes (die, decl);
12248
12249       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12250           && TREE_PUBLIC (decl)
12251           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12252           && !DECL_ABSTRACT (decl)
12253           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12254           && !is_fortran ())
12255         add_AT_string (die, DW_AT_MIPS_linkage_name,
12256                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12257     }
12258
12259 #ifdef VMS_DEBUGGING_INFO
12260   /* Get the function's name, as described by its RTL.  This may be different
12261      from the DECL_NAME name used in the source file.  */
12262   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12263     {
12264       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12265                    XEXP (DECL_RTL (decl), 0));
12266       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12267     }
12268 #endif
12269 }
12270
12271 /* Push a new declaration scope.  */
12272
12273 static void
12274 push_decl_scope (tree scope)
12275 {
12276   VEC_safe_push (tree, gc, decl_scope_table, scope);
12277 }
12278
12279 /* Pop a declaration scope.  */
12280
12281 static inline void
12282 pop_decl_scope (void)
12283 {
12284   VEC_pop (tree, decl_scope_table);
12285 }
12286
12287 /* Return the DIE for the scope that immediately contains this type.
12288    Non-named types get global scope.  Named types nested in other
12289    types get their containing scope if it's open, or global scope
12290    otherwise.  All other types (i.e. function-local named types) get
12291    the current active scope.  */
12292
12293 static dw_die_ref
12294 scope_die_for (tree t, dw_die_ref context_die)
12295 {
12296   dw_die_ref scope_die = NULL;
12297   tree containing_scope;
12298   int i;
12299
12300   /* Non-types always go in the current scope.  */
12301   gcc_assert (TYPE_P (t));
12302
12303   containing_scope = TYPE_CONTEXT (t);
12304
12305   /* Use the containing namespace if it was passed in (for a declaration).  */
12306   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12307     {
12308       if (context_die == lookup_decl_die (containing_scope))
12309         /* OK */;
12310       else
12311         containing_scope = NULL_TREE;
12312     }
12313
12314   /* Ignore function type "scopes" from the C frontend.  They mean that
12315      a tagged type is local to a parmlist of a function declarator, but
12316      that isn't useful to DWARF.  */
12317   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12318     containing_scope = NULL_TREE;
12319
12320   if (containing_scope == NULL_TREE)
12321     scope_die = comp_unit_die;
12322   else if (TYPE_P (containing_scope))
12323     {
12324       /* For types, we can just look up the appropriate DIE.  But
12325          first we check to see if we're in the middle of emitting it
12326          so we know where the new DIE should go.  */
12327       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12328         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12329           break;
12330
12331       if (i < 0)
12332         {
12333           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12334                       || TREE_ASM_WRITTEN (containing_scope));
12335
12336           /* If none of the current dies are suitable, we get file scope.  */
12337           scope_die = comp_unit_die;
12338         }
12339       else
12340         scope_die = lookup_type_die (containing_scope);
12341     }
12342   else
12343     scope_die = context_die;
12344
12345   return scope_die;
12346 }
12347
12348 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12349
12350 static inline int
12351 local_scope_p (dw_die_ref context_die)
12352 {
12353   for (; context_die; context_die = context_die->die_parent)
12354     if (context_die->die_tag == DW_TAG_inlined_subroutine
12355         || context_die->die_tag == DW_TAG_subprogram)
12356       return 1;
12357
12358   return 0;
12359 }
12360
12361 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12362    whether or not to treat a DIE in this context as a declaration.  */
12363
12364 static inline int
12365 class_or_namespace_scope_p (dw_die_ref context_die)
12366 {
12367   return (context_die
12368           && (context_die->die_tag == DW_TAG_structure_type
12369               || context_die->die_tag == DW_TAG_class_type
12370               || context_die->die_tag == DW_TAG_interface_type
12371               || context_die->die_tag == DW_TAG_union_type
12372               || context_die->die_tag == DW_TAG_namespace));
12373 }
12374
12375 /* Many forms of DIEs require a "type description" attribute.  This
12376    routine locates the proper "type descriptor" die for the type given
12377    by 'type', and adds a DW_AT_type attribute below the given die.  */
12378
12379 static void
12380 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12381                     int decl_volatile, dw_die_ref context_die)
12382 {
12383   enum tree_code code  = TREE_CODE (type);
12384   dw_die_ref type_die  = NULL;
12385
12386   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12387      or fixed-point type, use the inner type.  This is because we have no
12388      support for unnamed types in base_type_die.  This can happen if this is
12389      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12390   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12391       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12392     type = TREE_TYPE (type), code = TREE_CODE (type);
12393
12394   if (code == ERROR_MARK
12395       /* Handle a special case.  For functions whose return type is void, we
12396          generate *no* type attribute.  (Note that no object may have type
12397          `void', so this only applies to function return types).  */
12398       || code == VOID_TYPE)
12399     return;
12400
12401   type_die = modified_type_die (type,
12402                                 decl_const || TYPE_READONLY (type),
12403                                 decl_volatile || TYPE_VOLATILE (type),
12404                                 context_die);
12405
12406   if (type_die != NULL)
12407     add_AT_die_ref (object_die, DW_AT_type, type_die);
12408 }
12409
12410 /* Given an object die, add the calling convention attribute for the
12411    function call type.  */
12412 static void
12413 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12414 {
12415   enum dwarf_calling_convention value = DW_CC_normal;
12416
12417   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12418
12419   /* DWARF doesn't provide a way to identify a program's source-level
12420      entry point.  DW_AT_calling_convention attributes are only meant
12421      to describe functions' calling conventions.  However, lacking a
12422      better way to signal the Fortran main program, we use this for the
12423      time being, following existing custom.  */
12424   if (is_fortran ()
12425       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12426     value = DW_CC_program;
12427
12428   /* Only add the attribute if the backend requests it, and
12429      is not DW_CC_normal.  */
12430   if (value && (value != DW_CC_normal))
12431     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12432 }
12433
12434 /* Given a tree pointer to a struct, class, union, or enum type node, return
12435    a pointer to the (string) tag name for the given type, or zero if the type
12436    was declared without a tag.  */
12437
12438 static const char *
12439 type_tag (const_tree type)
12440 {
12441   const char *name = 0;
12442
12443   if (TYPE_NAME (type) != 0)
12444     {
12445       tree t = 0;
12446
12447       /* Find the IDENTIFIER_NODE for the type name.  */
12448       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12449         t = TYPE_NAME (type);
12450
12451       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12452          a TYPE_DECL node, regardless of whether or not a `typedef' was
12453          involved.  */
12454       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12455                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12456         {
12457           /* We want to be extra verbose.  Don't call dwarf_name if
12458              DECL_NAME isn't set.  The default hook for decl_printable_name
12459              doesn't like that, and in this context it's correct to return
12460              0, instead of "<anonymous>" or the like.  */
12461           if (DECL_NAME (TYPE_NAME (type)))
12462             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12463         }
12464
12465       /* Now get the name as a string, or invent one.  */
12466       if (!name && t != 0)
12467         name = IDENTIFIER_POINTER (t);
12468     }
12469
12470   return (name == 0 || *name == '\0') ? 0 : name;
12471 }
12472
12473 /* Return the type associated with a data member, make a special check
12474    for bit field types.  */
12475
12476 static inline tree
12477 member_declared_type (const_tree member)
12478 {
12479   return (DECL_BIT_FIELD_TYPE (member)
12480           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12481 }
12482
12483 /* Get the decl's label, as described by its RTL. This may be different
12484    from the DECL_NAME name used in the source file.  */
12485
12486 #if 0
12487 static const char *
12488 decl_start_label (tree decl)
12489 {
12490   rtx x;
12491   const char *fnname;
12492
12493   x = DECL_RTL (decl);
12494   gcc_assert (MEM_P (x));
12495
12496   x = XEXP (x, 0);
12497   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12498
12499   fnname = XSTR (x, 0);
12500   return fnname;
12501 }
12502 #endif
12503 \f
12504 /* These routines generate the internal representation of the DIE's for
12505    the compilation unit.  Debugging information is collected by walking
12506    the declaration trees passed in from dwarf2out_decl().  */
12507
12508 static void
12509 gen_array_type_die (tree type, dw_die_ref context_die)
12510 {
12511   dw_die_ref scope_die = scope_die_for (type, context_die);
12512   dw_die_ref array_die;
12513
12514   /* GNU compilers represent multidimensional array types as sequences of one
12515      dimensional array types whose element types are themselves array types.
12516      We sometimes squish that down to a single array_type DIE with multiple
12517      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12518      say that we are allowed to do this kind of compression in C, because
12519      there is no difference between an array of arrays and a multidimensional
12520      array.  We don't do this for Ada to remain as close as possible to the
12521      actual representation, which is especially important against the language
12522      flexibilty wrt arrays of variable size.  */
12523
12524   bool collapse_nested_arrays = !is_ada ();
12525   tree element_type;
12526   
12527   /* ??? The SGI dwarf reader fails for array of array of enum types
12528      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12529      array type comes before the outer array type.  We thus call gen_type_die
12530      before we new_die and must prevent nested array types collapsing for this
12531      target.  */
12532
12533 #ifdef MIPS_DEBUGGING_INFO
12534   gen_type_die (TREE_TYPE (type), context_die);
12535   collapse_nested_arrays = false;
12536 #endif
12537
12538   array_die = new_die (DW_TAG_array_type, scope_die, type);
12539   add_name_attribute (array_die, type_tag (type));
12540   equate_type_number_to_die (type, array_die);
12541
12542   if (TREE_CODE (type) == VECTOR_TYPE)
12543     {
12544       /* The frontend feeds us a representation for the vector as a struct
12545          containing an array.  Pull out the array type.  */
12546       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12547       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12548     }
12549
12550   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12551   if (is_fortran ()
12552       && TREE_CODE (type) == ARRAY_TYPE
12553       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
12554     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12555
12556 #if 0
12557   /* We default the array ordering.  SDB will probably do
12558      the right things even if DW_AT_ordering is not present.  It's not even
12559      an issue until we start to get into multidimensional arrays anyway.  If
12560      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12561      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12562      and when we find out that we need to put these in, we will only do so
12563      for multidimensional arrays.  */
12564   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12565 #endif
12566
12567 #ifdef MIPS_DEBUGGING_INFO
12568   /* The SGI compilers handle arrays of unknown bound by setting
12569      AT_declaration and not emitting any subrange DIEs.  */
12570   if (! TYPE_DOMAIN (type))
12571     add_AT_flag (array_die, DW_AT_declaration, 1);
12572   else
12573 #endif
12574     add_subscript_info (array_die, type, collapse_nested_arrays);
12575
12576   /* Add representation of the type of the elements of this array type and
12577      emit the corresponding DIE if we haven't done it already.  */  
12578   element_type = TREE_TYPE (type);
12579   if (collapse_nested_arrays)
12580     while (TREE_CODE (element_type) == ARRAY_TYPE)
12581       element_type = TREE_TYPE (element_type);
12582   
12583 #ifndef MIPS_DEBUGGING_INFO
12584   gen_type_die (element_type, context_die);
12585 #endif
12586
12587   add_type_attribute (array_die, element_type, 0, 0, context_die);
12588
12589   if (get_AT (array_die, DW_AT_name))
12590     add_pubtype (type, array_die);
12591 }
12592
12593 static dw_loc_descr_ref
12594 descr_info_loc (tree val, tree base_decl)
12595 {
12596   HOST_WIDE_INT size;
12597   dw_loc_descr_ref loc, loc2;
12598   enum dwarf_location_atom op;
12599
12600   if (val == base_decl)
12601     return new_loc_descr (DW_OP_push_object_address, 0, 0);
12602
12603   switch (TREE_CODE (val))
12604     {
12605     CASE_CONVERT:
12606       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12607     case INTEGER_CST:
12608       if (host_integerp (val, 0))
12609         return int_loc_descriptor (tree_low_cst (val, 0));
12610       break;
12611     case INDIRECT_REF:
12612       size = int_size_in_bytes (TREE_TYPE (val));
12613       if (size < 0)
12614         break;
12615       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12616       if (!loc)
12617         break;
12618       if (size == DWARF2_ADDR_SIZE)
12619         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
12620       else
12621         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
12622       return loc;
12623     case POINTER_PLUS_EXPR:
12624     case PLUS_EXPR:
12625       if (host_integerp (TREE_OPERAND (val, 1), 1)
12626           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
12627              < 16384)
12628         {
12629           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12630           if (!loc)
12631             break;
12632           add_loc_descr (&loc,
12633                          new_loc_descr (DW_OP_plus_uconst,
12634                                         tree_low_cst (TREE_OPERAND (val, 1),
12635                                                       1), 0));
12636         }
12637       else
12638         {
12639           op = DW_OP_plus;
12640         do_binop:
12641           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12642           if (!loc)
12643             break;
12644           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
12645           if (!loc2)
12646             break;
12647           add_loc_descr (&loc, loc2);
12648           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
12649         }
12650       return loc;
12651     case MINUS_EXPR:
12652       op = DW_OP_minus;
12653       goto do_binop;
12654     case MULT_EXPR:
12655       op = DW_OP_mul;
12656       goto do_binop;
12657     case EQ_EXPR:
12658       op = DW_OP_eq;
12659       goto do_binop;
12660     case NE_EXPR:
12661       op = DW_OP_ne;
12662       goto do_binop;
12663     default:
12664       break;
12665     }
12666   return NULL;
12667 }
12668
12669 static void
12670 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
12671                       tree val, tree base_decl)
12672 {
12673   dw_loc_descr_ref loc;
12674
12675   if (host_integerp (val, 0))
12676     {
12677       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
12678       return;
12679     }
12680
12681   loc = descr_info_loc (val, base_decl);
12682   if (!loc)
12683     return;
12684
12685   add_AT_loc (die, attr, loc);
12686 }
12687
12688 /* This routine generates DIE for array with hidden descriptor, details
12689    are filled into *info by a langhook.  */
12690
12691 static void
12692 gen_descr_array_type_die (tree type, struct array_descr_info *info,
12693                           dw_die_ref context_die)
12694 {
12695   dw_die_ref scope_die = scope_die_for (type, context_die);
12696   dw_die_ref array_die;
12697   int dim;
12698
12699   array_die = new_die (DW_TAG_array_type, scope_die, type);
12700   add_name_attribute (array_die, type_tag (type));
12701   equate_type_number_to_die (type, array_die);
12702
12703   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12704   if (is_fortran ()
12705       && info->ndimensions >= 2)
12706     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12707
12708   if (info->data_location)
12709     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
12710                           info->base_decl);
12711   if (info->associated)
12712     add_descr_info_field (array_die, DW_AT_associated, info->associated,
12713                           info->base_decl);
12714   if (info->allocated)
12715     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
12716                           info->base_decl);
12717
12718   for (dim = 0; dim < info->ndimensions; dim++)
12719     {
12720       dw_die_ref subrange_die
12721         = new_die (DW_TAG_subrange_type, array_die, NULL);
12722
12723       if (info->dimen[dim].lower_bound)
12724         {
12725           /* If it is the default value, omit it.  */
12726           if ((is_c_family () || is_java ())
12727               && integer_zerop (info->dimen[dim].lower_bound))
12728             ;
12729           else if (is_fortran ()
12730                    && integer_onep (info->dimen[dim].lower_bound))
12731             ;
12732           else
12733             add_descr_info_field (subrange_die, DW_AT_lower_bound,
12734                                   info->dimen[dim].lower_bound,
12735                                   info->base_decl);
12736         }
12737       if (info->dimen[dim].upper_bound)
12738         add_descr_info_field (subrange_die, DW_AT_upper_bound,
12739                               info->dimen[dim].upper_bound,
12740                               info->base_decl);
12741       if (info->dimen[dim].stride)
12742         add_descr_info_field (subrange_die, DW_AT_byte_stride,
12743                               info->dimen[dim].stride,
12744                               info->base_decl);
12745     }
12746
12747   gen_type_die (info->element_type, context_die);
12748   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
12749
12750   if (get_AT (array_die, DW_AT_name))
12751     add_pubtype (type, array_die);
12752 }
12753
12754 #if 0
12755 static void
12756 gen_entry_point_die (tree decl, dw_die_ref context_die)
12757 {
12758   tree origin = decl_ultimate_origin (decl);
12759   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
12760
12761   if (origin != NULL)
12762     add_abstract_origin_attribute (decl_die, origin);
12763   else
12764     {
12765       add_name_and_src_coords_attributes (decl_die, decl);
12766       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
12767                           0, 0, context_die);
12768     }
12769
12770   if (DECL_ABSTRACT (decl))
12771     equate_decl_number_to_die (decl, decl_die);
12772   else
12773     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
12774 }
12775 #endif
12776
12777 /* Walk through the list of incomplete types again, trying once more to
12778    emit full debugging info for them.  */
12779
12780 static void
12781 retry_incomplete_types (void)
12782 {
12783   int i;
12784
12785   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12786     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12787 }
12788
12789 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
12790
12791 static void
12792 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12793 {
12794   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12795
12796   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12797      be incomplete and such types are not marked.  */
12798   add_abstract_origin_attribute (type_die, type);
12799 }
12800
12801 /* Determine what tag to use for a record type.  */
12802
12803 static enum dwarf_tag
12804 record_type_tag (tree type)
12805 {
12806   if (! lang_hooks.types.classify_record)
12807     return DW_TAG_structure_type;
12808
12809   switch (lang_hooks.types.classify_record (type))
12810     {
12811     case RECORD_IS_STRUCT:
12812       return DW_TAG_structure_type;
12813
12814     case RECORD_IS_CLASS:
12815       return DW_TAG_class_type;
12816
12817     case RECORD_IS_INTERFACE:
12818       return DW_TAG_interface_type;
12819
12820     default:
12821       gcc_unreachable ();
12822     }
12823 }
12824
12825 /* Generate a DIE to represent an inlined instance of a structure type.  */
12826
12827 static void
12828 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12829 {
12830   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12831
12832   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12833      be incomplete and such types are not marked.  */
12834   add_abstract_origin_attribute (type_die, type);
12835 }
12836
12837 /* Generate a DIE to represent an inlined instance of a union type.  */
12838
12839 static void
12840 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12841 {
12842   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12843
12844   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12845      be incomplete and such types are not marked.  */
12846   add_abstract_origin_attribute (type_die, type);
12847 }
12848
12849 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
12850    include all of the information about the enumeration values also. Each
12851    enumerated type name/value is listed as a child of the enumerated type
12852    DIE.  */
12853
12854 static dw_die_ref
12855 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12856 {
12857   dw_die_ref type_die = lookup_type_die (type);
12858
12859   if (type_die == NULL)
12860     {
12861       type_die = new_die (DW_TAG_enumeration_type,
12862                           scope_die_for (type, context_die), type);
12863       equate_type_number_to_die (type, type_die);
12864       add_name_attribute (type_die, type_tag (type));
12865     }
12866   else if (! TYPE_SIZE (type))
12867     return type_die;
12868   else
12869     remove_AT (type_die, DW_AT_declaration);
12870
12871   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12872      given enum type is incomplete, do not generate the DW_AT_byte_size
12873      attribute or the DW_AT_element_list attribute.  */
12874   if (TYPE_SIZE (type))
12875     {
12876       tree link;
12877
12878       TREE_ASM_WRITTEN (type) = 1;
12879       add_byte_size_attribute (type_die, type);
12880       if (TYPE_STUB_DECL (type) != NULL_TREE)
12881         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12882
12883       /* If the first reference to this type was as the return type of an
12884          inline function, then it may not have a parent.  Fix this now.  */
12885       if (type_die->die_parent == NULL)
12886         add_child_die (scope_die_for (type, context_die), type_die);
12887
12888       for (link = TYPE_VALUES (type);
12889            link != NULL; link = TREE_CHAIN (link))
12890         {
12891           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12892           tree value = TREE_VALUE (link);
12893
12894           add_name_attribute (enum_die,
12895                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12896
12897           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12898             /* DWARF2 does not provide a way of indicating whether or
12899                not enumeration constants are signed or unsigned.  GDB
12900                always assumes the values are signed, so we output all
12901                values as if they were signed.  That means that
12902                enumeration constants with very large unsigned values
12903                will appear to have negative values in the debugger.  */
12904             add_AT_int (enum_die, DW_AT_const_value,
12905                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12906         }
12907     }
12908   else
12909     add_AT_flag (type_die, DW_AT_declaration, 1);
12910
12911   if (get_AT (type_die, DW_AT_name))
12912     add_pubtype (type, type_die);
12913
12914   return type_die;
12915 }
12916
12917 /* Generate a DIE to represent either a real live formal parameter decl or to
12918    represent just the type of some formal parameter position in some function
12919    type.
12920
12921    Note that this routine is a bit unusual because its argument may be a
12922    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12923    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12924    node.  If it's the former then this function is being called to output a
12925    DIE to represent a formal parameter object (or some inlining thereof).  If
12926    it's the latter, then this function is only being called to output a
12927    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12928    argument type of some subprogram type.  */
12929
12930 static dw_die_ref
12931 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12932 {
12933   dw_die_ref parm_die
12934     = new_die (DW_TAG_formal_parameter, context_die, node);
12935   tree origin;
12936
12937   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12938     {
12939     case tcc_declaration:
12940       origin = decl_ultimate_origin (node);
12941       if (origin != NULL)
12942         add_abstract_origin_attribute (parm_die, origin);
12943       else
12944         {
12945           tree type = TREE_TYPE (node);
12946           add_name_and_src_coords_attributes (parm_die, node);
12947           if (DECL_BY_REFERENCE (node))
12948             type = TREE_TYPE (type);
12949           add_type_attribute (parm_die, type,
12950                               TREE_READONLY (node),
12951                               TREE_THIS_VOLATILE (node),
12952                               context_die);
12953           if (DECL_ARTIFICIAL (node))
12954             add_AT_flag (parm_die, DW_AT_artificial, 1);
12955         }
12956
12957       equate_decl_number_to_die (node, parm_die);
12958       if (! DECL_ABSTRACT (node))
12959         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12960
12961       break;
12962
12963     case tcc_type:
12964       /* We were called with some kind of a ..._TYPE node.  */
12965       add_type_attribute (parm_die, node, 0, 0, context_die);
12966       break;
12967
12968     default:
12969       gcc_unreachable ();
12970     }
12971
12972   return parm_die;
12973 }
12974
12975 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12976    at the end of an (ANSI prototyped) formal parameters list.  */
12977
12978 static void
12979 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12980 {
12981   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12982 }
12983
12984 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12985    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12986    parameters as specified in some function type specification (except for
12987    those which appear as part of a function *definition*).  */
12988
12989 static void
12990 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12991 {
12992   tree link;
12993   tree formal_type = NULL;
12994   tree first_parm_type;
12995   tree arg;
12996
12997   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12998     {
12999       arg = DECL_ARGUMENTS (function_or_method_type);
13000       function_or_method_type = TREE_TYPE (function_or_method_type);
13001     }
13002   else
13003     arg = NULL_TREE;
13004
13005   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13006
13007   /* Make our first pass over the list of formal parameter types and output a
13008      DW_TAG_formal_parameter DIE for each one.  */
13009   for (link = first_parm_type; link; )
13010     {
13011       dw_die_ref parm_die;
13012
13013       formal_type = TREE_VALUE (link);
13014       if (formal_type == void_type_node)
13015         break;
13016
13017       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13018       parm_die = gen_formal_parameter_die (formal_type, context_die);
13019       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13020            && link == first_parm_type)
13021           || (arg && DECL_ARTIFICIAL (arg)))
13022         add_AT_flag (parm_die, DW_AT_artificial, 1);
13023
13024       link = TREE_CHAIN (link);
13025       if (arg)
13026         arg = TREE_CHAIN (arg);
13027     }
13028
13029   /* If this function type has an ellipsis, add a
13030      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13031   if (formal_type != void_type_node)
13032     gen_unspecified_parameters_die (function_or_method_type, context_die);
13033
13034   /* Make our second (and final) pass over the list of formal parameter types
13035      and output DIEs to represent those types (as necessary).  */
13036   for (link = TYPE_ARG_TYPES (function_or_method_type);
13037        link && TREE_VALUE (link);
13038        link = TREE_CHAIN (link))
13039     gen_type_die (TREE_VALUE (link), context_die);
13040 }
13041
13042 /* We want to generate the DIE for TYPE so that we can generate the
13043    die for MEMBER, which has been defined; we will need to refer back
13044    to the member declaration nested within TYPE.  If we're trying to
13045    generate minimal debug info for TYPE, processing TYPE won't do the
13046    trick; we need to attach the member declaration by hand.  */
13047
13048 static void
13049 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13050 {
13051   gen_type_die (type, context_die);
13052
13053   /* If we're trying to avoid duplicate debug info, we may not have
13054      emitted the member decl for this function.  Emit it now.  */
13055   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13056       && ! lookup_decl_die (member))
13057     {
13058       dw_die_ref type_die;
13059       gcc_assert (!decl_ultimate_origin (member));
13060
13061       push_decl_scope (type);
13062       type_die = lookup_type_die (type);
13063       if (TREE_CODE (member) == FUNCTION_DECL)
13064         gen_subprogram_die (member, type_die);
13065       else if (TREE_CODE (member) == FIELD_DECL)
13066         {
13067           /* Ignore the nameless fields that are used to skip bits but handle
13068              C++ anonymous unions and structs.  */
13069           if (DECL_NAME (member) != NULL_TREE
13070               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13071               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13072             {
13073               gen_type_die (member_declared_type (member), type_die);
13074               gen_field_die (member, type_die);
13075             }
13076         }
13077       else
13078         gen_variable_die (member, type_die);
13079
13080       pop_decl_scope ();
13081     }
13082 }
13083
13084 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13085    may later generate inlined and/or out-of-line instances of.  */
13086
13087 static void
13088 dwarf2out_abstract_function (tree decl)
13089 {
13090   dw_die_ref old_die;
13091   tree save_fn;
13092   tree context;
13093   int was_abstract = DECL_ABSTRACT (decl);
13094
13095   /* Make sure we have the actual abstract inline, not a clone.  */
13096   decl = DECL_ORIGIN (decl);
13097
13098   old_die = lookup_decl_die (decl);
13099   if (old_die && get_AT (old_die, DW_AT_inline))
13100     /* We've already generated the abstract instance.  */
13101     return;
13102
13103   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13104      we don't get confused by DECL_ABSTRACT.  */
13105   if (debug_info_level > DINFO_LEVEL_TERSE)
13106     {
13107       context = decl_class_context (decl);
13108       if (context)
13109         gen_type_die_for_member
13110           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13111     }
13112
13113   /* Pretend we've just finished compiling this function.  */
13114   save_fn = current_function_decl;
13115   current_function_decl = decl;
13116   push_cfun (DECL_STRUCT_FUNCTION (decl));
13117
13118   set_decl_abstract_flags (decl, 1);
13119   dwarf2out_decl (decl);
13120   if (! was_abstract)
13121     set_decl_abstract_flags (decl, 0);
13122
13123   current_function_decl = save_fn;
13124   pop_cfun ();
13125 }
13126
13127 /* Helper function of premark_used_types() which gets called through
13128    htab_traverse_resize().
13129
13130    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13131    marked as unused by prune_unused_types.  */
13132 static int
13133 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13134 {
13135   tree type;
13136   dw_die_ref die;
13137
13138   type = (tree) *slot;
13139   die = lookup_type_die (type);
13140   if (die != NULL)
13141     die->die_perennial_p = 1;
13142   return 1;
13143 }
13144
13145 /* Mark all members of used_types_hash as perennial.  */
13146 static void
13147 premark_used_types (void)
13148 {
13149   if (cfun && cfun->used_types_hash)
13150     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13151 }
13152
13153 /* Generate a DIE to represent a declared function (either file-scope or
13154    block-local).  */
13155
13156 static void
13157 gen_subprogram_die (tree decl, dw_die_ref context_die)
13158 {
13159   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13160   tree origin = decl_ultimate_origin (decl);
13161   dw_die_ref subr_die;
13162   tree fn_arg_types;
13163   tree outer_scope;
13164   dw_die_ref old_die = lookup_decl_die (decl);
13165   int declaration = (current_function_decl != decl
13166                      || class_or_namespace_scope_p (context_die));
13167
13168   premark_used_types ();
13169
13170   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13171      started to generate the abstract instance of an inline, decided to output
13172      its containing class, and proceeded to emit the declaration of the inline
13173      from the member list for the class.  If so, DECLARATION takes priority;
13174      we'll get back to the abstract instance when done with the class.  */
13175
13176   /* The class-scope declaration DIE must be the primary DIE.  */
13177   if (origin && declaration && class_or_namespace_scope_p (context_die))
13178     {
13179       origin = NULL;
13180       gcc_assert (!old_die);
13181     }
13182
13183   /* Now that the C++ front end lazily declares artificial member fns, we
13184      might need to retrofit the declaration into its class.  */
13185   if (!declaration && !origin && !old_die
13186       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13187       && !class_or_namespace_scope_p (context_die)
13188       && debug_info_level > DINFO_LEVEL_TERSE)
13189     old_die = force_decl_die (decl);
13190
13191   if (origin != NULL)
13192     {
13193       gcc_assert (!declaration || local_scope_p (context_die));
13194
13195       /* Fixup die_parent for the abstract instance of a nested
13196          inline function.  */
13197       if (old_die && old_die->die_parent == NULL)
13198         add_child_die (context_die, old_die);
13199
13200       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13201       add_abstract_origin_attribute (subr_die, origin);
13202     }
13203   else if (old_die)
13204     {
13205       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13206       struct dwarf_file_data * file_index = lookup_filename (s.file);
13207
13208       if (!get_AT_flag (old_die, DW_AT_declaration)
13209           /* We can have a normal definition following an inline one in the
13210              case of redefinition of GNU C extern inlines.
13211              It seems reasonable to use AT_specification in this case.  */
13212           && !get_AT (old_die, DW_AT_inline))
13213         {
13214           /* Detect and ignore this case, where we are trying to output
13215              something we have already output.  */
13216           return;
13217         }
13218
13219       /* If the definition comes from the same place as the declaration,
13220          maybe use the old DIE.  We always want the DIE for this function
13221          that has the *_pc attributes to be under comp_unit_die so the
13222          debugger can find it.  We also need to do this for abstract
13223          instances of inlines, since the spec requires the out-of-line copy
13224          to have the same parent.  For local class methods, this doesn't
13225          apply; we just use the old DIE.  */
13226       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13227           && (DECL_ARTIFICIAL (decl)
13228               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13229                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13230                       == (unsigned) s.line))))
13231         {
13232           subr_die = old_die;
13233
13234           /* Clear out the declaration attribute and the formal parameters.
13235              Do not remove all children, because it is possible that this
13236              declaration die was forced using force_decl_die(). In such
13237              cases die that forced declaration die (e.g. TAG_imported_module)
13238              is one of the children that we do not want to remove.  */
13239           remove_AT (subr_die, DW_AT_declaration);
13240           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13241         }
13242       else
13243         {
13244           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13245           add_AT_specification (subr_die, old_die);
13246           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13247             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13248           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13249             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13250         }
13251     }
13252   else
13253     {
13254       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13255
13256       if (TREE_PUBLIC (decl))
13257         add_AT_flag (subr_die, DW_AT_external, 1);
13258
13259       add_name_and_src_coords_attributes (subr_die, decl);
13260       if (debug_info_level > DINFO_LEVEL_TERSE)
13261         {
13262           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13263           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13264                               0, 0, context_die);
13265         }
13266
13267       add_pure_or_virtual_attribute (subr_die, decl);
13268       if (DECL_ARTIFICIAL (decl))
13269         add_AT_flag (subr_die, DW_AT_artificial, 1);
13270
13271       if (TREE_PROTECTED (decl))
13272         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13273       else if (TREE_PRIVATE (decl))
13274         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13275     }
13276
13277   if (declaration)
13278     {
13279       if (!old_die || !get_AT (old_die, DW_AT_inline))
13280         {
13281           add_AT_flag (subr_die, DW_AT_declaration, 1);
13282
13283           /* The first time we see a member function, it is in the context of
13284              the class to which it belongs.  We make sure of this by emitting
13285              the class first.  The next time is the definition, which is
13286              handled above.  The two may come from the same source text.
13287
13288              Note that force_decl_die() forces function declaration die. It is
13289              later reused to represent definition.  */
13290           equate_decl_number_to_die (decl, subr_die);
13291         }
13292     }
13293   else if (DECL_ABSTRACT (decl))
13294     {
13295       if (DECL_DECLARED_INLINE_P (decl))
13296         {
13297           if (cgraph_function_possibly_inlined_p (decl))
13298             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13299           else
13300             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13301         }
13302       else
13303         {
13304           if (cgraph_function_possibly_inlined_p (decl))
13305             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13306           else
13307             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13308         }
13309
13310       if (DECL_DECLARED_INLINE_P (decl)
13311           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13312         add_AT_flag (subr_die, DW_AT_artificial, 1);
13313
13314       equate_decl_number_to_die (decl, subr_die);
13315     }
13316   else if (!DECL_EXTERNAL (decl))
13317     {
13318       HOST_WIDE_INT cfa_fb_offset;
13319
13320       if (!old_die || !get_AT (old_die, DW_AT_inline))
13321         equate_decl_number_to_die (decl, subr_die);
13322
13323       if (!flag_reorder_blocks_and_partition)
13324         {
13325           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13326                                        current_function_funcdef_no);
13327           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13328           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13329                                        current_function_funcdef_no);
13330           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13331
13332           add_pubname (decl, subr_die);
13333           add_arange (decl, subr_die);
13334         }
13335       else
13336         {  /* Do nothing for now; maybe need to duplicate die, one for
13337               hot section and one for cold section, then use the hot/cold
13338               section begin/end labels to generate the aranges...  */
13339           /*
13340             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13341             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13342             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13343             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13344
13345             add_pubname (decl, subr_die);
13346             add_arange (decl, subr_die);
13347             add_arange (decl, subr_die);
13348            */
13349         }
13350
13351 #ifdef MIPS_DEBUGGING_INFO
13352       /* Add a reference to the FDE for this routine.  */
13353       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13354 #endif
13355
13356       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13357
13358       /* We define the "frame base" as the function's CFA.  This is more
13359          convenient for several reasons: (1) It's stable across the prologue
13360          and epilogue, which makes it better than just a frame pointer,
13361          (2) With dwarf3, there exists a one-byte encoding that allows us
13362          to reference the .debug_frame data by proxy, but failing that,
13363          (3) We can at least reuse the code inspection and interpretation
13364          code that determines the CFA position at various points in the
13365          function.  */
13366       /* ??? Use some command-line or configury switch to enable the use
13367          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13368          consumers that understand it; fall back to "pure" dwarf2 and
13369          convert the CFA data into a location list.  */
13370       {
13371         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13372         if (list->dw_loc_next)
13373           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13374         else
13375           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13376       }
13377
13378       /* Compute a displacement from the "steady-state frame pointer" to
13379          the CFA.  The former is what all stack slots and argument slots
13380          will reference in the rtl; the later is what we've told the
13381          debugger about.  We'll need to adjust all frame_base references
13382          by this displacement.  */
13383       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13384
13385       if (cfun->static_chain_decl)
13386         add_AT_location_description (subr_die, DW_AT_static_link,
13387                  loc_descriptor_from_tree (cfun->static_chain_decl));
13388     }
13389
13390   /* Now output descriptions of the arguments for this function. This gets
13391      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13392      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13393      `...' at the end of the formal parameter list.  In order to find out if
13394      there was a trailing ellipsis or not, we must instead look at the type
13395      associated with the FUNCTION_DECL.  This will be a node of type
13396      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13397      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13398      an ellipsis at the end.  */
13399
13400   /* In the case where we are describing a mere function declaration, all we
13401      need to do here (and all we *can* do here) is to describe the *types* of
13402      its formal parameters.  */
13403   if (debug_info_level <= DINFO_LEVEL_TERSE)
13404     ;
13405   else if (declaration)
13406     gen_formal_types_die (decl, subr_die);
13407   else
13408     {
13409       /* Generate DIEs to represent all known formal parameters.  */
13410       tree arg_decls = DECL_ARGUMENTS (decl);
13411       tree parm;
13412
13413       /* When generating DIEs, generate the unspecified_parameters DIE
13414          instead if we come across the arg "__builtin_va_alist" */
13415       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13416         if (TREE_CODE (parm) == PARM_DECL)
13417           {
13418             if (DECL_NAME (parm)
13419                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13420                             "__builtin_va_alist"))
13421               gen_unspecified_parameters_die (parm, subr_die);
13422             else
13423               gen_decl_die (parm, subr_die);
13424           }
13425
13426       /* Decide whether we need an unspecified_parameters DIE at the end.
13427          There are 2 more cases to do this for: 1) the ansi ... declaration -
13428          this is detectable when the end of the arg list is not a
13429          void_type_node 2) an unprototyped function declaration (not a
13430          definition).  This just means that we have no info about the
13431          parameters at all.  */
13432       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13433       if (fn_arg_types != NULL)
13434         {
13435           /* This is the prototyped case, check for....  */
13436           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13437             gen_unspecified_parameters_die (decl, subr_die);
13438         }
13439       else if (DECL_INITIAL (decl) == NULL_TREE)
13440         gen_unspecified_parameters_die (decl, subr_die);
13441     }
13442
13443   /* Output Dwarf info for all of the stuff within the body of the function
13444      (if it has one - it may be just a declaration).  */
13445   outer_scope = DECL_INITIAL (decl);
13446
13447   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13448      a function.  This BLOCK actually represents the outermost binding contour
13449      for the function, i.e. the contour in which the function's formal
13450      parameters and labels get declared. Curiously, it appears that the front
13451      end doesn't actually put the PARM_DECL nodes for the current function onto
13452      the BLOCK_VARS list for this outer scope, but are strung off of the
13453      DECL_ARGUMENTS list for the function instead.
13454
13455      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13456      the LABEL_DECL nodes for the function however, and we output DWARF info
13457      for those in decls_for_scope.  Just within the `outer_scope' there will be
13458      a BLOCK node representing the function's outermost pair of curly braces,
13459      and any blocks used for the base and member initializers of a C++
13460      constructor function.  */
13461   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13462     {
13463       /* Emit a DW_TAG_variable DIE for a named return value.  */
13464       if (DECL_NAME (DECL_RESULT (decl)))
13465         gen_decl_die (DECL_RESULT (decl), subr_die);
13466
13467       current_function_has_inlines = 0;
13468       decls_for_scope (outer_scope, subr_die, 0);
13469
13470 #if 0 && defined (MIPS_DEBUGGING_INFO)
13471       if (current_function_has_inlines)
13472         {
13473           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13474           if (! comp_unit_has_inlines)
13475             {
13476               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13477               comp_unit_has_inlines = 1;
13478             }
13479         }
13480 #endif
13481     }
13482   /* Add the calling convention attribute if requested.  */
13483   add_calling_convention_attribute (subr_die, decl);
13484
13485 }
13486
13487 /* Generate a DIE to represent a declared data object.  */
13488
13489 static void
13490 gen_variable_die (tree decl, dw_die_ref context_die)
13491 {
13492   HOST_WIDE_INT off;
13493   tree com_decl;
13494   dw_die_ref var_die;
13495   tree origin = decl_ultimate_origin (decl);
13496   dw_die_ref old_die = lookup_decl_die (decl);
13497   int declaration = (DECL_EXTERNAL (decl)
13498                      /* If DECL is COMDAT and has not actually been
13499                         emitted, we cannot take its address; there
13500                         might end up being no definition anywhere in
13501                         the program.  For example, consider the C++
13502                         test case:
13503
13504                           template <class T>
13505                           struct S { static const int i = 7; };
13506
13507                           template <class T>
13508                           const int S<T>::i;
13509
13510                           int f() { return S<int>::i; }
13511
13512                         Here, S<int>::i is not DECL_EXTERNAL, but no
13513                         definition is required, so the compiler will
13514                         not emit a definition.  */
13515                      || (TREE_CODE (decl) == VAR_DECL
13516                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
13517                      || class_or_namespace_scope_p (context_die));
13518
13519   com_decl = fortran_common (decl, &off);
13520
13521   /* Symbol in common gets emitted as a child of the common block, in the form
13522      of a data member.
13523
13524      ??? This creates a new common block die for every common block symbol.
13525      Better to share same common block die for all symbols in that block.  */
13526   if (com_decl)
13527     {
13528       tree field;
13529       dw_die_ref com_die;
13530       const char *cnam = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13531       dw_loc_descr_ref loc = loc_descriptor_from_tree (com_decl);
13532
13533       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13534       var_die = new_die (DW_TAG_common_block, context_die, decl);
13535       add_name_and_src_coords_attributes (var_die, field);
13536       add_AT_flag (var_die, DW_AT_external, 1);
13537       add_AT_loc (var_die, DW_AT_location, loc);
13538       com_die = new_die (DW_TAG_member, var_die, decl);
13539       add_name_and_src_coords_attributes (com_die, decl);
13540       add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
13541                           TREE_THIS_VOLATILE (decl), context_die);
13542       add_AT_loc (com_die, DW_AT_data_member_location,
13543                   int_loc_descriptor (off));
13544       add_pubname_string (cnam, var_die); /* ??? needed? */
13545       return;
13546     }
13547
13548   var_die = new_die (DW_TAG_variable, context_die, decl);
13549
13550   if (origin != NULL)
13551     add_abstract_origin_attribute (var_die, origin);
13552
13553   /* Loop unrolling can create multiple blocks that refer to the same
13554      static variable, so we must test for the DW_AT_declaration flag.
13555
13556      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
13557      copy decls and set the DECL_ABSTRACT flag on them instead of
13558      sharing them.
13559
13560      ??? Duplicated blocks have been rewritten to use .debug_ranges.
13561
13562      ??? The declare_in_namespace support causes us to get two DIEs for one
13563      variable, both of which are declarations.  We want to avoid considering
13564      one to be a specification, so we must test that this DIE is not a
13565      declaration.  */
13566   else if (old_die && TREE_STATIC (decl) && ! declaration
13567            && get_AT_flag (old_die, DW_AT_declaration) == 1)
13568     {
13569       /* This is a definition of a C++ class level static.  */
13570       add_AT_specification (var_die, old_die);
13571       if (DECL_NAME (decl))
13572         {
13573           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13574           struct dwarf_file_data * file_index = lookup_filename (s.file);
13575
13576           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13577             add_AT_file (var_die, DW_AT_decl_file, file_index);
13578
13579           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13580             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
13581         }
13582     }
13583   else
13584     {
13585       tree type = TREE_TYPE (decl);
13586       if ((TREE_CODE (decl) == PARM_DECL
13587            || TREE_CODE (decl) == RESULT_DECL)
13588           && DECL_BY_REFERENCE (decl))
13589         type = TREE_TYPE (type);
13590
13591       add_name_and_src_coords_attributes (var_die, decl);
13592       add_type_attribute (var_die, type, TREE_READONLY (decl),
13593                           TREE_THIS_VOLATILE (decl), context_die);
13594
13595       if (TREE_PUBLIC (decl))
13596         add_AT_flag (var_die, DW_AT_external, 1);
13597
13598       if (DECL_ARTIFICIAL (decl))
13599         add_AT_flag (var_die, DW_AT_artificial, 1);
13600
13601       if (TREE_PROTECTED (decl))
13602         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
13603       else if (TREE_PRIVATE (decl))
13604         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
13605     }
13606
13607   if (declaration)
13608     add_AT_flag (var_die, DW_AT_declaration, 1);
13609
13610   if (DECL_ABSTRACT (decl) || declaration)
13611     equate_decl_number_to_die (decl, var_die);
13612
13613   if (! declaration && ! DECL_ABSTRACT (decl))
13614     {
13615       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
13616       add_pubname (decl, var_die);
13617     }
13618   else
13619     tree_add_const_value_attribute (var_die, decl);
13620 }
13621
13622 /* Generate a DIE to represent a label identifier.  */
13623
13624 static void
13625 gen_label_die (tree decl, dw_die_ref context_die)
13626 {
13627   tree origin = decl_ultimate_origin (decl);
13628   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
13629   rtx insn;
13630   char label[MAX_ARTIFICIAL_LABEL_BYTES];
13631
13632   if (origin != NULL)
13633     add_abstract_origin_attribute (lbl_die, origin);
13634   else
13635     add_name_and_src_coords_attributes (lbl_die, decl);
13636
13637   if (DECL_ABSTRACT (decl))
13638     equate_decl_number_to_die (decl, lbl_die);
13639   else
13640     {
13641       insn = DECL_RTL_IF_SET (decl);
13642
13643       /* Deleted labels are programmer specified labels which have been
13644          eliminated because of various optimizations.  We still emit them
13645          here so that it is possible to put breakpoints on them.  */
13646       if (insn
13647           && (LABEL_P (insn)
13648               || ((NOTE_P (insn)
13649                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
13650         {
13651           /* When optimization is enabled (via -O) some parts of the compiler
13652              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
13653              represent source-level labels which were explicitly declared by
13654              the user.  This really shouldn't be happening though, so catch
13655              it if it ever does happen.  */
13656           gcc_assert (!INSN_DELETED_P (insn));
13657
13658           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
13659           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
13660         }
13661     }
13662 }
13663
13664 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
13665    attributes to the DIE for a block STMT, to describe where the inlined
13666    function was called from.  This is similar to add_src_coords_attributes.  */
13667
13668 static inline void
13669 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
13670 {
13671   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
13672
13673   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
13674   add_AT_unsigned (die, DW_AT_call_line, s.line);
13675 }
13676
13677
13678 /* If STMT's abstract origin is a function declaration and STMT's
13679    first subblock's abstract origin is the function's outermost block,
13680    then we're looking at the main entry point.  */
13681 static bool
13682 is_inlined_entry_point (const_tree stmt)
13683 {
13684   tree decl, block;
13685
13686   if (!stmt || TREE_CODE (stmt) != BLOCK)
13687     return false;
13688
13689   decl = block_ultimate_origin (stmt);
13690
13691   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
13692     return false;
13693
13694   block = BLOCK_SUBBLOCKS (stmt);
13695
13696   if (block)
13697     {
13698       if (TREE_CODE (block) != BLOCK)
13699         return false;
13700
13701       block = block_ultimate_origin (block);
13702     }
13703
13704   return block == DECL_INITIAL (decl);
13705 }
13706
13707 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
13708    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
13709
13710 static inline void
13711 add_high_low_attributes (tree stmt, dw_die_ref die)
13712 {
13713   char label[MAX_ARTIFICIAL_LABEL_BYTES];
13714
13715   if (BLOCK_FRAGMENT_CHAIN (stmt))
13716     {
13717       tree chain;
13718
13719       if (is_inlined_entry_point (stmt))
13720         {
13721           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13722                                        BLOCK_NUMBER (stmt));
13723           add_AT_lbl_id (die, DW_AT_entry_pc, label);
13724         }
13725
13726       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
13727
13728       chain = BLOCK_FRAGMENT_CHAIN (stmt);
13729       do
13730         {
13731           add_ranges (chain);
13732           chain = BLOCK_FRAGMENT_CHAIN (chain);
13733         }
13734       while (chain);
13735       add_ranges (NULL);
13736     }
13737   else
13738     {
13739       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13740                                    BLOCK_NUMBER (stmt));
13741       add_AT_lbl_id (die, DW_AT_low_pc, label);
13742       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
13743                                    BLOCK_NUMBER (stmt));
13744       add_AT_lbl_id (die, DW_AT_high_pc, label);
13745     }
13746 }
13747
13748 /* Generate a DIE for a lexical block.  */
13749
13750 static void
13751 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
13752 {
13753   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
13754
13755   if (! BLOCK_ABSTRACT (stmt))
13756     add_high_low_attributes (stmt, stmt_die);
13757
13758   decls_for_scope (stmt, stmt_die, depth);
13759 }
13760
13761 /* Generate a DIE for an inlined subprogram.  */
13762
13763 static void
13764 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
13765 {
13766   tree decl = block_ultimate_origin (stmt);
13767
13768   /* Emit info for the abstract instance first, if we haven't yet.  We
13769      must emit this even if the block is abstract, otherwise when we
13770      emit the block below (or elsewhere), we may end up trying to emit
13771      a die whose origin die hasn't been emitted, and crashing.  */
13772   dwarf2out_abstract_function (decl);
13773
13774   if (! BLOCK_ABSTRACT (stmt))
13775     {
13776       dw_die_ref subr_die
13777         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13778
13779       add_abstract_origin_attribute (subr_die, decl);
13780       add_high_low_attributes (stmt, subr_die);
13781       add_call_src_coords_attributes (stmt, subr_die);
13782
13783       decls_for_scope (stmt, subr_die, depth);
13784       current_function_has_inlines = 1;
13785     }
13786   else
13787     /* We may get here if we're the outer block of function A that was
13788        inlined into function B that was inlined into function C.  When
13789        generating debugging info for C, dwarf2out_abstract_function(B)
13790        would mark all inlined blocks as abstract, including this one.
13791        So, we wouldn't (and shouldn't) expect labels to be generated
13792        for this one.  Instead, just emit debugging info for
13793        declarations within the block.  This is particularly important
13794        in the case of initializers of arguments passed from B to us:
13795        if they're statement expressions containing declarations, we
13796        wouldn't generate dies for their abstract variables, and then,
13797        when generating dies for the real variables, we'd die (pun
13798        intended :-)  */
13799     gen_lexical_block_die (stmt, context_die, depth);
13800 }
13801
13802 /* Generate a DIE for a field in a record, or structure.  */
13803
13804 static void
13805 gen_field_die (tree decl, dw_die_ref context_die)
13806 {
13807   dw_die_ref decl_die;
13808
13809   if (TREE_TYPE (decl) == error_mark_node)
13810     return;
13811
13812   decl_die = new_die (DW_TAG_member, context_die, decl);
13813   add_name_and_src_coords_attributes (decl_die, decl);
13814   add_type_attribute (decl_die, member_declared_type (decl),
13815                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13816                       context_die);
13817
13818   if (DECL_BIT_FIELD_TYPE (decl))
13819     {
13820       add_byte_size_attribute (decl_die, decl);
13821       add_bit_size_attribute (decl_die, decl);
13822       add_bit_offset_attribute (decl_die, decl);
13823     }
13824
13825   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13826     add_data_member_location_attribute (decl_die, decl);
13827
13828   if (DECL_ARTIFICIAL (decl))
13829     add_AT_flag (decl_die, DW_AT_artificial, 1);
13830
13831   if (TREE_PROTECTED (decl))
13832     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13833   else if (TREE_PRIVATE (decl))
13834     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13835
13836   /* Equate decl number to die, so that we can look up this decl later on.  */
13837   equate_decl_number_to_die (decl, decl_die);
13838 }
13839
13840 #if 0
13841 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13842    Use modified_type_die instead.
13843    We keep this code here just in case these types of DIEs may be needed to
13844    represent certain things in other languages (e.g. Pascal) someday.  */
13845
13846 static void
13847 gen_pointer_type_die (tree type, dw_die_ref context_die)
13848 {
13849   dw_die_ref ptr_die
13850     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13851
13852   equate_type_number_to_die (type, ptr_die);
13853   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13854   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13855 }
13856
13857 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13858    Use modified_type_die instead.
13859    We keep this code here just in case these types of DIEs may be needed to
13860    represent certain things in other languages (e.g. Pascal) someday.  */
13861
13862 static void
13863 gen_reference_type_die (tree type, dw_die_ref context_die)
13864 {
13865   dw_die_ref ref_die
13866     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13867
13868   equate_type_number_to_die (type, ref_die);
13869   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13870   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13871 }
13872 #endif
13873
13874 /* Generate a DIE for a pointer to a member type.  */
13875
13876 static void
13877 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13878 {
13879   dw_die_ref ptr_die
13880     = new_die (DW_TAG_ptr_to_member_type,
13881                scope_die_for (type, context_die), type);
13882
13883   equate_type_number_to_die (type, ptr_die);
13884   add_AT_die_ref (ptr_die, DW_AT_containing_type,
13885                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13886   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13887 }
13888
13889 /* Generate the DIE for the compilation unit.  */
13890
13891 static dw_die_ref
13892 gen_compile_unit_die (const char *filename)
13893 {
13894   dw_die_ref die;
13895   char producer[250];
13896   const char *language_string = lang_hooks.name;
13897   int language;
13898
13899   die = new_die (DW_TAG_compile_unit, NULL, NULL);
13900
13901   if (filename)
13902     {
13903       add_name_attribute (die, filename);
13904       /* Don't add cwd for <built-in>.  */
13905       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13906         add_comp_dir_attribute (die);
13907     }
13908
13909   sprintf (producer, "%s %s", language_string, version_string);
13910
13911 #ifdef MIPS_DEBUGGING_INFO
13912   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13913      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13914      not appear in the producer string, the debugger reaches the conclusion
13915      that the object file is stripped and has no debugging information.
13916      To get the MIPS/SGI debugger to believe that there is debugging
13917      information in the object file, we add a -g to the producer string.  */
13918   if (debug_info_level > DINFO_LEVEL_TERSE)
13919     strcat (producer, " -g");
13920 #endif
13921
13922   add_AT_string (die, DW_AT_producer, producer);
13923
13924   if (strcmp (language_string, "GNU C++") == 0)
13925     language = DW_LANG_C_plus_plus;
13926   else if (strcmp (language_string, "GNU Ada") == 0)
13927     language = DW_LANG_Ada95;
13928   else if (strcmp (language_string, "GNU F77") == 0)
13929     language = DW_LANG_Fortran77;
13930   else if (strcmp (language_string, "GNU Fortran") == 0)
13931     language = DW_LANG_Fortran95;
13932   else if (strcmp (language_string, "GNU Pascal") == 0)
13933     language = DW_LANG_Pascal83;
13934   else if (strcmp (language_string, "GNU Java") == 0)
13935     language = DW_LANG_Java;
13936   else if (strcmp (language_string, "GNU Objective-C") == 0)
13937     language = DW_LANG_ObjC;
13938   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13939     language = DW_LANG_ObjC_plus_plus;
13940   else
13941     language = DW_LANG_C89;
13942
13943   add_AT_unsigned (die, DW_AT_language, language);
13944   return die;
13945 }
13946
13947 /* Generate the DIE for a base class.  */
13948
13949 static void
13950 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13951 {
13952   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13953
13954   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13955   add_data_member_location_attribute (die, binfo);
13956
13957   if (BINFO_VIRTUAL_P (binfo))
13958     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13959
13960   if (access == access_public_node)
13961     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13962   else if (access == access_protected_node)
13963     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13964 }
13965
13966 /* Generate a DIE for a class member.  */
13967
13968 static void
13969 gen_member_die (tree type, dw_die_ref context_die)
13970 {
13971   tree member;
13972   tree binfo = TYPE_BINFO (type);
13973   dw_die_ref child;
13974
13975   /* If this is not an incomplete type, output descriptions of each of its
13976      members. Note that as we output the DIEs necessary to represent the
13977      members of this record or union type, we will also be trying to output
13978      DIEs to represent the *types* of those members. However the `type'
13979      function (above) will specifically avoid generating type DIEs for member
13980      types *within* the list of member DIEs for this (containing) type except
13981      for those types (of members) which are explicitly marked as also being
13982      members of this (containing) type themselves.  The g++ front- end can
13983      force any given type to be treated as a member of some other (containing)
13984      type by setting the TYPE_CONTEXT of the given (member) type to point to
13985      the TREE node representing the appropriate (containing) type.  */
13986
13987   /* First output info about the base classes.  */
13988   if (binfo)
13989     {
13990       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13991       int i;
13992       tree base;
13993
13994       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13995         gen_inheritance_die (base,
13996                              (accesses ? VEC_index (tree, accesses, i)
13997                               : access_public_node), context_die);
13998     }
13999
14000   /* Now output info about the data members and type members.  */
14001   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14002     {
14003       /* If we thought we were generating minimal debug info for TYPE
14004          and then changed our minds, some of the member declarations
14005          may have already been defined.  Don't define them again, but
14006          do put them in the right order.  */
14007
14008       child = lookup_decl_die (member);
14009       if (child)
14010         splice_child_die (context_die, child);
14011       else
14012         gen_decl_die (member, context_die);
14013     }
14014
14015   /* Now output info about the function members (if any).  */
14016   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14017     {
14018       /* Don't include clones in the member list.  */
14019       if (DECL_ABSTRACT_ORIGIN (member))
14020         continue;
14021
14022       child = lookup_decl_die (member);
14023       if (child)
14024         splice_child_die (context_die, child);
14025       else
14026         gen_decl_die (member, context_die);
14027     }
14028 }
14029
14030 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14031    is set, we pretend that the type was never defined, so we only get the
14032    member DIEs needed by later specification DIEs.  */
14033
14034 static void
14035 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14036                                 enum debug_info_usage usage)
14037 {
14038   dw_die_ref type_die = lookup_type_die (type);
14039   dw_die_ref scope_die = 0;
14040   int nested = 0;
14041   int complete = (TYPE_SIZE (type)
14042                   && (! TYPE_STUB_DECL (type)
14043                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14044   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14045   complete = complete && should_emit_struct_debug (type, usage);
14046
14047   if (type_die && ! complete)
14048     return;
14049
14050   if (TYPE_CONTEXT (type) != NULL_TREE
14051       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14052           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14053     nested = 1;
14054
14055   scope_die = scope_die_for (type, context_die);
14056
14057   if (! type_die || (nested && scope_die == comp_unit_die))
14058     /* First occurrence of type or toplevel definition of nested class.  */
14059     {
14060       dw_die_ref old_die = type_die;
14061
14062       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14063                           ? record_type_tag (type) : DW_TAG_union_type,
14064                           scope_die, type);
14065       equate_type_number_to_die (type, type_die);
14066       if (old_die)
14067         add_AT_specification (type_die, old_die);
14068       else
14069         add_name_attribute (type_die, type_tag (type));
14070     }
14071   else
14072     remove_AT (type_die, DW_AT_declaration);
14073
14074   /* If this type has been completed, then give it a byte_size attribute and
14075      then give a list of members.  */
14076   if (complete && !ns_decl)
14077     {
14078       /* Prevent infinite recursion in cases where the type of some member of
14079          this type is expressed in terms of this type itself.  */
14080       TREE_ASM_WRITTEN (type) = 1;
14081       add_byte_size_attribute (type_die, type);
14082       if (TYPE_STUB_DECL (type) != NULL_TREE)
14083         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14084
14085       /* If the first reference to this type was as the return type of an
14086          inline function, then it may not have a parent.  Fix this now.  */
14087       if (type_die->die_parent == NULL)
14088         add_child_die (scope_die, type_die);
14089
14090       push_decl_scope (type);
14091       gen_member_die (type, type_die);
14092       pop_decl_scope ();
14093
14094       /* GNU extension: Record what type our vtable lives in.  */
14095       if (TYPE_VFIELD (type))
14096         {
14097           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14098
14099           gen_type_die (vtype, context_die);
14100           add_AT_die_ref (type_die, DW_AT_containing_type,
14101                           lookup_type_die (vtype));
14102         }
14103     }
14104   else
14105     {
14106       add_AT_flag (type_die, DW_AT_declaration, 1);
14107
14108       /* We don't need to do this for function-local types.  */
14109       if (TYPE_STUB_DECL (type)
14110           && ! decl_function_context (TYPE_STUB_DECL (type)))
14111         VEC_safe_push (tree, gc, incomplete_types, type);
14112     }
14113
14114   if (get_AT (type_die, DW_AT_name))
14115     add_pubtype (type, type_die);
14116 }
14117
14118 /* Generate a DIE for a subroutine _type_.  */
14119
14120 static void
14121 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14122 {
14123   tree return_type = TREE_TYPE (type);
14124   dw_die_ref subr_die
14125     = new_die (DW_TAG_subroutine_type,
14126                scope_die_for (type, context_die), type);
14127
14128   equate_type_number_to_die (type, subr_die);
14129   add_prototyped_attribute (subr_die, type);
14130   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14131   gen_formal_types_die (type, subr_die);
14132
14133   if (get_AT (subr_die, DW_AT_name))
14134     add_pubtype (type, subr_die);
14135 }
14136
14137 /* Generate a DIE for a type definition.  */
14138
14139 static void
14140 gen_typedef_die (tree decl, dw_die_ref context_die)
14141 {
14142   dw_die_ref type_die;
14143   tree origin;
14144
14145   if (TREE_ASM_WRITTEN (decl))
14146     return;
14147
14148   TREE_ASM_WRITTEN (decl) = 1;
14149   type_die = new_die (DW_TAG_typedef, context_die, decl);
14150   origin = decl_ultimate_origin (decl);
14151   if (origin != NULL)
14152     add_abstract_origin_attribute (type_die, origin);
14153   else
14154     {
14155       tree type;
14156
14157       add_name_and_src_coords_attributes (type_die, decl);
14158       if (DECL_ORIGINAL_TYPE (decl))
14159         {
14160           type = DECL_ORIGINAL_TYPE (decl);
14161
14162           gcc_assert (type != TREE_TYPE (decl));
14163           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14164         }
14165       else
14166         type = TREE_TYPE (decl);
14167
14168       add_type_attribute (type_die, type, TREE_READONLY (decl),
14169                           TREE_THIS_VOLATILE (decl), context_die);
14170     }
14171
14172   if (DECL_ABSTRACT (decl))
14173     equate_decl_number_to_die (decl, type_die);
14174
14175   if (get_AT (type_die, DW_AT_name))
14176     add_pubtype (decl, type_die);
14177 }
14178
14179 /* Generate a type description DIE.  */
14180
14181 static void
14182 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14183                                 enum debug_info_usage usage)
14184 {
14185   int need_pop;
14186   struct array_descr_info info;
14187
14188   if (type == NULL_TREE || type == error_mark_node)
14189     return;
14190
14191   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14192       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14193     {
14194       if (TREE_ASM_WRITTEN (type))
14195         return;
14196
14197       /* Prevent broken recursion; we can't hand off to the same type.  */
14198       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14199
14200       TREE_ASM_WRITTEN (type) = 1;
14201       gen_decl_die (TYPE_NAME (type), context_die);
14202       return;
14203     }
14204
14205   /* If this is an array type with hidden descriptor, handle it first.  */
14206   if (!TREE_ASM_WRITTEN (type)
14207       && lang_hooks.types.get_array_descr_info
14208       && lang_hooks.types.get_array_descr_info (type, &info))
14209     {
14210       gen_descr_array_type_die (type, &info, context_die);
14211       TREE_ASM_WRITTEN (type) = 1;
14212       return;
14213     }
14214
14215   /* We are going to output a DIE to represent the unqualified version
14216      of this type (i.e. without any const or volatile qualifiers) so
14217      get the main variant (i.e. the unqualified version) of this type
14218      now.  (Vectors are special because the debugging info is in the
14219      cloned type itself).  */
14220   if (TREE_CODE (type) != VECTOR_TYPE)
14221     type = type_main_variant (type);
14222
14223   if (TREE_ASM_WRITTEN (type))
14224     return;
14225
14226   switch (TREE_CODE (type))
14227     {
14228     case ERROR_MARK:
14229       break;
14230
14231     case POINTER_TYPE:
14232     case REFERENCE_TYPE:
14233       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14234          ensures that the gen_type_die recursion will terminate even if the
14235          type is recursive.  Recursive types are possible in Ada.  */
14236       /* ??? We could perhaps do this for all types before the switch
14237          statement.  */
14238       TREE_ASM_WRITTEN (type) = 1;
14239
14240       /* For these types, all that is required is that we output a DIE (or a
14241          set of DIEs) to represent the "basis" type.  */
14242       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14243                                 DINFO_USAGE_IND_USE);
14244       break;
14245
14246     case OFFSET_TYPE:
14247       /* This code is used for C++ pointer-to-data-member types.
14248          Output a description of the relevant class type.  */
14249       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14250                                         DINFO_USAGE_IND_USE);
14251
14252       /* Output a description of the type of the object pointed to.  */
14253       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14254                                         DINFO_USAGE_IND_USE);
14255
14256       /* Now output a DIE to represent this pointer-to-data-member type
14257          itself.  */
14258       gen_ptr_to_mbr_type_die (type, context_die);
14259       break;
14260
14261     case FUNCTION_TYPE:
14262       /* Force out return type (in case it wasn't forced out already).  */
14263       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14264                                         DINFO_USAGE_DIR_USE);
14265       gen_subroutine_type_die (type, context_die);
14266       break;
14267
14268     case METHOD_TYPE:
14269       /* Force out return type (in case it wasn't forced out already).  */
14270       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14271                                         DINFO_USAGE_DIR_USE);
14272       gen_subroutine_type_die (type, context_die);
14273       break;
14274
14275     case ARRAY_TYPE:
14276       gen_array_type_die (type, context_die);
14277       break;
14278
14279     case VECTOR_TYPE:
14280       gen_array_type_die (type, context_die);
14281       break;
14282
14283     case ENUMERAL_TYPE:
14284     case RECORD_TYPE:
14285     case UNION_TYPE:
14286     case QUAL_UNION_TYPE:
14287       /* If this is a nested type whose containing class hasn't been written
14288          out yet, writing it out will cover this one, too.  This does not apply
14289          to instantiations of member class templates; they need to be added to
14290          the containing class as they are generated.  FIXME: This hurts the
14291          idea of combining type decls from multiple TUs, since we can't predict
14292          what set of template instantiations we'll get.  */
14293       if (TYPE_CONTEXT (type)
14294           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14295           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14296         {
14297           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14298
14299           if (TREE_ASM_WRITTEN (type))
14300             return;
14301
14302           /* If that failed, attach ourselves to the stub.  */
14303           push_decl_scope (TYPE_CONTEXT (type));
14304           context_die = lookup_type_die (TYPE_CONTEXT (type));
14305           need_pop = 1;
14306         }
14307       else
14308         {
14309           declare_in_namespace (type, context_die);
14310           need_pop = 0;
14311         }
14312
14313       if (TREE_CODE (type) == ENUMERAL_TYPE)
14314         {
14315           /* This might have been written out by the call to
14316              declare_in_namespace.  */
14317           if (!TREE_ASM_WRITTEN (type))
14318             gen_enumeration_type_die (type, context_die);
14319         }
14320       else
14321         gen_struct_or_union_type_die (type, context_die, usage);
14322
14323       if (need_pop)
14324         pop_decl_scope ();
14325
14326       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14327          it up if it is ever completed.  gen_*_type_die will set it for us
14328          when appropriate.  */
14329       return;
14330
14331     case VOID_TYPE:
14332     case INTEGER_TYPE:
14333     case REAL_TYPE:
14334     case FIXED_POINT_TYPE:
14335     case COMPLEX_TYPE:
14336     case BOOLEAN_TYPE:
14337       /* No DIEs needed for fundamental types.  */
14338       break;
14339
14340     case LANG_TYPE:
14341       /* No Dwarf representation currently defined.  */
14342       break;
14343
14344     default:
14345       gcc_unreachable ();
14346     }
14347
14348   TREE_ASM_WRITTEN (type) = 1;
14349 }
14350
14351 static void
14352 gen_type_die (tree type, dw_die_ref context_die)
14353 {
14354   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14355 }
14356
14357 /* Generate a DIE for a tagged type instantiation.  */
14358
14359 static void
14360 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
14361 {
14362   if (type == NULL_TREE || type == error_mark_node)
14363     return;
14364
14365   /* We are going to output a DIE to represent the unqualified version of
14366      this type (i.e. without any const or volatile qualifiers) so make sure
14367      that we have the main variant (i.e. the unqualified version) of this
14368      type now.  */
14369   gcc_assert (type == type_main_variant (type));
14370
14371   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
14372      an instance of an unresolved type.  */
14373
14374   switch (TREE_CODE (type))
14375     {
14376     case ERROR_MARK:
14377       break;
14378
14379     case ENUMERAL_TYPE:
14380       gen_inlined_enumeration_type_die (type, context_die);
14381       break;
14382
14383     case RECORD_TYPE:
14384       gen_inlined_structure_type_die (type, context_die);
14385       break;
14386
14387     case UNION_TYPE:
14388     case QUAL_UNION_TYPE:
14389       gen_inlined_union_type_die (type, context_die);
14390       break;
14391
14392     default:
14393       gcc_unreachable ();
14394     }
14395 }
14396
14397 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14398    things which are local to the given block.  */
14399
14400 static void
14401 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14402 {
14403   int must_output_die = 0;
14404   tree origin;
14405   tree decl;
14406   enum tree_code origin_code;
14407
14408   /* Ignore blocks that are NULL.  */
14409   if (stmt == NULL_TREE)
14410     return;
14411
14412   /* If the block is one fragment of a non-contiguous block, do not
14413      process the variables, since they will have been done by the
14414      origin block.  Do process subblocks.  */
14415   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14416     {
14417       tree sub;
14418
14419       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14420         gen_block_die (sub, context_die, depth + 1);
14421
14422       return;
14423     }
14424
14425   /* Determine the "ultimate origin" of this block.  This block may be an
14426      inlined instance of an inlined instance of inline function, so we have
14427      to trace all of the way back through the origin chain to find out what
14428      sort of node actually served as the original seed for the creation of
14429      the current block.  */
14430   origin = block_ultimate_origin (stmt);
14431   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
14432
14433   /* Determine if we need to output any Dwarf DIEs at all to represent this
14434      block.  */
14435   if (origin_code == FUNCTION_DECL)
14436     /* The outer scopes for inlinings *must* always be represented.  We
14437        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14438     must_output_die = 1;
14439   else
14440     {
14441       /* In the case where the current block represents an inlining of the
14442          "body block" of an inline function, we must *NOT* output any DIE for
14443          this block because we have already output a DIE to represent the whole
14444          inlined function scope and the "body block" of any function doesn't
14445          really represent a different scope according to ANSI C rules.  So we
14446          check here to make sure that this block does not represent a "body
14447          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
14448       if (! is_body_block (origin ? origin : stmt))
14449         {
14450           /* Determine if this block directly contains any "significant"
14451              local declarations which we will need to output DIEs for.  */
14452           if (debug_info_level > DINFO_LEVEL_TERSE)
14453             /* We are not in terse mode so *any* local declaration counts
14454                as being a "significant" one.  */
14455             must_output_die = (BLOCK_VARS (stmt) != NULL
14456                                && (TREE_USED (stmt)
14457                                    || TREE_ASM_WRITTEN (stmt)
14458                                    || BLOCK_ABSTRACT (stmt)));
14459           else
14460             /* We are in terse mode, so only local (nested) function
14461                definitions count as "significant" local declarations.  */
14462             for (decl = BLOCK_VARS (stmt);
14463                  decl != NULL; decl = TREE_CHAIN (decl))
14464               if (TREE_CODE (decl) == FUNCTION_DECL
14465                   && DECL_INITIAL (decl))
14466                 {
14467                   must_output_die = 1;
14468                   break;
14469                 }
14470         }
14471     }
14472
14473   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14474      DIE for any block which contains no significant local declarations at
14475      all.  Rather, in such cases we just call `decls_for_scope' so that any
14476      needed Dwarf info for any sub-blocks will get properly generated. Note
14477      that in terse mode, our definition of what constitutes a "significant"
14478      local declaration gets restricted to include only inlined function
14479      instances and local (nested) function definitions.  */
14480   if (must_output_die)
14481     {
14482       if (origin_code == FUNCTION_DECL)
14483         gen_inlined_subroutine_die (stmt, context_die, depth);
14484       else
14485         gen_lexical_block_die (stmt, context_die, depth);
14486     }
14487   else
14488     decls_for_scope (stmt, context_die, depth);
14489 }
14490
14491 /* Generate all of the decls declared within a given scope and (recursively)
14492    all of its sub-blocks.  */
14493
14494 static void
14495 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14496 {
14497   tree decl;
14498   tree subblocks;
14499
14500   /* Ignore NULL blocks.  */
14501   if (stmt == NULL_TREE)
14502     return;
14503
14504   if (TREE_USED (stmt))
14505     {
14506       /* Output the DIEs to represent all of the data objects and typedefs
14507          declared directly within this block but not within any nested
14508          sub-blocks.  Also, nested function and tag DIEs have been
14509          generated with a parent of NULL; fix that up now.  */
14510       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14511         {
14512           dw_die_ref die;
14513
14514           if (TREE_CODE (decl) == FUNCTION_DECL)
14515             die = lookup_decl_die (decl);
14516           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
14517             die = lookup_type_die (TREE_TYPE (decl));
14518           else
14519             die = NULL;
14520
14521           if (die != NULL && die->die_parent == NULL)
14522             add_child_die (context_die, die);
14523           /* Do not produce debug information for static variables since
14524              these might be optimized out.  We are called for these later
14525              in varpool_analyze_pending_decls.
14526
14527              But *do* produce it for Fortran COMMON variables because,
14528              even though they are static, their names can differ depending
14529              on the scope, which we need to preserve.  */
14530           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
14531               && !(is_fortran () && TREE_PUBLIC (decl)))
14532             ;
14533           else
14534             gen_decl_die (decl, context_die);
14535         }
14536     }
14537
14538   /* If we're at -g1, we're not interested in subblocks.  */
14539   if (debug_info_level <= DINFO_LEVEL_TERSE)
14540     return;
14541
14542   /* Output the DIEs to represent all sub-blocks (and the items declared
14543      therein) of this block.  */
14544   for (subblocks = BLOCK_SUBBLOCKS (stmt);
14545        subblocks != NULL;
14546        subblocks = BLOCK_CHAIN (subblocks))
14547     gen_block_die (subblocks, context_die, depth + 1);
14548 }
14549
14550 /* Is this a typedef we can avoid emitting?  */
14551
14552 static inline int
14553 is_redundant_typedef (const_tree decl)
14554 {
14555   if (TYPE_DECL_IS_STUB (decl))
14556     return 1;
14557
14558   if (DECL_ARTIFICIAL (decl)
14559       && DECL_CONTEXT (decl)
14560       && is_tagged_type (DECL_CONTEXT (decl))
14561       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
14562       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
14563     /* Also ignore the artificial member typedef for the class name.  */
14564     return 1;
14565
14566   return 0;
14567 }
14568
14569 /* Returns the DIE for a context.  */
14570
14571 static inline dw_die_ref
14572 get_context_die (tree context)
14573 {
14574   if (context)
14575     {
14576       /* Find die that represents this context.  */
14577       if (TYPE_P (context))
14578         return force_type_die (context);
14579       else
14580         return force_decl_die (context);
14581     }
14582   return comp_unit_die;
14583 }
14584
14585 /* Returns the DIE for decl.  A DIE will always be returned.  */
14586
14587 static dw_die_ref
14588 force_decl_die (tree decl)
14589 {
14590   dw_die_ref decl_die;
14591   unsigned saved_external_flag;
14592   tree save_fn = NULL_TREE;
14593   decl_die = lookup_decl_die (decl);
14594   if (!decl_die)
14595     {
14596       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
14597
14598       decl_die = lookup_decl_die (decl);
14599       if (decl_die)
14600         return decl_die;
14601
14602       switch (TREE_CODE (decl))
14603         {
14604         case FUNCTION_DECL:
14605           /* Clear current_function_decl, so that gen_subprogram_die thinks
14606              that this is a declaration. At this point, we just want to force
14607              declaration die.  */
14608           save_fn = current_function_decl;
14609           current_function_decl = NULL_TREE;
14610           gen_subprogram_die (decl, context_die);
14611           current_function_decl = save_fn;
14612           break;
14613
14614         case VAR_DECL:
14615           /* Set external flag to force declaration die. Restore it after
14616            gen_decl_die() call.  */
14617           saved_external_flag = DECL_EXTERNAL (decl);
14618           DECL_EXTERNAL (decl) = 1;
14619           gen_decl_die (decl, context_die);
14620           DECL_EXTERNAL (decl) = saved_external_flag;
14621           break;
14622
14623         case NAMESPACE_DECL:
14624           dwarf2out_decl (decl);
14625           break;
14626
14627         default:
14628           gcc_unreachable ();
14629         }
14630
14631       /* We should be able to find the DIE now.  */
14632       if (!decl_die)
14633         decl_die = lookup_decl_die (decl);
14634       gcc_assert (decl_die);
14635     }
14636
14637   return decl_die;
14638 }
14639
14640 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
14641    always returned.  */
14642
14643 static dw_die_ref
14644 force_type_die (tree type)
14645 {
14646   dw_die_ref type_die;
14647
14648   type_die = lookup_type_die (type);
14649   if (!type_die)
14650     {
14651       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
14652
14653       type_die = modified_type_die (type, TYPE_READONLY (type),
14654                                     TYPE_VOLATILE (type), context_die);
14655       gcc_assert (type_die);
14656     }
14657   return type_die;
14658 }
14659
14660 /* Force out any required namespaces to be able to output DECL,
14661    and return the new context_die for it, if it's changed.  */
14662
14663 static dw_die_ref
14664 setup_namespace_context (tree thing, dw_die_ref context_die)
14665 {
14666   tree context = (DECL_P (thing)
14667                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
14668   if (context && TREE_CODE (context) == NAMESPACE_DECL)
14669     /* Force out the namespace.  */
14670     context_die = force_decl_die (context);
14671
14672   return context_die;
14673 }
14674
14675 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
14676    type) within its namespace, if appropriate.
14677
14678    For compatibility with older debuggers, namespace DIEs only contain
14679    declarations; all definitions are emitted at CU scope.  */
14680
14681 static void
14682 declare_in_namespace (tree thing, dw_die_ref context_die)
14683 {
14684   dw_die_ref ns_context;
14685
14686   if (debug_info_level <= DINFO_LEVEL_TERSE)
14687     return;
14688
14689   /* If this decl is from an inlined function, then don't try to emit it in its
14690      namespace, as we will get confused.  It would have already been emitted
14691      when the abstract instance of the inline function was emitted anyways.  */
14692   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
14693     return;
14694
14695   ns_context = setup_namespace_context (thing, context_die);
14696
14697   if (ns_context != context_die)
14698     {
14699       if (DECL_P (thing))
14700         gen_decl_die (thing, ns_context);
14701       else
14702         gen_type_die (thing, ns_context);
14703     }
14704 }
14705
14706 /* Generate a DIE for a namespace or namespace alias.  */
14707
14708 static void
14709 gen_namespace_die (tree decl)
14710 {
14711   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
14712
14713   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
14714      they are an alias of.  */
14715   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
14716     {
14717       /* Output a real namespace.  */
14718       dw_die_ref namespace_die
14719         = new_die (DW_TAG_namespace, context_die, decl);
14720       add_name_and_src_coords_attributes (namespace_die, decl);
14721       equate_decl_number_to_die (decl, namespace_die);
14722     }
14723   else
14724     {
14725       /* Output a namespace alias.  */
14726
14727       /* Force out the namespace we are an alias of, if necessary.  */
14728       dw_die_ref origin_die
14729         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
14730
14731       /* Now create the namespace alias DIE.  */
14732       dw_die_ref namespace_die
14733         = new_die (DW_TAG_imported_declaration, context_die, decl);
14734       add_name_and_src_coords_attributes (namespace_die, decl);
14735       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
14736       equate_decl_number_to_die (decl, namespace_die);
14737     }
14738 }
14739
14740 /* Generate Dwarf debug information for a decl described by DECL.  */
14741
14742 static void
14743 gen_decl_die (tree decl, dw_die_ref context_die)
14744 {
14745   tree origin;
14746
14747   if (DECL_P (decl) && DECL_IGNORED_P (decl))
14748     return;
14749
14750   switch (TREE_CODE (decl))
14751     {
14752     case ERROR_MARK:
14753       break;
14754
14755     case CONST_DECL:
14756       /* The individual enumerators of an enum type get output when we output
14757          the Dwarf representation of the relevant enum type itself.  */
14758       break;
14759
14760     case FUNCTION_DECL:
14761       /* Don't output any DIEs to represent mere function declarations,
14762          unless they are class members or explicit block externs.  */
14763       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
14764           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
14765         break;
14766
14767 #if 0
14768       /* FIXME */
14769       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
14770          on local redeclarations of global functions.  That seems broken.  */
14771       if (current_function_decl != decl)
14772         /* This is only a declaration.  */;
14773 #endif
14774
14775       /* If we're emitting a clone, emit info for the abstract instance.  */
14776       if (DECL_ORIGIN (decl) != decl)
14777         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14778
14779       /* If we're emitting an out-of-line copy of an inline function,
14780          emit info for the abstract instance and set up to refer to it.  */
14781       else if (cgraph_function_possibly_inlined_p (decl)
14782                && ! DECL_ABSTRACT (decl)
14783                && ! class_or_namespace_scope_p (context_die)
14784                /* dwarf2out_abstract_function won't emit a die if this is just
14785                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
14786                   that case, because that works only if we have a die.  */
14787                && DECL_INITIAL (decl) != NULL_TREE)
14788         {
14789           dwarf2out_abstract_function (decl);
14790           set_decl_origin_self (decl);
14791         }
14792
14793       /* Otherwise we're emitting the primary DIE for this decl.  */
14794       else if (debug_info_level > DINFO_LEVEL_TERSE)
14795         {
14796           /* Before we describe the FUNCTION_DECL itself, make sure that we
14797              have described its return type.  */
14798           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14799
14800           /* And its virtual context.  */
14801           if (DECL_VINDEX (decl) != NULL_TREE)
14802             gen_type_die (DECL_CONTEXT (decl), context_die);
14803
14804           /* And its containing type.  */
14805           origin = decl_class_context (decl);
14806           if (origin != NULL_TREE)
14807             gen_type_die_for_member (origin, decl, context_die);
14808
14809           /* And its containing namespace.  */
14810           declare_in_namespace (decl, context_die);
14811         }
14812
14813       /* Now output a DIE to represent the function itself.  */
14814       gen_subprogram_die (decl, context_die);
14815       break;
14816
14817     case TYPE_DECL:
14818       /* If we are in terse mode, don't generate any DIEs to represent any
14819          actual typedefs.  */
14820       if (debug_info_level <= DINFO_LEVEL_TERSE)
14821         break;
14822
14823       /* In the special case of a TYPE_DECL node representing the declaration
14824          of some type tag, if the given TYPE_DECL is marked as having been
14825          instantiated from some other (original) TYPE_DECL node (e.g. one which
14826          was generated within the original definition of an inline function) we
14827          have to generate a special (abbreviated) DW_TAG_structure_type,
14828          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
14829       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14830           && is_tagged_type (TREE_TYPE (decl)))
14831         {
14832           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14833           break;
14834         }
14835
14836       if (is_redundant_typedef (decl))
14837         gen_type_die (TREE_TYPE (decl), context_die);
14838       else
14839         /* Output a DIE to represent the typedef itself.  */
14840         gen_typedef_die (decl, context_die);
14841       break;
14842
14843     case LABEL_DECL:
14844       if (debug_info_level >= DINFO_LEVEL_NORMAL)
14845         gen_label_die (decl, context_die);
14846       break;
14847
14848     case VAR_DECL:
14849     case RESULT_DECL:
14850       /* If we are in terse mode, don't generate any DIEs to represent any
14851          variable declarations or definitions.  */
14852       if (debug_info_level <= DINFO_LEVEL_TERSE)
14853         break;
14854
14855       /* If this is the global definition of the Fortran COMMON block, we don't
14856          need to do anything.  Syntactically, the block itself has no identity,
14857          just its constituent identifiers.  */
14858       if (TREE_CODE (decl) == VAR_DECL
14859           && TREE_PUBLIC (decl)
14860           && TREE_STATIC (decl)
14861           && is_fortran ()
14862           && !DECL_HAS_VALUE_EXPR_P (decl))
14863         break;
14864
14865       /* Output any DIEs that are needed to specify the type of this data
14866          object.  */
14867       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14868         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14869       else
14870         gen_type_die (TREE_TYPE (decl), context_die);
14871
14872       /* And its containing type.  */
14873       origin = decl_class_context (decl);
14874       if (origin != NULL_TREE)
14875         gen_type_die_for_member (origin, decl, context_die);
14876
14877       /* And its containing namespace.  */
14878       declare_in_namespace (decl, context_die);
14879
14880       /* Now output the DIE to represent the data object itself.  This gets
14881          complicated because of the possibility that the VAR_DECL really
14882          represents an inlined instance of a formal parameter for an inline
14883          function.  */
14884       origin = decl_ultimate_origin (decl);
14885       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14886         gen_formal_parameter_die (decl, context_die);
14887       else
14888         gen_variable_die (decl, context_die);
14889       break;
14890
14891     case FIELD_DECL:
14892       /* Ignore the nameless fields that are used to skip bits but handle C++
14893          anonymous unions and structs.  */
14894       if (DECL_NAME (decl) != NULL_TREE
14895           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14896           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14897         {
14898           gen_type_die (member_declared_type (decl), context_die);
14899           gen_field_die (decl, context_die);
14900         }
14901       break;
14902
14903     case PARM_DECL:
14904       if (DECL_BY_REFERENCE (decl))
14905         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14906       else
14907         gen_type_die (TREE_TYPE (decl), context_die);
14908       gen_formal_parameter_die (decl, context_die);
14909       break;
14910
14911     case NAMESPACE_DECL:
14912       gen_namespace_die (decl);
14913       break;
14914
14915     default:
14916       /* Probably some frontend-internal decl.  Assume we don't care.  */
14917       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14918       break;
14919     }
14920 }
14921 \f
14922 /* Output debug information for global decl DECL.  Called from toplev.c after
14923    compilation proper has finished.  */
14924
14925 static void
14926 dwarf2out_global_decl (tree decl)
14927 {
14928   /* Output DWARF2 information for file-scope tentative data object
14929      declarations, file-scope (extern) function declarations (which
14930      had no corresponding body) and file-scope tagged type declarations
14931      and definitions which have not yet been forced out.
14932
14933      Ignore the global decl of any Fortran COMMON blocks which also
14934      wind up here though they have already been described in the local
14935      scope for the procedures using them.  */
14936   if (TREE_CODE (decl) == VAR_DECL
14937       && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14938     return;
14939
14940   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14941     dwarf2out_decl (decl);
14942 }
14943
14944 /* Output debug information for type decl DECL.  Called from toplev.c
14945    and from language front ends (to record built-in types).  */
14946 static void
14947 dwarf2out_type_decl (tree decl, int local)
14948 {
14949   if (!local)
14950     dwarf2out_decl (decl);
14951 }
14952
14953 /* Output debug information for imported module or decl.  */
14954
14955 static void
14956 dwarf2out_imported_module_or_decl (tree decl, tree context)
14957 {
14958   dw_die_ref imported_die, at_import_die;
14959   dw_die_ref scope_die;
14960   expanded_location xloc;
14961
14962   if (debug_info_level <= DINFO_LEVEL_TERSE)
14963     return;
14964
14965   gcc_assert (decl);
14966
14967   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14968      We need decl DIE for reference and scope die. First, get DIE for the decl
14969      itself.  */
14970
14971   /* Get the scope die for decl context. Use comp_unit_die for global module
14972      or decl. If die is not found for non globals, force new die.  */
14973   if (context
14974       && TYPE_P (context)
14975       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14976     return;
14977   scope_die = get_context_die (context);
14978
14979   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14980   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14981     {
14982       if (is_base_type (TREE_TYPE (decl)))
14983         at_import_die = base_type_die (TREE_TYPE (decl));
14984       else
14985         at_import_die = force_type_die (TREE_TYPE (decl));
14986       /* For namespace N { typedef void T; } using N::T; base_type_die
14987          returns NULL, but DW_TAG_imported_declaration requires
14988          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
14989       if (!at_import_die)
14990         {
14991           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
14992           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
14993           at_import_die = lookup_type_die (TREE_TYPE (decl));
14994           gcc_assert (at_import_die);
14995         }
14996     }
14997   else
14998     {
14999       at_import_die = lookup_decl_die (decl);
15000       if (!at_import_die)
15001         {
15002           /* If we're trying to avoid duplicate debug info, we may not have
15003              emitted the member decl for this field.  Emit it now.  */
15004           if (TREE_CODE (decl) == FIELD_DECL)
15005             {
15006               tree type = DECL_CONTEXT (decl);
15007
15008               if (TYPE_CONTEXT (type)
15009                   && TYPE_P (TYPE_CONTEXT (type))
15010                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15011                                                 DINFO_USAGE_DIR_USE))
15012                 return;
15013               gen_type_die_for_member (type, decl,
15014                                        get_context_die (TYPE_CONTEXT (type)));
15015             }
15016           at_import_die = force_decl_die (decl);
15017         }
15018     }
15019
15020   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15021   if (TREE_CODE (decl) == NAMESPACE_DECL)
15022     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
15023   else
15024     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
15025
15026   xloc = expand_location (input_location);
15027   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15028   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15029   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15030 }
15031
15032 /* Write the debugging output for DECL.  */
15033
15034 void
15035 dwarf2out_decl (tree decl)
15036 {
15037   dw_die_ref context_die = comp_unit_die;
15038
15039   switch (TREE_CODE (decl))
15040     {
15041     case ERROR_MARK:
15042       return;
15043
15044     case FUNCTION_DECL:
15045       /* What we would really like to do here is to filter out all mere
15046          file-scope declarations of file-scope functions which are never
15047          referenced later within this translation unit (and keep all of ones
15048          that *are* referenced later on) but we aren't clairvoyant, so we have
15049          no idea which functions will be referenced in the future (i.e. later
15050          on within the current translation unit). So here we just ignore all
15051          file-scope function declarations which are not also definitions.  If
15052          and when the debugger needs to know something about these functions,
15053          it will have to hunt around and find the DWARF information associated
15054          with the definition of the function.
15055
15056          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15057          nodes represent definitions and which ones represent mere
15058          declarations.  We have to check DECL_INITIAL instead. That's because
15059          the C front-end supports some weird semantics for "extern inline"
15060          function definitions.  These can get inlined within the current
15061          translation unit (and thus, we need to generate Dwarf info for their
15062          abstract instances so that the Dwarf info for the concrete inlined
15063          instances can have something to refer to) but the compiler never
15064          generates any out-of-lines instances of such things (despite the fact
15065          that they *are* definitions).
15066
15067          The important point is that the C front-end marks these "extern
15068          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15069          them anyway. Note that the C++ front-end also plays some similar games
15070          for inline function definitions appearing within include files which
15071          also contain `#pragma interface' pragmas.  */
15072       if (DECL_INITIAL (decl) == NULL_TREE)
15073         return;
15074
15075       /* If we're a nested function, initially use a parent of NULL; if we're
15076          a plain function, this will be fixed up in decls_for_scope.  If
15077          we're a method, it will be ignored, since we already have a DIE.  */
15078       if (decl_function_context (decl)
15079           /* But if we're in terse mode, we don't care about scope.  */
15080           && debug_info_level > DINFO_LEVEL_TERSE)
15081         context_die = NULL;
15082       break;
15083
15084     case VAR_DECL:
15085       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15086          declaration and if the declaration was never even referenced from
15087          within this entire compilation unit.  We suppress these DIEs in
15088          order to save space in the .debug section (by eliminating entries
15089          which are probably useless).  Note that we must not suppress
15090          block-local extern declarations (whether used or not) because that
15091          would screw-up the debugger's name lookup mechanism and cause it to
15092          miss things which really ought to be in scope at a given point.  */
15093       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15094         return;
15095
15096       /* For local statics lookup proper context die.  */
15097       if (TREE_STATIC (decl) && decl_function_context (decl))
15098         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15099
15100       /* If we are in terse mode, don't generate any DIEs to represent any
15101          variable declarations or definitions.  */
15102       if (debug_info_level <= DINFO_LEVEL_TERSE)
15103         return;
15104       break;
15105
15106     case NAMESPACE_DECL:
15107       if (debug_info_level <= DINFO_LEVEL_TERSE)
15108         return;
15109       if (lookup_decl_die (decl) != NULL)
15110         return;
15111       break;
15112
15113     case TYPE_DECL:
15114       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15115       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15116         return;
15117
15118       /* Don't bother trying to generate any DIEs to represent any of the
15119          normal built-in types for the language we are compiling.  */
15120       if (DECL_IS_BUILTIN (decl))
15121         {
15122           /* OK, we need to generate one for `bool' so GDB knows what type
15123              comparisons have.  */
15124           if (is_cxx ()
15125               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15126               && ! DECL_IGNORED_P (decl))
15127             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15128
15129           return;
15130         }
15131
15132       /* If we are in terse mode, don't generate any DIEs for types.  */
15133       if (debug_info_level <= DINFO_LEVEL_TERSE)
15134         return;
15135
15136       /* If we're a function-scope tag, initially use a parent of NULL;
15137          this will be fixed up in decls_for_scope.  */
15138       if (decl_function_context (decl))
15139         context_die = NULL;
15140
15141       break;
15142
15143     default:
15144       return;
15145     }
15146
15147   gen_decl_die (decl, context_die);
15148 }
15149
15150 /* Output a marker (i.e. a label) for the beginning of the generated code for
15151    a lexical block.  */
15152
15153 static void
15154 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15155                        unsigned int blocknum)
15156 {
15157   switch_to_section (current_function_section ());
15158   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15159 }
15160
15161 /* Output a marker (i.e. a label) for the end of the generated code for a
15162    lexical block.  */
15163
15164 static void
15165 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15166 {
15167   switch_to_section (current_function_section ());
15168   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15169 }
15170
15171 /* Returns nonzero if it is appropriate not to emit any debugging
15172    information for BLOCK, because it doesn't contain any instructions.
15173
15174    Don't allow this for blocks with nested functions or local classes
15175    as we would end up with orphans, and in the presence of scheduling
15176    we may end up calling them anyway.  */
15177
15178 static bool
15179 dwarf2out_ignore_block (const_tree block)
15180 {
15181   tree decl;
15182
15183   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15184     if (TREE_CODE (decl) == FUNCTION_DECL
15185         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15186       return 0;
15187
15188   return 1;
15189 }
15190
15191 /* Hash table routines for file_hash.  */
15192
15193 static int
15194 file_table_eq (const void *p1_p, const void *p2_p)
15195 {
15196   const struct dwarf_file_data *const p1 =
15197     (const struct dwarf_file_data *) p1_p;
15198   const char *const p2 = (const char *) p2_p;
15199   return strcmp (p1->filename, p2) == 0;
15200 }
15201
15202 static hashval_t
15203 file_table_hash (const void *p_p)
15204 {
15205   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15206   return htab_hash_string (p->filename);
15207 }
15208
15209 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15210    dwarf2out.c) and return its "index".  The index of each (known) filename is
15211    just a unique number which is associated with only that one filename.  We
15212    need such numbers for the sake of generating labels (in the .debug_sfnames
15213    section) and references to those files numbers (in the .debug_srcinfo
15214    and.debug_macinfo sections).  If the filename given as an argument is not
15215    found in our current list, add it to the list and assign it the next
15216    available unique index number.  In order to speed up searches, we remember
15217    the index of the filename was looked up last.  This handles the majority of
15218    all searches.  */
15219
15220 static struct dwarf_file_data *
15221 lookup_filename (const char *file_name)
15222 {
15223   void ** slot;
15224   struct dwarf_file_data * created;
15225
15226   /* Check to see if the file name that was searched on the previous
15227      call matches this file name.  If so, return the index.  */
15228   if (file_table_last_lookup
15229       && (file_name == file_table_last_lookup->filename
15230           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15231     return file_table_last_lookup;
15232
15233   /* Didn't match the previous lookup, search the table.  */
15234   slot = htab_find_slot_with_hash (file_table, file_name,
15235                                    htab_hash_string (file_name), INSERT);
15236   if (*slot)
15237     return (struct dwarf_file_data *) *slot;
15238
15239   created = GGC_NEW (struct dwarf_file_data);
15240   created->filename = file_name;
15241   created->emitted_number = 0;
15242   *slot = created;
15243   return created;
15244 }
15245
15246 /* If the assembler will construct the file table, then translate the compiler
15247    internal file table number into the assembler file table number, and emit
15248    a .file directive if we haven't already emitted one yet.  The file table
15249    numbers are different because we prune debug info for unused variables and
15250    types, which may include filenames.  */
15251
15252 static int
15253 maybe_emit_file (struct dwarf_file_data * fd)
15254 {
15255   if (! fd->emitted_number)
15256     {
15257       if (last_emitted_file)
15258         fd->emitted_number = last_emitted_file->emitted_number + 1;
15259       else
15260         fd->emitted_number = 1;
15261       last_emitted_file = fd;
15262
15263       if (DWARF2_ASM_LINE_DEBUG_INFO)
15264         {
15265           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15266           output_quoted_string (asm_out_file,
15267                                 remap_debug_filename (fd->filename));
15268           fputc ('\n', asm_out_file);
15269         }
15270     }
15271
15272   return fd->emitted_number;
15273 }
15274
15275 /* Called by the final INSN scan whenever we see a var location.  We
15276    use it to drop labels in the right places, and throw the location in
15277    our lookup table.  */
15278
15279 static void
15280 dwarf2out_var_location (rtx loc_note)
15281 {
15282   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15283   struct var_loc_node *newloc;
15284   rtx prev_insn;
15285   static rtx last_insn;
15286   static const char *last_label;
15287   tree decl;
15288
15289   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15290     return;
15291   prev_insn = PREV_INSN (loc_note);
15292
15293   newloc = GGC_CNEW (struct var_loc_node);
15294   /* If the insn we processed last time is the previous insn
15295      and it is also a var location note, use the label we emitted
15296      last time.  */
15297   if (last_insn != NULL_RTX
15298       && last_insn == prev_insn
15299       && NOTE_P (prev_insn)
15300       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15301     {
15302       newloc->label = last_label;
15303     }
15304   else
15305     {
15306       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15307       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15308       loclabel_num++;
15309       newloc->label = ggc_strdup (loclabel);
15310     }
15311   newloc->var_loc_note = loc_note;
15312   newloc->next = NULL;
15313
15314   if (cfun && in_cold_section_p)
15315     newloc->section_label = crtl->subsections.cold_section_label;
15316   else
15317     newloc->section_label = text_section_label;
15318
15319   last_insn = loc_note;
15320   last_label = newloc->label;
15321   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15322   add_var_loc_to_decl (decl, newloc);
15323 }
15324
15325 /* We need to reset the locations at the beginning of each
15326    function. We can't do this in the end_function hook, because the
15327    declarations that use the locations won't have been output when
15328    that hook is called.  Also compute have_multiple_function_sections here.  */
15329
15330 static void
15331 dwarf2out_begin_function (tree fun)
15332 {
15333   htab_empty (decl_loc_table);
15334
15335   if (function_section (fun) != text_section)
15336     have_multiple_function_sections = true;
15337
15338   dwarf2out_note_section_used ();
15339 }
15340
15341 /* Output a label to mark the beginning of a source code line entry
15342    and record information relating to this source line, in
15343    'line_info_table' for later output of the .debug_line section.  */
15344
15345 static void
15346 dwarf2out_source_line (unsigned int line, const char *filename)
15347 {
15348   if (debug_info_level >= DINFO_LEVEL_NORMAL
15349       && line != 0)
15350     {
15351       int file_num = maybe_emit_file (lookup_filename (filename));
15352
15353       switch_to_section (current_function_section ());
15354
15355       /* If requested, emit something human-readable.  */
15356       if (flag_debug_asm)
15357         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15358                  filename, line);
15359
15360       if (DWARF2_ASM_LINE_DEBUG_INFO)
15361         {
15362           /* Emit the .loc directive understood by GNU as.  */
15363           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15364
15365           /* Indicate that line number info exists.  */
15366           line_info_table_in_use++;
15367         }
15368       else if (function_section (current_function_decl) != text_section)
15369         {
15370           dw_separate_line_info_ref line_info;
15371           targetm.asm_out.internal_label (asm_out_file,
15372                                           SEPARATE_LINE_CODE_LABEL,
15373                                           separate_line_info_table_in_use);
15374
15375           /* Expand the line info table if necessary.  */
15376           if (separate_line_info_table_in_use
15377               == separate_line_info_table_allocated)
15378             {
15379               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15380               separate_line_info_table
15381                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15382                                  separate_line_info_table,
15383                                  separate_line_info_table_allocated);
15384               memset (separate_line_info_table
15385                        + separate_line_info_table_in_use,
15386                       0,
15387                       (LINE_INFO_TABLE_INCREMENT
15388                        * sizeof (dw_separate_line_info_entry)));
15389             }
15390
15391           /* Add the new entry at the end of the line_info_table.  */
15392           line_info
15393             = &separate_line_info_table[separate_line_info_table_in_use++];
15394           line_info->dw_file_num = file_num;
15395           line_info->dw_line_num = line;
15396           line_info->function = current_function_funcdef_no;
15397         }
15398       else
15399         {
15400           dw_line_info_ref line_info;
15401
15402           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15403                                      line_info_table_in_use);
15404
15405           /* Expand the line info table if necessary.  */
15406           if (line_info_table_in_use == line_info_table_allocated)
15407             {
15408               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15409               line_info_table
15410                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15411                                  line_info_table_allocated);
15412               memset (line_info_table + line_info_table_in_use, 0,
15413                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15414             }
15415
15416           /* Add the new entry at the end of the line_info_table.  */
15417           line_info = &line_info_table[line_info_table_in_use++];
15418           line_info->dw_file_num = file_num;
15419           line_info->dw_line_num = line;
15420         }
15421     }
15422 }
15423
15424 /* Record the beginning of a new source file.  */
15425
15426 static void
15427 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15428 {
15429   if (flag_eliminate_dwarf2_dups)
15430     {
15431       /* Record the beginning of the file for break_out_includes.  */
15432       dw_die_ref bincl_die;
15433
15434       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15435       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15436     }
15437
15438   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15439     {
15440       int file_num = maybe_emit_file (lookup_filename (filename));
15441
15442       switch_to_section (debug_macinfo_section);
15443       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15444       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15445                                    lineno);
15446
15447       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15448     }
15449 }
15450
15451 /* Record the end of a source file.  */
15452
15453 static void
15454 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15455 {
15456   if (flag_eliminate_dwarf2_dups)
15457     /* Record the end of the file for break_out_includes.  */
15458     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15459
15460   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15461     {
15462       switch_to_section (debug_macinfo_section);
15463       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15464     }
15465 }
15466
15467 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
15468    the tail part of the directive line, i.e. the part which is past the
15469    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15470
15471 static void
15472 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
15473                   const char *buffer ATTRIBUTE_UNUSED)
15474 {
15475   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15476     {
15477       switch_to_section (debug_macinfo_section);
15478       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
15479       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15480       dw2_asm_output_nstring (buffer, -1, "The macro");
15481     }
15482 }
15483
15484 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
15485    the tail part of the directive line, i.e. the part which is past the
15486    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15487
15488 static void
15489 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
15490                  const char *buffer ATTRIBUTE_UNUSED)
15491 {
15492   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15493     {
15494       switch_to_section (debug_macinfo_section);
15495       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
15496       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15497       dw2_asm_output_nstring (buffer, -1, "The macro");
15498     }
15499 }
15500
15501 /* Set up for Dwarf output at the start of compilation.  */
15502
15503 static void
15504 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
15505 {
15506   /* Allocate the file_table.  */
15507   file_table = htab_create_ggc (50, file_table_hash,
15508                                 file_table_eq, NULL);
15509
15510   /* Allocate the decl_die_table.  */
15511   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
15512                                     decl_die_table_eq, NULL);
15513
15514   /* Allocate the decl_loc_table.  */
15515   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
15516                                     decl_loc_table_eq, NULL);
15517
15518   /* Allocate the initial hunk of the decl_scope_table.  */
15519   decl_scope_table = VEC_alloc (tree, gc, 256);
15520
15521   /* Allocate the initial hunk of the abbrev_die_table.  */
15522   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
15523   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
15524   /* Zero-th entry is allocated, but unused.  */
15525   abbrev_die_table_in_use = 1;
15526
15527   /* Allocate the initial hunk of the line_info_table.  */
15528   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
15529   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
15530
15531   /* Zero-th entry is allocated, but unused.  */
15532   line_info_table_in_use = 1;
15533
15534   /* Allocate the pubtypes and pubnames vectors.  */
15535   pubname_table = VEC_alloc (pubname_entry, gc, 32);
15536   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
15537
15538   /* Generate the initial DIE for the .debug section.  Note that the (string)
15539      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
15540      will (typically) be a relative pathname and that this pathname should be
15541      taken as being relative to the directory from which the compiler was
15542      invoked when the given (base) source file was compiled.  We will fill
15543      in this value in dwarf2out_finish.  */
15544   comp_unit_die = gen_compile_unit_die (NULL);
15545
15546   incomplete_types = VEC_alloc (tree, gc, 64);
15547
15548   used_rtx_array = VEC_alloc (rtx, gc, 32);
15549
15550   debug_info_section = get_section (DEBUG_INFO_SECTION,
15551                                     SECTION_DEBUG, NULL);
15552   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
15553                                       SECTION_DEBUG, NULL);
15554   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
15555                                        SECTION_DEBUG, NULL);
15556   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
15557                                        SECTION_DEBUG, NULL);
15558   debug_line_section = get_section (DEBUG_LINE_SECTION,
15559                                     SECTION_DEBUG, NULL);
15560   debug_loc_section = get_section (DEBUG_LOC_SECTION,
15561                                    SECTION_DEBUG, NULL);
15562   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
15563                                         SECTION_DEBUG, NULL);
15564 #ifdef DEBUG_PUBTYPES_SECTION
15565   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
15566                                         SECTION_DEBUG, NULL);
15567 #endif
15568   debug_str_section = get_section (DEBUG_STR_SECTION,
15569                                    DEBUG_STR_SECTION_FLAGS, NULL);
15570   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
15571                                       SECTION_DEBUG, NULL);
15572   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
15573                                      SECTION_DEBUG, NULL);
15574
15575   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
15576   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
15577                                DEBUG_ABBREV_SECTION_LABEL, 0);
15578   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
15579   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
15580                                COLD_TEXT_SECTION_LABEL, 0);
15581   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
15582
15583   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
15584                                DEBUG_INFO_SECTION_LABEL, 0);
15585   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
15586                                DEBUG_LINE_SECTION_LABEL, 0);
15587   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
15588                                DEBUG_RANGES_SECTION_LABEL, 0);
15589   switch_to_section (debug_abbrev_section);
15590   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
15591   switch_to_section (debug_info_section);
15592   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
15593   switch_to_section (debug_line_section);
15594   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
15595
15596   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15597     {
15598       switch_to_section (debug_macinfo_section);
15599       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
15600                                    DEBUG_MACINFO_SECTION_LABEL, 0);
15601       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
15602     }
15603
15604   switch_to_section (text_section);
15605   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
15606   if (flag_reorder_blocks_and_partition)
15607     {
15608       cold_text_section = unlikely_text_section ();
15609       switch_to_section (cold_text_section);
15610       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
15611     }
15612 }
15613
15614 /* A helper function for dwarf2out_finish called through
15615    ht_forall.  Emit one queued .debug_str string.  */
15616
15617 static int
15618 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
15619 {
15620   struct indirect_string_node *node = (struct indirect_string_node *) *h;
15621
15622   if (node->form == DW_FORM_strp)
15623     {
15624       switch_to_section (debug_str_section);
15625       ASM_OUTPUT_LABEL (asm_out_file, node->label);
15626       assemble_string (node->str, strlen (node->str) + 1);
15627     }
15628
15629   return 1;
15630 }
15631
15632 #if ENABLE_ASSERT_CHECKING
15633 /* Verify that all marks are clear.  */
15634
15635 static void
15636 verify_marks_clear (dw_die_ref die)
15637 {
15638   dw_die_ref c;
15639
15640   gcc_assert (! die->die_mark);
15641   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
15642 }
15643 #endif /* ENABLE_ASSERT_CHECKING */
15644
15645 /* Clear the marks for a die and its children.
15646    Be cool if the mark isn't set.  */
15647
15648 static void
15649 prune_unmark_dies (dw_die_ref die)
15650 {
15651   dw_die_ref c;
15652
15653   if (die->die_mark)
15654     die->die_mark = 0;
15655   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
15656 }
15657
15658 /* Given DIE that we're marking as used, find any other dies
15659    it references as attributes and mark them as used.  */
15660
15661 static void
15662 prune_unused_types_walk_attribs (dw_die_ref die)
15663 {
15664   dw_attr_ref a;
15665   unsigned ix;
15666
15667   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15668     {
15669       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
15670         {
15671           /* A reference to another DIE.
15672              Make sure that it will get emitted.  */
15673           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
15674         }
15675       /* Set the string's refcount to 0 so that prune_unused_types_mark
15676          accounts properly for it.  */
15677       if (AT_class (a) == dw_val_class_str)
15678         a->dw_attr_val.v.val_str->refcount = 0;
15679     }
15680 }
15681
15682
15683 /* Mark DIE as being used.  If DOKIDS is true, then walk down
15684    to DIE's children.  */
15685
15686 static void
15687 prune_unused_types_mark (dw_die_ref die, int dokids)
15688 {
15689   dw_die_ref c;
15690
15691   if (die->die_mark == 0)
15692     {
15693       /* We haven't done this node yet.  Mark it as used.  */
15694       die->die_mark = 1;
15695
15696       /* We also have to mark its parents as used.
15697          (But we don't want to mark our parents' kids due to this.)  */
15698       if (die->die_parent)
15699         prune_unused_types_mark (die->die_parent, 0);
15700
15701       /* Mark any referenced nodes.  */
15702       prune_unused_types_walk_attribs (die);
15703
15704       /* If this node is a specification,
15705          also mark the definition, if it exists.  */
15706       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
15707         prune_unused_types_mark (die->die_definition, 1);
15708     }
15709
15710   if (dokids && die->die_mark != 2)
15711     {
15712       /* We need to walk the children, but haven't done so yet.
15713          Remember that we've walked the kids.  */
15714       die->die_mark = 2;
15715
15716       /* If this is an array type, we need to make sure our
15717          kids get marked, even if they're types.  */
15718       if (die->die_tag == DW_TAG_array_type)
15719         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
15720       else
15721         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15722     }
15723 }
15724
15725
15726 /* Walk the tree DIE and mark types that we actually use.  */
15727
15728 static void
15729 prune_unused_types_walk (dw_die_ref die)
15730 {
15731   dw_die_ref c;
15732
15733   /* Don't do anything if this node is already marked.  */
15734   if (die->die_mark)
15735     return;
15736
15737   switch (die->die_tag)
15738     {
15739     case DW_TAG_const_type:
15740     case DW_TAG_packed_type:
15741     case DW_TAG_pointer_type:
15742     case DW_TAG_reference_type:
15743     case DW_TAG_volatile_type:
15744     case DW_TAG_typedef:
15745     case DW_TAG_array_type:
15746     case DW_TAG_structure_type:
15747     case DW_TAG_union_type:
15748     case DW_TAG_class_type:
15749     case DW_TAG_interface_type:
15750     case DW_TAG_friend:
15751     case DW_TAG_variant_part:
15752     case DW_TAG_enumeration_type:
15753     case DW_TAG_subroutine_type:
15754     case DW_TAG_string_type:
15755     case DW_TAG_set_type:
15756     case DW_TAG_subrange_type:
15757     case DW_TAG_ptr_to_member_type:
15758     case DW_TAG_file_type:
15759       if (die->die_perennial_p)
15760         break;
15761
15762       /* It's a type node --- don't mark it.  */
15763       return;
15764
15765     default:
15766       /* Mark everything else.  */
15767       break;
15768   }
15769
15770   die->die_mark = 1;
15771
15772   /* Now, mark any dies referenced from here.  */
15773   prune_unused_types_walk_attribs (die);
15774
15775   /* Mark children.  */
15776   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15777 }
15778
15779 /* Increment the string counts on strings referred to from DIE's
15780    attributes.  */
15781
15782 static void
15783 prune_unused_types_update_strings (dw_die_ref die)
15784 {
15785   dw_attr_ref a;
15786   unsigned ix;
15787
15788   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15789     if (AT_class (a) == dw_val_class_str)
15790       {
15791         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15792         s->refcount++;
15793         /* Avoid unnecessarily putting strings that are used less than
15794            twice in the hash table.  */
15795         if (s->refcount
15796             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15797           {
15798             void ** slot;
15799             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15800                                              htab_hash_string (s->str),
15801                                              INSERT);
15802             gcc_assert (*slot == NULL);
15803             *slot = s;
15804           }
15805       }
15806 }
15807
15808 /* Remove from the tree DIE any dies that aren't marked.  */
15809
15810 static void
15811 prune_unused_types_prune (dw_die_ref die)
15812 {
15813   dw_die_ref c;
15814
15815   gcc_assert (die->die_mark);
15816   prune_unused_types_update_strings (die);
15817
15818   if (! die->die_child)
15819     return;
15820
15821   c = die->die_child;
15822   do {
15823     dw_die_ref prev = c;
15824     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15825       if (c == die->die_child)
15826         {
15827           /* No marked children between 'prev' and the end of the list.  */
15828           if (prev == c)
15829             /* No marked children at all.  */
15830             die->die_child = NULL;
15831           else
15832             {
15833               prev->die_sib = c->die_sib;
15834               die->die_child = prev;
15835             }
15836           return;
15837         }
15838
15839     if (c != prev->die_sib)
15840       prev->die_sib = c;
15841     prune_unused_types_prune (c);
15842   } while (c != die->die_child);
15843 }
15844
15845
15846 /* Remove dies representing declarations that we never use.  */
15847
15848 static void
15849 prune_unused_types (void)
15850 {
15851   unsigned int i;
15852   limbo_die_node *node;
15853   pubname_ref pub;
15854
15855 #if ENABLE_ASSERT_CHECKING
15856   /* All the marks should already be clear.  */
15857   verify_marks_clear (comp_unit_die);
15858   for (node = limbo_die_list; node; node = node->next)
15859     verify_marks_clear (node->die);
15860 #endif /* ENABLE_ASSERT_CHECKING */
15861
15862   /* Set the mark on nodes that are actually used.  */
15863   prune_unused_types_walk (comp_unit_die);
15864   for (node = limbo_die_list; node; node = node->next)
15865     prune_unused_types_walk (node->die);
15866
15867   /* Also set the mark on nodes referenced from the
15868      pubname_table or arange_table.  */
15869   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15870     prune_unused_types_mark (pub->die, 1);
15871   for (i = 0; i < arange_table_in_use; i++)
15872     prune_unused_types_mark (arange_table[i], 1);
15873
15874   /* Get rid of nodes that aren't marked; and update the string counts.  */
15875   if (debug_str_hash)
15876     htab_empty (debug_str_hash);
15877   prune_unused_types_prune (comp_unit_die);
15878   for (node = limbo_die_list; node; node = node->next)
15879     prune_unused_types_prune (node->die);
15880
15881   /* Leave the marks clear.  */
15882   prune_unmark_dies (comp_unit_die);
15883   for (node = limbo_die_list; node; node = node->next)
15884     prune_unmark_dies (node->die);
15885 }
15886
15887 /* Set the parameter to true if there are any relative pathnames in
15888    the file table.  */
15889 static int
15890 file_table_relative_p (void ** slot, void *param)
15891 {
15892   bool *p = (bool *) param;
15893   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
15894   if (!IS_ABSOLUTE_PATH (d->filename))
15895     {
15896       *p = true;
15897       return 0;
15898     }
15899   return 1;
15900 }
15901
15902 /* Output stuff that dwarf requires at the end of every file,
15903    and generate the DWARF-2 debugging info.  */
15904
15905 static void
15906 dwarf2out_finish (const char *filename)
15907 {
15908   limbo_die_node *node, *next_node;
15909   dw_die_ref die = 0;
15910
15911   /* Add the name for the main input file now.  We delayed this from
15912      dwarf2out_init to avoid complications with PCH.  */
15913   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15914   if (!IS_ABSOLUTE_PATH (filename))
15915     add_comp_dir_attribute (comp_unit_die);
15916   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15917     {
15918       bool p = false;
15919       htab_traverse (file_table, file_table_relative_p, &p);
15920       if (p)
15921         add_comp_dir_attribute (comp_unit_die);
15922     }
15923
15924   /* Traverse the limbo die list, and add parent/child links.  The only
15925      dies without parents that should be here are concrete instances of
15926      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15927      For concrete instances, we can get the parent die from the abstract
15928      instance.  */
15929   for (node = limbo_die_list; node; node = next_node)
15930     {
15931       next_node = node->next;
15932       die = node->die;
15933
15934       if (die->die_parent == NULL)
15935         {
15936           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15937
15938           if (origin)
15939             add_child_die (origin->die_parent, die);
15940           else if (die == comp_unit_die)
15941             ;
15942           else if (errorcount > 0 || sorrycount > 0)
15943             /* It's OK to be confused by errors in the input.  */
15944             add_child_die (comp_unit_die, die);
15945           else
15946             {
15947               /* In certain situations, the lexical block containing a
15948                  nested function can be optimized away, which results
15949                  in the nested function die being orphaned.  Likewise
15950                  with the return type of that nested function.  Force
15951                  this to be a child of the containing function.
15952
15953                  It may happen that even the containing function got fully
15954                  inlined and optimized out.  In that case we are lost and
15955                  assign the empty child.  This should not be big issue as
15956                  the function is likely unreachable too.  */
15957               tree context = NULL_TREE;
15958
15959               gcc_assert (node->created_for);
15960
15961               if (DECL_P (node->created_for))
15962                 context = DECL_CONTEXT (node->created_for);
15963               else if (TYPE_P (node->created_for))
15964                 context = TYPE_CONTEXT (node->created_for);
15965
15966               gcc_assert (context
15967                           && (TREE_CODE (context) == FUNCTION_DECL
15968                               || TREE_CODE (context) == NAMESPACE_DECL));
15969
15970               origin = lookup_decl_die (context);
15971               if (origin)
15972                 add_child_die (origin, die);
15973               else
15974                 add_child_die (comp_unit_die, die);
15975             }
15976         }
15977     }
15978
15979   limbo_die_list = NULL;
15980
15981   /* Walk through the list of incomplete types again, trying once more to
15982      emit full debugging info for them.  */
15983   retry_incomplete_types ();
15984
15985   if (flag_eliminate_unused_debug_types)
15986     prune_unused_types ();
15987
15988   /* Generate separate CUs for each of the include files we've seen.
15989      They will go into limbo_die_list.  */
15990   if (flag_eliminate_dwarf2_dups)
15991     break_out_includes (comp_unit_die);
15992
15993   /* Traverse the DIE's and add add sibling attributes to those DIE's
15994      that have children.  */
15995   add_sibling_attributes (comp_unit_die);
15996   for (node = limbo_die_list; node; node = node->next)
15997     add_sibling_attributes (node->die);
15998
15999   /* Output a terminator label for the .text section.  */
16000   switch_to_section (text_section);
16001   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16002   if (flag_reorder_blocks_and_partition)
16003     {
16004       switch_to_section (unlikely_text_section ());
16005       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16006     }
16007
16008   /* We can only use the low/high_pc attributes if all of the code was
16009      in .text.  */
16010   if (!have_multiple_function_sections)
16011     {
16012       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16013       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16014     }
16015
16016   else
16017     {
16018       unsigned fde_idx = 0;
16019
16020       /* We need to give .debug_loc and .debug_ranges an appropriate
16021          "base address".  Use zero so that these addresses become
16022          absolute.  Historically, we've emitted the unexpected
16023          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16024          Emit both to give time for other tools to adapt.  */
16025       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16026       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16027
16028       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16029                          add_ranges_by_labels (text_section_label,
16030                                                text_end_label));
16031       if (flag_reorder_blocks_and_partition)
16032         add_ranges_by_labels (cold_text_section_label,
16033                               cold_end_label);
16034
16035       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16036         {
16037           dw_fde_ref fde = &fde_table[fde_idx];
16038
16039           if (fde->dw_fde_switched_sections)
16040             {
16041               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16042                                     fde->dw_fde_hot_section_end_label);
16043               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16044                                     fde->dw_fde_unlikely_section_end_label);
16045             }
16046           else
16047             add_ranges_by_labels (fde->dw_fde_begin,
16048                                   fde->dw_fde_end);
16049         }
16050
16051       add_ranges (NULL);
16052     }
16053
16054   /* Output location list section if necessary.  */
16055   if (have_location_lists)
16056     {
16057       /* Output the location lists info.  */
16058       switch_to_section (debug_loc_section);
16059       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16060                                    DEBUG_LOC_SECTION_LABEL, 0);
16061       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16062       output_location_lists (die);
16063     }
16064
16065   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16066     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16067                     debug_line_section_label);
16068
16069   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16070     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16071
16072   /* Output all of the compilation units.  We put the main one last so that
16073      the offsets are available to output_pubnames.  */
16074   for (node = limbo_die_list; node; node = node->next)
16075     output_comp_unit (node->die, 0);
16076
16077   output_comp_unit (comp_unit_die, 0);
16078
16079   /* Output the abbreviation table.  */
16080   switch_to_section (debug_abbrev_section);
16081   output_abbrev_section ();
16082
16083   /* Output public names table if necessary.  */
16084   if (!VEC_empty (pubname_entry, pubname_table))
16085     {
16086       switch_to_section (debug_pubnames_section);
16087       output_pubnames (pubname_table);
16088     }
16089
16090 #ifdef DEBUG_PUBTYPES_SECTION
16091   /* Output public types table if necessary.  */
16092   if (!VEC_empty (pubname_entry, pubtype_table))
16093     {
16094       switch_to_section (debug_pubtypes_section);
16095       output_pubnames (pubtype_table);
16096     }
16097 #endif
16098
16099   /* Output the address range information.  We only put functions in the arange
16100      table, so don't write it out if we don't have any.  */
16101   if (fde_table_in_use)
16102     {
16103       switch_to_section (debug_aranges_section);
16104       output_aranges ();
16105     }
16106
16107   /* Output ranges section if necessary.  */
16108   if (ranges_table_in_use)
16109     {
16110       switch_to_section (debug_ranges_section);
16111       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16112       output_ranges ();
16113     }
16114
16115   /* Output the source line correspondence table.  We must do this
16116      even if there is no line information.  Otherwise, on an empty
16117      translation unit, we will generate a present, but empty,
16118      .debug_info section.  IRIX 6.5 `nm' will then complain when
16119      examining the file.  This is done late so that any filenames
16120      used by the debug_info section are marked as 'used'.  */
16121   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16122     {
16123       switch_to_section (debug_line_section);
16124       output_line_info ();
16125     }
16126
16127   /* Have to end the macro section.  */
16128   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16129     {
16130       switch_to_section (debug_macinfo_section);
16131       dw2_asm_output_data (1, 0, "End compilation unit");
16132     }
16133
16134   /* If we emitted any DW_FORM_strp form attribute, output the string
16135      table too.  */
16136   if (debug_str_hash)
16137     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16138 }
16139 #else
16140
16141 /* This should never be used, but its address is needed for comparisons.  */
16142 const struct gcc_debug_hooks dwarf2_debug_hooks;
16143
16144 #endif /* DWARF2_DEBUGGING_INFO */
16145
16146 #include "gt-dwarf2out.h"