OSDN Git Service

* dwarf2out.c (gen_formal_parameter_die, gen_variable_die): For
[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, tree, bool);
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 dw_die_ref 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           || c->die_tag == DW_TAG_module);
7201 }
7202
7203 static char *
7204 gen_internal_sym (const char *prefix)
7205 {
7206   char buf[256];
7207
7208   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7209   return xstrdup (buf);
7210 }
7211
7212 /* Assign symbols to all worthy DIEs under DIE.  */
7213
7214 static void
7215 assign_symbol_names (dw_die_ref die)
7216 {
7217   dw_die_ref c;
7218
7219   if (is_symbol_die (die))
7220     {
7221       if (comdat_symbol_id)
7222         {
7223           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7224
7225           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7226                    comdat_symbol_id, comdat_symbol_number++);
7227           die->die_symbol = xstrdup (p);
7228         }
7229       else
7230         die->die_symbol = gen_internal_sym ("LDIE");
7231     }
7232
7233   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7234 }
7235
7236 struct cu_hash_table_entry
7237 {
7238   dw_die_ref cu;
7239   unsigned min_comdat_num, max_comdat_num;
7240   struct cu_hash_table_entry *next;
7241 };
7242
7243 /* Routines to manipulate hash table of CUs.  */
7244 static hashval_t
7245 htab_cu_hash (const void *of)
7246 {
7247   const struct cu_hash_table_entry *const entry =
7248     (const struct cu_hash_table_entry *) of;
7249
7250   return htab_hash_string (entry->cu->die_symbol);
7251 }
7252
7253 static int
7254 htab_cu_eq (const void *of1, const void *of2)
7255 {
7256   const struct cu_hash_table_entry *const entry1 =
7257     (const struct cu_hash_table_entry *) of1;
7258   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7259
7260   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7261 }
7262
7263 static void
7264 htab_cu_del (void *what)
7265 {
7266   struct cu_hash_table_entry *next,
7267     *entry = (struct cu_hash_table_entry *) what;
7268
7269   while (entry)
7270     {
7271       next = entry->next;
7272       free (entry);
7273       entry = next;
7274     }
7275 }
7276
7277 /* Check whether we have already seen this CU and set up SYM_NUM
7278    accordingly.  */
7279 static int
7280 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7281 {
7282   struct cu_hash_table_entry dummy;
7283   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7284
7285   dummy.max_comdat_num = 0;
7286
7287   slot = (struct cu_hash_table_entry **)
7288     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7289         INSERT);
7290   entry = *slot;
7291
7292   for (; entry; last = entry, entry = entry->next)
7293     {
7294       if (same_die_p_wrap (cu, entry->cu))
7295         break;
7296     }
7297
7298   if (entry)
7299     {
7300       *sym_num = entry->min_comdat_num;
7301       return 1;
7302     }
7303
7304   entry = XCNEW (struct cu_hash_table_entry);
7305   entry->cu = cu;
7306   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7307   entry->next = *slot;
7308   *slot = entry;
7309
7310   return 0;
7311 }
7312
7313 /* Record SYM_NUM to record of CU in HTABLE.  */
7314 static void
7315 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7316 {
7317   struct cu_hash_table_entry **slot, *entry;
7318
7319   slot = (struct cu_hash_table_entry **)
7320     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7321         NO_INSERT);
7322   entry = *slot;
7323
7324   entry->max_comdat_num = sym_num;
7325 }
7326
7327 /* Traverse the DIE (which is always comp_unit_die), and set up
7328    additional compilation units for each of the include files we see
7329    bracketed by BINCL/EINCL.  */
7330
7331 static void
7332 break_out_includes (dw_die_ref die)
7333 {
7334   dw_die_ref c;
7335   dw_die_ref unit = NULL;
7336   limbo_die_node *node, **pnode;
7337   htab_t cu_hash_table;
7338
7339   c = die->die_child;
7340   if (c) do {
7341     dw_die_ref prev = c;
7342     c = c->die_sib;
7343     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7344            || (unit && is_comdat_die (c)))
7345       {
7346         dw_die_ref next = c->die_sib;
7347
7348         /* This DIE is for a secondary CU; remove it from the main one.  */
7349         remove_child_with_prev (c, prev);
7350
7351         if (c->die_tag == DW_TAG_GNU_BINCL)
7352           unit = push_new_compile_unit (unit, c);
7353         else if (c->die_tag == DW_TAG_GNU_EINCL)
7354           unit = pop_compile_unit (unit);
7355         else
7356           add_child_die (unit, c);
7357         c = next;
7358         if (c == die->die_child)
7359           break;
7360       }
7361   } while (c != die->die_child);
7362
7363 #if 0
7364   /* We can only use this in debugging, since the frontend doesn't check
7365      to make sure that we leave every include file we enter.  */
7366   gcc_assert (!unit);
7367 #endif
7368
7369   assign_symbol_names (die);
7370   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7371   for (node = limbo_die_list, pnode = &limbo_die_list;
7372        node;
7373        node = node->next)
7374     {
7375       int is_dupl;
7376
7377       compute_section_prefix (node->die);
7378       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7379                         &comdat_symbol_number);
7380       assign_symbol_names (node->die);
7381       if (is_dupl)
7382         *pnode = node->next;
7383       else
7384         {
7385           pnode = &node->next;
7386           record_comdat_symbol_number (node->die, cu_hash_table,
7387                 comdat_symbol_number);
7388         }
7389     }
7390   htab_delete (cu_hash_table);
7391 }
7392
7393 /* Traverse the DIE and add a sibling attribute if it may have the
7394    effect of speeding up access to siblings.  To save some space,
7395    avoid generating sibling attributes for DIE's without children.  */
7396
7397 static void
7398 add_sibling_attributes (dw_die_ref die)
7399 {
7400   dw_die_ref c;
7401
7402   if (! die->die_child)
7403     return;
7404
7405   if (die->die_parent && die != die->die_parent->die_child)
7406     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7407
7408   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7409 }
7410
7411 /* Output all location lists for the DIE and its children.  */
7412
7413 static void
7414 output_location_lists (dw_die_ref die)
7415 {
7416   dw_die_ref c;
7417   dw_attr_ref a;
7418   unsigned ix;
7419
7420   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7421     if (AT_class (a) == dw_val_class_loc_list)
7422       output_loc_list (AT_loc_list (a));
7423
7424   FOR_EACH_CHILD (die, c, output_location_lists (c));
7425 }
7426
7427 /* The format of each DIE (and its attribute value pairs) is encoded in an
7428    abbreviation table.  This routine builds the abbreviation table and assigns
7429    a unique abbreviation id for each abbreviation entry.  The children of each
7430    die are visited recursively.  */
7431
7432 static void
7433 build_abbrev_table (dw_die_ref die)
7434 {
7435   unsigned long abbrev_id;
7436   unsigned int n_alloc;
7437   dw_die_ref c;
7438   dw_attr_ref a;
7439   unsigned ix;
7440
7441   /* Scan the DIE references, and mark as external any that refer to
7442      DIEs from other CUs (i.e. those which are not marked).  */
7443   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7444     if (AT_class (a) == dw_val_class_die_ref
7445         && AT_ref (a)->die_mark == 0)
7446       {
7447         gcc_assert (AT_ref (a)->die_symbol);
7448
7449         set_AT_ref_external (a, 1);
7450       }
7451
7452   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7453     {
7454       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7455       dw_attr_ref die_a, abbrev_a;
7456       unsigned ix;
7457       bool ok = true;
7458
7459       if (abbrev->die_tag != die->die_tag)
7460         continue;
7461       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7462         continue;
7463
7464       if (VEC_length (dw_attr_node, abbrev->die_attr)
7465           != VEC_length (dw_attr_node, die->die_attr))
7466         continue;
7467
7468       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7469         {
7470           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7471           if ((abbrev_a->dw_attr != die_a->dw_attr)
7472               || (value_format (abbrev_a) != value_format (die_a)))
7473             {
7474               ok = false;
7475               break;
7476             }
7477         }
7478       if (ok)
7479         break;
7480     }
7481
7482   if (abbrev_id >= abbrev_die_table_in_use)
7483     {
7484       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7485         {
7486           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7487           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7488                                             n_alloc);
7489
7490           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7491                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7492           abbrev_die_table_allocated = n_alloc;
7493         }
7494
7495       ++abbrev_die_table_in_use;
7496       abbrev_die_table[abbrev_id] = die;
7497     }
7498
7499   die->die_abbrev = abbrev_id;
7500   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7501 }
7502 \f
7503 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7504
7505 static int
7506 constant_size (long unsigned int value)
7507 {
7508   int log;
7509
7510   if (value == 0)
7511     log = 0;
7512   else
7513     log = floor_log2 (value);
7514
7515   log = log / 8;
7516   log = 1 << (floor_log2 (log) + 1);
7517
7518   return log;
7519 }
7520
7521 /* Return the size of a DIE as it is represented in the
7522    .debug_info section.  */
7523
7524 static unsigned long
7525 size_of_die (dw_die_ref die)
7526 {
7527   unsigned long size = 0;
7528   dw_attr_ref a;
7529   unsigned ix;
7530
7531   size += size_of_uleb128 (die->die_abbrev);
7532   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7533     {
7534       switch (AT_class (a))
7535         {
7536         case dw_val_class_addr:
7537           size += DWARF2_ADDR_SIZE;
7538           break;
7539         case dw_val_class_offset:
7540           size += DWARF_OFFSET_SIZE;
7541           break;
7542         case dw_val_class_loc:
7543           {
7544             unsigned long lsize = size_of_locs (AT_loc (a));
7545
7546             /* Block length.  */
7547             size += constant_size (lsize);
7548             size += lsize;
7549           }
7550           break;
7551         case dw_val_class_loc_list:
7552           size += DWARF_OFFSET_SIZE;
7553           break;
7554         case dw_val_class_range_list:
7555           size += DWARF_OFFSET_SIZE;
7556           break;
7557         case dw_val_class_const:
7558           size += size_of_sleb128 (AT_int (a));
7559           break;
7560         case dw_val_class_unsigned_const:
7561           size += constant_size (AT_unsigned (a));
7562           break;
7563         case dw_val_class_long_long:
7564           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7565           break;
7566         case dw_val_class_vec:
7567           size += 1 + (a->dw_attr_val.v.val_vec.length
7568                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
7569           break;
7570         case dw_val_class_flag:
7571           size += 1;
7572           break;
7573         case dw_val_class_die_ref:
7574           if (AT_ref_external (a))
7575             size += DWARF2_ADDR_SIZE;
7576           else
7577             size += DWARF_OFFSET_SIZE;
7578           break;
7579         case dw_val_class_fde_ref:
7580           size += DWARF_OFFSET_SIZE;
7581           break;
7582         case dw_val_class_lbl_id:
7583           size += DWARF2_ADDR_SIZE;
7584           break;
7585         case dw_val_class_lineptr:
7586         case dw_val_class_macptr:
7587           size += DWARF_OFFSET_SIZE;
7588           break;
7589         case dw_val_class_str:
7590           if (AT_string_form (a) == DW_FORM_strp)
7591             size += DWARF_OFFSET_SIZE;
7592           else
7593             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7594           break;
7595         case dw_val_class_file:
7596           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7597           break;
7598         default:
7599           gcc_unreachable ();
7600         }
7601     }
7602
7603   return size;
7604 }
7605
7606 /* Size the debugging information associated with a given DIE.  Visits the
7607    DIE's children recursively.  Updates the global variable next_die_offset, on
7608    each time through.  Uses the current value of next_die_offset to update the
7609    die_offset field in each DIE.  */
7610
7611 static void
7612 calc_die_sizes (dw_die_ref die)
7613 {
7614   dw_die_ref c;
7615
7616   die->die_offset = next_die_offset;
7617   next_die_offset += size_of_die (die);
7618
7619   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7620
7621   if (die->die_child != NULL)
7622     /* Count the null byte used to terminate sibling lists.  */
7623     next_die_offset += 1;
7624 }
7625
7626 /* Set the marks for a die and its children.  We do this so
7627    that we know whether or not a reference needs to use FORM_ref_addr; only
7628    DIEs in the same CU will be marked.  We used to clear out the offset
7629    and use that as the flag, but ran into ordering problems.  */
7630
7631 static void
7632 mark_dies (dw_die_ref die)
7633 {
7634   dw_die_ref c;
7635
7636   gcc_assert (!die->die_mark);
7637
7638   die->die_mark = 1;
7639   FOR_EACH_CHILD (die, c, mark_dies (c));
7640 }
7641
7642 /* Clear the marks for a die and its children.  */
7643
7644 static void
7645 unmark_dies (dw_die_ref die)
7646 {
7647   dw_die_ref c;
7648
7649   gcc_assert (die->die_mark);
7650
7651   die->die_mark = 0;
7652   FOR_EACH_CHILD (die, c, unmark_dies (c));
7653 }
7654
7655 /* Clear the marks for a die, its children and referred dies.  */
7656
7657 static void
7658 unmark_all_dies (dw_die_ref die)
7659 {
7660   dw_die_ref c;
7661   dw_attr_ref a;
7662   unsigned ix;
7663
7664   if (!die->die_mark)
7665     return;
7666   die->die_mark = 0;
7667
7668   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7669
7670   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7671     if (AT_class (a) == dw_val_class_die_ref)
7672       unmark_all_dies (AT_ref (a));
7673 }
7674
7675 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7676    generated for the compilation unit.  */
7677
7678 static unsigned long
7679 size_of_pubnames (VEC (pubname_entry, gc) * names)
7680 {
7681   unsigned long size;
7682   unsigned i;
7683   pubname_ref p;
7684
7685   size = DWARF_PUBNAMES_HEADER_SIZE;
7686   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7687     if (names != pubtype_table
7688         || p->die->die_offset != 0
7689         || !flag_eliminate_unused_debug_types)
7690       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7691
7692   size += DWARF_OFFSET_SIZE;
7693   return size;
7694 }
7695
7696 /* Return the size of the information in the .debug_aranges section.  */
7697
7698 static unsigned long
7699 size_of_aranges (void)
7700 {
7701   unsigned long size;
7702
7703   size = DWARF_ARANGES_HEADER_SIZE;
7704
7705   /* Count the address/length pair for this compilation unit.  */
7706   if (text_section_used)
7707     size += 2 * DWARF2_ADDR_SIZE;
7708   if (cold_text_section_used)
7709     size += 2 * DWARF2_ADDR_SIZE;
7710   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7711
7712   /* Count the two zero words used to terminated the address range table.  */
7713   size += 2 * DWARF2_ADDR_SIZE;
7714   return size;
7715 }
7716 \f
7717 /* Select the encoding of an attribute value.  */
7718
7719 static enum dwarf_form
7720 value_format (dw_attr_ref a)
7721 {
7722   switch (a->dw_attr_val.val_class)
7723     {
7724     case dw_val_class_addr:
7725       return DW_FORM_addr;
7726     case dw_val_class_range_list:
7727     case dw_val_class_offset:
7728     case dw_val_class_loc_list:
7729       switch (DWARF_OFFSET_SIZE)
7730         {
7731         case 4:
7732           return DW_FORM_data4;
7733         case 8:
7734           return DW_FORM_data8;
7735         default:
7736           gcc_unreachable ();
7737         }
7738     case dw_val_class_loc:
7739       switch (constant_size (size_of_locs (AT_loc (a))))
7740         {
7741         case 1:
7742           return DW_FORM_block1;
7743         case 2:
7744           return DW_FORM_block2;
7745         default:
7746           gcc_unreachable ();
7747         }
7748     case dw_val_class_const:
7749       return DW_FORM_sdata;
7750     case dw_val_class_unsigned_const:
7751       switch (constant_size (AT_unsigned (a)))
7752         {
7753         case 1:
7754           return DW_FORM_data1;
7755         case 2:
7756           return DW_FORM_data2;
7757         case 4:
7758           return DW_FORM_data4;
7759         case 8:
7760           return DW_FORM_data8;
7761         default:
7762           gcc_unreachable ();
7763         }
7764     case dw_val_class_long_long:
7765       return DW_FORM_block1;
7766     case dw_val_class_vec:
7767       return DW_FORM_block1;
7768     case dw_val_class_flag:
7769       return DW_FORM_flag;
7770     case dw_val_class_die_ref:
7771       if (AT_ref_external (a))
7772         return DW_FORM_ref_addr;
7773       else
7774         return DW_FORM_ref;
7775     case dw_val_class_fde_ref:
7776       return DW_FORM_data;
7777     case dw_val_class_lbl_id:
7778       return DW_FORM_addr;
7779     case dw_val_class_lineptr:
7780     case dw_val_class_macptr:
7781       return DW_FORM_data;
7782     case dw_val_class_str:
7783       return AT_string_form (a);
7784     case dw_val_class_file:
7785       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7786         {
7787         case 1:
7788           return DW_FORM_data1;
7789         case 2:
7790           return DW_FORM_data2;
7791         case 4:
7792           return DW_FORM_data4;
7793         default:
7794           gcc_unreachable ();
7795         }
7796
7797     default:
7798       gcc_unreachable ();
7799     }
7800 }
7801
7802 /* Output the encoding of an attribute value.  */
7803
7804 static void
7805 output_value_format (dw_attr_ref a)
7806 {
7807   enum dwarf_form form = value_format (a);
7808
7809   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7810 }
7811
7812 /* Output the .debug_abbrev section which defines the DIE abbreviation
7813    table.  */
7814
7815 static void
7816 output_abbrev_section (void)
7817 {
7818   unsigned long abbrev_id;
7819
7820   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7821     {
7822       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7823       unsigned ix;
7824       dw_attr_ref a_attr;
7825
7826       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7827       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7828                                    dwarf_tag_name (abbrev->die_tag));
7829
7830       if (abbrev->die_child != NULL)
7831         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7832       else
7833         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7834
7835       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7836            ix++)
7837         {
7838           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7839                                        dwarf_attr_name (a_attr->dw_attr));
7840           output_value_format (a_attr);
7841         }
7842
7843       dw2_asm_output_data (1, 0, NULL);
7844       dw2_asm_output_data (1, 0, NULL);
7845     }
7846
7847   /* Terminate the table.  */
7848   dw2_asm_output_data (1, 0, NULL);
7849 }
7850
7851 /* Output a symbol we can use to refer to this DIE from another CU.  */
7852
7853 static inline void
7854 output_die_symbol (dw_die_ref die)
7855 {
7856   char *sym = die->die_symbol;
7857
7858   if (sym == 0)
7859     return;
7860
7861   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7862     /* We make these global, not weak; if the target doesn't support
7863        .linkonce, it doesn't support combining the sections, so debugging
7864        will break.  */
7865     targetm.asm_out.globalize_label (asm_out_file, sym);
7866
7867   ASM_OUTPUT_LABEL (asm_out_file, sym);
7868 }
7869
7870 /* Return a new location list, given the begin and end range, and the
7871    expression. gensym tells us whether to generate a new internal symbol for
7872    this location list node, which is done for the head of the list only.  */
7873
7874 static inline dw_loc_list_ref
7875 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7876               const char *section, unsigned int gensym)
7877 {
7878   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7879
7880   retlist->begin = begin;
7881   retlist->end = end;
7882   retlist->expr = expr;
7883   retlist->section = section;
7884   if (gensym)
7885     retlist->ll_symbol = gen_internal_sym ("LLST");
7886
7887   return retlist;
7888 }
7889
7890 /* Add a location description expression to a location list.  */
7891
7892 static inline void
7893 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7894                            const char *begin, const char *end,
7895                            const char *section)
7896 {
7897   dw_loc_list_ref *d;
7898
7899   /* Find the end of the chain.  */
7900   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7901     ;
7902
7903   /* Add a new location list node to the list.  */
7904   *d = new_loc_list (descr, begin, end, section, 0);
7905 }
7906
7907 /* Output the location list given to us.  */
7908
7909 static void
7910 output_loc_list (dw_loc_list_ref list_head)
7911 {
7912   dw_loc_list_ref curr = list_head;
7913
7914   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7915
7916   /* Walk the location list, and output each range + expression.  */
7917   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7918     {
7919       unsigned long size;
7920       /* Don't output an entry that starts and ends at the same address.  */
7921       if (strcmp (curr->begin, curr->end) == 0)
7922         continue;
7923       if (!have_multiple_function_sections)
7924         {
7925           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7926                                 "Location list begin address (%s)",
7927                                 list_head->ll_symbol);
7928           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7929                                 "Location list end address (%s)",
7930                                 list_head->ll_symbol);
7931         }
7932       else
7933         {
7934           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7935                                "Location list begin address (%s)",
7936                                list_head->ll_symbol);
7937           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7938                                "Location list end address (%s)",
7939                                list_head->ll_symbol);
7940         }
7941       size = size_of_locs (curr->expr);
7942
7943       /* Output the block length for this list of location operations.  */
7944       gcc_assert (size <= 0xffff);
7945       dw2_asm_output_data (2, size, "%s", "Location expression size");
7946
7947       output_loc_sequence (curr->expr);
7948     }
7949
7950   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7951                        "Location list terminator begin (%s)",
7952                        list_head->ll_symbol);
7953   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7954                        "Location list terminator end (%s)",
7955                        list_head->ll_symbol);
7956 }
7957
7958 /* Output the DIE and its attributes.  Called recursively to generate
7959    the definitions of each child DIE.  */
7960
7961 static void
7962 output_die (dw_die_ref die)
7963 {
7964   dw_attr_ref a;
7965   dw_die_ref c;
7966   unsigned long size;
7967   unsigned ix;
7968
7969   /* If someone in another CU might refer to us, set up a symbol for
7970      them to point to.  */
7971   if (die->die_symbol)
7972     output_die_symbol (die);
7973
7974   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7975                                (unsigned long)die->die_offset,
7976                                dwarf_tag_name (die->die_tag));
7977
7978   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7979     {
7980       const char *name = dwarf_attr_name (a->dw_attr);
7981
7982       switch (AT_class (a))
7983         {
7984         case dw_val_class_addr:
7985           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7986           break;
7987
7988         case dw_val_class_offset:
7989           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7990                                "%s", name);
7991           break;
7992
7993         case dw_val_class_range_list:
7994           {
7995             char *p = strchr (ranges_section_label, '\0');
7996
7997             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7998                      a->dw_attr_val.v.val_offset);
7999             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8000                                    debug_ranges_section, "%s", name);
8001             *p = '\0';
8002           }
8003           break;
8004
8005         case dw_val_class_loc:
8006           size = size_of_locs (AT_loc (a));
8007
8008           /* Output the block length for this list of location operations.  */
8009           dw2_asm_output_data (constant_size (size), size, "%s", name);
8010
8011           output_loc_sequence (AT_loc (a));
8012           break;
8013
8014         case dw_val_class_const:
8015           /* ??? It would be slightly more efficient to use a scheme like is
8016              used for unsigned constants below, but gdb 4.x does not sign
8017              extend.  Gdb 5.x does sign extend.  */
8018           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8019           break;
8020
8021         case dw_val_class_unsigned_const:
8022           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8023                                AT_unsigned (a), "%s", name);
8024           break;
8025
8026         case dw_val_class_long_long:
8027           {
8028             unsigned HOST_WIDE_INT first, second;
8029
8030             dw2_asm_output_data (1,
8031                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8032                                  "%s", name);
8033
8034             if (WORDS_BIG_ENDIAN)
8035               {
8036                 first = a->dw_attr_val.v.val_long_long.hi;
8037                 second = a->dw_attr_val.v.val_long_long.low;
8038               }
8039             else
8040               {
8041                 first = a->dw_attr_val.v.val_long_long.low;
8042                 second = a->dw_attr_val.v.val_long_long.hi;
8043               }
8044
8045             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8046                                  first, "long long constant");
8047             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8048                                  second, NULL);
8049           }
8050           break;
8051
8052         case dw_val_class_vec:
8053           {
8054             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8055             unsigned int len = a->dw_attr_val.v.val_vec.length;
8056             unsigned int i;
8057             unsigned char *p;
8058
8059             dw2_asm_output_data (1, len * elt_size, "%s", name);
8060             if (elt_size > sizeof (HOST_WIDE_INT))
8061               {
8062                 elt_size /= 2;
8063                 len *= 2;
8064               }
8065             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8066                  i < len;
8067                  i++, p += elt_size)
8068               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8069                                    "fp or vector constant word %u", i);
8070             break;
8071           }
8072
8073         case dw_val_class_flag:
8074           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8075           break;
8076
8077         case dw_val_class_loc_list:
8078           {
8079             char *sym = AT_loc_list (a)->ll_symbol;
8080
8081             gcc_assert (sym);
8082             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8083                                    "%s", name);
8084           }
8085           break;
8086
8087         case dw_val_class_die_ref:
8088           if (AT_ref_external (a))
8089             {
8090               char *sym = AT_ref (a)->die_symbol;
8091
8092               gcc_assert (sym);
8093               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8094                                      "%s", name);
8095             }
8096           else
8097             {
8098               gcc_assert (AT_ref (a)->die_offset);
8099               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8100                                    "%s", name);
8101             }
8102           break;
8103
8104         case dw_val_class_fde_ref:
8105           {
8106             char l1[20];
8107
8108             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8109                                          a->dw_attr_val.v.val_fde_index * 2);
8110             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8111                                    "%s", name);
8112           }
8113           break;
8114
8115         case dw_val_class_lbl_id:
8116           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8117           break;
8118
8119         case dw_val_class_lineptr:
8120           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8121                                  debug_line_section, "%s", name);
8122           break;
8123
8124         case dw_val_class_macptr:
8125           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8126                                  debug_macinfo_section, "%s", name);
8127           break;
8128
8129         case dw_val_class_str:
8130           if (AT_string_form (a) == DW_FORM_strp)
8131             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8132                                    a->dw_attr_val.v.val_str->label,
8133                                    debug_str_section,
8134                                    "%s: \"%s\"", name, AT_string (a));
8135           else
8136             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8137           break;
8138
8139         case dw_val_class_file:
8140           {
8141             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8142
8143             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8144                                  a->dw_attr_val.v.val_file->filename);
8145             break;
8146           }
8147
8148         default:
8149           gcc_unreachable ();
8150         }
8151     }
8152
8153   FOR_EACH_CHILD (die, c, output_die (c));
8154
8155   /* Add null byte to terminate sibling list.  */
8156   if (die->die_child != NULL)
8157     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8158                          (unsigned long) die->die_offset);
8159 }
8160
8161 /* Output the compilation unit that appears at the beginning of the
8162    .debug_info section, and precedes the DIE descriptions.  */
8163
8164 static void
8165 output_compilation_unit_header (void)
8166 {
8167   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8168     dw2_asm_output_data (4, 0xffffffff,
8169       "Initial length escape value indicating 64-bit DWARF extension");
8170   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8171                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8172                        "Length of Compilation Unit Info");
8173   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8174   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8175                          debug_abbrev_section,
8176                          "Offset Into Abbrev. Section");
8177   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8178 }
8179
8180 /* Output the compilation unit DIE and its children.  */
8181
8182 static void
8183 output_comp_unit (dw_die_ref die, int output_if_empty)
8184 {
8185   const char *secname;
8186   char *oldsym, *tmp;
8187
8188   /* Unless we are outputting main CU, we may throw away empty ones.  */
8189   if (!output_if_empty && die->die_child == NULL)
8190     return;
8191
8192   /* Even if there are no children of this DIE, we must output the information
8193      about the compilation unit.  Otherwise, on an empty translation unit, we
8194      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8195      will then complain when examining the file.  First mark all the DIEs in
8196      this CU so we know which get local refs.  */
8197   mark_dies (die);
8198
8199   build_abbrev_table (die);
8200
8201   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8202   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8203   calc_die_sizes (die);
8204
8205   oldsym = die->die_symbol;
8206   if (oldsym)
8207     {
8208       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8209
8210       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8211       secname = tmp;
8212       die->die_symbol = NULL;
8213       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8214     }
8215   else
8216     switch_to_section (debug_info_section);
8217
8218   /* Output debugging information.  */
8219   output_compilation_unit_header ();
8220   output_die (die);
8221
8222   /* Leave the marks on the main CU, so we can check them in
8223      output_pubnames.  */
8224   if (oldsym)
8225     {
8226       unmark_dies (die);
8227       die->die_symbol = oldsym;
8228     }
8229 }
8230
8231 /* Return the DWARF2/3 pubname associated with a decl.  */
8232
8233 static const char *
8234 dwarf2_name (tree decl, int scope)
8235 {
8236   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8237 }
8238
8239 /* Add a new entry to .debug_pubnames if appropriate.  */
8240
8241 static void
8242 add_pubname_string (const char *str, dw_die_ref die)
8243 {
8244   pubname_entry e;
8245
8246   e.die = die;
8247   e.name = xstrdup (str);
8248   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8249 }
8250
8251 static void
8252 add_pubname (tree decl, dw_die_ref die)
8253 {
8254
8255   if (TREE_PUBLIC (decl))
8256     add_pubname_string (dwarf2_name (decl, 1), die);
8257 }
8258
8259 /* Add a new entry to .debug_pubtypes if appropriate.  */
8260
8261 static void
8262 add_pubtype (tree decl, dw_die_ref die)
8263 {
8264   pubname_entry e;
8265
8266   e.name = NULL;
8267   if ((TREE_PUBLIC (decl)
8268        || die->die_parent == comp_unit_die)
8269       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8270     {
8271       e.die = die;
8272       if (TYPE_P (decl))
8273         {
8274           if (TYPE_NAME (decl))
8275             {
8276               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8277                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8278               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8279                        && DECL_NAME (TYPE_NAME (decl)))
8280                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8281               else
8282                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8283             }
8284         }
8285       else
8286         e.name = xstrdup (dwarf2_name (decl, 1));
8287
8288       /* If we don't have a name for the type, there's no point in adding
8289          it to the table.  */
8290       if (e.name && e.name[0] != '\0')
8291         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8292     }
8293 }
8294
8295 /* Output the public names table used to speed up access to externally
8296    visible names; or the public types table used to find type definitions.  */
8297
8298 static void
8299 output_pubnames (VEC (pubname_entry, gc) * names)
8300 {
8301   unsigned i;
8302   unsigned long pubnames_length = size_of_pubnames (names);
8303   pubname_ref pub;
8304
8305   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8306     dw2_asm_output_data (4, 0xffffffff,
8307       "Initial length escape value indicating 64-bit DWARF extension");
8308   if (names == pubname_table)
8309     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8310                          "Length of Public Names Info");
8311   else
8312     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8313                          "Length of Public Type Names Info");
8314   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8315   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8316                          debug_info_section,
8317                          "Offset of Compilation Unit Info");
8318   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8319                        "Compilation Unit Length");
8320
8321   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8322     {
8323       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8324       if (names == pubname_table)
8325         gcc_assert (pub->die->die_mark);
8326
8327       if (names != pubtype_table
8328           || pub->die->die_offset != 0
8329           || !flag_eliminate_unused_debug_types)
8330         {
8331           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8332                                "DIE offset");
8333
8334           dw2_asm_output_nstring (pub->name, -1, "external name");
8335         }
8336     }
8337
8338   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8339 }
8340
8341 /* Add a new entry to .debug_aranges if appropriate.  */
8342
8343 static void
8344 add_arange (tree decl, dw_die_ref die)
8345 {
8346   if (! DECL_SECTION_NAME (decl))
8347     return;
8348
8349   if (arange_table_in_use == arange_table_allocated)
8350     {
8351       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8352       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8353                                     arange_table_allocated);
8354       memset (arange_table + arange_table_in_use, 0,
8355               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8356     }
8357
8358   arange_table[arange_table_in_use++] = die;
8359 }
8360
8361 /* Output the information that goes into the .debug_aranges table.
8362    Namely, define the beginning and ending address range of the
8363    text section generated for this compilation unit.  */
8364
8365 static void
8366 output_aranges (void)
8367 {
8368   unsigned i;
8369   unsigned long aranges_length = size_of_aranges ();
8370
8371   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8372     dw2_asm_output_data (4, 0xffffffff,
8373       "Initial length escape value indicating 64-bit DWARF extension");
8374   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8375                        "Length of Address Ranges Info");
8376   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8377   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8378                          debug_info_section,
8379                          "Offset of Compilation Unit Info");
8380   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8381   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8382
8383   /* We need to align to twice the pointer size here.  */
8384   if (DWARF_ARANGES_PAD_SIZE)
8385     {
8386       /* Pad using a 2 byte words so that padding is correct for any
8387          pointer size.  */
8388       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8389                            2 * DWARF2_ADDR_SIZE);
8390       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8391         dw2_asm_output_data (2, 0, NULL);
8392     }
8393
8394   /* It is necessary not to output these entries if the sections were
8395      not used; if the sections were not used, the length will be 0 and
8396      the address may end up as 0 if the section is discarded by ld
8397      --gc-sections, leaving an invalid (0, 0) entry that can be
8398      confused with the terminator.  */
8399   if (text_section_used)
8400     {
8401       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8402       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8403                             text_section_label, "Length");
8404     }
8405   if (cold_text_section_used)
8406     {
8407       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8408                            "Address");
8409       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8410                             cold_text_section_label, "Length");
8411     }
8412
8413   for (i = 0; i < arange_table_in_use; i++)
8414     {
8415       dw_die_ref die = arange_table[i];
8416
8417       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8418       gcc_assert (die->die_mark);
8419
8420       if (die->die_tag == DW_TAG_subprogram)
8421         {
8422           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8423                                "Address");
8424           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8425                                 get_AT_low_pc (die), "Length");
8426         }
8427       else
8428         {
8429           /* A static variable; extract the symbol from DW_AT_location.
8430              Note that this code isn't currently hit, as we only emit
8431              aranges for functions (jason 9/23/99).  */
8432           dw_attr_ref a = get_AT (die, DW_AT_location);
8433           dw_loc_descr_ref loc;
8434
8435           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8436
8437           loc = AT_loc (a);
8438           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8439
8440           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8441                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8442           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8443                                get_AT_unsigned (die, DW_AT_byte_size),
8444                                "Length");
8445         }
8446     }
8447
8448   /* Output the terminator words.  */
8449   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8450   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8451 }
8452
8453 /* Add a new entry to .debug_ranges.  Return the offset at which it
8454    was placed.  */
8455
8456 static unsigned int
8457 add_ranges_num (int num)
8458 {
8459   unsigned int in_use = ranges_table_in_use;
8460
8461   if (in_use == ranges_table_allocated)
8462     {
8463       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8464       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8465                                     ranges_table_allocated);
8466       memset (ranges_table + ranges_table_in_use, 0,
8467               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8468     }
8469
8470   ranges_table[in_use].num = num;
8471   ranges_table_in_use = in_use + 1;
8472
8473   return in_use * 2 * DWARF2_ADDR_SIZE;
8474 }
8475
8476 /* Add a new entry to .debug_ranges corresponding to a block, or a
8477    range terminator if BLOCK is NULL.  */
8478
8479 static unsigned int
8480 add_ranges (const_tree block)
8481 {
8482   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8483 }
8484
8485 /* Add a new entry to .debug_ranges corresponding to a pair of
8486    labels.  */
8487
8488 static unsigned int
8489 add_ranges_by_labels (const char *begin, const char *end)
8490 {
8491   unsigned int in_use = ranges_by_label_in_use;
8492
8493   if (in_use == ranges_by_label_allocated)
8494     {
8495       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8496       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8497                                        ranges_by_label,
8498                                        ranges_by_label_allocated);
8499       memset (ranges_by_label + ranges_by_label_in_use, 0,
8500               RANGES_TABLE_INCREMENT
8501               * sizeof (struct dw_ranges_by_label_struct));
8502     }
8503
8504   ranges_by_label[in_use].begin = begin;
8505   ranges_by_label[in_use].end = end;
8506   ranges_by_label_in_use = in_use + 1;
8507
8508   return add_ranges_num (-(int)in_use - 1);
8509 }
8510
8511 static void
8512 output_ranges (void)
8513 {
8514   unsigned i;
8515   static const char *const start_fmt = "Offset 0x%x";
8516   const char *fmt = start_fmt;
8517
8518   for (i = 0; i < ranges_table_in_use; i++)
8519     {
8520       int block_num = ranges_table[i].num;
8521
8522       if (block_num > 0)
8523         {
8524           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8525           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8526
8527           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8528           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8529
8530           /* If all code is in the text section, then the compilation
8531              unit base address defaults to DW_AT_low_pc, which is the
8532              base of the text section.  */
8533           if (!have_multiple_function_sections)
8534             {
8535               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8536                                     text_section_label,
8537                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8538               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8539                                     text_section_label, NULL);
8540             }
8541
8542           /* Otherwise, the compilation unit base address is zero,
8543              which allows us to use absolute addresses, and not worry
8544              about whether the target supports cross-section
8545              arithmetic.  */
8546           else
8547             {
8548               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8549                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8550               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8551             }
8552
8553           fmt = NULL;
8554         }
8555
8556       /* Negative block_num stands for an index into ranges_by_label.  */
8557       else if (block_num < 0)
8558         {
8559           int lab_idx = - block_num - 1;
8560
8561           if (!have_multiple_function_sections)
8562             {
8563               gcc_unreachable ();
8564 #if 0
8565               /* If we ever use add_ranges_by_labels () for a single
8566                  function section, all we have to do is to take out
8567                  the #if 0 above.  */
8568               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8569                                     ranges_by_label[lab_idx].begin,
8570                                     text_section_label,
8571                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8572               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8573                                     ranges_by_label[lab_idx].end,
8574                                     text_section_label, NULL);
8575 #endif
8576             }
8577           else
8578             {
8579               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8580                                    ranges_by_label[lab_idx].begin,
8581                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8582               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8583                                    ranges_by_label[lab_idx].end,
8584                                    NULL);
8585             }
8586         }
8587       else
8588         {
8589           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8590           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8591           fmt = start_fmt;
8592         }
8593     }
8594 }
8595
8596 /* Data structure containing information about input files.  */
8597 struct file_info
8598 {
8599   const char *path;     /* Complete file name.  */
8600   const char *fname;    /* File name part.  */
8601   int length;           /* Length of entire string.  */
8602   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8603   int dir_idx;          /* Index in directory table.  */
8604 };
8605
8606 /* Data structure containing information about directories with source
8607    files.  */
8608 struct dir_info
8609 {
8610   const char *path;     /* Path including directory name.  */
8611   int length;           /* Path length.  */
8612   int prefix;           /* Index of directory entry which is a prefix.  */
8613   int count;            /* Number of files in this directory.  */
8614   int dir_idx;          /* Index of directory used as base.  */
8615 };
8616
8617 /* Callback function for file_info comparison.  We sort by looking at
8618    the directories in the path.  */
8619
8620 static int
8621 file_info_cmp (const void *p1, const void *p2)
8622 {
8623   const struct file_info *const s1 = (const struct file_info *) p1;
8624   const struct file_info *const s2 = (const struct file_info *) p2;
8625   const unsigned char *cp1;
8626   const unsigned char *cp2;
8627
8628   /* Take care of file names without directories.  We need to make sure that
8629      we return consistent values to qsort since some will get confused if
8630      we return the same value when identical operands are passed in opposite
8631      orders.  So if neither has a directory, return 0 and otherwise return
8632      1 or -1 depending on which one has the directory.  */
8633   if ((s1->path == s1->fname || s2->path == s2->fname))
8634     return (s2->path == s2->fname) - (s1->path == s1->fname);
8635
8636   cp1 = (const unsigned char *) s1->path;
8637   cp2 = (const unsigned char *) s2->path;
8638
8639   while (1)
8640     {
8641       ++cp1;
8642       ++cp2;
8643       /* Reached the end of the first path?  If so, handle like above.  */
8644       if ((cp1 == (const unsigned char *) s1->fname)
8645           || (cp2 == (const unsigned char *) s2->fname))
8646         return ((cp2 == (const unsigned char *) s2->fname)
8647                 - (cp1 == (const unsigned char *) s1->fname));
8648
8649       /* Character of current path component the same?  */
8650       else if (*cp1 != *cp2)
8651         return *cp1 - *cp2;
8652     }
8653 }
8654
8655 struct file_name_acquire_data
8656 {
8657   struct file_info *files;
8658   int used_files;
8659   int max_files;
8660 };
8661
8662 /* Traversal function for the hash table.  */
8663
8664 static int
8665 file_name_acquire (void ** slot, void *data)
8666 {
8667   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8668   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8669   struct file_info *fi;
8670   const char *f;
8671
8672   gcc_assert (fnad->max_files >= d->emitted_number);
8673
8674   if (! d->emitted_number)
8675     return 1;
8676
8677   gcc_assert (fnad->max_files != fnad->used_files);
8678
8679   fi = fnad->files + fnad->used_files++;
8680
8681   /* Skip all leading "./".  */
8682   f = d->filename;
8683   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8684     f += 2;
8685
8686   /* Create a new array entry.  */
8687   fi->path = f;
8688   fi->length = strlen (f);
8689   fi->file_idx = d;
8690
8691   /* Search for the file name part.  */
8692   f = strrchr (f, DIR_SEPARATOR);
8693 #if defined (DIR_SEPARATOR_2)
8694   {
8695     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8696
8697     if (g != NULL)
8698       {
8699         if (f == NULL || f < g)
8700           f = g;
8701       }
8702   }
8703 #endif
8704
8705   fi->fname = f == NULL ? fi->path : f + 1;
8706   return 1;
8707 }
8708
8709 /* Output the directory table and the file name table.  We try to minimize
8710    the total amount of memory needed.  A heuristic is used to avoid large
8711    slowdowns with many input files.  */
8712
8713 static void
8714 output_file_names (void)
8715 {
8716   struct file_name_acquire_data fnad;
8717   int numfiles;
8718   struct file_info *files;
8719   struct dir_info *dirs;
8720   int *saved;
8721   int *savehere;
8722   int *backmap;
8723   int ndirs;
8724   int idx_offset;
8725   int i;
8726   int idx;
8727
8728   if (!last_emitted_file)
8729     {
8730       dw2_asm_output_data (1, 0, "End directory table");
8731       dw2_asm_output_data (1, 0, "End file name table");
8732       return;
8733     }
8734
8735   numfiles = last_emitted_file->emitted_number;
8736
8737   /* Allocate the various arrays we need.  */
8738   files = XALLOCAVEC (struct file_info, numfiles);
8739   dirs = XALLOCAVEC (struct dir_info, numfiles);
8740
8741   fnad.files = files;
8742   fnad.used_files = 0;
8743   fnad.max_files = numfiles;
8744   htab_traverse (file_table, file_name_acquire, &fnad);
8745   gcc_assert (fnad.used_files == fnad.max_files);
8746
8747   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8748
8749   /* Find all the different directories used.  */
8750   dirs[0].path = files[0].path;
8751   dirs[0].length = files[0].fname - files[0].path;
8752   dirs[0].prefix = -1;
8753   dirs[0].count = 1;
8754   dirs[0].dir_idx = 0;
8755   files[0].dir_idx = 0;
8756   ndirs = 1;
8757
8758   for (i = 1; i < numfiles; i++)
8759     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8760         && memcmp (dirs[ndirs - 1].path, files[i].path,
8761                    dirs[ndirs - 1].length) == 0)
8762       {
8763         /* Same directory as last entry.  */
8764         files[i].dir_idx = ndirs - 1;
8765         ++dirs[ndirs - 1].count;
8766       }
8767     else
8768       {
8769         int j;
8770
8771         /* This is a new directory.  */
8772         dirs[ndirs].path = files[i].path;
8773         dirs[ndirs].length = files[i].fname - files[i].path;
8774         dirs[ndirs].count = 1;
8775         dirs[ndirs].dir_idx = ndirs;
8776         files[i].dir_idx = ndirs;
8777
8778         /* Search for a prefix.  */
8779         dirs[ndirs].prefix = -1;
8780         for (j = 0; j < ndirs; j++)
8781           if (dirs[j].length < dirs[ndirs].length
8782               && dirs[j].length > 1
8783               && (dirs[ndirs].prefix == -1
8784                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8785               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8786             dirs[ndirs].prefix = j;
8787
8788         ++ndirs;
8789       }
8790
8791   /* Now to the actual work.  We have to find a subset of the directories which
8792      allow expressing the file name using references to the directory table
8793      with the least amount of characters.  We do not do an exhaustive search
8794      where we would have to check out every combination of every single
8795      possible prefix.  Instead we use a heuristic which provides nearly optimal
8796      results in most cases and never is much off.  */
8797   saved = XALLOCAVEC (int, ndirs);
8798   savehere = XALLOCAVEC (int, ndirs);
8799
8800   memset (saved, '\0', ndirs * sizeof (saved[0]));
8801   for (i = 0; i < ndirs; i++)
8802     {
8803       int j;
8804       int total;
8805
8806       /* We can always save some space for the current directory.  But this
8807          does not mean it will be enough to justify adding the directory.  */
8808       savehere[i] = dirs[i].length;
8809       total = (savehere[i] - saved[i]) * dirs[i].count;
8810
8811       for (j = i + 1; j < ndirs; j++)
8812         {
8813           savehere[j] = 0;
8814           if (saved[j] < dirs[i].length)
8815             {
8816               /* Determine whether the dirs[i] path is a prefix of the
8817                  dirs[j] path.  */
8818               int k;
8819
8820               k = dirs[j].prefix;
8821               while (k != -1 && k != (int) i)
8822                 k = dirs[k].prefix;
8823
8824               if (k == (int) i)
8825                 {
8826                   /* Yes it is.  We can possibly save some memory by
8827                      writing the filenames in dirs[j] relative to
8828                      dirs[i].  */
8829                   savehere[j] = dirs[i].length;
8830                   total += (savehere[j] - saved[j]) * dirs[j].count;
8831                 }
8832             }
8833         }
8834
8835       /* Check whether we can save enough to justify adding the dirs[i]
8836          directory.  */
8837       if (total > dirs[i].length + 1)
8838         {
8839           /* It's worthwhile adding.  */
8840           for (j = i; j < ndirs; j++)
8841             if (savehere[j] > 0)
8842               {
8843                 /* Remember how much we saved for this directory so far.  */
8844                 saved[j] = savehere[j];
8845
8846                 /* Remember the prefix directory.  */
8847                 dirs[j].dir_idx = i;
8848               }
8849         }
8850     }
8851
8852   /* Emit the directory name table.  */
8853   idx = 1;
8854   idx_offset = dirs[0].length > 0 ? 1 : 0;
8855   for (i = 1 - idx_offset; i < ndirs; i++)
8856     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8857                             "Directory Entry: 0x%x", i + idx_offset);
8858
8859   dw2_asm_output_data (1, 0, "End directory table");
8860
8861   /* We have to emit them in the order of emitted_number since that's
8862      used in the debug info generation.  To do this efficiently we
8863      generate a back-mapping of the indices first.  */
8864   backmap = XALLOCAVEC (int, numfiles);
8865   for (i = 0; i < numfiles; i++)
8866     backmap[files[i].file_idx->emitted_number - 1] = i;
8867
8868   /* Now write all the file names.  */
8869   for (i = 0; i < numfiles; i++)
8870     {
8871       int file_idx = backmap[i];
8872       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8873
8874       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8875                               "File Entry: 0x%x", (unsigned) i + 1);
8876
8877       /* Include directory index.  */
8878       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8879
8880       /* Modification time.  */
8881       dw2_asm_output_data_uleb128 (0, NULL);
8882
8883       /* File length in bytes.  */
8884       dw2_asm_output_data_uleb128 (0, NULL);
8885     }
8886
8887   dw2_asm_output_data (1, 0, "End file name table");
8888 }
8889
8890
8891 /* Output the source line number correspondence information.  This
8892    information goes into the .debug_line section.  */
8893
8894 static void
8895 output_line_info (void)
8896 {
8897   char l1[20], l2[20], p1[20], p2[20];
8898   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8899   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8900   unsigned opc;
8901   unsigned n_op_args;
8902   unsigned long lt_index;
8903   unsigned long current_line;
8904   long line_offset;
8905   long line_delta;
8906   unsigned long current_file;
8907   unsigned long function;
8908
8909   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8910   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8911   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8912   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8913
8914   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8915     dw2_asm_output_data (4, 0xffffffff,
8916       "Initial length escape value indicating 64-bit DWARF extension");
8917   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8918                         "Length of Source Line Info");
8919   ASM_OUTPUT_LABEL (asm_out_file, l1);
8920
8921   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8922   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8923   ASM_OUTPUT_LABEL (asm_out_file, p1);
8924
8925   /* Define the architecture-dependent minimum instruction length (in
8926    bytes).  In this implementation of DWARF, this field is used for
8927    information purposes only.  Since GCC generates assembly language,
8928    we have no a priori knowledge of how many instruction bytes are
8929    generated for each source line, and therefore can use only the
8930    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8931    commands.  Accordingly, we fix this as `1', which is "correct
8932    enough" for all architectures, and don't let the target override.  */
8933   dw2_asm_output_data (1, 1,
8934                        "Minimum Instruction Length");
8935
8936   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8937                        "Default is_stmt_start flag");
8938   dw2_asm_output_data (1, DWARF_LINE_BASE,
8939                        "Line Base Value (Special Opcodes)");
8940   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8941                        "Line Range Value (Special Opcodes)");
8942   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8943                        "Special Opcode Base");
8944
8945   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8946     {
8947       switch (opc)
8948         {
8949         case DW_LNS_advance_pc:
8950         case DW_LNS_advance_line:
8951         case DW_LNS_set_file:
8952         case DW_LNS_set_column:
8953         case DW_LNS_fixed_advance_pc:
8954           n_op_args = 1;
8955           break;
8956         default:
8957           n_op_args = 0;
8958           break;
8959         }
8960
8961       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8962                            opc, n_op_args);
8963     }
8964
8965   /* Write out the information about the files we use.  */
8966   output_file_names ();
8967   ASM_OUTPUT_LABEL (asm_out_file, p2);
8968
8969   /* We used to set the address register to the first location in the text
8970      section here, but that didn't accomplish anything since we already
8971      have a line note for the opening brace of the first function.  */
8972
8973   /* Generate the line number to PC correspondence table, encoded as
8974      a series of state machine operations.  */
8975   current_file = 1;
8976   current_line = 1;
8977
8978   if (cfun && in_cold_section_p)
8979     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8980   else
8981     strcpy (prev_line_label, text_section_label);
8982   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8983     {
8984       dw_line_info_ref line_info = &line_info_table[lt_index];
8985
8986 #if 0
8987       /* Disable this optimization for now; GDB wants to see two line notes
8988          at the beginning of a function so it can find the end of the
8989          prologue.  */
8990
8991       /* Don't emit anything for redundant notes.  Just updating the
8992          address doesn't accomplish anything, because we already assume
8993          that anything after the last address is this line.  */
8994       if (line_info->dw_line_num == current_line
8995           && line_info->dw_file_num == current_file)
8996         continue;
8997 #endif
8998
8999       /* Emit debug info for the address of the current line.
9000
9001          Unfortunately, we have little choice here currently, and must always
9002          use the most general form.  GCC does not know the address delta
9003          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9004          attributes which will give an upper bound on the address range.  We
9005          could perhaps use length attributes to determine when it is safe to
9006          use DW_LNS_fixed_advance_pc.  */
9007
9008       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9009       if (0)
9010         {
9011           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9012           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9013                                "DW_LNS_fixed_advance_pc");
9014           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9015         }
9016       else
9017         {
9018           /* This can handle any delta.  This takes
9019              4+DWARF2_ADDR_SIZE bytes.  */
9020           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9021           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9022           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9023           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9024         }
9025
9026       strcpy (prev_line_label, line_label);
9027
9028       /* Emit debug info for the source file of the current line, if
9029          different from the previous line.  */
9030       if (line_info->dw_file_num != current_file)
9031         {
9032           current_file = line_info->dw_file_num;
9033           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9034           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9035         }
9036
9037       /* Emit debug info for the current line number, choosing the encoding
9038          that uses the least amount of space.  */
9039       if (line_info->dw_line_num != current_line)
9040         {
9041           line_offset = line_info->dw_line_num - current_line;
9042           line_delta = line_offset - DWARF_LINE_BASE;
9043           current_line = line_info->dw_line_num;
9044           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9045             /* This can handle deltas from -10 to 234, using the current
9046                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9047                takes 1 byte.  */
9048             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9049                                  "line %lu", current_line);
9050           else
9051             {
9052               /* This can handle any delta.  This takes at least 4 bytes,
9053                  depending on the value being encoded.  */
9054               dw2_asm_output_data (1, DW_LNS_advance_line,
9055                                    "advance to line %lu", current_line);
9056               dw2_asm_output_data_sleb128 (line_offset, NULL);
9057               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9058             }
9059         }
9060       else
9061         /* We still need to start a new row, so output a copy insn.  */
9062         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9063     }
9064
9065   /* Emit debug info for the address of the end of the function.  */
9066   if (0)
9067     {
9068       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9069                            "DW_LNS_fixed_advance_pc");
9070       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9071     }
9072   else
9073     {
9074       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9075       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9076       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9077       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9078     }
9079
9080   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9081   dw2_asm_output_data_uleb128 (1, NULL);
9082   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9083
9084   function = 0;
9085   current_file = 1;
9086   current_line = 1;
9087   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9088     {
9089       dw_separate_line_info_ref line_info
9090         = &separate_line_info_table[lt_index];
9091
9092 #if 0
9093       /* Don't emit anything for redundant notes.  */
9094       if (line_info->dw_line_num == current_line
9095           && line_info->dw_file_num == current_file
9096           && line_info->function == function)
9097         goto cont;
9098 #endif
9099
9100       /* Emit debug info for the address of the current line.  If this is
9101          a new function, or the first line of a function, then we need
9102          to handle it differently.  */
9103       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9104                                    lt_index);
9105       if (function != line_info->function)
9106         {
9107           function = line_info->function;
9108
9109           /* Set the address register to the first line in the function.  */
9110           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9111           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9112           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9113           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9114         }
9115       else
9116         {
9117           /* ??? See the DW_LNS_advance_pc comment above.  */
9118           if (0)
9119             {
9120               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9121                                    "DW_LNS_fixed_advance_pc");
9122               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9123             }
9124           else
9125             {
9126               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9127               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9128               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9129               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9130             }
9131         }
9132
9133       strcpy (prev_line_label, line_label);
9134
9135       /* Emit debug info for the source file of the current line, if
9136          different from the previous line.  */
9137       if (line_info->dw_file_num != current_file)
9138         {
9139           current_file = line_info->dw_file_num;
9140           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9141           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9142         }
9143
9144       /* Emit debug info for the current line number, choosing the encoding
9145          that uses the least amount of space.  */
9146       if (line_info->dw_line_num != current_line)
9147         {
9148           line_offset = line_info->dw_line_num - current_line;
9149           line_delta = line_offset - DWARF_LINE_BASE;
9150           current_line = line_info->dw_line_num;
9151           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9152             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9153                                  "line %lu", current_line);
9154           else
9155             {
9156               dw2_asm_output_data (1, DW_LNS_advance_line,
9157                                    "advance to line %lu", current_line);
9158               dw2_asm_output_data_sleb128 (line_offset, NULL);
9159               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9160             }
9161         }
9162       else
9163         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9164
9165 #if 0
9166     cont:
9167 #endif
9168
9169       lt_index++;
9170
9171       /* If we're done with a function, end its sequence.  */
9172       if (lt_index == separate_line_info_table_in_use
9173           || separate_line_info_table[lt_index].function != function)
9174         {
9175           current_file = 1;
9176           current_line = 1;
9177
9178           /* Emit debug info for the address of the end of the function.  */
9179           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9180           if (0)
9181             {
9182               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9183                                    "DW_LNS_fixed_advance_pc");
9184               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9185             }
9186           else
9187             {
9188               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9189               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9190               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9191               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9192             }
9193
9194           /* Output the marker for the end of this sequence.  */
9195           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9196           dw2_asm_output_data_uleb128 (1, NULL);
9197           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9198         }
9199     }
9200
9201   /* Output the marker for the end of the line number info.  */
9202   ASM_OUTPUT_LABEL (asm_out_file, l2);
9203 }
9204 \f
9205 /* Given a pointer to a tree node for some base type, return a pointer to
9206    a DIE that describes the given type.
9207
9208    This routine must only be called for GCC type nodes that correspond to
9209    Dwarf base (fundamental) types.  */
9210
9211 static dw_die_ref
9212 base_type_die (tree type)
9213 {
9214   dw_die_ref base_type_result;
9215   enum dwarf_type encoding;
9216
9217   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9218     return 0;
9219
9220   switch (TREE_CODE (type))
9221     {
9222     case INTEGER_TYPE:
9223       if (TYPE_STRING_FLAG (type))
9224         {
9225           if (TYPE_UNSIGNED (type))
9226             encoding = DW_ATE_unsigned_char;
9227           else
9228             encoding = DW_ATE_signed_char;
9229         }
9230       else if (TYPE_UNSIGNED (type))
9231         encoding = DW_ATE_unsigned;
9232       else
9233         encoding = DW_ATE_signed;
9234       break;
9235
9236     case REAL_TYPE:
9237       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9238         encoding = DW_ATE_decimal_float;
9239       else
9240         encoding = DW_ATE_float;
9241       break;
9242
9243     case FIXED_POINT_TYPE:
9244       if (TYPE_UNSIGNED (type))
9245         encoding = DW_ATE_unsigned_fixed;
9246       else
9247         encoding = DW_ATE_signed_fixed;
9248       break;
9249
9250       /* Dwarf2 doesn't know anything about complex ints, so use
9251          a user defined type for it.  */
9252     case COMPLEX_TYPE:
9253       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9254         encoding = DW_ATE_complex_float;
9255       else
9256         encoding = DW_ATE_lo_user;
9257       break;
9258
9259     case BOOLEAN_TYPE:
9260       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9261       encoding = DW_ATE_boolean;
9262       break;
9263
9264     default:
9265       /* No other TREE_CODEs are Dwarf fundamental types.  */
9266       gcc_unreachable ();
9267     }
9268
9269   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9270
9271   /* This probably indicates a bug.  */
9272   if (! TYPE_NAME (type))
9273     add_name_attribute (base_type_result, "__unknown__");
9274
9275   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9276                    int_size_in_bytes (type));
9277   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9278
9279   return base_type_result;
9280 }
9281
9282 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9283    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9284
9285 static inline int
9286 is_base_type (tree type)
9287 {
9288   switch (TREE_CODE (type))
9289     {
9290     case ERROR_MARK:
9291     case VOID_TYPE:
9292     case INTEGER_TYPE:
9293     case REAL_TYPE:
9294     case FIXED_POINT_TYPE:
9295     case COMPLEX_TYPE:
9296     case BOOLEAN_TYPE:
9297       return 1;
9298
9299     case ARRAY_TYPE:
9300     case RECORD_TYPE:
9301     case UNION_TYPE:
9302     case QUAL_UNION_TYPE:
9303     case ENUMERAL_TYPE:
9304     case FUNCTION_TYPE:
9305     case METHOD_TYPE:
9306     case POINTER_TYPE:
9307     case REFERENCE_TYPE:
9308     case OFFSET_TYPE:
9309     case LANG_TYPE:
9310     case VECTOR_TYPE:
9311       return 0;
9312
9313     default:
9314       gcc_unreachable ();
9315     }
9316
9317   return 0;
9318 }
9319
9320 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9321    node, return the size in bits for the type if it is a constant, or else
9322    return the alignment for the type if the type's size is not constant, or
9323    else return BITS_PER_WORD if the type actually turns out to be an
9324    ERROR_MARK node.  */
9325
9326 static inline unsigned HOST_WIDE_INT
9327 simple_type_size_in_bits (const_tree type)
9328 {
9329   if (TREE_CODE (type) == ERROR_MARK)
9330     return BITS_PER_WORD;
9331   else if (TYPE_SIZE (type) == NULL_TREE)
9332     return 0;
9333   else if (host_integerp (TYPE_SIZE (type), 1))
9334     return tree_low_cst (TYPE_SIZE (type), 1);
9335   else
9336     return TYPE_ALIGN (type);
9337 }
9338
9339 /* Return true if the debug information for the given type should be
9340    emitted as a subrange type.  */
9341
9342 static inline bool
9343 is_subrange_type (const_tree type)
9344 {
9345   tree subtype = TREE_TYPE (type);
9346
9347   /* Subrange types are identified by the fact that they are integer
9348      types, and that they have a subtype which is either an integer type
9349      or an enumeral type.  */
9350
9351   if (TREE_CODE (type) != INTEGER_TYPE
9352       || subtype == NULL_TREE)
9353     return false;
9354
9355   if (TREE_CODE (subtype) != INTEGER_TYPE
9356       && TREE_CODE (subtype) != ENUMERAL_TYPE
9357       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9358     return false;
9359
9360   if (TREE_CODE (type) == TREE_CODE (subtype)
9361       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9362       && TYPE_MIN_VALUE (type) != NULL
9363       && TYPE_MIN_VALUE (subtype) != NULL
9364       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9365       && TYPE_MAX_VALUE (type) != NULL
9366       && TYPE_MAX_VALUE (subtype) != NULL
9367       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9368     {
9369       /* The type and its subtype have the same representation.  If in
9370          addition the two types also have the same name, then the given
9371          type is not a subrange type, but rather a plain base type.  */
9372       /* FIXME: brobecker/2004-03-22:
9373          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9374          therefore be sufficient to check the TYPE_SIZE node pointers
9375          rather than checking the actual size.  Unfortunately, we have
9376          found some cases, such as in the Ada "integer" type, where
9377          this is not the case.  Until this problem is solved, we need to
9378          keep checking the actual size.  */
9379       tree type_name = TYPE_NAME (type);
9380       tree subtype_name = TYPE_NAME (subtype);
9381
9382       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9383         type_name = DECL_NAME (type_name);
9384
9385       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9386         subtype_name = DECL_NAME (subtype_name);
9387
9388       if (type_name == subtype_name)
9389         return false;
9390     }
9391
9392   return true;
9393 }
9394
9395 /*  Given a pointer to a tree node for a subrange type, return a pointer
9396     to a DIE that describes the given type.  */
9397
9398 static dw_die_ref
9399 subrange_type_die (tree type, dw_die_ref context_die)
9400 {
9401   dw_die_ref subrange_die;
9402   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9403
9404   if (context_die == NULL)
9405     context_die = comp_unit_die;
9406
9407   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9408
9409   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9410     {
9411       /* The size of the subrange type and its base type do not match,
9412          so we need to generate a size attribute for the subrange type.  */
9413       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9414     }
9415
9416   if (TYPE_MIN_VALUE (type) != NULL)
9417     add_bound_info (subrange_die, DW_AT_lower_bound,
9418                     TYPE_MIN_VALUE (type));
9419   if (TYPE_MAX_VALUE (type) != NULL)
9420     add_bound_info (subrange_die, DW_AT_upper_bound,
9421                     TYPE_MAX_VALUE (type));
9422
9423   return subrange_die;
9424 }
9425
9426 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9427    entry that chains various modifiers in front of the given type.  */
9428
9429 static dw_die_ref
9430 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9431                    dw_die_ref context_die)
9432 {
9433   enum tree_code code = TREE_CODE (type);
9434   dw_die_ref mod_type_die;
9435   dw_die_ref sub_die = NULL;
9436   tree item_type = NULL;
9437   tree qualified_type;
9438   tree name;
9439
9440   if (code == ERROR_MARK)
9441     return NULL;
9442
9443   /* See if we already have the appropriately qualified variant of
9444      this type.  */
9445   qualified_type
9446     = get_qualified_type (type,
9447                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9448                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9449
9450   /* If we do, then we can just use its DIE, if it exists.  */
9451   if (qualified_type)
9452     {
9453       mod_type_die = lookup_type_die (qualified_type);
9454       if (mod_type_die)
9455         return mod_type_die;
9456     }
9457
9458   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9459
9460   /* Handle C typedef types.  */
9461   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9462     {
9463       tree dtype = TREE_TYPE (name);
9464
9465       if (qualified_type == dtype)
9466         {
9467           /* For a named type, use the typedef.  */
9468           gen_type_die (qualified_type, context_die);
9469           return lookup_type_die (qualified_type);
9470         }
9471       else if (is_const_type < TYPE_READONLY (dtype)
9472                || is_volatile_type < TYPE_VOLATILE (dtype)
9473                || (is_const_type <= TYPE_READONLY (dtype)
9474                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9475                    && DECL_ORIGINAL_TYPE (name) != type))
9476         /* cv-unqualified version of named type.  Just use the unnamed
9477            type to which it refers.  */
9478         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9479                                   is_const_type, is_volatile_type,
9480                                   context_die);
9481       /* Else cv-qualified version of named type; fall through.  */
9482     }
9483
9484   if (is_const_type)
9485     {
9486       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9487       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9488     }
9489   else if (is_volatile_type)
9490     {
9491       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9492       sub_die = modified_type_die (type, 0, 0, context_die);
9493     }
9494   else if (code == POINTER_TYPE)
9495     {
9496       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9497       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9498                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9499       item_type = TREE_TYPE (type);
9500     }
9501   else if (code == REFERENCE_TYPE)
9502     {
9503       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9504       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9505                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9506       item_type = TREE_TYPE (type);
9507     }
9508   else if (is_subrange_type (type))
9509     {
9510       mod_type_die = subrange_type_die (type, context_die);
9511       item_type = TREE_TYPE (type);
9512     }
9513   else if (is_base_type (type))
9514     mod_type_die = base_type_die (type);
9515   else
9516     {
9517       gen_type_die (type, context_die);
9518
9519       /* We have to get the type_main_variant here (and pass that to the
9520          `lookup_type_die' routine) because the ..._TYPE node we have
9521          might simply be a *copy* of some original type node (where the
9522          copy was created to help us keep track of typedef names) and
9523          that copy might have a different TYPE_UID from the original
9524          ..._TYPE node.  */
9525       if (TREE_CODE (type) != VECTOR_TYPE)
9526         return lookup_type_die (type_main_variant (type));
9527       else
9528         /* Vectors have the debugging information in the type,
9529            not the main variant.  */
9530         return lookup_type_die (type);
9531     }
9532
9533   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9534      don't output a DW_TAG_typedef, since there isn't one in the
9535      user's program; just attach a DW_AT_name to the type.  */
9536   if (name
9537       && (TREE_CODE (name) != TYPE_DECL
9538           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9539     {
9540       if (TREE_CODE (name) == TYPE_DECL)
9541         /* Could just call add_name_and_src_coords_attributes here,
9542            but since this is a builtin type it doesn't have any
9543            useful source coordinates anyway.  */
9544         name = DECL_NAME (name);
9545       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9546     }
9547
9548   if (qualified_type)
9549     equate_type_number_to_die (qualified_type, mod_type_die);
9550
9551   if (item_type)
9552     /* We must do this after the equate_type_number_to_die call, in case
9553        this is a recursive type.  This ensures that the modified_type_die
9554        recursion will terminate even if the type is recursive.  Recursive
9555        types are possible in Ada.  */
9556     sub_die = modified_type_die (item_type,
9557                                  TYPE_READONLY (item_type),
9558                                  TYPE_VOLATILE (item_type),
9559                                  context_die);
9560
9561   if (sub_die != NULL)
9562     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9563
9564   return mod_type_die;
9565 }
9566
9567 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9568    an enumerated type.  */
9569
9570 static inline int
9571 type_is_enum (const_tree type)
9572 {
9573   return TREE_CODE (type) == ENUMERAL_TYPE;
9574 }
9575
9576 /* Return the DBX register number described by a given RTL node.  */
9577
9578 static unsigned int
9579 dbx_reg_number (const_rtx rtl)
9580 {
9581   unsigned regno = REGNO (rtl);
9582
9583   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9584
9585 #ifdef LEAF_REG_REMAP
9586   if (current_function_uses_only_leaf_regs)
9587     {
9588       int leaf_reg = LEAF_REG_REMAP (regno);
9589       if (leaf_reg != -1)
9590         regno = (unsigned) leaf_reg;
9591     }
9592 #endif
9593
9594   return DBX_REGISTER_NUMBER (regno);
9595 }
9596
9597 /* Optionally add a DW_OP_piece term to a location description expression.
9598    DW_OP_piece is only added if the location description expression already
9599    doesn't end with DW_OP_piece.  */
9600
9601 static void
9602 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9603 {
9604   dw_loc_descr_ref loc;
9605
9606   if (*list_head != NULL)
9607     {
9608       /* Find the end of the chain.  */
9609       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9610         ;
9611
9612       if (loc->dw_loc_opc != DW_OP_piece)
9613         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9614     }
9615 }
9616
9617 /* Return a location descriptor that designates a machine register or
9618    zero if there is none.  */
9619
9620 static dw_loc_descr_ref
9621 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9622 {
9623   rtx regs;
9624
9625   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9626     return 0;
9627
9628   regs = targetm.dwarf_register_span (rtl);
9629
9630   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9631     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9632   else
9633     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9634 }
9635
9636 /* Return a location descriptor that designates a machine register for
9637    a given hard register number.  */
9638
9639 static dw_loc_descr_ref
9640 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9641 {
9642   dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9643
9644   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9645     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9646
9647   return reg_loc_descr;
9648 }
9649
9650 /* Given an RTL of a register, return a location descriptor that
9651    designates a value that spans more than one register.  */
9652
9653 static dw_loc_descr_ref
9654 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9655                              enum var_init_status initialized)
9656 {
9657   int nregs, size, i;
9658   unsigned reg;
9659   dw_loc_descr_ref loc_result = NULL;
9660
9661   reg = REGNO (rtl);
9662 #ifdef LEAF_REG_REMAP
9663   if (current_function_uses_only_leaf_regs)
9664     {
9665       int leaf_reg = LEAF_REG_REMAP (reg);
9666       if (leaf_reg != -1)
9667         reg = (unsigned) leaf_reg;
9668     }
9669 #endif
9670   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9671   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9672
9673   /* Simple, contiguous registers.  */
9674   if (regs == NULL_RTX)
9675     {
9676       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9677
9678       loc_result = NULL;
9679       while (nregs--)
9680         {
9681           dw_loc_descr_ref t;
9682
9683           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9684                                       VAR_INIT_STATUS_INITIALIZED);
9685           add_loc_descr (&loc_result, t);
9686           add_loc_descr_op_piece (&loc_result, size);
9687           ++reg;
9688         }
9689       return loc_result;
9690     }
9691
9692   /* Now onto stupid register sets in non contiguous locations.  */
9693
9694   gcc_assert (GET_CODE (regs) == PARALLEL);
9695
9696   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9697   loc_result = NULL;
9698
9699   for (i = 0; i < XVECLEN (regs, 0); ++i)
9700     {
9701       dw_loc_descr_ref t;
9702
9703       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9704                                   VAR_INIT_STATUS_INITIALIZED);
9705       add_loc_descr (&loc_result, t);
9706       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9707       add_loc_descr_op_piece (&loc_result, size);
9708     }
9709
9710   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9711     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9712   return loc_result;
9713 }
9714
9715 #endif /* DWARF2_DEBUGGING_INFO */
9716
9717 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9718
9719 /* Return a location descriptor that designates a constant.  */
9720
9721 static dw_loc_descr_ref
9722 int_loc_descriptor (HOST_WIDE_INT i)
9723 {
9724   enum dwarf_location_atom op;
9725
9726   /* Pick the smallest representation of a constant, rather than just
9727      defaulting to the LEB encoding.  */
9728   if (i >= 0)
9729     {
9730       if (i <= 31)
9731         op = DW_OP_lit0 + i;
9732       else if (i <= 0xff)
9733         op = DW_OP_const1u;
9734       else if (i <= 0xffff)
9735         op = DW_OP_const2u;
9736       else if (HOST_BITS_PER_WIDE_INT == 32
9737                || i <= 0xffffffff)
9738         op = DW_OP_const4u;
9739       else
9740         op = DW_OP_constu;
9741     }
9742   else
9743     {
9744       if (i >= -0x80)
9745         op = DW_OP_const1s;
9746       else if (i >= -0x8000)
9747         op = DW_OP_const2s;
9748       else if (HOST_BITS_PER_WIDE_INT == 32
9749                || i >= -0x80000000)
9750         op = DW_OP_const4s;
9751       else
9752         op = DW_OP_consts;
9753     }
9754
9755   return new_loc_descr (op, i, 0);
9756 }
9757 #endif
9758
9759 #ifdef DWARF2_DEBUGGING_INFO
9760
9761 /* Return a location descriptor that designates a base+offset location.  */
9762
9763 static dw_loc_descr_ref
9764 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9765                  enum var_init_status initialized)
9766 {
9767   unsigned int regno;
9768   dw_loc_descr_ref result;
9769   dw_fde_ref fde = current_fde ();
9770
9771   /* We only use "frame base" when we're sure we're talking about the
9772      post-prologue local stack frame.  We do this by *not* running
9773      register elimination until this point, and recognizing the special
9774      argument pointer and soft frame pointer rtx's.  */
9775   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9776     {
9777       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9778
9779       if (elim != reg)
9780         {
9781           if (GET_CODE (elim) == PLUS)
9782             {
9783               offset += INTVAL (XEXP (elim, 1));
9784               elim = XEXP (elim, 0);
9785             }
9786           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9787                        && (elim == hard_frame_pointer_rtx
9788                            || elim == stack_pointer_rtx))
9789                       || elim == (frame_pointer_needed
9790                                   ? hard_frame_pointer_rtx
9791                                   : stack_pointer_rtx));
9792
9793           /* If drap register is used to align stack, use frame
9794              pointer + offset to access stack variables.  If stack
9795              is aligned without drap, use stack pointer + offset to
9796              access stack variables.  */
9797           if (crtl->stack_realign_tried
9798               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9799               && reg == frame_pointer_rtx)
9800             {
9801               int base_reg
9802                 = DWARF_FRAME_REGNUM (cfa.indirect
9803                                       ? HARD_FRAME_POINTER_REGNUM
9804                                       : STACK_POINTER_REGNUM);
9805               return new_reg_loc_descr (base_reg, offset);
9806             }
9807
9808           offset += frame_pointer_fb_offset;
9809           return new_loc_descr (DW_OP_fbreg, offset, 0);
9810         }
9811     }
9812   else if (fde
9813            && fde->drap_reg != INVALID_REGNUM
9814            && (fde->drap_reg == REGNO (reg)
9815                || fde->vdrap_reg == REGNO (reg)))
9816     {
9817       /* Use cfa+offset to represent the location of arguments passed
9818          on stack when drap is used to align stack.  */
9819       return new_loc_descr (DW_OP_fbreg, offset, 0);
9820     }
9821
9822   regno = dbx_reg_number (reg);
9823   if (regno <= 31)
9824     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9825   else
9826     result = new_loc_descr (DW_OP_bregx, regno, offset);
9827
9828   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9829     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9830
9831   return result;
9832 }
9833
9834 /* Return true if this RTL expression describes a base+offset calculation.  */
9835
9836 static inline int
9837 is_based_loc (const_rtx rtl)
9838 {
9839   return (GET_CODE (rtl) == PLUS
9840           && ((REG_P (XEXP (rtl, 0))
9841                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9842                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9843 }
9844
9845 /* Return a descriptor that describes the concatenation of N locations
9846    used to form the address of a memory location.  */
9847
9848 static dw_loc_descr_ref
9849 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9850                             enum var_init_status initialized)
9851 {
9852   unsigned int i;
9853   dw_loc_descr_ref cc_loc_result = NULL;
9854   unsigned int n = XVECLEN (concatn, 0);
9855
9856   for (i = 0; i < n; ++i)
9857     {
9858       dw_loc_descr_ref ref;
9859       rtx x = XVECEXP (concatn, 0, i);
9860
9861       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9862       if (ref == NULL)
9863         return NULL;
9864
9865       add_loc_descr (&cc_loc_result, ref);
9866       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9867     }
9868
9869   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9870     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9871
9872   return cc_loc_result;
9873 }
9874
9875 /* The following routine converts the RTL for a variable or parameter
9876    (resident in memory) into an equivalent Dwarf representation of a
9877    mechanism for getting the address of that same variable onto the top of a
9878    hypothetical "address evaluation" stack.
9879
9880    When creating memory location descriptors, we are effectively transforming
9881    the RTL for a memory-resident object into its Dwarf postfix expression
9882    equivalent.  This routine recursively descends an RTL tree, turning
9883    it into Dwarf postfix code as it goes.
9884
9885    MODE is the mode of the memory reference, needed to handle some
9886    autoincrement addressing modes.
9887
9888    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9889    location list for RTL.
9890
9891    Return 0 if we can't represent the location.  */
9892
9893 static dw_loc_descr_ref
9894 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9895                     enum var_init_status initialized)
9896 {
9897   dw_loc_descr_ref mem_loc_result = NULL;
9898   enum dwarf_location_atom op;
9899
9900   /* Note that for a dynamically sized array, the location we will generate a
9901      description of here will be the lowest numbered location which is
9902      actually within the array.  That's *not* necessarily the same as the
9903      zeroth element of the array.  */
9904
9905   rtl = targetm.delegitimize_address (rtl);
9906
9907   switch (GET_CODE (rtl))
9908     {
9909     case POST_INC:
9910     case POST_DEC:
9911     case POST_MODIFY:
9912       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9913          just fall into the SUBREG code.  */
9914
9915       /* ... fall through ...  */
9916
9917     case SUBREG:
9918       /* The case of a subreg may arise when we have a local (register)
9919          variable or a formal (register) parameter which doesn't quite fill
9920          up an entire register.  For now, just assume that it is
9921          legitimate to make the Dwarf info refer to the whole register which
9922          contains the given subreg.  */
9923       rtl = XEXP (rtl, 0);
9924
9925       /* ... fall through ...  */
9926
9927     case REG:
9928       /* Whenever a register number forms a part of the description of the
9929          method for calculating the (dynamic) address of a memory resident
9930          object, DWARF rules require the register number be referred to as
9931          a "base register".  This distinction is not based in any way upon
9932          what category of register the hardware believes the given register
9933          belongs to.  This is strictly DWARF terminology we're dealing with
9934          here. Note that in cases where the location of a memory-resident
9935          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9936          OP_CONST (0)) the actual DWARF location descriptor that we generate
9937          may just be OP_BASEREG (basereg).  This may look deceptively like
9938          the object in question was allocated to a register (rather than in
9939          memory) so DWARF consumers need to be aware of the subtle
9940          distinction between OP_REG and OP_BASEREG.  */
9941       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9942         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9943       break;
9944
9945     case MEM:
9946       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9947                                            VAR_INIT_STATUS_INITIALIZED);
9948       if (mem_loc_result != 0)
9949         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9950       break;
9951
9952     case LO_SUM:
9953          rtl = XEXP (rtl, 1);
9954
9955       /* ... fall through ...  */
9956
9957     case LABEL_REF:
9958       /* Some ports can transform a symbol ref into a label ref, because
9959          the symbol ref is too far away and has to be dumped into a constant
9960          pool.  */
9961     case CONST:
9962     case SYMBOL_REF:
9963       /* Alternatively, the symbol in the constant pool might be referenced
9964          by a different symbol.  */
9965       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9966         {
9967           bool marked;
9968           rtx tmp = get_pool_constant_mark (rtl, &marked);
9969
9970           if (GET_CODE (tmp) == SYMBOL_REF)
9971             {
9972               rtl = tmp;
9973               if (CONSTANT_POOL_ADDRESS_P (tmp))
9974                 get_pool_constant_mark (tmp, &marked);
9975               else
9976                 marked = true;
9977             }
9978
9979           /* If all references to this pool constant were optimized away,
9980              it was not output and thus we can't represent it.
9981              FIXME: might try to use DW_OP_const_value here, though
9982              DW_OP_piece complicates it.  */
9983           if (!marked)
9984             return 0;
9985         }
9986
9987       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9988       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9989       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9990       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9991       break;
9992
9993     case PRE_MODIFY:
9994       /* Extract the PLUS expression nested inside and fall into
9995          PLUS code below.  */
9996       rtl = XEXP (rtl, 1);
9997       goto plus;
9998
9999     case PRE_INC:
10000     case PRE_DEC:
10001       /* Turn these into a PLUS expression and fall into the PLUS code
10002          below.  */
10003       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10004                           GEN_INT (GET_CODE (rtl) == PRE_INC
10005                                    ? GET_MODE_UNIT_SIZE (mode)
10006                                    : -GET_MODE_UNIT_SIZE (mode)));
10007
10008       /* ... fall through ...  */
10009
10010     case PLUS:
10011     plus:
10012       if (is_based_loc (rtl))
10013         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10014                                           INTVAL (XEXP (rtl, 1)),
10015                                           VAR_INIT_STATUS_INITIALIZED);
10016       else
10017         {
10018           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10019                                                VAR_INIT_STATUS_INITIALIZED);
10020           if (mem_loc_result == 0)
10021             break;
10022
10023           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10024               && INTVAL (XEXP (rtl, 1)) >= 0)
10025             add_loc_descr (&mem_loc_result,
10026                            new_loc_descr (DW_OP_plus_uconst,
10027                                           INTVAL (XEXP (rtl, 1)), 0));
10028           else
10029             {
10030               add_loc_descr (&mem_loc_result,
10031                              mem_loc_descriptor (XEXP (rtl, 1), mode,
10032                                                  VAR_INIT_STATUS_INITIALIZED));
10033               add_loc_descr (&mem_loc_result,
10034                              new_loc_descr (DW_OP_plus, 0, 0));
10035             }
10036         }
10037       break;
10038
10039     /* If a pseudo-reg is optimized away, it is possible for it to
10040        be replaced with a MEM containing a multiply or shift.  */
10041     case MULT:
10042       op = DW_OP_mul;
10043       goto do_binop;
10044
10045     case ASHIFT:
10046       op = DW_OP_shl;
10047       goto do_binop;
10048
10049     case ASHIFTRT:
10050       op = DW_OP_shra;
10051       goto do_binop;
10052
10053     case LSHIFTRT:
10054       op = DW_OP_shr;
10055       goto do_binop;
10056
10057     do_binop:
10058       {
10059         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10060                                                    VAR_INIT_STATUS_INITIALIZED);
10061         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10062                                                    VAR_INIT_STATUS_INITIALIZED);
10063
10064         if (op0 == 0 || op1 == 0)
10065           break;
10066
10067         mem_loc_result = op0;
10068         add_loc_descr (&mem_loc_result, op1);
10069         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10070         break;
10071       }
10072
10073     case CONST_INT:
10074       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10075       break;
10076
10077     case CONCATN:
10078       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10079                                                    VAR_INIT_STATUS_INITIALIZED);
10080       break;
10081
10082     default:
10083       gcc_unreachable ();
10084     }
10085
10086   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10087     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10088
10089   return mem_loc_result;
10090 }
10091
10092 /* Return a descriptor that describes the concatenation of two locations.
10093    This is typically a complex variable.  */
10094
10095 static dw_loc_descr_ref
10096 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10097 {
10098   dw_loc_descr_ref cc_loc_result = NULL;
10099   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10100   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10101
10102   if (x0_ref == 0 || x1_ref == 0)
10103     return 0;
10104
10105   cc_loc_result = x0_ref;
10106   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10107
10108   add_loc_descr (&cc_loc_result, x1_ref);
10109   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10110
10111   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10112     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10113
10114   return cc_loc_result;
10115 }
10116
10117 /* Return a descriptor that describes the concatenation of N
10118    locations.  */
10119
10120 static dw_loc_descr_ref
10121 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10122 {
10123   unsigned int i;
10124   dw_loc_descr_ref cc_loc_result = NULL;
10125   unsigned int n = XVECLEN (concatn, 0);
10126
10127   for (i = 0; i < n; ++i)
10128     {
10129       dw_loc_descr_ref ref;
10130       rtx x = XVECEXP (concatn, 0, i);
10131
10132       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10133       if (ref == NULL)
10134         return NULL;
10135
10136       add_loc_descr (&cc_loc_result, ref);
10137       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10138     }
10139
10140   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10141     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10142
10143   return cc_loc_result;
10144 }
10145
10146 /* Output a proper Dwarf location descriptor for a variable or parameter
10147    which is either allocated in a register or in a memory location.  For a
10148    register, we just generate an OP_REG and the register number.  For a
10149    memory location we provide a Dwarf postfix expression describing how to
10150    generate the (dynamic) address of the object onto the address stack.
10151
10152    If we don't know how to describe it, return 0.  */
10153
10154 static dw_loc_descr_ref
10155 loc_descriptor (rtx rtl, enum var_init_status initialized)
10156 {
10157   dw_loc_descr_ref loc_result = NULL;
10158
10159   switch (GET_CODE (rtl))
10160     {
10161     case SUBREG:
10162       /* The case of a subreg may arise when we have a local (register)
10163          variable or a formal (register) parameter which doesn't quite fill
10164          up an entire register.  For now, just assume that it is
10165          legitimate to make the Dwarf info refer to the whole register which
10166          contains the given subreg.  */
10167       rtl = SUBREG_REG (rtl);
10168
10169       /* ... fall through ...  */
10170
10171     case REG:
10172       loc_result = reg_loc_descriptor (rtl, initialized);
10173       break;
10174
10175     case MEM:
10176       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10177                                        initialized);
10178       break;
10179
10180     case CONCAT:
10181       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10182                                           initialized);
10183       break;
10184
10185     case CONCATN:
10186       loc_result = concatn_loc_descriptor (rtl, initialized);
10187       break;
10188
10189     case VAR_LOCATION:
10190       /* Single part.  */
10191       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10192         {
10193           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10194           break;
10195         }
10196
10197       rtl = XEXP (rtl, 1);
10198       /* FALLTHRU */
10199
10200     case PARALLEL:
10201       {
10202         rtvec par_elems = XVEC (rtl, 0);
10203         int num_elem = GET_NUM_ELEM (par_elems);
10204         enum machine_mode mode;
10205         int i;
10206
10207         /* Create the first one, so we have something to add to.  */
10208         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10209                                      initialized);
10210         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10211         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10212         for (i = 1; i < num_elem; i++)
10213           {
10214             dw_loc_descr_ref temp;
10215
10216             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10217                                    initialized);
10218             add_loc_descr (&loc_result, temp);
10219             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10220             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10221           }
10222       }
10223       break;
10224
10225     default:
10226       gcc_unreachable ();
10227     }
10228
10229   return loc_result;
10230 }
10231
10232 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10233    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10234    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10235    top-level invocation, and we require the address of LOC; is 0 if we require
10236    the value of LOC.  */
10237
10238 static dw_loc_descr_ref
10239 loc_descriptor_from_tree_1 (tree loc, int want_address)
10240 {
10241   dw_loc_descr_ref ret, ret1;
10242   int have_address = 0;
10243   enum dwarf_location_atom op;
10244
10245   /* ??? Most of the time we do not take proper care for sign/zero
10246      extending the values properly.  Hopefully this won't be a real
10247      problem...  */
10248
10249   switch (TREE_CODE (loc))
10250     {
10251     case ERROR_MARK:
10252       return 0;
10253
10254     case PLACEHOLDER_EXPR:
10255       /* This case involves extracting fields from an object to determine the
10256          position of other fields.  We don't try to encode this here.  The
10257          only user of this is Ada, which encodes the needed information using
10258          the names of types.  */
10259       return 0;
10260
10261     case CALL_EXPR:
10262       return 0;
10263
10264     case PREINCREMENT_EXPR:
10265     case PREDECREMENT_EXPR:
10266     case POSTINCREMENT_EXPR:
10267     case POSTDECREMENT_EXPR:
10268       /* There are no opcodes for these operations.  */
10269       return 0;
10270
10271     case ADDR_EXPR:
10272       /* If we already want an address, there's nothing we can do.  */
10273       if (want_address)
10274         return 0;
10275
10276       /* Otherwise, process the argument and look for the address.  */
10277       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10278
10279     case VAR_DECL:
10280       if (DECL_THREAD_LOCAL_P (loc))
10281         {
10282           rtx rtl;
10283           unsigned first_op;
10284           unsigned second_op;
10285
10286           if (targetm.have_tls)
10287             {
10288               /* If this is not defined, we have no way to emit the
10289                  data.  */
10290               if (!targetm.asm_out.output_dwarf_dtprel)
10291                 return 0;
10292
10293                /* The way DW_OP_GNU_push_tls_address is specified, we
10294                   can only look up addresses of objects in the current
10295                   module.  */
10296               if (DECL_EXTERNAL (loc))
10297                 return 0;
10298               first_op = INTERNAL_DW_OP_tls_addr;
10299               second_op = DW_OP_GNU_push_tls_address;
10300             }
10301           else
10302             {
10303               if (!targetm.emutls.debug_form_tls_address)
10304                 return 0;
10305               loc = emutls_decl (loc);
10306               first_op = DW_OP_addr;
10307               second_op = DW_OP_form_tls_address;
10308             }
10309
10310           rtl = rtl_for_decl_location (loc);
10311           if (rtl == NULL_RTX)
10312             return 0;
10313
10314           if (!MEM_P (rtl))
10315             return 0;
10316           rtl = XEXP (rtl, 0);
10317           if (! CONSTANT_P (rtl))
10318             return 0;
10319
10320           ret = new_loc_descr (first_op, 0, 0);
10321           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10322           ret->dw_loc_oprnd1.v.val_addr = rtl;
10323
10324           ret1 = new_loc_descr (second_op, 0, 0);
10325           add_loc_descr (&ret, ret1);
10326
10327           have_address = 1;
10328           break;
10329         }
10330       /* FALLTHRU */
10331
10332     case PARM_DECL:
10333       if (DECL_HAS_VALUE_EXPR_P (loc))
10334         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10335                                            want_address);
10336       /* FALLTHRU */
10337
10338     case RESULT_DECL:
10339     case FUNCTION_DECL:
10340       {
10341         rtx rtl = rtl_for_decl_location (loc);
10342
10343         if (rtl == NULL_RTX)
10344           return 0;
10345         else if (GET_CODE (rtl) == CONST_INT)
10346           {
10347             HOST_WIDE_INT val = INTVAL (rtl);
10348             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10349               val &= GET_MODE_MASK (DECL_MODE (loc));
10350             ret = int_loc_descriptor (val);
10351           }
10352         else if (GET_CODE (rtl) == CONST_STRING)
10353           return 0;
10354         else if (CONSTANT_P (rtl))
10355           {
10356             ret = new_loc_descr (DW_OP_addr, 0, 0);
10357             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10358             ret->dw_loc_oprnd1.v.val_addr = rtl;
10359           }
10360         else
10361           {
10362             enum machine_mode mode;
10363
10364             /* Certain constructs can only be represented at top-level.  */
10365             if (want_address == 2)
10366               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10367
10368             mode = GET_MODE (rtl);
10369             if (MEM_P (rtl))
10370               {
10371                 rtl = XEXP (rtl, 0);
10372                 have_address = 1;
10373               }
10374             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10375           }
10376       }
10377       break;
10378
10379     case INDIRECT_REF:
10380       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10381       have_address = 1;
10382       break;
10383
10384     case COMPOUND_EXPR:
10385       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10386
10387     CASE_CONVERT:
10388     case VIEW_CONVERT_EXPR:
10389     case SAVE_EXPR:
10390     case MODIFY_EXPR:
10391       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10392
10393     case COMPONENT_REF:
10394     case BIT_FIELD_REF:
10395     case ARRAY_REF:
10396     case ARRAY_RANGE_REF:
10397       {
10398         tree obj, offset;
10399         HOST_WIDE_INT bitsize, bitpos, bytepos;
10400         enum machine_mode mode;
10401         int volatilep;
10402         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10403
10404         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10405                                    &unsignedp, &volatilep, false);
10406
10407         if (obj == loc)
10408           return 0;
10409
10410         ret = loc_descriptor_from_tree_1 (obj, 1);
10411         if (ret == 0
10412             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10413           return 0;
10414
10415         if (offset != NULL_TREE)
10416           {
10417             /* Variable offset.  */
10418             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
10419             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10420           }
10421
10422         bytepos = bitpos / BITS_PER_UNIT;
10423         if (bytepos > 0)
10424           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10425         else if (bytepos < 0)
10426           {
10427             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10428             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10429           }
10430
10431         have_address = 1;
10432         break;
10433       }
10434
10435     case INTEGER_CST:
10436       if (host_integerp (loc, 0))
10437         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10438       else
10439         return 0;
10440       break;
10441
10442     case CONSTRUCTOR:
10443       {
10444         /* Get an RTL for this, if something has been emitted.  */
10445         rtx rtl = lookup_constant_def (loc);
10446         enum machine_mode mode;
10447
10448         if (!rtl || !MEM_P (rtl))
10449           return 0;
10450         mode = GET_MODE (rtl);
10451         rtl = XEXP (rtl, 0);
10452         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10453         have_address = 1;
10454         break;
10455       }
10456
10457     case TRUTH_AND_EXPR:
10458     case TRUTH_ANDIF_EXPR:
10459     case BIT_AND_EXPR:
10460       op = DW_OP_and;
10461       goto do_binop;
10462
10463     case TRUTH_XOR_EXPR:
10464     case BIT_XOR_EXPR:
10465       op = DW_OP_xor;
10466       goto do_binop;
10467
10468     case TRUTH_OR_EXPR:
10469     case TRUTH_ORIF_EXPR:
10470     case BIT_IOR_EXPR:
10471       op = DW_OP_or;
10472       goto do_binop;
10473
10474     case FLOOR_DIV_EXPR:
10475     case CEIL_DIV_EXPR:
10476     case ROUND_DIV_EXPR:
10477     case TRUNC_DIV_EXPR:
10478       op = DW_OP_div;
10479       goto do_binop;
10480
10481     case MINUS_EXPR:
10482       op = DW_OP_minus;
10483       goto do_binop;
10484
10485     case FLOOR_MOD_EXPR:
10486     case CEIL_MOD_EXPR:
10487     case ROUND_MOD_EXPR:
10488     case TRUNC_MOD_EXPR:
10489       op = DW_OP_mod;
10490       goto do_binop;
10491
10492     case MULT_EXPR:
10493       op = DW_OP_mul;
10494       goto do_binop;
10495
10496     case LSHIFT_EXPR:
10497       op = DW_OP_shl;
10498       goto do_binop;
10499
10500     case RSHIFT_EXPR:
10501       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10502       goto do_binop;
10503
10504     case POINTER_PLUS_EXPR:
10505     case PLUS_EXPR:
10506       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10507           && host_integerp (TREE_OPERAND (loc, 1), 0))
10508         {
10509           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10510           if (ret == 0)
10511             return 0;
10512
10513           add_loc_descr (&ret,
10514                          new_loc_descr (DW_OP_plus_uconst,
10515                                         tree_low_cst (TREE_OPERAND (loc, 1),
10516                                                       0),
10517                                         0));
10518           break;
10519         }
10520
10521       op = DW_OP_plus;
10522       goto do_binop;
10523
10524     case LE_EXPR:
10525       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10526         return 0;
10527
10528       op = DW_OP_le;
10529       goto do_binop;
10530
10531     case GE_EXPR:
10532       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10533         return 0;
10534
10535       op = DW_OP_ge;
10536       goto do_binop;
10537
10538     case LT_EXPR:
10539       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10540         return 0;
10541
10542       op = DW_OP_lt;
10543       goto do_binop;
10544
10545     case GT_EXPR:
10546       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10547         return 0;
10548
10549       op = DW_OP_gt;
10550       goto do_binop;
10551
10552     case EQ_EXPR:
10553       op = DW_OP_eq;
10554       goto do_binop;
10555
10556     case NE_EXPR:
10557       op = DW_OP_ne;
10558       goto do_binop;
10559
10560     do_binop:
10561       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10562       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10563       if (ret == 0 || ret1 == 0)
10564         return 0;
10565
10566       add_loc_descr (&ret, ret1);
10567       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10568       break;
10569
10570     case TRUTH_NOT_EXPR:
10571     case BIT_NOT_EXPR:
10572       op = DW_OP_not;
10573       goto do_unop;
10574
10575     case ABS_EXPR:
10576       op = DW_OP_abs;
10577       goto do_unop;
10578
10579     case NEGATE_EXPR:
10580       op = DW_OP_neg;
10581       goto do_unop;
10582
10583     do_unop:
10584       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10585       if (ret == 0)
10586         return 0;
10587
10588       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10589       break;
10590
10591     case MIN_EXPR:
10592     case MAX_EXPR:
10593       {
10594         const enum tree_code code =
10595           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10596
10597         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10598                       build2 (code, integer_type_node,
10599                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10600                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10601       }
10602
10603       /* ... fall through ...  */
10604
10605     case COND_EXPR:
10606       {
10607         dw_loc_descr_ref lhs
10608           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10609         dw_loc_descr_ref rhs
10610           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10611         dw_loc_descr_ref bra_node, jump_node, tmp;
10612
10613         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10614         if (ret == 0 || lhs == 0 || rhs == 0)
10615           return 0;
10616
10617         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10618         add_loc_descr (&ret, bra_node);
10619
10620         add_loc_descr (&ret, rhs);
10621         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10622         add_loc_descr (&ret, jump_node);
10623
10624         add_loc_descr (&ret, lhs);
10625         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10626         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10627
10628         /* ??? Need a node to point the skip at.  Use a nop.  */
10629         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10630         add_loc_descr (&ret, tmp);
10631         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10632         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10633       }
10634       break;
10635
10636     case FIX_TRUNC_EXPR:
10637       return 0;
10638
10639     default:
10640       /* Leave front-end specific codes as simply unknown.  This comes
10641          up, for instance, with the C STMT_EXPR.  */
10642       if ((unsigned int) TREE_CODE (loc)
10643           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10644         return 0;
10645
10646 #ifdef ENABLE_CHECKING
10647       /* Otherwise this is a generic code; we should just lists all of
10648          these explicitly.  We forgot one.  */
10649       gcc_unreachable ();
10650 #else
10651       /* In a release build, we want to degrade gracefully: better to
10652          generate incomplete debugging information than to crash.  */
10653       return NULL;
10654 #endif
10655     }
10656
10657   /* Show if we can't fill the request for an address.  */
10658   if (want_address && !have_address)
10659     return 0;
10660
10661   /* If we've got an address and don't want one, dereference.  */
10662   if (!want_address && have_address && ret)
10663     {
10664       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10665
10666       if (size > DWARF2_ADDR_SIZE || size == -1)
10667         return 0;
10668       else if (size == DWARF2_ADDR_SIZE)
10669         op = DW_OP_deref;
10670       else
10671         op = DW_OP_deref_size;
10672
10673       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10674     }
10675
10676   return ret;
10677 }
10678
10679 static inline dw_loc_descr_ref
10680 loc_descriptor_from_tree (tree loc)
10681 {
10682   return loc_descriptor_from_tree_1 (loc, 2);
10683 }
10684
10685 /* Given a value, round it up to the lowest multiple of `boundary'
10686    which is not less than the value itself.  */
10687
10688 static inline HOST_WIDE_INT
10689 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10690 {
10691   return (((value + boundary - 1) / boundary) * boundary);
10692 }
10693
10694 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10695    pointer to the declared type for the relevant field variable, or return
10696    `integer_type_node' if the given node turns out to be an
10697    ERROR_MARK node.  */
10698
10699 static inline tree
10700 field_type (const_tree decl)
10701 {
10702   tree type;
10703
10704   if (TREE_CODE (decl) == ERROR_MARK)
10705     return integer_type_node;
10706
10707   type = DECL_BIT_FIELD_TYPE (decl);
10708   if (type == NULL_TREE)
10709     type = TREE_TYPE (decl);
10710
10711   return type;
10712 }
10713
10714 /* Given a pointer to a tree node, return the alignment in bits for
10715    it, or else return BITS_PER_WORD if the node actually turns out to
10716    be an ERROR_MARK node.  */
10717
10718 static inline unsigned
10719 simple_type_align_in_bits (const_tree type)
10720 {
10721   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10722 }
10723
10724 static inline unsigned
10725 simple_decl_align_in_bits (const_tree decl)
10726 {
10727   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10728 }
10729
10730 /* Return the result of rounding T up to ALIGN.  */
10731
10732 static inline HOST_WIDE_INT
10733 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10734 {
10735   /* We must be careful if T is negative because HOST_WIDE_INT can be
10736      either "above" or "below" unsigned int as per the C promotion
10737      rules, depending on the host, thus making the signedness of the
10738      direct multiplication and division unpredictable.  */
10739   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10740
10741   u += align - 1;
10742   u /= align;
10743   u *= align;
10744
10745   return (HOST_WIDE_INT) u;
10746 }
10747
10748 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10749    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10750    or return 0 if we are unable to determine what that offset is, either
10751    because the argument turns out to be a pointer to an ERROR_MARK node, or
10752    because the offset is actually variable.  (We can't handle the latter case
10753    just yet).  */
10754
10755 static HOST_WIDE_INT
10756 field_byte_offset (const_tree decl)
10757 {
10758   HOST_WIDE_INT object_offset_in_bits;
10759   HOST_WIDE_INT bitpos_int;
10760
10761   if (TREE_CODE (decl) == ERROR_MARK)
10762     return 0;
10763
10764   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10765
10766   /* We cannot yet cope with fields whose positions are variable, so
10767      for now, when we see such things, we simply return 0.  Someday, we may
10768      be able to handle such cases, but it will be damn difficult.  */
10769   if (! host_integerp (bit_position (decl), 0))
10770     return 0;
10771
10772   bitpos_int = int_bit_position (decl);
10773
10774 #ifdef PCC_BITFIELD_TYPE_MATTERS
10775   if (PCC_BITFIELD_TYPE_MATTERS)
10776     {
10777       tree type;
10778       tree field_size_tree;
10779       HOST_WIDE_INT deepest_bitpos;
10780       unsigned HOST_WIDE_INT field_size_in_bits;
10781       unsigned int type_align_in_bits;
10782       unsigned int decl_align_in_bits;
10783       unsigned HOST_WIDE_INT type_size_in_bits;
10784
10785       type = field_type (decl);
10786       field_size_tree = DECL_SIZE (decl);
10787
10788       /* The size could be unspecified if there was an error, or for
10789          a flexible array member.  */
10790       if (! field_size_tree)
10791         field_size_tree = bitsize_zero_node;
10792
10793       /* If we don't know the size of the field, pretend it's a full word.  */
10794       if (host_integerp (field_size_tree, 1))
10795         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10796       else
10797         field_size_in_bits = BITS_PER_WORD;
10798
10799       type_size_in_bits = simple_type_size_in_bits (type);
10800       type_align_in_bits = simple_type_align_in_bits (type);
10801       decl_align_in_bits = simple_decl_align_in_bits (decl);
10802
10803       /* The GCC front-end doesn't make any attempt to keep track of the
10804          starting bit offset (relative to the start of the containing
10805          structure type) of the hypothetical "containing object" for a
10806          bit-field.  Thus, when computing the byte offset value for the
10807          start of the "containing object" of a bit-field, we must deduce
10808          this information on our own. This can be rather tricky to do in
10809          some cases.  For example, handling the following structure type
10810          definition when compiling for an i386/i486 target (which only
10811          aligns long long's to 32-bit boundaries) can be very tricky:
10812
10813          struct S { int field1; long long field2:31; };
10814
10815          Fortunately, there is a simple rule-of-thumb which can be used
10816          in such cases.  When compiling for an i386/i486, GCC will
10817          allocate 8 bytes for the structure shown above.  It decides to
10818          do this based upon one simple rule for bit-field allocation.
10819          GCC allocates each "containing object" for each bit-field at
10820          the first (i.e. lowest addressed) legitimate alignment boundary
10821          (based upon the required minimum alignment for the declared
10822          type of the field) which it can possibly use, subject to the
10823          condition that there is still enough available space remaining
10824          in the containing object (when allocated at the selected point)
10825          to fully accommodate all of the bits of the bit-field itself.
10826
10827          This simple rule makes it obvious why GCC allocates 8 bytes for
10828          each object of the structure type shown above.  When looking
10829          for a place to allocate the "containing object" for `field2',
10830          the compiler simply tries to allocate a 64-bit "containing
10831          object" at each successive 32-bit boundary (starting at zero)
10832          until it finds a place to allocate that 64- bit field such that
10833          at least 31 contiguous (and previously unallocated) bits remain
10834          within that selected 64 bit field.  (As it turns out, for the
10835          example above, the compiler finds it is OK to allocate the
10836          "containing object" 64-bit field at bit-offset zero within the
10837          structure type.)
10838
10839          Here we attempt to work backwards from the limited set of facts
10840          we're given, and we try to deduce from those facts, where GCC
10841          must have believed that the containing object started (within
10842          the structure type). The value we deduce is then used (by the
10843          callers of this routine) to generate DW_AT_location and
10844          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10845          the case of DW_AT_location, regular fields as well).  */
10846
10847       /* Figure out the bit-distance from the start of the structure to
10848          the "deepest" bit of the bit-field.  */
10849       deepest_bitpos = bitpos_int + field_size_in_bits;
10850
10851       /* This is the tricky part.  Use some fancy footwork to deduce
10852          where the lowest addressed bit of the containing object must
10853          be.  */
10854       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10855
10856       /* Round up to type_align by default.  This works best for
10857          bitfields.  */
10858       object_offset_in_bits
10859         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10860
10861       if (object_offset_in_bits > bitpos_int)
10862         {
10863           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10864
10865           /* Round up to decl_align instead.  */
10866           object_offset_in_bits
10867             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10868         }
10869     }
10870   else
10871 #endif
10872     object_offset_in_bits = bitpos_int;
10873
10874   return object_offset_in_bits / BITS_PER_UNIT;
10875 }
10876 \f
10877 /* The following routines define various Dwarf attributes and any data
10878    associated with them.  */
10879
10880 /* Add a location description attribute value to a DIE.
10881
10882    This emits location attributes suitable for whole variables and
10883    whole parameters.  Note that the location attributes for struct fields are
10884    generated by the routine `data_member_location_attribute' below.  */
10885
10886 static inline void
10887 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10888                              dw_loc_descr_ref descr)
10889 {
10890   if (descr != 0)
10891     add_AT_loc (die, attr_kind, descr);
10892 }
10893
10894 /* Attach the specialized form of location attribute used for data members of
10895    struct and union types.  In the special case of a FIELD_DECL node which
10896    represents a bit-field, the "offset" part of this special location
10897    descriptor must indicate the distance in bytes from the lowest-addressed
10898    byte of the containing struct or union type to the lowest-addressed byte of
10899    the "containing object" for the bit-field.  (See the `field_byte_offset'
10900    function above).
10901
10902    For any given bit-field, the "containing object" is a hypothetical object
10903    (of some integral or enum type) within which the given bit-field lives.  The
10904    type of this hypothetical "containing object" is always the same as the
10905    declared type of the individual bit-field itself (for GCC anyway... the
10906    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10907    bytes) of the hypothetical "containing object" which will be given in the
10908    DW_AT_byte_size attribute for this bit-field.  (See the
10909    `byte_size_attribute' function below.)  It is also used when calculating the
10910    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10911    function below.)  */
10912
10913 static void
10914 add_data_member_location_attribute (dw_die_ref die, tree decl)
10915 {
10916   HOST_WIDE_INT offset;
10917   dw_loc_descr_ref loc_descr = 0;
10918
10919   if (TREE_CODE (decl) == TREE_BINFO)
10920     {
10921       /* We're working on the TAG_inheritance for a base class.  */
10922       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10923         {
10924           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10925              aren't at a fixed offset from all (sub)objects of the same
10926              type.  We need to extract the appropriate offset from our
10927              vtable.  The following dwarf expression means
10928
10929                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10930
10931              This is specific to the V3 ABI, of course.  */
10932
10933           dw_loc_descr_ref tmp;
10934
10935           /* Make a copy of the object address.  */
10936           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10937           add_loc_descr (&loc_descr, tmp);
10938
10939           /* Extract the vtable address.  */
10940           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10941           add_loc_descr (&loc_descr, tmp);
10942
10943           /* Calculate the address of the offset.  */
10944           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10945           gcc_assert (offset < 0);
10946
10947           tmp = int_loc_descriptor (-offset);
10948           add_loc_descr (&loc_descr, tmp);
10949           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10950           add_loc_descr (&loc_descr, tmp);
10951
10952           /* Extract the offset.  */
10953           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10954           add_loc_descr (&loc_descr, tmp);
10955
10956           /* Add it to the object address.  */
10957           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10958           add_loc_descr (&loc_descr, tmp);
10959         }
10960       else
10961         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10962     }
10963   else
10964     offset = field_byte_offset (decl);
10965
10966   if (! loc_descr)
10967     {
10968       enum dwarf_location_atom op;
10969
10970       /* The DWARF2 standard says that we should assume that the structure
10971          address is already on the stack, so we can specify a structure field
10972          address by using DW_OP_plus_uconst.  */
10973
10974 #ifdef MIPS_DEBUGGING_INFO
10975       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10976          operator correctly.  It works only if we leave the offset on the
10977          stack.  */
10978       op = DW_OP_constu;
10979 #else
10980       op = DW_OP_plus_uconst;
10981 #endif
10982
10983       loc_descr = new_loc_descr (op, offset, 0);
10984     }
10985
10986   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10987 }
10988
10989 /* Writes integer values to dw_vec_const array.  */
10990
10991 static void
10992 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10993 {
10994   while (size != 0)
10995     {
10996       *dest++ = val & 0xff;
10997       val >>= 8;
10998       --size;
10999     }
11000 }
11001
11002 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11003
11004 static HOST_WIDE_INT
11005 extract_int (const unsigned char *src, unsigned int size)
11006 {
11007   HOST_WIDE_INT val = 0;
11008
11009   src += size;
11010   while (size != 0)
11011     {
11012       val <<= 8;
11013       val |= *--src & 0xff;
11014       --size;
11015     }
11016   return val;
11017 }
11018
11019 /* Writes floating point values to dw_vec_const array.  */
11020
11021 static void
11022 insert_float (const_rtx rtl, unsigned char *array)
11023 {
11024   REAL_VALUE_TYPE rv;
11025   long val[4];
11026   int i;
11027
11028   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11029   real_to_target (val, &rv, GET_MODE (rtl));
11030
11031   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11032   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11033     {
11034       insert_int (val[i], 4, array);
11035       array += 4;
11036     }
11037 }
11038
11039 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11040    does not have a "location" either in memory or in a register.  These
11041    things can arise in GNU C when a constant is passed as an actual parameter
11042    to an inlined function.  They can also arise in C++ where declared
11043    constants do not necessarily get memory "homes".  */
11044
11045 static void
11046 add_const_value_attribute (dw_die_ref die, rtx rtl)
11047 {
11048   switch (GET_CODE (rtl))
11049     {
11050     case CONST_INT:
11051       {
11052         HOST_WIDE_INT val = INTVAL (rtl);
11053
11054         if (val < 0)
11055           add_AT_int (die, DW_AT_const_value, val);
11056         else
11057           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11058       }
11059       break;
11060
11061     case CONST_DOUBLE:
11062       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11063          floating-point constant.  A CONST_DOUBLE is used whenever the
11064          constant requires more than one word in order to be adequately
11065          represented.  We output CONST_DOUBLEs as blocks.  */
11066       {
11067         enum machine_mode mode = GET_MODE (rtl);
11068
11069         if (SCALAR_FLOAT_MODE_P (mode))
11070           {
11071             unsigned int length = GET_MODE_SIZE (mode);
11072             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11073
11074             insert_float (rtl, array);
11075             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11076           }
11077         else
11078           {
11079             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11080             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11081
11082             add_AT_long_long (die, DW_AT_const_value,
11083                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11084           }
11085       }
11086       break;
11087
11088     case CONST_VECTOR:
11089       {
11090         enum machine_mode mode = GET_MODE (rtl);
11091         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11092         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11093         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11094         unsigned int i;
11095         unsigned char *p;
11096
11097         switch (GET_MODE_CLASS (mode))
11098           {
11099           case MODE_VECTOR_INT:
11100             for (i = 0, p = array; i < length; i++, p += elt_size)
11101               {
11102                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11103                 HOST_WIDE_INT lo, hi;
11104
11105                 switch (GET_CODE (elt))
11106                   {
11107                   case CONST_INT:
11108                     lo = INTVAL (elt);
11109                     hi = -(lo < 0);
11110                     break;
11111
11112                   case CONST_DOUBLE:
11113                     lo = CONST_DOUBLE_LOW (elt);
11114                     hi = CONST_DOUBLE_HIGH (elt);
11115                     break;
11116
11117                   default:
11118                     gcc_unreachable ();
11119                   }
11120
11121                 if (elt_size <= sizeof (HOST_WIDE_INT))
11122                   insert_int (lo, elt_size, p);
11123                 else
11124                   {
11125                     unsigned char *p0 = p;
11126                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11127
11128                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11129                     if (WORDS_BIG_ENDIAN)
11130                       {
11131                         p0 = p1;
11132                         p1 = p;
11133                       }
11134                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11135                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11136                   }
11137               }
11138             break;
11139
11140           case MODE_VECTOR_FLOAT:
11141             for (i = 0, p = array; i < length; i++, p += elt_size)
11142               {
11143                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11144                 insert_float (elt, p);
11145               }
11146             break;
11147
11148           default:
11149             gcc_unreachable ();
11150           }
11151
11152         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11153       }
11154       break;
11155
11156     case CONST_STRING:
11157       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11158       break;
11159
11160     case SYMBOL_REF:
11161     case LABEL_REF:
11162     case CONST:
11163       add_AT_addr (die, DW_AT_const_value, rtl);
11164       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11165       break;
11166
11167     case PLUS:
11168       /* In cases where an inlined instance of an inline function is passed
11169          the address of an `auto' variable (which is local to the caller) we
11170          can get a situation where the DECL_RTL of the artificial local
11171          variable (for the inlining) which acts as a stand-in for the
11172          corresponding formal parameter (of the inline function) will look
11173          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11174          exactly a compile-time constant expression, but it isn't the address
11175          of the (artificial) local variable either.  Rather, it represents the
11176          *value* which the artificial local variable always has during its
11177          lifetime.  We currently have no way to represent such quasi-constant
11178          values in Dwarf, so for now we just punt and generate nothing.  */
11179       break;
11180
11181     default:
11182       /* No other kinds of rtx should be possible here.  */
11183       gcc_unreachable ();
11184     }
11185
11186 }
11187
11188 /* Determine whether the evaluation of EXPR references any variables
11189    or functions which aren't otherwise used (and therefore may not be
11190    output).  */
11191 static tree
11192 reference_to_unused (tree * tp, int * walk_subtrees,
11193                      void * data ATTRIBUTE_UNUSED)
11194 {
11195   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11196     *walk_subtrees = 0;
11197
11198   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11199       && ! TREE_ASM_WRITTEN (*tp))
11200     return *tp;
11201   /* ???  The C++ FE emits debug information for using decls, so
11202      putting gcc_unreachable here falls over.  See PR31899.  For now
11203      be conservative.  */
11204   else if (!cgraph_global_info_ready
11205            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11206     return *tp;
11207   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11208     {
11209       struct varpool_node *node = varpool_node (*tp);
11210       if (!node->needed)
11211         return *tp;
11212     }
11213   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11214            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11215     {
11216       struct cgraph_node *node = cgraph_node (*tp);
11217       if (!node->output)
11218         return *tp;
11219     }
11220   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11221     return *tp;
11222
11223   return NULL_TREE;
11224 }
11225
11226 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11227    for use in a later add_const_value_attribute call.  */
11228
11229 static rtx
11230 rtl_for_decl_init (tree init, tree type)
11231 {
11232   rtx rtl = NULL_RTX;
11233
11234   /* If a variable is initialized with a string constant without embedded
11235      zeros, build CONST_STRING.  */
11236   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11237     {
11238       tree enttype = TREE_TYPE (type);
11239       tree domain = TYPE_DOMAIN (type);
11240       enum machine_mode mode = TYPE_MODE (enttype);
11241
11242       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11243           && domain
11244           && integer_zerop (TYPE_MIN_VALUE (domain))
11245           && compare_tree_int (TYPE_MAX_VALUE (domain),
11246                                TREE_STRING_LENGTH (init) - 1) == 0
11247           && ((size_t) TREE_STRING_LENGTH (init)
11248               == strlen (TREE_STRING_POINTER (init)) + 1))
11249         rtl = gen_rtx_CONST_STRING (VOIDmode,
11250                                     ggc_strdup (TREE_STRING_POINTER (init)));
11251     }
11252   /* Other aggregates, and complex values, could be represented using
11253      CONCAT: FIXME!  */
11254   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11255     ;
11256   /* Vectors only work if their mode is supported by the target.
11257      FIXME: generic vectors ought to work too.  */
11258   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11259     ;
11260   /* If the initializer is something that we know will expand into an
11261      immediate RTL constant, expand it now.  We must be careful not to
11262      reference variables which won't be output.  */
11263   else if (initializer_constant_valid_p (init, type)
11264            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11265     {
11266       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11267          possible.  */
11268       if (TREE_CODE (type) == VECTOR_TYPE)
11269         switch (TREE_CODE (init))
11270           {
11271           case VECTOR_CST:
11272             break;
11273           case CONSTRUCTOR:
11274             if (TREE_CONSTANT (init))
11275               {
11276                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11277                 bool constant_p = true;
11278                 tree value;
11279                 unsigned HOST_WIDE_INT ix;
11280
11281                 /* Even when ctor is constant, it might contain non-*_CST
11282                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11283                    belong into VECTOR_CST nodes.  */
11284                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11285                   if (!CONSTANT_CLASS_P (value))
11286                     {
11287                       constant_p = false;
11288                       break;
11289                     }
11290
11291                 if (constant_p)
11292                   {
11293                     init = build_vector_from_ctor (type, elts);
11294                     break;
11295                   }
11296               }
11297             /* FALLTHRU */
11298
11299           default:
11300             return NULL;
11301           }
11302
11303       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11304
11305       /* If expand_expr returns a MEM, it wasn't immediate.  */
11306       gcc_assert (!rtl || !MEM_P (rtl));
11307     }
11308
11309   return rtl;
11310 }
11311
11312 /* Generate RTL for the variable DECL to represent its location.  */
11313
11314 static rtx
11315 rtl_for_decl_location (tree decl)
11316 {
11317   rtx rtl;
11318
11319   /* Here we have to decide where we are going to say the parameter "lives"
11320      (as far as the debugger is concerned).  We only have a couple of
11321      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11322
11323      DECL_RTL normally indicates where the parameter lives during most of the
11324      activation of the function.  If optimization is enabled however, this
11325      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11326      that the parameter doesn't really live anywhere (as far as the code
11327      generation parts of GCC are concerned) during most of the function's
11328      activation.  That will happen (for example) if the parameter is never
11329      referenced within the function.
11330
11331      We could just generate a location descriptor here for all non-NULL
11332      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11333      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11334      where DECL_RTL is NULL or is a pseudo-reg.
11335
11336      Note however that we can only get away with using DECL_INCOMING_RTL as
11337      a backup substitute for DECL_RTL in certain limited cases.  In cases
11338      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11339      we can be sure that the parameter was passed using the same type as it is
11340      declared to have within the function, and that its DECL_INCOMING_RTL
11341      points us to a place where a value of that type is passed.
11342
11343      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11344      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11345      because in these cases DECL_INCOMING_RTL points us to a value of some
11346      type which is *different* from the type of the parameter itself.  Thus,
11347      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11348      such cases, the debugger would end up (for example) trying to fetch a
11349      `float' from a place which actually contains the first part of a
11350      `double'.  That would lead to really incorrect and confusing
11351      output at debug-time.
11352
11353      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11354      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11355      are a couple of exceptions however.  On little-endian machines we can
11356      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11357      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11358      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11359      when (on a little-endian machine) a non-prototyped function has a
11360      parameter declared to be of type `short' or `char'.  In such cases,
11361      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11362      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11363      passed `int' value.  If the debugger then uses that address to fetch
11364      a `short' or a `char' (on a little-endian machine) the result will be
11365      the correct data, so we allow for such exceptional cases below.
11366
11367      Note that our goal here is to describe the place where the given formal
11368      parameter lives during most of the function's activation (i.e. between the
11369      end of the prologue and the start of the epilogue).  We'll do that as best
11370      as we can. Note however that if the given formal parameter is modified
11371      sometime during the execution of the function, then a stack backtrace (at
11372      debug-time) will show the function as having been called with the *new*
11373      value rather than the value which was originally passed in.  This happens
11374      rarely enough that it is not a major problem, but it *is* a problem, and
11375      I'd like to fix it.
11376
11377      A future version of dwarf2out.c may generate two additional attributes for
11378      any given DW_TAG_formal_parameter DIE which will describe the "passed
11379      type" and the "passed location" for the given formal parameter in addition
11380      to the attributes we now generate to indicate the "declared type" and the
11381      "active location" for each parameter.  This additional set of attributes
11382      could be used by debuggers for stack backtraces. Separately, note that
11383      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11384      This happens (for example) for inlined-instances of inline function formal
11385      parameters which are never referenced.  This really shouldn't be
11386      happening.  All PARM_DECL nodes should get valid non-NULL
11387      DECL_INCOMING_RTL values.  FIXME.  */
11388
11389   /* Use DECL_RTL as the "location" unless we find something better.  */
11390   rtl = DECL_RTL_IF_SET (decl);
11391
11392   /* When generating abstract instances, ignore everything except
11393      constants, symbols living in memory, and symbols living in
11394      fixed registers.  */
11395   if (! reload_completed)
11396     {
11397       if (rtl
11398           && (CONSTANT_P (rtl)
11399               || (MEM_P (rtl)
11400                   && CONSTANT_P (XEXP (rtl, 0)))
11401               || (REG_P (rtl)
11402                   && TREE_CODE (decl) == VAR_DECL
11403                   && TREE_STATIC (decl))))
11404         {
11405           rtl = targetm.delegitimize_address (rtl);
11406           return rtl;
11407         }
11408       rtl = NULL_RTX;
11409     }
11410   else if (TREE_CODE (decl) == PARM_DECL)
11411     {
11412       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11413         {
11414           tree declared_type = TREE_TYPE (decl);
11415           tree passed_type = DECL_ARG_TYPE (decl);
11416           enum machine_mode dmode = TYPE_MODE (declared_type);
11417           enum machine_mode pmode = TYPE_MODE (passed_type);
11418
11419           /* This decl represents a formal parameter which was optimized out.
11420              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11421              all cases where (rtl == NULL_RTX) just below.  */
11422           if (dmode == pmode)
11423             rtl = DECL_INCOMING_RTL (decl);
11424           else if (SCALAR_INT_MODE_P (dmode)
11425                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11426                    && DECL_INCOMING_RTL (decl))
11427             {
11428               rtx inc = DECL_INCOMING_RTL (decl);
11429               if (REG_P (inc))
11430                 rtl = inc;
11431               else if (MEM_P (inc))
11432                 {
11433                   if (BYTES_BIG_ENDIAN)
11434                     rtl = adjust_address_nv (inc, dmode,
11435                                              GET_MODE_SIZE (pmode)
11436                                              - GET_MODE_SIZE (dmode));
11437                   else
11438                     rtl = inc;
11439                 }
11440             }
11441         }
11442
11443       /* If the parm was passed in registers, but lives on the stack, then
11444          make a big endian correction if the mode of the type of the
11445          parameter is not the same as the mode of the rtl.  */
11446       /* ??? This is the same series of checks that are made in dbxout.c before
11447          we reach the big endian correction code there.  It isn't clear if all
11448          of these checks are necessary here, but keeping them all is the safe
11449          thing to do.  */
11450       else if (MEM_P (rtl)
11451                && XEXP (rtl, 0) != const0_rtx
11452                && ! CONSTANT_P (XEXP (rtl, 0))
11453                /* Not passed in memory.  */
11454                && !MEM_P (DECL_INCOMING_RTL (decl))
11455                /* Not passed by invisible reference.  */
11456                && (!REG_P (XEXP (rtl, 0))
11457                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11458                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11459 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11460                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11461 #endif
11462                      )
11463                /* Big endian correction check.  */
11464                && BYTES_BIG_ENDIAN
11465                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11466                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11467                    < UNITS_PER_WORD))
11468         {
11469           int offset = (UNITS_PER_WORD
11470                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11471
11472           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11473                              plus_constant (XEXP (rtl, 0), offset));
11474         }
11475     }
11476   else if (TREE_CODE (decl) == VAR_DECL
11477            && rtl
11478            && MEM_P (rtl)
11479            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11480            && BYTES_BIG_ENDIAN)
11481     {
11482       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11483       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11484
11485       /* If a variable is declared "register" yet is smaller than
11486          a register, then if we store the variable to memory, it
11487          looks like we're storing a register-sized value, when in
11488          fact we are not.  We need to adjust the offset of the
11489          storage location to reflect the actual value's bytes,
11490          else gdb will not be able to display it.  */
11491       if (rsize > dsize)
11492         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11493                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11494     }
11495
11496   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11497      and will have been substituted directly into all expressions that use it.
11498      C does not have such a concept, but C++ and other languages do.  */
11499   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11500     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11501
11502   if (rtl)
11503     rtl = targetm.delegitimize_address (rtl);
11504
11505   /* If we don't look past the constant pool, we risk emitting a
11506      reference to a constant pool entry that isn't referenced from
11507      code, and thus is not emitted.  */
11508   if (rtl)
11509     rtl = avoid_constant_pool_reference (rtl);
11510
11511   return rtl;
11512 }
11513
11514 /* We need to figure out what section we should use as the base for the
11515    address ranges where a given location is valid.
11516    1. If this particular DECL has a section associated with it, use that.
11517    2. If this function has a section associated with it, use that.
11518    3. Otherwise, use the text section.
11519    XXX: If you split a variable across multiple sections, we won't notice.  */
11520
11521 static const char *
11522 secname_for_decl (const_tree decl)
11523 {
11524   const char *secname;
11525
11526   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11527     {
11528       tree sectree = DECL_SECTION_NAME (decl);
11529       secname = TREE_STRING_POINTER (sectree);
11530     }
11531   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11532     {
11533       tree sectree = DECL_SECTION_NAME (current_function_decl);
11534       secname = TREE_STRING_POINTER (sectree);
11535     }
11536   else if (cfun && in_cold_section_p)
11537     secname = crtl->subsections.cold_section_label;
11538   else
11539     secname = text_section_label;
11540
11541   return secname;
11542 }
11543
11544 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_RTX is returned.
11545    If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
11546    value is the offset into the common block for the symbol.  */
11547
11548 static tree
11549 fortran_common (tree decl, HOST_WIDE_INT *value)
11550 {
11551   tree val_expr, cvar;
11552   enum machine_mode mode;
11553   HOST_WIDE_INT bitsize, bitpos;
11554   tree offset;
11555   int volatilep = 0, unsignedp = 0;
11556
11557   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11558      it does not have a value (the offset into the common area), or if it
11559      is thread local (as opposed to global) then it isn't common, and shouldn't
11560      be handled as such.  */
11561   if (TREE_CODE (decl) != VAR_DECL
11562       || !TREE_PUBLIC (decl)
11563       || !TREE_STATIC (decl)
11564       || !DECL_HAS_VALUE_EXPR_P (decl)
11565       || !is_fortran ())
11566     return NULL_TREE;
11567
11568   val_expr = DECL_VALUE_EXPR (decl);
11569   if (TREE_CODE (val_expr) != COMPONENT_REF)
11570     return NULL_TREE;
11571
11572   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11573                               &mode, &unsignedp, &volatilep, true);
11574
11575   if (cvar == NULL_TREE
11576       || TREE_CODE (cvar) != VAR_DECL
11577       || DECL_ARTIFICIAL (cvar)
11578       || !TREE_PUBLIC (cvar))
11579     return NULL_TREE;
11580
11581   *value = 0;
11582   if (offset != NULL)
11583     {
11584       if (!host_integerp (offset, 0))
11585         return NULL_TREE;
11586       *value = tree_low_cst (offset, 0);
11587     }
11588   if (bitpos != 0)
11589     *value += bitpos / BITS_PER_UNIT;
11590
11591   return cvar;
11592 }
11593
11594 /* Dereference a location expression LOC if DECL is passed by invisible
11595    reference.  */
11596
11597 static dw_loc_descr_ref
11598 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11599 {
11600   HOST_WIDE_INT size;
11601   enum dwarf_location_atom op;
11602
11603   if (loc == NULL)
11604     return NULL;
11605
11606   if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11607       || !DECL_BY_REFERENCE (decl))
11608     return loc;
11609
11610   size = int_size_in_bytes (TREE_TYPE (decl));
11611   if (size > DWARF2_ADDR_SIZE || size == -1)
11612     return 0;
11613   else if (size == DWARF2_ADDR_SIZE)
11614     op = DW_OP_deref;
11615   else
11616     op = DW_OP_deref_size;
11617   add_loc_descr (&loc, new_loc_descr (op, size, 0));
11618   return loc;
11619 }
11620
11621 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11622    data attribute for a variable or a parameter.  We generate the
11623    DW_AT_const_value attribute only in those cases where the given variable
11624    or parameter does not have a true "location" either in memory or in a
11625    register.  This can happen (for example) when a constant is passed as an
11626    actual argument in a call to an inline function.  (It's possible that
11627    these things can crop up in other ways also.)  Note that one type of
11628    constant value which can be passed into an inlined function is a constant
11629    pointer.  This can happen for example if an actual argument in an inlined
11630    function call evaluates to a compile-time constant address.  */
11631
11632 static void
11633 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11634                                        enum dwarf_attribute attr)
11635 {
11636   rtx rtl;
11637   dw_loc_descr_ref descr;
11638   var_loc_list *loc_list;
11639   struct var_loc_node *node;
11640   if (TREE_CODE (decl) == ERROR_MARK)
11641     return;
11642
11643   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11644               || TREE_CODE (decl) == RESULT_DECL);
11645
11646   /* See if we possibly have multiple locations for this variable.  */
11647   loc_list = lookup_decl_loc (decl);
11648
11649   /* If it truly has multiple locations, the first and last node will
11650      differ.  */
11651   if (loc_list && loc_list->first != loc_list->last)
11652     {
11653       const char *endname, *secname;
11654       dw_loc_list_ref list;
11655       rtx varloc;
11656       enum var_init_status initialized;
11657
11658       /* Now that we know what section we are using for a base,
11659          actually construct the list of locations.
11660          The first location information is what is passed to the
11661          function that creates the location list, and the remaining
11662          locations just get added on to that list.
11663          Note that we only know the start address for a location
11664          (IE location changes), so to build the range, we use
11665          the range [current location start, next location start].
11666          This means we have to special case the last node, and generate
11667          a range of [last location start, end of function label].  */
11668
11669       node = loc_list->first;
11670       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11671       secname = secname_for_decl (decl);
11672
11673       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11674         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11675       else
11676         initialized = VAR_INIT_STATUS_INITIALIZED;
11677
11678       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11679       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11680       node = node->next;
11681
11682       for (; node->next; node = node->next)
11683         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11684           {
11685             /* The variable has a location between NODE->LABEL and
11686                NODE->NEXT->LABEL.  */
11687             enum var_init_status initialized =
11688               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11689             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11690             descr = loc_by_reference (loc_descriptor (varloc, initialized),
11691                                       decl);
11692             add_loc_descr_to_loc_list (&list, descr,
11693                                        node->label, node->next->label, secname);
11694           }
11695
11696       /* If the variable has a location at the last label
11697          it keeps its location until the end of function.  */
11698       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11699         {
11700           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11701           enum var_init_status initialized =
11702             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11703
11704           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11705           if (!current_function_decl)
11706             endname = text_end_label;
11707           else
11708             {
11709               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11710                                            current_function_funcdef_no);
11711               endname = ggc_strdup (label_id);
11712             }
11713           descr = loc_by_reference (loc_descriptor (varloc, initialized),
11714                                     decl);
11715           add_loc_descr_to_loc_list (&list, descr,
11716                                      node->label, endname, secname);
11717         }
11718
11719       /* Finally, add the location list to the DIE, and we are done.  */
11720       add_AT_loc_list (die, attr, list);
11721       return;
11722     }
11723
11724   /* Try to get some constant RTL for this decl, and use that as the value of
11725      the location.  */
11726
11727   rtl = rtl_for_decl_location (decl);
11728   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11729     {
11730       add_const_value_attribute (die, rtl);
11731       return;
11732     }
11733
11734   /* If we have tried to generate the location otherwise, and it
11735      didn't work out (we wouldn't be here if we did), and we have a one entry
11736      location list, try generating a location from that.  */
11737   if (loc_list && loc_list->first)
11738     {
11739       enum var_init_status status;
11740       node = loc_list->first;
11741       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11742       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11743       if (descr)
11744         {
11745           descr = loc_by_reference (descr, decl);
11746           add_AT_location_description (die, attr, descr);
11747           return;
11748         }
11749     }
11750
11751   /* We couldn't get any rtl, so try directly generating the location
11752      description from the tree.  */
11753   descr = loc_descriptor_from_tree (decl);
11754   if (descr)
11755     {
11756       descr = loc_by_reference (descr, decl);
11757       add_AT_location_description (die, attr, descr);
11758       return;
11759     }
11760   /* None of that worked, so it must not really have a location;
11761      try adding a constant value attribute from the DECL_INITIAL.  */
11762   tree_add_const_value_attribute (die, decl);
11763 }
11764
11765 /* If we don't have a copy of this variable in memory for some reason (such
11766    as a C++ member constant that doesn't have an out-of-line definition),
11767    we should tell the debugger about the constant value.  */
11768
11769 static void
11770 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
11771 {
11772   tree init = DECL_INITIAL (decl);
11773   tree type = TREE_TYPE (decl);
11774   rtx rtl;
11775
11776   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
11777     /* OK */;
11778   else
11779     return;
11780
11781   rtl = rtl_for_decl_init (init, type);
11782   if (rtl)
11783     add_const_value_attribute (var_die, rtl);
11784 }
11785
11786 /* Convert the CFI instructions for the current function into a
11787    location list.  This is used for DW_AT_frame_base when we targeting
11788    a dwarf2 consumer that does not support the dwarf3
11789    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
11790    expressions.  */
11791
11792 static dw_loc_list_ref
11793 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
11794 {
11795   dw_fde_ref fde;
11796   dw_loc_list_ref list, *list_tail;
11797   dw_cfi_ref cfi;
11798   dw_cfa_location last_cfa, next_cfa;
11799   const char *start_label, *last_label, *section;
11800
11801   fde = current_fde ();
11802   gcc_assert (fde != NULL);
11803
11804   section = secname_for_decl (current_function_decl);
11805   list_tail = &list;
11806   list = NULL;
11807
11808   next_cfa.reg = INVALID_REGNUM;
11809   next_cfa.offset = 0;
11810   next_cfa.indirect = 0;
11811   next_cfa.base_offset = 0;
11812
11813   start_label = fde->dw_fde_begin;
11814
11815   /* ??? Bald assumption that the CIE opcode list does not contain
11816      advance opcodes.  */
11817   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11818     lookup_cfa_1 (cfi, &next_cfa);
11819
11820   last_cfa = next_cfa;
11821   last_label = start_label;
11822
11823   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11824     switch (cfi->dw_cfi_opc)
11825       {
11826       case DW_CFA_set_loc:
11827       case DW_CFA_advance_loc1:
11828       case DW_CFA_advance_loc2:
11829       case DW_CFA_advance_loc4:
11830         if (!cfa_equal_p (&last_cfa, &next_cfa))
11831           {
11832             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11833                                        start_label, last_label, section,
11834                                        list == NULL);
11835
11836             list_tail = &(*list_tail)->dw_loc_next;
11837             last_cfa = next_cfa;
11838             start_label = last_label;
11839           }
11840         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11841         break;
11842
11843       case DW_CFA_advance_loc:
11844         /* The encoding is complex enough that we should never emit this.  */
11845       case DW_CFA_remember_state:
11846       case DW_CFA_restore_state:
11847         /* We don't handle these two in this function.  It would be possible
11848            if it were to be required.  */
11849         gcc_unreachable ();
11850
11851       default:
11852         lookup_cfa_1 (cfi, &next_cfa);
11853         break;
11854       }
11855
11856   if (!cfa_equal_p (&last_cfa, &next_cfa))
11857     {
11858       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11859                                  start_label, last_label, section,
11860                                  list == NULL);
11861       list_tail = &(*list_tail)->dw_loc_next;
11862       start_label = last_label;
11863     }
11864   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11865                              start_label, fde->dw_fde_end, section,
11866                              list == NULL);
11867
11868   return list;
11869 }
11870
11871 /* Compute a displacement from the "steady-state frame pointer" to the
11872    frame base (often the same as the CFA), and store it in
11873    frame_pointer_fb_offset.  OFFSET is added to the displacement
11874    before the latter is negated.  */
11875
11876 static void
11877 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11878 {
11879   rtx reg, elim;
11880
11881 #ifdef FRAME_POINTER_CFA_OFFSET
11882   reg = frame_pointer_rtx;
11883   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11884 #else
11885   reg = arg_pointer_rtx;
11886   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11887 #endif
11888
11889   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11890   if (GET_CODE (elim) == PLUS)
11891     {
11892       offset += INTVAL (XEXP (elim, 1));
11893       elim = XEXP (elim, 0);
11894     }
11895
11896   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11897                && (elim == hard_frame_pointer_rtx
11898                    || elim == stack_pointer_rtx))
11899               || elim == (frame_pointer_needed
11900                           ? hard_frame_pointer_rtx
11901                           : stack_pointer_rtx));
11902
11903   frame_pointer_fb_offset = -offset;
11904 }
11905
11906 /* Generate a DW_AT_name attribute given some string value to be included as
11907    the value of the attribute.  */
11908
11909 static void
11910 add_name_attribute (dw_die_ref die, const char *name_string)
11911 {
11912   if (name_string != NULL && *name_string != 0)
11913     {
11914       if (demangle_name_func)
11915         name_string = (*demangle_name_func) (name_string);
11916
11917       add_AT_string (die, DW_AT_name, name_string);
11918     }
11919 }
11920
11921 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11922
11923 static void
11924 add_comp_dir_attribute (dw_die_ref die)
11925 {
11926   const char *wd = get_src_pwd ();
11927   if (wd != NULL)
11928     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11929 }
11930
11931 /* Given a tree node describing an array bound (either lower or upper) output
11932    a representation for that bound.  */
11933
11934 static void
11935 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11936 {
11937   switch (TREE_CODE (bound))
11938     {
11939     case ERROR_MARK:
11940       return;
11941
11942     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11943     case INTEGER_CST:
11944       if (! host_integerp (bound, 0)
11945           || (bound_attr == DW_AT_lower_bound
11946               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11947                   || (is_fortran () && integer_onep (bound)))))
11948         /* Use the default.  */
11949         ;
11950       else
11951         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11952       break;
11953
11954     CASE_CONVERT:
11955     case VIEW_CONVERT_EXPR:
11956       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11957       break;
11958
11959     case SAVE_EXPR:
11960       break;
11961
11962     case VAR_DECL:
11963     case PARM_DECL:
11964     case RESULT_DECL:
11965       {
11966         dw_die_ref decl_die = lookup_decl_die (bound);
11967         dw_loc_descr_ref loc;
11968
11969         /* ??? Can this happen, or should the variable have been bound
11970            first?  Probably it can, since I imagine that we try to create
11971            the types of parameters in the order in which they exist in
11972            the list, and won't have created a forward reference to a
11973            later parameter.  */
11974         if (decl_die != NULL)
11975           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11976         else
11977           {
11978             loc = loc_descriptor_from_tree_1 (bound, 0);
11979             add_AT_location_description (subrange_die, bound_attr, loc);
11980           }
11981         break;
11982       }
11983
11984     default:
11985       {
11986         /* Otherwise try to create a stack operation procedure to
11987            evaluate the value of the array bound.  */
11988
11989         dw_die_ref ctx, decl_die;
11990         dw_loc_descr_ref loc;
11991
11992         loc = loc_descriptor_from_tree (bound);
11993         if (loc == NULL)
11994           break;
11995
11996         if (current_function_decl == 0)
11997           ctx = comp_unit_die;
11998         else
11999           ctx = lookup_decl_die (current_function_decl);
12000
12001         decl_die = new_die (DW_TAG_variable, ctx, bound);
12002         add_AT_flag (decl_die, DW_AT_artificial, 1);
12003         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12004         add_AT_loc (decl_die, DW_AT_location, loc);
12005
12006         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12007         break;
12008       }
12009     }
12010 }
12011
12012 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12013    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12014    Note that the block of subscript information for an array type also
12015    includes information about the element type of the given array type.  */
12016
12017 static void
12018 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12019 {
12020   unsigned dimension_number;
12021   tree lower, upper;
12022   dw_die_ref subrange_die;
12023
12024   for (dimension_number = 0;
12025        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12026        type = TREE_TYPE (type), dimension_number++)
12027     {
12028       tree domain = TYPE_DOMAIN (type);
12029
12030       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12031         break;
12032
12033       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12034          and (in GNU C only) variable bounds.  Handle all three forms
12035          here.  */
12036       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12037       if (domain)
12038         {
12039           /* We have an array type with specified bounds.  */
12040           lower = TYPE_MIN_VALUE (domain);
12041           upper = TYPE_MAX_VALUE (domain);
12042
12043           /* Define the index type.  */
12044           if (TREE_TYPE (domain))
12045             {
12046               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12047                  TREE_TYPE field.  We can't emit debug info for this
12048                  because it is an unnamed integral type.  */
12049               if (TREE_CODE (domain) == INTEGER_TYPE
12050                   && TYPE_NAME (domain) == NULL_TREE
12051                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12052                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12053                 ;
12054               else
12055                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12056                                     type_die);
12057             }
12058
12059           /* ??? If upper is NULL, the array has unspecified length,
12060              but it does have a lower bound.  This happens with Fortran
12061                dimension arr(N:*)
12062              Since the debugger is definitely going to need to know N
12063              to produce useful results, go ahead and output the lower
12064              bound solo, and hope the debugger can cope.  */
12065
12066           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12067           if (upper)
12068             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12069         }
12070
12071       /* Otherwise we have an array type with an unspecified length.  The
12072          DWARF-2 spec does not say how to handle this; let's just leave out the
12073          bounds.  */
12074     }
12075 }
12076
12077 static void
12078 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12079 {
12080   unsigned size;
12081
12082   switch (TREE_CODE (tree_node))
12083     {
12084     case ERROR_MARK:
12085       size = 0;
12086       break;
12087     case ENUMERAL_TYPE:
12088     case RECORD_TYPE:
12089     case UNION_TYPE:
12090     case QUAL_UNION_TYPE:
12091       size = int_size_in_bytes (tree_node);
12092       break;
12093     case FIELD_DECL:
12094       /* For a data member of a struct or union, the DW_AT_byte_size is
12095          generally given as the number of bytes normally allocated for an
12096          object of the *declared* type of the member itself.  This is true
12097          even for bit-fields.  */
12098       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12099       break;
12100     default:
12101       gcc_unreachable ();
12102     }
12103
12104   /* Note that `size' might be -1 when we get to this point.  If it is, that
12105      indicates that the byte size of the entity in question is variable.  We
12106      have no good way of expressing this fact in Dwarf at the present time,
12107      so just let the -1 pass on through.  */
12108   add_AT_unsigned (die, DW_AT_byte_size, size);
12109 }
12110
12111 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12112    which specifies the distance in bits from the highest order bit of the
12113    "containing object" for the bit-field to the highest order bit of the
12114    bit-field itself.
12115
12116    For any given bit-field, the "containing object" is a hypothetical object
12117    (of some integral or enum type) within which the given bit-field lives.  The
12118    type of this hypothetical "containing object" is always the same as the
12119    declared type of the individual bit-field itself.  The determination of the
12120    exact location of the "containing object" for a bit-field is rather
12121    complicated.  It's handled by the `field_byte_offset' function (above).
12122
12123    Note that it is the size (in bytes) of the hypothetical "containing object"
12124    which will be given in the DW_AT_byte_size attribute for this bit-field.
12125    (See `byte_size_attribute' above).  */
12126
12127 static inline void
12128 add_bit_offset_attribute (dw_die_ref die, tree decl)
12129 {
12130   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12131   tree type = DECL_BIT_FIELD_TYPE (decl);
12132   HOST_WIDE_INT bitpos_int;
12133   HOST_WIDE_INT highest_order_object_bit_offset;
12134   HOST_WIDE_INT highest_order_field_bit_offset;
12135   HOST_WIDE_INT unsigned bit_offset;
12136
12137   /* Must be a field and a bit field.  */
12138   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12139
12140   /* We can't yet handle bit-fields whose offsets are variable, so if we
12141      encounter such things, just return without generating any attribute
12142      whatsoever.  Likewise for variable or too large size.  */
12143   if (! host_integerp (bit_position (decl), 0)
12144       || ! host_integerp (DECL_SIZE (decl), 1))
12145     return;
12146
12147   bitpos_int = int_bit_position (decl);
12148
12149   /* Note that the bit offset is always the distance (in bits) from the
12150      highest-order bit of the "containing object" to the highest-order bit of
12151      the bit-field itself.  Since the "high-order end" of any object or field
12152      is different on big-endian and little-endian machines, the computation
12153      below must take account of these differences.  */
12154   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12155   highest_order_field_bit_offset = bitpos_int;
12156
12157   if (! BYTES_BIG_ENDIAN)
12158     {
12159       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12160       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12161     }
12162
12163   bit_offset
12164     = (! BYTES_BIG_ENDIAN
12165        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12166        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12167
12168   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12169 }
12170
12171 /* For a FIELD_DECL node which represents a bit field, output an attribute
12172    which specifies the length in bits of the given field.  */
12173
12174 static inline void
12175 add_bit_size_attribute (dw_die_ref die, tree decl)
12176 {
12177   /* Must be a field and a bit field.  */
12178   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12179               && DECL_BIT_FIELD_TYPE (decl));
12180
12181   if (host_integerp (DECL_SIZE (decl), 1))
12182     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12183 }
12184
12185 /* If the compiled language is ANSI C, then add a 'prototyped'
12186    attribute, if arg types are given for the parameters of a function.  */
12187
12188 static inline void
12189 add_prototyped_attribute (dw_die_ref die, tree func_type)
12190 {
12191   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12192       && TYPE_ARG_TYPES (func_type) != NULL)
12193     add_AT_flag (die, DW_AT_prototyped, 1);
12194 }
12195
12196 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12197    by looking in either the type declaration or object declaration
12198    equate table.  */
12199
12200 static inline void
12201 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12202 {
12203   dw_die_ref origin_die = NULL;
12204
12205   if (TREE_CODE (origin) != FUNCTION_DECL)
12206     {
12207       /* We may have gotten separated from the block for the inlined
12208          function, if we're in an exception handler or some such; make
12209          sure that the abstract function has been written out.
12210
12211          Doing this for nested functions is wrong, however; functions are
12212          distinct units, and our context might not even be inline.  */
12213       tree fn = origin;
12214
12215       if (TYPE_P (fn))
12216         fn = TYPE_STUB_DECL (fn);
12217
12218       fn = decl_function_context (fn);
12219       if (fn)
12220         dwarf2out_abstract_function (fn);
12221     }
12222
12223   if (DECL_P (origin))
12224     origin_die = lookup_decl_die (origin);
12225   else if (TYPE_P (origin))
12226     origin_die = lookup_type_die (origin);
12227
12228   /* XXX: Functions that are never lowered don't always have correct block
12229      trees (in the case of java, they simply have no block tree, in some other
12230      languages).  For these functions, there is nothing we can really do to
12231      output correct debug info for inlined functions in all cases.  Rather
12232      than die, we'll just produce deficient debug info now, in that we will
12233      have variables without a proper abstract origin.  In the future, when all
12234      functions are lowered, we should re-add a gcc_assert (origin_die)
12235      here.  */
12236
12237   if (origin_die)
12238       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12239 }
12240
12241 /* We do not currently support the pure_virtual attribute.  */
12242
12243 static inline void
12244 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12245 {
12246   if (DECL_VINDEX (func_decl))
12247     {
12248       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12249
12250       if (host_integerp (DECL_VINDEX (func_decl), 0))
12251         add_AT_loc (die, DW_AT_vtable_elem_location,
12252                     new_loc_descr (DW_OP_constu,
12253                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12254                                    0));
12255
12256       /* GNU extension: Record what type this method came from originally.  */
12257       if (debug_info_level > DINFO_LEVEL_TERSE)
12258         add_AT_die_ref (die, DW_AT_containing_type,
12259                         lookup_type_die (DECL_CONTEXT (func_decl)));
12260     }
12261 }
12262 \f
12263 /* Add source coordinate attributes for the given decl.  */
12264
12265 static void
12266 add_src_coords_attributes (dw_die_ref die, tree decl)
12267 {
12268   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12269
12270   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12271   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12272 }
12273
12274 /* Add a DW_AT_name attribute and source coordinate attribute for the
12275    given decl, but only if it actually has a name.  */
12276
12277 static void
12278 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12279 {
12280   tree decl_name;
12281
12282   decl_name = DECL_NAME (decl);
12283   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12284     {
12285       add_name_attribute (die, dwarf2_name (decl, 0));
12286       if (! DECL_ARTIFICIAL (decl))
12287         add_src_coords_attributes (die, decl);
12288
12289       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12290           && TREE_PUBLIC (decl)
12291           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12292           && !DECL_ABSTRACT (decl)
12293           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12294           && !is_fortran ())
12295         add_AT_string (die, DW_AT_MIPS_linkage_name,
12296                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12297     }
12298
12299 #ifdef VMS_DEBUGGING_INFO
12300   /* Get the function's name, as described by its RTL.  This may be different
12301      from the DECL_NAME name used in the source file.  */
12302   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12303     {
12304       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12305                    XEXP (DECL_RTL (decl), 0));
12306       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12307     }
12308 #endif
12309 }
12310
12311 /* Push a new declaration scope.  */
12312
12313 static void
12314 push_decl_scope (tree scope)
12315 {
12316   VEC_safe_push (tree, gc, decl_scope_table, scope);
12317 }
12318
12319 /* Pop a declaration scope.  */
12320
12321 static inline void
12322 pop_decl_scope (void)
12323 {
12324   VEC_pop (tree, decl_scope_table);
12325 }
12326
12327 /* Return the DIE for the scope that immediately contains this type.
12328    Non-named types get global scope.  Named types nested in other
12329    types get their containing scope if it's open, or global scope
12330    otherwise.  All other types (i.e. function-local named types) get
12331    the current active scope.  */
12332
12333 static dw_die_ref
12334 scope_die_for (tree t, dw_die_ref context_die)
12335 {
12336   dw_die_ref scope_die = NULL;
12337   tree containing_scope;
12338   int i;
12339
12340   /* Non-types always go in the current scope.  */
12341   gcc_assert (TYPE_P (t));
12342
12343   containing_scope = TYPE_CONTEXT (t);
12344
12345   /* Use the containing namespace if it was passed in (for a declaration).  */
12346   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12347     {
12348       if (context_die == lookup_decl_die (containing_scope))
12349         /* OK */;
12350       else
12351         containing_scope = NULL_TREE;
12352     }
12353
12354   /* Ignore function type "scopes" from the C frontend.  They mean that
12355      a tagged type is local to a parmlist of a function declarator, but
12356      that isn't useful to DWARF.  */
12357   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12358     containing_scope = NULL_TREE;
12359
12360   if (containing_scope == NULL_TREE)
12361     scope_die = comp_unit_die;
12362   else if (TYPE_P (containing_scope))
12363     {
12364       /* For types, we can just look up the appropriate DIE.  But
12365          first we check to see if we're in the middle of emitting it
12366          so we know where the new DIE should go.  */
12367       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12368         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12369           break;
12370
12371       if (i < 0)
12372         {
12373           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12374                       || TREE_ASM_WRITTEN (containing_scope));
12375
12376           /* If none of the current dies are suitable, we get file scope.  */
12377           scope_die = comp_unit_die;
12378         }
12379       else
12380         scope_die = lookup_type_die (containing_scope);
12381     }
12382   else
12383     scope_die = context_die;
12384
12385   return scope_die;
12386 }
12387
12388 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12389
12390 static inline int
12391 local_scope_p (dw_die_ref context_die)
12392 {
12393   for (; context_die; context_die = context_die->die_parent)
12394     if (context_die->die_tag == DW_TAG_inlined_subroutine
12395         || context_die->die_tag == DW_TAG_subprogram)
12396       return 1;
12397
12398   return 0;
12399 }
12400
12401 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12402    whether or not to treat a DIE in this context as a declaration.  */
12403
12404 static inline int
12405 class_or_namespace_scope_p (dw_die_ref context_die)
12406 {
12407   return (context_die
12408           && (context_die->die_tag == DW_TAG_structure_type
12409               || context_die->die_tag == DW_TAG_class_type
12410               || context_die->die_tag == DW_TAG_interface_type
12411               || context_die->die_tag == DW_TAG_union_type
12412               || context_die->die_tag == DW_TAG_namespace));
12413 }
12414
12415 /* Many forms of DIEs require a "type description" attribute.  This
12416    routine locates the proper "type descriptor" die for the type given
12417    by 'type', and adds a DW_AT_type attribute below the given die.  */
12418
12419 static void
12420 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12421                     int decl_volatile, dw_die_ref context_die)
12422 {
12423   enum tree_code code  = TREE_CODE (type);
12424   dw_die_ref type_die  = NULL;
12425
12426   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12427      or fixed-point type, use the inner type.  This is because we have no
12428      support for unnamed types in base_type_die.  This can happen if this is
12429      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12430   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12431       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12432     type = TREE_TYPE (type), code = TREE_CODE (type);
12433
12434   if (code == ERROR_MARK
12435       /* Handle a special case.  For functions whose return type is void, we
12436          generate *no* type attribute.  (Note that no object may have type
12437          `void', so this only applies to function return types).  */
12438       || code == VOID_TYPE)
12439     return;
12440
12441   type_die = modified_type_die (type,
12442                                 decl_const || TYPE_READONLY (type),
12443                                 decl_volatile || TYPE_VOLATILE (type),
12444                                 context_die);
12445
12446   if (type_die != NULL)
12447     add_AT_die_ref (object_die, DW_AT_type, type_die);
12448 }
12449
12450 /* Given an object die, add the calling convention attribute for the
12451    function call type.  */
12452 static void
12453 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12454 {
12455   enum dwarf_calling_convention value = DW_CC_normal;
12456
12457   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12458
12459   /* DWARF doesn't provide a way to identify a program's source-level
12460      entry point.  DW_AT_calling_convention attributes are only meant
12461      to describe functions' calling conventions.  However, lacking a
12462      better way to signal the Fortran main program, we use this for the
12463      time being, following existing custom.  */
12464   if (is_fortran ()
12465       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12466     value = DW_CC_program;
12467
12468   /* Only add the attribute if the backend requests it, and
12469      is not DW_CC_normal.  */
12470   if (value && (value != DW_CC_normal))
12471     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12472 }
12473
12474 /* Given a tree pointer to a struct, class, union, or enum type node, return
12475    a pointer to the (string) tag name for the given type, or zero if the type
12476    was declared without a tag.  */
12477
12478 static const char *
12479 type_tag (const_tree type)
12480 {
12481   const char *name = 0;
12482
12483   if (TYPE_NAME (type) != 0)
12484     {
12485       tree t = 0;
12486
12487       /* Find the IDENTIFIER_NODE for the type name.  */
12488       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12489         t = TYPE_NAME (type);
12490
12491       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12492          a TYPE_DECL node, regardless of whether or not a `typedef' was
12493          involved.  */
12494       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12495                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12496         {
12497           /* We want to be extra verbose.  Don't call dwarf_name if
12498              DECL_NAME isn't set.  The default hook for decl_printable_name
12499              doesn't like that, and in this context it's correct to return
12500              0, instead of "<anonymous>" or the like.  */
12501           if (DECL_NAME (TYPE_NAME (type)))
12502             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12503         }
12504
12505       /* Now get the name as a string, or invent one.  */
12506       if (!name && t != 0)
12507         name = IDENTIFIER_POINTER (t);
12508     }
12509
12510   return (name == 0 || *name == '\0') ? 0 : name;
12511 }
12512
12513 /* Return the type associated with a data member, make a special check
12514    for bit field types.  */
12515
12516 static inline tree
12517 member_declared_type (const_tree member)
12518 {
12519   return (DECL_BIT_FIELD_TYPE (member)
12520           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12521 }
12522
12523 /* Get the decl's label, as described by its RTL. This may be different
12524    from the DECL_NAME name used in the source file.  */
12525
12526 #if 0
12527 static const char *
12528 decl_start_label (tree decl)
12529 {
12530   rtx x;
12531   const char *fnname;
12532
12533   x = DECL_RTL (decl);
12534   gcc_assert (MEM_P (x));
12535
12536   x = XEXP (x, 0);
12537   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12538
12539   fnname = XSTR (x, 0);
12540   return fnname;
12541 }
12542 #endif
12543 \f
12544 /* These routines generate the internal representation of the DIE's for
12545    the compilation unit.  Debugging information is collected by walking
12546    the declaration trees passed in from dwarf2out_decl().  */
12547
12548 static void
12549 gen_array_type_die (tree type, dw_die_ref context_die)
12550 {
12551   dw_die_ref scope_die = scope_die_for (type, context_die);
12552   dw_die_ref array_die;
12553
12554   /* GNU compilers represent multidimensional array types as sequences of one
12555      dimensional array types whose element types are themselves array types.
12556      We sometimes squish that down to a single array_type DIE with multiple
12557      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12558      say that we are allowed to do this kind of compression in C, because
12559      there is no difference between an array of arrays and a multidimensional
12560      array.  We don't do this for Ada to remain as close as possible to the
12561      actual representation, which is especially important against the language
12562      flexibilty wrt arrays of variable size.  */
12563
12564   bool collapse_nested_arrays = !is_ada ();
12565   tree element_type;
12566
12567   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12568      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
12569   if (TYPE_STRING_FLAG (type)
12570       && TREE_CODE (type) == ARRAY_TYPE
12571       && is_fortran ()
12572       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12573     {
12574       HOST_WIDE_INT size;
12575
12576       array_die = new_die (DW_TAG_string_type, scope_die, type);
12577       add_name_attribute (array_die, type_tag (type));
12578       equate_type_number_to_die (type, array_die);
12579       size = int_size_in_bytes (type);
12580       if (size >= 0)
12581         add_AT_unsigned (array_die, DW_AT_byte_size, size);
12582       else if (TYPE_DOMAIN (type) != NULL_TREE
12583                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12584                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12585         {
12586           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12587           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12588
12589           size = int_size_in_bytes (TREE_TYPE (szdecl));
12590           if (loc && size > 0)
12591             {
12592               add_AT_loc (array_die, DW_AT_string_length, loc);
12593               if (size != DWARF2_ADDR_SIZE)
12594                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12595             }
12596         }
12597       return;
12598     }
12599
12600   /* ??? The SGI dwarf reader fails for array of array of enum types
12601      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12602      array type comes before the outer array type.  We thus call gen_type_die
12603      before we new_die and must prevent nested array types collapsing for this
12604      target.  */
12605
12606 #ifdef MIPS_DEBUGGING_INFO
12607   gen_type_die (TREE_TYPE (type), context_die);
12608   collapse_nested_arrays = false;
12609 #endif
12610
12611   array_die = new_die (DW_TAG_array_type, scope_die, type);
12612   add_name_attribute (array_die, type_tag (type));
12613   equate_type_number_to_die (type, array_die);
12614
12615   if (TREE_CODE (type) == VECTOR_TYPE)
12616     {
12617       /* The frontend feeds us a representation for the vector as a struct
12618          containing an array.  Pull out the array type.  */
12619       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12620       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12621     }
12622
12623   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12624   if (is_fortran ()
12625       && TREE_CODE (type) == ARRAY_TYPE
12626       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12627       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12628     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12629
12630 #if 0
12631   /* We default the array ordering.  SDB will probably do
12632      the right things even if DW_AT_ordering is not present.  It's not even
12633      an issue until we start to get into multidimensional arrays anyway.  If
12634      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12635      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12636      and when we find out that we need to put these in, we will only do so
12637      for multidimensional arrays.  */
12638   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12639 #endif
12640
12641 #ifdef MIPS_DEBUGGING_INFO
12642   /* The SGI compilers handle arrays of unknown bound by setting
12643      AT_declaration and not emitting any subrange DIEs.  */
12644   if (! TYPE_DOMAIN (type))
12645     add_AT_flag (array_die, DW_AT_declaration, 1);
12646   else
12647 #endif
12648     add_subscript_info (array_die, type, collapse_nested_arrays);
12649
12650   /* Add representation of the type of the elements of this array type and
12651      emit the corresponding DIE if we haven't done it already.  */  
12652   element_type = TREE_TYPE (type);
12653   if (collapse_nested_arrays)
12654     while (TREE_CODE (element_type) == ARRAY_TYPE)
12655       {
12656         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12657           break;
12658         element_type = TREE_TYPE (element_type);
12659       }
12660
12661 #ifndef MIPS_DEBUGGING_INFO
12662   gen_type_die (element_type, context_die);
12663 #endif
12664
12665   add_type_attribute (array_die, element_type, 0, 0, context_die);
12666
12667   if (get_AT (array_die, DW_AT_name))
12668     add_pubtype (type, array_die);
12669 }
12670
12671 static dw_loc_descr_ref
12672 descr_info_loc (tree val, tree base_decl)
12673 {
12674   HOST_WIDE_INT size;
12675   dw_loc_descr_ref loc, loc2;
12676   enum dwarf_location_atom op;
12677
12678   if (val == base_decl)
12679     return new_loc_descr (DW_OP_push_object_address, 0, 0);
12680
12681   switch (TREE_CODE (val))
12682     {
12683     CASE_CONVERT:
12684       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12685     case INTEGER_CST:
12686       if (host_integerp (val, 0))
12687         return int_loc_descriptor (tree_low_cst (val, 0));
12688       break;
12689     case INDIRECT_REF:
12690       size = int_size_in_bytes (TREE_TYPE (val));
12691       if (size < 0)
12692         break;
12693       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12694       if (!loc)
12695         break;
12696       if (size == DWARF2_ADDR_SIZE)
12697         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
12698       else
12699         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
12700       return loc;
12701     case POINTER_PLUS_EXPR:
12702     case PLUS_EXPR:
12703       if (host_integerp (TREE_OPERAND (val, 1), 1)
12704           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
12705              < 16384)
12706         {
12707           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12708           if (!loc)
12709             break;
12710           add_loc_descr (&loc,
12711                          new_loc_descr (DW_OP_plus_uconst,
12712                                         tree_low_cst (TREE_OPERAND (val, 1),
12713                                                       1), 0));
12714         }
12715       else
12716         {
12717           op = DW_OP_plus;
12718         do_binop:
12719           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12720           if (!loc)
12721             break;
12722           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
12723           if (!loc2)
12724             break;
12725           add_loc_descr (&loc, loc2);
12726           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
12727         }
12728       return loc;
12729     case MINUS_EXPR:
12730       op = DW_OP_minus;
12731       goto do_binop;
12732     case MULT_EXPR:
12733       op = DW_OP_mul;
12734       goto do_binop;
12735     case EQ_EXPR:
12736       op = DW_OP_eq;
12737       goto do_binop;
12738     case NE_EXPR:
12739       op = DW_OP_ne;
12740       goto do_binop;
12741     default:
12742       break;
12743     }
12744   return NULL;
12745 }
12746
12747 static void
12748 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
12749                       tree val, tree base_decl)
12750 {
12751   dw_loc_descr_ref loc;
12752
12753   if (host_integerp (val, 0))
12754     {
12755       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
12756       return;
12757     }
12758
12759   loc = descr_info_loc (val, base_decl);
12760   if (!loc)
12761     return;
12762
12763   add_AT_loc (die, attr, loc);
12764 }
12765
12766 /* This routine generates DIE for array with hidden descriptor, details
12767    are filled into *info by a langhook.  */
12768
12769 static void
12770 gen_descr_array_type_die (tree type, struct array_descr_info *info,
12771                           dw_die_ref context_die)
12772 {
12773   dw_die_ref scope_die = scope_die_for (type, context_die);
12774   dw_die_ref array_die;
12775   int dim;
12776
12777   array_die = new_die (DW_TAG_array_type, scope_die, type);
12778   add_name_attribute (array_die, type_tag (type));
12779   equate_type_number_to_die (type, array_die);
12780
12781   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12782   if (is_fortran ()
12783       && info->ndimensions >= 2)
12784     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12785
12786   if (info->data_location)
12787     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
12788                           info->base_decl);
12789   if (info->associated)
12790     add_descr_info_field (array_die, DW_AT_associated, info->associated,
12791                           info->base_decl);
12792   if (info->allocated)
12793     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
12794                           info->base_decl);
12795
12796   for (dim = 0; dim < info->ndimensions; dim++)
12797     {
12798       dw_die_ref subrange_die
12799         = new_die (DW_TAG_subrange_type, array_die, NULL);
12800
12801       if (info->dimen[dim].lower_bound)
12802         {
12803           /* If it is the default value, omit it.  */
12804           if ((is_c_family () || is_java ())
12805               && integer_zerop (info->dimen[dim].lower_bound))
12806             ;
12807           else if (is_fortran ()
12808                    && integer_onep (info->dimen[dim].lower_bound))
12809             ;
12810           else
12811             add_descr_info_field (subrange_die, DW_AT_lower_bound,
12812                                   info->dimen[dim].lower_bound,
12813                                   info->base_decl);
12814         }
12815       if (info->dimen[dim].upper_bound)
12816         add_descr_info_field (subrange_die, DW_AT_upper_bound,
12817                               info->dimen[dim].upper_bound,
12818                               info->base_decl);
12819       if (info->dimen[dim].stride)
12820         add_descr_info_field (subrange_die, DW_AT_byte_stride,
12821                               info->dimen[dim].stride,
12822                               info->base_decl);
12823     }
12824
12825   gen_type_die (info->element_type, context_die);
12826   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
12827
12828   if (get_AT (array_die, DW_AT_name))
12829     add_pubtype (type, array_die);
12830 }
12831
12832 #if 0
12833 static void
12834 gen_entry_point_die (tree decl, dw_die_ref context_die)
12835 {
12836   tree origin = decl_ultimate_origin (decl);
12837   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
12838
12839   if (origin != NULL)
12840     add_abstract_origin_attribute (decl_die, origin);
12841   else
12842     {
12843       add_name_and_src_coords_attributes (decl_die, decl);
12844       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
12845                           0, 0, context_die);
12846     }
12847
12848   if (DECL_ABSTRACT (decl))
12849     equate_decl_number_to_die (decl, decl_die);
12850   else
12851     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
12852 }
12853 #endif
12854
12855 /* Walk through the list of incomplete types again, trying once more to
12856    emit full debugging info for them.  */
12857
12858 static void
12859 retry_incomplete_types (void)
12860 {
12861   int i;
12862
12863   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12864     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12865 }
12866
12867 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
12868
12869 static void
12870 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12871 {
12872   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12873
12874   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12875      be incomplete and such types are not marked.  */
12876   add_abstract_origin_attribute (type_die, type);
12877 }
12878
12879 /* Determine what tag to use for a record type.  */
12880
12881 static enum dwarf_tag
12882 record_type_tag (tree type)
12883 {
12884   if (! lang_hooks.types.classify_record)
12885     return DW_TAG_structure_type;
12886
12887   switch (lang_hooks.types.classify_record (type))
12888     {
12889     case RECORD_IS_STRUCT:
12890       return DW_TAG_structure_type;
12891
12892     case RECORD_IS_CLASS:
12893       return DW_TAG_class_type;
12894
12895     case RECORD_IS_INTERFACE:
12896       return DW_TAG_interface_type;
12897
12898     default:
12899       gcc_unreachable ();
12900     }
12901 }
12902
12903 /* Generate a DIE to represent an inlined instance of a structure type.  */
12904
12905 static void
12906 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12907 {
12908   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12909
12910   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12911      be incomplete and such types are not marked.  */
12912   add_abstract_origin_attribute (type_die, type);
12913 }
12914
12915 /* Generate a DIE to represent an inlined instance of a union type.  */
12916
12917 static void
12918 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12919 {
12920   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12921
12922   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12923      be incomplete and such types are not marked.  */
12924   add_abstract_origin_attribute (type_die, type);
12925 }
12926
12927 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
12928    include all of the information about the enumeration values also. Each
12929    enumerated type name/value is listed as a child of the enumerated type
12930    DIE.  */
12931
12932 static dw_die_ref
12933 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12934 {
12935   dw_die_ref type_die = lookup_type_die (type);
12936
12937   if (type_die == NULL)
12938     {
12939       type_die = new_die (DW_TAG_enumeration_type,
12940                           scope_die_for (type, context_die), type);
12941       equate_type_number_to_die (type, type_die);
12942       add_name_attribute (type_die, type_tag (type));
12943     }
12944   else if (! TYPE_SIZE (type))
12945     return type_die;
12946   else
12947     remove_AT (type_die, DW_AT_declaration);
12948
12949   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12950      given enum type is incomplete, do not generate the DW_AT_byte_size
12951      attribute or the DW_AT_element_list attribute.  */
12952   if (TYPE_SIZE (type))
12953     {
12954       tree link;
12955
12956       TREE_ASM_WRITTEN (type) = 1;
12957       add_byte_size_attribute (type_die, type);
12958       if (TYPE_STUB_DECL (type) != NULL_TREE)
12959         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12960
12961       /* If the first reference to this type was as the return type of an
12962          inline function, then it may not have a parent.  Fix this now.  */
12963       if (type_die->die_parent == NULL)
12964         add_child_die (scope_die_for (type, context_die), type_die);
12965
12966       for (link = TYPE_VALUES (type);
12967            link != NULL; link = TREE_CHAIN (link))
12968         {
12969           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12970           tree value = TREE_VALUE (link);
12971
12972           add_name_attribute (enum_die,
12973                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12974
12975           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12976             /* DWARF2 does not provide a way of indicating whether or
12977                not enumeration constants are signed or unsigned.  GDB
12978                always assumes the values are signed, so we output all
12979                values as if they were signed.  That means that
12980                enumeration constants with very large unsigned values
12981                will appear to have negative values in the debugger.  */
12982             add_AT_int (enum_die, DW_AT_const_value,
12983                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12984         }
12985     }
12986   else
12987     add_AT_flag (type_die, DW_AT_declaration, 1);
12988
12989   if (get_AT (type_die, DW_AT_name))
12990     add_pubtype (type, type_die);
12991
12992   return type_die;
12993 }
12994
12995 /* Generate a DIE to represent either a real live formal parameter decl or to
12996    represent just the type of some formal parameter position in some function
12997    type.
12998
12999    Note that this routine is a bit unusual because its argument may be a
13000    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13001    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13002    node.  If it's the former then this function is being called to output a
13003    DIE to represent a formal parameter object (or some inlining thereof).  If
13004    it's the latter, then this function is only being called to output a
13005    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13006    argument type of some subprogram type.  */
13007
13008 static dw_die_ref
13009 gen_formal_parameter_die (tree node, dw_die_ref context_die)
13010 {
13011   dw_die_ref parm_die
13012     = new_die (DW_TAG_formal_parameter, context_die, node);
13013   tree origin;
13014
13015   switch (TREE_CODE_CLASS (TREE_CODE (node)))
13016     {
13017     case tcc_declaration:
13018       origin = decl_ultimate_origin (node);
13019       if (origin != NULL)
13020         add_abstract_origin_attribute (parm_die, origin);
13021       else
13022         {
13023           tree type = TREE_TYPE (node);
13024           add_name_and_src_coords_attributes (parm_die, node);
13025           if (DECL_BY_REFERENCE (node))
13026             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13027                                 context_die);
13028           else
13029             add_type_attribute (parm_die, type,
13030                                 TREE_READONLY (node),
13031                                 TREE_THIS_VOLATILE (node),
13032                                 context_die);
13033           if (DECL_ARTIFICIAL (node))
13034             add_AT_flag (parm_die, DW_AT_artificial, 1);
13035         }
13036
13037       equate_decl_number_to_die (node, parm_die);
13038       if (! DECL_ABSTRACT (node))
13039         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
13040
13041       break;
13042
13043     case tcc_type:
13044       /* We were called with some kind of a ..._TYPE node.  */
13045       add_type_attribute (parm_die, node, 0, 0, context_die);
13046       break;
13047
13048     default:
13049       gcc_unreachable ();
13050     }
13051
13052   return parm_die;
13053 }
13054
13055 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13056    at the end of an (ANSI prototyped) formal parameters list.  */
13057
13058 static void
13059 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13060 {
13061   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13062 }
13063
13064 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13065    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13066    parameters as specified in some function type specification (except for
13067    those which appear as part of a function *definition*).  */
13068
13069 static void
13070 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13071 {
13072   tree link;
13073   tree formal_type = NULL;
13074   tree first_parm_type;
13075   tree arg;
13076
13077   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13078     {
13079       arg = DECL_ARGUMENTS (function_or_method_type);
13080       function_or_method_type = TREE_TYPE (function_or_method_type);
13081     }
13082   else
13083     arg = NULL_TREE;
13084
13085   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13086
13087   /* Make our first pass over the list of formal parameter types and output a
13088      DW_TAG_formal_parameter DIE for each one.  */
13089   for (link = first_parm_type; link; )
13090     {
13091       dw_die_ref parm_die;
13092
13093       formal_type = TREE_VALUE (link);
13094       if (formal_type == void_type_node)
13095         break;
13096
13097       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13098       parm_die = gen_formal_parameter_die (formal_type, context_die);
13099       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13100            && link == first_parm_type)
13101           || (arg && DECL_ARTIFICIAL (arg)))
13102         add_AT_flag (parm_die, DW_AT_artificial, 1);
13103
13104       link = TREE_CHAIN (link);
13105       if (arg)
13106         arg = TREE_CHAIN (arg);
13107     }
13108
13109   /* If this function type has an ellipsis, add a
13110      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13111   if (formal_type != void_type_node)
13112     gen_unspecified_parameters_die (function_or_method_type, context_die);
13113
13114   /* Make our second (and final) pass over the list of formal parameter types
13115      and output DIEs to represent those types (as necessary).  */
13116   for (link = TYPE_ARG_TYPES (function_or_method_type);
13117        link && TREE_VALUE (link);
13118        link = TREE_CHAIN (link))
13119     gen_type_die (TREE_VALUE (link), context_die);
13120 }
13121
13122 /* We want to generate the DIE for TYPE so that we can generate the
13123    die for MEMBER, which has been defined; we will need to refer back
13124    to the member declaration nested within TYPE.  If we're trying to
13125    generate minimal debug info for TYPE, processing TYPE won't do the
13126    trick; we need to attach the member declaration by hand.  */
13127
13128 static void
13129 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13130 {
13131   gen_type_die (type, context_die);
13132
13133   /* If we're trying to avoid duplicate debug info, we may not have
13134      emitted the member decl for this function.  Emit it now.  */
13135   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13136       && ! lookup_decl_die (member))
13137     {
13138       dw_die_ref type_die;
13139       gcc_assert (!decl_ultimate_origin (member));
13140
13141       push_decl_scope (type);
13142       type_die = lookup_type_die (type);
13143       if (TREE_CODE (member) == FUNCTION_DECL)
13144         gen_subprogram_die (member, type_die);
13145       else if (TREE_CODE (member) == FIELD_DECL)
13146         {
13147           /* Ignore the nameless fields that are used to skip bits but handle
13148              C++ anonymous unions and structs.  */
13149           if (DECL_NAME (member) != NULL_TREE
13150               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13151               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13152             {
13153               gen_type_die (member_declared_type (member), type_die);
13154               gen_field_die (member, type_die);
13155             }
13156         }
13157       else
13158         gen_variable_die (member, type_die);
13159
13160       pop_decl_scope ();
13161     }
13162 }
13163
13164 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13165    may later generate inlined and/or out-of-line instances of.  */
13166
13167 static void
13168 dwarf2out_abstract_function (tree decl)
13169 {
13170   dw_die_ref old_die;
13171   tree save_fn;
13172   tree context;
13173   int was_abstract = DECL_ABSTRACT (decl);
13174
13175   /* Make sure we have the actual abstract inline, not a clone.  */
13176   decl = DECL_ORIGIN (decl);
13177
13178   old_die = lookup_decl_die (decl);
13179   if (old_die && get_AT (old_die, DW_AT_inline))
13180     /* We've already generated the abstract instance.  */
13181     return;
13182
13183   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13184      we don't get confused by DECL_ABSTRACT.  */
13185   if (debug_info_level > DINFO_LEVEL_TERSE)
13186     {
13187       context = decl_class_context (decl);
13188       if (context)
13189         gen_type_die_for_member
13190           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13191     }
13192
13193   /* Pretend we've just finished compiling this function.  */
13194   save_fn = current_function_decl;
13195   current_function_decl = decl;
13196   push_cfun (DECL_STRUCT_FUNCTION (decl));
13197
13198   set_decl_abstract_flags (decl, 1);
13199   dwarf2out_decl (decl);
13200   if (! was_abstract)
13201     set_decl_abstract_flags (decl, 0);
13202
13203   current_function_decl = save_fn;
13204   pop_cfun ();
13205 }
13206
13207 /* Helper function of premark_used_types() which gets called through
13208    htab_traverse_resize().
13209
13210    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13211    marked as unused by prune_unused_types.  */
13212 static int
13213 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13214 {
13215   tree type;
13216   dw_die_ref die;
13217
13218   type = (tree) *slot;
13219   die = lookup_type_die (type);
13220   if (die != NULL)
13221     die->die_perennial_p = 1;
13222   return 1;
13223 }
13224
13225 /* Mark all members of used_types_hash as perennial.  */
13226 static void
13227 premark_used_types (void)
13228 {
13229   if (cfun && cfun->used_types_hash)
13230     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13231 }
13232
13233 /* Generate a DIE to represent a declared function (either file-scope or
13234    block-local).  */
13235
13236 static void
13237 gen_subprogram_die (tree decl, dw_die_ref context_die)
13238 {
13239   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13240   tree origin = decl_ultimate_origin (decl);
13241   dw_die_ref subr_die;
13242   tree fn_arg_types;
13243   tree outer_scope;
13244   dw_die_ref old_die = lookup_decl_die (decl);
13245   int declaration = (current_function_decl != decl
13246                      || class_or_namespace_scope_p (context_die));
13247
13248   premark_used_types ();
13249
13250   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13251      started to generate the abstract instance of an inline, decided to output
13252      its containing class, and proceeded to emit the declaration of the inline
13253      from the member list for the class.  If so, DECLARATION takes priority;
13254      we'll get back to the abstract instance when done with the class.  */
13255
13256   /* The class-scope declaration DIE must be the primary DIE.  */
13257   if (origin && declaration && class_or_namespace_scope_p (context_die))
13258     {
13259       origin = NULL;
13260       gcc_assert (!old_die);
13261     }
13262
13263   /* Now that the C++ front end lazily declares artificial member fns, we
13264      might need to retrofit the declaration into its class.  */
13265   if (!declaration && !origin && !old_die
13266       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13267       && !class_or_namespace_scope_p (context_die)
13268       && debug_info_level > DINFO_LEVEL_TERSE)
13269     old_die = force_decl_die (decl);
13270
13271   if (origin != NULL)
13272     {
13273       gcc_assert (!declaration || local_scope_p (context_die));
13274
13275       /* Fixup die_parent for the abstract instance of a nested
13276          inline function.  */
13277       if (old_die && old_die->die_parent == NULL)
13278         add_child_die (context_die, old_die);
13279
13280       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13281       add_abstract_origin_attribute (subr_die, origin);
13282     }
13283   else if (old_die)
13284     {
13285       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13286       struct dwarf_file_data * file_index = lookup_filename (s.file);
13287
13288       if (!get_AT_flag (old_die, DW_AT_declaration)
13289           /* We can have a normal definition following an inline one in the
13290              case of redefinition of GNU C extern inlines.
13291              It seems reasonable to use AT_specification in this case.  */
13292           && !get_AT (old_die, DW_AT_inline))
13293         {
13294           /* Detect and ignore this case, where we are trying to output
13295              something we have already output.  */
13296           return;
13297         }
13298
13299       /* If the definition comes from the same place as the declaration,
13300          maybe use the old DIE.  We always want the DIE for this function
13301          that has the *_pc attributes to be under comp_unit_die so the
13302          debugger can find it.  We also need to do this for abstract
13303          instances of inlines, since the spec requires the out-of-line copy
13304          to have the same parent.  For local class methods, this doesn't
13305          apply; we just use the old DIE.  */
13306       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13307           && (DECL_ARTIFICIAL (decl)
13308               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13309                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13310                       == (unsigned) s.line))))
13311         {
13312           subr_die = old_die;
13313
13314           /* Clear out the declaration attribute and the formal parameters.
13315              Do not remove all children, because it is possible that this
13316              declaration die was forced using force_decl_die(). In such
13317              cases die that forced declaration die (e.g. TAG_imported_module)
13318              is one of the children that we do not want to remove.  */
13319           remove_AT (subr_die, DW_AT_declaration);
13320           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13321         }
13322       else
13323         {
13324           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13325           add_AT_specification (subr_die, old_die);
13326           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13327             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13328           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13329             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13330         }
13331     }
13332   else
13333     {
13334       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13335
13336       if (TREE_PUBLIC (decl))
13337         add_AT_flag (subr_die, DW_AT_external, 1);
13338
13339       add_name_and_src_coords_attributes (subr_die, decl);
13340       if (debug_info_level > DINFO_LEVEL_TERSE)
13341         {
13342           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13343           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13344                               0, 0, context_die);
13345         }
13346
13347       add_pure_or_virtual_attribute (subr_die, decl);
13348       if (DECL_ARTIFICIAL (decl))
13349         add_AT_flag (subr_die, DW_AT_artificial, 1);
13350
13351       if (TREE_PROTECTED (decl))
13352         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13353       else if (TREE_PRIVATE (decl))
13354         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13355     }
13356
13357   if (declaration)
13358     {
13359       if (!old_die || !get_AT (old_die, DW_AT_inline))
13360         {
13361           add_AT_flag (subr_die, DW_AT_declaration, 1);
13362
13363           /* The first time we see a member function, it is in the context of
13364              the class to which it belongs.  We make sure of this by emitting
13365              the class first.  The next time is the definition, which is
13366              handled above.  The two may come from the same source text.
13367
13368              Note that force_decl_die() forces function declaration die. It is
13369              later reused to represent definition.  */
13370           equate_decl_number_to_die (decl, subr_die);
13371         }
13372     }
13373   else if (DECL_ABSTRACT (decl))
13374     {
13375       if (DECL_DECLARED_INLINE_P (decl))
13376         {
13377           if (cgraph_function_possibly_inlined_p (decl))
13378             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13379           else
13380             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13381         }
13382       else
13383         {
13384           if (cgraph_function_possibly_inlined_p (decl))
13385             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13386           else
13387             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13388         }
13389
13390       if (DECL_DECLARED_INLINE_P (decl)
13391           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13392         add_AT_flag (subr_die, DW_AT_artificial, 1);
13393
13394       equate_decl_number_to_die (decl, subr_die);
13395     }
13396   else if (!DECL_EXTERNAL (decl))
13397     {
13398       HOST_WIDE_INT cfa_fb_offset;
13399
13400       if (!old_die || !get_AT (old_die, DW_AT_inline))
13401         equate_decl_number_to_die (decl, subr_die);
13402
13403       if (!flag_reorder_blocks_and_partition)
13404         {
13405           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13406                                        current_function_funcdef_no);
13407           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13408           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13409                                        current_function_funcdef_no);
13410           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13411
13412           add_pubname (decl, subr_die);
13413           add_arange (decl, subr_die);
13414         }
13415       else
13416         {  /* Do nothing for now; maybe need to duplicate die, one for
13417               hot section and one for cold section, then use the hot/cold
13418               section begin/end labels to generate the aranges...  */
13419           /*
13420             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13421             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13422             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13423             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13424
13425             add_pubname (decl, subr_die);
13426             add_arange (decl, subr_die);
13427             add_arange (decl, subr_die);
13428            */
13429         }
13430
13431 #ifdef MIPS_DEBUGGING_INFO
13432       /* Add a reference to the FDE for this routine.  */
13433       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13434 #endif
13435
13436       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13437
13438       /* We define the "frame base" as the function's CFA.  This is more
13439          convenient for several reasons: (1) It's stable across the prologue
13440          and epilogue, which makes it better than just a frame pointer,
13441          (2) With dwarf3, there exists a one-byte encoding that allows us
13442          to reference the .debug_frame data by proxy, but failing that,
13443          (3) We can at least reuse the code inspection and interpretation
13444          code that determines the CFA position at various points in the
13445          function.  */
13446       /* ??? Use some command-line or configury switch to enable the use
13447          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13448          consumers that understand it; fall back to "pure" dwarf2 and
13449          convert the CFA data into a location list.  */
13450       {
13451         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13452         if (list->dw_loc_next)
13453           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13454         else
13455           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13456       }
13457
13458       /* Compute a displacement from the "steady-state frame pointer" to
13459          the CFA.  The former is what all stack slots and argument slots
13460          will reference in the rtl; the later is what we've told the
13461          debugger about.  We'll need to adjust all frame_base references
13462          by this displacement.  */
13463       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13464
13465       if (cfun->static_chain_decl)
13466         add_AT_location_description (subr_die, DW_AT_static_link,
13467                  loc_descriptor_from_tree (cfun->static_chain_decl));
13468     }
13469
13470   /* Now output descriptions of the arguments for this function. This gets
13471      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13472      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13473      `...' at the end of the formal parameter list.  In order to find out if
13474      there was a trailing ellipsis or not, we must instead look at the type
13475      associated with the FUNCTION_DECL.  This will be a node of type
13476      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13477      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13478      an ellipsis at the end.  */
13479
13480   /* In the case where we are describing a mere function declaration, all we
13481      need to do here (and all we *can* do here) is to describe the *types* of
13482      its formal parameters.  */
13483   if (debug_info_level <= DINFO_LEVEL_TERSE)
13484     ;
13485   else if (declaration)
13486     gen_formal_types_die (decl, subr_die);
13487   else
13488     {
13489       /* Generate DIEs to represent all known formal parameters.  */
13490       tree arg_decls = DECL_ARGUMENTS (decl);
13491       tree parm;
13492
13493       /* When generating DIEs, generate the unspecified_parameters DIE
13494          instead if we come across the arg "__builtin_va_alist" */
13495       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13496         if (TREE_CODE (parm) == PARM_DECL)
13497           {
13498             if (DECL_NAME (parm)
13499                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13500                             "__builtin_va_alist"))
13501               gen_unspecified_parameters_die (parm, subr_die);
13502             else
13503               gen_decl_die (parm, subr_die);
13504           }
13505
13506       /* Decide whether we need an unspecified_parameters DIE at the end.
13507          There are 2 more cases to do this for: 1) the ansi ... declaration -
13508          this is detectable when the end of the arg list is not a
13509          void_type_node 2) an unprototyped function declaration (not a
13510          definition).  This just means that we have no info about the
13511          parameters at all.  */
13512       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13513       if (fn_arg_types != NULL)
13514         {
13515           /* This is the prototyped case, check for....  */
13516           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13517             gen_unspecified_parameters_die (decl, subr_die);
13518         }
13519       else if (DECL_INITIAL (decl) == NULL_TREE)
13520         gen_unspecified_parameters_die (decl, subr_die);
13521     }
13522
13523   /* Output Dwarf info for all of the stuff within the body of the function
13524      (if it has one - it may be just a declaration).  */
13525   outer_scope = DECL_INITIAL (decl);
13526
13527   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13528      a function.  This BLOCK actually represents the outermost binding contour
13529      for the function, i.e. the contour in which the function's formal
13530      parameters and labels get declared. Curiously, it appears that the front
13531      end doesn't actually put the PARM_DECL nodes for the current function onto
13532      the BLOCK_VARS list for this outer scope, but are strung off of the
13533      DECL_ARGUMENTS list for the function instead.
13534
13535      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13536      the LABEL_DECL nodes for the function however, and we output DWARF info
13537      for those in decls_for_scope.  Just within the `outer_scope' there will be
13538      a BLOCK node representing the function's outermost pair of curly braces,
13539      and any blocks used for the base and member initializers of a C++
13540      constructor function.  */
13541   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13542     {
13543       /* Emit a DW_TAG_variable DIE for a named return value.  */
13544       if (DECL_NAME (DECL_RESULT (decl)))
13545         gen_decl_die (DECL_RESULT (decl), subr_die);
13546
13547       current_function_has_inlines = 0;
13548       decls_for_scope (outer_scope, subr_die, 0);
13549
13550 #if 0 && defined (MIPS_DEBUGGING_INFO)
13551       if (current_function_has_inlines)
13552         {
13553           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13554           if (! comp_unit_has_inlines)
13555             {
13556               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13557               comp_unit_has_inlines = 1;
13558             }
13559         }
13560 #endif
13561     }
13562   /* Add the calling convention attribute if requested.  */
13563   add_calling_convention_attribute (subr_die, decl);
13564
13565 }
13566
13567 /* Generate a DIE to represent a declared data object.  */
13568
13569 static void
13570 gen_variable_die (tree decl, dw_die_ref context_die)
13571 {
13572   HOST_WIDE_INT off;
13573   tree com_decl;
13574   dw_die_ref var_die;
13575   tree origin = decl_ultimate_origin (decl);
13576   dw_die_ref old_die = lookup_decl_die (decl);
13577   int declaration = (DECL_EXTERNAL (decl)
13578                      /* If DECL is COMDAT and has not actually been
13579                         emitted, we cannot take its address; there
13580                         might end up being no definition anywhere in
13581                         the program.  For example, consider the C++
13582                         test case:
13583
13584                           template <class T>
13585                           struct S { static const int i = 7; };
13586
13587                           template <class T>
13588                           const int S<T>::i;
13589
13590                           int f() { return S<int>::i; }
13591
13592                         Here, S<int>::i is not DECL_EXTERNAL, but no
13593                         definition is required, so the compiler will
13594                         not emit a definition.  */
13595                      || (TREE_CODE (decl) == VAR_DECL
13596                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
13597                      || class_or_namespace_scope_p (context_die));
13598
13599   com_decl = fortran_common (decl, &off);
13600
13601   /* Symbol in common gets emitted as a child of the common block, in the form
13602      of a data member.  */
13603   if (com_decl)
13604     {
13605       tree field;
13606       dw_die_ref com_die;
13607       dw_loc_descr_ref loc;
13608
13609       com_die = lookup_decl_die (decl);
13610       if (com_die)
13611         {
13612           if (get_AT (com_die, DW_AT_location) == NULL)
13613             {
13614               loc = loc_descriptor_from_tree (com_decl);
13615               if (loc)
13616                 {
13617                   if (off)
13618                     add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
13619                                                         off, 0));
13620                   add_AT_loc (com_die, DW_AT_location, loc);
13621                   remove_AT (com_die, DW_AT_declaration);
13622                 }
13623             }
13624           return;
13625         }
13626       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13627       var_die = lookup_decl_die (com_decl);
13628       loc = loc_descriptor_from_tree (com_decl);
13629       if (var_die == NULL)
13630         {
13631           const char *cnam
13632             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13633
13634           var_die = new_die (DW_TAG_common_block, context_die, decl);
13635           add_name_and_src_coords_attributes (var_die, com_decl);
13636           if (loc)
13637             {
13638               add_AT_loc (var_die, DW_AT_location, loc);
13639               /* Avoid sharing the same loc descriptor between
13640                  DW_TAG_common_block and DW_TAG_variable.  */
13641               loc = loc_descriptor_from_tree (com_decl);
13642             }
13643           else if (DECL_EXTERNAL (decl))
13644             add_AT_flag (var_die, DW_AT_declaration, 1);
13645           add_pubname_string (cnam, var_die); /* ??? needed? */
13646           equate_decl_number_to_die (com_decl, var_die);
13647         }
13648       else if (get_AT (var_die, DW_AT_location) == NULL && loc)
13649         {
13650           add_AT_loc (var_die, DW_AT_location, loc);
13651           loc = loc_descriptor_from_tree (com_decl);
13652           remove_AT (var_die, DW_AT_declaration);
13653         }
13654       com_die = new_die (DW_TAG_variable, var_die, decl);
13655       add_name_and_src_coords_attributes (com_die, decl);
13656       add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
13657                           TREE_THIS_VOLATILE (decl), context_die);
13658       add_AT_flag (com_die, DW_AT_external, 1);
13659       if (loc)
13660         {
13661           if (off)
13662             add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst, off, 0));
13663           add_AT_loc (com_die, DW_AT_location, loc);
13664         }
13665       else if (DECL_EXTERNAL (decl))
13666         add_AT_flag (com_die, DW_AT_declaration, 1);
13667       equate_decl_number_to_die (decl, com_die);
13668       return;
13669     }
13670
13671   var_die = new_die (DW_TAG_variable, context_die, decl);
13672
13673   if (origin != NULL)
13674     add_abstract_origin_attribute (var_die, origin);
13675
13676   /* Loop unrolling can create multiple blocks that refer to the same
13677      static variable, so we must test for the DW_AT_declaration flag.
13678
13679      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
13680      copy decls and set the DECL_ABSTRACT flag on them instead of
13681      sharing them.
13682
13683      ??? Duplicated blocks have been rewritten to use .debug_ranges.
13684
13685      ??? The declare_in_namespace support causes us to get two DIEs for one
13686      variable, both of which are declarations.  We want to avoid considering
13687      one to be a specification, so we must test that this DIE is not a
13688      declaration.  */
13689   else if (old_die && TREE_STATIC (decl) && ! declaration
13690            && get_AT_flag (old_die, DW_AT_declaration) == 1)
13691     {
13692       /* This is a definition of a C++ class level static.  */
13693       add_AT_specification (var_die, old_die);
13694       if (DECL_NAME (decl))
13695         {
13696           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13697           struct dwarf_file_data * file_index = lookup_filename (s.file);
13698
13699           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13700             add_AT_file (var_die, DW_AT_decl_file, file_index);
13701
13702           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13703             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
13704         }
13705     }
13706   else
13707     {
13708       tree type = TREE_TYPE (decl);
13709
13710       add_name_and_src_coords_attributes (var_die, decl);
13711       if ((TREE_CODE (decl) == PARM_DECL
13712            || TREE_CODE (decl) == RESULT_DECL)
13713           && DECL_BY_REFERENCE (decl))
13714         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
13715       else
13716         add_type_attribute (var_die, type, TREE_READONLY (decl),
13717                             TREE_THIS_VOLATILE (decl), context_die);
13718
13719       if (TREE_PUBLIC (decl))
13720         add_AT_flag (var_die, DW_AT_external, 1);
13721
13722       if (DECL_ARTIFICIAL (decl))
13723         add_AT_flag (var_die, DW_AT_artificial, 1);
13724
13725       if (TREE_PROTECTED (decl))
13726         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
13727       else if (TREE_PRIVATE (decl))
13728         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
13729     }
13730
13731   if (declaration)
13732     add_AT_flag (var_die, DW_AT_declaration, 1);
13733
13734   if (DECL_ABSTRACT (decl) || declaration)
13735     equate_decl_number_to_die (decl, var_die);
13736
13737   if (! declaration && ! DECL_ABSTRACT (decl))
13738     {
13739       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
13740       add_pubname (decl, var_die);
13741     }
13742   else
13743     tree_add_const_value_attribute (var_die, decl);
13744 }
13745
13746 /* Generate a DIE to represent a label identifier.  */
13747
13748 static void
13749 gen_label_die (tree decl, dw_die_ref context_die)
13750 {
13751   tree origin = decl_ultimate_origin (decl);
13752   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
13753   rtx insn;
13754   char label[MAX_ARTIFICIAL_LABEL_BYTES];
13755
13756   if (origin != NULL)
13757     add_abstract_origin_attribute (lbl_die, origin);
13758   else
13759     add_name_and_src_coords_attributes (lbl_die, decl);
13760
13761   if (DECL_ABSTRACT (decl))
13762     equate_decl_number_to_die (decl, lbl_die);
13763   else
13764     {
13765       insn = DECL_RTL_IF_SET (decl);
13766
13767       /* Deleted labels are programmer specified labels which have been
13768          eliminated because of various optimizations.  We still emit them
13769          here so that it is possible to put breakpoints on them.  */
13770       if (insn
13771           && (LABEL_P (insn)
13772               || ((NOTE_P (insn)
13773                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
13774         {
13775           /* When optimization is enabled (via -O) some parts of the compiler
13776              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
13777              represent source-level labels which were explicitly declared by
13778              the user.  This really shouldn't be happening though, so catch
13779              it if it ever does happen.  */
13780           gcc_assert (!INSN_DELETED_P (insn));
13781
13782           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
13783           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
13784         }
13785     }
13786 }
13787
13788 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
13789    attributes to the DIE for a block STMT, to describe where the inlined
13790    function was called from.  This is similar to add_src_coords_attributes.  */
13791
13792 static inline void
13793 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
13794 {
13795   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
13796
13797   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
13798   add_AT_unsigned (die, DW_AT_call_line, s.line);
13799 }
13800
13801
13802 /* If STMT's abstract origin is a function declaration and STMT's
13803    first subblock's abstract origin is the function's outermost block,
13804    then we're looking at the main entry point.  */
13805 static bool
13806 is_inlined_entry_point (const_tree stmt)
13807 {
13808   tree decl, block;
13809
13810   if (!stmt || TREE_CODE (stmt) != BLOCK)
13811     return false;
13812
13813   decl = block_ultimate_origin (stmt);
13814
13815   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
13816     return false;
13817
13818   block = BLOCK_SUBBLOCKS (stmt);
13819
13820   if (block)
13821     {
13822       if (TREE_CODE (block) != BLOCK)
13823         return false;
13824
13825       block = block_ultimate_origin (block);
13826     }
13827
13828   return block == DECL_INITIAL (decl);
13829 }
13830
13831 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
13832    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
13833
13834 static inline void
13835 add_high_low_attributes (tree stmt, dw_die_ref die)
13836 {
13837   char label[MAX_ARTIFICIAL_LABEL_BYTES];
13838
13839   if (BLOCK_FRAGMENT_CHAIN (stmt))
13840     {
13841       tree chain;
13842
13843       if (is_inlined_entry_point (stmt))
13844         {
13845           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13846                                        BLOCK_NUMBER (stmt));
13847           add_AT_lbl_id (die, DW_AT_entry_pc, label);
13848         }
13849
13850       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
13851
13852       chain = BLOCK_FRAGMENT_CHAIN (stmt);
13853       do
13854         {
13855           add_ranges (chain);
13856           chain = BLOCK_FRAGMENT_CHAIN (chain);
13857         }
13858       while (chain);
13859       add_ranges (NULL);
13860     }
13861   else
13862     {
13863       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
13864                                    BLOCK_NUMBER (stmt));
13865       add_AT_lbl_id (die, DW_AT_low_pc, label);
13866       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
13867                                    BLOCK_NUMBER (stmt));
13868       add_AT_lbl_id (die, DW_AT_high_pc, label);
13869     }
13870 }
13871
13872 /* Generate a DIE for a lexical block.  */
13873
13874 static void
13875 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
13876 {
13877   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
13878
13879   if (! BLOCK_ABSTRACT (stmt))
13880     add_high_low_attributes (stmt, stmt_die);
13881
13882   decls_for_scope (stmt, stmt_die, depth);
13883 }
13884
13885 /* Generate a DIE for an inlined subprogram.  */
13886
13887 static void
13888 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
13889 {
13890   tree decl = block_ultimate_origin (stmt);
13891
13892   /* Emit info for the abstract instance first, if we haven't yet.  We
13893      must emit this even if the block is abstract, otherwise when we
13894      emit the block below (or elsewhere), we may end up trying to emit
13895      a die whose origin die hasn't been emitted, and crashing.  */
13896   dwarf2out_abstract_function (decl);
13897
13898   if (! BLOCK_ABSTRACT (stmt))
13899     {
13900       dw_die_ref subr_die
13901         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13902
13903       add_abstract_origin_attribute (subr_die, decl);
13904       add_high_low_attributes (stmt, subr_die);
13905       add_call_src_coords_attributes (stmt, subr_die);
13906
13907       decls_for_scope (stmt, subr_die, depth);
13908       current_function_has_inlines = 1;
13909     }
13910   else
13911     /* We may get here if we're the outer block of function A that was
13912        inlined into function B that was inlined into function C.  When
13913        generating debugging info for C, dwarf2out_abstract_function(B)
13914        would mark all inlined blocks as abstract, including this one.
13915        So, we wouldn't (and shouldn't) expect labels to be generated
13916        for this one.  Instead, just emit debugging info for
13917        declarations within the block.  This is particularly important
13918        in the case of initializers of arguments passed from B to us:
13919        if they're statement expressions containing declarations, we
13920        wouldn't generate dies for their abstract variables, and then,
13921        when generating dies for the real variables, we'd die (pun
13922        intended :-)  */
13923     gen_lexical_block_die (stmt, context_die, depth);
13924 }
13925
13926 /* Generate a DIE for a field in a record, or structure.  */
13927
13928 static void
13929 gen_field_die (tree decl, dw_die_ref context_die)
13930 {
13931   dw_die_ref decl_die;
13932
13933   if (TREE_TYPE (decl) == error_mark_node)
13934     return;
13935
13936   decl_die = new_die (DW_TAG_member, context_die, decl);
13937   add_name_and_src_coords_attributes (decl_die, decl);
13938   add_type_attribute (decl_die, member_declared_type (decl),
13939                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13940                       context_die);
13941
13942   if (DECL_BIT_FIELD_TYPE (decl))
13943     {
13944       add_byte_size_attribute (decl_die, decl);
13945       add_bit_size_attribute (decl_die, decl);
13946       add_bit_offset_attribute (decl_die, decl);
13947     }
13948
13949   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13950     add_data_member_location_attribute (decl_die, decl);
13951
13952   if (DECL_ARTIFICIAL (decl))
13953     add_AT_flag (decl_die, DW_AT_artificial, 1);
13954
13955   if (TREE_PROTECTED (decl))
13956     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13957   else if (TREE_PRIVATE (decl))
13958     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13959
13960   /* Equate decl number to die, so that we can look up this decl later on.  */
13961   equate_decl_number_to_die (decl, decl_die);
13962 }
13963
13964 #if 0
13965 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13966    Use modified_type_die instead.
13967    We keep this code here just in case these types of DIEs may be needed to
13968    represent certain things in other languages (e.g. Pascal) someday.  */
13969
13970 static void
13971 gen_pointer_type_die (tree type, dw_die_ref context_die)
13972 {
13973   dw_die_ref ptr_die
13974     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13975
13976   equate_type_number_to_die (type, ptr_die);
13977   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13978   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13979 }
13980
13981 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13982    Use modified_type_die instead.
13983    We keep this code here just in case these types of DIEs may be needed to
13984    represent certain things in other languages (e.g. Pascal) someday.  */
13985
13986 static void
13987 gen_reference_type_die (tree type, dw_die_ref context_die)
13988 {
13989   dw_die_ref ref_die
13990     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13991
13992   equate_type_number_to_die (type, ref_die);
13993   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13994   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13995 }
13996 #endif
13997
13998 /* Generate a DIE for a pointer to a member type.  */
13999
14000 static void
14001 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14002 {
14003   dw_die_ref ptr_die
14004     = new_die (DW_TAG_ptr_to_member_type,
14005                scope_die_for (type, context_die), type);
14006
14007   equate_type_number_to_die (type, ptr_die);
14008   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14009                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14010   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14011 }
14012
14013 /* Generate the DIE for the compilation unit.  */
14014
14015 static dw_die_ref
14016 gen_compile_unit_die (const char *filename)
14017 {
14018   dw_die_ref die;
14019   char producer[250];
14020   const char *language_string = lang_hooks.name;
14021   int language;
14022
14023   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14024
14025   if (filename)
14026     {
14027       add_name_attribute (die, filename);
14028       /* Don't add cwd for <built-in>.  */
14029       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14030         add_comp_dir_attribute (die);
14031     }
14032
14033   sprintf (producer, "%s %s", language_string, version_string);
14034
14035 #ifdef MIPS_DEBUGGING_INFO
14036   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14037      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14038      not appear in the producer string, the debugger reaches the conclusion
14039      that the object file is stripped and has no debugging information.
14040      To get the MIPS/SGI debugger to believe that there is debugging
14041      information in the object file, we add a -g to the producer string.  */
14042   if (debug_info_level > DINFO_LEVEL_TERSE)
14043     strcat (producer, " -g");
14044 #endif
14045
14046   add_AT_string (die, DW_AT_producer, producer);
14047
14048   if (strcmp (language_string, "GNU C++") == 0)
14049     language = DW_LANG_C_plus_plus;
14050   else if (strcmp (language_string, "GNU Ada") == 0)
14051     language = DW_LANG_Ada95;
14052   else if (strcmp (language_string, "GNU F77") == 0)
14053     language = DW_LANG_Fortran77;
14054   else if (strcmp (language_string, "GNU Fortran") == 0)
14055     language = DW_LANG_Fortran95;
14056   else if (strcmp (language_string, "GNU Pascal") == 0)
14057     language = DW_LANG_Pascal83;
14058   else if (strcmp (language_string, "GNU Java") == 0)
14059     language = DW_LANG_Java;
14060   else if (strcmp (language_string, "GNU Objective-C") == 0)
14061     language = DW_LANG_ObjC;
14062   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14063     language = DW_LANG_ObjC_plus_plus;
14064   else
14065     language = DW_LANG_C89;
14066
14067   add_AT_unsigned (die, DW_AT_language, language);
14068   return die;
14069 }
14070
14071 /* Generate the DIE for a base class.  */
14072
14073 static void
14074 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14075 {
14076   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14077
14078   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14079   add_data_member_location_attribute (die, binfo);
14080
14081   if (BINFO_VIRTUAL_P (binfo))
14082     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14083
14084   if (access == access_public_node)
14085     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14086   else if (access == access_protected_node)
14087     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14088 }
14089
14090 /* Generate a DIE for a class member.  */
14091
14092 static void
14093 gen_member_die (tree type, dw_die_ref context_die)
14094 {
14095   tree member;
14096   tree binfo = TYPE_BINFO (type);
14097   dw_die_ref child;
14098
14099   /* If this is not an incomplete type, output descriptions of each of its
14100      members. Note that as we output the DIEs necessary to represent the
14101      members of this record or union type, we will also be trying to output
14102      DIEs to represent the *types* of those members. However the `type'
14103      function (above) will specifically avoid generating type DIEs for member
14104      types *within* the list of member DIEs for this (containing) type except
14105      for those types (of members) which are explicitly marked as also being
14106      members of this (containing) type themselves.  The g++ front- end can
14107      force any given type to be treated as a member of some other (containing)
14108      type by setting the TYPE_CONTEXT of the given (member) type to point to
14109      the TREE node representing the appropriate (containing) type.  */
14110
14111   /* First output info about the base classes.  */
14112   if (binfo)
14113     {
14114       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14115       int i;
14116       tree base;
14117
14118       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14119         gen_inheritance_die (base,
14120                              (accesses ? VEC_index (tree, accesses, i)
14121                               : access_public_node), context_die);
14122     }
14123
14124   /* Now output info about the data members and type members.  */
14125   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14126     {
14127       /* If we thought we were generating minimal debug info for TYPE
14128          and then changed our minds, some of the member declarations
14129          may have already been defined.  Don't define them again, but
14130          do put them in the right order.  */
14131
14132       child = lookup_decl_die (member);
14133       if (child)
14134         splice_child_die (context_die, child);
14135       else
14136         gen_decl_die (member, context_die);
14137     }
14138
14139   /* Now output info about the function members (if any).  */
14140   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14141     {
14142       /* Don't include clones in the member list.  */
14143       if (DECL_ABSTRACT_ORIGIN (member))
14144         continue;
14145
14146       child = lookup_decl_die (member);
14147       if (child)
14148         splice_child_die (context_die, child);
14149       else
14150         gen_decl_die (member, context_die);
14151     }
14152 }
14153
14154 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14155    is set, we pretend that the type was never defined, so we only get the
14156    member DIEs needed by later specification DIEs.  */
14157
14158 static void
14159 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14160                                 enum debug_info_usage usage)
14161 {
14162   dw_die_ref type_die = lookup_type_die (type);
14163   dw_die_ref scope_die = 0;
14164   int nested = 0;
14165   int complete = (TYPE_SIZE (type)
14166                   && (! TYPE_STUB_DECL (type)
14167                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14168   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14169   complete = complete && should_emit_struct_debug (type, usage);
14170
14171   if (type_die && ! complete)
14172     return;
14173
14174   if (TYPE_CONTEXT (type) != NULL_TREE
14175       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14176           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14177     nested = 1;
14178
14179   scope_die = scope_die_for (type, context_die);
14180
14181   if (! type_die || (nested && scope_die == comp_unit_die))
14182     /* First occurrence of type or toplevel definition of nested class.  */
14183     {
14184       dw_die_ref old_die = type_die;
14185
14186       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14187                           ? record_type_tag (type) : DW_TAG_union_type,
14188                           scope_die, type);
14189       equate_type_number_to_die (type, type_die);
14190       if (old_die)
14191         add_AT_specification (type_die, old_die);
14192       else
14193         add_name_attribute (type_die, type_tag (type));
14194     }
14195   else
14196     remove_AT (type_die, DW_AT_declaration);
14197
14198   /* If this type has been completed, then give it a byte_size attribute and
14199      then give a list of members.  */
14200   if (complete && !ns_decl)
14201     {
14202       /* Prevent infinite recursion in cases where the type of some member of
14203          this type is expressed in terms of this type itself.  */
14204       TREE_ASM_WRITTEN (type) = 1;
14205       add_byte_size_attribute (type_die, type);
14206       if (TYPE_STUB_DECL (type) != NULL_TREE)
14207         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14208
14209       /* If the first reference to this type was as the return type of an
14210          inline function, then it may not have a parent.  Fix this now.  */
14211       if (type_die->die_parent == NULL)
14212         add_child_die (scope_die, type_die);
14213
14214       push_decl_scope (type);
14215       gen_member_die (type, type_die);
14216       pop_decl_scope ();
14217
14218       /* GNU extension: Record what type our vtable lives in.  */
14219       if (TYPE_VFIELD (type))
14220         {
14221           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14222
14223           gen_type_die (vtype, context_die);
14224           add_AT_die_ref (type_die, DW_AT_containing_type,
14225                           lookup_type_die (vtype));
14226         }
14227     }
14228   else
14229     {
14230       add_AT_flag (type_die, DW_AT_declaration, 1);
14231
14232       /* We don't need to do this for function-local types.  */
14233       if (TYPE_STUB_DECL (type)
14234           && ! decl_function_context (TYPE_STUB_DECL (type)))
14235         VEC_safe_push (tree, gc, incomplete_types, type);
14236     }
14237
14238   if (get_AT (type_die, DW_AT_name))
14239     add_pubtype (type, type_die);
14240 }
14241
14242 /* Generate a DIE for a subroutine _type_.  */
14243
14244 static void
14245 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14246 {
14247   tree return_type = TREE_TYPE (type);
14248   dw_die_ref subr_die
14249     = new_die (DW_TAG_subroutine_type,
14250                scope_die_for (type, context_die), type);
14251
14252   equate_type_number_to_die (type, subr_die);
14253   add_prototyped_attribute (subr_die, type);
14254   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14255   gen_formal_types_die (type, subr_die);
14256
14257   if (get_AT (subr_die, DW_AT_name))
14258     add_pubtype (type, subr_die);
14259 }
14260
14261 /* Generate a DIE for a type definition.  */
14262
14263 static void
14264 gen_typedef_die (tree decl, dw_die_ref context_die)
14265 {
14266   dw_die_ref type_die;
14267   tree origin;
14268
14269   if (TREE_ASM_WRITTEN (decl))
14270     return;
14271
14272   TREE_ASM_WRITTEN (decl) = 1;
14273   type_die = new_die (DW_TAG_typedef, context_die, decl);
14274   origin = decl_ultimate_origin (decl);
14275   if (origin != NULL)
14276     add_abstract_origin_attribute (type_die, origin);
14277   else
14278     {
14279       tree type;
14280
14281       add_name_and_src_coords_attributes (type_die, decl);
14282       if (DECL_ORIGINAL_TYPE (decl))
14283         {
14284           type = DECL_ORIGINAL_TYPE (decl);
14285
14286           gcc_assert (type != TREE_TYPE (decl));
14287           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14288         }
14289       else
14290         type = TREE_TYPE (decl);
14291
14292       add_type_attribute (type_die, type, TREE_READONLY (decl),
14293                           TREE_THIS_VOLATILE (decl), context_die);
14294     }
14295
14296   if (DECL_ABSTRACT (decl))
14297     equate_decl_number_to_die (decl, type_die);
14298
14299   if (get_AT (type_die, DW_AT_name))
14300     add_pubtype (decl, type_die);
14301 }
14302
14303 /* Generate a type description DIE.  */
14304
14305 static void
14306 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14307                                 enum debug_info_usage usage)
14308 {
14309   int need_pop;
14310   struct array_descr_info info;
14311
14312   if (type == NULL_TREE || type == error_mark_node)
14313     return;
14314
14315   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14316       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14317     {
14318       if (TREE_ASM_WRITTEN (type))
14319         return;
14320
14321       /* Prevent broken recursion; we can't hand off to the same type.  */
14322       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14323
14324       TREE_ASM_WRITTEN (type) = 1;
14325       gen_decl_die (TYPE_NAME (type), context_die);
14326       return;
14327     }
14328
14329   /* If this is an array type with hidden descriptor, handle it first.  */
14330   if (!TREE_ASM_WRITTEN (type)
14331       && lang_hooks.types.get_array_descr_info
14332       && lang_hooks.types.get_array_descr_info (type, &info))
14333     {
14334       gen_descr_array_type_die (type, &info, context_die);
14335       TREE_ASM_WRITTEN (type) = 1;
14336       return;
14337     }
14338
14339   /* We are going to output a DIE to represent the unqualified version
14340      of this type (i.e. without any const or volatile qualifiers) so
14341      get the main variant (i.e. the unqualified version) of this type
14342      now.  (Vectors are special because the debugging info is in the
14343      cloned type itself).  */
14344   if (TREE_CODE (type) != VECTOR_TYPE)
14345     type = type_main_variant (type);
14346
14347   if (TREE_ASM_WRITTEN (type))
14348     return;
14349
14350   switch (TREE_CODE (type))
14351     {
14352     case ERROR_MARK:
14353       break;
14354
14355     case POINTER_TYPE:
14356     case REFERENCE_TYPE:
14357       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14358          ensures that the gen_type_die recursion will terminate even if the
14359          type is recursive.  Recursive types are possible in Ada.  */
14360       /* ??? We could perhaps do this for all types before the switch
14361          statement.  */
14362       TREE_ASM_WRITTEN (type) = 1;
14363
14364       /* For these types, all that is required is that we output a DIE (or a
14365          set of DIEs) to represent the "basis" type.  */
14366       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14367                                 DINFO_USAGE_IND_USE);
14368       break;
14369
14370     case OFFSET_TYPE:
14371       /* This code is used for C++ pointer-to-data-member types.
14372          Output a description of the relevant class type.  */
14373       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14374                                         DINFO_USAGE_IND_USE);
14375
14376       /* Output a description of the type of the object pointed to.  */
14377       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14378                                         DINFO_USAGE_IND_USE);
14379
14380       /* Now output a DIE to represent this pointer-to-data-member type
14381          itself.  */
14382       gen_ptr_to_mbr_type_die (type, context_die);
14383       break;
14384
14385     case FUNCTION_TYPE:
14386       /* Force out return type (in case it wasn't forced out already).  */
14387       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14388                                         DINFO_USAGE_DIR_USE);
14389       gen_subroutine_type_die (type, context_die);
14390       break;
14391
14392     case METHOD_TYPE:
14393       /* Force out return type (in case it wasn't forced out already).  */
14394       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14395                                         DINFO_USAGE_DIR_USE);
14396       gen_subroutine_type_die (type, context_die);
14397       break;
14398
14399     case ARRAY_TYPE:
14400       gen_array_type_die (type, context_die);
14401       break;
14402
14403     case VECTOR_TYPE:
14404       gen_array_type_die (type, context_die);
14405       break;
14406
14407     case ENUMERAL_TYPE:
14408     case RECORD_TYPE:
14409     case UNION_TYPE:
14410     case QUAL_UNION_TYPE:
14411       /* If this is a nested type whose containing class hasn't been written
14412          out yet, writing it out will cover this one, too.  This does not apply
14413          to instantiations of member class templates; they need to be added to
14414          the containing class as they are generated.  FIXME: This hurts the
14415          idea of combining type decls from multiple TUs, since we can't predict
14416          what set of template instantiations we'll get.  */
14417       if (TYPE_CONTEXT (type)
14418           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14419           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14420         {
14421           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14422
14423           if (TREE_ASM_WRITTEN (type))
14424             return;
14425
14426           /* If that failed, attach ourselves to the stub.  */
14427           push_decl_scope (TYPE_CONTEXT (type));
14428           context_die = lookup_type_die (TYPE_CONTEXT (type));
14429           need_pop = 1;
14430         }
14431       else
14432         {
14433           context_die = declare_in_namespace (type, context_die);
14434           need_pop = 0;
14435         }
14436
14437       if (TREE_CODE (type) == ENUMERAL_TYPE)
14438         {
14439           /* This might have been written out by the call to
14440              declare_in_namespace.  */
14441           if (!TREE_ASM_WRITTEN (type))
14442             gen_enumeration_type_die (type, context_die);
14443         }
14444       else
14445         gen_struct_or_union_type_die (type, context_die, usage);
14446
14447       if (need_pop)
14448         pop_decl_scope ();
14449
14450       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14451          it up if it is ever completed.  gen_*_type_die will set it for us
14452          when appropriate.  */
14453       return;
14454
14455     case VOID_TYPE:
14456     case INTEGER_TYPE:
14457     case REAL_TYPE:
14458     case FIXED_POINT_TYPE:
14459     case COMPLEX_TYPE:
14460     case BOOLEAN_TYPE:
14461       /* No DIEs needed for fundamental types.  */
14462       break;
14463
14464     case LANG_TYPE:
14465       /* No Dwarf representation currently defined.  */
14466       break;
14467
14468     default:
14469       gcc_unreachable ();
14470     }
14471
14472   TREE_ASM_WRITTEN (type) = 1;
14473 }
14474
14475 static void
14476 gen_type_die (tree type, dw_die_ref context_die)
14477 {
14478   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14479 }
14480
14481 /* Generate a DIE for a tagged type instantiation.  */
14482
14483 static void
14484 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
14485 {
14486   if (type == NULL_TREE || type == error_mark_node)
14487     return;
14488
14489   /* We are going to output a DIE to represent the unqualified version of
14490      this type (i.e. without any const or volatile qualifiers) so make sure
14491      that we have the main variant (i.e. the unqualified version) of this
14492      type now.  */
14493   gcc_assert (type == type_main_variant (type));
14494
14495   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
14496      an instance of an unresolved type.  */
14497
14498   switch (TREE_CODE (type))
14499     {
14500     case ERROR_MARK:
14501       break;
14502
14503     case ENUMERAL_TYPE:
14504       gen_inlined_enumeration_type_die (type, context_die);
14505       break;
14506
14507     case RECORD_TYPE:
14508       gen_inlined_structure_type_die (type, context_die);
14509       break;
14510
14511     case UNION_TYPE:
14512     case QUAL_UNION_TYPE:
14513       gen_inlined_union_type_die (type, context_die);
14514       break;
14515
14516     default:
14517       gcc_unreachable ();
14518     }
14519 }
14520
14521 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14522    things which are local to the given block.  */
14523
14524 static void
14525 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14526 {
14527   int must_output_die = 0;
14528   tree origin;
14529   tree decl;
14530   enum tree_code origin_code;
14531
14532   /* Ignore blocks that are NULL.  */
14533   if (stmt == NULL_TREE)
14534     return;
14535
14536   /* If the block is one fragment of a non-contiguous block, do not
14537      process the variables, since they will have been done by the
14538      origin block.  Do process subblocks.  */
14539   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14540     {
14541       tree sub;
14542
14543       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14544         gen_block_die (sub, context_die, depth + 1);
14545
14546       return;
14547     }
14548
14549   /* Determine the "ultimate origin" of this block.  This block may be an
14550      inlined instance of an inlined instance of inline function, so we have
14551      to trace all of the way back through the origin chain to find out what
14552      sort of node actually served as the original seed for the creation of
14553      the current block.  */
14554   origin = block_ultimate_origin (stmt);
14555   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
14556
14557   /* Determine if we need to output any Dwarf DIEs at all to represent this
14558      block.  */
14559   if (origin_code == FUNCTION_DECL)
14560     /* The outer scopes for inlinings *must* always be represented.  We
14561        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14562     must_output_die = 1;
14563   else
14564     {
14565       /* In the case where the current block represents an inlining of the
14566          "body block" of an inline function, we must *NOT* output any DIE for
14567          this block because we have already output a DIE to represent the whole
14568          inlined function scope and the "body block" of any function doesn't
14569          really represent a different scope according to ANSI C rules.  So we
14570          check here to make sure that this block does not represent a "body
14571          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
14572       if (! is_body_block (origin ? origin : stmt))
14573         {
14574           /* Determine if this block directly contains any "significant"
14575              local declarations which we will need to output DIEs for.  */
14576           if (debug_info_level > DINFO_LEVEL_TERSE)
14577             /* We are not in terse mode so *any* local declaration counts
14578                as being a "significant" one.  */
14579             must_output_die = (BLOCK_VARS (stmt) != NULL
14580                                && (TREE_USED (stmt)
14581                                    || TREE_ASM_WRITTEN (stmt)
14582                                    || BLOCK_ABSTRACT (stmt)));
14583           else
14584             /* We are in terse mode, so only local (nested) function
14585                definitions count as "significant" local declarations.  */
14586             for (decl = BLOCK_VARS (stmt);
14587                  decl != NULL; decl = TREE_CHAIN (decl))
14588               if (TREE_CODE (decl) == FUNCTION_DECL
14589                   && DECL_INITIAL (decl))
14590                 {
14591                   must_output_die = 1;
14592                   break;
14593                 }
14594         }
14595     }
14596
14597   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14598      DIE for any block which contains no significant local declarations at
14599      all.  Rather, in such cases we just call `decls_for_scope' so that any
14600      needed Dwarf info for any sub-blocks will get properly generated. Note
14601      that in terse mode, our definition of what constitutes a "significant"
14602      local declaration gets restricted to include only inlined function
14603      instances and local (nested) function definitions.  */
14604   if (must_output_die)
14605     {
14606       if (origin_code == FUNCTION_DECL)
14607         gen_inlined_subroutine_die (stmt, context_die, depth);
14608       else
14609         gen_lexical_block_die (stmt, context_die, depth);
14610     }
14611   else
14612     decls_for_scope (stmt, context_die, depth);
14613 }
14614
14615 /* Generate all of the decls declared within a given scope and (recursively)
14616    all of its sub-blocks.  */
14617
14618 static void
14619 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14620 {
14621   tree decl;
14622   tree subblocks;
14623
14624   /* Ignore NULL blocks.  */
14625   if (stmt == NULL_TREE)
14626     return;
14627
14628   if (TREE_USED (stmt))
14629     {
14630       /* Output the DIEs to represent all of the data objects and typedefs
14631          declared directly within this block but not within any nested
14632          sub-blocks.  Also, nested function and tag DIEs have been
14633          generated with a parent of NULL; fix that up now.  */
14634       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14635         {
14636           dw_die_ref die;
14637
14638           if (TREE_CODE (decl) == FUNCTION_DECL)
14639             die = lookup_decl_die (decl);
14640           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
14641             die = lookup_type_die (TREE_TYPE (decl));
14642           else
14643             die = NULL;
14644
14645           if (die != NULL && die->die_parent == NULL)
14646             add_child_die (context_die, die);
14647           /* Do not produce debug information for static variables since
14648              these might be optimized out.  We are called for these later
14649              in varpool_analyze_pending_decls.
14650
14651              But *do* produce it for Fortran COMMON variables because,
14652              even though they are static, their names can differ depending
14653              on the scope, which we need to preserve.  */
14654           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
14655               && !(is_fortran () && TREE_PUBLIC (decl)))
14656             ;
14657           else
14658             gen_decl_die (decl, context_die);
14659         }
14660     }
14661
14662   /* If we're at -g1, we're not interested in subblocks.  */
14663   if (debug_info_level <= DINFO_LEVEL_TERSE)
14664     return;
14665
14666   /* Output the DIEs to represent all sub-blocks (and the items declared
14667      therein) of this block.  */
14668   for (subblocks = BLOCK_SUBBLOCKS (stmt);
14669        subblocks != NULL;
14670        subblocks = BLOCK_CHAIN (subblocks))
14671     gen_block_die (subblocks, context_die, depth + 1);
14672 }
14673
14674 /* Is this a typedef we can avoid emitting?  */
14675
14676 static inline int
14677 is_redundant_typedef (const_tree decl)
14678 {
14679   if (TYPE_DECL_IS_STUB (decl))
14680     return 1;
14681
14682   if (DECL_ARTIFICIAL (decl)
14683       && DECL_CONTEXT (decl)
14684       && is_tagged_type (DECL_CONTEXT (decl))
14685       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
14686       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
14687     /* Also ignore the artificial member typedef for the class name.  */
14688     return 1;
14689
14690   return 0;
14691 }
14692
14693 /* Returns the DIE for a context.  */
14694
14695 static inline dw_die_ref
14696 get_context_die (tree context)
14697 {
14698   if (context)
14699     {
14700       /* Find die that represents this context.  */
14701       if (TYPE_P (context))
14702         return force_type_die (context);
14703       else
14704         return force_decl_die (context);
14705     }
14706   return comp_unit_die;
14707 }
14708
14709 /* Returns the DIE for decl.  A DIE will always be returned.  */
14710
14711 static dw_die_ref
14712 force_decl_die (tree decl)
14713 {
14714   dw_die_ref decl_die;
14715   unsigned saved_external_flag;
14716   tree save_fn = NULL_TREE;
14717   decl_die = lookup_decl_die (decl);
14718   if (!decl_die)
14719     {
14720       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
14721
14722       decl_die = lookup_decl_die (decl);
14723       if (decl_die)
14724         return decl_die;
14725
14726       switch (TREE_CODE (decl))
14727         {
14728         case FUNCTION_DECL:
14729           /* Clear current_function_decl, so that gen_subprogram_die thinks
14730              that this is a declaration. At this point, we just want to force
14731              declaration die.  */
14732           save_fn = current_function_decl;
14733           current_function_decl = NULL_TREE;
14734           gen_subprogram_die (decl, context_die);
14735           current_function_decl = save_fn;
14736           break;
14737
14738         case VAR_DECL:
14739           /* Set external flag to force declaration die. Restore it after
14740            gen_decl_die() call.  */
14741           saved_external_flag = DECL_EXTERNAL (decl);
14742           DECL_EXTERNAL (decl) = 1;
14743           gen_decl_die (decl, context_die);
14744           DECL_EXTERNAL (decl) = saved_external_flag;
14745           break;
14746
14747         case NAMESPACE_DECL:
14748           dwarf2out_decl (decl);
14749           break;
14750
14751         default:
14752           gcc_unreachable ();
14753         }
14754
14755       /* We should be able to find the DIE now.  */
14756       if (!decl_die)
14757         decl_die = lookup_decl_die (decl);
14758       gcc_assert (decl_die);
14759     }
14760
14761   return decl_die;
14762 }
14763
14764 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
14765    always returned.  */
14766
14767 static dw_die_ref
14768 force_type_die (tree type)
14769 {
14770   dw_die_ref type_die;
14771
14772   type_die = lookup_type_die (type);
14773   if (!type_die)
14774     {
14775       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
14776
14777       type_die = modified_type_die (type, TYPE_READONLY (type),
14778                                     TYPE_VOLATILE (type), context_die);
14779       gcc_assert (type_die);
14780     }
14781   return type_die;
14782 }
14783
14784 /* Force out any required namespaces to be able to output DECL,
14785    and return the new context_die for it, if it's changed.  */
14786
14787 static dw_die_ref
14788 setup_namespace_context (tree thing, dw_die_ref context_die)
14789 {
14790   tree context = (DECL_P (thing)
14791                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
14792   if (context && TREE_CODE (context) == NAMESPACE_DECL)
14793     /* Force out the namespace.  */
14794     context_die = force_decl_die (context);
14795
14796   return context_die;
14797 }
14798
14799 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
14800    type) within its namespace, if appropriate.
14801
14802    For compatibility with older debuggers, namespace DIEs only contain
14803    declarations; all definitions are emitted at CU scope.  */
14804
14805 static dw_die_ref
14806 declare_in_namespace (tree thing, dw_die_ref context_die)
14807 {
14808   dw_die_ref ns_context;
14809
14810   if (debug_info_level <= DINFO_LEVEL_TERSE)
14811     return context_die;
14812
14813   /* If this decl is from an inlined function, then don't try to emit it in its
14814      namespace, as we will get confused.  It would have already been emitted
14815      when the abstract instance of the inline function was emitted anyways.  */
14816   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
14817     return context_die;
14818
14819   ns_context = setup_namespace_context (thing, context_die);
14820
14821   if (ns_context != context_die)
14822     {
14823       if (is_fortran ())
14824         return ns_context;
14825       if (DECL_P (thing))
14826         gen_decl_die (thing, ns_context);
14827       else
14828         gen_type_die (thing, ns_context);
14829     }
14830   return context_die;
14831 }
14832
14833 /* Generate a DIE for a namespace or namespace alias.  */
14834
14835 static void
14836 gen_namespace_die (tree decl)
14837 {
14838   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
14839
14840   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
14841      they are an alias of.  */
14842   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
14843     {
14844       /* Output a real namespace.  */
14845       dw_die_ref namespace_die
14846         = new_die (is_fortran () ? DW_TAG_module : DW_TAG_namespace,
14847                    context_die, decl);
14848       add_name_and_src_coords_attributes (namespace_die, decl);
14849       if (DECL_EXTERNAL (decl))
14850         add_AT_flag (namespace_die, DW_AT_declaration, 1);
14851       equate_decl_number_to_die (decl, namespace_die);
14852     }
14853   else
14854     {
14855       /* Output a namespace alias.  */
14856
14857       /* Force out the namespace we are an alias of, if necessary.  */
14858       dw_die_ref origin_die
14859         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
14860
14861       /* Now create the namespace alias DIE.  */
14862       dw_die_ref namespace_die
14863         = new_die (DW_TAG_imported_declaration, context_die, decl);
14864       add_name_and_src_coords_attributes (namespace_die, decl);
14865       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
14866       equate_decl_number_to_die (decl, namespace_die);
14867     }
14868 }
14869
14870 /* Generate Dwarf debug information for a decl described by DECL.  */
14871
14872 static void
14873 gen_decl_die (tree decl, dw_die_ref context_die)
14874 {
14875   tree origin;
14876
14877   if (DECL_P (decl) && DECL_IGNORED_P (decl))
14878     return;
14879
14880   switch (TREE_CODE (decl))
14881     {
14882     case ERROR_MARK:
14883       break;
14884
14885     case CONST_DECL:
14886       /* The individual enumerators of an enum type get output when we output
14887          the Dwarf representation of the relevant enum type itself.  */
14888       break;
14889
14890     case FUNCTION_DECL:
14891       /* Don't output any DIEs to represent mere function declarations,
14892          unless they are class members or explicit block externs.  */
14893       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
14894           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
14895         break;
14896
14897 #if 0
14898       /* FIXME */
14899       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
14900          on local redeclarations of global functions.  That seems broken.  */
14901       if (current_function_decl != decl)
14902         /* This is only a declaration.  */;
14903 #endif
14904
14905       /* If we're emitting a clone, emit info for the abstract instance.  */
14906       if (DECL_ORIGIN (decl) != decl)
14907         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14908
14909       /* If we're emitting an out-of-line copy of an inline function,
14910          emit info for the abstract instance and set up to refer to it.  */
14911       else if (cgraph_function_possibly_inlined_p (decl)
14912                && ! DECL_ABSTRACT (decl)
14913                && ! class_or_namespace_scope_p (context_die)
14914                /* dwarf2out_abstract_function won't emit a die if this is just
14915                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
14916                   that case, because that works only if we have a die.  */
14917                && DECL_INITIAL (decl) != NULL_TREE)
14918         {
14919           dwarf2out_abstract_function (decl);
14920           set_decl_origin_self (decl);
14921         }
14922
14923       /* Otherwise we're emitting the primary DIE for this decl.  */
14924       else if (debug_info_level > DINFO_LEVEL_TERSE)
14925         {
14926           /* Before we describe the FUNCTION_DECL itself, make sure that we
14927              have described its return type.  */
14928           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14929
14930           /* And its virtual context.  */
14931           if (DECL_VINDEX (decl) != NULL_TREE)
14932             gen_type_die (DECL_CONTEXT (decl), context_die);
14933
14934           /* And its containing type.  */
14935           origin = decl_class_context (decl);
14936           if (origin != NULL_TREE)
14937             gen_type_die_for_member (origin, decl, context_die);
14938
14939           /* And its containing namespace.  */
14940           context_die = declare_in_namespace (decl, context_die);
14941         }
14942
14943       /* Now output a DIE to represent the function itself.  */
14944       gen_subprogram_die (decl, context_die);
14945       break;
14946
14947     case TYPE_DECL:
14948       /* If we are in terse mode, don't generate any DIEs to represent any
14949          actual typedefs.  */
14950       if (debug_info_level <= DINFO_LEVEL_TERSE)
14951         break;
14952
14953       /* In the special case of a TYPE_DECL node representing the declaration
14954          of some type tag, if the given TYPE_DECL is marked as having been
14955          instantiated from some other (original) TYPE_DECL node (e.g. one which
14956          was generated within the original definition of an inline function) we
14957          have to generate a special (abbreviated) DW_TAG_structure_type,
14958          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
14959       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14960           && is_tagged_type (TREE_TYPE (decl)))
14961         {
14962           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14963           break;
14964         }
14965
14966       if (is_redundant_typedef (decl))
14967         gen_type_die (TREE_TYPE (decl), context_die);
14968       else
14969         /* Output a DIE to represent the typedef itself.  */
14970         gen_typedef_die (decl, context_die);
14971       break;
14972
14973     case LABEL_DECL:
14974       if (debug_info_level >= DINFO_LEVEL_NORMAL)
14975         gen_label_die (decl, context_die);
14976       break;
14977
14978     case VAR_DECL:
14979     case RESULT_DECL:
14980       /* If we are in terse mode, don't generate any DIEs to represent any
14981          variable declarations or definitions.  */
14982       if (debug_info_level <= DINFO_LEVEL_TERSE)
14983         break;
14984
14985       /* Output any DIEs that are needed to specify the type of this data
14986          object.  */
14987       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14988         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14989       else
14990         gen_type_die (TREE_TYPE (decl), context_die);
14991
14992       /* And its containing type.  */
14993       origin = decl_class_context (decl);
14994       if (origin != NULL_TREE)
14995         gen_type_die_for_member (origin, decl, context_die);
14996
14997       /* And its containing namespace.  */
14998       context_die = declare_in_namespace (decl, context_die);
14999
15000       /* Now output the DIE to represent the data object itself.  This gets
15001          complicated because of the possibility that the VAR_DECL really
15002          represents an inlined instance of a formal parameter for an inline
15003          function.  */
15004       origin = decl_ultimate_origin (decl);
15005       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15006         gen_formal_parameter_die (decl, context_die);
15007       else
15008         gen_variable_die (decl, context_die);
15009       break;
15010
15011     case FIELD_DECL:
15012       /* Ignore the nameless fields that are used to skip bits but handle C++
15013          anonymous unions and structs.  */
15014       if (DECL_NAME (decl) != NULL_TREE
15015           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15016           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15017         {
15018           gen_type_die (member_declared_type (decl), context_die);
15019           gen_field_die (decl, context_die);
15020         }
15021       break;
15022
15023     case PARM_DECL:
15024       if (DECL_BY_REFERENCE (decl))
15025         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15026       else
15027         gen_type_die (TREE_TYPE (decl), context_die);
15028       gen_formal_parameter_die (decl, context_die);
15029       break;
15030
15031     case NAMESPACE_DECL:
15032       gen_namespace_die (decl);
15033       break;
15034
15035     default:
15036       /* Probably some frontend-internal decl.  Assume we don't care.  */
15037       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15038       break;
15039     }
15040 }
15041 \f
15042 /* Output debug information for global decl DECL.  Called from toplev.c after
15043    compilation proper has finished.  */
15044
15045 static void
15046 dwarf2out_global_decl (tree decl)
15047 {
15048   /* Output DWARF2 information for file-scope tentative data object
15049      declarations, file-scope (extern) function declarations (which
15050      had no corresponding body) and file-scope tagged type declarations
15051      and definitions which have not yet been forced out.  */
15052   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15053     dwarf2out_decl (decl);
15054 }
15055
15056 /* Output debug information for type decl DECL.  Called from toplev.c
15057    and from language front ends (to record built-in types).  */
15058 static void
15059 dwarf2out_type_decl (tree decl, int local)
15060 {
15061   if (!local)
15062     dwarf2out_decl (decl);
15063 }
15064
15065 /* Output debug information for imported module or decl DECL.
15066    NAME is non-NULL name in context if the decl has been renamed.
15067    CHILD is true if decl is one of the renamed decls as part of
15068    importing whole module.  */
15069
15070 static void
15071 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15072                                    bool child)
15073 {
15074   dw_die_ref imported_die, at_import_die;
15075   dw_die_ref scope_die;
15076   expanded_location xloc;
15077
15078   if (debug_info_level <= DINFO_LEVEL_TERSE)
15079     return;
15080
15081   gcc_assert (decl);
15082
15083   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15084      We need decl DIE for reference and scope die. First, get DIE for the decl
15085      itself.  */
15086
15087   /* Get the scope die for decl context. Use comp_unit_die for global module
15088      or decl. If die is not found for non globals, force new die.  */
15089   if (context
15090       && TYPE_P (context)
15091       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15092     return;
15093   scope_die = get_context_die (context);
15094
15095   if (child)
15096     {
15097       gcc_assert (scope_die->die_child);
15098       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15099       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15100       scope_die = scope_die->die_child;
15101     }
15102
15103   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
15104   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15105     {
15106       if (is_base_type (TREE_TYPE (decl)))
15107         at_import_die = base_type_die (TREE_TYPE (decl));
15108       else
15109         at_import_die = force_type_die (TREE_TYPE (decl));
15110       /* For namespace N { typedef void T; } using N::T; base_type_die
15111          returns NULL, but DW_TAG_imported_declaration requires
15112          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15113       if (!at_import_die)
15114         {
15115           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15116           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15117           at_import_die = lookup_type_die (TREE_TYPE (decl));
15118           gcc_assert (at_import_die);
15119         }
15120     }
15121   else
15122     {
15123       at_import_die = lookup_decl_die (decl);
15124       if (!at_import_die)
15125         {
15126           /* If we're trying to avoid duplicate debug info, we may not have
15127              emitted the member decl for this field.  Emit it now.  */
15128           if (TREE_CODE (decl) == FIELD_DECL)
15129             {
15130               tree type = DECL_CONTEXT (decl);
15131
15132               if (TYPE_CONTEXT (type)
15133                   && TYPE_P (TYPE_CONTEXT (type))
15134                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15135                                                 DINFO_USAGE_DIR_USE))
15136                 return;
15137               gen_type_die_for_member (type, decl,
15138                                        get_context_die (TYPE_CONTEXT (type)));
15139             }
15140           at_import_die = force_decl_die (decl);
15141         }
15142     }
15143
15144   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15145   if (TREE_CODE (decl) == NAMESPACE_DECL)
15146     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
15147   else
15148     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
15149
15150   xloc = expand_location (input_location);
15151   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15152   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15153   if (name)
15154     add_AT_string (imported_die, DW_AT_name, IDENTIFIER_POINTER (name));
15155   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15156 }
15157
15158 /* Write the debugging output for DECL.  */
15159
15160 void
15161 dwarf2out_decl (tree decl)
15162 {
15163   dw_die_ref context_die = comp_unit_die;
15164
15165   switch (TREE_CODE (decl))
15166     {
15167     case ERROR_MARK:
15168       return;
15169
15170     case FUNCTION_DECL:
15171       /* What we would really like to do here is to filter out all mere
15172          file-scope declarations of file-scope functions which are never
15173          referenced later within this translation unit (and keep all of ones
15174          that *are* referenced later on) but we aren't clairvoyant, so we have
15175          no idea which functions will be referenced in the future (i.e. later
15176          on within the current translation unit). So here we just ignore all
15177          file-scope function declarations which are not also definitions.  If
15178          and when the debugger needs to know something about these functions,
15179          it will have to hunt around and find the DWARF information associated
15180          with the definition of the function.
15181
15182          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15183          nodes represent definitions and which ones represent mere
15184          declarations.  We have to check DECL_INITIAL instead. That's because
15185          the C front-end supports some weird semantics for "extern inline"
15186          function definitions.  These can get inlined within the current
15187          translation unit (and thus, we need to generate Dwarf info for their
15188          abstract instances so that the Dwarf info for the concrete inlined
15189          instances can have something to refer to) but the compiler never
15190          generates any out-of-lines instances of such things (despite the fact
15191          that they *are* definitions).
15192
15193          The important point is that the C front-end marks these "extern
15194          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15195          them anyway. Note that the C++ front-end also plays some similar games
15196          for inline function definitions appearing within include files which
15197          also contain `#pragma interface' pragmas.  */
15198       if (DECL_INITIAL (decl) == NULL_TREE)
15199         return;
15200
15201       /* If we're a nested function, initially use a parent of NULL; if we're
15202          a plain function, this will be fixed up in decls_for_scope.  If
15203          we're a method, it will be ignored, since we already have a DIE.  */
15204       if (decl_function_context (decl)
15205           /* But if we're in terse mode, we don't care about scope.  */
15206           && debug_info_level > DINFO_LEVEL_TERSE)
15207         context_die = NULL;
15208       break;
15209
15210     case VAR_DECL:
15211       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15212          declaration and if the declaration was never even referenced from
15213          within this entire compilation unit.  We suppress these DIEs in
15214          order to save space in the .debug section (by eliminating entries
15215          which are probably useless).  Note that we must not suppress
15216          block-local extern declarations (whether used or not) because that
15217          would screw-up the debugger's name lookup mechanism and cause it to
15218          miss things which really ought to be in scope at a given point.  */
15219       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15220         return;
15221
15222       /* For local statics lookup proper context die.  */
15223       if (TREE_STATIC (decl) && decl_function_context (decl))
15224         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15225
15226       /* If we are in terse mode, don't generate any DIEs to represent any
15227          variable declarations or definitions.  */
15228       if (debug_info_level <= DINFO_LEVEL_TERSE)
15229         return;
15230       break;
15231
15232     case NAMESPACE_DECL:
15233       if (debug_info_level <= DINFO_LEVEL_TERSE)
15234         return;
15235       if (lookup_decl_die (decl) != NULL)
15236         return;
15237       break;
15238
15239     case TYPE_DECL:
15240       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15241       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15242         return;
15243
15244       /* Don't bother trying to generate any DIEs to represent any of the
15245          normal built-in types for the language we are compiling.  */
15246       if (DECL_IS_BUILTIN (decl))
15247         {
15248           /* OK, we need to generate one for `bool' so GDB knows what type
15249              comparisons have.  */
15250           if (is_cxx ()
15251               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15252               && ! DECL_IGNORED_P (decl))
15253             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15254
15255           return;
15256         }
15257
15258       /* If we are in terse mode, don't generate any DIEs for types.  */
15259       if (debug_info_level <= DINFO_LEVEL_TERSE)
15260         return;
15261
15262       /* If we're a function-scope tag, initially use a parent of NULL;
15263          this will be fixed up in decls_for_scope.  */
15264       if (decl_function_context (decl))
15265         context_die = NULL;
15266
15267       break;
15268
15269     default:
15270       return;
15271     }
15272
15273   gen_decl_die (decl, context_die);
15274 }
15275
15276 /* Output a marker (i.e. a label) for the beginning of the generated code for
15277    a lexical block.  */
15278
15279 static void
15280 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15281                        unsigned int blocknum)
15282 {
15283   switch_to_section (current_function_section ());
15284   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15285 }
15286
15287 /* Output a marker (i.e. a label) for the end of the generated code for a
15288    lexical block.  */
15289
15290 static void
15291 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15292 {
15293   switch_to_section (current_function_section ());
15294   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15295 }
15296
15297 /* Returns nonzero if it is appropriate not to emit any debugging
15298    information for BLOCK, because it doesn't contain any instructions.
15299
15300    Don't allow this for blocks with nested functions or local classes
15301    as we would end up with orphans, and in the presence of scheduling
15302    we may end up calling them anyway.  */
15303
15304 static bool
15305 dwarf2out_ignore_block (const_tree block)
15306 {
15307   tree decl;
15308
15309   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15310     if (TREE_CODE (decl) == FUNCTION_DECL
15311         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15312       return 0;
15313
15314   return 1;
15315 }
15316
15317 /* Hash table routines for file_hash.  */
15318
15319 static int
15320 file_table_eq (const void *p1_p, const void *p2_p)
15321 {
15322   const struct dwarf_file_data *const p1 =
15323     (const struct dwarf_file_data *) p1_p;
15324   const char *const p2 = (const char *) p2_p;
15325   return strcmp (p1->filename, p2) == 0;
15326 }
15327
15328 static hashval_t
15329 file_table_hash (const void *p_p)
15330 {
15331   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15332   return htab_hash_string (p->filename);
15333 }
15334
15335 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15336    dwarf2out.c) and return its "index".  The index of each (known) filename is
15337    just a unique number which is associated with only that one filename.  We
15338    need such numbers for the sake of generating labels (in the .debug_sfnames
15339    section) and references to those files numbers (in the .debug_srcinfo
15340    and.debug_macinfo sections).  If the filename given as an argument is not
15341    found in our current list, add it to the list and assign it the next
15342    available unique index number.  In order to speed up searches, we remember
15343    the index of the filename was looked up last.  This handles the majority of
15344    all searches.  */
15345
15346 static struct dwarf_file_data *
15347 lookup_filename (const char *file_name)
15348 {
15349   void ** slot;
15350   struct dwarf_file_data * created;
15351
15352   /* Check to see if the file name that was searched on the previous
15353      call matches this file name.  If so, return the index.  */
15354   if (file_table_last_lookup
15355       && (file_name == file_table_last_lookup->filename
15356           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15357     return file_table_last_lookup;
15358
15359   /* Didn't match the previous lookup, search the table.  */
15360   slot = htab_find_slot_with_hash (file_table, file_name,
15361                                    htab_hash_string (file_name), INSERT);
15362   if (*slot)
15363     return (struct dwarf_file_data *) *slot;
15364
15365   created = GGC_NEW (struct dwarf_file_data);
15366   created->filename = file_name;
15367   created->emitted_number = 0;
15368   *slot = created;
15369   return created;
15370 }
15371
15372 /* If the assembler will construct the file table, then translate the compiler
15373    internal file table number into the assembler file table number, and emit
15374    a .file directive if we haven't already emitted one yet.  The file table
15375    numbers are different because we prune debug info for unused variables and
15376    types, which may include filenames.  */
15377
15378 static int
15379 maybe_emit_file (struct dwarf_file_data * fd)
15380 {
15381   if (! fd->emitted_number)
15382     {
15383       if (last_emitted_file)
15384         fd->emitted_number = last_emitted_file->emitted_number + 1;
15385       else
15386         fd->emitted_number = 1;
15387       last_emitted_file = fd;
15388
15389       if (DWARF2_ASM_LINE_DEBUG_INFO)
15390         {
15391           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15392           output_quoted_string (asm_out_file,
15393                                 remap_debug_filename (fd->filename));
15394           fputc ('\n', asm_out_file);
15395         }
15396     }
15397
15398   return fd->emitted_number;
15399 }
15400
15401 /* Called by the final INSN scan whenever we see a var location.  We
15402    use it to drop labels in the right places, and throw the location in
15403    our lookup table.  */
15404
15405 static void
15406 dwarf2out_var_location (rtx loc_note)
15407 {
15408   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15409   struct var_loc_node *newloc;
15410   rtx prev_insn;
15411   static rtx last_insn;
15412   static const char *last_label;
15413   tree decl;
15414
15415   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15416     return;
15417   prev_insn = PREV_INSN (loc_note);
15418
15419   newloc = GGC_CNEW (struct var_loc_node);
15420   /* If the insn we processed last time is the previous insn
15421      and it is also a var location note, use the label we emitted
15422      last time.  */
15423   if (last_insn != NULL_RTX
15424       && last_insn == prev_insn
15425       && NOTE_P (prev_insn)
15426       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15427     {
15428       newloc->label = last_label;
15429     }
15430   else
15431     {
15432       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15433       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15434       loclabel_num++;
15435       newloc->label = ggc_strdup (loclabel);
15436     }
15437   newloc->var_loc_note = loc_note;
15438   newloc->next = NULL;
15439
15440   if (cfun && in_cold_section_p)
15441     newloc->section_label = crtl->subsections.cold_section_label;
15442   else
15443     newloc->section_label = text_section_label;
15444
15445   last_insn = loc_note;
15446   last_label = newloc->label;
15447   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15448   add_var_loc_to_decl (decl, newloc);
15449 }
15450
15451 /* We need to reset the locations at the beginning of each
15452    function. We can't do this in the end_function hook, because the
15453    declarations that use the locations won't have been output when
15454    that hook is called.  Also compute have_multiple_function_sections here.  */
15455
15456 static void
15457 dwarf2out_begin_function (tree fun)
15458 {
15459   htab_empty (decl_loc_table);
15460
15461   if (function_section (fun) != text_section)
15462     have_multiple_function_sections = true;
15463
15464   dwarf2out_note_section_used ();
15465 }
15466
15467 /* Output a label to mark the beginning of a source code line entry
15468    and record information relating to this source line, in
15469    'line_info_table' for later output of the .debug_line section.  */
15470
15471 static void
15472 dwarf2out_source_line (unsigned int line, const char *filename)
15473 {
15474   if (debug_info_level >= DINFO_LEVEL_NORMAL
15475       && line != 0)
15476     {
15477       int file_num = maybe_emit_file (lookup_filename (filename));
15478
15479       switch_to_section (current_function_section ());
15480
15481       /* If requested, emit something human-readable.  */
15482       if (flag_debug_asm)
15483         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15484                  filename, line);
15485
15486       if (DWARF2_ASM_LINE_DEBUG_INFO)
15487         {
15488           /* Emit the .loc directive understood by GNU as.  */
15489           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15490
15491           /* Indicate that line number info exists.  */
15492           line_info_table_in_use++;
15493         }
15494       else if (function_section (current_function_decl) != text_section)
15495         {
15496           dw_separate_line_info_ref line_info;
15497           targetm.asm_out.internal_label (asm_out_file,
15498                                           SEPARATE_LINE_CODE_LABEL,
15499                                           separate_line_info_table_in_use);
15500
15501           /* Expand the line info table if necessary.  */
15502           if (separate_line_info_table_in_use
15503               == separate_line_info_table_allocated)
15504             {
15505               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15506               separate_line_info_table
15507                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15508                                  separate_line_info_table,
15509                                  separate_line_info_table_allocated);
15510               memset (separate_line_info_table
15511                        + separate_line_info_table_in_use,
15512                       0,
15513                       (LINE_INFO_TABLE_INCREMENT
15514                        * sizeof (dw_separate_line_info_entry)));
15515             }
15516
15517           /* Add the new entry at the end of the line_info_table.  */
15518           line_info
15519             = &separate_line_info_table[separate_line_info_table_in_use++];
15520           line_info->dw_file_num = file_num;
15521           line_info->dw_line_num = line;
15522           line_info->function = current_function_funcdef_no;
15523         }
15524       else
15525         {
15526           dw_line_info_ref line_info;
15527
15528           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15529                                      line_info_table_in_use);
15530
15531           /* Expand the line info table if necessary.  */
15532           if (line_info_table_in_use == line_info_table_allocated)
15533             {
15534               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15535               line_info_table
15536                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15537                                  line_info_table_allocated);
15538               memset (line_info_table + line_info_table_in_use, 0,
15539                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15540             }
15541
15542           /* Add the new entry at the end of the line_info_table.  */
15543           line_info = &line_info_table[line_info_table_in_use++];
15544           line_info->dw_file_num = file_num;
15545           line_info->dw_line_num = line;
15546         }
15547     }
15548 }
15549
15550 /* Record the beginning of a new source file.  */
15551
15552 static void
15553 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15554 {
15555   if (flag_eliminate_dwarf2_dups)
15556     {
15557       /* Record the beginning of the file for break_out_includes.  */
15558       dw_die_ref bincl_die;
15559
15560       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15561       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15562     }
15563
15564   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15565     {
15566       int file_num = maybe_emit_file (lookup_filename (filename));
15567
15568       switch_to_section (debug_macinfo_section);
15569       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15570       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15571                                    lineno);
15572
15573       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15574     }
15575 }
15576
15577 /* Record the end of a source file.  */
15578
15579 static void
15580 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15581 {
15582   if (flag_eliminate_dwarf2_dups)
15583     /* Record the end of the file for break_out_includes.  */
15584     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15585
15586   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15587     {
15588       switch_to_section (debug_macinfo_section);
15589       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15590     }
15591 }
15592
15593 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
15594    the tail part of the directive line, i.e. the part which is past the
15595    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15596
15597 static void
15598 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
15599                   const char *buffer ATTRIBUTE_UNUSED)
15600 {
15601   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15602     {
15603       switch_to_section (debug_macinfo_section);
15604       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
15605       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15606       dw2_asm_output_nstring (buffer, -1, "The macro");
15607     }
15608 }
15609
15610 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
15611    the tail part of the directive line, i.e. the part which is past the
15612    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15613
15614 static void
15615 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
15616                  const char *buffer ATTRIBUTE_UNUSED)
15617 {
15618   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15619     {
15620       switch_to_section (debug_macinfo_section);
15621       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
15622       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15623       dw2_asm_output_nstring (buffer, -1, "The macro");
15624     }
15625 }
15626
15627 /* Set up for Dwarf output at the start of compilation.  */
15628
15629 static void
15630 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
15631 {
15632   /* Allocate the file_table.  */
15633   file_table = htab_create_ggc (50, file_table_hash,
15634                                 file_table_eq, NULL);
15635
15636   /* Allocate the decl_die_table.  */
15637   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
15638                                     decl_die_table_eq, NULL);
15639
15640   /* Allocate the decl_loc_table.  */
15641   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
15642                                     decl_loc_table_eq, NULL);
15643
15644   /* Allocate the initial hunk of the decl_scope_table.  */
15645   decl_scope_table = VEC_alloc (tree, gc, 256);
15646
15647   /* Allocate the initial hunk of the abbrev_die_table.  */
15648   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
15649   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
15650   /* Zero-th entry is allocated, but unused.  */
15651   abbrev_die_table_in_use = 1;
15652
15653   /* Allocate the initial hunk of the line_info_table.  */
15654   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
15655   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
15656
15657   /* Zero-th entry is allocated, but unused.  */
15658   line_info_table_in_use = 1;
15659
15660   /* Allocate the pubtypes and pubnames vectors.  */
15661   pubname_table = VEC_alloc (pubname_entry, gc, 32);
15662   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
15663
15664   /* Generate the initial DIE for the .debug section.  Note that the (string)
15665      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
15666      will (typically) be a relative pathname and that this pathname should be
15667      taken as being relative to the directory from which the compiler was
15668      invoked when the given (base) source file was compiled.  We will fill
15669      in this value in dwarf2out_finish.  */
15670   comp_unit_die = gen_compile_unit_die (NULL);
15671
15672   incomplete_types = VEC_alloc (tree, gc, 64);
15673
15674   used_rtx_array = VEC_alloc (rtx, gc, 32);
15675
15676   debug_info_section = get_section (DEBUG_INFO_SECTION,
15677                                     SECTION_DEBUG, NULL);
15678   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
15679                                       SECTION_DEBUG, NULL);
15680   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
15681                                        SECTION_DEBUG, NULL);
15682   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
15683                                        SECTION_DEBUG, NULL);
15684   debug_line_section = get_section (DEBUG_LINE_SECTION,
15685                                     SECTION_DEBUG, NULL);
15686   debug_loc_section = get_section (DEBUG_LOC_SECTION,
15687                                    SECTION_DEBUG, NULL);
15688   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
15689                                         SECTION_DEBUG, NULL);
15690 #ifdef DEBUG_PUBTYPES_SECTION
15691   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
15692                                         SECTION_DEBUG, NULL);
15693 #endif
15694   debug_str_section = get_section (DEBUG_STR_SECTION,
15695                                    DEBUG_STR_SECTION_FLAGS, NULL);
15696   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
15697                                       SECTION_DEBUG, NULL);
15698   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
15699                                      SECTION_DEBUG, NULL);
15700
15701   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
15702   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
15703                                DEBUG_ABBREV_SECTION_LABEL, 0);
15704   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
15705   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
15706                                COLD_TEXT_SECTION_LABEL, 0);
15707   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
15708
15709   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
15710                                DEBUG_INFO_SECTION_LABEL, 0);
15711   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
15712                                DEBUG_LINE_SECTION_LABEL, 0);
15713   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
15714                                DEBUG_RANGES_SECTION_LABEL, 0);
15715   switch_to_section (debug_abbrev_section);
15716   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
15717   switch_to_section (debug_info_section);
15718   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
15719   switch_to_section (debug_line_section);
15720   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
15721
15722   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15723     {
15724       switch_to_section (debug_macinfo_section);
15725       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
15726                                    DEBUG_MACINFO_SECTION_LABEL, 0);
15727       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
15728     }
15729
15730   switch_to_section (text_section);
15731   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
15732   if (flag_reorder_blocks_and_partition)
15733     {
15734       cold_text_section = unlikely_text_section ();
15735       switch_to_section (cold_text_section);
15736       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
15737     }
15738 }
15739
15740 /* A helper function for dwarf2out_finish called through
15741    ht_forall.  Emit one queued .debug_str string.  */
15742
15743 static int
15744 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
15745 {
15746   struct indirect_string_node *node = (struct indirect_string_node *) *h;
15747
15748   if (node->form == DW_FORM_strp)
15749     {
15750       switch_to_section (debug_str_section);
15751       ASM_OUTPUT_LABEL (asm_out_file, node->label);
15752       assemble_string (node->str, strlen (node->str) + 1);
15753     }
15754
15755   return 1;
15756 }
15757
15758 #if ENABLE_ASSERT_CHECKING
15759 /* Verify that all marks are clear.  */
15760
15761 static void
15762 verify_marks_clear (dw_die_ref die)
15763 {
15764   dw_die_ref c;
15765
15766   gcc_assert (! die->die_mark);
15767   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
15768 }
15769 #endif /* ENABLE_ASSERT_CHECKING */
15770
15771 /* Clear the marks for a die and its children.
15772    Be cool if the mark isn't set.  */
15773
15774 static void
15775 prune_unmark_dies (dw_die_ref die)
15776 {
15777   dw_die_ref c;
15778
15779   if (die->die_mark)
15780     die->die_mark = 0;
15781   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
15782 }
15783
15784 /* Given DIE that we're marking as used, find any other dies
15785    it references as attributes and mark them as used.  */
15786
15787 static void
15788 prune_unused_types_walk_attribs (dw_die_ref die)
15789 {
15790   dw_attr_ref a;
15791   unsigned ix;
15792
15793   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15794     {
15795       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
15796         {
15797           /* A reference to another DIE.
15798              Make sure that it will get emitted.  */
15799           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
15800         }
15801       /* Set the string's refcount to 0 so that prune_unused_types_mark
15802          accounts properly for it.  */
15803       if (AT_class (a) == dw_val_class_str)
15804         a->dw_attr_val.v.val_str->refcount = 0;
15805     }
15806 }
15807
15808
15809 /* Mark DIE as being used.  If DOKIDS is true, then walk down
15810    to DIE's children.  */
15811
15812 static void
15813 prune_unused_types_mark (dw_die_ref die, int dokids)
15814 {
15815   dw_die_ref c;
15816
15817   if (die->die_mark == 0)
15818     {
15819       /* We haven't done this node yet.  Mark it as used.  */
15820       die->die_mark = 1;
15821
15822       /* We also have to mark its parents as used.
15823          (But we don't want to mark our parents' kids due to this.)  */
15824       if (die->die_parent)
15825         prune_unused_types_mark (die->die_parent, 0);
15826
15827       /* Mark any referenced nodes.  */
15828       prune_unused_types_walk_attribs (die);
15829
15830       /* If this node is a specification,
15831          also mark the definition, if it exists.  */
15832       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
15833         prune_unused_types_mark (die->die_definition, 1);
15834     }
15835
15836   if (dokids && die->die_mark != 2)
15837     {
15838       /* We need to walk the children, but haven't done so yet.
15839          Remember that we've walked the kids.  */
15840       die->die_mark = 2;
15841
15842       /* If this is an array type, we need to make sure our
15843          kids get marked, even if they're types.  */
15844       if (die->die_tag == DW_TAG_array_type)
15845         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
15846       else
15847         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15848     }
15849 }
15850
15851
15852 /* Walk the tree DIE and mark types that we actually use.  */
15853
15854 static void
15855 prune_unused_types_walk (dw_die_ref die)
15856 {
15857   dw_die_ref c;
15858
15859   /* Don't do anything if this node is already marked.  */
15860   if (die->die_mark)
15861     return;
15862
15863   switch (die->die_tag)
15864     {
15865     case DW_TAG_const_type:
15866     case DW_TAG_packed_type:
15867     case DW_TAG_pointer_type:
15868     case DW_TAG_reference_type:
15869     case DW_TAG_volatile_type:
15870     case DW_TAG_typedef:
15871     case DW_TAG_array_type:
15872     case DW_TAG_structure_type:
15873     case DW_TAG_union_type:
15874     case DW_TAG_class_type:
15875     case DW_TAG_interface_type:
15876     case DW_TAG_friend:
15877     case DW_TAG_variant_part:
15878     case DW_TAG_enumeration_type:
15879     case DW_TAG_subroutine_type:
15880     case DW_TAG_string_type:
15881     case DW_TAG_set_type:
15882     case DW_TAG_subrange_type:
15883     case DW_TAG_ptr_to_member_type:
15884     case DW_TAG_file_type:
15885       if (die->die_perennial_p)
15886         break;
15887
15888       /* It's a type node --- don't mark it.  */
15889       return;
15890
15891     default:
15892       /* Mark everything else.  */
15893       break;
15894   }
15895
15896   die->die_mark = 1;
15897
15898   /* Now, mark any dies referenced from here.  */
15899   prune_unused_types_walk_attribs (die);
15900
15901   /* Mark children.  */
15902   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15903 }
15904
15905 /* Increment the string counts on strings referred to from DIE's
15906    attributes.  */
15907
15908 static void
15909 prune_unused_types_update_strings (dw_die_ref die)
15910 {
15911   dw_attr_ref a;
15912   unsigned ix;
15913
15914   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15915     if (AT_class (a) == dw_val_class_str)
15916       {
15917         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15918         s->refcount++;
15919         /* Avoid unnecessarily putting strings that are used less than
15920            twice in the hash table.  */
15921         if (s->refcount
15922             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15923           {
15924             void ** slot;
15925             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15926                                              htab_hash_string (s->str),
15927                                              INSERT);
15928             gcc_assert (*slot == NULL);
15929             *slot = s;
15930           }
15931       }
15932 }
15933
15934 /* Remove from the tree DIE any dies that aren't marked.  */
15935
15936 static void
15937 prune_unused_types_prune (dw_die_ref die)
15938 {
15939   dw_die_ref c;
15940
15941   gcc_assert (die->die_mark);
15942   prune_unused_types_update_strings (die);
15943
15944   if (! die->die_child)
15945     return;
15946
15947   c = die->die_child;
15948   do {
15949     dw_die_ref prev = c;
15950     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15951       if (c == die->die_child)
15952         {
15953           /* No marked children between 'prev' and the end of the list.  */
15954           if (prev == c)
15955             /* No marked children at all.  */
15956             die->die_child = NULL;
15957           else
15958             {
15959               prev->die_sib = c->die_sib;
15960               die->die_child = prev;
15961             }
15962           return;
15963         }
15964
15965     if (c != prev->die_sib)
15966       prev->die_sib = c;
15967     prune_unused_types_prune (c);
15968   } while (c != die->die_child);
15969 }
15970
15971
15972 /* Remove dies representing declarations that we never use.  */
15973
15974 static void
15975 prune_unused_types (void)
15976 {
15977   unsigned int i;
15978   limbo_die_node *node;
15979   pubname_ref pub;
15980
15981 #if ENABLE_ASSERT_CHECKING
15982   /* All the marks should already be clear.  */
15983   verify_marks_clear (comp_unit_die);
15984   for (node = limbo_die_list; node; node = node->next)
15985     verify_marks_clear (node->die);
15986 #endif /* ENABLE_ASSERT_CHECKING */
15987
15988   /* Set the mark on nodes that are actually used.  */
15989   prune_unused_types_walk (comp_unit_die);
15990   for (node = limbo_die_list; node; node = node->next)
15991     prune_unused_types_walk (node->die);
15992
15993   /* Also set the mark on nodes referenced from the
15994      pubname_table or arange_table.  */
15995   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15996     prune_unused_types_mark (pub->die, 1);
15997   for (i = 0; i < arange_table_in_use; i++)
15998     prune_unused_types_mark (arange_table[i], 1);
15999
16000   /* Get rid of nodes that aren't marked; and update the string counts.  */
16001   if (debug_str_hash)
16002     htab_empty (debug_str_hash);
16003   prune_unused_types_prune (comp_unit_die);
16004   for (node = limbo_die_list; node; node = node->next)
16005     prune_unused_types_prune (node->die);
16006
16007   /* Leave the marks clear.  */
16008   prune_unmark_dies (comp_unit_die);
16009   for (node = limbo_die_list; node; node = node->next)
16010     prune_unmark_dies (node->die);
16011 }
16012
16013 /* Set the parameter to true if there are any relative pathnames in
16014    the file table.  */
16015 static int
16016 file_table_relative_p (void ** slot, void *param)
16017 {
16018   bool *p = (bool *) param;
16019   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16020   if (!IS_ABSOLUTE_PATH (d->filename))
16021     {
16022       *p = true;
16023       return 0;
16024     }
16025   return 1;
16026 }
16027
16028 /* Output stuff that dwarf requires at the end of every file,
16029    and generate the DWARF-2 debugging info.  */
16030
16031 static void
16032 dwarf2out_finish (const char *filename)
16033 {
16034   limbo_die_node *node, *next_node;
16035   dw_die_ref die = 0;
16036
16037   /* Add the name for the main input file now.  We delayed this from
16038      dwarf2out_init to avoid complications with PCH.  */
16039   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16040   if (!IS_ABSOLUTE_PATH (filename))
16041     add_comp_dir_attribute (comp_unit_die);
16042   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16043     {
16044       bool p = false;
16045       htab_traverse (file_table, file_table_relative_p, &p);
16046       if (p)
16047         add_comp_dir_attribute (comp_unit_die);
16048     }
16049
16050   /* Traverse the limbo die list, and add parent/child links.  The only
16051      dies without parents that should be here are concrete instances of
16052      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16053      For concrete instances, we can get the parent die from the abstract
16054      instance.  */
16055   for (node = limbo_die_list; node; node = next_node)
16056     {
16057       next_node = node->next;
16058       die = node->die;
16059
16060       if (die->die_parent == NULL)
16061         {
16062           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16063
16064           if (origin)
16065             add_child_die (origin->die_parent, die);
16066           else if (die == comp_unit_die)
16067             ;
16068           else if (errorcount > 0 || sorrycount > 0)
16069             /* It's OK to be confused by errors in the input.  */
16070             add_child_die (comp_unit_die, die);
16071           else
16072             {
16073               /* In certain situations, the lexical block containing a
16074                  nested function can be optimized away, which results
16075                  in the nested function die being orphaned.  Likewise
16076                  with the return type of that nested function.  Force
16077                  this to be a child of the containing function.
16078
16079                  It may happen that even the containing function got fully
16080                  inlined and optimized out.  In that case we are lost and
16081                  assign the empty child.  This should not be big issue as
16082                  the function is likely unreachable too.  */
16083               tree context = NULL_TREE;
16084
16085               gcc_assert (node->created_for);
16086
16087               if (DECL_P (node->created_for))
16088                 context = DECL_CONTEXT (node->created_for);
16089               else if (TYPE_P (node->created_for))
16090                 context = TYPE_CONTEXT (node->created_for);
16091
16092               gcc_assert (context
16093                           && (TREE_CODE (context) == FUNCTION_DECL
16094                               || TREE_CODE (context) == NAMESPACE_DECL));
16095
16096               origin = lookup_decl_die (context);
16097               if (origin)
16098                 add_child_die (origin, die);
16099               else
16100                 add_child_die (comp_unit_die, die);
16101             }
16102         }
16103     }
16104
16105   limbo_die_list = NULL;
16106
16107   /* Walk through the list of incomplete types again, trying once more to
16108      emit full debugging info for them.  */
16109   retry_incomplete_types ();
16110
16111   if (flag_eliminate_unused_debug_types)
16112     prune_unused_types ();
16113
16114   /* Generate separate CUs for each of the include files we've seen.
16115      They will go into limbo_die_list.  */
16116   if (flag_eliminate_dwarf2_dups)
16117     break_out_includes (comp_unit_die);
16118
16119   /* Traverse the DIE's and add add sibling attributes to those DIE's
16120      that have children.  */
16121   add_sibling_attributes (comp_unit_die);
16122   for (node = limbo_die_list; node; node = node->next)
16123     add_sibling_attributes (node->die);
16124
16125   /* Output a terminator label for the .text section.  */
16126   switch_to_section (text_section);
16127   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16128   if (flag_reorder_blocks_and_partition)
16129     {
16130       switch_to_section (unlikely_text_section ());
16131       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16132     }
16133
16134   /* We can only use the low/high_pc attributes if all of the code was
16135      in .text.  */
16136   if (!have_multiple_function_sections)
16137     {
16138       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16139       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16140     }
16141
16142   else
16143     {
16144       unsigned fde_idx = 0;
16145
16146       /* We need to give .debug_loc and .debug_ranges an appropriate
16147          "base address".  Use zero so that these addresses become
16148          absolute.  Historically, we've emitted the unexpected
16149          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16150          Emit both to give time for other tools to adapt.  */
16151       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16152       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16153
16154       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16155                          add_ranges_by_labels (text_section_label,
16156                                                text_end_label));
16157       if (flag_reorder_blocks_and_partition)
16158         add_ranges_by_labels (cold_text_section_label,
16159                               cold_end_label);
16160
16161       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16162         {
16163           dw_fde_ref fde = &fde_table[fde_idx];
16164
16165           if (fde->dw_fde_switched_sections)
16166             {
16167               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16168                                     fde->dw_fde_hot_section_end_label);
16169               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16170                                     fde->dw_fde_unlikely_section_end_label);
16171             }
16172           else
16173             add_ranges_by_labels (fde->dw_fde_begin,
16174                                   fde->dw_fde_end);
16175         }
16176
16177       add_ranges (NULL);
16178     }
16179
16180   /* Output location list section if necessary.  */
16181   if (have_location_lists)
16182     {
16183       /* Output the location lists info.  */
16184       switch_to_section (debug_loc_section);
16185       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16186                                    DEBUG_LOC_SECTION_LABEL, 0);
16187       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16188       output_location_lists (die);
16189     }
16190
16191   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16192     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16193                     debug_line_section_label);
16194
16195   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16196     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16197
16198   /* Output all of the compilation units.  We put the main one last so that
16199      the offsets are available to output_pubnames.  */
16200   for (node = limbo_die_list; node; node = node->next)
16201     output_comp_unit (node->die, 0);
16202
16203   output_comp_unit (comp_unit_die, 0);
16204
16205   /* Output the abbreviation table.  */
16206   switch_to_section (debug_abbrev_section);
16207   output_abbrev_section ();
16208
16209   /* Output public names table if necessary.  */
16210   if (!VEC_empty (pubname_entry, pubname_table))
16211     {
16212       switch_to_section (debug_pubnames_section);
16213       output_pubnames (pubname_table);
16214     }
16215
16216 #ifdef DEBUG_PUBTYPES_SECTION
16217   /* Output public types table if necessary.  */
16218   if (!VEC_empty (pubname_entry, pubtype_table))
16219     {
16220       switch_to_section (debug_pubtypes_section);
16221       output_pubnames (pubtype_table);
16222     }
16223 #endif
16224
16225   /* Output the address range information.  We only put functions in the arange
16226      table, so don't write it out if we don't have any.  */
16227   if (fde_table_in_use)
16228     {
16229       switch_to_section (debug_aranges_section);
16230       output_aranges ();
16231     }
16232
16233   /* Output ranges section if necessary.  */
16234   if (ranges_table_in_use)
16235     {
16236       switch_to_section (debug_ranges_section);
16237       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16238       output_ranges ();
16239     }
16240
16241   /* Output the source line correspondence table.  We must do this
16242      even if there is no line information.  Otherwise, on an empty
16243      translation unit, we will generate a present, but empty,
16244      .debug_info section.  IRIX 6.5 `nm' will then complain when
16245      examining the file.  This is done late so that any filenames
16246      used by the debug_info section are marked as 'used'.  */
16247   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16248     {
16249       switch_to_section (debug_line_section);
16250       output_line_info ();
16251     }
16252
16253   /* Have to end the macro section.  */
16254   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16255     {
16256       switch_to_section (debug_macinfo_section);
16257       dw2_asm_output_data (1, 0, "End compilation unit");
16258     }
16259
16260   /* If we emitted any DW_FORM_strp form attribute, output the string
16261      table too.  */
16262   if (debug_str_hash)
16263     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16264 }
16265 #else
16266
16267 /* This should never be used, but its address is needed for comparisons.  */
16268 const struct gcc_debug_hooks dwarf2_debug_hooks;
16269
16270 #endif /* DWARF2_DEBUGGING_INFO */
16271
16272 #include "gt-dwarf2out.h"