OSDN Git Service

2008-05-27 Xuepeng Guo <xuepeng.guo@intel.com>
[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 /* Get the current fde_table entry we should use.  */
637
638 static inline struct dw_fde_struct * 
639 current_fde (void)
640 {
641   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
642 }
643
644 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
645    or to the CIE if LABEL is NULL.  */
646
647 static void
648 add_fde_cfi (const char *label, dw_cfi_ref cfi)
649 {
650   if (label)
651     {
652       dw_fde_ref fde = current_fde ();
653
654       gcc_assert (fde != NULL);
655
656       if (*label == 0)
657         label = dwarf2out_cfi_label ();
658
659       if (fde->dw_fde_current_label == NULL
660           || strcmp (label, fde->dw_fde_current_label) != 0)
661         {
662           dw_cfi_ref xcfi;
663
664           label = xstrdup (label);
665
666           /* Set the location counter to the new label.  */
667           xcfi = new_cfi ();
668           /* If we have a current label, advance from there, otherwise
669              set the location directly using set_loc.  */
670           xcfi->dw_cfi_opc = fde->dw_fde_current_label
671                              ? DW_CFA_advance_loc4
672                              : DW_CFA_set_loc;
673           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
674           add_cfi (&fde->dw_fde_cfi, xcfi);
675
676           fde->dw_fde_current_label = label;
677         }
678
679       add_cfi (&fde->dw_fde_cfi, cfi);
680     }
681
682   else
683     add_cfi (&cie_cfi_head, cfi);
684 }
685
686 /* Subroutine of lookup_cfa.  */
687
688 static void
689 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
690 {
691   switch (cfi->dw_cfi_opc)
692     {
693     case DW_CFA_def_cfa_offset:
694       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
695       break;
696     case DW_CFA_def_cfa_offset_sf:
697       loc->offset
698         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
699       break;
700     case DW_CFA_def_cfa_register:
701       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
702       break;
703     case DW_CFA_def_cfa:
704       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
705       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
706       break;
707     case DW_CFA_def_cfa_sf:
708       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
709       loc->offset
710         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
711       break;
712     case DW_CFA_def_cfa_expression:
713       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
714       break;
715     default:
716       break;
717     }
718 }
719
720 /* Find the previous value for the CFA.  */
721
722 static void
723 lookup_cfa (dw_cfa_location *loc)
724 {
725   dw_cfi_ref cfi;
726   dw_fde_ref fde;
727
728   loc->reg = INVALID_REGNUM;
729   loc->offset = 0;
730   loc->indirect = 0;
731   loc->base_offset = 0;
732
733   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
734     lookup_cfa_1 (cfi, loc);
735
736   fde = current_fde ();
737   if (fde)
738     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
739       lookup_cfa_1 (cfi, loc);
740 }
741
742 /* The current rule for calculating the DWARF2 canonical frame address.  */
743 static dw_cfa_location cfa;
744
745 /* The register used for saving registers to the stack, and its offset
746    from the CFA.  */
747 static dw_cfa_location cfa_store;
748
749 /* The running total of the size of arguments pushed onto the stack.  */
750 static HOST_WIDE_INT args_size;
751
752 /* The last args_size we actually output.  */
753 static HOST_WIDE_INT old_args_size;
754
755 /* Entry point to update the canonical frame address (CFA).
756    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
757    calculated from REG+OFFSET.  */
758
759 void
760 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
761 {
762   dw_cfa_location loc;
763   loc.indirect = 0;
764   loc.base_offset = 0;
765   loc.reg = reg;
766   loc.offset = offset;
767   def_cfa_1 (label, &loc);
768 }
769
770 /* Determine if two dw_cfa_location structures define the same data.  */
771
772 static bool
773 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
774 {
775   return (loc1->reg == loc2->reg
776           && loc1->offset == loc2->offset
777           && loc1->indirect == loc2->indirect
778           && (loc1->indirect == 0
779               || loc1->base_offset == loc2->base_offset));
780 }
781
782 /* This routine does the actual work.  The CFA is now calculated from
783    the dw_cfa_location structure.  */
784
785 static void
786 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
787 {
788   dw_cfi_ref cfi;
789   dw_cfa_location old_cfa, loc;
790
791   cfa = *loc_p;
792   loc = *loc_p;
793
794   if (cfa_store.reg == loc.reg && loc.indirect == 0)
795     cfa_store.offset = loc.offset;
796
797   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
798   lookup_cfa (&old_cfa);
799
800   /* If nothing changed, no need to issue any call frame instructions.  */
801   if (cfa_equal_p (&loc, &old_cfa))
802     return;
803
804   cfi = new_cfi ();
805
806   if (loc.reg == old_cfa.reg && !loc.indirect)
807     {
808       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
809          the CFA register did not change but the offset did.  */
810       if (loc.offset < 0)
811         {
812           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
813           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
814
815           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
816           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
817         }
818       else
819         {
820           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
821           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
822         }
823     }
824
825 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
826   else if (loc.offset == old_cfa.offset
827            && old_cfa.reg != INVALID_REGNUM
828            && !loc.indirect)
829     {
830       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
831          indicating the CFA register has changed to <register> but the
832          offset has not changed.  */
833       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
834       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
835     }
836 #endif
837
838   else if (loc.indirect == 0)
839     {
840       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
841          indicating the CFA register has changed to <register> with
842          the specified offset.  */
843       if (loc.offset < 0)
844         {
845           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
846           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
847
848           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
849           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
850           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
851         }
852       else
853         {
854           cfi->dw_cfi_opc = DW_CFA_def_cfa;
855           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
856           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
857         }
858     }
859   else
860     {
861       /* Construct a DW_CFA_def_cfa_expression instruction to
862          calculate the CFA using a full location expression since no
863          register-offset pair is available.  */
864       struct dw_loc_descr_struct *loc_list;
865
866       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
867       loc_list = build_cfa_loc (&loc, 0);
868       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
869     }
870
871   add_fde_cfi (label, cfi);
872 }
873
874 /* Add the CFI for saving a register.  REG is the CFA column number.
875    LABEL is passed to add_fde_cfi.
876    If SREG is -1, the register is saved at OFFSET from the CFA;
877    otherwise it is saved in SREG.  */
878
879 static void
880 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
881 {
882   dw_cfi_ref cfi = new_cfi ();
883
884   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
885
886   if (sreg == INVALID_REGNUM)
887     {
888       if (reg & ~0x3f)
889         /* The register number won't fit in 6 bits, so we have to use
890            the long form.  */
891         cfi->dw_cfi_opc = DW_CFA_offset_extended;
892       else
893         cfi->dw_cfi_opc = DW_CFA_offset;
894
895 #ifdef ENABLE_CHECKING
896       {
897         /* If we get an offset that is not a multiple of
898            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
899            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
900            description.  */
901         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
902
903         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
904       }
905 #endif
906       offset /= DWARF_CIE_DATA_ALIGNMENT;
907       if (offset < 0)
908         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
909
910       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
911     }
912   else if (sreg == reg)
913     cfi->dw_cfi_opc = DW_CFA_same_value;
914   else
915     {
916       cfi->dw_cfi_opc = DW_CFA_register;
917       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
918     }
919
920   add_fde_cfi (label, cfi);
921 }
922
923 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
924    This CFI tells the unwinder that it needs to restore the window registers
925    from the previous frame's window save area.
926
927    ??? Perhaps we should note in the CIE where windows are saved (instead of
928    assuming 0(cfa)) and what registers are in the window.  */
929
930 void
931 dwarf2out_window_save (const char *label)
932 {
933   dw_cfi_ref cfi = new_cfi ();
934
935   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
936   add_fde_cfi (label, cfi);
937 }
938
939 /* Add a CFI to update the running total of the size of arguments
940    pushed onto the stack.  */
941
942 void
943 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
944 {
945   dw_cfi_ref cfi;
946
947   if (size == old_args_size)
948     return;
949
950   old_args_size = size;
951
952   cfi = new_cfi ();
953   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
954   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
955   add_fde_cfi (label, cfi);
956 }
957
958 /* Entry point for saving a register to the stack.  REG is the GCC register
959    number.  LABEL and OFFSET are passed to reg_save.  */
960
961 void
962 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
963 {
964   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
965 }
966
967 /* Entry point for saving the return address in the stack.
968    LABEL and OFFSET are passed to reg_save.  */
969
970 void
971 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
972 {
973   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
974 }
975
976 /* Entry point for saving the return address in a register.
977    LABEL and SREG are passed to reg_save.  */
978
979 void
980 dwarf2out_return_reg (const char *label, unsigned int sreg)
981 {
982   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
983 }
984
985 #ifdef DWARF2_UNWIND_INFO
986 /* Record the initial position of the return address.  RTL is
987    INCOMING_RETURN_ADDR_RTX.  */
988
989 static void
990 initial_return_save (rtx rtl)
991 {
992   unsigned int reg = INVALID_REGNUM;
993   HOST_WIDE_INT offset = 0;
994
995   switch (GET_CODE (rtl))
996     {
997     case REG:
998       /* RA is in a register.  */
999       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1000       break;
1001
1002     case MEM:
1003       /* RA is on the stack.  */
1004       rtl = XEXP (rtl, 0);
1005       switch (GET_CODE (rtl))
1006         {
1007         case REG:
1008           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1009           offset = 0;
1010           break;
1011
1012         case PLUS:
1013           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1014           offset = INTVAL (XEXP (rtl, 1));
1015           break;
1016
1017         case MINUS:
1018           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1019           offset = -INTVAL (XEXP (rtl, 1));
1020           break;
1021
1022         default:
1023           gcc_unreachable ();
1024         }
1025
1026       break;
1027
1028     case PLUS:
1029       /* The return address is at some offset from any value we can
1030          actually load.  For instance, on the SPARC it is in %i7+8. Just
1031          ignore the offset for now; it doesn't matter for unwinding frames.  */
1032       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1033       initial_return_save (XEXP (rtl, 0));
1034       return;
1035
1036     default:
1037       gcc_unreachable ();
1038     }
1039
1040   if (reg != DWARF_FRAME_RETURN_COLUMN)
1041     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1042 }
1043 #endif
1044
1045 /* Given a SET, calculate the amount of stack adjustment it
1046    contains.  */
1047
1048 static HOST_WIDE_INT
1049 stack_adjust_offset (const_rtx pattern)
1050 {
1051   const_rtx src = SET_SRC (pattern);
1052   const_rtx dest = SET_DEST (pattern);
1053   HOST_WIDE_INT offset = 0;
1054   enum rtx_code code;
1055
1056   if (dest == stack_pointer_rtx)
1057     {
1058       /* (set (reg sp) (plus (reg sp) (const_int))) */
1059       code = GET_CODE (src);
1060       if (! (code == PLUS || code == MINUS)
1061           || XEXP (src, 0) != stack_pointer_rtx
1062           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1063         return 0;
1064
1065       offset = INTVAL (XEXP (src, 1));
1066       if (code == PLUS)
1067         offset = -offset;
1068     }
1069   else if (MEM_P (dest))
1070     {
1071       /* (set (mem (pre_dec (reg sp))) (foo)) */
1072       src = XEXP (dest, 0);
1073       code = GET_CODE (src);
1074
1075       switch (code)
1076         {
1077         case PRE_MODIFY:
1078         case POST_MODIFY:
1079           if (XEXP (src, 0) == stack_pointer_rtx)
1080             {
1081               rtx val = XEXP (XEXP (src, 1), 1);
1082               /* We handle only adjustments by constant amount.  */
1083               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1084                           && GET_CODE (val) == CONST_INT);
1085               offset = -INTVAL (val);
1086               break;
1087             }
1088           return 0;
1089
1090         case PRE_DEC:
1091         case POST_DEC:
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         case PRE_INC:
1100         case POST_INC:
1101           if (XEXP (src, 0) == stack_pointer_rtx)
1102             {
1103               offset = -GET_MODE_SIZE (GET_MODE (dest));
1104               break;
1105             }
1106           return 0;
1107
1108         default:
1109           return 0;
1110         }
1111     }
1112   else
1113     return 0;
1114
1115   return offset;
1116 }
1117
1118 /* Check INSN to see if it looks like a push or a stack adjustment, and
1119    make a note of it if it does.  EH uses this information to find out how
1120    much extra space it needs to pop off the stack.  */
1121
1122 static void
1123 dwarf2out_stack_adjust (rtx insn, bool after_p)
1124 {
1125   HOST_WIDE_INT offset;
1126   const char *label;
1127   int i;
1128
1129   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1130      with this function.  Proper support would require all frame-related
1131      insns to be marked, and to be able to handle saving state around
1132      epilogues textually in the middle of the function.  */
1133   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1134     return;
1135
1136   /* If only calls can throw, and we have a frame pointer,
1137      save up adjustments until we see the CALL_INSN.  */
1138   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1139     {
1140       if (CALL_P (insn) && !after_p)
1141         {
1142           /* Extract the size of the args from the CALL rtx itself.  */
1143           insn = PATTERN (insn);
1144           if (GET_CODE (insn) == PARALLEL)
1145             insn = XVECEXP (insn, 0, 0);
1146           if (GET_CODE (insn) == SET)
1147             insn = SET_SRC (insn);
1148           gcc_assert (GET_CODE (insn) == CALL);
1149           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1150         }
1151       return;
1152     }
1153
1154   if (CALL_P (insn) && !after_p)
1155     {
1156       if (!flag_asynchronous_unwind_tables)
1157         dwarf2out_args_size ("", args_size);
1158       return;
1159     }
1160   else if (BARRIER_P (insn))
1161     {
1162       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1163          the compiler will have already emitted a stack adjustment, but
1164          doesn't bother for calls to noreturn functions.  */
1165 #ifdef STACK_GROWS_DOWNWARD
1166       offset = -args_size;
1167 #else
1168       offset = args_size;
1169 #endif
1170     }
1171   else if (GET_CODE (PATTERN (insn)) == SET)
1172     offset = stack_adjust_offset (PATTERN (insn));
1173   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1174            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1175     {
1176       /* There may be stack adjustments inside compound insns.  Search
1177          for them.  */
1178       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1179         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1180           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1181     }
1182   else
1183     return;
1184
1185   if (offset == 0)
1186     return;
1187
1188   if (cfa.reg == STACK_POINTER_REGNUM)
1189     cfa.offset += offset;
1190
1191 #ifndef STACK_GROWS_DOWNWARD
1192   offset = -offset;
1193 #endif
1194
1195   args_size += offset;
1196   if (args_size < 0)
1197     args_size = 0;
1198
1199   label = dwarf2out_cfi_label ();
1200   def_cfa_1 (label, &cfa);
1201   if (flag_asynchronous_unwind_tables)
1202     dwarf2out_args_size (label, args_size);
1203 }
1204
1205 #endif
1206
1207 /* We delay emitting a register save until either (a) we reach the end
1208    of the prologue or (b) the register is clobbered.  This clusters
1209    register saves so that there are fewer pc advances.  */
1210
1211 struct queued_reg_save GTY(())
1212 {
1213   struct queued_reg_save *next;
1214   rtx reg;
1215   HOST_WIDE_INT cfa_offset;
1216   rtx saved_reg;
1217 };
1218
1219 static GTY(()) struct queued_reg_save *queued_reg_saves;
1220
1221 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1222 struct reg_saved_in_data GTY(()) {
1223   rtx orig_reg;
1224   rtx saved_in_reg;
1225 };
1226
1227 /* A list of registers saved in other registers.
1228    The list intentionally has a small maximum capacity of 4; if your
1229    port needs more than that, you might consider implementing a
1230    more efficient data structure.  */
1231 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1232 static GTY(()) size_t num_regs_saved_in_regs;
1233
1234 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1235 static const char *last_reg_save_label;
1236
1237 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1238    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1239
1240 static void
1241 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1242 {
1243   struct queued_reg_save *q;
1244
1245   /* Duplicates waste space, but it's also necessary to remove them
1246      for correctness, since the queue gets output in reverse
1247      order.  */
1248   for (q = queued_reg_saves; q != NULL; q = q->next)
1249     if (REGNO (q->reg) == REGNO (reg))
1250       break;
1251
1252   if (q == NULL)
1253     {
1254       q = ggc_alloc (sizeof (*q));
1255       q->next = queued_reg_saves;
1256       queued_reg_saves = q;
1257     }
1258
1259   q->reg = reg;
1260   q->cfa_offset = offset;
1261   q->saved_reg = sreg;
1262
1263   last_reg_save_label = label;
1264 }
1265
1266 /* Output all the entries in QUEUED_REG_SAVES.  */
1267
1268 static void
1269 flush_queued_reg_saves (void)
1270 {
1271   struct queued_reg_save *q;
1272
1273   for (q = queued_reg_saves; q; q = q->next)
1274     {
1275       size_t i;
1276       unsigned int reg, sreg;
1277
1278       for (i = 0; i < num_regs_saved_in_regs; i++)
1279         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1280           break;
1281       if (q->saved_reg && i == num_regs_saved_in_regs)
1282         {
1283           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1284           num_regs_saved_in_regs++;
1285         }
1286       if (i != num_regs_saved_in_regs)
1287         {
1288           regs_saved_in_regs[i].orig_reg = q->reg;
1289           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1290         }
1291
1292       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1293       if (q->saved_reg)
1294         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1295       else
1296         sreg = INVALID_REGNUM;
1297       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1298     }
1299
1300   queued_reg_saves = NULL;
1301   last_reg_save_label = NULL;
1302 }
1303
1304 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1305    location for?  Or, does it clobber a register which we've previously
1306    said that some other register is saved in, and for which we now
1307    have a new location for?  */
1308
1309 static bool
1310 clobbers_queued_reg_save (const_rtx insn)
1311 {
1312   struct queued_reg_save *q;
1313
1314   for (q = queued_reg_saves; q; q = q->next)
1315     {
1316       size_t i;
1317       if (modified_in_p (q->reg, insn))
1318         return true;
1319       for (i = 0; i < num_regs_saved_in_regs; i++)
1320         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1321             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1322           return true;
1323     }
1324
1325   return false;
1326 }
1327
1328 /* Entry point for saving the first register into the second.  */
1329
1330 void
1331 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1332 {
1333   size_t i;
1334   unsigned int regno, sregno;
1335
1336   for (i = 0; i < num_regs_saved_in_regs; i++)
1337     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1338       break;
1339   if (i == num_regs_saved_in_regs)
1340     {
1341       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1342       num_regs_saved_in_regs++;
1343     }
1344   regs_saved_in_regs[i].orig_reg = reg;
1345   regs_saved_in_regs[i].saved_in_reg = sreg;
1346
1347   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1348   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1349   reg_save (label, regno, sregno, 0);
1350 }
1351
1352 /* What register, if any, is currently saved in REG?  */
1353
1354 static rtx
1355 reg_saved_in (rtx reg)
1356 {
1357   unsigned int regn = REGNO (reg);
1358   size_t i;
1359   struct queued_reg_save *q;
1360
1361   for (q = queued_reg_saves; q; q = q->next)
1362     if (q->saved_reg && regn == REGNO (q->saved_reg))
1363       return q->reg;
1364
1365   for (i = 0; i < num_regs_saved_in_regs; i++)
1366     if (regs_saved_in_regs[i].saved_in_reg
1367         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1368       return regs_saved_in_regs[i].orig_reg;
1369
1370   return NULL_RTX;
1371 }
1372
1373
1374 /* A temporary register holding an integral value used in adjusting SP
1375    or setting up the store_reg.  The "offset" field holds the integer
1376    value, not an offset.  */
1377 static dw_cfa_location cfa_temp;
1378
1379 /* Record call frame debugging information for an expression EXPR,
1380    which either sets SP or FP (adjusting how we calculate the frame
1381    address) or saves a register to the stack or another register.
1382    LABEL indicates the address of EXPR.
1383
1384    This function encodes a state machine mapping rtxes to actions on
1385    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1386    users need not read the source code.
1387
1388   The High-Level Picture
1389
1390   Changes in the register we use to calculate the CFA: Currently we
1391   assume that if you copy the CFA register into another register, we
1392   should take the other one as the new CFA register; this seems to
1393   work pretty well.  If it's wrong for some target, it's simple
1394   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1395
1396   Changes in the register we use for saving registers to the stack:
1397   This is usually SP, but not always.  Again, we deduce that if you
1398   copy SP into another register (and SP is not the CFA register),
1399   then the new register is the one we will be using for register
1400   saves.  This also seems to work.
1401
1402   Register saves: There's not much guesswork about this one; if
1403   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1404   register save, and the register used to calculate the destination
1405   had better be the one we think we're using for this purpose.
1406   It's also assumed that a copy from a call-saved register to another
1407   register is saving that register if RTX_FRAME_RELATED_P is set on
1408   that instruction.  If the copy is from a call-saved register to
1409   the *same* register, that means that the register is now the same
1410   value as in the caller.
1411
1412   Except: If the register being saved is the CFA register, and the
1413   offset is nonzero, we are saving the CFA, so we assume we have to
1414   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1415   the intent is to save the value of SP from the previous frame.
1416
1417   In addition, if a register has previously been saved to a different
1418   register,
1419
1420   Invariants / Summaries of Rules
1421
1422   cfa          current rule for calculating the CFA.  It usually
1423                consists of a register and an offset.
1424   cfa_store    register used by prologue code to save things to the stack
1425                cfa_store.offset is the offset from the value of
1426                cfa_store.reg to the actual CFA
1427   cfa_temp     register holding an integral value.  cfa_temp.offset
1428                stores the value, which will be used to adjust the
1429                stack pointer.  cfa_temp is also used like cfa_store,
1430                to track stores to the stack via fp or a temp reg.
1431
1432   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1433                with cfa.reg as the first operand changes the cfa.reg and its
1434                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1435                cfa_temp.offset.
1436
1437   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1438                expression yielding a constant.  This sets cfa_temp.reg
1439                and cfa_temp.offset.
1440
1441   Rule 5:      Create a new register cfa_store used to save items to the
1442                stack.
1443
1444   Rules 10-14: Save a register to the stack.  Define offset as the
1445                difference of the original location and cfa_store's
1446                location (or cfa_temp's location if cfa_temp is used).
1447
1448   The Rules
1449
1450   "{a,b}" indicates a choice of a xor b.
1451   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1452
1453   Rule 1:
1454   (set <reg1> <reg2>:cfa.reg)
1455   effects: cfa.reg = <reg1>
1456            cfa.offset unchanged
1457            cfa_temp.reg = <reg1>
1458            cfa_temp.offset = cfa.offset
1459
1460   Rule 2:
1461   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1462                               {<const_int>,<reg>:cfa_temp.reg}))
1463   effects: cfa.reg = sp if fp used
1464            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1465            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1466              if cfa_store.reg==sp
1467
1468   Rule 3:
1469   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1470   effects: cfa.reg = fp
1471            cfa_offset += +/- <const_int>
1472
1473   Rule 4:
1474   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1475   constraints: <reg1> != fp
1476                <reg1> != sp
1477   effects: cfa.reg = <reg1>
1478            cfa_temp.reg = <reg1>
1479            cfa_temp.offset = cfa.offset
1480
1481   Rule 5:
1482   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1483   constraints: <reg1> != fp
1484                <reg1> != sp
1485   effects: cfa_store.reg = <reg1>
1486            cfa_store.offset = cfa.offset - cfa_temp.offset
1487
1488   Rule 6:
1489   (set <reg> <const_int>)
1490   effects: cfa_temp.reg = <reg>
1491            cfa_temp.offset = <const_int>
1492
1493   Rule 7:
1494   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1495   effects: cfa_temp.reg = <reg1>
1496            cfa_temp.offset |= <const_int>
1497
1498   Rule 8:
1499   (set <reg> (high <exp>))
1500   effects: none
1501
1502   Rule 9:
1503   (set <reg> (lo_sum <exp> <const_int>))
1504   effects: cfa_temp.reg = <reg>
1505            cfa_temp.offset = <const_int>
1506
1507   Rule 10:
1508   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1509   effects: cfa_store.offset -= <const_int>
1510            cfa.offset = cfa_store.offset if cfa.reg == sp
1511            cfa.reg = sp
1512            cfa.base_offset = -cfa_store.offset
1513
1514   Rule 11:
1515   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1516   effects: cfa_store.offset += -/+ mode_size(mem)
1517            cfa.offset = cfa_store.offset if cfa.reg == sp
1518            cfa.reg = sp
1519            cfa.base_offset = -cfa_store.offset
1520
1521   Rule 12:
1522   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1523
1524        <reg2>)
1525   effects: cfa.reg = <reg1>
1526            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1527
1528   Rule 13:
1529   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1530   effects: cfa.reg = <reg1>
1531            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1532
1533   Rule 14:
1534   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1535   effects: cfa.reg = <reg1>
1536            cfa.base_offset = -cfa_temp.offset
1537            cfa_temp.offset -= mode_size(mem)
1538
1539   Rule 15:
1540   (set <reg> {unspec, unspec_volatile})
1541   effects: target-dependent  */
1542
1543 static void
1544 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1545 {
1546   rtx src, dest, span;
1547   HOST_WIDE_INT offset;
1548
1549   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1550      the PARALLEL independently. The first element is always processed if
1551      it is a SET. This is for backward compatibility.   Other elements
1552      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1553      flag is set in them.  */
1554   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1555     {
1556       int par_index;
1557       int limit = XVECLEN (expr, 0);
1558       rtx elem;
1559
1560       /* PARALLELs have strict read-modify-write semantics, so we
1561          ought to evaluate every rvalue before changing any lvalue.
1562          It's cumbersome to do that in general, but there's an
1563          easy approximation that is enough for all current users:
1564          handle register saves before register assignments.  */
1565       if (GET_CODE (expr) == PARALLEL)
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))
1571                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1572               dwarf2out_frame_debug_expr (elem, label);
1573           }
1574
1575       for (par_index = 0; par_index < limit; par_index++)
1576         {
1577           elem = XVECEXP (expr, 0, par_index);
1578           if (GET_CODE (elem) == SET
1579               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1580               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1581             dwarf2out_frame_debug_expr (elem, label);
1582         }
1583       return;
1584     }
1585
1586   gcc_assert (GET_CODE (expr) == SET);
1587
1588   src = SET_SRC (expr);
1589   dest = SET_DEST (expr);
1590
1591   if (REG_P (src))
1592     {
1593       rtx rsi = reg_saved_in (src);
1594       if (rsi)
1595         src = rsi;
1596     }
1597
1598   switch (GET_CODE (dest))
1599     {
1600     case REG:
1601       switch (GET_CODE (src))
1602         {
1603           /* Setting FP from SP.  */
1604         case REG:
1605           if (cfa.reg == (unsigned) REGNO (src))
1606             {
1607               /* Rule 1 */
1608               /* Update the CFA rule wrt SP or FP.  Make sure src is
1609                  relative to the current CFA register.
1610
1611                  We used to require that dest be either SP or FP, but the
1612                  ARM copies SP to a temporary register, and from there to
1613                  FP.  So we just rely on the backends to only set
1614                  RTX_FRAME_RELATED_P on appropriate insns.  */
1615               cfa.reg = REGNO (dest);
1616               cfa_temp.reg = cfa.reg;
1617               cfa_temp.offset = cfa.offset;
1618             }
1619           else
1620             {
1621               /* Saving a register in a register.  */
1622               gcc_assert (!fixed_regs [REGNO (dest)]
1623                           /* For the SPARC and its register window.  */
1624                           || (DWARF_FRAME_REGNUM (REGNO (src))
1625                               == DWARF_FRAME_RETURN_COLUMN));
1626               queue_reg_save (label, src, dest, 0);
1627             }
1628           break;
1629
1630         case PLUS:
1631         case MINUS:
1632         case LO_SUM:
1633           if (dest == stack_pointer_rtx)
1634             {
1635               /* Rule 2 */
1636               /* Adjusting SP.  */
1637               switch (GET_CODE (XEXP (src, 1)))
1638                 {
1639                 case CONST_INT:
1640                   offset = INTVAL (XEXP (src, 1));
1641                   break;
1642                 case REG:
1643                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1644                               == cfa_temp.reg);
1645                   offset = cfa_temp.offset;
1646                   break;
1647                 default:
1648                   gcc_unreachable ();
1649                 }
1650
1651               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1652                 {
1653                   /* Restoring SP from FP in the epilogue.  */
1654                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1655                   cfa.reg = STACK_POINTER_REGNUM;
1656                 }
1657               else if (GET_CODE (src) == LO_SUM)
1658                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1659                 ;
1660               else
1661                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1662
1663               if (GET_CODE (src) != MINUS)
1664                 offset = -offset;
1665               if (cfa.reg == STACK_POINTER_REGNUM)
1666                 cfa.offset += offset;
1667               if (cfa_store.reg == STACK_POINTER_REGNUM)
1668                 cfa_store.offset += offset;
1669             }
1670           else if (dest == hard_frame_pointer_rtx)
1671             {
1672               /* Rule 3 */
1673               /* Either setting the FP from an offset of the SP,
1674                  or adjusting the FP */
1675               gcc_assert (frame_pointer_needed);
1676
1677               gcc_assert (REG_P (XEXP (src, 0))
1678                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1679                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1680               offset = INTVAL (XEXP (src, 1));
1681               if (GET_CODE (src) != MINUS)
1682                 offset = -offset;
1683               cfa.offset += offset;
1684               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1685             }
1686           else
1687             {
1688               gcc_assert (GET_CODE (src) != MINUS);
1689
1690               /* Rule 4 */
1691               if (REG_P (XEXP (src, 0))
1692                   && REGNO (XEXP (src, 0)) == cfa.reg
1693                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1694                 {
1695                   /* Setting a temporary CFA register that will be copied
1696                      into the FP later on.  */
1697                   offset = - INTVAL (XEXP (src, 1));
1698                   cfa.offset += offset;
1699                   cfa.reg = REGNO (dest);
1700                   /* Or used to save regs to the stack.  */
1701                   cfa_temp.reg = cfa.reg;
1702                   cfa_temp.offset = cfa.offset;
1703                 }
1704
1705               /* Rule 5 */
1706               else if (REG_P (XEXP (src, 0))
1707                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1708                        && XEXP (src, 1) == stack_pointer_rtx)
1709                 {
1710                   /* Setting a scratch register that we will use instead
1711                      of SP for saving registers to the stack.  */
1712                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1713                   cfa_store.reg = REGNO (dest);
1714                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1715                 }
1716
1717               /* Rule 9 */
1718               else if (GET_CODE (src) == LO_SUM
1719                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1720                 {
1721                   cfa_temp.reg = REGNO (dest);
1722                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1723                 }
1724               else
1725                 gcc_unreachable ();
1726             }
1727           break;
1728
1729           /* Rule 6 */
1730         case CONST_INT:
1731           cfa_temp.reg = REGNO (dest);
1732           cfa_temp.offset = INTVAL (src);
1733           break;
1734
1735           /* Rule 7 */
1736         case IOR:
1737           gcc_assert (REG_P (XEXP (src, 0))
1738                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1739                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1740
1741           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1742             cfa_temp.reg = REGNO (dest);
1743           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1744           break;
1745
1746           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1747              which will fill in all of the bits.  */
1748           /* Rule 8 */
1749         case HIGH:
1750           break;
1751
1752           /* Rule 15 */
1753         case UNSPEC:
1754         case UNSPEC_VOLATILE:
1755           gcc_assert (targetm.dwarf_handle_frame_unspec);
1756           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1757           return;
1758
1759         default:
1760           gcc_unreachable ();
1761         }
1762
1763       def_cfa_1 (label, &cfa);
1764       break;
1765
1766     case MEM:
1767       gcc_assert (REG_P (src));
1768
1769       /* Saving a register to the stack.  Make sure dest is relative to the
1770          CFA register.  */
1771       switch (GET_CODE (XEXP (dest, 0)))
1772         {
1773           /* Rule 10 */
1774           /* With a push.  */
1775         case PRE_MODIFY:
1776           /* We can't handle variable size modifications.  */
1777           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1778                       == CONST_INT);
1779           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1780
1781           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1782                       && cfa_store.reg == STACK_POINTER_REGNUM);
1783
1784           cfa_store.offset += offset;
1785           if (cfa.reg == STACK_POINTER_REGNUM)
1786             cfa.offset = cfa_store.offset;
1787
1788           offset = -cfa_store.offset;
1789           break;
1790
1791           /* Rule 11 */
1792         case PRE_INC:
1793         case PRE_DEC:
1794           offset = GET_MODE_SIZE (GET_MODE (dest));
1795           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1796             offset = -offset;
1797
1798           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1799                       && cfa_store.reg == STACK_POINTER_REGNUM);
1800
1801           cfa_store.offset += offset;
1802           if (cfa.reg == STACK_POINTER_REGNUM)
1803             cfa.offset = cfa_store.offset;
1804
1805           offset = -cfa_store.offset;
1806           break;
1807
1808           /* Rule 12 */
1809           /* With an offset.  */
1810         case PLUS:
1811         case MINUS:
1812         case LO_SUM:
1813           {
1814             int regno;
1815
1816             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1817                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1818             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1819             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1820               offset = -offset;
1821
1822             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1823
1824             if (cfa_store.reg == (unsigned) regno)
1825               offset -= cfa_store.offset;
1826             else
1827               {
1828                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1829                 offset -= cfa_temp.offset;
1830               }
1831           }
1832           break;
1833
1834           /* Rule 13 */
1835           /* Without an offset.  */
1836         case REG:
1837           {
1838             int regno = REGNO (XEXP (dest, 0));
1839
1840             if (cfa_store.reg == (unsigned) regno)
1841               offset = -cfa_store.offset;
1842             else
1843               {
1844                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1845                 offset = -cfa_temp.offset;
1846               }
1847           }
1848           break;
1849
1850           /* Rule 14 */
1851         case POST_INC:
1852           gcc_assert (cfa_temp.reg
1853                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1854           offset = -cfa_temp.offset;
1855           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1856           break;
1857
1858         default:
1859           gcc_unreachable ();
1860         }
1861
1862       if (REGNO (src) != STACK_POINTER_REGNUM
1863           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1864           && (unsigned) REGNO (src) == cfa.reg)
1865         {
1866           /* We're storing the current CFA reg into the stack.  */
1867
1868           if (cfa.offset == 0)
1869             {
1870               /* If the source register is exactly the CFA, assume
1871                  we're saving SP like any other register; this happens
1872                  on the ARM.  */
1873               def_cfa_1 (label, &cfa);
1874               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1875               break;
1876             }
1877           else
1878             {
1879               /* Otherwise, we'll need to look in the stack to
1880                  calculate the CFA.  */
1881               rtx x = XEXP (dest, 0);
1882
1883               if (!REG_P (x))
1884                 x = XEXP (x, 0);
1885               gcc_assert (REG_P (x));
1886
1887               cfa.reg = REGNO (x);
1888               cfa.base_offset = offset;
1889               cfa.indirect = 1;
1890               def_cfa_1 (label, &cfa);
1891               break;
1892             }
1893         }
1894
1895       def_cfa_1 (label, &cfa);
1896       {
1897         span = targetm.dwarf_register_span (src);
1898
1899         if (!span)
1900           queue_reg_save (label, src, NULL_RTX, offset);
1901         else
1902           {
1903             /* We have a PARALLEL describing where the contents of SRC
1904                live.  Queue register saves for each piece of the
1905                PARALLEL.  */
1906             int par_index;
1907             int limit;
1908             HOST_WIDE_INT span_offset = offset;
1909
1910             gcc_assert (GET_CODE (span) == PARALLEL);
1911
1912             limit = XVECLEN (span, 0);
1913             for (par_index = 0; par_index < limit; par_index++)
1914               {
1915                 rtx elem = XVECEXP (span, 0, par_index);
1916
1917                 queue_reg_save (label, elem, NULL_RTX, span_offset);
1918                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1919               }
1920           }
1921       }
1922       break;
1923
1924     default:
1925       gcc_unreachable ();
1926     }
1927 }
1928
1929 /* Record call frame debugging information for INSN, which either
1930    sets SP or FP (adjusting how we calculate the frame address) or saves a
1931    register to the stack.  If INSN is NULL_RTX, initialize our state.
1932
1933    If AFTER_P is false, we're being called before the insn is emitted,
1934    otherwise after.  Call instructions get invoked twice.  */
1935
1936 void
1937 dwarf2out_frame_debug (rtx insn, bool after_p)
1938 {
1939   const char *label;
1940   rtx src;
1941
1942   if (insn == NULL_RTX)
1943     {
1944       size_t i;
1945
1946       /* Flush any queued register saves.  */
1947       flush_queued_reg_saves ();
1948
1949       /* Set up state for generating call frame debug info.  */
1950       lookup_cfa (&cfa);
1951       gcc_assert (cfa.reg
1952                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1953
1954       cfa.reg = STACK_POINTER_REGNUM;
1955       cfa_store = cfa;
1956       cfa_temp.reg = -1;
1957       cfa_temp.offset = 0;
1958
1959       for (i = 0; i < num_regs_saved_in_regs; i++)
1960         {
1961           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1962           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1963         }
1964       num_regs_saved_in_regs = 0;
1965       return;
1966     }
1967
1968   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1969     flush_queued_reg_saves ();
1970
1971   if (! RTX_FRAME_RELATED_P (insn))
1972     {
1973       if (!ACCUMULATE_OUTGOING_ARGS)
1974         dwarf2out_stack_adjust (insn, after_p);
1975       return;
1976     }
1977
1978   label = dwarf2out_cfi_label ();
1979   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1980   if (src)
1981     insn = XEXP (src, 0);
1982   else
1983     insn = PATTERN (insn);
1984
1985   dwarf2out_frame_debug_expr (insn, label);
1986 }
1987
1988 #endif
1989
1990 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1991 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1992  (enum dwarf_call_frame_info cfi);
1993
1994 static enum dw_cfi_oprnd_type
1995 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1996 {
1997   switch (cfi)
1998     {
1999     case DW_CFA_nop:
2000     case DW_CFA_GNU_window_save:
2001       return dw_cfi_oprnd_unused;
2002
2003     case DW_CFA_set_loc:
2004     case DW_CFA_advance_loc1:
2005     case DW_CFA_advance_loc2:
2006     case DW_CFA_advance_loc4:
2007     case DW_CFA_MIPS_advance_loc8:
2008       return dw_cfi_oprnd_addr;
2009
2010     case DW_CFA_offset:
2011     case DW_CFA_offset_extended:
2012     case DW_CFA_def_cfa:
2013     case DW_CFA_offset_extended_sf:
2014     case DW_CFA_def_cfa_sf:
2015     case DW_CFA_restore_extended:
2016     case DW_CFA_undefined:
2017     case DW_CFA_same_value:
2018     case DW_CFA_def_cfa_register:
2019     case DW_CFA_register:
2020       return dw_cfi_oprnd_reg_num;
2021
2022     case DW_CFA_def_cfa_offset:
2023     case DW_CFA_GNU_args_size:
2024     case DW_CFA_def_cfa_offset_sf:
2025       return dw_cfi_oprnd_offset;
2026
2027     case DW_CFA_def_cfa_expression:
2028     case DW_CFA_expression:
2029       return dw_cfi_oprnd_loc;
2030
2031     default:
2032       gcc_unreachable ();
2033     }
2034 }
2035
2036 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2037 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2038  (enum dwarf_call_frame_info cfi);
2039
2040 static enum dw_cfi_oprnd_type
2041 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2042 {
2043   switch (cfi)
2044     {
2045     case DW_CFA_def_cfa:
2046     case DW_CFA_def_cfa_sf:
2047     case DW_CFA_offset:
2048     case DW_CFA_offset_extended_sf:
2049     case DW_CFA_offset_extended:
2050       return dw_cfi_oprnd_offset;
2051
2052     case DW_CFA_register:
2053       return dw_cfi_oprnd_reg_num;
2054
2055     default:
2056       return dw_cfi_oprnd_unused;
2057     }
2058 }
2059
2060 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2061
2062 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2063    switch to the data section instead, and write out a synthetic label
2064    for collect2.  */
2065
2066 static void
2067 switch_to_eh_frame_section (void)
2068 {
2069   tree label;
2070
2071 #ifdef EH_FRAME_SECTION_NAME
2072   if (eh_frame_section == 0)
2073     {
2074       int flags;
2075
2076       if (EH_TABLES_CAN_BE_READ_ONLY)
2077         {
2078           int fde_encoding;
2079           int per_encoding;
2080           int lsda_encoding;
2081
2082           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2083                                                        /*global=*/0);
2084           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2085                                                        /*global=*/1);
2086           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2087                                                         /*global=*/0);
2088           flags = ((! flag_pic
2089                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2090                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2091                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2092                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2093                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2094                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2095                    ? 0 : SECTION_WRITE);
2096         }
2097       else
2098         flags = SECTION_WRITE;
2099       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2100     }
2101 #endif
2102
2103   if (eh_frame_section)
2104     switch_to_section (eh_frame_section);
2105   else
2106     {
2107       /* We have no special eh_frame section.  Put the information in
2108          the data section and emit special labels to guide collect2.  */
2109       switch_to_section (data_section);
2110       label = get_file_function_name ("F");
2111       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2112       targetm.asm_out.globalize_label (asm_out_file,
2113                                        IDENTIFIER_POINTER (label));
2114       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2115     }
2116 }
2117
2118 /* Output a Call Frame Information opcode and its operand(s).  */
2119
2120 static void
2121 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2122 {
2123   unsigned long r;
2124   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2125     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2126                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2127                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2128                          ((unsigned HOST_WIDE_INT)
2129                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2130   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2131     {
2132       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2133       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2134                            "DW_CFA_offset, column 0x%lx", r);
2135       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2136     }
2137   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2138     {
2139       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2140       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2141                            "DW_CFA_restore, column 0x%lx", r);
2142     }
2143   else
2144     {
2145       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2146                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2147
2148       switch (cfi->dw_cfi_opc)
2149         {
2150         case DW_CFA_set_loc:
2151           if (for_eh)
2152             dw2_asm_output_encoded_addr_rtx (
2153                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2154                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2155                 false, NULL);
2156           else
2157             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2158                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2159           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2160           break;
2161
2162         case DW_CFA_advance_loc1:
2163           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2164                                 fde->dw_fde_current_label, NULL);
2165           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2166           break;
2167
2168         case DW_CFA_advance_loc2:
2169           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2170                                 fde->dw_fde_current_label, NULL);
2171           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2172           break;
2173
2174         case DW_CFA_advance_loc4:
2175           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2176                                 fde->dw_fde_current_label, NULL);
2177           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2178           break;
2179
2180         case DW_CFA_MIPS_advance_loc8:
2181           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2182                                 fde->dw_fde_current_label, NULL);
2183           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2184           break;
2185
2186         case DW_CFA_offset_extended:
2187         case DW_CFA_def_cfa:
2188           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2189           dw2_asm_output_data_uleb128 (r, NULL);
2190           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2191           break;
2192
2193         case DW_CFA_offset_extended_sf:
2194         case DW_CFA_def_cfa_sf:
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           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2198           break;
2199
2200         case DW_CFA_restore_extended:
2201         case DW_CFA_undefined:
2202         case DW_CFA_same_value:
2203         case DW_CFA_def_cfa_register:
2204           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2205           dw2_asm_output_data_uleb128 (r, NULL);
2206           break;
2207
2208         case DW_CFA_register:
2209           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2210           dw2_asm_output_data_uleb128 (r, NULL);
2211           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2212           dw2_asm_output_data_uleb128 (r, NULL);
2213           break;
2214
2215         case DW_CFA_def_cfa_offset:
2216         case DW_CFA_GNU_args_size:
2217           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2218           break;
2219
2220         case DW_CFA_def_cfa_offset_sf:
2221           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2222           break;
2223
2224         case DW_CFA_GNU_window_save:
2225           break;
2226
2227         case DW_CFA_def_cfa_expression:
2228         case DW_CFA_expression:
2229           output_cfa_loc (cfi);
2230           break;
2231
2232         case DW_CFA_GNU_negative_offset_extended:
2233           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2234           gcc_unreachable ();
2235
2236         default:
2237           break;
2238         }
2239     }
2240 }
2241
2242 /* Output the call frame information used to record information
2243    that relates to calculating the frame pointer, and records the
2244    location of saved registers.  */
2245
2246 static void
2247 output_call_frame_info (int for_eh)
2248 {
2249   unsigned int i;
2250   dw_fde_ref fde;
2251   dw_cfi_ref cfi;
2252   char l1[20], l2[20], section_start_label[20];
2253   bool any_lsda_needed = false;
2254   char augmentation[6];
2255   int augmentation_size;
2256   int fde_encoding = DW_EH_PE_absptr;
2257   int per_encoding = DW_EH_PE_absptr;
2258   int lsda_encoding = DW_EH_PE_absptr;
2259   int return_reg;
2260
2261   /* Don't emit a CIE if there won't be any FDEs.  */
2262   if (fde_table_in_use == 0)
2263     return;
2264
2265   /* If we make FDEs linkonce, we may have to emit an empty label for
2266      an FDE that wouldn't otherwise be emitted.  We want to avoid
2267      having an FDE kept around when the function it refers to is
2268      discarded.  Example where this matters: a primary function
2269      template in C++ requires EH information, but an explicit
2270      specialization doesn't.  */
2271   if (TARGET_USES_WEAK_UNWIND_INFO
2272       && ! flag_asynchronous_unwind_tables
2273       && flag_exceptions
2274       && for_eh)
2275     for (i = 0; i < fde_table_in_use; i++)
2276       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2277           && !fde_table[i].uses_eh_lsda
2278           && ! DECL_WEAK (fde_table[i].decl))
2279         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2280                                       for_eh, /* empty */ 1);
2281
2282   /* If we don't have any functions we'll want to unwind out of, don't
2283      emit any EH unwind information.  Note that if exceptions aren't
2284      enabled, we won't have collected nothrow information, and if we
2285      asked for asynchronous tables, we always want this info.  */
2286   if (for_eh)
2287     {
2288       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2289
2290       for (i = 0; i < fde_table_in_use; i++)
2291         if (fde_table[i].uses_eh_lsda)
2292           any_eh_needed = any_lsda_needed = true;
2293         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2294           any_eh_needed = true;
2295         else if (! fde_table[i].nothrow
2296                  && ! fde_table[i].all_throwers_are_sibcalls)
2297           any_eh_needed = true;
2298
2299       if (! any_eh_needed)
2300         return;
2301     }
2302
2303   /* We're going to be generating comments, so turn on app.  */
2304   if (flag_debug_asm)
2305     app_enable ();
2306
2307   if (for_eh)
2308     switch_to_eh_frame_section ();
2309   else
2310     {
2311       if (!debug_frame_section)
2312         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2313                                            SECTION_DEBUG, NULL);
2314       switch_to_section (debug_frame_section);
2315     }
2316
2317   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2318   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2319
2320   /* Output the CIE.  */
2321   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2322   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2323   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2324     dw2_asm_output_data (4, 0xffffffff,
2325       "Initial length escape value indicating 64-bit DWARF extension");
2326   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2327                         "Length of Common Information Entry");
2328   ASM_OUTPUT_LABEL (asm_out_file, l1);
2329
2330   /* Now that the CIE pointer is PC-relative for EH,
2331      use 0 to identify the CIE.  */
2332   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2333                        (for_eh ? 0 : DWARF_CIE_ID),
2334                        "CIE Identifier Tag");
2335
2336   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2337
2338   augmentation[0] = 0;
2339   augmentation_size = 0;
2340   if (for_eh)
2341     {
2342       char *p;
2343
2344       /* Augmentation:
2345          z      Indicates that a uleb128 is present to size the
2346                 augmentation section.
2347          L      Indicates the encoding (and thus presence) of
2348                 an LSDA pointer in the FDE augmentation.
2349          R      Indicates a non-default pointer encoding for
2350                 FDE code pointers.
2351          P      Indicates the presence of an encoding + language
2352                 personality routine in the CIE augmentation.  */
2353
2354       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2355       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2356       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2357
2358       p = augmentation + 1;
2359       if (eh_personality_libfunc)
2360         {
2361           *p++ = 'P';
2362           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2363           assemble_external_libcall (eh_personality_libfunc);
2364         }
2365       if (any_lsda_needed)
2366         {
2367           *p++ = 'L';
2368           augmentation_size += 1;
2369         }
2370       if (fde_encoding != DW_EH_PE_absptr)
2371         {
2372           *p++ = 'R';
2373           augmentation_size += 1;
2374         }
2375       if (p > augmentation + 1)
2376         {
2377           augmentation[0] = 'z';
2378           *p = '\0';
2379         }
2380
2381       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2382       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2383         {
2384           int offset = (  4             /* Length */
2385                         + 4             /* CIE Id */
2386                         + 1             /* CIE version */
2387                         + strlen (augmentation) + 1     /* Augmentation */
2388                         + size_of_uleb128 (1)           /* Code alignment */
2389                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2390                         + 1             /* RA column */
2391                         + 1             /* Augmentation size */
2392                         + 1             /* Personality encoding */ );
2393           int pad = -offset & (PTR_SIZE - 1);
2394
2395           augmentation_size += pad;
2396
2397           /* Augmentations should be small, so there's scarce need to
2398              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2399           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2400         }
2401     }
2402
2403   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2404   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2405   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2406                                "CIE Data Alignment Factor");
2407
2408   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2409   if (DW_CIE_VERSION == 1)
2410     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2411   else
2412     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2413
2414   if (augmentation[0])
2415     {
2416       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2417       if (eh_personality_libfunc)
2418         {
2419           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2420                                eh_data_format_name (per_encoding));
2421           dw2_asm_output_encoded_addr_rtx (per_encoding,
2422                                            eh_personality_libfunc,
2423                                            true, NULL);
2424         }
2425
2426       if (any_lsda_needed)
2427         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2428                              eh_data_format_name (lsda_encoding));
2429
2430       if (fde_encoding != DW_EH_PE_absptr)
2431         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2432                              eh_data_format_name (fde_encoding));
2433     }
2434
2435   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2436     output_cfi (cfi, NULL, for_eh);
2437
2438   /* Pad the CIE out to an address sized boundary.  */
2439   ASM_OUTPUT_ALIGN (asm_out_file,
2440                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2441   ASM_OUTPUT_LABEL (asm_out_file, l2);
2442
2443   /* Loop through all of the FDE's.  */
2444   for (i = 0; i < fde_table_in_use; i++)
2445     {
2446       fde = &fde_table[i];
2447
2448       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2449       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2450           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2451           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2452           && !fde->uses_eh_lsda)
2453         continue;
2454
2455       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2456       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2457       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2458       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2459       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2460         dw2_asm_output_data (4, 0xffffffff,
2461                              "Initial length escape value indicating 64-bit DWARF extension");
2462       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2463                             "FDE Length");
2464       ASM_OUTPUT_LABEL (asm_out_file, l1);
2465
2466       if (for_eh)
2467         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2468       else
2469         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2470                                debug_frame_section, "FDE CIE offset");
2471
2472       if (for_eh)
2473         {
2474           if (fde->dw_fde_switched_sections)
2475             {
2476               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2477                                       fde->dw_fde_unlikely_section_label);
2478               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2479                                       fde->dw_fde_hot_section_label);
2480               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2481               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2482               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2483                                                "FDE initial location");
2484               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2485                                     fde->dw_fde_hot_section_end_label,
2486                                     fde->dw_fde_hot_section_label,
2487                                     "FDE address range");
2488               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2489                                                "FDE initial location");
2490               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2491                                     fde->dw_fde_unlikely_section_end_label,
2492                                     fde->dw_fde_unlikely_section_label,
2493                                     "FDE address range");
2494             }
2495           else
2496             {
2497               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2498               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2499               dw2_asm_output_encoded_addr_rtx (fde_encoding,
2500                                                sym_ref,
2501                                                false,
2502                                                "FDE initial location");
2503               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2504                                     fde->dw_fde_end, fde->dw_fde_begin,
2505                                     "FDE address range");
2506             }
2507         }
2508       else
2509         {
2510           if (fde->dw_fde_switched_sections)
2511             {
2512               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2513                                    fde->dw_fde_hot_section_label,
2514                                    "FDE initial location");
2515               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2516                                     fde->dw_fde_hot_section_end_label,
2517                                     fde->dw_fde_hot_section_label,
2518                                     "FDE address range");
2519               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2520                                    fde->dw_fde_unlikely_section_label,
2521                                    "FDE initial location");
2522               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2523                                     fde->dw_fde_unlikely_section_end_label,
2524                                     fde->dw_fde_unlikely_section_label,
2525                                     "FDE address range");
2526             }
2527           else
2528             {
2529               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2530                                    "FDE initial location");
2531               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2532                                     fde->dw_fde_end, fde->dw_fde_begin,
2533                                     "FDE address range");
2534             }
2535         }
2536
2537       if (augmentation[0])
2538         {
2539           if (any_lsda_needed)
2540             {
2541               int size = size_of_encoded_value (lsda_encoding);
2542
2543               if (lsda_encoding == DW_EH_PE_aligned)
2544                 {
2545                   int offset = (  4             /* Length */
2546                                 + 4             /* CIE offset */
2547                                 + 2 * size_of_encoded_value (fde_encoding)
2548                                 + 1             /* Augmentation size */ );
2549                   int pad = -offset & (PTR_SIZE - 1);
2550
2551                   size += pad;
2552                   gcc_assert (size_of_uleb128 (size) == 1);
2553                 }
2554
2555               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2556
2557               if (fde->uses_eh_lsda)
2558                 {
2559                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2560                                                fde->funcdef_number);
2561                   dw2_asm_output_encoded_addr_rtx (
2562                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2563                         false, "Language Specific Data Area");
2564                 }
2565               else
2566                 {
2567                   if (lsda_encoding == DW_EH_PE_aligned)
2568                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2569                   dw2_asm_output_data
2570                     (size_of_encoded_value (lsda_encoding), 0,
2571                      "Language Specific Data Area (none)");
2572                 }
2573             }
2574           else
2575             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2576         }
2577
2578       /* Loop through the Call Frame Instructions associated with
2579          this FDE.  */
2580       fde->dw_fde_current_label = fde->dw_fde_begin;
2581       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2582         output_cfi (cfi, fde, for_eh);
2583
2584       /* Pad the FDE out to an address sized boundary.  */
2585       ASM_OUTPUT_ALIGN (asm_out_file,
2586                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2587       ASM_OUTPUT_LABEL (asm_out_file, l2);
2588     }
2589
2590   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2591     dw2_asm_output_data (4, 0, "End of Table");
2592 #ifdef MIPS_DEBUGGING_INFO
2593   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2594      get a value of 0.  Putting .align 0 after the label fixes it.  */
2595   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2596 #endif
2597
2598   /* Turn off app to make assembly quicker.  */
2599   if (flag_debug_asm)
2600     app_disable ();
2601 }
2602
2603 /* Output a marker (i.e. a label) for the beginning of a function, before
2604    the prologue.  */
2605
2606 void
2607 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2608                           const char *file ATTRIBUTE_UNUSED)
2609 {
2610   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2611   char * dup_label;
2612   dw_fde_ref fde;
2613
2614   current_function_func_begin_label = NULL;
2615
2616 #ifdef TARGET_UNWIND_INFO
2617   /* ??? current_function_func_begin_label is also used by except.c
2618      for call-site information.  We must emit this label if it might
2619      be used.  */
2620   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2621       && ! dwarf2out_do_frame ())
2622     return;
2623 #else
2624   if (! dwarf2out_do_frame ())
2625     return;
2626 #endif
2627
2628   switch_to_section (function_section (current_function_decl));
2629   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2630                                current_function_funcdef_no);
2631   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2632                           current_function_funcdef_no);
2633   dup_label = xstrdup (label);
2634   current_function_func_begin_label = dup_label;
2635
2636 #ifdef TARGET_UNWIND_INFO
2637   /* We can elide the fde allocation if we're not emitting debug info.  */
2638   if (! dwarf2out_do_frame ())
2639     return;
2640 #endif
2641
2642   /* Expand the fde table if necessary.  */
2643   if (fde_table_in_use == fde_table_allocated)
2644     {
2645       fde_table_allocated += FDE_TABLE_INCREMENT;
2646       fde_table = ggc_realloc (fde_table,
2647                                fde_table_allocated * sizeof (dw_fde_node));
2648       memset (fde_table + fde_table_in_use, 0,
2649               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2650     }
2651
2652   /* Record the FDE associated with this function.  */
2653   current_funcdef_fde = fde_table_in_use;
2654
2655   /* Add the new FDE at the end of the fde_table.  */
2656   fde = &fde_table[fde_table_in_use++];
2657   fde->decl = current_function_decl;
2658   fde->dw_fde_begin = dup_label;
2659   fde->dw_fde_current_label = dup_label;
2660   fde->dw_fde_hot_section_label = NULL;
2661   fde->dw_fde_hot_section_end_label = NULL;
2662   fde->dw_fde_unlikely_section_label = NULL;
2663   fde->dw_fde_unlikely_section_end_label = NULL;
2664   fde->dw_fde_switched_sections = false;
2665   fde->dw_fde_end = NULL;
2666   fde->dw_fde_cfi = NULL;
2667   fde->funcdef_number = current_function_funcdef_no;
2668   fde->nothrow = TREE_NOTHROW (current_function_decl);
2669   fde->uses_eh_lsda = crtl->uses_eh_lsda;
2670   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
2671
2672   args_size = old_args_size = 0;
2673
2674   /* We only want to output line number information for the genuine dwarf2
2675      prologue case, not the eh frame case.  */
2676 #ifdef DWARF2_DEBUGGING_INFO
2677   if (file)
2678     dwarf2out_source_line (line, file);
2679 #endif
2680 }
2681
2682 /* Output a marker (i.e. a label) for the absolute end of the generated code
2683    for a function definition.  This gets called *after* the epilogue code has
2684    been generated.  */
2685
2686 void
2687 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2688                         const char *file ATTRIBUTE_UNUSED)
2689 {
2690   dw_fde_ref fde;
2691   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2692
2693   /* Output a label to mark the endpoint of the code generated for this
2694      function.  */
2695   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2696                                current_function_funcdef_no);
2697   ASM_OUTPUT_LABEL (asm_out_file, label);
2698   fde = current_fde ();
2699   gcc_assert (fde != NULL);
2700   fde->dw_fde_end = xstrdup (label);
2701 }
2702
2703 void
2704 dwarf2out_frame_init (void)
2705 {
2706   /* Allocate the initial hunk of the fde_table.  */
2707   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2708   fde_table_allocated = FDE_TABLE_INCREMENT;
2709   fde_table_in_use = 0;
2710
2711   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2712      sake of lookup_cfa.  */
2713
2714   /* On entry, the Canonical Frame Address is at SP.  */
2715   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2716
2717 #ifdef DWARF2_UNWIND_INFO
2718   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2719     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2720 #endif
2721 }
2722
2723 void
2724 dwarf2out_frame_finish (void)
2725 {
2726   /* Output call frame information.  */
2727   if (DWARF2_FRAME_INFO)
2728     output_call_frame_info (0);
2729
2730 #ifndef TARGET_UNWIND_INFO
2731   /* Output another copy for the unwinder.  */
2732   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2733     output_call_frame_info (1);
2734 #endif
2735 }
2736
2737 /* Note that the current function section is being used for code.  */
2738
2739 static void
2740 dwarf2out_note_section_used (void)
2741 {
2742   section *sec = current_function_section ();
2743   if (sec == text_section)
2744     text_section_used = true;
2745   else if (sec == cold_text_section)
2746     cold_text_section_used = true;
2747 }
2748
2749 void
2750 dwarf2out_switch_text_section (void)
2751 {
2752   dw_fde_ref fde = current_fde ();
2753
2754   gcc_assert (cfun && fde);
2755
2756   fde->dw_fde_switched_sections = true;
2757   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
2758   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
2759   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
2760   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
2761   have_multiple_function_sections = true;
2762
2763   /* Reset the current label on switching text sections, so that we
2764      don't attempt to advance_loc4 between labels in different sections.  */
2765   fde->dw_fde_current_label = NULL;
2766
2767   /* There is no need to mark used sections when not debugging.  */
2768   if (cold_text_section != NULL)
2769     dwarf2out_note_section_used ();
2770 }
2771 #endif
2772 \f
2773 /* And now, the subset of the debugging information support code necessary
2774    for emitting location expressions.  */
2775
2776 /* Data about a single source file.  */
2777 struct dwarf_file_data GTY(())
2778 {
2779   const char * filename;
2780   int emitted_number;
2781 };
2782
2783 /* We need some way to distinguish DW_OP_addr with a direct symbol
2784    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2785 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2786
2787
2788 typedef struct dw_val_struct *dw_val_ref;
2789 typedef struct die_struct *dw_die_ref;
2790 typedef const struct die_struct *const_dw_die_ref;
2791 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2792 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2793
2794 /* Each DIE may have a series of attribute/value pairs.  Values
2795    can take on several forms.  The forms that are used in this
2796    implementation are listed below.  */
2797
2798 enum dw_val_class
2799 {
2800   dw_val_class_addr,
2801   dw_val_class_offset,
2802   dw_val_class_loc,
2803   dw_val_class_loc_list,
2804   dw_val_class_range_list,
2805   dw_val_class_const,
2806   dw_val_class_unsigned_const,
2807   dw_val_class_long_long,
2808   dw_val_class_vec,
2809   dw_val_class_flag,
2810   dw_val_class_die_ref,
2811   dw_val_class_fde_ref,
2812   dw_val_class_lbl_id,
2813   dw_val_class_lineptr,
2814   dw_val_class_str,
2815   dw_val_class_macptr,
2816   dw_val_class_file
2817 };
2818
2819 /* Describe a double word constant value.  */
2820 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2821
2822 typedef struct dw_long_long_struct GTY(())
2823 {
2824   unsigned long hi;
2825   unsigned long low;
2826 }
2827 dw_long_long_const;
2828
2829 /* Describe a floating point constant value, or a vector constant value.  */
2830
2831 typedef struct dw_vec_struct GTY(())
2832 {
2833   unsigned char * GTY((length ("%h.length"))) array;
2834   unsigned length;
2835   unsigned elt_size;
2836 }
2837 dw_vec_const;
2838
2839 /* The dw_val_node describes an attribute's value, as it is
2840    represented internally.  */
2841
2842 typedef struct dw_val_struct GTY(())
2843 {
2844   enum dw_val_class val_class;
2845   union dw_val_struct_union
2846     {
2847       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2848       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2849       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2850       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2851       HOST_WIDE_INT GTY ((default)) val_int;
2852       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2853       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2854       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2855       struct dw_val_die_union
2856         {
2857           dw_die_ref die;
2858           int external;
2859         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2860       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2861       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2862       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2863       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2864       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2865     }
2866   GTY ((desc ("%1.val_class"))) v;
2867 }
2868 dw_val_node;
2869
2870 /* Locations in memory are described using a sequence of stack machine
2871    operations.  */
2872
2873 typedef struct dw_loc_descr_struct GTY(())
2874 {
2875   dw_loc_descr_ref dw_loc_next;
2876   enum dwarf_location_atom dw_loc_opc;
2877   dw_val_node dw_loc_oprnd1;
2878   dw_val_node dw_loc_oprnd2;
2879   int dw_loc_addr;
2880 }
2881 dw_loc_descr_node;
2882
2883 /* Location lists are ranges + location descriptions for that range,
2884    so you can track variables that are in different places over
2885    their entire life.  */
2886 typedef struct dw_loc_list_struct GTY(())
2887 {
2888   dw_loc_list_ref dw_loc_next;
2889   const char *begin; /* Label for begin address of range */
2890   const char *end;  /* Label for end address of range */
2891   char *ll_symbol; /* Label for beginning of location list.
2892                       Only on head of list */
2893   const char *section; /* Section this loclist is relative to */
2894   dw_loc_descr_ref expr;
2895 } dw_loc_list_node;
2896
2897 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2898
2899 static const char *dwarf_stack_op_name (unsigned);
2900 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2901                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2902 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2903 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2904 static unsigned long size_of_locs (dw_loc_descr_ref);
2905 static void output_loc_operands (dw_loc_descr_ref);
2906 static void output_loc_sequence (dw_loc_descr_ref);
2907
2908 /* Convert a DWARF stack opcode into its string name.  */
2909
2910 static const char *
2911 dwarf_stack_op_name (unsigned int op)
2912 {
2913   switch (op)
2914     {
2915     case DW_OP_addr:
2916     case INTERNAL_DW_OP_tls_addr:
2917       return "DW_OP_addr";
2918     case DW_OP_deref:
2919       return "DW_OP_deref";
2920     case DW_OP_const1u:
2921       return "DW_OP_const1u";
2922     case DW_OP_const1s:
2923       return "DW_OP_const1s";
2924     case DW_OP_const2u:
2925       return "DW_OP_const2u";
2926     case DW_OP_const2s:
2927       return "DW_OP_const2s";
2928     case DW_OP_const4u:
2929       return "DW_OP_const4u";
2930     case DW_OP_const4s:
2931       return "DW_OP_const4s";
2932     case DW_OP_const8u:
2933       return "DW_OP_const8u";
2934     case DW_OP_const8s:
2935       return "DW_OP_const8s";
2936     case DW_OP_constu:
2937       return "DW_OP_constu";
2938     case DW_OP_consts:
2939       return "DW_OP_consts";
2940     case DW_OP_dup:
2941       return "DW_OP_dup";
2942     case DW_OP_drop:
2943       return "DW_OP_drop";
2944     case DW_OP_over:
2945       return "DW_OP_over";
2946     case DW_OP_pick:
2947       return "DW_OP_pick";
2948     case DW_OP_swap:
2949       return "DW_OP_swap";
2950     case DW_OP_rot:
2951       return "DW_OP_rot";
2952     case DW_OP_xderef:
2953       return "DW_OP_xderef";
2954     case DW_OP_abs:
2955       return "DW_OP_abs";
2956     case DW_OP_and:
2957       return "DW_OP_and";
2958     case DW_OP_div:
2959       return "DW_OP_div";
2960     case DW_OP_minus:
2961       return "DW_OP_minus";
2962     case DW_OP_mod:
2963       return "DW_OP_mod";
2964     case DW_OP_mul:
2965       return "DW_OP_mul";
2966     case DW_OP_neg:
2967       return "DW_OP_neg";
2968     case DW_OP_not:
2969       return "DW_OP_not";
2970     case DW_OP_or:
2971       return "DW_OP_or";
2972     case DW_OP_plus:
2973       return "DW_OP_plus";
2974     case DW_OP_plus_uconst:
2975       return "DW_OP_plus_uconst";
2976     case DW_OP_shl:
2977       return "DW_OP_shl";
2978     case DW_OP_shr:
2979       return "DW_OP_shr";
2980     case DW_OP_shra:
2981       return "DW_OP_shra";
2982     case DW_OP_xor:
2983       return "DW_OP_xor";
2984     case DW_OP_bra:
2985       return "DW_OP_bra";
2986     case DW_OP_eq:
2987       return "DW_OP_eq";
2988     case DW_OP_ge:
2989       return "DW_OP_ge";
2990     case DW_OP_gt:
2991       return "DW_OP_gt";
2992     case DW_OP_le:
2993       return "DW_OP_le";
2994     case DW_OP_lt:
2995       return "DW_OP_lt";
2996     case DW_OP_ne:
2997       return "DW_OP_ne";
2998     case DW_OP_skip:
2999       return "DW_OP_skip";
3000     case DW_OP_lit0:
3001       return "DW_OP_lit0";
3002     case DW_OP_lit1:
3003       return "DW_OP_lit1";
3004     case DW_OP_lit2:
3005       return "DW_OP_lit2";
3006     case DW_OP_lit3:
3007       return "DW_OP_lit3";
3008     case DW_OP_lit4:
3009       return "DW_OP_lit4";
3010     case DW_OP_lit5:
3011       return "DW_OP_lit5";
3012     case DW_OP_lit6:
3013       return "DW_OP_lit6";
3014     case DW_OP_lit7:
3015       return "DW_OP_lit7";
3016     case DW_OP_lit8:
3017       return "DW_OP_lit8";
3018     case DW_OP_lit9:
3019       return "DW_OP_lit9";
3020     case DW_OP_lit10:
3021       return "DW_OP_lit10";
3022     case DW_OP_lit11:
3023       return "DW_OP_lit11";
3024     case DW_OP_lit12:
3025       return "DW_OP_lit12";
3026     case DW_OP_lit13:
3027       return "DW_OP_lit13";
3028     case DW_OP_lit14:
3029       return "DW_OP_lit14";
3030     case DW_OP_lit15:
3031       return "DW_OP_lit15";
3032     case DW_OP_lit16:
3033       return "DW_OP_lit16";
3034     case DW_OP_lit17:
3035       return "DW_OP_lit17";
3036     case DW_OP_lit18:
3037       return "DW_OP_lit18";
3038     case DW_OP_lit19:
3039       return "DW_OP_lit19";
3040     case DW_OP_lit20:
3041       return "DW_OP_lit20";
3042     case DW_OP_lit21:
3043       return "DW_OP_lit21";
3044     case DW_OP_lit22:
3045       return "DW_OP_lit22";
3046     case DW_OP_lit23:
3047       return "DW_OP_lit23";
3048     case DW_OP_lit24:
3049       return "DW_OP_lit24";
3050     case DW_OP_lit25:
3051       return "DW_OP_lit25";
3052     case DW_OP_lit26:
3053       return "DW_OP_lit26";
3054     case DW_OP_lit27:
3055       return "DW_OP_lit27";
3056     case DW_OP_lit28:
3057       return "DW_OP_lit28";
3058     case DW_OP_lit29:
3059       return "DW_OP_lit29";
3060     case DW_OP_lit30:
3061       return "DW_OP_lit30";
3062     case DW_OP_lit31:
3063       return "DW_OP_lit31";
3064     case DW_OP_reg0:
3065       return "DW_OP_reg0";
3066     case DW_OP_reg1:
3067       return "DW_OP_reg1";
3068     case DW_OP_reg2:
3069       return "DW_OP_reg2";
3070     case DW_OP_reg3:
3071       return "DW_OP_reg3";
3072     case DW_OP_reg4:
3073       return "DW_OP_reg4";
3074     case DW_OP_reg5:
3075       return "DW_OP_reg5";
3076     case DW_OP_reg6:
3077       return "DW_OP_reg6";
3078     case DW_OP_reg7:
3079       return "DW_OP_reg7";
3080     case DW_OP_reg8:
3081       return "DW_OP_reg8";
3082     case DW_OP_reg9:
3083       return "DW_OP_reg9";
3084     case DW_OP_reg10:
3085       return "DW_OP_reg10";
3086     case DW_OP_reg11:
3087       return "DW_OP_reg11";
3088     case DW_OP_reg12:
3089       return "DW_OP_reg12";
3090     case DW_OP_reg13:
3091       return "DW_OP_reg13";
3092     case DW_OP_reg14:
3093       return "DW_OP_reg14";
3094     case DW_OP_reg15:
3095       return "DW_OP_reg15";
3096     case DW_OP_reg16:
3097       return "DW_OP_reg16";
3098     case DW_OP_reg17:
3099       return "DW_OP_reg17";
3100     case DW_OP_reg18:
3101       return "DW_OP_reg18";
3102     case DW_OP_reg19:
3103       return "DW_OP_reg19";
3104     case DW_OP_reg20:
3105       return "DW_OP_reg20";
3106     case DW_OP_reg21:
3107       return "DW_OP_reg21";
3108     case DW_OP_reg22:
3109       return "DW_OP_reg22";
3110     case DW_OP_reg23:
3111       return "DW_OP_reg23";
3112     case DW_OP_reg24:
3113       return "DW_OP_reg24";
3114     case DW_OP_reg25:
3115       return "DW_OP_reg25";
3116     case DW_OP_reg26:
3117       return "DW_OP_reg26";
3118     case DW_OP_reg27:
3119       return "DW_OP_reg27";
3120     case DW_OP_reg28:
3121       return "DW_OP_reg28";
3122     case DW_OP_reg29:
3123       return "DW_OP_reg29";
3124     case DW_OP_reg30:
3125       return "DW_OP_reg30";
3126     case DW_OP_reg31:
3127       return "DW_OP_reg31";
3128     case DW_OP_breg0:
3129       return "DW_OP_breg0";
3130     case DW_OP_breg1:
3131       return "DW_OP_breg1";
3132     case DW_OP_breg2:
3133       return "DW_OP_breg2";
3134     case DW_OP_breg3:
3135       return "DW_OP_breg3";
3136     case DW_OP_breg4:
3137       return "DW_OP_breg4";
3138     case DW_OP_breg5:
3139       return "DW_OP_breg5";
3140     case DW_OP_breg6:
3141       return "DW_OP_breg6";
3142     case DW_OP_breg7:
3143       return "DW_OP_breg7";
3144     case DW_OP_breg8:
3145       return "DW_OP_breg8";
3146     case DW_OP_breg9:
3147       return "DW_OP_breg9";
3148     case DW_OP_breg10:
3149       return "DW_OP_breg10";
3150     case DW_OP_breg11:
3151       return "DW_OP_breg11";
3152     case DW_OP_breg12:
3153       return "DW_OP_breg12";
3154     case DW_OP_breg13:
3155       return "DW_OP_breg13";
3156     case DW_OP_breg14:
3157       return "DW_OP_breg14";
3158     case DW_OP_breg15:
3159       return "DW_OP_breg15";
3160     case DW_OP_breg16:
3161       return "DW_OP_breg16";
3162     case DW_OP_breg17:
3163       return "DW_OP_breg17";
3164     case DW_OP_breg18:
3165       return "DW_OP_breg18";
3166     case DW_OP_breg19:
3167       return "DW_OP_breg19";
3168     case DW_OP_breg20:
3169       return "DW_OP_breg20";
3170     case DW_OP_breg21:
3171       return "DW_OP_breg21";
3172     case DW_OP_breg22:
3173       return "DW_OP_breg22";
3174     case DW_OP_breg23:
3175       return "DW_OP_breg23";
3176     case DW_OP_breg24:
3177       return "DW_OP_breg24";
3178     case DW_OP_breg25:
3179       return "DW_OP_breg25";
3180     case DW_OP_breg26:
3181       return "DW_OP_breg26";
3182     case DW_OP_breg27:
3183       return "DW_OP_breg27";
3184     case DW_OP_breg28:
3185       return "DW_OP_breg28";
3186     case DW_OP_breg29:
3187       return "DW_OP_breg29";
3188     case DW_OP_breg30:
3189       return "DW_OP_breg30";
3190     case DW_OP_breg31:
3191       return "DW_OP_breg31";
3192     case DW_OP_regx:
3193       return "DW_OP_regx";
3194     case DW_OP_fbreg:
3195       return "DW_OP_fbreg";
3196     case DW_OP_bregx:
3197       return "DW_OP_bregx";
3198     case DW_OP_piece:
3199       return "DW_OP_piece";
3200     case DW_OP_deref_size:
3201       return "DW_OP_deref_size";
3202     case DW_OP_xderef_size:
3203       return "DW_OP_xderef_size";
3204     case DW_OP_nop:
3205       return "DW_OP_nop";
3206     case DW_OP_push_object_address:
3207       return "DW_OP_push_object_address";
3208     case DW_OP_call2:
3209       return "DW_OP_call2";
3210     case DW_OP_call4:
3211       return "DW_OP_call4";
3212     case DW_OP_call_ref:
3213       return "DW_OP_call_ref";
3214     case DW_OP_GNU_push_tls_address:
3215       return "DW_OP_GNU_push_tls_address";
3216     case DW_OP_GNU_uninit:
3217       return "DW_OP_GNU_uninit";
3218     default:
3219       return "OP_<unknown>";
3220     }
3221 }
3222
3223 /* Return a pointer to a newly allocated location description.  Location
3224    descriptions are simple expression terms that can be strung
3225    together to form more complicated location (address) descriptions.  */
3226
3227 static inline dw_loc_descr_ref
3228 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3229                unsigned HOST_WIDE_INT oprnd2)
3230 {
3231   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3232
3233   descr->dw_loc_opc = op;
3234   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3235   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3236   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3237   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3238
3239   return descr;
3240 }
3241
3242 /* Add a location description term to a location description expression.  */
3243
3244 static inline void
3245 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3246 {
3247   dw_loc_descr_ref *d;
3248
3249   /* Find the end of the chain.  */
3250   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3251     ;
3252
3253   *d = descr;
3254 }
3255
3256 /* Return the size of a location descriptor.  */
3257
3258 static unsigned long
3259 size_of_loc_descr (dw_loc_descr_ref loc)
3260 {
3261   unsigned long size = 1;
3262
3263   switch (loc->dw_loc_opc)
3264     {
3265     case DW_OP_addr:
3266     case INTERNAL_DW_OP_tls_addr:
3267       size += DWARF2_ADDR_SIZE;
3268       break;
3269     case DW_OP_const1u:
3270     case DW_OP_const1s:
3271       size += 1;
3272       break;
3273     case DW_OP_const2u:
3274     case DW_OP_const2s:
3275       size += 2;
3276       break;
3277     case DW_OP_const4u:
3278     case DW_OP_const4s:
3279       size += 4;
3280       break;
3281     case DW_OP_const8u:
3282     case DW_OP_const8s:
3283       size += 8;
3284       break;
3285     case DW_OP_constu:
3286       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3287       break;
3288     case DW_OP_consts:
3289       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3290       break;
3291     case DW_OP_pick:
3292       size += 1;
3293       break;
3294     case DW_OP_plus_uconst:
3295       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3296       break;
3297     case DW_OP_skip:
3298     case DW_OP_bra:
3299       size += 2;
3300       break;
3301     case DW_OP_breg0:
3302     case DW_OP_breg1:
3303     case DW_OP_breg2:
3304     case DW_OP_breg3:
3305     case DW_OP_breg4:
3306     case DW_OP_breg5:
3307     case DW_OP_breg6:
3308     case DW_OP_breg7:
3309     case DW_OP_breg8:
3310     case DW_OP_breg9:
3311     case DW_OP_breg10:
3312     case DW_OP_breg11:
3313     case DW_OP_breg12:
3314     case DW_OP_breg13:
3315     case DW_OP_breg14:
3316     case DW_OP_breg15:
3317     case DW_OP_breg16:
3318     case DW_OP_breg17:
3319     case DW_OP_breg18:
3320     case DW_OP_breg19:
3321     case DW_OP_breg20:
3322     case DW_OP_breg21:
3323     case DW_OP_breg22:
3324     case DW_OP_breg23:
3325     case DW_OP_breg24:
3326     case DW_OP_breg25:
3327     case DW_OP_breg26:
3328     case DW_OP_breg27:
3329     case DW_OP_breg28:
3330     case DW_OP_breg29:
3331     case DW_OP_breg30:
3332     case DW_OP_breg31:
3333       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3334       break;
3335     case DW_OP_regx:
3336       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3337       break;
3338     case DW_OP_fbreg:
3339       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3340       break;
3341     case DW_OP_bregx:
3342       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3343       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3344       break;
3345     case DW_OP_piece:
3346       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3347       break;
3348     case DW_OP_deref_size:
3349     case DW_OP_xderef_size:
3350       size += 1;
3351       break;
3352     case DW_OP_call2:
3353       size += 2;
3354       break;
3355     case DW_OP_call4:
3356       size += 4;
3357       break;
3358     case DW_OP_call_ref:
3359       size += DWARF2_ADDR_SIZE;
3360       break;
3361     default:
3362       break;
3363     }
3364
3365   return size;
3366 }
3367
3368 /* Return the size of a series of location descriptors.  */
3369
3370 static unsigned long
3371 size_of_locs (dw_loc_descr_ref loc)
3372 {
3373   dw_loc_descr_ref l;
3374   unsigned long size;
3375
3376   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3377      field, to avoid writing to a PCH file.  */
3378   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3379     {
3380       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3381         break;
3382       size += size_of_loc_descr (l);
3383     }
3384   if (! l)
3385     return size;
3386
3387   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3388     {
3389       l->dw_loc_addr = size;
3390       size += size_of_loc_descr (l);
3391     }
3392
3393   return size;
3394 }
3395
3396 /* Output location description stack opcode's operands (if any).  */
3397
3398 static void
3399 output_loc_operands (dw_loc_descr_ref loc)
3400 {
3401   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3402   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3403
3404   switch (loc->dw_loc_opc)
3405     {
3406 #ifdef DWARF2_DEBUGGING_INFO
3407     case DW_OP_addr:
3408       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3409       break;
3410     case DW_OP_const2u:
3411     case DW_OP_const2s:
3412       dw2_asm_output_data (2, val1->v.val_int, NULL);
3413       break;
3414     case DW_OP_const4u:
3415     case DW_OP_const4s:
3416       dw2_asm_output_data (4, val1->v.val_int, NULL);
3417       break;
3418     case DW_OP_const8u:
3419     case DW_OP_const8s:
3420       gcc_assert (HOST_BITS_PER_LONG >= 64);
3421       dw2_asm_output_data (8, val1->v.val_int, NULL);
3422       break;
3423     case DW_OP_skip:
3424     case DW_OP_bra:
3425       {
3426         int offset;
3427
3428         gcc_assert (val1->val_class == dw_val_class_loc);
3429         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3430
3431         dw2_asm_output_data (2, offset, NULL);
3432       }
3433       break;
3434 #else
3435     case DW_OP_addr:
3436     case DW_OP_const2u:
3437     case DW_OP_const2s:
3438     case DW_OP_const4u:
3439     case DW_OP_const4s:
3440     case DW_OP_const8u:
3441     case DW_OP_const8s:
3442     case DW_OP_skip:
3443     case DW_OP_bra:
3444       /* We currently don't make any attempt to make sure these are
3445          aligned properly like we do for the main unwind info, so
3446          don't support emitting things larger than a byte if we're
3447          only doing unwinding.  */
3448       gcc_unreachable ();
3449 #endif
3450     case DW_OP_const1u:
3451     case DW_OP_const1s:
3452       dw2_asm_output_data (1, val1->v.val_int, NULL);
3453       break;
3454     case DW_OP_constu:
3455       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3456       break;
3457     case DW_OP_consts:
3458       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3459       break;
3460     case DW_OP_pick:
3461       dw2_asm_output_data (1, val1->v.val_int, NULL);
3462       break;
3463     case DW_OP_plus_uconst:
3464       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3465       break;
3466     case DW_OP_breg0:
3467     case DW_OP_breg1:
3468     case DW_OP_breg2:
3469     case DW_OP_breg3:
3470     case DW_OP_breg4:
3471     case DW_OP_breg5:
3472     case DW_OP_breg6:
3473     case DW_OP_breg7:
3474     case DW_OP_breg8:
3475     case DW_OP_breg9:
3476     case DW_OP_breg10:
3477     case DW_OP_breg11:
3478     case DW_OP_breg12:
3479     case DW_OP_breg13:
3480     case DW_OP_breg14:
3481     case DW_OP_breg15:
3482     case DW_OP_breg16:
3483     case DW_OP_breg17:
3484     case DW_OP_breg18:
3485     case DW_OP_breg19:
3486     case DW_OP_breg20:
3487     case DW_OP_breg21:
3488     case DW_OP_breg22:
3489     case DW_OP_breg23:
3490     case DW_OP_breg24:
3491     case DW_OP_breg25:
3492     case DW_OP_breg26:
3493     case DW_OP_breg27:
3494     case DW_OP_breg28:
3495     case DW_OP_breg29:
3496     case DW_OP_breg30:
3497     case DW_OP_breg31:
3498       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3499       break;
3500     case DW_OP_regx:
3501       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3502       break;
3503     case DW_OP_fbreg:
3504       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3505       break;
3506     case DW_OP_bregx:
3507       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3508       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3509       break;
3510     case DW_OP_piece:
3511       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3512       break;
3513     case DW_OP_deref_size:
3514     case DW_OP_xderef_size:
3515       dw2_asm_output_data (1, val1->v.val_int, NULL);
3516       break;
3517
3518     case INTERNAL_DW_OP_tls_addr:
3519       if (targetm.asm_out.output_dwarf_dtprel)
3520         {
3521           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3522                                                DWARF2_ADDR_SIZE,
3523                                                val1->v.val_addr);
3524           fputc ('\n', asm_out_file);
3525         }
3526       else
3527         gcc_unreachable ();
3528       break;
3529
3530     default:
3531       /* Other codes have no operands.  */
3532       break;
3533     }
3534 }
3535
3536 /* Output a sequence of location operations.  */
3537
3538 static void
3539 output_loc_sequence (dw_loc_descr_ref loc)
3540 {
3541   for (; loc != NULL; loc = loc->dw_loc_next)
3542     {
3543       /* Output the opcode.  */
3544       dw2_asm_output_data (1, loc->dw_loc_opc,
3545                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3546
3547       /* Output the operand(s) (if any).  */
3548       output_loc_operands (loc);
3549     }
3550 }
3551
3552 /* This routine will generate the correct assembly data for a location
3553    description based on a cfi entry with a complex address.  */
3554
3555 static void
3556 output_cfa_loc (dw_cfi_ref cfi)
3557 {
3558   dw_loc_descr_ref loc;
3559   unsigned long size;
3560
3561   /* Output the size of the block.  */
3562   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3563   size = size_of_locs (loc);
3564   dw2_asm_output_data_uleb128 (size, NULL);
3565
3566   /* Now output the operations themselves.  */
3567   output_loc_sequence (loc);
3568 }
3569
3570 /* This function builds a dwarf location descriptor sequence from a
3571    dw_cfa_location, adding the given OFFSET to the result of the
3572    expression.  */
3573
3574 static struct dw_loc_descr_struct *
3575 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3576 {
3577   struct dw_loc_descr_struct *head, *tmp;
3578
3579   offset += cfa->offset;
3580
3581   if (cfa->indirect)
3582     {
3583       if (cfa->base_offset)
3584         {
3585           if (cfa->reg <= 31)
3586             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3587           else
3588             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3589         }
3590       else if (cfa->reg <= 31)
3591         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3592       else
3593         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3594
3595       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3596       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3597       add_loc_descr (&head, tmp);
3598       if (offset != 0)
3599         {
3600           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3601           add_loc_descr (&head, tmp);
3602         }
3603     }
3604   else
3605     {
3606       if (offset == 0)
3607         if (cfa->reg <= 31)
3608           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3609         else
3610           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3611       else if (cfa->reg <= 31)
3612         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3613       else
3614         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3615     }
3616
3617   return head;
3618 }
3619
3620 /* This function fills in aa dw_cfa_location structure from a dwarf location
3621    descriptor sequence.  */
3622
3623 static void
3624 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3625 {
3626   struct dw_loc_descr_struct *ptr;
3627   cfa->offset = 0;
3628   cfa->base_offset = 0;
3629   cfa->indirect = 0;
3630   cfa->reg = -1;
3631
3632   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3633     {
3634       enum dwarf_location_atom op = ptr->dw_loc_opc;
3635
3636       switch (op)
3637         {
3638         case DW_OP_reg0:
3639         case DW_OP_reg1:
3640         case DW_OP_reg2:
3641         case DW_OP_reg3:
3642         case DW_OP_reg4:
3643         case DW_OP_reg5:
3644         case DW_OP_reg6:
3645         case DW_OP_reg7:
3646         case DW_OP_reg8:
3647         case DW_OP_reg9:
3648         case DW_OP_reg10:
3649         case DW_OP_reg11:
3650         case DW_OP_reg12:
3651         case DW_OP_reg13:
3652         case DW_OP_reg14:
3653         case DW_OP_reg15:
3654         case DW_OP_reg16:
3655         case DW_OP_reg17:
3656         case DW_OP_reg18:
3657         case DW_OP_reg19:
3658         case DW_OP_reg20:
3659         case DW_OP_reg21:
3660         case DW_OP_reg22:
3661         case DW_OP_reg23:
3662         case DW_OP_reg24:
3663         case DW_OP_reg25:
3664         case DW_OP_reg26:
3665         case DW_OP_reg27:
3666         case DW_OP_reg28:
3667         case DW_OP_reg29:
3668         case DW_OP_reg30:
3669         case DW_OP_reg31:
3670           cfa->reg = op - DW_OP_reg0;
3671           break;
3672         case DW_OP_regx:
3673           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3674           break;
3675         case DW_OP_breg0:
3676         case DW_OP_breg1:
3677         case DW_OP_breg2:
3678         case DW_OP_breg3:
3679         case DW_OP_breg4:
3680         case DW_OP_breg5:
3681         case DW_OP_breg6:
3682         case DW_OP_breg7:
3683         case DW_OP_breg8:
3684         case DW_OP_breg9:
3685         case DW_OP_breg10:
3686         case DW_OP_breg11:
3687         case DW_OP_breg12:
3688         case DW_OP_breg13:
3689         case DW_OP_breg14:
3690         case DW_OP_breg15:
3691         case DW_OP_breg16:
3692         case DW_OP_breg17:
3693         case DW_OP_breg18:
3694         case DW_OP_breg19:
3695         case DW_OP_breg20:
3696         case DW_OP_breg21:
3697         case DW_OP_breg22:
3698         case DW_OP_breg23:
3699         case DW_OP_breg24:
3700         case DW_OP_breg25:
3701         case DW_OP_breg26:
3702         case DW_OP_breg27:
3703         case DW_OP_breg28:
3704         case DW_OP_breg29:
3705         case DW_OP_breg30:
3706         case DW_OP_breg31:
3707           cfa->reg = op - DW_OP_breg0;
3708           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3709           break;
3710         case DW_OP_bregx:
3711           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3712           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3713           break;
3714         case DW_OP_deref:
3715           cfa->indirect = 1;
3716           break;
3717         case DW_OP_plus_uconst:
3718           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3719           break;
3720         default:
3721           internal_error ("DW_LOC_OP %s not implemented",
3722                           dwarf_stack_op_name (ptr->dw_loc_opc));
3723         }
3724     }
3725 }
3726 #endif /* .debug_frame support */
3727 \f
3728 /* And now, the support for symbolic debugging information.  */
3729 #ifdef DWARF2_DEBUGGING_INFO
3730
3731 /* .debug_str support.  */
3732 static int output_indirect_string (void **, void *);
3733
3734 static void dwarf2out_init (const char *);
3735 static void dwarf2out_finish (const char *);
3736 static void dwarf2out_define (unsigned int, const char *);
3737 static void dwarf2out_undef (unsigned int, const char *);
3738 static void dwarf2out_start_source_file (unsigned, const char *);
3739 static void dwarf2out_end_source_file (unsigned);
3740 static void dwarf2out_begin_block (unsigned, unsigned);
3741 static void dwarf2out_end_block (unsigned, unsigned);
3742 static bool dwarf2out_ignore_block (const_tree);
3743 static void dwarf2out_global_decl (tree);
3744 static void dwarf2out_type_decl (tree, int);
3745 static void dwarf2out_imported_module_or_decl (tree, tree);
3746 static void dwarf2out_abstract_function (tree);
3747 static void dwarf2out_var_location (rtx);
3748 static void dwarf2out_begin_function (tree);
3749
3750 /* The debug hooks structure.  */
3751
3752 const struct gcc_debug_hooks dwarf2_debug_hooks =
3753 {
3754   dwarf2out_init,
3755   dwarf2out_finish,
3756   dwarf2out_define,
3757   dwarf2out_undef,
3758   dwarf2out_start_source_file,
3759   dwarf2out_end_source_file,
3760   dwarf2out_begin_block,
3761   dwarf2out_end_block,
3762   dwarf2out_ignore_block,
3763   dwarf2out_source_line,
3764   dwarf2out_begin_prologue,
3765   debug_nothing_int_charstar,   /* end_prologue */
3766   dwarf2out_end_epilogue,
3767   dwarf2out_begin_function,
3768   debug_nothing_int,            /* end_function */
3769   dwarf2out_decl,               /* function_decl */
3770   dwarf2out_global_decl,
3771   dwarf2out_type_decl,          /* type_decl */
3772   dwarf2out_imported_module_or_decl,
3773   debug_nothing_tree,           /* deferred_inline_function */
3774   /* The DWARF 2 backend tries to reduce debugging bloat by not
3775      emitting the abstract description of inline functions until
3776      something tries to reference them.  */
3777   dwarf2out_abstract_function,  /* outlining_inline_function */
3778   debug_nothing_rtx,            /* label */
3779   debug_nothing_int,            /* handle_pch */
3780   dwarf2out_var_location,
3781   dwarf2out_switch_text_section,
3782   1                             /* start_end_main_source_file */
3783 };
3784 #endif
3785 \f
3786 /* NOTE: In the comments in this file, many references are made to
3787    "Debugging Information Entries".  This term is abbreviated as `DIE'
3788    throughout the remainder of this file.  */
3789
3790 /* An internal representation of the DWARF output is built, and then
3791    walked to generate the DWARF debugging info.  The walk of the internal
3792    representation is done after the entire program has been compiled.
3793    The types below are used to describe the internal representation.  */
3794
3795 /* Various DIE's use offsets relative to the beginning of the
3796    .debug_info section to refer to each other.  */
3797
3798 typedef long int dw_offset;
3799
3800 /* Define typedefs here to avoid circular dependencies.  */
3801
3802 typedef struct dw_attr_struct *dw_attr_ref;
3803 typedef struct dw_line_info_struct *dw_line_info_ref;
3804 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3805 typedef struct pubname_struct *pubname_ref;
3806 typedef struct dw_ranges_struct *dw_ranges_ref;
3807 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3808
3809 /* Each entry in the line_info_table maintains the file and
3810    line number associated with the label generated for that
3811    entry.  The label gives the PC value associated with
3812    the line number entry.  */
3813
3814 typedef struct dw_line_info_struct GTY(())
3815 {
3816   unsigned long dw_file_num;
3817   unsigned long dw_line_num;
3818 }
3819 dw_line_info_entry;
3820
3821 /* Line information for functions in separate sections; each one gets its
3822    own sequence.  */
3823 typedef struct dw_separate_line_info_struct GTY(())
3824 {
3825   unsigned long dw_file_num;
3826   unsigned long dw_line_num;
3827   unsigned long function;
3828 }
3829 dw_separate_line_info_entry;
3830
3831 /* Each DIE attribute has a field specifying the attribute kind,
3832    a link to the next attribute in the chain, and an attribute value.
3833    Attributes are typically linked below the DIE they modify.  */
3834
3835 typedef struct dw_attr_struct GTY(())
3836 {
3837   enum dwarf_attribute dw_attr;
3838   dw_val_node dw_attr_val;
3839 }
3840 dw_attr_node;
3841
3842 DEF_VEC_O(dw_attr_node);
3843 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3844
3845 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3846    The children of each node form a circular list linked by
3847    die_sib.  die_child points to the node *before* the "first" child node.  */
3848
3849 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
3850 {
3851   enum dwarf_tag die_tag;
3852   char *die_symbol;
3853   VEC(dw_attr_node,gc) * die_attr;
3854   dw_die_ref die_parent;
3855   dw_die_ref die_child;
3856   dw_die_ref die_sib;
3857   dw_die_ref die_definition; /* ref from a specification to its definition */
3858   dw_offset die_offset;
3859   unsigned long die_abbrev;
3860   int die_mark;
3861   /* Die is used and must not be pruned as unused.  */
3862   int die_perennial_p;
3863   unsigned int decl_id;
3864 }
3865 die_node;
3866
3867 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3868 #define FOR_EACH_CHILD(die, c, expr) do {       \
3869   c = die->die_child;                           \
3870   if (c) do {                                   \
3871     c = c->die_sib;                             \
3872     expr;                                       \
3873   } while (c != die->die_child);                \
3874 } while (0)
3875
3876 /* The pubname structure */
3877
3878 typedef struct pubname_struct GTY(())
3879 {
3880   dw_die_ref die;
3881   const char *name;
3882 }
3883 pubname_entry;
3884
3885 DEF_VEC_O(pubname_entry);
3886 DEF_VEC_ALLOC_O(pubname_entry, gc);
3887
3888 struct dw_ranges_struct GTY(())
3889 {
3890   /* If this is positive, it's a block number, otherwise it's a
3891      bitwise-negated index into dw_ranges_by_label.  */
3892   int num;
3893 };
3894
3895 struct dw_ranges_by_label_struct GTY(())
3896 {
3897   const char *begin;
3898   const char *end;
3899 };
3900
3901 /* The limbo die list structure.  */
3902 typedef struct limbo_die_struct GTY(())
3903 {
3904   dw_die_ref die;
3905   tree created_for;
3906   struct limbo_die_struct *next;
3907 }
3908 limbo_die_node;
3909
3910 /* How to start an assembler comment.  */
3911 #ifndef ASM_COMMENT_START
3912 #define ASM_COMMENT_START ";#"
3913 #endif
3914
3915 /* Define a macro which returns nonzero for a TYPE_DECL which was
3916    implicitly generated for a tagged type.
3917
3918    Note that unlike the gcc front end (which generates a NULL named
3919    TYPE_DECL node for each complete tagged type, each array type, and
3920    each function type node created) the g++ front end generates a
3921    _named_ TYPE_DECL node for each tagged type node created.
3922    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3923    generate a DW_TAG_typedef DIE for them.  */
3924
3925 #define TYPE_DECL_IS_STUB(decl)                         \
3926   (DECL_NAME (decl) == NULL_TREE                        \
3927    || (DECL_ARTIFICIAL (decl)                           \
3928        && is_tagged_type (TREE_TYPE (decl))             \
3929        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3930            /* This is necessary for stub decls that     \
3931               appear in nested inline functions.  */    \
3932            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3933                && (decl_ultimate_origin (decl)          \
3934                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3935
3936 /* Information concerning the compilation unit's programming
3937    language, and compiler version.  */
3938
3939 /* Fixed size portion of the DWARF compilation unit header.  */
3940 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3941   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3942
3943 /* Fixed size portion of public names info.  */
3944 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3945
3946 /* Fixed size portion of the address range info.  */
3947 #define DWARF_ARANGES_HEADER_SIZE                                       \
3948   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3949                 DWARF2_ADDR_SIZE * 2)                                   \
3950    - DWARF_INITIAL_LENGTH_SIZE)
3951
3952 /* Size of padding portion in the address range info.  It must be
3953    aligned to twice the pointer size.  */
3954 #define DWARF_ARANGES_PAD_SIZE \
3955   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3956                 DWARF2_ADDR_SIZE * 2)                              \
3957    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3958
3959 /* Use assembler line directives if available.  */
3960 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3961 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3962 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3963 #else
3964 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3965 #endif
3966 #endif
3967
3968 /* Minimum line offset in a special line info. opcode.
3969    This value was chosen to give a reasonable range of values.  */
3970 #define DWARF_LINE_BASE  -10
3971
3972 /* First special line opcode - leave room for the standard opcodes.  */
3973 #define DWARF_LINE_OPCODE_BASE  10
3974
3975 /* Range of line offsets in a special line info. opcode.  */
3976 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3977
3978 /* Flag that indicates the initial value of the is_stmt_start flag.
3979    In the present implementation, we do not mark any lines as
3980    the beginning of a source statement, because that information
3981    is not made available by the GCC front-end.  */
3982 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3983
3984 #ifdef DWARF2_DEBUGGING_INFO
3985 /* This location is used by calc_die_sizes() to keep track
3986    the offset of each DIE within the .debug_info section.  */
3987 static unsigned long next_die_offset;
3988 #endif
3989
3990 /* Record the root of the DIE's built for the current compilation unit.  */
3991 static GTY(()) dw_die_ref comp_unit_die;
3992
3993 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3994 static GTY(()) limbo_die_node *limbo_die_list;
3995
3996 /* Filenames referenced by this compilation unit.  */
3997 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3998
3999 /* A hash table of references to DIE's that describe declarations.
4000    The key is a DECL_UID() which is a unique number identifying each decl.  */
4001 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4002
4003 /* Node of the variable location list.  */
4004 struct var_loc_node GTY ((chain_next ("%h.next")))
4005 {
4006   rtx GTY (()) var_loc_note;
4007   const char * GTY (()) label;
4008   const char * GTY (()) section_label;
4009   struct var_loc_node * GTY (()) next;
4010 };
4011
4012 /* Variable location list.  */
4013 struct var_loc_list_def GTY (())
4014 {
4015   struct var_loc_node * GTY (()) first;
4016
4017   /* Do not mark the last element of the chained list because
4018      it is marked through the chain.  */
4019   struct var_loc_node * GTY ((skip ("%h"))) last;
4020
4021   /* DECL_UID of the variable decl.  */
4022   unsigned int decl_id;
4023 };
4024 typedef struct var_loc_list_def var_loc_list;
4025
4026
4027 /* Table of decl location linked lists.  */
4028 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4029
4030 /* A pointer to the base of a list of references to DIE's that
4031    are uniquely identified by their tag, presence/absence of
4032    children DIE's, and list of attribute/value pairs.  */
4033 static GTY((length ("abbrev_die_table_allocated")))
4034   dw_die_ref *abbrev_die_table;
4035
4036 /* Number of elements currently allocated for abbrev_die_table.  */
4037 static GTY(()) unsigned abbrev_die_table_allocated;
4038
4039 /* Number of elements in type_die_table currently in use.  */
4040 static GTY(()) unsigned abbrev_die_table_in_use;
4041
4042 /* Size (in elements) of increments by which we may expand the
4043    abbrev_die_table.  */
4044 #define ABBREV_DIE_TABLE_INCREMENT 256
4045
4046 /* A pointer to the base of a table that contains line information
4047    for each source code line in .text in the compilation unit.  */
4048 static GTY((length ("line_info_table_allocated")))
4049      dw_line_info_ref line_info_table;
4050
4051 /* Number of elements currently allocated for line_info_table.  */
4052 static GTY(()) unsigned line_info_table_allocated;
4053
4054 /* Number of elements in line_info_table currently in use.  */
4055 static GTY(()) unsigned line_info_table_in_use;
4056
4057 /* A pointer to the base of a table that contains line information
4058    for each source code line outside of .text in the compilation unit.  */
4059 static GTY ((length ("separate_line_info_table_allocated")))
4060      dw_separate_line_info_ref separate_line_info_table;
4061
4062 /* Number of elements currently allocated for separate_line_info_table.  */
4063 static GTY(()) unsigned separate_line_info_table_allocated;
4064
4065 /* Number of elements in separate_line_info_table currently in use.  */
4066 static GTY(()) unsigned separate_line_info_table_in_use;
4067
4068 /* Size (in elements) of increments by which we may expand the
4069    line_info_table.  */
4070 #define LINE_INFO_TABLE_INCREMENT 1024
4071
4072 /* A pointer to the base of a table that contains a list of publicly
4073    accessible names.  */
4074 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4075
4076 /* A pointer to the base of a table that contains a list of publicly
4077    accessible types.  */
4078 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4079
4080 /* Array of dies for which we should generate .debug_arange info.  */
4081 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4082
4083 /* Number of elements currently allocated for arange_table.  */
4084 static GTY(()) unsigned arange_table_allocated;
4085
4086 /* Number of elements in arange_table currently in use.  */
4087 static GTY(()) unsigned arange_table_in_use;
4088
4089 /* Size (in elements) of increments by which we may expand the
4090    arange_table.  */
4091 #define ARANGE_TABLE_INCREMENT 64
4092
4093 /* Array of dies for which we should generate .debug_ranges info.  */
4094 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4095
4096 /* Number of elements currently allocated for ranges_table.  */
4097 static GTY(()) unsigned ranges_table_allocated;
4098
4099 /* Number of elements in ranges_table currently in use.  */
4100 static GTY(()) unsigned ranges_table_in_use;
4101
4102 /* Array of pairs of labels referenced in ranges_table.  */
4103 static GTY ((length ("ranges_by_label_allocated")))
4104      dw_ranges_by_label_ref ranges_by_label;
4105
4106 /* Number of elements currently allocated for ranges_by_label.  */
4107 static GTY(()) unsigned ranges_by_label_allocated;
4108
4109 /* Number of elements in ranges_by_label currently in use.  */
4110 static GTY(()) unsigned ranges_by_label_in_use;
4111
4112 /* Size (in elements) of increments by which we may expand the
4113    ranges_table.  */
4114 #define RANGES_TABLE_INCREMENT 64
4115
4116 /* Whether we have location lists that need outputting */
4117 static GTY(()) bool have_location_lists;
4118
4119 /* Unique label counter.  */
4120 static GTY(()) unsigned int loclabel_num;
4121
4122 #ifdef DWARF2_DEBUGGING_INFO
4123 /* Record whether the function being analyzed contains inlined functions.  */
4124 static int current_function_has_inlines;
4125 #endif
4126 #if 0 && defined (MIPS_DEBUGGING_INFO)
4127 static int comp_unit_has_inlines;
4128 #endif
4129
4130 /* The last file entry emitted by maybe_emit_file().  */
4131 static GTY(()) struct dwarf_file_data * last_emitted_file;
4132
4133 /* Number of internal labels generated by gen_internal_sym().  */
4134 static GTY(()) int label_num;
4135
4136 /* Cached result of previous call to lookup_filename.  */
4137 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4138
4139 #ifdef DWARF2_DEBUGGING_INFO
4140
4141 /* Offset from the "steady-state frame pointer" to the frame base,
4142    within the current function.  */
4143 static HOST_WIDE_INT frame_pointer_fb_offset;
4144
4145 /* Forward declarations for functions defined in this file.  */
4146
4147 static int is_pseudo_reg (const_rtx);
4148 static tree type_main_variant (tree);
4149 static int is_tagged_type (const_tree);
4150 static const char *dwarf_tag_name (unsigned);
4151 static const char *dwarf_attr_name (unsigned);
4152 static const char *dwarf_form_name (unsigned);
4153 static tree decl_ultimate_origin (const_tree);
4154 static tree block_ultimate_origin (const_tree);
4155 static tree decl_class_context (tree);
4156 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4157 static inline enum dw_val_class AT_class (dw_attr_ref);
4158 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4159 static inline unsigned AT_flag (dw_attr_ref);
4160 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4161 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4162 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4163 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4164 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4165                               unsigned long);
4166 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4167                                unsigned int, unsigned char *);
4168 static hashval_t debug_str_do_hash (const void *);
4169 static int debug_str_eq (const void *, const void *);
4170 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4171 static inline const char *AT_string (dw_attr_ref);
4172 static int AT_string_form (dw_attr_ref);
4173 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4174 static void add_AT_specification (dw_die_ref, dw_die_ref);
4175 static inline dw_die_ref AT_ref (dw_attr_ref);
4176 static inline int AT_ref_external (dw_attr_ref);
4177 static inline void set_AT_ref_external (dw_attr_ref, int);
4178 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4179 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4180 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4181 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4182                              dw_loc_list_ref);
4183 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4184 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4185 static inline rtx AT_addr (dw_attr_ref);
4186 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4187 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4188 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4189 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4190                            unsigned HOST_WIDE_INT);
4191 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4192                                unsigned long);
4193 static inline const char *AT_lbl (dw_attr_ref);
4194 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4195 static const char *get_AT_low_pc (dw_die_ref);
4196 static const char *get_AT_hi_pc (dw_die_ref);
4197 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4198 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4199 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4200 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4201 static bool is_c_family (void);
4202 static bool is_cxx (void);
4203 static bool is_java (void);
4204 static bool is_fortran (void);
4205 static bool is_ada (void);
4206 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4207 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4208 static void add_child_die (dw_die_ref, dw_die_ref);
4209 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4210 static dw_die_ref lookup_type_die (tree);
4211 static void equate_type_number_to_die (tree, dw_die_ref);
4212 static hashval_t decl_die_table_hash (const void *);
4213 static int decl_die_table_eq (const void *, const void *);
4214 static dw_die_ref lookup_decl_die (tree);
4215 static hashval_t decl_loc_table_hash (const void *);
4216 static int decl_loc_table_eq (const void *, const void *);
4217 static var_loc_list *lookup_decl_loc (const_tree);
4218 static void equate_decl_number_to_die (tree, dw_die_ref);
4219 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4220 static void print_spaces (FILE *);
4221 static void print_die (dw_die_ref, FILE *);
4222 static void print_dwarf_line_table (FILE *);
4223 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4224 static dw_die_ref pop_compile_unit (dw_die_ref);
4225 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4226 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4227 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4228 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4229 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4230 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4231 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4232 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4233 static void compute_section_prefix (dw_die_ref);
4234 static int is_type_die (dw_die_ref);
4235 static int is_comdat_die (dw_die_ref);
4236 static int is_symbol_die (dw_die_ref);
4237 static void assign_symbol_names (dw_die_ref);
4238 static void break_out_includes (dw_die_ref);
4239 static hashval_t htab_cu_hash (const void *);
4240 static int htab_cu_eq (const void *, const void *);
4241 static void htab_cu_del (void *);
4242 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4243 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4244 static void add_sibling_attributes (dw_die_ref);
4245 static void build_abbrev_table (dw_die_ref);
4246 static void output_location_lists (dw_die_ref);
4247 static int constant_size (long unsigned);
4248 static unsigned long size_of_die (dw_die_ref);
4249 static void calc_die_sizes (dw_die_ref);
4250 static void mark_dies (dw_die_ref);
4251 static void unmark_dies (dw_die_ref);
4252 static void unmark_all_dies (dw_die_ref);
4253 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4254 static unsigned long size_of_aranges (void);
4255 static enum dwarf_form value_format (dw_attr_ref);
4256 static void output_value_format (dw_attr_ref);
4257 static void output_abbrev_section (void);
4258 static void output_die_symbol (dw_die_ref);
4259 static void output_die (dw_die_ref);
4260 static void output_compilation_unit_header (void);
4261 static void output_comp_unit (dw_die_ref, int);
4262 static const char *dwarf2_name (tree, int);
4263 static void add_pubname (tree, dw_die_ref);
4264 static void add_pubname_string (const char *, dw_die_ref);
4265 static void add_pubtype (tree, dw_die_ref);
4266 static void output_pubnames (VEC (pubname_entry,gc) *);
4267 static void add_arange (tree, dw_die_ref);
4268 static void output_aranges (void);
4269 static unsigned int add_ranges_num (int);
4270 static unsigned int add_ranges (const_tree);
4271 static unsigned int add_ranges_by_labels (const char *, const char *);
4272 static void output_ranges (void);
4273 static void output_line_info (void);
4274 static void output_file_names (void);
4275 static dw_die_ref base_type_die (tree);
4276 static int is_base_type (tree);
4277 static bool is_subrange_type (const_tree);
4278 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4279 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4280 static int type_is_enum (const_tree);
4281 static unsigned int dbx_reg_number (const_rtx);
4282 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4283 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4284 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int, 
4285                                                 enum var_init_status);
4286 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4287                                                      enum var_init_status);
4288 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4289 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4290                                          enum var_init_status);
4291 static int is_based_loc (const_rtx);
4292 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4293                                             enum var_init_status);
4294 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4295                                                enum var_init_status);
4296 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4297 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4298 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4299 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4300 static tree field_type (const_tree);
4301 static unsigned int simple_type_align_in_bits (const_tree);
4302 static unsigned int simple_decl_align_in_bits (const_tree);
4303 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4304 static HOST_WIDE_INT field_byte_offset (const_tree);
4305 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4306                                          dw_loc_descr_ref);
4307 static void add_data_member_location_attribute (dw_die_ref, tree);
4308 static void add_const_value_attribute (dw_die_ref, rtx);
4309 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4310 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4311 static void insert_float (const_rtx, unsigned char *);
4312 static rtx rtl_for_decl_location (tree);
4313 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4314                                                    enum dwarf_attribute);
4315 static void tree_add_const_value_attribute (dw_die_ref, tree);
4316 static void add_name_attribute (dw_die_ref, const char *);
4317 static void add_comp_dir_attribute (dw_die_ref);
4318 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4319 static void add_subscript_info (dw_die_ref, tree);
4320 static void add_byte_size_attribute (dw_die_ref, tree);
4321 static void add_bit_offset_attribute (dw_die_ref, tree);
4322 static void add_bit_size_attribute (dw_die_ref, tree);
4323 static void add_prototyped_attribute (dw_die_ref, tree);
4324 static void add_abstract_origin_attribute (dw_die_ref, tree);
4325 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4326 static void add_src_coords_attributes (dw_die_ref, tree);
4327 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4328 static void push_decl_scope (tree);
4329 static void pop_decl_scope (void);
4330 static dw_die_ref scope_die_for (tree, dw_die_ref);
4331 static inline int local_scope_p (dw_die_ref);
4332 static inline int class_or_namespace_scope_p (dw_die_ref);
4333 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4334 static void add_calling_convention_attribute (dw_die_ref, tree);
4335 static const char *type_tag (const_tree);
4336 static tree member_declared_type (const_tree);
4337 #if 0
4338 static const char *decl_start_label (tree);
4339 #endif
4340 static void gen_array_type_die (tree, dw_die_ref);
4341 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4342 #if 0
4343 static void gen_entry_point_die (tree, dw_die_ref);
4344 #endif
4345 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4346 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4347 static void gen_inlined_union_type_die (tree, dw_die_ref);
4348 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4349 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4350 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4351 static void gen_formal_types_die (tree, dw_die_ref);
4352 static void gen_subprogram_die (tree, dw_die_ref);
4353 static void gen_variable_die (tree, dw_die_ref);
4354 static void gen_label_die (tree, dw_die_ref);
4355 static void gen_lexical_block_die (tree, dw_die_ref, int);
4356 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4357 static void gen_field_die (tree, dw_die_ref);
4358 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4359 static dw_die_ref gen_compile_unit_die (const char *);
4360 static void gen_inheritance_die (tree, tree, dw_die_ref);
4361 static void gen_member_die (tree, dw_die_ref);
4362 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4363                                                 enum debug_info_usage);
4364 static void gen_subroutine_type_die (tree, dw_die_ref);
4365 static void gen_typedef_die (tree, dw_die_ref);
4366 static void gen_type_die (tree, dw_die_ref);
4367 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4368 static void gen_block_die (tree, dw_die_ref, int);
4369 static void decls_for_scope (tree, dw_die_ref, int);
4370 static int is_redundant_typedef (const_tree);
4371 static void gen_namespace_die (tree);
4372 static void gen_decl_die (tree, dw_die_ref);
4373 static dw_die_ref force_decl_die (tree);
4374 static dw_die_ref force_type_die (tree);
4375 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4376 static void declare_in_namespace (tree, dw_die_ref);
4377 static struct dwarf_file_data * lookup_filename (const char *);
4378 static void retry_incomplete_types (void);
4379 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4380 static void splice_child_die (dw_die_ref, dw_die_ref);
4381 static int file_info_cmp (const void *, const void *);
4382 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4383                                      const char *, const char *, unsigned);
4384 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4385                                        const char *, const char *,
4386                                        const char *);
4387 static void output_loc_list (dw_loc_list_ref);
4388 static char *gen_internal_sym (const char *);
4389
4390 static void prune_unmark_dies (dw_die_ref);
4391 static void prune_unused_types_mark (dw_die_ref, int);
4392 static void prune_unused_types_walk (dw_die_ref);
4393 static void prune_unused_types_walk_attribs (dw_die_ref);
4394 static void prune_unused_types_prune (dw_die_ref);
4395 static void prune_unused_types (void);
4396 static int maybe_emit_file (struct dwarf_file_data *fd);
4397
4398 /* Section names used to hold DWARF debugging information.  */
4399 #ifndef DEBUG_INFO_SECTION
4400 #define DEBUG_INFO_SECTION      ".debug_info"
4401 #endif
4402 #ifndef DEBUG_ABBREV_SECTION
4403 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4404 #endif
4405 #ifndef DEBUG_ARANGES_SECTION
4406 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4407 #endif
4408 #ifndef DEBUG_MACINFO_SECTION
4409 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4410 #endif
4411 #ifndef DEBUG_LINE_SECTION
4412 #define DEBUG_LINE_SECTION      ".debug_line"
4413 #endif
4414 #ifndef DEBUG_LOC_SECTION
4415 #define DEBUG_LOC_SECTION       ".debug_loc"
4416 #endif
4417 #ifndef DEBUG_PUBNAMES_SECTION
4418 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4419 #endif
4420 #ifndef DEBUG_STR_SECTION
4421 #define DEBUG_STR_SECTION       ".debug_str"
4422 #endif
4423 #ifndef DEBUG_RANGES_SECTION
4424 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4425 #endif
4426
4427 /* Standard ELF section names for compiled code and data.  */
4428 #ifndef TEXT_SECTION_NAME
4429 #define TEXT_SECTION_NAME       ".text"
4430 #endif
4431
4432 /* Section flags for .debug_str section.  */
4433 #define DEBUG_STR_SECTION_FLAGS \
4434   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4435    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4436    : SECTION_DEBUG)
4437
4438 /* Labels we insert at beginning sections we can reference instead of
4439    the section names themselves.  */
4440
4441 #ifndef TEXT_SECTION_LABEL
4442 #define TEXT_SECTION_LABEL              "Ltext"
4443 #endif
4444 #ifndef COLD_TEXT_SECTION_LABEL
4445 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4446 #endif
4447 #ifndef DEBUG_LINE_SECTION_LABEL
4448 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4449 #endif
4450 #ifndef DEBUG_INFO_SECTION_LABEL
4451 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4452 #endif
4453 #ifndef DEBUG_ABBREV_SECTION_LABEL
4454 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4455 #endif
4456 #ifndef DEBUG_LOC_SECTION_LABEL
4457 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4458 #endif
4459 #ifndef DEBUG_RANGES_SECTION_LABEL
4460 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4461 #endif
4462 #ifndef DEBUG_MACINFO_SECTION_LABEL
4463 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4464 #endif
4465
4466 /* Definitions of defaults for formats and names of various special
4467    (artificial) labels which may be generated within this file (when the -g
4468    options is used and DWARF2_DEBUGGING_INFO is in effect.
4469    If necessary, these may be overridden from within the tm.h file, but
4470    typically, overriding these defaults is unnecessary.  */
4471
4472 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4473 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4474 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4475 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4476 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4477 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4478 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4479 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4480 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4481 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4482
4483 #ifndef TEXT_END_LABEL
4484 #define TEXT_END_LABEL          "Letext"
4485 #endif
4486 #ifndef COLD_END_LABEL
4487 #define COLD_END_LABEL          "Letext_cold"
4488 #endif
4489 #ifndef BLOCK_BEGIN_LABEL
4490 #define BLOCK_BEGIN_LABEL       "LBB"
4491 #endif
4492 #ifndef BLOCK_END_LABEL
4493 #define BLOCK_END_LABEL         "LBE"
4494 #endif
4495 #ifndef LINE_CODE_LABEL
4496 #define LINE_CODE_LABEL         "LM"
4497 #endif
4498 #ifndef SEPARATE_LINE_CODE_LABEL
4499 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4500 #endif
4501
4502 \f
4503 /* We allow a language front-end to designate a function that is to be
4504    called to "demangle" any name before it is put into a DIE.  */
4505
4506 static const char *(*demangle_name_func) (const char *);
4507
4508 void
4509 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4510 {
4511   demangle_name_func = func;
4512 }
4513
4514 /* Test if rtl node points to a pseudo register.  */
4515
4516 static inline int
4517 is_pseudo_reg (const_rtx rtl)
4518 {
4519   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4520           || (GET_CODE (rtl) == SUBREG
4521               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4522 }
4523
4524 /* Return a reference to a type, with its const and volatile qualifiers
4525    removed.  */
4526
4527 static inline tree
4528 type_main_variant (tree type)
4529 {
4530   type = TYPE_MAIN_VARIANT (type);
4531
4532   /* ??? There really should be only one main variant among any group of
4533      variants of a given type (and all of the MAIN_VARIANT values for all
4534      members of the group should point to that one type) but sometimes the C
4535      front-end messes this up for array types, so we work around that bug
4536      here.  */
4537   if (TREE_CODE (type) == ARRAY_TYPE)
4538     while (type != TYPE_MAIN_VARIANT (type))
4539       type = TYPE_MAIN_VARIANT (type);
4540
4541   return type;
4542 }
4543
4544 /* Return nonzero if the given type node represents a tagged type.  */
4545
4546 static inline int
4547 is_tagged_type (const_tree type)
4548 {
4549   enum tree_code code = TREE_CODE (type);
4550
4551   return (code == RECORD_TYPE || code == UNION_TYPE
4552           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4553 }
4554
4555 /* Convert a DIE tag into its string name.  */
4556
4557 static const char *
4558 dwarf_tag_name (unsigned int tag)
4559 {
4560   switch (tag)
4561     {
4562     case DW_TAG_padding:
4563       return "DW_TAG_padding";
4564     case DW_TAG_array_type:
4565       return "DW_TAG_array_type";
4566     case DW_TAG_class_type:
4567       return "DW_TAG_class_type";
4568     case DW_TAG_entry_point:
4569       return "DW_TAG_entry_point";
4570     case DW_TAG_enumeration_type:
4571       return "DW_TAG_enumeration_type";
4572     case DW_TAG_formal_parameter:
4573       return "DW_TAG_formal_parameter";
4574     case DW_TAG_imported_declaration:
4575       return "DW_TAG_imported_declaration";
4576     case DW_TAG_label:
4577       return "DW_TAG_label";
4578     case DW_TAG_lexical_block:
4579       return "DW_TAG_lexical_block";
4580     case DW_TAG_member:
4581       return "DW_TAG_member";
4582     case DW_TAG_pointer_type:
4583       return "DW_TAG_pointer_type";
4584     case DW_TAG_reference_type:
4585       return "DW_TAG_reference_type";
4586     case DW_TAG_compile_unit:
4587       return "DW_TAG_compile_unit";
4588     case DW_TAG_string_type:
4589       return "DW_TAG_string_type";
4590     case DW_TAG_structure_type:
4591       return "DW_TAG_structure_type";
4592     case DW_TAG_subroutine_type:
4593       return "DW_TAG_subroutine_type";
4594     case DW_TAG_typedef:
4595       return "DW_TAG_typedef";
4596     case DW_TAG_union_type:
4597       return "DW_TAG_union_type";
4598     case DW_TAG_unspecified_parameters:
4599       return "DW_TAG_unspecified_parameters";
4600     case DW_TAG_variant:
4601       return "DW_TAG_variant";
4602     case DW_TAG_common_block:
4603       return "DW_TAG_common_block";
4604     case DW_TAG_common_inclusion:
4605       return "DW_TAG_common_inclusion";
4606     case DW_TAG_inheritance:
4607       return "DW_TAG_inheritance";
4608     case DW_TAG_inlined_subroutine:
4609       return "DW_TAG_inlined_subroutine";
4610     case DW_TAG_module:
4611       return "DW_TAG_module";
4612     case DW_TAG_ptr_to_member_type:
4613       return "DW_TAG_ptr_to_member_type";
4614     case DW_TAG_set_type:
4615       return "DW_TAG_set_type";
4616     case DW_TAG_subrange_type:
4617       return "DW_TAG_subrange_type";
4618     case DW_TAG_with_stmt:
4619       return "DW_TAG_with_stmt";
4620     case DW_TAG_access_declaration:
4621       return "DW_TAG_access_declaration";
4622     case DW_TAG_base_type:
4623       return "DW_TAG_base_type";
4624     case DW_TAG_catch_block:
4625       return "DW_TAG_catch_block";
4626     case DW_TAG_const_type:
4627       return "DW_TAG_const_type";
4628     case DW_TAG_constant:
4629       return "DW_TAG_constant";
4630     case DW_TAG_enumerator:
4631       return "DW_TAG_enumerator";
4632     case DW_TAG_file_type:
4633       return "DW_TAG_file_type";
4634     case DW_TAG_friend:
4635       return "DW_TAG_friend";
4636     case DW_TAG_namelist:
4637       return "DW_TAG_namelist";
4638     case DW_TAG_namelist_item:
4639       return "DW_TAG_namelist_item";
4640     case DW_TAG_packed_type:
4641       return "DW_TAG_packed_type";
4642     case DW_TAG_subprogram:
4643       return "DW_TAG_subprogram";
4644     case DW_TAG_template_type_param:
4645       return "DW_TAG_template_type_param";
4646     case DW_TAG_template_value_param:
4647       return "DW_TAG_template_value_param";
4648     case DW_TAG_thrown_type:
4649       return "DW_TAG_thrown_type";
4650     case DW_TAG_try_block:
4651       return "DW_TAG_try_block";
4652     case DW_TAG_variant_part:
4653       return "DW_TAG_variant_part";
4654     case DW_TAG_variable:
4655       return "DW_TAG_variable";
4656     case DW_TAG_volatile_type:
4657       return "DW_TAG_volatile_type";
4658     case DW_TAG_dwarf_procedure:
4659       return "DW_TAG_dwarf_procedure";
4660     case DW_TAG_restrict_type:
4661       return "DW_TAG_restrict_type";
4662     case DW_TAG_interface_type:
4663       return "DW_TAG_interface_type";
4664     case DW_TAG_namespace:
4665       return "DW_TAG_namespace";
4666     case DW_TAG_imported_module:
4667       return "DW_TAG_imported_module";
4668     case DW_TAG_unspecified_type:
4669       return "DW_TAG_unspecified_type";
4670     case DW_TAG_partial_unit:
4671       return "DW_TAG_partial_unit";
4672     case DW_TAG_imported_unit:
4673       return "DW_TAG_imported_unit";
4674     case DW_TAG_condition:
4675       return "DW_TAG_condition";
4676     case DW_TAG_shared_type:
4677       return "DW_TAG_shared_type";
4678     case DW_TAG_MIPS_loop:
4679       return "DW_TAG_MIPS_loop";
4680     case DW_TAG_format_label:
4681       return "DW_TAG_format_label";
4682     case DW_TAG_function_template:
4683       return "DW_TAG_function_template";
4684     case DW_TAG_class_template:
4685       return "DW_TAG_class_template";
4686     case DW_TAG_GNU_BINCL:
4687       return "DW_TAG_GNU_BINCL";
4688     case DW_TAG_GNU_EINCL:
4689       return "DW_TAG_GNU_EINCL";
4690     default:
4691       return "DW_TAG_<unknown>";
4692     }
4693 }
4694
4695 /* Convert a DWARF attribute code into its string name.  */
4696
4697 static const char *
4698 dwarf_attr_name (unsigned int attr)
4699 {
4700   switch (attr)
4701     {
4702     case DW_AT_sibling:
4703       return "DW_AT_sibling";
4704     case DW_AT_location:
4705       return "DW_AT_location";
4706     case DW_AT_name:
4707       return "DW_AT_name";
4708     case DW_AT_ordering:
4709       return "DW_AT_ordering";
4710     case DW_AT_subscr_data:
4711       return "DW_AT_subscr_data";
4712     case DW_AT_byte_size:
4713       return "DW_AT_byte_size";
4714     case DW_AT_bit_offset:
4715       return "DW_AT_bit_offset";
4716     case DW_AT_bit_size:
4717       return "DW_AT_bit_size";
4718     case DW_AT_element_list:
4719       return "DW_AT_element_list";
4720     case DW_AT_stmt_list:
4721       return "DW_AT_stmt_list";
4722     case DW_AT_low_pc:
4723       return "DW_AT_low_pc";
4724     case DW_AT_high_pc:
4725       return "DW_AT_high_pc";
4726     case DW_AT_language:
4727       return "DW_AT_language";
4728     case DW_AT_member:
4729       return "DW_AT_member";
4730     case DW_AT_discr:
4731       return "DW_AT_discr";
4732     case DW_AT_discr_value:
4733       return "DW_AT_discr_value";
4734     case DW_AT_visibility:
4735       return "DW_AT_visibility";
4736     case DW_AT_import:
4737       return "DW_AT_import";
4738     case DW_AT_string_length:
4739       return "DW_AT_string_length";
4740     case DW_AT_common_reference:
4741       return "DW_AT_common_reference";
4742     case DW_AT_comp_dir:
4743       return "DW_AT_comp_dir";
4744     case DW_AT_const_value:
4745       return "DW_AT_const_value";
4746     case DW_AT_containing_type:
4747       return "DW_AT_containing_type";
4748     case DW_AT_default_value:
4749       return "DW_AT_default_value";
4750     case DW_AT_inline:
4751       return "DW_AT_inline";
4752     case DW_AT_is_optional:
4753       return "DW_AT_is_optional";
4754     case DW_AT_lower_bound:
4755       return "DW_AT_lower_bound";
4756     case DW_AT_producer:
4757       return "DW_AT_producer";
4758     case DW_AT_prototyped:
4759       return "DW_AT_prototyped";
4760     case DW_AT_return_addr:
4761       return "DW_AT_return_addr";
4762     case DW_AT_start_scope:
4763       return "DW_AT_start_scope";
4764     case DW_AT_bit_stride:
4765       return "DW_AT_bit_stride";
4766     case DW_AT_upper_bound:
4767       return "DW_AT_upper_bound";
4768     case DW_AT_abstract_origin:
4769       return "DW_AT_abstract_origin";
4770     case DW_AT_accessibility:
4771       return "DW_AT_accessibility";
4772     case DW_AT_address_class:
4773       return "DW_AT_address_class";
4774     case DW_AT_artificial:
4775       return "DW_AT_artificial";
4776     case DW_AT_base_types:
4777       return "DW_AT_base_types";
4778     case DW_AT_calling_convention:
4779       return "DW_AT_calling_convention";
4780     case DW_AT_count:
4781       return "DW_AT_count";
4782     case DW_AT_data_member_location:
4783       return "DW_AT_data_member_location";
4784     case DW_AT_decl_column:
4785       return "DW_AT_decl_column";
4786     case DW_AT_decl_file:
4787       return "DW_AT_decl_file";
4788     case DW_AT_decl_line:
4789       return "DW_AT_decl_line";
4790     case DW_AT_declaration:
4791       return "DW_AT_declaration";
4792     case DW_AT_discr_list:
4793       return "DW_AT_discr_list";
4794     case DW_AT_encoding:
4795       return "DW_AT_encoding";
4796     case DW_AT_external:
4797       return "DW_AT_external";
4798     case DW_AT_frame_base:
4799       return "DW_AT_frame_base";
4800     case DW_AT_friend:
4801       return "DW_AT_friend";
4802     case DW_AT_identifier_case:
4803       return "DW_AT_identifier_case";
4804     case DW_AT_macro_info:
4805       return "DW_AT_macro_info";
4806     case DW_AT_namelist_items:
4807       return "DW_AT_namelist_items";
4808     case DW_AT_priority:
4809       return "DW_AT_priority";
4810     case DW_AT_segment:
4811       return "DW_AT_segment";
4812     case DW_AT_specification:
4813       return "DW_AT_specification";
4814     case DW_AT_static_link:
4815       return "DW_AT_static_link";
4816     case DW_AT_type:
4817       return "DW_AT_type";
4818     case DW_AT_use_location:
4819       return "DW_AT_use_location";
4820     case DW_AT_variable_parameter:
4821       return "DW_AT_variable_parameter";
4822     case DW_AT_virtuality:
4823       return "DW_AT_virtuality";
4824     case DW_AT_vtable_elem_location:
4825       return "DW_AT_vtable_elem_location";
4826
4827     case DW_AT_allocated:
4828       return "DW_AT_allocated";
4829     case DW_AT_associated:
4830       return "DW_AT_associated";
4831     case DW_AT_data_location:
4832       return "DW_AT_data_location";
4833     case DW_AT_byte_stride:
4834       return "DW_AT_byte_stride";
4835     case DW_AT_entry_pc:
4836       return "DW_AT_entry_pc";
4837     case DW_AT_use_UTF8:
4838       return "DW_AT_use_UTF8";
4839     case DW_AT_extension:
4840       return "DW_AT_extension";
4841     case DW_AT_ranges:
4842       return "DW_AT_ranges";
4843     case DW_AT_trampoline:
4844       return "DW_AT_trampoline";
4845     case DW_AT_call_column:
4846       return "DW_AT_call_column";
4847     case DW_AT_call_file:
4848       return "DW_AT_call_file";
4849     case DW_AT_call_line:
4850       return "DW_AT_call_line";
4851
4852     case DW_AT_MIPS_fde:
4853       return "DW_AT_MIPS_fde";
4854     case DW_AT_MIPS_loop_begin:
4855       return "DW_AT_MIPS_loop_begin";
4856     case DW_AT_MIPS_tail_loop_begin:
4857       return "DW_AT_MIPS_tail_loop_begin";
4858     case DW_AT_MIPS_epilog_begin:
4859       return "DW_AT_MIPS_epilog_begin";
4860     case DW_AT_MIPS_loop_unroll_factor:
4861       return "DW_AT_MIPS_loop_unroll_factor";
4862     case DW_AT_MIPS_software_pipeline_depth:
4863       return "DW_AT_MIPS_software_pipeline_depth";
4864     case DW_AT_MIPS_linkage_name:
4865       return "DW_AT_MIPS_linkage_name";
4866     case DW_AT_MIPS_stride:
4867       return "DW_AT_MIPS_stride";
4868     case DW_AT_MIPS_abstract_name:
4869       return "DW_AT_MIPS_abstract_name";
4870     case DW_AT_MIPS_clone_origin:
4871       return "DW_AT_MIPS_clone_origin";
4872     case DW_AT_MIPS_has_inlines:
4873       return "DW_AT_MIPS_has_inlines";
4874
4875     case DW_AT_sf_names:
4876       return "DW_AT_sf_names";
4877     case DW_AT_src_info:
4878       return "DW_AT_src_info";
4879     case DW_AT_mac_info:
4880       return "DW_AT_mac_info";
4881     case DW_AT_src_coords:
4882       return "DW_AT_src_coords";
4883     case DW_AT_body_begin:
4884       return "DW_AT_body_begin";
4885     case DW_AT_body_end:
4886       return "DW_AT_body_end";
4887     case DW_AT_GNU_vector:
4888       return "DW_AT_GNU_vector";
4889
4890     case DW_AT_VMS_rtnbeg_pd_address:
4891       return "DW_AT_VMS_rtnbeg_pd_address";
4892
4893     default:
4894       return "DW_AT_<unknown>";
4895     }
4896 }
4897
4898 /* Convert a DWARF value form code into its string name.  */
4899
4900 static const char *
4901 dwarf_form_name (unsigned int form)
4902 {
4903   switch (form)
4904     {
4905     case DW_FORM_addr:
4906       return "DW_FORM_addr";
4907     case DW_FORM_block2:
4908       return "DW_FORM_block2";
4909     case DW_FORM_block4:
4910       return "DW_FORM_block4";
4911     case DW_FORM_data2:
4912       return "DW_FORM_data2";
4913     case DW_FORM_data4:
4914       return "DW_FORM_data4";
4915     case DW_FORM_data8:
4916       return "DW_FORM_data8";
4917     case DW_FORM_string:
4918       return "DW_FORM_string";
4919     case DW_FORM_block:
4920       return "DW_FORM_block";
4921     case DW_FORM_block1:
4922       return "DW_FORM_block1";
4923     case DW_FORM_data1:
4924       return "DW_FORM_data1";
4925     case DW_FORM_flag:
4926       return "DW_FORM_flag";
4927     case DW_FORM_sdata:
4928       return "DW_FORM_sdata";
4929     case DW_FORM_strp:
4930       return "DW_FORM_strp";
4931     case DW_FORM_udata:
4932       return "DW_FORM_udata";
4933     case DW_FORM_ref_addr:
4934       return "DW_FORM_ref_addr";
4935     case DW_FORM_ref1:
4936       return "DW_FORM_ref1";
4937     case DW_FORM_ref2:
4938       return "DW_FORM_ref2";
4939     case DW_FORM_ref4:
4940       return "DW_FORM_ref4";
4941     case DW_FORM_ref8:
4942       return "DW_FORM_ref8";
4943     case DW_FORM_ref_udata:
4944       return "DW_FORM_ref_udata";
4945     case DW_FORM_indirect:
4946       return "DW_FORM_indirect";
4947     default:
4948       return "DW_FORM_<unknown>";
4949     }
4950 }
4951 \f
4952 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4953    instance of an inlined instance of a decl which is local to an inline
4954    function, so we have to trace all of the way back through the origin chain
4955    to find out what sort of node actually served as the original seed for the
4956    given block.  */
4957
4958 static tree
4959 decl_ultimate_origin (const_tree decl)
4960 {
4961   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4962     return NULL_TREE;
4963
4964   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4965      nodes in the function to point to themselves; ignore that if
4966      we're trying to output the abstract instance of this function.  */
4967   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4968     return NULL_TREE;
4969
4970   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4971      most distant ancestor, this should never happen.  */
4972   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4973
4974   return DECL_ABSTRACT_ORIGIN (decl);
4975 }
4976
4977 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4978    instance of an inlined instance of a block which is local to an inline
4979    function, so we have to trace all of the way back through the origin chain
4980    to find out what sort of node actually served as the original seed for the
4981    given block.  */
4982
4983 static tree
4984 block_ultimate_origin (const_tree block)
4985 {
4986   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4987
4988   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4989      nodes in the function to point to themselves; ignore that if
4990      we're trying to output the abstract instance of this function.  */
4991   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4992     return NULL_TREE;
4993
4994   if (immediate_origin == NULL_TREE)
4995     return NULL_TREE;
4996   else
4997     {
4998       tree ret_val;
4999       tree lookahead = immediate_origin;
5000
5001       do
5002         {
5003           ret_val = lookahead;
5004           lookahead = (TREE_CODE (ret_val) == BLOCK
5005                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5006         }
5007       while (lookahead != NULL && lookahead != ret_val);
5008
5009       /* The block's abstract origin chain may not be the *ultimate* origin of
5010          the block. It could lead to a DECL that has an abstract origin set.
5011          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5012          will give us if it has one).  Note that DECL's abstract origins are
5013          supposed to be the most distant ancestor (or so decl_ultimate_origin
5014          claims), so we don't need to loop following the DECL origins.  */
5015       if (DECL_P (ret_val))
5016         return DECL_ORIGIN (ret_val);
5017
5018       return ret_val;
5019     }
5020 }
5021
5022 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5023    of a virtual function may refer to a base class, so we check the 'this'
5024    parameter.  */
5025
5026 static tree
5027 decl_class_context (tree decl)
5028 {
5029   tree context = NULL_TREE;
5030
5031   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5032     context = DECL_CONTEXT (decl);
5033   else
5034     context = TYPE_MAIN_VARIANT
5035       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5036
5037   if (context && !TYPE_P (context))
5038     context = NULL_TREE;
5039
5040   return context;
5041 }
5042 \f
5043 /* Add an attribute/value pair to a DIE.  */
5044
5045 static inline void
5046 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5047 {
5048   /* Maybe this should be an assert?  */
5049   if (die == NULL)
5050     return;
5051
5052   if (die->die_attr == NULL)
5053     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5054   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5055 }
5056
5057 static inline enum dw_val_class
5058 AT_class (dw_attr_ref a)
5059 {
5060   return a->dw_attr_val.val_class;
5061 }
5062
5063 /* Add a flag value attribute to a DIE.  */
5064
5065 static inline void
5066 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5067 {
5068   dw_attr_node attr;
5069
5070   attr.dw_attr = attr_kind;
5071   attr.dw_attr_val.val_class = dw_val_class_flag;
5072   attr.dw_attr_val.v.val_flag = flag;
5073   add_dwarf_attr (die, &attr);
5074 }
5075
5076 static inline unsigned
5077 AT_flag (dw_attr_ref a)
5078 {
5079   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5080   return a->dw_attr_val.v.val_flag;
5081 }
5082
5083 /* Add a signed integer attribute value to a DIE.  */
5084
5085 static inline void
5086 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5087 {
5088   dw_attr_node attr;
5089
5090   attr.dw_attr = attr_kind;
5091   attr.dw_attr_val.val_class = dw_val_class_const;
5092   attr.dw_attr_val.v.val_int = int_val;
5093   add_dwarf_attr (die, &attr);
5094 }
5095
5096 static inline HOST_WIDE_INT
5097 AT_int (dw_attr_ref a)
5098 {
5099   gcc_assert (a && AT_class (a) == dw_val_class_const);
5100   return a->dw_attr_val.v.val_int;
5101 }
5102
5103 /* Add an unsigned integer attribute value to a DIE.  */
5104
5105 static inline void
5106 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5107                  unsigned HOST_WIDE_INT unsigned_val)
5108 {
5109   dw_attr_node attr;
5110
5111   attr.dw_attr = attr_kind;
5112   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5113   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5114   add_dwarf_attr (die, &attr);
5115 }
5116
5117 static inline unsigned HOST_WIDE_INT
5118 AT_unsigned (dw_attr_ref a)
5119 {
5120   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5121   return a->dw_attr_val.v.val_unsigned;
5122 }
5123
5124 /* Add an unsigned double integer attribute value to a DIE.  */
5125
5126 static inline void
5127 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5128                   long unsigned int val_hi, long unsigned int val_low)
5129 {
5130   dw_attr_node attr;
5131
5132   attr.dw_attr = attr_kind;
5133   attr.dw_attr_val.val_class = dw_val_class_long_long;
5134   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5135   attr.dw_attr_val.v.val_long_long.low = val_low;
5136   add_dwarf_attr (die, &attr);
5137 }
5138
5139 /* Add a floating point attribute value to a DIE and return it.  */
5140
5141 static inline void
5142 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5143             unsigned int length, unsigned int elt_size, unsigned char *array)
5144 {
5145   dw_attr_node attr;
5146
5147   attr.dw_attr = attr_kind;
5148   attr.dw_attr_val.val_class = dw_val_class_vec;
5149   attr.dw_attr_val.v.val_vec.length = length;
5150   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5151   attr.dw_attr_val.v.val_vec.array = array;
5152   add_dwarf_attr (die, &attr);
5153 }
5154
5155 /* Hash and equality functions for debug_str_hash.  */
5156
5157 static hashval_t
5158 debug_str_do_hash (const void *x)
5159 {
5160   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5161 }
5162
5163 static int
5164 debug_str_eq (const void *x1, const void *x2)
5165 {
5166   return strcmp ((((const struct indirect_string_node *)x1)->str),
5167                  (const char *)x2) == 0;
5168 }
5169
5170 /* Add a string attribute value to a DIE.  */
5171
5172 static inline void
5173 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5174 {
5175   dw_attr_node attr;
5176   struct indirect_string_node *node;
5177   void **slot;
5178
5179   if (! debug_str_hash)
5180     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5181                                       debug_str_eq, NULL);
5182
5183   slot = htab_find_slot_with_hash (debug_str_hash, str,
5184                                    htab_hash_string (str), INSERT);
5185   if (*slot == NULL)
5186     {
5187       node = (struct indirect_string_node *)
5188                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5189       node->str = ggc_strdup (str);
5190       *slot = node;
5191     }
5192   else
5193     node = (struct indirect_string_node *) *slot;
5194
5195   node->refcount++;
5196
5197   attr.dw_attr = attr_kind;
5198   attr.dw_attr_val.val_class = dw_val_class_str;
5199   attr.dw_attr_val.v.val_str = node;
5200   add_dwarf_attr (die, &attr);
5201 }
5202
5203 static inline const char *
5204 AT_string (dw_attr_ref a)
5205 {
5206   gcc_assert (a && AT_class (a) == dw_val_class_str);
5207   return a->dw_attr_val.v.val_str->str;
5208 }
5209
5210 /* Find out whether a string should be output inline in DIE
5211    or out-of-line in .debug_str section.  */
5212
5213 static int
5214 AT_string_form (dw_attr_ref a)
5215 {
5216   struct indirect_string_node *node;
5217   unsigned int len;
5218   char label[32];
5219
5220   gcc_assert (a && AT_class (a) == dw_val_class_str);
5221
5222   node = a->dw_attr_val.v.val_str;
5223   if (node->form)
5224     return node->form;
5225
5226   len = strlen (node->str) + 1;
5227
5228   /* If the string is shorter or equal to the size of the reference, it is
5229      always better to put it inline.  */
5230   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5231     return node->form = DW_FORM_string;
5232
5233   /* If we cannot expect the linker to merge strings in .debug_str
5234      section, only put it into .debug_str if it is worth even in this
5235      single module.  */
5236   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5237       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5238     return node->form = DW_FORM_string;
5239
5240   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5241   ++dw2_string_counter;
5242   node->label = xstrdup (label);
5243
5244   return node->form = DW_FORM_strp;
5245 }
5246
5247 /* Add a DIE reference attribute value to a DIE.  */
5248
5249 static inline void
5250 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5251 {
5252   dw_attr_node attr;
5253
5254   attr.dw_attr = attr_kind;
5255   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5256   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5257   attr.dw_attr_val.v.val_die_ref.external = 0;
5258   add_dwarf_attr (die, &attr);
5259 }
5260
5261 /* Add an AT_specification attribute to a DIE, and also make the back
5262    pointer from the specification to the definition.  */
5263
5264 static inline void
5265 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5266 {
5267   add_AT_die_ref (die, DW_AT_specification, targ_die);
5268   gcc_assert (!targ_die->die_definition);
5269   targ_die->die_definition = die;
5270 }
5271
5272 static inline dw_die_ref
5273 AT_ref (dw_attr_ref a)
5274 {
5275   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5276   return a->dw_attr_val.v.val_die_ref.die;
5277 }
5278
5279 static inline int
5280 AT_ref_external (dw_attr_ref a)
5281 {
5282   if (a && AT_class (a) == dw_val_class_die_ref)
5283     return a->dw_attr_val.v.val_die_ref.external;
5284
5285   return 0;
5286 }
5287
5288 static inline void
5289 set_AT_ref_external (dw_attr_ref a, int i)
5290 {
5291   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5292   a->dw_attr_val.v.val_die_ref.external = i;
5293 }
5294
5295 /* Add an FDE reference attribute value to a DIE.  */
5296
5297 static inline void
5298 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5299 {
5300   dw_attr_node attr;
5301
5302   attr.dw_attr = attr_kind;
5303   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5304   attr.dw_attr_val.v.val_fde_index = targ_fde;
5305   add_dwarf_attr (die, &attr);
5306 }
5307
5308 /* Add a location description attribute value to a DIE.  */
5309
5310 static inline void
5311 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5312 {
5313   dw_attr_node attr;
5314
5315   attr.dw_attr = attr_kind;
5316   attr.dw_attr_val.val_class = dw_val_class_loc;
5317   attr.dw_attr_val.v.val_loc = loc;
5318   add_dwarf_attr (die, &attr);
5319 }
5320
5321 static inline dw_loc_descr_ref
5322 AT_loc (dw_attr_ref a)
5323 {
5324   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5325   return a->dw_attr_val.v.val_loc;
5326 }
5327
5328 static inline void
5329 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5330 {
5331   dw_attr_node attr;
5332
5333   attr.dw_attr = attr_kind;
5334   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5335   attr.dw_attr_val.v.val_loc_list = loc_list;
5336   add_dwarf_attr (die, &attr);
5337   have_location_lists = true;
5338 }
5339
5340 static inline dw_loc_list_ref
5341 AT_loc_list (dw_attr_ref a)
5342 {
5343   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5344   return a->dw_attr_val.v.val_loc_list;
5345 }
5346
5347 /* Add an address constant attribute value to a DIE.  */
5348
5349 static inline void
5350 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5351 {
5352   dw_attr_node attr;
5353
5354   attr.dw_attr = attr_kind;
5355   attr.dw_attr_val.val_class = dw_val_class_addr;
5356   attr.dw_attr_val.v.val_addr = addr;
5357   add_dwarf_attr (die, &attr);
5358 }
5359
5360 /* Get the RTX from to an address DIE attribute.  */
5361
5362 static inline rtx
5363 AT_addr (dw_attr_ref a)
5364 {
5365   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5366   return a->dw_attr_val.v.val_addr;
5367 }
5368
5369 /* Add a file attribute value to a DIE.  */
5370
5371 static inline void
5372 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5373              struct dwarf_file_data *fd)
5374 {
5375   dw_attr_node attr;
5376
5377   attr.dw_attr = attr_kind;
5378   attr.dw_attr_val.val_class = dw_val_class_file;
5379   attr.dw_attr_val.v.val_file = fd;
5380   add_dwarf_attr (die, &attr);
5381 }
5382
5383 /* Get the dwarf_file_data from a file DIE attribute.  */
5384
5385 static inline struct dwarf_file_data *
5386 AT_file (dw_attr_ref a)
5387 {
5388   gcc_assert (a && AT_class (a) == dw_val_class_file);
5389   return a->dw_attr_val.v.val_file;
5390 }
5391
5392 /* Add a label identifier attribute value to a DIE.  */
5393
5394 static inline void
5395 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5396 {
5397   dw_attr_node attr;
5398
5399   attr.dw_attr = attr_kind;
5400   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5401   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5402   add_dwarf_attr (die, &attr);
5403 }
5404
5405 /* Add a section offset attribute value to a DIE, an offset into the
5406    debug_line section.  */
5407
5408 static inline void
5409 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5410                 const char *label)
5411 {
5412   dw_attr_node attr;
5413
5414   attr.dw_attr = attr_kind;
5415   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5416   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5417   add_dwarf_attr (die, &attr);
5418 }
5419
5420 /* Add a section offset attribute value to a DIE, an offset into the
5421    debug_macinfo section.  */
5422
5423 static inline void
5424 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5425                const char *label)
5426 {
5427   dw_attr_node attr;
5428
5429   attr.dw_attr = attr_kind;
5430   attr.dw_attr_val.val_class = dw_val_class_macptr;
5431   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5432   add_dwarf_attr (die, &attr);
5433 }
5434
5435 /* Add an offset attribute value to a DIE.  */
5436
5437 static inline void
5438 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5439                unsigned HOST_WIDE_INT offset)
5440 {
5441   dw_attr_node attr;
5442
5443   attr.dw_attr = attr_kind;
5444   attr.dw_attr_val.val_class = dw_val_class_offset;
5445   attr.dw_attr_val.v.val_offset = offset;
5446   add_dwarf_attr (die, &attr);
5447 }
5448
5449 /* Add an range_list attribute value to a DIE.  */
5450
5451 static void
5452 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5453                    long unsigned int offset)
5454 {
5455   dw_attr_node attr;
5456
5457   attr.dw_attr = attr_kind;
5458   attr.dw_attr_val.val_class = dw_val_class_range_list;
5459   attr.dw_attr_val.v.val_offset = offset;
5460   add_dwarf_attr (die, &attr);
5461 }
5462
5463 static inline const char *
5464 AT_lbl (dw_attr_ref a)
5465 {
5466   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5467                     || AT_class (a) == dw_val_class_lineptr
5468                     || AT_class (a) == dw_val_class_macptr));
5469   return a->dw_attr_val.v.val_lbl_id;
5470 }
5471
5472 /* Get the attribute of type attr_kind.  */
5473
5474 static dw_attr_ref
5475 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5476 {
5477   dw_attr_ref a;
5478   unsigned ix;
5479   dw_die_ref spec = NULL;
5480
5481   if (! die)
5482     return NULL;
5483
5484   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5485     if (a->dw_attr == attr_kind)
5486       return a;
5487     else if (a->dw_attr == DW_AT_specification
5488              || a->dw_attr == DW_AT_abstract_origin)
5489       spec = AT_ref (a);
5490
5491   if (spec)
5492     return get_AT (spec, attr_kind);
5493
5494   return NULL;
5495 }
5496
5497 /* Return the "low pc" attribute value, typically associated with a subprogram
5498    DIE.  Return null if the "low pc" attribute is either not present, or if it
5499    cannot be represented as an assembler label identifier.  */
5500
5501 static inline const char *
5502 get_AT_low_pc (dw_die_ref die)
5503 {
5504   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5505
5506   return a ? AT_lbl (a) : NULL;
5507 }
5508
5509 /* Return the "high pc" attribute value, typically associated with a subprogram
5510    DIE.  Return null if the "high pc" attribute is either not present, or if it
5511    cannot be represented as an assembler label identifier.  */
5512
5513 static inline const char *
5514 get_AT_hi_pc (dw_die_ref die)
5515 {
5516   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5517
5518   return a ? AT_lbl (a) : NULL;
5519 }
5520
5521 /* Return the value of the string attribute designated by ATTR_KIND, or
5522    NULL if it is not present.  */
5523
5524 static inline const char *
5525 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5526 {
5527   dw_attr_ref a = get_AT (die, attr_kind);
5528
5529   return a ? AT_string (a) : NULL;
5530 }
5531
5532 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5533    if it is not present.  */
5534
5535 static inline int
5536 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5537 {
5538   dw_attr_ref a = get_AT (die, attr_kind);
5539
5540   return a ? AT_flag (a) : 0;
5541 }
5542
5543 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5544    if it is not present.  */
5545
5546 static inline unsigned
5547 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5548 {
5549   dw_attr_ref a = get_AT (die, attr_kind);
5550
5551   return a ? AT_unsigned (a) : 0;
5552 }
5553
5554 static inline dw_die_ref
5555 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5556 {
5557   dw_attr_ref a = get_AT (die, attr_kind);
5558
5559   return a ? AT_ref (a) : NULL;
5560 }
5561
5562 static inline struct dwarf_file_data *
5563 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5564 {
5565   dw_attr_ref a = get_AT (die, attr_kind);
5566
5567   return a ? AT_file (a) : NULL;
5568 }
5569
5570 /* Return TRUE if the language is C or C++.  */
5571
5572 static inline bool
5573 is_c_family (void)
5574 {
5575   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5576
5577   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5578           || lang == DW_LANG_C99
5579           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5580 }
5581
5582 /* Return TRUE if the language is C++.  */
5583
5584 static inline bool
5585 is_cxx (void)
5586 {
5587   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5588
5589   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5590 }
5591
5592 /* Return TRUE if the language is Fortran.  */
5593
5594 static inline bool
5595 is_fortran (void)
5596 {
5597   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5598
5599   return (lang == DW_LANG_Fortran77
5600           || lang == DW_LANG_Fortran90
5601           || lang == DW_LANG_Fortran95);
5602 }
5603
5604 /* Return TRUE if the language is Java.  */
5605
5606 static inline bool
5607 is_java (void)
5608 {
5609   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5610
5611   return lang == DW_LANG_Java;
5612 }
5613
5614 /* Return TRUE if the language is Ada.  */
5615
5616 static inline bool
5617 is_ada (void)
5618 {
5619   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5620
5621   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5622 }
5623
5624 /* Remove the specified attribute if present.  */
5625
5626 static void
5627 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5628 {
5629   dw_attr_ref a;
5630   unsigned ix;
5631
5632   if (! die)
5633     return;
5634
5635   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5636     if (a->dw_attr == attr_kind)
5637       {
5638         if (AT_class (a) == dw_val_class_str)
5639           if (a->dw_attr_val.v.val_str->refcount)
5640             a->dw_attr_val.v.val_str->refcount--;
5641
5642         /* VEC_ordered_remove should help reduce the number of abbrevs
5643            that are needed.  */
5644         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5645         return;
5646       }
5647 }
5648
5649 /* Remove CHILD from its parent.  PREV must have the property that
5650    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5651
5652 static void
5653 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5654 {
5655   gcc_assert (child->die_parent == prev->die_parent);
5656   gcc_assert (prev->die_sib == child);
5657   if (prev == child)
5658     {
5659       gcc_assert (child->die_parent->die_child == child);
5660       prev = NULL;
5661     }
5662   else
5663     prev->die_sib = child->die_sib;
5664   if (child->die_parent->die_child == child)
5665     child->die_parent->die_child = prev;
5666 }
5667
5668 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5669    matches TAG.  */
5670
5671 static void
5672 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5673 {
5674   dw_die_ref c;
5675
5676   c = die->die_child;
5677   if (c) do {
5678     dw_die_ref prev = c;
5679     c = c->die_sib;
5680     while (c->die_tag == tag)
5681       {
5682         remove_child_with_prev (c, prev);
5683         /* Might have removed every child.  */
5684         if (c == c->die_sib)
5685           return;
5686         c = c->die_sib;
5687       }
5688   } while (c != die->die_child);
5689 }
5690
5691 /* Add a CHILD_DIE as the last child of DIE.  */
5692
5693 static void
5694 add_child_die (dw_die_ref die, dw_die_ref child_die)
5695 {
5696   /* FIXME this should probably be an assert.  */
5697   if (! die || ! child_die)
5698     return;
5699   gcc_assert (die != child_die);
5700
5701   child_die->die_parent = die;
5702   if (die->die_child)
5703     {
5704       child_die->die_sib = die->die_child->die_sib;
5705       die->die_child->die_sib = child_die;
5706     }
5707   else
5708     child_die->die_sib = child_die;
5709   die->die_child = child_die;
5710 }
5711
5712 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5713    is the specification, to the end of PARENT's list of children.
5714    This is done by removing and re-adding it.  */
5715
5716 static void
5717 splice_child_die (dw_die_ref parent, dw_die_ref child)
5718 {
5719   dw_die_ref p;
5720
5721   /* We want the declaration DIE from inside the class, not the
5722      specification DIE at toplevel.  */
5723   if (child->die_parent != parent)
5724     {
5725       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5726
5727       if (tmp)
5728         child = tmp;
5729     }
5730
5731   gcc_assert (child->die_parent == parent
5732               || (child->die_parent
5733                   == get_AT_ref (parent, DW_AT_specification)));
5734
5735   for (p = child->die_parent->die_child; ; p = p->die_sib)
5736     if (p->die_sib == child)
5737       {
5738         remove_child_with_prev (child, p);
5739         break;
5740       }
5741
5742   add_child_die (parent, child);
5743 }
5744
5745 /* Return a pointer to a newly created DIE node.  */
5746
5747 static inline dw_die_ref
5748 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5749 {
5750   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5751
5752   die->die_tag = tag_value;
5753
5754   if (parent_die != NULL)
5755     add_child_die (parent_die, die);
5756   else
5757     {
5758       limbo_die_node *limbo_node;
5759
5760       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5761       limbo_node->die = die;
5762       limbo_node->created_for = t;
5763       limbo_node->next = limbo_die_list;
5764       limbo_die_list = limbo_node;
5765     }
5766
5767   return die;
5768 }
5769
5770 /* Return the DIE associated with the given type specifier.  */
5771
5772 static inline dw_die_ref
5773 lookup_type_die (tree type)
5774 {
5775   return TYPE_SYMTAB_DIE (type);
5776 }
5777
5778 /* Equate a DIE to a given type specifier.  */
5779
5780 static inline void
5781 equate_type_number_to_die (tree type, dw_die_ref type_die)
5782 {
5783   TYPE_SYMTAB_DIE (type) = type_die;
5784 }
5785
5786 /* Returns a hash value for X (which really is a die_struct).  */
5787
5788 static hashval_t
5789 decl_die_table_hash (const void *x)
5790 {
5791   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5792 }
5793
5794 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5795
5796 static int
5797 decl_die_table_eq (const void *x, const void *y)
5798 {
5799   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5800 }
5801
5802 /* Return the DIE associated with a given declaration.  */
5803
5804 static inline dw_die_ref
5805 lookup_decl_die (tree decl)
5806 {
5807   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5808 }
5809
5810 /* Returns a hash value for X (which really is a var_loc_list).  */
5811
5812 static hashval_t
5813 decl_loc_table_hash (const void *x)
5814 {
5815   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5816 }
5817
5818 /* Return nonzero if decl_id of var_loc_list X is the same as
5819    UID of decl *Y.  */
5820
5821 static int
5822 decl_loc_table_eq (const void *x, const void *y)
5823 {
5824   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5825 }
5826
5827 /* Return the var_loc list associated with a given declaration.  */
5828
5829 static inline var_loc_list *
5830 lookup_decl_loc (const_tree decl)
5831 {
5832   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5833 }
5834
5835 /* Equate a DIE to a particular declaration.  */
5836
5837 static void
5838 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5839 {
5840   unsigned int decl_id = DECL_UID (decl);
5841   void **slot;
5842
5843   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5844   *slot = decl_die;
5845   decl_die->decl_id = decl_id;
5846 }
5847
5848 /* Add a variable location node to the linked list for DECL.  */
5849
5850 static void
5851 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5852 {
5853   unsigned int decl_id = DECL_UID (decl);
5854   var_loc_list *temp;
5855   void **slot;
5856
5857   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5858   if (*slot == NULL)
5859     {
5860       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5861       temp->decl_id = decl_id;
5862       *slot = temp;
5863     }
5864   else
5865     temp = *slot;
5866
5867   if (temp->last)
5868     {
5869       /* If the current location is the same as the end of the list,
5870          and either both or neither of the locations is uninitialized,
5871          we have nothing to do.  */
5872       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5873                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5874           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5875                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5876               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5877                    == VAR_INIT_STATUS_UNINITIALIZED)
5878                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5879                       == VAR_INIT_STATUS_UNINITIALIZED))))
5880         {
5881           /* Add LOC to the end of list and update LAST.  */
5882           temp->last->next = loc;
5883           temp->last = loc;
5884         }
5885     }
5886   /* Do not add empty location to the beginning of the list.  */
5887   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5888     {
5889       temp->first = loc;
5890       temp->last = loc;
5891     }
5892 }
5893 \f
5894 /* Keep track of the number of spaces used to indent the
5895    output of the debugging routines that print the structure of
5896    the DIE internal representation.  */
5897 static int print_indent;
5898
5899 /* Indent the line the number of spaces given by print_indent.  */
5900
5901 static inline void
5902 print_spaces (FILE *outfile)
5903 {
5904   fprintf (outfile, "%*s", print_indent, "");
5905 }
5906
5907 /* Print the information associated with a given DIE, and its children.
5908    This routine is a debugging aid only.  */
5909
5910 static void
5911 print_die (dw_die_ref die, FILE *outfile)
5912 {
5913   dw_attr_ref a;
5914   dw_die_ref c;
5915   unsigned ix;
5916
5917   print_spaces (outfile);
5918   fprintf (outfile, "DIE %4ld: %s\n",
5919            die->die_offset, dwarf_tag_name (die->die_tag));
5920   print_spaces (outfile);
5921   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5922   fprintf (outfile, " offset: %ld\n", die->die_offset);
5923
5924   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5925     {
5926       print_spaces (outfile);
5927       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5928
5929       switch (AT_class (a))
5930         {
5931         case dw_val_class_addr:
5932           fprintf (outfile, "address");
5933           break;
5934         case dw_val_class_offset:
5935           fprintf (outfile, "offset");
5936           break;
5937         case dw_val_class_loc:
5938           fprintf (outfile, "location descriptor");
5939           break;
5940         case dw_val_class_loc_list:
5941           fprintf (outfile, "location list -> label:%s",
5942                    AT_loc_list (a)->ll_symbol);
5943           break;
5944         case dw_val_class_range_list:
5945           fprintf (outfile, "range list");
5946           break;
5947         case dw_val_class_const:
5948           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5949           break;
5950         case dw_val_class_unsigned_const:
5951           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5952           break;
5953         case dw_val_class_long_long:
5954           fprintf (outfile, "constant (%lu,%lu)",
5955                    a->dw_attr_val.v.val_long_long.hi,
5956                    a->dw_attr_val.v.val_long_long.low);
5957           break;
5958         case dw_val_class_vec:
5959           fprintf (outfile, "floating-point or vector constant");
5960           break;
5961         case dw_val_class_flag:
5962           fprintf (outfile, "%u", AT_flag (a));
5963           break;
5964         case dw_val_class_die_ref:
5965           if (AT_ref (a) != NULL)
5966             {
5967               if (AT_ref (a)->die_symbol)
5968                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5969               else
5970                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5971             }
5972           else
5973             fprintf (outfile, "die -> <null>");
5974           break;
5975         case dw_val_class_lbl_id:
5976         case dw_val_class_lineptr:
5977         case dw_val_class_macptr:
5978           fprintf (outfile, "label: %s", AT_lbl (a));
5979           break;
5980         case dw_val_class_str:
5981           if (AT_string (a) != NULL)
5982             fprintf (outfile, "\"%s\"", AT_string (a));
5983           else
5984             fprintf (outfile, "<null>");
5985           break;
5986         case dw_val_class_file:
5987           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5988                    AT_file (a)->emitted_number);
5989           break;
5990         default:
5991           break;
5992         }
5993
5994       fprintf (outfile, "\n");
5995     }
5996
5997   if (die->die_child != NULL)
5998     {
5999       print_indent += 4;
6000       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6001       print_indent -= 4;
6002     }
6003   if (print_indent == 0)
6004     fprintf (outfile, "\n");
6005 }
6006
6007 /* Print the contents of the source code line number correspondence table.
6008    This routine is a debugging aid only.  */
6009
6010 static void
6011 print_dwarf_line_table (FILE *outfile)
6012 {
6013   unsigned i;
6014   dw_line_info_ref line_info;
6015
6016   fprintf (outfile, "\n\nDWARF source line information\n");
6017   for (i = 1; i < line_info_table_in_use; i++)
6018     {
6019       line_info = &line_info_table[i];
6020       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6021                line_info->dw_file_num,
6022                line_info->dw_line_num);
6023     }
6024
6025   fprintf (outfile, "\n\n");
6026 }
6027
6028 /* Print the information collected for a given DIE.  */
6029
6030 void
6031 debug_dwarf_die (dw_die_ref die)
6032 {
6033   print_die (die, stderr);
6034 }
6035
6036 /* Print all DWARF information collected for the compilation unit.
6037    This routine is a debugging aid only.  */
6038
6039 void
6040 debug_dwarf (void)
6041 {
6042   print_indent = 0;
6043   print_die (comp_unit_die, stderr);
6044   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6045     print_dwarf_line_table (stderr);
6046 }
6047 \f
6048 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6049    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6050    DIE that marks the start of the DIEs for this include file.  */
6051
6052 static dw_die_ref
6053 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6054 {
6055   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6056   dw_die_ref new_unit = gen_compile_unit_die (filename);
6057
6058   new_unit->die_sib = old_unit;
6059   return new_unit;
6060 }
6061
6062 /* Close an include-file CU and reopen the enclosing one.  */
6063
6064 static dw_die_ref
6065 pop_compile_unit (dw_die_ref old_unit)
6066 {
6067   dw_die_ref new_unit = old_unit->die_sib;
6068
6069   old_unit->die_sib = NULL;
6070   return new_unit;
6071 }
6072
6073 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6074 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6075
6076 /* Calculate the checksum of a location expression.  */
6077
6078 static inline void
6079 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6080 {
6081   CHECKSUM (loc->dw_loc_opc);
6082   CHECKSUM (loc->dw_loc_oprnd1);
6083   CHECKSUM (loc->dw_loc_oprnd2);
6084 }
6085
6086 /* Calculate the checksum of an attribute.  */
6087
6088 static void
6089 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6090 {
6091   dw_loc_descr_ref loc;
6092   rtx r;
6093
6094   CHECKSUM (at->dw_attr);
6095
6096   /* We don't care that this was compiled with a different compiler
6097      snapshot; if the output is the same, that's what matters.  */
6098   if (at->dw_attr == DW_AT_producer)
6099     return;
6100
6101   switch (AT_class (at))
6102     {
6103     case dw_val_class_const:
6104       CHECKSUM (at->dw_attr_val.v.val_int);
6105       break;
6106     case dw_val_class_unsigned_const:
6107       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6108       break;
6109     case dw_val_class_long_long:
6110       CHECKSUM (at->dw_attr_val.v.val_long_long);
6111       break;
6112     case dw_val_class_vec:
6113       CHECKSUM (at->dw_attr_val.v.val_vec);
6114       break;
6115     case dw_val_class_flag:
6116       CHECKSUM (at->dw_attr_val.v.val_flag);
6117       break;
6118     case dw_val_class_str:
6119       CHECKSUM_STRING (AT_string (at));
6120       break;
6121
6122     case dw_val_class_addr:
6123       r = AT_addr (at);
6124       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6125       CHECKSUM_STRING (XSTR (r, 0));
6126       break;
6127
6128     case dw_val_class_offset:
6129       CHECKSUM (at->dw_attr_val.v.val_offset);
6130       break;
6131
6132     case dw_val_class_loc:
6133       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6134         loc_checksum (loc, ctx);
6135       break;
6136
6137     case dw_val_class_die_ref:
6138       die_checksum (AT_ref (at), ctx, mark);
6139       break;
6140
6141     case dw_val_class_fde_ref:
6142     case dw_val_class_lbl_id:
6143     case dw_val_class_lineptr:
6144     case dw_val_class_macptr:
6145       break;
6146
6147     case dw_val_class_file:
6148       CHECKSUM_STRING (AT_file (at)->filename);
6149       break;
6150
6151     default:
6152       break;
6153     }
6154 }
6155
6156 /* Calculate the checksum of a DIE.  */
6157
6158 static void
6159 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6160 {
6161   dw_die_ref c;
6162   dw_attr_ref a;
6163   unsigned ix;
6164
6165   /* To avoid infinite recursion.  */
6166   if (die->die_mark)
6167     {
6168       CHECKSUM (die->die_mark);
6169       return;
6170     }
6171   die->die_mark = ++(*mark);
6172
6173   CHECKSUM (die->die_tag);
6174
6175   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6176     attr_checksum (a, ctx, mark);
6177
6178   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6179 }
6180
6181 #undef CHECKSUM
6182 #undef CHECKSUM_STRING
6183
6184 /* Do the location expressions look same?  */
6185 static inline int
6186 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6187 {
6188   return loc1->dw_loc_opc == loc2->dw_loc_opc
6189          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6190          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6191 }
6192
6193 /* Do the values look the same?  */
6194 static int
6195 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6196 {
6197   dw_loc_descr_ref loc1, loc2;
6198   rtx r1, r2;
6199
6200   if (v1->val_class != v2->val_class)
6201     return 0;
6202
6203   switch (v1->val_class)
6204     {
6205     case dw_val_class_const:
6206       return v1->v.val_int == v2->v.val_int;
6207     case dw_val_class_unsigned_const:
6208       return v1->v.val_unsigned == v2->v.val_unsigned;
6209     case dw_val_class_long_long:
6210       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6211              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6212     case dw_val_class_vec:
6213       if (v1->v.val_vec.length != v2->v.val_vec.length
6214           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6215         return 0;
6216       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6217                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6218         return 0;
6219       return 1;
6220     case dw_val_class_flag:
6221       return v1->v.val_flag == v2->v.val_flag;
6222     case dw_val_class_str:
6223       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6224
6225     case dw_val_class_addr:
6226       r1 = v1->v.val_addr;
6227       r2 = v2->v.val_addr;
6228       if (GET_CODE (r1) != GET_CODE (r2))
6229         return 0;
6230       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6231       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6232
6233     case dw_val_class_offset:
6234       return v1->v.val_offset == v2->v.val_offset;
6235
6236     case dw_val_class_loc:
6237       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6238            loc1 && loc2;
6239            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6240         if (!same_loc_p (loc1, loc2, mark))
6241           return 0;
6242       return !loc1 && !loc2;
6243
6244     case dw_val_class_die_ref:
6245       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6246
6247     case dw_val_class_fde_ref:
6248     case dw_val_class_lbl_id:
6249     case dw_val_class_lineptr:
6250     case dw_val_class_macptr:
6251       return 1;
6252
6253     case dw_val_class_file:
6254       return v1->v.val_file == v2->v.val_file;
6255
6256     default:
6257       return 1;
6258     }
6259 }
6260
6261 /* Do the attributes look the same?  */
6262
6263 static int
6264 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6265 {
6266   if (at1->dw_attr != at2->dw_attr)
6267     return 0;
6268
6269   /* We don't care that this was compiled with a different compiler
6270      snapshot; if the output is the same, that's what matters. */
6271   if (at1->dw_attr == DW_AT_producer)
6272     return 1;
6273
6274   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6275 }
6276
6277 /* Do the dies look the same?  */
6278
6279 static int
6280 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6281 {
6282   dw_die_ref c1, c2;
6283   dw_attr_ref a1;
6284   unsigned ix;
6285
6286   /* To avoid infinite recursion.  */
6287   if (die1->die_mark)
6288     return die1->die_mark == die2->die_mark;
6289   die1->die_mark = die2->die_mark = ++(*mark);
6290
6291   if (die1->die_tag != die2->die_tag)
6292     return 0;
6293
6294   if (VEC_length (dw_attr_node, die1->die_attr)
6295       != VEC_length (dw_attr_node, die2->die_attr))
6296     return 0;
6297
6298   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6299     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6300       return 0;
6301
6302   c1 = die1->die_child;
6303   c2 = die2->die_child;
6304   if (! c1)
6305     {
6306       if (c2)
6307         return 0;
6308     }
6309   else
6310     for (;;)
6311       {
6312         if (!same_die_p (c1, c2, mark))
6313           return 0;
6314         c1 = c1->die_sib;
6315         c2 = c2->die_sib;
6316         if (c1 == die1->die_child)
6317           {
6318             if (c2 == die2->die_child)
6319               break;
6320             else
6321               return 0;
6322           }
6323     }
6324
6325   return 1;
6326 }
6327
6328 /* Do the dies look the same?  Wrapper around same_die_p.  */
6329
6330 static int
6331 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6332 {
6333   int mark = 0;
6334   int ret = same_die_p (die1, die2, &mark);
6335
6336   unmark_all_dies (die1);
6337   unmark_all_dies (die2);
6338
6339   return ret;
6340 }
6341
6342 /* The prefix to attach to symbols on DIEs in the current comdat debug
6343    info section.  */
6344 static char *comdat_symbol_id;
6345
6346 /* The index of the current symbol within the current comdat CU.  */
6347 static unsigned int comdat_symbol_number;
6348
6349 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6350    children, and set comdat_symbol_id accordingly.  */
6351
6352 static void
6353 compute_section_prefix (dw_die_ref unit_die)
6354 {
6355   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6356   const char *base = die_name ? lbasename (die_name) : "anonymous";
6357   char *name = alloca (strlen (base) + 64);
6358   char *p;
6359   int i, mark;
6360   unsigned char checksum[16];
6361   struct md5_ctx ctx;
6362
6363   /* Compute the checksum of the DIE, then append part of it as hex digits to
6364      the name filename of the unit.  */
6365
6366   md5_init_ctx (&ctx);
6367   mark = 0;
6368   die_checksum (unit_die, &ctx, &mark);
6369   unmark_all_dies (unit_die);
6370   md5_finish_ctx (&ctx, checksum);
6371
6372   sprintf (name, "%s.", base);
6373   clean_symbol_name (name);
6374
6375   p = name + strlen (name);
6376   for (i = 0; i < 4; i++)
6377     {
6378       sprintf (p, "%.2x", checksum[i]);
6379       p += 2;
6380     }
6381
6382   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6383   comdat_symbol_number = 0;
6384 }
6385
6386 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6387
6388 static int
6389 is_type_die (dw_die_ref die)
6390 {
6391   switch (die->die_tag)
6392     {
6393     case DW_TAG_array_type:
6394     case DW_TAG_class_type:
6395     case DW_TAG_interface_type:
6396     case DW_TAG_enumeration_type:
6397     case DW_TAG_pointer_type:
6398     case DW_TAG_reference_type:
6399     case DW_TAG_string_type:
6400     case DW_TAG_structure_type:
6401     case DW_TAG_subroutine_type:
6402     case DW_TAG_union_type:
6403     case DW_TAG_ptr_to_member_type:
6404     case DW_TAG_set_type:
6405     case DW_TAG_subrange_type:
6406     case DW_TAG_base_type:
6407     case DW_TAG_const_type:
6408     case DW_TAG_file_type:
6409     case DW_TAG_packed_type:
6410     case DW_TAG_volatile_type:
6411     case DW_TAG_typedef:
6412       return 1;
6413     default:
6414       return 0;
6415     }
6416 }
6417
6418 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6419    Basically, we want to choose the bits that are likely to be shared between
6420    compilations (types) and leave out the bits that are specific to individual
6421    compilations (functions).  */
6422
6423 static int
6424 is_comdat_die (dw_die_ref c)
6425 {
6426   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6427      we do for stabs.  The advantage is a greater likelihood of sharing between
6428      objects that don't include headers in the same order (and therefore would
6429      put the base types in a different comdat).  jason 8/28/00 */
6430
6431   if (c->die_tag == DW_TAG_base_type)
6432     return 0;
6433
6434   if (c->die_tag == DW_TAG_pointer_type
6435       || c->die_tag == DW_TAG_reference_type
6436       || c->die_tag == DW_TAG_const_type
6437       || c->die_tag == DW_TAG_volatile_type)
6438     {
6439       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6440
6441       return t ? is_comdat_die (t) : 0;
6442     }
6443
6444   return is_type_die (c);
6445 }
6446
6447 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6448    compilation unit.  */
6449
6450 static int
6451 is_symbol_die (dw_die_ref c)
6452 {
6453   return (is_type_die (c)
6454           || (get_AT (c, DW_AT_declaration)
6455               && !get_AT (c, DW_AT_specification))
6456           || c->die_tag == DW_TAG_namespace);
6457 }
6458
6459 static char *
6460 gen_internal_sym (const char *prefix)
6461 {
6462   char buf[256];
6463
6464   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6465   return xstrdup (buf);
6466 }
6467
6468 /* Assign symbols to all worthy DIEs under DIE.  */
6469
6470 static void
6471 assign_symbol_names (dw_die_ref die)
6472 {
6473   dw_die_ref c;
6474
6475   if (is_symbol_die (die))
6476     {
6477       if (comdat_symbol_id)
6478         {
6479           char *p = alloca (strlen (comdat_symbol_id) + 64);
6480
6481           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6482                    comdat_symbol_id, comdat_symbol_number++);
6483           die->die_symbol = xstrdup (p);
6484         }
6485       else
6486         die->die_symbol = gen_internal_sym ("LDIE");
6487     }
6488
6489   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6490 }
6491
6492 struct cu_hash_table_entry
6493 {
6494   dw_die_ref cu;
6495   unsigned min_comdat_num, max_comdat_num;
6496   struct cu_hash_table_entry *next;
6497 };
6498
6499 /* Routines to manipulate hash table of CUs.  */
6500 static hashval_t
6501 htab_cu_hash (const void *of)
6502 {
6503   const struct cu_hash_table_entry *entry = of;
6504
6505   return htab_hash_string (entry->cu->die_symbol);
6506 }
6507
6508 static int
6509 htab_cu_eq (const void *of1, const void *of2)
6510 {
6511   const struct cu_hash_table_entry *entry1 = of1;
6512   const struct die_struct *entry2 = of2;
6513
6514   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6515 }
6516
6517 static void
6518 htab_cu_del (void *what)
6519 {
6520   struct cu_hash_table_entry *next, *entry = what;
6521
6522   while (entry)
6523     {
6524       next = entry->next;
6525       free (entry);
6526       entry = next;
6527     }
6528 }
6529
6530 /* Check whether we have already seen this CU and set up SYM_NUM
6531    accordingly.  */
6532 static int
6533 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6534 {
6535   struct cu_hash_table_entry dummy;
6536   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6537
6538   dummy.max_comdat_num = 0;
6539
6540   slot = (struct cu_hash_table_entry **)
6541     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6542         INSERT);
6543   entry = *slot;
6544
6545   for (; entry; last = entry, entry = entry->next)
6546     {
6547       if (same_die_p_wrap (cu, entry->cu))
6548         break;
6549     }
6550
6551   if (entry)
6552     {
6553       *sym_num = entry->min_comdat_num;
6554       return 1;
6555     }
6556
6557   entry = XCNEW (struct cu_hash_table_entry);
6558   entry->cu = cu;
6559   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6560   entry->next = *slot;
6561   *slot = entry;
6562
6563   return 0;
6564 }
6565
6566 /* Record SYM_NUM to record of CU in HTABLE.  */
6567 static void
6568 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6569 {
6570   struct cu_hash_table_entry **slot, *entry;
6571
6572   slot = (struct cu_hash_table_entry **)
6573     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6574         NO_INSERT);
6575   entry = *slot;
6576
6577   entry->max_comdat_num = sym_num;
6578 }
6579
6580 /* Traverse the DIE (which is always comp_unit_die), and set up
6581    additional compilation units for each of the include files we see
6582    bracketed by BINCL/EINCL.  */
6583
6584 static void
6585 break_out_includes (dw_die_ref die)
6586 {
6587   dw_die_ref c;
6588   dw_die_ref unit = NULL;
6589   limbo_die_node *node, **pnode;
6590   htab_t cu_hash_table;
6591
6592   c = die->die_child;
6593   if (c) do {
6594     dw_die_ref prev = c;
6595     c = c->die_sib;
6596     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6597            || (unit && is_comdat_die (c)))
6598       {
6599         dw_die_ref next = c->die_sib;
6600
6601         /* This DIE is for a secondary CU; remove it from the main one.  */
6602         remove_child_with_prev (c, prev);
6603
6604         if (c->die_tag == DW_TAG_GNU_BINCL)
6605           unit = push_new_compile_unit (unit, c);
6606         else if (c->die_tag == DW_TAG_GNU_EINCL)
6607           unit = pop_compile_unit (unit);
6608         else
6609           add_child_die (unit, c);
6610         c = next;
6611         if (c == die->die_child)
6612           break;
6613       }
6614   } while (c != die->die_child);
6615
6616 #if 0
6617   /* We can only use this in debugging, since the frontend doesn't check
6618      to make sure that we leave every include file we enter.  */
6619   gcc_assert (!unit);
6620 #endif
6621
6622   assign_symbol_names (die);
6623   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6624   for (node = limbo_die_list, pnode = &limbo_die_list;
6625        node;
6626        node = node->next)
6627     {
6628       int is_dupl;
6629
6630       compute_section_prefix (node->die);
6631       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6632                         &comdat_symbol_number);
6633       assign_symbol_names (node->die);
6634       if (is_dupl)
6635         *pnode = node->next;
6636       else
6637         {
6638           pnode = &node->next;
6639           record_comdat_symbol_number (node->die, cu_hash_table,
6640                 comdat_symbol_number);
6641         }
6642     }
6643   htab_delete (cu_hash_table);
6644 }
6645
6646 /* Traverse the DIE and add a sibling attribute if it may have the
6647    effect of speeding up access to siblings.  To save some space,
6648    avoid generating sibling attributes for DIE's without children.  */
6649
6650 static void
6651 add_sibling_attributes (dw_die_ref die)
6652 {
6653   dw_die_ref c;
6654
6655   if (! die->die_child)
6656     return;
6657
6658   if (die->die_parent && die != die->die_parent->die_child)
6659     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6660
6661   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6662 }
6663
6664 /* Output all location lists for the DIE and its children.  */
6665
6666 static void
6667 output_location_lists (dw_die_ref die)
6668 {
6669   dw_die_ref c;
6670   dw_attr_ref a;
6671   unsigned ix;
6672
6673   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6674     if (AT_class (a) == dw_val_class_loc_list)
6675       output_loc_list (AT_loc_list (a));
6676
6677   FOR_EACH_CHILD (die, c, output_location_lists (c));
6678 }
6679
6680 /* The format of each DIE (and its attribute value pairs) is encoded in an
6681    abbreviation table.  This routine builds the abbreviation table and assigns
6682    a unique abbreviation id for each abbreviation entry.  The children of each
6683    die are visited recursively.  */
6684
6685 static void
6686 build_abbrev_table (dw_die_ref die)
6687 {
6688   unsigned long abbrev_id;
6689   unsigned int n_alloc;
6690   dw_die_ref c;
6691   dw_attr_ref a;
6692   unsigned ix;
6693
6694   /* Scan the DIE references, and mark as external any that refer to
6695      DIEs from other CUs (i.e. those which are not marked).  */
6696   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6697     if (AT_class (a) == dw_val_class_die_ref
6698         && AT_ref (a)->die_mark == 0)
6699       {
6700         gcc_assert (AT_ref (a)->die_symbol);
6701
6702         set_AT_ref_external (a, 1);
6703       }
6704
6705   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6706     {
6707       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6708       dw_attr_ref die_a, abbrev_a;
6709       unsigned ix;
6710       bool ok = true;
6711
6712       if (abbrev->die_tag != die->die_tag)
6713         continue;
6714       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6715         continue;
6716
6717       if (VEC_length (dw_attr_node, abbrev->die_attr)
6718           != VEC_length (dw_attr_node, die->die_attr))
6719         continue;
6720
6721       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6722         {
6723           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6724           if ((abbrev_a->dw_attr != die_a->dw_attr)
6725               || (value_format (abbrev_a) != value_format (die_a)))
6726             {
6727               ok = false;
6728               break;
6729             }
6730         }
6731       if (ok)
6732         break;
6733     }
6734
6735   if (abbrev_id >= abbrev_die_table_in_use)
6736     {
6737       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6738         {
6739           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6740           abbrev_die_table = ggc_realloc (abbrev_die_table,
6741                                           sizeof (dw_die_ref) * n_alloc);
6742
6743           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6744                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6745           abbrev_die_table_allocated = n_alloc;
6746         }
6747
6748       ++abbrev_die_table_in_use;
6749       abbrev_die_table[abbrev_id] = die;
6750     }
6751
6752   die->die_abbrev = abbrev_id;
6753   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6754 }
6755 \f
6756 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6757
6758 static int
6759 constant_size (long unsigned int value)
6760 {
6761   int log;
6762
6763   if (value == 0)
6764     log = 0;
6765   else
6766     log = floor_log2 (value);
6767
6768   log = log / 8;
6769   log = 1 << (floor_log2 (log) + 1);
6770
6771   return log;
6772 }
6773
6774 /* Return the size of a DIE as it is represented in the
6775    .debug_info section.  */
6776
6777 static unsigned long
6778 size_of_die (dw_die_ref die)
6779 {
6780   unsigned long size = 0;
6781   dw_attr_ref a;
6782   unsigned ix;
6783
6784   size += size_of_uleb128 (die->die_abbrev);
6785   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6786     {
6787       switch (AT_class (a))
6788         {
6789         case dw_val_class_addr:
6790           size += DWARF2_ADDR_SIZE;
6791           break;
6792         case dw_val_class_offset:
6793           size += DWARF_OFFSET_SIZE;
6794           break;
6795         case dw_val_class_loc:
6796           {
6797             unsigned long lsize = size_of_locs (AT_loc (a));
6798
6799             /* Block length.  */
6800             size += constant_size (lsize);
6801             size += lsize;
6802           }
6803           break;
6804         case dw_val_class_loc_list:
6805           size += DWARF_OFFSET_SIZE;
6806           break;
6807         case dw_val_class_range_list:
6808           size += DWARF_OFFSET_SIZE;
6809           break;
6810         case dw_val_class_const:
6811           size += size_of_sleb128 (AT_int (a));
6812           break;
6813         case dw_val_class_unsigned_const:
6814           size += constant_size (AT_unsigned (a));
6815           break;
6816         case dw_val_class_long_long:
6817           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6818           break;
6819         case dw_val_class_vec:
6820           size += 1 + (a->dw_attr_val.v.val_vec.length
6821                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6822           break;
6823         case dw_val_class_flag:
6824           size += 1;
6825           break;
6826         case dw_val_class_die_ref:
6827           if (AT_ref_external (a))
6828             size += DWARF2_ADDR_SIZE;
6829           else
6830             size += DWARF_OFFSET_SIZE;
6831           break;
6832         case dw_val_class_fde_ref:
6833           size += DWARF_OFFSET_SIZE;
6834           break;
6835         case dw_val_class_lbl_id:
6836           size += DWARF2_ADDR_SIZE;
6837           break;
6838         case dw_val_class_lineptr:
6839         case dw_val_class_macptr:
6840           size += DWARF_OFFSET_SIZE;
6841           break;
6842         case dw_val_class_str:
6843           if (AT_string_form (a) == DW_FORM_strp)
6844             size += DWARF_OFFSET_SIZE;
6845           else
6846             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6847           break;
6848         case dw_val_class_file:
6849           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6850           break;
6851         default:
6852           gcc_unreachable ();
6853         }
6854     }
6855
6856   return size;
6857 }
6858
6859 /* Size the debugging information associated with a given DIE.  Visits the
6860    DIE's children recursively.  Updates the global variable next_die_offset, on
6861    each time through.  Uses the current value of next_die_offset to update the
6862    die_offset field in each DIE.  */
6863
6864 static void
6865 calc_die_sizes (dw_die_ref die)
6866 {
6867   dw_die_ref c;
6868
6869   die->die_offset = next_die_offset;
6870   next_die_offset += size_of_die (die);
6871
6872   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6873
6874   if (die->die_child != NULL)
6875     /* Count the null byte used to terminate sibling lists.  */
6876     next_die_offset += 1;
6877 }
6878
6879 /* Set the marks for a die and its children.  We do this so
6880    that we know whether or not a reference needs to use FORM_ref_addr; only
6881    DIEs in the same CU will be marked.  We used to clear out the offset
6882    and use that as the flag, but ran into ordering problems.  */
6883
6884 static void
6885 mark_dies (dw_die_ref die)
6886 {
6887   dw_die_ref c;
6888
6889   gcc_assert (!die->die_mark);
6890
6891   die->die_mark = 1;
6892   FOR_EACH_CHILD (die, c, mark_dies (c));
6893 }
6894
6895 /* Clear the marks for a die and its children.  */
6896
6897 static void
6898 unmark_dies (dw_die_ref die)
6899 {
6900   dw_die_ref c;
6901
6902   gcc_assert (die->die_mark);
6903
6904   die->die_mark = 0;
6905   FOR_EACH_CHILD (die, c, unmark_dies (c));
6906 }
6907
6908 /* Clear the marks for a die, its children and referred dies.  */
6909
6910 static void
6911 unmark_all_dies (dw_die_ref die)
6912 {
6913   dw_die_ref c;
6914   dw_attr_ref a;
6915   unsigned ix;
6916
6917   if (!die->die_mark)
6918     return;
6919   die->die_mark = 0;
6920
6921   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6922
6923   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6924     if (AT_class (a) == dw_val_class_die_ref)
6925       unmark_all_dies (AT_ref (a));
6926 }
6927
6928 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6929    generated for the compilation unit.  */
6930
6931 static unsigned long
6932 size_of_pubnames (VEC (pubname_entry, gc) * names)
6933 {
6934   unsigned long size;
6935   unsigned i;
6936   pubname_ref p;
6937
6938   size = DWARF_PUBNAMES_HEADER_SIZE;
6939   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6940     if (names != pubtype_table
6941         || p->die->die_offset != 0
6942         || !flag_eliminate_unused_debug_types)
6943       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6944
6945   size += DWARF_OFFSET_SIZE;
6946   return size;
6947 }
6948
6949 /* Return the size of the information in the .debug_aranges section.  */
6950
6951 static unsigned long
6952 size_of_aranges (void)
6953 {
6954   unsigned long size;
6955
6956   size = DWARF_ARANGES_HEADER_SIZE;
6957
6958   /* Count the address/length pair for this compilation unit.  */
6959   if (text_section_used)
6960     size += 2 * DWARF2_ADDR_SIZE;
6961   if (cold_text_section_used)
6962     size += 2 * DWARF2_ADDR_SIZE;
6963   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6964
6965   /* Count the two zero words used to terminated the address range table.  */
6966   size += 2 * DWARF2_ADDR_SIZE;
6967   return size;
6968 }
6969 \f
6970 /* Select the encoding of an attribute value.  */
6971
6972 static enum dwarf_form
6973 value_format (dw_attr_ref a)
6974 {
6975   switch (a->dw_attr_val.val_class)
6976     {
6977     case dw_val_class_addr:
6978       return DW_FORM_addr;
6979     case dw_val_class_range_list:
6980     case dw_val_class_offset:
6981     case dw_val_class_loc_list:
6982       switch (DWARF_OFFSET_SIZE)
6983         {
6984         case 4:
6985           return DW_FORM_data4;
6986         case 8:
6987           return DW_FORM_data8;
6988         default:
6989           gcc_unreachable ();
6990         }
6991     case dw_val_class_loc:
6992       switch (constant_size (size_of_locs (AT_loc (a))))
6993         {
6994         case 1:
6995           return DW_FORM_block1;
6996         case 2:
6997           return DW_FORM_block2;
6998         default:
6999           gcc_unreachable ();
7000         }
7001     case dw_val_class_const:
7002       return DW_FORM_sdata;
7003     case dw_val_class_unsigned_const:
7004       switch (constant_size (AT_unsigned (a)))
7005         {
7006         case 1:
7007           return DW_FORM_data1;
7008         case 2:
7009           return DW_FORM_data2;
7010         case 4:
7011           return DW_FORM_data4;
7012         case 8:
7013           return DW_FORM_data8;
7014         default:
7015           gcc_unreachable ();
7016         }
7017     case dw_val_class_long_long:
7018       return DW_FORM_block1;
7019     case dw_val_class_vec:
7020       return DW_FORM_block1;
7021     case dw_val_class_flag:
7022       return DW_FORM_flag;
7023     case dw_val_class_die_ref:
7024       if (AT_ref_external (a))
7025         return DW_FORM_ref_addr;
7026       else
7027         return DW_FORM_ref;
7028     case dw_val_class_fde_ref:
7029       return DW_FORM_data;
7030     case dw_val_class_lbl_id:
7031       return DW_FORM_addr;
7032     case dw_val_class_lineptr:
7033     case dw_val_class_macptr:
7034       return DW_FORM_data;
7035     case dw_val_class_str:
7036       return AT_string_form (a);
7037     case dw_val_class_file:
7038       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7039         {
7040         case 1:
7041           return DW_FORM_data1;
7042         case 2:
7043           return DW_FORM_data2;
7044         case 4:
7045           return DW_FORM_data4;
7046         default:
7047           gcc_unreachable ();
7048         }
7049
7050     default:
7051       gcc_unreachable ();
7052     }
7053 }
7054
7055 /* Output the encoding of an attribute value.  */
7056
7057 static void
7058 output_value_format (dw_attr_ref a)
7059 {
7060   enum dwarf_form form = value_format (a);
7061
7062   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7063 }
7064
7065 /* Output the .debug_abbrev section which defines the DIE abbreviation
7066    table.  */
7067
7068 static void
7069 output_abbrev_section (void)
7070 {
7071   unsigned long abbrev_id;
7072
7073   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7074     {
7075       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7076       unsigned ix;
7077       dw_attr_ref a_attr;
7078
7079       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7080       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7081                                    dwarf_tag_name (abbrev->die_tag));
7082
7083       if (abbrev->die_child != NULL)
7084         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7085       else
7086         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7087
7088       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7089            ix++)
7090         {
7091           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7092                                        dwarf_attr_name (a_attr->dw_attr));
7093           output_value_format (a_attr);
7094         }
7095
7096       dw2_asm_output_data (1, 0, NULL);
7097       dw2_asm_output_data (1, 0, NULL);
7098     }
7099
7100   /* Terminate the table.  */
7101   dw2_asm_output_data (1, 0, NULL);
7102 }
7103
7104 /* Output a symbol we can use to refer to this DIE from another CU.  */
7105
7106 static inline void
7107 output_die_symbol (dw_die_ref die)
7108 {
7109   char *sym = die->die_symbol;
7110
7111   if (sym == 0)
7112     return;
7113
7114   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7115     /* We make these global, not weak; if the target doesn't support
7116        .linkonce, it doesn't support combining the sections, so debugging
7117        will break.  */
7118     targetm.asm_out.globalize_label (asm_out_file, sym);
7119
7120   ASM_OUTPUT_LABEL (asm_out_file, sym);
7121 }
7122
7123 /* Return a new location list, given the begin and end range, and the
7124    expression. gensym tells us whether to generate a new internal symbol for
7125    this location list node, which is done for the head of the list only.  */
7126
7127 static inline dw_loc_list_ref
7128 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7129               const char *section, unsigned int gensym)
7130 {
7131   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7132
7133   retlist->begin = begin;
7134   retlist->end = end;
7135   retlist->expr = expr;
7136   retlist->section = section;
7137   if (gensym)
7138     retlist->ll_symbol = gen_internal_sym ("LLST");
7139
7140   return retlist;
7141 }
7142
7143 /* Add a location description expression to a location list.  */
7144
7145 static inline void
7146 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7147                            const char *begin, const char *end,
7148                            const char *section)
7149 {
7150   dw_loc_list_ref *d;
7151
7152   /* Find the end of the chain.  */
7153   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7154     ;
7155
7156   /* Add a new location list node to the list.  */
7157   *d = new_loc_list (descr, begin, end, section, 0);
7158 }
7159
7160 /* Output the location list given to us.  */
7161
7162 static void
7163 output_loc_list (dw_loc_list_ref list_head)
7164 {
7165   dw_loc_list_ref curr = list_head;
7166
7167   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7168
7169   /* Walk the location list, and output each range + expression.  */
7170   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7171     {
7172       unsigned long size;
7173       /* Don't output an entry that starts and ends at the same address.  */
7174       if (strcmp (curr->begin, curr->end) == 0)
7175         continue;
7176       if (!have_multiple_function_sections)
7177         {
7178           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7179                                 "Location list begin address (%s)",
7180                                 list_head->ll_symbol);
7181           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7182                                 "Location list end address (%s)",
7183                                 list_head->ll_symbol);
7184         }
7185       else
7186         {
7187           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7188                                "Location list begin address (%s)",
7189                                list_head->ll_symbol);
7190           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7191                                "Location list end address (%s)",
7192                                list_head->ll_symbol);
7193         }
7194       size = size_of_locs (curr->expr);
7195
7196       /* Output the block length for this list of location operations.  */
7197       gcc_assert (size <= 0xffff);
7198       dw2_asm_output_data (2, size, "%s", "Location expression size");
7199
7200       output_loc_sequence (curr->expr);
7201     }
7202
7203   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7204                        "Location list terminator begin (%s)",
7205                        list_head->ll_symbol);
7206   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7207                        "Location list terminator end (%s)",
7208                        list_head->ll_symbol);
7209 }
7210
7211 /* Output the DIE and its attributes.  Called recursively to generate
7212    the definitions of each child DIE.  */
7213
7214 static void
7215 output_die (dw_die_ref die)
7216 {
7217   dw_attr_ref a;
7218   dw_die_ref c;
7219   unsigned long size;
7220   unsigned ix;
7221
7222   /* If someone in another CU might refer to us, set up a symbol for
7223      them to point to.  */
7224   if (die->die_symbol)
7225     output_die_symbol (die);
7226
7227   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7228                                (unsigned long)die->die_offset,
7229                                dwarf_tag_name (die->die_tag));
7230
7231   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7232     {
7233       const char *name = dwarf_attr_name (a->dw_attr);
7234
7235       switch (AT_class (a))
7236         {
7237         case dw_val_class_addr:
7238           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7239           break;
7240
7241         case dw_val_class_offset:
7242           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7243                                "%s", name);
7244           break;
7245
7246         case dw_val_class_range_list:
7247           {
7248             char *p = strchr (ranges_section_label, '\0');
7249
7250             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7251                      a->dw_attr_val.v.val_offset);
7252             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7253                                    debug_ranges_section, "%s", name);
7254             *p = '\0';
7255           }
7256           break;
7257
7258         case dw_val_class_loc:
7259           size = size_of_locs (AT_loc (a));
7260
7261           /* Output the block length for this list of location operations.  */
7262           dw2_asm_output_data (constant_size (size), size, "%s", name);
7263
7264           output_loc_sequence (AT_loc (a));
7265           break;
7266
7267         case dw_val_class_const:
7268           /* ??? It would be slightly more efficient to use a scheme like is
7269              used for unsigned constants below, but gdb 4.x does not sign
7270              extend.  Gdb 5.x does sign extend.  */
7271           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7272           break;
7273
7274         case dw_val_class_unsigned_const:
7275           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7276                                AT_unsigned (a), "%s", name);
7277           break;
7278
7279         case dw_val_class_long_long:
7280           {
7281             unsigned HOST_WIDE_INT first, second;
7282
7283             dw2_asm_output_data (1,
7284                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7285                                  "%s", name);
7286
7287             if (WORDS_BIG_ENDIAN)
7288               {
7289                 first = a->dw_attr_val.v.val_long_long.hi;
7290                 second = a->dw_attr_val.v.val_long_long.low;
7291               }
7292             else
7293               {
7294                 first = a->dw_attr_val.v.val_long_long.low;
7295                 second = a->dw_attr_val.v.val_long_long.hi;
7296               }
7297
7298             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7299                                  first, "long long constant");
7300             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7301                                  second, NULL);
7302           }
7303           break;
7304
7305         case dw_val_class_vec:
7306           {
7307             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7308             unsigned int len = a->dw_attr_val.v.val_vec.length;
7309             unsigned int i;
7310             unsigned char *p;
7311
7312             dw2_asm_output_data (1, len * elt_size, "%s", name);
7313             if (elt_size > sizeof (HOST_WIDE_INT))
7314               {
7315                 elt_size /= 2;
7316                 len *= 2;
7317               }
7318             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7319                  i < len;
7320                  i++, p += elt_size)
7321               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7322                                    "fp or vector constant word %u", i);
7323             break;
7324           }
7325
7326         case dw_val_class_flag:
7327           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7328           break;
7329
7330         case dw_val_class_loc_list:
7331           {
7332             char *sym = AT_loc_list (a)->ll_symbol;
7333
7334             gcc_assert (sym);
7335             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7336                                    "%s", name);
7337           }
7338           break;
7339
7340         case dw_val_class_die_ref:
7341           if (AT_ref_external (a))
7342             {
7343               char *sym = AT_ref (a)->die_symbol;
7344
7345               gcc_assert (sym);
7346               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7347                                      "%s", name);
7348             }
7349           else
7350             {
7351               gcc_assert (AT_ref (a)->die_offset);
7352               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7353                                    "%s", name);
7354             }
7355           break;
7356
7357         case dw_val_class_fde_ref:
7358           {
7359             char l1[20];
7360
7361             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7362                                          a->dw_attr_val.v.val_fde_index * 2);
7363             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7364                                    "%s", name);
7365           }
7366           break;
7367
7368         case dw_val_class_lbl_id:
7369           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7370           break;
7371
7372         case dw_val_class_lineptr:
7373           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7374                                  debug_line_section, "%s", name);
7375           break;
7376
7377         case dw_val_class_macptr:
7378           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7379                                  debug_macinfo_section, "%s", name);
7380           break;
7381
7382         case dw_val_class_str:
7383           if (AT_string_form (a) == DW_FORM_strp)
7384             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7385                                    a->dw_attr_val.v.val_str->label,
7386                                    debug_str_section,
7387                                    "%s: \"%s\"", name, AT_string (a));
7388           else
7389             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7390           break;
7391
7392         case dw_val_class_file:
7393           {
7394             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7395
7396             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7397                                  a->dw_attr_val.v.val_file->filename);
7398             break;
7399           }
7400
7401         default:
7402           gcc_unreachable ();
7403         }
7404     }
7405
7406   FOR_EACH_CHILD (die, c, output_die (c));
7407
7408   /* Add null byte to terminate sibling list.  */
7409   if (die->die_child != NULL)
7410     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7411                          (unsigned long) die->die_offset);
7412 }
7413
7414 /* Output the compilation unit that appears at the beginning of the
7415    .debug_info section, and precedes the DIE descriptions.  */
7416
7417 static void
7418 output_compilation_unit_header (void)
7419 {
7420   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7421     dw2_asm_output_data (4, 0xffffffff,
7422       "Initial length escape value indicating 64-bit DWARF extension");
7423   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7424                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7425                        "Length of Compilation Unit Info");
7426   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7427   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7428                          debug_abbrev_section,
7429                          "Offset Into Abbrev. Section");
7430   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7431 }
7432
7433 /* Output the compilation unit DIE and its children.  */
7434
7435 static void
7436 output_comp_unit (dw_die_ref die, int output_if_empty)
7437 {
7438   const char *secname;
7439   char *oldsym, *tmp;
7440
7441   /* Unless we are outputting main CU, we may throw away empty ones.  */
7442   if (!output_if_empty && die->die_child == NULL)
7443     return;
7444
7445   /* Even if there are no children of this DIE, we must output the information
7446      about the compilation unit.  Otherwise, on an empty translation unit, we
7447      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7448      will then complain when examining the file.  First mark all the DIEs in
7449      this CU so we know which get local refs.  */
7450   mark_dies (die);
7451
7452   build_abbrev_table (die);
7453
7454   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7455   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7456   calc_die_sizes (die);
7457
7458   oldsym = die->die_symbol;
7459   if (oldsym)
7460     {
7461       tmp = alloca (strlen (oldsym) + 24);
7462
7463       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7464       secname = tmp;
7465       die->die_symbol = NULL;
7466       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7467     }
7468   else
7469     switch_to_section (debug_info_section);
7470
7471   /* Output debugging information.  */
7472   output_compilation_unit_header ();
7473   output_die (die);
7474
7475   /* Leave the marks on the main CU, so we can check them in
7476      output_pubnames.  */
7477   if (oldsym)
7478     {
7479       unmark_dies (die);
7480       die->die_symbol = oldsym;
7481     }
7482 }
7483
7484 /* Return the DWARF2/3 pubname associated with a decl.  */
7485
7486 static const char *
7487 dwarf2_name (tree decl, int scope)
7488 {
7489   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7490 }
7491
7492 /* Add a new entry to .debug_pubnames if appropriate.  */
7493
7494 static void
7495 add_pubname_string (const char *str, dw_die_ref die)
7496 {
7497   pubname_entry e;
7498
7499   e.die = die;
7500   e.name = xstrdup (str);
7501   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7502 }
7503
7504 static void
7505 add_pubname (tree decl, dw_die_ref die)
7506 {
7507
7508   if (TREE_PUBLIC (decl))
7509     add_pubname_string (dwarf2_name (decl, 1), die);
7510 }
7511
7512 /* Add a new entry to .debug_pubtypes if appropriate.  */
7513
7514 static void
7515 add_pubtype (tree decl, dw_die_ref die)
7516 {
7517   pubname_entry e;
7518
7519   e.name = NULL;
7520   if ((TREE_PUBLIC (decl)
7521        || die->die_parent == comp_unit_die)
7522       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7523     {
7524       e.die = die;
7525       if (TYPE_P (decl))
7526         {
7527           if (TYPE_NAME (decl))
7528             {
7529               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7530                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7531               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7532                        && DECL_NAME (TYPE_NAME (decl)))
7533                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7534               else
7535                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7536             }
7537         }
7538       else
7539         e.name = xstrdup (dwarf2_name (decl, 1));
7540
7541       /* If we don't have a name for the type, there's no point in adding
7542          it to the table.  */
7543       if (e.name && e.name[0] != '\0')
7544         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7545     }
7546 }
7547
7548 /* Output the public names table used to speed up access to externally
7549    visible names; or the public types table used to find type definitions.  */
7550
7551 static void
7552 output_pubnames (VEC (pubname_entry, gc) * names)
7553 {
7554   unsigned i;
7555   unsigned long pubnames_length = size_of_pubnames (names);
7556   pubname_ref pub;
7557
7558   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7559     dw2_asm_output_data (4, 0xffffffff,
7560       "Initial length escape value indicating 64-bit DWARF extension");
7561   if (names == pubname_table)
7562     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7563                          "Length of Public Names Info");
7564   else
7565     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7566                          "Length of Public Type Names Info");
7567   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7568   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7569                          debug_info_section,
7570                          "Offset of Compilation Unit Info");
7571   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7572                        "Compilation Unit Length");
7573
7574   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7575     {
7576       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7577       if (names == pubname_table)
7578         gcc_assert (pub->die->die_mark);
7579
7580       if (names != pubtype_table
7581           || pub->die->die_offset != 0
7582           || !flag_eliminate_unused_debug_types)
7583         {
7584           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7585                                "DIE offset");
7586
7587           dw2_asm_output_nstring (pub->name, -1, "external name");
7588         }
7589     }
7590
7591   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7592 }
7593
7594 /* Add a new entry to .debug_aranges if appropriate.  */
7595
7596 static void
7597 add_arange (tree decl, dw_die_ref die)
7598 {
7599   if (! DECL_SECTION_NAME (decl))
7600     return;
7601
7602   if (arange_table_in_use == arange_table_allocated)
7603     {
7604       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7605       arange_table = ggc_realloc (arange_table,
7606                                   (arange_table_allocated
7607                                    * sizeof (dw_die_ref)));
7608       memset (arange_table + arange_table_in_use, 0,
7609               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7610     }
7611
7612   arange_table[arange_table_in_use++] = die;
7613 }
7614
7615 /* Output the information that goes into the .debug_aranges table.
7616    Namely, define the beginning and ending address range of the
7617    text section generated for this compilation unit.  */
7618
7619 static void
7620 output_aranges (void)
7621 {
7622   unsigned i;
7623   unsigned long aranges_length = size_of_aranges ();
7624
7625   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7626     dw2_asm_output_data (4, 0xffffffff,
7627       "Initial length escape value indicating 64-bit DWARF extension");
7628   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7629                        "Length of Address Ranges Info");
7630   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7631   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7632                          debug_info_section,
7633                          "Offset of Compilation Unit Info");
7634   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7635   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7636
7637   /* We need to align to twice the pointer size here.  */
7638   if (DWARF_ARANGES_PAD_SIZE)
7639     {
7640       /* Pad using a 2 byte words so that padding is correct for any
7641          pointer size.  */
7642       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7643                            2 * DWARF2_ADDR_SIZE);
7644       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7645         dw2_asm_output_data (2, 0, NULL);
7646     }
7647
7648   /* It is necessary not to output these entries if the sections were
7649      not used; if the sections were not used, the length will be 0 and
7650      the address may end up as 0 if the section is discarded by ld
7651      --gc-sections, leaving an invalid (0, 0) entry that can be
7652      confused with the terminator.  */
7653   if (text_section_used)
7654     {
7655       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7656       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7657                             text_section_label, "Length");
7658     }
7659   if (cold_text_section_used)
7660     {
7661       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7662                            "Address");
7663       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7664                             cold_text_section_label, "Length");
7665     }
7666
7667   for (i = 0; i < arange_table_in_use; i++)
7668     {
7669       dw_die_ref die = arange_table[i];
7670
7671       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7672       gcc_assert (die->die_mark);
7673
7674       if (die->die_tag == DW_TAG_subprogram)
7675         {
7676           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7677                                "Address");
7678           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7679                                 get_AT_low_pc (die), "Length");
7680         }
7681       else
7682         {
7683           /* A static variable; extract the symbol from DW_AT_location.
7684              Note that this code isn't currently hit, as we only emit
7685              aranges for functions (jason 9/23/99).  */
7686           dw_attr_ref a = get_AT (die, DW_AT_location);
7687           dw_loc_descr_ref loc;
7688
7689           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7690
7691           loc = AT_loc (a);
7692           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7693
7694           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7695                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7696           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7697                                get_AT_unsigned (die, DW_AT_byte_size),
7698                                "Length");
7699         }
7700     }
7701
7702   /* Output the terminator words.  */
7703   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7704   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7705 }
7706
7707 /* Add a new entry to .debug_ranges.  Return the offset at which it
7708    was placed.  */
7709
7710 static unsigned int
7711 add_ranges_num (int num)
7712 {
7713   unsigned int in_use = ranges_table_in_use;
7714
7715   if (in_use == ranges_table_allocated)
7716     {
7717       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7718       ranges_table
7719         = ggc_realloc (ranges_table, (ranges_table_allocated
7720                                       * sizeof (struct dw_ranges_struct)));
7721       memset (ranges_table + ranges_table_in_use, 0,
7722               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7723     }
7724
7725   ranges_table[in_use].num = num;
7726   ranges_table_in_use = in_use + 1;
7727
7728   return in_use * 2 * DWARF2_ADDR_SIZE;
7729 }
7730
7731 /* Add a new entry to .debug_ranges corresponding to a block, or a
7732    range terminator if BLOCK is NULL.  */
7733
7734 static unsigned int
7735 add_ranges (const_tree block)
7736 {
7737   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7738 }
7739
7740 /* Add a new entry to .debug_ranges corresponding to a pair of
7741    labels.  */
7742
7743 static unsigned int
7744 add_ranges_by_labels (const char *begin, const char *end)
7745 {
7746   unsigned int in_use = ranges_by_label_in_use;
7747
7748   if (in_use == ranges_by_label_allocated)
7749     {
7750       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7751       ranges_by_label
7752         = ggc_realloc (ranges_by_label,
7753                        (ranges_by_label_allocated
7754                         * sizeof (struct dw_ranges_by_label_struct)));
7755       memset (ranges_by_label + ranges_by_label_in_use, 0,
7756               RANGES_TABLE_INCREMENT
7757               * sizeof (struct dw_ranges_by_label_struct));
7758     }
7759
7760   ranges_by_label[in_use].begin = begin;
7761   ranges_by_label[in_use].end = end;
7762   ranges_by_label_in_use = in_use + 1;
7763
7764   return add_ranges_num (-(int)in_use - 1);
7765 }
7766
7767 static void
7768 output_ranges (void)
7769 {
7770   unsigned i;
7771   static const char *const start_fmt = "Offset 0x%x";
7772   const char *fmt = start_fmt;
7773
7774   for (i = 0; i < ranges_table_in_use; i++)
7775     {
7776       int block_num = ranges_table[i].num;
7777
7778       if (block_num > 0)
7779         {
7780           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7781           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7782
7783           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7784           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7785
7786           /* If all code is in the text section, then the compilation
7787              unit base address defaults to DW_AT_low_pc, which is the
7788              base of the text section.  */
7789           if (!have_multiple_function_sections)
7790             {
7791               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7792                                     text_section_label,
7793                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7794               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7795                                     text_section_label, NULL);
7796             }
7797
7798           /* Otherwise, the compilation unit base address is zero,
7799              which allows us to use absolute addresses, and not worry
7800              about whether the target supports cross-section
7801              arithmetic.  */
7802           else
7803             {
7804               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7805                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7806               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7807             }
7808
7809           fmt = NULL;
7810         }
7811
7812       /* Negative block_num stands for an index into ranges_by_label.  */
7813       else if (block_num < 0)
7814         {
7815           int lab_idx = - block_num - 1;
7816
7817           if (!have_multiple_function_sections)
7818             {
7819               gcc_unreachable ();
7820 #if 0
7821               /* If we ever use add_ranges_by_labels () for a single
7822                  function section, all we have to do is to take out
7823                  the #if 0 above.  */
7824               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7825                                     ranges_by_label[lab_idx].begin,
7826                                     text_section_label,
7827                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7828               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7829                                     ranges_by_label[lab_idx].end,
7830                                     text_section_label, NULL);
7831 #endif
7832             }
7833           else
7834             {
7835               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7836                                    ranges_by_label[lab_idx].begin,
7837                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7838               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7839                                    ranges_by_label[lab_idx].end,
7840                                    NULL);
7841             }
7842         }
7843       else
7844         {
7845           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7846           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7847           fmt = start_fmt;
7848         }
7849     }
7850 }
7851
7852 /* Data structure containing information about input files.  */
7853 struct file_info
7854 {
7855   const char *path;     /* Complete file name.  */
7856   const char *fname;    /* File name part.  */
7857   int length;           /* Length of entire string.  */
7858   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7859   int dir_idx;          /* Index in directory table.  */
7860 };
7861
7862 /* Data structure containing information about directories with source
7863    files.  */
7864 struct dir_info
7865 {
7866   const char *path;     /* Path including directory name.  */
7867   int length;           /* Path length.  */
7868   int prefix;           /* Index of directory entry which is a prefix.  */
7869   int count;            /* Number of files in this directory.  */
7870   int dir_idx;          /* Index of directory used as base.  */
7871 };
7872
7873 /* Callback function for file_info comparison.  We sort by looking at
7874    the directories in the path.  */
7875
7876 static int
7877 file_info_cmp (const void *p1, const void *p2)
7878 {
7879   const struct file_info *s1 = p1;
7880   const struct file_info *s2 = p2;
7881   const unsigned char *cp1;
7882   const unsigned char *cp2;
7883
7884   /* Take care of file names without directories.  We need to make sure that
7885      we return consistent values to qsort since some will get confused if
7886      we return the same value when identical operands are passed in opposite
7887      orders.  So if neither has a directory, return 0 and otherwise return
7888      1 or -1 depending on which one has the directory.  */
7889   if ((s1->path == s1->fname || s2->path == s2->fname))
7890     return (s2->path == s2->fname) - (s1->path == s1->fname);
7891
7892   cp1 = (const unsigned char *) s1->path;
7893   cp2 = (const unsigned char *) s2->path;
7894
7895   while (1)
7896     {
7897       ++cp1;
7898       ++cp2;
7899       /* Reached the end of the first path?  If so, handle like above.  */
7900       if ((cp1 == (const unsigned char *) s1->fname)
7901           || (cp2 == (const unsigned char *) s2->fname))
7902         return ((cp2 == (const unsigned char *) s2->fname)
7903                 - (cp1 == (const unsigned char *) s1->fname));
7904
7905       /* Character of current path component the same?  */
7906       else if (*cp1 != *cp2)
7907         return *cp1 - *cp2;
7908     }
7909 }
7910
7911 struct file_name_acquire_data
7912 {
7913   struct file_info *files;
7914   int used_files;
7915   int max_files;
7916 };
7917
7918 /* Traversal function for the hash table.  */
7919
7920 static int
7921 file_name_acquire (void ** slot, void *data)
7922 {
7923   struct file_name_acquire_data *fnad = data;
7924   struct dwarf_file_data *d = *slot;
7925   struct file_info *fi;
7926   const char *f;
7927
7928   gcc_assert (fnad->max_files >= d->emitted_number);
7929
7930   if (! d->emitted_number)
7931     return 1;
7932
7933   gcc_assert (fnad->max_files != fnad->used_files);
7934
7935   fi = fnad->files + fnad->used_files++;
7936
7937   /* Skip all leading "./".  */
7938   f = d->filename;
7939   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7940     f += 2;
7941
7942   /* Create a new array entry.  */
7943   fi->path = f;
7944   fi->length = strlen (f);
7945   fi->file_idx = d;
7946
7947   /* Search for the file name part.  */
7948   f = strrchr (f, DIR_SEPARATOR);
7949 #if defined (DIR_SEPARATOR_2)
7950   {
7951     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7952
7953     if (g != NULL)
7954       {
7955         if (f == NULL || f < g)
7956           f = g;
7957       }
7958   }
7959 #endif
7960
7961   fi->fname = f == NULL ? fi->path : f + 1;
7962   return 1;
7963 }
7964
7965 /* Output the directory table and the file name table.  We try to minimize
7966    the total amount of memory needed.  A heuristic is used to avoid large
7967    slowdowns with many input files.  */
7968
7969 static void
7970 output_file_names (void)
7971 {
7972   struct file_name_acquire_data fnad;
7973   int numfiles;
7974   struct file_info *files;
7975   struct dir_info *dirs;
7976   int *saved;
7977   int *savehere;
7978   int *backmap;
7979   int ndirs;
7980   int idx_offset;
7981   int i;
7982   int idx;
7983
7984   if (!last_emitted_file)
7985     {
7986       dw2_asm_output_data (1, 0, "End directory table");
7987       dw2_asm_output_data (1, 0, "End file name table");
7988       return;
7989     }
7990
7991   numfiles = last_emitted_file->emitted_number;
7992
7993   /* Allocate the various arrays we need.  */
7994   files = alloca (numfiles * sizeof (struct file_info));
7995   dirs = alloca (numfiles * sizeof (struct dir_info));
7996
7997   fnad.files = files;
7998   fnad.used_files = 0;
7999   fnad.max_files = numfiles;
8000   htab_traverse (file_table, file_name_acquire, &fnad);
8001   gcc_assert (fnad.used_files == fnad.max_files);
8002
8003   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8004
8005   /* Find all the different directories used.  */
8006   dirs[0].path = files[0].path;
8007   dirs[0].length = files[0].fname - files[0].path;
8008   dirs[0].prefix = -1;
8009   dirs[0].count = 1;
8010   dirs[0].dir_idx = 0;
8011   files[0].dir_idx = 0;
8012   ndirs = 1;
8013
8014   for (i = 1; i < numfiles; i++)
8015     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8016         && memcmp (dirs[ndirs - 1].path, files[i].path,
8017                    dirs[ndirs - 1].length) == 0)
8018       {
8019         /* Same directory as last entry.  */
8020         files[i].dir_idx = ndirs - 1;
8021         ++dirs[ndirs - 1].count;
8022       }
8023     else
8024       {
8025         int j;
8026
8027         /* This is a new directory.  */
8028         dirs[ndirs].path = files[i].path;
8029         dirs[ndirs].length = files[i].fname - files[i].path;
8030         dirs[ndirs].count = 1;
8031         dirs[ndirs].dir_idx = ndirs;
8032         files[i].dir_idx = ndirs;
8033
8034         /* Search for a prefix.  */
8035         dirs[ndirs].prefix = -1;
8036         for (j = 0; j < ndirs; j++)
8037           if (dirs[j].length < dirs[ndirs].length
8038               && dirs[j].length > 1
8039               && (dirs[ndirs].prefix == -1
8040                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8041               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8042             dirs[ndirs].prefix = j;
8043
8044         ++ndirs;
8045       }
8046
8047   /* Now to the actual work.  We have to find a subset of the directories which
8048      allow expressing the file name using references to the directory table
8049      with the least amount of characters.  We do not do an exhaustive search
8050      where we would have to check out every combination of every single
8051      possible prefix.  Instead we use a heuristic which provides nearly optimal
8052      results in most cases and never is much off.  */
8053   saved = alloca (ndirs * sizeof (int));
8054   savehere = alloca (ndirs * sizeof (int));
8055
8056   memset (saved, '\0', ndirs * sizeof (saved[0]));
8057   for (i = 0; i < ndirs; i++)
8058     {
8059       int j;
8060       int total;
8061
8062       /* We can always save some space for the current directory.  But this
8063          does not mean it will be enough to justify adding the directory.  */
8064       savehere[i] = dirs[i].length;
8065       total = (savehere[i] - saved[i]) * dirs[i].count;
8066
8067       for (j = i + 1; j < ndirs; j++)
8068         {
8069           savehere[j] = 0;
8070           if (saved[j] < dirs[i].length)
8071             {
8072               /* Determine whether the dirs[i] path is a prefix of the
8073                  dirs[j] path.  */
8074               int k;
8075
8076               k = dirs[j].prefix;
8077               while (k != -1 && k != (int) i)
8078                 k = dirs[k].prefix;
8079
8080               if (k == (int) i)
8081                 {
8082                   /* Yes it is.  We can possibly save some memory by
8083                      writing the filenames in dirs[j] relative to
8084                      dirs[i].  */
8085                   savehere[j] = dirs[i].length;
8086                   total += (savehere[j] - saved[j]) * dirs[j].count;
8087                 }
8088             }
8089         }
8090
8091       /* Check whether we can save enough to justify adding the dirs[i]
8092          directory.  */
8093       if (total > dirs[i].length + 1)
8094         {
8095           /* It's worthwhile adding.  */
8096           for (j = i; j < ndirs; j++)
8097             if (savehere[j] > 0)
8098               {
8099                 /* Remember how much we saved for this directory so far.  */
8100                 saved[j] = savehere[j];
8101
8102                 /* Remember the prefix directory.  */
8103                 dirs[j].dir_idx = i;
8104               }
8105         }
8106     }
8107
8108   /* Emit the directory name table.  */
8109   idx = 1;
8110   idx_offset = dirs[0].length > 0 ? 1 : 0;
8111   for (i = 1 - idx_offset; i < ndirs; i++)
8112     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8113                             "Directory Entry: 0x%x", i + idx_offset);
8114
8115   dw2_asm_output_data (1, 0, "End directory table");
8116
8117   /* We have to emit them in the order of emitted_number since that's
8118      used in the debug info generation.  To do this efficiently we
8119      generate a back-mapping of the indices first.  */
8120   backmap = alloca (numfiles * sizeof (int));
8121   for (i = 0; i < numfiles; i++)
8122     backmap[files[i].file_idx->emitted_number - 1] = i;
8123
8124   /* Now write all the file names.  */
8125   for (i = 0; i < numfiles; i++)
8126     {
8127       int file_idx = backmap[i];
8128       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8129
8130       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8131                               "File Entry: 0x%x", (unsigned) i + 1);
8132
8133       /* Include directory index.  */
8134       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8135
8136       /* Modification time.  */
8137       dw2_asm_output_data_uleb128 (0, NULL);
8138
8139       /* File length in bytes.  */
8140       dw2_asm_output_data_uleb128 (0, NULL);
8141     }
8142
8143   dw2_asm_output_data (1, 0, "End file name table");
8144 }
8145
8146
8147 /* Output the source line number correspondence information.  This
8148    information goes into the .debug_line section.  */
8149
8150 static void
8151 output_line_info (void)
8152 {
8153   char l1[20], l2[20], p1[20], p2[20];
8154   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8155   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8156   unsigned opc;
8157   unsigned n_op_args;
8158   unsigned long lt_index;
8159   unsigned long current_line;
8160   long line_offset;
8161   long line_delta;
8162   unsigned long current_file;
8163   unsigned long function;
8164
8165   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8166   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8167   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8168   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8169
8170   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8171     dw2_asm_output_data (4, 0xffffffff,
8172       "Initial length escape value indicating 64-bit DWARF extension");
8173   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8174                         "Length of Source Line Info");
8175   ASM_OUTPUT_LABEL (asm_out_file, l1);
8176
8177   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8178   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8179   ASM_OUTPUT_LABEL (asm_out_file, p1);
8180
8181   /* Define the architecture-dependent minimum instruction length (in
8182    bytes).  In this implementation of DWARF, this field is used for
8183    information purposes only.  Since GCC generates assembly language,
8184    we have no a priori knowledge of how many instruction bytes are
8185    generated for each source line, and therefore can use only the
8186    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8187    commands.  Accordingly, we fix this as `1', which is "correct
8188    enough" for all architectures, and don't let the target override.  */
8189   dw2_asm_output_data (1, 1,
8190                        "Minimum Instruction Length");
8191
8192   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8193                        "Default is_stmt_start flag");
8194   dw2_asm_output_data (1, DWARF_LINE_BASE,
8195                        "Line Base Value (Special Opcodes)");
8196   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8197                        "Line Range Value (Special Opcodes)");
8198   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8199                        "Special Opcode Base");
8200
8201   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8202     {
8203       switch (opc)
8204         {
8205         case DW_LNS_advance_pc:
8206         case DW_LNS_advance_line:
8207         case DW_LNS_set_file:
8208         case DW_LNS_set_column:
8209         case DW_LNS_fixed_advance_pc:
8210           n_op_args = 1;
8211           break;
8212         default:
8213           n_op_args = 0;
8214           break;
8215         }
8216
8217       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8218                            opc, n_op_args);
8219     }
8220
8221   /* Write out the information about the files we use.  */
8222   output_file_names ();
8223   ASM_OUTPUT_LABEL (asm_out_file, p2);
8224
8225   /* We used to set the address register to the first location in the text
8226      section here, but that didn't accomplish anything since we already
8227      have a line note for the opening brace of the first function.  */
8228
8229   /* Generate the line number to PC correspondence table, encoded as
8230      a series of state machine operations.  */
8231   current_file = 1;
8232   current_line = 1;
8233
8234   if (cfun && in_cold_section_p)
8235     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8236   else
8237     strcpy (prev_line_label, text_section_label);
8238   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8239     {
8240       dw_line_info_ref line_info = &line_info_table[lt_index];
8241
8242 #if 0
8243       /* Disable this optimization for now; GDB wants to see two line notes
8244          at the beginning of a function so it can find the end of the
8245          prologue.  */
8246
8247       /* Don't emit anything for redundant notes.  Just updating the
8248          address doesn't accomplish anything, because we already assume
8249          that anything after the last address is this line.  */
8250       if (line_info->dw_line_num == current_line
8251           && line_info->dw_file_num == current_file)
8252         continue;
8253 #endif
8254
8255       /* Emit debug info for the address of the current line.
8256
8257          Unfortunately, we have little choice here currently, and must always
8258          use the most general form.  GCC does not know the address delta
8259          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8260          attributes which will give an upper bound on the address range.  We
8261          could perhaps use length attributes to determine when it is safe to
8262          use DW_LNS_fixed_advance_pc.  */
8263
8264       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8265       if (0)
8266         {
8267           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8268           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8269                                "DW_LNS_fixed_advance_pc");
8270           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8271         }
8272       else
8273         {
8274           /* This can handle any delta.  This takes
8275              4+DWARF2_ADDR_SIZE bytes.  */
8276           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8277           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8278           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8279           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8280         }
8281
8282       strcpy (prev_line_label, line_label);
8283
8284       /* Emit debug info for the source file of the current line, if
8285          different from the previous line.  */
8286       if (line_info->dw_file_num != current_file)
8287         {
8288           current_file = line_info->dw_file_num;
8289           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8290           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8291         }
8292
8293       /* Emit debug info for the current line number, choosing the encoding
8294          that uses the least amount of space.  */
8295       if (line_info->dw_line_num != current_line)
8296         {
8297           line_offset = line_info->dw_line_num - current_line;
8298           line_delta = line_offset - DWARF_LINE_BASE;
8299           current_line = line_info->dw_line_num;
8300           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8301             /* This can handle deltas from -10 to 234, using the current
8302                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8303                takes 1 byte.  */
8304             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8305                                  "line %lu", current_line);
8306           else
8307             {
8308               /* This can handle any delta.  This takes at least 4 bytes,
8309                  depending on the value being encoded.  */
8310               dw2_asm_output_data (1, DW_LNS_advance_line,
8311                                    "advance to line %lu", current_line);
8312               dw2_asm_output_data_sleb128 (line_offset, NULL);
8313               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8314             }
8315         }
8316       else
8317         /* We still need to start a new row, so output a copy insn.  */
8318         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8319     }
8320
8321   /* Emit debug info for the address of the end of the function.  */
8322   if (0)
8323     {
8324       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8325                            "DW_LNS_fixed_advance_pc");
8326       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8327     }
8328   else
8329     {
8330       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8331       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8332       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8333       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8334     }
8335
8336   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8337   dw2_asm_output_data_uleb128 (1, NULL);
8338   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8339
8340   function = 0;
8341   current_file = 1;
8342   current_line = 1;
8343   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8344     {
8345       dw_separate_line_info_ref line_info
8346         = &separate_line_info_table[lt_index];
8347
8348 #if 0
8349       /* Don't emit anything for redundant notes.  */
8350       if (line_info->dw_line_num == current_line
8351           && line_info->dw_file_num == current_file
8352           && line_info->function == function)
8353         goto cont;
8354 #endif
8355
8356       /* Emit debug info for the address of the current line.  If this is
8357          a new function, or the first line of a function, then we need
8358          to handle it differently.  */
8359       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8360                                    lt_index);
8361       if (function != line_info->function)
8362         {
8363           function = line_info->function;
8364
8365           /* Set the address register to the first line in the function.  */
8366           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8367           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8368           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8369           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8370         }
8371       else
8372         {
8373           /* ??? See the DW_LNS_advance_pc comment above.  */
8374           if (0)
8375             {
8376               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8377                                    "DW_LNS_fixed_advance_pc");
8378               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8379             }
8380           else
8381             {
8382               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8383               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8384               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8385               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8386             }
8387         }
8388
8389       strcpy (prev_line_label, line_label);
8390
8391       /* Emit debug info for the source file of the current line, if
8392          different from the previous line.  */
8393       if (line_info->dw_file_num != current_file)
8394         {
8395           current_file = line_info->dw_file_num;
8396           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8397           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8398         }
8399
8400       /* Emit debug info for the current line number, choosing the encoding
8401          that uses the least amount of space.  */
8402       if (line_info->dw_line_num != current_line)
8403         {
8404           line_offset = line_info->dw_line_num - current_line;
8405           line_delta = line_offset - DWARF_LINE_BASE;
8406           current_line = line_info->dw_line_num;
8407           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8408             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8409                                  "line %lu", current_line);
8410           else
8411             {
8412               dw2_asm_output_data (1, DW_LNS_advance_line,
8413                                    "advance to line %lu", current_line);
8414               dw2_asm_output_data_sleb128 (line_offset, NULL);
8415               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8416             }
8417         }
8418       else
8419         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8420
8421 #if 0
8422     cont:
8423 #endif
8424
8425       lt_index++;
8426
8427       /* If we're done with a function, end its sequence.  */
8428       if (lt_index == separate_line_info_table_in_use
8429           || separate_line_info_table[lt_index].function != function)
8430         {
8431           current_file = 1;
8432           current_line = 1;
8433
8434           /* Emit debug info for the address of the end of the function.  */
8435           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8436           if (0)
8437             {
8438               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8439                                    "DW_LNS_fixed_advance_pc");
8440               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8441             }
8442           else
8443             {
8444               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8445               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8446               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8447               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8448             }
8449
8450           /* Output the marker for the end of this sequence.  */
8451           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8452           dw2_asm_output_data_uleb128 (1, NULL);
8453           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8454         }
8455     }
8456
8457   /* Output the marker for the end of the line number info.  */
8458   ASM_OUTPUT_LABEL (asm_out_file, l2);
8459 }
8460 \f
8461 /* Given a pointer to a tree node for some base type, return a pointer to
8462    a DIE that describes the given type.
8463
8464    This routine must only be called for GCC type nodes that correspond to
8465    Dwarf base (fundamental) types.  */
8466
8467 static dw_die_ref
8468 base_type_die (tree type)
8469 {
8470   dw_die_ref base_type_result;
8471   enum dwarf_type encoding;
8472
8473   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8474     return 0;
8475
8476   switch (TREE_CODE (type))
8477     {
8478     case INTEGER_TYPE:
8479       if (TYPE_STRING_FLAG (type))
8480         {
8481           if (TYPE_UNSIGNED (type))
8482             encoding = DW_ATE_unsigned_char;
8483           else
8484             encoding = DW_ATE_signed_char;
8485         }
8486       else if (TYPE_UNSIGNED (type))
8487         encoding = DW_ATE_unsigned;
8488       else
8489         encoding = DW_ATE_signed;
8490       break;
8491
8492     case REAL_TYPE:
8493       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8494         encoding = DW_ATE_decimal_float;
8495       else
8496         encoding = DW_ATE_float;
8497       break;
8498
8499     case FIXED_POINT_TYPE:
8500       if (TYPE_UNSIGNED (type))
8501         encoding = DW_ATE_unsigned_fixed;
8502       else
8503         encoding = DW_ATE_signed_fixed;
8504       break;
8505
8506       /* Dwarf2 doesn't know anything about complex ints, so use
8507          a user defined type for it.  */
8508     case COMPLEX_TYPE:
8509       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8510         encoding = DW_ATE_complex_float;
8511       else
8512         encoding = DW_ATE_lo_user;
8513       break;
8514
8515     case BOOLEAN_TYPE:
8516       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8517       encoding = DW_ATE_boolean;
8518       break;
8519
8520     default:
8521       /* No other TREE_CODEs are Dwarf fundamental types.  */
8522       gcc_unreachable ();
8523     }
8524
8525   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8526
8527   /* This probably indicates a bug.  */
8528   if (! TYPE_NAME (type))
8529     add_name_attribute (base_type_result, "__unknown__");
8530
8531   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8532                    int_size_in_bytes (type));
8533   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8534
8535   return base_type_result;
8536 }
8537
8538 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8539    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8540
8541 static inline int
8542 is_base_type (tree type)
8543 {
8544   switch (TREE_CODE (type))
8545     {
8546     case ERROR_MARK:
8547     case VOID_TYPE:
8548     case INTEGER_TYPE:
8549     case REAL_TYPE:
8550     case FIXED_POINT_TYPE:
8551     case COMPLEX_TYPE:
8552     case BOOLEAN_TYPE:
8553       return 1;
8554
8555     case ARRAY_TYPE:
8556     case RECORD_TYPE:
8557     case UNION_TYPE:
8558     case QUAL_UNION_TYPE:
8559     case ENUMERAL_TYPE:
8560     case FUNCTION_TYPE:
8561     case METHOD_TYPE:
8562     case POINTER_TYPE:
8563     case REFERENCE_TYPE:
8564     case OFFSET_TYPE:
8565     case LANG_TYPE:
8566     case VECTOR_TYPE:
8567       return 0;
8568
8569     default:
8570       gcc_unreachable ();
8571     }
8572
8573   return 0;
8574 }
8575
8576 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8577    node, return the size in bits for the type if it is a constant, or else
8578    return the alignment for the type if the type's size is not constant, or
8579    else return BITS_PER_WORD if the type actually turns out to be an
8580    ERROR_MARK node.  */
8581
8582 static inline unsigned HOST_WIDE_INT
8583 simple_type_size_in_bits (const_tree type)
8584 {
8585   if (TREE_CODE (type) == ERROR_MARK)
8586     return BITS_PER_WORD;
8587   else if (TYPE_SIZE (type) == NULL_TREE)
8588     return 0;
8589   else if (host_integerp (TYPE_SIZE (type), 1))
8590     return tree_low_cst (TYPE_SIZE (type), 1);
8591   else
8592     return TYPE_ALIGN (type);
8593 }
8594
8595 /* Return true if the debug information for the given type should be
8596    emitted as a subrange type.  */
8597
8598 static inline bool
8599 is_subrange_type (const_tree type)
8600 {
8601   tree subtype = TREE_TYPE (type);
8602
8603   /* Subrange types are identified by the fact that they are integer
8604      types, and that they have a subtype which is either an integer type
8605      or an enumeral type.  */
8606
8607   if (TREE_CODE (type) != INTEGER_TYPE
8608       || subtype == NULL_TREE)
8609     return false;
8610
8611   if (TREE_CODE (subtype) != INTEGER_TYPE
8612       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8613     return false;
8614
8615   if (TREE_CODE (type) == TREE_CODE (subtype)
8616       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8617       && TYPE_MIN_VALUE (type) != NULL
8618       && TYPE_MIN_VALUE (subtype) != NULL
8619       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8620       && TYPE_MAX_VALUE (type) != NULL
8621       && TYPE_MAX_VALUE (subtype) != NULL
8622       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8623     {
8624       /* The type and its subtype have the same representation.  If in
8625          addition the two types also have the same name, then the given
8626          type is not a subrange type, but rather a plain base type.  */
8627       /* FIXME: brobecker/2004-03-22:
8628          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8629          therefore be sufficient to check the TYPE_SIZE node pointers
8630          rather than checking the actual size.  Unfortunately, we have
8631          found some cases, such as in the Ada "integer" type, where
8632          this is not the case.  Until this problem is solved, we need to
8633          keep checking the actual size.  */
8634       tree type_name = TYPE_NAME (type);
8635       tree subtype_name = TYPE_NAME (subtype);
8636
8637       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8638         type_name = DECL_NAME (type_name);
8639
8640       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8641         subtype_name = DECL_NAME (subtype_name);
8642
8643       if (type_name == subtype_name)
8644         return false;
8645     }
8646
8647   return true;
8648 }
8649
8650 /*  Given a pointer to a tree node for a subrange type, return a pointer
8651     to a DIE that describes the given type.  */
8652
8653 static dw_die_ref
8654 subrange_type_die (tree type, dw_die_ref context_die)
8655 {
8656   dw_die_ref subrange_die;
8657   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8658
8659   if (context_die == NULL)
8660     context_die = comp_unit_die;
8661
8662   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8663
8664   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8665     {
8666       /* The size of the subrange type and its base type do not match,
8667          so we need to generate a size attribute for the subrange type.  */
8668       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8669     }
8670
8671   if (TYPE_MIN_VALUE (type) != NULL)
8672     add_bound_info (subrange_die, DW_AT_lower_bound,
8673                     TYPE_MIN_VALUE (type));
8674   if (TYPE_MAX_VALUE (type) != NULL)
8675     add_bound_info (subrange_die, DW_AT_upper_bound,
8676                     TYPE_MAX_VALUE (type));
8677
8678   return subrange_die;
8679 }
8680
8681 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8682    entry that chains various modifiers in front of the given type.  */
8683
8684 static dw_die_ref
8685 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8686                    dw_die_ref context_die)
8687 {
8688   enum tree_code code = TREE_CODE (type);
8689   dw_die_ref mod_type_die;
8690   dw_die_ref sub_die = NULL;
8691   tree item_type = NULL;
8692   tree qualified_type;
8693   tree name;
8694
8695   if (code == ERROR_MARK)
8696     return NULL;
8697
8698   /* See if we already have the appropriately qualified variant of
8699      this type.  */
8700   qualified_type
8701     = get_qualified_type (type,
8702                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8703                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8704
8705   /* If we do, then we can just use its DIE, if it exists.  */
8706   if (qualified_type)
8707     {
8708       mod_type_die = lookup_type_die (qualified_type);
8709       if (mod_type_die)
8710         return mod_type_die;
8711     }
8712
8713   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8714
8715   /* Handle C typedef types.  */
8716   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8717     {
8718       tree dtype = TREE_TYPE (name);
8719
8720       if (qualified_type == dtype)
8721         {
8722           /* For a named type, use the typedef.  */
8723           gen_type_die (qualified_type, context_die);
8724           return lookup_type_die (qualified_type);
8725         }
8726       else if (is_const_type < TYPE_READONLY (dtype)
8727                || is_volatile_type < TYPE_VOLATILE (dtype)
8728                || (is_const_type <= TYPE_READONLY (dtype)
8729                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8730                    && DECL_ORIGINAL_TYPE (name) != type))
8731         /* cv-unqualified version of named type.  Just use the unnamed
8732            type to which it refers.  */
8733         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8734                                   is_const_type, is_volatile_type,
8735                                   context_die);
8736       /* Else cv-qualified version of named type; fall through.  */
8737     }
8738
8739   if (is_const_type)
8740     {
8741       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8742       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8743     }
8744   else if (is_volatile_type)
8745     {
8746       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8747       sub_die = modified_type_die (type, 0, 0, context_die);
8748     }
8749   else if (code == POINTER_TYPE)
8750     {
8751       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8752       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8753                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8754       item_type = TREE_TYPE (type);
8755     }
8756   else if (code == REFERENCE_TYPE)
8757     {
8758       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8759       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8760                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8761       item_type = TREE_TYPE (type);
8762     }
8763   else if (is_subrange_type (type))
8764     {
8765       mod_type_die = subrange_type_die (type, context_die);
8766       item_type = TREE_TYPE (type);
8767     }
8768   else if (is_base_type (type))
8769     mod_type_die = base_type_die (type);
8770   else
8771     {
8772       gen_type_die (type, context_die);
8773
8774       /* We have to get the type_main_variant here (and pass that to the
8775          `lookup_type_die' routine) because the ..._TYPE node we have
8776          might simply be a *copy* of some original type node (where the
8777          copy was created to help us keep track of typedef names) and
8778          that copy might have a different TYPE_UID from the original
8779          ..._TYPE node.  */
8780       if (TREE_CODE (type) != VECTOR_TYPE)
8781         return lookup_type_die (type_main_variant (type));
8782       else
8783         /* Vectors have the debugging information in the type,
8784            not the main variant.  */
8785         return lookup_type_die (type);
8786     }
8787
8788   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8789      don't output a DW_TAG_typedef, since there isn't one in the
8790      user's program; just attach a DW_AT_name to the type.  */
8791   if (name
8792       && (TREE_CODE (name) != TYPE_DECL
8793           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8794     {
8795       if (TREE_CODE (name) == TYPE_DECL)
8796         /* Could just call add_name_and_src_coords_attributes here,
8797            but since this is a builtin type it doesn't have any
8798            useful source coordinates anyway.  */
8799         name = DECL_NAME (name);
8800       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8801     }
8802
8803   if (qualified_type)
8804     equate_type_number_to_die (qualified_type, mod_type_die);
8805
8806   if (item_type)
8807     /* We must do this after the equate_type_number_to_die call, in case
8808        this is a recursive type.  This ensures that the modified_type_die
8809        recursion will terminate even if the type is recursive.  Recursive
8810        types are possible in Ada.  */
8811     sub_die = modified_type_die (item_type,
8812                                  TYPE_READONLY (item_type),
8813                                  TYPE_VOLATILE (item_type),
8814                                  context_die);
8815
8816   if (sub_die != NULL)
8817     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8818
8819   return mod_type_die;
8820 }
8821
8822 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8823    an enumerated type.  */
8824
8825 static inline int
8826 type_is_enum (const_tree type)
8827 {
8828   return TREE_CODE (type) == ENUMERAL_TYPE;
8829 }
8830
8831 /* Return the DBX register number described by a given RTL node.  */
8832
8833 static unsigned int
8834 dbx_reg_number (const_rtx rtl)
8835 {
8836   unsigned regno = REGNO (rtl);
8837
8838   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8839
8840 #ifdef LEAF_REG_REMAP
8841   if (current_function_uses_only_leaf_regs)
8842     {
8843       int leaf_reg = LEAF_REG_REMAP (regno);
8844       if (leaf_reg != -1)
8845         regno = (unsigned) leaf_reg;
8846     }
8847 #endif
8848
8849   return DBX_REGISTER_NUMBER (regno);
8850 }
8851
8852 /* Optionally add a DW_OP_piece term to a location description expression.
8853    DW_OP_piece is only added if the location description expression already
8854    doesn't end with DW_OP_piece.  */
8855
8856 static void
8857 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8858 {
8859   dw_loc_descr_ref loc;
8860
8861   if (*list_head != NULL)
8862     {
8863       /* Find the end of the chain.  */
8864       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8865         ;
8866
8867       if (loc->dw_loc_opc != DW_OP_piece)
8868         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8869     }
8870 }
8871
8872 /* Return a location descriptor that designates a machine register or
8873    zero if there is none.  */
8874
8875 static dw_loc_descr_ref
8876 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8877 {
8878   rtx regs;
8879
8880   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8881     return 0;
8882
8883   regs = targetm.dwarf_register_span (rtl);
8884
8885   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8886     return multiple_reg_loc_descriptor (rtl, regs, initialized);
8887   else
8888     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8889 }
8890
8891 /* Return a location descriptor that designates a machine register for
8892    a given hard register number.  */
8893
8894 static dw_loc_descr_ref
8895 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8896 {
8897   dw_loc_descr_ref reg_loc_descr;
8898   if (regno <= 31)
8899     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8900   else
8901     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8902
8903   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8904     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8905
8906   return reg_loc_descr;
8907 }
8908
8909 /* Given an RTL of a register, return a location descriptor that
8910    designates a value that spans more than one register.  */
8911
8912 static dw_loc_descr_ref
8913 multiple_reg_loc_descriptor (rtx rtl, rtx regs, 
8914                              enum var_init_status initialized)
8915 {
8916   int nregs, size, i;
8917   unsigned reg;
8918   dw_loc_descr_ref loc_result = NULL;
8919
8920   reg = REGNO (rtl);
8921 #ifdef LEAF_REG_REMAP
8922   if (current_function_uses_only_leaf_regs)
8923     {
8924       int leaf_reg = LEAF_REG_REMAP (reg);
8925       if (leaf_reg != -1)
8926         reg = (unsigned) leaf_reg;
8927     }
8928 #endif
8929   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8930   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8931
8932   /* Simple, contiguous registers.  */
8933   if (regs == NULL_RTX)
8934     {
8935       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8936
8937       loc_result = NULL;
8938       while (nregs--)
8939         {
8940           dw_loc_descr_ref t;
8941
8942           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8943                                       VAR_INIT_STATUS_INITIALIZED);
8944           add_loc_descr (&loc_result, t);
8945           add_loc_descr_op_piece (&loc_result, size);
8946           ++reg;
8947         }
8948       return loc_result;
8949     }
8950
8951   /* Now onto stupid register sets in non contiguous locations.  */
8952
8953   gcc_assert (GET_CODE (regs) == PARALLEL);
8954
8955   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8956   loc_result = NULL;
8957
8958   for (i = 0; i < XVECLEN (regs, 0); ++i)
8959     {
8960       dw_loc_descr_ref t;
8961
8962       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8963                                   VAR_INIT_STATUS_INITIALIZED);
8964       add_loc_descr (&loc_result, t);
8965       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8966       add_loc_descr_op_piece (&loc_result, size);
8967     }
8968
8969   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8970     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8971   return loc_result;
8972 }
8973
8974 /* Return a location descriptor that designates a constant.  */
8975
8976 static dw_loc_descr_ref
8977 int_loc_descriptor (HOST_WIDE_INT i)
8978 {
8979   enum dwarf_location_atom op;
8980
8981   /* Pick the smallest representation of a constant, rather than just
8982      defaulting to the LEB encoding.  */
8983   if (i >= 0)
8984     {
8985       if (i <= 31)
8986         op = DW_OP_lit0 + i;
8987       else if (i <= 0xff)
8988         op = DW_OP_const1u;
8989       else if (i <= 0xffff)
8990         op = DW_OP_const2u;
8991       else if (HOST_BITS_PER_WIDE_INT == 32
8992                || i <= 0xffffffff)
8993         op = DW_OP_const4u;
8994       else
8995         op = DW_OP_constu;
8996     }
8997   else
8998     {
8999       if (i >= -0x80)
9000         op = DW_OP_const1s;
9001       else if (i >= -0x8000)
9002         op = DW_OP_const2s;
9003       else if (HOST_BITS_PER_WIDE_INT == 32
9004                || i >= -0x80000000)
9005         op = DW_OP_const4s;
9006       else
9007         op = DW_OP_consts;
9008     }
9009
9010   return new_loc_descr (op, i, 0);
9011 }
9012
9013 /* Return a location descriptor that designates a base+offset location.  */
9014
9015 static dw_loc_descr_ref
9016 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9017                  enum var_init_status initialized)
9018 {
9019   unsigned int regno;
9020   dw_loc_descr_ref result;
9021
9022   /* We only use "frame base" when we're sure we're talking about the
9023      post-prologue local stack frame.  We do this by *not* running
9024      register elimination until this point, and recognizing the special
9025      argument pointer and soft frame pointer rtx's.  */
9026   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9027     {
9028       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9029
9030       if (elim != reg)
9031         {
9032           if (GET_CODE (elim) == PLUS)
9033             {
9034               offset += INTVAL (XEXP (elim, 1));
9035               elim = XEXP (elim, 0);
9036             }
9037           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
9038                       : stack_pointer_rtx));
9039           offset += frame_pointer_fb_offset;
9040
9041           return new_loc_descr (DW_OP_fbreg, offset, 0);
9042         }
9043     }
9044
9045   regno = dbx_reg_number (reg);
9046   if (regno <= 31)
9047     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9048   else
9049     result = new_loc_descr (DW_OP_bregx, regno, offset);
9050
9051   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9052     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9053
9054   return result;
9055 }
9056
9057 /* Return true if this RTL expression describes a base+offset calculation.  */
9058
9059 static inline int
9060 is_based_loc (const_rtx rtl)
9061 {
9062   return (GET_CODE (rtl) == PLUS
9063           && ((REG_P (XEXP (rtl, 0))
9064                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9065                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9066 }
9067
9068 /* Return a descriptor that describes the concatenation of N locations
9069    used to form the address of a memory location.  */
9070
9071 static dw_loc_descr_ref
9072 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9073                             enum var_init_status initialized)
9074 {
9075   unsigned int i;
9076   dw_loc_descr_ref cc_loc_result = NULL;
9077   unsigned int n = XVECLEN (concatn, 0);
9078
9079   for (i = 0; i < n; ++i)
9080     {
9081       dw_loc_descr_ref ref;
9082       rtx x = XVECEXP (concatn, 0, i);
9083
9084       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9085       if (ref == NULL)
9086         return NULL;
9087
9088       add_loc_descr (&cc_loc_result, ref);
9089       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9090     }
9091
9092   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9093     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9094
9095   return cc_loc_result;
9096 }
9097
9098 /* The following routine converts the RTL for a variable or parameter
9099    (resident in memory) into an equivalent Dwarf representation of a
9100    mechanism for getting the address of that same variable onto the top of a
9101    hypothetical "address evaluation" stack.
9102
9103    When creating memory location descriptors, we are effectively transforming
9104    the RTL for a memory-resident object into its Dwarf postfix expression
9105    equivalent.  This routine recursively descends an RTL tree, turning
9106    it into Dwarf postfix code as it goes.
9107
9108    MODE is the mode of the memory reference, needed to handle some
9109    autoincrement addressing modes.
9110
9111    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9112    location list for RTL.
9113
9114    Return 0 if we can't represent the location.  */
9115
9116 static dw_loc_descr_ref
9117 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9118                     enum var_init_status initialized)
9119 {
9120   dw_loc_descr_ref mem_loc_result = NULL;
9121   enum dwarf_location_atom op;
9122
9123   /* Note that for a dynamically sized array, the location we will generate a
9124      description of here will be the lowest numbered location which is
9125      actually within the array.  That's *not* necessarily the same as the
9126      zeroth element of the array.  */
9127
9128   rtl = targetm.delegitimize_address (rtl);
9129
9130   switch (GET_CODE (rtl))
9131     {
9132     case POST_INC:
9133     case POST_DEC:
9134     case POST_MODIFY:
9135       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9136          just fall into the SUBREG code.  */
9137
9138       /* ... fall through ...  */
9139
9140     case SUBREG:
9141       /* The case of a subreg may arise when we have a local (register)
9142          variable or a formal (register) parameter which doesn't quite fill
9143          up an entire register.  For now, just assume that it is
9144          legitimate to make the Dwarf info refer to the whole register which
9145          contains the given subreg.  */
9146       rtl = XEXP (rtl, 0);
9147
9148       /* ... fall through ...  */
9149
9150     case REG:
9151       /* Whenever a register number forms a part of the description of the
9152          method for calculating the (dynamic) address of a memory resident
9153          object, DWARF rules require the register number be referred to as
9154          a "base register".  This distinction is not based in any way upon
9155          what category of register the hardware believes the given register
9156          belongs to.  This is strictly DWARF terminology we're dealing with
9157          here. Note that in cases where the location of a memory-resident
9158          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9159          OP_CONST (0)) the actual DWARF location descriptor that we generate
9160          may just be OP_BASEREG (basereg).  This may look deceptively like
9161          the object in question was allocated to a register (rather than in
9162          memory) so DWARF consumers need to be aware of the subtle
9163          distinction between OP_REG and OP_BASEREG.  */
9164       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9165         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9166       break;
9167
9168     case MEM:
9169       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9170                                            VAR_INIT_STATUS_INITIALIZED);
9171       if (mem_loc_result != 0)
9172         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9173       break;
9174
9175     case LO_SUM:
9176          rtl = XEXP (rtl, 1);
9177
9178       /* ... fall through ...  */
9179
9180     case LABEL_REF:
9181       /* Some ports can transform a symbol ref into a label ref, because
9182          the symbol ref is too far away and has to be dumped into a constant
9183          pool.  */
9184     case CONST:
9185     case SYMBOL_REF:
9186       /* Alternatively, the symbol in the constant pool might be referenced
9187          by a different symbol.  */
9188       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9189         {
9190           bool marked;
9191           rtx tmp = get_pool_constant_mark (rtl, &marked);
9192
9193           if (GET_CODE (tmp) == SYMBOL_REF)
9194             {
9195               rtl = tmp;
9196               if (CONSTANT_POOL_ADDRESS_P (tmp))
9197                 get_pool_constant_mark (tmp, &marked);
9198               else
9199                 marked = true;
9200             }
9201
9202           /* If all references to this pool constant were optimized away,
9203              it was not output and thus we can't represent it.
9204              FIXME: might try to use DW_OP_const_value here, though
9205              DW_OP_piece complicates it.  */
9206           if (!marked)
9207             return 0;
9208         }
9209
9210       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9211       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9212       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9213       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9214       break;
9215
9216     case PRE_MODIFY:
9217       /* Extract the PLUS expression nested inside and fall into
9218          PLUS code below.  */
9219       rtl = XEXP (rtl, 1);
9220       goto plus;
9221
9222     case PRE_INC:
9223     case PRE_DEC:
9224       /* Turn these into a PLUS expression and fall into the PLUS code
9225          below.  */
9226       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9227                           GEN_INT (GET_CODE (rtl) == PRE_INC
9228                                    ? GET_MODE_UNIT_SIZE (mode)
9229                                    : -GET_MODE_UNIT_SIZE (mode)));
9230
9231       /* ... fall through ...  */
9232
9233     case PLUS:
9234     plus:
9235       if (is_based_loc (rtl))
9236         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9237                                           INTVAL (XEXP (rtl, 1)),
9238                                           VAR_INIT_STATUS_INITIALIZED);
9239       else
9240         {
9241           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9242                                                VAR_INIT_STATUS_INITIALIZED);
9243           if (mem_loc_result == 0)
9244             break;
9245
9246           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9247               && INTVAL (XEXP (rtl, 1)) >= 0)
9248             add_loc_descr (&mem_loc_result,
9249                            new_loc_descr (DW_OP_plus_uconst,
9250                                           INTVAL (XEXP (rtl, 1)), 0));
9251           else
9252             {
9253               add_loc_descr (&mem_loc_result,
9254                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9255                                                  VAR_INIT_STATUS_INITIALIZED));
9256               add_loc_descr (&mem_loc_result,
9257                              new_loc_descr (DW_OP_plus, 0, 0));
9258             }
9259         }
9260       break;
9261
9262     /* If a pseudo-reg is optimized away, it is possible for it to
9263        be replaced with a MEM containing a multiply or shift.  */
9264     case MULT:
9265       op = DW_OP_mul;
9266       goto do_binop;
9267
9268     case ASHIFT:
9269       op = DW_OP_shl;
9270       goto do_binop;
9271
9272     case ASHIFTRT:
9273       op = DW_OP_shra;
9274       goto do_binop;
9275
9276     case LSHIFTRT:
9277       op = DW_OP_shr;
9278       goto do_binop;
9279
9280     do_binop:
9281       {
9282         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9283                                                    VAR_INIT_STATUS_INITIALIZED);
9284         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9285                                                    VAR_INIT_STATUS_INITIALIZED);
9286
9287         if (op0 == 0 || op1 == 0)
9288           break;
9289
9290         mem_loc_result = op0;
9291         add_loc_descr (&mem_loc_result, op1);
9292         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9293         break;
9294       }
9295
9296     case CONST_INT:
9297       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9298       break;
9299
9300     case CONCATN:
9301       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode, 
9302                                                    VAR_INIT_STATUS_INITIALIZED);
9303       break;
9304
9305     default:
9306       gcc_unreachable ();
9307     }
9308
9309   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9310     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9311
9312   return mem_loc_result;
9313 }
9314
9315 /* Return a descriptor that describes the concatenation of two locations.
9316    This is typically a complex variable.  */
9317
9318 static dw_loc_descr_ref
9319 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9320 {
9321   dw_loc_descr_ref cc_loc_result = NULL;
9322   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9323   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9324
9325   if (x0_ref == 0 || x1_ref == 0)
9326     return 0;
9327
9328   cc_loc_result = x0_ref;
9329   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9330
9331   add_loc_descr (&cc_loc_result, x1_ref);
9332   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9333
9334   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9335     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9336
9337   return cc_loc_result;
9338 }
9339
9340 /* Return a descriptor that describes the concatenation of N
9341    locations.  */
9342
9343 static dw_loc_descr_ref
9344 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9345 {
9346   unsigned int i;
9347   dw_loc_descr_ref cc_loc_result = NULL;
9348   unsigned int n = XVECLEN (concatn, 0);
9349
9350   for (i = 0; i < n; ++i)
9351     {
9352       dw_loc_descr_ref ref;
9353       rtx x = XVECEXP (concatn, 0, i);
9354
9355       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9356       if (ref == NULL)
9357         return NULL;
9358
9359       add_loc_descr (&cc_loc_result, ref);
9360       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9361     }
9362
9363   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9364     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9365
9366   return cc_loc_result;
9367 }
9368
9369 /* Output a proper Dwarf location descriptor for a variable or parameter
9370    which is either allocated in a register or in a memory location.  For a
9371    register, we just generate an OP_REG and the register number.  For a
9372    memory location we provide a Dwarf postfix expression describing how to
9373    generate the (dynamic) address of the object onto the address stack.
9374
9375    If we don't know how to describe it, return 0.  */
9376
9377 static dw_loc_descr_ref
9378 loc_descriptor (rtx rtl, enum var_init_status initialized)
9379 {
9380   dw_loc_descr_ref loc_result = NULL;
9381
9382   switch (GET_CODE (rtl))
9383     {
9384     case SUBREG:
9385       /* The case of a subreg may arise when we have a local (register)
9386          variable or a formal (register) parameter which doesn't quite fill
9387          up an entire register.  For now, just assume that it is
9388          legitimate to make the Dwarf info refer to the whole register which
9389          contains the given subreg.  */
9390       rtl = SUBREG_REG (rtl);
9391
9392       /* ... fall through ...  */
9393
9394     case REG:
9395       loc_result = reg_loc_descriptor (rtl, initialized);
9396       break;
9397
9398     case MEM:
9399       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9400                                        initialized);
9401       break;
9402
9403     case CONCAT:
9404       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9405                                           initialized);
9406       break;
9407
9408     case CONCATN:
9409       loc_result = concatn_loc_descriptor (rtl, initialized);
9410       break;
9411
9412     case VAR_LOCATION:
9413       /* Single part.  */
9414       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9415         {
9416           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9417           break;
9418         }
9419
9420       rtl = XEXP (rtl, 1);
9421       /* FALLTHRU */
9422
9423     case PARALLEL:
9424       {
9425         rtvec par_elems = XVEC (rtl, 0);
9426         int num_elem = GET_NUM_ELEM (par_elems);
9427         enum machine_mode mode;
9428         int i;
9429
9430         /* Create the first one, so we have something to add to.  */
9431         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9432                                      initialized);
9433         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9434         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9435         for (i = 1; i < num_elem; i++)
9436           {
9437             dw_loc_descr_ref temp;
9438
9439             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9440                                    initialized);
9441             add_loc_descr (&loc_result, temp);
9442             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9443             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9444           }
9445       }
9446       break;
9447
9448     default:
9449       gcc_unreachable ();
9450     }
9451
9452   return loc_result;
9453 }
9454
9455 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9456    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9457    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9458    top-level invocation, and we require the address of LOC; is 0 if we require
9459    the value of LOC.  */
9460
9461 static dw_loc_descr_ref
9462 loc_descriptor_from_tree_1 (tree loc, int want_address)
9463 {
9464   dw_loc_descr_ref ret, ret1;
9465   int have_address = 0;
9466   enum dwarf_location_atom op;
9467
9468   /* ??? Most of the time we do not take proper care for sign/zero
9469      extending the values properly.  Hopefully this won't be a real
9470      problem...  */
9471
9472   switch (TREE_CODE (loc))
9473     {
9474     case ERROR_MARK:
9475       return 0;
9476
9477     case PLACEHOLDER_EXPR:
9478       /* This case involves extracting fields from an object to determine the
9479          position of other fields.  We don't try to encode this here.  The
9480          only user of this is Ada, which encodes the needed information using
9481          the names of types.  */
9482       return 0;
9483
9484     case CALL_EXPR:
9485       return 0;
9486
9487     case PREINCREMENT_EXPR:
9488     case PREDECREMENT_EXPR:
9489     case POSTINCREMENT_EXPR:
9490     case POSTDECREMENT_EXPR:
9491       /* There are no opcodes for these operations.  */
9492       return 0;
9493
9494     case ADDR_EXPR:
9495       /* If we already want an address, there's nothing we can do.  */
9496       if (want_address)
9497         return 0;
9498
9499       /* Otherwise, process the argument and look for the address.  */
9500       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9501
9502     case VAR_DECL:
9503       if (DECL_THREAD_LOCAL_P (loc))
9504         {
9505           rtx rtl;
9506           unsigned first_op;
9507           unsigned second_op;
9508
9509           if (targetm.have_tls)
9510             {
9511               /* If this is not defined, we have no way to emit the
9512                  data.  */
9513               if (!targetm.asm_out.output_dwarf_dtprel)
9514                 return 0;
9515
9516                /* The way DW_OP_GNU_push_tls_address is specified, we
9517                   can only look up addresses of objects in the current
9518                   module.  */
9519               if (DECL_EXTERNAL (loc))
9520                 return 0;
9521               first_op = INTERNAL_DW_OP_tls_addr;
9522               second_op = DW_OP_GNU_push_tls_address;
9523             }
9524           else
9525             {
9526               if (!targetm.emutls.debug_form_tls_address)
9527                 return 0;
9528               loc = emutls_decl (loc);
9529               first_op = DW_OP_addr;
9530               second_op = DW_OP_form_tls_address;
9531             }
9532           
9533           rtl = rtl_for_decl_location (loc);
9534           if (rtl == NULL_RTX)
9535             return 0;
9536
9537           if (!MEM_P (rtl))
9538             return 0;
9539           rtl = XEXP (rtl, 0);
9540           if (! CONSTANT_P (rtl))
9541             return 0;
9542
9543           ret = new_loc_descr (first_op, 0, 0);
9544           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9545           ret->dw_loc_oprnd1.v.val_addr = rtl;
9546           
9547           ret1 = new_loc_descr (second_op, 0, 0);
9548           add_loc_descr (&ret, ret1);
9549
9550           have_address = 1;
9551           break;
9552         }
9553       /* FALLTHRU */
9554
9555     case PARM_DECL:
9556       if (DECL_HAS_VALUE_EXPR_P (loc))
9557         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9558                                            want_address);
9559       /* FALLTHRU */
9560
9561     case RESULT_DECL:
9562     case FUNCTION_DECL:
9563       {
9564         rtx rtl = rtl_for_decl_location (loc);
9565
9566         if (rtl == NULL_RTX)
9567           return 0;
9568         else if (GET_CODE (rtl) == CONST_INT)
9569           {
9570             HOST_WIDE_INT val = INTVAL (rtl);
9571             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9572               val &= GET_MODE_MASK (DECL_MODE (loc));
9573             ret = int_loc_descriptor (val);
9574           }
9575         else if (GET_CODE (rtl) == CONST_STRING)
9576           return 0;
9577         else if (CONSTANT_P (rtl))
9578           {
9579             ret = new_loc_descr (DW_OP_addr, 0, 0);
9580             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9581             ret->dw_loc_oprnd1.v.val_addr = rtl;
9582           }
9583         else
9584           {
9585             enum machine_mode mode;
9586
9587             /* Certain constructs can only be represented at top-level.  */
9588             if (want_address == 2)
9589               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9590
9591             mode = GET_MODE (rtl);
9592             if (MEM_P (rtl))
9593               {
9594                 rtl = XEXP (rtl, 0);
9595                 have_address = 1;
9596               }
9597             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9598           }
9599       }
9600       break;
9601
9602     case INDIRECT_REF:
9603       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9604       have_address = 1;
9605       break;
9606
9607     case COMPOUND_EXPR:
9608       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9609
9610     CASE_CONVERT:
9611     case VIEW_CONVERT_EXPR:
9612     case SAVE_EXPR:
9613     case GIMPLE_MODIFY_STMT:
9614       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9615                                          want_address);
9616
9617     case COMPONENT_REF:
9618     case BIT_FIELD_REF:
9619     case ARRAY_REF:
9620     case ARRAY_RANGE_REF:
9621       {
9622         tree obj, offset;
9623         HOST_WIDE_INT bitsize, bitpos, bytepos;
9624         enum machine_mode mode;
9625         int volatilep;
9626         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9627
9628         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9629                                    &unsignedp, &volatilep, false);
9630
9631         if (obj == loc)
9632           return 0;
9633
9634         ret = loc_descriptor_from_tree_1 (obj, 1);
9635         if (ret == 0
9636             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9637           return 0;
9638
9639         if (offset != NULL_TREE)
9640           {
9641             /* Variable offset.  */
9642             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9643             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9644           }
9645
9646         bytepos = bitpos / BITS_PER_UNIT;
9647         if (bytepos > 0)
9648           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9649         else if (bytepos < 0)
9650           {
9651             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9652             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9653           }
9654
9655         have_address = 1;
9656         break;
9657       }
9658
9659     case INTEGER_CST:
9660       if (host_integerp (loc, 0))
9661         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9662       else
9663         return 0;
9664       break;
9665
9666     case CONSTRUCTOR:
9667       {
9668         /* Get an RTL for this, if something has been emitted.  */
9669         rtx rtl = lookup_constant_def (loc);
9670         enum machine_mode mode;
9671
9672         if (!rtl || !MEM_P (rtl))
9673           return 0;
9674         mode = GET_MODE (rtl);
9675         rtl = XEXP (rtl, 0);
9676         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9677         have_address = 1;
9678         break;
9679       }
9680
9681     case TRUTH_AND_EXPR:
9682     case TRUTH_ANDIF_EXPR:
9683     case BIT_AND_EXPR:
9684       op = DW_OP_and;
9685       goto do_binop;
9686
9687     case TRUTH_XOR_EXPR:
9688     case BIT_XOR_EXPR:
9689       op = DW_OP_xor;
9690       goto do_binop;
9691
9692     case TRUTH_OR_EXPR:
9693     case TRUTH_ORIF_EXPR:
9694     case BIT_IOR_EXPR:
9695       op = DW_OP_or;
9696       goto do_binop;
9697
9698     case FLOOR_DIV_EXPR:
9699     case CEIL_DIV_EXPR:
9700     case ROUND_DIV_EXPR:
9701     case TRUNC_DIV_EXPR:
9702       op = DW_OP_div;
9703       goto do_binop;
9704
9705     case MINUS_EXPR:
9706       op = DW_OP_minus;
9707       goto do_binop;
9708
9709     case FLOOR_MOD_EXPR:
9710     case CEIL_MOD_EXPR:
9711     case ROUND_MOD_EXPR:
9712     case TRUNC_MOD_EXPR:
9713       op = DW_OP_mod;
9714       goto do_binop;
9715
9716     case MULT_EXPR:
9717       op = DW_OP_mul;
9718       goto do_binop;
9719
9720     case LSHIFT_EXPR:
9721       op = DW_OP_shl;
9722       goto do_binop;
9723
9724     case RSHIFT_EXPR:
9725       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9726       goto do_binop;
9727
9728     case POINTER_PLUS_EXPR:
9729     case PLUS_EXPR:
9730       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9731           && host_integerp (TREE_OPERAND (loc, 1), 0))
9732         {
9733           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9734           if (ret == 0)
9735             return 0;
9736
9737           add_loc_descr (&ret,
9738                          new_loc_descr (DW_OP_plus_uconst,
9739                                         tree_low_cst (TREE_OPERAND (loc, 1),
9740                                                       0),
9741                                         0));
9742           break;
9743         }
9744
9745       op = DW_OP_plus;
9746       goto do_binop;
9747
9748     case LE_EXPR:
9749       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9750         return 0;
9751
9752       op = DW_OP_le;
9753       goto do_binop;
9754
9755     case GE_EXPR:
9756       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9757         return 0;
9758
9759       op = DW_OP_ge;
9760       goto do_binop;
9761
9762     case LT_EXPR:
9763       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9764         return 0;
9765
9766       op = DW_OP_lt;
9767       goto do_binop;
9768
9769     case GT_EXPR:
9770       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9771         return 0;
9772
9773       op = DW_OP_gt;
9774       goto do_binop;
9775
9776     case EQ_EXPR:
9777       op = DW_OP_eq;
9778       goto do_binop;
9779
9780     case NE_EXPR:
9781       op = DW_OP_ne;
9782       goto do_binop;
9783
9784     do_binop:
9785       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9786       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9787       if (ret == 0 || ret1 == 0)
9788         return 0;
9789
9790       add_loc_descr (&ret, ret1);
9791       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9792       break;
9793
9794     case TRUTH_NOT_EXPR:
9795     case BIT_NOT_EXPR:
9796       op = DW_OP_not;
9797       goto do_unop;
9798
9799     case ABS_EXPR:
9800       op = DW_OP_abs;
9801       goto do_unop;
9802
9803     case NEGATE_EXPR:
9804       op = DW_OP_neg;
9805       goto do_unop;
9806
9807     do_unop:
9808       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9809       if (ret == 0)
9810         return 0;
9811
9812       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9813       break;
9814
9815     case MIN_EXPR:
9816     case MAX_EXPR:
9817       {
9818         const enum tree_code code =
9819           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9820
9821         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9822                       build2 (code, integer_type_node,
9823                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9824                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9825       }
9826
9827       /* ... fall through ...  */
9828
9829     case COND_EXPR:
9830       {
9831         dw_loc_descr_ref lhs
9832           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9833         dw_loc_descr_ref rhs
9834           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9835         dw_loc_descr_ref bra_node, jump_node, tmp;
9836
9837         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9838         if (ret == 0 || lhs == 0 || rhs == 0)
9839           return 0;
9840
9841         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9842         add_loc_descr (&ret, bra_node);
9843
9844         add_loc_descr (&ret, rhs);
9845         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9846         add_loc_descr (&ret, jump_node);
9847
9848         add_loc_descr (&ret, lhs);
9849         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9850         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9851
9852         /* ??? Need a node to point the skip at.  Use a nop.  */
9853         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9854         add_loc_descr (&ret, tmp);
9855         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9856         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9857       }
9858       break;
9859
9860     case FIX_TRUNC_EXPR:
9861       return 0;
9862
9863     default:
9864       /* Leave front-end specific codes as simply unknown.  This comes
9865          up, for instance, with the C STMT_EXPR.  */
9866       if ((unsigned int) TREE_CODE (loc)
9867           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9868         return 0;
9869
9870 #ifdef ENABLE_CHECKING
9871       /* Otherwise this is a generic code; we should just lists all of
9872          these explicitly.  We forgot one.  */
9873       gcc_unreachable ();
9874 #else
9875       /* In a release build, we want to degrade gracefully: better to
9876          generate incomplete debugging information than to crash.  */
9877       return NULL;
9878 #endif
9879     }
9880
9881   /* Show if we can't fill the request for an address.  */
9882   if (want_address && !have_address)
9883     return 0;
9884
9885   /* If we've got an address and don't want one, dereference.  */
9886   if (!want_address && have_address && ret)
9887     {
9888       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9889
9890       if (size > DWARF2_ADDR_SIZE || size == -1)
9891         return 0;
9892       else if (size == DWARF2_ADDR_SIZE)
9893         op = DW_OP_deref;
9894       else
9895         op = DW_OP_deref_size;
9896
9897       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9898     }
9899
9900   return ret;
9901 }
9902
9903 static inline dw_loc_descr_ref
9904 loc_descriptor_from_tree (tree loc)
9905 {
9906   return loc_descriptor_from_tree_1 (loc, 2);
9907 }
9908
9909 /* Given a value, round it up to the lowest multiple of `boundary'
9910    which is not less than the value itself.  */
9911
9912 static inline HOST_WIDE_INT
9913 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9914 {
9915   return (((value + boundary - 1) / boundary) * boundary);
9916 }
9917
9918 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9919    pointer to the declared type for the relevant field variable, or return
9920    `integer_type_node' if the given node turns out to be an
9921    ERROR_MARK node.  */
9922
9923 static inline tree
9924 field_type (const_tree decl)
9925 {
9926   tree type;
9927
9928   if (TREE_CODE (decl) == ERROR_MARK)
9929     return integer_type_node;
9930
9931   type = DECL_BIT_FIELD_TYPE (decl);
9932   if (type == NULL_TREE)
9933     type = TREE_TYPE (decl);
9934
9935   return type;
9936 }
9937
9938 /* Given a pointer to a tree node, return the alignment in bits for
9939    it, or else return BITS_PER_WORD if the node actually turns out to
9940    be an ERROR_MARK node.  */
9941
9942 static inline unsigned
9943 simple_type_align_in_bits (const_tree type)
9944 {
9945   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9946 }
9947
9948 static inline unsigned
9949 simple_decl_align_in_bits (const_tree decl)
9950 {
9951   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9952 }
9953
9954 /* Return the result of rounding T up to ALIGN.  */
9955
9956 static inline HOST_WIDE_INT
9957 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9958 {
9959   /* We must be careful if T is negative because HOST_WIDE_INT can be
9960      either "above" or "below" unsigned int as per the C promotion
9961      rules, depending on the host, thus making the signedness of the
9962      direct multiplication and division unpredictable.  */
9963   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9964
9965   u += align - 1;
9966   u /= align;
9967   u *= align;
9968
9969   return (HOST_WIDE_INT) u;
9970 }
9971
9972 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9973    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9974    or return 0 if we are unable to determine what that offset is, either
9975    because the argument turns out to be a pointer to an ERROR_MARK node, or
9976    because the offset is actually variable.  (We can't handle the latter case
9977    just yet).  */
9978
9979 static HOST_WIDE_INT
9980 field_byte_offset (const_tree decl)
9981 {
9982   HOST_WIDE_INT object_offset_in_bits;
9983   HOST_WIDE_INT bitpos_int;
9984
9985   if (TREE_CODE (decl) == ERROR_MARK)
9986     return 0;
9987
9988   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9989
9990   /* We cannot yet cope with fields whose positions are variable, so
9991      for now, when we see such things, we simply return 0.  Someday, we may
9992      be able to handle such cases, but it will be damn difficult.  */
9993   if (! host_integerp (bit_position (decl), 0))
9994     return 0;
9995
9996   bitpos_int = int_bit_position (decl);
9997
9998 #ifdef PCC_BITFIELD_TYPE_MATTERS
9999   if (PCC_BITFIELD_TYPE_MATTERS)
10000     {
10001       tree type;
10002       tree field_size_tree;
10003       HOST_WIDE_INT deepest_bitpos;
10004       unsigned HOST_WIDE_INT field_size_in_bits;
10005       unsigned int type_align_in_bits;
10006       unsigned int decl_align_in_bits;
10007       unsigned HOST_WIDE_INT type_size_in_bits;
10008
10009       type = field_type (decl);
10010       field_size_tree = DECL_SIZE (decl);
10011
10012       /* The size could be unspecified if there was an error, or for
10013          a flexible array member.  */
10014       if (! field_size_tree)
10015         field_size_tree = bitsize_zero_node;
10016
10017       /* If we don't know the size of the field, pretend it's a full word.  */
10018       if (host_integerp (field_size_tree, 1))
10019         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10020       else
10021         field_size_in_bits = BITS_PER_WORD;
10022
10023       type_size_in_bits = simple_type_size_in_bits (type);
10024       type_align_in_bits = simple_type_align_in_bits (type);
10025       decl_align_in_bits = simple_decl_align_in_bits (decl);
10026
10027       /* The GCC front-end doesn't make any attempt to keep track of the
10028          starting bit offset (relative to the start of the containing
10029          structure type) of the hypothetical "containing object" for a
10030          bit-field.  Thus, when computing the byte offset value for the
10031          start of the "containing object" of a bit-field, we must deduce
10032          this information on our own. This can be rather tricky to do in
10033          some cases.  For example, handling the following structure type
10034          definition when compiling for an i386/i486 target (which only
10035          aligns long long's to 32-bit boundaries) can be very tricky:
10036
10037          struct S { int field1; long long field2:31; };
10038
10039          Fortunately, there is a simple rule-of-thumb which can be used
10040          in such cases.  When compiling for an i386/i486, GCC will
10041          allocate 8 bytes for the structure shown above.  It decides to
10042          do this based upon one simple rule for bit-field allocation.
10043          GCC allocates each "containing object" for each bit-field at
10044          the first (i.e. lowest addressed) legitimate alignment boundary
10045          (based upon the required minimum alignment for the declared
10046          type of the field) which it can possibly use, subject to the
10047          condition that there is still enough available space remaining
10048          in the containing object (when allocated at the selected point)
10049          to fully accommodate all of the bits of the bit-field itself.
10050
10051          This simple rule makes it obvious why GCC allocates 8 bytes for
10052          each object of the structure type shown above.  When looking
10053          for a place to allocate the "containing object" for `field2',
10054          the compiler simply tries to allocate a 64-bit "containing
10055          object" at each successive 32-bit boundary (starting at zero)
10056          until it finds a place to allocate that 64- bit field such that
10057          at least 31 contiguous (and previously unallocated) bits remain
10058          within that selected 64 bit field.  (As it turns out, for the
10059          example above, the compiler finds it is OK to allocate the
10060          "containing object" 64-bit field at bit-offset zero within the
10061          structure type.)
10062
10063          Here we attempt to work backwards from the limited set of facts
10064          we're given, and we try to deduce from those facts, where GCC
10065          must have believed that the containing object started (within
10066          the structure type). The value we deduce is then used (by the
10067          callers of this routine) to generate DW_AT_location and
10068          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10069          the case of DW_AT_location, regular fields as well).  */
10070
10071       /* Figure out the bit-distance from the start of the structure to
10072          the "deepest" bit of the bit-field.  */
10073       deepest_bitpos = bitpos_int + field_size_in_bits;
10074
10075       /* This is the tricky part.  Use some fancy footwork to deduce
10076          where the lowest addressed bit of the containing object must
10077          be.  */
10078       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10079
10080       /* Round up to type_align by default.  This works best for
10081          bitfields.  */
10082       object_offset_in_bits
10083         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10084
10085       if (object_offset_in_bits > bitpos_int)
10086         {
10087           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10088
10089           /* Round up to decl_align instead.  */
10090           object_offset_in_bits
10091             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10092         }
10093     }
10094   else
10095 #endif
10096     object_offset_in_bits = bitpos_int;
10097
10098   return object_offset_in_bits / BITS_PER_UNIT;
10099 }
10100 \f
10101 /* The following routines define various Dwarf attributes and any data
10102    associated with them.  */
10103
10104 /* Add a location description attribute value to a DIE.
10105
10106    This emits location attributes suitable for whole variables and
10107    whole parameters.  Note that the location attributes for struct fields are
10108    generated by the routine `data_member_location_attribute' below.  */
10109
10110 static inline void
10111 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10112                              dw_loc_descr_ref descr)
10113 {
10114   if (descr != 0)
10115     add_AT_loc (die, attr_kind, descr);
10116 }
10117
10118 /* Attach the specialized form of location attribute used for data members of
10119    struct and union types.  In the special case of a FIELD_DECL node which
10120    represents a bit-field, the "offset" part of this special location
10121    descriptor must indicate the distance in bytes from the lowest-addressed
10122    byte of the containing struct or union type to the lowest-addressed byte of
10123    the "containing object" for the bit-field.  (See the `field_byte_offset'
10124    function above).
10125
10126    For any given bit-field, the "containing object" is a hypothetical object
10127    (of some integral or enum type) within which the given bit-field lives.  The
10128    type of this hypothetical "containing object" is always the same as the
10129    declared type of the individual bit-field itself (for GCC anyway... the
10130    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10131    bytes) of the hypothetical "containing object" which will be given in the
10132    DW_AT_byte_size attribute for this bit-field.  (See the
10133    `byte_size_attribute' function below.)  It is also used when calculating the
10134    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10135    function below.)  */
10136
10137 static void
10138 add_data_member_location_attribute (dw_die_ref die, tree decl)
10139 {
10140   HOST_WIDE_INT offset;
10141   dw_loc_descr_ref loc_descr = 0;
10142
10143   if (TREE_CODE (decl) == TREE_BINFO)
10144     {
10145       /* We're working on the TAG_inheritance for a base class.  */
10146       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10147         {
10148           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10149              aren't at a fixed offset from all (sub)objects of the same
10150              type.  We need to extract the appropriate offset from our
10151              vtable.  The following dwarf expression means
10152
10153                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10154
10155              This is specific to the V3 ABI, of course.  */
10156
10157           dw_loc_descr_ref tmp;
10158
10159           /* Make a copy of the object address.  */
10160           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10161           add_loc_descr (&loc_descr, tmp);
10162
10163           /* Extract the vtable address.  */
10164           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10165           add_loc_descr (&loc_descr, tmp);
10166
10167           /* Calculate the address of the offset.  */
10168           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10169           gcc_assert (offset < 0);
10170
10171           tmp = int_loc_descriptor (-offset);
10172           add_loc_descr (&loc_descr, tmp);
10173           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10174           add_loc_descr (&loc_descr, tmp);
10175
10176           /* Extract the offset.  */
10177           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10178           add_loc_descr (&loc_descr, tmp);
10179
10180           /* Add it to the object address.  */
10181           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10182           add_loc_descr (&loc_descr, tmp);
10183         }
10184       else
10185         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10186     }
10187   else
10188     offset = field_byte_offset (decl);
10189
10190   if (! loc_descr)
10191     {
10192       enum dwarf_location_atom op;
10193
10194       /* The DWARF2 standard says that we should assume that the structure
10195          address is already on the stack, so we can specify a structure field
10196          address by using DW_OP_plus_uconst.  */
10197
10198 #ifdef MIPS_DEBUGGING_INFO
10199       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10200          operator correctly.  It works only if we leave the offset on the
10201          stack.  */
10202       op = DW_OP_constu;
10203 #else
10204       op = DW_OP_plus_uconst;
10205 #endif
10206
10207       loc_descr = new_loc_descr (op, offset, 0);
10208     }
10209
10210   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10211 }
10212
10213 /* Writes integer values to dw_vec_const array.  */
10214
10215 static void
10216 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10217 {
10218   while (size != 0)
10219     {
10220       *dest++ = val & 0xff;
10221       val >>= 8;
10222       --size;
10223     }
10224 }
10225
10226 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10227
10228 static HOST_WIDE_INT
10229 extract_int (const unsigned char *src, unsigned int size)
10230 {
10231   HOST_WIDE_INT val = 0;
10232
10233   src += size;
10234   while (size != 0)
10235     {
10236       val <<= 8;
10237       val |= *--src & 0xff;
10238       --size;
10239     }
10240   return val;
10241 }
10242
10243 /* Writes floating point values to dw_vec_const array.  */
10244
10245 static void
10246 insert_float (const_rtx rtl, unsigned char *array)
10247 {
10248   REAL_VALUE_TYPE rv;
10249   long val[4];
10250   int i;
10251
10252   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10253   real_to_target (val, &rv, GET_MODE (rtl));
10254
10255   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10256   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10257     {
10258       insert_int (val[i], 4, array);
10259       array += 4;
10260     }
10261 }
10262
10263 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10264    does not have a "location" either in memory or in a register.  These
10265    things can arise in GNU C when a constant is passed as an actual parameter
10266    to an inlined function.  They can also arise in C++ where declared
10267    constants do not necessarily get memory "homes".  */
10268
10269 static void
10270 add_const_value_attribute (dw_die_ref die, rtx rtl)
10271 {
10272   switch (GET_CODE (rtl))
10273     {
10274     case CONST_INT:
10275       {
10276         HOST_WIDE_INT val = INTVAL (rtl);
10277
10278         if (val < 0)
10279           add_AT_int (die, DW_AT_const_value, val);
10280         else
10281           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10282       }
10283       break;
10284
10285     case CONST_DOUBLE:
10286       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10287          floating-point constant.  A CONST_DOUBLE is used whenever the
10288          constant requires more than one word in order to be adequately
10289          represented.  We output CONST_DOUBLEs as blocks.  */
10290       {
10291         enum machine_mode mode = GET_MODE (rtl);
10292
10293         if (SCALAR_FLOAT_MODE_P (mode))
10294           {
10295             unsigned int length = GET_MODE_SIZE (mode);
10296             unsigned char *array = ggc_alloc (length);
10297
10298             insert_float (rtl, array);
10299             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10300           }
10301         else
10302           {
10303             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10304             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10305
10306             add_AT_long_long (die, DW_AT_const_value,
10307                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10308           }
10309       }
10310       break;
10311
10312     case CONST_VECTOR:
10313       {
10314         enum machine_mode mode = GET_MODE (rtl);
10315         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10316         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10317         unsigned char *array = ggc_alloc (length * elt_size);
10318         unsigned int i;
10319         unsigned char *p;
10320
10321         switch (GET_MODE_CLASS (mode))
10322           {
10323           case MODE_VECTOR_INT:
10324             for (i = 0, p = array; i < length; i++, p += elt_size)
10325               {
10326                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10327                 HOST_WIDE_INT lo, hi;
10328
10329                 switch (GET_CODE (elt))
10330                   {
10331                   case CONST_INT:
10332                     lo = INTVAL (elt);
10333                     hi = -(lo < 0);
10334                     break;
10335
10336                   case CONST_DOUBLE:
10337                     lo = CONST_DOUBLE_LOW (elt);
10338                     hi = CONST_DOUBLE_HIGH (elt);
10339                     break;
10340
10341                   default:
10342                     gcc_unreachable ();
10343                   }
10344
10345                 if (elt_size <= sizeof (HOST_WIDE_INT))
10346                   insert_int (lo, elt_size, p);
10347                 else
10348                   {
10349                     unsigned char *p0 = p;
10350                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10351
10352                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10353                     if (WORDS_BIG_ENDIAN)
10354                       {
10355                         p0 = p1;
10356                         p1 = p;
10357                       }
10358                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10359                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10360                   }
10361               }
10362             break;
10363
10364           case MODE_VECTOR_FLOAT:
10365             for (i = 0, p = array; i < length; i++, p += elt_size)
10366               {
10367                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10368                 insert_float (elt, p);
10369               }
10370             break;
10371
10372           default:
10373             gcc_unreachable ();
10374           }
10375
10376         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10377       }
10378       break;
10379
10380     case CONST_STRING:
10381       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10382       break;
10383
10384     case SYMBOL_REF:
10385     case LABEL_REF:
10386     case CONST:
10387       add_AT_addr (die, DW_AT_const_value, rtl);
10388       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10389       break;
10390
10391     case PLUS:
10392       /* In cases where an inlined instance of an inline function is passed
10393          the address of an `auto' variable (which is local to the caller) we
10394          can get a situation where the DECL_RTL of the artificial local
10395          variable (for the inlining) which acts as a stand-in for the
10396          corresponding formal parameter (of the inline function) will look
10397          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10398          exactly a compile-time constant expression, but it isn't the address
10399          of the (artificial) local variable either.  Rather, it represents the
10400          *value* which the artificial local variable always has during its
10401          lifetime.  We currently have no way to represent such quasi-constant
10402          values in Dwarf, so for now we just punt and generate nothing.  */
10403       break;
10404
10405     default:
10406       /* No other kinds of rtx should be possible here.  */
10407       gcc_unreachable ();
10408     }
10409
10410 }
10411
10412 /* Determine whether the evaluation of EXPR references any variables
10413    or functions which aren't otherwise used (and therefore may not be
10414    output).  */
10415 static tree
10416 reference_to_unused (tree * tp, int * walk_subtrees,
10417                      void * data ATTRIBUTE_UNUSED)
10418 {
10419   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10420     *walk_subtrees = 0;
10421
10422   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10423       && ! TREE_ASM_WRITTEN (*tp))
10424     return *tp;
10425   else if (!flag_unit_at_a_time)
10426     return NULL_TREE;
10427   /* ???  The C++ FE emits debug information for using decls, so
10428      putting gcc_unreachable here falls over.  See PR31899.  For now
10429      be conservative.  */
10430   else if (!cgraph_global_info_ready
10431            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10432     return *tp;
10433   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10434     {
10435       struct varpool_node *node = varpool_node (*tp);
10436       if (!node->needed)
10437         return *tp;
10438     }
10439   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10440            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10441     {
10442       struct cgraph_node *node = cgraph_node (*tp);
10443       if (!node->output)
10444         return *tp;
10445     }
10446   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10447     return *tp;
10448
10449   return NULL_TREE;
10450 }
10451
10452 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10453    for use in a later add_const_value_attribute call.  */
10454
10455 static rtx
10456 rtl_for_decl_init (tree init, tree type)
10457 {
10458   rtx rtl = NULL_RTX;
10459
10460   /* If a variable is initialized with a string constant without embedded
10461      zeros, build CONST_STRING.  */
10462   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10463     {
10464       tree enttype = TREE_TYPE (type);
10465       tree domain = TYPE_DOMAIN (type);
10466       enum machine_mode mode = TYPE_MODE (enttype);
10467
10468       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10469           && domain
10470           && integer_zerop (TYPE_MIN_VALUE (domain))
10471           && compare_tree_int (TYPE_MAX_VALUE (domain),
10472                                TREE_STRING_LENGTH (init) - 1) == 0
10473           && ((size_t) TREE_STRING_LENGTH (init)
10474               == strlen (TREE_STRING_POINTER (init)) + 1))
10475         rtl = gen_rtx_CONST_STRING (VOIDmode,
10476                                     ggc_strdup (TREE_STRING_POINTER (init)));
10477     }
10478   /* Other aggregates, and complex values, could be represented using
10479      CONCAT: FIXME!  */
10480   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10481     ;
10482   /* Vectors only work if their mode is supported by the target.
10483      FIXME: generic vectors ought to work too.  */
10484   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10485     ;
10486   /* If the initializer is something that we know will expand into an
10487      immediate RTL constant, expand it now.  We must be careful not to
10488      reference variables which won't be output.  */
10489   else if (initializer_constant_valid_p (init, type)
10490            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10491     {
10492       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10493          possible.  */
10494       if (TREE_CODE (type) == VECTOR_TYPE)
10495         switch (TREE_CODE (init))
10496           {
10497           case VECTOR_CST:
10498             break;
10499           case CONSTRUCTOR:
10500             if (TREE_CONSTANT (init))
10501               {
10502                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10503                 bool constant_p = true;
10504                 tree value;
10505                 unsigned HOST_WIDE_INT ix;
10506
10507                 /* Even when ctor is constant, it might contain non-*_CST
10508                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10509                    belong into VECTOR_CST nodes.  */
10510                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10511                   if (!CONSTANT_CLASS_P (value))
10512                     {
10513                       constant_p = false;
10514                       break;
10515                     }
10516
10517                 if (constant_p)
10518                   {
10519                     init = build_vector_from_ctor (type, elts);
10520                     break;
10521                   }
10522               }
10523             /* FALLTHRU */
10524
10525           default:
10526             return NULL;
10527           }
10528
10529       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10530
10531       /* If expand_expr returns a MEM, it wasn't immediate.  */
10532       gcc_assert (!rtl || !MEM_P (rtl));
10533     }
10534
10535   return rtl;
10536 }
10537
10538 /* Generate RTL for the variable DECL to represent its location.  */
10539
10540 static rtx
10541 rtl_for_decl_location (tree decl)
10542 {
10543   rtx rtl;
10544
10545   /* Here we have to decide where we are going to say the parameter "lives"
10546      (as far as the debugger is concerned).  We only have a couple of
10547      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10548
10549      DECL_RTL normally indicates where the parameter lives during most of the
10550      activation of the function.  If optimization is enabled however, this
10551      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10552      that the parameter doesn't really live anywhere (as far as the code
10553      generation parts of GCC are concerned) during most of the function's
10554      activation.  That will happen (for example) if the parameter is never
10555      referenced within the function.
10556
10557      We could just generate a location descriptor here for all non-NULL
10558      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10559      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10560      where DECL_RTL is NULL or is a pseudo-reg.
10561
10562      Note however that we can only get away with using DECL_INCOMING_RTL as
10563      a backup substitute for DECL_RTL in certain limited cases.  In cases
10564      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10565      we can be sure that the parameter was passed using the same type as it is
10566      declared to have within the function, and that its DECL_INCOMING_RTL
10567      points us to a place where a value of that type is passed.
10568
10569      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10570      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10571      because in these cases DECL_INCOMING_RTL points us to a value of some
10572      type which is *different* from the type of the parameter itself.  Thus,
10573      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10574      such cases, the debugger would end up (for example) trying to fetch a
10575      `float' from a place which actually contains the first part of a
10576      `double'.  That would lead to really incorrect and confusing
10577      output at debug-time.
10578
10579      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10580      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10581      are a couple of exceptions however.  On little-endian machines we can
10582      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10583      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10584      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10585      when (on a little-endian machine) a non-prototyped function has a
10586      parameter declared to be of type `short' or `char'.  In such cases,
10587      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10588      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10589      passed `int' value.  If the debugger then uses that address to fetch
10590      a `short' or a `char' (on a little-endian machine) the result will be
10591      the correct data, so we allow for such exceptional cases below.
10592
10593      Note that our goal here is to describe the place where the given formal
10594      parameter lives during most of the function's activation (i.e. between the
10595      end of the prologue and the start of the epilogue).  We'll do that as best
10596      as we can. Note however that if the given formal parameter is modified
10597      sometime during the execution of the function, then a stack backtrace (at
10598      debug-time) will show the function as having been called with the *new*
10599      value rather than the value which was originally passed in.  This happens
10600      rarely enough that it is not a major problem, but it *is* a problem, and
10601      I'd like to fix it.
10602
10603      A future version of dwarf2out.c may generate two additional attributes for
10604      any given DW_TAG_formal_parameter DIE which will describe the "passed
10605      type" and the "passed location" for the given formal parameter in addition
10606      to the attributes we now generate to indicate the "declared type" and the
10607      "active location" for each parameter.  This additional set of attributes
10608      could be used by debuggers for stack backtraces. Separately, note that
10609      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10610      This happens (for example) for inlined-instances of inline function formal
10611      parameters which are never referenced.  This really shouldn't be
10612      happening.  All PARM_DECL nodes should get valid non-NULL
10613      DECL_INCOMING_RTL values.  FIXME.  */
10614
10615   /* Use DECL_RTL as the "location" unless we find something better.  */
10616   rtl = DECL_RTL_IF_SET (decl);
10617
10618   /* When generating abstract instances, ignore everything except
10619      constants, symbols living in memory, and symbols living in
10620      fixed registers.  */
10621   if (! reload_completed)
10622     {
10623       if (rtl
10624           && (CONSTANT_P (rtl)
10625               || (MEM_P (rtl)
10626                   && CONSTANT_P (XEXP (rtl, 0)))
10627               || (REG_P (rtl)
10628                   && TREE_CODE (decl) == VAR_DECL
10629                   && TREE_STATIC (decl))))
10630         {
10631           rtl = targetm.delegitimize_address (rtl);
10632           return rtl;
10633         }
10634       rtl = NULL_RTX;
10635     }
10636   else if (TREE_CODE (decl) == PARM_DECL)
10637     {
10638       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10639         {
10640           tree declared_type = TREE_TYPE (decl);
10641           tree passed_type = DECL_ARG_TYPE (decl);
10642           enum machine_mode dmode = TYPE_MODE (declared_type);
10643           enum machine_mode pmode = TYPE_MODE (passed_type);
10644
10645           /* This decl represents a formal parameter which was optimized out.
10646              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10647              all cases where (rtl == NULL_RTX) just below.  */
10648           if (dmode == pmode)
10649             rtl = DECL_INCOMING_RTL (decl);
10650           else if (SCALAR_INT_MODE_P (dmode)
10651                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10652                    && DECL_INCOMING_RTL (decl))
10653             {
10654               rtx inc = DECL_INCOMING_RTL (decl);
10655               if (REG_P (inc))
10656                 rtl = inc;
10657               else if (MEM_P (inc))
10658                 {
10659                   if (BYTES_BIG_ENDIAN)
10660                     rtl = adjust_address_nv (inc, dmode,
10661                                              GET_MODE_SIZE (pmode)
10662                                              - GET_MODE_SIZE (dmode));
10663                   else
10664                     rtl = inc;
10665                 }
10666             }
10667         }
10668
10669       /* If the parm was passed in registers, but lives on the stack, then
10670          make a big endian correction if the mode of the type of the
10671          parameter is not the same as the mode of the rtl.  */
10672       /* ??? This is the same series of checks that are made in dbxout.c before
10673          we reach the big endian correction code there.  It isn't clear if all
10674          of these checks are necessary here, but keeping them all is the safe
10675          thing to do.  */
10676       else if (MEM_P (rtl)
10677                && XEXP (rtl, 0) != const0_rtx
10678                && ! CONSTANT_P (XEXP (rtl, 0))
10679                /* Not passed in memory.  */
10680                && !MEM_P (DECL_INCOMING_RTL (decl))
10681                /* Not passed by invisible reference.  */
10682                && (!REG_P (XEXP (rtl, 0))
10683                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10684                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10685 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10686                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10687 #endif
10688                      )
10689                /* Big endian correction check.  */
10690                && BYTES_BIG_ENDIAN
10691                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10692                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10693                    < UNITS_PER_WORD))
10694         {
10695           int offset = (UNITS_PER_WORD
10696                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10697
10698           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10699                              plus_constant (XEXP (rtl, 0), offset));
10700         }
10701     }
10702   else if (TREE_CODE (decl) == VAR_DECL
10703            && rtl
10704            && MEM_P (rtl)
10705            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10706            && BYTES_BIG_ENDIAN)
10707     {
10708       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10709       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10710
10711       /* If a variable is declared "register" yet is smaller than
10712          a register, then if we store the variable to memory, it
10713          looks like we're storing a register-sized value, when in
10714          fact we are not.  We need to adjust the offset of the
10715          storage location to reflect the actual value's bytes,
10716          else gdb will not be able to display it.  */
10717       if (rsize > dsize)
10718         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10719                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10720     }
10721
10722   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10723      and will have been substituted directly into all expressions that use it.
10724      C does not have such a concept, but C++ and other languages do.  */
10725   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10726     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10727
10728   if (rtl)
10729     rtl = targetm.delegitimize_address (rtl);
10730
10731   /* If we don't look past the constant pool, we risk emitting a
10732      reference to a constant pool entry that isn't referenced from
10733      code, and thus is not emitted.  */
10734   if (rtl)
10735     rtl = avoid_constant_pool_reference (rtl);
10736
10737   return rtl;
10738 }
10739
10740 /* We need to figure out what section we should use as the base for the
10741    address ranges where a given location is valid.
10742    1. If this particular DECL has a section associated with it, use that.
10743    2. If this function has a section associated with it, use that.
10744    3. Otherwise, use the text section.
10745    XXX: If you split a variable across multiple sections, we won't notice.  */
10746
10747 static const char *
10748 secname_for_decl (const_tree decl)
10749 {
10750   const char *secname;
10751
10752   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10753     {
10754       tree sectree = DECL_SECTION_NAME (decl);
10755       secname = TREE_STRING_POINTER (sectree);
10756     }
10757   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10758     {
10759       tree sectree = DECL_SECTION_NAME (current_function_decl);
10760       secname = TREE_STRING_POINTER (sectree);
10761     }
10762   else if (cfun && in_cold_section_p)
10763     secname = crtl->subsections.cold_section_label;
10764   else
10765     secname = text_section_label;
10766
10767   return secname;
10768 }
10769
10770 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_RTX is returned.
10771    If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
10772    value is the offset into the common block for the symbol.  */
10773
10774 static tree
10775 fortran_common (tree decl, HOST_WIDE_INT *value)
10776 {
10777   tree val_expr, cvar;
10778   enum machine_mode mode;
10779   HOST_WIDE_INT bitsize, bitpos;
10780   tree offset;
10781   int volatilep = 0, unsignedp = 0;
10782
10783   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
10784      it does not have a value (the offset into the common area), or if it
10785      is thread local (as opposed to global) then it isn't common, and shouldn't
10786      be handled as such.  */
10787   if (TREE_CODE (decl) != VAR_DECL
10788       || !TREE_PUBLIC (decl)
10789       || !TREE_STATIC (decl)
10790       || !DECL_HAS_VALUE_EXPR_P (decl)
10791       || !is_fortran ())
10792     return NULL_TREE;
10793
10794   val_expr = DECL_VALUE_EXPR (decl);
10795   if (TREE_CODE (val_expr) != COMPONENT_REF)
10796     return NULL_TREE;
10797
10798   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
10799                               &mode, &unsignedp, &volatilep, true);
10800
10801   if (cvar == NULL_TREE
10802       || TREE_CODE (cvar) != VAR_DECL
10803       || DECL_ARTIFICIAL (cvar)
10804       || !TREE_PUBLIC (cvar))
10805     return NULL_TREE;
10806
10807   *value = 0;
10808   if (offset != NULL)
10809     {
10810       if (!host_integerp (offset, 0))
10811         return NULL_TREE;
10812       *value = tree_low_cst (offset, 0);
10813     }
10814   if (bitpos != 0)
10815     *value += bitpos / BITS_PER_UNIT;
10816
10817   return cvar;
10818 }
10819
10820
10821 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10822    data attribute for a variable or a parameter.  We generate the
10823    DW_AT_const_value attribute only in those cases where the given variable
10824    or parameter does not have a true "location" either in memory or in a
10825    register.  This can happen (for example) when a constant is passed as an
10826    actual argument in a call to an inline function.  (It's possible that
10827    these things can crop up in other ways also.)  Note that one type of
10828    constant value which can be passed into an inlined function is a constant
10829    pointer.  This can happen for example if an actual argument in an inlined
10830    function call evaluates to a compile-time constant address.  */
10831
10832 static void
10833 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10834                                        enum dwarf_attribute attr)
10835 {
10836   rtx rtl;
10837   dw_loc_descr_ref descr;
10838   var_loc_list *loc_list;
10839   struct var_loc_node *node;
10840   if (TREE_CODE (decl) == ERROR_MARK)
10841     return;
10842
10843   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10844               || TREE_CODE (decl) == RESULT_DECL);
10845
10846   /* See if we possibly have multiple locations for this variable.  */
10847   loc_list = lookup_decl_loc (decl);
10848
10849   /* If it truly has multiple locations, the first and last node will
10850      differ.  */
10851   if (loc_list && loc_list->first != loc_list->last)
10852     {
10853       const char *endname, *secname;
10854       dw_loc_list_ref list;
10855       rtx varloc;
10856       enum var_init_status initialized;
10857
10858       /* Now that we know what section we are using for a base,
10859          actually construct the list of locations.
10860          The first location information is what is passed to the
10861          function that creates the location list, and the remaining
10862          locations just get added on to that list.
10863          Note that we only know the start address for a location
10864          (IE location changes), so to build the range, we use
10865          the range [current location start, next location start].
10866          This means we have to special case the last node, and generate
10867          a range of [last location start, end of function label].  */
10868
10869       node = loc_list->first;
10870       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10871       secname = secname_for_decl (decl);
10872
10873       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10874         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10875       else
10876         initialized = VAR_INIT_STATUS_INITIALIZED;
10877
10878       list = new_loc_list (loc_descriptor (varloc, initialized),
10879                            node->label, node->next->label, secname, 1);
10880       node = node->next;
10881
10882       for (; node->next; node = node->next)
10883         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10884           {
10885             /* The variable has a location between NODE->LABEL and
10886                NODE->NEXT->LABEL.  */
10887             enum var_init_status initialized =
10888               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10889             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10890             add_loc_descr_to_loc_list (&list, 
10891                                        loc_descriptor (varloc, initialized),
10892                                        node->label, node->next->label, secname);
10893           }
10894
10895       /* If the variable has a location at the last label
10896          it keeps its location until the end of function.  */
10897       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10898         {
10899           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10900           enum var_init_status initialized =
10901             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10902
10903           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10904           if (!current_function_decl)
10905             endname = text_end_label;
10906           else
10907             {
10908               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10909                                            current_function_funcdef_no);
10910               endname = ggc_strdup (label_id);
10911             }
10912           add_loc_descr_to_loc_list (&list, 
10913                                      loc_descriptor (varloc, initialized),
10914                                      node->label, endname, secname);
10915         }
10916
10917       /* Finally, add the location list to the DIE, and we are done.  */
10918       add_AT_loc_list (die, attr, list);
10919       return;
10920     }
10921
10922   /* Try to get some constant RTL for this decl, and use that as the value of
10923      the location.  */
10924
10925   rtl = rtl_for_decl_location (decl);
10926   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10927     {
10928       add_const_value_attribute (die, rtl);
10929       return;
10930     }
10931
10932   /* If we have tried to generate the location otherwise, and it
10933      didn't work out (we wouldn't be here if we did), and we have a one entry
10934      location list, try generating a location from that.  */
10935   if (loc_list && loc_list->first)
10936     {
10937       enum var_init_status status;
10938       node = loc_list->first;
10939       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10940       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10941       if (descr)
10942         {
10943           add_AT_location_description (die, attr, descr);
10944           return;
10945         }
10946     }
10947
10948   /* We couldn't get any rtl, so try directly generating the location
10949      description from the tree.  */
10950   descr = loc_descriptor_from_tree (decl);
10951   if (descr)
10952     {
10953       add_AT_location_description (die, attr, descr);
10954       return;
10955     }
10956   /* None of that worked, so it must not really have a location;
10957      try adding a constant value attribute from the DECL_INITIAL.  */
10958   tree_add_const_value_attribute (die, decl);
10959 }
10960
10961 /* If we don't have a copy of this variable in memory for some reason (such
10962    as a C++ member constant that doesn't have an out-of-line definition),
10963    we should tell the debugger about the constant value.  */
10964
10965 static void
10966 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10967 {
10968   tree init = DECL_INITIAL (decl);
10969   tree type = TREE_TYPE (decl);
10970   rtx rtl;
10971
10972   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10973     /* OK */;
10974   else
10975     return;
10976
10977   rtl = rtl_for_decl_init (init, type);
10978   if (rtl)
10979     add_const_value_attribute (var_die, rtl);
10980 }
10981
10982 /* Convert the CFI instructions for the current function into a
10983    location list.  This is used for DW_AT_frame_base when we targeting
10984    a dwarf2 consumer that does not support the dwarf3
10985    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10986    expressions.  */
10987
10988 static dw_loc_list_ref
10989 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10990 {
10991   dw_fde_ref fde;
10992   dw_loc_list_ref list, *list_tail;
10993   dw_cfi_ref cfi;
10994   dw_cfa_location last_cfa, next_cfa;
10995   const char *start_label, *last_label, *section;
10996
10997   fde = current_fde ();
10998   gcc_assert (fde != NULL);
10999
11000   section = secname_for_decl (current_function_decl);
11001   list_tail = &list;
11002   list = NULL;
11003
11004   next_cfa.reg = INVALID_REGNUM;
11005   next_cfa.offset = 0;
11006   next_cfa.indirect = 0;
11007   next_cfa.base_offset = 0;
11008
11009   start_label = fde->dw_fde_begin;
11010
11011   /* ??? Bald assumption that the CIE opcode list does not contain
11012      advance opcodes.  */
11013   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11014     lookup_cfa_1 (cfi, &next_cfa);
11015
11016   last_cfa = next_cfa;
11017   last_label = start_label;
11018
11019   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11020     switch (cfi->dw_cfi_opc)
11021       {
11022       case DW_CFA_set_loc:
11023       case DW_CFA_advance_loc1:
11024       case DW_CFA_advance_loc2:
11025       case DW_CFA_advance_loc4:
11026         if (!cfa_equal_p (&last_cfa, &next_cfa))
11027           {
11028             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11029                                        start_label, last_label, section,
11030                                        list == NULL);
11031
11032             list_tail = &(*list_tail)->dw_loc_next;
11033             last_cfa = next_cfa;
11034             start_label = last_label;
11035           }
11036         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11037         break;
11038
11039       case DW_CFA_advance_loc:
11040         /* The encoding is complex enough that we should never emit this.  */
11041       case DW_CFA_remember_state:
11042       case DW_CFA_restore_state:
11043         /* We don't handle these two in this function.  It would be possible
11044            if it were to be required.  */
11045         gcc_unreachable ();
11046
11047       default:
11048         lookup_cfa_1 (cfi, &next_cfa);
11049         break;
11050       }
11051
11052   if (!cfa_equal_p (&last_cfa, &next_cfa))
11053     {
11054       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11055                                  start_label, last_label, section,
11056                                  list == NULL);
11057       list_tail = &(*list_tail)->dw_loc_next;
11058       start_label = last_label;
11059     }
11060   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11061                              start_label, fde->dw_fde_end, section,
11062                              list == NULL);
11063
11064   return list;
11065 }
11066
11067 /* Compute a displacement from the "steady-state frame pointer" to the
11068    frame base (often the same as the CFA), and store it in
11069    frame_pointer_fb_offset.  OFFSET is added to the displacement
11070    before the latter is negated.  */
11071
11072 static void
11073 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11074 {
11075   rtx reg, elim;
11076
11077 #ifdef FRAME_POINTER_CFA_OFFSET
11078   reg = frame_pointer_rtx;
11079   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11080 #else
11081   reg = arg_pointer_rtx;
11082   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11083 #endif
11084
11085   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11086   if (GET_CODE (elim) == PLUS)
11087     {
11088       offset += INTVAL (XEXP (elim, 1));
11089       elim = XEXP (elim, 0);
11090     }
11091   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
11092                        : stack_pointer_rtx));
11093
11094   frame_pointer_fb_offset = -offset;
11095 }
11096
11097 /* Generate a DW_AT_name attribute given some string value to be included as
11098    the value of the attribute.  */
11099
11100 static void
11101 add_name_attribute (dw_die_ref die, const char *name_string)
11102 {
11103   if (name_string != NULL && *name_string != 0)
11104     {
11105       if (demangle_name_func)
11106         name_string = (*demangle_name_func) (name_string);
11107
11108       add_AT_string (die, DW_AT_name, name_string);
11109     }
11110 }
11111
11112 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11113
11114 static void
11115 add_comp_dir_attribute (dw_die_ref die)
11116 {
11117   const char *wd = get_src_pwd ();
11118   if (wd != NULL)
11119     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11120 }
11121
11122 /* Given a tree node describing an array bound (either lower or upper) output
11123    a representation for that bound.  */
11124
11125 static void
11126 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11127 {
11128   switch (TREE_CODE (bound))
11129     {
11130     case ERROR_MARK:
11131       return;
11132
11133     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11134     case INTEGER_CST:
11135       if (! host_integerp (bound, 0)
11136           || (bound_attr == DW_AT_lower_bound
11137               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11138                   || (is_fortran () && integer_onep (bound)))))
11139         /* Use the default.  */
11140         ;
11141       else
11142         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11143       break;
11144
11145     CASE_CONVERT:
11146     case VIEW_CONVERT_EXPR:
11147       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11148       break;
11149
11150     case SAVE_EXPR:
11151       break;
11152
11153     case VAR_DECL:
11154     case PARM_DECL:
11155     case RESULT_DECL:
11156       {
11157         dw_die_ref decl_die = lookup_decl_die (bound);
11158
11159         /* ??? Can this happen, or should the variable have been bound
11160            first?  Probably it can, since I imagine that we try to create
11161            the types of parameters in the order in which they exist in
11162            the list, and won't have created a forward reference to a
11163            later parameter.  */
11164         if (decl_die != NULL)
11165           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11166         break;
11167       }
11168
11169     default:
11170       {
11171         /* Otherwise try to create a stack operation procedure to
11172            evaluate the value of the array bound.  */
11173
11174         dw_die_ref ctx, decl_die;
11175         dw_loc_descr_ref loc;
11176
11177         loc = loc_descriptor_from_tree (bound);
11178         if (loc == NULL)
11179           break;
11180
11181         if (current_function_decl == 0)
11182           ctx = comp_unit_die;
11183         else
11184           ctx = lookup_decl_die (current_function_decl);
11185
11186         decl_die = new_die (DW_TAG_variable, ctx, bound);
11187         add_AT_flag (decl_die, DW_AT_artificial, 1);
11188         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11189         add_AT_loc (decl_die, DW_AT_location, loc);
11190
11191         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11192         break;
11193       }
11194     }
11195 }
11196
11197 /* Note that the block of subscript information for an array type also
11198    includes information about the element type of type given array type.  */
11199
11200 static void
11201 add_subscript_info (dw_die_ref type_die, tree type)
11202 {
11203 #ifndef MIPS_DEBUGGING_INFO
11204   unsigned dimension_number;
11205 #endif
11206   tree lower, upper;
11207   dw_die_ref subrange_die;
11208
11209   /* The GNU compilers represent multidimensional array types as sequences of
11210      one dimensional array types whose element types are themselves array
11211      types.  Here we squish that down, so that each multidimensional array
11212      type gets only one array_type DIE in the Dwarf debugging info. The draft
11213      Dwarf specification say that we are allowed to do this kind of
11214      compression in C (because there is no difference between an array or
11215      arrays and a multidimensional array in C) but for other source languages
11216      (e.g. Ada) we probably shouldn't do this.  */
11217
11218   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11219      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11220      We work around this by disabling this feature.  See also
11221      gen_array_type_die.  */
11222 #ifndef MIPS_DEBUGGING_INFO
11223   for (dimension_number = 0;
11224        TREE_CODE (type) == ARRAY_TYPE;
11225        type = TREE_TYPE (type), dimension_number++)
11226 #endif
11227     {
11228       tree domain = TYPE_DOMAIN (type);
11229
11230       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11231          and (in GNU C only) variable bounds.  Handle all three forms
11232          here.  */
11233       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11234       if (domain)
11235         {
11236           /* We have an array type with specified bounds.  */
11237           lower = TYPE_MIN_VALUE (domain);
11238           upper = TYPE_MAX_VALUE (domain);
11239
11240           /* Define the index type.  */
11241           if (TREE_TYPE (domain))
11242             {
11243               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11244                  TREE_TYPE field.  We can't emit debug info for this
11245                  because it is an unnamed integral type.  */
11246               if (TREE_CODE (domain) == INTEGER_TYPE
11247                   && TYPE_NAME (domain) == NULL_TREE
11248                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11249                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11250                 ;
11251               else
11252                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11253                                     type_die);
11254             }
11255
11256           /* ??? If upper is NULL, the array has unspecified length,
11257              but it does have a lower bound.  This happens with Fortran
11258                dimension arr(N:*)
11259              Since the debugger is definitely going to need to know N
11260              to produce useful results, go ahead and output the lower
11261              bound solo, and hope the debugger can cope.  */
11262
11263           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11264           if (upper)
11265             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11266         }
11267
11268       /* Otherwise we have an array type with an unspecified length.  The
11269          DWARF-2 spec does not say how to handle this; let's just leave out the
11270          bounds.  */
11271     }
11272 }
11273
11274 static void
11275 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11276 {
11277   unsigned size;
11278
11279   switch (TREE_CODE (tree_node))
11280     {
11281     case ERROR_MARK:
11282       size = 0;
11283       break;
11284     case ENUMERAL_TYPE:
11285     case RECORD_TYPE:
11286     case UNION_TYPE:
11287     case QUAL_UNION_TYPE:
11288       size = int_size_in_bytes (tree_node);
11289       break;
11290     case FIELD_DECL:
11291       /* For a data member of a struct or union, the DW_AT_byte_size is
11292          generally given as the number of bytes normally allocated for an
11293          object of the *declared* type of the member itself.  This is true
11294          even for bit-fields.  */
11295       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11296       break;
11297     default:
11298       gcc_unreachable ();
11299     }
11300
11301   /* Note that `size' might be -1 when we get to this point.  If it is, that
11302      indicates that the byte size of the entity in question is variable.  We
11303      have no good way of expressing this fact in Dwarf at the present time,
11304      so just let the -1 pass on through.  */
11305   add_AT_unsigned (die, DW_AT_byte_size, size);
11306 }
11307
11308 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11309    which specifies the distance in bits from the highest order bit of the
11310    "containing object" for the bit-field to the highest order bit of the
11311    bit-field itself.
11312
11313    For any given bit-field, the "containing object" is a hypothetical object
11314    (of some integral or enum type) within which the given bit-field lives.  The
11315    type of this hypothetical "containing object" is always the same as the
11316    declared type of the individual bit-field itself.  The determination of the
11317    exact location of the "containing object" for a bit-field is rather
11318    complicated.  It's handled by the `field_byte_offset' function (above).
11319
11320    Note that it is the size (in bytes) of the hypothetical "containing object"
11321    which will be given in the DW_AT_byte_size attribute for this bit-field.
11322    (See `byte_size_attribute' above).  */
11323
11324 static inline void
11325 add_bit_offset_attribute (dw_die_ref die, tree decl)
11326 {
11327   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11328   tree type = DECL_BIT_FIELD_TYPE (decl);
11329   HOST_WIDE_INT bitpos_int;
11330   HOST_WIDE_INT highest_order_object_bit_offset;
11331   HOST_WIDE_INT highest_order_field_bit_offset;
11332   HOST_WIDE_INT unsigned bit_offset;
11333
11334   /* Must be a field and a bit field.  */
11335   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11336
11337   /* We can't yet handle bit-fields whose offsets are variable, so if we
11338      encounter such things, just return without generating any attribute
11339      whatsoever.  Likewise for variable or too large size.  */
11340   if (! host_integerp (bit_position (decl), 0)
11341       || ! host_integerp (DECL_SIZE (decl), 1))
11342     return;
11343
11344   bitpos_int = int_bit_position (decl);
11345
11346   /* Note that the bit offset is always the distance (in bits) from the
11347      highest-order bit of the "containing object" to the highest-order bit of
11348      the bit-field itself.  Since the "high-order end" of any object or field
11349      is different on big-endian and little-endian machines, the computation
11350      below must take account of these differences.  */
11351   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11352   highest_order_field_bit_offset = bitpos_int;
11353
11354   if (! BYTES_BIG_ENDIAN)
11355     {
11356       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11357       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11358     }
11359
11360   bit_offset
11361     = (! BYTES_BIG_ENDIAN
11362        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11363        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11364
11365   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11366 }
11367
11368 /* For a FIELD_DECL node which represents a bit field, output an attribute
11369    which specifies the length in bits of the given field.  */
11370
11371 static inline void
11372 add_bit_size_attribute (dw_die_ref die, tree decl)
11373 {
11374   /* Must be a field and a bit field.  */
11375   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11376               && DECL_BIT_FIELD_TYPE (decl));
11377
11378   if (host_integerp (DECL_SIZE (decl), 1))
11379     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11380 }
11381
11382 /* If the compiled language is ANSI C, then add a 'prototyped'
11383    attribute, if arg types are given for the parameters of a function.  */
11384
11385 static inline void
11386 add_prototyped_attribute (dw_die_ref die, tree func_type)
11387 {
11388   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11389       && TYPE_ARG_TYPES (func_type) != NULL)
11390     add_AT_flag (die, DW_AT_prototyped, 1);
11391 }
11392
11393 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11394    by looking in either the type declaration or object declaration
11395    equate table.  */
11396
11397 static inline void
11398 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11399 {
11400   dw_die_ref origin_die = NULL;
11401
11402   if (TREE_CODE (origin) != FUNCTION_DECL)
11403     {
11404       /* We may have gotten separated from the block for the inlined
11405          function, if we're in an exception handler or some such; make
11406          sure that the abstract function has been written out.
11407
11408          Doing this for nested functions is wrong, however; functions are
11409          distinct units, and our context might not even be inline.  */
11410       tree fn = origin;
11411
11412       if (TYPE_P (fn))
11413         fn = TYPE_STUB_DECL (fn);
11414
11415       fn = decl_function_context (fn);
11416       if (fn)
11417         dwarf2out_abstract_function (fn);
11418     }
11419
11420   if (DECL_P (origin))
11421     origin_die = lookup_decl_die (origin);
11422   else if (TYPE_P (origin))
11423     origin_die = lookup_type_die (origin);
11424
11425   /* XXX: Functions that are never lowered don't always have correct block
11426      trees (in the case of java, they simply have no block tree, in some other
11427      languages).  For these functions, there is nothing we can really do to
11428      output correct debug info for inlined functions in all cases.  Rather
11429      than die, we'll just produce deficient debug info now, in that we will
11430      have variables without a proper abstract origin.  In the future, when all
11431      functions are lowered, we should re-add a gcc_assert (origin_die)
11432      here.  */
11433
11434   if (origin_die)
11435       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11436 }
11437
11438 /* We do not currently support the pure_virtual attribute.  */
11439
11440 static inline void
11441 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11442 {
11443   if (DECL_VINDEX (func_decl))
11444     {
11445       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11446
11447       if (host_integerp (DECL_VINDEX (func_decl), 0))
11448         add_AT_loc (die, DW_AT_vtable_elem_location,
11449                     new_loc_descr (DW_OP_constu,
11450                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11451                                    0));
11452
11453       /* GNU extension: Record what type this method came from originally.  */
11454       if (debug_info_level > DINFO_LEVEL_TERSE)
11455         add_AT_die_ref (die, DW_AT_containing_type,
11456                         lookup_type_die (DECL_CONTEXT (func_decl)));
11457     }
11458 }
11459 \f
11460 /* Add source coordinate attributes for the given decl.  */
11461
11462 static void
11463 add_src_coords_attributes (dw_die_ref die, tree decl)
11464 {
11465   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11466
11467   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11468   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11469 }
11470
11471 /* Add a DW_AT_name attribute and source coordinate attribute for the
11472    given decl, but only if it actually has a name.  */
11473
11474 static void
11475 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11476 {
11477   tree decl_name;
11478
11479   decl_name = DECL_NAME (decl);
11480   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11481     {
11482       add_name_attribute (die, dwarf2_name (decl, 0));
11483       if (! DECL_ARTIFICIAL (decl))
11484         add_src_coords_attributes (die, decl);
11485
11486       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11487           && TREE_PUBLIC (decl)
11488           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11489           && !DECL_ABSTRACT (decl)
11490           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11491           && !is_fortran ())
11492         add_AT_string (die, DW_AT_MIPS_linkage_name,
11493                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11494     }
11495
11496 #ifdef VMS_DEBUGGING_INFO
11497   /* Get the function's name, as described by its RTL.  This may be different
11498      from the DECL_NAME name used in the source file.  */
11499   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11500     {
11501       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11502                    XEXP (DECL_RTL (decl), 0));
11503       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11504     }
11505 #endif
11506 }
11507
11508 /* Push a new declaration scope.  */
11509
11510 static void
11511 push_decl_scope (tree scope)
11512 {
11513   VEC_safe_push (tree, gc, decl_scope_table, scope);
11514 }
11515
11516 /* Pop a declaration scope.  */
11517
11518 static inline void
11519 pop_decl_scope (void)
11520 {
11521   VEC_pop (tree, decl_scope_table);
11522 }
11523
11524 /* Return the DIE for the scope that immediately contains this type.
11525    Non-named types get global scope.  Named types nested in other
11526    types get their containing scope if it's open, or global scope
11527    otherwise.  All other types (i.e. function-local named types) get
11528    the current active scope.  */
11529
11530 static dw_die_ref
11531 scope_die_for (tree t, dw_die_ref context_die)
11532 {
11533   dw_die_ref scope_die = NULL;
11534   tree containing_scope;
11535   int i;
11536
11537   /* Non-types always go in the current scope.  */
11538   gcc_assert (TYPE_P (t));
11539
11540   containing_scope = TYPE_CONTEXT (t);
11541
11542   /* Use the containing namespace if it was passed in (for a declaration).  */
11543   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11544     {
11545       if (context_die == lookup_decl_die (containing_scope))
11546         /* OK */;
11547       else
11548         containing_scope = NULL_TREE;
11549     }
11550
11551   /* Ignore function type "scopes" from the C frontend.  They mean that
11552      a tagged type is local to a parmlist of a function declarator, but
11553      that isn't useful to DWARF.  */
11554   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11555     containing_scope = NULL_TREE;
11556
11557   if (containing_scope == NULL_TREE)
11558     scope_die = comp_unit_die;
11559   else if (TYPE_P (containing_scope))
11560     {
11561       /* For types, we can just look up the appropriate DIE.  But
11562          first we check to see if we're in the middle of emitting it
11563          so we know where the new DIE should go.  */
11564       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11565         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11566           break;
11567
11568       if (i < 0)
11569         {
11570           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11571                       || TREE_ASM_WRITTEN (containing_scope));
11572
11573           /* If none of the current dies are suitable, we get file scope.  */
11574           scope_die = comp_unit_die;
11575         }
11576       else
11577         scope_die = lookup_type_die (containing_scope);
11578     }
11579   else
11580     scope_die = context_die;
11581
11582   return scope_die;
11583 }
11584
11585 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11586
11587 static inline int
11588 local_scope_p (dw_die_ref context_die)
11589 {
11590   for (; context_die; context_die = context_die->die_parent)
11591     if (context_die->die_tag == DW_TAG_inlined_subroutine
11592         || context_die->die_tag == DW_TAG_subprogram)
11593       return 1;
11594
11595   return 0;
11596 }
11597
11598 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11599    whether or not to treat a DIE in this context as a declaration.  */
11600
11601 static inline int
11602 class_or_namespace_scope_p (dw_die_ref context_die)
11603 {
11604   return (context_die
11605           && (context_die->die_tag == DW_TAG_structure_type
11606               || context_die->die_tag == DW_TAG_class_type
11607               || context_die->die_tag == DW_TAG_interface_type
11608               || context_die->die_tag == DW_TAG_union_type
11609               || context_die->die_tag == DW_TAG_namespace));
11610 }
11611
11612 /* Many forms of DIEs require a "type description" attribute.  This
11613    routine locates the proper "type descriptor" die for the type given
11614    by 'type', and adds a DW_AT_type attribute below the given die.  */
11615
11616 static void
11617 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11618                     int decl_volatile, dw_die_ref context_die)
11619 {
11620   enum tree_code code  = TREE_CODE (type);
11621   dw_die_ref type_die  = NULL;
11622
11623   /* ??? If this type is an unnamed subrange type of an integral, floating-point
11624      or fixed-point type, use the inner type.  This is because we have no
11625      support for unnamed types in base_type_die.  This can happen if this is
11626      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11627   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11628       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11629     type = TREE_TYPE (type), code = TREE_CODE (type);
11630
11631   if (code == ERROR_MARK
11632       /* Handle a special case.  For functions whose return type is void, we
11633          generate *no* type attribute.  (Note that no object may have type
11634          `void', so this only applies to function return types).  */
11635       || code == VOID_TYPE)
11636     return;
11637
11638   type_die = modified_type_die (type,
11639                                 decl_const || TYPE_READONLY (type),
11640                                 decl_volatile || TYPE_VOLATILE (type),
11641                                 context_die);
11642
11643   if (type_die != NULL)
11644     add_AT_die_ref (object_die, DW_AT_type, type_die);
11645 }
11646
11647 /* Given an object die, add the calling convention attribute for the
11648    function call type.  */
11649 static void
11650 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11651 {
11652   enum dwarf_calling_convention value = DW_CC_normal;
11653
11654   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11655
11656   /* DWARF doesn't provide a way to identify a program's source-level
11657      entry point.  DW_AT_calling_convention attributes are only meant
11658      to describe functions' calling conventions.  However, lacking a
11659      better way to signal the Fortran main program, we use this for the
11660      time being, following existing custom.  */
11661   if (is_fortran ()
11662       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11663     value = DW_CC_program;
11664
11665   /* Only add the attribute if the backend requests it, and
11666      is not DW_CC_normal.  */
11667   if (value && (value != DW_CC_normal))
11668     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11669 }
11670
11671 /* Given a tree pointer to a struct, class, union, or enum type node, return
11672    a pointer to the (string) tag name for the given type, or zero if the type
11673    was declared without a tag.  */
11674
11675 static const char *
11676 type_tag (const_tree type)
11677 {
11678   const char *name = 0;
11679
11680   if (TYPE_NAME (type) != 0)
11681     {
11682       tree t = 0;
11683
11684       /* Find the IDENTIFIER_NODE for the type name.  */
11685       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11686         t = TYPE_NAME (type);
11687
11688       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11689          a TYPE_DECL node, regardless of whether or not a `typedef' was
11690          involved.  */
11691       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11692                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11693         {
11694           /* We want to be extra verbose.  Don't call dwarf_name if
11695              DECL_NAME isn't set.  The default hook for decl_printable_name
11696              doesn't like that, and in this context it's correct to return
11697              0, instead of "<anonymous>" or the like.  */
11698           if (DECL_NAME (TYPE_NAME (type)))
11699             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11700         }
11701
11702       /* Now get the name as a string, or invent one.  */
11703       if (!name && t != 0)
11704         name = IDENTIFIER_POINTER (t);
11705     }
11706
11707   return (name == 0 || *name == '\0') ? 0 : name;
11708 }
11709
11710 /* Return the type associated with a data member, make a special check
11711    for bit field types.  */
11712
11713 static inline tree
11714 member_declared_type (const_tree member)
11715 {
11716   return (DECL_BIT_FIELD_TYPE (member)
11717           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11718 }
11719
11720 /* Get the decl's label, as described by its RTL. This may be different
11721    from the DECL_NAME name used in the source file.  */
11722
11723 #if 0
11724 static const char *
11725 decl_start_label (tree decl)
11726 {
11727   rtx x;
11728   const char *fnname;
11729
11730   x = DECL_RTL (decl);
11731   gcc_assert (MEM_P (x));
11732
11733   x = XEXP (x, 0);
11734   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11735
11736   fnname = XSTR (x, 0);
11737   return fnname;
11738 }
11739 #endif
11740 \f
11741 /* These routines generate the internal representation of the DIE's for
11742    the compilation unit.  Debugging information is collected by walking
11743    the declaration trees passed in from dwarf2out_decl().  */
11744
11745 static void
11746 gen_array_type_die (tree type, dw_die_ref context_die)
11747 {
11748   dw_die_ref scope_die = scope_die_for (type, context_die);
11749   dw_die_ref array_die;
11750   tree element_type;
11751
11752   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11753      the inner array type comes before the outer array type.  Thus we must
11754      call gen_type_die before we call new_die.  See below also.  */
11755 #ifdef MIPS_DEBUGGING_INFO
11756   gen_type_die (TREE_TYPE (type), context_die);
11757 #endif
11758
11759   array_die = new_die (DW_TAG_array_type, scope_die, type);
11760   add_name_attribute (array_die, type_tag (type));
11761   equate_type_number_to_die (type, array_die);
11762
11763   if (TREE_CODE (type) == VECTOR_TYPE)
11764     {
11765       /* The frontend feeds us a representation for the vector as a struct
11766          containing an array.  Pull out the array type.  */
11767       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11768       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11769     }
11770
11771   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11772   if (is_fortran ()
11773       && TREE_CODE (type) == ARRAY_TYPE
11774       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11775     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11776
11777 #if 0
11778   /* We default the array ordering.  SDB will probably do
11779      the right things even if DW_AT_ordering is not present.  It's not even
11780      an issue until we start to get into multidimensional arrays anyway.  If
11781      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11782      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11783      and when we find out that we need to put these in, we will only do so
11784      for multidimensional arrays.  */
11785   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11786 #endif
11787
11788 #ifdef MIPS_DEBUGGING_INFO
11789   /* The SGI compilers handle arrays of unknown bound by setting
11790      AT_declaration and not emitting any subrange DIEs.  */
11791   if (! TYPE_DOMAIN (type))
11792     add_AT_flag (array_die, DW_AT_declaration, 1);
11793   else
11794 #endif
11795     add_subscript_info (array_die, type);
11796
11797   /* Add representation of the type of the elements of this array type.  */
11798   element_type = TREE_TYPE (type);
11799
11800   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11801      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11802      We work around this by disabling this feature.  See also
11803      add_subscript_info.  */
11804 #ifndef MIPS_DEBUGGING_INFO
11805   while (TREE_CODE (element_type) == ARRAY_TYPE)
11806     element_type = TREE_TYPE (element_type);
11807
11808   gen_type_die (element_type, context_die);
11809 #endif
11810
11811   add_type_attribute (array_die, element_type, 0, 0, context_die);
11812
11813   if (get_AT (array_die, DW_AT_name))
11814     add_pubtype (type, array_die);
11815 }
11816
11817 static dw_loc_descr_ref
11818 descr_info_loc (tree val, tree base_decl)
11819 {
11820   HOST_WIDE_INT size;
11821   dw_loc_descr_ref loc, loc2;
11822   enum dwarf_location_atom op;
11823
11824   if (val == base_decl)
11825     return new_loc_descr (DW_OP_push_object_address, 0, 0);
11826
11827   switch (TREE_CODE (val))
11828     {
11829     CASE_CONVERT:
11830       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11831     case INTEGER_CST:
11832       if (host_integerp (val, 0))
11833         return int_loc_descriptor (tree_low_cst (val, 0));
11834       break;
11835     case INDIRECT_REF:
11836       size = int_size_in_bytes (TREE_TYPE (val));
11837       if (size < 0)
11838         break;
11839       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11840       if (!loc)
11841         break;
11842       if (size == DWARF2_ADDR_SIZE)
11843         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11844       else
11845         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11846       return loc;
11847     case POINTER_PLUS_EXPR:
11848     case PLUS_EXPR:
11849       if (host_integerp (TREE_OPERAND (val, 1), 1)
11850           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11851              < 16384)
11852         {
11853           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11854           if (!loc)
11855             break;
11856           add_loc_descr (&loc,
11857                          new_loc_descr (DW_OP_plus_uconst,
11858                                         tree_low_cst (TREE_OPERAND (val, 1),
11859                                                       1), 0));
11860         }
11861       else
11862         {
11863           op = DW_OP_plus;
11864         do_binop:
11865           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11866           if (!loc)
11867             break;
11868           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11869           if (!loc2)
11870             break;
11871           add_loc_descr (&loc, loc2);
11872           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11873         }
11874       return loc;
11875     case MINUS_EXPR:
11876       op = DW_OP_minus;
11877       goto do_binop;
11878     case MULT_EXPR:
11879       op = DW_OP_mul;
11880       goto do_binop;
11881     case EQ_EXPR:
11882       op = DW_OP_eq;
11883       goto do_binop;
11884     case NE_EXPR:
11885       op = DW_OP_ne;
11886       goto do_binop;
11887     default:
11888       break;
11889     }
11890   return NULL;
11891 }
11892
11893 static void
11894 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11895                       tree val, tree base_decl)
11896 {
11897   dw_loc_descr_ref loc;
11898
11899   if (host_integerp (val, 0))
11900     {
11901       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11902       return;
11903     }
11904
11905   loc = descr_info_loc (val, base_decl);
11906   if (!loc)
11907     return;
11908
11909   add_AT_loc (die, attr, loc);
11910 }
11911
11912 /* This routine generates DIE for array with hidden descriptor, details
11913    are filled into *info by a langhook.  */
11914
11915 static void
11916 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11917                           dw_die_ref context_die)
11918 {
11919   dw_die_ref scope_die = scope_die_for (type, context_die);
11920   dw_die_ref array_die;
11921   int dim;
11922
11923   array_die = new_die (DW_TAG_array_type, scope_die, type);
11924   add_name_attribute (array_die, type_tag (type));
11925   equate_type_number_to_die (type, array_die);
11926
11927   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11928   if (is_fortran ()
11929       && info->ndimensions >= 2)
11930     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11931
11932   if (info->data_location)
11933     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11934                           info->base_decl);
11935   if (info->associated)
11936     add_descr_info_field (array_die, DW_AT_associated, info->associated,
11937                           info->base_decl);
11938   if (info->allocated)
11939     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11940                           info->base_decl);
11941
11942   for (dim = 0; dim < info->ndimensions; dim++)
11943     {
11944       dw_die_ref subrange_die
11945         = new_die (DW_TAG_subrange_type, array_die, NULL);
11946
11947       if (info->dimen[dim].lower_bound)
11948         {
11949           /* If it is the default value, omit it.  */
11950           if ((is_c_family () || is_java ())
11951               && integer_zerop (info->dimen[dim].lower_bound))
11952             ;
11953           else if (is_fortran ()
11954                    && integer_onep (info->dimen[dim].lower_bound))
11955             ;
11956           else
11957             add_descr_info_field (subrange_die, DW_AT_lower_bound,
11958                                   info->dimen[dim].lower_bound,
11959                                   info->base_decl);
11960         }
11961       if (info->dimen[dim].upper_bound)
11962         add_descr_info_field (subrange_die, DW_AT_upper_bound,
11963                               info->dimen[dim].upper_bound,
11964                               info->base_decl);
11965       if (info->dimen[dim].stride)
11966         add_descr_info_field (subrange_die, DW_AT_byte_stride,
11967                               info->dimen[dim].stride,
11968                               info->base_decl);
11969     }
11970
11971   gen_type_die (info->element_type, context_die);
11972   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11973
11974   if (get_AT (array_die, DW_AT_name))
11975     add_pubtype (type, array_die);
11976 }
11977
11978 #if 0
11979 static void
11980 gen_entry_point_die (tree decl, dw_die_ref context_die)
11981 {
11982   tree origin = decl_ultimate_origin (decl);
11983   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11984
11985   if (origin != NULL)
11986     add_abstract_origin_attribute (decl_die, origin);
11987   else
11988     {
11989       add_name_and_src_coords_attributes (decl_die, decl);
11990       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11991                           0, 0, context_die);
11992     }
11993
11994   if (DECL_ABSTRACT (decl))
11995     equate_decl_number_to_die (decl, decl_die);
11996   else
11997     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11998 }
11999 #endif
12000
12001 /* Walk through the list of incomplete types again, trying once more to
12002    emit full debugging info for them.  */
12003
12004 static void
12005 retry_incomplete_types (void)
12006 {
12007   int i;
12008
12009   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12010     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12011 }
12012
12013 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
12014
12015 static void
12016 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12017 {
12018   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12019
12020   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12021      be incomplete and such types are not marked.  */
12022   add_abstract_origin_attribute (type_die, type);
12023 }
12024
12025 /* Determine what tag to use for a record type.  */
12026
12027 static enum dwarf_tag
12028 record_type_tag (tree type)
12029 {
12030   if (! lang_hooks.types.classify_record)
12031     return DW_TAG_structure_type;
12032
12033   switch (lang_hooks.types.classify_record (type))
12034     {
12035     case RECORD_IS_STRUCT:
12036       return DW_TAG_structure_type;
12037
12038     case RECORD_IS_CLASS:
12039       return DW_TAG_class_type;
12040
12041     case RECORD_IS_INTERFACE:
12042       return DW_TAG_interface_type;
12043
12044     default:
12045       gcc_unreachable ();
12046     }
12047 }
12048
12049 /* Generate a DIE to represent an inlined instance of a structure type.  */
12050
12051 static void
12052 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12053 {
12054   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12055
12056   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12057      be incomplete and such types are not marked.  */
12058   add_abstract_origin_attribute (type_die, type);
12059 }
12060
12061 /* Generate a DIE to represent an inlined instance of a union type.  */
12062
12063 static void
12064 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12065 {
12066   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12067
12068   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12069      be incomplete and such types are not marked.  */
12070   add_abstract_origin_attribute (type_die, type);
12071 }
12072
12073 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
12074    include all of the information about the enumeration values also. Each
12075    enumerated type name/value is listed as a child of the enumerated type
12076    DIE.  */
12077
12078 static dw_die_ref
12079 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12080 {
12081   dw_die_ref type_die = lookup_type_die (type);
12082
12083   if (type_die == NULL)
12084     {
12085       type_die = new_die (DW_TAG_enumeration_type,
12086                           scope_die_for (type, context_die), type);
12087       equate_type_number_to_die (type, type_die);
12088       add_name_attribute (type_die, type_tag (type));
12089     }
12090   else if (! TYPE_SIZE (type))
12091     return type_die;
12092   else
12093     remove_AT (type_die, DW_AT_declaration);
12094
12095   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12096      given enum type is incomplete, do not generate the DW_AT_byte_size
12097      attribute or the DW_AT_element_list attribute.  */
12098   if (TYPE_SIZE (type))
12099     {
12100       tree link;
12101
12102       TREE_ASM_WRITTEN (type) = 1;
12103       add_byte_size_attribute (type_die, type);
12104       if (TYPE_STUB_DECL (type) != NULL_TREE)
12105         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12106
12107       /* If the first reference to this type was as the return type of an
12108          inline function, then it may not have a parent.  Fix this now.  */
12109       if (type_die->die_parent == NULL)
12110         add_child_die (scope_die_for (type, context_die), type_die);
12111
12112       for (link = TYPE_VALUES (type);
12113            link != NULL; link = TREE_CHAIN (link))
12114         {
12115           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12116           tree value = TREE_VALUE (link);
12117
12118           add_name_attribute (enum_die,
12119                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12120
12121           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12122             /* DWARF2 does not provide a way of indicating whether or
12123                not enumeration constants are signed or unsigned.  GDB
12124                always assumes the values are signed, so we output all
12125                values as if they were signed.  That means that
12126                enumeration constants with very large unsigned values
12127                will appear to have negative values in the debugger.  */
12128             add_AT_int (enum_die, DW_AT_const_value,
12129                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12130         }
12131     }
12132   else
12133     add_AT_flag (type_die, DW_AT_declaration, 1);
12134
12135   if (get_AT (type_die, DW_AT_name))
12136     add_pubtype (type, type_die);
12137
12138   return type_die;
12139 }
12140
12141 /* Generate a DIE to represent either a real live formal parameter decl or to
12142    represent just the type of some formal parameter position in some function
12143    type.
12144
12145    Note that this routine is a bit unusual because its argument may be a
12146    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12147    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12148    node.  If it's the former then this function is being called to output a
12149    DIE to represent a formal parameter object (or some inlining thereof).  If
12150    it's the latter, then this function is only being called to output a
12151    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12152    argument type of some subprogram type.  */
12153
12154 static dw_die_ref
12155 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12156 {
12157   dw_die_ref parm_die
12158     = new_die (DW_TAG_formal_parameter, context_die, node);
12159   tree origin;
12160
12161   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12162     {
12163     case tcc_declaration:
12164       origin = decl_ultimate_origin (node);
12165       if (origin != NULL)
12166         add_abstract_origin_attribute (parm_die, origin);
12167       else
12168         {
12169           tree type = TREE_TYPE (node);
12170           add_name_and_src_coords_attributes (parm_die, node);
12171           if (DECL_BY_REFERENCE (node))
12172             type = TREE_TYPE (type);
12173           add_type_attribute (parm_die, type,
12174                               TREE_READONLY (node),
12175                               TREE_THIS_VOLATILE (node),
12176                               context_die);
12177           if (DECL_ARTIFICIAL (node))
12178             add_AT_flag (parm_die, DW_AT_artificial, 1);
12179         }
12180
12181       equate_decl_number_to_die (node, parm_die);
12182       if (! DECL_ABSTRACT (node))
12183         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12184
12185       break;
12186
12187     case tcc_type:
12188       /* We were called with some kind of a ..._TYPE node.  */
12189       add_type_attribute (parm_die, node, 0, 0, context_die);
12190       break;
12191
12192     default:
12193       gcc_unreachable ();
12194     }
12195
12196   return parm_die;
12197 }
12198
12199 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12200    at the end of an (ANSI prototyped) formal parameters list.  */
12201
12202 static void
12203 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12204 {
12205   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12206 }
12207
12208 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12209    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12210    parameters as specified in some function type specification (except for
12211    those which appear as part of a function *definition*).  */
12212
12213 static void
12214 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12215 {
12216   tree link;
12217   tree formal_type = NULL;
12218   tree first_parm_type;
12219   tree arg;
12220
12221   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12222     {
12223       arg = DECL_ARGUMENTS (function_or_method_type);
12224       function_or_method_type = TREE_TYPE (function_or_method_type);
12225     }
12226   else
12227     arg = NULL_TREE;
12228
12229   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12230
12231   /* Make our first pass over the list of formal parameter types and output a
12232      DW_TAG_formal_parameter DIE for each one.  */
12233   for (link = first_parm_type; link; )
12234     {
12235       dw_die_ref parm_die;
12236
12237       formal_type = TREE_VALUE (link);
12238       if (formal_type == void_type_node)
12239         break;
12240
12241       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12242       parm_die = gen_formal_parameter_die (formal_type, context_die);
12243       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12244            && link == first_parm_type)
12245           || (arg && DECL_ARTIFICIAL (arg)))
12246         add_AT_flag (parm_die, DW_AT_artificial, 1);
12247
12248       link = TREE_CHAIN (link);
12249       if (arg)
12250         arg = TREE_CHAIN (arg);
12251     }
12252
12253   /* If this function type has an ellipsis, add a
12254      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12255   if (formal_type != void_type_node)
12256     gen_unspecified_parameters_die (function_or_method_type, context_die);
12257
12258   /* Make our second (and final) pass over the list of formal parameter types
12259      and output DIEs to represent those types (as necessary).  */
12260   for (link = TYPE_ARG_TYPES (function_or_method_type);
12261        link && TREE_VALUE (link);
12262        link = TREE_CHAIN (link))
12263     gen_type_die (TREE_VALUE (link), context_die);
12264 }
12265
12266 /* We want to generate the DIE for TYPE so that we can generate the
12267    die for MEMBER, which has been defined; we will need to refer back
12268    to the member declaration nested within TYPE.  If we're trying to
12269    generate minimal debug info for TYPE, processing TYPE won't do the
12270    trick; we need to attach the member declaration by hand.  */
12271
12272 static void
12273 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12274 {
12275   gen_type_die (type, context_die);
12276
12277   /* If we're trying to avoid duplicate debug info, we may not have
12278      emitted the member decl for this function.  Emit it now.  */
12279   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12280       && ! lookup_decl_die (member))
12281     {
12282       dw_die_ref type_die;
12283       gcc_assert (!decl_ultimate_origin (member));
12284
12285       push_decl_scope (type);
12286       type_die = lookup_type_die (type);
12287       if (TREE_CODE (member) == FUNCTION_DECL)
12288         gen_subprogram_die (member, type_die);
12289       else if (TREE_CODE (member) == FIELD_DECL)
12290         {
12291           /* Ignore the nameless fields that are used to skip bits but handle
12292              C++ anonymous unions and structs.  */
12293           if (DECL_NAME (member) != NULL_TREE
12294               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12295               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12296             {
12297               gen_type_die (member_declared_type (member), type_die);
12298               gen_field_die (member, type_die);
12299             }
12300         }
12301       else
12302         gen_variable_die (member, type_die);
12303
12304       pop_decl_scope ();
12305     }
12306 }
12307
12308 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12309    may later generate inlined and/or out-of-line instances of.  */
12310
12311 static void
12312 dwarf2out_abstract_function (tree decl)
12313 {
12314   dw_die_ref old_die;
12315   tree save_fn;
12316   tree context;
12317   int was_abstract = DECL_ABSTRACT (decl);
12318
12319   /* Make sure we have the actual abstract inline, not a clone.  */
12320   decl = DECL_ORIGIN (decl);
12321
12322   old_die = lookup_decl_die (decl);
12323   if (old_die && get_AT (old_die, DW_AT_inline))
12324     /* We've already generated the abstract instance.  */
12325     return;
12326
12327   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12328      we don't get confused by DECL_ABSTRACT.  */
12329   if (debug_info_level > DINFO_LEVEL_TERSE)
12330     {
12331       context = decl_class_context (decl);
12332       if (context)
12333         gen_type_die_for_member
12334           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12335     }
12336
12337   /* Pretend we've just finished compiling this function.  */
12338   save_fn = current_function_decl;
12339   current_function_decl = decl;
12340   push_cfun (DECL_STRUCT_FUNCTION (decl));
12341
12342   set_decl_abstract_flags (decl, 1);
12343   dwarf2out_decl (decl);
12344   if (! was_abstract)
12345     set_decl_abstract_flags (decl, 0);
12346
12347   current_function_decl = save_fn;
12348   pop_cfun ();
12349 }
12350
12351 /* Helper function of premark_used_types() which gets called through
12352    htab_traverse_resize().
12353
12354    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12355    marked as unused by prune_unused_types.  */
12356 static int
12357 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12358 {
12359   tree type;
12360   dw_die_ref die;
12361
12362   type = *slot;
12363   die = lookup_type_die (type);
12364   if (die != NULL)
12365     die->die_perennial_p = 1;
12366   return 1;
12367 }
12368
12369 /* Mark all members of used_types_hash as perennial.  */
12370 static void
12371 premark_used_types (void)
12372 {
12373   if (cfun && cfun->used_types_hash)
12374     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12375 }
12376
12377 /* Generate a DIE to represent a declared function (either file-scope or
12378    block-local).  */
12379
12380 static void
12381 gen_subprogram_die (tree decl, dw_die_ref context_die)
12382 {
12383   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12384   tree origin = decl_ultimate_origin (decl);
12385   dw_die_ref subr_die;
12386   tree fn_arg_types;
12387   tree outer_scope;
12388   dw_die_ref old_die = lookup_decl_die (decl);
12389   int declaration = (current_function_decl != decl
12390                      || class_or_namespace_scope_p (context_die));
12391
12392   premark_used_types ();
12393
12394   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12395      started to generate the abstract instance of an inline, decided to output
12396      its containing class, and proceeded to emit the declaration of the inline
12397      from the member list for the class.  If so, DECLARATION takes priority;
12398      we'll get back to the abstract instance when done with the class.  */
12399
12400   /* The class-scope declaration DIE must be the primary DIE.  */
12401   if (origin && declaration && class_or_namespace_scope_p (context_die))
12402     {
12403       origin = NULL;
12404       gcc_assert (!old_die);
12405     }
12406
12407   /* Now that the C++ front end lazily declares artificial member fns, we
12408      might need to retrofit the declaration into its class.  */
12409   if (!declaration && !origin && !old_die
12410       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12411       && !class_or_namespace_scope_p (context_die)
12412       && debug_info_level > DINFO_LEVEL_TERSE)
12413     old_die = force_decl_die (decl);
12414
12415   if (origin != NULL)
12416     {
12417       gcc_assert (!declaration || local_scope_p (context_die));
12418
12419       /* Fixup die_parent for the abstract instance of a nested
12420          inline function.  */
12421       if (old_die && old_die->die_parent == NULL)
12422         add_child_die (context_die, old_die);
12423
12424       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12425       add_abstract_origin_attribute (subr_die, origin);
12426     }
12427   else if (old_die)
12428     {
12429       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12430       struct dwarf_file_data * file_index = lookup_filename (s.file);
12431
12432       if (!get_AT_flag (old_die, DW_AT_declaration)
12433           /* We can have a normal definition following an inline one in the
12434              case of redefinition of GNU C extern inlines.
12435              It seems reasonable to use AT_specification in this case.  */
12436           && !get_AT (old_die, DW_AT_inline))
12437         {
12438           /* Detect and ignore this case, where we are trying to output
12439              something we have already output.  */
12440           return;
12441         }
12442
12443       /* If the definition comes from the same place as the declaration,
12444          maybe use the old DIE.  We always want the DIE for this function
12445          that has the *_pc attributes to be under comp_unit_die so the
12446          debugger can find it.  We also need to do this for abstract
12447          instances of inlines, since the spec requires the out-of-line copy
12448          to have the same parent.  For local class methods, this doesn't
12449          apply; we just use the old DIE.  */
12450       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12451           && (DECL_ARTIFICIAL (decl)
12452               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12453                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12454                       == (unsigned) s.line))))
12455         {
12456           subr_die = old_die;
12457
12458           /* Clear out the declaration attribute and the formal parameters.
12459              Do not remove all children, because it is possible that this
12460              declaration die was forced using force_decl_die(). In such
12461              cases die that forced declaration die (e.g. TAG_imported_module)
12462              is one of the children that we do not want to remove.  */
12463           remove_AT (subr_die, DW_AT_declaration);
12464           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12465         }
12466       else
12467         {
12468           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12469           add_AT_specification (subr_die, old_die);
12470           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12471             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12472           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12473             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12474         }
12475     }
12476   else
12477     {
12478       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12479
12480       if (TREE_PUBLIC (decl))
12481         add_AT_flag (subr_die, DW_AT_external, 1);
12482
12483       add_name_and_src_coords_attributes (subr_die, decl);
12484       if (debug_info_level > DINFO_LEVEL_TERSE)
12485         {
12486           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12487           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12488                               0, 0, context_die);
12489         }
12490
12491       add_pure_or_virtual_attribute (subr_die, decl);
12492       if (DECL_ARTIFICIAL (decl))
12493         add_AT_flag (subr_die, DW_AT_artificial, 1);
12494
12495       if (TREE_PROTECTED (decl))
12496         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12497       else if (TREE_PRIVATE (decl))
12498         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12499     }
12500
12501   if (declaration)
12502     {
12503       if (!old_die || !get_AT (old_die, DW_AT_inline))
12504         {
12505           add_AT_flag (subr_die, DW_AT_declaration, 1);
12506
12507           /* The first time we see a member function, it is in the context of
12508              the class to which it belongs.  We make sure of this by emitting
12509              the class first.  The next time is the definition, which is
12510              handled above.  The two may come from the same source text.
12511
12512              Note that force_decl_die() forces function declaration die. It is
12513              later reused to represent definition.  */
12514           equate_decl_number_to_die (decl, subr_die);
12515         }
12516     }
12517   else if (DECL_ABSTRACT (decl))
12518     {
12519       if (DECL_DECLARED_INLINE_P (decl))
12520         {
12521           if (cgraph_function_possibly_inlined_p (decl))
12522             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12523           else
12524             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12525         }
12526       else
12527         {
12528           if (cgraph_function_possibly_inlined_p (decl))
12529             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12530           else
12531             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12532         }
12533
12534       if (DECL_DECLARED_INLINE_P (decl)
12535           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12536         add_AT_flag (subr_die, DW_AT_artificial, 1);
12537
12538       equate_decl_number_to_die (decl, subr_die);
12539     }
12540   else if (!DECL_EXTERNAL (decl))
12541     {
12542       HOST_WIDE_INT cfa_fb_offset;
12543
12544       if (!old_die || !get_AT (old_die, DW_AT_inline))
12545         equate_decl_number_to_die (decl, subr_die);
12546
12547       if (!flag_reorder_blocks_and_partition)
12548         {
12549           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12550                                        current_function_funcdef_no);
12551           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12552           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12553                                        current_function_funcdef_no);
12554           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12555
12556           add_pubname (decl, subr_die);
12557           add_arange (decl, subr_die);
12558         }
12559       else
12560         {  /* Do nothing for now; maybe need to duplicate die, one for
12561               hot section and ond for cold section, then use the hot/cold
12562               section begin/end labels to generate the aranges...  */
12563           /*
12564             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12565             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12566             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12567             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12568
12569             add_pubname (decl, subr_die);
12570             add_arange (decl, subr_die);
12571             add_arange (decl, subr_die);
12572            */
12573         }
12574
12575 #ifdef MIPS_DEBUGGING_INFO
12576       /* Add a reference to the FDE for this routine.  */
12577       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12578 #endif
12579
12580       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12581
12582       /* We define the "frame base" as the function's CFA.  This is more
12583          convenient for several reasons: (1) It's stable across the prologue
12584          and epilogue, which makes it better than just a frame pointer,
12585          (2) With dwarf3, there exists a one-byte encoding that allows us
12586          to reference the .debug_frame data by proxy, but failing that,
12587          (3) We can at least reuse the code inspection and interpretation
12588          code that determines the CFA position at various points in the
12589          function.  */
12590       /* ??? Use some command-line or configury switch to enable the use
12591          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
12592          consumers that understand it; fall back to "pure" dwarf2 and
12593          convert the CFA data into a location list.  */
12594       {
12595         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12596         if (list->dw_loc_next)
12597           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12598         else
12599           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12600       }
12601
12602       /* Compute a displacement from the "steady-state frame pointer" to
12603          the CFA.  The former is what all stack slots and argument slots
12604          will reference in the rtl; the later is what we've told the
12605          debugger about.  We'll need to adjust all frame_base references
12606          by this displacement.  */
12607       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12608
12609       if (cfun->static_chain_decl)
12610         add_AT_location_description (subr_die, DW_AT_static_link,
12611                  loc_descriptor_from_tree (cfun->static_chain_decl));
12612     }
12613
12614   /* Now output descriptions of the arguments for this function. This gets
12615      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12616      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12617      `...' at the end of the formal parameter list.  In order to find out if
12618      there was a trailing ellipsis or not, we must instead look at the type
12619      associated with the FUNCTION_DECL.  This will be a node of type
12620      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12621      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12622      an ellipsis at the end.  */
12623
12624   /* In the case where we are describing a mere function declaration, all we
12625      need to do here (and all we *can* do here) is to describe the *types* of
12626      its formal parameters.  */
12627   if (debug_info_level <= DINFO_LEVEL_TERSE)
12628     ;
12629   else if (declaration)
12630     gen_formal_types_die (decl, subr_die);
12631   else
12632     {
12633       /* Generate DIEs to represent all known formal parameters.  */
12634       tree arg_decls = DECL_ARGUMENTS (decl);
12635       tree parm;
12636
12637       /* When generating DIEs, generate the unspecified_parameters DIE
12638          instead if we come across the arg "__builtin_va_alist" */
12639       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12640         if (TREE_CODE (parm) == PARM_DECL)
12641           {
12642             if (DECL_NAME (parm)
12643                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12644                             "__builtin_va_alist"))
12645               gen_unspecified_parameters_die (parm, subr_die);
12646             else
12647               gen_decl_die (parm, subr_die);
12648           }
12649
12650       /* Decide whether we need an unspecified_parameters DIE at the end.
12651          There are 2 more cases to do this for: 1) the ansi ... declaration -
12652          this is detectable when the end of the arg list is not a
12653          void_type_node 2) an unprototyped function declaration (not a
12654          definition).  This just means that we have no info about the
12655          parameters at all.  */
12656       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12657       if (fn_arg_types != NULL)
12658         {
12659           /* This is the prototyped case, check for....  */
12660           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12661             gen_unspecified_parameters_die (decl, subr_die);
12662         }
12663       else if (DECL_INITIAL (decl) == NULL_TREE)
12664         gen_unspecified_parameters_die (decl, subr_die);
12665     }
12666
12667   /* Output Dwarf info for all of the stuff within the body of the function
12668      (if it has one - it may be just a declaration).  */
12669   outer_scope = DECL_INITIAL (decl);
12670
12671   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12672      a function.  This BLOCK actually represents the outermost binding contour
12673      for the function, i.e. the contour in which the function's formal
12674      parameters and labels get declared. Curiously, it appears that the front
12675      end doesn't actually put the PARM_DECL nodes for the current function onto
12676      the BLOCK_VARS list for this outer scope, but are strung off of the
12677      DECL_ARGUMENTS list for the function instead.
12678
12679      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12680      the LABEL_DECL nodes for the function however, and we output DWARF info
12681      for those in decls_for_scope.  Just within the `outer_scope' there will be
12682      a BLOCK node representing the function's outermost pair of curly braces,
12683      and any blocks used for the base and member initializers of a C++
12684      constructor function.  */
12685   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12686     {
12687       /* Emit a DW_TAG_variable DIE for a named return value.  */
12688       if (DECL_NAME (DECL_RESULT (decl)))
12689         gen_decl_die (DECL_RESULT (decl), subr_die);
12690
12691       current_function_has_inlines = 0;
12692       decls_for_scope (outer_scope, subr_die, 0);
12693
12694 #if 0 && defined (MIPS_DEBUGGING_INFO)
12695       if (current_function_has_inlines)
12696         {
12697           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12698           if (! comp_unit_has_inlines)
12699             {
12700               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12701               comp_unit_has_inlines = 1;
12702             }
12703         }
12704 #endif
12705     }
12706   /* Add the calling convention attribute if requested.  */
12707   add_calling_convention_attribute (subr_die, decl);
12708
12709 }
12710
12711 /* Generate a DIE to represent a declared data object.  */
12712
12713 static void
12714 gen_variable_die (tree decl, dw_die_ref context_die)
12715 {
12716   HOST_WIDE_INT off;
12717   tree com_decl;
12718   dw_die_ref var_die;
12719   tree origin = decl_ultimate_origin (decl);
12720   dw_die_ref old_die = lookup_decl_die (decl);
12721   int declaration = (DECL_EXTERNAL (decl)
12722                      /* If DECL is COMDAT and has not actually been
12723                         emitted, we cannot take its address; there
12724                         might end up being no definition anywhere in
12725                         the program.  For example, consider the C++
12726                         test case:
12727
12728                           template <class T>
12729                           struct S { static const int i = 7; };
12730
12731                           template <class T>
12732                           const int S<T>::i;
12733
12734                           int f() { return S<int>::i; }
12735
12736                         Here, S<int>::i is not DECL_EXTERNAL, but no
12737                         definition is required, so the compiler will
12738                         not emit a definition.  */
12739                      || (TREE_CODE (decl) == VAR_DECL
12740                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12741                      || class_or_namespace_scope_p (context_die));
12742
12743   com_decl = fortran_common (decl, &off);
12744
12745   /* Symbol in common gets emitted as a child of the common block, in the form
12746      of a data member.
12747
12748      ??? This creates a new common block die for every common block symbol.
12749      Better to share same common block die for all symbols in that block.  */
12750   if (com_decl)
12751     {
12752       tree field;
12753       dw_die_ref com_die;
12754       const char *cnam = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
12755       dw_loc_descr_ref loc = loc_descriptor_from_tree (com_decl);
12756
12757       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
12758       var_die = new_die (DW_TAG_common_block, context_die, decl);
12759       add_name_and_src_coords_attributes (var_die, field);
12760       add_AT_flag (var_die, DW_AT_external, 1);
12761       add_AT_loc (var_die, DW_AT_location, loc);
12762       com_die = new_die (DW_TAG_member, var_die, decl);
12763       add_name_and_src_coords_attributes (com_die, decl);
12764       add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
12765                           TREE_THIS_VOLATILE (decl), context_die);
12766       add_AT_loc (com_die, DW_AT_data_member_location,
12767                   int_loc_descriptor (off));
12768       add_pubname_string (cnam, var_die); /* ??? needed? */
12769       return;
12770     }
12771
12772   var_die = new_die (DW_TAG_variable, context_die, decl);
12773
12774   if (origin != NULL)
12775     add_abstract_origin_attribute (var_die, origin);
12776
12777   /* Loop unrolling can create multiple blocks that refer to the same
12778      static variable, so we must test for the DW_AT_declaration flag.
12779
12780      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12781      copy decls and set the DECL_ABSTRACT flag on them instead of
12782      sharing them.
12783
12784      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12785
12786      ??? The declare_in_namespace support causes us to get two DIEs for one
12787      variable, both of which are declarations.  We want to avoid considering
12788      one to be a specification, so we must test that this DIE is not a
12789      declaration.  */
12790   else if (old_die && TREE_STATIC (decl) && ! declaration
12791            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12792     {
12793       /* This is a definition of a C++ class level static.  */
12794       add_AT_specification (var_die, old_die);
12795       if (DECL_NAME (decl))
12796         {
12797           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12798           struct dwarf_file_data * file_index = lookup_filename (s.file);
12799
12800           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12801             add_AT_file (var_die, DW_AT_decl_file, file_index);
12802
12803           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12804             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12805         }
12806     }
12807   else
12808     {
12809       tree type = TREE_TYPE (decl);
12810       if ((TREE_CODE (decl) == PARM_DECL
12811            || TREE_CODE (decl) == RESULT_DECL)
12812           && DECL_BY_REFERENCE (decl))
12813         type = TREE_TYPE (type);
12814
12815       add_name_and_src_coords_attributes (var_die, decl);
12816       add_type_attribute (var_die, type, TREE_READONLY (decl),
12817                           TREE_THIS_VOLATILE (decl), context_die);
12818
12819       if (TREE_PUBLIC (decl))
12820         add_AT_flag (var_die, DW_AT_external, 1);
12821
12822       if (DECL_ARTIFICIAL (decl))
12823         add_AT_flag (var_die, DW_AT_artificial, 1);
12824
12825       if (TREE_PROTECTED (decl))
12826         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12827       else if (TREE_PRIVATE (decl))
12828         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12829     }
12830
12831   if (declaration)
12832     add_AT_flag (var_die, DW_AT_declaration, 1);
12833
12834   if (DECL_ABSTRACT (decl) || declaration)
12835     equate_decl_number_to_die (decl, var_die);
12836
12837   if (! declaration && ! DECL_ABSTRACT (decl))
12838     {
12839       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12840       add_pubname (decl, var_die);
12841     }
12842   else
12843     tree_add_const_value_attribute (var_die, decl);
12844 }
12845
12846 /* Generate a DIE to represent a label identifier.  */
12847
12848 static void
12849 gen_label_die (tree decl, dw_die_ref context_die)
12850 {
12851   tree origin = decl_ultimate_origin (decl);
12852   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12853   rtx insn;
12854   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12855
12856   if (origin != NULL)
12857     add_abstract_origin_attribute (lbl_die, origin);
12858   else
12859     add_name_and_src_coords_attributes (lbl_die, decl);
12860
12861   if (DECL_ABSTRACT (decl))
12862     equate_decl_number_to_die (decl, lbl_die);
12863   else
12864     {
12865       insn = DECL_RTL_IF_SET (decl);
12866
12867       /* Deleted labels are programmer specified labels which have been
12868          eliminated because of various optimizations.  We still emit them
12869          here so that it is possible to put breakpoints on them.  */
12870       if (insn
12871           && (LABEL_P (insn)
12872               || ((NOTE_P (insn)
12873                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12874         {
12875           /* When optimization is enabled (via -O) some parts of the compiler
12876              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12877              represent source-level labels which were explicitly declared by
12878              the user.  This really shouldn't be happening though, so catch
12879              it if it ever does happen.  */
12880           gcc_assert (!INSN_DELETED_P (insn));
12881
12882           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12883           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12884         }
12885     }
12886 }
12887
12888 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12889    attributes to the DIE for a block STMT, to describe where the inlined
12890    function was called from.  This is similar to add_src_coords_attributes.  */
12891
12892 static inline void
12893 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12894 {
12895   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12896
12897   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12898   add_AT_unsigned (die, DW_AT_call_line, s.line);
12899 }
12900
12901
12902 /* If STMT's abstract origin is a function declaration and STMT's
12903    first subblock's abstract origin is the function's outermost block,
12904    then we're looking at the main entry point.  */
12905 static bool
12906 is_inlined_entry_point (const_tree stmt)
12907 {
12908   tree decl, block;
12909
12910   if (!stmt || TREE_CODE (stmt) != BLOCK)
12911     return false;
12912
12913   decl = block_ultimate_origin (stmt);
12914
12915   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12916     return false;
12917
12918   block = BLOCK_SUBBLOCKS (stmt);
12919
12920   if (block)
12921     {
12922       if (TREE_CODE (block) != BLOCK)
12923         return false;
12924
12925       block = block_ultimate_origin (block);
12926     }
12927
12928   return block == DECL_INITIAL (decl);
12929 }
12930
12931 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12932    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12933
12934 static inline void
12935 add_high_low_attributes (tree stmt, dw_die_ref die)
12936 {
12937   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12938
12939   if (BLOCK_FRAGMENT_CHAIN (stmt))
12940     {
12941       tree chain;
12942
12943       if (is_inlined_entry_point (stmt))
12944         {
12945           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12946                                        BLOCK_NUMBER (stmt));
12947           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12948         }
12949
12950       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12951
12952       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12953       do
12954         {
12955           add_ranges (chain);
12956           chain = BLOCK_FRAGMENT_CHAIN (chain);
12957         }
12958       while (chain);
12959       add_ranges (NULL);
12960     }
12961   else
12962     {
12963       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12964                                    BLOCK_NUMBER (stmt));
12965       add_AT_lbl_id (die, DW_AT_low_pc, label);
12966       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12967                                    BLOCK_NUMBER (stmt));
12968       add_AT_lbl_id (die, DW_AT_high_pc, label);
12969     }
12970 }
12971
12972 /* Generate a DIE for a lexical block.  */
12973
12974 static void
12975 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12976 {
12977   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12978
12979   if (! BLOCK_ABSTRACT (stmt))
12980     add_high_low_attributes (stmt, stmt_die);
12981
12982   decls_for_scope (stmt, stmt_die, depth);
12983 }
12984
12985 /* Generate a DIE for an inlined subprogram.  */
12986
12987 static void
12988 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12989 {
12990   tree decl = block_ultimate_origin (stmt);
12991
12992   /* Emit info for the abstract instance first, if we haven't yet.  We
12993      must emit this even if the block is abstract, otherwise when we
12994      emit the block below (or elsewhere), we may end up trying to emit
12995      a die whose origin die hasn't been emitted, and crashing.  */
12996   dwarf2out_abstract_function (decl);
12997
12998   if (! BLOCK_ABSTRACT (stmt))
12999     {
13000       dw_die_ref subr_die
13001         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13002
13003       add_abstract_origin_attribute (subr_die, decl);
13004       add_high_low_attributes (stmt, subr_die);
13005       add_call_src_coords_attributes (stmt, subr_die);
13006
13007       decls_for_scope (stmt, subr_die, depth);
13008       current_function_has_inlines = 1;
13009     }
13010   else
13011     /* We may get here if we're the outer block of function A that was
13012        inlined into function B that was inlined into function C.  When
13013        generating debugging info for C, dwarf2out_abstract_function(B)
13014        would mark all inlined blocks as abstract, including this one.
13015        So, we wouldn't (and shouldn't) expect labels to be generated
13016        for this one.  Instead, just emit debugging info for
13017        declarations within the block.  This is particularly important
13018        in the case of initializers of arguments passed from B to us:
13019        if they're statement expressions containing declarations, we
13020        wouldn't generate dies for their abstract variables, and then,
13021        when generating dies for the real variables, we'd die (pun
13022        intended :-)  */
13023     gen_lexical_block_die (stmt, context_die, depth);
13024 }
13025
13026 /* Generate a DIE for a field in a record, or structure.  */
13027
13028 static void
13029 gen_field_die (tree decl, dw_die_ref context_die)
13030 {
13031   dw_die_ref decl_die;
13032
13033   if (TREE_TYPE (decl) == error_mark_node)
13034     return;
13035
13036   decl_die = new_die (DW_TAG_member, context_die, decl);
13037   add_name_and_src_coords_attributes (decl_die, decl);
13038   add_type_attribute (decl_die, member_declared_type (decl),
13039                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13040                       context_die);
13041
13042   if (DECL_BIT_FIELD_TYPE (decl))
13043     {
13044       add_byte_size_attribute (decl_die, decl);
13045       add_bit_size_attribute (decl_die, decl);
13046       add_bit_offset_attribute (decl_die, decl);
13047     }
13048
13049   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13050     add_data_member_location_attribute (decl_die, decl);
13051
13052   if (DECL_ARTIFICIAL (decl))
13053     add_AT_flag (decl_die, DW_AT_artificial, 1);
13054
13055   if (TREE_PROTECTED (decl))
13056     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13057   else if (TREE_PRIVATE (decl))
13058     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13059
13060   /* Equate decl number to die, so that we can look up this decl later on.  */
13061   equate_decl_number_to_die (decl, decl_die);
13062 }
13063
13064 #if 0
13065 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13066    Use modified_type_die instead.
13067    We keep this code here just in case these types of DIEs may be needed to
13068    represent certain things in other languages (e.g. Pascal) someday.  */
13069
13070 static void
13071 gen_pointer_type_die (tree type, dw_die_ref context_die)
13072 {
13073   dw_die_ref ptr_die
13074     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13075
13076   equate_type_number_to_die (type, ptr_die);
13077   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13078   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13079 }
13080
13081 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13082    Use modified_type_die instead.
13083    We keep this code here just in case these types of DIEs may be needed to
13084    represent certain things in other languages (e.g. Pascal) someday.  */
13085
13086 static void
13087 gen_reference_type_die (tree type, dw_die_ref context_die)
13088 {
13089   dw_die_ref ref_die
13090     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13091
13092   equate_type_number_to_die (type, ref_die);
13093   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13094   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13095 }
13096 #endif
13097
13098 /* Generate a DIE for a pointer to a member type.  */
13099
13100 static void
13101 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13102 {
13103   dw_die_ref ptr_die
13104     = new_die (DW_TAG_ptr_to_member_type,
13105                scope_die_for (type, context_die), type);
13106
13107   equate_type_number_to_die (type, ptr_die);
13108   add_AT_die_ref (ptr_die, DW_AT_containing_type,
13109                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13110   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13111 }
13112
13113 /* Generate the DIE for the compilation unit.  */
13114
13115 static dw_die_ref
13116 gen_compile_unit_die (const char *filename)
13117 {
13118   dw_die_ref die;
13119   char producer[250];
13120   const char *language_string = lang_hooks.name;
13121   int language;
13122
13123   die = new_die (DW_TAG_compile_unit, NULL, NULL);
13124
13125   if (filename)
13126     {
13127       add_name_attribute (die, filename);
13128       /* Don't add cwd for <built-in>.  */
13129       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13130         add_comp_dir_attribute (die);
13131     }
13132
13133   sprintf (producer, "%s %s", language_string, version_string);
13134
13135 #ifdef MIPS_DEBUGGING_INFO
13136   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13137      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13138      not appear in the producer string, the debugger reaches the conclusion
13139      that the object file is stripped and has no debugging information.
13140      To get the MIPS/SGI debugger to believe that there is debugging
13141      information in the object file, we add a -g to the producer string.  */
13142   if (debug_info_level > DINFO_LEVEL_TERSE)
13143     strcat (producer, " -g");
13144 #endif
13145
13146   add_AT_string (die, DW_AT_producer, producer);
13147
13148   if (strcmp (language_string, "GNU C++") == 0)
13149     language = DW_LANG_C_plus_plus;
13150   else if (strcmp (language_string, "GNU Ada") == 0)
13151     language = DW_LANG_Ada95;
13152   else if (strcmp (language_string, "GNU F77") == 0)
13153     language = DW_LANG_Fortran77;
13154   else if (strcmp (language_string, "GNU Fortran") == 0)
13155     language = DW_LANG_Fortran95;
13156   else if (strcmp (language_string, "GNU Pascal") == 0)
13157     language = DW_LANG_Pascal83;
13158   else if (strcmp (language_string, "GNU Java") == 0)
13159     language = DW_LANG_Java;
13160   else if (strcmp (language_string, "GNU Objective-C") == 0)
13161     language = DW_LANG_ObjC;
13162   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13163     language = DW_LANG_ObjC_plus_plus;
13164   else
13165     language = DW_LANG_C89;
13166
13167   add_AT_unsigned (die, DW_AT_language, language);
13168   return die;
13169 }
13170
13171 /* Generate the DIE for a base class.  */
13172
13173 static void
13174 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13175 {
13176   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13177
13178   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13179   add_data_member_location_attribute (die, binfo);
13180
13181   if (BINFO_VIRTUAL_P (binfo))
13182     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13183
13184   if (access == access_public_node)
13185     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13186   else if (access == access_protected_node)
13187     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13188 }
13189
13190 /* Generate a DIE for a class member.  */
13191
13192 static void
13193 gen_member_die (tree type, dw_die_ref context_die)
13194 {
13195   tree member;
13196   tree binfo = TYPE_BINFO (type);
13197   dw_die_ref child;
13198
13199   /* If this is not an incomplete type, output descriptions of each of its
13200      members. Note that as we output the DIEs necessary to represent the
13201      members of this record or union type, we will also be trying to output
13202      DIEs to represent the *types* of those members. However the `type'
13203      function (above) will specifically avoid generating type DIEs for member
13204      types *within* the list of member DIEs for this (containing) type except
13205      for those types (of members) which are explicitly marked as also being
13206      members of this (containing) type themselves.  The g++ front- end can
13207      force any given type to be treated as a member of some other (containing)
13208      type by setting the TYPE_CONTEXT of the given (member) type to point to
13209      the TREE node representing the appropriate (containing) type.  */
13210
13211   /* First output info about the base classes.  */
13212   if (binfo)
13213     {
13214       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13215       int i;
13216       tree base;
13217
13218       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13219         gen_inheritance_die (base,
13220                              (accesses ? VEC_index (tree, accesses, i)
13221                               : access_public_node), context_die);
13222     }
13223
13224   /* Now output info about the data members and type members.  */
13225   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13226     {
13227       /* If we thought we were generating minimal debug info for TYPE
13228          and then changed our minds, some of the member declarations
13229          may have already been defined.  Don't define them again, but
13230          do put them in the right order.  */
13231
13232       child = lookup_decl_die (member);
13233       if (child)
13234         splice_child_die (context_die, child);
13235       else
13236         gen_decl_die (member, context_die);
13237     }
13238
13239   /* Now output info about the function members (if any).  */
13240   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13241     {
13242       /* Don't include clones in the member list.  */
13243       if (DECL_ABSTRACT_ORIGIN (member))
13244         continue;
13245
13246       child = lookup_decl_die (member);
13247       if (child)
13248         splice_child_die (context_die, child);
13249       else
13250         gen_decl_die (member, context_die);
13251     }
13252 }
13253
13254 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13255    is set, we pretend that the type was never defined, so we only get the
13256    member DIEs needed by later specification DIEs.  */
13257
13258 static void
13259 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13260                                 enum debug_info_usage usage)
13261 {
13262   dw_die_ref type_die = lookup_type_die (type);
13263   dw_die_ref scope_die = 0;
13264   int nested = 0;
13265   int complete = (TYPE_SIZE (type)
13266                   && (! TYPE_STUB_DECL (type)
13267                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13268   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13269   complete = complete && should_emit_struct_debug (type, usage);
13270
13271   if (type_die && ! complete)
13272     return;
13273
13274   if (TYPE_CONTEXT (type) != NULL_TREE
13275       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13276           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13277     nested = 1;
13278
13279   scope_die = scope_die_for (type, context_die);
13280
13281   if (! type_die || (nested && scope_die == comp_unit_die))
13282     /* First occurrence of type or toplevel definition of nested class.  */
13283     {
13284       dw_die_ref old_die = type_die;
13285
13286       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13287                           ? record_type_tag (type) : DW_TAG_union_type,
13288                           scope_die, type);
13289       equate_type_number_to_die (type, type_die);
13290       if (old_die)
13291         add_AT_specification (type_die, old_die);
13292       else
13293         add_name_attribute (type_die, type_tag (type));
13294     }
13295   else
13296     remove_AT (type_die, DW_AT_declaration);
13297
13298   /* If this type has been completed, then give it a byte_size attribute and
13299      then give a list of members.  */
13300   if (complete && !ns_decl)
13301     {
13302       /* Prevent infinite recursion in cases where the type of some member of
13303          this type is expressed in terms of this type itself.  */
13304       TREE_ASM_WRITTEN (type) = 1;
13305       add_byte_size_attribute (type_die, type);
13306       if (TYPE_STUB_DECL (type) != NULL_TREE)
13307         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13308
13309       /* If the first reference to this type was as the return type of an
13310          inline function, then it may not have a parent.  Fix this now.  */
13311       if (type_die->die_parent == NULL)
13312         add_child_die (scope_die, type_die);
13313
13314       push_decl_scope (type);
13315       gen_member_die (type, type_die);
13316       pop_decl_scope ();
13317
13318       /* GNU extension: Record what type our vtable lives in.  */
13319       if (TYPE_VFIELD (type))
13320         {
13321           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13322
13323           gen_type_die (vtype, context_die);
13324           add_AT_die_ref (type_die, DW_AT_containing_type,
13325                           lookup_type_die (vtype));
13326         }
13327     }
13328   else
13329     {
13330       add_AT_flag (type_die, DW_AT_declaration, 1);
13331
13332       /* We don't need to do this for function-local types.  */
13333       if (TYPE_STUB_DECL (type)
13334           && ! decl_function_context (TYPE_STUB_DECL (type)))
13335         VEC_safe_push (tree, gc, incomplete_types, type);
13336     }
13337
13338   if (get_AT (type_die, DW_AT_name))
13339     add_pubtype (type, type_die);
13340 }
13341
13342 /* Generate a DIE for a subroutine _type_.  */
13343
13344 static void
13345 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13346 {
13347   tree return_type = TREE_TYPE (type);
13348   dw_die_ref subr_die
13349     = new_die (DW_TAG_subroutine_type,
13350                scope_die_for (type, context_die), type);
13351
13352   equate_type_number_to_die (type, subr_die);
13353   add_prototyped_attribute (subr_die, type);
13354   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13355   gen_formal_types_die (type, subr_die);
13356
13357   if (get_AT (subr_die, DW_AT_name))
13358     add_pubtype (type, subr_die);
13359 }
13360
13361 /* Generate a DIE for a type definition.  */
13362
13363 static void
13364 gen_typedef_die (tree decl, dw_die_ref context_die)
13365 {
13366   dw_die_ref type_die;
13367   tree origin;
13368
13369   if (TREE_ASM_WRITTEN (decl))
13370     return;
13371
13372   TREE_ASM_WRITTEN (decl) = 1;
13373   type_die = new_die (DW_TAG_typedef, context_die, decl);
13374   origin = decl_ultimate_origin (decl);
13375   if (origin != NULL)
13376     add_abstract_origin_attribute (type_die, origin);
13377   else
13378     {
13379       tree type;
13380
13381       add_name_and_src_coords_attributes (type_die, decl);
13382       if (DECL_ORIGINAL_TYPE (decl))
13383         {
13384           type = DECL_ORIGINAL_TYPE (decl);
13385
13386           gcc_assert (type != TREE_TYPE (decl));
13387           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13388         }
13389       else
13390         type = TREE_TYPE (decl);
13391
13392       add_type_attribute (type_die, type, TREE_READONLY (decl),
13393                           TREE_THIS_VOLATILE (decl), context_die);
13394     }
13395
13396   if (DECL_ABSTRACT (decl))
13397     equate_decl_number_to_die (decl, type_die);
13398
13399   if (get_AT (type_die, DW_AT_name))
13400     add_pubtype (decl, type_die);
13401 }
13402
13403 /* Generate a type description DIE.  */
13404
13405 static void
13406 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13407                                 enum debug_info_usage usage)
13408 {
13409   int need_pop;
13410   struct array_descr_info info;
13411
13412   if (type == NULL_TREE || type == error_mark_node)
13413     return;
13414
13415   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13416       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13417     {
13418       if (TREE_ASM_WRITTEN (type))
13419         return;
13420
13421       /* Prevent broken recursion; we can't hand off to the same type.  */
13422       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13423
13424       TREE_ASM_WRITTEN (type) = 1;
13425       gen_decl_die (TYPE_NAME (type), context_die);
13426       return;
13427     }
13428
13429   /* If this is an array type with hidden descriptor, handle it first.  */
13430   if (!TREE_ASM_WRITTEN (type)
13431       && lang_hooks.types.get_array_descr_info
13432       && lang_hooks.types.get_array_descr_info (type, &info))
13433     {
13434       gen_descr_array_type_die (type, &info, context_die);
13435       TREE_ASM_WRITTEN (type) = 1;
13436       return;
13437     }
13438
13439   /* We are going to output a DIE to represent the unqualified version
13440      of this type (i.e. without any const or volatile qualifiers) so
13441      get the main variant (i.e. the unqualified version) of this type
13442      now.  (Vectors are special because the debugging info is in the
13443      cloned type itself).  */
13444   if (TREE_CODE (type) != VECTOR_TYPE)
13445     type = type_main_variant (type);
13446
13447   if (TREE_ASM_WRITTEN (type))
13448     return;
13449
13450   switch (TREE_CODE (type))
13451     {
13452     case ERROR_MARK:
13453       break;
13454
13455     case POINTER_TYPE:
13456     case REFERENCE_TYPE:
13457       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13458          ensures that the gen_type_die recursion will terminate even if the
13459          type is recursive.  Recursive types are possible in Ada.  */
13460       /* ??? We could perhaps do this for all types before the switch
13461          statement.  */
13462       TREE_ASM_WRITTEN (type) = 1;
13463
13464       /* For these types, all that is required is that we output a DIE (or a
13465          set of DIEs) to represent the "basis" type.  */
13466       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13467                                 DINFO_USAGE_IND_USE);
13468       break;
13469
13470     case OFFSET_TYPE:
13471       /* This code is used for C++ pointer-to-data-member types.
13472          Output a description of the relevant class type.  */
13473       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13474                                         DINFO_USAGE_IND_USE);
13475
13476       /* Output a description of the type of the object pointed to.  */
13477       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13478                                         DINFO_USAGE_IND_USE);
13479
13480       /* Now output a DIE to represent this pointer-to-data-member type
13481          itself.  */
13482       gen_ptr_to_mbr_type_die (type, context_die);
13483       break;
13484
13485     case FUNCTION_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 METHOD_TYPE:
13493       /* Force out return type (in case it wasn't forced out already).  */
13494       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13495                                         DINFO_USAGE_DIR_USE);
13496       gen_subroutine_type_die (type, context_die);
13497       break;
13498
13499     case ARRAY_TYPE:
13500       gen_array_type_die (type, context_die);
13501       break;
13502
13503     case VECTOR_TYPE:
13504       gen_array_type_die (type, context_die);
13505       break;
13506
13507     case ENUMERAL_TYPE:
13508     case RECORD_TYPE:
13509     case UNION_TYPE:
13510     case QUAL_UNION_TYPE:
13511       /* If this is a nested type whose containing class hasn't been written
13512          out yet, writing it out will cover this one, too.  This does not apply
13513          to instantiations of member class templates; they need to be added to
13514          the containing class as they are generated.  FIXME: This hurts the
13515          idea of combining type decls from multiple TUs, since we can't predict
13516          what set of template instantiations we'll get.  */
13517       if (TYPE_CONTEXT (type)
13518           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13519           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13520         {
13521           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13522
13523           if (TREE_ASM_WRITTEN (type))
13524             return;
13525
13526           /* If that failed, attach ourselves to the stub.  */
13527           push_decl_scope (TYPE_CONTEXT (type));
13528           context_die = lookup_type_die (TYPE_CONTEXT (type));
13529           need_pop = 1;
13530         }
13531       else
13532         {
13533           declare_in_namespace (type, context_die);
13534           need_pop = 0;
13535         }
13536
13537       if (TREE_CODE (type) == ENUMERAL_TYPE)
13538         {
13539           /* This might have been written out by the call to
13540              declare_in_namespace.  */
13541           if (!TREE_ASM_WRITTEN (type))
13542             gen_enumeration_type_die (type, context_die);
13543         }
13544       else
13545         gen_struct_or_union_type_die (type, context_die, usage);
13546
13547       if (need_pop)
13548         pop_decl_scope ();
13549
13550       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13551          it up if it is ever completed.  gen_*_type_die will set it for us
13552          when appropriate.  */
13553       return;
13554
13555     case VOID_TYPE:
13556     case INTEGER_TYPE:
13557     case REAL_TYPE:
13558     case FIXED_POINT_TYPE:
13559     case COMPLEX_TYPE:
13560     case BOOLEAN_TYPE:
13561       /* No DIEs needed for fundamental types.  */
13562       break;
13563
13564     case LANG_TYPE:
13565       /* No Dwarf representation currently defined.  */
13566       break;
13567
13568     default:
13569       gcc_unreachable ();
13570     }
13571
13572   TREE_ASM_WRITTEN (type) = 1;
13573 }
13574
13575 static void
13576 gen_type_die (tree type, dw_die_ref context_die)
13577 {
13578   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13579 }
13580
13581 /* Generate a DIE for a tagged type instantiation.  */
13582
13583 static void
13584 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13585 {
13586   if (type == NULL_TREE || type == error_mark_node)
13587     return;
13588
13589   /* We are going to output a DIE to represent the unqualified version of
13590      this type (i.e. without any const or volatile qualifiers) so make sure
13591      that we have the main variant (i.e. the unqualified version) of this
13592      type now.  */
13593   gcc_assert (type == type_main_variant (type));
13594
13595   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13596      an instance of an unresolved type.  */
13597
13598   switch (TREE_CODE (type))
13599     {
13600     case ERROR_MARK:
13601       break;
13602
13603     case ENUMERAL_TYPE:
13604       gen_inlined_enumeration_type_die (type, context_die);
13605       break;
13606
13607     case RECORD_TYPE:
13608       gen_inlined_structure_type_die (type, context_die);
13609       break;
13610
13611     case UNION_TYPE:
13612     case QUAL_UNION_TYPE:
13613       gen_inlined_union_type_die (type, context_die);
13614       break;
13615
13616     default:
13617       gcc_unreachable ();
13618     }
13619 }
13620
13621 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13622    things which are local to the given block.  */
13623
13624 static void
13625 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13626 {
13627   int must_output_die = 0;
13628   tree origin;
13629   tree decl;
13630   enum tree_code origin_code;
13631
13632   /* Ignore blocks that are NULL.  */
13633   if (stmt == NULL_TREE)
13634     return;
13635
13636   /* If the block is one fragment of a non-contiguous block, do not
13637      process the variables, since they will have been done by the
13638      origin block.  Do process subblocks.  */
13639   if (BLOCK_FRAGMENT_ORIGIN (stmt))
13640     {
13641       tree sub;
13642
13643       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13644         gen_block_die (sub, context_die, depth + 1);
13645
13646       return;
13647     }
13648
13649   /* Determine the "ultimate origin" of this block.  This block may be an
13650      inlined instance of an inlined instance of inline function, so we have
13651      to trace all of the way back through the origin chain to find out what
13652      sort of node actually served as the original seed for the creation of
13653      the current block.  */
13654   origin = block_ultimate_origin (stmt);
13655   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13656
13657   /* Determine if we need to output any Dwarf DIEs at all to represent this
13658      block.  */
13659   if (origin_code == FUNCTION_DECL)
13660     /* The outer scopes for inlinings *must* always be represented.  We
13661        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13662     must_output_die = 1;
13663   else
13664     {
13665       /* In the case where the current block represents an inlining of the
13666          "body block" of an inline function, we must *NOT* output any DIE for
13667          this block because we have already output a DIE to represent the whole
13668          inlined function scope and the "body block" of any function doesn't
13669          really represent a different scope according to ANSI C rules.  So we
13670          check here to make sure that this block does not represent a "body
13671          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13672       if (! is_body_block (origin ? origin : stmt))
13673         {
13674           /* Determine if this block directly contains any "significant"
13675              local declarations which we will need to output DIEs for.  */
13676           if (debug_info_level > DINFO_LEVEL_TERSE)
13677             /* We are not in terse mode so *any* local declaration counts
13678                as being a "significant" one.  */
13679             must_output_die = (BLOCK_VARS (stmt) != NULL
13680                                && (TREE_USED (stmt)
13681                                    || TREE_ASM_WRITTEN (stmt)
13682                                    || BLOCK_ABSTRACT (stmt)));
13683           else
13684             /* We are in terse mode, so only local (nested) function
13685                definitions count as "significant" local declarations.  */
13686             for (decl = BLOCK_VARS (stmt);
13687                  decl != NULL; decl = TREE_CHAIN (decl))
13688               if (TREE_CODE (decl) == FUNCTION_DECL
13689                   && DECL_INITIAL (decl))
13690                 {
13691                   must_output_die = 1;
13692                   break;
13693                 }
13694         }
13695     }
13696
13697   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13698      DIE for any block which contains no significant local declarations at
13699      all.  Rather, in such cases we just call `decls_for_scope' so that any
13700      needed Dwarf info for any sub-blocks will get properly generated. Note
13701      that in terse mode, our definition of what constitutes a "significant"
13702      local declaration gets restricted to include only inlined function
13703      instances and local (nested) function definitions.  */
13704   if (must_output_die)
13705     {
13706       if (origin_code == FUNCTION_DECL)
13707         gen_inlined_subroutine_die (stmt, context_die, depth);
13708       else
13709         gen_lexical_block_die (stmt, context_die, depth);
13710     }
13711   else
13712     decls_for_scope (stmt, context_die, depth);
13713 }
13714
13715 /* Generate all of the decls declared within a given scope and (recursively)
13716    all of its sub-blocks.  */
13717
13718 static void
13719 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13720 {
13721   tree decl;
13722   tree subblocks;
13723
13724   /* Ignore NULL blocks.  */
13725   if (stmt == NULL_TREE)
13726     return;
13727
13728   if (TREE_USED (stmt))
13729     {
13730       /* Output the DIEs to represent all of the data objects and typedefs
13731          declared directly within this block but not within any nested
13732          sub-blocks.  Also, nested function and tag DIEs have been
13733          generated with a parent of NULL; fix that up now.  */
13734       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13735         {
13736           dw_die_ref die;
13737
13738           if (TREE_CODE (decl) == FUNCTION_DECL)
13739             die = lookup_decl_die (decl);
13740           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13741             die = lookup_type_die (TREE_TYPE (decl));
13742           else
13743             die = NULL;
13744
13745           if (die != NULL && die->die_parent == NULL)
13746             add_child_die (context_die, die);
13747           /* Do not produce debug information for static variables since
13748              these might be optimized out.  We are called for these later
13749              in varpool_analyze_pending_decls.
13750
13751              But *do* produce it for Fortran COMMON variables because,
13752              even though they are static, their names can differ depending
13753              on the scope, which we need to preserve.  */
13754           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
13755               && !(is_fortran () && TREE_PUBLIC (decl)))
13756             ;
13757           else
13758             gen_decl_die (decl, context_die);
13759         }
13760     }
13761
13762   /* If we're at -g1, we're not interested in subblocks.  */
13763   if (debug_info_level <= DINFO_LEVEL_TERSE)
13764     return;
13765
13766   /* Output the DIEs to represent all sub-blocks (and the items declared
13767      therein) of this block.  */
13768   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13769        subblocks != NULL;
13770        subblocks = BLOCK_CHAIN (subblocks))
13771     gen_block_die (subblocks, context_die, depth + 1);
13772 }
13773
13774 /* Is this a typedef we can avoid emitting?  */
13775
13776 static inline int
13777 is_redundant_typedef (const_tree decl)
13778 {
13779   if (TYPE_DECL_IS_STUB (decl))
13780     return 1;
13781
13782   if (DECL_ARTIFICIAL (decl)
13783       && DECL_CONTEXT (decl)
13784       && is_tagged_type (DECL_CONTEXT (decl))
13785       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13786       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13787     /* Also ignore the artificial member typedef for the class name.  */
13788     return 1;
13789
13790   return 0;
13791 }
13792
13793 /* Returns the DIE for decl.  A DIE will always be returned.  */
13794
13795 static dw_die_ref
13796 force_decl_die (tree decl)
13797 {
13798   dw_die_ref decl_die;
13799   unsigned saved_external_flag;
13800   tree save_fn = NULL_TREE;
13801   decl_die = lookup_decl_die (decl);
13802   if (!decl_die)
13803     {
13804       dw_die_ref context_die;
13805       tree decl_context = DECL_CONTEXT (decl);
13806       if (decl_context)
13807         {
13808           /* Find die that represents this context.  */
13809           if (TYPE_P (decl_context))
13810             context_die = force_type_die (decl_context);
13811           else
13812             context_die = force_decl_die (decl_context);
13813         }
13814       else
13815         context_die = comp_unit_die;
13816
13817       decl_die = lookup_decl_die (decl);
13818       if (decl_die)
13819         return decl_die;
13820
13821       switch (TREE_CODE (decl))
13822         {
13823         case FUNCTION_DECL:
13824           /* Clear current_function_decl, so that gen_subprogram_die thinks
13825              that this is a declaration. At this point, we just want to force
13826              declaration die.  */
13827           save_fn = current_function_decl;
13828           current_function_decl = NULL_TREE;
13829           gen_subprogram_die (decl, context_die);
13830           current_function_decl = save_fn;
13831           break;
13832
13833         case VAR_DECL:
13834           /* Set external flag to force declaration die. Restore it after
13835            gen_decl_die() call.  */
13836           saved_external_flag = DECL_EXTERNAL (decl);
13837           DECL_EXTERNAL (decl) = 1;
13838           gen_decl_die (decl, context_die);
13839           DECL_EXTERNAL (decl) = saved_external_flag;
13840           break;
13841
13842         case NAMESPACE_DECL:
13843           dwarf2out_decl (decl);
13844           break;
13845
13846         default:
13847           gcc_unreachable ();
13848         }
13849
13850       /* We should be able to find the DIE now.  */
13851       if (!decl_die)
13852         decl_die = lookup_decl_die (decl);
13853       gcc_assert (decl_die);
13854     }
13855
13856   return decl_die;
13857 }
13858
13859 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13860    always returned.  */
13861
13862 static dw_die_ref
13863 force_type_die (tree type)
13864 {
13865   dw_die_ref type_die;
13866
13867   type_die = lookup_type_die (type);
13868   if (!type_die)
13869     {
13870       dw_die_ref context_die;
13871       if (TYPE_CONTEXT (type))
13872         {
13873           if (TYPE_P (TYPE_CONTEXT (type)))
13874             context_die = force_type_die (TYPE_CONTEXT (type));
13875           else
13876             context_die = force_decl_die (TYPE_CONTEXT (type));
13877         }
13878       else
13879         context_die = comp_unit_die;
13880
13881       type_die = modified_type_die (type, TYPE_READONLY (type),
13882                                     TYPE_VOLATILE (type), context_die);
13883       gcc_assert (type_die);
13884     }
13885   return type_die;
13886 }
13887
13888 /* Force out any required namespaces to be able to output DECL,
13889    and return the new context_die for it, if it's changed.  */
13890
13891 static dw_die_ref
13892 setup_namespace_context (tree thing, dw_die_ref context_die)
13893 {
13894   tree context = (DECL_P (thing)
13895                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13896   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13897     /* Force out the namespace.  */
13898     context_die = force_decl_die (context);
13899
13900   return context_die;
13901 }
13902
13903 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13904    type) within its namespace, if appropriate.
13905
13906    For compatibility with older debuggers, namespace DIEs only contain
13907    declarations; all definitions are emitted at CU scope.  */
13908
13909 static void
13910 declare_in_namespace (tree thing, dw_die_ref context_die)
13911 {
13912   dw_die_ref ns_context;
13913
13914   if (debug_info_level <= DINFO_LEVEL_TERSE)
13915     return;
13916
13917   /* If this decl is from an inlined function, then don't try to emit it in its
13918      namespace, as we will get confused.  It would have already been emitted
13919      when the abstract instance of the inline function was emitted anyways.  */
13920   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13921     return;
13922
13923   ns_context = setup_namespace_context (thing, context_die);
13924
13925   if (ns_context != context_die)
13926     {
13927       if (DECL_P (thing))
13928         gen_decl_die (thing, ns_context);
13929       else
13930         gen_type_die (thing, ns_context);
13931     }
13932 }
13933
13934 /* Generate a DIE for a namespace or namespace alias.  */
13935
13936 static void
13937 gen_namespace_die (tree decl)
13938 {
13939   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13940
13941   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13942      they are an alias of.  */
13943   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13944     {
13945       /* Output a real namespace.  */
13946       dw_die_ref namespace_die
13947         = new_die (DW_TAG_namespace, context_die, decl);
13948       add_name_and_src_coords_attributes (namespace_die, decl);
13949       equate_decl_number_to_die (decl, namespace_die);
13950     }
13951   else
13952     {
13953       /* Output a namespace alias.  */
13954
13955       /* Force out the namespace we are an alias of, if necessary.  */
13956       dw_die_ref origin_die
13957         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13958
13959       /* Now create the namespace alias DIE.  */
13960       dw_die_ref namespace_die
13961         = new_die (DW_TAG_imported_declaration, context_die, decl);
13962       add_name_and_src_coords_attributes (namespace_die, decl);
13963       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13964       equate_decl_number_to_die (decl, namespace_die);
13965     }
13966 }
13967
13968 /* Generate Dwarf debug information for a decl described by DECL.  */
13969
13970 static void
13971 gen_decl_die (tree decl, dw_die_ref context_die)
13972 {
13973   tree origin;
13974
13975   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13976     return;
13977
13978   switch (TREE_CODE (decl))
13979     {
13980     case ERROR_MARK:
13981       break;
13982
13983     case CONST_DECL:
13984       /* The individual enumerators of an enum type get output when we output
13985          the Dwarf representation of the relevant enum type itself.  */
13986       break;
13987
13988     case FUNCTION_DECL:
13989       /* Don't output any DIEs to represent mere function declarations,
13990          unless they are class members or explicit block externs.  */
13991       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13992           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13993         break;
13994
13995 #if 0
13996       /* FIXME */
13997       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13998          on local redeclarations of global functions.  That seems broken.  */
13999       if (current_function_decl != decl)
14000         /* This is only a declaration.  */;
14001 #endif
14002
14003       /* If we're emitting a clone, emit info for the abstract instance.  */
14004       if (DECL_ORIGIN (decl) != decl)
14005         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14006
14007       /* If we're emitting an out-of-line copy of an inline function,
14008          emit info for the abstract instance and set up to refer to it.  */
14009       else if (cgraph_function_possibly_inlined_p (decl)
14010                && ! DECL_ABSTRACT (decl)
14011                && ! class_or_namespace_scope_p (context_die)
14012                /* dwarf2out_abstract_function won't emit a die if this is just
14013                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
14014                   that case, because that works only if we have a die.  */
14015                && DECL_INITIAL (decl) != NULL_TREE)
14016         {
14017           dwarf2out_abstract_function (decl);
14018           set_decl_origin_self (decl);
14019         }
14020
14021       /* Otherwise we're emitting the primary DIE for this decl.  */
14022       else if (debug_info_level > DINFO_LEVEL_TERSE)
14023         {
14024           /* Before we describe the FUNCTION_DECL itself, make sure that we
14025              have described its return type.  */
14026           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14027
14028           /* And its virtual context.  */
14029           if (DECL_VINDEX (decl) != NULL_TREE)
14030             gen_type_die (DECL_CONTEXT (decl), context_die);
14031
14032           /* And its containing type.  */
14033           origin = decl_class_context (decl);
14034           if (origin != NULL_TREE)
14035             gen_type_die_for_member (origin, decl, context_die);
14036
14037           /* And its containing namespace.  */
14038           declare_in_namespace (decl, context_die);
14039         }
14040
14041       /* Now output a DIE to represent the function itself.  */
14042       gen_subprogram_die (decl, context_die);
14043       break;
14044
14045     case TYPE_DECL:
14046       /* If we are in terse mode, don't generate any DIEs to represent any
14047          actual typedefs.  */
14048       if (debug_info_level <= DINFO_LEVEL_TERSE)
14049         break;
14050
14051       /* In the special case of a TYPE_DECL node representing the declaration
14052          of some type tag, if the given TYPE_DECL is marked as having been
14053          instantiated from some other (original) TYPE_DECL node (e.g. one which
14054          was generated within the original definition of an inline function) we
14055          have to generate a special (abbreviated) DW_TAG_structure_type,
14056          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
14057       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14058           && is_tagged_type (TREE_TYPE (decl)))
14059         {
14060           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14061           break;
14062         }
14063
14064       if (is_redundant_typedef (decl))
14065         gen_type_die (TREE_TYPE (decl), context_die);
14066       else
14067         /* Output a DIE to represent the typedef itself.  */
14068         gen_typedef_die (decl, context_die);
14069       break;
14070
14071     case LABEL_DECL:
14072       if (debug_info_level >= DINFO_LEVEL_NORMAL)
14073         gen_label_die (decl, context_die);
14074       break;
14075
14076     case VAR_DECL:
14077     case RESULT_DECL:
14078       /* If we are in terse mode, don't generate any DIEs to represent any
14079          variable declarations or definitions.  */
14080       if (debug_info_level <= DINFO_LEVEL_TERSE)
14081         break;
14082
14083       /* If this is the global definition of the Fortran COMMON block, we don't
14084          need to do anything.  Syntactically, the block itself has no identity,
14085          just its constituent identifiers.  */
14086       if (TREE_CODE (decl) == VAR_DECL
14087           && TREE_PUBLIC (decl)
14088           && TREE_STATIC (decl)
14089           && is_fortran ()
14090           && !DECL_HAS_VALUE_EXPR_P (decl))
14091         break;
14092
14093       /* Output any DIEs that are needed to specify the type of this data
14094          object.  */
14095       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14096         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14097       else
14098         gen_type_die (TREE_TYPE (decl), context_die);
14099
14100       /* And its containing type.  */
14101       origin = decl_class_context (decl);
14102       if (origin != NULL_TREE)
14103         gen_type_die_for_member (origin, decl, context_die);
14104
14105       /* And its containing namespace.  */
14106       declare_in_namespace (decl, context_die);
14107
14108       /* Now output the DIE to represent the data object itself.  This gets
14109          complicated because of the possibility that the VAR_DECL really
14110          represents an inlined instance of a formal parameter for an inline
14111          function.  */
14112       origin = decl_ultimate_origin (decl);
14113       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14114         gen_formal_parameter_die (decl, context_die);
14115       else
14116         gen_variable_die (decl, context_die);
14117       break;
14118
14119     case FIELD_DECL:
14120       /* Ignore the nameless fields that are used to skip bits but handle C++
14121          anonymous unions and structs.  */
14122       if (DECL_NAME (decl) != NULL_TREE
14123           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14124           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14125         {
14126           gen_type_die (member_declared_type (decl), context_die);
14127           gen_field_die (decl, context_die);
14128         }
14129       break;
14130
14131     case PARM_DECL:
14132       if (DECL_BY_REFERENCE (decl))
14133         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14134       else
14135         gen_type_die (TREE_TYPE (decl), context_die);
14136       gen_formal_parameter_die (decl, context_die);
14137       break;
14138
14139     case NAMESPACE_DECL:
14140       gen_namespace_die (decl);
14141       break;
14142
14143     default:
14144       /* Probably some frontend-internal decl.  Assume we don't care.  */
14145       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14146       break;
14147     }
14148 }
14149 \f
14150 /* Output debug information for global decl DECL.  Called from toplev.c after
14151    compilation proper has finished.  */
14152
14153 static void
14154 dwarf2out_global_decl (tree decl)
14155 {
14156   /* Output DWARF2 information for file-scope tentative data object
14157      declarations, file-scope (extern) function declarations (which had no
14158      corresponding body) and file-scope tagged type declarations and
14159      definitions which have not yet been forced out.
14160
14161      Ignore the global decl of any Fortran COMMON blocks which also wind up here
14162      though they have already been described in the local scope for the 
14163      procedures using them.  */
14164   if (TREE_CODE (decl) == VAR_DECL
14165       && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14166     return;
14167
14168   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14169     dwarf2out_decl (decl);
14170 }
14171
14172 /* Output debug information for type decl DECL.  Called from toplev.c
14173    and from language front ends (to record built-in types).  */
14174 static void
14175 dwarf2out_type_decl (tree decl, int local)
14176 {
14177   if (!local)
14178     dwarf2out_decl (decl);
14179 }
14180
14181 /* Output debug information for imported module or decl.  */
14182
14183 static void
14184 dwarf2out_imported_module_or_decl (tree decl, tree context)
14185 {
14186   dw_die_ref imported_die, at_import_die;
14187   dw_die_ref scope_die;
14188   expanded_location xloc;
14189
14190   if (debug_info_level <= DINFO_LEVEL_TERSE)
14191     return;
14192
14193   gcc_assert (decl);
14194
14195   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14196      We need decl DIE for reference and scope die. First, get DIE for the decl
14197      itself.  */
14198
14199   /* Get the scope die for decl context. Use comp_unit_die for global module
14200      or decl. If die is not found for non globals, force new die.  */
14201   if (!context)
14202     scope_die = comp_unit_die;
14203   else if (TYPE_P (context))
14204     {
14205       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14206         return;
14207     scope_die = force_type_die (context);
14208     }
14209   else
14210     scope_die = force_decl_die (context);
14211
14212   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14213   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14214     {
14215       if (is_base_type (TREE_TYPE (decl)))
14216         at_import_die = base_type_die (TREE_TYPE (decl));
14217       else
14218         at_import_die = force_type_die (TREE_TYPE (decl));
14219     }
14220   else
14221     {
14222       at_import_die = lookup_decl_die (decl);
14223       if (!at_import_die)
14224         {
14225           /* If we're trying to avoid duplicate debug info, we may not have
14226              emitted the member decl for this field.  Emit it now.  */
14227           if (TREE_CODE (decl) == FIELD_DECL)
14228             {
14229               tree type = DECL_CONTEXT (decl);
14230               dw_die_ref type_context_die;
14231
14232               if (TYPE_CONTEXT (type))
14233                 if (TYPE_P (TYPE_CONTEXT (type)))
14234                   {
14235                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14236                                                    DINFO_USAGE_DIR_USE))
14237                       return;
14238                   type_context_die = force_type_die (TYPE_CONTEXT (type));
14239                   }
14240               else
14241                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14242               else
14243                 type_context_die = comp_unit_die;
14244               gen_type_die_for_member (type, decl, type_context_die);
14245             }
14246           at_import_die = force_decl_die (decl);
14247         }
14248     }
14249
14250   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14251   if (TREE_CODE (decl) == NAMESPACE_DECL)
14252     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14253   else
14254     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14255
14256   xloc = expand_location (input_location);
14257   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14258   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14259   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14260 }
14261
14262 /* Write the debugging output for DECL.  */
14263
14264 void
14265 dwarf2out_decl (tree decl)
14266 {
14267   dw_die_ref context_die = comp_unit_die;
14268
14269   switch (TREE_CODE (decl))
14270     {
14271     case ERROR_MARK:
14272       return;
14273
14274     case FUNCTION_DECL:
14275       /* What we would really like to do here is to filter out all mere
14276          file-scope declarations of file-scope functions which are never
14277          referenced later within this translation unit (and keep all of ones
14278          that *are* referenced later on) but we aren't clairvoyant, so we have
14279          no idea which functions will be referenced in the future (i.e. later
14280          on within the current translation unit). So here we just ignore all
14281          file-scope function declarations which are not also definitions.  If
14282          and when the debugger needs to know something about these functions,
14283          it will have to hunt around and find the DWARF information associated
14284          with the definition of the function.
14285
14286          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14287          nodes represent definitions and which ones represent mere
14288          declarations.  We have to check DECL_INITIAL instead. That's because
14289          the C front-end supports some weird semantics for "extern inline"
14290          function definitions.  These can get inlined within the current
14291          translation unit (and thus, we need to generate Dwarf info for their
14292          abstract instances so that the Dwarf info for the concrete inlined
14293          instances can have something to refer to) but the compiler never
14294          generates any out-of-lines instances of such things (despite the fact
14295          that they *are* definitions).
14296
14297          The important point is that the C front-end marks these "extern
14298          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14299          them anyway. Note that the C++ front-end also plays some similar games
14300          for inline function definitions appearing within include files which
14301          also contain `#pragma interface' pragmas.  */
14302       if (DECL_INITIAL (decl) == NULL_TREE)
14303         return;
14304
14305       /* If we're a nested function, initially use a parent of NULL; if we're
14306          a plain function, this will be fixed up in decls_for_scope.  If
14307          we're a method, it will be ignored, since we already have a DIE.  */
14308       if (decl_function_context (decl)
14309           /* But if we're in terse mode, we don't care about scope.  */
14310           && debug_info_level > DINFO_LEVEL_TERSE)
14311         context_die = NULL;
14312       break;
14313
14314     case VAR_DECL:
14315       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14316          declaration and if the declaration was never even referenced from
14317          within this entire compilation unit.  We suppress these DIEs in
14318          order to save space in the .debug section (by eliminating entries
14319          which are probably useless).  Note that we must not suppress
14320          block-local extern declarations (whether used or not) because that
14321          would screw-up the debugger's name lookup mechanism and cause it to
14322          miss things which really ought to be in scope at a given point.  */
14323       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14324         return;
14325
14326       /* For local statics lookup proper context die.  */
14327       if (TREE_STATIC (decl) && decl_function_context (decl))
14328         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14329
14330       /* If we are in terse mode, don't generate any DIEs to represent any
14331          variable declarations or definitions.  */
14332       if (debug_info_level <= DINFO_LEVEL_TERSE)
14333         return;
14334       break;
14335
14336     case NAMESPACE_DECL:
14337       if (debug_info_level <= DINFO_LEVEL_TERSE)
14338         return;
14339       if (lookup_decl_die (decl) != NULL)
14340         return;
14341       break;
14342
14343     case TYPE_DECL:
14344       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14345       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14346         return;
14347
14348       /* Don't bother trying to generate any DIEs to represent any of the
14349          normal built-in types for the language we are compiling.  */
14350       if (DECL_IS_BUILTIN (decl))
14351         {
14352           /* OK, we need to generate one for `bool' so GDB knows what type
14353              comparisons have.  */
14354           if (is_cxx ()
14355               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14356               && ! DECL_IGNORED_P (decl))
14357             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14358
14359           return;
14360         }
14361
14362       /* If we are in terse mode, don't generate any DIEs for types.  */
14363       if (debug_info_level <= DINFO_LEVEL_TERSE)
14364         return;
14365
14366       /* If we're a function-scope tag, initially use a parent of NULL;
14367          this will be fixed up in decls_for_scope.  */
14368       if (decl_function_context (decl))
14369         context_die = NULL;
14370
14371       break;
14372
14373     default:
14374       return;
14375     }
14376
14377   gen_decl_die (decl, context_die);
14378 }
14379
14380 /* Output a marker (i.e. a label) for the beginning of the generated code for
14381    a lexical block.  */
14382
14383 static void
14384 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14385                        unsigned int blocknum)
14386 {
14387   switch_to_section (current_function_section ());
14388   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14389 }
14390
14391 /* Output a marker (i.e. a label) for the end of the generated code for a
14392    lexical block.  */
14393
14394 static void
14395 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14396 {
14397   switch_to_section (current_function_section ());
14398   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14399 }
14400
14401 /* Returns nonzero if it is appropriate not to emit any debugging
14402    information for BLOCK, because it doesn't contain any instructions.
14403
14404    Don't allow this for blocks with nested functions or local classes
14405    as we would end up with orphans, and in the presence of scheduling
14406    we may end up calling them anyway.  */
14407
14408 static bool
14409 dwarf2out_ignore_block (const_tree block)
14410 {
14411   tree decl;
14412
14413   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14414     if (TREE_CODE (decl) == FUNCTION_DECL
14415         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14416       return 0;
14417
14418   return 1;
14419 }
14420
14421 /* Hash table routines for file_hash.  */
14422
14423 static int
14424 file_table_eq (const void *p1_p, const void *p2_p)
14425 {
14426   const struct dwarf_file_data * p1 = p1_p;
14427   const char * p2 = p2_p;
14428   return strcmp (p1->filename, p2) == 0;
14429 }
14430
14431 static hashval_t
14432 file_table_hash (const void *p_p)
14433 {
14434   const struct dwarf_file_data * p = p_p;
14435   return htab_hash_string (p->filename);
14436 }
14437
14438 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14439    dwarf2out.c) and return its "index".  The index of each (known) filename is
14440    just a unique number which is associated with only that one filename.  We
14441    need such numbers for the sake of generating labels (in the .debug_sfnames
14442    section) and references to those files numbers (in the .debug_srcinfo
14443    and.debug_macinfo sections).  If the filename given as an argument is not
14444    found in our current list, add it to the list and assign it the next
14445    available unique index number.  In order to speed up searches, we remember
14446    the index of the filename was looked up last.  This handles the majority of
14447    all searches.  */
14448
14449 static struct dwarf_file_data *
14450 lookup_filename (const char *file_name)
14451 {
14452   void ** slot;
14453   struct dwarf_file_data * created;
14454
14455   /* Check to see if the file name that was searched on the previous
14456      call matches this file name.  If so, return the index.  */
14457   if (file_table_last_lookup
14458       && (file_name == file_table_last_lookup->filename
14459           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14460     return file_table_last_lookup;
14461
14462   /* Didn't match the previous lookup, search the table.  */
14463   slot = htab_find_slot_with_hash (file_table, file_name,
14464                                    htab_hash_string (file_name), INSERT);
14465   if (*slot)
14466     return *slot;
14467
14468   created = ggc_alloc (sizeof (struct dwarf_file_data));
14469   created->filename = file_name;
14470   created->emitted_number = 0;
14471   *slot = created;
14472   return created;
14473 }
14474
14475 /* If the assembler will construct the file table, then translate the compiler
14476    internal file table number into the assembler file table number, and emit
14477    a .file directive if we haven't already emitted one yet.  The file table
14478    numbers are different because we prune debug info for unused variables and
14479    types, which may include filenames.  */
14480
14481 static int
14482 maybe_emit_file (struct dwarf_file_data * fd)
14483 {
14484   if (! fd->emitted_number)
14485     {
14486       if (last_emitted_file)
14487         fd->emitted_number = last_emitted_file->emitted_number + 1;
14488       else
14489         fd->emitted_number = 1;
14490       last_emitted_file = fd;
14491
14492       if (DWARF2_ASM_LINE_DEBUG_INFO)
14493         {
14494           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14495           output_quoted_string (asm_out_file,
14496                                 remap_debug_filename (fd->filename));
14497           fputc ('\n', asm_out_file);
14498         }
14499     }
14500
14501   return fd->emitted_number;
14502 }
14503
14504 /* Called by the final INSN scan whenever we see a var location.  We
14505    use it to drop labels in the right places, and throw the location in
14506    our lookup table.  */
14507
14508 static void
14509 dwarf2out_var_location (rtx loc_note)
14510 {
14511   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14512   struct var_loc_node *newloc;
14513   rtx prev_insn;
14514   static rtx last_insn;
14515   static const char *last_label;
14516   tree decl;
14517
14518   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14519     return;
14520   prev_insn = PREV_INSN (loc_note);
14521
14522   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14523   /* If the insn we processed last time is the previous insn
14524      and it is also a var location note, use the label we emitted
14525      last time.  */
14526   if (last_insn != NULL_RTX
14527       && last_insn == prev_insn
14528       && NOTE_P (prev_insn)
14529       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14530     {
14531       newloc->label = last_label;
14532     }
14533   else
14534     {
14535       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14536       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14537       loclabel_num++;
14538       newloc->label = ggc_strdup (loclabel);
14539     }
14540   newloc->var_loc_note = loc_note;
14541   newloc->next = NULL;
14542
14543   if (cfun && in_cold_section_p)
14544     newloc->section_label = crtl->subsections.cold_section_label;
14545   else
14546     newloc->section_label = text_section_label;
14547
14548   last_insn = loc_note;
14549   last_label = newloc->label;
14550   decl = NOTE_VAR_LOCATION_DECL (loc_note);
14551   add_var_loc_to_decl (decl, newloc);
14552 }
14553
14554 /* We need to reset the locations at the beginning of each
14555    function. We can't do this in the end_function hook, because the
14556    declarations that use the locations won't have been output when
14557    that hook is called.  Also compute have_multiple_function_sections here.  */
14558
14559 static void
14560 dwarf2out_begin_function (tree fun)
14561 {
14562   htab_empty (decl_loc_table);
14563
14564   if (function_section (fun) != text_section)
14565     have_multiple_function_sections = true;
14566
14567   dwarf2out_note_section_used ();
14568 }
14569
14570 /* Output a label to mark the beginning of a source code line entry
14571    and record information relating to this source line, in
14572    'line_info_table' for later output of the .debug_line section.  */
14573
14574 static void
14575 dwarf2out_source_line (unsigned int line, const char *filename)
14576 {
14577   if (debug_info_level >= DINFO_LEVEL_NORMAL
14578       && line != 0)
14579     {
14580       int file_num = maybe_emit_file (lookup_filename (filename));
14581
14582       switch_to_section (current_function_section ());
14583
14584       /* If requested, emit something human-readable.  */
14585       if (flag_debug_asm)
14586         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14587                  filename, line);
14588
14589       if (DWARF2_ASM_LINE_DEBUG_INFO)
14590         {
14591           /* Emit the .loc directive understood by GNU as.  */
14592           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14593
14594           /* Indicate that line number info exists.  */
14595           line_info_table_in_use++;
14596         }
14597       else if (function_section (current_function_decl) != text_section)
14598         {
14599           dw_separate_line_info_ref line_info;
14600           targetm.asm_out.internal_label (asm_out_file,
14601                                           SEPARATE_LINE_CODE_LABEL,
14602                                           separate_line_info_table_in_use);
14603
14604           /* Expand the line info table if necessary.  */
14605           if (separate_line_info_table_in_use
14606               == separate_line_info_table_allocated)
14607             {
14608               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14609               separate_line_info_table
14610                 = ggc_realloc (separate_line_info_table,
14611                                separate_line_info_table_allocated
14612                                * sizeof (dw_separate_line_info_entry));
14613               memset (separate_line_info_table
14614                        + separate_line_info_table_in_use,
14615                       0,
14616                       (LINE_INFO_TABLE_INCREMENT
14617                        * sizeof (dw_separate_line_info_entry)));
14618             }
14619
14620           /* Add the new entry at the end of the line_info_table.  */
14621           line_info
14622             = &separate_line_info_table[separate_line_info_table_in_use++];
14623           line_info->dw_file_num = file_num;
14624           line_info->dw_line_num = line;
14625           line_info->function = current_function_funcdef_no;
14626         }
14627       else
14628         {
14629           dw_line_info_ref line_info;
14630
14631           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14632                                      line_info_table_in_use);
14633
14634           /* Expand the line info table if necessary.  */
14635           if (line_info_table_in_use == line_info_table_allocated)
14636             {
14637               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14638               line_info_table
14639                 = ggc_realloc (line_info_table,
14640                                (line_info_table_allocated
14641                                 * sizeof (dw_line_info_entry)));
14642               memset (line_info_table + line_info_table_in_use, 0,
14643                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14644             }
14645
14646           /* Add the new entry at the end of the line_info_table.  */
14647           line_info = &line_info_table[line_info_table_in_use++];
14648           line_info->dw_file_num = file_num;
14649           line_info->dw_line_num = line;
14650         }
14651     }
14652 }
14653
14654 /* Record the beginning of a new source file.  */
14655
14656 static void
14657 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14658 {
14659   if (flag_eliminate_dwarf2_dups)
14660     {
14661       /* Record the beginning of the file for break_out_includes.  */
14662       dw_die_ref bincl_die;
14663
14664       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14665       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14666     }
14667
14668   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14669     {
14670       int file_num = maybe_emit_file (lookup_filename (filename));
14671
14672       switch_to_section (debug_macinfo_section);
14673       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14674       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14675                                    lineno);
14676
14677       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14678     }
14679 }
14680
14681 /* Record the end of a source file.  */
14682
14683 static void
14684 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14685 {
14686   if (flag_eliminate_dwarf2_dups)
14687     /* Record the end of the file for break_out_includes.  */
14688     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14689
14690   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14691     {
14692       switch_to_section (debug_macinfo_section);
14693       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14694     }
14695 }
14696
14697 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14698    the tail part of the directive line, i.e. the part which is past the
14699    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14700
14701 static void
14702 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14703                   const char *buffer ATTRIBUTE_UNUSED)
14704 {
14705   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14706     {
14707       switch_to_section (debug_macinfo_section);
14708       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14709       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14710       dw2_asm_output_nstring (buffer, -1, "The macro");
14711     }
14712 }
14713
14714 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14715    the tail part of the directive line, i.e. the part which is past the
14716    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14717
14718 static void
14719 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14720                  const char *buffer ATTRIBUTE_UNUSED)
14721 {
14722   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14723     {
14724       switch_to_section (debug_macinfo_section);
14725       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14726       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14727       dw2_asm_output_nstring (buffer, -1, "The macro");
14728     }
14729 }
14730
14731 /* Set up for Dwarf output at the start of compilation.  */
14732
14733 static void
14734 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14735 {
14736   /* Allocate the file_table.  */
14737   file_table = htab_create_ggc (50, file_table_hash,
14738                                 file_table_eq, NULL);
14739
14740   /* Allocate the decl_die_table.  */
14741   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14742                                     decl_die_table_eq, NULL);
14743
14744   /* Allocate the decl_loc_table.  */
14745   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14746                                     decl_loc_table_eq, NULL);
14747
14748   /* Allocate the initial hunk of the decl_scope_table.  */
14749   decl_scope_table = VEC_alloc (tree, gc, 256);
14750
14751   /* Allocate the initial hunk of the abbrev_die_table.  */
14752   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14753                                         * sizeof (dw_die_ref));
14754   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14755   /* Zero-th entry is allocated, but unused.  */
14756   abbrev_die_table_in_use = 1;
14757
14758   /* Allocate the initial hunk of the line_info_table.  */
14759   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14760                                        * sizeof (dw_line_info_entry));
14761   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14762
14763   /* Zero-th entry is allocated, but unused.  */
14764   line_info_table_in_use = 1;
14765
14766   /* Allocate the pubtypes and pubnames vectors.  */
14767   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14768   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14769
14770   /* Generate the initial DIE for the .debug section.  Note that the (string)
14771      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14772      will (typically) be a relative pathname and that this pathname should be
14773      taken as being relative to the directory from which the compiler was
14774      invoked when the given (base) source file was compiled.  We will fill
14775      in this value in dwarf2out_finish.  */
14776   comp_unit_die = gen_compile_unit_die (NULL);
14777
14778   incomplete_types = VEC_alloc (tree, gc, 64);
14779
14780   used_rtx_array = VEC_alloc (rtx, gc, 32);
14781
14782   debug_info_section = get_section (DEBUG_INFO_SECTION,
14783                                     SECTION_DEBUG, NULL);
14784   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14785                                       SECTION_DEBUG, NULL);
14786   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14787                                        SECTION_DEBUG, NULL);
14788   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14789                                        SECTION_DEBUG, NULL);
14790   debug_line_section = get_section (DEBUG_LINE_SECTION,
14791                                     SECTION_DEBUG, NULL);
14792   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14793                                    SECTION_DEBUG, NULL);
14794   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14795                                         SECTION_DEBUG, NULL);
14796 #ifdef DEBUG_PUBTYPES_SECTION
14797   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14798                                         SECTION_DEBUG, NULL);
14799 #endif
14800   debug_str_section = get_section (DEBUG_STR_SECTION,
14801                                    DEBUG_STR_SECTION_FLAGS, NULL);
14802   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14803                                       SECTION_DEBUG, NULL);
14804   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14805                                      SECTION_DEBUG, NULL);
14806
14807   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14808   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14809                                DEBUG_ABBREV_SECTION_LABEL, 0);
14810   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14811   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14812                                COLD_TEXT_SECTION_LABEL, 0);
14813   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14814
14815   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14816                                DEBUG_INFO_SECTION_LABEL, 0);
14817   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14818                                DEBUG_LINE_SECTION_LABEL, 0);
14819   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14820                                DEBUG_RANGES_SECTION_LABEL, 0);
14821   switch_to_section (debug_abbrev_section);
14822   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14823   switch_to_section (debug_info_section);
14824   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14825   switch_to_section (debug_line_section);
14826   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14827
14828   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14829     {
14830       switch_to_section (debug_macinfo_section);
14831       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14832                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14833       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14834     }
14835
14836   switch_to_section (text_section);
14837   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14838   if (flag_reorder_blocks_and_partition)
14839     {
14840       cold_text_section = unlikely_text_section ();
14841       switch_to_section (cold_text_section);
14842       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14843     }
14844 }
14845
14846 /* A helper function for dwarf2out_finish called through
14847    ht_forall.  Emit one queued .debug_str string.  */
14848
14849 static int
14850 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14851 {
14852   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14853
14854   if (node->form == DW_FORM_strp)
14855     {
14856       switch_to_section (debug_str_section);
14857       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14858       assemble_string (node->str, strlen (node->str) + 1);
14859     }
14860
14861   return 1;
14862 }
14863
14864 #if ENABLE_ASSERT_CHECKING
14865 /* Verify that all marks are clear.  */
14866
14867 static void
14868 verify_marks_clear (dw_die_ref die)
14869 {
14870   dw_die_ref c;
14871
14872   gcc_assert (! die->die_mark);
14873   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14874 }
14875 #endif /* ENABLE_ASSERT_CHECKING */
14876
14877 /* Clear the marks for a die and its children.
14878    Be cool if the mark isn't set.  */
14879
14880 static void
14881 prune_unmark_dies (dw_die_ref die)
14882 {
14883   dw_die_ref c;
14884
14885   if (die->die_mark)
14886     die->die_mark = 0;
14887   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14888 }
14889
14890 /* Given DIE that we're marking as used, find any other dies
14891    it references as attributes and mark them as used.  */
14892
14893 static void
14894 prune_unused_types_walk_attribs (dw_die_ref die)
14895 {
14896   dw_attr_ref a;
14897   unsigned ix;
14898
14899   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14900     {
14901       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14902         {
14903           /* A reference to another DIE.
14904              Make sure that it will get emitted.  */
14905           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14906         }
14907       /* Set the string's refcount to 0 so that prune_unused_types_mark
14908          accounts properly for it.  */
14909       if (AT_class (a) == dw_val_class_str)
14910         a->dw_attr_val.v.val_str->refcount = 0;
14911     }
14912 }
14913
14914
14915 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14916    to DIE's children.  */
14917
14918 static void
14919 prune_unused_types_mark (dw_die_ref die, int dokids)
14920 {
14921   dw_die_ref c;
14922
14923   if (die->die_mark == 0)
14924     {
14925       /* We haven't done this node yet.  Mark it as used.  */
14926       die->die_mark = 1;
14927
14928       /* We also have to mark its parents as used.
14929          (But we don't want to mark our parents' kids due to this.)  */
14930       if (die->die_parent)
14931         prune_unused_types_mark (die->die_parent, 0);
14932
14933       /* Mark any referenced nodes.  */
14934       prune_unused_types_walk_attribs (die);
14935
14936       /* If this node is a specification,
14937          also mark the definition, if it exists.  */
14938       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14939         prune_unused_types_mark (die->die_definition, 1);
14940     }
14941
14942   if (dokids && die->die_mark != 2)
14943     {
14944       /* We need to walk the children, but haven't done so yet.
14945          Remember that we've walked the kids.  */
14946       die->die_mark = 2;
14947
14948       /* If this is an array type, we need to make sure our
14949          kids get marked, even if they're types.  */
14950       if (die->die_tag == DW_TAG_array_type)
14951         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14952       else
14953         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14954     }
14955 }
14956
14957
14958 /* Walk the tree DIE and mark types that we actually use.  */
14959
14960 static void
14961 prune_unused_types_walk (dw_die_ref die)
14962 {
14963   dw_die_ref c;
14964
14965   /* Don't do anything if this node is already marked.  */
14966   if (die->die_mark)
14967     return;
14968
14969   switch (die->die_tag)
14970     {
14971     case DW_TAG_const_type:
14972     case DW_TAG_packed_type:
14973     case DW_TAG_pointer_type:
14974     case DW_TAG_reference_type:
14975     case DW_TAG_volatile_type:
14976     case DW_TAG_typedef:
14977     case DW_TAG_array_type:
14978     case DW_TAG_structure_type:
14979     case DW_TAG_union_type:
14980     case DW_TAG_class_type:
14981     case DW_TAG_interface_type:
14982     case DW_TAG_friend:
14983     case DW_TAG_variant_part:
14984     case DW_TAG_enumeration_type:
14985     case DW_TAG_subroutine_type:
14986     case DW_TAG_string_type:
14987     case DW_TAG_set_type:
14988     case DW_TAG_subrange_type:
14989     case DW_TAG_ptr_to_member_type:
14990     case DW_TAG_file_type:
14991       if (die->die_perennial_p)
14992         break;
14993
14994       /* It's a type node --- don't mark it.  */
14995       return;
14996
14997     default:
14998       /* Mark everything else.  */
14999       break;
15000   }
15001
15002   die->die_mark = 1;
15003
15004   /* Now, mark any dies referenced from here.  */
15005   prune_unused_types_walk_attribs (die);
15006
15007   /* Mark children.  */
15008   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15009 }
15010
15011 /* Increment the string counts on strings referred to from DIE's
15012    attributes.  */
15013
15014 static void
15015 prune_unused_types_update_strings (dw_die_ref die)
15016 {
15017   dw_attr_ref a;
15018   unsigned ix;
15019
15020   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15021     if (AT_class (a) == dw_val_class_str)
15022       {
15023         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15024         s->refcount++;
15025         /* Avoid unnecessarily putting strings that are used less than
15026            twice in the hash table.  */
15027         if (s->refcount
15028             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15029           {
15030             void ** slot;
15031             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15032                                              htab_hash_string (s->str),
15033                                              INSERT);
15034             gcc_assert (*slot == NULL);
15035             *slot = s;
15036           }
15037       }
15038 }
15039
15040 /* Remove from the tree DIE any dies that aren't marked.  */
15041
15042 static void
15043 prune_unused_types_prune (dw_die_ref die)
15044 {
15045   dw_die_ref c;
15046
15047   gcc_assert (die->die_mark);
15048   prune_unused_types_update_strings (die);
15049
15050   if (! die->die_child)
15051     return;
15052
15053   c = die->die_child;
15054   do {
15055     dw_die_ref prev = c;
15056     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15057       if (c == die->die_child)
15058         {
15059           /* No marked children between 'prev' and the end of the list.  */
15060           if (prev == c)
15061             /* No marked children at all.  */
15062             die->die_child = NULL;
15063           else
15064             {
15065               prev->die_sib = c->die_sib;
15066               die->die_child = prev;
15067             }
15068           return;
15069         }
15070
15071     if (c != prev->die_sib)
15072       prev->die_sib = c;
15073     prune_unused_types_prune (c);
15074   } while (c != die->die_child);
15075 }
15076
15077
15078 /* Remove dies representing declarations that we never use.  */
15079
15080 static void
15081 prune_unused_types (void)
15082 {
15083   unsigned int i;
15084   limbo_die_node *node;
15085   pubname_ref pub;
15086
15087 #if ENABLE_ASSERT_CHECKING
15088   /* All the marks should already be clear.  */
15089   verify_marks_clear (comp_unit_die);
15090   for (node = limbo_die_list; node; node = node->next)
15091     verify_marks_clear (node->die);
15092 #endif /* ENABLE_ASSERT_CHECKING */
15093
15094   /* Set the mark on nodes that are actually used.  */
15095   prune_unused_types_walk (comp_unit_die);
15096   for (node = limbo_die_list; node; node = node->next)
15097     prune_unused_types_walk (node->die);
15098
15099   /* Also set the mark on nodes referenced from the
15100      pubname_table or arange_table.  */
15101   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15102     prune_unused_types_mark (pub->die, 1);
15103   for (i = 0; i < arange_table_in_use; i++)
15104     prune_unused_types_mark (arange_table[i], 1);
15105
15106   /* Get rid of nodes that aren't marked; and update the string counts.  */
15107   if (debug_str_hash)
15108     htab_empty (debug_str_hash);
15109   prune_unused_types_prune (comp_unit_die);
15110   for (node = limbo_die_list; node; node = node->next)
15111     prune_unused_types_prune (node->die);
15112
15113   /* Leave the marks clear.  */
15114   prune_unmark_dies (comp_unit_die);
15115   for (node = limbo_die_list; node; node = node->next)
15116     prune_unmark_dies (node->die);
15117 }
15118
15119 /* Set the parameter to true if there are any relative pathnames in
15120    the file table.  */
15121 static int
15122 file_table_relative_p (void ** slot, void *param)
15123 {
15124   bool *p = param;
15125   struct dwarf_file_data *d = *slot;
15126   if (!IS_ABSOLUTE_PATH (d->filename))
15127     {
15128       *p = true;
15129       return 0;
15130     }
15131   return 1;
15132 }
15133
15134 /* Output stuff that dwarf requires at the end of every file,
15135    and generate the DWARF-2 debugging info.  */
15136
15137 static void
15138 dwarf2out_finish (const char *filename)
15139 {
15140   limbo_die_node *node, *next_node;
15141   dw_die_ref die = 0;
15142
15143   /* Add the name for the main input file now.  We delayed this from
15144      dwarf2out_init to avoid complications with PCH.  */
15145   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15146   if (!IS_ABSOLUTE_PATH (filename))
15147     add_comp_dir_attribute (comp_unit_die);
15148   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15149     {
15150       bool p = false;
15151       htab_traverse (file_table, file_table_relative_p, &p);
15152       if (p)
15153         add_comp_dir_attribute (comp_unit_die);
15154     }
15155
15156   /* Traverse the limbo die list, and add parent/child links.  The only
15157      dies without parents that should be here are concrete instances of
15158      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15159      For concrete instances, we can get the parent die from the abstract
15160      instance.  */
15161   for (node = limbo_die_list; node; node = next_node)
15162     {
15163       next_node = node->next;
15164       die = node->die;
15165
15166       if (die->die_parent == NULL)
15167         {
15168           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15169
15170           if (origin)
15171             add_child_die (origin->die_parent, die);
15172           else if (die == comp_unit_die)
15173             ;
15174           else if (errorcount > 0 || sorrycount > 0)
15175             /* It's OK to be confused by errors in the input.  */
15176             add_child_die (comp_unit_die, die);
15177           else
15178             {
15179               /* In certain situations, the lexical block containing a
15180                  nested function can be optimized away, which results
15181                  in the nested function die being orphaned.  Likewise
15182                  with the return type of that nested function.  Force
15183                  this to be a child of the containing function.
15184
15185                  It may happen that even the containing function got fully
15186                  inlined and optimized out.  In that case we are lost and
15187                  assign the empty child.  This should not be big issue as
15188                  the function is likely unreachable too.  */
15189               tree context = NULL_TREE;
15190
15191               gcc_assert (node->created_for);
15192
15193               if (DECL_P (node->created_for))
15194                 context = DECL_CONTEXT (node->created_for);
15195               else if (TYPE_P (node->created_for))
15196                 context = TYPE_CONTEXT (node->created_for);
15197
15198               gcc_assert (context
15199                           && (TREE_CODE (context) == FUNCTION_DECL
15200                               || TREE_CODE (context) == NAMESPACE_DECL));
15201
15202               origin = lookup_decl_die (context);
15203               if (origin)
15204                 add_child_die (origin, die);
15205               else
15206                 add_child_die (comp_unit_die, die);
15207             }
15208         }
15209     }
15210
15211   limbo_die_list = NULL;
15212
15213   /* Walk through the list of incomplete types again, trying once more to
15214      emit full debugging info for them.  */
15215   retry_incomplete_types ();
15216
15217   if (flag_eliminate_unused_debug_types)
15218     prune_unused_types ();
15219
15220   /* Generate separate CUs for each of the include files we've seen.
15221      They will go into limbo_die_list.  */
15222   if (flag_eliminate_dwarf2_dups)
15223     break_out_includes (comp_unit_die);
15224
15225   /* Traverse the DIE's and add add sibling attributes to those DIE's
15226      that have children.  */
15227   add_sibling_attributes (comp_unit_die);
15228   for (node = limbo_die_list; node; node = node->next)
15229     add_sibling_attributes (node->die);
15230
15231   /* Output a terminator label for the .text section.  */
15232   switch_to_section (text_section);
15233   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15234   if (flag_reorder_blocks_and_partition)
15235     {
15236       switch_to_section (unlikely_text_section ());
15237       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15238     }
15239
15240   /* We can only use the low/high_pc attributes if all of the code was
15241      in .text.  */
15242   if (!have_multiple_function_sections)
15243     {
15244       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15245       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15246     }
15247
15248   else
15249     {
15250       unsigned fde_idx = 0;
15251
15252       /* We need to give .debug_loc and .debug_ranges an appropriate
15253          "base address".  Use zero so that these addresses become
15254          absolute.  Historically, we've emitted the unexpected
15255          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15256          Emit both to give time for other tools to adapt.  */
15257       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15258       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15259
15260       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15261                          add_ranges_by_labels (text_section_label,
15262                                                text_end_label));
15263       if (flag_reorder_blocks_and_partition)
15264         add_ranges_by_labels (cold_text_section_label,
15265                               cold_end_label);
15266
15267       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15268         {
15269           dw_fde_ref fde = &fde_table[fde_idx];
15270
15271           if (fde->dw_fde_switched_sections)
15272             {
15273               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15274                                     fde->dw_fde_hot_section_end_label);
15275               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15276                                     fde->dw_fde_unlikely_section_end_label);
15277             }
15278           else
15279             add_ranges_by_labels (fde->dw_fde_begin,
15280                                   fde->dw_fde_end);
15281         }
15282
15283       add_ranges (NULL);
15284     }
15285
15286   /* Output location list section if necessary.  */
15287   if (have_location_lists)
15288     {
15289       /* Output the location lists info.  */
15290       switch_to_section (debug_loc_section);
15291       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15292                                    DEBUG_LOC_SECTION_LABEL, 0);
15293       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15294       output_location_lists (die);
15295     }
15296
15297   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15298     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15299                     debug_line_section_label);
15300
15301   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15302     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15303
15304   /* Output all of the compilation units.  We put the main one last so that
15305      the offsets are available to output_pubnames.  */
15306   for (node = limbo_die_list; node; node = node->next)
15307     output_comp_unit (node->die, 0);
15308
15309   output_comp_unit (comp_unit_die, 0);
15310
15311   /* Output the abbreviation table.  */
15312   switch_to_section (debug_abbrev_section);
15313   output_abbrev_section ();
15314
15315   /* Output public names table if necessary.  */
15316   if (!VEC_empty (pubname_entry, pubname_table))
15317     {
15318       switch_to_section (debug_pubnames_section);
15319       output_pubnames (pubname_table);
15320     }
15321
15322 #ifdef DEBUG_PUBTYPES_SECTION
15323   /* Output public types table if necessary.  */
15324   if (!VEC_empty (pubname_entry, pubtype_table))
15325     {
15326       switch_to_section (debug_pubtypes_section);
15327       output_pubnames (pubtype_table);
15328     }
15329 #endif
15330
15331   /* Output the address range information.  We only put functions in the arange
15332      table, so don't write it out if we don't have any.  */
15333   if (fde_table_in_use)
15334     {
15335       switch_to_section (debug_aranges_section);
15336       output_aranges ();
15337     }
15338
15339   /* Output ranges section if necessary.  */
15340   if (ranges_table_in_use)
15341     {
15342       switch_to_section (debug_ranges_section);
15343       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15344       output_ranges ();
15345     }
15346
15347   /* Output the source line correspondence table.  We must do this
15348      even if there is no line information.  Otherwise, on an empty
15349      translation unit, we will generate a present, but empty,
15350      .debug_info section.  IRIX 6.5 `nm' will then complain when
15351      examining the file.  This is done late so that any filenames
15352      used by the debug_info section are marked as 'used'.  */
15353   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15354     {
15355       switch_to_section (debug_line_section);
15356       output_line_info ();
15357     }
15358
15359   /* Have to end the macro section.  */
15360   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15361     {
15362       switch_to_section (debug_macinfo_section);
15363       dw2_asm_output_data (1, 0, "End compilation unit");
15364     }
15365
15366   /* If we emitted any DW_FORM_strp form attribute, output the string
15367      table too.  */
15368   if (debug_str_hash)
15369     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15370 }
15371 #else
15372
15373 /* This should never be used, but its address is needed for comparisons.  */
15374 const struct gcc_debug_hooks dwarf2_debug_hooks;
15375
15376 #endif /* DWARF2_DEBUGGING_INFO */
15377
15378 #include "gt-dwarf2out.h"