OSDN Git Service

2008-07-28 Richard Guenther <rguenther@suse.de>
[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 /* Get the current fde_table entry we should use.  */
319
320 static inline dw_fde_ref
321 current_fde (void)
322 {
323   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
324 }
325
326 /* A list of call frame insns for the CIE.  */
327 static GTY(()) dw_cfi_ref cie_cfi_head;
328
329 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
330 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
331    attribute that accelerates the lookup of the FDE associated
332    with the subprogram.  This variable holds the table index of the FDE
333    associated with the current function (body) definition.  */
334 static unsigned current_funcdef_fde;
335 #endif
336
337 struct indirect_string_node GTY(())
338 {
339   const char *str;
340   unsigned int refcount;
341   unsigned int form;
342   char *label;
343 };
344
345 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
346
347 static GTY(()) int dw2_string_counter;
348 static GTY(()) unsigned long dwarf2out_cfi_label_num;
349
350 /* True if the compilation unit places functions in more than one section.  */
351 static GTY(()) bool have_multiple_function_sections = false;
352
353 /* Whether the default text and cold text sections have been used at all.  */
354
355 static GTY(()) bool text_section_used = false;
356 static GTY(()) bool cold_text_section_used = false;
357
358 /* The default cold text section.  */
359 static GTY(()) section *cold_text_section;
360
361 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
362
363 /* Forward declarations for functions defined in this file.  */
364
365 static char *stripattributes (const char *);
366 static const char *dwarf_cfi_name (unsigned);
367 static dw_cfi_ref new_cfi (void);
368 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
369 static void add_fde_cfi (const char *, dw_cfi_ref);
370 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
371 static void lookup_cfa (dw_cfa_location *);
372 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
373 #ifdef DWARF2_UNWIND_INFO
374 static void initial_return_save (rtx);
375 #endif
376 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
377 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
378 static void output_call_frame_info (int);
379 static void dwarf2out_note_section_used (void);
380 static void dwarf2out_stack_adjust (rtx, bool);
381 static void flush_queued_reg_saves (void);
382 static bool clobbers_queued_reg_save (const_rtx);
383 static void dwarf2out_frame_debug_expr (rtx, const char *);
384
385 /* Support for complex CFA locations.  */
386 static void output_cfa_loc (dw_cfi_ref);
387 static void get_cfa_from_loc_descr (dw_cfa_location *,
388                                     struct dw_loc_descr_struct *);
389 static struct dw_loc_descr_struct *build_cfa_loc
390   (dw_cfa_location *, HOST_WIDE_INT);
391 static void def_cfa_1 (const char *, dw_cfa_location *);
392
393 /* How to start an assembler comment.  */
394 #ifndef ASM_COMMENT_START
395 #define ASM_COMMENT_START ";#"
396 #endif
397
398 /* Data and reference forms for relocatable data.  */
399 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
400 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
401
402 #ifndef DEBUG_FRAME_SECTION
403 #define DEBUG_FRAME_SECTION     ".debug_frame"
404 #endif
405
406 #ifndef FUNC_BEGIN_LABEL
407 #define FUNC_BEGIN_LABEL        "LFB"
408 #endif
409
410 #ifndef FUNC_END_LABEL
411 #define FUNC_END_LABEL          "LFE"
412 #endif
413
414 #ifndef FRAME_BEGIN_LABEL
415 #define FRAME_BEGIN_LABEL       "Lframe"
416 #endif
417 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
418 #define CIE_END_LABEL           "LECIE"
419 #define FDE_LABEL               "LSFDE"
420 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
421 #define FDE_END_LABEL           "LEFDE"
422 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
423 #define LINE_NUMBER_END_LABEL   "LELT"
424 #define LN_PROLOG_AS_LABEL      "LASLTP"
425 #define LN_PROLOG_END_LABEL     "LELTP"
426 #define DIE_LABEL_PREFIX        "DW"
427
428 /* The DWARF 2 CFA column which tracks the return address.  Normally this
429    is the column for PC, or the first column after all of the hard
430    registers.  */
431 #ifndef DWARF_FRAME_RETURN_COLUMN
432 #ifdef PC_REGNUM
433 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
434 #else
435 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
436 #endif
437 #endif
438
439 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
440    default, we just provide columns for all registers.  */
441 #ifndef DWARF_FRAME_REGNUM
442 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
443 #endif
444 \f
445 /* Hook used by __throw.  */
446
447 rtx
448 expand_builtin_dwarf_sp_column (void)
449 {
450   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
451   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
452 }
453
454 /* Return a pointer to a copy of the section string name S with all
455    attributes stripped off, and an asterisk prepended (for assemble_name).  */
456
457 static inline char *
458 stripattributes (const char *s)
459 {
460   char *stripped = XNEWVEC (char, strlen (s) + 2);
461   char *p = stripped;
462
463   *p++ = '*';
464
465   while (*s && *s != ',')
466     *p++ = *s++;
467
468   *p = '\0';
469   return stripped;
470 }
471
472 /* MEM is a memory reference for the register size table, each element of
473    which has mode MODE.  Initialize column C as a return address column.  */
474
475 static void
476 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
477 {
478   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
479   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
480   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
481 }
482
483 /* Generate code to initialize the register size table.  */
484
485 void
486 expand_builtin_init_dwarf_reg_sizes (tree address)
487 {
488   unsigned int i;
489   enum machine_mode mode = TYPE_MODE (char_type_node);
490   rtx addr = expand_normal (address);
491   rtx mem = gen_rtx_MEM (BLKmode, addr);
492   bool wrote_return_column = false;
493
494   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
495     {
496       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
497
498       if (rnum < DWARF_FRAME_REGISTERS)
499         {
500           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
501           enum machine_mode save_mode = reg_raw_mode[i];
502           HOST_WIDE_INT size;
503
504           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
505             save_mode = choose_hard_reg_mode (i, 1, true);
506           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
507             {
508               if (save_mode == VOIDmode)
509                 continue;
510               wrote_return_column = true;
511             }
512           size = GET_MODE_SIZE (save_mode);
513           if (offset < 0)
514             continue;
515
516           emit_move_insn (adjust_address (mem, mode, offset),
517                           gen_int_mode (size, mode));
518         }
519     }
520
521   if (!wrote_return_column)
522     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
523
524 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
525   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
526 #endif
527
528   targetm.init_dwarf_reg_sizes_extra (address);
529 }
530
531 /* Convert a DWARF call frame info. operation to its string name */
532
533 static const char *
534 dwarf_cfi_name (unsigned int cfi_opc)
535 {
536   switch (cfi_opc)
537     {
538     case DW_CFA_advance_loc:
539       return "DW_CFA_advance_loc";
540     case DW_CFA_offset:
541       return "DW_CFA_offset";
542     case DW_CFA_restore:
543       return "DW_CFA_restore";
544     case DW_CFA_nop:
545       return "DW_CFA_nop";
546     case DW_CFA_set_loc:
547       return "DW_CFA_set_loc";
548     case DW_CFA_advance_loc1:
549       return "DW_CFA_advance_loc1";
550     case DW_CFA_advance_loc2:
551       return "DW_CFA_advance_loc2";
552     case DW_CFA_advance_loc4:
553       return "DW_CFA_advance_loc4";
554     case DW_CFA_offset_extended:
555       return "DW_CFA_offset_extended";
556     case DW_CFA_restore_extended:
557       return "DW_CFA_restore_extended";
558     case DW_CFA_undefined:
559       return "DW_CFA_undefined";
560     case DW_CFA_same_value:
561       return "DW_CFA_same_value";
562     case DW_CFA_register:
563       return "DW_CFA_register";
564     case DW_CFA_remember_state:
565       return "DW_CFA_remember_state";
566     case DW_CFA_restore_state:
567       return "DW_CFA_restore_state";
568     case DW_CFA_def_cfa:
569       return "DW_CFA_def_cfa";
570     case DW_CFA_def_cfa_register:
571       return "DW_CFA_def_cfa_register";
572     case DW_CFA_def_cfa_offset:
573       return "DW_CFA_def_cfa_offset";
574
575     /* DWARF 3 */
576     case DW_CFA_def_cfa_expression:
577       return "DW_CFA_def_cfa_expression";
578     case DW_CFA_expression:
579       return "DW_CFA_expression";
580     case DW_CFA_offset_extended_sf:
581       return "DW_CFA_offset_extended_sf";
582     case DW_CFA_def_cfa_sf:
583       return "DW_CFA_def_cfa_sf";
584     case DW_CFA_def_cfa_offset_sf:
585       return "DW_CFA_def_cfa_offset_sf";
586
587     /* SGI/MIPS specific */
588     case DW_CFA_MIPS_advance_loc8:
589       return "DW_CFA_MIPS_advance_loc8";
590
591     /* GNU extensions */
592     case DW_CFA_GNU_window_save:
593       return "DW_CFA_GNU_window_save";
594     case DW_CFA_GNU_args_size:
595       return "DW_CFA_GNU_args_size";
596     case DW_CFA_GNU_negative_offset_extended:
597       return "DW_CFA_GNU_negative_offset_extended";
598
599     default:
600       return "DW_CFA_<unknown>";
601     }
602 }
603
604 /* Return a pointer to a newly allocated Call Frame Instruction.  */
605
606 static inline dw_cfi_ref
607 new_cfi (void)
608 {
609   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
610
611   cfi->dw_cfi_next = NULL;
612   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
613   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
614
615   return cfi;
616 }
617
618 /* Add a Call Frame Instruction to list of instructions.  */
619
620 static inline void
621 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
622 {
623   dw_cfi_ref *p;
624
625   /* Find the end of the chain.  */
626   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
627     ;
628
629   *p = cfi;
630 }
631
632 /* Generate a new label for the CFI info to refer to.  */
633
634 char *
635 dwarf2out_cfi_label (void)
636 {
637   static char label[20];
638
639   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
640   ASM_OUTPUT_LABEL (asm_out_file, label);
641   return label;
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_NEW (struct queued_reg_save);
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           else if (GET_CODE (elem) == SET
1583                    && par_index != 0
1584                    && !RTX_FRAME_RELATED_P (elem))
1585             {
1586               /* Stack adjustment combining might combine some post-prologue
1587                  stack adjustment into a prologue stack adjustment.  */
1588               HOST_WIDE_INT offset = stack_adjust_offset (elem);
1589
1590               if (offset != 0)
1591                 {
1592                   if (cfa.reg == STACK_POINTER_REGNUM)
1593                     cfa.offset += offset;
1594
1595 #ifndef STACK_GROWS_DOWNWARD
1596                   offset = -offset;
1597 #endif
1598
1599                   args_size += offset;
1600                   if (args_size < 0)
1601                     args_size = 0;
1602
1603                   def_cfa_1 (label, &cfa);
1604                   if (flag_asynchronous_unwind_tables)
1605                     dwarf2out_args_size (label, args_size);
1606                 }
1607             }
1608         }
1609       return;
1610     }
1611
1612   gcc_assert (GET_CODE (expr) == SET);
1613
1614   src = SET_SRC (expr);
1615   dest = SET_DEST (expr);
1616
1617   if (REG_P (src))
1618     {
1619       rtx rsi = reg_saved_in (src);
1620       if (rsi)
1621         src = rsi;
1622     }
1623
1624   switch (GET_CODE (dest))
1625     {
1626     case REG:
1627       switch (GET_CODE (src))
1628         {
1629           /* Setting FP from SP.  */
1630         case REG:
1631           if (cfa.reg == (unsigned) REGNO (src))
1632             {
1633               /* Rule 1 */
1634               /* Update the CFA rule wrt SP or FP.  Make sure src is
1635                  relative to the current CFA register.
1636
1637                  We used to require that dest be either SP or FP, but the
1638                  ARM copies SP to a temporary register, and from there to
1639                  FP.  So we just rely on the backends to only set
1640                  RTX_FRAME_RELATED_P on appropriate insns.  */
1641               cfa.reg = REGNO (dest);
1642               cfa_temp.reg = cfa.reg;
1643               cfa_temp.offset = cfa.offset;
1644             }
1645           else
1646             {
1647               /* Saving a register in a register.  */
1648               gcc_assert (!fixed_regs [REGNO (dest)]
1649                           /* For the SPARC and its register window.  */
1650                           || (DWARF_FRAME_REGNUM (REGNO (src))
1651                               == DWARF_FRAME_RETURN_COLUMN));
1652               queue_reg_save (label, src, dest, 0);
1653             }
1654           break;
1655
1656         case PLUS:
1657         case MINUS:
1658         case LO_SUM:
1659           if (dest == stack_pointer_rtx)
1660             {
1661               /* Rule 2 */
1662               /* Adjusting SP.  */
1663               switch (GET_CODE (XEXP (src, 1)))
1664                 {
1665                 case CONST_INT:
1666                   offset = INTVAL (XEXP (src, 1));
1667                   break;
1668                 case REG:
1669                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1670                               == cfa_temp.reg);
1671                   offset = cfa_temp.offset;
1672                   break;
1673                 default:
1674                   gcc_unreachable ();
1675                 }
1676
1677               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1678                 {
1679                   /* Restoring SP from FP in the epilogue.  */
1680                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1681                   cfa.reg = STACK_POINTER_REGNUM;
1682                 }
1683               else if (GET_CODE (src) == LO_SUM)
1684                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1685                 ;
1686               else
1687                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1688
1689               if (GET_CODE (src) != MINUS)
1690                 offset = -offset;
1691               if (cfa.reg == STACK_POINTER_REGNUM)
1692                 cfa.offset += offset;
1693               if (cfa_store.reg == STACK_POINTER_REGNUM)
1694                 cfa_store.offset += offset;
1695             }
1696           else if (dest == hard_frame_pointer_rtx)
1697             {
1698               /* Rule 3 */
1699               /* Either setting the FP from an offset of the SP,
1700                  or adjusting the FP */
1701               gcc_assert (frame_pointer_needed);
1702
1703               gcc_assert (REG_P (XEXP (src, 0))
1704                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1705                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1706               offset = INTVAL (XEXP (src, 1));
1707               if (GET_CODE (src) != MINUS)
1708                 offset = -offset;
1709               cfa.offset += offset;
1710               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1711             }
1712           else
1713             {
1714               gcc_assert (GET_CODE (src) != MINUS);
1715
1716               /* Rule 4 */
1717               if (REG_P (XEXP (src, 0))
1718                   && REGNO (XEXP (src, 0)) == cfa.reg
1719                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1720                 {
1721                   /* Setting a temporary CFA register that will be copied
1722                      into the FP later on.  */
1723                   offset = - INTVAL (XEXP (src, 1));
1724                   cfa.offset += offset;
1725                   cfa.reg = REGNO (dest);
1726                   /* Or used to save regs to the stack.  */
1727                   cfa_temp.reg = cfa.reg;
1728                   cfa_temp.offset = cfa.offset;
1729                 }
1730
1731               /* Rule 5 */
1732               else if (REG_P (XEXP (src, 0))
1733                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1734                        && XEXP (src, 1) == stack_pointer_rtx)
1735                 {
1736                   /* Setting a scratch register that we will use instead
1737                      of SP for saving registers to the stack.  */
1738                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1739                   cfa_store.reg = REGNO (dest);
1740                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1741                 }
1742
1743               /* Rule 9 */
1744               else if (GET_CODE (src) == LO_SUM
1745                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1746                 {
1747                   cfa_temp.reg = REGNO (dest);
1748                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1749                 }
1750               else
1751                 gcc_unreachable ();
1752             }
1753           break;
1754
1755           /* Rule 6 */
1756         case CONST_INT:
1757           cfa_temp.reg = REGNO (dest);
1758           cfa_temp.offset = INTVAL (src);
1759           break;
1760
1761           /* Rule 7 */
1762         case IOR:
1763           gcc_assert (REG_P (XEXP (src, 0))
1764                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1765                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1766
1767           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1768             cfa_temp.reg = REGNO (dest);
1769           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1770           break;
1771
1772           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1773              which will fill in all of the bits.  */
1774           /* Rule 8 */
1775         case HIGH:
1776           break;
1777
1778           /* Rule 15 */
1779         case UNSPEC:
1780         case UNSPEC_VOLATILE:
1781           gcc_assert (targetm.dwarf_handle_frame_unspec);
1782           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1783           return;
1784
1785         default:
1786           gcc_unreachable ();
1787         }
1788
1789       def_cfa_1 (label, &cfa);
1790       break;
1791
1792     case MEM:
1793       gcc_assert (REG_P (src));
1794
1795       /* Saving a register to the stack.  Make sure dest is relative to the
1796          CFA register.  */
1797       switch (GET_CODE (XEXP (dest, 0)))
1798         {
1799           /* Rule 10 */
1800           /* With a push.  */
1801         case PRE_MODIFY:
1802           /* We can't handle variable size modifications.  */
1803           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1804                       == CONST_INT);
1805           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1806
1807           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1808                       && cfa_store.reg == STACK_POINTER_REGNUM);
1809
1810           cfa_store.offset += offset;
1811           if (cfa.reg == STACK_POINTER_REGNUM)
1812             cfa.offset = cfa_store.offset;
1813
1814           offset = -cfa_store.offset;
1815           break;
1816
1817           /* Rule 11 */
1818         case PRE_INC:
1819         case PRE_DEC:
1820           offset = GET_MODE_SIZE (GET_MODE (dest));
1821           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1822             offset = -offset;
1823
1824           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1825                       && cfa_store.reg == STACK_POINTER_REGNUM);
1826
1827           cfa_store.offset += offset;
1828           if (cfa.reg == STACK_POINTER_REGNUM)
1829             cfa.offset = cfa_store.offset;
1830
1831           offset = -cfa_store.offset;
1832           break;
1833
1834           /* Rule 12 */
1835           /* With an offset.  */
1836         case PLUS:
1837         case MINUS:
1838         case LO_SUM:
1839           {
1840             int regno;
1841
1842             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1843                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1844             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1845             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1846               offset = -offset;
1847
1848             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1849
1850             if (cfa_store.reg == (unsigned) regno)
1851               offset -= cfa_store.offset;
1852             else
1853               {
1854                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1855                 offset -= cfa_temp.offset;
1856               }
1857           }
1858           break;
1859
1860           /* Rule 13 */
1861           /* Without an offset.  */
1862         case REG:
1863           {
1864             int regno = REGNO (XEXP (dest, 0));
1865
1866             if (cfa_store.reg == (unsigned) regno)
1867               offset = -cfa_store.offset;
1868             else
1869               {
1870                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1871                 offset = -cfa_temp.offset;
1872               }
1873           }
1874           break;
1875
1876           /* Rule 14 */
1877         case POST_INC:
1878           gcc_assert (cfa_temp.reg
1879                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1880           offset = -cfa_temp.offset;
1881           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1882           break;
1883
1884         default:
1885           gcc_unreachable ();
1886         }
1887
1888       if (REGNO (src) != STACK_POINTER_REGNUM
1889           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1890           && (unsigned) REGNO (src) == cfa.reg)
1891         {
1892           /* We're storing the current CFA reg into the stack.  */
1893
1894           if (cfa.offset == 0)
1895             {
1896               /* If the source register is exactly the CFA, assume
1897                  we're saving SP like any other register; this happens
1898                  on the ARM.  */
1899               def_cfa_1 (label, &cfa);
1900               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1901               break;
1902             }
1903           else
1904             {
1905               /* Otherwise, we'll need to look in the stack to
1906                  calculate the CFA.  */
1907               rtx x = XEXP (dest, 0);
1908
1909               if (!REG_P (x))
1910                 x = XEXP (x, 0);
1911               gcc_assert (REG_P (x));
1912
1913               cfa.reg = REGNO (x);
1914               cfa.base_offset = offset;
1915               cfa.indirect = 1;
1916               def_cfa_1 (label, &cfa);
1917               break;
1918             }
1919         }
1920
1921       def_cfa_1 (label, &cfa);
1922       {
1923         span = targetm.dwarf_register_span (src);
1924
1925         if (!span)
1926           queue_reg_save (label, src, NULL_RTX, offset);
1927         else
1928           {
1929             /* We have a PARALLEL describing where the contents of SRC
1930                live.  Queue register saves for each piece of the
1931                PARALLEL.  */
1932             int par_index;
1933             int limit;
1934             HOST_WIDE_INT span_offset = offset;
1935
1936             gcc_assert (GET_CODE (span) == PARALLEL);
1937
1938             limit = XVECLEN (span, 0);
1939             for (par_index = 0; par_index < limit; par_index++)
1940               {
1941                 rtx elem = XVECEXP (span, 0, par_index);
1942
1943                 queue_reg_save (label, elem, NULL_RTX, span_offset);
1944                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1945               }
1946           }
1947       }
1948       break;
1949
1950     default:
1951       gcc_unreachable ();
1952     }
1953 }
1954
1955 /* Record call frame debugging information for INSN, which either
1956    sets SP or FP (adjusting how we calculate the frame address) or saves a
1957    register to the stack.  If INSN is NULL_RTX, initialize our state.
1958
1959    If AFTER_P is false, we're being called before the insn is emitted,
1960    otherwise after.  Call instructions get invoked twice.  */
1961
1962 void
1963 dwarf2out_frame_debug (rtx insn, bool after_p)
1964 {
1965   const char *label;
1966   rtx src;
1967
1968   if (insn == NULL_RTX)
1969     {
1970       size_t i;
1971
1972       /* Flush any queued register saves.  */
1973       flush_queued_reg_saves ();
1974
1975       /* Set up state for generating call frame debug info.  */
1976       lookup_cfa (&cfa);
1977       gcc_assert (cfa.reg
1978                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1979
1980       cfa.reg = STACK_POINTER_REGNUM;
1981       cfa_store = cfa;
1982       cfa_temp.reg = -1;
1983       cfa_temp.offset = 0;
1984
1985       for (i = 0; i < num_regs_saved_in_regs; i++)
1986         {
1987           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1988           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1989         }
1990       num_regs_saved_in_regs = 0;
1991       return;
1992     }
1993
1994   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1995     flush_queued_reg_saves ();
1996
1997   if (! RTX_FRAME_RELATED_P (insn))
1998     {
1999       if (!ACCUMULATE_OUTGOING_ARGS)
2000         dwarf2out_stack_adjust (insn, after_p);
2001       return;
2002     }
2003
2004   label = dwarf2out_cfi_label ();
2005   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2006   if (src)
2007     insn = XEXP (src, 0);
2008   else
2009     insn = PATTERN (insn);
2010
2011   dwarf2out_frame_debug_expr (insn, label);
2012 }
2013
2014 #endif
2015
2016 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2017 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2018  (enum dwarf_call_frame_info cfi);
2019
2020 static enum dw_cfi_oprnd_type
2021 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2022 {
2023   switch (cfi)
2024     {
2025     case DW_CFA_nop:
2026     case DW_CFA_GNU_window_save:
2027       return dw_cfi_oprnd_unused;
2028
2029     case DW_CFA_set_loc:
2030     case DW_CFA_advance_loc1:
2031     case DW_CFA_advance_loc2:
2032     case DW_CFA_advance_loc4:
2033     case DW_CFA_MIPS_advance_loc8:
2034       return dw_cfi_oprnd_addr;
2035
2036     case DW_CFA_offset:
2037     case DW_CFA_offset_extended:
2038     case DW_CFA_def_cfa:
2039     case DW_CFA_offset_extended_sf:
2040     case DW_CFA_def_cfa_sf:
2041     case DW_CFA_restore_extended:
2042     case DW_CFA_undefined:
2043     case DW_CFA_same_value:
2044     case DW_CFA_def_cfa_register:
2045     case DW_CFA_register:
2046       return dw_cfi_oprnd_reg_num;
2047
2048     case DW_CFA_def_cfa_offset:
2049     case DW_CFA_GNU_args_size:
2050     case DW_CFA_def_cfa_offset_sf:
2051       return dw_cfi_oprnd_offset;
2052
2053     case DW_CFA_def_cfa_expression:
2054     case DW_CFA_expression:
2055       return dw_cfi_oprnd_loc;
2056
2057     default:
2058       gcc_unreachable ();
2059     }
2060 }
2061
2062 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2063 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2064  (enum dwarf_call_frame_info cfi);
2065
2066 static enum dw_cfi_oprnd_type
2067 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2068 {
2069   switch (cfi)
2070     {
2071     case DW_CFA_def_cfa:
2072     case DW_CFA_def_cfa_sf:
2073     case DW_CFA_offset:
2074     case DW_CFA_offset_extended_sf:
2075     case DW_CFA_offset_extended:
2076       return dw_cfi_oprnd_offset;
2077
2078     case DW_CFA_register:
2079       return dw_cfi_oprnd_reg_num;
2080
2081     default:
2082       return dw_cfi_oprnd_unused;
2083     }
2084 }
2085
2086 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2087
2088 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2089    switch to the data section instead, and write out a synthetic label
2090    for collect2.  */
2091
2092 static void
2093 switch_to_eh_frame_section (void)
2094 {
2095   tree label;
2096
2097 #ifdef EH_FRAME_SECTION_NAME
2098   if (eh_frame_section == 0)
2099     {
2100       int flags;
2101
2102       if (EH_TABLES_CAN_BE_READ_ONLY)
2103         {
2104           int fde_encoding;
2105           int per_encoding;
2106           int lsda_encoding;
2107
2108           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2109                                                        /*global=*/0);
2110           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2111                                                        /*global=*/1);
2112           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2113                                                         /*global=*/0);
2114           flags = ((! flag_pic
2115                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2116                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2117                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2118                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2119                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2120                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2121                    ? 0 : SECTION_WRITE);
2122         }
2123       else
2124         flags = SECTION_WRITE;
2125       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2126     }
2127 #endif
2128
2129   if (eh_frame_section)
2130     switch_to_section (eh_frame_section);
2131   else
2132     {
2133       /* We have no special eh_frame section.  Put the information in
2134          the data section and emit special labels to guide collect2.  */
2135       switch_to_section (data_section);
2136       label = get_file_function_name ("F");
2137       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2138       targetm.asm_out.globalize_label (asm_out_file,
2139                                        IDENTIFIER_POINTER (label));
2140       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2141     }
2142 }
2143
2144 /* Output a Call Frame Information opcode and its operand(s).  */
2145
2146 static void
2147 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2148 {
2149   unsigned long r;
2150   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2151     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2152                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2153                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2154                          ((unsigned HOST_WIDE_INT)
2155                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2156   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2157     {
2158       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2159       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2160                            "DW_CFA_offset, column 0x%lx", r);
2161       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2162     }
2163   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2164     {
2165       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2166       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2167                            "DW_CFA_restore, column 0x%lx", r);
2168     }
2169   else
2170     {
2171       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2172                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2173
2174       switch (cfi->dw_cfi_opc)
2175         {
2176         case DW_CFA_set_loc:
2177           if (for_eh)
2178             dw2_asm_output_encoded_addr_rtx (
2179                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2180                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2181                 false, NULL);
2182           else
2183             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2184                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2185           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2186           break;
2187
2188         case DW_CFA_advance_loc1:
2189           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2190                                 fde->dw_fde_current_label, NULL);
2191           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2192           break;
2193
2194         case DW_CFA_advance_loc2:
2195           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2196                                 fde->dw_fde_current_label, NULL);
2197           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2198           break;
2199
2200         case DW_CFA_advance_loc4:
2201           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2202                                 fde->dw_fde_current_label, NULL);
2203           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2204           break;
2205
2206         case DW_CFA_MIPS_advance_loc8:
2207           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2208                                 fde->dw_fde_current_label, NULL);
2209           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2210           break;
2211
2212         case DW_CFA_offset_extended:
2213         case DW_CFA_def_cfa:
2214           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2215           dw2_asm_output_data_uleb128 (r, NULL);
2216           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2217           break;
2218
2219         case DW_CFA_offset_extended_sf:
2220         case DW_CFA_def_cfa_sf:
2221           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2222           dw2_asm_output_data_uleb128 (r, NULL);
2223           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2224           break;
2225
2226         case DW_CFA_restore_extended:
2227         case DW_CFA_undefined:
2228         case DW_CFA_same_value:
2229         case DW_CFA_def_cfa_register:
2230           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2231           dw2_asm_output_data_uleb128 (r, NULL);
2232           break;
2233
2234         case DW_CFA_register:
2235           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2236           dw2_asm_output_data_uleb128 (r, NULL);
2237           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2238           dw2_asm_output_data_uleb128 (r, NULL);
2239           break;
2240
2241         case DW_CFA_def_cfa_offset:
2242         case DW_CFA_GNU_args_size:
2243           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2244           break;
2245
2246         case DW_CFA_def_cfa_offset_sf:
2247           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2248           break;
2249
2250         case DW_CFA_GNU_window_save:
2251           break;
2252
2253         case DW_CFA_def_cfa_expression:
2254         case DW_CFA_expression:
2255           output_cfa_loc (cfi);
2256           break;
2257
2258         case DW_CFA_GNU_negative_offset_extended:
2259           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2260           gcc_unreachable ();
2261
2262         default:
2263           break;
2264         }
2265     }
2266 }
2267
2268 /* Output the call frame information used to record information
2269    that relates to calculating the frame pointer, and records the
2270    location of saved registers.  */
2271
2272 static void
2273 output_call_frame_info (int for_eh)
2274 {
2275   unsigned int i;
2276   dw_fde_ref fde;
2277   dw_cfi_ref cfi;
2278   char l1[20], l2[20], section_start_label[20];
2279   bool any_lsda_needed = false;
2280   char augmentation[6];
2281   int augmentation_size;
2282   int fde_encoding = DW_EH_PE_absptr;
2283   int per_encoding = DW_EH_PE_absptr;
2284   int lsda_encoding = DW_EH_PE_absptr;
2285   int return_reg;
2286
2287   /* Don't emit a CIE if there won't be any FDEs.  */
2288   if (fde_table_in_use == 0)
2289     return;
2290
2291   /* If we make FDEs linkonce, we may have to emit an empty label for
2292      an FDE that wouldn't otherwise be emitted.  We want to avoid
2293      having an FDE kept around when the function it refers to is
2294      discarded.  Example where this matters: a primary function
2295      template in C++ requires EH information, but an explicit
2296      specialization doesn't.  */
2297   if (TARGET_USES_WEAK_UNWIND_INFO
2298       && ! flag_asynchronous_unwind_tables
2299       && flag_exceptions
2300       && for_eh)
2301     for (i = 0; i < fde_table_in_use; i++)
2302       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2303           && !fde_table[i].uses_eh_lsda
2304           && ! DECL_WEAK (fde_table[i].decl))
2305         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2306                                       for_eh, /* empty */ 1);
2307
2308   /* If we don't have any functions we'll want to unwind out of, don't
2309      emit any EH unwind information.  Note that if exceptions aren't
2310      enabled, we won't have collected nothrow information, and if we
2311      asked for asynchronous tables, we always want this info.  */
2312   if (for_eh)
2313     {
2314       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2315
2316       for (i = 0; i < fde_table_in_use; i++)
2317         if (fde_table[i].uses_eh_lsda)
2318           any_eh_needed = any_lsda_needed = true;
2319         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2320           any_eh_needed = true;
2321         else if (! fde_table[i].nothrow
2322                  && ! fde_table[i].all_throwers_are_sibcalls)
2323           any_eh_needed = true;
2324
2325       if (! any_eh_needed)
2326         return;
2327     }
2328
2329   /* We're going to be generating comments, so turn on app.  */
2330   if (flag_debug_asm)
2331     app_enable ();
2332
2333   if (for_eh)
2334     switch_to_eh_frame_section ();
2335   else
2336     {
2337       if (!debug_frame_section)
2338         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2339                                            SECTION_DEBUG, NULL);
2340       switch_to_section (debug_frame_section);
2341     }
2342
2343   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2344   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2345
2346   /* Output the CIE.  */
2347   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2348   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2349   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2350     dw2_asm_output_data (4, 0xffffffff,
2351       "Initial length escape value indicating 64-bit DWARF extension");
2352   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2353                         "Length of Common Information Entry");
2354   ASM_OUTPUT_LABEL (asm_out_file, l1);
2355
2356   /* Now that the CIE pointer is PC-relative for EH,
2357      use 0 to identify the CIE.  */
2358   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2359                        (for_eh ? 0 : DWARF_CIE_ID),
2360                        "CIE Identifier Tag");
2361
2362   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2363
2364   augmentation[0] = 0;
2365   augmentation_size = 0;
2366   if (for_eh)
2367     {
2368       char *p;
2369
2370       /* Augmentation:
2371          z      Indicates that a uleb128 is present to size the
2372                 augmentation section.
2373          L      Indicates the encoding (and thus presence) of
2374                 an LSDA pointer in the FDE augmentation.
2375          R      Indicates a non-default pointer encoding for
2376                 FDE code pointers.
2377          P      Indicates the presence of an encoding + language
2378                 personality routine in the CIE augmentation.  */
2379
2380       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2381       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2382       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2383
2384       p = augmentation + 1;
2385       if (eh_personality_libfunc)
2386         {
2387           *p++ = 'P';
2388           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2389           assemble_external_libcall (eh_personality_libfunc);
2390         }
2391       if (any_lsda_needed)
2392         {
2393           *p++ = 'L';
2394           augmentation_size += 1;
2395         }
2396       if (fde_encoding != DW_EH_PE_absptr)
2397         {
2398           *p++ = 'R';
2399           augmentation_size += 1;
2400         }
2401       if (p > augmentation + 1)
2402         {
2403           augmentation[0] = 'z';
2404           *p = '\0';
2405         }
2406
2407       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2408       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2409         {
2410           int offset = (  4             /* Length */
2411                         + 4             /* CIE Id */
2412                         + 1             /* CIE version */
2413                         + strlen (augmentation) + 1     /* Augmentation */
2414                         + size_of_uleb128 (1)           /* Code alignment */
2415                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2416                         + 1             /* RA column */
2417                         + 1             /* Augmentation size */
2418                         + 1             /* Personality encoding */ );
2419           int pad = -offset & (PTR_SIZE - 1);
2420
2421           augmentation_size += pad;
2422
2423           /* Augmentations should be small, so there's scarce need to
2424              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2425           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2426         }
2427     }
2428
2429   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2430   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2431   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2432                                "CIE Data Alignment Factor");
2433
2434   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2435   if (DW_CIE_VERSION == 1)
2436     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2437   else
2438     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2439
2440   if (augmentation[0])
2441     {
2442       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2443       if (eh_personality_libfunc)
2444         {
2445           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2446                                eh_data_format_name (per_encoding));
2447           dw2_asm_output_encoded_addr_rtx (per_encoding,
2448                                            eh_personality_libfunc,
2449                                            true, NULL);
2450         }
2451
2452       if (any_lsda_needed)
2453         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2454                              eh_data_format_name (lsda_encoding));
2455
2456       if (fde_encoding != DW_EH_PE_absptr)
2457         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2458                              eh_data_format_name (fde_encoding));
2459     }
2460
2461   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2462     output_cfi (cfi, NULL, for_eh);
2463
2464   /* Pad the CIE out to an address sized boundary.  */
2465   ASM_OUTPUT_ALIGN (asm_out_file,
2466                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2467   ASM_OUTPUT_LABEL (asm_out_file, l2);
2468
2469   /* Loop through all of the FDE's.  */
2470   for (i = 0; i < fde_table_in_use; i++)
2471     {
2472       fde = &fde_table[i];
2473
2474       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2475       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2476           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2477           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2478           && !fde->uses_eh_lsda)
2479         continue;
2480
2481       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2482       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2483       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2484       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2485       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2486         dw2_asm_output_data (4, 0xffffffff,
2487                              "Initial length escape value indicating 64-bit DWARF extension");
2488       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2489                             "FDE Length");
2490       ASM_OUTPUT_LABEL (asm_out_file, l1);
2491
2492       if (for_eh)
2493         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2494       else
2495         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2496                                debug_frame_section, "FDE CIE offset");
2497
2498       if (for_eh)
2499         {
2500           if (fde->dw_fde_switched_sections)
2501             {
2502               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2503                                       fde->dw_fde_unlikely_section_label);
2504               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2505                                       fde->dw_fde_hot_section_label);
2506               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2507               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2508               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2509                                                "FDE initial location");
2510               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2511                                     fde->dw_fde_hot_section_end_label,
2512                                     fde->dw_fde_hot_section_label,
2513                                     "FDE address range");
2514               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2515                                                "FDE initial location");
2516               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2517                                     fde->dw_fde_unlikely_section_end_label,
2518                                     fde->dw_fde_unlikely_section_label,
2519                                     "FDE address range");
2520             }
2521           else
2522             {
2523               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2524               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2525               dw2_asm_output_encoded_addr_rtx (fde_encoding,
2526                                                sym_ref,
2527                                                false,
2528                                                "FDE initial location");
2529               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2530                                     fde->dw_fde_end, fde->dw_fde_begin,
2531                                     "FDE address range");
2532             }
2533         }
2534       else
2535         {
2536           if (fde->dw_fde_switched_sections)
2537             {
2538               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2539                                    fde->dw_fde_hot_section_label,
2540                                    "FDE initial location");
2541               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2542                                     fde->dw_fde_hot_section_end_label,
2543                                     fde->dw_fde_hot_section_label,
2544                                     "FDE address range");
2545               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2546                                    fde->dw_fde_unlikely_section_label,
2547                                    "FDE initial location");
2548               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2549                                     fde->dw_fde_unlikely_section_end_label,
2550                                     fde->dw_fde_unlikely_section_label,
2551                                     "FDE address range");
2552             }
2553           else
2554             {
2555               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2556                                    "FDE initial location");
2557               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2558                                     fde->dw_fde_end, fde->dw_fde_begin,
2559                                     "FDE address range");
2560             }
2561         }
2562
2563       if (augmentation[0])
2564         {
2565           if (any_lsda_needed)
2566             {
2567               int size = size_of_encoded_value (lsda_encoding);
2568
2569               if (lsda_encoding == DW_EH_PE_aligned)
2570                 {
2571                   int offset = (  4             /* Length */
2572                                 + 4             /* CIE offset */
2573                                 + 2 * size_of_encoded_value (fde_encoding)
2574                                 + 1             /* Augmentation size */ );
2575                   int pad = -offset & (PTR_SIZE - 1);
2576
2577                   size += pad;
2578                   gcc_assert (size_of_uleb128 (size) == 1);
2579                 }
2580
2581               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2582
2583               if (fde->uses_eh_lsda)
2584                 {
2585                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2586                                                fde->funcdef_number);
2587                   dw2_asm_output_encoded_addr_rtx (
2588                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2589                         false, "Language Specific Data Area");
2590                 }
2591               else
2592                 {
2593                   if (lsda_encoding == DW_EH_PE_aligned)
2594                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2595                   dw2_asm_output_data
2596                     (size_of_encoded_value (lsda_encoding), 0,
2597                      "Language Specific Data Area (none)");
2598                 }
2599             }
2600           else
2601             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2602         }
2603
2604       /* Loop through the Call Frame Instructions associated with
2605          this FDE.  */
2606       fde->dw_fde_current_label = fde->dw_fde_begin;
2607       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2608         output_cfi (cfi, fde, for_eh);
2609
2610       /* Pad the FDE out to an address sized boundary.  */
2611       ASM_OUTPUT_ALIGN (asm_out_file,
2612                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2613       ASM_OUTPUT_LABEL (asm_out_file, l2);
2614     }
2615
2616   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2617     dw2_asm_output_data (4, 0, "End of Table");
2618 #ifdef MIPS_DEBUGGING_INFO
2619   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2620      get a value of 0.  Putting .align 0 after the label fixes it.  */
2621   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2622 #endif
2623
2624   /* Turn off app to make assembly quicker.  */
2625   if (flag_debug_asm)
2626     app_disable ();
2627 }
2628
2629 /* Output a marker (i.e. a label) for the beginning of a function, before
2630    the prologue.  */
2631
2632 void
2633 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2634                           const char *file ATTRIBUTE_UNUSED)
2635 {
2636   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2637   char * dup_label;
2638   dw_fde_ref fde;
2639
2640   current_function_func_begin_label = NULL;
2641
2642 #ifdef TARGET_UNWIND_INFO
2643   /* ??? current_function_func_begin_label is also used by except.c
2644      for call-site information.  We must emit this label if it might
2645      be used.  */
2646   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2647       && ! dwarf2out_do_frame ())
2648     return;
2649 #else
2650   if (! dwarf2out_do_frame ())
2651     return;
2652 #endif
2653
2654   switch_to_section (function_section (current_function_decl));
2655   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2656                                current_function_funcdef_no);
2657   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2658                           current_function_funcdef_no);
2659   dup_label = xstrdup (label);
2660   current_function_func_begin_label = dup_label;
2661
2662 #ifdef TARGET_UNWIND_INFO
2663   /* We can elide the fde allocation if we're not emitting debug info.  */
2664   if (! dwarf2out_do_frame ())
2665     return;
2666 #endif
2667
2668   /* Expand the fde table if necessary.  */
2669   if (fde_table_in_use == fde_table_allocated)
2670     {
2671       fde_table_allocated += FDE_TABLE_INCREMENT;
2672       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
2673       memset (fde_table + fde_table_in_use, 0,
2674               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2675     }
2676
2677   /* Record the FDE associated with this function.  */
2678   current_funcdef_fde = fde_table_in_use;
2679
2680   /* Add the new FDE at the end of the fde_table.  */
2681   fde = &fde_table[fde_table_in_use++];
2682   fde->decl = current_function_decl;
2683   fde->dw_fde_begin = dup_label;
2684   fde->dw_fde_current_label = dup_label;
2685   fde->dw_fde_hot_section_label = NULL;
2686   fde->dw_fde_hot_section_end_label = NULL;
2687   fde->dw_fde_unlikely_section_label = NULL;
2688   fde->dw_fde_unlikely_section_end_label = NULL;
2689   fde->dw_fde_switched_sections = false;
2690   fde->dw_fde_end = NULL;
2691   fde->dw_fde_cfi = NULL;
2692   fde->funcdef_number = current_function_funcdef_no;
2693   fde->nothrow = TREE_NOTHROW (current_function_decl);
2694   fde->uses_eh_lsda = crtl->uses_eh_lsda;
2695   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
2696
2697   args_size = old_args_size = 0;
2698
2699   /* We only want to output line number information for the genuine dwarf2
2700      prologue case, not the eh frame case.  */
2701 #ifdef DWARF2_DEBUGGING_INFO
2702   if (file)
2703     dwarf2out_source_line (line, file);
2704 #endif
2705 }
2706
2707 /* Output a marker (i.e. a label) for the absolute end of the generated code
2708    for a function definition.  This gets called *after* the epilogue code has
2709    been generated.  */
2710
2711 void
2712 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2713                         const char *file ATTRIBUTE_UNUSED)
2714 {
2715   dw_fde_ref fde;
2716   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2717
2718   /* Output a label to mark the endpoint of the code generated for this
2719      function.  */
2720   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2721                                current_function_funcdef_no);
2722   ASM_OUTPUT_LABEL (asm_out_file, label);
2723   fde = current_fde ();
2724   gcc_assert (fde != NULL);
2725   fde->dw_fde_end = xstrdup (label);
2726 }
2727
2728 void
2729 dwarf2out_frame_init (void)
2730 {
2731   /* Allocate the initial hunk of the fde_table.  */
2732   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
2733   fde_table_allocated = FDE_TABLE_INCREMENT;
2734   fde_table_in_use = 0;
2735
2736   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2737      sake of lookup_cfa.  */
2738
2739   /* On entry, the Canonical Frame Address is at SP.  */
2740   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2741
2742 #ifdef DWARF2_UNWIND_INFO
2743   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2744     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2745 #endif
2746 }
2747
2748 void
2749 dwarf2out_frame_finish (void)
2750 {
2751   /* Output call frame information.  */
2752   if (DWARF2_FRAME_INFO)
2753     output_call_frame_info (0);
2754
2755 #ifndef TARGET_UNWIND_INFO
2756   /* Output another copy for the unwinder.  */
2757   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2758     output_call_frame_info (1);
2759 #endif
2760 }
2761
2762 /* Note that the current function section is being used for code.  */
2763
2764 static void
2765 dwarf2out_note_section_used (void)
2766 {
2767   section *sec = current_function_section ();
2768   if (sec == text_section)
2769     text_section_used = true;
2770   else if (sec == cold_text_section)
2771     cold_text_section_used = true;
2772 }
2773
2774 void
2775 dwarf2out_switch_text_section (void)
2776 {
2777   dw_fde_ref fde = current_fde ();
2778
2779   gcc_assert (cfun && fde);
2780
2781   fde->dw_fde_switched_sections = true;
2782   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
2783   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
2784   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
2785   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
2786   have_multiple_function_sections = true;
2787
2788   /* Reset the current label on switching text sections, so that we
2789      don't attempt to advance_loc4 between labels in different sections.  */
2790   fde->dw_fde_current_label = NULL;
2791
2792   /* There is no need to mark used sections when not debugging.  */
2793   if (cold_text_section != NULL)
2794     dwarf2out_note_section_used ();
2795 }
2796 #endif
2797 \f
2798 /* And now, the subset of the debugging information support code necessary
2799    for emitting location expressions.  */
2800
2801 /* Data about a single source file.  */
2802 struct dwarf_file_data GTY(())
2803 {
2804   const char * filename;
2805   int emitted_number;
2806 };
2807
2808 /* We need some way to distinguish DW_OP_addr with a direct symbol
2809    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2810 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2811
2812
2813 typedef struct dw_val_struct *dw_val_ref;
2814 typedef struct die_struct *dw_die_ref;
2815 typedef const struct die_struct *const_dw_die_ref;
2816 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2817 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2818
2819 /* Each DIE may have a series of attribute/value pairs.  Values
2820    can take on several forms.  The forms that are used in this
2821    implementation are listed below.  */
2822
2823 enum dw_val_class
2824 {
2825   dw_val_class_addr,
2826   dw_val_class_offset,
2827   dw_val_class_loc,
2828   dw_val_class_loc_list,
2829   dw_val_class_range_list,
2830   dw_val_class_const,
2831   dw_val_class_unsigned_const,
2832   dw_val_class_long_long,
2833   dw_val_class_vec,
2834   dw_val_class_flag,
2835   dw_val_class_die_ref,
2836   dw_val_class_fde_ref,
2837   dw_val_class_lbl_id,
2838   dw_val_class_lineptr,
2839   dw_val_class_str,
2840   dw_val_class_macptr,
2841   dw_val_class_file
2842 };
2843
2844 /* Describe a double word constant value.  */
2845 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2846
2847 typedef struct dw_long_long_struct GTY(())
2848 {
2849   unsigned long hi;
2850   unsigned long low;
2851 }
2852 dw_long_long_const;
2853
2854 /* Describe a floating point constant value, or a vector constant value.  */
2855
2856 typedef struct dw_vec_struct GTY(())
2857 {
2858   unsigned char * GTY((length ("%h.length"))) array;
2859   unsigned length;
2860   unsigned elt_size;
2861 }
2862 dw_vec_const;
2863
2864 /* The dw_val_node describes an attribute's value, as it is
2865    represented internally.  */
2866
2867 typedef struct dw_val_struct GTY(())
2868 {
2869   enum dw_val_class val_class;
2870   union dw_val_struct_union
2871     {
2872       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2873       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2874       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2875       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2876       HOST_WIDE_INT GTY ((default)) val_int;
2877       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2878       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2879       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2880       struct dw_val_die_union
2881         {
2882           dw_die_ref die;
2883           int external;
2884         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2885       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2886       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2887       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2888       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2889       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2890     }
2891   GTY ((desc ("%1.val_class"))) v;
2892 }
2893 dw_val_node;
2894
2895 /* Locations in memory are described using a sequence of stack machine
2896    operations.  */
2897
2898 typedef struct dw_loc_descr_struct GTY(())
2899 {
2900   dw_loc_descr_ref dw_loc_next;
2901   enum dwarf_location_atom dw_loc_opc;
2902   dw_val_node dw_loc_oprnd1;
2903   dw_val_node dw_loc_oprnd2;
2904   int dw_loc_addr;
2905 }
2906 dw_loc_descr_node;
2907
2908 /* Location lists are ranges + location descriptions for that range,
2909    so you can track variables that are in different places over
2910    their entire life.  */
2911 typedef struct dw_loc_list_struct GTY(())
2912 {
2913   dw_loc_list_ref dw_loc_next;
2914   const char *begin; /* Label for begin address of range */
2915   const char *end;  /* Label for end address of range */
2916   char *ll_symbol; /* Label for beginning of location list.
2917                       Only on head of list */
2918   const char *section; /* Section this loclist is relative to */
2919   dw_loc_descr_ref expr;
2920 } dw_loc_list_node;
2921
2922 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2923
2924 static const char *dwarf_stack_op_name (unsigned);
2925 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2926                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2927 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2928 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2929 static unsigned long size_of_locs (dw_loc_descr_ref);
2930 static void output_loc_operands (dw_loc_descr_ref);
2931 static void output_loc_sequence (dw_loc_descr_ref);
2932
2933 /* Convert a DWARF stack opcode into its string name.  */
2934
2935 static const char *
2936 dwarf_stack_op_name (unsigned int op)
2937 {
2938   switch (op)
2939     {
2940     case DW_OP_addr:
2941     case INTERNAL_DW_OP_tls_addr:
2942       return "DW_OP_addr";
2943     case DW_OP_deref:
2944       return "DW_OP_deref";
2945     case DW_OP_const1u:
2946       return "DW_OP_const1u";
2947     case DW_OP_const1s:
2948       return "DW_OP_const1s";
2949     case DW_OP_const2u:
2950       return "DW_OP_const2u";
2951     case DW_OP_const2s:
2952       return "DW_OP_const2s";
2953     case DW_OP_const4u:
2954       return "DW_OP_const4u";
2955     case DW_OP_const4s:
2956       return "DW_OP_const4s";
2957     case DW_OP_const8u:
2958       return "DW_OP_const8u";
2959     case DW_OP_const8s:
2960       return "DW_OP_const8s";
2961     case DW_OP_constu:
2962       return "DW_OP_constu";
2963     case DW_OP_consts:
2964       return "DW_OP_consts";
2965     case DW_OP_dup:
2966       return "DW_OP_dup";
2967     case DW_OP_drop:
2968       return "DW_OP_drop";
2969     case DW_OP_over:
2970       return "DW_OP_over";
2971     case DW_OP_pick:
2972       return "DW_OP_pick";
2973     case DW_OP_swap:
2974       return "DW_OP_swap";
2975     case DW_OP_rot:
2976       return "DW_OP_rot";
2977     case DW_OP_xderef:
2978       return "DW_OP_xderef";
2979     case DW_OP_abs:
2980       return "DW_OP_abs";
2981     case DW_OP_and:
2982       return "DW_OP_and";
2983     case DW_OP_div:
2984       return "DW_OP_div";
2985     case DW_OP_minus:
2986       return "DW_OP_minus";
2987     case DW_OP_mod:
2988       return "DW_OP_mod";
2989     case DW_OP_mul:
2990       return "DW_OP_mul";
2991     case DW_OP_neg:
2992       return "DW_OP_neg";
2993     case DW_OP_not:
2994       return "DW_OP_not";
2995     case DW_OP_or:
2996       return "DW_OP_or";
2997     case DW_OP_plus:
2998       return "DW_OP_plus";
2999     case DW_OP_plus_uconst:
3000       return "DW_OP_plus_uconst";
3001     case DW_OP_shl:
3002       return "DW_OP_shl";
3003     case DW_OP_shr:
3004       return "DW_OP_shr";
3005     case DW_OP_shra:
3006       return "DW_OP_shra";
3007     case DW_OP_xor:
3008       return "DW_OP_xor";
3009     case DW_OP_bra:
3010       return "DW_OP_bra";
3011     case DW_OP_eq:
3012       return "DW_OP_eq";
3013     case DW_OP_ge:
3014       return "DW_OP_ge";
3015     case DW_OP_gt:
3016       return "DW_OP_gt";
3017     case DW_OP_le:
3018       return "DW_OP_le";
3019     case DW_OP_lt:
3020       return "DW_OP_lt";
3021     case DW_OP_ne:
3022       return "DW_OP_ne";
3023     case DW_OP_skip:
3024       return "DW_OP_skip";
3025     case DW_OP_lit0:
3026       return "DW_OP_lit0";
3027     case DW_OP_lit1:
3028       return "DW_OP_lit1";
3029     case DW_OP_lit2:
3030       return "DW_OP_lit2";
3031     case DW_OP_lit3:
3032       return "DW_OP_lit3";
3033     case DW_OP_lit4:
3034       return "DW_OP_lit4";
3035     case DW_OP_lit5:
3036       return "DW_OP_lit5";
3037     case DW_OP_lit6:
3038       return "DW_OP_lit6";
3039     case DW_OP_lit7:
3040       return "DW_OP_lit7";
3041     case DW_OP_lit8:
3042       return "DW_OP_lit8";
3043     case DW_OP_lit9:
3044       return "DW_OP_lit9";
3045     case DW_OP_lit10:
3046       return "DW_OP_lit10";
3047     case DW_OP_lit11:
3048       return "DW_OP_lit11";
3049     case DW_OP_lit12:
3050       return "DW_OP_lit12";
3051     case DW_OP_lit13:
3052       return "DW_OP_lit13";
3053     case DW_OP_lit14:
3054       return "DW_OP_lit14";
3055     case DW_OP_lit15:
3056       return "DW_OP_lit15";
3057     case DW_OP_lit16:
3058       return "DW_OP_lit16";
3059     case DW_OP_lit17:
3060       return "DW_OP_lit17";
3061     case DW_OP_lit18:
3062       return "DW_OP_lit18";
3063     case DW_OP_lit19:
3064       return "DW_OP_lit19";
3065     case DW_OP_lit20:
3066       return "DW_OP_lit20";
3067     case DW_OP_lit21:
3068       return "DW_OP_lit21";
3069     case DW_OP_lit22:
3070       return "DW_OP_lit22";
3071     case DW_OP_lit23:
3072       return "DW_OP_lit23";
3073     case DW_OP_lit24:
3074       return "DW_OP_lit24";
3075     case DW_OP_lit25:
3076       return "DW_OP_lit25";
3077     case DW_OP_lit26:
3078       return "DW_OP_lit26";
3079     case DW_OP_lit27:
3080       return "DW_OP_lit27";
3081     case DW_OP_lit28:
3082       return "DW_OP_lit28";
3083     case DW_OP_lit29:
3084       return "DW_OP_lit29";
3085     case DW_OP_lit30:
3086       return "DW_OP_lit30";
3087     case DW_OP_lit31:
3088       return "DW_OP_lit31";
3089     case DW_OP_reg0:
3090       return "DW_OP_reg0";
3091     case DW_OP_reg1:
3092       return "DW_OP_reg1";
3093     case DW_OP_reg2:
3094       return "DW_OP_reg2";
3095     case DW_OP_reg3:
3096       return "DW_OP_reg3";
3097     case DW_OP_reg4:
3098       return "DW_OP_reg4";
3099     case DW_OP_reg5:
3100       return "DW_OP_reg5";
3101     case DW_OP_reg6:
3102       return "DW_OP_reg6";
3103     case DW_OP_reg7:
3104       return "DW_OP_reg7";
3105     case DW_OP_reg8:
3106       return "DW_OP_reg8";
3107     case DW_OP_reg9:
3108       return "DW_OP_reg9";
3109     case DW_OP_reg10:
3110       return "DW_OP_reg10";
3111     case DW_OP_reg11:
3112       return "DW_OP_reg11";
3113     case DW_OP_reg12:
3114       return "DW_OP_reg12";
3115     case DW_OP_reg13:
3116       return "DW_OP_reg13";
3117     case DW_OP_reg14:
3118       return "DW_OP_reg14";
3119     case DW_OP_reg15:
3120       return "DW_OP_reg15";
3121     case DW_OP_reg16:
3122       return "DW_OP_reg16";
3123     case DW_OP_reg17:
3124       return "DW_OP_reg17";
3125     case DW_OP_reg18:
3126       return "DW_OP_reg18";
3127     case DW_OP_reg19:
3128       return "DW_OP_reg19";
3129     case DW_OP_reg20:
3130       return "DW_OP_reg20";
3131     case DW_OP_reg21:
3132       return "DW_OP_reg21";
3133     case DW_OP_reg22:
3134       return "DW_OP_reg22";
3135     case DW_OP_reg23:
3136       return "DW_OP_reg23";
3137     case DW_OP_reg24:
3138       return "DW_OP_reg24";
3139     case DW_OP_reg25:
3140       return "DW_OP_reg25";
3141     case DW_OP_reg26:
3142       return "DW_OP_reg26";
3143     case DW_OP_reg27:
3144       return "DW_OP_reg27";
3145     case DW_OP_reg28:
3146       return "DW_OP_reg28";
3147     case DW_OP_reg29:
3148       return "DW_OP_reg29";
3149     case DW_OP_reg30:
3150       return "DW_OP_reg30";
3151     case DW_OP_reg31:
3152       return "DW_OP_reg31";
3153     case DW_OP_breg0:
3154       return "DW_OP_breg0";
3155     case DW_OP_breg1:
3156       return "DW_OP_breg1";
3157     case DW_OP_breg2:
3158       return "DW_OP_breg2";
3159     case DW_OP_breg3:
3160       return "DW_OP_breg3";
3161     case DW_OP_breg4:
3162       return "DW_OP_breg4";
3163     case DW_OP_breg5:
3164       return "DW_OP_breg5";
3165     case DW_OP_breg6:
3166       return "DW_OP_breg6";
3167     case DW_OP_breg7:
3168       return "DW_OP_breg7";
3169     case DW_OP_breg8:
3170       return "DW_OP_breg8";
3171     case DW_OP_breg9:
3172       return "DW_OP_breg9";
3173     case DW_OP_breg10:
3174       return "DW_OP_breg10";
3175     case DW_OP_breg11:
3176       return "DW_OP_breg11";
3177     case DW_OP_breg12:
3178       return "DW_OP_breg12";
3179     case DW_OP_breg13:
3180       return "DW_OP_breg13";
3181     case DW_OP_breg14:
3182       return "DW_OP_breg14";
3183     case DW_OP_breg15:
3184       return "DW_OP_breg15";
3185     case DW_OP_breg16:
3186       return "DW_OP_breg16";
3187     case DW_OP_breg17:
3188       return "DW_OP_breg17";
3189     case DW_OP_breg18:
3190       return "DW_OP_breg18";
3191     case DW_OP_breg19:
3192       return "DW_OP_breg19";
3193     case DW_OP_breg20:
3194       return "DW_OP_breg20";
3195     case DW_OP_breg21:
3196       return "DW_OP_breg21";
3197     case DW_OP_breg22:
3198       return "DW_OP_breg22";
3199     case DW_OP_breg23:
3200       return "DW_OP_breg23";
3201     case DW_OP_breg24:
3202       return "DW_OP_breg24";
3203     case DW_OP_breg25:
3204       return "DW_OP_breg25";
3205     case DW_OP_breg26:
3206       return "DW_OP_breg26";
3207     case DW_OP_breg27:
3208       return "DW_OP_breg27";
3209     case DW_OP_breg28:
3210       return "DW_OP_breg28";
3211     case DW_OP_breg29:
3212       return "DW_OP_breg29";
3213     case DW_OP_breg30:
3214       return "DW_OP_breg30";
3215     case DW_OP_breg31:
3216       return "DW_OP_breg31";
3217     case DW_OP_regx:
3218       return "DW_OP_regx";
3219     case DW_OP_fbreg:
3220       return "DW_OP_fbreg";
3221     case DW_OP_bregx:
3222       return "DW_OP_bregx";
3223     case DW_OP_piece:
3224       return "DW_OP_piece";
3225     case DW_OP_deref_size:
3226       return "DW_OP_deref_size";
3227     case DW_OP_xderef_size:
3228       return "DW_OP_xderef_size";
3229     case DW_OP_nop:
3230       return "DW_OP_nop";
3231     case DW_OP_push_object_address:
3232       return "DW_OP_push_object_address";
3233     case DW_OP_call2:
3234       return "DW_OP_call2";
3235     case DW_OP_call4:
3236       return "DW_OP_call4";
3237     case DW_OP_call_ref:
3238       return "DW_OP_call_ref";
3239     case DW_OP_GNU_push_tls_address:
3240       return "DW_OP_GNU_push_tls_address";
3241     case DW_OP_GNU_uninit:
3242       return "DW_OP_GNU_uninit";
3243     default:
3244       return "OP_<unknown>";
3245     }
3246 }
3247
3248 /* Return a pointer to a newly allocated location description.  Location
3249    descriptions are simple expression terms that can be strung
3250    together to form more complicated location (address) descriptions.  */
3251
3252 static inline dw_loc_descr_ref
3253 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3254                unsigned HOST_WIDE_INT oprnd2)
3255 {
3256   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
3257
3258   descr->dw_loc_opc = op;
3259   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3260   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3261   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3262   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3263
3264   return descr;
3265 }
3266
3267 /* Add a location description term to a location description expression.  */
3268
3269 static inline void
3270 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3271 {
3272   dw_loc_descr_ref *d;
3273
3274   /* Find the end of the chain.  */
3275   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3276     ;
3277
3278   *d = descr;
3279 }
3280
3281 /* Return the size of a location descriptor.  */
3282
3283 static unsigned long
3284 size_of_loc_descr (dw_loc_descr_ref loc)
3285 {
3286   unsigned long size = 1;
3287
3288   switch (loc->dw_loc_opc)
3289     {
3290     case DW_OP_addr:
3291     case INTERNAL_DW_OP_tls_addr:
3292       size += DWARF2_ADDR_SIZE;
3293       break;
3294     case DW_OP_const1u:
3295     case DW_OP_const1s:
3296       size += 1;
3297       break;
3298     case DW_OP_const2u:
3299     case DW_OP_const2s:
3300       size += 2;
3301       break;
3302     case DW_OP_const4u:
3303     case DW_OP_const4s:
3304       size += 4;
3305       break;
3306     case DW_OP_const8u:
3307     case DW_OP_const8s:
3308       size += 8;
3309       break;
3310     case DW_OP_constu:
3311       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3312       break;
3313     case DW_OP_consts:
3314       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3315       break;
3316     case DW_OP_pick:
3317       size += 1;
3318       break;
3319     case DW_OP_plus_uconst:
3320       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3321       break;
3322     case DW_OP_skip:
3323     case DW_OP_bra:
3324       size += 2;
3325       break;
3326     case DW_OP_breg0:
3327     case DW_OP_breg1:
3328     case DW_OP_breg2:
3329     case DW_OP_breg3:
3330     case DW_OP_breg4:
3331     case DW_OP_breg5:
3332     case DW_OP_breg6:
3333     case DW_OP_breg7:
3334     case DW_OP_breg8:
3335     case DW_OP_breg9:
3336     case DW_OP_breg10:
3337     case DW_OP_breg11:
3338     case DW_OP_breg12:
3339     case DW_OP_breg13:
3340     case DW_OP_breg14:
3341     case DW_OP_breg15:
3342     case DW_OP_breg16:
3343     case DW_OP_breg17:
3344     case DW_OP_breg18:
3345     case DW_OP_breg19:
3346     case DW_OP_breg20:
3347     case DW_OP_breg21:
3348     case DW_OP_breg22:
3349     case DW_OP_breg23:
3350     case DW_OP_breg24:
3351     case DW_OP_breg25:
3352     case DW_OP_breg26:
3353     case DW_OP_breg27:
3354     case DW_OP_breg28:
3355     case DW_OP_breg29:
3356     case DW_OP_breg30:
3357     case DW_OP_breg31:
3358       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3359       break;
3360     case DW_OP_regx:
3361       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3362       break;
3363     case DW_OP_fbreg:
3364       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3365       break;
3366     case DW_OP_bregx:
3367       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3368       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3369       break;
3370     case DW_OP_piece:
3371       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3372       break;
3373     case DW_OP_deref_size:
3374     case DW_OP_xderef_size:
3375       size += 1;
3376       break;
3377     case DW_OP_call2:
3378       size += 2;
3379       break;
3380     case DW_OP_call4:
3381       size += 4;
3382       break;
3383     case DW_OP_call_ref:
3384       size += DWARF2_ADDR_SIZE;
3385       break;
3386     default:
3387       break;
3388     }
3389
3390   return size;
3391 }
3392
3393 /* Return the size of a series of location descriptors.  */
3394
3395 static unsigned long
3396 size_of_locs (dw_loc_descr_ref loc)
3397 {
3398   dw_loc_descr_ref l;
3399   unsigned long size;
3400
3401   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3402      field, to avoid writing to a PCH file.  */
3403   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3404     {
3405       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3406         break;
3407       size += size_of_loc_descr (l);
3408     }
3409   if (! l)
3410     return size;
3411
3412   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3413     {
3414       l->dw_loc_addr = size;
3415       size += size_of_loc_descr (l);
3416     }
3417
3418   return size;
3419 }
3420
3421 /* Output location description stack opcode's operands (if any).  */
3422
3423 static void
3424 output_loc_operands (dw_loc_descr_ref loc)
3425 {
3426   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3427   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3428
3429   switch (loc->dw_loc_opc)
3430     {
3431 #ifdef DWARF2_DEBUGGING_INFO
3432     case DW_OP_addr:
3433       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3434       break;
3435     case DW_OP_const2u:
3436     case DW_OP_const2s:
3437       dw2_asm_output_data (2, val1->v.val_int, NULL);
3438       break;
3439     case DW_OP_const4u:
3440     case DW_OP_const4s:
3441       dw2_asm_output_data (4, val1->v.val_int, NULL);
3442       break;
3443     case DW_OP_const8u:
3444     case DW_OP_const8s:
3445       gcc_assert (HOST_BITS_PER_LONG >= 64);
3446       dw2_asm_output_data (8, val1->v.val_int, NULL);
3447       break;
3448     case DW_OP_skip:
3449     case DW_OP_bra:
3450       {
3451         int offset;
3452
3453         gcc_assert (val1->val_class == dw_val_class_loc);
3454         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3455
3456         dw2_asm_output_data (2, offset, NULL);
3457       }
3458       break;
3459 #else
3460     case DW_OP_addr:
3461     case DW_OP_const2u:
3462     case DW_OP_const2s:
3463     case DW_OP_const4u:
3464     case DW_OP_const4s:
3465     case DW_OP_const8u:
3466     case DW_OP_const8s:
3467     case DW_OP_skip:
3468     case DW_OP_bra:
3469       /* We currently don't make any attempt to make sure these are
3470          aligned properly like we do for the main unwind info, so
3471          don't support emitting things larger than a byte if we're
3472          only doing unwinding.  */
3473       gcc_unreachable ();
3474 #endif
3475     case DW_OP_const1u:
3476     case DW_OP_const1s:
3477       dw2_asm_output_data (1, val1->v.val_int, NULL);
3478       break;
3479     case DW_OP_constu:
3480       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3481       break;
3482     case DW_OP_consts:
3483       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3484       break;
3485     case DW_OP_pick:
3486       dw2_asm_output_data (1, val1->v.val_int, NULL);
3487       break;
3488     case DW_OP_plus_uconst:
3489       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3490       break;
3491     case DW_OP_breg0:
3492     case DW_OP_breg1:
3493     case DW_OP_breg2:
3494     case DW_OP_breg3:
3495     case DW_OP_breg4:
3496     case DW_OP_breg5:
3497     case DW_OP_breg6:
3498     case DW_OP_breg7:
3499     case DW_OP_breg8:
3500     case DW_OP_breg9:
3501     case DW_OP_breg10:
3502     case DW_OP_breg11:
3503     case DW_OP_breg12:
3504     case DW_OP_breg13:
3505     case DW_OP_breg14:
3506     case DW_OP_breg15:
3507     case DW_OP_breg16:
3508     case DW_OP_breg17:
3509     case DW_OP_breg18:
3510     case DW_OP_breg19:
3511     case DW_OP_breg20:
3512     case DW_OP_breg21:
3513     case DW_OP_breg22:
3514     case DW_OP_breg23:
3515     case DW_OP_breg24:
3516     case DW_OP_breg25:
3517     case DW_OP_breg26:
3518     case DW_OP_breg27:
3519     case DW_OP_breg28:
3520     case DW_OP_breg29:
3521     case DW_OP_breg30:
3522     case DW_OP_breg31:
3523       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3524       break;
3525     case DW_OP_regx:
3526       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3527       break;
3528     case DW_OP_fbreg:
3529       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3530       break;
3531     case DW_OP_bregx:
3532       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3533       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3534       break;
3535     case DW_OP_piece:
3536       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3537       break;
3538     case DW_OP_deref_size:
3539     case DW_OP_xderef_size:
3540       dw2_asm_output_data (1, val1->v.val_int, NULL);
3541       break;
3542
3543     case INTERNAL_DW_OP_tls_addr:
3544       if (targetm.asm_out.output_dwarf_dtprel)
3545         {
3546           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3547                                                DWARF2_ADDR_SIZE,
3548                                                val1->v.val_addr);
3549           fputc ('\n', asm_out_file);
3550         }
3551       else
3552         gcc_unreachable ();
3553       break;
3554
3555     default:
3556       /* Other codes have no operands.  */
3557       break;
3558     }
3559 }
3560
3561 /* Output a sequence of location operations.  */
3562
3563 static void
3564 output_loc_sequence (dw_loc_descr_ref loc)
3565 {
3566   for (; loc != NULL; loc = loc->dw_loc_next)
3567     {
3568       /* Output the opcode.  */
3569       dw2_asm_output_data (1, loc->dw_loc_opc,
3570                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3571
3572       /* Output the operand(s) (if any).  */
3573       output_loc_operands (loc);
3574     }
3575 }
3576
3577 /* This routine will generate the correct assembly data for a location
3578    description based on a cfi entry with a complex address.  */
3579
3580 static void
3581 output_cfa_loc (dw_cfi_ref cfi)
3582 {
3583   dw_loc_descr_ref loc;
3584   unsigned long size;
3585
3586   /* Output the size of the block.  */
3587   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3588   size = size_of_locs (loc);
3589   dw2_asm_output_data_uleb128 (size, NULL);
3590
3591   /* Now output the operations themselves.  */
3592   output_loc_sequence (loc);
3593 }
3594
3595 /* This function builds a dwarf location descriptor sequence from a
3596    dw_cfa_location, adding the given OFFSET to the result of the
3597    expression.  */
3598
3599 static struct dw_loc_descr_struct *
3600 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3601 {
3602   struct dw_loc_descr_struct *head, *tmp;
3603
3604   offset += cfa->offset;
3605
3606   if (cfa->indirect)
3607     {
3608       if (cfa->base_offset)
3609         {
3610           if (cfa->reg <= 31)
3611             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3612           else
3613             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3614         }
3615       else if (cfa->reg <= 31)
3616         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3617       else
3618         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3619
3620       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3621       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3622       add_loc_descr (&head, tmp);
3623       if (offset != 0)
3624         {
3625           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3626           add_loc_descr (&head, tmp);
3627         }
3628     }
3629   else
3630     {
3631       if (offset == 0)
3632         if (cfa->reg <= 31)
3633           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3634         else
3635           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3636       else if (cfa->reg <= 31)
3637         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3638       else
3639         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3640     }
3641
3642   return head;
3643 }
3644
3645 /* This function fills in aa dw_cfa_location structure from a dwarf location
3646    descriptor sequence.  */
3647
3648 static void
3649 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3650 {
3651   struct dw_loc_descr_struct *ptr;
3652   cfa->offset = 0;
3653   cfa->base_offset = 0;
3654   cfa->indirect = 0;
3655   cfa->reg = -1;
3656
3657   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3658     {
3659       enum dwarf_location_atom op = ptr->dw_loc_opc;
3660
3661       switch (op)
3662         {
3663         case DW_OP_reg0:
3664         case DW_OP_reg1:
3665         case DW_OP_reg2:
3666         case DW_OP_reg3:
3667         case DW_OP_reg4:
3668         case DW_OP_reg5:
3669         case DW_OP_reg6:
3670         case DW_OP_reg7:
3671         case DW_OP_reg8:
3672         case DW_OP_reg9:
3673         case DW_OP_reg10:
3674         case DW_OP_reg11:
3675         case DW_OP_reg12:
3676         case DW_OP_reg13:
3677         case DW_OP_reg14:
3678         case DW_OP_reg15:
3679         case DW_OP_reg16:
3680         case DW_OP_reg17:
3681         case DW_OP_reg18:
3682         case DW_OP_reg19:
3683         case DW_OP_reg20:
3684         case DW_OP_reg21:
3685         case DW_OP_reg22:
3686         case DW_OP_reg23:
3687         case DW_OP_reg24:
3688         case DW_OP_reg25:
3689         case DW_OP_reg26:
3690         case DW_OP_reg27:
3691         case DW_OP_reg28:
3692         case DW_OP_reg29:
3693         case DW_OP_reg30:
3694         case DW_OP_reg31:
3695           cfa->reg = op - DW_OP_reg0;
3696           break;
3697         case DW_OP_regx:
3698           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3699           break;
3700         case DW_OP_breg0:
3701         case DW_OP_breg1:
3702         case DW_OP_breg2:
3703         case DW_OP_breg3:
3704         case DW_OP_breg4:
3705         case DW_OP_breg5:
3706         case DW_OP_breg6:
3707         case DW_OP_breg7:
3708         case DW_OP_breg8:
3709         case DW_OP_breg9:
3710         case DW_OP_breg10:
3711         case DW_OP_breg11:
3712         case DW_OP_breg12:
3713         case DW_OP_breg13:
3714         case DW_OP_breg14:
3715         case DW_OP_breg15:
3716         case DW_OP_breg16:
3717         case DW_OP_breg17:
3718         case DW_OP_breg18:
3719         case DW_OP_breg19:
3720         case DW_OP_breg20:
3721         case DW_OP_breg21:
3722         case DW_OP_breg22:
3723         case DW_OP_breg23:
3724         case DW_OP_breg24:
3725         case DW_OP_breg25:
3726         case DW_OP_breg26:
3727         case DW_OP_breg27:
3728         case DW_OP_breg28:
3729         case DW_OP_breg29:
3730         case DW_OP_breg30:
3731         case DW_OP_breg31:
3732           cfa->reg = op - DW_OP_breg0;
3733           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3734           break;
3735         case DW_OP_bregx:
3736           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3737           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3738           break;
3739         case DW_OP_deref:
3740           cfa->indirect = 1;
3741           break;
3742         case DW_OP_plus_uconst:
3743           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3744           break;
3745         default:
3746           internal_error ("DW_LOC_OP %s not implemented",
3747                           dwarf_stack_op_name (ptr->dw_loc_opc));
3748         }
3749     }
3750 }
3751 #endif /* .debug_frame support */
3752 \f
3753 /* And now, the support for symbolic debugging information.  */
3754 #ifdef DWARF2_DEBUGGING_INFO
3755
3756 /* .debug_str support.  */
3757 static int output_indirect_string (void **, void *);
3758
3759 static void dwarf2out_init (const char *);
3760 static void dwarf2out_finish (const char *);
3761 static void dwarf2out_define (unsigned int, const char *);
3762 static void dwarf2out_undef (unsigned int, const char *);
3763 static void dwarf2out_start_source_file (unsigned, const char *);
3764 static void dwarf2out_end_source_file (unsigned);
3765 static void dwarf2out_begin_block (unsigned, unsigned);
3766 static void dwarf2out_end_block (unsigned, unsigned);
3767 static bool dwarf2out_ignore_block (const_tree);
3768 static void dwarf2out_global_decl (tree);
3769 static void dwarf2out_type_decl (tree, int);
3770 static void dwarf2out_imported_module_or_decl (tree, tree);
3771 static void dwarf2out_abstract_function (tree);
3772 static void dwarf2out_var_location (rtx);
3773 static void dwarf2out_begin_function (tree);
3774
3775 /* The debug hooks structure.  */
3776
3777 const struct gcc_debug_hooks dwarf2_debug_hooks =
3778 {
3779   dwarf2out_init,
3780   dwarf2out_finish,
3781   dwarf2out_define,
3782   dwarf2out_undef,
3783   dwarf2out_start_source_file,
3784   dwarf2out_end_source_file,
3785   dwarf2out_begin_block,
3786   dwarf2out_end_block,
3787   dwarf2out_ignore_block,
3788   dwarf2out_source_line,
3789   dwarf2out_begin_prologue,
3790   debug_nothing_int_charstar,   /* end_prologue */
3791   dwarf2out_end_epilogue,
3792   dwarf2out_begin_function,
3793   debug_nothing_int,            /* end_function */
3794   dwarf2out_decl,               /* function_decl */
3795   dwarf2out_global_decl,
3796   dwarf2out_type_decl,          /* type_decl */
3797   dwarf2out_imported_module_or_decl,
3798   debug_nothing_tree,           /* deferred_inline_function */
3799   /* The DWARF 2 backend tries to reduce debugging bloat by not
3800      emitting the abstract description of inline functions until
3801      something tries to reference them.  */
3802   dwarf2out_abstract_function,  /* outlining_inline_function */
3803   debug_nothing_rtx,            /* label */
3804   debug_nothing_int,            /* handle_pch */
3805   dwarf2out_var_location,
3806   dwarf2out_switch_text_section,
3807   1                             /* start_end_main_source_file */
3808 };
3809 #endif
3810 \f
3811 /* NOTE: In the comments in this file, many references are made to
3812    "Debugging Information Entries".  This term is abbreviated as `DIE'
3813    throughout the remainder of this file.  */
3814
3815 /* An internal representation of the DWARF output is built, and then
3816    walked to generate the DWARF debugging info.  The walk of the internal
3817    representation is done after the entire program has been compiled.
3818    The types below are used to describe the internal representation.  */
3819
3820 /* Various DIE's use offsets relative to the beginning of the
3821    .debug_info section to refer to each other.  */
3822
3823 typedef long int dw_offset;
3824
3825 /* Define typedefs here to avoid circular dependencies.  */
3826
3827 typedef struct dw_attr_struct *dw_attr_ref;
3828 typedef struct dw_line_info_struct *dw_line_info_ref;
3829 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3830 typedef struct pubname_struct *pubname_ref;
3831 typedef struct dw_ranges_struct *dw_ranges_ref;
3832 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3833
3834 /* Each entry in the line_info_table maintains the file and
3835    line number associated with the label generated for that
3836    entry.  The label gives the PC value associated with
3837    the line number entry.  */
3838
3839 typedef struct dw_line_info_struct GTY(())
3840 {
3841   unsigned long dw_file_num;
3842   unsigned long dw_line_num;
3843 }
3844 dw_line_info_entry;
3845
3846 /* Line information for functions in separate sections; each one gets its
3847    own sequence.  */
3848 typedef struct dw_separate_line_info_struct GTY(())
3849 {
3850   unsigned long dw_file_num;
3851   unsigned long dw_line_num;
3852   unsigned long function;
3853 }
3854 dw_separate_line_info_entry;
3855
3856 /* Each DIE attribute has a field specifying the attribute kind,
3857    a link to the next attribute in the chain, and an attribute value.
3858    Attributes are typically linked below the DIE they modify.  */
3859
3860 typedef struct dw_attr_struct GTY(())
3861 {
3862   enum dwarf_attribute dw_attr;
3863   dw_val_node dw_attr_val;
3864 }
3865 dw_attr_node;
3866
3867 DEF_VEC_O(dw_attr_node);
3868 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3869
3870 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3871    The children of each node form a circular list linked by
3872    die_sib.  die_child points to the node *before* the "first" child node.  */
3873
3874 typedef struct die_struct GTY((chain_circular ("%h.die_sib")))
3875 {
3876   enum dwarf_tag die_tag;
3877   char *die_symbol;
3878   VEC(dw_attr_node,gc) * die_attr;
3879   dw_die_ref die_parent;
3880   dw_die_ref die_child;
3881   dw_die_ref die_sib;
3882   dw_die_ref die_definition; /* ref from a specification to its definition */
3883   dw_offset die_offset;
3884   unsigned long die_abbrev;
3885   int die_mark;
3886   /* Die is used and must not be pruned as unused.  */
3887   int die_perennial_p;
3888   unsigned int decl_id;
3889 }
3890 die_node;
3891
3892 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3893 #define FOR_EACH_CHILD(die, c, expr) do {       \
3894   c = die->die_child;                           \
3895   if (c) do {                                   \
3896     c = c->die_sib;                             \
3897     expr;                                       \
3898   } while (c != die->die_child);                \
3899 } while (0)
3900
3901 /* The pubname structure */
3902
3903 typedef struct pubname_struct GTY(())
3904 {
3905   dw_die_ref die;
3906   const char *name;
3907 }
3908 pubname_entry;
3909
3910 DEF_VEC_O(pubname_entry);
3911 DEF_VEC_ALLOC_O(pubname_entry, gc);
3912
3913 struct dw_ranges_struct GTY(())
3914 {
3915   /* If this is positive, it's a block number, otherwise it's a
3916      bitwise-negated index into dw_ranges_by_label.  */
3917   int num;
3918 };
3919
3920 struct dw_ranges_by_label_struct GTY(())
3921 {
3922   const char *begin;
3923   const char *end;
3924 };
3925
3926 /* The limbo die list structure.  */
3927 typedef struct limbo_die_struct GTY(())
3928 {
3929   dw_die_ref die;
3930   tree created_for;
3931   struct limbo_die_struct *next;
3932 }
3933 limbo_die_node;
3934
3935 /* How to start an assembler comment.  */
3936 #ifndef ASM_COMMENT_START
3937 #define ASM_COMMENT_START ";#"
3938 #endif
3939
3940 /* Define a macro which returns nonzero for a TYPE_DECL which was
3941    implicitly generated for a tagged type.
3942
3943    Note that unlike the gcc front end (which generates a NULL named
3944    TYPE_DECL node for each complete tagged type, each array type, and
3945    each function type node created) the g++ front end generates a
3946    _named_ TYPE_DECL node for each tagged type node created.
3947    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3948    generate a DW_TAG_typedef DIE for them.  */
3949
3950 #define TYPE_DECL_IS_STUB(decl)                         \
3951   (DECL_NAME (decl) == NULL_TREE                        \
3952    || (DECL_ARTIFICIAL (decl)                           \
3953        && is_tagged_type (TREE_TYPE (decl))             \
3954        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3955            /* This is necessary for stub decls that     \
3956               appear in nested inline functions.  */    \
3957            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3958                && (decl_ultimate_origin (decl)          \
3959                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3960
3961 /* Information concerning the compilation unit's programming
3962    language, and compiler version.  */
3963
3964 /* Fixed size portion of the DWARF compilation unit header.  */
3965 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3966   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3967
3968 /* Fixed size portion of public names info.  */
3969 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3970
3971 /* Fixed size portion of the address range info.  */
3972 #define DWARF_ARANGES_HEADER_SIZE                                       \
3973   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3974                 DWARF2_ADDR_SIZE * 2)                                   \
3975    - DWARF_INITIAL_LENGTH_SIZE)
3976
3977 /* Size of padding portion in the address range info.  It must be
3978    aligned to twice the pointer size.  */
3979 #define DWARF_ARANGES_PAD_SIZE \
3980   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3981                 DWARF2_ADDR_SIZE * 2)                              \
3982    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3983
3984 /* Use assembler line directives if available.  */
3985 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3986 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3987 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3988 #else
3989 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3990 #endif
3991 #endif
3992
3993 /* Minimum line offset in a special line info. opcode.
3994    This value was chosen to give a reasonable range of values.  */
3995 #define DWARF_LINE_BASE  -10
3996
3997 /* First special line opcode - leave room for the standard opcodes.  */
3998 #define DWARF_LINE_OPCODE_BASE  10
3999
4000 /* Range of line offsets in a special line info. opcode.  */
4001 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
4002
4003 /* Flag that indicates the initial value of the is_stmt_start flag.
4004    In the present implementation, we do not mark any lines as
4005    the beginning of a source statement, because that information
4006    is not made available by the GCC front-end.  */
4007 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
4008
4009 #ifdef DWARF2_DEBUGGING_INFO
4010 /* This location is used by calc_die_sizes() to keep track
4011    the offset of each DIE within the .debug_info section.  */
4012 static unsigned long next_die_offset;
4013 #endif
4014
4015 /* Record the root of the DIE's built for the current compilation unit.  */
4016 static GTY(()) dw_die_ref comp_unit_die;
4017
4018 /* A list of DIEs with a NULL parent waiting to be relocated.  */
4019 static GTY(()) limbo_die_node *limbo_die_list;
4020
4021 /* Filenames referenced by this compilation unit.  */
4022 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
4023
4024 /* A hash table of references to DIE's that describe declarations.
4025    The key is a DECL_UID() which is a unique number identifying each decl.  */
4026 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
4027
4028 /* Node of the variable location list.  */
4029 struct var_loc_node GTY ((chain_next ("%h.next")))
4030 {
4031   rtx GTY (()) var_loc_note;
4032   const char * GTY (()) label;
4033   const char * GTY (()) section_label;
4034   struct var_loc_node * GTY (()) next;
4035 };
4036
4037 /* Variable location list.  */
4038 struct var_loc_list_def GTY (())
4039 {
4040   struct var_loc_node * GTY (()) first;
4041
4042   /* Do not mark the last element of the chained list because
4043      it is marked through the chain.  */
4044   struct var_loc_node * GTY ((skip ("%h"))) last;
4045
4046   /* DECL_UID of the variable decl.  */
4047   unsigned int decl_id;
4048 };
4049 typedef struct var_loc_list_def var_loc_list;
4050
4051
4052 /* Table of decl location linked lists.  */
4053 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4054
4055 /* A pointer to the base of a list of references to DIE's that
4056    are uniquely identified by their tag, presence/absence of
4057    children DIE's, and list of attribute/value pairs.  */
4058 static GTY((length ("abbrev_die_table_allocated")))
4059   dw_die_ref *abbrev_die_table;
4060
4061 /* Number of elements currently allocated for abbrev_die_table.  */
4062 static GTY(()) unsigned abbrev_die_table_allocated;
4063
4064 /* Number of elements in type_die_table currently in use.  */
4065 static GTY(()) unsigned abbrev_die_table_in_use;
4066
4067 /* Size (in elements) of increments by which we may expand the
4068    abbrev_die_table.  */
4069 #define ABBREV_DIE_TABLE_INCREMENT 256
4070
4071 /* A pointer to the base of a table that contains line information
4072    for each source code line in .text in the compilation unit.  */
4073 static GTY((length ("line_info_table_allocated")))
4074      dw_line_info_ref line_info_table;
4075
4076 /* Number of elements currently allocated for line_info_table.  */
4077 static GTY(()) unsigned line_info_table_allocated;
4078
4079 /* Number of elements in line_info_table currently in use.  */
4080 static GTY(()) unsigned line_info_table_in_use;
4081
4082 /* A pointer to the base of a table that contains line information
4083    for each source code line outside of .text in the compilation unit.  */
4084 static GTY ((length ("separate_line_info_table_allocated")))
4085      dw_separate_line_info_ref separate_line_info_table;
4086
4087 /* Number of elements currently allocated for separate_line_info_table.  */
4088 static GTY(()) unsigned separate_line_info_table_allocated;
4089
4090 /* Number of elements in separate_line_info_table currently in use.  */
4091 static GTY(()) unsigned separate_line_info_table_in_use;
4092
4093 /* Size (in elements) of increments by which we may expand the
4094    line_info_table.  */
4095 #define LINE_INFO_TABLE_INCREMENT 1024
4096
4097 /* A pointer to the base of a table that contains a list of publicly
4098    accessible names.  */
4099 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4100
4101 /* A pointer to the base of a table that contains a list of publicly
4102    accessible types.  */
4103 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4104
4105 /* Array of dies for which we should generate .debug_arange info.  */
4106 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4107
4108 /* Number of elements currently allocated for arange_table.  */
4109 static GTY(()) unsigned arange_table_allocated;
4110
4111 /* Number of elements in arange_table currently in use.  */
4112 static GTY(()) unsigned arange_table_in_use;
4113
4114 /* Size (in elements) of increments by which we may expand the
4115    arange_table.  */
4116 #define ARANGE_TABLE_INCREMENT 64
4117
4118 /* Array of dies for which we should generate .debug_ranges info.  */
4119 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4120
4121 /* Number of elements currently allocated for ranges_table.  */
4122 static GTY(()) unsigned ranges_table_allocated;
4123
4124 /* Number of elements in ranges_table currently in use.  */
4125 static GTY(()) unsigned ranges_table_in_use;
4126
4127 /* Array of pairs of labels referenced in ranges_table.  */
4128 static GTY ((length ("ranges_by_label_allocated")))
4129      dw_ranges_by_label_ref ranges_by_label;
4130
4131 /* Number of elements currently allocated for ranges_by_label.  */
4132 static GTY(()) unsigned ranges_by_label_allocated;
4133
4134 /* Number of elements in ranges_by_label currently in use.  */
4135 static GTY(()) unsigned ranges_by_label_in_use;
4136
4137 /* Size (in elements) of increments by which we may expand the
4138    ranges_table.  */
4139 #define RANGES_TABLE_INCREMENT 64
4140
4141 /* Whether we have location lists that need outputting */
4142 static GTY(()) bool have_location_lists;
4143
4144 /* Unique label counter.  */
4145 static GTY(()) unsigned int loclabel_num;
4146
4147 #ifdef DWARF2_DEBUGGING_INFO
4148 /* Record whether the function being analyzed contains inlined functions.  */
4149 static int current_function_has_inlines;
4150 #endif
4151 #if 0 && defined (MIPS_DEBUGGING_INFO)
4152 static int comp_unit_has_inlines;
4153 #endif
4154
4155 /* The last file entry emitted by maybe_emit_file().  */
4156 static GTY(()) struct dwarf_file_data * last_emitted_file;
4157
4158 /* Number of internal labels generated by gen_internal_sym().  */
4159 static GTY(()) int label_num;
4160
4161 /* Cached result of previous call to lookup_filename.  */
4162 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4163
4164 #ifdef DWARF2_DEBUGGING_INFO
4165
4166 /* Offset from the "steady-state frame pointer" to the frame base,
4167    within the current function.  */
4168 static HOST_WIDE_INT frame_pointer_fb_offset;
4169
4170 /* Forward declarations for functions defined in this file.  */
4171
4172 static int is_pseudo_reg (const_rtx);
4173 static tree type_main_variant (tree);
4174 static int is_tagged_type (const_tree);
4175 static const char *dwarf_tag_name (unsigned);
4176 static const char *dwarf_attr_name (unsigned);
4177 static const char *dwarf_form_name (unsigned);
4178 static tree decl_ultimate_origin (const_tree);
4179 static tree block_ultimate_origin (const_tree);
4180 static tree decl_class_context (tree);
4181 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4182 static inline enum dw_val_class AT_class (dw_attr_ref);
4183 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4184 static inline unsigned AT_flag (dw_attr_ref);
4185 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4186 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4187 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4188 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4189 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4190                               unsigned long);
4191 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4192                                unsigned int, unsigned char *);
4193 static hashval_t debug_str_do_hash (const void *);
4194 static int debug_str_eq (const void *, const void *);
4195 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4196 static inline const char *AT_string (dw_attr_ref);
4197 static int AT_string_form (dw_attr_ref);
4198 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4199 static void add_AT_specification (dw_die_ref, dw_die_ref);
4200 static inline dw_die_ref AT_ref (dw_attr_ref);
4201 static inline int AT_ref_external (dw_attr_ref);
4202 static inline void set_AT_ref_external (dw_attr_ref, int);
4203 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4204 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4205 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4206 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4207                              dw_loc_list_ref);
4208 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4209 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4210 static inline rtx AT_addr (dw_attr_ref);
4211 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4212 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4213 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4214 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4215                            unsigned HOST_WIDE_INT);
4216 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4217                                unsigned long);
4218 static inline const char *AT_lbl (dw_attr_ref);
4219 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4220 static const char *get_AT_low_pc (dw_die_ref);
4221 static const char *get_AT_hi_pc (dw_die_ref);
4222 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4223 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4224 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4225 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4226 static bool is_c_family (void);
4227 static bool is_cxx (void);
4228 static bool is_java (void);
4229 static bool is_fortran (void);
4230 static bool is_ada (void);
4231 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4232 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4233 static void add_child_die (dw_die_ref, dw_die_ref);
4234 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4235 static dw_die_ref lookup_type_die (tree);
4236 static void equate_type_number_to_die (tree, dw_die_ref);
4237 static hashval_t decl_die_table_hash (const void *);
4238 static int decl_die_table_eq (const void *, const void *);
4239 static dw_die_ref lookup_decl_die (tree);
4240 static hashval_t decl_loc_table_hash (const void *);
4241 static int decl_loc_table_eq (const void *, const void *);
4242 static var_loc_list *lookup_decl_loc (const_tree);
4243 static void equate_decl_number_to_die (tree, dw_die_ref);
4244 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4245 static void print_spaces (FILE *);
4246 static void print_die (dw_die_ref, FILE *);
4247 static void print_dwarf_line_table (FILE *);
4248 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4249 static dw_die_ref pop_compile_unit (dw_die_ref);
4250 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4251 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4252 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4253 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4254 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4255 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4256 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4257 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4258 static void compute_section_prefix (dw_die_ref);
4259 static int is_type_die (dw_die_ref);
4260 static int is_comdat_die (dw_die_ref);
4261 static int is_symbol_die (dw_die_ref);
4262 static void assign_symbol_names (dw_die_ref);
4263 static void break_out_includes (dw_die_ref);
4264 static hashval_t htab_cu_hash (const void *);
4265 static int htab_cu_eq (const void *, const void *);
4266 static void htab_cu_del (void *);
4267 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4268 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4269 static void add_sibling_attributes (dw_die_ref);
4270 static void build_abbrev_table (dw_die_ref);
4271 static void output_location_lists (dw_die_ref);
4272 static int constant_size (long unsigned);
4273 static unsigned long size_of_die (dw_die_ref);
4274 static void calc_die_sizes (dw_die_ref);
4275 static void mark_dies (dw_die_ref);
4276 static void unmark_dies (dw_die_ref);
4277 static void unmark_all_dies (dw_die_ref);
4278 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4279 static unsigned long size_of_aranges (void);
4280 static enum dwarf_form value_format (dw_attr_ref);
4281 static void output_value_format (dw_attr_ref);
4282 static void output_abbrev_section (void);
4283 static void output_die_symbol (dw_die_ref);
4284 static void output_die (dw_die_ref);
4285 static void output_compilation_unit_header (void);
4286 static void output_comp_unit (dw_die_ref, int);
4287 static const char *dwarf2_name (tree, int);
4288 static void add_pubname (tree, dw_die_ref);
4289 static void add_pubname_string (const char *, dw_die_ref);
4290 static void add_pubtype (tree, dw_die_ref);
4291 static void output_pubnames (VEC (pubname_entry,gc) *);
4292 static void add_arange (tree, dw_die_ref);
4293 static void output_aranges (void);
4294 static unsigned int add_ranges_num (int);
4295 static unsigned int add_ranges (const_tree);
4296 static unsigned int add_ranges_by_labels (const char *, const char *);
4297 static void output_ranges (void);
4298 static void output_line_info (void);
4299 static void output_file_names (void);
4300 static dw_die_ref base_type_die (tree);
4301 static int is_base_type (tree);
4302 static bool is_subrange_type (const_tree);
4303 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4304 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4305 static int type_is_enum (const_tree);
4306 static unsigned int dbx_reg_number (const_rtx);
4307 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4308 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4309 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
4310                                                 enum var_init_status);
4311 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4312                                                      enum var_init_status);
4313 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4314 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4315                                          enum var_init_status);
4316 static int is_based_loc (const_rtx);
4317 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4318                                             enum var_init_status);
4319 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4320                                                enum var_init_status);
4321 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4322 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4323 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4324 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4325 static tree field_type (const_tree);
4326 static unsigned int simple_type_align_in_bits (const_tree);
4327 static unsigned int simple_decl_align_in_bits (const_tree);
4328 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4329 static HOST_WIDE_INT field_byte_offset (const_tree);
4330 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4331                                          dw_loc_descr_ref);
4332 static void add_data_member_location_attribute (dw_die_ref, tree);
4333 static void add_const_value_attribute (dw_die_ref, rtx);
4334 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4335 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4336 static void insert_float (const_rtx, unsigned char *);
4337 static rtx rtl_for_decl_location (tree);
4338 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4339                                                    enum dwarf_attribute);
4340 static void tree_add_const_value_attribute (dw_die_ref, tree);
4341 static void add_name_attribute (dw_die_ref, const char *);
4342 static void add_comp_dir_attribute (dw_die_ref);
4343 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4344 static void add_subscript_info (dw_die_ref, tree, bool);
4345 static void add_byte_size_attribute (dw_die_ref, tree);
4346 static void add_bit_offset_attribute (dw_die_ref, tree);
4347 static void add_bit_size_attribute (dw_die_ref, tree);
4348 static void add_prototyped_attribute (dw_die_ref, tree);
4349 static void add_abstract_origin_attribute (dw_die_ref, tree);
4350 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4351 static void add_src_coords_attributes (dw_die_ref, tree);
4352 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4353 static void push_decl_scope (tree);
4354 static void pop_decl_scope (void);
4355 static dw_die_ref scope_die_for (tree, dw_die_ref);
4356 static inline int local_scope_p (dw_die_ref);
4357 static inline int class_or_namespace_scope_p (dw_die_ref);
4358 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4359 static void add_calling_convention_attribute (dw_die_ref, tree);
4360 static const char *type_tag (const_tree);
4361 static tree member_declared_type (const_tree);
4362 #if 0
4363 static const char *decl_start_label (tree);
4364 #endif
4365 static void gen_array_type_die (tree, dw_die_ref);
4366 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4367 #if 0
4368 static void gen_entry_point_die (tree, dw_die_ref);
4369 #endif
4370 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4371 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4372 static void gen_inlined_union_type_die (tree, dw_die_ref);
4373 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4374 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4375 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4376 static void gen_formal_types_die (tree, dw_die_ref);
4377 static void gen_subprogram_die (tree, dw_die_ref);
4378 static void gen_variable_die (tree, dw_die_ref);
4379 static void gen_label_die (tree, dw_die_ref);
4380 static void gen_lexical_block_die (tree, dw_die_ref, int);
4381 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4382 static void gen_field_die (tree, dw_die_ref);
4383 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4384 static dw_die_ref gen_compile_unit_die (const char *);
4385 static void gen_inheritance_die (tree, tree, dw_die_ref);
4386 static void gen_member_die (tree, dw_die_ref);
4387 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4388                                                 enum debug_info_usage);
4389 static void gen_subroutine_type_die (tree, dw_die_ref);
4390 static void gen_typedef_die (tree, dw_die_ref);
4391 static void gen_type_die (tree, dw_die_ref);
4392 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4393 static void gen_block_die (tree, dw_die_ref, int);
4394 static void decls_for_scope (tree, dw_die_ref, int);
4395 static int is_redundant_typedef (const_tree);
4396 static void gen_namespace_die (tree);
4397 static void gen_decl_die (tree, dw_die_ref);
4398 static dw_die_ref force_decl_die (tree);
4399 static dw_die_ref force_type_die (tree);
4400 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4401 static void declare_in_namespace (tree, dw_die_ref);
4402 static struct dwarf_file_data * lookup_filename (const char *);
4403 static void retry_incomplete_types (void);
4404 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4405 static void splice_child_die (dw_die_ref, dw_die_ref);
4406 static int file_info_cmp (const void *, const void *);
4407 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4408                                      const char *, const char *, unsigned);
4409 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4410                                        const char *, const char *,
4411                                        const char *);
4412 static void output_loc_list (dw_loc_list_ref);
4413 static char *gen_internal_sym (const char *);
4414
4415 static void prune_unmark_dies (dw_die_ref);
4416 static void prune_unused_types_mark (dw_die_ref, int);
4417 static void prune_unused_types_walk (dw_die_ref);
4418 static void prune_unused_types_walk_attribs (dw_die_ref);
4419 static void prune_unused_types_prune (dw_die_ref);
4420 static void prune_unused_types (void);
4421 static int maybe_emit_file (struct dwarf_file_data *fd);
4422
4423 /* Section names used to hold DWARF debugging information.  */
4424 #ifndef DEBUG_INFO_SECTION
4425 #define DEBUG_INFO_SECTION      ".debug_info"
4426 #endif
4427 #ifndef DEBUG_ABBREV_SECTION
4428 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4429 #endif
4430 #ifndef DEBUG_ARANGES_SECTION
4431 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4432 #endif
4433 #ifndef DEBUG_MACINFO_SECTION
4434 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4435 #endif
4436 #ifndef DEBUG_LINE_SECTION
4437 #define DEBUG_LINE_SECTION      ".debug_line"
4438 #endif
4439 #ifndef DEBUG_LOC_SECTION
4440 #define DEBUG_LOC_SECTION       ".debug_loc"
4441 #endif
4442 #ifndef DEBUG_PUBNAMES_SECTION
4443 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4444 #endif
4445 #ifndef DEBUG_STR_SECTION
4446 #define DEBUG_STR_SECTION       ".debug_str"
4447 #endif
4448 #ifndef DEBUG_RANGES_SECTION
4449 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4450 #endif
4451
4452 /* Standard ELF section names for compiled code and data.  */
4453 #ifndef TEXT_SECTION_NAME
4454 #define TEXT_SECTION_NAME       ".text"
4455 #endif
4456
4457 /* Section flags for .debug_str section.  */
4458 #define DEBUG_STR_SECTION_FLAGS \
4459   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4460    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4461    : SECTION_DEBUG)
4462
4463 /* Labels we insert at beginning sections we can reference instead of
4464    the section names themselves.  */
4465
4466 #ifndef TEXT_SECTION_LABEL
4467 #define TEXT_SECTION_LABEL              "Ltext"
4468 #endif
4469 #ifndef COLD_TEXT_SECTION_LABEL
4470 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4471 #endif
4472 #ifndef DEBUG_LINE_SECTION_LABEL
4473 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4474 #endif
4475 #ifndef DEBUG_INFO_SECTION_LABEL
4476 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4477 #endif
4478 #ifndef DEBUG_ABBREV_SECTION_LABEL
4479 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4480 #endif
4481 #ifndef DEBUG_LOC_SECTION_LABEL
4482 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4483 #endif
4484 #ifndef DEBUG_RANGES_SECTION_LABEL
4485 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4486 #endif
4487 #ifndef DEBUG_MACINFO_SECTION_LABEL
4488 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4489 #endif
4490
4491 /* Definitions of defaults for formats and names of various special
4492    (artificial) labels which may be generated within this file (when the -g
4493    options is used and DWARF2_DEBUGGING_INFO is in effect.
4494    If necessary, these may be overridden from within the tm.h file, but
4495    typically, overriding these defaults is unnecessary.  */
4496
4497 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4498 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4499 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4500 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4501 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4502 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4503 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4504 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4505 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4506 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4507
4508 #ifndef TEXT_END_LABEL
4509 #define TEXT_END_LABEL          "Letext"
4510 #endif
4511 #ifndef COLD_END_LABEL
4512 #define COLD_END_LABEL          "Letext_cold"
4513 #endif
4514 #ifndef BLOCK_BEGIN_LABEL
4515 #define BLOCK_BEGIN_LABEL       "LBB"
4516 #endif
4517 #ifndef BLOCK_END_LABEL
4518 #define BLOCK_END_LABEL         "LBE"
4519 #endif
4520 #ifndef LINE_CODE_LABEL
4521 #define LINE_CODE_LABEL         "LM"
4522 #endif
4523 #ifndef SEPARATE_LINE_CODE_LABEL
4524 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4525 #endif
4526
4527 \f
4528 /* We allow a language front-end to designate a function that is to be
4529    called to "demangle" any name before it is put into a DIE.  */
4530
4531 static const char *(*demangle_name_func) (const char *);
4532
4533 void
4534 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4535 {
4536   demangle_name_func = func;
4537 }
4538
4539 /* Test if rtl node points to a pseudo register.  */
4540
4541 static inline int
4542 is_pseudo_reg (const_rtx rtl)
4543 {
4544   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4545           || (GET_CODE (rtl) == SUBREG
4546               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4547 }
4548
4549 /* Return a reference to a type, with its const and volatile qualifiers
4550    removed.  */
4551
4552 static inline tree
4553 type_main_variant (tree type)
4554 {
4555   type = TYPE_MAIN_VARIANT (type);
4556
4557   /* ??? There really should be only one main variant among any group of
4558      variants of a given type (and all of the MAIN_VARIANT values for all
4559      members of the group should point to that one type) but sometimes the C
4560      front-end messes this up for array types, so we work around that bug
4561      here.  */
4562   if (TREE_CODE (type) == ARRAY_TYPE)
4563     while (type != TYPE_MAIN_VARIANT (type))
4564       type = TYPE_MAIN_VARIANT (type);
4565
4566   return type;
4567 }
4568
4569 /* Return nonzero if the given type node represents a tagged type.  */
4570
4571 static inline int
4572 is_tagged_type (const_tree type)
4573 {
4574   enum tree_code code = TREE_CODE (type);
4575
4576   return (code == RECORD_TYPE || code == UNION_TYPE
4577           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4578 }
4579
4580 /* Convert a DIE tag into its string name.  */
4581
4582 static const char *
4583 dwarf_tag_name (unsigned int tag)
4584 {
4585   switch (tag)
4586     {
4587     case DW_TAG_padding:
4588       return "DW_TAG_padding";
4589     case DW_TAG_array_type:
4590       return "DW_TAG_array_type";
4591     case DW_TAG_class_type:
4592       return "DW_TAG_class_type";
4593     case DW_TAG_entry_point:
4594       return "DW_TAG_entry_point";
4595     case DW_TAG_enumeration_type:
4596       return "DW_TAG_enumeration_type";
4597     case DW_TAG_formal_parameter:
4598       return "DW_TAG_formal_parameter";
4599     case DW_TAG_imported_declaration:
4600       return "DW_TAG_imported_declaration";
4601     case DW_TAG_label:
4602       return "DW_TAG_label";
4603     case DW_TAG_lexical_block:
4604       return "DW_TAG_lexical_block";
4605     case DW_TAG_member:
4606       return "DW_TAG_member";
4607     case DW_TAG_pointer_type:
4608       return "DW_TAG_pointer_type";
4609     case DW_TAG_reference_type:
4610       return "DW_TAG_reference_type";
4611     case DW_TAG_compile_unit:
4612       return "DW_TAG_compile_unit";
4613     case DW_TAG_string_type:
4614       return "DW_TAG_string_type";
4615     case DW_TAG_structure_type:
4616       return "DW_TAG_structure_type";
4617     case DW_TAG_subroutine_type:
4618       return "DW_TAG_subroutine_type";
4619     case DW_TAG_typedef:
4620       return "DW_TAG_typedef";
4621     case DW_TAG_union_type:
4622       return "DW_TAG_union_type";
4623     case DW_TAG_unspecified_parameters:
4624       return "DW_TAG_unspecified_parameters";
4625     case DW_TAG_variant:
4626       return "DW_TAG_variant";
4627     case DW_TAG_common_block:
4628       return "DW_TAG_common_block";
4629     case DW_TAG_common_inclusion:
4630       return "DW_TAG_common_inclusion";
4631     case DW_TAG_inheritance:
4632       return "DW_TAG_inheritance";
4633     case DW_TAG_inlined_subroutine:
4634       return "DW_TAG_inlined_subroutine";
4635     case DW_TAG_module:
4636       return "DW_TAG_module";
4637     case DW_TAG_ptr_to_member_type:
4638       return "DW_TAG_ptr_to_member_type";
4639     case DW_TAG_set_type:
4640       return "DW_TAG_set_type";
4641     case DW_TAG_subrange_type:
4642       return "DW_TAG_subrange_type";
4643     case DW_TAG_with_stmt:
4644       return "DW_TAG_with_stmt";
4645     case DW_TAG_access_declaration:
4646       return "DW_TAG_access_declaration";
4647     case DW_TAG_base_type:
4648       return "DW_TAG_base_type";
4649     case DW_TAG_catch_block:
4650       return "DW_TAG_catch_block";
4651     case DW_TAG_const_type:
4652       return "DW_TAG_const_type";
4653     case DW_TAG_constant:
4654       return "DW_TAG_constant";
4655     case DW_TAG_enumerator:
4656       return "DW_TAG_enumerator";
4657     case DW_TAG_file_type:
4658       return "DW_TAG_file_type";
4659     case DW_TAG_friend:
4660       return "DW_TAG_friend";
4661     case DW_TAG_namelist:
4662       return "DW_TAG_namelist";
4663     case DW_TAG_namelist_item:
4664       return "DW_TAG_namelist_item";
4665     case DW_TAG_packed_type:
4666       return "DW_TAG_packed_type";
4667     case DW_TAG_subprogram:
4668       return "DW_TAG_subprogram";
4669     case DW_TAG_template_type_param:
4670       return "DW_TAG_template_type_param";
4671     case DW_TAG_template_value_param:
4672       return "DW_TAG_template_value_param";
4673     case DW_TAG_thrown_type:
4674       return "DW_TAG_thrown_type";
4675     case DW_TAG_try_block:
4676       return "DW_TAG_try_block";
4677     case DW_TAG_variant_part:
4678       return "DW_TAG_variant_part";
4679     case DW_TAG_variable:
4680       return "DW_TAG_variable";
4681     case DW_TAG_volatile_type:
4682       return "DW_TAG_volatile_type";
4683     case DW_TAG_dwarf_procedure:
4684       return "DW_TAG_dwarf_procedure";
4685     case DW_TAG_restrict_type:
4686       return "DW_TAG_restrict_type";
4687     case DW_TAG_interface_type:
4688       return "DW_TAG_interface_type";
4689     case DW_TAG_namespace:
4690       return "DW_TAG_namespace";
4691     case DW_TAG_imported_module:
4692       return "DW_TAG_imported_module";
4693     case DW_TAG_unspecified_type:
4694       return "DW_TAG_unspecified_type";
4695     case DW_TAG_partial_unit:
4696       return "DW_TAG_partial_unit";
4697     case DW_TAG_imported_unit:
4698       return "DW_TAG_imported_unit";
4699     case DW_TAG_condition:
4700       return "DW_TAG_condition";
4701     case DW_TAG_shared_type:
4702       return "DW_TAG_shared_type";
4703     case DW_TAG_MIPS_loop:
4704       return "DW_TAG_MIPS_loop";
4705     case DW_TAG_format_label:
4706       return "DW_TAG_format_label";
4707     case DW_TAG_function_template:
4708       return "DW_TAG_function_template";
4709     case DW_TAG_class_template:
4710       return "DW_TAG_class_template";
4711     case DW_TAG_GNU_BINCL:
4712       return "DW_TAG_GNU_BINCL";
4713     case DW_TAG_GNU_EINCL:
4714       return "DW_TAG_GNU_EINCL";
4715     default:
4716       return "DW_TAG_<unknown>";
4717     }
4718 }
4719
4720 /* Convert a DWARF attribute code into its string name.  */
4721
4722 static const char *
4723 dwarf_attr_name (unsigned int attr)
4724 {
4725   switch (attr)
4726     {
4727     case DW_AT_sibling:
4728       return "DW_AT_sibling";
4729     case DW_AT_location:
4730       return "DW_AT_location";
4731     case DW_AT_name:
4732       return "DW_AT_name";
4733     case DW_AT_ordering:
4734       return "DW_AT_ordering";
4735     case DW_AT_subscr_data:
4736       return "DW_AT_subscr_data";
4737     case DW_AT_byte_size:
4738       return "DW_AT_byte_size";
4739     case DW_AT_bit_offset:
4740       return "DW_AT_bit_offset";
4741     case DW_AT_bit_size:
4742       return "DW_AT_bit_size";
4743     case DW_AT_element_list:
4744       return "DW_AT_element_list";
4745     case DW_AT_stmt_list:
4746       return "DW_AT_stmt_list";
4747     case DW_AT_low_pc:
4748       return "DW_AT_low_pc";
4749     case DW_AT_high_pc:
4750       return "DW_AT_high_pc";
4751     case DW_AT_language:
4752       return "DW_AT_language";
4753     case DW_AT_member:
4754       return "DW_AT_member";
4755     case DW_AT_discr:
4756       return "DW_AT_discr";
4757     case DW_AT_discr_value:
4758       return "DW_AT_discr_value";
4759     case DW_AT_visibility:
4760       return "DW_AT_visibility";
4761     case DW_AT_import:
4762       return "DW_AT_import";
4763     case DW_AT_string_length:
4764       return "DW_AT_string_length";
4765     case DW_AT_common_reference:
4766       return "DW_AT_common_reference";
4767     case DW_AT_comp_dir:
4768       return "DW_AT_comp_dir";
4769     case DW_AT_const_value:
4770       return "DW_AT_const_value";
4771     case DW_AT_containing_type:
4772       return "DW_AT_containing_type";
4773     case DW_AT_default_value:
4774       return "DW_AT_default_value";
4775     case DW_AT_inline:
4776       return "DW_AT_inline";
4777     case DW_AT_is_optional:
4778       return "DW_AT_is_optional";
4779     case DW_AT_lower_bound:
4780       return "DW_AT_lower_bound";
4781     case DW_AT_producer:
4782       return "DW_AT_producer";
4783     case DW_AT_prototyped:
4784       return "DW_AT_prototyped";
4785     case DW_AT_return_addr:
4786       return "DW_AT_return_addr";
4787     case DW_AT_start_scope:
4788       return "DW_AT_start_scope";
4789     case DW_AT_bit_stride:
4790       return "DW_AT_bit_stride";
4791     case DW_AT_upper_bound:
4792       return "DW_AT_upper_bound";
4793     case DW_AT_abstract_origin:
4794       return "DW_AT_abstract_origin";
4795     case DW_AT_accessibility:
4796       return "DW_AT_accessibility";
4797     case DW_AT_address_class:
4798       return "DW_AT_address_class";
4799     case DW_AT_artificial:
4800       return "DW_AT_artificial";
4801     case DW_AT_base_types:
4802       return "DW_AT_base_types";
4803     case DW_AT_calling_convention:
4804       return "DW_AT_calling_convention";
4805     case DW_AT_count:
4806       return "DW_AT_count";
4807     case DW_AT_data_member_location:
4808       return "DW_AT_data_member_location";
4809     case DW_AT_decl_column:
4810       return "DW_AT_decl_column";
4811     case DW_AT_decl_file:
4812       return "DW_AT_decl_file";
4813     case DW_AT_decl_line:
4814       return "DW_AT_decl_line";
4815     case DW_AT_declaration:
4816       return "DW_AT_declaration";
4817     case DW_AT_discr_list:
4818       return "DW_AT_discr_list";
4819     case DW_AT_encoding:
4820       return "DW_AT_encoding";
4821     case DW_AT_external:
4822       return "DW_AT_external";
4823     case DW_AT_frame_base:
4824       return "DW_AT_frame_base";
4825     case DW_AT_friend:
4826       return "DW_AT_friend";
4827     case DW_AT_identifier_case:
4828       return "DW_AT_identifier_case";
4829     case DW_AT_macro_info:
4830       return "DW_AT_macro_info";
4831     case DW_AT_namelist_items:
4832       return "DW_AT_namelist_items";
4833     case DW_AT_priority:
4834       return "DW_AT_priority";
4835     case DW_AT_segment:
4836       return "DW_AT_segment";
4837     case DW_AT_specification:
4838       return "DW_AT_specification";
4839     case DW_AT_static_link:
4840       return "DW_AT_static_link";
4841     case DW_AT_type:
4842       return "DW_AT_type";
4843     case DW_AT_use_location:
4844       return "DW_AT_use_location";
4845     case DW_AT_variable_parameter:
4846       return "DW_AT_variable_parameter";
4847     case DW_AT_virtuality:
4848       return "DW_AT_virtuality";
4849     case DW_AT_vtable_elem_location:
4850       return "DW_AT_vtable_elem_location";
4851
4852     case DW_AT_allocated:
4853       return "DW_AT_allocated";
4854     case DW_AT_associated:
4855       return "DW_AT_associated";
4856     case DW_AT_data_location:
4857       return "DW_AT_data_location";
4858     case DW_AT_byte_stride:
4859       return "DW_AT_byte_stride";
4860     case DW_AT_entry_pc:
4861       return "DW_AT_entry_pc";
4862     case DW_AT_use_UTF8:
4863       return "DW_AT_use_UTF8";
4864     case DW_AT_extension:
4865       return "DW_AT_extension";
4866     case DW_AT_ranges:
4867       return "DW_AT_ranges";
4868     case DW_AT_trampoline:
4869       return "DW_AT_trampoline";
4870     case DW_AT_call_column:
4871       return "DW_AT_call_column";
4872     case DW_AT_call_file:
4873       return "DW_AT_call_file";
4874     case DW_AT_call_line:
4875       return "DW_AT_call_line";
4876
4877     case DW_AT_MIPS_fde:
4878       return "DW_AT_MIPS_fde";
4879     case DW_AT_MIPS_loop_begin:
4880       return "DW_AT_MIPS_loop_begin";
4881     case DW_AT_MIPS_tail_loop_begin:
4882       return "DW_AT_MIPS_tail_loop_begin";
4883     case DW_AT_MIPS_epilog_begin:
4884       return "DW_AT_MIPS_epilog_begin";
4885     case DW_AT_MIPS_loop_unroll_factor:
4886       return "DW_AT_MIPS_loop_unroll_factor";
4887     case DW_AT_MIPS_software_pipeline_depth:
4888       return "DW_AT_MIPS_software_pipeline_depth";
4889     case DW_AT_MIPS_linkage_name:
4890       return "DW_AT_MIPS_linkage_name";
4891     case DW_AT_MIPS_stride:
4892       return "DW_AT_MIPS_stride";
4893     case DW_AT_MIPS_abstract_name:
4894       return "DW_AT_MIPS_abstract_name";
4895     case DW_AT_MIPS_clone_origin:
4896       return "DW_AT_MIPS_clone_origin";
4897     case DW_AT_MIPS_has_inlines:
4898       return "DW_AT_MIPS_has_inlines";
4899
4900     case DW_AT_sf_names:
4901       return "DW_AT_sf_names";
4902     case DW_AT_src_info:
4903       return "DW_AT_src_info";
4904     case DW_AT_mac_info:
4905       return "DW_AT_mac_info";
4906     case DW_AT_src_coords:
4907       return "DW_AT_src_coords";
4908     case DW_AT_body_begin:
4909       return "DW_AT_body_begin";
4910     case DW_AT_body_end:
4911       return "DW_AT_body_end";
4912     case DW_AT_GNU_vector:
4913       return "DW_AT_GNU_vector";
4914
4915     case DW_AT_VMS_rtnbeg_pd_address:
4916       return "DW_AT_VMS_rtnbeg_pd_address";
4917
4918     default:
4919       return "DW_AT_<unknown>";
4920     }
4921 }
4922
4923 /* Convert a DWARF value form code into its string name.  */
4924
4925 static const char *
4926 dwarf_form_name (unsigned int form)
4927 {
4928   switch (form)
4929     {
4930     case DW_FORM_addr:
4931       return "DW_FORM_addr";
4932     case DW_FORM_block2:
4933       return "DW_FORM_block2";
4934     case DW_FORM_block4:
4935       return "DW_FORM_block4";
4936     case DW_FORM_data2:
4937       return "DW_FORM_data2";
4938     case DW_FORM_data4:
4939       return "DW_FORM_data4";
4940     case DW_FORM_data8:
4941       return "DW_FORM_data8";
4942     case DW_FORM_string:
4943       return "DW_FORM_string";
4944     case DW_FORM_block:
4945       return "DW_FORM_block";
4946     case DW_FORM_block1:
4947       return "DW_FORM_block1";
4948     case DW_FORM_data1:
4949       return "DW_FORM_data1";
4950     case DW_FORM_flag:
4951       return "DW_FORM_flag";
4952     case DW_FORM_sdata:
4953       return "DW_FORM_sdata";
4954     case DW_FORM_strp:
4955       return "DW_FORM_strp";
4956     case DW_FORM_udata:
4957       return "DW_FORM_udata";
4958     case DW_FORM_ref_addr:
4959       return "DW_FORM_ref_addr";
4960     case DW_FORM_ref1:
4961       return "DW_FORM_ref1";
4962     case DW_FORM_ref2:
4963       return "DW_FORM_ref2";
4964     case DW_FORM_ref4:
4965       return "DW_FORM_ref4";
4966     case DW_FORM_ref8:
4967       return "DW_FORM_ref8";
4968     case DW_FORM_ref_udata:
4969       return "DW_FORM_ref_udata";
4970     case DW_FORM_indirect:
4971       return "DW_FORM_indirect";
4972     default:
4973       return "DW_FORM_<unknown>";
4974     }
4975 }
4976 \f
4977 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4978    instance of an inlined instance of a decl 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 decl_ultimate_origin (const_tree decl)
4985 {
4986   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4987     return NULL_TREE;
4988
4989   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4990      nodes in the function to point to themselves; ignore that if
4991      we're trying to output the abstract instance of this function.  */
4992   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4993     return NULL_TREE;
4994
4995   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4996      most distant ancestor, this should never happen.  */
4997   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4998
4999   return DECL_ABSTRACT_ORIGIN (decl);
5000 }
5001
5002 /* Determine the "ultimate origin" of a block.  The block may be an inlined
5003    instance of an inlined instance of a block which is local to an inline
5004    function, so we have to trace all of the way back through the origin chain
5005    to find out what sort of node actually served as the original seed for the
5006    given block.  */
5007
5008 static tree
5009 block_ultimate_origin (const_tree block)
5010 {
5011   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
5012
5013   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
5014      nodes in the function to point to themselves; ignore that if
5015      we're trying to output the abstract instance of this function.  */
5016   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
5017     return NULL_TREE;
5018
5019   if (immediate_origin == NULL_TREE)
5020     return NULL_TREE;
5021   else
5022     {
5023       tree ret_val;
5024       tree lookahead = immediate_origin;
5025
5026       do
5027         {
5028           ret_val = lookahead;
5029           lookahead = (TREE_CODE (ret_val) == BLOCK
5030                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
5031         }
5032       while (lookahead != NULL && lookahead != ret_val);
5033
5034       /* The block's abstract origin chain may not be the *ultimate* origin of
5035          the block. It could lead to a DECL that has an abstract origin set.
5036          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5037          will give us if it has one).  Note that DECL's abstract origins are
5038          supposed to be the most distant ancestor (or so decl_ultimate_origin
5039          claims), so we don't need to loop following the DECL origins.  */
5040       if (DECL_P (ret_val))
5041         return DECL_ORIGIN (ret_val);
5042
5043       return ret_val;
5044     }
5045 }
5046
5047 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5048    of a virtual function may refer to a base class, so we check the 'this'
5049    parameter.  */
5050
5051 static tree
5052 decl_class_context (tree decl)
5053 {
5054   tree context = NULL_TREE;
5055
5056   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5057     context = DECL_CONTEXT (decl);
5058   else
5059     context = TYPE_MAIN_VARIANT
5060       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5061
5062   if (context && !TYPE_P (context))
5063     context = NULL_TREE;
5064
5065   return context;
5066 }
5067 \f
5068 /* Add an attribute/value pair to a DIE.  */
5069
5070 static inline void
5071 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5072 {
5073   /* Maybe this should be an assert?  */
5074   if (die == NULL)
5075     return;
5076
5077   if (die->die_attr == NULL)
5078     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5079   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5080 }
5081
5082 static inline enum dw_val_class
5083 AT_class (dw_attr_ref a)
5084 {
5085   return a->dw_attr_val.val_class;
5086 }
5087
5088 /* Add a flag value attribute to a DIE.  */
5089
5090 static inline void
5091 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5092 {
5093   dw_attr_node attr;
5094
5095   attr.dw_attr = attr_kind;
5096   attr.dw_attr_val.val_class = dw_val_class_flag;
5097   attr.dw_attr_val.v.val_flag = flag;
5098   add_dwarf_attr (die, &attr);
5099 }
5100
5101 static inline unsigned
5102 AT_flag (dw_attr_ref a)
5103 {
5104   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5105   return a->dw_attr_val.v.val_flag;
5106 }
5107
5108 /* Add a signed integer attribute value to a DIE.  */
5109
5110 static inline void
5111 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5112 {
5113   dw_attr_node attr;
5114
5115   attr.dw_attr = attr_kind;
5116   attr.dw_attr_val.val_class = dw_val_class_const;
5117   attr.dw_attr_val.v.val_int = int_val;
5118   add_dwarf_attr (die, &attr);
5119 }
5120
5121 static inline HOST_WIDE_INT
5122 AT_int (dw_attr_ref a)
5123 {
5124   gcc_assert (a && AT_class (a) == dw_val_class_const);
5125   return a->dw_attr_val.v.val_int;
5126 }
5127
5128 /* Add an unsigned integer attribute value to a DIE.  */
5129
5130 static inline void
5131 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5132                  unsigned HOST_WIDE_INT unsigned_val)
5133 {
5134   dw_attr_node attr;
5135
5136   attr.dw_attr = attr_kind;
5137   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5138   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5139   add_dwarf_attr (die, &attr);
5140 }
5141
5142 static inline unsigned HOST_WIDE_INT
5143 AT_unsigned (dw_attr_ref a)
5144 {
5145   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5146   return a->dw_attr_val.v.val_unsigned;
5147 }
5148
5149 /* Add an unsigned double integer attribute value to a DIE.  */
5150
5151 static inline void
5152 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5153                   long unsigned int val_hi, long unsigned int val_low)
5154 {
5155   dw_attr_node attr;
5156
5157   attr.dw_attr = attr_kind;
5158   attr.dw_attr_val.val_class = dw_val_class_long_long;
5159   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5160   attr.dw_attr_val.v.val_long_long.low = val_low;
5161   add_dwarf_attr (die, &attr);
5162 }
5163
5164 /* Add a floating point attribute value to a DIE and return it.  */
5165
5166 static inline void
5167 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5168             unsigned int length, unsigned int elt_size, unsigned char *array)
5169 {
5170   dw_attr_node attr;
5171
5172   attr.dw_attr = attr_kind;
5173   attr.dw_attr_val.val_class = dw_val_class_vec;
5174   attr.dw_attr_val.v.val_vec.length = length;
5175   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5176   attr.dw_attr_val.v.val_vec.array = array;
5177   add_dwarf_attr (die, &attr);
5178 }
5179
5180 /* Hash and equality functions for debug_str_hash.  */
5181
5182 static hashval_t
5183 debug_str_do_hash (const void *x)
5184 {
5185   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5186 }
5187
5188 static int
5189 debug_str_eq (const void *x1, const void *x2)
5190 {
5191   return strcmp ((((const struct indirect_string_node *)x1)->str),
5192                  (const char *)x2) == 0;
5193 }
5194
5195 /* Add a string attribute value to a DIE.  */
5196
5197 static inline void
5198 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5199 {
5200   dw_attr_node attr;
5201   struct indirect_string_node *node;
5202   void **slot;
5203
5204   if (! debug_str_hash)
5205     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5206                                       debug_str_eq, NULL);
5207
5208   slot = htab_find_slot_with_hash (debug_str_hash, str,
5209                                    htab_hash_string (str), INSERT);
5210   if (*slot == NULL)
5211     {
5212       node = (struct indirect_string_node *)
5213                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5214       node->str = ggc_strdup (str);
5215       *slot = node;
5216     }
5217   else
5218     node = (struct indirect_string_node *) *slot;
5219
5220   node->refcount++;
5221
5222   attr.dw_attr = attr_kind;
5223   attr.dw_attr_val.val_class = dw_val_class_str;
5224   attr.dw_attr_val.v.val_str = node;
5225   add_dwarf_attr (die, &attr);
5226 }
5227
5228 static inline const char *
5229 AT_string (dw_attr_ref a)
5230 {
5231   gcc_assert (a && AT_class (a) == dw_val_class_str);
5232   return a->dw_attr_val.v.val_str->str;
5233 }
5234
5235 /* Find out whether a string should be output inline in DIE
5236    or out-of-line in .debug_str section.  */
5237
5238 static int
5239 AT_string_form (dw_attr_ref a)
5240 {
5241   struct indirect_string_node *node;
5242   unsigned int len;
5243   char label[32];
5244
5245   gcc_assert (a && AT_class (a) == dw_val_class_str);
5246
5247   node = a->dw_attr_val.v.val_str;
5248   if (node->form)
5249     return node->form;
5250
5251   len = strlen (node->str) + 1;
5252
5253   /* If the string is shorter or equal to the size of the reference, it is
5254      always better to put it inline.  */
5255   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5256     return node->form = DW_FORM_string;
5257
5258   /* If we cannot expect the linker to merge strings in .debug_str
5259      section, only put it into .debug_str if it is worth even in this
5260      single module.  */
5261   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5262       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5263     return node->form = DW_FORM_string;
5264
5265   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5266   ++dw2_string_counter;
5267   node->label = xstrdup (label);
5268
5269   return node->form = DW_FORM_strp;
5270 }
5271
5272 /* Add a DIE reference attribute value to a DIE.  */
5273
5274 static inline void
5275 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5276 {
5277   dw_attr_node attr;
5278
5279   attr.dw_attr = attr_kind;
5280   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5281   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5282   attr.dw_attr_val.v.val_die_ref.external = 0;
5283   add_dwarf_attr (die, &attr);
5284 }
5285
5286 /* Add an AT_specification attribute to a DIE, and also make the back
5287    pointer from the specification to the definition.  */
5288
5289 static inline void
5290 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5291 {
5292   add_AT_die_ref (die, DW_AT_specification, targ_die);
5293   gcc_assert (!targ_die->die_definition);
5294   targ_die->die_definition = die;
5295 }
5296
5297 static inline dw_die_ref
5298 AT_ref (dw_attr_ref a)
5299 {
5300   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5301   return a->dw_attr_val.v.val_die_ref.die;
5302 }
5303
5304 static inline int
5305 AT_ref_external (dw_attr_ref a)
5306 {
5307   if (a && AT_class (a) == dw_val_class_die_ref)
5308     return a->dw_attr_val.v.val_die_ref.external;
5309
5310   return 0;
5311 }
5312
5313 static inline void
5314 set_AT_ref_external (dw_attr_ref a, int i)
5315 {
5316   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5317   a->dw_attr_val.v.val_die_ref.external = i;
5318 }
5319
5320 /* Add an FDE reference attribute value to a DIE.  */
5321
5322 static inline void
5323 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5324 {
5325   dw_attr_node attr;
5326
5327   attr.dw_attr = attr_kind;
5328   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5329   attr.dw_attr_val.v.val_fde_index = targ_fde;
5330   add_dwarf_attr (die, &attr);
5331 }
5332
5333 /* Add a location description attribute value to a DIE.  */
5334
5335 static inline void
5336 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5337 {
5338   dw_attr_node attr;
5339
5340   attr.dw_attr = attr_kind;
5341   attr.dw_attr_val.val_class = dw_val_class_loc;
5342   attr.dw_attr_val.v.val_loc = loc;
5343   add_dwarf_attr (die, &attr);
5344 }
5345
5346 static inline dw_loc_descr_ref
5347 AT_loc (dw_attr_ref a)
5348 {
5349   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5350   return a->dw_attr_val.v.val_loc;
5351 }
5352
5353 static inline void
5354 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5355 {
5356   dw_attr_node attr;
5357
5358   attr.dw_attr = attr_kind;
5359   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5360   attr.dw_attr_val.v.val_loc_list = loc_list;
5361   add_dwarf_attr (die, &attr);
5362   have_location_lists = true;
5363 }
5364
5365 static inline dw_loc_list_ref
5366 AT_loc_list (dw_attr_ref a)
5367 {
5368   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5369   return a->dw_attr_val.v.val_loc_list;
5370 }
5371
5372 /* Add an address constant attribute value to a DIE.  */
5373
5374 static inline void
5375 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5376 {
5377   dw_attr_node attr;
5378
5379   attr.dw_attr = attr_kind;
5380   attr.dw_attr_val.val_class = dw_val_class_addr;
5381   attr.dw_attr_val.v.val_addr = addr;
5382   add_dwarf_attr (die, &attr);
5383 }
5384
5385 /* Get the RTX from to an address DIE attribute.  */
5386
5387 static inline rtx
5388 AT_addr (dw_attr_ref a)
5389 {
5390   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5391   return a->dw_attr_val.v.val_addr;
5392 }
5393
5394 /* Add a file attribute value to a DIE.  */
5395
5396 static inline void
5397 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5398              struct dwarf_file_data *fd)
5399 {
5400   dw_attr_node attr;
5401
5402   attr.dw_attr = attr_kind;
5403   attr.dw_attr_val.val_class = dw_val_class_file;
5404   attr.dw_attr_val.v.val_file = fd;
5405   add_dwarf_attr (die, &attr);
5406 }
5407
5408 /* Get the dwarf_file_data from a file DIE attribute.  */
5409
5410 static inline struct dwarf_file_data *
5411 AT_file (dw_attr_ref a)
5412 {
5413   gcc_assert (a && AT_class (a) == dw_val_class_file);
5414   return a->dw_attr_val.v.val_file;
5415 }
5416
5417 /* Add a label identifier attribute value to a DIE.  */
5418
5419 static inline void
5420 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5421 {
5422   dw_attr_node attr;
5423
5424   attr.dw_attr = attr_kind;
5425   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5426   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5427   add_dwarf_attr (die, &attr);
5428 }
5429
5430 /* Add a section offset attribute value to a DIE, an offset into the
5431    debug_line section.  */
5432
5433 static inline void
5434 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5435                 const char *label)
5436 {
5437   dw_attr_node attr;
5438
5439   attr.dw_attr = attr_kind;
5440   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5441   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5442   add_dwarf_attr (die, &attr);
5443 }
5444
5445 /* Add a section offset attribute value to a DIE, an offset into the
5446    debug_macinfo section.  */
5447
5448 static inline void
5449 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5450                const char *label)
5451 {
5452   dw_attr_node attr;
5453
5454   attr.dw_attr = attr_kind;
5455   attr.dw_attr_val.val_class = dw_val_class_macptr;
5456   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5457   add_dwarf_attr (die, &attr);
5458 }
5459
5460 /* Add an offset attribute value to a DIE.  */
5461
5462 static inline void
5463 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5464                unsigned HOST_WIDE_INT offset)
5465 {
5466   dw_attr_node attr;
5467
5468   attr.dw_attr = attr_kind;
5469   attr.dw_attr_val.val_class = dw_val_class_offset;
5470   attr.dw_attr_val.v.val_offset = offset;
5471   add_dwarf_attr (die, &attr);
5472 }
5473
5474 /* Add an range_list attribute value to a DIE.  */
5475
5476 static void
5477 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5478                    long unsigned int offset)
5479 {
5480   dw_attr_node attr;
5481
5482   attr.dw_attr = attr_kind;
5483   attr.dw_attr_val.val_class = dw_val_class_range_list;
5484   attr.dw_attr_val.v.val_offset = offset;
5485   add_dwarf_attr (die, &attr);
5486 }
5487
5488 static inline const char *
5489 AT_lbl (dw_attr_ref a)
5490 {
5491   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5492                     || AT_class (a) == dw_val_class_lineptr
5493                     || AT_class (a) == dw_val_class_macptr));
5494   return a->dw_attr_val.v.val_lbl_id;
5495 }
5496
5497 /* Get the attribute of type attr_kind.  */
5498
5499 static dw_attr_ref
5500 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5501 {
5502   dw_attr_ref a;
5503   unsigned ix;
5504   dw_die_ref spec = NULL;
5505
5506   if (! die)
5507     return NULL;
5508
5509   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5510     if (a->dw_attr == attr_kind)
5511       return a;
5512     else if (a->dw_attr == DW_AT_specification
5513              || a->dw_attr == DW_AT_abstract_origin)
5514       spec = AT_ref (a);
5515
5516   if (spec)
5517     return get_AT (spec, attr_kind);
5518
5519   return NULL;
5520 }
5521
5522 /* Return the "low pc" attribute value, typically associated with a subprogram
5523    DIE.  Return null if the "low pc" attribute is either not present, or if it
5524    cannot be represented as an assembler label identifier.  */
5525
5526 static inline const char *
5527 get_AT_low_pc (dw_die_ref die)
5528 {
5529   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5530
5531   return a ? AT_lbl (a) : NULL;
5532 }
5533
5534 /* Return the "high pc" attribute value, typically associated with a subprogram
5535    DIE.  Return null if the "high pc" attribute is either not present, or if it
5536    cannot be represented as an assembler label identifier.  */
5537
5538 static inline const char *
5539 get_AT_hi_pc (dw_die_ref die)
5540 {
5541   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5542
5543   return a ? AT_lbl (a) : NULL;
5544 }
5545
5546 /* Return the value of the string attribute designated by ATTR_KIND, or
5547    NULL if it is not present.  */
5548
5549 static inline const char *
5550 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5551 {
5552   dw_attr_ref a = get_AT (die, attr_kind);
5553
5554   return a ? AT_string (a) : NULL;
5555 }
5556
5557 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5558    if it is not present.  */
5559
5560 static inline int
5561 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5562 {
5563   dw_attr_ref a = get_AT (die, attr_kind);
5564
5565   return a ? AT_flag (a) : 0;
5566 }
5567
5568 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5569    if it is not present.  */
5570
5571 static inline unsigned
5572 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5573 {
5574   dw_attr_ref a = get_AT (die, attr_kind);
5575
5576   return a ? AT_unsigned (a) : 0;
5577 }
5578
5579 static inline dw_die_ref
5580 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5581 {
5582   dw_attr_ref a = get_AT (die, attr_kind);
5583
5584   return a ? AT_ref (a) : NULL;
5585 }
5586
5587 static inline struct dwarf_file_data *
5588 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5589 {
5590   dw_attr_ref a = get_AT (die, attr_kind);
5591
5592   return a ? AT_file (a) : NULL;
5593 }
5594
5595 /* Return TRUE if the language is C or C++.  */
5596
5597 static inline bool
5598 is_c_family (void)
5599 {
5600   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5601
5602   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5603           || lang == DW_LANG_C99
5604           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5605 }
5606
5607 /* Return TRUE if the language is C++.  */
5608
5609 static inline bool
5610 is_cxx (void)
5611 {
5612   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5613
5614   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5615 }
5616
5617 /* Return TRUE if the language is Fortran.  */
5618
5619 static inline bool
5620 is_fortran (void)
5621 {
5622   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5623
5624   return (lang == DW_LANG_Fortran77
5625           || lang == DW_LANG_Fortran90
5626           || lang == DW_LANG_Fortran95);
5627 }
5628
5629 /* Return TRUE if the language is Java.  */
5630
5631 static inline bool
5632 is_java (void)
5633 {
5634   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5635
5636   return lang == DW_LANG_Java;
5637 }
5638
5639 /* Return TRUE if the language is Ada.  */
5640
5641 static inline bool
5642 is_ada (void)
5643 {
5644   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5645
5646   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5647 }
5648
5649 /* Remove the specified attribute if present.  */
5650
5651 static void
5652 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5653 {
5654   dw_attr_ref a;
5655   unsigned ix;
5656
5657   if (! die)
5658     return;
5659
5660   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5661     if (a->dw_attr == attr_kind)
5662       {
5663         if (AT_class (a) == dw_val_class_str)
5664           if (a->dw_attr_val.v.val_str->refcount)
5665             a->dw_attr_val.v.val_str->refcount--;
5666
5667         /* VEC_ordered_remove should help reduce the number of abbrevs
5668            that are needed.  */
5669         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5670         return;
5671       }
5672 }
5673
5674 /* Remove CHILD from its parent.  PREV must have the property that
5675    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5676
5677 static void
5678 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5679 {
5680   gcc_assert (child->die_parent == prev->die_parent);
5681   gcc_assert (prev->die_sib == child);
5682   if (prev == child)
5683     {
5684       gcc_assert (child->die_parent->die_child == child);
5685       prev = NULL;
5686     }
5687   else
5688     prev->die_sib = child->die_sib;
5689   if (child->die_parent->die_child == child)
5690     child->die_parent->die_child = prev;
5691 }
5692
5693 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5694    matches TAG.  */
5695
5696 static void
5697 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5698 {
5699   dw_die_ref c;
5700
5701   c = die->die_child;
5702   if (c) do {
5703     dw_die_ref prev = c;
5704     c = c->die_sib;
5705     while (c->die_tag == tag)
5706       {
5707         remove_child_with_prev (c, prev);
5708         /* Might have removed every child.  */
5709         if (c == c->die_sib)
5710           return;
5711         c = c->die_sib;
5712       }
5713   } while (c != die->die_child);
5714 }
5715
5716 /* Add a CHILD_DIE as the last child of DIE.  */
5717
5718 static void
5719 add_child_die (dw_die_ref die, dw_die_ref child_die)
5720 {
5721   /* FIXME this should probably be an assert.  */
5722   if (! die || ! child_die)
5723     return;
5724   gcc_assert (die != child_die);
5725
5726   child_die->die_parent = die;
5727   if (die->die_child)
5728     {
5729       child_die->die_sib = die->die_child->die_sib;
5730       die->die_child->die_sib = child_die;
5731     }
5732   else
5733     child_die->die_sib = child_die;
5734   die->die_child = child_die;
5735 }
5736
5737 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5738    is the specification, to the end of PARENT's list of children.
5739    This is done by removing and re-adding it.  */
5740
5741 static void
5742 splice_child_die (dw_die_ref parent, dw_die_ref child)
5743 {
5744   dw_die_ref p;
5745
5746   /* We want the declaration DIE from inside the class, not the
5747      specification DIE at toplevel.  */
5748   if (child->die_parent != parent)
5749     {
5750       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5751
5752       if (tmp)
5753         child = tmp;
5754     }
5755
5756   gcc_assert (child->die_parent == parent
5757               || (child->die_parent
5758                   == get_AT_ref (parent, DW_AT_specification)));
5759
5760   for (p = child->die_parent->die_child; ; p = p->die_sib)
5761     if (p->die_sib == child)
5762       {
5763         remove_child_with_prev (child, p);
5764         break;
5765       }
5766
5767   add_child_die (parent, child);
5768 }
5769
5770 /* Return a pointer to a newly created DIE node.  */
5771
5772 static inline dw_die_ref
5773 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5774 {
5775   dw_die_ref die = GGC_CNEW (die_node);
5776
5777   die->die_tag = tag_value;
5778
5779   if (parent_die != NULL)
5780     add_child_die (parent_die, die);
5781   else
5782     {
5783       limbo_die_node *limbo_node;
5784
5785       limbo_node = GGC_CNEW (limbo_die_node);
5786       limbo_node->die = die;
5787       limbo_node->created_for = t;
5788       limbo_node->next = limbo_die_list;
5789       limbo_die_list = limbo_node;
5790     }
5791
5792   return die;
5793 }
5794
5795 /* Return the DIE associated with the given type specifier.  */
5796
5797 static inline dw_die_ref
5798 lookup_type_die (tree type)
5799 {
5800   return TYPE_SYMTAB_DIE (type);
5801 }
5802
5803 /* Equate a DIE to a given type specifier.  */
5804
5805 static inline void
5806 equate_type_number_to_die (tree type, dw_die_ref type_die)
5807 {
5808   TYPE_SYMTAB_DIE (type) = type_die;
5809 }
5810
5811 /* Returns a hash value for X (which really is a die_struct).  */
5812
5813 static hashval_t
5814 decl_die_table_hash (const void *x)
5815 {
5816   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5817 }
5818
5819 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5820
5821 static int
5822 decl_die_table_eq (const void *x, const void *y)
5823 {
5824   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5825 }
5826
5827 /* Return the DIE associated with a given declaration.  */
5828
5829 static inline dw_die_ref
5830 lookup_decl_die (tree decl)
5831 {
5832   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5833 }
5834
5835 /* Returns a hash value for X (which really is a var_loc_list).  */
5836
5837 static hashval_t
5838 decl_loc_table_hash (const void *x)
5839 {
5840   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5841 }
5842
5843 /* Return nonzero if decl_id of var_loc_list X is the same as
5844    UID of decl *Y.  */
5845
5846 static int
5847 decl_loc_table_eq (const void *x, const void *y)
5848 {
5849   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5850 }
5851
5852 /* Return the var_loc list associated with a given declaration.  */
5853
5854 static inline var_loc_list *
5855 lookup_decl_loc (const_tree decl)
5856 {
5857   return (var_loc_list *)
5858     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5859 }
5860
5861 /* Equate a DIE to a particular declaration.  */
5862
5863 static void
5864 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5865 {
5866   unsigned int decl_id = DECL_UID (decl);
5867   void **slot;
5868
5869   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5870   *slot = decl_die;
5871   decl_die->decl_id = decl_id;
5872 }
5873
5874 /* Add a variable location node to the linked list for DECL.  */
5875
5876 static void
5877 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5878 {
5879   unsigned int decl_id = DECL_UID (decl);
5880   var_loc_list *temp;
5881   void **slot;
5882
5883   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5884   if (*slot == NULL)
5885     {
5886       temp = GGC_CNEW (var_loc_list);
5887       temp->decl_id = decl_id;
5888       *slot = temp;
5889     }
5890   else
5891     temp = (var_loc_list *) *slot;
5892
5893   if (temp->last)
5894     {
5895       /* If the current location is the same as the end of the list,
5896          and either both or neither of the locations is uninitialized,
5897          we have nothing to do.  */
5898       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5899                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5900           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5901                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5902               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5903                    == VAR_INIT_STATUS_UNINITIALIZED)
5904                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5905                       == VAR_INIT_STATUS_UNINITIALIZED))))
5906         {
5907           /* Add LOC to the end of list and update LAST.  */
5908           temp->last->next = loc;
5909           temp->last = loc;
5910         }
5911     }
5912   /* Do not add empty location to the beginning of the list.  */
5913   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5914     {
5915       temp->first = loc;
5916       temp->last = loc;
5917     }
5918 }
5919 \f
5920 /* Keep track of the number of spaces used to indent the
5921    output of the debugging routines that print the structure of
5922    the DIE internal representation.  */
5923 static int print_indent;
5924
5925 /* Indent the line the number of spaces given by print_indent.  */
5926
5927 static inline void
5928 print_spaces (FILE *outfile)
5929 {
5930   fprintf (outfile, "%*s", print_indent, "");
5931 }
5932
5933 /* Print the information associated with a given DIE, and its children.
5934    This routine is a debugging aid only.  */
5935
5936 static void
5937 print_die (dw_die_ref die, FILE *outfile)
5938 {
5939   dw_attr_ref a;
5940   dw_die_ref c;
5941   unsigned ix;
5942
5943   print_spaces (outfile);
5944   fprintf (outfile, "DIE %4ld: %s\n",
5945            die->die_offset, dwarf_tag_name (die->die_tag));
5946   print_spaces (outfile);
5947   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5948   fprintf (outfile, " offset: %ld\n", die->die_offset);
5949
5950   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5951     {
5952       print_spaces (outfile);
5953       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5954
5955       switch (AT_class (a))
5956         {
5957         case dw_val_class_addr:
5958           fprintf (outfile, "address");
5959           break;
5960         case dw_val_class_offset:
5961           fprintf (outfile, "offset");
5962           break;
5963         case dw_val_class_loc:
5964           fprintf (outfile, "location descriptor");
5965           break;
5966         case dw_val_class_loc_list:
5967           fprintf (outfile, "location list -> label:%s",
5968                    AT_loc_list (a)->ll_symbol);
5969           break;
5970         case dw_val_class_range_list:
5971           fprintf (outfile, "range list");
5972           break;
5973         case dw_val_class_const:
5974           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5975           break;
5976         case dw_val_class_unsigned_const:
5977           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5978           break;
5979         case dw_val_class_long_long:
5980           fprintf (outfile, "constant (%lu,%lu)",
5981                    a->dw_attr_val.v.val_long_long.hi,
5982                    a->dw_attr_val.v.val_long_long.low);
5983           break;
5984         case dw_val_class_vec:
5985           fprintf (outfile, "floating-point or vector constant");
5986           break;
5987         case dw_val_class_flag:
5988           fprintf (outfile, "%u", AT_flag (a));
5989           break;
5990         case dw_val_class_die_ref:
5991           if (AT_ref (a) != NULL)
5992             {
5993               if (AT_ref (a)->die_symbol)
5994                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5995               else
5996                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5997             }
5998           else
5999             fprintf (outfile, "die -> <null>");
6000           break;
6001         case dw_val_class_lbl_id:
6002         case dw_val_class_lineptr:
6003         case dw_val_class_macptr:
6004           fprintf (outfile, "label: %s", AT_lbl (a));
6005           break;
6006         case dw_val_class_str:
6007           if (AT_string (a) != NULL)
6008             fprintf (outfile, "\"%s\"", AT_string (a));
6009           else
6010             fprintf (outfile, "<null>");
6011           break;
6012         case dw_val_class_file:
6013           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6014                    AT_file (a)->emitted_number);
6015           break;
6016         default:
6017           break;
6018         }
6019
6020       fprintf (outfile, "\n");
6021     }
6022
6023   if (die->die_child != NULL)
6024     {
6025       print_indent += 4;
6026       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6027       print_indent -= 4;
6028     }
6029   if (print_indent == 0)
6030     fprintf (outfile, "\n");
6031 }
6032
6033 /* Print the contents of the source code line number correspondence table.
6034    This routine is a debugging aid only.  */
6035
6036 static void
6037 print_dwarf_line_table (FILE *outfile)
6038 {
6039   unsigned i;
6040   dw_line_info_ref line_info;
6041
6042   fprintf (outfile, "\n\nDWARF source line information\n");
6043   for (i = 1; i < line_info_table_in_use; i++)
6044     {
6045       line_info = &line_info_table[i];
6046       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6047                line_info->dw_file_num,
6048                line_info->dw_line_num);
6049     }
6050
6051   fprintf (outfile, "\n\n");
6052 }
6053
6054 /* Print the information collected for a given DIE.  */
6055
6056 void
6057 debug_dwarf_die (dw_die_ref die)
6058 {
6059   print_die (die, stderr);
6060 }
6061
6062 /* Print all DWARF information collected for the compilation unit.
6063    This routine is a debugging aid only.  */
6064
6065 void
6066 debug_dwarf (void)
6067 {
6068   print_indent = 0;
6069   print_die (comp_unit_die, stderr);
6070   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6071     print_dwarf_line_table (stderr);
6072 }
6073 \f
6074 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6075    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6076    DIE that marks the start of the DIEs for this include file.  */
6077
6078 static dw_die_ref
6079 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6080 {
6081   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6082   dw_die_ref new_unit = gen_compile_unit_die (filename);
6083
6084   new_unit->die_sib = old_unit;
6085   return new_unit;
6086 }
6087
6088 /* Close an include-file CU and reopen the enclosing one.  */
6089
6090 static dw_die_ref
6091 pop_compile_unit (dw_die_ref old_unit)
6092 {
6093   dw_die_ref new_unit = old_unit->die_sib;
6094
6095   old_unit->die_sib = NULL;
6096   return new_unit;
6097 }
6098
6099 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6100 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6101
6102 /* Calculate the checksum of a location expression.  */
6103
6104 static inline void
6105 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6106 {
6107   CHECKSUM (loc->dw_loc_opc);
6108   CHECKSUM (loc->dw_loc_oprnd1);
6109   CHECKSUM (loc->dw_loc_oprnd2);
6110 }
6111
6112 /* Calculate the checksum of an attribute.  */
6113
6114 static void
6115 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6116 {
6117   dw_loc_descr_ref loc;
6118   rtx r;
6119
6120   CHECKSUM (at->dw_attr);
6121
6122   /* We don't care that this was compiled with a different compiler
6123      snapshot; if the output is the same, that's what matters.  */
6124   if (at->dw_attr == DW_AT_producer)
6125     return;
6126
6127   switch (AT_class (at))
6128     {
6129     case dw_val_class_const:
6130       CHECKSUM (at->dw_attr_val.v.val_int);
6131       break;
6132     case dw_val_class_unsigned_const:
6133       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6134       break;
6135     case dw_val_class_long_long:
6136       CHECKSUM (at->dw_attr_val.v.val_long_long);
6137       break;
6138     case dw_val_class_vec:
6139       CHECKSUM (at->dw_attr_val.v.val_vec);
6140       break;
6141     case dw_val_class_flag:
6142       CHECKSUM (at->dw_attr_val.v.val_flag);
6143       break;
6144     case dw_val_class_str:
6145       CHECKSUM_STRING (AT_string (at));
6146       break;
6147
6148     case dw_val_class_addr:
6149       r = AT_addr (at);
6150       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6151       CHECKSUM_STRING (XSTR (r, 0));
6152       break;
6153
6154     case dw_val_class_offset:
6155       CHECKSUM (at->dw_attr_val.v.val_offset);
6156       break;
6157
6158     case dw_val_class_loc:
6159       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6160         loc_checksum (loc, ctx);
6161       break;
6162
6163     case dw_val_class_die_ref:
6164       die_checksum (AT_ref (at), ctx, mark);
6165       break;
6166
6167     case dw_val_class_fde_ref:
6168     case dw_val_class_lbl_id:
6169     case dw_val_class_lineptr:
6170     case dw_val_class_macptr:
6171       break;
6172
6173     case dw_val_class_file:
6174       CHECKSUM_STRING (AT_file (at)->filename);
6175       break;
6176
6177     default:
6178       break;
6179     }
6180 }
6181
6182 /* Calculate the checksum of a DIE.  */
6183
6184 static void
6185 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6186 {
6187   dw_die_ref c;
6188   dw_attr_ref a;
6189   unsigned ix;
6190
6191   /* To avoid infinite recursion.  */
6192   if (die->die_mark)
6193     {
6194       CHECKSUM (die->die_mark);
6195       return;
6196     }
6197   die->die_mark = ++(*mark);
6198
6199   CHECKSUM (die->die_tag);
6200
6201   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6202     attr_checksum (a, ctx, mark);
6203
6204   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6205 }
6206
6207 #undef CHECKSUM
6208 #undef CHECKSUM_STRING
6209
6210 /* Do the location expressions look same?  */
6211 static inline int
6212 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6213 {
6214   return loc1->dw_loc_opc == loc2->dw_loc_opc
6215          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6216          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6217 }
6218
6219 /* Do the values look the same?  */
6220 static int
6221 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6222 {
6223   dw_loc_descr_ref loc1, loc2;
6224   rtx r1, r2;
6225
6226   if (v1->val_class != v2->val_class)
6227     return 0;
6228
6229   switch (v1->val_class)
6230     {
6231     case dw_val_class_const:
6232       return v1->v.val_int == v2->v.val_int;
6233     case dw_val_class_unsigned_const:
6234       return v1->v.val_unsigned == v2->v.val_unsigned;
6235     case dw_val_class_long_long:
6236       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6237              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6238     case dw_val_class_vec:
6239       if (v1->v.val_vec.length != v2->v.val_vec.length
6240           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6241         return 0;
6242       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6243                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6244         return 0;
6245       return 1;
6246     case dw_val_class_flag:
6247       return v1->v.val_flag == v2->v.val_flag;
6248     case dw_val_class_str:
6249       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6250
6251     case dw_val_class_addr:
6252       r1 = v1->v.val_addr;
6253       r2 = v2->v.val_addr;
6254       if (GET_CODE (r1) != GET_CODE (r2))
6255         return 0;
6256       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6257       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6258
6259     case dw_val_class_offset:
6260       return v1->v.val_offset == v2->v.val_offset;
6261
6262     case dw_val_class_loc:
6263       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6264            loc1 && loc2;
6265            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6266         if (!same_loc_p (loc1, loc2, mark))
6267           return 0;
6268       return !loc1 && !loc2;
6269
6270     case dw_val_class_die_ref:
6271       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6272
6273     case dw_val_class_fde_ref:
6274     case dw_val_class_lbl_id:
6275     case dw_val_class_lineptr:
6276     case dw_val_class_macptr:
6277       return 1;
6278
6279     case dw_val_class_file:
6280       return v1->v.val_file == v2->v.val_file;
6281
6282     default:
6283       return 1;
6284     }
6285 }
6286
6287 /* Do the attributes look the same?  */
6288
6289 static int
6290 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6291 {
6292   if (at1->dw_attr != at2->dw_attr)
6293     return 0;
6294
6295   /* We don't care that this was compiled with a different compiler
6296      snapshot; if the output is the same, that's what matters. */
6297   if (at1->dw_attr == DW_AT_producer)
6298     return 1;
6299
6300   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6301 }
6302
6303 /* Do the dies look the same?  */
6304
6305 static int
6306 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6307 {
6308   dw_die_ref c1, c2;
6309   dw_attr_ref a1;
6310   unsigned ix;
6311
6312   /* To avoid infinite recursion.  */
6313   if (die1->die_mark)
6314     return die1->die_mark == die2->die_mark;
6315   die1->die_mark = die2->die_mark = ++(*mark);
6316
6317   if (die1->die_tag != die2->die_tag)
6318     return 0;
6319
6320   if (VEC_length (dw_attr_node, die1->die_attr)
6321       != VEC_length (dw_attr_node, die2->die_attr))
6322     return 0;
6323
6324   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6325     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6326       return 0;
6327
6328   c1 = die1->die_child;
6329   c2 = die2->die_child;
6330   if (! c1)
6331     {
6332       if (c2)
6333         return 0;
6334     }
6335   else
6336     for (;;)
6337       {
6338         if (!same_die_p (c1, c2, mark))
6339           return 0;
6340         c1 = c1->die_sib;
6341         c2 = c2->die_sib;
6342         if (c1 == die1->die_child)
6343           {
6344             if (c2 == die2->die_child)
6345               break;
6346             else
6347               return 0;
6348           }
6349     }
6350
6351   return 1;
6352 }
6353
6354 /* Do the dies look the same?  Wrapper around same_die_p.  */
6355
6356 static int
6357 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6358 {
6359   int mark = 0;
6360   int ret = same_die_p (die1, die2, &mark);
6361
6362   unmark_all_dies (die1);
6363   unmark_all_dies (die2);
6364
6365   return ret;
6366 }
6367
6368 /* The prefix to attach to symbols on DIEs in the current comdat debug
6369    info section.  */
6370 static char *comdat_symbol_id;
6371
6372 /* The index of the current symbol within the current comdat CU.  */
6373 static unsigned int comdat_symbol_number;
6374
6375 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6376    children, and set comdat_symbol_id accordingly.  */
6377
6378 static void
6379 compute_section_prefix (dw_die_ref unit_die)
6380 {
6381   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6382   const char *base = die_name ? lbasename (die_name) : "anonymous";
6383   char *name = XALLOCAVEC (char, strlen (base) + 64);
6384   char *p;
6385   int i, mark;
6386   unsigned char checksum[16];
6387   struct md5_ctx ctx;
6388
6389   /* Compute the checksum of the DIE, then append part of it as hex digits to
6390      the name filename of the unit.  */
6391
6392   md5_init_ctx (&ctx);
6393   mark = 0;
6394   die_checksum (unit_die, &ctx, &mark);
6395   unmark_all_dies (unit_die);
6396   md5_finish_ctx (&ctx, checksum);
6397
6398   sprintf (name, "%s.", base);
6399   clean_symbol_name (name);
6400
6401   p = name + strlen (name);
6402   for (i = 0; i < 4; i++)
6403     {
6404       sprintf (p, "%.2x", checksum[i]);
6405       p += 2;
6406     }
6407
6408   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6409   comdat_symbol_number = 0;
6410 }
6411
6412 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6413
6414 static int
6415 is_type_die (dw_die_ref die)
6416 {
6417   switch (die->die_tag)
6418     {
6419     case DW_TAG_array_type:
6420     case DW_TAG_class_type:
6421     case DW_TAG_interface_type:
6422     case DW_TAG_enumeration_type:
6423     case DW_TAG_pointer_type:
6424     case DW_TAG_reference_type:
6425     case DW_TAG_string_type:
6426     case DW_TAG_structure_type:
6427     case DW_TAG_subroutine_type:
6428     case DW_TAG_union_type:
6429     case DW_TAG_ptr_to_member_type:
6430     case DW_TAG_set_type:
6431     case DW_TAG_subrange_type:
6432     case DW_TAG_base_type:
6433     case DW_TAG_const_type:
6434     case DW_TAG_file_type:
6435     case DW_TAG_packed_type:
6436     case DW_TAG_volatile_type:
6437     case DW_TAG_typedef:
6438       return 1;
6439     default:
6440       return 0;
6441     }
6442 }
6443
6444 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6445    Basically, we want to choose the bits that are likely to be shared between
6446    compilations (types) and leave out the bits that are specific to individual
6447    compilations (functions).  */
6448
6449 static int
6450 is_comdat_die (dw_die_ref c)
6451 {
6452   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6453      we do for stabs.  The advantage is a greater likelihood of sharing between
6454      objects that don't include headers in the same order (and therefore would
6455      put the base types in a different comdat).  jason 8/28/00 */
6456
6457   if (c->die_tag == DW_TAG_base_type)
6458     return 0;
6459
6460   if (c->die_tag == DW_TAG_pointer_type
6461       || c->die_tag == DW_TAG_reference_type
6462       || c->die_tag == DW_TAG_const_type
6463       || c->die_tag == DW_TAG_volatile_type)
6464     {
6465       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6466
6467       return t ? is_comdat_die (t) : 0;
6468     }
6469
6470   return is_type_die (c);
6471 }
6472
6473 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6474    compilation unit.  */
6475
6476 static int
6477 is_symbol_die (dw_die_ref c)
6478 {
6479   return (is_type_die (c)
6480           || (get_AT (c, DW_AT_declaration)
6481               && !get_AT (c, DW_AT_specification))
6482           || c->die_tag == DW_TAG_namespace);
6483 }
6484
6485 static char *
6486 gen_internal_sym (const char *prefix)
6487 {
6488   char buf[256];
6489
6490   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6491   return xstrdup (buf);
6492 }
6493
6494 /* Assign symbols to all worthy DIEs under DIE.  */
6495
6496 static void
6497 assign_symbol_names (dw_die_ref die)
6498 {
6499   dw_die_ref c;
6500
6501   if (is_symbol_die (die))
6502     {
6503       if (comdat_symbol_id)
6504         {
6505           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6506
6507           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6508                    comdat_symbol_id, comdat_symbol_number++);
6509           die->die_symbol = xstrdup (p);
6510         }
6511       else
6512         die->die_symbol = gen_internal_sym ("LDIE");
6513     }
6514
6515   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6516 }
6517
6518 struct cu_hash_table_entry
6519 {
6520   dw_die_ref cu;
6521   unsigned min_comdat_num, max_comdat_num;
6522   struct cu_hash_table_entry *next;
6523 };
6524
6525 /* Routines to manipulate hash table of CUs.  */
6526 static hashval_t
6527 htab_cu_hash (const void *of)
6528 {
6529   const struct cu_hash_table_entry *const entry =
6530     (const struct cu_hash_table_entry *) of;
6531
6532   return htab_hash_string (entry->cu->die_symbol);
6533 }
6534
6535 static int
6536 htab_cu_eq (const void *of1, const void *of2)
6537 {
6538   const struct cu_hash_table_entry *const entry1 =
6539     (const struct cu_hash_table_entry *) of1;
6540   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6541
6542   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6543 }
6544
6545 static void
6546 htab_cu_del (void *what)
6547 {
6548   struct cu_hash_table_entry *next,
6549     *entry = (struct cu_hash_table_entry *) what;
6550
6551   while (entry)
6552     {
6553       next = entry->next;
6554       free (entry);
6555       entry = next;
6556     }
6557 }
6558
6559 /* Check whether we have already seen this CU and set up SYM_NUM
6560    accordingly.  */
6561 static int
6562 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6563 {
6564   struct cu_hash_table_entry dummy;
6565   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6566
6567   dummy.max_comdat_num = 0;
6568
6569   slot = (struct cu_hash_table_entry **)
6570     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6571         INSERT);
6572   entry = *slot;
6573
6574   for (; entry; last = entry, entry = entry->next)
6575     {
6576       if (same_die_p_wrap (cu, entry->cu))
6577         break;
6578     }
6579
6580   if (entry)
6581     {
6582       *sym_num = entry->min_comdat_num;
6583       return 1;
6584     }
6585
6586   entry = XCNEW (struct cu_hash_table_entry);
6587   entry->cu = cu;
6588   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6589   entry->next = *slot;
6590   *slot = entry;
6591
6592   return 0;
6593 }
6594
6595 /* Record SYM_NUM to record of CU in HTABLE.  */
6596 static void
6597 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6598 {
6599   struct cu_hash_table_entry **slot, *entry;
6600
6601   slot = (struct cu_hash_table_entry **)
6602     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6603         NO_INSERT);
6604   entry = *slot;
6605
6606   entry->max_comdat_num = sym_num;
6607 }
6608
6609 /* Traverse the DIE (which is always comp_unit_die), and set up
6610    additional compilation units for each of the include files we see
6611    bracketed by BINCL/EINCL.  */
6612
6613 static void
6614 break_out_includes (dw_die_ref die)
6615 {
6616   dw_die_ref c;
6617   dw_die_ref unit = NULL;
6618   limbo_die_node *node, **pnode;
6619   htab_t cu_hash_table;
6620
6621   c = die->die_child;
6622   if (c) do {
6623     dw_die_ref prev = c;
6624     c = c->die_sib;
6625     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6626            || (unit && is_comdat_die (c)))
6627       {
6628         dw_die_ref next = c->die_sib;
6629
6630         /* This DIE is for a secondary CU; remove it from the main one.  */
6631         remove_child_with_prev (c, prev);
6632
6633         if (c->die_tag == DW_TAG_GNU_BINCL)
6634           unit = push_new_compile_unit (unit, c);
6635         else if (c->die_tag == DW_TAG_GNU_EINCL)
6636           unit = pop_compile_unit (unit);
6637         else
6638           add_child_die (unit, c);
6639         c = next;
6640         if (c == die->die_child)
6641           break;
6642       }
6643   } while (c != die->die_child);
6644
6645 #if 0
6646   /* We can only use this in debugging, since the frontend doesn't check
6647      to make sure that we leave every include file we enter.  */
6648   gcc_assert (!unit);
6649 #endif
6650
6651   assign_symbol_names (die);
6652   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6653   for (node = limbo_die_list, pnode = &limbo_die_list;
6654        node;
6655        node = node->next)
6656     {
6657       int is_dupl;
6658
6659       compute_section_prefix (node->die);
6660       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6661                         &comdat_symbol_number);
6662       assign_symbol_names (node->die);
6663       if (is_dupl)
6664         *pnode = node->next;
6665       else
6666         {
6667           pnode = &node->next;
6668           record_comdat_symbol_number (node->die, cu_hash_table,
6669                 comdat_symbol_number);
6670         }
6671     }
6672   htab_delete (cu_hash_table);
6673 }
6674
6675 /* Traverse the DIE and add a sibling attribute if it may have the
6676    effect of speeding up access to siblings.  To save some space,
6677    avoid generating sibling attributes for DIE's without children.  */
6678
6679 static void
6680 add_sibling_attributes (dw_die_ref die)
6681 {
6682   dw_die_ref c;
6683
6684   if (! die->die_child)
6685     return;
6686
6687   if (die->die_parent && die != die->die_parent->die_child)
6688     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6689
6690   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6691 }
6692
6693 /* Output all location lists for the DIE and its children.  */
6694
6695 static void
6696 output_location_lists (dw_die_ref die)
6697 {
6698   dw_die_ref c;
6699   dw_attr_ref a;
6700   unsigned ix;
6701
6702   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6703     if (AT_class (a) == dw_val_class_loc_list)
6704       output_loc_list (AT_loc_list (a));
6705
6706   FOR_EACH_CHILD (die, c, output_location_lists (c));
6707 }
6708
6709 /* The format of each DIE (and its attribute value pairs) is encoded in an
6710    abbreviation table.  This routine builds the abbreviation table and assigns
6711    a unique abbreviation id for each abbreviation entry.  The children of each
6712    die are visited recursively.  */
6713
6714 static void
6715 build_abbrev_table (dw_die_ref die)
6716 {
6717   unsigned long abbrev_id;
6718   unsigned int n_alloc;
6719   dw_die_ref c;
6720   dw_attr_ref a;
6721   unsigned ix;
6722
6723   /* Scan the DIE references, and mark as external any that refer to
6724      DIEs from other CUs (i.e. those which are not marked).  */
6725   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6726     if (AT_class (a) == dw_val_class_die_ref
6727         && AT_ref (a)->die_mark == 0)
6728       {
6729         gcc_assert (AT_ref (a)->die_symbol);
6730
6731         set_AT_ref_external (a, 1);
6732       }
6733
6734   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6735     {
6736       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6737       dw_attr_ref die_a, abbrev_a;
6738       unsigned ix;
6739       bool ok = true;
6740
6741       if (abbrev->die_tag != die->die_tag)
6742         continue;
6743       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6744         continue;
6745
6746       if (VEC_length (dw_attr_node, abbrev->die_attr)
6747           != VEC_length (dw_attr_node, die->die_attr))
6748         continue;
6749
6750       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6751         {
6752           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6753           if ((abbrev_a->dw_attr != die_a->dw_attr)
6754               || (value_format (abbrev_a) != value_format (die_a)))
6755             {
6756               ok = false;
6757               break;
6758             }
6759         }
6760       if (ok)
6761         break;
6762     }
6763
6764   if (abbrev_id >= abbrev_die_table_in_use)
6765     {
6766       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6767         {
6768           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6769           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
6770                                             n_alloc);
6771
6772           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6773                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6774           abbrev_die_table_allocated = n_alloc;
6775         }
6776
6777       ++abbrev_die_table_in_use;
6778       abbrev_die_table[abbrev_id] = die;
6779     }
6780
6781   die->die_abbrev = abbrev_id;
6782   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6783 }
6784 \f
6785 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6786
6787 static int
6788 constant_size (long unsigned int value)
6789 {
6790   int log;
6791
6792   if (value == 0)
6793     log = 0;
6794   else
6795     log = floor_log2 (value);
6796
6797   log = log / 8;
6798   log = 1 << (floor_log2 (log) + 1);
6799
6800   return log;
6801 }
6802
6803 /* Return the size of a DIE as it is represented in the
6804    .debug_info section.  */
6805
6806 static unsigned long
6807 size_of_die (dw_die_ref die)
6808 {
6809   unsigned long size = 0;
6810   dw_attr_ref a;
6811   unsigned ix;
6812
6813   size += size_of_uleb128 (die->die_abbrev);
6814   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6815     {
6816       switch (AT_class (a))
6817         {
6818         case dw_val_class_addr:
6819           size += DWARF2_ADDR_SIZE;
6820           break;
6821         case dw_val_class_offset:
6822           size += DWARF_OFFSET_SIZE;
6823           break;
6824         case dw_val_class_loc:
6825           {
6826             unsigned long lsize = size_of_locs (AT_loc (a));
6827
6828             /* Block length.  */
6829             size += constant_size (lsize);
6830             size += lsize;
6831           }
6832           break;
6833         case dw_val_class_loc_list:
6834           size += DWARF_OFFSET_SIZE;
6835           break;
6836         case dw_val_class_range_list:
6837           size += DWARF_OFFSET_SIZE;
6838           break;
6839         case dw_val_class_const:
6840           size += size_of_sleb128 (AT_int (a));
6841           break;
6842         case dw_val_class_unsigned_const:
6843           size += constant_size (AT_unsigned (a));
6844           break;
6845         case dw_val_class_long_long:
6846           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6847           break;
6848         case dw_val_class_vec:
6849           size += 1 + (a->dw_attr_val.v.val_vec.length
6850                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6851           break;
6852         case dw_val_class_flag:
6853           size += 1;
6854           break;
6855         case dw_val_class_die_ref:
6856           if (AT_ref_external (a))
6857             size += DWARF2_ADDR_SIZE;
6858           else
6859             size += DWARF_OFFSET_SIZE;
6860           break;
6861         case dw_val_class_fde_ref:
6862           size += DWARF_OFFSET_SIZE;
6863           break;
6864         case dw_val_class_lbl_id:
6865           size += DWARF2_ADDR_SIZE;
6866           break;
6867         case dw_val_class_lineptr:
6868         case dw_val_class_macptr:
6869           size += DWARF_OFFSET_SIZE;
6870           break;
6871         case dw_val_class_str:
6872           if (AT_string_form (a) == DW_FORM_strp)
6873             size += DWARF_OFFSET_SIZE;
6874           else
6875             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6876           break;
6877         case dw_val_class_file:
6878           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6879           break;
6880         default:
6881           gcc_unreachable ();
6882         }
6883     }
6884
6885   return size;
6886 }
6887
6888 /* Size the debugging information associated with a given DIE.  Visits the
6889    DIE's children recursively.  Updates the global variable next_die_offset, on
6890    each time through.  Uses the current value of next_die_offset to update the
6891    die_offset field in each DIE.  */
6892
6893 static void
6894 calc_die_sizes (dw_die_ref die)
6895 {
6896   dw_die_ref c;
6897
6898   die->die_offset = next_die_offset;
6899   next_die_offset += size_of_die (die);
6900
6901   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6902
6903   if (die->die_child != NULL)
6904     /* Count the null byte used to terminate sibling lists.  */
6905     next_die_offset += 1;
6906 }
6907
6908 /* Set the marks for a die and its children.  We do this so
6909    that we know whether or not a reference needs to use FORM_ref_addr; only
6910    DIEs in the same CU will be marked.  We used to clear out the offset
6911    and use that as the flag, but ran into ordering problems.  */
6912
6913 static void
6914 mark_dies (dw_die_ref die)
6915 {
6916   dw_die_ref c;
6917
6918   gcc_assert (!die->die_mark);
6919
6920   die->die_mark = 1;
6921   FOR_EACH_CHILD (die, c, mark_dies (c));
6922 }
6923
6924 /* Clear the marks for a die and its children.  */
6925
6926 static void
6927 unmark_dies (dw_die_ref die)
6928 {
6929   dw_die_ref c;
6930
6931   gcc_assert (die->die_mark);
6932
6933   die->die_mark = 0;
6934   FOR_EACH_CHILD (die, c, unmark_dies (c));
6935 }
6936
6937 /* Clear the marks for a die, its children and referred dies.  */
6938
6939 static void
6940 unmark_all_dies (dw_die_ref die)
6941 {
6942   dw_die_ref c;
6943   dw_attr_ref a;
6944   unsigned ix;
6945
6946   if (!die->die_mark)
6947     return;
6948   die->die_mark = 0;
6949
6950   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6951
6952   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6953     if (AT_class (a) == dw_val_class_die_ref)
6954       unmark_all_dies (AT_ref (a));
6955 }
6956
6957 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6958    generated for the compilation unit.  */
6959
6960 static unsigned long
6961 size_of_pubnames (VEC (pubname_entry, gc) * names)
6962 {
6963   unsigned long size;
6964   unsigned i;
6965   pubname_ref p;
6966
6967   size = DWARF_PUBNAMES_HEADER_SIZE;
6968   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6969     if (names != pubtype_table
6970         || p->die->die_offset != 0
6971         || !flag_eliminate_unused_debug_types)
6972       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6973
6974   size += DWARF_OFFSET_SIZE;
6975   return size;
6976 }
6977
6978 /* Return the size of the information in the .debug_aranges section.  */
6979
6980 static unsigned long
6981 size_of_aranges (void)
6982 {
6983   unsigned long size;
6984
6985   size = DWARF_ARANGES_HEADER_SIZE;
6986
6987   /* Count the address/length pair for this compilation unit.  */
6988   if (text_section_used)
6989     size += 2 * DWARF2_ADDR_SIZE;
6990   if (cold_text_section_used)
6991     size += 2 * DWARF2_ADDR_SIZE;
6992   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6993
6994   /* Count the two zero words used to terminated the address range table.  */
6995   size += 2 * DWARF2_ADDR_SIZE;
6996   return size;
6997 }
6998 \f
6999 /* Select the encoding of an attribute value.  */
7000
7001 static enum dwarf_form
7002 value_format (dw_attr_ref a)
7003 {
7004   switch (a->dw_attr_val.val_class)
7005     {
7006     case dw_val_class_addr:
7007       return DW_FORM_addr;
7008     case dw_val_class_range_list:
7009     case dw_val_class_offset:
7010     case dw_val_class_loc_list:
7011       switch (DWARF_OFFSET_SIZE)
7012         {
7013         case 4:
7014           return DW_FORM_data4;
7015         case 8:
7016           return DW_FORM_data8;
7017         default:
7018           gcc_unreachable ();
7019         }
7020     case dw_val_class_loc:
7021       switch (constant_size (size_of_locs (AT_loc (a))))
7022         {
7023         case 1:
7024           return DW_FORM_block1;
7025         case 2:
7026           return DW_FORM_block2;
7027         default:
7028           gcc_unreachable ();
7029         }
7030     case dw_val_class_const:
7031       return DW_FORM_sdata;
7032     case dw_val_class_unsigned_const:
7033       switch (constant_size (AT_unsigned (a)))
7034         {
7035         case 1:
7036           return DW_FORM_data1;
7037         case 2:
7038           return DW_FORM_data2;
7039         case 4:
7040           return DW_FORM_data4;
7041         case 8:
7042           return DW_FORM_data8;
7043         default:
7044           gcc_unreachable ();
7045         }
7046     case dw_val_class_long_long:
7047       return DW_FORM_block1;
7048     case dw_val_class_vec:
7049       return DW_FORM_block1;
7050     case dw_val_class_flag:
7051       return DW_FORM_flag;
7052     case dw_val_class_die_ref:
7053       if (AT_ref_external (a))
7054         return DW_FORM_ref_addr;
7055       else
7056         return DW_FORM_ref;
7057     case dw_val_class_fde_ref:
7058       return DW_FORM_data;
7059     case dw_val_class_lbl_id:
7060       return DW_FORM_addr;
7061     case dw_val_class_lineptr:
7062     case dw_val_class_macptr:
7063       return DW_FORM_data;
7064     case dw_val_class_str:
7065       return AT_string_form (a);
7066     case dw_val_class_file:
7067       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7068         {
7069         case 1:
7070           return DW_FORM_data1;
7071         case 2:
7072           return DW_FORM_data2;
7073         case 4:
7074           return DW_FORM_data4;
7075         default:
7076           gcc_unreachable ();
7077         }
7078
7079     default:
7080       gcc_unreachable ();
7081     }
7082 }
7083
7084 /* Output the encoding of an attribute value.  */
7085
7086 static void
7087 output_value_format (dw_attr_ref a)
7088 {
7089   enum dwarf_form form = value_format (a);
7090
7091   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7092 }
7093
7094 /* Output the .debug_abbrev section which defines the DIE abbreviation
7095    table.  */
7096
7097 static void
7098 output_abbrev_section (void)
7099 {
7100   unsigned long abbrev_id;
7101
7102   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7103     {
7104       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7105       unsigned ix;
7106       dw_attr_ref a_attr;
7107
7108       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7109       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7110                                    dwarf_tag_name (abbrev->die_tag));
7111
7112       if (abbrev->die_child != NULL)
7113         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7114       else
7115         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7116
7117       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7118            ix++)
7119         {
7120           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7121                                        dwarf_attr_name (a_attr->dw_attr));
7122           output_value_format (a_attr);
7123         }
7124
7125       dw2_asm_output_data (1, 0, NULL);
7126       dw2_asm_output_data (1, 0, NULL);
7127     }
7128
7129   /* Terminate the table.  */
7130   dw2_asm_output_data (1, 0, NULL);
7131 }
7132
7133 /* Output a symbol we can use to refer to this DIE from another CU.  */
7134
7135 static inline void
7136 output_die_symbol (dw_die_ref die)
7137 {
7138   char *sym = die->die_symbol;
7139
7140   if (sym == 0)
7141     return;
7142
7143   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7144     /* We make these global, not weak; if the target doesn't support
7145        .linkonce, it doesn't support combining the sections, so debugging
7146        will break.  */
7147     targetm.asm_out.globalize_label (asm_out_file, sym);
7148
7149   ASM_OUTPUT_LABEL (asm_out_file, sym);
7150 }
7151
7152 /* Return a new location list, given the begin and end range, and the
7153    expression. gensym tells us whether to generate a new internal symbol for
7154    this location list node, which is done for the head of the list only.  */
7155
7156 static inline dw_loc_list_ref
7157 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7158               const char *section, unsigned int gensym)
7159 {
7160   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
7161
7162   retlist->begin = begin;
7163   retlist->end = end;
7164   retlist->expr = expr;
7165   retlist->section = section;
7166   if (gensym)
7167     retlist->ll_symbol = gen_internal_sym ("LLST");
7168
7169   return retlist;
7170 }
7171
7172 /* Add a location description expression to a location list.  */
7173
7174 static inline void
7175 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7176                            const char *begin, const char *end,
7177                            const char *section)
7178 {
7179   dw_loc_list_ref *d;
7180
7181   /* Find the end of the chain.  */
7182   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7183     ;
7184
7185   /* Add a new location list node to the list.  */
7186   *d = new_loc_list (descr, begin, end, section, 0);
7187 }
7188
7189 /* Output the location list given to us.  */
7190
7191 static void
7192 output_loc_list (dw_loc_list_ref list_head)
7193 {
7194   dw_loc_list_ref curr = list_head;
7195
7196   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7197
7198   /* Walk the location list, and output each range + expression.  */
7199   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7200     {
7201       unsigned long size;
7202       /* Don't output an entry that starts and ends at the same address.  */
7203       if (strcmp (curr->begin, curr->end) == 0)
7204         continue;
7205       if (!have_multiple_function_sections)
7206         {
7207           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7208                                 "Location list begin address (%s)",
7209                                 list_head->ll_symbol);
7210           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7211                                 "Location list end address (%s)",
7212                                 list_head->ll_symbol);
7213         }
7214       else
7215         {
7216           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7217                                "Location list begin address (%s)",
7218                                list_head->ll_symbol);
7219           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7220                                "Location list end address (%s)",
7221                                list_head->ll_symbol);
7222         }
7223       size = size_of_locs (curr->expr);
7224
7225       /* Output the block length for this list of location operations.  */
7226       gcc_assert (size <= 0xffff);
7227       dw2_asm_output_data (2, size, "%s", "Location expression size");
7228
7229       output_loc_sequence (curr->expr);
7230     }
7231
7232   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7233                        "Location list terminator begin (%s)",
7234                        list_head->ll_symbol);
7235   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7236                        "Location list terminator end (%s)",
7237                        list_head->ll_symbol);
7238 }
7239
7240 /* Output the DIE and its attributes.  Called recursively to generate
7241    the definitions of each child DIE.  */
7242
7243 static void
7244 output_die (dw_die_ref die)
7245 {
7246   dw_attr_ref a;
7247   dw_die_ref c;
7248   unsigned long size;
7249   unsigned ix;
7250
7251   /* If someone in another CU might refer to us, set up a symbol for
7252      them to point to.  */
7253   if (die->die_symbol)
7254     output_die_symbol (die);
7255
7256   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7257                                (unsigned long)die->die_offset,
7258                                dwarf_tag_name (die->die_tag));
7259
7260   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7261     {
7262       const char *name = dwarf_attr_name (a->dw_attr);
7263
7264       switch (AT_class (a))
7265         {
7266         case dw_val_class_addr:
7267           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7268           break;
7269
7270         case dw_val_class_offset:
7271           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7272                                "%s", name);
7273           break;
7274
7275         case dw_val_class_range_list:
7276           {
7277             char *p = strchr (ranges_section_label, '\0');
7278
7279             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7280                      a->dw_attr_val.v.val_offset);
7281             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7282                                    debug_ranges_section, "%s", name);
7283             *p = '\0';
7284           }
7285           break;
7286
7287         case dw_val_class_loc:
7288           size = size_of_locs (AT_loc (a));
7289
7290           /* Output the block length for this list of location operations.  */
7291           dw2_asm_output_data (constant_size (size), size, "%s", name);
7292
7293           output_loc_sequence (AT_loc (a));
7294           break;
7295
7296         case dw_val_class_const:
7297           /* ??? It would be slightly more efficient to use a scheme like is
7298              used for unsigned constants below, but gdb 4.x does not sign
7299              extend.  Gdb 5.x does sign extend.  */
7300           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7301           break;
7302
7303         case dw_val_class_unsigned_const:
7304           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7305                                AT_unsigned (a), "%s", name);
7306           break;
7307
7308         case dw_val_class_long_long:
7309           {
7310             unsigned HOST_WIDE_INT first, second;
7311
7312             dw2_asm_output_data (1,
7313                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7314                                  "%s", name);
7315
7316             if (WORDS_BIG_ENDIAN)
7317               {
7318                 first = a->dw_attr_val.v.val_long_long.hi;
7319                 second = a->dw_attr_val.v.val_long_long.low;
7320               }
7321             else
7322               {
7323                 first = a->dw_attr_val.v.val_long_long.low;
7324                 second = a->dw_attr_val.v.val_long_long.hi;
7325               }
7326
7327             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7328                                  first, "long long constant");
7329             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7330                                  second, NULL);
7331           }
7332           break;
7333
7334         case dw_val_class_vec:
7335           {
7336             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7337             unsigned int len = a->dw_attr_val.v.val_vec.length;
7338             unsigned int i;
7339             unsigned char *p;
7340
7341             dw2_asm_output_data (1, len * elt_size, "%s", name);
7342             if (elt_size > sizeof (HOST_WIDE_INT))
7343               {
7344                 elt_size /= 2;
7345                 len *= 2;
7346               }
7347             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7348                  i < len;
7349                  i++, p += elt_size)
7350               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7351                                    "fp or vector constant word %u", i);
7352             break;
7353           }
7354
7355         case dw_val_class_flag:
7356           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7357           break;
7358
7359         case dw_val_class_loc_list:
7360           {
7361             char *sym = AT_loc_list (a)->ll_symbol;
7362
7363             gcc_assert (sym);
7364             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7365                                    "%s", name);
7366           }
7367           break;
7368
7369         case dw_val_class_die_ref:
7370           if (AT_ref_external (a))
7371             {
7372               char *sym = AT_ref (a)->die_symbol;
7373
7374               gcc_assert (sym);
7375               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7376                                      "%s", name);
7377             }
7378           else
7379             {
7380               gcc_assert (AT_ref (a)->die_offset);
7381               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7382                                    "%s", name);
7383             }
7384           break;
7385
7386         case dw_val_class_fde_ref:
7387           {
7388             char l1[20];
7389
7390             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7391                                          a->dw_attr_val.v.val_fde_index * 2);
7392             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7393                                    "%s", name);
7394           }
7395           break;
7396
7397         case dw_val_class_lbl_id:
7398           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7399           break;
7400
7401         case dw_val_class_lineptr:
7402           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7403                                  debug_line_section, "%s", name);
7404           break;
7405
7406         case dw_val_class_macptr:
7407           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7408                                  debug_macinfo_section, "%s", name);
7409           break;
7410
7411         case dw_val_class_str:
7412           if (AT_string_form (a) == DW_FORM_strp)
7413             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7414                                    a->dw_attr_val.v.val_str->label,
7415                                    debug_str_section,
7416                                    "%s: \"%s\"", name, AT_string (a));
7417           else
7418             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7419           break;
7420
7421         case dw_val_class_file:
7422           {
7423             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7424
7425             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7426                                  a->dw_attr_val.v.val_file->filename);
7427             break;
7428           }
7429
7430         default:
7431           gcc_unreachable ();
7432         }
7433     }
7434
7435   FOR_EACH_CHILD (die, c, output_die (c));
7436
7437   /* Add null byte to terminate sibling list.  */
7438   if (die->die_child != NULL)
7439     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7440                          (unsigned long) die->die_offset);
7441 }
7442
7443 /* Output the compilation unit that appears at the beginning of the
7444    .debug_info section, and precedes the DIE descriptions.  */
7445
7446 static void
7447 output_compilation_unit_header (void)
7448 {
7449   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7450     dw2_asm_output_data (4, 0xffffffff,
7451       "Initial length escape value indicating 64-bit DWARF extension");
7452   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7453                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7454                        "Length of Compilation Unit Info");
7455   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7456   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7457                          debug_abbrev_section,
7458                          "Offset Into Abbrev. Section");
7459   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7460 }
7461
7462 /* Output the compilation unit DIE and its children.  */
7463
7464 static void
7465 output_comp_unit (dw_die_ref die, int output_if_empty)
7466 {
7467   const char *secname;
7468   char *oldsym, *tmp;
7469
7470   /* Unless we are outputting main CU, we may throw away empty ones.  */
7471   if (!output_if_empty && die->die_child == NULL)
7472     return;
7473
7474   /* Even if there are no children of this DIE, we must output the information
7475      about the compilation unit.  Otherwise, on an empty translation unit, we
7476      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7477      will then complain when examining the file.  First mark all the DIEs in
7478      this CU so we know which get local refs.  */
7479   mark_dies (die);
7480
7481   build_abbrev_table (die);
7482
7483   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7484   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7485   calc_die_sizes (die);
7486
7487   oldsym = die->die_symbol;
7488   if (oldsym)
7489     {
7490       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
7491
7492       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7493       secname = tmp;
7494       die->die_symbol = NULL;
7495       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7496     }
7497   else
7498     switch_to_section (debug_info_section);
7499
7500   /* Output debugging information.  */
7501   output_compilation_unit_header ();
7502   output_die (die);
7503
7504   /* Leave the marks on the main CU, so we can check them in
7505      output_pubnames.  */
7506   if (oldsym)
7507     {
7508       unmark_dies (die);
7509       die->die_symbol = oldsym;
7510     }
7511 }
7512
7513 /* Return the DWARF2/3 pubname associated with a decl.  */
7514
7515 static const char *
7516 dwarf2_name (tree decl, int scope)
7517 {
7518   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7519 }
7520
7521 /* Add a new entry to .debug_pubnames if appropriate.  */
7522
7523 static void
7524 add_pubname_string (const char *str, dw_die_ref die)
7525 {
7526   pubname_entry e;
7527
7528   e.die = die;
7529   e.name = xstrdup (str);
7530   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7531 }
7532
7533 static void
7534 add_pubname (tree decl, dw_die_ref die)
7535 {
7536
7537   if (TREE_PUBLIC (decl))
7538     add_pubname_string (dwarf2_name (decl, 1), die);
7539 }
7540
7541 /* Add a new entry to .debug_pubtypes if appropriate.  */
7542
7543 static void
7544 add_pubtype (tree decl, dw_die_ref die)
7545 {
7546   pubname_entry e;
7547
7548   e.name = NULL;
7549   if ((TREE_PUBLIC (decl)
7550        || die->die_parent == comp_unit_die)
7551       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7552     {
7553       e.die = die;
7554       if (TYPE_P (decl))
7555         {
7556           if (TYPE_NAME (decl))
7557             {
7558               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7559                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7560               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7561                        && DECL_NAME (TYPE_NAME (decl)))
7562                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7563               else
7564                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7565             }
7566         }
7567       else
7568         e.name = xstrdup (dwarf2_name (decl, 1));
7569
7570       /* If we don't have a name for the type, there's no point in adding
7571          it to the table.  */
7572       if (e.name && e.name[0] != '\0')
7573         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7574     }
7575 }
7576
7577 /* Output the public names table used to speed up access to externally
7578    visible names; or the public types table used to find type definitions.  */
7579
7580 static void
7581 output_pubnames (VEC (pubname_entry, gc) * names)
7582 {
7583   unsigned i;
7584   unsigned long pubnames_length = size_of_pubnames (names);
7585   pubname_ref pub;
7586
7587   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7588     dw2_asm_output_data (4, 0xffffffff,
7589       "Initial length escape value indicating 64-bit DWARF extension");
7590   if (names == pubname_table)
7591     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7592                          "Length of Public Names Info");
7593   else
7594     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7595                          "Length of Public Type Names Info");
7596   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7597   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7598                          debug_info_section,
7599                          "Offset of Compilation Unit Info");
7600   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7601                        "Compilation Unit Length");
7602
7603   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7604     {
7605       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7606       if (names == pubname_table)
7607         gcc_assert (pub->die->die_mark);
7608
7609       if (names != pubtype_table
7610           || pub->die->die_offset != 0
7611           || !flag_eliminate_unused_debug_types)
7612         {
7613           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7614                                "DIE offset");
7615
7616           dw2_asm_output_nstring (pub->name, -1, "external name");
7617         }
7618     }
7619
7620   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7621 }
7622
7623 /* Add a new entry to .debug_aranges if appropriate.  */
7624
7625 static void
7626 add_arange (tree decl, dw_die_ref die)
7627 {
7628   if (! DECL_SECTION_NAME (decl))
7629     return;
7630
7631   if (arange_table_in_use == arange_table_allocated)
7632     {
7633       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7634       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
7635                                     arange_table_allocated);
7636       memset (arange_table + arange_table_in_use, 0,
7637               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7638     }
7639
7640   arange_table[arange_table_in_use++] = die;
7641 }
7642
7643 /* Output the information that goes into the .debug_aranges table.
7644    Namely, define the beginning and ending address range of the
7645    text section generated for this compilation unit.  */
7646
7647 static void
7648 output_aranges (void)
7649 {
7650   unsigned i;
7651   unsigned long aranges_length = size_of_aranges ();
7652
7653   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7654     dw2_asm_output_data (4, 0xffffffff,
7655       "Initial length escape value indicating 64-bit DWARF extension");
7656   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7657                        "Length of Address Ranges Info");
7658   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7659   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7660                          debug_info_section,
7661                          "Offset of Compilation Unit Info");
7662   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7663   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7664
7665   /* We need to align to twice the pointer size here.  */
7666   if (DWARF_ARANGES_PAD_SIZE)
7667     {
7668       /* Pad using a 2 byte words so that padding is correct for any
7669          pointer size.  */
7670       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7671                            2 * DWARF2_ADDR_SIZE);
7672       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7673         dw2_asm_output_data (2, 0, NULL);
7674     }
7675
7676   /* It is necessary not to output these entries if the sections were
7677      not used; if the sections were not used, the length will be 0 and
7678      the address may end up as 0 if the section is discarded by ld
7679      --gc-sections, leaving an invalid (0, 0) entry that can be
7680      confused with the terminator.  */
7681   if (text_section_used)
7682     {
7683       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7684       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7685                             text_section_label, "Length");
7686     }
7687   if (cold_text_section_used)
7688     {
7689       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7690                            "Address");
7691       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7692                             cold_text_section_label, "Length");
7693     }
7694
7695   for (i = 0; i < arange_table_in_use; i++)
7696     {
7697       dw_die_ref die = arange_table[i];
7698
7699       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7700       gcc_assert (die->die_mark);
7701
7702       if (die->die_tag == DW_TAG_subprogram)
7703         {
7704           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7705                                "Address");
7706           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7707                                 get_AT_low_pc (die), "Length");
7708         }
7709       else
7710         {
7711           /* A static variable; extract the symbol from DW_AT_location.
7712              Note that this code isn't currently hit, as we only emit
7713              aranges for functions (jason 9/23/99).  */
7714           dw_attr_ref a = get_AT (die, DW_AT_location);
7715           dw_loc_descr_ref loc;
7716
7717           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7718
7719           loc = AT_loc (a);
7720           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7721
7722           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7723                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7724           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7725                                get_AT_unsigned (die, DW_AT_byte_size),
7726                                "Length");
7727         }
7728     }
7729
7730   /* Output the terminator words.  */
7731   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7732   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7733 }
7734
7735 /* Add a new entry to .debug_ranges.  Return the offset at which it
7736    was placed.  */
7737
7738 static unsigned int
7739 add_ranges_num (int num)
7740 {
7741   unsigned int in_use = ranges_table_in_use;
7742
7743   if (in_use == ranges_table_allocated)
7744     {
7745       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7746       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
7747                                     ranges_table_allocated);
7748       memset (ranges_table + ranges_table_in_use, 0,
7749               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7750     }
7751
7752   ranges_table[in_use].num = num;
7753   ranges_table_in_use = in_use + 1;
7754
7755   return in_use * 2 * DWARF2_ADDR_SIZE;
7756 }
7757
7758 /* Add a new entry to .debug_ranges corresponding to a block, or a
7759    range terminator if BLOCK is NULL.  */
7760
7761 static unsigned int
7762 add_ranges (const_tree block)
7763 {
7764   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7765 }
7766
7767 /* Add a new entry to .debug_ranges corresponding to a pair of
7768    labels.  */
7769
7770 static unsigned int
7771 add_ranges_by_labels (const char *begin, const char *end)
7772 {
7773   unsigned int in_use = ranges_by_label_in_use;
7774
7775   if (in_use == ranges_by_label_allocated)
7776     {
7777       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7778       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
7779                                        ranges_by_label,
7780                                        ranges_by_label_allocated);
7781       memset (ranges_by_label + ranges_by_label_in_use, 0,
7782               RANGES_TABLE_INCREMENT
7783               * sizeof (struct dw_ranges_by_label_struct));
7784     }
7785
7786   ranges_by_label[in_use].begin = begin;
7787   ranges_by_label[in_use].end = end;
7788   ranges_by_label_in_use = in_use + 1;
7789
7790   return add_ranges_num (-(int)in_use - 1);
7791 }
7792
7793 static void
7794 output_ranges (void)
7795 {
7796   unsigned i;
7797   static const char *const start_fmt = "Offset 0x%x";
7798   const char *fmt = start_fmt;
7799
7800   for (i = 0; i < ranges_table_in_use; i++)
7801     {
7802       int block_num = ranges_table[i].num;
7803
7804       if (block_num > 0)
7805         {
7806           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7807           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7808
7809           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7810           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7811
7812           /* If all code is in the text section, then the compilation
7813              unit base address defaults to DW_AT_low_pc, which is the
7814              base of the text section.  */
7815           if (!have_multiple_function_sections)
7816             {
7817               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7818                                     text_section_label,
7819                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7820               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7821                                     text_section_label, NULL);
7822             }
7823
7824           /* Otherwise, the compilation unit base address is zero,
7825              which allows us to use absolute addresses, and not worry
7826              about whether the target supports cross-section
7827              arithmetic.  */
7828           else
7829             {
7830               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7831                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7832               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7833             }
7834
7835           fmt = NULL;
7836         }
7837
7838       /* Negative block_num stands for an index into ranges_by_label.  */
7839       else if (block_num < 0)
7840         {
7841           int lab_idx = - block_num - 1;
7842
7843           if (!have_multiple_function_sections)
7844             {
7845               gcc_unreachable ();
7846 #if 0
7847               /* If we ever use add_ranges_by_labels () for a single
7848                  function section, all we have to do is to take out
7849                  the #if 0 above.  */
7850               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7851                                     ranges_by_label[lab_idx].begin,
7852                                     text_section_label,
7853                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7854               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7855                                     ranges_by_label[lab_idx].end,
7856                                     text_section_label, NULL);
7857 #endif
7858             }
7859           else
7860             {
7861               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7862                                    ranges_by_label[lab_idx].begin,
7863                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7864               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7865                                    ranges_by_label[lab_idx].end,
7866                                    NULL);
7867             }
7868         }
7869       else
7870         {
7871           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7872           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7873           fmt = start_fmt;
7874         }
7875     }
7876 }
7877
7878 /* Data structure containing information about input files.  */
7879 struct file_info
7880 {
7881   const char *path;     /* Complete file name.  */
7882   const char *fname;    /* File name part.  */
7883   int length;           /* Length of entire string.  */
7884   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7885   int dir_idx;          /* Index in directory table.  */
7886 };
7887
7888 /* Data structure containing information about directories with source
7889    files.  */
7890 struct dir_info
7891 {
7892   const char *path;     /* Path including directory name.  */
7893   int length;           /* Path length.  */
7894   int prefix;           /* Index of directory entry which is a prefix.  */
7895   int count;            /* Number of files in this directory.  */
7896   int dir_idx;          /* Index of directory used as base.  */
7897 };
7898
7899 /* Callback function for file_info comparison.  We sort by looking at
7900    the directories in the path.  */
7901
7902 static int
7903 file_info_cmp (const void *p1, const void *p2)
7904 {
7905   const struct file_info *const s1 = (const struct file_info *) p1;
7906   const struct file_info *const s2 = (const struct file_info *) p2;
7907   const unsigned char *cp1;
7908   const unsigned char *cp2;
7909
7910   /* Take care of file names without directories.  We need to make sure that
7911      we return consistent values to qsort since some will get confused if
7912      we return the same value when identical operands are passed in opposite
7913      orders.  So if neither has a directory, return 0 and otherwise return
7914      1 or -1 depending on which one has the directory.  */
7915   if ((s1->path == s1->fname || s2->path == s2->fname))
7916     return (s2->path == s2->fname) - (s1->path == s1->fname);
7917
7918   cp1 = (const unsigned char *) s1->path;
7919   cp2 = (const unsigned char *) s2->path;
7920
7921   while (1)
7922     {
7923       ++cp1;
7924       ++cp2;
7925       /* Reached the end of the first path?  If so, handle like above.  */
7926       if ((cp1 == (const unsigned char *) s1->fname)
7927           || (cp2 == (const unsigned char *) s2->fname))
7928         return ((cp2 == (const unsigned char *) s2->fname)
7929                 - (cp1 == (const unsigned char *) s1->fname));
7930
7931       /* Character of current path component the same?  */
7932       else if (*cp1 != *cp2)
7933         return *cp1 - *cp2;
7934     }
7935 }
7936
7937 struct file_name_acquire_data
7938 {
7939   struct file_info *files;
7940   int used_files;
7941   int max_files;
7942 };
7943
7944 /* Traversal function for the hash table.  */
7945
7946 static int
7947 file_name_acquire (void ** slot, void *data)
7948 {
7949   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
7950   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
7951   struct file_info *fi;
7952   const char *f;
7953
7954   gcc_assert (fnad->max_files >= d->emitted_number);
7955
7956   if (! d->emitted_number)
7957     return 1;
7958
7959   gcc_assert (fnad->max_files != fnad->used_files);
7960
7961   fi = fnad->files + fnad->used_files++;
7962
7963   /* Skip all leading "./".  */
7964   f = d->filename;
7965   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7966     f += 2;
7967
7968   /* Create a new array entry.  */
7969   fi->path = f;
7970   fi->length = strlen (f);
7971   fi->file_idx = d;
7972
7973   /* Search for the file name part.  */
7974   f = strrchr (f, DIR_SEPARATOR);
7975 #if defined (DIR_SEPARATOR_2)
7976   {
7977     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7978
7979     if (g != NULL)
7980       {
7981         if (f == NULL || f < g)
7982           f = g;
7983       }
7984   }
7985 #endif
7986
7987   fi->fname = f == NULL ? fi->path : f + 1;
7988   return 1;
7989 }
7990
7991 /* Output the directory table and the file name table.  We try to minimize
7992    the total amount of memory needed.  A heuristic is used to avoid large
7993    slowdowns with many input files.  */
7994
7995 static void
7996 output_file_names (void)
7997 {
7998   struct file_name_acquire_data fnad;
7999   int numfiles;
8000   struct file_info *files;
8001   struct dir_info *dirs;
8002   int *saved;
8003   int *savehere;
8004   int *backmap;
8005   int ndirs;
8006   int idx_offset;
8007   int i;
8008   int idx;
8009
8010   if (!last_emitted_file)
8011     {
8012       dw2_asm_output_data (1, 0, "End directory table");
8013       dw2_asm_output_data (1, 0, "End file name table");
8014       return;
8015     }
8016
8017   numfiles = last_emitted_file->emitted_number;
8018
8019   /* Allocate the various arrays we need.  */
8020   files = XALLOCAVEC (struct file_info, numfiles);
8021   dirs = XALLOCAVEC (struct dir_info, numfiles);
8022
8023   fnad.files = files;
8024   fnad.used_files = 0;
8025   fnad.max_files = numfiles;
8026   htab_traverse (file_table, file_name_acquire, &fnad);
8027   gcc_assert (fnad.used_files == fnad.max_files);
8028
8029   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8030
8031   /* Find all the different directories used.  */
8032   dirs[0].path = files[0].path;
8033   dirs[0].length = files[0].fname - files[0].path;
8034   dirs[0].prefix = -1;
8035   dirs[0].count = 1;
8036   dirs[0].dir_idx = 0;
8037   files[0].dir_idx = 0;
8038   ndirs = 1;
8039
8040   for (i = 1; i < numfiles; i++)
8041     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8042         && memcmp (dirs[ndirs - 1].path, files[i].path,
8043                    dirs[ndirs - 1].length) == 0)
8044       {
8045         /* Same directory as last entry.  */
8046         files[i].dir_idx = ndirs - 1;
8047         ++dirs[ndirs - 1].count;
8048       }
8049     else
8050       {
8051         int j;
8052
8053         /* This is a new directory.  */
8054         dirs[ndirs].path = files[i].path;
8055         dirs[ndirs].length = files[i].fname - files[i].path;
8056         dirs[ndirs].count = 1;
8057         dirs[ndirs].dir_idx = ndirs;
8058         files[i].dir_idx = ndirs;
8059
8060         /* Search for a prefix.  */
8061         dirs[ndirs].prefix = -1;
8062         for (j = 0; j < ndirs; j++)
8063           if (dirs[j].length < dirs[ndirs].length
8064               && dirs[j].length > 1
8065               && (dirs[ndirs].prefix == -1
8066                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8067               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8068             dirs[ndirs].prefix = j;
8069
8070         ++ndirs;
8071       }
8072
8073   /* Now to the actual work.  We have to find a subset of the directories which
8074      allow expressing the file name using references to the directory table
8075      with the least amount of characters.  We do not do an exhaustive search
8076      where we would have to check out every combination of every single
8077      possible prefix.  Instead we use a heuristic which provides nearly optimal
8078      results in most cases and never is much off.  */
8079   saved = XALLOCAVEC (int, ndirs);
8080   savehere = XALLOCAVEC (int, ndirs);
8081
8082   memset (saved, '\0', ndirs * sizeof (saved[0]));
8083   for (i = 0; i < ndirs; i++)
8084     {
8085       int j;
8086       int total;
8087
8088       /* We can always save some space for the current directory.  But this
8089          does not mean it will be enough to justify adding the directory.  */
8090       savehere[i] = dirs[i].length;
8091       total = (savehere[i] - saved[i]) * dirs[i].count;
8092
8093       for (j = i + 1; j < ndirs; j++)
8094         {
8095           savehere[j] = 0;
8096           if (saved[j] < dirs[i].length)
8097             {
8098               /* Determine whether the dirs[i] path is a prefix of the
8099                  dirs[j] path.  */
8100               int k;
8101
8102               k = dirs[j].prefix;
8103               while (k != -1 && k != (int) i)
8104                 k = dirs[k].prefix;
8105
8106               if (k == (int) i)
8107                 {
8108                   /* Yes it is.  We can possibly save some memory by
8109                      writing the filenames in dirs[j] relative to
8110                      dirs[i].  */
8111                   savehere[j] = dirs[i].length;
8112                   total += (savehere[j] - saved[j]) * dirs[j].count;
8113                 }
8114             }
8115         }
8116
8117       /* Check whether we can save enough to justify adding the dirs[i]
8118          directory.  */
8119       if (total > dirs[i].length + 1)
8120         {
8121           /* It's worthwhile adding.  */
8122           for (j = i; j < ndirs; j++)
8123             if (savehere[j] > 0)
8124               {
8125                 /* Remember how much we saved for this directory so far.  */
8126                 saved[j] = savehere[j];
8127
8128                 /* Remember the prefix directory.  */
8129                 dirs[j].dir_idx = i;
8130               }
8131         }
8132     }
8133
8134   /* Emit the directory name table.  */
8135   idx = 1;
8136   idx_offset = dirs[0].length > 0 ? 1 : 0;
8137   for (i = 1 - idx_offset; i < ndirs; i++)
8138     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8139                             "Directory Entry: 0x%x", i + idx_offset);
8140
8141   dw2_asm_output_data (1, 0, "End directory table");
8142
8143   /* We have to emit them in the order of emitted_number since that's
8144      used in the debug info generation.  To do this efficiently we
8145      generate a back-mapping of the indices first.  */
8146   backmap = XALLOCAVEC (int, numfiles);
8147   for (i = 0; i < numfiles; i++)
8148     backmap[files[i].file_idx->emitted_number - 1] = i;
8149
8150   /* Now write all the file names.  */
8151   for (i = 0; i < numfiles; i++)
8152     {
8153       int file_idx = backmap[i];
8154       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8155
8156       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8157                               "File Entry: 0x%x", (unsigned) i + 1);
8158
8159       /* Include directory index.  */
8160       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8161
8162       /* Modification time.  */
8163       dw2_asm_output_data_uleb128 (0, NULL);
8164
8165       /* File length in bytes.  */
8166       dw2_asm_output_data_uleb128 (0, NULL);
8167     }
8168
8169   dw2_asm_output_data (1, 0, "End file name table");
8170 }
8171
8172
8173 /* Output the source line number correspondence information.  This
8174    information goes into the .debug_line section.  */
8175
8176 static void
8177 output_line_info (void)
8178 {
8179   char l1[20], l2[20], p1[20], p2[20];
8180   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8181   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8182   unsigned opc;
8183   unsigned n_op_args;
8184   unsigned long lt_index;
8185   unsigned long current_line;
8186   long line_offset;
8187   long line_delta;
8188   unsigned long current_file;
8189   unsigned long function;
8190
8191   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8192   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8193   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8194   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8195
8196   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8197     dw2_asm_output_data (4, 0xffffffff,
8198       "Initial length escape value indicating 64-bit DWARF extension");
8199   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8200                         "Length of Source Line Info");
8201   ASM_OUTPUT_LABEL (asm_out_file, l1);
8202
8203   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8204   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8205   ASM_OUTPUT_LABEL (asm_out_file, p1);
8206
8207   /* Define the architecture-dependent minimum instruction length (in
8208    bytes).  In this implementation of DWARF, this field is used for
8209    information purposes only.  Since GCC generates assembly language,
8210    we have no a priori knowledge of how many instruction bytes are
8211    generated for each source line, and therefore can use only the
8212    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8213    commands.  Accordingly, we fix this as `1', which is "correct
8214    enough" for all architectures, and don't let the target override.  */
8215   dw2_asm_output_data (1, 1,
8216                        "Minimum Instruction Length");
8217
8218   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8219                        "Default is_stmt_start flag");
8220   dw2_asm_output_data (1, DWARF_LINE_BASE,
8221                        "Line Base Value (Special Opcodes)");
8222   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8223                        "Line Range Value (Special Opcodes)");
8224   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8225                        "Special Opcode Base");
8226
8227   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8228     {
8229       switch (opc)
8230         {
8231         case DW_LNS_advance_pc:
8232         case DW_LNS_advance_line:
8233         case DW_LNS_set_file:
8234         case DW_LNS_set_column:
8235         case DW_LNS_fixed_advance_pc:
8236           n_op_args = 1;
8237           break;
8238         default:
8239           n_op_args = 0;
8240           break;
8241         }
8242
8243       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8244                            opc, n_op_args);
8245     }
8246
8247   /* Write out the information about the files we use.  */
8248   output_file_names ();
8249   ASM_OUTPUT_LABEL (asm_out_file, p2);
8250
8251   /* We used to set the address register to the first location in the text
8252      section here, but that didn't accomplish anything since we already
8253      have a line note for the opening brace of the first function.  */
8254
8255   /* Generate the line number to PC correspondence table, encoded as
8256      a series of state machine operations.  */
8257   current_file = 1;
8258   current_line = 1;
8259
8260   if (cfun && in_cold_section_p)
8261     strcpy (prev_line_label, crtl->subsections.cold_section_label);
8262   else
8263     strcpy (prev_line_label, text_section_label);
8264   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8265     {
8266       dw_line_info_ref line_info = &line_info_table[lt_index];
8267
8268 #if 0
8269       /* Disable this optimization for now; GDB wants to see two line notes
8270          at the beginning of a function so it can find the end of the
8271          prologue.  */
8272
8273       /* Don't emit anything for redundant notes.  Just updating the
8274          address doesn't accomplish anything, because we already assume
8275          that anything after the last address is this line.  */
8276       if (line_info->dw_line_num == current_line
8277           && line_info->dw_file_num == current_file)
8278         continue;
8279 #endif
8280
8281       /* Emit debug info for the address of the current line.
8282
8283          Unfortunately, we have little choice here currently, and must always
8284          use the most general form.  GCC does not know the address delta
8285          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8286          attributes which will give an upper bound on the address range.  We
8287          could perhaps use length attributes to determine when it is safe to
8288          use DW_LNS_fixed_advance_pc.  */
8289
8290       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8291       if (0)
8292         {
8293           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8294           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8295                                "DW_LNS_fixed_advance_pc");
8296           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8297         }
8298       else
8299         {
8300           /* This can handle any delta.  This takes
8301              4+DWARF2_ADDR_SIZE bytes.  */
8302           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8303           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8304           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8305           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8306         }
8307
8308       strcpy (prev_line_label, line_label);
8309
8310       /* Emit debug info for the source file of the current line, if
8311          different from the previous line.  */
8312       if (line_info->dw_file_num != current_file)
8313         {
8314           current_file = line_info->dw_file_num;
8315           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8316           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8317         }
8318
8319       /* Emit debug info for the current line number, choosing the encoding
8320          that uses the least amount of space.  */
8321       if (line_info->dw_line_num != current_line)
8322         {
8323           line_offset = line_info->dw_line_num - current_line;
8324           line_delta = line_offset - DWARF_LINE_BASE;
8325           current_line = line_info->dw_line_num;
8326           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8327             /* This can handle deltas from -10 to 234, using the current
8328                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8329                takes 1 byte.  */
8330             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8331                                  "line %lu", current_line);
8332           else
8333             {
8334               /* This can handle any delta.  This takes at least 4 bytes,
8335                  depending on the value being encoded.  */
8336               dw2_asm_output_data (1, DW_LNS_advance_line,
8337                                    "advance to line %lu", current_line);
8338               dw2_asm_output_data_sleb128 (line_offset, NULL);
8339               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8340             }
8341         }
8342       else
8343         /* We still need to start a new row, so output a copy insn.  */
8344         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8345     }
8346
8347   /* Emit debug info for the address of the end of the function.  */
8348   if (0)
8349     {
8350       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8351                            "DW_LNS_fixed_advance_pc");
8352       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8353     }
8354   else
8355     {
8356       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8357       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8358       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8359       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8360     }
8361
8362   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8363   dw2_asm_output_data_uleb128 (1, NULL);
8364   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8365
8366   function = 0;
8367   current_file = 1;
8368   current_line = 1;
8369   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8370     {
8371       dw_separate_line_info_ref line_info
8372         = &separate_line_info_table[lt_index];
8373
8374 #if 0
8375       /* Don't emit anything for redundant notes.  */
8376       if (line_info->dw_line_num == current_line
8377           && line_info->dw_file_num == current_file
8378           && line_info->function == function)
8379         goto cont;
8380 #endif
8381
8382       /* Emit debug info for the address of the current line.  If this is
8383          a new function, or the first line of a function, then we need
8384          to handle it differently.  */
8385       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8386                                    lt_index);
8387       if (function != line_info->function)
8388         {
8389           function = line_info->function;
8390
8391           /* Set the address register to the first line in the function.  */
8392           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8393           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8394           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8395           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8396         }
8397       else
8398         {
8399           /* ??? See the DW_LNS_advance_pc comment above.  */
8400           if (0)
8401             {
8402               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8403                                    "DW_LNS_fixed_advance_pc");
8404               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8405             }
8406           else
8407             {
8408               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8409               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8410               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8411               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8412             }
8413         }
8414
8415       strcpy (prev_line_label, line_label);
8416
8417       /* Emit debug info for the source file of the current line, if
8418          different from the previous line.  */
8419       if (line_info->dw_file_num != current_file)
8420         {
8421           current_file = line_info->dw_file_num;
8422           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8423           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8424         }
8425
8426       /* Emit debug info for the current line number, choosing the encoding
8427          that uses the least amount of space.  */
8428       if (line_info->dw_line_num != current_line)
8429         {
8430           line_offset = line_info->dw_line_num - current_line;
8431           line_delta = line_offset - DWARF_LINE_BASE;
8432           current_line = line_info->dw_line_num;
8433           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8434             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8435                                  "line %lu", current_line);
8436           else
8437             {
8438               dw2_asm_output_data (1, DW_LNS_advance_line,
8439                                    "advance to line %lu", current_line);
8440               dw2_asm_output_data_sleb128 (line_offset, NULL);
8441               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8442             }
8443         }
8444       else
8445         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8446
8447 #if 0
8448     cont:
8449 #endif
8450
8451       lt_index++;
8452
8453       /* If we're done with a function, end its sequence.  */
8454       if (lt_index == separate_line_info_table_in_use
8455           || separate_line_info_table[lt_index].function != function)
8456         {
8457           current_file = 1;
8458           current_line = 1;
8459
8460           /* Emit debug info for the address of the end of the function.  */
8461           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8462           if (0)
8463             {
8464               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8465                                    "DW_LNS_fixed_advance_pc");
8466               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8467             }
8468           else
8469             {
8470               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8471               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8472               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8473               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8474             }
8475
8476           /* Output the marker for the end of this sequence.  */
8477           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8478           dw2_asm_output_data_uleb128 (1, NULL);
8479           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8480         }
8481     }
8482
8483   /* Output the marker for the end of the line number info.  */
8484   ASM_OUTPUT_LABEL (asm_out_file, l2);
8485 }
8486 \f
8487 /* Given a pointer to a tree node for some base type, return a pointer to
8488    a DIE that describes the given type.
8489
8490    This routine must only be called for GCC type nodes that correspond to
8491    Dwarf base (fundamental) types.  */
8492
8493 static dw_die_ref
8494 base_type_die (tree type)
8495 {
8496   dw_die_ref base_type_result;
8497   enum dwarf_type encoding;
8498
8499   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8500     return 0;
8501
8502   switch (TREE_CODE (type))
8503     {
8504     case INTEGER_TYPE:
8505       if (TYPE_STRING_FLAG (type))
8506         {
8507           if (TYPE_UNSIGNED (type))
8508             encoding = DW_ATE_unsigned_char;
8509           else
8510             encoding = DW_ATE_signed_char;
8511         }
8512       else if (TYPE_UNSIGNED (type))
8513         encoding = DW_ATE_unsigned;
8514       else
8515         encoding = DW_ATE_signed;
8516       break;
8517
8518     case REAL_TYPE:
8519       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8520         encoding = DW_ATE_decimal_float;
8521       else
8522         encoding = DW_ATE_float;
8523       break;
8524
8525     case FIXED_POINT_TYPE:
8526       if (TYPE_UNSIGNED (type))
8527         encoding = DW_ATE_unsigned_fixed;
8528       else
8529         encoding = DW_ATE_signed_fixed;
8530       break;
8531
8532       /* Dwarf2 doesn't know anything about complex ints, so use
8533          a user defined type for it.  */
8534     case COMPLEX_TYPE:
8535       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8536         encoding = DW_ATE_complex_float;
8537       else
8538         encoding = DW_ATE_lo_user;
8539       break;
8540
8541     case BOOLEAN_TYPE:
8542       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8543       encoding = DW_ATE_boolean;
8544       break;
8545
8546     default:
8547       /* No other TREE_CODEs are Dwarf fundamental types.  */
8548       gcc_unreachable ();
8549     }
8550
8551   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8552
8553   /* This probably indicates a bug.  */
8554   if (! TYPE_NAME (type))
8555     add_name_attribute (base_type_result, "__unknown__");
8556
8557   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8558                    int_size_in_bytes (type));
8559   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8560
8561   return base_type_result;
8562 }
8563
8564 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8565    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8566
8567 static inline int
8568 is_base_type (tree type)
8569 {
8570   switch (TREE_CODE (type))
8571     {
8572     case ERROR_MARK:
8573     case VOID_TYPE:
8574     case INTEGER_TYPE:
8575     case REAL_TYPE:
8576     case FIXED_POINT_TYPE:
8577     case COMPLEX_TYPE:
8578     case BOOLEAN_TYPE:
8579       return 1;
8580
8581     case ARRAY_TYPE:
8582     case RECORD_TYPE:
8583     case UNION_TYPE:
8584     case QUAL_UNION_TYPE:
8585     case ENUMERAL_TYPE:
8586     case FUNCTION_TYPE:
8587     case METHOD_TYPE:
8588     case POINTER_TYPE:
8589     case REFERENCE_TYPE:
8590     case OFFSET_TYPE:
8591     case LANG_TYPE:
8592     case VECTOR_TYPE:
8593       return 0;
8594
8595     default:
8596       gcc_unreachable ();
8597     }
8598
8599   return 0;
8600 }
8601
8602 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8603    node, return the size in bits for the type if it is a constant, or else
8604    return the alignment for the type if the type's size is not constant, or
8605    else return BITS_PER_WORD if the type actually turns out to be an
8606    ERROR_MARK node.  */
8607
8608 static inline unsigned HOST_WIDE_INT
8609 simple_type_size_in_bits (const_tree type)
8610 {
8611   if (TREE_CODE (type) == ERROR_MARK)
8612     return BITS_PER_WORD;
8613   else if (TYPE_SIZE (type) == NULL_TREE)
8614     return 0;
8615   else if (host_integerp (TYPE_SIZE (type), 1))
8616     return tree_low_cst (TYPE_SIZE (type), 1);
8617   else
8618     return TYPE_ALIGN (type);
8619 }
8620
8621 /* Return true if the debug information for the given type should be
8622    emitted as a subrange type.  */
8623
8624 static inline bool
8625 is_subrange_type (const_tree type)
8626 {
8627   tree subtype = TREE_TYPE (type);
8628
8629   /* Subrange types are identified by the fact that they are integer
8630      types, and that they have a subtype which is either an integer type
8631      or an enumeral type.  */
8632
8633   if (TREE_CODE (type) != INTEGER_TYPE
8634       || subtype == NULL_TREE)
8635     return false;
8636
8637   if (TREE_CODE (subtype) != INTEGER_TYPE
8638       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8639     return false;
8640
8641   if (TREE_CODE (type) == TREE_CODE (subtype)
8642       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8643       && TYPE_MIN_VALUE (type) != NULL
8644       && TYPE_MIN_VALUE (subtype) != NULL
8645       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8646       && TYPE_MAX_VALUE (type) != NULL
8647       && TYPE_MAX_VALUE (subtype) != NULL
8648       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8649     {
8650       /* The type and its subtype have the same representation.  If in
8651          addition the two types also have the same name, then the given
8652          type is not a subrange type, but rather a plain base type.  */
8653       /* FIXME: brobecker/2004-03-22:
8654          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8655          therefore be sufficient to check the TYPE_SIZE node pointers
8656          rather than checking the actual size.  Unfortunately, we have
8657          found some cases, such as in the Ada "integer" type, where
8658          this is not the case.  Until this problem is solved, we need to
8659          keep checking the actual size.  */
8660       tree type_name = TYPE_NAME (type);
8661       tree subtype_name = TYPE_NAME (subtype);
8662
8663       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8664         type_name = DECL_NAME (type_name);
8665
8666       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8667         subtype_name = DECL_NAME (subtype_name);
8668
8669       if (type_name == subtype_name)
8670         return false;
8671     }
8672
8673   return true;
8674 }
8675
8676 /*  Given a pointer to a tree node for a subrange type, return a pointer
8677     to a DIE that describes the given type.  */
8678
8679 static dw_die_ref
8680 subrange_type_die (tree type, dw_die_ref context_die)
8681 {
8682   dw_die_ref subrange_die;
8683   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8684
8685   if (context_die == NULL)
8686     context_die = comp_unit_die;
8687
8688   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8689
8690   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8691     {
8692       /* The size of the subrange type and its base type do not match,
8693          so we need to generate a size attribute for the subrange type.  */
8694       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8695     }
8696
8697   if (TYPE_MIN_VALUE (type) != NULL)
8698     add_bound_info (subrange_die, DW_AT_lower_bound,
8699                     TYPE_MIN_VALUE (type));
8700   if (TYPE_MAX_VALUE (type) != NULL)
8701     add_bound_info (subrange_die, DW_AT_upper_bound,
8702                     TYPE_MAX_VALUE (type));
8703
8704   return subrange_die;
8705 }
8706
8707 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8708    entry that chains various modifiers in front of the given type.  */
8709
8710 static dw_die_ref
8711 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8712                    dw_die_ref context_die)
8713 {
8714   enum tree_code code = TREE_CODE (type);
8715   dw_die_ref mod_type_die;
8716   dw_die_ref sub_die = NULL;
8717   tree item_type = NULL;
8718   tree qualified_type;
8719   tree name;
8720
8721   if (code == ERROR_MARK)
8722     return NULL;
8723
8724   /* See if we already have the appropriately qualified variant of
8725      this type.  */
8726   qualified_type
8727     = get_qualified_type (type,
8728                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8729                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8730
8731   /* If we do, then we can just use its DIE, if it exists.  */
8732   if (qualified_type)
8733     {
8734       mod_type_die = lookup_type_die (qualified_type);
8735       if (mod_type_die)
8736         return mod_type_die;
8737     }
8738
8739   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8740
8741   /* Handle C typedef types.  */
8742   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8743     {
8744       tree dtype = TREE_TYPE (name);
8745
8746       if (qualified_type == dtype)
8747         {
8748           /* For a named type, use the typedef.  */
8749           gen_type_die (qualified_type, context_die);
8750           return lookup_type_die (qualified_type);
8751         }
8752       else if (is_const_type < TYPE_READONLY (dtype)
8753                || is_volatile_type < TYPE_VOLATILE (dtype)
8754                || (is_const_type <= TYPE_READONLY (dtype)
8755                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8756                    && DECL_ORIGINAL_TYPE (name) != type))
8757         /* cv-unqualified version of named type.  Just use the unnamed
8758            type to which it refers.  */
8759         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8760                                   is_const_type, is_volatile_type,
8761                                   context_die);
8762       /* Else cv-qualified version of named type; fall through.  */
8763     }
8764
8765   if (is_const_type)
8766     {
8767       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8768       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8769     }
8770   else if (is_volatile_type)
8771     {
8772       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8773       sub_die = modified_type_die (type, 0, 0, context_die);
8774     }
8775   else if (code == POINTER_TYPE)
8776     {
8777       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8778       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8779                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8780       item_type = TREE_TYPE (type);
8781     }
8782   else if (code == REFERENCE_TYPE)
8783     {
8784       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8785       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8786                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8787       item_type = TREE_TYPE (type);
8788     }
8789   else if (is_subrange_type (type))
8790     {
8791       mod_type_die = subrange_type_die (type, context_die);
8792       item_type = TREE_TYPE (type);
8793     }
8794   else if (is_base_type (type))
8795     mod_type_die = base_type_die (type);
8796   else
8797     {
8798       gen_type_die (type, context_die);
8799
8800       /* We have to get the type_main_variant here (and pass that to the
8801          `lookup_type_die' routine) because the ..._TYPE node we have
8802          might simply be a *copy* of some original type node (where the
8803          copy was created to help us keep track of typedef names) and
8804          that copy might have a different TYPE_UID from the original
8805          ..._TYPE node.  */
8806       if (TREE_CODE (type) != VECTOR_TYPE)
8807         return lookup_type_die (type_main_variant (type));
8808       else
8809         /* Vectors have the debugging information in the type,
8810            not the main variant.  */
8811         return lookup_type_die (type);
8812     }
8813
8814   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8815      don't output a DW_TAG_typedef, since there isn't one in the
8816      user's program; just attach a DW_AT_name to the type.  */
8817   if (name
8818       && (TREE_CODE (name) != TYPE_DECL
8819           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8820     {
8821       if (TREE_CODE (name) == TYPE_DECL)
8822         /* Could just call add_name_and_src_coords_attributes here,
8823            but since this is a builtin type it doesn't have any
8824            useful source coordinates anyway.  */
8825         name = DECL_NAME (name);
8826       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8827     }
8828
8829   if (qualified_type)
8830     equate_type_number_to_die (qualified_type, mod_type_die);
8831
8832   if (item_type)
8833     /* We must do this after the equate_type_number_to_die call, in case
8834        this is a recursive type.  This ensures that the modified_type_die
8835        recursion will terminate even if the type is recursive.  Recursive
8836        types are possible in Ada.  */
8837     sub_die = modified_type_die (item_type,
8838                                  TYPE_READONLY (item_type),
8839                                  TYPE_VOLATILE (item_type),
8840                                  context_die);
8841
8842   if (sub_die != NULL)
8843     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8844
8845   return mod_type_die;
8846 }
8847
8848 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8849    an enumerated type.  */
8850
8851 static inline int
8852 type_is_enum (const_tree type)
8853 {
8854   return TREE_CODE (type) == ENUMERAL_TYPE;
8855 }
8856
8857 /* Return the DBX register number described by a given RTL node.  */
8858
8859 static unsigned int
8860 dbx_reg_number (const_rtx rtl)
8861 {
8862   unsigned regno = REGNO (rtl);
8863
8864   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8865
8866 #ifdef LEAF_REG_REMAP
8867   if (current_function_uses_only_leaf_regs)
8868     {
8869       int leaf_reg = LEAF_REG_REMAP (regno);
8870       if (leaf_reg != -1)
8871         regno = (unsigned) leaf_reg;
8872     }
8873 #endif
8874
8875   return DBX_REGISTER_NUMBER (regno);
8876 }
8877
8878 /* Optionally add a DW_OP_piece term to a location description expression.
8879    DW_OP_piece is only added if the location description expression already
8880    doesn't end with DW_OP_piece.  */
8881
8882 static void
8883 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8884 {
8885   dw_loc_descr_ref loc;
8886
8887   if (*list_head != NULL)
8888     {
8889       /* Find the end of the chain.  */
8890       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8891         ;
8892
8893       if (loc->dw_loc_opc != DW_OP_piece)
8894         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8895     }
8896 }
8897
8898 /* Return a location descriptor that designates a machine register or
8899    zero if there is none.  */
8900
8901 static dw_loc_descr_ref
8902 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8903 {
8904   rtx regs;
8905
8906   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8907     return 0;
8908
8909   regs = targetm.dwarf_register_span (rtl);
8910
8911   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8912     return multiple_reg_loc_descriptor (rtl, regs, initialized);
8913   else
8914     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8915 }
8916
8917 /* Return a location descriptor that designates a machine register for
8918    a given hard register number.  */
8919
8920 static dw_loc_descr_ref
8921 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8922 {
8923   dw_loc_descr_ref reg_loc_descr;
8924   if (regno <= 31)
8925     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8926   else
8927     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8928
8929   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8930     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8931
8932   return reg_loc_descr;
8933 }
8934
8935 /* Given an RTL of a register, return a location descriptor that
8936    designates a value that spans more than one register.  */
8937
8938 static dw_loc_descr_ref
8939 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
8940                              enum var_init_status initialized)
8941 {
8942   int nregs, size, i;
8943   unsigned reg;
8944   dw_loc_descr_ref loc_result = NULL;
8945
8946   reg = REGNO (rtl);
8947 #ifdef LEAF_REG_REMAP
8948   if (current_function_uses_only_leaf_regs)
8949     {
8950       int leaf_reg = LEAF_REG_REMAP (reg);
8951       if (leaf_reg != -1)
8952         reg = (unsigned) leaf_reg;
8953     }
8954 #endif
8955   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8956   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8957
8958   /* Simple, contiguous registers.  */
8959   if (regs == NULL_RTX)
8960     {
8961       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8962
8963       loc_result = NULL;
8964       while (nregs--)
8965         {
8966           dw_loc_descr_ref t;
8967
8968           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8969                                       VAR_INIT_STATUS_INITIALIZED);
8970           add_loc_descr (&loc_result, t);
8971           add_loc_descr_op_piece (&loc_result, size);
8972           ++reg;
8973         }
8974       return loc_result;
8975     }
8976
8977   /* Now onto stupid register sets in non contiguous locations.  */
8978
8979   gcc_assert (GET_CODE (regs) == PARALLEL);
8980
8981   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8982   loc_result = NULL;
8983
8984   for (i = 0; i < XVECLEN (regs, 0); ++i)
8985     {
8986       dw_loc_descr_ref t;
8987
8988       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8989                                   VAR_INIT_STATUS_INITIALIZED);
8990       add_loc_descr (&loc_result, t);
8991       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8992       add_loc_descr_op_piece (&loc_result, size);
8993     }
8994
8995   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8996     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8997   return loc_result;
8998 }
8999
9000 /* Return a location descriptor that designates a constant.  */
9001
9002 static dw_loc_descr_ref
9003 int_loc_descriptor (HOST_WIDE_INT i)
9004 {
9005   enum dwarf_location_atom op;
9006
9007   /* Pick the smallest representation of a constant, rather than just
9008      defaulting to the LEB encoding.  */
9009   if (i >= 0)
9010     {
9011       if (i <= 31)
9012         op = DW_OP_lit0 + i;
9013       else if (i <= 0xff)
9014         op = DW_OP_const1u;
9015       else if (i <= 0xffff)
9016         op = DW_OP_const2u;
9017       else if (HOST_BITS_PER_WIDE_INT == 32
9018                || i <= 0xffffffff)
9019         op = DW_OP_const4u;
9020       else
9021         op = DW_OP_constu;
9022     }
9023   else
9024     {
9025       if (i >= -0x80)
9026         op = DW_OP_const1s;
9027       else if (i >= -0x8000)
9028         op = DW_OP_const2s;
9029       else if (HOST_BITS_PER_WIDE_INT == 32
9030                || i >= -0x80000000)
9031         op = DW_OP_const4s;
9032       else
9033         op = DW_OP_consts;
9034     }
9035
9036   return new_loc_descr (op, i, 0);
9037 }
9038
9039 /* Return a location descriptor that designates a base+offset location.  */
9040
9041 static dw_loc_descr_ref
9042 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9043                  enum var_init_status initialized)
9044 {
9045   unsigned int regno;
9046   dw_loc_descr_ref result;
9047
9048   /* We only use "frame base" when we're sure we're talking about the
9049      post-prologue local stack frame.  We do this by *not* running
9050      register elimination until this point, and recognizing the special
9051      argument pointer and soft frame pointer rtx's.  */
9052   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9053     {
9054       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9055
9056       if (elim != reg)
9057         {
9058           if (GET_CODE (elim) == PLUS)
9059             {
9060               offset += INTVAL (XEXP (elim, 1));
9061               elim = XEXP (elim, 0);
9062             }
9063           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
9064                       : stack_pointer_rtx));
9065           offset += frame_pointer_fb_offset;
9066
9067           return new_loc_descr (DW_OP_fbreg, offset, 0);
9068         }
9069     }
9070
9071   regno = dbx_reg_number (reg);
9072   if (regno <= 31)
9073     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9074   else
9075     result = new_loc_descr (DW_OP_bregx, regno, offset);
9076
9077   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9078     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9079
9080   return result;
9081 }
9082
9083 /* Return true if this RTL expression describes a base+offset calculation.  */
9084
9085 static inline int
9086 is_based_loc (const_rtx rtl)
9087 {
9088   return (GET_CODE (rtl) == PLUS
9089           && ((REG_P (XEXP (rtl, 0))
9090                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9091                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9092 }
9093
9094 /* Return a descriptor that describes the concatenation of N locations
9095    used to form the address of a memory location.  */
9096
9097 static dw_loc_descr_ref
9098 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9099                             enum var_init_status initialized)
9100 {
9101   unsigned int i;
9102   dw_loc_descr_ref cc_loc_result = NULL;
9103   unsigned int n = XVECLEN (concatn, 0);
9104
9105   for (i = 0; i < n; ++i)
9106     {
9107       dw_loc_descr_ref ref;
9108       rtx x = XVECEXP (concatn, 0, i);
9109
9110       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9111       if (ref == NULL)
9112         return NULL;
9113
9114       add_loc_descr (&cc_loc_result, ref);
9115       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9116     }
9117
9118   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9119     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9120
9121   return cc_loc_result;
9122 }
9123
9124 /* The following routine converts the RTL for a variable or parameter
9125    (resident in memory) into an equivalent Dwarf representation of a
9126    mechanism for getting the address of that same variable onto the top of a
9127    hypothetical "address evaluation" stack.
9128
9129    When creating memory location descriptors, we are effectively transforming
9130    the RTL for a memory-resident object into its Dwarf postfix expression
9131    equivalent.  This routine recursively descends an RTL tree, turning
9132    it into Dwarf postfix code as it goes.
9133
9134    MODE is the mode of the memory reference, needed to handle some
9135    autoincrement addressing modes.
9136
9137    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9138    location list for RTL.
9139
9140    Return 0 if we can't represent the location.  */
9141
9142 static dw_loc_descr_ref
9143 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9144                     enum var_init_status initialized)
9145 {
9146   dw_loc_descr_ref mem_loc_result = NULL;
9147   enum dwarf_location_atom op;
9148
9149   /* Note that for a dynamically sized array, the location we will generate a
9150      description of here will be the lowest numbered location which is
9151      actually within the array.  That's *not* necessarily the same as the
9152      zeroth element of the array.  */
9153
9154   rtl = targetm.delegitimize_address (rtl);
9155
9156   switch (GET_CODE (rtl))
9157     {
9158     case POST_INC:
9159     case POST_DEC:
9160     case POST_MODIFY:
9161       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9162          just fall into the SUBREG code.  */
9163
9164       /* ... fall through ...  */
9165
9166     case SUBREG:
9167       /* The case of a subreg may arise when we have a local (register)
9168          variable or a formal (register) parameter which doesn't quite fill
9169          up an entire register.  For now, just assume that it is
9170          legitimate to make the Dwarf info refer to the whole register which
9171          contains the given subreg.  */
9172       rtl = XEXP (rtl, 0);
9173
9174       /* ... fall through ...  */
9175
9176     case REG:
9177       /* Whenever a register number forms a part of the description of the
9178          method for calculating the (dynamic) address of a memory resident
9179          object, DWARF rules require the register number be referred to as
9180          a "base register".  This distinction is not based in any way upon
9181          what category of register the hardware believes the given register
9182          belongs to.  This is strictly DWARF terminology we're dealing with
9183          here. Note that in cases where the location of a memory-resident
9184          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9185          OP_CONST (0)) the actual DWARF location descriptor that we generate
9186          may just be OP_BASEREG (basereg).  This may look deceptively like
9187          the object in question was allocated to a register (rather than in
9188          memory) so DWARF consumers need to be aware of the subtle
9189          distinction between OP_REG and OP_BASEREG.  */
9190       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9191         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9192       break;
9193
9194     case MEM:
9195       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9196                                            VAR_INIT_STATUS_INITIALIZED);
9197       if (mem_loc_result != 0)
9198         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9199       break;
9200
9201     case LO_SUM:
9202          rtl = XEXP (rtl, 1);
9203
9204       /* ... fall through ...  */
9205
9206     case LABEL_REF:
9207       /* Some ports can transform a symbol ref into a label ref, because
9208          the symbol ref is too far away and has to be dumped into a constant
9209          pool.  */
9210     case CONST:
9211     case SYMBOL_REF:
9212       /* Alternatively, the symbol in the constant pool might be referenced
9213          by a different symbol.  */
9214       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9215         {
9216           bool marked;
9217           rtx tmp = get_pool_constant_mark (rtl, &marked);
9218
9219           if (GET_CODE (tmp) == SYMBOL_REF)
9220             {
9221               rtl = tmp;
9222               if (CONSTANT_POOL_ADDRESS_P (tmp))
9223                 get_pool_constant_mark (tmp, &marked);
9224               else
9225                 marked = true;
9226             }
9227
9228           /* If all references to this pool constant were optimized away,
9229              it was not output and thus we can't represent it.
9230              FIXME: might try to use DW_OP_const_value here, though
9231              DW_OP_piece complicates it.  */
9232           if (!marked)
9233             return 0;
9234         }
9235
9236       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9237       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9238       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9239       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9240       break;
9241
9242     case PRE_MODIFY:
9243       /* Extract the PLUS expression nested inside and fall into
9244          PLUS code below.  */
9245       rtl = XEXP (rtl, 1);
9246       goto plus;
9247
9248     case PRE_INC:
9249     case PRE_DEC:
9250       /* Turn these into a PLUS expression and fall into the PLUS code
9251          below.  */
9252       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9253                           GEN_INT (GET_CODE (rtl) == PRE_INC
9254                                    ? GET_MODE_UNIT_SIZE (mode)
9255                                    : -GET_MODE_UNIT_SIZE (mode)));
9256
9257       /* ... fall through ...  */
9258
9259     case PLUS:
9260     plus:
9261       if (is_based_loc (rtl))
9262         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9263                                           INTVAL (XEXP (rtl, 1)),
9264                                           VAR_INIT_STATUS_INITIALIZED);
9265       else
9266         {
9267           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9268                                                VAR_INIT_STATUS_INITIALIZED);
9269           if (mem_loc_result == 0)
9270             break;
9271
9272           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9273               && INTVAL (XEXP (rtl, 1)) >= 0)
9274             add_loc_descr (&mem_loc_result,
9275                            new_loc_descr (DW_OP_plus_uconst,
9276                                           INTVAL (XEXP (rtl, 1)), 0));
9277           else
9278             {
9279               add_loc_descr (&mem_loc_result,
9280                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9281                                                  VAR_INIT_STATUS_INITIALIZED));
9282               add_loc_descr (&mem_loc_result,
9283                              new_loc_descr (DW_OP_plus, 0, 0));
9284             }
9285         }
9286       break;
9287
9288     /* If a pseudo-reg is optimized away, it is possible for it to
9289        be replaced with a MEM containing a multiply or shift.  */
9290     case MULT:
9291       op = DW_OP_mul;
9292       goto do_binop;
9293
9294     case ASHIFT:
9295       op = DW_OP_shl;
9296       goto do_binop;
9297
9298     case ASHIFTRT:
9299       op = DW_OP_shra;
9300       goto do_binop;
9301
9302     case LSHIFTRT:
9303       op = DW_OP_shr;
9304       goto do_binop;
9305
9306     do_binop:
9307       {
9308         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9309                                                    VAR_INIT_STATUS_INITIALIZED);
9310         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9311                                                    VAR_INIT_STATUS_INITIALIZED);
9312
9313         if (op0 == 0 || op1 == 0)
9314           break;
9315
9316         mem_loc_result = op0;
9317         add_loc_descr (&mem_loc_result, op1);
9318         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9319         break;
9320       }
9321
9322     case CONST_INT:
9323       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9324       break;
9325
9326     case CONCATN:
9327       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
9328                                                    VAR_INIT_STATUS_INITIALIZED);
9329       break;
9330
9331     default:
9332       gcc_unreachable ();
9333     }
9334
9335   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9336     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9337
9338   return mem_loc_result;
9339 }
9340
9341 /* Return a descriptor that describes the concatenation of two locations.
9342    This is typically a complex variable.  */
9343
9344 static dw_loc_descr_ref
9345 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9346 {
9347   dw_loc_descr_ref cc_loc_result = NULL;
9348   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9349   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9350
9351   if (x0_ref == 0 || x1_ref == 0)
9352     return 0;
9353
9354   cc_loc_result = x0_ref;
9355   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9356
9357   add_loc_descr (&cc_loc_result, x1_ref);
9358   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9359
9360   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9361     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9362
9363   return cc_loc_result;
9364 }
9365
9366 /* Return a descriptor that describes the concatenation of N
9367    locations.  */
9368
9369 static dw_loc_descr_ref
9370 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9371 {
9372   unsigned int i;
9373   dw_loc_descr_ref cc_loc_result = NULL;
9374   unsigned int n = XVECLEN (concatn, 0);
9375
9376   for (i = 0; i < n; ++i)
9377     {
9378       dw_loc_descr_ref ref;
9379       rtx x = XVECEXP (concatn, 0, i);
9380
9381       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9382       if (ref == NULL)
9383         return NULL;
9384
9385       add_loc_descr (&cc_loc_result, ref);
9386       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9387     }
9388
9389   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9390     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9391
9392   return cc_loc_result;
9393 }
9394
9395 /* Output a proper Dwarf location descriptor for a variable or parameter
9396    which is either allocated in a register or in a memory location.  For a
9397    register, we just generate an OP_REG and the register number.  For a
9398    memory location we provide a Dwarf postfix expression describing how to
9399    generate the (dynamic) address of the object onto the address stack.
9400
9401    If we don't know how to describe it, return 0.  */
9402
9403 static dw_loc_descr_ref
9404 loc_descriptor (rtx rtl, enum var_init_status initialized)
9405 {
9406   dw_loc_descr_ref loc_result = NULL;
9407
9408   switch (GET_CODE (rtl))
9409     {
9410     case SUBREG:
9411       /* The case of a subreg may arise when we have a local (register)
9412          variable or a formal (register) parameter which doesn't quite fill
9413          up an entire register.  For now, just assume that it is
9414          legitimate to make the Dwarf info refer to the whole register which
9415          contains the given subreg.  */
9416       rtl = SUBREG_REG (rtl);
9417
9418       /* ... fall through ...  */
9419
9420     case REG:
9421       loc_result = reg_loc_descriptor (rtl, initialized);
9422       break;
9423
9424     case MEM:
9425       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9426                                        initialized);
9427       break;
9428
9429     case CONCAT:
9430       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9431                                           initialized);
9432       break;
9433
9434     case CONCATN:
9435       loc_result = concatn_loc_descriptor (rtl, initialized);
9436       break;
9437
9438     case VAR_LOCATION:
9439       /* Single part.  */
9440       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9441         {
9442           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9443           break;
9444         }
9445
9446       rtl = XEXP (rtl, 1);
9447       /* FALLTHRU */
9448
9449     case PARALLEL:
9450       {
9451         rtvec par_elems = XVEC (rtl, 0);
9452         int num_elem = GET_NUM_ELEM (par_elems);
9453         enum machine_mode mode;
9454         int i;
9455
9456         /* Create the first one, so we have something to add to.  */
9457         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9458                                      initialized);
9459         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9460         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9461         for (i = 1; i < num_elem; i++)
9462           {
9463             dw_loc_descr_ref temp;
9464
9465             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9466                                    initialized);
9467             add_loc_descr (&loc_result, temp);
9468             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9469             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9470           }
9471       }
9472       break;
9473
9474     default:
9475       gcc_unreachable ();
9476     }
9477
9478   return loc_result;
9479 }
9480
9481 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9482    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9483    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9484    top-level invocation, and we require the address of LOC; is 0 if we require
9485    the value of LOC.  */
9486
9487 static dw_loc_descr_ref
9488 loc_descriptor_from_tree_1 (tree loc, int want_address)
9489 {
9490   dw_loc_descr_ref ret, ret1;
9491   int have_address = 0;
9492   enum dwarf_location_atom op;
9493
9494   /* ??? Most of the time we do not take proper care for sign/zero
9495      extending the values properly.  Hopefully this won't be a real
9496      problem...  */
9497
9498   switch (TREE_CODE (loc))
9499     {
9500     case ERROR_MARK:
9501       return 0;
9502
9503     case PLACEHOLDER_EXPR:
9504       /* This case involves extracting fields from an object to determine the
9505          position of other fields.  We don't try to encode this here.  The
9506          only user of this is Ada, which encodes the needed information using
9507          the names of types.  */
9508       return 0;
9509
9510     case CALL_EXPR:
9511       return 0;
9512
9513     case PREINCREMENT_EXPR:
9514     case PREDECREMENT_EXPR:
9515     case POSTINCREMENT_EXPR:
9516     case POSTDECREMENT_EXPR:
9517       /* There are no opcodes for these operations.  */
9518       return 0;
9519
9520     case ADDR_EXPR:
9521       /* If we already want an address, there's nothing we can do.  */
9522       if (want_address)
9523         return 0;
9524
9525       /* Otherwise, process the argument and look for the address.  */
9526       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9527
9528     case VAR_DECL:
9529       if (DECL_THREAD_LOCAL_P (loc))
9530         {
9531           rtx rtl;
9532           unsigned first_op;
9533           unsigned second_op;
9534
9535           if (targetm.have_tls)
9536             {
9537               /* If this is not defined, we have no way to emit the
9538                  data.  */
9539               if (!targetm.asm_out.output_dwarf_dtprel)
9540                 return 0;
9541
9542                /* The way DW_OP_GNU_push_tls_address is specified, we
9543                   can only look up addresses of objects in the current
9544                   module.  */
9545               if (DECL_EXTERNAL (loc))
9546                 return 0;
9547               first_op = INTERNAL_DW_OP_tls_addr;
9548               second_op = DW_OP_GNU_push_tls_address;
9549             }
9550           else
9551             {
9552               if (!targetm.emutls.debug_form_tls_address)
9553                 return 0;
9554               loc = emutls_decl (loc);
9555               first_op = DW_OP_addr;
9556               second_op = DW_OP_form_tls_address;
9557             }
9558
9559           rtl = rtl_for_decl_location (loc);
9560           if (rtl == NULL_RTX)
9561             return 0;
9562
9563           if (!MEM_P (rtl))
9564             return 0;
9565           rtl = XEXP (rtl, 0);
9566           if (! CONSTANT_P (rtl))
9567             return 0;
9568
9569           ret = new_loc_descr (first_op, 0, 0);
9570           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9571           ret->dw_loc_oprnd1.v.val_addr = rtl;
9572
9573           ret1 = new_loc_descr (second_op, 0, 0);
9574           add_loc_descr (&ret, ret1);
9575
9576           have_address = 1;
9577           break;
9578         }
9579       /* FALLTHRU */
9580
9581     case PARM_DECL:
9582       if (DECL_HAS_VALUE_EXPR_P (loc))
9583         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9584                                            want_address);
9585       /* FALLTHRU */
9586
9587     case RESULT_DECL:
9588     case FUNCTION_DECL:
9589       {
9590         rtx rtl = rtl_for_decl_location (loc);
9591
9592         if (rtl == NULL_RTX)
9593           return 0;
9594         else if (GET_CODE (rtl) == CONST_INT)
9595           {
9596             HOST_WIDE_INT val = INTVAL (rtl);
9597             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9598               val &= GET_MODE_MASK (DECL_MODE (loc));
9599             ret = int_loc_descriptor (val);
9600           }
9601         else if (GET_CODE (rtl) == CONST_STRING)
9602           return 0;
9603         else if (CONSTANT_P (rtl))
9604           {
9605             ret = new_loc_descr (DW_OP_addr, 0, 0);
9606             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9607             ret->dw_loc_oprnd1.v.val_addr = rtl;
9608           }
9609         else
9610           {
9611             enum machine_mode mode;
9612
9613             /* Certain constructs can only be represented at top-level.  */
9614             if (want_address == 2)
9615               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9616
9617             mode = GET_MODE (rtl);
9618             if (MEM_P (rtl))
9619               {
9620                 rtl = XEXP (rtl, 0);
9621                 have_address = 1;
9622               }
9623             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9624           }
9625       }
9626       break;
9627
9628     case INDIRECT_REF:
9629       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9630       have_address = 1;
9631       break;
9632
9633     case COMPOUND_EXPR:
9634       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9635
9636     CASE_CONVERT:
9637     case VIEW_CONVERT_EXPR:
9638     case SAVE_EXPR:
9639     case MODIFY_EXPR:
9640       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
9641
9642     case COMPONENT_REF:
9643     case BIT_FIELD_REF:
9644     case ARRAY_REF:
9645     case ARRAY_RANGE_REF:
9646       {
9647         tree obj, offset;
9648         HOST_WIDE_INT bitsize, bitpos, bytepos;
9649         enum machine_mode mode;
9650         int volatilep;
9651         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9652
9653         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9654                                    &unsignedp, &volatilep, false);
9655
9656         if (obj == loc)
9657           return 0;
9658
9659         ret = loc_descriptor_from_tree_1 (obj, 1);
9660         if (ret == 0
9661             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9662           return 0;
9663
9664         if (offset != NULL_TREE)
9665           {
9666             /* Variable offset.  */
9667             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9668             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9669           }
9670
9671         bytepos = bitpos / BITS_PER_UNIT;
9672         if (bytepos > 0)
9673           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9674         else if (bytepos < 0)
9675           {
9676             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9677             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9678           }
9679
9680         have_address = 1;
9681         break;
9682       }
9683
9684     case INTEGER_CST:
9685       if (host_integerp (loc, 0))
9686         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9687       else
9688         return 0;
9689       break;
9690
9691     case CONSTRUCTOR:
9692       {
9693         /* Get an RTL for this, if something has been emitted.  */
9694         rtx rtl = lookup_constant_def (loc);
9695         enum machine_mode mode;
9696
9697         if (!rtl || !MEM_P (rtl))
9698           return 0;
9699         mode = GET_MODE (rtl);
9700         rtl = XEXP (rtl, 0);
9701         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9702         have_address = 1;
9703         break;
9704       }
9705
9706     case TRUTH_AND_EXPR:
9707     case TRUTH_ANDIF_EXPR:
9708     case BIT_AND_EXPR:
9709       op = DW_OP_and;
9710       goto do_binop;
9711
9712     case TRUTH_XOR_EXPR:
9713     case BIT_XOR_EXPR:
9714       op = DW_OP_xor;
9715       goto do_binop;
9716
9717     case TRUTH_OR_EXPR:
9718     case TRUTH_ORIF_EXPR:
9719     case BIT_IOR_EXPR:
9720       op = DW_OP_or;
9721       goto do_binop;
9722
9723     case FLOOR_DIV_EXPR:
9724     case CEIL_DIV_EXPR:
9725     case ROUND_DIV_EXPR:
9726     case TRUNC_DIV_EXPR:
9727       op = DW_OP_div;
9728       goto do_binop;
9729
9730     case MINUS_EXPR:
9731       op = DW_OP_minus;
9732       goto do_binop;
9733
9734     case FLOOR_MOD_EXPR:
9735     case CEIL_MOD_EXPR:
9736     case ROUND_MOD_EXPR:
9737     case TRUNC_MOD_EXPR:
9738       op = DW_OP_mod;
9739       goto do_binop;
9740
9741     case MULT_EXPR:
9742       op = DW_OP_mul;
9743       goto do_binop;
9744
9745     case LSHIFT_EXPR:
9746       op = DW_OP_shl;
9747       goto do_binop;
9748
9749     case RSHIFT_EXPR:
9750       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9751       goto do_binop;
9752
9753     case POINTER_PLUS_EXPR:
9754     case PLUS_EXPR:
9755       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9756           && host_integerp (TREE_OPERAND (loc, 1), 0))
9757         {
9758           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9759           if (ret == 0)
9760             return 0;
9761
9762           add_loc_descr (&ret,
9763                          new_loc_descr (DW_OP_plus_uconst,
9764                                         tree_low_cst (TREE_OPERAND (loc, 1),
9765                                                       0),
9766                                         0));
9767           break;
9768         }
9769
9770       op = DW_OP_plus;
9771       goto do_binop;
9772
9773     case LE_EXPR:
9774       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9775         return 0;
9776
9777       op = DW_OP_le;
9778       goto do_binop;
9779
9780     case GE_EXPR:
9781       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9782         return 0;
9783
9784       op = DW_OP_ge;
9785       goto do_binop;
9786
9787     case LT_EXPR:
9788       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9789         return 0;
9790
9791       op = DW_OP_lt;
9792       goto do_binop;
9793
9794     case GT_EXPR:
9795       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9796         return 0;
9797
9798       op = DW_OP_gt;
9799       goto do_binop;
9800
9801     case EQ_EXPR:
9802       op = DW_OP_eq;
9803       goto do_binop;
9804
9805     case NE_EXPR:
9806       op = DW_OP_ne;
9807       goto do_binop;
9808
9809     do_binop:
9810       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9811       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9812       if (ret == 0 || ret1 == 0)
9813         return 0;
9814
9815       add_loc_descr (&ret, ret1);
9816       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9817       break;
9818
9819     case TRUTH_NOT_EXPR:
9820     case BIT_NOT_EXPR:
9821       op = DW_OP_not;
9822       goto do_unop;
9823
9824     case ABS_EXPR:
9825       op = DW_OP_abs;
9826       goto do_unop;
9827
9828     case NEGATE_EXPR:
9829       op = DW_OP_neg;
9830       goto do_unop;
9831
9832     do_unop:
9833       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9834       if (ret == 0)
9835         return 0;
9836
9837       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9838       break;
9839
9840     case MIN_EXPR:
9841     case MAX_EXPR:
9842       {
9843         const enum tree_code code =
9844           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9845
9846         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9847                       build2 (code, integer_type_node,
9848                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9849                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9850       }
9851
9852       /* ... fall through ...  */
9853
9854     case COND_EXPR:
9855       {
9856         dw_loc_descr_ref lhs
9857           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9858         dw_loc_descr_ref rhs
9859           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9860         dw_loc_descr_ref bra_node, jump_node, tmp;
9861
9862         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9863         if (ret == 0 || lhs == 0 || rhs == 0)
9864           return 0;
9865
9866         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9867         add_loc_descr (&ret, bra_node);
9868
9869         add_loc_descr (&ret, rhs);
9870         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9871         add_loc_descr (&ret, jump_node);
9872
9873         add_loc_descr (&ret, lhs);
9874         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9875         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9876
9877         /* ??? Need a node to point the skip at.  Use a nop.  */
9878         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9879         add_loc_descr (&ret, tmp);
9880         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9881         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9882       }
9883       break;
9884
9885     case FIX_TRUNC_EXPR:
9886       return 0;
9887
9888     default:
9889       /* Leave front-end specific codes as simply unknown.  This comes
9890          up, for instance, with the C STMT_EXPR.  */
9891       if ((unsigned int) TREE_CODE (loc)
9892           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9893         return 0;
9894
9895 #ifdef ENABLE_CHECKING
9896       /* Otherwise this is a generic code; we should just lists all of
9897          these explicitly.  We forgot one.  */
9898       gcc_unreachable ();
9899 #else
9900       /* In a release build, we want to degrade gracefully: better to
9901          generate incomplete debugging information than to crash.  */
9902       return NULL;
9903 #endif
9904     }
9905
9906   /* Show if we can't fill the request for an address.  */
9907   if (want_address && !have_address)
9908     return 0;
9909
9910   /* If we've got an address and don't want one, dereference.  */
9911   if (!want_address && have_address && ret)
9912     {
9913       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9914
9915       if (size > DWARF2_ADDR_SIZE || size == -1)
9916         return 0;
9917       else if (size == DWARF2_ADDR_SIZE)
9918         op = DW_OP_deref;
9919       else
9920         op = DW_OP_deref_size;
9921
9922       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9923     }
9924
9925   return ret;
9926 }
9927
9928 static inline dw_loc_descr_ref
9929 loc_descriptor_from_tree (tree loc)
9930 {
9931   return loc_descriptor_from_tree_1 (loc, 2);
9932 }
9933
9934 /* Given a value, round it up to the lowest multiple of `boundary'
9935    which is not less than the value itself.  */
9936
9937 static inline HOST_WIDE_INT
9938 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9939 {
9940   return (((value + boundary - 1) / boundary) * boundary);
9941 }
9942
9943 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9944    pointer to the declared type for the relevant field variable, or return
9945    `integer_type_node' if the given node turns out to be an
9946    ERROR_MARK node.  */
9947
9948 static inline tree
9949 field_type (const_tree decl)
9950 {
9951   tree type;
9952
9953   if (TREE_CODE (decl) == ERROR_MARK)
9954     return integer_type_node;
9955
9956   type = DECL_BIT_FIELD_TYPE (decl);
9957   if (type == NULL_TREE)
9958     type = TREE_TYPE (decl);
9959
9960   return type;
9961 }
9962
9963 /* Given a pointer to a tree node, return the alignment in bits for
9964    it, or else return BITS_PER_WORD if the node actually turns out to
9965    be an ERROR_MARK node.  */
9966
9967 static inline unsigned
9968 simple_type_align_in_bits (const_tree type)
9969 {
9970   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9971 }
9972
9973 static inline unsigned
9974 simple_decl_align_in_bits (const_tree decl)
9975 {
9976   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9977 }
9978
9979 /* Return the result of rounding T up to ALIGN.  */
9980
9981 static inline HOST_WIDE_INT
9982 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9983 {
9984   /* We must be careful if T is negative because HOST_WIDE_INT can be
9985      either "above" or "below" unsigned int as per the C promotion
9986      rules, depending on the host, thus making the signedness of the
9987      direct multiplication and division unpredictable.  */
9988   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9989
9990   u += align - 1;
9991   u /= align;
9992   u *= align;
9993
9994   return (HOST_WIDE_INT) u;
9995 }
9996
9997 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9998    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9999    or return 0 if we are unable to determine what that offset is, either
10000    because the argument turns out to be a pointer to an ERROR_MARK node, or
10001    because the offset is actually variable.  (We can't handle the latter case
10002    just yet).  */
10003
10004 static HOST_WIDE_INT
10005 field_byte_offset (const_tree decl)
10006 {
10007   HOST_WIDE_INT object_offset_in_bits;
10008   HOST_WIDE_INT bitpos_int;
10009
10010   if (TREE_CODE (decl) == ERROR_MARK)
10011     return 0;
10012
10013   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
10014
10015   /* We cannot yet cope with fields whose positions are variable, so
10016      for now, when we see such things, we simply return 0.  Someday, we may
10017      be able to handle such cases, but it will be damn difficult.  */
10018   if (! host_integerp (bit_position (decl), 0))
10019     return 0;
10020
10021   bitpos_int = int_bit_position (decl);
10022
10023 #ifdef PCC_BITFIELD_TYPE_MATTERS
10024   if (PCC_BITFIELD_TYPE_MATTERS)
10025     {
10026       tree type;
10027       tree field_size_tree;
10028       HOST_WIDE_INT deepest_bitpos;
10029       unsigned HOST_WIDE_INT field_size_in_bits;
10030       unsigned int type_align_in_bits;
10031       unsigned int decl_align_in_bits;
10032       unsigned HOST_WIDE_INT type_size_in_bits;
10033
10034       type = field_type (decl);
10035       field_size_tree = DECL_SIZE (decl);
10036
10037       /* The size could be unspecified if there was an error, or for
10038          a flexible array member.  */
10039       if (! field_size_tree)
10040         field_size_tree = bitsize_zero_node;
10041
10042       /* If we don't know the size of the field, pretend it's a full word.  */
10043       if (host_integerp (field_size_tree, 1))
10044         field_size_in_bits = tree_low_cst (field_size_tree, 1);
10045       else
10046         field_size_in_bits = BITS_PER_WORD;
10047
10048       type_size_in_bits = simple_type_size_in_bits (type);
10049       type_align_in_bits = simple_type_align_in_bits (type);
10050       decl_align_in_bits = simple_decl_align_in_bits (decl);
10051
10052       /* The GCC front-end doesn't make any attempt to keep track of the
10053          starting bit offset (relative to the start of the containing
10054          structure type) of the hypothetical "containing object" for a
10055          bit-field.  Thus, when computing the byte offset value for the
10056          start of the "containing object" of a bit-field, we must deduce
10057          this information on our own. This can be rather tricky to do in
10058          some cases.  For example, handling the following structure type
10059          definition when compiling for an i386/i486 target (which only
10060          aligns long long's to 32-bit boundaries) can be very tricky:
10061
10062          struct S { int field1; long long field2:31; };
10063
10064          Fortunately, there is a simple rule-of-thumb which can be used
10065          in such cases.  When compiling for an i386/i486, GCC will
10066          allocate 8 bytes for the structure shown above.  It decides to
10067          do this based upon one simple rule for bit-field allocation.
10068          GCC allocates each "containing object" for each bit-field at
10069          the first (i.e. lowest addressed) legitimate alignment boundary
10070          (based upon the required minimum alignment for the declared
10071          type of the field) which it can possibly use, subject to the
10072          condition that there is still enough available space remaining
10073          in the containing object (when allocated at the selected point)
10074          to fully accommodate all of the bits of the bit-field itself.
10075
10076          This simple rule makes it obvious why GCC allocates 8 bytes for
10077          each object of the structure type shown above.  When looking
10078          for a place to allocate the "containing object" for `field2',
10079          the compiler simply tries to allocate a 64-bit "containing
10080          object" at each successive 32-bit boundary (starting at zero)
10081          until it finds a place to allocate that 64- bit field such that
10082          at least 31 contiguous (and previously unallocated) bits remain
10083          within that selected 64 bit field.  (As it turns out, for the
10084          example above, the compiler finds it is OK to allocate the
10085          "containing object" 64-bit field at bit-offset zero within the
10086          structure type.)
10087
10088          Here we attempt to work backwards from the limited set of facts
10089          we're given, and we try to deduce from those facts, where GCC
10090          must have believed that the containing object started (within
10091          the structure type). The value we deduce is then used (by the
10092          callers of this routine) to generate DW_AT_location and
10093          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10094          the case of DW_AT_location, regular fields as well).  */
10095
10096       /* Figure out the bit-distance from the start of the structure to
10097          the "deepest" bit of the bit-field.  */
10098       deepest_bitpos = bitpos_int + field_size_in_bits;
10099
10100       /* This is the tricky part.  Use some fancy footwork to deduce
10101          where the lowest addressed bit of the containing object must
10102          be.  */
10103       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10104
10105       /* Round up to type_align by default.  This works best for
10106          bitfields.  */
10107       object_offset_in_bits
10108         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10109
10110       if (object_offset_in_bits > bitpos_int)
10111         {
10112           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10113
10114           /* Round up to decl_align instead.  */
10115           object_offset_in_bits
10116             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10117         }
10118     }
10119   else
10120 #endif
10121     object_offset_in_bits = bitpos_int;
10122
10123   return object_offset_in_bits / BITS_PER_UNIT;
10124 }
10125 \f
10126 /* The following routines define various Dwarf attributes and any data
10127    associated with them.  */
10128
10129 /* Add a location description attribute value to a DIE.
10130
10131    This emits location attributes suitable for whole variables and
10132    whole parameters.  Note that the location attributes for struct fields are
10133    generated by the routine `data_member_location_attribute' below.  */
10134
10135 static inline void
10136 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10137                              dw_loc_descr_ref descr)
10138 {
10139   if (descr != 0)
10140     add_AT_loc (die, attr_kind, descr);
10141 }
10142
10143 /* Attach the specialized form of location attribute used for data members of
10144    struct and union types.  In the special case of a FIELD_DECL node which
10145    represents a bit-field, the "offset" part of this special location
10146    descriptor must indicate the distance in bytes from the lowest-addressed
10147    byte of the containing struct or union type to the lowest-addressed byte of
10148    the "containing object" for the bit-field.  (See the `field_byte_offset'
10149    function above).
10150
10151    For any given bit-field, the "containing object" is a hypothetical object
10152    (of some integral or enum type) within which the given bit-field lives.  The
10153    type of this hypothetical "containing object" is always the same as the
10154    declared type of the individual bit-field itself (for GCC anyway... the
10155    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10156    bytes) of the hypothetical "containing object" which will be given in the
10157    DW_AT_byte_size attribute for this bit-field.  (See the
10158    `byte_size_attribute' function below.)  It is also used when calculating the
10159    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10160    function below.)  */
10161
10162 static void
10163 add_data_member_location_attribute (dw_die_ref die, tree decl)
10164 {
10165   HOST_WIDE_INT offset;
10166   dw_loc_descr_ref loc_descr = 0;
10167
10168   if (TREE_CODE (decl) == TREE_BINFO)
10169     {
10170       /* We're working on the TAG_inheritance for a base class.  */
10171       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10172         {
10173           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10174              aren't at a fixed offset from all (sub)objects of the same
10175              type.  We need to extract the appropriate offset from our
10176              vtable.  The following dwarf expression means
10177
10178                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10179
10180              This is specific to the V3 ABI, of course.  */
10181
10182           dw_loc_descr_ref tmp;
10183
10184           /* Make a copy of the object address.  */
10185           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10186           add_loc_descr (&loc_descr, tmp);
10187
10188           /* Extract the vtable address.  */
10189           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10190           add_loc_descr (&loc_descr, tmp);
10191
10192           /* Calculate the address of the offset.  */
10193           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10194           gcc_assert (offset < 0);
10195
10196           tmp = int_loc_descriptor (-offset);
10197           add_loc_descr (&loc_descr, tmp);
10198           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10199           add_loc_descr (&loc_descr, tmp);
10200
10201           /* Extract the offset.  */
10202           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10203           add_loc_descr (&loc_descr, tmp);
10204
10205           /* Add it to the object address.  */
10206           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10207           add_loc_descr (&loc_descr, tmp);
10208         }
10209       else
10210         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10211     }
10212   else
10213     offset = field_byte_offset (decl);
10214
10215   if (! loc_descr)
10216     {
10217       enum dwarf_location_atom op;
10218
10219       /* The DWARF2 standard says that we should assume that the structure
10220          address is already on the stack, so we can specify a structure field
10221          address by using DW_OP_plus_uconst.  */
10222
10223 #ifdef MIPS_DEBUGGING_INFO
10224       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10225          operator correctly.  It works only if we leave the offset on the
10226          stack.  */
10227       op = DW_OP_constu;
10228 #else
10229       op = DW_OP_plus_uconst;
10230 #endif
10231
10232       loc_descr = new_loc_descr (op, offset, 0);
10233     }
10234
10235   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10236 }
10237
10238 /* Writes integer values to dw_vec_const array.  */
10239
10240 static void
10241 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10242 {
10243   while (size != 0)
10244     {
10245       *dest++ = val & 0xff;
10246       val >>= 8;
10247       --size;
10248     }
10249 }
10250
10251 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10252
10253 static HOST_WIDE_INT
10254 extract_int (const unsigned char *src, unsigned int size)
10255 {
10256   HOST_WIDE_INT val = 0;
10257
10258   src += size;
10259   while (size != 0)
10260     {
10261       val <<= 8;
10262       val |= *--src & 0xff;
10263       --size;
10264     }
10265   return val;
10266 }
10267
10268 /* Writes floating point values to dw_vec_const array.  */
10269
10270 static void
10271 insert_float (const_rtx rtl, unsigned char *array)
10272 {
10273   REAL_VALUE_TYPE rv;
10274   long val[4];
10275   int i;
10276
10277   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10278   real_to_target (val, &rv, GET_MODE (rtl));
10279
10280   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10281   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10282     {
10283       insert_int (val[i], 4, array);
10284       array += 4;
10285     }
10286 }
10287
10288 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10289    does not have a "location" either in memory or in a register.  These
10290    things can arise in GNU C when a constant is passed as an actual parameter
10291    to an inlined function.  They can also arise in C++ where declared
10292    constants do not necessarily get memory "homes".  */
10293
10294 static void
10295 add_const_value_attribute (dw_die_ref die, rtx rtl)
10296 {
10297   switch (GET_CODE (rtl))
10298     {
10299     case CONST_INT:
10300       {
10301         HOST_WIDE_INT val = INTVAL (rtl);
10302
10303         if (val < 0)
10304           add_AT_int (die, DW_AT_const_value, val);
10305         else
10306           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10307       }
10308       break;
10309
10310     case CONST_DOUBLE:
10311       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10312          floating-point constant.  A CONST_DOUBLE is used whenever the
10313          constant requires more than one word in order to be adequately
10314          represented.  We output CONST_DOUBLEs as blocks.  */
10315       {
10316         enum machine_mode mode = GET_MODE (rtl);
10317
10318         if (SCALAR_FLOAT_MODE_P (mode))
10319           {
10320             unsigned int length = GET_MODE_SIZE (mode);
10321             unsigned char *array = GGC_NEWVEC (unsigned char, length);
10322
10323             insert_float (rtl, array);
10324             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10325           }
10326         else
10327           {
10328             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10329             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10330
10331             add_AT_long_long (die, DW_AT_const_value,
10332                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10333           }
10334       }
10335       break;
10336
10337     case CONST_VECTOR:
10338       {
10339         enum machine_mode mode = GET_MODE (rtl);
10340         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10341         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10342         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
10343         unsigned int i;
10344         unsigned char *p;
10345
10346         switch (GET_MODE_CLASS (mode))
10347           {
10348           case MODE_VECTOR_INT:
10349             for (i = 0, p = array; i < length; i++, p += elt_size)
10350               {
10351                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10352                 HOST_WIDE_INT lo, hi;
10353
10354                 switch (GET_CODE (elt))
10355                   {
10356                   case CONST_INT:
10357                     lo = INTVAL (elt);
10358                     hi = -(lo < 0);
10359                     break;
10360
10361                   case CONST_DOUBLE:
10362                     lo = CONST_DOUBLE_LOW (elt);
10363                     hi = CONST_DOUBLE_HIGH (elt);
10364                     break;
10365
10366                   default:
10367                     gcc_unreachable ();
10368                   }
10369
10370                 if (elt_size <= sizeof (HOST_WIDE_INT))
10371                   insert_int (lo, elt_size, p);
10372                 else
10373                   {
10374                     unsigned char *p0 = p;
10375                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10376
10377                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10378                     if (WORDS_BIG_ENDIAN)
10379                       {
10380                         p0 = p1;
10381                         p1 = p;
10382                       }
10383                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10384                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10385                   }
10386               }
10387             break;
10388
10389           case MODE_VECTOR_FLOAT:
10390             for (i = 0, p = array; i < length; i++, p += elt_size)
10391               {
10392                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10393                 insert_float (elt, p);
10394               }
10395             break;
10396
10397           default:
10398             gcc_unreachable ();
10399           }
10400
10401         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10402       }
10403       break;
10404
10405     case CONST_STRING:
10406       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10407       break;
10408
10409     case SYMBOL_REF:
10410     case LABEL_REF:
10411     case CONST:
10412       add_AT_addr (die, DW_AT_const_value, rtl);
10413       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10414       break;
10415
10416     case PLUS:
10417       /* In cases where an inlined instance of an inline function is passed
10418          the address of an `auto' variable (which is local to the caller) we
10419          can get a situation where the DECL_RTL of the artificial local
10420          variable (for the inlining) which acts as a stand-in for the
10421          corresponding formal parameter (of the inline function) will look
10422          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10423          exactly a compile-time constant expression, but it isn't the address
10424          of the (artificial) local variable either.  Rather, it represents the
10425          *value* which the artificial local variable always has during its
10426          lifetime.  We currently have no way to represent such quasi-constant
10427          values in Dwarf, so for now we just punt and generate nothing.  */
10428       break;
10429
10430     default:
10431       /* No other kinds of rtx should be possible here.  */
10432       gcc_unreachable ();
10433     }
10434
10435 }
10436
10437 /* Determine whether the evaluation of EXPR references any variables
10438    or functions which aren't otherwise used (and therefore may not be
10439    output).  */
10440 static tree
10441 reference_to_unused (tree * tp, int * walk_subtrees,
10442                      void * data ATTRIBUTE_UNUSED)
10443 {
10444   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10445     *walk_subtrees = 0;
10446
10447   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10448       && ! TREE_ASM_WRITTEN (*tp))
10449     return *tp;
10450   /* ???  The C++ FE emits debug information for using decls, so
10451      putting gcc_unreachable here falls over.  See PR31899.  For now
10452      be conservative.  */
10453   else if (!cgraph_global_info_ready
10454            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10455     return *tp;
10456   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10457     {
10458       struct varpool_node *node = varpool_node (*tp);
10459       if (!node->needed)
10460         return *tp;
10461     }
10462   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10463            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10464     {
10465       struct cgraph_node *node = cgraph_node (*tp);
10466       if (!node->output)
10467         return *tp;
10468     }
10469   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10470     return *tp;
10471
10472   return NULL_TREE;
10473 }
10474
10475 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10476    for use in a later add_const_value_attribute call.  */
10477
10478 static rtx
10479 rtl_for_decl_init (tree init, tree type)
10480 {
10481   rtx rtl = NULL_RTX;
10482
10483   /* If a variable is initialized with a string constant without embedded
10484      zeros, build CONST_STRING.  */
10485   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10486     {
10487       tree enttype = TREE_TYPE (type);
10488       tree domain = TYPE_DOMAIN (type);
10489       enum machine_mode mode = TYPE_MODE (enttype);
10490
10491       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10492           && domain
10493           && integer_zerop (TYPE_MIN_VALUE (domain))
10494           && compare_tree_int (TYPE_MAX_VALUE (domain),
10495                                TREE_STRING_LENGTH (init) - 1) == 0
10496           && ((size_t) TREE_STRING_LENGTH (init)
10497               == strlen (TREE_STRING_POINTER (init)) + 1))
10498         rtl = gen_rtx_CONST_STRING (VOIDmode,
10499                                     ggc_strdup (TREE_STRING_POINTER (init)));
10500     }
10501   /* Other aggregates, and complex values, could be represented using
10502      CONCAT: FIXME!  */
10503   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10504     ;
10505   /* Vectors only work if their mode is supported by the target.
10506      FIXME: generic vectors ought to work too.  */
10507   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10508     ;
10509   /* If the initializer is something that we know will expand into an
10510      immediate RTL constant, expand it now.  We must be careful not to
10511      reference variables which won't be output.  */
10512   else if (initializer_constant_valid_p (init, type)
10513            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10514     {
10515       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10516          possible.  */
10517       if (TREE_CODE (type) == VECTOR_TYPE)
10518         switch (TREE_CODE (init))
10519           {
10520           case VECTOR_CST:
10521             break;
10522           case CONSTRUCTOR:
10523             if (TREE_CONSTANT (init))
10524               {
10525                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10526                 bool constant_p = true;
10527                 tree value;
10528                 unsigned HOST_WIDE_INT ix;
10529
10530                 /* Even when ctor is constant, it might contain non-*_CST
10531                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10532                    belong into VECTOR_CST nodes.  */
10533                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10534                   if (!CONSTANT_CLASS_P (value))
10535                     {
10536                       constant_p = false;
10537                       break;
10538                     }
10539
10540                 if (constant_p)
10541                   {
10542                     init = build_vector_from_ctor (type, elts);
10543                     break;
10544                   }
10545               }
10546             /* FALLTHRU */
10547
10548           default:
10549             return NULL;
10550           }
10551
10552       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10553
10554       /* If expand_expr returns a MEM, it wasn't immediate.  */
10555       gcc_assert (!rtl || !MEM_P (rtl));
10556     }
10557
10558   return rtl;
10559 }
10560
10561 /* Generate RTL for the variable DECL to represent its location.  */
10562
10563 static rtx
10564 rtl_for_decl_location (tree decl)
10565 {
10566   rtx rtl;
10567
10568   /* Here we have to decide where we are going to say the parameter "lives"
10569      (as far as the debugger is concerned).  We only have a couple of
10570      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10571
10572      DECL_RTL normally indicates where the parameter lives during most of the
10573      activation of the function.  If optimization is enabled however, this
10574      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10575      that the parameter doesn't really live anywhere (as far as the code
10576      generation parts of GCC are concerned) during most of the function's
10577      activation.  That will happen (for example) if the parameter is never
10578      referenced within the function.
10579
10580      We could just generate a location descriptor here for all non-NULL
10581      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10582      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10583      where DECL_RTL is NULL or is a pseudo-reg.
10584
10585      Note however that we can only get away with using DECL_INCOMING_RTL as
10586      a backup substitute for DECL_RTL in certain limited cases.  In cases
10587      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10588      we can be sure that the parameter was passed using the same type as it is
10589      declared to have within the function, and that its DECL_INCOMING_RTL
10590      points us to a place where a value of that type is passed.
10591
10592      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10593      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10594      because in these cases DECL_INCOMING_RTL points us to a value of some
10595      type which is *different* from the type of the parameter itself.  Thus,
10596      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10597      such cases, the debugger would end up (for example) trying to fetch a
10598      `float' from a place which actually contains the first part of a
10599      `double'.  That would lead to really incorrect and confusing
10600      output at debug-time.
10601
10602      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10603      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10604      are a couple of exceptions however.  On little-endian machines we can
10605      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10606      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10607      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10608      when (on a little-endian machine) a non-prototyped function has a
10609      parameter declared to be of type `short' or `char'.  In such cases,
10610      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10611      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10612      passed `int' value.  If the debugger then uses that address to fetch
10613      a `short' or a `char' (on a little-endian machine) the result will be
10614      the correct data, so we allow for such exceptional cases below.
10615
10616      Note that our goal here is to describe the place where the given formal
10617      parameter lives during most of the function's activation (i.e. between the
10618      end of the prologue and the start of the epilogue).  We'll do that as best
10619      as we can. Note however that if the given formal parameter is modified
10620      sometime during the execution of the function, then a stack backtrace (at
10621      debug-time) will show the function as having been called with the *new*
10622      value rather than the value which was originally passed in.  This happens
10623      rarely enough that it is not a major problem, but it *is* a problem, and
10624      I'd like to fix it.
10625
10626      A future version of dwarf2out.c may generate two additional attributes for
10627      any given DW_TAG_formal_parameter DIE which will describe the "passed
10628      type" and the "passed location" for the given formal parameter in addition
10629      to the attributes we now generate to indicate the "declared type" and the
10630      "active location" for each parameter.  This additional set of attributes
10631      could be used by debuggers for stack backtraces. Separately, note that
10632      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10633      This happens (for example) for inlined-instances of inline function formal
10634      parameters which are never referenced.  This really shouldn't be
10635      happening.  All PARM_DECL nodes should get valid non-NULL
10636      DECL_INCOMING_RTL values.  FIXME.  */
10637
10638   /* Use DECL_RTL as the "location" unless we find something better.  */
10639   rtl = DECL_RTL_IF_SET (decl);
10640
10641   /* When generating abstract instances, ignore everything except
10642      constants, symbols living in memory, and symbols living in
10643      fixed registers.  */
10644   if (! reload_completed)
10645     {
10646       if (rtl
10647           && (CONSTANT_P (rtl)
10648               || (MEM_P (rtl)
10649                   && CONSTANT_P (XEXP (rtl, 0)))
10650               || (REG_P (rtl)
10651                   && TREE_CODE (decl) == VAR_DECL
10652                   && TREE_STATIC (decl))))
10653         {
10654           rtl = targetm.delegitimize_address (rtl);
10655           return rtl;
10656         }
10657       rtl = NULL_RTX;
10658     }
10659   else if (TREE_CODE (decl) == PARM_DECL)
10660     {
10661       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10662         {
10663           tree declared_type = TREE_TYPE (decl);
10664           tree passed_type = DECL_ARG_TYPE (decl);
10665           enum machine_mode dmode = TYPE_MODE (declared_type);
10666           enum machine_mode pmode = TYPE_MODE (passed_type);
10667
10668           /* This decl represents a formal parameter which was optimized out.
10669              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10670              all cases where (rtl == NULL_RTX) just below.  */
10671           if (dmode == pmode)
10672             rtl = DECL_INCOMING_RTL (decl);
10673           else if (SCALAR_INT_MODE_P (dmode)
10674                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10675                    && DECL_INCOMING_RTL (decl))
10676             {
10677               rtx inc = DECL_INCOMING_RTL (decl);
10678               if (REG_P (inc))
10679                 rtl = inc;
10680               else if (MEM_P (inc))
10681                 {
10682                   if (BYTES_BIG_ENDIAN)
10683                     rtl = adjust_address_nv (inc, dmode,
10684                                              GET_MODE_SIZE (pmode)
10685                                              - GET_MODE_SIZE (dmode));
10686                   else
10687                     rtl = inc;
10688                 }
10689             }
10690         }
10691
10692       /* If the parm was passed in registers, but lives on the stack, then
10693          make a big endian correction if the mode of the type of the
10694          parameter is not the same as the mode of the rtl.  */
10695       /* ??? This is the same series of checks that are made in dbxout.c before
10696          we reach the big endian correction code there.  It isn't clear if all
10697          of these checks are necessary here, but keeping them all is the safe
10698          thing to do.  */
10699       else if (MEM_P (rtl)
10700                && XEXP (rtl, 0) != const0_rtx
10701                && ! CONSTANT_P (XEXP (rtl, 0))
10702                /* Not passed in memory.  */
10703                && !MEM_P (DECL_INCOMING_RTL (decl))
10704                /* Not passed by invisible reference.  */
10705                && (!REG_P (XEXP (rtl, 0))
10706                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10707                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10708 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10709                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10710 #endif
10711                      )
10712                /* Big endian correction check.  */
10713                && BYTES_BIG_ENDIAN
10714                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10715                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10716                    < UNITS_PER_WORD))
10717         {
10718           int offset = (UNITS_PER_WORD
10719                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10720
10721           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10722                              plus_constant (XEXP (rtl, 0), offset));
10723         }
10724     }
10725   else if (TREE_CODE (decl) == VAR_DECL
10726            && rtl
10727            && MEM_P (rtl)
10728            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10729            && BYTES_BIG_ENDIAN)
10730     {
10731       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10732       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10733
10734       /* If a variable is declared "register" yet is smaller than
10735          a register, then if we store the variable to memory, it
10736          looks like we're storing a register-sized value, when in
10737          fact we are not.  We need to adjust the offset of the
10738          storage location to reflect the actual value's bytes,
10739          else gdb will not be able to display it.  */
10740       if (rsize > dsize)
10741         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10742                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10743     }
10744
10745   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10746      and will have been substituted directly into all expressions that use it.
10747      C does not have such a concept, but C++ and other languages do.  */
10748   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10749     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10750
10751   if (rtl)
10752     rtl = targetm.delegitimize_address (rtl);
10753
10754   /* If we don't look past the constant pool, we risk emitting a
10755      reference to a constant pool entry that isn't referenced from
10756      code, and thus is not emitted.  */
10757   if (rtl)
10758     rtl = avoid_constant_pool_reference (rtl);
10759
10760   return rtl;
10761 }
10762
10763 /* We need to figure out what section we should use as the base for the
10764    address ranges where a given location is valid.
10765    1. If this particular DECL has a section associated with it, use that.
10766    2. If this function has a section associated with it, use that.
10767    3. Otherwise, use the text section.
10768    XXX: If you split a variable across multiple sections, we won't notice.  */
10769
10770 static const char *
10771 secname_for_decl (const_tree decl)
10772 {
10773   const char *secname;
10774
10775   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10776     {
10777       tree sectree = DECL_SECTION_NAME (decl);
10778       secname = TREE_STRING_POINTER (sectree);
10779     }
10780   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10781     {
10782       tree sectree = DECL_SECTION_NAME (current_function_decl);
10783       secname = TREE_STRING_POINTER (sectree);
10784     }
10785   else if (cfun && in_cold_section_p)
10786     secname = crtl->subsections.cold_section_label;
10787   else
10788     secname = text_section_label;
10789
10790   return secname;
10791 }
10792
10793 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_RTX is returned.
10794    If so, the rtx for the SYMBOL_REF for the COMMON block is returned, and the
10795    value is the offset into the common block for the symbol.  */
10796
10797 static tree
10798 fortran_common (tree decl, HOST_WIDE_INT *value)
10799 {
10800   tree val_expr, cvar;
10801   enum machine_mode mode;
10802   HOST_WIDE_INT bitsize, bitpos;
10803   tree offset;
10804   int volatilep = 0, unsignedp = 0;
10805
10806   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
10807      it does not have a value (the offset into the common area), or if it
10808      is thread local (as opposed to global) then it isn't common, and shouldn't
10809      be handled as such.  */
10810   if (TREE_CODE (decl) != VAR_DECL
10811       || !TREE_PUBLIC (decl)
10812       || !TREE_STATIC (decl)
10813       || !DECL_HAS_VALUE_EXPR_P (decl)
10814       || !is_fortran ())
10815     return NULL_TREE;
10816
10817   val_expr = DECL_VALUE_EXPR (decl);
10818   if (TREE_CODE (val_expr) != COMPONENT_REF)
10819     return NULL_TREE;
10820
10821   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
10822                               &mode, &unsignedp, &volatilep, true);
10823
10824   if (cvar == NULL_TREE
10825       || TREE_CODE (cvar) != VAR_DECL
10826       || DECL_ARTIFICIAL (cvar)
10827       || !TREE_PUBLIC (cvar))
10828     return NULL_TREE;
10829
10830   *value = 0;
10831   if (offset != NULL)
10832     {
10833       if (!host_integerp (offset, 0))
10834         return NULL_TREE;
10835       *value = tree_low_cst (offset, 0);
10836     }
10837   if (bitpos != 0)
10838     *value += bitpos / BITS_PER_UNIT;
10839
10840   return cvar;
10841 }
10842
10843
10844 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10845    data attribute for a variable or a parameter.  We generate the
10846    DW_AT_const_value attribute only in those cases where the given variable
10847    or parameter does not have a true "location" either in memory or in a
10848    register.  This can happen (for example) when a constant is passed as an
10849    actual argument in a call to an inline function.  (It's possible that
10850    these things can crop up in other ways also.)  Note that one type of
10851    constant value which can be passed into an inlined function is a constant
10852    pointer.  This can happen for example if an actual argument in an inlined
10853    function call evaluates to a compile-time constant address.  */
10854
10855 static void
10856 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10857                                        enum dwarf_attribute attr)
10858 {
10859   rtx rtl;
10860   dw_loc_descr_ref descr;
10861   var_loc_list *loc_list;
10862   struct var_loc_node *node;
10863   if (TREE_CODE (decl) == ERROR_MARK)
10864     return;
10865
10866   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10867               || TREE_CODE (decl) == RESULT_DECL);
10868
10869   /* See if we possibly have multiple locations for this variable.  */
10870   loc_list = lookup_decl_loc (decl);
10871
10872   /* If it truly has multiple locations, the first and last node will
10873      differ.  */
10874   if (loc_list && loc_list->first != loc_list->last)
10875     {
10876       const char *endname, *secname;
10877       dw_loc_list_ref list;
10878       rtx varloc;
10879       enum var_init_status initialized;
10880
10881       /* Now that we know what section we are using for a base,
10882          actually construct the list of locations.
10883          The first location information is what is passed to the
10884          function that creates the location list, and the remaining
10885          locations just get added on to that list.
10886          Note that we only know the start address for a location
10887          (IE location changes), so to build the range, we use
10888          the range [current location start, next location start].
10889          This means we have to special case the last node, and generate
10890          a range of [last location start, end of function label].  */
10891
10892       node = loc_list->first;
10893       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10894       secname = secname_for_decl (decl);
10895
10896       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10897         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10898       else
10899         initialized = VAR_INIT_STATUS_INITIALIZED;
10900
10901       list = new_loc_list (loc_descriptor (varloc, initialized),
10902                            node->label, node->next->label, secname, 1);
10903       node = node->next;
10904
10905       for (; node->next; node = node->next)
10906         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10907           {
10908             /* The variable has a location between NODE->LABEL and
10909                NODE->NEXT->LABEL.  */
10910             enum var_init_status initialized =
10911               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10912             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10913             add_loc_descr_to_loc_list (&list,
10914                                        loc_descriptor (varloc, initialized),
10915                                        node->label, node->next->label, secname);
10916           }
10917
10918       /* If the variable has a location at the last label
10919          it keeps its location until the end of function.  */
10920       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10921         {
10922           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10923           enum var_init_status initialized =
10924             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10925
10926           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10927           if (!current_function_decl)
10928             endname = text_end_label;
10929           else
10930             {
10931               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10932                                            current_function_funcdef_no);
10933               endname = ggc_strdup (label_id);
10934             }
10935           add_loc_descr_to_loc_list (&list,
10936                                      loc_descriptor (varloc, initialized),
10937                                      node->label, endname, secname);
10938         }
10939
10940       /* Finally, add the location list to the DIE, and we are done.  */
10941       add_AT_loc_list (die, attr, list);
10942       return;
10943     }
10944
10945   /* Try to get some constant RTL for this decl, and use that as the value of
10946      the location.  */
10947
10948   rtl = rtl_for_decl_location (decl);
10949   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10950     {
10951       add_const_value_attribute (die, rtl);
10952       return;
10953     }
10954
10955   /* If we have tried to generate the location otherwise, and it
10956      didn't work out (we wouldn't be here if we did), and we have a one entry
10957      location list, try generating a location from that.  */
10958   if (loc_list && loc_list->first)
10959     {
10960       enum var_init_status status;
10961       node = loc_list->first;
10962       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10963       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10964       if (descr)
10965         {
10966           add_AT_location_description (die, attr, descr);
10967           return;
10968         }
10969     }
10970
10971   /* We couldn't get any rtl, so try directly generating the location
10972      description from the tree.  */
10973   descr = loc_descriptor_from_tree (decl);
10974   if (descr)
10975     {
10976       add_AT_location_description (die, attr, descr);
10977       return;
10978     }
10979   /* None of that worked, so it must not really have a location;
10980      try adding a constant value attribute from the DECL_INITIAL.  */
10981   tree_add_const_value_attribute (die, decl);
10982 }
10983
10984 /* If we don't have a copy of this variable in memory for some reason (such
10985    as a C++ member constant that doesn't have an out-of-line definition),
10986    we should tell the debugger about the constant value.  */
10987
10988 static void
10989 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10990 {
10991   tree init = DECL_INITIAL (decl);
10992   tree type = TREE_TYPE (decl);
10993   rtx rtl;
10994
10995   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10996     /* OK */;
10997   else
10998     return;
10999
11000   rtl = rtl_for_decl_init (init, type);
11001   if (rtl)
11002     add_const_value_attribute (var_die, rtl);
11003 }
11004
11005 /* Convert the CFI instructions for the current function into a
11006    location list.  This is used for DW_AT_frame_base when we targeting
11007    a dwarf2 consumer that does not support the dwarf3
11008    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
11009    expressions.  */
11010
11011 static dw_loc_list_ref
11012 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
11013 {
11014   dw_fde_ref fde;
11015   dw_loc_list_ref list, *list_tail;
11016   dw_cfi_ref cfi;
11017   dw_cfa_location last_cfa, next_cfa;
11018   const char *start_label, *last_label, *section;
11019
11020   fde = current_fde ();
11021   gcc_assert (fde != NULL);
11022
11023   section = secname_for_decl (current_function_decl);
11024   list_tail = &list;
11025   list = NULL;
11026
11027   next_cfa.reg = INVALID_REGNUM;
11028   next_cfa.offset = 0;
11029   next_cfa.indirect = 0;
11030   next_cfa.base_offset = 0;
11031
11032   start_label = fde->dw_fde_begin;
11033
11034   /* ??? Bald assumption that the CIE opcode list does not contain
11035      advance opcodes.  */
11036   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
11037     lookup_cfa_1 (cfi, &next_cfa);
11038
11039   last_cfa = next_cfa;
11040   last_label = start_label;
11041
11042   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
11043     switch (cfi->dw_cfi_opc)
11044       {
11045       case DW_CFA_set_loc:
11046       case DW_CFA_advance_loc1:
11047       case DW_CFA_advance_loc2:
11048       case DW_CFA_advance_loc4:
11049         if (!cfa_equal_p (&last_cfa, &next_cfa))
11050           {
11051             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11052                                        start_label, last_label, section,
11053                                        list == NULL);
11054
11055             list_tail = &(*list_tail)->dw_loc_next;
11056             last_cfa = next_cfa;
11057             start_label = last_label;
11058           }
11059         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
11060         break;
11061
11062       case DW_CFA_advance_loc:
11063         /* The encoding is complex enough that we should never emit this.  */
11064       case DW_CFA_remember_state:
11065       case DW_CFA_restore_state:
11066         /* We don't handle these two in this function.  It would be possible
11067            if it were to be required.  */
11068         gcc_unreachable ();
11069
11070       default:
11071         lookup_cfa_1 (cfi, &next_cfa);
11072         break;
11073       }
11074
11075   if (!cfa_equal_p (&last_cfa, &next_cfa))
11076     {
11077       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
11078                                  start_label, last_label, section,
11079                                  list == NULL);
11080       list_tail = &(*list_tail)->dw_loc_next;
11081       start_label = last_label;
11082     }
11083   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
11084                              start_label, fde->dw_fde_end, section,
11085                              list == NULL);
11086
11087   return list;
11088 }
11089
11090 /* Compute a displacement from the "steady-state frame pointer" to the
11091    frame base (often the same as the CFA), and store it in
11092    frame_pointer_fb_offset.  OFFSET is added to the displacement
11093    before the latter is negated.  */
11094
11095 static void
11096 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
11097 {
11098   rtx reg, elim;
11099
11100 #ifdef FRAME_POINTER_CFA_OFFSET
11101   reg = frame_pointer_rtx;
11102   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
11103 #else
11104   reg = arg_pointer_rtx;
11105   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11106 #endif
11107
11108   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11109   if (GET_CODE (elim) == PLUS)
11110     {
11111       offset += INTVAL (XEXP (elim, 1));
11112       elim = XEXP (elim, 0);
11113     }
11114   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
11115                        : stack_pointer_rtx));
11116
11117   frame_pointer_fb_offset = -offset;
11118 }
11119
11120 /* Generate a DW_AT_name attribute given some string value to be included as
11121    the value of the attribute.  */
11122
11123 static void
11124 add_name_attribute (dw_die_ref die, const char *name_string)
11125 {
11126   if (name_string != NULL && *name_string != 0)
11127     {
11128       if (demangle_name_func)
11129         name_string = (*demangle_name_func) (name_string);
11130
11131       add_AT_string (die, DW_AT_name, name_string);
11132     }
11133 }
11134
11135 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11136
11137 static void
11138 add_comp_dir_attribute (dw_die_ref die)
11139 {
11140   const char *wd = get_src_pwd ();
11141   if (wd != NULL)
11142     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11143 }
11144
11145 /* Given a tree node describing an array bound (either lower or upper) output
11146    a representation for that bound.  */
11147
11148 static void
11149 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11150 {
11151   switch (TREE_CODE (bound))
11152     {
11153     case ERROR_MARK:
11154       return;
11155
11156     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11157     case INTEGER_CST:
11158       if (! host_integerp (bound, 0)
11159           || (bound_attr == DW_AT_lower_bound
11160               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11161                   || (is_fortran () && integer_onep (bound)))))
11162         /* Use the default.  */
11163         ;
11164       else
11165         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11166       break;
11167
11168     CASE_CONVERT:
11169     case VIEW_CONVERT_EXPR:
11170       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11171       break;
11172
11173     case SAVE_EXPR:
11174       break;
11175
11176     case VAR_DECL:
11177     case PARM_DECL:
11178     case RESULT_DECL:
11179       {
11180         dw_die_ref decl_die = lookup_decl_die (bound);
11181
11182         /* ??? Can this happen, or should the variable have been bound
11183            first?  Probably it can, since I imagine that we try to create
11184            the types of parameters in the order in which they exist in
11185            the list, and won't have created a forward reference to a
11186            later parameter.  */
11187         if (decl_die != NULL)
11188           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11189         break;
11190       }
11191
11192     default:
11193       {
11194         /* Otherwise try to create a stack operation procedure to
11195            evaluate the value of the array bound.  */
11196
11197         dw_die_ref ctx, decl_die;
11198         dw_loc_descr_ref loc;
11199
11200         loc = loc_descriptor_from_tree (bound);
11201         if (loc == NULL)
11202           break;
11203
11204         if (current_function_decl == 0)
11205           ctx = comp_unit_die;
11206         else
11207           ctx = lookup_decl_die (current_function_decl);
11208
11209         decl_die = new_die (DW_TAG_variable, ctx, bound);
11210         add_AT_flag (decl_die, DW_AT_artificial, 1);
11211         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11212         add_AT_loc (decl_die, DW_AT_location, loc);
11213
11214         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11215         break;
11216       }
11217     }
11218 }
11219
11220 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
11221    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
11222    Note that the block of subscript information for an array type also
11223    includes information about the element type of the given array type.  */
11224
11225 static void
11226 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
11227 {
11228   unsigned dimension_number;
11229   tree lower, upper;
11230   dw_die_ref subrange_die;
11231
11232   for (dimension_number = 0;
11233        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
11234        type = TREE_TYPE (type), dimension_number++)
11235     {
11236       tree domain = TYPE_DOMAIN (type);
11237
11238       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11239          and (in GNU C only) variable bounds.  Handle all three forms
11240          here.  */
11241       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11242       if (domain)
11243         {
11244           /* We have an array type with specified bounds.  */
11245           lower = TYPE_MIN_VALUE (domain);
11246           upper = TYPE_MAX_VALUE (domain);
11247
11248           /* Define the index type.  */
11249           if (TREE_TYPE (domain))
11250             {
11251               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11252                  TREE_TYPE field.  We can't emit debug info for this
11253                  because it is an unnamed integral type.  */
11254               if (TREE_CODE (domain) == INTEGER_TYPE
11255                   && TYPE_NAME (domain) == NULL_TREE
11256                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11257                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11258                 ;
11259               else
11260                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11261                                     type_die);
11262             }
11263
11264           /* ??? If upper is NULL, the array has unspecified length,
11265              but it does have a lower bound.  This happens with Fortran
11266                dimension arr(N:*)
11267              Since the debugger is definitely going to need to know N
11268              to produce useful results, go ahead and output the lower
11269              bound solo, and hope the debugger can cope.  */
11270
11271           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11272           if (upper)
11273             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11274         }
11275
11276       /* Otherwise we have an array type with an unspecified length.  The
11277          DWARF-2 spec does not say how to handle this; let's just leave out the
11278          bounds.  */
11279     }
11280 }
11281
11282 static void
11283 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11284 {
11285   unsigned size;
11286
11287   switch (TREE_CODE (tree_node))
11288     {
11289     case ERROR_MARK:
11290       size = 0;
11291       break;
11292     case ENUMERAL_TYPE:
11293     case RECORD_TYPE:
11294     case UNION_TYPE:
11295     case QUAL_UNION_TYPE:
11296       size = int_size_in_bytes (tree_node);
11297       break;
11298     case FIELD_DECL:
11299       /* For a data member of a struct or union, the DW_AT_byte_size is
11300          generally given as the number of bytes normally allocated for an
11301          object of the *declared* type of the member itself.  This is true
11302          even for bit-fields.  */
11303       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11304       break;
11305     default:
11306       gcc_unreachable ();
11307     }
11308
11309   /* Note that `size' might be -1 when we get to this point.  If it is, that
11310      indicates that the byte size of the entity in question is variable.  We
11311      have no good way of expressing this fact in Dwarf at the present time,
11312      so just let the -1 pass on through.  */
11313   add_AT_unsigned (die, DW_AT_byte_size, size);
11314 }
11315
11316 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11317    which specifies the distance in bits from the highest order bit of the
11318    "containing object" for the bit-field to the highest order bit of the
11319    bit-field itself.
11320
11321    For any given bit-field, the "containing object" is a hypothetical object
11322    (of some integral or enum type) within which the given bit-field lives.  The
11323    type of this hypothetical "containing object" is always the same as the
11324    declared type of the individual bit-field itself.  The determination of the
11325    exact location of the "containing object" for a bit-field is rather
11326    complicated.  It's handled by the `field_byte_offset' function (above).
11327
11328    Note that it is the size (in bytes) of the hypothetical "containing object"
11329    which will be given in the DW_AT_byte_size attribute for this bit-field.
11330    (See `byte_size_attribute' above).  */
11331
11332 static inline void
11333 add_bit_offset_attribute (dw_die_ref die, tree decl)
11334 {
11335   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11336   tree type = DECL_BIT_FIELD_TYPE (decl);
11337   HOST_WIDE_INT bitpos_int;
11338   HOST_WIDE_INT highest_order_object_bit_offset;
11339   HOST_WIDE_INT highest_order_field_bit_offset;
11340   HOST_WIDE_INT unsigned bit_offset;
11341
11342   /* Must be a field and a bit field.  */
11343   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11344
11345   /* We can't yet handle bit-fields whose offsets are variable, so if we
11346      encounter such things, just return without generating any attribute
11347      whatsoever.  Likewise for variable or too large size.  */
11348   if (! host_integerp (bit_position (decl), 0)
11349       || ! host_integerp (DECL_SIZE (decl), 1))
11350     return;
11351
11352   bitpos_int = int_bit_position (decl);
11353
11354   /* Note that the bit offset is always the distance (in bits) from the
11355      highest-order bit of the "containing object" to the highest-order bit of
11356      the bit-field itself.  Since the "high-order end" of any object or field
11357      is different on big-endian and little-endian machines, the computation
11358      below must take account of these differences.  */
11359   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11360   highest_order_field_bit_offset = bitpos_int;
11361
11362   if (! BYTES_BIG_ENDIAN)
11363     {
11364       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11365       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11366     }
11367
11368   bit_offset
11369     = (! BYTES_BIG_ENDIAN
11370        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11371        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11372
11373   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11374 }
11375
11376 /* For a FIELD_DECL node which represents a bit field, output an attribute
11377    which specifies the length in bits of the given field.  */
11378
11379 static inline void
11380 add_bit_size_attribute (dw_die_ref die, tree decl)
11381 {
11382   /* Must be a field and a bit field.  */
11383   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11384               && DECL_BIT_FIELD_TYPE (decl));
11385
11386   if (host_integerp (DECL_SIZE (decl), 1))
11387     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11388 }
11389
11390 /* If the compiled language is ANSI C, then add a 'prototyped'
11391    attribute, if arg types are given for the parameters of a function.  */
11392
11393 static inline void
11394 add_prototyped_attribute (dw_die_ref die, tree func_type)
11395 {
11396   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11397       && TYPE_ARG_TYPES (func_type) != NULL)
11398     add_AT_flag (die, DW_AT_prototyped, 1);
11399 }
11400
11401 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11402    by looking in either the type declaration or object declaration
11403    equate table.  */
11404
11405 static inline void
11406 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11407 {
11408   dw_die_ref origin_die = NULL;
11409
11410   if (TREE_CODE (origin) != FUNCTION_DECL)
11411     {
11412       /* We may have gotten separated from the block for the inlined
11413          function, if we're in an exception handler or some such; make
11414          sure that the abstract function has been written out.
11415
11416          Doing this for nested functions is wrong, however; functions are
11417          distinct units, and our context might not even be inline.  */
11418       tree fn = origin;
11419
11420       if (TYPE_P (fn))
11421         fn = TYPE_STUB_DECL (fn);
11422
11423       fn = decl_function_context (fn);
11424       if (fn)
11425         dwarf2out_abstract_function (fn);
11426     }
11427
11428   if (DECL_P (origin))
11429     origin_die = lookup_decl_die (origin);
11430   else if (TYPE_P (origin))
11431     origin_die = lookup_type_die (origin);
11432
11433   /* XXX: Functions that are never lowered don't always have correct block
11434      trees (in the case of java, they simply have no block tree, in some other
11435      languages).  For these functions, there is nothing we can really do to
11436      output correct debug info for inlined functions in all cases.  Rather
11437      than die, we'll just produce deficient debug info now, in that we will
11438      have variables without a proper abstract origin.  In the future, when all
11439      functions are lowered, we should re-add a gcc_assert (origin_die)
11440      here.  */
11441
11442   if (origin_die)
11443       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11444 }
11445
11446 /* We do not currently support the pure_virtual attribute.  */
11447
11448 static inline void
11449 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11450 {
11451   if (DECL_VINDEX (func_decl))
11452     {
11453       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11454
11455       if (host_integerp (DECL_VINDEX (func_decl), 0))
11456         add_AT_loc (die, DW_AT_vtable_elem_location,
11457                     new_loc_descr (DW_OP_constu,
11458                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11459                                    0));
11460
11461       /* GNU extension: Record what type this method came from originally.  */
11462       if (debug_info_level > DINFO_LEVEL_TERSE)
11463         add_AT_die_ref (die, DW_AT_containing_type,
11464                         lookup_type_die (DECL_CONTEXT (func_decl)));
11465     }
11466 }
11467 \f
11468 /* Add source coordinate attributes for the given decl.  */
11469
11470 static void
11471 add_src_coords_attributes (dw_die_ref die, tree decl)
11472 {
11473   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11474
11475   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11476   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11477 }
11478
11479 /* Add a DW_AT_name attribute and source coordinate attribute for the
11480    given decl, but only if it actually has a name.  */
11481
11482 static void
11483 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11484 {
11485   tree decl_name;
11486
11487   decl_name = DECL_NAME (decl);
11488   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11489     {
11490       add_name_attribute (die, dwarf2_name (decl, 0));
11491       if (! DECL_ARTIFICIAL (decl))
11492         add_src_coords_attributes (die, decl);
11493
11494       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11495           && TREE_PUBLIC (decl)
11496           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11497           && !DECL_ABSTRACT (decl)
11498           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11499           && !is_fortran ())
11500         add_AT_string (die, DW_AT_MIPS_linkage_name,
11501                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11502     }
11503
11504 #ifdef VMS_DEBUGGING_INFO
11505   /* Get the function's name, as described by its RTL.  This may be different
11506      from the DECL_NAME name used in the source file.  */
11507   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11508     {
11509       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11510                    XEXP (DECL_RTL (decl), 0));
11511       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11512     }
11513 #endif
11514 }
11515
11516 /* Push a new declaration scope.  */
11517
11518 static void
11519 push_decl_scope (tree scope)
11520 {
11521   VEC_safe_push (tree, gc, decl_scope_table, scope);
11522 }
11523
11524 /* Pop a declaration scope.  */
11525
11526 static inline void
11527 pop_decl_scope (void)
11528 {
11529   VEC_pop (tree, decl_scope_table);
11530 }
11531
11532 /* Return the DIE for the scope that immediately contains this type.
11533    Non-named types get global scope.  Named types nested in other
11534    types get their containing scope if it's open, or global scope
11535    otherwise.  All other types (i.e. function-local named types) get
11536    the current active scope.  */
11537
11538 static dw_die_ref
11539 scope_die_for (tree t, dw_die_ref context_die)
11540 {
11541   dw_die_ref scope_die = NULL;
11542   tree containing_scope;
11543   int i;
11544
11545   /* Non-types always go in the current scope.  */
11546   gcc_assert (TYPE_P (t));
11547
11548   containing_scope = TYPE_CONTEXT (t);
11549
11550   /* Use the containing namespace if it was passed in (for a declaration).  */
11551   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11552     {
11553       if (context_die == lookup_decl_die (containing_scope))
11554         /* OK */;
11555       else
11556         containing_scope = NULL_TREE;
11557     }
11558
11559   /* Ignore function type "scopes" from the C frontend.  They mean that
11560      a tagged type is local to a parmlist of a function declarator, but
11561      that isn't useful to DWARF.  */
11562   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11563     containing_scope = NULL_TREE;
11564
11565   if (containing_scope == NULL_TREE)
11566     scope_die = comp_unit_die;
11567   else if (TYPE_P (containing_scope))
11568     {
11569       /* For types, we can just look up the appropriate DIE.  But
11570          first we check to see if we're in the middle of emitting it
11571          so we know where the new DIE should go.  */
11572       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11573         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11574           break;
11575
11576       if (i < 0)
11577         {
11578           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11579                       || TREE_ASM_WRITTEN (containing_scope));
11580
11581           /* If none of the current dies are suitable, we get file scope.  */
11582           scope_die = comp_unit_die;
11583         }
11584       else
11585         scope_die = lookup_type_die (containing_scope);
11586     }
11587   else
11588     scope_die = context_die;
11589
11590   return scope_die;
11591 }
11592
11593 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11594
11595 static inline int
11596 local_scope_p (dw_die_ref context_die)
11597 {
11598   for (; context_die; context_die = context_die->die_parent)
11599     if (context_die->die_tag == DW_TAG_inlined_subroutine
11600         || context_die->die_tag == DW_TAG_subprogram)
11601       return 1;
11602
11603   return 0;
11604 }
11605
11606 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11607    whether or not to treat a DIE in this context as a declaration.  */
11608
11609 static inline int
11610 class_or_namespace_scope_p (dw_die_ref context_die)
11611 {
11612   return (context_die
11613           && (context_die->die_tag == DW_TAG_structure_type
11614               || context_die->die_tag == DW_TAG_class_type
11615               || context_die->die_tag == DW_TAG_interface_type
11616               || context_die->die_tag == DW_TAG_union_type
11617               || context_die->die_tag == DW_TAG_namespace));
11618 }
11619
11620 /* Many forms of DIEs require a "type description" attribute.  This
11621    routine locates the proper "type descriptor" die for the type given
11622    by 'type', and adds a DW_AT_type attribute below the given die.  */
11623
11624 static void
11625 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11626                     int decl_volatile, dw_die_ref context_die)
11627 {
11628   enum tree_code code  = TREE_CODE (type);
11629   dw_die_ref type_die  = NULL;
11630
11631   /* ??? If this type is an unnamed subrange type of an integral, floating-point
11632      or fixed-point type, use the inner type.  This is because we have no
11633      support for unnamed types in base_type_die.  This can happen if this is
11634      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11635   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11636       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11637     type = TREE_TYPE (type), code = TREE_CODE (type);
11638
11639   if (code == ERROR_MARK
11640       /* Handle a special case.  For functions whose return type is void, we
11641          generate *no* type attribute.  (Note that no object may have type
11642          `void', so this only applies to function return types).  */
11643       || code == VOID_TYPE)
11644     return;
11645
11646   type_die = modified_type_die (type,
11647                                 decl_const || TYPE_READONLY (type),
11648                                 decl_volatile || TYPE_VOLATILE (type),
11649                                 context_die);
11650
11651   if (type_die != NULL)
11652     add_AT_die_ref (object_die, DW_AT_type, type_die);
11653 }
11654
11655 /* Given an object die, add the calling convention attribute for the
11656    function call type.  */
11657 static void
11658 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11659 {
11660   enum dwarf_calling_convention value = DW_CC_normal;
11661
11662   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11663
11664   /* DWARF doesn't provide a way to identify a program's source-level
11665      entry point.  DW_AT_calling_convention attributes are only meant
11666      to describe functions' calling conventions.  However, lacking a
11667      better way to signal the Fortran main program, we use this for the
11668      time being, following existing custom.  */
11669   if (is_fortran ()
11670       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11671     value = DW_CC_program;
11672
11673   /* Only add the attribute if the backend requests it, and
11674      is not DW_CC_normal.  */
11675   if (value && (value != DW_CC_normal))
11676     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11677 }
11678
11679 /* Given a tree pointer to a struct, class, union, or enum type node, return
11680    a pointer to the (string) tag name for the given type, or zero if the type
11681    was declared without a tag.  */
11682
11683 static const char *
11684 type_tag (const_tree type)
11685 {
11686   const char *name = 0;
11687
11688   if (TYPE_NAME (type) != 0)
11689     {
11690       tree t = 0;
11691
11692       /* Find the IDENTIFIER_NODE for the type name.  */
11693       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11694         t = TYPE_NAME (type);
11695
11696       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11697          a TYPE_DECL node, regardless of whether or not a `typedef' was
11698          involved.  */
11699       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11700                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11701         {
11702           /* We want to be extra verbose.  Don't call dwarf_name if
11703              DECL_NAME isn't set.  The default hook for decl_printable_name
11704              doesn't like that, and in this context it's correct to return
11705              0, instead of "<anonymous>" or the like.  */
11706           if (DECL_NAME (TYPE_NAME (type)))
11707             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11708         }
11709
11710       /* Now get the name as a string, or invent one.  */
11711       if (!name && t != 0)
11712         name = IDENTIFIER_POINTER (t);
11713     }
11714
11715   return (name == 0 || *name == '\0') ? 0 : name;
11716 }
11717
11718 /* Return the type associated with a data member, make a special check
11719    for bit field types.  */
11720
11721 static inline tree
11722 member_declared_type (const_tree member)
11723 {
11724   return (DECL_BIT_FIELD_TYPE (member)
11725           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11726 }
11727
11728 /* Get the decl's label, as described by its RTL. This may be different
11729    from the DECL_NAME name used in the source file.  */
11730
11731 #if 0
11732 static const char *
11733 decl_start_label (tree decl)
11734 {
11735   rtx x;
11736   const char *fnname;
11737
11738   x = DECL_RTL (decl);
11739   gcc_assert (MEM_P (x));
11740
11741   x = XEXP (x, 0);
11742   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11743
11744   fnname = XSTR (x, 0);
11745   return fnname;
11746 }
11747 #endif
11748 \f
11749 /* These routines generate the internal representation of the DIE's for
11750    the compilation unit.  Debugging information is collected by walking
11751    the declaration trees passed in from dwarf2out_decl().  */
11752
11753 static void
11754 gen_array_type_die (tree type, dw_die_ref context_die)
11755 {
11756   dw_die_ref scope_die = scope_die_for (type, context_die);
11757   dw_die_ref array_die;
11758
11759   /* GNU compilers represent multidimensional array types as sequences of one
11760      dimensional array types whose element types are themselves array types.
11761      We sometimes squish that down to a single array_type DIE with multiple
11762      subscripts in the Dwarf debugging info.  The draft Dwarf specification
11763      say that we are allowed to do this kind of compression in C, because
11764      there is no difference between an array of arrays and a multidimensional
11765      array.  We don't do this for Ada to remain as close as possible to the
11766      actual representation, which is especially important against the language
11767      flexibilty wrt arrays of variable size.  */
11768
11769   bool collapse_nested_arrays = !is_ada ();
11770   tree element_type;
11771   
11772   /* ??? The SGI dwarf reader fails for array of array of enum types
11773      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
11774      array type comes before the outer array type.  We thus call gen_type_die
11775      before we new_die and must prevent nested array types collapsing for this
11776      target.  */
11777
11778 #ifdef MIPS_DEBUGGING_INFO
11779   gen_type_die (TREE_TYPE (type), context_die);
11780   collapse_nested_arrays = false;
11781 #endif
11782
11783   array_die = new_die (DW_TAG_array_type, scope_die, type);
11784   add_name_attribute (array_die, type_tag (type));
11785   equate_type_number_to_die (type, array_die);
11786
11787   if (TREE_CODE (type) == VECTOR_TYPE)
11788     {
11789       /* The frontend feeds us a representation for the vector as a struct
11790          containing an array.  Pull out the array type.  */
11791       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11792       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11793     }
11794
11795   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11796   if (is_fortran ()
11797       && TREE_CODE (type) == ARRAY_TYPE
11798       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11799     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11800
11801 #if 0
11802   /* We default the array ordering.  SDB will probably do
11803      the right things even if DW_AT_ordering is not present.  It's not even
11804      an issue until we start to get into multidimensional arrays anyway.  If
11805      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11806      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11807      and when we find out that we need to put these in, we will only do so
11808      for multidimensional arrays.  */
11809   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11810 #endif
11811
11812 #ifdef MIPS_DEBUGGING_INFO
11813   /* The SGI compilers handle arrays of unknown bound by setting
11814      AT_declaration and not emitting any subrange DIEs.  */
11815   if (! TYPE_DOMAIN (type))
11816     add_AT_flag (array_die, DW_AT_declaration, 1);
11817   else
11818 #endif
11819     add_subscript_info (array_die, type, collapse_nested_arrays);
11820
11821   /* Add representation of the type of the elements of this array type and
11822      emit the corresponding DIE if we haven't done it already.  */  
11823   element_type = TREE_TYPE (type);
11824   if (collapse_nested_arrays)
11825     while (TREE_CODE (element_type) == ARRAY_TYPE)
11826       element_type = TREE_TYPE (element_type);
11827   
11828 #ifndef MIPS_DEBUGGING_INFO
11829   gen_type_die (element_type, context_die);
11830 #endif
11831
11832   add_type_attribute (array_die, element_type, 0, 0, context_die);
11833
11834   if (get_AT (array_die, DW_AT_name))
11835     add_pubtype (type, array_die);
11836 }
11837
11838 static dw_loc_descr_ref
11839 descr_info_loc (tree val, tree base_decl)
11840 {
11841   HOST_WIDE_INT size;
11842   dw_loc_descr_ref loc, loc2;
11843   enum dwarf_location_atom op;
11844
11845   if (val == base_decl)
11846     return new_loc_descr (DW_OP_push_object_address, 0, 0);
11847
11848   switch (TREE_CODE (val))
11849     {
11850     CASE_CONVERT:
11851       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11852     case INTEGER_CST:
11853       if (host_integerp (val, 0))
11854         return int_loc_descriptor (tree_low_cst (val, 0));
11855       break;
11856     case INDIRECT_REF:
11857       size = int_size_in_bytes (TREE_TYPE (val));
11858       if (size < 0)
11859         break;
11860       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11861       if (!loc)
11862         break;
11863       if (size == DWARF2_ADDR_SIZE)
11864         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11865       else
11866         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11867       return loc;
11868     case POINTER_PLUS_EXPR:
11869     case PLUS_EXPR:
11870       if (host_integerp (TREE_OPERAND (val, 1), 1)
11871           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11872              < 16384)
11873         {
11874           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11875           if (!loc)
11876             break;
11877           add_loc_descr (&loc,
11878                          new_loc_descr (DW_OP_plus_uconst,
11879                                         tree_low_cst (TREE_OPERAND (val, 1),
11880                                                       1), 0));
11881         }
11882       else
11883         {
11884           op = DW_OP_plus;
11885         do_binop:
11886           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11887           if (!loc)
11888             break;
11889           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11890           if (!loc2)
11891             break;
11892           add_loc_descr (&loc, loc2);
11893           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11894         }
11895       return loc;
11896     case MINUS_EXPR:
11897       op = DW_OP_minus;
11898       goto do_binop;
11899     case MULT_EXPR:
11900       op = DW_OP_mul;
11901       goto do_binop;
11902     case EQ_EXPR:
11903       op = DW_OP_eq;
11904       goto do_binop;
11905     case NE_EXPR:
11906       op = DW_OP_ne;
11907       goto do_binop;
11908     default:
11909       break;
11910     }
11911   return NULL;
11912 }
11913
11914 static void
11915 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11916                       tree val, tree base_decl)
11917 {
11918   dw_loc_descr_ref loc;
11919
11920   if (host_integerp (val, 0))
11921     {
11922       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11923       return;
11924     }
11925
11926   loc = descr_info_loc (val, base_decl);
11927   if (!loc)
11928     return;
11929
11930   add_AT_loc (die, attr, loc);
11931 }
11932
11933 /* This routine generates DIE for array with hidden descriptor, details
11934    are filled into *info by a langhook.  */
11935
11936 static void
11937 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11938                           dw_die_ref context_die)
11939 {
11940   dw_die_ref scope_die = scope_die_for (type, context_die);
11941   dw_die_ref array_die;
11942   int dim;
11943
11944   array_die = new_die (DW_TAG_array_type, scope_die, type);
11945   add_name_attribute (array_die, type_tag (type));
11946   equate_type_number_to_die (type, array_die);
11947
11948   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11949   if (is_fortran ()
11950       && info->ndimensions >= 2)
11951     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11952
11953   if (info->data_location)
11954     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11955                           info->base_decl);
11956   if (info->associated)
11957     add_descr_info_field (array_die, DW_AT_associated, info->associated,
11958                           info->base_decl);
11959   if (info->allocated)
11960     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11961                           info->base_decl);
11962
11963   for (dim = 0; dim < info->ndimensions; dim++)
11964     {
11965       dw_die_ref subrange_die
11966         = new_die (DW_TAG_subrange_type, array_die, NULL);
11967
11968       if (info->dimen[dim].lower_bound)
11969         {
11970           /* If it is the default value, omit it.  */
11971           if ((is_c_family () || is_java ())
11972               && integer_zerop (info->dimen[dim].lower_bound))
11973             ;
11974           else if (is_fortran ()
11975                    && integer_onep (info->dimen[dim].lower_bound))
11976             ;
11977           else
11978             add_descr_info_field (subrange_die, DW_AT_lower_bound,
11979                                   info->dimen[dim].lower_bound,
11980                                   info->base_decl);
11981         }
11982       if (info->dimen[dim].upper_bound)
11983         add_descr_info_field (subrange_die, DW_AT_upper_bound,
11984                               info->dimen[dim].upper_bound,
11985                               info->base_decl);
11986       if (info->dimen[dim].stride)
11987         add_descr_info_field (subrange_die, DW_AT_byte_stride,
11988                               info->dimen[dim].stride,
11989                               info->base_decl);
11990     }
11991
11992   gen_type_die (info->element_type, context_die);
11993   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11994
11995   if (get_AT (array_die, DW_AT_name))
11996     add_pubtype (type, array_die);
11997 }
11998
11999 #if 0
12000 static void
12001 gen_entry_point_die (tree decl, dw_die_ref context_die)
12002 {
12003   tree origin = decl_ultimate_origin (decl);
12004   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
12005
12006   if (origin != NULL)
12007     add_abstract_origin_attribute (decl_die, origin);
12008   else
12009     {
12010       add_name_and_src_coords_attributes (decl_die, decl);
12011       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
12012                           0, 0, context_die);
12013     }
12014
12015   if (DECL_ABSTRACT (decl))
12016     equate_decl_number_to_die (decl, decl_die);
12017   else
12018     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
12019 }
12020 #endif
12021
12022 /* Walk through the list of incomplete types again, trying once more to
12023    emit full debugging info for them.  */
12024
12025 static void
12026 retry_incomplete_types (void)
12027 {
12028   int i;
12029
12030   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
12031     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
12032 }
12033
12034 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
12035
12036 static void
12037 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
12038 {
12039   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
12040
12041   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12042      be incomplete and such types are not marked.  */
12043   add_abstract_origin_attribute (type_die, type);
12044 }
12045
12046 /* Determine what tag to use for a record type.  */
12047
12048 static enum dwarf_tag
12049 record_type_tag (tree type)
12050 {
12051   if (! lang_hooks.types.classify_record)
12052     return DW_TAG_structure_type;
12053
12054   switch (lang_hooks.types.classify_record (type))
12055     {
12056     case RECORD_IS_STRUCT:
12057       return DW_TAG_structure_type;
12058
12059     case RECORD_IS_CLASS:
12060       return DW_TAG_class_type;
12061
12062     case RECORD_IS_INTERFACE:
12063       return DW_TAG_interface_type;
12064
12065     default:
12066       gcc_unreachable ();
12067     }
12068 }
12069
12070 /* Generate a DIE to represent an inlined instance of a structure type.  */
12071
12072 static void
12073 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
12074 {
12075   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
12076
12077   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12078      be incomplete and such types are not marked.  */
12079   add_abstract_origin_attribute (type_die, type);
12080 }
12081
12082 /* Generate a DIE to represent an inlined instance of a union type.  */
12083
12084 static void
12085 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
12086 {
12087   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
12088
12089   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
12090      be incomplete and such types are not marked.  */
12091   add_abstract_origin_attribute (type_die, type);
12092 }
12093
12094 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
12095    include all of the information about the enumeration values also. Each
12096    enumerated type name/value is listed as a child of the enumerated type
12097    DIE.  */
12098
12099 static dw_die_ref
12100 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12101 {
12102   dw_die_ref type_die = lookup_type_die (type);
12103
12104   if (type_die == NULL)
12105     {
12106       type_die = new_die (DW_TAG_enumeration_type,
12107                           scope_die_for (type, context_die), type);
12108       equate_type_number_to_die (type, type_die);
12109       add_name_attribute (type_die, type_tag (type));
12110     }
12111   else if (! TYPE_SIZE (type))
12112     return type_die;
12113   else
12114     remove_AT (type_die, DW_AT_declaration);
12115
12116   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12117      given enum type is incomplete, do not generate the DW_AT_byte_size
12118      attribute or the DW_AT_element_list attribute.  */
12119   if (TYPE_SIZE (type))
12120     {
12121       tree link;
12122
12123       TREE_ASM_WRITTEN (type) = 1;
12124       add_byte_size_attribute (type_die, type);
12125       if (TYPE_STUB_DECL (type) != NULL_TREE)
12126         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12127
12128       /* If the first reference to this type was as the return type of an
12129          inline function, then it may not have a parent.  Fix this now.  */
12130       if (type_die->die_parent == NULL)
12131         add_child_die (scope_die_for (type, context_die), type_die);
12132
12133       for (link = TYPE_VALUES (type);
12134            link != NULL; link = TREE_CHAIN (link))
12135         {
12136           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12137           tree value = TREE_VALUE (link);
12138
12139           add_name_attribute (enum_die,
12140                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12141
12142           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12143             /* DWARF2 does not provide a way of indicating whether or
12144                not enumeration constants are signed or unsigned.  GDB
12145                always assumes the values are signed, so we output all
12146                values as if they were signed.  That means that
12147                enumeration constants with very large unsigned values
12148                will appear to have negative values in the debugger.  */
12149             add_AT_int (enum_die, DW_AT_const_value,
12150                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12151         }
12152     }
12153   else
12154     add_AT_flag (type_die, DW_AT_declaration, 1);
12155
12156   if (get_AT (type_die, DW_AT_name))
12157     add_pubtype (type, type_die);
12158
12159   return type_die;
12160 }
12161
12162 /* Generate a DIE to represent either a real live formal parameter decl or to
12163    represent just the type of some formal parameter position in some function
12164    type.
12165
12166    Note that this routine is a bit unusual because its argument may be a
12167    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12168    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12169    node.  If it's the former then this function is being called to output a
12170    DIE to represent a formal parameter object (or some inlining thereof).  If
12171    it's the latter, then this function is only being called to output a
12172    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12173    argument type of some subprogram type.  */
12174
12175 static dw_die_ref
12176 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12177 {
12178   dw_die_ref parm_die
12179     = new_die (DW_TAG_formal_parameter, context_die, node);
12180   tree origin;
12181
12182   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12183     {
12184     case tcc_declaration:
12185       origin = decl_ultimate_origin (node);
12186       if (origin != NULL)
12187         add_abstract_origin_attribute (parm_die, origin);
12188       else
12189         {
12190           tree type = TREE_TYPE (node);
12191           add_name_and_src_coords_attributes (parm_die, node);
12192           if (DECL_BY_REFERENCE (node))
12193             type = TREE_TYPE (type);
12194           add_type_attribute (parm_die, type,
12195                               TREE_READONLY (node),
12196                               TREE_THIS_VOLATILE (node),
12197                               context_die);
12198           if (DECL_ARTIFICIAL (node))
12199             add_AT_flag (parm_die, DW_AT_artificial, 1);
12200         }
12201
12202       equate_decl_number_to_die (node, parm_die);
12203       if (! DECL_ABSTRACT (node))
12204         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12205
12206       break;
12207
12208     case tcc_type:
12209       /* We were called with some kind of a ..._TYPE node.  */
12210       add_type_attribute (parm_die, node, 0, 0, context_die);
12211       break;
12212
12213     default:
12214       gcc_unreachable ();
12215     }
12216
12217   return parm_die;
12218 }
12219
12220 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12221    at the end of an (ANSI prototyped) formal parameters list.  */
12222
12223 static void
12224 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12225 {
12226   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12227 }
12228
12229 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12230    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12231    parameters as specified in some function type specification (except for
12232    those which appear as part of a function *definition*).  */
12233
12234 static void
12235 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12236 {
12237   tree link;
12238   tree formal_type = NULL;
12239   tree first_parm_type;
12240   tree arg;
12241
12242   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12243     {
12244       arg = DECL_ARGUMENTS (function_or_method_type);
12245       function_or_method_type = TREE_TYPE (function_or_method_type);
12246     }
12247   else
12248     arg = NULL_TREE;
12249
12250   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12251
12252   /* Make our first pass over the list of formal parameter types and output a
12253      DW_TAG_formal_parameter DIE for each one.  */
12254   for (link = first_parm_type; link; )
12255     {
12256       dw_die_ref parm_die;
12257
12258       formal_type = TREE_VALUE (link);
12259       if (formal_type == void_type_node)
12260         break;
12261
12262       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12263       parm_die = gen_formal_parameter_die (formal_type, context_die);
12264       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12265            && link == first_parm_type)
12266           || (arg && DECL_ARTIFICIAL (arg)))
12267         add_AT_flag (parm_die, DW_AT_artificial, 1);
12268
12269       link = TREE_CHAIN (link);
12270       if (arg)
12271         arg = TREE_CHAIN (arg);
12272     }
12273
12274   /* If this function type has an ellipsis, add a
12275      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12276   if (formal_type != void_type_node)
12277     gen_unspecified_parameters_die (function_or_method_type, context_die);
12278
12279   /* Make our second (and final) pass over the list of formal parameter types
12280      and output DIEs to represent those types (as necessary).  */
12281   for (link = TYPE_ARG_TYPES (function_or_method_type);
12282        link && TREE_VALUE (link);
12283        link = TREE_CHAIN (link))
12284     gen_type_die (TREE_VALUE (link), context_die);
12285 }
12286
12287 /* We want to generate the DIE for TYPE so that we can generate the
12288    die for MEMBER, which has been defined; we will need to refer back
12289    to the member declaration nested within TYPE.  If we're trying to
12290    generate minimal debug info for TYPE, processing TYPE won't do the
12291    trick; we need to attach the member declaration by hand.  */
12292
12293 static void
12294 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12295 {
12296   gen_type_die (type, context_die);
12297
12298   /* If we're trying to avoid duplicate debug info, we may not have
12299      emitted the member decl for this function.  Emit it now.  */
12300   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12301       && ! lookup_decl_die (member))
12302     {
12303       dw_die_ref type_die;
12304       gcc_assert (!decl_ultimate_origin (member));
12305
12306       push_decl_scope (type);
12307       type_die = lookup_type_die (type);
12308       if (TREE_CODE (member) == FUNCTION_DECL)
12309         gen_subprogram_die (member, type_die);
12310       else if (TREE_CODE (member) == FIELD_DECL)
12311         {
12312           /* Ignore the nameless fields that are used to skip bits but handle
12313              C++ anonymous unions and structs.  */
12314           if (DECL_NAME (member) != NULL_TREE
12315               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12316               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12317             {
12318               gen_type_die (member_declared_type (member), type_die);
12319               gen_field_die (member, type_die);
12320             }
12321         }
12322       else
12323         gen_variable_die (member, type_die);
12324
12325       pop_decl_scope ();
12326     }
12327 }
12328
12329 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12330    may later generate inlined and/or out-of-line instances of.  */
12331
12332 static void
12333 dwarf2out_abstract_function (tree decl)
12334 {
12335   dw_die_ref old_die;
12336   tree save_fn;
12337   tree context;
12338   int was_abstract = DECL_ABSTRACT (decl);
12339
12340   /* Make sure we have the actual abstract inline, not a clone.  */
12341   decl = DECL_ORIGIN (decl);
12342
12343   old_die = lookup_decl_die (decl);
12344   if (old_die && get_AT (old_die, DW_AT_inline))
12345     /* We've already generated the abstract instance.  */
12346     return;
12347
12348   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12349      we don't get confused by DECL_ABSTRACT.  */
12350   if (debug_info_level > DINFO_LEVEL_TERSE)
12351     {
12352       context = decl_class_context (decl);
12353       if (context)
12354         gen_type_die_for_member
12355           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12356     }
12357
12358   /* Pretend we've just finished compiling this function.  */
12359   save_fn = current_function_decl;
12360   current_function_decl = decl;
12361   push_cfun (DECL_STRUCT_FUNCTION (decl));
12362
12363   set_decl_abstract_flags (decl, 1);
12364   dwarf2out_decl (decl);
12365   if (! was_abstract)
12366     set_decl_abstract_flags (decl, 0);
12367
12368   current_function_decl = save_fn;
12369   pop_cfun ();
12370 }
12371
12372 /* Helper function of premark_used_types() which gets called through
12373    htab_traverse_resize().
12374
12375    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12376    marked as unused by prune_unused_types.  */
12377 static int
12378 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12379 {
12380   tree type;
12381   dw_die_ref die;
12382
12383   type = (tree) *slot;
12384   die = lookup_type_die (type);
12385   if (die != NULL)
12386     die->die_perennial_p = 1;
12387   return 1;
12388 }
12389
12390 /* Mark all members of used_types_hash as perennial.  */
12391 static void
12392 premark_used_types (void)
12393 {
12394   if (cfun && cfun->used_types_hash)
12395     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12396 }
12397
12398 /* Generate a DIE to represent a declared function (either file-scope or
12399    block-local).  */
12400
12401 static void
12402 gen_subprogram_die (tree decl, dw_die_ref context_die)
12403 {
12404   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12405   tree origin = decl_ultimate_origin (decl);
12406   dw_die_ref subr_die;
12407   tree fn_arg_types;
12408   tree outer_scope;
12409   dw_die_ref old_die = lookup_decl_die (decl);
12410   int declaration = (current_function_decl != decl
12411                      || class_or_namespace_scope_p (context_die));
12412
12413   premark_used_types ();
12414
12415   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12416      started to generate the abstract instance of an inline, decided to output
12417      its containing class, and proceeded to emit the declaration of the inline
12418      from the member list for the class.  If so, DECLARATION takes priority;
12419      we'll get back to the abstract instance when done with the class.  */
12420
12421   /* The class-scope declaration DIE must be the primary DIE.  */
12422   if (origin && declaration && class_or_namespace_scope_p (context_die))
12423     {
12424       origin = NULL;
12425       gcc_assert (!old_die);
12426     }
12427
12428   /* Now that the C++ front end lazily declares artificial member fns, we
12429      might need to retrofit the declaration into its class.  */
12430   if (!declaration && !origin && !old_die
12431       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12432       && !class_or_namespace_scope_p (context_die)
12433       && debug_info_level > DINFO_LEVEL_TERSE)
12434     old_die = force_decl_die (decl);
12435
12436   if (origin != NULL)
12437     {
12438       gcc_assert (!declaration || local_scope_p (context_die));
12439
12440       /* Fixup die_parent for the abstract instance of a nested
12441          inline function.  */
12442       if (old_die && old_die->die_parent == NULL)
12443         add_child_die (context_die, old_die);
12444
12445       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12446       add_abstract_origin_attribute (subr_die, origin);
12447     }
12448   else if (old_die)
12449     {
12450       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12451       struct dwarf_file_data * file_index = lookup_filename (s.file);
12452
12453       if (!get_AT_flag (old_die, DW_AT_declaration)
12454           /* We can have a normal definition following an inline one in the
12455              case of redefinition of GNU C extern inlines.
12456              It seems reasonable to use AT_specification in this case.  */
12457           && !get_AT (old_die, DW_AT_inline))
12458         {
12459           /* Detect and ignore this case, where we are trying to output
12460              something we have already output.  */
12461           return;
12462         }
12463
12464       /* If the definition comes from the same place as the declaration,
12465          maybe use the old DIE.  We always want the DIE for this function
12466          that has the *_pc attributes to be under comp_unit_die so the
12467          debugger can find it.  We also need to do this for abstract
12468          instances of inlines, since the spec requires the out-of-line copy
12469          to have the same parent.  For local class methods, this doesn't
12470          apply; we just use the old DIE.  */
12471       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12472           && (DECL_ARTIFICIAL (decl)
12473               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12474                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12475                       == (unsigned) s.line))))
12476         {
12477           subr_die = old_die;
12478
12479           /* Clear out the declaration attribute and the formal parameters.
12480              Do not remove all children, because it is possible that this
12481              declaration die was forced using force_decl_die(). In such
12482              cases die that forced declaration die (e.g. TAG_imported_module)
12483              is one of the children that we do not want to remove.  */
12484           remove_AT (subr_die, DW_AT_declaration);
12485           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12486         }
12487       else
12488         {
12489           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12490           add_AT_specification (subr_die, old_die);
12491           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12492             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12493           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12494             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12495         }
12496     }
12497   else
12498     {
12499       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12500
12501       if (TREE_PUBLIC (decl))
12502         add_AT_flag (subr_die, DW_AT_external, 1);
12503
12504       add_name_and_src_coords_attributes (subr_die, decl);
12505       if (debug_info_level > DINFO_LEVEL_TERSE)
12506         {
12507           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12508           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12509                               0, 0, context_die);
12510         }
12511
12512       add_pure_or_virtual_attribute (subr_die, decl);
12513       if (DECL_ARTIFICIAL (decl))
12514         add_AT_flag (subr_die, DW_AT_artificial, 1);
12515
12516       if (TREE_PROTECTED (decl))
12517         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12518       else if (TREE_PRIVATE (decl))
12519         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12520     }
12521
12522   if (declaration)
12523     {
12524       if (!old_die || !get_AT (old_die, DW_AT_inline))
12525         {
12526           add_AT_flag (subr_die, DW_AT_declaration, 1);
12527
12528           /* The first time we see a member function, it is in the context of
12529              the class to which it belongs.  We make sure of this by emitting
12530              the class first.  The next time is the definition, which is
12531              handled above.  The two may come from the same source text.
12532
12533              Note that force_decl_die() forces function declaration die. It is
12534              later reused to represent definition.  */
12535           equate_decl_number_to_die (decl, subr_die);
12536         }
12537     }
12538   else if (DECL_ABSTRACT (decl))
12539     {
12540       if (DECL_DECLARED_INLINE_P (decl))
12541         {
12542           if (cgraph_function_possibly_inlined_p (decl))
12543             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12544           else
12545             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12546         }
12547       else
12548         {
12549           if (cgraph_function_possibly_inlined_p (decl))
12550             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12551           else
12552             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12553         }
12554
12555       if (DECL_DECLARED_INLINE_P (decl)
12556           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12557         add_AT_flag (subr_die, DW_AT_artificial, 1);
12558
12559       equate_decl_number_to_die (decl, subr_die);
12560     }
12561   else if (!DECL_EXTERNAL (decl))
12562     {
12563       HOST_WIDE_INT cfa_fb_offset;
12564
12565       if (!old_die || !get_AT (old_die, DW_AT_inline))
12566         equate_decl_number_to_die (decl, subr_die);
12567
12568       if (!flag_reorder_blocks_and_partition)
12569         {
12570           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12571                                        current_function_funcdef_no);
12572           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12573           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12574                                        current_function_funcdef_no);
12575           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12576
12577           add_pubname (decl, subr_die);
12578           add_arange (decl, subr_die);
12579         }
12580       else
12581         {  /* Do nothing for now; maybe need to duplicate die, one for
12582               hot section and one for cold section, then use the hot/cold
12583               section begin/end labels to generate the aranges...  */
12584           /*
12585             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12586             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12587             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12588             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12589
12590             add_pubname (decl, subr_die);
12591             add_arange (decl, subr_die);
12592             add_arange (decl, subr_die);
12593            */
12594         }
12595
12596 #ifdef MIPS_DEBUGGING_INFO
12597       /* Add a reference to the FDE for this routine.  */
12598       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12599 #endif
12600
12601       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12602
12603       /* We define the "frame base" as the function's CFA.  This is more
12604          convenient for several reasons: (1) It's stable across the prologue
12605          and epilogue, which makes it better than just a frame pointer,
12606          (2) With dwarf3, there exists a one-byte encoding that allows us
12607          to reference the .debug_frame data by proxy, but failing that,
12608          (3) We can at least reuse the code inspection and interpretation
12609          code that determines the CFA position at various points in the
12610          function.  */
12611       /* ??? Use some command-line or configury switch to enable the use
12612          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
12613          consumers that understand it; fall back to "pure" dwarf2 and
12614          convert the CFA data into a location list.  */
12615       {
12616         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12617         if (list->dw_loc_next)
12618           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12619         else
12620           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12621       }
12622
12623       /* Compute a displacement from the "steady-state frame pointer" to
12624          the CFA.  The former is what all stack slots and argument slots
12625          will reference in the rtl; the later is what we've told the
12626          debugger about.  We'll need to adjust all frame_base references
12627          by this displacement.  */
12628       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12629
12630       if (cfun->static_chain_decl)
12631         add_AT_location_description (subr_die, DW_AT_static_link,
12632                  loc_descriptor_from_tree (cfun->static_chain_decl));
12633     }
12634
12635   /* Now output descriptions of the arguments for this function. This gets
12636      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12637      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12638      `...' at the end of the formal parameter list.  In order to find out if
12639      there was a trailing ellipsis or not, we must instead look at the type
12640      associated with the FUNCTION_DECL.  This will be a node of type
12641      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12642      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12643      an ellipsis at the end.  */
12644
12645   /* In the case where we are describing a mere function declaration, all we
12646      need to do here (and all we *can* do here) is to describe the *types* of
12647      its formal parameters.  */
12648   if (debug_info_level <= DINFO_LEVEL_TERSE)
12649     ;
12650   else if (declaration)
12651     gen_formal_types_die (decl, subr_die);
12652   else
12653     {
12654       /* Generate DIEs to represent all known formal parameters.  */
12655       tree arg_decls = DECL_ARGUMENTS (decl);
12656       tree parm;
12657
12658       /* When generating DIEs, generate the unspecified_parameters DIE
12659          instead if we come across the arg "__builtin_va_alist" */
12660       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12661         if (TREE_CODE (parm) == PARM_DECL)
12662           {
12663             if (DECL_NAME (parm)
12664                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12665                             "__builtin_va_alist"))
12666               gen_unspecified_parameters_die (parm, subr_die);
12667             else
12668               gen_decl_die (parm, subr_die);
12669           }
12670
12671       /* Decide whether we need an unspecified_parameters DIE at the end.
12672          There are 2 more cases to do this for: 1) the ansi ... declaration -
12673          this is detectable when the end of the arg list is not a
12674          void_type_node 2) an unprototyped function declaration (not a
12675          definition).  This just means that we have no info about the
12676          parameters at all.  */
12677       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12678       if (fn_arg_types != NULL)
12679         {
12680           /* This is the prototyped case, check for....  */
12681           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12682             gen_unspecified_parameters_die (decl, subr_die);
12683         }
12684       else if (DECL_INITIAL (decl) == NULL_TREE)
12685         gen_unspecified_parameters_die (decl, subr_die);
12686     }
12687
12688   /* Output Dwarf info for all of the stuff within the body of the function
12689      (if it has one - it may be just a declaration).  */
12690   outer_scope = DECL_INITIAL (decl);
12691
12692   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12693      a function.  This BLOCK actually represents the outermost binding contour
12694      for the function, i.e. the contour in which the function's formal
12695      parameters and labels get declared. Curiously, it appears that the front
12696      end doesn't actually put the PARM_DECL nodes for the current function onto
12697      the BLOCK_VARS list for this outer scope, but are strung off of the
12698      DECL_ARGUMENTS list for the function instead.
12699
12700      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12701      the LABEL_DECL nodes for the function however, and we output DWARF info
12702      for those in decls_for_scope.  Just within the `outer_scope' there will be
12703      a BLOCK node representing the function's outermost pair of curly braces,
12704      and any blocks used for the base and member initializers of a C++
12705      constructor function.  */
12706   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12707     {
12708       /* Emit a DW_TAG_variable DIE for a named return value.  */
12709       if (DECL_NAME (DECL_RESULT (decl)))
12710         gen_decl_die (DECL_RESULT (decl), subr_die);
12711
12712       current_function_has_inlines = 0;
12713       decls_for_scope (outer_scope, subr_die, 0);
12714
12715 #if 0 && defined (MIPS_DEBUGGING_INFO)
12716       if (current_function_has_inlines)
12717         {
12718           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12719           if (! comp_unit_has_inlines)
12720             {
12721               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12722               comp_unit_has_inlines = 1;
12723             }
12724         }
12725 #endif
12726     }
12727   /* Add the calling convention attribute if requested.  */
12728   add_calling_convention_attribute (subr_die, decl);
12729
12730 }
12731
12732 /* Generate a DIE to represent a declared data object.  */
12733
12734 static void
12735 gen_variable_die (tree decl, dw_die_ref context_die)
12736 {
12737   HOST_WIDE_INT off;
12738   tree com_decl;
12739   dw_die_ref var_die;
12740   tree origin = decl_ultimate_origin (decl);
12741   dw_die_ref old_die = lookup_decl_die (decl);
12742   int declaration = (DECL_EXTERNAL (decl)
12743                      /* If DECL is COMDAT and has not actually been
12744                         emitted, we cannot take its address; there
12745                         might end up being no definition anywhere in
12746                         the program.  For example, consider the C++
12747                         test case:
12748
12749                           template <class T>
12750                           struct S { static const int i = 7; };
12751
12752                           template <class T>
12753                           const int S<T>::i;
12754
12755                           int f() { return S<int>::i; }
12756
12757                         Here, S<int>::i is not DECL_EXTERNAL, but no
12758                         definition is required, so the compiler will
12759                         not emit a definition.  */
12760                      || (TREE_CODE (decl) == VAR_DECL
12761                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12762                      || class_or_namespace_scope_p (context_die));
12763
12764   com_decl = fortran_common (decl, &off);
12765
12766   /* Symbol in common gets emitted as a child of the common block, in the form
12767      of a data member.
12768
12769      ??? This creates a new common block die for every common block symbol.
12770      Better to share same common block die for all symbols in that block.  */
12771   if (com_decl)
12772     {
12773       tree field;
12774       dw_die_ref com_die;
12775       const char *cnam = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
12776       dw_loc_descr_ref loc = loc_descriptor_from_tree (com_decl);
12777
12778       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
12779       var_die = new_die (DW_TAG_common_block, context_die, decl);
12780       add_name_and_src_coords_attributes (var_die, field);
12781       add_AT_flag (var_die, DW_AT_external, 1);
12782       add_AT_loc (var_die, DW_AT_location, loc);
12783       com_die = new_die (DW_TAG_member, var_die, decl);
12784       add_name_and_src_coords_attributes (com_die, decl);
12785       add_type_attribute (com_die, TREE_TYPE (decl), TREE_READONLY (decl),
12786                           TREE_THIS_VOLATILE (decl), context_die);
12787       add_AT_loc (com_die, DW_AT_data_member_location,
12788                   int_loc_descriptor (off));
12789       add_pubname_string (cnam, var_die); /* ??? needed? */
12790       return;
12791     }
12792
12793   var_die = new_die (DW_TAG_variable, context_die, decl);
12794
12795   if (origin != NULL)
12796     add_abstract_origin_attribute (var_die, origin);
12797
12798   /* Loop unrolling can create multiple blocks that refer to the same
12799      static variable, so we must test for the DW_AT_declaration flag.
12800
12801      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12802      copy decls and set the DECL_ABSTRACT flag on them instead of
12803      sharing them.
12804
12805      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12806
12807      ??? The declare_in_namespace support causes us to get two DIEs for one
12808      variable, both of which are declarations.  We want to avoid considering
12809      one to be a specification, so we must test that this DIE is not a
12810      declaration.  */
12811   else if (old_die && TREE_STATIC (decl) && ! declaration
12812            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12813     {
12814       /* This is a definition of a C++ class level static.  */
12815       add_AT_specification (var_die, old_die);
12816       if (DECL_NAME (decl))
12817         {
12818           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12819           struct dwarf_file_data * file_index = lookup_filename (s.file);
12820
12821           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12822             add_AT_file (var_die, DW_AT_decl_file, file_index);
12823
12824           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12825             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12826         }
12827     }
12828   else
12829     {
12830       tree type = TREE_TYPE (decl);
12831       if ((TREE_CODE (decl) == PARM_DECL
12832            || TREE_CODE (decl) == RESULT_DECL)
12833           && DECL_BY_REFERENCE (decl))
12834         type = TREE_TYPE (type);
12835
12836       add_name_and_src_coords_attributes (var_die, decl);
12837       add_type_attribute (var_die, type, TREE_READONLY (decl),
12838                           TREE_THIS_VOLATILE (decl), context_die);
12839
12840       if (TREE_PUBLIC (decl))
12841         add_AT_flag (var_die, DW_AT_external, 1);
12842
12843       if (DECL_ARTIFICIAL (decl))
12844         add_AT_flag (var_die, DW_AT_artificial, 1);
12845
12846       if (TREE_PROTECTED (decl))
12847         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12848       else if (TREE_PRIVATE (decl))
12849         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12850     }
12851
12852   if (declaration)
12853     add_AT_flag (var_die, DW_AT_declaration, 1);
12854
12855   if (DECL_ABSTRACT (decl) || declaration)
12856     equate_decl_number_to_die (decl, var_die);
12857
12858   if (! declaration && ! DECL_ABSTRACT (decl))
12859     {
12860       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12861       add_pubname (decl, var_die);
12862     }
12863   else
12864     tree_add_const_value_attribute (var_die, decl);
12865 }
12866
12867 /* Generate a DIE to represent a label identifier.  */
12868
12869 static void
12870 gen_label_die (tree decl, dw_die_ref context_die)
12871 {
12872   tree origin = decl_ultimate_origin (decl);
12873   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12874   rtx insn;
12875   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12876
12877   if (origin != NULL)
12878     add_abstract_origin_attribute (lbl_die, origin);
12879   else
12880     add_name_and_src_coords_attributes (lbl_die, decl);
12881
12882   if (DECL_ABSTRACT (decl))
12883     equate_decl_number_to_die (decl, lbl_die);
12884   else
12885     {
12886       insn = DECL_RTL_IF_SET (decl);
12887
12888       /* Deleted labels are programmer specified labels which have been
12889          eliminated because of various optimizations.  We still emit them
12890          here so that it is possible to put breakpoints on them.  */
12891       if (insn
12892           && (LABEL_P (insn)
12893               || ((NOTE_P (insn)
12894                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12895         {
12896           /* When optimization is enabled (via -O) some parts of the compiler
12897              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12898              represent source-level labels which were explicitly declared by
12899              the user.  This really shouldn't be happening though, so catch
12900              it if it ever does happen.  */
12901           gcc_assert (!INSN_DELETED_P (insn));
12902
12903           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12904           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12905         }
12906     }
12907 }
12908
12909 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12910    attributes to the DIE for a block STMT, to describe where the inlined
12911    function was called from.  This is similar to add_src_coords_attributes.  */
12912
12913 static inline void
12914 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12915 {
12916   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12917
12918   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12919   add_AT_unsigned (die, DW_AT_call_line, s.line);
12920 }
12921
12922
12923 /* If STMT's abstract origin is a function declaration and STMT's
12924    first subblock's abstract origin is the function's outermost block,
12925    then we're looking at the main entry point.  */
12926 static bool
12927 is_inlined_entry_point (const_tree stmt)
12928 {
12929   tree decl, block;
12930
12931   if (!stmt || TREE_CODE (stmt) != BLOCK)
12932     return false;
12933
12934   decl = block_ultimate_origin (stmt);
12935
12936   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12937     return false;
12938
12939   block = BLOCK_SUBBLOCKS (stmt);
12940
12941   if (block)
12942     {
12943       if (TREE_CODE (block) != BLOCK)
12944         return false;
12945
12946       block = block_ultimate_origin (block);
12947     }
12948
12949   return block == DECL_INITIAL (decl);
12950 }
12951
12952 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12953    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12954
12955 static inline void
12956 add_high_low_attributes (tree stmt, dw_die_ref die)
12957 {
12958   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12959
12960   if (BLOCK_FRAGMENT_CHAIN (stmt))
12961     {
12962       tree chain;
12963
12964       if (is_inlined_entry_point (stmt))
12965         {
12966           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12967                                        BLOCK_NUMBER (stmt));
12968           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12969         }
12970
12971       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12972
12973       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12974       do
12975         {
12976           add_ranges (chain);
12977           chain = BLOCK_FRAGMENT_CHAIN (chain);
12978         }
12979       while (chain);
12980       add_ranges (NULL);
12981     }
12982   else
12983     {
12984       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12985                                    BLOCK_NUMBER (stmt));
12986       add_AT_lbl_id (die, DW_AT_low_pc, label);
12987       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12988                                    BLOCK_NUMBER (stmt));
12989       add_AT_lbl_id (die, DW_AT_high_pc, label);
12990     }
12991 }
12992
12993 /* Generate a DIE for a lexical block.  */
12994
12995 static void
12996 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12997 {
12998   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12999
13000   if (! BLOCK_ABSTRACT (stmt))
13001     add_high_low_attributes (stmt, stmt_die);
13002
13003   decls_for_scope (stmt, stmt_die, depth);
13004 }
13005
13006 /* Generate a DIE for an inlined subprogram.  */
13007
13008 static void
13009 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
13010 {
13011   tree decl = block_ultimate_origin (stmt);
13012
13013   /* Emit info for the abstract instance first, if we haven't yet.  We
13014      must emit this even if the block is abstract, otherwise when we
13015      emit the block below (or elsewhere), we may end up trying to emit
13016      a die whose origin die hasn't been emitted, and crashing.  */
13017   dwarf2out_abstract_function (decl);
13018
13019   if (! BLOCK_ABSTRACT (stmt))
13020     {
13021       dw_die_ref subr_die
13022         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
13023
13024       add_abstract_origin_attribute (subr_die, decl);
13025       add_high_low_attributes (stmt, subr_die);
13026       add_call_src_coords_attributes (stmt, subr_die);
13027
13028       decls_for_scope (stmt, subr_die, depth);
13029       current_function_has_inlines = 1;
13030     }
13031   else
13032     /* We may get here if we're the outer block of function A that was
13033        inlined into function B that was inlined into function C.  When
13034        generating debugging info for C, dwarf2out_abstract_function(B)
13035        would mark all inlined blocks as abstract, including this one.
13036        So, we wouldn't (and shouldn't) expect labels to be generated
13037        for this one.  Instead, just emit debugging info for
13038        declarations within the block.  This is particularly important
13039        in the case of initializers of arguments passed from B to us:
13040        if they're statement expressions containing declarations, we
13041        wouldn't generate dies for their abstract variables, and then,
13042        when generating dies for the real variables, we'd die (pun
13043        intended :-)  */
13044     gen_lexical_block_die (stmt, context_die, depth);
13045 }
13046
13047 /* Generate a DIE for a field in a record, or structure.  */
13048
13049 static void
13050 gen_field_die (tree decl, dw_die_ref context_die)
13051 {
13052   dw_die_ref decl_die;
13053
13054   if (TREE_TYPE (decl) == error_mark_node)
13055     return;
13056
13057   decl_die = new_die (DW_TAG_member, context_die, decl);
13058   add_name_and_src_coords_attributes (decl_die, decl);
13059   add_type_attribute (decl_die, member_declared_type (decl),
13060                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
13061                       context_die);
13062
13063   if (DECL_BIT_FIELD_TYPE (decl))
13064     {
13065       add_byte_size_attribute (decl_die, decl);
13066       add_bit_size_attribute (decl_die, decl);
13067       add_bit_offset_attribute (decl_die, decl);
13068     }
13069
13070   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
13071     add_data_member_location_attribute (decl_die, decl);
13072
13073   if (DECL_ARTIFICIAL (decl))
13074     add_AT_flag (decl_die, DW_AT_artificial, 1);
13075
13076   if (TREE_PROTECTED (decl))
13077     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
13078   else if (TREE_PRIVATE (decl))
13079     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
13080
13081   /* Equate decl number to die, so that we can look up this decl later on.  */
13082   equate_decl_number_to_die (decl, decl_die);
13083 }
13084
13085 #if 0
13086 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13087    Use modified_type_die instead.
13088    We keep this code here just in case these types of DIEs may be needed to
13089    represent certain things in other languages (e.g. Pascal) someday.  */
13090
13091 static void
13092 gen_pointer_type_die (tree type, dw_die_ref context_die)
13093 {
13094   dw_die_ref ptr_die
13095     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
13096
13097   equate_type_number_to_die (type, ptr_die);
13098   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13099   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13100 }
13101
13102 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
13103    Use modified_type_die instead.
13104    We keep this code here just in case these types of DIEs may be needed to
13105    represent certain things in other languages (e.g. Pascal) someday.  */
13106
13107 static void
13108 gen_reference_type_die (tree type, dw_die_ref context_die)
13109 {
13110   dw_die_ref ref_die
13111     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
13112
13113   equate_type_number_to_die (type, ref_die);
13114   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
13115   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
13116 }
13117 #endif
13118
13119 /* Generate a DIE for a pointer to a member type.  */
13120
13121 static void
13122 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
13123 {
13124   dw_die_ref ptr_die
13125     = new_die (DW_TAG_ptr_to_member_type,
13126                scope_die_for (type, context_die), type);
13127
13128   equate_type_number_to_die (type, ptr_die);
13129   add_AT_die_ref (ptr_die, DW_AT_containing_type,
13130                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
13131   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
13132 }
13133
13134 /* Generate the DIE for the compilation unit.  */
13135
13136 static dw_die_ref
13137 gen_compile_unit_die (const char *filename)
13138 {
13139   dw_die_ref die;
13140   char producer[250];
13141   const char *language_string = lang_hooks.name;
13142   int language;
13143
13144   die = new_die (DW_TAG_compile_unit, NULL, NULL);
13145
13146   if (filename)
13147     {
13148       add_name_attribute (die, filename);
13149       /* Don't add cwd for <built-in>.  */
13150       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13151         add_comp_dir_attribute (die);
13152     }
13153
13154   sprintf (producer, "%s %s", language_string, version_string);
13155
13156 #ifdef MIPS_DEBUGGING_INFO
13157   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13158      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13159      not appear in the producer string, the debugger reaches the conclusion
13160      that the object file is stripped and has no debugging information.
13161      To get the MIPS/SGI debugger to believe that there is debugging
13162      information in the object file, we add a -g to the producer string.  */
13163   if (debug_info_level > DINFO_LEVEL_TERSE)
13164     strcat (producer, " -g");
13165 #endif
13166
13167   add_AT_string (die, DW_AT_producer, producer);
13168
13169   if (strcmp (language_string, "GNU C++") == 0)
13170     language = DW_LANG_C_plus_plus;
13171   else if (strcmp (language_string, "GNU Ada") == 0)
13172     language = DW_LANG_Ada95;
13173   else if (strcmp (language_string, "GNU F77") == 0)
13174     language = DW_LANG_Fortran77;
13175   else if (strcmp (language_string, "GNU Fortran") == 0)
13176     language = DW_LANG_Fortran95;
13177   else if (strcmp (language_string, "GNU Pascal") == 0)
13178     language = DW_LANG_Pascal83;
13179   else if (strcmp (language_string, "GNU Java") == 0)
13180     language = DW_LANG_Java;
13181   else if (strcmp (language_string, "GNU Objective-C") == 0)
13182     language = DW_LANG_ObjC;
13183   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13184     language = DW_LANG_ObjC_plus_plus;
13185   else
13186     language = DW_LANG_C89;
13187
13188   add_AT_unsigned (die, DW_AT_language, language);
13189   return die;
13190 }
13191
13192 /* Generate the DIE for a base class.  */
13193
13194 static void
13195 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13196 {
13197   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13198
13199   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13200   add_data_member_location_attribute (die, binfo);
13201
13202   if (BINFO_VIRTUAL_P (binfo))
13203     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13204
13205   if (access == access_public_node)
13206     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13207   else if (access == access_protected_node)
13208     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13209 }
13210
13211 /* Generate a DIE for a class member.  */
13212
13213 static void
13214 gen_member_die (tree type, dw_die_ref context_die)
13215 {
13216   tree member;
13217   tree binfo = TYPE_BINFO (type);
13218   dw_die_ref child;
13219
13220   /* If this is not an incomplete type, output descriptions of each of its
13221      members. Note that as we output the DIEs necessary to represent the
13222      members of this record or union type, we will also be trying to output
13223      DIEs to represent the *types* of those members. However the `type'
13224      function (above) will specifically avoid generating type DIEs for member
13225      types *within* the list of member DIEs for this (containing) type except
13226      for those types (of members) which are explicitly marked as also being
13227      members of this (containing) type themselves.  The g++ front- end can
13228      force any given type to be treated as a member of some other (containing)
13229      type by setting the TYPE_CONTEXT of the given (member) type to point to
13230      the TREE node representing the appropriate (containing) type.  */
13231
13232   /* First output info about the base classes.  */
13233   if (binfo)
13234     {
13235       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13236       int i;
13237       tree base;
13238
13239       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13240         gen_inheritance_die (base,
13241                              (accesses ? VEC_index (tree, accesses, i)
13242                               : access_public_node), context_die);
13243     }
13244
13245   /* Now output info about the data members and type members.  */
13246   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13247     {
13248       /* If we thought we were generating minimal debug info for TYPE
13249          and then changed our minds, some of the member declarations
13250          may have already been defined.  Don't define them again, but
13251          do put them in the right order.  */
13252
13253       child = lookup_decl_die (member);
13254       if (child)
13255         splice_child_die (context_die, child);
13256       else
13257         gen_decl_die (member, context_die);
13258     }
13259
13260   /* Now output info about the function members (if any).  */
13261   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13262     {
13263       /* Don't include clones in the member list.  */
13264       if (DECL_ABSTRACT_ORIGIN (member))
13265         continue;
13266
13267       child = lookup_decl_die (member);
13268       if (child)
13269         splice_child_die (context_die, child);
13270       else
13271         gen_decl_die (member, context_die);
13272     }
13273 }
13274
13275 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13276    is set, we pretend that the type was never defined, so we only get the
13277    member DIEs needed by later specification DIEs.  */
13278
13279 static void
13280 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13281                                 enum debug_info_usage usage)
13282 {
13283   dw_die_ref type_die = lookup_type_die (type);
13284   dw_die_ref scope_die = 0;
13285   int nested = 0;
13286   int complete = (TYPE_SIZE (type)
13287                   && (! TYPE_STUB_DECL (type)
13288                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13289   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13290   complete = complete && should_emit_struct_debug (type, usage);
13291
13292   if (type_die && ! complete)
13293     return;
13294
13295   if (TYPE_CONTEXT (type) != NULL_TREE
13296       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13297           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13298     nested = 1;
13299
13300   scope_die = scope_die_for (type, context_die);
13301
13302   if (! type_die || (nested && scope_die == comp_unit_die))
13303     /* First occurrence of type or toplevel definition of nested class.  */
13304     {
13305       dw_die_ref old_die = type_die;
13306
13307       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13308                           ? record_type_tag (type) : DW_TAG_union_type,
13309                           scope_die, type);
13310       equate_type_number_to_die (type, type_die);
13311       if (old_die)
13312         add_AT_specification (type_die, old_die);
13313       else
13314         add_name_attribute (type_die, type_tag (type));
13315     }
13316   else
13317     remove_AT (type_die, DW_AT_declaration);
13318
13319   /* If this type has been completed, then give it a byte_size attribute and
13320      then give a list of members.  */
13321   if (complete && !ns_decl)
13322     {
13323       /* Prevent infinite recursion in cases where the type of some member of
13324          this type is expressed in terms of this type itself.  */
13325       TREE_ASM_WRITTEN (type) = 1;
13326       add_byte_size_attribute (type_die, type);
13327       if (TYPE_STUB_DECL (type) != NULL_TREE)
13328         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13329
13330       /* If the first reference to this type was as the return type of an
13331          inline function, then it may not have a parent.  Fix this now.  */
13332       if (type_die->die_parent == NULL)
13333         add_child_die (scope_die, type_die);
13334
13335       push_decl_scope (type);
13336       gen_member_die (type, type_die);
13337       pop_decl_scope ();
13338
13339       /* GNU extension: Record what type our vtable lives in.  */
13340       if (TYPE_VFIELD (type))
13341         {
13342           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13343
13344           gen_type_die (vtype, context_die);
13345           add_AT_die_ref (type_die, DW_AT_containing_type,
13346                           lookup_type_die (vtype));
13347         }
13348     }
13349   else
13350     {
13351       add_AT_flag (type_die, DW_AT_declaration, 1);
13352
13353       /* We don't need to do this for function-local types.  */
13354       if (TYPE_STUB_DECL (type)
13355           && ! decl_function_context (TYPE_STUB_DECL (type)))
13356         VEC_safe_push (tree, gc, incomplete_types, type);
13357     }
13358
13359   if (get_AT (type_die, DW_AT_name))
13360     add_pubtype (type, type_die);
13361 }
13362
13363 /* Generate a DIE for a subroutine _type_.  */
13364
13365 static void
13366 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13367 {
13368   tree return_type = TREE_TYPE (type);
13369   dw_die_ref subr_die
13370     = new_die (DW_TAG_subroutine_type,
13371                scope_die_for (type, context_die), type);
13372
13373   equate_type_number_to_die (type, subr_die);
13374   add_prototyped_attribute (subr_die, type);
13375   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13376   gen_formal_types_die (type, subr_die);
13377
13378   if (get_AT (subr_die, DW_AT_name))
13379     add_pubtype (type, subr_die);
13380 }
13381
13382 /* Generate a DIE for a type definition.  */
13383
13384 static void
13385 gen_typedef_die (tree decl, dw_die_ref context_die)
13386 {
13387   dw_die_ref type_die;
13388   tree origin;
13389
13390   if (TREE_ASM_WRITTEN (decl))
13391     return;
13392
13393   TREE_ASM_WRITTEN (decl) = 1;
13394   type_die = new_die (DW_TAG_typedef, context_die, decl);
13395   origin = decl_ultimate_origin (decl);
13396   if (origin != NULL)
13397     add_abstract_origin_attribute (type_die, origin);
13398   else
13399     {
13400       tree type;
13401
13402       add_name_and_src_coords_attributes (type_die, decl);
13403       if (DECL_ORIGINAL_TYPE (decl))
13404         {
13405           type = DECL_ORIGINAL_TYPE (decl);
13406
13407           gcc_assert (type != TREE_TYPE (decl));
13408           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13409         }
13410       else
13411         type = TREE_TYPE (decl);
13412
13413       add_type_attribute (type_die, type, TREE_READONLY (decl),
13414                           TREE_THIS_VOLATILE (decl), context_die);
13415     }
13416
13417   if (DECL_ABSTRACT (decl))
13418     equate_decl_number_to_die (decl, type_die);
13419
13420   if (get_AT (type_die, DW_AT_name))
13421     add_pubtype (decl, type_die);
13422 }
13423
13424 /* Generate a type description DIE.  */
13425
13426 static void
13427 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13428                                 enum debug_info_usage usage)
13429 {
13430   int need_pop;
13431   struct array_descr_info info;
13432
13433   if (type == NULL_TREE || type == error_mark_node)
13434     return;
13435
13436   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13437       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13438     {
13439       if (TREE_ASM_WRITTEN (type))
13440         return;
13441
13442       /* Prevent broken recursion; we can't hand off to the same type.  */
13443       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13444
13445       TREE_ASM_WRITTEN (type) = 1;
13446       gen_decl_die (TYPE_NAME (type), context_die);
13447       return;
13448     }
13449
13450   /* If this is an array type with hidden descriptor, handle it first.  */
13451   if (!TREE_ASM_WRITTEN (type)
13452       && lang_hooks.types.get_array_descr_info
13453       && lang_hooks.types.get_array_descr_info (type, &info))
13454     {
13455       gen_descr_array_type_die (type, &info, context_die);
13456       TREE_ASM_WRITTEN (type) = 1;
13457       return;
13458     }
13459
13460   /* We are going to output a DIE to represent the unqualified version
13461      of this type (i.e. without any const or volatile qualifiers) so
13462      get the main variant (i.e. the unqualified version) of this type
13463      now.  (Vectors are special because the debugging info is in the
13464      cloned type itself).  */
13465   if (TREE_CODE (type) != VECTOR_TYPE)
13466     type = type_main_variant (type);
13467
13468   if (TREE_ASM_WRITTEN (type))
13469     return;
13470
13471   switch (TREE_CODE (type))
13472     {
13473     case ERROR_MARK:
13474       break;
13475
13476     case POINTER_TYPE:
13477     case REFERENCE_TYPE:
13478       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13479          ensures that the gen_type_die recursion will terminate even if the
13480          type is recursive.  Recursive types are possible in Ada.  */
13481       /* ??? We could perhaps do this for all types before the switch
13482          statement.  */
13483       TREE_ASM_WRITTEN (type) = 1;
13484
13485       /* For these types, all that is required is that we output a DIE (or a
13486          set of DIEs) to represent the "basis" type.  */
13487       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13488                                 DINFO_USAGE_IND_USE);
13489       break;
13490
13491     case OFFSET_TYPE:
13492       /* This code is used for C++ pointer-to-data-member types.
13493          Output a description of the relevant class type.  */
13494       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13495                                         DINFO_USAGE_IND_USE);
13496
13497       /* Output a description of the type of the object pointed to.  */
13498       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13499                                         DINFO_USAGE_IND_USE);
13500
13501       /* Now output a DIE to represent this pointer-to-data-member type
13502          itself.  */
13503       gen_ptr_to_mbr_type_die (type, context_die);
13504       break;
13505
13506     case FUNCTION_TYPE:
13507       /* Force out return type (in case it wasn't forced out already).  */
13508       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13509                                         DINFO_USAGE_DIR_USE);
13510       gen_subroutine_type_die (type, context_die);
13511       break;
13512
13513     case METHOD_TYPE:
13514       /* Force out return type (in case it wasn't forced out already).  */
13515       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13516                                         DINFO_USAGE_DIR_USE);
13517       gen_subroutine_type_die (type, context_die);
13518       break;
13519
13520     case ARRAY_TYPE:
13521       gen_array_type_die (type, context_die);
13522       break;
13523
13524     case VECTOR_TYPE:
13525       gen_array_type_die (type, context_die);
13526       break;
13527
13528     case ENUMERAL_TYPE:
13529     case RECORD_TYPE:
13530     case UNION_TYPE:
13531     case QUAL_UNION_TYPE:
13532       /* If this is a nested type whose containing class hasn't been written
13533          out yet, writing it out will cover this one, too.  This does not apply
13534          to instantiations of member class templates; they need to be added to
13535          the containing class as they are generated.  FIXME: This hurts the
13536          idea of combining type decls from multiple TUs, since we can't predict
13537          what set of template instantiations we'll get.  */
13538       if (TYPE_CONTEXT (type)
13539           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13540           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13541         {
13542           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13543
13544           if (TREE_ASM_WRITTEN (type))
13545             return;
13546
13547           /* If that failed, attach ourselves to the stub.  */
13548           push_decl_scope (TYPE_CONTEXT (type));
13549           context_die = lookup_type_die (TYPE_CONTEXT (type));
13550           need_pop = 1;
13551         }
13552       else
13553         {
13554           declare_in_namespace (type, context_die);
13555           need_pop = 0;
13556         }
13557
13558       if (TREE_CODE (type) == ENUMERAL_TYPE)
13559         {
13560           /* This might have been written out by the call to
13561              declare_in_namespace.  */
13562           if (!TREE_ASM_WRITTEN (type))
13563             gen_enumeration_type_die (type, context_die);
13564         }
13565       else
13566         gen_struct_or_union_type_die (type, context_die, usage);
13567
13568       if (need_pop)
13569         pop_decl_scope ();
13570
13571       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13572          it up if it is ever completed.  gen_*_type_die will set it for us
13573          when appropriate.  */
13574       return;
13575
13576     case VOID_TYPE:
13577     case INTEGER_TYPE:
13578     case REAL_TYPE:
13579     case FIXED_POINT_TYPE:
13580     case COMPLEX_TYPE:
13581     case BOOLEAN_TYPE:
13582       /* No DIEs needed for fundamental types.  */
13583       break;
13584
13585     case LANG_TYPE:
13586       /* No Dwarf representation currently defined.  */
13587       break;
13588
13589     default:
13590       gcc_unreachable ();
13591     }
13592
13593   TREE_ASM_WRITTEN (type) = 1;
13594 }
13595
13596 static void
13597 gen_type_die (tree type, dw_die_ref context_die)
13598 {
13599   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13600 }
13601
13602 /* Generate a DIE for a tagged type instantiation.  */
13603
13604 static void
13605 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13606 {
13607   if (type == NULL_TREE || type == error_mark_node)
13608     return;
13609
13610   /* We are going to output a DIE to represent the unqualified version of
13611      this type (i.e. without any const or volatile qualifiers) so make sure
13612      that we have the main variant (i.e. the unqualified version) of this
13613      type now.  */
13614   gcc_assert (type == type_main_variant (type));
13615
13616   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13617      an instance of an unresolved type.  */
13618
13619   switch (TREE_CODE (type))
13620     {
13621     case ERROR_MARK:
13622       break;
13623
13624     case ENUMERAL_TYPE:
13625       gen_inlined_enumeration_type_die (type, context_die);
13626       break;
13627
13628     case RECORD_TYPE:
13629       gen_inlined_structure_type_die (type, context_die);
13630       break;
13631
13632     case UNION_TYPE:
13633     case QUAL_UNION_TYPE:
13634       gen_inlined_union_type_die (type, context_die);
13635       break;
13636
13637     default:
13638       gcc_unreachable ();
13639     }
13640 }
13641
13642 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13643    things which are local to the given block.  */
13644
13645 static void
13646 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13647 {
13648   int must_output_die = 0;
13649   tree origin;
13650   tree decl;
13651   enum tree_code origin_code;
13652
13653   /* Ignore blocks that are NULL.  */
13654   if (stmt == NULL_TREE)
13655     return;
13656
13657   /* If the block is one fragment of a non-contiguous block, do not
13658      process the variables, since they will have been done by the
13659      origin block.  Do process subblocks.  */
13660   if (BLOCK_FRAGMENT_ORIGIN (stmt))
13661     {
13662       tree sub;
13663
13664       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13665         gen_block_die (sub, context_die, depth + 1);
13666
13667       return;
13668     }
13669
13670   /* Determine the "ultimate origin" of this block.  This block may be an
13671      inlined instance of an inlined instance of inline function, so we have
13672      to trace all of the way back through the origin chain to find out what
13673      sort of node actually served as the original seed for the creation of
13674      the current block.  */
13675   origin = block_ultimate_origin (stmt);
13676   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13677
13678   /* Determine if we need to output any Dwarf DIEs at all to represent this
13679      block.  */
13680   if (origin_code == FUNCTION_DECL)
13681     /* The outer scopes for inlinings *must* always be represented.  We
13682        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13683     must_output_die = 1;
13684   else
13685     {
13686       /* In the case where the current block represents an inlining of the
13687          "body block" of an inline function, we must *NOT* output any DIE for
13688          this block because we have already output a DIE to represent the whole
13689          inlined function scope and the "body block" of any function doesn't
13690          really represent a different scope according to ANSI C rules.  So we
13691          check here to make sure that this block does not represent a "body
13692          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13693       if (! is_body_block (origin ? origin : stmt))
13694         {
13695           /* Determine if this block directly contains any "significant"
13696              local declarations which we will need to output DIEs for.  */
13697           if (debug_info_level > DINFO_LEVEL_TERSE)
13698             /* We are not in terse mode so *any* local declaration counts
13699                as being a "significant" one.  */
13700             must_output_die = (BLOCK_VARS (stmt) != NULL
13701                                && (TREE_USED (stmt)
13702                                    || TREE_ASM_WRITTEN (stmt)
13703                                    || BLOCK_ABSTRACT (stmt)));
13704           else
13705             /* We are in terse mode, so only local (nested) function
13706                definitions count as "significant" local declarations.  */
13707             for (decl = BLOCK_VARS (stmt);
13708                  decl != NULL; decl = TREE_CHAIN (decl))
13709               if (TREE_CODE (decl) == FUNCTION_DECL
13710                   && DECL_INITIAL (decl))
13711                 {
13712                   must_output_die = 1;
13713                   break;
13714                 }
13715         }
13716     }
13717
13718   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13719      DIE for any block which contains no significant local declarations at
13720      all.  Rather, in such cases we just call `decls_for_scope' so that any
13721      needed Dwarf info for any sub-blocks will get properly generated. Note
13722      that in terse mode, our definition of what constitutes a "significant"
13723      local declaration gets restricted to include only inlined function
13724      instances and local (nested) function definitions.  */
13725   if (must_output_die)
13726     {
13727       if (origin_code == FUNCTION_DECL)
13728         gen_inlined_subroutine_die (stmt, context_die, depth);
13729       else
13730         gen_lexical_block_die (stmt, context_die, depth);
13731     }
13732   else
13733     decls_for_scope (stmt, context_die, depth);
13734 }
13735
13736 /* Generate all of the decls declared within a given scope and (recursively)
13737    all of its sub-blocks.  */
13738
13739 static void
13740 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13741 {
13742   tree decl;
13743   tree subblocks;
13744
13745   /* Ignore NULL blocks.  */
13746   if (stmt == NULL_TREE)
13747     return;
13748
13749   if (TREE_USED (stmt))
13750     {
13751       /* Output the DIEs to represent all of the data objects and typedefs
13752          declared directly within this block but not within any nested
13753          sub-blocks.  Also, nested function and tag DIEs have been
13754          generated with a parent of NULL; fix that up now.  */
13755       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13756         {
13757           dw_die_ref die;
13758
13759           if (TREE_CODE (decl) == FUNCTION_DECL)
13760             die = lookup_decl_die (decl);
13761           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13762             die = lookup_type_die (TREE_TYPE (decl));
13763           else
13764             die = NULL;
13765
13766           if (die != NULL && die->die_parent == NULL)
13767             add_child_die (context_die, die);
13768           /* Do not produce debug information for static variables since
13769              these might be optimized out.  We are called for these later
13770              in varpool_analyze_pending_decls.
13771
13772              But *do* produce it for Fortran COMMON variables because,
13773              even though they are static, their names can differ depending
13774              on the scope, which we need to preserve.  */
13775           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)
13776               && !(is_fortran () && TREE_PUBLIC (decl)))
13777             ;
13778           else
13779             gen_decl_die (decl, context_die);
13780         }
13781     }
13782
13783   /* If we're at -g1, we're not interested in subblocks.  */
13784   if (debug_info_level <= DINFO_LEVEL_TERSE)
13785     return;
13786
13787   /* Output the DIEs to represent all sub-blocks (and the items declared
13788      therein) of this block.  */
13789   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13790        subblocks != NULL;
13791        subblocks = BLOCK_CHAIN (subblocks))
13792     gen_block_die (subblocks, context_die, depth + 1);
13793 }
13794
13795 /* Is this a typedef we can avoid emitting?  */
13796
13797 static inline int
13798 is_redundant_typedef (const_tree decl)
13799 {
13800   if (TYPE_DECL_IS_STUB (decl))
13801     return 1;
13802
13803   if (DECL_ARTIFICIAL (decl)
13804       && DECL_CONTEXT (decl)
13805       && is_tagged_type (DECL_CONTEXT (decl))
13806       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13807       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13808     /* Also ignore the artificial member typedef for the class name.  */
13809     return 1;
13810
13811   return 0;
13812 }
13813
13814 /* Returns the DIE for decl.  A DIE will always be returned.  */
13815
13816 static dw_die_ref
13817 force_decl_die (tree decl)
13818 {
13819   dw_die_ref decl_die;
13820   unsigned saved_external_flag;
13821   tree save_fn = NULL_TREE;
13822   decl_die = lookup_decl_die (decl);
13823   if (!decl_die)
13824     {
13825       dw_die_ref context_die;
13826       tree decl_context = DECL_CONTEXT (decl);
13827       if (decl_context)
13828         {
13829           /* Find die that represents this context.  */
13830           if (TYPE_P (decl_context))
13831             context_die = force_type_die (decl_context);
13832           else
13833             context_die = force_decl_die (decl_context);
13834         }
13835       else
13836         context_die = comp_unit_die;
13837
13838       decl_die = lookup_decl_die (decl);
13839       if (decl_die)
13840         return decl_die;
13841
13842       switch (TREE_CODE (decl))
13843         {
13844         case FUNCTION_DECL:
13845           /* Clear current_function_decl, so that gen_subprogram_die thinks
13846              that this is a declaration. At this point, we just want to force
13847              declaration die.  */
13848           save_fn = current_function_decl;
13849           current_function_decl = NULL_TREE;
13850           gen_subprogram_die (decl, context_die);
13851           current_function_decl = save_fn;
13852           break;
13853
13854         case VAR_DECL:
13855           /* Set external flag to force declaration die. Restore it after
13856            gen_decl_die() call.  */
13857           saved_external_flag = DECL_EXTERNAL (decl);
13858           DECL_EXTERNAL (decl) = 1;
13859           gen_decl_die (decl, context_die);
13860           DECL_EXTERNAL (decl) = saved_external_flag;
13861           break;
13862
13863         case NAMESPACE_DECL:
13864           dwarf2out_decl (decl);
13865           break;
13866
13867         default:
13868           gcc_unreachable ();
13869         }
13870
13871       /* We should be able to find the DIE now.  */
13872       if (!decl_die)
13873         decl_die = lookup_decl_die (decl);
13874       gcc_assert (decl_die);
13875     }
13876
13877   return decl_die;
13878 }
13879
13880 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13881    always returned.  */
13882
13883 static dw_die_ref
13884 force_type_die (tree type)
13885 {
13886   dw_die_ref type_die;
13887
13888   type_die = lookup_type_die (type);
13889   if (!type_die)
13890     {
13891       dw_die_ref context_die;
13892       if (TYPE_CONTEXT (type))
13893         {
13894           if (TYPE_P (TYPE_CONTEXT (type)))
13895             context_die = force_type_die (TYPE_CONTEXT (type));
13896           else
13897             context_die = force_decl_die (TYPE_CONTEXT (type));
13898         }
13899       else
13900         context_die = comp_unit_die;
13901
13902       type_die = modified_type_die (type, TYPE_READONLY (type),
13903                                     TYPE_VOLATILE (type), context_die);
13904       gcc_assert (type_die);
13905     }
13906   return type_die;
13907 }
13908
13909 /* Force out any required namespaces to be able to output DECL,
13910    and return the new context_die for it, if it's changed.  */
13911
13912 static dw_die_ref
13913 setup_namespace_context (tree thing, dw_die_ref context_die)
13914 {
13915   tree context = (DECL_P (thing)
13916                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13917   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13918     /* Force out the namespace.  */
13919     context_die = force_decl_die (context);
13920
13921   return context_die;
13922 }
13923
13924 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13925    type) within its namespace, if appropriate.
13926
13927    For compatibility with older debuggers, namespace DIEs only contain
13928    declarations; all definitions are emitted at CU scope.  */
13929
13930 static void
13931 declare_in_namespace (tree thing, dw_die_ref context_die)
13932 {
13933   dw_die_ref ns_context;
13934
13935   if (debug_info_level <= DINFO_LEVEL_TERSE)
13936     return;
13937
13938   /* If this decl is from an inlined function, then don't try to emit it in its
13939      namespace, as we will get confused.  It would have already been emitted
13940      when the abstract instance of the inline function was emitted anyways.  */
13941   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13942     return;
13943
13944   ns_context = setup_namespace_context (thing, context_die);
13945
13946   if (ns_context != context_die)
13947     {
13948       if (DECL_P (thing))
13949         gen_decl_die (thing, ns_context);
13950       else
13951         gen_type_die (thing, ns_context);
13952     }
13953 }
13954
13955 /* Generate a DIE for a namespace or namespace alias.  */
13956
13957 static void
13958 gen_namespace_die (tree decl)
13959 {
13960   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13961
13962   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13963      they are an alias of.  */
13964   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13965     {
13966       /* Output a real namespace.  */
13967       dw_die_ref namespace_die
13968         = new_die (DW_TAG_namespace, context_die, decl);
13969       add_name_and_src_coords_attributes (namespace_die, decl);
13970       equate_decl_number_to_die (decl, namespace_die);
13971     }
13972   else
13973     {
13974       /* Output a namespace alias.  */
13975
13976       /* Force out the namespace we are an alias of, if necessary.  */
13977       dw_die_ref origin_die
13978         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13979
13980       /* Now create the namespace alias DIE.  */
13981       dw_die_ref namespace_die
13982         = new_die (DW_TAG_imported_declaration, context_die, decl);
13983       add_name_and_src_coords_attributes (namespace_die, decl);
13984       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13985       equate_decl_number_to_die (decl, namespace_die);
13986     }
13987 }
13988
13989 /* Generate Dwarf debug information for a decl described by DECL.  */
13990
13991 static void
13992 gen_decl_die (tree decl, dw_die_ref context_die)
13993 {
13994   tree origin;
13995
13996   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13997     return;
13998
13999   switch (TREE_CODE (decl))
14000     {
14001     case ERROR_MARK:
14002       break;
14003
14004     case CONST_DECL:
14005       /* The individual enumerators of an enum type get output when we output
14006          the Dwarf representation of the relevant enum type itself.  */
14007       break;
14008
14009     case FUNCTION_DECL:
14010       /* Don't output any DIEs to represent mere function declarations,
14011          unless they are class members or explicit block externs.  */
14012       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
14013           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
14014         break;
14015
14016 #if 0
14017       /* FIXME */
14018       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
14019          on local redeclarations of global functions.  That seems broken.  */
14020       if (current_function_decl != decl)
14021         /* This is only a declaration.  */;
14022 #endif
14023
14024       /* If we're emitting a clone, emit info for the abstract instance.  */
14025       if (DECL_ORIGIN (decl) != decl)
14026         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
14027
14028       /* If we're emitting an out-of-line copy of an inline function,
14029          emit info for the abstract instance and set up to refer to it.  */
14030       else if (cgraph_function_possibly_inlined_p (decl)
14031                && ! DECL_ABSTRACT (decl)
14032                && ! class_or_namespace_scope_p (context_die)
14033                /* dwarf2out_abstract_function won't emit a die if this is just
14034                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
14035                   that case, because that works only if we have a die.  */
14036                && DECL_INITIAL (decl) != NULL_TREE)
14037         {
14038           dwarf2out_abstract_function (decl);
14039           set_decl_origin_self (decl);
14040         }
14041
14042       /* Otherwise we're emitting the primary DIE for this decl.  */
14043       else if (debug_info_level > DINFO_LEVEL_TERSE)
14044         {
14045           /* Before we describe the FUNCTION_DECL itself, make sure that we
14046              have described its return type.  */
14047           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14048
14049           /* And its virtual context.  */
14050           if (DECL_VINDEX (decl) != NULL_TREE)
14051             gen_type_die (DECL_CONTEXT (decl), context_die);
14052
14053           /* And its containing type.  */
14054           origin = decl_class_context (decl);
14055           if (origin != NULL_TREE)
14056             gen_type_die_for_member (origin, decl, context_die);
14057
14058           /* And its containing namespace.  */
14059           declare_in_namespace (decl, context_die);
14060         }
14061
14062       /* Now output a DIE to represent the function itself.  */
14063       gen_subprogram_die (decl, context_die);
14064       break;
14065
14066     case TYPE_DECL:
14067       /* If we are in terse mode, don't generate any DIEs to represent any
14068          actual typedefs.  */
14069       if (debug_info_level <= DINFO_LEVEL_TERSE)
14070         break;
14071
14072       /* In the special case of a TYPE_DECL node representing the declaration
14073          of some type tag, if the given TYPE_DECL is marked as having been
14074          instantiated from some other (original) TYPE_DECL node (e.g. one which
14075          was generated within the original definition of an inline function) we
14076          have to generate a special (abbreviated) DW_TAG_structure_type,
14077          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
14078       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
14079           && is_tagged_type (TREE_TYPE (decl)))
14080         {
14081           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
14082           break;
14083         }
14084
14085       if (is_redundant_typedef (decl))
14086         gen_type_die (TREE_TYPE (decl), context_die);
14087       else
14088         /* Output a DIE to represent the typedef itself.  */
14089         gen_typedef_die (decl, context_die);
14090       break;
14091
14092     case LABEL_DECL:
14093       if (debug_info_level >= DINFO_LEVEL_NORMAL)
14094         gen_label_die (decl, context_die);
14095       break;
14096
14097     case VAR_DECL:
14098     case RESULT_DECL:
14099       /* If we are in terse mode, don't generate any DIEs to represent any
14100          variable declarations or definitions.  */
14101       if (debug_info_level <= DINFO_LEVEL_TERSE)
14102         break;
14103
14104       /* If this is the global definition of the Fortran COMMON block, we don't
14105          need to do anything.  Syntactically, the block itself has no identity,
14106          just its constituent identifiers.  */
14107       if (TREE_CODE (decl) == VAR_DECL
14108           && TREE_PUBLIC (decl)
14109           && TREE_STATIC (decl)
14110           && is_fortran ()
14111           && !DECL_HAS_VALUE_EXPR_P (decl))
14112         break;
14113
14114       /* Output any DIEs that are needed to specify the type of this data
14115          object.  */
14116       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
14117         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14118       else
14119         gen_type_die (TREE_TYPE (decl), context_die);
14120
14121       /* And its containing type.  */
14122       origin = decl_class_context (decl);
14123       if (origin != NULL_TREE)
14124         gen_type_die_for_member (origin, decl, context_die);
14125
14126       /* And its containing namespace.  */
14127       declare_in_namespace (decl, context_die);
14128
14129       /* Now output the DIE to represent the data object itself.  This gets
14130          complicated because of the possibility that the VAR_DECL really
14131          represents an inlined instance of a formal parameter for an inline
14132          function.  */
14133       origin = decl_ultimate_origin (decl);
14134       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
14135         gen_formal_parameter_die (decl, context_die);
14136       else
14137         gen_variable_die (decl, context_die);
14138       break;
14139
14140     case FIELD_DECL:
14141       /* Ignore the nameless fields that are used to skip bits but handle C++
14142          anonymous unions and structs.  */
14143       if (DECL_NAME (decl) != NULL_TREE
14144           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
14145           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
14146         {
14147           gen_type_die (member_declared_type (decl), context_die);
14148           gen_field_die (decl, context_die);
14149         }
14150       break;
14151
14152     case PARM_DECL:
14153       if (DECL_BY_REFERENCE (decl))
14154         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14155       else
14156         gen_type_die (TREE_TYPE (decl), context_die);
14157       gen_formal_parameter_die (decl, context_die);
14158       break;
14159
14160     case NAMESPACE_DECL:
14161       gen_namespace_die (decl);
14162       break;
14163
14164     default:
14165       /* Probably some frontend-internal decl.  Assume we don't care.  */
14166       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14167       break;
14168     }
14169 }
14170 \f
14171 /* Output debug information for global decl DECL.  Called from toplev.c after
14172    compilation proper has finished.  */
14173
14174 static void
14175 dwarf2out_global_decl (tree decl)
14176 {
14177   /* Output DWARF2 information for file-scope tentative data object
14178      declarations, file-scope (extern) function declarations (which
14179      had no corresponding body) and file-scope tagged type declarations
14180      and definitions which have not yet been forced out.
14181
14182      Ignore the global decl of any Fortran COMMON blocks which also
14183      wind up here though they have already been described in the local
14184      scope for the procedures using them.  */
14185   if (TREE_CODE (decl) == VAR_DECL
14186       && TREE_PUBLIC (decl) && TREE_STATIC (decl) && is_fortran ())
14187     return;
14188
14189   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14190     dwarf2out_decl (decl);
14191 }
14192
14193 /* Output debug information for type decl DECL.  Called from toplev.c
14194    and from language front ends (to record built-in types).  */
14195 static void
14196 dwarf2out_type_decl (tree decl, int local)
14197 {
14198   if (!local)
14199     dwarf2out_decl (decl);
14200 }
14201
14202 /* Output debug information for imported module or decl.  */
14203
14204 static void
14205 dwarf2out_imported_module_or_decl (tree decl, tree context)
14206 {
14207   dw_die_ref imported_die, at_import_die;
14208   dw_die_ref scope_die;
14209   expanded_location xloc;
14210
14211   if (debug_info_level <= DINFO_LEVEL_TERSE)
14212     return;
14213
14214   gcc_assert (decl);
14215
14216   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14217      We need decl DIE for reference and scope die. First, get DIE for the decl
14218      itself.  */
14219
14220   /* Get the scope die for decl context. Use comp_unit_die for global module
14221      or decl. If die is not found for non globals, force new die.  */
14222   if (!context)
14223     scope_die = comp_unit_die;
14224   else if (TYPE_P (context))
14225     {
14226       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14227         return;
14228     scope_die = force_type_die (context);
14229     }
14230   else
14231     scope_die = force_decl_die (context);
14232
14233   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14234   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14235     {
14236       if (is_base_type (TREE_TYPE (decl)))
14237         at_import_die = base_type_die (TREE_TYPE (decl));
14238       else
14239         at_import_die = force_type_die (TREE_TYPE (decl));
14240     }
14241   else
14242     {
14243       at_import_die = lookup_decl_die (decl);
14244       if (!at_import_die)
14245         {
14246           /* If we're trying to avoid duplicate debug info, we may not have
14247              emitted the member decl for this field.  Emit it now.  */
14248           if (TREE_CODE (decl) == FIELD_DECL)
14249             {
14250               tree type = DECL_CONTEXT (decl);
14251               dw_die_ref type_context_die;
14252
14253               if (TYPE_CONTEXT (type))
14254                 if (TYPE_P (TYPE_CONTEXT (type)))
14255                   {
14256                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14257                                                    DINFO_USAGE_DIR_USE))
14258                       return;
14259                   type_context_die = force_type_die (TYPE_CONTEXT (type));
14260                   }
14261               else
14262                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14263               else
14264                 type_context_die = comp_unit_die;
14265               gen_type_die_for_member (type, decl, type_context_die);
14266             }
14267           at_import_die = force_decl_die (decl);
14268         }
14269     }
14270
14271   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14272   if (TREE_CODE (decl) == NAMESPACE_DECL)
14273     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14274   else
14275     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14276
14277   xloc = expand_location (input_location);
14278   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14279   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14280   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14281 }
14282
14283 /* Write the debugging output for DECL.  */
14284
14285 void
14286 dwarf2out_decl (tree decl)
14287 {
14288   dw_die_ref context_die = comp_unit_die;
14289
14290   switch (TREE_CODE (decl))
14291     {
14292     case ERROR_MARK:
14293       return;
14294
14295     case FUNCTION_DECL:
14296       /* What we would really like to do here is to filter out all mere
14297          file-scope declarations of file-scope functions which are never
14298          referenced later within this translation unit (and keep all of ones
14299          that *are* referenced later on) but we aren't clairvoyant, so we have
14300          no idea which functions will be referenced in the future (i.e. later
14301          on within the current translation unit). So here we just ignore all
14302          file-scope function declarations which are not also definitions.  If
14303          and when the debugger needs to know something about these functions,
14304          it will have to hunt around and find the DWARF information associated
14305          with the definition of the function.
14306
14307          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14308          nodes represent definitions and which ones represent mere
14309          declarations.  We have to check DECL_INITIAL instead. That's because
14310          the C front-end supports some weird semantics for "extern inline"
14311          function definitions.  These can get inlined within the current
14312          translation unit (and thus, we need to generate Dwarf info for their
14313          abstract instances so that the Dwarf info for the concrete inlined
14314          instances can have something to refer to) but the compiler never
14315          generates any out-of-lines instances of such things (despite the fact
14316          that they *are* definitions).
14317
14318          The important point is that the C front-end marks these "extern
14319          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14320          them anyway. Note that the C++ front-end also plays some similar games
14321          for inline function definitions appearing within include files which
14322          also contain `#pragma interface' pragmas.  */
14323       if (DECL_INITIAL (decl) == NULL_TREE)
14324         return;
14325
14326       /* If we're a nested function, initially use a parent of NULL; if we're
14327          a plain function, this will be fixed up in decls_for_scope.  If
14328          we're a method, it will be ignored, since we already have a DIE.  */
14329       if (decl_function_context (decl)
14330           /* But if we're in terse mode, we don't care about scope.  */
14331           && debug_info_level > DINFO_LEVEL_TERSE)
14332         context_die = NULL;
14333       break;
14334
14335     case VAR_DECL:
14336       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14337          declaration and if the declaration was never even referenced from
14338          within this entire compilation unit.  We suppress these DIEs in
14339          order to save space in the .debug section (by eliminating entries
14340          which are probably useless).  Note that we must not suppress
14341          block-local extern declarations (whether used or not) because that
14342          would screw-up the debugger's name lookup mechanism and cause it to
14343          miss things which really ought to be in scope at a given point.  */
14344       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14345         return;
14346
14347       /* For local statics lookup proper context die.  */
14348       if (TREE_STATIC (decl) && decl_function_context (decl))
14349         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14350
14351       /* If we are in terse mode, don't generate any DIEs to represent any
14352          variable declarations or definitions.  */
14353       if (debug_info_level <= DINFO_LEVEL_TERSE)
14354         return;
14355       break;
14356
14357     case NAMESPACE_DECL:
14358       if (debug_info_level <= DINFO_LEVEL_TERSE)
14359         return;
14360       if (lookup_decl_die (decl) != NULL)
14361         return;
14362       break;
14363
14364     case TYPE_DECL:
14365       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14366       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14367         return;
14368
14369       /* Don't bother trying to generate any DIEs to represent any of the
14370          normal built-in types for the language we are compiling.  */
14371       if (DECL_IS_BUILTIN (decl))
14372         {
14373           /* OK, we need to generate one for `bool' so GDB knows what type
14374              comparisons have.  */
14375           if (is_cxx ()
14376               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14377               && ! DECL_IGNORED_P (decl))
14378             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14379
14380           return;
14381         }
14382
14383       /* If we are in terse mode, don't generate any DIEs for types.  */
14384       if (debug_info_level <= DINFO_LEVEL_TERSE)
14385         return;
14386
14387       /* If we're a function-scope tag, initially use a parent of NULL;
14388          this will be fixed up in decls_for_scope.  */
14389       if (decl_function_context (decl))
14390         context_die = NULL;
14391
14392       break;
14393
14394     default:
14395       return;
14396     }
14397
14398   gen_decl_die (decl, context_die);
14399 }
14400
14401 /* Output a marker (i.e. a label) for the beginning of the generated code for
14402    a lexical block.  */
14403
14404 static void
14405 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14406                        unsigned int blocknum)
14407 {
14408   switch_to_section (current_function_section ());
14409   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14410 }
14411
14412 /* Output a marker (i.e. a label) for the end of the generated code for a
14413    lexical block.  */
14414
14415 static void
14416 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14417 {
14418   switch_to_section (current_function_section ());
14419   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14420 }
14421
14422 /* Returns nonzero if it is appropriate not to emit any debugging
14423    information for BLOCK, because it doesn't contain any instructions.
14424
14425    Don't allow this for blocks with nested functions or local classes
14426    as we would end up with orphans, and in the presence of scheduling
14427    we may end up calling them anyway.  */
14428
14429 static bool
14430 dwarf2out_ignore_block (const_tree block)
14431 {
14432   tree decl;
14433
14434   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14435     if (TREE_CODE (decl) == FUNCTION_DECL
14436         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14437       return 0;
14438
14439   return 1;
14440 }
14441
14442 /* Hash table routines for file_hash.  */
14443
14444 static int
14445 file_table_eq (const void *p1_p, const void *p2_p)
14446 {
14447   const struct dwarf_file_data *const p1 =
14448     (const struct dwarf_file_data *) p1_p;
14449   const char *const p2 = (const char *) p2_p;
14450   return strcmp (p1->filename, p2) == 0;
14451 }
14452
14453 static hashval_t
14454 file_table_hash (const void *p_p)
14455 {
14456   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
14457   return htab_hash_string (p->filename);
14458 }
14459
14460 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14461    dwarf2out.c) and return its "index".  The index of each (known) filename is
14462    just a unique number which is associated with only that one filename.  We
14463    need such numbers for the sake of generating labels (in the .debug_sfnames
14464    section) and references to those files numbers (in the .debug_srcinfo
14465    and.debug_macinfo sections).  If the filename given as an argument is not
14466    found in our current list, add it to the list and assign it the next
14467    available unique index number.  In order to speed up searches, we remember
14468    the index of the filename was looked up last.  This handles the majority of
14469    all searches.  */
14470
14471 static struct dwarf_file_data *
14472 lookup_filename (const char *file_name)
14473 {
14474   void ** slot;
14475   struct dwarf_file_data * created;
14476
14477   /* Check to see if the file name that was searched on the previous
14478      call matches this file name.  If so, return the index.  */
14479   if (file_table_last_lookup
14480       && (file_name == file_table_last_lookup->filename
14481           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14482     return file_table_last_lookup;
14483
14484   /* Didn't match the previous lookup, search the table.  */
14485   slot = htab_find_slot_with_hash (file_table, file_name,
14486                                    htab_hash_string (file_name), INSERT);
14487   if (*slot)
14488     return (struct dwarf_file_data *) *slot;
14489
14490   created = GGC_NEW (struct dwarf_file_data);
14491   created->filename = file_name;
14492   created->emitted_number = 0;
14493   *slot = created;
14494   return created;
14495 }
14496
14497 /* If the assembler will construct the file table, then translate the compiler
14498    internal file table number into the assembler file table number, and emit
14499    a .file directive if we haven't already emitted one yet.  The file table
14500    numbers are different because we prune debug info for unused variables and
14501    types, which may include filenames.  */
14502
14503 static int
14504 maybe_emit_file (struct dwarf_file_data * fd)
14505 {
14506   if (! fd->emitted_number)
14507     {
14508       if (last_emitted_file)
14509         fd->emitted_number = last_emitted_file->emitted_number + 1;
14510       else
14511         fd->emitted_number = 1;
14512       last_emitted_file = fd;
14513
14514       if (DWARF2_ASM_LINE_DEBUG_INFO)
14515         {
14516           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14517           output_quoted_string (asm_out_file,
14518                                 remap_debug_filename (fd->filename));
14519           fputc ('\n', asm_out_file);
14520         }
14521     }
14522
14523   return fd->emitted_number;
14524 }
14525
14526 /* Called by the final INSN scan whenever we see a var location.  We
14527    use it to drop labels in the right places, and throw the location in
14528    our lookup table.  */
14529
14530 static void
14531 dwarf2out_var_location (rtx loc_note)
14532 {
14533   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14534   struct var_loc_node *newloc;
14535   rtx prev_insn;
14536   static rtx last_insn;
14537   static const char *last_label;
14538   tree decl;
14539
14540   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14541     return;
14542   prev_insn = PREV_INSN (loc_note);
14543
14544   newloc = GGC_CNEW (struct var_loc_node);
14545   /* If the insn we processed last time is the previous insn
14546      and it is also a var location note, use the label we emitted
14547      last time.  */
14548   if (last_insn != NULL_RTX
14549       && last_insn == prev_insn
14550       && NOTE_P (prev_insn)
14551       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14552     {
14553       newloc->label = last_label;
14554     }
14555   else
14556     {
14557       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14558       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14559       loclabel_num++;
14560       newloc->label = ggc_strdup (loclabel);
14561     }
14562   newloc->var_loc_note = loc_note;
14563   newloc->next = NULL;
14564
14565   if (cfun && in_cold_section_p)
14566     newloc->section_label = crtl->subsections.cold_section_label;
14567   else
14568     newloc->section_label = text_section_label;
14569
14570   last_insn = loc_note;
14571   last_label = newloc->label;
14572   decl = NOTE_VAR_LOCATION_DECL (loc_note);
14573   add_var_loc_to_decl (decl, newloc);
14574 }
14575
14576 /* We need to reset the locations at the beginning of each
14577    function. We can't do this in the end_function hook, because the
14578    declarations that use the locations won't have been output when
14579    that hook is called.  Also compute have_multiple_function_sections here.  */
14580
14581 static void
14582 dwarf2out_begin_function (tree fun)
14583 {
14584   htab_empty (decl_loc_table);
14585
14586   if (function_section (fun) != text_section)
14587     have_multiple_function_sections = true;
14588
14589   dwarf2out_note_section_used ();
14590 }
14591
14592 /* Output a label to mark the beginning of a source code line entry
14593    and record information relating to this source line, in
14594    'line_info_table' for later output of the .debug_line section.  */
14595
14596 static void
14597 dwarf2out_source_line (unsigned int line, const char *filename)
14598 {
14599   if (debug_info_level >= DINFO_LEVEL_NORMAL
14600       && line != 0)
14601     {
14602       int file_num = maybe_emit_file (lookup_filename (filename));
14603
14604       switch_to_section (current_function_section ());
14605
14606       /* If requested, emit something human-readable.  */
14607       if (flag_debug_asm)
14608         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14609                  filename, line);
14610
14611       if (DWARF2_ASM_LINE_DEBUG_INFO)
14612         {
14613           /* Emit the .loc directive understood by GNU as.  */
14614           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14615
14616           /* Indicate that line number info exists.  */
14617           line_info_table_in_use++;
14618         }
14619       else if (function_section (current_function_decl) != text_section)
14620         {
14621           dw_separate_line_info_ref line_info;
14622           targetm.asm_out.internal_label (asm_out_file,
14623                                           SEPARATE_LINE_CODE_LABEL,
14624                                           separate_line_info_table_in_use);
14625
14626           /* Expand the line info table if necessary.  */
14627           if (separate_line_info_table_in_use
14628               == separate_line_info_table_allocated)
14629             {
14630               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14631               separate_line_info_table
14632                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
14633                                  separate_line_info_table,
14634                                  separate_line_info_table_allocated);
14635               memset (separate_line_info_table
14636                        + separate_line_info_table_in_use,
14637                       0,
14638                       (LINE_INFO_TABLE_INCREMENT
14639                        * sizeof (dw_separate_line_info_entry)));
14640             }
14641
14642           /* Add the new entry at the end of the line_info_table.  */
14643           line_info
14644             = &separate_line_info_table[separate_line_info_table_in_use++];
14645           line_info->dw_file_num = file_num;
14646           line_info->dw_line_num = line;
14647           line_info->function = current_function_funcdef_no;
14648         }
14649       else
14650         {
14651           dw_line_info_ref line_info;
14652
14653           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14654                                      line_info_table_in_use);
14655
14656           /* Expand the line info table if necessary.  */
14657           if (line_info_table_in_use == line_info_table_allocated)
14658             {
14659               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14660               line_info_table
14661                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
14662                                  line_info_table_allocated);
14663               memset (line_info_table + line_info_table_in_use, 0,
14664                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14665             }
14666
14667           /* Add the new entry at the end of the line_info_table.  */
14668           line_info = &line_info_table[line_info_table_in_use++];
14669           line_info->dw_file_num = file_num;
14670           line_info->dw_line_num = line;
14671         }
14672     }
14673 }
14674
14675 /* Record the beginning of a new source file.  */
14676
14677 static void
14678 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14679 {
14680   if (flag_eliminate_dwarf2_dups)
14681     {
14682       /* Record the beginning of the file for break_out_includes.  */
14683       dw_die_ref bincl_die;
14684
14685       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14686       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14687     }
14688
14689   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14690     {
14691       int file_num = maybe_emit_file (lookup_filename (filename));
14692
14693       switch_to_section (debug_macinfo_section);
14694       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14695       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14696                                    lineno);
14697
14698       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14699     }
14700 }
14701
14702 /* Record the end of a source file.  */
14703
14704 static void
14705 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14706 {
14707   if (flag_eliminate_dwarf2_dups)
14708     /* Record the end of the file for break_out_includes.  */
14709     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14710
14711   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14712     {
14713       switch_to_section (debug_macinfo_section);
14714       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14715     }
14716 }
14717
14718 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14719    the tail part of the directive line, i.e. the part which is past the
14720    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14721
14722 static void
14723 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14724                   const char *buffer ATTRIBUTE_UNUSED)
14725 {
14726   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14727     {
14728       switch_to_section (debug_macinfo_section);
14729       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14730       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14731       dw2_asm_output_nstring (buffer, -1, "The macro");
14732     }
14733 }
14734
14735 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14736    the tail part of the directive line, i.e. the part which is past the
14737    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14738
14739 static void
14740 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14741                  const char *buffer ATTRIBUTE_UNUSED)
14742 {
14743   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14744     {
14745       switch_to_section (debug_macinfo_section);
14746       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14747       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14748       dw2_asm_output_nstring (buffer, -1, "The macro");
14749     }
14750 }
14751
14752 /* Set up for Dwarf output at the start of compilation.  */
14753
14754 static void
14755 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14756 {
14757   /* Allocate the file_table.  */
14758   file_table = htab_create_ggc (50, file_table_hash,
14759                                 file_table_eq, NULL);
14760
14761   /* Allocate the decl_die_table.  */
14762   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14763                                     decl_die_table_eq, NULL);
14764
14765   /* Allocate the decl_loc_table.  */
14766   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14767                                     decl_loc_table_eq, NULL);
14768
14769   /* Allocate the initial hunk of the decl_scope_table.  */
14770   decl_scope_table = VEC_alloc (tree, gc, 256);
14771
14772   /* Allocate the initial hunk of the abbrev_die_table.  */
14773   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
14774   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14775   /* Zero-th entry is allocated, but unused.  */
14776   abbrev_die_table_in_use = 1;
14777
14778   /* Allocate the initial hunk of the line_info_table.  */
14779   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
14780   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14781
14782   /* Zero-th entry is allocated, but unused.  */
14783   line_info_table_in_use = 1;
14784
14785   /* Allocate the pubtypes and pubnames vectors.  */
14786   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14787   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14788
14789   /* Generate the initial DIE for the .debug section.  Note that the (string)
14790      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14791      will (typically) be a relative pathname and that this pathname should be
14792      taken as being relative to the directory from which the compiler was
14793      invoked when the given (base) source file was compiled.  We will fill
14794      in this value in dwarf2out_finish.  */
14795   comp_unit_die = gen_compile_unit_die (NULL);
14796
14797   incomplete_types = VEC_alloc (tree, gc, 64);
14798
14799   used_rtx_array = VEC_alloc (rtx, gc, 32);
14800
14801   debug_info_section = get_section (DEBUG_INFO_SECTION,
14802                                     SECTION_DEBUG, NULL);
14803   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14804                                       SECTION_DEBUG, NULL);
14805   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14806                                        SECTION_DEBUG, NULL);
14807   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14808                                        SECTION_DEBUG, NULL);
14809   debug_line_section = get_section (DEBUG_LINE_SECTION,
14810                                     SECTION_DEBUG, NULL);
14811   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14812                                    SECTION_DEBUG, NULL);
14813   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14814                                         SECTION_DEBUG, NULL);
14815 #ifdef DEBUG_PUBTYPES_SECTION
14816   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14817                                         SECTION_DEBUG, NULL);
14818 #endif
14819   debug_str_section = get_section (DEBUG_STR_SECTION,
14820                                    DEBUG_STR_SECTION_FLAGS, NULL);
14821   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14822                                       SECTION_DEBUG, NULL);
14823   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14824                                      SECTION_DEBUG, NULL);
14825
14826   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14827   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14828                                DEBUG_ABBREV_SECTION_LABEL, 0);
14829   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14830   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14831                                COLD_TEXT_SECTION_LABEL, 0);
14832   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14833
14834   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14835                                DEBUG_INFO_SECTION_LABEL, 0);
14836   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14837                                DEBUG_LINE_SECTION_LABEL, 0);
14838   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14839                                DEBUG_RANGES_SECTION_LABEL, 0);
14840   switch_to_section (debug_abbrev_section);
14841   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14842   switch_to_section (debug_info_section);
14843   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14844   switch_to_section (debug_line_section);
14845   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14846
14847   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14848     {
14849       switch_to_section (debug_macinfo_section);
14850       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14851                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14852       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14853     }
14854
14855   switch_to_section (text_section);
14856   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14857   if (flag_reorder_blocks_and_partition)
14858     {
14859       cold_text_section = unlikely_text_section ();
14860       switch_to_section (cold_text_section);
14861       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14862     }
14863 }
14864
14865 /* A helper function for dwarf2out_finish called through
14866    ht_forall.  Emit one queued .debug_str string.  */
14867
14868 static int
14869 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14870 {
14871   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14872
14873   if (node->form == DW_FORM_strp)
14874     {
14875       switch_to_section (debug_str_section);
14876       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14877       assemble_string (node->str, strlen (node->str) + 1);
14878     }
14879
14880   return 1;
14881 }
14882
14883 #if ENABLE_ASSERT_CHECKING
14884 /* Verify that all marks are clear.  */
14885
14886 static void
14887 verify_marks_clear (dw_die_ref die)
14888 {
14889   dw_die_ref c;
14890
14891   gcc_assert (! die->die_mark);
14892   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14893 }
14894 #endif /* ENABLE_ASSERT_CHECKING */
14895
14896 /* Clear the marks for a die and its children.
14897    Be cool if the mark isn't set.  */
14898
14899 static void
14900 prune_unmark_dies (dw_die_ref die)
14901 {
14902   dw_die_ref c;
14903
14904   if (die->die_mark)
14905     die->die_mark = 0;
14906   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14907 }
14908
14909 /* Given DIE that we're marking as used, find any other dies
14910    it references as attributes and mark them as used.  */
14911
14912 static void
14913 prune_unused_types_walk_attribs (dw_die_ref die)
14914 {
14915   dw_attr_ref a;
14916   unsigned ix;
14917
14918   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14919     {
14920       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14921         {
14922           /* A reference to another DIE.
14923              Make sure that it will get emitted.  */
14924           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14925         }
14926       /* Set the string's refcount to 0 so that prune_unused_types_mark
14927          accounts properly for it.  */
14928       if (AT_class (a) == dw_val_class_str)
14929         a->dw_attr_val.v.val_str->refcount = 0;
14930     }
14931 }
14932
14933
14934 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14935    to DIE's children.  */
14936
14937 static void
14938 prune_unused_types_mark (dw_die_ref die, int dokids)
14939 {
14940   dw_die_ref c;
14941
14942   if (die->die_mark == 0)
14943     {
14944       /* We haven't done this node yet.  Mark it as used.  */
14945       die->die_mark = 1;
14946
14947       /* We also have to mark its parents as used.
14948          (But we don't want to mark our parents' kids due to this.)  */
14949       if (die->die_parent)
14950         prune_unused_types_mark (die->die_parent, 0);
14951
14952       /* Mark any referenced nodes.  */
14953       prune_unused_types_walk_attribs (die);
14954
14955       /* If this node is a specification,
14956          also mark the definition, if it exists.  */
14957       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14958         prune_unused_types_mark (die->die_definition, 1);
14959     }
14960
14961   if (dokids && die->die_mark != 2)
14962     {
14963       /* We need to walk the children, but haven't done so yet.
14964          Remember that we've walked the kids.  */
14965       die->die_mark = 2;
14966
14967       /* If this is an array type, we need to make sure our
14968          kids get marked, even if they're types.  */
14969       if (die->die_tag == DW_TAG_array_type)
14970         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14971       else
14972         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14973     }
14974 }
14975
14976
14977 /* Walk the tree DIE and mark types that we actually use.  */
14978
14979 static void
14980 prune_unused_types_walk (dw_die_ref die)
14981 {
14982   dw_die_ref c;
14983
14984   /* Don't do anything if this node is already marked.  */
14985   if (die->die_mark)
14986     return;
14987
14988   switch (die->die_tag)
14989     {
14990     case DW_TAG_const_type:
14991     case DW_TAG_packed_type:
14992     case DW_TAG_pointer_type:
14993     case DW_TAG_reference_type:
14994     case DW_TAG_volatile_type:
14995     case DW_TAG_typedef:
14996     case DW_TAG_array_type:
14997     case DW_TAG_structure_type:
14998     case DW_TAG_union_type:
14999     case DW_TAG_class_type:
15000     case DW_TAG_interface_type:
15001     case DW_TAG_friend:
15002     case DW_TAG_variant_part:
15003     case DW_TAG_enumeration_type:
15004     case DW_TAG_subroutine_type:
15005     case DW_TAG_string_type:
15006     case DW_TAG_set_type:
15007     case DW_TAG_subrange_type:
15008     case DW_TAG_ptr_to_member_type:
15009     case DW_TAG_file_type:
15010       if (die->die_perennial_p)
15011         break;
15012
15013       /* It's a type node --- don't mark it.  */
15014       return;
15015
15016     default:
15017       /* Mark everything else.  */
15018       break;
15019   }
15020
15021   die->die_mark = 1;
15022
15023   /* Now, mark any dies referenced from here.  */
15024   prune_unused_types_walk_attribs (die);
15025
15026   /* Mark children.  */
15027   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
15028 }
15029
15030 /* Increment the string counts on strings referred to from DIE's
15031    attributes.  */
15032
15033 static void
15034 prune_unused_types_update_strings (dw_die_ref die)
15035 {
15036   dw_attr_ref a;
15037   unsigned ix;
15038
15039   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
15040     if (AT_class (a) == dw_val_class_str)
15041       {
15042         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
15043         s->refcount++;
15044         /* Avoid unnecessarily putting strings that are used less than
15045            twice in the hash table.  */
15046         if (s->refcount
15047             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
15048           {
15049             void ** slot;
15050             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
15051                                              htab_hash_string (s->str),
15052                                              INSERT);
15053             gcc_assert (*slot == NULL);
15054             *slot = s;
15055           }
15056       }
15057 }
15058
15059 /* Remove from the tree DIE any dies that aren't marked.  */
15060
15061 static void
15062 prune_unused_types_prune (dw_die_ref die)
15063 {
15064   dw_die_ref c;
15065
15066   gcc_assert (die->die_mark);
15067   prune_unused_types_update_strings (die);
15068
15069   if (! die->die_child)
15070     return;
15071
15072   c = die->die_child;
15073   do {
15074     dw_die_ref prev = c;
15075     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
15076       if (c == die->die_child)
15077         {
15078           /* No marked children between 'prev' and the end of the list.  */
15079           if (prev == c)
15080             /* No marked children at all.  */
15081             die->die_child = NULL;
15082           else
15083             {
15084               prev->die_sib = c->die_sib;
15085               die->die_child = prev;
15086             }
15087           return;
15088         }
15089
15090     if (c != prev->die_sib)
15091       prev->die_sib = c;
15092     prune_unused_types_prune (c);
15093   } while (c != die->die_child);
15094 }
15095
15096
15097 /* Remove dies representing declarations that we never use.  */
15098
15099 static void
15100 prune_unused_types (void)
15101 {
15102   unsigned int i;
15103   limbo_die_node *node;
15104   pubname_ref pub;
15105
15106 #if ENABLE_ASSERT_CHECKING
15107   /* All the marks should already be clear.  */
15108   verify_marks_clear (comp_unit_die);
15109   for (node = limbo_die_list; node; node = node->next)
15110     verify_marks_clear (node->die);
15111 #endif /* ENABLE_ASSERT_CHECKING */
15112
15113   /* Set the mark on nodes that are actually used.  */
15114   prune_unused_types_walk (comp_unit_die);
15115   for (node = limbo_die_list; node; node = node->next)
15116     prune_unused_types_walk (node->die);
15117
15118   /* Also set the mark on nodes referenced from the
15119      pubname_table or arange_table.  */
15120   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
15121     prune_unused_types_mark (pub->die, 1);
15122   for (i = 0; i < arange_table_in_use; i++)
15123     prune_unused_types_mark (arange_table[i], 1);
15124
15125   /* Get rid of nodes that aren't marked; and update the string counts.  */
15126   if (debug_str_hash)
15127     htab_empty (debug_str_hash);
15128   prune_unused_types_prune (comp_unit_die);
15129   for (node = limbo_die_list; node; node = node->next)
15130     prune_unused_types_prune (node->die);
15131
15132   /* Leave the marks clear.  */
15133   prune_unmark_dies (comp_unit_die);
15134   for (node = limbo_die_list; node; node = node->next)
15135     prune_unmark_dies (node->die);
15136 }
15137
15138 /* Set the parameter to true if there are any relative pathnames in
15139    the file table.  */
15140 static int
15141 file_table_relative_p (void ** slot, void *param)
15142 {
15143   bool *p = (bool *) param;
15144   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
15145   if (!IS_ABSOLUTE_PATH (d->filename))
15146     {
15147       *p = true;
15148       return 0;
15149     }
15150   return 1;
15151 }
15152
15153 /* Output stuff that dwarf requires at the end of every file,
15154    and generate the DWARF-2 debugging info.  */
15155
15156 static void
15157 dwarf2out_finish (const char *filename)
15158 {
15159   limbo_die_node *node, *next_node;
15160   dw_die_ref die = 0;
15161
15162   /* Add the name for the main input file now.  We delayed this from
15163      dwarf2out_init to avoid complications with PCH.  */
15164   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15165   if (!IS_ABSOLUTE_PATH (filename))
15166     add_comp_dir_attribute (comp_unit_die);
15167   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15168     {
15169       bool p = false;
15170       htab_traverse (file_table, file_table_relative_p, &p);
15171       if (p)
15172         add_comp_dir_attribute (comp_unit_die);
15173     }
15174
15175   /* Traverse the limbo die list, and add parent/child links.  The only
15176      dies without parents that should be here are concrete instances of
15177      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15178      For concrete instances, we can get the parent die from the abstract
15179      instance.  */
15180   for (node = limbo_die_list; node; node = next_node)
15181     {
15182       next_node = node->next;
15183       die = node->die;
15184
15185       if (die->die_parent == NULL)
15186         {
15187           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15188
15189           if (origin)
15190             add_child_die (origin->die_parent, die);
15191           else if (die == comp_unit_die)
15192             ;
15193           else if (errorcount > 0 || sorrycount > 0)
15194             /* It's OK to be confused by errors in the input.  */
15195             add_child_die (comp_unit_die, die);
15196           else
15197             {
15198               /* In certain situations, the lexical block containing a
15199                  nested function can be optimized away, which results
15200                  in the nested function die being orphaned.  Likewise
15201                  with the return type of that nested function.  Force
15202                  this to be a child of the containing function.
15203
15204                  It may happen that even the containing function got fully
15205                  inlined and optimized out.  In that case we are lost and
15206                  assign the empty child.  This should not be big issue as
15207                  the function is likely unreachable too.  */
15208               tree context = NULL_TREE;
15209
15210               gcc_assert (node->created_for);
15211
15212               if (DECL_P (node->created_for))
15213                 context = DECL_CONTEXT (node->created_for);
15214               else if (TYPE_P (node->created_for))
15215                 context = TYPE_CONTEXT (node->created_for);
15216
15217               gcc_assert (context
15218                           && (TREE_CODE (context) == FUNCTION_DECL
15219                               || TREE_CODE (context) == NAMESPACE_DECL));
15220
15221               origin = lookup_decl_die (context);
15222               if (origin)
15223                 add_child_die (origin, die);
15224               else
15225                 add_child_die (comp_unit_die, die);
15226             }
15227         }
15228     }
15229
15230   limbo_die_list = NULL;
15231
15232   /* Walk through the list of incomplete types again, trying once more to
15233      emit full debugging info for them.  */
15234   retry_incomplete_types ();
15235
15236   if (flag_eliminate_unused_debug_types)
15237     prune_unused_types ();
15238
15239   /* Generate separate CUs for each of the include files we've seen.
15240      They will go into limbo_die_list.  */
15241   if (flag_eliminate_dwarf2_dups)
15242     break_out_includes (comp_unit_die);
15243
15244   /* Traverse the DIE's and add add sibling attributes to those DIE's
15245      that have children.  */
15246   add_sibling_attributes (comp_unit_die);
15247   for (node = limbo_die_list; node; node = node->next)
15248     add_sibling_attributes (node->die);
15249
15250   /* Output a terminator label for the .text section.  */
15251   switch_to_section (text_section);
15252   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15253   if (flag_reorder_blocks_and_partition)
15254     {
15255       switch_to_section (unlikely_text_section ());
15256       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15257     }
15258
15259   /* We can only use the low/high_pc attributes if all of the code was
15260      in .text.  */
15261   if (!have_multiple_function_sections)
15262     {
15263       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15264       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15265     }
15266
15267   else
15268     {
15269       unsigned fde_idx = 0;
15270
15271       /* We need to give .debug_loc and .debug_ranges an appropriate
15272          "base address".  Use zero so that these addresses become
15273          absolute.  Historically, we've emitted the unexpected
15274          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15275          Emit both to give time for other tools to adapt.  */
15276       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15277       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15278
15279       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15280                          add_ranges_by_labels (text_section_label,
15281                                                text_end_label));
15282       if (flag_reorder_blocks_and_partition)
15283         add_ranges_by_labels (cold_text_section_label,
15284                               cold_end_label);
15285
15286       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15287         {
15288           dw_fde_ref fde = &fde_table[fde_idx];
15289
15290           if (fde->dw_fde_switched_sections)
15291             {
15292               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15293                                     fde->dw_fde_hot_section_end_label);
15294               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15295                                     fde->dw_fde_unlikely_section_end_label);
15296             }
15297           else
15298             add_ranges_by_labels (fde->dw_fde_begin,
15299                                   fde->dw_fde_end);
15300         }
15301
15302       add_ranges (NULL);
15303     }
15304
15305   /* Output location list section if necessary.  */
15306   if (have_location_lists)
15307     {
15308       /* Output the location lists info.  */
15309       switch_to_section (debug_loc_section);
15310       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15311                                    DEBUG_LOC_SECTION_LABEL, 0);
15312       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15313       output_location_lists (die);
15314     }
15315
15316   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15317     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15318                     debug_line_section_label);
15319
15320   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15321     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15322
15323   /* Output all of the compilation units.  We put the main one last so that
15324      the offsets are available to output_pubnames.  */
15325   for (node = limbo_die_list; node; node = node->next)
15326     output_comp_unit (node->die, 0);
15327
15328   output_comp_unit (comp_unit_die, 0);
15329
15330   /* Output the abbreviation table.  */
15331   switch_to_section (debug_abbrev_section);
15332   output_abbrev_section ();
15333
15334   /* Output public names table if necessary.  */
15335   if (!VEC_empty (pubname_entry, pubname_table))
15336     {
15337       switch_to_section (debug_pubnames_section);
15338       output_pubnames (pubname_table);
15339     }
15340
15341 #ifdef DEBUG_PUBTYPES_SECTION
15342   /* Output public types table if necessary.  */
15343   if (!VEC_empty (pubname_entry, pubtype_table))
15344     {
15345       switch_to_section (debug_pubtypes_section);
15346       output_pubnames (pubtype_table);
15347     }
15348 #endif
15349
15350   /* Output the address range information.  We only put functions in the arange
15351      table, so don't write it out if we don't have any.  */
15352   if (fde_table_in_use)
15353     {
15354       switch_to_section (debug_aranges_section);
15355       output_aranges ();
15356     }
15357
15358   /* Output ranges section if necessary.  */
15359   if (ranges_table_in_use)
15360     {
15361       switch_to_section (debug_ranges_section);
15362       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15363       output_ranges ();
15364     }
15365
15366   /* Output the source line correspondence table.  We must do this
15367      even if there is no line information.  Otherwise, on an empty
15368      translation unit, we will generate a present, but empty,
15369      .debug_info section.  IRIX 6.5 `nm' will then complain when
15370      examining the file.  This is done late so that any filenames
15371      used by the debug_info section are marked as 'used'.  */
15372   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15373     {
15374       switch_to_section (debug_line_section);
15375       output_line_info ();
15376     }
15377
15378   /* Have to end the macro section.  */
15379   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15380     {
15381       switch_to_section (debug_macinfo_section);
15382       dw2_asm_output_data (1, 0, "End compilation unit");
15383     }
15384
15385   /* If we emitted any DW_FORM_strp form attribute, output the string
15386      table too.  */
15387   if (debug_str_hash)
15388     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15389 }
15390 #else
15391
15392 /* This should never be used, but its address is needed for comparisons.  */
15393 const struct gcc_debug_hooks dwarf2_debug_hooks;
15394
15395 #endif /* DWARF2_DEBUGGING_INFO */
15396
15397 #include "gt-dwarf2out.h"