OSDN Git Service

861d3b3b1dee5534bb8ba4e09655b5cdeebc4a2d
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 #  define DWARF2_FRAME_INFO \
100   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 #  define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107    collected using DWARF_FRAME_REGNUM to those that should be output in
108    .debug_frame and .eh_frame.  */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Decide whether we want to emit frame unwind information for the current
114    translation unit.  */
115
116 int
117 dwarf2out_do_frame (void)
118 {
119   /* We want to emit correct CFA location expressions or lists, so we
120      have to return true if we're going to output debug info, even if
121      we're not going to output frame or unwind info.  */
122   return (write_symbols == DWARF2_DEBUG
123           || write_symbols == VMS_AND_DWARF2_DEBUG
124           || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126           || (DWARF2_UNWIND_INFO
127               && (flag_unwind_tables
128                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
130           );
131 }
132
133 /* The size of the target's pointer type.  */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
137
138 /* Array of RTXes referenced by the debugging information, which therefore
139    must be kept around forever.  */
140 static GTY(()) VEC(rtx,gc) *used_rtx_array;
141
142 /* A pointer to the base of a list of incomplete types which might be
143    completed at some later time.  incomplete_types_list needs to be a
144    VEC(tree,gc) because we want to tell the garbage collector about
145    it.  */
146 static GTY(()) VEC(tree,gc) *incomplete_types;
147
148 /* A pointer to the base of a table of references to declaration
149    scopes.  This table is a display which tracks the nesting
150    of declaration scopes at the current scope and containing
151    scopes.  This table is used to find the proper place to
152    define type declaration DIE's.  */
153 static GTY(()) VEC(tree,gc) *decl_scope_table;
154
155 /* Pointers to various DWARF2 sections.  */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_ranges_section;
166 static GTY(()) section *debug_frame_section;
167
168 /* How to start an assembler comment.  */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
172
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
176
177 /* Call frames are described using a sequence of Call Frame
178    Information instructions.  The register number, offset
179    and address fields are provided as possible operands;
180    their use is selected by the opcode field.  */
181
182 enum dw_cfi_oprnd_type {
183   dw_cfi_oprnd_unused,
184   dw_cfi_oprnd_reg_num,
185   dw_cfi_oprnd_offset,
186   dw_cfi_oprnd_addr,
187   dw_cfi_oprnd_loc
188 };
189
190 typedef union dw_cfi_oprnd_struct GTY(())
191 {
192   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
193   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
194   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
196 }
197 dw_cfi_oprnd;
198
199 typedef struct dw_cfi_struct GTY(())
200 {
201   dw_cfi_ref dw_cfi_next;
202   enum dwarf_call_frame_info dw_cfi_opc;
203   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
204     dw_cfi_oprnd1;
205   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
206     dw_cfi_oprnd2;
207 }
208 dw_cfi_node;
209
210 /* This is how we define the location of the CFA. We use to handle it
211    as REG + OFFSET all the time,  but now it can be more complex.
212    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
213    Instead of passing around REG and OFFSET, we pass a copy
214    of this structure.  */
215 typedef struct cfa_loc GTY(())
216 {
217   HOST_WIDE_INT offset;
218   HOST_WIDE_INT base_offset;
219   unsigned int reg;
220   int indirect;            /* 1 if CFA is accessed via a dereference.  */
221 } dw_cfa_location;
222
223 /* All call frame descriptions (FDE's) in the GCC generated DWARF
224    refer to a single Common Information Entry (CIE), defined at
225    the beginning of the .debug_frame section.  This use of a single
226    CIE obviates the need to keep track of multiple CIE's
227    in the DWARF generation routines below.  */
228
229 typedef struct dw_fde_struct GTY(())
230 {
231   tree decl;
232   const char *dw_fde_begin;
233   const char *dw_fde_current_label;
234   const char *dw_fde_end;
235   const char *dw_fde_hot_section_label;
236   const char *dw_fde_hot_section_end_label;
237   const char *dw_fde_unlikely_section_label;
238   const char *dw_fde_unlikely_section_end_label;
239   bool dw_fde_switched_sections;
240   dw_cfi_ref dw_fde_cfi;
241   unsigned funcdef_number;
242   unsigned all_throwers_are_sibcalls : 1;
243   unsigned nothrow : 1;
244   unsigned uses_eh_lsda : 1;
245 }
246 dw_fde_node;
247
248 /* Maximum size (in bytes) of an artificially generated label.  */
249 #define MAX_ARTIFICIAL_LABEL_BYTES      30
250
251 /* The size of addresses as they appear in the Dwarf 2 data.
252    Some architectures use word addresses to refer to code locations,
253    but Dwarf 2 info always uses byte addresses.  On such machines,
254    Dwarf 2 addresses need to be larger than the architecture's
255    pointers.  */
256 #ifndef DWARF2_ADDR_SIZE
257 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
258 #endif
259
260 /* The size in bytes of a DWARF field indicating an offset or length
261    relative to a debug info section, specified to be 4 bytes in the
262    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
263    as PTR_SIZE.  */
264
265 #ifndef DWARF_OFFSET_SIZE
266 #define DWARF_OFFSET_SIZE 4
267 #endif
268
269 /* According to the (draft) DWARF 3 specification, the initial length
270    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
271    bytes are 0xffffffff, followed by the length stored in the next 8
272    bytes.
273
274    However, the SGI/MIPS ABI uses an initial length which is equal to
275    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
276
277 #ifndef DWARF_INITIAL_LENGTH_SIZE
278 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
279 #endif
280
281 #define DWARF_VERSION 2
282
283 /* Round SIZE up to the nearest BOUNDARY.  */
284 #define DWARF_ROUND(SIZE,BOUNDARY) \
285   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
286
287 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
288 #ifndef DWARF_CIE_DATA_ALIGNMENT
289 #ifdef STACK_GROWS_DOWNWARD
290 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
291 #else
292 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
293 #endif
294 #endif
295
296 /* CIE identifier.  */
297 #if HOST_BITS_PER_WIDE_INT >= 64
298 #define DWARF_CIE_ID \
299   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
300 #else
301 #define DWARF_CIE_ID DW_CIE_ID
302 #endif
303
304 /* A pointer to the base of a table that contains frame description
305    information for each routine.  */
306 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
307
308 /* Number of elements currently allocated for fde_table.  */
309 static GTY(()) unsigned fde_table_allocated;
310
311 /* Number of elements in fde_table currently in use.  */
312 static GTY(()) unsigned fde_table_in_use;
313
314 /* Size (in elements) of increments by which we may expand the
315    fde_table.  */
316 #define FDE_TABLE_INCREMENT 256
317
318 /* A list of call frame insns for the CIE.  */
319 static GTY(()) dw_cfi_ref cie_cfi_head;
320
321 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
322 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
323    attribute that accelerates the lookup of the FDE associated
324    with the subprogram.  This variable holds the table index of the FDE
325    associated with the current function (body) definition.  */
326 static unsigned current_funcdef_fde;
327 #endif
328
329 struct indirect_string_node GTY(())
330 {
331   const char *str;
332   unsigned int refcount;
333   unsigned int form;
334   char *label;
335 };
336
337 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
338
339 static GTY(()) int dw2_string_counter;
340 static GTY(()) unsigned long dwarf2out_cfi_label_num;
341
342 /* True if the compilation unit places functions in more than one section.  */
343 static GTY(()) bool have_multiple_function_sections = false;
344
345 /* Whether the default text and cold text sections have been used at all.  */
346
347 static GTY(()) bool text_section_used = false;
348 static GTY(()) bool cold_text_section_used = false;
349
350 /* The default cold text section.  */
351 static GTY(()) section *cold_text_section;
352
353 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
354
355 /* Forward declarations for functions defined in this file.  */
356
357 static char *stripattributes (const char *);
358 static const char *dwarf_cfi_name (unsigned);
359 static dw_cfi_ref new_cfi (void);
360 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
361 static void add_fde_cfi (const char *, dw_cfi_ref);
362 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
363 static void lookup_cfa (dw_cfa_location *);
364 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
365 #ifdef DWARF2_UNWIND_INFO
366 static void initial_return_save (rtx);
367 #endif
368 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
369 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
370 static void output_call_frame_info (int);
371 static void dwarf2out_note_section_used (void);
372 static void dwarf2out_stack_adjust (rtx, bool);
373 static void flush_queued_reg_saves (void);
374 static bool clobbers_queued_reg_save (const_rtx);
375 static void dwarf2out_frame_debug_expr (rtx, const char *);
376
377 /* Support for complex CFA locations.  */
378 static void output_cfa_loc (dw_cfi_ref);
379 static void get_cfa_from_loc_descr (dw_cfa_location *,
380                                     struct dw_loc_descr_struct *);
381 static struct dw_loc_descr_struct *build_cfa_loc
382   (dw_cfa_location *, HOST_WIDE_INT);
383 static void def_cfa_1 (const char *, dw_cfa_location *);
384
385 /* How to start an assembler comment.  */
386 #ifndef ASM_COMMENT_START
387 #define ASM_COMMENT_START ";#"
388 #endif
389
390 /* Data and reference forms for relocatable data.  */
391 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
392 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
393
394 #ifndef DEBUG_FRAME_SECTION
395 #define DEBUG_FRAME_SECTION     ".debug_frame"
396 #endif
397
398 #ifndef FUNC_BEGIN_LABEL
399 #define FUNC_BEGIN_LABEL        "LFB"
400 #endif
401
402 #ifndef FUNC_END_LABEL
403 #define FUNC_END_LABEL          "LFE"
404 #endif
405
406 #ifndef FRAME_BEGIN_LABEL
407 #define FRAME_BEGIN_LABEL       "Lframe"
408 #endif
409 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
410 #define CIE_END_LABEL           "LECIE"
411 #define FDE_LABEL               "LSFDE"
412 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
413 #define FDE_END_LABEL           "LEFDE"
414 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
415 #define LINE_NUMBER_END_LABEL   "LELT"
416 #define LN_PROLOG_AS_LABEL      "LASLTP"
417 #define LN_PROLOG_END_LABEL     "LELTP"
418 #define DIE_LABEL_PREFIX        "DW"
419
420 /* The DWARF 2 CFA column which tracks the return address.  Normally this
421    is the column for PC, or the first column after all of the hard
422    registers.  */
423 #ifndef DWARF_FRAME_RETURN_COLUMN
424 #ifdef PC_REGNUM
425 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
426 #else
427 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
428 #endif
429 #endif
430
431 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
432    default, we just provide columns for all registers.  */
433 #ifndef DWARF_FRAME_REGNUM
434 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
435 #endif
436 \f
437 /* Hook used by __throw.  */
438
439 rtx
440 expand_builtin_dwarf_sp_column (void)
441 {
442   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
443   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
444 }
445
446 /* Return a pointer to a copy of the section string name S with all
447    attributes stripped off, and an asterisk prepended (for assemble_name).  */
448
449 static inline char *
450 stripattributes (const char *s)
451 {
452   char *stripped = XNEWVEC (char, strlen (s) + 2);
453   char *p = stripped;
454
455   *p++ = '*';
456
457   while (*s && *s != ',')
458     *p++ = *s++;
459
460   *p = '\0';
461   return stripped;
462 }
463
464 /* MEM is a memory reference for the register size table, each element of
465    which has mode MODE.  Initialize column C as a return address column.  */
466
467 static void
468 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
469 {
470   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
471   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
472   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
473 }
474
475 /* Generate code to initialize the register size table.  */
476
477 void
478 expand_builtin_init_dwarf_reg_sizes (tree address)
479 {
480   unsigned int i;
481   enum machine_mode mode = TYPE_MODE (char_type_node);
482   rtx addr = expand_normal (address);
483   rtx mem = gen_rtx_MEM (BLKmode, addr);
484   bool wrote_return_column = false;
485
486   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
487     {
488       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
489
490       if (rnum < DWARF_FRAME_REGISTERS)
491         {
492           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
493           enum machine_mode save_mode = reg_raw_mode[i];
494           HOST_WIDE_INT size;
495
496           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
497             save_mode = choose_hard_reg_mode (i, 1, true);
498           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
499             {
500               if (save_mode == VOIDmode)
501                 continue;
502               wrote_return_column = true;
503             }
504           size = GET_MODE_SIZE (save_mode);
505           if (offset < 0)
506             continue;
507
508           emit_move_insn (adjust_address (mem, mode, offset),
509                           gen_int_mode (size, mode));
510         }
511     }
512
513   if (!wrote_return_column)
514     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
515
516 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
517   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
518 #endif
519
520   targetm.init_dwarf_reg_sizes_extra (address);
521 }
522
523 /* Convert a DWARF call frame info. operation to its string name */
524
525 static const char *
526 dwarf_cfi_name (unsigned int cfi_opc)
527 {
528   switch (cfi_opc)
529     {
530     case DW_CFA_advance_loc:
531       return "DW_CFA_advance_loc";
532     case DW_CFA_offset:
533       return "DW_CFA_offset";
534     case DW_CFA_restore:
535       return "DW_CFA_restore";
536     case DW_CFA_nop:
537       return "DW_CFA_nop";
538     case DW_CFA_set_loc:
539       return "DW_CFA_set_loc";
540     case DW_CFA_advance_loc1:
541       return "DW_CFA_advance_loc1";
542     case DW_CFA_advance_loc2:
543       return "DW_CFA_advance_loc2";
544     case DW_CFA_advance_loc4:
545       return "DW_CFA_advance_loc4";
546     case DW_CFA_offset_extended:
547       return "DW_CFA_offset_extended";
548     case DW_CFA_restore_extended:
549       return "DW_CFA_restore_extended";
550     case DW_CFA_undefined:
551       return "DW_CFA_undefined";
552     case DW_CFA_same_value:
553       return "DW_CFA_same_value";
554     case DW_CFA_register:
555       return "DW_CFA_register";
556     case DW_CFA_remember_state:
557       return "DW_CFA_remember_state";
558     case DW_CFA_restore_state:
559       return "DW_CFA_restore_state";
560     case DW_CFA_def_cfa:
561       return "DW_CFA_def_cfa";
562     case DW_CFA_def_cfa_register:
563       return "DW_CFA_def_cfa_register";
564     case DW_CFA_def_cfa_offset:
565       return "DW_CFA_def_cfa_offset";
566
567     /* DWARF 3 */
568     case DW_CFA_def_cfa_expression:
569       return "DW_CFA_def_cfa_expression";
570     case DW_CFA_expression:
571       return "DW_CFA_expression";
572     case DW_CFA_offset_extended_sf:
573       return "DW_CFA_offset_extended_sf";
574     case DW_CFA_def_cfa_sf:
575       return "DW_CFA_def_cfa_sf";
576     case DW_CFA_def_cfa_offset_sf:
577       return "DW_CFA_def_cfa_offset_sf";
578
579     /* SGI/MIPS specific */
580     case DW_CFA_MIPS_advance_loc8:
581       return "DW_CFA_MIPS_advance_loc8";
582
583     /* GNU extensions */
584     case DW_CFA_GNU_window_save:
585       return "DW_CFA_GNU_window_save";
586     case DW_CFA_GNU_args_size:
587       return "DW_CFA_GNU_args_size";
588     case DW_CFA_GNU_negative_offset_extended:
589       return "DW_CFA_GNU_negative_offset_extended";
590
591     default:
592       return "DW_CFA_<unknown>";
593     }
594 }
595
596 /* Return a pointer to a newly allocated Call Frame Instruction.  */
597
598 static inline dw_cfi_ref
599 new_cfi (void)
600 {
601   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
602
603   cfi->dw_cfi_next = NULL;
604   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
605   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
606
607   return cfi;
608 }
609
610 /* Add a Call Frame Instruction to list of instructions.  */
611
612 static inline void
613 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
614 {
615   dw_cfi_ref *p;
616
617   /* Find the end of the chain.  */
618   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
619     ;
620
621   *p = cfi;
622 }
623
624 /* Generate a new label for the CFI info to refer to.  */
625
626 char *
627 dwarf2out_cfi_label (void)
628 {
629   static char label[20];
630
631   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
632   ASM_OUTPUT_LABEL (asm_out_file, label);
633   return label;
634 }
635
636 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
637    or to the CIE if LABEL is NULL.  */
638
639 static void
640 add_fde_cfi (const char *label, dw_cfi_ref cfi)
641 {
642   if (label)
643     {
644       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
645
646       if (*label == 0)
647         label = dwarf2out_cfi_label ();
648
649       if (fde->dw_fde_current_label == NULL
650           || strcmp (label, fde->dw_fde_current_label) != 0)
651         {
652           dw_cfi_ref xcfi;
653
654           label = xstrdup (label);
655
656           /* Set the location counter to the new label.  */
657           xcfi = new_cfi ();
658           /* If we have a current label, advance from there, otherwise
659              set the location directly using set_loc.  */
660           xcfi->dw_cfi_opc = fde->dw_fde_current_label
661                              ? DW_CFA_advance_loc4
662                              : DW_CFA_set_loc;
663           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
664           add_cfi (&fde->dw_fde_cfi, xcfi);
665
666           fde->dw_fde_current_label = label;
667         }
668
669       add_cfi (&fde->dw_fde_cfi, cfi);
670     }
671
672   else
673     add_cfi (&cie_cfi_head, cfi);
674 }
675
676 /* Subroutine of lookup_cfa.  */
677
678 static void
679 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
680 {
681   switch (cfi->dw_cfi_opc)
682     {
683     case DW_CFA_def_cfa_offset:
684       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
685       break;
686     case DW_CFA_def_cfa_offset_sf:
687       loc->offset
688         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689       break;
690     case DW_CFA_def_cfa_register:
691       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
692       break;
693     case DW_CFA_def_cfa:
694       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
695       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
696       break;
697     case DW_CFA_def_cfa_sf:
698       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
699       loc->offset
700         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
701       break;
702     case DW_CFA_def_cfa_expression:
703       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
704       break;
705     default:
706       break;
707     }
708 }
709
710 /* Find the previous value for the CFA.  */
711
712 static void
713 lookup_cfa (dw_cfa_location *loc)
714 {
715   dw_cfi_ref cfi;
716
717   loc->reg = INVALID_REGNUM;
718   loc->offset = 0;
719   loc->indirect = 0;
720   loc->base_offset = 0;
721
722   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
723     lookup_cfa_1 (cfi, loc);
724
725   if (fde_table_in_use)
726     {
727       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
728       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
729         lookup_cfa_1 (cfi, loc);
730     }
731 }
732
733 /* The current rule for calculating the DWARF2 canonical frame address.  */
734 static dw_cfa_location cfa;
735
736 /* The register used for saving registers to the stack, and its offset
737    from the CFA.  */
738 static dw_cfa_location cfa_store;
739
740 /* The running total of the size of arguments pushed onto the stack.  */
741 static HOST_WIDE_INT args_size;
742
743 /* The last args_size we actually output.  */
744 static HOST_WIDE_INT old_args_size;
745
746 /* Entry point to update the canonical frame address (CFA).
747    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
748    calculated from REG+OFFSET.  */
749
750 void
751 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
752 {
753   dw_cfa_location loc;
754   loc.indirect = 0;
755   loc.base_offset = 0;
756   loc.reg = reg;
757   loc.offset = offset;
758   def_cfa_1 (label, &loc);
759 }
760
761 /* Determine if two dw_cfa_location structures define the same data.  */
762
763 static bool
764 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
765 {
766   return (loc1->reg == loc2->reg
767           && loc1->offset == loc2->offset
768           && loc1->indirect == loc2->indirect
769           && (loc1->indirect == 0
770               || loc1->base_offset == loc2->base_offset));
771 }
772
773 /* This routine does the actual work.  The CFA is now calculated from
774    the dw_cfa_location structure.  */
775
776 static void
777 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
778 {
779   dw_cfi_ref cfi;
780   dw_cfa_location old_cfa, loc;
781
782   cfa = *loc_p;
783   loc = *loc_p;
784
785   if (cfa_store.reg == loc.reg && loc.indirect == 0)
786     cfa_store.offset = loc.offset;
787
788   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
789   lookup_cfa (&old_cfa);
790
791   /* If nothing changed, no need to issue any call frame instructions.  */
792   if (cfa_equal_p (&loc, &old_cfa))
793     return;
794
795   cfi = new_cfi ();
796
797   if (loc.reg == old_cfa.reg && !loc.indirect)
798     {
799       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
800          the CFA register did not change but the offset did.  */
801       if (loc.offset < 0)
802         {
803           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
804           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
805
806           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
807           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
808         }
809       else
810         {
811           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
812           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
813         }
814     }
815
816 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
817   else if (loc.offset == old_cfa.offset
818            && old_cfa.reg != INVALID_REGNUM
819            && !loc.indirect)
820     {
821       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
822          indicating the CFA register has changed to <register> but the
823          offset has not changed.  */
824       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
825       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
826     }
827 #endif
828
829   else if (loc.indirect == 0)
830     {
831       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
832          indicating the CFA register has changed to <register> with
833          the specified offset.  */
834       if (loc.offset < 0)
835         {
836           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
837           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
838
839           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
840           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
841           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
842         }
843       else
844         {
845           cfi->dw_cfi_opc = DW_CFA_def_cfa;
846           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
847           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
848         }
849     }
850   else
851     {
852       /* Construct a DW_CFA_def_cfa_expression instruction to
853          calculate the CFA using a full location expression since no
854          register-offset pair is available.  */
855       struct dw_loc_descr_struct *loc_list;
856
857       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
858       loc_list = build_cfa_loc (&loc, 0);
859       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
860     }
861
862   add_fde_cfi (label, cfi);
863 }
864
865 /* Add the CFI for saving a register.  REG is the CFA column number.
866    LABEL is passed to add_fde_cfi.
867    If SREG is -1, the register is saved at OFFSET from the CFA;
868    otherwise it is saved in SREG.  */
869
870 static void
871 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
872 {
873   dw_cfi_ref cfi = new_cfi ();
874
875   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
876
877   if (sreg == INVALID_REGNUM)
878     {
879       if (reg & ~0x3f)
880         /* The register number won't fit in 6 bits, so we have to use
881            the long form.  */
882         cfi->dw_cfi_opc = DW_CFA_offset_extended;
883       else
884         cfi->dw_cfi_opc = DW_CFA_offset;
885
886 #ifdef ENABLE_CHECKING
887       {
888         /* If we get an offset that is not a multiple of
889            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
890            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
891            description.  */
892         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
893
894         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
895       }
896 #endif
897       offset /= DWARF_CIE_DATA_ALIGNMENT;
898       if (offset < 0)
899         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
900
901       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
902     }
903   else if (sreg == reg)
904     cfi->dw_cfi_opc = DW_CFA_same_value;
905   else
906     {
907       cfi->dw_cfi_opc = DW_CFA_register;
908       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
909     }
910
911   add_fde_cfi (label, cfi);
912 }
913
914 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
915    This CFI tells the unwinder that it needs to restore the window registers
916    from the previous frame's window save area.
917
918    ??? Perhaps we should note in the CIE where windows are saved (instead of
919    assuming 0(cfa)) and what registers are in the window.  */
920
921 void
922 dwarf2out_window_save (const char *label)
923 {
924   dw_cfi_ref cfi = new_cfi ();
925
926   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
927   add_fde_cfi (label, cfi);
928 }
929
930 /* Add a CFI to update the running total of the size of arguments
931    pushed onto the stack.  */
932
933 void
934 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
935 {
936   dw_cfi_ref cfi;
937
938   if (size == old_args_size)
939     return;
940
941   old_args_size = size;
942
943   cfi = new_cfi ();
944   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
945   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
946   add_fde_cfi (label, cfi);
947 }
948
949 /* Entry point for saving a register to the stack.  REG is the GCC register
950    number.  LABEL and OFFSET are passed to reg_save.  */
951
952 void
953 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
954 {
955   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
956 }
957
958 /* Entry point for saving the return address in the stack.
959    LABEL and OFFSET are passed to reg_save.  */
960
961 void
962 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
963 {
964   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
965 }
966
967 /* Entry point for saving the return address in a register.
968    LABEL and SREG are passed to reg_save.  */
969
970 void
971 dwarf2out_return_reg (const char *label, unsigned int sreg)
972 {
973   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
974 }
975
976 #ifdef DWARF2_UNWIND_INFO
977 /* Record the initial position of the return address.  RTL is
978    INCOMING_RETURN_ADDR_RTX.  */
979
980 static void
981 initial_return_save (rtx rtl)
982 {
983   unsigned int reg = INVALID_REGNUM;
984   HOST_WIDE_INT offset = 0;
985
986   switch (GET_CODE (rtl))
987     {
988     case REG:
989       /* RA is in a register.  */
990       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
991       break;
992
993     case MEM:
994       /* RA is on the stack.  */
995       rtl = XEXP (rtl, 0);
996       switch (GET_CODE (rtl))
997         {
998         case REG:
999           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1000           offset = 0;
1001           break;
1002
1003         case PLUS:
1004           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1005           offset = INTVAL (XEXP (rtl, 1));
1006           break;
1007
1008         case MINUS:
1009           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1010           offset = -INTVAL (XEXP (rtl, 1));
1011           break;
1012
1013         default:
1014           gcc_unreachable ();
1015         }
1016
1017       break;
1018
1019     case PLUS:
1020       /* The return address is at some offset from any value we can
1021          actually load.  For instance, on the SPARC it is in %i7+8. Just
1022          ignore the offset for now; it doesn't matter for unwinding frames.  */
1023       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1024       initial_return_save (XEXP (rtl, 0));
1025       return;
1026
1027     default:
1028       gcc_unreachable ();
1029     }
1030
1031   if (reg != DWARF_FRAME_RETURN_COLUMN)
1032     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1033 }
1034 #endif
1035
1036 /* Given a SET, calculate the amount of stack adjustment it
1037    contains.  */
1038
1039 static HOST_WIDE_INT
1040 stack_adjust_offset (const_rtx pattern)
1041 {
1042   const_rtx src = SET_SRC (pattern);
1043   const_rtx dest = SET_DEST (pattern);
1044   HOST_WIDE_INT offset = 0;
1045   enum rtx_code code;
1046
1047   if (dest == stack_pointer_rtx)
1048     {
1049       /* (set (reg sp) (plus (reg sp) (const_int))) */
1050       code = GET_CODE (src);
1051       if (! (code == PLUS || code == MINUS)
1052           || XEXP (src, 0) != stack_pointer_rtx
1053           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1054         return 0;
1055
1056       offset = INTVAL (XEXP (src, 1));
1057       if (code == PLUS)
1058         offset = -offset;
1059     }
1060   else if (MEM_P (dest))
1061     {
1062       /* (set (mem (pre_dec (reg sp))) (foo)) */
1063       src = XEXP (dest, 0);
1064       code = GET_CODE (src);
1065
1066       switch (code)
1067         {
1068         case PRE_MODIFY:
1069         case POST_MODIFY:
1070           if (XEXP (src, 0) == stack_pointer_rtx)
1071             {
1072               rtx val = XEXP (XEXP (src, 1), 1);
1073               /* We handle only adjustments by constant amount.  */
1074               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1075                           && GET_CODE (val) == CONST_INT);
1076               offset = -INTVAL (val);
1077               break;
1078             }
1079           return 0;
1080
1081         case PRE_DEC:
1082         case POST_DEC:
1083           if (XEXP (src, 0) == stack_pointer_rtx)
1084             {
1085               offset = GET_MODE_SIZE (GET_MODE (dest));
1086               break;
1087             }
1088           return 0;
1089
1090         case PRE_INC:
1091         case POST_INC:
1092           if (XEXP (src, 0) == stack_pointer_rtx)
1093             {
1094               offset = -GET_MODE_SIZE (GET_MODE (dest));
1095               break;
1096             }
1097           return 0;
1098
1099         default:
1100           return 0;
1101         }
1102     }
1103   else
1104     return 0;
1105
1106   return offset;
1107 }
1108
1109 /* Check INSN to see if it looks like a push or a stack adjustment, and
1110    make a note of it if it does.  EH uses this information to find out how
1111    much extra space it needs to pop off the stack.  */
1112
1113 static void
1114 dwarf2out_stack_adjust (rtx insn, bool after_p)
1115 {
1116   HOST_WIDE_INT offset;
1117   const char *label;
1118   int i;
1119
1120   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1121      with this function.  Proper support would require all frame-related
1122      insns to be marked, and to be able to handle saving state around
1123      epilogues textually in the middle of the function.  */
1124   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1125     return;
1126
1127   /* If only calls can throw, and we have a frame pointer,
1128      save up adjustments until we see the CALL_INSN.  */
1129   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1130     {
1131       if (CALL_P (insn) && !after_p)
1132         {
1133           /* Extract the size of the args from the CALL rtx itself.  */
1134           insn = PATTERN (insn);
1135           if (GET_CODE (insn) == PARALLEL)
1136             insn = XVECEXP (insn, 0, 0);
1137           if (GET_CODE (insn) == SET)
1138             insn = SET_SRC (insn);
1139           gcc_assert (GET_CODE (insn) == CALL);
1140           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1141         }
1142       return;
1143     }
1144
1145   if (CALL_P (insn) && !after_p)
1146     {
1147       if (!flag_asynchronous_unwind_tables)
1148         dwarf2out_args_size ("", args_size);
1149       return;
1150     }
1151   else if (BARRIER_P (insn))
1152     {
1153       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1154          the compiler will have already emitted a stack adjustment, but
1155          doesn't bother for calls to noreturn functions.  */
1156 #ifdef STACK_GROWS_DOWNWARD
1157       offset = -args_size;
1158 #else
1159       offset = args_size;
1160 #endif
1161     }
1162   else if (GET_CODE (PATTERN (insn)) == SET)
1163     offset = stack_adjust_offset (PATTERN (insn));
1164   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1165            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1166     {
1167       /* There may be stack adjustments inside compound insns.  Search
1168          for them.  */
1169       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1170         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1171           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1172     }
1173   else
1174     return;
1175
1176   if (offset == 0)
1177     return;
1178
1179   if (cfa.reg == STACK_POINTER_REGNUM)
1180     cfa.offset += offset;
1181
1182 #ifndef STACK_GROWS_DOWNWARD
1183   offset = -offset;
1184 #endif
1185
1186   args_size += offset;
1187   if (args_size < 0)
1188     args_size = 0;
1189
1190   label = dwarf2out_cfi_label ();
1191   def_cfa_1 (label, &cfa);
1192   if (flag_asynchronous_unwind_tables)
1193     dwarf2out_args_size (label, args_size);
1194 }
1195
1196 #endif
1197
1198 /* We delay emitting a register save until either (a) we reach the end
1199    of the prologue or (b) the register is clobbered.  This clusters
1200    register saves so that there are fewer pc advances.  */
1201
1202 struct queued_reg_save GTY(())
1203 {
1204   struct queued_reg_save *next;
1205   rtx reg;
1206   HOST_WIDE_INT cfa_offset;
1207   rtx saved_reg;
1208 };
1209
1210 static GTY(()) struct queued_reg_save *queued_reg_saves;
1211
1212 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1213 struct reg_saved_in_data GTY(()) {
1214   rtx orig_reg;
1215   rtx saved_in_reg;
1216 };
1217
1218 /* A list of registers saved in other registers.
1219    The list intentionally has a small maximum capacity of 4; if your
1220    port needs more than that, you might consider implementing a
1221    more efficient data structure.  */
1222 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1223 static GTY(()) size_t num_regs_saved_in_regs;
1224
1225 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1226 static const char *last_reg_save_label;
1227
1228 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1229    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1230
1231 static void
1232 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1233 {
1234   struct queued_reg_save *q;
1235
1236   /* Duplicates waste space, but it's also necessary to remove them
1237      for correctness, since the queue gets output in reverse
1238      order.  */
1239   for (q = queued_reg_saves; q != NULL; q = q->next)
1240     if (REGNO (q->reg) == REGNO (reg))
1241       break;
1242
1243   if (q == NULL)
1244     {
1245       q = ggc_alloc (sizeof (*q));
1246       q->next = queued_reg_saves;
1247       queued_reg_saves = q;
1248     }
1249
1250   q->reg = reg;
1251   q->cfa_offset = offset;
1252   q->saved_reg = sreg;
1253
1254   last_reg_save_label = label;
1255 }
1256
1257 /* Output all the entries in QUEUED_REG_SAVES.  */
1258
1259 static void
1260 flush_queued_reg_saves (void)
1261 {
1262   struct queued_reg_save *q;
1263
1264   for (q = queued_reg_saves; q; q = q->next)
1265     {
1266       size_t i;
1267       unsigned int reg, sreg;
1268
1269       for (i = 0; i < num_regs_saved_in_regs; i++)
1270         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1271           break;
1272       if (q->saved_reg && i == num_regs_saved_in_regs)
1273         {
1274           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1275           num_regs_saved_in_regs++;
1276         }
1277       if (i != num_regs_saved_in_regs)
1278         {
1279           regs_saved_in_regs[i].orig_reg = q->reg;
1280           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1281         }
1282
1283       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1284       if (q->saved_reg)
1285         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1286       else
1287         sreg = INVALID_REGNUM;
1288       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1289     }
1290
1291   queued_reg_saves = NULL;
1292   last_reg_save_label = NULL;
1293 }
1294
1295 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1296    location for?  Or, does it clobber a register which we've previously
1297    said that some other register is saved in, and for which we now
1298    have a new location for?  */
1299
1300 static bool
1301 clobbers_queued_reg_save (const_rtx insn)
1302 {
1303   struct queued_reg_save *q;
1304
1305   for (q = queued_reg_saves; q; q = q->next)
1306     {
1307       size_t i;
1308       if (modified_in_p (q->reg, insn))
1309         return true;
1310       for (i = 0; i < num_regs_saved_in_regs; i++)
1311         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1312             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1313           return true;
1314     }
1315
1316   return false;
1317 }
1318
1319 /* Entry point for saving the first register into the second.  */
1320
1321 void
1322 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1323 {
1324   size_t i;
1325   unsigned int regno, sregno;
1326
1327   for (i = 0; i < num_regs_saved_in_regs; i++)
1328     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1329       break;
1330   if (i == num_regs_saved_in_regs)
1331     {
1332       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1333       num_regs_saved_in_regs++;
1334     }
1335   regs_saved_in_regs[i].orig_reg = reg;
1336   regs_saved_in_regs[i].saved_in_reg = sreg;
1337
1338   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1339   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1340   reg_save (label, regno, sregno, 0);
1341 }
1342
1343 /* What register, if any, is currently saved in REG?  */
1344
1345 static rtx
1346 reg_saved_in (rtx reg)
1347 {
1348   unsigned int regn = REGNO (reg);
1349   size_t i;
1350   struct queued_reg_save *q;
1351
1352   for (q = queued_reg_saves; q; q = q->next)
1353     if (q->saved_reg && regn == REGNO (q->saved_reg))
1354       return q->reg;
1355
1356   for (i = 0; i < num_regs_saved_in_regs; i++)
1357     if (regs_saved_in_regs[i].saved_in_reg
1358         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1359       return regs_saved_in_regs[i].orig_reg;
1360
1361   return NULL_RTX;
1362 }
1363
1364
1365 /* A temporary register holding an integral value used in adjusting SP
1366    or setting up the store_reg.  The "offset" field holds the integer
1367    value, not an offset.  */
1368 static dw_cfa_location cfa_temp;
1369
1370 /* Record call frame debugging information for an expression EXPR,
1371    which either sets SP or FP (adjusting how we calculate the frame
1372    address) or saves a register to the stack or another register.
1373    LABEL indicates the address of EXPR.
1374
1375    This function encodes a state machine mapping rtxes to actions on
1376    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1377    users need not read the source code.
1378
1379   The High-Level Picture
1380
1381   Changes in the register we use to calculate the CFA: Currently we
1382   assume that if you copy the CFA register into another register, we
1383   should take the other one as the new CFA register; this seems to
1384   work pretty well.  If it's wrong for some target, it's simple
1385   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1386
1387   Changes in the register we use for saving registers to the stack:
1388   This is usually SP, but not always.  Again, we deduce that if you
1389   copy SP into another register (and SP is not the CFA register),
1390   then the new register is the one we will be using for register
1391   saves.  This also seems to work.
1392
1393   Register saves: There's not much guesswork about this one; if
1394   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1395   register save, and the register used to calculate the destination
1396   had better be the one we think we're using for this purpose.
1397   It's also assumed that a copy from a call-saved register to another
1398   register is saving that register if RTX_FRAME_RELATED_P is set on
1399   that instruction.  If the copy is from a call-saved register to
1400   the *same* register, that means that the register is now the same
1401   value as in the caller.
1402
1403   Except: If the register being saved is the CFA register, and the
1404   offset is nonzero, we are saving the CFA, so we assume we have to
1405   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1406   the intent is to save the value of SP from the previous frame.
1407
1408   In addition, if a register has previously been saved to a different
1409   register,
1410
1411   Invariants / Summaries of Rules
1412
1413   cfa          current rule for calculating the CFA.  It usually
1414                consists of a register and an offset.
1415   cfa_store    register used by prologue code to save things to the stack
1416                cfa_store.offset is the offset from the value of
1417                cfa_store.reg to the actual CFA
1418   cfa_temp     register holding an integral value.  cfa_temp.offset
1419                stores the value, which will be used to adjust the
1420                stack pointer.  cfa_temp is also used like cfa_store,
1421                to track stores to the stack via fp or a temp reg.
1422
1423   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1424                with cfa.reg as the first operand changes the cfa.reg and its
1425                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1426                cfa_temp.offset.
1427
1428   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1429                expression yielding a constant.  This sets cfa_temp.reg
1430                and cfa_temp.offset.
1431
1432   Rule 5:      Create a new register cfa_store used to save items to the
1433                stack.
1434
1435   Rules 10-14: Save a register to the stack.  Define offset as the
1436                difference of the original location and cfa_store's
1437                location (or cfa_temp's location if cfa_temp is used).
1438
1439   The Rules
1440
1441   "{a,b}" indicates a choice of a xor b.
1442   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1443
1444   Rule 1:
1445   (set <reg1> <reg2>:cfa.reg)
1446   effects: cfa.reg = <reg1>
1447            cfa.offset unchanged
1448            cfa_temp.reg = <reg1>
1449            cfa_temp.offset = cfa.offset
1450
1451   Rule 2:
1452   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1453                               {<const_int>,<reg>:cfa_temp.reg}))
1454   effects: cfa.reg = sp if fp used
1455            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1456            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1457              if cfa_store.reg==sp
1458
1459   Rule 3:
1460   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1461   effects: cfa.reg = fp
1462            cfa_offset += +/- <const_int>
1463
1464   Rule 4:
1465   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1466   constraints: <reg1> != fp
1467                <reg1> != sp
1468   effects: cfa.reg = <reg1>
1469            cfa_temp.reg = <reg1>
1470            cfa_temp.offset = cfa.offset
1471
1472   Rule 5:
1473   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1474   constraints: <reg1> != fp
1475                <reg1> != sp
1476   effects: cfa_store.reg = <reg1>
1477            cfa_store.offset = cfa.offset - cfa_temp.offset
1478
1479   Rule 6:
1480   (set <reg> <const_int>)
1481   effects: cfa_temp.reg = <reg>
1482            cfa_temp.offset = <const_int>
1483
1484   Rule 7:
1485   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1486   effects: cfa_temp.reg = <reg1>
1487            cfa_temp.offset |= <const_int>
1488
1489   Rule 8:
1490   (set <reg> (high <exp>))
1491   effects: none
1492
1493   Rule 9:
1494   (set <reg> (lo_sum <exp> <const_int>))
1495   effects: cfa_temp.reg = <reg>
1496            cfa_temp.offset = <const_int>
1497
1498   Rule 10:
1499   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1500   effects: cfa_store.offset -= <const_int>
1501            cfa.offset = cfa_store.offset if cfa.reg == sp
1502            cfa.reg = sp
1503            cfa.base_offset = -cfa_store.offset
1504
1505   Rule 11:
1506   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1507   effects: cfa_store.offset += -/+ mode_size(mem)
1508            cfa.offset = cfa_store.offset if cfa.reg == sp
1509            cfa.reg = sp
1510            cfa.base_offset = -cfa_store.offset
1511
1512   Rule 12:
1513   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1514
1515        <reg2>)
1516   effects: cfa.reg = <reg1>
1517            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1518
1519   Rule 13:
1520   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1521   effects: cfa.reg = <reg1>
1522            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1523
1524   Rule 14:
1525   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1526   effects: cfa.reg = <reg1>
1527            cfa.base_offset = -cfa_temp.offset
1528            cfa_temp.offset -= mode_size(mem)
1529
1530   Rule 15:
1531   (set <reg> {unspec, unspec_volatile})
1532   effects: target-dependent  */
1533
1534 static void
1535 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1536 {
1537   rtx src, dest, span;
1538   HOST_WIDE_INT offset;
1539
1540   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1541      the PARALLEL independently. The first element is always processed if
1542      it is a SET. This is for backward compatibility.   Other elements
1543      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1544      flag is set in them.  */
1545   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1546     {
1547       int par_index;
1548       int limit = XVECLEN (expr, 0);
1549       rtx elem;
1550
1551       /* PARALLELs have strict read-modify-write semantics, so we
1552          ought to evaluate every rvalue before changing any lvalue.
1553          It's cumbersome to do that in general, but there's an
1554          easy approximation that is enough for all current users:
1555          handle register saves before register assignments.  */
1556       if (GET_CODE (expr) == PARALLEL)
1557         for (par_index = 0; par_index < limit; par_index++)
1558           {
1559             elem = XVECEXP (expr, 0, par_index);
1560             if (GET_CODE (elem) == SET
1561                 && MEM_P (SET_DEST (elem))
1562                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1563               dwarf2out_frame_debug_expr (elem, label);
1564           }
1565
1566       for (par_index = 0; par_index < limit; par_index++)
1567         {
1568           elem = XVECEXP (expr, 0, par_index);
1569           if (GET_CODE (elem) == SET
1570               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1571               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1572             dwarf2out_frame_debug_expr (elem, label);
1573         }
1574       return;
1575     }
1576
1577   gcc_assert (GET_CODE (expr) == SET);
1578
1579   src = SET_SRC (expr);
1580   dest = SET_DEST (expr);
1581
1582   if (REG_P (src))
1583     {
1584       rtx rsi = reg_saved_in (src);
1585       if (rsi)
1586         src = rsi;
1587     }
1588
1589   switch (GET_CODE (dest))
1590     {
1591     case REG:
1592       switch (GET_CODE (src))
1593         {
1594           /* Setting FP from SP.  */
1595         case REG:
1596           if (cfa.reg == (unsigned) REGNO (src))
1597             {
1598               /* Rule 1 */
1599               /* Update the CFA rule wrt SP or FP.  Make sure src is
1600                  relative to the current CFA register.
1601
1602                  We used to require that dest be either SP or FP, but the
1603                  ARM copies SP to a temporary register, and from there to
1604                  FP.  So we just rely on the backends to only set
1605                  RTX_FRAME_RELATED_P on appropriate insns.  */
1606               cfa.reg = REGNO (dest);
1607               cfa_temp.reg = cfa.reg;
1608               cfa_temp.offset = cfa.offset;
1609             }
1610           else
1611             {
1612               /* Saving a register in a register.  */
1613               gcc_assert (!fixed_regs [REGNO (dest)]
1614                           /* For the SPARC and its register window.  */
1615                           || (DWARF_FRAME_REGNUM (REGNO (src))
1616                               == DWARF_FRAME_RETURN_COLUMN));
1617               queue_reg_save (label, src, dest, 0);
1618             }
1619           break;
1620
1621         case PLUS:
1622         case MINUS:
1623         case LO_SUM:
1624           if (dest == stack_pointer_rtx)
1625             {
1626               /* Rule 2 */
1627               /* Adjusting SP.  */
1628               switch (GET_CODE (XEXP (src, 1)))
1629                 {
1630                 case CONST_INT:
1631                   offset = INTVAL (XEXP (src, 1));
1632                   break;
1633                 case REG:
1634                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1635                               == cfa_temp.reg);
1636                   offset = cfa_temp.offset;
1637                   break;
1638                 default:
1639                   gcc_unreachable ();
1640                 }
1641
1642               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1643                 {
1644                   /* Restoring SP from FP in the epilogue.  */
1645                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1646                   cfa.reg = STACK_POINTER_REGNUM;
1647                 }
1648               else if (GET_CODE (src) == LO_SUM)
1649                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1650                 ;
1651               else
1652                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1653
1654               if (GET_CODE (src) != MINUS)
1655                 offset = -offset;
1656               if (cfa.reg == STACK_POINTER_REGNUM)
1657                 cfa.offset += offset;
1658               if (cfa_store.reg == STACK_POINTER_REGNUM)
1659                 cfa_store.offset += offset;
1660             }
1661           else if (dest == hard_frame_pointer_rtx)
1662             {
1663               /* Rule 3 */
1664               /* Either setting the FP from an offset of the SP,
1665                  or adjusting the FP */
1666               gcc_assert (frame_pointer_needed);
1667
1668               gcc_assert (REG_P (XEXP (src, 0))
1669                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1670                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1671               offset = INTVAL (XEXP (src, 1));
1672               if (GET_CODE (src) != MINUS)
1673                 offset = -offset;
1674               cfa.offset += offset;
1675               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1676             }
1677           else
1678             {
1679               gcc_assert (GET_CODE (src) != MINUS);
1680
1681               /* Rule 4 */
1682               if (REG_P (XEXP (src, 0))
1683                   && REGNO (XEXP (src, 0)) == cfa.reg
1684                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1685                 {
1686                   /* Setting a temporary CFA register that will be copied
1687                      into the FP later on.  */
1688                   offset = - INTVAL (XEXP (src, 1));
1689                   cfa.offset += offset;
1690                   cfa.reg = REGNO (dest);
1691                   /* Or used to save regs to the stack.  */
1692                   cfa_temp.reg = cfa.reg;
1693                   cfa_temp.offset = cfa.offset;
1694                 }
1695
1696               /* Rule 5 */
1697               else if (REG_P (XEXP (src, 0))
1698                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1699                        && XEXP (src, 1) == stack_pointer_rtx)
1700                 {
1701                   /* Setting a scratch register that we will use instead
1702                      of SP for saving registers to the stack.  */
1703                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1704                   cfa_store.reg = REGNO (dest);
1705                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1706                 }
1707
1708               /* Rule 9 */
1709               else if (GET_CODE (src) == LO_SUM
1710                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1711                 {
1712                   cfa_temp.reg = REGNO (dest);
1713                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1714                 }
1715               else
1716                 gcc_unreachable ();
1717             }
1718           break;
1719
1720           /* Rule 6 */
1721         case CONST_INT:
1722           cfa_temp.reg = REGNO (dest);
1723           cfa_temp.offset = INTVAL (src);
1724           break;
1725
1726           /* Rule 7 */
1727         case IOR:
1728           gcc_assert (REG_P (XEXP (src, 0))
1729                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1730                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1731
1732           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1733             cfa_temp.reg = REGNO (dest);
1734           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1735           break;
1736
1737           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1738              which will fill in all of the bits.  */
1739           /* Rule 8 */
1740         case HIGH:
1741           break;
1742
1743           /* Rule 15 */
1744         case UNSPEC:
1745         case UNSPEC_VOLATILE:
1746           gcc_assert (targetm.dwarf_handle_frame_unspec);
1747           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1748           return;
1749
1750         default:
1751           gcc_unreachable ();
1752         }
1753
1754       def_cfa_1 (label, &cfa);
1755       break;
1756
1757     case MEM:
1758       gcc_assert (REG_P (src));
1759
1760       /* Saving a register to the stack.  Make sure dest is relative to the
1761          CFA register.  */
1762       switch (GET_CODE (XEXP (dest, 0)))
1763         {
1764           /* Rule 10 */
1765           /* With a push.  */
1766         case PRE_MODIFY:
1767           /* We can't handle variable size modifications.  */
1768           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1769                       == CONST_INT);
1770           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1771
1772           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1773                       && cfa_store.reg == STACK_POINTER_REGNUM);
1774
1775           cfa_store.offset += offset;
1776           if (cfa.reg == STACK_POINTER_REGNUM)
1777             cfa.offset = cfa_store.offset;
1778
1779           offset = -cfa_store.offset;
1780           break;
1781
1782           /* Rule 11 */
1783         case PRE_INC:
1784         case PRE_DEC:
1785           offset = GET_MODE_SIZE (GET_MODE (dest));
1786           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1787             offset = -offset;
1788
1789           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1790                       && cfa_store.reg == STACK_POINTER_REGNUM);
1791
1792           cfa_store.offset += offset;
1793           if (cfa.reg == STACK_POINTER_REGNUM)
1794             cfa.offset = cfa_store.offset;
1795
1796           offset = -cfa_store.offset;
1797           break;
1798
1799           /* Rule 12 */
1800           /* With an offset.  */
1801         case PLUS:
1802         case MINUS:
1803         case LO_SUM:
1804           {
1805             int regno;
1806
1807             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1808                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1809             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1810             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1811               offset = -offset;
1812
1813             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1814
1815             if (cfa_store.reg == (unsigned) regno)
1816               offset -= cfa_store.offset;
1817             else
1818               {
1819                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1820                 offset -= cfa_temp.offset;
1821               }
1822           }
1823           break;
1824
1825           /* Rule 13 */
1826           /* Without an offset.  */
1827         case REG:
1828           {
1829             int regno = REGNO (XEXP (dest, 0));
1830
1831             if (cfa_store.reg == (unsigned) regno)
1832               offset = -cfa_store.offset;
1833             else
1834               {
1835                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1836                 offset = -cfa_temp.offset;
1837               }
1838           }
1839           break;
1840
1841           /* Rule 14 */
1842         case POST_INC:
1843           gcc_assert (cfa_temp.reg
1844                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1845           offset = -cfa_temp.offset;
1846           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1847           break;
1848
1849         default:
1850           gcc_unreachable ();
1851         }
1852
1853       if (REGNO (src) != STACK_POINTER_REGNUM
1854           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1855           && (unsigned) REGNO (src) == cfa.reg)
1856         {
1857           /* We're storing the current CFA reg into the stack.  */
1858
1859           if (cfa.offset == 0)
1860             {
1861               /* If the source register is exactly the CFA, assume
1862                  we're saving SP like any other register; this happens
1863                  on the ARM.  */
1864               def_cfa_1 (label, &cfa);
1865               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1866               break;
1867             }
1868           else
1869             {
1870               /* Otherwise, we'll need to look in the stack to
1871                  calculate the CFA.  */
1872               rtx x = XEXP (dest, 0);
1873
1874               if (!REG_P (x))
1875                 x = XEXP (x, 0);
1876               gcc_assert (REG_P (x));
1877
1878               cfa.reg = REGNO (x);
1879               cfa.base_offset = offset;
1880               cfa.indirect = 1;
1881               def_cfa_1 (label, &cfa);
1882               break;
1883             }
1884         }
1885
1886       def_cfa_1 (label, &cfa);
1887       {
1888         span = targetm.dwarf_register_span (src);
1889
1890         if (!span)
1891           queue_reg_save (label, src, NULL_RTX, offset);
1892         else
1893           {
1894             /* We have a PARALLEL describing where the contents of SRC
1895                live.  Queue register saves for each piece of the
1896                PARALLEL.  */
1897             int par_index;
1898             int limit;
1899             HOST_WIDE_INT span_offset = offset;
1900
1901             gcc_assert (GET_CODE (span) == PARALLEL);
1902
1903             limit = XVECLEN (span, 0);
1904             for (par_index = 0; par_index < limit; par_index++)
1905               {
1906                 rtx elem = XVECEXP (span, 0, par_index);
1907
1908                 queue_reg_save (label, elem, NULL_RTX, span_offset);
1909                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1910               }
1911           }
1912       }
1913       break;
1914
1915     default:
1916       gcc_unreachable ();
1917     }
1918 }
1919
1920 /* Record call frame debugging information for INSN, which either
1921    sets SP or FP (adjusting how we calculate the frame address) or saves a
1922    register to the stack.  If INSN is NULL_RTX, initialize our state.
1923
1924    If AFTER_P is false, we're being called before the insn is emitted,
1925    otherwise after.  Call instructions get invoked twice.  */
1926
1927 void
1928 dwarf2out_frame_debug (rtx insn, bool after_p)
1929 {
1930   const char *label;
1931   rtx src;
1932
1933   if (insn == NULL_RTX)
1934     {
1935       size_t i;
1936
1937       /* Flush any queued register saves.  */
1938       flush_queued_reg_saves ();
1939
1940       /* Set up state for generating call frame debug info.  */
1941       lookup_cfa (&cfa);
1942       gcc_assert (cfa.reg
1943                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1944
1945       cfa.reg = STACK_POINTER_REGNUM;
1946       cfa_store = cfa;
1947       cfa_temp.reg = -1;
1948       cfa_temp.offset = 0;
1949
1950       for (i = 0; i < num_regs_saved_in_regs; i++)
1951         {
1952           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1953           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1954         }
1955       num_regs_saved_in_regs = 0;
1956       return;
1957     }
1958
1959   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1960     flush_queued_reg_saves ();
1961
1962   if (! RTX_FRAME_RELATED_P (insn))
1963     {
1964       if (!ACCUMULATE_OUTGOING_ARGS)
1965         dwarf2out_stack_adjust (insn, after_p);
1966       return;
1967     }
1968
1969   label = dwarf2out_cfi_label ();
1970   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1971   if (src)
1972     insn = XEXP (src, 0);
1973   else
1974     insn = PATTERN (insn);
1975
1976   dwarf2out_frame_debug_expr (insn, label);
1977 }
1978
1979 #endif
1980
1981 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1982 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1983  (enum dwarf_call_frame_info cfi);
1984
1985 static enum dw_cfi_oprnd_type
1986 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1987 {
1988   switch (cfi)
1989     {
1990     case DW_CFA_nop:
1991     case DW_CFA_GNU_window_save:
1992       return dw_cfi_oprnd_unused;
1993
1994     case DW_CFA_set_loc:
1995     case DW_CFA_advance_loc1:
1996     case DW_CFA_advance_loc2:
1997     case DW_CFA_advance_loc4:
1998     case DW_CFA_MIPS_advance_loc8:
1999       return dw_cfi_oprnd_addr;
2000
2001     case DW_CFA_offset:
2002     case DW_CFA_offset_extended:
2003     case DW_CFA_def_cfa:
2004     case DW_CFA_offset_extended_sf:
2005     case DW_CFA_def_cfa_sf:
2006     case DW_CFA_restore_extended:
2007     case DW_CFA_undefined:
2008     case DW_CFA_same_value:
2009     case DW_CFA_def_cfa_register:
2010     case DW_CFA_register:
2011       return dw_cfi_oprnd_reg_num;
2012
2013     case DW_CFA_def_cfa_offset:
2014     case DW_CFA_GNU_args_size:
2015     case DW_CFA_def_cfa_offset_sf:
2016       return dw_cfi_oprnd_offset;
2017
2018     case DW_CFA_def_cfa_expression:
2019     case DW_CFA_expression:
2020       return dw_cfi_oprnd_loc;
2021
2022     default:
2023       gcc_unreachable ();
2024     }
2025 }
2026
2027 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2028 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2029  (enum dwarf_call_frame_info cfi);
2030
2031 static enum dw_cfi_oprnd_type
2032 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2033 {
2034   switch (cfi)
2035     {
2036     case DW_CFA_def_cfa:
2037     case DW_CFA_def_cfa_sf:
2038     case DW_CFA_offset:
2039     case DW_CFA_offset_extended_sf:
2040     case DW_CFA_offset_extended:
2041       return dw_cfi_oprnd_offset;
2042
2043     case DW_CFA_register:
2044       return dw_cfi_oprnd_reg_num;
2045
2046     default:
2047       return dw_cfi_oprnd_unused;
2048     }
2049 }
2050
2051 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2052
2053 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2054    switch to the data section instead, and write out a synthetic label
2055    for collect2.  */
2056
2057 static void
2058 switch_to_eh_frame_section (void)
2059 {
2060   tree label;
2061
2062 #ifdef EH_FRAME_SECTION_NAME
2063   if (eh_frame_section == 0)
2064     {
2065       int flags;
2066
2067       if (EH_TABLES_CAN_BE_READ_ONLY)
2068         {
2069           int fde_encoding;
2070           int per_encoding;
2071           int lsda_encoding;
2072
2073           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2074                                                        /*global=*/0);
2075           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2076                                                        /*global=*/1);
2077           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2078                                                         /*global=*/0);
2079           flags = ((! flag_pic
2080                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2081                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2082                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2083                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2084                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2085                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2086                    ? 0 : SECTION_WRITE);
2087         }
2088       else
2089         flags = SECTION_WRITE;
2090       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2091     }
2092 #endif
2093
2094   if (eh_frame_section)
2095     switch_to_section (eh_frame_section);
2096   else
2097     {
2098       /* We have no special eh_frame section.  Put the information in
2099          the data section and emit special labels to guide collect2.  */
2100       switch_to_section (data_section);
2101       label = get_file_function_name ("F");
2102       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2103       targetm.asm_out.globalize_label (asm_out_file,
2104                                        IDENTIFIER_POINTER (label));
2105       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2106     }
2107 }
2108
2109 /* Output a Call Frame Information opcode and its operand(s).  */
2110
2111 static void
2112 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2113 {
2114   unsigned long r;
2115   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2116     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2117                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2118                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2119                          ((unsigned HOST_WIDE_INT)
2120                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2121   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2122     {
2123       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2124       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2125                            "DW_CFA_offset, column 0x%lx", r);
2126       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2127     }
2128   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2129     {
2130       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2131       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2132                            "DW_CFA_restore, column 0x%lx", r);
2133     }
2134   else
2135     {
2136       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2137                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2138
2139       switch (cfi->dw_cfi_opc)
2140         {
2141         case DW_CFA_set_loc:
2142           if (for_eh)
2143             dw2_asm_output_encoded_addr_rtx (
2144                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2145                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2146                 false, NULL);
2147           else
2148             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2149                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2150           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2151           break;
2152
2153         case DW_CFA_advance_loc1:
2154           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2155                                 fde->dw_fde_current_label, NULL);
2156           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2157           break;
2158
2159         case DW_CFA_advance_loc2:
2160           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2161                                 fde->dw_fde_current_label, NULL);
2162           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2163           break;
2164
2165         case DW_CFA_advance_loc4:
2166           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2167                                 fde->dw_fde_current_label, NULL);
2168           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2169           break;
2170
2171         case DW_CFA_MIPS_advance_loc8:
2172           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2173                                 fde->dw_fde_current_label, NULL);
2174           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2175           break;
2176
2177         case DW_CFA_offset_extended:
2178         case DW_CFA_def_cfa:
2179           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2180           dw2_asm_output_data_uleb128 (r, NULL);
2181           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2182           break;
2183
2184         case DW_CFA_offset_extended_sf:
2185         case DW_CFA_def_cfa_sf:
2186           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2187           dw2_asm_output_data_uleb128 (r, NULL);
2188           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2189           break;
2190
2191         case DW_CFA_restore_extended:
2192         case DW_CFA_undefined:
2193         case DW_CFA_same_value:
2194         case DW_CFA_def_cfa_register:
2195           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2196           dw2_asm_output_data_uleb128 (r, NULL);
2197           break;
2198
2199         case DW_CFA_register:
2200           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2201           dw2_asm_output_data_uleb128 (r, NULL);
2202           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2203           dw2_asm_output_data_uleb128 (r, NULL);
2204           break;
2205
2206         case DW_CFA_def_cfa_offset:
2207         case DW_CFA_GNU_args_size:
2208           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2209           break;
2210
2211         case DW_CFA_def_cfa_offset_sf:
2212           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2213           break;
2214
2215         case DW_CFA_GNU_window_save:
2216           break;
2217
2218         case DW_CFA_def_cfa_expression:
2219         case DW_CFA_expression:
2220           output_cfa_loc (cfi);
2221           break;
2222
2223         case DW_CFA_GNU_negative_offset_extended:
2224           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2225           gcc_unreachable ();
2226
2227         default:
2228           break;
2229         }
2230     }
2231 }
2232
2233 /* Output the call frame information used to record information
2234    that relates to calculating the frame pointer, and records the
2235    location of saved registers.  */
2236
2237 static void
2238 output_call_frame_info (int for_eh)
2239 {
2240   unsigned int i;
2241   dw_fde_ref fde;
2242   dw_cfi_ref cfi;
2243   char l1[20], l2[20], section_start_label[20];
2244   bool any_lsda_needed = false;
2245   char augmentation[6];
2246   int augmentation_size;
2247   int fde_encoding = DW_EH_PE_absptr;
2248   int per_encoding = DW_EH_PE_absptr;
2249   int lsda_encoding = DW_EH_PE_absptr;
2250   int return_reg;
2251
2252   /* Don't emit a CIE if there won't be any FDEs.  */
2253   if (fde_table_in_use == 0)
2254     return;
2255
2256   /* If we make FDEs linkonce, we may have to emit an empty label for
2257      an FDE that wouldn't otherwise be emitted.  We want to avoid
2258      having an FDE kept around when the function it refers to is
2259      discarded.  Example where this matters: a primary function
2260      template in C++ requires EH information, but an explicit
2261      specialization doesn't.  */
2262   if (TARGET_USES_WEAK_UNWIND_INFO
2263       && ! flag_asynchronous_unwind_tables
2264       && flag_exceptions
2265       && for_eh)
2266     for (i = 0; i < fde_table_in_use; i++)
2267       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2268           && !fde_table[i].uses_eh_lsda
2269           && ! DECL_WEAK (fde_table[i].decl))
2270         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2271                                       for_eh, /* empty */ 1);
2272
2273   /* If we don't have any functions we'll want to unwind out of, don't
2274      emit any EH unwind information.  Note that if exceptions aren't
2275      enabled, we won't have collected nothrow information, and if we
2276      asked for asynchronous tables, we always want this info.  */
2277   if (for_eh)
2278     {
2279       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2280
2281       for (i = 0; i < fde_table_in_use; i++)
2282         if (fde_table[i].uses_eh_lsda)
2283           any_eh_needed = any_lsda_needed = true;
2284         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2285           any_eh_needed = true;
2286         else if (! fde_table[i].nothrow
2287                  && ! fde_table[i].all_throwers_are_sibcalls)
2288           any_eh_needed = true;
2289
2290       if (! any_eh_needed)
2291         return;
2292     }
2293
2294   /* We're going to be generating comments, so turn on app.  */
2295   if (flag_debug_asm)
2296     app_enable ();
2297
2298   if (for_eh)
2299     switch_to_eh_frame_section ();
2300   else
2301     {
2302       if (!debug_frame_section)
2303         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2304                                            SECTION_DEBUG, NULL);
2305       switch_to_section (debug_frame_section);
2306     }
2307
2308   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2309   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2310
2311   /* Output the CIE.  */
2312   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2313   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2314   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2315     dw2_asm_output_data (4, 0xffffffff,
2316       "Initial length escape value indicating 64-bit DWARF extension");
2317   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2318                         "Length of Common Information Entry");
2319   ASM_OUTPUT_LABEL (asm_out_file, l1);
2320
2321   /* Now that the CIE pointer is PC-relative for EH,
2322      use 0 to identify the CIE.  */
2323   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2324                        (for_eh ? 0 : DWARF_CIE_ID),
2325                        "CIE Identifier Tag");
2326
2327   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2328
2329   augmentation[0] = 0;
2330   augmentation_size = 0;
2331   if (for_eh)
2332     {
2333       char *p;
2334
2335       /* Augmentation:
2336          z      Indicates that a uleb128 is present to size the
2337                 augmentation section.
2338          L      Indicates the encoding (and thus presence) of
2339                 an LSDA pointer in the FDE augmentation.
2340          R      Indicates a non-default pointer encoding for
2341                 FDE code pointers.
2342          P      Indicates the presence of an encoding + language
2343                 personality routine in the CIE augmentation.  */
2344
2345       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2346       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2347       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2348
2349       p = augmentation + 1;
2350       if (eh_personality_libfunc)
2351         {
2352           *p++ = 'P';
2353           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2354           assemble_external_libcall (eh_personality_libfunc);
2355         }
2356       if (any_lsda_needed)
2357         {
2358           *p++ = 'L';
2359           augmentation_size += 1;
2360         }
2361       if (fde_encoding != DW_EH_PE_absptr)
2362         {
2363           *p++ = 'R';
2364           augmentation_size += 1;
2365         }
2366       if (p > augmentation + 1)
2367         {
2368           augmentation[0] = 'z';
2369           *p = '\0';
2370         }
2371
2372       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2373       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2374         {
2375           int offset = (  4             /* Length */
2376                         + 4             /* CIE Id */
2377                         + 1             /* CIE version */
2378                         + strlen (augmentation) + 1     /* Augmentation */
2379                         + size_of_uleb128 (1)           /* Code alignment */
2380                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2381                         + 1             /* RA column */
2382                         + 1             /* Augmentation size */
2383                         + 1             /* Personality encoding */ );
2384           int pad = -offset & (PTR_SIZE - 1);
2385
2386           augmentation_size += pad;
2387
2388           /* Augmentations should be small, so there's scarce need to
2389              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2390           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2391         }
2392     }
2393
2394   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2395   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2396   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2397                                "CIE Data Alignment Factor");
2398
2399   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2400   if (DW_CIE_VERSION == 1)
2401     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2402   else
2403     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2404
2405   if (augmentation[0])
2406     {
2407       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2408       if (eh_personality_libfunc)
2409         {
2410           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2411                                eh_data_format_name (per_encoding));
2412           dw2_asm_output_encoded_addr_rtx (per_encoding,
2413                                            eh_personality_libfunc,
2414                                            true, NULL);
2415         }
2416
2417       if (any_lsda_needed)
2418         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2419                              eh_data_format_name (lsda_encoding));
2420
2421       if (fde_encoding != DW_EH_PE_absptr)
2422         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2423                              eh_data_format_name (fde_encoding));
2424     }
2425
2426   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2427     output_cfi (cfi, NULL, for_eh);
2428
2429   /* Pad the CIE out to an address sized boundary.  */
2430   ASM_OUTPUT_ALIGN (asm_out_file,
2431                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2432   ASM_OUTPUT_LABEL (asm_out_file, l2);
2433
2434   /* Loop through all of the FDE's.  */
2435   for (i = 0; i < fde_table_in_use; i++)
2436     {
2437       fde = &fde_table[i];
2438
2439       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2440       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2441           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2442           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2443           && !fde->uses_eh_lsda)
2444         continue;
2445
2446       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2447       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2448       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2449       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2450       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2451         dw2_asm_output_data (4, 0xffffffff,
2452                              "Initial length escape value indicating 64-bit DWARF extension");
2453       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2454                             "FDE Length");
2455       ASM_OUTPUT_LABEL (asm_out_file, l1);
2456
2457       if (for_eh)
2458         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2459       else
2460         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2461                                debug_frame_section, "FDE CIE offset");
2462
2463       if (for_eh)
2464         {
2465           if (fde->dw_fde_switched_sections)
2466             {
2467               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2468                                       fde->dw_fde_unlikely_section_label);
2469               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2470                                       fde->dw_fde_hot_section_label);
2471               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2472               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2473               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2474                                                "FDE initial location");
2475               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2476                                     fde->dw_fde_hot_section_end_label,
2477                                     fde->dw_fde_hot_section_label,
2478                                     "FDE address range");
2479               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2480                                                "FDE initial location");
2481               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2482                                     fde->dw_fde_unlikely_section_end_label,
2483                                     fde->dw_fde_unlikely_section_label,
2484                                     "FDE address range");
2485             }
2486           else
2487             {
2488               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2489               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2490               dw2_asm_output_encoded_addr_rtx (fde_encoding,
2491                                                sym_ref,
2492                                                false,
2493                                                "FDE initial location");
2494               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2495                                     fde->dw_fde_end, fde->dw_fde_begin,
2496                                     "FDE address range");
2497             }
2498         }
2499       else
2500         {
2501           if (fde->dw_fde_switched_sections)
2502             {
2503               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2504                                    fde->dw_fde_hot_section_label,
2505                                    "FDE initial location");
2506               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2507                                     fde->dw_fde_hot_section_end_label,
2508                                     fde->dw_fde_hot_section_label,
2509                                     "FDE address range");
2510               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2511                                    fde->dw_fde_unlikely_section_label,
2512                                    "FDE initial location");
2513               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2514                                     fde->dw_fde_unlikely_section_end_label,
2515                                     fde->dw_fde_unlikely_section_label,
2516                                     "FDE address range");
2517             }
2518           else
2519             {
2520               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2521                                    "FDE initial location");
2522               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2523                                     fde->dw_fde_end, fde->dw_fde_begin,
2524                                     "FDE address range");
2525             }
2526         }
2527
2528       if (augmentation[0])
2529         {
2530           if (any_lsda_needed)
2531             {
2532               int size = size_of_encoded_value (lsda_encoding);
2533
2534               if (lsda_encoding == DW_EH_PE_aligned)
2535                 {
2536                   int offset = (  4             /* Length */
2537                                 + 4             /* CIE offset */
2538                                 + 2 * size_of_encoded_value (fde_encoding)
2539                                 + 1             /* Augmentation size */ );
2540                   int pad = -offset & (PTR_SIZE - 1);
2541
2542                   size += pad;
2543                   gcc_assert (size_of_uleb128 (size) == 1);
2544                 }
2545
2546               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2547
2548               if (fde->uses_eh_lsda)
2549                 {
2550                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2551                                                fde->funcdef_number);
2552                   dw2_asm_output_encoded_addr_rtx (
2553                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2554                         false, "Language Specific Data Area");
2555                 }
2556               else
2557                 {
2558                   if (lsda_encoding == DW_EH_PE_aligned)
2559                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2560                   dw2_asm_output_data
2561                     (size_of_encoded_value (lsda_encoding), 0,
2562                      "Language Specific Data Area (none)");
2563                 }
2564             }
2565           else
2566             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2567         }
2568
2569       /* Loop through the Call Frame Instructions associated with
2570          this FDE.  */
2571       fde->dw_fde_current_label = fde->dw_fde_begin;
2572       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2573         output_cfi (cfi, fde, for_eh);
2574
2575       /* Pad the FDE out to an address sized boundary.  */
2576       ASM_OUTPUT_ALIGN (asm_out_file,
2577                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2578       ASM_OUTPUT_LABEL (asm_out_file, l2);
2579     }
2580
2581   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2582     dw2_asm_output_data (4, 0, "End of Table");
2583 #ifdef MIPS_DEBUGGING_INFO
2584   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2585      get a value of 0.  Putting .align 0 after the label fixes it.  */
2586   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2587 #endif
2588
2589   /* Turn off app to make assembly quicker.  */
2590   if (flag_debug_asm)
2591     app_disable ();
2592 }
2593
2594 /* Output a marker (i.e. a label) for the beginning of a function, before
2595    the prologue.  */
2596
2597 void
2598 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2599                           const char *file ATTRIBUTE_UNUSED)
2600 {
2601   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2602   char * dup_label;
2603   dw_fde_ref fde;
2604
2605   current_function_func_begin_label = NULL;
2606
2607 #ifdef TARGET_UNWIND_INFO
2608   /* ??? current_function_func_begin_label is also used by except.c
2609      for call-site information.  We must emit this label if it might
2610      be used.  */
2611   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2612       && ! dwarf2out_do_frame ())
2613     return;
2614 #else
2615   if (! dwarf2out_do_frame ())
2616     return;
2617 #endif
2618
2619   switch_to_section (function_section (current_function_decl));
2620   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2621                                current_function_funcdef_no);
2622   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2623                           current_function_funcdef_no);
2624   dup_label = xstrdup (label);
2625   current_function_func_begin_label = dup_label;
2626
2627 #ifdef TARGET_UNWIND_INFO
2628   /* We can elide the fde allocation if we're not emitting debug info.  */
2629   if (! dwarf2out_do_frame ())
2630     return;
2631 #endif
2632
2633   /* Expand the fde table if necessary.  */
2634   if (fde_table_in_use == fde_table_allocated)
2635     {
2636       fde_table_allocated += FDE_TABLE_INCREMENT;
2637       fde_table = ggc_realloc (fde_table,
2638                                fde_table_allocated * sizeof (dw_fde_node));
2639       memset (fde_table + fde_table_in_use, 0,
2640               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2641     }
2642
2643   /* Record the FDE associated with this function.  */
2644   current_funcdef_fde = fde_table_in_use;
2645
2646   /* Add the new FDE at the end of the fde_table.  */
2647   fde = &fde_table[fde_table_in_use++];
2648   fde->decl = current_function_decl;
2649   fde->dw_fde_begin = dup_label;
2650   fde->dw_fde_current_label = dup_label;
2651   fde->dw_fde_hot_section_label = NULL;
2652   fde->dw_fde_hot_section_end_label = NULL;
2653   fde->dw_fde_unlikely_section_label = NULL;
2654   fde->dw_fde_unlikely_section_end_label = NULL;
2655   fde->dw_fde_switched_sections = false;
2656   fde->dw_fde_end = NULL;
2657   fde->dw_fde_cfi = NULL;
2658   fde->funcdef_number = current_function_funcdef_no;
2659   fde->nothrow = TREE_NOTHROW (current_function_decl);
2660   fde->uses_eh_lsda = crtl->uses_eh_lsda;
2661   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
2662
2663   args_size = old_args_size = 0;
2664
2665   /* We only want to output line number information for the genuine dwarf2
2666      prologue case, not the eh frame case.  */
2667 #ifdef DWARF2_DEBUGGING_INFO
2668   if (file)
2669     dwarf2out_source_line (line, file);
2670 #endif
2671 }
2672
2673 /* Output a marker (i.e. a label) for the absolute end of the generated code
2674    for a function definition.  This gets called *after* the epilogue code has
2675    been generated.  */
2676
2677 void
2678 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2679                         const char *file ATTRIBUTE_UNUSED)
2680 {
2681   dw_fde_ref fde;
2682   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2683
2684   /* Output a label to mark the endpoint of the code generated for this
2685      function.  */
2686   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2687                                current_function_funcdef_no);
2688   ASM_OUTPUT_LABEL (asm_out_file, label);
2689   fde = &fde_table[fde_table_in_use - 1];
2690   fde->dw_fde_end = xstrdup (label);
2691 }
2692
2693 void
2694 dwarf2out_frame_init (void)
2695 {
2696   /* Allocate the initial hunk of the fde_table.  */
2697   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2698   fde_table_allocated = FDE_TABLE_INCREMENT;
2699   fde_table_in_use = 0;
2700
2701   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2702      sake of lookup_cfa.  */
2703
2704   /* On entry, the Canonical Frame Address is at SP.  */
2705   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2706
2707 #ifdef DWARF2_UNWIND_INFO
2708   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2709     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2710 #endif
2711 }
2712
2713 void
2714 dwarf2out_frame_finish (void)
2715 {
2716   /* Output call frame information.  */
2717   if (DWARF2_FRAME_INFO)
2718     output_call_frame_info (0);
2719
2720 #ifndef TARGET_UNWIND_INFO
2721   /* Output another copy for the unwinder.  */
2722   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2723     output_call_frame_info (1);
2724 #endif
2725 }
2726
2727 /* Note that the current function section is being used for code.  */
2728
2729 static void
2730 dwarf2out_note_section_used (void)
2731 {
2732   section *sec = current_function_section ();
2733   if (sec == text_section)
2734     text_section_used = true;
2735   else if (sec == cold_text_section)
2736     cold_text_section_used = true;
2737 }
2738
2739 void
2740 dwarf2out_switch_text_section (void)
2741 {
2742   dw_fde_ref fde;
2743
2744   gcc_assert (cfun);
2745
2746   fde = &fde_table[fde_table_in_use - 1];
2747   fde->dw_fde_switched_sections = true;
2748   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
2749   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
2750   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
2751   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
2752   have_multiple_function_sections = true;
2753
2754   /* Reset the current label on switching text sections, so that we
2755      don't attempt to advance_loc4 between labels in different sections.  */
2756   fde->dw_fde_current_label = NULL;
2757
2758   /* There is no need to mark used sections when not debugging.  */
2759   if (cold_text_section != NULL)
2760     dwarf2out_note_section_used ();
2761 }
2762 #endif
2763 \f
2764 /* And now, the subset of the debugging information support code necessary
2765    for emitting location expressions.  */
2766
2767 /* Data about a single source file.  */
2768 struct dwarf_file_data GTY(())
2769 {
2770   const char * filename;
2771   int emitted_number;
2772 };
2773
2774 /* We need some way to distinguish DW_OP_addr with a direct symbol
2775    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2776 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2777
2778
2779 typedef struct dw_val_struct *dw_val_ref;
2780 typedef struct die_struct *dw_die_ref;
2781 typedef const struct die_struct *const_dw_die_ref;
2782 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2783 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2784
2785 /* Each DIE may have a series of attribute/value pairs.  Values
2786    can take on several forms.  The forms that are used in this
2787    implementation are listed below.  */
2788
2789 enum dw_val_class
2790 {
2791   dw_val_class_addr,
2792   dw_val_class_offset,
2793   dw_val_class_loc,
2794   dw_val_class_loc_list,
2795   dw_val_class_range_list,
2796   dw_val_class_const,
2797   dw_val_class_unsigned_const,
2798   dw_val_class_long_long,
2799   dw_val_class_vec,
2800   dw_val_class_flag,
2801   dw_val_class_die_ref,
2802   dw_val_class_fde_ref,
2803   dw_val_class_lbl_id,
2804   dw_val_class_lineptr,
2805   dw_val_class_str,
2806   dw_val_class_macptr,
2807   dw_val_class_file
2808 };
2809
2810 /* Describe a double word constant value.  */
2811 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2812
2813 typedef struct dw_long_long_struct GTY(())
2814 {
2815   unsigned long hi;
2816   unsigned long low;
2817 }
2818 dw_long_long_const;
2819
2820 /* Describe a floating point constant value, or a vector constant value.  */
2821
2822 typedef struct dw_vec_struct GTY(())
2823 {
2824   unsigned char * GTY((length ("%h.length"))) array;
2825   unsigned length;
2826   unsigned elt_size;
2827 }
2828 dw_vec_const;
2829
2830 /* The dw_val_node describes an attribute's value, as it is
2831    represented internally.  */
2832
2833 typedef struct dw_val_struct GTY(())
2834 {
2835   enum dw_val_class val_class;
2836   union dw_val_struct_union
2837     {
2838       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2839       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2840       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2841       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2842       HOST_WIDE_INT GTY ((default)) val_int;
2843       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2844       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2845       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2846       struct dw_val_die_union
2847         {
2848           dw_die_ref die;
2849           int external;
2850         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2851       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2852       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2853       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2854       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2855       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2856     }
2857   GTY ((desc ("%1.val_class"))) v;
2858 }
2859 dw_val_node;
2860
2861 /* Locations in memory are described using a sequence of stack machine
2862    operations.  */
2863
2864 typedef struct dw_loc_descr_struct GTY(())
2865 {
2866   dw_loc_descr_ref dw_loc_next;
2867   enum dwarf_location_atom dw_loc_opc;
2868   dw_val_node dw_loc_oprnd1;
2869   dw_val_node dw_loc_oprnd2;
2870   int dw_loc_addr;
2871 }
2872 dw_loc_descr_node;
2873
2874 /* Location lists are ranges + location descriptions for that range,
2875    so you can track variables that are in different places over
2876    their entire life.  */
2877 typedef struct dw_loc_list_struct GTY(())
2878 {
2879   dw_loc_list_ref dw_loc_next;
2880   const char *begin; /* Label for begin address of range */
2881   const char *end;  /* Label for end address of range */
2882   char *ll_symbol; /* Label for beginning of location list.
2883                       Only on head of list */
2884   const char *section; /* Section this loclist is relative to */
2885   dw_loc_descr_ref expr;
2886 } dw_loc_list_node;
2887
2888 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2889
2890 static const char *dwarf_stack_op_name (unsigned);
2891 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2892                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2893 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2894 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2895 static unsigned long size_of_locs (dw_loc_descr_ref);
2896 static void output_loc_operands (dw_loc_descr_ref);
2897 static void output_loc_sequence (dw_loc_descr_ref);
2898
2899 /* Convert a DWARF stack opcode into its string name.  */
2900
2901 static const char *
2902 dwarf_stack_op_name (unsigned int op)
2903 {
2904   switch (op)
2905     {
2906     case DW_OP_addr:
2907     case INTERNAL_DW_OP_tls_addr:
2908       return "DW_OP_addr";
2909     case DW_OP_deref:
2910       return "DW_OP_deref";
2911     case DW_OP_const1u:
2912       return "DW_OP_const1u";
2913     case DW_OP_const1s:
2914       return "DW_OP_const1s";
2915     case DW_OP_const2u:
2916       return "DW_OP_const2u";
2917     case DW_OP_const2s:
2918       return "DW_OP_const2s";
2919     case DW_OP_const4u:
2920       return "DW_OP_const4u";
2921     case DW_OP_const4s:
2922       return "DW_OP_const4s";
2923     case DW_OP_const8u:
2924       return "DW_OP_const8u";
2925     case DW_OP_const8s:
2926       return "DW_OP_const8s";
2927     case DW_OP_constu:
2928       return "DW_OP_constu";
2929     case DW_OP_consts:
2930       return "DW_OP_consts";
2931     case DW_OP_dup:
2932       return "DW_OP_dup";
2933     case DW_OP_drop:
2934       return "DW_OP_drop";
2935     case DW_OP_over:
2936       return "DW_OP_over";
2937     case DW_OP_pick:
2938       return "DW_OP_pick";
2939     case DW_OP_swap:
2940       return "DW_OP_swap";
2941     case DW_OP_rot:
2942       return "DW_OP_rot";
2943     case DW_OP_xderef:
2944       return "DW_OP_xderef";
2945     case DW_OP_abs:
2946       return "DW_OP_abs";
2947     case DW_OP_and:
2948       return "DW_OP_and";
2949     case DW_OP_div:
2950       return "DW_OP_div";
2951     case DW_OP_minus:
2952       return "DW_OP_minus";
2953     case DW_OP_mod:
2954       return "DW_OP_mod";
2955     case DW_OP_mul:
2956       return "DW_OP_mul";
2957     case DW_OP_neg:
2958       return "DW_OP_neg";
2959     case DW_OP_not:
2960       return "DW_OP_not";
2961     case DW_OP_or:
2962       return "DW_OP_or";
2963     case DW_OP_plus:
2964       return "DW_OP_plus";
2965     case DW_OP_plus_uconst:
2966       return "DW_OP_plus_uconst";
2967     case DW_OP_shl:
2968       return "DW_OP_shl";
2969     case DW_OP_shr:
2970       return "DW_OP_shr";
2971     case DW_OP_shra:
2972       return "DW_OP_shra";
2973     case DW_OP_xor:
2974       return "DW_OP_xor";
2975     case DW_OP_bra:
2976       return "DW_OP_bra";
2977     case DW_OP_eq:
2978       return "DW_OP_eq";
2979     case DW_OP_ge:
2980       return "DW_OP_ge";
2981     case DW_OP_gt:
2982       return "DW_OP_gt";
2983     case DW_OP_le:
2984       return "DW_OP_le";
2985     case DW_OP_lt:
2986       return "DW_OP_lt";
2987     case DW_OP_ne:
2988       return "DW_OP_ne";
2989     case DW_OP_skip:
2990       return "DW_OP_skip";
2991     case DW_OP_lit0:
2992       return "DW_OP_lit0";
2993     case DW_OP_lit1:
2994       return "DW_OP_lit1";
2995     case DW_OP_lit2:
2996       return "DW_OP_lit2";
2997     case DW_OP_lit3:
2998       return "DW_OP_lit3";
2999     case DW_OP_lit4:
3000       return "DW_OP_lit4";
3001     case DW_OP_lit5:
3002       return "DW_OP_lit5";
3003     case DW_OP_lit6:
3004       return "DW_OP_lit6";
3005     case DW_OP_lit7:
3006       return "DW_OP_lit7";
3007     case DW_OP_lit8:
3008       return "DW_OP_lit8";
3009     case DW_OP_lit9:
3010       return "DW_OP_lit9";
3011     case DW_OP_lit10:
3012       return "DW_OP_lit10";
3013     case DW_OP_lit11:
3014       return "DW_OP_lit11";
3015     case DW_OP_lit12:
3016       return "DW_OP_lit12";
3017     case DW_OP_lit13:
3018       return "DW_OP_lit13";
3019     case DW_OP_lit14:
3020       return "DW_OP_lit14";
3021     case DW_OP_lit15:
3022       return "DW_OP_lit15";
3023     case DW_OP_lit16:
3024       return "DW_OP_lit16";
3025     case DW_OP_lit17:
3026       return "DW_OP_lit17";
3027     case DW_OP_lit18:
3028       return "DW_OP_lit18";
3029     case DW_OP_lit19:
3030       return "DW_OP_lit19";
3031     case DW_OP_lit20:
3032       return "DW_OP_lit20";
3033     case DW_OP_lit21:
3034       return "DW_OP_lit21";
3035     case DW_OP_lit22:
3036       return "DW_OP_lit22";
3037     case DW_OP_lit23:
3038       return "DW_OP_lit23";
3039     case DW_OP_lit24:
3040       return "DW_OP_lit24";
3041     case DW_OP_lit25:
3042       return "DW_OP_lit25";
3043     case DW_OP_lit26:
3044       return "DW_OP_lit26";
3045     case DW_OP_lit27:
3046       return "DW_OP_lit27";
3047     case DW_OP_lit28:
3048       return "DW_OP_lit28";
3049     case DW_OP_lit29:
3050       return "DW_OP_lit29";
3051     case DW_OP_lit30:
3052       return "DW_OP_lit30";
3053     case DW_OP_lit31:
3054       return "DW_OP_lit31";
3055     case DW_OP_reg0:
3056       return "DW_OP_reg0";
3057     case DW_OP_reg1:
3058       return "DW_OP_reg1";
3059     case DW_OP_reg2:
3060       return "DW_OP_reg2";
3061     case DW_OP_reg3:
3062       return "DW_OP_reg3";
3063     case DW_OP_reg4:
3064       return "DW_OP_reg4";
3065     case DW_OP_reg5:
3066       return "DW_OP_reg5";
3067     case DW_OP_reg6:
3068       return "DW_OP_reg6";
3069     case DW_OP_reg7:
3070       return "DW_OP_reg7";
3071     case DW_OP_reg8:
3072       return "DW_OP_reg8";
3073     case DW_OP_reg9:
3074       return "DW_OP_reg9";
3075     case DW_OP_reg10:
3076       return "DW_OP_reg10";
3077     case DW_OP_reg11:
3078       return "DW_OP_reg11";
3079     case DW_OP_reg12:
3080       return "DW_OP_reg12";
3081     case DW_OP_reg13:
3082       return "DW_OP_reg13";
3083     case DW_OP_reg14:
3084       return "DW_OP_reg14";
3085     case DW_OP_reg15:
3086       return "DW_OP_reg15";
3087     case DW_OP_reg16:
3088       return "DW_OP_reg16";
3089     case DW_OP_reg17:
3090       return "DW_OP_reg17";
3091     case DW_OP_reg18:
3092       return "DW_OP_reg18";
3093     case DW_OP_reg19:
3094       return "DW_OP_reg19";
3095     case DW_OP_reg20:
3096       return "DW_OP_reg20";
3097     case DW_OP_reg21:
3098       return "DW_OP_reg21";
3099     case DW_OP_reg22:
3100       return "DW_OP_reg22";
3101     case DW_OP_reg23:
3102       return "DW_OP_reg23";
3103     case DW_OP_reg24:
3104       return "DW_OP_reg24";
3105     case DW_OP_reg25:
3106       return "DW_OP_reg25";
3107     case DW_OP_reg26:
3108       return "DW_OP_reg26";
3109     case DW_OP_reg27:
3110       return "DW_OP_reg27";
3111     case DW_OP_reg28:
3112       return "DW_OP_reg28";
3113     case DW_OP_reg29:
3114       return "DW_OP_reg29";
3115     case DW_OP_reg30:
3116       return "DW_OP_reg30";
3117     case DW_OP_reg31:
3118       return "DW_OP_reg31";
3119     case DW_OP_breg0:
3120       return "DW_OP_breg0";
3121     case DW_OP_breg1:
3122       return "DW_OP_breg1";
3123     case DW_OP_breg2:
3124       return "DW_OP_breg2";
3125     case DW_OP_breg3:
3126       return "DW_OP_breg3";
3127     case DW_OP_breg4:
3128       return "DW_OP_breg4";
3129     case DW_OP_breg5:
3130       return "DW_OP_breg5";
3131     case DW_OP_breg6:
3132       return "DW_OP_breg6";
3133     case DW_OP_breg7:
3134       return "DW_OP_breg7";
3135     case DW_OP_breg8:
3136       return "DW_OP_breg8";
3137     case DW_OP_breg9:
3138       return "DW_OP_breg9";
3139     case DW_OP_breg10:
3140       return "DW_OP_breg10";
3141     case DW_OP_breg11:
3142       return "DW_OP_breg11";
3143     case DW_OP_breg12:
3144       return "DW_OP_breg12";
3145     case DW_OP_breg13:
3146       return "DW_OP_breg13";
3147     case DW_OP_breg14:
3148       return "DW_OP_breg14";
3149     case DW_OP_breg15:
3150       return "DW_OP_breg15";
3151     case DW_OP_breg16:
3152       return "DW_OP_breg16";
3153     case DW_OP_breg17:
3154       return "DW_OP_breg17";
3155     case DW_OP_breg18:
3156       return "DW_OP_breg18";
3157     case DW_OP_breg19:
3158       return "DW_OP_breg19";
3159     case DW_OP_breg20:
3160       return "DW_OP_breg20";
3161     case DW_OP_breg21:
3162       return "DW_OP_breg21";
3163     case DW_OP_breg22:
3164       return "DW_OP_breg22";
3165     case DW_OP_breg23:
3166       return "DW_OP_breg23";
3167     case DW_OP_breg24:
3168       return "DW_OP_breg24";
3169     case DW_OP_breg25:
3170       return "DW_OP_breg25";
3171     case DW_OP_breg26:
3172       return "DW_OP_breg26";
3173     case DW_OP_breg27:
3174       return "DW_OP_breg27";
3175     case DW_OP_breg28:
3176       return "DW_OP_breg28";
3177     case DW_OP_breg29:
3178       return "DW_OP_breg29";
3179     case DW_OP_breg30:
3180       return "DW_OP_breg30";
3181     case DW_OP_breg31:
3182       return "DW_OP_breg31";
3183     case DW_OP_regx:
3184       return "DW_OP_regx";
3185     case DW_OP_fbreg:
3186       return "DW_OP_fbreg";
3187     case DW_OP_bregx:
3188       return "DW_OP_bregx";
3189     case DW_OP_piece:
3190       return "DW_OP_piece";
3191     case DW_OP_deref_size:
3192       return "DW_OP_deref_size";
3193     case DW_OP_xderef_size:
3194       return "DW_OP_xderef_size";
3195     case DW_OP_nop:
3196       return "DW_OP_nop";
3197     case DW_OP_push_object_address:
3198       return "DW_OP_push_object_address";
3199     case DW_OP_call2:
3200       return "DW_OP_call2";
3201     case DW_OP_call4:
3202       return "DW_OP_call4";
3203     case DW_OP_call_ref:
3204       return "DW_OP_call_ref";
3205     case DW_OP_GNU_push_tls_address:
3206       return "DW_OP_GNU_push_tls_address";
3207     case DW_OP_GNU_uninit:
3208       return "DW_OP_GNU_uninit";
3209     default:
3210       return "OP_<unknown>";
3211     }
3212 }
3213
3214 /* Return a pointer to a newly allocated location description.  Location
3215    descriptions are simple expression terms that can be strung
3216    together to form more complicated location (address) descriptions.  */
3217
3218 static inline dw_loc_descr_ref
3219 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3220                unsigned HOST_WIDE_INT oprnd2)
3221 {
3222   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3223
3224   descr->dw_loc_opc = op;
3225   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3226   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3227   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3228   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3229
3230   return descr;
3231 }
3232
3233 /* Add a location description term to a location description expression.  */
3234
3235 static inline void
3236 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3237 {
3238   dw_loc_descr_ref *d;
3239
3240   /* Find the end of the chain.  */
3241   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3242     ;
3243
3244   *d = descr;
3245 }
3246
3247 /* Return the size of a location descriptor.  */
3248
3249 static unsigned long
3250 size_of_loc_descr (dw_loc_descr_ref loc)
3251 {
3252   unsigned long size = 1;
3253
3254   switch (loc->dw_loc_opc)
3255     {
3256     case DW_OP_addr:
3257     case INTERNAL_DW_OP_tls_addr:
3258       size += DWARF2_ADDR_SIZE;
3259       break;
3260     case DW_OP_const1u:
3261     case DW_OP_const1s:
3262       size += 1;
3263       break;
3264     case DW_OP_const2u:
3265     case DW_OP_const2s:
3266       size += 2;
3267       break;
3268     case DW_OP_const4u:
3269     case DW_OP_const4s:
3270       size += 4;
3271       break;
3272     case DW_OP_const8u:
3273     case DW_OP_const8s:
3274       size += 8;
3275       break;
3276     case DW_OP_constu:
3277       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3278       break;
3279     case DW_OP_consts:
3280       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3281       break;
3282     case DW_OP_pick:
3283       size += 1;
3284       break;
3285     case DW_OP_plus_uconst:
3286       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3287       break;
3288     case DW_OP_skip:
3289     case DW_OP_bra:
3290       size += 2;
3291       break;
3292     case DW_OP_breg0:
3293     case DW_OP_breg1:
3294     case DW_OP_breg2:
3295     case DW_OP_breg3:
3296     case DW_OP_breg4:
3297     case DW_OP_breg5:
3298     case DW_OP_breg6:
3299     case DW_OP_breg7:
3300     case DW_OP_breg8:
3301     case DW_OP_breg9:
3302     case DW_OP_breg10:
3303     case DW_OP_breg11:
3304     case DW_OP_breg12:
3305     case DW_OP_breg13:
3306     case DW_OP_breg14:
3307     case DW_OP_breg15:
3308     case DW_OP_breg16:
3309     case DW_OP_breg17:
3310     case DW_OP_breg18:
3311     case DW_OP_breg19:
3312     case DW_OP_breg20:
3313     case DW_OP_breg21:
3314     case DW_OP_breg22:
3315     case DW_OP_breg23:
3316     case DW_OP_breg24:
3317     case DW_OP_breg25:
3318     case DW_OP_breg26:
3319     case DW_OP_breg27:
3320     case DW_OP_breg28:
3321     case DW_OP_breg29:
3322     case DW_OP_breg30:
3323     case DW_OP_breg31:
3324       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3325       break;
3326     case DW_OP_regx:
3327       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3328       break;
3329     case DW_OP_fbreg:
3330       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3331       break;
3332     case DW_OP_bregx:
3333       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3334       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3335       break;
3336     case DW_OP_piece:
3337       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3338       break;
3339     case DW_OP_deref_size:
3340     case DW_OP_xderef_size:
3341       size += 1;
3342       break;
3343     case DW_OP_call2:
3344       size += 2;
3345       break;
3346     case DW_OP_call4:
3347       size += 4;
3348       break;
3349     case DW_OP_call_ref:
3350       size += DWARF2_ADDR_SIZE;
3351       break;
3352     default:
3353       break;
3354     }
3355
3356   return size;
3357 }
3358
3359 /* Return the size of a series of location descriptors.  */
3360
3361 static unsigned long
3362 size_of_locs (dw_loc_descr_ref loc)
3363 {
3364   dw_loc_descr_ref l;
3365   unsigned long size;
3366
3367   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3368      field, to avoid writing to a PCH file.  */
3369   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3370     {
3371       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3372         break;
3373       size += size_of_loc_descr (l);
3374     }
3375   if (! l)
3376     return size;
3377
3378   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3379     {
3380       l->dw_loc_addr = size;
3381       size += size_of_loc_descr (l);
3382     }
3383
3384   return size;
3385 }
3386
3387 /* Output location description stack opcode's operands (if any).  */
3388
3389 static void
3390 output_loc_operands (dw_loc_descr_ref loc)
3391 {
3392   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3393   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3394
3395   switch (loc->dw_loc_opc)
3396     {
3397 #ifdef DWARF2_DEBUGGING_INFO
3398     case DW_OP_addr:
3399       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3400       break;
3401     case DW_OP_const2u:
3402     case DW_OP_const2s:
3403       dw2_asm_output_data (2, val1->v.val_int, NULL);
3404       break;
3405     case DW_OP_const4u:
3406     case DW_OP_const4s:
3407       dw2_asm_output_data (4, val1->v.val_int, NULL);
3408       break;
3409     case DW_OP_const8u:
3410     case DW_OP_const8s:
3411       gcc_assert (HOST_BITS_PER_LONG >= 64);
3412       dw2_asm_output_data (8, val1->v.val_int, NULL);
3413       break;
3414     case DW_OP_skip:
3415     case DW_OP_bra:
3416       {
3417         int offset;
3418
3419         gcc_assert (val1->val_class == dw_val_class_loc);
3420         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3421
3422         dw2_asm_output_data (2, offset, NULL);
3423       }
3424       break;
3425 #else
3426     case DW_OP_addr:
3427     case DW_OP_const2u:
3428     case DW_OP_const2s:
3429     case DW_OP_const4u:
3430     case DW_OP_const4s:
3431     case DW_OP_const8u:
3432     case DW_OP_const8s:
3433     case DW_OP_skip:
3434     case DW_OP_bra:
3435       /* We currently don't make any attempt to make sure these are
3436          aligned properly like we do for the main unwind info, so
3437          don't support emitting things larger than a byte if we're
3438          only doing unwinding.  */
3439       gcc_unreachable ();
3440 #endif
3441     case DW_OP_const1u:
3442     case DW_OP_const1s:
3443       dw2_asm_output_data (1, val1->v.val_int, NULL);
3444       break;
3445     case DW_OP_constu:
3446       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3447       break;
3448     case DW_OP_consts:
3449       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3450       break;
3451     case DW_OP_pick:
3452       dw2_asm_output_data (1, val1->v.val_int, NULL);
3453       break;
3454     case DW_OP_plus_uconst:
3455       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3456       break;
3457     case DW_OP_breg0:
3458     case DW_OP_breg1:
3459     case DW_OP_breg2:
3460     case DW_OP_breg3:
3461     case DW_OP_breg4:
3462     case DW_OP_breg5:
3463     case DW_OP_breg6:
3464     case DW_OP_breg7:
3465     case DW_OP_breg8:
3466     case DW_OP_breg9:
3467     case DW_OP_breg10:
3468     case DW_OP_breg11:
3469     case DW_OP_breg12:
3470     case DW_OP_breg13:
3471     case DW_OP_breg14:
3472     case DW_OP_breg15:
3473     case DW_OP_breg16:
3474     case DW_OP_breg17:
3475     case DW_OP_breg18:
3476     case DW_OP_breg19:
3477     case DW_OP_breg20:
3478     case DW_OP_breg21:
3479     case DW_OP_breg22:
3480     case DW_OP_breg23:
3481     case DW_OP_breg24:
3482     case DW_OP_breg25:
3483     case DW_OP_breg26:
3484     case DW_OP_breg27:
3485     case DW_OP_breg28:
3486     case DW_OP_breg29:
3487     case DW_OP_breg30:
3488     case DW_OP_breg31:
3489       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3490       break;
3491     case DW_OP_regx:
3492       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3493       break;
3494     case DW_OP_fbreg:
3495       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3496       break;
3497     case DW_OP_bregx:
3498       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3499       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3500       break;
3501     case DW_OP_piece:
3502       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3503       break;
3504     case DW_OP_deref_size:
3505     case DW_OP_xderef_size:
3506       dw2_asm_output_data (1, val1->v.val_int, NULL);
3507       break;
3508
3509     case INTERNAL_DW_OP_tls_addr:
3510       if (targetm.asm_out.output_dwarf_dtprel)
3511         {
3512           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3513                                                DWARF2_ADDR_SIZE,
3514                                                val1->v.val_addr);
3515           fputc ('\n', asm_out_file);
3516         }
3517       else
3518         gcc_unreachable ();
3519       break;
3520
3521     default:
3522       /* Other codes have no operands.  */
3523       break;
3524     }
3525 }
3526
3527 /* Output a sequence of location operations.  */
3528
3529 static void
3530 output_loc_sequence (dw_loc_descr_ref loc)
3531 {
3532   for (; loc != NULL; loc = loc->dw_loc_next)
3533     {
3534       /* Output the opcode.  */
3535       dw2_asm_output_data (1, loc->dw_loc_opc,
3536                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3537
3538       /* Output the operand(s) (if any).  */
3539       output_loc_operands (loc);
3540     }
3541 }
3542
3543 /* This routine will generate the correct assembly data for a location
3544    description based on a cfi entry with a complex address.  */
3545
3546 static void
3547 output_cfa_loc (dw_cfi_ref cfi)
3548 {
3549   dw_loc_descr_ref loc;
3550   unsigned long size;
3551
3552   /* Output the size of the block.  */
3553   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3554   size = size_of_locs (loc);
3555   dw2_asm_output_data_uleb128 (size, NULL);
3556
3557   /* Now output the operations themselves.  */
3558   output_loc_sequence (loc);
3559 }
3560
3561 /* This function builds a dwarf location descriptor sequence from a
3562    dw_cfa_location, adding the given OFFSET to the result of the
3563    expression.  */
3564
3565 static struct dw_loc_descr_struct *
3566 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3567 {
3568   struct dw_loc_descr_struct *head, *tmp;
3569
3570   offset += cfa->offset;
3571
3572   if (cfa->indirect)
3573     {
3574       if (cfa->base_offset)
3575         {
3576           if (cfa->reg <= 31)
3577             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3578           else
3579             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3580         }
3581       else if (cfa->reg <= 31)
3582         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3583       else
3584         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3585
3586       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3587       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3588       add_loc_descr (&head, tmp);
3589       if (offset != 0)
3590         {
3591           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3592           add_loc_descr (&head, tmp);
3593         }
3594     }
3595   else
3596     {
3597       if (offset == 0)
3598         if (cfa->reg <= 31)
3599           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3600         else
3601           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3602       else if (cfa->reg <= 31)
3603         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3604       else
3605         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3606     }
3607
3608   return head;
3609 }
3610
3611 /* This function fills in aa dw_cfa_location structure from a dwarf location
3612    descriptor sequence.  */
3613
3614 static void
3615 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3616 {
3617   struct dw_loc_descr_struct *ptr;
3618   cfa->offset = 0;
3619   cfa->base_offset = 0;
3620   cfa->indirect = 0;
3621   cfa->reg = -1;
3622
3623   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3624     {
3625       enum dwarf_location_atom op = ptr->dw_loc_opc;
3626
3627       switch (op)
3628         {
3629         case DW_OP_reg0:
3630         case DW_OP_reg1:
3631         case DW_OP_reg2:
3632         case DW_OP_reg3:
3633         case DW_OP_reg4:
3634         case DW_OP_reg5:
3635         case DW_OP_reg6:
3636         case DW_OP_reg7:
3637         case DW_OP_reg8:
3638         case DW_OP_reg9:
3639         case DW_OP_reg10:
3640         case DW_OP_reg11:
3641         case DW_OP_reg12:
3642         case DW_OP_reg13:
3643         case DW_OP_reg14:
3644         case DW_OP_reg15:
3645         case DW_OP_reg16:
3646         case DW_OP_reg17:
3647         case DW_OP_reg18:
3648         case DW_OP_reg19:
3649         case DW_OP_reg20:
3650         case DW_OP_reg21:
3651         case DW_OP_reg22:
3652         case DW_OP_reg23:
3653         case DW_OP_reg24:
3654         case DW_OP_reg25:
3655         case DW_OP_reg26:
3656         case DW_OP_reg27:
3657         case DW_OP_reg28:
3658         case DW_OP_reg29:
3659         case DW_OP_reg30:
3660         case DW_OP_reg31:
3661           cfa->reg = op - DW_OP_reg0;
3662           break;
3663         case DW_OP_regx:
3664           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3665           break;
3666         case DW_OP_breg0:
3667         case DW_OP_breg1:
3668         case DW_OP_breg2:
3669         case DW_OP_breg3:
3670         case DW_OP_breg4:
3671         case DW_OP_breg5:
3672         case DW_OP_breg6:
3673         case DW_OP_breg7:
3674         case DW_OP_breg8:
3675         case DW_OP_breg9:
3676         case DW_OP_breg10:
3677         case DW_OP_breg11:
3678         case DW_OP_breg12:
3679         case DW_OP_breg13:
3680         case DW_OP_breg14:
3681         case DW_OP_breg15:
3682         case DW_OP_breg16:
3683         case DW_OP_breg17:
3684         case DW_OP_breg18:
3685         case DW_OP_breg19:
3686         case DW_OP_breg20:
3687         case DW_OP_breg21:
3688         case DW_OP_breg22:
3689         case DW_OP_breg23:
3690         case DW_OP_breg24:
3691         case DW_OP_breg25:
3692         case DW_OP_breg26:
3693         case DW_OP_breg27:
3694         case DW_OP_breg28:
3695         case DW_OP_breg29:
3696         case DW_OP_breg30:
3697         case DW_OP_breg31:
3698           cfa->reg = op - DW_OP_breg0;
3699           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3700           break;
3701         case DW_OP_bregx:
3702           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3703           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3704           break;
3705         case DW_OP_deref:
3706           cfa->indirect = 1;
3707           break;
3708         case DW_OP_plus_uconst:
3709           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3710           break;
3711         default:
3712           internal_error ("DW_LOC_OP %s not implemented",
3713                           dwarf_stack_op_name (ptr->dw_loc_opc));
3714         }
3715     }
3716 }
3717 #endif /* .debug_frame support */
3718 \f
3719 /* And now, the support for symbolic debugging information.  */
3720 #ifdef DWARF2_DEBUGGING_INFO
3721
3722 /* .debug_str support.  */
3723 static int output_indirect_string (void **, void *);
3724
3725 static void dwarf2out_init (const char *);
3726 static void dwarf2out_finish (const char *);
3727 static void dwarf2out_define (unsigned int, const char *);
3728 static void dwarf2out_undef (unsigned int, const char *);
3729 static void dwarf2out_start_source_file (unsigned, const char *);
3730 static void dwarf2out_end_source_file (unsigned);
3731 static void dwarf2out_begin_block (unsigned, unsigned);
3732 static void dwarf2out_end_block (unsigned, unsigned);
3733 static bool dwarf2out_ignore_block (const_tree);
3734 static void dwarf2out_global_decl (tree);
3735 static void dwarf2out_type_decl (tree, int);
3736 static void dwarf2out_imported_module_or_decl (tree, tree);
3737 static void dwarf2out_abstract_function (tree);
3738 static void dwarf2out_var_location (rtx);
3739 static void dwarf2out_begin_function (tree);
3740
3741 /* The debug hooks structure.  */
3742
3743 const struct gcc_debug_hooks dwarf2_debug_hooks =
3744 {
3745   dwarf2out_init,
3746   dwarf2out_finish,
3747   dwarf2out_define,
3748   dwarf2out_undef,
3749   dwarf2out_start_source_file,
3750   dwarf2out_end_source_file,
3751   dwarf2out_begin_block,
3752   dwarf2out_end_block,
3753   dwarf2out_ignore_block,
3754   dwarf2out_source_line,
3755   dwarf2out_begin_prologue,
3756   debug_nothing_int_charstar,   /* end_prologue */
3757   dwarf2out_end_epilogue,
3758   dwarf2out_begin_function,
3759   debug_nothing_int,            /* end_function */
3760   dwarf2out_decl,               /* function_decl */
3761   dwarf2out_global_decl,
3762   dwarf2out_type_decl,          /* type_decl */
3763   dwarf2out_imported_module_or_decl,
3764   debug_nothing_tree,           /* deferred_inline_function */
3765   /* The DWARF 2 backend tries to reduce debugging bloat by not
3766      emitting the abstract description of inline functions until
3767      something tries to reference them.  */
3768   dwarf2out_abstract_function,  /* outlining_inline_function */
3769   debug_nothing_rtx,            /* label */
3770   debug_nothing_int,            /* handle_pch */
3771   dwarf2out_var_location,
3772   dwarf2out_switch_text_section,
3773   1                             /* start_end_main_source_file */
3774 };
3775 #endif
3776 \f
3777 /* NOTE: In the comments in this file, many references are made to
3778    "Debugging Information Entries".  This term is abbreviated as `DIE'
3779    throughout the remainder of this file.  */
3780
3781 /* An internal representation of the DWARF output is built, and then
3782    walked to generate the DWARF debugging info.  The walk of the internal
3783    representation is done after the entire program has been compiled.
3784    The types below are used to describe the internal representation.  */
3785
3786 /* Various DIE's use offsets relative to the beginning of the
3787    .debug_info section to refer to each other.  */
3788
3789 typedef long int dw_offset;
3790
3791 /* Define typedefs here to avoid circular dependencies.  */
3792
3793 typedef struct dw_attr_struct *dw_attr_ref;
3794 typedef struct dw_line_info_struct *dw_line_info_ref;
3795 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3796 typedef struct pubname_struct *pubname_ref;
3797 typedef struct dw_ranges_struct *dw_ranges_ref;
3798 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3799
3800 /* Each entry in the line_info_table maintains the file and
3801    line number associated with the label generated for that
3802    entry.  The label gives the PC value associated with
3803    the line number entry.  */
3804
3805 typedef struct dw_line_info_struct GTY(())
3806 {
3807   unsigned long dw_file_num;
3808   unsigned long dw_line_num;
3809 }
3810 dw_line_info_entry;
3811
3812 /* Line information for functions in separate sections; each one gets its
3813    own sequence.  */
3814 typedef struct dw_separate_line_info_struct GTY(())
3815 {
3816   unsigned long dw_file_num;
3817   unsigned long dw_line_num;
3818   unsigned long function;
3819 }
3820 dw_separate_line_info_entry;
3821
3822 /* Each DIE attribute has a field specifying the attribute kind,
3823    a link to the next attribute in the chain, and an attribute value.
3824    Attributes are typically linked below the DIE they modify.  */
3825
3826 typedef struct dw_attr_struct GTY(())
3827 {
3828   enum dwarf_attribute dw_attr;
3829   dw_val_node dw_attr_val;
3830 }
3831 dw_attr_node;
3832
3833 DEF_VEC_O(dw_attr_node);
3834 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3835
3836 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3837    The children of each node form a circular list linked by
3838    die_sib.  die_child points to the node *before* the "first" child node.  */
3839
3840 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
3841 {
3842   enum dwarf_tag die_tag;
3843   char *die_symbol;
3844   VEC(dw_attr_node,gc) * die_attr;
3845   dw_die_ref die_parent;
3846   dw_die_ref die_child;
3847   dw_die_ref die_sib;
3848   dw_die_ref die_definition; /* ref from a specification to its definition */
3849   dw_offset die_offset;
3850   unsigned long die_abbrev;
3851   int die_mark;
3852   /* Die is used and must not be pruned as unused.  */
3853   int die_perennial_p;
3854   unsigned int decl_id;
3855 }
3856 die_node;
3857
3858 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3859 #define FOR_EACH_CHILD(die, c, expr) do {       \
3860   c = die->die_child;                           \
3861   if (c) do {                                   \
3862     c = c->die_sib;                             \
3863     expr;                                       \
3864   } while (c != die->die_child);                \
3865 } while (0)
3866
3867 /* The pubname structure */
3868
3869 typedef struct pubname_struct GTY(())
3870 {
3871   dw_die_ref die;
3872   const char *name;
3873 }
3874 pubname_entry;
3875
3876 DEF_VEC_O(pubname_entry);
3877 DEF_VEC_ALLOC_O(pubname_entry, gc);
3878
3879 struct dw_ranges_struct GTY(())
3880 {
3881   /* If this is positive, it's a block number, otherwise it's a
3882      bitwise-negated index into dw_ranges_by_label.  */
3883   int num;
3884 };
3885
3886 struct dw_ranges_by_label_struct GTY(())
3887 {
3888   const char *begin;
3889   const char *end;
3890 };
3891
3892 /* The limbo die list structure.  */
3893 typedef struct limbo_die_struct GTY(())
3894 {
3895   dw_die_ref die;
3896   tree created_for;
3897   struct limbo_die_struct *next;
3898 }
3899 limbo_die_node;
3900
3901 /* How to start an assembler comment.  */
3902 #ifndef ASM_COMMENT_START
3903 #define ASM_COMMENT_START ";#"
3904 #endif
3905
3906 /* Define a macro which returns nonzero for a TYPE_DECL which was
3907    implicitly generated for a tagged type.
3908
3909    Note that unlike the gcc front end (which generates a NULL named
3910    TYPE_DECL node for each complete tagged type, each array type, and
3911    each function type node created) the g++ front end generates a
3912    _named_ TYPE_DECL node for each tagged type node created.
3913    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3914    generate a DW_TAG_typedef DIE for them.  */
3915
3916 #define TYPE_DECL_IS_STUB(decl)                         \
3917   (DECL_NAME (decl) == NULL_TREE                        \
3918    || (DECL_ARTIFICIAL (decl)                           \
3919        && is_tagged_type (TREE_TYPE (decl))             \
3920        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3921            /* This is necessary for stub decls that     \
3922               appear in nested inline functions.  */    \
3923            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3924                && (decl_ultimate_origin (decl)          \
3925                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3926
3927 /* Information concerning the compilation unit's programming
3928    language, and compiler version.  */
3929
3930 /* Fixed size portion of the DWARF compilation unit header.  */
3931 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3932   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3933
3934 /* Fixed size portion of public names info.  */
3935 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3936
3937 /* Fixed size portion of the address range info.  */
3938 #define DWARF_ARANGES_HEADER_SIZE                                       \
3939   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3940                 DWARF2_ADDR_SIZE * 2)                                   \
3941    - DWARF_INITIAL_LENGTH_SIZE)
3942
3943 /* Size of padding portion in the address range info.  It must be
3944    aligned to twice the pointer size.  */
3945 #define DWARF_ARANGES_PAD_SIZE \
3946   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3947                 DWARF2_ADDR_SIZE * 2)                              \
3948    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3949
3950 /* Use assembler line directives if available.  */
3951 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3952 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3953 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3954 #else
3955 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3956 #endif
3957 #endif
3958
3959 /* Minimum line offset in a special line info. opcode.
3960    This value was chosen to give a reasonable range of values.  */
3961 #define DWARF_LINE_BASE  -10
3962
3963 /* First special line opcode - leave room for the standard opcodes.  */
3964 #define DWARF_LINE_OPCODE_BASE  10
3965
3966 /* Range of line offsets in a special line info. opcode.  */
3967 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3968
3969 /* Flag that indicates the initial value of the is_stmt_start flag.
3970    In the present implementation, we do not mark any lines as
3971    the beginning of a source statement, because that information
3972    is not made available by the GCC front-end.  */
3973 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3974
3975 #ifdef DWARF2_DEBUGGING_INFO
3976 /* This location is used by calc_die_sizes() to keep track
3977    the offset of each DIE within the .debug_info section.  */
3978 static unsigned long next_die_offset;
3979 #endif
3980
3981 /* Record the root of the DIE's built for the current compilation unit.  */
3982 static GTY(()) dw_die_ref comp_unit_die;
3983
3984 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3985 static GTY(()) limbo_die_node *limbo_die_list;
3986
3987 /* Filenames referenced by this compilation unit.  */
3988 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3989
3990 /* A hash table of references to DIE's that describe declarations.
3991    The key is a DECL_UID() which is a unique number identifying each decl.  */
3992 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3993
3994 /* Node of the variable location list.  */
3995 struct var_loc_node GTY ((chain_next ("%h.next")))
3996 {
3997   rtx GTY (()) var_loc_note;
3998   const char * GTY (()) label;
3999   const char * GTY (()) section_label;
4000   struct var_loc_node * GTY (()) next;
4001 };
4002
4003 /* Variable location list.  */
4004 struct var_loc_list_def GTY (())
4005 {
4006   struct var_loc_node * GTY (()) first;
4007
4008   /* Do not mark the last element of the chained list because
4009      it is marked through the chain.  */
4010   struct var_loc_node * GTY ((skip ("%h"))) last;
4011
4012   /* DECL_UID of the variable decl.  */
4013   unsigned int decl_id;
4014 };
4015 typedef struct var_loc_list_def var_loc_list;
4016
4017
4018 /* Table of decl location linked lists.  */
4019 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4020
4021 /* A pointer to the base of a list of references to DIE's that
4022    are uniquely identified by their tag, presence/absence of
4023    children DIE's, and list of attribute/value pairs.  */
4024 static GTY((length ("abbrev_die_table_allocated")))
4025   dw_die_ref *abbrev_die_table;
4026
4027 /* Number of elements currently allocated for abbrev_die_table.  */
4028 static GTY(()) unsigned abbrev_die_table_allocated;
4029
4030 /* Number of elements in type_die_table currently in use.  */
4031 static GTY(()) unsigned abbrev_die_table_in_use;
4032
4033 /* Size (in elements) of increments by which we may expand the
4034    abbrev_die_table.  */
4035 #define ABBREV_DIE_TABLE_INCREMENT 256
4036
4037 /* A pointer to the base of a table that contains line information
4038    for each source code line in .text in the compilation unit.  */
4039 static GTY((length ("line_info_table_allocated")))
4040      dw_line_info_ref line_info_table;
4041
4042 /* Number of elements currently allocated for line_info_table.  */
4043 static GTY(()) unsigned line_info_table_allocated;
4044
4045 /* Number of elements in line_info_table currently in use.  */
4046 static GTY(()) unsigned line_info_table_in_use;
4047
4048 /* A pointer to the base of a table that contains line information
4049    for each source code line outside of .text in the compilation unit.  */
4050 static GTY ((length ("separate_line_info_table_allocated")))
4051      dw_separate_line_info_ref separate_line_info_table;
4052
4053 /* Number of elements currently allocated for separate_line_info_table.  */
4054 static GTY(()) unsigned separate_line_info_table_allocated;
4055
4056 /* Number of elements in separate_line_info_table currently in use.  */
4057 static GTY(()) unsigned separate_line_info_table_in_use;
4058
4059 /* Size (in elements) of increments by which we may expand the
4060    line_info_table.  */
4061 #define LINE_INFO_TABLE_INCREMENT 1024
4062
4063 /* A pointer to the base of a table that contains a list of publicly
4064    accessible names.  */
4065 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4066
4067 /* A pointer to the base of a table that contains a list of publicly
4068    accessible types.  */
4069 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4070
4071 /* Array of dies for which we should generate .debug_arange info.  */
4072 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4073
4074 /* Number of elements currently allocated for arange_table.  */
4075 static GTY(()) unsigned arange_table_allocated;
4076
4077 /* Number of elements in arange_table currently in use.  */
4078 static GTY(()) unsigned arange_table_in_use;
4079
4080 /* Size (in elements) of increments by which we may expand the
4081    arange_table.  */
4082 #define ARANGE_TABLE_INCREMENT 64
4083
4084 /* Array of dies for which we should generate .debug_ranges info.  */
4085 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4086
4087 /* Number of elements currently allocated for ranges_table.  */
4088 static GTY(()) unsigned ranges_table_allocated;
4089
4090 /* Number of elements in ranges_table currently in use.  */
4091 static GTY(()) unsigned ranges_table_in_use;
4092
4093 /* Array of pairs of labels referenced in ranges_table.  */
4094 static GTY ((length ("ranges_by_label_allocated")))
4095      dw_ranges_by_label_ref ranges_by_label;
4096
4097 /* Number of elements currently allocated for ranges_by_label.  */
4098 static GTY(()) unsigned ranges_by_label_allocated;
4099
4100 /* Number of elements in ranges_by_label currently in use.  */
4101 static GTY(()) unsigned ranges_by_label_in_use;
4102
4103 /* Size (in elements) of increments by which we may expand the
4104    ranges_table.  */
4105 #define RANGES_TABLE_INCREMENT 64
4106
4107 /* Whether we have location lists that need outputting */
4108 static GTY(()) bool have_location_lists;
4109
4110 /* Unique label counter.  */
4111 static GTY(()) unsigned int loclabel_num;
4112
4113 #ifdef DWARF2_DEBUGGING_INFO
4114 /* Record whether the function being analyzed contains inlined functions.  */
4115 static int current_function_has_inlines;
4116 #endif
4117 #if 0 && defined (MIPS_DEBUGGING_INFO)
4118 static int comp_unit_has_inlines;
4119 #endif
4120
4121 /* The last file entry emitted by maybe_emit_file().  */
4122 static GTY(()) struct dwarf_file_data * last_emitted_file;
4123
4124 /* Number of internal labels generated by gen_internal_sym().  */
4125 static GTY(()) int label_num;
4126
4127 /* Cached result of previous call to lookup_filename.  */
4128 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4129
4130 #ifdef DWARF2_DEBUGGING_INFO
4131
4132 /* Offset from the "steady-state frame pointer" to the frame base,
4133    within the current function.  */
4134 static HOST_WIDE_INT frame_pointer_fb_offset;
4135
4136 /* Forward declarations for functions defined in this file.  */
4137
4138 static int is_pseudo_reg (const_rtx);
4139 static tree type_main_variant (tree);
4140 static int is_tagged_type (const_tree);
4141 static const char *dwarf_tag_name (unsigned);
4142 static const char *dwarf_attr_name (unsigned);
4143 static const char *dwarf_form_name (unsigned);
4144 static tree decl_ultimate_origin (const_tree);
4145 static tree block_ultimate_origin (const_tree);
4146 static tree decl_class_context (tree);
4147 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4148 static inline enum dw_val_class AT_class (dw_attr_ref);
4149 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4150 static inline unsigned AT_flag (dw_attr_ref);
4151 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4152 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4153 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4154 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4155 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4156                               unsigned long);
4157 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4158                                unsigned int, unsigned char *);
4159 static hashval_t debug_str_do_hash (const void *);
4160 static int debug_str_eq (const void *, const void *);
4161 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4162 static inline const char *AT_string (dw_attr_ref);
4163 static int AT_string_form (dw_attr_ref);
4164 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4165 static void add_AT_specification (dw_die_ref, dw_die_ref);
4166 static inline dw_die_ref AT_ref (dw_attr_ref);
4167 static inline int AT_ref_external (dw_attr_ref);
4168 static inline void set_AT_ref_external (dw_attr_ref, int);
4169 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4170 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4171 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4172 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4173                              dw_loc_list_ref);
4174 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4175 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4176 static inline rtx AT_addr (dw_attr_ref);
4177 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4178 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4179 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4180 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4181                            unsigned HOST_WIDE_INT);
4182 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4183                                unsigned long);
4184 static inline const char *AT_lbl (dw_attr_ref);
4185 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4186 static const char *get_AT_low_pc (dw_die_ref);
4187 static const char *get_AT_hi_pc (dw_die_ref);
4188 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4189 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4190 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4191 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4192 static bool is_c_family (void);
4193 static bool is_cxx (void);
4194 static bool is_java (void);
4195 static bool is_fortran (void);
4196 static bool is_ada (void);
4197 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4198 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4199 static void add_child_die (dw_die_ref, dw_die_ref);
4200 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4201 static dw_die_ref lookup_type_die (tree);
4202 static void equate_type_number_to_die (tree, dw_die_ref);
4203 static hashval_t decl_die_table_hash (const void *);
4204 static int decl_die_table_eq (const void *, const void *);
4205 static dw_die_ref lookup_decl_die (tree);
4206 static hashval_t decl_loc_table_hash (const void *);
4207 static int decl_loc_table_eq (const void *, const void *);
4208 static var_loc_list *lookup_decl_loc (const_tree);
4209 static void equate_decl_number_to_die (tree, dw_die_ref);
4210 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4211 static void print_spaces (FILE *);
4212 static void print_die (dw_die_ref, FILE *);
4213 static void print_dwarf_line_table (FILE *);
4214 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4215 static dw_die_ref pop_compile_unit (dw_die_ref);
4216 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4217 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4218 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4219 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4220 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4221 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4222 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4223 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4224 static void compute_section_prefix (dw_die_ref);
4225 static int is_type_die (dw_die_ref);
4226 static int is_comdat_die (dw_die_ref);
4227 static int is_symbol_die (dw_die_ref);
4228 static void assign_symbol_names (dw_die_ref);
4229 static void break_out_includes (dw_die_ref);
4230 static hashval_t htab_cu_hash (const void *);
4231 static int htab_cu_eq (const void *, const void *);
4232 static void htab_cu_del (void *);
4233 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4234 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4235 static void add_sibling_attributes (dw_die_ref);
4236 static void build_abbrev_table (dw_die_ref);
4237 static void output_location_lists (dw_die_ref);
4238 static int constant_size (long unsigned);
4239 static unsigned long size_of_die (dw_die_ref);
4240 static void calc_die_sizes (dw_die_ref);
4241 static void mark_dies (dw_die_ref);
4242 static void unmark_dies (dw_die_ref);
4243 static void unmark_all_dies (dw_die_ref);
4244 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4245 static unsigned long size_of_aranges (void);
4246 static enum dwarf_form value_format (dw_attr_ref);
4247 static void output_value_format (dw_attr_ref);
4248 static void output_abbrev_section (void);
4249 static void output_die_symbol (dw_die_ref);
4250 static void output_die (dw_die_ref);
4251 static void output_compilation_unit_header (void);
4252 static void output_comp_unit (dw_die_ref, int);
4253 static const char *dwarf2_name (tree, int);
4254 static void add_pubname (tree, dw_die_ref);
4255 static void add_pubname_string (const char *, dw_die_ref);
4256 static void add_pubtype (tree, dw_die_ref);
4257 static void output_pubnames (VEC (pubname_entry,gc) *);
4258 static void add_arange (tree, dw_die_ref);
4259 static void output_aranges (void);
4260 static unsigned int add_ranges_num (int);
4261 static unsigned int add_ranges (const_tree);
4262 static unsigned int add_ranges_by_labels (const char *, const char *);
4263 static void output_ranges (void);
4264 static void output_line_info (void);
4265 static void output_file_names (void);
4266 static dw_die_ref base_type_die (tree);
4267 static int is_base_type (tree);
4268 static bool is_subrange_type (const_tree);
4269 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4270 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4271 static int type_is_enum (const_tree);
4272 static unsigned int dbx_reg_number (const_rtx);
4273 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4274 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4275 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int, 
4276                                                 enum var_init_status);
4277 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4278                                                      enum var_init_status);
4279 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4280 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4281                                          enum var_init_status);
4282 static int is_based_loc (const_rtx);
4283 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4284                                             enum var_init_status);
4285 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4286                                                enum var_init_status);
4287 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4288 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4289 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4290 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4291 static tree field_type (const_tree);
4292 static unsigned int simple_type_align_in_bits (const_tree);
4293 static unsigned int simple_decl_align_in_bits (const_tree);
4294 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4295 static HOST_WIDE_INT field_byte_offset (const_tree);
4296 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4297                                          dw_loc_descr_ref);
4298 static void add_data_member_location_attribute (dw_die_ref, tree);
4299 static void add_const_value_attribute (dw_die_ref, rtx);
4300 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4301 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4302 static void insert_float (const_rtx, unsigned char *);
4303 static rtx rtl_for_decl_location (tree);
4304 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4305                                                    enum dwarf_attribute);
4306 static void tree_add_const_value_attribute (dw_die_ref, tree);
4307 static void add_name_attribute (dw_die_ref, const char *);
4308 static void add_comp_dir_attribute (dw_die_ref);
4309 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4310 static void add_subscript_info (dw_die_ref, tree);
4311 static void add_byte_size_attribute (dw_die_ref, tree);
4312 static void add_bit_offset_attribute (dw_die_ref, tree);
4313 static void add_bit_size_attribute (dw_die_ref, tree);
4314 static void add_prototyped_attribute (dw_die_ref, tree);
4315 static void add_abstract_origin_attribute (dw_die_ref, tree);
4316 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4317 static void add_src_coords_attributes (dw_die_ref, tree);
4318 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4319 static void push_decl_scope (tree);
4320 static void pop_decl_scope (void);
4321 static dw_die_ref scope_die_for (tree, dw_die_ref);
4322 static inline int local_scope_p (dw_die_ref);
4323 static inline int class_or_namespace_scope_p (dw_die_ref);
4324 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4325 static void add_calling_convention_attribute (dw_die_ref, tree);
4326 static const char *type_tag (const_tree);
4327 static tree member_declared_type (const_tree);
4328 #if 0
4329 static const char *decl_start_label (tree);
4330 #endif
4331 static void gen_array_type_die (tree, dw_die_ref);
4332 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4333 #if 0
4334 static void gen_entry_point_die (tree, dw_die_ref);
4335 #endif
4336 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4337 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4338 static void gen_inlined_union_type_die (tree, dw_die_ref);
4339 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4340 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4341 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4342 static void gen_formal_types_die (tree, dw_die_ref);
4343 static void gen_subprogram_die (tree, dw_die_ref);
4344 static void gen_variable_die (tree, dw_die_ref);
4345 static void gen_label_die (tree, dw_die_ref);
4346 static void gen_lexical_block_die (tree, dw_die_ref, int);
4347 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4348 static void gen_field_die (tree, dw_die_ref);
4349 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4350 static dw_die_ref gen_compile_unit_die (const char *);
4351 static void gen_inheritance_die (tree, tree, dw_die_ref);
4352 static void gen_member_die (tree, dw_die_ref);
4353 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4354                                                 enum debug_info_usage);
4355 static void gen_subroutine_type_die (tree, dw_die_ref);
4356 static void gen_typedef_die (tree, dw_die_ref);
4357 static void gen_type_die (tree, dw_die_ref);
4358 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4359 static void gen_block_die (tree, dw_die_ref, int);
4360 static void decls_for_scope (tree, dw_die_ref, int);
4361 static int is_redundant_typedef (const_tree);
4362 static void gen_namespace_die (tree);
4363 static void gen_decl_die (tree, dw_die_ref);
4364 static dw_die_ref force_decl_die (tree);
4365 static dw_die_ref force_type_die (tree);
4366 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4367 static void declare_in_namespace (tree, dw_die_ref);
4368 static struct dwarf_file_data * lookup_filename (const char *);
4369 static void retry_incomplete_types (void);
4370 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4371 static void splice_child_die (dw_die_ref, dw_die_ref);
4372 static int file_info_cmp (const void *, const void *);
4373 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4374                                      const char *, const char *, unsigned);
4375 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4376                                        const char *, const char *,
4377                                        const char *);
4378 static void output_loc_list (dw_loc_list_ref);
4379 static char *gen_internal_sym (const char *);
4380
4381 static void prune_unmark_dies (dw_die_ref);
4382 static void prune_unused_types_mark (dw_die_ref, int);
4383 static void prune_unused_types_walk (dw_die_ref);
4384 static void prune_unused_types_walk_attribs (dw_die_ref);
4385 static void prune_unused_types_prune (dw_die_ref);
4386 static void prune_unused_types (void);
4387 static int maybe_emit_file (struct dwarf_file_data *fd);
4388
4389 /* Section names used to hold DWARF debugging information.  */
4390 #ifndef DEBUG_INFO_SECTION
4391 #define DEBUG_INFO_SECTION      ".debug_info"
4392 #endif
4393 #ifndef DEBUG_ABBREV_SECTION
4394 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4395 #endif
4396 #ifndef DEBUG_ARANGES_SECTION
4397 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4398 #endif
4399 #ifndef DEBUG_MACINFO_SECTION
4400 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4401 #endif
4402 #ifndef DEBUG_LINE_SECTION
4403 #define DEBUG_LINE_SECTION      ".debug_line"
4404 #endif
4405 #ifndef DEBUG_LOC_SECTION
4406 #define DEBUG_LOC_SECTION       ".debug_loc"
4407 #endif
4408 #ifndef DEBUG_PUBNAMES_SECTION
4409 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4410 #endif
4411 #ifndef DEBUG_STR_SECTION
4412 #define DEBUG_STR_SECTION       ".debug_str"
4413 #endif
4414 #ifndef DEBUG_RANGES_SECTION
4415 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4416 #endif
4417
4418 /* Standard ELF section names for compiled code and data.  */
4419 #ifndef TEXT_SECTION_NAME
4420 #define TEXT_SECTION_NAME       ".text"
4421 #endif
4422
4423 /* Section flags for .debug_str section.  */
4424 #define DEBUG_STR_SECTION_FLAGS \
4425   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4426    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4427    : SECTION_DEBUG)
4428
4429 /* Labels we insert at beginning sections we can reference instead of
4430    the section names themselves.  */
4431
4432 #ifndef TEXT_SECTION_LABEL
4433 #define TEXT_SECTION_LABEL              "Ltext"
4434 #endif
4435 #ifndef COLD_TEXT_SECTION_LABEL
4436 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4437 #endif
4438 #ifndef DEBUG_LINE_SECTION_LABEL
4439 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4440 #endif
4441 #ifndef DEBUG_INFO_SECTION_LABEL
4442 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4443 #endif
4444 #ifndef DEBUG_ABBREV_SECTION_LABEL
4445 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4446 #endif
4447 #ifndef DEBUG_LOC_SECTION_LABEL
4448 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4449 #endif
4450 #ifndef DEBUG_RANGES_SECTION_LABEL
4451 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4452 #endif
4453 #ifndef DEBUG_MACINFO_SECTION_LABEL
4454 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4455 #endif
4456
4457 /* Definitions of defaults for formats and names of various special
4458    (artificial) labels which may be generated within this file (when the -g
4459    options is used and DWARF2_DEBUGGING_INFO is in effect.
4460    If necessary, these may be overridden from within the tm.h file, but
4461    typically, overriding these defaults is unnecessary.  */
4462
4463 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4464 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4465 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4466 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4467 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4468 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4469 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4470 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4471 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4472 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4473
4474 #ifndef TEXT_END_LABEL
4475 #define TEXT_END_LABEL          "Letext"
4476 #endif
4477 #ifndef COLD_END_LABEL
4478 #define COLD_END_LABEL          "Letext_cold"
4479 #endif
4480 #ifndef BLOCK_BEGIN_LABEL
4481 #define BLOCK_BEGIN_LABEL       "LBB"
4482 #endif
4483 #ifndef BLOCK_END_LABEL
4484 #define BLOCK_END_LABEL         "LBE"
4485 #endif
4486 #ifndef LINE_CODE_LABEL
4487 #define LINE_CODE_LABEL         "LM"
4488 #endif
4489 #ifndef SEPARATE_LINE_CODE_LABEL
4490 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4491 #endif
4492
4493 \f
4494 /* We allow a language front-end to designate a function that is to be
4495    called to "demangle" any name before it is put into a DIE.  */
4496
4497 static const char *(*demangle_name_func) (const char *);
4498
4499 void
4500 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4501 {
4502   demangle_name_func = func;
4503 }
4504
4505 /* Test if rtl node points to a pseudo register.  */
4506
4507 static inline int
4508 is_pseudo_reg (const_rtx rtl)
4509 {
4510   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4511           || (GET_CODE (rtl) == SUBREG
4512               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4513 }
4514
4515 /* Return a reference to a type, with its const and volatile qualifiers
4516    removed.  */
4517
4518 static inline tree
4519 type_main_variant (tree type)
4520 {
4521   type = TYPE_MAIN_VARIANT (type);
4522
4523   /* ??? There really should be only one main variant among any group of
4524      variants of a given type (and all of the MAIN_VARIANT values for all
4525      members of the group should point to that one type) but sometimes the C
4526      front-end messes this up for array types, so we work around that bug
4527      here.  */
4528   if (TREE_CODE (type) == ARRAY_TYPE)
4529     while (type != TYPE_MAIN_VARIANT (type))
4530       type = TYPE_MAIN_VARIANT (type);
4531
4532   return type;
4533 }
4534
4535 /* Return nonzero if the given type node represents a tagged type.  */
4536
4537 static inline int
4538 is_tagged_type (const_tree type)
4539 {
4540   enum tree_code code = TREE_CODE (type);
4541
4542   return (code == RECORD_TYPE || code == UNION_TYPE
4543           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4544 }
4545
4546 /* Convert a DIE tag into its string name.  */
4547
4548 static const char *
4549 dwarf_tag_name (unsigned int tag)
4550 {
4551   switch (tag)
4552     {
4553     case DW_TAG_padding:
4554       return "DW_TAG_padding";
4555     case DW_TAG_array_type:
4556       return "DW_TAG_array_type";
4557     case DW_TAG_class_type:
4558       return "DW_TAG_class_type";
4559     case DW_TAG_entry_point:
4560       return "DW_TAG_entry_point";
4561     case DW_TAG_enumeration_type:
4562       return "DW_TAG_enumeration_type";
4563     case DW_TAG_formal_parameter:
4564       return "DW_TAG_formal_parameter";
4565     case DW_TAG_imported_declaration:
4566       return "DW_TAG_imported_declaration";
4567     case DW_TAG_label:
4568       return "DW_TAG_label";
4569     case DW_TAG_lexical_block:
4570       return "DW_TAG_lexical_block";
4571     case DW_TAG_member:
4572       return "DW_TAG_member";
4573     case DW_TAG_pointer_type:
4574       return "DW_TAG_pointer_type";
4575     case DW_TAG_reference_type:
4576       return "DW_TAG_reference_type";
4577     case DW_TAG_compile_unit:
4578       return "DW_TAG_compile_unit";
4579     case DW_TAG_string_type:
4580       return "DW_TAG_string_type";
4581     case DW_TAG_structure_type:
4582       return "DW_TAG_structure_type";
4583     case DW_TAG_subroutine_type:
4584       return "DW_TAG_subroutine_type";
4585     case DW_TAG_typedef:
4586       return "DW_TAG_typedef";
4587     case DW_TAG_union_type:
4588       return "DW_TAG_union_type";
4589     case DW_TAG_unspecified_parameters:
4590       return "DW_TAG_unspecified_parameters";
4591     case DW_TAG_variant:
4592       return "DW_TAG_variant";
4593     case DW_TAG_common_block:
4594       return "DW_TAG_common_block";
4595     case DW_TAG_common_inclusion:
4596       return "DW_TAG_common_inclusion";
4597     case DW_TAG_inheritance:
4598       return "DW_TAG_inheritance";
4599     case DW_TAG_inlined_subroutine:
4600       return "DW_TAG_inlined_subroutine";
4601     case DW_TAG_module:
4602       return "DW_TAG_module";
4603     case DW_TAG_ptr_to_member_type:
4604       return "DW_TAG_ptr_to_member_type";
4605     case DW_TAG_set_type:
4606       return "DW_TAG_set_type";
4607     case DW_TAG_subrange_type:
4608       return "DW_TAG_subrange_type";
4609     case DW_TAG_with_stmt:
4610       return "DW_TAG_with_stmt";
4611     case DW_TAG_access_declaration:
4612       return "DW_TAG_access_declaration";
4613     case DW_TAG_base_type:
4614       return "DW_TAG_base_type";
4615     case DW_TAG_catch_block:
4616       return "DW_TAG_catch_block";
4617     case DW_TAG_const_type:
4618       return "DW_TAG_const_type";
4619     case DW_TAG_constant:
4620       return "DW_TAG_constant";
4621     case DW_TAG_enumerator:
4622       return "DW_TAG_enumerator";
4623     case DW_TAG_file_type:
4624       return "DW_TAG_file_type";
4625     case DW_TAG_friend:
4626       return "DW_TAG_friend";
4627     case DW_TAG_namelist:
4628       return "DW_TAG_namelist";
4629     case DW_TAG_namelist_item:
4630       return "DW_TAG_namelist_item";
4631     case DW_TAG_packed_type:
4632       return "DW_TAG_packed_type";
4633     case DW_TAG_subprogram:
4634       return "DW_TAG_subprogram";
4635     case DW_TAG_template_type_param:
4636       return "DW_TAG_template_type_param";
4637     case DW_TAG_template_value_param:
4638       return "DW_TAG_template_value_param";
4639     case DW_TAG_thrown_type:
4640       return "DW_TAG_thrown_type";
4641     case DW_TAG_try_block:
4642       return "DW_TAG_try_block";
4643     case DW_TAG_variant_part:
4644       return "DW_TAG_variant_part";
4645     case DW_TAG_variable:
4646       return "DW_TAG_variable";
4647     case DW_TAG_volatile_type:
4648       return "DW_TAG_volatile_type";
4649     case DW_TAG_dwarf_procedure:
4650       return "DW_TAG_dwarf_procedure";
4651     case DW_TAG_restrict_type:
4652       return "DW_TAG_restrict_type";
4653     case DW_TAG_interface_type:
4654       return "DW_TAG_interface_type";
4655     case DW_TAG_namespace:
4656       return "DW_TAG_namespace";
4657     case DW_TAG_imported_module:
4658       return "DW_TAG_imported_module";
4659     case DW_TAG_unspecified_type:
4660       return "DW_TAG_unspecified_type";
4661     case DW_TAG_partial_unit:
4662       return "DW_TAG_partial_unit";
4663     case DW_TAG_imported_unit:
4664       return "DW_TAG_imported_unit";
4665     case DW_TAG_condition:
4666       return "DW_TAG_condition";
4667     case DW_TAG_shared_type:
4668       return "DW_TAG_shared_type";
4669     case DW_TAG_MIPS_loop:
4670       return "DW_TAG_MIPS_loop";
4671     case DW_TAG_format_label:
4672       return "DW_TAG_format_label";
4673     case DW_TAG_function_template:
4674       return "DW_TAG_function_template";
4675     case DW_TAG_class_template:
4676       return "DW_TAG_class_template";
4677     case DW_TAG_GNU_BINCL:
4678       return "DW_TAG_GNU_BINCL";
4679     case DW_TAG_GNU_EINCL:
4680       return "DW_TAG_GNU_EINCL";
4681     default:
4682       return "DW_TAG_<unknown>";
4683     }
4684 }
4685
4686 /* Convert a DWARF attribute code into its string name.  */
4687
4688 static const char *
4689 dwarf_attr_name (unsigned int attr)
4690 {
4691   switch (attr)
4692     {
4693     case DW_AT_sibling:
4694       return "DW_AT_sibling";
4695     case DW_AT_location:
4696       return "DW_AT_location";
4697     case DW_AT_name:
4698       return "DW_AT_name";
4699     case DW_AT_ordering:
4700       return "DW_AT_ordering";
4701     case DW_AT_subscr_data:
4702       return "DW_AT_subscr_data";
4703     case DW_AT_byte_size:
4704       return "DW_AT_byte_size";
4705     case DW_AT_bit_offset:
4706       return "DW_AT_bit_offset";
4707     case DW_AT_bit_size:
4708       return "DW_AT_bit_size";
4709     case DW_AT_element_list:
4710       return "DW_AT_element_list";
4711     case DW_AT_stmt_list:
4712       return "DW_AT_stmt_list";
4713     case DW_AT_low_pc:
4714       return "DW_AT_low_pc";
4715     case DW_AT_high_pc:
4716       return "DW_AT_high_pc";
4717     case DW_AT_language:
4718       return "DW_AT_language";
4719     case DW_AT_member:
4720       return "DW_AT_member";
4721     case DW_AT_discr:
4722       return "DW_AT_discr";
4723     case DW_AT_discr_value:
4724       return "DW_AT_discr_value";
4725     case DW_AT_visibility:
4726       return "DW_AT_visibility";
4727     case DW_AT_import:
4728       return "DW_AT_import";
4729     case DW_AT_string_length:
4730       return "DW_AT_string_length";
4731     case DW_AT_common_reference:
4732       return "DW_AT_common_reference";
4733     case DW_AT_comp_dir:
4734       return "DW_AT_comp_dir";
4735     case DW_AT_const_value:
4736       return "DW_AT_const_value";
4737     case DW_AT_containing_type:
4738       return "DW_AT_containing_type";
4739     case DW_AT_default_value:
4740       return "DW_AT_default_value";
4741     case DW_AT_inline:
4742       return "DW_AT_inline";
4743     case DW_AT_is_optional:
4744       return "DW_AT_is_optional";
4745     case DW_AT_lower_bound:
4746       return "DW_AT_lower_bound";
4747     case DW_AT_producer:
4748       return "DW_AT_producer";
4749     case DW_AT_prototyped:
4750       return "DW_AT_prototyped";
4751     case DW_AT_return_addr:
4752       return "DW_AT_return_addr";
4753     case DW_AT_start_scope:
4754       return "DW_AT_start_scope";
4755     case DW_AT_bit_stride:
4756       return "DW_AT_bit_stride";
4757     case DW_AT_upper_bound:
4758       return "DW_AT_upper_bound";
4759     case DW_AT_abstract_origin:
4760       return "DW_AT_abstract_origin";
4761     case DW_AT_accessibility:
4762       return "DW_AT_accessibility";
4763     case DW_AT_address_class:
4764       return "DW_AT_address_class";
4765     case DW_AT_artificial:
4766       return "DW_AT_artificial";
4767     case DW_AT_base_types:
4768       return "DW_AT_base_types";
4769     case DW_AT_calling_convention:
4770       return "DW_AT_calling_convention";
4771     case DW_AT_count:
4772       return "DW_AT_count";
4773     case DW_AT_data_member_location:
4774       return "DW_AT_data_member_location";
4775     case DW_AT_decl_column:
4776       return "DW_AT_decl_column";
4777     case DW_AT_decl_file:
4778       return "DW_AT_decl_file";
4779     case DW_AT_decl_line:
4780       return "DW_AT_decl_line";
4781     case DW_AT_declaration:
4782       return "DW_AT_declaration";
4783     case DW_AT_discr_list:
4784       return "DW_AT_discr_list";
4785     case DW_AT_encoding:
4786       return "DW_AT_encoding";
4787     case DW_AT_external:
4788       return "DW_AT_external";
4789     case DW_AT_frame_base:
4790       return "DW_AT_frame_base";
4791     case DW_AT_friend:
4792       return "DW_AT_friend";
4793     case DW_AT_identifier_case:
4794       return "DW_AT_identifier_case";
4795     case DW_AT_macro_info:
4796       return "DW_AT_macro_info";
4797     case DW_AT_namelist_items:
4798       return "DW_AT_namelist_items";
4799     case DW_AT_priority:
4800       return "DW_AT_priority";
4801     case DW_AT_segment:
4802       return "DW_AT_segment";
4803     case DW_AT_specification:
4804       return "DW_AT_specification";
4805     case DW_AT_static_link:
4806       return "DW_AT_static_link";
4807     case DW_AT_type:
4808       return "DW_AT_type";
4809     case DW_AT_use_location:
4810       return "DW_AT_use_location";
4811     case DW_AT_variable_parameter:
4812       return "DW_AT_variable_parameter";
4813     case DW_AT_virtuality:
4814       return "DW_AT_virtuality";
4815     case DW_AT_vtable_elem_location:
4816       return "DW_AT_vtable_elem_location";
4817
4818     case DW_AT_allocated:
4819       return "DW_AT_allocated";
4820     case DW_AT_associated:
4821       return "DW_AT_associated";
4822     case DW_AT_data_location:
4823       return "DW_AT_data_location";
4824     case DW_AT_byte_stride:
4825       return "DW_AT_byte_stride";
4826     case DW_AT_entry_pc:
4827       return "DW_AT_entry_pc";
4828     case DW_AT_use_UTF8:
4829       return "DW_AT_use_UTF8";
4830     case DW_AT_extension:
4831       return "DW_AT_extension";
4832     case DW_AT_ranges:
4833       return "DW_AT_ranges";
4834     case DW_AT_trampoline:
4835       return "DW_AT_trampoline";
4836     case DW_AT_call_column:
4837       return "DW_AT_call_column";
4838     case DW_AT_call_file:
4839       return "DW_AT_call_file";
4840     case DW_AT_call_line:
4841       return "DW_AT_call_line";
4842
4843     case DW_AT_MIPS_fde:
4844       return "DW_AT_MIPS_fde";
4845     case DW_AT_MIPS_loop_begin:
4846       return "DW_AT_MIPS_loop_begin";
4847     case DW_AT_MIPS_tail_loop_begin:
4848       return "DW_AT_MIPS_tail_loop_begin";
4849     case DW_AT_MIPS_epilog_begin:
4850       return "DW_AT_MIPS_epilog_begin";
4851     case DW_AT_MIPS_loop_unroll_factor:
4852       return "DW_AT_MIPS_loop_unroll_factor";
4853     case DW_AT_MIPS_software_pipeline_depth:
4854       return "DW_AT_MIPS_software_pipeline_depth";
4855     case DW_AT_MIPS_linkage_name:
4856       return "DW_AT_MIPS_linkage_name";
4857     case DW_AT_MIPS_stride:
4858       return "DW_AT_MIPS_stride";
4859     case DW_AT_MIPS_abstract_name:
4860       return "DW_AT_MIPS_abstract_name";
4861     case DW_AT_MIPS_clone_origin:
4862       return "DW_AT_MIPS_clone_origin";
4863     case DW_AT_MIPS_has_inlines:
4864       return "DW_AT_MIPS_has_inlines";
4865
4866     case DW_AT_sf_names:
4867       return "DW_AT_sf_names";
4868     case DW_AT_src_info:
4869       return "DW_AT_src_info";
4870     case DW_AT_mac_info:
4871       return "DW_AT_mac_info";
4872     case DW_AT_src_coords:
4873       return "DW_AT_src_coords";
4874     case DW_AT_body_begin:
4875       return "DW_AT_body_begin";
4876     case DW_AT_body_end:
4877       return "DW_AT_body_end";
4878     case DW_AT_GNU_vector:
4879       return "DW_AT_GNU_vector";
4880
4881     case DW_AT_VMS_rtnbeg_pd_address:
4882       return "DW_AT_VMS_rtnbeg_pd_address";
4883
4884     default:
4885       return "DW_AT_<unknown>";
4886     }
4887 }
4888
4889 /* Convert a DWARF value form code into its string name.  */
4890
4891 static const char *
4892 dwarf_form_name (unsigned int form)
4893 {
4894   switch (form)
4895     {
4896     case DW_FORM_addr:
4897       return "DW_FORM_addr";
4898     case DW_FORM_block2:
4899       return "DW_FORM_block2";
4900     case DW_FORM_block4:
4901       return "DW_FORM_block4";
4902     case DW_FORM_data2:
4903       return "DW_FORM_data2";
4904     case DW_FORM_data4:
4905       return "DW_FORM_data4";
4906     case DW_FORM_data8:
4907       return "DW_FORM_data8";
4908     case DW_FORM_string:
4909       return "DW_FORM_string";
4910     case DW_FORM_block:
4911       return "DW_FORM_block";
4912     case DW_FORM_block1:
4913       return "DW_FORM_block1";
4914     case DW_FORM_data1:
4915       return "DW_FORM_data1";
4916     case DW_FORM_flag:
4917       return "DW_FORM_flag";
4918     case DW_FORM_sdata:
4919       return "DW_FORM_sdata";
4920     case DW_FORM_strp:
4921       return "DW_FORM_strp";
4922     case DW_FORM_udata:
4923       return "DW_FORM_udata";
4924     case DW_FORM_ref_addr:
4925       return "DW_FORM_ref_addr";
4926     case DW_FORM_ref1:
4927       return "DW_FORM_ref1";
4928     case DW_FORM_ref2:
4929       return "DW_FORM_ref2";
4930     case DW_FORM_ref4:
4931       return "DW_FORM_ref4";
4932     case DW_FORM_ref8:
4933       return "DW_FORM_ref8";
4934     case DW_FORM_ref_udata:
4935       return "DW_FORM_ref_udata";
4936     case DW_FORM_indirect:
4937       return "DW_FORM_indirect";
4938     default:
4939       return "DW_FORM_<unknown>";
4940     }
4941 }
4942 \f
4943 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4944    instance of an inlined instance of a decl which is local to an inline
4945    function, so we have to trace all of the way back through the origin chain
4946    to find out what sort of node actually served as the original seed for the
4947    given block.  */
4948
4949 static tree
4950 decl_ultimate_origin (const_tree decl)
4951 {
4952   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4953     return NULL_TREE;
4954
4955   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4956      nodes in the function to point to themselves; ignore that if
4957      we're trying to output the abstract instance of this function.  */
4958   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4959     return NULL_TREE;
4960
4961   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4962      most distant ancestor, this should never happen.  */
4963   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4964
4965   return DECL_ABSTRACT_ORIGIN (decl);
4966 }
4967
4968 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4969    instance of an inlined instance of a block which is local to an inline
4970    function, so we have to trace all of the way back through the origin chain
4971    to find out what sort of node actually served as the original seed for the
4972    given block.  */
4973
4974 static tree
4975 block_ultimate_origin (const_tree block)
4976 {
4977   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4978
4979   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4980      nodes in the function to point to themselves; ignore that if
4981      we're trying to output the abstract instance of this function.  */
4982   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4983     return NULL_TREE;
4984
4985   if (immediate_origin == NULL_TREE)
4986     return NULL_TREE;
4987   else
4988     {
4989       tree ret_val;
4990       tree lookahead = immediate_origin;
4991
4992       do
4993         {
4994           ret_val = lookahead;
4995           lookahead = (TREE_CODE (ret_val) == BLOCK
4996                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4997         }
4998       while (lookahead != NULL && lookahead != ret_val);
4999
5000       /* The block's abstract origin chain may not be the *ultimate* origin of
5001          the block. It could lead to a DECL that has an abstract origin set.
5002          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5003          will give us if it has one).  Note that DECL's abstract origins are
5004          supposed to be the most distant ancestor (or so decl_ultimate_origin
5005          claims), so we don't need to loop following the DECL origins.  */
5006       if (DECL_P (ret_val))
5007         return DECL_ORIGIN (ret_val);
5008
5009       return ret_val;
5010     }
5011 }
5012
5013 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5014    of a virtual function may refer to a base class, so we check the 'this'
5015    parameter.  */
5016
5017 static tree
5018 decl_class_context (tree decl)
5019 {
5020   tree context = NULL_TREE;
5021
5022   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5023     context = DECL_CONTEXT (decl);
5024   else
5025     context = TYPE_MAIN_VARIANT
5026       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5027
5028   if (context && !TYPE_P (context))
5029     context = NULL_TREE;
5030
5031   return context;
5032 }
5033 \f
5034 /* Add an attribute/value pair to a DIE.  */
5035
5036 static inline void
5037 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5038 {
5039   /* Maybe this should be an assert?  */
5040   if (die == NULL)
5041     return;
5042
5043   if (die->die_attr == NULL)
5044     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5045   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5046 }
5047
5048 static inline enum dw_val_class
5049 AT_class (dw_attr_ref a)
5050 {
5051   return a->dw_attr_val.val_class;
5052 }
5053
5054 /* Add a flag value attribute to a DIE.  */
5055
5056 static inline void
5057 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5058 {
5059   dw_attr_node attr;
5060
5061   attr.dw_attr = attr_kind;
5062   attr.dw_attr_val.val_class = dw_val_class_flag;
5063   attr.dw_attr_val.v.val_flag = flag;
5064   add_dwarf_attr (die, &attr);
5065 }
5066
5067 static inline unsigned
5068 AT_flag (dw_attr_ref a)
5069 {
5070   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5071   return a->dw_attr_val.v.val_flag;
5072 }
5073
5074 /* Add a signed integer attribute value to a DIE.  */
5075
5076 static inline void
5077 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5078 {
5079   dw_attr_node attr;
5080
5081   attr.dw_attr = attr_kind;
5082   attr.dw_attr_val.val_class = dw_val_class_const;
5083   attr.dw_attr_val.v.val_int = int_val;
5084   add_dwarf_attr (die, &attr);
5085 }
5086
5087 static inline HOST_WIDE_INT
5088 AT_int (dw_attr_ref a)
5089 {
5090   gcc_assert (a && AT_class (a) == dw_val_class_const);
5091   return a->dw_attr_val.v.val_int;
5092 }
5093
5094 /* Add an unsigned integer attribute value to a DIE.  */
5095
5096 static inline void
5097 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5098                  unsigned HOST_WIDE_INT unsigned_val)
5099 {
5100   dw_attr_node attr;
5101
5102   attr.dw_attr = attr_kind;
5103   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5104   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5105   add_dwarf_attr (die, &attr);
5106 }
5107
5108 static inline unsigned HOST_WIDE_INT
5109 AT_unsigned (dw_attr_ref a)
5110 {
5111   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5112   return a->dw_attr_val.v.val_unsigned;
5113 }
5114
5115 /* Add an unsigned double integer attribute value to a DIE.  */
5116
5117 static inline void
5118 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5119                   long unsigned int val_hi, long unsigned int val_low)
5120 {
5121   dw_attr_node attr;
5122
5123   attr.dw_attr = attr_kind;
5124   attr.dw_attr_val.val_class = dw_val_class_long_long;
5125   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5126   attr.dw_attr_val.v.val_long_long.low = val_low;
5127   add_dwarf_attr (die, &attr);
5128 }
5129
5130 /* Add a floating point attribute value to a DIE and return it.  */
5131
5132 static inline void
5133 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5134             unsigned int length, unsigned int elt_size, unsigned char *array)
5135 {
5136   dw_attr_node attr;
5137
5138   attr.dw_attr = attr_kind;
5139   attr.dw_attr_val.val_class = dw_val_class_vec;
5140   attr.dw_attr_val.v.val_vec.length = length;
5141   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5142   attr.dw_attr_val.v.val_vec.array = array;
5143   add_dwarf_attr (die, &attr);
5144 }
5145
5146 /* Hash and equality functions for debug_str_hash.  */
5147
5148 static hashval_t
5149 debug_str_do_hash (const void *x)
5150 {
5151   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5152 }
5153
5154 static int
5155 debug_str_eq (const void *x1, const void *x2)
5156 {
5157   return strcmp ((((const struct indirect_string_node *)x1)->str),
5158                  (const char *)x2) == 0;
5159 }
5160
5161 /* Add a string attribute value to a DIE.  */
5162
5163 static inline void
5164 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5165 {
5166   dw_attr_node attr;
5167   struct indirect_string_node *node;
5168   void **slot;
5169
5170   if (! debug_str_hash)
5171     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5172                                       debug_str_eq, NULL);
5173
5174   slot = htab_find_slot_with_hash (debug_str_hash, str,
5175                                    htab_hash_string (str), INSERT);
5176   if (*slot == NULL)
5177     {
5178       node = (struct indirect_string_node *)
5179                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5180       node->str = ggc_strdup (str);
5181       *slot = node;
5182     }
5183   else
5184     node = (struct indirect_string_node *) *slot;
5185
5186   node->refcount++;
5187
5188   attr.dw_attr = attr_kind;
5189   attr.dw_attr_val.val_class = dw_val_class_str;
5190   attr.dw_attr_val.v.val_str = node;
5191   add_dwarf_attr (die, &attr);
5192 }
5193
5194 static inline const char *
5195 AT_string (dw_attr_ref a)
5196 {
5197   gcc_assert (a && AT_class (a) == dw_val_class_str);
5198   return a->dw_attr_val.v.val_str->str;
5199 }
5200
5201 /* Find out whether a string should be output inline in DIE
5202    or out-of-line in .debug_str section.  */
5203
5204 static int
5205 AT_string_form (dw_attr_ref a)
5206 {
5207   struct indirect_string_node *node;
5208   unsigned int len;
5209   char label[32];
5210
5211   gcc_assert (a && AT_class (a) == dw_val_class_str);
5212
5213   node = a->dw_attr_val.v.val_str;
5214   if (node->form)
5215     return node->form;
5216
5217   len = strlen (node->str) + 1;
5218
5219   /* If the string is shorter or equal to the size of the reference, it is
5220      always better to put it inline.  */
5221   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5222     return node->form = DW_FORM_string;
5223
5224   /* If we cannot expect the linker to merge strings in .debug_str
5225      section, only put it into .debug_str if it is worth even in this
5226      single module.  */
5227   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5228       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5229     return node->form = DW_FORM_string;
5230
5231   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5232   ++dw2_string_counter;
5233   node->label = xstrdup (label);
5234
5235   return node->form = DW_FORM_strp;
5236 }
5237
5238 /* Add a DIE reference attribute value to a DIE.  */
5239
5240 static inline void
5241 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5242 {
5243   dw_attr_node attr;
5244
5245   attr.dw_attr = attr_kind;
5246   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5247   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5248   attr.dw_attr_val.v.val_die_ref.external = 0;
5249   add_dwarf_attr (die, &attr);
5250 }
5251
5252 /* Add an AT_specification attribute to a DIE, and also make the back
5253    pointer from the specification to the definition.  */
5254
5255 static inline void
5256 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5257 {
5258   add_AT_die_ref (die, DW_AT_specification, targ_die);
5259   gcc_assert (!targ_die->die_definition);
5260   targ_die->die_definition = die;
5261 }
5262
5263 static inline dw_die_ref
5264 AT_ref (dw_attr_ref a)
5265 {
5266   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5267   return a->dw_attr_val.v.val_die_ref.die;
5268 }
5269
5270 static inline int
5271 AT_ref_external (dw_attr_ref a)
5272 {
5273   if (a && AT_class (a) == dw_val_class_die_ref)
5274     return a->dw_attr_val.v.val_die_ref.external;
5275
5276   return 0;
5277 }
5278
5279 static inline void
5280 set_AT_ref_external (dw_attr_ref a, int i)
5281 {
5282   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5283   a->dw_attr_val.v.val_die_ref.external = i;
5284 }
5285
5286 /* Add an FDE reference attribute value to a DIE.  */
5287
5288 static inline void
5289 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5290 {
5291   dw_attr_node attr;
5292
5293   attr.dw_attr = attr_kind;
5294   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5295   attr.dw_attr_val.v.val_fde_index = targ_fde;
5296   add_dwarf_attr (die, &attr);
5297 }
5298
5299 /* Add a location description attribute value to a DIE.  */
5300
5301 static inline void
5302 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5303 {
5304   dw_attr_node attr;
5305
5306   attr.dw_attr = attr_kind;
5307   attr.dw_attr_val.val_class = dw_val_class_loc;
5308   attr.dw_attr_val.v.val_loc = loc;
5309   add_dwarf_attr (die, &attr);
5310 }
5311
5312 static inline dw_loc_descr_ref
5313 AT_loc (dw_attr_ref a)
5314 {
5315   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5316   return a->dw_attr_val.v.val_loc;
5317 }
5318
5319 static inline void
5320 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5321 {
5322   dw_attr_node attr;
5323
5324   attr.dw_attr = attr_kind;
5325   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5326   attr.dw_attr_val.v.val_loc_list = loc_list;
5327   add_dwarf_attr (die, &attr);
5328   have_location_lists = true;
5329 }
5330
5331 static inline dw_loc_list_ref
5332 AT_loc_list (dw_attr_ref a)
5333 {
5334   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5335   return a->dw_attr_val.v.val_loc_list;
5336 }
5337
5338 /* Add an address constant attribute value to a DIE.  */
5339
5340 static inline void
5341 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5342 {
5343   dw_attr_node attr;
5344
5345   attr.dw_attr = attr_kind;
5346   attr.dw_attr_val.val_class = dw_val_class_addr;
5347   attr.dw_attr_val.v.val_addr = addr;
5348   add_dwarf_attr (die, &attr);
5349 }
5350
5351 /* Get the RTX from to an address DIE attribute.  */
5352
5353 static inline rtx
5354 AT_addr (dw_attr_ref a)
5355 {
5356   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5357   return a->dw_attr_val.v.val_addr;
5358 }
5359
5360 /* Add a file attribute value to a DIE.  */
5361
5362 static inline void
5363 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5364              struct dwarf_file_data *fd)
5365 {
5366   dw_attr_node attr;
5367
5368   attr.dw_attr = attr_kind;
5369   attr.dw_attr_val.val_class = dw_val_class_file;
5370   attr.dw_attr_val.v.val_file = fd;
5371   add_dwarf_attr (die, &attr);
5372 }
5373
5374 /* Get the dwarf_file_data from a file DIE attribute.  */
5375
5376 static inline struct dwarf_file_data *
5377 AT_file (dw_attr_ref a)
5378 {
5379   gcc_assert (a && AT_class (a) == dw_val_class_file);
5380   return a->dw_attr_val.v.val_file;
5381 }
5382
5383 /* Add a label identifier attribute value to a DIE.  */
5384
5385 static inline void
5386 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5387 {
5388   dw_attr_node attr;
5389
5390   attr.dw_attr = attr_kind;
5391   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5392   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5393   add_dwarf_attr (die, &attr);
5394 }
5395
5396 /* Add a section offset attribute value to a DIE, an offset into the
5397    debug_line section.  */
5398
5399 static inline void
5400 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5401                 const char *label)
5402 {
5403   dw_attr_node attr;
5404
5405   attr.dw_attr = attr_kind;
5406   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5407   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5408   add_dwarf_attr (die, &attr);
5409 }
5410
5411 /* Add a section offset attribute value to a DIE, an offset into the
5412    debug_macinfo section.  */
5413
5414 static inline void
5415 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5416                const char *label)
5417 {
5418   dw_attr_node attr;
5419
5420   attr.dw_attr = attr_kind;
5421   attr.dw_attr_val.val_class = dw_val_class_macptr;
5422   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5423   add_dwarf_attr (die, &attr);
5424 }
5425
5426 /* Add an offset attribute value to a DIE.  */
5427
5428 static inline void
5429 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5430                unsigned HOST_WIDE_INT offset)
5431 {
5432   dw_attr_node attr;
5433
5434   attr.dw_attr = attr_kind;
5435   attr.dw_attr_val.val_class = dw_val_class_offset;
5436   attr.dw_attr_val.v.val_offset = offset;
5437   add_dwarf_attr (die, &attr);
5438 }
5439
5440 /* Add an range_list attribute value to a DIE.  */
5441
5442 static void
5443 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5444                    long unsigned int offset)
5445 {
5446   dw_attr_node attr;
5447
5448   attr.dw_attr = attr_kind;
5449   attr.dw_attr_val.val_class = dw_val_class_range_list;
5450   attr.dw_attr_val.v.val_offset = offset;
5451   add_dwarf_attr (die, &attr);
5452 }
5453
5454 static inline const char *
5455 AT_lbl (dw_attr_ref a)
5456 {
5457   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5458                     || AT_class (a) == dw_val_class_lineptr
5459                     || AT_class (a) == dw_val_class_macptr));
5460   return a->dw_attr_val.v.val_lbl_id;
5461 }
5462
5463 /* Get the attribute of type attr_kind.  */
5464
5465 static dw_attr_ref
5466 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5467 {
5468   dw_attr_ref a;
5469   unsigned ix;
5470   dw_die_ref spec = NULL;
5471
5472   if (! die)
5473     return NULL;
5474
5475   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5476     if (a->dw_attr == attr_kind)
5477       return a;
5478     else if (a->dw_attr == DW_AT_specification
5479              || a->dw_attr == DW_AT_abstract_origin)
5480       spec = AT_ref (a);
5481
5482   if (spec)
5483     return get_AT (spec, attr_kind);
5484
5485   return NULL;
5486 }
5487
5488 /* Return the "low pc" attribute value, typically associated with a subprogram
5489    DIE.  Return null if the "low pc" attribute is either not present, or if it
5490    cannot be represented as an assembler label identifier.  */
5491
5492 static inline const char *
5493 get_AT_low_pc (dw_die_ref die)
5494 {
5495   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5496
5497   return a ? AT_lbl (a) : NULL;
5498 }
5499
5500 /* Return the "high pc" attribute value, typically associated with a subprogram
5501    DIE.  Return null if the "high pc" attribute is either not present, or if it
5502    cannot be represented as an assembler label identifier.  */
5503
5504 static inline const char *
5505 get_AT_hi_pc (dw_die_ref die)
5506 {
5507   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5508
5509   return a ? AT_lbl (a) : NULL;
5510 }
5511
5512 /* Return the value of the string attribute designated by ATTR_KIND, or
5513    NULL if it is not present.  */
5514
5515 static inline const char *
5516 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5517 {
5518   dw_attr_ref a = get_AT (die, attr_kind);
5519
5520   return a ? AT_string (a) : NULL;
5521 }
5522
5523 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5524    if it is not present.  */
5525
5526 static inline int
5527 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5528 {
5529   dw_attr_ref a = get_AT (die, attr_kind);
5530
5531   return a ? AT_flag (a) : 0;
5532 }
5533
5534 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5535    if it is not present.  */
5536
5537 static inline unsigned
5538 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5539 {
5540   dw_attr_ref a = get_AT (die, attr_kind);
5541
5542   return a ? AT_unsigned (a) : 0;
5543 }
5544
5545 static inline dw_die_ref
5546 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5547 {
5548   dw_attr_ref a = get_AT (die, attr_kind);
5549
5550   return a ? AT_ref (a) : NULL;
5551 }
5552
5553 static inline struct dwarf_file_data *
5554 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5555 {
5556   dw_attr_ref a = get_AT (die, attr_kind);
5557
5558   return a ? AT_file (a) : NULL;
5559 }
5560
5561 /* Return TRUE if the language is C or C++.  */
5562
5563 static inline bool
5564 is_c_family (void)
5565 {
5566   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5567
5568   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5569           || lang == DW_LANG_C99
5570           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5571 }
5572
5573 /* Return TRUE if the language is C++.  */
5574
5575 static inline bool
5576 is_cxx (void)
5577 {
5578   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5579
5580   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5581 }
5582
5583 /* Return TRUE if the language is Fortran.  */
5584
5585 static inline bool
5586 is_fortran (void)
5587 {
5588   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5589
5590   return (lang == DW_LANG_Fortran77
5591           || lang == DW_LANG_Fortran90
5592           || lang == DW_LANG_Fortran95);
5593 }
5594
5595 /* Return TRUE if the language is Java.  */
5596
5597 static inline bool
5598 is_java (void)
5599 {
5600   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5601
5602   return lang == DW_LANG_Java;
5603 }
5604
5605 /* Return TRUE if the language is Ada.  */
5606
5607 static inline bool
5608 is_ada (void)
5609 {
5610   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5611
5612   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5613 }
5614
5615 /* Remove the specified attribute if present.  */
5616
5617 static void
5618 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5619 {
5620   dw_attr_ref a;
5621   unsigned ix;
5622
5623   if (! die)
5624     return;
5625
5626   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5627     if (a->dw_attr == attr_kind)
5628       {
5629         if (AT_class (a) == dw_val_class_str)
5630           if (a->dw_attr_val.v.val_str->refcount)
5631             a->dw_attr_val.v.val_str->refcount--;
5632
5633         /* VEC_ordered_remove should help reduce the number of abbrevs
5634            that are needed.  */
5635         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5636         return;
5637       }
5638 }
5639
5640 /* Remove CHILD from its parent.  PREV must have the property that
5641    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5642
5643 static void
5644 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5645 {
5646   gcc_assert (child->die_parent == prev->die_parent);
5647   gcc_assert (prev->die_sib == child);
5648   if (prev == child)
5649     {
5650       gcc_assert (child->die_parent->die_child == child);
5651       prev = NULL;
5652     }
5653   else
5654     prev->die_sib = child->die_sib;
5655   if (child->die_parent->die_child == child)
5656     child->die_parent->die_child = prev;
5657 }
5658
5659 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5660    matches TAG.  */
5661
5662 static void
5663 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5664 {
5665   dw_die_ref c;
5666
5667   c = die->die_child;
5668   if (c) do {
5669     dw_die_ref prev = c;
5670     c = c->die_sib;
5671     while (c->die_tag == tag)
5672       {
5673         remove_child_with_prev (c, prev);
5674         /* Might have removed every child.  */
5675         if (c == c->die_sib)
5676           return;
5677         c = c->die_sib;
5678       }
5679   } while (c != die->die_child);
5680 }
5681
5682 /* Add a CHILD_DIE as the last child of DIE.  */
5683
5684 static void
5685 add_child_die (dw_die_ref die, dw_die_ref child_die)
5686 {
5687   /* FIXME this should probably be an assert.  */
5688   if (! die || ! child_die)
5689     return;
5690   gcc_assert (die != child_die);
5691
5692   child_die->die_parent = die;
5693   if (die->die_child)
5694     {
5695       child_die->die_sib = die->die_child->die_sib;
5696       die->die_child->die_sib = child_die;
5697     }
5698   else
5699     child_die->die_sib = child_die;
5700   die->die_child = child_die;
5701 }
5702
5703 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5704    is the specification, to the end of PARENT's list of children.
5705    This is done by removing and re-adding it.  */
5706
5707 static void
5708 splice_child_die (dw_die_ref parent, dw_die_ref child)
5709 {
5710   dw_die_ref p;
5711
5712   /* We want the declaration DIE from inside the class, not the
5713      specification DIE at toplevel.  */
5714   if (child->die_parent != parent)
5715     {
5716       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5717
5718       if (tmp)
5719         child = tmp;
5720     }
5721
5722   gcc_assert (child->die_parent == parent
5723               || (child->die_parent
5724                   == get_AT_ref (parent, DW_AT_specification)));
5725
5726   for (p = child->die_parent->die_child; ; p = p->die_sib)
5727     if (p->die_sib == child)
5728       {
5729         remove_child_with_prev (child, p);
5730         break;
5731       }
5732
5733   add_child_die (parent, child);
5734 }
5735
5736 /* Return a pointer to a newly created DIE node.  */
5737
5738 static inline dw_die_ref
5739 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5740 {
5741   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5742
5743   die->die_tag = tag_value;
5744
5745   if (parent_die != NULL)
5746     add_child_die (parent_die, die);
5747   else
5748     {
5749       limbo_die_node *limbo_node;
5750
5751       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5752       limbo_node->die = die;
5753       limbo_node->created_for = t;
5754       limbo_node->next = limbo_die_list;
5755       limbo_die_list = limbo_node;
5756     }
5757
5758   return die;
5759 }
5760
5761 /* Return the DIE associated with the given type specifier.  */
5762
5763 static inline dw_die_ref
5764 lookup_type_die (tree type)
5765 {
5766   return TYPE_SYMTAB_DIE (type);
5767 }
5768
5769 /* Equate a DIE to a given type specifier.  */
5770
5771 static inline void
5772 equate_type_number_to_die (tree type, dw_die_ref type_die)
5773 {
5774   TYPE_SYMTAB_DIE (type) = type_die;
5775 }
5776
5777 /* Returns a hash value for X (which really is a die_struct).  */
5778
5779 static hashval_t
5780 decl_die_table_hash (const void *x)
5781 {
5782   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5783 }
5784
5785 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5786
5787 static int
5788 decl_die_table_eq (const void *x, const void *y)
5789 {
5790   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5791 }
5792
5793 /* Return the DIE associated with a given declaration.  */
5794
5795 static inline dw_die_ref
5796 lookup_decl_die (tree decl)
5797 {
5798   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5799 }
5800
5801 /* Returns a hash value for X (which really is a var_loc_list).  */
5802
5803 static hashval_t
5804 decl_loc_table_hash (const void *x)
5805 {
5806   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5807 }
5808
5809 /* Return nonzero if decl_id of var_loc_list X is the same as
5810    UID of decl *Y.  */
5811
5812 static int
5813 decl_loc_table_eq (const void *x, const void *y)
5814 {
5815   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5816 }
5817
5818 /* Return the var_loc list associated with a given declaration.  */
5819
5820 static inline var_loc_list *
5821 lookup_decl_loc (const_tree decl)
5822 {
5823   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5824 }
5825
5826 /* Equate a DIE to a particular declaration.  */
5827
5828 static void
5829 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5830 {
5831   unsigned int decl_id = DECL_UID (decl);
5832   void **slot;
5833
5834   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5835   *slot = decl_die;
5836   decl_die->decl_id = decl_id;
5837 }
5838
5839 /* Add a variable location node to the linked list for DECL.  */
5840
5841 static void
5842 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5843 {
5844   unsigned int decl_id = DECL_UID (decl);
5845   var_loc_list *temp;
5846   void **slot;
5847
5848   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5849   if (*slot == NULL)
5850     {
5851       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5852       temp->decl_id = decl_id;
5853       *slot = temp;
5854     }
5855   else
5856     temp = *slot;
5857
5858   if (temp->last)
5859     {
5860       /* If the current location is the same as the end of the list,
5861          and either both or neither of the locations is uninitialized,
5862          we have nothing to do.  */
5863       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5864                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5865           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5866                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5867               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5868                    == VAR_INIT_STATUS_UNINITIALIZED)
5869                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5870                       == VAR_INIT_STATUS_UNINITIALIZED))))
5871         {
5872           /* Add LOC to the end of list and update LAST.  */
5873           temp->last->next = loc;
5874           temp->last = loc;
5875         }
5876     }
5877   /* Do not add empty location to the beginning of the list.  */
5878   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5879     {
5880       temp->first = loc;
5881       temp->last = loc;
5882     }
5883 }
5884 \f
5885 /* Keep track of the number of spaces used to indent the
5886    output of the debugging routines that print the structure of
5887    the DIE internal representation.  */
5888 static int print_indent;
5889
5890 /* Indent the line the number of spaces given by print_indent.  */
5891
5892 static inline void
5893 print_spaces (FILE *outfile)
5894 {
5895   fprintf (outfile, "%*s", print_indent, "");
5896 }
5897
5898 /* Print the information associated with a given DIE, and its children.
5899    This routine is a debugging aid only.  */
5900
5901 static void
5902 print_die (dw_die_ref die, FILE *outfile)
5903 {
5904   dw_attr_ref a;
5905   dw_die_ref c;
5906   unsigned ix;
5907
5908   print_spaces (outfile);
5909   fprintf (outfile, "DIE %4ld: %s\n",
5910            die->die_offset, dwarf_tag_name (die->die_tag));
5911   print_spaces (outfile);
5912   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5913   fprintf (outfile, " offset: %ld\n", die->die_offset);
5914
5915   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5916     {
5917       print_spaces (outfile);
5918       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5919
5920       switch (AT_class (a))
5921         {
5922         case dw_val_class_addr:
5923           fprintf (outfile, "address");
5924           break;
5925         case dw_val_class_offset:
5926           fprintf (outfile, "offset");
5927           break;
5928         case dw_val_class_loc:
5929           fprintf (outfile, "location descriptor");
5930           break;
5931         case dw_val_class_loc_list:
5932           fprintf (outfile, "location list -> label:%s",
5933                    AT_loc_list (a)->ll_symbol);
5934           break;
5935         case dw_val_class_range_list:
5936           fprintf (outfile, "range list");
5937           break;
5938         case dw_val_class_const:
5939           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5940           break;
5941         case dw_val_class_unsigned_const:
5942           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5943           break;
5944         case dw_val_class_long_long:
5945           fprintf (outfile, "constant (%lu,%lu)",
5946                    a->dw_attr_val.v.val_long_long.hi,
5947                    a->dw_attr_val.v.val_long_long.low);
5948           break;
5949         case dw_val_class_vec:
5950           fprintf (outfile, "floating-point or vector constant");
5951           break;
5952         case dw_val_class_flag:
5953           fprintf (outfile, "%u", AT_flag (a));
5954           break;
5955         case dw_val_class_die_ref:
5956           if (AT_ref (a) != NULL)
5957             {
5958               if (AT_ref (a)->die_symbol)
5959                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5960               else
5961                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5962             }
5963           else
5964             fprintf (outfile, "die -> <null>");
5965           break;
5966         case dw_val_class_lbl_id:
5967         case dw_val_class_lineptr:
5968         case dw_val_class_macptr:
5969           fprintf (outfile, "label: %s", AT_lbl (a));
5970           break;
5971         case dw_val_class_str:
5972           if (AT_string (a) != NULL)
5973             fprintf (outfile, "\"%s\"", AT_string (a));
5974           else
5975             fprintf (outfile, "<null>");
5976           break;
5977         case dw_val_class_file:
5978           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5979                    AT_file (a)->emitted_number);
5980           break;
5981         default:
5982           break;
5983         }
5984
5985       fprintf (outfile, "\n");
5986     }
5987
5988   if (die->die_child != NULL)
5989     {
5990       print_indent += 4;
5991       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5992       print_indent -= 4;
5993     }
5994   if (print_indent == 0)
5995     fprintf (outfile, "\n");
5996 }
5997
5998 /* Print the contents of the source code line number correspondence table.
5999    This routine is a debugging aid only.  */
6000
6001 static void
6002 print_dwarf_line_table (FILE *outfile)
6003 {
6004   unsigned i;
6005   dw_line_info_ref line_info;
6006
6007   fprintf (outfile, "\n\nDWARF source line information\n");
6008   for (i = 1; i < line_info_table_in_use; i++)
6009     {
6010       line_info = &line_info_table[i];
6011       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6012                line_info->dw_file_num,
6013                line_info->dw_line_num);
6014     }
6015
6016   fprintf (outfile, "\n\n");
6017 }
6018
6019 /* Print the information collected for a given DIE.  */
6020
6021 void
6022 debug_dwarf_die (dw_die_ref die)
6023 {
6024   print_die (die, stderr);
6025 }
6026
6027 /* Print all DWARF information collected for the compilation unit.
6028    This routine is a debugging aid only.  */
6029
6030 void
6031 debug_dwarf (void)
6032 {
6033   print_indent = 0;
6034   print_die (comp_unit_die, stderr);
6035   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6036     print_dwarf_line_table (stderr);
6037 }
6038 \f
6039 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6040    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6041    DIE that marks the start of the DIEs for this include file.  */
6042
6043 static dw_die_ref
6044 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6045 {
6046   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6047   dw_die_ref new_unit = gen_compile_unit_die (filename);
6048
6049   new_unit->die_sib = old_unit;
6050   return new_unit;
6051 }
6052
6053 /* Close an include-file CU and reopen the enclosing one.  */
6054
6055 static dw_die_ref
6056 pop_compile_unit (dw_die_ref old_unit)
6057 {
6058   dw_die_ref new_unit = old_unit->die_sib;
6059
6060   old_unit->die_sib = NULL;
6061   return new_unit;
6062 }
6063
6064 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6065 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6066
6067 /* Calculate the checksum of a location expression.  */
6068
6069 static inline void
6070 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6071 {
6072   CHECKSUM (loc->dw_loc_opc);
6073   CHECKSUM (loc->dw_loc_oprnd1);
6074   CHECKSUM (loc->dw_loc_oprnd2);
6075 }
6076
6077 /* Calculate the checksum of an attribute.  */
6078
6079 static void
6080 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6081 {
6082   dw_loc_descr_ref loc;
6083   rtx r;
6084
6085   CHECKSUM (at->dw_attr);
6086
6087   /* We don't care that this was compiled with a different compiler
6088      snapshot; if the output is the same, that's what matters.  */
6089   if (at->dw_attr == DW_AT_producer)
6090     return;
6091
6092   switch (AT_class (at))
6093     {
6094     case dw_val_class_const:
6095       CHECKSUM (at->dw_attr_val.v.val_int);
6096       break;
6097     case dw_val_class_unsigned_const:
6098       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6099       break;
6100     case dw_val_class_long_long:
6101       CHECKSUM (at->dw_attr_val.v.val_long_long);
6102       break;
6103     case dw_val_class_vec:
6104       CHECKSUM (at->dw_attr_val.v.val_vec);
6105       break;
6106     case dw_val_class_flag:
6107       CHECKSUM (at->dw_attr_val.v.val_flag);
6108       break;
6109     case dw_val_class_str:
6110       CHECKSUM_STRING (AT_string (at));
6111       break;
6112
6113     case dw_val_class_addr:
6114       r = AT_addr (at);
6115       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6116       CHECKSUM_STRING (XSTR (r, 0));
6117       break;
6118
6119     case dw_val_class_offset:
6120       CHECKSUM (at->dw_attr_val.v.val_offset);
6121       break;
6122
6123     case dw_val_class_loc:
6124       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6125         loc_checksum (loc, ctx);
6126       break;
6127
6128     case dw_val_class_die_ref:
6129       die_checksum (AT_ref (at), ctx, mark);
6130       break;
6131
6132     case dw_val_class_fde_ref:
6133     case dw_val_class_lbl_id:
6134     case dw_val_class_lineptr:
6135     case dw_val_class_macptr:
6136       break;
6137
6138     case dw_val_class_file:
6139       CHECKSUM_STRING (AT_file (at)->filename);
6140       break;
6141
6142     default:
6143       break;
6144     }
6145 }
6146
6147 /* Calculate the checksum of a DIE.  */
6148
6149 static void
6150 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6151 {
6152   dw_die_ref c;
6153   dw_attr_ref a;
6154   unsigned ix;
6155
6156   /* To avoid infinite recursion.  */
6157   if (die->die_mark)
6158     {
6159       CHECKSUM (die->die_mark);
6160       return;
6161     }
6162   die->die_mark = ++(*mark);
6163
6164   CHECKSUM (die->die_tag);
6165
6166   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6167     attr_checksum (a, ctx, mark);
6168
6169   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6170 }
6171
6172 #undef CHECKSUM
6173 #undef CHECKSUM_STRING
6174
6175 /* Do the location expressions look same?  */
6176 static inline int
6177 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6178 {
6179   return loc1->dw_loc_opc == loc2->dw_loc_opc
6180          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6181          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6182 }
6183
6184 /* Do the values look the same?  */
6185 static int
6186 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6187 {
6188   dw_loc_descr_ref loc1, loc2;
6189   rtx r1, r2;
6190
6191   if (v1->val_class != v2->val_class)
6192     return 0;
6193
6194   switch (v1->val_class)
6195     {
6196     case dw_val_class_const:
6197       return v1->v.val_int == v2->v.val_int;
6198     case dw_val_class_unsigned_const:
6199       return v1->v.val_unsigned == v2->v.val_unsigned;
6200     case dw_val_class_long_long:
6201       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6202              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6203     case dw_val_class_vec:
6204       if (v1->v.val_vec.length != v2->v.val_vec.length
6205           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6206         return 0;
6207       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6208                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6209         return 0;
6210       return 1;
6211     case dw_val_class_flag:
6212       return v1->v.val_flag == v2->v.val_flag;
6213     case dw_val_class_str:
6214       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6215
6216     case dw_val_class_addr:
6217       r1 = v1->v.val_addr;
6218       r2 = v2->v.val_addr;
6219       if (GET_CODE (r1) != GET_CODE (r2))
6220         return 0;
6221       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6222       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6223
6224     case dw_val_class_offset:
6225       return v1->v.val_offset == v2->v.val_offset;
6226
6227     case dw_val_class_loc:
6228       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6229            loc1 && loc2;
6230            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6231         if (!same_loc_p (loc1, loc2, mark))
6232           return 0;
6233       return !loc1 && !loc2;
6234
6235     case dw_val_class_die_ref:
6236       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6237
6238     case dw_val_class_fde_ref:
6239     case dw_val_class_lbl_id:
6240     case dw_val_class_lineptr:
6241     case dw_val_class_macptr:
6242       return 1;
6243
6244     case dw_val_class_file:
6245       return v1->v.val_file == v2->v.val_file;
6246
6247     default:
6248       return 1;
6249     }
6250 }
6251
6252 /* Do the attributes look the same?  */
6253
6254 static int
6255 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6256 {
6257   if (at1->dw_attr != at2->dw_attr)
6258     return 0;
6259
6260   /* We don't care that this was compiled with a different compiler
6261      snapshot; if the output is the same, that's what matters. */
6262   if (at1->dw_attr == DW_AT_producer)
6263     return 1;
6264
6265   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6266 }
6267
6268 /* Do the dies look the same?  */
6269
6270 static int
6271 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6272 {
6273   dw_die_ref c1, c2;
6274   dw_attr_ref a1;
6275   unsigned ix;
6276
6277   /* To avoid infinite recursion.  */
6278   if (die1->die_mark)
6279     return die1->die_mark == die2->die_mark;
6280   die1->die_mark = die2->die_mark = ++(*mark);
6281
6282   if (die1->die_tag != die2->die_tag)
6283     return 0;
6284
6285   if (VEC_length (dw_attr_node, die1->die_attr)
6286       != VEC_length (dw_attr_node, die2->die_attr))
6287     return 0;
6288
6289   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6290     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6291       return 0;
6292
6293   c1 = die1->die_child;
6294   c2 = die2->die_child;
6295   if (! c1)
6296     {
6297       if (c2)
6298         return 0;
6299     }
6300   else
6301     for (;;)
6302       {
6303         if (!same_die_p (c1, c2, mark))
6304           return 0;
6305         c1 = c1->die_sib;
6306         c2 = c2->die_sib;
6307         if (c1 == die1->die_child)
6308           {
6309             if (c2 == die2->die_child)
6310               break;
6311             else
6312               return 0;
6313           }
6314     }
6315
6316   return 1;
6317 }
6318
6319 /* Do the dies look the same?  Wrapper around same_die_p.  */
6320
6321 static int
6322 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6323 {
6324   int mark = 0;
6325   int ret = same_die_p (die1, die2, &mark);
6326
6327   unmark_all_dies (die1);
6328   unmark_all_dies (die2);
6329
6330   return ret;
6331 }
6332
6333 /* The prefix to attach to symbols on DIEs in the current comdat debug
6334    info section.  */
6335 static char *comdat_symbol_id;
6336
6337 /* The index of the current symbol within the current comdat CU.  */
6338 static unsigned int comdat_symbol_number;
6339
6340 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6341    children, and set comdat_symbol_id accordingly.  */
6342
6343 static void
6344 compute_section_prefix (dw_die_ref unit_die)
6345 {
6346   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6347   const char *base = die_name ? lbasename (die_name) : "anonymous";
6348   char *name = alloca (strlen (base) + 64);
6349   char *p;
6350   int i, mark;
6351   unsigned char checksum[16];
6352   struct md5_ctx ctx;
6353
6354   /* Compute the checksum of the DIE, then append part of it as hex digits to
6355      the name filename of the unit.  */
6356
6357   md5_init_ctx (&ctx);
6358   mark = 0;
6359   die_checksum (unit_die, &ctx, &mark);
6360   unmark_all_dies (unit_die);
6361   md5_finish_ctx (&ctx, checksum);
6362
6363   sprintf (name, "%s.", base);
6364   clean_symbol_name (name);
6365
6366   p = name + strlen (name);
6367   for (i = 0; i < 4; i++)
6368     {
6369       sprintf (p, "%.2x", checksum[i]);
6370       p += 2;
6371     }
6372
6373   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6374   comdat_symbol_number = 0;
6375 }
6376
6377 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6378
6379 static int
6380 is_type_die (dw_die_ref die)
6381 {
6382   switch (die->die_tag)
6383     {
6384     case DW_TAG_array_type:
6385     case DW_TAG_class_type:
6386     case DW_TAG_interface_type:
6387     case DW_TAG_enumeration_type:
6388     case DW_TAG_pointer_type:
6389     case DW_TAG_reference_type:
6390     case DW_TAG_string_type:
6391     case DW_TAG_structure_type:
6392     case DW_TAG_subroutine_type:
6393     case DW_TAG_union_type:
6394     case DW_TAG_ptr_to_member_type:
6395     case DW_TAG_set_type:
6396     case DW_TAG_subrange_type:
6397     case DW_TAG_base_type:
6398     case DW_TAG_const_type:
6399     case DW_TAG_file_type:
6400     case DW_TAG_packed_type:
6401     case DW_TAG_volatile_type:
6402     case DW_TAG_typedef:
6403       return 1;
6404     default:
6405       return 0;
6406     }
6407 }
6408
6409 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6410    Basically, we want to choose the bits that are likely to be shared between
6411    compilations (types) and leave out the bits that are specific to individual
6412    compilations (functions).  */
6413
6414 static int
6415 is_comdat_die (dw_die_ref c)
6416 {
6417   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6418      we do for stabs.  The advantage is a greater likelihood of sharing between
6419      objects that don't include headers in the same order (and therefore would
6420      put the base types in a different comdat).  jason 8/28/00 */
6421
6422   if (c->die_tag == DW_TAG_base_type)
6423     return 0;
6424
6425   if (c->die_tag == DW_TAG_pointer_type
6426       || c->die_tag == DW_TAG_reference_type
6427       || c->die_tag == DW_TAG_const_type
6428       || c->die_tag == DW_TAG_volatile_type)
6429     {
6430       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6431
6432       return t ? is_comdat_die (t) : 0;
6433     }
6434
6435   return is_type_die (c);
6436 }
6437
6438 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6439    compilation unit.  */
6440
6441 static int
6442 is_symbol_die (dw_die_ref c)
6443 {
6444   return (is_type_die (c)
6445           || (get_AT (c, DW_AT_declaration)
6446               && !get_AT (c, DW_AT_specification))
6447           || c->die_tag == DW_TAG_namespace);
6448 }
6449
6450 static char *
6451 gen_internal_sym (const char *prefix)
6452 {
6453   char buf[256];
6454
6455   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6456   return xstrdup (buf);
6457 }
6458
6459 /* Assign symbols to all worthy DIEs under DIE.  */
6460
6461 static void
6462 assign_symbol_names (dw_die_ref die)
6463 {
6464   dw_die_ref c;
6465
6466   if (is_symbol_die (die))
6467     {
6468       if (comdat_symbol_id)
6469         {
6470           char *p = alloca (strlen (comdat_symbol_id) + 64);
6471
6472           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6473                    comdat_symbol_id, comdat_symbol_number++);
6474           die->die_symbol = xstrdup (p);
6475         }
6476       else
6477         die->die_symbol = gen_internal_sym ("LDIE");
6478     }
6479
6480   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6481 }
6482
6483 struct cu_hash_table_entry
6484 {
6485   dw_die_ref cu;
6486   unsigned min_comdat_num, max_comdat_num;
6487   struct cu_hash_table_entry *next;
6488 };
6489
6490 /* Routines to manipulate hash table of CUs.  */
6491 static hashval_t
6492 htab_cu_hash (const void *of)
6493 {
6494   const struct cu_hash_table_entry *entry = of;
6495
6496   return htab_hash_string (entry->cu->die_symbol);
6497 }
6498
6499 static int
6500 htab_cu_eq (const void *of1, const void *of2)
6501 {
6502   const struct cu_hash_table_entry *entry1 = of1;
6503   const struct die_struct *entry2 = of2;
6504
6505   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6506 }
6507
6508 static void
6509 htab_cu_del (void *what)
6510 {
6511   struct cu_hash_table_entry *next, *entry = what;
6512
6513   while (entry)
6514     {
6515       next = entry->next;
6516       free (entry);
6517       entry = next;
6518     }
6519 }
6520
6521 /* Check whether we have already seen this CU and set up SYM_NUM
6522    accordingly.  */
6523 static int
6524 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6525 {
6526   struct cu_hash_table_entry dummy;
6527   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6528
6529   dummy.max_comdat_num = 0;
6530
6531   slot = (struct cu_hash_table_entry **)
6532     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6533         INSERT);
6534   entry = *slot;
6535
6536   for (; entry; last = entry, entry = entry->next)
6537     {
6538       if (same_die_p_wrap (cu, entry->cu))
6539         break;
6540     }
6541
6542   if (entry)
6543     {
6544       *sym_num = entry->min_comdat_num;
6545       return 1;
6546     }
6547
6548   entry = XCNEW (struct cu_hash_table_entry);
6549   entry->cu = cu;
6550   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6551   entry->next = *slot;
6552   *slot = entry;
6553
6554   return 0;
6555 }
6556
6557 /* Record SYM_NUM to record of CU in HTABLE.  */
6558 static void
6559 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6560 {
6561   struct cu_hash_table_entry **slot, *entry;
6562
6563   slot = (struct cu_hash_table_entry **)
6564     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6565         NO_INSERT);
6566   entry = *slot;
6567
6568   entry->max_comdat_num = sym_num;
6569 }
6570
6571 /* Traverse the DIE (which is always comp_unit_die), and set up
6572    additional compilation units for each of the include files we see
6573    bracketed by BINCL/EINCL.  */
6574
6575 static void
6576 break_out_includes (dw_die_ref die)
6577 {
6578   dw_die_ref c;
6579   dw_die_ref unit = NULL;
6580   limbo_die_node *node, **pnode;
6581   htab_t cu_hash_table;
6582
6583   c = die->die_child;
6584   if (c) do {
6585     dw_die_ref prev = c;
6586     c = c->die_sib;
6587     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6588            || (unit && is_comdat_die (c)))
6589       {
6590         dw_die_ref next = c->die_sib;
6591
6592         /* This DIE is for a secondary CU; remove it from the main one.  */
6593         remove_child_with_prev (c, prev);
6594
6595         if (c->die_tag == DW_TAG_GNU_BINCL)
6596           unit = push_new_compile_unit (unit, c);
6597         else if (c->die_tag == DW_TAG_GNU_EINCL)
6598           unit = pop_compile_unit (unit);
6599         else
6600           add_child_die (unit, c);
6601         c = next;
6602         if (c == die->die_child)
6603           break;
6604       }
6605   } while (c != die->die_child);
6606
6607 #if 0
6608   /* We can only use this in debugging, since the frontend doesn't check
6609      to make sure that we leave every include file we enter.  */
6610   gcc_assert (!unit);
6611 #endif
6612
6613   assign_symbol_names (die);
6614   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6615   for (node = limbo_die_list, pnode = &limbo_die_list;
6616        node;
6617        node = node->next)
6618     {
6619       int is_dupl;
6620
6621       compute_section_prefix (node->die);
6622       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6623                         &comdat_symbol_number);
6624       assign_symbol_names (node->die);
6625       if (is_dupl)
6626         *pnode = node->next;
6627       else
6628         {
6629           pnode = &node->next;
6630           record_comdat_symbol_number (node->die, cu_hash_table,
6631                 comdat_symbol_number);
6632         }
6633     }
6634   htab_delete (cu_hash_table);
6635 }
6636
6637 /* Traverse the DIE and add a sibling attribute if it may have the
6638    effect of speeding up access to siblings.  To save some space,
6639    avoid generating sibling attributes for DIE's without children.  */
6640
6641 static void
6642 add_sibling_attributes (dw_die_ref die)
6643 {
6644   dw_die_ref c;
6645
6646   if (! die->die_child)
6647     return;
6648
6649   if (die->die_parent && die != die->die_parent->die_child)
6650     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6651
6652   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6653 }
6654
6655 /* Output all location lists for the DIE and its children.  */
6656
6657 static void
6658 output_location_lists (dw_die_ref die)
6659 {
6660   dw_die_ref c;
6661   dw_attr_ref a;
6662   unsigned ix;
6663
6664   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6665     if (AT_class (a) == dw_val_class_loc_list)
6666       output_loc_list (AT_loc_list (a));
6667
6668   FOR_EACH_CHILD (die, c, output_location_lists (c));
6669 }
6670
6671 /* The format of each DIE (and its attribute value pairs) is encoded in an
6672    abbreviation table.  This routine builds the abbreviation table and assigns
6673    a unique abbreviation id for each abbreviation entry.  The children of each
6674    die are visited recursively.  */
6675
6676 static void
6677 build_abbrev_table (dw_die_ref die)
6678 {
6679   unsigned long abbrev_id;
6680   unsigned int n_alloc;
6681   dw_die_ref c;
6682   dw_attr_ref a;
6683   unsigned ix;
6684
6685   /* Scan the DIE references, and mark as external any that refer to
6686      DIEs from other CUs (i.e. those which are not marked).  */
6687   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6688     if (AT_class (a) == dw_val_class_die_ref
6689         && AT_ref (a)->die_mark == 0)
6690       {
6691         gcc_assert (AT_ref (a)->die_symbol);
6692
6693         set_AT_ref_external (a, 1);
6694       }
6695
6696   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6697     {
6698       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6699       dw_attr_ref die_a, abbrev_a;
6700       unsigned ix;
6701       bool ok = true;
6702
6703       if (abbrev->die_tag != die->die_tag)
6704         continue;
6705       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6706         continue;
6707
6708       if (VEC_length (dw_attr_node, abbrev->die_attr)
6709           != VEC_length (dw_attr_node, die->die_attr))
6710         continue;
6711
6712       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6713         {
6714           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6715           if ((abbrev_a->dw_attr != die_a->dw_attr)
6716               || (value_format (abbrev_a) != value_format (die_a)))
6717             {
6718               ok = false;
6719               break;
6720             }
6721         }
6722       if (ok)
6723         break;
6724     }
6725
6726   if (abbrev_id >= abbrev_die_table_in_use)
6727     {
6728       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6729         {
6730           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6731           abbrev_die_table = ggc_realloc (abbrev_die_table,
6732                                           sizeof (dw_die_ref) * n_alloc);
6733
6734           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6735                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6736           abbrev_die_table_allocated = n_alloc;
6737         }
6738
6739       ++abbrev_die_table_in_use;
6740       abbrev_die_table[abbrev_id] = die;
6741     }
6742
6743   die->die_abbrev = abbrev_id;
6744   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6745 }
6746 \f
6747 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6748
6749 static int
6750 constant_size (long unsigned int value)
6751 {
6752   int log;
6753
6754   if (value == 0)
6755     log = 0;
6756   else
6757     log = floor_log2 (value);
6758
6759   log = log / 8;
6760   log = 1 << (floor_log2 (log) + 1);
6761
6762   return log;
6763 }
6764
6765 /* Return the size of a DIE as it is represented in the
6766    .debug_info section.  */
6767
6768 static unsigned long
6769 size_of_die (dw_die_ref die)
6770 {
6771   unsigned long size = 0;
6772   dw_attr_ref a;
6773   unsigned ix;
6774
6775   size += size_of_uleb128 (die->die_abbrev);
6776   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6777     {
6778       switch (AT_class (a))
6779         {
6780         case dw_val_class_addr:
6781           size += DWARF2_ADDR_SIZE;
6782           break;
6783         case dw_val_class_offset:
6784           size += DWARF_OFFSET_SIZE;
6785           break;
6786         case dw_val_class_loc:
6787           {
6788             unsigned long lsize = size_of_locs (AT_loc (a));
6789
6790             /* Block length.  */
6791             size += constant_size (lsize);
6792             size += lsize;
6793           }
6794           break;
6795         case dw_val_class_loc_list:
6796           size += DWARF_OFFSET_SIZE;
6797           break;
6798         case dw_val_class_range_list:
6799           size += DWARF_OFFSET_SIZE;
6800           break;
6801         case dw_val_class_const:
6802           size += size_of_sleb128 (AT_int (a));
6803           break;
6804         case dw_val_class_unsigned_const:
6805           size += constant_size (AT_unsigned (a));
6806           break;
6807         case dw_val_class_long_long:
6808           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6809           break;
6810         case dw_val_class_vec:
6811           size += 1 + (a->dw_attr_val.v.val_vec.length
6812                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6813           break;
6814         case dw_val_class_flag:
6815           size += 1;
6816           break;
6817         case dw_val_class_die_ref:
6818           if (AT_ref_external (a))
6819             size += DWARF2_ADDR_SIZE;
6820           else
6821             size += DWARF_OFFSET_SIZE;
6822           break;
6823         case dw_val_class_fde_ref:
6824           size += DWARF_OFFSET_SIZE;
6825           break;
6826         case dw_val_class_lbl_id:
6827           size += DWARF2_ADDR_SIZE;
6828           break;
6829         case dw_val_class_lineptr:
6830         case dw_val_class_macptr:
6831           size += DWARF_OFFSET_SIZE;
6832           break;
6833         case dw_val_class_str:
6834           if (AT_string_form (a) == DW_FORM_strp)
6835             size += DWARF_OFFSET_SIZE;
6836           else
6837             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6838           break;
6839         case dw_val_class_file:
6840           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6841           break;
6842         default:
6843           gcc_unreachable ();
6844         }
6845     }
6846
6847   return size;
6848 }
6849
6850 /* Size the debugging information associated with a given DIE.  Visits the
6851    DIE's children recursively.  Updates the global variable next_die_offset, on
6852    each time through.  Uses the current value of next_die_offset to update the
6853    die_offset field in each DIE.  */
6854
6855 static void
6856 calc_die_sizes (dw_die_ref die)
6857 {
6858   dw_die_ref c;
6859
6860   die->die_offset = next_die_offset;
6861   next_die_offset += size_of_die (die);
6862
6863   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6864
6865   if (die->die_child != NULL)
6866     /* Count the null byte used to terminate sibling lists.  */
6867     next_die_offset += 1;
6868 }
6869
6870 /* Set the marks for a die and its children.  We do this so
6871    that we know whether or not a reference needs to use FORM_ref_addr; only
6872    DIEs in the same CU will be marked.  We used to clear out the offset
6873    and use that as the flag, but ran into ordering problems.  */
6874
6875 static void
6876 mark_dies (dw_die_ref die)
6877 {
6878   dw_die_ref c;
6879
6880   gcc_assert (!die->die_mark);
6881
6882   die->die_mark = 1;
6883   FOR_EACH_CHILD (die, c, mark_dies (c));
6884 }
6885
6886 /* Clear the marks for a die and its children.  */
6887
6888 static void
6889 unmark_dies (dw_die_ref die)
6890 {
6891   dw_die_ref c;
6892
6893   gcc_assert (die->die_mark);
6894
6895   die->die_mark = 0;
6896   FOR_EACH_CHILD (die, c, unmark_dies (c));
6897 }
6898
6899 /* Clear the marks for a die, its children and referred dies.  */
6900
6901 static void
6902 unmark_all_dies (dw_die_ref die)
6903 {
6904   dw_die_ref c;
6905   dw_attr_ref a;
6906   unsigned ix;
6907
6908   if (!die->die_mark)
6909     return;
6910   die->die_mark = 0;
6911
6912   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6913
6914   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6915     if (AT_class (a) == dw_val_class_die_ref)
6916       unmark_all_dies (AT_ref (a));
6917 }
6918
6919 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6920    generated for the compilation unit.  */
6921
6922 static unsigned long
6923 size_of_pubnames (VEC (pubname_entry, gc) * names)
6924 {
6925   unsigned long size;
6926   unsigned i;
6927   pubname_ref p;
6928
6929   size = DWARF_PUBNAMES_HEADER_SIZE;
6930   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6931     if (names != pubtype_table
6932         || p->die->die_offset != 0
6933         || !flag_eliminate_unused_debug_types)
6934       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6935
6936   size += DWARF_OFFSET_SIZE;
6937   return size;
6938 }
6939
6940 /* Return the size of the information in the .debug_aranges section.  */
6941
6942 static unsigned long
6943 size_of_aranges (void)
6944 {
6945   unsigned long size;
6946
6947   size = DWARF_ARANGES_HEADER_SIZE;
6948
6949   /* Count the address/length pair for this compilation unit.  */
6950   if (text_section_used)
6951     size += 2 * DWARF2_ADDR_SIZE;
6952   if (cold_text_section_used)
6953     size += 2 * DWARF2_ADDR_SIZE;
6954   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6955
6956   /* Count the two zero words used to terminated the address range table.  */
6957   size += 2 * DWARF2_ADDR_SIZE;
6958   return size;
6959 }
6960 \f
6961 /* Select the encoding of an attribute value.  */
6962
6963 static enum dwarf_form
6964 value_format (dw_attr_ref a)
6965 {
6966   switch (a->dw_attr_val.val_class)
6967     {
6968     case dw_val_class_addr:
6969       return DW_FORM_addr;
6970     case dw_val_class_range_list:
6971     case dw_val_class_offset:
6972     case dw_val_class_loc_list:
6973       switch (DWARF_OFFSET_SIZE)
6974         {
6975         case 4:
6976           return DW_FORM_data4;
6977         case 8:
6978           return DW_FORM_data8;
6979         default:
6980           gcc_unreachable ();
6981         }
6982     case dw_val_class_loc:
6983       switch (constant_size (size_of_locs (AT_loc (a))))
6984         {
6985         case 1:
6986           return DW_FORM_block1;
6987         case 2:
6988           return DW_FORM_block2;
6989         default:
6990           gcc_unreachable ();
6991         }
6992     case dw_val_class_const:
6993       return DW_FORM_sdata;
6994     case dw_val_class_unsigned_const:
6995       switch (constant_size (AT_unsigned (a)))
6996         {
6997         case 1:
6998           return DW_FORM_data1;
6999         case 2:
7000           return DW_FORM_data2;
7001         case 4:
7002           return DW_FORM_data4;
7003         case 8:
7004           return DW_FORM_data8;
7005         default:
7006           gcc_unreachable ();
7007         }
7008     case dw_val_class_long_long:
7009       return DW_FORM_block1;
7010     case dw_val_class_vec:
7011       return DW_FORM_block1;
7012     case dw_val_class_flag:
7013       return DW_FORM_flag;
7014     case dw_val_class_die_ref:
7015       if (AT_ref_external (a))
7016         return DW_FORM_ref_addr;
7017       else
7018         return DW_FORM_ref;
7019     case dw_val_class_fde_ref:
7020       return DW_FORM_data;
7021     case dw_val_class_lbl_id:
7022       return DW_FORM_addr;
7023     case dw_val_class_lineptr:
7024     case dw_val_class_macptr:
7025       return DW_FORM_data;
7026     case dw_val_class_str:
7027       return AT_string_form (a);
7028     case dw_val_class_file:
7029       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7030         {
7031         case 1:
7032           return DW_FORM_data1;
7033         case 2:
7034           return DW_FORM_data2;
7035         case 4:
7036           return DW_FORM_data4;
7037         default:
7038           gcc_unreachable ();
7039         }
7040
7041     default:
7042       gcc_unreachable ();
7043     }
7044 }
7045
7046 /* Output the encoding of an attribute value.  */
7047
7048 static void
7049 output_value_format (dw_attr_ref a)
7050 {
7051   enum dwarf_form form = value_format (a);
7052
7053   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7054 }
7055
7056 /* Output the .debug_abbrev section which defines the DIE abbreviation
7057    table.  */
7058
7059 static void
7060 output_abbrev_section (void)
7061 {
7062   unsigned long abbrev_id;
7063
7064   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7065     {
7066       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7067       unsigned ix;
7068       dw_attr_ref a_attr;
7069
7070       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7071       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7072                                    dwarf_tag_name (abbrev->die_tag));
7073
7074       if (abbrev->die_child != NULL)
7075         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7076       else
7077         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7078
7079       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7080            ix++)
7081         {
7082           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7083                                        dwarf_attr_name (a_attr->dw_attr));
7084           output_value_format (a_attr);
7085         }
7086
7087       dw2_asm_output_data (1, 0, NULL);
7088       dw2_asm_output_data (1, 0, NULL);
7089     }
7090
7091   /* Terminate the table.  */
7092   dw2_asm_output_data (1, 0, NULL);
7093 }
7094
7095 /* Output a symbol we can use to refer to this DIE from another CU.  */
7096
7097 static inline void
7098 output_die_symbol (dw_die_ref die)
7099 {
7100   char *sym = die->die_symbol;
7101
7102   if (sym == 0)
7103     return;
7104
7105   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7106     /* We make these global, not weak; if the target doesn't support
7107        .linkonce, it doesn't support combining the sections, so debugging
7108        will break.  */
7109     targetm.asm_out.globalize_label (asm_out_file, sym);
7110
7111   ASM_OUTPUT_LABEL (asm_out_file, sym);
7112 }
7113
7114 /* Return a new location list, given the begin and end range, and the
7115    expression. gensym tells us whether to generate a new internal symbol for
7116    this location list node, which is done for the head of the list only.  */
7117
7118 static inline dw_loc_list_ref
7119 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7120               const char *section, unsigned int gensym)
7121 {
7122   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7123
7124   retlist->begin = begin;
7125   retlist->end = end;
7126   retlist->expr = expr;
7127   retlist->section = section;
7128   if (gensym)
7129     retlist->ll_symbol = gen_internal_sym ("LLST");
7130
7131   return retlist;
7132 }
7133
7134 /* Add a location description expression to a location list.  */
7135
7136 static inline void
7137 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7138                            const char *begin, const char *end,
7139                            const char *section)
7140 {
7141   dw_loc_list_ref *d;
7142
7143   /* Find the end of the chain.  */
7144   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7145     ;
7146
7147   /* Add a new location list node to the list.  */
7148   *d = new_loc_list (descr, begin, end, section, 0);
7149 }
7150
7151 /* Output the location list given to us.  */
7152
7153 static void
7154 output_loc_list (dw_loc_list_ref list_head)
7155 {
7156   dw_loc_list_ref curr = list_head;
7157
7158   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7159
7160   /* Walk the location list, and output each range + expression.  */
7161   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7162     {
7163       unsigned long size;
7164       /* Don't output an entry that starts and ends at the same address.  */
7165       if (strcmp (curr->begin, curr->end) == 0)
7166         continue;
7167       if (!have_multiple_function_sections)
7168         {
7169           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7170                                 "Location list begin address (%s)",
7171                                 list_head->ll_symbol);
7172           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7173                                 "Location list end address (%s)",
7174                                 list_head->ll_symbol);
7175         }
7176       else
7177         {
7178           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7179                                "Location list begin address (%s)",
7180                                list_head->ll_symbol);
7181           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7182                                "Location list end address (%s)",
7183                                list_head->ll_symbol);
7184         }
7185       size = size_of_locs (curr->expr);
7186
7187       /* Output the block length for this list of location operations.  */
7188       gcc_assert (size <= 0xffff);
7189       dw2_asm_output_data (2, size, "%s", "Location expression size");
7190
7191       output_loc_sequence (curr->expr);
7192     }
7193
7194   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7195                        "Location list terminator begin (%s)",
7196                        list_head->ll_symbol);
7197   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7198                        "Location list terminator end (%s)",
7199                        list_head->ll_symbol);
7200 }
7201
7202 /* Output the DIE and its attributes.  Called recursively to generate
7203    the definitions of each child DIE.  */
7204
7205 static void
7206 output_die (dw_die_ref die)
7207 {
7208   dw_attr_ref a;
7209   dw_die_ref c;
7210   unsigned long size;
7211   unsigned ix;
7212
7213   /* If someone in another CU might refer to us, set up a symbol for
7214      them to point to.  */
7215   if (die->die_symbol)
7216     output_die_symbol (die);
7217
7218   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7219                                (unsigned long)die->die_offset,
7220                                dwarf_tag_name (die->die_tag));
7221
7222   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7223     {
7224       const char *name = dwarf_attr_name (a->dw_attr);
7225
7226       switch (AT_class (a))
7227         {
7228         case dw_val_class_addr:
7229           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7230           break;
7231
7232         case dw_val_class_offset:
7233           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7234                                "%s", name);
7235           break;
7236
7237         case dw_val_class_range_list:
7238           {
7239             char *p = strchr (ranges_section_label, '\0');
7240
7241             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7242                      a->dw_attr_val.v.val_offset);
7243             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7244                                    debug_ranges_section, "%s", name);
7245             *p = '\0';
7246           }
7247           break;
7248
7249         case dw_val_class_loc:
7250           size = size_of_locs (AT_loc (a));
7251
7252           /* Output the block length for this list of location operations.  */
7253           dw2_asm_output_data (constant_size (size), size, "%s", name);
7254
7255           output_loc_sequence (AT_loc (a));
7256           break;
7257
7258         case dw_val_class_const:
7259           /* ??? It would be slightly more efficient to use a scheme like is
7260              used for unsigned constants below, but gdb 4.x does not sign
7261              extend.  Gdb 5.x does sign extend.  */
7262           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7263           break;
7264
7265         case dw_val_class_unsigned_const:
7266           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7267                                AT_unsigned (a), "%s", name);
7268           break;
7269
7270         case dw_val_class_long_long:
7271           {
7272             unsigned HOST_WIDE_INT first, second;
7273
7274             dw2_asm_output_data (1,
7275                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7276                                  "%s", name);
7277
7278             if (WORDS_BIG_ENDIAN)
7279               {
7280                 first = a->dw_attr_val.v.val_long_long.hi;
7281                 second = a->dw_attr_val.v.val_long_long.low;
7282               }
7283             else
7284               {
7285                 first = a->dw_attr_val.v.val_long_long.low;
7286                 second = a->dw_attr_val.v.val_long_long.hi;
7287               }
7288
7289             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7290                                  first, "long long constant");
7291             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7292                                  second, NULL);
7293           }
7294           break;
7295
7296         case dw_val_class_vec:
7297           {
7298             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7299             unsigned int len = a->dw_attr_val.v.val_vec.length;
7300             unsigned int i;
7301             unsigned char *p;
7302
7303             dw2_asm_output_data (1, len * elt_size, "%s", name);
7304             if (elt_size > sizeof (HOST_WIDE_INT))
7305               {
7306                 elt_size /= 2;
7307                 len *= 2;
7308               }
7309             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7310                  i < len;
7311                  i++, p += elt_size)
7312               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7313                                    "fp or vector constant word %u", i);
7314             break;
7315           }
7316
7317         case dw_val_class_flag:
7318           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7319           break;
7320
7321         case dw_val_class_loc_list:
7322           {
7323             char *sym = AT_loc_list (a)->ll_symbol;
7324
7325             gcc_assert (sym);
7326             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7327                                    "%s", name);
7328           }
7329           break;
7330
7331         case dw_val_class_die_ref:
7332           if (AT_ref_external (a))
7333             {
7334               char *sym = AT_ref (a)->die_symbol;
7335
7336               gcc_assert (sym);
7337               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7338                                      "%s", name);
7339             }
7340           else
7341             {
7342               gcc_assert (AT_ref (a)->die_offset);
7343               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7344                                    "%s", name);
7345             }
7346           break;
7347
7348         case dw_val_class_fde_ref:
7349           {
7350             char l1[20];
7351
7352             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7353                                          a->dw_attr_val.v.val_fde_index * 2);
7354             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7355                                    "%s", name);
7356           }
7357           break;
7358
7359         case dw_val_class_lbl_id:
7360           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7361           break;
7362
7363         case dw_val_class_lineptr:
7364           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7365                                  debug_line_section, "%s", name);
7366           break;
7367
7368         case dw_val_class_macptr:
7369           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7370                                  debug_macinfo_section, "%s", name);
7371           break;
7372
7373         case dw_val_class_str:
7374           if (AT_string_form (a) == DW_FORM_strp)
7375             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7376                                    a->dw_attr_val.v.val_str->label,
7377                                    debug_str_section,
7378                                    "%s: \"%s\"", name, AT_string (a));
7379           else
7380             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7381           break;
7382
7383         case dw_val_class_file:
7384           {
7385             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7386
7387             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7388                                  a->dw_attr_val.v.val_file->filename);
7389             break;
7390           }
7391
7392         default:
7393           gcc_unreachable ();
7394         }
7395     }
7396
7397   FOR_EACH_CHILD (die, c, output_die (c));
7398
7399   /* Add null byte to terminate sibling list.  */
7400   if (die->die_child != NULL)
7401     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7402                          (unsigned long) die->die_offset);
7403 }
7404
7405 /* Output the compilation unit that appears at the beginning of the
7406    .debug_info section, and precedes the DIE descriptions.  */
7407
7408 static void
7409 output_compilation_unit_header (void)
7410 {
7411   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7412     dw2_asm_output_data (4, 0xffffffff,
7413       "Initial length escape value indicating 64-bit DWARF extension");
7414   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7415                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7416                        "Length of Compilation Unit Info");
7417   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7418   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7419                          debug_abbrev_section,
7420                          "Offset Into Abbrev. Section");
7421   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7422 }
7423
7424 /* Output the compilation unit DIE and its children.  */
7425
7426 static void
7427 output_comp_unit (dw_die_ref die, int output_if_empty)
7428 {
7429   const char *secname;
7430   char *oldsym, *tmp;
7431
7432   /* Unless we are outputting main CU, we may throw away empty ones.  */
7433   if (!output_if_empty && die->die_child == NULL)
7434     return;
7435
7436   /* Even if there are no children of this DIE, we must output the information
7437      about the compilation unit.  Otherwise, on an empty translation unit, we
7438      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7439      will then complain when examining the file.  First mark all the DIEs in
7440      this CU so we know which get local refs.  */
7441   mark_dies (die);
7442
7443   build_abbrev_table (die);
7444
7445   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7446   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7447   calc_die_sizes (die);
7448
7449   oldsym = die->die_symbol;
7450   if (oldsym)
7451     {
7452       tmp = alloca (strlen (oldsym) + 24);
7453
7454       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7455       secname = tmp;
7456       die->die_symbol = NULL;
7457       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7458     }
7459   else
7460     switch_to_section (debug_info_section);
7461
7462   /* Output debugging information.  */
7463   output_compilation_unit_header ();
7464   output_die (die);
7465
7466   /* Leave the marks on the main CU, so we can check them in
7467      output_pubnames.  */
7468   if (oldsym)
7469     {
7470       unmark_dies (die);
7471       die->die_symbol = oldsym;
7472     }
7473 }
7474
7475 /* Return the DWARF2/3 pubname associated with a decl.  */
7476
7477 static const char *
7478 dwarf2_name (tree decl, int scope)
7479 {
7480   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7481 }
7482
7483 /* Add a new entry to .debug_pubnames if appropriate.  */
7484
7485 static void
7486 add_pubname_string (const char *str, dw_die_ref die)
7487 {
7488   pubname_entry e;
7489
7490   e.die = die;
7491   e.name = xstrdup (str);
7492   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7493 }
7494
7495 static void
7496 add_pubname (tree decl, dw_die_ref die)
7497 {
7498
7499   if (TREE_PUBLIC (decl))
7500     add_pubname_string (dwarf2_name (decl, 1), die);
7501 }
7502
7503 /* Add a new entry to .debug_pubtypes if appropriate.  */
7504
7505 static void
7506 add_pubtype (tree decl, dw_die_ref die)
7507 {
7508   pubname_entry e;
7509
7510   e.name = NULL;
7511   if ((TREE_PUBLIC (decl)
7512        || die->die_parent == comp_unit_die)
7513       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7514     {
7515       e.die = die;
7516       if (TYPE_P (decl))
7517         {
7518           if (TYPE_NAME (decl))
7519             {
7520               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7521                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7522               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7523                        && DECL_NAME (TYPE_NAME (decl)))
7524                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7525               else
7526                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7527             }
7528         }
7529       else
7530         e.name = xstrdup (dwarf2_name (decl, 1));
7531
7532       /* If we don't have a name for the type, there's no point in adding
7533          it to the table.  */
7534       if (e.name && e.name[0] != '\0')
7535         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7536     }
7537 }
7538
7539 /* Output the public names table used to speed up access to externally
7540    visible names; or the public types table used to find type definitions.  */
7541
7542 static void
7543 output_pubnames (VEC (pubname_entry, gc) * names)
7544 {
7545   unsigned i;
7546   unsigned long pubnames_length = size_of_pubnames (names);
7547   pubname_ref pub;
7548
7549   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7550     dw2_asm_output_data (4, 0xffffffff,
7551       "Initial length escape value indicating 64-bit DWARF extension");
7552   if (names == pubname_table)
7553     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7554                          "Length of Public Names Info");
7555   else
7556     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7557                          "Length of Public Type Names Info");
7558   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7559   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7560                          debug_info_section,
7561                          "Offset of Compilation Unit Info");
7562   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7563                        "Compilation Unit Length");
7564
7565   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7566     {
7567       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7568       if (names == pubname_table)
7569         gcc_assert (pub->die->die_mark);
7570
7571       if (names != pubtype_table
7572           || pub->die->die_offset != 0
7573           || !flag_eliminate_unused_debug_types)
7574         {
7575           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7576                                "DIE offset");
7577
7578           dw2_asm_output_nstring (pub->name, -1, "external name");
7579         }
7580     }
7581
7582   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7583 }
7584
7585 /* Add a new entry to .debug_aranges if appropriate.  */
7586
7587 static void
7588 add_arange (tree decl, dw_die_ref die)
7589 {
7590   if (! DECL_SECTION_NAME (decl))
7591     return;
7592
7593   if (arange_table_in_use == arange_table_allocated)
7594     {
7595       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7596       arange_table = ggc_realloc (arange_table,
7597                                   (arange_table_allocated
7598                                    * sizeof (dw_die_ref)));
7599       memset (arange_table + arange_table_in_use, 0,
7600               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7601     }
7602
7603   arange_table[arange_table_in_use++] = die;
7604 }
7605
7606 /* Output the information that goes into the .debug_aranges table.
7607    Namely, define the beginning and ending address range of the
7608    text section generated for this compilation unit.  */
7609
7610 static void
7611 output_aranges (void)
7612 {
7613   unsigned i;
7614   unsigned long aranges_length = size_of_aranges ();
7615
7616   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7617     dw2_asm_output_data (4, 0xffffffff,
7618       "Initial length escape value indicating 64-bit DWARF extension");
7619   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7620                        "Length of Address Ranges Info");
7621   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7622   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7623                          debug_info_section,
7624                          "Offset of Compilation Unit Info");
7625   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7626   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7627
7628   /* We need to align to twice the pointer size here.  */
7629   if (DWARF_ARANGES_PAD_SIZE)
7630     {
7631       /* Pad using a 2 byte words so that padding is correct for any
7632          pointer size.  */
7633       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7634                            2 * DWARF2_ADDR_SIZE);
7635       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7636         dw2_asm_output_data (2, 0, NULL);
7637     }
7638
7639   /* It is necessary not to output these entries if the sections were
7640      not used; if the sections were not used, the length will be 0 and
7641      the address may end up as 0 if the section is discarded by ld
7642      --gc-sections, leaving an invalid (0, 0) entry that can be
7643      confused with the terminator.  */
7644   if (text_section_used)
7645     {
7646       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7647       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7648                             text_section_label, "Length");
7649     }
7650   if (cold_text_section_used)
7651     {
7652       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7653                            "Address");
7654       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7655                             cold_text_section_label, "Length");
7656     }
7657
7658   for (i = 0; i < arange_table_in_use; i++)
7659     {
7660       dw_die_ref die = arange_table[i];
7661
7662       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7663       gcc_assert (die->die_mark);
7664
7665       if (die->die_tag == DW_TAG_subprogram)
7666         {
7667           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7668                                "Address");
7669           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7670                                 get_AT_low_pc (die), "Length");
7671         }
7672       else
7673         {
7674           /* A static variable; extract the symbol from DW_AT_location.
7675              Note that this code isn't currently hit, as we only emit
7676              aranges for functions (jason 9/23/99).  */
7677           dw_attr_ref a = get_AT (die, DW_AT_location);
7678           dw_loc_descr_ref loc;
7679
7680           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7681
7682           loc = AT_loc (a);
7683           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7684
7685           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7686                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7687           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7688                                get_AT_unsigned (die, DW_AT_byte_size),
7689                                "Length");
7690         }
7691     }
7692
7693   /* Output the terminator words.  */
7694   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7695   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7696 }
7697
7698 /* Add a new entry to .debug_ranges.  Return the offset at which it
7699    was placed.  */
7700
7701 static unsigned int
7702 add_ranges_num (int num)
7703 {
7704   unsigned int in_use = ranges_table_in_use;
7705
7706   if (in_use == ranges_table_allocated)
7707     {
7708       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7709       ranges_table
7710         = ggc_realloc (ranges_table, (ranges_table_allocated
7711                                       * sizeof (struct dw_ranges_struct)));
7712       memset (ranges_table + ranges_table_in_use, 0,
7713               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7714     }
7715
7716   ranges_table[in_use].num = num;
7717   ranges_table_in_use = in_use + 1;
7718
7719   return in_use * 2 * DWARF2_ADDR_SIZE;
7720 }
7721
7722 /* Add a new entry to .debug_ranges corresponding to a block, or a
7723    range terminator if BLOCK is NULL.  */
7724
7725 static unsigned int
7726 add_ranges (const_tree block)
7727 {
7728   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7729 }
7730
7731 /* Add a new entry to .debug_ranges corresponding to a pair of
7732    labels.  */
7733
7734 static unsigned int
7735 add_ranges_by_labels (const char *begin, const char *end)
7736 {
7737   unsigned int in_use = ranges_by_label_in_use;
7738
7739   if (in_use == ranges_by_label_allocated)
7740     {
7741       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7742       ranges_by_label
7743         = ggc_realloc (ranges_by_label,
7744                        (ranges_by_label_allocated
7745                         * sizeof (struct dw_ranges_by_label_struct)));
7746       memset (ranges_by_label + ranges_by_label_in_use, 0,
7747               RANGES_TABLE_INCREMENT
7748               * sizeof (struct dw_ranges_by_label_struct));
7749     }
7750
7751   ranges_by_label[in_use].begin = begin;
7752   ranges_by_label[in_use].end = end;
7753   ranges_by_label_in_use = in_use + 1;
7754
7755   return add_ranges_num (-(int)in_use - 1);
7756 }
7757
7758 static void
7759 output_ranges (void)
7760 {
7761   unsigned i;
7762   static const char *const start_fmt = "Offset 0x%x";
7763   const char *fmt = start_fmt;
7764
7765   for (i = 0; i < ranges_table_in_use; i++)
7766     {
7767       int block_num = ranges_table[i].num;
7768
7769       if (block_num > 0)
7770         {
7771           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7772           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7773
7774           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7775           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7776
7777           /* If all code is in the text section, then the compilation
7778              unit base address defaults to DW_AT_low_pc, which is the
7779              base of the text section.  */
7780           if (!have_multiple_function_sections)
7781             {
7782               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7783                                     text_section_label,
7784                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7785               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7786                                     text_section_label, NULL);
7787             }
7788
7789           /* Otherwise, the compilation unit base address is zero,
7790              which allows us to use absolute addresses, and not worry
7791              about whether the target supports cross-section
7792              arithmetic.  */
7793           else
7794             {
7795               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7796                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7797               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7798             }
7799
7800           fmt = NULL;
7801         }
7802
7803       /* Negative block_num stands for an index into ranges_by_label.  */
7804       else if (block_num < 0)
7805         {
7806           int lab_idx = - block_num - 1;
7807
7808           if (!have_multiple_function_sections)
7809             {
7810               gcc_unreachable ();
7811 #if 0
7812               /* If we ever use add_ranges_by_labels () for a single
7813                  function section, all we have to do is to take out
7814                  the #if 0 above.  */
7815               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7816                                     ranges_by_label[lab_idx].begin,
7817                                     text_section_label,
7818                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7819               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7820                                     ranges_by_label[lab_idx].end,
7821                                     text_section_label, NULL);
7822 #endif
7823             }
7824           else
7825             {
7826               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7827                                    ranges_by_label[lab_idx].begin,
7828                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7829               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7830                                    ranges_by_label[lab_idx].end,
7831                                    NULL);
7832             }
7833         }
7834       else
7835         {
7836           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7837           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7838           fmt = start_fmt;
7839         }
7840     }
7841 }
7842
7843 /* Data structure containing information about input files.  */
7844 struct file_info
7845 {
7846   const char *path;     /* Complete file name.  */
7847   const char *fname;    /* File name part.  */
7848   int length;           /* Length of entire string.  */
7849   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7850   int dir_idx;          /* Index in directory table.  */
7851 };
7852
7853 /* Data structure containing information about directories with source
7854    files.  */
7855 struct dir_info
7856 {
7857   const char *path;     /* Path including directory name.  */
7858   int length;           /* Path length.  */
7859   int prefix;           /* Index of directory entry which is a prefix.  */
7860   int count;            /* Number of files in this directory.  */
7861   int dir_idx;          /* Index of directory used as base.  */
7862 };
7863
7864 /* Callback function for file_info comparison.  We sort by looking at
7865    the directories in the path.  */
7866
7867 static int
7868 file_info_cmp (const void *p1, const void *p2)
7869 {
7870   const struct file_info *s1 = p1;
7871   const struct file_info *s2 = p2;
7872   const unsigned char *cp1;
7873   const unsigned char *cp2;
7874
7875   /* Take care of file names without directories.  We need to make sure that
7876      we return consistent values to qsort since some will get confused if
7877      we return the same value when identical operands are passed in opposite
7878      orders.  So if neither has a directory, return 0 and otherwise return
7879      1 or -1 depending on which one has the directory.  */
7880   if ((s1->path == s1->fname || s2->path == s2->fname))
7881     return (s2->path == s2->fname) - (s1->path == s1->fname);
7882
7883   cp1 = (const unsigned char *) s1->path;
7884   cp2 = (const unsigned char *) s2->path;
7885
7886   while (1)
7887     {
7888       ++cp1;
7889       ++cp2;
7890       /* Reached the end of the first path?  If so, handle like above.  */
7891       if ((cp1 == (const unsigned char *) s1->fname)
7892           || (cp2 == (const unsigned char *) s2->fname))
7893         return ((cp2 == (const unsigned char *) s2->fname)
7894                 - (cp1 == (const unsigned char *) s1->fname));
7895
7896       /* Character of current path component the same?  */
7897       else if (*cp1 != *cp2)
7898         return *cp1 - *cp2;
7899     }
7900 }
7901
7902 struct file_name_acquire_data
7903 {
7904   struct file_info *files;
7905   int used_files;
7906   int max_files;
7907 };
7908
7909 /* Traversal function for the hash table.  */
7910
7911 static int
7912 file_name_acquire (void ** slot, void *data)
7913 {
7914   struct file_name_acquire_data *fnad = data;
7915   struct dwarf_file_data *d = *slot;
7916   struct file_info *fi;
7917   const char *f;
7918
7919   gcc_assert (fnad->max_files >= d->emitted_number);
7920
7921   if (! d->emitted_number)
7922     return 1;
7923
7924   gcc_assert (fnad->max_files != fnad->used_files);
7925
7926   fi = fnad->files + fnad->used_files++;
7927
7928   /* Skip all leading "./".  */
7929   f = d->filename;
7930   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7931     f += 2;
7932
7933   /* Create a new array entry.  */
7934   fi->path = f;
7935   fi->length = strlen (f);
7936   fi->file_idx = d;
7937
7938   /* Search for the file name part.  */
7939   f = strrchr (f, DIR_SEPARATOR);
7940 #if defined (DIR_SEPARATOR_2)
7941   {
7942     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7943
7944     if (g != NULL)
7945       {
7946         if (f == NULL || f < g)
7947           f = g;
7948       }
7949   }
7950 #endif
7951
7952   fi->fname = f == NULL ? fi->path : f + 1;
7953   return 1;
7954 }
7955
7956 /* Output the directory table and the file name table.  We try to minimize
7957    the total amount of memory needed.  A heuristic is used to avoid large
7958    slowdowns with many input files.  */
7959
7960 static void
7961 output_file_names (void)
7962 {
7963   struct file_name_acquire_data fnad;
7964   int numfiles;
7965   struct file_info *files;
7966   struct dir_info *dirs;
7967   int *saved;
7968   int *savehere;
7969   int *backmap;
7970   int ndirs;
7971   int idx_offset;
7972   int i;
7973   int idx;
7974
7975   if (!last_emitted_file)
7976     {
7977       dw2_asm_output_data (1, 0, "End directory table");
7978       dw2_asm_output_data (1, 0, "End file name table");
7979       return;
7980     }
7981
7982   numfiles = last_emitted_file->emitted_number;
7983
7984   /* Allocate the various arrays we need.  */
7985   files = alloca (numfiles * sizeof (struct file_info));
7986   dirs = alloca (numfiles * sizeof (struct dir_info));
7987
7988   fnad.files = files;
7989   fnad.used_files = 0;
7990   fnad.max_files = numfiles;
7991   htab_traverse (file_table, file_name_acquire, &fnad);
7992   gcc_assert (fnad.used_files == fnad.max_files);
7993
7994   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7995
7996   /* Find all the different directories used.  */
7997   dirs[0].path = files[0].path;
7998   dirs[0].length = files[0].fname - files[0].path;
7999   dirs[0].prefix = -1;
8000   dirs[0].count = 1;
8001   dirs[0].dir_idx = 0;
8002   files[0].dir_idx = 0;
8003   ndirs = 1;
8004
8005   for (i = 1; i < numfiles; i++)
8006     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8007         && memcmp (dirs[ndirs - 1].path, files[i].path,
8008                    dirs[ndirs - 1].length) == 0)
8009       {
8010         /* Same directory as last entry.  */
8011         files[i].dir_idx = ndirs - 1;
8012         ++dirs[ndirs - 1].count;
8013       }
8014     else
8015       {
8016         int j;
8017
8018         /* This is a new directory.  */
8019         dirs[ndirs].path = files[i].path;
8020         dirs[ndirs].length = files[i].fname - files[i].path;
8021         dirs[ndirs].count = 1;
8022         dirs[ndirs].dir_idx = ndirs;
8023         files[i].dir_idx = ndirs;
8024
8025         /* Search for a prefix.  */
8026         dirs[ndirs].prefix = -1;
8027         for (j = 0; j < ndirs; j++)
8028           if (dirs[j].length < dirs[ndirs].length
8029               && dirs[j].length > 1
8030               && (dirs[ndirs].prefix == -1
8031                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8032               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8033             dirs[ndirs].prefix = j;
8034
8035         ++ndirs;
8036       }
8037
8038   /* Now to the actual work.  We have to find a subset of the directories which
8039      allow expressing the file name using references to the directory table
8040      with the least amount of characters.  We do not do an exhaustive search
8041      where we would have to check out every combination of every single
8042      possible prefix.  Instead we use a heuristic which provides nearly optimal
8043      results in most cases and never is much off.  */
8044   saved = alloca (ndirs * sizeof (int));
8045   savehere = alloca (ndirs * sizeof (int));
8046
8047   memset (saved, '\0', ndirs * sizeof (saved[0]));
8048   for (i = 0; i < ndirs; i++)
8049     {
8050       int j;
8051       int total;
8052
8053       /* We can always save some space for the current directory.  But this
8054          does not mean it will be enough to justify adding the directory.  */
8055       savehere[i] = dirs[i].length;
8056       total = (savehere[i] - saved[i]) * dirs[i].count;
8057
8058       for (j = i + 1; j < ndirs; j++)
8059         {
8060           savehere[j] = 0;
8061           if (saved[j] < dirs[i].length)
8062             {
8063               /* Determine whether the dirs[i] path is a prefix of the
8064                  dirs[j] path.  */
8065               int k;
8066
8067               k = dirs[j].prefix;
8068               while (k != -1 && k != (int) i)
8069                 k = dirs[k].prefix;
8070
8071               if (k == (int) i)
8072                 {
8073                   /* Yes it is.  We can possibly save some memory by
8074                      writing the filenames in dirs[j] relative to
8075                      dirs[i].  */
8076                   savehere[j] = dirs[i].length;
8077                   total += (savehere[j] - saved[j]) * dirs[j].count;
8078                 }
8079             }
8080         }
8081
8082       /* Check whether we can save enough to justify adding the dirs[i]
8083          directory.  */
8084       if (total > dirs[i].length + 1)
8085         {
8086           /* It's worthwhile adding.  */
8087           for (j = i; j < ndirs; j++)
8088             if (savehere[j] > 0)
8089               {
8090                 /* Remember how much we saved for this directory so far.  */
8091                 saved[j] = savehere[j];
8092
8093                 /* Remember the prefix directory.  */
8094                 dirs[j].dir_idx = i;
8095               }
8096         }
8097     }
8098
8099   /* Emit the directory name table.  */
8100   idx = 1;
8101   idx_offset = dirs[0].length > 0 ? 1 : 0;
8102   for (i = 1 - idx_offset; i < ndirs; i++)
8103     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8104                             "Directory Entry: 0x%x", i + idx_offset);
8105
8106   dw2_asm_output_data (1, 0, "End directory table");
8107
8108   /* We have to emit them in the order of emitted_number since that's
8109      used in the debug info generation.  To do this efficiently we
8110      generate a back-mapping of the indices first.  */
8111   backmap = alloca (numfiles * sizeof (int));
8112   for (i = 0; i < numfiles; i++)
8113     backmap[files[i].file_idx->emitted_number - 1] = i;
8114
8115   /* Now write all the file names.  */
8116   for (i = 0; i < numfiles; i++)
8117     {
8118       int file_idx = backmap[i];
8119       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8120
8121       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8122                               "File Entry: 0x%x", (unsigned) i + 1);
8123
8124       /* Include directory index.  */
8125       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8126
8127       /* Modification time.  */
8128       dw2_asm_output_data_uleb128 (0, NULL);
8129
8130       /* File length in bytes.  */
8131       dw2_asm_output_data_uleb128 (0, NULL);
8132     }
8133
8134   dw2_asm_output_data (1, 0, "End file name table");
8135 }
8136
8137
8138 /* Output the source line number correspondence information.  This
8139    information goes into the .debug_line section.  */
8140
8141 static void
8142 output_line_info (void)
8143 {
8144   char l1[20], l2[20], p1[20], p2[20];
8145   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8146   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8147   unsigned opc;
8148   unsigned n_op_args;
8149   unsigned long lt_index;
8150   unsigned long current_line;
8151   long line_offset;
8152   long line_delta;
8153   unsigned long current_file;
8154   unsigned long function;
8155
8156   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8157   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8158   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8159   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8160
8161   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8162     dw2_asm_output_data (4, 0xffffffff,
8163       "Initial length escape value indicating 64-bit DWARF extension");
8164   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8165                         "Length of Source Line Info");
8166   ASM_OUTPUT_LABEL (asm_out_file, l1);
8167
8168   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8169   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8170   ASM_OUTPUT_LABEL (asm_out_file, p1);
8171
8172   /* Define the architecture-dependent minimum instruction length (in
8173    bytes).  In this implementation of DWARF, this field is used for
8174    information purposes only.  Since GCC generates assembly language,
8175    we have no a priori knowledge of how many instruction bytes are
8176    generated for each source line, and therefore can use only the
8177    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8178    commands.  Accordingly, we fix this as `1', which is "correct
8179    enough" for all architectures, and don't let the target override.  */
8180   dw2_asm_output_data (1, 1,
8181                        "Minimum Instruction Length");
8182
8183   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8184                        "Default is_stmt_start flag");
8185   dw2_asm_output_data (1, DWARF_LINE_BASE,
8186                        "Line Base Value (Special Opcodes)");
8187   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8188                        "Line Range Value (Special Opcodes)");
8189   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8190                        "Special Opcode Base");
8191
8192   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8193     {
8194       switch (opc)
8195         {
8196         case DW_LNS_advance_pc:
8197         case DW_LNS_advance_line:
8198         case DW_LNS_set_file:
8199         case DW_LNS_set_column:
8200         case DW_LNS_fixed_advance_pc:
8201           n_op_args = 1;
8202           break;
8203         default:
8204           n_op_args = 0;
8205           break;
8206         }
8207
8208       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8209                            opc, n_op_args);
8210     }
8211
8212   /* Write out the information about the files we use.  */
8213   output_file_names ();
8214   ASM_OUTPUT_LABEL (asm_out_file, p2);
8215
8216   /* We used to set the address register to the first location in the text
8217      section here, but that didn't accomplish anything since we already
8218      have a line note for the opening brace of the first function.  */
8219
8220   /* Generate the line number to PC correspondence table, encoded as
8221      a series of state machine operations.  */
8222   current_file = 1;
8223   current_line = 1;
8224
8225   if (cfun && in_cold_section_p)
8226     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8227   else
8228     strcpy (prev_line_label, text_section_label);
8229   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8230     {
8231       dw_line_info_ref line_info = &line_info_table[lt_index];
8232
8233 #if 0
8234       /* Disable this optimization for now; GDB wants to see two line notes
8235          at the beginning of a function so it can find the end of the
8236          prologue.  */
8237
8238       /* Don't emit anything for redundant notes.  Just updating the
8239          address doesn't accomplish anything, because we already assume
8240          that anything after the last address is this line.  */
8241       if (line_info->dw_line_num == current_line
8242           && line_info->dw_file_num == current_file)
8243         continue;
8244 #endif
8245
8246       /* Emit debug info for the address of the current line.
8247
8248          Unfortunately, we have little choice here currently, and must always
8249          use the most general form.  GCC does not know the address delta
8250          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8251          attributes which will give an upper bound on the address range.  We
8252          could perhaps use length attributes to determine when it is safe to
8253          use DW_LNS_fixed_advance_pc.  */
8254
8255       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8256       if (0)
8257         {
8258           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8259           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8260                                "DW_LNS_fixed_advance_pc");
8261           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8262         }
8263       else
8264         {
8265           /* This can handle any delta.  This takes
8266              4+DWARF2_ADDR_SIZE bytes.  */
8267           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8268           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8269           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8270           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8271         }
8272
8273       strcpy (prev_line_label, line_label);
8274
8275       /* Emit debug info for the source file of the current line, if
8276          different from the previous line.  */
8277       if (line_info->dw_file_num != current_file)
8278         {
8279           current_file = line_info->dw_file_num;
8280           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8281           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8282         }
8283
8284       /* Emit debug info for the current line number, choosing the encoding
8285          that uses the least amount of space.  */
8286       if (line_info->dw_line_num != current_line)
8287         {
8288           line_offset = line_info->dw_line_num - current_line;
8289           line_delta = line_offset - DWARF_LINE_BASE;
8290           current_line = line_info->dw_line_num;
8291           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8292             /* This can handle deltas from -10 to 234, using the current
8293                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8294                takes 1 byte.  */
8295             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8296                                  "line %lu", current_line);
8297           else
8298             {
8299               /* This can handle any delta.  This takes at least 4 bytes,
8300                  depending on the value being encoded.  */
8301               dw2_asm_output_data (1, DW_LNS_advance_line,
8302                                    "advance to line %lu", current_line);
8303               dw2_asm_output_data_sleb128 (line_offset, NULL);
8304               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8305             }
8306         }
8307       else
8308         /* We still need to start a new row, so output a copy insn.  */
8309         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8310     }
8311
8312   /* Emit debug info for the address of the end of the function.  */
8313   if (0)
8314     {
8315       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8316                            "DW_LNS_fixed_advance_pc");
8317       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8318     }
8319   else
8320     {
8321       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8322       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8323       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8324       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8325     }
8326
8327   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8328   dw2_asm_output_data_uleb128 (1, NULL);
8329   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8330
8331   function = 0;
8332   current_file = 1;
8333   current_line = 1;
8334   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8335     {
8336       dw_separate_line_info_ref line_info
8337         = &separate_line_info_table[lt_index];
8338
8339 #if 0
8340       /* Don't emit anything for redundant notes.  */
8341       if (line_info->dw_line_num == current_line
8342           && line_info->dw_file_num == current_file
8343           && line_info->function == function)
8344         goto cont;
8345 #endif
8346
8347       /* Emit debug info for the address of the current line.  If this is
8348          a new function, or the first line of a function, then we need
8349          to handle it differently.  */
8350       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8351                                    lt_index);
8352       if (function != line_info->function)
8353         {
8354           function = line_info->function;
8355
8356           /* Set the address register to the first line in the function.  */
8357           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8358           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8359           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8360           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8361         }
8362       else
8363         {
8364           /* ??? See the DW_LNS_advance_pc comment above.  */
8365           if (0)
8366             {
8367               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8368                                    "DW_LNS_fixed_advance_pc");
8369               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8370             }
8371           else
8372             {
8373               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8374               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8375               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8376               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8377             }
8378         }
8379
8380       strcpy (prev_line_label, line_label);
8381
8382       /* Emit debug info for the source file of the current line, if
8383          different from the previous line.  */
8384       if (line_info->dw_file_num != current_file)
8385         {
8386           current_file = line_info->dw_file_num;
8387           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8388           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8389         }
8390
8391       /* Emit debug info for the current line number, choosing the encoding
8392          that uses the least amount of space.  */
8393       if (line_info->dw_line_num != current_line)
8394         {
8395           line_offset = line_info->dw_line_num - current_line;
8396           line_delta = line_offset - DWARF_LINE_BASE;
8397           current_line = line_info->dw_line_num;
8398           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8399             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8400                                  "line %lu", current_line);
8401           else
8402             {
8403               dw2_asm_output_data (1, DW_LNS_advance_line,
8404                                    "advance to line %lu", current_line);
8405               dw2_asm_output_data_sleb128 (line_offset, NULL);
8406               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8407             }
8408         }
8409       else
8410         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8411
8412 #if 0
8413     cont:
8414 #endif
8415
8416       lt_index++;
8417
8418       /* If we're done with a function, end its sequence.  */
8419       if (lt_index == separate_line_info_table_in_use
8420           || separate_line_info_table[lt_index].function != function)
8421         {
8422           current_file = 1;
8423           current_line = 1;
8424
8425           /* Emit debug info for the address of the end of the function.  */
8426           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8427           if (0)
8428             {
8429               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8430                                    "DW_LNS_fixed_advance_pc");
8431               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8432             }
8433           else
8434             {
8435               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8436               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8437               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8438               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8439             }
8440
8441           /* Output the marker for the end of this sequence.  */
8442           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8443           dw2_asm_output_data_uleb128 (1, NULL);
8444           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8445         }
8446     }
8447
8448   /* Output the marker for the end of the line number info.  */
8449   ASM_OUTPUT_LABEL (asm_out_file, l2);
8450 }
8451 \f
8452 /* Given a pointer to a tree node for some base type, return a pointer to
8453    a DIE that describes the given type.
8454
8455    This routine must only be called for GCC type nodes that correspond to
8456    Dwarf base (fundamental) types.  */
8457
8458 static dw_die_ref
8459 base_type_die (tree type)
8460 {
8461   dw_die_ref base_type_result;
8462   enum dwarf_type encoding;
8463
8464   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8465     return 0;
8466
8467   switch (TREE_CODE (type))
8468     {
8469     case INTEGER_TYPE:
8470       if (TYPE_STRING_FLAG (type))
8471         {
8472           if (TYPE_UNSIGNED (type))
8473             encoding = DW_ATE_unsigned_char;
8474           else
8475             encoding = DW_ATE_signed_char;
8476         }
8477       else if (TYPE_UNSIGNED (type))
8478         encoding = DW_ATE_unsigned;
8479       else
8480         encoding = DW_ATE_signed;
8481       break;
8482
8483     case REAL_TYPE:
8484       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8485         encoding = DW_ATE_decimal_float;
8486       else
8487         encoding = DW_ATE_float;
8488       break;
8489
8490     case FIXED_POINT_TYPE:
8491       if (TYPE_UNSIGNED (type))
8492         encoding = DW_ATE_unsigned_fixed;
8493       else
8494         encoding = DW_ATE_signed_fixed;
8495       break;
8496
8497       /* Dwarf2 doesn't know anything about complex ints, so use
8498          a user defined type for it.  */
8499     case COMPLEX_TYPE:
8500       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8501         encoding = DW_ATE_complex_float;
8502       else
8503         encoding = DW_ATE_lo_user;
8504       break;
8505
8506     case BOOLEAN_TYPE:
8507       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8508       encoding = DW_ATE_boolean;
8509       break;
8510
8511     default:
8512       /* No other TREE_CODEs are Dwarf fundamental types.  */
8513       gcc_unreachable ();
8514     }
8515
8516   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8517
8518   /* This probably indicates a bug.  */
8519   if (! TYPE_NAME (type))
8520     add_name_attribute (base_type_result, "__unknown__");
8521
8522   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8523                    int_size_in_bytes (type));
8524   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8525
8526   return base_type_result;
8527 }
8528
8529 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8530    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8531
8532 static inline int
8533 is_base_type (tree type)
8534 {
8535   switch (TREE_CODE (type))
8536     {
8537     case ERROR_MARK:
8538     case VOID_TYPE:
8539     case INTEGER_TYPE:
8540     case REAL_TYPE:
8541     case FIXED_POINT_TYPE:
8542     case COMPLEX_TYPE:
8543     case BOOLEAN_TYPE:
8544       return 1;
8545
8546     case ARRAY_TYPE:
8547     case RECORD_TYPE:
8548     case UNION_TYPE:
8549     case QUAL_UNION_TYPE:
8550     case ENUMERAL_TYPE:
8551     case FUNCTION_TYPE:
8552     case METHOD_TYPE:
8553     case POINTER_TYPE:
8554     case REFERENCE_TYPE:
8555     case OFFSET_TYPE:
8556     case LANG_TYPE:
8557     case VECTOR_TYPE:
8558       return 0;
8559
8560     default:
8561       gcc_unreachable ();
8562     }
8563
8564   return 0;
8565 }
8566
8567 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8568    node, return the size in bits for the type if it is a constant, or else
8569    return the alignment for the type if the type's size is not constant, or
8570    else return BITS_PER_WORD if the type actually turns out to be an
8571    ERROR_MARK node.  */
8572
8573 static inline unsigned HOST_WIDE_INT
8574 simple_type_size_in_bits (const_tree type)
8575 {
8576   if (TREE_CODE (type) == ERROR_MARK)
8577     return BITS_PER_WORD;
8578   else if (TYPE_SIZE (type) == NULL_TREE)
8579     return 0;
8580   else if (host_integerp (TYPE_SIZE (type), 1))
8581     return tree_low_cst (TYPE_SIZE (type), 1);
8582   else
8583     return TYPE_ALIGN (type);
8584 }
8585
8586 /* Return true if the debug information for the given type should be
8587    emitted as a subrange type.  */
8588
8589 static inline bool
8590 is_subrange_type (const_tree type)
8591 {
8592   tree subtype = TREE_TYPE (type);
8593
8594   /* Subrange types are identified by the fact that they are integer
8595      types, and that they have a subtype which is either an integer type
8596      or an enumeral type.  */
8597
8598   if (TREE_CODE (type) != INTEGER_TYPE
8599       || subtype == NULL_TREE)
8600     return false;
8601
8602   if (TREE_CODE (subtype) != INTEGER_TYPE
8603       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8604     return false;
8605
8606   if (TREE_CODE (type) == TREE_CODE (subtype)
8607       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8608       && TYPE_MIN_VALUE (type) != NULL
8609       && TYPE_MIN_VALUE (subtype) != NULL
8610       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8611       && TYPE_MAX_VALUE (type) != NULL
8612       && TYPE_MAX_VALUE (subtype) != NULL
8613       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8614     {
8615       /* The type and its subtype have the same representation.  If in
8616          addition the two types also have the same name, then the given
8617          type is not a subrange type, but rather a plain base type.  */
8618       /* FIXME: brobecker/2004-03-22:
8619          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8620          therefore be sufficient to check the TYPE_SIZE node pointers
8621          rather than checking the actual size.  Unfortunately, we have
8622          found some cases, such as in the Ada "integer" type, where
8623          this is not the case.  Until this problem is solved, we need to
8624          keep checking the actual size.  */
8625       tree type_name = TYPE_NAME (type);
8626       tree subtype_name = TYPE_NAME (subtype);
8627
8628       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8629         type_name = DECL_NAME (type_name);
8630
8631       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8632         subtype_name = DECL_NAME (subtype_name);
8633
8634       if (type_name == subtype_name)
8635         return false;
8636     }
8637
8638   return true;
8639 }
8640
8641 /*  Given a pointer to a tree node for a subrange type, return a pointer
8642     to a DIE that describes the given type.  */
8643
8644 static dw_die_ref
8645 subrange_type_die (tree type, dw_die_ref context_die)
8646 {
8647   dw_die_ref subrange_die;
8648   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8649
8650   if (context_die == NULL)
8651     context_die = comp_unit_die;
8652
8653   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8654
8655   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8656     {
8657       /* The size of the subrange type and its base type do not match,
8658          so we need to generate a size attribute for the subrange type.  */
8659       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8660     }
8661
8662   if (TYPE_MIN_VALUE (type) != NULL)
8663     add_bound_info (subrange_die, DW_AT_lower_bound,
8664                     TYPE_MIN_VALUE (type));
8665   if (TYPE_MAX_VALUE (type) != NULL)
8666     add_bound_info (subrange_die, DW_AT_upper_bound,
8667                     TYPE_MAX_VALUE (type));
8668
8669   return subrange_die;
8670 }
8671
8672 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8673    entry that chains various modifiers in front of the given type.  */
8674
8675 static dw_die_ref
8676 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8677                    dw_die_ref context_die)
8678 {
8679   enum tree_code code = TREE_CODE (type);
8680   dw_die_ref mod_type_die;
8681   dw_die_ref sub_die = NULL;
8682   tree item_type = NULL;
8683   tree qualified_type;
8684   tree name;
8685
8686   if (code == ERROR_MARK)
8687     return NULL;
8688
8689   /* See if we already have the appropriately qualified variant of
8690      this type.  */
8691   qualified_type
8692     = get_qualified_type (type,
8693                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8694                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8695
8696   /* If we do, then we can just use its DIE, if it exists.  */
8697   if (qualified_type)
8698     {
8699       mod_type_die = lookup_type_die (qualified_type);
8700       if (mod_type_die)
8701         return mod_type_die;
8702     }
8703
8704   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8705
8706   /* Handle C typedef types.  */
8707   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8708     {
8709       tree dtype = TREE_TYPE (name);
8710
8711       if (qualified_type == dtype)
8712         {
8713           /* For a named type, use the typedef.  */
8714           gen_type_die (qualified_type, context_die);
8715           return lookup_type_die (qualified_type);
8716         }
8717       else if (is_const_type < TYPE_READONLY (dtype)
8718                || is_volatile_type < TYPE_VOLATILE (dtype)
8719                || (is_const_type <= TYPE_READONLY (dtype)
8720                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8721                    && DECL_ORIGINAL_TYPE (name) != type))
8722         /* cv-unqualified version of named type.  Just use the unnamed
8723            type to which it refers.  */
8724         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8725                                   is_const_type, is_volatile_type,
8726                                   context_die);
8727       /* Else cv-qualified version of named type; fall through.  */
8728     }
8729
8730   if (is_const_type)
8731     {
8732       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8733       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8734     }
8735   else if (is_volatile_type)
8736     {
8737       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8738       sub_die = modified_type_die (type, 0, 0, context_die);
8739     }
8740   else if (code == POINTER_TYPE)
8741     {
8742       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8743       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8744                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8745       item_type = TREE_TYPE (type);
8746     }
8747   else if (code == REFERENCE_TYPE)
8748     {
8749       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8750       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8751                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8752       item_type = TREE_TYPE (type);
8753     }
8754   else if (is_subrange_type (type))
8755     {
8756       mod_type_die = subrange_type_die (type, context_die);
8757       item_type = TREE_TYPE (type);
8758     }
8759   else if (is_base_type (type))
8760     mod_type_die = base_type_die (type);
8761   else
8762     {
8763       gen_type_die (type, context_die);
8764
8765       /* We have to get the type_main_variant here (and pass that to the
8766          `lookup_type_die' routine) because the ..._TYPE node we have
8767          might simply be a *copy* of some original type node (where the
8768          copy was created to help us keep track of typedef names) and
8769          that copy might have a different TYPE_UID from the original
8770          ..._TYPE node.  */
8771       if (TREE_CODE (type) != VECTOR_TYPE)
8772         return lookup_type_die (type_main_variant (type));
8773       else
8774         /* Vectors have the debugging information in the type,
8775            not the main variant.  */
8776         return lookup_type_die (type);
8777     }
8778
8779   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8780      don't output a DW_TAG_typedef, since there isn't one in the
8781      user's program; just attach a DW_AT_name to the type.  */
8782   if (name
8783       && (TREE_CODE (name) != TYPE_DECL
8784           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8785     {
8786       if (TREE_CODE (name) == TYPE_DECL)
8787         /* Could just call add_name_and_src_coords_attributes here,
8788            but since this is a builtin type it doesn't have any
8789            useful source coordinates anyway.  */
8790         name = DECL_NAME (name);
8791       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8792     }
8793
8794   if (qualified_type)
8795     equate_type_number_to_die (qualified_type, mod_type_die);
8796
8797   if (item_type)
8798     /* We must do this after the equate_type_number_to_die call, in case
8799        this is a recursive type.  This ensures that the modified_type_die
8800        recursion will terminate even if the type is recursive.  Recursive
8801        types are possible in Ada.  */
8802     sub_die = modified_type_die (item_type,
8803                                  TYPE_READONLY (item_type),
8804                                  TYPE_VOLATILE (item_type),
8805                                  context_die);
8806
8807   if (sub_die != NULL)
8808     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8809
8810   return mod_type_die;
8811 }
8812
8813 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8814    an enumerated type.  */
8815
8816 static inline int
8817 type_is_enum (const_tree type)
8818 {
8819   return TREE_CODE (type) == ENUMERAL_TYPE;
8820 }
8821
8822 /* Return the DBX register number described by a given RTL node.  */
8823
8824 static unsigned int
8825 dbx_reg_number (const_rtx rtl)
8826 {
8827   unsigned regno = REGNO (rtl);
8828
8829   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8830
8831 #ifdef LEAF_REG_REMAP
8832   if (current_function_uses_only_leaf_regs)
8833     {
8834       int leaf_reg = LEAF_REG_REMAP (regno);
8835       if (leaf_reg != -1)
8836         regno = (unsigned) leaf_reg;
8837     }
8838 #endif
8839
8840   return DBX_REGISTER_NUMBER (regno);
8841 }
8842
8843 /* Optionally add a DW_OP_piece term to a location description expression.
8844    DW_OP_piece is only added if the location description expression already
8845    doesn't end with DW_OP_piece.  */
8846
8847 static void
8848 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8849 {
8850   dw_loc_descr_ref loc;
8851
8852   if (*list_head != NULL)
8853     {
8854       /* Find the end of the chain.  */
8855       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8856         ;
8857
8858       if (loc->dw_loc_opc != DW_OP_piece)
8859         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8860     }
8861 }
8862
8863 /* Return a location descriptor that designates a machine register or
8864    zero if there is none.  */
8865
8866 static dw_loc_descr_ref
8867 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8868 {
8869   rtx regs;
8870
8871   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8872     return 0;
8873
8874   regs = targetm.dwarf_register_span (rtl);
8875
8876   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8877     return multiple_reg_loc_descriptor (rtl, regs, initialized);
8878   else
8879     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8880 }
8881
8882 /* Return a location descriptor that designates a machine register for
8883    a given hard register number.  */
8884
8885 static dw_loc_descr_ref
8886 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8887 {
8888   dw_loc_descr_ref reg_loc_descr;
8889   if (regno <= 31)
8890     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8891   else
8892     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8893
8894   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8895     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8896
8897   return reg_loc_descr;
8898 }
8899
8900 /* Given an RTL of a register, return a location descriptor that
8901    designates a value that spans more than one register.  */
8902
8903 static dw_loc_descr_ref
8904 multiple_reg_loc_descriptor (rtx rtl, rtx regs, 
8905                              enum var_init_status initialized)
8906 {
8907   int nregs, size, i;
8908   unsigned reg;
8909   dw_loc_descr_ref loc_result = NULL;
8910
8911   reg = REGNO (rtl);
8912 #ifdef LEAF_REG_REMAP
8913   if (current_function_uses_only_leaf_regs)
8914     {
8915       int leaf_reg = LEAF_REG_REMAP (reg);
8916       if (leaf_reg != -1)
8917         reg = (unsigned) leaf_reg;
8918     }
8919 #endif
8920   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8921   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8922
8923   /* Simple, contiguous registers.  */
8924   if (regs == NULL_RTX)
8925     {
8926       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8927
8928       loc_result = NULL;
8929       while (nregs--)
8930         {
8931           dw_loc_descr_ref t;
8932
8933           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8934                                       VAR_INIT_STATUS_INITIALIZED);
8935           add_loc_descr (&loc_result, t);
8936           add_loc_descr_op_piece (&loc_result, size);
8937           ++reg;
8938         }
8939       return loc_result;
8940     }
8941
8942   /* Now onto stupid register sets in non contiguous locations.  */
8943
8944   gcc_assert (GET_CODE (regs) == PARALLEL);
8945
8946   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8947   loc_result = NULL;
8948
8949   for (i = 0; i < XVECLEN (regs, 0); ++i)
8950     {
8951       dw_loc_descr_ref t;
8952
8953       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8954                                   VAR_INIT_STATUS_INITIALIZED);
8955       add_loc_descr (&loc_result, t);
8956       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8957       add_loc_descr_op_piece (&loc_result, size);
8958     }
8959
8960   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8961     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8962   return loc_result;
8963 }
8964
8965 /* Return a location descriptor that designates a constant.  */
8966
8967 static dw_loc_descr_ref
8968 int_loc_descriptor (HOST_WIDE_INT i)
8969 {
8970   enum dwarf_location_atom op;
8971
8972   /* Pick the smallest representation of a constant, rather than just
8973      defaulting to the LEB encoding.  */
8974   if (i >= 0)
8975     {
8976       if (i <= 31)
8977         op = DW_OP_lit0 + i;
8978       else if (i <= 0xff)
8979         op = DW_OP_const1u;
8980       else if (i <= 0xffff)
8981         op = DW_OP_const2u;
8982       else if (HOST_BITS_PER_WIDE_INT == 32
8983                || i <= 0xffffffff)
8984         op = DW_OP_const4u;
8985       else
8986         op = DW_OP_constu;
8987     }
8988   else
8989     {
8990       if (i >= -0x80)
8991         op = DW_OP_const1s;
8992       else if (i >= -0x8000)
8993         op = DW_OP_const2s;
8994       else if (HOST_BITS_PER_WIDE_INT == 32
8995                || i >= -0x80000000)
8996         op = DW_OP_const4s;
8997       else
8998         op = DW_OP_consts;
8999     }
9000
9001   return new_loc_descr (op, i, 0);
9002 }
9003
9004 /* Return a location descriptor that designates a base+offset location.  */
9005
9006 static dw_loc_descr_ref
9007 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9008                  enum var_init_status initialized)
9009 {
9010   unsigned int regno;
9011   dw_loc_descr_ref result;
9012
9013   /* We only use "frame base" when we're sure we're talking about the
9014      post-prologue local stack frame.  We do this by *not* running
9015      register elimination until this point, and recognizing the special
9016      argument pointer and soft frame pointer rtx's.  */
9017   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9018     {
9019       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9020
9021       if (elim != reg)
9022         {
9023           if (GET_CODE (elim) == PLUS)
9024             {
9025               offset += INTVAL (XEXP (elim, 1));
9026               elim = XEXP (elim, 0);
9027             }
9028           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
9029                       : stack_pointer_rtx));
9030           offset += frame_pointer_fb_offset;
9031
9032           return new_loc_descr (DW_OP_fbreg, offset, 0);
9033         }
9034     }
9035
9036   regno = dbx_reg_number (reg);
9037   if (regno <= 31)
9038     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9039   else
9040     result = new_loc_descr (DW_OP_bregx, regno, offset);
9041
9042   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9043     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9044
9045   return result;
9046 }
9047
9048 /* Return true if this RTL expression describes a base+offset calculation.  */
9049
9050 static inline int
9051 is_based_loc (const_rtx rtl)
9052 {
9053   return (GET_CODE (rtl) == PLUS
9054           && ((REG_P (XEXP (rtl, 0))
9055                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9056                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9057 }
9058
9059 /* Return a descriptor that describes the concatenation of N locations
9060    used to form the address of a memory location.  */
9061
9062 static dw_loc_descr_ref
9063 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9064                             enum var_init_status initialized)
9065 {
9066   unsigned int i;
9067   dw_loc_descr_ref cc_loc_result = NULL;
9068   unsigned int n = XVECLEN (concatn, 0);
9069
9070   for (i = 0; i < n; ++i)
9071     {
9072       dw_loc_descr_ref ref;
9073       rtx x = XVECEXP (concatn, 0, i);
9074
9075       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9076       if (ref == NULL)
9077         return NULL;
9078
9079       add_loc_descr (&cc_loc_result, ref);
9080       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9081     }
9082
9083   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9084     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9085
9086   return cc_loc_result;
9087 }
9088
9089 /* The following routine converts the RTL for a variable or parameter
9090    (resident in memory) into an equivalent Dwarf representation of a
9091    mechanism for getting the address of that same variable onto the top of a
9092    hypothetical "address evaluation" stack.
9093
9094    When creating memory location descriptors, we are effectively transforming
9095    the RTL for a memory-resident object into its Dwarf postfix expression
9096    equivalent.  This routine recursively descends an RTL tree, turning
9097    it into Dwarf postfix code as it goes.
9098
9099    MODE is the mode of the memory reference, needed to handle some
9100    autoincrement addressing modes.
9101
9102    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9103    location list for RTL.
9104
9105    Return 0 if we can't represent the location.  */
9106
9107 static dw_loc_descr_ref
9108 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9109                     enum var_init_status initialized)
9110 {
9111   dw_loc_descr_ref mem_loc_result = NULL;
9112   enum dwarf_location_atom op;
9113
9114   /* Note that for a dynamically sized array, the location we will generate a
9115      description of here will be the lowest numbered location which is
9116      actually within the array.  That's *not* necessarily the same as the
9117      zeroth element of the array.  */
9118
9119   rtl = targetm.delegitimize_address (rtl);
9120
9121   switch (GET_CODE (rtl))
9122     {
9123     case POST_INC:
9124     case POST_DEC:
9125     case POST_MODIFY:
9126       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9127          just fall into the SUBREG code.  */
9128
9129       /* ... fall through ...  */
9130
9131     case SUBREG:
9132       /* The case of a subreg may arise when we have a local (register)
9133          variable or a formal (register) parameter which doesn't quite fill
9134          up an entire register.  For now, just assume that it is
9135          legitimate to make the Dwarf info refer to the whole register which
9136          contains the given subreg.  */
9137       rtl = XEXP (rtl, 0);
9138
9139       /* ... fall through ...  */
9140
9141     case REG:
9142       /* Whenever a register number forms a part of the description of the
9143          method for calculating the (dynamic) address of a memory resident
9144          object, DWARF rules require the register number be referred to as
9145          a "base register".  This distinction is not based in any way upon
9146          what category of register the hardware believes the given register
9147          belongs to.  This is strictly DWARF terminology we're dealing with
9148          here. Note that in cases where the location of a memory-resident
9149          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9150          OP_CONST (0)) the actual DWARF location descriptor that we generate
9151          may just be OP_BASEREG (basereg).  This may look deceptively like
9152          the object in question was allocated to a register (rather than in
9153          memory) so DWARF consumers need to be aware of the subtle
9154          distinction between OP_REG and OP_BASEREG.  */
9155       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9156         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9157       break;
9158
9159     case MEM:
9160       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9161                                            VAR_INIT_STATUS_INITIALIZED);
9162       if (mem_loc_result != 0)
9163         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9164       break;
9165
9166     case LO_SUM:
9167          rtl = XEXP (rtl, 1);
9168
9169       /* ... fall through ...  */
9170
9171     case LABEL_REF:
9172       /* Some ports can transform a symbol ref into a label ref, because
9173          the symbol ref is too far away and has to be dumped into a constant
9174          pool.  */
9175     case CONST:
9176     case SYMBOL_REF:
9177       /* Alternatively, the symbol in the constant pool might be referenced
9178          by a different symbol.  */
9179       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9180         {
9181           bool marked;
9182           rtx tmp = get_pool_constant_mark (rtl, &marked);
9183
9184           if (GET_CODE (tmp) == SYMBOL_REF)
9185             {
9186               rtl = tmp;
9187               if (CONSTANT_POOL_ADDRESS_P (tmp))
9188                 get_pool_constant_mark (tmp, &marked);
9189               else
9190                 marked = true;
9191             }
9192
9193           /* If all references to this pool constant were optimized away,
9194              it was not output and thus we can't represent it.
9195              FIXME: might try to use DW_OP_const_value here, though
9196              DW_OP_piece complicates it.  */
9197           if (!marked)
9198             return 0;
9199         }
9200
9201       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9202       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9203       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9204       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9205       break;
9206
9207     case PRE_MODIFY:
9208       /* Extract the PLUS expression nested inside and fall into
9209          PLUS code below.  */
9210       rtl = XEXP (rtl, 1);
9211       goto plus;
9212
9213     case PRE_INC:
9214     case PRE_DEC:
9215       /* Turn these into a PLUS expression and fall into the PLUS code
9216          below.  */
9217       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9218                           GEN_INT (GET_CODE (rtl) == PRE_INC
9219                                    ? GET_MODE_UNIT_SIZE (mode)
9220                                    : -GET_MODE_UNIT_SIZE (mode)));
9221
9222       /* ... fall through ...  */
9223
9224     case PLUS:
9225     plus:
9226       if (is_based_loc (rtl))
9227         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9228                                           INTVAL (XEXP (rtl, 1)),
9229                                           VAR_INIT_STATUS_INITIALIZED);
9230       else
9231         {
9232           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9233                                                VAR_INIT_STATUS_INITIALIZED);
9234           if (mem_loc_result == 0)
9235             break;
9236
9237           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9238               && INTVAL (XEXP (rtl, 1)) >= 0)
9239             add_loc_descr (&mem_loc_result,
9240                            new_loc_descr (DW_OP_plus_uconst,
9241                                           INTVAL (XEXP (rtl, 1)), 0));
9242           else
9243             {
9244               add_loc_descr (&mem_loc_result,
9245                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9246                                                  VAR_INIT_STATUS_INITIALIZED));
9247               add_loc_descr (&mem_loc_result,
9248                              new_loc_descr (DW_OP_plus, 0, 0));
9249             }
9250         }
9251       break;
9252
9253     /* If a pseudo-reg is optimized away, it is possible for it to
9254        be replaced with a MEM containing a multiply or shift.  */
9255     case MULT:
9256       op = DW_OP_mul;
9257       goto do_binop;
9258
9259     case ASHIFT:
9260       op = DW_OP_shl;
9261       goto do_binop;
9262
9263     case ASHIFTRT:
9264       op = DW_OP_shra;
9265       goto do_binop;
9266
9267     case LSHIFTRT:
9268       op = DW_OP_shr;
9269       goto do_binop;
9270
9271     do_binop:
9272       {
9273         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9274                                                    VAR_INIT_STATUS_INITIALIZED);
9275         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9276                                                    VAR_INIT_STATUS_INITIALIZED);
9277
9278         if (op0 == 0 || op1 == 0)
9279           break;
9280
9281         mem_loc_result = op0;
9282         add_loc_descr (&mem_loc_result, op1);
9283         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9284         break;
9285       }
9286
9287     case CONST_INT:
9288       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9289       break;
9290
9291     case CONCATN:
9292       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode, 
9293                                                    VAR_INIT_STATUS_INITIALIZED);
9294       break;
9295
9296     default:
9297       gcc_unreachable ();
9298     }
9299
9300   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9301     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9302
9303   return mem_loc_result;
9304 }
9305
9306 /* Return a descriptor that describes the concatenation of two locations.
9307    This is typically a complex variable.  */
9308
9309 static dw_loc_descr_ref
9310 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9311 {
9312   dw_loc_descr_ref cc_loc_result = NULL;
9313   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9314   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9315
9316   if (x0_ref == 0 || x1_ref == 0)
9317     return 0;
9318
9319   cc_loc_result = x0_ref;
9320   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9321
9322   add_loc_descr (&cc_loc_result, x1_ref);
9323   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9324
9325   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9326     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9327
9328   return cc_loc_result;
9329 }
9330
9331 /* Return a descriptor that describes the concatenation of N
9332    locations.  */
9333
9334 static dw_loc_descr_ref
9335 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9336 {
9337   unsigned int i;
9338   dw_loc_descr_ref cc_loc_result = NULL;
9339   unsigned int n = XVECLEN (concatn, 0);
9340
9341   for (i = 0; i < n; ++i)
9342     {
9343       dw_loc_descr_ref ref;
9344       rtx x = XVECEXP (concatn, 0, i);
9345
9346       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9347       if (ref == NULL)
9348         return NULL;
9349
9350       add_loc_descr (&cc_loc_result, ref);
9351       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9352     }
9353
9354   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9355     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9356
9357   return cc_loc_result;
9358 }
9359
9360 /* Output a proper Dwarf location descriptor for a variable or parameter
9361    which is either allocated in a register or in a memory location.  For a
9362    register, we just generate an OP_REG and the register number.  For a
9363    memory location we provide a Dwarf postfix expression describing how to
9364    generate the (dynamic) address of the object onto the address stack.
9365
9366    If we don't know how to describe it, return 0.  */
9367
9368 static dw_loc_descr_ref
9369 loc_descriptor (rtx rtl, enum var_init_status initialized)
9370 {
9371   dw_loc_descr_ref loc_result = NULL;
9372
9373   switch (GET_CODE (rtl))
9374     {
9375     case SUBREG:
9376       /* The case of a subreg may arise when we have a local (register)
9377          variable or a formal (register) parameter which doesn't quite fill
9378          up an entire register.  For now, just assume that it is
9379          legitimate to make the Dwarf info refer to the whole register which
9380          contains the given subreg.  */
9381       rtl = SUBREG_REG (rtl);
9382
9383       /* ... fall through ...  */
9384
9385     case REG:
9386       loc_result = reg_loc_descriptor (rtl, initialized);
9387       break;
9388
9389     case MEM:
9390       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9391                                        initialized);
9392       break;
9393
9394     case CONCAT:
9395       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9396                                           initialized);
9397       break;
9398
9399     case CONCATN:
9400       loc_result = concatn_loc_descriptor (rtl, initialized);
9401       break;
9402
9403     case VAR_LOCATION:
9404       /* Single part.  */
9405       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9406         {
9407           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9408           break;
9409         }
9410
9411       rtl = XEXP (rtl, 1);
9412       /* FALLTHRU */
9413
9414     case PARALLEL:
9415       {
9416         rtvec par_elems = XVEC (rtl, 0);
9417         int num_elem = GET_NUM_ELEM (par_elems);
9418         enum machine_mode mode;
9419         int i;
9420
9421         /* Create the first one, so we have something to add to.  */
9422         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9423                                      initialized);
9424         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9425         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9426         for (i = 1; i < num_elem; i++)
9427           {
9428             dw_loc_descr_ref temp;
9429
9430             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9431                                    initialized);
9432             add_loc_descr (&loc_result, temp);
9433             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9434             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9435           }
9436       }
9437       break;
9438
9439     default:
9440       gcc_unreachable ();
9441     }
9442
9443   return loc_result;
9444 }
9445
9446 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9447    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9448    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9449    top-level invocation, and we require the address of LOC; is 0 if we require
9450    the value of LOC.  */
9451
9452 static dw_loc_descr_ref
9453 loc_descriptor_from_tree_1 (tree loc, int want_address)
9454 {
9455   dw_loc_descr_ref ret, ret1;
9456   int have_address = 0;
9457   enum dwarf_location_atom op;
9458
9459   /* ??? Most of the time we do not take proper care for sign/zero
9460      extending the values properly.  Hopefully this won't be a real
9461      problem...  */
9462
9463   switch (TREE_CODE (loc))
9464     {
9465     case ERROR_MARK:
9466       return 0;
9467
9468     case PLACEHOLDER_EXPR:
9469       /* This case involves extracting fields from an object to determine the
9470          position of other fields.  We don't try to encode this here.  The
9471          only user of this is Ada, which encodes the needed information using
9472          the names of types.  */
9473       return 0;
9474
9475     case CALL_EXPR:
9476       return 0;
9477
9478     case PREINCREMENT_EXPR:
9479     case PREDECREMENT_EXPR:
9480     case POSTINCREMENT_EXPR:
9481     case POSTDECREMENT_EXPR:
9482       /* There are no opcodes for these operations.  */
9483       return 0;
9484
9485     case ADDR_EXPR:
9486       /* If we already want an address, there's nothing we can do.  */
9487       if (want_address)
9488         return 0;
9489
9490       /* Otherwise, process the argument and look for the address.  */
9491       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9492
9493     case VAR_DECL:
9494       if (DECL_THREAD_LOCAL_P (loc))
9495         {
9496           rtx rtl;
9497           unsigned first_op;
9498           unsigned second_op;
9499
9500           if (targetm.have_tls)
9501             {
9502               /* If this is not defined, we have no way to emit the
9503                  data.  */
9504               if (!targetm.asm_out.output_dwarf_dtprel)
9505                 return 0;
9506
9507                /* The way DW_OP_GNU_push_tls_address is specified, we
9508                   can only look up addresses of objects in the current
9509                   module.  */
9510               if (DECL_EXTERNAL (loc))
9511                 return 0;
9512               first_op = INTERNAL_DW_OP_tls_addr;
9513               second_op = DW_OP_GNU_push_tls_address;
9514             }
9515           else
9516             {
9517               if (!targetm.emutls.debug_form_tls_address)
9518                 return 0;
9519               loc = emutls_decl (loc);
9520               first_op = DW_OP_addr;
9521               second_op = DW_OP_form_tls_address;
9522             }
9523           
9524           rtl = rtl_for_decl_location (loc);
9525           if (rtl == NULL_RTX)
9526             return 0;
9527
9528           if (!MEM_P (rtl))
9529             return 0;
9530           rtl = XEXP (rtl, 0);
9531           if (! CONSTANT_P (rtl))
9532             return 0;
9533
9534           ret = new_loc_descr (first_op, 0, 0);
9535           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9536           ret->dw_loc_oprnd1.v.val_addr = rtl;
9537           
9538           ret1 = new_loc_descr (second_op, 0, 0);
9539           add_loc_descr (&ret, ret1);
9540
9541           have_address = 1;
9542           break;
9543         }
9544       /* FALLTHRU */
9545
9546     case PARM_DECL:
9547       if (DECL_HAS_VALUE_EXPR_P (loc))
9548         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9549                                            want_address);
9550       /* FALLTHRU */
9551
9552     case RESULT_DECL:
9553     case FUNCTION_DECL:
9554       {
9555         rtx rtl = rtl_for_decl_location (loc);
9556
9557         if (rtl == NULL_RTX)
9558           return 0;
9559         else if (GET_CODE (rtl) == CONST_INT)
9560           {
9561             HOST_WIDE_INT val = INTVAL (rtl);
9562             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9563               val &= GET_MODE_MASK (DECL_MODE (loc));
9564             ret = int_loc_descriptor (val);
9565           }
9566         else if (GET_CODE (rtl) == CONST_STRING)
9567           return 0;
9568         else if (CONSTANT_P (rtl))
9569           {
9570             ret = new_loc_descr (DW_OP_addr, 0, 0);
9571             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9572             ret->dw_loc_oprnd1.v.val_addr = rtl;
9573           }
9574         else
9575           {
9576             enum machine_mode mode;
9577
9578             /* Certain constructs can only be represented at top-level.  */
9579             if (want_address == 2)
9580               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9581
9582             mode = GET_MODE (rtl);
9583             if (MEM_P (rtl))
9584               {
9585                 rtl = XEXP (rtl, 0);
9586                 have_address = 1;
9587               }
9588             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9589           }
9590       }
9591       break;
9592
9593     case INDIRECT_REF:
9594       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9595       have_address = 1;
9596       break;
9597
9598     case COMPOUND_EXPR:
9599       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9600
9601     case NOP_EXPR:
9602     case CONVERT_EXPR:
9603     case VIEW_CONVERT_EXPR:
9604     case SAVE_EXPR:
9605     case GIMPLE_MODIFY_STMT:
9606       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9607                                          want_address);
9608
9609     case COMPONENT_REF:
9610     case BIT_FIELD_REF:
9611     case ARRAY_REF:
9612     case ARRAY_RANGE_REF:
9613       {
9614         tree obj, offset;
9615         HOST_WIDE_INT bitsize, bitpos, bytepos;
9616         enum machine_mode mode;
9617         int volatilep;
9618         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9619
9620         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9621                                    &unsignedp, &volatilep, false);
9622
9623         if (obj == loc)
9624           return 0;
9625
9626         ret = loc_descriptor_from_tree_1 (obj, 1);
9627         if (ret == 0
9628             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9629           return 0;
9630
9631         if (offset != NULL_TREE)
9632           {
9633             /* Variable offset.  */
9634             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9635             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9636           }
9637
9638         bytepos = bitpos / BITS_PER_UNIT;
9639         if (bytepos > 0)
9640           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9641         else if (bytepos < 0)
9642           {
9643             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9644             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9645           }
9646
9647         have_address = 1;
9648         break;
9649       }
9650
9651     case INTEGER_CST:
9652       if (host_integerp (loc, 0))
9653         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9654       else
9655         return 0;
9656       break;
9657
9658     case CONSTRUCTOR:
9659       {
9660         /* Get an RTL for this, if something has been emitted.  */
9661         rtx rtl = lookup_constant_def (loc);
9662         enum machine_mode mode;
9663
9664         if (!rtl || !MEM_P (rtl))
9665           return 0;
9666         mode = GET_MODE (rtl);
9667         rtl = XEXP (rtl, 0);
9668         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9669         have_address = 1;
9670         break;
9671       }
9672
9673     case TRUTH_AND_EXPR:
9674     case TRUTH_ANDIF_EXPR:
9675     case BIT_AND_EXPR:
9676       op = DW_OP_and;
9677       goto do_binop;
9678
9679     case TRUTH_XOR_EXPR:
9680     case BIT_XOR_EXPR:
9681       op = DW_OP_xor;
9682       goto do_binop;
9683
9684     case TRUTH_OR_EXPR:
9685     case TRUTH_ORIF_EXPR:
9686     case BIT_IOR_EXPR:
9687       op = DW_OP_or;
9688       goto do_binop;
9689
9690     case FLOOR_DIV_EXPR:
9691     case CEIL_DIV_EXPR:
9692     case ROUND_DIV_EXPR:
9693     case TRUNC_DIV_EXPR:
9694       op = DW_OP_div;
9695       goto do_binop;
9696
9697     case MINUS_EXPR:
9698       op = DW_OP_minus;
9699       goto do_binop;
9700
9701     case FLOOR_MOD_EXPR:
9702     case CEIL_MOD_EXPR:
9703     case ROUND_MOD_EXPR:
9704     case TRUNC_MOD_EXPR:
9705       op = DW_OP_mod;
9706       goto do_binop;
9707
9708     case MULT_EXPR:
9709       op = DW_OP_mul;
9710       goto do_binop;
9711
9712     case LSHIFT_EXPR:
9713       op = DW_OP_shl;
9714       goto do_binop;
9715
9716     case RSHIFT_EXPR:
9717       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9718       goto do_binop;
9719
9720     case POINTER_PLUS_EXPR:
9721     case PLUS_EXPR:
9722       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9723           && host_integerp (TREE_OPERAND (loc, 1), 0))
9724         {
9725           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9726           if (ret == 0)
9727             return 0;
9728
9729           add_loc_descr (&ret,
9730                          new_loc_descr (DW_OP_plus_uconst,
9731                                         tree_low_cst (TREE_OPERAND (loc, 1),
9732                                                       0),
9733                                         0));
9734           break;
9735         }
9736
9737       op = DW_OP_plus;
9738       goto do_binop;
9739
9740     case LE_EXPR:
9741       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9742         return 0;
9743
9744       op = DW_OP_le;
9745       goto do_binop;
9746
9747     case GE_EXPR:
9748       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9749         return 0;
9750
9751       op = DW_OP_ge;
9752       goto do_binop;
9753
9754     case LT_EXPR:
9755       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9756         return 0;
9757
9758       op = DW_OP_lt;
9759       goto do_binop;
9760
9761     case GT_EXPR:
9762       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9763         return 0;
9764
9765       op = DW_OP_gt;
9766       goto do_binop;
9767
9768     case EQ_EXPR:
9769       op = DW_OP_eq;
9770       goto do_binop;
9771
9772     case NE_EXPR:
9773       op = DW_OP_ne;
9774       goto do_binop;
9775
9776     do_binop:
9777       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9778       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9779       if (ret == 0 || ret1 == 0)
9780         return 0;
9781
9782       add_loc_descr (&ret, ret1);
9783       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9784       break;
9785
9786     case TRUTH_NOT_EXPR:
9787     case BIT_NOT_EXPR:
9788       op = DW_OP_not;
9789       goto do_unop;
9790
9791     case ABS_EXPR:
9792       op = DW_OP_abs;
9793       goto do_unop;
9794
9795     case NEGATE_EXPR:
9796       op = DW_OP_neg;
9797       goto do_unop;
9798
9799     do_unop:
9800       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9801       if (ret == 0)
9802         return 0;
9803
9804       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9805       break;
9806
9807     case MIN_EXPR:
9808     case MAX_EXPR:
9809       {
9810         const enum tree_code code =
9811           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9812
9813         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9814                       build2 (code, integer_type_node,
9815                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9816                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9817       }
9818
9819       /* ... fall through ...  */
9820
9821     case COND_EXPR:
9822       {
9823         dw_loc_descr_ref lhs
9824           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9825         dw_loc_descr_ref rhs
9826           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9827         dw_loc_descr_ref bra_node, jump_node, tmp;
9828
9829         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9830         if (ret == 0 || lhs == 0 || rhs == 0)
9831           return 0;
9832
9833         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9834         add_loc_descr (&ret, bra_node);
9835
9836         add_loc_descr (&ret, rhs);
9837         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9838         add_loc_descr (&ret, jump_node);
9839
9840         add_loc_descr (&ret, lhs);
9841         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9842         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9843
9844         /* ??? Need a node to point the skip at.  Use a nop.  */
9845         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9846         add_loc_descr (&ret, tmp);
9847         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9848         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9849       }
9850       break;
9851
9852     case FIX_TRUNC_EXPR:
9853       return 0;
9854
9855     default:
9856       /* Leave front-end specific codes as simply unknown.  This comes
9857          up, for instance, with the C STMT_EXPR.  */
9858       if ((unsigned int) TREE_CODE (loc)
9859           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9860         return 0;
9861
9862 #ifdef ENABLE_CHECKING
9863       /* Otherwise this is a generic code; we should just lists all of
9864          these explicitly.  We forgot one.  */
9865       gcc_unreachable ();
9866 #else
9867       /* In a release build, we want to degrade gracefully: better to
9868          generate incomplete debugging information than to crash.  */
9869       return NULL;
9870 #endif
9871     }
9872
9873   /* Show if we can't fill the request for an address.  */
9874   if (want_address && !have_address)
9875     return 0;
9876
9877   /* If we've got an address and don't want one, dereference.  */
9878   if (!want_address && have_address && ret)
9879     {
9880       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9881
9882       if (size > DWARF2_ADDR_SIZE || size == -1)
9883         return 0;
9884       else if (size == DWARF2_ADDR_SIZE)
9885         op = DW_OP_deref;
9886       else
9887         op = DW_OP_deref_size;
9888
9889       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9890     }
9891
9892   return ret;
9893 }
9894
9895 static inline dw_loc_descr_ref
9896 loc_descriptor_from_tree (tree loc)
9897 {
9898   return loc_descriptor_from_tree_1 (loc, 2);
9899 }
9900
9901 /* Given a value, round it up to the lowest multiple of `boundary'
9902    which is not less than the value itself.  */
9903
9904 static inline HOST_WIDE_INT
9905 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9906 {
9907   return (((value + boundary - 1) / boundary) * boundary);
9908 }
9909
9910 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9911    pointer to the declared type for the relevant field variable, or return
9912    `integer_type_node' if the given node turns out to be an
9913    ERROR_MARK node.  */
9914
9915 static inline tree
9916 field_type (const_tree decl)
9917 {
9918   tree type;
9919
9920   if (TREE_CODE (decl) == ERROR_MARK)
9921     return integer_type_node;
9922
9923   type = DECL_BIT_FIELD_TYPE (decl);
9924   if (type == NULL_TREE)
9925     type = TREE_TYPE (decl);
9926
9927   return type;
9928 }
9929
9930 /* Given a pointer to a tree node, return the alignment in bits for
9931    it, or else return BITS_PER_WORD if the node actually turns out to
9932    be an ERROR_MARK node.  */
9933
9934 static inline unsigned
9935 simple_type_align_in_bits (const_tree type)
9936 {
9937   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9938 }
9939
9940 static inline unsigned
9941 simple_decl_align_in_bits (const_tree decl)
9942 {
9943   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9944 }
9945
9946 /* Return the result of rounding T up to ALIGN.  */
9947
9948 static inline HOST_WIDE_INT
9949 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9950 {
9951   /* We must be careful if T is negative because HOST_WIDE_INT can be
9952      either "above" or "below" unsigned int as per the C promotion
9953      rules, depending on the host, thus making the signedness of the
9954      direct multiplication and division unpredictable.  */
9955   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9956
9957   u += align - 1;
9958   u /= align;
9959   u *= align;
9960
9961   return (HOST_WIDE_INT) u;
9962 }
9963
9964 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9965    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9966    or return 0 if we are unable to determine what that offset is, either
9967    because the argument turns out to be a pointer to an ERROR_MARK node, or
9968    because the offset is actually variable.  (We can't handle the latter case
9969    just yet).  */
9970
9971 static HOST_WIDE_INT
9972 field_byte_offset (const_tree decl)
9973 {
9974   HOST_WIDE_INT object_offset_in_bits;
9975   HOST_WIDE_INT bitpos_int;
9976
9977   if (TREE_CODE (decl) == ERROR_MARK)
9978     return 0;
9979
9980   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9981
9982   /* We cannot yet cope with fields whose positions are variable, so
9983      for now, when we see such things, we simply return 0.  Someday, we may
9984      be able to handle such cases, but it will be damn difficult.  */
9985   if (! host_integerp (bit_position (decl), 0))
9986     return 0;
9987
9988   bitpos_int = int_bit_position (decl);
9989
9990 #ifdef PCC_BITFIELD_TYPE_MATTERS
9991   if (PCC_BITFIELD_TYPE_MATTERS)
9992     {
9993       tree type;
9994       tree field_size_tree;
9995       HOST_WIDE_INT deepest_bitpos;
9996       unsigned HOST_WIDE_INT field_size_in_bits;
9997       unsigned int type_align_in_bits;
9998       unsigned int decl_align_in_bits;
9999       unsigned HOST_WIDE_INT type_size_in_bits;
10000
10001       type = field_type (decl);
10002       field_size_tree = DECL_SIZE (decl);
10003
10004       /* The size could be unspecified if there was an error, or for
10005          a flexible array member.  */
10006       if (! field_size_tree)
10007         field_size_tree = bitsize_zero_node;
10008
10009       /* If we don't know the size of the field, pretend it's a full word.  */
10010       if (host_integerp (field_size_tree, 1))
10011         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10012       else
10013         field_size_in_bits = BITS_PER_WORD;
10014
10015       type_size_in_bits = simple_type_size_in_bits (type);
10016       type_align_in_bits = simple_type_align_in_bits (type);
10017       decl_align_in_bits = simple_decl_align_in_bits (decl);
10018
10019       /* The GCC front-end doesn't make any attempt to keep track of the
10020          starting bit offset (relative to the start of the containing
10021          structure type) of the hypothetical "containing object" for a
10022          bit-field.  Thus, when computing the byte offset value for the
10023          start of the "containing object" of a bit-field, we must deduce
10024          this information on our own. This can be rather tricky to do in
10025          some cases.  For example, handling the following structure type
10026          definition when compiling for an i386/i486 target (which only
10027          aligns long long's to 32-bit boundaries) can be very tricky:
10028
10029          struct S { int field1; long long field2:31; };
10030
10031          Fortunately, there is a simple rule-of-thumb which can be used
10032          in such cases.  When compiling for an i386/i486, GCC will
10033          allocate 8 bytes for the structure shown above.  It decides to
10034          do this based upon one simple rule for bit-field allocation.
10035          GCC allocates each "containing object" for each bit-field at
10036          the first (i.e. lowest addressed) legitimate alignment boundary
10037          (based upon the required minimum alignment for the declared
10038          type of the field) which it can possibly use, subject to the
10039          condition that there is still enough available space remaining
10040          in the containing object (when allocated at the selected point)
10041          to fully accommodate all of the bits of the bit-field itself.
10042
10043          This simple rule makes it obvious why GCC allocates 8 bytes for
10044          each object of the structure type shown above.  When looking
10045          for a place to allocate the "containing object" for `field2',
10046          the compiler simply tries to allocate a 64-bit "containing
10047          object" at each successive 32-bit boundary (starting at zero)
10048          until it finds a place to allocate that 64- bit field such that
10049          at least 31 contiguous (and previously unallocated) bits remain
10050          within that selected 64 bit field.  (As it turns out, for the
10051          example above, the compiler finds it is OK to allocate the
10052          "containing object" 64-bit field at bit-offset zero within the
10053          structure type.)
10054
10055          Here we attempt to work backwards from the limited set of facts
10056          we're given, and we try to deduce from those facts, where GCC
10057          must have believed that the containing object started (within
10058          the structure type). The value we deduce is then used (by the
10059          callers of this routine) to generate DW_AT_location and
10060          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10061          the case of DW_AT_location, regular fields as well).  */
10062
10063       /* Figure out the bit-distance from the start of the structure to
10064          the "deepest" bit of the bit-field.  */
10065       deepest_bitpos = bitpos_int + field_size_in_bits;
10066
10067       /* This is the tricky part.  Use some fancy footwork to deduce
10068          where the lowest addressed bit of the containing object must
10069          be.  */
10070       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10071
10072       /* Round up to type_align by default.  This works best for
10073          bitfields.  */
10074       object_offset_in_bits
10075         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10076
10077       if (object_offset_in_bits > bitpos_int)
10078         {
10079           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10080
10081           /* Round up to decl_align instead.  */
10082           object_offset_in_bits
10083             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10084         }
10085     }
10086   else
10087 #endif
10088     object_offset_in_bits = bitpos_int;
10089
10090   return object_offset_in_bits / BITS_PER_UNIT;
10091 }
10092 \f
10093 /* The following routines define various Dwarf attributes and any data
10094    associated with them.  */
10095
10096 /* Add a location description attribute value to a DIE.
10097
10098    This emits location attributes suitable for whole variables and
10099    whole parameters.  Note that the location attributes for struct fields are
10100    generated by the routine `data_member_location_attribute' below.  */
10101
10102 static inline void
10103 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10104                              dw_loc_descr_ref descr)
10105 {
10106   if (descr != 0)
10107     add_AT_loc (die, attr_kind, descr);
10108 }
10109
10110 /* Attach the specialized form of location attribute used for data members of
10111    struct and union types.  In the special case of a FIELD_DECL node which
10112    represents a bit-field, the "offset" part of this special location
10113    descriptor must indicate the distance in bytes from the lowest-addressed
10114    byte of the containing struct or union type to the lowest-addressed byte of
10115    the "containing object" for the bit-field.  (See the `field_byte_offset'
10116    function above).
10117
10118    For any given bit-field, the "containing object" is a hypothetical object
10119    (of some integral or enum type) within which the given bit-field lives.  The
10120    type of this hypothetical "containing object" is always the same as the
10121    declared type of the individual bit-field itself (for GCC anyway... the
10122    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10123    bytes) of the hypothetical "containing object" which will be given in the
10124    DW_AT_byte_size attribute for this bit-field.  (See the
10125    `byte_size_attribute' function below.)  It is also used when calculating the
10126    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10127    function below.)  */
10128
10129 static void
10130 add_data_member_location_attribute (dw_die_ref die, tree decl)
10131 {
10132   HOST_WIDE_INT offset;
10133   dw_loc_descr_ref loc_descr = 0;
10134
10135   if (TREE_CODE (decl) == TREE_BINFO)
10136     {
10137       /* We're working on the TAG_inheritance for a base class.  */
10138       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10139         {
10140           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10141              aren't at a fixed offset from all (sub)objects of the same
10142              type.  We need to extract the appropriate offset from our
10143              vtable.  The following dwarf expression means
10144
10145                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10146
10147              This is specific to the V3 ABI, of course.  */
10148
10149           dw_loc_descr_ref tmp;
10150
10151           /* Make a copy of the object address.  */
10152           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10153           add_loc_descr (&loc_descr, tmp);
10154
10155           /* Extract the vtable address.  */
10156           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10157           add_loc_descr (&loc_descr, tmp);
10158
10159           /* Calculate the address of the offset.  */
10160           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10161           gcc_assert (offset < 0);
10162
10163           tmp = int_loc_descriptor (-offset);
10164           add_loc_descr (&loc_descr, tmp);
10165           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10166           add_loc_descr (&loc_descr, tmp);
10167
10168           /* Extract the offset.  */
10169           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10170           add_loc_descr (&loc_descr, tmp);
10171
10172           /* Add it to the object address.  */
10173           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10174           add_loc_descr (&loc_descr, tmp);
10175         }
10176       else
10177         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10178     }
10179   else
10180     offset = field_byte_offset (decl);
10181
10182   if (! loc_descr)
10183     {
10184       enum dwarf_location_atom op;
10185
10186       /* The DWARF2 standard says that we should assume that the structure
10187          address is already on the stack, so we can specify a structure field
10188          address by using DW_OP_plus_uconst.  */
10189
10190 #ifdef MIPS_DEBUGGING_INFO
10191       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10192          operator correctly.  It works only if we leave the offset on the
10193          stack.  */
10194       op = DW_OP_constu;
10195 #else
10196       op = DW_OP_plus_uconst;
10197 #endif
10198
10199       loc_descr = new_loc_descr (op, offset, 0);
10200     }
10201
10202   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10203 }
10204
10205 /* Writes integer values to dw_vec_const array.  */
10206
10207 static void
10208 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10209 {
10210   while (size != 0)
10211     {
10212       *dest++ = val & 0xff;
10213       val >>= 8;
10214       --size;
10215     }
10216 }
10217
10218 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10219
10220 static HOST_WIDE_INT
10221 extract_int (const unsigned char *src, unsigned int size)
10222 {
10223   HOST_WIDE_INT val = 0;
10224
10225   src += size;
10226   while (size != 0)
10227     {
10228       val <<= 8;
10229       val |= *--src & 0xff;
10230       --size;
10231     }
10232   return val;
10233 }
10234
10235 /* Writes floating point values to dw_vec_const array.  */
10236
10237 static void
10238 insert_float (const_rtx rtl, unsigned char *array)
10239 {
10240   REAL_VALUE_TYPE rv;
10241   long val[4];
10242   int i;
10243
10244   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10245   real_to_target (val, &rv, GET_MODE (rtl));
10246
10247   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10248   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10249     {
10250       insert_int (val[i], 4, array);
10251       array += 4;
10252     }
10253 }
10254
10255 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10256    does not have a "location" either in memory or in a register.  These
10257    things can arise in GNU C when a constant is passed as an actual parameter
10258    to an inlined function.  They can also arise in C++ where declared
10259    constants do not necessarily get memory "homes".  */
10260
10261 static void
10262 add_const_value_attribute (dw_die_ref die, rtx rtl)
10263 {
10264   switch (GET_CODE (rtl))
10265     {
10266     case CONST_INT:
10267       {
10268         HOST_WIDE_INT val = INTVAL (rtl);
10269
10270         if (val < 0)
10271           add_AT_int (die, DW_AT_const_value, val);
10272         else
10273           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10274       }
10275       break;
10276
10277     case CONST_DOUBLE:
10278       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10279          floating-point constant.  A CONST_DOUBLE is used whenever the
10280          constant requires more than one word in order to be adequately
10281          represented.  We output CONST_DOUBLEs as blocks.  */
10282       {
10283         enum machine_mode mode = GET_MODE (rtl);
10284
10285         if (SCALAR_FLOAT_MODE_P (mode))
10286           {
10287             unsigned int length = GET_MODE_SIZE (mode);
10288             unsigned char *array = ggc_alloc (length);
10289
10290             insert_float (rtl, array);
10291             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10292           }
10293         else
10294           {
10295             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10296             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10297
10298             add_AT_long_long (die, DW_AT_const_value,
10299                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10300           }
10301       }
10302       break;
10303
10304     case CONST_VECTOR:
10305       {
10306         enum machine_mode mode = GET_MODE (rtl);
10307         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10308         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10309         unsigned char *array = ggc_alloc (length * elt_size);
10310         unsigned int i;
10311         unsigned char *p;
10312
10313         switch (GET_MODE_CLASS (mode))
10314           {
10315           case MODE_VECTOR_INT:
10316             for (i = 0, p = array; i < length; i++, p += elt_size)
10317               {
10318                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10319                 HOST_WIDE_INT lo, hi;
10320
10321                 switch (GET_CODE (elt))
10322                   {
10323                   case CONST_INT:
10324                     lo = INTVAL (elt);
10325                     hi = -(lo < 0);
10326                     break;
10327
10328                   case CONST_DOUBLE:
10329                     lo = CONST_DOUBLE_LOW (elt);
10330                     hi = CONST_DOUBLE_HIGH (elt);
10331                     break;
10332
10333                   default:
10334                     gcc_unreachable ();
10335                   }
10336
10337                 if (elt_size <= sizeof (HOST_WIDE_INT))
10338                   insert_int (lo, elt_size, p);
10339                 else
10340                   {
10341                     unsigned char *p0 = p;
10342                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10343
10344                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10345                     if (WORDS_BIG_ENDIAN)
10346                       {
10347                         p0 = p1;
10348                         p1 = p;
10349                       }
10350                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10351                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10352                   }
10353               }
10354             break;
10355
10356           case MODE_VECTOR_FLOAT:
10357             for (i = 0, p = array; i < length; i++, p += elt_size)
10358               {
10359                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10360                 insert_float (elt, p);
10361               }
10362             break;
10363
10364           default:
10365             gcc_unreachable ();
10366           }
10367
10368         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10369       }
10370       break;
10371
10372     case CONST_STRING:
10373       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10374       break;
10375
10376     case SYMBOL_REF:
10377     case LABEL_REF:
10378     case CONST:
10379       add_AT_addr (die, DW_AT_const_value, rtl);
10380       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10381       break;
10382
10383     case PLUS:
10384       /* In cases where an inlined instance of an inline function is passed
10385          the address of an `auto' variable (which is local to the caller) we
10386          can get a situation where the DECL_RTL of the artificial local
10387          variable (for the inlining) which acts as a stand-in for the
10388          corresponding formal parameter (of the inline function) will look
10389          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10390          exactly a compile-time constant expression, but it isn't the address
10391          of the (artificial) local variable either.  Rather, it represents the
10392          *value* which the artificial local variable always has during its
10393          lifetime.  We currently have no way to represent such quasi-constant
10394          values in Dwarf, so for now we just punt and generate nothing.  */
10395       break;
10396
10397     default:
10398       /* No other kinds of rtx should be possible here.  */
10399       gcc_unreachable ();
10400     }
10401
10402 }
10403
10404 /* Determine whether the evaluation of EXPR references any variables
10405    or functions which aren't otherwise used (and therefore may not be
10406    output).  */
10407 static tree
10408 reference_to_unused (tree * tp, int * walk_subtrees,
10409                      void * data ATTRIBUTE_UNUSED)
10410 {
10411   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10412     *walk_subtrees = 0;
10413
10414   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10415       && ! TREE_ASM_WRITTEN (*tp))
10416     return *tp;
10417   else if (!flag_unit_at_a_time)
10418     return NULL_TREE;
10419   /* ???  The C++ FE emits debug information for using decls, so
10420      putting gcc_unreachable here falls over.  See PR31899.  For now
10421      be conservative.  */
10422   else if (!cgraph_global_info_ready
10423            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10424     return *tp;
10425   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10426     {
10427       struct varpool_node *node = varpool_node (*tp);
10428       if (!node->needed)
10429         return *tp;
10430     }
10431   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10432            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10433     {
10434       struct cgraph_node *node = cgraph_node (*tp);
10435       if (!node->output)
10436         return *tp;
10437     }
10438   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10439     return *tp;
10440
10441   return NULL_TREE;
10442 }
10443
10444 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10445    for use in a later add_const_value_attribute call.  */
10446
10447 static rtx
10448 rtl_for_decl_init (tree init, tree type)
10449 {
10450   rtx rtl = NULL_RTX;
10451
10452   /* If a variable is initialized with a string constant without embedded
10453      zeros, build CONST_STRING.  */
10454   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10455     {
10456       tree enttype = TREE_TYPE (type);
10457       tree domain = TYPE_DOMAIN (type);
10458       enum machine_mode mode = TYPE_MODE (enttype);
10459
10460       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10461           && domain
10462           && integer_zerop (TYPE_MIN_VALUE (domain))
10463           && compare_tree_int (TYPE_MAX_VALUE (domain),
10464                                TREE_STRING_LENGTH (init) - 1) == 0
10465           && ((size_t) TREE_STRING_LENGTH (init)
10466               == strlen (TREE_STRING_POINTER (init)) + 1))
10467         rtl = gen_rtx_CONST_STRING (VOIDmode,
10468                                     ggc_strdup (TREE_STRING_POINTER (init)));
10469     }
10470   /* Other aggregates, and complex values, could be represented using
10471      CONCAT: FIXME!  */
10472   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10473     ;
10474   /* Vectors only work if their mode is supported by the target.
10475      FIXME: generic vectors ought to work too.  */
10476   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10477     ;
10478   /* If the initializer is something that we know will expand into an
10479      immediate RTL constant, expand it now.  We must be careful not to
10480      reference variables which won't be output.  */
10481   else if (initializer_constant_valid_p (init, type)
10482            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10483     {
10484       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10485          possible.  */
10486       if (TREE_CODE (type) == VECTOR_TYPE)
10487         switch (TREE_CODE (init))
10488           {
10489           case VECTOR_CST:
10490             break;
10491           case CONSTRUCTOR:
10492             if (TREE_CONSTANT (init))
10493               {
10494                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10495                 bool constant_p = true;
10496                 tree value;
10497                 unsigned HOST_WIDE_INT ix;
10498
10499                 /* Even when ctor is constant, it might contain non-*_CST
10500                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10501                    belong into VECTOR_CST nodes.  */
10502                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10503                   if (!CONSTANT_CLASS_P (value))
10504                     {
10505                       constant_p = false;
10506                       break;
10507                     }
10508
10509                 if (constant_p)
10510                   {
10511                     init = build_vector_from_ctor (type, elts);
10512                     break;
10513                   }
10514               }
10515             /* FALLTHRU */
10516
10517           default:
10518             return NULL;
10519           }
10520
10521       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10522
10523       /* If expand_expr returns a MEM, it wasn't immediate.  */
10524       gcc_assert (!rtl || !MEM_P (rtl));
10525     }
10526
10527   return rtl;
10528 }
10529
10530 /* Generate RTL for the variable DECL to represent its location.  */
10531
10532 static rtx
10533 rtl_for_decl_location (tree decl)
10534 {
10535   rtx rtl;
10536
10537   /* Here we have to decide where we are going to say the parameter "lives"
10538      (as far as the debugger is concerned).  We only have a couple of
10539      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10540
10541      DECL_RTL normally indicates where the parameter lives during most of the
10542      activation of the function.  If optimization is enabled however, this
10543      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10544      that the parameter doesn't really live anywhere (as far as the code
10545      generation parts of GCC are concerned) during most of the function's
10546      activation.  That will happen (for example) if the parameter is never
10547      referenced within the function.
10548
10549      We could just generate a location descriptor here for all non-NULL
10550      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10551      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10552      where DECL_RTL is NULL or is a pseudo-reg.
10553
10554      Note however that we can only get away with using DECL_INCOMING_RTL as
10555      a backup substitute for DECL_RTL in certain limited cases.  In cases
10556      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10557      we can be sure that the parameter was passed using the same type as it is
10558      declared to have within the function, and that its DECL_INCOMING_RTL
10559      points us to a place where a value of that type is passed.
10560
10561      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10562      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10563      because in these cases DECL_INCOMING_RTL points us to a value of some
10564      type which is *different* from the type of the parameter itself.  Thus,
10565      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10566      such cases, the debugger would end up (for example) trying to fetch a
10567      `float' from a place which actually contains the first part of a
10568      `double'.  That would lead to really incorrect and confusing
10569      output at debug-time.
10570
10571      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10572      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10573      are a couple of exceptions however.  On little-endian machines we can
10574      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10575      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10576      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10577      when (on a little-endian machine) a non-prototyped function has a
10578      parameter declared to be of type `short' or `char'.  In such cases,
10579      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10580      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10581      passed `int' value.  If the debugger then uses that address to fetch
10582      a `short' or a `char' (on a little-endian machine) the result will be
10583      the correct data, so we allow for such exceptional cases below.
10584
10585      Note that our goal here is to describe the place where the given formal
10586      parameter lives during most of the function's activation (i.e. between the
10587      end of the prologue and the start of the epilogue).  We'll do that as best
10588      as we can. Note however that if the given formal parameter is modified
10589      sometime during the execution of the function, then a stack backtrace (at
10590      debug-time) will show the function as having been called with the *new*
10591      value rather than the value which was originally passed in.  This happens
10592      rarely enough that it is not a major problem, but it *is* a problem, and
10593      I'd like to fix it.
10594
10595      A future version of dwarf2out.c may generate two additional attributes for
10596      any given DW_TAG_formal_parameter DIE which will describe the "passed
10597      type" and the "passed location" for the given formal parameter in addition
10598      to the attributes we now generate to indicate the "declared type" and the
10599      "active location" for each parameter.  This additional set of attributes
10600      could be used by debuggers for stack backtraces. Separately, note that
10601      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10602      This happens (for example) for inlined-instances of inline function formal
10603      parameters which are never referenced.  This really shouldn't be
10604      happening.  All PARM_DECL nodes should get valid non-NULL
10605      DECL_INCOMING_RTL values.  FIXME.  */
10606
10607   /* Use DECL_RTL as the "location" unless we find something better.  */
10608   rtl = DECL_RTL_IF_SET (decl);
10609
10610   /* When generating abstract instances, ignore everything except
10611      constants, symbols living in memory, and symbols living in
10612      fixed registers.  */
10613   if (! reload_completed)
10614     {
10615       if (rtl
10616           && (CONSTANT_P (rtl)
10617               || (MEM_P (rtl)
10618                   && CONSTANT_P (XEXP (rtl, 0)))
10619               || (REG_P (rtl)
10620                   && TREE_CODE (decl) == VAR_DECL
10621                   && TREE_STATIC (decl))))
10622         {
10623           rtl = targetm.delegitimize_address (rtl);
10624           return rtl;
10625         }
10626       rtl = NULL_RTX;
10627     }
10628   else if (TREE_CODE (decl) == PARM_DECL)
10629     {
10630       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10631         {
10632           tree declared_type = TREE_TYPE (decl);
10633           tree passed_type = DECL_ARG_TYPE (decl);
10634           enum machine_mode dmode = TYPE_MODE (declared_type);
10635           enum machine_mode pmode = TYPE_MODE (passed_type);
10636
10637           /* This decl represents a formal parameter which was optimized out.
10638              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10639              all cases where (rtl == NULL_RTX) just below.  */
10640           if (dmode == pmode)
10641             rtl = DECL_INCOMING_RTL (decl);
10642           else if (SCALAR_INT_MODE_P (dmode)
10643                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10644                    && DECL_INCOMING_RTL (decl))
10645             {
10646               rtx inc = DECL_INCOMING_RTL (decl);
10647               if (REG_P (inc))
10648                 rtl = inc;
10649               else if (MEM_P (inc))
10650                 {
10651                   if (BYTES_BIG_ENDIAN)
10652                     rtl = adjust_address_nv (inc, dmode,
10653                                              GET_MODE_SIZE (pmode)
10654                                              - GET_MODE_SIZE (dmode));
10655                   else
10656                     rtl = inc;
10657                 }
10658             }
10659         }
10660
10661       /* If the parm was passed in registers, but lives on the stack, then
10662          make a big endian correction if the mode of the type of the
10663          parameter is not the same as the mode of the rtl.  */
10664       /* ??? This is the same series of checks that are made in dbxout.c before
10665          we reach the big endian correction code there.  It isn't clear if all
10666          of these checks are necessary here, but keeping them all is the safe
10667          thing to do.  */
10668       else if (MEM_P (rtl)
10669                && XEXP (rtl, 0) != const0_rtx
10670                && ! CONSTANT_P (XEXP (rtl, 0))
10671                /* Not passed in memory.  */
10672                && !MEM_P (DECL_INCOMING_RTL (decl))
10673                /* Not passed by invisible reference.  */
10674                && (!REG_P (XEXP (rtl, 0))
10675                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10676                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10677 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10678                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10679 #endif
10680                      )
10681                /* Big endian correction check.  */
10682                && BYTES_BIG_ENDIAN
10683                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10684                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10685                    < UNITS_PER_WORD))
10686         {
10687           int offset = (UNITS_PER_WORD
10688                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10689
10690           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10691                              plus_constant (XEXP (rtl, 0), offset));
10692         }
10693     }
10694   else if (TREE_CODE (decl) == VAR_DECL
10695            && rtl
10696            && MEM_P (rtl)
10697            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10698            && BYTES_BIG_ENDIAN)
10699     {
10700       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10701       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10702
10703       /* If a variable is declared "register" yet is smaller than
10704          a register, then if we store the variable to memory, it
10705          looks like we're storing a register-sized value, when in
10706          fact we are not.  We need to adjust the offset of the
10707          storage location to reflect the actual value's bytes,
10708          else gdb will not be able to display it.  */
10709       if (rsize > dsize)
10710         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10711                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10712     }
10713
10714   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10715      and will have been substituted directly into all expressions that use it.
10716      C does not have such a concept, but C++ and other languages do.  */
10717   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10718     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10719
10720   if (rtl)
10721     rtl = targetm.delegitimize_address (rtl);
10722
10723   /* If we don't look past the constant pool, we risk emitting a
10724      reference to a constant pool entry that isn't referenced from
10725      code, and thus is not emitted.  */
10726   if (rtl)
10727     rtl = avoid_constant_pool_reference (rtl);
10728
10729   return rtl;
10730 }
10731
10732 /* We need to figure out what section we should use as the base for the
10733    address ranges where a given location is valid.
10734    1. If this particular DECL has a section associated with it, use that.
10735    2. If this function has a section associated with it, use that.
10736    3. Otherwise, use the text section.
10737    XXX: If you split a variable across multiple sections, we won't notice.  */
10738
10739 static const char *
10740 secname_for_decl (const_tree decl)
10741 {
10742   const char *secname;
10743
10744   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10745     {
10746       tree sectree = DECL_SECTION_NAME (decl);
10747       secname = TREE_STRING_POINTER (sectree);
10748     }
10749   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10750     {
10751       tree sectree = DECL_SECTION_NAME (current_function_decl);
10752       secname = TREE_STRING_POINTER (sectree);
10753     }
10754   else if (cfun && in_cold_section_p)
10755     secname = crtl->subsections.cold_section_label;
10756   else
10757     secname = text_section_label;
10758
10759   return secname;
10760 }
10761
10762 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_RTX is returned.
10763    If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
10764    value is the offset into the common block for the symbol.  */
10765
10766 static tree
10767 fortran_common (tree decl, HOST_WIDE_INT *value)
10768 {
10769   tree val_expr, cvar;
10770   enum machine_mode mode;
10771   HOST_WIDE_INT bitsize, bitpos;
10772   tree offset;
10773   int volatilep = 0, unsignedp = 0;
10774
10775   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
10776      it does not have a value (the offset into the common area), or if it
10777      is thread local (as opposed to global) then it isn't common, and shouldn't
10778      be handled as such.  */
10779   if (TREE_CODE (decl) != VAR_DECL
10780       || !TREE_PUBLIC (decl)
10781       || !TREE_STATIC (decl)
10782       || !DECL_HAS_VALUE_EXPR_P (decl)
10783       || !is_fortran ())
10784     return NULL_TREE;
10785
10786   val_expr = DECL_VALUE_EXPR (decl);
10787   if (TREE_CODE (val_expr) != COMPONENT_REF)
10788     return NULL_TREE;
10789
10790   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
10791                               &mode, &unsignedp, &volatilep, true);
10792
10793   if (cvar == NULL_TREE
10794       || TREE_CODE (cvar) != VAR_DECL
10795       || DECL_ARTIFICIAL (cvar)
10796       || !TREE_PUBLIC (cvar))
10797     return NULL_TREE;
10798
10799   *value = 0;
10800   if (offset != NULL)
10801     {
10802       if (!host_integerp (offset, 0))
10803         return NULL_TREE;
10804       *value = tree_low_cst (offset, 0);
10805     }
10806   if (bitpos != 0)
10807     *value += bitpos / BITS_PER_UNIT;
10808
10809   return cvar;
10810 }
10811
10812
10813 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10814    data attribute for a variable or a parameter.  We generate the
10815    DW_AT_const_value attribute only in those cases where the given variable
10816    or parameter does not have a true "location" either in memory or in a
10817    register.  This can happen (for example) when a constant is passed as an
10818    actual argument in a call to an inline function.  (It's possible that
10819    these things can crop up in other ways also.)  Note that one type of
10820    constant value which can be passed into an inlined function is a constant
10821    pointer.  This can happen for example if an actual argument in an inlined
10822    function call evaluates to a compile-time constant address.  */
10823
10824 static void
10825 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10826                                        enum dwarf_attribute attr)
10827 {
10828   rtx rtl;
10829   dw_loc_descr_ref descr;
10830   var_loc_list *loc_list;
10831   struct var_loc_node *node;
10832   if (TREE_CODE (decl) == ERROR_MARK)
10833     return;
10834
10835   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10836               || TREE_CODE (decl) == RESULT_DECL);
10837
10838   /* See if we possibly have multiple locations for this variable.  */
10839   loc_list = lookup_decl_loc (decl);
10840
10841   /* If it truly has multiple locations, the first and last node will
10842      differ.  */
10843   if (loc_list && loc_list->first != loc_list->last)
10844     {
10845       const char *endname, *secname;
10846       dw_loc_list_ref list;
10847       rtx varloc;
10848       enum var_init_status initialized;
10849
10850       /* Now that we know what section we are using for a base,
10851          actually construct the list of locations.
10852          The first location information is what is passed to the
10853          function that creates the location list, and the remaining
10854          locations just get added on to that list.
10855          Note that we only know the start address for a location
10856          (IE location changes), so to build the range, we use
10857          the range [current location start, next location start].
10858          This means we have to special case the last node, and generate
10859          a range of [last location start, end of function label].  */
10860
10861       node = loc_list->first;
10862       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10863       secname = secname_for_decl (decl);
10864
10865       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10866         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10867       else
10868         initialized = VAR_INIT_STATUS_INITIALIZED;
10869
10870       list = new_loc_list (loc_descriptor (varloc, initialized),
10871                            node->label, node->next->label, secname, 1);
10872       node = node->next;
10873
10874       for (; node->next; node = node->next)
10875         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10876           {
10877             /* The variable has a location between NODE->LABEL and
10878                NODE->NEXT->LABEL.  */
10879             enum var_init_status initialized =
10880               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10881             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10882             add_loc_descr_to_loc_list (&list, 
10883                                        loc_descriptor (varloc, initialized),
10884                                        node->label, node->next->label, secname);
10885           }
10886
10887       /* If the variable has a location at the last label
10888          it keeps its location until the end of function.  */
10889       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10890         {
10891           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10892           enum var_init_status initialized =
10893             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10894
10895           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10896           if (!current_function_decl)
10897             endname = text_end_label;
10898           else
10899             {
10900               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10901                                            current_function_funcdef_no);
10902               endname = ggc_strdup (label_id);
10903             }
10904           add_loc_descr_to_loc_list (&list, 
10905                                      loc_descriptor (varloc, initialized),
10906                                      node->label, endname, secname);
10907         }
10908
10909       /* Finally, add the location list to the DIE, and we are done.  */
10910       add_AT_loc_list (die, attr, list);
10911       return;
10912     }
10913
10914   /* Try to get some constant RTL for this decl, and use that as the value of
10915      the location.  */
10916
10917   rtl = rtl_for_decl_location (decl);
10918   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10919     {
10920       add_const_value_attribute (die, rtl);
10921       return;
10922     }
10923
10924   /* If we have tried to generate the location otherwise, and it
10925      didn't work out (we wouldn't be here if we did), and we have a one entry
10926      location list, try generating a location from that.  */
10927   if (loc_list && loc_list->first)
10928     {
10929       enum var_init_status status;
10930       node = loc_list->first;
10931       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10932       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10933       if (descr)
10934         {
10935           add_AT_location_description (die, attr, descr);
10936           return;
10937         }
10938     }
10939
10940   /* We couldn't get any rtl, so try directly generating the location
10941      description from the tree.  */
10942   descr = loc_descriptor_from_tree (decl);
10943   if (descr)
10944     {
10945       add_AT_location_description (die, attr, descr);
10946       return;
10947     }
10948   /* None of that worked, so it must not really have a location;
10949      try adding a constant value attribute from the DECL_INITIAL.  */
10950   tree_add_const_value_attribute (die, decl);
10951 }
10952
10953 /* If we don't have a copy of this variable in memory for some reason (such
10954    as a C++ member constant that doesn't have an out-of-line definition),
10955    we should tell the debugger about the constant value.  */
10956
10957 static void
10958 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10959 {
10960   tree init = DECL_INITIAL (decl);
10961   tree type = TREE_TYPE (decl);
10962   rtx rtl;
10963
10964   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10965     /* OK */;
10966   else
10967     return;
10968
10969   rtl = rtl_for_decl_init (init, type);
10970   if (rtl)
10971     add_const_value_attribute (var_die, rtl);
10972 }
10973
10974 /* Convert the CFI instructions for the current function into a
10975    location list.  This is used for DW_AT_frame_base when we targeting
10976    a dwarf2 consumer that does not support the dwarf3
10977    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10978    expressions.  */
10979
10980 static dw_loc_list_ref
10981 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10982 {
10983   dw_fde_ref fde;
10984   dw_loc_list_ref list, *list_tail;
10985   dw_cfi_ref cfi;
10986   dw_cfa_location last_cfa, next_cfa;
10987   const char *start_label, *last_label, *section;
10988
10989   fde = &fde_table[fde_table_in_use - 1];
10990
10991   section = secname_for_decl (current_function_decl);
10992   list_tail = &list;
10993   list = NULL;
10994
10995   next_cfa.reg = INVALID_REGNUM;
10996   next_cfa.offset = 0;
10997   next_cfa.indirect = 0;
10998   next_cfa.base_offset = 0;
10999
11000   start_label = fde->dw_fde_begin;
11001
11002   /* ??? Bald assumption that the CIE opcode list does not contain
11003      advance opcodes.  */
11004   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11005     lookup_cfa_1 (cfi, &next_cfa);
11006
11007   last_cfa = next_cfa;
11008   last_label = start_label;
11009
11010   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11011     switch (cfi->dw_cfi_opc)
11012       {
11013       case DW_CFA_set_loc:
11014       case DW_CFA_advance_loc1:
11015       case DW_CFA_advance_loc2:
11016       case DW_CFA_advance_loc4:
11017         if (!cfa_equal_p (&last_cfa, &next_cfa))
11018           {
11019             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11020                                        start_label, last_label, section,
11021                                        list == NULL);
11022
11023             list_tail = &(*list_tail)->dw_loc_next;
11024             last_cfa = next_cfa;
11025             start_label = last_label;
11026           }
11027         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11028         break;
11029
11030       case DW_CFA_advance_loc:
11031         /* The encoding is complex enough that we should never emit this.  */
11032       case DW_CFA_remember_state:
11033       case DW_CFA_restore_state:
11034         /* We don't handle these two in this function.  It would be possible
11035            if it were to be required.  */
11036         gcc_unreachable ();
11037
11038       default:
11039         lookup_cfa_1 (cfi, &next_cfa);
11040         break;
11041       }
11042
11043   if (!cfa_equal_p (&last_cfa, &next_cfa))
11044     {
11045       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11046                                  start_label, last_label, section,
11047                                  list == NULL);
11048       list_tail = &(*list_tail)->dw_loc_next;
11049       start_label = last_label;
11050     }
11051   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11052                              start_label, fde->dw_fde_end, section,
11053                              list == NULL);
11054
11055   return list;
11056 }
11057
11058 /* Compute a displacement from the "steady-state frame pointer" to the
11059    frame base (often the same as the CFA), and store it in
11060    frame_pointer_fb_offset.  OFFSET is added to the displacement
11061    before the latter is negated.  */
11062
11063 static void
11064 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11065 {
11066   rtx reg, elim;
11067
11068 #ifdef FRAME_POINTER_CFA_OFFSET
11069   reg = frame_pointer_rtx;
11070   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11071 #else
11072   reg = arg_pointer_rtx;
11073   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11074 #endif
11075
11076   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11077   if (GET_CODE (elim) == PLUS)
11078     {
11079       offset += INTVAL (XEXP (elim, 1));
11080       elim = XEXP (elim, 0);
11081     }
11082   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
11083                        : stack_pointer_rtx));
11084
11085   frame_pointer_fb_offset = -offset;
11086 }
11087
11088 /* Generate a DW_AT_name attribute given some string value to be included as
11089    the value of the attribute.  */
11090
11091 static void
11092 add_name_attribute (dw_die_ref die, const char *name_string)
11093 {
11094   if (name_string != NULL && *name_string != 0)
11095     {
11096       if (demangle_name_func)
11097         name_string = (*demangle_name_func) (name_string);
11098
11099       add_AT_string (die, DW_AT_name, name_string);
11100     }
11101 }
11102
11103 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11104
11105 static void
11106 add_comp_dir_attribute (dw_die_ref die)
11107 {
11108   const char *wd = get_src_pwd ();
11109   if (wd != NULL)
11110     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11111 }
11112
11113 /* Given a tree node describing an array bound (either lower or upper) output
11114    a representation for that bound.  */
11115
11116 static void
11117 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11118 {
11119   switch (TREE_CODE (bound))
11120     {
11121     case ERROR_MARK:
11122       return;
11123
11124     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11125     case INTEGER_CST:
11126       if (! host_integerp (bound, 0)
11127           || (bound_attr == DW_AT_lower_bound
11128               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11129                   || (is_fortran () && integer_onep (bound)))))
11130         /* Use the default.  */
11131         ;
11132       else
11133         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11134       break;
11135
11136     case CONVERT_EXPR:
11137     case NOP_EXPR:
11138     case VIEW_CONVERT_EXPR:
11139       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11140       break;
11141
11142     case SAVE_EXPR:
11143       break;
11144
11145     case VAR_DECL:
11146     case PARM_DECL:
11147     case RESULT_DECL:
11148       {
11149         dw_die_ref decl_die = lookup_decl_die (bound);
11150
11151         /* ??? Can this happen, or should the variable have been bound
11152            first?  Probably it can, since I imagine that we try to create
11153            the types of parameters in the order in which they exist in
11154            the list, and won't have created a forward reference to a
11155            later parameter.  */
11156         if (decl_die != NULL)
11157           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11158         break;
11159       }
11160
11161     default:
11162       {
11163         /* Otherwise try to create a stack operation procedure to
11164            evaluate the value of the array bound.  */
11165
11166         dw_die_ref ctx, decl_die;
11167         dw_loc_descr_ref loc;
11168
11169         loc = loc_descriptor_from_tree (bound);
11170         if (loc == NULL)
11171           break;
11172
11173         if (current_function_decl == 0)
11174           ctx = comp_unit_die;
11175         else
11176           ctx = lookup_decl_die (current_function_decl);
11177
11178         decl_die = new_die (DW_TAG_variable, ctx, bound);
11179         add_AT_flag (decl_die, DW_AT_artificial, 1);
11180         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11181         add_AT_loc (decl_die, DW_AT_location, loc);
11182
11183         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11184         break;
11185       }
11186     }
11187 }
11188
11189 /* Note that the block of subscript information for an array type also
11190    includes information about the element type of type given array type.  */
11191
11192 static void
11193 add_subscript_info (dw_die_ref type_die, tree type)
11194 {
11195 #ifndef MIPS_DEBUGGING_INFO
11196   unsigned dimension_number;
11197 #endif
11198   tree lower, upper;
11199   dw_die_ref subrange_die;
11200
11201   /* The GNU compilers represent multidimensional array types as sequences of
11202      one dimensional array types whose element types are themselves array
11203      types.  Here we squish that down, so that each multidimensional array
11204      type gets only one array_type DIE in the Dwarf debugging info. The draft
11205      Dwarf specification say that we are allowed to do this kind of
11206      compression in C (because there is no difference between an array or
11207      arrays and a multidimensional array in C) but for other source languages
11208      (e.g. Ada) we probably shouldn't do this.  */
11209
11210   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11211      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11212      We work around this by disabling this feature.  See also
11213      gen_array_type_die.  */
11214 #ifndef MIPS_DEBUGGING_INFO
11215   for (dimension_number = 0;
11216        TREE_CODE (type) == ARRAY_TYPE;
11217        type = TREE_TYPE (type), dimension_number++)
11218 #endif
11219     {
11220       tree domain = TYPE_DOMAIN (type);
11221
11222       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11223          and (in GNU C only) variable bounds.  Handle all three forms
11224          here.  */
11225       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11226       if (domain)
11227         {
11228           /* We have an array type with specified bounds.  */
11229           lower = TYPE_MIN_VALUE (domain);
11230           upper = TYPE_MAX_VALUE (domain);
11231
11232           /* Define the index type.  */
11233           if (TREE_TYPE (domain))
11234             {
11235               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11236                  TREE_TYPE field.  We can't emit debug info for this
11237                  because it is an unnamed integral type.  */
11238               if (TREE_CODE (domain) == INTEGER_TYPE
11239                   && TYPE_NAME (domain) == NULL_TREE
11240                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11241                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11242                 ;
11243               else
11244                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11245                                     type_die);
11246             }
11247
11248           /* ??? If upper is NULL, the array has unspecified length,
11249              but it does have a lower bound.  This happens with Fortran
11250                dimension arr(N:*)
11251              Since the debugger is definitely going to need to know N
11252              to produce useful results, go ahead and output the lower
11253              bound solo, and hope the debugger can cope.  */
11254
11255           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11256           if (upper)
11257             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11258         }
11259
11260       /* Otherwise we have an array type with an unspecified length.  The
11261          DWARF-2 spec does not say how to handle this; let's just leave out the
11262          bounds.  */
11263     }
11264 }
11265
11266 static void
11267 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11268 {
11269   unsigned size;
11270
11271   switch (TREE_CODE (tree_node))
11272     {
11273     case ERROR_MARK:
11274       size = 0;
11275       break;
11276     case ENUMERAL_TYPE:
11277     case RECORD_TYPE:
11278     case UNION_TYPE:
11279     case QUAL_UNION_TYPE:
11280       size = int_size_in_bytes (tree_node);
11281       break;
11282     case FIELD_DECL:
11283       /* For a data member of a struct or union, the DW_AT_byte_size is
11284          generally given as the number of bytes normally allocated for an
11285          object of the *declared* type of the member itself.  This is true
11286          even for bit-fields.  */
11287       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11288       break;
11289     default:
11290       gcc_unreachable ();
11291     }
11292
11293   /* Note that `size' might be -1 when we get to this point.  If it is, that
11294      indicates that the byte size of the entity in question is variable.  We
11295      have no good way of expressing this fact in Dwarf at the present time,
11296      so just let the -1 pass on through.  */
11297   add_AT_unsigned (die, DW_AT_byte_size, size);
11298 }
11299
11300 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11301    which specifies the distance in bits from the highest order bit of the
11302    "containing object" for the bit-field to the highest order bit of the
11303    bit-field itself.
11304
11305    For any given bit-field, the "containing object" is a hypothetical object
11306    (of some integral or enum type) within which the given bit-field lives.  The
11307    type of this hypothetical "containing object" is always the same as the
11308    declared type of the individual bit-field itself.  The determination of the
11309    exact location of the "containing object" for a bit-field is rather
11310    complicated.  It's handled by the `field_byte_offset' function (above).
11311
11312    Note that it is the size (in bytes) of the hypothetical "containing object"
11313    which will be given in the DW_AT_byte_size attribute for this bit-field.
11314    (See `byte_size_attribute' above).  */
11315
11316 static inline void
11317 add_bit_offset_attribute (dw_die_ref die, tree decl)
11318 {
11319   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11320   tree type = DECL_BIT_FIELD_TYPE (decl);
11321   HOST_WIDE_INT bitpos_int;
11322   HOST_WIDE_INT highest_order_object_bit_offset;
11323   HOST_WIDE_INT highest_order_field_bit_offset;
11324   HOST_WIDE_INT unsigned bit_offset;
11325
11326   /* Must be a field and a bit field.  */
11327   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11328
11329   /* We can't yet handle bit-fields whose offsets are variable, so if we
11330      encounter such things, just return without generating any attribute
11331      whatsoever.  Likewise for variable or too large size.  */
11332   if (! host_integerp (bit_position (decl), 0)
11333       || ! host_integerp (DECL_SIZE (decl), 1))
11334     return;
11335
11336   bitpos_int = int_bit_position (decl);
11337
11338   /* Note that the bit offset is always the distance (in bits) from the
11339      highest-order bit of the "containing object" to the highest-order bit of
11340      the bit-field itself.  Since the "high-order end" of any object or field
11341      is different on big-endian and little-endian machines, the computation
11342      below must take account of these differences.  */
11343   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11344   highest_order_field_bit_offset = bitpos_int;
11345
11346   if (! BYTES_BIG_ENDIAN)
11347     {
11348       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11349       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11350     }
11351
11352   bit_offset
11353     = (! BYTES_BIG_ENDIAN
11354        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11355        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11356
11357   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11358 }
11359
11360 /* For a FIELD_DECL node which represents a bit field, output an attribute
11361    which specifies the length in bits of the given field.  */
11362
11363 static inline void
11364 add_bit_size_attribute (dw_die_ref die, tree decl)
11365 {
11366   /* Must be a field and a bit field.  */
11367   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11368               && DECL_BIT_FIELD_TYPE (decl));
11369
11370   if (host_integerp (DECL_SIZE (decl), 1))
11371     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11372 }
11373
11374 /* If the compiled language is ANSI C, then add a 'prototyped'
11375    attribute, if arg types are given for the parameters of a function.  */
11376
11377 static inline void
11378 add_prototyped_attribute (dw_die_ref die, tree func_type)
11379 {
11380   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11381       && TYPE_ARG_TYPES (func_type) != NULL)
11382     add_AT_flag (die, DW_AT_prototyped, 1);
11383 }
11384
11385 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11386    by looking in either the type declaration or object declaration
11387    equate table.  */
11388
11389 static inline void
11390 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11391 {
11392   dw_die_ref origin_die = NULL;
11393
11394   if (TREE_CODE (origin) != FUNCTION_DECL)
11395     {
11396       /* We may have gotten separated from the block for the inlined
11397          function, if we're in an exception handler or some such; make
11398          sure that the abstract function has been written out.
11399
11400          Doing this for nested functions is wrong, however; functions are
11401          distinct units, and our context might not even be inline.  */
11402       tree fn = origin;
11403
11404       if (TYPE_P (fn))
11405         fn = TYPE_STUB_DECL (fn);
11406
11407       fn = decl_function_context (fn);
11408       if (fn)
11409         dwarf2out_abstract_function (fn);
11410     }
11411
11412   if (DECL_P (origin))
11413     origin_die = lookup_decl_die (origin);
11414   else if (TYPE_P (origin))
11415     origin_die = lookup_type_die (origin);
11416
11417   /* XXX: Functions that are never lowered don't always have correct block
11418      trees (in the case of java, they simply have no block tree, in some other
11419      languages).  For these functions, there is nothing we can really do to
11420      output correct debug info for inlined functions in all cases.  Rather
11421      than die, we'll just produce deficient debug info now, in that we will
11422      have variables without a proper abstract origin.  In the future, when all
11423      functions are lowered, we should re-add a gcc_assert (origin_die)
11424      here.  */
11425
11426   if (origin_die)
11427       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11428 }
11429
11430 /* We do not currently support the pure_virtual attribute.  */
11431
11432 static inline void
11433 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11434 {
11435   if (DECL_VINDEX (func_decl))
11436     {
11437       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11438
11439       if (host_integerp (DECL_VINDEX (func_decl), 0))
11440         add_AT_loc (die, DW_AT_vtable_elem_location,
11441                     new_loc_descr (DW_OP_constu,
11442                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11443                                    0));
11444
11445       /* GNU extension: Record what type this method came from originally.  */
11446       if (debug_info_level > DINFO_LEVEL_TERSE)
11447         add_AT_die_ref (die, DW_AT_containing_type,
11448                         lookup_type_die (DECL_CONTEXT (func_decl)));
11449     }
11450 }
11451 \f
11452 /* Add source coordinate attributes for the given decl.  */
11453
11454 static void
11455 add_src_coords_attributes (dw_die_ref die, tree decl)
11456 {
11457   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11458
11459   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11460   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11461 }
11462
11463 /* Add a DW_AT_name attribute and source coordinate attribute for the
11464    given decl, but only if it actually has a name.  */
11465
11466 static void
11467 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11468 {
11469   tree decl_name;
11470
11471   decl_name = DECL_NAME (decl);
11472   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11473     {
11474       add_name_attribute (die, dwarf2_name (decl, 0));
11475       if (! DECL_ARTIFICIAL (decl))
11476         add_src_coords_attributes (die, decl);
11477
11478       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11479           && TREE_PUBLIC (decl)
11480           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11481           && !DECL_ABSTRACT (decl)
11482           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11483           && !is_fortran ())
11484         add_AT_string (die, DW_AT_MIPS_linkage_name,
11485                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11486     }
11487
11488 #ifdef VMS_DEBUGGING_INFO
11489   /* Get the function's name, as described by its RTL.  This may be different
11490      from the DECL_NAME name used in the source file.  */
11491   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11492     {
11493       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11494                    XEXP (DECL_RTL (decl), 0));
11495       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11496     }
11497 #endif
11498 }
11499
11500 /* Push a new declaration scope.  */
11501
11502 static void
11503 push_decl_scope (tree scope)
11504 {
11505   VEC_safe_push (tree, gc, decl_scope_table, scope);
11506 }
11507
11508 /* Pop a declaration scope.  */
11509
11510 static inline void
11511 pop_decl_scope (void)
11512 {
11513   VEC_pop (tree, decl_scope_table);
11514 }
11515
11516 /* Return the DIE for the scope that immediately contains this type.
11517    Non-named types get global scope.  Named types nested in other
11518    types get their containing scope if it's open, or global scope
11519    otherwise.  All other types (i.e. function-local named types) get
11520    the current active scope.  */
11521
11522 static dw_die_ref
11523 scope_die_for (tree t, dw_die_ref context_die)
11524 {
11525   dw_die_ref scope_die = NULL;
11526   tree containing_scope;
11527   int i;
11528
11529   /* Non-types always go in the current scope.  */
11530   gcc_assert (TYPE_P (t));
11531
11532   containing_scope = TYPE_CONTEXT (t);
11533
11534   /* Use the containing namespace if it was passed in (for a declaration).  */
11535   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11536     {
11537       if (context_die == lookup_decl_die (containing_scope))
11538         /* OK */;
11539       else
11540         containing_scope = NULL_TREE;
11541     }
11542
11543   /* Ignore function type "scopes" from the C frontend.  They mean that
11544      a tagged type is local to a parmlist of a function declarator, but
11545      that isn't useful to DWARF.  */
11546   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11547     containing_scope = NULL_TREE;
11548
11549   if (containing_scope == NULL_TREE)
11550     scope_die = comp_unit_die;
11551   else if (TYPE_P (containing_scope))
11552     {
11553       /* For types, we can just look up the appropriate DIE.  But
11554          first we check to see if we're in the middle of emitting it
11555          so we know where the new DIE should go.  */
11556       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11557         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11558           break;
11559
11560       if (i < 0)
11561         {
11562           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11563                       || TREE_ASM_WRITTEN (containing_scope));
11564
11565           /* If none of the current dies are suitable, we get file scope.  */
11566           scope_die = comp_unit_die;
11567         }
11568       else
11569         scope_die = lookup_type_die (containing_scope);
11570     }
11571   else
11572     scope_die = context_die;
11573
11574   return scope_die;
11575 }
11576
11577 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11578
11579 static inline int
11580 local_scope_p (dw_die_ref context_die)
11581 {
11582   for (; context_die; context_die = context_die->die_parent)
11583     if (context_die->die_tag == DW_TAG_inlined_subroutine
11584         || context_die->die_tag == DW_TAG_subprogram)
11585       return 1;
11586
11587   return 0;
11588 }
11589
11590 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11591    whether or not to treat a DIE in this context as a declaration.  */
11592
11593 static inline int
11594 class_or_namespace_scope_p (dw_die_ref context_die)
11595 {
11596   return (context_die
11597           && (context_die->die_tag == DW_TAG_structure_type
11598               || context_die->die_tag == DW_TAG_class_type
11599               || context_die->die_tag == DW_TAG_interface_type
11600               || context_die->die_tag == DW_TAG_union_type
11601               || context_die->die_tag == DW_TAG_namespace));
11602 }
11603
11604 /* Many forms of DIEs require a "type description" attribute.  This
11605    routine locates the proper "type descriptor" die for the type given
11606    by 'type', and adds a DW_AT_type attribute below the given die.  */
11607
11608 static void
11609 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11610                     int decl_volatile, dw_die_ref context_die)
11611 {
11612   enum tree_code code  = TREE_CODE (type);
11613   dw_die_ref type_die  = NULL;
11614
11615   /* ??? If this type is an unnamed subrange type of an integral, floating-point
11616      or fixed-point type, use the inner type.  This is because we have no
11617      support for unnamed types in base_type_die.  This can happen if this is
11618      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11619   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11620       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11621     type = TREE_TYPE (type), code = TREE_CODE (type);
11622
11623   if (code == ERROR_MARK
11624       /* Handle a special case.  For functions whose return type is void, we
11625          generate *no* type attribute.  (Note that no object may have type
11626          `void', so this only applies to function return types).  */
11627       || code == VOID_TYPE)
11628     return;
11629
11630   type_die = modified_type_die (type,
11631                                 decl_const || TYPE_READONLY (type),
11632                                 decl_volatile || TYPE_VOLATILE (type),
11633                                 context_die);
11634
11635   if (type_die != NULL)
11636     add_AT_die_ref (object_die, DW_AT_type, type_die);
11637 }
11638
11639 /* Given an object die, add the calling convention attribute for the
11640    function call type.  */
11641 static void
11642 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11643 {
11644   enum dwarf_calling_convention value = DW_CC_normal;
11645
11646   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11647
11648   /* DWARF doesn't provide a way to identify a program's source-level
11649      entry point.  DW_AT_calling_convention attributes are only meant
11650      to describe functions' calling conventions.  However, lacking a
11651      better way to signal the Fortran main program, we use this for the
11652      time being, following existing custom.  */
11653   if (is_fortran ()
11654       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11655     value = DW_CC_program;
11656
11657   /* Only add the attribute if the backend requests it, and
11658      is not DW_CC_normal.  */
11659   if (value && (value != DW_CC_normal))
11660     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11661 }
11662
11663 /* Given a tree pointer to a struct, class, union, or enum type node, return
11664    a pointer to the (string) tag name for the given type, or zero if the type
11665    was declared without a tag.  */
11666
11667 static const char *
11668 type_tag (const_tree type)
11669 {
11670   const char *name = 0;
11671
11672   if (TYPE_NAME (type) != 0)
11673     {
11674       tree t = 0;
11675
11676       /* Find the IDENTIFIER_NODE for the type name.  */
11677       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11678         t = TYPE_NAME (type);
11679
11680       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11681          a TYPE_DECL node, regardless of whether or not a `typedef' was
11682          involved.  */
11683       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11684                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11685         {
11686           /* We want to be extra verbose.  Don't call dwarf_name if
11687              DECL_NAME isn't set.  The default hook for decl_printable_name
11688              doesn't like that, and in this context it's correct to return
11689              0, instead of "<anonymous>" or the like.  */
11690           if (DECL_NAME (TYPE_NAME (type)))
11691             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11692         }
11693
11694       /* Now get the name as a string, or invent one.  */
11695       if (!name && t != 0)
11696         name = IDENTIFIER_POINTER (t);
11697     }
11698
11699   return (name == 0 || *name == '\0') ? 0 : name;
11700 }
11701
11702 /* Return the type associated with a data member, make a special check
11703    for bit field types.  */
11704
11705 static inline tree
11706 member_declared_type (const_tree member)
11707 {
11708   return (DECL_BIT_FIELD_TYPE (member)
11709           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11710 }
11711
11712 /* Get the decl's label, as described by its RTL. This may be different
11713    from the DECL_NAME name used in the source file.  */
11714
11715 #if 0
11716 static const char *
11717 decl_start_label (tree decl)
11718 {
11719   rtx x;
11720   const char *fnname;
11721
11722   x = DECL_RTL (decl);
11723   gcc_assert (MEM_P (x));
11724
11725   x = XEXP (x, 0);
11726   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11727
11728   fnname = XSTR (x, 0);
11729   return fnname;
11730 }
11731 #endif
11732 \f
11733 /* These routines generate the internal representation of the DIE's for
11734    the compilation unit.  Debugging information is collected by walking
11735    the declaration trees passed in from dwarf2out_decl().  */
11736
11737 static void
11738 gen_array_type_die (tree type, dw_die_ref context_die)
11739 {
11740   dw_die_ref scope_die = scope_die_for (type, context_die);
11741   dw_die_ref array_die;
11742   tree element_type;
11743
11744   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11745      the inner array type comes before the outer array type.  Thus we must
11746      call gen_type_die before we call new_die.  See below also.  */
11747 #ifdef MIPS_DEBUGGING_INFO
11748   gen_type_die (TREE_TYPE (type), context_die);
11749 #endif
11750
11751   array_die = new_die (DW_TAG_array_type, scope_die, type);
11752   add_name_attribute (array_die, type_tag (type));
11753   equate_type_number_to_die (type, array_die);
11754
11755   if (TREE_CODE (type) == VECTOR_TYPE)
11756     {
11757       /* The frontend feeds us a representation for the vector as a struct
11758          containing an array.  Pull out the array type.  */
11759       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11760       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11761     }
11762
11763   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11764   if (is_fortran ()
11765       && TREE_CODE (type) == ARRAY_TYPE
11766       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11767     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11768
11769 #if 0
11770   /* We default the array ordering.  SDB will probably do
11771      the right things even if DW_AT_ordering is not present.  It's not even
11772      an issue until we start to get into multidimensional arrays anyway.  If
11773      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11774      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11775      and when we find out that we need to put these in, we will only do so
11776      for multidimensional arrays.  */
11777   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11778 #endif
11779
11780 #ifdef MIPS_DEBUGGING_INFO
11781   /* The SGI compilers handle arrays of unknown bound by setting
11782      AT_declaration and not emitting any subrange DIEs.  */
11783   if (! TYPE_DOMAIN (type))
11784     add_AT_flag (array_die, DW_AT_declaration, 1);
11785   else
11786 #endif
11787     add_subscript_info (array_die, type);
11788
11789   /* Add representation of the type of the elements of this array type.  */
11790   element_type = TREE_TYPE (type);
11791
11792   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11793      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11794      We work around this by disabling this feature.  See also
11795      add_subscript_info.  */
11796 #ifndef MIPS_DEBUGGING_INFO
11797   while (TREE_CODE (element_type) == ARRAY_TYPE)
11798     element_type = TREE_TYPE (element_type);
11799
11800   gen_type_die (element_type, context_die);
11801 #endif
11802
11803   add_type_attribute (array_die, element_type, 0, 0, context_die);
11804
11805   if (get_AT (array_die, DW_AT_name))
11806     add_pubtype (type, array_die);
11807 }
11808
11809 static dw_loc_descr_ref
11810 descr_info_loc (tree val, tree base_decl)
11811 {
11812   HOST_WIDE_INT size;
11813   dw_loc_descr_ref loc, loc2;
11814   enum dwarf_location_atom op;
11815
11816   if (val == base_decl)
11817     return new_loc_descr (DW_OP_push_object_address, 0, 0);
11818
11819   switch (TREE_CODE (val))
11820     {
11821     case NOP_EXPR:
11822     case CONVERT_EXPR:
11823       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11824     case INTEGER_CST:
11825       if (host_integerp (val, 0))
11826         return int_loc_descriptor (tree_low_cst (val, 0));
11827       break;
11828     case INDIRECT_REF:
11829       size = int_size_in_bytes (TREE_TYPE (val));
11830       if (size < 0)
11831         break;
11832       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11833       if (!loc)
11834         break;
11835       if (size == DWARF2_ADDR_SIZE)
11836         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11837       else
11838         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11839       return loc;
11840     case POINTER_PLUS_EXPR:
11841     case PLUS_EXPR:
11842       if (host_integerp (TREE_OPERAND (val, 1), 1)
11843           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11844              < 16384)
11845         {
11846           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11847           if (!loc)
11848             break;
11849           add_loc_descr (&loc,
11850                          new_loc_descr (DW_OP_plus_uconst,
11851                                         tree_low_cst (TREE_OPERAND (val, 1),
11852                                                       1), 0));
11853         }
11854       else
11855         {
11856           op = DW_OP_plus;
11857         do_binop:
11858           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11859           if (!loc)
11860             break;
11861           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11862           if (!loc2)
11863             break;
11864           add_loc_descr (&loc, loc2);
11865           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11866         }
11867       return loc;
11868     case MINUS_EXPR:
11869       op = DW_OP_minus;
11870       goto do_binop;
11871     case MULT_EXPR:
11872       op = DW_OP_mul;
11873       goto do_binop;
11874     case EQ_EXPR:
11875       op = DW_OP_eq;
11876       goto do_binop;
11877     case NE_EXPR:
11878       op = DW_OP_ne;
11879       goto do_binop;
11880     default:
11881       break;
11882     }
11883   return NULL;
11884 }
11885
11886 static void
11887 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11888                       tree val, tree base_decl)
11889 {
11890   dw_loc_descr_ref loc;
11891
11892   if (host_integerp (val, 0))
11893     {
11894       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11895       return;
11896     }
11897
11898   loc = descr_info_loc (val, base_decl);
11899   if (!loc)
11900     return;
11901
11902   add_AT_loc (die, attr, loc);
11903 }
11904
11905 /* This routine generates DIE for array with hidden descriptor, details
11906    are filled into *info by a langhook.  */
11907
11908 static void
11909 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11910                           dw_die_ref context_die)
11911 {
11912   dw_die_ref scope_die = scope_die_for (type, context_die);
11913   dw_die_ref array_die;
11914   int dim;
11915
11916   array_die = new_die (DW_TAG_array_type, scope_die, type);
11917   add_name_attribute (array_die, type_tag (type));
11918   equate_type_number_to_die (type, array_die);
11919
11920   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11921   if (is_fortran ()
11922       && info->ndimensions >= 2)
11923     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11924
11925   if (info->data_location)
11926     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11927                           info->base_decl);
11928   if (info->associated)
11929     add_descr_info_field (array_die, DW_AT_associated, info->associated,
11930                           info->base_decl);
11931   if (info->allocated)
11932     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11933                           info->base_decl);
11934
11935   for (dim = 0; dim < info->ndimensions; dim++)
11936     {
11937       dw_die_ref subrange_die
11938         = new_die (DW_TAG_subrange_type, array_die, NULL);
11939
11940       if (info->dimen[dim].lower_bound)
11941         {
11942           /* If it is the default value, omit it.  */
11943           if ((is_c_family () || is_java ())
11944               && integer_zerop (info->dimen[dim].lower_bound))
11945             ;
11946           else if (is_fortran ()
11947                    && integer_onep (info->dimen[dim].lower_bound))
11948             ;
11949           else
11950             add_descr_info_field (subrange_die, DW_AT_lower_bound,
11951                                   info->dimen[dim].lower_bound,
11952                                   info->base_decl);
11953         }
11954       if (info->dimen[dim].upper_bound)
11955         add_descr_info_field (subrange_die, DW_AT_upper_bound,
11956                               info->dimen[dim].upper_bound,
11957                               info->base_decl);
11958       if (info->dimen[dim].stride)
11959         add_descr_info_field (subrange_die, DW_AT_byte_stride,
11960                               info->dimen[dim].stride,
11961                               info->base_decl);
11962     }
11963
11964   gen_type_die (info->element_type, context_die);
11965   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11966
11967   if (get_AT (array_die, DW_AT_name))
11968     add_pubtype (type, array_die);
11969 }
11970
11971 #if 0
11972 static void
11973 gen_entry_point_die (tree decl, dw_die_ref context_die)
11974 {
11975   tree origin = decl_ultimate_origin (decl);
11976   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11977
11978   if (origin != NULL)
11979     add_abstract_origin_attribute (decl_die, origin);
11980   else
11981     {
11982       add_name_and_src_coords_attributes (decl_die, decl);
11983       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11984                           0, 0, context_die);
11985     }
11986
11987   if (DECL_ABSTRACT (decl))
11988     equate_decl_number_to_die (decl, decl_die);
11989   else
11990     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11991 }
11992 #endif
11993
11994 /* Walk through the list of incomplete types again, trying once more to
11995    emit full debugging info for them.  */
11996
11997 static void
11998 retry_incomplete_types (void)
11999 {
12000   int i;
12001
12002   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12003     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12004 }
12005
12006 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
12007
12008 static void
12009 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12010 {
12011   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12012
12013   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12014      be incomplete and such types are not marked.  */
12015   add_abstract_origin_attribute (type_die, type);
12016 }
12017
12018 /* Determine what tag to use for a record type.  */
12019
12020 static enum dwarf_tag
12021 record_type_tag (tree type)
12022 {
12023   if (! lang_hooks.types.classify_record)
12024     return DW_TAG_structure_type;
12025
12026   switch (lang_hooks.types.classify_record (type))
12027     {
12028     case RECORD_IS_STRUCT:
12029       return DW_TAG_structure_type;
12030
12031     case RECORD_IS_CLASS:
12032       return DW_TAG_class_type;
12033
12034     case RECORD_IS_INTERFACE:
12035       return DW_TAG_interface_type;
12036
12037     default:
12038       gcc_unreachable ();
12039     }
12040 }
12041
12042 /* Generate a DIE to represent an inlined instance of a structure type.  */
12043
12044 static void
12045 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12046 {
12047   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12048
12049   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12050      be incomplete and such types are not marked.  */
12051   add_abstract_origin_attribute (type_die, type);
12052 }
12053
12054 /* Generate a DIE to represent an inlined instance of a union type.  */
12055
12056 static void
12057 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12058 {
12059   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12060
12061   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12062      be incomplete and such types are not marked.  */
12063   add_abstract_origin_attribute (type_die, type);
12064 }
12065
12066 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
12067    include all of the information about the enumeration values also. Each
12068    enumerated type name/value is listed as a child of the enumerated type
12069    DIE.  */
12070
12071 static dw_die_ref
12072 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12073 {
12074   dw_die_ref type_die = lookup_type_die (type);
12075
12076   if (type_die == NULL)
12077     {
12078       type_die = new_die (DW_TAG_enumeration_type,
12079                           scope_die_for (type, context_die), type);
12080       equate_type_number_to_die (type, type_die);
12081       add_name_attribute (type_die, type_tag (type));
12082     }
12083   else if (! TYPE_SIZE (type))
12084     return type_die;
12085   else
12086     remove_AT (type_die, DW_AT_declaration);
12087
12088   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12089      given enum type is incomplete, do not generate the DW_AT_byte_size
12090      attribute or the DW_AT_element_list attribute.  */
12091   if (TYPE_SIZE (type))
12092     {
12093       tree link;
12094
12095       TREE_ASM_WRITTEN (type) = 1;
12096       add_byte_size_attribute (type_die, type);
12097       if (TYPE_STUB_DECL (type) != NULL_TREE)
12098         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12099
12100       /* If the first reference to this type was as the return type of an
12101          inline function, then it may not have a parent.  Fix this now.  */
12102       if (type_die->die_parent == NULL)
12103         add_child_die (scope_die_for (type, context_die), type_die);
12104
12105       for (link = TYPE_VALUES (type);
12106            link != NULL; link = TREE_CHAIN (link))
12107         {
12108           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12109           tree value = TREE_VALUE (link);
12110
12111           add_name_attribute (enum_die,
12112                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12113
12114           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12115             /* DWARF2 does not provide a way of indicating whether or
12116                not enumeration constants are signed or unsigned.  GDB
12117                always assumes the values are signed, so we output all
12118                values as if they were signed.  That means that
12119                enumeration constants with very large unsigned values
12120                will appear to have negative values in the debugger.  */
12121             add_AT_int (enum_die, DW_AT_const_value,
12122                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12123         }
12124     }
12125   else
12126     add_AT_flag (type_die, DW_AT_declaration, 1);
12127
12128   if (get_AT (type_die, DW_AT_name))
12129     add_pubtype (type, type_die);
12130
12131   return type_die;
12132 }
12133
12134 /* Generate a DIE to represent either a real live formal parameter decl or to
12135    represent just the type of some formal parameter position in some function
12136    type.
12137
12138    Note that this routine is a bit unusual because its argument may be a
12139    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12140    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12141    node.  If it's the former then this function is being called to output a
12142    DIE to represent a formal parameter object (or some inlining thereof).  If
12143    it's the latter, then this function is only being called to output a
12144    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12145    argument type of some subprogram type.  */
12146
12147 static dw_die_ref
12148 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12149 {
12150   dw_die_ref parm_die
12151     = new_die (DW_TAG_formal_parameter, context_die, node);
12152   tree origin;
12153
12154   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12155     {
12156     case tcc_declaration:
12157       origin = decl_ultimate_origin (node);
12158       if (origin != NULL)
12159         add_abstract_origin_attribute (parm_die, origin);
12160       else
12161         {
12162           tree type = TREE_TYPE (node);
12163           add_name_and_src_coords_attributes (parm_die, node);
12164           if (DECL_BY_REFERENCE (node))
12165             type = TREE_TYPE (type);
12166           add_type_attribute (parm_die, type,
12167                               TREE_READONLY (node),
12168                               TREE_THIS_VOLATILE (node),
12169                               context_die);
12170           if (DECL_ARTIFICIAL (node))
12171             add_AT_flag (parm_die, DW_AT_artificial, 1);
12172         }
12173
12174       equate_decl_number_to_die (node, parm_die);
12175       if (! DECL_ABSTRACT (node))
12176         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12177
12178       break;
12179
12180     case tcc_type:
12181       /* We were called with some kind of a ..._TYPE node.  */
12182       add_type_attribute (parm_die, node, 0, 0, context_die);
12183       break;
12184
12185     default:
12186       gcc_unreachable ();
12187     }
12188
12189   return parm_die;
12190 }
12191
12192 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12193    at the end of an (ANSI prototyped) formal parameters list.  */
12194
12195 static void
12196 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12197 {
12198   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12199 }
12200
12201 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12202    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12203    parameters as specified in some function type specification (except for
12204    those which appear as part of a function *definition*).  */
12205
12206 static void
12207 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12208 {
12209   tree link;
12210   tree formal_type = NULL;
12211   tree first_parm_type;
12212   tree arg;
12213
12214   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12215     {
12216       arg = DECL_ARGUMENTS (function_or_method_type);
12217       function_or_method_type = TREE_TYPE (function_or_method_type);
12218     }
12219   else
12220     arg = NULL_TREE;
12221
12222   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12223
12224   /* Make our first pass over the list of formal parameter types and output a
12225      DW_TAG_formal_parameter DIE for each one.  */
12226   for (link = first_parm_type; link; )
12227     {
12228       dw_die_ref parm_die;
12229
12230       formal_type = TREE_VALUE (link);
12231       if (formal_type == void_type_node)
12232         break;
12233
12234       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12235       parm_die = gen_formal_parameter_die (formal_type, context_die);
12236       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12237            && link == first_parm_type)
12238           || (arg && DECL_ARTIFICIAL (arg)))
12239         add_AT_flag (parm_die, DW_AT_artificial, 1);
12240
12241       link = TREE_CHAIN (link);
12242       if (arg)
12243         arg = TREE_CHAIN (arg);
12244     }
12245
12246   /* If this function type has an ellipsis, add a
12247      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12248   if (formal_type != void_type_node)
12249     gen_unspecified_parameters_die (function_or_method_type, context_die);
12250
12251   /* Make our second (and final) pass over the list of formal parameter types
12252      and output DIEs to represent those types (as necessary).  */
12253   for (link = TYPE_ARG_TYPES (function_or_method_type);
12254        link && TREE_VALUE (link);
12255        link = TREE_CHAIN (link))
12256     gen_type_die (TREE_VALUE (link), context_die);
12257 }
12258
12259 /* We want to generate the DIE for TYPE so that we can generate the
12260    die for MEMBER, which has been defined; we will need to refer back
12261    to the member declaration nested within TYPE.  If we're trying to
12262    generate minimal debug info for TYPE, processing TYPE won't do the
12263    trick; we need to attach the member declaration by hand.  */
12264
12265 static void
12266 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12267 {
12268   gen_type_die (type, context_die);
12269
12270   /* If we're trying to avoid duplicate debug info, we may not have
12271      emitted the member decl for this function.  Emit it now.  */
12272   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12273       && ! lookup_decl_die (member))
12274     {
12275       dw_die_ref type_die;
12276       gcc_assert (!decl_ultimate_origin (member));
12277
12278       push_decl_scope (type);
12279       type_die = lookup_type_die (type);
12280       if (TREE_CODE (member) == FUNCTION_DECL)
12281         gen_subprogram_die (member, type_die);
12282       else if (TREE_CODE (member) == FIELD_DECL)
12283         {
12284           /* Ignore the nameless fields that are used to skip bits but handle
12285              C++ anonymous unions and structs.  */
12286           if (DECL_NAME (member) != NULL_TREE
12287               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12288               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12289             {
12290               gen_type_die (member_declared_type (member), type_die);
12291               gen_field_die (member, type_die);
12292             }
12293         }
12294       else
12295         gen_variable_die (member, type_die);
12296
12297       pop_decl_scope ();
12298     }
12299 }
12300
12301 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12302    may later generate inlined and/or out-of-line instances of.  */
12303
12304 static void
12305 dwarf2out_abstract_function (tree decl)
12306 {
12307   dw_die_ref old_die;
12308   tree save_fn;
12309   tree context;
12310   int was_abstract = DECL_ABSTRACT (decl);
12311
12312   /* Make sure we have the actual abstract inline, not a clone.  */
12313   decl = DECL_ORIGIN (decl);
12314
12315   old_die = lookup_decl_die (decl);
12316   if (old_die && get_AT (old_die, DW_AT_inline))
12317     /* We've already generated the abstract instance.  */
12318     return;
12319
12320   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12321      we don't get confused by DECL_ABSTRACT.  */
12322   if (debug_info_level > DINFO_LEVEL_TERSE)
12323     {
12324       context = decl_class_context (decl);
12325       if (context)
12326         gen_type_die_for_member
12327           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12328     }
12329
12330   /* Pretend we've just finished compiling this function.  */
12331   save_fn = current_function_decl;
12332   current_function_decl = decl;
12333   push_cfun (DECL_STRUCT_FUNCTION (decl));
12334
12335   set_decl_abstract_flags (decl, 1);
12336   dwarf2out_decl (decl);
12337   if (! was_abstract)
12338     set_decl_abstract_flags (decl, 0);
12339
12340   current_function_decl = save_fn;
12341   pop_cfun ();
12342 }
12343
12344 /* Helper function of premark_used_types() which gets called through
12345    htab_traverse_resize().
12346
12347    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12348    marked as unused by prune_unused_types.  */
12349 static int
12350 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12351 {
12352   tree type;
12353   dw_die_ref die;
12354
12355   type = *slot;
12356   die = lookup_type_die (type);
12357   if (die != NULL)
12358     die->die_perennial_p = 1;
12359   return 1;
12360 }
12361
12362 /* Mark all members of used_types_hash as perennial.  */
12363 static void
12364 premark_used_types (void)
12365 {
12366   if (cfun && cfun->used_types_hash)
12367     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12368 }
12369
12370 /* Generate a DIE to represent a declared function (either file-scope or
12371    block-local).  */
12372
12373 static void
12374 gen_subprogram_die (tree decl, dw_die_ref context_die)
12375 {
12376   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12377   tree origin = decl_ultimate_origin (decl);
12378   dw_die_ref subr_die;
12379   tree fn_arg_types;
12380   tree outer_scope;
12381   dw_die_ref old_die = lookup_decl_die (decl);
12382   int declaration = (current_function_decl != decl
12383                      || class_or_namespace_scope_p (context_die));
12384
12385   premark_used_types ();
12386
12387   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12388      started to generate the abstract instance of an inline, decided to output
12389      its containing class, and proceeded to emit the declaration of the inline
12390      from the member list for the class.  If so, DECLARATION takes priority;
12391      we'll get back to the abstract instance when done with the class.  */
12392
12393   /* The class-scope declaration DIE must be the primary DIE.  */
12394   if (origin && declaration && class_or_namespace_scope_p (context_die))
12395     {
12396       origin = NULL;
12397       gcc_assert (!old_die);
12398     }
12399
12400   /* Now that the C++ front end lazily declares artificial member fns, we
12401      might need to retrofit the declaration into its class.  */
12402   if (!declaration && !origin && !old_die
12403       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12404       && !class_or_namespace_scope_p (context_die)
12405       && debug_info_level > DINFO_LEVEL_TERSE)
12406     old_die = force_decl_die (decl);
12407
12408   if (origin != NULL)
12409     {
12410       gcc_assert (!declaration || local_scope_p (context_die));
12411
12412       /* Fixup die_parent for the abstract instance of a nested
12413          inline function.  */
12414       if (old_die && old_die->die_parent == NULL)
12415         add_child_die (context_die, old_die);
12416
12417       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12418       add_abstract_origin_attribute (subr_die, origin);
12419     }
12420   else if (old_die)
12421     {
12422       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12423       struct dwarf_file_data * file_index = lookup_filename (s.file);
12424
12425       if (!get_AT_flag (old_die, DW_AT_declaration)
12426           /* We can have a normal definition following an inline one in the
12427              case of redefinition of GNU C extern inlines.
12428              It seems reasonable to use AT_specification in this case.  */
12429           && !get_AT (old_die, DW_AT_inline))
12430         {
12431           /* Detect and ignore this case, where we are trying to output
12432              something we have already output.  */
12433           return;
12434         }
12435
12436       /* If the definition comes from the same place as the declaration,
12437          maybe use the old DIE.  We always want the DIE for this function
12438          that has the *_pc attributes to be under comp_unit_die so the
12439          debugger can find it.  We also need to do this for abstract
12440          instances of inlines, since the spec requires the out-of-line copy
12441          to have the same parent.  For local class methods, this doesn't
12442          apply; we just use the old DIE.  */
12443       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12444           && (DECL_ARTIFICIAL (decl)
12445               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12446                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12447                       == (unsigned) s.line))))
12448         {
12449           subr_die = old_die;
12450
12451           /* Clear out the declaration attribute and the formal parameters.
12452              Do not remove all children, because it is possible that this
12453              declaration die was forced using force_decl_die(). In such
12454              cases die that forced declaration die (e.g. TAG_imported_module)
12455              is one of the children that we do not want to remove.  */
12456           remove_AT (subr_die, DW_AT_declaration);
12457           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12458         }
12459       else
12460         {
12461           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12462           add_AT_specification (subr_die, old_die);
12463           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12464             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12465           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12466             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12467         }
12468     }
12469   else
12470     {
12471       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12472
12473       if (TREE_PUBLIC (decl))
12474         add_AT_flag (subr_die, DW_AT_external, 1);
12475
12476       add_name_and_src_coords_attributes (subr_die, decl);
12477       if (debug_info_level > DINFO_LEVEL_TERSE)
12478         {
12479           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12480           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12481                               0, 0, context_die);
12482         }
12483
12484       add_pure_or_virtual_attribute (subr_die, decl);
12485       if (DECL_ARTIFICIAL (decl))
12486         add_AT_flag (subr_die, DW_AT_artificial, 1);
12487
12488       if (TREE_PROTECTED (decl))
12489         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12490       else if (TREE_PRIVATE (decl))
12491         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12492     }
12493
12494   if (declaration)
12495     {
12496       if (!old_die || !get_AT (old_die, DW_AT_inline))
12497         {
12498           add_AT_flag (subr_die, DW_AT_declaration, 1);
12499
12500           /* The first time we see a member function, it is in the context of
12501              the class to which it belongs.  We make sure of this by emitting
12502              the class first.  The next time is the definition, which is
12503              handled above.  The two may come from the same source text.
12504
12505              Note that force_decl_die() forces function declaration die. It is
12506              later reused to represent definition.  */
12507           equate_decl_number_to_die (decl, subr_die);
12508         }
12509     }
12510   else if (DECL_ABSTRACT (decl))
12511     {
12512       if (DECL_DECLARED_INLINE_P (decl))
12513         {
12514           if (cgraph_function_possibly_inlined_p (decl))
12515             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12516           else
12517             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12518         }
12519       else
12520         {
12521           if (cgraph_function_possibly_inlined_p (decl))
12522             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12523           else
12524             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12525         }
12526
12527       if (DECL_DECLARED_INLINE_P (decl)
12528           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12529         add_AT_flag (subr_die, DW_AT_artificial, 1);
12530
12531       equate_decl_number_to_die (decl, subr_die);
12532     }
12533   else if (!DECL_EXTERNAL (decl))
12534     {
12535       HOST_WIDE_INT cfa_fb_offset;
12536
12537       if (!old_die || !get_AT (old_die, DW_AT_inline))
12538         equate_decl_number_to_die (decl, subr_die);
12539
12540       if (!flag_reorder_blocks_and_partition)
12541         {
12542           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12543                                        current_function_funcdef_no);
12544           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12545           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12546                                        current_function_funcdef_no);
12547           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12548
12549           add_pubname (decl, subr_die);
12550           add_arange (decl, subr_die);
12551         }
12552       else
12553         {  /* Do nothing for now; maybe need to duplicate die, one for
12554               hot section and ond for cold section, then use the hot/cold
12555               section begin/end labels to generate the aranges...  */
12556           /*
12557             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12558             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12559             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12560             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12561
12562             add_pubname (decl, subr_die);
12563             add_arange (decl, subr_die);
12564             add_arange (decl, subr_die);
12565            */
12566         }
12567
12568 #ifdef MIPS_DEBUGGING_INFO
12569       /* Add a reference to the FDE for this routine.  */
12570       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12571 #endif
12572
12573       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12574
12575       /* We define the "frame base" as the function's CFA.  This is more
12576          convenient for several reasons: (1) It's stable across the prologue
12577          and epilogue, which makes it better than just a frame pointer,
12578          (2) With dwarf3, there exists a one-byte encoding that allows us
12579          to reference the .debug_frame data by proxy, but failing that,
12580          (3) We can at least reuse the code inspection and interpretation
12581          code that determines the CFA position at various points in the
12582          function.  */
12583       /* ??? Use some command-line or configury switch to enable the use
12584          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
12585          consumers that understand it; fall back to "pure" dwarf2 and
12586          convert the CFA data into a location list.  */
12587       {
12588         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12589         if (list->dw_loc_next)
12590           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12591         else
12592           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12593       }
12594
12595       /* Compute a displacement from the "steady-state frame pointer" to
12596          the CFA.  The former is what all stack slots and argument slots
12597          will reference in the rtl; the later is what we've told the
12598          debugger about.  We'll need to adjust all frame_base references
12599          by this displacement.  */
12600       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12601
12602       if (cfun->static_chain_decl)
12603         add_AT_location_description (subr_die, DW_AT_static_link,
12604                  loc_descriptor_from_tree (cfun->static_chain_decl));
12605     }
12606
12607   /* Now output descriptions of the arguments for this function. This gets
12608      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12609      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12610      `...' at the end of the formal parameter list.  In order to find out if
12611      there was a trailing ellipsis or not, we must instead look at the type
12612      associated with the FUNCTION_DECL.  This will be a node of type
12613      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12614      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12615      an ellipsis at the end.  */
12616
12617   /* In the case where we are describing a mere function declaration, all we
12618      need to do here (and all we *can* do here) is to describe the *types* of
12619      its formal parameters.  */
12620   if (debug_info_level <= DINFO_LEVEL_TERSE)
12621     ;
12622   else if (declaration)
12623     gen_formal_types_die (decl, subr_die);
12624   else
12625     {
12626       /* Generate DIEs to represent all known formal parameters.  */
12627       tree arg_decls = DECL_ARGUMENTS (decl);
12628       tree parm;
12629
12630       /* When generating DIEs, generate the unspecified_parameters DIE
12631          instead if we come across the arg "__builtin_va_alist" */
12632       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12633         if (TREE_CODE (parm) == PARM_DECL)
12634           {
12635             if (DECL_NAME (parm)
12636                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12637                             "__builtin_va_alist"))
12638               gen_unspecified_parameters_die (parm, subr_die);
12639             else
12640               gen_decl_die (parm, subr_die);
12641           }
12642
12643       /* Decide whether we need an unspecified_parameters DIE at the end.
12644          There are 2 more cases to do this for: 1) the ansi ... declaration -
12645          this is detectable when the end of the arg list is not a
12646          void_type_node 2) an unprototyped function declaration (not a
12647          definition).  This just means that we have no info about the
12648          parameters at all.  */
12649       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12650       if (fn_arg_types != NULL)
12651         {
12652           /* This is the prototyped case, check for....  */
12653           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12654             gen_unspecified_parameters_die (decl, subr_die);
12655         }
12656       else if (DECL_INITIAL (decl) == NULL_TREE)
12657         gen_unspecified_parameters_die (decl, subr_die);
12658     }
12659
12660   /* Output Dwarf info for all of the stuff within the body of the function
12661      (if it has one - it may be just a declaration).  */
12662   outer_scope = DECL_INITIAL (decl);
12663
12664   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12665      a function.  This BLOCK actually represents the outermost binding contour
12666      for the function, i.e. the contour in which the function's formal
12667      parameters and labels get declared. Curiously, it appears that the front
12668      end doesn't actually put the PARM_DECL nodes for the current function onto
12669      the BLOCK_VARS list for this outer scope, but are strung off of the
12670      DECL_ARGUMENTS list for the function instead.
12671
12672      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12673      the LABEL_DECL nodes for the function however, and we output DWARF info
12674      for those in decls_for_scope.  Just within the `outer_scope' there will be
12675      a BLOCK node representing the function's outermost pair of curly braces,
12676      and any blocks used for the base and member initializers of a C++
12677      constructor function.  */
12678   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12679     {
12680       /* Emit a DW_TAG_variable DIE for a named return value.  */
12681       if (DECL_NAME (DECL_RESULT (decl)))
12682         gen_decl_die (DECL_RESULT (decl), subr_die);
12683
12684       current_function_has_inlines = 0;
12685       decls_for_scope (outer_scope, subr_die, 0);
12686
12687 #if 0 && defined (MIPS_DEBUGGING_INFO)
12688       if (current_function_has_inlines)
12689         {
12690           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12691           if (! comp_unit_has_inlines)
12692             {
12693               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12694               comp_unit_has_inlines = 1;
12695             }
12696         }
12697 #endif
12698     }
12699   /* Add the calling convention attribute if requested.  */
12700   add_calling_convention_attribute (subr_die, decl);
12701
12702 }
12703
12704 /* Generate a DIE to represent a declared data object.  */
12705
12706 static void
12707 gen_variable_die (tree decl, dw_die_ref context_die)
12708 {
12709   HOST_WIDE_INT off;
12710   tree com_decl;
12711   dw_die_ref var_die;
12712   tree origin = decl_ultimate_origin (decl);
12713   dw_die_ref old_die = lookup_decl_die (decl);
12714   int declaration = (DECL_EXTERNAL (decl)
12715                      /* If DECL is COMDAT and has not actually been
12716                         emitted, we cannot take its address; there
12717                         might end up being no definition anywhere in
12718                         the program.  For example, consider the C++
12719                         test case:
12720
12721                           template <class T>
12722                           struct S { static const int i = 7; };
12723
12724                           template <class T>
12725                           const int S<T>::i;
12726
12727                           int f() { return S<int>::i; }
12728
12729                         Here, S<int>::i is not DECL_EXTERNAL, but no
12730                         definition is required, so the compiler will
12731                         not emit a definition.  */
12732                      || (TREE_CODE (decl) == VAR_DECL
12733                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12734                      || class_or_namespace_scope_p (context_die));
12735
12736   com_decl = fortran_common (decl, &off);
12737
12738   /* Symbol in common gets emitted as a child of the common block, in the form
12739      of a data member.
12740
12741      ??? This creates a new common block die for every common block symbol.
12742      Better to share same common block die for all symbols in that block.  */
12743   if (com_decl)
12744     {
12745       tree field;
12746       dw_die_ref com_die;
12747       const char *cnam = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
12748       dw_loc_descr_ref loc = loc_descriptor_from_tree (com_decl);
12749
12750       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
12751       var_die = new_die (DW_TAG_common_block, context_die, decl);
12752       add_name_and_src_coords_attributes (var_die, field);
12753       add_AT_flag (var_die, DW_AT_external, 1);
12754       add_AT_loc (var_die, DW_AT_location, loc);
12755       com_die = new_die (DW_TAG_member, var_die, decl);
12756       add_name_and_src_coords_attributes (com_die, decl);
12757       add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
12758                           TREE_THIS_VOLATILE (decl), context_die);
12759       add_AT_loc (com_die, DW_AT_data_member_location,
12760                   int_loc_descriptor (off));
12761       add_pubname_string (cnam, var_die); /* ??? needed? */
12762       return;
12763     }
12764
12765   var_die = new_die (DW_TAG_variable, context_die, decl);
12766
12767   if (origin != NULL)
12768     add_abstract_origin_attribute (var_die, origin);
12769
12770   /* Loop unrolling can create multiple blocks that refer to the same
12771      static variable, so we must test for the DW_AT_declaration flag.
12772
12773      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12774      copy decls and set the DECL_ABSTRACT flag on them instead of
12775      sharing them.
12776
12777      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12778
12779      ??? The declare_in_namespace support causes us to get two DIEs for one
12780      variable, both of which are declarations.  We want to avoid considering
12781      one to be a specification, so we must test that this DIE is not a
12782      declaration.  */
12783   else if (old_die && TREE_STATIC (decl) && ! declaration
12784            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12785     {
12786       /* This is a definition of a C++ class level static.  */
12787       add_AT_specification (var_die, old_die);
12788       if (DECL_NAME (decl))
12789         {
12790           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12791           struct dwarf_file_data * file_index = lookup_filename (s.file);
12792
12793           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12794             add_AT_file (var_die, DW_AT_decl_file, file_index);
12795
12796           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12797             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12798         }
12799     }
12800   else
12801     {
12802       tree type = TREE_TYPE (decl);
12803       if ((TREE_CODE (decl) == PARM_DECL
12804            || TREE_CODE (decl) == RESULT_DECL)
12805           && DECL_BY_REFERENCE (decl))
12806         type = TREE_TYPE (type);
12807
12808       add_name_and_src_coords_attributes (var_die, decl);
12809       add_type_attribute (var_die, type, TREE_READONLY (decl),
12810                           TREE_THIS_VOLATILE (decl), context_die);
12811
12812       if (TREE_PUBLIC (decl))
12813         add_AT_flag (var_die, DW_AT_external, 1);
12814
12815       if (DECL_ARTIFICIAL (decl))
12816         add_AT_flag (var_die, DW_AT_artificial, 1);
12817
12818       if (TREE_PROTECTED (decl))
12819         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12820       else if (TREE_PRIVATE (decl))
12821         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12822     }
12823
12824   if (declaration)
12825     add_AT_flag (var_die, DW_AT_declaration, 1);
12826
12827   if (DECL_ABSTRACT (decl) || declaration)
12828     equate_decl_number_to_die (decl, var_die);
12829
12830   if (! declaration && ! DECL_ABSTRACT (decl))
12831     {
12832       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12833       add_pubname (decl, var_die);
12834     }
12835   else
12836     tree_add_const_value_attribute (var_die, decl);
12837 }
12838
12839 /* Generate a DIE to represent a label identifier.  */
12840
12841 static void
12842 gen_label_die (tree decl, dw_die_ref context_die)
12843 {
12844   tree origin = decl_ultimate_origin (decl);
12845   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12846   rtx insn;
12847   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12848
12849   if (origin != NULL)
12850     add_abstract_origin_attribute (lbl_die, origin);
12851   else
12852     add_name_and_src_coords_attributes (lbl_die, decl);
12853
12854   if (DECL_ABSTRACT (decl))
12855     equate_decl_number_to_die (decl, lbl_die);
12856   else
12857     {
12858       insn = DECL_RTL_IF_SET (decl);
12859
12860       /* Deleted labels are programmer specified labels which have been
12861          eliminated because of various optimizations.  We still emit them
12862          here so that it is possible to put breakpoints on them.  */
12863       if (insn
12864           && (LABEL_P (insn)
12865               || ((NOTE_P (insn)
12866                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12867         {
12868           /* When optimization is enabled (via -O) some parts of the compiler
12869              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12870              represent source-level labels which were explicitly declared by
12871              the user.  This really shouldn't be happening though, so catch
12872              it if it ever does happen.  */
12873           gcc_assert (!INSN_DELETED_P (insn));
12874
12875           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12876           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12877         }
12878     }
12879 }
12880
12881 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12882    attributes to the DIE for a block STMT, to describe where the inlined
12883    function was called from.  This is similar to add_src_coords_attributes.  */
12884
12885 static inline void
12886 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12887 {
12888   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12889
12890   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12891   add_AT_unsigned (die, DW_AT_call_line, s.line);
12892 }
12893
12894
12895 /* If STMT's abstract origin is a function declaration and STMT's
12896    first subblock's abstract origin is the function's outermost block,
12897    then we're looking at the main entry point.  */
12898 static bool
12899 is_inlined_entry_point (const_tree stmt)
12900 {
12901   tree decl, block;
12902
12903   if (!stmt || TREE_CODE (stmt) != BLOCK)
12904     return false;
12905
12906   decl = block_ultimate_origin (stmt);
12907
12908   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12909     return false;
12910
12911   block = BLOCK_SUBBLOCKS (stmt);
12912
12913   if (block)
12914     {
12915       if (TREE_CODE (block) != BLOCK)
12916         return false;
12917
12918       block = block_ultimate_origin (block);
12919     }
12920
12921   return block == DECL_INITIAL (decl);
12922 }
12923
12924 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12925    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12926
12927 static inline void
12928 add_high_low_attributes (tree stmt, dw_die_ref die)
12929 {
12930   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12931
12932   if (BLOCK_FRAGMENT_CHAIN (stmt))
12933     {
12934       tree chain;
12935
12936       if (is_inlined_entry_point (stmt))
12937         {
12938           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12939                                        BLOCK_NUMBER (stmt));
12940           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12941         }
12942
12943       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12944
12945       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12946       do
12947         {
12948           add_ranges (chain);
12949           chain = BLOCK_FRAGMENT_CHAIN (chain);
12950         }
12951       while (chain);
12952       add_ranges (NULL);
12953     }
12954   else
12955     {
12956       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12957                                    BLOCK_NUMBER (stmt));
12958       add_AT_lbl_id (die, DW_AT_low_pc, label);
12959       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12960                                    BLOCK_NUMBER (stmt));
12961       add_AT_lbl_id (die, DW_AT_high_pc, label);
12962     }
12963 }
12964
12965 /* Generate a DIE for a lexical block.  */
12966
12967 static void
12968 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12969 {
12970   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12971
12972   if (! BLOCK_ABSTRACT (stmt))
12973     add_high_low_attributes (stmt, stmt_die);
12974
12975   decls_for_scope (stmt, stmt_die, depth);
12976 }
12977
12978 /* Generate a DIE for an inlined subprogram.  */
12979
12980 static void
12981 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12982 {
12983   tree decl = block_ultimate_origin (stmt);
12984
12985   /* Emit info for the abstract instance first, if we haven't yet.  We
12986      must emit this even if the block is abstract, otherwise when we
12987      emit the block below (or elsewhere), we may end up trying to emit
12988      a die whose origin die hasn't been emitted, and crashing.  */
12989   dwarf2out_abstract_function (decl);
12990
12991   if (! BLOCK_ABSTRACT (stmt))
12992     {
12993       dw_die_ref subr_die
12994         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12995
12996       add_abstract_origin_attribute (subr_die, decl);
12997       add_high_low_attributes (stmt, subr_die);
12998       add_call_src_coords_attributes (stmt, subr_die);
12999
13000       decls_for_scope (stmt, subr_die, depth);
13001       current_function_has_inlines = 1;
13002     }
13003   else
13004     /* We may get here if we're the outer block of function A that was
13005        inlined into function B that was inlined into function C.  When
13006        generating debugging info for C, dwarf2out_abstract_function(B)
13007        would mark all inlined blocks as abstract, including this one.
13008        So, we wouldn't (and shouldn't) expect labels to be generated
13009        for this one.  Instead, just emit debugging info for
13010        declarations within the block.  This is particularly important
13011        in the case of initializers of arguments passed from B to us:
13012        if they're statement expressions containing declarations, we
13013        wouldn't generate dies for their abstract variables, and then,
13014        when generating dies for the real variables, we'd die (pun
13015        intended :-)  */
13016     gen_lexical_block_die (stmt, context_die, depth);
13017 }
13018
13019 /* Generate a DIE for a field in a record, or structure.  */
13020
13021 static void
13022 gen_field_die (tree decl, dw_die_ref context_die)
13023 {
13024   dw_die_ref decl_die;
13025
13026   if (TREE_TYPE (decl) == error_mark_node)
13027     return;
13028
13029   decl_die = new_die (DW_TAG_member, context_die, decl);
13030   add_name_and_src_coords_attributes (decl_die, decl);
13031   add_type_attribute (decl_die, member_declared_type (decl),
13032                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13033                       context_die);
13034
13035   if (DECL_BIT_FIELD_TYPE (decl))
13036     {
13037       add_byte_size_attribute (decl_die, decl);
13038       add_bit_size_attribute (decl_die, decl);
13039       add_bit_offset_attribute (decl_die, decl);
13040     }
13041
13042   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13043     add_data_member_location_attribute (decl_die, decl);
13044
13045   if (DECL_ARTIFICIAL (decl))
13046     add_AT_flag (decl_die, DW_AT_artificial, 1);
13047
13048   if (TREE_PROTECTED (decl))
13049     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13050   else if (TREE_PRIVATE (decl))
13051     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13052
13053   /* Equate decl number to die, so that we can look up this decl later on.  */
13054   equate_decl_number_to_die (decl, decl_die);
13055 }
13056
13057 #if 0
13058 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13059    Use modified_type_die instead.
13060    We keep this code here just in case these types of DIEs may be needed to
13061    represent certain things in other languages (e.g. Pascal) someday.  */
13062
13063 static void
13064 gen_pointer_type_die (tree type, dw_die_ref context_die)
13065 {
13066   dw_die_ref ptr_die
13067     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13068
13069   equate_type_number_to_die (type, ptr_die);
13070   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13071   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13072 }
13073
13074 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13075    Use modified_type_die instead.
13076    We keep this code here just in case these types of DIEs may be needed to
13077    represent certain things in other languages (e.g. Pascal) someday.  */
13078
13079 static void
13080 gen_reference_type_die (tree type, dw_die_ref context_die)
13081 {
13082   dw_die_ref ref_die
13083     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13084
13085   equate_type_number_to_die (type, ref_die);
13086   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13087   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13088 }
13089 #endif
13090
13091 /* Generate a DIE for a pointer to a member type.  */
13092
13093 static void
13094 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13095 {
13096   dw_die_ref ptr_die
13097     = new_die (DW_TAG_ptr_to_member_type,
13098                scope_die_for (type, context_die), type);
13099
13100   equate_type_number_to_die (type, ptr_die);
13101   add_AT_die_ref (ptr_die, DW_AT_containing_type,
13102                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13103   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13104 }
13105
13106 /* Generate the DIE for the compilation unit.  */
13107
13108 static dw_die_ref
13109 gen_compile_unit_die (const char *filename)
13110 {
13111   dw_die_ref die;
13112   char producer[250];
13113   const char *language_string = lang_hooks.name;
13114   int language;
13115
13116   die = new_die (DW_TAG_compile_unit, NULL, NULL);
13117
13118   if (filename)
13119     {
13120       add_name_attribute (die, filename);
13121       /* Don't add cwd for <built-in>.  */
13122       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13123         add_comp_dir_attribute (die);
13124     }
13125
13126   sprintf (producer, "%s %s", language_string, version_string);
13127
13128 #ifdef MIPS_DEBUGGING_INFO
13129   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13130      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13131      not appear in the producer string, the debugger reaches the conclusion
13132      that the object file is stripped and has no debugging information.
13133      To get the MIPS/SGI debugger to believe that there is debugging
13134      information in the object file, we add a -g to the producer string.  */
13135   if (debug_info_level > DINFO_LEVEL_TERSE)
13136     strcat (producer, " -g");
13137 #endif
13138
13139   add_AT_string (die, DW_AT_producer, producer);
13140
13141   if (strcmp (language_string, "GNU C++") == 0)
13142     language = DW_LANG_C_plus_plus;
13143   else if (strcmp (language_string, "GNU Ada") == 0)
13144     language = DW_LANG_Ada95;
13145   else if (strcmp (language_string, "GNU F77") == 0)
13146     language = DW_LANG_Fortran77;
13147   else if (strcmp (language_string, "GNU Fortran") == 0)
13148     language = DW_LANG_Fortran95;
13149   else if (strcmp (language_string, "GNU Pascal") == 0)
13150     language = DW_LANG_Pascal83;
13151   else if (strcmp (language_string, "GNU Java") == 0)
13152     language = DW_LANG_Java;
13153   else if (strcmp (language_string, "GNU Objective-C") == 0)
13154     language = DW_LANG_ObjC;
13155   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13156     language = DW_LANG_ObjC_plus_plus;
13157   else
13158     language = DW_LANG_C89;
13159
13160   add_AT_unsigned (die, DW_AT_language, language);
13161   return die;
13162 }
13163
13164 /* Generate the DIE for a base class.  */
13165
13166 static void
13167 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13168 {
13169   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13170
13171   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13172   add_data_member_location_attribute (die, binfo);
13173
13174   if (BINFO_VIRTUAL_P (binfo))
13175     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13176
13177   if (access == access_public_node)
13178     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13179   else if (access == access_protected_node)
13180     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13181 }
13182
13183 /* Generate a DIE for a class member.  */
13184
13185 static void
13186 gen_member_die (tree type, dw_die_ref context_die)
13187 {
13188   tree member;
13189   tree binfo = TYPE_BINFO (type);
13190   dw_die_ref child;
13191
13192   /* If this is not an incomplete type, output descriptions of each of its
13193      members. Note that as we output the DIEs necessary to represent the
13194      members of this record or union type, we will also be trying to output
13195      DIEs to represent the *types* of those members. However the `type'
13196      function (above) will specifically avoid generating type DIEs for member
13197      types *within* the list of member DIEs for this (containing) type except
13198      for those types (of members) which are explicitly marked as also being
13199      members of this (containing) type themselves.  The g++ front- end can
13200      force any given type to be treated as a member of some other (containing)
13201      type by setting the TYPE_CONTEXT of the given (member) type to point to
13202      the TREE node representing the appropriate (containing) type.  */
13203
13204   /* First output info about the base classes.  */
13205   if (binfo)
13206     {
13207       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13208       int i;
13209       tree base;
13210
13211       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13212         gen_inheritance_die (base,
13213                              (accesses ? VEC_index (tree, accesses, i)
13214                               : access_public_node), context_die);
13215     }
13216
13217   /* Now output info about the data members and type members.  */
13218   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13219     {
13220       /* If we thought we were generating minimal debug info for TYPE
13221          and then changed our minds, some of the member declarations
13222          may have already been defined.  Don't define them again, but
13223          do put them in the right order.  */
13224
13225       child = lookup_decl_die (member);
13226       if (child)
13227         splice_child_die (context_die, child);
13228       else
13229         gen_decl_die (member, context_die);
13230     }
13231
13232   /* Now output info about the function members (if any).  */
13233   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13234     {
13235       /* Don't include clones in the member list.  */
13236       if (DECL_ABSTRACT_ORIGIN (member))
13237         continue;
13238
13239       child = lookup_decl_die (member);
13240       if (child)
13241         splice_child_die (context_die, child);
13242       else
13243         gen_decl_die (member, context_die);
13244     }
13245 }
13246
13247 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13248    is set, we pretend that the type was never defined, so we only get the
13249    member DIEs needed by later specification DIEs.  */
13250
13251 static void
13252 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13253                                 enum debug_info_usage usage)
13254 {
13255   dw_die_ref type_die = lookup_type_die (type);
13256   dw_die_ref scope_die = 0;
13257   int nested = 0;
13258   int complete = (TYPE_SIZE (type)
13259                   && (! TYPE_STUB_DECL (type)
13260                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13261   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13262   complete = complete && should_emit_struct_debug (type, usage);
13263
13264   if (type_die && ! complete)
13265     return;
13266
13267   if (TYPE_CONTEXT (type) != NULL_TREE
13268       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13269           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13270     nested = 1;
13271
13272   scope_die = scope_die_for (type, context_die);
13273
13274   if (! type_die || (nested && scope_die == comp_unit_die))
13275     /* First occurrence of type or toplevel definition of nested class.  */
13276     {
13277       dw_die_ref old_die = type_die;
13278
13279       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13280                           ? record_type_tag (type) : DW_TAG_union_type,
13281                           scope_die, type);
13282       equate_type_number_to_die (type, type_die);
13283       if (old_die)
13284         add_AT_specification (type_die, old_die);
13285       else
13286         add_name_attribute (type_die, type_tag (type));
13287     }
13288   else
13289     remove_AT (type_die, DW_AT_declaration);
13290
13291   /* If this type has been completed, then give it a byte_size attribute and
13292      then give a list of members.  */
13293   if (complete && !ns_decl)
13294     {
13295       /* Prevent infinite recursion in cases where the type of some member of
13296          this type is expressed in terms of this type itself.  */
13297       TREE_ASM_WRITTEN (type) = 1;
13298       add_byte_size_attribute (type_die, type);
13299       if (TYPE_STUB_DECL (type) != NULL_TREE)
13300         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13301
13302       /* If the first reference to this type was as the return type of an
13303          inline function, then it may not have a parent.  Fix this now.  */
13304       if (type_die->die_parent == NULL)
13305         add_child_die (scope_die, type_die);
13306
13307       push_decl_scope (type);
13308       gen_member_die (type, type_die);
13309       pop_decl_scope ();
13310
13311       /* GNU extension: Record what type our vtable lives in.  */
13312       if (TYPE_VFIELD (type))
13313         {
13314           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13315
13316           gen_type_die (vtype, context_die);
13317           add_AT_die_ref (type_die, DW_AT_containing_type,
13318                           lookup_type_die (vtype));
13319         }
13320     }
13321   else
13322     {
13323       add_AT_flag (type_die, DW_AT_declaration, 1);
13324
13325       /* We don't need to do this for function-local types.  */
13326       if (TYPE_STUB_DECL (type)
13327           && ! decl_function_context (TYPE_STUB_DECL (type)))
13328         VEC_safe_push (tree, gc, incomplete_types, type);
13329     }
13330
13331   if (get_AT (type_die, DW_AT_name))
13332     add_pubtype (type, type_die);
13333 }
13334
13335 /* Generate a DIE for a subroutine _type_.  */
13336
13337 static void
13338 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13339 {
13340   tree return_type = TREE_TYPE (type);
13341   dw_die_ref subr_die
13342     = new_die (DW_TAG_subroutine_type,
13343                scope_die_for (type, context_die), type);
13344
13345   equate_type_number_to_die (type, subr_die);
13346   add_prototyped_attribute (subr_die, type);
13347   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13348   gen_formal_types_die (type, subr_die);
13349
13350   if (get_AT (subr_die, DW_AT_name))
13351     add_pubtype (type, subr_die);
13352 }
13353
13354 /* Generate a DIE for a type definition.  */
13355
13356 static void
13357 gen_typedef_die (tree decl, dw_die_ref context_die)
13358 {
13359   dw_die_ref type_die;
13360   tree origin;
13361
13362   if (TREE_ASM_WRITTEN (decl))
13363     return;
13364
13365   TREE_ASM_WRITTEN (decl) = 1;
13366   type_die = new_die (DW_TAG_typedef, context_die, decl);
13367   origin = decl_ultimate_origin (decl);
13368   if (origin != NULL)
13369     add_abstract_origin_attribute (type_die, origin);
13370   else
13371     {
13372       tree type;
13373
13374       add_name_and_src_coords_attributes (type_die, decl);
13375       if (DECL_ORIGINAL_TYPE (decl))
13376         {
13377           type = DECL_ORIGINAL_TYPE (decl);
13378
13379           gcc_assert (type != TREE_TYPE (decl));
13380           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13381         }
13382       else
13383         type = TREE_TYPE (decl);
13384
13385       add_type_attribute (type_die, type, TREE_READONLY (decl),
13386                           TREE_THIS_VOLATILE (decl), context_die);
13387     }
13388
13389   if (DECL_ABSTRACT (decl))
13390     equate_decl_number_to_die (decl, type_die);
13391
13392   if (get_AT (type_die, DW_AT_name))
13393     add_pubtype (decl, type_die);
13394 }
13395
13396 /* Generate a type description DIE.  */
13397
13398 static void
13399 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13400                                 enum debug_info_usage usage)
13401 {
13402   int need_pop;
13403   struct array_descr_info info;
13404
13405   if (type == NULL_TREE || type == error_mark_node)
13406     return;
13407
13408   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13409       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13410     {
13411       if (TREE_ASM_WRITTEN (type))
13412         return;
13413
13414       /* Prevent broken recursion; we can't hand off to the same type.  */
13415       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13416
13417       TREE_ASM_WRITTEN (type) = 1;
13418       gen_decl_die (TYPE_NAME (type), context_die);
13419       return;
13420     }
13421
13422   /* If this is an array type with hidden descriptor, handle it first.  */
13423   if (!TREE_ASM_WRITTEN (type)
13424       && lang_hooks.types.get_array_descr_info
13425       && lang_hooks.types.get_array_descr_info (type, &info))
13426     {
13427       gen_descr_array_type_die (type, &info, context_die);
13428       TREE_ASM_WRITTEN (type) = 1;
13429       return;
13430     }
13431
13432   /* We are going to output a DIE to represent the unqualified version
13433      of this type (i.e. without any const or volatile qualifiers) so
13434      get the main variant (i.e. the unqualified version) of this type
13435      now.  (Vectors are special because the debugging info is in the
13436      cloned type itself).  */
13437   if (TREE_CODE (type) != VECTOR_TYPE)
13438     type = type_main_variant (type);
13439
13440   if (TREE_ASM_WRITTEN (type))
13441     return;
13442
13443   switch (TREE_CODE (type))
13444     {
13445     case ERROR_MARK:
13446       break;
13447
13448     case POINTER_TYPE:
13449     case REFERENCE_TYPE:
13450       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13451          ensures that the gen_type_die recursion will terminate even if the
13452          type is recursive.  Recursive types are possible in Ada.  */
13453       /* ??? We could perhaps do this for all types before the switch
13454          statement.  */
13455       TREE_ASM_WRITTEN (type) = 1;
13456
13457       /* For these types, all that is required is that we output a DIE (or a
13458          set of DIEs) to represent the "basis" type.  */
13459       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13460                                 DINFO_USAGE_IND_USE);
13461       break;
13462
13463     case OFFSET_TYPE:
13464       /* This code is used for C++ pointer-to-data-member types.
13465          Output a description of the relevant class type.  */
13466       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13467                                         DINFO_USAGE_IND_USE);
13468
13469       /* Output a description of the type of the object pointed to.  */
13470       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13471                                         DINFO_USAGE_IND_USE);
13472
13473       /* Now output a DIE to represent this pointer-to-data-member type
13474          itself.  */
13475       gen_ptr_to_mbr_type_die (type, context_die);
13476       break;
13477
13478     case FUNCTION_TYPE:
13479       /* Force out return type (in case it wasn't forced out already).  */
13480       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13481                                         DINFO_USAGE_DIR_USE);
13482       gen_subroutine_type_die (type, context_die);
13483       break;
13484
13485     case METHOD_TYPE:
13486       /* Force out return type (in case it wasn't forced out already).  */
13487       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13488                                         DINFO_USAGE_DIR_USE);
13489       gen_subroutine_type_die (type, context_die);
13490       break;
13491
13492     case ARRAY_TYPE:
13493       gen_array_type_die (type, context_die);
13494       break;
13495
13496     case VECTOR_TYPE:
13497       gen_array_type_die (type, context_die);
13498       break;
13499
13500     case ENUMERAL_TYPE:
13501     case RECORD_TYPE:
13502     case UNION_TYPE:
13503     case QUAL_UNION_TYPE:
13504       /* If this is a nested type whose containing class hasn't been written
13505          out yet, writing it out will cover this one, too.  This does not apply
13506          to instantiations of member class templates; they need to be added to
13507          the containing class as they are generated.  FIXME: This hurts the
13508          idea of combining type decls from multiple TUs, since we can't predict
13509          what set of template instantiations we'll get.  */
13510       if (TYPE_CONTEXT (type)
13511           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13512           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13513         {
13514           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13515
13516           if (TREE_ASM_WRITTEN (type))
13517             return;
13518
13519           /* If that failed, attach ourselves to the stub.  */
13520           push_decl_scope (TYPE_CONTEXT (type));
13521           context_die = lookup_type_die (TYPE_CONTEXT (type));
13522           need_pop = 1;
13523         }
13524       else
13525         {
13526           declare_in_namespace (type, context_die);
13527           need_pop = 0;
13528         }
13529
13530       if (TREE_CODE (type) == ENUMERAL_TYPE)
13531         {
13532           /* This might have been written out by the call to
13533              declare_in_namespace.  */
13534           if (!TREE_ASM_WRITTEN (type))
13535             gen_enumeration_type_die (type, context_die);
13536         }
13537       else
13538         gen_struct_or_union_type_die (type, context_die, usage);
13539
13540       if (need_pop)
13541         pop_decl_scope ();
13542
13543       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13544          it up if it is ever completed.  gen_*_type_die will set it for us
13545          when appropriate.  */
13546       return;
13547
13548     case VOID_TYPE:
13549     case INTEGER_TYPE:
13550     case REAL_TYPE:
13551     case FIXED_POINT_TYPE:
13552     case COMPLEX_TYPE:
13553     case BOOLEAN_TYPE:
13554       /* No DIEs needed for fundamental types.  */
13555       break;
13556
13557     case LANG_TYPE:
13558       /* No Dwarf representation currently defined.  */
13559       break;
13560
13561     default:
13562       gcc_unreachable ();
13563     }
13564
13565   TREE_ASM_WRITTEN (type) = 1;
13566 }
13567
13568 static void
13569 gen_type_die (tree type, dw_die_ref context_die)
13570 {
13571   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13572 }
13573
13574 /* Generate a DIE for a tagged type instantiation.  */
13575
13576 static void
13577 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13578 {
13579   if (type == NULL_TREE || type == error_mark_node)
13580     return;
13581
13582   /* We are going to output a DIE to represent the unqualified version of
13583      this type (i.e. without any const or volatile qualifiers) so make sure
13584      that we have the main variant (i.e. the unqualified version) of this
13585      type now.  */
13586   gcc_assert (type == type_main_variant (type));
13587
13588   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13589      an instance of an unresolved type.  */
13590
13591   switch (TREE_CODE (type))
13592     {
13593     case ERROR_MARK:
13594       break;
13595
13596     case ENUMERAL_TYPE:
13597       gen_inlined_enumeration_type_die (type, context_die);
13598       break;
13599
13600     case RECORD_TYPE:
13601       gen_inlined_structure_type_die (type, context_die);
13602       break;
13603
13604     case UNION_TYPE:
13605     case QUAL_UNION_TYPE:
13606       gen_inlined_union_type_die (type, context_die);
13607       break;
13608
13609     default:
13610       gcc_unreachable ();
13611     }
13612 }
13613
13614 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13615    things which are local to the given block.  */
13616
13617 static void
13618 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13619 {
13620   int must_output_die = 0;
13621   tree origin;
13622   tree decl;
13623   enum tree_code origin_code;
13624
13625   /* Ignore blocks that are NULL.  */
13626   if (stmt == NULL_TREE)
13627     return;
13628
13629   /* If the block is one fragment of a non-contiguous block, do not
13630      process the variables, since they will have been done by the
13631      origin block.  Do process subblocks.  */
13632   if (BLOCK_FRAGMENT_ORIGIN (stmt))
13633     {
13634       tree sub;
13635
13636       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13637         gen_block_die (sub, context_die, depth + 1);
13638
13639       return;
13640     }
13641
13642   /* Determine the "ultimate origin" of this block.  This block may be an
13643      inlined instance of an inlined instance of inline function, so we have
13644      to trace all of the way back through the origin chain to find out what
13645      sort of node actually served as the original seed for the creation of
13646      the current block.  */
13647   origin = block_ultimate_origin (stmt);
13648   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13649
13650   /* Determine if we need to output any Dwarf DIEs at all to represent this
13651      block.  */
13652   if (origin_code == FUNCTION_DECL)
13653     /* The outer scopes for inlinings *must* always be represented.  We
13654        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13655     must_output_die = 1;
13656   else
13657     {
13658       /* In the case where the current block represents an inlining of the
13659          "body block" of an inline function, we must *NOT* output any DIE for
13660          this block because we have already output a DIE to represent the whole
13661          inlined function scope and the "body block" of any function doesn't
13662          really represent a different scope according to ANSI C rules.  So we
13663          check here to make sure that this block does not represent a "body
13664          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13665       if (! is_body_block (origin ? origin : stmt))
13666         {
13667           /* Determine if this block directly contains any "significant"
13668              local declarations which we will need to output DIEs for.  */
13669           if (debug_info_level > DINFO_LEVEL_TERSE)
13670             /* We are not in terse mode so *any* local declaration counts
13671                as being a "significant" one.  */
13672             must_output_die = (BLOCK_VARS (stmt) != NULL
13673                                && (TREE_USED (stmt)
13674                                    || TREE_ASM_WRITTEN (stmt)
13675                                    || BLOCK_ABSTRACT (stmt)));
13676           else
13677             /* We are in terse mode, so only local (nested) function
13678                definitions count as "significant" local declarations.  */
13679             for (decl = BLOCK_VARS (stmt);
13680                  decl != NULL; decl = TREE_CHAIN (decl))
13681               if (TREE_CODE (decl) == FUNCTION_DECL
13682                   && DECL_INITIAL (decl))
13683                 {
13684                   must_output_die = 1;
13685                   break;
13686                 }
13687         }
13688     }
13689
13690   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13691      DIE for any block which contains no significant local declarations at
13692      all.  Rather, in such cases we just call `decls_for_scope' so that any
13693      needed Dwarf info for any sub-blocks will get properly generated. Note
13694      that in terse mode, our definition of what constitutes a "significant"
13695      local declaration gets restricted to include only inlined function
13696      instances and local (nested) function definitions.  */
13697   if (must_output_die)
13698     {
13699       if (origin_code == FUNCTION_DECL)
13700         gen_inlined_subroutine_die (stmt, context_die, depth);
13701       else
13702         gen_lexical_block_die (stmt, context_die, depth);
13703     }
13704   else
13705     decls_for_scope (stmt, context_die, depth);
13706 }
13707
13708 /* Generate all of the decls declared within a given scope and (recursively)
13709    all of its sub-blocks.  */
13710
13711 static void
13712 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13713 {
13714   tree decl;
13715   tree subblocks;
13716
13717   /* Ignore NULL blocks.  */
13718   if (stmt == NULL_TREE)
13719     return;
13720
13721   if (TREE_USED (stmt))
13722     {
13723       /* Output the DIEs to represent all of the data objects and typedefs
13724          declared directly within this block but not within any nested
13725          sub-blocks.  Also, nested function and tag DIEs have been
13726          generated with a parent of NULL; fix that up now.  */
13727       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13728         {
13729           dw_die_ref die;
13730
13731           if (TREE_CODE (decl) == FUNCTION_DECL)
13732             die = lookup_decl_die (decl);
13733           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13734             die = lookup_type_die (TREE_TYPE (decl));
13735           else
13736             die = NULL;
13737
13738           if (die != NULL && die->die_parent == NULL)
13739             add_child_die (context_die, die);
13740           /* Do not produce debug information for static variables since
13741              these might be optimized out.  We are called for these later
13742              in varpool_analyze_pending_decls.
13743
13744              But *do* produce it for Fortran COMMON variables because,
13745              even though they are static, their names can differ depending
13746              on the scope, which we need to preserve.  */
13747           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
13748               && !(is_fortran () && TREE_PUBLIC (decl)))
13749             ;
13750           else
13751             gen_decl_die (decl, context_die);
13752         }
13753     }
13754
13755   /* If we're at -g1, we're not interested in subblocks.  */
13756   if (debug_info_level <= DINFO_LEVEL_TERSE)
13757     return;
13758
13759   /* Output the DIEs to represent all sub-blocks (and the items declared
13760      therein) of this block.  */
13761   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13762        subblocks != NULL;
13763        subblocks = BLOCK_CHAIN (subblocks))
13764     gen_block_die (subblocks, context_die, depth + 1);
13765 }
13766
13767 /* Is this a typedef we can avoid emitting?  */
13768
13769 static inline int
13770 is_redundant_typedef (const_tree decl)
13771 {
13772   if (TYPE_DECL_IS_STUB (decl))
13773     return 1;
13774
13775   if (DECL_ARTIFICIAL (decl)
13776       && DECL_CONTEXT (decl)
13777       && is_tagged_type (DECL_CONTEXT (decl))
13778       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13779       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13780     /* Also ignore the artificial member typedef for the class name.  */
13781     return 1;
13782
13783   return 0;
13784 }
13785
13786 /* Returns the DIE for decl.  A DIE will always be returned.  */
13787
13788 static dw_die_ref
13789 force_decl_die (tree decl)
13790 {
13791   dw_die_ref decl_die;
13792   unsigned saved_external_flag;
13793   tree save_fn = NULL_TREE;
13794   decl_die = lookup_decl_die (decl);
13795   if (!decl_die)
13796     {
13797       dw_die_ref context_die;
13798       tree decl_context = DECL_CONTEXT (decl);
13799       if (decl_context)
13800         {
13801           /* Find die that represents this context.  */
13802           if (TYPE_P (decl_context))
13803             context_die = force_type_die (decl_context);
13804           else
13805             context_die = force_decl_die (decl_context);
13806         }
13807       else
13808         context_die = comp_unit_die;
13809
13810       decl_die = lookup_decl_die (decl);
13811       if (decl_die)
13812         return decl_die;
13813
13814       switch (TREE_CODE (decl))
13815         {
13816         case FUNCTION_DECL:
13817           /* Clear current_function_decl, so that gen_subprogram_die thinks
13818              that this is a declaration. At this point, we just want to force
13819              declaration die.  */
13820           save_fn = current_function_decl;
13821           current_function_decl = NULL_TREE;
13822           gen_subprogram_die (decl, context_die);
13823           current_function_decl = save_fn;
13824           break;
13825
13826         case VAR_DECL:
13827           /* Set external flag to force declaration die. Restore it after
13828            gen_decl_die() call.  */
13829           saved_external_flag = DECL_EXTERNAL (decl);
13830           DECL_EXTERNAL (decl) = 1;
13831           gen_decl_die (decl, context_die);
13832           DECL_EXTERNAL (decl) = saved_external_flag;
13833           break;
13834
13835         case NAMESPACE_DECL:
13836           dwarf2out_decl (decl);
13837           break;
13838
13839         default:
13840           gcc_unreachable ();
13841         }
13842
13843       /* We should be able to find the DIE now.  */
13844       if (!decl_die)
13845         decl_die = lookup_decl_die (decl);
13846       gcc_assert (decl_die);
13847     }
13848
13849   return decl_die;
13850 }
13851
13852 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13853    always returned.  */
13854
13855 static dw_die_ref
13856 force_type_die (tree type)
13857 {
13858   dw_die_ref type_die;
13859
13860   type_die = lookup_type_die (type);
13861   if (!type_die)
13862     {
13863       dw_die_ref context_die;
13864       if (TYPE_CONTEXT (type))
13865         {
13866           if (TYPE_P (TYPE_CONTEXT (type)))
13867             context_die = force_type_die (TYPE_CONTEXT (type));
13868           else
13869             context_die = force_decl_die (TYPE_CONTEXT (type));
13870         }
13871       else
13872         context_die = comp_unit_die;
13873
13874       type_die = modified_type_die (type, TYPE_READONLY (type),
13875                                     TYPE_VOLATILE (type), context_die);
13876       gcc_assert (type_die);
13877     }
13878   return type_die;
13879 }
13880
13881 /* Force out any required namespaces to be able to output DECL,
13882    and return the new context_die for it, if it's changed.  */
13883
13884 static dw_die_ref
13885 setup_namespace_context (tree thing, dw_die_ref context_die)
13886 {
13887   tree context = (DECL_P (thing)
13888                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13889   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13890     /* Force out the namespace.  */
13891     context_die = force_decl_die (context);
13892
13893   return context_die;
13894 }
13895
13896 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13897    type) within its namespace, if appropriate.
13898
13899    For compatibility with older debuggers, namespace DIEs only contain
13900    declarations; all definitions are emitted at CU scope.  */
13901
13902 static void
13903 declare_in_namespace (tree thing, dw_die_ref context_die)
13904 {
13905   dw_die_ref ns_context;
13906
13907   if (debug_info_level <= DINFO_LEVEL_TERSE)
13908     return;
13909
13910   /* If this decl is from an inlined function, then don't try to emit it in its
13911      namespace, as we will get confused.  It would have already been emitted
13912      when the abstract instance of the inline function was emitted anyways.  */
13913   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13914     return;
13915
13916   ns_context = setup_namespace_context (thing, context_die);
13917
13918   if (ns_context != context_die)
13919     {
13920       if (DECL_P (thing))
13921         gen_decl_die (thing, ns_context);
13922       else
13923         gen_type_die (thing, ns_context);
13924     }
13925 }
13926
13927 /* Generate a DIE for a namespace or namespace alias.  */
13928
13929 static void
13930 gen_namespace_die (tree decl)
13931 {
13932   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13933
13934   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13935      they are an alias of.  */
13936   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13937     {
13938       /* Output a real namespace.  */
13939       dw_die_ref namespace_die
13940         = new_die (DW_TAG_namespace, context_die, decl);
13941       add_name_and_src_coords_attributes (namespace_die, decl);
13942       equate_decl_number_to_die (decl, namespace_die);
13943     }
13944   else
13945     {
13946       /* Output a namespace alias.  */
13947
13948       /* Force out the namespace we are an alias of, if necessary.  */
13949       dw_die_ref origin_die
13950         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13951
13952       /* Now create the namespace alias DIE.  */
13953       dw_die_ref namespace_die
13954         = new_die (DW_TAG_imported_declaration, context_die, decl);
13955       add_name_and_src_coords_attributes (namespace_die, decl);
13956       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13957       equate_decl_number_to_die (decl, namespace_die);
13958     }
13959 }
13960
13961 /* Generate Dwarf debug information for a decl described by DECL.  */
13962
13963 static void
13964 gen_decl_die (tree decl, dw_die_ref context_die)
13965 {
13966   tree origin;
13967
13968   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13969     return;
13970
13971   switch (TREE_CODE (decl))
13972     {
13973     case ERROR_MARK:
13974       break;
13975
13976     case CONST_DECL:
13977       /* The individual enumerators of an enum type get output when we output
13978          the Dwarf representation of the relevant enum type itself.  */
13979       break;
13980
13981     case FUNCTION_DECL:
13982       /* Don't output any DIEs to represent mere function declarations,
13983          unless they are class members or explicit block externs.  */
13984       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13985           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13986         break;
13987
13988 #if 0
13989       /* FIXME */
13990       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13991          on local redeclarations of global functions.  That seems broken.  */
13992       if (current_function_decl != decl)
13993         /* This is only a declaration.  */;
13994 #endif
13995
13996       /* If we're emitting a clone, emit info for the abstract instance.  */
13997       if (DECL_ORIGIN (decl) != decl)
13998         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13999
14000       /* If we're emitting an out-of-line copy of an inline function,
14001          emit info for the abstract instance and set up to refer to it.  */
14002       else if (cgraph_function_possibly_inlined_p (decl)
14003                && ! DECL_ABSTRACT (decl)
14004                && ! class_or_namespace_scope_p (context_die)
14005                /* dwarf2out_abstract_function won't emit a die if this is just
14006                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
14007                   that case, because that works only if we have a die.  */
14008                && DECL_INITIAL (decl) != NULL_TREE)
14009         {
14010           dwarf2out_abstract_function (decl);
14011           set_decl_origin_self (decl);
14012         }
14013
14014       /* Otherwise we're emitting the primary DIE for this decl.  */
14015       else if (debug_info_level > DINFO_LEVEL_TERSE)
14016         {
14017           /* Before we describe the FUNCTION_DECL itself, make sure that we
14018              have described its return type.  */
14019           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14020
14021           /* And its virtual context.  */
14022           if (DECL_VINDEX (decl) != NULL_TREE)
14023             gen_type_die (DECL_CONTEXT (decl), context_die);
14024
14025           /* And its containing type.  */
14026           origin = decl_class_context (decl);
14027           if (origin != NULL_TREE)
14028             gen_type_die_for_member (origin, decl, context_die);
14029
14030           /* And its containing namespace.  */
14031           declare_in_namespace (decl, context_die);
14032         }
14033
14034       /* Now output a DIE to represent the function itself.  */
14035       gen_subprogram_die (decl, context_die);
14036       break;
14037
14038     case TYPE_DECL:
14039       /* If we are in terse mode, don't generate any DIEs to represent any
14040          actual typedefs.  */
14041       if (debug_info_level <= DINFO_LEVEL_TERSE)
14042         break;
14043
14044       /* In the special case of a TYPE_DECL node representing the declaration
14045          of some type tag, if the given TYPE_DECL is marked as having been
14046          instantiated from some other (original) TYPE_DECL node (e.g. one which
14047          was generated within the original definition of an inline function) we
14048          have to generate a special (abbreviated) DW_TAG_structure_type,
14049          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
14050       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14051           && is_tagged_type (TREE_TYPE (decl)))
14052         {
14053           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14054           break;
14055         }
14056
14057       if (is_redundant_typedef (decl))
14058         gen_type_die (TREE_TYPE (decl), context_die);
14059       else
14060         /* Output a DIE to represent the typedef itself.  */
14061         gen_typedef_die (decl, context_die);
14062       break;
14063
14064     case LABEL_DECL:
14065       if (debug_info_level >= DINFO_LEVEL_NORMAL)
14066         gen_label_die (decl, context_die);
14067       break;
14068
14069     case VAR_DECL:
14070     case RESULT_DECL:
14071       /* If we are in terse mode, don't generate any DIEs to represent any
14072          variable declarations or definitions.  */
14073       if (debug_info_level <= DINFO_LEVEL_TERSE)
14074         break;
14075
14076       /* If this is the global definition of the Fortran COMMON block, we don't
14077          need to do anything.  Syntactically, the block itself has no identity,
14078          just its constituent identifiers.  */
14079       if (TREE_CODE (decl) == VAR_DECL
14080           && TREE_PUBLIC (decl)
14081           && TREE_STATIC (decl)
14082           && is_fortran ()
14083           && !DECL_HAS_VALUE_EXPR_P (decl))
14084         break;
14085
14086       /* Output any DIEs that are needed to specify the type of this data
14087          object.  */
14088       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14089         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14090       else
14091         gen_type_die (TREE_TYPE (decl), context_die);
14092
14093       /* And its containing type.  */
14094       origin = decl_class_context (decl);
14095       if (origin != NULL_TREE)
14096         gen_type_die_for_member (origin, decl, context_die);
14097
14098       /* And its containing namespace.  */
14099       declare_in_namespace (decl, context_die);
14100
14101       /* Now output the DIE to represent the data object itself.  This gets
14102          complicated because of the possibility that the VAR_DECL really
14103          represents an inlined instance of a formal parameter for an inline
14104          function.  */
14105       origin = decl_ultimate_origin (decl);
14106       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14107         gen_formal_parameter_die (decl, context_die);
14108       else
14109         gen_variable_die (decl, context_die);
14110       break;
14111
14112     case FIELD_DECL:
14113       /* Ignore the nameless fields that are used to skip bits but handle C++
14114          anonymous unions and structs.  */
14115       if (DECL_NAME (decl) != NULL_TREE
14116           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14117           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14118         {
14119           gen_type_die (member_declared_type (decl), context_die);
14120           gen_field_die (decl, context_die);
14121         }
14122       break;
14123
14124     case PARM_DECL:
14125       if (DECL_BY_REFERENCE (decl))
14126         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14127       else
14128         gen_type_die (TREE_TYPE (decl), context_die);
14129       gen_formal_parameter_die (decl, context_die);
14130       break;
14131
14132     case NAMESPACE_DECL:
14133       gen_namespace_die (decl);
14134       break;
14135
14136     default:
14137       /* Probably some frontend-internal decl.  Assume we don't care.  */
14138       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14139       break;
14140     }
14141 }
14142 \f
14143 /* Output debug information for global decl DECL.  Called from toplev.c after
14144    compilation proper has finished.  */
14145
14146 static void
14147 dwarf2out_global_decl (tree decl)
14148 {
14149   /* Output DWARF2 information for file-scope tentative data object
14150      declarations, file-scope (extern) function declarations (which had no
14151      corresponding body) and file-scope tagged type declarations and
14152      definitions which have not yet been forced out.
14153
14154      Ignore the global decl of any Fortran COMMON blocks which also wind up here
14155      though they have already been described in the local scope for the 
14156      procedures using them.  */
14157   if (TREE_CODE (decl) == VAR_DECL
14158       && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14159     return;
14160
14161   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14162     dwarf2out_decl (decl);
14163 }
14164
14165 /* Output debug information for type decl DECL.  Called from toplev.c
14166    and from language front ends (to record built-in types).  */
14167 static void
14168 dwarf2out_type_decl (tree decl, int local)
14169 {
14170   if (!local)
14171     dwarf2out_decl (decl);
14172 }
14173
14174 /* Output debug information for imported module or decl.  */
14175
14176 static void
14177 dwarf2out_imported_module_or_decl (tree decl, tree context)
14178 {
14179   dw_die_ref imported_die, at_import_die;
14180   dw_die_ref scope_die;
14181   expanded_location xloc;
14182
14183   if (debug_info_level <= DINFO_LEVEL_TERSE)
14184     return;
14185
14186   gcc_assert (decl);
14187
14188   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14189      We need decl DIE for reference and scope die. First, get DIE for the decl
14190      itself.  */
14191
14192   /* Get the scope die for decl context. Use comp_unit_die for global module
14193      or decl. If die is not found for non globals, force new die.  */
14194   if (!context)
14195     scope_die = comp_unit_die;
14196   else if (TYPE_P (context))
14197     {
14198       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14199         return;
14200     scope_die = force_type_die (context);
14201     }
14202   else
14203     scope_die = force_decl_die (context);
14204
14205   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14206   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14207     {
14208       if (is_base_type (TREE_TYPE (decl)))
14209         at_import_die = base_type_die (TREE_TYPE (decl));
14210       else
14211         at_import_die = force_type_die (TREE_TYPE (decl));
14212     }
14213   else
14214     {
14215       at_import_die = lookup_decl_die (decl);
14216       if (!at_import_die)
14217         {
14218           /* If we're trying to avoid duplicate debug info, we may not have
14219              emitted the member decl for this field.  Emit it now.  */
14220           if (TREE_CODE (decl) == FIELD_DECL)
14221             {
14222               tree type = DECL_CONTEXT (decl);
14223               dw_die_ref type_context_die;
14224
14225               if (TYPE_CONTEXT (type))
14226                 if (TYPE_P (TYPE_CONTEXT (type)))
14227                   {
14228                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14229                                                    DINFO_USAGE_DIR_USE))
14230                       return;
14231                   type_context_die = force_type_die (TYPE_CONTEXT (type));
14232                   }
14233               else
14234                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14235               else
14236                 type_context_die = comp_unit_die;
14237               gen_type_die_for_member (type, decl, type_context_die);
14238             }
14239           at_import_die = force_decl_die (decl);
14240         }
14241     }
14242
14243   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14244   if (TREE_CODE (decl) == NAMESPACE_DECL)
14245     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14246   else
14247     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14248
14249   xloc = expand_location (input_location);
14250   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14251   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14252   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14253 }
14254
14255 /* Write the debugging output for DECL.  */
14256
14257 void
14258 dwarf2out_decl (tree decl)
14259 {
14260   dw_die_ref context_die = comp_unit_die;
14261
14262   switch (TREE_CODE (decl))
14263     {
14264     case ERROR_MARK:
14265       return;
14266
14267     case FUNCTION_DECL:
14268       /* What we would really like to do here is to filter out all mere
14269          file-scope declarations of file-scope functions which are never
14270          referenced later within this translation unit (and keep all of ones
14271          that *are* referenced later on) but we aren't clairvoyant, so we have
14272          no idea which functions will be referenced in the future (i.e. later
14273          on within the current translation unit). So here we just ignore all
14274          file-scope function declarations which are not also definitions.  If
14275          and when the debugger needs to know something about these functions,
14276          it will have to hunt around and find the DWARF information associated
14277          with the definition of the function.
14278
14279          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14280          nodes represent definitions and which ones represent mere
14281          declarations.  We have to check DECL_INITIAL instead. That's because
14282          the C front-end supports some weird semantics for "extern inline"
14283          function definitions.  These can get inlined within the current
14284          translation unit (and thus, we need to generate Dwarf info for their
14285          abstract instances so that the Dwarf info for the concrete inlined
14286          instances can have something to refer to) but the compiler never
14287          generates any out-of-lines instances of such things (despite the fact
14288          that they *are* definitions).
14289
14290          The important point is that the C front-end marks these "extern
14291          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14292          them anyway. Note that the C++ front-end also plays some similar games
14293          for inline function definitions appearing within include files which
14294          also contain `#pragma interface' pragmas.  */
14295       if (DECL_INITIAL (decl) == NULL_TREE)
14296         return;
14297
14298       /* If we're a nested function, initially use a parent of NULL; if we're
14299          a plain function, this will be fixed up in decls_for_scope.  If
14300          we're a method, it will be ignored, since we already have a DIE.  */
14301       if (decl_function_context (decl)
14302           /* But if we're in terse mode, we don't care about scope.  */
14303           && debug_info_level > DINFO_LEVEL_TERSE)
14304         context_die = NULL;
14305       break;
14306
14307     case VAR_DECL:
14308       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14309          declaration and if the declaration was never even referenced from
14310          within this entire compilation unit.  We suppress these DIEs in
14311          order to save space in the .debug section (by eliminating entries
14312          which are probably useless).  Note that we must not suppress
14313          block-local extern declarations (whether used or not) because that
14314          would screw-up the debugger's name lookup mechanism and cause it to
14315          miss things which really ought to be in scope at a given point.  */
14316       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14317         return;
14318
14319       /* For local statics lookup proper context die.  */
14320       if (TREE_STATIC (decl) && decl_function_context (decl))
14321         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14322
14323       /* If we are in terse mode, don't generate any DIEs to represent any
14324          variable declarations or definitions.  */
14325       if (debug_info_level <= DINFO_LEVEL_TERSE)
14326         return;
14327       break;
14328
14329     case NAMESPACE_DECL:
14330       if (debug_info_level <= DINFO_LEVEL_TERSE)
14331         return;
14332       if (lookup_decl_die (decl) != NULL)
14333         return;
14334       break;
14335
14336     case TYPE_DECL:
14337       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14338       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14339         return;
14340
14341       /* Don't bother trying to generate any DIEs to represent any of the
14342          normal built-in types for the language we are compiling.  */
14343       if (DECL_IS_BUILTIN (decl))
14344         {
14345           /* OK, we need to generate one for `bool' so GDB knows what type
14346              comparisons have.  */
14347           if (is_cxx ()
14348               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14349               && ! DECL_IGNORED_P (decl))
14350             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14351
14352           return;
14353         }
14354
14355       /* If we are in terse mode, don't generate any DIEs for types.  */
14356       if (debug_info_level <= DINFO_LEVEL_TERSE)
14357         return;
14358
14359       /* If we're a function-scope tag, initially use a parent of NULL;
14360          this will be fixed up in decls_for_scope.  */
14361       if (decl_function_context (decl))
14362         context_die = NULL;
14363
14364       break;
14365
14366     default:
14367       return;
14368     }
14369
14370   gen_decl_die (decl, context_die);
14371 }
14372
14373 /* Output a marker (i.e. a label) for the beginning of the generated code for
14374    a lexical block.  */
14375
14376 static void
14377 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14378                        unsigned int blocknum)
14379 {
14380   switch_to_section (current_function_section ());
14381   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14382 }
14383
14384 /* Output a marker (i.e. a label) for the end of the generated code for a
14385    lexical block.  */
14386
14387 static void
14388 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14389 {
14390   switch_to_section (current_function_section ());
14391   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14392 }
14393
14394 /* Returns nonzero if it is appropriate not to emit any debugging
14395    information for BLOCK, because it doesn't contain any instructions.
14396
14397    Don't allow this for blocks with nested functions or local classes
14398    as we would end up with orphans, and in the presence of scheduling
14399    we may end up calling them anyway.  */
14400
14401 static bool
14402 dwarf2out_ignore_block (const_tree block)
14403 {
14404   tree decl;
14405
14406   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14407     if (TREE_CODE (decl) == FUNCTION_DECL
14408         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14409       return 0;
14410
14411   return 1;
14412 }
14413
14414 /* Hash table routines for file_hash.  */
14415
14416 static int
14417 file_table_eq (const void *p1_p, const void *p2_p)
14418 {
14419   const struct dwarf_file_data * p1 = p1_p;
14420   const char * p2 = p2_p;
14421   return strcmp (p1->filename, p2) == 0;
14422 }
14423
14424 static hashval_t
14425 file_table_hash (const void *p_p)
14426 {
14427   const struct dwarf_file_data * p = p_p;
14428   return htab_hash_string (p->filename);
14429 }
14430
14431 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14432    dwarf2out.c) and return its "index".  The index of each (known) filename is
14433    just a unique number which is associated with only that one filename.  We
14434    need such numbers for the sake of generating labels (in the .debug_sfnames
14435    section) and references to those files numbers (in the .debug_srcinfo
14436    and.debug_macinfo sections).  If the filename given as an argument is not
14437    found in our current list, add it to the list and assign it the next
14438    available unique index number.  In order to speed up searches, we remember
14439    the index of the filename was looked up last.  This handles the majority of
14440    all searches.  */
14441
14442 static struct dwarf_file_data *
14443 lookup_filename (const char *file_name)
14444 {
14445   void ** slot;
14446   struct dwarf_file_data * created;
14447
14448   /* Check to see if the file name that was searched on the previous
14449      call matches this file name.  If so, return the index.  */
14450   if (file_table_last_lookup
14451       && (file_name == file_table_last_lookup->filename
14452           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14453     return file_table_last_lookup;
14454
14455   /* Didn't match the previous lookup, search the table.  */
14456   slot = htab_find_slot_with_hash (file_table, file_name,
14457                                    htab_hash_string (file_name), INSERT);
14458   if (*slot)
14459     return *slot;
14460
14461   created = ggc_alloc (sizeof (struct dwarf_file_data));
14462   created->filename = file_name;
14463   created->emitted_number = 0;
14464   *slot = created;
14465   return created;
14466 }
14467
14468 /* If the assembler will construct the file table, then translate the compiler
14469    internal file table number into the assembler file table number, and emit
14470    a .file directive if we haven't already emitted one yet.  The file table
14471    numbers are different because we prune debug info for unused variables and
14472    types, which may include filenames.  */
14473
14474 static int
14475 maybe_emit_file (struct dwarf_file_data * fd)
14476 {
14477   if (! fd->emitted_number)
14478     {
14479       if (last_emitted_file)
14480         fd->emitted_number = last_emitted_file->emitted_number + 1;
14481       else
14482         fd->emitted_number = 1;
14483       last_emitted_file = fd;
14484
14485       if (DWARF2_ASM_LINE_DEBUG_INFO)
14486         {
14487           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14488           output_quoted_string (asm_out_file,
14489                                 remap_debug_filename (fd->filename));
14490           fputc ('\n', asm_out_file);
14491         }
14492     }
14493
14494   return fd->emitted_number;
14495 }
14496
14497 /* Called by the final INSN scan whenever we see a var location.  We
14498    use it to drop labels in the right places, and throw the location in
14499    our lookup table.  */
14500
14501 static void
14502 dwarf2out_var_location (rtx loc_note)
14503 {
14504   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14505   struct var_loc_node *newloc;
14506   rtx prev_insn;
14507   static rtx last_insn;
14508   static const char *last_label;
14509   tree decl;
14510
14511   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14512     return;
14513   prev_insn = PREV_INSN (loc_note);
14514
14515   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14516   /* If the insn we processed last time is the previous insn
14517      and it is also a var location note, use the label we emitted
14518      last time.  */
14519   if (last_insn != NULL_RTX
14520       && last_insn == prev_insn
14521       && NOTE_P (prev_insn)
14522       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14523     {
14524       newloc->label = last_label;
14525     }
14526   else
14527     {
14528       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14529       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14530       loclabel_num++;
14531       newloc->label = ggc_strdup (loclabel);
14532     }
14533   newloc->var_loc_note = loc_note;
14534   newloc->next = NULL;
14535
14536   if (cfun && in_cold_section_p)
14537     newloc->section_label = crtl->subsections.cold_section_label;
14538   else
14539     newloc->section_label = text_section_label;
14540
14541   last_insn = loc_note;
14542   last_label = newloc->label;
14543   decl = NOTE_VAR_LOCATION_DECL (loc_note);
14544   add_var_loc_to_decl (decl, newloc);
14545 }
14546
14547 /* We need to reset the locations at the beginning of each
14548    function. We can't do this in the end_function hook, because the
14549    declarations that use the locations won't have been output when
14550    that hook is called.  Also compute have_multiple_function_sections here.  */
14551
14552 static void
14553 dwarf2out_begin_function (tree fun)
14554 {
14555   htab_empty (decl_loc_table);
14556
14557   if (function_section (fun) != text_section)
14558     have_multiple_function_sections = true;
14559
14560   dwarf2out_note_section_used ();
14561 }
14562
14563 /* Output a label to mark the beginning of a source code line entry
14564    and record information relating to this source line, in
14565    'line_info_table' for later output of the .debug_line section.  */
14566
14567 static void
14568 dwarf2out_source_line (unsigned int line, const char *filename)
14569 {
14570   if (debug_info_level >= DINFO_LEVEL_NORMAL
14571       && line != 0)
14572     {
14573       int file_num = maybe_emit_file (lookup_filename (filename));
14574
14575       switch_to_section (current_function_section ());
14576
14577       /* If requested, emit something human-readable.  */
14578       if (flag_debug_asm)
14579         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14580                  filename, line);
14581
14582       if (DWARF2_ASM_LINE_DEBUG_INFO)
14583         {
14584           /* Emit the .loc directive understood by GNU as.  */
14585           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14586
14587           /* Indicate that line number info exists.  */
14588           line_info_table_in_use++;
14589         }
14590       else if (function_section (current_function_decl) != text_section)
14591         {
14592           dw_separate_line_info_ref line_info;
14593           targetm.asm_out.internal_label (asm_out_file,
14594                                           SEPARATE_LINE_CODE_LABEL,
14595                                           separate_line_info_table_in_use);
14596
14597           /* Expand the line info table if necessary.  */
14598           if (separate_line_info_table_in_use
14599               == separate_line_info_table_allocated)
14600             {
14601               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14602               separate_line_info_table
14603                 = ggc_realloc (separate_line_info_table,
14604                                separate_line_info_table_allocated
14605                                * sizeof (dw_separate_line_info_entry));
14606               memset (separate_line_info_table
14607                        + separate_line_info_table_in_use,
14608                       0,
14609                       (LINE_INFO_TABLE_INCREMENT
14610                        * sizeof (dw_separate_line_info_entry)));
14611             }
14612
14613           /* Add the new entry at the end of the line_info_table.  */
14614           line_info
14615             = &separate_line_info_table[separate_line_info_table_in_use++];
14616           line_info->dw_file_num = file_num;
14617           line_info->dw_line_num = line;
14618           line_info->function = current_function_funcdef_no;
14619         }
14620       else
14621         {
14622           dw_line_info_ref line_info;
14623
14624           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14625                                      line_info_table_in_use);
14626
14627           /* Expand the line info table if necessary.  */
14628           if (line_info_table_in_use == line_info_table_allocated)
14629             {
14630               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14631               line_info_table
14632                 = ggc_realloc (line_info_table,
14633                                (line_info_table_allocated
14634                                 * sizeof (dw_line_info_entry)));
14635               memset (line_info_table + line_info_table_in_use, 0,
14636                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14637             }
14638
14639           /* Add the new entry at the end of the line_info_table.  */
14640           line_info = &line_info_table[line_info_table_in_use++];
14641           line_info->dw_file_num = file_num;
14642           line_info->dw_line_num = line;
14643         }
14644     }
14645 }
14646
14647 /* Record the beginning of a new source file.  */
14648
14649 static void
14650 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14651 {
14652   if (flag_eliminate_dwarf2_dups)
14653     {
14654       /* Record the beginning of the file for break_out_includes.  */
14655       dw_die_ref bincl_die;
14656
14657       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14658       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14659     }
14660
14661   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14662     {
14663       int file_num = maybe_emit_file (lookup_filename (filename));
14664
14665       switch_to_section (debug_macinfo_section);
14666       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14667       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14668                                    lineno);
14669
14670       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14671     }
14672 }
14673
14674 /* Record the end of a source file.  */
14675
14676 static void
14677 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14678 {
14679   if (flag_eliminate_dwarf2_dups)
14680     /* Record the end of the file for break_out_includes.  */
14681     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14682
14683   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14684     {
14685       switch_to_section (debug_macinfo_section);
14686       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14687     }
14688 }
14689
14690 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14691    the tail part of the directive line, i.e. the part which is past the
14692    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14693
14694 static void
14695 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14696                   const char *buffer ATTRIBUTE_UNUSED)
14697 {
14698   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14699     {
14700       switch_to_section (debug_macinfo_section);
14701       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14702       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14703       dw2_asm_output_nstring (buffer, -1, "The macro");
14704     }
14705 }
14706
14707 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14708    the tail part of the directive line, i.e. the part which is past the
14709    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14710
14711 static void
14712 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14713                  const char *buffer ATTRIBUTE_UNUSED)
14714 {
14715   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14716     {
14717       switch_to_section (debug_macinfo_section);
14718       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14719       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14720       dw2_asm_output_nstring (buffer, -1, "The macro");
14721     }
14722 }
14723
14724 /* Set up for Dwarf output at the start of compilation.  */
14725
14726 static void
14727 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14728 {
14729   /* Allocate the file_table.  */
14730   file_table = htab_create_ggc (50, file_table_hash,
14731                                 file_table_eq, NULL);
14732
14733   /* Allocate the decl_die_table.  */
14734   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14735                                     decl_die_table_eq, NULL);
14736
14737   /* Allocate the decl_loc_table.  */
14738   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14739                                     decl_loc_table_eq, NULL);
14740
14741   /* Allocate the initial hunk of the decl_scope_table.  */
14742   decl_scope_table = VEC_alloc (tree, gc, 256);
14743
14744   /* Allocate the initial hunk of the abbrev_die_table.  */
14745   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14746                                         * sizeof (dw_die_ref));
14747   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14748   /* Zero-th entry is allocated, but unused.  */
14749   abbrev_die_table_in_use = 1;
14750
14751   /* Allocate the initial hunk of the line_info_table.  */
14752   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14753                                        * sizeof (dw_line_info_entry));
14754   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14755
14756   /* Zero-th entry is allocated, but unused.  */
14757   line_info_table_in_use = 1;
14758
14759   /* Allocate the pubtypes and pubnames vectors.  */
14760   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14761   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14762
14763   /* Generate the initial DIE for the .debug section.  Note that the (string)
14764      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14765      will (typically) be a relative pathname and that this pathname should be
14766      taken as being relative to the directory from which the compiler was
14767      invoked when the given (base) source file was compiled.  We will fill
14768      in this value in dwarf2out_finish.  */
14769   comp_unit_die = gen_compile_unit_die (NULL);
14770
14771   incomplete_types = VEC_alloc (tree, gc, 64);
14772
14773   used_rtx_array = VEC_alloc (rtx, gc, 32);
14774
14775   debug_info_section = get_section (DEBUG_INFO_SECTION,
14776                                     SECTION_DEBUG, NULL);
14777   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14778                                       SECTION_DEBUG, NULL);
14779   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14780                                        SECTION_DEBUG, NULL);
14781   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14782                                        SECTION_DEBUG, NULL);
14783   debug_line_section = get_section (DEBUG_LINE_SECTION,
14784                                     SECTION_DEBUG, NULL);
14785   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14786                                    SECTION_DEBUG, NULL);
14787   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14788                                         SECTION_DEBUG, NULL);
14789 #ifdef DEBUG_PUBTYPES_SECTION
14790   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14791                                         SECTION_DEBUG, NULL);
14792 #endif
14793   debug_str_section = get_section (DEBUG_STR_SECTION,
14794                                    DEBUG_STR_SECTION_FLAGS, NULL);
14795   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14796                                       SECTION_DEBUG, NULL);
14797   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14798                                      SECTION_DEBUG, NULL);
14799
14800   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14801   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14802                                DEBUG_ABBREV_SECTION_LABEL, 0);
14803   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14804   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14805                                COLD_TEXT_SECTION_LABEL, 0);
14806   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14807
14808   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14809                                DEBUG_INFO_SECTION_LABEL, 0);
14810   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14811                                DEBUG_LINE_SECTION_LABEL, 0);
14812   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14813                                DEBUG_RANGES_SECTION_LABEL, 0);
14814   switch_to_section (debug_abbrev_section);
14815   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14816   switch_to_section (debug_info_section);
14817   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14818   switch_to_section (debug_line_section);
14819   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14820
14821   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14822     {
14823       switch_to_section (debug_macinfo_section);
14824       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14825                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14826       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14827     }
14828
14829   switch_to_section (text_section);
14830   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14831   if (flag_reorder_blocks_and_partition)
14832     {
14833       cold_text_section = unlikely_text_section ();
14834       switch_to_section (cold_text_section);
14835       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14836     }
14837 }
14838
14839 /* A helper function for dwarf2out_finish called through
14840    ht_forall.  Emit one queued .debug_str string.  */
14841
14842 static int
14843 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14844 {
14845   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14846
14847   if (node->form == DW_FORM_strp)
14848     {
14849       switch_to_section (debug_str_section);
14850       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14851       assemble_string (node->str, strlen (node->str) + 1);
14852     }
14853
14854   return 1;
14855 }
14856
14857 #if ENABLE_ASSERT_CHECKING
14858 /* Verify that all marks are clear.  */
14859
14860 static void
14861 verify_marks_clear (dw_die_ref die)
14862 {
14863   dw_die_ref c;
14864
14865   gcc_assert (! die->die_mark);
14866   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14867 }
14868 #endif /* ENABLE_ASSERT_CHECKING */
14869
14870 /* Clear the marks for a die and its children.
14871    Be cool if the mark isn't set.  */
14872
14873 static void
14874 prune_unmark_dies (dw_die_ref die)
14875 {
14876   dw_die_ref c;
14877
14878   if (die->die_mark)
14879     die->die_mark = 0;
14880   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14881 }
14882
14883 /* Given DIE that we're marking as used, find any other dies
14884    it references as attributes and mark them as used.  */
14885
14886 static void
14887 prune_unused_types_walk_attribs (dw_die_ref die)
14888 {
14889   dw_attr_ref a;
14890   unsigned ix;
14891
14892   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14893     {
14894       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14895         {
14896           /* A reference to another DIE.
14897              Make sure that it will get emitted.  */
14898           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14899         }
14900       /* Set the string's refcount to 0 so that prune_unused_types_mark
14901          accounts properly for it.  */
14902       if (AT_class (a) == dw_val_class_str)
14903         a->dw_attr_val.v.val_str->refcount = 0;
14904     }
14905 }
14906
14907
14908 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14909    to DIE's children.  */
14910
14911 static void
14912 prune_unused_types_mark (dw_die_ref die, int dokids)
14913 {
14914   dw_die_ref c;
14915
14916   if (die->die_mark == 0)
14917     {
14918       /* We haven't done this node yet.  Mark it as used.  */
14919       die->die_mark = 1;
14920
14921       /* We also have to mark its parents as used.
14922          (But we don't want to mark our parents' kids due to this.)  */
14923       if (die->die_parent)
14924         prune_unused_types_mark (die->die_parent, 0);
14925
14926       /* Mark any referenced nodes.  */
14927       prune_unused_types_walk_attribs (die);
14928
14929       /* If this node is a specification,
14930          also mark the definition, if it exists.  */
14931       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14932         prune_unused_types_mark (die->die_definition, 1);
14933     }
14934
14935   if (dokids && die->die_mark != 2)
14936     {
14937       /* We need to walk the children, but haven't done so yet.
14938          Remember that we've walked the kids.  */
14939       die->die_mark = 2;
14940
14941       /* If this is an array type, we need to make sure our
14942          kids get marked, even if they're types.  */
14943       if (die->die_tag == DW_TAG_array_type)
14944         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14945       else
14946         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14947     }
14948 }
14949
14950
14951 /* Walk the tree DIE and mark types that we actually use.  */
14952
14953 static void
14954 prune_unused_types_walk (dw_die_ref die)
14955 {
14956   dw_die_ref c;
14957
14958   /* Don't do anything if this node is already marked.  */
14959   if (die->die_mark)
14960     return;
14961
14962   switch (die->die_tag)
14963     {
14964     case DW_TAG_const_type:
14965     case DW_TAG_packed_type:
14966     case DW_TAG_pointer_type:
14967     case DW_TAG_reference_type:
14968     case DW_TAG_volatile_type:
14969     case DW_TAG_typedef:
14970     case DW_TAG_array_type:
14971     case DW_TAG_structure_type:
14972     case DW_TAG_union_type:
14973     case DW_TAG_class_type:
14974     case DW_TAG_interface_type:
14975     case DW_TAG_friend:
14976     case DW_TAG_variant_part:
14977     case DW_TAG_enumeration_type:
14978     case DW_TAG_subroutine_type:
14979     case DW_TAG_string_type:
14980     case DW_TAG_set_type:
14981     case DW_TAG_subrange_type:
14982     case DW_TAG_ptr_to_member_type:
14983     case DW_TAG_file_type:
14984       if (die->die_perennial_p)
14985         break;
14986
14987       /* It's a type node --- don't mark it.  */
14988       return;
14989
14990     default:
14991       /* Mark everything else.  */
14992       break;
14993   }
14994
14995   die->die_mark = 1;
14996
14997   /* Now, mark any dies referenced from here.  */
14998   prune_unused_types_walk_attribs (die);
14999
15000   /* Mark children.  */
15001   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15002 }
15003
15004 /* Increment the string counts on strings referred to from DIE's
15005    attributes.  */
15006
15007 static void
15008 prune_unused_types_update_strings (dw_die_ref die)
15009 {
15010   dw_attr_ref a;
15011   unsigned ix;
15012
15013   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15014     if (AT_class (a) == dw_val_class_str)
15015       {
15016         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15017         s->refcount++;
15018         /* Avoid unnecessarily putting strings that are used less than
15019            twice in the hash table.  */
15020         if (s->refcount
15021             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15022           {
15023             void ** slot;
15024             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15025                                              htab_hash_string (s->str),
15026                                              INSERT);
15027             gcc_assert (*slot == NULL);
15028             *slot = s;
15029           }
15030       }
15031 }
15032
15033 /* Remove from the tree DIE any dies that aren't marked.  */
15034
15035 static void
15036 prune_unused_types_prune (dw_die_ref die)
15037 {
15038   dw_die_ref c;
15039
15040   gcc_assert (die->die_mark);
15041   prune_unused_types_update_strings (die);
15042
15043   if (! die->die_child)
15044     return;
15045
15046   c = die->die_child;
15047   do {
15048     dw_die_ref prev = c;
15049     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15050       if (c == die->die_child)
15051         {
15052           /* No marked children between 'prev' and the end of the list.  */
15053           if (prev == c)
15054             /* No marked children at all.  */
15055             die->die_child = NULL;
15056           else
15057             {
15058               prev->die_sib = c->die_sib;
15059               die->die_child = prev;
15060             }
15061           return;
15062         }
15063
15064     if (c != prev->die_sib)
15065       prev->die_sib = c;
15066     prune_unused_types_prune (c);
15067   } while (c != die->die_child);
15068 }
15069
15070
15071 /* Remove dies representing declarations that we never use.  */
15072
15073 static void
15074 prune_unused_types (void)
15075 {
15076   unsigned int i;
15077   limbo_die_node *node;
15078   pubname_ref pub;
15079
15080 #if ENABLE_ASSERT_CHECKING
15081   /* All the marks should already be clear.  */
15082   verify_marks_clear (comp_unit_die);
15083   for (node = limbo_die_list; node; node = node->next)
15084     verify_marks_clear (node->die);
15085 #endif /* ENABLE_ASSERT_CHECKING */
15086
15087   /* Set the mark on nodes that are actually used.  */
15088   prune_unused_types_walk (comp_unit_die);
15089   for (node = limbo_die_list; node; node = node->next)
15090     prune_unused_types_walk (node->die);
15091
15092   /* Also set the mark on nodes referenced from the
15093      pubname_table or arange_table.  */
15094   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15095     prune_unused_types_mark (pub->die, 1);
15096   for (i = 0; i < arange_table_in_use; i++)
15097     prune_unused_types_mark (arange_table[i], 1);
15098
15099   /* Get rid of nodes that aren't marked; and update the string counts.  */
15100   if (debug_str_hash)
15101     htab_empty (debug_str_hash);
15102   prune_unused_types_prune (comp_unit_die);
15103   for (node = limbo_die_list; node; node = node->next)
15104     prune_unused_types_prune (node->die);
15105
15106   /* Leave the marks clear.  */
15107   prune_unmark_dies (comp_unit_die);
15108   for (node = limbo_die_list; node; node = node->next)
15109     prune_unmark_dies (node->die);
15110 }
15111
15112 /* Set the parameter to true if there are any relative pathnames in
15113    the file table.  */
15114 static int
15115 file_table_relative_p (void ** slot, void *param)
15116 {
15117   bool *p = param;
15118   struct dwarf_file_data *d = *slot;
15119   if (!IS_ABSOLUTE_PATH (d->filename))
15120     {
15121       *p = true;
15122       return 0;
15123     }
15124   return 1;
15125 }
15126
15127 /* Output stuff that dwarf requires at the end of every file,
15128    and generate the DWARF-2 debugging info.  */
15129
15130 static void
15131 dwarf2out_finish (const char *filename)
15132 {
15133   limbo_die_node *node, *next_node;
15134   dw_die_ref die = 0;
15135
15136   /* Add the name for the main input file now.  We delayed this from
15137      dwarf2out_init to avoid complications with PCH.  */
15138   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15139   if (!IS_ABSOLUTE_PATH (filename))
15140     add_comp_dir_attribute (comp_unit_die);
15141   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15142     {
15143       bool p = false;
15144       htab_traverse (file_table, file_table_relative_p, &p);
15145       if (p)
15146         add_comp_dir_attribute (comp_unit_die);
15147     }
15148
15149   /* Traverse the limbo die list, and add parent/child links.  The only
15150      dies without parents that should be here are concrete instances of
15151      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15152      For concrete instances, we can get the parent die from the abstract
15153      instance.  */
15154   for (node = limbo_die_list; node; node = next_node)
15155     {
15156       next_node = node->next;
15157       die = node->die;
15158
15159       if (die->die_parent == NULL)
15160         {
15161           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15162
15163           if (origin)
15164             add_child_die (origin->die_parent, die);
15165           else if (die == comp_unit_die)
15166             ;
15167           else if (errorcount > 0 || sorrycount > 0)
15168             /* It's OK to be confused by errors in the input.  */
15169             add_child_die (comp_unit_die, die);
15170           else
15171             {
15172               /* In certain situations, the lexical block containing a
15173                  nested function can be optimized away, which results
15174                  in the nested function die being orphaned.  Likewise
15175                  with the return type of that nested function.  Force
15176                  this to be a child of the containing function.
15177
15178                  It may happen that even the containing function got fully
15179                  inlined and optimized out.  In that case we are lost and
15180                  assign the empty child.  This should not be big issue as
15181                  the function is likely unreachable too.  */
15182               tree context = NULL_TREE;
15183
15184               gcc_assert (node->created_for);
15185
15186               if (DECL_P (node->created_for))
15187                 context = DECL_CONTEXT (node->created_for);
15188               else if (TYPE_P (node->created_for))
15189                 context = TYPE_CONTEXT (node->created_for);
15190
15191               gcc_assert (context
15192                           && (TREE_CODE (context) == FUNCTION_DECL
15193                               || TREE_CODE (context) == NAMESPACE_DECL));
15194
15195               origin = lookup_decl_die (context);
15196               if (origin)
15197                 add_child_die (origin, die);
15198               else
15199                 add_child_die (comp_unit_die, die);
15200             }
15201         }
15202     }
15203
15204   limbo_die_list = NULL;
15205
15206   /* Walk through the list of incomplete types again, trying once more to
15207      emit full debugging info for them.  */
15208   retry_incomplete_types ();
15209
15210   if (flag_eliminate_unused_debug_types)
15211     prune_unused_types ();
15212
15213   /* Generate separate CUs for each of the include files we've seen.
15214      They will go into limbo_die_list.  */
15215   if (flag_eliminate_dwarf2_dups)
15216     break_out_includes (comp_unit_die);
15217
15218   /* Traverse the DIE's and add add sibling attributes to those DIE's
15219      that have children.  */
15220   add_sibling_attributes (comp_unit_die);
15221   for (node = limbo_die_list; node; node = node->next)
15222     add_sibling_attributes (node->die);
15223
15224   /* Output a terminator label for the .text section.  */
15225   switch_to_section (text_section);
15226   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15227   if (flag_reorder_blocks_and_partition)
15228     {
15229       switch_to_section (unlikely_text_section ());
15230       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15231     }
15232
15233   /* We can only use the low/high_pc attributes if all of the code was
15234      in .text.  */
15235   if (!have_multiple_function_sections)
15236     {
15237       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15238       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15239     }
15240
15241   else
15242     {
15243       unsigned fde_idx = 0;
15244
15245       /* We need to give .debug_loc and .debug_ranges an appropriate
15246          "base address".  Use zero so that these addresses become
15247          absolute.  Historically, we've emitted the unexpected
15248          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15249          Emit both to give time for other tools to adapt.  */
15250       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15251       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15252
15253       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15254                          add_ranges_by_labels (text_section_label,
15255                                                text_end_label));
15256       if (flag_reorder_blocks_and_partition)
15257         add_ranges_by_labels (cold_text_section_label,
15258                               cold_end_label);
15259
15260       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15261         {
15262           dw_fde_ref fde = &fde_table[fde_idx];
15263
15264           if (fde->dw_fde_switched_sections)
15265             {
15266               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15267                                     fde->dw_fde_hot_section_end_label);
15268               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15269                                     fde->dw_fde_unlikely_section_end_label);
15270             }
15271           else
15272             add_ranges_by_labels (fde->dw_fde_begin,
15273                                   fde->dw_fde_end);
15274         }
15275
15276       add_ranges (NULL);
15277     }
15278
15279   /* Output location list section if necessary.  */
15280   if (have_location_lists)
15281     {
15282       /* Output the location lists info.  */
15283       switch_to_section (debug_loc_section);
15284       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15285                                    DEBUG_LOC_SECTION_LABEL, 0);
15286       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15287       output_location_lists (die);
15288     }
15289
15290   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15291     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15292                     debug_line_section_label);
15293
15294   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15295     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15296
15297   /* Output all of the compilation units.  We put the main one last so that
15298      the offsets are available to output_pubnames.  */
15299   for (node = limbo_die_list; node; node = node->next)
15300     output_comp_unit (node->die, 0);
15301
15302   output_comp_unit (comp_unit_die, 0);
15303
15304   /* Output the abbreviation table.  */
15305   switch_to_section (debug_abbrev_section);
15306   output_abbrev_section ();
15307
15308   /* Output public names table if necessary.  */
15309   if (!VEC_empty (pubname_entry, pubname_table))
15310     {
15311       switch_to_section (debug_pubnames_section);
15312       output_pubnames (pubname_table);
15313     }
15314
15315 #ifdef DEBUG_PUBTYPES_SECTION
15316   /* Output public types table if necessary.  */
15317   if (!VEC_empty (pubname_entry, pubtype_table))
15318     {
15319       switch_to_section (debug_pubtypes_section);
15320       output_pubnames (pubtype_table);
15321     }
15322 #endif
15323
15324   /* Output the address range information.  We only put functions in the arange
15325      table, so don't write it out if we don't have any.  */
15326   if (fde_table_in_use)
15327     {
15328       switch_to_section (debug_aranges_section);
15329       output_aranges ();
15330     }
15331
15332   /* Output ranges section if necessary.  */
15333   if (ranges_table_in_use)
15334     {
15335       switch_to_section (debug_ranges_section);
15336       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15337       output_ranges ();
15338     }
15339
15340   /* Output the source line correspondence table.  We must do this
15341      even if there is no line information.  Otherwise, on an empty
15342      translation unit, we will generate a present, but empty,
15343      .debug_info section.  IRIX 6.5 `nm' will then complain when
15344      examining the file.  This is done late so that any filenames
15345      used by the debug_info section are marked as 'used'.  */
15346   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15347     {
15348       switch_to_section (debug_line_section);
15349       output_line_info ();
15350     }
15351
15352   /* Have to end the macro section.  */
15353   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15354     {
15355       switch_to_section (debug_macinfo_section);
15356       dw2_asm_output_data (1, 0, "End compilation unit");
15357     }
15358
15359   /* If we emitted any DW_FORM_strp form attribute, output the string
15360      table too.  */
15361   if (debug_str_hash)
15362     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15363 }
15364 #else
15365
15366 /* This should never be used, but its address is needed for comparisons.  */
15367 const struct gcc_debug_hooks dwarf2_debug_hooks;
15368
15369 #endif /* DWARF2_DEBUGGING_INFO */
15370
15371 #include "gt-dwarf2out.h"