OSDN Git Service

* dwarf2out.c (constant_size): Use HOST_WIDE_INT in parameter type.
[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 #ifdef MIPS_DEBUGGING_INFO
141   return false;
142 #endif
143   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
144     return false;
145   if (!eh_personality_libfunc)
146     return true;
147   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
148     return false;
149
150   /* Make sure the personality encoding is one the assembler can support.
151      In particular, aligned addresses can't be handled.  */
152   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
153   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
154     return false;
155   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
156   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
157     return false;
158
159   return true;
160 }
161
162 /* The size of the target's pointer type.  */
163 #ifndef PTR_SIZE
164 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
165 #endif
166
167 /* Array of RTXes referenced by the debugging information, which therefore
168    must be kept around forever.  */
169 static GTY(()) VEC(rtx,gc) *used_rtx_array;
170
171 /* A pointer to the base of a list of incomplete types which might be
172    completed at some later time.  incomplete_types_list needs to be a
173    VEC(tree,gc) because we want to tell the garbage collector about
174    it.  */
175 static GTY(()) VEC(tree,gc) *incomplete_types;
176
177 /* A pointer to the base of a table of references to declaration
178    scopes.  This table is a display which tracks the nesting
179    of declaration scopes at the current scope and containing
180    scopes.  This table is used to find the proper place to
181    define type declaration DIE's.  */
182 static GTY(()) VEC(tree,gc) *decl_scope_table;
183
184 /* Pointers to various DWARF2 sections.  */
185 static GTY(()) section *debug_info_section;
186 static GTY(()) section *debug_abbrev_section;
187 static GTY(()) section *debug_aranges_section;
188 static GTY(()) section *debug_macinfo_section;
189 static GTY(()) section *debug_line_section;
190 static GTY(()) section *debug_loc_section;
191 static GTY(()) section *debug_pubnames_section;
192 static GTY(()) section *debug_pubtypes_section;
193 static GTY(()) section *debug_str_section;
194 static GTY(()) section *debug_ranges_section;
195 static GTY(()) section *debug_frame_section;
196
197 /* How to start an assembler comment.  */
198 #ifndef ASM_COMMENT_START
199 #define ASM_COMMENT_START ";#"
200 #endif
201
202 typedef struct dw_cfi_struct *dw_cfi_ref;
203 typedef struct dw_fde_struct *dw_fde_ref;
204 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
205
206 /* Call frames are described using a sequence of Call Frame
207    Information instructions.  The register number, offset
208    and address fields are provided as possible operands;
209    their use is selected by the opcode field.  */
210
211 enum dw_cfi_oprnd_type {
212   dw_cfi_oprnd_unused,
213   dw_cfi_oprnd_reg_num,
214   dw_cfi_oprnd_offset,
215   dw_cfi_oprnd_addr,
216   dw_cfi_oprnd_loc
217 };
218
219 typedef union dw_cfi_oprnd_struct GTY(())
220 {
221   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
222   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
223   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
224   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
225 }
226 dw_cfi_oprnd;
227
228 typedef struct dw_cfi_struct GTY(())
229 {
230   dw_cfi_ref dw_cfi_next;
231   enum dwarf_call_frame_info dw_cfi_opc;
232   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
233     dw_cfi_oprnd1;
234   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
235     dw_cfi_oprnd2;
236 }
237 dw_cfi_node;
238
239 /* This is how we define the location of the CFA. We use to handle it
240    as REG + OFFSET all the time,  but now it can be more complex.
241    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
242    Instead of passing around REG and OFFSET, we pass a copy
243    of this structure.  */
244 typedef struct cfa_loc GTY(())
245 {
246   HOST_WIDE_INT offset;
247   HOST_WIDE_INT base_offset;
248   unsigned int reg;
249   int indirect;            /* 1 if CFA is accessed via a dereference.  */
250 } dw_cfa_location;
251
252 /* All call frame descriptions (FDE's) in the GCC generated DWARF
253    refer to a single Common Information Entry (CIE), defined at
254    the beginning of the .debug_frame section.  This use of a single
255    CIE obviates the need to keep track of multiple CIE's
256    in the DWARF generation routines below.  */
257
258 typedef struct dw_fde_struct GTY(())
259 {
260   tree decl;
261   const char *dw_fde_begin;
262   const char *dw_fde_current_label;
263   const char *dw_fde_end;
264   const char *dw_fde_hot_section_label;
265   const char *dw_fde_hot_section_end_label;
266   const char *dw_fde_unlikely_section_label;
267   const char *dw_fde_unlikely_section_end_label;
268   bool dw_fde_switched_sections;
269   dw_cfi_ref dw_fde_cfi;
270   unsigned funcdef_number;
271   HOST_WIDE_INT stack_realignment;
272   /* Dynamic realign argument pointer register.  */
273   unsigned int drap_reg;
274   /* Virtual dynamic realign argument pointer register.  */
275   unsigned int vdrap_reg;
276   unsigned all_throwers_are_sibcalls : 1;
277   unsigned nothrow : 1;
278   unsigned uses_eh_lsda : 1;
279   /* Whether we did stack realign in this call frame.  */
280   unsigned stack_realign : 1;
281   /* Whether dynamic realign argument pointer register has been saved.  */
282   unsigned drap_reg_saved: 1;
283 }
284 dw_fde_node;
285
286 /* Maximum size (in bytes) of an artificially generated label.  */
287 #define MAX_ARTIFICIAL_LABEL_BYTES      30
288
289 /* The size of addresses as they appear in the Dwarf 2 data.
290    Some architectures use word addresses to refer to code locations,
291    but Dwarf 2 info always uses byte addresses.  On such machines,
292    Dwarf 2 addresses need to be larger than the architecture's
293    pointers.  */
294 #ifndef DWARF2_ADDR_SIZE
295 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
296 #endif
297
298 /* The size in bytes of a DWARF field indicating an offset or length
299    relative to a debug info section, specified to be 4 bytes in the
300    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
301    as PTR_SIZE.  */
302
303 #ifndef DWARF_OFFSET_SIZE
304 #define DWARF_OFFSET_SIZE 4
305 #endif
306
307 /* According to the (draft) DWARF 3 specification, the initial length
308    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
309    bytes are 0xffffffff, followed by the length stored in the next 8
310    bytes.
311
312    However, the SGI/MIPS ABI uses an initial length which is equal to
313    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
314
315 #ifndef DWARF_INITIAL_LENGTH_SIZE
316 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
317 #endif
318
319 #define DWARF_VERSION 2
320
321 /* Round SIZE up to the nearest BOUNDARY.  */
322 #define DWARF_ROUND(SIZE,BOUNDARY) \
323   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
324
325 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
326 #ifndef DWARF_CIE_DATA_ALIGNMENT
327 #ifdef STACK_GROWS_DOWNWARD
328 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
329 #else
330 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
331 #endif
332 #endif
333
334 /* CIE identifier.  */
335 #if HOST_BITS_PER_WIDE_INT >= 64
336 #define DWARF_CIE_ID \
337   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
338 #else
339 #define DWARF_CIE_ID DW_CIE_ID
340 #endif
341
342 /* A pointer to the base of a table that contains frame description
343    information for each routine.  */
344 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
345
346 /* Number of elements currently allocated for fde_table.  */
347 static GTY(()) unsigned fde_table_allocated;
348
349 /* Number of elements in fde_table currently in use.  */
350 static GTY(()) unsigned fde_table_in_use;
351
352 /* Size (in elements) of increments by which we may expand the
353    fde_table.  */
354 #define FDE_TABLE_INCREMENT 256
355
356 /* Get the current fde_table entry we should use.  */
357
358 static inline dw_fde_ref
359 current_fde (void)
360 {
361   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
362 }
363
364 /* A list of call frame insns for the CIE.  */
365 static GTY(()) dw_cfi_ref cie_cfi_head;
366
367 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
368 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
369    attribute that accelerates the lookup of the FDE associated
370    with the subprogram.  This variable holds the table index of the FDE
371    associated with the current function (body) definition.  */
372 static unsigned current_funcdef_fde;
373 #endif
374
375 struct indirect_string_node GTY(())
376 {
377   const char *str;
378   unsigned int refcount;
379   unsigned int form;
380   char *label;
381 };
382
383 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
384
385 static GTY(()) int dw2_string_counter;
386 static GTY(()) unsigned long dwarf2out_cfi_label_num;
387
388 /* True if the compilation unit places functions in more than one section.  */
389 static GTY(()) bool have_multiple_function_sections = false;
390
391 /* Whether the default text and cold text sections have been used at all.  */
392
393 static GTY(()) bool text_section_used = false;
394 static GTY(()) bool cold_text_section_used = false;
395
396 /* The default cold text section.  */
397 static GTY(()) section *cold_text_section;
398
399 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
400
401 /* Forward declarations for functions defined in this file.  */
402
403 static char *stripattributes (const char *);
404 static const char *dwarf_cfi_name (unsigned);
405 static dw_cfi_ref new_cfi (void);
406 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
407 static void add_fde_cfi (const char *, dw_cfi_ref);
408 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
409 static void lookup_cfa (dw_cfa_location *);
410 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
411 #ifdef DWARF2_UNWIND_INFO
412 static void initial_return_save (rtx);
413 #endif
414 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
415 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
416 static void output_cfi_directive (dw_cfi_ref);
417 static void output_call_frame_info (int);
418 static void dwarf2out_note_section_used (void);
419 static void dwarf2out_stack_adjust (rtx, bool);
420 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
421 static void flush_queued_reg_saves (void);
422 static bool clobbers_queued_reg_save (const_rtx);
423 static void dwarf2out_frame_debug_expr (rtx, const char *);
424
425 /* Support for complex CFA locations.  */
426 static void output_cfa_loc (dw_cfi_ref);
427 static void output_cfa_loc_raw (dw_cfi_ref);
428 static void get_cfa_from_loc_descr (dw_cfa_location *,
429                                     struct dw_loc_descr_struct *);
430 static struct dw_loc_descr_struct *build_cfa_loc
431   (dw_cfa_location *, HOST_WIDE_INT);
432 static struct dw_loc_descr_struct *build_cfa_aligned_loc
433   (HOST_WIDE_INT, HOST_WIDE_INT);
434 static void def_cfa_1 (const char *, dw_cfa_location *);
435
436 /* How to start an assembler comment.  */
437 #ifndef ASM_COMMENT_START
438 #define ASM_COMMENT_START ";#"
439 #endif
440
441 /* Data and reference forms for relocatable data.  */
442 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
443 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
444
445 #ifndef DEBUG_FRAME_SECTION
446 #define DEBUG_FRAME_SECTION     ".debug_frame"
447 #endif
448
449 #ifndef FUNC_BEGIN_LABEL
450 #define FUNC_BEGIN_LABEL        "LFB"
451 #endif
452
453 #ifndef FUNC_END_LABEL
454 #define FUNC_END_LABEL          "LFE"
455 #endif
456
457 #ifndef FRAME_BEGIN_LABEL
458 #define FRAME_BEGIN_LABEL       "Lframe"
459 #endif
460 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
461 #define CIE_END_LABEL           "LECIE"
462 #define FDE_LABEL               "LSFDE"
463 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
464 #define FDE_END_LABEL           "LEFDE"
465 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
466 #define LINE_NUMBER_END_LABEL   "LELT"
467 #define LN_PROLOG_AS_LABEL      "LASLTP"
468 #define LN_PROLOG_END_LABEL     "LELTP"
469 #define DIE_LABEL_PREFIX        "DW"
470
471 /* The DWARF 2 CFA column which tracks the return address.  Normally this
472    is the column for PC, or the first column after all of the hard
473    registers.  */
474 #ifndef DWARF_FRAME_RETURN_COLUMN
475 #ifdef PC_REGNUM
476 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
477 #else
478 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
479 #endif
480 #endif
481
482 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
483    default, we just provide columns for all registers.  */
484 #ifndef DWARF_FRAME_REGNUM
485 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
486 #endif
487 \f
488 /* Hook used by __throw.  */
489
490 rtx
491 expand_builtin_dwarf_sp_column (void)
492 {
493   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
494   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
495 }
496
497 /* Return a pointer to a copy of the section string name S with all
498    attributes stripped off, and an asterisk prepended (for assemble_name).  */
499
500 static inline char *
501 stripattributes (const char *s)
502 {
503   char *stripped = XNEWVEC (char, strlen (s) + 2);
504   char *p = stripped;
505
506   *p++ = '*';
507
508   while (*s && *s != ',')
509     *p++ = *s++;
510
511   *p = '\0';
512   return stripped;
513 }
514
515 /* MEM is a memory reference for the register size table, each element of
516    which has mode MODE.  Initialize column C as a return address column.  */
517
518 static void
519 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
520 {
521   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
522   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
523   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
524 }
525
526 /* Generate code to initialize the register size table.  */
527
528 void
529 expand_builtin_init_dwarf_reg_sizes (tree address)
530 {
531   unsigned int i;
532   enum machine_mode mode = TYPE_MODE (char_type_node);
533   rtx addr = expand_normal (address);
534   rtx mem = gen_rtx_MEM (BLKmode, addr);
535   bool wrote_return_column = false;
536
537   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
538     {
539       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
540
541       if (rnum < DWARF_FRAME_REGISTERS)
542         {
543           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
544           enum machine_mode save_mode = reg_raw_mode[i];
545           HOST_WIDE_INT size;
546
547           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
548             save_mode = choose_hard_reg_mode (i, 1, true);
549           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
550             {
551               if (save_mode == VOIDmode)
552                 continue;
553               wrote_return_column = true;
554             }
555           size = GET_MODE_SIZE (save_mode);
556           if (offset < 0)
557             continue;
558
559           emit_move_insn (adjust_address (mem, mode, offset),
560                           gen_int_mode (size, mode));
561         }
562     }
563
564   if (!wrote_return_column)
565     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
566
567 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
568   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
569 #endif
570
571   targetm.init_dwarf_reg_sizes_extra (address);
572 }
573
574 /* Convert a DWARF call frame info. operation to its string name */
575
576 static const char *
577 dwarf_cfi_name (unsigned int cfi_opc)
578 {
579   switch (cfi_opc)
580     {
581     case DW_CFA_advance_loc:
582       return "DW_CFA_advance_loc";
583     case DW_CFA_offset:
584       return "DW_CFA_offset";
585     case DW_CFA_restore:
586       return "DW_CFA_restore";
587     case DW_CFA_nop:
588       return "DW_CFA_nop";
589     case DW_CFA_set_loc:
590       return "DW_CFA_set_loc";
591     case DW_CFA_advance_loc1:
592       return "DW_CFA_advance_loc1";
593     case DW_CFA_advance_loc2:
594       return "DW_CFA_advance_loc2";
595     case DW_CFA_advance_loc4:
596       return "DW_CFA_advance_loc4";
597     case DW_CFA_offset_extended:
598       return "DW_CFA_offset_extended";
599     case DW_CFA_restore_extended:
600       return "DW_CFA_restore_extended";
601     case DW_CFA_undefined:
602       return "DW_CFA_undefined";
603     case DW_CFA_same_value:
604       return "DW_CFA_same_value";
605     case DW_CFA_register:
606       return "DW_CFA_register";
607     case DW_CFA_remember_state:
608       return "DW_CFA_remember_state";
609     case DW_CFA_restore_state:
610       return "DW_CFA_restore_state";
611     case DW_CFA_def_cfa:
612       return "DW_CFA_def_cfa";
613     case DW_CFA_def_cfa_register:
614       return "DW_CFA_def_cfa_register";
615     case DW_CFA_def_cfa_offset:
616       return "DW_CFA_def_cfa_offset";
617
618     /* DWARF 3 */
619     case DW_CFA_def_cfa_expression:
620       return "DW_CFA_def_cfa_expression";
621     case DW_CFA_expression:
622       return "DW_CFA_expression";
623     case DW_CFA_offset_extended_sf:
624       return "DW_CFA_offset_extended_sf";
625     case DW_CFA_def_cfa_sf:
626       return "DW_CFA_def_cfa_sf";
627     case DW_CFA_def_cfa_offset_sf:
628       return "DW_CFA_def_cfa_offset_sf";
629
630     /* SGI/MIPS specific */
631     case DW_CFA_MIPS_advance_loc8:
632       return "DW_CFA_MIPS_advance_loc8";
633
634     /* GNU extensions */
635     case DW_CFA_GNU_window_save:
636       return "DW_CFA_GNU_window_save";
637     case DW_CFA_GNU_args_size:
638       return "DW_CFA_GNU_args_size";
639     case DW_CFA_GNU_negative_offset_extended:
640       return "DW_CFA_GNU_negative_offset_extended";
641
642     default:
643       return "DW_CFA_<unknown>";
644     }
645 }
646
647 /* Return a pointer to a newly allocated Call Frame Instruction.  */
648
649 static inline dw_cfi_ref
650 new_cfi (void)
651 {
652   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
653
654   cfi->dw_cfi_next = NULL;
655   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
656   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
657
658   return cfi;
659 }
660
661 /* Add a Call Frame Instruction to list of instructions.  */
662
663 static inline void
664 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
665 {
666   dw_cfi_ref *p;
667   dw_fde_ref fde = current_fde ();
668
669   /* When DRAP is used, CFA is defined with an expression.  Redefine
670      CFA may lead to a different CFA value.   */
671   if (fde && fde->drap_reg != INVALID_REGNUM)
672     switch (cfi->dw_cfi_opc)
673       {
674         case DW_CFA_def_cfa_register:
675         case DW_CFA_def_cfa_offset:
676         case DW_CFA_def_cfa_offset_sf:
677         case DW_CFA_def_cfa:
678         case DW_CFA_def_cfa_sf:
679           gcc_unreachable ();
680
681         default:
682           break;
683       }
684
685   /* Find the end of the chain.  */
686   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
687     ;
688
689   *p = cfi;
690 }
691
692 /* Generate a new label for the CFI info to refer to.  */
693
694 char *
695 dwarf2out_cfi_label (void)
696 {
697   static char label[20];
698
699   if (dwarf2out_do_cfi_asm ())
700     {
701       /* In this case, we will be emitting the asm directive instead of
702          the label, so just return a placeholder to keep the rest of the
703          interfaces happy.  */
704       strcpy (label, "<do not output>");
705     }
706   else
707     {
708       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
709       ASM_OUTPUT_LABEL (asm_out_file, label);
710     }
711
712   return label;
713 }
714
715 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
716    or to the CIE if LABEL is NULL.  */
717
718 static void
719 add_fde_cfi (const char *label, dw_cfi_ref cfi)
720 {
721   dw_cfi_ref *list_head = &cie_cfi_head;
722
723   if (dwarf2out_do_cfi_asm ())
724     {
725       if (label)
726         {
727           output_cfi_directive (cfi);
728
729           /* We still have to add the cfi to the list so that
730              lookup_cfa works later on.  */
731           list_head = &current_fde ()->dw_fde_cfi;
732         }
733       /* ??? If this is a CFI for the CIE, we don't emit.  This
734          assumes that the standard CIE contents that the assembler
735          uses matches the standard CIE contents that the compiler
736          uses.  This is probably a bad assumption.  I'm not quite
737          sure how to address this for now.  */
738     }
739   else if (label)
740     {
741       dw_fde_ref fde = current_fde ();
742
743       gcc_assert (fde != NULL);
744
745       if (*label == 0)
746         label = dwarf2out_cfi_label ();
747
748       if (fde->dw_fde_current_label == NULL
749           || strcmp (label, fde->dw_fde_current_label) != 0)
750         {
751           dw_cfi_ref xcfi;
752
753           label = xstrdup (label);
754
755           /* Set the location counter to the new label.  */
756           xcfi = new_cfi ();
757           /* If we have a current label, advance from there, otherwise
758              set the location directly using set_loc.  */
759           xcfi->dw_cfi_opc = fde->dw_fde_current_label
760                              ? DW_CFA_advance_loc4
761                              : DW_CFA_set_loc;
762           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
763           add_cfi (&fde->dw_fde_cfi, xcfi);
764
765           fde->dw_fde_current_label = label;
766         }
767
768       list_head = &fde->dw_fde_cfi;
769     }
770
771   add_cfi (list_head, cfi);
772 }
773
774 /* Subroutine of lookup_cfa.  */
775
776 static void
777 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
778 {
779   switch (cfi->dw_cfi_opc)
780     {
781     case DW_CFA_def_cfa_offset:
782     case DW_CFA_def_cfa_offset_sf:
783       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
784       break;
785     case DW_CFA_def_cfa_register:
786       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
787       break;
788     case DW_CFA_def_cfa:
789     case DW_CFA_def_cfa_sf:
790       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
791       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
792       break;
793     case DW_CFA_def_cfa_expression:
794       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
795       break;
796     default:
797       break;
798     }
799 }
800
801 /* Find the previous value for the CFA.  */
802
803 static void
804 lookup_cfa (dw_cfa_location *loc)
805 {
806   dw_cfi_ref cfi;
807   dw_fde_ref fde;
808
809   loc->reg = INVALID_REGNUM;
810   loc->offset = 0;
811   loc->indirect = 0;
812   loc->base_offset = 0;
813
814   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
815     lookup_cfa_1 (cfi, loc);
816
817   fde = current_fde ();
818   if (fde)
819     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
820       lookup_cfa_1 (cfi, loc);
821 }
822
823 /* The current rule for calculating the DWARF2 canonical frame address.  */
824 static dw_cfa_location cfa;
825
826 /* The register used for saving registers to the stack, and its offset
827    from the CFA.  */
828 static dw_cfa_location cfa_store;
829
830 /* The running total of the size of arguments pushed onto the stack.  */
831 static HOST_WIDE_INT args_size;
832
833 /* The last args_size we actually output.  */
834 static HOST_WIDE_INT old_args_size;
835
836 /* Entry point to update the canonical frame address (CFA).
837    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
838    calculated from REG+OFFSET.  */
839
840 void
841 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
842 {
843   dw_cfa_location loc;
844   loc.indirect = 0;
845   loc.base_offset = 0;
846   loc.reg = reg;
847   loc.offset = offset;
848   def_cfa_1 (label, &loc);
849 }
850
851 /* Determine if two dw_cfa_location structures define the same data.  */
852
853 static bool
854 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
855 {
856   return (loc1->reg == loc2->reg
857           && loc1->offset == loc2->offset
858           && loc1->indirect == loc2->indirect
859           && (loc1->indirect == 0
860               || loc1->base_offset == loc2->base_offset));
861 }
862
863 /* This routine does the actual work.  The CFA is now calculated from
864    the dw_cfa_location structure.  */
865
866 static void
867 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
868 {
869   dw_cfi_ref cfi;
870   dw_cfa_location old_cfa, loc;
871
872   cfa = *loc_p;
873   loc = *loc_p;
874
875   if (cfa_store.reg == loc.reg && loc.indirect == 0)
876     cfa_store.offset = loc.offset;
877
878   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
879   lookup_cfa (&old_cfa);
880
881   /* If nothing changed, no need to issue any call frame instructions.  */
882   if (cfa_equal_p (&loc, &old_cfa))
883     return;
884
885   cfi = new_cfi ();
886
887   if (loc.reg == old_cfa.reg && !loc.indirect)
888     {
889       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
890          the CFA register did not change but the offset did.  The data 
891          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
892          in the assembler via the .cfi_def_cfa_offset directive.  */
893       if (loc.offset < 0)
894         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
895       else
896         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
897       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
898     }
899
900 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
901   else if (loc.offset == old_cfa.offset
902            && old_cfa.reg != INVALID_REGNUM
903            && !loc.indirect)
904     {
905       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
906          indicating the CFA register has changed to <register> but the
907          offset has not changed.  */
908       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
909       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
910     }
911 #endif
912
913   else if (loc.indirect == 0)
914     {
915       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
916          indicating the CFA register has changed to <register> with
917          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
918          happens in output_cfi, or in the assembler via the .cfi_def_cfa
919          directive.  */
920       if (loc.offset < 0)
921         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
922       else
923         cfi->dw_cfi_opc = DW_CFA_def_cfa;
924       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
925       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
926     }
927   else
928     {
929       /* Construct a DW_CFA_def_cfa_expression instruction to
930          calculate the CFA using a full location expression since no
931          register-offset pair is available.  */
932       struct dw_loc_descr_struct *loc_list;
933
934       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
935       loc_list = build_cfa_loc (&loc, 0);
936       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
937     }
938
939   add_fde_cfi (label, cfi);
940 }
941
942 /* Add the CFI for saving a register.  REG is the CFA column number.
943    LABEL is passed to add_fde_cfi.
944    If SREG is -1, the register is saved at OFFSET from the CFA;
945    otherwise it is saved in SREG.  */
946
947 static void
948 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
949 {
950   dw_cfi_ref cfi = new_cfi ();
951   dw_fde_ref fde = current_fde ();
952
953   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
954
955   /* When stack is aligned, store REG using DW_CFA_expression with
956      FP.  */
957   if (fde
958       && fde->stack_realign
959       && sreg == INVALID_REGNUM)
960     {
961       cfi->dw_cfi_opc = DW_CFA_expression;
962       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
963       cfi->dw_cfi_oprnd1.dw_cfi_loc
964         = build_cfa_aligned_loc (offset, fde->stack_realignment);
965     }
966   else if (sreg == INVALID_REGNUM)
967     {
968       if (offset < 0)
969         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
970       else if (reg & ~0x3f)
971         cfi->dw_cfi_opc = DW_CFA_offset_extended;
972       else
973         cfi->dw_cfi_opc = DW_CFA_offset;
974       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
975     }
976   else if (sreg == reg)
977     cfi->dw_cfi_opc = DW_CFA_same_value;
978   else
979     {
980       cfi->dw_cfi_opc = DW_CFA_register;
981       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
982     }
983
984   add_fde_cfi (label, cfi);
985 }
986
987 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
988    This CFI tells the unwinder that it needs to restore the window registers
989    from the previous frame's window save area.
990
991    ??? Perhaps we should note in the CIE where windows are saved (instead of
992    assuming 0(cfa)) and what registers are in the window.  */
993
994 void
995 dwarf2out_window_save (const char *label)
996 {
997   dw_cfi_ref cfi = new_cfi ();
998
999   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1000   add_fde_cfi (label, cfi);
1001 }
1002
1003 /* Add a CFI to update the running total of the size of arguments
1004    pushed onto the stack.  */
1005
1006 void
1007 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1008 {
1009   dw_cfi_ref cfi;
1010
1011   if (size == old_args_size)
1012     return;
1013
1014   old_args_size = size;
1015
1016   cfi = new_cfi ();
1017   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1018   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1019   add_fde_cfi (label, cfi);
1020 }
1021
1022 /* Entry point for saving a register to the stack.  REG is the GCC register
1023    number.  LABEL and OFFSET are passed to reg_save.  */
1024
1025 void
1026 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1027 {
1028   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1029 }
1030
1031 /* Entry point for saving the return address in the stack.
1032    LABEL and OFFSET are passed to reg_save.  */
1033
1034 void
1035 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1036 {
1037   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1038 }
1039
1040 /* Entry point for saving the return address in a register.
1041    LABEL and SREG are passed to reg_save.  */
1042
1043 void
1044 dwarf2out_return_reg (const char *label, unsigned int sreg)
1045 {
1046   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1047 }
1048
1049 #ifdef DWARF2_UNWIND_INFO
1050 /* Record the initial position of the return address.  RTL is
1051    INCOMING_RETURN_ADDR_RTX.  */
1052
1053 static void
1054 initial_return_save (rtx rtl)
1055 {
1056   unsigned int reg = INVALID_REGNUM;
1057   HOST_WIDE_INT offset = 0;
1058
1059   switch (GET_CODE (rtl))
1060     {
1061     case REG:
1062       /* RA is in a register.  */
1063       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1064       break;
1065
1066     case MEM:
1067       /* RA is on the stack.  */
1068       rtl = XEXP (rtl, 0);
1069       switch (GET_CODE (rtl))
1070         {
1071         case REG:
1072           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1073           offset = 0;
1074           break;
1075
1076         case PLUS:
1077           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1078           offset = INTVAL (XEXP (rtl, 1));
1079           break;
1080
1081         case MINUS:
1082           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1083           offset = -INTVAL (XEXP (rtl, 1));
1084           break;
1085
1086         default:
1087           gcc_unreachable ();
1088         }
1089
1090       break;
1091
1092     case PLUS:
1093       /* The return address is at some offset from any value we can
1094          actually load.  For instance, on the SPARC it is in %i7+8. Just
1095          ignore the offset for now; it doesn't matter for unwinding frames.  */
1096       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1097       initial_return_save (XEXP (rtl, 0));
1098       return;
1099
1100     default:
1101       gcc_unreachable ();
1102     }
1103
1104   if (reg != DWARF_FRAME_RETURN_COLUMN)
1105     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1106 }
1107 #endif
1108
1109 /* Given a SET, calculate the amount of stack adjustment it
1110    contains.  */
1111
1112 static HOST_WIDE_INT
1113 stack_adjust_offset (const_rtx pattern)
1114 {
1115   const_rtx src = SET_SRC (pattern);
1116   const_rtx dest = SET_DEST (pattern);
1117   HOST_WIDE_INT offset = 0;
1118   enum rtx_code code;
1119
1120   if (dest == stack_pointer_rtx)
1121     {
1122       /* (set (reg sp) (plus (reg sp) (const_int))) */
1123       code = GET_CODE (src);
1124       if (! (code == PLUS || code == MINUS)
1125           || XEXP (src, 0) != stack_pointer_rtx
1126           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1127         return 0;
1128
1129       offset = INTVAL (XEXP (src, 1));
1130       if (code == PLUS)
1131         offset = -offset;
1132     }
1133   else if (MEM_P (dest))
1134     {
1135       /* (set (mem (pre_dec (reg sp))) (foo)) */
1136       src = XEXP (dest, 0);
1137       code = GET_CODE (src);
1138
1139       switch (code)
1140         {
1141         case PRE_MODIFY:
1142         case POST_MODIFY:
1143           if (XEXP (src, 0) == stack_pointer_rtx)
1144             {
1145               rtx val = XEXP (XEXP (src, 1), 1);
1146               /* We handle only adjustments by constant amount.  */
1147               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1148                           && GET_CODE (val) == CONST_INT);
1149               offset = -INTVAL (val);
1150               break;
1151             }
1152           return 0;
1153
1154         case PRE_DEC:
1155         case POST_DEC:
1156           if (XEXP (src, 0) == stack_pointer_rtx)
1157             {
1158               offset = GET_MODE_SIZE (GET_MODE (dest));
1159               break;
1160             }
1161           return 0;
1162
1163         case PRE_INC:
1164         case POST_INC:
1165           if (XEXP (src, 0) == stack_pointer_rtx)
1166             {
1167               offset = -GET_MODE_SIZE (GET_MODE (dest));
1168               break;
1169             }
1170           return 0;
1171
1172         default:
1173           return 0;
1174         }
1175     }
1176   else
1177     return 0;
1178
1179   return offset;
1180 }
1181
1182 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1183    indexed by INSN_UID.  */
1184
1185 static HOST_WIDE_INT *barrier_args_size;
1186
1187 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1188
1189 static HOST_WIDE_INT
1190 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1191                              VEC (rtx, heap) **next)
1192 {
1193   HOST_WIDE_INT offset = 0;
1194   int i;
1195
1196   if (! RTX_FRAME_RELATED_P (insn))
1197     {
1198       if (prologue_epilogue_contains (insn)
1199           || sibcall_epilogue_contains (insn))
1200         /* Nothing */;
1201       else if (GET_CODE (PATTERN (insn)) == SET)
1202         offset = stack_adjust_offset (PATTERN (insn));
1203       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1204                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1205         {
1206           /* There may be stack adjustments inside compound insns.  Search
1207              for them.  */
1208           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1209             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1210               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1211         }
1212     }
1213   else
1214     {
1215       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1216
1217       if (expr)
1218         {
1219           expr = XEXP (expr, 0);
1220           if (GET_CODE (expr) == PARALLEL
1221               || GET_CODE (expr) == SEQUENCE)
1222             for (i = 1; i < XVECLEN (expr, 0); i++)
1223               {
1224                 rtx elem = XVECEXP (expr, 0, i);
1225
1226                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1227                   offset += stack_adjust_offset (elem);
1228               }
1229         }
1230     }
1231
1232 #ifndef STACK_GROWS_DOWNWARD
1233   offset = -offset;
1234 #endif
1235
1236   cur_args_size += offset;
1237   if (cur_args_size < 0)
1238     cur_args_size = 0;
1239
1240   if (JUMP_P (insn))
1241     {
1242       rtx dest = JUMP_LABEL (insn);
1243
1244       if (dest)
1245         {
1246           if (barrier_args_size [INSN_UID (dest)] < 0)
1247             {
1248               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1249               VEC_safe_push (rtx, heap, *next, dest);
1250             }
1251         }
1252     }
1253
1254   return cur_args_size;
1255 }
1256
1257 /* Walk the whole function and compute args_size on BARRIERs.  */
1258
1259 static void
1260 compute_barrier_args_size (void)
1261 {
1262   int max_uid = get_max_uid (), i;
1263   rtx insn;
1264   VEC (rtx, heap) *worklist, *next, *tmp;
1265
1266   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1267   for (i = 0; i < max_uid; i++)
1268     barrier_args_size[i] = -1;
1269
1270   worklist = VEC_alloc (rtx, heap, 20);
1271   next = VEC_alloc (rtx, heap, 20);
1272   insn = get_insns ();
1273   barrier_args_size[INSN_UID (insn)] = 0;
1274   VEC_quick_push (rtx, worklist, insn);
1275   for (;;)
1276     {
1277       while (!VEC_empty (rtx, worklist))
1278         {
1279           rtx prev, body, first_insn;
1280           HOST_WIDE_INT cur_args_size;
1281
1282           first_insn = insn = VEC_pop (rtx, worklist);
1283           cur_args_size = barrier_args_size[INSN_UID (insn)];
1284           prev = prev_nonnote_insn (insn);
1285           if (prev && BARRIER_P (prev))
1286             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1287
1288           for (; insn; insn = NEXT_INSN (insn))
1289             {
1290               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1291                 continue;
1292               if (BARRIER_P (insn))
1293                 break;
1294
1295               if (LABEL_P (insn))
1296                 {
1297                   if (insn == first_insn)
1298                     continue;
1299                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1300                     {
1301                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1302                       continue;
1303                     }
1304                   else
1305                     {
1306                       /* The insns starting with this label have been
1307                          already scanned or are in the worklist.  */
1308                       break;
1309                     }
1310                 }
1311
1312               body = PATTERN (insn);
1313               if (GET_CODE (body) == SEQUENCE)
1314                 {
1315                   for (i = 1; i < XVECLEN (body, 0); i++)
1316                     cur_args_size
1317                       = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1318                                                      cur_args_size, &next);
1319                   cur_args_size
1320                     = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1321                                                    cur_args_size, &next);
1322                 }
1323               else
1324                 cur_args_size
1325                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1326             }
1327         }
1328
1329       if (VEC_empty (rtx, next))
1330         break;
1331
1332       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1333       tmp = next;
1334       next = worklist;
1335       worklist = tmp;
1336       VEC_truncate (rtx, next, 0);
1337     }
1338
1339   VEC_free (rtx, heap, worklist);
1340   VEC_free (rtx, heap, next);
1341 }
1342
1343
1344 /* Check INSN to see if it looks like a push or a stack adjustment, and
1345    make a note of it if it does.  EH uses this information to find out how
1346    much extra space it needs to pop off the stack.  */
1347
1348 static void
1349 dwarf2out_stack_adjust (rtx insn, bool after_p)
1350 {
1351   HOST_WIDE_INT offset;
1352   const char *label;
1353   int i;
1354
1355   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1356      with this function.  Proper support would require all frame-related
1357      insns to be marked, and to be able to handle saving state around
1358      epilogues textually in the middle of the function.  */
1359   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1360     return;
1361
1362   /* If only calls can throw, and we have a frame pointer,
1363      save up adjustments until we see the CALL_INSN.  */
1364   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1365     {
1366       if (CALL_P (insn) && !after_p)
1367         {
1368           /* Extract the size of the args from the CALL rtx itself.  */
1369           insn = PATTERN (insn);
1370           if (GET_CODE (insn) == PARALLEL)
1371             insn = XVECEXP (insn, 0, 0);
1372           if (GET_CODE (insn) == SET)
1373             insn = SET_SRC (insn);
1374           gcc_assert (GET_CODE (insn) == CALL);
1375           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1376         }
1377       return;
1378     }
1379
1380   if (CALL_P (insn) && !after_p)
1381     {
1382       if (!flag_asynchronous_unwind_tables)
1383         dwarf2out_args_size ("", args_size);
1384       return;
1385     }
1386   else if (BARRIER_P (insn))
1387     {
1388       /* Don't call compute_barrier_args_size () if the only
1389          BARRIER is at the end of function.  */
1390       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1391         compute_barrier_args_size ();
1392       if (barrier_args_size == NULL)
1393         offset = 0;
1394       else
1395         {
1396           offset = barrier_args_size[INSN_UID (insn)];
1397           if (offset < 0)
1398             offset = 0;
1399         }
1400
1401       offset -= args_size;
1402 #ifndef STACK_GROWS_DOWNWARD
1403       offset = -offset;
1404 #endif
1405     }
1406   else if (GET_CODE (PATTERN (insn)) == SET)
1407     offset = stack_adjust_offset (PATTERN (insn));
1408   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1409            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1410     {
1411       /* There may be stack adjustments inside compound insns.  Search
1412          for them.  */
1413       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1414         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1415           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1416     }
1417   else
1418     return;
1419
1420   if (offset == 0)
1421     return;
1422
1423   label = dwarf2out_cfi_label ();
1424   dwarf2out_args_size_adjust (offset, label);
1425 }
1426
1427 /* Adjust args_size based on stack adjustment OFFSET.  */
1428
1429 static void
1430 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1431 {
1432   if (cfa.reg == STACK_POINTER_REGNUM)
1433     cfa.offset += offset;
1434
1435   if (cfa_store.reg == STACK_POINTER_REGNUM)
1436     cfa_store.offset += offset;
1437
1438 #ifndef STACK_GROWS_DOWNWARD
1439   offset = -offset;
1440 #endif
1441
1442   args_size += offset;
1443   if (args_size < 0)
1444     args_size = 0;
1445
1446   def_cfa_1 (label, &cfa);
1447   if (flag_asynchronous_unwind_tables)
1448     dwarf2out_args_size (label, args_size);
1449 }
1450
1451 #endif
1452
1453 /* We delay emitting a register save until either (a) we reach the end
1454    of the prologue or (b) the register is clobbered.  This clusters
1455    register saves so that there are fewer pc advances.  */
1456
1457 struct queued_reg_save GTY(())
1458 {
1459   struct queued_reg_save *next;
1460   rtx reg;
1461   HOST_WIDE_INT cfa_offset;
1462   rtx saved_reg;
1463 };
1464
1465 static GTY(()) struct queued_reg_save *queued_reg_saves;
1466
1467 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1468 struct reg_saved_in_data GTY(()) {
1469   rtx orig_reg;
1470   rtx saved_in_reg;
1471 };
1472
1473 /* A list of registers saved in other registers.
1474    The list intentionally has a small maximum capacity of 4; if your
1475    port needs more than that, you might consider implementing a
1476    more efficient data structure.  */
1477 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1478 static GTY(()) size_t num_regs_saved_in_regs;
1479
1480 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1481 static const char *last_reg_save_label;
1482
1483 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1484    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1485
1486 static void
1487 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1488 {
1489   struct queued_reg_save *q;
1490
1491   /* Duplicates waste space, but it's also necessary to remove them
1492      for correctness, since the queue gets output in reverse
1493      order.  */
1494   for (q = queued_reg_saves; q != NULL; q = q->next)
1495     if (REGNO (q->reg) == REGNO (reg))
1496       break;
1497
1498   if (q == NULL)
1499     {
1500       q = GGC_NEW (struct queued_reg_save);
1501       q->next = queued_reg_saves;
1502       queued_reg_saves = q;
1503     }
1504
1505   q->reg = reg;
1506   q->cfa_offset = offset;
1507   q->saved_reg = sreg;
1508
1509   last_reg_save_label = label;
1510 }
1511
1512 /* Output all the entries in QUEUED_REG_SAVES.  */
1513
1514 static void
1515 flush_queued_reg_saves (void)
1516 {
1517   struct queued_reg_save *q;
1518
1519   for (q = queued_reg_saves; q; q = q->next)
1520     {
1521       size_t i;
1522       unsigned int reg, sreg;
1523
1524       for (i = 0; i < num_regs_saved_in_regs; i++)
1525         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1526           break;
1527       if (q->saved_reg && i == num_regs_saved_in_regs)
1528         {
1529           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1530           num_regs_saved_in_regs++;
1531         }
1532       if (i != num_regs_saved_in_regs)
1533         {
1534           regs_saved_in_regs[i].orig_reg = q->reg;
1535           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1536         }
1537
1538       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1539       if (q->saved_reg)
1540         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1541       else
1542         sreg = INVALID_REGNUM;
1543       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1544     }
1545
1546   queued_reg_saves = NULL;
1547   last_reg_save_label = NULL;
1548 }
1549
1550 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1551    location for?  Or, does it clobber a register which we've previously
1552    said that some other register is saved in, and for which we now
1553    have a new location for?  */
1554
1555 static bool
1556 clobbers_queued_reg_save (const_rtx insn)
1557 {
1558   struct queued_reg_save *q;
1559
1560   for (q = queued_reg_saves; q; q = q->next)
1561     {
1562       size_t i;
1563       if (modified_in_p (q->reg, insn))
1564         return true;
1565       for (i = 0; i < num_regs_saved_in_regs; i++)
1566         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1567             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1568           return true;
1569     }
1570
1571   return false;
1572 }
1573
1574 /* Entry point for saving the first register into the second.  */
1575
1576 void
1577 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1578 {
1579   size_t i;
1580   unsigned int regno, sregno;
1581
1582   for (i = 0; i < num_regs_saved_in_regs; i++)
1583     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1584       break;
1585   if (i == num_regs_saved_in_regs)
1586     {
1587       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1588       num_regs_saved_in_regs++;
1589     }
1590   regs_saved_in_regs[i].orig_reg = reg;
1591   regs_saved_in_regs[i].saved_in_reg = sreg;
1592
1593   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1594   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1595   reg_save (label, regno, sregno, 0);
1596 }
1597
1598 /* What register, if any, is currently saved in REG?  */
1599
1600 static rtx
1601 reg_saved_in (rtx reg)
1602 {
1603   unsigned int regn = REGNO (reg);
1604   size_t i;
1605   struct queued_reg_save *q;
1606
1607   for (q = queued_reg_saves; q; q = q->next)
1608     if (q->saved_reg && regn == REGNO (q->saved_reg))
1609       return q->reg;
1610
1611   for (i = 0; i < num_regs_saved_in_regs; i++)
1612     if (regs_saved_in_regs[i].saved_in_reg
1613         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1614       return regs_saved_in_regs[i].orig_reg;
1615
1616   return NULL_RTX;
1617 }
1618
1619
1620 /* A temporary register holding an integral value used in adjusting SP
1621    or setting up the store_reg.  The "offset" field holds the integer
1622    value, not an offset.  */
1623 static dw_cfa_location cfa_temp;
1624
1625 /* Record call frame debugging information for an expression EXPR,
1626    which either sets SP or FP (adjusting how we calculate the frame
1627    address) or saves a register to the stack or another register.
1628    LABEL indicates the address of EXPR.
1629
1630    This function encodes a state machine mapping rtxes to actions on
1631    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1632    users need not read the source code.
1633
1634   The High-Level Picture
1635
1636   Changes in the register we use to calculate the CFA: Currently we
1637   assume that if you copy the CFA register into another register, we
1638   should take the other one as the new CFA register; this seems to
1639   work pretty well.  If it's wrong for some target, it's simple
1640   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1641
1642   Changes in the register we use for saving registers to the stack:
1643   This is usually SP, but not always.  Again, we deduce that if you
1644   copy SP into another register (and SP is not the CFA register),
1645   then the new register is the one we will be using for register
1646   saves.  This also seems to work.
1647
1648   Register saves: There's not much guesswork about this one; if
1649   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1650   register save, and the register used to calculate the destination
1651   had better be the one we think we're using for this purpose.
1652   It's also assumed that a copy from a call-saved register to another
1653   register is saving that register if RTX_FRAME_RELATED_P is set on
1654   that instruction.  If the copy is from a call-saved register to
1655   the *same* register, that means that the register is now the same
1656   value as in the caller.
1657
1658   Except: If the register being saved is the CFA register, and the
1659   offset is nonzero, we are saving the CFA, so we assume we have to
1660   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1661   the intent is to save the value of SP from the previous frame.
1662
1663   In addition, if a register has previously been saved to a different
1664   register,
1665
1666   Invariants / Summaries of Rules
1667
1668   cfa          current rule for calculating the CFA.  It usually
1669                consists of a register and an offset.
1670   cfa_store    register used by prologue code to save things to the stack
1671                cfa_store.offset is the offset from the value of
1672                cfa_store.reg to the actual CFA
1673   cfa_temp     register holding an integral value.  cfa_temp.offset
1674                stores the value, which will be used to adjust the
1675                stack pointer.  cfa_temp is also used like cfa_store,
1676                to track stores to the stack via fp or a temp reg.
1677
1678   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1679                with cfa.reg as the first operand changes the cfa.reg and its
1680                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1681                cfa_temp.offset.
1682
1683   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1684                expression yielding a constant.  This sets cfa_temp.reg
1685                and cfa_temp.offset.
1686
1687   Rule 5:      Create a new register cfa_store used to save items to the
1688                stack.
1689
1690   Rules 10-14: Save a register to the stack.  Define offset as the
1691                difference of the original location and cfa_store's
1692                location (or cfa_temp's location if cfa_temp is used).
1693
1694   Rules 16-20: If AND operation happens on sp in prologue, we assume
1695                stack is realigned.  We will use a group of DW_OP_XXX
1696                expressions to represent the location of the stored
1697                register instead of CFA+offset.
1698
1699   The Rules
1700
1701   "{a,b}" indicates a choice of a xor b.
1702   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1703
1704   Rule 1:
1705   (set <reg1> <reg2>:cfa.reg)
1706   effects: cfa.reg = <reg1>
1707            cfa.offset unchanged
1708            cfa_temp.reg = <reg1>
1709            cfa_temp.offset = cfa.offset
1710
1711   Rule 2:
1712   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1713                               {<const_int>,<reg>:cfa_temp.reg}))
1714   effects: cfa.reg = sp if fp used
1715            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1716            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1717              if cfa_store.reg==sp
1718
1719   Rule 3:
1720   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1721   effects: cfa.reg = fp
1722            cfa_offset += +/- <const_int>
1723
1724   Rule 4:
1725   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1726   constraints: <reg1> != fp
1727                <reg1> != sp
1728   effects: cfa.reg = <reg1>
1729            cfa_temp.reg = <reg1>
1730            cfa_temp.offset = cfa.offset
1731
1732   Rule 5:
1733   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1734   constraints: <reg1> != fp
1735                <reg1> != sp
1736   effects: cfa_store.reg = <reg1>
1737            cfa_store.offset = cfa.offset - cfa_temp.offset
1738
1739   Rule 6:
1740   (set <reg> <const_int>)
1741   effects: cfa_temp.reg = <reg>
1742            cfa_temp.offset = <const_int>
1743
1744   Rule 7:
1745   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1746   effects: cfa_temp.reg = <reg1>
1747            cfa_temp.offset |= <const_int>
1748
1749   Rule 8:
1750   (set <reg> (high <exp>))
1751   effects: none
1752
1753   Rule 9:
1754   (set <reg> (lo_sum <exp> <const_int>))
1755   effects: cfa_temp.reg = <reg>
1756            cfa_temp.offset = <const_int>
1757
1758   Rule 10:
1759   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1760   effects: cfa_store.offset -= <const_int>
1761            cfa.offset = cfa_store.offset if cfa.reg == sp
1762            cfa.reg = sp
1763            cfa.base_offset = -cfa_store.offset
1764
1765   Rule 11:
1766   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1767   effects: cfa_store.offset += -/+ mode_size(mem)
1768            cfa.offset = cfa_store.offset if cfa.reg == sp
1769            cfa.reg = sp
1770            cfa.base_offset = -cfa_store.offset
1771
1772   Rule 12:
1773   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1774
1775        <reg2>)
1776   effects: cfa.reg = <reg1>
1777            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1778
1779   Rule 13:
1780   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1781   effects: cfa.reg = <reg1>
1782            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1783
1784   Rule 14:
1785   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1786   effects: cfa.reg = <reg1>
1787            cfa.base_offset = -cfa_temp.offset
1788            cfa_temp.offset -= mode_size(mem)
1789
1790   Rule 15:
1791   (set <reg> {unspec, unspec_volatile})
1792   effects: target-dependent
1793
1794   Rule 16:
1795   (set sp (and: sp <const_int>))
1796   constraints: cfa_store.reg == sp
1797   effects: current_fde.stack_realign = 1
1798            cfa_store.offset = 0
1799            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
1800
1801   Rule 17:
1802   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
1803   effects: cfa_store.offset += -/+ mode_size(mem)
1804
1805   Rule 18:
1806   (set (mem ({pre_inc, pre_dec} sp)) fp)
1807   constraints: fde->stack_realign == 1
1808   effects: cfa_store.offset = 0
1809            cfa.reg != HARD_FRAME_POINTER_REGNUM
1810
1811   Rule 19:
1812   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
1813   constraints: fde->stack_realign == 1
1814                && cfa.offset == 0
1815                && cfa.indirect == 0
1816                && cfa.reg != HARD_FRAME_POINTER_REGNUM
1817   effects: Use DW_CFA_def_cfa_expression to define cfa
1818            cfa.reg == fde->drap_reg
1819
1820   Rule 20:
1821   (set reg fde->drap_reg)
1822   constraints: fde->vdrap_reg == INVALID_REGNUM
1823   effects: fde->vdrap_reg = reg.
1824   (set mem fde->drap_reg)
1825   constraints: fde->drap_reg_saved == 1
1826   effects: none.  */
1827
1828 static void
1829 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1830 {
1831   rtx src, dest, span;
1832   HOST_WIDE_INT offset;
1833   dw_fde_ref fde;
1834
1835   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1836      the PARALLEL independently. The first element is always processed if
1837      it is a SET. This is for backward compatibility.   Other elements
1838      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1839      flag is set in them.  */
1840   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1841     {
1842       int par_index;
1843       int limit = XVECLEN (expr, 0);
1844       rtx elem;
1845
1846       /* PARALLELs have strict read-modify-write semantics, so we
1847          ought to evaluate every rvalue before changing any lvalue.
1848          It's cumbersome to do that in general, but there's an
1849          easy approximation that is enough for all current users:
1850          handle register saves before register assignments.  */
1851       if (GET_CODE (expr) == PARALLEL)
1852         for (par_index = 0; par_index < limit; par_index++)
1853           {
1854             elem = XVECEXP (expr, 0, par_index);
1855             if (GET_CODE (elem) == SET
1856                 && MEM_P (SET_DEST (elem))
1857                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1858               dwarf2out_frame_debug_expr (elem, label);
1859           }
1860
1861       for (par_index = 0; par_index < limit; par_index++)
1862         {
1863           elem = XVECEXP (expr, 0, par_index);
1864           if (GET_CODE (elem) == SET
1865               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1866               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1867             dwarf2out_frame_debug_expr (elem, label);
1868           else if (GET_CODE (elem) == SET
1869                    && par_index != 0
1870                    && !RTX_FRAME_RELATED_P (elem))
1871             {
1872               /* Stack adjustment combining might combine some post-prologue
1873                  stack adjustment into a prologue stack adjustment.  */
1874               HOST_WIDE_INT offset = stack_adjust_offset (elem);
1875
1876               if (offset != 0)
1877                 dwarf2out_args_size_adjust (offset, label);
1878             }
1879         }
1880       return;
1881     }
1882
1883   gcc_assert (GET_CODE (expr) == SET);
1884
1885   src = SET_SRC (expr);
1886   dest = SET_DEST (expr);
1887
1888   if (REG_P (src))
1889     {
1890       rtx rsi = reg_saved_in (src);
1891       if (rsi)
1892         src = rsi;
1893     }
1894
1895   fde = current_fde ();
1896
1897   if (GET_CODE (src) == REG
1898       && fde
1899       && fde->drap_reg == REGNO (src)
1900       && (fde->drap_reg_saved
1901           || GET_CODE (dest) == REG))
1902     {
1903       /* Rule 20 */
1904       /* If we are saving dynamic realign argument pointer to a
1905          register, the destination is virtual dynamic realign
1906          argument pointer.  It may be used to access argument.  */
1907       if (GET_CODE (dest) == REG)
1908         {
1909           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
1910           fde->vdrap_reg = REGNO (dest);
1911         }
1912       return;
1913     }
1914
1915   switch (GET_CODE (dest))
1916     {
1917     case REG:
1918       switch (GET_CODE (src))
1919         {
1920           /* Setting FP from SP.  */
1921         case REG:
1922           if (cfa.reg == (unsigned) REGNO (src))
1923             {
1924               /* Rule 1 */
1925               /* Update the CFA rule wrt SP or FP.  Make sure src is
1926                  relative to the current CFA register.
1927
1928                  We used to require that dest be either SP or FP, but the
1929                  ARM copies SP to a temporary register, and from there to
1930                  FP.  So we just rely on the backends to only set
1931                  RTX_FRAME_RELATED_P on appropriate insns.  */
1932               cfa.reg = REGNO (dest);
1933               cfa_temp.reg = cfa.reg;
1934               cfa_temp.offset = cfa.offset;
1935             }
1936           else
1937             {
1938               /* Saving a register in a register.  */
1939               gcc_assert (!fixed_regs [REGNO (dest)]
1940                           /* For the SPARC and its register window.  */
1941                           || (DWARF_FRAME_REGNUM (REGNO (src))
1942                               == DWARF_FRAME_RETURN_COLUMN));
1943
1944               /* After stack is aligned, we can only save SP in FP
1945                  if drap register is used.  In this case, we have
1946                  to restore stack pointer with the CFA value and we
1947                  don't generate this DWARF information.  */
1948               if (fde
1949                   && fde->stack_realign
1950                   && REGNO (src) == STACK_POINTER_REGNUM)
1951                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
1952                             && fde->drap_reg != INVALID_REGNUM
1953                             && cfa.reg != REGNO (src));
1954               else
1955                 queue_reg_save (label, src, dest, 0);
1956             }
1957           break;
1958
1959         case PLUS:
1960         case MINUS:
1961         case LO_SUM:
1962           if (dest == stack_pointer_rtx)
1963             {
1964               /* Rule 2 */
1965               /* Adjusting SP.  */
1966               switch (GET_CODE (XEXP (src, 1)))
1967                 {
1968                 case CONST_INT:
1969                   offset = INTVAL (XEXP (src, 1));
1970                   break;
1971                 case REG:
1972                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1973                               == cfa_temp.reg);
1974                   offset = cfa_temp.offset;
1975                   break;
1976                 default:
1977                   gcc_unreachable ();
1978                 }
1979
1980               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1981                 {
1982                   /* Restoring SP from FP in the epilogue.  */
1983                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1984                   cfa.reg = STACK_POINTER_REGNUM;
1985                 }
1986               else if (GET_CODE (src) == LO_SUM)
1987                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1988                 ;
1989               else
1990                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1991
1992               if (GET_CODE (src) != MINUS)
1993                 offset = -offset;
1994               if (cfa.reg == STACK_POINTER_REGNUM)
1995                 cfa.offset += offset;
1996               if (cfa_store.reg == STACK_POINTER_REGNUM)
1997                 cfa_store.offset += offset;
1998             }
1999           else if (dest == hard_frame_pointer_rtx)
2000             {
2001               /* Rule 3 */
2002               /* Either setting the FP from an offset of the SP,
2003                  or adjusting the FP */
2004               gcc_assert (frame_pointer_needed);
2005
2006               gcc_assert (REG_P (XEXP (src, 0))
2007                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2008                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2009               offset = INTVAL (XEXP (src, 1));
2010               if (GET_CODE (src) != MINUS)
2011                 offset = -offset;
2012               cfa.offset += offset;
2013               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2014             }
2015           else
2016             {
2017               gcc_assert (GET_CODE (src) != MINUS);
2018
2019               /* Rule 4 */
2020               if (REG_P (XEXP (src, 0))
2021                   && REGNO (XEXP (src, 0)) == cfa.reg
2022                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2023                 {
2024                   /* Setting a temporary CFA register that will be copied
2025                      into the FP later on.  */
2026                   offset = - INTVAL (XEXP (src, 1));
2027                   cfa.offset += offset;
2028                   cfa.reg = REGNO (dest);
2029                   /* Or used to save regs to the stack.  */
2030                   cfa_temp.reg = cfa.reg;
2031                   cfa_temp.offset = cfa.offset;
2032                 }
2033
2034               /* Rule 5 */
2035               else if (REG_P (XEXP (src, 0))
2036                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2037                        && XEXP (src, 1) == stack_pointer_rtx)
2038                 {
2039                   /* Setting a scratch register that we will use instead
2040                      of SP for saving registers to the stack.  */
2041                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2042                   cfa_store.reg = REGNO (dest);
2043                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2044                 }
2045
2046               /* Rule 9 */
2047               else if (GET_CODE (src) == LO_SUM
2048                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2049                 {
2050                   cfa_temp.reg = REGNO (dest);
2051                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2052                 }
2053               else
2054                 gcc_unreachable ();
2055             }
2056           break;
2057
2058           /* Rule 6 */
2059         case CONST_INT:
2060           cfa_temp.reg = REGNO (dest);
2061           cfa_temp.offset = INTVAL (src);
2062           break;
2063
2064           /* Rule 7 */
2065         case IOR:
2066           gcc_assert (REG_P (XEXP (src, 0))
2067                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2068                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2069
2070           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2071             cfa_temp.reg = REGNO (dest);
2072           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2073           break;
2074
2075           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2076              which will fill in all of the bits.  */
2077           /* Rule 8 */
2078         case HIGH:
2079           break;
2080
2081           /* Rule 15 */
2082         case UNSPEC:
2083         case UNSPEC_VOLATILE:
2084           gcc_assert (targetm.dwarf_handle_frame_unspec);
2085           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2086           return;
2087
2088           /* Rule 16 */
2089         case AND:
2090           /* If this AND operation happens on stack pointer in prologue,
2091              we assume the stack is realigned and we extract the
2092              alignment.  */
2093           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2094             {
2095               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2096               fde->stack_realign = 1;
2097               fde->stack_realignment = INTVAL (XEXP (src, 1));
2098               cfa_store.offset = 0;
2099
2100               if (cfa.reg != STACK_POINTER_REGNUM
2101                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2102                 fde->drap_reg = cfa.reg;
2103             }
2104           return;
2105
2106         default:
2107           gcc_unreachable ();
2108         }
2109
2110       def_cfa_1 (label, &cfa);
2111       break;
2112
2113     case MEM:
2114
2115       /* Saving a register to the stack.  Make sure dest is relative to the
2116          CFA register.  */
2117       switch (GET_CODE (XEXP (dest, 0)))
2118         {
2119           /* Rule 10 */
2120           /* With a push.  */
2121         case PRE_MODIFY:
2122           /* We can't handle variable size modifications.  */
2123           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2124                       == CONST_INT);
2125           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2126
2127           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2128                       && cfa_store.reg == STACK_POINTER_REGNUM);
2129
2130           cfa_store.offset += offset;
2131           if (cfa.reg == STACK_POINTER_REGNUM)
2132             cfa.offset = cfa_store.offset;
2133
2134           offset = -cfa_store.offset;
2135           break;
2136
2137           /* Rule 11 */
2138         case PRE_INC:
2139         case PRE_DEC:
2140           offset = GET_MODE_SIZE (GET_MODE (dest));
2141           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2142             offset = -offset;
2143
2144           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2145                        == STACK_POINTER_REGNUM)
2146                       && cfa_store.reg == STACK_POINTER_REGNUM);
2147
2148           cfa_store.offset += offset;
2149
2150           /* Rule 18: If stack is aligned, we will use FP as a
2151              reference to represent the address of the stored
2152              regiser.  */
2153           if (fde
2154               && fde->stack_realign
2155               && src == hard_frame_pointer_rtx)
2156             {
2157               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2158               cfa_store.offset = 0;
2159             }
2160
2161           if (cfa.reg == STACK_POINTER_REGNUM)
2162             cfa.offset = cfa_store.offset;
2163
2164           offset = -cfa_store.offset;
2165           break;
2166
2167           /* Rule 12 */
2168           /* With an offset.  */
2169         case PLUS:
2170         case MINUS:
2171         case LO_SUM:
2172           {
2173             int regno;
2174
2175             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2176                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2177             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2178             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2179               offset = -offset;
2180
2181             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2182
2183             if (cfa_store.reg == (unsigned) regno)
2184               offset -= cfa_store.offset;
2185             else
2186               {
2187                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2188                 offset -= cfa_temp.offset;
2189               }
2190           }
2191           break;
2192
2193           /* Rule 13 */
2194           /* Without an offset.  */
2195         case REG:
2196           {
2197             int regno = REGNO (XEXP (dest, 0));
2198
2199             if (cfa_store.reg == (unsigned) regno)
2200               offset = -cfa_store.offset;
2201             else
2202               {
2203                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2204                 offset = -cfa_temp.offset;
2205               }
2206           }
2207           break;
2208
2209           /* Rule 14 */
2210         case POST_INC:
2211           gcc_assert (cfa_temp.reg
2212                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2213           offset = -cfa_temp.offset;
2214           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2215           break;
2216
2217         default:
2218           gcc_unreachable ();
2219         }
2220
2221         /* Rule 17 */
2222         /* If the source operand of this MEM operation is not a
2223            register, basically the source is return address.  Here
2224            we only care how much stack grew and we don't save it.  */
2225       if (!REG_P (src))
2226         break;
2227
2228       if (REGNO (src) != STACK_POINTER_REGNUM
2229           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2230           && (unsigned) REGNO (src) == cfa.reg)
2231         {
2232           /* We're storing the current CFA reg into the stack.  */
2233
2234           if (cfa.offset == 0)
2235             {
2236               /* Rule 19 */
2237               /* If stack is aligned, putting CFA reg into stack means
2238                  we can no longer use reg + offset to represent CFA.
2239                  Here we use DW_CFA_def_cfa_expression instead.  The
2240                  result of this expression equals to the original CFA
2241                  value.  */
2242               if (fde
2243                   && fde->stack_realign
2244                   && cfa.indirect == 0
2245                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2246                 {
2247                   dw_cfa_location cfa_exp;
2248
2249                   gcc_assert (fde->drap_reg == cfa.reg);
2250
2251                   cfa_exp.indirect = 1;
2252                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2253                   cfa_exp.base_offset = offset;
2254                   cfa_exp.offset = 0;
2255
2256                   fde->drap_reg_saved = 1;
2257
2258                   def_cfa_1 (label, &cfa_exp);
2259                   break;
2260                 }
2261
2262               /* If the source register is exactly the CFA, assume
2263                  we're saving SP like any other register; this happens
2264                  on the ARM.  */
2265               def_cfa_1 (label, &cfa);
2266               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2267               break;
2268             }
2269           else
2270             {
2271               /* Otherwise, we'll need to look in the stack to
2272                  calculate the CFA.  */
2273               rtx x = XEXP (dest, 0);
2274
2275               if (!REG_P (x))
2276                 x = XEXP (x, 0);
2277               gcc_assert (REG_P (x));
2278
2279               cfa.reg = REGNO (x);
2280               cfa.base_offset = offset;
2281               cfa.indirect = 1;
2282               def_cfa_1 (label, &cfa);
2283               break;
2284             }
2285         }
2286
2287       def_cfa_1 (label, &cfa);
2288       {
2289         span = targetm.dwarf_register_span (src);
2290
2291         if (!span)
2292           queue_reg_save (label, src, NULL_RTX, offset);
2293         else
2294           {
2295             /* We have a PARALLEL describing where the contents of SRC
2296                live.  Queue register saves for each piece of the
2297                PARALLEL.  */
2298             int par_index;
2299             int limit;
2300             HOST_WIDE_INT span_offset = offset;
2301
2302             gcc_assert (GET_CODE (span) == PARALLEL);
2303
2304             limit = XVECLEN (span, 0);
2305             for (par_index = 0; par_index < limit; par_index++)
2306               {
2307                 rtx elem = XVECEXP (span, 0, par_index);
2308
2309                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2310                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2311               }
2312           }
2313       }
2314       break;
2315
2316     default:
2317       gcc_unreachable ();
2318     }
2319 }
2320
2321 /* Record call frame debugging information for INSN, which either
2322    sets SP or FP (adjusting how we calculate the frame address) or saves a
2323    register to the stack.  If INSN is NULL_RTX, initialize our state.
2324
2325    If AFTER_P is false, we're being called before the insn is emitted,
2326    otherwise after.  Call instructions get invoked twice.  */
2327
2328 void
2329 dwarf2out_frame_debug (rtx insn, bool after_p)
2330 {
2331   const char *label;
2332   rtx src;
2333
2334   if (insn == NULL_RTX)
2335     {
2336       size_t i;
2337
2338       /* Flush any queued register saves.  */
2339       flush_queued_reg_saves ();
2340
2341       /* Set up state for generating call frame debug info.  */
2342       lookup_cfa (&cfa);
2343       gcc_assert (cfa.reg
2344                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2345
2346       cfa.reg = STACK_POINTER_REGNUM;
2347       cfa_store = cfa;
2348       cfa_temp.reg = -1;
2349       cfa_temp.offset = 0;
2350
2351       for (i = 0; i < num_regs_saved_in_regs; i++)
2352         {
2353           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2354           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2355         }
2356       num_regs_saved_in_regs = 0;
2357
2358       if (barrier_args_size)
2359         {
2360           XDELETEVEC (barrier_args_size);
2361           barrier_args_size = NULL;
2362         }
2363       return;
2364     }
2365
2366   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2367     flush_queued_reg_saves ();
2368
2369   if (! RTX_FRAME_RELATED_P (insn))
2370     {
2371       if (!ACCUMULATE_OUTGOING_ARGS)
2372         dwarf2out_stack_adjust (insn, after_p);
2373       return;
2374     }
2375
2376   label = dwarf2out_cfi_label ();
2377   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2378   if (src)
2379     insn = XEXP (src, 0);
2380   else
2381     insn = PATTERN (insn);
2382
2383   dwarf2out_frame_debug_expr (insn, label);
2384 }
2385
2386 #endif
2387
2388 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2389 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2390  (enum dwarf_call_frame_info cfi);
2391
2392 static enum dw_cfi_oprnd_type
2393 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2394 {
2395   switch (cfi)
2396     {
2397     case DW_CFA_nop:
2398     case DW_CFA_GNU_window_save:
2399       return dw_cfi_oprnd_unused;
2400
2401     case DW_CFA_set_loc:
2402     case DW_CFA_advance_loc1:
2403     case DW_CFA_advance_loc2:
2404     case DW_CFA_advance_loc4:
2405     case DW_CFA_MIPS_advance_loc8:
2406       return dw_cfi_oprnd_addr;
2407
2408     case DW_CFA_offset:
2409     case DW_CFA_offset_extended:
2410     case DW_CFA_def_cfa:
2411     case DW_CFA_offset_extended_sf:
2412     case DW_CFA_def_cfa_sf:
2413     case DW_CFA_restore_extended:
2414     case DW_CFA_undefined:
2415     case DW_CFA_same_value:
2416     case DW_CFA_def_cfa_register:
2417     case DW_CFA_register:
2418       return dw_cfi_oprnd_reg_num;
2419
2420     case DW_CFA_def_cfa_offset:
2421     case DW_CFA_GNU_args_size:
2422     case DW_CFA_def_cfa_offset_sf:
2423       return dw_cfi_oprnd_offset;
2424
2425     case DW_CFA_def_cfa_expression:
2426     case DW_CFA_expression:
2427       return dw_cfi_oprnd_loc;
2428
2429     default:
2430       gcc_unreachable ();
2431     }
2432 }
2433
2434 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2435 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2436  (enum dwarf_call_frame_info cfi);
2437
2438 static enum dw_cfi_oprnd_type
2439 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2440 {
2441   switch (cfi)
2442     {
2443     case DW_CFA_def_cfa:
2444     case DW_CFA_def_cfa_sf:
2445     case DW_CFA_offset:
2446     case DW_CFA_offset_extended_sf:
2447     case DW_CFA_offset_extended:
2448       return dw_cfi_oprnd_offset;
2449
2450     case DW_CFA_register:
2451       return dw_cfi_oprnd_reg_num;
2452
2453     default:
2454       return dw_cfi_oprnd_unused;
2455     }
2456 }
2457
2458 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2459
2460 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2461    switch to the data section instead, and write out a synthetic label
2462    for collect2.  */
2463
2464 static void
2465 switch_to_eh_frame_section (void)
2466 {
2467   tree label;
2468
2469 #ifdef EH_FRAME_SECTION_NAME
2470   if (eh_frame_section == 0)
2471     {
2472       int flags;
2473
2474       if (EH_TABLES_CAN_BE_READ_ONLY)
2475         {
2476           int fde_encoding;
2477           int per_encoding;
2478           int lsda_encoding;
2479
2480           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2481                                                        /*global=*/0);
2482           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2483                                                        /*global=*/1);
2484           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2485                                                         /*global=*/0);
2486           flags = ((! flag_pic
2487                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2488                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2489                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2490                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2491                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2492                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2493                    ? 0 : SECTION_WRITE);
2494         }
2495       else
2496         flags = SECTION_WRITE;
2497       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2498     }
2499 #endif
2500
2501   if (eh_frame_section)
2502     switch_to_section (eh_frame_section);
2503   else
2504     {
2505       /* We have no special eh_frame section.  Put the information in
2506          the data section and emit special labels to guide collect2.  */
2507       switch_to_section (data_section);
2508       label = get_file_function_name ("F");
2509       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2510       targetm.asm_out.globalize_label (asm_out_file,
2511                                        IDENTIFIER_POINTER (label));
2512       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2513     }
2514 }
2515
2516 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2517
2518 static HOST_WIDE_INT
2519 div_data_align (HOST_WIDE_INT off)
2520 {
2521   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2522   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2523   return r;
2524 }
2525
2526 /* Output a Call Frame Information opcode and its operand(s).  */
2527
2528 static void
2529 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2530 {
2531   unsigned long r;
2532   HOST_WIDE_INT off;
2533
2534   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2535     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2536                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2537                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2538                          ((unsigned HOST_WIDE_INT)
2539                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2540   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2541     {
2542       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2543       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2544                            "DW_CFA_offset, column 0x%lx", r);
2545       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2546       dw2_asm_output_data_uleb128 (off, NULL);
2547     }
2548   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2549     {
2550       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2551       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2552                            "DW_CFA_restore, column 0x%lx", r);
2553     }
2554   else
2555     {
2556       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2557                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2558
2559       switch (cfi->dw_cfi_opc)
2560         {
2561         case DW_CFA_set_loc:
2562           if (for_eh)
2563             dw2_asm_output_encoded_addr_rtx (
2564                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2565                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2566                 false, NULL);
2567           else
2568             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2569                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2570           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2571           break;
2572
2573         case DW_CFA_advance_loc1:
2574           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2575                                 fde->dw_fde_current_label, NULL);
2576           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2577           break;
2578
2579         case DW_CFA_advance_loc2:
2580           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2581                                 fde->dw_fde_current_label, NULL);
2582           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2583           break;
2584
2585         case DW_CFA_advance_loc4:
2586           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2587                                 fde->dw_fde_current_label, NULL);
2588           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2589           break;
2590
2591         case DW_CFA_MIPS_advance_loc8:
2592           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2593                                 fde->dw_fde_current_label, NULL);
2594           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2595           break;
2596
2597         case DW_CFA_offset_extended:
2598           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2599           dw2_asm_output_data_uleb128 (r, NULL);
2600           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2601           dw2_asm_output_data_uleb128 (off, NULL);
2602           break;
2603
2604         case DW_CFA_def_cfa:
2605           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2606           dw2_asm_output_data_uleb128 (r, NULL);
2607           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2608           break;
2609
2610         case DW_CFA_offset_extended_sf:
2611           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2612           dw2_asm_output_data_uleb128 (r, NULL);
2613           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2614           dw2_asm_output_data_sleb128 (off, NULL);
2615           break;
2616
2617         case DW_CFA_def_cfa_sf:
2618           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2619           dw2_asm_output_data_uleb128 (r, NULL);
2620           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2621           dw2_asm_output_data_sleb128 (off, NULL);
2622           break;
2623
2624         case DW_CFA_restore_extended:
2625         case DW_CFA_undefined:
2626         case DW_CFA_same_value:
2627         case DW_CFA_def_cfa_register:
2628           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2629           dw2_asm_output_data_uleb128 (r, NULL);
2630           break;
2631
2632         case DW_CFA_register:
2633           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2634           dw2_asm_output_data_uleb128 (r, NULL);
2635           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2636           dw2_asm_output_data_uleb128 (r, NULL);
2637           break;
2638
2639         case DW_CFA_def_cfa_offset:
2640         case DW_CFA_GNU_args_size:
2641           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2642           break;
2643
2644         case DW_CFA_def_cfa_offset_sf:
2645           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2646           dw2_asm_output_data_sleb128 (off, NULL);
2647           break;
2648
2649         case DW_CFA_GNU_window_save:
2650           break;
2651
2652         case DW_CFA_def_cfa_expression:
2653         case DW_CFA_expression:
2654           output_cfa_loc (cfi);
2655           break;
2656
2657         case DW_CFA_GNU_negative_offset_extended:
2658           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2659           gcc_unreachable ();
2660
2661         default:
2662           break;
2663         }
2664     }
2665 }
2666
2667 /* Similar, but do it via assembler directives instead.  */
2668
2669 static void
2670 output_cfi_directive (dw_cfi_ref cfi)
2671 {
2672   unsigned long r, r2;
2673
2674   switch (cfi->dw_cfi_opc)
2675     {
2676     case DW_CFA_advance_loc:
2677     case DW_CFA_advance_loc1:
2678     case DW_CFA_advance_loc2:
2679     case DW_CFA_advance_loc4:
2680     case DW_CFA_MIPS_advance_loc8:
2681     case DW_CFA_set_loc:
2682       /* Should only be created by add_fde_cfi in a code path not
2683          followed when emitting via directives.  The assembler is
2684          going to take care of this for us.  */
2685       gcc_unreachable ();
2686
2687     case DW_CFA_offset:
2688     case DW_CFA_offset_extended:
2689     case DW_CFA_offset_extended_sf:
2690       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2691       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2692                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2693       break;
2694
2695     case DW_CFA_restore:
2696     case DW_CFA_restore_extended:
2697       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2698       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
2699       break;
2700
2701     case DW_CFA_undefined:
2702       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2703       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
2704       break;
2705
2706     case DW_CFA_same_value:
2707       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2708       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
2709       break;
2710
2711     case DW_CFA_def_cfa:
2712     case DW_CFA_def_cfa_sf:
2713       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2714       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
2715                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
2716       break;
2717
2718     case DW_CFA_def_cfa_register:
2719       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2720       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
2721       break;
2722
2723     case DW_CFA_register:
2724       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
2725       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
2726       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
2727       break;
2728
2729     case DW_CFA_def_cfa_offset:
2730     case DW_CFA_def_cfa_offset_sf:
2731       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
2732                HOST_WIDE_INT_PRINT_DEC"\n",
2733                cfi->dw_cfi_oprnd1.dw_cfi_offset);
2734       break;
2735
2736     case DW_CFA_GNU_args_size:
2737       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
2738       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2739       if (flag_debug_asm)
2740         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
2741                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
2742       fputc ('\n', asm_out_file);
2743       break;
2744
2745     case DW_CFA_GNU_window_save:
2746       fprintf (asm_out_file, "\t.cfi_window_save\n");
2747       break;
2748
2749     case DW_CFA_def_cfa_expression:
2750     case DW_CFA_expression:
2751       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
2752       output_cfa_loc_raw (cfi);
2753       fputc ('\n', asm_out_file);
2754       break;
2755
2756     default:
2757       gcc_unreachable ();
2758     }
2759 }
2760
2761 /* Output the call frame information used to record information
2762    that relates to calculating the frame pointer, and records the
2763    location of saved registers.  */
2764
2765 static void
2766 output_call_frame_info (int for_eh)
2767 {
2768   unsigned int i;
2769   dw_fde_ref fde;
2770   dw_cfi_ref cfi;
2771   char l1[20], l2[20], section_start_label[20];
2772   bool any_lsda_needed = false;
2773   char augmentation[6];
2774   int augmentation_size;
2775   int fde_encoding = DW_EH_PE_absptr;
2776   int per_encoding = DW_EH_PE_absptr;
2777   int lsda_encoding = DW_EH_PE_absptr;
2778   int return_reg;
2779
2780   /* Don't emit a CIE if there won't be any FDEs.  */
2781   if (fde_table_in_use == 0)
2782     return;
2783
2784   /* Nothing to do if the assembler's doing it all.  */
2785   if (dwarf2out_do_cfi_asm ())
2786     return;
2787
2788   /* If we make FDEs linkonce, we may have to emit an empty label for
2789      an FDE that wouldn't otherwise be emitted.  We want to avoid
2790      having an FDE kept around when the function it refers to is
2791      discarded.  Example where this matters: a primary function
2792      template in C++ requires EH information, but an explicit
2793      specialization doesn't.  */
2794   if (TARGET_USES_WEAK_UNWIND_INFO
2795       && ! flag_asynchronous_unwind_tables
2796       && flag_exceptions
2797       && for_eh)
2798     for (i = 0; i < fde_table_in_use; i++)
2799       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2800           && !fde_table[i].uses_eh_lsda
2801           && ! DECL_WEAK (fde_table[i].decl))
2802         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2803                                       for_eh, /* empty */ 1);
2804
2805   /* If we don't have any functions we'll want to unwind out of, don't
2806      emit any EH unwind information.  Note that if exceptions aren't
2807      enabled, we won't have collected nothrow information, and if we
2808      asked for asynchronous tables, we always want this info.  */
2809   if (for_eh)
2810     {
2811       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2812
2813       for (i = 0; i < fde_table_in_use; i++)
2814         if (fde_table[i].uses_eh_lsda)
2815           any_eh_needed = any_lsda_needed = true;
2816         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2817           any_eh_needed = true;
2818         else if (! fde_table[i].nothrow
2819                  && ! fde_table[i].all_throwers_are_sibcalls)
2820           any_eh_needed = true;
2821
2822       if (! any_eh_needed)
2823         return;
2824     }
2825
2826   /* We're going to be generating comments, so turn on app.  */
2827   if (flag_debug_asm)
2828     app_enable ();
2829
2830   if (for_eh)
2831     switch_to_eh_frame_section ();
2832   else
2833     {
2834       if (!debug_frame_section)
2835         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2836                                            SECTION_DEBUG, NULL);
2837       switch_to_section (debug_frame_section);
2838     }
2839
2840   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2841   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2842
2843   /* Output the CIE.  */
2844   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2845   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2846   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2847     dw2_asm_output_data (4, 0xffffffff,
2848       "Initial length escape value indicating 64-bit DWARF extension");
2849   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2850                         "Length of Common Information Entry");
2851   ASM_OUTPUT_LABEL (asm_out_file, l1);
2852
2853   /* Now that the CIE pointer is PC-relative for EH,
2854      use 0 to identify the CIE.  */
2855   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2856                        (for_eh ? 0 : DWARF_CIE_ID),
2857                        "CIE Identifier Tag");
2858
2859   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2860
2861   augmentation[0] = 0;
2862   augmentation_size = 0;
2863   if (for_eh)
2864     {
2865       char *p;
2866
2867       /* Augmentation:
2868          z      Indicates that a uleb128 is present to size the
2869                 augmentation section.
2870          L      Indicates the encoding (and thus presence) of
2871                 an LSDA pointer in the FDE augmentation.
2872          R      Indicates a non-default pointer encoding for
2873                 FDE code pointers.
2874          P      Indicates the presence of an encoding + language
2875                 personality routine in the CIE augmentation.  */
2876
2877       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2878       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2879       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2880
2881       p = augmentation + 1;
2882       if (eh_personality_libfunc)
2883         {
2884           *p++ = 'P';
2885           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2886           assemble_external_libcall (eh_personality_libfunc);
2887         }
2888       if (any_lsda_needed)
2889         {
2890           *p++ = 'L';
2891           augmentation_size += 1;
2892         }
2893       if (fde_encoding != DW_EH_PE_absptr)
2894         {
2895           *p++ = 'R';
2896           augmentation_size += 1;
2897         }
2898       if (p > augmentation + 1)
2899         {
2900           augmentation[0] = 'z';
2901           *p = '\0';
2902         }
2903
2904       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2905       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2906         {
2907           int offset = (  4             /* Length */
2908                         + 4             /* CIE Id */
2909                         + 1             /* CIE version */
2910                         + strlen (augmentation) + 1     /* Augmentation */
2911                         + size_of_uleb128 (1)           /* Code alignment */
2912                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2913                         + 1             /* RA column */
2914                         + 1             /* Augmentation size */
2915                         + 1             /* Personality encoding */ );
2916           int pad = -offset & (PTR_SIZE - 1);
2917
2918           augmentation_size += pad;
2919
2920           /* Augmentations should be small, so there's scarce need to
2921              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2922           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2923         }
2924     }
2925
2926   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2927   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2928   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2929                                "CIE Data Alignment Factor");
2930
2931   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2932   if (DW_CIE_VERSION == 1)
2933     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2934   else
2935     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2936
2937   if (augmentation[0])
2938     {
2939       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2940       if (eh_personality_libfunc)
2941         {
2942           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2943                                eh_data_format_name (per_encoding));
2944           dw2_asm_output_encoded_addr_rtx (per_encoding,
2945                                            eh_personality_libfunc,
2946                                            true, NULL);
2947         }
2948
2949       if (any_lsda_needed)
2950         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2951                              eh_data_format_name (lsda_encoding));
2952
2953       if (fde_encoding != DW_EH_PE_absptr)
2954         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2955                              eh_data_format_name (fde_encoding));
2956     }
2957
2958   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2959     output_cfi (cfi, NULL, for_eh);
2960
2961   /* Pad the CIE out to an address sized boundary.  */
2962   ASM_OUTPUT_ALIGN (asm_out_file,
2963                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2964   ASM_OUTPUT_LABEL (asm_out_file, l2);
2965
2966   /* Loop through all of the FDE's.  */
2967   for (i = 0; i < fde_table_in_use; i++)
2968     {
2969       fde = &fde_table[i];
2970
2971       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2972       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2973           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2974           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2975           && !fde->uses_eh_lsda)
2976         continue;
2977
2978       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2979       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2980       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2981       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2982       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2983         dw2_asm_output_data (4, 0xffffffff,
2984                              "Initial length escape value indicating 64-bit DWARF extension");
2985       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2986                             "FDE Length");
2987       ASM_OUTPUT_LABEL (asm_out_file, l1);
2988
2989       if (for_eh)
2990         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2991       else
2992         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2993                                debug_frame_section, "FDE CIE offset");
2994
2995       if (for_eh)
2996         {
2997           if (fde->dw_fde_switched_sections)
2998             {
2999               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3000                                       fde->dw_fde_unlikely_section_label);
3001               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3002                                       fde->dw_fde_hot_section_label);
3003               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3004               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3005               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3006                                                "FDE initial location");
3007               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3008                                     fde->dw_fde_hot_section_end_label,
3009                                     fde->dw_fde_hot_section_label,
3010                                     "FDE address range");
3011               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3012                                                "FDE initial location");
3013               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3014                                     fde->dw_fde_unlikely_section_end_label,
3015                                     fde->dw_fde_unlikely_section_label,
3016                                     "FDE address range");
3017             }
3018           else
3019             {
3020               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3021               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3022               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3023                                                sym_ref,
3024                                                false,
3025                                                "FDE initial location");
3026               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3027                                     fde->dw_fde_end, fde->dw_fde_begin,
3028                                     "FDE address range");
3029             }
3030         }
3031       else
3032         {
3033           if (fde->dw_fde_switched_sections)
3034             {
3035               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3036                                    fde->dw_fde_hot_section_label,
3037                                    "FDE initial location");
3038               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3039                                     fde->dw_fde_hot_section_end_label,
3040                                     fde->dw_fde_hot_section_label,
3041                                     "FDE address range");
3042               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3043                                    fde->dw_fde_unlikely_section_label,
3044                                    "FDE initial location");
3045               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3046                                     fde->dw_fde_unlikely_section_end_label,
3047                                     fde->dw_fde_unlikely_section_label,
3048                                     "FDE address range");
3049             }
3050           else
3051             {
3052               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3053                                    "FDE initial location");
3054               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3055                                     fde->dw_fde_end, fde->dw_fde_begin,
3056                                     "FDE address range");
3057             }
3058         }
3059
3060       if (augmentation[0])
3061         {
3062           if (any_lsda_needed)
3063             {
3064               int size = size_of_encoded_value (lsda_encoding);
3065
3066               if (lsda_encoding == DW_EH_PE_aligned)
3067                 {
3068                   int offset = (  4             /* Length */
3069                                 + 4             /* CIE offset */
3070                                 + 2 * size_of_encoded_value (fde_encoding)
3071                                 + 1             /* Augmentation size */ );
3072                   int pad = -offset & (PTR_SIZE - 1);
3073
3074                   size += pad;
3075                   gcc_assert (size_of_uleb128 (size) == 1);
3076                 }
3077
3078               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3079
3080               if (fde->uses_eh_lsda)
3081                 {
3082                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3083                                                fde->funcdef_number);
3084                   dw2_asm_output_encoded_addr_rtx (
3085                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3086                         false, "Language Specific Data Area");
3087                 }
3088               else
3089                 {
3090                   if (lsda_encoding == DW_EH_PE_aligned)
3091                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3092                   dw2_asm_output_data
3093                     (size_of_encoded_value (lsda_encoding), 0,
3094                      "Language Specific Data Area (none)");
3095                 }
3096             }
3097           else
3098             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3099         }
3100
3101       /* Loop through the Call Frame Instructions associated with
3102          this FDE.  */
3103       fde->dw_fde_current_label = fde->dw_fde_begin;
3104       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3105         output_cfi (cfi, fde, for_eh);
3106
3107       /* Pad the FDE out to an address sized boundary.  */
3108       ASM_OUTPUT_ALIGN (asm_out_file,
3109                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3110       ASM_OUTPUT_LABEL (asm_out_file, l2);
3111     }
3112
3113   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3114     dw2_asm_output_data (4, 0, "End of Table");
3115 #ifdef MIPS_DEBUGGING_INFO
3116   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3117      get a value of 0.  Putting .align 0 after the label fixes it.  */
3118   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3119 #endif
3120
3121   /* Turn off app to make assembly quicker.  */
3122   if (flag_debug_asm)
3123     app_disable ();
3124 }
3125
3126 /* Output a marker (i.e. a label) for the beginning of a function, before
3127    the prologue.  */
3128
3129 void
3130 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3131                           const char *file ATTRIBUTE_UNUSED)
3132 {
3133   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3134   char * dup_label;
3135   dw_fde_ref fde;
3136
3137   current_function_func_begin_label = NULL;
3138
3139 #ifdef TARGET_UNWIND_INFO
3140   /* ??? current_function_func_begin_label is also used by except.c
3141      for call-site information.  We must emit this label if it might
3142      be used.  */
3143   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3144       && ! dwarf2out_do_frame ())
3145     return;
3146 #else
3147   if (! dwarf2out_do_frame ())
3148     return;
3149 #endif
3150
3151   switch_to_section (function_section (current_function_decl));
3152   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3153                                current_function_funcdef_no);
3154   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3155                           current_function_funcdef_no);
3156   dup_label = xstrdup (label);
3157   current_function_func_begin_label = dup_label;
3158
3159 #ifdef TARGET_UNWIND_INFO
3160   /* We can elide the fde allocation if we're not emitting debug info.  */
3161   if (! dwarf2out_do_frame ())
3162     return;
3163 #endif
3164
3165   /* Expand the fde table if necessary.  */
3166   if (fde_table_in_use == fde_table_allocated)
3167     {
3168       fde_table_allocated += FDE_TABLE_INCREMENT;
3169       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3170       memset (fde_table + fde_table_in_use, 0,
3171               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3172     }
3173
3174   /* Record the FDE associated with this function.  */
3175   current_funcdef_fde = fde_table_in_use;
3176
3177   /* Add the new FDE at the end of the fde_table.  */
3178   fde = &fde_table[fde_table_in_use++];
3179   fde->decl = current_function_decl;
3180   fde->dw_fde_begin = dup_label;
3181   fde->dw_fde_current_label = dup_label;
3182   fde->dw_fde_hot_section_label = NULL;
3183   fde->dw_fde_hot_section_end_label = NULL;
3184   fde->dw_fde_unlikely_section_label = NULL;
3185   fde->dw_fde_unlikely_section_end_label = NULL;
3186   fde->dw_fde_switched_sections = false;
3187   fde->dw_fde_end = NULL;
3188   fde->dw_fde_cfi = NULL;
3189   fde->funcdef_number = current_function_funcdef_no;
3190   fde->nothrow = TREE_NOTHROW (current_function_decl);
3191   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3192   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3193   fde->drap_reg = INVALID_REGNUM;
3194   fde->vdrap_reg = INVALID_REGNUM;
3195
3196   args_size = old_args_size = 0;
3197
3198   /* We only want to output line number information for the genuine dwarf2
3199      prologue case, not the eh frame case.  */
3200 #ifdef DWARF2_DEBUGGING_INFO
3201   if (file)
3202     dwarf2out_source_line (line, file);
3203 #endif
3204
3205   if (dwarf2out_do_cfi_asm ())
3206     {
3207       int enc;
3208       rtx ref;
3209
3210       fprintf (asm_out_file, "\t.cfi_startproc\n");
3211
3212       if (eh_personality_libfunc)
3213         {
3214           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3215           ref = eh_personality_libfunc;
3216
3217           /* ??? The GAS support isn't entirely consistent.  We have to
3218              handle indirect support ourselves, but PC-relative is done
3219              in the assembler.  Further, the assembler can't handle any
3220              of the weirder relocation types.  */
3221           if (enc & DW_EH_PE_indirect)
3222             ref = dw2_force_const_mem (ref, true);
3223
3224           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3225           output_addr_const (asm_out_file, ref);
3226           fputc ('\n', asm_out_file);
3227         }
3228
3229       if (crtl->uses_eh_lsda)
3230         {
3231           char lab[20];
3232
3233           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3234           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3235                                        current_function_funcdef_no);
3236           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3237           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3238
3239           if (enc & DW_EH_PE_indirect)
3240             ref = dw2_force_const_mem (ref, true);
3241
3242           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3243           output_addr_const (asm_out_file, ref);
3244           fputc ('\n', asm_out_file);
3245         }
3246     }
3247 }
3248
3249 /* Output a marker (i.e. a label) for the absolute end of the generated code
3250    for a function definition.  This gets called *after* the epilogue code has
3251    been generated.  */
3252
3253 void
3254 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3255                         const char *file ATTRIBUTE_UNUSED)
3256 {
3257   dw_fde_ref fde;
3258   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3259
3260   if (dwarf2out_do_cfi_asm ())
3261     fprintf (asm_out_file, "\t.cfi_endproc\n");
3262
3263   /* Output a label to mark the endpoint of the code generated for this
3264      function.  */
3265   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3266                                current_function_funcdef_no);
3267   ASM_OUTPUT_LABEL (asm_out_file, label);
3268   fde = current_fde ();
3269   gcc_assert (fde != NULL);
3270   fde->dw_fde_end = xstrdup (label);
3271 }
3272
3273 void
3274 dwarf2out_frame_init (void)
3275 {
3276   /* Allocate the initial hunk of the fde_table.  */
3277   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3278   fde_table_allocated = FDE_TABLE_INCREMENT;
3279   fde_table_in_use = 0;
3280
3281   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3282      sake of lookup_cfa.  */
3283
3284   /* On entry, the Canonical Frame Address is at SP.  */
3285   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3286
3287 #ifdef DWARF2_UNWIND_INFO
3288   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3289     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3290 #endif
3291 }
3292
3293 void
3294 dwarf2out_frame_finish (void)
3295 {
3296   /* Output call frame information.  */
3297   if (DWARF2_FRAME_INFO)
3298     output_call_frame_info (0);
3299
3300 #ifndef TARGET_UNWIND_INFO
3301   /* Output another copy for the unwinder.  */
3302   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3303     output_call_frame_info (1);
3304 #endif
3305 }
3306
3307 /* Note that the current function section is being used for code.  */
3308
3309 static void
3310 dwarf2out_note_section_used (void)
3311 {
3312   section *sec = current_function_section ();
3313   if (sec == text_section)
3314     text_section_used = true;
3315   else if (sec == cold_text_section)
3316     cold_text_section_used = true;
3317 }
3318
3319 void
3320 dwarf2out_switch_text_section (void)
3321 {
3322   dw_fde_ref fde = current_fde ();
3323
3324   gcc_assert (cfun && fde);
3325
3326   fde->dw_fde_switched_sections = true;
3327   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3328   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3329   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3330   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3331   have_multiple_function_sections = true;
3332
3333   /* Reset the current label on switching text sections, so that we
3334      don't attempt to advance_loc4 between labels in different sections.  */
3335   fde->dw_fde_current_label = NULL;
3336
3337   /* There is no need to mark used sections when not debugging.  */
3338   if (cold_text_section != NULL)
3339     dwarf2out_note_section_used ();
3340 }
3341 #endif
3342 \f
3343 /* And now, the subset of the debugging information support code necessary
3344    for emitting location expressions.  */
3345
3346 /* Data about a single source file.  */
3347 struct dwarf_file_data GTY(())
3348 {
3349   const char * filename;
3350   int emitted_number;
3351 };
3352
3353 /* We need some way to distinguish DW_OP_addr with a direct symbol
3354    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3355 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3356
3357
3358 typedef struct dw_val_struct *dw_val_ref;
3359 typedef struct die_struct *dw_die_ref;
3360 typedef const struct die_struct *const_dw_die_ref;
3361 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3362 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3363
3364 /* Each DIE may have a series of attribute/value pairs.  Values
3365    can take on several forms.  The forms that are used in this
3366    implementation are listed below.  */
3367
3368 enum dw_val_class
3369 {
3370   dw_val_class_addr,
3371   dw_val_class_offset,
3372   dw_val_class_loc,
3373   dw_val_class_loc_list,
3374   dw_val_class_range_list,
3375   dw_val_class_const,
3376   dw_val_class_unsigned_const,
3377   dw_val_class_long_long,
3378   dw_val_class_vec,
3379   dw_val_class_flag,
3380   dw_val_class_die_ref,
3381   dw_val_class_fde_ref,
3382   dw_val_class_lbl_id,
3383   dw_val_class_lineptr,
3384   dw_val_class_str,
3385   dw_val_class_macptr,
3386   dw_val_class_file
3387 };
3388
3389 /* Describe a double word constant value.  */
3390 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3391
3392 typedef struct dw_long_long_struct GTY(())
3393 {
3394   unsigned long hi;
3395   unsigned long low;
3396 }
3397 dw_long_long_const;
3398
3399 /* Describe a floating point constant value, or a vector constant value.  */
3400
3401 typedef struct dw_vec_struct GTY(())
3402 {
3403   unsigned char * GTY((length ("%h.length"))) array;
3404   unsigned length;
3405   unsigned elt_size;
3406 }
3407 dw_vec_const;
3408
3409 /* The dw_val_node describes an attribute's value, as it is
3410    represented internally.  */
3411
3412 typedef struct dw_val_struct GTY(())
3413 {
3414   enum dw_val_class val_class;
3415   union dw_val_struct_union
3416     {
3417       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3418       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3419       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3420       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3421       HOST_WIDE_INT GTY ((default)) val_int;
3422       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3423       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3424       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3425       struct dw_val_die_union
3426         {
3427           dw_die_ref die;
3428           int external;
3429         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3430       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3431       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3432       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3433       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3434       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3435     }
3436   GTY ((desc ("%1.val_class"))) v;
3437 }
3438 dw_val_node;
3439
3440 /* Locations in memory are described using a sequence of stack machine
3441    operations.  */
3442
3443 typedef struct dw_loc_descr_struct GTY(())
3444 {
3445   dw_loc_descr_ref dw_loc_next;
3446   enum dwarf_location_atom dw_loc_opc;
3447   dw_val_node dw_loc_oprnd1;
3448   dw_val_node dw_loc_oprnd2;
3449   int dw_loc_addr;
3450 }
3451 dw_loc_descr_node;
3452
3453 /* Location lists are ranges + location descriptions for that range,
3454    so you can track variables that are in different places over
3455    their entire life.  */
3456 typedef struct dw_loc_list_struct GTY(())
3457 {
3458   dw_loc_list_ref dw_loc_next;
3459   const char *begin; /* Label for begin address of range */
3460   const char *end;  /* Label for end address of range */
3461   char *ll_symbol; /* Label for beginning of location list.
3462                       Only on head of list */
3463   const char *section; /* Section this loclist is relative to */
3464   dw_loc_descr_ref expr;
3465 } dw_loc_list_node;
3466
3467 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3468
3469 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3470
3471 /* Convert a DWARF stack opcode into its string name.  */
3472
3473 static const char *
3474 dwarf_stack_op_name (unsigned int op)
3475 {
3476   switch (op)
3477     {
3478     case DW_OP_addr:
3479     case INTERNAL_DW_OP_tls_addr:
3480       return "DW_OP_addr";
3481     case DW_OP_deref:
3482       return "DW_OP_deref";
3483     case DW_OP_const1u:
3484       return "DW_OP_const1u";
3485     case DW_OP_const1s:
3486       return "DW_OP_const1s";
3487     case DW_OP_const2u:
3488       return "DW_OP_const2u";
3489     case DW_OP_const2s:
3490       return "DW_OP_const2s";
3491     case DW_OP_const4u:
3492       return "DW_OP_const4u";
3493     case DW_OP_const4s:
3494       return "DW_OP_const4s";
3495     case DW_OP_const8u:
3496       return "DW_OP_const8u";
3497     case DW_OP_const8s:
3498       return "DW_OP_const8s";
3499     case DW_OP_constu:
3500       return "DW_OP_constu";
3501     case DW_OP_consts:
3502       return "DW_OP_consts";
3503     case DW_OP_dup:
3504       return "DW_OP_dup";
3505     case DW_OP_drop:
3506       return "DW_OP_drop";
3507     case DW_OP_over:
3508       return "DW_OP_over";
3509     case DW_OP_pick:
3510       return "DW_OP_pick";
3511     case DW_OP_swap:
3512       return "DW_OP_swap";
3513     case DW_OP_rot:
3514       return "DW_OP_rot";
3515     case DW_OP_xderef:
3516       return "DW_OP_xderef";
3517     case DW_OP_abs:
3518       return "DW_OP_abs";
3519     case DW_OP_and:
3520       return "DW_OP_and";
3521     case DW_OP_div:
3522       return "DW_OP_div";
3523     case DW_OP_minus:
3524       return "DW_OP_minus";
3525     case DW_OP_mod:
3526       return "DW_OP_mod";
3527     case DW_OP_mul:
3528       return "DW_OP_mul";
3529     case DW_OP_neg:
3530       return "DW_OP_neg";
3531     case DW_OP_not:
3532       return "DW_OP_not";
3533     case DW_OP_or:
3534       return "DW_OP_or";
3535     case DW_OP_plus:
3536       return "DW_OP_plus";
3537     case DW_OP_plus_uconst:
3538       return "DW_OP_plus_uconst";
3539     case DW_OP_shl:
3540       return "DW_OP_shl";
3541     case DW_OP_shr:
3542       return "DW_OP_shr";
3543     case DW_OP_shra:
3544       return "DW_OP_shra";
3545     case DW_OP_xor:
3546       return "DW_OP_xor";
3547     case DW_OP_bra:
3548       return "DW_OP_bra";
3549     case DW_OP_eq:
3550       return "DW_OP_eq";
3551     case DW_OP_ge:
3552       return "DW_OP_ge";
3553     case DW_OP_gt:
3554       return "DW_OP_gt";
3555     case DW_OP_le:
3556       return "DW_OP_le";
3557     case DW_OP_lt:
3558       return "DW_OP_lt";
3559     case DW_OP_ne:
3560       return "DW_OP_ne";
3561     case DW_OP_skip:
3562       return "DW_OP_skip";
3563     case DW_OP_lit0:
3564       return "DW_OP_lit0";
3565     case DW_OP_lit1:
3566       return "DW_OP_lit1";
3567     case DW_OP_lit2:
3568       return "DW_OP_lit2";
3569     case DW_OP_lit3:
3570       return "DW_OP_lit3";
3571     case DW_OP_lit4:
3572       return "DW_OP_lit4";
3573     case DW_OP_lit5:
3574       return "DW_OP_lit5";
3575     case DW_OP_lit6:
3576       return "DW_OP_lit6";
3577     case DW_OP_lit7:
3578       return "DW_OP_lit7";
3579     case DW_OP_lit8:
3580       return "DW_OP_lit8";
3581     case DW_OP_lit9:
3582       return "DW_OP_lit9";
3583     case DW_OP_lit10:
3584       return "DW_OP_lit10";
3585     case DW_OP_lit11:
3586       return "DW_OP_lit11";
3587     case DW_OP_lit12:
3588       return "DW_OP_lit12";
3589     case DW_OP_lit13:
3590       return "DW_OP_lit13";
3591     case DW_OP_lit14:
3592       return "DW_OP_lit14";
3593     case DW_OP_lit15:
3594       return "DW_OP_lit15";
3595     case DW_OP_lit16:
3596       return "DW_OP_lit16";
3597     case DW_OP_lit17:
3598       return "DW_OP_lit17";
3599     case DW_OP_lit18:
3600       return "DW_OP_lit18";
3601     case DW_OP_lit19:
3602       return "DW_OP_lit19";
3603     case DW_OP_lit20:
3604       return "DW_OP_lit20";
3605     case DW_OP_lit21:
3606       return "DW_OP_lit21";
3607     case DW_OP_lit22:
3608       return "DW_OP_lit22";
3609     case DW_OP_lit23:
3610       return "DW_OP_lit23";
3611     case DW_OP_lit24:
3612       return "DW_OP_lit24";
3613     case DW_OP_lit25:
3614       return "DW_OP_lit25";
3615     case DW_OP_lit26:
3616       return "DW_OP_lit26";
3617     case DW_OP_lit27:
3618       return "DW_OP_lit27";
3619     case DW_OP_lit28:
3620       return "DW_OP_lit28";
3621     case DW_OP_lit29:
3622       return "DW_OP_lit29";
3623     case DW_OP_lit30:
3624       return "DW_OP_lit30";
3625     case DW_OP_lit31:
3626       return "DW_OP_lit31";
3627     case DW_OP_reg0:
3628       return "DW_OP_reg0";
3629     case DW_OP_reg1:
3630       return "DW_OP_reg1";
3631     case DW_OP_reg2:
3632       return "DW_OP_reg2";
3633     case DW_OP_reg3:
3634       return "DW_OP_reg3";
3635     case DW_OP_reg4:
3636       return "DW_OP_reg4";
3637     case DW_OP_reg5:
3638       return "DW_OP_reg5";
3639     case DW_OP_reg6:
3640       return "DW_OP_reg6";
3641     case DW_OP_reg7:
3642       return "DW_OP_reg7";
3643     case DW_OP_reg8:
3644       return "DW_OP_reg8";
3645     case DW_OP_reg9:
3646       return "DW_OP_reg9";
3647     case DW_OP_reg10:
3648       return "DW_OP_reg10";
3649     case DW_OP_reg11:
3650       return "DW_OP_reg11";
3651     case DW_OP_reg12:
3652       return "DW_OP_reg12";
3653     case DW_OP_reg13:
3654       return "DW_OP_reg13";
3655     case DW_OP_reg14:
3656       return "DW_OP_reg14";
3657     case DW_OP_reg15:
3658       return "DW_OP_reg15";
3659     case DW_OP_reg16:
3660       return "DW_OP_reg16";
3661     case DW_OP_reg17:
3662       return "DW_OP_reg17";
3663     case DW_OP_reg18:
3664       return "DW_OP_reg18";
3665     case DW_OP_reg19:
3666       return "DW_OP_reg19";
3667     case DW_OP_reg20:
3668       return "DW_OP_reg20";
3669     case DW_OP_reg21:
3670       return "DW_OP_reg21";
3671     case DW_OP_reg22:
3672       return "DW_OP_reg22";
3673     case DW_OP_reg23:
3674       return "DW_OP_reg23";
3675     case DW_OP_reg24:
3676       return "DW_OP_reg24";
3677     case DW_OP_reg25:
3678       return "DW_OP_reg25";
3679     case DW_OP_reg26:
3680       return "DW_OP_reg26";
3681     case DW_OP_reg27:
3682       return "DW_OP_reg27";
3683     case DW_OP_reg28:
3684       return "DW_OP_reg28";
3685     case DW_OP_reg29:
3686       return "DW_OP_reg29";
3687     case DW_OP_reg30:
3688       return "DW_OP_reg30";
3689     case DW_OP_reg31:
3690       return "DW_OP_reg31";
3691     case DW_OP_breg0:
3692       return "DW_OP_breg0";
3693     case DW_OP_breg1:
3694       return "DW_OP_breg1";
3695     case DW_OP_breg2:
3696       return "DW_OP_breg2";
3697     case DW_OP_breg3:
3698       return "DW_OP_breg3";
3699     case DW_OP_breg4:
3700       return "DW_OP_breg4";
3701     case DW_OP_breg5:
3702       return "DW_OP_breg5";
3703     case DW_OP_breg6:
3704       return "DW_OP_breg6";
3705     case DW_OP_breg7:
3706       return "DW_OP_breg7";
3707     case DW_OP_breg8:
3708       return "DW_OP_breg8";
3709     case DW_OP_breg9:
3710       return "DW_OP_breg9";
3711     case DW_OP_breg10:
3712       return "DW_OP_breg10";
3713     case DW_OP_breg11:
3714       return "DW_OP_breg11";
3715     case DW_OP_breg12:
3716       return "DW_OP_breg12";
3717     case DW_OP_breg13:
3718       return "DW_OP_breg13";
3719     case DW_OP_breg14:
3720       return "DW_OP_breg14";
3721     case DW_OP_breg15:
3722       return "DW_OP_breg15";
3723     case DW_OP_breg16:
3724       return "DW_OP_breg16";
3725     case DW_OP_breg17:
3726       return "DW_OP_breg17";
3727     case DW_OP_breg18:
3728       return "DW_OP_breg18";
3729     case DW_OP_breg19:
3730       return "DW_OP_breg19";
3731     case DW_OP_breg20:
3732       return "DW_OP_breg20";
3733     case DW_OP_breg21:
3734       return "DW_OP_breg21";
3735     case DW_OP_breg22:
3736       return "DW_OP_breg22";
3737     case DW_OP_breg23:
3738       return "DW_OP_breg23";
3739     case DW_OP_breg24:
3740       return "DW_OP_breg24";
3741     case DW_OP_breg25:
3742       return "DW_OP_breg25";
3743     case DW_OP_breg26:
3744       return "DW_OP_breg26";
3745     case DW_OP_breg27:
3746       return "DW_OP_breg27";
3747     case DW_OP_breg28:
3748       return "DW_OP_breg28";
3749     case DW_OP_breg29:
3750       return "DW_OP_breg29";
3751     case DW_OP_breg30:
3752       return "DW_OP_breg30";
3753     case DW_OP_breg31:
3754       return "DW_OP_breg31";
3755     case DW_OP_regx:
3756       return "DW_OP_regx";
3757     case DW_OP_fbreg:
3758       return "DW_OP_fbreg";
3759     case DW_OP_bregx:
3760       return "DW_OP_bregx";
3761     case DW_OP_piece:
3762       return "DW_OP_piece";
3763     case DW_OP_deref_size:
3764       return "DW_OP_deref_size";
3765     case DW_OP_xderef_size:
3766       return "DW_OP_xderef_size";
3767     case DW_OP_nop:
3768       return "DW_OP_nop";
3769     case DW_OP_push_object_address:
3770       return "DW_OP_push_object_address";
3771     case DW_OP_call2:
3772       return "DW_OP_call2";
3773     case DW_OP_call4:
3774       return "DW_OP_call4";
3775     case DW_OP_call_ref:
3776       return "DW_OP_call_ref";
3777     case DW_OP_GNU_push_tls_address:
3778       return "DW_OP_GNU_push_tls_address";
3779     case DW_OP_GNU_uninit:
3780       return "DW_OP_GNU_uninit";
3781     default:
3782       return "OP_<unknown>";
3783     }
3784 }
3785
3786 /* Return a pointer to a newly allocated location description.  Location
3787    descriptions are simple expression terms that can be strung
3788    together to form more complicated location (address) descriptions.  */
3789
3790 static inline dw_loc_descr_ref
3791 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3792                unsigned HOST_WIDE_INT oprnd2)
3793 {
3794   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3795
3796   descr->dw_loc_opc = op;
3797   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3798   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3799   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3800   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3801
3802   return descr;
3803 }
3804
3805 /* Return a pointer to a newly allocated location description for
3806    REG and OFFSET.  */
3807
3808 static inline dw_loc_descr_ref
3809 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
3810 {
3811   if (offset)
3812     {
3813       if (reg <= 31)
3814         return new_loc_descr (DW_OP_breg0 + reg, offset, 0);
3815       else
3816         return new_loc_descr (DW_OP_bregx, reg, offset);
3817     }
3818   else if (reg <= 31)
3819     return new_loc_descr (DW_OP_reg0 + reg, 0, 0);
3820   else
3821    return new_loc_descr (DW_OP_regx, reg, 0);
3822 }
3823
3824 /* Add a location description term to a location description expression.  */
3825
3826 static inline void
3827 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3828 {
3829   dw_loc_descr_ref *d;
3830
3831   /* Find the end of the chain.  */
3832   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3833     ;
3834
3835   *d = descr;
3836 }
3837
3838 /* Return the size of a location descriptor.  */
3839
3840 static unsigned long
3841 size_of_loc_descr (dw_loc_descr_ref loc)
3842 {
3843   unsigned long size = 1;
3844
3845   switch (loc->dw_loc_opc)
3846     {
3847     case DW_OP_addr:
3848     case INTERNAL_DW_OP_tls_addr:
3849       size += DWARF2_ADDR_SIZE;
3850       break;
3851     case DW_OP_const1u:
3852     case DW_OP_const1s:
3853       size += 1;
3854       break;
3855     case DW_OP_const2u:
3856     case DW_OP_const2s:
3857       size += 2;
3858       break;
3859     case DW_OP_const4u:
3860     case DW_OP_const4s:
3861       size += 4;
3862       break;
3863     case DW_OP_const8u:
3864     case DW_OP_const8s:
3865       size += 8;
3866       break;
3867     case DW_OP_constu:
3868       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3869       break;
3870     case DW_OP_consts:
3871       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3872       break;
3873     case DW_OP_pick:
3874       size += 1;
3875       break;
3876     case DW_OP_plus_uconst:
3877       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3878       break;
3879     case DW_OP_skip:
3880     case DW_OP_bra:
3881       size += 2;
3882       break;
3883     case DW_OP_breg0:
3884     case DW_OP_breg1:
3885     case DW_OP_breg2:
3886     case DW_OP_breg3:
3887     case DW_OP_breg4:
3888     case DW_OP_breg5:
3889     case DW_OP_breg6:
3890     case DW_OP_breg7:
3891     case DW_OP_breg8:
3892     case DW_OP_breg9:
3893     case DW_OP_breg10:
3894     case DW_OP_breg11:
3895     case DW_OP_breg12:
3896     case DW_OP_breg13:
3897     case DW_OP_breg14:
3898     case DW_OP_breg15:
3899     case DW_OP_breg16:
3900     case DW_OP_breg17:
3901     case DW_OP_breg18:
3902     case DW_OP_breg19:
3903     case DW_OP_breg20:
3904     case DW_OP_breg21:
3905     case DW_OP_breg22:
3906     case DW_OP_breg23:
3907     case DW_OP_breg24:
3908     case DW_OP_breg25:
3909     case DW_OP_breg26:
3910     case DW_OP_breg27:
3911     case DW_OP_breg28:
3912     case DW_OP_breg29:
3913     case DW_OP_breg30:
3914     case DW_OP_breg31:
3915       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3916       break;
3917     case DW_OP_regx:
3918       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3919       break;
3920     case DW_OP_fbreg:
3921       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3922       break;
3923     case DW_OP_bregx:
3924       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3925       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3926       break;
3927     case DW_OP_piece:
3928       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3929       break;
3930     case DW_OP_deref_size:
3931     case DW_OP_xderef_size:
3932       size += 1;
3933       break;
3934     case DW_OP_call2:
3935       size += 2;
3936       break;
3937     case DW_OP_call4:
3938       size += 4;
3939       break;
3940     case DW_OP_call_ref:
3941       size += DWARF2_ADDR_SIZE;
3942       break;
3943     default:
3944       break;
3945     }
3946
3947   return size;
3948 }
3949
3950 /* Return the size of a series of location descriptors.  */
3951
3952 static unsigned long
3953 size_of_locs (dw_loc_descr_ref loc)
3954 {
3955   dw_loc_descr_ref l;
3956   unsigned long size;
3957
3958   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3959      field, to avoid writing to a PCH file.  */
3960   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3961     {
3962       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3963         break;
3964       size += size_of_loc_descr (l);
3965     }
3966   if (! l)
3967     return size;
3968
3969   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3970     {
3971       l->dw_loc_addr = size;
3972       size += size_of_loc_descr (l);
3973     }
3974
3975   return size;
3976 }
3977
3978 /* Output location description stack opcode's operands (if any).  */
3979
3980 static void
3981 output_loc_operands (dw_loc_descr_ref loc)
3982 {
3983   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3984   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3985
3986   switch (loc->dw_loc_opc)
3987     {
3988 #ifdef DWARF2_DEBUGGING_INFO
3989     case DW_OP_addr:
3990       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3991       break;
3992     case DW_OP_const2u:
3993     case DW_OP_const2s:
3994       dw2_asm_output_data (2, val1->v.val_int, NULL);
3995       break;
3996     case DW_OP_const4u:
3997     case DW_OP_const4s:
3998       dw2_asm_output_data (4, val1->v.val_int, NULL);
3999       break;
4000     case DW_OP_const8u:
4001     case DW_OP_const8s:
4002       gcc_assert (HOST_BITS_PER_LONG >= 64);
4003       dw2_asm_output_data (8, val1->v.val_int, NULL);
4004       break;
4005     case DW_OP_skip:
4006     case DW_OP_bra:
4007       {
4008         int offset;
4009
4010         gcc_assert (val1->val_class == dw_val_class_loc);
4011         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4012
4013         dw2_asm_output_data (2, offset, NULL);
4014       }
4015       break;
4016 #else
4017     case DW_OP_addr:
4018     case DW_OP_const2u:
4019     case DW_OP_const2s:
4020     case DW_OP_const4u:
4021     case DW_OP_const4s:
4022     case DW_OP_const8u:
4023     case DW_OP_const8s:
4024     case DW_OP_skip:
4025     case DW_OP_bra:
4026       /* We currently don't make any attempt to make sure these are
4027          aligned properly like we do for the main unwind info, so
4028          don't support emitting things larger than a byte if we're
4029          only doing unwinding.  */
4030       gcc_unreachable ();
4031 #endif
4032     case DW_OP_const1u:
4033     case DW_OP_const1s:
4034       dw2_asm_output_data (1, val1->v.val_int, NULL);
4035       break;
4036     case DW_OP_constu:
4037       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4038       break;
4039     case DW_OP_consts:
4040       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4041       break;
4042     case DW_OP_pick:
4043       dw2_asm_output_data (1, val1->v.val_int, NULL);
4044       break;
4045     case DW_OP_plus_uconst:
4046       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4047       break;
4048     case DW_OP_breg0:
4049     case DW_OP_breg1:
4050     case DW_OP_breg2:
4051     case DW_OP_breg3:
4052     case DW_OP_breg4:
4053     case DW_OP_breg5:
4054     case DW_OP_breg6:
4055     case DW_OP_breg7:
4056     case DW_OP_breg8:
4057     case DW_OP_breg9:
4058     case DW_OP_breg10:
4059     case DW_OP_breg11:
4060     case DW_OP_breg12:
4061     case DW_OP_breg13:
4062     case DW_OP_breg14:
4063     case DW_OP_breg15:
4064     case DW_OP_breg16:
4065     case DW_OP_breg17:
4066     case DW_OP_breg18:
4067     case DW_OP_breg19:
4068     case DW_OP_breg20:
4069     case DW_OP_breg21:
4070     case DW_OP_breg22:
4071     case DW_OP_breg23:
4072     case DW_OP_breg24:
4073     case DW_OP_breg25:
4074     case DW_OP_breg26:
4075     case DW_OP_breg27:
4076     case DW_OP_breg28:
4077     case DW_OP_breg29:
4078     case DW_OP_breg30:
4079     case DW_OP_breg31:
4080       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4081       break;
4082     case DW_OP_regx:
4083       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4084       break;
4085     case DW_OP_fbreg:
4086       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4087       break;
4088     case DW_OP_bregx:
4089       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4090       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4091       break;
4092     case DW_OP_piece:
4093       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4094       break;
4095     case DW_OP_deref_size:
4096     case DW_OP_xderef_size:
4097       dw2_asm_output_data (1, val1->v.val_int, NULL);
4098       break;
4099
4100     case INTERNAL_DW_OP_tls_addr:
4101       if (targetm.asm_out.output_dwarf_dtprel)
4102         {
4103           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4104                                                DWARF2_ADDR_SIZE,
4105                                                val1->v.val_addr);
4106           fputc ('\n', asm_out_file);
4107         }
4108       else
4109         gcc_unreachable ();
4110       break;
4111
4112     default:
4113       /* Other codes have no operands.  */
4114       break;
4115     }
4116 }
4117
4118 /* Output a sequence of location operations.  */
4119
4120 static void
4121 output_loc_sequence (dw_loc_descr_ref loc)
4122 {
4123   for (; loc != NULL; loc = loc->dw_loc_next)
4124     {
4125       /* Output the opcode.  */
4126       dw2_asm_output_data (1, loc->dw_loc_opc,
4127                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4128
4129       /* Output the operand(s) (if any).  */
4130       output_loc_operands (loc);
4131     }
4132 }
4133
4134 /* Output location description stack opcode's operands (if any).
4135    The output is single bytes on a line, suitable for .cfi_escape.  */
4136
4137 static void
4138 output_loc_operands_raw (dw_loc_descr_ref loc)
4139 {
4140   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4141   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4142
4143   switch (loc->dw_loc_opc)
4144     {
4145     case DW_OP_addr:
4146       /* We cannot output addresses in .cfi_escape, only bytes.  */
4147       gcc_unreachable ();
4148
4149     case DW_OP_const1u:
4150     case DW_OP_const1s:
4151     case DW_OP_pick:
4152     case DW_OP_deref_size:
4153     case DW_OP_xderef_size:
4154       fputc (',', asm_out_file);
4155       dw2_asm_output_data_raw (1, val1->v.val_int);
4156       break;
4157
4158     case DW_OP_const2u:
4159     case DW_OP_const2s:
4160       fputc (',', asm_out_file);
4161       dw2_asm_output_data_raw (2, val1->v.val_int);
4162       break;
4163
4164     case DW_OP_const4u:
4165     case DW_OP_const4s:
4166       fputc (',', asm_out_file);
4167       dw2_asm_output_data_raw (4, val1->v.val_int);
4168       break;
4169
4170     case DW_OP_const8u:
4171     case DW_OP_const8s:
4172       gcc_assert (HOST_BITS_PER_LONG >= 64);
4173       fputc (',', asm_out_file);
4174       dw2_asm_output_data_raw (8, val1->v.val_int);
4175       break;
4176
4177     case DW_OP_skip:
4178     case DW_OP_bra:
4179       {
4180         int offset;
4181
4182         gcc_assert (val1->val_class == dw_val_class_loc);
4183         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4184
4185         fputc (',', asm_out_file);
4186         dw2_asm_output_data_raw (2, offset);
4187       }
4188       break;
4189
4190     case DW_OP_constu:
4191     case DW_OP_plus_uconst:
4192     case DW_OP_regx:
4193     case DW_OP_piece:
4194       fputc (',', asm_out_file);
4195       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4196       break;
4197
4198     case DW_OP_consts:
4199     case DW_OP_breg0:
4200     case DW_OP_breg1:
4201     case DW_OP_breg2:
4202     case DW_OP_breg3:
4203     case DW_OP_breg4:
4204     case DW_OP_breg5:
4205     case DW_OP_breg6:
4206     case DW_OP_breg7:
4207     case DW_OP_breg8:
4208     case DW_OP_breg9:
4209     case DW_OP_breg10:
4210     case DW_OP_breg11:
4211     case DW_OP_breg12:
4212     case DW_OP_breg13:
4213     case DW_OP_breg14:
4214     case DW_OP_breg15:
4215     case DW_OP_breg16:
4216     case DW_OP_breg17:
4217     case DW_OP_breg18:
4218     case DW_OP_breg19:
4219     case DW_OP_breg20:
4220     case DW_OP_breg21:
4221     case DW_OP_breg22:
4222     case DW_OP_breg23:
4223     case DW_OP_breg24:
4224     case DW_OP_breg25:
4225     case DW_OP_breg26:
4226     case DW_OP_breg27:
4227     case DW_OP_breg28:
4228     case DW_OP_breg29:
4229     case DW_OP_breg30:
4230     case DW_OP_breg31:
4231     case DW_OP_fbreg:
4232       fputc (',', asm_out_file);
4233       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4234       break;
4235
4236     case DW_OP_bregx:
4237       fputc (',', asm_out_file);
4238       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4239       fputc (',', asm_out_file);
4240       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4241       break;
4242
4243     case INTERNAL_DW_OP_tls_addr:
4244       gcc_unreachable ();
4245
4246     default:
4247       /* Other codes have no operands.  */
4248       break;
4249     }
4250 }
4251
4252 static void
4253 output_loc_sequence_raw (dw_loc_descr_ref loc)
4254 {
4255   while (1)
4256     {
4257       /* Output the opcode.  */
4258       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4259       output_loc_operands_raw (loc);
4260
4261       if (!loc->dw_loc_next)
4262         break;
4263       loc = loc->dw_loc_next;
4264
4265       fputc (',', asm_out_file);
4266     }
4267 }
4268
4269 /* This routine will generate the correct assembly data for a location
4270    description based on a cfi entry with a complex address.  */
4271
4272 static void
4273 output_cfa_loc (dw_cfi_ref cfi)
4274 {
4275   dw_loc_descr_ref loc;
4276   unsigned long size;
4277
4278   if (cfi->dw_cfi_opc == DW_CFA_expression)
4279     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4280
4281   /* Output the size of the block.  */
4282   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4283   size = size_of_locs (loc);
4284   dw2_asm_output_data_uleb128 (size, NULL);
4285
4286   /* Now output the operations themselves.  */
4287   output_loc_sequence (loc);
4288 }
4289
4290 /* Similar, but used for .cfi_escape.  */
4291
4292 static void
4293 output_cfa_loc_raw (dw_cfi_ref cfi)
4294 {
4295   dw_loc_descr_ref loc;
4296   unsigned long size;
4297
4298   if (cfi->dw_cfi_opc == DW_CFA_expression)
4299     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4300
4301   /* Output the size of the block.  */
4302   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4303   size = size_of_locs (loc);
4304   dw2_asm_output_data_uleb128_raw (size);
4305   fputc (',', asm_out_file);
4306
4307   /* Now output the operations themselves.  */
4308   output_loc_sequence_raw (loc);
4309 }
4310
4311 /* This function builds a dwarf location descriptor sequence from a
4312    dw_cfa_location, adding the given OFFSET to the result of the
4313    expression.  */
4314
4315 static struct dw_loc_descr_struct *
4316 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4317 {
4318   struct dw_loc_descr_struct *head, *tmp;
4319
4320   offset += cfa->offset;
4321
4322   if (cfa->indirect)
4323     {
4324       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4325       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4326       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4327       add_loc_descr (&head, tmp);
4328       if (offset != 0)
4329         {
4330           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4331           add_loc_descr (&head, tmp);
4332         }
4333     }
4334   else
4335     head = new_reg_loc_descr (cfa->reg, offset);
4336
4337   return head;
4338 }
4339
4340 /* This function builds a dwarf location descriptor sequence for
4341    the address at OFFSET from the CFA when stack is aligned to
4342    ALIGNMENT byte.  */
4343
4344 static struct dw_loc_descr_struct *
4345 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4346 {
4347   struct dw_loc_descr_struct *head;
4348   unsigned int dwarf_fp
4349     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4350
4351  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4352   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4353     {
4354       head = new_reg_loc_descr (dwarf_fp, 0);
4355       add_loc_descr (&head, int_loc_descriptor (alignment));
4356       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4357
4358       add_loc_descr (&head, int_loc_descriptor (offset));
4359       add_loc_descr (&head, new_loc_descr (DW_OP_plus, 0, 0));
4360     }
4361   else
4362     head = new_reg_loc_descr (dwarf_fp, offset);
4363   return head;
4364 }
4365
4366 /* This function fills in aa dw_cfa_location structure from a dwarf location
4367    descriptor sequence.  */
4368
4369 static void
4370 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4371 {
4372   struct dw_loc_descr_struct *ptr;
4373   cfa->offset = 0;
4374   cfa->base_offset = 0;
4375   cfa->indirect = 0;
4376   cfa->reg = -1;
4377
4378   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4379     {
4380       enum dwarf_location_atom op = ptr->dw_loc_opc;
4381
4382       switch (op)
4383         {
4384         case DW_OP_reg0:
4385         case DW_OP_reg1:
4386         case DW_OP_reg2:
4387         case DW_OP_reg3:
4388         case DW_OP_reg4:
4389         case DW_OP_reg5:
4390         case DW_OP_reg6:
4391         case DW_OP_reg7:
4392         case DW_OP_reg8:
4393         case DW_OP_reg9:
4394         case DW_OP_reg10:
4395         case DW_OP_reg11:
4396         case DW_OP_reg12:
4397         case DW_OP_reg13:
4398         case DW_OP_reg14:
4399         case DW_OP_reg15:
4400         case DW_OP_reg16:
4401         case DW_OP_reg17:
4402         case DW_OP_reg18:
4403         case DW_OP_reg19:
4404         case DW_OP_reg20:
4405         case DW_OP_reg21:
4406         case DW_OP_reg22:
4407         case DW_OP_reg23:
4408         case DW_OP_reg24:
4409         case DW_OP_reg25:
4410         case DW_OP_reg26:
4411         case DW_OP_reg27:
4412         case DW_OP_reg28:
4413         case DW_OP_reg29:
4414         case DW_OP_reg30:
4415         case DW_OP_reg31:
4416           cfa->reg = op - DW_OP_reg0;
4417           break;
4418         case DW_OP_regx:
4419           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4420           break;
4421         case DW_OP_breg0:
4422         case DW_OP_breg1:
4423         case DW_OP_breg2:
4424         case DW_OP_breg3:
4425         case DW_OP_breg4:
4426         case DW_OP_breg5:
4427         case DW_OP_breg6:
4428         case DW_OP_breg7:
4429         case DW_OP_breg8:
4430         case DW_OP_breg9:
4431         case DW_OP_breg10:
4432         case DW_OP_breg11:
4433         case DW_OP_breg12:
4434         case DW_OP_breg13:
4435         case DW_OP_breg14:
4436         case DW_OP_breg15:
4437         case DW_OP_breg16:
4438         case DW_OP_breg17:
4439         case DW_OP_breg18:
4440         case DW_OP_breg19:
4441         case DW_OP_breg20:
4442         case DW_OP_breg21:
4443         case DW_OP_breg22:
4444         case DW_OP_breg23:
4445         case DW_OP_breg24:
4446         case DW_OP_breg25:
4447         case DW_OP_breg26:
4448         case DW_OP_breg27:
4449         case DW_OP_breg28:
4450         case DW_OP_breg29:
4451         case DW_OP_breg30:
4452         case DW_OP_breg31:
4453           cfa->reg = op - DW_OP_breg0;
4454           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4455           break;
4456         case DW_OP_bregx:
4457           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4458           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4459           break;
4460         case DW_OP_deref:
4461           cfa->indirect = 1;
4462           break;
4463         case DW_OP_plus_uconst:
4464           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4465           break;
4466         default:
4467           internal_error ("DW_LOC_OP %s not implemented",
4468                           dwarf_stack_op_name (ptr->dw_loc_opc));
4469         }
4470     }
4471 }
4472 #endif /* .debug_frame support */
4473 \f
4474 /* And now, the support for symbolic debugging information.  */
4475 #ifdef DWARF2_DEBUGGING_INFO
4476
4477 /* .debug_str support.  */
4478 static int output_indirect_string (void **, void *);
4479
4480 static void dwarf2out_init (const char *);
4481 static void dwarf2out_finish (const char *);
4482 static void dwarf2out_define (unsigned int, const char *);
4483 static void dwarf2out_undef (unsigned int, const char *);
4484 static void dwarf2out_start_source_file (unsigned, const char *);
4485 static void dwarf2out_end_source_file (unsigned);
4486 static void dwarf2out_begin_block (unsigned, unsigned);
4487 static void dwarf2out_end_block (unsigned, unsigned);
4488 static bool dwarf2out_ignore_block (const_tree);
4489 static void dwarf2out_global_decl (tree);
4490 static void dwarf2out_type_decl (tree, int);
4491 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4492 static void dwarf2out_abstract_function (tree);
4493 static void dwarf2out_var_location (rtx);
4494 static void dwarf2out_begin_function (tree);
4495
4496 /* The debug hooks structure.  */
4497
4498 const struct gcc_debug_hooks dwarf2_debug_hooks =
4499 {
4500   dwarf2out_init,
4501   dwarf2out_finish,
4502   dwarf2out_define,
4503   dwarf2out_undef,
4504   dwarf2out_start_source_file,
4505   dwarf2out_end_source_file,
4506   dwarf2out_begin_block,
4507   dwarf2out_end_block,
4508   dwarf2out_ignore_block,
4509   dwarf2out_source_line,
4510   dwarf2out_begin_prologue,
4511   debug_nothing_int_charstar,   /* end_prologue */
4512   dwarf2out_end_epilogue,
4513   dwarf2out_begin_function,
4514   debug_nothing_int,            /* end_function */
4515   dwarf2out_decl,               /* function_decl */
4516   dwarf2out_global_decl,
4517   dwarf2out_type_decl,          /* type_decl */
4518   dwarf2out_imported_module_or_decl,
4519   debug_nothing_tree,           /* deferred_inline_function */
4520   /* The DWARF 2 backend tries to reduce debugging bloat by not
4521      emitting the abstract description of inline functions until
4522      something tries to reference them.  */
4523   dwarf2out_abstract_function,  /* outlining_inline_function */
4524   debug_nothing_rtx,            /* label */
4525   debug_nothing_int,            /* handle_pch */
4526   dwarf2out_var_location,
4527   dwarf2out_switch_text_section,
4528   1                             /* start_end_main_source_file */
4529 };
4530 #endif
4531 \f
4532 /* NOTE: In the comments in this file, many references are made to
4533    "Debugging Information Entries".  This term is abbreviated as `DIE'
4534    throughout the remainder of this file.  */
4535
4536 /* An internal representation of the DWARF output is built, and then
4537    walked to generate the DWARF debugging info.  The walk of the internal
4538    representation is done after the entire program has been compiled.
4539    The types below are used to describe the internal representation.  */
4540
4541 /* Various DIE's use offsets relative to the beginning of the
4542    .debug_info section to refer to each other.  */
4543
4544 typedef long int dw_offset;
4545
4546 /* Define typedefs here to avoid circular dependencies.  */
4547
4548 typedef struct dw_attr_struct *dw_attr_ref;
4549 typedef struct dw_line_info_struct *dw_line_info_ref;
4550 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4551 typedef struct pubname_struct *pubname_ref;
4552 typedef struct dw_ranges_struct *dw_ranges_ref;
4553 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4554
4555 /* Each entry in the line_info_table maintains the file and
4556    line number associated with the label generated for that
4557    entry.  The label gives the PC value associated with
4558    the line number entry.  */
4559
4560 typedef struct dw_line_info_struct GTY(())
4561 {
4562   unsigned long dw_file_num;
4563   unsigned long dw_line_num;
4564 }
4565 dw_line_info_entry;
4566
4567 /* Line information for functions in separate sections; each one gets its
4568    own sequence.  */
4569 typedef struct dw_separate_line_info_struct GTY(())
4570 {
4571   unsigned long dw_file_num;
4572   unsigned long dw_line_num;
4573   unsigned long function;
4574 }
4575 dw_separate_line_info_entry;
4576
4577 /* Each DIE attribute has a field specifying the attribute kind,
4578    a link to the next attribute in the chain, and an attribute value.
4579    Attributes are typically linked below the DIE they modify.  */
4580
4581 typedef struct dw_attr_struct GTY(())
4582 {
4583   enum dwarf_attribute dw_attr;
4584   dw_val_node dw_attr_val;
4585 }
4586 dw_attr_node;
4587
4588 DEF_VEC_O(dw_attr_node);
4589 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4590
4591 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4592    The children of each node form a circular list linked by
4593    die_sib.  die_child points to the node *before* the "first" child node.  */
4594
4595 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
4596 {
4597   enum dwarf_tag die_tag;
4598   char *die_symbol;
4599   VEC(dw_attr_node,gc) * die_attr;
4600   dw_die_ref die_parent;
4601   dw_die_ref die_child;
4602   dw_die_ref die_sib;
4603   dw_die_ref die_definition; /* ref from a specification to its definition */
4604   dw_offset die_offset;
4605   unsigned long die_abbrev;
4606   int die_mark;
4607   /* Die is used and must not be pruned as unused.  */
4608   int die_perennial_p;
4609   unsigned int decl_id;
4610 }
4611 die_node;
4612
4613 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
4614 #define FOR_EACH_CHILD(die, c, expr) do {       \
4615   c = die->die_child;                           \
4616   if (c) do {                                   \
4617     c = c->die_sib;                             \
4618     expr;                                       \
4619   } while (c != die->die_child);                \
4620 } while (0)
4621
4622 /* The pubname structure */
4623
4624 typedef struct pubname_struct GTY(())
4625 {
4626   dw_die_ref die;
4627   const char *name;
4628 }
4629 pubname_entry;
4630
4631 DEF_VEC_O(pubname_entry);
4632 DEF_VEC_ALLOC_O(pubname_entry, gc);
4633
4634 struct dw_ranges_struct GTY(())
4635 {
4636   /* If this is positive, it's a block number, otherwise it's a
4637      bitwise-negated index into dw_ranges_by_label.  */
4638   int num;
4639 };
4640
4641 struct dw_ranges_by_label_struct GTY(())
4642 {
4643   const char *begin;
4644   const char *end;
4645 };
4646
4647 /* The limbo die list structure.  */
4648 typedef struct limbo_die_struct GTY(())
4649 {
4650   dw_die_ref die;
4651   tree created_for;
4652   struct limbo_die_struct *next;
4653 }
4654 limbo_die_node;
4655
4656 /* How to start an assembler comment.  */
4657 #ifndef ASM_COMMENT_START
4658 #define ASM_COMMENT_START ";#"
4659 #endif
4660
4661 /* Define a macro which returns nonzero for a TYPE_DECL which was
4662    implicitly generated for a tagged type.
4663
4664    Note that unlike the gcc front end (which generates a NULL named
4665    TYPE_DECL node for each complete tagged type, each array type, and
4666    each function type node created) the g++ front end generates a
4667    _named_ TYPE_DECL node for each tagged type node created.
4668    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
4669    generate a DW_TAG_typedef DIE for them.  */
4670
4671 #define TYPE_DECL_IS_STUB(decl)                         \
4672   (DECL_NAME (decl) == NULL_TREE                        \
4673    || (DECL_ARTIFICIAL (decl)                           \
4674        && is_tagged_type (TREE_TYPE (decl))             \
4675        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
4676            /* This is necessary for stub decls that     \
4677               appear in nested inline functions.  */    \
4678            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
4679                && (decl_ultimate_origin (decl)          \
4680                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
4681
4682 /* Information concerning the compilation unit's programming
4683    language, and compiler version.  */
4684
4685 /* Fixed size portion of the DWARF compilation unit header.  */
4686 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
4687   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
4688
4689 /* Fixed size portion of public names info.  */
4690 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
4691
4692 /* Fixed size portion of the address range info.  */
4693 #define DWARF_ARANGES_HEADER_SIZE                                       \
4694   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
4695                 DWARF2_ADDR_SIZE * 2)                                   \
4696    - DWARF_INITIAL_LENGTH_SIZE)
4697
4698 /* Size of padding portion in the address range info.  It must be
4699    aligned to twice the pointer size.  */
4700 #define DWARF_ARANGES_PAD_SIZE \
4701   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
4702                 DWARF2_ADDR_SIZE * 2)                              \
4703    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
4704
4705 /* Use assembler line directives if available.  */
4706 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
4707 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
4708 #define DWARF2_ASM_LINE_DEBUG_INFO 1
4709 #else
4710 #define DWARF2_ASM_LINE_DEBUG_INFO 0
4711 #endif
4712 #endif
4713
4714 /* Minimum line offset in a special line info. opcode.
4715    This value was chosen to give a reasonable range of values.  */
4716 #define DWARF_LINE_BASE  -10
4717
4718 /* First special line opcode - leave room for the standard opcodes.  */
4719 #define DWARF_LINE_OPCODE_BASE  10
4720
4721 /* Range of line offsets in a special line info. opcode.  */
4722 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4723
4724 /* Flag that indicates the initial value of the is_stmt_start flag.
4725    In the present implementation, we do not mark any lines as
4726    the beginning of a source statement, because that information
4727    is not made available by the GCC front-end.  */
4728 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4729
4730 #ifdef DWARF2_DEBUGGING_INFO
4731 /* This location is used by calc_die_sizes() to keep track
4732    the offset of each DIE within the .debug_info section.  */
4733 static unsigned long next_die_offset;
4734 #endif
4735
4736 /* Record the root of the DIE's built for the current compilation unit.  */
4737 static GTY(()) dw_die_ref comp_unit_die;
4738
4739 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4740 static GTY(()) limbo_die_node *limbo_die_list;
4741
4742 /* Filenames referenced by this compilation unit.  */
4743 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4744
4745 /* A hash table of references to DIE's that describe declarations.
4746    The key is a DECL_UID() which is a unique number identifying each decl.  */
4747 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4748
4749 /* Node of the variable location list.  */
4750 struct var_loc_node GTY ((chain_next ("%h.next")))
4751 {
4752   rtx GTY (()) var_loc_note;
4753   const char * GTY (()) label;
4754   const char * GTY (()) section_label;
4755   struct var_loc_node * GTY (()) next;
4756 };
4757
4758 /* Variable location list.  */
4759 struct var_loc_list_def GTY (())
4760 {
4761   struct var_loc_node * GTY (()) first;
4762
4763   /* Do not mark the last element of the chained list because
4764      it is marked through the chain.  */
4765   struct var_loc_node * GTY ((skip ("%h"))) last;
4766
4767   /* DECL_UID of the variable decl.  */
4768   unsigned int decl_id;
4769 };
4770 typedef struct var_loc_list_def var_loc_list;
4771
4772
4773 /* Table of decl location linked lists.  */
4774 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4775
4776 /* A pointer to the base of a list of references to DIE's that
4777    are uniquely identified by their tag, presence/absence of
4778    children DIE's, and list of attribute/value pairs.  */
4779 static GTY((length ("abbrev_die_table_allocated")))
4780   dw_die_ref *abbrev_die_table;
4781
4782 /* Number of elements currently allocated for abbrev_die_table.  */
4783 static GTY(()) unsigned abbrev_die_table_allocated;
4784
4785 /* Number of elements in type_die_table currently in use.  */
4786 static GTY(()) unsigned abbrev_die_table_in_use;
4787
4788 /* Size (in elements) of increments by which we may expand the
4789    abbrev_die_table.  */
4790 #define ABBREV_DIE_TABLE_INCREMENT 256
4791
4792 /* A pointer to the base of a table that contains line information
4793    for each source code line in .text in the compilation unit.  */
4794 static GTY((length ("line_info_table_allocated")))
4795      dw_line_info_ref line_info_table;
4796
4797 /* Number of elements currently allocated for line_info_table.  */
4798 static GTY(()) unsigned line_info_table_allocated;
4799
4800 /* Number of elements in line_info_table currently in use.  */
4801 static GTY(()) unsigned line_info_table_in_use;
4802
4803 /* A pointer to the base of a table that contains line information
4804    for each source code line outside of .text in the compilation unit.  */
4805 static GTY ((length ("separate_line_info_table_allocated")))
4806      dw_separate_line_info_ref separate_line_info_table;
4807
4808 /* Number of elements currently allocated for separate_line_info_table.  */
4809 static GTY(()) unsigned separate_line_info_table_allocated;
4810
4811 /* Number of elements in separate_line_info_table currently in use.  */
4812 static GTY(()) unsigned separate_line_info_table_in_use;
4813
4814 /* Size (in elements) of increments by which we may expand the
4815    line_info_table.  */
4816 #define LINE_INFO_TABLE_INCREMENT 1024
4817
4818 /* A pointer to the base of a table that contains a list of publicly
4819    accessible names.  */
4820 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4821
4822 /* A pointer to the base of a table that contains a list of publicly
4823    accessible types.  */
4824 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4825
4826 /* Array of dies for which we should generate .debug_arange info.  */
4827 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4828
4829 /* Number of elements currently allocated for arange_table.  */
4830 static GTY(()) unsigned arange_table_allocated;
4831
4832 /* Number of elements in arange_table currently in use.  */
4833 static GTY(()) unsigned arange_table_in_use;
4834
4835 /* Size (in elements) of increments by which we may expand the
4836    arange_table.  */
4837 #define ARANGE_TABLE_INCREMENT 64
4838
4839 /* Array of dies for which we should generate .debug_ranges info.  */
4840 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4841
4842 /* Number of elements currently allocated for ranges_table.  */
4843 static GTY(()) unsigned ranges_table_allocated;
4844
4845 /* Number of elements in ranges_table currently in use.  */
4846 static GTY(()) unsigned ranges_table_in_use;
4847
4848 /* Array of pairs of labels referenced in ranges_table.  */
4849 static GTY ((length ("ranges_by_label_allocated")))
4850      dw_ranges_by_label_ref ranges_by_label;
4851
4852 /* Number of elements currently allocated for ranges_by_label.  */
4853 static GTY(()) unsigned ranges_by_label_allocated;
4854
4855 /* Number of elements in ranges_by_label currently in use.  */
4856 static GTY(()) unsigned ranges_by_label_in_use;
4857
4858 /* Size (in elements) of increments by which we may expand the
4859    ranges_table.  */
4860 #define RANGES_TABLE_INCREMENT 64
4861
4862 /* Whether we have location lists that need outputting */
4863 static GTY(()) bool have_location_lists;
4864
4865 /* Unique label counter.  */
4866 static GTY(()) unsigned int loclabel_num;
4867
4868 #ifdef DWARF2_DEBUGGING_INFO
4869 /* Record whether the function being analyzed contains inlined functions.  */
4870 static int current_function_has_inlines;
4871 #endif
4872 #if 0 && defined (MIPS_DEBUGGING_INFO)
4873 static int comp_unit_has_inlines;
4874 #endif
4875
4876 /* The last file entry emitted by maybe_emit_file().  */
4877 static GTY(()) struct dwarf_file_data * last_emitted_file;
4878
4879 /* Number of internal labels generated by gen_internal_sym().  */
4880 static GTY(()) int label_num;
4881
4882 /* Cached result of previous call to lookup_filename.  */
4883 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4884
4885 #ifdef DWARF2_DEBUGGING_INFO
4886
4887 /* Offset from the "steady-state frame pointer" to the frame base,
4888    within the current function.  */
4889 static HOST_WIDE_INT frame_pointer_fb_offset;
4890
4891 /* Forward declarations for functions defined in this file.  */
4892
4893 static int is_pseudo_reg (const_rtx);
4894 static tree type_main_variant (tree);
4895 static int is_tagged_type (const_tree);
4896 static const char *dwarf_tag_name (unsigned);
4897 static const char *dwarf_attr_name (unsigned);
4898 static const char *dwarf_form_name (unsigned);
4899 static tree decl_ultimate_origin (const_tree);
4900 static tree block_ultimate_origin (const_tree);
4901 static tree decl_class_context (tree);
4902 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4903 static inline enum dw_val_class AT_class (dw_attr_ref);
4904 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4905 static inline unsigned AT_flag (dw_attr_ref);
4906 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4907 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4908 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4909 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4910 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4911                               unsigned long);
4912 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4913                                unsigned int, unsigned char *);
4914 static hashval_t debug_str_do_hash (const void *);
4915 static int debug_str_eq (const void *, const void *);
4916 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4917 static inline const char *AT_string (dw_attr_ref);
4918 static int AT_string_form (dw_attr_ref);
4919 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4920 static void add_AT_specification (dw_die_ref, dw_die_ref);
4921 static inline dw_die_ref AT_ref (dw_attr_ref);
4922 static inline int AT_ref_external (dw_attr_ref);
4923 static inline void set_AT_ref_external (dw_attr_ref, int);
4924 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4925 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4926 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4927 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4928                              dw_loc_list_ref);
4929 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4930 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4931 static inline rtx AT_addr (dw_attr_ref);
4932 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4933 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4934 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4935 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4936                            unsigned HOST_WIDE_INT);
4937 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4938                                unsigned long);
4939 static inline const char *AT_lbl (dw_attr_ref);
4940 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4941 static const char *get_AT_low_pc (dw_die_ref);
4942 static const char *get_AT_hi_pc (dw_die_ref);
4943 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4944 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4945 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4946 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4947 static bool is_c_family (void);
4948 static bool is_cxx (void);
4949 static bool is_java (void);
4950 static bool is_fortran (void);
4951 static bool is_ada (void);
4952 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4953 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4954 static void add_child_die (dw_die_ref, dw_die_ref);
4955 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4956 static dw_die_ref lookup_type_die (tree);
4957 static void equate_type_number_to_die (tree, dw_die_ref);
4958 static hashval_t decl_die_table_hash (const void *);
4959 static int decl_die_table_eq (const void *, const void *);
4960 static dw_die_ref lookup_decl_die (tree);
4961 static hashval_t decl_loc_table_hash (const void *);
4962 static int decl_loc_table_eq (const void *, const void *);
4963 static var_loc_list *lookup_decl_loc (const_tree);
4964 static void equate_decl_number_to_die (tree, dw_die_ref);
4965 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4966 static void print_spaces (FILE *);
4967 static void print_die (dw_die_ref, FILE *);
4968 static void print_dwarf_line_table (FILE *);
4969 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4970 static dw_die_ref pop_compile_unit (dw_die_ref);
4971 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4972 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4973 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4974 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4975 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4976 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4977 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4978 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4979 static void compute_section_prefix (dw_die_ref);
4980 static int is_type_die (dw_die_ref);
4981 static int is_comdat_die (dw_die_ref);
4982 static int is_symbol_die (dw_die_ref);
4983 static void assign_symbol_names (dw_die_ref);
4984 static void break_out_includes (dw_die_ref);
4985 static hashval_t htab_cu_hash (const void *);
4986 static int htab_cu_eq (const void *, const void *);
4987 static void htab_cu_del (void *);
4988 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4989 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4990 static void add_sibling_attributes (dw_die_ref);
4991 static void build_abbrev_table (dw_die_ref);
4992 static void output_location_lists (dw_die_ref);
4993 static int constant_size (unsigned HOST_WIDE_INT);
4994 static unsigned long size_of_die (dw_die_ref);
4995 static void calc_die_sizes (dw_die_ref);
4996 static void mark_dies (dw_die_ref);
4997 static void unmark_dies (dw_die_ref);
4998 static void unmark_all_dies (dw_die_ref);
4999 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5000 static unsigned long size_of_aranges (void);
5001 static enum dwarf_form value_format (dw_attr_ref);
5002 static void output_value_format (dw_attr_ref);
5003 static void output_abbrev_section (void);
5004 static void output_die_symbol (dw_die_ref);
5005 static void output_die (dw_die_ref);
5006 static void output_compilation_unit_header (void);
5007 static void output_comp_unit (dw_die_ref, int);
5008 static const char *dwarf2_name (tree, int);
5009 static void add_pubname (tree, dw_die_ref);
5010 static void add_pubname_string (const char *, dw_die_ref);
5011 static void add_pubtype (tree, dw_die_ref);
5012 static void output_pubnames (VEC (pubname_entry,gc) *);
5013 static void add_arange (tree, dw_die_ref);
5014 static void output_aranges (void);
5015 static unsigned int add_ranges_num (int);
5016 static unsigned int add_ranges (const_tree);
5017 static unsigned int add_ranges_by_labels (const char *, const char *);
5018 static void output_ranges (void);
5019 static void output_line_info (void);
5020 static void output_file_names (void);
5021 static dw_die_ref base_type_die (tree);
5022 static int is_base_type (tree);
5023 static bool is_subrange_type (const_tree);
5024 static dw_die_ref subrange_type_die (tree, dw_die_ref);
5025 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5026 static int type_is_enum (const_tree);
5027 static unsigned int dbx_reg_number (const_rtx);
5028 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5029 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5030 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5031                                                 enum var_init_status);
5032 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5033                                                      enum var_init_status);
5034 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5035                                          enum var_init_status);
5036 static int is_based_loc (const_rtx);
5037 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5038                                             enum var_init_status);
5039 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5040                                                enum var_init_status);
5041 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5042 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5043 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5044 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5045 static tree field_type (const_tree);
5046 static unsigned int simple_type_align_in_bits (const_tree);
5047 static unsigned int simple_decl_align_in_bits (const_tree);
5048 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5049 static HOST_WIDE_INT field_byte_offset (const_tree);
5050 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5051                                          dw_loc_descr_ref);
5052 static void add_data_member_location_attribute (dw_die_ref, tree);
5053 static void add_const_value_attribute (dw_die_ref, rtx);
5054 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5055 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5056 static void insert_float (const_rtx, unsigned char *);
5057 static rtx rtl_for_decl_location (tree);
5058 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5059                                                    enum dwarf_attribute);
5060 static void tree_add_const_value_attribute (dw_die_ref, tree);
5061 static void add_name_attribute (dw_die_ref, const char *);
5062 static void add_comp_dir_attribute (dw_die_ref);
5063 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5064 static void add_subscript_info (dw_die_ref, tree, bool);
5065 static void add_byte_size_attribute (dw_die_ref, tree);
5066 static void add_bit_offset_attribute (dw_die_ref, tree);
5067 static void add_bit_size_attribute (dw_die_ref, tree);
5068 static void add_prototyped_attribute (dw_die_ref, tree);
5069 static void add_abstract_origin_attribute (dw_die_ref, tree);
5070 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5071 static void add_src_coords_attributes (dw_die_ref, tree);
5072 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5073 static void push_decl_scope (tree);
5074 static void pop_decl_scope (void);
5075 static dw_die_ref scope_die_for (tree, dw_die_ref);
5076 static inline int local_scope_p (dw_die_ref);
5077 static inline int class_or_namespace_scope_p (dw_die_ref);
5078 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5079 static void add_calling_convention_attribute (dw_die_ref, tree);
5080 static const char *type_tag (const_tree);
5081 static tree member_declared_type (const_tree);
5082 #if 0
5083 static const char *decl_start_label (tree);
5084 #endif
5085 static void gen_array_type_die (tree, dw_die_ref);
5086 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5087 #if 0
5088 static void gen_entry_point_die (tree, dw_die_ref);
5089 #endif
5090 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
5091 static void gen_inlined_structure_type_die (tree, dw_die_ref);
5092 static void gen_inlined_union_type_die (tree, dw_die_ref);
5093 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5094 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
5095 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5096 static void gen_formal_types_die (tree, dw_die_ref);
5097 static void gen_subprogram_die (tree, dw_die_ref);
5098 static void gen_variable_die (tree, dw_die_ref);
5099 static void gen_const_die (tree, dw_die_ref);
5100 static void gen_label_die (tree, dw_die_ref);
5101 static void gen_lexical_block_die (tree, dw_die_ref, int);
5102 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5103 static void gen_field_die (tree, dw_die_ref);
5104 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5105 static dw_die_ref gen_compile_unit_die (const char *);
5106 static void gen_inheritance_die (tree, tree, dw_die_ref);
5107 static void gen_member_die (tree, dw_die_ref);
5108 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5109                                                 enum debug_info_usage);
5110 static void gen_subroutine_type_die (tree, dw_die_ref);
5111 static void gen_typedef_die (tree, dw_die_ref);
5112 static void gen_type_die (tree, dw_die_ref);
5113 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
5114 static void gen_block_die (tree, dw_die_ref, int);
5115 static void decls_for_scope (tree, dw_die_ref, int);
5116 static int is_redundant_typedef (const_tree);
5117 static void gen_namespace_die (tree);
5118 static void gen_decl_die (tree, dw_die_ref);
5119 static dw_die_ref force_decl_die (tree);
5120 static dw_die_ref force_type_die (tree);
5121 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5122 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5123 static struct dwarf_file_data * lookup_filename (const char *);
5124 static void retry_incomplete_types (void);
5125 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5126 static void splice_child_die (dw_die_ref, dw_die_ref);
5127 static int file_info_cmp (const void *, const void *);
5128 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5129                                      const char *, const char *, unsigned);
5130 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5131                                        const char *, const char *,
5132                                        const char *);
5133 static void output_loc_list (dw_loc_list_ref);
5134 static char *gen_internal_sym (const char *);
5135
5136 static void prune_unmark_dies (dw_die_ref);
5137 static void prune_unused_types_mark (dw_die_ref, int);
5138 static void prune_unused_types_walk (dw_die_ref);
5139 static void prune_unused_types_walk_attribs (dw_die_ref);
5140 static void prune_unused_types_prune (dw_die_ref);
5141 static void prune_unused_types (void);
5142 static int maybe_emit_file (struct dwarf_file_data *fd);
5143
5144 /* Section names used to hold DWARF debugging information.  */
5145 #ifndef DEBUG_INFO_SECTION
5146 #define DEBUG_INFO_SECTION      ".debug_info"
5147 #endif
5148 #ifndef DEBUG_ABBREV_SECTION
5149 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5150 #endif
5151 #ifndef DEBUG_ARANGES_SECTION
5152 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5153 #endif
5154 #ifndef DEBUG_MACINFO_SECTION
5155 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5156 #endif
5157 #ifndef DEBUG_LINE_SECTION
5158 #define DEBUG_LINE_SECTION      ".debug_line"
5159 #endif
5160 #ifndef DEBUG_LOC_SECTION
5161 #define DEBUG_LOC_SECTION       ".debug_loc"
5162 #endif
5163 #ifndef DEBUG_PUBNAMES_SECTION
5164 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5165 #endif
5166 #ifndef DEBUG_STR_SECTION
5167 #define DEBUG_STR_SECTION       ".debug_str"
5168 #endif
5169 #ifndef DEBUG_RANGES_SECTION
5170 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5171 #endif
5172
5173 /* Standard ELF section names for compiled code and data.  */
5174 #ifndef TEXT_SECTION_NAME
5175 #define TEXT_SECTION_NAME       ".text"
5176 #endif
5177
5178 /* Section flags for .debug_str section.  */
5179 #define DEBUG_STR_SECTION_FLAGS \
5180   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5181    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5182    : SECTION_DEBUG)
5183
5184 /* Labels we insert at beginning sections we can reference instead of
5185    the section names themselves.  */
5186
5187 #ifndef TEXT_SECTION_LABEL
5188 #define TEXT_SECTION_LABEL              "Ltext"
5189 #endif
5190 #ifndef COLD_TEXT_SECTION_LABEL
5191 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5192 #endif
5193 #ifndef DEBUG_LINE_SECTION_LABEL
5194 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5195 #endif
5196 #ifndef DEBUG_INFO_SECTION_LABEL
5197 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5198 #endif
5199 #ifndef DEBUG_ABBREV_SECTION_LABEL
5200 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5201 #endif
5202 #ifndef DEBUG_LOC_SECTION_LABEL
5203 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5204 #endif
5205 #ifndef DEBUG_RANGES_SECTION_LABEL
5206 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5207 #endif
5208 #ifndef DEBUG_MACINFO_SECTION_LABEL
5209 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5210 #endif
5211
5212 /* Definitions of defaults for formats and names of various special
5213    (artificial) labels which may be generated within this file (when the -g
5214    options is used and DWARF2_DEBUGGING_INFO is in effect.
5215    If necessary, these may be overridden from within the tm.h file, but
5216    typically, overriding these defaults is unnecessary.  */
5217
5218 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5219 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5220 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5221 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5222 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5223 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5224 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5225 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5226 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5227 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5228
5229 #ifndef TEXT_END_LABEL
5230 #define TEXT_END_LABEL          "Letext"
5231 #endif
5232 #ifndef COLD_END_LABEL
5233 #define COLD_END_LABEL          "Letext_cold"
5234 #endif
5235 #ifndef BLOCK_BEGIN_LABEL
5236 #define BLOCK_BEGIN_LABEL       "LBB"
5237 #endif
5238 #ifndef BLOCK_END_LABEL
5239 #define BLOCK_END_LABEL         "LBE"
5240 #endif
5241 #ifndef LINE_CODE_LABEL
5242 #define LINE_CODE_LABEL         "LM"
5243 #endif
5244 #ifndef SEPARATE_LINE_CODE_LABEL
5245 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5246 #endif
5247
5248 \f
5249 /* We allow a language front-end to designate a function that is to be
5250    called to "demangle" any name before it is put into a DIE.  */
5251
5252 static const char *(*demangle_name_func) (const char *);
5253
5254 void
5255 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5256 {
5257   demangle_name_func = func;
5258 }
5259
5260 /* Test if rtl node points to a pseudo register.  */
5261
5262 static inline int
5263 is_pseudo_reg (const_rtx rtl)
5264 {
5265   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5266           || (GET_CODE (rtl) == SUBREG
5267               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5268 }
5269
5270 /* Return a reference to a type, with its const and volatile qualifiers
5271    removed.  */
5272
5273 static inline tree
5274 type_main_variant (tree type)
5275 {
5276   type = TYPE_MAIN_VARIANT (type);
5277
5278   /* ??? There really should be only one main variant among any group of
5279      variants of a given type (and all of the MAIN_VARIANT values for all
5280      members of the group should point to that one type) but sometimes the C
5281      front-end messes this up for array types, so we work around that bug
5282      here.  */
5283   if (TREE_CODE (type) == ARRAY_TYPE)
5284     while (type != TYPE_MAIN_VARIANT (type))
5285       type = TYPE_MAIN_VARIANT (type);
5286
5287   return type;
5288 }
5289
5290 /* Return nonzero if the given type node represents a tagged type.  */
5291
5292 static inline int
5293 is_tagged_type (const_tree type)
5294 {
5295   enum tree_code code = TREE_CODE (type);
5296
5297   return (code == RECORD_TYPE || code == UNION_TYPE
5298           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5299 }
5300
5301 /* Convert a DIE tag into its string name.  */
5302
5303 static const char *
5304 dwarf_tag_name (unsigned int tag)
5305 {
5306   switch (tag)
5307     {
5308     case DW_TAG_padding:
5309       return "DW_TAG_padding";
5310     case DW_TAG_array_type:
5311       return "DW_TAG_array_type";
5312     case DW_TAG_class_type:
5313       return "DW_TAG_class_type";
5314     case DW_TAG_entry_point:
5315       return "DW_TAG_entry_point";
5316     case DW_TAG_enumeration_type:
5317       return "DW_TAG_enumeration_type";
5318     case DW_TAG_formal_parameter:
5319       return "DW_TAG_formal_parameter";
5320     case DW_TAG_imported_declaration:
5321       return "DW_TAG_imported_declaration";
5322     case DW_TAG_label:
5323       return "DW_TAG_label";
5324     case DW_TAG_lexical_block:
5325       return "DW_TAG_lexical_block";
5326     case DW_TAG_member:
5327       return "DW_TAG_member";
5328     case DW_TAG_pointer_type:
5329       return "DW_TAG_pointer_type";
5330     case DW_TAG_reference_type:
5331       return "DW_TAG_reference_type";
5332     case DW_TAG_compile_unit:
5333       return "DW_TAG_compile_unit";
5334     case DW_TAG_string_type:
5335       return "DW_TAG_string_type";
5336     case DW_TAG_structure_type:
5337       return "DW_TAG_structure_type";
5338     case DW_TAG_subroutine_type:
5339       return "DW_TAG_subroutine_type";
5340     case DW_TAG_typedef:
5341       return "DW_TAG_typedef";
5342     case DW_TAG_union_type:
5343       return "DW_TAG_union_type";
5344     case DW_TAG_unspecified_parameters:
5345       return "DW_TAG_unspecified_parameters";
5346     case DW_TAG_variant:
5347       return "DW_TAG_variant";
5348     case DW_TAG_common_block:
5349       return "DW_TAG_common_block";
5350     case DW_TAG_common_inclusion:
5351       return "DW_TAG_common_inclusion";
5352     case DW_TAG_inheritance:
5353       return "DW_TAG_inheritance";
5354     case DW_TAG_inlined_subroutine:
5355       return "DW_TAG_inlined_subroutine";
5356     case DW_TAG_module:
5357       return "DW_TAG_module";
5358     case DW_TAG_ptr_to_member_type:
5359       return "DW_TAG_ptr_to_member_type";
5360     case DW_TAG_set_type:
5361       return "DW_TAG_set_type";
5362     case DW_TAG_subrange_type:
5363       return "DW_TAG_subrange_type";
5364     case DW_TAG_with_stmt:
5365       return "DW_TAG_with_stmt";
5366     case DW_TAG_access_declaration:
5367       return "DW_TAG_access_declaration";
5368     case DW_TAG_base_type:
5369       return "DW_TAG_base_type";
5370     case DW_TAG_catch_block:
5371       return "DW_TAG_catch_block";
5372     case DW_TAG_const_type:
5373       return "DW_TAG_const_type";
5374     case DW_TAG_constant:
5375       return "DW_TAG_constant";
5376     case DW_TAG_enumerator:
5377       return "DW_TAG_enumerator";
5378     case DW_TAG_file_type:
5379       return "DW_TAG_file_type";
5380     case DW_TAG_friend:
5381       return "DW_TAG_friend";
5382     case DW_TAG_namelist:
5383       return "DW_TAG_namelist";
5384     case DW_TAG_namelist_item:
5385       return "DW_TAG_namelist_item";
5386     case DW_TAG_packed_type:
5387       return "DW_TAG_packed_type";
5388     case DW_TAG_subprogram:
5389       return "DW_TAG_subprogram";
5390     case DW_TAG_template_type_param:
5391       return "DW_TAG_template_type_param";
5392     case DW_TAG_template_value_param:
5393       return "DW_TAG_template_value_param";
5394     case DW_TAG_thrown_type:
5395       return "DW_TAG_thrown_type";
5396     case DW_TAG_try_block:
5397       return "DW_TAG_try_block";
5398     case DW_TAG_variant_part:
5399       return "DW_TAG_variant_part";
5400     case DW_TAG_variable:
5401       return "DW_TAG_variable";
5402     case DW_TAG_volatile_type:
5403       return "DW_TAG_volatile_type";
5404     case DW_TAG_dwarf_procedure:
5405       return "DW_TAG_dwarf_procedure";
5406     case DW_TAG_restrict_type:
5407       return "DW_TAG_restrict_type";
5408     case DW_TAG_interface_type:
5409       return "DW_TAG_interface_type";
5410     case DW_TAG_namespace:
5411       return "DW_TAG_namespace";
5412     case DW_TAG_imported_module:
5413       return "DW_TAG_imported_module";
5414     case DW_TAG_unspecified_type:
5415       return "DW_TAG_unspecified_type";
5416     case DW_TAG_partial_unit:
5417       return "DW_TAG_partial_unit";
5418     case DW_TAG_imported_unit:
5419       return "DW_TAG_imported_unit";
5420     case DW_TAG_condition:
5421       return "DW_TAG_condition";
5422     case DW_TAG_shared_type:
5423       return "DW_TAG_shared_type";
5424     case DW_TAG_MIPS_loop:
5425       return "DW_TAG_MIPS_loop";
5426     case DW_TAG_format_label:
5427       return "DW_TAG_format_label";
5428     case DW_TAG_function_template:
5429       return "DW_TAG_function_template";
5430     case DW_TAG_class_template:
5431       return "DW_TAG_class_template";
5432     case DW_TAG_GNU_BINCL:
5433       return "DW_TAG_GNU_BINCL";
5434     case DW_TAG_GNU_EINCL:
5435       return "DW_TAG_GNU_EINCL";
5436     default:
5437       return "DW_TAG_<unknown>";
5438     }
5439 }
5440
5441 /* Convert a DWARF attribute code into its string name.  */
5442
5443 static const char *
5444 dwarf_attr_name (unsigned int attr)
5445 {
5446   switch (attr)
5447     {
5448     case DW_AT_sibling:
5449       return "DW_AT_sibling";
5450     case DW_AT_location:
5451       return "DW_AT_location";
5452     case DW_AT_name:
5453       return "DW_AT_name";
5454     case DW_AT_ordering:
5455       return "DW_AT_ordering";
5456     case DW_AT_subscr_data:
5457       return "DW_AT_subscr_data";
5458     case DW_AT_byte_size:
5459       return "DW_AT_byte_size";
5460     case DW_AT_bit_offset:
5461       return "DW_AT_bit_offset";
5462     case DW_AT_bit_size:
5463       return "DW_AT_bit_size";
5464     case DW_AT_element_list:
5465       return "DW_AT_element_list";
5466     case DW_AT_stmt_list:
5467       return "DW_AT_stmt_list";
5468     case DW_AT_low_pc:
5469       return "DW_AT_low_pc";
5470     case DW_AT_high_pc:
5471       return "DW_AT_high_pc";
5472     case DW_AT_language:
5473       return "DW_AT_language";
5474     case DW_AT_member:
5475       return "DW_AT_member";
5476     case DW_AT_discr:
5477       return "DW_AT_discr";
5478     case DW_AT_discr_value:
5479       return "DW_AT_discr_value";
5480     case DW_AT_visibility:
5481       return "DW_AT_visibility";
5482     case DW_AT_import:
5483       return "DW_AT_import";
5484     case DW_AT_string_length:
5485       return "DW_AT_string_length";
5486     case DW_AT_common_reference:
5487       return "DW_AT_common_reference";
5488     case DW_AT_comp_dir:
5489       return "DW_AT_comp_dir";
5490     case DW_AT_const_value:
5491       return "DW_AT_const_value";
5492     case DW_AT_containing_type:
5493       return "DW_AT_containing_type";
5494     case DW_AT_default_value:
5495       return "DW_AT_default_value";
5496     case DW_AT_inline:
5497       return "DW_AT_inline";
5498     case DW_AT_is_optional:
5499       return "DW_AT_is_optional";
5500     case DW_AT_lower_bound:
5501       return "DW_AT_lower_bound";
5502     case DW_AT_producer:
5503       return "DW_AT_producer";
5504     case DW_AT_prototyped:
5505       return "DW_AT_prototyped";
5506     case DW_AT_return_addr:
5507       return "DW_AT_return_addr";
5508     case DW_AT_start_scope:
5509       return "DW_AT_start_scope";
5510     case DW_AT_bit_stride:
5511       return "DW_AT_bit_stride";
5512     case DW_AT_upper_bound:
5513       return "DW_AT_upper_bound";
5514     case DW_AT_abstract_origin:
5515       return "DW_AT_abstract_origin";
5516     case DW_AT_accessibility:
5517       return "DW_AT_accessibility";
5518     case DW_AT_address_class:
5519       return "DW_AT_address_class";
5520     case DW_AT_artificial:
5521       return "DW_AT_artificial";
5522     case DW_AT_base_types:
5523       return "DW_AT_base_types";
5524     case DW_AT_calling_convention:
5525       return "DW_AT_calling_convention";
5526     case DW_AT_count:
5527       return "DW_AT_count";
5528     case DW_AT_data_member_location:
5529       return "DW_AT_data_member_location";
5530     case DW_AT_decl_column:
5531       return "DW_AT_decl_column";
5532     case DW_AT_decl_file:
5533       return "DW_AT_decl_file";
5534     case DW_AT_decl_line:
5535       return "DW_AT_decl_line";
5536     case DW_AT_declaration:
5537       return "DW_AT_declaration";
5538     case DW_AT_discr_list:
5539       return "DW_AT_discr_list";
5540     case DW_AT_encoding:
5541       return "DW_AT_encoding";
5542     case DW_AT_external:
5543       return "DW_AT_external";
5544     case DW_AT_frame_base:
5545       return "DW_AT_frame_base";
5546     case DW_AT_friend:
5547       return "DW_AT_friend";
5548     case DW_AT_identifier_case:
5549       return "DW_AT_identifier_case";
5550     case DW_AT_macro_info:
5551       return "DW_AT_macro_info";
5552     case DW_AT_namelist_items:
5553       return "DW_AT_namelist_items";
5554     case DW_AT_priority:
5555       return "DW_AT_priority";
5556     case DW_AT_segment:
5557       return "DW_AT_segment";
5558     case DW_AT_specification:
5559       return "DW_AT_specification";
5560     case DW_AT_static_link:
5561       return "DW_AT_static_link";
5562     case DW_AT_type:
5563       return "DW_AT_type";
5564     case DW_AT_use_location:
5565       return "DW_AT_use_location";
5566     case DW_AT_variable_parameter:
5567       return "DW_AT_variable_parameter";
5568     case DW_AT_virtuality:
5569       return "DW_AT_virtuality";
5570     case DW_AT_vtable_elem_location:
5571       return "DW_AT_vtable_elem_location";
5572
5573     case DW_AT_allocated:
5574       return "DW_AT_allocated";
5575     case DW_AT_associated:
5576       return "DW_AT_associated";
5577     case DW_AT_data_location:
5578       return "DW_AT_data_location";
5579     case DW_AT_byte_stride:
5580       return "DW_AT_byte_stride";
5581     case DW_AT_entry_pc:
5582       return "DW_AT_entry_pc";
5583     case DW_AT_use_UTF8:
5584       return "DW_AT_use_UTF8";
5585     case DW_AT_extension:
5586       return "DW_AT_extension";
5587     case DW_AT_ranges:
5588       return "DW_AT_ranges";
5589     case DW_AT_trampoline:
5590       return "DW_AT_trampoline";
5591     case DW_AT_call_column:
5592       return "DW_AT_call_column";
5593     case DW_AT_call_file:
5594       return "DW_AT_call_file";
5595     case DW_AT_call_line:
5596       return "DW_AT_call_line";
5597
5598     case DW_AT_MIPS_fde:
5599       return "DW_AT_MIPS_fde";
5600     case DW_AT_MIPS_loop_begin:
5601       return "DW_AT_MIPS_loop_begin";
5602     case DW_AT_MIPS_tail_loop_begin:
5603       return "DW_AT_MIPS_tail_loop_begin";
5604     case DW_AT_MIPS_epilog_begin:
5605       return "DW_AT_MIPS_epilog_begin";
5606     case DW_AT_MIPS_loop_unroll_factor:
5607       return "DW_AT_MIPS_loop_unroll_factor";
5608     case DW_AT_MIPS_software_pipeline_depth:
5609       return "DW_AT_MIPS_software_pipeline_depth";
5610     case DW_AT_MIPS_linkage_name:
5611       return "DW_AT_MIPS_linkage_name";
5612     case DW_AT_MIPS_stride:
5613       return "DW_AT_MIPS_stride";
5614     case DW_AT_MIPS_abstract_name:
5615       return "DW_AT_MIPS_abstract_name";
5616     case DW_AT_MIPS_clone_origin:
5617       return "DW_AT_MIPS_clone_origin";
5618     case DW_AT_MIPS_has_inlines:
5619       return "DW_AT_MIPS_has_inlines";
5620
5621     case DW_AT_sf_names:
5622       return "DW_AT_sf_names";
5623     case DW_AT_src_info:
5624       return "DW_AT_src_info";
5625     case DW_AT_mac_info:
5626       return "DW_AT_mac_info";
5627     case DW_AT_src_coords:
5628       return "DW_AT_src_coords";
5629     case DW_AT_body_begin:
5630       return "DW_AT_body_begin";
5631     case DW_AT_body_end:
5632       return "DW_AT_body_end";
5633     case DW_AT_GNU_vector:
5634       return "DW_AT_GNU_vector";
5635
5636     case DW_AT_VMS_rtnbeg_pd_address:
5637       return "DW_AT_VMS_rtnbeg_pd_address";
5638
5639     default:
5640       return "DW_AT_<unknown>";
5641     }
5642 }
5643
5644 /* Convert a DWARF value form code into its string name.  */
5645
5646 static const char *
5647 dwarf_form_name (unsigned int form)
5648 {
5649   switch (form)
5650     {
5651     case DW_FORM_addr:
5652       return "DW_FORM_addr";
5653     case DW_FORM_block2:
5654       return "DW_FORM_block2";
5655     case DW_FORM_block4:
5656       return "DW_FORM_block4";
5657     case DW_FORM_data2:
5658       return "DW_FORM_data2";
5659     case DW_FORM_data4:
5660       return "DW_FORM_data4";
5661     case DW_FORM_data8:
5662       return "DW_FORM_data8";
5663     case DW_FORM_string:
5664       return "DW_FORM_string";
5665     case DW_FORM_block:
5666       return "DW_FORM_block";
5667     case DW_FORM_block1:
5668       return "DW_FORM_block1";
5669     case DW_FORM_data1:
5670       return "DW_FORM_data1";
5671     case DW_FORM_flag:
5672       return "DW_FORM_flag";
5673     case DW_FORM_sdata:
5674       return "DW_FORM_sdata";
5675     case DW_FORM_strp:
5676       return "DW_FORM_strp";
5677     case DW_FORM_udata:
5678       return "DW_FORM_udata";
5679     case DW_FORM_ref_addr:
5680       return "DW_FORM_ref_addr";
5681     case DW_FORM_ref1:
5682       return "DW_FORM_ref1";
5683     case DW_FORM_ref2:
5684       return "DW_FORM_ref2";
5685     case DW_FORM_ref4:
5686       return "DW_FORM_ref4";
5687     case DW_FORM_ref8:
5688       return "DW_FORM_ref8";
5689     case DW_FORM_ref_udata:
5690       return "DW_FORM_ref_udata";
5691     case DW_FORM_indirect:
5692       return "DW_FORM_indirect";
5693     default:
5694       return "DW_FORM_<unknown>";
5695     }
5696 }
5697 \f
5698 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
5699    instance of an inlined instance of a decl which is local to an inline
5700    function, so we have to trace all of the way back through the origin chain
5701    to find out what sort of node actually served as the original seed for the
5702    given block.  */
5703
5704 static tree
5705 decl_ultimate_origin (const_tree decl)
5706 {
5707   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
5708     return NULL_TREE;
5709
5710   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
5711      nodes in the function to point to themselves; ignore that if
5712      we're trying to output the abstract instance of this function.  */
5713   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
5714     return NULL_TREE;
5715
5716   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
5717      most distant ancestor, this should never happen.  */
5718   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
5719
5720   return DECL_ABSTRACT_ORIGIN (decl);
5721 }
5722
5723 /* Determine the "ultimate origin" of a block.  The block may be an inlined
5724    instance of an inlined instance of a block which is local to an inline
5725    function, so we have to trace all of the way back through the origin chain
5726    to find out what sort of node actually served as the original seed for the
5727    given block.  */
5728
5729 static tree
5730 block_ultimate_origin (const_tree block)
5731 {
5732   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
5733
5734   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
5735      nodes in the function to point to themselves; ignore that if
5736      we're trying to output the abstract instance of this function.  */
5737   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
5738     return NULL_TREE;
5739
5740   if (immediate_origin == NULL_TREE)
5741     return NULL_TREE;
5742   else
5743     {
5744       tree ret_val;
5745       tree lookahead = immediate_origin;
5746
5747       do
5748         {
5749           ret_val = lookahead;
5750           lookahead = (TREE_CODE (ret_val) == BLOCK
5751                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5752         }
5753       while (lookahead != NULL && lookahead != ret_val);
5754
5755       /* The block's abstract origin chain may not be the *ultimate* origin of
5756          the block. It could lead to a DECL that has an abstract origin set.
5757          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5758          will give us if it has one).  Note that DECL's abstract origins are
5759          supposed to be the most distant ancestor (or so decl_ultimate_origin
5760          claims), so we don't need to loop following the DECL origins.  */
5761       if (DECL_P (ret_val))
5762         return DECL_ORIGIN (ret_val);
5763
5764       return ret_val;
5765     }
5766 }
5767
5768 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5769    of a virtual function may refer to a base class, so we check the 'this'
5770    parameter.  */
5771
5772 static tree
5773 decl_class_context (tree decl)
5774 {
5775   tree context = NULL_TREE;
5776
5777   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5778     context = DECL_CONTEXT (decl);
5779   else
5780     context = TYPE_MAIN_VARIANT
5781       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5782
5783   if (context && !TYPE_P (context))
5784     context = NULL_TREE;
5785
5786   return context;
5787 }
5788 \f
5789 /* Add an attribute/value pair to a DIE.  */
5790
5791 static inline void
5792 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5793 {
5794   /* Maybe this should be an assert?  */
5795   if (die == NULL)
5796     return;
5797
5798   if (die->die_attr == NULL)
5799     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5800   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5801 }
5802
5803 static inline enum dw_val_class
5804 AT_class (dw_attr_ref a)
5805 {
5806   return a->dw_attr_val.val_class;
5807 }
5808
5809 /* Add a flag value attribute to a DIE.  */
5810
5811 static inline void
5812 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5813 {
5814   dw_attr_node attr;
5815
5816   attr.dw_attr = attr_kind;
5817   attr.dw_attr_val.val_class = dw_val_class_flag;
5818   attr.dw_attr_val.v.val_flag = flag;
5819   add_dwarf_attr (die, &attr);
5820 }
5821
5822 static inline unsigned
5823 AT_flag (dw_attr_ref a)
5824 {
5825   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5826   return a->dw_attr_val.v.val_flag;
5827 }
5828
5829 /* Add a signed integer attribute value to a DIE.  */
5830
5831 static inline void
5832 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5833 {
5834   dw_attr_node attr;
5835
5836   attr.dw_attr = attr_kind;
5837   attr.dw_attr_val.val_class = dw_val_class_const;
5838   attr.dw_attr_val.v.val_int = int_val;
5839   add_dwarf_attr (die, &attr);
5840 }
5841
5842 static inline HOST_WIDE_INT
5843 AT_int (dw_attr_ref a)
5844 {
5845   gcc_assert (a && AT_class (a) == dw_val_class_const);
5846   return a->dw_attr_val.v.val_int;
5847 }
5848
5849 /* Add an unsigned integer attribute value to a DIE.  */
5850
5851 static inline void
5852 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5853                  unsigned HOST_WIDE_INT unsigned_val)
5854 {
5855   dw_attr_node attr;
5856
5857   attr.dw_attr = attr_kind;
5858   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5859   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5860   add_dwarf_attr (die, &attr);
5861 }
5862
5863 static inline unsigned HOST_WIDE_INT
5864 AT_unsigned (dw_attr_ref a)
5865 {
5866   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5867   return a->dw_attr_val.v.val_unsigned;
5868 }
5869
5870 /* Add an unsigned double integer attribute value to a DIE.  */
5871
5872 static inline void
5873 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5874                   long unsigned int val_hi, long unsigned int val_low)
5875 {
5876   dw_attr_node attr;
5877
5878   attr.dw_attr = attr_kind;
5879   attr.dw_attr_val.val_class = dw_val_class_long_long;
5880   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5881   attr.dw_attr_val.v.val_long_long.low = val_low;
5882   add_dwarf_attr (die, &attr);
5883 }
5884
5885 /* Add a floating point attribute value to a DIE and return it.  */
5886
5887 static inline void
5888 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5889             unsigned int length, unsigned int elt_size, unsigned char *array)
5890 {
5891   dw_attr_node attr;
5892
5893   attr.dw_attr = attr_kind;
5894   attr.dw_attr_val.val_class = dw_val_class_vec;
5895   attr.dw_attr_val.v.val_vec.length = length;
5896   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5897   attr.dw_attr_val.v.val_vec.array = array;
5898   add_dwarf_attr (die, &attr);
5899 }
5900
5901 /* Hash and equality functions for debug_str_hash.  */
5902
5903 static hashval_t
5904 debug_str_do_hash (const void *x)
5905 {
5906   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5907 }
5908
5909 static int
5910 debug_str_eq (const void *x1, const void *x2)
5911 {
5912   return strcmp ((((const struct indirect_string_node *)x1)->str),
5913                  (const char *)x2) == 0;
5914 }
5915
5916 /* Add a string attribute value to a DIE.  */
5917
5918 static inline void
5919 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5920 {
5921   dw_attr_node attr;
5922   struct indirect_string_node *node;
5923   void **slot;
5924
5925   if (! debug_str_hash)
5926     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5927                                       debug_str_eq, NULL);
5928
5929   slot = htab_find_slot_with_hash (debug_str_hash, str,
5930                                    htab_hash_string (str), INSERT);
5931   if (*slot == NULL)
5932     {
5933       node = (struct indirect_string_node *)
5934                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5935       node->str = ggc_strdup (str);
5936       *slot = node;
5937     }
5938   else
5939     node = (struct indirect_string_node *) *slot;
5940
5941   node->refcount++;
5942
5943   attr.dw_attr = attr_kind;
5944   attr.dw_attr_val.val_class = dw_val_class_str;
5945   attr.dw_attr_val.v.val_str = node;
5946   add_dwarf_attr (die, &attr);
5947 }
5948
5949 static inline const char *
5950 AT_string (dw_attr_ref a)
5951 {
5952   gcc_assert (a && AT_class (a) == dw_val_class_str);
5953   return a->dw_attr_val.v.val_str->str;
5954 }
5955
5956 /* Find out whether a string should be output inline in DIE
5957    or out-of-line in .debug_str section.  */
5958
5959 static int
5960 AT_string_form (dw_attr_ref a)
5961 {
5962   struct indirect_string_node *node;
5963   unsigned int len;
5964   char label[32];
5965
5966   gcc_assert (a && AT_class (a) == dw_val_class_str);
5967
5968   node = a->dw_attr_val.v.val_str;
5969   if (node->form)
5970     return node->form;
5971
5972   len = strlen (node->str) + 1;
5973
5974   /* If the string is shorter or equal to the size of the reference, it is
5975      always better to put it inline.  */
5976   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5977     return node->form = DW_FORM_string;
5978
5979   /* If we cannot expect the linker to merge strings in .debug_str
5980      section, only put it into .debug_str if it is worth even in this
5981      single module.  */
5982   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5983       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5984     return node->form = DW_FORM_string;
5985
5986   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5987   ++dw2_string_counter;
5988   node->label = xstrdup (label);
5989
5990   return node->form = DW_FORM_strp;
5991 }
5992
5993 /* Add a DIE reference attribute value to a DIE.  */
5994
5995 static inline void
5996 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5997 {
5998   dw_attr_node attr;
5999
6000   attr.dw_attr = attr_kind;
6001   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6002   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6003   attr.dw_attr_val.v.val_die_ref.external = 0;
6004   add_dwarf_attr (die, &attr);
6005 }
6006
6007 /* Add an AT_specification attribute to a DIE, and also make the back
6008    pointer from the specification to the definition.  */
6009
6010 static inline void
6011 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6012 {
6013   add_AT_die_ref (die, DW_AT_specification, targ_die);
6014   gcc_assert (!targ_die->die_definition);
6015   targ_die->die_definition = die;
6016 }
6017
6018 static inline dw_die_ref
6019 AT_ref (dw_attr_ref a)
6020 {
6021   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6022   return a->dw_attr_val.v.val_die_ref.die;
6023 }
6024
6025 static inline int
6026 AT_ref_external (dw_attr_ref a)
6027 {
6028   if (a && AT_class (a) == dw_val_class_die_ref)
6029     return a->dw_attr_val.v.val_die_ref.external;
6030
6031   return 0;
6032 }
6033
6034 static inline void
6035 set_AT_ref_external (dw_attr_ref a, int i)
6036 {
6037   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6038   a->dw_attr_val.v.val_die_ref.external = i;
6039 }
6040
6041 /* Add an FDE reference attribute value to a DIE.  */
6042
6043 static inline void
6044 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6045 {
6046   dw_attr_node attr;
6047
6048   attr.dw_attr = attr_kind;
6049   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6050   attr.dw_attr_val.v.val_fde_index = targ_fde;
6051   add_dwarf_attr (die, &attr);
6052 }
6053
6054 /* Add a location description attribute value to a DIE.  */
6055
6056 static inline void
6057 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6058 {
6059   dw_attr_node attr;
6060
6061   attr.dw_attr = attr_kind;
6062   attr.dw_attr_val.val_class = dw_val_class_loc;
6063   attr.dw_attr_val.v.val_loc = loc;
6064   add_dwarf_attr (die, &attr);
6065 }
6066
6067 static inline dw_loc_descr_ref
6068 AT_loc (dw_attr_ref a)
6069 {
6070   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6071   return a->dw_attr_val.v.val_loc;
6072 }
6073
6074 static inline void
6075 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6076 {
6077   dw_attr_node attr;
6078
6079   attr.dw_attr = attr_kind;
6080   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6081   attr.dw_attr_val.v.val_loc_list = loc_list;
6082   add_dwarf_attr (die, &attr);
6083   have_location_lists = true;
6084 }
6085
6086 static inline dw_loc_list_ref
6087 AT_loc_list (dw_attr_ref a)
6088 {
6089   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6090   return a->dw_attr_val.v.val_loc_list;
6091 }
6092
6093 /* Add an address constant attribute value to a DIE.  */
6094
6095 static inline void
6096 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6097 {
6098   dw_attr_node attr;
6099
6100   attr.dw_attr = attr_kind;
6101   attr.dw_attr_val.val_class = dw_val_class_addr;
6102   attr.dw_attr_val.v.val_addr = addr;
6103   add_dwarf_attr (die, &attr);
6104 }
6105
6106 /* Get the RTX from to an address DIE attribute.  */
6107
6108 static inline rtx
6109 AT_addr (dw_attr_ref a)
6110 {
6111   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6112   return a->dw_attr_val.v.val_addr;
6113 }
6114
6115 /* Add a file attribute value to a DIE.  */
6116
6117 static inline void
6118 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6119              struct dwarf_file_data *fd)
6120 {
6121   dw_attr_node attr;
6122
6123   attr.dw_attr = attr_kind;
6124   attr.dw_attr_val.val_class = dw_val_class_file;
6125   attr.dw_attr_val.v.val_file = fd;
6126   add_dwarf_attr (die, &attr);
6127 }
6128
6129 /* Get the dwarf_file_data from a file DIE attribute.  */
6130
6131 static inline struct dwarf_file_data *
6132 AT_file (dw_attr_ref a)
6133 {
6134   gcc_assert (a && AT_class (a) == dw_val_class_file);
6135   return a->dw_attr_val.v.val_file;
6136 }
6137
6138 /* Add a label identifier attribute value to a DIE.  */
6139
6140 static inline void
6141 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6142 {
6143   dw_attr_node attr;
6144
6145   attr.dw_attr = attr_kind;
6146   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6147   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6148   add_dwarf_attr (die, &attr);
6149 }
6150
6151 /* Add a section offset attribute value to a DIE, an offset into the
6152    debug_line section.  */
6153
6154 static inline void
6155 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6156                 const char *label)
6157 {
6158   dw_attr_node attr;
6159
6160   attr.dw_attr = attr_kind;
6161   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6162   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6163   add_dwarf_attr (die, &attr);
6164 }
6165
6166 /* Add a section offset attribute value to a DIE, an offset into the
6167    debug_macinfo section.  */
6168
6169 static inline void
6170 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6171                const char *label)
6172 {
6173   dw_attr_node attr;
6174
6175   attr.dw_attr = attr_kind;
6176   attr.dw_attr_val.val_class = dw_val_class_macptr;
6177   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6178   add_dwarf_attr (die, &attr);
6179 }
6180
6181 /* Add an offset attribute value to a DIE.  */
6182
6183 static inline void
6184 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6185                unsigned HOST_WIDE_INT offset)
6186 {
6187   dw_attr_node attr;
6188
6189   attr.dw_attr = attr_kind;
6190   attr.dw_attr_val.val_class = dw_val_class_offset;
6191   attr.dw_attr_val.v.val_offset = offset;
6192   add_dwarf_attr (die, &attr);
6193 }
6194
6195 /* Add an range_list attribute value to a DIE.  */
6196
6197 static void
6198 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6199                    long unsigned int offset)
6200 {
6201   dw_attr_node attr;
6202
6203   attr.dw_attr = attr_kind;
6204   attr.dw_attr_val.val_class = dw_val_class_range_list;
6205   attr.dw_attr_val.v.val_offset = offset;
6206   add_dwarf_attr (die, &attr);
6207 }
6208
6209 static inline const char *
6210 AT_lbl (dw_attr_ref a)
6211 {
6212   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6213                     || AT_class (a) == dw_val_class_lineptr
6214                     || AT_class (a) == dw_val_class_macptr));
6215   return a->dw_attr_val.v.val_lbl_id;
6216 }
6217
6218 /* Get the attribute of type attr_kind.  */
6219
6220 static dw_attr_ref
6221 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6222 {
6223   dw_attr_ref a;
6224   unsigned ix;
6225   dw_die_ref spec = NULL;
6226
6227   if (! die)
6228     return NULL;
6229
6230   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6231     if (a->dw_attr == attr_kind)
6232       return a;
6233     else if (a->dw_attr == DW_AT_specification
6234              || a->dw_attr == DW_AT_abstract_origin)
6235       spec = AT_ref (a);
6236
6237   if (spec)
6238     return get_AT (spec, attr_kind);
6239
6240   return NULL;
6241 }
6242
6243 /* Return the "low pc" attribute value, typically associated with a subprogram
6244    DIE.  Return null if the "low pc" attribute is either not present, or if it
6245    cannot be represented as an assembler label identifier.  */
6246
6247 static inline const char *
6248 get_AT_low_pc (dw_die_ref die)
6249 {
6250   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6251
6252   return a ? AT_lbl (a) : NULL;
6253 }
6254
6255 /* Return the "high pc" attribute value, typically associated with a subprogram
6256    DIE.  Return null if the "high pc" attribute is either not present, or if it
6257    cannot be represented as an assembler label identifier.  */
6258
6259 static inline const char *
6260 get_AT_hi_pc (dw_die_ref die)
6261 {
6262   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6263
6264   return a ? AT_lbl (a) : NULL;
6265 }
6266
6267 /* Return the value of the string attribute designated by ATTR_KIND, or
6268    NULL if it is not present.  */
6269
6270 static inline const char *
6271 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6272 {
6273   dw_attr_ref a = get_AT (die, attr_kind);
6274
6275   return a ? AT_string (a) : NULL;
6276 }
6277
6278 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6279    if it is not present.  */
6280
6281 static inline int
6282 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6283 {
6284   dw_attr_ref a = get_AT (die, attr_kind);
6285
6286   return a ? AT_flag (a) : 0;
6287 }
6288
6289 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6290    if it is not present.  */
6291
6292 static inline unsigned
6293 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6294 {
6295   dw_attr_ref a = get_AT (die, attr_kind);
6296
6297   return a ? AT_unsigned (a) : 0;
6298 }
6299
6300 static inline dw_die_ref
6301 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6302 {
6303   dw_attr_ref a = get_AT (die, attr_kind);
6304
6305   return a ? AT_ref (a) : NULL;
6306 }
6307
6308 static inline struct dwarf_file_data *
6309 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6310 {
6311   dw_attr_ref a = get_AT (die, attr_kind);
6312
6313   return a ? AT_file (a) : NULL;
6314 }
6315
6316 /* Return TRUE if the language is C or C++.  */
6317
6318 static inline bool
6319 is_c_family (void)
6320 {
6321   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6322
6323   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6324           || lang == DW_LANG_C99
6325           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6326 }
6327
6328 /* Return TRUE if the language is C++.  */
6329
6330 static inline bool
6331 is_cxx (void)
6332 {
6333   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6334
6335   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6336 }
6337
6338 /* Return TRUE if the language is Fortran.  */
6339
6340 static inline bool
6341 is_fortran (void)
6342 {
6343   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6344
6345   return (lang == DW_LANG_Fortran77
6346           || lang == DW_LANG_Fortran90
6347           || lang == DW_LANG_Fortran95);
6348 }
6349
6350 /* Return TRUE if the language is Java.  */
6351
6352 static inline bool
6353 is_java (void)
6354 {
6355   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6356
6357   return lang == DW_LANG_Java;
6358 }
6359
6360 /* Return TRUE if the language is Ada.  */
6361
6362 static inline bool
6363 is_ada (void)
6364 {
6365   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6366
6367   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6368 }
6369
6370 /* Remove the specified attribute if present.  */
6371
6372 static void
6373 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6374 {
6375   dw_attr_ref a;
6376   unsigned ix;
6377
6378   if (! die)
6379     return;
6380
6381   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6382     if (a->dw_attr == attr_kind)
6383       {
6384         if (AT_class (a) == dw_val_class_str)
6385           if (a->dw_attr_val.v.val_str->refcount)
6386             a->dw_attr_val.v.val_str->refcount--;
6387
6388         /* VEC_ordered_remove should help reduce the number of abbrevs
6389            that are needed.  */
6390         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6391         return;
6392       }
6393 }
6394
6395 /* Remove CHILD from its parent.  PREV must have the property that
6396    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6397
6398 static void
6399 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6400 {
6401   gcc_assert (child->die_parent == prev->die_parent);
6402   gcc_assert (prev->die_sib == child);
6403   if (prev == child)
6404     {
6405       gcc_assert (child->die_parent->die_child == child);
6406       prev = NULL;
6407     }
6408   else
6409     prev->die_sib = child->die_sib;
6410   if (child->die_parent->die_child == child)
6411     child->die_parent->die_child = prev;
6412 }
6413
6414 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6415    matches TAG.  */
6416
6417 static void
6418 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6419 {
6420   dw_die_ref c;
6421
6422   c = die->die_child;
6423   if (c) do {
6424     dw_die_ref prev = c;
6425     c = c->die_sib;
6426     while (c->die_tag == tag)
6427       {
6428         remove_child_with_prev (c, prev);
6429         /* Might have removed every child.  */
6430         if (c == c->die_sib)
6431           return;
6432         c = c->die_sib;
6433       }
6434   } while (c != die->die_child);
6435 }
6436
6437 /* Add a CHILD_DIE as the last child of DIE.  */
6438
6439 static void
6440 add_child_die (dw_die_ref die, dw_die_ref child_die)
6441 {
6442   /* FIXME this should probably be an assert.  */
6443   if (! die || ! child_die)
6444     return;
6445   gcc_assert (die != child_die);
6446
6447   child_die->die_parent = die;
6448   if (die->die_child)
6449     {
6450       child_die->die_sib = die->die_child->die_sib;
6451       die->die_child->die_sib = child_die;
6452     }
6453   else
6454     child_die->die_sib = child_die;
6455   die->die_child = child_die;
6456 }
6457
6458 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6459    is the specification, to the end of PARENT's list of children.
6460    This is done by removing and re-adding it.  */
6461
6462 static void
6463 splice_child_die (dw_die_ref parent, dw_die_ref child)
6464 {
6465   dw_die_ref p;
6466
6467   /* We want the declaration DIE from inside the class, not the
6468      specification DIE at toplevel.  */
6469   if (child->die_parent != parent)
6470     {
6471       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6472
6473       if (tmp)
6474         child = tmp;
6475     }
6476
6477   gcc_assert (child->die_parent == parent
6478               || (child->die_parent
6479                   == get_AT_ref (parent, DW_AT_specification)));
6480
6481   for (p = child->die_parent->die_child; ; p = p->die_sib)
6482     if (p->die_sib == child)
6483       {
6484         remove_child_with_prev (child, p);
6485         break;
6486       }
6487
6488   add_child_die (parent, child);
6489 }
6490
6491 /* Return a pointer to a newly created DIE node.  */
6492
6493 static inline dw_die_ref
6494 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6495 {
6496   dw_die_ref die = GGC_CNEW (die_node);
6497
6498   die->die_tag = tag_value;
6499
6500   if (parent_die != NULL)
6501     add_child_die (parent_die, die);
6502   else
6503     {
6504       limbo_die_node *limbo_node;
6505
6506       limbo_node = GGC_CNEW (limbo_die_node);
6507       limbo_node->die = die;
6508       limbo_node->created_for = t;
6509       limbo_node->next = limbo_die_list;
6510       limbo_die_list = limbo_node;
6511     }
6512
6513   return die;
6514 }
6515
6516 /* Return the DIE associated with the given type specifier.  */
6517
6518 static inline dw_die_ref
6519 lookup_type_die (tree type)
6520 {
6521   return TYPE_SYMTAB_DIE (type);
6522 }
6523
6524 /* Equate a DIE to a given type specifier.  */
6525
6526 static inline void
6527 equate_type_number_to_die (tree type, dw_die_ref type_die)
6528 {
6529   TYPE_SYMTAB_DIE (type) = type_die;
6530 }
6531
6532 /* Returns a hash value for X (which really is a die_struct).  */
6533
6534 static hashval_t
6535 decl_die_table_hash (const void *x)
6536 {
6537   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6538 }
6539
6540 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6541
6542 static int
6543 decl_die_table_eq (const void *x, const void *y)
6544 {
6545   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6546 }
6547
6548 /* Return the DIE associated with a given declaration.  */
6549
6550 static inline dw_die_ref
6551 lookup_decl_die (tree decl)
6552 {
6553   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6554 }
6555
6556 /* Returns a hash value for X (which really is a var_loc_list).  */
6557
6558 static hashval_t
6559 decl_loc_table_hash (const void *x)
6560 {
6561   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6562 }
6563
6564 /* Return nonzero if decl_id of var_loc_list X is the same as
6565    UID of decl *Y.  */
6566
6567 static int
6568 decl_loc_table_eq (const void *x, const void *y)
6569 {
6570   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6571 }
6572
6573 /* Return the var_loc list associated with a given declaration.  */
6574
6575 static inline var_loc_list *
6576 lookup_decl_loc (const_tree decl)
6577 {
6578   return (var_loc_list *)
6579     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6580 }
6581
6582 /* Equate a DIE to a particular declaration.  */
6583
6584 static void
6585 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6586 {
6587   unsigned int decl_id = DECL_UID (decl);
6588   void **slot;
6589
6590   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6591   *slot = decl_die;
6592   decl_die->decl_id = decl_id;
6593 }
6594
6595 /* Add a variable location node to the linked list for DECL.  */
6596
6597 static void
6598 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6599 {
6600   unsigned int decl_id = DECL_UID (decl);
6601   var_loc_list *temp;
6602   void **slot;
6603
6604   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6605   if (*slot == NULL)
6606     {
6607       temp = GGC_CNEW (var_loc_list);
6608       temp->decl_id = decl_id;
6609       *slot = temp;
6610     }
6611   else
6612     temp = (var_loc_list *) *slot;
6613
6614   if (temp->last)
6615     {
6616       /* If the current location is the same as the end of the list,
6617          and either both or neither of the locations is uninitialized,
6618          we have nothing to do.  */
6619       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6620                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6621           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6622                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6623               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6624                    == VAR_INIT_STATUS_UNINITIALIZED)
6625                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6626                       == VAR_INIT_STATUS_UNINITIALIZED))))
6627         {
6628           /* Add LOC to the end of list and update LAST.  */
6629           temp->last->next = loc;
6630           temp->last = loc;
6631         }
6632     }
6633   /* Do not add empty location to the beginning of the list.  */
6634   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6635     {
6636       temp->first = loc;
6637       temp->last = loc;
6638     }
6639 }
6640 \f
6641 /* Keep track of the number of spaces used to indent the
6642    output of the debugging routines that print the structure of
6643    the DIE internal representation.  */
6644 static int print_indent;
6645
6646 /* Indent the line the number of spaces given by print_indent.  */
6647
6648 static inline void
6649 print_spaces (FILE *outfile)
6650 {
6651   fprintf (outfile, "%*s", print_indent, "");
6652 }
6653
6654 /* Print the information associated with a given DIE, and its children.
6655    This routine is a debugging aid only.  */
6656
6657 static void
6658 print_die (dw_die_ref die, FILE *outfile)
6659 {
6660   dw_attr_ref a;
6661   dw_die_ref c;
6662   unsigned ix;
6663
6664   print_spaces (outfile);
6665   fprintf (outfile, "DIE %4ld: %s\n",
6666            die->die_offset, dwarf_tag_name (die->die_tag));
6667   print_spaces (outfile);
6668   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6669   fprintf (outfile, " offset: %ld\n", die->die_offset);
6670
6671   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6672     {
6673       print_spaces (outfile);
6674       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6675
6676       switch (AT_class (a))
6677         {
6678         case dw_val_class_addr:
6679           fprintf (outfile, "address");
6680           break;
6681         case dw_val_class_offset:
6682           fprintf (outfile, "offset");
6683           break;
6684         case dw_val_class_loc:
6685           fprintf (outfile, "location descriptor");
6686           break;
6687         case dw_val_class_loc_list:
6688           fprintf (outfile, "location list -> label:%s",
6689                    AT_loc_list (a)->ll_symbol);
6690           break;
6691         case dw_val_class_range_list:
6692           fprintf (outfile, "range list");
6693           break;
6694         case dw_val_class_const:
6695           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6696           break;
6697         case dw_val_class_unsigned_const:
6698           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6699           break;
6700         case dw_val_class_long_long:
6701           fprintf (outfile, "constant (%lu,%lu)",
6702                    a->dw_attr_val.v.val_long_long.hi,
6703                    a->dw_attr_val.v.val_long_long.low);
6704           break;
6705         case dw_val_class_vec:
6706           fprintf (outfile, "floating-point or vector constant");
6707           break;
6708         case dw_val_class_flag:
6709           fprintf (outfile, "%u", AT_flag (a));
6710           break;
6711         case dw_val_class_die_ref:
6712           if (AT_ref (a) != NULL)
6713             {
6714               if (AT_ref (a)->die_symbol)
6715                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
6716               else
6717                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6718             }
6719           else
6720             fprintf (outfile, "die -> <null>");
6721           break;
6722         case dw_val_class_lbl_id:
6723         case dw_val_class_lineptr:
6724         case dw_val_class_macptr:
6725           fprintf (outfile, "label: %s", AT_lbl (a));
6726           break;
6727         case dw_val_class_str:
6728           if (AT_string (a) != NULL)
6729             fprintf (outfile, "\"%s\"", AT_string (a));
6730           else
6731             fprintf (outfile, "<null>");
6732           break;
6733         case dw_val_class_file:
6734           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6735                    AT_file (a)->emitted_number);
6736           break;
6737         default:
6738           break;
6739         }
6740
6741       fprintf (outfile, "\n");
6742     }
6743
6744   if (die->die_child != NULL)
6745     {
6746       print_indent += 4;
6747       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6748       print_indent -= 4;
6749     }
6750   if (print_indent == 0)
6751     fprintf (outfile, "\n");
6752 }
6753
6754 /* Print the contents of the source code line number correspondence table.
6755    This routine is a debugging aid only.  */
6756
6757 static void
6758 print_dwarf_line_table (FILE *outfile)
6759 {
6760   unsigned i;
6761   dw_line_info_ref line_info;
6762
6763   fprintf (outfile, "\n\nDWARF source line information\n");
6764   for (i = 1; i < line_info_table_in_use; i++)
6765     {
6766       line_info = &line_info_table[i];
6767       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6768                line_info->dw_file_num,
6769                line_info->dw_line_num);
6770     }
6771
6772   fprintf (outfile, "\n\n");
6773 }
6774
6775 /* Print the information collected for a given DIE.  */
6776
6777 void
6778 debug_dwarf_die (dw_die_ref die)
6779 {
6780   print_die (die, stderr);
6781 }
6782
6783 /* Print all DWARF information collected for the compilation unit.
6784    This routine is a debugging aid only.  */
6785
6786 void
6787 debug_dwarf (void)
6788 {
6789   print_indent = 0;
6790   print_die (comp_unit_die, stderr);
6791   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6792     print_dwarf_line_table (stderr);
6793 }
6794 \f
6795 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6796    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6797    DIE that marks the start of the DIEs for this include file.  */
6798
6799 static dw_die_ref
6800 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6801 {
6802   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6803   dw_die_ref new_unit = gen_compile_unit_die (filename);
6804
6805   new_unit->die_sib = old_unit;
6806   return new_unit;
6807 }
6808
6809 /* Close an include-file CU and reopen the enclosing one.  */
6810
6811 static dw_die_ref
6812 pop_compile_unit (dw_die_ref old_unit)
6813 {
6814   dw_die_ref new_unit = old_unit->die_sib;
6815
6816   old_unit->die_sib = NULL;
6817   return new_unit;
6818 }
6819
6820 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6821 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6822
6823 /* Calculate the checksum of a location expression.  */
6824
6825 static inline void
6826 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6827 {
6828   CHECKSUM (loc->dw_loc_opc);
6829   CHECKSUM (loc->dw_loc_oprnd1);
6830   CHECKSUM (loc->dw_loc_oprnd2);
6831 }
6832
6833 /* Calculate the checksum of an attribute.  */
6834
6835 static void
6836 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6837 {
6838   dw_loc_descr_ref loc;
6839   rtx r;
6840
6841   CHECKSUM (at->dw_attr);
6842
6843   /* We don't care that this was compiled with a different compiler
6844      snapshot; if the output is the same, that's what matters.  */
6845   if (at->dw_attr == DW_AT_producer)
6846     return;
6847
6848   switch (AT_class (at))
6849     {
6850     case dw_val_class_const:
6851       CHECKSUM (at->dw_attr_val.v.val_int);
6852       break;
6853     case dw_val_class_unsigned_const:
6854       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6855       break;
6856     case dw_val_class_long_long:
6857       CHECKSUM (at->dw_attr_val.v.val_long_long);
6858       break;
6859     case dw_val_class_vec:
6860       CHECKSUM (at->dw_attr_val.v.val_vec);
6861       break;
6862     case dw_val_class_flag:
6863       CHECKSUM (at->dw_attr_val.v.val_flag);
6864       break;
6865     case dw_val_class_str:
6866       CHECKSUM_STRING (AT_string (at));
6867       break;
6868
6869     case dw_val_class_addr:
6870       r = AT_addr (at);
6871       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6872       CHECKSUM_STRING (XSTR (r, 0));
6873       break;
6874
6875     case dw_val_class_offset:
6876       CHECKSUM (at->dw_attr_val.v.val_offset);
6877       break;
6878
6879     case dw_val_class_loc:
6880       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6881         loc_checksum (loc, ctx);
6882       break;
6883
6884     case dw_val_class_die_ref:
6885       die_checksum (AT_ref (at), ctx, mark);
6886       break;
6887
6888     case dw_val_class_fde_ref:
6889     case dw_val_class_lbl_id:
6890     case dw_val_class_lineptr:
6891     case dw_val_class_macptr:
6892       break;
6893
6894     case dw_val_class_file:
6895       CHECKSUM_STRING (AT_file (at)->filename);
6896       break;
6897
6898     default:
6899       break;
6900     }
6901 }
6902
6903 /* Calculate the checksum of a DIE.  */
6904
6905 static void
6906 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6907 {
6908   dw_die_ref c;
6909   dw_attr_ref a;
6910   unsigned ix;
6911
6912   /* To avoid infinite recursion.  */
6913   if (die->die_mark)
6914     {
6915       CHECKSUM (die->die_mark);
6916       return;
6917     }
6918   die->die_mark = ++(*mark);
6919
6920   CHECKSUM (die->die_tag);
6921
6922   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6923     attr_checksum (a, ctx, mark);
6924
6925   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6926 }
6927
6928 #undef CHECKSUM
6929 #undef CHECKSUM_STRING
6930
6931 /* Do the location expressions look same?  */
6932 static inline int
6933 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6934 {
6935   return loc1->dw_loc_opc == loc2->dw_loc_opc
6936          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6937          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6938 }
6939
6940 /* Do the values look the same?  */
6941 static int
6942 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6943 {
6944   dw_loc_descr_ref loc1, loc2;
6945   rtx r1, r2;
6946
6947   if (v1->val_class != v2->val_class)
6948     return 0;
6949
6950   switch (v1->val_class)
6951     {
6952     case dw_val_class_const:
6953       return v1->v.val_int == v2->v.val_int;
6954     case dw_val_class_unsigned_const:
6955       return v1->v.val_unsigned == v2->v.val_unsigned;
6956     case dw_val_class_long_long:
6957       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6958              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6959     case dw_val_class_vec:
6960       if (v1->v.val_vec.length != v2->v.val_vec.length
6961           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6962         return 0;
6963       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6964                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6965         return 0;
6966       return 1;
6967     case dw_val_class_flag:
6968       return v1->v.val_flag == v2->v.val_flag;
6969     case dw_val_class_str:
6970       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6971
6972     case dw_val_class_addr:
6973       r1 = v1->v.val_addr;
6974       r2 = v2->v.val_addr;
6975       if (GET_CODE (r1) != GET_CODE (r2))
6976         return 0;
6977       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6978       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6979
6980     case dw_val_class_offset:
6981       return v1->v.val_offset == v2->v.val_offset;
6982
6983     case dw_val_class_loc:
6984       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6985            loc1 && loc2;
6986            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6987         if (!same_loc_p (loc1, loc2, mark))
6988           return 0;
6989       return !loc1 && !loc2;
6990
6991     case dw_val_class_die_ref:
6992       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6993
6994     case dw_val_class_fde_ref:
6995     case dw_val_class_lbl_id:
6996     case dw_val_class_lineptr:
6997     case dw_val_class_macptr:
6998       return 1;
6999
7000     case dw_val_class_file:
7001       return v1->v.val_file == v2->v.val_file;
7002
7003     default:
7004       return 1;
7005     }
7006 }
7007
7008 /* Do the attributes look the same?  */
7009
7010 static int
7011 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7012 {
7013   if (at1->dw_attr != at2->dw_attr)
7014     return 0;
7015
7016   /* We don't care that this was compiled with a different compiler
7017      snapshot; if the output is the same, that's what matters. */
7018   if (at1->dw_attr == DW_AT_producer)
7019     return 1;
7020
7021   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7022 }
7023
7024 /* Do the dies look the same?  */
7025
7026 static int
7027 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7028 {
7029   dw_die_ref c1, c2;
7030   dw_attr_ref a1;
7031   unsigned ix;
7032
7033   /* To avoid infinite recursion.  */
7034   if (die1->die_mark)
7035     return die1->die_mark == die2->die_mark;
7036   die1->die_mark = die2->die_mark = ++(*mark);
7037
7038   if (die1->die_tag != die2->die_tag)
7039     return 0;
7040
7041   if (VEC_length (dw_attr_node, die1->die_attr)
7042       != VEC_length (dw_attr_node, die2->die_attr))
7043     return 0;
7044
7045   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7046     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7047       return 0;
7048
7049   c1 = die1->die_child;
7050   c2 = die2->die_child;
7051   if (! c1)
7052     {
7053       if (c2)
7054         return 0;
7055     }
7056   else
7057     for (;;)
7058       {
7059         if (!same_die_p (c1, c2, mark))
7060           return 0;
7061         c1 = c1->die_sib;
7062         c2 = c2->die_sib;
7063         if (c1 == die1->die_child)
7064           {
7065             if (c2 == die2->die_child)
7066               break;
7067             else
7068               return 0;
7069           }
7070     }
7071
7072   return 1;
7073 }
7074
7075 /* Do the dies look the same?  Wrapper around same_die_p.  */
7076
7077 static int
7078 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7079 {
7080   int mark = 0;
7081   int ret = same_die_p (die1, die2, &mark);
7082
7083   unmark_all_dies (die1);
7084   unmark_all_dies (die2);
7085
7086   return ret;
7087 }
7088
7089 /* The prefix to attach to symbols on DIEs in the current comdat debug
7090    info section.  */
7091 static char *comdat_symbol_id;
7092
7093 /* The index of the current symbol within the current comdat CU.  */
7094 static unsigned int comdat_symbol_number;
7095
7096 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7097    children, and set comdat_symbol_id accordingly.  */
7098
7099 static void
7100 compute_section_prefix (dw_die_ref unit_die)
7101 {
7102   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7103   const char *base = die_name ? lbasename (die_name) : "anonymous";
7104   char *name = XALLOCAVEC (char, strlen (base) + 64);
7105   char *p;
7106   int i, mark;
7107   unsigned char checksum[16];
7108   struct md5_ctx ctx;
7109
7110   /* Compute the checksum of the DIE, then append part of it as hex digits to
7111      the name filename of the unit.  */
7112
7113   md5_init_ctx (&ctx);
7114   mark = 0;
7115   die_checksum (unit_die, &ctx, &mark);
7116   unmark_all_dies (unit_die);
7117   md5_finish_ctx (&ctx, checksum);
7118
7119   sprintf (name, "%s.", base);
7120   clean_symbol_name (name);
7121
7122   p = name + strlen (name);
7123   for (i = 0; i < 4; i++)
7124     {
7125       sprintf (p, "%.2x", checksum[i]);
7126       p += 2;
7127     }
7128
7129   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7130   comdat_symbol_number = 0;
7131 }
7132
7133 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7134
7135 static int
7136 is_type_die (dw_die_ref die)
7137 {
7138   switch (die->die_tag)
7139     {
7140     case DW_TAG_array_type:
7141     case DW_TAG_class_type:
7142     case DW_TAG_interface_type:
7143     case DW_TAG_enumeration_type:
7144     case DW_TAG_pointer_type:
7145     case DW_TAG_reference_type:
7146     case DW_TAG_string_type:
7147     case DW_TAG_structure_type:
7148     case DW_TAG_subroutine_type:
7149     case DW_TAG_union_type:
7150     case DW_TAG_ptr_to_member_type:
7151     case DW_TAG_set_type:
7152     case DW_TAG_subrange_type:
7153     case DW_TAG_base_type:
7154     case DW_TAG_const_type:
7155     case DW_TAG_file_type:
7156     case DW_TAG_packed_type:
7157     case DW_TAG_volatile_type:
7158     case DW_TAG_typedef:
7159       return 1;
7160     default:
7161       return 0;
7162     }
7163 }
7164
7165 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7166    Basically, we want to choose the bits that are likely to be shared between
7167    compilations (types) and leave out the bits that are specific to individual
7168    compilations (functions).  */
7169
7170 static int
7171 is_comdat_die (dw_die_ref c)
7172 {
7173   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7174      we do for stabs.  The advantage is a greater likelihood of sharing between
7175      objects that don't include headers in the same order (and therefore would
7176      put the base types in a different comdat).  jason 8/28/00 */
7177
7178   if (c->die_tag == DW_TAG_base_type)
7179     return 0;
7180
7181   if (c->die_tag == DW_TAG_pointer_type
7182       || c->die_tag == DW_TAG_reference_type
7183       || c->die_tag == DW_TAG_const_type
7184       || c->die_tag == DW_TAG_volatile_type)
7185     {
7186       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7187
7188       return t ? is_comdat_die (t) : 0;
7189     }
7190
7191   return is_type_die (c);
7192 }
7193
7194 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7195    compilation unit.  */
7196
7197 static int
7198 is_symbol_die (dw_die_ref c)
7199 {
7200   return (is_type_die (c)
7201           || (get_AT (c, DW_AT_declaration)
7202               && !get_AT (c, DW_AT_specification))
7203           || c->die_tag == DW_TAG_namespace
7204           || c->die_tag == DW_TAG_module);
7205 }
7206
7207 static char *
7208 gen_internal_sym (const char *prefix)
7209 {
7210   char buf[256];
7211
7212   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7213   return xstrdup (buf);
7214 }
7215
7216 /* Assign symbols to all worthy DIEs under DIE.  */
7217
7218 static void
7219 assign_symbol_names (dw_die_ref die)
7220 {
7221   dw_die_ref c;
7222
7223   if (is_symbol_die (die))
7224     {
7225       if (comdat_symbol_id)
7226         {
7227           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7228
7229           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7230                    comdat_symbol_id, comdat_symbol_number++);
7231           die->die_symbol = xstrdup (p);
7232         }
7233       else
7234         die->die_symbol = gen_internal_sym ("LDIE");
7235     }
7236
7237   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7238 }
7239
7240 struct cu_hash_table_entry
7241 {
7242   dw_die_ref cu;
7243   unsigned min_comdat_num, max_comdat_num;
7244   struct cu_hash_table_entry *next;
7245 };
7246
7247 /* Routines to manipulate hash table of CUs.  */
7248 static hashval_t
7249 htab_cu_hash (const void *of)
7250 {
7251   const struct cu_hash_table_entry *const entry =
7252     (const struct cu_hash_table_entry *) of;
7253
7254   return htab_hash_string (entry->cu->die_symbol);
7255 }
7256
7257 static int
7258 htab_cu_eq (const void *of1, const void *of2)
7259 {
7260   const struct cu_hash_table_entry *const entry1 =
7261     (const struct cu_hash_table_entry *) of1;
7262   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7263
7264   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7265 }
7266
7267 static void
7268 htab_cu_del (void *what)
7269 {
7270   struct cu_hash_table_entry *next,
7271     *entry = (struct cu_hash_table_entry *) what;
7272
7273   while (entry)
7274     {
7275       next = entry->next;
7276       free (entry);
7277       entry = next;
7278     }
7279 }
7280
7281 /* Check whether we have already seen this CU and set up SYM_NUM
7282    accordingly.  */
7283 static int
7284 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7285 {
7286   struct cu_hash_table_entry dummy;
7287   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7288
7289   dummy.max_comdat_num = 0;
7290
7291   slot = (struct cu_hash_table_entry **)
7292     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7293         INSERT);
7294   entry = *slot;
7295
7296   for (; entry; last = entry, entry = entry->next)
7297     {
7298       if (same_die_p_wrap (cu, entry->cu))
7299         break;
7300     }
7301
7302   if (entry)
7303     {
7304       *sym_num = entry->min_comdat_num;
7305       return 1;
7306     }
7307
7308   entry = XCNEW (struct cu_hash_table_entry);
7309   entry->cu = cu;
7310   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7311   entry->next = *slot;
7312   *slot = entry;
7313
7314   return 0;
7315 }
7316
7317 /* Record SYM_NUM to record of CU in HTABLE.  */
7318 static void
7319 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7320 {
7321   struct cu_hash_table_entry **slot, *entry;
7322
7323   slot = (struct cu_hash_table_entry **)
7324     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7325         NO_INSERT);
7326   entry = *slot;
7327
7328   entry->max_comdat_num = sym_num;
7329 }
7330
7331 /* Traverse the DIE (which is always comp_unit_die), and set up
7332    additional compilation units for each of the include files we see
7333    bracketed by BINCL/EINCL.  */
7334
7335 static void
7336 break_out_includes (dw_die_ref die)
7337 {
7338   dw_die_ref c;
7339   dw_die_ref unit = NULL;
7340   limbo_die_node *node, **pnode;
7341   htab_t cu_hash_table;
7342
7343   c = die->die_child;
7344   if (c) do {
7345     dw_die_ref prev = c;
7346     c = c->die_sib;
7347     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7348            || (unit && is_comdat_die (c)))
7349       {
7350         dw_die_ref next = c->die_sib;
7351
7352         /* This DIE is for a secondary CU; remove it from the main one.  */
7353         remove_child_with_prev (c, prev);
7354
7355         if (c->die_tag == DW_TAG_GNU_BINCL)
7356           unit = push_new_compile_unit (unit, c);
7357         else if (c->die_tag == DW_TAG_GNU_EINCL)
7358           unit = pop_compile_unit (unit);
7359         else
7360           add_child_die (unit, c);
7361         c = next;
7362         if (c == die->die_child)
7363           break;
7364       }
7365   } while (c != die->die_child);
7366
7367 #if 0
7368   /* We can only use this in debugging, since the frontend doesn't check
7369      to make sure that we leave every include file we enter.  */
7370   gcc_assert (!unit);
7371 #endif
7372
7373   assign_symbol_names (die);
7374   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7375   for (node = limbo_die_list, pnode = &limbo_die_list;
7376        node;
7377        node = node->next)
7378     {
7379       int is_dupl;
7380
7381       compute_section_prefix (node->die);
7382       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7383                         &comdat_symbol_number);
7384       assign_symbol_names (node->die);
7385       if (is_dupl)
7386         *pnode = node->next;
7387       else
7388         {
7389           pnode = &node->next;
7390           record_comdat_symbol_number (node->die, cu_hash_table,
7391                 comdat_symbol_number);
7392         }
7393     }
7394   htab_delete (cu_hash_table);
7395 }
7396
7397 /* Traverse the DIE and add a sibling attribute if it may have the
7398    effect of speeding up access to siblings.  To save some space,
7399    avoid generating sibling attributes for DIE's without children.  */
7400
7401 static void
7402 add_sibling_attributes (dw_die_ref die)
7403 {
7404   dw_die_ref c;
7405
7406   if (! die->die_child)
7407     return;
7408
7409   if (die->die_parent && die != die->die_parent->die_child)
7410     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7411
7412   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7413 }
7414
7415 /* Output all location lists for the DIE and its children.  */
7416
7417 static void
7418 output_location_lists (dw_die_ref die)
7419 {
7420   dw_die_ref c;
7421   dw_attr_ref a;
7422   unsigned ix;
7423
7424   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7425     if (AT_class (a) == dw_val_class_loc_list)
7426       output_loc_list (AT_loc_list (a));
7427
7428   FOR_EACH_CHILD (die, c, output_location_lists (c));
7429 }
7430
7431 /* The format of each DIE (and its attribute value pairs) is encoded in an
7432    abbreviation table.  This routine builds the abbreviation table and assigns
7433    a unique abbreviation id for each abbreviation entry.  The children of each
7434    die are visited recursively.  */
7435
7436 static void
7437 build_abbrev_table (dw_die_ref die)
7438 {
7439   unsigned long abbrev_id;
7440   unsigned int n_alloc;
7441   dw_die_ref c;
7442   dw_attr_ref a;
7443   unsigned ix;
7444
7445   /* Scan the DIE references, and mark as external any that refer to
7446      DIEs from other CUs (i.e. those which are not marked).  */
7447   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7448     if (AT_class (a) == dw_val_class_die_ref
7449         && AT_ref (a)->die_mark == 0)
7450       {
7451         gcc_assert (AT_ref (a)->die_symbol);
7452
7453         set_AT_ref_external (a, 1);
7454       }
7455
7456   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7457     {
7458       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7459       dw_attr_ref die_a, abbrev_a;
7460       unsigned ix;
7461       bool ok = true;
7462
7463       if (abbrev->die_tag != die->die_tag)
7464         continue;
7465       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7466         continue;
7467
7468       if (VEC_length (dw_attr_node, abbrev->die_attr)
7469           != VEC_length (dw_attr_node, die->die_attr))
7470         continue;
7471
7472       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7473         {
7474           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7475           if ((abbrev_a->dw_attr != die_a->dw_attr)
7476               || (value_format (abbrev_a) != value_format (die_a)))
7477             {
7478               ok = false;
7479               break;
7480             }
7481         }
7482       if (ok)
7483         break;
7484     }
7485
7486   if (abbrev_id >= abbrev_die_table_in_use)
7487     {
7488       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7489         {
7490           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7491           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7492                                             n_alloc);
7493
7494           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7495                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7496           abbrev_die_table_allocated = n_alloc;
7497         }
7498
7499       ++abbrev_die_table_in_use;
7500       abbrev_die_table[abbrev_id] = die;
7501     }
7502
7503   die->die_abbrev = abbrev_id;
7504   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7505 }
7506 \f
7507 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7508
7509 static int
7510 constant_size (unsigned HOST_WIDE_INT value)
7511 {
7512   int log;
7513
7514   if (value == 0)
7515     log = 0;
7516   else
7517     log = floor_log2 (value);
7518
7519   log = log / 8;
7520   log = 1 << (floor_log2 (log) + 1);
7521
7522   return log;
7523 }
7524
7525 /* Return the size of a DIE as it is represented in the
7526    .debug_info section.  */
7527
7528 static unsigned long
7529 size_of_die (dw_die_ref die)
7530 {
7531   unsigned long size = 0;
7532   dw_attr_ref a;
7533   unsigned ix;
7534
7535   size += size_of_uleb128 (die->die_abbrev);
7536   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7537     {
7538       switch (AT_class (a))
7539         {
7540         case dw_val_class_addr:
7541           size += DWARF2_ADDR_SIZE;
7542           break;
7543         case dw_val_class_offset:
7544           size += DWARF_OFFSET_SIZE;
7545           break;
7546         case dw_val_class_loc:
7547           {
7548             unsigned long lsize = size_of_locs (AT_loc (a));
7549
7550             /* Block length.  */
7551             size += constant_size (lsize);
7552             size += lsize;
7553           }
7554           break;
7555         case dw_val_class_loc_list:
7556           size += DWARF_OFFSET_SIZE;
7557           break;
7558         case dw_val_class_range_list:
7559           size += DWARF_OFFSET_SIZE;
7560           break;
7561         case dw_val_class_const:
7562           size += size_of_sleb128 (AT_int (a));
7563           break;
7564         case dw_val_class_unsigned_const:
7565           size += constant_size (AT_unsigned (a));
7566           break;
7567         case dw_val_class_long_long:
7568           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7569           break;
7570         case dw_val_class_vec:
7571           size += constant_size (a->dw_attr_val.v.val_vec.length
7572                                  * a->dw_attr_val.v.val_vec.elt_size)
7573                   + a->dw_attr_val.v.val_vec.length
7574                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7575           break;
7576         case dw_val_class_flag:
7577           size += 1;
7578           break;
7579         case dw_val_class_die_ref:
7580           if (AT_ref_external (a))
7581             size += DWARF2_ADDR_SIZE;
7582           else
7583             size += DWARF_OFFSET_SIZE;
7584           break;
7585         case dw_val_class_fde_ref:
7586           size += DWARF_OFFSET_SIZE;
7587           break;
7588         case dw_val_class_lbl_id:
7589           size += DWARF2_ADDR_SIZE;
7590           break;
7591         case dw_val_class_lineptr:
7592         case dw_val_class_macptr:
7593           size += DWARF_OFFSET_SIZE;
7594           break;
7595         case dw_val_class_str:
7596           if (AT_string_form (a) == DW_FORM_strp)
7597             size += DWARF_OFFSET_SIZE;
7598           else
7599             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7600           break;
7601         case dw_val_class_file:
7602           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7603           break;
7604         default:
7605           gcc_unreachable ();
7606         }
7607     }
7608
7609   return size;
7610 }
7611
7612 /* Size the debugging information associated with a given DIE.  Visits the
7613    DIE's children recursively.  Updates the global variable next_die_offset, on
7614    each time through.  Uses the current value of next_die_offset to update the
7615    die_offset field in each DIE.  */
7616
7617 static void
7618 calc_die_sizes (dw_die_ref die)
7619 {
7620   dw_die_ref c;
7621
7622   die->die_offset = next_die_offset;
7623   next_die_offset += size_of_die (die);
7624
7625   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7626
7627   if (die->die_child != NULL)
7628     /* Count the null byte used to terminate sibling lists.  */
7629     next_die_offset += 1;
7630 }
7631
7632 /* Set the marks for a die and its children.  We do this so
7633    that we know whether or not a reference needs to use FORM_ref_addr; only
7634    DIEs in the same CU will be marked.  We used to clear out the offset
7635    and use that as the flag, but ran into ordering problems.  */
7636
7637 static void
7638 mark_dies (dw_die_ref die)
7639 {
7640   dw_die_ref c;
7641
7642   gcc_assert (!die->die_mark);
7643
7644   die->die_mark = 1;
7645   FOR_EACH_CHILD (die, c, mark_dies (c));
7646 }
7647
7648 /* Clear the marks for a die and its children.  */
7649
7650 static void
7651 unmark_dies (dw_die_ref die)
7652 {
7653   dw_die_ref c;
7654
7655   gcc_assert (die->die_mark);
7656
7657   die->die_mark = 0;
7658   FOR_EACH_CHILD (die, c, unmark_dies (c));
7659 }
7660
7661 /* Clear the marks for a die, its children and referred dies.  */
7662
7663 static void
7664 unmark_all_dies (dw_die_ref die)
7665 {
7666   dw_die_ref c;
7667   dw_attr_ref a;
7668   unsigned ix;
7669
7670   if (!die->die_mark)
7671     return;
7672   die->die_mark = 0;
7673
7674   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7675
7676   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7677     if (AT_class (a) == dw_val_class_die_ref)
7678       unmark_all_dies (AT_ref (a));
7679 }
7680
7681 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7682    generated for the compilation unit.  */
7683
7684 static unsigned long
7685 size_of_pubnames (VEC (pubname_entry, gc) * names)
7686 {
7687   unsigned long size;
7688   unsigned i;
7689   pubname_ref p;
7690
7691   size = DWARF_PUBNAMES_HEADER_SIZE;
7692   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
7693     if (names != pubtype_table
7694         || p->die->die_offset != 0
7695         || !flag_eliminate_unused_debug_types)
7696       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7697
7698   size += DWARF_OFFSET_SIZE;
7699   return size;
7700 }
7701
7702 /* Return the size of the information in the .debug_aranges section.  */
7703
7704 static unsigned long
7705 size_of_aranges (void)
7706 {
7707   unsigned long size;
7708
7709   size = DWARF_ARANGES_HEADER_SIZE;
7710
7711   /* Count the address/length pair for this compilation unit.  */
7712   if (text_section_used)
7713     size += 2 * DWARF2_ADDR_SIZE;
7714   if (cold_text_section_used)
7715     size += 2 * DWARF2_ADDR_SIZE;
7716   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
7717
7718   /* Count the two zero words used to terminated the address range table.  */
7719   size += 2 * DWARF2_ADDR_SIZE;
7720   return size;
7721 }
7722 \f
7723 /* Select the encoding of an attribute value.  */
7724
7725 static enum dwarf_form
7726 value_format (dw_attr_ref a)
7727 {
7728   switch (a->dw_attr_val.val_class)
7729     {
7730     case dw_val_class_addr:
7731       return DW_FORM_addr;
7732     case dw_val_class_range_list:
7733     case dw_val_class_offset:
7734     case dw_val_class_loc_list:
7735       switch (DWARF_OFFSET_SIZE)
7736         {
7737         case 4:
7738           return DW_FORM_data4;
7739         case 8:
7740           return DW_FORM_data8;
7741         default:
7742           gcc_unreachable ();
7743         }
7744     case dw_val_class_loc:
7745       switch (constant_size (size_of_locs (AT_loc (a))))
7746         {
7747         case 1:
7748           return DW_FORM_block1;
7749         case 2:
7750           return DW_FORM_block2;
7751         default:
7752           gcc_unreachable ();
7753         }
7754     case dw_val_class_const:
7755       return DW_FORM_sdata;
7756     case dw_val_class_unsigned_const:
7757       switch (constant_size (AT_unsigned (a)))
7758         {
7759         case 1:
7760           return DW_FORM_data1;
7761         case 2:
7762           return DW_FORM_data2;
7763         case 4:
7764           return DW_FORM_data4;
7765         case 8:
7766           return DW_FORM_data8;
7767         default:
7768           gcc_unreachable ();
7769         }
7770     case dw_val_class_long_long:
7771       return DW_FORM_block1;
7772     case dw_val_class_vec:
7773       switch (constant_size (a->dw_attr_val.v.val_vec.length
7774                              * a->dw_attr_val.v.val_vec.elt_size))
7775         {
7776         case 1:
7777           return DW_FORM_block1;
7778         case 2:
7779           return DW_FORM_block2;
7780         case 4:
7781           return DW_FORM_block4;
7782         default:
7783           gcc_unreachable ();
7784         }
7785     case dw_val_class_flag:
7786       return DW_FORM_flag;
7787     case dw_val_class_die_ref:
7788       if (AT_ref_external (a))
7789         return DW_FORM_ref_addr;
7790       else
7791         return DW_FORM_ref;
7792     case dw_val_class_fde_ref:
7793       return DW_FORM_data;
7794     case dw_val_class_lbl_id:
7795       return DW_FORM_addr;
7796     case dw_val_class_lineptr:
7797     case dw_val_class_macptr:
7798       return DW_FORM_data;
7799     case dw_val_class_str:
7800       return AT_string_form (a);
7801     case dw_val_class_file:
7802       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7803         {
7804         case 1:
7805           return DW_FORM_data1;
7806         case 2:
7807           return DW_FORM_data2;
7808         case 4:
7809           return DW_FORM_data4;
7810         default:
7811           gcc_unreachable ();
7812         }
7813
7814     default:
7815       gcc_unreachable ();
7816     }
7817 }
7818
7819 /* Output the encoding of an attribute value.  */
7820
7821 static void
7822 output_value_format (dw_attr_ref a)
7823 {
7824   enum dwarf_form form = value_format (a);
7825
7826   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7827 }
7828
7829 /* Output the .debug_abbrev section which defines the DIE abbreviation
7830    table.  */
7831
7832 static void
7833 output_abbrev_section (void)
7834 {
7835   unsigned long abbrev_id;
7836
7837   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7838     {
7839       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7840       unsigned ix;
7841       dw_attr_ref a_attr;
7842
7843       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7844       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7845                                    dwarf_tag_name (abbrev->die_tag));
7846
7847       if (abbrev->die_child != NULL)
7848         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7849       else
7850         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7851
7852       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7853            ix++)
7854         {
7855           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7856                                        dwarf_attr_name (a_attr->dw_attr));
7857           output_value_format (a_attr);
7858         }
7859
7860       dw2_asm_output_data (1, 0, NULL);
7861       dw2_asm_output_data (1, 0, NULL);
7862     }
7863
7864   /* Terminate the table.  */
7865   dw2_asm_output_data (1, 0, NULL);
7866 }
7867
7868 /* Output a symbol we can use to refer to this DIE from another CU.  */
7869
7870 static inline void
7871 output_die_symbol (dw_die_ref die)
7872 {
7873   char *sym = die->die_symbol;
7874
7875   if (sym == 0)
7876     return;
7877
7878   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7879     /* We make these global, not weak; if the target doesn't support
7880        .linkonce, it doesn't support combining the sections, so debugging
7881        will break.  */
7882     targetm.asm_out.globalize_label (asm_out_file, sym);
7883
7884   ASM_OUTPUT_LABEL (asm_out_file, sym);
7885 }
7886
7887 /* Return a new location list, given the begin and end range, and the
7888    expression. gensym tells us whether to generate a new internal symbol for
7889    this location list node, which is done for the head of the list only.  */
7890
7891 static inline dw_loc_list_ref
7892 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7893               const char *section, unsigned int gensym)
7894 {
7895   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7896
7897   retlist->begin = begin;
7898   retlist->end = end;
7899   retlist->expr = expr;
7900   retlist->section = section;
7901   if (gensym)
7902     retlist->ll_symbol = gen_internal_sym ("LLST");
7903
7904   return retlist;
7905 }
7906
7907 /* Add a location description expression to a location list.  */
7908
7909 static inline void
7910 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7911                            const char *begin, const char *end,
7912                            const char *section)
7913 {
7914   dw_loc_list_ref *d;
7915
7916   /* Find the end of the chain.  */
7917   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7918     ;
7919
7920   /* Add a new location list node to the list.  */
7921   *d = new_loc_list (descr, begin, end, section, 0);
7922 }
7923
7924 /* Output the location list given to us.  */
7925
7926 static void
7927 output_loc_list (dw_loc_list_ref list_head)
7928 {
7929   dw_loc_list_ref curr = list_head;
7930
7931   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7932
7933   /* Walk the location list, and output each range + expression.  */
7934   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7935     {
7936       unsigned long size;
7937       /* Don't output an entry that starts and ends at the same address.  */
7938       if (strcmp (curr->begin, curr->end) == 0)
7939         continue;
7940       if (!have_multiple_function_sections)
7941         {
7942           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7943                                 "Location list begin address (%s)",
7944                                 list_head->ll_symbol);
7945           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7946                                 "Location list end address (%s)",
7947                                 list_head->ll_symbol);
7948         }
7949       else
7950         {
7951           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7952                                "Location list begin address (%s)",
7953                                list_head->ll_symbol);
7954           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7955                                "Location list end address (%s)",
7956                                list_head->ll_symbol);
7957         }
7958       size = size_of_locs (curr->expr);
7959
7960       /* Output the block length for this list of location operations.  */
7961       gcc_assert (size <= 0xffff);
7962       dw2_asm_output_data (2, size, "%s", "Location expression size");
7963
7964       output_loc_sequence (curr->expr);
7965     }
7966
7967   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7968                        "Location list terminator begin (%s)",
7969                        list_head->ll_symbol);
7970   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7971                        "Location list terminator end (%s)",
7972                        list_head->ll_symbol);
7973 }
7974
7975 /* Output the DIE and its attributes.  Called recursively to generate
7976    the definitions of each child DIE.  */
7977
7978 static void
7979 output_die (dw_die_ref die)
7980 {
7981   dw_attr_ref a;
7982   dw_die_ref c;
7983   unsigned long size;
7984   unsigned ix;
7985
7986   /* If someone in another CU might refer to us, set up a symbol for
7987      them to point to.  */
7988   if (die->die_symbol)
7989     output_die_symbol (die);
7990
7991   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7992                                (unsigned long)die->die_offset,
7993                                dwarf_tag_name (die->die_tag));
7994
7995   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7996     {
7997       const char *name = dwarf_attr_name (a->dw_attr);
7998
7999       switch (AT_class (a))
8000         {
8001         case dw_val_class_addr:
8002           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8003           break;
8004
8005         case dw_val_class_offset:
8006           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8007                                "%s", name);
8008           break;
8009
8010         case dw_val_class_range_list:
8011           {
8012             char *p = strchr (ranges_section_label, '\0');
8013
8014             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8015                      a->dw_attr_val.v.val_offset);
8016             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8017                                    debug_ranges_section, "%s", name);
8018             *p = '\0';
8019           }
8020           break;
8021
8022         case dw_val_class_loc:
8023           size = size_of_locs (AT_loc (a));
8024
8025           /* Output the block length for this list of location operations.  */
8026           dw2_asm_output_data (constant_size (size), size, "%s", name);
8027
8028           output_loc_sequence (AT_loc (a));
8029           break;
8030
8031         case dw_val_class_const:
8032           /* ??? It would be slightly more efficient to use a scheme like is
8033              used for unsigned constants below, but gdb 4.x does not sign
8034              extend.  Gdb 5.x does sign extend.  */
8035           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8036           break;
8037
8038         case dw_val_class_unsigned_const:
8039           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8040                                AT_unsigned (a), "%s", name);
8041           break;
8042
8043         case dw_val_class_long_long:
8044           {
8045             unsigned HOST_WIDE_INT first, second;
8046
8047             dw2_asm_output_data (1,
8048                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8049                                  "%s", name);
8050
8051             if (WORDS_BIG_ENDIAN)
8052               {
8053                 first = a->dw_attr_val.v.val_long_long.hi;
8054                 second = a->dw_attr_val.v.val_long_long.low;
8055               }
8056             else
8057               {
8058                 first = a->dw_attr_val.v.val_long_long.low;
8059                 second = a->dw_attr_val.v.val_long_long.hi;
8060               }
8061
8062             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8063                                  first, "long long constant");
8064             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8065                                  second, NULL);
8066           }
8067           break;
8068
8069         case dw_val_class_vec:
8070           {
8071             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8072             unsigned int len = a->dw_attr_val.v.val_vec.length;
8073             unsigned int i;
8074             unsigned char *p;
8075
8076             dw2_asm_output_data (constant_size (len * elt_size),
8077                                  len * elt_size, "%s", name);
8078             if (elt_size > sizeof (HOST_WIDE_INT))
8079               {
8080                 elt_size /= 2;
8081                 len *= 2;
8082               }
8083             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8084                  i < len;
8085                  i++, p += elt_size)
8086               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8087                                    "fp or vector constant word %u", i);
8088             break;
8089           }
8090
8091         case dw_val_class_flag:
8092           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8093           break;
8094
8095         case dw_val_class_loc_list:
8096           {
8097             char *sym = AT_loc_list (a)->ll_symbol;
8098
8099             gcc_assert (sym);
8100             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8101                                    "%s", name);
8102           }
8103           break;
8104
8105         case dw_val_class_die_ref:
8106           if (AT_ref_external (a))
8107             {
8108               char *sym = AT_ref (a)->die_symbol;
8109
8110               gcc_assert (sym);
8111               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8112                                      "%s", name);
8113             }
8114           else
8115             {
8116               gcc_assert (AT_ref (a)->die_offset);
8117               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8118                                    "%s", name);
8119             }
8120           break;
8121
8122         case dw_val_class_fde_ref:
8123           {
8124             char l1[20];
8125
8126             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8127                                          a->dw_attr_val.v.val_fde_index * 2);
8128             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8129                                    "%s", name);
8130           }
8131           break;
8132
8133         case dw_val_class_lbl_id:
8134           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8135           break;
8136
8137         case dw_val_class_lineptr:
8138           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8139                                  debug_line_section, "%s", name);
8140           break;
8141
8142         case dw_val_class_macptr:
8143           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8144                                  debug_macinfo_section, "%s", name);
8145           break;
8146
8147         case dw_val_class_str:
8148           if (AT_string_form (a) == DW_FORM_strp)
8149             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8150                                    a->dw_attr_val.v.val_str->label,
8151                                    debug_str_section,
8152                                    "%s: \"%s\"", name, AT_string (a));
8153           else
8154             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8155           break;
8156
8157         case dw_val_class_file:
8158           {
8159             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8160
8161             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8162                                  a->dw_attr_val.v.val_file->filename);
8163             break;
8164           }
8165
8166         default:
8167           gcc_unreachable ();
8168         }
8169     }
8170
8171   FOR_EACH_CHILD (die, c, output_die (c));
8172
8173   /* Add null byte to terminate sibling list.  */
8174   if (die->die_child != NULL)
8175     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8176                          (unsigned long) die->die_offset);
8177 }
8178
8179 /* Output the compilation unit that appears at the beginning of the
8180    .debug_info section, and precedes the DIE descriptions.  */
8181
8182 static void
8183 output_compilation_unit_header (void)
8184 {
8185   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8186     dw2_asm_output_data (4, 0xffffffff,
8187       "Initial length escape value indicating 64-bit DWARF extension");
8188   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8189                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8190                        "Length of Compilation Unit Info");
8191   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8192   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8193                          debug_abbrev_section,
8194                          "Offset Into Abbrev. Section");
8195   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8196 }
8197
8198 /* Output the compilation unit DIE and its children.  */
8199
8200 static void
8201 output_comp_unit (dw_die_ref die, int output_if_empty)
8202 {
8203   const char *secname;
8204   char *oldsym, *tmp;
8205
8206   /* Unless we are outputting main CU, we may throw away empty ones.  */
8207   if (!output_if_empty && die->die_child == NULL)
8208     return;
8209
8210   /* Even if there are no children of this DIE, we must output the information
8211      about the compilation unit.  Otherwise, on an empty translation unit, we
8212      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8213      will then complain when examining the file.  First mark all the DIEs in
8214      this CU so we know which get local refs.  */
8215   mark_dies (die);
8216
8217   build_abbrev_table (die);
8218
8219   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8220   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8221   calc_die_sizes (die);
8222
8223   oldsym = die->die_symbol;
8224   if (oldsym)
8225     {
8226       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8227
8228       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8229       secname = tmp;
8230       die->die_symbol = NULL;
8231       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8232     }
8233   else
8234     switch_to_section (debug_info_section);
8235
8236   /* Output debugging information.  */
8237   output_compilation_unit_header ();
8238   output_die (die);
8239
8240   /* Leave the marks on the main CU, so we can check them in
8241      output_pubnames.  */
8242   if (oldsym)
8243     {
8244       unmark_dies (die);
8245       die->die_symbol = oldsym;
8246     }
8247 }
8248
8249 /* Return the DWARF2/3 pubname associated with a decl.  */
8250
8251 static const char *
8252 dwarf2_name (tree decl, int scope)
8253 {
8254   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8255 }
8256
8257 /* Add a new entry to .debug_pubnames if appropriate.  */
8258
8259 static void
8260 add_pubname_string (const char *str, dw_die_ref die)
8261 {
8262   pubname_entry e;
8263
8264   e.die = die;
8265   e.name = xstrdup (str);
8266   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8267 }
8268
8269 static void
8270 add_pubname (tree decl, dw_die_ref die)
8271 {
8272
8273   if (TREE_PUBLIC (decl))
8274     add_pubname_string (dwarf2_name (decl, 1), die);
8275 }
8276
8277 /* Add a new entry to .debug_pubtypes if appropriate.  */
8278
8279 static void
8280 add_pubtype (tree decl, dw_die_ref die)
8281 {
8282   pubname_entry e;
8283
8284   e.name = NULL;
8285   if ((TREE_PUBLIC (decl)
8286        || die->die_parent == comp_unit_die)
8287       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8288     {
8289       e.die = die;
8290       if (TYPE_P (decl))
8291         {
8292           if (TYPE_NAME (decl))
8293             {
8294               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8295                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8296               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8297                        && DECL_NAME (TYPE_NAME (decl)))
8298                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8299               else
8300                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8301             }
8302         }
8303       else
8304         e.name = xstrdup (dwarf2_name (decl, 1));
8305
8306       /* If we don't have a name for the type, there's no point in adding
8307          it to the table.  */
8308       if (e.name && e.name[0] != '\0')
8309         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8310     }
8311 }
8312
8313 /* Output the public names table used to speed up access to externally
8314    visible names; or the public types table used to find type definitions.  */
8315
8316 static void
8317 output_pubnames (VEC (pubname_entry, gc) * names)
8318 {
8319   unsigned i;
8320   unsigned long pubnames_length = size_of_pubnames (names);
8321   pubname_ref pub;
8322
8323   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8324     dw2_asm_output_data (4, 0xffffffff,
8325       "Initial length escape value indicating 64-bit DWARF extension");
8326   if (names == pubname_table)
8327     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8328                          "Length of Public Names Info");
8329   else
8330     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8331                          "Length of Public Type Names Info");
8332   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8333   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8334                          debug_info_section,
8335                          "Offset of Compilation Unit Info");
8336   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8337                        "Compilation Unit Length");
8338
8339   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8340     {
8341       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8342       if (names == pubname_table)
8343         gcc_assert (pub->die->die_mark);
8344
8345       if (names != pubtype_table
8346           || pub->die->die_offset != 0
8347           || !flag_eliminate_unused_debug_types)
8348         {
8349           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8350                                "DIE offset");
8351
8352           dw2_asm_output_nstring (pub->name, -1, "external name");
8353         }
8354     }
8355
8356   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8357 }
8358
8359 /* Add a new entry to .debug_aranges if appropriate.  */
8360
8361 static void
8362 add_arange (tree decl, dw_die_ref die)
8363 {
8364   if (! DECL_SECTION_NAME (decl))
8365     return;
8366
8367   if (arange_table_in_use == arange_table_allocated)
8368     {
8369       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8370       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8371                                     arange_table_allocated);
8372       memset (arange_table + arange_table_in_use, 0,
8373               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8374     }
8375
8376   arange_table[arange_table_in_use++] = die;
8377 }
8378
8379 /* Output the information that goes into the .debug_aranges table.
8380    Namely, define the beginning and ending address range of the
8381    text section generated for this compilation unit.  */
8382
8383 static void
8384 output_aranges (void)
8385 {
8386   unsigned i;
8387   unsigned long aranges_length = size_of_aranges ();
8388
8389   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8390     dw2_asm_output_data (4, 0xffffffff,
8391       "Initial length escape value indicating 64-bit DWARF extension");
8392   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8393                        "Length of Address Ranges Info");
8394   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8395   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8396                          debug_info_section,
8397                          "Offset of Compilation Unit Info");
8398   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8399   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8400
8401   /* We need to align to twice the pointer size here.  */
8402   if (DWARF_ARANGES_PAD_SIZE)
8403     {
8404       /* Pad using a 2 byte words so that padding is correct for any
8405          pointer size.  */
8406       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8407                            2 * DWARF2_ADDR_SIZE);
8408       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8409         dw2_asm_output_data (2, 0, NULL);
8410     }
8411
8412   /* It is necessary not to output these entries if the sections were
8413      not used; if the sections were not used, the length will be 0 and
8414      the address may end up as 0 if the section is discarded by ld
8415      --gc-sections, leaving an invalid (0, 0) entry that can be
8416      confused with the terminator.  */
8417   if (text_section_used)
8418     {
8419       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8420       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8421                             text_section_label, "Length");
8422     }
8423   if (cold_text_section_used)
8424     {
8425       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8426                            "Address");
8427       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8428                             cold_text_section_label, "Length");
8429     }
8430
8431   for (i = 0; i < arange_table_in_use; i++)
8432     {
8433       dw_die_ref die = arange_table[i];
8434
8435       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8436       gcc_assert (die->die_mark);
8437
8438       if (die->die_tag == DW_TAG_subprogram)
8439         {
8440           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8441                                "Address");
8442           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8443                                 get_AT_low_pc (die), "Length");
8444         }
8445       else
8446         {
8447           /* A static variable; extract the symbol from DW_AT_location.
8448              Note that this code isn't currently hit, as we only emit
8449              aranges for functions (jason 9/23/99).  */
8450           dw_attr_ref a = get_AT (die, DW_AT_location);
8451           dw_loc_descr_ref loc;
8452
8453           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8454
8455           loc = AT_loc (a);
8456           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8457
8458           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8459                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8460           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8461                                get_AT_unsigned (die, DW_AT_byte_size),
8462                                "Length");
8463         }
8464     }
8465
8466   /* Output the terminator words.  */
8467   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8468   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8469 }
8470
8471 /* Add a new entry to .debug_ranges.  Return the offset at which it
8472    was placed.  */
8473
8474 static unsigned int
8475 add_ranges_num (int num)
8476 {
8477   unsigned int in_use = ranges_table_in_use;
8478
8479   if (in_use == ranges_table_allocated)
8480     {
8481       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8482       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8483                                     ranges_table_allocated);
8484       memset (ranges_table + ranges_table_in_use, 0,
8485               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8486     }
8487
8488   ranges_table[in_use].num = num;
8489   ranges_table_in_use = in_use + 1;
8490
8491   return in_use * 2 * DWARF2_ADDR_SIZE;
8492 }
8493
8494 /* Add a new entry to .debug_ranges corresponding to a block, or a
8495    range terminator if BLOCK is NULL.  */
8496
8497 static unsigned int
8498 add_ranges (const_tree block)
8499 {
8500   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8501 }
8502
8503 /* Add a new entry to .debug_ranges corresponding to a pair of
8504    labels.  */
8505
8506 static unsigned int
8507 add_ranges_by_labels (const char *begin, const char *end)
8508 {
8509   unsigned int in_use = ranges_by_label_in_use;
8510
8511   if (in_use == ranges_by_label_allocated)
8512     {
8513       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8514       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8515                                        ranges_by_label,
8516                                        ranges_by_label_allocated);
8517       memset (ranges_by_label + ranges_by_label_in_use, 0,
8518               RANGES_TABLE_INCREMENT
8519               * sizeof (struct dw_ranges_by_label_struct));
8520     }
8521
8522   ranges_by_label[in_use].begin = begin;
8523   ranges_by_label[in_use].end = end;
8524   ranges_by_label_in_use = in_use + 1;
8525
8526   return add_ranges_num (-(int)in_use - 1);
8527 }
8528
8529 static void
8530 output_ranges (void)
8531 {
8532   unsigned i;
8533   static const char *const start_fmt = "Offset 0x%x";
8534   const char *fmt = start_fmt;
8535
8536   for (i = 0; i < ranges_table_in_use; i++)
8537     {
8538       int block_num = ranges_table[i].num;
8539
8540       if (block_num > 0)
8541         {
8542           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8543           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8544
8545           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8546           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8547
8548           /* If all code is in the text section, then the compilation
8549              unit base address defaults to DW_AT_low_pc, which is the
8550              base of the text section.  */
8551           if (!have_multiple_function_sections)
8552             {
8553               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8554                                     text_section_label,
8555                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8556               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8557                                     text_section_label, NULL);
8558             }
8559
8560           /* Otherwise, the compilation unit base address is zero,
8561              which allows us to use absolute addresses, and not worry
8562              about whether the target supports cross-section
8563              arithmetic.  */
8564           else
8565             {
8566               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8567                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8568               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8569             }
8570
8571           fmt = NULL;
8572         }
8573
8574       /* Negative block_num stands for an index into ranges_by_label.  */
8575       else if (block_num < 0)
8576         {
8577           int lab_idx = - block_num - 1;
8578
8579           if (!have_multiple_function_sections)
8580             {
8581               gcc_unreachable ();
8582 #if 0
8583               /* If we ever use add_ranges_by_labels () for a single
8584                  function section, all we have to do is to take out
8585                  the #if 0 above.  */
8586               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8587                                     ranges_by_label[lab_idx].begin,
8588                                     text_section_label,
8589                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8590               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8591                                     ranges_by_label[lab_idx].end,
8592                                     text_section_label, NULL);
8593 #endif
8594             }
8595           else
8596             {
8597               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8598                                    ranges_by_label[lab_idx].begin,
8599                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8600               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8601                                    ranges_by_label[lab_idx].end,
8602                                    NULL);
8603             }
8604         }
8605       else
8606         {
8607           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8608           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8609           fmt = start_fmt;
8610         }
8611     }
8612 }
8613
8614 /* Data structure containing information about input files.  */
8615 struct file_info
8616 {
8617   const char *path;     /* Complete file name.  */
8618   const char *fname;    /* File name part.  */
8619   int length;           /* Length of entire string.  */
8620   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8621   int dir_idx;          /* Index in directory table.  */
8622 };
8623
8624 /* Data structure containing information about directories with source
8625    files.  */
8626 struct dir_info
8627 {
8628   const char *path;     /* Path including directory name.  */
8629   int length;           /* Path length.  */
8630   int prefix;           /* Index of directory entry which is a prefix.  */
8631   int count;            /* Number of files in this directory.  */
8632   int dir_idx;          /* Index of directory used as base.  */
8633 };
8634
8635 /* Callback function for file_info comparison.  We sort by looking at
8636    the directories in the path.  */
8637
8638 static int
8639 file_info_cmp (const void *p1, const void *p2)
8640 {
8641   const struct file_info *const s1 = (const struct file_info *) p1;
8642   const struct file_info *const s2 = (const struct file_info *) p2;
8643   const unsigned char *cp1;
8644   const unsigned char *cp2;
8645
8646   /* Take care of file names without directories.  We need to make sure that
8647      we return consistent values to qsort since some will get confused if
8648      we return the same value when identical operands are passed in opposite
8649      orders.  So if neither has a directory, return 0 and otherwise return
8650      1 or -1 depending on which one has the directory.  */
8651   if ((s1->path == s1->fname || s2->path == s2->fname))
8652     return (s2->path == s2->fname) - (s1->path == s1->fname);
8653
8654   cp1 = (const unsigned char *) s1->path;
8655   cp2 = (const unsigned char *) s2->path;
8656
8657   while (1)
8658     {
8659       ++cp1;
8660       ++cp2;
8661       /* Reached the end of the first path?  If so, handle like above.  */
8662       if ((cp1 == (const unsigned char *) s1->fname)
8663           || (cp2 == (const unsigned char *) s2->fname))
8664         return ((cp2 == (const unsigned char *) s2->fname)
8665                 - (cp1 == (const unsigned char *) s1->fname));
8666
8667       /* Character of current path component the same?  */
8668       else if (*cp1 != *cp2)
8669         return *cp1 - *cp2;
8670     }
8671 }
8672
8673 struct file_name_acquire_data
8674 {
8675   struct file_info *files;
8676   int used_files;
8677   int max_files;
8678 };
8679
8680 /* Traversal function for the hash table.  */
8681
8682 static int
8683 file_name_acquire (void ** slot, void *data)
8684 {
8685   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8686   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8687   struct file_info *fi;
8688   const char *f;
8689
8690   gcc_assert (fnad->max_files >= d->emitted_number);
8691
8692   if (! d->emitted_number)
8693     return 1;
8694
8695   gcc_assert (fnad->max_files != fnad->used_files);
8696
8697   fi = fnad->files + fnad->used_files++;
8698
8699   /* Skip all leading "./".  */
8700   f = d->filename;
8701   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8702     f += 2;
8703
8704   /* Create a new array entry.  */
8705   fi->path = f;
8706   fi->length = strlen (f);
8707   fi->file_idx = d;
8708
8709   /* Search for the file name part.  */
8710   f = strrchr (f, DIR_SEPARATOR);
8711 #if defined (DIR_SEPARATOR_2)
8712   {
8713     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8714
8715     if (g != NULL)
8716       {
8717         if (f == NULL || f < g)
8718           f = g;
8719       }
8720   }
8721 #endif
8722
8723   fi->fname = f == NULL ? fi->path : f + 1;
8724   return 1;
8725 }
8726
8727 /* Output the directory table and the file name table.  We try to minimize
8728    the total amount of memory needed.  A heuristic is used to avoid large
8729    slowdowns with many input files.  */
8730
8731 static void
8732 output_file_names (void)
8733 {
8734   struct file_name_acquire_data fnad;
8735   int numfiles;
8736   struct file_info *files;
8737   struct dir_info *dirs;
8738   int *saved;
8739   int *savehere;
8740   int *backmap;
8741   int ndirs;
8742   int idx_offset;
8743   int i;
8744   int idx;
8745
8746   if (!last_emitted_file)
8747     {
8748       dw2_asm_output_data (1, 0, "End directory table");
8749       dw2_asm_output_data (1, 0, "End file name table");
8750       return;
8751     }
8752
8753   numfiles = last_emitted_file->emitted_number;
8754
8755   /* Allocate the various arrays we need.  */
8756   files = XALLOCAVEC (struct file_info, numfiles);
8757   dirs = XALLOCAVEC (struct dir_info, numfiles);
8758
8759   fnad.files = files;
8760   fnad.used_files = 0;
8761   fnad.max_files = numfiles;
8762   htab_traverse (file_table, file_name_acquire, &fnad);
8763   gcc_assert (fnad.used_files == fnad.max_files);
8764
8765   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8766
8767   /* Find all the different directories used.  */
8768   dirs[0].path = files[0].path;
8769   dirs[0].length = files[0].fname - files[0].path;
8770   dirs[0].prefix = -1;
8771   dirs[0].count = 1;
8772   dirs[0].dir_idx = 0;
8773   files[0].dir_idx = 0;
8774   ndirs = 1;
8775
8776   for (i = 1; i < numfiles; i++)
8777     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8778         && memcmp (dirs[ndirs - 1].path, files[i].path,
8779                    dirs[ndirs - 1].length) == 0)
8780       {
8781         /* Same directory as last entry.  */
8782         files[i].dir_idx = ndirs - 1;
8783         ++dirs[ndirs - 1].count;
8784       }
8785     else
8786       {
8787         int j;
8788
8789         /* This is a new directory.  */
8790         dirs[ndirs].path = files[i].path;
8791         dirs[ndirs].length = files[i].fname - files[i].path;
8792         dirs[ndirs].count = 1;
8793         dirs[ndirs].dir_idx = ndirs;
8794         files[i].dir_idx = ndirs;
8795
8796         /* Search for a prefix.  */
8797         dirs[ndirs].prefix = -1;
8798         for (j = 0; j < ndirs; j++)
8799           if (dirs[j].length < dirs[ndirs].length
8800               && dirs[j].length > 1
8801               && (dirs[ndirs].prefix == -1
8802                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8803               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8804             dirs[ndirs].prefix = j;
8805
8806         ++ndirs;
8807       }
8808
8809   /* Now to the actual work.  We have to find a subset of the directories which
8810      allow expressing the file name using references to the directory table
8811      with the least amount of characters.  We do not do an exhaustive search
8812      where we would have to check out every combination of every single
8813      possible prefix.  Instead we use a heuristic which provides nearly optimal
8814      results in most cases and never is much off.  */
8815   saved = XALLOCAVEC (int, ndirs);
8816   savehere = XALLOCAVEC (int, ndirs);
8817
8818   memset (saved, '\0', ndirs * sizeof (saved[0]));
8819   for (i = 0; i < ndirs; i++)
8820     {
8821       int j;
8822       int total;
8823
8824       /* We can always save some space for the current directory.  But this
8825          does not mean it will be enough to justify adding the directory.  */
8826       savehere[i] = dirs[i].length;
8827       total = (savehere[i] - saved[i]) * dirs[i].count;
8828
8829       for (j = i + 1; j < ndirs; j++)
8830         {
8831           savehere[j] = 0;
8832           if (saved[j] < dirs[i].length)
8833             {
8834               /* Determine whether the dirs[i] path is a prefix of the
8835                  dirs[j] path.  */
8836               int k;
8837
8838               k = dirs[j].prefix;
8839               while (k != -1 && k != (int) i)
8840                 k = dirs[k].prefix;
8841
8842               if (k == (int) i)
8843                 {
8844                   /* Yes it is.  We can possibly save some memory by
8845                      writing the filenames in dirs[j] relative to
8846                      dirs[i].  */
8847                   savehere[j] = dirs[i].length;
8848                   total += (savehere[j] - saved[j]) * dirs[j].count;
8849                 }
8850             }
8851         }
8852
8853       /* Check whether we can save enough to justify adding the dirs[i]
8854          directory.  */
8855       if (total > dirs[i].length + 1)
8856         {
8857           /* It's worthwhile adding.  */
8858           for (j = i; j < ndirs; j++)
8859             if (savehere[j] > 0)
8860               {
8861                 /* Remember how much we saved for this directory so far.  */
8862                 saved[j] = savehere[j];
8863
8864                 /* Remember the prefix directory.  */
8865                 dirs[j].dir_idx = i;
8866               }
8867         }
8868     }
8869
8870   /* Emit the directory name table.  */
8871   idx = 1;
8872   idx_offset = dirs[0].length > 0 ? 1 : 0;
8873   for (i = 1 - idx_offset; i < ndirs; i++)
8874     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8875                             "Directory Entry: 0x%x", i + idx_offset);
8876
8877   dw2_asm_output_data (1, 0, "End directory table");
8878
8879   /* We have to emit them in the order of emitted_number since that's
8880      used in the debug info generation.  To do this efficiently we
8881      generate a back-mapping of the indices first.  */
8882   backmap = XALLOCAVEC (int, numfiles);
8883   for (i = 0; i < numfiles; i++)
8884     backmap[files[i].file_idx->emitted_number - 1] = i;
8885
8886   /* Now write all the file names.  */
8887   for (i = 0; i < numfiles; i++)
8888     {
8889       int file_idx = backmap[i];
8890       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8891
8892       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8893                               "File Entry: 0x%x", (unsigned) i + 1);
8894
8895       /* Include directory index.  */
8896       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8897
8898       /* Modification time.  */
8899       dw2_asm_output_data_uleb128 (0, NULL);
8900
8901       /* File length in bytes.  */
8902       dw2_asm_output_data_uleb128 (0, NULL);
8903     }
8904
8905   dw2_asm_output_data (1, 0, "End file name table");
8906 }
8907
8908
8909 /* Output the source line number correspondence information.  This
8910    information goes into the .debug_line section.  */
8911
8912 static void
8913 output_line_info (void)
8914 {
8915   char l1[20], l2[20], p1[20], p2[20];
8916   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8917   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8918   unsigned opc;
8919   unsigned n_op_args;
8920   unsigned long lt_index;
8921   unsigned long current_line;
8922   long line_offset;
8923   long line_delta;
8924   unsigned long current_file;
8925   unsigned long function;
8926
8927   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8928   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8929   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8930   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8931
8932   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8933     dw2_asm_output_data (4, 0xffffffff,
8934       "Initial length escape value indicating 64-bit DWARF extension");
8935   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8936                         "Length of Source Line Info");
8937   ASM_OUTPUT_LABEL (asm_out_file, l1);
8938
8939   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8940   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8941   ASM_OUTPUT_LABEL (asm_out_file, p1);
8942
8943   /* Define the architecture-dependent minimum instruction length (in
8944    bytes).  In this implementation of DWARF, this field is used for
8945    information purposes only.  Since GCC generates assembly language,
8946    we have no a priori knowledge of how many instruction bytes are
8947    generated for each source line, and therefore can use only the
8948    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8949    commands.  Accordingly, we fix this as `1', which is "correct
8950    enough" for all architectures, and don't let the target override.  */
8951   dw2_asm_output_data (1, 1,
8952                        "Minimum Instruction Length");
8953
8954   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8955                        "Default is_stmt_start flag");
8956   dw2_asm_output_data (1, DWARF_LINE_BASE,
8957                        "Line Base Value (Special Opcodes)");
8958   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8959                        "Line Range Value (Special Opcodes)");
8960   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8961                        "Special Opcode Base");
8962
8963   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8964     {
8965       switch (opc)
8966         {
8967         case DW_LNS_advance_pc:
8968         case DW_LNS_advance_line:
8969         case DW_LNS_set_file:
8970         case DW_LNS_set_column:
8971         case DW_LNS_fixed_advance_pc:
8972           n_op_args = 1;
8973           break;
8974         default:
8975           n_op_args = 0;
8976           break;
8977         }
8978
8979       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8980                            opc, n_op_args);
8981     }
8982
8983   /* Write out the information about the files we use.  */
8984   output_file_names ();
8985   ASM_OUTPUT_LABEL (asm_out_file, p2);
8986
8987   /* We used to set the address register to the first location in the text
8988      section here, but that didn't accomplish anything since we already
8989      have a line note for the opening brace of the first function.  */
8990
8991   /* Generate the line number to PC correspondence table, encoded as
8992      a series of state machine operations.  */
8993   current_file = 1;
8994   current_line = 1;
8995
8996   if (cfun && in_cold_section_p)
8997     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8998   else
8999     strcpy (prev_line_label, text_section_label);
9000   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9001     {
9002       dw_line_info_ref line_info = &line_info_table[lt_index];
9003
9004 #if 0
9005       /* Disable this optimization for now; GDB wants to see two line notes
9006          at the beginning of a function so it can find the end of the
9007          prologue.  */
9008
9009       /* Don't emit anything for redundant notes.  Just updating the
9010          address doesn't accomplish anything, because we already assume
9011          that anything after the last address is this line.  */
9012       if (line_info->dw_line_num == current_line
9013           && line_info->dw_file_num == current_file)
9014         continue;
9015 #endif
9016
9017       /* Emit debug info for the address of the current line.
9018
9019          Unfortunately, we have little choice here currently, and must always
9020          use the most general form.  GCC does not know the address delta
9021          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9022          attributes which will give an upper bound on the address range.  We
9023          could perhaps use length attributes to determine when it is safe to
9024          use DW_LNS_fixed_advance_pc.  */
9025
9026       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9027       if (0)
9028         {
9029           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9030           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9031                                "DW_LNS_fixed_advance_pc");
9032           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9033         }
9034       else
9035         {
9036           /* This can handle any delta.  This takes
9037              4+DWARF2_ADDR_SIZE bytes.  */
9038           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9039           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9040           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9041           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9042         }
9043
9044       strcpy (prev_line_label, line_label);
9045
9046       /* Emit debug info for the source file of the current line, if
9047          different from the previous line.  */
9048       if (line_info->dw_file_num != current_file)
9049         {
9050           current_file = line_info->dw_file_num;
9051           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9052           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9053         }
9054
9055       /* Emit debug info for the current line number, choosing the encoding
9056          that uses the least amount of space.  */
9057       if (line_info->dw_line_num != current_line)
9058         {
9059           line_offset = line_info->dw_line_num - current_line;
9060           line_delta = line_offset - DWARF_LINE_BASE;
9061           current_line = line_info->dw_line_num;
9062           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9063             /* This can handle deltas from -10 to 234, using the current
9064                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9065                takes 1 byte.  */
9066             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9067                                  "line %lu", current_line);
9068           else
9069             {
9070               /* This can handle any delta.  This takes at least 4 bytes,
9071                  depending on the value being encoded.  */
9072               dw2_asm_output_data (1, DW_LNS_advance_line,
9073                                    "advance to line %lu", current_line);
9074               dw2_asm_output_data_sleb128 (line_offset, NULL);
9075               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9076             }
9077         }
9078       else
9079         /* We still need to start a new row, so output a copy insn.  */
9080         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9081     }
9082
9083   /* Emit debug info for the address of the end of the function.  */
9084   if (0)
9085     {
9086       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9087                            "DW_LNS_fixed_advance_pc");
9088       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9089     }
9090   else
9091     {
9092       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9093       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9094       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9095       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9096     }
9097
9098   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9099   dw2_asm_output_data_uleb128 (1, NULL);
9100   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9101
9102   function = 0;
9103   current_file = 1;
9104   current_line = 1;
9105   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9106     {
9107       dw_separate_line_info_ref line_info
9108         = &separate_line_info_table[lt_index];
9109
9110 #if 0
9111       /* Don't emit anything for redundant notes.  */
9112       if (line_info->dw_line_num == current_line
9113           && line_info->dw_file_num == current_file
9114           && line_info->function == function)
9115         goto cont;
9116 #endif
9117
9118       /* Emit debug info for the address of the current line.  If this is
9119          a new function, or the first line of a function, then we need
9120          to handle it differently.  */
9121       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9122                                    lt_index);
9123       if (function != line_info->function)
9124         {
9125           function = line_info->function;
9126
9127           /* Set the address register to the first line in the function.  */
9128           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9129           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9130           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9131           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9132         }
9133       else
9134         {
9135           /* ??? See the DW_LNS_advance_pc comment above.  */
9136           if (0)
9137             {
9138               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9139                                    "DW_LNS_fixed_advance_pc");
9140               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9141             }
9142           else
9143             {
9144               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9145               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9146               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9147               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9148             }
9149         }
9150
9151       strcpy (prev_line_label, line_label);
9152
9153       /* Emit debug info for the source file of the current line, if
9154          different from the previous line.  */
9155       if (line_info->dw_file_num != current_file)
9156         {
9157           current_file = line_info->dw_file_num;
9158           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9159           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9160         }
9161
9162       /* Emit debug info for the current line number, choosing the encoding
9163          that uses the least amount of space.  */
9164       if (line_info->dw_line_num != current_line)
9165         {
9166           line_offset = line_info->dw_line_num - current_line;
9167           line_delta = line_offset - DWARF_LINE_BASE;
9168           current_line = line_info->dw_line_num;
9169           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9170             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9171                                  "line %lu", current_line);
9172           else
9173             {
9174               dw2_asm_output_data (1, DW_LNS_advance_line,
9175                                    "advance to line %lu", current_line);
9176               dw2_asm_output_data_sleb128 (line_offset, NULL);
9177               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9178             }
9179         }
9180       else
9181         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9182
9183 #if 0
9184     cont:
9185 #endif
9186
9187       lt_index++;
9188
9189       /* If we're done with a function, end its sequence.  */
9190       if (lt_index == separate_line_info_table_in_use
9191           || separate_line_info_table[lt_index].function != function)
9192         {
9193           current_file = 1;
9194           current_line = 1;
9195
9196           /* Emit debug info for the address of the end of the function.  */
9197           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9198           if (0)
9199             {
9200               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9201                                    "DW_LNS_fixed_advance_pc");
9202               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9203             }
9204           else
9205             {
9206               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9207               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9208               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9209               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9210             }
9211
9212           /* Output the marker for the end of this sequence.  */
9213           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9214           dw2_asm_output_data_uleb128 (1, NULL);
9215           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9216         }
9217     }
9218
9219   /* Output the marker for the end of the line number info.  */
9220   ASM_OUTPUT_LABEL (asm_out_file, l2);
9221 }
9222 \f
9223 /* Given a pointer to a tree node for some base type, return a pointer to
9224    a DIE that describes the given type.
9225
9226    This routine must only be called for GCC type nodes that correspond to
9227    Dwarf base (fundamental) types.  */
9228
9229 static dw_die_ref
9230 base_type_die (tree type)
9231 {
9232   dw_die_ref base_type_result;
9233   enum dwarf_type encoding;
9234
9235   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9236     return 0;
9237
9238   switch (TREE_CODE (type))
9239     {
9240     case INTEGER_TYPE:
9241       if (TYPE_STRING_FLAG (type))
9242         {
9243           if (TYPE_UNSIGNED (type))
9244             encoding = DW_ATE_unsigned_char;
9245           else
9246             encoding = DW_ATE_signed_char;
9247         }
9248       else if (TYPE_UNSIGNED (type))
9249         encoding = DW_ATE_unsigned;
9250       else
9251         encoding = DW_ATE_signed;
9252       break;
9253
9254     case REAL_TYPE:
9255       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9256         encoding = DW_ATE_decimal_float;
9257       else
9258         encoding = DW_ATE_float;
9259       break;
9260
9261     case FIXED_POINT_TYPE:
9262       if (TYPE_UNSIGNED (type))
9263         encoding = DW_ATE_unsigned_fixed;
9264       else
9265         encoding = DW_ATE_signed_fixed;
9266       break;
9267
9268       /* Dwarf2 doesn't know anything about complex ints, so use
9269          a user defined type for it.  */
9270     case COMPLEX_TYPE:
9271       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9272         encoding = DW_ATE_complex_float;
9273       else
9274         encoding = DW_ATE_lo_user;
9275       break;
9276
9277     case BOOLEAN_TYPE:
9278       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9279       encoding = DW_ATE_boolean;
9280       break;
9281
9282     default:
9283       /* No other TREE_CODEs are Dwarf fundamental types.  */
9284       gcc_unreachable ();
9285     }
9286
9287   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9288
9289   /* This probably indicates a bug.  */
9290   if (! TYPE_NAME (type))
9291     add_name_attribute (base_type_result, "__unknown__");
9292
9293   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9294                    int_size_in_bytes (type));
9295   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9296
9297   return base_type_result;
9298 }
9299
9300 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9301    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9302
9303 static inline int
9304 is_base_type (tree type)
9305 {
9306   switch (TREE_CODE (type))
9307     {
9308     case ERROR_MARK:
9309     case VOID_TYPE:
9310     case INTEGER_TYPE:
9311     case REAL_TYPE:
9312     case FIXED_POINT_TYPE:
9313     case COMPLEX_TYPE:
9314     case BOOLEAN_TYPE:
9315       return 1;
9316
9317     case ARRAY_TYPE:
9318     case RECORD_TYPE:
9319     case UNION_TYPE:
9320     case QUAL_UNION_TYPE:
9321     case ENUMERAL_TYPE:
9322     case FUNCTION_TYPE:
9323     case METHOD_TYPE:
9324     case POINTER_TYPE:
9325     case REFERENCE_TYPE:
9326     case OFFSET_TYPE:
9327     case LANG_TYPE:
9328     case VECTOR_TYPE:
9329       return 0;
9330
9331     default:
9332       gcc_unreachable ();
9333     }
9334
9335   return 0;
9336 }
9337
9338 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9339    node, return the size in bits for the type if it is a constant, or else
9340    return the alignment for the type if the type's size is not constant, or
9341    else return BITS_PER_WORD if the type actually turns out to be an
9342    ERROR_MARK node.  */
9343
9344 static inline unsigned HOST_WIDE_INT
9345 simple_type_size_in_bits (const_tree type)
9346 {
9347   if (TREE_CODE (type) == ERROR_MARK)
9348     return BITS_PER_WORD;
9349   else if (TYPE_SIZE (type) == NULL_TREE)
9350     return 0;
9351   else if (host_integerp (TYPE_SIZE (type), 1))
9352     return tree_low_cst (TYPE_SIZE (type), 1);
9353   else
9354     return TYPE_ALIGN (type);
9355 }
9356
9357 /* Return true if the debug information for the given type should be
9358    emitted as a subrange type.  */
9359
9360 static inline bool
9361 is_subrange_type (const_tree type)
9362 {
9363   tree subtype = TREE_TYPE (type);
9364
9365   /* Subrange types are identified by the fact that they are integer
9366      types, and that they have a subtype which is either an integer type
9367      or an enumeral type.  */
9368
9369   if (TREE_CODE (type) != INTEGER_TYPE
9370       || subtype == NULL_TREE)
9371     return false;
9372
9373   if (TREE_CODE (subtype) != INTEGER_TYPE
9374       && TREE_CODE (subtype) != ENUMERAL_TYPE
9375       && TREE_CODE (subtype) != BOOLEAN_TYPE)
9376     return false;
9377
9378   if (TREE_CODE (type) == TREE_CODE (subtype)
9379       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
9380       && TYPE_MIN_VALUE (type) != NULL
9381       && TYPE_MIN_VALUE (subtype) != NULL
9382       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
9383       && TYPE_MAX_VALUE (type) != NULL
9384       && TYPE_MAX_VALUE (subtype) != NULL
9385       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
9386     {
9387       /* The type and its subtype have the same representation.  If in
9388          addition the two types also have the same name, then the given
9389          type is not a subrange type, but rather a plain base type.  */
9390       /* FIXME: brobecker/2004-03-22:
9391          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
9392          therefore be sufficient to check the TYPE_SIZE node pointers
9393          rather than checking the actual size.  Unfortunately, we have
9394          found some cases, such as in the Ada "integer" type, where
9395          this is not the case.  Until this problem is solved, we need to
9396          keep checking the actual size.  */
9397       tree type_name = TYPE_NAME (type);
9398       tree subtype_name = TYPE_NAME (subtype);
9399
9400       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
9401         type_name = DECL_NAME (type_name);
9402
9403       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
9404         subtype_name = DECL_NAME (subtype_name);
9405
9406       if (type_name == subtype_name)
9407         return false;
9408     }
9409
9410   return true;
9411 }
9412
9413 /*  Given a pointer to a tree node for a subrange type, return a pointer
9414     to a DIE that describes the given type.  */
9415
9416 static dw_die_ref
9417 subrange_type_die (tree type, dw_die_ref context_die)
9418 {
9419   dw_die_ref subrange_die;
9420   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9421
9422   if (context_die == NULL)
9423     context_die = comp_unit_die;
9424
9425   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9426
9427   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9428     {
9429       /* The size of the subrange type and its base type do not match,
9430          so we need to generate a size attribute for the subrange type.  */
9431       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9432     }
9433
9434   if (TYPE_MIN_VALUE (type) != NULL)
9435     add_bound_info (subrange_die, DW_AT_lower_bound,
9436                     TYPE_MIN_VALUE (type));
9437   if (TYPE_MAX_VALUE (type) != NULL)
9438     add_bound_info (subrange_die, DW_AT_upper_bound,
9439                     TYPE_MAX_VALUE (type));
9440
9441   return subrange_die;
9442 }
9443
9444 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9445    entry that chains various modifiers in front of the given type.  */
9446
9447 static dw_die_ref
9448 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9449                    dw_die_ref context_die)
9450 {
9451   enum tree_code code = TREE_CODE (type);
9452   dw_die_ref mod_type_die;
9453   dw_die_ref sub_die = NULL;
9454   tree item_type = NULL;
9455   tree qualified_type;
9456   tree name;
9457
9458   if (code == ERROR_MARK)
9459     return NULL;
9460
9461   /* See if we already have the appropriately qualified variant of
9462      this type.  */
9463   qualified_type
9464     = get_qualified_type (type,
9465                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9466                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9467
9468   /* If we do, then we can just use its DIE, if it exists.  */
9469   if (qualified_type)
9470     {
9471       mod_type_die = lookup_type_die (qualified_type);
9472       if (mod_type_die)
9473         return mod_type_die;
9474     }
9475
9476   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9477
9478   /* Handle C typedef types.  */
9479   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9480     {
9481       tree dtype = TREE_TYPE (name);
9482
9483       if (qualified_type == dtype)
9484         {
9485           /* For a named type, use the typedef.  */
9486           gen_type_die (qualified_type, context_die);
9487           return lookup_type_die (qualified_type);
9488         }
9489       else if (is_const_type < TYPE_READONLY (dtype)
9490                || is_volatile_type < TYPE_VOLATILE (dtype)
9491                || (is_const_type <= TYPE_READONLY (dtype)
9492                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9493                    && DECL_ORIGINAL_TYPE (name) != type))
9494         /* cv-unqualified version of named type.  Just use the unnamed
9495            type to which it refers.  */
9496         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9497                                   is_const_type, is_volatile_type,
9498                                   context_die);
9499       /* Else cv-qualified version of named type; fall through.  */
9500     }
9501
9502   if (is_const_type)
9503     {
9504       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9505       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9506     }
9507   else if (is_volatile_type)
9508     {
9509       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9510       sub_die = modified_type_die (type, 0, 0, context_die);
9511     }
9512   else if (code == POINTER_TYPE)
9513     {
9514       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9515       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9516                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9517       item_type = TREE_TYPE (type);
9518     }
9519   else if (code == REFERENCE_TYPE)
9520     {
9521       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9522       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9523                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9524       item_type = TREE_TYPE (type);
9525     }
9526   else if (is_subrange_type (type))
9527     {
9528       mod_type_die = subrange_type_die (type, context_die);
9529       item_type = TREE_TYPE (type);
9530     }
9531   else if (is_base_type (type))
9532     mod_type_die = base_type_die (type);
9533   else
9534     {
9535       gen_type_die (type, context_die);
9536
9537       /* We have to get the type_main_variant here (and pass that to the
9538          `lookup_type_die' routine) because the ..._TYPE node we have
9539          might simply be a *copy* of some original type node (where the
9540          copy was created to help us keep track of typedef names) and
9541          that copy might have a different TYPE_UID from the original
9542          ..._TYPE node.  */
9543       if (TREE_CODE (type) != VECTOR_TYPE)
9544         return lookup_type_die (type_main_variant (type));
9545       else
9546         /* Vectors have the debugging information in the type,
9547            not the main variant.  */
9548         return lookup_type_die (type);
9549     }
9550
9551   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9552      don't output a DW_TAG_typedef, since there isn't one in the
9553      user's program; just attach a DW_AT_name to the type.  */
9554   if (name
9555       && (TREE_CODE (name) != TYPE_DECL
9556           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9557     {
9558       if (TREE_CODE (name) == TYPE_DECL)
9559         /* Could just call add_name_and_src_coords_attributes here,
9560            but since this is a builtin type it doesn't have any
9561            useful source coordinates anyway.  */
9562         name = DECL_NAME (name);
9563       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9564     }
9565
9566   if (qualified_type)
9567     equate_type_number_to_die (qualified_type, mod_type_die);
9568
9569   if (item_type)
9570     /* We must do this after the equate_type_number_to_die call, in case
9571        this is a recursive type.  This ensures that the modified_type_die
9572        recursion will terminate even if the type is recursive.  Recursive
9573        types are possible in Ada.  */
9574     sub_die = modified_type_die (item_type,
9575                                  TYPE_READONLY (item_type),
9576                                  TYPE_VOLATILE (item_type),
9577                                  context_die);
9578
9579   if (sub_die != NULL)
9580     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9581
9582   return mod_type_die;
9583 }
9584
9585 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9586    an enumerated type.  */
9587
9588 static inline int
9589 type_is_enum (const_tree type)
9590 {
9591   return TREE_CODE (type) == ENUMERAL_TYPE;
9592 }
9593
9594 /* Return the DBX register number described by a given RTL node.  */
9595
9596 static unsigned int
9597 dbx_reg_number (const_rtx rtl)
9598 {
9599   unsigned regno = REGNO (rtl);
9600
9601   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9602
9603 #ifdef LEAF_REG_REMAP
9604   if (current_function_uses_only_leaf_regs)
9605     {
9606       int leaf_reg = LEAF_REG_REMAP (regno);
9607       if (leaf_reg != -1)
9608         regno = (unsigned) leaf_reg;
9609     }
9610 #endif
9611
9612   return DBX_REGISTER_NUMBER (regno);
9613 }
9614
9615 /* Optionally add a DW_OP_piece term to a location description expression.
9616    DW_OP_piece is only added if the location description expression already
9617    doesn't end with DW_OP_piece.  */
9618
9619 static void
9620 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9621 {
9622   dw_loc_descr_ref loc;
9623
9624   if (*list_head != NULL)
9625     {
9626       /* Find the end of the chain.  */
9627       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9628         ;
9629
9630       if (loc->dw_loc_opc != DW_OP_piece)
9631         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9632     }
9633 }
9634
9635 /* Return a location descriptor that designates a machine register or
9636    zero if there is none.  */
9637
9638 static dw_loc_descr_ref
9639 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9640 {
9641   rtx regs;
9642
9643   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9644     return 0;
9645
9646   regs = targetm.dwarf_register_span (rtl);
9647
9648   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9649     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9650   else
9651     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9652 }
9653
9654 /* Return a location descriptor that designates a machine register for
9655    a given hard register number.  */
9656
9657 static dw_loc_descr_ref
9658 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9659 {
9660   dw_loc_descr_ref reg_loc_descr = new_reg_loc_descr (regno, 0);
9661
9662   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9663     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9664
9665   return reg_loc_descr;
9666 }
9667
9668 /* Given an RTL of a register, return a location descriptor that
9669    designates a value that spans more than one register.  */
9670
9671 static dw_loc_descr_ref
9672 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9673                              enum var_init_status initialized)
9674 {
9675   int nregs, size, i;
9676   unsigned reg;
9677   dw_loc_descr_ref loc_result = NULL;
9678
9679   reg = REGNO (rtl);
9680 #ifdef LEAF_REG_REMAP
9681   if (current_function_uses_only_leaf_regs)
9682     {
9683       int leaf_reg = LEAF_REG_REMAP (reg);
9684       if (leaf_reg != -1)
9685         reg = (unsigned) leaf_reg;
9686     }
9687 #endif
9688   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9689   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9690
9691   /* Simple, contiguous registers.  */
9692   if (regs == NULL_RTX)
9693     {
9694       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9695
9696       loc_result = NULL;
9697       while (nregs--)
9698         {
9699           dw_loc_descr_ref t;
9700
9701           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9702                                       VAR_INIT_STATUS_INITIALIZED);
9703           add_loc_descr (&loc_result, t);
9704           add_loc_descr_op_piece (&loc_result, size);
9705           ++reg;
9706         }
9707       return loc_result;
9708     }
9709
9710   /* Now onto stupid register sets in non contiguous locations.  */
9711
9712   gcc_assert (GET_CODE (regs) == PARALLEL);
9713
9714   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9715   loc_result = NULL;
9716
9717   for (i = 0; i < XVECLEN (regs, 0); ++i)
9718     {
9719       dw_loc_descr_ref t;
9720
9721       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9722                                   VAR_INIT_STATUS_INITIALIZED);
9723       add_loc_descr (&loc_result, t);
9724       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9725       add_loc_descr_op_piece (&loc_result, size);
9726     }
9727
9728   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9729     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9730   return loc_result;
9731 }
9732
9733 #endif /* DWARF2_DEBUGGING_INFO */
9734
9735 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
9736
9737 /* Return a location descriptor that designates a constant.  */
9738
9739 static dw_loc_descr_ref
9740 int_loc_descriptor (HOST_WIDE_INT i)
9741 {
9742   enum dwarf_location_atom op;
9743
9744   /* Pick the smallest representation of a constant, rather than just
9745      defaulting to the LEB encoding.  */
9746   if (i >= 0)
9747     {
9748       if (i <= 31)
9749         op = DW_OP_lit0 + i;
9750       else if (i <= 0xff)
9751         op = DW_OP_const1u;
9752       else if (i <= 0xffff)
9753         op = DW_OP_const2u;
9754       else if (HOST_BITS_PER_WIDE_INT == 32
9755                || i <= 0xffffffff)
9756         op = DW_OP_const4u;
9757       else
9758         op = DW_OP_constu;
9759     }
9760   else
9761     {
9762       if (i >= -0x80)
9763         op = DW_OP_const1s;
9764       else if (i >= -0x8000)
9765         op = DW_OP_const2s;
9766       else if (HOST_BITS_PER_WIDE_INT == 32
9767                || i >= -0x80000000)
9768         op = DW_OP_const4s;
9769       else
9770         op = DW_OP_consts;
9771     }
9772
9773   return new_loc_descr (op, i, 0);
9774 }
9775 #endif
9776
9777 #ifdef DWARF2_DEBUGGING_INFO
9778
9779 /* Return a location descriptor that designates a base+offset location.  */
9780
9781 static dw_loc_descr_ref
9782 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9783                  enum var_init_status initialized)
9784 {
9785   unsigned int regno;
9786   dw_loc_descr_ref result;
9787   dw_fde_ref fde = current_fde ();
9788
9789   /* We only use "frame base" when we're sure we're talking about the
9790      post-prologue local stack frame.  We do this by *not* running
9791      register elimination until this point, and recognizing the special
9792      argument pointer and soft frame pointer rtx's.  */
9793   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9794     {
9795       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9796
9797       if (elim != reg)
9798         {
9799           if (GET_CODE (elim) == PLUS)
9800             {
9801               offset += INTVAL (XEXP (elim, 1));
9802               elim = XEXP (elim, 0);
9803             }
9804           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
9805                        && (elim == hard_frame_pointer_rtx
9806                            || elim == stack_pointer_rtx))
9807                       || elim == (frame_pointer_needed
9808                                   ? hard_frame_pointer_rtx
9809                                   : stack_pointer_rtx));
9810
9811           /* If drap register is used to align stack, use frame
9812              pointer + offset to access stack variables.  If stack
9813              is aligned without drap, use stack pointer + offset to
9814              access stack variables.  */
9815           if (crtl->stack_realign_tried
9816               && cfa.reg == HARD_FRAME_POINTER_REGNUM
9817               && reg == frame_pointer_rtx)
9818             {
9819               int base_reg
9820                 = DWARF_FRAME_REGNUM (cfa.indirect
9821                                       ? HARD_FRAME_POINTER_REGNUM
9822                                       : STACK_POINTER_REGNUM);
9823               return new_reg_loc_descr (base_reg, offset);
9824             }
9825
9826           offset += frame_pointer_fb_offset;
9827           return new_loc_descr (DW_OP_fbreg, offset, 0);
9828         }
9829     }
9830   else if (fde
9831            && fde->drap_reg != INVALID_REGNUM
9832            && (fde->drap_reg == REGNO (reg)
9833                || fde->vdrap_reg == REGNO (reg)))
9834     {
9835       /* Use cfa+offset to represent the location of arguments passed
9836          on stack when drap is used to align stack.  */
9837       return new_loc_descr (DW_OP_fbreg, offset, 0);
9838     }
9839
9840   regno = dbx_reg_number (reg);
9841   if (regno <= 31)
9842     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9843   else
9844     result = new_loc_descr (DW_OP_bregx, regno, offset);
9845
9846   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9847     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9848
9849   return result;
9850 }
9851
9852 /* Return true if this RTL expression describes a base+offset calculation.  */
9853
9854 static inline int
9855 is_based_loc (const_rtx rtl)
9856 {
9857   return (GET_CODE (rtl) == PLUS
9858           && ((REG_P (XEXP (rtl, 0))
9859                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9860                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9861 }
9862
9863 /* Return a descriptor that describes the concatenation of N locations
9864    used to form the address of a memory location.  */
9865
9866 static dw_loc_descr_ref
9867 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9868                             enum var_init_status initialized)
9869 {
9870   unsigned int i;
9871   dw_loc_descr_ref cc_loc_result = NULL;
9872   unsigned int n = XVECLEN (concatn, 0);
9873
9874   for (i = 0; i < n; ++i)
9875     {
9876       dw_loc_descr_ref ref;
9877       rtx x = XVECEXP (concatn, 0, i);
9878
9879       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9880       if (ref == NULL)
9881         return NULL;
9882
9883       add_loc_descr (&cc_loc_result, ref);
9884       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9885     }
9886
9887   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9888     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9889
9890   return cc_loc_result;
9891 }
9892
9893 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
9894    failed.  */
9895
9896 static dw_loc_descr_ref
9897 tls_mem_loc_descriptor (rtx mem)
9898 {
9899   tree base;
9900   dw_loc_descr_ref loc_result, loc_result2;
9901
9902   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
9903     return NULL;
9904
9905   base = get_base_address (MEM_EXPR (mem));
9906   if (base == NULL
9907       || TREE_CODE (base) != VAR_DECL
9908       || !DECL_THREAD_LOCAL_P (base))
9909     return NULL;
9910
9911   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
9912   if (loc_result == NULL)
9913     return NULL;
9914
9915   if (INTVAL (MEM_OFFSET (mem)))
9916     {
9917       if (INTVAL (MEM_OFFSET (mem)) >= 0)
9918         add_loc_descr (&loc_result,
9919                        new_loc_descr (DW_OP_plus_uconst,
9920                                       INTVAL (MEM_OFFSET (mem)), 0));
9921       else
9922         {
9923           loc_result2 = mem_loc_descriptor (MEM_OFFSET (mem), GET_MODE (mem),
9924                                             VAR_INIT_STATUS_INITIALIZED);
9925           if (loc_result2 == 0)
9926             return NULL;
9927           add_loc_descr (&loc_result, loc_result2);
9928           add_loc_descr (&loc_result, new_loc_descr (DW_OP_plus, 0, 0));
9929         }
9930     }
9931
9932   return loc_result;
9933 }
9934
9935 /* The following routine converts the RTL for a variable or parameter
9936    (resident in memory) into an equivalent Dwarf representation of a
9937    mechanism for getting the address of that same variable onto the top of a
9938    hypothetical "address evaluation" stack.
9939
9940    When creating memory location descriptors, we are effectively transforming
9941    the RTL for a memory-resident object into its Dwarf postfix expression
9942    equivalent.  This routine recursively descends an RTL tree, turning
9943    it into Dwarf postfix code as it goes.
9944
9945    MODE is the mode of the memory reference, needed to handle some
9946    autoincrement addressing modes.
9947
9948    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9949    location list for RTL.
9950
9951    Return 0 if we can't represent the location.  */
9952
9953 static dw_loc_descr_ref
9954 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9955                     enum var_init_status initialized)
9956 {
9957   dw_loc_descr_ref mem_loc_result = NULL;
9958   enum dwarf_location_atom op;
9959
9960   /* Note that for a dynamically sized array, the location we will generate a
9961      description of here will be the lowest numbered location which is
9962      actually within the array.  That's *not* necessarily the same as the
9963      zeroth element of the array.  */
9964
9965   rtl = targetm.delegitimize_address (rtl);
9966
9967   switch (GET_CODE (rtl))
9968     {
9969     case POST_INC:
9970     case POST_DEC:
9971     case POST_MODIFY:
9972       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9973          just fall into the SUBREG code.  */
9974
9975       /* ... fall through ...  */
9976
9977     case SUBREG:
9978       /* The case of a subreg may arise when we have a local (register)
9979          variable or a formal (register) parameter which doesn't quite fill
9980          up an entire register.  For now, just assume that it is
9981          legitimate to make the Dwarf info refer to the whole register which
9982          contains the given subreg.  */
9983       rtl = XEXP (rtl, 0);
9984
9985       /* ... fall through ...  */
9986
9987     case REG:
9988       /* Whenever a register number forms a part of the description of the
9989          method for calculating the (dynamic) address of a memory resident
9990          object, DWARF rules require the register number be referred to as
9991          a "base register".  This distinction is not based in any way upon
9992          what category of register the hardware believes the given register
9993          belongs to.  This is strictly DWARF terminology we're dealing with
9994          here. Note that in cases where the location of a memory-resident
9995          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9996          OP_CONST (0)) the actual DWARF location descriptor that we generate
9997          may just be OP_BASEREG (basereg).  This may look deceptively like
9998          the object in question was allocated to a register (rather than in
9999          memory) so DWARF consumers need to be aware of the subtle
10000          distinction between OP_REG and OP_BASEREG.  */
10001       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10002         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10003       break;
10004
10005     case MEM:
10006       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10007                                            VAR_INIT_STATUS_INITIALIZED);
10008       if (mem_loc_result == NULL)
10009         mem_loc_result = tls_mem_loc_descriptor (rtl);
10010       if (mem_loc_result != 0)
10011         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10012       break;
10013
10014     case LO_SUM:
10015          rtl = XEXP (rtl, 1);
10016
10017       /* ... fall through ...  */
10018
10019     case LABEL_REF:
10020       /* Some ports can transform a symbol ref into a label ref, because
10021          the symbol ref is too far away and has to be dumped into a constant
10022          pool.  */
10023     case CONST:
10024     case SYMBOL_REF:
10025       /* Alternatively, the symbol in the constant pool might be referenced
10026          by a different symbol.  */
10027       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10028         {
10029           bool marked;
10030           rtx tmp = get_pool_constant_mark (rtl, &marked);
10031
10032           if (GET_CODE (tmp) == SYMBOL_REF)
10033             {
10034               rtl = tmp;
10035               if (CONSTANT_POOL_ADDRESS_P (tmp))
10036                 get_pool_constant_mark (tmp, &marked);
10037               else
10038                 marked = true;
10039             }
10040
10041           /* If all references to this pool constant were optimized away,
10042              it was not output and thus we can't represent it.
10043              FIXME: might try to use DW_OP_const_value here, though
10044              DW_OP_piece complicates it.  */
10045           if (!marked)
10046             return 0;
10047         }
10048
10049       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10050       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10051       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10052       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10053       break;
10054
10055     case PRE_MODIFY:
10056       /* Extract the PLUS expression nested inside and fall into
10057          PLUS code below.  */
10058       rtl = XEXP (rtl, 1);
10059       goto plus;
10060
10061     case PRE_INC:
10062     case PRE_DEC:
10063       /* Turn these into a PLUS expression and fall into the PLUS code
10064          below.  */
10065       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10066                           GEN_INT (GET_CODE (rtl) == PRE_INC
10067                                    ? GET_MODE_UNIT_SIZE (mode)
10068                                    : -GET_MODE_UNIT_SIZE (mode)));
10069
10070       /* ... fall through ...  */
10071
10072     case PLUS:
10073     plus:
10074       if (is_based_loc (rtl))
10075         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10076                                           INTVAL (XEXP (rtl, 1)),
10077                                           VAR_INIT_STATUS_INITIALIZED);
10078       else
10079         {
10080           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10081                                                VAR_INIT_STATUS_INITIALIZED);
10082           if (mem_loc_result == 0)
10083             break;
10084
10085           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
10086               && INTVAL (XEXP (rtl, 1)) >= 0)
10087             add_loc_descr (&mem_loc_result,
10088                            new_loc_descr (DW_OP_plus_uconst,
10089                                           INTVAL (XEXP (rtl, 1)), 0));
10090           else
10091             {
10092               dw_loc_descr_ref mem_loc_result2
10093                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10094                                       VAR_INIT_STATUS_INITIALIZED);
10095               if (mem_loc_result2 == 0)
10096                 break;
10097               add_loc_descr (&mem_loc_result, mem_loc_result2);
10098               add_loc_descr (&mem_loc_result,
10099                              new_loc_descr (DW_OP_plus, 0, 0));
10100             }
10101         }
10102       break;
10103
10104     /* If a pseudo-reg is optimized away, it is possible for it to
10105        be replaced with a MEM containing a multiply or shift.  */
10106     case MULT:
10107       op = DW_OP_mul;
10108       goto do_binop;
10109
10110     case ASHIFT:
10111       op = DW_OP_shl;
10112       goto do_binop;
10113
10114     case ASHIFTRT:
10115       op = DW_OP_shra;
10116       goto do_binop;
10117
10118     case LSHIFTRT:
10119       op = DW_OP_shr;
10120       goto do_binop;
10121
10122     do_binop:
10123       {
10124         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10125                                                    VAR_INIT_STATUS_INITIALIZED);
10126         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10127                                                    VAR_INIT_STATUS_INITIALIZED);
10128
10129         if (op0 == 0 || op1 == 0)
10130           break;
10131
10132         mem_loc_result = op0;
10133         add_loc_descr (&mem_loc_result, op1);
10134         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10135         break;
10136       }
10137
10138     case CONST_INT:
10139       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10140       break;
10141
10142     case CONCATN:
10143       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10144                                                    VAR_INIT_STATUS_INITIALIZED);
10145       break;
10146
10147     case UNSPEC:
10148       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10149          can't express it in the debug info.  This can happen e.g. with some
10150          TLS UNSPECs.  */
10151       break;
10152
10153     default:
10154       gcc_unreachable ();
10155     }
10156
10157   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10158     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10159
10160   return mem_loc_result;
10161 }
10162
10163 /* Return a descriptor that describes the concatenation of two locations.
10164    This is typically a complex variable.  */
10165
10166 static dw_loc_descr_ref
10167 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10168 {
10169   dw_loc_descr_ref cc_loc_result = NULL;
10170   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10171   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10172
10173   if (x0_ref == 0 || x1_ref == 0)
10174     return 0;
10175
10176   cc_loc_result = x0_ref;
10177   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10178
10179   add_loc_descr (&cc_loc_result, x1_ref);
10180   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10181
10182   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10183     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10184
10185   return cc_loc_result;
10186 }
10187
10188 /* Return a descriptor that describes the concatenation of N
10189    locations.  */
10190
10191 static dw_loc_descr_ref
10192 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10193 {
10194   unsigned int i;
10195   dw_loc_descr_ref cc_loc_result = NULL;
10196   unsigned int n = XVECLEN (concatn, 0);
10197
10198   for (i = 0; i < n; ++i)
10199     {
10200       dw_loc_descr_ref ref;
10201       rtx x = XVECEXP (concatn, 0, i);
10202
10203       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10204       if (ref == NULL)
10205         return NULL;
10206
10207       add_loc_descr (&cc_loc_result, ref);
10208       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10209     }
10210
10211   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10212     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10213
10214   return cc_loc_result;
10215 }
10216
10217 /* Output a proper Dwarf location descriptor for a variable or parameter
10218    which is either allocated in a register or in a memory location.  For a
10219    register, we just generate an OP_REG and the register number.  For a
10220    memory location we provide a Dwarf postfix expression describing how to
10221    generate the (dynamic) address of the object onto the address stack.
10222
10223    If we don't know how to describe it, return 0.  */
10224
10225 static dw_loc_descr_ref
10226 loc_descriptor (rtx rtl, enum var_init_status initialized)
10227 {
10228   dw_loc_descr_ref loc_result = NULL;
10229
10230   switch (GET_CODE (rtl))
10231     {
10232     case SUBREG:
10233       /* The case of a subreg may arise when we have a local (register)
10234          variable or a formal (register) parameter which doesn't quite fill
10235          up an entire register.  For now, just assume that it is
10236          legitimate to make the Dwarf info refer to the whole register which
10237          contains the given subreg.  */
10238       rtl = SUBREG_REG (rtl);
10239
10240       /* ... fall through ...  */
10241
10242     case REG:
10243       loc_result = reg_loc_descriptor (rtl, initialized);
10244       break;
10245
10246     case MEM:
10247       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10248                                        initialized);
10249       if (loc_result == NULL)
10250         loc_result = tls_mem_loc_descriptor (rtl);
10251       break;
10252
10253     case CONCAT:
10254       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10255                                           initialized);
10256       break;
10257
10258     case CONCATN:
10259       loc_result = concatn_loc_descriptor (rtl, initialized);
10260       break;
10261
10262     case VAR_LOCATION:
10263       /* Single part.  */
10264       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10265         {
10266           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10267           break;
10268         }
10269
10270       rtl = XEXP (rtl, 1);
10271       /* FALLTHRU */
10272
10273     case PARALLEL:
10274       {
10275         rtvec par_elems = XVEC (rtl, 0);
10276         int num_elem = GET_NUM_ELEM (par_elems);
10277         enum machine_mode mode;
10278         int i;
10279
10280         /* Create the first one, so we have something to add to.  */
10281         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10282                                      initialized);
10283         if (loc_result == NULL)
10284           return NULL;
10285         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10286         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10287         for (i = 1; i < num_elem; i++)
10288           {
10289             dw_loc_descr_ref temp;
10290
10291             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10292                                    initialized);
10293             if (temp == NULL)
10294               return NULL;
10295             add_loc_descr (&loc_result, temp);
10296             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10297             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10298           }
10299       }
10300       break;
10301
10302     default:
10303       gcc_unreachable ();
10304     }
10305
10306   return loc_result;
10307 }
10308
10309 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10310    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10311    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10312    top-level invocation, and we require the address of LOC; is 0 if we require
10313    the value of LOC.  */
10314
10315 static dw_loc_descr_ref
10316 loc_descriptor_from_tree_1 (tree loc, int want_address)
10317 {
10318   dw_loc_descr_ref ret, ret1;
10319   int have_address = 0;
10320   enum dwarf_location_atom op;
10321
10322   /* ??? Most of the time we do not take proper care for sign/zero
10323      extending the values properly.  Hopefully this won't be a real
10324      problem...  */
10325
10326   switch (TREE_CODE (loc))
10327     {
10328     case ERROR_MARK:
10329       return 0;
10330
10331     case PLACEHOLDER_EXPR:
10332       /* This case involves extracting fields from an object to determine the
10333          position of other fields.  We don't try to encode this here.  The
10334          only user of this is Ada, which encodes the needed information using
10335          the names of types.  */
10336       return 0;
10337
10338     case CALL_EXPR:
10339       return 0;
10340
10341     case PREINCREMENT_EXPR:
10342     case PREDECREMENT_EXPR:
10343     case POSTINCREMENT_EXPR:
10344     case POSTDECREMENT_EXPR:
10345       /* There are no opcodes for these operations.  */
10346       return 0;
10347
10348     case ADDR_EXPR:
10349       /* If we already want an address, there's nothing we can do.  */
10350       if (want_address)
10351         return 0;
10352
10353       /* Otherwise, process the argument and look for the address.  */
10354       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10355
10356     case VAR_DECL:
10357       if (DECL_THREAD_LOCAL_P (loc))
10358         {
10359           rtx rtl;
10360           unsigned first_op;
10361           unsigned second_op;
10362
10363           if (targetm.have_tls)
10364             {
10365               /* If this is not defined, we have no way to emit the
10366                  data.  */
10367               if (!targetm.asm_out.output_dwarf_dtprel)
10368                 return 0;
10369
10370                /* The way DW_OP_GNU_push_tls_address is specified, we
10371                   can only look up addresses of objects in the current
10372                   module.  */
10373               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10374                 return 0;
10375               first_op = INTERNAL_DW_OP_tls_addr;
10376               second_op = DW_OP_GNU_push_tls_address;
10377             }
10378           else
10379             {
10380               if (!targetm.emutls.debug_form_tls_address)
10381                 return 0;
10382               loc = emutls_decl (loc);
10383               first_op = DW_OP_addr;
10384               second_op = DW_OP_form_tls_address;
10385             }
10386
10387           rtl = rtl_for_decl_location (loc);
10388           if (rtl == NULL_RTX)
10389             return 0;
10390
10391           if (!MEM_P (rtl))
10392             return 0;
10393           rtl = XEXP (rtl, 0);
10394           if (! CONSTANT_P (rtl))
10395             return 0;
10396
10397           ret = new_loc_descr (first_op, 0, 0);
10398           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10399           ret->dw_loc_oprnd1.v.val_addr = rtl;
10400
10401           ret1 = new_loc_descr (second_op, 0, 0);
10402           add_loc_descr (&ret, ret1);
10403
10404           have_address = 1;
10405           break;
10406         }
10407       /* FALLTHRU */
10408
10409     case PARM_DECL:
10410       if (DECL_HAS_VALUE_EXPR_P (loc))
10411         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10412                                            want_address);
10413       /* FALLTHRU */
10414
10415     case RESULT_DECL:
10416     case FUNCTION_DECL:
10417       {
10418         rtx rtl = rtl_for_decl_location (loc);
10419
10420         if (rtl == NULL_RTX)
10421           return 0;
10422         else if (GET_CODE (rtl) == CONST_INT)
10423           {
10424             HOST_WIDE_INT val = INTVAL (rtl);
10425             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10426               val &= GET_MODE_MASK (DECL_MODE (loc));
10427             ret = int_loc_descriptor (val);
10428           }
10429         else if (GET_CODE (rtl) == CONST_STRING)
10430           return 0;
10431         else if (CONSTANT_P (rtl))
10432           {
10433             ret = new_loc_descr (DW_OP_addr, 0, 0);
10434             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10435             ret->dw_loc_oprnd1.v.val_addr = rtl;
10436           }
10437         else
10438           {
10439             enum machine_mode mode;
10440
10441             /* Certain constructs can only be represented at top-level.  */
10442             if (want_address == 2)
10443               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10444
10445             mode = GET_MODE (rtl);
10446             if (MEM_P (rtl))
10447               {
10448                 rtl = XEXP (rtl, 0);
10449                 have_address = 1;
10450               }
10451             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10452           }
10453       }
10454       break;
10455
10456     case INDIRECT_REF:
10457       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10458       have_address = 1;
10459       break;
10460
10461     case COMPOUND_EXPR:
10462       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10463
10464     CASE_CONVERT:
10465     case VIEW_CONVERT_EXPR:
10466     case SAVE_EXPR:
10467     case MODIFY_EXPR:
10468       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10469
10470     case COMPONENT_REF:
10471     case BIT_FIELD_REF:
10472     case ARRAY_REF:
10473     case ARRAY_RANGE_REF:
10474       {
10475         tree obj, offset;
10476         HOST_WIDE_INT bitsize, bitpos, bytepos;
10477         enum machine_mode mode;
10478         int volatilep;
10479         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10480
10481         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10482                                    &unsignedp, &volatilep, false);
10483
10484         if (obj == loc)
10485           return 0;
10486
10487         ret = loc_descriptor_from_tree_1 (obj, 1);
10488         if (ret == 0
10489             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10490           return 0;
10491
10492         if (offset != NULL_TREE)
10493           {
10494             /* Variable offset.  */
10495             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10496             if (ret1 == 0)
10497               return 0;
10498             add_loc_descr (&ret, ret1);
10499             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10500           }
10501
10502         bytepos = bitpos / BITS_PER_UNIT;
10503         if (bytepos > 0)
10504           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
10505         else if (bytepos < 0)
10506           {
10507             add_loc_descr (&ret, int_loc_descriptor (bytepos));
10508             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10509           }
10510
10511         have_address = 1;
10512         break;
10513       }
10514
10515     case INTEGER_CST:
10516       if (host_integerp (loc, 0))
10517         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10518       else
10519         return 0;
10520       break;
10521
10522     case CONSTRUCTOR:
10523       {
10524         /* Get an RTL for this, if something has been emitted.  */
10525         rtx rtl = lookup_constant_def (loc);
10526         enum machine_mode mode;
10527
10528         if (!rtl || !MEM_P (rtl))
10529           return 0;
10530         mode = GET_MODE (rtl);
10531         rtl = XEXP (rtl, 0);
10532         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10533         have_address = 1;
10534         break;
10535       }
10536
10537     case TRUTH_AND_EXPR:
10538     case TRUTH_ANDIF_EXPR:
10539     case BIT_AND_EXPR:
10540       op = DW_OP_and;
10541       goto do_binop;
10542
10543     case TRUTH_XOR_EXPR:
10544     case BIT_XOR_EXPR:
10545       op = DW_OP_xor;
10546       goto do_binop;
10547
10548     case TRUTH_OR_EXPR:
10549     case TRUTH_ORIF_EXPR:
10550     case BIT_IOR_EXPR:
10551       op = DW_OP_or;
10552       goto do_binop;
10553
10554     case FLOOR_DIV_EXPR:
10555     case CEIL_DIV_EXPR:
10556     case ROUND_DIV_EXPR:
10557     case TRUNC_DIV_EXPR:
10558       op = DW_OP_div;
10559       goto do_binop;
10560
10561     case MINUS_EXPR:
10562       op = DW_OP_minus;
10563       goto do_binop;
10564
10565     case FLOOR_MOD_EXPR:
10566     case CEIL_MOD_EXPR:
10567     case ROUND_MOD_EXPR:
10568     case TRUNC_MOD_EXPR:
10569       op = DW_OP_mod;
10570       goto do_binop;
10571
10572     case MULT_EXPR:
10573       op = DW_OP_mul;
10574       goto do_binop;
10575
10576     case LSHIFT_EXPR:
10577       op = DW_OP_shl;
10578       goto do_binop;
10579
10580     case RSHIFT_EXPR:
10581       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10582       goto do_binop;
10583
10584     case POINTER_PLUS_EXPR:
10585     case PLUS_EXPR:
10586       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10587           && host_integerp (TREE_OPERAND (loc, 1), 0))
10588         {
10589           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10590           if (ret == 0)
10591             return 0;
10592
10593           add_loc_descr (&ret,
10594                          new_loc_descr (DW_OP_plus_uconst,
10595                                         tree_low_cst (TREE_OPERAND (loc, 1),
10596                                                       0),
10597                                         0));
10598           break;
10599         }
10600
10601       op = DW_OP_plus;
10602       goto do_binop;
10603
10604     case LE_EXPR:
10605       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10606         return 0;
10607
10608       op = DW_OP_le;
10609       goto do_binop;
10610
10611     case GE_EXPR:
10612       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10613         return 0;
10614
10615       op = DW_OP_ge;
10616       goto do_binop;
10617
10618     case LT_EXPR:
10619       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10620         return 0;
10621
10622       op = DW_OP_lt;
10623       goto do_binop;
10624
10625     case GT_EXPR:
10626       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10627         return 0;
10628
10629       op = DW_OP_gt;
10630       goto do_binop;
10631
10632     case EQ_EXPR:
10633       op = DW_OP_eq;
10634       goto do_binop;
10635
10636     case NE_EXPR:
10637       op = DW_OP_ne;
10638       goto do_binop;
10639
10640     do_binop:
10641       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10642       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10643       if (ret == 0 || ret1 == 0)
10644         return 0;
10645
10646       add_loc_descr (&ret, ret1);
10647       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10648       break;
10649
10650     case TRUTH_NOT_EXPR:
10651     case BIT_NOT_EXPR:
10652       op = DW_OP_not;
10653       goto do_unop;
10654
10655     case ABS_EXPR:
10656       op = DW_OP_abs;
10657       goto do_unop;
10658
10659     case NEGATE_EXPR:
10660       op = DW_OP_neg;
10661       goto do_unop;
10662
10663     do_unop:
10664       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10665       if (ret == 0)
10666         return 0;
10667
10668       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10669       break;
10670
10671     case MIN_EXPR:
10672     case MAX_EXPR:
10673       {
10674         const enum tree_code code =
10675           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10676
10677         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10678                       build2 (code, integer_type_node,
10679                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10680                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10681       }
10682
10683       /* ... fall through ...  */
10684
10685     case COND_EXPR:
10686       {
10687         dw_loc_descr_ref lhs
10688           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10689         dw_loc_descr_ref rhs
10690           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10691         dw_loc_descr_ref bra_node, jump_node, tmp;
10692
10693         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10694         if (ret == 0 || lhs == 0 || rhs == 0)
10695           return 0;
10696
10697         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10698         add_loc_descr (&ret, bra_node);
10699
10700         add_loc_descr (&ret, rhs);
10701         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
10702         add_loc_descr (&ret, jump_node);
10703
10704         add_loc_descr (&ret, lhs);
10705         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10706         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
10707
10708         /* ??? Need a node to point the skip at.  Use a nop.  */
10709         tmp = new_loc_descr (DW_OP_nop, 0, 0);
10710         add_loc_descr (&ret, tmp);
10711         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10712         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
10713       }
10714       break;
10715
10716     case FIX_TRUNC_EXPR:
10717       return 0;
10718
10719     default:
10720       /* Leave front-end specific codes as simply unknown.  This comes
10721          up, for instance, with the C STMT_EXPR.  */
10722       if ((unsigned int) TREE_CODE (loc)
10723           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
10724         return 0;
10725
10726 #ifdef ENABLE_CHECKING
10727       /* Otherwise this is a generic code; we should just lists all of
10728          these explicitly.  We forgot one.  */
10729       gcc_unreachable ();
10730 #else
10731       /* In a release build, we want to degrade gracefully: better to
10732          generate incomplete debugging information than to crash.  */
10733       return NULL;
10734 #endif
10735     }
10736
10737   /* Show if we can't fill the request for an address.  */
10738   if (want_address && !have_address)
10739     return 0;
10740
10741   /* If we've got an address and don't want one, dereference.  */
10742   if (!want_address && have_address && ret)
10743     {
10744       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
10745
10746       if (size > DWARF2_ADDR_SIZE || size == -1)
10747         return 0;
10748       else if (size == DWARF2_ADDR_SIZE)
10749         op = DW_OP_deref;
10750       else
10751         op = DW_OP_deref_size;
10752
10753       add_loc_descr (&ret, new_loc_descr (op, size, 0));
10754     }
10755
10756   return ret;
10757 }
10758
10759 static inline dw_loc_descr_ref
10760 loc_descriptor_from_tree (tree loc)
10761 {
10762   return loc_descriptor_from_tree_1 (loc, 2);
10763 }
10764
10765 /* Given a value, round it up to the lowest multiple of `boundary'
10766    which is not less than the value itself.  */
10767
10768 static inline HOST_WIDE_INT
10769 ceiling (HOST_WIDE_INT value, unsigned int boundary)
10770 {
10771   return (((value + boundary - 1) / boundary) * boundary);
10772 }
10773
10774 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
10775    pointer to the declared type for the relevant field variable, or return
10776    `integer_type_node' if the given node turns out to be an
10777    ERROR_MARK node.  */
10778
10779 static inline tree
10780 field_type (const_tree decl)
10781 {
10782   tree type;
10783
10784   if (TREE_CODE (decl) == ERROR_MARK)
10785     return integer_type_node;
10786
10787   type = DECL_BIT_FIELD_TYPE (decl);
10788   if (type == NULL_TREE)
10789     type = TREE_TYPE (decl);
10790
10791   return type;
10792 }
10793
10794 /* Given a pointer to a tree node, return the alignment in bits for
10795    it, or else return BITS_PER_WORD if the node actually turns out to
10796    be an ERROR_MARK node.  */
10797
10798 static inline unsigned
10799 simple_type_align_in_bits (const_tree type)
10800 {
10801   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
10802 }
10803
10804 static inline unsigned
10805 simple_decl_align_in_bits (const_tree decl)
10806 {
10807   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
10808 }
10809
10810 /* Return the result of rounding T up to ALIGN.  */
10811
10812 static inline HOST_WIDE_INT
10813 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
10814 {
10815   /* We must be careful if T is negative because HOST_WIDE_INT can be
10816      either "above" or "below" unsigned int as per the C promotion
10817      rules, depending on the host, thus making the signedness of the
10818      direct multiplication and division unpredictable.  */
10819   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
10820
10821   u += align - 1;
10822   u /= align;
10823   u *= align;
10824
10825   return (HOST_WIDE_INT) u;
10826 }
10827
10828 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
10829    lowest addressed byte of the "containing object" for the given FIELD_DECL,
10830    or return 0 if we are unable to determine what that offset is, either
10831    because the argument turns out to be a pointer to an ERROR_MARK node, or
10832    because the offset is actually variable.  (We can't handle the latter case
10833    just yet).  */
10834
10835 static HOST_WIDE_INT
10836 field_byte_offset (const_tree decl)
10837 {
10838   HOST_WIDE_INT object_offset_in_bits;
10839   HOST_WIDE_INT bitpos_int;
10840
10841   if (TREE_CODE (decl) == ERROR_MARK)
10842     return 0;
10843
10844   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10845
10846   /* We cannot yet cope with fields whose positions are variable, so
10847      for now, when we see such things, we simply return 0.  Someday, we may
10848      be able to handle such cases, but it will be damn difficult.  */
10849   if (! host_integerp (bit_position (decl), 0))
10850     return 0;
10851
10852   bitpos_int = int_bit_position (decl);
10853
10854 #ifdef PCC_BITFIELD_TYPE_MATTERS
10855   if (PCC_BITFIELD_TYPE_MATTERS)
10856     {
10857       tree type;
10858       tree field_size_tree;
10859       HOST_WIDE_INT deepest_bitpos;
10860       unsigned HOST_WIDE_INT field_size_in_bits;
10861       unsigned int type_align_in_bits;
10862       unsigned int decl_align_in_bits;
10863       unsigned HOST_WIDE_INT type_size_in_bits;
10864
10865       type = field_type (decl);
10866       field_size_tree = DECL_SIZE (decl);
10867
10868       /* The size could be unspecified if there was an error, or for
10869          a flexible array member.  */
10870       if (! field_size_tree)
10871         field_size_tree = bitsize_zero_node;
10872
10873       /* If we don't know the size of the field, pretend it's a full word.  */
10874       if (host_integerp (field_size_tree, 1))
10875         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10876       else
10877         field_size_in_bits = BITS_PER_WORD;
10878
10879       type_size_in_bits = simple_type_size_in_bits (type);
10880       type_align_in_bits = simple_type_align_in_bits (type);
10881       decl_align_in_bits = simple_decl_align_in_bits (decl);
10882
10883       /* The GCC front-end doesn't make any attempt to keep track of the
10884          starting bit offset (relative to the start of the containing
10885          structure type) of the hypothetical "containing object" for a
10886          bit-field.  Thus, when computing the byte offset value for the
10887          start of the "containing object" of a bit-field, we must deduce
10888          this information on our own. This can be rather tricky to do in
10889          some cases.  For example, handling the following structure type
10890          definition when compiling for an i386/i486 target (which only
10891          aligns long long's to 32-bit boundaries) can be very tricky:
10892
10893          struct S { int field1; long long field2:31; };
10894
10895          Fortunately, there is a simple rule-of-thumb which can be used
10896          in such cases.  When compiling for an i386/i486, GCC will
10897          allocate 8 bytes for the structure shown above.  It decides to
10898          do this based upon one simple rule for bit-field allocation.
10899          GCC allocates each "containing object" for each bit-field at
10900          the first (i.e. lowest addressed) legitimate alignment boundary
10901          (based upon the required minimum alignment for the declared
10902          type of the field) which it can possibly use, subject to the
10903          condition that there is still enough available space remaining
10904          in the containing object (when allocated at the selected point)
10905          to fully accommodate all of the bits of the bit-field itself.
10906
10907          This simple rule makes it obvious why GCC allocates 8 bytes for
10908          each object of the structure type shown above.  When looking
10909          for a place to allocate the "containing object" for `field2',
10910          the compiler simply tries to allocate a 64-bit "containing
10911          object" at each successive 32-bit boundary (starting at zero)
10912          until it finds a place to allocate that 64- bit field such that
10913          at least 31 contiguous (and previously unallocated) bits remain
10914          within that selected 64 bit field.  (As it turns out, for the
10915          example above, the compiler finds it is OK to allocate the
10916          "containing object" 64-bit field at bit-offset zero within the
10917          structure type.)
10918
10919          Here we attempt to work backwards from the limited set of facts
10920          we're given, and we try to deduce from those facts, where GCC
10921          must have believed that the containing object started (within
10922          the structure type). The value we deduce is then used (by the
10923          callers of this routine) to generate DW_AT_location and
10924          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10925          the case of DW_AT_location, regular fields as well).  */
10926
10927       /* Figure out the bit-distance from the start of the structure to
10928          the "deepest" bit of the bit-field.  */
10929       deepest_bitpos = bitpos_int + field_size_in_bits;
10930
10931       /* This is the tricky part.  Use some fancy footwork to deduce
10932          where the lowest addressed bit of the containing object must
10933          be.  */
10934       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10935
10936       /* Round up to type_align by default.  This works best for
10937          bitfields.  */
10938       object_offset_in_bits
10939         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10940
10941       if (object_offset_in_bits > bitpos_int)
10942         {
10943           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10944
10945           /* Round up to decl_align instead.  */
10946           object_offset_in_bits
10947             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10948         }
10949     }
10950   else
10951 #endif
10952     object_offset_in_bits = bitpos_int;
10953
10954   return object_offset_in_bits / BITS_PER_UNIT;
10955 }
10956 \f
10957 /* The following routines define various Dwarf attributes and any data
10958    associated with them.  */
10959
10960 /* Add a location description attribute value to a DIE.
10961
10962    This emits location attributes suitable for whole variables and
10963    whole parameters.  Note that the location attributes for struct fields are
10964    generated by the routine `data_member_location_attribute' below.  */
10965
10966 static inline void
10967 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10968                              dw_loc_descr_ref descr)
10969 {
10970   if (descr != 0)
10971     add_AT_loc (die, attr_kind, descr);
10972 }
10973
10974 /* Attach the specialized form of location attribute used for data members of
10975    struct and union types.  In the special case of a FIELD_DECL node which
10976    represents a bit-field, the "offset" part of this special location
10977    descriptor must indicate the distance in bytes from the lowest-addressed
10978    byte of the containing struct or union type to the lowest-addressed byte of
10979    the "containing object" for the bit-field.  (See the `field_byte_offset'
10980    function above).
10981
10982    For any given bit-field, the "containing object" is a hypothetical object
10983    (of some integral or enum type) within which the given bit-field lives.  The
10984    type of this hypothetical "containing object" is always the same as the
10985    declared type of the individual bit-field itself (for GCC anyway... the
10986    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10987    bytes) of the hypothetical "containing object" which will be given in the
10988    DW_AT_byte_size attribute for this bit-field.  (See the
10989    `byte_size_attribute' function below.)  It is also used when calculating the
10990    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10991    function below.)  */
10992
10993 static void
10994 add_data_member_location_attribute (dw_die_ref die, tree decl)
10995 {
10996   HOST_WIDE_INT offset;
10997   dw_loc_descr_ref loc_descr = 0;
10998
10999   if (TREE_CODE (decl) == TREE_BINFO)
11000     {
11001       /* We're working on the TAG_inheritance for a base class.  */
11002       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11003         {
11004           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11005              aren't at a fixed offset from all (sub)objects of the same
11006              type.  We need to extract the appropriate offset from our
11007              vtable.  The following dwarf expression means
11008
11009                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11010
11011              This is specific to the V3 ABI, of course.  */
11012
11013           dw_loc_descr_ref tmp;
11014
11015           /* Make a copy of the object address.  */
11016           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11017           add_loc_descr (&loc_descr, tmp);
11018
11019           /* Extract the vtable address.  */
11020           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11021           add_loc_descr (&loc_descr, tmp);
11022
11023           /* Calculate the address of the offset.  */
11024           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11025           gcc_assert (offset < 0);
11026
11027           tmp = int_loc_descriptor (-offset);
11028           add_loc_descr (&loc_descr, tmp);
11029           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11030           add_loc_descr (&loc_descr, tmp);
11031
11032           /* Extract the offset.  */
11033           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11034           add_loc_descr (&loc_descr, tmp);
11035
11036           /* Add it to the object address.  */
11037           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11038           add_loc_descr (&loc_descr, tmp);
11039         }
11040       else
11041         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11042     }
11043   else
11044     offset = field_byte_offset (decl);
11045
11046   if (! loc_descr)
11047     {
11048       enum dwarf_location_atom op;
11049
11050       /* The DWARF2 standard says that we should assume that the structure
11051          address is already on the stack, so we can specify a structure field
11052          address by using DW_OP_plus_uconst.  */
11053
11054 #ifdef MIPS_DEBUGGING_INFO
11055       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11056          operator correctly.  It works only if we leave the offset on the
11057          stack.  */
11058       op = DW_OP_constu;
11059 #else
11060       op = DW_OP_plus_uconst;
11061 #endif
11062
11063       loc_descr = new_loc_descr (op, offset, 0);
11064     }
11065
11066   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11067 }
11068
11069 /* Writes integer values to dw_vec_const array.  */
11070
11071 static void
11072 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11073 {
11074   while (size != 0)
11075     {
11076       *dest++ = val & 0xff;
11077       val >>= 8;
11078       --size;
11079     }
11080 }
11081
11082 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11083
11084 static HOST_WIDE_INT
11085 extract_int (const unsigned char *src, unsigned int size)
11086 {
11087   HOST_WIDE_INT val = 0;
11088
11089   src += size;
11090   while (size != 0)
11091     {
11092       val <<= 8;
11093       val |= *--src & 0xff;
11094       --size;
11095     }
11096   return val;
11097 }
11098
11099 /* Writes floating point values to dw_vec_const array.  */
11100
11101 static void
11102 insert_float (const_rtx rtl, unsigned char *array)
11103 {
11104   REAL_VALUE_TYPE rv;
11105   long val[4];
11106   int i;
11107
11108   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11109   real_to_target (val, &rv, GET_MODE (rtl));
11110
11111   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11112   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11113     {
11114       insert_int (val[i], 4, array);
11115       array += 4;
11116     }
11117 }
11118
11119 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11120    does not have a "location" either in memory or in a register.  These
11121    things can arise in GNU C when a constant is passed as an actual parameter
11122    to an inlined function.  They can also arise in C++ where declared
11123    constants do not necessarily get memory "homes".  */
11124
11125 static void
11126 add_const_value_attribute (dw_die_ref die, rtx rtl)
11127 {
11128   switch (GET_CODE (rtl))
11129     {
11130     case CONST_INT:
11131       {
11132         HOST_WIDE_INT val = INTVAL (rtl);
11133
11134         if (val < 0)
11135           add_AT_int (die, DW_AT_const_value, val);
11136         else
11137           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11138       }
11139       break;
11140
11141     case CONST_DOUBLE:
11142       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11143          floating-point constant.  A CONST_DOUBLE is used whenever the
11144          constant requires more than one word in order to be adequately
11145          represented.  We output CONST_DOUBLEs as blocks.  */
11146       {
11147         enum machine_mode mode = GET_MODE (rtl);
11148
11149         if (SCALAR_FLOAT_MODE_P (mode))
11150           {
11151             unsigned int length = GET_MODE_SIZE (mode);
11152             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11153
11154             insert_float (rtl, array);
11155             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11156           }
11157         else
11158           {
11159             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11160             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11161
11162             add_AT_long_long (die, DW_AT_const_value,
11163                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11164           }
11165       }
11166       break;
11167
11168     case CONST_VECTOR:
11169       {
11170         enum machine_mode mode = GET_MODE (rtl);
11171         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11172         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11173         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11174         unsigned int i;
11175         unsigned char *p;
11176
11177         switch (GET_MODE_CLASS (mode))
11178           {
11179           case MODE_VECTOR_INT:
11180             for (i = 0, p = array; i < length; i++, p += elt_size)
11181               {
11182                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11183                 HOST_WIDE_INT lo, hi;
11184
11185                 switch (GET_CODE (elt))
11186                   {
11187                   case CONST_INT:
11188                     lo = INTVAL (elt);
11189                     hi = -(lo < 0);
11190                     break;
11191
11192                   case CONST_DOUBLE:
11193                     lo = CONST_DOUBLE_LOW (elt);
11194                     hi = CONST_DOUBLE_HIGH (elt);
11195                     break;
11196
11197                   default:
11198                     gcc_unreachable ();
11199                   }
11200
11201                 if (elt_size <= sizeof (HOST_WIDE_INT))
11202                   insert_int (lo, elt_size, p);
11203                 else
11204                   {
11205                     unsigned char *p0 = p;
11206                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11207
11208                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11209                     if (WORDS_BIG_ENDIAN)
11210                       {
11211                         p0 = p1;
11212                         p1 = p;
11213                       }
11214                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11215                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11216                   }
11217               }
11218             break;
11219
11220           case MODE_VECTOR_FLOAT:
11221             for (i = 0, p = array; i < length; i++, p += elt_size)
11222               {
11223                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11224                 insert_float (elt, p);
11225               }
11226             break;
11227
11228           default:
11229             gcc_unreachable ();
11230           }
11231
11232         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11233       }
11234       break;
11235
11236     case CONST_STRING:
11237       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11238       break;
11239
11240     case SYMBOL_REF:
11241     case LABEL_REF:
11242     case CONST:
11243       add_AT_addr (die, DW_AT_const_value, rtl);
11244       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11245       break;
11246
11247     case PLUS:
11248       /* In cases where an inlined instance of an inline function is passed
11249          the address of an `auto' variable (which is local to the caller) we
11250          can get a situation where the DECL_RTL of the artificial local
11251          variable (for the inlining) which acts as a stand-in for the
11252          corresponding formal parameter (of the inline function) will look
11253          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11254          exactly a compile-time constant expression, but it isn't the address
11255          of the (artificial) local variable either.  Rather, it represents the
11256          *value* which the artificial local variable always has during its
11257          lifetime.  We currently have no way to represent such quasi-constant
11258          values in Dwarf, so for now we just punt and generate nothing.  */
11259       break;
11260
11261     default:
11262       /* No other kinds of rtx should be possible here.  */
11263       gcc_unreachable ();
11264     }
11265
11266 }
11267
11268 /* Determine whether the evaluation of EXPR references any variables
11269    or functions which aren't otherwise used (and therefore may not be
11270    output).  */
11271 static tree
11272 reference_to_unused (tree * tp, int * walk_subtrees,
11273                      void * data ATTRIBUTE_UNUSED)
11274 {
11275   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11276     *walk_subtrees = 0;
11277
11278   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11279       && ! TREE_ASM_WRITTEN (*tp))
11280     return *tp;
11281   /* ???  The C++ FE emits debug information for using decls, so
11282      putting gcc_unreachable here falls over.  See PR31899.  For now
11283      be conservative.  */
11284   else if (!cgraph_global_info_ready
11285            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11286     return *tp;
11287   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11288     {
11289       struct varpool_node *node = varpool_node (*tp);
11290       if (!node->needed)
11291         return *tp;
11292     }
11293   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11294            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11295     {
11296       struct cgraph_node *node = cgraph_node (*tp);
11297       if (!node->output)
11298         return *tp;
11299     }
11300   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11301     return *tp;
11302
11303   return NULL_TREE;
11304 }
11305
11306 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11307    for use in a later add_const_value_attribute call.  */
11308
11309 static rtx
11310 rtl_for_decl_init (tree init, tree type)
11311 {
11312   rtx rtl = NULL_RTX;
11313
11314   /* If a variable is initialized with a string constant without embedded
11315      zeros, build CONST_STRING.  */
11316   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11317     {
11318       tree enttype = TREE_TYPE (type);
11319       tree domain = TYPE_DOMAIN (type);
11320       enum machine_mode mode = TYPE_MODE (enttype);
11321
11322       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11323           && domain
11324           && integer_zerop (TYPE_MIN_VALUE (domain))
11325           && compare_tree_int (TYPE_MAX_VALUE (domain),
11326                                TREE_STRING_LENGTH (init) - 1) == 0
11327           && ((size_t) TREE_STRING_LENGTH (init)
11328               == strlen (TREE_STRING_POINTER (init)) + 1))
11329         rtl = gen_rtx_CONST_STRING (VOIDmode,
11330                                     ggc_strdup (TREE_STRING_POINTER (init)));
11331     }
11332   /* Other aggregates, and complex values, could be represented using
11333      CONCAT: FIXME!  */
11334   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11335     ;
11336   /* Vectors only work if their mode is supported by the target.
11337      FIXME: generic vectors ought to work too.  */
11338   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11339     ;
11340   /* If the initializer is something that we know will expand into an
11341      immediate RTL constant, expand it now.  We must be careful not to
11342      reference variables which won't be output.  */
11343   else if (initializer_constant_valid_p (init, type)
11344            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11345     {
11346       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11347          possible.  */
11348       if (TREE_CODE (type) == VECTOR_TYPE)
11349         switch (TREE_CODE (init))
11350           {
11351           case VECTOR_CST:
11352             break;
11353           case CONSTRUCTOR:
11354             if (TREE_CONSTANT (init))
11355               {
11356                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11357                 bool constant_p = true;
11358                 tree value;
11359                 unsigned HOST_WIDE_INT ix;
11360
11361                 /* Even when ctor is constant, it might contain non-*_CST
11362                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11363                    belong into VECTOR_CST nodes.  */
11364                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11365                   if (!CONSTANT_CLASS_P (value))
11366                     {
11367                       constant_p = false;
11368                       break;
11369                     }
11370
11371                 if (constant_p)
11372                   {
11373                     init = build_vector_from_ctor (type, elts);
11374                     break;
11375                   }
11376               }
11377             /* FALLTHRU */
11378
11379           default:
11380             return NULL;
11381           }
11382
11383       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11384
11385       /* If expand_expr returns a MEM, it wasn't immediate.  */
11386       gcc_assert (!rtl || !MEM_P (rtl));
11387     }
11388
11389   return rtl;
11390 }
11391
11392 /* Generate RTL for the variable DECL to represent its location.  */
11393
11394 static rtx
11395 rtl_for_decl_location (tree decl)
11396 {
11397   rtx rtl;
11398
11399   /* Here we have to decide where we are going to say the parameter "lives"
11400      (as far as the debugger is concerned).  We only have a couple of
11401      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11402
11403      DECL_RTL normally indicates where the parameter lives during most of the
11404      activation of the function.  If optimization is enabled however, this
11405      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11406      that the parameter doesn't really live anywhere (as far as the code
11407      generation parts of GCC are concerned) during most of the function's
11408      activation.  That will happen (for example) if the parameter is never
11409      referenced within the function.
11410
11411      We could just generate a location descriptor here for all non-NULL
11412      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11413      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11414      where DECL_RTL is NULL or is a pseudo-reg.
11415
11416      Note however that we can only get away with using DECL_INCOMING_RTL as
11417      a backup substitute for DECL_RTL in certain limited cases.  In cases
11418      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11419      we can be sure that the parameter was passed using the same type as it is
11420      declared to have within the function, and that its DECL_INCOMING_RTL
11421      points us to a place where a value of that type is passed.
11422
11423      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11424      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11425      because in these cases DECL_INCOMING_RTL points us to a value of some
11426      type which is *different* from the type of the parameter itself.  Thus,
11427      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11428      such cases, the debugger would end up (for example) trying to fetch a
11429      `float' from a place which actually contains the first part of a
11430      `double'.  That would lead to really incorrect and confusing
11431      output at debug-time.
11432
11433      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11434      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11435      are a couple of exceptions however.  On little-endian machines we can
11436      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11437      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11438      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11439      when (on a little-endian machine) a non-prototyped function has a
11440      parameter declared to be of type `short' or `char'.  In such cases,
11441      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11442      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11443      passed `int' value.  If the debugger then uses that address to fetch
11444      a `short' or a `char' (on a little-endian machine) the result will be
11445      the correct data, so we allow for such exceptional cases below.
11446
11447      Note that our goal here is to describe the place where the given formal
11448      parameter lives during most of the function's activation (i.e. between the
11449      end of the prologue and the start of the epilogue).  We'll do that as best
11450      as we can. Note however that if the given formal parameter is modified
11451      sometime during the execution of the function, then a stack backtrace (at
11452      debug-time) will show the function as having been called with the *new*
11453      value rather than the value which was originally passed in.  This happens
11454      rarely enough that it is not a major problem, but it *is* a problem, and
11455      I'd like to fix it.
11456
11457      A future version of dwarf2out.c may generate two additional attributes for
11458      any given DW_TAG_formal_parameter DIE which will describe the "passed
11459      type" and the "passed location" for the given formal parameter in addition
11460      to the attributes we now generate to indicate the "declared type" and the
11461      "active location" for each parameter.  This additional set of attributes
11462      could be used by debuggers for stack backtraces. Separately, note that
11463      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11464      This happens (for example) for inlined-instances of inline function formal
11465      parameters which are never referenced.  This really shouldn't be
11466      happening.  All PARM_DECL nodes should get valid non-NULL
11467      DECL_INCOMING_RTL values.  FIXME.  */
11468
11469   /* Use DECL_RTL as the "location" unless we find something better.  */
11470   rtl = DECL_RTL_IF_SET (decl);
11471
11472   /* When generating abstract instances, ignore everything except
11473      constants, symbols living in memory, and symbols living in
11474      fixed registers.  */
11475   if (! reload_completed)
11476     {
11477       if (rtl
11478           && (CONSTANT_P (rtl)
11479               || (MEM_P (rtl)
11480                   && CONSTANT_P (XEXP (rtl, 0)))
11481               || (REG_P (rtl)
11482                   && TREE_CODE (decl) == VAR_DECL
11483                   && TREE_STATIC (decl))))
11484         {
11485           rtl = targetm.delegitimize_address (rtl);
11486           return rtl;
11487         }
11488       rtl = NULL_RTX;
11489     }
11490   else if (TREE_CODE (decl) == PARM_DECL)
11491     {
11492       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11493         {
11494           tree declared_type = TREE_TYPE (decl);
11495           tree passed_type = DECL_ARG_TYPE (decl);
11496           enum machine_mode dmode = TYPE_MODE (declared_type);
11497           enum machine_mode pmode = TYPE_MODE (passed_type);
11498
11499           /* This decl represents a formal parameter which was optimized out.
11500              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11501              all cases where (rtl == NULL_RTX) just below.  */
11502           if (dmode == pmode)
11503             rtl = DECL_INCOMING_RTL (decl);
11504           else if (SCALAR_INT_MODE_P (dmode)
11505                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11506                    && DECL_INCOMING_RTL (decl))
11507             {
11508               rtx inc = DECL_INCOMING_RTL (decl);
11509               if (REG_P (inc))
11510                 rtl = inc;
11511               else if (MEM_P (inc))
11512                 {
11513                   if (BYTES_BIG_ENDIAN)
11514                     rtl = adjust_address_nv (inc, dmode,
11515                                              GET_MODE_SIZE (pmode)
11516                                              - GET_MODE_SIZE (dmode));
11517                   else
11518                     rtl = inc;
11519                 }
11520             }
11521         }
11522
11523       /* If the parm was passed in registers, but lives on the stack, then
11524          make a big endian correction if the mode of the type of the
11525          parameter is not the same as the mode of the rtl.  */
11526       /* ??? This is the same series of checks that are made in dbxout.c before
11527          we reach the big endian correction code there.  It isn't clear if all
11528          of these checks are necessary here, but keeping them all is the safe
11529          thing to do.  */
11530       else if (MEM_P (rtl)
11531                && XEXP (rtl, 0) != const0_rtx
11532                && ! CONSTANT_P (XEXP (rtl, 0))
11533                /* Not passed in memory.  */
11534                && !MEM_P (DECL_INCOMING_RTL (decl))
11535                /* Not passed by invisible reference.  */
11536                && (!REG_P (XEXP (rtl, 0))
11537                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11538                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11539 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11540                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11541 #endif
11542                      )
11543                /* Big endian correction check.  */
11544                && BYTES_BIG_ENDIAN
11545                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11546                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11547                    < UNITS_PER_WORD))
11548         {
11549           int offset = (UNITS_PER_WORD
11550                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11551
11552           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11553                              plus_constant (XEXP (rtl, 0), offset));
11554         }
11555     }
11556   else if (TREE_CODE (decl) == VAR_DECL
11557            && rtl
11558            && MEM_P (rtl)
11559            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11560            && BYTES_BIG_ENDIAN)
11561     {
11562       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11563       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11564
11565       /* If a variable is declared "register" yet is smaller than
11566          a register, then if we store the variable to memory, it
11567          looks like we're storing a register-sized value, when in
11568          fact we are not.  We need to adjust the offset of the
11569          storage location to reflect the actual value's bytes,
11570          else gdb will not be able to display it.  */
11571       if (rsize > dsize)
11572         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11573                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11574     }
11575
11576   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11577      and will have been substituted directly into all expressions that use it.
11578      C does not have such a concept, but C++ and other languages do.  */
11579   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11580     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11581
11582   if (rtl)
11583     rtl = targetm.delegitimize_address (rtl);
11584
11585   /* If we don't look past the constant pool, we risk emitting a
11586      reference to a constant pool entry that isn't referenced from
11587      code, and thus is not emitted.  */
11588   if (rtl)
11589     rtl = avoid_constant_pool_reference (rtl);
11590
11591   return rtl;
11592 }
11593
11594 /* We need to figure out what section we should use as the base for the
11595    address ranges where a given location is valid.
11596    1. If this particular DECL has a section associated with it, use that.
11597    2. If this function has a section associated with it, use that.
11598    3. Otherwise, use the text section.
11599    XXX: If you split a variable across multiple sections, we won't notice.  */
11600
11601 static const char *
11602 secname_for_decl (const_tree decl)
11603 {
11604   const char *secname;
11605
11606   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11607     {
11608       tree sectree = DECL_SECTION_NAME (decl);
11609       secname = TREE_STRING_POINTER (sectree);
11610     }
11611   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11612     {
11613       tree sectree = DECL_SECTION_NAME (current_function_decl);
11614       secname = TREE_STRING_POINTER (sectree);
11615     }
11616   else if (cfun && in_cold_section_p)
11617     secname = crtl->subsections.cold_section_label;
11618   else
11619     secname = text_section_label;
11620
11621   return secname;
11622 }
11623
11624 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11625    returned.  If so, the decl for the COMMON block is returned, and the
11626    value is the offset into the common block for the symbol.  */
11627
11628 static tree
11629 fortran_common (tree decl, HOST_WIDE_INT *value)
11630 {
11631   tree val_expr, cvar;
11632   enum machine_mode mode;
11633   HOST_WIDE_INT bitsize, bitpos;
11634   tree offset;
11635   int volatilep = 0, unsignedp = 0;
11636
11637   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11638      it does not have a value (the offset into the common area), or if it
11639      is thread local (as opposed to global) then it isn't common, and shouldn't
11640      be handled as such.  */
11641   if (TREE_CODE (decl) != VAR_DECL
11642       || !TREE_PUBLIC (decl)
11643       || !TREE_STATIC (decl)
11644       || !DECL_HAS_VALUE_EXPR_P (decl)
11645       || !is_fortran ())
11646     return NULL_TREE;
11647
11648   val_expr = DECL_VALUE_EXPR (decl);
11649   if (TREE_CODE (val_expr) != COMPONENT_REF)
11650     return NULL_TREE;
11651
11652   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11653                               &mode, &unsignedp, &volatilep, true);
11654
11655   if (cvar == NULL_TREE
11656       || TREE_CODE (cvar) != VAR_DECL
11657       || DECL_ARTIFICIAL (cvar)
11658       || !TREE_PUBLIC (cvar))
11659     return NULL_TREE;
11660
11661   *value = 0;
11662   if (offset != NULL)
11663     {
11664       if (!host_integerp (offset, 0))
11665         return NULL_TREE;
11666       *value = tree_low_cst (offset, 0);
11667     }
11668   if (bitpos != 0)
11669     *value += bitpos / BITS_PER_UNIT;
11670
11671   return cvar;
11672 }
11673
11674 /* Dereference a location expression LOC if DECL is passed by invisible
11675    reference.  */
11676
11677 static dw_loc_descr_ref
11678 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11679 {
11680   HOST_WIDE_INT size;
11681   enum dwarf_location_atom op;
11682
11683   if (loc == NULL)
11684     return NULL;
11685
11686   if ((TREE_CODE (decl) != PARM_DECL && TREE_CODE (decl) != RESULT_DECL)
11687       || !DECL_BY_REFERENCE (decl))
11688     return loc;
11689
11690   size = int_size_in_bytes (TREE_TYPE (decl));
11691   if (size > DWARF2_ADDR_SIZE || size == -1)
11692     return 0;
11693   else if (size == DWARF2_ADDR_SIZE)
11694     op = DW_OP_deref;
11695   else
11696     op = DW_OP_deref_size;
11697   add_loc_descr (&loc, new_loc_descr (op, size, 0));
11698   return loc;
11699 }
11700
11701 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
11702    data attribute for a variable or a parameter.  We generate the
11703    DW_AT_const_value attribute only in those cases where the given variable
11704    or parameter does not have a true "location" either in memory or in a
11705    register.  This can happen (for example) when a constant is passed as an
11706    actual argument in a call to an inline function.  (It's possible that
11707    these things can crop up in other ways also.)  Note that one type of
11708    constant value which can be passed into an inlined function is a constant
11709    pointer.  This can happen for example if an actual argument in an inlined
11710    function call evaluates to a compile-time constant address.  */
11711
11712 static void
11713 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
11714                                        enum dwarf_attribute attr)
11715 {
11716   rtx rtl;
11717   dw_loc_descr_ref descr;
11718   var_loc_list *loc_list;
11719   struct var_loc_node *node;
11720   if (TREE_CODE (decl) == ERROR_MARK)
11721     return;
11722
11723   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
11724               || TREE_CODE (decl) == RESULT_DECL);
11725
11726   /* See if we possibly have multiple locations for this variable.  */
11727   loc_list = lookup_decl_loc (decl);
11728
11729   /* If it truly has multiple locations, the first and last node will
11730      differ.  */
11731   if (loc_list && loc_list->first != loc_list->last)
11732     {
11733       const char *endname, *secname;
11734       dw_loc_list_ref list;
11735       rtx varloc;
11736       enum var_init_status initialized;
11737
11738       /* Now that we know what section we are using for a base,
11739          actually construct the list of locations.
11740          The first location information is what is passed to the
11741          function that creates the location list, and the remaining
11742          locations just get added on to that list.
11743          Note that we only know the start address for a location
11744          (IE location changes), so to build the range, we use
11745          the range [current location start, next location start].
11746          This means we have to special case the last node, and generate
11747          a range of [last location start, end of function label].  */
11748
11749       node = loc_list->first;
11750       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11751       secname = secname_for_decl (decl);
11752
11753       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
11754         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11755       else
11756         initialized = VAR_INIT_STATUS_INITIALIZED;
11757
11758       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
11759       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
11760       node = node->next;
11761
11762       for (; node->next; node = node->next)
11763         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11764           {
11765             /* The variable has a location between NODE->LABEL and
11766                NODE->NEXT->LABEL.  */
11767             enum var_init_status initialized =
11768               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11769             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11770             descr = loc_by_reference (loc_descriptor (varloc, initialized),
11771                                       decl);
11772             add_loc_descr_to_loc_list (&list, descr,
11773                                        node->label, node->next->label, secname);
11774           }
11775
11776       /* If the variable has a location at the last label
11777          it keeps its location until the end of function.  */
11778       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
11779         {
11780           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11781           enum var_init_status initialized =
11782             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11783
11784           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
11785           if (!current_function_decl)
11786             endname = text_end_label;
11787           else
11788             {
11789               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11790                                            current_function_funcdef_no);
11791               endname = ggc_strdup (label_id);
11792             }
11793           descr = loc_by_reference (loc_descriptor (varloc, initialized),
11794                                     decl);
11795           add_loc_descr_to_loc_list (&list, descr,
11796                                      node->label, endname, secname);
11797         }
11798
11799       /* Finally, add the location list to the DIE, and we are done.  */
11800       add_AT_loc_list (die, attr, list);
11801       return;
11802     }
11803
11804   /* Try to get some constant RTL for this decl, and use that as the value of
11805      the location.  */
11806
11807   rtl = rtl_for_decl_location (decl);
11808   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
11809     {
11810       add_const_value_attribute (die, rtl);
11811       return;
11812     }
11813
11814   /* If we have tried to generate the location otherwise, and it
11815      didn't work out (we wouldn't be here if we did), and we have a one entry
11816      location list, try generating a location from that.  */
11817   if (loc_list && loc_list->first)
11818     {
11819       enum var_init_status status;
11820       node = loc_list->first;
11821       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
11822       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
11823       if (descr)
11824         {
11825           descr = loc_by_reference (descr, decl);
11826           add_AT_location_description (die, attr, descr);
11827           return;
11828         }
11829     }
11830
11831   /* We couldn't get any rtl, so try directly generating the location
11832      description from the tree.  */
11833   descr = loc_descriptor_from_tree (decl);
11834   if (descr)
11835     {
11836       descr = loc_by_reference (descr, decl);
11837       add_AT_location_description (die, attr, descr);
11838       return;
11839     }
11840   /* None of that worked, so it must not really have a location;
11841      try adding a constant value attribute from the DECL_INITIAL.  */
11842   tree_add_const_value_attribute (die, decl);
11843 }
11844
11845 /* Helper function for tree_add_const_value_attribute.  Natively encode
11846    initializer INIT into an array.  Return true if successful.  */
11847
11848 static bool
11849 native_encode_initializer (tree init, unsigned char *array, int size)
11850 {
11851   tree type;
11852
11853   if (init == NULL_TREE)
11854     return false;
11855
11856   STRIP_NOPS (init);
11857   switch (TREE_CODE (init))
11858     {
11859     case STRING_CST:
11860       type = TREE_TYPE (init);
11861       if (TREE_CODE (type) == ARRAY_TYPE)
11862         {
11863           tree enttype = TREE_TYPE (type);
11864           enum machine_mode mode = TYPE_MODE (enttype);
11865
11866           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
11867             return false;
11868           if (int_size_in_bytes (type) != size)
11869             return false;
11870           if (size > TREE_STRING_LENGTH (init))
11871             {
11872               memcpy (array, TREE_STRING_POINTER (init),
11873                       TREE_STRING_LENGTH (init));
11874               memset (array + TREE_STRING_LENGTH (init),
11875                       '\0', size - TREE_STRING_LENGTH (init));
11876             }
11877           else
11878             memcpy (array, TREE_STRING_POINTER (init), size);
11879           return true;
11880         }
11881       return false;
11882     case CONSTRUCTOR:
11883       type = TREE_TYPE (init);
11884       if (int_size_in_bytes (type) != size)
11885         return false;
11886       if (TREE_CODE (type) == ARRAY_TYPE)
11887         {
11888           HOST_WIDE_INT min_index;
11889           unsigned HOST_WIDE_INT cnt;
11890           int curpos = 0, fieldsize;
11891           constructor_elt *ce;
11892
11893           if (TYPE_DOMAIN (type) == NULL_TREE
11894               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
11895             return false;
11896
11897           fieldsize = int_size_in_bytes (TREE_TYPE (type));
11898           if (fieldsize <= 0)
11899             return false;
11900
11901           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
11902           memset (array, '\0', size);
11903           for (cnt = 0;
11904                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11905                cnt++)
11906             {
11907               tree val = ce->value;
11908               tree index = ce->index;
11909               int pos = curpos;
11910               if (index && TREE_CODE (index) == RANGE_EXPR)
11911                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
11912                       * fieldsize;
11913               else if (index)
11914                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
11915
11916               if (val)
11917                 {
11918                   STRIP_NOPS (val);
11919                   if (!native_encode_initializer (val, array + pos, fieldsize))
11920                     return false;
11921                 }
11922               curpos = pos + fieldsize;
11923               if (index && TREE_CODE (index) == RANGE_EXPR)
11924                 {
11925                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
11926                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
11927                   while (count > 0)
11928                     {
11929                       if (val)
11930                         memcpy (array + curpos, array + pos, fieldsize);
11931                       curpos += fieldsize;
11932                     }
11933                 }
11934               gcc_assert (curpos <= size);
11935             }
11936           return true;
11937         }
11938       else if (TREE_CODE (type) == RECORD_TYPE
11939                || TREE_CODE (type) == UNION_TYPE)
11940         {
11941           tree field = NULL_TREE;
11942           unsigned HOST_WIDE_INT cnt;
11943           constructor_elt *ce;
11944
11945           if (int_size_in_bytes (type) != size)
11946             return false;
11947
11948           if (TREE_CODE (type) == RECORD_TYPE)
11949             field = TYPE_FIELDS (type);
11950
11951           for (cnt = 0;
11952                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
11953                cnt++, field = field ? TREE_CHAIN (field) : 0)
11954             {
11955               tree val = ce->value;
11956               int pos, fieldsize;
11957
11958               if (ce->index != 0)
11959                 field = ce->index;
11960
11961               if (val)
11962                 STRIP_NOPS (val);
11963
11964               if (field == NULL_TREE || DECL_BIT_FIELD (field))
11965                 return false;
11966
11967               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
11968                   && TYPE_DOMAIN (TREE_TYPE (field))
11969                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
11970                 return false;
11971               else if (DECL_SIZE_UNIT (field) == NULL_TREE
11972                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
11973                 return false;
11974               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
11975               pos = int_byte_position (field);
11976               gcc_assert (pos + fieldsize <= size);
11977               if (val
11978                   && !native_encode_initializer (val, array + pos, fieldsize))
11979                 return false;
11980             }
11981           return true;
11982         }
11983       return false;
11984     case VIEW_CONVERT_EXPR:
11985     case NON_LVALUE_EXPR:
11986       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
11987     default:
11988       return native_encode_expr (init, array, size) == size;
11989     }
11990 }
11991
11992 /* If we don't have a copy of this variable in memory for some reason (such
11993    as a C++ member constant that doesn't have an out-of-line definition),
11994    we should tell the debugger about the constant value.  */
11995
11996 static void
11997 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
11998 {
11999   tree init;
12000   tree type = TREE_TYPE (decl);
12001   rtx rtl;
12002
12003   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12004     return;
12005
12006   init = DECL_INITIAL (decl);
12007   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12008     /* OK */;
12009   else
12010     return;
12011
12012   rtl = rtl_for_decl_init (init, type);
12013   if (rtl)
12014     add_const_value_attribute (var_die, rtl);
12015   /* If the host and target are sane, try harder.  */
12016   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12017            && initializer_constant_valid_p (init, type))
12018     {
12019       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12020       if (size > 0 && (int) size == size)
12021         {
12022           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12023
12024           if (native_encode_initializer (init, array, size))
12025             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12026         }
12027     }
12028 }
12029
12030 /* Convert the CFI instructions for the current function into a
12031    location list.  This is used for DW_AT_frame_base when we targeting
12032    a dwarf2 consumer that does not support the dwarf3
12033    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12034    expressions.  */
12035
12036 static dw_loc_list_ref
12037 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12038 {
12039   dw_fde_ref fde;
12040   dw_loc_list_ref list, *list_tail;
12041   dw_cfi_ref cfi;
12042   dw_cfa_location last_cfa, next_cfa;
12043   const char *start_label, *last_label, *section;
12044
12045   fde = current_fde ();
12046   gcc_assert (fde != NULL);
12047
12048   section = secname_for_decl (current_function_decl);
12049   list_tail = &list;
12050   list = NULL;
12051
12052   next_cfa.reg = INVALID_REGNUM;
12053   next_cfa.offset = 0;
12054   next_cfa.indirect = 0;
12055   next_cfa.base_offset = 0;
12056
12057   start_label = fde->dw_fde_begin;
12058
12059   /* ??? Bald assumption that the CIE opcode list does not contain
12060      advance opcodes.  */
12061   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12062     lookup_cfa_1 (cfi, &next_cfa);
12063
12064   last_cfa = next_cfa;
12065   last_label = start_label;
12066
12067   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12068     switch (cfi->dw_cfi_opc)
12069       {
12070       case DW_CFA_set_loc:
12071       case DW_CFA_advance_loc1:
12072       case DW_CFA_advance_loc2:
12073       case DW_CFA_advance_loc4:
12074         if (!cfa_equal_p (&last_cfa, &next_cfa))
12075           {
12076             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12077                                        start_label, last_label, section,
12078                                        list == NULL);
12079
12080             list_tail = &(*list_tail)->dw_loc_next;
12081             last_cfa = next_cfa;
12082             start_label = last_label;
12083           }
12084         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12085         break;
12086
12087       case DW_CFA_advance_loc:
12088         /* The encoding is complex enough that we should never emit this.  */
12089       case DW_CFA_remember_state:
12090       case DW_CFA_restore_state:
12091         /* We don't handle these two in this function.  It would be possible
12092            if it were to be required.  */
12093         gcc_unreachable ();
12094
12095       default:
12096         lookup_cfa_1 (cfi, &next_cfa);
12097         break;
12098       }
12099
12100   if (!cfa_equal_p (&last_cfa, &next_cfa))
12101     {
12102       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12103                                  start_label, last_label, section,
12104                                  list == NULL);
12105       list_tail = &(*list_tail)->dw_loc_next;
12106       start_label = last_label;
12107     }
12108   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12109                              start_label, fde->dw_fde_end, section,
12110                              list == NULL);
12111
12112   return list;
12113 }
12114
12115 /* Compute a displacement from the "steady-state frame pointer" to the
12116    frame base (often the same as the CFA), and store it in
12117    frame_pointer_fb_offset.  OFFSET is added to the displacement
12118    before the latter is negated.  */
12119
12120 static void
12121 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12122 {
12123   rtx reg, elim;
12124
12125 #ifdef FRAME_POINTER_CFA_OFFSET
12126   reg = frame_pointer_rtx;
12127   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12128 #else
12129   reg = arg_pointer_rtx;
12130   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12131 #endif
12132
12133   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12134   if (GET_CODE (elim) == PLUS)
12135     {
12136       offset += INTVAL (XEXP (elim, 1));
12137       elim = XEXP (elim, 0);
12138     }
12139
12140   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12141                && (elim == hard_frame_pointer_rtx
12142                    || elim == stack_pointer_rtx))
12143               || elim == (frame_pointer_needed
12144                           ? hard_frame_pointer_rtx
12145                           : stack_pointer_rtx));
12146
12147   frame_pointer_fb_offset = -offset;
12148 }
12149
12150 /* Generate a DW_AT_name attribute given some string value to be included as
12151    the value of the attribute.  */
12152
12153 static void
12154 add_name_attribute (dw_die_ref die, const char *name_string)
12155 {
12156   if (name_string != NULL && *name_string != 0)
12157     {
12158       if (demangle_name_func)
12159         name_string = (*demangle_name_func) (name_string);
12160
12161       add_AT_string (die, DW_AT_name, name_string);
12162     }
12163 }
12164
12165 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12166
12167 static void
12168 add_comp_dir_attribute (dw_die_ref die)
12169 {
12170   const char *wd = get_src_pwd ();
12171   if (wd != NULL)
12172     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12173 }
12174
12175 /* Given a tree node describing an array bound (either lower or upper) output
12176    a representation for that bound.  */
12177
12178 static void
12179 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12180 {
12181   switch (TREE_CODE (bound))
12182     {
12183     case ERROR_MARK:
12184       return;
12185
12186     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12187     case INTEGER_CST:
12188       if (! host_integerp (bound, 0)
12189           || (bound_attr == DW_AT_lower_bound
12190               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12191                   || (is_fortran () && integer_onep (bound)))))
12192         /* Use the default.  */
12193         ;
12194       else
12195         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12196       break;
12197
12198     CASE_CONVERT:
12199     case VIEW_CONVERT_EXPR:
12200       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12201       break;
12202
12203     case SAVE_EXPR:
12204       break;
12205
12206     case VAR_DECL:
12207     case PARM_DECL:
12208     case RESULT_DECL:
12209       {
12210         dw_die_ref decl_die = lookup_decl_die (bound);
12211         dw_loc_descr_ref loc;
12212
12213         /* ??? Can this happen, or should the variable have been bound
12214            first?  Probably it can, since I imagine that we try to create
12215            the types of parameters in the order in which they exist in
12216            the list, and won't have created a forward reference to a
12217            later parameter.  */
12218         if (decl_die != NULL)
12219           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12220         else
12221           {
12222             loc = loc_descriptor_from_tree_1 (bound, 0);
12223             add_AT_location_description (subrange_die, bound_attr, loc);
12224           }
12225         break;
12226       }
12227
12228     default:
12229       {
12230         /* Otherwise try to create a stack operation procedure to
12231            evaluate the value of the array bound.  */
12232
12233         dw_die_ref ctx, decl_die;
12234         dw_loc_descr_ref loc;
12235
12236         loc = loc_descriptor_from_tree (bound);
12237         if (loc == NULL)
12238           break;
12239
12240         if (current_function_decl == 0)
12241           ctx = comp_unit_die;
12242         else
12243           ctx = lookup_decl_die (current_function_decl);
12244
12245         decl_die = new_die (DW_TAG_variable, ctx, bound);
12246         add_AT_flag (decl_die, DW_AT_artificial, 1);
12247         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12248         add_AT_loc (decl_die, DW_AT_location, loc);
12249
12250         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12251         break;
12252       }
12253     }
12254 }
12255
12256 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12257    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12258    Note that the block of subscript information for an array type also
12259    includes information about the element type of the given array type.  */
12260
12261 static void
12262 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12263 {
12264   unsigned dimension_number;
12265   tree lower, upper;
12266   dw_die_ref subrange_die;
12267
12268   for (dimension_number = 0;
12269        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12270        type = TREE_TYPE (type), dimension_number++)
12271     {
12272       tree domain = TYPE_DOMAIN (type);
12273
12274       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12275         break;
12276
12277       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12278          and (in GNU C only) variable bounds.  Handle all three forms
12279          here.  */
12280       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12281       if (domain)
12282         {
12283           /* We have an array type with specified bounds.  */
12284           lower = TYPE_MIN_VALUE (domain);
12285           upper = TYPE_MAX_VALUE (domain);
12286
12287           /* Define the index type.  */
12288           if (TREE_TYPE (domain))
12289             {
12290               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12291                  TREE_TYPE field.  We can't emit debug info for this
12292                  because it is an unnamed integral type.  */
12293               if (TREE_CODE (domain) == INTEGER_TYPE
12294                   && TYPE_NAME (domain) == NULL_TREE
12295                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12296                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12297                 ;
12298               else
12299                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12300                                     type_die);
12301             }
12302
12303           /* ??? If upper is NULL, the array has unspecified length,
12304              but it does have a lower bound.  This happens with Fortran
12305                dimension arr(N:*)
12306              Since the debugger is definitely going to need to know N
12307              to produce useful results, go ahead and output the lower
12308              bound solo, and hope the debugger can cope.  */
12309
12310           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12311           if (upper)
12312             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12313         }
12314
12315       /* Otherwise we have an array type with an unspecified length.  The
12316          DWARF-2 spec does not say how to handle this; let's just leave out the
12317          bounds.  */
12318     }
12319 }
12320
12321 static void
12322 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12323 {
12324   unsigned size;
12325
12326   switch (TREE_CODE (tree_node))
12327     {
12328     case ERROR_MARK:
12329       size = 0;
12330       break;
12331     case ENUMERAL_TYPE:
12332     case RECORD_TYPE:
12333     case UNION_TYPE:
12334     case QUAL_UNION_TYPE:
12335       size = int_size_in_bytes (tree_node);
12336       break;
12337     case FIELD_DECL:
12338       /* For a data member of a struct or union, the DW_AT_byte_size is
12339          generally given as the number of bytes normally allocated for an
12340          object of the *declared* type of the member itself.  This is true
12341          even for bit-fields.  */
12342       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12343       break;
12344     default:
12345       gcc_unreachable ();
12346     }
12347
12348   /* Note that `size' might be -1 when we get to this point.  If it is, that
12349      indicates that the byte size of the entity in question is variable.  We
12350      have no good way of expressing this fact in Dwarf at the present time,
12351      so just let the -1 pass on through.  */
12352   add_AT_unsigned (die, DW_AT_byte_size, size);
12353 }
12354
12355 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12356    which specifies the distance in bits from the highest order bit of the
12357    "containing object" for the bit-field to the highest order bit of the
12358    bit-field itself.
12359
12360    For any given bit-field, the "containing object" is a hypothetical object
12361    (of some integral or enum type) within which the given bit-field lives.  The
12362    type of this hypothetical "containing object" is always the same as the
12363    declared type of the individual bit-field itself.  The determination of the
12364    exact location of the "containing object" for a bit-field is rather
12365    complicated.  It's handled by the `field_byte_offset' function (above).
12366
12367    Note that it is the size (in bytes) of the hypothetical "containing object"
12368    which will be given in the DW_AT_byte_size attribute for this bit-field.
12369    (See `byte_size_attribute' above).  */
12370
12371 static inline void
12372 add_bit_offset_attribute (dw_die_ref die, tree decl)
12373 {
12374   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12375   tree type = DECL_BIT_FIELD_TYPE (decl);
12376   HOST_WIDE_INT bitpos_int;
12377   HOST_WIDE_INT highest_order_object_bit_offset;
12378   HOST_WIDE_INT highest_order_field_bit_offset;
12379   HOST_WIDE_INT unsigned bit_offset;
12380
12381   /* Must be a field and a bit field.  */
12382   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12383
12384   /* We can't yet handle bit-fields whose offsets are variable, so if we
12385      encounter such things, just return without generating any attribute
12386      whatsoever.  Likewise for variable or too large size.  */
12387   if (! host_integerp (bit_position (decl), 0)
12388       || ! host_integerp (DECL_SIZE (decl), 1))
12389     return;
12390
12391   bitpos_int = int_bit_position (decl);
12392
12393   /* Note that the bit offset is always the distance (in bits) from the
12394      highest-order bit of the "containing object" to the highest-order bit of
12395      the bit-field itself.  Since the "high-order end" of any object or field
12396      is different on big-endian and little-endian machines, the computation
12397      below must take account of these differences.  */
12398   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12399   highest_order_field_bit_offset = bitpos_int;
12400
12401   if (! BYTES_BIG_ENDIAN)
12402     {
12403       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12404       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12405     }
12406
12407   bit_offset
12408     = (! BYTES_BIG_ENDIAN
12409        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12410        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12411
12412   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12413 }
12414
12415 /* For a FIELD_DECL node which represents a bit field, output an attribute
12416    which specifies the length in bits of the given field.  */
12417
12418 static inline void
12419 add_bit_size_attribute (dw_die_ref die, tree decl)
12420 {
12421   /* Must be a field and a bit field.  */
12422   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12423               && DECL_BIT_FIELD_TYPE (decl));
12424
12425   if (host_integerp (DECL_SIZE (decl), 1))
12426     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12427 }
12428
12429 /* If the compiled language is ANSI C, then add a 'prototyped'
12430    attribute, if arg types are given for the parameters of a function.  */
12431
12432 static inline void
12433 add_prototyped_attribute (dw_die_ref die, tree func_type)
12434 {
12435   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12436       && TYPE_ARG_TYPES (func_type) != NULL)
12437     add_AT_flag (die, DW_AT_prototyped, 1);
12438 }
12439
12440 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12441    by looking in either the type declaration or object declaration
12442    equate table.  */
12443
12444 static inline void
12445 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12446 {
12447   dw_die_ref origin_die = NULL;
12448
12449   if (TREE_CODE (origin) != FUNCTION_DECL)
12450     {
12451       /* We may have gotten separated from the block for the inlined
12452          function, if we're in an exception handler or some such; make
12453          sure that the abstract function has been written out.
12454
12455          Doing this for nested functions is wrong, however; functions are
12456          distinct units, and our context might not even be inline.  */
12457       tree fn = origin;
12458
12459       if (TYPE_P (fn))
12460         fn = TYPE_STUB_DECL (fn);
12461
12462       fn = decl_function_context (fn);
12463       if (fn)
12464         dwarf2out_abstract_function (fn);
12465     }
12466
12467   if (DECL_P (origin))
12468     origin_die = lookup_decl_die (origin);
12469   else if (TYPE_P (origin))
12470     origin_die = lookup_type_die (origin);
12471
12472   /* XXX: Functions that are never lowered don't always have correct block
12473      trees (in the case of java, they simply have no block tree, in some other
12474      languages).  For these functions, there is nothing we can really do to
12475      output correct debug info for inlined functions in all cases.  Rather
12476      than die, we'll just produce deficient debug info now, in that we will
12477      have variables without a proper abstract origin.  In the future, when all
12478      functions are lowered, we should re-add a gcc_assert (origin_die)
12479      here.  */
12480
12481   if (origin_die)
12482       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12483 }
12484
12485 /* We do not currently support the pure_virtual attribute.  */
12486
12487 static inline void
12488 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12489 {
12490   if (DECL_VINDEX (func_decl))
12491     {
12492       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12493
12494       if (host_integerp (DECL_VINDEX (func_decl), 0))
12495         add_AT_loc (die, DW_AT_vtable_elem_location,
12496                     new_loc_descr (DW_OP_constu,
12497                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12498                                    0));
12499
12500       /* GNU extension: Record what type this method came from originally.  */
12501       if (debug_info_level > DINFO_LEVEL_TERSE)
12502         add_AT_die_ref (die, DW_AT_containing_type,
12503                         lookup_type_die (DECL_CONTEXT (func_decl)));
12504     }
12505 }
12506 \f
12507 /* Add source coordinate attributes for the given decl.  */
12508
12509 static void
12510 add_src_coords_attributes (dw_die_ref die, tree decl)
12511 {
12512   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12513
12514   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12515   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12516 }
12517
12518 /* Add a DW_AT_name attribute and source coordinate attribute for the
12519    given decl, but only if it actually has a name.  */
12520
12521 static void
12522 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12523 {
12524   tree decl_name;
12525
12526   decl_name = DECL_NAME (decl);
12527   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12528     {
12529       add_name_attribute (die, dwarf2_name (decl, 0));
12530       if (! DECL_ARTIFICIAL (decl))
12531         add_src_coords_attributes (die, decl);
12532
12533       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12534           && TREE_PUBLIC (decl)
12535           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12536           && !DECL_ABSTRACT (decl)
12537           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12538           && !is_fortran ())
12539         add_AT_string (die, DW_AT_MIPS_linkage_name,
12540                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12541     }
12542
12543 #ifdef VMS_DEBUGGING_INFO
12544   /* Get the function's name, as described by its RTL.  This may be different
12545      from the DECL_NAME name used in the source file.  */
12546   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12547     {
12548       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12549                    XEXP (DECL_RTL (decl), 0));
12550       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12551     }
12552 #endif
12553 }
12554
12555 /* Push a new declaration scope.  */
12556
12557 static void
12558 push_decl_scope (tree scope)
12559 {
12560   VEC_safe_push (tree, gc, decl_scope_table, scope);
12561 }
12562
12563 /* Pop a declaration scope.  */
12564
12565 static inline void
12566 pop_decl_scope (void)
12567 {
12568   VEC_pop (tree, decl_scope_table);
12569 }
12570
12571 /* Return the DIE for the scope that immediately contains this type.
12572    Non-named types get global scope.  Named types nested in other
12573    types get their containing scope if it's open, or global scope
12574    otherwise.  All other types (i.e. function-local named types) get
12575    the current active scope.  */
12576
12577 static dw_die_ref
12578 scope_die_for (tree t, dw_die_ref context_die)
12579 {
12580   dw_die_ref scope_die = NULL;
12581   tree containing_scope;
12582   int i;
12583
12584   /* Non-types always go in the current scope.  */
12585   gcc_assert (TYPE_P (t));
12586
12587   containing_scope = TYPE_CONTEXT (t);
12588
12589   /* Use the containing namespace if it was passed in (for a declaration).  */
12590   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12591     {
12592       if (context_die == lookup_decl_die (containing_scope))
12593         /* OK */;
12594       else
12595         containing_scope = NULL_TREE;
12596     }
12597
12598   /* Ignore function type "scopes" from the C frontend.  They mean that
12599      a tagged type is local to a parmlist of a function declarator, but
12600      that isn't useful to DWARF.  */
12601   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12602     containing_scope = NULL_TREE;
12603
12604   if (containing_scope == NULL_TREE)
12605     scope_die = comp_unit_die;
12606   else if (TYPE_P (containing_scope))
12607     {
12608       /* For types, we can just look up the appropriate DIE.  But
12609          first we check to see if we're in the middle of emitting it
12610          so we know where the new DIE should go.  */
12611       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12612         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12613           break;
12614
12615       if (i < 0)
12616         {
12617           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12618                       || TREE_ASM_WRITTEN (containing_scope));
12619
12620           /* If none of the current dies are suitable, we get file scope.  */
12621           scope_die = comp_unit_die;
12622         }
12623       else
12624         scope_die = lookup_type_die (containing_scope);
12625     }
12626   else
12627     scope_die = context_die;
12628
12629   return scope_die;
12630 }
12631
12632 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12633
12634 static inline int
12635 local_scope_p (dw_die_ref context_die)
12636 {
12637   for (; context_die; context_die = context_die->die_parent)
12638     if (context_die->die_tag == DW_TAG_inlined_subroutine
12639         || context_die->die_tag == DW_TAG_subprogram)
12640       return 1;
12641
12642   return 0;
12643 }
12644
12645 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12646    whether or not to treat a DIE in this context as a declaration.  */
12647
12648 static inline int
12649 class_or_namespace_scope_p (dw_die_ref context_die)
12650 {
12651   return (context_die
12652           && (context_die->die_tag == DW_TAG_structure_type
12653               || context_die->die_tag == DW_TAG_class_type
12654               || context_die->die_tag == DW_TAG_interface_type
12655               || context_die->die_tag == DW_TAG_union_type
12656               || context_die->die_tag == DW_TAG_namespace));
12657 }
12658
12659 /* Many forms of DIEs require a "type description" attribute.  This
12660    routine locates the proper "type descriptor" die for the type given
12661    by 'type', and adds a DW_AT_type attribute below the given die.  */
12662
12663 static void
12664 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
12665                     int decl_volatile, dw_die_ref context_die)
12666 {
12667   enum tree_code code  = TREE_CODE (type);
12668   dw_die_ref type_die  = NULL;
12669
12670   /* ??? If this type is an unnamed subrange type of an integral, floating-point
12671      or fixed-point type, use the inner type.  This is because we have no
12672      support for unnamed types in base_type_die.  This can happen if this is
12673      an Ada subrange type.  Correct solution is emit a subrange type die.  */
12674   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
12675       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
12676     type = TREE_TYPE (type), code = TREE_CODE (type);
12677
12678   if (code == ERROR_MARK
12679       /* Handle a special case.  For functions whose return type is void, we
12680          generate *no* type attribute.  (Note that no object may have type
12681          `void', so this only applies to function return types).  */
12682       || code == VOID_TYPE)
12683     return;
12684
12685   type_die = modified_type_die (type,
12686                                 decl_const || TYPE_READONLY (type),
12687                                 decl_volatile || TYPE_VOLATILE (type),
12688                                 context_die);
12689
12690   if (type_die != NULL)
12691     add_AT_die_ref (object_die, DW_AT_type, type_die);
12692 }
12693
12694 /* Given an object die, add the calling convention attribute for the
12695    function call type.  */
12696 static void
12697 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
12698 {
12699   enum dwarf_calling_convention value = DW_CC_normal;
12700
12701   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
12702
12703   /* DWARF doesn't provide a way to identify a program's source-level
12704      entry point.  DW_AT_calling_convention attributes are only meant
12705      to describe functions' calling conventions.  However, lacking a
12706      better way to signal the Fortran main program, we use this for the
12707      time being, following existing custom.  */
12708   if (is_fortran ()
12709       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
12710     value = DW_CC_program;
12711
12712   /* Only add the attribute if the backend requests it, and
12713      is not DW_CC_normal.  */
12714   if (value && (value != DW_CC_normal))
12715     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
12716 }
12717
12718 /* Given a tree pointer to a struct, class, union, or enum type node, return
12719    a pointer to the (string) tag name for the given type, or zero if the type
12720    was declared without a tag.  */
12721
12722 static const char *
12723 type_tag (const_tree type)
12724 {
12725   const char *name = 0;
12726
12727   if (TYPE_NAME (type) != 0)
12728     {
12729       tree t = 0;
12730
12731       /* Find the IDENTIFIER_NODE for the type name.  */
12732       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
12733         t = TYPE_NAME (type);
12734
12735       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
12736          a TYPE_DECL node, regardless of whether or not a `typedef' was
12737          involved.  */
12738       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12739                && ! DECL_IGNORED_P (TYPE_NAME (type)))
12740         {
12741           /* We want to be extra verbose.  Don't call dwarf_name if
12742              DECL_NAME isn't set.  The default hook for decl_printable_name
12743              doesn't like that, and in this context it's correct to return
12744              0, instead of "<anonymous>" or the like.  */
12745           if (DECL_NAME (TYPE_NAME (type)))
12746             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
12747         }
12748
12749       /* Now get the name as a string, or invent one.  */
12750       if (!name && t != 0)
12751         name = IDENTIFIER_POINTER (t);
12752     }
12753
12754   return (name == 0 || *name == '\0') ? 0 : name;
12755 }
12756
12757 /* Return the type associated with a data member, make a special check
12758    for bit field types.  */
12759
12760 static inline tree
12761 member_declared_type (const_tree member)
12762 {
12763   return (DECL_BIT_FIELD_TYPE (member)
12764           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
12765 }
12766
12767 /* Get the decl's label, as described by its RTL. This may be different
12768    from the DECL_NAME name used in the source file.  */
12769
12770 #if 0
12771 static const char *
12772 decl_start_label (tree decl)
12773 {
12774   rtx x;
12775   const char *fnname;
12776
12777   x = DECL_RTL (decl);
12778   gcc_assert (MEM_P (x));
12779
12780   x = XEXP (x, 0);
12781   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12782
12783   fnname = XSTR (x, 0);
12784   return fnname;
12785 }
12786 #endif
12787 \f
12788 /* These routines generate the internal representation of the DIE's for
12789    the compilation unit.  Debugging information is collected by walking
12790    the declaration trees passed in from dwarf2out_decl().  */
12791
12792 static void
12793 gen_array_type_die (tree type, dw_die_ref context_die)
12794 {
12795   dw_die_ref scope_die = scope_die_for (type, context_die);
12796   dw_die_ref array_die;
12797
12798   /* GNU compilers represent multidimensional array types as sequences of one
12799      dimensional array types whose element types are themselves array types.
12800      We sometimes squish that down to a single array_type DIE with multiple
12801      subscripts in the Dwarf debugging info.  The draft Dwarf specification
12802      say that we are allowed to do this kind of compression in C, because
12803      there is no difference between an array of arrays and a multidimensional
12804      array.  We don't do this for Ada to remain as close as possible to the
12805      actual representation, which is especially important against the language
12806      flexibilty wrt arrays of variable size.  */
12807
12808   bool collapse_nested_arrays = !is_ada ();
12809   tree element_type;
12810
12811   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
12812      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
12813   if (TYPE_STRING_FLAG (type)
12814       && TREE_CODE (type) == ARRAY_TYPE
12815       && is_fortran ()
12816       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
12817     {
12818       HOST_WIDE_INT size;
12819
12820       array_die = new_die (DW_TAG_string_type, scope_die, type);
12821       add_name_attribute (array_die, type_tag (type));
12822       equate_type_number_to_die (type, array_die);
12823       size = int_size_in_bytes (type);
12824       if (size >= 0)
12825         add_AT_unsigned (array_die, DW_AT_byte_size, size);
12826       else if (TYPE_DOMAIN (type) != NULL_TREE
12827                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
12828                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12829         {
12830           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
12831           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
12832
12833           size = int_size_in_bytes (TREE_TYPE (szdecl));
12834           if (loc && size > 0)
12835             {
12836               add_AT_loc (array_die, DW_AT_string_length, loc);
12837               if (size != DWARF2_ADDR_SIZE)
12838                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
12839             }
12840         }
12841       return;
12842     }
12843
12844   /* ??? The SGI dwarf reader fails for array of array of enum types
12845      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
12846      array type comes before the outer array type.  We thus call gen_type_die
12847      before we new_die and must prevent nested array types collapsing for this
12848      target.  */
12849
12850 #ifdef MIPS_DEBUGGING_INFO
12851   gen_type_die (TREE_TYPE (type), context_die);
12852   collapse_nested_arrays = false;
12853 #endif
12854
12855   array_die = new_die (DW_TAG_array_type, scope_die, type);
12856   add_name_attribute (array_die, type_tag (type));
12857   equate_type_number_to_die (type, array_die);
12858
12859   if (TREE_CODE (type) == VECTOR_TYPE)
12860     {
12861       /* The frontend feeds us a representation for the vector as a struct
12862          containing an array.  Pull out the array type.  */
12863       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
12864       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
12865     }
12866
12867   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
12868   if (is_fortran ()
12869       && TREE_CODE (type) == ARRAY_TYPE
12870       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
12871       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
12872     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
12873
12874 #if 0
12875   /* We default the array ordering.  SDB will probably do
12876      the right things even if DW_AT_ordering is not present.  It's not even
12877      an issue until we start to get into multidimensional arrays anyway.  If
12878      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
12879      then we'll have to put the DW_AT_ordering attribute back in.  (But if
12880      and when we find out that we need to put these in, we will only do so
12881      for multidimensional arrays.  */
12882   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
12883 #endif
12884
12885 #ifdef MIPS_DEBUGGING_INFO
12886   /* The SGI compilers handle arrays of unknown bound by setting
12887      AT_declaration and not emitting any subrange DIEs.  */
12888   if (! TYPE_DOMAIN (type))
12889     add_AT_flag (array_die, DW_AT_declaration, 1);
12890   else
12891 #endif
12892     add_subscript_info (array_die, type, collapse_nested_arrays);
12893
12894   /* Add representation of the type of the elements of this array type and
12895      emit the corresponding DIE if we haven't done it already.  */  
12896   element_type = TREE_TYPE (type);
12897   if (collapse_nested_arrays)
12898     while (TREE_CODE (element_type) == ARRAY_TYPE)
12899       {
12900         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
12901           break;
12902         element_type = TREE_TYPE (element_type);
12903       }
12904
12905 #ifndef MIPS_DEBUGGING_INFO
12906   gen_type_die (element_type, context_die);
12907 #endif
12908
12909   add_type_attribute (array_die, element_type, 0, 0, context_die);
12910
12911   if (get_AT (array_die, DW_AT_name))
12912     add_pubtype (type, array_die);
12913 }
12914
12915 static dw_loc_descr_ref
12916 descr_info_loc (tree val, tree base_decl)
12917 {
12918   HOST_WIDE_INT size;
12919   dw_loc_descr_ref loc, loc2;
12920   enum dwarf_location_atom op;
12921
12922   if (val == base_decl)
12923     return new_loc_descr (DW_OP_push_object_address, 0, 0);
12924
12925   switch (TREE_CODE (val))
12926     {
12927     CASE_CONVERT:
12928       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12929     case VAR_DECL:
12930       return loc_descriptor_from_tree_1 (val, 0);
12931     case INTEGER_CST:
12932       if (host_integerp (val, 0))
12933         return int_loc_descriptor (tree_low_cst (val, 0));
12934       break;
12935     case INDIRECT_REF:
12936       size = int_size_in_bytes (TREE_TYPE (val));
12937       if (size < 0)
12938         break;
12939       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12940       if (!loc)
12941         break;
12942       if (size == DWARF2_ADDR_SIZE)
12943         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
12944       else
12945         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
12946       return loc;
12947     case POINTER_PLUS_EXPR:
12948     case PLUS_EXPR:
12949       if (host_integerp (TREE_OPERAND (val, 1), 1)
12950           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
12951              < 16384)
12952         {
12953           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12954           if (!loc)
12955             break;
12956           add_loc_descr (&loc,
12957                          new_loc_descr (DW_OP_plus_uconst,
12958                                         tree_low_cst (TREE_OPERAND (val, 1),
12959                                                       1), 0));
12960         }
12961       else
12962         {
12963           op = DW_OP_plus;
12964         do_binop:
12965           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
12966           if (!loc)
12967             break;
12968           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
12969           if (!loc2)
12970             break;
12971           add_loc_descr (&loc, loc2);
12972           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
12973         }
12974       return loc;
12975     case MINUS_EXPR:
12976       op = DW_OP_minus;
12977       goto do_binop;
12978     case MULT_EXPR:
12979       op = DW_OP_mul;
12980       goto do_binop;
12981     case EQ_EXPR:
12982       op = DW_OP_eq;
12983       goto do_binop;
12984     case NE_EXPR:
12985       op = DW_OP_ne;
12986       goto do_binop;
12987     default:
12988       break;
12989     }
12990   return NULL;
12991 }
12992
12993 static void
12994 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
12995                       tree val, tree base_decl)
12996 {
12997   dw_loc_descr_ref loc;
12998
12999   if (host_integerp (val, 0))
13000     {
13001       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13002       return;
13003     }
13004
13005   loc = descr_info_loc (val, base_decl);
13006   if (!loc)
13007     return;
13008
13009   add_AT_loc (die, attr, loc);
13010 }
13011
13012 /* This routine generates DIE for array with hidden descriptor, details
13013    are filled into *info by a langhook.  */
13014
13015 static void
13016 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13017                           dw_die_ref context_die)
13018 {
13019   dw_die_ref scope_die = scope_die_for (type, context_die);
13020   dw_die_ref array_die;
13021   int dim;
13022
13023   array_die = new_die (DW_TAG_array_type, scope_die, type);
13024   add_name_attribute (array_die, type_tag (type));
13025   equate_type_number_to_die (type, array_die);
13026
13027   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13028   if (is_fortran ()
13029       && info->ndimensions >= 2)
13030     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13031
13032   if (info->data_location)
13033     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13034                           info->base_decl);
13035   if (info->associated)
13036     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13037                           info->base_decl);
13038   if (info->allocated)
13039     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13040                           info->base_decl);
13041
13042   for (dim = 0; dim < info->ndimensions; dim++)
13043     {
13044       dw_die_ref subrange_die
13045         = new_die (DW_TAG_subrange_type, array_die, NULL);
13046
13047       if (info->dimen[dim].lower_bound)
13048         {
13049           /* If it is the default value, omit it.  */
13050           if ((is_c_family () || is_java ())
13051               && integer_zerop (info->dimen[dim].lower_bound))
13052             ;
13053           else if (is_fortran ()
13054                    && integer_onep (info->dimen[dim].lower_bound))
13055             ;
13056           else
13057             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13058                                   info->dimen[dim].lower_bound,
13059                                   info->base_decl);
13060         }
13061       if (info->dimen[dim].upper_bound)
13062         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13063                               info->dimen[dim].upper_bound,
13064                               info->base_decl);
13065       if (info->dimen[dim].stride)
13066         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13067                               info->dimen[dim].stride,
13068                               info->base_decl);
13069     }
13070
13071   gen_type_die (info->element_type, context_die);
13072   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13073
13074   if (get_AT (array_die, DW_AT_name))
13075     add_pubtype (type, array_die);
13076 }
13077
13078 #if 0
13079 static void
13080 gen_entry_point_die (tree decl, dw_die_ref context_die)
13081 {
13082   tree origin = decl_ultimate_origin (decl);
13083   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13084
13085   if (origin != NULL)
13086     add_abstract_origin_attribute (decl_die, origin);
13087   else
13088     {
13089       add_name_and_src_coords_attributes (decl_die, decl);
13090       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13091                           0, 0, context_die);
13092     }
13093
13094   if (DECL_ABSTRACT (decl))
13095     equate_decl_number_to_die (decl, decl_die);
13096   else
13097     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13098 }
13099 #endif
13100
13101 /* Walk through the list of incomplete types again, trying once more to
13102    emit full debugging info for them.  */
13103
13104 static void
13105 retry_incomplete_types (void)
13106 {
13107   int i;
13108
13109   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13110     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13111 }
13112
13113 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
13114
13115 static void
13116 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
13117 {
13118   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
13119
13120   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13121      be incomplete and such types are not marked.  */
13122   add_abstract_origin_attribute (type_die, type);
13123 }
13124
13125 /* Determine what tag to use for a record type.  */
13126
13127 static enum dwarf_tag
13128 record_type_tag (tree type)
13129 {
13130   if (! lang_hooks.types.classify_record)
13131     return DW_TAG_structure_type;
13132
13133   switch (lang_hooks.types.classify_record (type))
13134     {
13135     case RECORD_IS_STRUCT:
13136       return DW_TAG_structure_type;
13137
13138     case RECORD_IS_CLASS:
13139       return DW_TAG_class_type;
13140
13141     case RECORD_IS_INTERFACE:
13142       return DW_TAG_interface_type;
13143
13144     default:
13145       gcc_unreachable ();
13146     }
13147 }
13148
13149 /* Generate a DIE to represent an inlined instance of a structure type.  */
13150
13151 static void
13152 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
13153 {
13154   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
13155
13156   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13157      be incomplete and such types are not marked.  */
13158   add_abstract_origin_attribute (type_die, type);
13159 }
13160
13161 /* Generate a DIE to represent an inlined instance of a union type.  */
13162
13163 static void
13164 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
13165 {
13166   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
13167
13168   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
13169      be incomplete and such types are not marked.  */
13170   add_abstract_origin_attribute (type_die, type);
13171 }
13172
13173 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13174    include all of the information about the enumeration values also. Each
13175    enumerated type name/value is listed as a child of the enumerated type
13176    DIE.  */
13177
13178 static dw_die_ref
13179 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13180 {
13181   dw_die_ref type_die = lookup_type_die (type);
13182
13183   if (type_die == NULL)
13184     {
13185       type_die = new_die (DW_TAG_enumeration_type,
13186                           scope_die_for (type, context_die), type);
13187       equate_type_number_to_die (type, type_die);
13188       add_name_attribute (type_die, type_tag (type));
13189     }
13190   else if (! TYPE_SIZE (type))
13191     return type_die;
13192   else
13193     remove_AT (type_die, DW_AT_declaration);
13194
13195   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13196      given enum type is incomplete, do not generate the DW_AT_byte_size
13197      attribute or the DW_AT_element_list attribute.  */
13198   if (TYPE_SIZE (type))
13199     {
13200       tree link;
13201
13202       TREE_ASM_WRITTEN (type) = 1;
13203       add_byte_size_attribute (type_die, type);
13204       if (TYPE_STUB_DECL (type) != NULL_TREE)
13205         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13206
13207       /* If the first reference to this type was as the return type of an
13208          inline function, then it may not have a parent.  Fix this now.  */
13209       if (type_die->die_parent == NULL)
13210         add_child_die (scope_die_for (type, context_die), type_die);
13211
13212       for (link = TYPE_VALUES (type);
13213            link != NULL; link = TREE_CHAIN (link))
13214         {
13215           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13216           tree value = TREE_VALUE (link);
13217
13218           add_name_attribute (enum_die,
13219                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13220
13221           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13222             /* DWARF2 does not provide a way of indicating whether or
13223                not enumeration constants are signed or unsigned.  GDB
13224                always assumes the values are signed, so we output all
13225                values as if they were signed.  That means that
13226                enumeration constants with very large unsigned values
13227                will appear to have negative values in the debugger.  */
13228             add_AT_int (enum_die, DW_AT_const_value,
13229                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13230         }
13231     }
13232   else
13233     add_AT_flag (type_die, DW_AT_declaration, 1);
13234
13235   if (get_AT (type_die, DW_AT_name))
13236     add_pubtype (type, type_die);
13237
13238   return type_die;
13239 }
13240
13241 /* Generate a DIE to represent either a real live formal parameter decl or to
13242    represent just the type of some formal parameter position in some function
13243    type.
13244
13245    Note that this routine is a bit unusual because its argument may be a
13246    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13247    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13248    node.  If it's the former then this function is being called to output a
13249    DIE to represent a formal parameter object (or some inlining thereof).  If
13250    it's the latter, then this function is only being called to output a
13251    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13252    argument type of some subprogram type.  */
13253
13254 static dw_die_ref
13255 gen_formal_parameter_die (tree node, dw_die_ref context_die)
13256 {
13257   dw_die_ref parm_die
13258     = new_die (DW_TAG_formal_parameter, context_die, node);
13259   tree origin;
13260
13261   switch (TREE_CODE_CLASS (TREE_CODE (node)))
13262     {
13263     case tcc_declaration:
13264       origin = decl_ultimate_origin (node);
13265       if (origin != NULL)
13266         add_abstract_origin_attribute (parm_die, origin);
13267       else
13268         {
13269           tree type = TREE_TYPE (node);
13270           add_name_and_src_coords_attributes (parm_die, node);
13271           if (DECL_BY_REFERENCE (node))
13272             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13273                                 context_die);
13274           else
13275             add_type_attribute (parm_die, type,
13276                                 TREE_READONLY (node),
13277                                 TREE_THIS_VOLATILE (node),
13278                                 context_die);
13279           if (DECL_ARTIFICIAL (node))
13280             add_AT_flag (parm_die, DW_AT_artificial, 1);
13281         }
13282
13283       equate_decl_number_to_die (node, parm_die);
13284       if (! DECL_ABSTRACT (node))
13285         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
13286
13287       break;
13288
13289     case tcc_type:
13290       /* We were called with some kind of a ..._TYPE node.  */
13291       add_type_attribute (parm_die, node, 0, 0, context_die);
13292       break;
13293
13294     default:
13295       gcc_unreachable ();
13296     }
13297
13298   return parm_die;
13299 }
13300
13301 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13302    at the end of an (ANSI prototyped) formal parameters list.  */
13303
13304 static void
13305 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13306 {
13307   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13308 }
13309
13310 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13311    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13312    parameters as specified in some function type specification (except for
13313    those which appear as part of a function *definition*).  */
13314
13315 static void
13316 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13317 {
13318   tree link;
13319   tree formal_type = NULL;
13320   tree first_parm_type;
13321   tree arg;
13322
13323   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13324     {
13325       arg = DECL_ARGUMENTS (function_or_method_type);
13326       function_or_method_type = TREE_TYPE (function_or_method_type);
13327     }
13328   else
13329     arg = NULL_TREE;
13330
13331   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13332
13333   /* Make our first pass over the list of formal parameter types and output a
13334      DW_TAG_formal_parameter DIE for each one.  */
13335   for (link = first_parm_type; link; )
13336     {
13337       dw_die_ref parm_die;
13338
13339       formal_type = TREE_VALUE (link);
13340       if (formal_type == void_type_node)
13341         break;
13342
13343       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13344       parm_die = gen_formal_parameter_die (formal_type, context_die);
13345       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13346            && link == first_parm_type)
13347           || (arg && DECL_ARTIFICIAL (arg)))
13348         add_AT_flag (parm_die, DW_AT_artificial, 1);
13349
13350       link = TREE_CHAIN (link);
13351       if (arg)
13352         arg = TREE_CHAIN (arg);
13353     }
13354
13355   /* If this function type has an ellipsis, add a
13356      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13357   if (formal_type != void_type_node)
13358     gen_unspecified_parameters_die (function_or_method_type, context_die);
13359
13360   /* Make our second (and final) pass over the list of formal parameter types
13361      and output DIEs to represent those types (as necessary).  */
13362   for (link = TYPE_ARG_TYPES (function_or_method_type);
13363        link && TREE_VALUE (link);
13364        link = TREE_CHAIN (link))
13365     gen_type_die (TREE_VALUE (link), context_die);
13366 }
13367
13368 /* We want to generate the DIE for TYPE so that we can generate the
13369    die for MEMBER, which has been defined; we will need to refer back
13370    to the member declaration nested within TYPE.  If we're trying to
13371    generate minimal debug info for TYPE, processing TYPE won't do the
13372    trick; we need to attach the member declaration by hand.  */
13373
13374 static void
13375 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13376 {
13377   gen_type_die (type, context_die);
13378
13379   /* If we're trying to avoid duplicate debug info, we may not have
13380      emitted the member decl for this function.  Emit it now.  */
13381   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13382       && ! lookup_decl_die (member))
13383     {
13384       dw_die_ref type_die;
13385       gcc_assert (!decl_ultimate_origin (member));
13386
13387       push_decl_scope (type);
13388       type_die = lookup_type_die (type);
13389       if (TREE_CODE (member) == FUNCTION_DECL)
13390         gen_subprogram_die (member, type_die);
13391       else if (TREE_CODE (member) == FIELD_DECL)
13392         {
13393           /* Ignore the nameless fields that are used to skip bits but handle
13394              C++ anonymous unions and structs.  */
13395           if (DECL_NAME (member) != NULL_TREE
13396               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13397               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13398             {
13399               gen_type_die (member_declared_type (member), type_die);
13400               gen_field_die (member, type_die);
13401             }
13402         }
13403       else
13404         gen_variable_die (member, type_die);
13405
13406       pop_decl_scope ();
13407     }
13408 }
13409
13410 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13411    may later generate inlined and/or out-of-line instances of.  */
13412
13413 static void
13414 dwarf2out_abstract_function (tree decl)
13415 {
13416   dw_die_ref old_die;
13417   tree save_fn;
13418   tree context;
13419   int was_abstract = DECL_ABSTRACT (decl);
13420
13421   /* Make sure we have the actual abstract inline, not a clone.  */
13422   decl = DECL_ORIGIN (decl);
13423
13424   old_die = lookup_decl_die (decl);
13425   if (old_die && get_AT (old_die, DW_AT_inline))
13426     /* We've already generated the abstract instance.  */
13427     return;
13428
13429   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13430      we don't get confused by DECL_ABSTRACT.  */
13431   if (debug_info_level > DINFO_LEVEL_TERSE)
13432     {
13433       context = decl_class_context (decl);
13434       if (context)
13435         gen_type_die_for_member
13436           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13437     }
13438
13439   /* Pretend we've just finished compiling this function.  */
13440   save_fn = current_function_decl;
13441   current_function_decl = decl;
13442   push_cfun (DECL_STRUCT_FUNCTION (decl));
13443
13444   set_decl_abstract_flags (decl, 1);
13445   dwarf2out_decl (decl);
13446   if (! was_abstract)
13447     set_decl_abstract_flags (decl, 0);
13448
13449   current_function_decl = save_fn;
13450   pop_cfun ();
13451 }
13452
13453 /* Helper function of premark_used_types() which gets called through
13454    htab_traverse_resize().
13455
13456    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13457    marked as unused by prune_unused_types.  */
13458 static int
13459 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13460 {
13461   tree type;
13462   dw_die_ref die;
13463
13464   type = (tree) *slot;
13465   die = lookup_type_die (type);
13466   if (die != NULL)
13467     die->die_perennial_p = 1;
13468   return 1;
13469 }
13470
13471 /* Mark all members of used_types_hash as perennial.  */
13472 static void
13473 premark_used_types (void)
13474 {
13475   if (cfun && cfun->used_types_hash)
13476     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13477 }
13478
13479 /* Generate a DIE to represent a declared function (either file-scope or
13480    block-local).  */
13481
13482 static void
13483 gen_subprogram_die (tree decl, dw_die_ref context_die)
13484 {
13485   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13486   tree origin = decl_ultimate_origin (decl);
13487   dw_die_ref subr_die;
13488   tree fn_arg_types;
13489   tree outer_scope;
13490   dw_die_ref old_die = lookup_decl_die (decl);
13491   int declaration = (current_function_decl != decl
13492                      || class_or_namespace_scope_p (context_die));
13493
13494   premark_used_types ();
13495
13496   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13497      started to generate the abstract instance of an inline, decided to output
13498      its containing class, and proceeded to emit the declaration of the inline
13499      from the member list for the class.  If so, DECLARATION takes priority;
13500      we'll get back to the abstract instance when done with the class.  */
13501
13502   /* The class-scope declaration DIE must be the primary DIE.  */
13503   if (origin && declaration && class_or_namespace_scope_p (context_die))
13504     {
13505       origin = NULL;
13506       gcc_assert (!old_die);
13507     }
13508
13509   /* Now that the C++ front end lazily declares artificial member fns, we
13510      might need to retrofit the declaration into its class.  */
13511   if (!declaration && !origin && !old_die
13512       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13513       && !class_or_namespace_scope_p (context_die)
13514       && debug_info_level > DINFO_LEVEL_TERSE)
13515     old_die = force_decl_die (decl);
13516
13517   if (origin != NULL)
13518     {
13519       gcc_assert (!declaration || local_scope_p (context_die));
13520
13521       /* Fixup die_parent for the abstract instance of a nested
13522          inline function.  */
13523       if (old_die && old_die->die_parent == NULL)
13524         add_child_die (context_die, old_die);
13525
13526       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13527       add_abstract_origin_attribute (subr_die, origin);
13528     }
13529   else if (old_die)
13530     {
13531       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13532       struct dwarf_file_data * file_index = lookup_filename (s.file);
13533
13534       if (!get_AT_flag (old_die, DW_AT_declaration)
13535           /* We can have a normal definition following an inline one in the
13536              case of redefinition of GNU C extern inlines.
13537              It seems reasonable to use AT_specification in this case.  */
13538           && !get_AT (old_die, DW_AT_inline))
13539         {
13540           /* Detect and ignore this case, where we are trying to output
13541              something we have already output.  */
13542           return;
13543         }
13544
13545       /* If the definition comes from the same place as the declaration,
13546          maybe use the old DIE.  We always want the DIE for this function
13547          that has the *_pc attributes to be under comp_unit_die so the
13548          debugger can find it.  We also need to do this for abstract
13549          instances of inlines, since the spec requires the out-of-line copy
13550          to have the same parent.  For local class methods, this doesn't
13551          apply; we just use the old DIE.  */
13552       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13553           && (DECL_ARTIFICIAL (decl)
13554               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13555                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13556                       == (unsigned) s.line))))
13557         {
13558           subr_die = old_die;
13559
13560           /* Clear out the declaration attribute and the formal parameters.
13561              Do not remove all children, because it is possible that this
13562              declaration die was forced using force_decl_die(). In such
13563              cases die that forced declaration die (e.g. TAG_imported_module)
13564              is one of the children that we do not want to remove.  */
13565           remove_AT (subr_die, DW_AT_declaration);
13566           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13567         }
13568       else
13569         {
13570           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13571           add_AT_specification (subr_die, old_die);
13572           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13573             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13574           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13575             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13576         }
13577     }
13578   else
13579     {
13580       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13581
13582       if (TREE_PUBLIC (decl))
13583         add_AT_flag (subr_die, DW_AT_external, 1);
13584
13585       add_name_and_src_coords_attributes (subr_die, decl);
13586       if (debug_info_level > DINFO_LEVEL_TERSE)
13587         {
13588           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13589           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13590                               0, 0, context_die);
13591         }
13592
13593       add_pure_or_virtual_attribute (subr_die, decl);
13594       if (DECL_ARTIFICIAL (decl))
13595         add_AT_flag (subr_die, DW_AT_artificial, 1);
13596
13597       if (TREE_PROTECTED (decl))
13598         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13599       else if (TREE_PRIVATE (decl))
13600         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13601     }
13602
13603   if (declaration)
13604     {
13605       if (!old_die || !get_AT (old_die, DW_AT_inline))
13606         {
13607           add_AT_flag (subr_die, DW_AT_declaration, 1);
13608
13609           /* The first time we see a member function, it is in the context of
13610              the class to which it belongs.  We make sure of this by emitting
13611              the class first.  The next time is the definition, which is
13612              handled above.  The two may come from the same source text.
13613
13614              Note that force_decl_die() forces function declaration die. It is
13615              later reused to represent definition.  */
13616           equate_decl_number_to_die (decl, subr_die);
13617         }
13618     }
13619   else if (DECL_ABSTRACT (decl))
13620     {
13621       if (DECL_DECLARED_INLINE_P (decl))
13622         {
13623           if (cgraph_function_possibly_inlined_p (decl))
13624             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13625           else
13626             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13627         }
13628       else
13629         {
13630           if (cgraph_function_possibly_inlined_p (decl))
13631             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13632           else
13633             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13634         }
13635
13636       if (DECL_DECLARED_INLINE_P (decl)
13637           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13638         add_AT_flag (subr_die, DW_AT_artificial, 1);
13639
13640       equate_decl_number_to_die (decl, subr_die);
13641     }
13642   else if (!DECL_EXTERNAL (decl))
13643     {
13644       HOST_WIDE_INT cfa_fb_offset;
13645
13646       if (!old_die || !get_AT (old_die, DW_AT_inline))
13647         equate_decl_number_to_die (decl, subr_die);
13648
13649       if (!flag_reorder_blocks_and_partition)
13650         {
13651           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13652                                        current_function_funcdef_no);
13653           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13654           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13655                                        current_function_funcdef_no);
13656           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13657
13658           add_pubname (decl, subr_die);
13659           add_arange (decl, subr_die);
13660         }
13661       else
13662         {  /* Do nothing for now; maybe need to duplicate die, one for
13663               hot section and one for cold section, then use the hot/cold
13664               section begin/end labels to generate the aranges...  */
13665           /*
13666             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13667             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13668             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13669             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13670
13671             add_pubname (decl, subr_die);
13672             add_arange (decl, subr_die);
13673             add_arange (decl, subr_die);
13674            */
13675         }
13676
13677 #ifdef MIPS_DEBUGGING_INFO
13678       /* Add a reference to the FDE for this routine.  */
13679       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13680 #endif
13681
13682       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13683
13684       /* We define the "frame base" as the function's CFA.  This is more
13685          convenient for several reasons: (1) It's stable across the prologue
13686          and epilogue, which makes it better than just a frame pointer,
13687          (2) With dwarf3, there exists a one-byte encoding that allows us
13688          to reference the .debug_frame data by proxy, but failing that,
13689          (3) We can at least reuse the code inspection and interpretation
13690          code that determines the CFA position at various points in the
13691          function.  */
13692       /* ??? Use some command-line or configury switch to enable the use
13693          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
13694          consumers that understand it; fall back to "pure" dwarf2 and
13695          convert the CFA data into a location list.  */
13696       {
13697         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
13698         if (list->dw_loc_next)
13699           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
13700         else
13701           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
13702       }
13703
13704       /* Compute a displacement from the "steady-state frame pointer" to
13705          the CFA.  The former is what all stack slots and argument slots
13706          will reference in the rtl; the later is what we've told the
13707          debugger about.  We'll need to adjust all frame_base references
13708          by this displacement.  */
13709       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
13710
13711       if (cfun->static_chain_decl)
13712         add_AT_location_description (subr_die, DW_AT_static_link,
13713                  loc_descriptor_from_tree (cfun->static_chain_decl));
13714     }
13715
13716   /* Now output descriptions of the arguments for this function. This gets
13717      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
13718      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
13719      `...' at the end of the formal parameter list.  In order to find out if
13720      there was a trailing ellipsis or not, we must instead look at the type
13721      associated with the FUNCTION_DECL.  This will be a node of type
13722      FUNCTION_TYPE. If the chain of type nodes hanging off of this
13723      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
13724      an ellipsis at the end.  */
13725
13726   /* In the case where we are describing a mere function declaration, all we
13727      need to do here (and all we *can* do here) is to describe the *types* of
13728      its formal parameters.  */
13729   if (debug_info_level <= DINFO_LEVEL_TERSE)
13730     ;
13731   else if (declaration)
13732     gen_formal_types_die (decl, subr_die);
13733   else
13734     {
13735       /* Generate DIEs to represent all known formal parameters.  */
13736       tree arg_decls = DECL_ARGUMENTS (decl);
13737       tree parm;
13738
13739       /* When generating DIEs, generate the unspecified_parameters DIE
13740          instead if we come across the arg "__builtin_va_alist" */
13741       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
13742         if (TREE_CODE (parm) == PARM_DECL)
13743           {
13744             if (DECL_NAME (parm)
13745                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
13746                             "__builtin_va_alist"))
13747               gen_unspecified_parameters_die (parm, subr_die);
13748             else
13749               gen_decl_die (parm, subr_die);
13750           }
13751
13752       /* Decide whether we need an unspecified_parameters DIE at the end.
13753          There are 2 more cases to do this for: 1) the ansi ... declaration -
13754          this is detectable when the end of the arg list is not a
13755          void_type_node 2) an unprototyped function declaration (not a
13756          definition).  This just means that we have no info about the
13757          parameters at all.  */
13758       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
13759       if (fn_arg_types != NULL)
13760         {
13761           /* This is the prototyped case, check for....  */
13762           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
13763             gen_unspecified_parameters_die (decl, subr_die);
13764         }
13765       else if (DECL_INITIAL (decl) == NULL_TREE)
13766         gen_unspecified_parameters_die (decl, subr_die);
13767     }
13768
13769   /* Output Dwarf info for all of the stuff within the body of the function
13770      (if it has one - it may be just a declaration).  */
13771   outer_scope = DECL_INITIAL (decl);
13772
13773   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
13774      a function.  This BLOCK actually represents the outermost binding contour
13775      for the function, i.e. the contour in which the function's formal
13776      parameters and labels get declared. Curiously, it appears that the front
13777      end doesn't actually put the PARM_DECL nodes for the current function onto
13778      the BLOCK_VARS list for this outer scope, but are strung off of the
13779      DECL_ARGUMENTS list for the function instead.
13780
13781      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
13782      the LABEL_DECL nodes for the function however, and we output DWARF info
13783      for those in decls_for_scope.  Just within the `outer_scope' there will be
13784      a BLOCK node representing the function's outermost pair of curly braces,
13785      and any blocks used for the base and member initializers of a C++
13786      constructor function.  */
13787   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
13788     {
13789       /* Emit a DW_TAG_variable DIE for a named return value.  */
13790       if (DECL_NAME (DECL_RESULT (decl)))
13791         gen_decl_die (DECL_RESULT (decl), subr_die);
13792
13793       current_function_has_inlines = 0;
13794       decls_for_scope (outer_scope, subr_die, 0);
13795
13796 #if 0 && defined (MIPS_DEBUGGING_INFO)
13797       if (current_function_has_inlines)
13798         {
13799           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
13800           if (! comp_unit_has_inlines)
13801             {
13802               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
13803               comp_unit_has_inlines = 1;
13804             }
13805         }
13806 #endif
13807     }
13808   /* Add the calling convention attribute if requested.  */
13809   add_calling_convention_attribute (subr_die, decl);
13810
13811 }
13812
13813 /* Generate a DIE to represent a declared data object.  */
13814
13815 static void
13816 gen_variable_die (tree decl, dw_die_ref context_die)
13817 {
13818   HOST_WIDE_INT off;
13819   tree com_decl;
13820   dw_die_ref var_die;
13821   tree origin = decl_ultimate_origin (decl);
13822   dw_die_ref old_die = lookup_decl_die (decl);
13823   int declaration = (DECL_EXTERNAL (decl)
13824                      /* If DECL is COMDAT and has not actually been
13825                         emitted, we cannot take its address; there
13826                         might end up being no definition anywhere in
13827                         the program.  For example, consider the C++
13828                         test case:
13829
13830                           template <class T>
13831                           struct S { static const int i = 7; };
13832
13833                           template <class T>
13834                           const int S<T>::i;
13835
13836                           int f() { return S<int>::i; }
13837
13838                         Here, S<int>::i is not DECL_EXTERNAL, but no
13839                         definition is required, so the compiler will
13840                         not emit a definition.  */
13841                      || (TREE_CODE (decl) == VAR_DECL
13842                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
13843                      || class_or_namespace_scope_p (context_die));
13844
13845   com_decl = fortran_common (decl, &off);
13846
13847   /* Symbol in common gets emitted as a child of the common block, in the form
13848      of a data member.  */
13849   if (com_decl)
13850     {
13851       tree field;
13852       dw_die_ref com_die;
13853       dw_loc_descr_ref loc;
13854
13855       var_die = lookup_decl_die (decl);
13856       if (var_die)
13857         {
13858           if (get_AT (var_die, DW_AT_location) == NULL)
13859             {
13860               loc = loc_descriptor_from_tree (com_decl);
13861               if (loc)
13862                 {
13863                   if (off)
13864                     add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst,
13865                                                         off, 0));
13866                   add_AT_loc (var_die, DW_AT_location, loc);
13867                   remove_AT (var_die, DW_AT_declaration);
13868                 }
13869             }
13870           return;
13871         }
13872       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
13873       com_die = lookup_decl_die (com_decl);
13874       loc = loc_descriptor_from_tree (com_decl);
13875       if (com_die == NULL)
13876         {
13877           const char *cnam
13878             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
13879
13880           com_die = new_die (DW_TAG_common_block, context_die, decl);
13881           add_name_and_src_coords_attributes (com_die, com_decl);
13882           if (loc)
13883             {
13884               add_AT_loc (com_die, DW_AT_location, loc);
13885               /* Avoid sharing the same loc descriptor between
13886                  DW_TAG_common_block and DW_TAG_variable.  */
13887               loc = loc_descriptor_from_tree (com_decl);
13888             }
13889           else if (DECL_EXTERNAL (decl))
13890             add_AT_flag (com_die, DW_AT_declaration, 1);
13891           add_pubname_string (cnam, com_die); /* ??? needed? */
13892           equate_decl_number_to_die (com_decl, com_die);
13893         }
13894       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
13895         {
13896           add_AT_loc (com_die, DW_AT_location, loc);
13897           loc = loc_descriptor_from_tree (com_decl);
13898           remove_AT (com_die, DW_AT_declaration);
13899         }
13900       var_die = new_die (DW_TAG_variable, com_die, decl);
13901       add_name_and_src_coords_attributes (var_die, decl);
13902       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
13903                           TREE_THIS_VOLATILE (decl), context_die);
13904       add_AT_flag (var_die, DW_AT_external, 1);
13905       if (loc)
13906         {
13907           if (off)
13908             add_loc_descr (&loc, new_loc_descr (DW_OP_plus_uconst, off, 0));
13909           add_AT_loc (var_die, DW_AT_location, loc);
13910         }
13911       else if (DECL_EXTERNAL (decl))
13912         add_AT_flag (var_die, DW_AT_declaration, 1);
13913       equate_decl_number_to_die (decl, var_die);
13914       return;
13915     }
13916
13917   var_die = new_die (DW_TAG_variable, context_die, decl);
13918
13919   if (origin != NULL)
13920     add_abstract_origin_attribute (var_die, origin);
13921
13922   /* Loop unrolling can create multiple blocks that refer to the same
13923      static variable, so we must test for the DW_AT_declaration flag.
13924
13925      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
13926      copy decls and set the DECL_ABSTRACT flag on them instead of
13927      sharing them.
13928
13929      ??? Duplicated blocks have been rewritten to use .debug_ranges.
13930
13931      ??? The declare_in_namespace support causes us to get two DIEs for one
13932      variable, both of which are declarations.  We want to avoid considering
13933      one to be a specification, so we must test that this DIE is not a
13934      declaration.  */
13935   else if (old_die && TREE_STATIC (decl) && ! declaration
13936            && get_AT_flag (old_die, DW_AT_declaration) == 1)
13937     {
13938       /* This is a definition of a C++ class level static.  */
13939       add_AT_specification (var_die, old_die);
13940       if (DECL_NAME (decl))
13941         {
13942           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13943           struct dwarf_file_data * file_index = lookup_filename (s.file);
13944
13945           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13946             add_AT_file (var_die, DW_AT_decl_file, file_index);
13947
13948           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13949             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
13950         }
13951     }
13952   else
13953     {
13954       tree type = TREE_TYPE (decl);
13955
13956       add_name_and_src_coords_attributes (var_die, decl);
13957       if ((TREE_CODE (decl) == PARM_DECL
13958            || TREE_CODE (decl) == RESULT_DECL)
13959           && DECL_BY_REFERENCE (decl))
13960         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
13961       else
13962         add_type_attribute (var_die, type, TREE_READONLY (decl),
13963                             TREE_THIS_VOLATILE (decl), context_die);
13964
13965       if (TREE_PUBLIC (decl))
13966         add_AT_flag (var_die, DW_AT_external, 1);
13967
13968       if (DECL_ARTIFICIAL (decl))
13969         add_AT_flag (var_die, DW_AT_artificial, 1);
13970
13971       if (TREE_PROTECTED (decl))
13972         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
13973       else if (TREE_PRIVATE (decl))
13974         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
13975     }
13976
13977   if (declaration)
13978     add_AT_flag (var_die, DW_AT_declaration, 1);
13979
13980   if (DECL_ABSTRACT (decl) || declaration)
13981     equate_decl_number_to_die (decl, var_die);
13982
13983   if (! declaration && ! DECL_ABSTRACT (decl))
13984     {
13985       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
13986       add_pubname (decl, var_die);
13987     }
13988   else
13989     tree_add_const_value_attribute (var_die, decl);
13990 }
13991
13992 /* Generate a DIE to represent a named constant.  */
13993
13994 static void
13995 gen_const_die (tree decl, dw_die_ref context_die)
13996 {
13997   dw_die_ref const_die;
13998   tree type = TREE_TYPE (decl);
13999
14000   const_die = new_die (DW_TAG_constant, context_die, decl);
14001   add_name_and_src_coords_attributes (const_die, decl);
14002   add_type_attribute (const_die, type, 1, 0, context_die);
14003   if (TREE_PUBLIC (decl))
14004     add_AT_flag (const_die, DW_AT_external, 1);
14005   if (DECL_ARTIFICIAL (decl))
14006     add_AT_flag (const_die, DW_AT_artificial, 1);
14007   tree_add_const_value_attribute (const_die, decl);
14008 }
14009
14010 /* Generate a DIE to represent a label identifier.  */
14011
14012 static void
14013 gen_label_die (tree decl, dw_die_ref context_die)
14014 {
14015   tree origin = decl_ultimate_origin (decl);
14016   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14017   rtx insn;
14018   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14019
14020   if (origin != NULL)
14021     add_abstract_origin_attribute (lbl_die, origin);
14022   else
14023     add_name_and_src_coords_attributes (lbl_die, decl);
14024
14025   if (DECL_ABSTRACT (decl))
14026     equate_decl_number_to_die (decl, lbl_die);
14027   else
14028     {
14029       insn = DECL_RTL_IF_SET (decl);
14030
14031       /* Deleted labels are programmer specified labels which have been
14032          eliminated because of various optimizations.  We still emit them
14033          here so that it is possible to put breakpoints on them.  */
14034       if (insn
14035           && (LABEL_P (insn)
14036               || ((NOTE_P (insn)
14037                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14038         {
14039           /* When optimization is enabled (via -O) some parts of the compiler
14040              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14041              represent source-level labels which were explicitly declared by
14042              the user.  This really shouldn't be happening though, so catch
14043              it if it ever does happen.  */
14044           gcc_assert (!INSN_DELETED_P (insn));
14045
14046           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14047           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14048         }
14049     }
14050 }
14051
14052 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14053    attributes to the DIE for a block STMT, to describe where the inlined
14054    function was called from.  This is similar to add_src_coords_attributes.  */
14055
14056 static inline void
14057 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14058 {
14059   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14060
14061   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14062   add_AT_unsigned (die, DW_AT_call_line, s.line);
14063 }
14064
14065
14066 /* If STMT's abstract origin is a function declaration and STMT's
14067    first subblock's abstract origin is the function's outermost block,
14068    then we're looking at the main entry point.  */
14069 static bool
14070 is_inlined_entry_point (const_tree stmt)
14071 {
14072   tree decl, block;
14073
14074   if (!stmt || TREE_CODE (stmt) != BLOCK)
14075     return false;
14076
14077   decl = block_ultimate_origin (stmt);
14078
14079   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
14080     return false;
14081
14082   block = BLOCK_SUBBLOCKS (stmt);
14083
14084   if (block)
14085     {
14086       if (TREE_CODE (block) != BLOCK)
14087         return false;
14088
14089       block = block_ultimate_origin (block);
14090     }
14091
14092   return block == DECL_INITIAL (decl);
14093 }
14094
14095 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14096    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14097
14098 static inline void
14099 add_high_low_attributes (tree stmt, dw_die_ref die)
14100 {
14101   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14102
14103   if (BLOCK_FRAGMENT_CHAIN (stmt))
14104     {
14105       tree chain;
14106
14107       if (is_inlined_entry_point (stmt))
14108         {
14109           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14110                                        BLOCK_NUMBER (stmt));
14111           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14112         }
14113
14114       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14115
14116       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14117       do
14118         {
14119           add_ranges (chain);
14120           chain = BLOCK_FRAGMENT_CHAIN (chain);
14121         }
14122       while (chain);
14123       add_ranges (NULL);
14124     }
14125   else
14126     {
14127       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14128                                    BLOCK_NUMBER (stmt));
14129       add_AT_lbl_id (die, DW_AT_low_pc, label);
14130       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14131                                    BLOCK_NUMBER (stmt));
14132       add_AT_lbl_id (die, DW_AT_high_pc, label);
14133     }
14134 }
14135
14136 /* Generate a DIE for a lexical block.  */
14137
14138 static void
14139 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14140 {
14141   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14142
14143   if (! BLOCK_ABSTRACT (stmt))
14144     add_high_low_attributes (stmt, stmt_die);
14145
14146   decls_for_scope (stmt, stmt_die, depth);
14147 }
14148
14149 /* Generate a DIE for an inlined subprogram.  */
14150
14151 static void
14152 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14153 {
14154   tree decl = block_ultimate_origin (stmt);
14155
14156   /* Emit info for the abstract instance first, if we haven't yet.  We
14157      must emit this even if the block is abstract, otherwise when we
14158      emit the block below (or elsewhere), we may end up trying to emit
14159      a die whose origin die hasn't been emitted, and crashing.  */
14160   dwarf2out_abstract_function (decl);
14161
14162   if (! BLOCK_ABSTRACT (stmt))
14163     {
14164       dw_die_ref subr_die
14165         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14166
14167       add_abstract_origin_attribute (subr_die, decl);
14168       add_high_low_attributes (stmt, subr_die);
14169       add_call_src_coords_attributes (stmt, subr_die);
14170
14171       decls_for_scope (stmt, subr_die, depth);
14172       current_function_has_inlines = 1;
14173     }
14174   else
14175     /* We may get here if we're the outer block of function A that was
14176        inlined into function B that was inlined into function C.  When
14177        generating debugging info for C, dwarf2out_abstract_function(B)
14178        would mark all inlined blocks as abstract, including this one.
14179        So, we wouldn't (and shouldn't) expect labels to be generated
14180        for this one.  Instead, just emit debugging info for
14181        declarations within the block.  This is particularly important
14182        in the case of initializers of arguments passed from B to us:
14183        if they're statement expressions containing declarations, we
14184        wouldn't generate dies for their abstract variables, and then,
14185        when generating dies for the real variables, we'd die (pun
14186        intended :-)  */
14187     gen_lexical_block_die (stmt, context_die, depth);
14188 }
14189
14190 /* Generate a DIE for a field in a record, or structure.  */
14191
14192 static void
14193 gen_field_die (tree decl, dw_die_ref context_die)
14194 {
14195   dw_die_ref decl_die;
14196
14197   if (TREE_TYPE (decl) == error_mark_node)
14198     return;
14199
14200   decl_die = new_die (DW_TAG_member, context_die, decl);
14201   add_name_and_src_coords_attributes (decl_die, decl);
14202   add_type_attribute (decl_die, member_declared_type (decl),
14203                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14204                       context_die);
14205
14206   if (DECL_BIT_FIELD_TYPE (decl))
14207     {
14208       add_byte_size_attribute (decl_die, decl);
14209       add_bit_size_attribute (decl_die, decl);
14210       add_bit_offset_attribute (decl_die, decl);
14211     }
14212
14213   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14214     add_data_member_location_attribute (decl_die, decl);
14215
14216   if (DECL_ARTIFICIAL (decl))
14217     add_AT_flag (decl_die, DW_AT_artificial, 1);
14218
14219   if (TREE_PROTECTED (decl))
14220     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14221   else if (TREE_PRIVATE (decl))
14222     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14223
14224   /* Equate decl number to die, so that we can look up this decl later on.  */
14225   equate_decl_number_to_die (decl, decl_die);
14226 }
14227
14228 #if 0
14229 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14230    Use modified_type_die instead.
14231    We keep this code here just in case these types of DIEs may be needed to
14232    represent certain things in other languages (e.g. Pascal) someday.  */
14233
14234 static void
14235 gen_pointer_type_die (tree type, dw_die_ref context_die)
14236 {
14237   dw_die_ref ptr_die
14238     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14239
14240   equate_type_number_to_die (type, ptr_die);
14241   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14242   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14243 }
14244
14245 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14246    Use modified_type_die instead.
14247    We keep this code here just in case these types of DIEs may be needed to
14248    represent certain things in other languages (e.g. Pascal) someday.  */
14249
14250 static void
14251 gen_reference_type_die (tree type, dw_die_ref context_die)
14252 {
14253   dw_die_ref ref_die
14254     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14255
14256   equate_type_number_to_die (type, ref_die);
14257   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14258   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14259 }
14260 #endif
14261
14262 /* Generate a DIE for a pointer to a member type.  */
14263
14264 static void
14265 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14266 {
14267   dw_die_ref ptr_die
14268     = new_die (DW_TAG_ptr_to_member_type,
14269                scope_die_for (type, context_die), type);
14270
14271   equate_type_number_to_die (type, ptr_die);
14272   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14273                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14274   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14275 }
14276
14277 /* Generate the DIE for the compilation unit.  */
14278
14279 static dw_die_ref
14280 gen_compile_unit_die (const char *filename)
14281 {
14282   dw_die_ref die;
14283   char producer[250];
14284   const char *language_string = lang_hooks.name;
14285   int language;
14286
14287   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14288
14289   if (filename)
14290     {
14291       add_name_attribute (die, filename);
14292       /* Don't add cwd for <built-in>.  */
14293       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14294         add_comp_dir_attribute (die);
14295     }
14296
14297   sprintf (producer, "%s %s", language_string, version_string);
14298
14299 #ifdef MIPS_DEBUGGING_INFO
14300   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14301      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14302      not appear in the producer string, the debugger reaches the conclusion
14303      that the object file is stripped and has no debugging information.
14304      To get the MIPS/SGI debugger to believe that there is debugging
14305      information in the object file, we add a -g to the producer string.  */
14306   if (debug_info_level > DINFO_LEVEL_TERSE)
14307     strcat (producer, " -g");
14308 #endif
14309
14310   add_AT_string (die, DW_AT_producer, producer);
14311
14312   if (strcmp (language_string, "GNU C++") == 0)
14313     language = DW_LANG_C_plus_plus;
14314   else if (strcmp (language_string, "GNU Ada") == 0)
14315     language = DW_LANG_Ada95;
14316   else if (strcmp (language_string, "GNU F77") == 0)
14317     language = DW_LANG_Fortran77;
14318   else if (strcmp (language_string, "GNU Fortran") == 0)
14319     language = DW_LANG_Fortran95;
14320   else if (strcmp (language_string, "GNU Pascal") == 0)
14321     language = DW_LANG_Pascal83;
14322   else if (strcmp (language_string, "GNU Java") == 0)
14323     language = DW_LANG_Java;
14324   else if (strcmp (language_string, "GNU Objective-C") == 0)
14325     language = DW_LANG_ObjC;
14326   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14327     language = DW_LANG_ObjC_plus_plus;
14328   else
14329     language = DW_LANG_C89;
14330
14331   add_AT_unsigned (die, DW_AT_language, language);
14332   return die;
14333 }
14334
14335 /* Generate the DIE for a base class.  */
14336
14337 static void
14338 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14339 {
14340   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14341
14342   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14343   add_data_member_location_attribute (die, binfo);
14344
14345   if (BINFO_VIRTUAL_P (binfo))
14346     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14347
14348   if (access == access_public_node)
14349     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14350   else if (access == access_protected_node)
14351     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14352 }
14353
14354 /* Generate a DIE for a class member.  */
14355
14356 static void
14357 gen_member_die (tree type, dw_die_ref context_die)
14358 {
14359   tree member;
14360   tree binfo = TYPE_BINFO (type);
14361   dw_die_ref child;
14362
14363   /* If this is not an incomplete type, output descriptions of each of its
14364      members. Note that as we output the DIEs necessary to represent the
14365      members of this record or union type, we will also be trying to output
14366      DIEs to represent the *types* of those members. However the `type'
14367      function (above) will specifically avoid generating type DIEs for member
14368      types *within* the list of member DIEs for this (containing) type except
14369      for those types (of members) which are explicitly marked as also being
14370      members of this (containing) type themselves.  The g++ front- end can
14371      force any given type to be treated as a member of some other (containing)
14372      type by setting the TYPE_CONTEXT of the given (member) type to point to
14373      the TREE node representing the appropriate (containing) type.  */
14374
14375   /* First output info about the base classes.  */
14376   if (binfo)
14377     {
14378       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14379       int i;
14380       tree base;
14381
14382       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14383         gen_inheritance_die (base,
14384                              (accesses ? VEC_index (tree, accesses, i)
14385                               : access_public_node), context_die);
14386     }
14387
14388   /* Now output info about the data members and type members.  */
14389   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14390     {
14391       /* If we thought we were generating minimal debug info for TYPE
14392          and then changed our minds, some of the member declarations
14393          may have already been defined.  Don't define them again, but
14394          do put them in the right order.  */
14395
14396       child = lookup_decl_die (member);
14397       if (child)
14398         splice_child_die (context_die, child);
14399       else
14400         gen_decl_die (member, context_die);
14401     }
14402
14403   /* Now output info about the function members (if any).  */
14404   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14405     {
14406       /* Don't include clones in the member list.  */
14407       if (DECL_ABSTRACT_ORIGIN (member))
14408         continue;
14409
14410       child = lookup_decl_die (member);
14411       if (child)
14412         splice_child_die (context_die, child);
14413       else
14414         gen_decl_die (member, context_die);
14415     }
14416 }
14417
14418 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14419    is set, we pretend that the type was never defined, so we only get the
14420    member DIEs needed by later specification DIEs.  */
14421
14422 static void
14423 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14424                                 enum debug_info_usage usage)
14425 {
14426   dw_die_ref type_die = lookup_type_die (type);
14427   dw_die_ref scope_die = 0;
14428   int nested = 0;
14429   int complete = (TYPE_SIZE (type)
14430                   && (! TYPE_STUB_DECL (type)
14431                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14432   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14433   complete = complete && should_emit_struct_debug (type, usage);
14434
14435   if (type_die && ! complete)
14436     return;
14437
14438   if (TYPE_CONTEXT (type) != NULL_TREE
14439       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14440           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14441     nested = 1;
14442
14443   scope_die = scope_die_for (type, context_die);
14444
14445   if (! type_die || (nested && scope_die == comp_unit_die))
14446     /* First occurrence of type or toplevel definition of nested class.  */
14447     {
14448       dw_die_ref old_die = type_die;
14449
14450       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14451                           ? record_type_tag (type) : DW_TAG_union_type,
14452                           scope_die, type);
14453       equate_type_number_to_die (type, type_die);
14454       if (old_die)
14455         add_AT_specification (type_die, old_die);
14456       else
14457         add_name_attribute (type_die, type_tag (type));
14458     }
14459   else
14460     remove_AT (type_die, DW_AT_declaration);
14461
14462   /* If this type has been completed, then give it a byte_size attribute and
14463      then give a list of members.  */
14464   if (complete && !ns_decl)
14465     {
14466       /* Prevent infinite recursion in cases where the type of some member of
14467          this type is expressed in terms of this type itself.  */
14468       TREE_ASM_WRITTEN (type) = 1;
14469       add_byte_size_attribute (type_die, type);
14470       if (TYPE_STUB_DECL (type) != NULL_TREE)
14471         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14472
14473       /* If the first reference to this type was as the return type of an
14474          inline function, then it may not have a parent.  Fix this now.  */
14475       if (type_die->die_parent == NULL)
14476         add_child_die (scope_die, type_die);
14477
14478       push_decl_scope (type);
14479       gen_member_die (type, type_die);
14480       pop_decl_scope ();
14481
14482       /* GNU extension: Record what type our vtable lives in.  */
14483       if (TYPE_VFIELD (type))
14484         {
14485           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14486
14487           gen_type_die (vtype, context_die);
14488           add_AT_die_ref (type_die, DW_AT_containing_type,
14489                           lookup_type_die (vtype));
14490         }
14491     }
14492   else
14493     {
14494       add_AT_flag (type_die, DW_AT_declaration, 1);
14495
14496       /* We don't need to do this for function-local types.  */
14497       if (TYPE_STUB_DECL (type)
14498           && ! decl_function_context (TYPE_STUB_DECL (type)))
14499         VEC_safe_push (tree, gc, incomplete_types, type);
14500     }
14501
14502   if (get_AT (type_die, DW_AT_name))
14503     add_pubtype (type, type_die);
14504 }
14505
14506 /* Generate a DIE for a subroutine _type_.  */
14507
14508 static void
14509 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14510 {
14511   tree return_type = TREE_TYPE (type);
14512   dw_die_ref subr_die
14513     = new_die (DW_TAG_subroutine_type,
14514                scope_die_for (type, context_die), type);
14515
14516   equate_type_number_to_die (type, subr_die);
14517   add_prototyped_attribute (subr_die, type);
14518   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14519   gen_formal_types_die (type, subr_die);
14520
14521   if (get_AT (subr_die, DW_AT_name))
14522     add_pubtype (type, subr_die);
14523 }
14524
14525 /* Generate a DIE for a type definition.  */
14526
14527 static void
14528 gen_typedef_die (tree decl, dw_die_ref context_die)
14529 {
14530   dw_die_ref type_die;
14531   tree origin;
14532
14533   if (TREE_ASM_WRITTEN (decl))
14534     return;
14535
14536   TREE_ASM_WRITTEN (decl) = 1;
14537   type_die = new_die (DW_TAG_typedef, context_die, decl);
14538   origin = decl_ultimate_origin (decl);
14539   if (origin != NULL)
14540     add_abstract_origin_attribute (type_die, origin);
14541   else
14542     {
14543       tree type;
14544
14545       add_name_and_src_coords_attributes (type_die, decl);
14546       if (DECL_ORIGINAL_TYPE (decl))
14547         {
14548           type = DECL_ORIGINAL_TYPE (decl);
14549
14550           gcc_assert (type != TREE_TYPE (decl));
14551           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14552         }
14553       else
14554         type = TREE_TYPE (decl);
14555
14556       add_type_attribute (type_die, type, TREE_READONLY (decl),
14557                           TREE_THIS_VOLATILE (decl), context_die);
14558     }
14559
14560   if (DECL_ABSTRACT (decl))
14561     equate_decl_number_to_die (decl, type_die);
14562
14563   if (get_AT (type_die, DW_AT_name))
14564     add_pubtype (decl, type_die);
14565 }
14566
14567 /* Generate a type description DIE.  */
14568
14569 static void
14570 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14571                                 enum debug_info_usage usage)
14572 {
14573   int need_pop;
14574   struct array_descr_info info;
14575
14576   if (type == NULL_TREE || type == error_mark_node)
14577     return;
14578
14579   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14580       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14581     {
14582       if (TREE_ASM_WRITTEN (type))
14583         return;
14584
14585       /* Prevent broken recursion; we can't hand off to the same type.  */
14586       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14587
14588       TREE_ASM_WRITTEN (type) = 1;
14589       gen_decl_die (TYPE_NAME (type), context_die);
14590       return;
14591     }
14592
14593   /* If this is an array type with hidden descriptor, handle it first.  */
14594   if (!TREE_ASM_WRITTEN (type)
14595       && lang_hooks.types.get_array_descr_info
14596       && lang_hooks.types.get_array_descr_info (type, &info))
14597     {
14598       gen_descr_array_type_die (type, &info, context_die);
14599       TREE_ASM_WRITTEN (type) = 1;
14600       return;
14601     }
14602
14603   /* We are going to output a DIE to represent the unqualified version
14604      of this type (i.e. without any const or volatile qualifiers) so
14605      get the main variant (i.e. the unqualified version) of this type
14606      now.  (Vectors are special because the debugging info is in the
14607      cloned type itself).  */
14608   if (TREE_CODE (type) != VECTOR_TYPE)
14609     type = type_main_variant (type);
14610
14611   if (TREE_ASM_WRITTEN (type))
14612     return;
14613
14614   switch (TREE_CODE (type))
14615     {
14616     case ERROR_MARK:
14617       break;
14618
14619     case POINTER_TYPE:
14620     case REFERENCE_TYPE:
14621       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
14622          ensures that the gen_type_die recursion will terminate even if the
14623          type is recursive.  Recursive types are possible in Ada.  */
14624       /* ??? We could perhaps do this for all types before the switch
14625          statement.  */
14626       TREE_ASM_WRITTEN (type) = 1;
14627
14628       /* For these types, all that is required is that we output a DIE (or a
14629          set of DIEs) to represent the "basis" type.  */
14630       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14631                                 DINFO_USAGE_IND_USE);
14632       break;
14633
14634     case OFFSET_TYPE:
14635       /* This code is used for C++ pointer-to-data-member types.
14636          Output a description of the relevant class type.  */
14637       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
14638                                         DINFO_USAGE_IND_USE);
14639
14640       /* Output a description of the type of the object pointed to.  */
14641       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14642                                         DINFO_USAGE_IND_USE);
14643
14644       /* Now output a DIE to represent this pointer-to-data-member type
14645          itself.  */
14646       gen_ptr_to_mbr_type_die (type, context_die);
14647       break;
14648
14649     case FUNCTION_TYPE:
14650       /* Force out return type (in case it wasn't forced out already).  */
14651       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14652                                         DINFO_USAGE_DIR_USE);
14653       gen_subroutine_type_die (type, context_die);
14654       break;
14655
14656     case METHOD_TYPE:
14657       /* Force out return type (in case it wasn't forced out already).  */
14658       gen_type_die_with_usage (TREE_TYPE (type), context_die,
14659                                         DINFO_USAGE_DIR_USE);
14660       gen_subroutine_type_die (type, context_die);
14661       break;
14662
14663     case ARRAY_TYPE:
14664       gen_array_type_die (type, context_die);
14665       break;
14666
14667     case VECTOR_TYPE:
14668       gen_array_type_die (type, context_die);
14669       break;
14670
14671     case ENUMERAL_TYPE:
14672     case RECORD_TYPE:
14673     case UNION_TYPE:
14674     case QUAL_UNION_TYPE:
14675       /* If this is a nested type whose containing class hasn't been written
14676          out yet, writing it out will cover this one, too.  This does not apply
14677          to instantiations of member class templates; they need to be added to
14678          the containing class as they are generated.  FIXME: This hurts the
14679          idea of combining type decls from multiple TUs, since we can't predict
14680          what set of template instantiations we'll get.  */
14681       if (TYPE_CONTEXT (type)
14682           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14683           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
14684         {
14685           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
14686
14687           if (TREE_ASM_WRITTEN (type))
14688             return;
14689
14690           /* If that failed, attach ourselves to the stub.  */
14691           push_decl_scope (TYPE_CONTEXT (type));
14692           context_die = lookup_type_die (TYPE_CONTEXT (type));
14693           need_pop = 1;
14694         }
14695       else
14696         {
14697           context_die = declare_in_namespace (type, context_die);
14698           need_pop = 0;
14699         }
14700
14701       if (TREE_CODE (type) == ENUMERAL_TYPE)
14702         {
14703           /* This might have been written out by the call to
14704              declare_in_namespace.  */
14705           if (!TREE_ASM_WRITTEN (type))
14706             gen_enumeration_type_die (type, context_die);
14707         }
14708       else
14709         gen_struct_or_union_type_die (type, context_die, usage);
14710
14711       if (need_pop)
14712         pop_decl_scope ();
14713
14714       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
14715          it up if it is ever completed.  gen_*_type_die will set it for us
14716          when appropriate.  */
14717       return;
14718
14719     case VOID_TYPE:
14720     case INTEGER_TYPE:
14721     case REAL_TYPE:
14722     case FIXED_POINT_TYPE:
14723     case COMPLEX_TYPE:
14724     case BOOLEAN_TYPE:
14725       /* No DIEs needed for fundamental types.  */
14726       break;
14727
14728     case LANG_TYPE:
14729       /* No Dwarf representation currently defined.  */
14730       break;
14731
14732     default:
14733       gcc_unreachable ();
14734     }
14735
14736   TREE_ASM_WRITTEN (type) = 1;
14737 }
14738
14739 static void
14740 gen_type_die (tree type, dw_die_ref context_die)
14741 {
14742   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
14743 }
14744
14745 /* Generate a DIE for a tagged type instantiation.  */
14746
14747 static void
14748 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
14749 {
14750   if (type == NULL_TREE || type == error_mark_node)
14751     return;
14752
14753   /* We are going to output a DIE to represent the unqualified version of
14754      this type (i.e. without any const or volatile qualifiers) so make sure
14755      that we have the main variant (i.e. the unqualified version) of this
14756      type now.  */
14757   gcc_assert (type == type_main_variant (type));
14758
14759   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
14760      an instance of an unresolved type.  */
14761
14762   switch (TREE_CODE (type))
14763     {
14764     case ERROR_MARK:
14765       break;
14766
14767     case ENUMERAL_TYPE:
14768       gen_inlined_enumeration_type_die (type, context_die);
14769       break;
14770
14771     case RECORD_TYPE:
14772       gen_inlined_structure_type_die (type, context_die);
14773       break;
14774
14775     case UNION_TYPE:
14776     case QUAL_UNION_TYPE:
14777       gen_inlined_union_type_die (type, context_die);
14778       break;
14779
14780     default:
14781       gcc_unreachable ();
14782     }
14783 }
14784
14785 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
14786    things which are local to the given block.  */
14787
14788 static void
14789 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
14790 {
14791   int must_output_die = 0;
14792   tree origin;
14793   tree decl;
14794   enum tree_code origin_code;
14795
14796   /* Ignore blocks that are NULL.  */
14797   if (stmt == NULL_TREE)
14798     return;
14799
14800   /* If the block is one fragment of a non-contiguous block, do not
14801      process the variables, since they will have been done by the
14802      origin block.  Do process subblocks.  */
14803   if (BLOCK_FRAGMENT_ORIGIN (stmt))
14804     {
14805       tree sub;
14806
14807       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
14808         gen_block_die (sub, context_die, depth + 1);
14809
14810       return;
14811     }
14812
14813   /* Determine the "ultimate origin" of this block.  This block may be an
14814      inlined instance of an inlined instance of inline function, so we have
14815      to trace all of the way back through the origin chain to find out what
14816      sort of node actually served as the original seed for the creation of
14817      the current block.  */
14818   origin = block_ultimate_origin (stmt);
14819   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
14820
14821   /* Determine if we need to output any Dwarf DIEs at all to represent this
14822      block.  */
14823   if (origin_code == FUNCTION_DECL)
14824     /* The outer scopes for inlinings *must* always be represented.  We
14825        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
14826     must_output_die = 1;
14827   else
14828     {
14829       /* In the case where the current block represents an inlining of the
14830          "body block" of an inline function, we must *NOT* output any DIE for
14831          this block because we have already output a DIE to represent the whole
14832          inlined function scope and the "body block" of any function doesn't
14833          really represent a different scope according to ANSI C rules.  So we
14834          check here to make sure that this block does not represent a "body
14835          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
14836       if (! is_body_block (origin ? origin : stmt))
14837         {
14838           /* Determine if this block directly contains any "significant"
14839              local declarations which we will need to output DIEs for.  */
14840           if (debug_info_level > DINFO_LEVEL_TERSE)
14841             /* We are not in terse mode so *any* local declaration counts
14842                as being a "significant" one.  */
14843             must_output_die = (BLOCK_VARS (stmt) != NULL
14844                                && (TREE_USED (stmt)
14845                                    || TREE_ASM_WRITTEN (stmt)
14846                                    || BLOCK_ABSTRACT (stmt)));
14847           else
14848             /* We are in terse mode, so only local (nested) function
14849                definitions count as "significant" local declarations.  */
14850             for (decl = BLOCK_VARS (stmt);
14851                  decl != NULL; decl = TREE_CHAIN (decl))
14852               if (TREE_CODE (decl) == FUNCTION_DECL
14853                   && DECL_INITIAL (decl))
14854                 {
14855                   must_output_die = 1;
14856                   break;
14857                 }
14858         }
14859     }
14860
14861   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
14862      DIE for any block which contains no significant local declarations at
14863      all.  Rather, in such cases we just call `decls_for_scope' so that any
14864      needed Dwarf info for any sub-blocks will get properly generated. Note
14865      that in terse mode, our definition of what constitutes a "significant"
14866      local declaration gets restricted to include only inlined function
14867      instances and local (nested) function definitions.  */
14868   if (must_output_die)
14869     {
14870       if (origin_code == FUNCTION_DECL)
14871         gen_inlined_subroutine_die (stmt, context_die, depth);
14872       else
14873         gen_lexical_block_die (stmt, context_die, depth);
14874     }
14875   else
14876     decls_for_scope (stmt, context_die, depth);
14877 }
14878
14879 /* Generate all of the decls declared within a given scope and (recursively)
14880    all of its sub-blocks.  */
14881
14882 static void
14883 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
14884 {
14885   tree decl;
14886   tree subblocks;
14887
14888   /* Ignore NULL blocks.  */
14889   if (stmt == NULL_TREE)
14890     return;
14891
14892   if (TREE_USED (stmt))
14893     {
14894       /* Output the DIEs to represent all of the data objects and typedefs
14895          declared directly within this block but not within any nested
14896          sub-blocks.  Also, nested function and tag DIEs have been
14897          generated with a parent of NULL; fix that up now.  */
14898       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
14899         {
14900           dw_die_ref die;
14901
14902           if (TREE_CODE (decl) == FUNCTION_DECL)
14903             die = lookup_decl_die (decl);
14904           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
14905             die = lookup_type_die (TREE_TYPE (decl));
14906           else
14907             die = NULL;
14908
14909           if (die != NULL && die->die_parent == NULL)
14910             add_child_die (context_die, die);
14911           /* Do not produce debug information for static variables since
14912              these might be optimized out.  We are called for these later
14913              in varpool_analyze_pending_decls.
14914
14915              But *do* produce it for Fortran COMMON variables because,
14916              even though they are static, their names can differ depending
14917              on the scope, which we need to preserve.  */
14918           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
14919               && !(is_fortran () && TREE_PUBLIC (decl)))
14920             ;
14921           else
14922             gen_decl_die (decl, context_die);
14923         }
14924     }
14925
14926   /* If we're at -g1, we're not interested in subblocks.  */
14927   if (debug_info_level <= DINFO_LEVEL_TERSE)
14928     return;
14929
14930   /* Output the DIEs to represent all sub-blocks (and the items declared
14931      therein) of this block.  */
14932   for (subblocks = BLOCK_SUBBLOCKS (stmt);
14933        subblocks != NULL;
14934        subblocks = BLOCK_CHAIN (subblocks))
14935     gen_block_die (subblocks, context_die, depth + 1);
14936 }
14937
14938 /* Is this a typedef we can avoid emitting?  */
14939
14940 static inline int
14941 is_redundant_typedef (const_tree decl)
14942 {
14943   if (TYPE_DECL_IS_STUB (decl))
14944     return 1;
14945
14946   if (DECL_ARTIFICIAL (decl)
14947       && DECL_CONTEXT (decl)
14948       && is_tagged_type (DECL_CONTEXT (decl))
14949       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
14950       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
14951     /* Also ignore the artificial member typedef for the class name.  */
14952     return 1;
14953
14954   return 0;
14955 }
14956
14957 /* Returns the DIE for a context.  */
14958
14959 static inline dw_die_ref
14960 get_context_die (tree context)
14961 {
14962   if (context)
14963     {
14964       /* Find die that represents this context.  */
14965       if (TYPE_P (context))
14966         return force_type_die (context);
14967       else
14968         return force_decl_die (context);
14969     }
14970   return comp_unit_die;
14971 }
14972
14973 /* Returns the DIE for decl.  A DIE will always be returned.  */
14974
14975 static dw_die_ref
14976 force_decl_die (tree decl)
14977 {
14978   dw_die_ref decl_die;
14979   unsigned saved_external_flag;
14980   tree save_fn = NULL_TREE;
14981   decl_die = lookup_decl_die (decl);
14982   if (!decl_die)
14983     {
14984       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
14985
14986       decl_die = lookup_decl_die (decl);
14987       if (decl_die)
14988         return decl_die;
14989
14990       switch (TREE_CODE (decl))
14991         {
14992         case FUNCTION_DECL:
14993           /* Clear current_function_decl, so that gen_subprogram_die thinks
14994              that this is a declaration. At this point, we just want to force
14995              declaration die.  */
14996           save_fn = current_function_decl;
14997           current_function_decl = NULL_TREE;
14998           gen_subprogram_die (decl, context_die);
14999           current_function_decl = save_fn;
15000           break;
15001
15002         case VAR_DECL:
15003           /* Set external flag to force declaration die. Restore it after
15004            gen_decl_die() call.  */
15005           saved_external_flag = DECL_EXTERNAL (decl);
15006           DECL_EXTERNAL (decl) = 1;
15007           gen_decl_die (decl, context_die);
15008           DECL_EXTERNAL (decl) = saved_external_flag;
15009           break;
15010
15011         case NAMESPACE_DECL:
15012           dwarf2out_decl (decl);
15013           break;
15014
15015         default:
15016           gcc_unreachable ();
15017         }
15018
15019       /* We should be able to find the DIE now.  */
15020       if (!decl_die)
15021         decl_die = lookup_decl_die (decl);
15022       gcc_assert (decl_die);
15023     }
15024
15025   return decl_die;
15026 }
15027
15028 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15029    always returned.  */
15030
15031 static dw_die_ref
15032 force_type_die (tree type)
15033 {
15034   dw_die_ref type_die;
15035
15036   type_die = lookup_type_die (type);
15037   if (!type_die)
15038     {
15039       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15040
15041       type_die = modified_type_die (type, TYPE_READONLY (type),
15042                                     TYPE_VOLATILE (type), context_die);
15043       gcc_assert (type_die);
15044     }
15045   return type_die;
15046 }
15047
15048 /* Force out any required namespaces to be able to output DECL,
15049    and return the new context_die for it, if it's changed.  */
15050
15051 static dw_die_ref
15052 setup_namespace_context (tree thing, dw_die_ref context_die)
15053 {
15054   tree context = (DECL_P (thing)
15055                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15056   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15057     /* Force out the namespace.  */
15058     context_die = force_decl_die (context);
15059
15060   return context_die;
15061 }
15062
15063 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15064    type) within its namespace, if appropriate.
15065
15066    For compatibility with older debuggers, namespace DIEs only contain
15067    declarations; all definitions are emitted at CU scope.  */
15068
15069 static dw_die_ref
15070 declare_in_namespace (tree thing, dw_die_ref context_die)
15071 {
15072   dw_die_ref ns_context;
15073
15074   if (debug_info_level <= DINFO_LEVEL_TERSE)
15075     return context_die;
15076
15077   /* If this decl is from an inlined function, then don't try to emit it in its
15078      namespace, as we will get confused.  It would have already been emitted
15079      when the abstract instance of the inline function was emitted anyways.  */
15080   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15081     return context_die;
15082
15083   ns_context = setup_namespace_context (thing, context_die);
15084
15085   if (ns_context != context_die)
15086     {
15087       if (is_fortran ())
15088         return ns_context;
15089       if (DECL_P (thing))
15090         gen_decl_die (thing, ns_context);
15091       else
15092         gen_type_die (thing, ns_context);
15093     }
15094   return context_die;
15095 }
15096
15097 /* Generate a DIE for a namespace or namespace alias.  */
15098
15099 static void
15100 gen_namespace_die (tree decl)
15101 {
15102   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
15103
15104   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15105      they are an alias of.  */
15106   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15107     {
15108       /* Output a real namespace or module.  */
15109       dw_die_ref namespace_die
15110         = new_die (is_fortran () ? DW_TAG_module : DW_TAG_namespace,
15111                    context_die, decl);
15112       /* For Fortran modules defined in different CU don't add src coords.  */
15113       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15114         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15115       else
15116         add_name_and_src_coords_attributes (namespace_die, decl);
15117       if (DECL_EXTERNAL (decl))
15118         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15119       equate_decl_number_to_die (decl, namespace_die);
15120     }
15121   else
15122     {
15123       /* Output a namespace alias.  */
15124
15125       /* Force out the namespace we are an alias of, if necessary.  */
15126       dw_die_ref origin_die
15127         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15128
15129       /* Now create the namespace alias DIE.  */
15130       dw_die_ref namespace_die
15131         = new_die (DW_TAG_imported_declaration, context_die, decl);
15132       add_name_and_src_coords_attributes (namespace_die, decl);
15133       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15134       equate_decl_number_to_die (decl, namespace_die);
15135     }
15136 }
15137
15138 /* Generate Dwarf debug information for a decl described by DECL.  */
15139
15140 static void
15141 gen_decl_die (tree decl, dw_die_ref context_die)
15142 {
15143   tree origin;
15144
15145   if (DECL_P (decl) && DECL_IGNORED_P (decl))
15146     return;
15147
15148   switch (TREE_CODE (decl))
15149     {
15150     case ERROR_MARK:
15151       break;
15152
15153     case CONST_DECL:
15154       if (!is_fortran ())
15155         {
15156           /* The individual enumerators of an enum type get output when we output
15157              the Dwarf representation of the relevant enum type itself.  */
15158           break;
15159         }
15160
15161       /* Emit its type.  */
15162       gen_type_die (TREE_TYPE (decl), context_die);
15163
15164       /* And its containing namespace.  */
15165       context_die = declare_in_namespace (decl, context_die);
15166
15167       gen_const_die (decl, context_die);
15168       break;
15169
15170     case FUNCTION_DECL:
15171       /* Don't output any DIEs to represent mere function declarations,
15172          unless they are class members or explicit block externs.  */
15173       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
15174           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
15175         break;
15176
15177 #if 0
15178       /* FIXME */
15179       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15180          on local redeclarations of global functions.  That seems broken.  */
15181       if (current_function_decl != decl)
15182         /* This is only a declaration.  */;
15183 #endif
15184
15185       /* If we're emitting a clone, emit info for the abstract instance.  */
15186       if (DECL_ORIGIN (decl) != decl)
15187         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
15188
15189       /* If we're emitting an out-of-line copy of an inline function,
15190          emit info for the abstract instance and set up to refer to it.  */
15191       else if (cgraph_function_possibly_inlined_p (decl)
15192                && ! DECL_ABSTRACT (decl)
15193                && ! class_or_namespace_scope_p (context_die)
15194                /* dwarf2out_abstract_function won't emit a die if this is just
15195                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15196                   that case, because that works only if we have a die.  */
15197                && DECL_INITIAL (decl) != NULL_TREE)
15198         {
15199           dwarf2out_abstract_function (decl);
15200           set_decl_origin_self (decl);
15201         }
15202
15203       /* Otherwise we're emitting the primary DIE for this decl.  */
15204       else if (debug_info_level > DINFO_LEVEL_TERSE)
15205         {
15206           /* Before we describe the FUNCTION_DECL itself, make sure that we
15207              have described its return type.  */
15208           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15209
15210           /* And its virtual context.  */
15211           if (DECL_VINDEX (decl) != NULL_TREE)
15212             gen_type_die (DECL_CONTEXT (decl), context_die);
15213
15214           /* And its containing type.  */
15215           origin = decl_class_context (decl);
15216           if (origin != NULL_TREE)
15217             gen_type_die_for_member (origin, decl, context_die);
15218
15219           /* And its containing namespace.  */
15220           context_die = declare_in_namespace (decl, context_die);
15221         }
15222
15223       /* Now output a DIE to represent the function itself.  */
15224       gen_subprogram_die (decl, context_die);
15225       break;
15226
15227     case TYPE_DECL:
15228       /* If we are in terse mode, don't generate any DIEs to represent any
15229          actual typedefs.  */
15230       if (debug_info_level <= DINFO_LEVEL_TERSE)
15231         break;
15232
15233       /* In the special case of a TYPE_DECL node representing the declaration
15234          of some type tag, if the given TYPE_DECL is marked as having been
15235          instantiated from some other (original) TYPE_DECL node (e.g. one which
15236          was generated within the original definition of an inline function) we
15237          have to generate a special (abbreviated) DW_TAG_structure_type,
15238          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
15239       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
15240           && is_tagged_type (TREE_TYPE (decl)))
15241         {
15242           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
15243           break;
15244         }
15245
15246       if (is_redundant_typedef (decl))
15247         gen_type_die (TREE_TYPE (decl), context_die);
15248       else
15249         /* Output a DIE to represent the typedef itself.  */
15250         gen_typedef_die (decl, context_die);
15251       break;
15252
15253     case LABEL_DECL:
15254       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15255         gen_label_die (decl, context_die);
15256       break;
15257
15258     case VAR_DECL:
15259     case RESULT_DECL:
15260       /* If we are in terse mode, don't generate any DIEs to represent any
15261          variable declarations or definitions.  */
15262       if (debug_info_level <= DINFO_LEVEL_TERSE)
15263         break;
15264
15265       /* Output any DIEs that are needed to specify the type of this data
15266          object.  */
15267       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
15268         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15269       else
15270         gen_type_die (TREE_TYPE (decl), context_die);
15271
15272       /* And its containing type.  */
15273       origin = decl_class_context (decl);
15274       if (origin != NULL_TREE)
15275         gen_type_die_for_member (origin, decl, context_die);
15276
15277       /* And its containing namespace.  */
15278       context_die = declare_in_namespace (decl, context_die);
15279
15280       /* Now output the DIE to represent the data object itself.  This gets
15281          complicated because of the possibility that the VAR_DECL really
15282          represents an inlined instance of a formal parameter for an inline
15283          function.  */
15284       origin = decl_ultimate_origin (decl);
15285       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15286         gen_formal_parameter_die (decl, context_die);
15287       else
15288         gen_variable_die (decl, context_die);
15289       break;
15290
15291     case FIELD_DECL:
15292       /* Ignore the nameless fields that are used to skip bits but handle C++
15293          anonymous unions and structs.  */
15294       if (DECL_NAME (decl) != NULL_TREE
15295           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15296           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15297         {
15298           gen_type_die (member_declared_type (decl), context_die);
15299           gen_field_die (decl, context_die);
15300         }
15301       break;
15302
15303     case PARM_DECL:
15304       if (DECL_BY_REFERENCE (decl))
15305         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15306       else
15307         gen_type_die (TREE_TYPE (decl), context_die);
15308       gen_formal_parameter_die (decl, context_die);
15309       break;
15310
15311     case NAMESPACE_DECL:
15312       gen_namespace_die (decl);
15313       break;
15314
15315     default:
15316       /* Probably some frontend-internal decl.  Assume we don't care.  */
15317       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15318       break;
15319     }
15320 }
15321 \f
15322 /* Output debug information for global decl DECL.  Called from toplev.c after
15323    compilation proper has finished.  */
15324
15325 static void
15326 dwarf2out_global_decl (tree decl)
15327 {
15328   /* Output DWARF2 information for file-scope tentative data object
15329      declarations, file-scope (extern) function declarations (which
15330      had no corresponding body) and file-scope tagged type declarations
15331      and definitions which have not yet been forced out.  */
15332   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15333     dwarf2out_decl (decl);
15334 }
15335
15336 /* Output debug information for type decl DECL.  Called from toplev.c
15337    and from language front ends (to record built-in types).  */
15338 static void
15339 dwarf2out_type_decl (tree decl, int local)
15340 {
15341   if (!local)
15342     dwarf2out_decl (decl);
15343 }
15344
15345 /* Output debug information for imported module or decl DECL.
15346    NAME is non-NULL name in context if the decl has been renamed.
15347    CHILD is true if decl is one of the renamed decls as part of
15348    importing whole module.  */
15349
15350 static void
15351 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15352                                    bool child)
15353 {
15354   dw_die_ref imported_die, at_import_die;
15355   dw_die_ref scope_die;
15356   expanded_location xloc;
15357
15358   if (debug_info_level <= DINFO_LEVEL_TERSE)
15359     return;
15360
15361   gcc_assert (decl);
15362
15363   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15364      We need decl DIE for reference and scope die. First, get DIE for the decl
15365      itself.  */
15366
15367   /* Get the scope die for decl context. Use comp_unit_die for global module
15368      or decl. If die is not found for non globals, force new die.  */
15369   if (context
15370       && TYPE_P (context)
15371       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15372     return;
15373   scope_die = get_context_die (context);
15374
15375   if (child)
15376     {
15377       gcc_assert (scope_die->die_child);
15378       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15379       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15380       scope_die = scope_die->die_child;
15381     }
15382
15383   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
15384   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15385     {
15386       if (is_base_type (TREE_TYPE (decl)))
15387         at_import_die = base_type_die (TREE_TYPE (decl));
15388       else
15389         at_import_die = force_type_die (TREE_TYPE (decl));
15390       /* For namespace N { typedef void T; } using N::T; base_type_die
15391          returns NULL, but DW_TAG_imported_declaration requires
15392          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15393       if (!at_import_die)
15394         {
15395           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15396           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15397           at_import_die = lookup_type_die (TREE_TYPE (decl));
15398           gcc_assert (at_import_die);
15399         }
15400     }
15401   else
15402     {
15403       at_import_die = lookup_decl_die (decl);
15404       if (!at_import_die)
15405         {
15406           /* If we're trying to avoid duplicate debug info, we may not have
15407              emitted the member decl for this field.  Emit it now.  */
15408           if (TREE_CODE (decl) == FIELD_DECL)
15409             {
15410               tree type = DECL_CONTEXT (decl);
15411
15412               if (TYPE_CONTEXT (type)
15413                   && TYPE_P (TYPE_CONTEXT (type))
15414                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15415                                                 DINFO_USAGE_DIR_USE))
15416                 return;
15417               gen_type_die_for_member (type, decl,
15418                                        get_context_die (TYPE_CONTEXT (type)));
15419             }
15420           at_import_die = force_decl_die (decl);
15421         }
15422     }
15423
15424   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15425   if (TREE_CODE (decl) == NAMESPACE_DECL)
15426     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
15427   else
15428     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
15429
15430   xloc = expand_location (input_location);
15431   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15432   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15433   if (name)
15434     add_AT_string (imported_die, DW_AT_name, IDENTIFIER_POINTER (name));
15435   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15436 }
15437
15438 /* Write the debugging output for DECL.  */
15439
15440 void
15441 dwarf2out_decl (tree decl)
15442 {
15443   dw_die_ref context_die = comp_unit_die;
15444
15445   switch (TREE_CODE (decl))
15446     {
15447     case ERROR_MARK:
15448       return;
15449
15450     case FUNCTION_DECL:
15451       /* What we would really like to do here is to filter out all mere
15452          file-scope declarations of file-scope functions which are never
15453          referenced later within this translation unit (and keep all of ones
15454          that *are* referenced later on) but we aren't clairvoyant, so we have
15455          no idea which functions will be referenced in the future (i.e. later
15456          on within the current translation unit). So here we just ignore all
15457          file-scope function declarations which are not also definitions.  If
15458          and when the debugger needs to know something about these functions,
15459          it will have to hunt around and find the DWARF information associated
15460          with the definition of the function.
15461
15462          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15463          nodes represent definitions and which ones represent mere
15464          declarations.  We have to check DECL_INITIAL instead. That's because
15465          the C front-end supports some weird semantics for "extern inline"
15466          function definitions.  These can get inlined within the current
15467          translation unit (and thus, we need to generate Dwarf info for their
15468          abstract instances so that the Dwarf info for the concrete inlined
15469          instances can have something to refer to) but the compiler never
15470          generates any out-of-lines instances of such things (despite the fact
15471          that they *are* definitions).
15472
15473          The important point is that the C front-end marks these "extern
15474          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15475          them anyway. Note that the C++ front-end also plays some similar games
15476          for inline function definitions appearing within include files which
15477          also contain `#pragma interface' pragmas.  */
15478       if (DECL_INITIAL (decl) == NULL_TREE)
15479         return;
15480
15481       /* If we're a nested function, initially use a parent of NULL; if we're
15482          a plain function, this will be fixed up in decls_for_scope.  If
15483          we're a method, it will be ignored, since we already have a DIE.  */
15484       if (decl_function_context (decl)
15485           /* But if we're in terse mode, we don't care about scope.  */
15486           && debug_info_level > DINFO_LEVEL_TERSE)
15487         context_die = NULL;
15488       break;
15489
15490     case VAR_DECL:
15491       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15492          declaration and if the declaration was never even referenced from
15493          within this entire compilation unit.  We suppress these DIEs in
15494          order to save space in the .debug section (by eliminating entries
15495          which are probably useless).  Note that we must not suppress
15496          block-local extern declarations (whether used or not) because that
15497          would screw-up the debugger's name lookup mechanism and cause it to
15498          miss things which really ought to be in scope at a given point.  */
15499       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15500         return;
15501
15502       /* For local statics lookup proper context die.  */
15503       if (TREE_STATIC (decl) && decl_function_context (decl))
15504         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15505
15506       /* If we are in terse mode, don't generate any DIEs to represent any
15507          variable declarations or definitions.  */
15508       if (debug_info_level <= DINFO_LEVEL_TERSE)
15509         return;
15510       break;
15511
15512     case CONST_DECL:
15513       if (debug_info_level <= DINFO_LEVEL_TERSE)
15514         return;
15515       if (!is_fortran ())
15516         return;
15517       if (TREE_STATIC (decl) && decl_function_context (decl))
15518         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15519       break;
15520
15521     case NAMESPACE_DECL:
15522       if (debug_info_level <= DINFO_LEVEL_TERSE)
15523         return;
15524       if (lookup_decl_die (decl) != NULL)
15525         return;
15526       break;
15527
15528     case TYPE_DECL:
15529       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15530       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15531         return;
15532
15533       /* Don't bother trying to generate any DIEs to represent any of the
15534          normal built-in types for the language we are compiling.  */
15535       if (DECL_IS_BUILTIN (decl))
15536         {
15537           /* OK, we need to generate one for `bool' so GDB knows what type
15538              comparisons have.  */
15539           if (is_cxx ()
15540               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15541               && ! DECL_IGNORED_P (decl))
15542             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15543
15544           return;
15545         }
15546
15547       /* If we are in terse mode, don't generate any DIEs for types.  */
15548       if (debug_info_level <= DINFO_LEVEL_TERSE)
15549         return;
15550
15551       /* If we're a function-scope tag, initially use a parent of NULL;
15552          this will be fixed up in decls_for_scope.  */
15553       if (decl_function_context (decl))
15554         context_die = NULL;
15555
15556       break;
15557
15558     default:
15559       return;
15560     }
15561
15562   gen_decl_die (decl, context_die);
15563 }
15564
15565 /* Output a marker (i.e. a label) for the beginning of the generated code for
15566    a lexical block.  */
15567
15568 static void
15569 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15570                        unsigned int blocknum)
15571 {
15572   switch_to_section (current_function_section ());
15573   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15574 }
15575
15576 /* Output a marker (i.e. a label) for the end of the generated code for a
15577    lexical block.  */
15578
15579 static void
15580 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15581 {
15582   switch_to_section (current_function_section ());
15583   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15584 }
15585
15586 /* Returns nonzero if it is appropriate not to emit any debugging
15587    information for BLOCK, because it doesn't contain any instructions.
15588
15589    Don't allow this for blocks with nested functions or local classes
15590    as we would end up with orphans, and in the presence of scheduling
15591    we may end up calling them anyway.  */
15592
15593 static bool
15594 dwarf2out_ignore_block (const_tree block)
15595 {
15596   tree decl;
15597
15598   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15599     if (TREE_CODE (decl) == FUNCTION_DECL
15600         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15601       return 0;
15602
15603   return 1;
15604 }
15605
15606 /* Hash table routines for file_hash.  */
15607
15608 static int
15609 file_table_eq (const void *p1_p, const void *p2_p)
15610 {
15611   const struct dwarf_file_data *const p1 =
15612     (const struct dwarf_file_data *) p1_p;
15613   const char *const p2 = (const char *) p2_p;
15614   return strcmp (p1->filename, p2) == 0;
15615 }
15616
15617 static hashval_t
15618 file_table_hash (const void *p_p)
15619 {
15620   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15621   return htab_hash_string (p->filename);
15622 }
15623
15624 /* Lookup FILE_NAME (in the list of filenames that we know about here in
15625    dwarf2out.c) and return its "index".  The index of each (known) filename is
15626    just a unique number which is associated with only that one filename.  We
15627    need such numbers for the sake of generating labels (in the .debug_sfnames
15628    section) and references to those files numbers (in the .debug_srcinfo
15629    and.debug_macinfo sections).  If the filename given as an argument is not
15630    found in our current list, add it to the list and assign it the next
15631    available unique index number.  In order to speed up searches, we remember
15632    the index of the filename was looked up last.  This handles the majority of
15633    all searches.  */
15634
15635 static struct dwarf_file_data *
15636 lookup_filename (const char *file_name)
15637 {
15638   void ** slot;
15639   struct dwarf_file_data * created;
15640
15641   /* Check to see if the file name that was searched on the previous
15642      call matches this file name.  If so, return the index.  */
15643   if (file_table_last_lookup
15644       && (file_name == file_table_last_lookup->filename
15645           || strcmp (file_table_last_lookup->filename, file_name) == 0))
15646     return file_table_last_lookup;
15647
15648   /* Didn't match the previous lookup, search the table.  */
15649   slot = htab_find_slot_with_hash (file_table, file_name,
15650                                    htab_hash_string (file_name), INSERT);
15651   if (*slot)
15652     return (struct dwarf_file_data *) *slot;
15653
15654   created = GGC_NEW (struct dwarf_file_data);
15655   created->filename = file_name;
15656   created->emitted_number = 0;
15657   *slot = created;
15658   return created;
15659 }
15660
15661 /* If the assembler will construct the file table, then translate the compiler
15662    internal file table number into the assembler file table number, and emit
15663    a .file directive if we haven't already emitted one yet.  The file table
15664    numbers are different because we prune debug info for unused variables and
15665    types, which may include filenames.  */
15666
15667 static int
15668 maybe_emit_file (struct dwarf_file_data * fd)
15669 {
15670   if (! fd->emitted_number)
15671     {
15672       if (last_emitted_file)
15673         fd->emitted_number = last_emitted_file->emitted_number + 1;
15674       else
15675         fd->emitted_number = 1;
15676       last_emitted_file = fd;
15677
15678       if (DWARF2_ASM_LINE_DEBUG_INFO)
15679         {
15680           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
15681           output_quoted_string (asm_out_file,
15682                                 remap_debug_filename (fd->filename));
15683           fputc ('\n', asm_out_file);
15684         }
15685     }
15686
15687   return fd->emitted_number;
15688 }
15689
15690 /* Called by the final INSN scan whenever we see a var location.  We
15691    use it to drop labels in the right places, and throw the location in
15692    our lookup table.  */
15693
15694 static void
15695 dwarf2out_var_location (rtx loc_note)
15696 {
15697   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
15698   struct var_loc_node *newloc;
15699   rtx prev_insn;
15700   static rtx last_insn;
15701   static const char *last_label;
15702   tree decl;
15703
15704   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
15705     return;
15706   prev_insn = PREV_INSN (loc_note);
15707
15708   newloc = GGC_CNEW (struct var_loc_node);
15709   /* If the insn we processed last time is the previous insn
15710      and it is also a var location note, use the label we emitted
15711      last time.  */
15712   if (last_insn != NULL_RTX
15713       && last_insn == prev_insn
15714       && NOTE_P (prev_insn)
15715       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
15716     {
15717       newloc->label = last_label;
15718     }
15719   else
15720     {
15721       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
15722       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
15723       loclabel_num++;
15724       newloc->label = ggc_strdup (loclabel);
15725     }
15726   newloc->var_loc_note = loc_note;
15727   newloc->next = NULL;
15728
15729   if (cfun && in_cold_section_p)
15730     newloc->section_label = crtl->subsections.cold_section_label;
15731   else
15732     newloc->section_label = text_section_label;
15733
15734   last_insn = loc_note;
15735   last_label = newloc->label;
15736   decl = NOTE_VAR_LOCATION_DECL (loc_note);
15737   add_var_loc_to_decl (decl, newloc);
15738 }
15739
15740 /* We need to reset the locations at the beginning of each
15741    function. We can't do this in the end_function hook, because the
15742    declarations that use the locations won't have been output when
15743    that hook is called.  Also compute have_multiple_function_sections here.  */
15744
15745 static void
15746 dwarf2out_begin_function (tree fun)
15747 {
15748   htab_empty (decl_loc_table);
15749
15750   if (function_section (fun) != text_section)
15751     have_multiple_function_sections = true;
15752
15753   dwarf2out_note_section_used ();
15754 }
15755
15756 /* Output a label to mark the beginning of a source code line entry
15757    and record information relating to this source line, in
15758    'line_info_table' for later output of the .debug_line section.  */
15759
15760 static void
15761 dwarf2out_source_line (unsigned int line, const char *filename)
15762 {
15763   if (debug_info_level >= DINFO_LEVEL_NORMAL
15764       && line != 0)
15765     {
15766       int file_num = maybe_emit_file (lookup_filename (filename));
15767
15768       switch_to_section (current_function_section ());
15769
15770       /* If requested, emit something human-readable.  */
15771       if (flag_debug_asm)
15772         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
15773                  filename, line);
15774
15775       if (DWARF2_ASM_LINE_DEBUG_INFO)
15776         {
15777           /* Emit the .loc directive understood by GNU as.  */
15778           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
15779
15780           /* Indicate that line number info exists.  */
15781           line_info_table_in_use++;
15782         }
15783       else if (function_section (current_function_decl) != text_section)
15784         {
15785           dw_separate_line_info_ref line_info;
15786           targetm.asm_out.internal_label (asm_out_file,
15787                                           SEPARATE_LINE_CODE_LABEL,
15788                                           separate_line_info_table_in_use);
15789
15790           /* Expand the line info table if necessary.  */
15791           if (separate_line_info_table_in_use
15792               == separate_line_info_table_allocated)
15793             {
15794               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15795               separate_line_info_table
15796                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
15797                                  separate_line_info_table,
15798                                  separate_line_info_table_allocated);
15799               memset (separate_line_info_table
15800                        + separate_line_info_table_in_use,
15801                       0,
15802                       (LINE_INFO_TABLE_INCREMENT
15803                        * sizeof (dw_separate_line_info_entry)));
15804             }
15805
15806           /* Add the new entry at the end of the line_info_table.  */
15807           line_info
15808             = &separate_line_info_table[separate_line_info_table_in_use++];
15809           line_info->dw_file_num = file_num;
15810           line_info->dw_line_num = line;
15811           line_info->function = current_function_funcdef_no;
15812         }
15813       else
15814         {
15815           dw_line_info_ref line_info;
15816
15817           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
15818                                      line_info_table_in_use);
15819
15820           /* Expand the line info table if necessary.  */
15821           if (line_info_table_in_use == line_info_table_allocated)
15822             {
15823               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
15824               line_info_table
15825                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
15826                                  line_info_table_allocated);
15827               memset (line_info_table + line_info_table_in_use, 0,
15828                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
15829             }
15830
15831           /* Add the new entry at the end of the line_info_table.  */
15832           line_info = &line_info_table[line_info_table_in_use++];
15833           line_info->dw_file_num = file_num;
15834           line_info->dw_line_num = line;
15835         }
15836     }
15837 }
15838
15839 /* Record the beginning of a new source file.  */
15840
15841 static void
15842 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
15843 {
15844   if (flag_eliminate_dwarf2_dups)
15845     {
15846       /* Record the beginning of the file for break_out_includes.  */
15847       dw_die_ref bincl_die;
15848
15849       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
15850       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
15851     }
15852
15853   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15854     {
15855       int file_num = maybe_emit_file (lookup_filename (filename));
15856
15857       switch_to_section (debug_macinfo_section);
15858       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
15859       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
15860                                    lineno);
15861
15862       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
15863     }
15864 }
15865
15866 /* Record the end of a source file.  */
15867
15868 static void
15869 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
15870 {
15871   if (flag_eliminate_dwarf2_dups)
15872     /* Record the end of the file for break_out_includes.  */
15873     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
15874
15875   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15876     {
15877       switch_to_section (debug_macinfo_section);
15878       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
15879     }
15880 }
15881
15882 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
15883    the tail part of the directive line, i.e. the part which is past the
15884    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15885
15886 static void
15887 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
15888                   const char *buffer ATTRIBUTE_UNUSED)
15889 {
15890   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15891     {
15892       switch_to_section (debug_macinfo_section);
15893       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
15894       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15895       dw2_asm_output_nstring (buffer, -1, "The macro");
15896     }
15897 }
15898
15899 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
15900    the tail part of the directive line, i.e. the part which is past the
15901    initial whitespace, #, whitespace, directive-name, whitespace part.  */
15902
15903 static void
15904 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
15905                  const char *buffer ATTRIBUTE_UNUSED)
15906 {
15907   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15908     {
15909       switch_to_section (debug_macinfo_section);
15910       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
15911       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
15912       dw2_asm_output_nstring (buffer, -1, "The macro");
15913     }
15914 }
15915
15916 /* Set up for Dwarf output at the start of compilation.  */
15917
15918 static void
15919 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
15920 {
15921   /* Allocate the file_table.  */
15922   file_table = htab_create_ggc (50, file_table_hash,
15923                                 file_table_eq, NULL);
15924
15925   /* Allocate the decl_die_table.  */
15926   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
15927                                     decl_die_table_eq, NULL);
15928
15929   /* Allocate the decl_loc_table.  */
15930   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
15931                                     decl_loc_table_eq, NULL);
15932
15933   /* Allocate the initial hunk of the decl_scope_table.  */
15934   decl_scope_table = VEC_alloc (tree, gc, 256);
15935
15936   /* Allocate the initial hunk of the abbrev_die_table.  */
15937   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
15938   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
15939   /* Zero-th entry is allocated, but unused.  */
15940   abbrev_die_table_in_use = 1;
15941
15942   /* Allocate the initial hunk of the line_info_table.  */
15943   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
15944   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
15945
15946   /* Zero-th entry is allocated, but unused.  */
15947   line_info_table_in_use = 1;
15948
15949   /* Allocate the pubtypes and pubnames vectors.  */
15950   pubname_table = VEC_alloc (pubname_entry, gc, 32);
15951   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
15952
15953   /* Generate the initial DIE for the .debug section.  Note that the (string)
15954      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
15955      will (typically) be a relative pathname and that this pathname should be
15956      taken as being relative to the directory from which the compiler was
15957      invoked when the given (base) source file was compiled.  We will fill
15958      in this value in dwarf2out_finish.  */
15959   comp_unit_die = gen_compile_unit_die (NULL);
15960
15961   incomplete_types = VEC_alloc (tree, gc, 64);
15962
15963   used_rtx_array = VEC_alloc (rtx, gc, 32);
15964
15965   debug_info_section = get_section (DEBUG_INFO_SECTION,
15966                                     SECTION_DEBUG, NULL);
15967   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
15968                                       SECTION_DEBUG, NULL);
15969   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
15970                                        SECTION_DEBUG, NULL);
15971   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
15972                                        SECTION_DEBUG, NULL);
15973   debug_line_section = get_section (DEBUG_LINE_SECTION,
15974                                     SECTION_DEBUG, NULL);
15975   debug_loc_section = get_section (DEBUG_LOC_SECTION,
15976                                    SECTION_DEBUG, NULL);
15977   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
15978                                         SECTION_DEBUG, NULL);
15979 #ifdef DEBUG_PUBTYPES_SECTION
15980   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
15981                                         SECTION_DEBUG, NULL);
15982 #endif
15983   debug_str_section = get_section (DEBUG_STR_SECTION,
15984                                    DEBUG_STR_SECTION_FLAGS, NULL);
15985   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
15986                                       SECTION_DEBUG, NULL);
15987   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
15988                                      SECTION_DEBUG, NULL);
15989
15990   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
15991   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
15992                                DEBUG_ABBREV_SECTION_LABEL, 0);
15993   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
15994   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
15995                                COLD_TEXT_SECTION_LABEL, 0);
15996   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
15997
15998   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
15999                                DEBUG_INFO_SECTION_LABEL, 0);
16000   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16001                                DEBUG_LINE_SECTION_LABEL, 0);
16002   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16003                                DEBUG_RANGES_SECTION_LABEL, 0);
16004   switch_to_section (debug_abbrev_section);
16005   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16006   switch_to_section (debug_info_section);
16007   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16008   switch_to_section (debug_line_section);
16009   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16010
16011   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16012     {
16013       switch_to_section (debug_macinfo_section);
16014       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16015                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16016       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16017     }
16018
16019   switch_to_section (text_section);
16020   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16021   if (flag_reorder_blocks_and_partition)
16022     {
16023       cold_text_section = unlikely_text_section ();
16024       switch_to_section (cold_text_section);
16025       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16026     }
16027 }
16028
16029 /* A helper function for dwarf2out_finish called through
16030    ht_forall.  Emit one queued .debug_str string.  */
16031
16032 static int
16033 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16034 {
16035   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16036
16037   if (node->form == DW_FORM_strp)
16038     {
16039       switch_to_section (debug_str_section);
16040       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16041       assemble_string (node->str, strlen (node->str) + 1);
16042     }
16043
16044   return 1;
16045 }
16046
16047 #if ENABLE_ASSERT_CHECKING
16048 /* Verify that all marks are clear.  */
16049
16050 static void
16051 verify_marks_clear (dw_die_ref die)
16052 {
16053   dw_die_ref c;
16054
16055   gcc_assert (! die->die_mark);
16056   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16057 }
16058 #endif /* ENABLE_ASSERT_CHECKING */
16059
16060 /* Clear the marks for a die and its children.
16061    Be cool if the mark isn't set.  */
16062
16063 static void
16064 prune_unmark_dies (dw_die_ref die)
16065 {
16066   dw_die_ref c;
16067
16068   if (die->die_mark)
16069     die->die_mark = 0;
16070   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16071 }
16072
16073 /* Given DIE that we're marking as used, find any other dies
16074    it references as attributes and mark them as used.  */
16075
16076 static void
16077 prune_unused_types_walk_attribs (dw_die_ref die)
16078 {
16079   dw_attr_ref a;
16080   unsigned ix;
16081
16082   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16083     {
16084       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16085         {
16086           /* A reference to another DIE.
16087              Make sure that it will get emitted.  */
16088           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16089         }
16090       /* Set the string's refcount to 0 so that prune_unused_types_mark
16091          accounts properly for it.  */
16092       if (AT_class (a) == dw_val_class_str)
16093         a->dw_attr_val.v.val_str->refcount = 0;
16094     }
16095 }
16096
16097
16098 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16099    to DIE's children.  */
16100
16101 static void
16102 prune_unused_types_mark (dw_die_ref die, int dokids)
16103 {
16104   dw_die_ref c;
16105
16106   if (die->die_mark == 0)
16107     {
16108       /* We haven't done this node yet.  Mark it as used.  */
16109       die->die_mark = 1;
16110
16111       /* We also have to mark its parents as used.
16112          (But we don't want to mark our parents' kids due to this.)  */
16113       if (die->die_parent)
16114         prune_unused_types_mark (die->die_parent, 0);
16115
16116       /* Mark any referenced nodes.  */
16117       prune_unused_types_walk_attribs (die);
16118
16119       /* If this node is a specification,
16120          also mark the definition, if it exists.  */
16121       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16122         prune_unused_types_mark (die->die_definition, 1);
16123     }
16124
16125   if (dokids && die->die_mark != 2)
16126     {
16127       /* We need to walk the children, but haven't done so yet.
16128          Remember that we've walked the kids.  */
16129       die->die_mark = 2;
16130
16131       /* If this is an array type, we need to make sure our
16132          kids get marked, even if they're types.  */
16133       if (die->die_tag == DW_TAG_array_type)
16134         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16135       else
16136         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16137     }
16138 }
16139
16140
16141 /* Walk the tree DIE and mark types that we actually use.  */
16142
16143 static void
16144 prune_unused_types_walk (dw_die_ref die)
16145 {
16146   dw_die_ref c;
16147
16148   /* Don't do anything if this node is already marked.  */
16149   if (die->die_mark)
16150     return;
16151
16152   switch (die->die_tag)
16153     {
16154     case DW_TAG_const_type:
16155     case DW_TAG_packed_type:
16156     case DW_TAG_pointer_type:
16157     case DW_TAG_reference_type:
16158     case DW_TAG_volatile_type:
16159     case DW_TAG_typedef:
16160     case DW_TAG_array_type:
16161     case DW_TAG_structure_type:
16162     case DW_TAG_union_type:
16163     case DW_TAG_class_type:
16164     case DW_TAG_interface_type:
16165     case DW_TAG_friend:
16166     case DW_TAG_variant_part:
16167     case DW_TAG_enumeration_type:
16168     case DW_TAG_subroutine_type:
16169     case DW_TAG_string_type:
16170     case DW_TAG_set_type:
16171     case DW_TAG_subrange_type:
16172     case DW_TAG_ptr_to_member_type:
16173     case DW_TAG_file_type:
16174       if (die->die_perennial_p)
16175         break;
16176
16177       /* It's a type node --- don't mark it.  */
16178       return;
16179
16180     default:
16181       /* Mark everything else.  */
16182       break;
16183   }
16184
16185   die->die_mark = 1;
16186
16187   /* Now, mark any dies referenced from here.  */
16188   prune_unused_types_walk_attribs (die);
16189
16190   /* Mark children.  */
16191   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16192 }
16193
16194 /* Increment the string counts on strings referred to from DIE's
16195    attributes.  */
16196
16197 static void
16198 prune_unused_types_update_strings (dw_die_ref die)
16199 {
16200   dw_attr_ref a;
16201   unsigned ix;
16202
16203   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16204     if (AT_class (a) == dw_val_class_str)
16205       {
16206         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16207         s->refcount++;
16208         /* Avoid unnecessarily putting strings that are used less than
16209            twice in the hash table.  */
16210         if (s->refcount
16211             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16212           {
16213             void ** slot;
16214             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16215                                              htab_hash_string (s->str),
16216                                              INSERT);
16217             gcc_assert (*slot == NULL);
16218             *slot = s;
16219           }
16220       }
16221 }
16222
16223 /* Remove from the tree DIE any dies that aren't marked.  */
16224
16225 static void
16226 prune_unused_types_prune (dw_die_ref die)
16227 {
16228   dw_die_ref c;
16229
16230   gcc_assert (die->die_mark);
16231   prune_unused_types_update_strings (die);
16232
16233   if (! die->die_child)
16234     return;
16235
16236   c = die->die_child;
16237   do {
16238     dw_die_ref prev = c;
16239     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16240       if (c == die->die_child)
16241         {
16242           /* No marked children between 'prev' and the end of the list.  */
16243           if (prev == c)
16244             /* No marked children at all.  */
16245             die->die_child = NULL;
16246           else
16247             {
16248               prev->die_sib = c->die_sib;
16249               die->die_child = prev;
16250             }
16251           return;
16252         }
16253
16254     if (c != prev->die_sib)
16255       prev->die_sib = c;
16256     prune_unused_types_prune (c);
16257   } while (c != die->die_child);
16258 }
16259
16260
16261 /* Remove dies representing declarations that we never use.  */
16262
16263 static void
16264 prune_unused_types (void)
16265 {
16266   unsigned int i;
16267   limbo_die_node *node;
16268   pubname_ref pub;
16269
16270 #if ENABLE_ASSERT_CHECKING
16271   /* All the marks should already be clear.  */
16272   verify_marks_clear (comp_unit_die);
16273   for (node = limbo_die_list; node; node = node->next)
16274     verify_marks_clear (node->die);
16275 #endif /* ENABLE_ASSERT_CHECKING */
16276
16277   /* Set the mark on nodes that are actually used.  */
16278   prune_unused_types_walk (comp_unit_die);
16279   for (node = limbo_die_list; node; node = node->next)
16280     prune_unused_types_walk (node->die);
16281
16282   /* Also set the mark on nodes referenced from the
16283      pubname_table or arange_table.  */
16284   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16285     prune_unused_types_mark (pub->die, 1);
16286   for (i = 0; i < arange_table_in_use; i++)
16287     prune_unused_types_mark (arange_table[i], 1);
16288
16289   /* Get rid of nodes that aren't marked; and update the string counts.  */
16290   if (debug_str_hash)
16291     htab_empty (debug_str_hash);
16292   prune_unused_types_prune (comp_unit_die);
16293   for (node = limbo_die_list; node; node = node->next)
16294     prune_unused_types_prune (node->die);
16295
16296   /* Leave the marks clear.  */
16297   prune_unmark_dies (comp_unit_die);
16298   for (node = limbo_die_list; node; node = node->next)
16299     prune_unmark_dies (node->die);
16300 }
16301
16302 /* Set the parameter to true if there are any relative pathnames in
16303    the file table.  */
16304 static int
16305 file_table_relative_p (void ** slot, void *param)
16306 {
16307   bool *p = (bool *) param;
16308   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16309   if (!IS_ABSOLUTE_PATH (d->filename))
16310     {
16311       *p = true;
16312       return 0;
16313     }
16314   return 1;
16315 }
16316
16317 /* Output stuff that dwarf requires at the end of every file,
16318    and generate the DWARF-2 debugging info.  */
16319
16320 static void
16321 dwarf2out_finish (const char *filename)
16322 {
16323   limbo_die_node *node, *next_node;
16324   dw_die_ref die = 0;
16325
16326   /* Add the name for the main input file now.  We delayed this from
16327      dwarf2out_init to avoid complications with PCH.  */
16328   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16329   if (!IS_ABSOLUTE_PATH (filename))
16330     add_comp_dir_attribute (comp_unit_die);
16331   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16332     {
16333       bool p = false;
16334       htab_traverse (file_table, file_table_relative_p, &p);
16335       if (p)
16336         add_comp_dir_attribute (comp_unit_die);
16337     }
16338
16339   /* Traverse the limbo die list, and add parent/child links.  The only
16340      dies without parents that should be here are concrete instances of
16341      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16342      For concrete instances, we can get the parent die from the abstract
16343      instance.  */
16344   for (node = limbo_die_list; node; node = next_node)
16345     {
16346       next_node = node->next;
16347       die = node->die;
16348
16349       if (die->die_parent == NULL)
16350         {
16351           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16352
16353           if (origin)
16354             add_child_die (origin->die_parent, die);
16355           else if (die == comp_unit_die)
16356             ;
16357           else if (errorcount > 0 || sorrycount > 0)
16358             /* It's OK to be confused by errors in the input.  */
16359             add_child_die (comp_unit_die, die);
16360           else
16361             {
16362               /* In certain situations, the lexical block containing a
16363                  nested function can be optimized away, which results
16364                  in the nested function die being orphaned.  Likewise
16365                  with the return type of that nested function.  Force
16366                  this to be a child of the containing function.
16367
16368                  It may happen that even the containing function got fully
16369                  inlined and optimized out.  In that case we are lost and
16370                  assign the empty child.  This should not be big issue as
16371                  the function is likely unreachable too.  */
16372               tree context = NULL_TREE;
16373
16374               gcc_assert (node->created_for);
16375
16376               if (DECL_P (node->created_for))
16377                 context = DECL_CONTEXT (node->created_for);
16378               else if (TYPE_P (node->created_for))
16379                 context = TYPE_CONTEXT (node->created_for);
16380
16381               gcc_assert (context
16382                           && (TREE_CODE (context) == FUNCTION_DECL
16383                               || TREE_CODE (context) == NAMESPACE_DECL));
16384
16385               origin = lookup_decl_die (context);
16386               if (origin)
16387                 add_child_die (origin, die);
16388               else
16389                 add_child_die (comp_unit_die, die);
16390             }
16391         }
16392     }
16393
16394   limbo_die_list = NULL;
16395
16396   /* Walk through the list of incomplete types again, trying once more to
16397      emit full debugging info for them.  */
16398   retry_incomplete_types ();
16399
16400   if (flag_eliminate_unused_debug_types)
16401     prune_unused_types ();
16402
16403   /* Generate separate CUs for each of the include files we've seen.
16404      They will go into limbo_die_list.  */
16405   if (flag_eliminate_dwarf2_dups)
16406     break_out_includes (comp_unit_die);
16407
16408   /* Traverse the DIE's and add add sibling attributes to those DIE's
16409      that have children.  */
16410   add_sibling_attributes (comp_unit_die);
16411   for (node = limbo_die_list; node; node = node->next)
16412     add_sibling_attributes (node->die);
16413
16414   /* Output a terminator label for the .text section.  */
16415   switch_to_section (text_section);
16416   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16417   if (flag_reorder_blocks_and_partition)
16418     {
16419       switch_to_section (unlikely_text_section ());
16420       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16421     }
16422
16423   /* We can only use the low/high_pc attributes if all of the code was
16424      in .text.  */
16425   if (!have_multiple_function_sections)
16426     {
16427       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16428       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16429     }
16430
16431   else
16432     {
16433       unsigned fde_idx = 0;
16434
16435       /* We need to give .debug_loc and .debug_ranges an appropriate
16436          "base address".  Use zero so that these addresses become
16437          absolute.  Historically, we've emitted the unexpected
16438          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16439          Emit both to give time for other tools to adapt.  */
16440       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16441       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16442
16443       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16444                          add_ranges_by_labels (text_section_label,
16445                                                text_end_label));
16446       if (flag_reorder_blocks_and_partition)
16447         add_ranges_by_labels (cold_text_section_label,
16448                               cold_end_label);
16449
16450       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16451         {
16452           dw_fde_ref fde = &fde_table[fde_idx];
16453
16454           if (fde->dw_fde_switched_sections)
16455             {
16456               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16457                                     fde->dw_fde_hot_section_end_label);
16458               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16459                                     fde->dw_fde_unlikely_section_end_label);
16460             }
16461           else
16462             add_ranges_by_labels (fde->dw_fde_begin,
16463                                   fde->dw_fde_end);
16464         }
16465
16466       add_ranges (NULL);
16467     }
16468
16469   /* Output location list section if necessary.  */
16470   if (have_location_lists)
16471     {
16472       /* Output the location lists info.  */
16473       switch_to_section (debug_loc_section);
16474       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16475                                    DEBUG_LOC_SECTION_LABEL, 0);
16476       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16477       output_location_lists (die);
16478     }
16479
16480   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16481     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16482                     debug_line_section_label);
16483
16484   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16485     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16486
16487   /* Output all of the compilation units.  We put the main one last so that
16488      the offsets are available to output_pubnames.  */
16489   for (node = limbo_die_list; node; node = node->next)
16490     output_comp_unit (node->die, 0);
16491
16492   output_comp_unit (comp_unit_die, 0);
16493
16494   /* Output the abbreviation table.  */
16495   switch_to_section (debug_abbrev_section);
16496   output_abbrev_section ();
16497
16498   /* Output public names table if necessary.  */
16499   if (!VEC_empty (pubname_entry, pubname_table))
16500     {
16501       switch_to_section (debug_pubnames_section);
16502       output_pubnames (pubname_table);
16503     }
16504
16505 #ifdef DEBUG_PUBTYPES_SECTION
16506   /* Output public types table if necessary.  */
16507   if (!VEC_empty (pubname_entry, pubtype_table))
16508     {
16509       switch_to_section (debug_pubtypes_section);
16510       output_pubnames (pubtype_table);
16511     }
16512 #endif
16513
16514   /* Output the address range information.  We only put functions in the arange
16515      table, so don't write it out if we don't have any.  */
16516   if (fde_table_in_use)
16517     {
16518       switch_to_section (debug_aranges_section);
16519       output_aranges ();
16520     }
16521
16522   /* Output ranges section if necessary.  */
16523   if (ranges_table_in_use)
16524     {
16525       switch_to_section (debug_ranges_section);
16526       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16527       output_ranges ();
16528     }
16529
16530   /* Output the source line correspondence table.  We must do this
16531      even if there is no line information.  Otherwise, on an empty
16532      translation unit, we will generate a present, but empty,
16533      .debug_info section.  IRIX 6.5 `nm' will then complain when
16534      examining the file.  This is done late so that any filenames
16535      used by the debug_info section are marked as 'used'.  */
16536   if (! DWARF2_ASM_LINE_DEBUG_INFO)
16537     {
16538       switch_to_section (debug_line_section);
16539       output_line_info ();
16540     }
16541
16542   /* Have to end the macro section.  */
16543   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16544     {
16545       switch_to_section (debug_macinfo_section);
16546       dw2_asm_output_data (1, 0, "End compilation unit");
16547     }
16548
16549   /* If we emitted any DW_FORM_strp form attribute, output the string
16550      table too.  */
16551   if (debug_str_hash)
16552     htab_traverse (debug_str_hash, output_indirect_string, NULL);
16553 }
16554 #else
16555
16556 /* This should never be used, but its address is needed for comparisons.  */
16557 const struct gcc_debug_hooks dwarf2_debug_hooks;
16558
16559 #endif /* DWARF2_DEBUGGING_INFO */
16560
16561 #include "gt-dwarf2out.h"