OSDN Git Service

2007-06-05 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007 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 2, 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 COPYING.  If not, write to the Free
22 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 02110-1301, USA.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "version.h"
43 #include "flags.h"
44 #include "real.h"
45 #include "rtl.h"
46 #include "hard-reg-set.h"
47 #include "regs.h"
48 #include "insn-config.h"
49 #include "reload.h"
50 #include "function.h"
51 #include "output.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "except.h"
55 #include "dwarf2.h"
56 #include "dwarf2out.h"
57 #include "dwarf2asm.h"
58 #include "toplev.h"
59 #include "varray.h"
60 #include "ggc.h"
61 #include "md5.h"
62 #include "tm_p.h"
63 #include "diagnostic.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "hashtab.h"
68 #include "cgraph.h"
69 #include "input.h"
70
71 #ifdef DWARF2_DEBUGGING_INFO
72 static void dwarf2out_source_line (unsigned int, const char *);
73 #endif
74
75 /* DWARF2 Abbreviation Glossary:
76    CFA = Canonical Frame Address
77            a fixed address on the stack which identifies a call frame.
78            We define it to be the value of SP just before the call insn.
79            The CFA register and offset, which may change during the course
80            of the function, are used to calculate its value at runtime.
81    CFI = Call Frame Instruction
82            an instruction for the DWARF2 abstract machine
83    CIE = Common Information Entry
84            information describing information common to one or more FDEs
85    DIE = Debugging Information Entry
86    FDE = Frame Description Entry
87            information describing the stack call frame, in particular,
88            how to restore registers
89
90    DW_CFA_... = DWARF2 CFA call frame instruction
91    DW_TAG_... = DWARF2 DIE tag */
92
93 #ifndef DWARF2_FRAME_INFO
94 # ifdef DWARF2_DEBUGGING_INFO
95 #  define DWARF2_FRAME_INFO \
96   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
97 # else
98 #  define DWARF2_FRAME_INFO 0
99 # endif
100 #endif
101
102 /* Map register numbers held in the call frame info that gcc has
103    collected using DWARF_FRAME_REGNUM to those that should be output in
104    .debug_frame and .eh_frame.  */
105 #ifndef DWARF2_FRAME_REG_OUT
106 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
107 #endif
108
109 /* Decide whether we want to emit frame unwind information for the current
110    translation unit.  */
111
112 int
113 dwarf2out_do_frame (void)
114 {
115   /* We want to emit correct CFA location expressions or lists, so we
116      have to return true if we're going to output debug info, even if
117      we're not going to output frame or unwind info.  */
118   return (write_symbols == DWARF2_DEBUG
119           || write_symbols == VMS_AND_DWARF2_DEBUG
120           || DWARF2_FRAME_INFO
121 #ifdef DWARF2_UNWIND_INFO
122           || (DWARF2_UNWIND_INFO
123               && (flag_unwind_tables
124                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
125 #endif
126           );
127 }
128
129 /* The size of the target's pointer type.  */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
133
134 /* Array of RTXes referenced by the debugging information, which therefore
135    must be kept around forever.  */
136 static GTY(()) VEC(rtx,gc) *used_rtx_array;
137
138 /* A pointer to the base of a list of incomplete types which might be
139    completed at some later time.  incomplete_types_list needs to be a
140    VEC(tree,gc) because we want to tell the garbage collector about
141    it.  */
142 static GTY(()) VEC(tree,gc) *incomplete_types;
143
144 /* A pointer to the base of a table of references to declaration
145    scopes.  This table is a display which tracks the nesting
146    of declaration scopes at the current scope and containing
147    scopes.  This table is used to find the proper place to
148    define type declaration DIE's.  */
149 static GTY(()) VEC(tree,gc) *decl_scope_table;
150
151 /* Pointers to various DWARF2 sections.  */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_abbrev_section;
154 static GTY(()) section *debug_aranges_section;
155 static GTY(()) section *debug_macinfo_section;
156 static GTY(()) section *debug_line_section;
157 static GTY(()) section *debug_loc_section;
158 static GTY(()) section *debug_pubnames_section;
159 static GTY(()) section *debug_pubtypes_section;
160 static GTY(()) section *debug_str_section;
161 static GTY(()) section *debug_ranges_section;
162 static GTY(()) section *debug_frame_section;
163
164 /* How to start an assembler comment.  */
165 #ifndef ASM_COMMENT_START
166 #define ASM_COMMENT_START ";#"
167 #endif
168
169 typedef struct dw_cfi_struct *dw_cfi_ref;
170 typedef struct dw_fde_struct *dw_fde_ref;
171 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
172
173 /* Call frames are described using a sequence of Call Frame
174    Information instructions.  The register number, offset
175    and address fields are provided as possible operands;
176    their use is selected by the opcode field.  */
177
178 enum dw_cfi_oprnd_type {
179   dw_cfi_oprnd_unused,
180   dw_cfi_oprnd_reg_num,
181   dw_cfi_oprnd_offset,
182   dw_cfi_oprnd_addr,
183   dw_cfi_oprnd_loc
184 };
185
186 typedef union dw_cfi_oprnd_struct GTY(())
187 {
188   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
189   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
190   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
191   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
192 }
193 dw_cfi_oprnd;
194
195 typedef struct dw_cfi_struct GTY(())
196 {
197   dw_cfi_ref dw_cfi_next;
198   enum dwarf_call_frame_info dw_cfi_opc;
199   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
200     dw_cfi_oprnd1;
201   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
202     dw_cfi_oprnd2;
203 }
204 dw_cfi_node;
205
206 /* This is how we define the location of the CFA. We use to handle it
207    as REG + OFFSET all the time,  but now it can be more complex.
208    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
209    Instead of passing around REG and OFFSET, we pass a copy
210    of this structure.  */
211 typedef struct cfa_loc GTY(())
212 {
213   HOST_WIDE_INT offset;
214   HOST_WIDE_INT base_offset;
215   unsigned int reg;
216   int indirect;            /* 1 if CFA is accessed via a dereference.  */
217 } dw_cfa_location;
218
219 /* All call frame descriptions (FDE's) in the GCC generated DWARF
220    refer to a single Common Information Entry (CIE), defined at
221    the beginning of the .debug_frame section.  This use of a single
222    CIE obviates the need to keep track of multiple CIE's
223    in the DWARF generation routines below.  */
224
225 typedef struct dw_fde_struct GTY(())
226 {
227   tree decl;
228   const char *dw_fde_begin;
229   const char *dw_fde_current_label;
230   const char *dw_fde_end;
231   const char *dw_fde_hot_section_label;
232   const char *dw_fde_hot_section_end_label;
233   const char *dw_fde_unlikely_section_label;
234   const char *dw_fde_unlikely_section_end_label;
235   bool dw_fde_switched_sections;
236   dw_cfi_ref dw_fde_cfi;
237   unsigned funcdef_number;
238   unsigned all_throwers_are_sibcalls : 1;
239   unsigned nothrow : 1;
240   unsigned uses_eh_lsda : 1;
241 }
242 dw_fde_node;
243
244 /* Maximum size (in bytes) of an artificially generated label.  */
245 #define MAX_ARTIFICIAL_LABEL_BYTES      30
246
247 /* The size of addresses as they appear in the Dwarf 2 data.
248    Some architectures use word addresses to refer to code locations,
249    but Dwarf 2 info always uses byte addresses.  On such machines,
250    Dwarf 2 addresses need to be larger than the architecture's
251    pointers.  */
252 #ifndef DWARF2_ADDR_SIZE
253 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
254 #endif
255
256 /* The size in bytes of a DWARF field indicating an offset or length
257    relative to a debug info section, specified to be 4 bytes in the
258    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
259    as PTR_SIZE.  */
260
261 #ifndef DWARF_OFFSET_SIZE
262 #define DWARF_OFFSET_SIZE 4
263 #endif
264
265 /* According to the (draft) DWARF 3 specification, the initial length
266    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
267    bytes are 0xffffffff, followed by the length stored in the next 8
268    bytes.
269
270    However, the SGI/MIPS ABI uses an initial length which is equal to
271    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
272
273 #ifndef DWARF_INITIAL_LENGTH_SIZE
274 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
275 #endif
276
277 #define DWARF_VERSION 2
278
279 /* Round SIZE up to the nearest BOUNDARY.  */
280 #define DWARF_ROUND(SIZE,BOUNDARY) \
281   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
282
283 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
284 #ifndef DWARF_CIE_DATA_ALIGNMENT
285 #ifdef STACK_GROWS_DOWNWARD
286 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
287 #else
288 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
289 #endif
290 #endif
291
292 /* CIE identifier.  */
293 #if HOST_BITS_PER_WIDE_INT >= 64
294 #define DWARF_CIE_ID \
295   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
296 #else
297 #define DWARF_CIE_ID DW_CIE_ID
298 #endif
299
300 /* A pointer to the base of a table that contains frame description
301    information for each routine.  */
302 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
303
304 /* Number of elements currently allocated for fde_table.  */
305 static GTY(()) unsigned fde_table_allocated;
306
307 /* Number of elements in fde_table currently in use.  */
308 static GTY(()) unsigned fde_table_in_use;
309
310 /* Size (in elements) of increments by which we may expand the
311    fde_table.  */
312 #define FDE_TABLE_INCREMENT 256
313
314 /* A list of call frame insns for the CIE.  */
315 static GTY(()) dw_cfi_ref cie_cfi_head;
316
317 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
318 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
319    attribute that accelerates the lookup of the FDE associated
320    with the subprogram.  This variable holds the table index of the FDE
321    associated with the current function (body) definition.  */
322 static unsigned current_funcdef_fde;
323 #endif
324
325 struct indirect_string_node GTY(())
326 {
327   const char *str;
328   unsigned int refcount;
329   unsigned int form;
330   char *label;
331 };
332
333 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
334
335 static GTY(()) int dw2_string_counter;
336 static GTY(()) unsigned long dwarf2out_cfi_label_num;
337
338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
339
340 /* Forward declarations for functions defined in this file.  */
341
342 static char *stripattributes (const char *);
343 static const char *dwarf_cfi_name (unsigned);
344 static dw_cfi_ref new_cfi (void);
345 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
346 static void add_fde_cfi (const char *, dw_cfi_ref);
347 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
348 static void lookup_cfa (dw_cfa_location *);
349 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
350 static void initial_return_save (rtx);
351 static HOST_WIDE_INT stack_adjust_offset (rtx);
352 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
353 static void output_call_frame_info (int);
354 static void dwarf2out_stack_adjust (rtx, bool);
355 static void flush_queued_reg_saves (void);
356 static bool clobbers_queued_reg_save (rtx);
357 static void dwarf2out_frame_debug_expr (rtx, const char *);
358
359 /* Support for complex CFA locations.  */
360 static void output_cfa_loc (dw_cfi_ref);
361 static void get_cfa_from_loc_descr (dw_cfa_location *,
362                                     struct dw_loc_descr_struct *);
363 static struct dw_loc_descr_struct *build_cfa_loc
364   (dw_cfa_location *, HOST_WIDE_INT);
365 static void def_cfa_1 (const char *, dw_cfa_location *);
366
367 /* How to start an assembler comment.  */
368 #ifndef ASM_COMMENT_START
369 #define ASM_COMMENT_START ";#"
370 #endif
371
372 /* Data and reference forms for relocatable data.  */
373 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
374 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
375
376 #ifndef DEBUG_FRAME_SECTION
377 #define DEBUG_FRAME_SECTION     ".debug_frame"
378 #endif
379
380 #ifndef FUNC_BEGIN_LABEL
381 #define FUNC_BEGIN_LABEL        "LFB"
382 #endif
383
384 #ifndef FUNC_END_LABEL
385 #define FUNC_END_LABEL          "LFE"
386 #endif
387
388 #ifndef FRAME_BEGIN_LABEL
389 #define FRAME_BEGIN_LABEL       "Lframe"
390 #endif
391 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
392 #define CIE_END_LABEL           "LECIE"
393 #define FDE_LABEL               "LSFDE"
394 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
395 #define FDE_END_LABEL           "LEFDE"
396 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
397 #define LINE_NUMBER_END_LABEL   "LELT"
398 #define LN_PROLOG_AS_LABEL      "LASLTP"
399 #define LN_PROLOG_END_LABEL     "LELTP"
400 #define DIE_LABEL_PREFIX        "DW"
401
402 /* The DWARF 2 CFA column which tracks the return address.  Normally this
403    is the column for PC, or the first column after all of the hard
404    registers.  */
405 #ifndef DWARF_FRAME_RETURN_COLUMN
406 #ifdef PC_REGNUM
407 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
408 #else
409 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
410 #endif
411 #endif
412
413 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
414    default, we just provide columns for all registers.  */
415 #ifndef DWARF_FRAME_REGNUM
416 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
417 #endif
418 \f
419 /* Hook used by __throw.  */
420
421 rtx
422 expand_builtin_dwarf_sp_column (void)
423 {
424   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
425   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
426 }
427
428 /* Return a pointer to a copy of the section string name S with all
429    attributes stripped off, and an asterisk prepended (for assemble_name).  */
430
431 static inline char *
432 stripattributes (const char *s)
433 {
434   char *stripped = XNEWVEC (char, strlen (s) + 2);
435   char *p = stripped;
436
437   *p++ = '*';
438
439   while (*s && *s != ',')
440     *p++ = *s++;
441
442   *p = '\0';
443   return stripped;
444 }
445
446 /* MEM is a memory reference for the register size table, each element of
447    which has mode MODE.  Initialize column C as a return address column.  */
448
449 static void
450 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
451 {
452   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
453   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
454   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
455 }
456
457 /* Generate code to initialize the register size table.  */
458
459 void
460 expand_builtin_init_dwarf_reg_sizes (tree address)
461 {
462   unsigned int i;
463   enum machine_mode mode = TYPE_MODE (char_type_node);
464   rtx addr = expand_normal (address);
465   rtx mem = gen_rtx_MEM (BLKmode, addr);
466   bool wrote_return_column = false;
467
468   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
469     {
470       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
471
472       if (rnum < DWARF_FRAME_REGISTERS)
473         {
474           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
475           enum machine_mode save_mode = reg_raw_mode[i];
476           HOST_WIDE_INT size;
477
478           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
479             save_mode = choose_hard_reg_mode (i, 1, true);
480           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
481             {
482               if (save_mode == VOIDmode)
483                 continue;
484               wrote_return_column = true;
485             }
486           size = GET_MODE_SIZE (save_mode);
487           if (offset < 0)
488             continue;
489
490           emit_move_insn (adjust_address (mem, mode, offset),
491                           gen_int_mode (size, mode));
492         }
493     }
494
495   if (!wrote_return_column)
496     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
497
498 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
499   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
500 #endif
501
502   targetm.init_dwarf_reg_sizes_extra (address);
503 }
504
505 /* Convert a DWARF call frame info. operation to its string name */
506
507 static const char *
508 dwarf_cfi_name (unsigned int cfi_opc)
509 {
510   switch (cfi_opc)
511     {
512     case DW_CFA_advance_loc:
513       return "DW_CFA_advance_loc";
514     case DW_CFA_offset:
515       return "DW_CFA_offset";
516     case DW_CFA_restore:
517       return "DW_CFA_restore";
518     case DW_CFA_nop:
519       return "DW_CFA_nop";
520     case DW_CFA_set_loc:
521       return "DW_CFA_set_loc";
522     case DW_CFA_advance_loc1:
523       return "DW_CFA_advance_loc1";
524     case DW_CFA_advance_loc2:
525       return "DW_CFA_advance_loc2";
526     case DW_CFA_advance_loc4:
527       return "DW_CFA_advance_loc4";
528     case DW_CFA_offset_extended:
529       return "DW_CFA_offset_extended";
530     case DW_CFA_restore_extended:
531       return "DW_CFA_restore_extended";
532     case DW_CFA_undefined:
533       return "DW_CFA_undefined";
534     case DW_CFA_same_value:
535       return "DW_CFA_same_value";
536     case DW_CFA_register:
537       return "DW_CFA_register";
538     case DW_CFA_remember_state:
539       return "DW_CFA_remember_state";
540     case DW_CFA_restore_state:
541       return "DW_CFA_restore_state";
542     case DW_CFA_def_cfa:
543       return "DW_CFA_def_cfa";
544     case DW_CFA_def_cfa_register:
545       return "DW_CFA_def_cfa_register";
546     case DW_CFA_def_cfa_offset:
547       return "DW_CFA_def_cfa_offset";
548
549     /* DWARF 3 */
550     case DW_CFA_def_cfa_expression:
551       return "DW_CFA_def_cfa_expression";
552     case DW_CFA_expression:
553       return "DW_CFA_expression";
554     case DW_CFA_offset_extended_sf:
555       return "DW_CFA_offset_extended_sf";
556     case DW_CFA_def_cfa_sf:
557       return "DW_CFA_def_cfa_sf";
558     case DW_CFA_def_cfa_offset_sf:
559       return "DW_CFA_def_cfa_offset_sf";
560
561     /* SGI/MIPS specific */
562     case DW_CFA_MIPS_advance_loc8:
563       return "DW_CFA_MIPS_advance_loc8";
564
565     /* GNU extensions */
566     case DW_CFA_GNU_window_save:
567       return "DW_CFA_GNU_window_save";
568     case DW_CFA_GNU_args_size:
569       return "DW_CFA_GNU_args_size";
570     case DW_CFA_GNU_negative_offset_extended:
571       return "DW_CFA_GNU_negative_offset_extended";
572
573     default:
574       return "DW_CFA_<unknown>";
575     }
576 }
577
578 /* Return a pointer to a newly allocated Call Frame Instruction.  */
579
580 static inline dw_cfi_ref
581 new_cfi (void)
582 {
583   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
584
585   cfi->dw_cfi_next = NULL;
586   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
587   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
588
589   return cfi;
590 }
591
592 /* Add a Call Frame Instruction to list of instructions.  */
593
594 static inline void
595 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
596 {
597   dw_cfi_ref *p;
598
599   /* Find the end of the chain.  */
600   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
601     ;
602
603   *p = cfi;
604 }
605
606 /* Generate a new label for the CFI info to refer to.  */
607
608 char *
609 dwarf2out_cfi_label (void)
610 {
611   static char label[20];
612
613   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
614   ASM_OUTPUT_LABEL (asm_out_file, label);
615   return label;
616 }
617
618 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
619    or to the CIE if LABEL is NULL.  */
620
621 static void
622 add_fde_cfi (const char *label, dw_cfi_ref cfi)
623 {
624   if (label)
625     {
626       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
627
628       if (*label == 0)
629         label = dwarf2out_cfi_label ();
630
631       if (fde->dw_fde_current_label == NULL
632           || strcmp (label, fde->dw_fde_current_label) != 0)
633         {
634           dw_cfi_ref xcfi;
635
636           label = xstrdup (label);
637
638           /* Set the location counter to the new label.  */
639           xcfi = new_cfi ();
640           /* If we have a current label, advance from there, otherwise
641              set the location directly using set_loc.  */
642           xcfi->dw_cfi_opc = fde->dw_fde_current_label
643                              ? DW_CFA_advance_loc4
644                              : DW_CFA_set_loc;
645           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
646           add_cfi (&fde->dw_fde_cfi, xcfi);
647
648           fde->dw_fde_current_label = label;
649         }
650
651       add_cfi (&fde->dw_fde_cfi, cfi);
652     }
653
654   else
655     add_cfi (&cie_cfi_head, cfi);
656 }
657
658 /* Subroutine of lookup_cfa.  */
659
660 static void
661 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
662 {
663   switch (cfi->dw_cfi_opc)
664     {
665     case DW_CFA_def_cfa_offset:
666       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
667       break;
668     case DW_CFA_def_cfa_offset_sf:
669       loc->offset
670         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
671       break;
672     case DW_CFA_def_cfa_register:
673       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
674       break;
675     case DW_CFA_def_cfa:
676       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
677       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
678       break;
679     case DW_CFA_def_cfa_sf:
680       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
681       loc->offset
682         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
683       break;
684     case DW_CFA_def_cfa_expression:
685       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
686       break;
687     default:
688       break;
689     }
690 }
691
692 /* Find the previous value for the CFA.  */
693
694 static void
695 lookup_cfa (dw_cfa_location *loc)
696 {
697   dw_cfi_ref cfi;
698
699   loc->reg = INVALID_REGNUM;
700   loc->offset = 0;
701   loc->indirect = 0;
702   loc->base_offset = 0;
703
704   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
705     lookup_cfa_1 (cfi, loc);
706
707   if (fde_table_in_use)
708     {
709       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
710       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
711         lookup_cfa_1 (cfi, loc);
712     }
713 }
714
715 /* The current rule for calculating the DWARF2 canonical frame address.  */
716 static dw_cfa_location cfa;
717
718 /* The register used for saving registers to the stack, and its offset
719    from the CFA.  */
720 static dw_cfa_location cfa_store;
721
722 /* The running total of the size of arguments pushed onto the stack.  */
723 static HOST_WIDE_INT args_size;
724
725 /* The last args_size we actually output.  */
726 static HOST_WIDE_INT old_args_size;
727
728 /* Entry point to update the canonical frame address (CFA).
729    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
730    calculated from REG+OFFSET.  */
731
732 void
733 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
734 {
735   dw_cfa_location loc;
736   loc.indirect = 0;
737   loc.base_offset = 0;
738   loc.reg = reg;
739   loc.offset = offset;
740   def_cfa_1 (label, &loc);
741 }
742
743 /* Determine if two dw_cfa_location structures define the same data.  */
744
745 static bool
746 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
747 {
748   return (loc1->reg == loc2->reg
749           && loc1->offset == loc2->offset
750           && loc1->indirect == loc2->indirect
751           && (loc1->indirect == 0
752               || loc1->base_offset == loc2->base_offset));
753 }
754
755 /* This routine does the actual work.  The CFA is now calculated from
756    the dw_cfa_location structure.  */
757
758 static void
759 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
760 {
761   dw_cfi_ref cfi;
762   dw_cfa_location old_cfa, loc;
763
764   cfa = *loc_p;
765   loc = *loc_p;
766
767   if (cfa_store.reg == loc.reg && loc.indirect == 0)
768     cfa_store.offset = loc.offset;
769
770   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
771   lookup_cfa (&old_cfa);
772
773   /* If nothing changed, no need to issue any call frame instructions.  */
774   if (cfa_equal_p (&loc, &old_cfa))
775     return;
776
777   cfi = new_cfi ();
778
779   if (loc.reg == old_cfa.reg && !loc.indirect)
780     {
781       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
782          the CFA register did not change but the offset did.  */
783       if (loc.offset < 0)
784         {
785           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
786           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
787
788           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
789           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
790         }
791       else
792         {
793           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
794           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
795         }
796     }
797
798 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
799   else if (loc.offset == old_cfa.offset
800            && old_cfa.reg != INVALID_REGNUM
801            && !loc.indirect)
802     {
803       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
804          indicating the CFA register has changed to <register> but the
805          offset has not changed.  */
806       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
807       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
808     }
809 #endif
810
811   else if (loc.indirect == 0)
812     {
813       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
814          indicating the CFA register has changed to <register> with
815          the specified offset.  */
816       if (loc.offset < 0)
817         {
818           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
819           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
820
821           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
822           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
823           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
824         }
825       else
826         {
827           cfi->dw_cfi_opc = DW_CFA_def_cfa;
828           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
830         }
831     }
832   else
833     {
834       /* Construct a DW_CFA_def_cfa_expression instruction to
835          calculate the CFA using a full location expression since no
836          register-offset pair is available.  */
837       struct dw_loc_descr_struct *loc_list;
838
839       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
840       loc_list = build_cfa_loc (&loc, 0);
841       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
842     }
843
844   add_fde_cfi (label, cfi);
845 }
846
847 /* Add the CFI for saving a register.  REG is the CFA column number.
848    LABEL is passed to add_fde_cfi.
849    If SREG is -1, the register is saved at OFFSET from the CFA;
850    otherwise it is saved in SREG.  */
851
852 static void
853 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
854 {
855   dw_cfi_ref cfi = new_cfi ();
856
857   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
858
859   if (sreg == INVALID_REGNUM)
860     {
861       if (reg & ~0x3f)
862         /* The register number won't fit in 6 bits, so we have to use
863            the long form.  */
864         cfi->dw_cfi_opc = DW_CFA_offset_extended;
865       else
866         cfi->dw_cfi_opc = DW_CFA_offset;
867
868 #ifdef ENABLE_CHECKING
869       {
870         /* If we get an offset that is not a multiple of
871            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
872            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
873            description.  */
874         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
875
876         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
877       }
878 #endif
879       offset /= DWARF_CIE_DATA_ALIGNMENT;
880       if (offset < 0)
881         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
882
883       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
884     }
885   else if (sreg == reg)
886     cfi->dw_cfi_opc = DW_CFA_same_value;
887   else
888     {
889       cfi->dw_cfi_opc = DW_CFA_register;
890       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
891     }
892
893   add_fde_cfi (label, cfi);
894 }
895
896 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
897    This CFI tells the unwinder that it needs to restore the window registers
898    from the previous frame's window save area.
899
900    ??? Perhaps we should note in the CIE where windows are saved (instead of
901    assuming 0(cfa)) and what registers are in the window.  */
902
903 void
904 dwarf2out_window_save (const char *label)
905 {
906   dw_cfi_ref cfi = new_cfi ();
907
908   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
909   add_fde_cfi (label, cfi);
910 }
911
912 /* Add a CFI to update the running total of the size of arguments
913    pushed onto the stack.  */
914
915 void
916 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
917 {
918   dw_cfi_ref cfi;
919
920   if (size == old_args_size)
921     return;
922
923   old_args_size = size;
924
925   cfi = new_cfi ();
926   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
927   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
928   add_fde_cfi (label, cfi);
929 }
930
931 /* Entry point for saving a register to the stack.  REG is the GCC register
932    number.  LABEL and OFFSET are passed to reg_save.  */
933
934 void
935 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
936 {
937   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
938 }
939
940 /* Entry point for saving the return address in the stack.
941    LABEL and OFFSET are passed to reg_save.  */
942
943 void
944 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
945 {
946   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
947 }
948
949 /* Entry point for saving the return address in a register.
950    LABEL and SREG are passed to reg_save.  */
951
952 void
953 dwarf2out_return_reg (const char *label, unsigned int sreg)
954 {
955   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
956 }
957
958 /* Record the initial position of the return address.  RTL is
959    INCOMING_RETURN_ADDR_RTX.  */
960
961 static void
962 initial_return_save (rtx rtl)
963 {
964   unsigned int reg = INVALID_REGNUM;
965   HOST_WIDE_INT offset = 0;
966
967   switch (GET_CODE (rtl))
968     {
969     case REG:
970       /* RA is in a register.  */
971       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
972       break;
973
974     case MEM:
975       /* RA is on the stack.  */
976       rtl = XEXP (rtl, 0);
977       switch (GET_CODE (rtl))
978         {
979         case REG:
980           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
981           offset = 0;
982           break;
983
984         case PLUS:
985           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
986           offset = INTVAL (XEXP (rtl, 1));
987           break;
988
989         case MINUS:
990           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
991           offset = -INTVAL (XEXP (rtl, 1));
992           break;
993
994         default:
995           gcc_unreachable ();
996         }
997
998       break;
999
1000     case PLUS:
1001       /* The return address is at some offset from any value we can
1002          actually load.  For instance, on the SPARC it is in %i7+8. Just
1003          ignore the offset for now; it doesn't matter for unwinding frames.  */
1004       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1005       initial_return_save (XEXP (rtl, 0));
1006       return;
1007
1008     default:
1009       gcc_unreachable ();
1010     }
1011
1012   if (reg != DWARF_FRAME_RETURN_COLUMN)
1013     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1014 }
1015
1016 /* Given a SET, calculate the amount of stack adjustment it
1017    contains.  */
1018
1019 static HOST_WIDE_INT
1020 stack_adjust_offset (rtx pattern)
1021 {
1022   rtx src = SET_SRC (pattern);
1023   rtx dest = SET_DEST (pattern);
1024   HOST_WIDE_INT offset = 0;
1025   enum rtx_code code;
1026
1027   if (dest == stack_pointer_rtx)
1028     {
1029       /* (set (reg sp) (plus (reg sp) (const_int))) */
1030       code = GET_CODE (src);
1031       if (! (code == PLUS || code == MINUS)
1032           || XEXP (src, 0) != stack_pointer_rtx
1033           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1034         return 0;
1035
1036       offset = INTVAL (XEXP (src, 1));
1037       if (code == PLUS)
1038         offset = -offset;
1039     }
1040   else if (MEM_P (dest))
1041     {
1042       /* (set (mem (pre_dec (reg sp))) (foo)) */
1043       src = XEXP (dest, 0);
1044       code = GET_CODE (src);
1045
1046       switch (code)
1047         {
1048         case PRE_MODIFY:
1049         case POST_MODIFY:
1050           if (XEXP (src, 0) == stack_pointer_rtx)
1051             {
1052               rtx val = XEXP (XEXP (src, 1), 1);
1053               /* We handle only adjustments by constant amount.  */
1054               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1055                           && GET_CODE (val) == CONST_INT);
1056               offset = -INTVAL (val);
1057               break;
1058             }
1059           return 0;
1060
1061         case PRE_DEC:
1062         case POST_DEC:
1063           if (XEXP (src, 0) == stack_pointer_rtx)
1064             {
1065               offset = GET_MODE_SIZE (GET_MODE (dest));
1066               break;
1067             }
1068           return 0;
1069
1070         case PRE_INC:
1071         case POST_INC:
1072           if (XEXP (src, 0) == stack_pointer_rtx)
1073             {
1074               offset = -GET_MODE_SIZE (GET_MODE (dest));
1075               break;
1076             }
1077           return 0;
1078
1079         default:
1080           return 0;
1081         }
1082     }
1083   else
1084     return 0;
1085
1086   return offset;
1087 }
1088
1089 /* Check INSN to see if it looks like a push or a stack adjustment, and
1090    make a note of it if it does.  EH uses this information to find out how
1091    much extra space it needs to pop off the stack.  */
1092
1093 static void
1094 dwarf2out_stack_adjust (rtx insn, bool after_p)
1095 {
1096   HOST_WIDE_INT offset;
1097   const char *label;
1098   int i;
1099
1100   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1101      with this function.  Proper support would require all frame-related
1102      insns to be marked, and to be able to handle saving state around
1103      epilogues textually in the middle of the function.  */
1104   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1105     return;
1106
1107   /* If only calls can throw, and we have a frame pointer,
1108      save up adjustments until we see the CALL_INSN.  */
1109   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1110     {
1111       if (CALL_P (insn) && !after_p)
1112         {
1113           /* Extract the size of the args from the CALL rtx itself.  */
1114           insn = PATTERN (insn);
1115           if (GET_CODE (insn) == PARALLEL)
1116             insn = XVECEXP (insn, 0, 0);
1117           if (GET_CODE (insn) == SET)
1118             insn = SET_SRC (insn);
1119           gcc_assert (GET_CODE (insn) == CALL);
1120           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1121         }
1122       return;
1123     }
1124
1125   if (CALL_P (insn) && !after_p)
1126     {
1127       if (!flag_asynchronous_unwind_tables)
1128         dwarf2out_args_size ("", args_size);
1129       return;
1130     }
1131   else if (BARRIER_P (insn))
1132     {
1133       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1134          the compiler will have already emitted a stack adjustment, but
1135          doesn't bother for calls to noreturn functions.  */
1136 #ifdef STACK_GROWS_DOWNWARD
1137       offset = -args_size;
1138 #else
1139       offset = args_size;
1140 #endif
1141     }
1142   else if (GET_CODE (PATTERN (insn)) == SET)
1143     offset = stack_adjust_offset (PATTERN (insn));
1144   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1145            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1146     {
1147       /* There may be stack adjustments inside compound insns.  Search
1148          for them.  */
1149       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1150         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1151           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1152     }
1153   else
1154     return;
1155
1156   if (offset == 0)
1157     return;
1158
1159   if (cfa.reg == STACK_POINTER_REGNUM)
1160     cfa.offset += offset;
1161
1162 #ifndef STACK_GROWS_DOWNWARD
1163   offset = -offset;
1164 #endif
1165
1166   args_size += offset;
1167   if (args_size < 0)
1168     args_size = 0;
1169
1170   label = dwarf2out_cfi_label ();
1171   def_cfa_1 (label, &cfa);
1172   if (flag_asynchronous_unwind_tables)
1173     dwarf2out_args_size (label, args_size);
1174 }
1175
1176 #endif
1177
1178 /* We delay emitting a register save until either (a) we reach the end
1179    of the prologue or (b) the register is clobbered.  This clusters
1180    register saves so that there are fewer pc advances.  */
1181
1182 struct queued_reg_save GTY(())
1183 {
1184   struct queued_reg_save *next;
1185   rtx reg;
1186   HOST_WIDE_INT cfa_offset;
1187   rtx saved_reg;
1188 };
1189
1190 static GTY(()) struct queued_reg_save *queued_reg_saves;
1191
1192 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1193 struct reg_saved_in_data GTY(()) {
1194   rtx orig_reg;
1195   rtx saved_in_reg;
1196 };
1197
1198 /* A list of registers saved in other registers.
1199    The list intentionally has a small maximum capacity of 4; if your
1200    port needs more than that, you might consider implementing a
1201    more efficient data structure.  */
1202 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1203 static GTY(()) size_t num_regs_saved_in_regs;
1204
1205 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1206 static const char *last_reg_save_label;
1207
1208 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1209    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1210
1211 static void
1212 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1213 {
1214   struct queued_reg_save *q;
1215
1216   /* Duplicates waste space, but it's also necessary to remove them
1217      for correctness, since the queue gets output in reverse
1218      order.  */
1219   for (q = queued_reg_saves; q != NULL; q = q->next)
1220     if (REGNO (q->reg) == REGNO (reg))
1221       break;
1222
1223   if (q == NULL)
1224     {
1225       q = ggc_alloc (sizeof (*q));
1226       q->next = queued_reg_saves;
1227       queued_reg_saves = q;
1228     }
1229
1230   q->reg = reg;
1231   q->cfa_offset = offset;
1232   q->saved_reg = sreg;
1233
1234   last_reg_save_label = label;
1235 }
1236
1237 /* Output all the entries in QUEUED_REG_SAVES.  */
1238
1239 static void
1240 flush_queued_reg_saves (void)
1241 {
1242   struct queued_reg_save *q;
1243
1244   for (q = queued_reg_saves; q; q = q->next)
1245     {
1246       size_t i;
1247       unsigned int reg, sreg;
1248
1249       for (i = 0; i < num_regs_saved_in_regs; i++)
1250         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1251           break;
1252       if (q->saved_reg && i == num_regs_saved_in_regs)
1253         {
1254           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1255           num_regs_saved_in_regs++;
1256         }
1257       if (i != num_regs_saved_in_regs)
1258         {
1259           regs_saved_in_regs[i].orig_reg = q->reg;
1260           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1261         }
1262
1263       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1264       if (q->saved_reg)
1265         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1266       else
1267         sreg = INVALID_REGNUM;
1268       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1269     }
1270
1271   queued_reg_saves = NULL;
1272   last_reg_save_label = NULL;
1273 }
1274
1275 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1276    location for?  Or, does it clobber a register which we've previously
1277    said that some other register is saved in, and for which we now
1278    have a new location for?  */
1279
1280 static bool
1281 clobbers_queued_reg_save (rtx insn)
1282 {
1283   struct queued_reg_save *q;
1284
1285   for (q = queued_reg_saves; q; q = q->next)
1286     {
1287       size_t i;
1288       if (modified_in_p (q->reg, insn))
1289         return true;
1290       for (i = 0; i < num_regs_saved_in_regs; i++)
1291         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1292             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1293           return true;
1294     }
1295
1296   return false;
1297 }
1298
1299 /* Entry point for saving the first register into the second.  */
1300
1301 void
1302 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1303 {
1304   size_t i;
1305   unsigned int regno, sregno;
1306
1307   for (i = 0; i < num_regs_saved_in_regs; i++)
1308     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1309       break;
1310   if (i == num_regs_saved_in_regs)
1311     {
1312       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1313       num_regs_saved_in_regs++;
1314     }
1315   regs_saved_in_regs[i].orig_reg = reg;
1316   regs_saved_in_regs[i].saved_in_reg = sreg;
1317
1318   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1319   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1320   reg_save (label, regno, sregno, 0);
1321 }
1322
1323 /* What register, if any, is currently saved in REG?  */
1324
1325 static rtx
1326 reg_saved_in (rtx reg)
1327 {
1328   unsigned int regn = REGNO (reg);
1329   size_t i;
1330   struct queued_reg_save *q;
1331
1332   for (q = queued_reg_saves; q; q = q->next)
1333     if (q->saved_reg && regn == REGNO (q->saved_reg))
1334       return q->reg;
1335
1336   for (i = 0; i < num_regs_saved_in_regs; i++)
1337     if (regs_saved_in_regs[i].saved_in_reg
1338         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1339       return regs_saved_in_regs[i].orig_reg;
1340
1341   return NULL_RTX;
1342 }
1343
1344
1345 /* A temporary register holding an integral value used in adjusting SP
1346    or setting up the store_reg.  The "offset" field holds the integer
1347    value, not an offset.  */
1348 static dw_cfa_location cfa_temp;
1349
1350 /* Record call frame debugging information for an expression EXPR,
1351    which either sets SP or FP (adjusting how we calculate the frame
1352    address) or saves a register to the stack or another register.
1353    LABEL indicates the address of EXPR.
1354
1355    This function encodes a state machine mapping rtxes to actions on
1356    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1357    users need not read the source code.
1358
1359   The High-Level Picture
1360
1361   Changes in the register we use to calculate the CFA: Currently we
1362   assume that if you copy the CFA register into another register, we
1363   should take the other one as the new CFA register; this seems to
1364   work pretty well.  If it's wrong for some target, it's simple
1365   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1366
1367   Changes in the register we use for saving registers to the stack:
1368   This is usually SP, but not always.  Again, we deduce that if you
1369   copy SP into another register (and SP is not the CFA register),
1370   then the new register is the one we will be using for register
1371   saves.  This also seems to work.
1372
1373   Register saves: There's not much guesswork about this one; if
1374   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1375   register save, and the register used to calculate the destination
1376   had better be the one we think we're using for this purpose.
1377   It's also assumed that a copy from a call-saved register to another
1378   register is saving that register if RTX_FRAME_RELATED_P is set on
1379   that instruction.  If the copy is from a call-saved register to
1380   the *same* register, that means that the register is now the same
1381   value as in the caller.
1382
1383   Except: If the register being saved is the CFA register, and the
1384   offset is nonzero, we are saving the CFA, so we assume we have to
1385   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1386   the intent is to save the value of SP from the previous frame.
1387
1388   In addition, if a register has previously been saved to a different
1389   register,
1390
1391   Invariants / Summaries of Rules
1392
1393   cfa          current rule for calculating the CFA.  It usually
1394                consists of a register and an offset.
1395   cfa_store    register used by prologue code to save things to the stack
1396                cfa_store.offset is the offset from the value of
1397                cfa_store.reg to the actual CFA
1398   cfa_temp     register holding an integral value.  cfa_temp.offset
1399                stores the value, which will be used to adjust the
1400                stack pointer.  cfa_temp is also used like cfa_store,
1401                to track stores to the stack via fp or a temp reg.
1402
1403   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1404                with cfa.reg as the first operand changes the cfa.reg and its
1405                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1406                cfa_temp.offset.
1407
1408   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1409                expression yielding a constant.  This sets cfa_temp.reg
1410                and cfa_temp.offset.
1411
1412   Rule 5:      Create a new register cfa_store used to save items to the
1413                stack.
1414
1415   Rules 10-14: Save a register to the stack.  Define offset as the
1416                difference of the original location and cfa_store's
1417                location (or cfa_temp's location if cfa_temp is used).
1418
1419   The Rules
1420
1421   "{a,b}" indicates a choice of a xor b.
1422   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1423
1424   Rule 1:
1425   (set <reg1> <reg2>:cfa.reg)
1426   effects: cfa.reg = <reg1>
1427            cfa.offset unchanged
1428            cfa_temp.reg = <reg1>
1429            cfa_temp.offset = cfa.offset
1430
1431   Rule 2:
1432   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1433                               {<const_int>,<reg>:cfa_temp.reg}))
1434   effects: cfa.reg = sp if fp used
1435            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1436            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1437              if cfa_store.reg==sp
1438
1439   Rule 3:
1440   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1441   effects: cfa.reg = fp
1442            cfa_offset += +/- <const_int>
1443
1444   Rule 4:
1445   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1446   constraints: <reg1> != fp
1447                <reg1> != sp
1448   effects: cfa.reg = <reg1>
1449            cfa_temp.reg = <reg1>
1450            cfa_temp.offset = cfa.offset
1451
1452   Rule 5:
1453   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1454   constraints: <reg1> != fp
1455                <reg1> != sp
1456   effects: cfa_store.reg = <reg1>
1457            cfa_store.offset = cfa.offset - cfa_temp.offset
1458
1459   Rule 6:
1460   (set <reg> <const_int>)
1461   effects: cfa_temp.reg = <reg>
1462            cfa_temp.offset = <const_int>
1463
1464   Rule 7:
1465   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1466   effects: cfa_temp.reg = <reg1>
1467            cfa_temp.offset |= <const_int>
1468
1469   Rule 8:
1470   (set <reg> (high <exp>))
1471   effects: none
1472
1473   Rule 9:
1474   (set <reg> (lo_sum <exp> <const_int>))
1475   effects: cfa_temp.reg = <reg>
1476            cfa_temp.offset = <const_int>
1477
1478   Rule 10:
1479   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1480   effects: cfa_store.offset -= <const_int>
1481            cfa.offset = cfa_store.offset if cfa.reg == sp
1482            cfa.reg = sp
1483            cfa.base_offset = -cfa_store.offset
1484
1485   Rule 11:
1486   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1487   effects: cfa_store.offset += -/+ mode_size(mem)
1488            cfa.offset = cfa_store.offset if cfa.reg == sp
1489            cfa.reg = sp
1490            cfa.base_offset = -cfa_store.offset
1491
1492   Rule 12:
1493   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1494
1495        <reg2>)
1496   effects: cfa.reg = <reg1>
1497            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1498
1499   Rule 13:
1500   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1501   effects: cfa.reg = <reg1>
1502            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1503
1504   Rule 14:
1505   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1506   effects: cfa.reg = <reg1>
1507            cfa.base_offset = -cfa_temp.offset
1508            cfa_temp.offset -= mode_size(mem)
1509
1510   Rule 15:
1511   (set <reg> {unspec, unspec_volatile})
1512   effects: target-dependent  */
1513
1514 static void
1515 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1516 {
1517   rtx src, dest;
1518   HOST_WIDE_INT offset;
1519
1520   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1521      the PARALLEL independently. The first element is always processed if
1522      it is a SET. This is for backward compatibility.   Other elements
1523      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1524      flag is set in them.  */
1525   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1526     {
1527       int par_index;
1528       int limit = XVECLEN (expr, 0);
1529       rtx elem;
1530
1531       /* PARALLELs have strict read-modify-write semantics, so we
1532          ought to evaluate every rvalue before changing any lvalue.
1533          It's cumbersome to do that in general, but there's an
1534          easy approximation that is enough for all current users:
1535          handle register saves before register assignments.  */
1536       if (GET_CODE (expr) == PARALLEL)
1537         for (par_index = 0; par_index < limit; par_index++)
1538           {
1539             elem = XVECEXP (expr, 0, par_index);
1540             if (GET_CODE (elem) == SET
1541                 && MEM_P (SET_DEST (elem))
1542                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1543               dwarf2out_frame_debug_expr (elem, label);
1544           }
1545
1546       for (par_index = 0; par_index < limit; par_index++)
1547         {
1548           elem = XVECEXP (expr, 0, par_index);
1549           if (GET_CODE (elem) == SET
1550               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1551               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1552             dwarf2out_frame_debug_expr (elem, label);
1553         }
1554       return;
1555     }
1556
1557   gcc_assert (GET_CODE (expr) == SET);
1558
1559   src = SET_SRC (expr);
1560   dest = SET_DEST (expr);
1561
1562   if (REG_P (src))
1563     {
1564       rtx rsi = reg_saved_in (src);
1565       if (rsi)
1566         src = rsi;
1567     }
1568
1569   switch (GET_CODE (dest))
1570     {
1571     case REG:
1572       switch (GET_CODE (src))
1573         {
1574           /* Setting FP from SP.  */
1575         case REG:
1576           if (cfa.reg == (unsigned) REGNO (src))
1577             {
1578               /* Rule 1 */
1579               /* Update the CFA rule wrt SP or FP.  Make sure src is
1580                  relative to the current CFA register.
1581
1582                  We used to require that dest be either SP or FP, but the
1583                  ARM copies SP to a temporary register, and from there to
1584                  FP.  So we just rely on the backends to only set
1585                  RTX_FRAME_RELATED_P on appropriate insns.  */
1586               cfa.reg = REGNO (dest);
1587               cfa_temp.reg = cfa.reg;
1588               cfa_temp.offset = cfa.offset;
1589             }
1590           else
1591             {
1592               /* Saving a register in a register.  */
1593               gcc_assert (!fixed_regs [REGNO (dest)]
1594                           /* For the SPARC and its register window.  */
1595                           || (DWARF_FRAME_REGNUM (REGNO (src))
1596                               == DWARF_FRAME_RETURN_COLUMN));
1597               queue_reg_save (label, src, dest, 0);
1598             }
1599           break;
1600
1601         case PLUS:
1602         case MINUS:
1603         case LO_SUM:
1604           if (dest == stack_pointer_rtx)
1605             {
1606               /* Rule 2 */
1607               /* Adjusting SP.  */
1608               switch (GET_CODE (XEXP (src, 1)))
1609                 {
1610                 case CONST_INT:
1611                   offset = INTVAL (XEXP (src, 1));
1612                   break;
1613                 case REG:
1614                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1615                               == cfa_temp.reg);
1616                   offset = cfa_temp.offset;
1617                   break;
1618                 default:
1619                   gcc_unreachable ();
1620                 }
1621
1622               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1623                 {
1624                   /* Restoring SP from FP in the epilogue.  */
1625                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1626                   cfa.reg = STACK_POINTER_REGNUM;
1627                 }
1628               else if (GET_CODE (src) == LO_SUM)
1629                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1630                 ;
1631               else
1632                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1633
1634               if (GET_CODE (src) != MINUS)
1635                 offset = -offset;
1636               if (cfa.reg == STACK_POINTER_REGNUM)
1637                 cfa.offset += offset;
1638               if (cfa_store.reg == STACK_POINTER_REGNUM)
1639                 cfa_store.offset += offset;
1640             }
1641           else if (dest == hard_frame_pointer_rtx)
1642             {
1643               /* Rule 3 */
1644               /* Either setting the FP from an offset of the SP,
1645                  or adjusting the FP */
1646               gcc_assert (frame_pointer_needed);
1647
1648               gcc_assert (REG_P (XEXP (src, 0))
1649                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1650                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1651               offset = INTVAL (XEXP (src, 1));
1652               if (GET_CODE (src) != MINUS)
1653                 offset = -offset;
1654               cfa.offset += offset;
1655               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1656             }
1657           else
1658             {
1659               gcc_assert (GET_CODE (src) != MINUS);
1660
1661               /* Rule 4 */
1662               if (REG_P (XEXP (src, 0))
1663                   && REGNO (XEXP (src, 0)) == cfa.reg
1664                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1665                 {
1666                   /* Setting a temporary CFA register that will be copied
1667                      into the FP later on.  */
1668                   offset = - INTVAL (XEXP (src, 1));
1669                   cfa.offset += offset;
1670                   cfa.reg = REGNO (dest);
1671                   /* Or used to save regs to the stack.  */
1672                   cfa_temp.reg = cfa.reg;
1673                   cfa_temp.offset = cfa.offset;
1674                 }
1675
1676               /* Rule 5 */
1677               else if (REG_P (XEXP (src, 0))
1678                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1679                        && XEXP (src, 1) == stack_pointer_rtx)
1680                 {
1681                   /* Setting a scratch register that we will use instead
1682                      of SP for saving registers to the stack.  */
1683                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1684                   cfa_store.reg = REGNO (dest);
1685                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1686                 }
1687
1688               /* Rule 9 */
1689               else if (GET_CODE (src) == LO_SUM
1690                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1691                 {
1692                   cfa_temp.reg = REGNO (dest);
1693                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1694                 }
1695               else
1696                 gcc_unreachable ();
1697             }
1698           break;
1699
1700           /* Rule 6 */
1701         case CONST_INT:
1702           cfa_temp.reg = REGNO (dest);
1703           cfa_temp.offset = INTVAL (src);
1704           break;
1705
1706           /* Rule 7 */
1707         case IOR:
1708           gcc_assert (REG_P (XEXP (src, 0))
1709                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1710                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1711
1712           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1713             cfa_temp.reg = REGNO (dest);
1714           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1715           break;
1716
1717           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1718              which will fill in all of the bits.  */
1719           /* Rule 8 */
1720         case HIGH:
1721           break;
1722
1723           /* Rule 15 */
1724         case UNSPEC:
1725         case UNSPEC_VOLATILE:
1726           gcc_assert (targetm.dwarf_handle_frame_unspec);
1727           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1728           return;
1729
1730         default:
1731           gcc_unreachable ();
1732         }
1733
1734       def_cfa_1 (label, &cfa);
1735       break;
1736
1737     case MEM:
1738       gcc_assert (REG_P (src));
1739
1740       /* Saving a register to the stack.  Make sure dest is relative to the
1741          CFA register.  */
1742       switch (GET_CODE (XEXP (dest, 0)))
1743         {
1744           /* Rule 10 */
1745           /* With a push.  */
1746         case PRE_MODIFY:
1747           /* We can't handle variable size modifications.  */
1748           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1749                       == CONST_INT);
1750           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1751
1752           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1753                       && cfa_store.reg == STACK_POINTER_REGNUM);
1754
1755           cfa_store.offset += offset;
1756           if (cfa.reg == STACK_POINTER_REGNUM)
1757             cfa.offset = cfa_store.offset;
1758
1759           offset = -cfa_store.offset;
1760           break;
1761
1762           /* Rule 11 */
1763         case PRE_INC:
1764         case PRE_DEC:
1765           offset = GET_MODE_SIZE (GET_MODE (dest));
1766           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1767             offset = -offset;
1768
1769           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1770                       && cfa_store.reg == STACK_POINTER_REGNUM);
1771
1772           cfa_store.offset += offset;
1773           if (cfa.reg == STACK_POINTER_REGNUM)
1774             cfa.offset = cfa_store.offset;
1775
1776           offset = -cfa_store.offset;
1777           break;
1778
1779           /* Rule 12 */
1780           /* With an offset.  */
1781         case PLUS:
1782         case MINUS:
1783         case LO_SUM:
1784           {
1785             int regno;
1786
1787             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1788                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1789             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1790             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1791               offset = -offset;
1792
1793             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1794
1795             if (cfa_store.reg == (unsigned) regno)
1796               offset -= cfa_store.offset;
1797             else
1798               {
1799                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1800                 offset -= cfa_temp.offset;
1801               }
1802           }
1803           break;
1804
1805           /* Rule 13 */
1806           /* Without an offset.  */
1807         case REG:
1808           {
1809             int regno = REGNO (XEXP (dest, 0));
1810
1811             if (cfa_store.reg == (unsigned) regno)
1812               offset = -cfa_store.offset;
1813             else
1814               {
1815                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1816                 offset = -cfa_temp.offset;
1817               }
1818           }
1819           break;
1820
1821           /* Rule 14 */
1822         case POST_INC:
1823           gcc_assert (cfa_temp.reg
1824                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1825           offset = -cfa_temp.offset;
1826           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1827           break;
1828
1829         default:
1830           gcc_unreachable ();
1831         }
1832
1833       if (REGNO (src) != STACK_POINTER_REGNUM
1834           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1835           && (unsigned) REGNO (src) == cfa.reg)
1836         {
1837           /* We're storing the current CFA reg into the stack.  */
1838
1839           if (cfa.offset == 0)
1840             {
1841               /* If the source register is exactly the CFA, assume
1842                  we're saving SP like any other register; this happens
1843                  on the ARM.  */
1844               def_cfa_1 (label, &cfa);
1845               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1846               break;
1847             }
1848           else
1849             {
1850               /* Otherwise, we'll need to look in the stack to
1851                  calculate the CFA.  */
1852               rtx x = XEXP (dest, 0);
1853
1854               if (!REG_P (x))
1855                 x = XEXP (x, 0);
1856               gcc_assert (REG_P (x));
1857
1858               cfa.reg = REGNO (x);
1859               cfa.base_offset = offset;
1860               cfa.indirect = 1;
1861               def_cfa_1 (label, &cfa);
1862               break;
1863             }
1864         }
1865
1866       def_cfa_1 (label, &cfa);
1867       queue_reg_save (label, src, NULL_RTX, offset);
1868       break;
1869
1870     default:
1871       gcc_unreachable ();
1872     }
1873 }
1874
1875 /* Record call frame debugging information for INSN, which either
1876    sets SP or FP (adjusting how we calculate the frame address) or saves a
1877    register to the stack.  If INSN is NULL_RTX, initialize our state.
1878
1879    If AFTER_P is false, we're being called before the insn is emitted,
1880    otherwise after.  Call instructions get invoked twice.  */
1881
1882 void
1883 dwarf2out_frame_debug (rtx insn, bool after_p)
1884 {
1885   const char *label;
1886   rtx src;
1887
1888   if (insn == NULL_RTX)
1889     {
1890       size_t i;
1891
1892       /* Flush any queued register saves.  */
1893       flush_queued_reg_saves ();
1894
1895       /* Set up state for generating call frame debug info.  */
1896       lookup_cfa (&cfa);
1897       gcc_assert (cfa.reg
1898                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1899
1900       cfa.reg = STACK_POINTER_REGNUM;
1901       cfa_store = cfa;
1902       cfa_temp.reg = -1;
1903       cfa_temp.offset = 0;
1904
1905       for (i = 0; i < num_regs_saved_in_regs; i++)
1906         {
1907           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1908           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1909         }
1910       num_regs_saved_in_regs = 0;
1911       return;
1912     }
1913
1914   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1915     flush_queued_reg_saves ();
1916
1917   if (! RTX_FRAME_RELATED_P (insn))
1918     {
1919       if (!ACCUMULATE_OUTGOING_ARGS)
1920         dwarf2out_stack_adjust (insn, after_p);
1921       return;
1922     }
1923
1924   label = dwarf2out_cfi_label ();
1925   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1926   if (src)
1927     insn = XEXP (src, 0);
1928   else
1929     insn = PATTERN (insn);
1930
1931   dwarf2out_frame_debug_expr (insn, label);
1932 }
1933
1934 #endif
1935
1936 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1937 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1938  (enum dwarf_call_frame_info cfi);
1939
1940 static enum dw_cfi_oprnd_type
1941 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1942 {
1943   switch (cfi)
1944     {
1945     case DW_CFA_nop:
1946     case DW_CFA_GNU_window_save:
1947       return dw_cfi_oprnd_unused;
1948
1949     case DW_CFA_set_loc:
1950     case DW_CFA_advance_loc1:
1951     case DW_CFA_advance_loc2:
1952     case DW_CFA_advance_loc4:
1953     case DW_CFA_MIPS_advance_loc8:
1954       return dw_cfi_oprnd_addr;
1955
1956     case DW_CFA_offset:
1957     case DW_CFA_offset_extended:
1958     case DW_CFA_def_cfa:
1959     case DW_CFA_offset_extended_sf:
1960     case DW_CFA_def_cfa_sf:
1961     case DW_CFA_restore_extended:
1962     case DW_CFA_undefined:
1963     case DW_CFA_same_value:
1964     case DW_CFA_def_cfa_register:
1965     case DW_CFA_register:
1966       return dw_cfi_oprnd_reg_num;
1967
1968     case DW_CFA_def_cfa_offset:
1969     case DW_CFA_GNU_args_size:
1970     case DW_CFA_def_cfa_offset_sf:
1971       return dw_cfi_oprnd_offset;
1972
1973     case DW_CFA_def_cfa_expression:
1974     case DW_CFA_expression:
1975       return dw_cfi_oprnd_loc;
1976
1977     default:
1978       gcc_unreachable ();
1979     }
1980 }
1981
1982 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1983 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1984  (enum dwarf_call_frame_info cfi);
1985
1986 static enum dw_cfi_oprnd_type
1987 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1988 {
1989   switch (cfi)
1990     {
1991     case DW_CFA_def_cfa:
1992     case DW_CFA_def_cfa_sf:
1993     case DW_CFA_offset:
1994     case DW_CFA_offset_extended_sf:
1995     case DW_CFA_offset_extended:
1996       return dw_cfi_oprnd_offset;
1997
1998     case DW_CFA_register:
1999       return dw_cfi_oprnd_reg_num;
2000
2001     default:
2002       return dw_cfi_oprnd_unused;
2003     }
2004 }
2005
2006 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2007
2008 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2009    switch to the data section instead, and write out a synthetic label
2010    for collect2.  */
2011
2012 static void
2013 switch_to_eh_frame_section (void)
2014 {
2015   tree label;
2016
2017 #ifdef EH_FRAME_SECTION_NAME
2018   if (eh_frame_section == 0)
2019     {
2020       int flags;
2021
2022       if (EH_TABLES_CAN_BE_READ_ONLY)
2023         {
2024           int fde_encoding;
2025           int per_encoding;
2026           int lsda_encoding;
2027
2028           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2029                                                        /*global=*/0);
2030           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2031                                                        /*global=*/1);
2032           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2033                                                         /*global=*/0);
2034           flags = ((! flag_pic
2035                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2036                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2037                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2038                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2039                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2040                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2041                    ? 0 : SECTION_WRITE);
2042         }
2043       else
2044         flags = SECTION_WRITE;
2045       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2046     }
2047 #endif
2048
2049   if (eh_frame_section)
2050     switch_to_section (eh_frame_section);
2051   else
2052     {
2053       /* We have no special eh_frame section.  Put the information in
2054          the data section and emit special labels to guide collect2.  */
2055       switch_to_section (data_section);
2056       label = get_file_function_name ("F");
2057       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2058       targetm.asm_out.globalize_label (asm_out_file,
2059                                        IDENTIFIER_POINTER (label));
2060       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2061     }
2062 }
2063
2064 /* Output a Call Frame Information opcode and its operand(s).  */
2065
2066 static void
2067 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2068 {
2069   unsigned long r;
2070   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2071     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2072                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2073                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2074                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
2075   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2076     {
2077       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2078       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2079                            "DW_CFA_offset, column 0x%lx", r);
2080       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2081     }
2082   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2083     {
2084       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2085       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2086                            "DW_CFA_restore, column 0x%lx", r);
2087     }
2088   else
2089     {
2090       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2091                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2092
2093       switch (cfi->dw_cfi_opc)
2094         {
2095         case DW_CFA_set_loc:
2096           if (for_eh)
2097             dw2_asm_output_encoded_addr_rtx (
2098                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2099                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2100                 false, NULL);
2101           else
2102             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2103                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2104           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2105           break;
2106
2107         case DW_CFA_advance_loc1:
2108           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2109                                 fde->dw_fde_current_label, NULL);
2110           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2111           break;
2112
2113         case DW_CFA_advance_loc2:
2114           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2115                                 fde->dw_fde_current_label, NULL);
2116           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2117           break;
2118
2119         case DW_CFA_advance_loc4:
2120           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2121                                 fde->dw_fde_current_label, NULL);
2122           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2123           break;
2124
2125         case DW_CFA_MIPS_advance_loc8:
2126           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2127                                 fde->dw_fde_current_label, NULL);
2128           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2129           break;
2130
2131         case DW_CFA_offset_extended:
2132         case DW_CFA_def_cfa:
2133           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2134           dw2_asm_output_data_uleb128 (r, NULL);
2135           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2136           break;
2137
2138         case DW_CFA_offset_extended_sf:
2139         case DW_CFA_def_cfa_sf:
2140           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2141           dw2_asm_output_data_uleb128 (r, NULL);
2142           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2143           break;
2144
2145         case DW_CFA_restore_extended:
2146         case DW_CFA_undefined:
2147         case DW_CFA_same_value:
2148         case DW_CFA_def_cfa_register:
2149           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2150           dw2_asm_output_data_uleb128 (r, NULL);
2151           break;
2152
2153         case DW_CFA_register:
2154           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2155           dw2_asm_output_data_uleb128 (r, NULL);
2156           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2157           dw2_asm_output_data_uleb128 (r, NULL);
2158           break;
2159
2160         case DW_CFA_def_cfa_offset:
2161         case DW_CFA_GNU_args_size:
2162           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2163           break;
2164
2165         case DW_CFA_def_cfa_offset_sf:
2166           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2167           break;
2168
2169         case DW_CFA_GNU_window_save:
2170           break;
2171
2172         case DW_CFA_def_cfa_expression:
2173         case DW_CFA_expression:
2174           output_cfa_loc (cfi);
2175           break;
2176
2177         case DW_CFA_GNU_negative_offset_extended:
2178           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2179           gcc_unreachable ();
2180
2181         default:
2182           break;
2183         }
2184     }
2185 }
2186
2187 /* Output the call frame information used to record information
2188    that relates to calculating the frame pointer, and records the
2189    location of saved registers.  */
2190
2191 static void
2192 output_call_frame_info (int for_eh)
2193 {
2194   unsigned int i;
2195   dw_fde_ref fde;
2196   dw_cfi_ref cfi;
2197   char l1[20], l2[20], section_start_label[20];
2198   bool any_lsda_needed = false;
2199   char augmentation[6];
2200   int augmentation_size;
2201   int fde_encoding = DW_EH_PE_absptr;
2202   int per_encoding = DW_EH_PE_absptr;
2203   int lsda_encoding = DW_EH_PE_absptr;
2204   int return_reg;
2205
2206   /* Don't emit a CIE if there won't be any FDEs.  */
2207   if (fde_table_in_use == 0)
2208     return;
2209
2210   /* If we make FDEs linkonce, we may have to emit an empty label for
2211      an FDE that wouldn't otherwise be emitted.  We want to avoid
2212      having an FDE kept around when the function it refers to is
2213      discarded.  Example where this matters: a primary function
2214      template in C++ requires EH information, but an explicit
2215      specialization doesn't.  */
2216   if (TARGET_USES_WEAK_UNWIND_INFO
2217       && ! flag_asynchronous_unwind_tables
2218       && for_eh)
2219     for (i = 0; i < fde_table_in_use; i++)
2220       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2221           && !fde_table[i].uses_eh_lsda
2222           && ! DECL_WEAK (fde_table[i].decl))
2223         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2224                                       for_eh, /* empty */ 1);
2225
2226   /* If we don't have any functions we'll want to unwind out of, don't
2227      emit any EH unwind information.  Note that if exceptions aren't
2228      enabled, we won't have collected nothrow information, and if we
2229      asked for asynchronous tables, we always want this info.  */
2230   if (for_eh)
2231     {
2232       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2233
2234       for (i = 0; i < fde_table_in_use; i++)
2235         if (fde_table[i].uses_eh_lsda)
2236           any_eh_needed = any_lsda_needed = true;
2237         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2238           any_eh_needed = true;
2239         else if (! fde_table[i].nothrow
2240                  && ! fde_table[i].all_throwers_are_sibcalls)
2241           any_eh_needed = true;
2242
2243       if (! any_eh_needed)
2244         return;
2245     }
2246
2247   /* We're going to be generating comments, so turn on app.  */
2248   if (flag_debug_asm)
2249     app_enable ();
2250
2251   if (for_eh)
2252     switch_to_eh_frame_section ();
2253   else
2254     {
2255       if (!debug_frame_section)
2256         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2257                                            SECTION_DEBUG, NULL);
2258       switch_to_section (debug_frame_section);
2259     }
2260
2261   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2262   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2263
2264   /* Output the CIE.  */
2265   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2266   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2267   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2268     dw2_asm_output_data (4, 0xffffffff,
2269       "Initial length escape value indicating 64-bit DWARF extension");
2270   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2271                         "Length of Common Information Entry");
2272   ASM_OUTPUT_LABEL (asm_out_file, l1);
2273
2274   /* Now that the CIE pointer is PC-relative for EH,
2275      use 0 to identify the CIE.  */
2276   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2277                        (for_eh ? 0 : DWARF_CIE_ID),
2278                        "CIE Identifier Tag");
2279
2280   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2281
2282   augmentation[0] = 0;
2283   augmentation_size = 0;
2284   if (for_eh)
2285     {
2286       char *p;
2287
2288       /* Augmentation:
2289          z      Indicates that a uleb128 is present to size the
2290                 augmentation section.
2291          L      Indicates the encoding (and thus presence) of
2292                 an LSDA pointer in the FDE augmentation.
2293          R      Indicates a non-default pointer encoding for
2294                 FDE code pointers.
2295          P      Indicates the presence of an encoding + language
2296                 personality routine in the CIE augmentation.  */
2297
2298       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2299       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2300       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2301
2302       p = augmentation + 1;
2303       if (eh_personality_libfunc)
2304         {
2305           *p++ = 'P';
2306           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2307           assemble_external_libcall (eh_personality_libfunc);
2308         }
2309       if (any_lsda_needed)
2310         {
2311           *p++ = 'L';
2312           augmentation_size += 1;
2313         }
2314       if (fde_encoding != DW_EH_PE_absptr)
2315         {
2316           *p++ = 'R';
2317           augmentation_size += 1;
2318         }
2319       if (p > augmentation + 1)
2320         {
2321           augmentation[0] = 'z';
2322           *p = '\0';
2323         }
2324
2325       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2326       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2327         {
2328           int offset = (  4             /* Length */
2329                         + 4             /* CIE Id */
2330                         + 1             /* CIE version */
2331                         + strlen (augmentation) + 1     /* Augmentation */
2332                         + size_of_uleb128 (1)           /* Code alignment */
2333                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2334                         + 1             /* RA column */
2335                         + 1             /* Augmentation size */
2336                         + 1             /* Personality encoding */ );
2337           int pad = -offset & (PTR_SIZE - 1);
2338
2339           augmentation_size += pad;
2340
2341           /* Augmentations should be small, so there's scarce need to
2342              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2343           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2344         }
2345     }
2346
2347   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2348   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2349   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2350                                "CIE Data Alignment Factor");
2351
2352   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2353   if (DW_CIE_VERSION == 1)
2354     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2355   else
2356     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2357
2358   if (augmentation[0])
2359     {
2360       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2361       if (eh_personality_libfunc)
2362         {
2363           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2364                                eh_data_format_name (per_encoding));
2365           dw2_asm_output_encoded_addr_rtx (per_encoding,
2366                                            eh_personality_libfunc,
2367                                            true, NULL);
2368         }
2369
2370       if (any_lsda_needed)
2371         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2372                              eh_data_format_name (lsda_encoding));
2373
2374       if (fde_encoding != DW_EH_PE_absptr)
2375         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2376                              eh_data_format_name (fde_encoding));
2377     }
2378
2379   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2380     output_cfi (cfi, NULL, for_eh);
2381
2382   /* Pad the CIE out to an address sized boundary.  */
2383   ASM_OUTPUT_ALIGN (asm_out_file,
2384                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2385   ASM_OUTPUT_LABEL (asm_out_file, l2);
2386
2387   /* Loop through all of the FDE's.  */
2388   for (i = 0; i < fde_table_in_use; i++)
2389     {
2390       fde = &fde_table[i];
2391
2392       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2393       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2394           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2395           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2396           && !fde->uses_eh_lsda)
2397         continue;
2398
2399       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2400       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2401       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2402       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2403       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2404         dw2_asm_output_data (4, 0xffffffff,
2405                              "Initial length escape value indicating 64-bit DWARF extension");
2406       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2407                             "FDE Length");
2408       ASM_OUTPUT_LABEL (asm_out_file, l1);
2409
2410       if (for_eh)
2411         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2412       else
2413         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2414                                debug_frame_section, "FDE CIE offset");
2415
2416       if (for_eh)
2417         {
2418           rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2419           SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2420           dw2_asm_output_encoded_addr_rtx (fde_encoding,
2421                                            sym_ref,
2422                                            false,
2423                                            "FDE initial location");
2424           if (fde->dw_fde_switched_sections)
2425             {
2426               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2427                                       fde->dw_fde_unlikely_section_label);
2428               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2429                                       fde->dw_fde_hot_section_label);
2430               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2431               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2432               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2433                                                "FDE initial location");
2434               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2435                                     fde->dw_fde_hot_section_end_label,
2436                                     fde->dw_fde_hot_section_label,
2437                                     "FDE address range");
2438               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2439                                                "FDE initial location");
2440               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2441                                     fde->dw_fde_unlikely_section_end_label,
2442                                     fde->dw_fde_unlikely_section_label,
2443                                     "FDE address range");
2444             }
2445           else
2446             dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2447                                   fde->dw_fde_end, fde->dw_fde_begin,
2448                                   "FDE address range");
2449         }
2450       else
2451         {
2452           dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2453                                "FDE initial location");
2454           if (fde->dw_fde_switched_sections)
2455             {
2456               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2457                                    fde->dw_fde_hot_section_label,
2458                                    "FDE initial location");
2459               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2460                                     fde->dw_fde_hot_section_end_label,
2461                                     fde->dw_fde_hot_section_label,
2462                                     "FDE address range");
2463               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2464                                    fde->dw_fde_unlikely_section_label,
2465                                    "FDE initial location");
2466               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2467                                     fde->dw_fde_unlikely_section_end_label,
2468                                     fde->dw_fde_unlikely_section_label,
2469                                     "FDE address range");
2470             }
2471           else
2472             dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2473                                   fde->dw_fde_end, fde->dw_fde_begin,
2474                                   "FDE address range");
2475         }
2476
2477       if (augmentation[0])
2478         {
2479           if (any_lsda_needed)
2480             {
2481               int size = size_of_encoded_value (lsda_encoding);
2482
2483               if (lsda_encoding == DW_EH_PE_aligned)
2484                 {
2485                   int offset = (  4             /* Length */
2486                                 + 4             /* CIE offset */
2487                                 + 2 * size_of_encoded_value (fde_encoding)
2488                                 + 1             /* Augmentation size */ );
2489                   int pad = -offset & (PTR_SIZE - 1);
2490
2491                   size += pad;
2492                   gcc_assert (size_of_uleb128 (size) == 1);
2493                 }
2494
2495               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2496
2497               if (fde->uses_eh_lsda)
2498                 {
2499                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2500                                                fde->funcdef_number);
2501                   dw2_asm_output_encoded_addr_rtx (
2502                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2503                         false, "Language Specific Data Area");
2504                 }
2505               else
2506                 {
2507                   if (lsda_encoding == DW_EH_PE_aligned)
2508                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2509                   dw2_asm_output_data
2510                     (size_of_encoded_value (lsda_encoding), 0,
2511                      "Language Specific Data Area (none)");
2512                 }
2513             }
2514           else
2515             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2516         }
2517
2518       /* Loop through the Call Frame Instructions associated with
2519          this FDE.  */
2520       fde->dw_fde_current_label = fde->dw_fde_begin;
2521       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2522         output_cfi (cfi, fde, for_eh);
2523
2524       /* Pad the FDE out to an address sized boundary.  */
2525       ASM_OUTPUT_ALIGN (asm_out_file,
2526                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2527       ASM_OUTPUT_LABEL (asm_out_file, l2);
2528     }
2529
2530   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2531     dw2_asm_output_data (4, 0, "End of Table");
2532 #ifdef MIPS_DEBUGGING_INFO
2533   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2534      get a value of 0.  Putting .align 0 after the label fixes it.  */
2535   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2536 #endif
2537
2538   /* Turn off app to make assembly quicker.  */
2539   if (flag_debug_asm)
2540     app_disable ();
2541 }
2542
2543 /* Output a marker (i.e. a label) for the beginning of a function, before
2544    the prologue.  */
2545
2546 void
2547 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2548                           const char *file ATTRIBUTE_UNUSED)
2549 {
2550   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2551   char * dup_label;
2552   dw_fde_ref fde;
2553
2554   current_function_func_begin_label = NULL;
2555
2556 #ifdef TARGET_UNWIND_INFO
2557   /* ??? current_function_func_begin_label is also used by except.c
2558      for call-site information.  We must emit this label if it might
2559      be used.  */
2560   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2561       && ! dwarf2out_do_frame ())
2562     return;
2563 #else
2564   if (! dwarf2out_do_frame ())
2565     return;
2566 #endif
2567
2568   switch_to_section (function_section (current_function_decl));
2569   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2570                                current_function_funcdef_no);
2571   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2572                           current_function_funcdef_no);
2573   dup_label = xstrdup (label);
2574   current_function_func_begin_label = dup_label;
2575
2576 #ifdef TARGET_UNWIND_INFO
2577   /* We can elide the fde allocation if we're not emitting debug info.  */
2578   if (! dwarf2out_do_frame ())
2579     return;
2580 #endif
2581
2582   /* Expand the fde table if necessary.  */
2583   if (fde_table_in_use == fde_table_allocated)
2584     {
2585       fde_table_allocated += FDE_TABLE_INCREMENT;
2586       fde_table = ggc_realloc (fde_table,
2587                                fde_table_allocated * sizeof (dw_fde_node));
2588       memset (fde_table + fde_table_in_use, 0,
2589               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2590     }
2591
2592   /* Record the FDE associated with this function.  */
2593   current_funcdef_fde = fde_table_in_use;
2594
2595   /* Add the new FDE at the end of the fde_table.  */
2596   fde = &fde_table[fde_table_in_use++];
2597   fde->decl = current_function_decl;
2598   fde->dw_fde_begin = dup_label;
2599   fde->dw_fde_current_label = dup_label;
2600   fde->dw_fde_hot_section_label = NULL;
2601   fde->dw_fde_hot_section_end_label = NULL;
2602   fde->dw_fde_unlikely_section_label = NULL;
2603   fde->dw_fde_unlikely_section_end_label = NULL;
2604   fde->dw_fde_switched_sections = false;
2605   fde->dw_fde_end = NULL;
2606   fde->dw_fde_cfi = NULL;
2607   fde->funcdef_number = current_function_funcdef_no;
2608   fde->nothrow = TREE_NOTHROW (current_function_decl);
2609   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2610   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2611
2612   args_size = old_args_size = 0;
2613
2614   /* We only want to output line number information for the genuine dwarf2
2615      prologue case, not the eh frame case.  */
2616 #ifdef DWARF2_DEBUGGING_INFO
2617   if (file)
2618     dwarf2out_source_line (line, file);
2619 #endif
2620 }
2621
2622 /* Output a marker (i.e. a label) for the absolute end of the generated code
2623    for a function definition.  This gets called *after* the epilogue code has
2624    been generated.  */
2625
2626 void
2627 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2628                         const char *file ATTRIBUTE_UNUSED)
2629 {
2630   dw_fde_ref fde;
2631   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2632
2633   /* Output a label to mark the endpoint of the code generated for this
2634      function.  */
2635   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2636                                current_function_funcdef_no);
2637   ASM_OUTPUT_LABEL (asm_out_file, label);
2638   fde = &fde_table[fde_table_in_use - 1];
2639   fde->dw_fde_end = xstrdup (label);
2640 }
2641
2642 void
2643 dwarf2out_frame_init (void)
2644 {
2645   /* Allocate the initial hunk of the fde_table.  */
2646   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2647   fde_table_allocated = FDE_TABLE_INCREMENT;
2648   fde_table_in_use = 0;
2649
2650   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2651      sake of lookup_cfa.  */
2652
2653   /* On entry, the Canonical Frame Address is at SP.  */
2654   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2655
2656 #ifdef DWARF2_UNWIND_INFO
2657   if (DWARF2_UNWIND_INFO)
2658     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2659 #endif
2660 }
2661
2662 void
2663 dwarf2out_frame_finish (void)
2664 {
2665   /* Output call frame information.  */
2666   if (DWARF2_FRAME_INFO)
2667     output_call_frame_info (0);
2668
2669 #ifndef TARGET_UNWIND_INFO
2670   /* Output another copy for the unwinder.  */
2671   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2672     output_call_frame_info (1);
2673 #endif
2674 }
2675 #endif
2676 \f
2677 /* And now, the subset of the debugging information support code necessary
2678    for emitting location expressions.  */
2679
2680 /* Data about a single source file.  */
2681 struct dwarf_file_data GTY(())
2682 {
2683   const char * filename;
2684   int emitted_number;
2685 };
2686
2687 /* We need some way to distinguish DW_OP_addr with a direct symbol
2688    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2689 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2690
2691
2692 typedef struct dw_val_struct *dw_val_ref;
2693 typedef struct die_struct *dw_die_ref;
2694 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2695 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2696
2697 /* Each DIE may have a series of attribute/value pairs.  Values
2698    can take on several forms.  The forms that are used in this
2699    implementation are listed below.  */
2700
2701 enum dw_val_class
2702 {
2703   dw_val_class_addr,
2704   dw_val_class_offset,
2705   dw_val_class_loc,
2706   dw_val_class_loc_list,
2707   dw_val_class_range_list,
2708   dw_val_class_const,
2709   dw_val_class_unsigned_const,
2710   dw_val_class_long_long,
2711   dw_val_class_vec,
2712   dw_val_class_flag,
2713   dw_val_class_die_ref,
2714   dw_val_class_fde_ref,
2715   dw_val_class_lbl_id,
2716   dw_val_class_lineptr,
2717   dw_val_class_str,
2718   dw_val_class_macptr,
2719   dw_val_class_file
2720 };
2721
2722 /* Describe a double word constant value.  */
2723 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2724
2725 typedef struct dw_long_long_struct GTY(())
2726 {
2727   unsigned long hi;
2728   unsigned long low;
2729 }
2730 dw_long_long_const;
2731
2732 /* Describe a floating point constant value, or a vector constant value.  */
2733
2734 typedef struct dw_vec_struct GTY(())
2735 {
2736   unsigned char * GTY((length ("%h.length"))) array;
2737   unsigned length;
2738   unsigned elt_size;
2739 }
2740 dw_vec_const;
2741
2742 /* The dw_val_node describes an attribute's value, as it is
2743    represented internally.  */
2744
2745 typedef struct dw_val_struct GTY(())
2746 {
2747   enum dw_val_class val_class;
2748   union dw_val_struct_union
2749     {
2750       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2751       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2752       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2753       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2754       HOST_WIDE_INT GTY ((default)) val_int;
2755       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2756       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2757       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2758       struct dw_val_die_union
2759         {
2760           dw_die_ref die;
2761           int external;
2762         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2763       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2764       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2765       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2766       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2767       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2768     }
2769   GTY ((desc ("%1.val_class"))) v;
2770 }
2771 dw_val_node;
2772
2773 /* Locations in memory are described using a sequence of stack machine
2774    operations.  */
2775
2776 typedef struct dw_loc_descr_struct GTY(())
2777 {
2778   dw_loc_descr_ref dw_loc_next;
2779   enum dwarf_location_atom dw_loc_opc;
2780   dw_val_node dw_loc_oprnd1;
2781   dw_val_node dw_loc_oprnd2;
2782   int dw_loc_addr;
2783 }
2784 dw_loc_descr_node;
2785
2786 /* Location lists are ranges + location descriptions for that range,
2787    so you can track variables that are in different places over
2788    their entire life.  */
2789 typedef struct dw_loc_list_struct GTY(())
2790 {
2791   dw_loc_list_ref dw_loc_next;
2792   const char *begin; /* Label for begin address of range */
2793   const char *end;  /* Label for end address of range */
2794   char *ll_symbol; /* Label for beginning of location list.
2795                       Only on head of list */
2796   const char *section; /* Section this loclist is relative to */
2797   dw_loc_descr_ref expr;
2798 } dw_loc_list_node;
2799
2800 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2801
2802 static const char *dwarf_stack_op_name (unsigned);
2803 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2804                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2805 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2806 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2807 static unsigned long size_of_locs (dw_loc_descr_ref);
2808 static void output_loc_operands (dw_loc_descr_ref);
2809 static void output_loc_sequence (dw_loc_descr_ref);
2810
2811 /* Convert a DWARF stack opcode into its string name.  */
2812
2813 static const char *
2814 dwarf_stack_op_name (unsigned int op)
2815 {
2816   switch (op)
2817     {
2818     case DW_OP_addr:
2819     case INTERNAL_DW_OP_tls_addr:
2820       return "DW_OP_addr";
2821     case DW_OP_deref:
2822       return "DW_OP_deref";
2823     case DW_OP_const1u:
2824       return "DW_OP_const1u";
2825     case DW_OP_const1s:
2826       return "DW_OP_const1s";
2827     case DW_OP_const2u:
2828       return "DW_OP_const2u";
2829     case DW_OP_const2s:
2830       return "DW_OP_const2s";
2831     case DW_OP_const4u:
2832       return "DW_OP_const4u";
2833     case DW_OP_const4s:
2834       return "DW_OP_const4s";
2835     case DW_OP_const8u:
2836       return "DW_OP_const8u";
2837     case DW_OP_const8s:
2838       return "DW_OP_const8s";
2839     case DW_OP_constu:
2840       return "DW_OP_constu";
2841     case DW_OP_consts:
2842       return "DW_OP_consts";
2843     case DW_OP_dup:
2844       return "DW_OP_dup";
2845     case DW_OP_drop:
2846       return "DW_OP_drop";
2847     case DW_OP_over:
2848       return "DW_OP_over";
2849     case DW_OP_pick:
2850       return "DW_OP_pick";
2851     case DW_OP_swap:
2852       return "DW_OP_swap";
2853     case DW_OP_rot:
2854       return "DW_OP_rot";
2855     case DW_OP_xderef:
2856       return "DW_OP_xderef";
2857     case DW_OP_abs:
2858       return "DW_OP_abs";
2859     case DW_OP_and:
2860       return "DW_OP_and";
2861     case DW_OP_div:
2862       return "DW_OP_div";
2863     case DW_OP_minus:
2864       return "DW_OP_minus";
2865     case DW_OP_mod:
2866       return "DW_OP_mod";
2867     case DW_OP_mul:
2868       return "DW_OP_mul";
2869     case DW_OP_neg:
2870       return "DW_OP_neg";
2871     case DW_OP_not:
2872       return "DW_OP_not";
2873     case DW_OP_or:
2874       return "DW_OP_or";
2875     case DW_OP_plus:
2876       return "DW_OP_plus";
2877     case DW_OP_plus_uconst:
2878       return "DW_OP_plus_uconst";
2879     case DW_OP_shl:
2880       return "DW_OP_shl";
2881     case DW_OP_shr:
2882       return "DW_OP_shr";
2883     case DW_OP_shra:
2884       return "DW_OP_shra";
2885     case DW_OP_xor:
2886       return "DW_OP_xor";
2887     case DW_OP_bra:
2888       return "DW_OP_bra";
2889     case DW_OP_eq:
2890       return "DW_OP_eq";
2891     case DW_OP_ge:
2892       return "DW_OP_ge";
2893     case DW_OP_gt:
2894       return "DW_OP_gt";
2895     case DW_OP_le:
2896       return "DW_OP_le";
2897     case DW_OP_lt:
2898       return "DW_OP_lt";
2899     case DW_OP_ne:
2900       return "DW_OP_ne";
2901     case DW_OP_skip:
2902       return "DW_OP_skip";
2903     case DW_OP_lit0:
2904       return "DW_OP_lit0";
2905     case DW_OP_lit1:
2906       return "DW_OP_lit1";
2907     case DW_OP_lit2:
2908       return "DW_OP_lit2";
2909     case DW_OP_lit3:
2910       return "DW_OP_lit3";
2911     case DW_OP_lit4:
2912       return "DW_OP_lit4";
2913     case DW_OP_lit5:
2914       return "DW_OP_lit5";
2915     case DW_OP_lit6:
2916       return "DW_OP_lit6";
2917     case DW_OP_lit7:
2918       return "DW_OP_lit7";
2919     case DW_OP_lit8:
2920       return "DW_OP_lit8";
2921     case DW_OP_lit9:
2922       return "DW_OP_lit9";
2923     case DW_OP_lit10:
2924       return "DW_OP_lit10";
2925     case DW_OP_lit11:
2926       return "DW_OP_lit11";
2927     case DW_OP_lit12:
2928       return "DW_OP_lit12";
2929     case DW_OP_lit13:
2930       return "DW_OP_lit13";
2931     case DW_OP_lit14:
2932       return "DW_OP_lit14";
2933     case DW_OP_lit15:
2934       return "DW_OP_lit15";
2935     case DW_OP_lit16:
2936       return "DW_OP_lit16";
2937     case DW_OP_lit17:
2938       return "DW_OP_lit17";
2939     case DW_OP_lit18:
2940       return "DW_OP_lit18";
2941     case DW_OP_lit19:
2942       return "DW_OP_lit19";
2943     case DW_OP_lit20:
2944       return "DW_OP_lit20";
2945     case DW_OP_lit21:
2946       return "DW_OP_lit21";
2947     case DW_OP_lit22:
2948       return "DW_OP_lit22";
2949     case DW_OP_lit23:
2950       return "DW_OP_lit23";
2951     case DW_OP_lit24:
2952       return "DW_OP_lit24";
2953     case DW_OP_lit25:
2954       return "DW_OP_lit25";
2955     case DW_OP_lit26:
2956       return "DW_OP_lit26";
2957     case DW_OP_lit27:
2958       return "DW_OP_lit27";
2959     case DW_OP_lit28:
2960       return "DW_OP_lit28";
2961     case DW_OP_lit29:
2962       return "DW_OP_lit29";
2963     case DW_OP_lit30:
2964       return "DW_OP_lit30";
2965     case DW_OP_lit31:
2966       return "DW_OP_lit31";
2967     case DW_OP_reg0:
2968       return "DW_OP_reg0";
2969     case DW_OP_reg1:
2970       return "DW_OP_reg1";
2971     case DW_OP_reg2:
2972       return "DW_OP_reg2";
2973     case DW_OP_reg3:
2974       return "DW_OP_reg3";
2975     case DW_OP_reg4:
2976       return "DW_OP_reg4";
2977     case DW_OP_reg5:
2978       return "DW_OP_reg5";
2979     case DW_OP_reg6:
2980       return "DW_OP_reg6";
2981     case DW_OP_reg7:
2982       return "DW_OP_reg7";
2983     case DW_OP_reg8:
2984       return "DW_OP_reg8";
2985     case DW_OP_reg9:
2986       return "DW_OP_reg9";
2987     case DW_OP_reg10:
2988       return "DW_OP_reg10";
2989     case DW_OP_reg11:
2990       return "DW_OP_reg11";
2991     case DW_OP_reg12:
2992       return "DW_OP_reg12";
2993     case DW_OP_reg13:
2994       return "DW_OP_reg13";
2995     case DW_OP_reg14:
2996       return "DW_OP_reg14";
2997     case DW_OP_reg15:
2998       return "DW_OP_reg15";
2999     case DW_OP_reg16:
3000       return "DW_OP_reg16";
3001     case DW_OP_reg17:
3002       return "DW_OP_reg17";
3003     case DW_OP_reg18:
3004       return "DW_OP_reg18";
3005     case DW_OP_reg19:
3006       return "DW_OP_reg19";
3007     case DW_OP_reg20:
3008       return "DW_OP_reg20";
3009     case DW_OP_reg21:
3010       return "DW_OP_reg21";
3011     case DW_OP_reg22:
3012       return "DW_OP_reg22";
3013     case DW_OP_reg23:
3014       return "DW_OP_reg23";
3015     case DW_OP_reg24:
3016       return "DW_OP_reg24";
3017     case DW_OP_reg25:
3018       return "DW_OP_reg25";
3019     case DW_OP_reg26:
3020       return "DW_OP_reg26";
3021     case DW_OP_reg27:
3022       return "DW_OP_reg27";
3023     case DW_OP_reg28:
3024       return "DW_OP_reg28";
3025     case DW_OP_reg29:
3026       return "DW_OP_reg29";
3027     case DW_OP_reg30:
3028       return "DW_OP_reg30";
3029     case DW_OP_reg31:
3030       return "DW_OP_reg31";
3031     case DW_OP_breg0:
3032       return "DW_OP_breg0";
3033     case DW_OP_breg1:
3034       return "DW_OP_breg1";
3035     case DW_OP_breg2:
3036       return "DW_OP_breg2";
3037     case DW_OP_breg3:
3038       return "DW_OP_breg3";
3039     case DW_OP_breg4:
3040       return "DW_OP_breg4";
3041     case DW_OP_breg5:
3042       return "DW_OP_breg5";
3043     case DW_OP_breg6:
3044       return "DW_OP_breg6";
3045     case DW_OP_breg7:
3046       return "DW_OP_breg7";
3047     case DW_OP_breg8:
3048       return "DW_OP_breg8";
3049     case DW_OP_breg9:
3050       return "DW_OP_breg9";
3051     case DW_OP_breg10:
3052       return "DW_OP_breg10";
3053     case DW_OP_breg11:
3054       return "DW_OP_breg11";
3055     case DW_OP_breg12:
3056       return "DW_OP_breg12";
3057     case DW_OP_breg13:
3058       return "DW_OP_breg13";
3059     case DW_OP_breg14:
3060       return "DW_OP_breg14";
3061     case DW_OP_breg15:
3062       return "DW_OP_breg15";
3063     case DW_OP_breg16:
3064       return "DW_OP_breg16";
3065     case DW_OP_breg17:
3066       return "DW_OP_breg17";
3067     case DW_OP_breg18:
3068       return "DW_OP_breg18";
3069     case DW_OP_breg19:
3070       return "DW_OP_breg19";
3071     case DW_OP_breg20:
3072       return "DW_OP_breg20";
3073     case DW_OP_breg21:
3074       return "DW_OP_breg21";
3075     case DW_OP_breg22:
3076       return "DW_OP_breg22";
3077     case DW_OP_breg23:
3078       return "DW_OP_breg23";
3079     case DW_OP_breg24:
3080       return "DW_OP_breg24";
3081     case DW_OP_breg25:
3082       return "DW_OP_breg25";
3083     case DW_OP_breg26:
3084       return "DW_OP_breg26";
3085     case DW_OP_breg27:
3086       return "DW_OP_breg27";
3087     case DW_OP_breg28:
3088       return "DW_OP_breg28";
3089     case DW_OP_breg29:
3090       return "DW_OP_breg29";
3091     case DW_OP_breg30:
3092       return "DW_OP_breg30";
3093     case DW_OP_breg31:
3094       return "DW_OP_breg31";
3095     case DW_OP_regx:
3096       return "DW_OP_regx";
3097     case DW_OP_fbreg:
3098       return "DW_OP_fbreg";
3099     case DW_OP_bregx:
3100       return "DW_OP_bregx";
3101     case DW_OP_piece:
3102       return "DW_OP_piece";
3103     case DW_OP_deref_size:
3104       return "DW_OP_deref_size";
3105     case DW_OP_xderef_size:
3106       return "DW_OP_xderef_size";
3107     case DW_OP_nop:
3108       return "DW_OP_nop";
3109     case DW_OP_push_object_address:
3110       return "DW_OP_push_object_address";
3111     case DW_OP_call2:
3112       return "DW_OP_call2";
3113     case DW_OP_call4:
3114       return "DW_OP_call4";
3115     case DW_OP_call_ref:
3116       return "DW_OP_call_ref";
3117     case DW_OP_GNU_push_tls_address:
3118       return "DW_OP_GNU_push_tls_address";
3119     default:
3120       return "OP_<unknown>";
3121     }
3122 }
3123
3124 /* Return a pointer to a newly allocated location description.  Location
3125    descriptions are simple expression terms that can be strung
3126    together to form more complicated location (address) descriptions.  */
3127
3128 static inline dw_loc_descr_ref
3129 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3130                unsigned HOST_WIDE_INT oprnd2)
3131 {
3132   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3133
3134   descr->dw_loc_opc = op;
3135   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3136   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3137   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3138   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3139
3140   return descr;
3141 }
3142
3143 /* Add a location description term to a location description expression.  */
3144
3145 static inline void
3146 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3147 {
3148   dw_loc_descr_ref *d;
3149
3150   /* Find the end of the chain.  */
3151   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3152     ;
3153
3154   *d = descr;
3155 }
3156
3157 /* Return the size of a location descriptor.  */
3158
3159 static unsigned long
3160 size_of_loc_descr (dw_loc_descr_ref loc)
3161 {
3162   unsigned long size = 1;
3163
3164   switch (loc->dw_loc_opc)
3165     {
3166     case DW_OP_addr:
3167     case INTERNAL_DW_OP_tls_addr:
3168       size += DWARF2_ADDR_SIZE;
3169       break;
3170     case DW_OP_const1u:
3171     case DW_OP_const1s:
3172       size += 1;
3173       break;
3174     case DW_OP_const2u:
3175     case DW_OP_const2s:
3176       size += 2;
3177       break;
3178     case DW_OP_const4u:
3179     case DW_OP_const4s:
3180       size += 4;
3181       break;
3182     case DW_OP_const8u:
3183     case DW_OP_const8s:
3184       size += 8;
3185       break;
3186     case DW_OP_constu:
3187       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3188       break;
3189     case DW_OP_consts:
3190       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3191       break;
3192     case DW_OP_pick:
3193       size += 1;
3194       break;
3195     case DW_OP_plus_uconst:
3196       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3197       break;
3198     case DW_OP_skip:
3199     case DW_OP_bra:
3200       size += 2;
3201       break;
3202     case DW_OP_breg0:
3203     case DW_OP_breg1:
3204     case DW_OP_breg2:
3205     case DW_OP_breg3:
3206     case DW_OP_breg4:
3207     case DW_OP_breg5:
3208     case DW_OP_breg6:
3209     case DW_OP_breg7:
3210     case DW_OP_breg8:
3211     case DW_OP_breg9:
3212     case DW_OP_breg10:
3213     case DW_OP_breg11:
3214     case DW_OP_breg12:
3215     case DW_OP_breg13:
3216     case DW_OP_breg14:
3217     case DW_OP_breg15:
3218     case DW_OP_breg16:
3219     case DW_OP_breg17:
3220     case DW_OP_breg18:
3221     case DW_OP_breg19:
3222     case DW_OP_breg20:
3223     case DW_OP_breg21:
3224     case DW_OP_breg22:
3225     case DW_OP_breg23:
3226     case DW_OP_breg24:
3227     case DW_OP_breg25:
3228     case DW_OP_breg26:
3229     case DW_OP_breg27:
3230     case DW_OP_breg28:
3231     case DW_OP_breg29:
3232     case DW_OP_breg30:
3233     case DW_OP_breg31:
3234       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3235       break;
3236     case DW_OP_regx:
3237       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3238       break;
3239     case DW_OP_fbreg:
3240       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3241       break;
3242     case DW_OP_bregx:
3243       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3244       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3245       break;
3246     case DW_OP_piece:
3247       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3248       break;
3249     case DW_OP_deref_size:
3250     case DW_OP_xderef_size:
3251       size += 1;
3252       break;
3253     case DW_OP_call2:
3254       size += 2;
3255       break;
3256     case DW_OP_call4:
3257       size += 4;
3258       break;
3259     case DW_OP_call_ref:
3260       size += DWARF2_ADDR_SIZE;
3261       break;
3262     default:
3263       break;
3264     }
3265
3266   return size;
3267 }
3268
3269 /* Return the size of a series of location descriptors.  */
3270
3271 static unsigned long
3272 size_of_locs (dw_loc_descr_ref loc)
3273 {
3274   dw_loc_descr_ref l;
3275   unsigned long size;
3276
3277   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3278      field, to avoid writing to a PCH file.  */
3279   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3280     {
3281       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3282         break;
3283       size += size_of_loc_descr (l);
3284     }
3285   if (! l)
3286     return size;
3287
3288   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3289     {
3290       l->dw_loc_addr = size;
3291       size += size_of_loc_descr (l);
3292     }
3293
3294   return size;
3295 }
3296
3297 /* Output location description stack opcode's operands (if any).  */
3298
3299 static void
3300 output_loc_operands (dw_loc_descr_ref loc)
3301 {
3302   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3303   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3304
3305   switch (loc->dw_loc_opc)
3306     {
3307 #ifdef DWARF2_DEBUGGING_INFO
3308     case DW_OP_addr:
3309       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3310       break;
3311     case DW_OP_const2u:
3312     case DW_OP_const2s:
3313       dw2_asm_output_data (2, val1->v.val_int, NULL);
3314       break;
3315     case DW_OP_const4u:
3316     case DW_OP_const4s:
3317       dw2_asm_output_data (4, val1->v.val_int, NULL);
3318       break;
3319     case DW_OP_const8u:
3320     case DW_OP_const8s:
3321       gcc_assert (HOST_BITS_PER_LONG >= 64);
3322       dw2_asm_output_data (8, val1->v.val_int, NULL);
3323       break;
3324     case DW_OP_skip:
3325     case DW_OP_bra:
3326       {
3327         int offset;
3328
3329         gcc_assert (val1->val_class == dw_val_class_loc);
3330         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3331
3332         dw2_asm_output_data (2, offset, NULL);
3333       }
3334       break;
3335 #else
3336     case DW_OP_addr:
3337     case DW_OP_const2u:
3338     case DW_OP_const2s:
3339     case DW_OP_const4u:
3340     case DW_OP_const4s:
3341     case DW_OP_const8u:
3342     case DW_OP_const8s:
3343     case DW_OP_skip:
3344     case DW_OP_bra:
3345       /* We currently don't make any attempt to make sure these are
3346          aligned properly like we do for the main unwind info, so
3347          don't support emitting things larger than a byte if we're
3348          only doing unwinding.  */
3349       gcc_unreachable ();
3350 #endif
3351     case DW_OP_const1u:
3352     case DW_OP_const1s:
3353       dw2_asm_output_data (1, val1->v.val_int, NULL);
3354       break;
3355     case DW_OP_constu:
3356       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3357       break;
3358     case DW_OP_consts:
3359       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3360       break;
3361     case DW_OP_pick:
3362       dw2_asm_output_data (1, val1->v.val_int, NULL);
3363       break;
3364     case DW_OP_plus_uconst:
3365       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3366       break;
3367     case DW_OP_breg0:
3368     case DW_OP_breg1:
3369     case DW_OP_breg2:
3370     case DW_OP_breg3:
3371     case DW_OP_breg4:
3372     case DW_OP_breg5:
3373     case DW_OP_breg6:
3374     case DW_OP_breg7:
3375     case DW_OP_breg8:
3376     case DW_OP_breg9:
3377     case DW_OP_breg10:
3378     case DW_OP_breg11:
3379     case DW_OP_breg12:
3380     case DW_OP_breg13:
3381     case DW_OP_breg14:
3382     case DW_OP_breg15:
3383     case DW_OP_breg16:
3384     case DW_OP_breg17:
3385     case DW_OP_breg18:
3386     case DW_OP_breg19:
3387     case DW_OP_breg20:
3388     case DW_OP_breg21:
3389     case DW_OP_breg22:
3390     case DW_OP_breg23:
3391     case DW_OP_breg24:
3392     case DW_OP_breg25:
3393     case DW_OP_breg26:
3394     case DW_OP_breg27:
3395     case DW_OP_breg28:
3396     case DW_OP_breg29:
3397     case DW_OP_breg30:
3398     case DW_OP_breg31:
3399       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3400       break;
3401     case DW_OP_regx:
3402       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3403       break;
3404     case DW_OP_fbreg:
3405       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3406       break;
3407     case DW_OP_bregx:
3408       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3409       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3410       break;
3411     case DW_OP_piece:
3412       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3413       break;
3414     case DW_OP_deref_size:
3415     case DW_OP_xderef_size:
3416       dw2_asm_output_data (1, val1->v.val_int, NULL);
3417       break;
3418
3419     case INTERNAL_DW_OP_tls_addr:
3420       if (targetm.asm_out.output_dwarf_dtprel)
3421         {
3422           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3423                                                DWARF2_ADDR_SIZE,
3424                                                val1->v.val_addr);
3425           fputc ('\n', asm_out_file);
3426         }
3427       else
3428         gcc_unreachable ();
3429       break;
3430
3431     default:
3432       /* Other codes have no operands.  */
3433       break;
3434     }
3435 }
3436
3437 /* Output a sequence of location operations.  */
3438
3439 static void
3440 output_loc_sequence (dw_loc_descr_ref loc)
3441 {
3442   for (; loc != NULL; loc = loc->dw_loc_next)
3443     {
3444       /* Output the opcode.  */
3445       dw2_asm_output_data (1, loc->dw_loc_opc,
3446                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3447
3448       /* Output the operand(s) (if any).  */
3449       output_loc_operands (loc);
3450     }
3451 }
3452
3453 /* This routine will generate the correct assembly data for a location
3454    description based on a cfi entry with a complex address.  */
3455
3456 static void
3457 output_cfa_loc (dw_cfi_ref cfi)
3458 {
3459   dw_loc_descr_ref loc;
3460   unsigned long size;
3461
3462   /* Output the size of the block.  */
3463   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3464   size = size_of_locs (loc);
3465   dw2_asm_output_data_uleb128 (size, NULL);
3466
3467   /* Now output the operations themselves.  */
3468   output_loc_sequence (loc);
3469 }
3470
3471 /* This function builds a dwarf location descriptor sequence from a
3472    dw_cfa_location, adding the given OFFSET to the result of the
3473    expression.  */
3474
3475 static struct dw_loc_descr_struct *
3476 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3477 {
3478   struct dw_loc_descr_struct *head, *tmp;
3479
3480   offset += cfa->offset;
3481
3482   if (cfa->indirect)
3483     {
3484       if (cfa->base_offset)
3485         {
3486           if (cfa->reg <= 31)
3487             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3488           else
3489             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3490         }
3491       else if (cfa->reg <= 31)
3492         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3493       else
3494         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3495
3496       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3497       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3498       add_loc_descr (&head, tmp);
3499       if (offset != 0)
3500         {
3501           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3502           add_loc_descr (&head, tmp);
3503         }
3504     }
3505   else
3506     {
3507       if (offset == 0)
3508         if (cfa->reg <= 31)
3509           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3510         else
3511           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3512       else if (cfa->reg <= 31)
3513         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3514       else
3515         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3516     }
3517
3518   return head;
3519 }
3520
3521 /* This function fills in aa dw_cfa_location structure from a dwarf location
3522    descriptor sequence.  */
3523
3524 static void
3525 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3526 {
3527   struct dw_loc_descr_struct *ptr;
3528   cfa->offset = 0;
3529   cfa->base_offset = 0;
3530   cfa->indirect = 0;
3531   cfa->reg = -1;
3532
3533   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3534     {
3535       enum dwarf_location_atom op = ptr->dw_loc_opc;
3536
3537       switch (op)
3538         {
3539         case DW_OP_reg0:
3540         case DW_OP_reg1:
3541         case DW_OP_reg2:
3542         case DW_OP_reg3:
3543         case DW_OP_reg4:
3544         case DW_OP_reg5:
3545         case DW_OP_reg6:
3546         case DW_OP_reg7:
3547         case DW_OP_reg8:
3548         case DW_OP_reg9:
3549         case DW_OP_reg10:
3550         case DW_OP_reg11:
3551         case DW_OP_reg12:
3552         case DW_OP_reg13:
3553         case DW_OP_reg14:
3554         case DW_OP_reg15:
3555         case DW_OP_reg16:
3556         case DW_OP_reg17:
3557         case DW_OP_reg18:
3558         case DW_OP_reg19:
3559         case DW_OP_reg20:
3560         case DW_OP_reg21:
3561         case DW_OP_reg22:
3562         case DW_OP_reg23:
3563         case DW_OP_reg24:
3564         case DW_OP_reg25:
3565         case DW_OP_reg26:
3566         case DW_OP_reg27:
3567         case DW_OP_reg28:
3568         case DW_OP_reg29:
3569         case DW_OP_reg30:
3570         case DW_OP_reg31:
3571           cfa->reg = op - DW_OP_reg0;
3572           break;
3573         case DW_OP_regx:
3574           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3575           break;
3576         case DW_OP_breg0:
3577         case DW_OP_breg1:
3578         case DW_OP_breg2:
3579         case DW_OP_breg3:
3580         case DW_OP_breg4:
3581         case DW_OP_breg5:
3582         case DW_OP_breg6:
3583         case DW_OP_breg7:
3584         case DW_OP_breg8:
3585         case DW_OP_breg9:
3586         case DW_OP_breg10:
3587         case DW_OP_breg11:
3588         case DW_OP_breg12:
3589         case DW_OP_breg13:
3590         case DW_OP_breg14:
3591         case DW_OP_breg15:
3592         case DW_OP_breg16:
3593         case DW_OP_breg17:
3594         case DW_OP_breg18:
3595         case DW_OP_breg19:
3596         case DW_OP_breg20:
3597         case DW_OP_breg21:
3598         case DW_OP_breg22:
3599         case DW_OP_breg23:
3600         case DW_OP_breg24:
3601         case DW_OP_breg25:
3602         case DW_OP_breg26:
3603         case DW_OP_breg27:
3604         case DW_OP_breg28:
3605         case DW_OP_breg29:
3606         case DW_OP_breg30:
3607         case DW_OP_breg31:
3608           cfa->reg = op - DW_OP_breg0;
3609           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3610           break;
3611         case DW_OP_bregx:
3612           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3613           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3614           break;
3615         case DW_OP_deref:
3616           cfa->indirect = 1;
3617           break;
3618         case DW_OP_plus_uconst:
3619           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3620           break;
3621         default:
3622           internal_error ("DW_LOC_OP %s not implemented",
3623                           dwarf_stack_op_name (ptr->dw_loc_opc));
3624         }
3625     }
3626 }
3627 #endif /* .debug_frame support */
3628 \f
3629 /* And now, the support for symbolic debugging information.  */
3630 #ifdef DWARF2_DEBUGGING_INFO
3631
3632 /* .debug_str support.  */
3633 static int output_indirect_string (void **, void *);
3634
3635 static void dwarf2out_init (const char *);
3636 static void dwarf2out_finish (const char *);
3637 static void dwarf2out_define (unsigned int, const char *);
3638 static void dwarf2out_undef (unsigned int, const char *);
3639 static void dwarf2out_start_source_file (unsigned, const char *);
3640 static void dwarf2out_end_source_file (unsigned);
3641 static void dwarf2out_begin_block (unsigned, unsigned);
3642 static void dwarf2out_end_block (unsigned, unsigned);
3643 static bool dwarf2out_ignore_block (tree);
3644 static void dwarf2out_global_decl (tree);
3645 static void dwarf2out_type_decl (tree, int);
3646 static void dwarf2out_imported_module_or_decl (tree, tree);
3647 static void dwarf2out_abstract_function (tree);
3648 static void dwarf2out_var_location (rtx);
3649 static void dwarf2out_begin_function (tree);
3650 static void dwarf2out_switch_text_section (void);
3651
3652 /* The debug hooks structure.  */
3653
3654 const struct gcc_debug_hooks dwarf2_debug_hooks =
3655 {
3656   dwarf2out_init,
3657   dwarf2out_finish,
3658   dwarf2out_define,
3659   dwarf2out_undef,
3660   dwarf2out_start_source_file,
3661   dwarf2out_end_source_file,
3662   dwarf2out_begin_block,
3663   dwarf2out_end_block,
3664   dwarf2out_ignore_block,
3665   dwarf2out_source_line,
3666   dwarf2out_begin_prologue,
3667   debug_nothing_int_charstar,   /* end_prologue */
3668   dwarf2out_end_epilogue,
3669   dwarf2out_begin_function,
3670   debug_nothing_int,            /* end_function */
3671   dwarf2out_decl,               /* function_decl */
3672   dwarf2out_global_decl,
3673   dwarf2out_type_decl,          /* type_decl */
3674   dwarf2out_imported_module_or_decl,
3675   debug_nothing_tree,           /* deferred_inline_function */
3676   /* The DWARF 2 backend tries to reduce debugging bloat by not
3677      emitting the abstract description of inline functions until
3678      something tries to reference them.  */
3679   dwarf2out_abstract_function,  /* outlining_inline_function */
3680   debug_nothing_rtx,            /* label */
3681   debug_nothing_int,            /* handle_pch */
3682   dwarf2out_var_location,
3683   dwarf2out_switch_text_section,
3684   1                             /* start_end_main_source_file */
3685 };
3686 #endif
3687 \f
3688 /* NOTE: In the comments in this file, many references are made to
3689    "Debugging Information Entries".  This term is abbreviated as `DIE'
3690    throughout the remainder of this file.  */
3691
3692 /* An internal representation of the DWARF output is built, and then
3693    walked to generate the DWARF debugging info.  The walk of the internal
3694    representation is done after the entire program has been compiled.
3695    The types below are used to describe the internal representation.  */
3696
3697 /* Various DIE's use offsets relative to the beginning of the
3698    .debug_info section to refer to each other.  */
3699
3700 typedef long int dw_offset;
3701
3702 /* Define typedefs here to avoid circular dependencies.  */
3703
3704 typedef struct dw_attr_struct *dw_attr_ref;
3705 typedef struct dw_line_info_struct *dw_line_info_ref;
3706 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3707 typedef struct pubname_struct *pubname_ref;
3708 typedef struct dw_ranges_struct *dw_ranges_ref;
3709
3710 /* Each entry in the line_info_table maintains the file and
3711    line number associated with the label generated for that
3712    entry.  The label gives the PC value associated with
3713    the line number entry.  */
3714
3715 typedef struct dw_line_info_struct GTY(())
3716 {
3717   unsigned long dw_file_num;
3718   unsigned long dw_line_num;
3719 }
3720 dw_line_info_entry;
3721
3722 /* Line information for functions in separate sections; each one gets its
3723    own sequence.  */
3724 typedef struct dw_separate_line_info_struct GTY(())
3725 {
3726   unsigned long dw_file_num;
3727   unsigned long dw_line_num;
3728   unsigned long function;
3729 }
3730 dw_separate_line_info_entry;
3731
3732 /* Each DIE attribute has a field specifying the attribute kind,
3733    a link to the next attribute in the chain, and an attribute value.
3734    Attributes are typically linked below the DIE they modify.  */
3735
3736 typedef struct dw_attr_struct GTY(())
3737 {
3738   enum dwarf_attribute dw_attr;
3739   dw_val_node dw_attr_val;
3740 }
3741 dw_attr_node;
3742
3743 DEF_VEC_O(dw_attr_node);
3744 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3745
3746 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3747    The children of each node form a circular list linked by
3748    die_sib.  die_child points to the node *before* the "first" child node.  */
3749
3750 typedef struct die_struct GTY(())
3751 {
3752   enum dwarf_tag die_tag;
3753   char *die_symbol;
3754   VEC(dw_attr_node,gc) * die_attr;
3755   dw_die_ref die_parent;
3756   dw_die_ref die_child;
3757   dw_die_ref die_sib;
3758   dw_die_ref die_definition; /* ref from a specification to its definition */
3759   dw_offset die_offset;
3760   unsigned long die_abbrev;
3761   int die_mark;
3762   /* Die is used and must not be pruned as unused.  */
3763   int die_perennial_p;
3764   unsigned int decl_id;
3765 }
3766 die_node;
3767
3768 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3769 #define FOR_EACH_CHILD(die, c, expr) do {       \
3770   c = die->die_child;                           \
3771   if (c) do {                                   \
3772     c = c->die_sib;                             \
3773     expr;                                       \
3774   } while (c != die->die_child);                \
3775 } while (0)
3776
3777 /* The pubname structure */
3778
3779 typedef struct pubname_struct GTY(())
3780 {
3781   dw_die_ref die;
3782   const char *name;
3783 }
3784 pubname_entry;
3785
3786 DEF_VEC_O(pubname_entry);
3787 DEF_VEC_ALLOC_O(pubname_entry, gc);
3788
3789 struct dw_ranges_struct GTY(())
3790 {
3791   int block_num;
3792 };
3793
3794 /* The limbo die list structure.  */
3795 typedef struct limbo_die_struct GTY(())
3796 {
3797   dw_die_ref die;
3798   tree created_for;
3799   struct limbo_die_struct *next;
3800 }
3801 limbo_die_node;
3802
3803 /* How to start an assembler comment.  */
3804 #ifndef ASM_COMMENT_START
3805 #define ASM_COMMENT_START ";#"
3806 #endif
3807
3808 /* Define a macro which returns nonzero for a TYPE_DECL which was
3809    implicitly generated for a tagged type.
3810
3811    Note that unlike the gcc front end (which generates a NULL named
3812    TYPE_DECL node for each complete tagged type, each array type, and
3813    each function type node created) the g++ front end generates a
3814    _named_ TYPE_DECL node for each tagged type node created.
3815    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3816    generate a DW_TAG_typedef DIE for them.  */
3817
3818 #define TYPE_DECL_IS_STUB(decl)                         \
3819   (DECL_NAME (decl) == NULL_TREE                        \
3820    || (DECL_ARTIFICIAL (decl)                           \
3821        && is_tagged_type (TREE_TYPE (decl))             \
3822        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3823            /* This is necessary for stub decls that     \
3824               appear in nested inline functions.  */    \
3825            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3826                && (decl_ultimate_origin (decl)          \
3827                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3828
3829 /* Information concerning the compilation unit's programming
3830    language, and compiler version.  */
3831
3832 /* Fixed size portion of the DWARF compilation unit header.  */
3833 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3834   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3835
3836 /* Fixed size portion of public names info.  */
3837 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3838
3839 /* Fixed size portion of the address range info.  */
3840 #define DWARF_ARANGES_HEADER_SIZE                                       \
3841   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3842                 DWARF2_ADDR_SIZE * 2)                                   \
3843    - DWARF_INITIAL_LENGTH_SIZE)
3844
3845 /* Size of padding portion in the address range info.  It must be
3846    aligned to twice the pointer size.  */
3847 #define DWARF_ARANGES_PAD_SIZE \
3848   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3849                 DWARF2_ADDR_SIZE * 2)                              \
3850    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3851
3852 /* Use assembler line directives if available.  */
3853 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3854 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3855 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3856 #else
3857 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3858 #endif
3859 #endif
3860
3861 /* Minimum line offset in a special line info. opcode.
3862    This value was chosen to give a reasonable range of values.  */
3863 #define DWARF_LINE_BASE  -10
3864
3865 /* First special line opcode - leave room for the standard opcodes.  */
3866 #define DWARF_LINE_OPCODE_BASE  10
3867
3868 /* Range of line offsets in a special line info. opcode.  */
3869 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3870
3871 /* Flag that indicates the initial value of the is_stmt_start flag.
3872    In the present implementation, we do not mark any lines as
3873    the beginning of a source statement, because that information
3874    is not made available by the GCC front-end.  */
3875 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3876
3877 #ifdef DWARF2_DEBUGGING_INFO
3878 /* This location is used by calc_die_sizes() to keep track
3879    the offset of each DIE within the .debug_info section.  */
3880 static unsigned long next_die_offset;
3881 #endif
3882
3883 /* Record the root of the DIE's built for the current compilation unit.  */
3884 static GTY(()) dw_die_ref comp_unit_die;
3885
3886 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3887 static GTY(()) limbo_die_node *limbo_die_list;
3888
3889 /* Filenames referenced by this compilation unit.  */
3890 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3891
3892 /* A hash table of references to DIE's that describe declarations.
3893    The key is a DECL_UID() which is a unique number identifying each decl.  */
3894 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3895
3896 /* Node of the variable location list.  */
3897 struct var_loc_node GTY ((chain_next ("%h.next")))
3898 {
3899   rtx GTY (()) var_loc_note;
3900   const char * GTY (()) label;
3901   const char * GTY (()) section_label;
3902   struct var_loc_node * GTY (()) next;
3903 };
3904
3905 /* Variable location list.  */
3906 struct var_loc_list_def GTY (())
3907 {
3908   struct var_loc_node * GTY (()) first;
3909
3910   /* Do not mark the last element of the chained list because
3911      it is marked through the chain.  */
3912   struct var_loc_node * GTY ((skip ("%h"))) last;
3913
3914   /* DECL_UID of the variable decl.  */
3915   unsigned int decl_id;
3916 };
3917 typedef struct var_loc_list_def var_loc_list;
3918
3919
3920 /* Table of decl location linked lists.  */
3921 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3922
3923 /* A pointer to the base of a list of references to DIE's that
3924    are uniquely identified by their tag, presence/absence of
3925    children DIE's, and list of attribute/value pairs.  */
3926 static GTY((length ("abbrev_die_table_allocated")))
3927   dw_die_ref *abbrev_die_table;
3928
3929 /* Number of elements currently allocated for abbrev_die_table.  */
3930 static GTY(()) unsigned abbrev_die_table_allocated;
3931
3932 /* Number of elements in type_die_table currently in use.  */
3933 static GTY(()) unsigned abbrev_die_table_in_use;
3934
3935 /* Size (in elements) of increments by which we may expand the
3936    abbrev_die_table.  */
3937 #define ABBREV_DIE_TABLE_INCREMENT 256
3938
3939 /* A pointer to the base of a table that contains line information
3940    for each source code line in .text in the compilation unit.  */
3941 static GTY((length ("line_info_table_allocated")))
3942      dw_line_info_ref line_info_table;
3943
3944 /* Number of elements currently allocated for line_info_table.  */
3945 static GTY(()) unsigned line_info_table_allocated;
3946
3947 /* Number of elements in line_info_table currently in use.  */
3948 static GTY(()) unsigned line_info_table_in_use;
3949
3950 /* True if the compilation unit places functions in more than one section.  */
3951 static GTY(()) bool have_multiple_function_sections = false;
3952
3953 /* A pointer to the base of a table that contains line information
3954    for each source code line outside of .text in the compilation unit.  */
3955 static GTY ((length ("separate_line_info_table_allocated")))
3956      dw_separate_line_info_ref separate_line_info_table;
3957
3958 /* Number of elements currently allocated for separate_line_info_table.  */
3959 static GTY(()) unsigned separate_line_info_table_allocated;
3960
3961 /* Number of elements in separate_line_info_table currently in use.  */
3962 static GTY(()) unsigned separate_line_info_table_in_use;
3963
3964 /* Size (in elements) of increments by which we may expand the
3965    line_info_table.  */
3966 #define LINE_INFO_TABLE_INCREMENT 1024
3967
3968 /* A pointer to the base of a table that contains a list of publicly
3969    accessible names.  */
3970 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3971
3972 /* A pointer to the base of a table that contains a list of publicly
3973    accessible types.  */
3974 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3975
3976 /* Array of dies for which we should generate .debug_arange info.  */
3977 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3978
3979 /* Number of elements currently allocated for arange_table.  */
3980 static GTY(()) unsigned arange_table_allocated;
3981
3982 /* Number of elements in arange_table currently in use.  */
3983 static GTY(()) unsigned arange_table_in_use;
3984
3985 /* Size (in elements) of increments by which we may expand the
3986    arange_table.  */
3987 #define ARANGE_TABLE_INCREMENT 64
3988
3989 /* Array of dies for which we should generate .debug_ranges info.  */
3990 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3991
3992 /* Number of elements currently allocated for ranges_table.  */
3993 static GTY(()) unsigned ranges_table_allocated;
3994
3995 /* Number of elements in ranges_table currently in use.  */
3996 static GTY(()) unsigned ranges_table_in_use;
3997
3998 /* Size (in elements) of increments by which we may expand the
3999    ranges_table.  */
4000 #define RANGES_TABLE_INCREMENT 64
4001
4002 /* Whether we have location lists that need outputting */
4003 static GTY(()) bool have_location_lists;
4004
4005 /* Unique label counter.  */
4006 static GTY(()) unsigned int loclabel_num;
4007
4008 #ifdef DWARF2_DEBUGGING_INFO
4009 /* Record whether the function being analyzed contains inlined functions.  */
4010 static int current_function_has_inlines;
4011 #endif
4012 #if 0 && defined (MIPS_DEBUGGING_INFO)
4013 static int comp_unit_has_inlines;
4014 #endif
4015
4016 /* The last file entry emitted by maybe_emit_file().  */
4017 static GTY(()) struct dwarf_file_data * last_emitted_file;
4018
4019 /* Number of internal labels generated by gen_internal_sym().  */
4020 static GTY(()) int label_num;
4021
4022 /* Cached result of previous call to lookup_filename.  */
4023 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4024
4025 #ifdef DWARF2_DEBUGGING_INFO
4026
4027 /* Offset from the "steady-state frame pointer" to the frame base,
4028    within the current function.  */
4029 static HOST_WIDE_INT frame_pointer_fb_offset;
4030
4031 /* Forward declarations for functions defined in this file.  */
4032
4033 static int is_pseudo_reg (rtx);
4034 static tree type_main_variant (tree);
4035 static int is_tagged_type (tree);
4036 static const char *dwarf_tag_name (unsigned);
4037 static const char *dwarf_attr_name (unsigned);
4038 static const char *dwarf_form_name (unsigned);
4039 static tree decl_ultimate_origin (tree);
4040 static tree block_ultimate_origin (tree);
4041 static tree decl_class_context (tree);
4042 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4043 static inline enum dw_val_class AT_class (dw_attr_ref);
4044 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4045 static inline unsigned AT_flag (dw_attr_ref);
4046 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4047 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4048 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4049 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4050 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4051                               unsigned long);
4052 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4053                                unsigned int, unsigned char *);
4054 static hashval_t debug_str_do_hash (const void *);
4055 static int debug_str_eq (const void *, const void *);
4056 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4057 static inline const char *AT_string (dw_attr_ref);
4058 static int AT_string_form (dw_attr_ref);
4059 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4060 static void add_AT_specification (dw_die_ref, dw_die_ref);
4061 static inline dw_die_ref AT_ref (dw_attr_ref);
4062 static inline int AT_ref_external (dw_attr_ref);
4063 static inline void set_AT_ref_external (dw_attr_ref, int);
4064 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4065 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4066 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4067 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4068                              dw_loc_list_ref);
4069 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4070 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4071 static inline rtx AT_addr (dw_attr_ref);
4072 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4073 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4074 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4075 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4076                            unsigned HOST_WIDE_INT);
4077 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4078                                unsigned long);
4079 static inline const char *AT_lbl (dw_attr_ref);
4080 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4081 static const char *get_AT_low_pc (dw_die_ref);
4082 static const char *get_AT_hi_pc (dw_die_ref);
4083 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4084 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4085 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4086 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4087 static bool is_c_family (void);
4088 static bool is_cxx (void);
4089 static bool is_java (void);
4090 static bool is_fortran (void);
4091 static bool is_ada (void);
4092 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4093 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4094 static void add_child_die (dw_die_ref, dw_die_ref);
4095 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4096 static dw_die_ref lookup_type_die (tree);
4097 static void equate_type_number_to_die (tree, dw_die_ref);
4098 static hashval_t decl_die_table_hash (const void *);
4099 static int decl_die_table_eq (const void *, const void *);
4100 static dw_die_ref lookup_decl_die (tree);
4101 static hashval_t decl_loc_table_hash (const void *);
4102 static int decl_loc_table_eq (const void *, const void *);
4103 static var_loc_list *lookup_decl_loc (tree);
4104 static void equate_decl_number_to_die (tree, dw_die_ref);
4105 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4106 static void print_spaces (FILE *);
4107 static void print_die (dw_die_ref, FILE *);
4108 static void print_dwarf_line_table (FILE *);
4109 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4110 static dw_die_ref pop_compile_unit (dw_die_ref);
4111 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4112 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4113 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4114 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4115 static int same_dw_val_p (dw_val_node *, dw_val_node *, int *);
4116 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4117 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4118 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4119 static void compute_section_prefix (dw_die_ref);
4120 static int is_type_die (dw_die_ref);
4121 static int is_comdat_die (dw_die_ref);
4122 static int is_symbol_die (dw_die_ref);
4123 static void assign_symbol_names (dw_die_ref);
4124 static void break_out_includes (dw_die_ref);
4125 static hashval_t htab_cu_hash (const void *);
4126 static int htab_cu_eq (const void *, const void *);
4127 static void htab_cu_del (void *);
4128 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4129 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4130 static void add_sibling_attributes (dw_die_ref);
4131 static void build_abbrev_table (dw_die_ref);
4132 static void output_location_lists (dw_die_ref);
4133 static int constant_size (long unsigned);
4134 static unsigned long size_of_die (dw_die_ref);
4135 static void calc_die_sizes (dw_die_ref);
4136 static void mark_dies (dw_die_ref);
4137 static void unmark_dies (dw_die_ref);
4138 static void unmark_all_dies (dw_die_ref);
4139 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4140 static unsigned long size_of_aranges (void);
4141 static enum dwarf_form value_format (dw_attr_ref);
4142 static void output_value_format (dw_attr_ref);
4143 static void output_abbrev_section (void);
4144 static void output_die_symbol (dw_die_ref);
4145 static void output_die (dw_die_ref);
4146 static void output_compilation_unit_header (void);
4147 static void output_comp_unit (dw_die_ref, int);
4148 static const char *dwarf2_name (tree, int);
4149 static void add_pubname (tree, dw_die_ref);
4150 static void add_pubtype (tree, dw_die_ref);
4151 static void output_pubnames (VEC (pubname_entry,gc) *);
4152 static void add_arange (tree, dw_die_ref);
4153 static void output_aranges (void);
4154 static unsigned int add_ranges (tree);
4155 static void output_ranges (void);
4156 static void output_line_info (void);
4157 static void output_file_names (void);
4158 static dw_die_ref base_type_die (tree);
4159 static int is_base_type (tree);
4160 static bool is_subrange_type (tree);
4161 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4162 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4163 static int type_is_enum (tree);
4164 static unsigned int dbx_reg_number (rtx);
4165 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4166 static dw_loc_descr_ref reg_loc_descriptor (rtx);
4167 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int);
4168 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx);
4169 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4170 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT);
4171 static int is_based_loc (rtx);
4172 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode);
4173 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx);
4174 static dw_loc_descr_ref loc_descriptor (rtx);
4175 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4176 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4177 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4178 static tree field_type (tree);
4179 static unsigned int simple_type_align_in_bits (tree);
4180 static unsigned int simple_decl_align_in_bits (tree);
4181 static unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
4182 static HOST_WIDE_INT field_byte_offset (tree);
4183 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4184                                          dw_loc_descr_ref);
4185 static void add_data_member_location_attribute (dw_die_ref, tree);
4186 static void add_const_value_attribute (dw_die_ref, rtx);
4187 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4188 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4189 static void insert_float (rtx, unsigned char *);
4190 static rtx rtl_for_decl_location (tree);
4191 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4192                                                    enum dwarf_attribute);
4193 static void tree_add_const_value_attribute (dw_die_ref, tree);
4194 static void add_name_attribute (dw_die_ref, const char *);
4195 static void add_comp_dir_attribute (dw_die_ref);
4196 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4197 static void add_subscript_info (dw_die_ref, tree);
4198 static void add_byte_size_attribute (dw_die_ref, tree);
4199 static void add_bit_offset_attribute (dw_die_ref, tree);
4200 static void add_bit_size_attribute (dw_die_ref, tree);
4201 static void add_prototyped_attribute (dw_die_ref, tree);
4202 static void add_abstract_origin_attribute (dw_die_ref, tree);
4203 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4204 static void add_src_coords_attributes (dw_die_ref, tree);
4205 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4206 static void push_decl_scope (tree);
4207 static void pop_decl_scope (void);
4208 static dw_die_ref scope_die_for (tree, dw_die_ref);
4209 static inline int local_scope_p (dw_die_ref);
4210 static inline int class_or_namespace_scope_p (dw_die_ref);
4211 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4212 static void add_calling_convention_attribute (dw_die_ref, tree);
4213 static const char *type_tag (tree);
4214 static tree member_declared_type (tree);
4215 #if 0
4216 static const char *decl_start_label (tree);
4217 #endif
4218 static void gen_array_type_die (tree, dw_die_ref);
4219 #if 0
4220 static void gen_entry_point_die (tree, dw_die_ref);
4221 #endif
4222 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4223 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4224 static void gen_inlined_union_type_die (tree, dw_die_ref);
4225 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4226 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4227 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4228 static void gen_formal_types_die (tree, dw_die_ref);
4229 static void gen_subprogram_die (tree, dw_die_ref);
4230 static void gen_variable_die (tree, dw_die_ref);
4231 static void gen_label_die (tree, dw_die_ref);
4232 static void gen_lexical_block_die (tree, dw_die_ref, int);
4233 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4234 static void gen_field_die (tree, dw_die_ref);
4235 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4236 static dw_die_ref gen_compile_unit_die (const char *);
4237 static void gen_inheritance_die (tree, tree, dw_die_ref);
4238 static void gen_member_die (tree, dw_die_ref);
4239 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4240                                                 enum debug_info_usage);
4241 static void gen_subroutine_type_die (tree, dw_die_ref);
4242 static void gen_typedef_die (tree, dw_die_ref);
4243 static void gen_type_die (tree, dw_die_ref);
4244 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4245 static void gen_block_die (tree, dw_die_ref, int);
4246 static void decls_for_scope (tree, dw_die_ref, int);
4247 static int is_redundant_typedef (tree);
4248 static void gen_namespace_die (tree);
4249 static void gen_decl_die (tree, dw_die_ref);
4250 static dw_die_ref force_decl_die (tree);
4251 static dw_die_ref force_type_die (tree);
4252 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4253 static void declare_in_namespace (tree, dw_die_ref);
4254 static struct dwarf_file_data * lookup_filename (const char *);
4255 static void retry_incomplete_types (void);
4256 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4257 static void splice_child_die (dw_die_ref, dw_die_ref);
4258 static int file_info_cmp (const void *, const void *);
4259 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4260                                      const char *, const char *, unsigned);
4261 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4262                                        const char *, const char *,
4263                                        const char *);
4264 static void output_loc_list (dw_loc_list_ref);
4265 static char *gen_internal_sym (const char *);
4266
4267 static void prune_unmark_dies (dw_die_ref);
4268 static void prune_unused_types_mark (dw_die_ref, int);
4269 static void prune_unused_types_walk (dw_die_ref);
4270 static void prune_unused_types_walk_attribs (dw_die_ref);
4271 static void prune_unused_types_prune (dw_die_ref);
4272 static void prune_unused_types (void);
4273 static int maybe_emit_file (struct dwarf_file_data *fd);
4274
4275 /* Section names used to hold DWARF debugging information.  */
4276 #ifndef DEBUG_INFO_SECTION
4277 #define DEBUG_INFO_SECTION      ".debug_info"
4278 #endif
4279 #ifndef DEBUG_ABBREV_SECTION
4280 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4281 #endif
4282 #ifndef DEBUG_ARANGES_SECTION
4283 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4284 #endif
4285 #ifndef DEBUG_MACINFO_SECTION
4286 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4287 #endif
4288 #ifndef DEBUG_LINE_SECTION
4289 #define DEBUG_LINE_SECTION      ".debug_line"
4290 #endif
4291 #ifndef DEBUG_LOC_SECTION
4292 #define DEBUG_LOC_SECTION       ".debug_loc"
4293 #endif
4294 #ifndef DEBUG_PUBNAMES_SECTION
4295 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4296 #endif
4297 #ifndef DEBUG_STR_SECTION
4298 #define DEBUG_STR_SECTION       ".debug_str"
4299 #endif
4300 #ifndef DEBUG_RANGES_SECTION
4301 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4302 #endif
4303
4304 /* Standard ELF section names for compiled code and data.  */
4305 #ifndef TEXT_SECTION_NAME
4306 #define TEXT_SECTION_NAME       ".text"
4307 #endif
4308
4309 /* Section flags for .debug_str section.  */
4310 #define DEBUG_STR_SECTION_FLAGS \
4311   (HAVE_GAS_SHF_MERGE && flag_merge_constants                   \
4312    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4313    : SECTION_DEBUG)
4314
4315 /* Labels we insert at beginning sections we can reference instead of
4316    the section names themselves.  */
4317
4318 #ifndef TEXT_SECTION_LABEL
4319 #define TEXT_SECTION_LABEL              "Ltext"
4320 #endif
4321 #ifndef COLD_TEXT_SECTION_LABEL
4322 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4323 #endif
4324 #ifndef DEBUG_LINE_SECTION_LABEL
4325 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4326 #endif
4327 #ifndef DEBUG_INFO_SECTION_LABEL
4328 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4329 #endif
4330 #ifndef DEBUG_ABBREV_SECTION_LABEL
4331 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4332 #endif
4333 #ifndef DEBUG_LOC_SECTION_LABEL
4334 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4335 #endif
4336 #ifndef DEBUG_RANGES_SECTION_LABEL
4337 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4338 #endif
4339 #ifndef DEBUG_MACINFO_SECTION_LABEL
4340 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4341 #endif
4342
4343 /* Definitions of defaults for formats and names of various special
4344    (artificial) labels which may be generated within this file (when the -g
4345    options is used and DWARF2_DEBUGGING_INFO is in effect.
4346    If necessary, these may be overridden from within the tm.h file, but
4347    typically, overriding these defaults is unnecessary.  */
4348
4349 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4350 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4351 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4352 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4353 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4354 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4355 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4356 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4357 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4358 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4359
4360 #ifndef TEXT_END_LABEL
4361 #define TEXT_END_LABEL          "Letext"
4362 #endif
4363 #ifndef COLD_END_LABEL
4364 #define COLD_END_LABEL          "Letext_cold"
4365 #endif
4366 #ifndef BLOCK_BEGIN_LABEL
4367 #define BLOCK_BEGIN_LABEL       "LBB"
4368 #endif
4369 #ifndef BLOCK_END_LABEL
4370 #define BLOCK_END_LABEL         "LBE"
4371 #endif
4372 #ifndef LINE_CODE_LABEL
4373 #define LINE_CODE_LABEL         "LM"
4374 #endif
4375 #ifndef SEPARATE_LINE_CODE_LABEL
4376 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4377 #endif
4378 \f
4379 /* We allow a language front-end to designate a function that is to be
4380    called to "demangle" any name before it is put into a DIE.  */
4381
4382 static const char *(*demangle_name_func) (const char *);
4383
4384 void
4385 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4386 {
4387   demangle_name_func = func;
4388 }
4389
4390 /* Test if rtl node points to a pseudo register.  */
4391
4392 static inline int
4393 is_pseudo_reg (rtx rtl)
4394 {
4395   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4396           || (GET_CODE (rtl) == SUBREG
4397               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4398 }
4399
4400 /* Return a reference to a type, with its const and volatile qualifiers
4401    removed.  */
4402
4403 static inline tree
4404 type_main_variant (tree type)
4405 {
4406   type = TYPE_MAIN_VARIANT (type);
4407
4408   /* ??? There really should be only one main variant among any group of
4409      variants of a given type (and all of the MAIN_VARIANT values for all
4410      members of the group should point to that one type) but sometimes the C
4411      front-end messes this up for array types, so we work around that bug
4412      here.  */
4413   if (TREE_CODE (type) == ARRAY_TYPE)
4414     while (type != TYPE_MAIN_VARIANT (type))
4415       type = TYPE_MAIN_VARIANT (type);
4416
4417   return type;
4418 }
4419
4420 /* Return nonzero if the given type node represents a tagged type.  */
4421
4422 static inline int
4423 is_tagged_type (tree type)
4424 {
4425   enum tree_code code = TREE_CODE (type);
4426
4427   return (code == RECORD_TYPE || code == UNION_TYPE
4428           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4429 }
4430
4431 /* Convert a DIE tag into its string name.  */
4432
4433 static const char *
4434 dwarf_tag_name (unsigned int tag)
4435 {
4436   switch (tag)
4437     {
4438     case DW_TAG_padding:
4439       return "DW_TAG_padding";
4440     case DW_TAG_array_type:
4441       return "DW_TAG_array_type";
4442     case DW_TAG_class_type:
4443       return "DW_TAG_class_type";
4444     case DW_TAG_entry_point:
4445       return "DW_TAG_entry_point";
4446     case DW_TAG_enumeration_type:
4447       return "DW_TAG_enumeration_type";
4448     case DW_TAG_formal_parameter:
4449       return "DW_TAG_formal_parameter";
4450     case DW_TAG_imported_declaration:
4451       return "DW_TAG_imported_declaration";
4452     case DW_TAG_label:
4453       return "DW_TAG_label";
4454     case DW_TAG_lexical_block:
4455       return "DW_TAG_lexical_block";
4456     case DW_TAG_member:
4457       return "DW_TAG_member";
4458     case DW_TAG_pointer_type:
4459       return "DW_TAG_pointer_type";
4460     case DW_TAG_reference_type:
4461       return "DW_TAG_reference_type";
4462     case DW_TAG_compile_unit:
4463       return "DW_TAG_compile_unit";
4464     case DW_TAG_string_type:
4465       return "DW_TAG_string_type";
4466     case DW_TAG_structure_type:
4467       return "DW_TAG_structure_type";
4468     case DW_TAG_subroutine_type:
4469       return "DW_TAG_subroutine_type";
4470     case DW_TAG_typedef:
4471       return "DW_TAG_typedef";
4472     case DW_TAG_union_type:
4473       return "DW_TAG_union_type";
4474     case DW_TAG_unspecified_parameters:
4475       return "DW_TAG_unspecified_parameters";
4476     case DW_TAG_variant:
4477       return "DW_TAG_variant";
4478     case DW_TAG_common_block:
4479       return "DW_TAG_common_block";
4480     case DW_TAG_common_inclusion:
4481       return "DW_TAG_common_inclusion";
4482     case DW_TAG_inheritance:
4483       return "DW_TAG_inheritance";
4484     case DW_TAG_inlined_subroutine:
4485       return "DW_TAG_inlined_subroutine";
4486     case DW_TAG_module:
4487       return "DW_TAG_module";
4488     case DW_TAG_ptr_to_member_type:
4489       return "DW_TAG_ptr_to_member_type";
4490     case DW_TAG_set_type:
4491       return "DW_TAG_set_type";
4492     case DW_TAG_subrange_type:
4493       return "DW_TAG_subrange_type";
4494     case DW_TAG_with_stmt:
4495       return "DW_TAG_with_stmt";
4496     case DW_TAG_access_declaration:
4497       return "DW_TAG_access_declaration";
4498     case DW_TAG_base_type:
4499       return "DW_TAG_base_type";
4500     case DW_TAG_catch_block:
4501       return "DW_TAG_catch_block";
4502     case DW_TAG_const_type:
4503       return "DW_TAG_const_type";
4504     case DW_TAG_constant:
4505       return "DW_TAG_constant";
4506     case DW_TAG_enumerator:
4507       return "DW_TAG_enumerator";
4508     case DW_TAG_file_type:
4509       return "DW_TAG_file_type";
4510     case DW_TAG_friend:
4511       return "DW_TAG_friend";
4512     case DW_TAG_namelist:
4513       return "DW_TAG_namelist";
4514     case DW_TAG_namelist_item:
4515       return "DW_TAG_namelist_item";
4516     case DW_TAG_namespace:
4517       return "DW_TAG_namespace";
4518     case DW_TAG_packed_type:
4519       return "DW_TAG_packed_type";
4520     case DW_TAG_subprogram:
4521       return "DW_TAG_subprogram";
4522     case DW_TAG_template_type_param:
4523       return "DW_TAG_template_type_param";
4524     case DW_TAG_template_value_param:
4525       return "DW_TAG_template_value_param";
4526     case DW_TAG_thrown_type:
4527       return "DW_TAG_thrown_type";
4528     case DW_TAG_try_block:
4529       return "DW_TAG_try_block";
4530     case DW_TAG_variant_part:
4531       return "DW_TAG_variant_part";
4532     case DW_TAG_variable:
4533       return "DW_TAG_variable";
4534     case DW_TAG_volatile_type:
4535       return "DW_TAG_volatile_type";
4536     case DW_TAG_imported_module:
4537       return "DW_TAG_imported_module";
4538     case DW_TAG_MIPS_loop:
4539       return "DW_TAG_MIPS_loop";
4540     case DW_TAG_format_label:
4541       return "DW_TAG_format_label";
4542     case DW_TAG_function_template:
4543       return "DW_TAG_function_template";
4544     case DW_TAG_class_template:
4545       return "DW_TAG_class_template";
4546     case DW_TAG_GNU_BINCL:
4547       return "DW_TAG_GNU_BINCL";
4548     case DW_TAG_GNU_EINCL:
4549       return "DW_TAG_GNU_EINCL";
4550     default:
4551       return "DW_TAG_<unknown>";
4552     }
4553 }
4554
4555 /* Convert a DWARF attribute code into its string name.  */
4556
4557 static const char *
4558 dwarf_attr_name (unsigned int attr)
4559 {
4560   switch (attr)
4561     {
4562     case DW_AT_sibling:
4563       return "DW_AT_sibling";
4564     case DW_AT_location:
4565       return "DW_AT_location";
4566     case DW_AT_name:
4567       return "DW_AT_name";
4568     case DW_AT_ordering:
4569       return "DW_AT_ordering";
4570     case DW_AT_subscr_data:
4571       return "DW_AT_subscr_data";
4572     case DW_AT_byte_size:
4573       return "DW_AT_byte_size";
4574     case DW_AT_bit_offset:
4575       return "DW_AT_bit_offset";
4576     case DW_AT_bit_size:
4577       return "DW_AT_bit_size";
4578     case DW_AT_element_list:
4579       return "DW_AT_element_list";
4580     case DW_AT_stmt_list:
4581       return "DW_AT_stmt_list";
4582     case DW_AT_low_pc:
4583       return "DW_AT_low_pc";
4584     case DW_AT_high_pc:
4585       return "DW_AT_high_pc";
4586     case DW_AT_language:
4587       return "DW_AT_language";
4588     case DW_AT_member:
4589       return "DW_AT_member";
4590     case DW_AT_discr:
4591       return "DW_AT_discr";
4592     case DW_AT_discr_value:
4593       return "DW_AT_discr_value";
4594     case DW_AT_visibility:
4595       return "DW_AT_visibility";
4596     case DW_AT_import:
4597       return "DW_AT_import";
4598     case DW_AT_string_length:
4599       return "DW_AT_string_length";
4600     case DW_AT_common_reference:
4601       return "DW_AT_common_reference";
4602     case DW_AT_comp_dir:
4603       return "DW_AT_comp_dir";
4604     case DW_AT_const_value:
4605       return "DW_AT_const_value";
4606     case DW_AT_containing_type:
4607       return "DW_AT_containing_type";
4608     case DW_AT_default_value:
4609       return "DW_AT_default_value";
4610     case DW_AT_inline:
4611       return "DW_AT_inline";
4612     case DW_AT_is_optional:
4613       return "DW_AT_is_optional";
4614     case DW_AT_lower_bound:
4615       return "DW_AT_lower_bound";
4616     case DW_AT_producer:
4617       return "DW_AT_producer";
4618     case DW_AT_prototyped:
4619       return "DW_AT_prototyped";
4620     case DW_AT_return_addr:
4621       return "DW_AT_return_addr";
4622     case DW_AT_start_scope:
4623       return "DW_AT_start_scope";
4624     case DW_AT_stride_size:
4625       return "DW_AT_stride_size";
4626     case DW_AT_upper_bound:
4627       return "DW_AT_upper_bound";
4628     case DW_AT_abstract_origin:
4629       return "DW_AT_abstract_origin";
4630     case DW_AT_accessibility:
4631       return "DW_AT_accessibility";
4632     case DW_AT_address_class:
4633       return "DW_AT_address_class";
4634     case DW_AT_artificial:
4635       return "DW_AT_artificial";
4636     case DW_AT_base_types:
4637       return "DW_AT_base_types";
4638     case DW_AT_calling_convention:
4639       return "DW_AT_calling_convention";
4640     case DW_AT_count:
4641       return "DW_AT_count";
4642     case DW_AT_data_member_location:
4643       return "DW_AT_data_member_location";
4644     case DW_AT_decl_column:
4645       return "DW_AT_decl_column";
4646     case DW_AT_decl_file:
4647       return "DW_AT_decl_file";
4648     case DW_AT_decl_line:
4649       return "DW_AT_decl_line";
4650     case DW_AT_declaration:
4651       return "DW_AT_declaration";
4652     case DW_AT_discr_list:
4653       return "DW_AT_discr_list";
4654     case DW_AT_encoding:
4655       return "DW_AT_encoding";
4656     case DW_AT_external:
4657       return "DW_AT_external";
4658     case DW_AT_frame_base:
4659       return "DW_AT_frame_base";
4660     case DW_AT_friend:
4661       return "DW_AT_friend";
4662     case DW_AT_identifier_case:
4663       return "DW_AT_identifier_case";
4664     case DW_AT_macro_info:
4665       return "DW_AT_macro_info";
4666     case DW_AT_namelist_items:
4667       return "DW_AT_namelist_items";
4668     case DW_AT_priority:
4669       return "DW_AT_priority";
4670     case DW_AT_segment:
4671       return "DW_AT_segment";
4672     case DW_AT_specification:
4673       return "DW_AT_specification";
4674     case DW_AT_static_link:
4675       return "DW_AT_static_link";
4676     case DW_AT_type:
4677       return "DW_AT_type";
4678     case DW_AT_use_location:
4679       return "DW_AT_use_location";
4680     case DW_AT_variable_parameter:
4681       return "DW_AT_variable_parameter";
4682     case DW_AT_virtuality:
4683       return "DW_AT_virtuality";
4684     case DW_AT_vtable_elem_location:
4685       return "DW_AT_vtable_elem_location";
4686
4687     case DW_AT_allocated:
4688       return "DW_AT_allocated";
4689     case DW_AT_associated:
4690       return "DW_AT_associated";
4691     case DW_AT_data_location:
4692       return "DW_AT_data_location";
4693     case DW_AT_stride:
4694       return "DW_AT_stride";
4695     case DW_AT_entry_pc:
4696       return "DW_AT_entry_pc";
4697     case DW_AT_use_UTF8:
4698       return "DW_AT_use_UTF8";
4699     case DW_AT_extension:
4700       return "DW_AT_extension";
4701     case DW_AT_ranges:
4702       return "DW_AT_ranges";
4703     case DW_AT_trampoline:
4704       return "DW_AT_trampoline";
4705     case DW_AT_call_column:
4706       return "DW_AT_call_column";
4707     case DW_AT_call_file:
4708       return "DW_AT_call_file";
4709     case DW_AT_call_line:
4710       return "DW_AT_call_line";
4711
4712     case DW_AT_MIPS_fde:
4713       return "DW_AT_MIPS_fde";
4714     case DW_AT_MIPS_loop_begin:
4715       return "DW_AT_MIPS_loop_begin";
4716     case DW_AT_MIPS_tail_loop_begin:
4717       return "DW_AT_MIPS_tail_loop_begin";
4718     case DW_AT_MIPS_epilog_begin:
4719       return "DW_AT_MIPS_epilog_begin";
4720     case DW_AT_MIPS_loop_unroll_factor:
4721       return "DW_AT_MIPS_loop_unroll_factor";
4722     case DW_AT_MIPS_software_pipeline_depth:
4723       return "DW_AT_MIPS_software_pipeline_depth";
4724     case DW_AT_MIPS_linkage_name:
4725       return "DW_AT_MIPS_linkage_name";
4726     case DW_AT_MIPS_stride:
4727       return "DW_AT_MIPS_stride";
4728     case DW_AT_MIPS_abstract_name:
4729       return "DW_AT_MIPS_abstract_name";
4730     case DW_AT_MIPS_clone_origin:
4731       return "DW_AT_MIPS_clone_origin";
4732     case DW_AT_MIPS_has_inlines:
4733       return "DW_AT_MIPS_has_inlines";
4734
4735     case DW_AT_sf_names:
4736       return "DW_AT_sf_names";
4737     case DW_AT_src_info:
4738       return "DW_AT_src_info";
4739     case DW_AT_mac_info:
4740       return "DW_AT_mac_info";
4741     case DW_AT_src_coords:
4742       return "DW_AT_src_coords";
4743     case DW_AT_body_begin:
4744       return "DW_AT_body_begin";
4745     case DW_AT_body_end:
4746       return "DW_AT_body_end";
4747     case DW_AT_GNU_vector:
4748       return "DW_AT_GNU_vector";
4749
4750     case DW_AT_VMS_rtnbeg_pd_address:
4751       return "DW_AT_VMS_rtnbeg_pd_address";
4752
4753     default:
4754       return "DW_AT_<unknown>";
4755     }
4756 }
4757
4758 /* Convert a DWARF value form code into its string name.  */
4759
4760 static const char *
4761 dwarf_form_name (unsigned int form)
4762 {
4763   switch (form)
4764     {
4765     case DW_FORM_addr:
4766       return "DW_FORM_addr";
4767     case DW_FORM_block2:
4768       return "DW_FORM_block2";
4769     case DW_FORM_block4:
4770       return "DW_FORM_block4";
4771     case DW_FORM_data2:
4772       return "DW_FORM_data2";
4773     case DW_FORM_data4:
4774       return "DW_FORM_data4";
4775     case DW_FORM_data8:
4776       return "DW_FORM_data8";
4777     case DW_FORM_string:
4778       return "DW_FORM_string";
4779     case DW_FORM_block:
4780       return "DW_FORM_block";
4781     case DW_FORM_block1:
4782       return "DW_FORM_block1";
4783     case DW_FORM_data1:
4784       return "DW_FORM_data1";
4785     case DW_FORM_flag:
4786       return "DW_FORM_flag";
4787     case DW_FORM_sdata:
4788       return "DW_FORM_sdata";
4789     case DW_FORM_strp:
4790       return "DW_FORM_strp";
4791     case DW_FORM_udata:
4792       return "DW_FORM_udata";
4793     case DW_FORM_ref_addr:
4794       return "DW_FORM_ref_addr";
4795     case DW_FORM_ref1:
4796       return "DW_FORM_ref1";
4797     case DW_FORM_ref2:
4798       return "DW_FORM_ref2";
4799     case DW_FORM_ref4:
4800       return "DW_FORM_ref4";
4801     case DW_FORM_ref8:
4802       return "DW_FORM_ref8";
4803     case DW_FORM_ref_udata:
4804       return "DW_FORM_ref_udata";
4805     case DW_FORM_indirect:
4806       return "DW_FORM_indirect";
4807     default:
4808       return "DW_FORM_<unknown>";
4809     }
4810 }
4811 \f
4812 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4813    instance of an inlined instance of a decl which is local to an inline
4814    function, so we have to trace all of the way back through the origin chain
4815    to find out what sort of node actually served as the original seed for the
4816    given block.  */
4817
4818 static tree
4819 decl_ultimate_origin (tree decl)
4820 {
4821   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4822     return NULL_TREE;
4823
4824   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4825      nodes in the function to point to themselves; ignore that if
4826      we're trying to output the abstract instance of this function.  */
4827   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4828     return NULL_TREE;
4829
4830   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4831      most distant ancestor, this should never happen.  */
4832   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4833
4834   return DECL_ABSTRACT_ORIGIN (decl);
4835 }
4836
4837 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4838    instance of an inlined instance of a block which is local to an inline
4839    function, so we have to trace all of the way back through the origin chain
4840    to find out what sort of node actually served as the original seed for the
4841    given block.  */
4842
4843 static tree
4844 block_ultimate_origin (tree block)
4845 {
4846   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4847
4848   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4849      nodes in the function to point to themselves; ignore that if
4850      we're trying to output the abstract instance of this function.  */
4851   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4852     return NULL_TREE;
4853
4854   if (immediate_origin == NULL_TREE)
4855     return NULL_TREE;
4856   else
4857     {
4858       tree ret_val;
4859       tree lookahead = immediate_origin;
4860
4861       do
4862         {
4863           ret_val = lookahead;
4864           lookahead = (TREE_CODE (ret_val) == BLOCK
4865                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4866         }
4867       while (lookahead != NULL && lookahead != ret_val);
4868
4869       /* The block's abstract origin chain may not be the *ultimate* origin of
4870          the block. It could lead to a DECL that has an abstract origin set.
4871          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4872          will give us if it has one).  Note that DECL's abstract origins are
4873          supposed to be the most distant ancestor (or so decl_ultimate_origin
4874          claims), so we don't need to loop following the DECL origins.  */
4875       if (DECL_P (ret_val))
4876         return DECL_ORIGIN (ret_val);
4877
4878       return ret_val;
4879     }
4880 }
4881
4882 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4883    of a virtual function may refer to a base class, so we check the 'this'
4884    parameter.  */
4885
4886 static tree
4887 decl_class_context (tree decl)
4888 {
4889   tree context = NULL_TREE;
4890
4891   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4892     context = DECL_CONTEXT (decl);
4893   else
4894     context = TYPE_MAIN_VARIANT
4895       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4896
4897   if (context && !TYPE_P (context))
4898     context = NULL_TREE;
4899
4900   return context;
4901 }
4902 \f
4903 /* Add an attribute/value pair to a DIE.  */
4904
4905 static inline void
4906 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4907 {
4908   /* Maybe this should be an assert?  */
4909   if (die == NULL)
4910     return;
4911
4912   if (die->die_attr == NULL)
4913     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4914   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4915 }
4916
4917 static inline enum dw_val_class
4918 AT_class (dw_attr_ref a)
4919 {
4920   return a->dw_attr_val.val_class;
4921 }
4922
4923 /* Add a flag value attribute to a DIE.  */
4924
4925 static inline void
4926 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4927 {
4928   dw_attr_node attr;
4929
4930   attr.dw_attr = attr_kind;
4931   attr.dw_attr_val.val_class = dw_val_class_flag;
4932   attr.dw_attr_val.v.val_flag = flag;
4933   add_dwarf_attr (die, &attr);
4934 }
4935
4936 static inline unsigned
4937 AT_flag (dw_attr_ref a)
4938 {
4939   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4940   return a->dw_attr_val.v.val_flag;
4941 }
4942
4943 /* Add a signed integer attribute value to a DIE.  */
4944
4945 static inline void
4946 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4947 {
4948   dw_attr_node attr;
4949
4950   attr.dw_attr = attr_kind;
4951   attr.dw_attr_val.val_class = dw_val_class_const;
4952   attr.dw_attr_val.v.val_int = int_val;
4953   add_dwarf_attr (die, &attr);
4954 }
4955
4956 static inline HOST_WIDE_INT
4957 AT_int (dw_attr_ref a)
4958 {
4959   gcc_assert (a && AT_class (a) == dw_val_class_const);
4960   return a->dw_attr_val.v.val_int;
4961 }
4962
4963 /* Add an unsigned integer attribute value to a DIE.  */
4964
4965 static inline void
4966 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4967                  unsigned HOST_WIDE_INT unsigned_val)
4968 {
4969   dw_attr_node attr;
4970
4971   attr.dw_attr = attr_kind;
4972   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4973   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4974   add_dwarf_attr (die, &attr);
4975 }
4976
4977 static inline unsigned HOST_WIDE_INT
4978 AT_unsigned (dw_attr_ref a)
4979 {
4980   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4981   return a->dw_attr_val.v.val_unsigned;
4982 }
4983
4984 /* Add an unsigned double integer attribute value to a DIE.  */
4985
4986 static inline void
4987 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
4988                   long unsigned int val_hi, long unsigned int val_low)
4989 {
4990   dw_attr_node attr;
4991
4992   attr.dw_attr = attr_kind;
4993   attr.dw_attr_val.val_class = dw_val_class_long_long;
4994   attr.dw_attr_val.v.val_long_long.hi = val_hi;
4995   attr.dw_attr_val.v.val_long_long.low = val_low;
4996   add_dwarf_attr (die, &attr);
4997 }
4998
4999 /* Add a floating point attribute value to a DIE and return it.  */
5000
5001 static inline void
5002 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5003             unsigned int length, unsigned int elt_size, unsigned char *array)
5004 {
5005   dw_attr_node attr;
5006
5007   attr.dw_attr = attr_kind;
5008   attr.dw_attr_val.val_class = dw_val_class_vec;
5009   attr.dw_attr_val.v.val_vec.length = length;
5010   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5011   attr.dw_attr_val.v.val_vec.array = array;
5012   add_dwarf_attr (die, &attr);
5013 }
5014
5015 /* Hash and equality functions for debug_str_hash.  */
5016
5017 static hashval_t
5018 debug_str_do_hash (const void *x)
5019 {
5020   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5021 }
5022
5023 static int
5024 debug_str_eq (const void *x1, const void *x2)
5025 {
5026   return strcmp ((((const struct indirect_string_node *)x1)->str),
5027                  (const char *)x2) == 0;
5028 }
5029
5030 /* Add a string attribute value to a DIE.  */
5031
5032 static inline void
5033 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5034 {
5035   dw_attr_node attr;
5036   struct indirect_string_node *node;
5037   void **slot;
5038
5039   if (! debug_str_hash)
5040     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5041                                       debug_str_eq, NULL);
5042
5043   slot = htab_find_slot_with_hash (debug_str_hash, str,
5044                                    htab_hash_string (str), INSERT);
5045   if (*slot == NULL)
5046     {
5047       node = (struct indirect_string_node *)
5048                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5049       node->str = ggc_strdup (str);
5050       *slot = node;
5051     }
5052   else
5053     node = (struct indirect_string_node *) *slot;
5054
5055   node->refcount++;
5056
5057   attr.dw_attr = attr_kind;
5058   attr.dw_attr_val.val_class = dw_val_class_str;
5059   attr.dw_attr_val.v.val_str = node;
5060   add_dwarf_attr (die, &attr);
5061 }
5062
5063 static inline const char *
5064 AT_string (dw_attr_ref a)
5065 {
5066   gcc_assert (a && AT_class (a) == dw_val_class_str);
5067   return a->dw_attr_val.v.val_str->str;
5068 }
5069
5070 /* Find out whether a string should be output inline in DIE
5071    or out-of-line in .debug_str section.  */
5072
5073 static int
5074 AT_string_form (dw_attr_ref a)
5075 {
5076   struct indirect_string_node *node;
5077   unsigned int len;
5078   char label[32];
5079
5080   gcc_assert (a && AT_class (a) == dw_val_class_str);
5081
5082   node = a->dw_attr_val.v.val_str;
5083   if (node->form)
5084     return node->form;
5085
5086   len = strlen (node->str) + 1;
5087
5088   /* If the string is shorter or equal to the size of the reference, it is
5089      always better to put it inline.  */
5090   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5091     return node->form = DW_FORM_string;
5092
5093   /* If we cannot expect the linker to merge strings in .debug_str
5094      section, only put it into .debug_str if it is worth even in this
5095      single module.  */
5096   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5097       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5098     return node->form = DW_FORM_string;
5099
5100   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5101   ++dw2_string_counter;
5102   node->label = xstrdup (label);
5103
5104   return node->form = DW_FORM_strp;
5105 }
5106
5107 /* Add a DIE reference attribute value to a DIE.  */
5108
5109 static inline void
5110 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5111 {
5112   dw_attr_node attr;
5113
5114   attr.dw_attr = attr_kind;
5115   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5116   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5117   attr.dw_attr_val.v.val_die_ref.external = 0;
5118   add_dwarf_attr (die, &attr);
5119 }
5120
5121 /* Add an AT_specification attribute to a DIE, and also make the back
5122    pointer from the specification to the definition.  */
5123
5124 static inline void
5125 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5126 {
5127   add_AT_die_ref (die, DW_AT_specification, targ_die);
5128   gcc_assert (!targ_die->die_definition);
5129   targ_die->die_definition = die;
5130 }
5131
5132 static inline dw_die_ref
5133 AT_ref (dw_attr_ref a)
5134 {
5135   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5136   return a->dw_attr_val.v.val_die_ref.die;
5137 }
5138
5139 static inline int
5140 AT_ref_external (dw_attr_ref a)
5141 {
5142   if (a && AT_class (a) == dw_val_class_die_ref)
5143     return a->dw_attr_val.v.val_die_ref.external;
5144
5145   return 0;
5146 }
5147
5148 static inline void
5149 set_AT_ref_external (dw_attr_ref a, int i)
5150 {
5151   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5152   a->dw_attr_val.v.val_die_ref.external = i;
5153 }
5154
5155 /* Add an FDE reference attribute value to a DIE.  */
5156
5157 static inline void
5158 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5159 {
5160   dw_attr_node attr;
5161
5162   attr.dw_attr = attr_kind;
5163   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5164   attr.dw_attr_val.v.val_fde_index = targ_fde;
5165   add_dwarf_attr (die, &attr);
5166 }
5167
5168 /* Add a location description attribute value to a DIE.  */
5169
5170 static inline void
5171 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5172 {
5173   dw_attr_node attr;
5174
5175   attr.dw_attr = attr_kind;
5176   attr.dw_attr_val.val_class = dw_val_class_loc;
5177   attr.dw_attr_val.v.val_loc = loc;
5178   add_dwarf_attr (die, &attr);
5179 }
5180
5181 static inline dw_loc_descr_ref
5182 AT_loc (dw_attr_ref a)
5183 {
5184   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5185   return a->dw_attr_val.v.val_loc;
5186 }
5187
5188 static inline void
5189 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5190 {
5191   dw_attr_node attr;
5192
5193   attr.dw_attr = attr_kind;
5194   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5195   attr.dw_attr_val.v.val_loc_list = loc_list;
5196   add_dwarf_attr (die, &attr);
5197   have_location_lists = true;
5198 }
5199
5200 static inline dw_loc_list_ref
5201 AT_loc_list (dw_attr_ref a)
5202 {
5203   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5204   return a->dw_attr_val.v.val_loc_list;
5205 }
5206
5207 /* Add an address constant attribute value to a DIE.  */
5208
5209 static inline void
5210 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5211 {
5212   dw_attr_node attr;
5213
5214   attr.dw_attr = attr_kind;
5215   attr.dw_attr_val.val_class = dw_val_class_addr;
5216   attr.dw_attr_val.v.val_addr = addr;
5217   add_dwarf_attr (die, &attr);
5218 }
5219
5220 /* Get the RTX from to an address DIE attribute.  */
5221
5222 static inline rtx
5223 AT_addr (dw_attr_ref a)
5224 {
5225   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5226   return a->dw_attr_val.v.val_addr;
5227 }
5228
5229 /* Add a file attribute value to a DIE.  */
5230
5231 static inline void
5232 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5233              struct dwarf_file_data *fd)
5234 {
5235   dw_attr_node attr;
5236
5237   attr.dw_attr = attr_kind;
5238   attr.dw_attr_val.val_class = dw_val_class_file;
5239   attr.dw_attr_val.v.val_file = fd;
5240   add_dwarf_attr (die, &attr);
5241 }
5242
5243 /* Get the dwarf_file_data from a file DIE attribute.  */
5244
5245 static inline struct dwarf_file_data *
5246 AT_file (dw_attr_ref a)
5247 {
5248   gcc_assert (a && AT_class (a) == dw_val_class_file);
5249   return a->dw_attr_val.v.val_file;
5250 }
5251
5252 /* Add a label identifier attribute value to a DIE.  */
5253
5254 static inline void
5255 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5256 {
5257   dw_attr_node attr;
5258
5259   attr.dw_attr = attr_kind;
5260   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5261   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5262   add_dwarf_attr (die, &attr);
5263 }
5264
5265 /* Add a section offset attribute value to a DIE, an offset into the
5266    debug_line section.  */
5267
5268 static inline void
5269 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5270                 const char *label)
5271 {
5272   dw_attr_node attr;
5273
5274   attr.dw_attr = attr_kind;
5275   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5276   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5277   add_dwarf_attr (die, &attr);
5278 }
5279
5280 /* Add a section offset attribute value to a DIE, an offset into the
5281    debug_macinfo section.  */
5282
5283 static inline void
5284 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5285                const char *label)
5286 {
5287   dw_attr_node attr;
5288
5289   attr.dw_attr = attr_kind;
5290   attr.dw_attr_val.val_class = dw_val_class_macptr;
5291   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5292   add_dwarf_attr (die, &attr);
5293 }
5294
5295 /* Add an offset attribute value to a DIE.  */
5296
5297 static inline void
5298 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5299                unsigned HOST_WIDE_INT offset)
5300 {
5301   dw_attr_node attr;
5302
5303   attr.dw_attr = attr_kind;
5304   attr.dw_attr_val.val_class = dw_val_class_offset;
5305   attr.dw_attr_val.v.val_offset = offset;
5306   add_dwarf_attr (die, &attr);
5307 }
5308
5309 /* Add an range_list attribute value to a DIE.  */
5310
5311 static void
5312 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5313                    long unsigned int offset)
5314 {
5315   dw_attr_node attr;
5316
5317   attr.dw_attr = attr_kind;
5318   attr.dw_attr_val.val_class = dw_val_class_range_list;
5319   attr.dw_attr_val.v.val_offset = offset;
5320   add_dwarf_attr (die, &attr);
5321 }
5322
5323 static inline const char *
5324 AT_lbl (dw_attr_ref a)
5325 {
5326   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5327                     || AT_class (a) == dw_val_class_lineptr
5328                     || AT_class (a) == dw_val_class_macptr));
5329   return a->dw_attr_val.v.val_lbl_id;
5330 }
5331
5332 /* Get the attribute of type attr_kind.  */
5333
5334 static dw_attr_ref
5335 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5336 {
5337   dw_attr_ref a;
5338   unsigned ix;
5339   dw_die_ref spec = NULL;
5340
5341   if (! die)
5342     return NULL;
5343
5344   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5345     if (a->dw_attr == attr_kind)
5346       return a;
5347     else if (a->dw_attr == DW_AT_specification
5348              || a->dw_attr == DW_AT_abstract_origin)
5349       spec = AT_ref (a);
5350
5351   if (spec)
5352     return get_AT (spec, attr_kind);
5353
5354   return NULL;
5355 }
5356
5357 /* Return the "low pc" attribute value, typically associated with a subprogram
5358    DIE.  Return null if the "low pc" attribute is either not present, or if it
5359    cannot be represented as an assembler label identifier.  */
5360
5361 static inline const char *
5362 get_AT_low_pc (dw_die_ref die)
5363 {
5364   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5365
5366   return a ? AT_lbl (a) : NULL;
5367 }
5368
5369 /* Return the "high pc" attribute value, typically associated with a subprogram
5370    DIE.  Return null if the "high pc" attribute is either not present, or if it
5371    cannot be represented as an assembler label identifier.  */
5372
5373 static inline const char *
5374 get_AT_hi_pc (dw_die_ref die)
5375 {
5376   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5377
5378   return a ? AT_lbl (a) : NULL;
5379 }
5380
5381 /* Return the value of the string attribute designated by ATTR_KIND, or
5382    NULL if it is not present.  */
5383
5384 static inline const char *
5385 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5386 {
5387   dw_attr_ref a = get_AT (die, attr_kind);
5388
5389   return a ? AT_string (a) : NULL;
5390 }
5391
5392 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5393    if it is not present.  */
5394
5395 static inline int
5396 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5397 {
5398   dw_attr_ref a = get_AT (die, attr_kind);
5399
5400   return a ? AT_flag (a) : 0;
5401 }
5402
5403 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5404    if it is not present.  */
5405
5406 static inline unsigned
5407 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5408 {
5409   dw_attr_ref a = get_AT (die, attr_kind);
5410
5411   return a ? AT_unsigned (a) : 0;
5412 }
5413
5414 static inline dw_die_ref
5415 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5416 {
5417   dw_attr_ref a = get_AT (die, attr_kind);
5418
5419   return a ? AT_ref (a) : NULL;
5420 }
5421
5422 static inline struct dwarf_file_data *
5423 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5424 {
5425   dw_attr_ref a = get_AT (die, attr_kind);
5426
5427   return a ? AT_file (a) : NULL;
5428 }
5429
5430 /* Return TRUE if the language is C or C++.  */
5431
5432 static inline bool
5433 is_c_family (void)
5434 {
5435   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5436
5437   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5438           || lang == DW_LANG_C99
5439           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5440 }
5441
5442 /* Return TRUE if the language is C++.  */
5443
5444 static inline bool
5445 is_cxx (void)
5446 {
5447   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5448
5449   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5450 }
5451
5452 /* Return TRUE if the language is Fortran.  */
5453
5454 static inline bool
5455 is_fortran (void)
5456 {
5457   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5458
5459   return (lang == DW_LANG_Fortran77
5460           || lang == DW_LANG_Fortran90
5461           || lang == DW_LANG_Fortran95);
5462 }
5463
5464 /* Return TRUE if the language is Java.  */
5465
5466 static inline bool
5467 is_java (void)
5468 {
5469   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5470
5471   return lang == DW_LANG_Java;
5472 }
5473
5474 /* Return TRUE if the language is Ada.  */
5475
5476 static inline bool
5477 is_ada (void)
5478 {
5479   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5480
5481   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5482 }
5483
5484 /* Remove the specified attribute if present.  */
5485
5486 static void
5487 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5488 {
5489   dw_attr_ref a;
5490   unsigned ix;
5491
5492   if (! die)
5493     return;
5494
5495   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5496     if (a->dw_attr == attr_kind)
5497       {
5498         if (AT_class (a) == dw_val_class_str)
5499           if (a->dw_attr_val.v.val_str->refcount)
5500             a->dw_attr_val.v.val_str->refcount--;
5501
5502         /* VEC_ordered_remove should help reduce the number of abbrevs
5503            that are needed.  */
5504         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5505         return;
5506       }
5507 }
5508
5509 /* Remove CHILD from its parent.  PREV must have the property that
5510    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5511
5512 static void
5513 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5514 {
5515   gcc_assert (child->die_parent == prev->die_parent);
5516   gcc_assert (prev->die_sib == child);
5517   if (prev == child)
5518     {
5519       gcc_assert (child->die_parent->die_child == child);
5520       prev = NULL;
5521     }
5522   else
5523     prev->die_sib = child->die_sib;
5524   if (child->die_parent->die_child == child)
5525     child->die_parent->die_child = prev;
5526 }
5527
5528 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5529    matches TAG.  */
5530
5531 static void
5532 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5533 {
5534   dw_die_ref c;
5535
5536   c = die->die_child;
5537   if (c) do {
5538     dw_die_ref prev = c;
5539     c = c->die_sib;
5540     while (c->die_tag == tag)
5541       {
5542         remove_child_with_prev (c, prev);
5543         /* Might have removed every child.  */
5544         if (c == c->die_sib)
5545           return;
5546         c = c->die_sib;
5547       }
5548   } while (c != die->die_child);
5549 }
5550
5551 /* Add a CHILD_DIE as the last child of DIE.  */
5552
5553 static void
5554 add_child_die (dw_die_ref die, dw_die_ref child_die)
5555 {
5556   /* FIXME this should probably be an assert.  */
5557   if (! die || ! child_die)
5558     return;
5559   gcc_assert (die != child_die);
5560
5561   child_die->die_parent = die;
5562   if (die->die_child)
5563     {
5564       child_die->die_sib = die->die_child->die_sib;
5565       die->die_child->die_sib = child_die;
5566     }
5567   else
5568     child_die->die_sib = child_die;
5569   die->die_child = child_die;
5570 }
5571
5572 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5573    is the specification, to the end of PARENT's list of children.
5574    This is done by removing and re-adding it.  */
5575
5576 static void
5577 splice_child_die (dw_die_ref parent, dw_die_ref child)
5578 {
5579   dw_die_ref p;
5580
5581   /* We want the declaration DIE from inside the class, not the
5582      specification DIE at toplevel.  */
5583   if (child->die_parent != parent)
5584     {
5585       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5586
5587       if (tmp)
5588         child = tmp;
5589     }
5590
5591   gcc_assert (child->die_parent == parent
5592               || (child->die_parent
5593                   == get_AT_ref (parent, DW_AT_specification)));
5594
5595   for (p = child->die_parent->die_child; ; p = p->die_sib)
5596     if (p->die_sib == child)
5597       {
5598         remove_child_with_prev (child, p);
5599         break;
5600       }
5601
5602   add_child_die (parent, child);
5603 }
5604
5605 /* Return a pointer to a newly created DIE node.  */
5606
5607 static inline dw_die_ref
5608 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5609 {
5610   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5611
5612   die->die_tag = tag_value;
5613
5614   if (parent_die != NULL)
5615     add_child_die (parent_die, die);
5616   else
5617     {
5618       limbo_die_node *limbo_node;
5619
5620       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5621       limbo_node->die = die;
5622       limbo_node->created_for = t;
5623       limbo_node->next = limbo_die_list;
5624       limbo_die_list = limbo_node;
5625     }
5626
5627   return die;
5628 }
5629
5630 /* Return the DIE associated with the given type specifier.  */
5631
5632 static inline dw_die_ref
5633 lookup_type_die (tree type)
5634 {
5635   return TYPE_SYMTAB_DIE (type);
5636 }
5637
5638 /* Equate a DIE to a given type specifier.  */
5639
5640 static inline void
5641 equate_type_number_to_die (tree type, dw_die_ref type_die)
5642 {
5643   TYPE_SYMTAB_DIE (type) = type_die;
5644 }
5645
5646 /* Returns a hash value for X (which really is a die_struct).  */
5647
5648 static hashval_t
5649 decl_die_table_hash (const void *x)
5650 {
5651   return (hashval_t) ((const dw_die_ref) x)->decl_id;
5652 }
5653
5654 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5655
5656 static int
5657 decl_die_table_eq (const void *x, const void *y)
5658 {
5659   return (((const dw_die_ref) x)->decl_id == DECL_UID ((const tree) y));
5660 }
5661
5662 /* Return the DIE associated with a given declaration.  */
5663
5664 static inline dw_die_ref
5665 lookup_decl_die (tree decl)
5666 {
5667   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5668 }
5669
5670 /* Returns a hash value for X (which really is a var_loc_list).  */
5671
5672 static hashval_t
5673 decl_loc_table_hash (const void *x)
5674 {
5675   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5676 }
5677
5678 /* Return nonzero if decl_id of var_loc_list X is the same as
5679    UID of decl *Y.  */
5680
5681 static int
5682 decl_loc_table_eq (const void *x, const void *y)
5683 {
5684   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const tree) y));
5685 }
5686
5687 /* Return the var_loc list associated with a given declaration.  */
5688
5689 static inline var_loc_list *
5690 lookup_decl_loc (tree decl)
5691 {
5692   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5693 }
5694
5695 /* Equate a DIE to a particular declaration.  */
5696
5697 static void
5698 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5699 {
5700   unsigned int decl_id = DECL_UID (decl);
5701   void **slot;
5702
5703   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5704   *slot = decl_die;
5705   decl_die->decl_id = decl_id;
5706 }
5707
5708 /* Add a variable location node to the linked list for DECL.  */
5709
5710 static void
5711 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5712 {
5713   unsigned int decl_id = DECL_UID (decl);
5714   var_loc_list *temp;
5715   void **slot;
5716
5717   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5718   if (*slot == NULL)
5719     {
5720       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5721       temp->decl_id = decl_id;
5722       *slot = temp;
5723     }
5724   else
5725     temp = *slot;
5726
5727   if (temp->last)
5728     {
5729       /* If the current location is the same as the end of the list,
5730          we have nothing to do.  */
5731       if (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5732                         NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5733         {
5734           /* Add LOC to the end of list and update LAST.  */
5735           temp->last->next = loc;
5736           temp->last = loc;
5737         }
5738     }
5739   /* Do not add empty location to the beginning of the list.  */
5740   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5741     {
5742       temp->first = loc;
5743       temp->last = loc;
5744     }
5745 }
5746 \f
5747 /* Keep track of the number of spaces used to indent the
5748    output of the debugging routines that print the structure of
5749    the DIE internal representation.  */
5750 static int print_indent;
5751
5752 /* Indent the line the number of spaces given by print_indent.  */
5753
5754 static inline void
5755 print_spaces (FILE *outfile)
5756 {
5757   fprintf (outfile, "%*s", print_indent, "");
5758 }
5759
5760 /* Print the information associated with a given DIE, and its children.
5761    This routine is a debugging aid only.  */
5762
5763 static void
5764 print_die (dw_die_ref die, FILE *outfile)
5765 {
5766   dw_attr_ref a;
5767   dw_die_ref c;
5768   unsigned ix;
5769
5770   print_spaces (outfile);
5771   fprintf (outfile, "DIE %4ld: %s\n",
5772            die->die_offset, dwarf_tag_name (die->die_tag));
5773   print_spaces (outfile);
5774   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5775   fprintf (outfile, " offset: %ld\n", die->die_offset);
5776
5777   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5778     {
5779       print_spaces (outfile);
5780       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5781
5782       switch (AT_class (a))
5783         {
5784         case dw_val_class_addr:
5785           fprintf (outfile, "address");
5786           break;
5787         case dw_val_class_offset:
5788           fprintf (outfile, "offset");
5789           break;
5790         case dw_val_class_loc:
5791           fprintf (outfile, "location descriptor");
5792           break;
5793         case dw_val_class_loc_list:
5794           fprintf (outfile, "location list -> label:%s",
5795                    AT_loc_list (a)->ll_symbol);
5796           break;
5797         case dw_val_class_range_list:
5798           fprintf (outfile, "range list");
5799           break;
5800         case dw_val_class_const:
5801           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5802           break;
5803         case dw_val_class_unsigned_const:
5804           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5805           break;
5806         case dw_val_class_long_long:
5807           fprintf (outfile, "constant (%lu,%lu)",
5808                    a->dw_attr_val.v.val_long_long.hi,
5809                    a->dw_attr_val.v.val_long_long.low);
5810           break;
5811         case dw_val_class_vec:
5812           fprintf (outfile, "floating-point or vector constant");
5813           break;
5814         case dw_val_class_flag:
5815           fprintf (outfile, "%u", AT_flag (a));
5816           break;
5817         case dw_val_class_die_ref:
5818           if (AT_ref (a) != NULL)
5819             {
5820               if (AT_ref (a)->die_symbol)
5821                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5822               else
5823                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5824             }
5825           else
5826             fprintf (outfile, "die -> <null>");
5827           break;
5828         case dw_val_class_lbl_id:
5829         case dw_val_class_lineptr:
5830         case dw_val_class_macptr:
5831           fprintf (outfile, "label: %s", AT_lbl (a));
5832           break;
5833         case dw_val_class_str:
5834           if (AT_string (a) != NULL)
5835             fprintf (outfile, "\"%s\"", AT_string (a));
5836           else
5837             fprintf (outfile, "<null>");
5838           break;
5839         case dw_val_class_file:
5840           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5841                    AT_file (a)->emitted_number);
5842           break;
5843         default:
5844           break;
5845         }
5846
5847       fprintf (outfile, "\n");
5848     }
5849
5850   if (die->die_child != NULL)
5851     {
5852       print_indent += 4;
5853       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5854       print_indent -= 4;
5855     }
5856   if (print_indent == 0)
5857     fprintf (outfile, "\n");
5858 }
5859
5860 /* Print the contents of the source code line number correspondence table.
5861    This routine is a debugging aid only.  */
5862
5863 static void
5864 print_dwarf_line_table (FILE *outfile)
5865 {
5866   unsigned i;
5867   dw_line_info_ref line_info;
5868
5869   fprintf (outfile, "\n\nDWARF source line information\n");
5870   for (i = 1; i < line_info_table_in_use; i++)
5871     {
5872       line_info = &line_info_table[i];
5873       fprintf (outfile, "%5d: %4ld %6ld\n", i,
5874                line_info->dw_file_num,
5875                line_info->dw_line_num);
5876     }
5877
5878   fprintf (outfile, "\n\n");
5879 }
5880
5881 /* Print the information collected for a given DIE.  */
5882
5883 void
5884 debug_dwarf_die (dw_die_ref die)
5885 {
5886   print_die (die, stderr);
5887 }
5888
5889 /* Print all DWARF information collected for the compilation unit.
5890    This routine is a debugging aid only.  */
5891
5892 void
5893 debug_dwarf (void)
5894 {
5895   print_indent = 0;
5896   print_die (comp_unit_die, stderr);
5897   if (! DWARF2_ASM_LINE_DEBUG_INFO)
5898     print_dwarf_line_table (stderr);
5899 }
5900 \f
5901 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5902    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5903    DIE that marks the start of the DIEs for this include file.  */
5904
5905 static dw_die_ref
5906 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5907 {
5908   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5909   dw_die_ref new_unit = gen_compile_unit_die (filename);
5910
5911   new_unit->die_sib = old_unit;
5912   return new_unit;
5913 }
5914
5915 /* Close an include-file CU and reopen the enclosing one.  */
5916
5917 static dw_die_ref
5918 pop_compile_unit (dw_die_ref old_unit)
5919 {
5920   dw_die_ref new_unit = old_unit->die_sib;
5921
5922   old_unit->die_sib = NULL;
5923   return new_unit;
5924 }
5925
5926 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5927 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5928
5929 /* Calculate the checksum of a location expression.  */
5930
5931 static inline void
5932 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5933 {
5934   CHECKSUM (loc->dw_loc_opc);
5935   CHECKSUM (loc->dw_loc_oprnd1);
5936   CHECKSUM (loc->dw_loc_oprnd2);
5937 }
5938
5939 /* Calculate the checksum of an attribute.  */
5940
5941 static void
5942 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5943 {
5944   dw_loc_descr_ref loc;
5945   rtx r;
5946
5947   CHECKSUM (at->dw_attr);
5948
5949   /* We don't care that this was compiled with a different compiler
5950      snapshot; if the output is the same, that's what matters.  */
5951   if (at->dw_attr == DW_AT_producer)
5952     return;
5953
5954   switch (AT_class (at))
5955     {
5956     case dw_val_class_const:
5957       CHECKSUM (at->dw_attr_val.v.val_int);
5958       break;
5959     case dw_val_class_unsigned_const:
5960       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5961       break;
5962     case dw_val_class_long_long:
5963       CHECKSUM (at->dw_attr_val.v.val_long_long);
5964       break;
5965     case dw_val_class_vec:
5966       CHECKSUM (at->dw_attr_val.v.val_vec);
5967       break;
5968     case dw_val_class_flag:
5969       CHECKSUM (at->dw_attr_val.v.val_flag);
5970       break;
5971     case dw_val_class_str:
5972       CHECKSUM_STRING (AT_string (at));
5973       break;
5974
5975     case dw_val_class_addr:
5976       r = AT_addr (at);
5977       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5978       CHECKSUM_STRING (XSTR (r, 0));
5979       break;
5980
5981     case dw_val_class_offset:
5982       CHECKSUM (at->dw_attr_val.v.val_offset);
5983       break;
5984
5985     case dw_val_class_loc:
5986       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5987         loc_checksum (loc, ctx);
5988       break;
5989
5990     case dw_val_class_die_ref:
5991       die_checksum (AT_ref (at), ctx, mark);
5992       break;
5993
5994     case dw_val_class_fde_ref:
5995     case dw_val_class_lbl_id:
5996     case dw_val_class_lineptr:
5997     case dw_val_class_macptr:
5998       break;
5999
6000     case dw_val_class_file:
6001       CHECKSUM_STRING (AT_file (at)->filename);
6002       break;
6003
6004     default:
6005       break;
6006     }
6007 }
6008
6009 /* Calculate the checksum of a DIE.  */
6010
6011 static void
6012 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6013 {
6014   dw_die_ref c;
6015   dw_attr_ref a;
6016   unsigned ix;
6017
6018   /* To avoid infinite recursion.  */
6019   if (die->die_mark)
6020     {
6021       CHECKSUM (die->die_mark);
6022       return;
6023     }
6024   die->die_mark = ++(*mark);
6025
6026   CHECKSUM (die->die_tag);
6027
6028   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6029     attr_checksum (a, ctx, mark);
6030
6031   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6032 }
6033
6034 #undef CHECKSUM
6035 #undef CHECKSUM_STRING
6036
6037 /* Do the location expressions look same?  */
6038 static inline int
6039 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6040 {
6041   return loc1->dw_loc_opc == loc2->dw_loc_opc
6042          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6043          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6044 }
6045
6046 /* Do the values look the same?  */
6047 static int
6048 same_dw_val_p (dw_val_node *v1, dw_val_node *v2, int *mark)
6049 {
6050   dw_loc_descr_ref loc1, loc2;
6051   rtx r1, r2;
6052
6053   if (v1->val_class != v2->val_class)
6054     return 0;
6055
6056   switch (v1->val_class)
6057     {
6058     case dw_val_class_const:
6059       return v1->v.val_int == v2->v.val_int;
6060     case dw_val_class_unsigned_const:
6061       return v1->v.val_unsigned == v2->v.val_unsigned;
6062     case dw_val_class_long_long:
6063       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6064              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6065     case dw_val_class_vec:
6066       if (v1->v.val_vec.length != v2->v.val_vec.length
6067           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6068         return 0;
6069       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6070                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6071         return 0;
6072       return 1;
6073     case dw_val_class_flag:
6074       return v1->v.val_flag == v2->v.val_flag;
6075     case dw_val_class_str:
6076       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6077
6078     case dw_val_class_addr:
6079       r1 = v1->v.val_addr;
6080       r2 = v2->v.val_addr;
6081       if (GET_CODE (r1) != GET_CODE (r2))
6082         return 0;
6083       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6084       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6085
6086     case dw_val_class_offset:
6087       return v1->v.val_offset == v2->v.val_offset;
6088
6089     case dw_val_class_loc:
6090       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6091            loc1 && loc2;
6092            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6093         if (!same_loc_p (loc1, loc2, mark))
6094           return 0;
6095       return !loc1 && !loc2;
6096
6097     case dw_val_class_die_ref:
6098       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6099
6100     case dw_val_class_fde_ref:
6101     case dw_val_class_lbl_id:
6102     case dw_val_class_lineptr:
6103     case dw_val_class_macptr:
6104       return 1;
6105
6106     case dw_val_class_file:
6107       return v1->v.val_file == v2->v.val_file;
6108
6109     default:
6110       return 1;
6111     }
6112 }
6113
6114 /* Do the attributes look the same?  */
6115
6116 static int
6117 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6118 {
6119   if (at1->dw_attr != at2->dw_attr)
6120     return 0;
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 (at1->dw_attr == DW_AT_producer)
6125     return 1;
6126
6127   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6128 }
6129
6130 /* Do the dies look the same?  */
6131
6132 static int
6133 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6134 {
6135   dw_die_ref c1, c2;
6136   dw_attr_ref a1;
6137   unsigned ix;
6138
6139   /* To avoid infinite recursion.  */
6140   if (die1->die_mark)
6141     return die1->die_mark == die2->die_mark;
6142   die1->die_mark = die2->die_mark = ++(*mark);
6143
6144   if (die1->die_tag != die2->die_tag)
6145     return 0;
6146
6147   if (VEC_length (dw_attr_node, die1->die_attr)
6148       != VEC_length (dw_attr_node, die2->die_attr))
6149     return 0;
6150
6151   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6152     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6153       return 0;
6154
6155   c1 = die1->die_child;
6156   c2 = die2->die_child;
6157   if (! c1)
6158     {
6159       if (c2)
6160         return 0;
6161     }
6162   else
6163     for (;;)
6164       {
6165         if (!same_die_p (c1, c2, mark))
6166           return 0;
6167         c1 = c1->die_sib;
6168         c2 = c2->die_sib;
6169         if (c1 == die1->die_child)
6170           {
6171             if (c2 == die2->die_child)
6172               break;
6173             else
6174               return 0;
6175           }
6176     }
6177
6178   return 1;
6179 }
6180
6181 /* Do the dies look the same?  Wrapper around same_die_p.  */
6182
6183 static int
6184 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6185 {
6186   int mark = 0;
6187   int ret = same_die_p (die1, die2, &mark);
6188
6189   unmark_all_dies (die1);
6190   unmark_all_dies (die2);
6191
6192   return ret;
6193 }
6194
6195 /* The prefix to attach to symbols on DIEs in the current comdat debug
6196    info section.  */
6197 static char *comdat_symbol_id;
6198
6199 /* The index of the current symbol within the current comdat CU.  */
6200 static unsigned int comdat_symbol_number;
6201
6202 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6203    children, and set comdat_symbol_id accordingly.  */
6204
6205 static void
6206 compute_section_prefix (dw_die_ref unit_die)
6207 {
6208   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6209   const char *base = die_name ? lbasename (die_name) : "anonymous";
6210   char *name = alloca (strlen (base) + 64);
6211   char *p;
6212   int i, mark;
6213   unsigned char checksum[16];
6214   struct md5_ctx ctx;
6215
6216   /* Compute the checksum of the DIE, then append part of it as hex digits to
6217      the name filename of the unit.  */
6218
6219   md5_init_ctx (&ctx);
6220   mark = 0;
6221   die_checksum (unit_die, &ctx, &mark);
6222   unmark_all_dies (unit_die);
6223   md5_finish_ctx (&ctx, checksum);
6224
6225   sprintf (name, "%s.", base);
6226   clean_symbol_name (name);
6227
6228   p = name + strlen (name);
6229   for (i = 0; i < 4; i++)
6230     {
6231       sprintf (p, "%.2x", checksum[i]);
6232       p += 2;
6233     }
6234
6235   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6236   comdat_symbol_number = 0;
6237 }
6238
6239 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6240
6241 static int
6242 is_type_die (dw_die_ref die)
6243 {
6244   switch (die->die_tag)
6245     {
6246     case DW_TAG_array_type:
6247     case DW_TAG_class_type:
6248     case DW_TAG_enumeration_type:
6249     case DW_TAG_pointer_type:
6250     case DW_TAG_reference_type:
6251     case DW_TAG_string_type:
6252     case DW_TAG_structure_type:
6253     case DW_TAG_subroutine_type:
6254     case DW_TAG_union_type:
6255     case DW_TAG_ptr_to_member_type:
6256     case DW_TAG_set_type:
6257     case DW_TAG_subrange_type:
6258     case DW_TAG_base_type:
6259     case DW_TAG_const_type:
6260     case DW_TAG_file_type:
6261     case DW_TAG_packed_type:
6262     case DW_TAG_volatile_type:
6263     case DW_TAG_typedef:
6264       return 1;
6265     default:
6266       return 0;
6267     }
6268 }
6269
6270 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6271    Basically, we want to choose the bits that are likely to be shared between
6272    compilations (types) and leave out the bits that are specific to individual
6273    compilations (functions).  */
6274
6275 static int
6276 is_comdat_die (dw_die_ref c)
6277 {
6278   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6279      we do for stabs.  The advantage is a greater likelihood of sharing between
6280      objects that don't include headers in the same order (and therefore would
6281      put the base types in a different comdat).  jason 8/28/00 */
6282
6283   if (c->die_tag == DW_TAG_base_type)
6284     return 0;
6285
6286   if (c->die_tag == DW_TAG_pointer_type
6287       || c->die_tag == DW_TAG_reference_type
6288       || c->die_tag == DW_TAG_const_type
6289       || c->die_tag == DW_TAG_volatile_type)
6290     {
6291       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6292
6293       return t ? is_comdat_die (t) : 0;
6294     }
6295
6296   return is_type_die (c);
6297 }
6298
6299 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6300    compilation unit.  */
6301
6302 static int
6303 is_symbol_die (dw_die_ref c)
6304 {
6305   return (is_type_die (c)
6306           || (get_AT (c, DW_AT_declaration)
6307               && !get_AT (c, DW_AT_specification))
6308           || c->die_tag == DW_TAG_namespace);
6309 }
6310
6311 static char *
6312 gen_internal_sym (const char *prefix)
6313 {
6314   char buf[256];
6315
6316   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6317   return xstrdup (buf);
6318 }
6319
6320 /* Assign symbols to all worthy DIEs under DIE.  */
6321
6322 static void
6323 assign_symbol_names (dw_die_ref die)
6324 {
6325   dw_die_ref c;
6326
6327   if (is_symbol_die (die))
6328     {
6329       if (comdat_symbol_id)
6330         {
6331           char *p = alloca (strlen (comdat_symbol_id) + 64);
6332
6333           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6334                    comdat_symbol_id, comdat_symbol_number++);
6335           die->die_symbol = xstrdup (p);
6336         }
6337       else
6338         die->die_symbol = gen_internal_sym ("LDIE");
6339     }
6340
6341   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6342 }
6343
6344 struct cu_hash_table_entry
6345 {
6346   dw_die_ref cu;
6347   unsigned min_comdat_num, max_comdat_num;
6348   struct cu_hash_table_entry *next;
6349 };
6350
6351 /* Routines to manipulate hash table of CUs.  */
6352 static hashval_t
6353 htab_cu_hash (const void *of)
6354 {
6355   const struct cu_hash_table_entry *entry = of;
6356
6357   return htab_hash_string (entry->cu->die_symbol);
6358 }
6359
6360 static int
6361 htab_cu_eq (const void *of1, const void *of2)
6362 {
6363   const struct cu_hash_table_entry *entry1 = of1;
6364   const struct die_struct *entry2 = of2;
6365
6366   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6367 }
6368
6369 static void
6370 htab_cu_del (void *what)
6371 {
6372   struct cu_hash_table_entry *next, *entry = what;
6373
6374   while (entry)
6375     {
6376       next = entry->next;
6377       free (entry);
6378       entry = next;
6379     }
6380 }
6381
6382 /* Check whether we have already seen this CU and set up SYM_NUM
6383    accordingly.  */
6384 static int
6385 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6386 {
6387   struct cu_hash_table_entry dummy;
6388   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6389
6390   dummy.max_comdat_num = 0;
6391
6392   slot = (struct cu_hash_table_entry **)
6393     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6394         INSERT);
6395   entry = *slot;
6396
6397   for (; entry; last = entry, entry = entry->next)
6398     {
6399       if (same_die_p_wrap (cu, entry->cu))
6400         break;
6401     }
6402
6403   if (entry)
6404     {
6405       *sym_num = entry->min_comdat_num;
6406       return 1;
6407     }
6408
6409   entry = XCNEW (struct cu_hash_table_entry);
6410   entry->cu = cu;
6411   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6412   entry->next = *slot;
6413   *slot = entry;
6414
6415   return 0;
6416 }
6417
6418 /* Record SYM_NUM to record of CU in HTABLE.  */
6419 static void
6420 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6421 {
6422   struct cu_hash_table_entry **slot, *entry;
6423
6424   slot = (struct cu_hash_table_entry **)
6425     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6426         NO_INSERT);
6427   entry = *slot;
6428
6429   entry->max_comdat_num = sym_num;
6430 }
6431
6432 /* Traverse the DIE (which is always comp_unit_die), and set up
6433    additional compilation units for each of the include files we see
6434    bracketed by BINCL/EINCL.  */
6435
6436 static void
6437 break_out_includes (dw_die_ref die)
6438 {
6439   dw_die_ref c;
6440   dw_die_ref unit = NULL;
6441   limbo_die_node *node, **pnode;
6442   htab_t cu_hash_table;
6443
6444   c = die->die_child;
6445   if (c) do {
6446     dw_die_ref prev = c;
6447     c = c->die_sib;
6448     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6449            || (unit && is_comdat_die (c)))
6450       {
6451         dw_die_ref next = c->die_sib;
6452
6453         /* This DIE is for a secondary CU; remove it from the main one.  */
6454         remove_child_with_prev (c, prev);
6455
6456         if (c->die_tag == DW_TAG_GNU_BINCL)
6457           unit = push_new_compile_unit (unit, c);
6458         else if (c->die_tag == DW_TAG_GNU_EINCL)
6459           unit = pop_compile_unit (unit);
6460         else
6461           add_child_die (unit, c);
6462         c = next;
6463         if (c == die->die_child)
6464           break;
6465       }
6466   } while (c != die->die_child);
6467
6468 #if 0
6469   /* We can only use this in debugging, since the frontend doesn't check
6470      to make sure that we leave every include file we enter.  */
6471   gcc_assert (!unit);
6472 #endif
6473
6474   assign_symbol_names (die);
6475   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6476   for (node = limbo_die_list, pnode = &limbo_die_list;
6477        node;
6478        node = node->next)
6479     {
6480       int is_dupl;
6481
6482       compute_section_prefix (node->die);
6483       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6484                         &comdat_symbol_number);
6485       assign_symbol_names (node->die);
6486       if (is_dupl)
6487         *pnode = node->next;
6488       else
6489         {
6490           pnode = &node->next;
6491           record_comdat_symbol_number (node->die, cu_hash_table,
6492                 comdat_symbol_number);
6493         }
6494     }
6495   htab_delete (cu_hash_table);
6496 }
6497
6498 /* Traverse the DIE and add a sibling attribute if it may have the
6499    effect of speeding up access to siblings.  To save some space,
6500    avoid generating sibling attributes for DIE's without children.  */
6501
6502 static void
6503 add_sibling_attributes (dw_die_ref die)
6504 {
6505   dw_die_ref c;
6506
6507   if (! die->die_child)
6508     return;
6509
6510   if (die->die_parent && die != die->die_parent->die_child)
6511     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6512
6513   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6514 }
6515
6516 /* Output all location lists for the DIE and its children.  */
6517
6518 static void
6519 output_location_lists (dw_die_ref die)
6520 {
6521   dw_die_ref c;
6522   dw_attr_ref a;
6523   unsigned ix;
6524
6525   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6526     if (AT_class (a) == dw_val_class_loc_list)
6527       output_loc_list (AT_loc_list (a));
6528
6529   FOR_EACH_CHILD (die, c, output_location_lists (c));
6530 }
6531
6532 /* The format of each DIE (and its attribute value pairs) is encoded in an
6533    abbreviation table.  This routine builds the abbreviation table and assigns
6534    a unique abbreviation id for each abbreviation entry.  The children of each
6535    die are visited recursively.  */
6536
6537 static void
6538 build_abbrev_table (dw_die_ref die)
6539 {
6540   unsigned long abbrev_id;
6541   unsigned int n_alloc;
6542   dw_die_ref c;
6543   dw_attr_ref a;
6544   unsigned ix;
6545
6546   /* Scan the DIE references, and mark as external any that refer to
6547      DIEs from other CUs (i.e. those which are not marked).  */
6548   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6549     if (AT_class (a) == dw_val_class_die_ref
6550         && AT_ref (a)->die_mark == 0)
6551       {
6552         gcc_assert (AT_ref (a)->die_symbol);
6553
6554         set_AT_ref_external (a, 1);
6555       }
6556
6557   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6558     {
6559       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6560       dw_attr_ref die_a, abbrev_a;
6561       unsigned ix;
6562       bool ok = true;
6563
6564       if (abbrev->die_tag != die->die_tag)
6565         continue;
6566       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6567         continue;
6568
6569       if (VEC_length (dw_attr_node, abbrev->die_attr)
6570           != VEC_length (dw_attr_node, die->die_attr))
6571         continue;
6572
6573       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6574         {
6575           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6576           if ((abbrev_a->dw_attr != die_a->dw_attr)
6577               || (value_format (abbrev_a) != value_format (die_a)))
6578             {
6579               ok = false;
6580               break;
6581             }
6582         }
6583       if (ok)
6584         break;
6585     }
6586
6587   if (abbrev_id >= abbrev_die_table_in_use)
6588     {
6589       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6590         {
6591           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6592           abbrev_die_table = ggc_realloc (abbrev_die_table,
6593                                           sizeof (dw_die_ref) * n_alloc);
6594
6595           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6596                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6597           abbrev_die_table_allocated = n_alloc;
6598         }
6599
6600       ++abbrev_die_table_in_use;
6601       abbrev_die_table[abbrev_id] = die;
6602     }
6603
6604   die->die_abbrev = abbrev_id;
6605   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6606 }
6607 \f
6608 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6609
6610 static int
6611 constant_size (long unsigned int value)
6612 {
6613   int log;
6614
6615   if (value == 0)
6616     log = 0;
6617   else
6618     log = floor_log2 (value);
6619
6620   log = log / 8;
6621   log = 1 << (floor_log2 (log) + 1);
6622
6623   return log;
6624 }
6625
6626 /* Return the size of a DIE as it is represented in the
6627    .debug_info section.  */
6628
6629 static unsigned long
6630 size_of_die (dw_die_ref die)
6631 {
6632   unsigned long size = 0;
6633   dw_attr_ref a;
6634   unsigned ix;
6635
6636   size += size_of_uleb128 (die->die_abbrev);
6637   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6638     {
6639       switch (AT_class (a))
6640         {
6641         case dw_val_class_addr:
6642           size += DWARF2_ADDR_SIZE;
6643           break;
6644         case dw_val_class_offset:
6645           size += DWARF_OFFSET_SIZE;
6646           break;
6647         case dw_val_class_loc:
6648           {
6649             unsigned long lsize = size_of_locs (AT_loc (a));
6650
6651             /* Block length.  */
6652             size += constant_size (lsize);
6653             size += lsize;
6654           }
6655           break;
6656         case dw_val_class_loc_list:
6657           size += DWARF_OFFSET_SIZE;
6658           break;
6659         case dw_val_class_range_list:
6660           size += DWARF_OFFSET_SIZE;
6661           break;
6662         case dw_val_class_const:
6663           size += size_of_sleb128 (AT_int (a));
6664           break;
6665         case dw_val_class_unsigned_const:
6666           size += constant_size (AT_unsigned (a));
6667           break;
6668         case dw_val_class_long_long:
6669           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6670           break;
6671         case dw_val_class_vec:
6672           size += 1 + (a->dw_attr_val.v.val_vec.length
6673                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6674           break;
6675         case dw_val_class_flag:
6676           size += 1;
6677           break;
6678         case dw_val_class_die_ref:
6679           if (AT_ref_external (a))
6680             size += DWARF2_ADDR_SIZE;
6681           else
6682             size += DWARF_OFFSET_SIZE;
6683           break;
6684         case dw_val_class_fde_ref:
6685           size += DWARF_OFFSET_SIZE;
6686           break;
6687         case dw_val_class_lbl_id:
6688           size += DWARF2_ADDR_SIZE;
6689           break;
6690         case dw_val_class_lineptr:
6691         case dw_val_class_macptr:
6692           size += DWARF_OFFSET_SIZE;
6693           break;
6694         case dw_val_class_str:
6695           if (AT_string_form (a) == DW_FORM_strp)
6696             size += DWARF_OFFSET_SIZE;
6697           else
6698             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6699           break;
6700         case dw_val_class_file:
6701           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6702           break;
6703         default:
6704           gcc_unreachable ();
6705         }
6706     }
6707
6708   return size;
6709 }
6710
6711 /* Size the debugging information associated with a given DIE.  Visits the
6712    DIE's children recursively.  Updates the global variable next_die_offset, on
6713    each time through.  Uses the current value of next_die_offset to update the
6714    die_offset field in each DIE.  */
6715
6716 static void
6717 calc_die_sizes (dw_die_ref die)
6718 {
6719   dw_die_ref c;
6720
6721   die->die_offset = next_die_offset;
6722   next_die_offset += size_of_die (die);
6723
6724   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6725
6726   if (die->die_child != NULL)
6727     /* Count the null byte used to terminate sibling lists.  */
6728     next_die_offset += 1;
6729 }
6730
6731 /* Set the marks for a die and its children.  We do this so
6732    that we know whether or not a reference needs to use FORM_ref_addr; only
6733    DIEs in the same CU will be marked.  We used to clear out the offset
6734    and use that as the flag, but ran into ordering problems.  */
6735
6736 static void
6737 mark_dies (dw_die_ref die)
6738 {
6739   dw_die_ref c;
6740
6741   gcc_assert (!die->die_mark);
6742
6743   die->die_mark = 1;
6744   FOR_EACH_CHILD (die, c, mark_dies (c));
6745 }
6746
6747 /* Clear the marks for a die and its children.  */
6748
6749 static void
6750 unmark_dies (dw_die_ref die)
6751 {
6752   dw_die_ref c;
6753
6754   gcc_assert (die->die_mark);
6755
6756   die->die_mark = 0;
6757   FOR_EACH_CHILD (die, c, unmark_dies (c));
6758 }
6759
6760 /* Clear the marks for a die, its children and referred dies.  */
6761
6762 static void
6763 unmark_all_dies (dw_die_ref die)
6764 {
6765   dw_die_ref c;
6766   dw_attr_ref a;
6767   unsigned ix;
6768
6769   if (!die->die_mark)
6770     return;
6771   die->die_mark = 0;
6772
6773   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6774
6775   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6776     if (AT_class (a) == dw_val_class_die_ref)
6777       unmark_all_dies (AT_ref (a));
6778 }
6779
6780 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6781    generated for the compilation unit.  */
6782
6783 static unsigned long
6784 size_of_pubnames (VEC (pubname_entry, gc) * names)
6785 {
6786   unsigned long size;
6787   unsigned i;
6788   pubname_ref p;
6789
6790   size = DWARF_PUBNAMES_HEADER_SIZE;
6791   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6792     if (names != pubtype_table
6793         || p->die->die_offset != 0
6794         || !flag_eliminate_unused_debug_types)
6795       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6796
6797   size += DWARF_OFFSET_SIZE;
6798   return size;
6799 }
6800
6801 /* Return the size of the information in the .debug_aranges section.  */
6802
6803 static unsigned long
6804 size_of_aranges (void)
6805 {
6806   unsigned long size;
6807
6808   size = DWARF_ARANGES_HEADER_SIZE;
6809
6810   /* Count the address/length pair for this compilation unit.  */
6811   size += 2 * DWARF2_ADDR_SIZE;
6812   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6813
6814   /* Count the two zero words used to terminated the address range table.  */
6815   size += 2 * DWARF2_ADDR_SIZE;
6816   return size;
6817 }
6818 \f
6819 /* Select the encoding of an attribute value.  */
6820
6821 static enum dwarf_form
6822 value_format (dw_attr_ref a)
6823 {
6824   switch (a->dw_attr_val.val_class)
6825     {
6826     case dw_val_class_addr:
6827       return DW_FORM_addr;
6828     case dw_val_class_range_list:
6829     case dw_val_class_offset:
6830     case dw_val_class_loc_list:
6831       switch (DWARF_OFFSET_SIZE)
6832         {
6833         case 4:
6834           return DW_FORM_data4;
6835         case 8:
6836           return DW_FORM_data8;
6837         default:
6838           gcc_unreachable ();
6839         }
6840     case dw_val_class_loc:
6841       switch (constant_size (size_of_locs (AT_loc (a))))
6842         {
6843         case 1:
6844           return DW_FORM_block1;
6845         case 2:
6846           return DW_FORM_block2;
6847         default:
6848           gcc_unreachable ();
6849         }
6850     case dw_val_class_const:
6851       return DW_FORM_sdata;
6852     case dw_val_class_unsigned_const:
6853       switch (constant_size (AT_unsigned (a)))
6854         {
6855         case 1:
6856           return DW_FORM_data1;
6857         case 2:
6858           return DW_FORM_data2;
6859         case 4:
6860           return DW_FORM_data4;
6861         case 8:
6862           return DW_FORM_data8;
6863         default:
6864           gcc_unreachable ();
6865         }
6866     case dw_val_class_long_long:
6867       return DW_FORM_block1;
6868     case dw_val_class_vec:
6869       return DW_FORM_block1;
6870     case dw_val_class_flag:
6871       return DW_FORM_flag;
6872     case dw_val_class_die_ref:
6873       if (AT_ref_external (a))
6874         return DW_FORM_ref_addr;
6875       else
6876         return DW_FORM_ref;
6877     case dw_val_class_fde_ref:
6878       return DW_FORM_data;
6879     case dw_val_class_lbl_id:
6880       return DW_FORM_addr;
6881     case dw_val_class_lineptr:
6882     case dw_val_class_macptr:
6883       return DW_FORM_data;
6884     case dw_val_class_str:
6885       return AT_string_form (a);
6886     case dw_val_class_file:
6887       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6888         {
6889         case 1:
6890           return DW_FORM_data1;
6891         case 2:
6892           return DW_FORM_data2;
6893         case 4:
6894           return DW_FORM_data4;
6895         default:
6896           gcc_unreachable ();
6897         }
6898
6899     default:
6900       gcc_unreachable ();
6901     }
6902 }
6903
6904 /* Output the encoding of an attribute value.  */
6905
6906 static void
6907 output_value_format (dw_attr_ref a)
6908 {
6909   enum dwarf_form form = value_format (a);
6910
6911   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6912 }
6913
6914 /* Output the .debug_abbrev section which defines the DIE abbreviation
6915    table.  */
6916
6917 static void
6918 output_abbrev_section (void)
6919 {
6920   unsigned long abbrev_id;
6921
6922   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6923     {
6924       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6925       unsigned ix;
6926       dw_attr_ref a_attr;
6927
6928       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
6929       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
6930                                    dwarf_tag_name (abbrev->die_tag));
6931
6932       if (abbrev->die_child != NULL)
6933         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
6934       else
6935         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
6936
6937       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
6938            ix++)
6939         {
6940           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
6941                                        dwarf_attr_name (a_attr->dw_attr));
6942           output_value_format (a_attr);
6943         }
6944
6945       dw2_asm_output_data (1, 0, NULL);
6946       dw2_asm_output_data (1, 0, NULL);
6947     }
6948
6949   /* Terminate the table.  */
6950   dw2_asm_output_data (1, 0, NULL);
6951 }
6952
6953 /* Output a symbol we can use to refer to this DIE from another CU.  */
6954
6955 static inline void
6956 output_die_symbol (dw_die_ref die)
6957 {
6958   char *sym = die->die_symbol;
6959
6960   if (sym == 0)
6961     return;
6962
6963   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
6964     /* We make these global, not weak; if the target doesn't support
6965        .linkonce, it doesn't support combining the sections, so debugging
6966        will break.  */
6967     targetm.asm_out.globalize_label (asm_out_file, sym);
6968
6969   ASM_OUTPUT_LABEL (asm_out_file, sym);
6970 }
6971
6972 /* Return a new location list, given the begin and end range, and the
6973    expression. gensym tells us whether to generate a new internal symbol for
6974    this location list node, which is done for the head of the list only.  */
6975
6976 static inline dw_loc_list_ref
6977 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
6978               const char *section, unsigned int gensym)
6979 {
6980   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
6981
6982   retlist->begin = begin;
6983   retlist->end = end;
6984   retlist->expr = expr;
6985   retlist->section = section;
6986   if (gensym)
6987     retlist->ll_symbol = gen_internal_sym ("LLST");
6988
6989   return retlist;
6990 }
6991
6992 /* Add a location description expression to a location list.  */
6993
6994 static inline void
6995 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
6996                            const char *begin, const char *end,
6997                            const char *section)
6998 {
6999   dw_loc_list_ref *d;
7000
7001   /* Find the end of the chain.  */
7002   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7003     ;
7004
7005   /* Add a new location list node to the list.  */
7006   *d = new_loc_list (descr, begin, end, section, 0);
7007 }
7008
7009 static void
7010 dwarf2out_switch_text_section (void)
7011 {
7012   dw_fde_ref fde;
7013
7014   gcc_assert (cfun);
7015
7016   fde = &fde_table[fde_table_in_use - 1];
7017   fde->dw_fde_switched_sections = true;
7018   fde->dw_fde_hot_section_label = cfun->hot_section_label;
7019   fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7020   fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7021   fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
7022   have_multiple_function_sections = true;
7023
7024   /* Reset the current label on switching text sections, so that we
7025      don't attempt to advance_loc4 between labels in different sections.  */
7026   fde->dw_fde_current_label = NULL;
7027 }
7028
7029 /* Output the location list given to us.  */
7030
7031 static void
7032 output_loc_list (dw_loc_list_ref list_head)
7033 {
7034   dw_loc_list_ref curr = list_head;
7035
7036   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7037
7038   /* Walk the location list, and output each range + expression.  */
7039   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7040     {
7041       unsigned long size;
7042       if (!have_multiple_function_sections)
7043         {
7044           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7045                                 "Location list begin address (%s)",
7046                                 list_head->ll_symbol);
7047           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7048                                 "Location list end address (%s)",
7049                                 list_head->ll_symbol);
7050         }
7051       else
7052         {
7053           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7054                                "Location list begin address (%s)",
7055                                list_head->ll_symbol);
7056           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7057                                "Location list end address (%s)",
7058                                list_head->ll_symbol);
7059         }
7060       size = size_of_locs (curr->expr);
7061
7062       /* Output the block length for this list of location operations.  */
7063       gcc_assert (size <= 0xffff);
7064       dw2_asm_output_data (2, size, "%s", "Location expression size");
7065
7066       output_loc_sequence (curr->expr);
7067     }
7068
7069   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7070                        "Location list terminator begin (%s)",
7071                        list_head->ll_symbol);
7072   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7073                        "Location list terminator end (%s)",
7074                        list_head->ll_symbol);
7075 }
7076
7077 /* Output the DIE and its attributes.  Called recursively to generate
7078    the definitions of each child DIE.  */
7079
7080 static void
7081 output_die (dw_die_ref die)
7082 {
7083   dw_attr_ref a;
7084   dw_die_ref c;
7085   unsigned long size;
7086   unsigned ix;
7087
7088   /* If someone in another CU might refer to us, set up a symbol for
7089      them to point to.  */
7090   if (die->die_symbol)
7091     output_die_symbol (die);
7092
7093   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7094                                (unsigned long)die->die_offset,
7095                                dwarf_tag_name (die->die_tag));
7096
7097   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7098     {
7099       const char *name = dwarf_attr_name (a->dw_attr);
7100
7101       switch (AT_class (a))
7102         {
7103         case dw_val_class_addr:
7104           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7105           break;
7106
7107         case dw_val_class_offset:
7108           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7109                                "%s", name);
7110           break;
7111
7112         case dw_val_class_range_list:
7113           {
7114             char *p = strchr (ranges_section_label, '\0');
7115
7116             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7117                      a->dw_attr_val.v.val_offset);
7118             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7119                                    debug_ranges_section, "%s", name);
7120             *p = '\0';
7121           }
7122           break;
7123
7124         case dw_val_class_loc:
7125           size = size_of_locs (AT_loc (a));
7126
7127           /* Output the block length for this list of location operations.  */
7128           dw2_asm_output_data (constant_size (size), size, "%s", name);
7129
7130           output_loc_sequence (AT_loc (a));
7131           break;
7132
7133         case dw_val_class_const:
7134           /* ??? It would be slightly more efficient to use a scheme like is
7135              used for unsigned constants below, but gdb 4.x does not sign
7136              extend.  Gdb 5.x does sign extend.  */
7137           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7138           break;
7139
7140         case dw_val_class_unsigned_const:
7141           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7142                                AT_unsigned (a), "%s", name);
7143           break;
7144
7145         case dw_val_class_long_long:
7146           {
7147             unsigned HOST_WIDE_INT first, second;
7148
7149             dw2_asm_output_data (1,
7150                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7151                                  "%s", name);
7152
7153             if (WORDS_BIG_ENDIAN)
7154               {
7155                 first = a->dw_attr_val.v.val_long_long.hi;
7156                 second = a->dw_attr_val.v.val_long_long.low;
7157               }
7158             else
7159               {
7160                 first = a->dw_attr_val.v.val_long_long.low;
7161                 second = a->dw_attr_val.v.val_long_long.hi;
7162               }
7163
7164             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7165                                  first, "long long constant");
7166             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7167                                  second, NULL);
7168           }
7169           break;
7170
7171         case dw_val_class_vec:
7172           {
7173             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7174             unsigned int len = a->dw_attr_val.v.val_vec.length;
7175             unsigned int i;
7176             unsigned char *p;
7177
7178             dw2_asm_output_data (1, len * elt_size, "%s", name);
7179             if (elt_size > sizeof (HOST_WIDE_INT))
7180               {
7181                 elt_size /= 2;
7182                 len *= 2;
7183               }
7184             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7185                  i < len;
7186                  i++, p += elt_size)
7187               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7188                                    "fp or vector constant word %u", i);
7189             break;
7190           }
7191
7192         case dw_val_class_flag:
7193           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7194           break;
7195
7196         case dw_val_class_loc_list:
7197           {
7198             char *sym = AT_loc_list (a)->ll_symbol;
7199
7200             gcc_assert (sym);
7201             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7202                                    "%s", name);
7203           }
7204           break;
7205
7206         case dw_val_class_die_ref:
7207           if (AT_ref_external (a))
7208             {
7209               char *sym = AT_ref (a)->die_symbol;
7210
7211               gcc_assert (sym);
7212               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7213                                      "%s", name);
7214             }
7215           else
7216             {
7217               gcc_assert (AT_ref (a)->die_offset);
7218               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7219                                    "%s", name);
7220             }
7221           break;
7222
7223         case dw_val_class_fde_ref:
7224           {
7225             char l1[20];
7226
7227             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7228                                          a->dw_attr_val.v.val_fde_index * 2);
7229             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7230                                    "%s", name);
7231           }
7232           break;
7233
7234         case dw_val_class_lbl_id:
7235           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7236           break;
7237
7238         case dw_val_class_lineptr:
7239           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7240                                  debug_line_section, "%s", name);
7241           break;
7242
7243         case dw_val_class_macptr:
7244           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7245                                  debug_macinfo_section, "%s", name);
7246           break;
7247
7248         case dw_val_class_str:
7249           if (AT_string_form (a) == DW_FORM_strp)
7250             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7251                                    a->dw_attr_val.v.val_str->label,
7252                                    debug_str_section,
7253                                    "%s: \"%s\"", name, AT_string (a));
7254           else
7255             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7256           break;
7257
7258         case dw_val_class_file:
7259           {
7260             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7261
7262             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7263                                  a->dw_attr_val.v.val_file->filename);
7264             break;
7265           }
7266
7267         default:
7268           gcc_unreachable ();
7269         }
7270     }
7271
7272   FOR_EACH_CHILD (die, c, output_die (c));
7273
7274   /* Add null byte to terminate sibling list.  */
7275   if (die->die_child != NULL)
7276     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7277                          (unsigned long) die->die_offset);
7278 }
7279
7280 /* Output the compilation unit that appears at the beginning of the
7281    .debug_info section, and precedes the DIE descriptions.  */
7282
7283 static void
7284 output_compilation_unit_header (void)
7285 {
7286   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7287     dw2_asm_output_data (4, 0xffffffff,
7288       "Initial length escape value indicating 64-bit DWARF extension");
7289   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7290                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7291                        "Length of Compilation Unit Info");
7292   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7293   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7294                          debug_abbrev_section,
7295                          "Offset Into Abbrev. Section");
7296   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7297 }
7298
7299 /* Output the compilation unit DIE and its children.  */
7300
7301 static void
7302 output_comp_unit (dw_die_ref die, int output_if_empty)
7303 {
7304   const char *secname;
7305   char *oldsym, *tmp;
7306
7307   /* Unless we are outputting main CU, we may throw away empty ones.  */
7308   if (!output_if_empty && die->die_child == NULL)
7309     return;
7310
7311   /* Even if there are no children of this DIE, we must output the information
7312      about the compilation unit.  Otherwise, on an empty translation unit, we
7313      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7314      will then complain when examining the file.  First mark all the DIEs in
7315      this CU so we know which get local refs.  */
7316   mark_dies (die);
7317
7318   build_abbrev_table (die);
7319
7320   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7321   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7322   calc_die_sizes (die);
7323
7324   oldsym = die->die_symbol;
7325   if (oldsym)
7326     {
7327       tmp = alloca (strlen (oldsym) + 24);
7328
7329       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7330       secname = tmp;
7331       die->die_symbol = NULL;
7332       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7333     }
7334   else
7335     switch_to_section (debug_info_section);
7336
7337   /* Output debugging information.  */
7338   output_compilation_unit_header ();
7339   output_die (die);
7340
7341   /* Leave the marks on the main CU, so we can check them in
7342      output_pubnames.  */
7343   if (oldsym)
7344     {
7345       unmark_dies (die);
7346       die->die_symbol = oldsym;
7347     }
7348 }
7349
7350 /* Return the DWARF2/3 pubname associated with a decl.  */
7351
7352 static const char *
7353 dwarf2_name (tree decl, int scope)
7354 {
7355   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7356 }
7357
7358 /* Add a new entry to .debug_pubnames if appropriate.  */
7359
7360 static void
7361 add_pubname (tree decl, dw_die_ref die)
7362 {
7363   pubname_entry e;
7364
7365   if (! TREE_PUBLIC (decl))
7366     return;
7367
7368   e.die = die;
7369   e.name = xstrdup (dwarf2_name (decl, 1));
7370   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7371 }
7372
7373 /* Add a new entry to .debug_pubtypes if appropriate.  */
7374
7375 static void
7376 add_pubtype (tree decl, dw_die_ref die)
7377 {
7378   pubname_entry e;
7379
7380   e.name = NULL;
7381   if ((TREE_PUBLIC (decl)
7382        || die->die_parent == comp_unit_die)
7383       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7384     {
7385       e.die = die;
7386       if (TYPE_P (decl))
7387         {
7388           if (TYPE_NAME (decl))
7389             {
7390               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7391                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7392               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7393                        && DECL_NAME (TYPE_NAME (decl)))
7394                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7395               else
7396                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7397             }
7398         }
7399       else
7400         e.name = xstrdup (dwarf2_name (decl, 1));
7401
7402       /* If we don't have a name for the type, there's no point in adding
7403          it to the table.  */
7404       if (e.name && e.name[0] != '\0')
7405         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7406     }
7407 }
7408
7409 /* Output the public names table used to speed up access to externally
7410    visible names; or the public types table used to find type definitions.  */
7411
7412 static void
7413 output_pubnames (VEC (pubname_entry, gc) * names)
7414 {
7415   unsigned i;
7416   unsigned long pubnames_length = size_of_pubnames (names);
7417   pubname_ref pub;
7418
7419   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7420     dw2_asm_output_data (4, 0xffffffff,
7421       "Initial length escape value indicating 64-bit DWARF extension");
7422   if (names == pubname_table)
7423     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7424                          "Length of Public Names Info");
7425   else
7426     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7427                          "Length of Public Type Names Info");
7428   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7429   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7430                          debug_info_section,
7431                          "Offset of Compilation Unit Info");
7432   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7433                        "Compilation Unit Length");
7434
7435   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7436     {
7437       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7438       if (names == pubname_table)
7439         gcc_assert (pub->die->die_mark);
7440
7441       if (names != pubtype_table
7442           || pub->die->die_offset != 0
7443           || !flag_eliminate_unused_debug_types)
7444         {
7445           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7446                                "DIE offset");
7447
7448           dw2_asm_output_nstring (pub->name, -1, "external name");
7449         }
7450     }
7451
7452   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7453 }
7454
7455 /* Add a new entry to .debug_aranges if appropriate.  */
7456
7457 static void
7458 add_arange (tree decl, dw_die_ref die)
7459 {
7460   if (! DECL_SECTION_NAME (decl))
7461     return;
7462
7463   if (arange_table_in_use == arange_table_allocated)
7464     {
7465       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7466       arange_table = ggc_realloc (arange_table,
7467                                   (arange_table_allocated
7468                                    * sizeof (dw_die_ref)));
7469       memset (arange_table + arange_table_in_use, 0,
7470               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7471     }
7472
7473   arange_table[arange_table_in_use++] = die;
7474 }
7475
7476 /* Output the information that goes into the .debug_aranges table.
7477    Namely, define the beginning and ending address range of the
7478    text section generated for this compilation unit.  */
7479
7480 static void
7481 output_aranges (void)
7482 {
7483   unsigned i;
7484   unsigned long aranges_length = size_of_aranges ();
7485
7486   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7487     dw2_asm_output_data (4, 0xffffffff,
7488       "Initial length escape value indicating 64-bit DWARF extension");
7489   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7490                        "Length of Address Ranges Info");
7491   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7492   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7493                          debug_info_section,
7494                          "Offset of Compilation Unit Info");
7495   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7496   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7497
7498   /* We need to align to twice the pointer size here.  */
7499   if (DWARF_ARANGES_PAD_SIZE)
7500     {
7501       /* Pad using a 2 byte words so that padding is correct for any
7502          pointer size.  */
7503       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7504                            2 * DWARF2_ADDR_SIZE);
7505       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7506         dw2_asm_output_data (2, 0, NULL);
7507     }
7508
7509   dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7510   dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7511                         text_section_label, "Length");
7512   if (flag_reorder_blocks_and_partition)
7513     {
7514       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7515                            "Address");
7516       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7517                             cold_text_section_label, "Length");
7518     }
7519
7520   for (i = 0; i < arange_table_in_use; i++)
7521     {
7522       dw_die_ref die = arange_table[i];
7523
7524       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7525       gcc_assert (die->die_mark);
7526
7527       if (die->die_tag == DW_TAG_subprogram)
7528         {
7529           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7530                                "Address");
7531           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7532                                 get_AT_low_pc (die), "Length");
7533         }
7534       else
7535         {
7536           /* A static variable; extract the symbol from DW_AT_location.
7537              Note that this code isn't currently hit, as we only emit
7538              aranges for functions (jason 9/23/99).  */
7539           dw_attr_ref a = get_AT (die, DW_AT_location);
7540           dw_loc_descr_ref loc;
7541
7542           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7543
7544           loc = AT_loc (a);
7545           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7546
7547           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7548                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7549           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7550                                get_AT_unsigned (die, DW_AT_byte_size),
7551                                "Length");
7552         }
7553     }
7554
7555   /* Output the terminator words.  */
7556   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7557   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7558 }
7559
7560 /* Add a new entry to .debug_ranges.  Return the offset at which it
7561    was placed.  */
7562
7563 static unsigned int
7564 add_ranges (tree block)
7565 {
7566   unsigned int in_use = ranges_table_in_use;
7567
7568   if (in_use == ranges_table_allocated)
7569     {
7570       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7571       ranges_table
7572         = ggc_realloc (ranges_table, (ranges_table_allocated
7573                                       * sizeof (struct dw_ranges_struct)));
7574       memset (ranges_table + ranges_table_in_use, 0,
7575               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7576     }
7577
7578   ranges_table[in_use].block_num = (block ? BLOCK_NUMBER (block) : 0);
7579   ranges_table_in_use = in_use + 1;
7580
7581   return in_use * 2 * DWARF2_ADDR_SIZE;
7582 }
7583
7584 static void
7585 output_ranges (void)
7586 {
7587   unsigned i;
7588   static const char *const start_fmt = "Offset 0x%x";
7589   const char *fmt = start_fmt;
7590
7591   for (i = 0; i < ranges_table_in_use; i++)
7592     {
7593       int block_num = ranges_table[i].block_num;
7594
7595       if (block_num)
7596         {
7597           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7598           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7599
7600           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7601           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7602
7603           /* If all code is in the text section, then the compilation
7604              unit base address defaults to DW_AT_low_pc, which is the
7605              base of the text section.  */
7606           if (!have_multiple_function_sections)
7607             {
7608               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7609                                     text_section_label,
7610                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7611               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7612                                     text_section_label, NULL);
7613             }
7614
7615           /* Otherwise, we add a DW_AT_entry_pc attribute to force the
7616              compilation unit base address to zero, which allows us to
7617              use absolute addresses, and not worry about whether the
7618              target supports cross-section arithmetic.  */
7619           else
7620             {
7621               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7622                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7623               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7624             }
7625
7626           fmt = NULL;
7627         }
7628       else
7629         {
7630           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7631           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7632           fmt = start_fmt;
7633         }
7634     }
7635 }
7636
7637 /* Data structure containing information about input files.  */
7638 struct file_info
7639 {
7640   const char *path;     /* Complete file name.  */
7641   const char *fname;    /* File name part.  */
7642   int length;           /* Length of entire string.  */
7643   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7644   int dir_idx;          /* Index in directory table.  */
7645 };
7646
7647 /* Data structure containing information about directories with source
7648    files.  */
7649 struct dir_info
7650 {
7651   const char *path;     /* Path including directory name.  */
7652   int length;           /* Path length.  */
7653   int prefix;           /* Index of directory entry which is a prefix.  */
7654   int count;            /* Number of files in this directory.  */
7655   int dir_idx;          /* Index of directory used as base.  */
7656 };
7657
7658 /* Callback function for file_info comparison.  We sort by looking at
7659    the directories in the path.  */
7660
7661 static int
7662 file_info_cmp (const void *p1, const void *p2)
7663 {
7664   const struct file_info *s1 = p1;
7665   const struct file_info *s2 = p2;
7666   unsigned char *cp1;
7667   unsigned char *cp2;
7668
7669   /* Take care of file names without directories.  We need to make sure that
7670      we return consistent values to qsort since some will get confused if
7671      we return the same value when identical operands are passed in opposite
7672      orders.  So if neither has a directory, return 0 and otherwise return
7673      1 or -1 depending on which one has the directory.  */
7674   if ((s1->path == s1->fname || s2->path == s2->fname))
7675     return (s2->path == s2->fname) - (s1->path == s1->fname);
7676
7677   cp1 = (unsigned char *) s1->path;
7678   cp2 = (unsigned char *) s2->path;
7679
7680   while (1)
7681     {
7682       ++cp1;
7683       ++cp2;
7684       /* Reached the end of the first path?  If so, handle like above.  */
7685       if ((cp1 == (unsigned char *) s1->fname)
7686           || (cp2 == (unsigned char *) s2->fname))
7687         return ((cp2 == (unsigned char *) s2->fname)
7688                 - (cp1 == (unsigned char *) s1->fname));
7689
7690       /* Character of current path component the same?  */
7691       else if (*cp1 != *cp2)
7692         return *cp1 - *cp2;
7693     }
7694 }
7695
7696 struct file_name_acquire_data
7697 {
7698   struct file_info *files;
7699   int used_files;
7700   int max_files;
7701 };
7702
7703 /* Traversal function for the hash table.  */
7704
7705 static int
7706 file_name_acquire (void ** slot, void *data)
7707 {
7708   struct file_name_acquire_data *fnad = data;
7709   struct dwarf_file_data *d = *slot;
7710   struct file_info *fi;
7711   const char *f;
7712
7713   gcc_assert (fnad->max_files >= d->emitted_number);
7714
7715   if (! d->emitted_number)
7716     return 1;
7717
7718   gcc_assert (fnad->max_files != fnad->used_files);
7719
7720   fi = fnad->files + fnad->used_files++;
7721
7722   /* Skip all leading "./".  */
7723   f = d->filename;
7724   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7725     f += 2;
7726
7727   /* Create a new array entry.  */
7728   fi->path = f;
7729   fi->length = strlen (f);
7730   fi->file_idx = d;
7731
7732   /* Search for the file name part.  */
7733   f = strrchr (f, DIR_SEPARATOR);
7734 #if defined (DIR_SEPARATOR_2)
7735   {
7736     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7737
7738     if (g != NULL)
7739       {
7740         if (f == NULL || f < g)
7741           f = g;
7742       }
7743   }
7744 #endif
7745
7746   fi->fname = f == NULL ? fi->path : f + 1;
7747   return 1;
7748 }
7749
7750 /* Output the directory table and the file name table.  We try to minimize
7751    the total amount of memory needed.  A heuristic is used to avoid large
7752    slowdowns with many input files.  */
7753
7754 static void
7755 output_file_names (void)
7756 {
7757   struct file_name_acquire_data fnad;
7758   int numfiles;
7759   struct file_info *files;
7760   struct dir_info *dirs;
7761   int *saved;
7762   int *savehere;
7763   int *backmap;
7764   int ndirs;
7765   int idx_offset;
7766   int i;
7767   int idx;
7768
7769   if (!last_emitted_file)
7770     {
7771       dw2_asm_output_data (1, 0, "End directory table");
7772       dw2_asm_output_data (1, 0, "End file name table");
7773       return;
7774     }
7775
7776   numfiles = last_emitted_file->emitted_number;
7777
7778   /* Allocate the various arrays we need.  */
7779   files = alloca (numfiles * sizeof (struct file_info));
7780   dirs = alloca (numfiles * sizeof (struct dir_info));
7781
7782   fnad.files = files;
7783   fnad.used_files = 0;
7784   fnad.max_files = numfiles;
7785   htab_traverse (file_table, file_name_acquire, &fnad);
7786   gcc_assert (fnad.used_files == fnad.max_files);
7787
7788   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7789
7790   /* Find all the different directories used.  */
7791   dirs[0].path = files[0].path;
7792   dirs[0].length = files[0].fname - files[0].path;
7793   dirs[0].prefix = -1;
7794   dirs[0].count = 1;
7795   dirs[0].dir_idx = 0;
7796   files[0].dir_idx = 0;
7797   ndirs = 1;
7798
7799   for (i = 1; i < numfiles; i++)
7800     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7801         && memcmp (dirs[ndirs - 1].path, files[i].path,
7802                    dirs[ndirs - 1].length) == 0)
7803       {
7804         /* Same directory as last entry.  */
7805         files[i].dir_idx = ndirs - 1;
7806         ++dirs[ndirs - 1].count;
7807       }
7808     else
7809       {
7810         int j;
7811
7812         /* This is a new directory.  */
7813         dirs[ndirs].path = files[i].path;
7814         dirs[ndirs].length = files[i].fname - files[i].path;
7815         dirs[ndirs].count = 1;
7816         dirs[ndirs].dir_idx = ndirs;
7817         files[i].dir_idx = ndirs;
7818
7819         /* Search for a prefix.  */
7820         dirs[ndirs].prefix = -1;
7821         for (j = 0; j < ndirs; j++)
7822           if (dirs[j].length < dirs[ndirs].length
7823               && dirs[j].length > 1
7824               && (dirs[ndirs].prefix == -1
7825                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7826               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7827             dirs[ndirs].prefix = j;
7828
7829         ++ndirs;
7830       }
7831
7832   /* Now to the actual work.  We have to find a subset of the directories which
7833      allow expressing the file name using references to the directory table
7834      with the least amount of characters.  We do not do an exhaustive search
7835      where we would have to check out every combination of every single
7836      possible prefix.  Instead we use a heuristic which provides nearly optimal
7837      results in most cases and never is much off.  */
7838   saved = alloca (ndirs * sizeof (int));
7839   savehere = alloca (ndirs * sizeof (int));
7840
7841   memset (saved, '\0', ndirs * sizeof (saved[0]));
7842   for (i = 0; i < ndirs; i++)
7843     {
7844       int j;
7845       int total;
7846
7847       /* We can always save some space for the current directory.  But this
7848          does not mean it will be enough to justify adding the directory.  */
7849       savehere[i] = dirs[i].length;
7850       total = (savehere[i] - saved[i]) * dirs[i].count;
7851
7852       for (j = i + 1; j < ndirs; j++)
7853         {
7854           savehere[j] = 0;
7855           if (saved[j] < dirs[i].length)
7856             {
7857               /* Determine whether the dirs[i] path is a prefix of the
7858                  dirs[j] path.  */
7859               int k;
7860
7861               k = dirs[j].prefix;
7862               while (k != -1 && k != (int) i)
7863                 k = dirs[k].prefix;
7864
7865               if (k == (int) i)
7866                 {
7867                   /* Yes it is.  We can possibly save some memory by
7868                      writing the filenames in dirs[j] relative to
7869                      dirs[i].  */
7870                   savehere[j] = dirs[i].length;
7871                   total += (savehere[j] - saved[j]) * dirs[j].count;
7872                 }
7873             }
7874         }
7875
7876       /* Check whether we can save enough to justify adding the dirs[i]
7877          directory.  */
7878       if (total > dirs[i].length + 1)
7879         {
7880           /* It's worthwhile adding.  */
7881           for (j = i; j < ndirs; j++)
7882             if (savehere[j] > 0)
7883               {
7884                 /* Remember how much we saved for this directory so far.  */
7885                 saved[j] = savehere[j];
7886
7887                 /* Remember the prefix directory.  */
7888                 dirs[j].dir_idx = i;
7889               }
7890         }
7891     }
7892
7893   /* Emit the directory name table.  */
7894   idx = 1;
7895   idx_offset = dirs[0].length > 0 ? 1 : 0;
7896   for (i = 1 - idx_offset; i < ndirs; i++)
7897     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
7898                             "Directory Entry: 0x%x", i + idx_offset);
7899
7900   dw2_asm_output_data (1, 0, "End directory table");
7901
7902   /* We have to emit them in the order of emitted_number since that's
7903      used in the debug info generation.  To do this efficiently we
7904      generate a back-mapping of the indices first.  */
7905   backmap = alloca (numfiles * sizeof (int));
7906   for (i = 0; i < numfiles; i++)
7907     backmap[files[i].file_idx->emitted_number - 1] = i;
7908
7909   /* Now write all the file names.  */
7910   for (i = 0; i < numfiles; i++)
7911     {
7912       int file_idx = backmap[i];
7913       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
7914
7915       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
7916                               "File Entry: 0x%x", (unsigned) i + 1);
7917
7918       /* Include directory index.  */
7919       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
7920
7921       /* Modification time.  */
7922       dw2_asm_output_data_uleb128 (0, NULL);
7923
7924       /* File length in bytes.  */
7925       dw2_asm_output_data_uleb128 (0, NULL);
7926     }
7927
7928   dw2_asm_output_data (1, 0, "End file name table");
7929 }
7930
7931
7932 /* Output the source line number correspondence information.  This
7933    information goes into the .debug_line section.  */
7934
7935 static void
7936 output_line_info (void)
7937 {
7938   char l1[20], l2[20], p1[20], p2[20];
7939   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7940   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
7941   unsigned opc;
7942   unsigned n_op_args;
7943   unsigned long lt_index;
7944   unsigned long current_line;
7945   long line_offset;
7946   long line_delta;
7947   unsigned long current_file;
7948   unsigned long function;
7949
7950   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
7951   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
7952   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
7953   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
7954
7955   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7956     dw2_asm_output_data (4, 0xffffffff,
7957       "Initial length escape value indicating 64-bit DWARF extension");
7958   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
7959                         "Length of Source Line Info");
7960   ASM_OUTPUT_LABEL (asm_out_file, l1);
7961
7962   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7963   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
7964   ASM_OUTPUT_LABEL (asm_out_file, p1);
7965
7966   /* Define the architecture-dependent minimum instruction length (in
7967    bytes).  In this implementation of DWARF, this field is used for
7968    information purposes only.  Since GCC generates assembly language,
7969    we have no a priori knowledge of how many instruction bytes are
7970    generated for each source line, and therefore can use only the
7971    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
7972    commands.  Accordingly, we fix this as `1', which is "correct
7973    enough" for all architectures, and don't let the target override.  */
7974   dw2_asm_output_data (1, 1,
7975                        "Minimum Instruction Length");
7976
7977   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
7978                        "Default is_stmt_start flag");
7979   dw2_asm_output_data (1, DWARF_LINE_BASE,
7980                        "Line Base Value (Special Opcodes)");
7981   dw2_asm_output_data (1, DWARF_LINE_RANGE,
7982                        "Line Range Value (Special Opcodes)");
7983   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
7984                        "Special Opcode Base");
7985
7986   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
7987     {
7988       switch (opc)
7989         {
7990         case DW_LNS_advance_pc:
7991         case DW_LNS_advance_line:
7992         case DW_LNS_set_file:
7993         case DW_LNS_set_column:
7994         case DW_LNS_fixed_advance_pc:
7995           n_op_args = 1;
7996           break;
7997         default:
7998           n_op_args = 0;
7999           break;
8000         }
8001
8002       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8003                            opc, n_op_args);
8004     }
8005
8006   /* Write out the information about the files we use.  */
8007   output_file_names ();
8008   ASM_OUTPUT_LABEL (asm_out_file, p2);
8009
8010   /* We used to set the address register to the first location in the text
8011      section here, but that didn't accomplish anything since we already
8012      have a line note for the opening brace of the first function.  */
8013
8014   /* Generate the line number to PC correspondence table, encoded as
8015      a series of state machine operations.  */
8016   current_file = 1;
8017   current_line = 1;
8018
8019   if (cfun && in_cold_section_p)
8020     strcpy (prev_line_label, cfun->cold_section_label);
8021   else
8022     strcpy (prev_line_label, text_section_label);
8023   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8024     {
8025       dw_line_info_ref line_info = &line_info_table[lt_index];
8026
8027 #if 0
8028       /* Disable this optimization for now; GDB wants to see two line notes
8029          at the beginning of a function so it can find the end of the
8030          prologue.  */
8031
8032       /* Don't emit anything for redundant notes.  Just updating the
8033          address doesn't accomplish anything, because we already assume
8034          that anything after the last address is this line.  */
8035       if (line_info->dw_line_num == current_line
8036           && line_info->dw_file_num == current_file)
8037         continue;
8038 #endif
8039
8040       /* Emit debug info for the address of the current line.
8041
8042          Unfortunately, we have little choice here currently, and must always
8043          use the most general form.  GCC does not know the address delta
8044          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8045          attributes which will give an upper bound on the address range.  We
8046          could perhaps use length attributes to determine when it is safe to
8047          use DW_LNS_fixed_advance_pc.  */
8048
8049       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8050       if (0)
8051         {
8052           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8053           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8054                                "DW_LNS_fixed_advance_pc");
8055           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8056         }
8057       else
8058         {
8059           /* This can handle any delta.  This takes
8060              4+DWARF2_ADDR_SIZE bytes.  */
8061           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8062           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8063           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8064           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8065         }
8066
8067       strcpy (prev_line_label, line_label);
8068
8069       /* Emit debug info for the source file of the current line, if
8070          different from the previous line.  */
8071       if (line_info->dw_file_num != current_file)
8072         {
8073           current_file = line_info->dw_file_num;
8074           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8075           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8076         }
8077
8078       /* Emit debug info for the current line number, choosing the encoding
8079          that uses the least amount of space.  */
8080       if (line_info->dw_line_num != current_line)
8081         {
8082           line_offset = line_info->dw_line_num - current_line;
8083           line_delta = line_offset - DWARF_LINE_BASE;
8084           current_line = line_info->dw_line_num;
8085           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8086             /* This can handle deltas from -10 to 234, using the current
8087                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8088                takes 1 byte.  */
8089             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8090                                  "line %lu", current_line);
8091           else
8092             {
8093               /* This can handle any delta.  This takes at least 4 bytes,
8094                  depending on the value being encoded.  */
8095               dw2_asm_output_data (1, DW_LNS_advance_line,
8096                                    "advance to line %lu", current_line);
8097               dw2_asm_output_data_sleb128 (line_offset, NULL);
8098               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8099             }
8100         }
8101       else
8102         /* We still need to start a new row, so output a copy insn.  */
8103         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8104     }
8105
8106   /* Emit debug info for the address of the end of the function.  */
8107   if (0)
8108     {
8109       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8110                            "DW_LNS_fixed_advance_pc");
8111       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8112     }
8113   else
8114     {
8115       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8116       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8117       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8118       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8119     }
8120
8121   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8122   dw2_asm_output_data_uleb128 (1, NULL);
8123   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8124
8125   function = 0;
8126   current_file = 1;
8127   current_line = 1;
8128   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8129     {
8130       dw_separate_line_info_ref line_info
8131         = &separate_line_info_table[lt_index];
8132
8133 #if 0
8134       /* Don't emit anything for redundant notes.  */
8135       if (line_info->dw_line_num == current_line
8136           && line_info->dw_file_num == current_file
8137           && line_info->function == function)
8138         goto cont;
8139 #endif
8140
8141       /* Emit debug info for the address of the current line.  If this is
8142          a new function, or the first line of a function, then we need
8143          to handle it differently.  */
8144       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8145                                    lt_index);
8146       if (function != line_info->function)
8147         {
8148           function = line_info->function;
8149
8150           /* Set the address register to the first line in the function.  */
8151           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8152           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8153           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8154           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8155         }
8156       else
8157         {
8158           /* ??? See the DW_LNS_advance_pc comment above.  */
8159           if (0)
8160             {
8161               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8162                                    "DW_LNS_fixed_advance_pc");
8163               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8164             }
8165           else
8166             {
8167               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8168               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8169               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8170               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8171             }
8172         }
8173
8174       strcpy (prev_line_label, line_label);
8175
8176       /* Emit debug info for the source file of the current line, if
8177          different from the previous line.  */
8178       if (line_info->dw_file_num != current_file)
8179         {
8180           current_file = line_info->dw_file_num;
8181           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8182           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8183         }
8184
8185       /* Emit debug info for the current line number, choosing the encoding
8186          that uses the least amount of space.  */
8187       if (line_info->dw_line_num != current_line)
8188         {
8189           line_offset = line_info->dw_line_num - current_line;
8190           line_delta = line_offset - DWARF_LINE_BASE;
8191           current_line = line_info->dw_line_num;
8192           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8193             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8194                                  "line %lu", current_line);
8195           else
8196             {
8197               dw2_asm_output_data (1, DW_LNS_advance_line,
8198                                    "advance to line %lu", current_line);
8199               dw2_asm_output_data_sleb128 (line_offset, NULL);
8200               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8201             }
8202         }
8203       else
8204         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8205
8206 #if 0
8207     cont:
8208 #endif
8209
8210       lt_index++;
8211
8212       /* If we're done with a function, end its sequence.  */
8213       if (lt_index == separate_line_info_table_in_use
8214           || separate_line_info_table[lt_index].function != function)
8215         {
8216           current_file = 1;
8217           current_line = 1;
8218
8219           /* Emit debug info for the address of the end of the function.  */
8220           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8221           if (0)
8222             {
8223               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8224                                    "DW_LNS_fixed_advance_pc");
8225               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8226             }
8227           else
8228             {
8229               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8230               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8231               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8232               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8233             }
8234
8235           /* Output the marker for the end of this sequence.  */
8236           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8237           dw2_asm_output_data_uleb128 (1, NULL);
8238           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8239         }
8240     }
8241
8242   /* Output the marker for the end of the line number info.  */
8243   ASM_OUTPUT_LABEL (asm_out_file, l2);
8244 }
8245 \f
8246 /* Given a pointer to a tree node for some base type, return a pointer to
8247    a DIE that describes the given type.
8248
8249    This routine must only be called for GCC type nodes that correspond to
8250    Dwarf base (fundamental) types.  */
8251
8252 static dw_die_ref
8253 base_type_die (tree type)
8254 {
8255   dw_die_ref base_type_result;
8256   enum dwarf_type encoding;
8257
8258   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8259     return 0;
8260
8261   switch (TREE_CODE (type))
8262     {
8263     case INTEGER_TYPE:
8264       if (TYPE_STRING_FLAG (type))
8265         {
8266           if (TYPE_UNSIGNED (type))
8267             encoding = DW_ATE_unsigned_char;
8268           else
8269             encoding = DW_ATE_signed_char;
8270         }
8271       else if (TYPE_UNSIGNED (type))
8272         encoding = DW_ATE_unsigned;
8273       else
8274         encoding = DW_ATE_signed;
8275       break;
8276
8277     case REAL_TYPE:
8278       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8279         encoding = DW_ATE_decimal_float;
8280       else
8281         encoding = DW_ATE_float;
8282       break;
8283
8284       /* Dwarf2 doesn't know anything about complex ints, so use
8285          a user defined type for it.  */
8286     case COMPLEX_TYPE:
8287       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8288         encoding = DW_ATE_complex_float;
8289       else
8290         encoding = DW_ATE_lo_user;
8291       break;
8292
8293     case BOOLEAN_TYPE:
8294       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8295       encoding = DW_ATE_boolean;
8296       break;
8297
8298     default:
8299       /* No other TREE_CODEs are Dwarf fundamental types.  */
8300       gcc_unreachable ();
8301     }
8302
8303   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8304
8305   /* This probably indicates a bug.  */
8306   if (! TYPE_NAME (type))
8307     add_name_attribute (base_type_result, "__unknown__");
8308
8309   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8310                    int_size_in_bytes (type));
8311   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8312
8313   return base_type_result;
8314 }
8315
8316 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8317    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8318
8319 static inline int
8320 is_base_type (tree type)
8321 {
8322   switch (TREE_CODE (type))
8323     {
8324     case ERROR_MARK:
8325     case VOID_TYPE:
8326     case INTEGER_TYPE:
8327     case REAL_TYPE:
8328     case COMPLEX_TYPE:
8329     case BOOLEAN_TYPE:
8330       return 1;
8331
8332     case ARRAY_TYPE:
8333     case RECORD_TYPE:
8334     case UNION_TYPE:
8335     case QUAL_UNION_TYPE:
8336     case ENUMERAL_TYPE:
8337     case FUNCTION_TYPE:
8338     case METHOD_TYPE:
8339     case POINTER_TYPE:
8340     case REFERENCE_TYPE:
8341     case OFFSET_TYPE:
8342     case LANG_TYPE:
8343     case VECTOR_TYPE:
8344       return 0;
8345
8346     default:
8347       gcc_unreachable ();
8348     }
8349
8350   return 0;
8351 }
8352
8353 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8354    node, return the size in bits for the type if it is a constant, or else
8355    return the alignment for the type if the type's size is not constant, or
8356    else return BITS_PER_WORD if the type actually turns out to be an
8357    ERROR_MARK node.  */
8358
8359 static inline unsigned HOST_WIDE_INT
8360 simple_type_size_in_bits (tree type)
8361 {
8362   if (TREE_CODE (type) == ERROR_MARK)
8363     return BITS_PER_WORD;
8364   else if (TYPE_SIZE (type) == NULL_TREE)
8365     return 0;
8366   else if (host_integerp (TYPE_SIZE (type), 1))
8367     return tree_low_cst (TYPE_SIZE (type), 1);
8368   else
8369     return TYPE_ALIGN (type);
8370 }
8371
8372 /* Return true if the debug information for the given type should be
8373    emitted as a subrange type.  */
8374
8375 static inline bool
8376 is_subrange_type (tree type)
8377 {
8378   tree subtype = TREE_TYPE (type);
8379
8380   /* Subrange types are identified by the fact that they are integer
8381      types, and that they have a subtype which is either an integer type
8382      or an enumeral type.  */
8383
8384   if (TREE_CODE (type) != INTEGER_TYPE
8385       || subtype == NULL_TREE)
8386     return false;
8387
8388   if (TREE_CODE (subtype) != INTEGER_TYPE
8389       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8390     return false;
8391
8392   if (TREE_CODE (type) == TREE_CODE (subtype)
8393       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8394       && TYPE_MIN_VALUE (type) != NULL
8395       && TYPE_MIN_VALUE (subtype) != NULL
8396       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8397       && TYPE_MAX_VALUE (type) != NULL
8398       && TYPE_MAX_VALUE (subtype) != NULL
8399       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8400     {
8401       /* The type and its subtype have the same representation.  If in
8402          addition the two types also have the same name, then the given
8403          type is not a subrange type, but rather a plain base type.  */
8404       /* FIXME: brobecker/2004-03-22:
8405          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8406          therefore be sufficient to check the TYPE_SIZE node pointers
8407          rather than checking the actual size.  Unfortunately, we have
8408          found some cases, such as in the Ada "integer" type, where
8409          this is not the case.  Until this problem is solved, we need to
8410          keep checking the actual size.  */
8411       tree type_name = TYPE_NAME (type);
8412       tree subtype_name = TYPE_NAME (subtype);
8413
8414       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8415         type_name = DECL_NAME (type_name);
8416
8417       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8418         subtype_name = DECL_NAME (subtype_name);
8419
8420       if (type_name == subtype_name)
8421         return false;
8422     }
8423
8424   return true;
8425 }
8426
8427 /*  Given a pointer to a tree node for a subrange type, return a pointer
8428     to a DIE that describes the given type.  */
8429
8430 static dw_die_ref
8431 subrange_type_die (tree type, dw_die_ref context_die)
8432 {
8433   dw_die_ref subrange_die;
8434   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8435
8436   if (context_die == NULL)
8437     context_die = comp_unit_die;
8438
8439   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8440
8441   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8442     {
8443       /* The size of the subrange type and its base type do not match,
8444          so we need to generate a size attribute for the subrange type.  */
8445       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8446     }
8447
8448   if (TYPE_MIN_VALUE (type) != NULL)
8449     add_bound_info (subrange_die, DW_AT_lower_bound,
8450                     TYPE_MIN_VALUE (type));
8451   if (TYPE_MAX_VALUE (type) != NULL)
8452     add_bound_info (subrange_die, DW_AT_upper_bound,
8453                     TYPE_MAX_VALUE (type));
8454
8455   return subrange_die;
8456 }
8457
8458 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8459    entry that chains various modifiers in front of the given type.  */
8460
8461 static dw_die_ref
8462 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8463                    dw_die_ref context_die)
8464 {
8465   enum tree_code code = TREE_CODE (type);
8466   dw_die_ref mod_type_die;
8467   dw_die_ref sub_die = NULL;
8468   tree item_type = NULL;
8469   tree qualified_type;
8470   tree name;
8471
8472   if (code == ERROR_MARK)
8473     return NULL;
8474
8475   /* See if we already have the appropriately qualified variant of
8476      this type.  */
8477   qualified_type
8478     = get_qualified_type (type,
8479                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8480                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8481
8482   /* If we do, then we can just use its DIE, if it exists.  */
8483   if (qualified_type)
8484     {
8485       mod_type_die = lookup_type_die (qualified_type);
8486       if (mod_type_die)
8487         return mod_type_die;
8488     }
8489
8490   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8491
8492   /* Handle C typedef types.  */
8493   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8494     {
8495       tree dtype = TREE_TYPE (name);
8496
8497       if (qualified_type == dtype)
8498         {
8499           /* For a named type, use the typedef.  */
8500           gen_type_die (qualified_type, context_die);
8501           return lookup_type_die (qualified_type);
8502         }
8503       else if (is_const_type < TYPE_READONLY (dtype)
8504                || is_volatile_type < TYPE_VOLATILE (dtype)
8505                || (is_const_type <= TYPE_READONLY (dtype)
8506                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8507                    && DECL_ORIGINAL_TYPE (name) != type))
8508         /* cv-unqualified version of named type.  Just use the unnamed
8509            type to which it refers.  */
8510         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8511                                   is_const_type, is_volatile_type,
8512                                   context_die);
8513       /* Else cv-qualified version of named type; fall through.  */
8514     }
8515
8516   if (is_const_type)
8517     {
8518       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8519       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8520     }
8521   else if (is_volatile_type)
8522     {
8523       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8524       sub_die = modified_type_die (type, 0, 0, context_die);
8525     }
8526   else if (code == POINTER_TYPE)
8527     {
8528       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8529       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8530                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8531       item_type = TREE_TYPE (type);
8532     }
8533   else if (code == REFERENCE_TYPE)
8534     {
8535       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8536       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8537                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8538       item_type = TREE_TYPE (type);
8539     }
8540   else if (is_subrange_type (type))
8541     {
8542       mod_type_die = subrange_type_die (type, context_die);
8543       item_type = TREE_TYPE (type);
8544     }
8545   else if (is_base_type (type))
8546     mod_type_die = base_type_die (type);
8547   else
8548     {
8549       gen_type_die (type, context_die);
8550
8551       /* We have to get the type_main_variant here (and pass that to the
8552          `lookup_type_die' routine) because the ..._TYPE node we have
8553          might simply be a *copy* of some original type node (where the
8554          copy was created to help us keep track of typedef names) and
8555          that copy might have a different TYPE_UID from the original
8556          ..._TYPE node.  */
8557       if (TREE_CODE (type) != VECTOR_TYPE)
8558         return lookup_type_die (type_main_variant (type));
8559       else
8560         /* Vectors have the debugging information in the type,
8561            not the main variant.  */
8562         return lookup_type_die (type);
8563     }
8564
8565   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8566      don't output a DW_TAG_typedef, since there isn't one in the
8567      user's program; just attach a DW_AT_name to the type.  */
8568   if (name
8569       && (TREE_CODE (name) != TYPE_DECL || TREE_TYPE (name) == qualified_type))
8570     {
8571       if (TREE_CODE (name) == TYPE_DECL)
8572         /* Could just call add_name_and_src_coords_attributes here,
8573            but since this is a builtin type it doesn't have any
8574            useful source coordinates anyway.  */
8575         name = DECL_NAME (name);
8576       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8577     }
8578
8579   if (qualified_type)
8580     equate_type_number_to_die (qualified_type, mod_type_die);
8581
8582   if (item_type)
8583     /* We must do this after the equate_type_number_to_die call, in case
8584        this is a recursive type.  This ensures that the modified_type_die
8585        recursion will terminate even if the type is recursive.  Recursive
8586        types are possible in Ada.  */
8587     sub_die = modified_type_die (item_type,
8588                                  TYPE_READONLY (item_type),
8589                                  TYPE_VOLATILE (item_type),
8590                                  context_die);
8591
8592   if (sub_die != NULL)
8593     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8594
8595   return mod_type_die;
8596 }
8597
8598 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8599    an enumerated type.  */
8600
8601 static inline int
8602 type_is_enum (tree type)
8603 {
8604   return TREE_CODE (type) == ENUMERAL_TYPE;
8605 }
8606
8607 /* Return the DBX register number described by a given RTL node.  */
8608
8609 static unsigned int
8610 dbx_reg_number (rtx rtl)
8611 {
8612   unsigned regno = REGNO (rtl);
8613
8614   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8615
8616 #ifdef LEAF_REG_REMAP
8617   if (current_function_uses_only_leaf_regs)
8618     {
8619       int leaf_reg = LEAF_REG_REMAP (regno);
8620       if (leaf_reg != -1)
8621         regno = (unsigned) leaf_reg;
8622     }
8623 #endif
8624
8625   return DBX_REGISTER_NUMBER (regno);
8626 }
8627
8628 /* Optionally add a DW_OP_piece term to a location description expression.
8629    DW_OP_piece is only added if the location description expression already
8630    doesn't end with DW_OP_piece.  */
8631
8632 static void
8633 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8634 {
8635   dw_loc_descr_ref loc;
8636
8637   if (*list_head != NULL)
8638     {
8639       /* Find the end of the chain.  */
8640       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8641         ;
8642
8643       if (loc->dw_loc_opc != DW_OP_piece)
8644         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8645     }
8646 }
8647
8648 /* Return a location descriptor that designates a machine register or
8649    zero if there is none.  */
8650
8651 static dw_loc_descr_ref
8652 reg_loc_descriptor (rtx rtl)
8653 {
8654   rtx regs;
8655
8656   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8657     return 0;
8658
8659   regs = targetm.dwarf_register_span (rtl);
8660
8661   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8662     return multiple_reg_loc_descriptor (rtl, regs);
8663   else
8664     return one_reg_loc_descriptor (dbx_reg_number (rtl));
8665 }
8666
8667 /* Return a location descriptor that designates a machine register for
8668    a given hard register number.  */
8669
8670 static dw_loc_descr_ref
8671 one_reg_loc_descriptor (unsigned int regno)
8672 {
8673   if (regno <= 31)
8674     return new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8675   else
8676     return new_loc_descr (DW_OP_regx, regno, 0);
8677 }
8678
8679 /* Given an RTL of a register, return a location descriptor that
8680    designates a value that spans more than one register.  */
8681
8682 static dw_loc_descr_ref
8683 multiple_reg_loc_descriptor (rtx rtl, rtx regs)
8684 {
8685   int nregs, size, i;
8686   unsigned reg;
8687   dw_loc_descr_ref loc_result = NULL;
8688
8689   reg = REGNO (rtl);
8690 #ifdef LEAF_REG_REMAP
8691   if (current_function_uses_only_leaf_regs)
8692     {
8693       int leaf_reg = LEAF_REG_REMAP (reg);
8694       if (leaf_reg != -1)
8695         reg = (unsigned) leaf_reg;
8696     }
8697 #endif
8698   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8699   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8700
8701   /* Simple, contiguous registers.  */
8702   if (regs == NULL_RTX)
8703     {
8704       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8705
8706       loc_result = NULL;
8707       while (nregs--)
8708         {
8709           dw_loc_descr_ref t;
8710
8711           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg));
8712           add_loc_descr (&loc_result, t);
8713           add_loc_descr_op_piece (&loc_result, size);
8714           ++reg;
8715         }
8716       return loc_result;
8717     }
8718
8719   /* Now onto stupid register sets in non contiguous locations.  */
8720
8721   gcc_assert (GET_CODE (regs) == PARALLEL);
8722
8723   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8724   loc_result = NULL;
8725
8726   for (i = 0; i < XVECLEN (regs, 0); ++i)
8727     {
8728       dw_loc_descr_ref t;
8729
8730       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)));
8731       add_loc_descr (&loc_result, t);
8732       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8733       add_loc_descr_op_piece (&loc_result, size);
8734     }
8735   return loc_result;
8736 }
8737
8738 /* Return a location descriptor that designates a constant.  */
8739
8740 static dw_loc_descr_ref
8741 int_loc_descriptor (HOST_WIDE_INT i)
8742 {
8743   enum dwarf_location_atom op;
8744
8745   /* Pick the smallest representation of a constant, rather than just
8746      defaulting to the LEB encoding.  */
8747   if (i >= 0)
8748     {
8749       if (i <= 31)
8750         op = DW_OP_lit0 + i;
8751       else if (i <= 0xff)
8752         op = DW_OP_const1u;
8753       else if (i <= 0xffff)
8754         op = DW_OP_const2u;
8755       else if (HOST_BITS_PER_WIDE_INT == 32
8756                || i <= 0xffffffff)
8757         op = DW_OP_const4u;
8758       else
8759         op = DW_OP_constu;
8760     }
8761   else
8762     {
8763       if (i >= -0x80)
8764         op = DW_OP_const1s;
8765       else if (i >= -0x8000)
8766         op = DW_OP_const2s;
8767       else if (HOST_BITS_PER_WIDE_INT == 32
8768                || i >= -0x80000000)
8769         op = DW_OP_const4s;
8770       else
8771         op = DW_OP_consts;
8772     }
8773
8774   return new_loc_descr (op, i, 0);
8775 }
8776
8777 /* Return a location descriptor that designates a base+offset location.  */
8778
8779 static dw_loc_descr_ref
8780 based_loc_descr (rtx reg, HOST_WIDE_INT offset)
8781 {
8782   unsigned int regno;
8783
8784   /* We only use "frame base" when we're sure we're talking about the
8785      post-prologue local stack frame.  We do this by *not* running
8786      register elimination until this point, and recognizing the special
8787      argument pointer and soft frame pointer rtx's.  */
8788   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8789     {
8790       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8791
8792       if (elim != reg)
8793         {
8794           if (GET_CODE (elim) == PLUS)
8795             {
8796               offset += INTVAL (XEXP (elim, 1));
8797               elim = XEXP (elim, 0);
8798             }
8799           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8800                       : stack_pointer_rtx));
8801           offset += frame_pointer_fb_offset;
8802
8803           return new_loc_descr (DW_OP_fbreg, offset, 0);
8804         }
8805     }
8806
8807   regno = dbx_reg_number (reg);
8808   if (regno <= 31)
8809     return new_loc_descr (DW_OP_breg0 + regno, offset, 0);
8810   else
8811     return new_loc_descr (DW_OP_bregx, regno, offset);
8812 }
8813
8814 /* Return true if this RTL expression describes a base+offset calculation.  */
8815
8816 static inline int
8817 is_based_loc (rtx rtl)
8818 {
8819   return (GET_CODE (rtl) == PLUS
8820           && ((REG_P (XEXP (rtl, 0))
8821                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
8822                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
8823 }
8824
8825 /* Return a descriptor that describes the concatenation of N locations
8826    used to form the address of a memory location.  */
8827
8828 static dw_loc_descr_ref
8829 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode)
8830 {
8831   unsigned int i;
8832   dw_loc_descr_ref cc_loc_result = NULL;
8833   unsigned int n = XVECLEN (concatn, 0);
8834
8835   for (i = 0; i < n; ++i)
8836     {
8837       dw_loc_descr_ref ref;
8838       rtx x = XVECEXP (concatn, 0, i);
8839
8840       ref = mem_loc_descriptor (x, mode);
8841       if (ref == NULL)
8842         return NULL;
8843
8844       add_loc_descr (&cc_loc_result, ref);
8845       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
8846     }
8847
8848   return cc_loc_result;
8849 }
8850
8851 /* The following routine converts the RTL for a variable or parameter
8852    (resident in memory) into an equivalent Dwarf representation of a
8853    mechanism for getting the address of that same variable onto the top of a
8854    hypothetical "address evaluation" stack.
8855
8856    When creating memory location descriptors, we are effectively transforming
8857    the RTL for a memory-resident object into its Dwarf postfix expression
8858    equivalent.  This routine recursively descends an RTL tree, turning
8859    it into Dwarf postfix code as it goes.
8860
8861    MODE is the mode of the memory reference, needed to handle some
8862    autoincrement addressing modes.
8863
8864    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
8865    location list for RTL.
8866
8867    Return 0 if we can't represent the location.  */
8868
8869 static dw_loc_descr_ref
8870 mem_loc_descriptor (rtx rtl, enum machine_mode mode)
8871 {
8872   dw_loc_descr_ref mem_loc_result = NULL;
8873   enum dwarf_location_atom op;
8874
8875   /* Note that for a dynamically sized array, the location we will generate a
8876      description of here will be the lowest numbered location which is
8877      actually within the array.  That's *not* necessarily the same as the
8878      zeroth element of the array.  */
8879
8880   rtl = targetm.delegitimize_address (rtl);
8881
8882   switch (GET_CODE (rtl))
8883     {
8884     case POST_INC:
8885     case POST_DEC:
8886     case POST_MODIFY:
8887       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
8888          just fall into the SUBREG code.  */
8889
8890       /* ... fall through ...  */
8891
8892     case SUBREG:
8893       /* The case of a subreg may arise when we have a local (register)
8894          variable or a formal (register) parameter which doesn't quite fill
8895          up an entire register.  For now, just assume that it is
8896          legitimate to make the Dwarf info refer to the whole register which
8897          contains the given subreg.  */
8898       rtl = XEXP (rtl, 0);
8899
8900       /* ... fall through ...  */
8901
8902     case REG:
8903       /* Whenever a register number forms a part of the description of the
8904          method for calculating the (dynamic) address of a memory resident
8905          object, DWARF rules require the register number be referred to as
8906          a "base register".  This distinction is not based in any way upon
8907          what category of register the hardware believes the given register
8908          belongs to.  This is strictly DWARF terminology we're dealing with
8909          here. Note that in cases where the location of a memory-resident
8910          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
8911          OP_CONST (0)) the actual DWARF location descriptor that we generate
8912          may just be OP_BASEREG (basereg).  This may look deceptively like
8913          the object in question was allocated to a register (rather than in
8914          memory) so DWARF consumers need to be aware of the subtle
8915          distinction between OP_REG and OP_BASEREG.  */
8916       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
8917         mem_loc_result = based_loc_descr (rtl, 0);
8918       break;
8919
8920     case MEM:
8921       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
8922       if (mem_loc_result != 0)
8923         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
8924       break;
8925
8926     case LO_SUM:
8927          rtl = XEXP (rtl, 1);
8928
8929       /* ... fall through ...  */
8930
8931     case LABEL_REF:
8932       /* Some ports can transform a symbol ref into a label ref, because
8933          the symbol ref is too far away and has to be dumped into a constant
8934          pool.  */
8935     case CONST:
8936     case SYMBOL_REF:
8937       /* Alternatively, the symbol in the constant pool might be referenced
8938          by a different symbol.  */
8939       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
8940         {
8941           bool marked;
8942           rtx tmp = get_pool_constant_mark (rtl, &marked);
8943
8944           if (GET_CODE (tmp) == SYMBOL_REF)
8945             {
8946               rtl = tmp;
8947               if (CONSTANT_POOL_ADDRESS_P (tmp))
8948                 get_pool_constant_mark (tmp, &marked);
8949               else
8950                 marked = true;
8951             }
8952
8953           /* If all references to this pool constant were optimized away,
8954              it was not output and thus we can't represent it.
8955              FIXME: might try to use DW_OP_const_value here, though
8956              DW_OP_piece complicates it.  */
8957           if (!marked)
8958             return 0;
8959         }
8960
8961       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
8962       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
8963       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
8964       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
8965       break;
8966
8967     case PRE_MODIFY:
8968       /* Extract the PLUS expression nested inside and fall into
8969          PLUS code below.  */
8970       rtl = XEXP (rtl, 1);
8971       goto plus;
8972
8973     case PRE_INC:
8974     case PRE_DEC:
8975       /* Turn these into a PLUS expression and fall into the PLUS code
8976          below.  */
8977       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
8978                           GEN_INT (GET_CODE (rtl) == PRE_INC
8979                                    ? GET_MODE_UNIT_SIZE (mode)
8980                                    : -GET_MODE_UNIT_SIZE (mode)));
8981
8982       /* ... fall through ...  */
8983
8984     case PLUS:
8985     plus:
8986       if (is_based_loc (rtl))
8987         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
8988                                           INTVAL (XEXP (rtl, 1)));
8989       else
8990         {
8991           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode);
8992           if (mem_loc_result == 0)
8993             break;
8994
8995           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
8996               && INTVAL (XEXP (rtl, 1)) >= 0)
8997             add_loc_descr (&mem_loc_result,
8998                            new_loc_descr (DW_OP_plus_uconst,
8999                                           INTVAL (XEXP (rtl, 1)), 0));
9000           else
9001             {
9002               add_loc_descr (&mem_loc_result,
9003                              mem_loc_descriptor (XEXP (rtl, 1), mode));
9004               add_loc_descr (&mem_loc_result,
9005                              new_loc_descr (DW_OP_plus, 0, 0));
9006             }
9007         }
9008       break;
9009
9010     /* If a pseudo-reg is optimized away, it is possible for it to
9011        be replaced with a MEM containing a multiply or shift.  */
9012     case MULT:
9013       op = DW_OP_mul;
9014       goto do_binop;
9015
9016     case ASHIFT:
9017       op = DW_OP_shl;
9018       goto do_binop;
9019
9020     case ASHIFTRT:
9021       op = DW_OP_shra;
9022       goto do_binop;
9023
9024     case LSHIFTRT:
9025       op = DW_OP_shr;
9026       goto do_binop;
9027
9028     do_binop:
9029       {
9030         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode);
9031         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode);
9032
9033         if (op0 == 0 || op1 == 0)
9034           break;
9035
9036         mem_loc_result = op0;
9037         add_loc_descr (&mem_loc_result, op1);
9038         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9039         break;
9040       }
9041
9042     case CONST_INT:
9043       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9044       break;
9045
9046     case CONCATN:
9047       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode);
9048       break;
9049
9050     default:
9051       gcc_unreachable ();
9052     }
9053
9054   return mem_loc_result;
9055 }
9056
9057 /* Return a descriptor that describes the concatenation of two locations.
9058    This is typically a complex variable.  */
9059
9060 static dw_loc_descr_ref
9061 concat_loc_descriptor (rtx x0, rtx x1)
9062 {
9063   dw_loc_descr_ref cc_loc_result = NULL;
9064   dw_loc_descr_ref x0_ref = loc_descriptor (x0);
9065   dw_loc_descr_ref x1_ref = loc_descriptor (x1);
9066
9067   if (x0_ref == 0 || x1_ref == 0)
9068     return 0;
9069
9070   cc_loc_result = x0_ref;
9071   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9072
9073   add_loc_descr (&cc_loc_result, x1_ref);
9074   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9075
9076   return cc_loc_result;
9077 }
9078
9079 /* Return a descriptor that describes the concatenation of N
9080    locations.  */
9081
9082 static dw_loc_descr_ref
9083 concatn_loc_descriptor (rtx concatn)
9084 {
9085   unsigned int i;
9086   dw_loc_descr_ref cc_loc_result = NULL;
9087   unsigned int n = XVECLEN (concatn, 0);
9088
9089   for (i = 0; i < n; ++i)
9090     {
9091       dw_loc_descr_ref ref;
9092       rtx x = XVECEXP (concatn, 0, i);
9093
9094       ref = loc_descriptor (x);
9095       if (ref == NULL)
9096         return NULL;
9097
9098       add_loc_descr (&cc_loc_result, ref);
9099       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9100     }
9101
9102   return cc_loc_result;
9103 }
9104
9105 /* Output a proper Dwarf location descriptor for a variable or parameter
9106    which is either allocated in a register or in a memory location.  For a
9107    register, we just generate an OP_REG and the register number.  For a
9108    memory location we provide a Dwarf postfix expression describing how to
9109    generate the (dynamic) address of the object onto the address stack.
9110
9111    If we don't know how to describe it, return 0.  */
9112
9113 static dw_loc_descr_ref
9114 loc_descriptor (rtx rtl)
9115 {
9116   dw_loc_descr_ref loc_result = NULL;
9117
9118   switch (GET_CODE (rtl))
9119     {
9120     case SUBREG:
9121       /* The case of a subreg may arise when we have a local (register)
9122          variable or a formal (register) parameter which doesn't quite fill
9123          up an entire register.  For now, just assume that it is
9124          legitimate to make the Dwarf info refer to the whole register which
9125          contains the given subreg.  */
9126       rtl = SUBREG_REG (rtl);
9127
9128       /* ... fall through ...  */
9129
9130     case REG:
9131       loc_result = reg_loc_descriptor (rtl);
9132       break;
9133
9134     case MEM:
9135       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl));
9136       break;
9137
9138     case CONCAT:
9139       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1));
9140       break;
9141
9142     case CONCATN:
9143       loc_result = concatn_loc_descriptor (rtl);
9144       break;
9145
9146     case VAR_LOCATION:
9147       /* Single part.  */
9148       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9149         {
9150           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0));
9151           break;
9152         }
9153
9154       rtl = XEXP (rtl, 1);
9155       /* FALLTHRU */
9156
9157     case PARALLEL:
9158       {
9159         rtvec par_elems = XVEC (rtl, 0);
9160         int num_elem = GET_NUM_ELEM (par_elems);
9161         enum machine_mode mode;
9162         int i;
9163
9164         /* Create the first one, so we have something to add to.  */
9165         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0));
9166         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9167         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9168         for (i = 1; i < num_elem; i++)
9169           {
9170             dw_loc_descr_ref temp;
9171
9172             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0));
9173             add_loc_descr (&loc_result, temp);
9174             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9175             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9176           }
9177       }
9178       break;
9179
9180     default:
9181       gcc_unreachable ();
9182     }
9183
9184   return loc_result;
9185 }
9186
9187 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9188    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9189    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9190    top-level invocation, and we require the address of LOC; is 0 if we require
9191    the value of LOC.  */
9192
9193 static dw_loc_descr_ref
9194 loc_descriptor_from_tree_1 (tree loc, int want_address)
9195 {
9196   dw_loc_descr_ref ret, ret1;
9197   int have_address = 0;
9198   enum dwarf_location_atom op;
9199
9200   /* ??? Most of the time we do not take proper care for sign/zero
9201      extending the values properly.  Hopefully this won't be a real
9202      problem...  */
9203
9204   switch (TREE_CODE (loc))
9205     {
9206     case ERROR_MARK:
9207       return 0;
9208
9209     case PLACEHOLDER_EXPR:
9210       /* This case involves extracting fields from an object to determine the
9211          position of other fields.  We don't try to encode this here.  The
9212          only user of this is Ada, which encodes the needed information using
9213          the names of types.  */
9214       return 0;
9215
9216     case CALL_EXPR:
9217       return 0;
9218
9219     case PREINCREMENT_EXPR:
9220     case PREDECREMENT_EXPR:
9221     case POSTINCREMENT_EXPR:
9222     case POSTDECREMENT_EXPR:
9223       /* There are no opcodes for these operations.  */
9224       return 0;
9225
9226     case ADDR_EXPR:
9227       /* If we already want an address, there's nothing we can do.  */
9228       if (want_address)
9229         return 0;
9230
9231       /* Otherwise, process the argument and look for the address.  */
9232       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9233
9234     case VAR_DECL:
9235       if (DECL_THREAD_LOCAL_P (loc))
9236         {
9237           rtx rtl;
9238
9239           /* If this is not defined, we have no way to emit the data.  */
9240           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9241             return 0;
9242
9243           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9244              look up addresses of objects in the current module.  */
9245           if (DECL_EXTERNAL (loc))
9246             return 0;
9247
9248           rtl = rtl_for_decl_location (loc);
9249           if (rtl == NULL_RTX)
9250             return 0;
9251
9252           if (!MEM_P (rtl))
9253             return 0;
9254           rtl = XEXP (rtl, 0);
9255           if (! CONSTANT_P (rtl))
9256             return 0;
9257
9258           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9259           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9260           ret->dw_loc_oprnd1.v.val_addr = rtl;
9261
9262           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9263           add_loc_descr (&ret, ret1);
9264
9265           have_address = 1;
9266           break;
9267         }
9268       /* FALLTHRU */
9269
9270     case PARM_DECL:
9271       if (DECL_HAS_VALUE_EXPR_P (loc))
9272         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9273                                            want_address);
9274       /* FALLTHRU */
9275
9276     case RESULT_DECL:
9277     case FUNCTION_DECL:
9278       {
9279         rtx rtl = rtl_for_decl_location (loc);
9280
9281         if (rtl == NULL_RTX)
9282           return 0;
9283         else if (GET_CODE (rtl) == CONST_INT)
9284           {
9285             HOST_WIDE_INT val = INTVAL (rtl);
9286             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9287               val &= GET_MODE_MASK (DECL_MODE (loc));
9288             ret = int_loc_descriptor (val);
9289           }
9290         else if (GET_CODE (rtl) == CONST_STRING)
9291           return 0;
9292         else if (CONSTANT_P (rtl))
9293           {
9294             ret = new_loc_descr (DW_OP_addr, 0, 0);
9295             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9296             ret->dw_loc_oprnd1.v.val_addr = rtl;
9297           }
9298         else
9299           {
9300             enum machine_mode mode;
9301
9302             /* Certain constructs can only be represented at top-level.  */
9303             if (want_address == 2)
9304               return loc_descriptor (rtl);
9305
9306             mode = GET_MODE (rtl);
9307             if (MEM_P (rtl))
9308               {
9309                 rtl = XEXP (rtl, 0);
9310                 have_address = 1;
9311               }
9312             ret = mem_loc_descriptor (rtl, mode);
9313           }
9314       }
9315       break;
9316
9317     case INDIRECT_REF:
9318       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9319       have_address = 1;
9320       break;
9321
9322     case COMPOUND_EXPR:
9323       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9324
9325     case NOP_EXPR:
9326     case CONVERT_EXPR:
9327     case NON_LVALUE_EXPR:
9328     case VIEW_CONVERT_EXPR:
9329     case SAVE_EXPR:
9330     case GIMPLE_MODIFY_STMT:
9331       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9332                                          want_address);
9333
9334     case COMPONENT_REF:
9335     case BIT_FIELD_REF:
9336     case ARRAY_REF:
9337     case ARRAY_RANGE_REF:
9338       {
9339         tree obj, offset;
9340         HOST_WIDE_INT bitsize, bitpos, bytepos;
9341         enum machine_mode mode;
9342         int volatilep;
9343         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9344
9345         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9346                                    &unsignedp, &volatilep, false);
9347
9348         if (obj == loc)
9349           return 0;
9350
9351         ret = loc_descriptor_from_tree_1 (obj, 1);
9352         if (ret == 0
9353             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9354           return 0;
9355
9356         if (offset != NULL_TREE)
9357           {
9358             /* Variable offset.  */
9359             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9360             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9361           }
9362
9363         bytepos = bitpos / BITS_PER_UNIT;
9364         if (bytepos > 0)
9365           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9366         else if (bytepos < 0)
9367           {
9368             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9369             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9370           }
9371
9372         have_address = 1;
9373         break;
9374       }
9375
9376     case INTEGER_CST:
9377       if (host_integerp (loc, 0))
9378         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9379       else
9380         return 0;
9381       break;
9382
9383     case CONSTRUCTOR:
9384       {
9385         /* Get an RTL for this, if something has been emitted.  */
9386         rtx rtl = lookup_constant_def (loc);
9387         enum machine_mode mode;
9388
9389         if (!rtl || !MEM_P (rtl))
9390           return 0;
9391         mode = GET_MODE (rtl);
9392         rtl = XEXP (rtl, 0);
9393         ret = mem_loc_descriptor (rtl, mode);
9394         have_address = 1;
9395         break;
9396       }
9397
9398     case TRUTH_AND_EXPR:
9399     case TRUTH_ANDIF_EXPR:
9400     case BIT_AND_EXPR:
9401       op = DW_OP_and;
9402       goto do_binop;
9403
9404     case TRUTH_XOR_EXPR:
9405     case BIT_XOR_EXPR:
9406       op = DW_OP_xor;
9407       goto do_binop;
9408
9409     case TRUTH_OR_EXPR:
9410     case TRUTH_ORIF_EXPR:
9411     case BIT_IOR_EXPR:
9412       op = DW_OP_or;
9413       goto do_binop;
9414
9415     case FLOOR_DIV_EXPR:
9416     case CEIL_DIV_EXPR:
9417     case ROUND_DIV_EXPR:
9418     case TRUNC_DIV_EXPR:
9419       op = DW_OP_div;
9420       goto do_binop;
9421
9422     case MINUS_EXPR:
9423       op = DW_OP_minus;
9424       goto do_binop;
9425
9426     case FLOOR_MOD_EXPR:
9427     case CEIL_MOD_EXPR:
9428     case ROUND_MOD_EXPR:
9429     case TRUNC_MOD_EXPR:
9430       op = DW_OP_mod;
9431       goto do_binop;
9432
9433     case MULT_EXPR:
9434       op = DW_OP_mul;
9435       goto do_binop;
9436
9437     case LSHIFT_EXPR:
9438       op = DW_OP_shl;
9439       goto do_binop;
9440
9441     case RSHIFT_EXPR:
9442       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9443       goto do_binop;
9444
9445     case PLUS_EXPR:
9446       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9447           && host_integerp (TREE_OPERAND (loc, 1), 0))
9448         {
9449           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9450           if (ret == 0)
9451             return 0;
9452
9453           add_loc_descr (&ret,
9454                          new_loc_descr (DW_OP_plus_uconst,
9455                                         tree_low_cst (TREE_OPERAND (loc, 1),
9456                                                       0),
9457                                         0));
9458           break;
9459         }
9460
9461       op = DW_OP_plus;
9462       goto do_binop;
9463
9464     case LE_EXPR:
9465       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9466         return 0;
9467
9468       op = DW_OP_le;
9469       goto do_binop;
9470
9471     case GE_EXPR:
9472       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9473         return 0;
9474
9475       op = DW_OP_ge;
9476       goto do_binop;
9477
9478     case LT_EXPR:
9479       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9480         return 0;
9481
9482       op = DW_OP_lt;
9483       goto do_binop;
9484
9485     case GT_EXPR:
9486       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9487         return 0;
9488
9489       op = DW_OP_gt;
9490       goto do_binop;
9491
9492     case EQ_EXPR:
9493       op = DW_OP_eq;
9494       goto do_binop;
9495
9496     case NE_EXPR:
9497       op = DW_OP_ne;
9498       goto do_binop;
9499
9500     do_binop:
9501       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9502       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9503       if (ret == 0 || ret1 == 0)
9504         return 0;
9505
9506       add_loc_descr (&ret, ret1);
9507       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9508       break;
9509
9510     case TRUTH_NOT_EXPR:
9511     case BIT_NOT_EXPR:
9512       op = DW_OP_not;
9513       goto do_unop;
9514
9515     case ABS_EXPR:
9516       op = DW_OP_abs;
9517       goto do_unop;
9518
9519     case NEGATE_EXPR:
9520       op = DW_OP_neg;
9521       goto do_unop;
9522
9523     do_unop:
9524       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9525       if (ret == 0)
9526         return 0;
9527
9528       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9529       break;
9530
9531     case MIN_EXPR:
9532     case MAX_EXPR:
9533       {
9534         const enum tree_code code =
9535           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9536
9537         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9538                       build2 (code, integer_type_node,
9539                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9540                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9541       }
9542
9543       /* ... fall through ...  */
9544
9545     case COND_EXPR:
9546       {
9547         dw_loc_descr_ref lhs
9548           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9549         dw_loc_descr_ref rhs
9550           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9551         dw_loc_descr_ref bra_node, jump_node, tmp;
9552
9553         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9554         if (ret == 0 || lhs == 0 || rhs == 0)
9555           return 0;
9556
9557         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9558         add_loc_descr (&ret, bra_node);
9559
9560         add_loc_descr (&ret, rhs);
9561         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9562         add_loc_descr (&ret, jump_node);
9563
9564         add_loc_descr (&ret, lhs);
9565         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9566         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9567
9568         /* ??? Need a node to point the skip at.  Use a nop.  */
9569         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9570         add_loc_descr (&ret, tmp);
9571         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9572         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9573       }
9574       break;
9575
9576     case FIX_TRUNC_EXPR:
9577       return 0;
9578
9579     default:
9580       /* Leave front-end specific codes as simply unknown.  This comes
9581          up, for instance, with the C STMT_EXPR.  */
9582       if ((unsigned int) TREE_CODE (loc)
9583           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9584         return 0;
9585
9586 #ifdef ENABLE_CHECKING
9587       /* Otherwise this is a generic code; we should just lists all of
9588          these explicitly.  We forgot one.  */
9589       gcc_unreachable ();
9590 #else
9591       /* In a release build, we want to degrade gracefully: better to
9592          generate incomplete debugging information than to crash.  */
9593       return NULL;
9594 #endif
9595     }
9596
9597   /* Show if we can't fill the request for an address.  */
9598   if (want_address && !have_address)
9599     return 0;
9600
9601   /* If we've got an address and don't want one, dereference.  */
9602   if (!want_address && have_address && ret)
9603     {
9604       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9605
9606       if (size > DWARF2_ADDR_SIZE || size == -1)
9607         return 0;
9608       else if (size == DWARF2_ADDR_SIZE)
9609         op = DW_OP_deref;
9610       else
9611         op = DW_OP_deref_size;
9612
9613       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9614     }
9615
9616   return ret;
9617 }
9618
9619 static inline dw_loc_descr_ref
9620 loc_descriptor_from_tree (tree loc)
9621 {
9622   return loc_descriptor_from_tree_1 (loc, 2);
9623 }
9624
9625 /* Given a value, round it up to the lowest multiple of `boundary'
9626    which is not less than the value itself.  */
9627
9628 static inline HOST_WIDE_INT
9629 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9630 {
9631   return (((value + boundary - 1) / boundary) * boundary);
9632 }
9633
9634 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9635    pointer to the declared type for the relevant field variable, or return
9636    `integer_type_node' if the given node turns out to be an
9637    ERROR_MARK node.  */
9638
9639 static inline tree
9640 field_type (tree decl)
9641 {
9642   tree type;
9643
9644   if (TREE_CODE (decl) == ERROR_MARK)
9645     return integer_type_node;
9646
9647   type = DECL_BIT_FIELD_TYPE (decl);
9648   if (type == NULL_TREE)
9649     type = TREE_TYPE (decl);
9650
9651   return type;
9652 }
9653
9654 /* Given a pointer to a tree node, return the alignment in bits for
9655    it, or else return BITS_PER_WORD if the node actually turns out to
9656    be an ERROR_MARK node.  */
9657
9658 static inline unsigned
9659 simple_type_align_in_bits (tree type)
9660 {
9661   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9662 }
9663
9664 static inline unsigned
9665 simple_decl_align_in_bits (tree decl)
9666 {
9667   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9668 }
9669
9670 /* Return the result of rounding T up to ALIGN.  */
9671
9672 static inline HOST_WIDE_INT
9673 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9674 {
9675   /* We must be careful if T is negative because HOST_WIDE_INT can be
9676      either "above" or "below" unsigned int as per the C promotion
9677      rules, depending on the host, thus making the signedness of the
9678      direct multiplication and division unpredictable.  */
9679   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9680
9681   u += align - 1;
9682   u /= align;
9683   u *= align;
9684
9685   return (HOST_WIDE_INT) u;
9686 }
9687
9688 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9689    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9690    or return 0 if we are unable to determine what that offset is, either
9691    because the argument turns out to be a pointer to an ERROR_MARK node, or
9692    because the offset is actually variable.  (We can't handle the latter case
9693    just yet).  */
9694
9695 static HOST_WIDE_INT
9696 field_byte_offset (tree decl)
9697 {
9698   HOST_WIDE_INT object_offset_in_bits;
9699   HOST_WIDE_INT bitpos_int;
9700
9701   if (TREE_CODE (decl) == ERROR_MARK)
9702     return 0;
9703
9704   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9705
9706   /* We cannot yet cope with fields whose positions are variable, so
9707      for now, when we see such things, we simply return 0.  Someday, we may
9708      be able to handle such cases, but it will be damn difficult.  */
9709   if (! host_integerp (bit_position (decl), 0))
9710     return 0;
9711
9712   bitpos_int = int_bit_position (decl);
9713
9714 #ifdef PCC_BITFIELD_TYPE_MATTERS
9715   if (PCC_BITFIELD_TYPE_MATTERS)
9716     {
9717       tree type;
9718       tree field_size_tree;
9719       HOST_WIDE_INT deepest_bitpos;
9720       unsigned HOST_WIDE_INT field_size_in_bits;
9721       unsigned int type_align_in_bits;
9722       unsigned int decl_align_in_bits;
9723       unsigned HOST_WIDE_INT type_size_in_bits;
9724
9725       type = field_type (decl);
9726       field_size_tree = DECL_SIZE (decl);
9727
9728       /* The size could be unspecified if there was an error, or for
9729          a flexible array member.  */
9730       if (! field_size_tree)
9731         field_size_tree = bitsize_zero_node;
9732
9733       /* If we don't know the size of the field, pretend it's a full word.  */
9734       if (host_integerp (field_size_tree, 1))
9735         field_size_in_bits = tree_low_cst (field_size_tree, 1);
9736       else
9737         field_size_in_bits = BITS_PER_WORD;
9738
9739       type_size_in_bits = simple_type_size_in_bits (type);
9740       type_align_in_bits = simple_type_align_in_bits (type);
9741       decl_align_in_bits = simple_decl_align_in_bits (decl);
9742
9743       /* The GCC front-end doesn't make any attempt to keep track of the
9744          starting bit offset (relative to the start of the containing
9745          structure type) of the hypothetical "containing object" for a
9746          bit-field.  Thus, when computing the byte offset value for the
9747          start of the "containing object" of a bit-field, we must deduce
9748          this information on our own. This can be rather tricky to do in
9749          some cases.  For example, handling the following structure type
9750          definition when compiling for an i386/i486 target (which only
9751          aligns long long's to 32-bit boundaries) can be very tricky:
9752
9753          struct S { int field1; long long field2:31; };
9754
9755          Fortunately, there is a simple rule-of-thumb which can be used
9756          in such cases.  When compiling for an i386/i486, GCC will
9757          allocate 8 bytes for the structure shown above.  It decides to
9758          do this based upon one simple rule for bit-field allocation.
9759          GCC allocates each "containing object" for each bit-field at
9760          the first (i.e. lowest addressed) legitimate alignment boundary
9761          (based upon the required minimum alignment for the declared
9762          type of the field) which it can possibly use, subject to the
9763          condition that there is still enough available space remaining
9764          in the containing object (when allocated at the selected point)
9765          to fully accommodate all of the bits of the bit-field itself.
9766
9767          This simple rule makes it obvious why GCC allocates 8 bytes for
9768          each object of the structure type shown above.  When looking
9769          for a place to allocate the "containing object" for `field2',
9770          the compiler simply tries to allocate a 64-bit "containing
9771          object" at each successive 32-bit boundary (starting at zero)
9772          until it finds a place to allocate that 64- bit field such that
9773          at least 31 contiguous (and previously unallocated) bits remain
9774          within that selected 64 bit field.  (As it turns out, for the
9775          example above, the compiler finds it is OK to allocate the
9776          "containing object" 64-bit field at bit-offset zero within the
9777          structure type.)
9778
9779          Here we attempt to work backwards from the limited set of facts
9780          we're given, and we try to deduce from those facts, where GCC
9781          must have believed that the containing object started (within
9782          the structure type). The value we deduce is then used (by the
9783          callers of this routine) to generate DW_AT_location and
9784          DW_AT_bit_offset attributes for fields (both bit-fields and, in
9785          the case of DW_AT_location, regular fields as well).  */
9786
9787       /* Figure out the bit-distance from the start of the structure to
9788          the "deepest" bit of the bit-field.  */
9789       deepest_bitpos = bitpos_int + field_size_in_bits;
9790
9791       /* This is the tricky part.  Use some fancy footwork to deduce
9792          where the lowest addressed bit of the containing object must
9793          be.  */
9794       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9795
9796       /* Round up to type_align by default.  This works best for
9797          bitfields.  */
9798       object_offset_in_bits
9799         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
9800
9801       if (object_offset_in_bits > bitpos_int)
9802         {
9803           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
9804
9805           /* Round up to decl_align instead.  */
9806           object_offset_in_bits
9807             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
9808         }
9809     }
9810   else
9811 #endif
9812     object_offset_in_bits = bitpos_int;
9813
9814   return object_offset_in_bits / BITS_PER_UNIT;
9815 }
9816 \f
9817 /* The following routines define various Dwarf attributes and any data
9818    associated with them.  */
9819
9820 /* Add a location description attribute value to a DIE.
9821
9822    This emits location attributes suitable for whole variables and
9823    whole parameters.  Note that the location attributes for struct fields are
9824    generated by the routine `data_member_location_attribute' below.  */
9825
9826 static inline void
9827 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
9828                              dw_loc_descr_ref descr)
9829 {
9830   if (descr != 0)
9831     add_AT_loc (die, attr_kind, descr);
9832 }
9833
9834 /* Attach the specialized form of location attribute used for data members of
9835    struct and union types.  In the special case of a FIELD_DECL node which
9836    represents a bit-field, the "offset" part of this special location
9837    descriptor must indicate the distance in bytes from the lowest-addressed
9838    byte of the containing struct or union type to the lowest-addressed byte of
9839    the "containing object" for the bit-field.  (See the `field_byte_offset'
9840    function above).
9841
9842    For any given bit-field, the "containing object" is a hypothetical object
9843    (of some integral or enum type) within which the given bit-field lives.  The
9844    type of this hypothetical "containing object" is always the same as the
9845    declared type of the individual bit-field itself (for GCC anyway... the
9846    DWARF spec doesn't actually mandate this).  Note that it is the size (in
9847    bytes) of the hypothetical "containing object" which will be given in the
9848    DW_AT_byte_size attribute for this bit-field.  (See the
9849    `byte_size_attribute' function below.)  It is also used when calculating the
9850    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
9851    function below.)  */
9852
9853 static void
9854 add_data_member_location_attribute (dw_die_ref die, tree decl)
9855 {
9856   HOST_WIDE_INT offset;
9857   dw_loc_descr_ref loc_descr = 0;
9858
9859   if (TREE_CODE (decl) == TREE_BINFO)
9860     {
9861       /* We're working on the TAG_inheritance for a base class.  */
9862       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
9863         {
9864           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
9865              aren't at a fixed offset from all (sub)objects of the same
9866              type.  We need to extract the appropriate offset from our
9867              vtable.  The following dwarf expression means
9868
9869                BaseAddr = ObAddr + *((*ObAddr) - Offset)
9870
9871              This is specific to the V3 ABI, of course.  */
9872
9873           dw_loc_descr_ref tmp;
9874
9875           /* Make a copy of the object address.  */
9876           tmp = new_loc_descr (DW_OP_dup, 0, 0);
9877           add_loc_descr (&loc_descr, tmp);
9878
9879           /* Extract the vtable address.  */
9880           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9881           add_loc_descr (&loc_descr, tmp);
9882
9883           /* Calculate the address of the offset.  */
9884           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
9885           gcc_assert (offset < 0);
9886
9887           tmp = int_loc_descriptor (-offset);
9888           add_loc_descr (&loc_descr, tmp);
9889           tmp = new_loc_descr (DW_OP_minus, 0, 0);
9890           add_loc_descr (&loc_descr, tmp);
9891
9892           /* Extract the offset.  */
9893           tmp = new_loc_descr (DW_OP_deref, 0, 0);
9894           add_loc_descr (&loc_descr, tmp);
9895
9896           /* Add it to the object address.  */
9897           tmp = new_loc_descr (DW_OP_plus, 0, 0);
9898           add_loc_descr (&loc_descr, tmp);
9899         }
9900       else
9901         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
9902     }
9903   else
9904     offset = field_byte_offset (decl);
9905
9906   if (! loc_descr)
9907     {
9908       enum dwarf_location_atom op;
9909
9910       /* The DWARF2 standard says that we should assume that the structure
9911          address is already on the stack, so we can specify a structure field
9912          address by using DW_OP_plus_uconst.  */
9913
9914 #ifdef MIPS_DEBUGGING_INFO
9915       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
9916          operator correctly.  It works only if we leave the offset on the
9917          stack.  */
9918       op = DW_OP_constu;
9919 #else
9920       op = DW_OP_plus_uconst;
9921 #endif
9922
9923       loc_descr = new_loc_descr (op, offset, 0);
9924     }
9925
9926   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
9927 }
9928
9929 /* Writes integer values to dw_vec_const array.  */
9930
9931 static void
9932 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
9933 {
9934   while (size != 0)
9935     {
9936       *dest++ = val & 0xff;
9937       val >>= 8;
9938       --size;
9939     }
9940 }
9941
9942 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
9943
9944 static HOST_WIDE_INT
9945 extract_int (const unsigned char *src, unsigned int size)
9946 {
9947   HOST_WIDE_INT val = 0;
9948
9949   src += size;
9950   while (size != 0)
9951     {
9952       val <<= 8;
9953       val |= *--src & 0xff;
9954       --size;
9955     }
9956   return val;
9957 }
9958
9959 /* Writes floating point values to dw_vec_const array.  */
9960
9961 static void
9962 insert_float (rtx rtl, unsigned char *array)
9963 {
9964   REAL_VALUE_TYPE rv;
9965   long val[4];
9966   int i;
9967
9968   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
9969   real_to_target (val, &rv, GET_MODE (rtl));
9970
9971   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
9972   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
9973     {
9974       insert_int (val[i], 4, array);
9975       array += 4;
9976     }
9977 }
9978
9979 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
9980    does not have a "location" either in memory or in a register.  These
9981    things can arise in GNU C when a constant is passed as an actual parameter
9982    to an inlined function.  They can also arise in C++ where declared
9983    constants do not necessarily get memory "homes".  */
9984
9985 static void
9986 add_const_value_attribute (dw_die_ref die, rtx rtl)
9987 {
9988   switch (GET_CODE (rtl))
9989     {
9990     case CONST_INT:
9991       {
9992         HOST_WIDE_INT val = INTVAL (rtl);
9993
9994         if (val < 0)
9995           add_AT_int (die, DW_AT_const_value, val);
9996         else
9997           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
9998       }
9999       break;
10000
10001     case CONST_DOUBLE:
10002       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10003          floating-point constant.  A CONST_DOUBLE is used whenever the
10004          constant requires more than one word in order to be adequately
10005          represented.  We output CONST_DOUBLEs as blocks.  */
10006       {
10007         enum machine_mode mode = GET_MODE (rtl);
10008
10009         if (SCALAR_FLOAT_MODE_P (mode))
10010           {
10011             unsigned int length = GET_MODE_SIZE (mode);
10012             unsigned char *array = ggc_alloc (length);
10013
10014             insert_float (rtl, array);
10015             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10016           }
10017         else
10018           {
10019             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10020             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10021
10022             add_AT_long_long (die, DW_AT_const_value,
10023                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10024           }
10025       }
10026       break;
10027
10028     case CONST_VECTOR:
10029       {
10030         enum machine_mode mode = GET_MODE (rtl);
10031         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10032         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10033         unsigned char *array = ggc_alloc (length * elt_size);
10034         unsigned int i;
10035         unsigned char *p;
10036
10037         switch (GET_MODE_CLASS (mode))
10038           {
10039           case MODE_VECTOR_INT:
10040             for (i = 0, p = array; i < length; i++, p += elt_size)
10041               {
10042                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10043                 HOST_WIDE_INT lo, hi;
10044
10045                 switch (GET_CODE (elt))
10046                   {
10047                   case CONST_INT:
10048                     lo = INTVAL (elt);
10049                     hi = -(lo < 0);
10050                     break;
10051
10052                   case CONST_DOUBLE:
10053                     lo = CONST_DOUBLE_LOW (elt);
10054                     hi = CONST_DOUBLE_HIGH (elt);
10055                     break;
10056
10057                   default:
10058                     gcc_unreachable ();
10059                   }
10060
10061                 if (elt_size <= sizeof (HOST_WIDE_INT))
10062                   insert_int (lo, elt_size, p);
10063                 else
10064                   {
10065                     unsigned char *p0 = p;
10066                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10067
10068                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10069                     if (WORDS_BIG_ENDIAN)
10070                       {
10071                         p0 = p1;
10072                         p1 = p;
10073                       }
10074                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10075                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10076                   }
10077               }
10078             break;
10079
10080           case MODE_VECTOR_FLOAT:
10081             for (i = 0, p = array; i < length; i++, p += elt_size)
10082               {
10083                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10084                 insert_float (elt, p);
10085               }
10086             break;
10087
10088           default:
10089             gcc_unreachable ();
10090           }
10091
10092         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10093       }
10094       break;
10095
10096     case CONST_STRING:
10097       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10098       break;
10099
10100     case SYMBOL_REF:
10101     case LABEL_REF:
10102     case CONST:
10103       add_AT_addr (die, DW_AT_const_value, rtl);
10104       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10105       break;
10106
10107     case PLUS:
10108       /* In cases where an inlined instance of an inline function is passed
10109          the address of an `auto' variable (which is local to the caller) we
10110          can get a situation where the DECL_RTL of the artificial local
10111          variable (for the inlining) which acts as a stand-in for the
10112          corresponding formal parameter (of the inline function) will look
10113          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10114          exactly a compile-time constant expression, but it isn't the address
10115          of the (artificial) local variable either.  Rather, it represents the
10116          *value* which the artificial local variable always has during its
10117          lifetime.  We currently have no way to represent such quasi-constant
10118          values in Dwarf, so for now we just punt and generate nothing.  */
10119       break;
10120
10121     default:
10122       /* No other kinds of rtx should be possible here.  */
10123       gcc_unreachable ();
10124     }
10125
10126 }
10127
10128 /* Determine whether the evaluation of EXPR references any variables
10129    or functions which aren't otherwise used (and therefore may not be
10130    output).  */
10131 static tree
10132 reference_to_unused (tree * tp, int * walk_subtrees,
10133                      void * data ATTRIBUTE_UNUSED)
10134 {
10135   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10136     *walk_subtrees = 0;
10137
10138   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10139       && ! TREE_ASM_WRITTEN (*tp))
10140     return *tp;
10141   else if (!flag_unit_at_a_time)
10142     return NULL_TREE;
10143   else if (!cgraph_global_info_ready
10144            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10145     gcc_unreachable ();
10146   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10147     {
10148       struct varpool_node *node = varpool_node (*tp);
10149       if (!node->needed)
10150         return *tp;
10151     }
10152   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10153            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10154     {
10155       struct cgraph_node *node = cgraph_node (*tp);
10156       if (!node->output)
10157         return *tp;
10158     }
10159
10160   return NULL_TREE;
10161 }
10162
10163 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10164    for use in a later add_const_value_attribute call.  */
10165
10166 static rtx
10167 rtl_for_decl_init (tree init, tree type)
10168 {
10169   rtx rtl = NULL_RTX;
10170
10171   /* If a variable is initialized with a string constant without embedded
10172      zeros, build CONST_STRING.  */
10173   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10174     {
10175       tree enttype = TREE_TYPE (type);
10176       tree domain = TYPE_DOMAIN (type);
10177       enum machine_mode mode = TYPE_MODE (enttype);
10178
10179       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10180           && domain
10181           && integer_zerop (TYPE_MIN_VALUE (domain))
10182           && compare_tree_int (TYPE_MAX_VALUE (domain),
10183                                TREE_STRING_LENGTH (init) - 1) == 0
10184           && ((size_t) TREE_STRING_LENGTH (init)
10185               == strlen (TREE_STRING_POINTER (init)) + 1))
10186         rtl = gen_rtx_CONST_STRING (VOIDmode,
10187                                     ggc_strdup (TREE_STRING_POINTER (init)));
10188     }
10189   /* Other aggregates, and complex values, could be represented using
10190      CONCAT: FIXME!  */
10191   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10192     ;
10193   /* Vectors only work if their mode is supported by the target.
10194      FIXME: generic vectors ought to work too.  */
10195   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10196     ;
10197   /* If the initializer is something that we know will expand into an
10198      immediate RTL constant, expand it now.  We must be careful not to
10199      reference variables which won't be output.  */
10200   else if (initializer_constant_valid_p (init, type)
10201            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10202     {
10203       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10204
10205       /* If expand_expr returns a MEM, it wasn't immediate.  */
10206       gcc_assert (!rtl || !MEM_P (rtl));
10207     }
10208
10209   return rtl;
10210 }
10211
10212 /* Generate RTL for the variable DECL to represent its location.  */
10213
10214 static rtx
10215 rtl_for_decl_location (tree decl)
10216 {
10217   rtx rtl;
10218
10219   /* Here we have to decide where we are going to say the parameter "lives"
10220      (as far as the debugger is concerned).  We only have a couple of
10221      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10222
10223      DECL_RTL normally indicates where the parameter lives during most of the
10224      activation of the function.  If optimization is enabled however, this
10225      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10226      that the parameter doesn't really live anywhere (as far as the code
10227      generation parts of GCC are concerned) during most of the function's
10228      activation.  That will happen (for example) if the parameter is never
10229      referenced within the function.
10230
10231      We could just generate a location descriptor here for all non-NULL
10232      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10233      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10234      where DECL_RTL is NULL or is a pseudo-reg.
10235
10236      Note however that we can only get away with using DECL_INCOMING_RTL as
10237      a backup substitute for DECL_RTL in certain limited cases.  In cases
10238      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10239      we can be sure that the parameter was passed using the same type as it is
10240      declared to have within the function, and that its DECL_INCOMING_RTL
10241      points us to a place where a value of that type is passed.
10242
10243      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10244      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10245      because in these cases DECL_INCOMING_RTL points us to a value of some
10246      type which is *different* from the type of the parameter itself.  Thus,
10247      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10248      such cases, the debugger would end up (for example) trying to fetch a
10249      `float' from a place which actually contains the first part of a
10250      `double'.  That would lead to really incorrect and confusing
10251      output at debug-time.
10252
10253      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10254      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10255      are a couple of exceptions however.  On little-endian machines we can
10256      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10257      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10258      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10259      when (on a little-endian machine) a non-prototyped function has a
10260      parameter declared to be of type `short' or `char'.  In such cases,
10261      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10262      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10263      passed `int' value.  If the debugger then uses that address to fetch
10264      a `short' or a `char' (on a little-endian machine) the result will be
10265      the correct data, so we allow for such exceptional cases below.
10266
10267      Note that our goal here is to describe the place where the given formal
10268      parameter lives during most of the function's activation (i.e. between the
10269      end of the prologue and the start of the epilogue).  We'll do that as best
10270      as we can. Note however that if the given formal parameter is modified
10271      sometime during the execution of the function, then a stack backtrace (at
10272      debug-time) will show the function as having been called with the *new*
10273      value rather than the value which was originally passed in.  This happens
10274      rarely enough that it is not a major problem, but it *is* a problem, and
10275      I'd like to fix it.
10276
10277      A future version of dwarf2out.c may generate two additional attributes for
10278      any given DW_TAG_formal_parameter DIE which will describe the "passed
10279      type" and the "passed location" for the given formal parameter in addition
10280      to the attributes we now generate to indicate the "declared type" and the
10281      "active location" for each parameter.  This additional set of attributes
10282      could be used by debuggers for stack backtraces. Separately, note that
10283      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10284      This happens (for example) for inlined-instances of inline function formal
10285      parameters which are never referenced.  This really shouldn't be
10286      happening.  All PARM_DECL nodes should get valid non-NULL
10287      DECL_INCOMING_RTL values.  FIXME.  */
10288
10289   /* Use DECL_RTL as the "location" unless we find something better.  */
10290   rtl = DECL_RTL_IF_SET (decl);
10291
10292   /* When generating abstract instances, ignore everything except
10293      constants, symbols living in memory, and symbols living in
10294      fixed registers.  */
10295   if (! reload_completed)
10296     {
10297       if (rtl
10298           && (CONSTANT_P (rtl)
10299               || (MEM_P (rtl)
10300                   && CONSTANT_P (XEXP (rtl, 0)))
10301               || (REG_P (rtl)
10302                   && TREE_CODE (decl) == VAR_DECL
10303                   && TREE_STATIC (decl))))
10304         {
10305           rtl = targetm.delegitimize_address (rtl);
10306           return rtl;
10307         }
10308       rtl = NULL_RTX;
10309     }
10310   else if (TREE_CODE (decl) == PARM_DECL)
10311     {
10312       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10313         {
10314           tree declared_type = TREE_TYPE (decl);
10315           tree passed_type = DECL_ARG_TYPE (decl);
10316           enum machine_mode dmode = TYPE_MODE (declared_type);
10317           enum machine_mode pmode = TYPE_MODE (passed_type);
10318
10319           /* This decl represents a formal parameter which was optimized out.
10320              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10321              all cases where (rtl == NULL_RTX) just below.  */
10322           if (dmode == pmode)
10323             rtl = DECL_INCOMING_RTL (decl);
10324           else if (SCALAR_INT_MODE_P (dmode)
10325                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10326                    && DECL_INCOMING_RTL (decl))
10327             {
10328               rtx inc = DECL_INCOMING_RTL (decl);
10329               if (REG_P (inc))
10330                 rtl = inc;
10331               else if (MEM_P (inc))
10332                 {
10333                   if (BYTES_BIG_ENDIAN)
10334                     rtl = adjust_address_nv (inc, dmode,
10335                                              GET_MODE_SIZE (pmode)
10336                                              - GET_MODE_SIZE (dmode));
10337                   else
10338                     rtl = inc;
10339                 }
10340             }
10341         }
10342
10343       /* If the parm was passed in registers, but lives on the stack, then
10344          make a big endian correction if the mode of the type of the
10345          parameter is not the same as the mode of the rtl.  */
10346       /* ??? This is the same series of checks that are made in dbxout.c before
10347          we reach the big endian correction code there.  It isn't clear if all
10348          of these checks are necessary here, but keeping them all is the safe
10349          thing to do.  */
10350       else if (MEM_P (rtl)
10351                && XEXP (rtl, 0) != const0_rtx
10352                && ! CONSTANT_P (XEXP (rtl, 0))
10353                /* Not passed in memory.  */
10354                && !MEM_P (DECL_INCOMING_RTL (decl))
10355                /* Not passed by invisible reference.  */
10356                && (!REG_P (XEXP (rtl, 0))
10357                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10358                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10359 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10360                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10361 #endif
10362                      )
10363                /* Big endian correction check.  */
10364                && BYTES_BIG_ENDIAN
10365                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10366                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10367                    < UNITS_PER_WORD))
10368         {
10369           int offset = (UNITS_PER_WORD
10370                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10371
10372           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10373                              plus_constant (XEXP (rtl, 0), offset));
10374         }
10375     }
10376   else if (TREE_CODE (decl) == VAR_DECL
10377            && rtl
10378            && MEM_P (rtl)
10379            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10380            && BYTES_BIG_ENDIAN)
10381     {
10382       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10383       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10384
10385       /* If a variable is declared "register" yet is smaller than
10386          a register, then if we store the variable to memory, it
10387          looks like we're storing a register-sized value, when in
10388          fact we are not.  We need to adjust the offset of the
10389          storage location to reflect the actual value's bytes,
10390          else gdb will not be able to display it.  */
10391       if (rsize > dsize)
10392         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10393                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10394     }
10395
10396   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10397      and will have been substituted directly into all expressions that use it.
10398      C does not have such a concept, but C++ and other languages do.  */
10399   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10400     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10401
10402   if (rtl)
10403     rtl = targetm.delegitimize_address (rtl);
10404
10405   /* If we don't look past the constant pool, we risk emitting a
10406      reference to a constant pool entry that isn't referenced from
10407      code, and thus is not emitted.  */
10408   if (rtl)
10409     rtl = avoid_constant_pool_reference (rtl);
10410
10411   return rtl;
10412 }
10413
10414 /* We need to figure out what section we should use as the base for the
10415    address ranges where a given location is valid.
10416    1. If this particular DECL has a section associated with it, use that.
10417    2. If this function has a section associated with it, use that.
10418    3. Otherwise, use the text section.
10419    XXX: If you split a variable across multiple sections, we won't notice.  */
10420
10421 static const char *
10422 secname_for_decl (tree decl)
10423 {
10424   const char *secname;
10425
10426   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10427     {
10428       tree sectree = DECL_SECTION_NAME (decl);
10429       secname = TREE_STRING_POINTER (sectree);
10430     }
10431   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10432     {
10433       tree sectree = DECL_SECTION_NAME (current_function_decl);
10434       secname = TREE_STRING_POINTER (sectree);
10435     }
10436   else if (cfun && in_cold_section_p)
10437     secname = cfun->cold_section_label;
10438   else
10439     secname = text_section_label;
10440
10441   return secname;
10442 }
10443
10444 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10445    data attribute for a variable or a parameter.  We generate the
10446    DW_AT_const_value attribute only in those cases where the given variable
10447    or parameter does not have a true "location" either in memory or in a
10448    register.  This can happen (for example) when a constant is passed as an
10449    actual argument in a call to an inline function.  (It's possible that
10450    these things can crop up in other ways also.)  Note that one type of
10451    constant value which can be passed into an inlined function is a constant
10452    pointer.  This can happen for example if an actual argument in an inlined
10453    function call evaluates to a compile-time constant address.  */
10454
10455 static void
10456 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10457                                        enum dwarf_attribute attr)
10458 {
10459   rtx rtl;
10460   dw_loc_descr_ref descr;
10461   var_loc_list *loc_list;
10462   struct var_loc_node *node;
10463   if (TREE_CODE (decl) == ERROR_MARK)
10464     return;
10465
10466   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10467               || TREE_CODE (decl) == RESULT_DECL);
10468
10469   /* See if we possibly have multiple locations for this variable.  */
10470   loc_list = lookup_decl_loc (decl);
10471
10472   /* If it truly has multiple locations, the first and last node will
10473      differ.  */
10474   if (loc_list && loc_list->first != loc_list->last)
10475     {
10476       const char *endname, *secname;
10477       dw_loc_list_ref list;
10478       rtx varloc;
10479
10480       /* Now that we know what section we are using for a base,
10481          actually construct the list of locations.
10482          The first location information is what is passed to the
10483          function that creates the location list, and the remaining
10484          locations just get added on to that list.
10485          Note that we only know the start address for a location
10486          (IE location changes), so to build the range, we use
10487          the range [current location start, next location start].
10488          This means we have to special case the last node, and generate
10489          a range of [last location start, end of function label].  */
10490
10491       node = loc_list->first;
10492       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10493       secname = secname_for_decl (decl);
10494
10495       list = new_loc_list (loc_descriptor (varloc),
10496                            node->label, node->next->label, secname, 1);
10497       node = node->next;
10498
10499       for (; node->next; node = node->next)
10500         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10501           {
10502             /* The variable has a location between NODE->LABEL and
10503                NODE->NEXT->LABEL.  */
10504             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10505             add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10506                                        node->label, node->next->label, secname);
10507           }
10508
10509       /* If the variable has a location at the last label
10510          it keeps its location until the end of function.  */
10511       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10512         {
10513           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10514
10515           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10516           if (!current_function_decl)
10517             endname = text_end_label;
10518           else
10519             {
10520               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10521                                            current_function_funcdef_no);
10522               endname = ggc_strdup (label_id);
10523             }
10524           add_loc_descr_to_loc_list (&list, loc_descriptor (varloc),
10525                                      node->label, endname, secname);
10526         }
10527
10528       /* Finally, add the location list to the DIE, and we are done.  */
10529       add_AT_loc_list (die, attr, list);
10530       return;
10531     }
10532
10533   /* Try to get some constant RTL for this decl, and use that as the value of
10534      the location.  */
10535
10536   rtl = rtl_for_decl_location (decl);
10537   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10538     {
10539       add_const_value_attribute (die, rtl);
10540       return;
10541     }
10542
10543   /* If we have tried to generate the location otherwise, and it
10544      didn't work out (we wouldn't be here if we did), and we have a one entry
10545      location list, try generating a location from that.  */
10546   if (loc_list && loc_list->first)
10547     {
10548       node = loc_list->first;
10549       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note));
10550       if (descr)
10551         {
10552           add_AT_location_description (die, attr, descr);
10553           return;
10554         }
10555     }
10556
10557   /* We couldn't get any rtl, so try directly generating the location
10558      description from the tree.  */
10559   descr = loc_descriptor_from_tree (decl);
10560   if (descr)
10561     {
10562       add_AT_location_description (die, attr, descr);
10563       return;
10564     }
10565   /* None of that worked, so it must not really have a location;
10566      try adding a constant value attribute from the DECL_INITIAL.  */
10567   tree_add_const_value_attribute (die, decl);
10568 }
10569
10570 /* If we don't have a copy of this variable in memory for some reason (such
10571    as a C++ member constant that doesn't have an out-of-line definition),
10572    we should tell the debugger about the constant value.  */
10573
10574 static void
10575 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10576 {
10577   tree init = DECL_INITIAL (decl);
10578   tree type = TREE_TYPE (decl);
10579   rtx rtl;
10580
10581   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10582     /* OK */;
10583   else
10584     return;
10585
10586   rtl = rtl_for_decl_init (init, type);
10587   if (rtl)
10588     add_const_value_attribute (var_die, rtl);
10589 }
10590
10591 /* Convert the CFI instructions for the current function into a
10592    location list.  This is used for DW_AT_frame_base when we targeting
10593    a dwarf2 consumer that does not support the dwarf3
10594    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10595    expressions.  */
10596
10597 static dw_loc_list_ref
10598 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10599 {
10600   dw_fde_ref fde;
10601   dw_loc_list_ref list, *list_tail;
10602   dw_cfi_ref cfi;
10603   dw_cfa_location last_cfa, next_cfa;
10604   const char *start_label, *last_label, *section;
10605
10606   fde = &fde_table[fde_table_in_use - 1];
10607
10608   section = secname_for_decl (current_function_decl);
10609   list_tail = &list;
10610   list = NULL;
10611
10612   next_cfa.reg = INVALID_REGNUM;
10613   next_cfa.offset = 0;
10614   next_cfa.indirect = 0;
10615   next_cfa.base_offset = 0;
10616
10617   start_label = fde->dw_fde_begin;
10618
10619   /* ??? Bald assumption that the CIE opcode list does not contain
10620      advance opcodes.  */
10621   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10622     lookup_cfa_1 (cfi, &next_cfa);
10623
10624   last_cfa = next_cfa;
10625   last_label = start_label;
10626
10627   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10628     switch (cfi->dw_cfi_opc)
10629       {
10630       case DW_CFA_set_loc:
10631       case DW_CFA_advance_loc1:
10632       case DW_CFA_advance_loc2:
10633       case DW_CFA_advance_loc4:
10634         if (!cfa_equal_p (&last_cfa, &next_cfa))
10635           {
10636             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10637                                        start_label, last_label, section,
10638                                        list == NULL);
10639
10640             list_tail = &(*list_tail)->dw_loc_next;
10641             last_cfa = next_cfa;
10642             start_label = last_label;
10643           }
10644         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10645         break;
10646
10647       case DW_CFA_advance_loc:
10648         /* The encoding is complex enough that we should never emit this.  */
10649       case DW_CFA_remember_state:
10650       case DW_CFA_restore_state:
10651         /* We don't handle these two in this function.  It would be possible
10652            if it were to be required.  */
10653         gcc_unreachable ();
10654
10655       default:
10656         lookup_cfa_1 (cfi, &next_cfa);
10657         break;
10658       }
10659
10660   if (!cfa_equal_p (&last_cfa, &next_cfa))
10661     {
10662       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10663                                  start_label, last_label, section,
10664                                  list == NULL);
10665       list_tail = &(*list_tail)->dw_loc_next;
10666       start_label = last_label;
10667     }
10668   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10669                              start_label, fde->dw_fde_end, section,
10670                              list == NULL);
10671
10672   return list;
10673 }
10674
10675 /* Compute a displacement from the "steady-state frame pointer" to the
10676    frame base (often the same as the CFA), and store it in
10677    frame_pointer_fb_offset.  OFFSET is added to the displacement
10678    before the latter is negated.  */
10679
10680 static void
10681 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10682 {
10683   rtx reg, elim;
10684
10685 #ifdef FRAME_POINTER_CFA_OFFSET
10686   reg = frame_pointer_rtx;
10687   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10688 #else
10689   reg = arg_pointer_rtx;
10690   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10691 #endif
10692
10693   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10694   if (GET_CODE (elim) == PLUS)
10695     {
10696       offset += INTVAL (XEXP (elim, 1));
10697       elim = XEXP (elim, 0);
10698     }
10699   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10700                        : stack_pointer_rtx));
10701
10702   frame_pointer_fb_offset = -offset;
10703 }
10704
10705 /* Generate a DW_AT_name attribute given some string value to be included as
10706    the value of the attribute.  */
10707
10708 static void
10709 add_name_attribute (dw_die_ref die, const char *name_string)
10710 {
10711   if (name_string != NULL && *name_string != 0)
10712     {
10713       if (demangle_name_func)
10714         name_string = (*demangle_name_func) (name_string);
10715
10716       add_AT_string (die, DW_AT_name, name_string);
10717     }
10718 }
10719
10720 /* Generate a DW_AT_comp_dir attribute for DIE.  */
10721
10722 static void
10723 add_comp_dir_attribute (dw_die_ref die)
10724 {
10725   const char *wd = get_src_pwd ();
10726   if (wd != NULL)
10727     add_AT_string (die, DW_AT_comp_dir, wd);
10728 }
10729
10730 /* Given a tree node describing an array bound (either lower or upper) output
10731    a representation for that bound.  */
10732
10733 static void
10734 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
10735 {
10736   switch (TREE_CODE (bound))
10737     {
10738     case ERROR_MARK:
10739       return;
10740
10741     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
10742     case INTEGER_CST:
10743       if (! host_integerp (bound, 0)
10744           || (bound_attr == DW_AT_lower_bound
10745               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
10746                   || (is_fortran () && integer_onep (bound)))))
10747         /* Use the default.  */
10748         ;
10749       else
10750         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
10751       break;
10752
10753     case CONVERT_EXPR:
10754     case NOP_EXPR:
10755     case NON_LVALUE_EXPR:
10756     case VIEW_CONVERT_EXPR:
10757       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
10758       break;
10759
10760     case SAVE_EXPR:
10761       break;
10762
10763     case VAR_DECL:
10764     case PARM_DECL:
10765     case RESULT_DECL:
10766       {
10767         dw_die_ref decl_die = lookup_decl_die (bound);
10768
10769         /* ??? Can this happen, or should the variable have been bound
10770            first?  Probably it can, since I imagine that we try to create
10771            the types of parameters in the order in which they exist in
10772            the list, and won't have created a forward reference to a
10773            later parameter.  */
10774         if (decl_die != NULL)
10775           add_AT_die_ref (subrange_die, bound_attr, decl_die);
10776         break;
10777       }
10778
10779     default:
10780       {
10781         /* Otherwise try to create a stack operation procedure to
10782            evaluate the value of the array bound.  */
10783
10784         dw_die_ref ctx, decl_die;
10785         dw_loc_descr_ref loc;
10786
10787         loc = loc_descriptor_from_tree (bound);
10788         if (loc == NULL)
10789           break;
10790
10791         if (current_function_decl == 0)
10792           ctx = comp_unit_die;
10793         else
10794           ctx = lookup_decl_die (current_function_decl);
10795
10796         decl_die = new_die (DW_TAG_variable, ctx, bound);
10797         add_AT_flag (decl_die, DW_AT_artificial, 1);
10798         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
10799         add_AT_loc (decl_die, DW_AT_location, loc);
10800
10801         add_AT_die_ref (subrange_die, bound_attr, decl_die);
10802         break;
10803       }
10804     }
10805 }
10806
10807 /* Note that the block of subscript information for an array type also
10808    includes information about the element type of type given array type.  */
10809
10810 static void
10811 add_subscript_info (dw_die_ref type_die, tree type)
10812 {
10813 #ifndef MIPS_DEBUGGING_INFO
10814   unsigned dimension_number;
10815 #endif
10816   tree lower, upper;
10817   dw_die_ref subrange_die;
10818
10819   /* The GNU compilers represent multidimensional array types as sequences of
10820      one dimensional array types whose element types are themselves array
10821      types.  Here we squish that down, so that each multidimensional array
10822      type gets only one array_type DIE in the Dwarf debugging info. The draft
10823      Dwarf specification say that we are allowed to do this kind of
10824      compression in C (because there is no difference between an array or
10825      arrays and a multidimensional array in C) but for other source languages
10826      (e.g. Ada) we probably shouldn't do this.  */
10827
10828   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
10829      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
10830      We work around this by disabling this feature.  See also
10831      gen_array_type_die.  */
10832 #ifndef MIPS_DEBUGGING_INFO
10833   for (dimension_number = 0;
10834        TREE_CODE (type) == ARRAY_TYPE;
10835        type = TREE_TYPE (type), dimension_number++)
10836 #endif
10837     {
10838       tree domain = TYPE_DOMAIN (type);
10839
10840       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
10841          and (in GNU C only) variable bounds.  Handle all three forms
10842          here.  */
10843       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
10844       if (domain)
10845         {
10846           /* We have an array type with specified bounds.  */
10847           lower = TYPE_MIN_VALUE (domain);
10848           upper = TYPE_MAX_VALUE (domain);
10849
10850           /* Define the index type.  */
10851           if (TREE_TYPE (domain))
10852             {
10853               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
10854                  TREE_TYPE field.  We can't emit debug info for this
10855                  because it is an unnamed integral type.  */
10856               if (TREE_CODE (domain) == INTEGER_TYPE
10857                   && TYPE_NAME (domain) == NULL_TREE
10858                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
10859                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
10860                 ;
10861               else
10862                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
10863                                     type_die);
10864             }
10865
10866           /* ??? If upper is NULL, the array has unspecified length,
10867              but it does have a lower bound.  This happens with Fortran
10868                dimension arr(N:*)
10869              Since the debugger is definitely going to need to know N
10870              to produce useful results, go ahead and output the lower
10871              bound solo, and hope the debugger can cope.  */
10872
10873           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
10874           if (upper)
10875             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
10876         }
10877
10878       /* Otherwise we have an array type with an unspecified length.  The
10879          DWARF-2 spec does not say how to handle this; let's just leave out the
10880          bounds.  */
10881     }
10882 }
10883
10884 static void
10885 add_byte_size_attribute (dw_die_ref die, tree tree_node)
10886 {
10887   unsigned size;
10888
10889   switch (TREE_CODE (tree_node))
10890     {
10891     case ERROR_MARK:
10892       size = 0;
10893       break;
10894     case ENUMERAL_TYPE:
10895     case RECORD_TYPE:
10896     case UNION_TYPE:
10897     case QUAL_UNION_TYPE:
10898       size = int_size_in_bytes (tree_node);
10899       break;
10900     case FIELD_DECL:
10901       /* For a data member of a struct or union, the DW_AT_byte_size is
10902          generally given as the number of bytes normally allocated for an
10903          object of the *declared* type of the member itself.  This is true
10904          even for bit-fields.  */
10905       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
10906       break;
10907     default:
10908       gcc_unreachable ();
10909     }
10910
10911   /* Note that `size' might be -1 when we get to this point.  If it is, that
10912      indicates that the byte size of the entity in question is variable.  We
10913      have no good way of expressing this fact in Dwarf at the present time,
10914      so just let the -1 pass on through.  */
10915   add_AT_unsigned (die, DW_AT_byte_size, size);
10916 }
10917
10918 /* For a FIELD_DECL node which represents a bit-field, output an attribute
10919    which specifies the distance in bits from the highest order bit of the
10920    "containing object" for the bit-field to the highest order bit of the
10921    bit-field itself.
10922
10923    For any given bit-field, the "containing object" is a hypothetical object
10924    (of some integral or enum type) within which the given bit-field lives.  The
10925    type of this hypothetical "containing object" is always the same as the
10926    declared type of the individual bit-field itself.  The determination of the
10927    exact location of the "containing object" for a bit-field is rather
10928    complicated.  It's handled by the `field_byte_offset' function (above).
10929
10930    Note that it is the size (in bytes) of the hypothetical "containing object"
10931    which will be given in the DW_AT_byte_size attribute for this bit-field.
10932    (See `byte_size_attribute' above).  */
10933
10934 static inline void
10935 add_bit_offset_attribute (dw_die_ref die, tree decl)
10936 {
10937   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
10938   tree type = DECL_BIT_FIELD_TYPE (decl);
10939   HOST_WIDE_INT bitpos_int;
10940   HOST_WIDE_INT highest_order_object_bit_offset;
10941   HOST_WIDE_INT highest_order_field_bit_offset;
10942   HOST_WIDE_INT unsigned bit_offset;
10943
10944   /* Must be a field and a bit field.  */
10945   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
10946
10947   /* We can't yet handle bit-fields whose offsets are variable, so if we
10948      encounter such things, just return without generating any attribute
10949      whatsoever.  Likewise for variable or too large size.  */
10950   if (! host_integerp (bit_position (decl), 0)
10951       || ! host_integerp (DECL_SIZE (decl), 1))
10952     return;
10953
10954   bitpos_int = int_bit_position (decl);
10955
10956   /* Note that the bit offset is always the distance (in bits) from the
10957      highest-order bit of the "containing object" to the highest-order bit of
10958      the bit-field itself.  Since the "high-order end" of any object or field
10959      is different on big-endian and little-endian machines, the computation
10960      below must take account of these differences.  */
10961   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
10962   highest_order_field_bit_offset = bitpos_int;
10963
10964   if (! BYTES_BIG_ENDIAN)
10965     {
10966       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
10967       highest_order_object_bit_offset += simple_type_size_in_bits (type);
10968     }
10969
10970   bit_offset
10971     = (! BYTES_BIG_ENDIAN
10972        ? highest_order_object_bit_offset - highest_order_field_bit_offset
10973        : highest_order_field_bit_offset - highest_order_object_bit_offset);
10974
10975   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
10976 }
10977
10978 /* For a FIELD_DECL node which represents a bit field, output an attribute
10979    which specifies the length in bits of the given field.  */
10980
10981 static inline void
10982 add_bit_size_attribute (dw_die_ref die, tree decl)
10983 {
10984   /* Must be a field and a bit field.  */
10985   gcc_assert (TREE_CODE (decl) == FIELD_DECL
10986               && DECL_BIT_FIELD_TYPE (decl));
10987
10988   if (host_integerp (DECL_SIZE (decl), 1))
10989     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
10990 }
10991
10992 /* If the compiled language is ANSI C, then add a 'prototyped'
10993    attribute, if arg types are given for the parameters of a function.  */
10994
10995 static inline void
10996 add_prototyped_attribute (dw_die_ref die, tree func_type)
10997 {
10998   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
10999       && TYPE_ARG_TYPES (func_type) != NULL)
11000     add_AT_flag (die, DW_AT_prototyped, 1);
11001 }
11002
11003 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11004    by looking in either the type declaration or object declaration
11005    equate table.  */
11006
11007 static inline void
11008 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11009 {
11010   dw_die_ref origin_die = NULL;
11011
11012   if (TREE_CODE (origin) != FUNCTION_DECL)
11013     {
11014       /* We may have gotten separated from the block for the inlined
11015          function, if we're in an exception handler or some such; make
11016          sure that the abstract function has been written out.
11017
11018          Doing this for nested functions is wrong, however; functions are
11019          distinct units, and our context might not even be inline.  */
11020       tree fn = origin;
11021
11022       if (TYPE_P (fn))
11023         fn = TYPE_STUB_DECL (fn);
11024
11025       fn = decl_function_context (fn);
11026       if (fn)
11027         dwarf2out_abstract_function (fn);
11028     }
11029
11030   if (DECL_P (origin))
11031     origin_die = lookup_decl_die (origin);
11032   else if (TYPE_P (origin))
11033     origin_die = lookup_type_die (origin);
11034
11035   /* XXX: Functions that are never lowered don't always have correct block
11036      trees (in the case of java, they simply have no block tree, in some other
11037      languages).  For these functions, there is nothing we can really do to
11038      output correct debug info for inlined functions in all cases.  Rather
11039      than die, we'll just produce deficient debug info now, in that we will
11040      have variables without a proper abstract origin.  In the future, when all
11041      functions are lowered, we should re-add a gcc_assert (origin_die)
11042      here.  */
11043
11044   if (origin_die)
11045       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11046 }
11047
11048 /* We do not currently support the pure_virtual attribute.  */
11049
11050 static inline void
11051 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11052 {
11053   if (DECL_VINDEX (func_decl))
11054     {
11055       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11056
11057       if (host_integerp (DECL_VINDEX (func_decl), 0))
11058         add_AT_loc (die, DW_AT_vtable_elem_location,
11059                     new_loc_descr (DW_OP_constu,
11060                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11061                                    0));
11062
11063       /* GNU extension: Record what type this method came from originally.  */
11064       if (debug_info_level > DINFO_LEVEL_TERSE)
11065         add_AT_die_ref (die, DW_AT_containing_type,
11066                         lookup_type_die (DECL_CONTEXT (func_decl)));
11067     }
11068 }
11069 \f
11070 /* Add source coordinate attributes for the given decl.  */
11071
11072 static void
11073 add_src_coords_attributes (dw_die_ref die, tree decl)
11074 {
11075   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11076
11077   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11078   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11079 }
11080
11081 /* Add a DW_AT_name attribute and source coordinate attribute for the
11082    given decl, but only if it actually has a name.  */
11083
11084 static void
11085 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11086 {
11087   tree decl_name;
11088
11089   decl_name = DECL_NAME (decl);
11090   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11091     {
11092       add_name_attribute (die, dwarf2_name (decl, 0));
11093       if (! DECL_ARTIFICIAL (decl))
11094         add_src_coords_attributes (die, decl);
11095
11096       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11097           && TREE_PUBLIC (decl)
11098           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11099           && !DECL_ABSTRACT (decl)
11100           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl)))
11101         add_AT_string (die, DW_AT_MIPS_linkage_name,
11102                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11103     }
11104
11105 #ifdef VMS_DEBUGGING_INFO
11106   /* Get the function's name, as described by its RTL.  This may be different
11107      from the DECL_NAME name used in the source file.  */
11108   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11109     {
11110       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11111                    XEXP (DECL_RTL (decl), 0));
11112       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11113     }
11114 #endif
11115 }
11116
11117 /* Push a new declaration scope.  */
11118
11119 static void
11120 push_decl_scope (tree scope)
11121 {
11122   VEC_safe_push (tree, gc, decl_scope_table, scope);
11123 }
11124
11125 /* Pop a declaration scope.  */
11126
11127 static inline void
11128 pop_decl_scope (void)
11129 {
11130   VEC_pop (tree, decl_scope_table);
11131 }
11132
11133 /* Return the DIE for the scope that immediately contains this type.
11134    Non-named types get global scope.  Named types nested in other
11135    types get their containing scope if it's open, or global scope
11136    otherwise.  All other types (i.e. function-local named types) get
11137    the current active scope.  */
11138
11139 static dw_die_ref
11140 scope_die_for (tree t, dw_die_ref context_die)
11141 {
11142   dw_die_ref scope_die = NULL;
11143   tree containing_scope;
11144   int i;
11145
11146   /* Non-types always go in the current scope.  */
11147   gcc_assert (TYPE_P (t));
11148
11149   containing_scope = TYPE_CONTEXT (t);
11150
11151   /* Use the containing namespace if it was passed in (for a declaration).  */
11152   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11153     {
11154       if (context_die == lookup_decl_die (containing_scope))
11155         /* OK */;
11156       else
11157         containing_scope = NULL_TREE;
11158     }
11159
11160   /* Ignore function type "scopes" from the C frontend.  They mean that
11161      a tagged type is local to a parmlist of a function declarator, but
11162      that isn't useful to DWARF.  */
11163   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11164     containing_scope = NULL_TREE;
11165
11166   if (containing_scope == NULL_TREE)
11167     scope_die = comp_unit_die;
11168   else if (TYPE_P (containing_scope))
11169     {
11170       /* For types, we can just look up the appropriate DIE.  But
11171          first we check to see if we're in the middle of emitting it
11172          so we know where the new DIE should go.  */
11173       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11174         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11175           break;
11176
11177       if (i < 0)
11178         {
11179           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11180                       || TREE_ASM_WRITTEN (containing_scope));
11181
11182           /* If none of the current dies are suitable, we get file scope.  */
11183           scope_die = comp_unit_die;
11184         }
11185       else
11186         scope_die = lookup_type_die (containing_scope);
11187     }
11188   else
11189     scope_die = context_die;
11190
11191   return scope_die;
11192 }
11193
11194 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11195
11196 static inline int
11197 local_scope_p (dw_die_ref context_die)
11198 {
11199   for (; context_die; context_die = context_die->die_parent)
11200     if (context_die->die_tag == DW_TAG_inlined_subroutine
11201         || context_die->die_tag == DW_TAG_subprogram)
11202       return 1;
11203
11204   return 0;
11205 }
11206
11207 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11208    whether or not to treat a DIE in this context as a declaration.  */
11209
11210 static inline int
11211 class_or_namespace_scope_p (dw_die_ref context_die)
11212 {
11213   return (context_die
11214           && (context_die->die_tag == DW_TAG_structure_type
11215               || context_die->die_tag == DW_TAG_union_type
11216               || context_die->die_tag == DW_TAG_namespace));
11217 }
11218
11219 /* Many forms of DIEs require a "type description" attribute.  This
11220    routine locates the proper "type descriptor" die for the type given
11221    by 'type', and adds a DW_AT_type attribute below the given die.  */
11222
11223 static void
11224 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11225                     int decl_volatile, dw_die_ref context_die)
11226 {
11227   enum tree_code code  = TREE_CODE (type);
11228   dw_die_ref type_die  = NULL;
11229
11230   /* ??? If this type is an unnamed subrange type of an integral or
11231      floating-point type, use the inner type.  This is because we have no
11232      support for unnamed types in base_type_die.  This can happen if this is
11233      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11234   if ((code == INTEGER_TYPE || code == REAL_TYPE)
11235       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11236     type = TREE_TYPE (type), code = TREE_CODE (type);
11237
11238   if (code == ERROR_MARK
11239       /* Handle a special case.  For functions whose return type is void, we
11240          generate *no* type attribute.  (Note that no object may have type
11241          `void', so this only applies to function return types).  */
11242       || code == VOID_TYPE)
11243     return;
11244
11245   type_die = modified_type_die (type,
11246                                 decl_const || TYPE_READONLY (type),
11247                                 decl_volatile || TYPE_VOLATILE (type),
11248                                 context_die);
11249
11250   if (type_die != NULL)
11251     add_AT_die_ref (object_die, DW_AT_type, type_die);
11252 }
11253
11254 /* Given an object die, add the calling convention attribute for the
11255    function call type.  */
11256 static void
11257 add_calling_convention_attribute (dw_die_ref subr_die, tree type)
11258 {
11259   enum dwarf_calling_convention value = DW_CC_normal;
11260
11261   value = targetm.dwarf_calling_convention (type);
11262
11263   /* Only add the attribute if the backend requests it, and
11264      is not DW_CC_normal.  */
11265   if (value && (value != DW_CC_normal))
11266     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11267 }
11268
11269 /* Given a tree pointer to a struct, class, union, or enum type node, return
11270    a pointer to the (string) tag name for the given type, or zero if the type
11271    was declared without a tag.  */
11272
11273 static const char *
11274 type_tag (tree type)
11275 {
11276   const char *name = 0;
11277
11278   if (TYPE_NAME (type) != 0)
11279     {
11280       tree t = 0;
11281
11282       /* Find the IDENTIFIER_NODE for the type name.  */
11283       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11284         t = TYPE_NAME (type);
11285
11286       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11287          a TYPE_DECL node, regardless of whether or not a `typedef' was
11288          involved.  */
11289       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11290                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11291         {
11292           /* We want to be extra verbose.  Don't call dwarf_name if
11293              DECL_NAME isn't set.  The default hook for decl_printable_name
11294              doesn't like that, and in this context it's correct to return
11295              0, instead of "<anonymous>" or the like.  */
11296           if (DECL_NAME (TYPE_NAME (type)))
11297             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11298         }
11299
11300       /* Now get the name as a string, or invent one.  */
11301       if (!name && t != 0)
11302         name = IDENTIFIER_POINTER (t);
11303     }
11304
11305   return (name == 0 || *name == '\0') ? 0 : name;
11306 }
11307
11308 /* Return the type associated with a data member, make a special check
11309    for bit field types.  */
11310
11311 static inline tree
11312 member_declared_type (tree member)
11313 {
11314   return (DECL_BIT_FIELD_TYPE (member)
11315           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11316 }
11317
11318 /* Get the decl's label, as described by its RTL. This may be different
11319    from the DECL_NAME name used in the source file.  */
11320
11321 #if 0
11322 static const char *
11323 decl_start_label (tree decl)
11324 {
11325   rtx x;
11326   const char *fnname;
11327
11328   x = DECL_RTL (decl);
11329   gcc_assert (MEM_P (x));
11330
11331   x = XEXP (x, 0);
11332   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11333
11334   fnname = XSTR (x, 0);
11335   return fnname;
11336 }
11337 #endif
11338 \f
11339 /* These routines generate the internal representation of the DIE's for
11340    the compilation unit.  Debugging information is collected by walking
11341    the declaration trees passed in from dwarf2out_decl().  */
11342
11343 static void
11344 gen_array_type_die (tree type, dw_die_ref context_die)
11345 {
11346   dw_die_ref scope_die = scope_die_for (type, context_die);
11347   dw_die_ref array_die;
11348   tree element_type;
11349
11350   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11351      the inner array type comes before the outer array type.  Thus we must
11352      call gen_type_die before we call new_die.  See below also.  */
11353 #ifdef MIPS_DEBUGGING_INFO
11354   gen_type_die (TREE_TYPE (type), context_die);
11355 #endif
11356
11357   array_die = new_die (DW_TAG_array_type, scope_die, type);
11358   add_name_attribute (array_die, type_tag (type));
11359   equate_type_number_to_die (type, array_die);
11360
11361   if (TREE_CODE (type) == VECTOR_TYPE)
11362     {
11363       /* The frontend feeds us a representation for the vector as a struct
11364          containing an array.  Pull out the array type.  */
11365       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11366       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11367     }
11368
11369 #if 0
11370   /* We default the array ordering.  SDB will probably do
11371      the right things even if DW_AT_ordering is not present.  It's not even
11372      an issue until we start to get into multidimensional arrays anyway.  If
11373      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11374      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11375      and when we find out that we need to put these in, we will only do so
11376      for multidimensional arrays.  */
11377   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11378 #endif
11379
11380 #ifdef MIPS_DEBUGGING_INFO
11381   /* The SGI compilers handle arrays of unknown bound by setting
11382      AT_declaration and not emitting any subrange DIEs.  */
11383   if (! TYPE_DOMAIN (type))
11384     add_AT_flag (array_die, DW_AT_declaration, 1);
11385   else
11386 #endif
11387     add_subscript_info (array_die, type);
11388
11389   /* Add representation of the type of the elements of this array type.  */
11390   element_type = TREE_TYPE (type);
11391
11392   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11393      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11394      We work around this by disabling this feature.  See also
11395      add_subscript_info.  */
11396 #ifndef MIPS_DEBUGGING_INFO
11397   while (TREE_CODE (element_type) == ARRAY_TYPE)
11398     element_type = TREE_TYPE (element_type);
11399
11400   gen_type_die (element_type, context_die);
11401 #endif
11402
11403   add_type_attribute (array_die, element_type, 0, 0, context_die);
11404
11405   if (get_AT (array_die, DW_AT_name))
11406     add_pubtype (type, array_die);
11407 }
11408
11409 #if 0
11410 static void
11411 gen_entry_point_die (tree decl, dw_die_ref context_die)
11412 {
11413   tree origin = decl_ultimate_origin (decl);
11414   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11415
11416   if (origin != NULL)
11417     add_abstract_origin_attribute (decl_die, origin);
11418   else
11419     {
11420       add_name_and_src_coords_attributes (decl_die, decl);
11421       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11422                           0, 0, context_die);
11423     }
11424
11425   if (DECL_ABSTRACT (decl))
11426     equate_decl_number_to_die (decl, decl_die);
11427   else
11428     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11429 }
11430 #endif
11431
11432 /* Walk through the list of incomplete types again, trying once more to
11433    emit full debugging info for them.  */
11434
11435 static void
11436 retry_incomplete_types (void)
11437 {
11438   int i;
11439
11440   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11441     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11442 }
11443
11444 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11445
11446 static void
11447 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11448 {
11449   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11450
11451   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11452      be incomplete and such types are not marked.  */
11453   add_abstract_origin_attribute (type_die, type);
11454 }
11455
11456 /* Generate a DIE to represent an inlined instance of a structure type.  */
11457
11458 static void
11459 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11460 {
11461   dw_die_ref type_die = new_die (DW_TAG_structure_type, context_die, type);
11462
11463   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11464      be incomplete and such types are not marked.  */
11465   add_abstract_origin_attribute (type_die, type);
11466 }
11467
11468 /* Generate a DIE to represent an inlined instance of a union type.  */
11469
11470 static void
11471 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11472 {
11473   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11474
11475   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11476      be incomplete and such types are not marked.  */
11477   add_abstract_origin_attribute (type_die, type);
11478 }
11479
11480 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11481    include all of the information about the enumeration values also. Each
11482    enumerated type name/value is listed as a child of the enumerated type
11483    DIE.  */
11484
11485 static dw_die_ref
11486 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11487 {
11488   dw_die_ref type_die = lookup_type_die (type);
11489
11490   if (type_die == NULL)
11491     {
11492       type_die = new_die (DW_TAG_enumeration_type,
11493                           scope_die_for (type, context_die), type);
11494       equate_type_number_to_die (type, type_die);
11495       add_name_attribute (type_die, type_tag (type));
11496     }
11497   else if (! TYPE_SIZE (type))
11498     return type_die;
11499   else
11500     remove_AT (type_die, DW_AT_declaration);
11501
11502   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11503      given enum type is incomplete, do not generate the DW_AT_byte_size
11504      attribute or the DW_AT_element_list attribute.  */
11505   if (TYPE_SIZE (type))
11506     {
11507       tree link;
11508
11509       TREE_ASM_WRITTEN (type) = 1;
11510       add_byte_size_attribute (type_die, type);
11511       if (TYPE_STUB_DECL (type) != NULL_TREE)
11512         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11513
11514       /* If the first reference to this type was as the return type of an
11515          inline function, then it may not have a parent.  Fix this now.  */
11516       if (type_die->die_parent == NULL)
11517         add_child_die (scope_die_for (type, context_die), type_die);
11518
11519       for (link = TYPE_VALUES (type);
11520            link != NULL; link = TREE_CHAIN (link))
11521         {
11522           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
11523           tree value = TREE_VALUE (link);
11524
11525           add_name_attribute (enum_die,
11526                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
11527
11528           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
11529             /* DWARF2 does not provide a way of indicating whether or
11530                not enumeration constants are signed or unsigned.  GDB
11531                always assumes the values are signed, so we output all
11532                values as if they were signed.  That means that
11533                enumeration constants with very large unsigned values
11534                will appear to have negative values in the debugger.  */
11535             add_AT_int (enum_die, DW_AT_const_value,
11536                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
11537         }
11538     }
11539   else
11540     add_AT_flag (type_die, DW_AT_declaration, 1);
11541
11542   if (get_AT (type_die, DW_AT_name))
11543     add_pubtype (type, type_die);
11544
11545   return type_die;
11546 }
11547
11548 /* Generate a DIE to represent either a real live formal parameter decl or to
11549    represent just the type of some formal parameter position in some function
11550    type.
11551
11552    Note that this routine is a bit unusual because its argument may be a
11553    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
11554    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
11555    node.  If it's the former then this function is being called to output a
11556    DIE to represent a formal parameter object (or some inlining thereof).  If
11557    it's the latter, then this function is only being called to output a
11558    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
11559    argument type of some subprogram type.  */
11560
11561 static dw_die_ref
11562 gen_formal_parameter_die (tree node, dw_die_ref context_die)
11563 {
11564   dw_die_ref parm_die
11565     = new_die (DW_TAG_formal_parameter, context_die, node);
11566   tree origin;
11567
11568   switch (TREE_CODE_CLASS (TREE_CODE (node)))
11569     {
11570     case tcc_declaration:
11571       origin = decl_ultimate_origin (node);
11572       if (origin != NULL)
11573         add_abstract_origin_attribute (parm_die, origin);
11574       else
11575         {
11576           add_name_and_src_coords_attributes (parm_die, node);
11577           add_type_attribute (parm_die, TREE_TYPE (node),
11578                               TREE_READONLY (node),
11579                               TREE_THIS_VOLATILE (node),
11580                               context_die);
11581           if (DECL_ARTIFICIAL (node))
11582             add_AT_flag (parm_die, DW_AT_artificial, 1);
11583         }
11584
11585       equate_decl_number_to_die (node, parm_die);
11586       if (! DECL_ABSTRACT (node))
11587         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
11588
11589       break;
11590
11591     case tcc_type:
11592       /* We were called with some kind of a ..._TYPE node.  */
11593       add_type_attribute (parm_die, node, 0, 0, context_die);
11594       break;
11595
11596     default:
11597       gcc_unreachable ();
11598     }
11599
11600   return parm_die;
11601 }
11602
11603 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
11604    at the end of an (ANSI prototyped) formal parameters list.  */
11605
11606 static void
11607 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
11608 {
11609   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
11610 }
11611
11612 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
11613    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
11614    parameters as specified in some function type specification (except for
11615    those which appear as part of a function *definition*).  */
11616
11617 static void
11618 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
11619 {
11620   tree link;
11621   tree formal_type = NULL;
11622   tree first_parm_type;
11623   tree arg;
11624
11625   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
11626     {
11627       arg = DECL_ARGUMENTS (function_or_method_type);
11628       function_or_method_type = TREE_TYPE (function_or_method_type);
11629     }
11630   else
11631     arg = NULL_TREE;
11632
11633   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
11634
11635   /* Make our first pass over the list of formal parameter types and output a
11636      DW_TAG_formal_parameter DIE for each one.  */
11637   for (link = first_parm_type; link; )
11638     {
11639       dw_die_ref parm_die;
11640
11641       formal_type = TREE_VALUE (link);
11642       if (formal_type == void_type_node)
11643         break;
11644
11645       /* Output a (nameless) DIE to represent the formal parameter itself.  */
11646       parm_die = gen_formal_parameter_die (formal_type, context_die);
11647       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
11648            && link == first_parm_type)
11649           || (arg && DECL_ARTIFICIAL (arg)))
11650         add_AT_flag (parm_die, DW_AT_artificial, 1);
11651
11652       link = TREE_CHAIN (link);
11653       if (arg)
11654         arg = TREE_CHAIN (arg);
11655     }
11656
11657   /* If this function type has an ellipsis, add a
11658      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
11659   if (formal_type != void_type_node)
11660     gen_unspecified_parameters_die (function_or_method_type, context_die);
11661
11662   /* Make our second (and final) pass over the list of formal parameter types
11663      and output DIEs to represent those types (as necessary).  */
11664   for (link = TYPE_ARG_TYPES (function_or_method_type);
11665        link && TREE_VALUE (link);
11666        link = TREE_CHAIN (link))
11667     gen_type_die (TREE_VALUE (link), context_die);
11668 }
11669
11670 /* We want to generate the DIE for TYPE so that we can generate the
11671    die for MEMBER, which has been defined; we will need to refer back
11672    to the member declaration nested within TYPE.  If we're trying to
11673    generate minimal debug info for TYPE, processing TYPE won't do the
11674    trick; we need to attach the member declaration by hand.  */
11675
11676 static void
11677 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
11678 {
11679   gen_type_die (type, context_die);
11680
11681   /* If we're trying to avoid duplicate debug info, we may not have
11682      emitted the member decl for this function.  Emit it now.  */
11683   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
11684       && ! lookup_decl_die (member))
11685     {
11686       dw_die_ref type_die;
11687       gcc_assert (!decl_ultimate_origin (member));
11688
11689       push_decl_scope (type);
11690       type_die = lookup_type_die (type);
11691       if (TREE_CODE (member) == FUNCTION_DECL)
11692         gen_subprogram_die (member, type_die);
11693       else if (TREE_CODE (member) == FIELD_DECL)
11694         {
11695           /* Ignore the nameless fields that are used to skip bits but handle
11696              C++ anonymous unions and structs.  */
11697           if (DECL_NAME (member) != NULL_TREE
11698               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
11699               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
11700             {
11701               gen_type_die (member_declared_type (member), type_die);
11702               gen_field_die (member, type_die);
11703             }
11704         }
11705       else
11706         gen_variable_die (member, type_die);
11707
11708       pop_decl_scope ();
11709     }
11710 }
11711
11712 /* Generate the DWARF2 info for the "abstract" instance of a function which we
11713    may later generate inlined and/or out-of-line instances of.  */
11714
11715 static void
11716 dwarf2out_abstract_function (tree decl)
11717 {
11718   dw_die_ref old_die;
11719   tree save_fn;
11720   struct function *save_cfun;
11721   tree context;
11722   int was_abstract = DECL_ABSTRACT (decl);
11723
11724   /* Make sure we have the actual abstract inline, not a clone.  */
11725   decl = DECL_ORIGIN (decl);
11726
11727   old_die = lookup_decl_die (decl);
11728   if (old_die && get_AT (old_die, DW_AT_inline))
11729     /* We've already generated the abstract instance.  */
11730     return;
11731
11732   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
11733      we don't get confused by DECL_ABSTRACT.  */
11734   if (debug_info_level > DINFO_LEVEL_TERSE)
11735     {
11736       context = decl_class_context (decl);
11737       if (context)
11738         gen_type_die_for_member
11739           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
11740     }
11741
11742   /* Pretend we've just finished compiling this function.  */
11743   save_fn = current_function_decl;
11744   save_cfun = cfun;
11745   current_function_decl = decl;
11746   cfun = DECL_STRUCT_FUNCTION (decl);
11747
11748   set_decl_abstract_flags (decl, 1);
11749   dwarf2out_decl (decl);
11750   if (! was_abstract)
11751     set_decl_abstract_flags (decl, 0);
11752
11753   current_function_decl = save_fn;
11754   cfun = save_cfun;
11755 }
11756
11757 /* Helper function of premark_used_types() which gets called through
11758    htab_traverse_resize().
11759
11760    Marks the DIE of a given type in *SLOT as perennial, so it never gets
11761    marked as unused by prune_unused_types.  */
11762 static int
11763 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
11764 {
11765   tree type;
11766   dw_die_ref die;
11767
11768   type = *slot;
11769   die = lookup_type_die (type);
11770   if (die != NULL)
11771     die->die_perennial_p = 1;
11772   return 1;
11773 }
11774
11775 /* Mark all members of used_types_hash as perennial.  */
11776 static void
11777 premark_used_types (void)
11778 {
11779   if (cfun && cfun->used_types_hash)
11780     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
11781 }
11782
11783 /* Generate a DIE to represent a declared function (either file-scope or
11784    block-local).  */
11785
11786 static void
11787 gen_subprogram_die (tree decl, dw_die_ref context_die)
11788 {
11789   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
11790   tree origin = decl_ultimate_origin (decl);
11791   dw_die_ref subr_die;
11792   tree fn_arg_types;
11793   tree outer_scope;
11794   dw_die_ref old_die = lookup_decl_die (decl);
11795   int declaration = (current_function_decl != decl
11796                      || class_or_namespace_scope_p (context_die));
11797
11798   premark_used_types ();
11799
11800   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
11801      started to generate the abstract instance of an inline, decided to output
11802      its containing class, and proceeded to emit the declaration of the inline
11803      from the member list for the class.  If so, DECLARATION takes priority;
11804      we'll get back to the abstract instance when done with the class.  */
11805
11806   /* The class-scope declaration DIE must be the primary DIE.  */
11807   if (origin && declaration && class_or_namespace_scope_p (context_die))
11808     {
11809       origin = NULL;
11810       gcc_assert (!old_die);
11811     }
11812
11813   /* Now that the C++ front end lazily declares artificial member fns, we
11814      might need to retrofit the declaration into its class.  */
11815   if (!declaration && !origin && !old_die
11816       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
11817       && !class_or_namespace_scope_p (context_die)
11818       && debug_info_level > DINFO_LEVEL_TERSE)
11819     old_die = force_decl_die (decl);
11820
11821   if (origin != NULL)
11822     {
11823       gcc_assert (!declaration || local_scope_p (context_die));
11824
11825       /* Fixup die_parent for the abstract instance of a nested
11826          inline function.  */
11827       if (old_die && old_die->die_parent == NULL)
11828         add_child_die (context_die, old_die);
11829
11830       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11831       add_abstract_origin_attribute (subr_die, origin);
11832     }
11833   else if (old_die)
11834     {
11835       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11836       struct dwarf_file_data * file_index = lookup_filename (s.file);
11837
11838       if (!get_AT_flag (old_die, DW_AT_declaration)
11839           /* We can have a normal definition following an inline one in the
11840              case of redefinition of GNU C extern inlines.
11841              It seems reasonable to use AT_specification in this case.  */
11842           && !get_AT (old_die, DW_AT_inline))
11843         {
11844           /* Detect and ignore this case, where we are trying to output
11845              something we have already output.  */
11846           return;
11847         }
11848
11849       /* If the definition comes from the same place as the declaration,
11850          maybe use the old DIE.  We always want the DIE for this function
11851          that has the *_pc attributes to be under comp_unit_die so the
11852          debugger can find it.  We also need to do this for abstract
11853          instances of inlines, since the spec requires the out-of-line copy
11854          to have the same parent.  For local class methods, this doesn't
11855          apply; we just use the old DIE.  */
11856       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
11857           && (DECL_ARTIFICIAL (decl)
11858               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
11859                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
11860                       == (unsigned) s.line))))
11861         {
11862           subr_die = old_die;
11863
11864           /* Clear out the declaration attribute and the formal parameters.
11865              Do not remove all children, because it is possible that this
11866              declaration die was forced using force_decl_die(). In such
11867              cases die that forced declaration die (e.g. TAG_imported_module)
11868              is one of the children that we do not want to remove.  */
11869           remove_AT (subr_die, DW_AT_declaration);
11870           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
11871         }
11872       else
11873         {
11874           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11875           add_AT_specification (subr_die, old_die);
11876           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
11877             add_AT_file (subr_die, DW_AT_decl_file, file_index);
11878           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
11879             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
11880         }
11881     }
11882   else
11883     {
11884       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
11885
11886       if (TREE_PUBLIC (decl))
11887         add_AT_flag (subr_die, DW_AT_external, 1);
11888
11889       add_name_and_src_coords_attributes (subr_die, decl);
11890       if (debug_info_level > DINFO_LEVEL_TERSE)
11891         {
11892           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
11893           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
11894                               0, 0, context_die);
11895         }
11896
11897       add_pure_or_virtual_attribute (subr_die, decl);
11898       if (DECL_ARTIFICIAL (decl))
11899         add_AT_flag (subr_die, DW_AT_artificial, 1);
11900
11901       if (TREE_PROTECTED (decl))
11902         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
11903       else if (TREE_PRIVATE (decl))
11904         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
11905     }
11906
11907   if (declaration)
11908     {
11909       if (!old_die || !get_AT (old_die, DW_AT_inline))
11910         {
11911           add_AT_flag (subr_die, DW_AT_declaration, 1);
11912
11913           /* The first time we see a member function, it is in the context of
11914              the class to which it belongs.  We make sure of this by emitting
11915              the class first.  The next time is the definition, which is
11916              handled above.  The two may come from the same source text.
11917
11918              Note that force_decl_die() forces function declaration die. It is
11919              later reused to represent definition.  */
11920           equate_decl_number_to_die (decl, subr_die);
11921         }
11922     }
11923   else if (DECL_ABSTRACT (decl))
11924     {
11925       if (DECL_DECLARED_INLINE_P (decl))
11926         {
11927           if (cgraph_function_possibly_inlined_p (decl))
11928             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
11929           else
11930             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
11931         }
11932       else
11933         {
11934           if (cgraph_function_possibly_inlined_p (decl))
11935             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
11936           else
11937             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
11938         }
11939
11940       equate_decl_number_to_die (decl, subr_die);
11941     }
11942   else if (!DECL_EXTERNAL (decl))
11943     {
11944       HOST_WIDE_INT cfa_fb_offset;
11945
11946       if (!old_die || !get_AT (old_die, DW_AT_inline))
11947         equate_decl_number_to_die (decl, subr_die);
11948
11949       if (!flag_reorder_blocks_and_partition)
11950         {
11951           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
11952                                        current_function_funcdef_no);
11953           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
11954           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
11955                                        current_function_funcdef_no);
11956           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
11957
11958           add_pubname (decl, subr_die);
11959           add_arange (decl, subr_die);
11960         }
11961       else
11962         {  /* Do nothing for now; maybe need to duplicate die, one for
11963               hot section and ond for cold section, then use the hot/cold
11964               section begin/end labels to generate the aranges...  */
11965           /*
11966             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
11967             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
11968             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
11969             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
11970
11971             add_pubname (decl, subr_die);
11972             add_arange (decl, subr_die);
11973             add_arange (decl, subr_die);
11974            */
11975         }
11976
11977 #ifdef MIPS_DEBUGGING_INFO
11978       /* Add a reference to the FDE for this routine.  */
11979       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
11980 #endif
11981
11982       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
11983
11984       /* We define the "frame base" as the function's CFA.  This is more
11985          convenient for several reasons: (1) It's stable across the prologue
11986          and epilogue, which makes it better than just a frame pointer,
11987          (2) With dwarf3, there exists a one-byte encoding that allows us
11988          to reference the .debug_frame data by proxy, but failing that,
11989          (3) We can at least reuse the code inspection and interpretation
11990          code that determines the CFA position at various points in the
11991          function.  */
11992       /* ??? Use some command-line or configury switch to enable the use
11993          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
11994          consumers that understand it; fall back to "pure" dwarf2 and
11995          convert the CFA data into a location list.  */
11996       {
11997         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
11998         if (list->dw_loc_next)
11999           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12000         else
12001           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12002       }
12003
12004       /* Compute a displacement from the "steady-state frame pointer" to
12005          the CFA.  The former is what all stack slots and argument slots
12006          will reference in the rtl; the later is what we've told the
12007          debugger about.  We'll need to adjust all frame_base references
12008          by this displacement.  */
12009       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12010
12011       if (cfun->static_chain_decl)
12012         add_AT_location_description (subr_die, DW_AT_static_link,
12013                  loc_descriptor_from_tree (cfun->static_chain_decl));
12014     }
12015
12016   /* Now output descriptions of the arguments for this function. This gets
12017      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12018      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12019      `...' at the end of the formal parameter list.  In order to find out if
12020      there was a trailing ellipsis or not, we must instead look at the type
12021      associated with the FUNCTION_DECL.  This will be a node of type
12022      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12023      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12024      an ellipsis at the end.  */
12025
12026   /* In the case where we are describing a mere function declaration, all we
12027      need to do here (and all we *can* do here) is to describe the *types* of
12028      its formal parameters.  */
12029   if (debug_info_level <= DINFO_LEVEL_TERSE)
12030     ;
12031   else if (declaration)
12032     gen_formal_types_die (decl, subr_die);
12033   else
12034     {
12035       /* Generate DIEs to represent all known formal parameters.  */
12036       tree arg_decls = DECL_ARGUMENTS (decl);
12037       tree parm;
12038
12039       /* When generating DIEs, generate the unspecified_parameters DIE
12040          instead if we come across the arg "__builtin_va_alist" */
12041       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12042         if (TREE_CODE (parm) == PARM_DECL)
12043           {
12044             if (DECL_NAME (parm)
12045                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12046                             "__builtin_va_alist"))
12047               gen_unspecified_parameters_die (parm, subr_die);
12048             else
12049               gen_decl_die (parm, subr_die);
12050           }
12051
12052       /* Decide whether we need an unspecified_parameters DIE at the end.
12053          There are 2 more cases to do this for: 1) the ansi ... declaration -
12054          this is detectable when the end of the arg list is not a
12055          void_type_node 2) an unprototyped function declaration (not a
12056          definition).  This just means that we have no info about the
12057          parameters at all.  */
12058       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12059       if (fn_arg_types != NULL)
12060         {
12061           /* This is the prototyped case, check for....  */
12062           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12063             gen_unspecified_parameters_die (decl, subr_die);
12064         }
12065       else if (DECL_INITIAL (decl) == NULL_TREE)
12066         gen_unspecified_parameters_die (decl, subr_die);
12067     }
12068
12069   /* Output Dwarf info for all of the stuff within the body of the function
12070      (if it has one - it may be just a declaration).  */
12071   outer_scope = DECL_INITIAL (decl);
12072
12073   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12074      a function.  This BLOCK actually represents the outermost binding contour
12075      for the function, i.e. the contour in which the function's formal
12076      parameters and labels get declared. Curiously, it appears that the front
12077      end doesn't actually put the PARM_DECL nodes for the current function onto
12078      the BLOCK_VARS list for this outer scope, but are strung off of the
12079      DECL_ARGUMENTS list for the function instead.
12080
12081      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12082      the LABEL_DECL nodes for the function however, and we output DWARF info
12083      for those in decls_for_scope.  Just within the `outer_scope' there will be
12084      a BLOCK node representing the function's outermost pair of curly braces,
12085      and any blocks used for the base and member initializers of a C++
12086      constructor function.  */
12087   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12088     {
12089       /* Emit a DW_TAG_variable DIE for a named return value.  */
12090       if (DECL_NAME (DECL_RESULT (decl)))
12091         gen_decl_die (DECL_RESULT (decl), subr_die);
12092
12093       current_function_has_inlines = 0;
12094       decls_for_scope (outer_scope, subr_die, 0);
12095
12096 #if 0 && defined (MIPS_DEBUGGING_INFO)
12097       if (current_function_has_inlines)
12098         {
12099           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12100           if (! comp_unit_has_inlines)
12101             {
12102               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12103               comp_unit_has_inlines = 1;
12104             }
12105         }
12106 #endif
12107     }
12108   /* Add the calling convention attribute if requested.  */
12109   add_calling_convention_attribute (subr_die, TREE_TYPE (decl));
12110
12111 }
12112
12113 /* Generate a DIE to represent a declared data object.  */
12114
12115 static void
12116 gen_variable_die (tree decl, dw_die_ref context_die)
12117 {
12118   tree origin = decl_ultimate_origin (decl);
12119   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12120
12121   dw_die_ref old_die = lookup_decl_die (decl);
12122   int declaration = (DECL_EXTERNAL (decl)
12123                      /* If DECL is COMDAT and has not actually been
12124                         emitted, we cannot take its address; there
12125                         might end up being no definition anywhere in
12126                         the program.  For example, consider the C++
12127                         test case:
12128
12129                           template <class T>
12130                           struct S { static const int i = 7; };
12131
12132                           template <class T>
12133                           const int S<T>::i;
12134
12135                           int f() { return S<int>::i; }
12136
12137                         Here, S<int>::i is not DECL_EXTERNAL, but no
12138                         definition is required, so the compiler will
12139                         not emit a definition.  */
12140                      || (TREE_CODE (decl) == VAR_DECL
12141                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12142                      || class_or_namespace_scope_p (context_die));
12143
12144   if (origin != NULL)
12145     add_abstract_origin_attribute (var_die, origin);
12146
12147   /* Loop unrolling can create multiple blocks that refer to the same
12148      static variable, so we must test for the DW_AT_declaration flag.
12149
12150      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12151      copy decls and set the DECL_ABSTRACT flag on them instead of
12152      sharing them.
12153
12154      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12155
12156      ??? The declare_in_namespace support causes us to get two DIEs for one
12157      variable, both of which are declarations.  We want to avoid considering
12158      one to be a specification, so we must test that this DIE is not a
12159      declaration.  */
12160   else if (old_die && TREE_STATIC (decl) && ! declaration
12161            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12162     {
12163       /* This is a definition of a C++ class level static.  */
12164       add_AT_specification (var_die, old_die);
12165       if (DECL_NAME (decl))
12166         {
12167           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12168           struct dwarf_file_data * file_index = lookup_filename (s.file);
12169
12170           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12171             add_AT_file (var_die, DW_AT_decl_file, file_index);
12172
12173           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12174             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12175         }
12176     }
12177   else
12178     {
12179       add_name_and_src_coords_attributes (var_die, decl);
12180       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
12181                           TREE_THIS_VOLATILE (decl), context_die);
12182
12183       if (TREE_PUBLIC (decl))
12184         add_AT_flag (var_die, DW_AT_external, 1);
12185
12186       if (DECL_ARTIFICIAL (decl))
12187         add_AT_flag (var_die, DW_AT_artificial, 1);
12188
12189       if (TREE_PROTECTED (decl))
12190         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12191       else if (TREE_PRIVATE (decl))
12192         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12193     }
12194
12195   if (declaration)
12196     add_AT_flag (var_die, DW_AT_declaration, 1);
12197
12198   if (DECL_ABSTRACT (decl) || declaration)
12199     equate_decl_number_to_die (decl, var_die);
12200
12201   if (! declaration && ! DECL_ABSTRACT (decl))
12202     {
12203       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12204       add_pubname (decl, var_die);
12205     }
12206   else
12207     tree_add_const_value_attribute (var_die, decl);
12208 }
12209
12210 /* Generate a DIE to represent a label identifier.  */
12211
12212 static void
12213 gen_label_die (tree decl, dw_die_ref context_die)
12214 {
12215   tree origin = decl_ultimate_origin (decl);
12216   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12217   rtx insn;
12218   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12219
12220   if (origin != NULL)
12221     add_abstract_origin_attribute (lbl_die, origin);
12222   else
12223     add_name_and_src_coords_attributes (lbl_die, decl);
12224
12225   if (DECL_ABSTRACT (decl))
12226     equate_decl_number_to_die (decl, lbl_die);
12227   else
12228     {
12229       insn = DECL_RTL_IF_SET (decl);
12230
12231       /* Deleted labels are programmer specified labels which have been
12232          eliminated because of various optimizations.  We still emit them
12233          here so that it is possible to put breakpoints on them.  */
12234       if (insn
12235           && (LABEL_P (insn)
12236               || ((NOTE_P (insn)
12237                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12238         {
12239           /* When optimization is enabled (via -O) some parts of the compiler
12240              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12241              represent source-level labels which were explicitly declared by
12242              the user.  This really shouldn't be happening though, so catch
12243              it if it ever does happen.  */
12244           gcc_assert (!INSN_DELETED_P (insn));
12245
12246           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12247           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12248         }
12249     }
12250 }
12251
12252 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12253    attributes to the DIE for a block STMT, to describe where the inlined
12254    function was called from.  This is similar to add_src_coords_attributes.  */
12255
12256 static inline void
12257 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12258 {
12259   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12260
12261   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12262   add_AT_unsigned (die, DW_AT_call_line, s.line);
12263 }
12264
12265
12266 /* If STMT's abstract origin is a function declaration and STMT's
12267    first subblock's abstract origin is the function's outermost block,
12268    then we're looking at the main entry point.  */
12269 static bool
12270 is_inlined_entry_point (tree stmt)
12271 {
12272   tree decl, block;
12273
12274   if (!stmt || TREE_CODE (stmt) != BLOCK)
12275     return false;
12276
12277   decl = block_ultimate_origin (stmt);
12278
12279   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12280     return false;
12281
12282   block = BLOCK_SUBBLOCKS (stmt);
12283
12284   if (block)
12285     {
12286       if (TREE_CODE (block) != BLOCK)
12287         return false;
12288
12289       block = block_ultimate_origin (block);
12290     }
12291
12292   return block == DECL_INITIAL (decl);
12293 }
12294
12295 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12296    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12297
12298 static inline void
12299 add_high_low_attributes (tree stmt, dw_die_ref die)
12300 {
12301   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12302
12303   if (BLOCK_FRAGMENT_CHAIN (stmt))
12304     {
12305       tree chain;
12306
12307       if (is_inlined_entry_point (stmt))
12308         {
12309           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12310                                        BLOCK_NUMBER (stmt));
12311           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12312         }
12313
12314       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12315
12316       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12317       do
12318         {
12319           add_ranges (chain);
12320           chain = BLOCK_FRAGMENT_CHAIN (chain);
12321         }
12322       while (chain);
12323       add_ranges (NULL);
12324     }
12325   else
12326     {
12327       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12328                                    BLOCK_NUMBER (stmt));
12329       add_AT_lbl_id (die, DW_AT_low_pc, label);
12330       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12331                                    BLOCK_NUMBER (stmt));
12332       add_AT_lbl_id (die, DW_AT_high_pc, label);
12333     }
12334 }
12335
12336 /* Generate a DIE for a lexical block.  */
12337
12338 static void
12339 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12340 {
12341   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12342
12343   if (! BLOCK_ABSTRACT (stmt))
12344     add_high_low_attributes (stmt, stmt_die);
12345
12346   decls_for_scope (stmt, stmt_die, depth);
12347 }
12348
12349 /* Generate a DIE for an inlined subprogram.  */
12350
12351 static void
12352 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12353 {
12354   tree decl = block_ultimate_origin (stmt);
12355
12356   /* Emit info for the abstract instance first, if we haven't yet.  We
12357      must emit this even if the block is abstract, otherwise when we
12358      emit the block below (or elsewhere), we may end up trying to emit
12359      a die whose origin die hasn't been emitted, and crashing.  */
12360   dwarf2out_abstract_function (decl);
12361
12362   if (! BLOCK_ABSTRACT (stmt))
12363     {
12364       dw_die_ref subr_die
12365         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12366
12367       add_abstract_origin_attribute (subr_die, decl);
12368       add_high_low_attributes (stmt, subr_die);
12369       add_call_src_coords_attributes (stmt, subr_die);
12370
12371       decls_for_scope (stmt, subr_die, depth);
12372       current_function_has_inlines = 1;
12373     }
12374   else
12375     /* We may get here if we're the outer block of function A that was
12376        inlined into function B that was inlined into function C.  When
12377        generating debugging info for C, dwarf2out_abstract_function(B)
12378        would mark all inlined blocks as abstract, including this one.
12379        So, we wouldn't (and shouldn't) expect labels to be generated
12380        for this one.  Instead, just emit debugging info for
12381        declarations within the block.  This is particularly important
12382        in the case of initializers of arguments passed from B to us:
12383        if they're statement expressions containing declarations, we
12384        wouldn't generate dies for their abstract variables, and then,
12385        when generating dies for the real variables, we'd die (pun
12386        intended :-)  */
12387     gen_lexical_block_die (stmt, context_die, depth);
12388 }
12389
12390 /* Generate a DIE for a field in a record, or structure.  */
12391
12392 static void
12393 gen_field_die (tree decl, dw_die_ref context_die)
12394 {
12395   dw_die_ref decl_die;
12396
12397   if (TREE_TYPE (decl) == error_mark_node)
12398     return;
12399
12400   decl_die = new_die (DW_TAG_member, context_die, decl);
12401   add_name_and_src_coords_attributes (decl_die, decl);
12402   add_type_attribute (decl_die, member_declared_type (decl),
12403                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12404                       context_die);
12405
12406   if (DECL_BIT_FIELD_TYPE (decl))
12407     {
12408       add_byte_size_attribute (decl_die, decl);
12409       add_bit_size_attribute (decl_die, decl);
12410       add_bit_offset_attribute (decl_die, decl);
12411     }
12412
12413   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12414     add_data_member_location_attribute (decl_die, decl);
12415
12416   if (DECL_ARTIFICIAL (decl))
12417     add_AT_flag (decl_die, DW_AT_artificial, 1);
12418
12419   if (TREE_PROTECTED (decl))
12420     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12421   else if (TREE_PRIVATE (decl))
12422     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12423
12424   /* Equate decl number to die, so that we can look up this decl later on.  */
12425   equate_decl_number_to_die (decl, decl_die);
12426 }
12427
12428 #if 0
12429 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12430    Use modified_type_die instead.
12431    We keep this code here just in case these types of DIEs may be needed to
12432    represent certain things in other languages (e.g. Pascal) someday.  */
12433
12434 static void
12435 gen_pointer_type_die (tree type, dw_die_ref context_die)
12436 {
12437   dw_die_ref ptr_die
12438     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12439
12440   equate_type_number_to_die (type, ptr_die);
12441   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12442   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12443 }
12444
12445 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12446    Use modified_type_die instead.
12447    We keep this code here just in case these types of DIEs may be needed to
12448    represent certain things in other languages (e.g. Pascal) someday.  */
12449
12450 static void
12451 gen_reference_type_die (tree type, dw_die_ref context_die)
12452 {
12453   dw_die_ref ref_die
12454     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12455
12456   equate_type_number_to_die (type, ref_die);
12457   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12458   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12459 }
12460 #endif
12461
12462 /* Generate a DIE for a pointer to a member type.  */
12463
12464 static void
12465 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12466 {
12467   dw_die_ref ptr_die
12468     = new_die (DW_TAG_ptr_to_member_type,
12469                scope_die_for (type, context_die), type);
12470
12471   equate_type_number_to_die (type, ptr_die);
12472   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12473                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12474   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12475 }
12476
12477 /* Generate the DIE for the compilation unit.  */
12478
12479 static dw_die_ref
12480 gen_compile_unit_die (const char *filename)
12481 {
12482   dw_die_ref die;
12483   char producer[250];
12484   const char *language_string = lang_hooks.name;
12485   int language;
12486
12487   die = new_die (DW_TAG_compile_unit, NULL, NULL);
12488
12489   if (filename)
12490     {
12491       add_name_attribute (die, filename);
12492       /* Don't add cwd for <built-in>.  */
12493       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12494         add_comp_dir_attribute (die);
12495     }
12496
12497   sprintf (producer, "%s %s", language_string, version_string);
12498
12499 #ifdef MIPS_DEBUGGING_INFO
12500   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12501      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12502      not appear in the producer string, the debugger reaches the conclusion
12503      that the object file is stripped and has no debugging information.
12504      To get the MIPS/SGI debugger to believe that there is debugging
12505      information in the object file, we add a -g to the producer string.  */
12506   if (debug_info_level > DINFO_LEVEL_TERSE)
12507     strcat (producer, " -g");
12508 #endif
12509
12510   add_AT_string (die, DW_AT_producer, producer);
12511
12512   if (strcmp (language_string, "GNU C++") == 0)
12513     language = DW_LANG_C_plus_plus;
12514   else if (strcmp (language_string, "GNU Ada") == 0)
12515     language = DW_LANG_Ada95;
12516   else if (strcmp (language_string, "GNU F77") == 0)
12517     language = DW_LANG_Fortran77;
12518   else if (strcmp (language_string, "GNU F95") == 0)
12519     language = DW_LANG_Fortran95;
12520   else if (strcmp (language_string, "GNU Pascal") == 0)
12521     language = DW_LANG_Pascal83;
12522   else if (strcmp (language_string, "GNU Java") == 0)
12523     language = DW_LANG_Java;
12524   else if (strcmp (language_string, "GNU Objective-C") == 0)
12525     language = DW_LANG_ObjC;
12526   else if (strcmp (language_string, "GNU Objective-C++") == 0)
12527     language = DW_LANG_ObjC_plus_plus;
12528   else
12529     language = DW_LANG_C89;
12530
12531   add_AT_unsigned (die, DW_AT_language, language);
12532   return die;
12533 }
12534
12535 /* Generate the DIE for a base class.  */
12536
12537 static void
12538 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
12539 {
12540   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
12541
12542   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
12543   add_data_member_location_attribute (die, binfo);
12544
12545   if (BINFO_VIRTUAL_P (binfo))
12546     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12547
12548   if (access == access_public_node)
12549     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
12550   else if (access == access_protected_node)
12551     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
12552 }
12553
12554 /* Generate a DIE for a class member.  */
12555
12556 static void
12557 gen_member_die (tree type, dw_die_ref context_die)
12558 {
12559   tree member;
12560   tree binfo = TYPE_BINFO (type);
12561   dw_die_ref child;
12562
12563   /* If this is not an incomplete type, output descriptions of each of its
12564      members. Note that as we output the DIEs necessary to represent the
12565      members of this record or union type, we will also be trying to output
12566      DIEs to represent the *types* of those members. However the `type'
12567      function (above) will specifically avoid generating type DIEs for member
12568      types *within* the list of member DIEs for this (containing) type except
12569      for those types (of members) which are explicitly marked as also being
12570      members of this (containing) type themselves.  The g++ front- end can
12571      force any given type to be treated as a member of some other (containing)
12572      type by setting the TYPE_CONTEXT of the given (member) type to point to
12573      the TREE node representing the appropriate (containing) type.  */
12574
12575   /* First output info about the base classes.  */
12576   if (binfo)
12577     {
12578       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
12579       int i;
12580       tree base;
12581
12582       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
12583         gen_inheritance_die (base,
12584                              (accesses ? VEC_index (tree, accesses, i)
12585                               : access_public_node), context_die);
12586     }
12587
12588   /* Now output info about the data members and type members.  */
12589   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
12590     {
12591       /* If we thought we were generating minimal debug info for TYPE
12592          and then changed our minds, some of the member declarations
12593          may have already been defined.  Don't define them again, but
12594          do put them in the right order.  */
12595
12596       child = lookup_decl_die (member);
12597       if (child)
12598         splice_child_die (context_die, child);
12599       else
12600         gen_decl_die (member, context_die);
12601     }
12602
12603   /* Now output info about the function members (if any).  */
12604   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
12605     {
12606       /* Don't include clones in the member list.  */
12607       if (DECL_ABSTRACT_ORIGIN (member))
12608         continue;
12609
12610       child = lookup_decl_die (member);
12611       if (child)
12612         splice_child_die (context_die, child);
12613       else
12614         gen_decl_die (member, context_die);
12615     }
12616 }
12617
12618 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
12619    is set, we pretend that the type was never defined, so we only get the
12620    member DIEs needed by later specification DIEs.  */
12621
12622 static void
12623 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
12624                                 enum debug_info_usage usage)
12625 {
12626   dw_die_ref type_die = lookup_type_die (type);
12627   dw_die_ref scope_die = 0;
12628   int nested = 0;
12629   int complete = (TYPE_SIZE (type)
12630                   && (! TYPE_STUB_DECL (type)
12631                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
12632   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
12633   complete = complete && should_emit_struct_debug (type, usage);
12634
12635   if (type_die && ! complete)
12636     return;
12637
12638   if (TYPE_CONTEXT (type) != NULL_TREE
12639       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12640           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
12641     nested = 1;
12642
12643   scope_die = scope_die_for (type, context_die);
12644
12645   if (! type_die || (nested && scope_die == comp_unit_die))
12646     /* First occurrence of type or toplevel definition of nested class.  */
12647     {
12648       dw_die_ref old_die = type_die;
12649
12650       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
12651                           ? DW_TAG_structure_type : DW_TAG_union_type,
12652                           scope_die, type);
12653       equate_type_number_to_die (type, type_die);
12654       if (old_die)
12655         add_AT_specification (type_die, old_die);
12656       else
12657         add_name_attribute (type_die, type_tag (type));
12658     }
12659   else
12660     remove_AT (type_die, DW_AT_declaration);
12661
12662   /* If this type has been completed, then give it a byte_size attribute and
12663      then give a list of members.  */
12664   if (complete && !ns_decl)
12665     {
12666       /* Prevent infinite recursion in cases where the type of some member of
12667          this type is expressed in terms of this type itself.  */
12668       TREE_ASM_WRITTEN (type) = 1;
12669       add_byte_size_attribute (type_die, type);
12670       if (TYPE_STUB_DECL (type) != NULL_TREE)
12671         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12672
12673       /* If the first reference to this type was as the return type of an
12674          inline function, then it may not have a parent.  Fix this now.  */
12675       if (type_die->die_parent == NULL)
12676         add_child_die (scope_die, type_die);
12677
12678       push_decl_scope (type);
12679       gen_member_die (type, type_die);
12680       pop_decl_scope ();
12681
12682       /* GNU extension: Record what type our vtable lives in.  */
12683       if (TYPE_VFIELD (type))
12684         {
12685           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
12686
12687           gen_type_die (vtype, context_die);
12688           add_AT_die_ref (type_die, DW_AT_containing_type,
12689                           lookup_type_die (vtype));
12690         }
12691     }
12692   else
12693     {
12694       add_AT_flag (type_die, DW_AT_declaration, 1);
12695
12696       /* We don't need to do this for function-local types.  */
12697       if (TYPE_STUB_DECL (type)
12698           && ! decl_function_context (TYPE_STUB_DECL (type)))
12699         VEC_safe_push (tree, gc, incomplete_types, type);
12700     }
12701
12702   if (get_AT (type_die, DW_AT_name))
12703     add_pubtype (type, type_die);
12704 }
12705
12706 /* Generate a DIE for a subroutine _type_.  */
12707
12708 static void
12709 gen_subroutine_type_die (tree type, dw_die_ref context_die)
12710 {
12711   tree return_type = TREE_TYPE (type);
12712   dw_die_ref subr_die
12713     = new_die (DW_TAG_subroutine_type,
12714                scope_die_for (type, context_die), type);
12715
12716   equate_type_number_to_die (type, subr_die);
12717   add_prototyped_attribute (subr_die, type);
12718   add_type_attribute (subr_die, return_type, 0, 0, context_die);
12719   gen_formal_types_die (type, subr_die);
12720
12721   if (get_AT (subr_die, DW_AT_name))
12722     add_pubtype (type, subr_die);
12723 }
12724
12725 /* Generate a DIE for a type definition.  */
12726
12727 static void
12728 gen_typedef_die (tree decl, dw_die_ref context_die)
12729 {
12730   dw_die_ref type_die;
12731   tree origin;
12732
12733   if (TREE_ASM_WRITTEN (decl))
12734     return;
12735
12736   TREE_ASM_WRITTEN (decl) = 1;
12737   type_die = new_die (DW_TAG_typedef, context_die, decl);
12738   origin = decl_ultimate_origin (decl);
12739   if (origin != NULL)
12740     add_abstract_origin_attribute (type_die, origin);
12741   else
12742     {
12743       tree type;
12744
12745       add_name_and_src_coords_attributes (type_die, decl);
12746       if (DECL_ORIGINAL_TYPE (decl))
12747         {
12748           type = DECL_ORIGINAL_TYPE (decl);
12749
12750           gcc_assert (type != TREE_TYPE (decl));
12751           equate_type_number_to_die (TREE_TYPE (decl), type_die);
12752         }
12753       else
12754         type = TREE_TYPE (decl);
12755
12756       add_type_attribute (type_die, type, TREE_READONLY (decl),
12757                           TREE_THIS_VOLATILE (decl), context_die);
12758     }
12759
12760   if (DECL_ABSTRACT (decl))
12761     equate_decl_number_to_die (decl, type_die);
12762
12763   if (get_AT (type_die, DW_AT_name))
12764     add_pubtype (decl, type_die);
12765 }
12766
12767 /* Generate a type description DIE.  */
12768
12769 static void
12770 gen_type_die_with_usage (tree type, dw_die_ref context_die,
12771                                 enum debug_info_usage usage)
12772 {
12773   int need_pop;
12774
12775   if (type == NULL_TREE || type == error_mark_node)
12776     return;
12777
12778   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12779       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
12780     {
12781       if (TREE_ASM_WRITTEN (type))
12782         return;
12783
12784       /* Prevent broken recursion; we can't hand off to the same type.  */
12785       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
12786
12787       TREE_ASM_WRITTEN (type) = 1;
12788       gen_decl_die (TYPE_NAME (type), context_die);
12789       return;
12790     }
12791
12792   /* We are going to output a DIE to represent the unqualified version
12793      of this type (i.e. without any const or volatile qualifiers) so
12794      get the main variant (i.e. the unqualified version) of this type
12795      now.  (Vectors are special because the debugging info is in the
12796      cloned type itself).  */
12797   if (TREE_CODE (type) != VECTOR_TYPE)
12798     type = type_main_variant (type);
12799
12800   if (TREE_ASM_WRITTEN (type))
12801     return;
12802
12803   switch (TREE_CODE (type))
12804     {
12805     case ERROR_MARK:
12806       break;
12807
12808     case POINTER_TYPE:
12809     case REFERENCE_TYPE:
12810       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
12811          ensures that the gen_type_die recursion will terminate even if the
12812          type is recursive.  Recursive types are possible in Ada.  */
12813       /* ??? We could perhaps do this for all types before the switch
12814          statement.  */
12815       TREE_ASM_WRITTEN (type) = 1;
12816
12817       /* For these types, all that is required is that we output a DIE (or a
12818          set of DIEs) to represent the "basis" type.  */
12819       gen_type_die_with_usage (TREE_TYPE (type), context_die,
12820                                 DINFO_USAGE_IND_USE);
12821       break;
12822
12823     case OFFSET_TYPE:
12824       /* This code is used for C++ pointer-to-data-member types.
12825          Output a description of the relevant class type.  */
12826       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
12827                                         DINFO_USAGE_IND_USE);
12828
12829       /* Output a description of the type of the object pointed to.  */
12830       gen_type_die_with_usage (TREE_TYPE (type), context_die,
12831                                         DINFO_USAGE_IND_USE);
12832
12833       /* Now output a DIE to represent this pointer-to-data-member type
12834          itself.  */
12835       gen_ptr_to_mbr_type_die (type, context_die);
12836       break;
12837
12838     case FUNCTION_TYPE:
12839       /* Force out return type (in case it wasn't forced out already).  */
12840       gen_type_die_with_usage (TREE_TYPE (type), context_die,
12841                                         DINFO_USAGE_DIR_USE);
12842       gen_subroutine_type_die (type, context_die);
12843       break;
12844
12845     case METHOD_TYPE:
12846       /* Force out return type (in case it wasn't forced out already).  */
12847       gen_type_die_with_usage (TREE_TYPE (type), context_die,
12848                                         DINFO_USAGE_DIR_USE);
12849       gen_subroutine_type_die (type, context_die);
12850       break;
12851
12852     case ARRAY_TYPE:
12853       gen_array_type_die (type, context_die);
12854       break;
12855
12856     case VECTOR_TYPE:
12857       gen_array_type_die (type, context_die);
12858       break;
12859
12860     case ENUMERAL_TYPE:
12861     case RECORD_TYPE:
12862     case UNION_TYPE:
12863     case QUAL_UNION_TYPE:
12864       /* If this is a nested type whose containing class hasn't been written
12865          out yet, writing it out will cover this one, too.  This does not apply
12866          to instantiations of member class templates; they need to be added to
12867          the containing class as they are generated.  FIXME: This hurts the
12868          idea of combining type decls from multiple TUs, since we can't predict
12869          what set of template instantiations we'll get.  */
12870       if (TYPE_CONTEXT (type)
12871           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
12872           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
12873         {
12874           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
12875
12876           if (TREE_ASM_WRITTEN (type))
12877             return;
12878
12879           /* If that failed, attach ourselves to the stub.  */
12880           push_decl_scope (TYPE_CONTEXT (type));
12881           context_die = lookup_type_die (TYPE_CONTEXT (type));
12882           need_pop = 1;
12883         }
12884       else
12885         {
12886           declare_in_namespace (type, context_die);
12887           need_pop = 0;
12888         }
12889
12890       if (TREE_CODE (type) == ENUMERAL_TYPE)
12891         {
12892           /* This might have been written out by the call to
12893              declare_in_namespace.  */
12894           if (!TREE_ASM_WRITTEN (type))
12895             gen_enumeration_type_die (type, context_die);
12896         }
12897       else
12898         gen_struct_or_union_type_die (type, context_die, usage);
12899
12900       if (need_pop)
12901         pop_decl_scope ();
12902
12903       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
12904          it up if it is ever completed.  gen_*_type_die will set it for us
12905          when appropriate.  */
12906       return;
12907
12908     case VOID_TYPE:
12909     case INTEGER_TYPE:
12910     case REAL_TYPE:
12911     case COMPLEX_TYPE:
12912     case BOOLEAN_TYPE:
12913       /* No DIEs needed for fundamental types.  */
12914       break;
12915
12916     case LANG_TYPE:
12917       /* No Dwarf representation currently defined.  */
12918       break;
12919
12920     default:
12921       gcc_unreachable ();
12922     }
12923
12924   TREE_ASM_WRITTEN (type) = 1;
12925 }
12926
12927 static void
12928 gen_type_die (tree type, dw_die_ref context_die)
12929 {
12930   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
12931 }
12932
12933 /* Generate a DIE for a tagged type instantiation.  */
12934
12935 static void
12936 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
12937 {
12938   if (type == NULL_TREE || type == error_mark_node)
12939     return;
12940
12941   /* We are going to output a DIE to represent the unqualified version of
12942      this type (i.e. without any const or volatile qualifiers) so make sure
12943      that we have the main variant (i.e. the unqualified version) of this
12944      type now.  */
12945   gcc_assert (type == type_main_variant (type));
12946
12947   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
12948      an instance of an unresolved type.  */
12949
12950   switch (TREE_CODE (type))
12951     {
12952     case ERROR_MARK:
12953       break;
12954
12955     case ENUMERAL_TYPE:
12956       gen_inlined_enumeration_type_die (type, context_die);
12957       break;
12958
12959     case RECORD_TYPE:
12960       gen_inlined_structure_type_die (type, context_die);
12961       break;
12962
12963     case UNION_TYPE:
12964     case QUAL_UNION_TYPE:
12965       gen_inlined_union_type_die (type, context_die);
12966       break;
12967
12968     default:
12969       gcc_unreachable ();
12970     }
12971 }
12972
12973 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
12974    things which are local to the given block.  */
12975
12976 static void
12977 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
12978 {
12979   int must_output_die = 0;
12980   tree origin;
12981   tree decl;
12982   enum tree_code origin_code;
12983
12984   /* Ignore blocks that are NULL.  */
12985   if (stmt == NULL_TREE)
12986     return;
12987
12988   /* If the block is one fragment of a non-contiguous block, do not
12989      process the variables, since they will have been done by the
12990      origin block.  Do process subblocks.  */
12991   if (BLOCK_FRAGMENT_ORIGIN (stmt))
12992     {
12993       tree sub;
12994
12995       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
12996         gen_block_die (sub, context_die, depth + 1);
12997
12998       return;
12999     }
13000
13001   /* Determine the "ultimate origin" of this block.  This block may be an
13002      inlined instance of an inlined instance of inline function, so we have
13003      to trace all of the way back through the origin chain to find out what
13004      sort of node actually served as the original seed for the creation of
13005      the current block.  */
13006   origin = block_ultimate_origin (stmt);
13007   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13008
13009   /* Determine if we need to output any Dwarf DIEs at all to represent this
13010      block.  */
13011   if (origin_code == FUNCTION_DECL)
13012     /* The outer scopes for inlinings *must* always be represented.  We
13013        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13014     must_output_die = 1;
13015   else
13016     {
13017       /* In the case where the current block represents an inlining of the
13018          "body block" of an inline function, we must *NOT* output any DIE for
13019          this block because we have already output a DIE to represent the whole
13020          inlined function scope and the "body block" of any function doesn't
13021          really represent a different scope according to ANSI C rules.  So we
13022          check here to make sure that this block does not represent a "body
13023          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13024       if (! is_body_block (origin ? origin : stmt))
13025         {
13026           /* Determine if this block directly contains any "significant"
13027              local declarations which we will need to output DIEs for.  */
13028           if (debug_info_level > DINFO_LEVEL_TERSE)
13029             /* We are not in terse mode so *any* local declaration counts
13030                as being a "significant" one.  */
13031             must_output_die = (BLOCK_VARS (stmt) != NULL
13032                                && (TREE_USED (stmt)
13033                                    || TREE_ASM_WRITTEN (stmt)
13034                                    || BLOCK_ABSTRACT (stmt)));
13035           else
13036             /* We are in terse mode, so only local (nested) function
13037                definitions count as "significant" local declarations.  */
13038             for (decl = BLOCK_VARS (stmt);
13039                  decl != NULL; decl = TREE_CHAIN (decl))
13040               if (TREE_CODE (decl) == FUNCTION_DECL
13041                   && DECL_INITIAL (decl))
13042                 {
13043                   must_output_die = 1;
13044                   break;
13045                 }
13046         }
13047     }
13048
13049   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13050      DIE for any block which contains no significant local declarations at
13051      all.  Rather, in such cases we just call `decls_for_scope' so that any
13052      needed Dwarf info for any sub-blocks will get properly generated. Note
13053      that in terse mode, our definition of what constitutes a "significant"
13054      local declaration gets restricted to include only inlined function
13055      instances and local (nested) function definitions.  */
13056   if (must_output_die)
13057     {
13058       if (origin_code == FUNCTION_DECL)
13059         gen_inlined_subroutine_die (stmt, context_die, depth);
13060       else
13061         gen_lexical_block_die (stmt, context_die, depth);
13062     }
13063   else
13064     decls_for_scope (stmt, context_die, depth);
13065 }
13066
13067 /* Generate all of the decls declared within a given scope and (recursively)
13068    all of its sub-blocks.  */
13069
13070 static void
13071 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13072 {
13073   tree decl;
13074   tree subblocks;
13075
13076   /* Ignore NULL blocks.  */
13077   if (stmt == NULL_TREE)
13078     return;
13079
13080   if (TREE_USED (stmt))
13081     {
13082       /* Output the DIEs to represent all of the data objects and typedefs
13083          declared directly within this block but not within any nested
13084          sub-blocks.  Also, nested function and tag DIEs have been
13085          generated with a parent of NULL; fix that up now.  */
13086       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13087         {
13088           dw_die_ref die;
13089
13090           if (TREE_CODE (decl) == FUNCTION_DECL)
13091             die = lookup_decl_die (decl);
13092           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13093             die = lookup_type_die (TREE_TYPE (decl));
13094           else
13095             die = NULL;
13096
13097           if (die != NULL && die->die_parent == NULL)
13098             add_child_die (context_die, die);
13099           /* Do not produce debug information for static variables since
13100              these might be optimized out.  We are called for these later
13101              in varpool_analyze_pending_decls. */
13102           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13103             ;
13104           else
13105             gen_decl_die (decl, context_die);
13106         }
13107     }
13108
13109   /* If we're at -g1, we're not interested in subblocks.  */
13110   if (debug_info_level <= DINFO_LEVEL_TERSE)
13111     return;
13112
13113   /* Output the DIEs to represent all sub-blocks (and the items declared
13114      therein) of this block.  */
13115   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13116        subblocks != NULL;
13117        subblocks = BLOCK_CHAIN (subblocks))
13118     gen_block_die (subblocks, context_die, depth + 1);
13119 }
13120
13121 /* Is this a typedef we can avoid emitting?  */
13122
13123 static inline int
13124 is_redundant_typedef (tree decl)
13125 {
13126   if (TYPE_DECL_IS_STUB (decl))
13127     return 1;
13128
13129   if (DECL_ARTIFICIAL (decl)
13130       && DECL_CONTEXT (decl)
13131       && is_tagged_type (DECL_CONTEXT (decl))
13132       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13133       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13134     /* Also ignore the artificial member typedef for the class name.  */
13135     return 1;
13136
13137   return 0;
13138 }
13139
13140 /* Returns the DIE for decl.  A DIE will always be returned.  */
13141
13142 static dw_die_ref
13143 force_decl_die (tree decl)
13144 {
13145   dw_die_ref decl_die;
13146   unsigned saved_external_flag;
13147   tree save_fn = NULL_TREE;
13148   decl_die = lookup_decl_die (decl);
13149   if (!decl_die)
13150     {
13151       dw_die_ref context_die;
13152       tree decl_context = DECL_CONTEXT (decl);
13153       if (decl_context)
13154         {
13155           /* Find die that represents this context.  */
13156           if (TYPE_P (decl_context))
13157             context_die = force_type_die (decl_context);
13158           else
13159             context_die = force_decl_die (decl_context);
13160         }
13161       else
13162         context_die = comp_unit_die;
13163
13164       decl_die = lookup_decl_die (decl);
13165       if (decl_die)
13166         return decl_die;
13167
13168       switch (TREE_CODE (decl))
13169         {
13170         case FUNCTION_DECL:
13171           /* Clear current_function_decl, so that gen_subprogram_die thinks
13172              that this is a declaration. At this point, we just want to force
13173              declaration die.  */
13174           save_fn = current_function_decl;
13175           current_function_decl = NULL_TREE;
13176           gen_subprogram_die (decl, context_die);
13177           current_function_decl = save_fn;
13178           break;
13179
13180         case VAR_DECL:
13181           /* Set external flag to force declaration die. Restore it after
13182            gen_decl_die() call.  */
13183           saved_external_flag = DECL_EXTERNAL (decl);
13184           DECL_EXTERNAL (decl) = 1;
13185           gen_decl_die (decl, context_die);
13186           DECL_EXTERNAL (decl) = saved_external_flag;
13187           break;
13188
13189         case NAMESPACE_DECL:
13190           dwarf2out_decl (decl);
13191           break;
13192
13193         default:
13194           gcc_unreachable ();
13195         }
13196
13197       /* We should be able to find the DIE now.  */
13198       if (!decl_die)
13199         decl_die = lookup_decl_die (decl);
13200       gcc_assert (decl_die);
13201     }
13202
13203   return decl_die;
13204 }
13205
13206 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13207    always returned.  */
13208
13209 static dw_die_ref
13210 force_type_die (tree type)
13211 {
13212   dw_die_ref type_die;
13213
13214   type_die = lookup_type_die (type);
13215   if (!type_die)
13216     {
13217       dw_die_ref context_die;
13218       if (TYPE_CONTEXT (type))
13219         {
13220           if (TYPE_P (TYPE_CONTEXT (type)))
13221             context_die = force_type_die (TYPE_CONTEXT (type));
13222           else
13223             context_die = force_decl_die (TYPE_CONTEXT (type));
13224         }
13225       else
13226         context_die = comp_unit_die;
13227
13228       type_die = lookup_type_die (type);
13229       if (type_die)
13230         return type_die;
13231       gen_type_die (type, context_die);
13232       type_die = lookup_type_die (type);
13233       gcc_assert (type_die);
13234     }
13235   return type_die;
13236 }
13237
13238 /* Force out any required namespaces to be able to output DECL,
13239    and return the new context_die for it, if it's changed.  */
13240
13241 static dw_die_ref
13242 setup_namespace_context (tree thing, dw_die_ref context_die)
13243 {
13244   tree context = (DECL_P (thing)
13245                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13246   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13247     /* Force out the namespace.  */
13248     context_die = force_decl_die (context);
13249
13250   return context_die;
13251 }
13252
13253 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13254    type) within its namespace, if appropriate.
13255
13256    For compatibility with older debuggers, namespace DIEs only contain
13257    declarations; all definitions are emitted at CU scope.  */
13258
13259 static void
13260 declare_in_namespace (tree thing, dw_die_ref context_die)
13261 {
13262   dw_die_ref ns_context;
13263
13264   if (debug_info_level <= DINFO_LEVEL_TERSE)
13265     return;
13266
13267   /* If this decl is from an inlined function, then don't try to emit it in its
13268      namespace, as we will get confused.  It would have already been emitted
13269      when the abstract instance of the inline function was emitted anyways.  */
13270   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13271     return;
13272
13273   ns_context = setup_namespace_context (thing, context_die);
13274
13275   if (ns_context != context_die)
13276     {
13277       if (DECL_P (thing))
13278         gen_decl_die (thing, ns_context);
13279       else
13280         gen_type_die (thing, ns_context);
13281     }
13282 }
13283
13284 /* Generate a DIE for a namespace or namespace alias.  */
13285
13286 static void
13287 gen_namespace_die (tree decl)
13288 {
13289   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13290
13291   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13292      they are an alias of.  */
13293   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13294     {
13295       /* Output a real namespace.  */
13296       dw_die_ref namespace_die
13297         = new_die (DW_TAG_namespace, context_die, decl);
13298       add_name_and_src_coords_attributes (namespace_die, decl);
13299       equate_decl_number_to_die (decl, namespace_die);
13300     }
13301   else
13302     {
13303       /* Output a namespace alias.  */
13304
13305       /* Force out the namespace we are an alias of, if necessary.  */
13306       dw_die_ref origin_die
13307         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13308
13309       /* Now create the namespace alias DIE.  */
13310       dw_die_ref namespace_die
13311         = new_die (DW_TAG_imported_declaration, context_die, decl);
13312       add_name_and_src_coords_attributes (namespace_die, decl);
13313       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13314       equate_decl_number_to_die (decl, namespace_die);
13315     }
13316 }
13317
13318 /* Generate Dwarf debug information for a decl described by DECL.  */
13319
13320 static void
13321 gen_decl_die (tree decl, dw_die_ref context_die)
13322 {
13323   tree origin;
13324
13325   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13326     return;
13327
13328   switch (TREE_CODE (decl))
13329     {
13330     case ERROR_MARK:
13331       break;
13332
13333     case CONST_DECL:
13334       /* The individual enumerators of an enum type get output when we output
13335          the Dwarf representation of the relevant enum type itself.  */
13336       break;
13337
13338     case FUNCTION_DECL:
13339       /* Don't output any DIEs to represent mere function declarations,
13340          unless they are class members or explicit block externs.  */
13341       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13342           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13343         break;
13344
13345 #if 0
13346       /* FIXME */
13347       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13348          on local redeclarations of global functions.  That seems broken.  */
13349       if (current_function_decl != decl)
13350         /* This is only a declaration.  */;
13351 #endif
13352
13353       /* If we're emitting a clone, emit info for the abstract instance.  */
13354       if (DECL_ORIGIN (decl) != decl)
13355         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13356
13357       /* If we're emitting an out-of-line copy of an inline function,
13358          emit info for the abstract instance and set up to refer to it.  */
13359       else if (cgraph_function_possibly_inlined_p (decl)
13360                && ! DECL_ABSTRACT (decl)
13361                && ! class_or_namespace_scope_p (context_die)
13362                /* dwarf2out_abstract_function won't emit a die if this is just
13363                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13364                   that case, because that works only if we have a die.  */
13365                && DECL_INITIAL (decl) != NULL_TREE)
13366         {
13367           dwarf2out_abstract_function (decl);
13368           set_decl_origin_self (decl);
13369         }
13370
13371       /* Otherwise we're emitting the primary DIE for this decl.  */
13372       else if (debug_info_level > DINFO_LEVEL_TERSE)
13373         {
13374           /* Before we describe the FUNCTION_DECL itself, make sure that we
13375              have described its return type.  */
13376           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13377
13378           /* And its virtual context.  */
13379           if (DECL_VINDEX (decl) != NULL_TREE)
13380             gen_type_die (DECL_CONTEXT (decl), context_die);
13381
13382           /* And its containing type.  */
13383           origin = decl_class_context (decl);
13384           if (origin != NULL_TREE)
13385             gen_type_die_for_member (origin, decl, context_die);
13386
13387           /* And its containing namespace.  */
13388           declare_in_namespace (decl, context_die);
13389         }
13390
13391       /* Now output a DIE to represent the function itself.  */
13392       gen_subprogram_die (decl, context_die);
13393       break;
13394
13395     case TYPE_DECL:
13396       /* If we are in terse mode, don't generate any DIEs to represent any
13397          actual typedefs.  */
13398       if (debug_info_level <= DINFO_LEVEL_TERSE)
13399         break;
13400
13401       /* In the special case of a TYPE_DECL node representing the declaration
13402          of some type tag, if the given TYPE_DECL is marked as having been
13403          instantiated from some other (original) TYPE_DECL node (e.g. one which
13404          was generated within the original definition of an inline function) we
13405          have to generate a special (abbreviated) DW_TAG_structure_type,
13406          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13407       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
13408         {
13409           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13410           break;
13411         }
13412
13413       if (is_redundant_typedef (decl))
13414         gen_type_die (TREE_TYPE (decl), context_die);
13415       else
13416         /* Output a DIE to represent the typedef itself.  */
13417         gen_typedef_die (decl, context_die);
13418       break;
13419
13420     case LABEL_DECL:
13421       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13422         gen_label_die (decl, context_die);
13423       break;
13424
13425     case VAR_DECL:
13426     case RESULT_DECL:
13427       /* If we are in terse mode, don't generate any DIEs to represent any
13428          variable declarations or definitions.  */
13429       if (debug_info_level <= DINFO_LEVEL_TERSE)
13430         break;
13431
13432       /* Output any DIEs that are needed to specify the type of this data
13433          object.  */
13434       gen_type_die (TREE_TYPE (decl), context_die);
13435
13436       /* And its containing type.  */
13437       origin = decl_class_context (decl);
13438       if (origin != NULL_TREE)
13439         gen_type_die_for_member (origin, decl, context_die);
13440
13441       /* And its containing namespace.  */
13442       declare_in_namespace (decl, context_die);
13443
13444       /* Now output the DIE to represent the data object itself.  This gets
13445          complicated because of the possibility that the VAR_DECL really
13446          represents an inlined instance of a formal parameter for an inline
13447          function.  */
13448       origin = decl_ultimate_origin (decl);
13449       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13450         gen_formal_parameter_die (decl, context_die);
13451       else
13452         gen_variable_die (decl, context_die);
13453       break;
13454
13455     case FIELD_DECL:
13456       /* Ignore the nameless fields that are used to skip bits but handle C++
13457          anonymous unions and structs.  */
13458       if (DECL_NAME (decl) != NULL_TREE
13459           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13460           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13461         {
13462           gen_type_die (member_declared_type (decl), context_die);
13463           gen_field_die (decl, context_die);
13464         }
13465       break;
13466
13467     case PARM_DECL:
13468       gen_type_die (TREE_TYPE (decl), context_die);
13469       gen_formal_parameter_die (decl, context_die);
13470       break;
13471
13472     case NAMESPACE_DECL:
13473       gen_namespace_die (decl);
13474       break;
13475
13476     default:
13477       /* Probably some frontend-internal decl.  Assume we don't care.  */
13478       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13479       break;
13480     }
13481 }
13482 \f
13483 /* Output debug information for global decl DECL.  Called from toplev.c after
13484    compilation proper has finished.  */
13485
13486 static void
13487 dwarf2out_global_decl (tree decl)
13488 {
13489   /* Output DWARF2 information for file-scope tentative data object
13490      declarations, file-scope (extern) function declarations (which had no
13491      corresponding body) and file-scope tagged type declarations and
13492      definitions which have not yet been forced out.  */
13493   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
13494     dwarf2out_decl (decl);
13495 }
13496
13497 /* Output debug information for type decl DECL.  Called from toplev.c
13498    and from language front ends (to record built-in types).  */
13499 static void
13500 dwarf2out_type_decl (tree decl, int local)
13501 {
13502   if (!local)
13503     dwarf2out_decl (decl);
13504 }
13505
13506 /* Output debug information for imported module or decl.  */
13507
13508 static void
13509 dwarf2out_imported_module_or_decl (tree decl, tree context)
13510 {
13511   dw_die_ref imported_die, at_import_die;
13512   dw_die_ref scope_die;
13513   expanded_location xloc;
13514
13515   if (debug_info_level <= DINFO_LEVEL_TERSE)
13516     return;
13517
13518   gcc_assert (decl);
13519
13520   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
13521      We need decl DIE for reference and scope die. First, get DIE for the decl
13522      itself.  */
13523
13524   /* Get the scope die for decl context. Use comp_unit_die for global module
13525      or decl. If die is not found for non globals, force new die.  */
13526   if (!context)
13527     scope_die = comp_unit_die;
13528   else if (TYPE_P (context))
13529     {
13530       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
13531         return;
13532     scope_die = force_type_die (context);
13533     }
13534   else
13535     scope_die = force_decl_die (context);
13536
13537   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
13538   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
13539     {
13540       if (is_base_type (TREE_TYPE (decl)))
13541         at_import_die = base_type_die (TREE_TYPE (decl));
13542       else
13543         at_import_die = force_type_die (TREE_TYPE (decl));
13544     }
13545   else
13546     {
13547       at_import_die = lookup_decl_die (decl);
13548       if (!at_import_die)
13549         {
13550           /* If we're trying to avoid duplicate debug info, we may not have
13551              emitted the member decl for this field.  Emit it now.  */
13552           if (TREE_CODE (decl) == FIELD_DECL)
13553             {
13554               tree type = DECL_CONTEXT (decl);
13555               dw_die_ref type_context_die;
13556
13557               if (TYPE_CONTEXT (type))
13558                 if (TYPE_P (TYPE_CONTEXT (type)))
13559                   {
13560                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
13561                                                    DINFO_USAGE_DIR_USE))
13562                       return;
13563                   type_context_die = force_type_die (TYPE_CONTEXT (type));
13564                   }
13565               else
13566                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
13567               else
13568                 type_context_die = comp_unit_die;
13569               gen_type_die_for_member (type, decl, type_context_die);
13570             }
13571           at_import_die = force_decl_die (decl);
13572         }
13573     }
13574
13575   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
13576   if (TREE_CODE (decl) == NAMESPACE_DECL)
13577     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
13578   else
13579     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
13580
13581   xloc = expand_location (input_location);
13582   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
13583   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
13584   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
13585 }
13586
13587 /* Write the debugging output for DECL.  */
13588
13589 void
13590 dwarf2out_decl (tree decl)
13591 {
13592   dw_die_ref context_die = comp_unit_die;
13593
13594   switch (TREE_CODE (decl))
13595     {
13596     case ERROR_MARK:
13597       return;
13598
13599     case FUNCTION_DECL:
13600       /* What we would really like to do here is to filter out all mere
13601          file-scope declarations of file-scope functions which are never
13602          referenced later within this translation unit (and keep all of ones
13603          that *are* referenced later on) but we aren't clairvoyant, so we have
13604          no idea which functions will be referenced in the future (i.e. later
13605          on within the current translation unit). So here we just ignore all
13606          file-scope function declarations which are not also definitions.  If
13607          and when the debugger needs to know something about these functions,
13608          it will have to hunt around and find the DWARF information associated
13609          with the definition of the function.
13610
13611          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
13612          nodes represent definitions and which ones represent mere
13613          declarations.  We have to check DECL_INITIAL instead. That's because
13614          the C front-end supports some weird semantics for "extern inline"
13615          function definitions.  These can get inlined within the current
13616          translation unit (and thus, we need to generate Dwarf info for their
13617          abstract instances so that the Dwarf info for the concrete inlined
13618          instances can have something to refer to) but the compiler never
13619          generates any out-of-lines instances of such things (despite the fact
13620          that they *are* definitions).
13621
13622          The important point is that the C front-end marks these "extern
13623          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
13624          them anyway. Note that the C++ front-end also plays some similar games
13625          for inline function definitions appearing within include files which
13626          also contain `#pragma interface' pragmas.  */
13627       if (DECL_INITIAL (decl) == NULL_TREE)
13628         return;
13629
13630       /* If we're a nested function, initially use a parent of NULL; if we're
13631          a plain function, this will be fixed up in decls_for_scope.  If
13632          we're a method, it will be ignored, since we already have a DIE.  */
13633       if (decl_function_context (decl)
13634           /* But if we're in terse mode, we don't care about scope.  */
13635           && debug_info_level > DINFO_LEVEL_TERSE)
13636         context_die = NULL;
13637       break;
13638
13639     case VAR_DECL:
13640       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
13641          declaration and if the declaration was never even referenced from
13642          within this entire compilation unit.  We suppress these DIEs in
13643          order to save space in the .debug section (by eliminating entries
13644          which are probably useless).  Note that we must not suppress
13645          block-local extern declarations (whether used or not) because that
13646          would screw-up the debugger's name lookup mechanism and cause it to
13647          miss things which really ought to be in scope at a given point.  */
13648       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
13649         return;
13650
13651       /* For local statics lookup proper context die.  */
13652       if (TREE_STATIC (decl) && decl_function_context (decl))
13653         context_die = lookup_decl_die (DECL_CONTEXT (decl));
13654
13655       /* If we are in terse mode, don't generate any DIEs to represent any
13656          variable declarations or definitions.  */
13657       if (debug_info_level <= DINFO_LEVEL_TERSE)
13658         return;
13659       break;
13660
13661     case NAMESPACE_DECL:
13662       if (debug_info_level <= DINFO_LEVEL_TERSE)
13663         return;
13664       if (lookup_decl_die (decl) != NULL)
13665         return;
13666       break;
13667
13668     case TYPE_DECL:
13669       /* Don't emit stubs for types unless they are needed by other DIEs.  */
13670       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
13671         return;
13672
13673       /* Don't bother trying to generate any DIEs to represent any of the
13674          normal built-in types for the language we are compiling.  */
13675       if (DECL_IS_BUILTIN (decl))
13676         {
13677           /* OK, we need to generate one for `bool' so GDB knows what type
13678              comparisons have.  */
13679           if (is_cxx ()
13680               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
13681               && ! DECL_IGNORED_P (decl))
13682             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
13683
13684           return;
13685         }
13686
13687       /* If we are in terse mode, don't generate any DIEs for types.  */
13688       if (debug_info_level <= DINFO_LEVEL_TERSE)
13689         return;
13690
13691       /* If we're a function-scope tag, initially use a parent of NULL;
13692          this will be fixed up in decls_for_scope.  */
13693       if (decl_function_context (decl))
13694         context_die = NULL;
13695
13696       break;
13697
13698     default:
13699       return;
13700     }
13701
13702   gen_decl_die (decl, context_die);
13703 }
13704
13705 /* Output a marker (i.e. a label) for the beginning of the generated code for
13706    a lexical block.  */
13707
13708 static void
13709 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
13710                        unsigned int blocknum)
13711 {
13712   switch_to_section (current_function_section ());
13713   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
13714 }
13715
13716 /* Output a marker (i.e. a label) for the end of the generated code for a
13717    lexical block.  */
13718
13719 static void
13720 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
13721 {
13722   switch_to_section (current_function_section ());
13723   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
13724 }
13725
13726 /* Returns nonzero if it is appropriate not to emit any debugging
13727    information for BLOCK, because it doesn't contain any instructions.
13728
13729    Don't allow this for blocks with nested functions or local classes
13730    as we would end up with orphans, and in the presence of scheduling
13731    we may end up calling them anyway.  */
13732
13733 static bool
13734 dwarf2out_ignore_block (tree block)
13735 {
13736   tree decl;
13737
13738   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
13739     if (TREE_CODE (decl) == FUNCTION_DECL
13740         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
13741       return 0;
13742
13743   return 1;
13744 }
13745
13746 /* Hash table routines for file_hash.  */
13747
13748 static int
13749 file_table_eq (const void *p1_p, const void *p2_p)
13750 {
13751   const struct dwarf_file_data * p1 = p1_p;
13752   const char * p2 = p2_p;
13753   return strcmp (p1->filename, p2) == 0;
13754 }
13755
13756 static hashval_t
13757 file_table_hash (const void *p_p)
13758 {
13759   const struct dwarf_file_data * p = p_p;
13760   return htab_hash_string (p->filename);
13761 }
13762
13763 /* Lookup FILE_NAME (in the list of filenames that we know about here in
13764    dwarf2out.c) and return its "index".  The index of each (known) filename is
13765    just a unique number which is associated with only that one filename.  We
13766    need such numbers for the sake of generating labels (in the .debug_sfnames
13767    section) and references to those files numbers (in the .debug_srcinfo
13768    and.debug_macinfo sections).  If the filename given as an argument is not
13769    found in our current list, add it to the list and assign it the next
13770    available unique index number.  In order to speed up searches, we remember
13771    the index of the filename was looked up last.  This handles the majority of
13772    all searches.  */
13773
13774 static struct dwarf_file_data *
13775 lookup_filename (const char *file_name)
13776 {
13777   void ** slot;
13778   struct dwarf_file_data * created;
13779
13780   /* Check to see if the file name that was searched on the previous
13781      call matches this file name.  If so, return the index.  */
13782   if (file_table_last_lookup
13783       && (file_name == file_table_last_lookup->filename
13784           || strcmp (file_table_last_lookup->filename, file_name) == 0))
13785     return file_table_last_lookup;
13786
13787   /* Didn't match the previous lookup, search the table.  */
13788   slot = htab_find_slot_with_hash (file_table, file_name,
13789                                    htab_hash_string (file_name), INSERT);
13790   if (*slot)
13791     return *slot;
13792
13793   created = ggc_alloc (sizeof (struct dwarf_file_data));
13794   created->filename = file_name;
13795   created->emitted_number = 0;
13796   *slot = created;
13797   return created;
13798 }
13799
13800 /* If the assembler will construct the file table, then translate the compiler
13801    internal file table number into the assembler file table number, and emit
13802    a .file directive if we haven't already emitted one yet.  The file table
13803    numbers are different because we prune debug info for unused variables and
13804    types, which may include filenames.  */
13805
13806 static int
13807 maybe_emit_file (struct dwarf_file_data * fd)
13808 {
13809   if (! fd->emitted_number)
13810     {
13811       if (last_emitted_file)
13812         fd->emitted_number = last_emitted_file->emitted_number + 1;
13813       else
13814         fd->emitted_number = 1;
13815       last_emitted_file = fd;
13816
13817       if (DWARF2_ASM_LINE_DEBUG_INFO)
13818         {
13819           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
13820           output_quoted_string (asm_out_file, fd->filename);
13821           fputc ('\n', asm_out_file);
13822         }
13823     }
13824
13825   return fd->emitted_number;
13826 }
13827
13828 /* Called by the final INSN scan whenever we see a var location.  We
13829    use it to drop labels in the right places, and throw the location in
13830    our lookup table.  */
13831
13832 static void
13833 dwarf2out_var_location (rtx loc_note)
13834 {
13835   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
13836   struct var_loc_node *newloc;
13837   rtx prev_insn;
13838   static rtx last_insn;
13839   static const char *last_label;
13840   tree decl;
13841
13842   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
13843     return;
13844   prev_insn = PREV_INSN (loc_note);
13845
13846   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
13847   /* If the insn we processed last time is the previous insn
13848      and it is also a var location note, use the label we emitted
13849      last time.  */
13850   if (last_insn != NULL_RTX
13851       && last_insn == prev_insn
13852       && NOTE_P (prev_insn)
13853       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
13854     {
13855       newloc->label = last_label;
13856     }
13857   else
13858     {
13859       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
13860       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
13861       loclabel_num++;
13862       newloc->label = ggc_strdup (loclabel);
13863     }
13864   newloc->var_loc_note = loc_note;
13865   newloc->next = NULL;
13866
13867   if (cfun && in_cold_section_p)
13868     newloc->section_label = cfun->cold_section_label;
13869   else
13870     newloc->section_label = text_section_label;
13871
13872   last_insn = loc_note;
13873   last_label = newloc->label;
13874   decl = NOTE_VAR_LOCATION_DECL (loc_note);
13875   add_var_loc_to_decl (decl, newloc);
13876 }
13877
13878 /* We need to reset the locations at the beginning of each
13879    function. We can't do this in the end_function hook, because the
13880    declarations that use the locations won't have been output when
13881    that hook is called.  Also compute have_multiple_function_sections here.  */
13882
13883 static void
13884 dwarf2out_begin_function (tree fun)
13885 {
13886   htab_empty (decl_loc_table);
13887
13888   if (function_section (fun) != text_section)
13889     have_multiple_function_sections = true;
13890 }
13891
13892 /* Output a label to mark the beginning of a source code line entry
13893    and record information relating to this source line, in
13894    'line_info_table' for later output of the .debug_line section.  */
13895
13896 static void
13897 dwarf2out_source_line (unsigned int line, const char *filename)
13898 {
13899   if (debug_info_level >= DINFO_LEVEL_NORMAL
13900       && line != 0)
13901     {
13902       int file_num = maybe_emit_file (lookup_filename (filename));
13903
13904       switch_to_section (current_function_section ());
13905
13906       /* If requested, emit something human-readable.  */
13907       if (flag_debug_asm)
13908         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
13909                  filename, line);
13910
13911       if (DWARF2_ASM_LINE_DEBUG_INFO)
13912         {
13913           /* Emit the .loc directive understood by GNU as.  */
13914           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
13915
13916           /* Indicate that line number info exists.  */
13917           line_info_table_in_use++;
13918         }
13919       else if (function_section (current_function_decl) != text_section)
13920         {
13921           dw_separate_line_info_ref line_info;
13922           targetm.asm_out.internal_label (asm_out_file,
13923                                           SEPARATE_LINE_CODE_LABEL,
13924                                           separate_line_info_table_in_use);
13925
13926           /* Expand the line info table if necessary.  */
13927           if (separate_line_info_table_in_use
13928               == separate_line_info_table_allocated)
13929             {
13930               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13931               separate_line_info_table
13932                 = ggc_realloc (separate_line_info_table,
13933                                separate_line_info_table_allocated
13934                                * sizeof (dw_separate_line_info_entry));
13935               memset (separate_line_info_table
13936                        + separate_line_info_table_in_use,
13937                       0,
13938                       (LINE_INFO_TABLE_INCREMENT
13939                        * sizeof (dw_separate_line_info_entry)));
13940             }
13941
13942           /* Add the new entry at the end of the line_info_table.  */
13943           line_info
13944             = &separate_line_info_table[separate_line_info_table_in_use++];
13945           line_info->dw_file_num = file_num;
13946           line_info->dw_line_num = line;
13947           line_info->function = current_function_funcdef_no;
13948         }
13949       else
13950         {
13951           dw_line_info_ref line_info;
13952
13953           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
13954                                      line_info_table_in_use);
13955
13956           /* Expand the line info table if necessary.  */
13957           if (line_info_table_in_use == line_info_table_allocated)
13958             {
13959               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
13960               line_info_table
13961                 = ggc_realloc (line_info_table,
13962                                (line_info_table_allocated
13963                                 * sizeof (dw_line_info_entry)));
13964               memset (line_info_table + line_info_table_in_use, 0,
13965                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
13966             }
13967
13968           /* Add the new entry at the end of the line_info_table.  */
13969           line_info = &line_info_table[line_info_table_in_use++];
13970           line_info->dw_file_num = file_num;
13971           line_info->dw_line_num = line;
13972         }
13973     }
13974 }
13975
13976 /* Record the beginning of a new source file.  */
13977
13978 static void
13979 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
13980 {
13981   if (flag_eliminate_dwarf2_dups)
13982     {
13983       /* Record the beginning of the file for break_out_includes.  */
13984       dw_die_ref bincl_die;
13985
13986       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
13987       add_AT_string (bincl_die, DW_AT_name, filename);
13988     }
13989
13990   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
13991     {
13992       int file_num = maybe_emit_file (lookup_filename (filename));
13993
13994       switch_to_section (debug_macinfo_section);
13995       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
13996       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
13997                                    lineno);
13998
13999       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14000     }
14001 }
14002
14003 /* Record the end of a source file.  */
14004
14005 static void
14006 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14007 {
14008   if (flag_eliminate_dwarf2_dups)
14009     /* Record the end of the file for break_out_includes.  */
14010     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14011
14012   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14013     {
14014       switch_to_section (debug_macinfo_section);
14015       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14016     }
14017 }
14018
14019 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14020    the tail part of the directive line, i.e. the part which is past the
14021    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14022
14023 static void
14024 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14025                   const char *buffer ATTRIBUTE_UNUSED)
14026 {
14027   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14028     {
14029       switch_to_section (debug_macinfo_section);
14030       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14031       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14032       dw2_asm_output_nstring (buffer, -1, "The macro");
14033     }
14034 }
14035
14036 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14037    the tail part of the directive line, i.e. the part which is past the
14038    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14039
14040 static void
14041 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14042                  const char *buffer ATTRIBUTE_UNUSED)
14043 {
14044   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14045     {
14046       switch_to_section (debug_macinfo_section);
14047       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14048       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14049       dw2_asm_output_nstring (buffer, -1, "The macro");
14050     }
14051 }
14052
14053 /* Set up for Dwarf output at the start of compilation.  */
14054
14055 static void
14056 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14057 {
14058   /* Allocate the file_table.  */
14059   file_table = htab_create_ggc (50, file_table_hash,
14060                                 file_table_eq, NULL);
14061
14062   /* Allocate the decl_die_table.  */
14063   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14064                                     decl_die_table_eq, NULL);
14065
14066   /* Allocate the decl_loc_table.  */
14067   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14068                                     decl_loc_table_eq, NULL);
14069
14070   /* Allocate the initial hunk of the decl_scope_table.  */
14071   decl_scope_table = VEC_alloc (tree, gc, 256);
14072
14073   /* Allocate the initial hunk of the abbrev_die_table.  */
14074   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14075                                         * sizeof (dw_die_ref));
14076   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14077   /* Zero-th entry is allocated, but unused.  */
14078   abbrev_die_table_in_use = 1;
14079
14080   /* Allocate the initial hunk of the line_info_table.  */
14081   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14082                                        * sizeof (dw_line_info_entry));
14083   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14084
14085   /* Zero-th entry is allocated, but unused.  */
14086   line_info_table_in_use = 1;
14087
14088   /* Allocate the pubtypes and pubnames vectors.  */
14089   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14090   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14091
14092   /* Generate the initial DIE for the .debug section.  Note that the (string)
14093      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14094      will (typically) be a relative pathname and that this pathname should be
14095      taken as being relative to the directory from which the compiler was
14096      invoked when the given (base) source file was compiled.  We will fill
14097      in this value in dwarf2out_finish.  */
14098   comp_unit_die = gen_compile_unit_die (NULL);
14099
14100   incomplete_types = VEC_alloc (tree, gc, 64);
14101
14102   used_rtx_array = VEC_alloc (rtx, gc, 32);
14103
14104   debug_info_section = get_section (DEBUG_INFO_SECTION,
14105                                     SECTION_DEBUG, NULL);
14106   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14107                                       SECTION_DEBUG, NULL);
14108   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14109                                        SECTION_DEBUG, NULL);
14110   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14111                                        SECTION_DEBUG, NULL);
14112   debug_line_section = get_section (DEBUG_LINE_SECTION,
14113                                     SECTION_DEBUG, NULL);
14114   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14115                                    SECTION_DEBUG, NULL);
14116   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14117                                         SECTION_DEBUG, NULL);
14118 #ifdef DEBUG_PUBTYPES_SECTION
14119   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14120                                         SECTION_DEBUG, NULL);
14121 #endif
14122   debug_str_section = get_section (DEBUG_STR_SECTION,
14123                                    DEBUG_STR_SECTION_FLAGS, NULL);
14124   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14125                                       SECTION_DEBUG, NULL);
14126   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14127                                      SECTION_DEBUG, NULL);
14128
14129   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14130   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14131                                DEBUG_ABBREV_SECTION_LABEL, 0);
14132   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14133   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14134                                COLD_TEXT_SECTION_LABEL, 0);
14135   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14136
14137   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14138                                DEBUG_INFO_SECTION_LABEL, 0);
14139   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14140                                DEBUG_LINE_SECTION_LABEL, 0);
14141   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14142                                DEBUG_RANGES_SECTION_LABEL, 0);
14143   switch_to_section (debug_abbrev_section);
14144   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14145   switch_to_section (debug_info_section);
14146   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14147   switch_to_section (debug_line_section);
14148   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14149
14150   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14151     {
14152       switch_to_section (debug_macinfo_section);
14153       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14154                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14155       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14156     }
14157
14158   switch_to_section (text_section);
14159   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14160   if (flag_reorder_blocks_and_partition)
14161     {
14162       switch_to_section (unlikely_text_section ());
14163       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14164     }
14165 }
14166
14167 /* A helper function for dwarf2out_finish called through
14168    ht_forall.  Emit one queued .debug_str string.  */
14169
14170 static int
14171 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14172 {
14173   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14174
14175   if (node->form == DW_FORM_strp)
14176     {
14177       switch_to_section (debug_str_section);
14178       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14179       assemble_string (node->str, strlen (node->str) + 1);
14180     }
14181
14182   return 1;
14183 }
14184
14185 #if ENABLE_ASSERT_CHECKING
14186 /* Verify that all marks are clear.  */
14187
14188 static void
14189 verify_marks_clear (dw_die_ref die)
14190 {
14191   dw_die_ref c;
14192
14193   gcc_assert (! die->die_mark);
14194   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14195 }
14196 #endif /* ENABLE_ASSERT_CHECKING */
14197
14198 /* Clear the marks for a die and its children.
14199    Be cool if the mark isn't set.  */
14200
14201 static void
14202 prune_unmark_dies (dw_die_ref die)
14203 {
14204   dw_die_ref c;
14205
14206   if (die->die_mark)
14207     die->die_mark = 0;
14208   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14209 }
14210
14211 /* Given DIE that we're marking as used, find any other dies
14212    it references as attributes and mark them as used.  */
14213
14214 static void
14215 prune_unused_types_walk_attribs (dw_die_ref die)
14216 {
14217   dw_attr_ref a;
14218   unsigned ix;
14219
14220   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14221     {
14222       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14223         {
14224           /* A reference to another DIE.
14225              Make sure that it will get emitted.  */
14226           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14227         }
14228       /* Set the string's refcount to 0 so that prune_unused_types_mark
14229          accounts properly for it.  */
14230       if (AT_class (a) == dw_val_class_str)
14231         a->dw_attr_val.v.val_str->refcount = 0;
14232     }
14233 }
14234
14235
14236 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14237    to DIE's children.  */
14238
14239 static void
14240 prune_unused_types_mark (dw_die_ref die, int dokids)
14241 {
14242   dw_die_ref c;
14243
14244   if (die->die_mark == 0)
14245     {
14246       /* We haven't done this node yet.  Mark it as used.  */
14247       die->die_mark = 1;
14248
14249       /* We also have to mark its parents as used.
14250          (But we don't want to mark our parents' kids due to this.)  */
14251       if (die->die_parent)
14252         prune_unused_types_mark (die->die_parent, 0);
14253
14254       /* Mark any referenced nodes.  */
14255       prune_unused_types_walk_attribs (die);
14256
14257       /* If this node is a specification,
14258          also mark the definition, if it exists.  */
14259       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14260         prune_unused_types_mark (die->die_definition, 1);
14261     }
14262
14263   if (dokids && die->die_mark != 2)
14264     {
14265       /* We need to walk the children, but haven't done so yet.
14266          Remember that we've walked the kids.  */
14267       die->die_mark = 2;
14268
14269       /* If this is an array type, we need to make sure our
14270          kids get marked, even if they're types.  */
14271       if (die->die_tag == DW_TAG_array_type)
14272         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14273       else
14274         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14275     }
14276 }
14277
14278
14279 /* Walk the tree DIE and mark types that we actually use.  */
14280
14281 static void
14282 prune_unused_types_walk (dw_die_ref die)
14283 {
14284   dw_die_ref c;
14285
14286   /* Don't do anything if this node is already marked.  */
14287   if (die->die_mark)
14288     return;
14289
14290   switch (die->die_tag)
14291     {
14292     case DW_TAG_const_type:
14293     case DW_TAG_packed_type:
14294     case DW_TAG_pointer_type:
14295     case DW_TAG_reference_type:
14296     case DW_TAG_volatile_type:
14297     case DW_TAG_typedef:
14298     case DW_TAG_array_type:
14299     case DW_TAG_structure_type:
14300     case DW_TAG_union_type:
14301     case DW_TAG_class_type:
14302     case DW_TAG_friend:
14303     case DW_TAG_variant_part:
14304     case DW_TAG_enumeration_type:
14305     case DW_TAG_subroutine_type:
14306     case DW_TAG_string_type:
14307     case DW_TAG_set_type:
14308     case DW_TAG_subrange_type:
14309     case DW_TAG_ptr_to_member_type:
14310     case DW_TAG_file_type:
14311       if (die->die_perennial_p)
14312         break;
14313
14314       /* It's a type node --- don't mark it.  */
14315       return;
14316
14317     default:
14318       /* Mark everything else.  */
14319       break;
14320   }
14321
14322   die->die_mark = 1;
14323
14324   /* Now, mark any dies referenced from here.  */
14325   prune_unused_types_walk_attribs (die);
14326
14327   /* Mark children.  */
14328   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14329 }
14330
14331 /* Increment the string counts on strings referred to from DIE's
14332    attributes.  */
14333
14334 static void
14335 prune_unused_types_update_strings (dw_die_ref die)
14336 {
14337   dw_attr_ref a;
14338   unsigned ix;
14339
14340   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14341     if (AT_class (a) == dw_val_class_str)
14342       {
14343         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14344         s->refcount++;
14345         /* Avoid unnecessarily putting strings that are used less than
14346            twice in the hash table.  */
14347         if (s->refcount
14348             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14349           {
14350             void ** slot;
14351             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14352                                              htab_hash_string (s->str),
14353                                              INSERT);
14354             gcc_assert (*slot == NULL);
14355             *slot = s;
14356           }
14357       }
14358 }
14359
14360 /* Remove from the tree DIE any dies that aren't marked.  */
14361
14362 static void
14363 prune_unused_types_prune (dw_die_ref die)
14364 {
14365   dw_die_ref c;
14366
14367   gcc_assert (die->die_mark);
14368   prune_unused_types_update_strings (die);
14369
14370   if (! die->die_child)
14371     return;
14372
14373   c = die->die_child;
14374   do {
14375     dw_die_ref prev = c;
14376     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14377       if (c == die->die_child)
14378         {
14379           /* No marked children between 'prev' and the end of the list.  */
14380           if (prev == c)
14381             /* No marked children at all.  */
14382             die->die_child = NULL;
14383           else
14384             {
14385               prev->die_sib = c->die_sib;
14386               die->die_child = prev;
14387             }
14388           return;
14389         }
14390
14391     if (c != prev->die_sib)
14392       prev->die_sib = c;
14393     prune_unused_types_prune (c);
14394   } while (c != die->die_child);
14395 }
14396
14397
14398 /* Remove dies representing declarations that we never use.  */
14399
14400 static void
14401 prune_unused_types (void)
14402 {
14403   unsigned int i;
14404   limbo_die_node *node;
14405   pubname_ref pub;
14406
14407 #if ENABLE_ASSERT_CHECKING
14408   /* All the marks should already be clear.  */
14409   verify_marks_clear (comp_unit_die);
14410   for (node = limbo_die_list; node; node = node->next)
14411     verify_marks_clear (node->die);
14412 #endif /* ENABLE_ASSERT_CHECKING */
14413
14414   /* Set the mark on nodes that are actually used.  */
14415   prune_unused_types_walk (comp_unit_die);
14416   for (node = limbo_die_list; node; node = node->next)
14417     prune_unused_types_walk (node->die);
14418
14419   /* Also set the mark on nodes referenced from the
14420      pubname_table or arange_table.  */
14421   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14422     prune_unused_types_mark (pub->die, 1);
14423   for (i = 0; i < arange_table_in_use; i++)
14424     prune_unused_types_mark (arange_table[i], 1);
14425
14426   /* Get rid of nodes that aren't marked; and update the string counts.  */
14427   if (debug_str_hash)
14428     htab_empty (debug_str_hash);
14429   prune_unused_types_prune (comp_unit_die);
14430   for (node = limbo_die_list; node; node = node->next)
14431     prune_unused_types_prune (node->die);
14432
14433   /* Leave the marks clear.  */
14434   prune_unmark_dies (comp_unit_die);
14435   for (node = limbo_die_list; node; node = node->next)
14436     prune_unmark_dies (node->die);
14437 }
14438
14439 /* Set the parameter to true if there are any relative pathnames in
14440    the file table.  */
14441 static int
14442 file_table_relative_p (void ** slot, void *param)
14443 {
14444   bool *p = param;
14445   struct dwarf_file_data *d = *slot;
14446   if (d->emitted_number && !IS_ABSOLUTE_PATH (d->filename))
14447     {
14448       *p = true;
14449       return 0;
14450     }
14451   return 1;
14452 }
14453
14454 /* Output stuff that dwarf requires at the end of every file,
14455    and generate the DWARF-2 debugging info.  */
14456
14457 static void
14458 dwarf2out_finish (const char *filename)
14459 {
14460   limbo_die_node *node, *next_node;
14461   dw_die_ref die = 0;
14462
14463   /* Add the name for the main input file now.  We delayed this from
14464      dwarf2out_init to avoid complications with PCH.  */
14465   add_name_attribute (comp_unit_die, filename);
14466   if (!IS_ABSOLUTE_PATH (filename))
14467     add_comp_dir_attribute (comp_unit_die);
14468   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14469     {
14470       bool p = false;
14471       htab_traverse (file_table, file_table_relative_p, &p);
14472       if (p)
14473         add_comp_dir_attribute (comp_unit_die);
14474     }
14475
14476   /* Traverse the limbo die list, and add parent/child links.  The only
14477      dies without parents that should be here are concrete instances of
14478      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
14479      For concrete instances, we can get the parent die from the abstract
14480      instance.  */
14481   for (node = limbo_die_list; node; node = next_node)
14482     {
14483       next_node = node->next;
14484       die = node->die;
14485
14486       if (die->die_parent == NULL)
14487         {
14488           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
14489
14490           if (origin)
14491             add_child_die (origin->die_parent, die);
14492           else if (die == comp_unit_die)
14493             ;
14494           else if (errorcount > 0 || sorrycount > 0)
14495             /* It's OK to be confused by errors in the input.  */
14496             add_child_die (comp_unit_die, die);
14497           else
14498             {
14499               /* In certain situations, the lexical block containing a
14500                  nested function can be optimized away, which results
14501                  in the nested function die being orphaned.  Likewise
14502                  with the return type of that nested function.  Force
14503                  this to be a child of the containing function.
14504
14505                  It may happen that even the containing function got fully
14506                  inlined and optimized out.  In that case we are lost and
14507                  assign the empty child.  This should not be big issue as
14508                  the function is likely unreachable too.  */
14509               tree context = NULL_TREE;
14510
14511               gcc_assert (node->created_for);
14512
14513               if (DECL_P (node->created_for))
14514                 context = DECL_CONTEXT (node->created_for);
14515               else if (TYPE_P (node->created_for))
14516                 context = TYPE_CONTEXT (node->created_for);
14517
14518               gcc_assert (context
14519                           && (TREE_CODE (context) == FUNCTION_DECL
14520                               || TREE_CODE (context) == NAMESPACE_DECL));
14521
14522               origin = lookup_decl_die (context);
14523               if (origin)
14524                 add_child_die (origin, die);
14525               else
14526                 add_child_die (comp_unit_die, die);
14527             }
14528         }
14529     }
14530
14531   limbo_die_list = NULL;
14532
14533   /* Walk through the list of incomplete types again, trying once more to
14534      emit full debugging info for them.  */
14535   retry_incomplete_types ();
14536
14537   if (flag_eliminate_unused_debug_types)
14538     prune_unused_types ();
14539
14540   /* Generate separate CUs for each of the include files we've seen.
14541      They will go into limbo_die_list.  */
14542   if (flag_eliminate_dwarf2_dups)
14543     break_out_includes (comp_unit_die);
14544
14545   /* Traverse the DIE's and add add sibling attributes to those DIE's
14546      that have children.  */
14547   add_sibling_attributes (comp_unit_die);
14548   for (node = limbo_die_list; node; node = node->next)
14549     add_sibling_attributes (node->die);
14550
14551   /* Output a terminator label for the .text section.  */
14552   switch_to_section (text_section);
14553   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
14554   if (flag_reorder_blocks_and_partition)
14555     {
14556       switch_to_section (unlikely_text_section ());
14557       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
14558     }
14559
14560   /* We can only use the low/high_pc attributes if all of the code was
14561      in .text.  */
14562   if (!have_multiple_function_sections)
14563     {
14564       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
14565       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
14566     }
14567
14568   /* If it wasn't, we need to give .debug_loc and .debug_ranges an appropriate
14569      "base address".  Use zero so that these addresses become absolute.  */
14570   else if (have_location_lists || ranges_table_in_use)
14571     add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
14572
14573   /* Output location list section if necessary.  */
14574   if (have_location_lists)
14575     {
14576       /* Output the location lists info.  */
14577       switch_to_section (debug_loc_section);
14578       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
14579                                    DEBUG_LOC_SECTION_LABEL, 0);
14580       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
14581       output_location_lists (die);
14582     }
14583
14584   if (debug_info_level >= DINFO_LEVEL_NORMAL)
14585     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
14586                     debug_line_section_label);
14587
14588   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14589     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
14590
14591   /* Output all of the compilation units.  We put the main one last so that
14592      the offsets are available to output_pubnames.  */
14593   for (node = limbo_die_list; node; node = node->next)
14594     output_comp_unit (node->die, 0);
14595
14596   output_comp_unit (comp_unit_die, 0);
14597
14598   /* Output the abbreviation table.  */
14599   switch_to_section (debug_abbrev_section);
14600   output_abbrev_section ();
14601
14602   /* Output public names table if necessary.  */
14603   if (!VEC_empty (pubname_entry, pubname_table))
14604     {
14605       switch_to_section (debug_pubnames_section);
14606       output_pubnames (pubname_table);
14607     }
14608
14609 #ifdef DEBUG_PUBTYPES_SECTION
14610   /* Output public types table if necessary.  */
14611   if (!VEC_empty (pubname_entry, pubtype_table))
14612     {
14613       switch_to_section (debug_pubtypes_section);
14614       output_pubnames (pubtype_table);
14615     }
14616 #endif
14617
14618   /* Output the address range information.  We only put functions in the arange
14619      table, so don't write it out if we don't have any.  */
14620   if (fde_table_in_use)
14621     {
14622       switch_to_section (debug_aranges_section);
14623       output_aranges ();
14624     }
14625
14626   /* Output ranges section if necessary.  */
14627   if (ranges_table_in_use)
14628     {
14629       switch_to_section (debug_ranges_section);
14630       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
14631       output_ranges ();
14632     }
14633
14634   /* Output the source line correspondence table.  We must do this
14635      even if there is no line information.  Otherwise, on an empty
14636      translation unit, we will generate a present, but empty,
14637      .debug_info section.  IRIX 6.5 `nm' will then complain when
14638      examining the file.  This is done late so that any filenames
14639      used by the debug_info section are marked as 'used'.  */
14640   if (! DWARF2_ASM_LINE_DEBUG_INFO)
14641     {
14642       switch_to_section (debug_line_section);
14643       output_line_info ();
14644     }
14645
14646   /* Have to end the macro section.  */
14647   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14648     {
14649       switch_to_section (debug_macinfo_section);
14650       dw2_asm_output_data (1, 0, "End compilation unit");
14651     }
14652
14653   /* If we emitted any DW_FORM_strp form attribute, output the string
14654      table too.  */
14655   if (debug_str_hash)
14656     htab_traverse (debug_str_hash, output_indirect_string, NULL);
14657 }
14658 #else
14659
14660 /* This should never be used, but its address is needed for comparisons.  */
14661 const struct gcc_debug_hooks dwarf2_debug_hooks;
14662
14663 #endif /* DWARF2_DEBUGGING_INFO */
14664
14665 #include "gt-dwarf2out.h"