OSDN Git Service

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