OSDN Git Service

Fix oversight.
[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 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 #  define DWARF2_FRAME_INFO \
100   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 #  define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107    collected using DWARF_FRAME_REGNUM to those that should be output in
108    .debug_frame and .eh_frame.  */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Decide whether we want to emit frame unwind information for the current
114    translation unit.  */
115
116 int
117 dwarf2out_do_frame (void)
118 {
119   /* We want to emit correct CFA location expressions or lists, so we
120      have to return true if we're going to output debug info, even if
121      we're not going to output frame or unwind info.  */
122   return (write_symbols == DWARF2_DEBUG
123           || write_symbols == VMS_AND_DWARF2_DEBUG
124           || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126           || (DWARF2_UNWIND_INFO
127               && (flag_unwind_tables
128                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
130           );
131 }
132
133 /* The size of the target's pointer type.  */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
137
138 /* Array of RTXes referenced by the debugging information, which therefore
139    must be kept around forever.  */
140 static GTY(()) VEC(rtx,gc) *used_rtx_array;
141
142 /* A pointer to the base of a list of incomplete types which might be
143    completed at some later time.  incomplete_types_list needs to be a
144    VEC(tree,gc) because we want to tell the garbage collector about
145    it.  */
146 static GTY(()) VEC(tree,gc) *incomplete_types;
147
148 /* A pointer to the base of a table of references to declaration
149    scopes.  This table is a display which tracks the nesting
150    of declaration scopes at the current scope and containing
151    scopes.  This table is used to find the proper place to
152    define type declaration DIE's.  */
153 static GTY(()) VEC(tree,gc) *decl_scope_table;
154
155 /* Pointers to various DWARF2 sections.  */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_ranges_section;
166 static GTY(()) section *debug_frame_section;
167
168 /* How to start an assembler comment.  */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
172
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
176
177 /* Call frames are described using a sequence of Call Frame
178    Information instructions.  The register number, offset
179    and address fields are provided as possible operands;
180    their use is selected by the opcode field.  */
181
182 enum dw_cfi_oprnd_type {
183   dw_cfi_oprnd_unused,
184   dw_cfi_oprnd_reg_num,
185   dw_cfi_oprnd_offset,
186   dw_cfi_oprnd_addr,
187   dw_cfi_oprnd_loc
188 };
189
190 typedef union dw_cfi_oprnd_struct GTY(())
191 {
192   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
193   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
194   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
196 }
197 dw_cfi_oprnd;
198
199 typedef struct dw_cfi_struct GTY(())
200 {
201   dw_cfi_ref dw_cfi_next;
202   enum dwarf_call_frame_info dw_cfi_opc;
203   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
204     dw_cfi_oprnd1;
205   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
206     dw_cfi_oprnd2;
207 }
208 dw_cfi_node;
209
210 /* This is how we define the location of the CFA. We use to handle it
211    as REG + OFFSET all the time,  but now it can be more complex.
212    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
213    Instead of passing around REG and OFFSET, we pass a copy
214    of this structure.  */
215 typedef struct cfa_loc GTY(())
216 {
217   HOST_WIDE_INT offset;
218   HOST_WIDE_INT base_offset;
219   unsigned int reg;
220   int indirect;            /* 1 if CFA is accessed via a dereference.  */
221 } dw_cfa_location;
222
223 /* All call frame descriptions (FDE's) in the GCC generated DWARF
224    refer to a single Common Information Entry (CIE), defined at
225    the beginning of the .debug_frame section.  This use of a single
226    CIE obviates the need to keep track of multiple CIE's
227    in the DWARF generation routines below.  */
228
229 typedef struct dw_fde_struct GTY(())
230 {
231   tree decl;
232   const char *dw_fde_begin;
233   const char *dw_fde_current_label;
234   const char *dw_fde_end;
235   const char *dw_fde_hot_section_label;
236   const char *dw_fde_hot_section_end_label;
237   const char *dw_fde_unlikely_section_label;
238   const char *dw_fde_unlikely_section_end_label;
239   bool dw_fde_switched_sections;
240   dw_cfi_ref dw_fde_cfi;
241   unsigned funcdef_number;
242   unsigned all_throwers_are_sibcalls : 1;
243   unsigned nothrow : 1;
244   unsigned uses_eh_lsda : 1;
245 }
246 dw_fde_node;
247
248 /* Maximum size (in bytes) of an artificially generated label.  */
249 #define MAX_ARTIFICIAL_LABEL_BYTES      30
250
251 /* The size of addresses as they appear in the Dwarf 2 data.
252    Some architectures use word addresses to refer to code locations,
253    but Dwarf 2 info always uses byte addresses.  On such machines,
254    Dwarf 2 addresses need to be larger than the architecture's
255    pointers.  */
256 #ifndef DWARF2_ADDR_SIZE
257 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
258 #endif
259
260 /* The size in bytes of a DWARF field indicating an offset or length
261    relative to a debug info section, specified to be 4 bytes in the
262    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
263    as PTR_SIZE.  */
264
265 #ifndef DWARF_OFFSET_SIZE
266 #define DWARF_OFFSET_SIZE 4
267 #endif
268
269 /* According to the (draft) DWARF 3 specification, the initial length
270    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
271    bytes are 0xffffffff, followed by the length stored in the next 8
272    bytes.
273
274    However, the SGI/MIPS ABI uses an initial length which is equal to
275    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
276
277 #ifndef DWARF_INITIAL_LENGTH_SIZE
278 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
279 #endif
280
281 #define DWARF_VERSION 2
282
283 /* Round SIZE up to the nearest BOUNDARY.  */
284 #define DWARF_ROUND(SIZE,BOUNDARY) \
285   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
286
287 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
288 #ifndef DWARF_CIE_DATA_ALIGNMENT
289 #ifdef STACK_GROWS_DOWNWARD
290 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
291 #else
292 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
293 #endif
294 #endif
295
296 /* CIE identifier.  */
297 #if HOST_BITS_PER_WIDE_INT >= 64
298 #define DWARF_CIE_ID \
299   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
300 #else
301 #define DWARF_CIE_ID DW_CIE_ID
302 #endif
303
304 /* A pointer to the base of a table that contains frame description
305    information for each routine.  */
306 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
307
308 /* Number of elements currently allocated for fde_table.  */
309 static GTY(()) unsigned fde_table_allocated;
310
311 /* Number of elements in fde_table currently in use.  */
312 static GTY(()) unsigned fde_table_in_use;
313
314 /* Size (in elements) of increments by which we may expand the
315    fde_table.  */
316 #define FDE_TABLE_INCREMENT 256
317
318 /* A list of call frame insns for the CIE.  */
319 static GTY(()) dw_cfi_ref cie_cfi_head;
320
321 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
322 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
323    attribute that accelerates the lookup of the FDE associated
324    with the subprogram.  This variable holds the table index of the FDE
325    associated with the current function (body) definition.  */
326 static unsigned current_funcdef_fde;
327 #endif
328
329 struct indirect_string_node GTY(())
330 {
331   const char *str;
332   unsigned int refcount;
333   unsigned int form;
334   char *label;
335 };
336
337 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
338
339 static GTY(()) int dw2_string_counter;
340 static GTY(()) unsigned long dwarf2out_cfi_label_num;
341
342 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
343
344 /* Forward declarations for functions defined in this file.  */
345
346 static char *stripattributes (const char *);
347 static const char *dwarf_cfi_name (unsigned);
348 static dw_cfi_ref new_cfi (void);
349 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
350 static void add_fde_cfi (const char *, dw_cfi_ref);
351 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
352 static void lookup_cfa (dw_cfa_location *);
353 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
354 #ifdef DWARF2_UNWIND_INFO
355 static void initial_return_save (rtx);
356 #endif
357 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
358 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
359 static void output_call_frame_info (int);
360 static void dwarf2out_stack_adjust (rtx, bool);
361 static void flush_queued_reg_saves (void);
362 static bool clobbers_queued_reg_save (const_rtx);
363 static void dwarf2out_frame_debug_expr (rtx, const char *);
364
365 /* Support for complex CFA locations.  */
366 static void output_cfa_loc (dw_cfi_ref);
367 static void get_cfa_from_loc_descr (dw_cfa_location *,
368                                     struct dw_loc_descr_struct *);
369 static struct dw_loc_descr_struct *build_cfa_loc
370   (dw_cfa_location *, HOST_WIDE_INT);
371 static void def_cfa_1 (const char *, dw_cfa_location *);
372
373 /* How to start an assembler comment.  */
374 #ifndef ASM_COMMENT_START
375 #define ASM_COMMENT_START ";#"
376 #endif
377
378 /* Data and reference forms for relocatable data.  */
379 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
380 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
381
382 #ifndef DEBUG_FRAME_SECTION
383 #define DEBUG_FRAME_SECTION     ".debug_frame"
384 #endif
385
386 #ifndef FUNC_BEGIN_LABEL
387 #define FUNC_BEGIN_LABEL        "LFB"
388 #endif
389
390 #ifndef FUNC_END_LABEL
391 #define FUNC_END_LABEL          "LFE"
392 #endif
393
394 #ifndef FRAME_BEGIN_LABEL
395 #define FRAME_BEGIN_LABEL       "Lframe"
396 #endif
397 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
398 #define CIE_END_LABEL           "LECIE"
399 #define FDE_LABEL               "LSFDE"
400 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
401 #define FDE_END_LABEL           "LEFDE"
402 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
403 #define LINE_NUMBER_END_LABEL   "LELT"
404 #define LN_PROLOG_AS_LABEL      "LASLTP"
405 #define LN_PROLOG_END_LABEL     "LELTP"
406 #define DIE_LABEL_PREFIX        "DW"
407
408 /* The DWARF 2 CFA column which tracks the return address.  Normally this
409    is the column for PC, or the first column after all of the hard
410    registers.  */
411 #ifndef DWARF_FRAME_RETURN_COLUMN
412 #ifdef PC_REGNUM
413 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
414 #else
415 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
416 #endif
417 #endif
418
419 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
420    default, we just provide columns for all registers.  */
421 #ifndef DWARF_FRAME_REGNUM
422 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
423 #endif
424 \f
425 /* Hook used by __throw.  */
426
427 rtx
428 expand_builtin_dwarf_sp_column (void)
429 {
430   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
431   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
432 }
433
434 /* Return a pointer to a copy of the section string name S with all
435    attributes stripped off, and an asterisk prepended (for assemble_name).  */
436
437 static inline char *
438 stripattributes (const char *s)
439 {
440   char *stripped = XNEWVEC (char, strlen (s) + 2);
441   char *p = stripped;
442
443   *p++ = '*';
444
445   while (*s && *s != ',')
446     *p++ = *s++;
447
448   *p = '\0';
449   return stripped;
450 }
451
452 /* MEM is a memory reference for the register size table, each element of
453    which has mode MODE.  Initialize column C as a return address column.  */
454
455 static void
456 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
457 {
458   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
459   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
460   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
461 }
462
463 /* Generate code to initialize the register size table.  */
464
465 void
466 expand_builtin_init_dwarf_reg_sizes (tree address)
467 {
468   unsigned int i;
469   enum machine_mode mode = TYPE_MODE (char_type_node);
470   rtx addr = expand_normal (address);
471   rtx mem = gen_rtx_MEM (BLKmode, addr);
472   bool wrote_return_column = false;
473
474   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
475     {
476       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
477
478       if (rnum < DWARF_FRAME_REGISTERS)
479         {
480           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
481           enum machine_mode save_mode = reg_raw_mode[i];
482           HOST_WIDE_INT size;
483
484           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
485             save_mode = choose_hard_reg_mode (i, 1, true);
486           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
487             {
488               if (save_mode == VOIDmode)
489                 continue;
490               wrote_return_column = true;
491             }
492           size = GET_MODE_SIZE (save_mode);
493           if (offset < 0)
494             continue;
495
496           emit_move_insn (adjust_address (mem, mode, offset),
497                           gen_int_mode (size, mode));
498         }
499     }
500
501   if (!wrote_return_column)
502     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
503
504 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
505   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
506 #endif
507
508   targetm.init_dwarf_reg_sizes_extra (address);
509 }
510
511 /* Convert a DWARF call frame info. operation to its string name */
512
513 static const char *
514 dwarf_cfi_name (unsigned int cfi_opc)
515 {
516   switch (cfi_opc)
517     {
518     case DW_CFA_advance_loc:
519       return "DW_CFA_advance_loc";
520     case DW_CFA_offset:
521       return "DW_CFA_offset";
522     case DW_CFA_restore:
523       return "DW_CFA_restore";
524     case DW_CFA_nop:
525       return "DW_CFA_nop";
526     case DW_CFA_set_loc:
527       return "DW_CFA_set_loc";
528     case DW_CFA_advance_loc1:
529       return "DW_CFA_advance_loc1";
530     case DW_CFA_advance_loc2:
531       return "DW_CFA_advance_loc2";
532     case DW_CFA_advance_loc4:
533       return "DW_CFA_advance_loc4";
534     case DW_CFA_offset_extended:
535       return "DW_CFA_offset_extended";
536     case DW_CFA_restore_extended:
537       return "DW_CFA_restore_extended";
538     case DW_CFA_undefined:
539       return "DW_CFA_undefined";
540     case DW_CFA_same_value:
541       return "DW_CFA_same_value";
542     case DW_CFA_register:
543       return "DW_CFA_register";
544     case DW_CFA_remember_state:
545       return "DW_CFA_remember_state";
546     case DW_CFA_restore_state:
547       return "DW_CFA_restore_state";
548     case DW_CFA_def_cfa:
549       return "DW_CFA_def_cfa";
550     case DW_CFA_def_cfa_register:
551       return "DW_CFA_def_cfa_register";
552     case DW_CFA_def_cfa_offset:
553       return "DW_CFA_def_cfa_offset";
554
555     /* DWARF 3 */
556     case DW_CFA_def_cfa_expression:
557       return "DW_CFA_def_cfa_expression";
558     case DW_CFA_expression:
559       return "DW_CFA_expression";
560     case DW_CFA_offset_extended_sf:
561       return "DW_CFA_offset_extended_sf";
562     case DW_CFA_def_cfa_sf:
563       return "DW_CFA_def_cfa_sf";
564     case DW_CFA_def_cfa_offset_sf:
565       return "DW_CFA_def_cfa_offset_sf";
566
567     /* SGI/MIPS specific */
568     case DW_CFA_MIPS_advance_loc8:
569       return "DW_CFA_MIPS_advance_loc8";
570
571     /* GNU extensions */
572     case DW_CFA_GNU_window_save:
573       return "DW_CFA_GNU_window_save";
574     case DW_CFA_GNU_args_size:
575       return "DW_CFA_GNU_args_size";
576     case DW_CFA_GNU_negative_offset_extended:
577       return "DW_CFA_GNU_negative_offset_extended";
578
579     default:
580       return "DW_CFA_<unknown>";
581     }
582 }
583
584 /* Return a pointer to a newly allocated Call Frame Instruction.  */
585
586 static inline dw_cfi_ref
587 new_cfi (void)
588 {
589   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
590
591   cfi->dw_cfi_next = NULL;
592   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
593   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
594
595   return cfi;
596 }
597
598 /* Add a Call Frame Instruction to list of instructions.  */
599
600 static inline void
601 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
602 {
603   dw_cfi_ref *p;
604
605   /* Find the end of the chain.  */
606   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
607     ;
608
609   *p = cfi;
610 }
611
612 /* Generate a new label for the CFI info to refer to.  */
613
614 char *
615 dwarf2out_cfi_label (void)
616 {
617   static char label[20];
618
619   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
620   ASM_OUTPUT_LABEL (asm_out_file, label);
621   return label;
622 }
623
624 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
625    or to the CIE if LABEL is NULL.  */
626
627 static void
628 add_fde_cfi (const char *label, dw_cfi_ref cfi)
629 {
630   if (label)
631     {
632       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
633
634       if (*label == 0)
635         label = dwarf2out_cfi_label ();
636
637       if (fde->dw_fde_current_label == NULL
638           || strcmp (label, fde->dw_fde_current_label) != 0)
639         {
640           dw_cfi_ref xcfi;
641
642           label = xstrdup (label);
643
644           /* Set the location counter to the new label.  */
645           xcfi = new_cfi ();
646           /* If we have a current label, advance from there, otherwise
647              set the location directly using set_loc.  */
648           xcfi->dw_cfi_opc = fde->dw_fde_current_label
649                              ? DW_CFA_advance_loc4
650                              : DW_CFA_set_loc;
651           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
652           add_cfi (&fde->dw_fde_cfi, xcfi);
653
654           fde->dw_fde_current_label = label;
655         }
656
657       add_cfi (&fde->dw_fde_cfi, cfi);
658     }
659
660   else
661     add_cfi (&cie_cfi_head, cfi);
662 }
663
664 /* Subroutine of lookup_cfa.  */
665
666 static void
667 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
668 {
669   switch (cfi->dw_cfi_opc)
670     {
671     case DW_CFA_def_cfa_offset:
672       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
673       break;
674     case DW_CFA_def_cfa_offset_sf:
675       loc->offset
676         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
677       break;
678     case DW_CFA_def_cfa_register:
679       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
680       break;
681     case DW_CFA_def_cfa:
682       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
683       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
684       break;
685     case DW_CFA_def_cfa_sf:
686       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
687       loc->offset
688         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689       break;
690     case DW_CFA_def_cfa_expression:
691       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
692       break;
693     default:
694       break;
695     }
696 }
697
698 /* Find the previous value for the CFA.  */
699
700 static void
701 lookup_cfa (dw_cfa_location *loc)
702 {
703   dw_cfi_ref cfi;
704
705   loc->reg = INVALID_REGNUM;
706   loc->offset = 0;
707   loc->indirect = 0;
708   loc->base_offset = 0;
709
710   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
711     lookup_cfa_1 (cfi, loc);
712
713   if (fde_table_in_use)
714     {
715       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
716       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
717         lookup_cfa_1 (cfi, loc);
718     }
719 }
720
721 /* The current rule for calculating the DWARF2 canonical frame address.  */
722 static dw_cfa_location cfa;
723
724 /* The register used for saving registers to the stack, and its offset
725    from the CFA.  */
726 static dw_cfa_location cfa_store;
727
728 /* The running total of the size of arguments pushed onto the stack.  */
729 static HOST_WIDE_INT args_size;
730
731 /* The last args_size we actually output.  */
732 static HOST_WIDE_INT old_args_size;
733
734 /* Entry point to update the canonical frame address (CFA).
735    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
736    calculated from REG+OFFSET.  */
737
738 void
739 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
740 {
741   dw_cfa_location loc;
742   loc.indirect = 0;
743   loc.base_offset = 0;
744   loc.reg = reg;
745   loc.offset = offset;
746   def_cfa_1 (label, &loc);
747 }
748
749 /* Determine if two dw_cfa_location structures define the same data.  */
750
751 static bool
752 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
753 {
754   return (loc1->reg == loc2->reg
755           && loc1->offset == loc2->offset
756           && loc1->indirect == loc2->indirect
757           && (loc1->indirect == 0
758               || loc1->base_offset == loc2->base_offset));
759 }
760
761 /* This routine does the actual work.  The CFA is now calculated from
762    the dw_cfa_location structure.  */
763
764 static void
765 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
766 {
767   dw_cfi_ref cfi;
768   dw_cfa_location old_cfa, loc;
769
770   cfa = *loc_p;
771   loc = *loc_p;
772
773   if (cfa_store.reg == loc.reg && loc.indirect == 0)
774     cfa_store.offset = loc.offset;
775
776   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
777   lookup_cfa (&old_cfa);
778
779   /* If nothing changed, no need to issue any call frame instructions.  */
780   if (cfa_equal_p (&loc, &old_cfa))
781     return;
782
783   cfi = new_cfi ();
784
785   if (loc.reg == old_cfa.reg && !loc.indirect)
786     {
787       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
788          the CFA register did not change but the offset did.  */
789       if (loc.offset < 0)
790         {
791           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
792           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
793
794           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
795           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
796         }
797       else
798         {
799           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
800           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
801         }
802     }
803
804 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
805   else if (loc.offset == old_cfa.offset
806            && old_cfa.reg != INVALID_REGNUM
807            && !loc.indirect)
808     {
809       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
810          indicating the CFA register has changed to <register> but the
811          offset has not changed.  */
812       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
813       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
814     }
815 #endif
816
817   else if (loc.indirect == 0)
818     {
819       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
820          indicating the CFA register has changed to <register> with
821          the specified offset.  */
822       if (loc.offset < 0)
823         {
824           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
825           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
826
827           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
828           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
829           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
830         }
831       else
832         {
833           cfi->dw_cfi_opc = DW_CFA_def_cfa;
834           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
835           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
836         }
837     }
838   else
839     {
840       /* Construct a DW_CFA_def_cfa_expression instruction to
841          calculate the CFA using a full location expression since no
842          register-offset pair is available.  */
843       struct dw_loc_descr_struct *loc_list;
844
845       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
846       loc_list = build_cfa_loc (&loc, 0);
847       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
848     }
849
850   add_fde_cfi (label, cfi);
851 }
852
853 /* Add the CFI for saving a register.  REG is the CFA column number.
854    LABEL is passed to add_fde_cfi.
855    If SREG is -1, the register is saved at OFFSET from the CFA;
856    otherwise it is saved in SREG.  */
857
858 static void
859 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
860 {
861   dw_cfi_ref cfi = new_cfi ();
862
863   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
864
865   if (sreg == INVALID_REGNUM)
866     {
867       if (reg & ~0x3f)
868         /* The register number won't fit in 6 bits, so we have to use
869            the long form.  */
870         cfi->dw_cfi_opc = DW_CFA_offset_extended;
871       else
872         cfi->dw_cfi_opc = DW_CFA_offset;
873
874 #ifdef ENABLE_CHECKING
875       {
876         /* If we get an offset that is not a multiple of
877            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
878            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
879            description.  */
880         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
881
882         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
883       }
884 #endif
885       offset /= DWARF_CIE_DATA_ALIGNMENT;
886       if (offset < 0)
887         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
888
889       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
890     }
891   else if (sreg == reg)
892     cfi->dw_cfi_opc = DW_CFA_same_value;
893   else
894     {
895       cfi->dw_cfi_opc = DW_CFA_register;
896       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
897     }
898
899   add_fde_cfi (label, cfi);
900 }
901
902 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
903    This CFI tells the unwinder that it needs to restore the window registers
904    from the previous frame's window save area.
905
906    ??? Perhaps we should note in the CIE where windows are saved (instead of
907    assuming 0(cfa)) and what registers are in the window.  */
908
909 void
910 dwarf2out_window_save (const char *label)
911 {
912   dw_cfi_ref cfi = new_cfi ();
913
914   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
915   add_fde_cfi (label, cfi);
916 }
917
918 /* Add a CFI to update the running total of the size of arguments
919    pushed onto the stack.  */
920
921 void
922 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
923 {
924   dw_cfi_ref cfi;
925
926   if (size == old_args_size)
927     return;
928
929   old_args_size = size;
930
931   cfi = new_cfi ();
932   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
933   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
934   add_fde_cfi (label, cfi);
935 }
936
937 /* Entry point for saving a register to the stack.  REG is the GCC register
938    number.  LABEL and OFFSET are passed to reg_save.  */
939
940 void
941 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
942 {
943   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
944 }
945
946 /* Entry point for saving the return address in the stack.
947    LABEL and OFFSET are passed to reg_save.  */
948
949 void
950 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
951 {
952   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
953 }
954
955 /* Entry point for saving the return address in a register.
956    LABEL and SREG are passed to reg_save.  */
957
958 void
959 dwarf2out_return_reg (const char *label, unsigned int sreg)
960 {
961   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
962 }
963
964 #ifdef DWARF2_UNWIND_INFO
965 /* Record the initial position of the return address.  RTL is
966    INCOMING_RETURN_ADDR_RTX.  */
967
968 static void
969 initial_return_save (rtx rtl)
970 {
971   unsigned int reg = INVALID_REGNUM;
972   HOST_WIDE_INT offset = 0;
973
974   switch (GET_CODE (rtl))
975     {
976     case REG:
977       /* RA is in a register.  */
978       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
979       break;
980
981     case MEM:
982       /* RA is on the stack.  */
983       rtl = XEXP (rtl, 0);
984       switch (GET_CODE (rtl))
985         {
986         case REG:
987           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
988           offset = 0;
989           break;
990
991         case PLUS:
992           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
993           offset = INTVAL (XEXP (rtl, 1));
994           break;
995
996         case MINUS:
997           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
998           offset = -INTVAL (XEXP (rtl, 1));
999           break;
1000
1001         default:
1002           gcc_unreachable ();
1003         }
1004
1005       break;
1006
1007     case PLUS:
1008       /* The return address is at some offset from any value we can
1009          actually load.  For instance, on the SPARC it is in %i7+8. Just
1010          ignore the offset for now; it doesn't matter for unwinding frames.  */
1011       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1012       initial_return_save (XEXP (rtl, 0));
1013       return;
1014
1015     default:
1016       gcc_unreachable ();
1017     }
1018
1019   if (reg != DWARF_FRAME_RETURN_COLUMN)
1020     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1021 }
1022 #endif
1023
1024 /* Given a SET, calculate the amount of stack adjustment it
1025    contains.  */
1026
1027 static HOST_WIDE_INT
1028 stack_adjust_offset (const_rtx pattern)
1029 {
1030   const_rtx src = SET_SRC (pattern);
1031   const_rtx dest = SET_DEST (pattern);
1032   HOST_WIDE_INT offset = 0;
1033   enum rtx_code code;
1034
1035   if (dest == stack_pointer_rtx)
1036     {
1037       /* (set (reg sp) (plus (reg sp) (const_int))) */
1038       code = GET_CODE (src);
1039       if (! (code == PLUS || code == MINUS)
1040           || XEXP (src, 0) != stack_pointer_rtx
1041           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1042         return 0;
1043
1044       offset = INTVAL (XEXP (src, 1));
1045       if (code == PLUS)
1046         offset = -offset;
1047     }
1048   else if (MEM_P (dest))
1049     {
1050       /* (set (mem (pre_dec (reg sp))) (foo)) */
1051       src = XEXP (dest, 0);
1052       code = GET_CODE (src);
1053
1054       switch (code)
1055         {
1056         case PRE_MODIFY:
1057         case POST_MODIFY:
1058           if (XEXP (src, 0) == stack_pointer_rtx)
1059             {
1060               rtx val = XEXP (XEXP (src, 1), 1);
1061               /* We handle only adjustments by constant amount.  */
1062               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1063                           && GET_CODE (val) == CONST_INT);
1064               offset = -INTVAL (val);
1065               break;
1066             }
1067           return 0;
1068
1069         case PRE_DEC:
1070         case POST_DEC:
1071           if (XEXP (src, 0) == stack_pointer_rtx)
1072             {
1073               offset = GET_MODE_SIZE (GET_MODE (dest));
1074               break;
1075             }
1076           return 0;
1077
1078         case PRE_INC:
1079         case POST_INC:
1080           if (XEXP (src, 0) == stack_pointer_rtx)
1081             {
1082               offset = -GET_MODE_SIZE (GET_MODE (dest));
1083               break;
1084             }
1085           return 0;
1086
1087         default:
1088           return 0;
1089         }
1090     }
1091   else
1092     return 0;
1093
1094   return offset;
1095 }
1096
1097 /* Check INSN to see if it looks like a push or a stack adjustment, and
1098    make a note of it if it does.  EH uses this information to find out how
1099    much extra space it needs to pop off the stack.  */
1100
1101 static void
1102 dwarf2out_stack_adjust (rtx insn, bool after_p)
1103 {
1104   HOST_WIDE_INT offset;
1105   const char *label;
1106   int i;
1107
1108   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1109      with this function.  Proper support would require all frame-related
1110      insns to be marked, and to be able to handle saving state around
1111      epilogues textually in the middle of the function.  */
1112   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1113     return;
1114
1115   /* If only calls can throw, and we have a frame pointer,
1116      save up adjustments until we see the CALL_INSN.  */
1117   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1118     {
1119       if (CALL_P (insn) && !after_p)
1120         {
1121           /* Extract the size of the args from the CALL rtx itself.  */
1122           insn = PATTERN (insn);
1123           if (GET_CODE (insn) == PARALLEL)
1124             insn = XVECEXP (insn, 0, 0);
1125           if (GET_CODE (insn) == SET)
1126             insn = SET_SRC (insn);
1127           gcc_assert (GET_CODE (insn) == CALL);
1128           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1129         }
1130       return;
1131     }
1132
1133   if (CALL_P (insn) && !after_p)
1134     {
1135       if (!flag_asynchronous_unwind_tables)
1136         dwarf2out_args_size ("", args_size);
1137       return;
1138     }
1139   else if (BARRIER_P (insn))
1140     {
1141       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1142          the compiler will have already emitted a stack adjustment, but
1143          doesn't bother for calls to noreturn functions.  */
1144 #ifdef STACK_GROWS_DOWNWARD
1145       offset = -args_size;
1146 #else
1147       offset = args_size;
1148 #endif
1149     }
1150   else if (GET_CODE (PATTERN (insn)) == SET)
1151     offset = stack_adjust_offset (PATTERN (insn));
1152   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1153            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1154     {
1155       /* There may be stack adjustments inside compound insns.  Search
1156          for them.  */
1157       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1158         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1159           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1160     }
1161   else
1162     return;
1163
1164   if (offset == 0)
1165     return;
1166
1167   if (cfa.reg == STACK_POINTER_REGNUM)
1168     cfa.offset += offset;
1169
1170 #ifndef STACK_GROWS_DOWNWARD
1171   offset = -offset;
1172 #endif
1173
1174   args_size += offset;
1175   if (args_size < 0)
1176     args_size = 0;
1177
1178   label = dwarf2out_cfi_label ();
1179   def_cfa_1 (label, &cfa);
1180   if (flag_asynchronous_unwind_tables)
1181     dwarf2out_args_size (label, args_size);
1182 }
1183
1184 #endif
1185
1186 /* We delay emitting a register save until either (a) we reach the end
1187    of the prologue or (b) the register is clobbered.  This clusters
1188    register saves so that there are fewer pc advances.  */
1189
1190 struct queued_reg_save GTY(())
1191 {
1192   struct queued_reg_save *next;
1193   rtx reg;
1194   HOST_WIDE_INT cfa_offset;
1195   rtx saved_reg;
1196 };
1197
1198 static GTY(()) struct queued_reg_save *queued_reg_saves;
1199
1200 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1201 struct reg_saved_in_data GTY(()) {
1202   rtx orig_reg;
1203   rtx saved_in_reg;
1204 };
1205
1206 /* A list of registers saved in other registers.
1207    The list intentionally has a small maximum capacity of 4; if your
1208    port needs more than that, you might consider implementing a
1209    more efficient data structure.  */
1210 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1211 static GTY(()) size_t num_regs_saved_in_regs;
1212
1213 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1214 static const char *last_reg_save_label;
1215
1216 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1217    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1218
1219 static void
1220 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1221 {
1222   struct queued_reg_save *q;
1223
1224   /* Duplicates waste space, but it's also necessary to remove them
1225      for correctness, since the queue gets output in reverse
1226      order.  */
1227   for (q = queued_reg_saves; q != NULL; q = q->next)
1228     if (REGNO (q->reg) == REGNO (reg))
1229       break;
1230
1231   if (q == NULL)
1232     {
1233       q = ggc_alloc (sizeof (*q));
1234       q->next = queued_reg_saves;
1235       queued_reg_saves = q;
1236     }
1237
1238   q->reg = reg;
1239   q->cfa_offset = offset;
1240   q->saved_reg = sreg;
1241
1242   last_reg_save_label = label;
1243 }
1244
1245 /* Output all the entries in QUEUED_REG_SAVES.  */
1246
1247 static void
1248 flush_queued_reg_saves (void)
1249 {
1250   struct queued_reg_save *q;
1251
1252   for (q = queued_reg_saves; q; q = q->next)
1253     {
1254       size_t i;
1255       unsigned int reg, sreg;
1256
1257       for (i = 0; i < num_regs_saved_in_regs; i++)
1258         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1259           break;
1260       if (q->saved_reg && i == num_regs_saved_in_regs)
1261         {
1262           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1263           num_regs_saved_in_regs++;
1264         }
1265       if (i != num_regs_saved_in_regs)
1266         {
1267           regs_saved_in_regs[i].orig_reg = q->reg;
1268           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1269         }
1270
1271       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1272       if (q->saved_reg)
1273         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1274       else
1275         sreg = INVALID_REGNUM;
1276       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1277     }
1278
1279   queued_reg_saves = NULL;
1280   last_reg_save_label = NULL;
1281 }
1282
1283 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1284    location for?  Or, does it clobber a register which we've previously
1285    said that some other register is saved in, and for which we now
1286    have a new location for?  */
1287
1288 static bool
1289 clobbers_queued_reg_save (const_rtx insn)
1290 {
1291   struct queued_reg_save *q;
1292
1293   for (q = queued_reg_saves; q; q = q->next)
1294     {
1295       size_t i;
1296       if (modified_in_p (q->reg, insn))
1297         return true;
1298       for (i = 0; i < num_regs_saved_in_regs; i++)
1299         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1300             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1301           return true;
1302     }
1303
1304   return false;
1305 }
1306
1307 /* Entry point for saving the first register into the second.  */
1308
1309 void
1310 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1311 {
1312   size_t i;
1313   unsigned int regno, sregno;
1314
1315   for (i = 0; i < num_regs_saved_in_regs; i++)
1316     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1317       break;
1318   if (i == num_regs_saved_in_regs)
1319     {
1320       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1321       num_regs_saved_in_regs++;
1322     }
1323   regs_saved_in_regs[i].orig_reg = reg;
1324   regs_saved_in_regs[i].saved_in_reg = sreg;
1325
1326   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1327   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1328   reg_save (label, regno, sregno, 0);
1329 }
1330
1331 /* What register, if any, is currently saved in REG?  */
1332
1333 static rtx
1334 reg_saved_in (rtx reg)
1335 {
1336   unsigned int regn = REGNO (reg);
1337   size_t i;
1338   struct queued_reg_save *q;
1339
1340   for (q = queued_reg_saves; q; q = q->next)
1341     if (q->saved_reg && regn == REGNO (q->saved_reg))
1342       return q->reg;
1343
1344   for (i = 0; i < num_regs_saved_in_regs; i++)
1345     if (regs_saved_in_regs[i].saved_in_reg
1346         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1347       return regs_saved_in_regs[i].orig_reg;
1348
1349   return NULL_RTX;
1350 }
1351
1352
1353 /* A temporary register holding an integral value used in adjusting SP
1354    or setting up the store_reg.  The "offset" field holds the integer
1355    value, not an offset.  */
1356 static dw_cfa_location cfa_temp;
1357
1358 /* Record call frame debugging information for an expression EXPR,
1359    which either sets SP or FP (adjusting how we calculate the frame
1360    address) or saves a register to the stack or another register.
1361    LABEL indicates the address of EXPR.
1362
1363    This function encodes a state machine mapping rtxes to actions on
1364    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1365    users need not read the source code.
1366
1367   The High-Level Picture
1368
1369   Changes in the register we use to calculate the CFA: Currently we
1370   assume that if you copy the CFA register into another register, we
1371   should take the other one as the new CFA register; this seems to
1372   work pretty well.  If it's wrong for some target, it's simple
1373   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1374
1375   Changes in the register we use for saving registers to the stack:
1376   This is usually SP, but not always.  Again, we deduce that if you
1377   copy SP into another register (and SP is not the CFA register),
1378   then the new register is the one we will be using for register
1379   saves.  This also seems to work.
1380
1381   Register saves: There's not much guesswork about this one; if
1382   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1383   register save, and the register used to calculate the destination
1384   had better be the one we think we're using for this purpose.
1385   It's also assumed that a copy from a call-saved register to another
1386   register is saving that register if RTX_FRAME_RELATED_P is set on
1387   that instruction.  If the copy is from a call-saved register to
1388   the *same* register, that means that the register is now the same
1389   value as in the caller.
1390
1391   Except: If the register being saved is the CFA register, and the
1392   offset is nonzero, we are saving the CFA, so we assume we have to
1393   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1394   the intent is to save the value of SP from the previous frame.
1395
1396   In addition, if a register has previously been saved to a different
1397   register,
1398
1399   Invariants / Summaries of Rules
1400
1401   cfa          current rule for calculating the CFA.  It usually
1402                consists of a register and an offset.
1403   cfa_store    register used by prologue code to save things to the stack
1404                cfa_store.offset is the offset from the value of
1405                cfa_store.reg to the actual CFA
1406   cfa_temp     register holding an integral value.  cfa_temp.offset
1407                stores the value, which will be used to adjust the
1408                stack pointer.  cfa_temp is also used like cfa_store,
1409                to track stores to the stack via fp or a temp reg.
1410
1411   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1412                with cfa.reg as the first operand changes the cfa.reg and its
1413                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1414                cfa_temp.offset.
1415
1416   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1417                expression yielding a constant.  This sets cfa_temp.reg
1418                and cfa_temp.offset.
1419
1420   Rule 5:      Create a new register cfa_store used to save items to the
1421                stack.
1422
1423   Rules 10-14: Save a register to the stack.  Define offset as the
1424                difference of the original location and cfa_store's
1425                location (or cfa_temp's location if cfa_temp is used).
1426
1427   The Rules
1428
1429   "{a,b}" indicates a choice of a xor b.
1430   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1431
1432   Rule 1:
1433   (set <reg1> <reg2>:cfa.reg)
1434   effects: cfa.reg = <reg1>
1435            cfa.offset unchanged
1436            cfa_temp.reg = <reg1>
1437            cfa_temp.offset = cfa.offset
1438
1439   Rule 2:
1440   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1441                               {<const_int>,<reg>:cfa_temp.reg}))
1442   effects: cfa.reg = sp if fp used
1443            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1444            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1445              if cfa_store.reg==sp
1446
1447   Rule 3:
1448   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1449   effects: cfa.reg = fp
1450            cfa_offset += +/- <const_int>
1451
1452   Rule 4:
1453   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1454   constraints: <reg1> != fp
1455                <reg1> != sp
1456   effects: cfa.reg = <reg1>
1457            cfa_temp.reg = <reg1>
1458            cfa_temp.offset = cfa.offset
1459
1460   Rule 5:
1461   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1462   constraints: <reg1> != fp
1463                <reg1> != sp
1464   effects: cfa_store.reg = <reg1>
1465            cfa_store.offset = cfa.offset - cfa_temp.offset
1466
1467   Rule 6:
1468   (set <reg> <const_int>)
1469   effects: cfa_temp.reg = <reg>
1470            cfa_temp.offset = <const_int>
1471
1472   Rule 7:
1473   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1474   effects: cfa_temp.reg = <reg1>
1475            cfa_temp.offset |= <const_int>
1476
1477   Rule 8:
1478   (set <reg> (high <exp>))
1479   effects: none
1480
1481   Rule 9:
1482   (set <reg> (lo_sum <exp> <const_int>))
1483   effects: cfa_temp.reg = <reg>
1484            cfa_temp.offset = <const_int>
1485
1486   Rule 10:
1487   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1488   effects: cfa_store.offset -= <const_int>
1489            cfa.offset = cfa_store.offset if cfa.reg == sp
1490            cfa.reg = sp
1491            cfa.base_offset = -cfa_store.offset
1492
1493   Rule 11:
1494   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1495   effects: cfa_store.offset += -/+ mode_size(mem)
1496            cfa.offset = cfa_store.offset if cfa.reg == sp
1497            cfa.reg = sp
1498            cfa.base_offset = -cfa_store.offset
1499
1500   Rule 12:
1501   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1502
1503        <reg2>)
1504   effects: cfa.reg = <reg1>
1505            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1506
1507   Rule 13:
1508   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1509   effects: cfa.reg = <reg1>
1510            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1511
1512   Rule 14:
1513   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1514   effects: cfa.reg = <reg1>
1515            cfa.base_offset = -cfa_temp.offset
1516            cfa_temp.offset -= mode_size(mem)
1517
1518   Rule 15:
1519   (set <reg> {unspec, unspec_volatile})
1520   effects: target-dependent  */
1521
1522 static void
1523 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1524 {
1525   rtx src, dest;
1526   HOST_WIDE_INT offset;
1527
1528   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1529      the PARALLEL independently. The first element is always processed if
1530      it is a SET. This is for backward compatibility.   Other elements
1531      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1532      flag is set in them.  */
1533   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1534     {
1535       int par_index;
1536       int limit = XVECLEN (expr, 0);
1537       rtx elem;
1538
1539       /* PARALLELs have strict read-modify-write semantics, so we
1540          ought to evaluate every rvalue before changing any lvalue.
1541          It's cumbersome to do that in general, but there's an
1542          easy approximation that is enough for all current users:
1543          handle register saves before register assignments.  */
1544       if (GET_CODE (expr) == PARALLEL)
1545         for (par_index = 0; par_index < limit; par_index++)
1546           {
1547             elem = XVECEXP (expr, 0, par_index);
1548             if (GET_CODE (elem) == SET
1549                 && MEM_P (SET_DEST (elem))
1550                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1551               dwarf2out_frame_debug_expr (elem, label);
1552           }
1553
1554       for (par_index = 0; par_index < limit; par_index++)
1555         {
1556           elem = XVECEXP (expr, 0, par_index);
1557           if (GET_CODE (elem) == SET
1558               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1559               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1560             dwarf2out_frame_debug_expr (elem, label);
1561         }
1562       return;
1563     }
1564
1565   gcc_assert (GET_CODE (expr) == SET);
1566
1567   src = SET_SRC (expr);
1568   dest = SET_DEST (expr);
1569
1570   if (REG_P (src))
1571     {
1572       rtx rsi = reg_saved_in (src);
1573       if (rsi)
1574         src = rsi;
1575     }
1576
1577   switch (GET_CODE (dest))
1578     {
1579     case REG:
1580       switch (GET_CODE (src))
1581         {
1582           /* Setting FP from SP.  */
1583         case REG:
1584           if (cfa.reg == (unsigned) REGNO (src))
1585             {
1586               /* Rule 1 */
1587               /* Update the CFA rule wrt SP or FP.  Make sure src is
1588                  relative to the current CFA register.
1589
1590                  We used to require that dest be either SP or FP, but the
1591                  ARM copies SP to a temporary register, and from there to
1592                  FP.  So we just rely on the backends to only set
1593                  RTX_FRAME_RELATED_P on appropriate insns.  */
1594               cfa.reg = REGNO (dest);
1595               cfa_temp.reg = cfa.reg;
1596               cfa_temp.offset = cfa.offset;
1597             }
1598           else
1599             {
1600               /* Saving a register in a register.  */
1601               gcc_assert (!fixed_regs [REGNO (dest)]
1602                           /* For the SPARC and its register window.  */
1603                           || (DWARF_FRAME_REGNUM (REGNO (src))
1604                               == DWARF_FRAME_RETURN_COLUMN));
1605               queue_reg_save (label, src, dest, 0);
1606             }
1607           break;
1608
1609         case PLUS:
1610         case MINUS:
1611         case LO_SUM:
1612           if (dest == stack_pointer_rtx)
1613             {
1614               /* Rule 2 */
1615               /* Adjusting SP.  */
1616               switch (GET_CODE (XEXP (src, 1)))
1617                 {
1618                 case CONST_INT:
1619                   offset = INTVAL (XEXP (src, 1));
1620                   break;
1621                 case REG:
1622                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1623                               == cfa_temp.reg);
1624                   offset = cfa_temp.offset;
1625                   break;
1626                 default:
1627                   gcc_unreachable ();
1628                 }
1629
1630               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1631                 {
1632                   /* Restoring SP from FP in the epilogue.  */
1633                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1634                   cfa.reg = STACK_POINTER_REGNUM;
1635                 }
1636               else if (GET_CODE (src) == LO_SUM)
1637                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1638                 ;
1639               else
1640                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1641
1642               if (GET_CODE (src) != MINUS)
1643                 offset = -offset;
1644               if (cfa.reg == STACK_POINTER_REGNUM)
1645                 cfa.offset += offset;
1646               if (cfa_store.reg == STACK_POINTER_REGNUM)
1647                 cfa_store.offset += offset;
1648             }
1649           else if (dest == hard_frame_pointer_rtx)
1650             {
1651               /* Rule 3 */
1652               /* Either setting the FP from an offset of the SP,
1653                  or adjusting the FP */
1654               gcc_assert (frame_pointer_needed);
1655
1656               gcc_assert (REG_P (XEXP (src, 0))
1657                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1658                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1659               offset = INTVAL (XEXP (src, 1));
1660               if (GET_CODE (src) != MINUS)
1661                 offset = -offset;
1662               cfa.offset += offset;
1663               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1664             }
1665           else
1666             {
1667               gcc_assert (GET_CODE (src) != MINUS);
1668
1669               /* Rule 4 */
1670               if (REG_P (XEXP (src, 0))
1671                   && REGNO (XEXP (src, 0)) == cfa.reg
1672                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1673                 {
1674                   /* Setting a temporary CFA register that will be copied
1675                      into the FP later on.  */
1676                   offset = - INTVAL (XEXP (src, 1));
1677                   cfa.offset += offset;
1678                   cfa.reg = REGNO (dest);
1679                   /* Or used to save regs to the stack.  */
1680                   cfa_temp.reg = cfa.reg;
1681                   cfa_temp.offset = cfa.offset;
1682                 }
1683
1684               /* Rule 5 */
1685               else if (REG_P (XEXP (src, 0))
1686                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1687                        && XEXP (src, 1) == stack_pointer_rtx)
1688                 {
1689                   /* Setting a scratch register that we will use instead
1690                      of SP for saving registers to the stack.  */
1691                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1692                   cfa_store.reg = REGNO (dest);
1693                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1694                 }
1695
1696               /* Rule 9 */
1697               else if (GET_CODE (src) == LO_SUM
1698                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1699                 {
1700                   cfa_temp.reg = REGNO (dest);
1701                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1702                 }
1703               else
1704                 gcc_unreachable ();
1705             }
1706           break;
1707
1708           /* Rule 6 */
1709         case CONST_INT:
1710           cfa_temp.reg = REGNO (dest);
1711           cfa_temp.offset = INTVAL (src);
1712           break;
1713
1714           /* Rule 7 */
1715         case IOR:
1716           gcc_assert (REG_P (XEXP (src, 0))
1717                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1718                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1719
1720           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1721             cfa_temp.reg = REGNO (dest);
1722           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1723           break;
1724
1725           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1726              which will fill in all of the bits.  */
1727           /* Rule 8 */
1728         case HIGH:
1729           break;
1730
1731           /* Rule 15 */
1732         case UNSPEC:
1733         case UNSPEC_VOLATILE:
1734           gcc_assert (targetm.dwarf_handle_frame_unspec);
1735           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1736           return;
1737
1738         default:
1739           gcc_unreachable ();
1740         }
1741
1742       def_cfa_1 (label, &cfa);
1743       break;
1744
1745     case MEM:
1746       gcc_assert (REG_P (src));
1747
1748       /* Saving a register to the stack.  Make sure dest is relative to the
1749          CFA register.  */
1750       switch (GET_CODE (XEXP (dest, 0)))
1751         {
1752           /* Rule 10 */
1753           /* With a push.  */
1754         case PRE_MODIFY:
1755           /* We can't handle variable size modifications.  */
1756           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1757                       == CONST_INT);
1758           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1759
1760           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1761                       && cfa_store.reg == STACK_POINTER_REGNUM);
1762
1763           cfa_store.offset += offset;
1764           if (cfa.reg == STACK_POINTER_REGNUM)
1765             cfa.offset = cfa_store.offset;
1766
1767           offset = -cfa_store.offset;
1768           break;
1769
1770           /* Rule 11 */
1771         case PRE_INC:
1772         case PRE_DEC:
1773           offset = GET_MODE_SIZE (GET_MODE (dest));
1774           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1775             offset = -offset;
1776
1777           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1778                       && cfa_store.reg == STACK_POINTER_REGNUM);
1779
1780           cfa_store.offset += offset;
1781           if (cfa.reg == STACK_POINTER_REGNUM)
1782             cfa.offset = cfa_store.offset;
1783
1784           offset = -cfa_store.offset;
1785           break;
1786
1787           /* Rule 12 */
1788           /* With an offset.  */
1789         case PLUS:
1790         case MINUS:
1791         case LO_SUM:
1792           {
1793             int regno;
1794
1795             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1796                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1797             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1798             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1799               offset = -offset;
1800
1801             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1802
1803             if (cfa_store.reg == (unsigned) regno)
1804               offset -= cfa_store.offset;
1805             else
1806               {
1807                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1808                 offset -= cfa_temp.offset;
1809               }
1810           }
1811           break;
1812
1813           /* Rule 13 */
1814           /* Without an offset.  */
1815         case REG:
1816           {
1817             int regno = REGNO (XEXP (dest, 0));
1818
1819             if (cfa_store.reg == (unsigned) regno)
1820               offset = -cfa_store.offset;
1821             else
1822               {
1823                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1824                 offset = -cfa_temp.offset;
1825               }
1826           }
1827           break;
1828
1829           /* Rule 14 */
1830         case POST_INC:
1831           gcc_assert (cfa_temp.reg
1832                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1833           offset = -cfa_temp.offset;
1834           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1835           break;
1836
1837         default:
1838           gcc_unreachable ();
1839         }
1840
1841       if (REGNO (src) != STACK_POINTER_REGNUM
1842           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1843           && (unsigned) REGNO (src) == cfa.reg)
1844         {
1845           /* We're storing the current CFA reg into the stack.  */
1846
1847           if (cfa.offset == 0)
1848             {
1849               /* If the source register is exactly the CFA, assume
1850                  we're saving SP like any other register; this happens
1851                  on the ARM.  */
1852               def_cfa_1 (label, &cfa);
1853               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1854               break;
1855             }
1856           else
1857             {
1858               /* Otherwise, we'll need to look in the stack to
1859                  calculate the CFA.  */
1860               rtx x = XEXP (dest, 0);
1861
1862               if (!REG_P (x))
1863                 x = XEXP (x, 0);
1864               gcc_assert (REG_P (x));
1865
1866               cfa.reg = REGNO (x);
1867               cfa.base_offset = offset;
1868               cfa.indirect = 1;
1869               def_cfa_1 (label, &cfa);
1870               break;
1871             }
1872         }
1873
1874       def_cfa_1 (label, &cfa);
1875       queue_reg_save (label, src, NULL_RTX, offset);
1876       break;
1877
1878     default:
1879       gcc_unreachable ();
1880     }
1881 }
1882
1883 /* Record call frame debugging information for INSN, which either
1884    sets SP or FP (adjusting how we calculate the frame address) or saves a
1885    register to the stack.  If INSN is NULL_RTX, initialize our state.
1886
1887    If AFTER_P is false, we're being called before the insn is emitted,
1888    otherwise after.  Call instructions get invoked twice.  */
1889
1890 void
1891 dwarf2out_frame_debug (rtx insn, bool after_p)
1892 {
1893   const char *label;
1894   rtx src;
1895
1896   if (insn == NULL_RTX)
1897     {
1898       size_t i;
1899
1900       /* Flush any queued register saves.  */
1901       flush_queued_reg_saves ();
1902
1903       /* Set up state for generating call frame debug info.  */
1904       lookup_cfa (&cfa);
1905       gcc_assert (cfa.reg
1906                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1907
1908       cfa.reg = STACK_POINTER_REGNUM;
1909       cfa_store = cfa;
1910       cfa_temp.reg = -1;
1911       cfa_temp.offset = 0;
1912
1913       for (i = 0; i < num_regs_saved_in_regs; i++)
1914         {
1915           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1916           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1917         }
1918       num_regs_saved_in_regs = 0;
1919       return;
1920     }
1921
1922   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1923     flush_queued_reg_saves ();
1924
1925   if (! RTX_FRAME_RELATED_P (insn))
1926     {
1927       if (!ACCUMULATE_OUTGOING_ARGS)
1928         dwarf2out_stack_adjust (insn, after_p);
1929       return;
1930     }
1931
1932   label = dwarf2out_cfi_label ();
1933   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1934   if (src)
1935     insn = XEXP (src, 0);
1936   else
1937     insn = PATTERN (insn);
1938
1939   dwarf2out_frame_debug_expr (insn, label);
1940 }
1941
1942 #endif
1943
1944 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1945 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1946  (enum dwarf_call_frame_info cfi);
1947
1948 static enum dw_cfi_oprnd_type
1949 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1950 {
1951   switch (cfi)
1952     {
1953     case DW_CFA_nop:
1954     case DW_CFA_GNU_window_save:
1955       return dw_cfi_oprnd_unused;
1956
1957     case DW_CFA_set_loc:
1958     case DW_CFA_advance_loc1:
1959     case DW_CFA_advance_loc2:
1960     case DW_CFA_advance_loc4:
1961     case DW_CFA_MIPS_advance_loc8:
1962       return dw_cfi_oprnd_addr;
1963
1964     case DW_CFA_offset:
1965     case DW_CFA_offset_extended:
1966     case DW_CFA_def_cfa:
1967     case DW_CFA_offset_extended_sf:
1968     case DW_CFA_def_cfa_sf:
1969     case DW_CFA_restore_extended:
1970     case DW_CFA_undefined:
1971     case DW_CFA_same_value:
1972     case DW_CFA_def_cfa_register:
1973     case DW_CFA_register:
1974       return dw_cfi_oprnd_reg_num;
1975
1976     case DW_CFA_def_cfa_offset:
1977     case DW_CFA_GNU_args_size:
1978     case DW_CFA_def_cfa_offset_sf:
1979       return dw_cfi_oprnd_offset;
1980
1981     case DW_CFA_def_cfa_expression:
1982     case DW_CFA_expression:
1983       return dw_cfi_oprnd_loc;
1984
1985     default:
1986       gcc_unreachable ();
1987     }
1988 }
1989
1990 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
1991 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
1992  (enum dwarf_call_frame_info cfi);
1993
1994 static enum dw_cfi_oprnd_type
1995 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
1996 {
1997   switch (cfi)
1998     {
1999     case DW_CFA_def_cfa:
2000     case DW_CFA_def_cfa_sf:
2001     case DW_CFA_offset:
2002     case DW_CFA_offset_extended_sf:
2003     case DW_CFA_offset_extended:
2004       return dw_cfi_oprnd_offset;
2005
2006     case DW_CFA_register:
2007       return dw_cfi_oprnd_reg_num;
2008
2009     default:
2010       return dw_cfi_oprnd_unused;
2011     }
2012 }
2013
2014 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2015
2016 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2017    switch to the data section instead, and write out a synthetic label
2018    for collect2.  */
2019
2020 static void
2021 switch_to_eh_frame_section (void)
2022 {
2023   tree label;
2024
2025 #ifdef EH_FRAME_SECTION_NAME
2026   if (eh_frame_section == 0)
2027     {
2028       int flags;
2029
2030       if (EH_TABLES_CAN_BE_READ_ONLY)
2031         {
2032           int fde_encoding;
2033           int per_encoding;
2034           int lsda_encoding;
2035
2036           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2037                                                        /*global=*/0);
2038           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2039                                                        /*global=*/1);
2040           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2041                                                         /*global=*/0);
2042           flags = ((! flag_pic
2043                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2044                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2045                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2046                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2047                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2048                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2049                    ? 0 : SECTION_WRITE);
2050         }
2051       else
2052         flags = SECTION_WRITE;
2053       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2054     }
2055 #endif
2056
2057   if (eh_frame_section)
2058     switch_to_section (eh_frame_section);
2059   else
2060     {
2061       /* We have no special eh_frame section.  Put the information in
2062          the data section and emit special labels to guide collect2.  */
2063       switch_to_section (data_section);
2064       label = get_file_function_name ("F");
2065       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2066       targetm.asm_out.globalize_label (asm_out_file,
2067                                        IDENTIFIER_POINTER (label));
2068       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2069     }
2070 }
2071
2072 /* Output a Call Frame Information opcode and its operand(s).  */
2073
2074 static void
2075 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2076 {
2077   unsigned long r;
2078   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2079     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2080                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2081                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2082                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
2083   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2084     {
2085       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2086       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2087                            "DW_CFA_offset, column 0x%lx", r);
2088       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2089     }
2090   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2091     {
2092       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2093       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2094                            "DW_CFA_restore, column 0x%lx", r);
2095     }
2096   else
2097     {
2098       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2099                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2100
2101       switch (cfi->dw_cfi_opc)
2102         {
2103         case DW_CFA_set_loc:
2104           if (for_eh)
2105             dw2_asm_output_encoded_addr_rtx (
2106                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2107                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2108                 false, NULL);
2109           else
2110             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2111                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2112           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2113           break;
2114
2115         case DW_CFA_advance_loc1:
2116           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2117                                 fde->dw_fde_current_label, NULL);
2118           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2119           break;
2120
2121         case DW_CFA_advance_loc2:
2122           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2123                                 fde->dw_fde_current_label, NULL);
2124           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2125           break;
2126
2127         case DW_CFA_advance_loc4:
2128           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2129                                 fde->dw_fde_current_label, NULL);
2130           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2131           break;
2132
2133         case DW_CFA_MIPS_advance_loc8:
2134           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2135                                 fde->dw_fde_current_label, NULL);
2136           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2137           break;
2138
2139         case DW_CFA_offset_extended:
2140         case DW_CFA_def_cfa:
2141           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2142           dw2_asm_output_data_uleb128 (r, NULL);
2143           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2144           break;
2145
2146         case DW_CFA_offset_extended_sf:
2147         case DW_CFA_def_cfa_sf:
2148           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2149           dw2_asm_output_data_uleb128 (r, NULL);
2150           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2151           break;
2152
2153         case DW_CFA_restore_extended:
2154         case DW_CFA_undefined:
2155         case DW_CFA_same_value:
2156         case DW_CFA_def_cfa_register:
2157           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2158           dw2_asm_output_data_uleb128 (r, NULL);
2159           break;
2160
2161         case DW_CFA_register:
2162           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2163           dw2_asm_output_data_uleb128 (r, NULL);
2164           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2165           dw2_asm_output_data_uleb128 (r, NULL);
2166           break;
2167
2168         case DW_CFA_def_cfa_offset:
2169         case DW_CFA_GNU_args_size:
2170           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2171           break;
2172
2173         case DW_CFA_def_cfa_offset_sf:
2174           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2175           break;
2176
2177         case DW_CFA_GNU_window_save:
2178           break;
2179
2180         case DW_CFA_def_cfa_expression:
2181         case DW_CFA_expression:
2182           output_cfa_loc (cfi);
2183           break;
2184
2185         case DW_CFA_GNU_negative_offset_extended:
2186           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2187           gcc_unreachable ();
2188
2189         default:
2190           break;
2191         }
2192     }
2193 }
2194
2195 /* Output the call frame information used to record information
2196    that relates to calculating the frame pointer, and records the
2197    location of saved registers.  */
2198
2199 static void
2200 output_call_frame_info (int for_eh)
2201 {
2202   unsigned int i;
2203   dw_fde_ref fde;
2204   dw_cfi_ref cfi;
2205   char l1[20], l2[20], section_start_label[20];
2206   bool any_lsda_needed = false;
2207   char augmentation[6];
2208   int augmentation_size;
2209   int fde_encoding = DW_EH_PE_absptr;
2210   int per_encoding = DW_EH_PE_absptr;
2211   int lsda_encoding = DW_EH_PE_absptr;
2212   int return_reg;
2213
2214   /* Don't emit a CIE if there won't be any FDEs.  */
2215   if (fde_table_in_use == 0)
2216     return;
2217
2218   /* If we make FDEs linkonce, we may have to emit an empty label for
2219      an FDE that wouldn't otherwise be emitted.  We want to avoid
2220      having an FDE kept around when the function it refers to is
2221      discarded.  Example where this matters: a primary function
2222      template in C++ requires EH information, but an explicit
2223      specialization doesn't.  */
2224   if (TARGET_USES_WEAK_UNWIND_INFO
2225       && ! flag_asynchronous_unwind_tables
2226       && flag_exceptions
2227       && for_eh)
2228     for (i = 0; i < fde_table_in_use; i++)
2229       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2230           && !fde_table[i].uses_eh_lsda
2231           && ! DECL_WEAK (fde_table[i].decl))
2232         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2233                                       for_eh, /* empty */ 1);
2234
2235   /* If we don't have any functions we'll want to unwind out of, don't
2236      emit any EH unwind information.  Note that if exceptions aren't
2237      enabled, we won't have collected nothrow information, and if we
2238      asked for asynchronous tables, we always want this info.  */
2239   if (for_eh)
2240     {
2241       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2242
2243       for (i = 0; i < fde_table_in_use; i++)
2244         if (fde_table[i].uses_eh_lsda)
2245           any_eh_needed = any_lsda_needed = true;
2246         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2247           any_eh_needed = true;
2248         else if (! fde_table[i].nothrow
2249                  && ! fde_table[i].all_throwers_are_sibcalls)
2250           any_eh_needed = true;
2251
2252       if (! any_eh_needed)
2253         return;
2254     }
2255
2256   /* We're going to be generating comments, so turn on app.  */
2257   if (flag_debug_asm)
2258     app_enable ();
2259
2260   if (for_eh)
2261     switch_to_eh_frame_section ();
2262   else
2263     {
2264       if (!debug_frame_section)
2265         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2266                                            SECTION_DEBUG, NULL);
2267       switch_to_section (debug_frame_section);
2268     }
2269
2270   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2271   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2272
2273   /* Output the CIE.  */
2274   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2275   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2276   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2277     dw2_asm_output_data (4, 0xffffffff,
2278       "Initial length escape value indicating 64-bit DWARF extension");
2279   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2280                         "Length of Common Information Entry");
2281   ASM_OUTPUT_LABEL (asm_out_file, l1);
2282
2283   /* Now that the CIE pointer is PC-relative for EH,
2284      use 0 to identify the CIE.  */
2285   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2286                        (for_eh ? 0 : DWARF_CIE_ID),
2287                        "CIE Identifier Tag");
2288
2289   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2290
2291   augmentation[0] = 0;
2292   augmentation_size = 0;
2293   if (for_eh)
2294     {
2295       char *p;
2296
2297       /* Augmentation:
2298          z      Indicates that a uleb128 is present to size the
2299                 augmentation section.
2300          L      Indicates the encoding (and thus presence) of
2301                 an LSDA pointer in the FDE augmentation.
2302          R      Indicates a non-default pointer encoding for
2303                 FDE code pointers.
2304          P      Indicates the presence of an encoding + language
2305                 personality routine in the CIE augmentation.  */
2306
2307       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2308       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2309       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2310
2311       p = augmentation + 1;
2312       if (eh_personality_libfunc)
2313         {
2314           *p++ = 'P';
2315           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2316           assemble_external_libcall (eh_personality_libfunc);
2317         }
2318       if (any_lsda_needed)
2319         {
2320           *p++ = 'L';
2321           augmentation_size += 1;
2322         }
2323       if (fde_encoding != DW_EH_PE_absptr)
2324         {
2325           *p++ = 'R';
2326           augmentation_size += 1;
2327         }
2328       if (p > augmentation + 1)
2329         {
2330           augmentation[0] = 'z';
2331           *p = '\0';
2332         }
2333
2334       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2335       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2336         {
2337           int offset = (  4             /* Length */
2338                         + 4             /* CIE Id */
2339                         + 1             /* CIE version */
2340                         + strlen (augmentation) + 1     /* Augmentation */
2341                         + size_of_uleb128 (1)           /* Code alignment */
2342                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2343                         + 1             /* RA column */
2344                         + 1             /* Augmentation size */
2345                         + 1             /* Personality encoding */ );
2346           int pad = -offset & (PTR_SIZE - 1);
2347
2348           augmentation_size += pad;
2349
2350           /* Augmentations should be small, so there's scarce need to
2351              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2352           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2353         }
2354     }
2355
2356   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2357   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2358   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2359                                "CIE Data Alignment Factor");
2360
2361   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2362   if (DW_CIE_VERSION == 1)
2363     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2364   else
2365     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2366
2367   if (augmentation[0])
2368     {
2369       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2370       if (eh_personality_libfunc)
2371         {
2372           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2373                                eh_data_format_name (per_encoding));
2374           dw2_asm_output_encoded_addr_rtx (per_encoding,
2375                                            eh_personality_libfunc,
2376                                            true, NULL);
2377         }
2378
2379       if (any_lsda_needed)
2380         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2381                              eh_data_format_name (lsda_encoding));
2382
2383       if (fde_encoding != DW_EH_PE_absptr)
2384         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2385                              eh_data_format_name (fde_encoding));
2386     }
2387
2388   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2389     output_cfi (cfi, NULL, for_eh);
2390
2391   /* Pad the CIE out to an address sized boundary.  */
2392   ASM_OUTPUT_ALIGN (asm_out_file,
2393                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2394   ASM_OUTPUT_LABEL (asm_out_file, l2);
2395
2396   /* Loop through all of the FDE's.  */
2397   for (i = 0; i < fde_table_in_use; i++)
2398     {
2399       fde = &fde_table[i];
2400
2401       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2402       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2403           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2404           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2405           && !fde->uses_eh_lsda)
2406         continue;
2407
2408       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2409       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2410       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2411       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2412       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2413         dw2_asm_output_data (4, 0xffffffff,
2414                              "Initial length escape value indicating 64-bit DWARF extension");
2415       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2416                             "FDE Length");
2417       ASM_OUTPUT_LABEL (asm_out_file, l1);
2418
2419       if (for_eh)
2420         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2421       else
2422         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2423                                debug_frame_section, "FDE CIE offset");
2424
2425       if (for_eh)
2426         {
2427           rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2428           SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2429           dw2_asm_output_encoded_addr_rtx (fde_encoding,
2430                                            sym_ref,
2431                                            false,
2432                                            "FDE initial location");
2433           if (fde->dw_fde_switched_sections)
2434             {
2435               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2436                                       fde->dw_fde_unlikely_section_label);
2437               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2438                                       fde->dw_fde_hot_section_label);
2439               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2440               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2441               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2442                                                "FDE initial location");
2443               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2444                                     fde->dw_fde_hot_section_end_label,
2445                                     fde->dw_fde_hot_section_label,
2446                                     "FDE address range");
2447               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2448                                                "FDE initial location");
2449               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2450                                     fde->dw_fde_unlikely_section_end_label,
2451                                     fde->dw_fde_unlikely_section_label,
2452                                     "FDE address range");
2453             }
2454           else
2455             dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2456                                   fde->dw_fde_end, fde->dw_fde_begin,
2457                                   "FDE address range");
2458         }
2459       else
2460         {
2461           dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2462                                "FDE initial location");
2463           if (fde->dw_fde_switched_sections)
2464             {
2465               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2466                                    fde->dw_fde_hot_section_label,
2467                                    "FDE initial location");
2468               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2469                                     fde->dw_fde_hot_section_end_label,
2470                                     fde->dw_fde_hot_section_label,
2471                                     "FDE address range");
2472               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2473                                    fde->dw_fde_unlikely_section_label,
2474                                    "FDE initial location");
2475               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2476                                     fde->dw_fde_unlikely_section_end_label,
2477                                     fde->dw_fde_unlikely_section_label,
2478                                     "FDE address range");
2479             }
2480           else
2481             dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2482                                   fde->dw_fde_end, fde->dw_fde_begin,
2483                                   "FDE address range");
2484         }
2485
2486       if (augmentation[0])
2487         {
2488           if (any_lsda_needed)
2489             {
2490               int size = size_of_encoded_value (lsda_encoding);
2491
2492               if (lsda_encoding == DW_EH_PE_aligned)
2493                 {
2494                   int offset = (  4             /* Length */
2495                                 + 4             /* CIE offset */
2496                                 + 2 * size_of_encoded_value (fde_encoding)
2497                                 + 1             /* Augmentation size */ );
2498                   int pad = -offset & (PTR_SIZE - 1);
2499
2500                   size += pad;
2501                   gcc_assert (size_of_uleb128 (size) == 1);
2502                 }
2503
2504               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2505
2506               if (fde->uses_eh_lsda)
2507                 {
2508                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2509                                                fde->funcdef_number);
2510                   dw2_asm_output_encoded_addr_rtx (
2511                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2512                         false, "Language Specific Data Area");
2513                 }
2514               else
2515                 {
2516                   if (lsda_encoding == DW_EH_PE_aligned)
2517                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2518                   dw2_asm_output_data
2519                     (size_of_encoded_value (lsda_encoding), 0,
2520                      "Language Specific Data Area (none)");
2521                 }
2522             }
2523           else
2524             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2525         }
2526
2527       /* Loop through the Call Frame Instructions associated with
2528          this FDE.  */
2529       fde->dw_fde_current_label = fde->dw_fde_begin;
2530       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2531         output_cfi (cfi, fde, for_eh);
2532
2533       /* Pad the FDE out to an address sized boundary.  */
2534       ASM_OUTPUT_ALIGN (asm_out_file,
2535                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2536       ASM_OUTPUT_LABEL (asm_out_file, l2);
2537     }
2538
2539   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2540     dw2_asm_output_data (4, 0, "End of Table");
2541 #ifdef MIPS_DEBUGGING_INFO
2542   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2543      get a value of 0.  Putting .align 0 after the label fixes it.  */
2544   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2545 #endif
2546
2547   /* Turn off app to make assembly quicker.  */
2548   if (flag_debug_asm)
2549     app_disable ();
2550 }
2551
2552 /* Output a marker (i.e. a label) for the beginning of a function, before
2553    the prologue.  */
2554
2555 void
2556 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2557                           const char *file ATTRIBUTE_UNUSED)
2558 {
2559   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2560   char * dup_label;
2561   dw_fde_ref fde;
2562
2563   current_function_func_begin_label = NULL;
2564
2565 #ifdef TARGET_UNWIND_INFO
2566   /* ??? current_function_func_begin_label is also used by except.c
2567      for call-site information.  We must emit this label if it might
2568      be used.  */
2569   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2570       && ! dwarf2out_do_frame ())
2571     return;
2572 #else
2573   if (! dwarf2out_do_frame ())
2574     return;
2575 #endif
2576
2577   switch_to_section (function_section (current_function_decl));
2578   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2579                                current_function_funcdef_no);
2580   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2581                           current_function_funcdef_no);
2582   dup_label = xstrdup (label);
2583   current_function_func_begin_label = dup_label;
2584
2585 #ifdef TARGET_UNWIND_INFO
2586   /* We can elide the fde allocation if we're not emitting debug info.  */
2587   if (! dwarf2out_do_frame ())
2588     return;
2589 #endif
2590
2591   /* Expand the fde table if necessary.  */
2592   if (fde_table_in_use == fde_table_allocated)
2593     {
2594       fde_table_allocated += FDE_TABLE_INCREMENT;
2595       fde_table = ggc_realloc (fde_table,
2596                                fde_table_allocated * sizeof (dw_fde_node));
2597       memset (fde_table + fde_table_in_use, 0,
2598               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2599     }
2600
2601   /* Record the FDE associated with this function.  */
2602   current_funcdef_fde = fde_table_in_use;
2603
2604   /* Add the new FDE at the end of the fde_table.  */
2605   fde = &fde_table[fde_table_in_use++];
2606   fde->decl = current_function_decl;
2607   fde->dw_fde_begin = dup_label;
2608   fde->dw_fde_current_label = dup_label;
2609   fde->dw_fde_hot_section_label = NULL;
2610   fde->dw_fde_hot_section_end_label = NULL;
2611   fde->dw_fde_unlikely_section_label = NULL;
2612   fde->dw_fde_unlikely_section_end_label = NULL;
2613   fde->dw_fde_switched_sections = false;
2614   fde->dw_fde_end = NULL;
2615   fde->dw_fde_cfi = NULL;
2616   fde->funcdef_number = current_function_funcdef_no;
2617   fde->nothrow = TREE_NOTHROW (current_function_decl);
2618   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2619   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2620
2621   args_size = old_args_size = 0;
2622
2623   /* We only want to output line number information for the genuine dwarf2
2624      prologue case, not the eh frame case.  */
2625 #ifdef DWARF2_DEBUGGING_INFO
2626   if (file)
2627     dwarf2out_source_line (line, file);
2628 #endif
2629 }
2630
2631 /* Output a marker (i.e. a label) for the absolute end of the generated code
2632    for a function definition.  This gets called *after* the epilogue code has
2633    been generated.  */
2634
2635 void
2636 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2637                         const char *file ATTRIBUTE_UNUSED)
2638 {
2639   dw_fde_ref fde;
2640   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2641
2642   /* Output a label to mark the endpoint of the code generated for this
2643      function.  */
2644   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2645                                current_function_funcdef_no);
2646   ASM_OUTPUT_LABEL (asm_out_file, label);
2647   fde = &fde_table[fde_table_in_use - 1];
2648   fde->dw_fde_end = xstrdup (label);
2649 }
2650
2651 void
2652 dwarf2out_frame_init (void)
2653 {
2654   /* Allocate the initial hunk of the fde_table.  */
2655   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2656   fde_table_allocated = FDE_TABLE_INCREMENT;
2657   fde_table_in_use = 0;
2658
2659   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2660      sake of lookup_cfa.  */
2661
2662   /* On entry, the Canonical Frame Address is at SP.  */
2663   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2664
2665 #ifdef DWARF2_UNWIND_INFO
2666   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2667     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2668 #endif
2669 }
2670
2671 void
2672 dwarf2out_frame_finish (void)
2673 {
2674   /* Output call frame information.  */
2675   if (DWARF2_FRAME_INFO)
2676     output_call_frame_info (0);
2677
2678 #ifndef TARGET_UNWIND_INFO
2679   /* Output another copy for the unwinder.  */
2680   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2681     output_call_frame_info (1);
2682 #endif
2683 }
2684 #endif
2685 \f
2686 /* And now, the subset of the debugging information support code necessary
2687    for emitting location expressions.  */
2688
2689 /* Data about a single source file.  */
2690 struct dwarf_file_data GTY(())
2691 {
2692   const char * filename;
2693   int emitted_number;
2694 };
2695
2696 /* We need some way to distinguish DW_OP_addr with a direct symbol
2697    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2698 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2699
2700
2701 typedef struct dw_val_struct *dw_val_ref;
2702 typedef struct die_struct *dw_die_ref;
2703 typedef const struct die_struct *const_dw_die_ref;
2704 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2705 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2706
2707 /* Each DIE may have a series of attribute/value pairs.  Values
2708    can take on several forms.  The forms that are used in this
2709    implementation are listed below.  */
2710
2711 enum dw_val_class
2712 {
2713   dw_val_class_addr,
2714   dw_val_class_offset,
2715   dw_val_class_loc,
2716   dw_val_class_loc_list,
2717   dw_val_class_range_list,
2718   dw_val_class_const,
2719   dw_val_class_unsigned_const,
2720   dw_val_class_long_long,
2721   dw_val_class_vec,
2722   dw_val_class_flag,
2723   dw_val_class_die_ref,
2724   dw_val_class_fde_ref,
2725   dw_val_class_lbl_id,
2726   dw_val_class_lineptr,
2727   dw_val_class_str,
2728   dw_val_class_macptr,
2729   dw_val_class_file
2730 };
2731
2732 /* Describe a double word constant value.  */
2733 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2734
2735 typedef struct dw_long_long_struct GTY(())
2736 {
2737   unsigned long hi;
2738   unsigned long low;
2739 }
2740 dw_long_long_const;
2741
2742 /* Describe a floating point constant value, or a vector constant value.  */
2743
2744 typedef struct dw_vec_struct GTY(())
2745 {
2746   unsigned char * GTY((length ("%h.length"))) array;
2747   unsigned length;
2748   unsigned elt_size;
2749 }
2750 dw_vec_const;
2751
2752 /* The dw_val_node describes an attribute's value, as it is
2753    represented internally.  */
2754
2755 typedef struct dw_val_struct GTY(())
2756 {
2757   enum dw_val_class val_class;
2758   union dw_val_struct_union
2759     {
2760       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2761       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2762       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2763       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2764       HOST_WIDE_INT GTY ((default)) val_int;
2765       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2766       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2767       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2768       struct dw_val_die_union
2769         {
2770           dw_die_ref die;
2771           int external;
2772         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2773       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2774       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2775       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2776       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2777       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2778     }
2779   GTY ((desc ("%1.val_class"))) v;
2780 }
2781 dw_val_node;
2782
2783 /* Locations in memory are described using a sequence of stack machine
2784    operations.  */
2785
2786 typedef struct dw_loc_descr_struct GTY(())
2787 {
2788   dw_loc_descr_ref dw_loc_next;
2789   enum dwarf_location_atom dw_loc_opc;
2790   dw_val_node dw_loc_oprnd1;
2791   dw_val_node dw_loc_oprnd2;
2792   int dw_loc_addr;
2793 }
2794 dw_loc_descr_node;
2795
2796 /* Location lists are ranges + location descriptions for that range,
2797    so you can track variables that are in different places over
2798    their entire life.  */
2799 typedef struct dw_loc_list_struct GTY(())
2800 {
2801   dw_loc_list_ref dw_loc_next;
2802   const char *begin; /* Label for begin address of range */
2803   const char *end;  /* Label for end address of range */
2804   char *ll_symbol; /* Label for beginning of location list.
2805                       Only on head of list */
2806   const char *section; /* Section this loclist is relative to */
2807   dw_loc_descr_ref expr;
2808 } dw_loc_list_node;
2809
2810 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2811
2812 static const char *dwarf_stack_op_name (unsigned);
2813 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2814                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2815 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2816 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2817 static unsigned long size_of_locs (dw_loc_descr_ref);
2818 static void output_loc_operands (dw_loc_descr_ref);
2819 static void output_loc_sequence (dw_loc_descr_ref);
2820
2821 /* Convert a DWARF stack opcode into its string name.  */
2822
2823 static const char *
2824 dwarf_stack_op_name (unsigned int op)
2825 {
2826   switch (op)
2827     {
2828     case DW_OP_addr:
2829     case INTERNAL_DW_OP_tls_addr:
2830       return "DW_OP_addr";
2831     case DW_OP_deref:
2832       return "DW_OP_deref";
2833     case DW_OP_const1u:
2834       return "DW_OP_const1u";
2835     case DW_OP_const1s:
2836       return "DW_OP_const1s";
2837     case DW_OP_const2u:
2838       return "DW_OP_const2u";
2839     case DW_OP_const2s:
2840       return "DW_OP_const2s";
2841     case DW_OP_const4u:
2842       return "DW_OP_const4u";
2843     case DW_OP_const4s:
2844       return "DW_OP_const4s";
2845     case DW_OP_const8u:
2846       return "DW_OP_const8u";
2847     case DW_OP_const8s:
2848       return "DW_OP_const8s";
2849     case DW_OP_constu:
2850       return "DW_OP_constu";
2851     case DW_OP_consts:
2852       return "DW_OP_consts";
2853     case DW_OP_dup:
2854       return "DW_OP_dup";
2855     case DW_OP_drop:
2856       return "DW_OP_drop";
2857     case DW_OP_over:
2858       return "DW_OP_over";
2859     case DW_OP_pick:
2860       return "DW_OP_pick";
2861     case DW_OP_swap:
2862       return "DW_OP_swap";
2863     case DW_OP_rot:
2864       return "DW_OP_rot";
2865     case DW_OP_xderef:
2866       return "DW_OP_xderef";
2867     case DW_OP_abs:
2868       return "DW_OP_abs";
2869     case DW_OP_and:
2870       return "DW_OP_and";
2871     case DW_OP_div:
2872       return "DW_OP_div";
2873     case DW_OP_minus:
2874       return "DW_OP_minus";
2875     case DW_OP_mod:
2876       return "DW_OP_mod";
2877     case DW_OP_mul:
2878       return "DW_OP_mul";
2879     case DW_OP_neg:
2880       return "DW_OP_neg";
2881     case DW_OP_not:
2882       return "DW_OP_not";
2883     case DW_OP_or:
2884       return "DW_OP_or";
2885     case DW_OP_plus:
2886       return "DW_OP_plus";
2887     case DW_OP_plus_uconst:
2888       return "DW_OP_plus_uconst";
2889     case DW_OP_shl:
2890       return "DW_OP_shl";
2891     case DW_OP_shr:
2892       return "DW_OP_shr";
2893     case DW_OP_shra:
2894       return "DW_OP_shra";
2895     case DW_OP_xor:
2896       return "DW_OP_xor";
2897     case DW_OP_bra:
2898       return "DW_OP_bra";
2899     case DW_OP_eq:
2900       return "DW_OP_eq";
2901     case DW_OP_ge:
2902       return "DW_OP_ge";
2903     case DW_OP_gt:
2904       return "DW_OP_gt";
2905     case DW_OP_le:
2906       return "DW_OP_le";
2907     case DW_OP_lt:
2908       return "DW_OP_lt";
2909     case DW_OP_ne:
2910       return "DW_OP_ne";
2911     case DW_OP_skip:
2912       return "DW_OP_skip";
2913     case DW_OP_lit0:
2914       return "DW_OP_lit0";
2915     case DW_OP_lit1:
2916       return "DW_OP_lit1";
2917     case DW_OP_lit2:
2918       return "DW_OP_lit2";
2919     case DW_OP_lit3:
2920       return "DW_OP_lit3";
2921     case DW_OP_lit4:
2922       return "DW_OP_lit4";
2923     case DW_OP_lit5:
2924       return "DW_OP_lit5";
2925     case DW_OP_lit6:
2926       return "DW_OP_lit6";
2927     case DW_OP_lit7:
2928       return "DW_OP_lit7";
2929     case DW_OP_lit8:
2930       return "DW_OP_lit8";
2931     case DW_OP_lit9:
2932       return "DW_OP_lit9";
2933     case DW_OP_lit10:
2934       return "DW_OP_lit10";
2935     case DW_OP_lit11:
2936       return "DW_OP_lit11";
2937     case DW_OP_lit12:
2938       return "DW_OP_lit12";
2939     case DW_OP_lit13:
2940       return "DW_OP_lit13";
2941     case DW_OP_lit14:
2942       return "DW_OP_lit14";
2943     case DW_OP_lit15:
2944       return "DW_OP_lit15";
2945     case DW_OP_lit16:
2946       return "DW_OP_lit16";
2947     case DW_OP_lit17:
2948       return "DW_OP_lit17";
2949     case DW_OP_lit18:
2950       return "DW_OP_lit18";
2951     case DW_OP_lit19:
2952       return "DW_OP_lit19";
2953     case DW_OP_lit20:
2954       return "DW_OP_lit20";
2955     case DW_OP_lit21:
2956       return "DW_OP_lit21";
2957     case DW_OP_lit22:
2958       return "DW_OP_lit22";
2959     case DW_OP_lit23:
2960       return "DW_OP_lit23";
2961     case DW_OP_lit24:
2962       return "DW_OP_lit24";
2963     case DW_OP_lit25:
2964       return "DW_OP_lit25";
2965     case DW_OP_lit26:
2966       return "DW_OP_lit26";
2967     case DW_OP_lit27:
2968       return "DW_OP_lit27";
2969     case DW_OP_lit28:
2970       return "DW_OP_lit28";
2971     case DW_OP_lit29:
2972       return "DW_OP_lit29";
2973     case DW_OP_lit30:
2974       return "DW_OP_lit30";
2975     case DW_OP_lit31:
2976       return "DW_OP_lit31";
2977     case DW_OP_reg0:
2978       return "DW_OP_reg0";
2979     case DW_OP_reg1:
2980       return "DW_OP_reg1";
2981     case DW_OP_reg2:
2982       return "DW_OP_reg2";
2983     case DW_OP_reg3:
2984       return "DW_OP_reg3";
2985     case DW_OP_reg4:
2986       return "DW_OP_reg4";
2987     case DW_OP_reg5:
2988       return "DW_OP_reg5";
2989     case DW_OP_reg6:
2990       return "DW_OP_reg6";
2991     case DW_OP_reg7:
2992       return "DW_OP_reg7";
2993     case DW_OP_reg8:
2994       return "DW_OP_reg8";
2995     case DW_OP_reg9:
2996       return "DW_OP_reg9";
2997     case DW_OP_reg10:
2998       return "DW_OP_reg10";
2999     case DW_OP_reg11:
3000       return "DW_OP_reg11";
3001     case DW_OP_reg12:
3002       return "DW_OP_reg12";
3003     case DW_OP_reg13:
3004       return "DW_OP_reg13";
3005     case DW_OP_reg14:
3006       return "DW_OP_reg14";
3007     case DW_OP_reg15:
3008       return "DW_OP_reg15";
3009     case DW_OP_reg16:
3010       return "DW_OP_reg16";
3011     case DW_OP_reg17:
3012       return "DW_OP_reg17";
3013     case DW_OP_reg18:
3014       return "DW_OP_reg18";
3015     case DW_OP_reg19:
3016       return "DW_OP_reg19";
3017     case DW_OP_reg20:
3018       return "DW_OP_reg20";
3019     case DW_OP_reg21:
3020       return "DW_OP_reg21";
3021     case DW_OP_reg22:
3022       return "DW_OP_reg22";
3023     case DW_OP_reg23:
3024       return "DW_OP_reg23";
3025     case DW_OP_reg24:
3026       return "DW_OP_reg24";
3027     case DW_OP_reg25:
3028       return "DW_OP_reg25";
3029     case DW_OP_reg26:
3030       return "DW_OP_reg26";
3031     case DW_OP_reg27:
3032       return "DW_OP_reg27";
3033     case DW_OP_reg28:
3034       return "DW_OP_reg28";
3035     case DW_OP_reg29:
3036       return "DW_OP_reg29";
3037     case DW_OP_reg30:
3038       return "DW_OP_reg30";
3039     case DW_OP_reg31:
3040       return "DW_OP_reg31";
3041     case DW_OP_breg0:
3042       return "DW_OP_breg0";
3043     case DW_OP_breg1:
3044       return "DW_OP_breg1";
3045     case DW_OP_breg2:
3046       return "DW_OP_breg2";
3047     case DW_OP_breg3:
3048       return "DW_OP_breg3";
3049     case DW_OP_breg4:
3050       return "DW_OP_breg4";
3051     case DW_OP_breg5:
3052       return "DW_OP_breg5";
3053     case DW_OP_breg6:
3054       return "DW_OP_breg6";
3055     case DW_OP_breg7:
3056       return "DW_OP_breg7";
3057     case DW_OP_breg8:
3058       return "DW_OP_breg8";
3059     case DW_OP_breg9:
3060       return "DW_OP_breg9";
3061     case DW_OP_breg10:
3062       return "DW_OP_breg10";
3063     case DW_OP_breg11:
3064       return "DW_OP_breg11";
3065     case DW_OP_breg12:
3066       return "DW_OP_breg12";
3067     case DW_OP_breg13:
3068       return "DW_OP_breg13";
3069     case DW_OP_breg14:
3070       return "DW_OP_breg14";
3071     case DW_OP_breg15:
3072       return "DW_OP_breg15";
3073     case DW_OP_breg16:
3074       return "DW_OP_breg16";
3075     case DW_OP_breg17:
3076       return "DW_OP_breg17";
3077     case DW_OP_breg18:
3078       return "DW_OP_breg18";
3079     case DW_OP_breg19:
3080       return "DW_OP_breg19";
3081     case DW_OP_breg20:
3082       return "DW_OP_breg20";
3083     case DW_OP_breg21:
3084       return "DW_OP_breg21";
3085     case DW_OP_breg22:
3086       return "DW_OP_breg22";
3087     case DW_OP_breg23:
3088       return "DW_OP_breg23";
3089     case DW_OP_breg24:
3090       return "DW_OP_breg24";
3091     case DW_OP_breg25:
3092       return "DW_OP_breg25";
3093     case DW_OP_breg26:
3094       return "DW_OP_breg26";
3095     case DW_OP_breg27:
3096       return "DW_OP_breg27";
3097     case DW_OP_breg28:
3098       return "DW_OP_breg28";
3099     case DW_OP_breg29:
3100       return "DW_OP_breg29";
3101     case DW_OP_breg30:
3102       return "DW_OP_breg30";
3103     case DW_OP_breg31:
3104       return "DW_OP_breg31";
3105     case DW_OP_regx:
3106       return "DW_OP_regx";
3107     case DW_OP_fbreg:
3108       return "DW_OP_fbreg";
3109     case DW_OP_bregx:
3110       return "DW_OP_bregx";
3111     case DW_OP_piece:
3112       return "DW_OP_piece";
3113     case DW_OP_deref_size:
3114       return "DW_OP_deref_size";
3115     case DW_OP_xderef_size:
3116       return "DW_OP_xderef_size";
3117     case DW_OP_nop:
3118       return "DW_OP_nop";
3119     case DW_OP_push_object_address:
3120       return "DW_OP_push_object_address";
3121     case DW_OP_call2:
3122       return "DW_OP_call2";
3123     case DW_OP_call4:
3124       return "DW_OP_call4";
3125     case DW_OP_call_ref:
3126       return "DW_OP_call_ref";
3127     case DW_OP_GNU_push_tls_address:
3128       return "DW_OP_GNU_push_tls_address";
3129     case DW_OP_GNU_uninit:
3130       return "DW_OP_GNU_uninit";
3131     default:
3132       return "OP_<unknown>";
3133     }
3134 }
3135
3136 /* Return a pointer to a newly allocated location description.  Location
3137    descriptions are simple expression terms that can be strung
3138    together to form more complicated location (address) descriptions.  */
3139
3140 static inline dw_loc_descr_ref
3141 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3142                unsigned HOST_WIDE_INT oprnd2)
3143 {
3144   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3145
3146   descr->dw_loc_opc = op;
3147   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3148   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3149   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3150   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3151
3152   return descr;
3153 }
3154
3155 /* Add a location description term to a location description expression.  */
3156
3157 static inline void
3158 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3159 {
3160   dw_loc_descr_ref *d;
3161
3162   /* Find the end of the chain.  */
3163   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3164     ;
3165
3166   *d = descr;
3167 }
3168
3169 /* Return the size of a location descriptor.  */
3170
3171 static unsigned long
3172 size_of_loc_descr (dw_loc_descr_ref loc)
3173 {
3174   unsigned long size = 1;
3175
3176   switch (loc->dw_loc_opc)
3177     {
3178     case DW_OP_addr:
3179     case INTERNAL_DW_OP_tls_addr:
3180       size += DWARF2_ADDR_SIZE;
3181       break;
3182     case DW_OP_const1u:
3183     case DW_OP_const1s:
3184       size += 1;
3185       break;
3186     case DW_OP_const2u:
3187     case DW_OP_const2s:
3188       size += 2;
3189       break;
3190     case DW_OP_const4u:
3191     case DW_OP_const4s:
3192       size += 4;
3193       break;
3194     case DW_OP_const8u:
3195     case DW_OP_const8s:
3196       size += 8;
3197       break;
3198     case DW_OP_constu:
3199       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3200       break;
3201     case DW_OP_consts:
3202       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3203       break;
3204     case DW_OP_pick:
3205       size += 1;
3206       break;
3207     case DW_OP_plus_uconst:
3208       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3209       break;
3210     case DW_OP_skip:
3211     case DW_OP_bra:
3212       size += 2;
3213       break;
3214     case DW_OP_breg0:
3215     case DW_OP_breg1:
3216     case DW_OP_breg2:
3217     case DW_OP_breg3:
3218     case DW_OP_breg4:
3219     case DW_OP_breg5:
3220     case DW_OP_breg6:
3221     case DW_OP_breg7:
3222     case DW_OP_breg8:
3223     case DW_OP_breg9:
3224     case DW_OP_breg10:
3225     case DW_OP_breg11:
3226     case DW_OP_breg12:
3227     case DW_OP_breg13:
3228     case DW_OP_breg14:
3229     case DW_OP_breg15:
3230     case DW_OP_breg16:
3231     case DW_OP_breg17:
3232     case DW_OP_breg18:
3233     case DW_OP_breg19:
3234     case DW_OP_breg20:
3235     case DW_OP_breg21:
3236     case DW_OP_breg22:
3237     case DW_OP_breg23:
3238     case DW_OP_breg24:
3239     case DW_OP_breg25:
3240     case DW_OP_breg26:
3241     case DW_OP_breg27:
3242     case DW_OP_breg28:
3243     case DW_OP_breg29:
3244     case DW_OP_breg30:
3245     case DW_OP_breg31:
3246       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3247       break;
3248     case DW_OP_regx:
3249       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3250       break;
3251     case DW_OP_fbreg:
3252       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3253       break;
3254     case DW_OP_bregx:
3255       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3256       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3257       break;
3258     case DW_OP_piece:
3259       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3260       break;
3261     case DW_OP_deref_size:
3262     case DW_OP_xderef_size:
3263       size += 1;
3264       break;
3265     case DW_OP_call2:
3266       size += 2;
3267       break;
3268     case DW_OP_call4:
3269       size += 4;
3270       break;
3271     case DW_OP_call_ref:
3272       size += DWARF2_ADDR_SIZE;
3273       break;
3274     default:
3275       break;
3276     }
3277
3278   return size;
3279 }
3280
3281 /* Return the size of a series of location descriptors.  */
3282
3283 static unsigned long
3284 size_of_locs (dw_loc_descr_ref loc)
3285 {
3286   dw_loc_descr_ref l;
3287   unsigned long size;
3288
3289   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3290      field, to avoid writing to a PCH file.  */
3291   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3292     {
3293       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3294         break;
3295       size += size_of_loc_descr (l);
3296     }
3297   if (! l)
3298     return size;
3299
3300   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3301     {
3302       l->dw_loc_addr = size;
3303       size += size_of_loc_descr (l);
3304     }
3305
3306   return size;
3307 }
3308
3309 /* Output location description stack opcode's operands (if any).  */
3310
3311 static void
3312 output_loc_operands (dw_loc_descr_ref loc)
3313 {
3314   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3315   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3316
3317   switch (loc->dw_loc_opc)
3318     {
3319 #ifdef DWARF2_DEBUGGING_INFO
3320     case DW_OP_addr:
3321       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3322       break;
3323     case DW_OP_const2u:
3324     case DW_OP_const2s:
3325       dw2_asm_output_data (2, val1->v.val_int, NULL);
3326       break;
3327     case DW_OP_const4u:
3328     case DW_OP_const4s:
3329       dw2_asm_output_data (4, val1->v.val_int, NULL);
3330       break;
3331     case DW_OP_const8u:
3332     case DW_OP_const8s:
3333       gcc_assert (HOST_BITS_PER_LONG >= 64);
3334       dw2_asm_output_data (8, val1->v.val_int, NULL);
3335       break;
3336     case DW_OP_skip:
3337     case DW_OP_bra:
3338       {
3339         int offset;
3340
3341         gcc_assert (val1->val_class == dw_val_class_loc);
3342         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3343
3344         dw2_asm_output_data (2, offset, NULL);
3345       }
3346       break;
3347 #else
3348     case DW_OP_addr:
3349     case DW_OP_const2u:
3350     case DW_OP_const2s:
3351     case DW_OP_const4u:
3352     case DW_OP_const4s:
3353     case DW_OP_const8u:
3354     case DW_OP_const8s:
3355     case DW_OP_skip:
3356     case DW_OP_bra:
3357       /* We currently don't make any attempt to make sure these are
3358          aligned properly like we do for the main unwind info, so
3359          don't support emitting things larger than a byte if we're
3360          only doing unwinding.  */
3361       gcc_unreachable ();
3362 #endif
3363     case DW_OP_const1u:
3364     case DW_OP_const1s:
3365       dw2_asm_output_data (1, val1->v.val_int, NULL);
3366       break;
3367     case DW_OP_constu:
3368       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3369       break;
3370     case DW_OP_consts:
3371       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3372       break;
3373     case DW_OP_pick:
3374       dw2_asm_output_data (1, val1->v.val_int, NULL);
3375       break;
3376     case DW_OP_plus_uconst:
3377       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3378       break;
3379     case DW_OP_breg0:
3380     case DW_OP_breg1:
3381     case DW_OP_breg2:
3382     case DW_OP_breg3:
3383     case DW_OP_breg4:
3384     case DW_OP_breg5:
3385     case DW_OP_breg6:
3386     case DW_OP_breg7:
3387     case DW_OP_breg8:
3388     case DW_OP_breg9:
3389     case DW_OP_breg10:
3390     case DW_OP_breg11:
3391     case DW_OP_breg12:
3392     case DW_OP_breg13:
3393     case DW_OP_breg14:
3394     case DW_OP_breg15:
3395     case DW_OP_breg16:
3396     case DW_OP_breg17:
3397     case DW_OP_breg18:
3398     case DW_OP_breg19:
3399     case DW_OP_breg20:
3400     case DW_OP_breg21:
3401     case DW_OP_breg22:
3402     case DW_OP_breg23:
3403     case DW_OP_breg24:
3404     case DW_OP_breg25:
3405     case DW_OP_breg26:
3406     case DW_OP_breg27:
3407     case DW_OP_breg28:
3408     case DW_OP_breg29:
3409     case DW_OP_breg30:
3410     case DW_OP_breg31:
3411       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3412       break;
3413     case DW_OP_regx:
3414       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3415       break;
3416     case DW_OP_fbreg:
3417       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3418       break;
3419     case DW_OP_bregx:
3420       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3421       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3422       break;
3423     case DW_OP_piece:
3424       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3425       break;
3426     case DW_OP_deref_size:
3427     case DW_OP_xderef_size:
3428       dw2_asm_output_data (1, val1->v.val_int, NULL);
3429       break;
3430
3431     case INTERNAL_DW_OP_tls_addr:
3432       if (targetm.asm_out.output_dwarf_dtprel)
3433         {
3434           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3435                                                DWARF2_ADDR_SIZE,
3436                                                val1->v.val_addr);
3437           fputc ('\n', asm_out_file);
3438         }
3439       else
3440         gcc_unreachable ();
3441       break;
3442
3443     default:
3444       /* Other codes have no operands.  */
3445       break;
3446     }
3447 }
3448
3449 /* Output a sequence of location operations.  */
3450
3451 static void
3452 output_loc_sequence (dw_loc_descr_ref loc)
3453 {
3454   for (; loc != NULL; loc = loc->dw_loc_next)
3455     {
3456       /* Output the opcode.  */
3457       dw2_asm_output_data (1, loc->dw_loc_opc,
3458                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3459
3460       /* Output the operand(s) (if any).  */
3461       output_loc_operands (loc);
3462     }
3463 }
3464
3465 /* This routine will generate the correct assembly data for a location
3466    description based on a cfi entry with a complex address.  */
3467
3468 static void
3469 output_cfa_loc (dw_cfi_ref cfi)
3470 {
3471   dw_loc_descr_ref loc;
3472   unsigned long size;
3473
3474   /* Output the size of the block.  */
3475   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3476   size = size_of_locs (loc);
3477   dw2_asm_output_data_uleb128 (size, NULL);
3478
3479   /* Now output the operations themselves.  */
3480   output_loc_sequence (loc);
3481 }
3482
3483 /* This function builds a dwarf location descriptor sequence from a
3484    dw_cfa_location, adding the given OFFSET to the result of the
3485    expression.  */
3486
3487 static struct dw_loc_descr_struct *
3488 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3489 {
3490   struct dw_loc_descr_struct *head, *tmp;
3491
3492   offset += cfa->offset;
3493
3494   if (cfa->indirect)
3495     {
3496       if (cfa->base_offset)
3497         {
3498           if (cfa->reg <= 31)
3499             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3500           else
3501             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3502         }
3503       else if (cfa->reg <= 31)
3504         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3505       else
3506         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3507
3508       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3509       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3510       add_loc_descr (&head, tmp);
3511       if (offset != 0)
3512         {
3513           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3514           add_loc_descr (&head, tmp);
3515         }
3516     }
3517   else
3518     {
3519       if (offset == 0)
3520         if (cfa->reg <= 31)
3521           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3522         else
3523           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3524       else if (cfa->reg <= 31)
3525         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3526       else
3527         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3528     }
3529
3530   return head;
3531 }
3532
3533 /* This function fills in aa dw_cfa_location structure from a dwarf location
3534    descriptor sequence.  */
3535
3536 static void
3537 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3538 {
3539   struct dw_loc_descr_struct *ptr;
3540   cfa->offset = 0;
3541   cfa->base_offset = 0;
3542   cfa->indirect = 0;
3543   cfa->reg = -1;
3544
3545   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3546     {
3547       enum dwarf_location_atom op = ptr->dw_loc_opc;
3548
3549       switch (op)
3550         {
3551         case DW_OP_reg0:
3552         case DW_OP_reg1:
3553         case DW_OP_reg2:
3554         case DW_OP_reg3:
3555         case DW_OP_reg4:
3556         case DW_OP_reg5:
3557         case DW_OP_reg6:
3558         case DW_OP_reg7:
3559         case DW_OP_reg8:
3560         case DW_OP_reg9:
3561         case DW_OP_reg10:
3562         case DW_OP_reg11:
3563         case DW_OP_reg12:
3564         case DW_OP_reg13:
3565         case DW_OP_reg14:
3566         case DW_OP_reg15:
3567         case DW_OP_reg16:
3568         case DW_OP_reg17:
3569         case DW_OP_reg18:
3570         case DW_OP_reg19:
3571         case DW_OP_reg20:
3572         case DW_OP_reg21:
3573         case DW_OP_reg22:
3574         case DW_OP_reg23:
3575         case DW_OP_reg24:
3576         case DW_OP_reg25:
3577         case DW_OP_reg26:
3578         case DW_OP_reg27:
3579         case DW_OP_reg28:
3580         case DW_OP_reg29:
3581         case DW_OP_reg30:
3582         case DW_OP_reg31:
3583           cfa->reg = op - DW_OP_reg0;
3584           break;
3585         case DW_OP_regx:
3586           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3587           break;
3588         case DW_OP_breg0:
3589         case DW_OP_breg1:
3590         case DW_OP_breg2:
3591         case DW_OP_breg3:
3592         case DW_OP_breg4:
3593         case DW_OP_breg5:
3594         case DW_OP_breg6:
3595         case DW_OP_breg7:
3596         case DW_OP_breg8:
3597         case DW_OP_breg9:
3598         case DW_OP_breg10:
3599         case DW_OP_breg11:
3600         case DW_OP_breg12:
3601         case DW_OP_breg13:
3602         case DW_OP_breg14:
3603         case DW_OP_breg15:
3604         case DW_OP_breg16:
3605         case DW_OP_breg17:
3606         case DW_OP_breg18:
3607         case DW_OP_breg19:
3608         case DW_OP_breg20:
3609         case DW_OP_breg21:
3610         case DW_OP_breg22:
3611         case DW_OP_breg23:
3612         case DW_OP_breg24:
3613         case DW_OP_breg25:
3614         case DW_OP_breg26:
3615         case DW_OP_breg27:
3616         case DW_OP_breg28:
3617         case DW_OP_breg29:
3618         case DW_OP_breg30:
3619         case DW_OP_breg31:
3620           cfa->reg = op - DW_OP_breg0;
3621           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3622           break;
3623         case DW_OP_bregx:
3624           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3625           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3626           break;
3627         case DW_OP_deref:
3628           cfa->indirect = 1;
3629           break;
3630         case DW_OP_plus_uconst:
3631           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3632           break;
3633         default:
3634           internal_error ("DW_LOC_OP %s not implemented",
3635                           dwarf_stack_op_name (ptr->dw_loc_opc));
3636         }
3637     }
3638 }
3639 #endif /* .debug_frame support */
3640 \f
3641 /* And now, the support for symbolic debugging information.  */
3642 #ifdef DWARF2_DEBUGGING_INFO
3643
3644 /* .debug_str support.  */
3645 static int output_indirect_string (void **, void *);
3646
3647 static void dwarf2out_init (const char *);
3648 static void dwarf2out_finish (const char *);
3649 static void dwarf2out_define (unsigned int, const char *);
3650 static void dwarf2out_undef (unsigned int, const char *);
3651 static void dwarf2out_start_source_file (unsigned, const char *);
3652 static void dwarf2out_end_source_file (unsigned);
3653 static void dwarf2out_begin_block (unsigned, unsigned);
3654 static void dwarf2out_end_block (unsigned, unsigned);
3655 static bool dwarf2out_ignore_block (const_tree);
3656 static void dwarf2out_global_decl (tree);
3657 static void dwarf2out_type_decl (tree, int);
3658 static void dwarf2out_imported_module_or_decl (tree, tree);
3659 static void dwarf2out_abstract_function (tree);
3660 static void dwarf2out_var_location (rtx);
3661 static void dwarf2out_begin_function (tree);
3662 static void dwarf2out_switch_text_section (void);
3663
3664 /* The debug hooks structure.  */
3665
3666 const struct gcc_debug_hooks dwarf2_debug_hooks =
3667 {
3668   dwarf2out_init,
3669   dwarf2out_finish,
3670   dwarf2out_define,
3671   dwarf2out_undef,
3672   dwarf2out_start_source_file,
3673   dwarf2out_end_source_file,
3674   dwarf2out_begin_block,
3675   dwarf2out_end_block,
3676   dwarf2out_ignore_block,
3677   dwarf2out_source_line,
3678   dwarf2out_begin_prologue,
3679   debug_nothing_int_charstar,   /* end_prologue */
3680   dwarf2out_end_epilogue,
3681   dwarf2out_begin_function,
3682   debug_nothing_int,            /* end_function */
3683   dwarf2out_decl,               /* function_decl */
3684   dwarf2out_global_decl,
3685   dwarf2out_type_decl,          /* type_decl */
3686   dwarf2out_imported_module_or_decl,
3687   debug_nothing_tree,           /* deferred_inline_function */
3688   /* The DWARF 2 backend tries to reduce debugging bloat by not
3689      emitting the abstract description of inline functions until
3690      something tries to reference them.  */
3691   dwarf2out_abstract_function,  /* outlining_inline_function */
3692   debug_nothing_rtx,            /* label */
3693   debug_nothing_int,            /* handle_pch */
3694   dwarf2out_var_location,
3695   dwarf2out_switch_text_section,
3696   1                             /* start_end_main_source_file */
3697 };
3698 #endif
3699 \f
3700 /* NOTE: In the comments in this file, many references are made to
3701    "Debugging Information Entries".  This term is abbreviated as `DIE'
3702    throughout the remainder of this file.  */
3703
3704 /* An internal representation of the DWARF output is built, and then
3705    walked to generate the DWARF debugging info.  The walk of the internal
3706    representation is done after the entire program has been compiled.
3707    The types below are used to describe the internal representation.  */
3708
3709 /* Various DIE's use offsets relative to the beginning of the
3710    .debug_info section to refer to each other.  */
3711
3712 typedef long int dw_offset;
3713
3714 /* Define typedefs here to avoid circular dependencies.  */
3715
3716 typedef struct dw_attr_struct *dw_attr_ref;
3717 typedef struct dw_line_info_struct *dw_line_info_ref;
3718 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3719 typedef struct pubname_struct *pubname_ref;
3720 typedef struct dw_ranges_struct *dw_ranges_ref;
3721 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3722
3723 /* Each entry in the line_info_table maintains the file and
3724    line number associated with the label generated for that
3725    entry.  The label gives the PC value associated with
3726    the line number entry.  */
3727
3728 typedef struct dw_line_info_struct GTY(())
3729 {
3730   unsigned long dw_file_num;
3731   unsigned long dw_line_num;
3732 }
3733 dw_line_info_entry;
3734
3735 /* Line information for functions in separate sections; each one gets its
3736    own sequence.  */
3737 typedef struct dw_separate_line_info_struct GTY(())
3738 {
3739   unsigned long dw_file_num;
3740   unsigned long dw_line_num;
3741   unsigned long function;
3742 }
3743 dw_separate_line_info_entry;
3744
3745 /* Each DIE attribute has a field specifying the attribute kind,
3746    a link to the next attribute in the chain, and an attribute value.
3747    Attributes are typically linked below the DIE they modify.  */
3748
3749 typedef struct dw_attr_struct GTY(())
3750 {
3751   enum dwarf_attribute dw_attr;
3752   dw_val_node dw_attr_val;
3753 }
3754 dw_attr_node;
3755
3756 DEF_VEC_O(dw_attr_node);
3757 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3758
3759 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3760    The children of each node form a circular list linked by
3761    die_sib.  die_child points to the node *before* the "first" child node.  */
3762
3763 typedef struct die_struct GTY(())
3764 {
3765   enum dwarf_tag die_tag;
3766   char *die_symbol;
3767   VEC(dw_attr_node,gc) * die_attr;
3768   dw_die_ref die_parent;
3769   dw_die_ref die_child;
3770   dw_die_ref die_sib;
3771   dw_die_ref die_definition; /* ref from a specification to its definition */
3772   dw_offset die_offset;
3773   unsigned long die_abbrev;
3774   int die_mark;
3775   /* Die is used and must not be pruned as unused.  */
3776   int die_perennial_p;
3777   unsigned int decl_id;
3778 }
3779 die_node;
3780
3781 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3782 #define FOR_EACH_CHILD(die, c, expr) do {       \
3783   c = die->die_child;                           \
3784   if (c) do {                                   \
3785     c = c->die_sib;                             \
3786     expr;                                       \
3787   } while (c != die->die_child);                \
3788 } while (0)
3789
3790 /* The pubname structure */
3791
3792 typedef struct pubname_struct GTY(())
3793 {
3794   dw_die_ref die;
3795   const char *name;
3796 }
3797 pubname_entry;
3798
3799 DEF_VEC_O(pubname_entry);
3800 DEF_VEC_ALLOC_O(pubname_entry, gc);
3801
3802 struct dw_ranges_struct GTY(())
3803 {
3804   /* If this is positive, it's a block number, otherwise it's a
3805      bitwise-negated index into dw_ranges_by_label.  */
3806   int num;
3807 };
3808
3809 struct dw_ranges_by_label_struct GTY(())
3810 {
3811   const char *begin;
3812   const char *end;
3813 };
3814
3815 /* The limbo die list structure.  */
3816 typedef struct limbo_die_struct GTY(())
3817 {
3818   dw_die_ref die;
3819   tree created_for;
3820   struct limbo_die_struct *next;
3821 }
3822 limbo_die_node;
3823
3824 /* How to start an assembler comment.  */
3825 #ifndef ASM_COMMENT_START
3826 #define ASM_COMMENT_START ";#"
3827 #endif
3828
3829 /* Define a macro which returns nonzero for a TYPE_DECL which was
3830    implicitly generated for a tagged type.
3831
3832    Note that unlike the gcc front end (which generates a NULL named
3833    TYPE_DECL node for each complete tagged type, each array type, and
3834    each function type node created) the g++ front end generates a
3835    _named_ TYPE_DECL node for each tagged type node created.
3836    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3837    generate a DW_TAG_typedef DIE for them.  */
3838
3839 #define TYPE_DECL_IS_STUB(decl)                         \
3840   (DECL_NAME (decl) == NULL_TREE                        \
3841    || (DECL_ARTIFICIAL (decl)                           \
3842        && is_tagged_type (TREE_TYPE (decl))             \
3843        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3844            /* This is necessary for stub decls that     \
3845               appear in nested inline functions.  */    \
3846            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3847                && (decl_ultimate_origin (decl)          \
3848                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3849
3850 /* Information concerning the compilation unit's programming
3851    language, and compiler version.  */
3852
3853 /* Fixed size portion of the DWARF compilation unit header.  */
3854 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3855   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3856
3857 /* Fixed size portion of public names info.  */
3858 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3859
3860 /* Fixed size portion of the address range info.  */
3861 #define DWARF_ARANGES_HEADER_SIZE                                       \
3862   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3863                 DWARF2_ADDR_SIZE * 2)                                   \
3864    - DWARF_INITIAL_LENGTH_SIZE)
3865
3866 /* Size of padding portion in the address range info.  It must be
3867    aligned to twice the pointer size.  */
3868 #define DWARF_ARANGES_PAD_SIZE \
3869   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3870                 DWARF2_ADDR_SIZE * 2)                              \
3871    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3872
3873 /* Use assembler line directives if available.  */
3874 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3875 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3876 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3877 #else
3878 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3879 #endif
3880 #endif
3881
3882 /* Minimum line offset in a special line info. opcode.
3883    This value was chosen to give a reasonable range of values.  */
3884 #define DWARF_LINE_BASE  -10
3885
3886 /* First special line opcode - leave room for the standard opcodes.  */
3887 #define DWARF_LINE_OPCODE_BASE  10
3888
3889 /* Range of line offsets in a special line info. opcode.  */
3890 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3891
3892 /* Flag that indicates the initial value of the is_stmt_start flag.
3893    In the present implementation, we do not mark any lines as
3894    the beginning of a source statement, because that information
3895    is not made available by the GCC front-end.  */
3896 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3897
3898 #ifdef DWARF2_DEBUGGING_INFO
3899 /* This location is used by calc_die_sizes() to keep track
3900    the offset of each DIE within the .debug_info section.  */
3901 static unsigned long next_die_offset;
3902 #endif
3903
3904 /* Record the root of the DIE's built for the current compilation unit.  */
3905 static GTY(()) dw_die_ref comp_unit_die;
3906
3907 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3908 static GTY(()) limbo_die_node *limbo_die_list;
3909
3910 /* Filenames referenced by this compilation unit.  */
3911 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3912
3913 /* A hash table of references to DIE's that describe declarations.
3914    The key is a DECL_UID() which is a unique number identifying each decl.  */
3915 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3916
3917 /* Node of the variable location list.  */
3918 struct var_loc_node GTY ((chain_next ("%h.next")))
3919 {
3920   rtx GTY (()) var_loc_note;
3921   const char * GTY (()) label;
3922   const char * GTY (()) section_label;
3923   struct var_loc_node * GTY (()) next;
3924 };
3925
3926 /* Variable location list.  */
3927 struct var_loc_list_def GTY (())
3928 {
3929   struct var_loc_node * GTY (()) first;
3930
3931   /* Do not mark the last element of the chained list because
3932      it is marked through the chain.  */
3933   struct var_loc_node * GTY ((skip ("%h"))) last;
3934
3935   /* DECL_UID of the variable decl.  */
3936   unsigned int decl_id;
3937 };
3938 typedef struct var_loc_list_def var_loc_list;
3939
3940
3941 /* Table of decl location linked lists.  */
3942 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3943
3944 /* A pointer to the base of a list of references to DIE's that
3945    are uniquely identified by their tag, presence/absence of
3946    children DIE's, and list of attribute/value pairs.  */
3947 static GTY((length ("abbrev_die_table_allocated")))
3948   dw_die_ref *abbrev_die_table;
3949
3950 /* Number of elements currently allocated for abbrev_die_table.  */
3951 static GTY(()) unsigned abbrev_die_table_allocated;
3952
3953 /* Number of elements in type_die_table currently in use.  */
3954 static GTY(()) unsigned abbrev_die_table_in_use;
3955
3956 /* Size (in elements) of increments by which we may expand the
3957    abbrev_die_table.  */
3958 #define ABBREV_DIE_TABLE_INCREMENT 256
3959
3960 /* A pointer to the base of a table that contains line information
3961    for each source code line in .text in the compilation unit.  */
3962 static GTY((length ("line_info_table_allocated")))
3963      dw_line_info_ref line_info_table;
3964
3965 /* Number of elements currently allocated for line_info_table.  */
3966 static GTY(()) unsigned line_info_table_allocated;
3967
3968 /* Number of elements in line_info_table currently in use.  */
3969 static GTY(()) unsigned line_info_table_in_use;
3970
3971 /* True if the compilation unit places functions in more than one section.  */
3972 static GTY(()) bool have_multiple_function_sections = false;
3973
3974 /* A pointer to the base of a table that contains line information
3975    for each source code line outside of .text in the compilation unit.  */
3976 static GTY ((length ("separate_line_info_table_allocated")))
3977      dw_separate_line_info_ref separate_line_info_table;
3978
3979 /* Number of elements currently allocated for separate_line_info_table.  */
3980 static GTY(()) unsigned separate_line_info_table_allocated;
3981
3982 /* Number of elements in separate_line_info_table currently in use.  */
3983 static GTY(()) unsigned separate_line_info_table_in_use;
3984
3985 /* Size (in elements) of increments by which we may expand the
3986    line_info_table.  */
3987 #define LINE_INFO_TABLE_INCREMENT 1024
3988
3989 /* A pointer to the base of a table that contains a list of publicly
3990    accessible names.  */
3991 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3992
3993 /* A pointer to the base of a table that contains a list of publicly
3994    accessible types.  */
3995 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3996
3997 /* Array of dies for which we should generate .debug_arange info.  */
3998 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
3999
4000 /* Number of elements currently allocated for arange_table.  */
4001 static GTY(()) unsigned arange_table_allocated;
4002
4003 /* Number of elements in arange_table currently in use.  */
4004 static GTY(()) unsigned arange_table_in_use;
4005
4006 /* Size (in elements) of increments by which we may expand the
4007    arange_table.  */
4008 #define ARANGE_TABLE_INCREMENT 64
4009
4010 /* Array of dies for which we should generate .debug_ranges info.  */
4011 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4012
4013 /* Number of elements currently allocated for ranges_table.  */
4014 static GTY(()) unsigned ranges_table_allocated;
4015
4016 /* Number of elements in ranges_table currently in use.  */
4017 static GTY(()) unsigned ranges_table_in_use;
4018
4019 /* Array of pairs of labels referenced in ranges_table.  */
4020 static GTY ((length ("ranges_by_label_allocated")))
4021      dw_ranges_by_label_ref ranges_by_label;
4022
4023 /* Number of elements currently allocated for ranges_by_label.  */
4024 static GTY(()) unsigned ranges_by_label_allocated;
4025
4026 /* Number of elements in ranges_by_label currently in use.  */
4027 static GTY(()) unsigned ranges_by_label_in_use;
4028
4029 /* Size (in elements) of increments by which we may expand the
4030    ranges_table.  */
4031 #define RANGES_TABLE_INCREMENT 64
4032
4033 /* Whether we have location lists that need outputting */
4034 static GTY(()) bool have_location_lists;
4035
4036 /* Unique label counter.  */
4037 static GTY(()) unsigned int loclabel_num;
4038
4039 #ifdef DWARF2_DEBUGGING_INFO
4040 /* Record whether the function being analyzed contains inlined functions.  */
4041 static int current_function_has_inlines;
4042 #endif
4043 #if 0 && defined (MIPS_DEBUGGING_INFO)
4044 static int comp_unit_has_inlines;
4045 #endif
4046
4047 /* The last file entry emitted by maybe_emit_file().  */
4048 static GTY(()) struct dwarf_file_data * last_emitted_file;
4049
4050 /* Number of internal labels generated by gen_internal_sym().  */
4051 static GTY(()) int label_num;
4052
4053 /* Cached result of previous call to lookup_filename.  */
4054 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4055
4056 /* Whether the default text and cold text sections have been used at
4057    all.  */
4058
4059 static GTY(()) bool text_section_used = false;
4060 static GTY(()) bool cold_text_section_used = false;
4061
4062 /* The default cold text section.  */
4063 static GTY(()) section *cold_text_section;
4064
4065 #ifdef DWARF2_DEBUGGING_INFO
4066
4067 /* Offset from the "steady-state frame pointer" to the frame base,
4068    within the current function.  */
4069 static HOST_WIDE_INT frame_pointer_fb_offset;
4070
4071 /* Forward declarations for functions defined in this file.  */
4072
4073 static int is_pseudo_reg (const_rtx);
4074 static tree type_main_variant (tree);
4075 static int is_tagged_type (const_tree);
4076 static const char *dwarf_tag_name (unsigned);
4077 static const char *dwarf_attr_name (unsigned);
4078 static const char *dwarf_form_name (unsigned);
4079 static tree decl_ultimate_origin (const_tree);
4080 static tree block_ultimate_origin (const_tree);
4081 static tree decl_class_context (tree);
4082 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4083 static inline enum dw_val_class AT_class (dw_attr_ref);
4084 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4085 static inline unsigned AT_flag (dw_attr_ref);
4086 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4087 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4088 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4089 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4090 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4091                               unsigned long);
4092 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4093                                unsigned int, unsigned char *);
4094 static hashval_t debug_str_do_hash (const void *);
4095 static int debug_str_eq (const void *, const void *);
4096 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4097 static inline const char *AT_string (dw_attr_ref);
4098 static int AT_string_form (dw_attr_ref);
4099 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4100 static void add_AT_specification (dw_die_ref, dw_die_ref);
4101 static inline dw_die_ref AT_ref (dw_attr_ref);
4102 static inline int AT_ref_external (dw_attr_ref);
4103 static inline void set_AT_ref_external (dw_attr_ref, int);
4104 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4105 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4106 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4107 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4108                              dw_loc_list_ref);
4109 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4110 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4111 static inline rtx AT_addr (dw_attr_ref);
4112 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4113 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4114 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4115 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4116                            unsigned HOST_WIDE_INT);
4117 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4118                                unsigned long);
4119 static inline const char *AT_lbl (dw_attr_ref);
4120 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4121 static const char *get_AT_low_pc (dw_die_ref);
4122 static const char *get_AT_hi_pc (dw_die_ref);
4123 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4124 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4125 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4126 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4127 static bool is_c_family (void);
4128 static bool is_cxx (void);
4129 static bool is_java (void);
4130 static bool is_fortran (void);
4131 static bool is_ada (void);
4132 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4133 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4134 static void add_child_die (dw_die_ref, dw_die_ref);
4135 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4136 static dw_die_ref lookup_type_die (tree);
4137 static void equate_type_number_to_die (tree, dw_die_ref);
4138 static hashval_t decl_die_table_hash (const void *);
4139 static int decl_die_table_eq (const void *, const void *);
4140 static dw_die_ref lookup_decl_die (tree);
4141 static hashval_t decl_loc_table_hash (const void *);
4142 static int decl_loc_table_eq (const void *, const void *);
4143 static var_loc_list *lookup_decl_loc (const_tree);
4144 static void equate_decl_number_to_die (tree, dw_die_ref);
4145 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4146 static void print_spaces (FILE *);
4147 static void print_die (dw_die_ref, FILE *);
4148 static void print_dwarf_line_table (FILE *);
4149 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4150 static dw_die_ref pop_compile_unit (dw_die_ref);
4151 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4152 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4153 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4154 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4155 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4156 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4157 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4158 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4159 static void compute_section_prefix (dw_die_ref);
4160 static int is_type_die (dw_die_ref);
4161 static int is_comdat_die (dw_die_ref);
4162 static int is_symbol_die (dw_die_ref);
4163 static void assign_symbol_names (dw_die_ref);
4164 static void break_out_includes (dw_die_ref);
4165 static hashval_t htab_cu_hash (const void *);
4166 static int htab_cu_eq (const void *, const void *);
4167 static void htab_cu_del (void *);
4168 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4169 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4170 static void add_sibling_attributes (dw_die_ref);
4171 static void build_abbrev_table (dw_die_ref);
4172 static void output_location_lists (dw_die_ref);
4173 static int constant_size (long unsigned);
4174 static unsigned long size_of_die (dw_die_ref);
4175 static void calc_die_sizes (dw_die_ref);
4176 static void mark_dies (dw_die_ref);
4177 static void unmark_dies (dw_die_ref);
4178 static void unmark_all_dies (dw_die_ref);
4179 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4180 static unsigned long size_of_aranges (void);
4181 static enum dwarf_form value_format (dw_attr_ref);
4182 static void output_value_format (dw_attr_ref);
4183 static void output_abbrev_section (void);
4184 static void output_die_symbol (dw_die_ref);
4185 static void output_die (dw_die_ref);
4186 static void output_compilation_unit_header (void);
4187 static void output_comp_unit (dw_die_ref, int);
4188 static const char *dwarf2_name (tree, int);
4189 static void add_pubname (tree, dw_die_ref);
4190 static void add_pubtype (tree, dw_die_ref);
4191 static void output_pubnames (VEC (pubname_entry,gc) *);
4192 static void add_arange (tree, dw_die_ref);
4193 static void output_aranges (void);
4194 static unsigned int add_ranges_num (int);
4195 static unsigned int add_ranges (const_tree);
4196 static unsigned int add_ranges_by_labels (const char *, const char *);
4197 static void output_ranges (void);
4198 static void output_line_info (void);
4199 static void output_file_names (void);
4200 static dw_die_ref base_type_die (tree);
4201 static int is_base_type (tree);
4202 static bool is_subrange_type (const_tree);
4203 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4204 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4205 static int type_is_enum (const_tree);
4206 static unsigned int dbx_reg_number (const_rtx);
4207 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4208 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4209 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int, 
4210                                                 enum var_init_status);
4211 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4212                                                      enum var_init_status);
4213 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4214 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4215                                          enum var_init_status);
4216 static int is_based_loc (const_rtx);
4217 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4218                                             enum var_init_status);
4219 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4220                                                enum var_init_status);
4221 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4222 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4223 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4224 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4225 static tree field_type (const_tree);
4226 static unsigned int simple_type_align_in_bits (const_tree);
4227 static unsigned int simple_decl_align_in_bits (const_tree);
4228 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4229 static HOST_WIDE_INT field_byte_offset (const_tree);
4230 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4231                                          dw_loc_descr_ref);
4232 static void add_data_member_location_attribute (dw_die_ref, tree);
4233 static void add_const_value_attribute (dw_die_ref, rtx);
4234 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4235 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4236 static void insert_float (const_rtx, unsigned char *);
4237 static rtx rtl_for_decl_location (tree);
4238 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4239                                                    enum dwarf_attribute);
4240 static void tree_add_const_value_attribute (dw_die_ref, tree);
4241 static void add_name_attribute (dw_die_ref, const char *);
4242 static void add_comp_dir_attribute (dw_die_ref);
4243 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4244 static void add_subscript_info (dw_die_ref, tree);
4245 static void add_byte_size_attribute (dw_die_ref, tree);
4246 static void add_bit_offset_attribute (dw_die_ref, tree);
4247 static void add_bit_size_attribute (dw_die_ref, tree);
4248 static void add_prototyped_attribute (dw_die_ref, tree);
4249 static void add_abstract_origin_attribute (dw_die_ref, tree);
4250 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4251 static void add_src_coords_attributes (dw_die_ref, tree);
4252 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4253 static void push_decl_scope (tree);
4254 static void pop_decl_scope (void);
4255 static dw_die_ref scope_die_for (tree, dw_die_ref);
4256 static inline int local_scope_p (dw_die_ref);
4257 static inline int class_or_namespace_scope_p (dw_die_ref);
4258 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4259 static void add_calling_convention_attribute (dw_die_ref, tree);
4260 static const char *type_tag (const_tree);
4261 static tree member_declared_type (const_tree);
4262 #if 0
4263 static const char *decl_start_label (tree);
4264 #endif
4265 static void gen_array_type_die (tree, dw_die_ref);
4266 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4267 #if 0
4268 static void gen_entry_point_die (tree, dw_die_ref);
4269 #endif
4270 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4271 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4272 static void gen_inlined_union_type_die (tree, dw_die_ref);
4273 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4274 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4275 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4276 static void gen_formal_types_die (tree, dw_die_ref);
4277 static void gen_subprogram_die (tree, dw_die_ref);
4278 static void gen_variable_die (tree, dw_die_ref);
4279 static void gen_label_die (tree, dw_die_ref);
4280 static void gen_lexical_block_die (tree, dw_die_ref, int);
4281 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4282 static void gen_field_die (tree, dw_die_ref);
4283 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4284 static dw_die_ref gen_compile_unit_die (const char *);
4285 static void gen_inheritance_die (tree, tree, dw_die_ref);
4286 static void gen_member_die (tree, dw_die_ref);
4287 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4288                                                 enum debug_info_usage);
4289 static void gen_subroutine_type_die (tree, dw_die_ref);
4290 static void gen_typedef_die (tree, dw_die_ref);
4291 static void gen_type_die (tree, dw_die_ref);
4292 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4293 static void gen_block_die (tree, dw_die_ref, int);
4294 static void decls_for_scope (tree, dw_die_ref, int);
4295 static int is_redundant_typedef (const_tree);
4296 static void gen_namespace_die (tree);
4297 static void gen_decl_die (tree, dw_die_ref);
4298 static dw_die_ref force_decl_die (tree);
4299 static dw_die_ref force_type_die (tree);
4300 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4301 static void declare_in_namespace (tree, dw_die_ref);
4302 static struct dwarf_file_data * lookup_filename (const char *);
4303 static void retry_incomplete_types (void);
4304 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4305 static void splice_child_die (dw_die_ref, dw_die_ref);
4306 static int file_info_cmp (const void *, const void *);
4307 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4308                                      const char *, const char *, unsigned);
4309 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4310                                        const char *, const char *,
4311                                        const char *);
4312 static void output_loc_list (dw_loc_list_ref);
4313 static char *gen_internal_sym (const char *);
4314
4315 static void prune_unmark_dies (dw_die_ref);
4316 static void prune_unused_types_mark (dw_die_ref, int);
4317 static void prune_unused_types_walk (dw_die_ref);
4318 static void prune_unused_types_walk_attribs (dw_die_ref);
4319 static void prune_unused_types_prune (dw_die_ref);
4320 static void prune_unused_types (void);
4321 static int maybe_emit_file (struct dwarf_file_data *fd);
4322
4323 /* Section names used to hold DWARF debugging information.  */
4324 #ifndef DEBUG_INFO_SECTION
4325 #define DEBUG_INFO_SECTION      ".debug_info"
4326 #endif
4327 #ifndef DEBUG_ABBREV_SECTION
4328 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4329 #endif
4330 #ifndef DEBUG_ARANGES_SECTION
4331 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4332 #endif
4333 #ifndef DEBUG_MACINFO_SECTION
4334 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4335 #endif
4336 #ifndef DEBUG_LINE_SECTION
4337 #define DEBUG_LINE_SECTION      ".debug_line"
4338 #endif
4339 #ifndef DEBUG_LOC_SECTION
4340 #define DEBUG_LOC_SECTION       ".debug_loc"
4341 #endif
4342 #ifndef DEBUG_PUBNAMES_SECTION
4343 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4344 #endif
4345 #ifndef DEBUG_STR_SECTION
4346 #define DEBUG_STR_SECTION       ".debug_str"
4347 #endif
4348 #ifndef DEBUG_RANGES_SECTION
4349 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4350 #endif
4351
4352 /* Standard ELF section names for compiled code and data.  */
4353 #ifndef TEXT_SECTION_NAME
4354 #define TEXT_SECTION_NAME       ".text"
4355 #endif
4356
4357 /* Section flags for .debug_str section.  */
4358 #define DEBUG_STR_SECTION_FLAGS \
4359   (HAVE_GAS_SHF_MERGE && flag_merge_constants                   \
4360    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4361    : SECTION_DEBUG)
4362
4363 /* Labels we insert at beginning sections we can reference instead of
4364    the section names themselves.  */
4365
4366 #ifndef TEXT_SECTION_LABEL
4367 #define TEXT_SECTION_LABEL              "Ltext"
4368 #endif
4369 #ifndef COLD_TEXT_SECTION_LABEL
4370 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4371 #endif
4372 #ifndef DEBUG_LINE_SECTION_LABEL
4373 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4374 #endif
4375 #ifndef DEBUG_INFO_SECTION_LABEL
4376 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4377 #endif
4378 #ifndef DEBUG_ABBREV_SECTION_LABEL
4379 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4380 #endif
4381 #ifndef DEBUG_LOC_SECTION_LABEL
4382 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4383 #endif
4384 #ifndef DEBUG_RANGES_SECTION_LABEL
4385 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4386 #endif
4387 #ifndef DEBUG_MACINFO_SECTION_LABEL
4388 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4389 #endif
4390
4391 /* Definitions of defaults for formats and names of various special
4392    (artificial) labels which may be generated within this file (when the -g
4393    options is used and DWARF2_DEBUGGING_INFO is in effect.
4394    If necessary, these may be overridden from within the tm.h file, but
4395    typically, overriding these defaults is unnecessary.  */
4396
4397 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4398 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4399 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4400 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4401 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4402 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4403 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4404 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4405 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4406 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4407
4408 #ifndef TEXT_END_LABEL
4409 #define TEXT_END_LABEL          "Letext"
4410 #endif
4411 #ifndef COLD_END_LABEL
4412 #define COLD_END_LABEL          "Letext_cold"
4413 #endif
4414 #ifndef BLOCK_BEGIN_LABEL
4415 #define BLOCK_BEGIN_LABEL       "LBB"
4416 #endif
4417 #ifndef BLOCK_END_LABEL
4418 #define BLOCK_END_LABEL         "LBE"
4419 #endif
4420 #ifndef LINE_CODE_LABEL
4421 #define LINE_CODE_LABEL         "LM"
4422 #endif
4423 #ifndef SEPARATE_LINE_CODE_LABEL
4424 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4425 #endif
4426
4427 \f
4428 /* We allow a language front-end to designate a function that is to be
4429    called to "demangle" any name before it is put into a DIE.  */
4430
4431 static const char *(*demangle_name_func) (const char *);
4432
4433 void
4434 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4435 {
4436   demangle_name_func = func;
4437 }
4438
4439 /* Test if rtl node points to a pseudo register.  */
4440
4441 static inline int
4442 is_pseudo_reg (const_rtx rtl)
4443 {
4444   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4445           || (GET_CODE (rtl) == SUBREG
4446               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4447 }
4448
4449 /* Return a reference to a type, with its const and volatile qualifiers
4450    removed.  */
4451
4452 static inline tree
4453 type_main_variant (tree type)
4454 {
4455   type = TYPE_MAIN_VARIANT (type);
4456
4457   /* ??? There really should be only one main variant among any group of
4458      variants of a given type (and all of the MAIN_VARIANT values for all
4459      members of the group should point to that one type) but sometimes the C
4460      front-end messes this up for array types, so we work around that bug
4461      here.  */
4462   if (TREE_CODE (type) == ARRAY_TYPE)
4463     while (type != TYPE_MAIN_VARIANT (type))
4464       type = TYPE_MAIN_VARIANT (type);
4465
4466   return type;
4467 }
4468
4469 /* Return nonzero if the given type node represents a tagged type.  */
4470
4471 static inline int
4472 is_tagged_type (const_tree type)
4473 {
4474   enum tree_code code = TREE_CODE (type);
4475
4476   return (code == RECORD_TYPE || code == UNION_TYPE
4477           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4478 }
4479
4480 /* Convert a DIE tag into its string name.  */
4481
4482 static const char *
4483 dwarf_tag_name (unsigned int tag)
4484 {
4485   switch (tag)
4486     {
4487     case DW_TAG_padding:
4488       return "DW_TAG_padding";
4489     case DW_TAG_array_type:
4490       return "DW_TAG_array_type";
4491     case DW_TAG_class_type:
4492       return "DW_TAG_class_type";
4493     case DW_TAG_entry_point:
4494       return "DW_TAG_entry_point";
4495     case DW_TAG_enumeration_type:
4496       return "DW_TAG_enumeration_type";
4497     case DW_TAG_formal_parameter:
4498       return "DW_TAG_formal_parameter";
4499     case DW_TAG_imported_declaration:
4500       return "DW_TAG_imported_declaration";
4501     case DW_TAG_label:
4502       return "DW_TAG_label";
4503     case DW_TAG_lexical_block:
4504       return "DW_TAG_lexical_block";
4505     case DW_TAG_member:
4506       return "DW_TAG_member";
4507     case DW_TAG_pointer_type:
4508       return "DW_TAG_pointer_type";
4509     case DW_TAG_reference_type:
4510       return "DW_TAG_reference_type";
4511     case DW_TAG_compile_unit:
4512       return "DW_TAG_compile_unit";
4513     case DW_TAG_string_type:
4514       return "DW_TAG_string_type";
4515     case DW_TAG_structure_type:
4516       return "DW_TAG_structure_type";
4517     case DW_TAG_subroutine_type:
4518       return "DW_TAG_subroutine_type";
4519     case DW_TAG_typedef:
4520       return "DW_TAG_typedef";
4521     case DW_TAG_union_type:
4522       return "DW_TAG_union_type";
4523     case DW_TAG_unspecified_parameters:
4524       return "DW_TAG_unspecified_parameters";
4525     case DW_TAG_variant:
4526       return "DW_TAG_variant";
4527     case DW_TAG_common_block:
4528       return "DW_TAG_common_block";
4529     case DW_TAG_common_inclusion:
4530       return "DW_TAG_common_inclusion";
4531     case DW_TAG_inheritance:
4532       return "DW_TAG_inheritance";
4533     case DW_TAG_inlined_subroutine:
4534       return "DW_TAG_inlined_subroutine";
4535     case DW_TAG_module:
4536       return "DW_TAG_module";
4537     case DW_TAG_ptr_to_member_type:
4538       return "DW_TAG_ptr_to_member_type";
4539     case DW_TAG_set_type:
4540       return "DW_TAG_set_type";
4541     case DW_TAG_subrange_type:
4542       return "DW_TAG_subrange_type";
4543     case DW_TAG_with_stmt:
4544       return "DW_TAG_with_stmt";
4545     case DW_TAG_access_declaration:
4546       return "DW_TAG_access_declaration";
4547     case DW_TAG_base_type:
4548       return "DW_TAG_base_type";
4549     case DW_TAG_catch_block:
4550       return "DW_TAG_catch_block";
4551     case DW_TAG_const_type:
4552       return "DW_TAG_const_type";
4553     case DW_TAG_constant:
4554       return "DW_TAG_constant";
4555     case DW_TAG_enumerator:
4556       return "DW_TAG_enumerator";
4557     case DW_TAG_file_type:
4558       return "DW_TAG_file_type";
4559     case DW_TAG_friend:
4560       return "DW_TAG_friend";
4561     case DW_TAG_namelist:
4562       return "DW_TAG_namelist";
4563     case DW_TAG_namelist_item:
4564       return "DW_TAG_namelist_item";
4565     case DW_TAG_packed_type:
4566       return "DW_TAG_packed_type";
4567     case DW_TAG_subprogram:
4568       return "DW_TAG_subprogram";
4569     case DW_TAG_template_type_param:
4570       return "DW_TAG_template_type_param";
4571     case DW_TAG_template_value_param:
4572       return "DW_TAG_template_value_param";
4573     case DW_TAG_thrown_type:
4574       return "DW_TAG_thrown_type";
4575     case DW_TAG_try_block:
4576       return "DW_TAG_try_block";
4577     case DW_TAG_variant_part:
4578       return "DW_TAG_variant_part";
4579     case DW_TAG_variable:
4580       return "DW_TAG_variable";
4581     case DW_TAG_volatile_type:
4582       return "DW_TAG_volatile_type";
4583     case DW_TAG_dwarf_procedure:
4584       return "DW_TAG_dwarf_procedure";
4585     case DW_TAG_restrict_type:
4586       return "DW_TAG_restrict_type";
4587     case DW_TAG_interface_type:
4588       return "DW_TAG_interface_type";
4589     case DW_TAG_namespace:
4590       return "DW_TAG_namespace";
4591     case DW_TAG_imported_module:
4592       return "DW_TAG_imported_module";
4593     case DW_TAG_unspecified_type:
4594       return "DW_TAG_unspecified_type";
4595     case DW_TAG_partial_unit:
4596       return "DW_TAG_partial_unit";
4597     case DW_TAG_imported_unit:
4598       return "DW_TAG_imported_unit";
4599     case DW_TAG_condition:
4600       return "DW_TAG_condition";
4601     case DW_TAG_shared_type:
4602       return "DW_TAG_shared_type";
4603     case DW_TAG_MIPS_loop:
4604       return "DW_TAG_MIPS_loop";
4605     case DW_TAG_format_label:
4606       return "DW_TAG_format_label";
4607     case DW_TAG_function_template:
4608       return "DW_TAG_function_template";
4609     case DW_TAG_class_template:
4610       return "DW_TAG_class_template";
4611     case DW_TAG_GNU_BINCL:
4612       return "DW_TAG_GNU_BINCL";
4613     case DW_TAG_GNU_EINCL:
4614       return "DW_TAG_GNU_EINCL";
4615     default:
4616       return "DW_TAG_<unknown>";
4617     }
4618 }
4619
4620 /* Convert a DWARF attribute code into its string name.  */
4621
4622 static const char *
4623 dwarf_attr_name (unsigned int attr)
4624 {
4625   switch (attr)
4626     {
4627     case DW_AT_sibling:
4628       return "DW_AT_sibling";
4629     case DW_AT_location:
4630       return "DW_AT_location";
4631     case DW_AT_name:
4632       return "DW_AT_name";
4633     case DW_AT_ordering:
4634       return "DW_AT_ordering";
4635     case DW_AT_subscr_data:
4636       return "DW_AT_subscr_data";
4637     case DW_AT_byte_size:
4638       return "DW_AT_byte_size";
4639     case DW_AT_bit_offset:
4640       return "DW_AT_bit_offset";
4641     case DW_AT_bit_size:
4642       return "DW_AT_bit_size";
4643     case DW_AT_element_list:
4644       return "DW_AT_element_list";
4645     case DW_AT_stmt_list:
4646       return "DW_AT_stmt_list";
4647     case DW_AT_low_pc:
4648       return "DW_AT_low_pc";
4649     case DW_AT_high_pc:
4650       return "DW_AT_high_pc";
4651     case DW_AT_language:
4652       return "DW_AT_language";
4653     case DW_AT_member:
4654       return "DW_AT_member";
4655     case DW_AT_discr:
4656       return "DW_AT_discr";
4657     case DW_AT_discr_value:
4658       return "DW_AT_discr_value";
4659     case DW_AT_visibility:
4660       return "DW_AT_visibility";
4661     case DW_AT_import:
4662       return "DW_AT_import";
4663     case DW_AT_string_length:
4664       return "DW_AT_string_length";
4665     case DW_AT_common_reference:
4666       return "DW_AT_common_reference";
4667     case DW_AT_comp_dir:
4668       return "DW_AT_comp_dir";
4669     case DW_AT_const_value:
4670       return "DW_AT_const_value";
4671     case DW_AT_containing_type:
4672       return "DW_AT_containing_type";
4673     case DW_AT_default_value:
4674       return "DW_AT_default_value";
4675     case DW_AT_inline:
4676       return "DW_AT_inline";
4677     case DW_AT_is_optional:
4678       return "DW_AT_is_optional";
4679     case DW_AT_lower_bound:
4680       return "DW_AT_lower_bound";
4681     case DW_AT_producer:
4682       return "DW_AT_producer";
4683     case DW_AT_prototyped:
4684       return "DW_AT_prototyped";
4685     case DW_AT_return_addr:
4686       return "DW_AT_return_addr";
4687     case DW_AT_start_scope:
4688       return "DW_AT_start_scope";
4689     case DW_AT_bit_stride:
4690       return "DW_AT_bit_stride";
4691     case DW_AT_upper_bound:
4692       return "DW_AT_upper_bound";
4693     case DW_AT_abstract_origin:
4694       return "DW_AT_abstract_origin";
4695     case DW_AT_accessibility:
4696       return "DW_AT_accessibility";
4697     case DW_AT_address_class:
4698       return "DW_AT_address_class";
4699     case DW_AT_artificial:
4700       return "DW_AT_artificial";
4701     case DW_AT_base_types:
4702       return "DW_AT_base_types";
4703     case DW_AT_calling_convention:
4704       return "DW_AT_calling_convention";
4705     case DW_AT_count:
4706       return "DW_AT_count";
4707     case DW_AT_data_member_location:
4708       return "DW_AT_data_member_location";
4709     case DW_AT_decl_column:
4710       return "DW_AT_decl_column";
4711     case DW_AT_decl_file:
4712       return "DW_AT_decl_file";
4713     case DW_AT_decl_line:
4714       return "DW_AT_decl_line";
4715     case DW_AT_declaration:
4716       return "DW_AT_declaration";
4717     case DW_AT_discr_list:
4718       return "DW_AT_discr_list";
4719     case DW_AT_encoding:
4720       return "DW_AT_encoding";
4721     case DW_AT_external:
4722       return "DW_AT_external";
4723     case DW_AT_frame_base:
4724       return "DW_AT_frame_base";
4725     case DW_AT_friend:
4726       return "DW_AT_friend";
4727     case DW_AT_identifier_case:
4728       return "DW_AT_identifier_case";
4729     case DW_AT_macro_info:
4730       return "DW_AT_macro_info";
4731     case DW_AT_namelist_items:
4732       return "DW_AT_namelist_items";
4733     case DW_AT_priority:
4734       return "DW_AT_priority";
4735     case DW_AT_segment:
4736       return "DW_AT_segment";
4737     case DW_AT_specification:
4738       return "DW_AT_specification";
4739     case DW_AT_static_link:
4740       return "DW_AT_static_link";
4741     case DW_AT_type:
4742       return "DW_AT_type";
4743     case DW_AT_use_location:
4744       return "DW_AT_use_location";
4745     case DW_AT_variable_parameter:
4746       return "DW_AT_variable_parameter";
4747     case DW_AT_virtuality:
4748       return "DW_AT_virtuality";
4749     case DW_AT_vtable_elem_location:
4750       return "DW_AT_vtable_elem_location";
4751
4752     case DW_AT_allocated:
4753       return "DW_AT_allocated";
4754     case DW_AT_associated:
4755       return "DW_AT_associated";
4756     case DW_AT_data_location:
4757       return "DW_AT_data_location";
4758     case DW_AT_byte_stride:
4759       return "DW_AT_byte_stride";
4760     case DW_AT_entry_pc:
4761       return "DW_AT_entry_pc";
4762     case DW_AT_use_UTF8:
4763       return "DW_AT_use_UTF8";
4764     case DW_AT_extension:
4765       return "DW_AT_extension";
4766     case DW_AT_ranges:
4767       return "DW_AT_ranges";
4768     case DW_AT_trampoline:
4769       return "DW_AT_trampoline";
4770     case DW_AT_call_column:
4771       return "DW_AT_call_column";
4772     case DW_AT_call_file:
4773       return "DW_AT_call_file";
4774     case DW_AT_call_line:
4775       return "DW_AT_call_line";
4776
4777     case DW_AT_MIPS_fde:
4778       return "DW_AT_MIPS_fde";
4779     case DW_AT_MIPS_loop_begin:
4780       return "DW_AT_MIPS_loop_begin";
4781     case DW_AT_MIPS_tail_loop_begin:
4782       return "DW_AT_MIPS_tail_loop_begin";
4783     case DW_AT_MIPS_epilog_begin:
4784       return "DW_AT_MIPS_epilog_begin";
4785     case DW_AT_MIPS_loop_unroll_factor:
4786       return "DW_AT_MIPS_loop_unroll_factor";
4787     case DW_AT_MIPS_software_pipeline_depth:
4788       return "DW_AT_MIPS_software_pipeline_depth";
4789     case DW_AT_MIPS_linkage_name:
4790       return "DW_AT_MIPS_linkage_name";
4791     case DW_AT_MIPS_stride:
4792       return "DW_AT_MIPS_stride";
4793     case DW_AT_MIPS_abstract_name:
4794       return "DW_AT_MIPS_abstract_name";
4795     case DW_AT_MIPS_clone_origin:
4796       return "DW_AT_MIPS_clone_origin";
4797     case DW_AT_MIPS_has_inlines:
4798       return "DW_AT_MIPS_has_inlines";
4799
4800     case DW_AT_sf_names:
4801       return "DW_AT_sf_names";
4802     case DW_AT_src_info:
4803       return "DW_AT_src_info";
4804     case DW_AT_mac_info:
4805       return "DW_AT_mac_info";
4806     case DW_AT_src_coords:
4807       return "DW_AT_src_coords";
4808     case DW_AT_body_begin:
4809       return "DW_AT_body_begin";
4810     case DW_AT_body_end:
4811       return "DW_AT_body_end";
4812     case DW_AT_GNU_vector:
4813       return "DW_AT_GNU_vector";
4814
4815     case DW_AT_VMS_rtnbeg_pd_address:
4816       return "DW_AT_VMS_rtnbeg_pd_address";
4817
4818     default:
4819       return "DW_AT_<unknown>";
4820     }
4821 }
4822
4823 /* Convert a DWARF value form code into its string name.  */
4824
4825 static const char *
4826 dwarf_form_name (unsigned int form)
4827 {
4828   switch (form)
4829     {
4830     case DW_FORM_addr:
4831       return "DW_FORM_addr";
4832     case DW_FORM_block2:
4833       return "DW_FORM_block2";
4834     case DW_FORM_block4:
4835       return "DW_FORM_block4";
4836     case DW_FORM_data2:
4837       return "DW_FORM_data2";
4838     case DW_FORM_data4:
4839       return "DW_FORM_data4";
4840     case DW_FORM_data8:
4841       return "DW_FORM_data8";
4842     case DW_FORM_string:
4843       return "DW_FORM_string";
4844     case DW_FORM_block:
4845       return "DW_FORM_block";
4846     case DW_FORM_block1:
4847       return "DW_FORM_block1";
4848     case DW_FORM_data1:
4849       return "DW_FORM_data1";
4850     case DW_FORM_flag:
4851       return "DW_FORM_flag";
4852     case DW_FORM_sdata:
4853       return "DW_FORM_sdata";
4854     case DW_FORM_strp:
4855       return "DW_FORM_strp";
4856     case DW_FORM_udata:
4857       return "DW_FORM_udata";
4858     case DW_FORM_ref_addr:
4859       return "DW_FORM_ref_addr";
4860     case DW_FORM_ref1:
4861       return "DW_FORM_ref1";
4862     case DW_FORM_ref2:
4863       return "DW_FORM_ref2";
4864     case DW_FORM_ref4:
4865       return "DW_FORM_ref4";
4866     case DW_FORM_ref8:
4867       return "DW_FORM_ref8";
4868     case DW_FORM_ref_udata:
4869       return "DW_FORM_ref_udata";
4870     case DW_FORM_indirect:
4871       return "DW_FORM_indirect";
4872     default:
4873       return "DW_FORM_<unknown>";
4874     }
4875 }
4876 \f
4877 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4878    instance of an inlined instance of a decl which is local to an inline
4879    function, so we have to trace all of the way back through the origin chain
4880    to find out what sort of node actually served as the original seed for the
4881    given block.  */
4882
4883 static tree
4884 decl_ultimate_origin (const_tree decl)
4885 {
4886   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4887     return NULL_TREE;
4888
4889   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4890      nodes in the function to point to themselves; ignore that if
4891      we're trying to output the abstract instance of this function.  */
4892   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4893     return NULL_TREE;
4894
4895   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4896      most distant ancestor, this should never happen.  */
4897   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4898
4899   return DECL_ABSTRACT_ORIGIN (decl);
4900 }
4901
4902 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4903    instance of an inlined instance of a block which is local to an inline
4904    function, so we have to trace all of the way back through the origin chain
4905    to find out what sort of node actually served as the original seed for the
4906    given block.  */
4907
4908 static tree
4909 block_ultimate_origin (const_tree block)
4910 {
4911   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4912
4913   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4914      nodes in the function to point to themselves; ignore that if
4915      we're trying to output the abstract instance of this function.  */
4916   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4917     return NULL_TREE;
4918
4919   if (immediate_origin == NULL_TREE)
4920     return NULL_TREE;
4921   else
4922     {
4923       tree ret_val;
4924       tree lookahead = immediate_origin;
4925
4926       do
4927         {
4928           ret_val = lookahead;
4929           lookahead = (TREE_CODE (ret_val) == BLOCK
4930                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4931         }
4932       while (lookahead != NULL && lookahead != ret_val);
4933
4934       /* The block's abstract origin chain may not be the *ultimate* origin of
4935          the block. It could lead to a DECL that has an abstract origin set.
4936          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
4937          will give us if it has one).  Note that DECL's abstract origins are
4938          supposed to be the most distant ancestor (or so decl_ultimate_origin
4939          claims), so we don't need to loop following the DECL origins.  */
4940       if (DECL_P (ret_val))
4941         return DECL_ORIGIN (ret_val);
4942
4943       return ret_val;
4944     }
4945 }
4946
4947 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4948    of a virtual function may refer to a base class, so we check the 'this'
4949    parameter.  */
4950
4951 static tree
4952 decl_class_context (tree decl)
4953 {
4954   tree context = NULL_TREE;
4955
4956   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4957     context = DECL_CONTEXT (decl);
4958   else
4959     context = TYPE_MAIN_VARIANT
4960       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4961
4962   if (context && !TYPE_P (context))
4963     context = NULL_TREE;
4964
4965   return context;
4966 }
4967 \f
4968 /* Add an attribute/value pair to a DIE.  */
4969
4970 static inline void
4971 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4972 {
4973   /* Maybe this should be an assert?  */
4974   if (die == NULL)
4975     return;
4976
4977   if (die->die_attr == NULL)
4978     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4979   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4980 }
4981
4982 static inline enum dw_val_class
4983 AT_class (dw_attr_ref a)
4984 {
4985   return a->dw_attr_val.val_class;
4986 }
4987
4988 /* Add a flag value attribute to a DIE.  */
4989
4990 static inline void
4991 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4992 {
4993   dw_attr_node attr;
4994
4995   attr.dw_attr = attr_kind;
4996   attr.dw_attr_val.val_class = dw_val_class_flag;
4997   attr.dw_attr_val.v.val_flag = flag;
4998   add_dwarf_attr (die, &attr);
4999 }
5000
5001 static inline unsigned
5002 AT_flag (dw_attr_ref a)
5003 {
5004   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5005   return a->dw_attr_val.v.val_flag;
5006 }
5007
5008 /* Add a signed integer attribute value to a DIE.  */
5009
5010 static inline void
5011 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5012 {
5013   dw_attr_node attr;
5014
5015   attr.dw_attr = attr_kind;
5016   attr.dw_attr_val.val_class = dw_val_class_const;
5017   attr.dw_attr_val.v.val_int = int_val;
5018   add_dwarf_attr (die, &attr);
5019 }
5020
5021 static inline HOST_WIDE_INT
5022 AT_int (dw_attr_ref a)
5023 {
5024   gcc_assert (a && AT_class (a) == dw_val_class_const);
5025   return a->dw_attr_val.v.val_int;
5026 }
5027
5028 /* Add an unsigned integer attribute value to a DIE.  */
5029
5030 static inline void
5031 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5032                  unsigned HOST_WIDE_INT unsigned_val)
5033 {
5034   dw_attr_node attr;
5035
5036   attr.dw_attr = attr_kind;
5037   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5038   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5039   add_dwarf_attr (die, &attr);
5040 }
5041
5042 static inline unsigned HOST_WIDE_INT
5043 AT_unsigned (dw_attr_ref a)
5044 {
5045   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5046   return a->dw_attr_val.v.val_unsigned;
5047 }
5048
5049 /* Add an unsigned double integer attribute value to a DIE.  */
5050
5051 static inline void
5052 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5053                   long unsigned int val_hi, long unsigned int val_low)
5054 {
5055   dw_attr_node attr;
5056
5057   attr.dw_attr = attr_kind;
5058   attr.dw_attr_val.val_class = dw_val_class_long_long;
5059   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5060   attr.dw_attr_val.v.val_long_long.low = val_low;
5061   add_dwarf_attr (die, &attr);
5062 }
5063
5064 /* Add a floating point attribute value to a DIE and return it.  */
5065
5066 static inline void
5067 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5068             unsigned int length, unsigned int elt_size, unsigned char *array)
5069 {
5070   dw_attr_node attr;
5071
5072   attr.dw_attr = attr_kind;
5073   attr.dw_attr_val.val_class = dw_val_class_vec;
5074   attr.dw_attr_val.v.val_vec.length = length;
5075   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5076   attr.dw_attr_val.v.val_vec.array = array;
5077   add_dwarf_attr (die, &attr);
5078 }
5079
5080 /* Hash and equality functions for debug_str_hash.  */
5081
5082 static hashval_t
5083 debug_str_do_hash (const void *x)
5084 {
5085   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5086 }
5087
5088 static int
5089 debug_str_eq (const void *x1, const void *x2)
5090 {
5091   return strcmp ((((const struct indirect_string_node *)x1)->str),
5092                  (const char *)x2) == 0;
5093 }
5094
5095 /* Add a string attribute value to a DIE.  */
5096
5097 static inline void
5098 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5099 {
5100   dw_attr_node attr;
5101   struct indirect_string_node *node;
5102   void **slot;
5103
5104   if (! debug_str_hash)
5105     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5106                                       debug_str_eq, NULL);
5107
5108   slot = htab_find_slot_with_hash (debug_str_hash, str,
5109                                    htab_hash_string (str), INSERT);
5110   if (*slot == NULL)
5111     {
5112       node = (struct indirect_string_node *)
5113                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5114       node->str = ggc_strdup (str);
5115       *slot = node;
5116     }
5117   else
5118     node = (struct indirect_string_node *) *slot;
5119
5120   node->refcount++;
5121
5122   attr.dw_attr = attr_kind;
5123   attr.dw_attr_val.val_class = dw_val_class_str;
5124   attr.dw_attr_val.v.val_str = node;
5125   add_dwarf_attr (die, &attr);
5126 }
5127
5128 static inline const char *
5129 AT_string (dw_attr_ref a)
5130 {
5131   gcc_assert (a && AT_class (a) == dw_val_class_str);
5132   return a->dw_attr_val.v.val_str->str;
5133 }
5134
5135 /* Find out whether a string should be output inline in DIE
5136    or out-of-line in .debug_str section.  */
5137
5138 static int
5139 AT_string_form (dw_attr_ref a)
5140 {
5141   struct indirect_string_node *node;
5142   unsigned int len;
5143   char label[32];
5144
5145   gcc_assert (a && AT_class (a) == dw_val_class_str);
5146
5147   node = a->dw_attr_val.v.val_str;
5148   if (node->form)
5149     return node->form;
5150
5151   len = strlen (node->str) + 1;
5152
5153   /* If the string is shorter or equal to the size of the reference, it is
5154      always better to put it inline.  */
5155   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5156     return node->form = DW_FORM_string;
5157
5158   /* If we cannot expect the linker to merge strings in .debug_str
5159      section, only put it into .debug_str if it is worth even in this
5160      single module.  */
5161   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5162       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5163     return node->form = DW_FORM_string;
5164
5165   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5166   ++dw2_string_counter;
5167   node->label = xstrdup (label);
5168
5169   return node->form = DW_FORM_strp;
5170 }
5171
5172 /* Add a DIE reference attribute value to a DIE.  */
5173
5174 static inline void
5175 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5176 {
5177   dw_attr_node attr;
5178
5179   attr.dw_attr = attr_kind;
5180   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5181   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5182   attr.dw_attr_val.v.val_die_ref.external = 0;
5183   add_dwarf_attr (die, &attr);
5184 }
5185
5186 /* Add an AT_specification attribute to a DIE, and also make the back
5187    pointer from the specification to the definition.  */
5188
5189 static inline void
5190 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5191 {
5192   add_AT_die_ref (die, DW_AT_specification, targ_die);
5193   gcc_assert (!targ_die->die_definition);
5194   targ_die->die_definition = die;
5195 }
5196
5197 static inline dw_die_ref
5198 AT_ref (dw_attr_ref a)
5199 {
5200   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5201   return a->dw_attr_val.v.val_die_ref.die;
5202 }
5203
5204 static inline int
5205 AT_ref_external (dw_attr_ref a)
5206 {
5207   if (a && AT_class (a) == dw_val_class_die_ref)
5208     return a->dw_attr_val.v.val_die_ref.external;
5209
5210   return 0;
5211 }
5212
5213 static inline void
5214 set_AT_ref_external (dw_attr_ref a, int i)
5215 {
5216   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5217   a->dw_attr_val.v.val_die_ref.external = i;
5218 }
5219
5220 /* Add an FDE reference attribute value to a DIE.  */
5221
5222 static inline void
5223 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5224 {
5225   dw_attr_node attr;
5226
5227   attr.dw_attr = attr_kind;
5228   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5229   attr.dw_attr_val.v.val_fde_index = targ_fde;
5230   add_dwarf_attr (die, &attr);
5231 }
5232
5233 /* Add a location description attribute value to a DIE.  */
5234
5235 static inline void
5236 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5237 {
5238   dw_attr_node attr;
5239
5240   attr.dw_attr = attr_kind;
5241   attr.dw_attr_val.val_class = dw_val_class_loc;
5242   attr.dw_attr_val.v.val_loc = loc;
5243   add_dwarf_attr (die, &attr);
5244 }
5245
5246 static inline dw_loc_descr_ref
5247 AT_loc (dw_attr_ref a)
5248 {
5249   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5250   return a->dw_attr_val.v.val_loc;
5251 }
5252
5253 static inline void
5254 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5255 {
5256   dw_attr_node attr;
5257
5258   attr.dw_attr = attr_kind;
5259   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5260   attr.dw_attr_val.v.val_loc_list = loc_list;
5261   add_dwarf_attr (die, &attr);
5262   have_location_lists = true;
5263 }
5264
5265 static inline dw_loc_list_ref
5266 AT_loc_list (dw_attr_ref a)
5267 {
5268   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5269   return a->dw_attr_val.v.val_loc_list;
5270 }
5271
5272 /* Add an address constant attribute value to a DIE.  */
5273
5274 static inline void
5275 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5276 {
5277   dw_attr_node attr;
5278
5279   attr.dw_attr = attr_kind;
5280   attr.dw_attr_val.val_class = dw_val_class_addr;
5281   attr.dw_attr_val.v.val_addr = addr;
5282   add_dwarf_attr (die, &attr);
5283 }
5284
5285 /* Get the RTX from to an address DIE attribute.  */
5286
5287 static inline rtx
5288 AT_addr (dw_attr_ref a)
5289 {
5290   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5291   return a->dw_attr_val.v.val_addr;
5292 }
5293
5294 /* Add a file attribute value to a DIE.  */
5295
5296 static inline void
5297 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5298              struct dwarf_file_data *fd)
5299 {
5300   dw_attr_node attr;
5301
5302   attr.dw_attr = attr_kind;
5303   attr.dw_attr_val.val_class = dw_val_class_file;
5304   attr.dw_attr_val.v.val_file = fd;
5305   add_dwarf_attr (die, &attr);
5306 }
5307
5308 /* Get the dwarf_file_data from a file DIE attribute.  */
5309
5310 static inline struct dwarf_file_data *
5311 AT_file (dw_attr_ref a)
5312 {
5313   gcc_assert (a && AT_class (a) == dw_val_class_file);
5314   return a->dw_attr_val.v.val_file;
5315 }
5316
5317 /* Add a label identifier attribute value to a DIE.  */
5318
5319 static inline void
5320 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5321 {
5322   dw_attr_node attr;
5323
5324   attr.dw_attr = attr_kind;
5325   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5326   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5327   add_dwarf_attr (die, &attr);
5328 }
5329
5330 /* Add a section offset attribute value to a DIE, an offset into the
5331    debug_line section.  */
5332
5333 static inline void
5334 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5335                 const char *label)
5336 {
5337   dw_attr_node attr;
5338
5339   attr.dw_attr = attr_kind;
5340   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5341   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5342   add_dwarf_attr (die, &attr);
5343 }
5344
5345 /* Add a section offset attribute value to a DIE, an offset into the
5346    debug_macinfo section.  */
5347
5348 static inline void
5349 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5350                const char *label)
5351 {
5352   dw_attr_node attr;
5353
5354   attr.dw_attr = attr_kind;
5355   attr.dw_attr_val.val_class = dw_val_class_macptr;
5356   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5357   add_dwarf_attr (die, &attr);
5358 }
5359
5360 /* Add an offset attribute value to a DIE.  */
5361
5362 static inline void
5363 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5364                unsigned HOST_WIDE_INT offset)
5365 {
5366   dw_attr_node attr;
5367
5368   attr.dw_attr = attr_kind;
5369   attr.dw_attr_val.val_class = dw_val_class_offset;
5370   attr.dw_attr_val.v.val_offset = offset;
5371   add_dwarf_attr (die, &attr);
5372 }
5373
5374 /* Add an range_list attribute value to a DIE.  */
5375
5376 static void
5377 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5378                    long unsigned int offset)
5379 {
5380   dw_attr_node attr;
5381
5382   attr.dw_attr = attr_kind;
5383   attr.dw_attr_val.val_class = dw_val_class_range_list;
5384   attr.dw_attr_val.v.val_offset = offset;
5385   add_dwarf_attr (die, &attr);
5386 }
5387
5388 static inline const char *
5389 AT_lbl (dw_attr_ref a)
5390 {
5391   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5392                     || AT_class (a) == dw_val_class_lineptr
5393                     || AT_class (a) == dw_val_class_macptr));
5394   return a->dw_attr_val.v.val_lbl_id;
5395 }
5396
5397 /* Get the attribute of type attr_kind.  */
5398
5399 static dw_attr_ref
5400 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5401 {
5402   dw_attr_ref a;
5403   unsigned ix;
5404   dw_die_ref spec = NULL;
5405
5406   if (! die)
5407     return NULL;
5408
5409   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5410     if (a->dw_attr == attr_kind)
5411       return a;
5412     else if (a->dw_attr == DW_AT_specification
5413              || a->dw_attr == DW_AT_abstract_origin)
5414       spec = AT_ref (a);
5415
5416   if (spec)
5417     return get_AT (spec, attr_kind);
5418
5419   return NULL;
5420 }
5421
5422 /* Return the "low pc" attribute value, typically associated with a subprogram
5423    DIE.  Return null if the "low pc" attribute is either not present, or if it
5424    cannot be represented as an assembler label identifier.  */
5425
5426 static inline const char *
5427 get_AT_low_pc (dw_die_ref die)
5428 {
5429   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5430
5431   return a ? AT_lbl (a) : NULL;
5432 }
5433
5434 /* Return the "high pc" attribute value, typically associated with a subprogram
5435    DIE.  Return null if the "high pc" attribute is either not present, or if it
5436    cannot be represented as an assembler label identifier.  */
5437
5438 static inline const char *
5439 get_AT_hi_pc (dw_die_ref die)
5440 {
5441   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5442
5443   return a ? AT_lbl (a) : NULL;
5444 }
5445
5446 /* Return the value of the string attribute designated by ATTR_KIND, or
5447    NULL if it is not present.  */
5448
5449 static inline const char *
5450 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5451 {
5452   dw_attr_ref a = get_AT (die, attr_kind);
5453
5454   return a ? AT_string (a) : NULL;
5455 }
5456
5457 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5458    if it is not present.  */
5459
5460 static inline int
5461 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5462 {
5463   dw_attr_ref a = get_AT (die, attr_kind);
5464
5465   return a ? AT_flag (a) : 0;
5466 }
5467
5468 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5469    if it is not present.  */
5470
5471 static inline unsigned
5472 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5473 {
5474   dw_attr_ref a = get_AT (die, attr_kind);
5475
5476   return a ? AT_unsigned (a) : 0;
5477 }
5478
5479 static inline dw_die_ref
5480 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5481 {
5482   dw_attr_ref a = get_AT (die, attr_kind);
5483
5484   return a ? AT_ref (a) : NULL;
5485 }
5486
5487 static inline struct dwarf_file_data *
5488 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5489 {
5490   dw_attr_ref a = get_AT (die, attr_kind);
5491
5492   return a ? AT_file (a) : NULL;
5493 }
5494
5495 /* Return TRUE if the language is C or C++.  */
5496
5497 static inline bool
5498 is_c_family (void)
5499 {
5500   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5501
5502   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5503           || lang == DW_LANG_C99
5504           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5505 }
5506
5507 /* Return TRUE if the language is C++.  */
5508
5509 static inline bool
5510 is_cxx (void)
5511 {
5512   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5513
5514   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5515 }
5516
5517 /* Return TRUE if the language is Fortran.  */
5518
5519 static inline bool
5520 is_fortran (void)
5521 {
5522   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5523
5524   return (lang == DW_LANG_Fortran77
5525           || lang == DW_LANG_Fortran90
5526           || lang == DW_LANG_Fortran95);
5527 }
5528
5529 /* Return TRUE if the language is Java.  */
5530
5531 static inline bool
5532 is_java (void)
5533 {
5534   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5535
5536   return lang == DW_LANG_Java;
5537 }
5538
5539 /* Return TRUE if the language is Ada.  */
5540
5541 static inline bool
5542 is_ada (void)
5543 {
5544   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5545
5546   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5547 }
5548
5549 /* Remove the specified attribute if present.  */
5550
5551 static void
5552 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5553 {
5554   dw_attr_ref a;
5555   unsigned ix;
5556
5557   if (! die)
5558     return;
5559
5560   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5561     if (a->dw_attr == attr_kind)
5562       {
5563         if (AT_class (a) == dw_val_class_str)
5564           if (a->dw_attr_val.v.val_str->refcount)
5565             a->dw_attr_val.v.val_str->refcount--;
5566
5567         /* VEC_ordered_remove should help reduce the number of abbrevs
5568            that are needed.  */
5569         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5570         return;
5571       }
5572 }
5573
5574 /* Remove CHILD from its parent.  PREV must have the property that
5575    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5576
5577 static void
5578 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5579 {
5580   gcc_assert (child->die_parent == prev->die_parent);
5581   gcc_assert (prev->die_sib == child);
5582   if (prev == child)
5583     {
5584       gcc_assert (child->die_parent->die_child == child);
5585       prev = NULL;
5586     }
5587   else
5588     prev->die_sib = child->die_sib;
5589   if (child->die_parent->die_child == child)
5590     child->die_parent->die_child = prev;
5591 }
5592
5593 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5594    matches TAG.  */
5595
5596 static void
5597 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5598 {
5599   dw_die_ref c;
5600
5601   c = die->die_child;
5602   if (c) do {
5603     dw_die_ref prev = c;
5604     c = c->die_sib;
5605     while (c->die_tag == tag)
5606       {
5607         remove_child_with_prev (c, prev);
5608         /* Might have removed every child.  */
5609         if (c == c->die_sib)
5610           return;
5611         c = c->die_sib;
5612       }
5613   } while (c != die->die_child);
5614 }
5615
5616 /* Add a CHILD_DIE as the last child of DIE.  */
5617
5618 static void
5619 add_child_die (dw_die_ref die, dw_die_ref child_die)
5620 {
5621   /* FIXME this should probably be an assert.  */
5622   if (! die || ! child_die)
5623     return;
5624   gcc_assert (die != child_die);
5625
5626   child_die->die_parent = die;
5627   if (die->die_child)
5628     {
5629       child_die->die_sib = die->die_child->die_sib;
5630       die->die_child->die_sib = child_die;
5631     }
5632   else
5633     child_die->die_sib = child_die;
5634   die->die_child = child_die;
5635 }
5636
5637 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5638    is the specification, to the end of PARENT's list of children.
5639    This is done by removing and re-adding it.  */
5640
5641 static void
5642 splice_child_die (dw_die_ref parent, dw_die_ref child)
5643 {
5644   dw_die_ref p;
5645
5646   /* We want the declaration DIE from inside the class, not the
5647      specification DIE at toplevel.  */
5648   if (child->die_parent != parent)
5649     {
5650       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5651
5652       if (tmp)
5653         child = tmp;
5654     }
5655
5656   gcc_assert (child->die_parent == parent
5657               || (child->die_parent
5658                   == get_AT_ref (parent, DW_AT_specification)));
5659
5660   for (p = child->die_parent->die_child; ; p = p->die_sib)
5661     if (p->die_sib == child)
5662       {
5663         remove_child_with_prev (child, p);
5664         break;
5665       }
5666
5667   add_child_die (parent, child);
5668 }
5669
5670 /* Return a pointer to a newly created DIE node.  */
5671
5672 static inline dw_die_ref
5673 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5674 {
5675   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5676
5677   die->die_tag = tag_value;
5678
5679   if (parent_die != NULL)
5680     add_child_die (parent_die, die);
5681   else
5682     {
5683       limbo_die_node *limbo_node;
5684
5685       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5686       limbo_node->die = die;
5687       limbo_node->created_for = t;
5688       limbo_node->next = limbo_die_list;
5689       limbo_die_list = limbo_node;
5690     }
5691
5692   return die;
5693 }
5694
5695 /* Return the DIE associated with the given type specifier.  */
5696
5697 static inline dw_die_ref
5698 lookup_type_die (tree type)
5699 {
5700   return TYPE_SYMTAB_DIE (type);
5701 }
5702
5703 /* Equate a DIE to a given type specifier.  */
5704
5705 static inline void
5706 equate_type_number_to_die (tree type, dw_die_ref type_die)
5707 {
5708   TYPE_SYMTAB_DIE (type) = type_die;
5709 }
5710
5711 /* Returns a hash value for X (which really is a die_struct).  */
5712
5713 static hashval_t
5714 decl_die_table_hash (const void *x)
5715 {
5716   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5717 }
5718
5719 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5720
5721 static int
5722 decl_die_table_eq (const void *x, const void *y)
5723 {
5724   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5725 }
5726
5727 /* Return the DIE associated with a given declaration.  */
5728
5729 static inline dw_die_ref
5730 lookup_decl_die (tree decl)
5731 {
5732   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5733 }
5734
5735 /* Returns a hash value for X (which really is a var_loc_list).  */
5736
5737 static hashval_t
5738 decl_loc_table_hash (const void *x)
5739 {
5740   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5741 }
5742
5743 /* Return nonzero if decl_id of var_loc_list X is the same as
5744    UID of decl *Y.  */
5745
5746 static int
5747 decl_loc_table_eq (const void *x, const void *y)
5748 {
5749   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5750 }
5751
5752 /* Return the var_loc list associated with a given declaration.  */
5753
5754 static inline var_loc_list *
5755 lookup_decl_loc (const_tree decl)
5756 {
5757   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5758 }
5759
5760 /* Equate a DIE to a particular declaration.  */
5761
5762 static void
5763 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5764 {
5765   unsigned int decl_id = DECL_UID (decl);
5766   void **slot;
5767
5768   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5769   *slot = decl_die;
5770   decl_die->decl_id = decl_id;
5771 }
5772
5773 /* Add a variable location node to the linked list for DECL.  */
5774
5775 static void
5776 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5777 {
5778   unsigned int decl_id = DECL_UID (decl);
5779   var_loc_list *temp;
5780   void **slot;
5781
5782   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5783   if (*slot == NULL)
5784     {
5785       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5786       temp->decl_id = decl_id;
5787       *slot = temp;
5788     }
5789   else
5790     temp = *slot;
5791
5792   if (temp->last)
5793     {
5794       /* If the current location is the same as the end of the list,
5795          and either both or neither of the locations is uninitialized,
5796          we have nothing to do.  */
5797       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5798                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5799           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5800                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5801               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5802                    == VAR_INIT_STATUS_UNINITIALIZED)
5803                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5804                       == VAR_INIT_STATUS_UNINITIALIZED))))
5805         {
5806           /* Add LOC to the end of list and update LAST.  */
5807           temp->last->next = loc;
5808           temp->last = loc;
5809         }
5810     }
5811   /* Do not add empty location to the beginning of the list.  */
5812   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5813     {
5814       temp->first = loc;
5815       temp->last = loc;
5816     }
5817 }
5818 \f
5819 /* Keep track of the number of spaces used to indent the
5820    output of the debugging routines that print the structure of
5821    the DIE internal representation.  */
5822 static int print_indent;
5823
5824 /* Indent the line the number of spaces given by print_indent.  */
5825
5826 static inline void
5827 print_spaces (FILE *outfile)
5828 {
5829   fprintf (outfile, "%*s", print_indent, "");
5830 }
5831
5832 /* Print the information associated with a given DIE, and its children.
5833    This routine is a debugging aid only.  */
5834
5835 static void
5836 print_die (dw_die_ref die, FILE *outfile)
5837 {
5838   dw_attr_ref a;
5839   dw_die_ref c;
5840   unsigned ix;
5841
5842   print_spaces (outfile);
5843   fprintf (outfile, "DIE %4ld: %s\n",
5844            die->die_offset, dwarf_tag_name (die->die_tag));
5845   print_spaces (outfile);
5846   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5847   fprintf (outfile, " offset: %ld\n", die->die_offset);
5848
5849   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5850     {
5851       print_spaces (outfile);
5852       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5853
5854       switch (AT_class (a))
5855         {
5856         case dw_val_class_addr:
5857           fprintf (outfile, "address");
5858           break;
5859         case dw_val_class_offset:
5860           fprintf (outfile, "offset");
5861           break;
5862         case dw_val_class_loc:
5863           fprintf (outfile, "location descriptor");
5864           break;
5865         case dw_val_class_loc_list:
5866           fprintf (outfile, "location list -> label:%s",
5867                    AT_loc_list (a)->ll_symbol);
5868           break;
5869         case dw_val_class_range_list:
5870           fprintf (outfile, "range list");
5871           break;
5872         case dw_val_class_const:
5873           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5874           break;
5875         case dw_val_class_unsigned_const:
5876           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5877           break;
5878         case dw_val_class_long_long:
5879           fprintf (outfile, "constant (%lu,%lu)",
5880                    a->dw_attr_val.v.val_long_long.hi,
5881                    a->dw_attr_val.v.val_long_long.low);
5882           break;
5883         case dw_val_class_vec:
5884           fprintf (outfile, "floating-point or vector constant");
5885           break;
5886         case dw_val_class_flag:
5887           fprintf (outfile, "%u", AT_flag (a));
5888           break;
5889         case dw_val_class_die_ref:
5890           if (AT_ref (a) != NULL)
5891             {
5892               if (AT_ref (a)->die_symbol)
5893                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5894               else
5895                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5896             }
5897           else
5898             fprintf (outfile, "die -> <null>");
5899           break;
5900         case dw_val_class_lbl_id:
5901         case dw_val_class_lineptr:
5902         case dw_val_class_macptr:
5903           fprintf (outfile, "label: %s", AT_lbl (a));
5904           break;
5905         case dw_val_class_str:
5906           if (AT_string (a) != NULL)
5907             fprintf (outfile, "\"%s\"", AT_string (a));
5908           else
5909             fprintf (outfile, "<null>");
5910           break;
5911         case dw_val_class_file:
5912           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5913                    AT_file (a)->emitted_number);
5914           break;
5915         default:
5916           break;
5917         }
5918
5919       fprintf (outfile, "\n");
5920     }
5921
5922   if (die->die_child != NULL)
5923     {
5924       print_indent += 4;
5925       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5926       print_indent -= 4;
5927     }
5928   if (print_indent == 0)
5929     fprintf (outfile, "\n");
5930 }
5931
5932 /* Print the contents of the source code line number correspondence table.
5933    This routine is a debugging aid only.  */
5934
5935 static void
5936 print_dwarf_line_table (FILE *outfile)
5937 {
5938   unsigned i;
5939   dw_line_info_ref line_info;
5940
5941   fprintf (outfile, "\n\nDWARF source line information\n");
5942   for (i = 1; i < line_info_table_in_use; i++)
5943     {
5944       line_info = &line_info_table[i];
5945       fprintf (outfile, "%5d: %4ld %6ld\n", i,
5946                line_info->dw_file_num,
5947                line_info->dw_line_num);
5948     }
5949
5950   fprintf (outfile, "\n\n");
5951 }
5952
5953 /* Print the information collected for a given DIE.  */
5954
5955 void
5956 debug_dwarf_die (dw_die_ref die)
5957 {
5958   print_die (die, stderr);
5959 }
5960
5961 /* Print all DWARF information collected for the compilation unit.
5962    This routine is a debugging aid only.  */
5963
5964 void
5965 debug_dwarf (void)
5966 {
5967   print_indent = 0;
5968   print_die (comp_unit_die, stderr);
5969   if (! DWARF2_ASM_LINE_DEBUG_INFO)
5970     print_dwarf_line_table (stderr);
5971 }
5972 \f
5973 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5974    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5975    DIE that marks the start of the DIEs for this include file.  */
5976
5977 static dw_die_ref
5978 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5979 {
5980   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5981   dw_die_ref new_unit = gen_compile_unit_die (filename);
5982
5983   new_unit->die_sib = old_unit;
5984   return new_unit;
5985 }
5986
5987 /* Close an include-file CU and reopen the enclosing one.  */
5988
5989 static dw_die_ref
5990 pop_compile_unit (dw_die_ref old_unit)
5991 {
5992   dw_die_ref new_unit = old_unit->die_sib;
5993
5994   old_unit->die_sib = NULL;
5995   return new_unit;
5996 }
5997
5998 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5999 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6000
6001 /* Calculate the checksum of a location expression.  */
6002
6003 static inline void
6004 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6005 {
6006   CHECKSUM (loc->dw_loc_opc);
6007   CHECKSUM (loc->dw_loc_oprnd1);
6008   CHECKSUM (loc->dw_loc_oprnd2);
6009 }
6010
6011 /* Calculate the checksum of an attribute.  */
6012
6013 static void
6014 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6015 {
6016   dw_loc_descr_ref loc;
6017   rtx r;
6018
6019   CHECKSUM (at->dw_attr);
6020
6021   /* We don't care that this was compiled with a different compiler
6022      snapshot; if the output is the same, that's what matters.  */
6023   if (at->dw_attr == DW_AT_producer)
6024     return;
6025
6026   switch (AT_class (at))
6027     {
6028     case dw_val_class_const:
6029       CHECKSUM (at->dw_attr_val.v.val_int);
6030       break;
6031     case dw_val_class_unsigned_const:
6032       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6033       break;
6034     case dw_val_class_long_long:
6035       CHECKSUM (at->dw_attr_val.v.val_long_long);
6036       break;
6037     case dw_val_class_vec:
6038       CHECKSUM (at->dw_attr_val.v.val_vec);
6039       break;
6040     case dw_val_class_flag:
6041       CHECKSUM (at->dw_attr_val.v.val_flag);
6042       break;
6043     case dw_val_class_str:
6044       CHECKSUM_STRING (AT_string (at));
6045       break;
6046
6047     case dw_val_class_addr:
6048       r = AT_addr (at);
6049       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6050       CHECKSUM_STRING (XSTR (r, 0));
6051       break;
6052
6053     case dw_val_class_offset:
6054       CHECKSUM (at->dw_attr_val.v.val_offset);
6055       break;
6056
6057     case dw_val_class_loc:
6058       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6059         loc_checksum (loc, ctx);
6060       break;
6061
6062     case dw_val_class_die_ref:
6063       die_checksum (AT_ref (at), ctx, mark);
6064       break;
6065
6066     case dw_val_class_fde_ref:
6067     case dw_val_class_lbl_id:
6068     case dw_val_class_lineptr:
6069     case dw_val_class_macptr:
6070       break;
6071
6072     case dw_val_class_file:
6073       CHECKSUM_STRING (AT_file (at)->filename);
6074       break;
6075
6076     default:
6077       break;
6078     }
6079 }
6080
6081 /* Calculate the checksum of a DIE.  */
6082
6083 static void
6084 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6085 {
6086   dw_die_ref c;
6087   dw_attr_ref a;
6088   unsigned ix;
6089
6090   /* To avoid infinite recursion.  */
6091   if (die->die_mark)
6092     {
6093       CHECKSUM (die->die_mark);
6094       return;
6095     }
6096   die->die_mark = ++(*mark);
6097
6098   CHECKSUM (die->die_tag);
6099
6100   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6101     attr_checksum (a, ctx, mark);
6102
6103   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6104 }
6105
6106 #undef CHECKSUM
6107 #undef CHECKSUM_STRING
6108
6109 /* Do the location expressions look same?  */
6110 static inline int
6111 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6112 {
6113   return loc1->dw_loc_opc == loc2->dw_loc_opc
6114          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6115          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6116 }
6117
6118 /* Do the values look the same?  */
6119 static int
6120 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6121 {
6122   dw_loc_descr_ref loc1, loc2;
6123   rtx r1, r2;
6124
6125   if (v1->val_class != v2->val_class)
6126     return 0;
6127
6128   switch (v1->val_class)
6129     {
6130     case dw_val_class_const:
6131       return v1->v.val_int == v2->v.val_int;
6132     case dw_val_class_unsigned_const:
6133       return v1->v.val_unsigned == v2->v.val_unsigned;
6134     case dw_val_class_long_long:
6135       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6136              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6137     case dw_val_class_vec:
6138       if (v1->v.val_vec.length != v2->v.val_vec.length
6139           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6140         return 0;
6141       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6142                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6143         return 0;
6144       return 1;
6145     case dw_val_class_flag:
6146       return v1->v.val_flag == v2->v.val_flag;
6147     case dw_val_class_str:
6148       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6149
6150     case dw_val_class_addr:
6151       r1 = v1->v.val_addr;
6152       r2 = v2->v.val_addr;
6153       if (GET_CODE (r1) != GET_CODE (r2))
6154         return 0;
6155       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6156       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6157
6158     case dw_val_class_offset:
6159       return v1->v.val_offset == v2->v.val_offset;
6160
6161     case dw_val_class_loc:
6162       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6163            loc1 && loc2;
6164            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6165         if (!same_loc_p (loc1, loc2, mark))
6166           return 0;
6167       return !loc1 && !loc2;
6168
6169     case dw_val_class_die_ref:
6170       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6171
6172     case dw_val_class_fde_ref:
6173     case dw_val_class_lbl_id:
6174     case dw_val_class_lineptr:
6175     case dw_val_class_macptr:
6176       return 1;
6177
6178     case dw_val_class_file:
6179       return v1->v.val_file == v2->v.val_file;
6180
6181     default:
6182       return 1;
6183     }
6184 }
6185
6186 /* Do the attributes look the same?  */
6187
6188 static int
6189 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6190 {
6191   if (at1->dw_attr != at2->dw_attr)
6192     return 0;
6193
6194   /* We don't care that this was compiled with a different compiler
6195      snapshot; if the output is the same, that's what matters. */
6196   if (at1->dw_attr == DW_AT_producer)
6197     return 1;
6198
6199   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6200 }
6201
6202 /* Do the dies look the same?  */
6203
6204 static int
6205 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6206 {
6207   dw_die_ref c1, c2;
6208   dw_attr_ref a1;
6209   unsigned ix;
6210
6211   /* To avoid infinite recursion.  */
6212   if (die1->die_mark)
6213     return die1->die_mark == die2->die_mark;
6214   die1->die_mark = die2->die_mark = ++(*mark);
6215
6216   if (die1->die_tag != die2->die_tag)
6217     return 0;
6218
6219   if (VEC_length (dw_attr_node, die1->die_attr)
6220       != VEC_length (dw_attr_node, die2->die_attr))
6221     return 0;
6222
6223   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6224     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6225       return 0;
6226
6227   c1 = die1->die_child;
6228   c2 = die2->die_child;
6229   if (! c1)
6230     {
6231       if (c2)
6232         return 0;
6233     }
6234   else
6235     for (;;)
6236       {
6237         if (!same_die_p (c1, c2, mark))
6238           return 0;
6239         c1 = c1->die_sib;
6240         c2 = c2->die_sib;
6241         if (c1 == die1->die_child)
6242           {
6243             if (c2 == die2->die_child)
6244               break;
6245             else
6246               return 0;
6247           }
6248     }
6249
6250   return 1;
6251 }
6252
6253 /* Do the dies look the same?  Wrapper around same_die_p.  */
6254
6255 static int
6256 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6257 {
6258   int mark = 0;
6259   int ret = same_die_p (die1, die2, &mark);
6260
6261   unmark_all_dies (die1);
6262   unmark_all_dies (die2);
6263
6264   return ret;
6265 }
6266
6267 /* The prefix to attach to symbols on DIEs in the current comdat debug
6268    info section.  */
6269 static char *comdat_symbol_id;
6270
6271 /* The index of the current symbol within the current comdat CU.  */
6272 static unsigned int comdat_symbol_number;
6273
6274 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6275    children, and set comdat_symbol_id accordingly.  */
6276
6277 static void
6278 compute_section_prefix (dw_die_ref unit_die)
6279 {
6280   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6281   const char *base = die_name ? lbasename (die_name) : "anonymous";
6282   char *name = alloca (strlen (base) + 64);
6283   char *p;
6284   int i, mark;
6285   unsigned char checksum[16];
6286   struct md5_ctx ctx;
6287
6288   /* Compute the checksum of the DIE, then append part of it as hex digits to
6289      the name filename of the unit.  */
6290
6291   md5_init_ctx (&ctx);
6292   mark = 0;
6293   die_checksum (unit_die, &ctx, &mark);
6294   unmark_all_dies (unit_die);
6295   md5_finish_ctx (&ctx, checksum);
6296
6297   sprintf (name, "%s.", base);
6298   clean_symbol_name (name);
6299
6300   p = name + strlen (name);
6301   for (i = 0; i < 4; i++)
6302     {
6303       sprintf (p, "%.2x", checksum[i]);
6304       p += 2;
6305     }
6306
6307   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6308   comdat_symbol_number = 0;
6309 }
6310
6311 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6312
6313 static int
6314 is_type_die (dw_die_ref die)
6315 {
6316   switch (die->die_tag)
6317     {
6318     case DW_TAG_array_type:
6319     case DW_TAG_class_type:
6320     case DW_TAG_interface_type:
6321     case DW_TAG_enumeration_type:
6322     case DW_TAG_pointer_type:
6323     case DW_TAG_reference_type:
6324     case DW_TAG_string_type:
6325     case DW_TAG_structure_type:
6326     case DW_TAG_subroutine_type:
6327     case DW_TAG_union_type:
6328     case DW_TAG_ptr_to_member_type:
6329     case DW_TAG_set_type:
6330     case DW_TAG_subrange_type:
6331     case DW_TAG_base_type:
6332     case DW_TAG_const_type:
6333     case DW_TAG_file_type:
6334     case DW_TAG_packed_type:
6335     case DW_TAG_volatile_type:
6336     case DW_TAG_typedef:
6337       return 1;
6338     default:
6339       return 0;
6340     }
6341 }
6342
6343 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6344    Basically, we want to choose the bits that are likely to be shared between
6345    compilations (types) and leave out the bits that are specific to individual
6346    compilations (functions).  */
6347
6348 static int
6349 is_comdat_die (dw_die_ref c)
6350 {
6351   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6352      we do for stabs.  The advantage is a greater likelihood of sharing between
6353      objects that don't include headers in the same order (and therefore would
6354      put the base types in a different comdat).  jason 8/28/00 */
6355
6356   if (c->die_tag == DW_TAG_base_type)
6357     return 0;
6358
6359   if (c->die_tag == DW_TAG_pointer_type
6360       || c->die_tag == DW_TAG_reference_type
6361       || c->die_tag == DW_TAG_const_type
6362       || c->die_tag == DW_TAG_volatile_type)
6363     {
6364       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6365
6366       return t ? is_comdat_die (t) : 0;
6367     }
6368
6369   return is_type_die (c);
6370 }
6371
6372 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6373    compilation unit.  */
6374
6375 static int
6376 is_symbol_die (dw_die_ref c)
6377 {
6378   return (is_type_die (c)
6379           || (get_AT (c, DW_AT_declaration)
6380               && !get_AT (c, DW_AT_specification))
6381           || c->die_tag == DW_TAG_namespace);
6382 }
6383
6384 static char *
6385 gen_internal_sym (const char *prefix)
6386 {
6387   char buf[256];
6388
6389   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6390   return xstrdup (buf);
6391 }
6392
6393 /* Assign symbols to all worthy DIEs under DIE.  */
6394
6395 static void
6396 assign_symbol_names (dw_die_ref die)
6397 {
6398   dw_die_ref c;
6399
6400   if (is_symbol_die (die))
6401     {
6402       if (comdat_symbol_id)
6403         {
6404           char *p = alloca (strlen (comdat_symbol_id) + 64);
6405
6406           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6407                    comdat_symbol_id, comdat_symbol_number++);
6408           die->die_symbol = xstrdup (p);
6409         }
6410       else
6411         die->die_symbol = gen_internal_sym ("LDIE");
6412     }
6413
6414   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6415 }
6416
6417 struct cu_hash_table_entry
6418 {
6419   dw_die_ref cu;
6420   unsigned min_comdat_num, max_comdat_num;
6421   struct cu_hash_table_entry *next;
6422 };
6423
6424 /* Routines to manipulate hash table of CUs.  */
6425 static hashval_t
6426 htab_cu_hash (const void *of)
6427 {
6428   const struct cu_hash_table_entry *entry = of;
6429
6430   return htab_hash_string (entry->cu->die_symbol);
6431 }
6432
6433 static int
6434 htab_cu_eq (const void *of1, const void *of2)
6435 {
6436   const struct cu_hash_table_entry *entry1 = of1;
6437   const struct die_struct *entry2 = of2;
6438
6439   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6440 }
6441
6442 static void
6443 htab_cu_del (void *what)
6444 {
6445   struct cu_hash_table_entry *next, *entry = what;
6446
6447   while (entry)
6448     {
6449       next = entry->next;
6450       free (entry);
6451       entry = next;
6452     }
6453 }
6454
6455 /* Check whether we have already seen this CU and set up SYM_NUM
6456    accordingly.  */
6457 static int
6458 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6459 {
6460   struct cu_hash_table_entry dummy;
6461   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6462
6463   dummy.max_comdat_num = 0;
6464
6465   slot = (struct cu_hash_table_entry **)
6466     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6467         INSERT);
6468   entry = *slot;
6469
6470   for (; entry; last = entry, entry = entry->next)
6471     {
6472       if (same_die_p_wrap (cu, entry->cu))
6473         break;
6474     }
6475
6476   if (entry)
6477     {
6478       *sym_num = entry->min_comdat_num;
6479       return 1;
6480     }
6481
6482   entry = XCNEW (struct cu_hash_table_entry);
6483   entry->cu = cu;
6484   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6485   entry->next = *slot;
6486   *slot = entry;
6487
6488   return 0;
6489 }
6490
6491 /* Record SYM_NUM to record of CU in HTABLE.  */
6492 static void
6493 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6494 {
6495   struct cu_hash_table_entry **slot, *entry;
6496
6497   slot = (struct cu_hash_table_entry **)
6498     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6499         NO_INSERT);
6500   entry = *slot;
6501
6502   entry->max_comdat_num = sym_num;
6503 }
6504
6505 /* Traverse the DIE (which is always comp_unit_die), and set up
6506    additional compilation units for each of the include files we see
6507    bracketed by BINCL/EINCL.  */
6508
6509 static void
6510 break_out_includes (dw_die_ref die)
6511 {
6512   dw_die_ref c;
6513   dw_die_ref unit = NULL;
6514   limbo_die_node *node, **pnode;
6515   htab_t cu_hash_table;
6516
6517   c = die->die_child;
6518   if (c) do {
6519     dw_die_ref prev = c;
6520     c = c->die_sib;
6521     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6522            || (unit && is_comdat_die (c)))
6523       {
6524         dw_die_ref next = c->die_sib;
6525
6526         /* This DIE is for a secondary CU; remove it from the main one.  */
6527         remove_child_with_prev (c, prev);
6528
6529         if (c->die_tag == DW_TAG_GNU_BINCL)
6530           unit = push_new_compile_unit (unit, c);
6531         else if (c->die_tag == DW_TAG_GNU_EINCL)
6532           unit = pop_compile_unit (unit);
6533         else
6534           add_child_die (unit, c);
6535         c = next;
6536         if (c == die->die_child)
6537           break;
6538       }
6539   } while (c != die->die_child);
6540
6541 #if 0
6542   /* We can only use this in debugging, since the frontend doesn't check
6543      to make sure that we leave every include file we enter.  */
6544   gcc_assert (!unit);
6545 #endif
6546
6547   assign_symbol_names (die);
6548   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6549   for (node = limbo_die_list, pnode = &limbo_die_list;
6550        node;
6551        node = node->next)
6552     {
6553       int is_dupl;
6554
6555       compute_section_prefix (node->die);
6556       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6557                         &comdat_symbol_number);
6558       assign_symbol_names (node->die);
6559       if (is_dupl)
6560         *pnode = node->next;
6561       else
6562         {
6563           pnode = &node->next;
6564           record_comdat_symbol_number (node->die, cu_hash_table,
6565                 comdat_symbol_number);
6566         }
6567     }
6568   htab_delete (cu_hash_table);
6569 }
6570
6571 /* Traverse the DIE and add a sibling attribute if it may have the
6572    effect of speeding up access to siblings.  To save some space,
6573    avoid generating sibling attributes for DIE's without children.  */
6574
6575 static void
6576 add_sibling_attributes (dw_die_ref die)
6577 {
6578   dw_die_ref c;
6579
6580   if (! die->die_child)
6581     return;
6582
6583   if (die->die_parent && die != die->die_parent->die_child)
6584     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6585
6586   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6587 }
6588
6589 /* Output all location lists for the DIE and its children.  */
6590
6591 static void
6592 output_location_lists (dw_die_ref die)
6593 {
6594   dw_die_ref c;
6595   dw_attr_ref a;
6596   unsigned ix;
6597
6598   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6599     if (AT_class (a) == dw_val_class_loc_list)
6600       output_loc_list (AT_loc_list (a));
6601
6602   FOR_EACH_CHILD (die, c, output_location_lists (c));
6603 }
6604
6605 /* The format of each DIE (and its attribute value pairs) is encoded in an
6606    abbreviation table.  This routine builds the abbreviation table and assigns
6607    a unique abbreviation id for each abbreviation entry.  The children of each
6608    die are visited recursively.  */
6609
6610 static void
6611 build_abbrev_table (dw_die_ref die)
6612 {
6613   unsigned long abbrev_id;
6614   unsigned int n_alloc;
6615   dw_die_ref c;
6616   dw_attr_ref a;
6617   unsigned ix;
6618
6619   /* Scan the DIE references, and mark as external any that refer to
6620      DIEs from other CUs (i.e. those which are not marked).  */
6621   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6622     if (AT_class (a) == dw_val_class_die_ref
6623         && AT_ref (a)->die_mark == 0)
6624       {
6625         gcc_assert (AT_ref (a)->die_symbol);
6626
6627         set_AT_ref_external (a, 1);
6628       }
6629
6630   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6631     {
6632       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6633       dw_attr_ref die_a, abbrev_a;
6634       unsigned ix;
6635       bool ok = true;
6636
6637       if (abbrev->die_tag != die->die_tag)
6638         continue;
6639       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6640         continue;
6641
6642       if (VEC_length (dw_attr_node, abbrev->die_attr)
6643           != VEC_length (dw_attr_node, die->die_attr))
6644         continue;
6645
6646       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6647         {
6648           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6649           if ((abbrev_a->dw_attr != die_a->dw_attr)
6650               || (value_format (abbrev_a) != value_format (die_a)))
6651             {
6652               ok = false;
6653               break;
6654             }
6655         }
6656       if (ok)
6657         break;
6658     }
6659
6660   if (abbrev_id >= abbrev_die_table_in_use)
6661     {
6662       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6663         {
6664           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6665           abbrev_die_table = ggc_realloc (abbrev_die_table,
6666                                           sizeof (dw_die_ref) * n_alloc);
6667
6668           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6669                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6670           abbrev_die_table_allocated = n_alloc;
6671         }
6672
6673       ++abbrev_die_table_in_use;
6674       abbrev_die_table[abbrev_id] = die;
6675     }
6676
6677   die->die_abbrev = abbrev_id;
6678   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6679 }
6680 \f
6681 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6682
6683 static int
6684 constant_size (long unsigned int value)
6685 {
6686   int log;
6687
6688   if (value == 0)
6689     log = 0;
6690   else
6691     log = floor_log2 (value);
6692
6693   log = log / 8;
6694   log = 1 << (floor_log2 (log) + 1);
6695
6696   return log;
6697 }
6698
6699 /* Return the size of a DIE as it is represented in the
6700    .debug_info section.  */
6701
6702 static unsigned long
6703 size_of_die (dw_die_ref die)
6704 {
6705   unsigned long size = 0;
6706   dw_attr_ref a;
6707   unsigned ix;
6708
6709   size += size_of_uleb128 (die->die_abbrev);
6710   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6711     {
6712       switch (AT_class (a))
6713         {
6714         case dw_val_class_addr:
6715           size += DWARF2_ADDR_SIZE;
6716           break;
6717         case dw_val_class_offset:
6718           size += DWARF_OFFSET_SIZE;
6719           break;
6720         case dw_val_class_loc:
6721           {
6722             unsigned long lsize = size_of_locs (AT_loc (a));
6723
6724             /* Block length.  */
6725             size += constant_size (lsize);
6726             size += lsize;
6727           }
6728           break;
6729         case dw_val_class_loc_list:
6730           size += DWARF_OFFSET_SIZE;
6731           break;
6732         case dw_val_class_range_list:
6733           size += DWARF_OFFSET_SIZE;
6734           break;
6735         case dw_val_class_const:
6736           size += size_of_sleb128 (AT_int (a));
6737           break;
6738         case dw_val_class_unsigned_const:
6739           size += constant_size (AT_unsigned (a));
6740           break;
6741         case dw_val_class_long_long:
6742           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6743           break;
6744         case dw_val_class_vec:
6745           size += 1 + (a->dw_attr_val.v.val_vec.length
6746                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6747           break;
6748         case dw_val_class_flag:
6749           size += 1;
6750           break;
6751         case dw_val_class_die_ref:
6752           if (AT_ref_external (a))
6753             size += DWARF2_ADDR_SIZE;
6754           else
6755             size += DWARF_OFFSET_SIZE;
6756           break;
6757         case dw_val_class_fde_ref:
6758           size += DWARF_OFFSET_SIZE;
6759           break;
6760         case dw_val_class_lbl_id:
6761           size += DWARF2_ADDR_SIZE;
6762           break;
6763         case dw_val_class_lineptr:
6764         case dw_val_class_macptr:
6765           size += DWARF_OFFSET_SIZE;
6766           break;
6767         case dw_val_class_str:
6768           if (AT_string_form (a) == DW_FORM_strp)
6769             size += DWARF_OFFSET_SIZE;
6770           else
6771             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6772           break;
6773         case dw_val_class_file:
6774           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6775           break;
6776         default:
6777           gcc_unreachable ();
6778         }
6779     }
6780
6781   return size;
6782 }
6783
6784 /* Size the debugging information associated with a given DIE.  Visits the
6785    DIE's children recursively.  Updates the global variable next_die_offset, on
6786    each time through.  Uses the current value of next_die_offset to update the
6787    die_offset field in each DIE.  */
6788
6789 static void
6790 calc_die_sizes (dw_die_ref die)
6791 {
6792   dw_die_ref c;
6793
6794   die->die_offset = next_die_offset;
6795   next_die_offset += size_of_die (die);
6796
6797   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6798
6799   if (die->die_child != NULL)
6800     /* Count the null byte used to terminate sibling lists.  */
6801     next_die_offset += 1;
6802 }
6803
6804 /* Set the marks for a die and its children.  We do this so
6805    that we know whether or not a reference needs to use FORM_ref_addr; only
6806    DIEs in the same CU will be marked.  We used to clear out the offset
6807    and use that as the flag, but ran into ordering problems.  */
6808
6809 static void
6810 mark_dies (dw_die_ref die)
6811 {
6812   dw_die_ref c;
6813
6814   gcc_assert (!die->die_mark);
6815
6816   die->die_mark = 1;
6817   FOR_EACH_CHILD (die, c, mark_dies (c));
6818 }
6819
6820 /* Clear the marks for a die and its children.  */
6821
6822 static void
6823 unmark_dies (dw_die_ref die)
6824 {
6825   dw_die_ref c;
6826
6827   gcc_assert (die->die_mark);
6828
6829   die->die_mark = 0;
6830   FOR_EACH_CHILD (die, c, unmark_dies (c));
6831 }
6832
6833 /* Clear the marks for a die, its children and referred dies.  */
6834
6835 static void
6836 unmark_all_dies (dw_die_ref die)
6837 {
6838   dw_die_ref c;
6839   dw_attr_ref a;
6840   unsigned ix;
6841
6842   if (!die->die_mark)
6843     return;
6844   die->die_mark = 0;
6845
6846   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6847
6848   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6849     if (AT_class (a) == dw_val_class_die_ref)
6850       unmark_all_dies (AT_ref (a));
6851 }
6852
6853 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6854    generated for the compilation unit.  */
6855
6856 static unsigned long
6857 size_of_pubnames (VEC (pubname_entry, gc) * names)
6858 {
6859   unsigned long size;
6860   unsigned i;
6861   pubname_ref p;
6862
6863   size = DWARF_PUBNAMES_HEADER_SIZE;
6864   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6865     if (names != pubtype_table
6866         || p->die->die_offset != 0
6867         || !flag_eliminate_unused_debug_types)
6868       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6869
6870   size += DWARF_OFFSET_SIZE;
6871   return size;
6872 }
6873
6874 /* Return the size of the information in the .debug_aranges section.  */
6875
6876 static unsigned long
6877 size_of_aranges (void)
6878 {
6879   unsigned long size;
6880
6881   size = DWARF_ARANGES_HEADER_SIZE;
6882
6883   /* Count the address/length pair for this compilation unit.  */
6884   if (text_section_used)
6885     size += 2 * DWARF2_ADDR_SIZE;
6886   if (cold_text_section_used)
6887     size += 2 * DWARF2_ADDR_SIZE;
6888   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6889
6890   /* Count the two zero words used to terminated the address range table.  */
6891   size += 2 * DWARF2_ADDR_SIZE;
6892   return size;
6893 }
6894 \f
6895 /* Select the encoding of an attribute value.  */
6896
6897 static enum dwarf_form
6898 value_format (dw_attr_ref a)
6899 {
6900   switch (a->dw_attr_val.val_class)
6901     {
6902     case dw_val_class_addr:
6903       return DW_FORM_addr;
6904     case dw_val_class_range_list:
6905     case dw_val_class_offset:
6906     case dw_val_class_loc_list:
6907       switch (DWARF_OFFSET_SIZE)
6908         {
6909         case 4:
6910           return DW_FORM_data4;
6911         case 8:
6912           return DW_FORM_data8;
6913         default:
6914           gcc_unreachable ();
6915         }
6916     case dw_val_class_loc:
6917       switch (constant_size (size_of_locs (AT_loc (a))))
6918         {
6919         case 1:
6920           return DW_FORM_block1;
6921         case 2:
6922           return DW_FORM_block2;
6923         default:
6924           gcc_unreachable ();
6925         }
6926     case dw_val_class_const:
6927       return DW_FORM_sdata;
6928     case dw_val_class_unsigned_const:
6929       switch (constant_size (AT_unsigned (a)))
6930         {
6931         case 1:
6932           return DW_FORM_data1;
6933         case 2:
6934           return DW_FORM_data2;
6935         case 4:
6936           return DW_FORM_data4;
6937         case 8:
6938           return DW_FORM_data8;
6939         default:
6940           gcc_unreachable ();
6941         }
6942     case dw_val_class_long_long:
6943       return DW_FORM_block1;
6944     case dw_val_class_vec:
6945       return DW_FORM_block1;
6946     case dw_val_class_flag:
6947       return DW_FORM_flag;
6948     case dw_val_class_die_ref:
6949       if (AT_ref_external (a))
6950         return DW_FORM_ref_addr;
6951       else
6952         return DW_FORM_ref;
6953     case dw_val_class_fde_ref:
6954       return DW_FORM_data;
6955     case dw_val_class_lbl_id:
6956       return DW_FORM_addr;
6957     case dw_val_class_lineptr:
6958     case dw_val_class_macptr:
6959       return DW_FORM_data;
6960     case dw_val_class_str:
6961       return AT_string_form (a);
6962     case dw_val_class_file:
6963       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
6964         {
6965         case 1:
6966           return DW_FORM_data1;
6967         case 2:
6968           return DW_FORM_data2;
6969         case 4:
6970           return DW_FORM_data4;
6971         default:
6972           gcc_unreachable ();
6973         }
6974
6975     default:
6976       gcc_unreachable ();
6977     }
6978 }
6979
6980 /* Output the encoding of an attribute value.  */
6981
6982 static void
6983 output_value_format (dw_attr_ref a)
6984 {
6985   enum dwarf_form form = value_format (a);
6986
6987   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
6988 }
6989
6990 /* Output the .debug_abbrev section which defines the DIE abbreviation
6991    table.  */
6992
6993 static void
6994 output_abbrev_section (void)
6995 {
6996   unsigned long abbrev_id;
6997
6998   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6999     {
7000       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7001       unsigned ix;
7002       dw_attr_ref a_attr;
7003
7004       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7005       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7006                                    dwarf_tag_name (abbrev->die_tag));
7007
7008       if (abbrev->die_child != NULL)
7009         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7010       else
7011         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7012
7013       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7014            ix++)
7015         {
7016           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7017                                        dwarf_attr_name (a_attr->dw_attr));
7018           output_value_format (a_attr);
7019         }
7020
7021       dw2_asm_output_data (1, 0, NULL);
7022       dw2_asm_output_data (1, 0, NULL);
7023     }
7024
7025   /* Terminate the table.  */
7026   dw2_asm_output_data (1, 0, NULL);
7027 }
7028
7029 /* Output a symbol we can use to refer to this DIE from another CU.  */
7030
7031 static inline void
7032 output_die_symbol (dw_die_ref die)
7033 {
7034   char *sym = die->die_symbol;
7035
7036   if (sym == 0)
7037     return;
7038
7039   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7040     /* We make these global, not weak; if the target doesn't support
7041        .linkonce, it doesn't support combining the sections, so debugging
7042        will break.  */
7043     targetm.asm_out.globalize_label (asm_out_file, sym);
7044
7045   ASM_OUTPUT_LABEL (asm_out_file, sym);
7046 }
7047
7048 /* Return a new location list, given the begin and end range, and the
7049    expression. gensym tells us whether to generate a new internal symbol for
7050    this location list node, which is done for the head of the list only.  */
7051
7052 static inline dw_loc_list_ref
7053 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7054               const char *section, unsigned int gensym)
7055 {
7056   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7057
7058   retlist->begin = begin;
7059   retlist->end = end;
7060   retlist->expr = expr;
7061   retlist->section = section;
7062   if (gensym)
7063     retlist->ll_symbol = gen_internal_sym ("LLST");
7064
7065   return retlist;
7066 }
7067
7068 /* Add a location description expression to a location list.  */
7069
7070 static inline void
7071 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7072                            const char *begin, const char *end,
7073                            const char *section)
7074 {
7075   dw_loc_list_ref *d;
7076
7077   /* Find the end of the chain.  */
7078   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7079     ;
7080
7081   /* Add a new location list node to the list.  */
7082   *d = new_loc_list (descr, begin, end, section, 0);
7083 }
7084
7085 /* Note that the current function section is being used for code.  */
7086
7087 static void
7088 dwarf2out_note_section_used (void)
7089 {
7090   section *sec = current_function_section ();
7091   if (sec == text_section)
7092     text_section_used = true;
7093   else if (sec == cold_text_section)
7094     cold_text_section_used = true;
7095 }
7096
7097 static void
7098 dwarf2out_switch_text_section (void)
7099 {
7100   dw_fde_ref fde;
7101
7102   gcc_assert (cfun);
7103
7104   fde = &fde_table[fde_table_in_use - 1];
7105   fde->dw_fde_switched_sections = true;
7106   fde->dw_fde_hot_section_label = cfun->hot_section_label;
7107   fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
7108   fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
7109   fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
7110   have_multiple_function_sections = true;
7111
7112   /* Reset the current label on switching text sections, so that we
7113      don't attempt to advance_loc4 between labels in different sections.  */
7114   fde->dw_fde_current_label = NULL;
7115
7116   dwarf2out_note_section_used ();
7117 }
7118
7119 /* Output the location list given to us.  */
7120
7121 static void
7122 output_loc_list (dw_loc_list_ref list_head)
7123 {
7124   dw_loc_list_ref curr = list_head;
7125
7126   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7127
7128   /* Walk the location list, and output each range + expression.  */
7129   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7130     {
7131       unsigned long size;
7132       /* Don't output an entry that starts and ends at the same address.  */
7133       if (strcmp (curr->begin, curr->end) == 0)
7134         continue;
7135       if (!have_multiple_function_sections)
7136         {
7137           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7138                                 "Location list begin address (%s)",
7139                                 list_head->ll_symbol);
7140           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7141                                 "Location list end address (%s)",
7142                                 list_head->ll_symbol);
7143         }
7144       else
7145         {
7146           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7147                                "Location list begin address (%s)",
7148                                list_head->ll_symbol);
7149           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7150                                "Location list end address (%s)",
7151                                list_head->ll_symbol);
7152         }
7153       size = size_of_locs (curr->expr);
7154
7155       /* Output the block length for this list of location operations.  */
7156       gcc_assert (size <= 0xffff);
7157       dw2_asm_output_data (2, size, "%s", "Location expression size");
7158
7159       output_loc_sequence (curr->expr);
7160     }
7161
7162   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7163                        "Location list terminator begin (%s)",
7164                        list_head->ll_symbol);
7165   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7166                        "Location list terminator end (%s)",
7167                        list_head->ll_symbol);
7168 }
7169
7170 /* Output the DIE and its attributes.  Called recursively to generate
7171    the definitions of each child DIE.  */
7172
7173 static void
7174 output_die (dw_die_ref die)
7175 {
7176   dw_attr_ref a;
7177   dw_die_ref c;
7178   unsigned long size;
7179   unsigned ix;
7180
7181   /* If someone in another CU might refer to us, set up a symbol for
7182      them to point to.  */
7183   if (die->die_symbol)
7184     output_die_symbol (die);
7185
7186   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7187                                (unsigned long)die->die_offset,
7188                                dwarf_tag_name (die->die_tag));
7189
7190   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7191     {
7192       const char *name = dwarf_attr_name (a->dw_attr);
7193
7194       switch (AT_class (a))
7195         {
7196         case dw_val_class_addr:
7197           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7198           break;
7199
7200         case dw_val_class_offset:
7201           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7202                                "%s", name);
7203           break;
7204
7205         case dw_val_class_range_list:
7206           {
7207             char *p = strchr (ranges_section_label, '\0');
7208
7209             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7210                      a->dw_attr_val.v.val_offset);
7211             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7212                                    debug_ranges_section, "%s", name);
7213             *p = '\0';
7214           }
7215           break;
7216
7217         case dw_val_class_loc:
7218           size = size_of_locs (AT_loc (a));
7219
7220           /* Output the block length for this list of location operations.  */
7221           dw2_asm_output_data (constant_size (size), size, "%s", name);
7222
7223           output_loc_sequence (AT_loc (a));
7224           break;
7225
7226         case dw_val_class_const:
7227           /* ??? It would be slightly more efficient to use a scheme like is
7228              used for unsigned constants below, but gdb 4.x does not sign
7229              extend.  Gdb 5.x does sign extend.  */
7230           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7231           break;
7232
7233         case dw_val_class_unsigned_const:
7234           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7235                                AT_unsigned (a), "%s", name);
7236           break;
7237
7238         case dw_val_class_long_long:
7239           {
7240             unsigned HOST_WIDE_INT first, second;
7241
7242             dw2_asm_output_data (1,
7243                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7244                                  "%s", name);
7245
7246             if (WORDS_BIG_ENDIAN)
7247               {
7248                 first = a->dw_attr_val.v.val_long_long.hi;
7249                 second = a->dw_attr_val.v.val_long_long.low;
7250               }
7251             else
7252               {
7253                 first = a->dw_attr_val.v.val_long_long.low;
7254                 second = a->dw_attr_val.v.val_long_long.hi;
7255               }
7256
7257             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7258                                  first, "long long constant");
7259             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7260                                  second, NULL);
7261           }
7262           break;
7263
7264         case dw_val_class_vec:
7265           {
7266             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7267             unsigned int len = a->dw_attr_val.v.val_vec.length;
7268             unsigned int i;
7269             unsigned char *p;
7270
7271             dw2_asm_output_data (1, len * elt_size, "%s", name);
7272             if (elt_size > sizeof (HOST_WIDE_INT))
7273               {
7274                 elt_size /= 2;
7275                 len *= 2;
7276               }
7277             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7278                  i < len;
7279                  i++, p += elt_size)
7280               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7281                                    "fp or vector constant word %u", i);
7282             break;
7283           }
7284
7285         case dw_val_class_flag:
7286           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7287           break;
7288
7289         case dw_val_class_loc_list:
7290           {
7291             char *sym = AT_loc_list (a)->ll_symbol;
7292
7293             gcc_assert (sym);
7294             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7295                                    "%s", name);
7296           }
7297           break;
7298
7299         case dw_val_class_die_ref:
7300           if (AT_ref_external (a))
7301             {
7302               char *sym = AT_ref (a)->die_symbol;
7303
7304               gcc_assert (sym);
7305               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7306                                      "%s", name);
7307             }
7308           else
7309             {
7310               gcc_assert (AT_ref (a)->die_offset);
7311               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7312                                    "%s", name);
7313             }
7314           break;
7315
7316         case dw_val_class_fde_ref:
7317           {
7318             char l1[20];
7319
7320             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7321                                          a->dw_attr_val.v.val_fde_index * 2);
7322             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7323                                    "%s", name);
7324           }
7325           break;
7326
7327         case dw_val_class_lbl_id:
7328           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7329           break;
7330
7331         case dw_val_class_lineptr:
7332           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7333                                  debug_line_section, "%s", name);
7334           break;
7335
7336         case dw_val_class_macptr:
7337           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7338                                  debug_macinfo_section, "%s", name);
7339           break;
7340
7341         case dw_val_class_str:
7342           if (AT_string_form (a) == DW_FORM_strp)
7343             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7344                                    a->dw_attr_val.v.val_str->label,
7345                                    debug_str_section,
7346                                    "%s: \"%s\"", name, AT_string (a));
7347           else
7348             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7349           break;
7350
7351         case dw_val_class_file:
7352           {
7353             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7354
7355             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7356                                  a->dw_attr_val.v.val_file->filename);
7357             break;
7358           }
7359
7360         default:
7361           gcc_unreachable ();
7362         }
7363     }
7364
7365   FOR_EACH_CHILD (die, c, output_die (c));
7366
7367   /* Add null byte to terminate sibling list.  */
7368   if (die->die_child != NULL)
7369     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7370                          (unsigned long) die->die_offset);
7371 }
7372
7373 /* Output the compilation unit that appears at the beginning of the
7374    .debug_info section, and precedes the DIE descriptions.  */
7375
7376 static void
7377 output_compilation_unit_header (void)
7378 {
7379   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7380     dw2_asm_output_data (4, 0xffffffff,
7381       "Initial length escape value indicating 64-bit DWARF extension");
7382   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7383                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7384                        "Length of Compilation Unit Info");
7385   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7386   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7387                          debug_abbrev_section,
7388                          "Offset Into Abbrev. Section");
7389   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7390 }
7391
7392 /* Output the compilation unit DIE and its children.  */
7393
7394 static void
7395 output_comp_unit (dw_die_ref die, int output_if_empty)
7396 {
7397   const char *secname;
7398   char *oldsym, *tmp;
7399
7400   /* Unless we are outputting main CU, we may throw away empty ones.  */
7401   if (!output_if_empty && die->die_child == NULL)
7402     return;
7403
7404   /* Even if there are no children of this DIE, we must output the information
7405      about the compilation unit.  Otherwise, on an empty translation unit, we
7406      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7407      will then complain when examining the file.  First mark all the DIEs in
7408      this CU so we know which get local refs.  */
7409   mark_dies (die);
7410
7411   build_abbrev_table (die);
7412
7413   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7414   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7415   calc_die_sizes (die);
7416
7417   oldsym = die->die_symbol;
7418   if (oldsym)
7419     {
7420       tmp = alloca (strlen (oldsym) + 24);
7421
7422       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7423       secname = tmp;
7424       die->die_symbol = NULL;
7425       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7426     }
7427   else
7428     switch_to_section (debug_info_section);
7429
7430   /* Output debugging information.  */
7431   output_compilation_unit_header ();
7432   output_die (die);
7433
7434   /* Leave the marks on the main CU, so we can check them in
7435      output_pubnames.  */
7436   if (oldsym)
7437     {
7438       unmark_dies (die);
7439       die->die_symbol = oldsym;
7440     }
7441 }
7442
7443 /* Return the DWARF2/3 pubname associated with a decl.  */
7444
7445 static const char *
7446 dwarf2_name (tree decl, int scope)
7447 {
7448   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7449 }
7450
7451 /* Add a new entry to .debug_pubnames if appropriate.  */
7452
7453 static void
7454 add_pubname (tree decl, dw_die_ref die)
7455 {
7456   pubname_entry e;
7457
7458   if (! TREE_PUBLIC (decl))
7459     return;
7460
7461   e.die = die;
7462   e.name = xstrdup (dwarf2_name (decl, 1));
7463   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7464 }
7465
7466 /* Add a new entry to .debug_pubtypes if appropriate.  */
7467
7468 static void
7469 add_pubtype (tree decl, dw_die_ref die)
7470 {
7471   pubname_entry e;
7472
7473   e.name = NULL;
7474   if ((TREE_PUBLIC (decl)
7475        || die->die_parent == comp_unit_die)
7476       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7477     {
7478       e.die = die;
7479       if (TYPE_P (decl))
7480         {
7481           if (TYPE_NAME (decl))
7482             {
7483               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7484                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7485               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7486                        && DECL_NAME (TYPE_NAME (decl)))
7487                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7488               else
7489                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7490             }
7491         }
7492       else
7493         e.name = xstrdup (dwarf2_name (decl, 1));
7494
7495       /* If we don't have a name for the type, there's no point in adding
7496          it to the table.  */
7497       if (e.name && e.name[0] != '\0')
7498         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7499     }
7500 }
7501
7502 /* Output the public names table used to speed up access to externally
7503    visible names; or the public types table used to find type definitions.  */
7504
7505 static void
7506 output_pubnames (VEC (pubname_entry, gc) * names)
7507 {
7508   unsigned i;
7509   unsigned long pubnames_length = size_of_pubnames (names);
7510   pubname_ref pub;
7511
7512   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7513     dw2_asm_output_data (4, 0xffffffff,
7514       "Initial length escape value indicating 64-bit DWARF extension");
7515   if (names == pubname_table)
7516     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7517                          "Length of Public Names Info");
7518   else
7519     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7520                          "Length of Public Type Names Info");
7521   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7522   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7523                          debug_info_section,
7524                          "Offset of Compilation Unit Info");
7525   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7526                        "Compilation Unit Length");
7527
7528   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7529     {
7530       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7531       if (names == pubname_table)
7532         gcc_assert (pub->die->die_mark);
7533
7534       if (names != pubtype_table
7535           || pub->die->die_offset != 0
7536           || !flag_eliminate_unused_debug_types)
7537         {
7538           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7539                                "DIE offset");
7540
7541           dw2_asm_output_nstring (pub->name, -1, "external name");
7542         }
7543     }
7544
7545   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7546 }
7547
7548 /* Add a new entry to .debug_aranges if appropriate.  */
7549
7550 static void
7551 add_arange (tree decl, dw_die_ref die)
7552 {
7553   if (! DECL_SECTION_NAME (decl))
7554     return;
7555
7556   if (arange_table_in_use == arange_table_allocated)
7557     {
7558       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7559       arange_table = ggc_realloc (arange_table,
7560                                   (arange_table_allocated
7561                                    * sizeof (dw_die_ref)));
7562       memset (arange_table + arange_table_in_use, 0,
7563               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7564     }
7565
7566   arange_table[arange_table_in_use++] = die;
7567 }
7568
7569 /* Output the information that goes into the .debug_aranges table.
7570    Namely, define the beginning and ending address range of the
7571    text section generated for this compilation unit.  */
7572
7573 static void
7574 output_aranges (void)
7575 {
7576   unsigned i;
7577   unsigned long aranges_length = size_of_aranges ();
7578
7579   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7580     dw2_asm_output_data (4, 0xffffffff,
7581       "Initial length escape value indicating 64-bit DWARF extension");
7582   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7583                        "Length of Address Ranges Info");
7584   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7585   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7586                          debug_info_section,
7587                          "Offset of Compilation Unit Info");
7588   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7589   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7590
7591   /* We need to align to twice the pointer size here.  */
7592   if (DWARF_ARANGES_PAD_SIZE)
7593     {
7594       /* Pad using a 2 byte words so that padding is correct for any
7595          pointer size.  */
7596       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7597                            2 * DWARF2_ADDR_SIZE);
7598       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7599         dw2_asm_output_data (2, 0, NULL);
7600     }
7601
7602   /* It is necessary not to output these entries if the sections were
7603      not used; if the sections were not used, the length will be 0 and
7604      the address may end up as 0 if the section is discarded by ld
7605      --gc-sections, leaving an invalid (0, 0) entry that can be
7606      confused with the terminator.  */
7607   if (text_section_used)
7608     {
7609       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7610       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7611                             text_section_label, "Length");
7612     }
7613   if (cold_text_section_used)
7614     {
7615       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7616                            "Address");
7617       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7618                             cold_text_section_label, "Length");
7619     }
7620
7621   for (i = 0; i < arange_table_in_use; i++)
7622     {
7623       dw_die_ref die = arange_table[i];
7624
7625       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7626       gcc_assert (die->die_mark);
7627
7628       if (die->die_tag == DW_TAG_subprogram)
7629         {
7630           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7631                                "Address");
7632           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7633                                 get_AT_low_pc (die), "Length");
7634         }
7635       else
7636         {
7637           /* A static variable; extract the symbol from DW_AT_location.
7638              Note that this code isn't currently hit, as we only emit
7639              aranges for functions (jason 9/23/99).  */
7640           dw_attr_ref a = get_AT (die, DW_AT_location);
7641           dw_loc_descr_ref loc;
7642
7643           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7644
7645           loc = AT_loc (a);
7646           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7647
7648           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7649                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7650           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7651                                get_AT_unsigned (die, DW_AT_byte_size),
7652                                "Length");
7653         }
7654     }
7655
7656   /* Output the terminator words.  */
7657   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7658   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7659 }
7660
7661 /* Add a new entry to .debug_ranges.  Return the offset at which it
7662    was placed.  */
7663
7664 static unsigned int
7665 add_ranges_num (int num)
7666 {
7667   unsigned int in_use = ranges_table_in_use;
7668
7669   if (in_use == ranges_table_allocated)
7670     {
7671       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7672       ranges_table
7673         = ggc_realloc (ranges_table, (ranges_table_allocated
7674                                       * sizeof (struct dw_ranges_struct)));
7675       memset (ranges_table + ranges_table_in_use, 0,
7676               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7677     }
7678
7679   ranges_table[in_use].num = num;
7680   ranges_table_in_use = in_use + 1;
7681
7682   return in_use * 2 * DWARF2_ADDR_SIZE;
7683 }
7684
7685 /* Add a new entry to .debug_ranges corresponding to a block, or a
7686    range terminator if BLOCK is NULL.  */
7687
7688 static unsigned int
7689 add_ranges (const_tree block)
7690 {
7691   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7692 }
7693
7694 /* Add a new entry to .debug_ranges corresponding to a pair of
7695    labels.  */
7696
7697 static unsigned int
7698 add_ranges_by_labels (const char *begin, const char *end)
7699 {
7700   unsigned int in_use = ranges_by_label_in_use;
7701
7702   if (in_use == ranges_by_label_allocated)
7703     {
7704       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7705       ranges_by_label
7706         = ggc_realloc (ranges_by_label,
7707                        (ranges_by_label_allocated
7708                         * sizeof (struct dw_ranges_by_label_struct)));
7709       memset (ranges_by_label + ranges_by_label_in_use, 0,
7710               RANGES_TABLE_INCREMENT
7711               * sizeof (struct dw_ranges_by_label_struct));
7712     }
7713
7714   ranges_by_label[in_use].begin = begin;
7715   ranges_by_label[in_use].end = end;
7716   ranges_by_label_in_use = in_use + 1;
7717
7718   return add_ranges_num (-(int)in_use - 1);
7719 }
7720
7721 static void
7722 output_ranges (void)
7723 {
7724   unsigned i;
7725   static const char *const start_fmt = "Offset 0x%x";
7726   const char *fmt = start_fmt;
7727
7728   for (i = 0; i < ranges_table_in_use; i++)
7729     {
7730       int block_num = ranges_table[i].num;
7731
7732       if (block_num > 0)
7733         {
7734           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7735           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7736
7737           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7738           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7739
7740           /* If all code is in the text section, then the compilation
7741              unit base address defaults to DW_AT_low_pc, which is the
7742              base of the text section.  */
7743           if (!have_multiple_function_sections)
7744             {
7745               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7746                                     text_section_label,
7747                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7748               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7749                                     text_section_label, NULL);
7750             }
7751
7752           /* Otherwise, the compilation unit base address is zero,
7753              which allows us to use absolute addresses, and not worry
7754              about whether the target supports cross-section
7755              arithmetic.  */
7756           else
7757             {
7758               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7759                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7760               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7761             }
7762
7763           fmt = NULL;
7764         }
7765
7766       /* Negative block_num stands for an index into ranges_by_label.  */
7767       else if (block_num < 0)
7768         {
7769           int lab_idx = - block_num - 1;
7770
7771           if (!have_multiple_function_sections)
7772             {
7773               gcc_unreachable ();
7774 #if 0
7775               /* If we ever use add_ranges_by_labels () for a single
7776                  function section, all we have to do is to take out
7777                  the #if 0 above.  */
7778               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7779                                     ranges_by_label[lab_idx].begin,
7780                                     text_section_label,
7781                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7782               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7783                                     ranges_by_label[lab_idx].end,
7784                                     text_section_label, NULL);
7785 #endif
7786             }
7787           else
7788             {
7789               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7790                                    ranges_by_label[lab_idx].begin,
7791                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7792               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7793                                    ranges_by_label[lab_idx].end,
7794                                    NULL);
7795             }
7796         }
7797       else
7798         {
7799           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7800           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7801           fmt = start_fmt;
7802         }
7803     }
7804 }
7805
7806 /* Data structure containing information about input files.  */
7807 struct file_info
7808 {
7809   const char *path;     /* Complete file name.  */
7810   const char *fname;    /* File name part.  */
7811   int length;           /* Length of entire string.  */
7812   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7813   int dir_idx;          /* Index in directory table.  */
7814 };
7815
7816 /* Data structure containing information about directories with source
7817    files.  */
7818 struct dir_info
7819 {
7820   const char *path;     /* Path including directory name.  */
7821   int length;           /* Path length.  */
7822   int prefix;           /* Index of directory entry which is a prefix.  */
7823   int count;            /* Number of files in this directory.  */
7824   int dir_idx;          /* Index of directory used as base.  */
7825 };
7826
7827 /* Callback function for file_info comparison.  We sort by looking at
7828    the directories in the path.  */
7829
7830 static int
7831 file_info_cmp (const void *p1, const void *p2)
7832 {
7833   const struct file_info *s1 = p1;
7834   const struct file_info *s2 = p2;
7835   const unsigned char *cp1;
7836   const unsigned char *cp2;
7837
7838   /* Take care of file names without directories.  We need to make sure that
7839      we return consistent values to qsort since some will get confused if
7840      we return the same value when identical operands are passed in opposite
7841      orders.  So if neither has a directory, return 0 and otherwise return
7842      1 or -1 depending on which one has the directory.  */
7843   if ((s1->path == s1->fname || s2->path == s2->fname))
7844     return (s2->path == s2->fname) - (s1->path == s1->fname);
7845
7846   cp1 = (const unsigned char *) s1->path;
7847   cp2 = (const unsigned char *) s2->path;
7848
7849   while (1)
7850     {
7851       ++cp1;
7852       ++cp2;
7853       /* Reached the end of the first path?  If so, handle like above.  */
7854       if ((cp1 == (const unsigned char *) s1->fname)
7855           || (cp2 == (const unsigned char *) s2->fname))
7856         return ((cp2 == (const unsigned char *) s2->fname)
7857                 - (cp1 == (const unsigned char *) s1->fname));
7858
7859       /* Character of current path component the same?  */
7860       else if (*cp1 != *cp2)
7861         return *cp1 - *cp2;
7862     }
7863 }
7864
7865 struct file_name_acquire_data
7866 {
7867   struct file_info *files;
7868   int used_files;
7869   int max_files;
7870 };
7871
7872 /* Traversal function for the hash table.  */
7873
7874 static int
7875 file_name_acquire (void ** slot, void *data)
7876 {
7877   struct file_name_acquire_data *fnad = data;
7878   struct dwarf_file_data *d = *slot;
7879   struct file_info *fi;
7880   const char *f;
7881
7882   gcc_assert (fnad->max_files >= d->emitted_number);
7883
7884   if (! d->emitted_number)
7885     return 1;
7886
7887   gcc_assert (fnad->max_files != fnad->used_files);
7888
7889   fi = fnad->files + fnad->used_files++;
7890
7891   /* Skip all leading "./".  */
7892   f = d->filename;
7893   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7894     f += 2;
7895
7896   /* Create a new array entry.  */
7897   fi->path = f;
7898   fi->length = strlen (f);
7899   fi->file_idx = d;
7900
7901   /* Search for the file name part.  */
7902   f = strrchr (f, DIR_SEPARATOR);
7903 #if defined (DIR_SEPARATOR_2)
7904   {
7905     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7906
7907     if (g != NULL)
7908       {
7909         if (f == NULL || f < g)
7910           f = g;
7911       }
7912   }
7913 #endif
7914
7915   fi->fname = f == NULL ? fi->path : f + 1;
7916   return 1;
7917 }
7918
7919 /* Output the directory table and the file name table.  We try to minimize
7920    the total amount of memory needed.  A heuristic is used to avoid large
7921    slowdowns with many input files.  */
7922
7923 static void
7924 output_file_names (void)
7925 {
7926   struct file_name_acquire_data fnad;
7927   int numfiles;
7928   struct file_info *files;
7929   struct dir_info *dirs;
7930   int *saved;
7931   int *savehere;
7932   int *backmap;
7933   int ndirs;
7934   int idx_offset;
7935   int i;
7936   int idx;
7937
7938   if (!last_emitted_file)
7939     {
7940       dw2_asm_output_data (1, 0, "End directory table");
7941       dw2_asm_output_data (1, 0, "End file name table");
7942       return;
7943     }
7944
7945   numfiles = last_emitted_file->emitted_number;
7946
7947   /* Allocate the various arrays we need.  */
7948   files = alloca (numfiles * sizeof (struct file_info));
7949   dirs = alloca (numfiles * sizeof (struct dir_info));
7950
7951   fnad.files = files;
7952   fnad.used_files = 0;
7953   fnad.max_files = numfiles;
7954   htab_traverse (file_table, file_name_acquire, &fnad);
7955   gcc_assert (fnad.used_files == fnad.max_files);
7956
7957   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7958
7959   /* Find all the different directories used.  */
7960   dirs[0].path = files[0].path;
7961   dirs[0].length = files[0].fname - files[0].path;
7962   dirs[0].prefix = -1;
7963   dirs[0].count = 1;
7964   dirs[0].dir_idx = 0;
7965   files[0].dir_idx = 0;
7966   ndirs = 1;
7967
7968   for (i = 1; i < numfiles; i++)
7969     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
7970         && memcmp (dirs[ndirs - 1].path, files[i].path,
7971                    dirs[ndirs - 1].length) == 0)
7972       {
7973         /* Same directory as last entry.  */
7974         files[i].dir_idx = ndirs - 1;
7975         ++dirs[ndirs - 1].count;
7976       }
7977     else
7978       {
7979         int j;
7980
7981         /* This is a new directory.  */
7982         dirs[ndirs].path = files[i].path;
7983         dirs[ndirs].length = files[i].fname - files[i].path;
7984         dirs[ndirs].count = 1;
7985         dirs[ndirs].dir_idx = ndirs;
7986         files[i].dir_idx = ndirs;
7987
7988         /* Search for a prefix.  */
7989         dirs[ndirs].prefix = -1;
7990         for (j = 0; j < ndirs; j++)
7991           if (dirs[j].length < dirs[ndirs].length
7992               && dirs[j].length > 1
7993               && (dirs[ndirs].prefix == -1
7994                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
7995               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
7996             dirs[ndirs].prefix = j;
7997
7998         ++ndirs;
7999       }
8000
8001   /* Now to the actual work.  We have to find a subset of the directories which
8002      allow expressing the file name using references to the directory table
8003      with the least amount of characters.  We do not do an exhaustive search
8004      where we would have to check out every combination of every single
8005      possible prefix.  Instead we use a heuristic which provides nearly optimal
8006      results in most cases and never is much off.  */
8007   saved = alloca (ndirs * sizeof (int));
8008   savehere = alloca (ndirs * sizeof (int));
8009
8010   memset (saved, '\0', ndirs * sizeof (saved[0]));
8011   for (i = 0; i < ndirs; i++)
8012     {
8013       int j;
8014       int total;
8015
8016       /* We can always save some space for the current directory.  But this
8017          does not mean it will be enough to justify adding the directory.  */
8018       savehere[i] = dirs[i].length;
8019       total = (savehere[i] - saved[i]) * dirs[i].count;
8020
8021       for (j = i + 1; j < ndirs; j++)
8022         {
8023           savehere[j] = 0;
8024           if (saved[j] < dirs[i].length)
8025             {
8026               /* Determine whether the dirs[i] path is a prefix of the
8027                  dirs[j] path.  */
8028               int k;
8029
8030               k = dirs[j].prefix;
8031               while (k != -1 && k != (int) i)
8032                 k = dirs[k].prefix;
8033
8034               if (k == (int) i)
8035                 {
8036                   /* Yes it is.  We can possibly save some memory by
8037                      writing the filenames in dirs[j] relative to
8038                      dirs[i].  */
8039                   savehere[j] = dirs[i].length;
8040                   total += (savehere[j] - saved[j]) * dirs[j].count;
8041                 }
8042             }
8043         }
8044
8045       /* Check whether we can save enough to justify adding the dirs[i]
8046          directory.  */
8047       if (total > dirs[i].length + 1)
8048         {
8049           /* It's worthwhile adding.  */
8050           for (j = i; j < ndirs; j++)
8051             if (savehere[j] > 0)
8052               {
8053                 /* Remember how much we saved for this directory so far.  */
8054                 saved[j] = savehere[j];
8055
8056                 /* Remember the prefix directory.  */
8057                 dirs[j].dir_idx = i;
8058               }
8059         }
8060     }
8061
8062   /* Emit the directory name table.  */
8063   idx = 1;
8064   idx_offset = dirs[0].length > 0 ? 1 : 0;
8065   for (i = 1 - idx_offset; i < ndirs; i++)
8066     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8067                             "Directory Entry: 0x%x", i + idx_offset);
8068
8069   dw2_asm_output_data (1, 0, "End directory table");
8070
8071   /* We have to emit them in the order of emitted_number since that's
8072      used in the debug info generation.  To do this efficiently we
8073      generate a back-mapping of the indices first.  */
8074   backmap = alloca (numfiles * sizeof (int));
8075   for (i = 0; i < numfiles; i++)
8076     backmap[files[i].file_idx->emitted_number - 1] = i;
8077
8078   /* Now write all the file names.  */
8079   for (i = 0; i < numfiles; i++)
8080     {
8081       int file_idx = backmap[i];
8082       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8083
8084       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8085                               "File Entry: 0x%x", (unsigned) i + 1);
8086
8087       /* Include directory index.  */
8088       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8089
8090       /* Modification time.  */
8091       dw2_asm_output_data_uleb128 (0, NULL);
8092
8093       /* File length in bytes.  */
8094       dw2_asm_output_data_uleb128 (0, NULL);
8095     }
8096
8097   dw2_asm_output_data (1, 0, "End file name table");
8098 }
8099
8100
8101 /* Output the source line number correspondence information.  This
8102    information goes into the .debug_line section.  */
8103
8104 static void
8105 output_line_info (void)
8106 {
8107   char l1[20], l2[20], p1[20], p2[20];
8108   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8109   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8110   unsigned opc;
8111   unsigned n_op_args;
8112   unsigned long lt_index;
8113   unsigned long current_line;
8114   long line_offset;
8115   long line_delta;
8116   unsigned long current_file;
8117   unsigned long function;
8118
8119   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8120   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8121   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8122   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8123
8124   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8125     dw2_asm_output_data (4, 0xffffffff,
8126       "Initial length escape value indicating 64-bit DWARF extension");
8127   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8128                         "Length of Source Line Info");
8129   ASM_OUTPUT_LABEL (asm_out_file, l1);
8130
8131   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8132   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8133   ASM_OUTPUT_LABEL (asm_out_file, p1);
8134
8135   /* Define the architecture-dependent minimum instruction length (in
8136    bytes).  In this implementation of DWARF, this field is used for
8137    information purposes only.  Since GCC generates assembly language,
8138    we have no a priori knowledge of how many instruction bytes are
8139    generated for each source line, and therefore can use only the
8140    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8141    commands.  Accordingly, we fix this as `1', which is "correct
8142    enough" for all architectures, and don't let the target override.  */
8143   dw2_asm_output_data (1, 1,
8144                        "Minimum Instruction Length");
8145
8146   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8147                        "Default is_stmt_start flag");
8148   dw2_asm_output_data (1, DWARF_LINE_BASE,
8149                        "Line Base Value (Special Opcodes)");
8150   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8151                        "Line Range Value (Special Opcodes)");
8152   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8153                        "Special Opcode Base");
8154
8155   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8156     {
8157       switch (opc)
8158         {
8159         case DW_LNS_advance_pc:
8160         case DW_LNS_advance_line:
8161         case DW_LNS_set_file:
8162         case DW_LNS_set_column:
8163         case DW_LNS_fixed_advance_pc:
8164           n_op_args = 1;
8165           break;
8166         default:
8167           n_op_args = 0;
8168           break;
8169         }
8170
8171       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8172                            opc, n_op_args);
8173     }
8174
8175   /* Write out the information about the files we use.  */
8176   output_file_names ();
8177   ASM_OUTPUT_LABEL (asm_out_file, p2);
8178
8179   /* We used to set the address register to the first location in the text
8180      section here, but that didn't accomplish anything since we already
8181      have a line note for the opening brace of the first function.  */
8182
8183   /* Generate the line number to PC correspondence table, encoded as
8184      a series of state machine operations.  */
8185   current_file = 1;
8186   current_line = 1;
8187
8188   if (cfun && in_cold_section_p)
8189     strcpy (prev_line_label, cfun->cold_section_label);
8190   else
8191     strcpy (prev_line_label, text_section_label);
8192   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8193     {
8194       dw_line_info_ref line_info = &line_info_table[lt_index];
8195
8196 #if 0
8197       /* Disable this optimization for now; GDB wants to see two line notes
8198          at the beginning of a function so it can find the end of the
8199          prologue.  */
8200
8201       /* Don't emit anything for redundant notes.  Just updating the
8202          address doesn't accomplish anything, because we already assume
8203          that anything after the last address is this line.  */
8204       if (line_info->dw_line_num == current_line
8205           && line_info->dw_file_num == current_file)
8206         continue;
8207 #endif
8208
8209       /* Emit debug info for the address of the current line.
8210
8211          Unfortunately, we have little choice here currently, and must always
8212          use the most general form.  GCC does not know the address delta
8213          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8214          attributes which will give an upper bound on the address range.  We
8215          could perhaps use length attributes to determine when it is safe to
8216          use DW_LNS_fixed_advance_pc.  */
8217
8218       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8219       if (0)
8220         {
8221           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8222           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8223                                "DW_LNS_fixed_advance_pc");
8224           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8225         }
8226       else
8227         {
8228           /* This can handle any delta.  This takes
8229              4+DWARF2_ADDR_SIZE bytes.  */
8230           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8231           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8232           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8233           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8234         }
8235
8236       strcpy (prev_line_label, line_label);
8237
8238       /* Emit debug info for the source file of the current line, if
8239          different from the previous line.  */
8240       if (line_info->dw_file_num != current_file)
8241         {
8242           current_file = line_info->dw_file_num;
8243           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8244           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8245         }
8246
8247       /* Emit debug info for the current line number, choosing the encoding
8248          that uses the least amount of space.  */
8249       if (line_info->dw_line_num != current_line)
8250         {
8251           line_offset = line_info->dw_line_num - current_line;
8252           line_delta = line_offset - DWARF_LINE_BASE;
8253           current_line = line_info->dw_line_num;
8254           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8255             /* This can handle deltas from -10 to 234, using the current
8256                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8257                takes 1 byte.  */
8258             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8259                                  "line %lu", current_line);
8260           else
8261             {
8262               /* This can handle any delta.  This takes at least 4 bytes,
8263                  depending on the value being encoded.  */
8264               dw2_asm_output_data (1, DW_LNS_advance_line,
8265                                    "advance to line %lu", current_line);
8266               dw2_asm_output_data_sleb128 (line_offset, NULL);
8267               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8268             }
8269         }
8270       else
8271         /* We still need to start a new row, so output a copy insn.  */
8272         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8273     }
8274
8275   /* Emit debug info for the address of the end of the function.  */
8276   if (0)
8277     {
8278       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8279                            "DW_LNS_fixed_advance_pc");
8280       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8281     }
8282   else
8283     {
8284       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8285       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8286       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8287       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8288     }
8289
8290   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8291   dw2_asm_output_data_uleb128 (1, NULL);
8292   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8293
8294   function = 0;
8295   current_file = 1;
8296   current_line = 1;
8297   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8298     {
8299       dw_separate_line_info_ref line_info
8300         = &separate_line_info_table[lt_index];
8301
8302 #if 0
8303       /* Don't emit anything for redundant notes.  */
8304       if (line_info->dw_line_num == current_line
8305           && line_info->dw_file_num == current_file
8306           && line_info->function == function)
8307         goto cont;
8308 #endif
8309
8310       /* Emit debug info for the address of the current line.  If this is
8311          a new function, or the first line of a function, then we need
8312          to handle it differently.  */
8313       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8314                                    lt_index);
8315       if (function != line_info->function)
8316         {
8317           function = line_info->function;
8318
8319           /* Set the address register to the first line in the function.  */
8320           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8321           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8322           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8323           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8324         }
8325       else
8326         {
8327           /* ??? See the DW_LNS_advance_pc comment above.  */
8328           if (0)
8329             {
8330               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8331                                    "DW_LNS_fixed_advance_pc");
8332               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8333             }
8334           else
8335             {
8336               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8337               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8338               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8339               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8340             }
8341         }
8342
8343       strcpy (prev_line_label, line_label);
8344
8345       /* Emit debug info for the source file of the current line, if
8346          different from the previous line.  */
8347       if (line_info->dw_file_num != current_file)
8348         {
8349           current_file = line_info->dw_file_num;
8350           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8351           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8352         }
8353
8354       /* Emit debug info for the current line number, choosing the encoding
8355          that uses the least amount of space.  */
8356       if (line_info->dw_line_num != current_line)
8357         {
8358           line_offset = line_info->dw_line_num - current_line;
8359           line_delta = line_offset - DWARF_LINE_BASE;
8360           current_line = line_info->dw_line_num;
8361           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8362             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8363                                  "line %lu", current_line);
8364           else
8365             {
8366               dw2_asm_output_data (1, DW_LNS_advance_line,
8367                                    "advance to line %lu", current_line);
8368               dw2_asm_output_data_sleb128 (line_offset, NULL);
8369               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8370             }
8371         }
8372       else
8373         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8374
8375 #if 0
8376     cont:
8377 #endif
8378
8379       lt_index++;
8380
8381       /* If we're done with a function, end its sequence.  */
8382       if (lt_index == separate_line_info_table_in_use
8383           || separate_line_info_table[lt_index].function != function)
8384         {
8385           current_file = 1;
8386           current_line = 1;
8387
8388           /* Emit debug info for the address of the end of the function.  */
8389           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8390           if (0)
8391             {
8392               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8393                                    "DW_LNS_fixed_advance_pc");
8394               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8395             }
8396           else
8397             {
8398               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8399               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8400               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8401               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8402             }
8403
8404           /* Output the marker for the end of this sequence.  */
8405           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8406           dw2_asm_output_data_uleb128 (1, NULL);
8407           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8408         }
8409     }
8410
8411   /* Output the marker for the end of the line number info.  */
8412   ASM_OUTPUT_LABEL (asm_out_file, l2);
8413 }
8414 \f
8415 /* Given a pointer to a tree node for some base type, return a pointer to
8416    a DIE that describes the given type.
8417
8418    This routine must only be called for GCC type nodes that correspond to
8419    Dwarf base (fundamental) types.  */
8420
8421 static dw_die_ref
8422 base_type_die (tree type)
8423 {
8424   dw_die_ref base_type_result;
8425   enum dwarf_type encoding;
8426
8427   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8428     return 0;
8429
8430   switch (TREE_CODE (type))
8431     {
8432     case INTEGER_TYPE:
8433       if (TYPE_STRING_FLAG (type))
8434         {
8435           if (TYPE_UNSIGNED (type))
8436             encoding = DW_ATE_unsigned_char;
8437           else
8438             encoding = DW_ATE_signed_char;
8439         }
8440       else if (TYPE_UNSIGNED (type))
8441         encoding = DW_ATE_unsigned;
8442       else
8443         encoding = DW_ATE_signed;
8444       break;
8445
8446     case REAL_TYPE:
8447       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8448         encoding = DW_ATE_decimal_float;
8449       else
8450         encoding = DW_ATE_float;
8451       break;
8452
8453     case FIXED_POINT_TYPE:
8454       if (TYPE_UNSIGNED (type))
8455         encoding = DW_ATE_unsigned_fixed;
8456       else
8457         encoding = DW_ATE_signed_fixed;
8458       break;
8459
8460       /* Dwarf2 doesn't know anything about complex ints, so use
8461          a user defined type for it.  */
8462     case COMPLEX_TYPE:
8463       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8464         encoding = DW_ATE_complex_float;
8465       else
8466         encoding = DW_ATE_lo_user;
8467       break;
8468
8469     case BOOLEAN_TYPE:
8470       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8471       encoding = DW_ATE_boolean;
8472       break;
8473
8474     default:
8475       /* No other TREE_CODEs are Dwarf fundamental types.  */
8476       gcc_unreachable ();
8477     }
8478
8479   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8480
8481   /* This probably indicates a bug.  */
8482   if (! TYPE_NAME (type))
8483     add_name_attribute (base_type_result, "__unknown__");
8484
8485   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8486                    int_size_in_bytes (type));
8487   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8488
8489   return base_type_result;
8490 }
8491
8492 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8493    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8494
8495 static inline int
8496 is_base_type (tree type)
8497 {
8498   switch (TREE_CODE (type))
8499     {
8500     case ERROR_MARK:
8501     case VOID_TYPE:
8502     case INTEGER_TYPE:
8503     case REAL_TYPE:
8504     case FIXED_POINT_TYPE:
8505     case COMPLEX_TYPE:
8506     case BOOLEAN_TYPE:
8507       return 1;
8508
8509     case ARRAY_TYPE:
8510     case RECORD_TYPE:
8511     case UNION_TYPE:
8512     case QUAL_UNION_TYPE:
8513     case ENUMERAL_TYPE:
8514     case FUNCTION_TYPE:
8515     case METHOD_TYPE:
8516     case POINTER_TYPE:
8517     case REFERENCE_TYPE:
8518     case OFFSET_TYPE:
8519     case LANG_TYPE:
8520     case VECTOR_TYPE:
8521       return 0;
8522
8523     default:
8524       gcc_unreachable ();
8525     }
8526
8527   return 0;
8528 }
8529
8530 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8531    node, return the size in bits for the type if it is a constant, or else
8532    return the alignment for the type if the type's size is not constant, or
8533    else return BITS_PER_WORD if the type actually turns out to be an
8534    ERROR_MARK node.  */
8535
8536 static inline unsigned HOST_WIDE_INT
8537 simple_type_size_in_bits (const_tree type)
8538 {
8539   if (TREE_CODE (type) == ERROR_MARK)
8540     return BITS_PER_WORD;
8541   else if (TYPE_SIZE (type) == NULL_TREE)
8542     return 0;
8543   else if (host_integerp (TYPE_SIZE (type), 1))
8544     return tree_low_cst (TYPE_SIZE (type), 1);
8545   else
8546     return TYPE_ALIGN (type);
8547 }
8548
8549 /* Return true if the debug information for the given type should be
8550    emitted as a subrange type.  */
8551
8552 static inline bool
8553 is_subrange_type (const_tree type)
8554 {
8555   tree subtype = TREE_TYPE (type);
8556
8557   /* Subrange types are identified by the fact that they are integer
8558      types, and that they have a subtype which is either an integer type
8559      or an enumeral type.  */
8560
8561   if (TREE_CODE (type) != INTEGER_TYPE
8562       || subtype == NULL_TREE)
8563     return false;
8564
8565   if (TREE_CODE (subtype) != INTEGER_TYPE
8566       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8567     return false;
8568
8569   if (TREE_CODE (type) == TREE_CODE (subtype)
8570       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8571       && TYPE_MIN_VALUE (type) != NULL
8572       && TYPE_MIN_VALUE (subtype) != NULL
8573       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8574       && TYPE_MAX_VALUE (type) != NULL
8575       && TYPE_MAX_VALUE (subtype) != NULL
8576       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8577     {
8578       /* The type and its subtype have the same representation.  If in
8579          addition the two types also have the same name, then the given
8580          type is not a subrange type, but rather a plain base type.  */
8581       /* FIXME: brobecker/2004-03-22:
8582          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8583          therefore be sufficient to check the TYPE_SIZE node pointers
8584          rather than checking the actual size.  Unfortunately, we have
8585          found some cases, such as in the Ada "integer" type, where
8586          this is not the case.  Until this problem is solved, we need to
8587          keep checking the actual size.  */
8588       tree type_name = TYPE_NAME (type);
8589       tree subtype_name = TYPE_NAME (subtype);
8590
8591       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8592         type_name = DECL_NAME (type_name);
8593
8594       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8595         subtype_name = DECL_NAME (subtype_name);
8596
8597       if (type_name == subtype_name)
8598         return false;
8599     }
8600
8601   return true;
8602 }
8603
8604 /*  Given a pointer to a tree node for a subrange type, return a pointer
8605     to a DIE that describes the given type.  */
8606
8607 static dw_die_ref
8608 subrange_type_die (tree type, dw_die_ref context_die)
8609 {
8610   dw_die_ref subrange_die;
8611   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8612
8613   if (context_die == NULL)
8614     context_die = comp_unit_die;
8615
8616   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8617
8618   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8619     {
8620       /* The size of the subrange type and its base type do not match,
8621          so we need to generate a size attribute for the subrange type.  */
8622       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8623     }
8624
8625   if (TYPE_MIN_VALUE (type) != NULL)
8626     add_bound_info (subrange_die, DW_AT_lower_bound,
8627                     TYPE_MIN_VALUE (type));
8628   if (TYPE_MAX_VALUE (type) != NULL)
8629     add_bound_info (subrange_die, DW_AT_upper_bound,
8630                     TYPE_MAX_VALUE (type));
8631
8632   return subrange_die;
8633 }
8634
8635 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8636    entry that chains various modifiers in front of the given type.  */
8637
8638 static dw_die_ref
8639 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8640                    dw_die_ref context_die)
8641 {
8642   enum tree_code code = TREE_CODE (type);
8643   dw_die_ref mod_type_die;
8644   dw_die_ref sub_die = NULL;
8645   tree item_type = NULL;
8646   tree qualified_type;
8647   tree name;
8648
8649   if (code == ERROR_MARK)
8650     return NULL;
8651
8652   /* See if we already have the appropriately qualified variant of
8653      this type.  */
8654   qualified_type
8655     = get_qualified_type (type,
8656                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8657                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8658
8659   /* If we do, then we can just use its DIE, if it exists.  */
8660   if (qualified_type)
8661     {
8662       mod_type_die = lookup_type_die (qualified_type);
8663       if (mod_type_die)
8664         return mod_type_die;
8665     }
8666
8667   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8668
8669   /* Handle C typedef types.  */
8670   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8671     {
8672       tree dtype = TREE_TYPE (name);
8673
8674       if (qualified_type == dtype)
8675         {
8676           /* For a named type, use the typedef.  */
8677           gen_type_die (qualified_type, context_die);
8678           return lookup_type_die (qualified_type);
8679         }
8680       else if (is_const_type < TYPE_READONLY (dtype)
8681                || is_volatile_type < TYPE_VOLATILE (dtype)
8682                || (is_const_type <= TYPE_READONLY (dtype)
8683                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8684                    && DECL_ORIGINAL_TYPE (name) != type))
8685         /* cv-unqualified version of named type.  Just use the unnamed
8686            type to which it refers.  */
8687         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8688                                   is_const_type, is_volatile_type,
8689                                   context_die);
8690       /* Else cv-qualified version of named type; fall through.  */
8691     }
8692
8693   if (is_const_type)
8694     {
8695       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8696       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8697     }
8698   else if (is_volatile_type)
8699     {
8700       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8701       sub_die = modified_type_die (type, 0, 0, context_die);
8702     }
8703   else if (code == POINTER_TYPE)
8704     {
8705       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8706       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8707                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8708       item_type = TREE_TYPE (type);
8709     }
8710   else if (code == REFERENCE_TYPE)
8711     {
8712       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8713       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8714                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8715       item_type = TREE_TYPE (type);
8716     }
8717   else if (is_subrange_type (type))
8718     {
8719       mod_type_die = subrange_type_die (type, context_die);
8720       item_type = TREE_TYPE (type);
8721     }
8722   else if (is_base_type (type))
8723     mod_type_die = base_type_die (type);
8724   else
8725     {
8726       gen_type_die (type, context_die);
8727
8728       /* We have to get the type_main_variant here (and pass that to the
8729          `lookup_type_die' routine) because the ..._TYPE node we have
8730          might simply be a *copy* of some original type node (where the
8731          copy was created to help us keep track of typedef names) and
8732          that copy might have a different TYPE_UID from the original
8733          ..._TYPE node.  */
8734       if (TREE_CODE (type) != VECTOR_TYPE)
8735         return lookup_type_die (type_main_variant (type));
8736       else
8737         /* Vectors have the debugging information in the type,
8738            not the main variant.  */
8739         return lookup_type_die (type);
8740     }
8741
8742   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8743      don't output a DW_TAG_typedef, since there isn't one in the
8744      user's program; just attach a DW_AT_name to the type.  */
8745   if (name
8746       && (TREE_CODE (name) != TYPE_DECL
8747           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8748     {
8749       if (TREE_CODE (name) == TYPE_DECL)
8750         /* Could just call add_name_and_src_coords_attributes here,
8751            but since this is a builtin type it doesn't have any
8752            useful source coordinates anyway.  */
8753         name = DECL_NAME (name);
8754       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8755     }
8756
8757   if (qualified_type)
8758     equate_type_number_to_die (qualified_type, mod_type_die);
8759
8760   if (item_type)
8761     /* We must do this after the equate_type_number_to_die call, in case
8762        this is a recursive type.  This ensures that the modified_type_die
8763        recursion will terminate even if the type is recursive.  Recursive
8764        types are possible in Ada.  */
8765     sub_die = modified_type_die (item_type,
8766                                  TYPE_READONLY (item_type),
8767                                  TYPE_VOLATILE (item_type),
8768                                  context_die);
8769
8770   if (sub_die != NULL)
8771     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8772
8773   return mod_type_die;
8774 }
8775
8776 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8777    an enumerated type.  */
8778
8779 static inline int
8780 type_is_enum (const_tree type)
8781 {
8782   return TREE_CODE (type) == ENUMERAL_TYPE;
8783 }
8784
8785 /* Return the DBX register number described by a given RTL node.  */
8786
8787 static unsigned int
8788 dbx_reg_number (const_rtx rtl)
8789 {
8790   unsigned regno = REGNO (rtl);
8791
8792   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8793
8794 #ifdef LEAF_REG_REMAP
8795   if (current_function_uses_only_leaf_regs)
8796     {
8797       int leaf_reg = LEAF_REG_REMAP (regno);
8798       if (leaf_reg != -1)
8799         regno = (unsigned) leaf_reg;
8800     }
8801 #endif
8802
8803   return DBX_REGISTER_NUMBER (regno);
8804 }
8805
8806 /* Optionally add a DW_OP_piece term to a location description expression.
8807    DW_OP_piece is only added if the location description expression already
8808    doesn't end with DW_OP_piece.  */
8809
8810 static void
8811 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8812 {
8813   dw_loc_descr_ref loc;
8814
8815   if (*list_head != NULL)
8816     {
8817       /* Find the end of the chain.  */
8818       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8819         ;
8820
8821       if (loc->dw_loc_opc != DW_OP_piece)
8822         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8823     }
8824 }
8825
8826 /* Return a location descriptor that designates a machine register or
8827    zero if there is none.  */
8828
8829 static dw_loc_descr_ref
8830 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8831 {
8832   rtx regs;
8833
8834   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8835     return 0;
8836
8837   regs = targetm.dwarf_register_span (rtl);
8838
8839   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8840     return multiple_reg_loc_descriptor (rtl, regs, initialized);
8841   else
8842     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8843 }
8844
8845 /* Return a location descriptor that designates a machine register for
8846    a given hard register number.  */
8847
8848 static dw_loc_descr_ref
8849 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8850 {
8851   dw_loc_descr_ref reg_loc_descr;
8852   if (regno <= 31)
8853     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8854   else
8855     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8856
8857   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8858     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8859
8860   return reg_loc_descr;
8861 }
8862
8863 /* Given an RTL of a register, return a location descriptor that
8864    designates a value that spans more than one register.  */
8865
8866 static dw_loc_descr_ref
8867 multiple_reg_loc_descriptor (rtx rtl, rtx regs, 
8868                              enum var_init_status initialized)
8869 {
8870   int nregs, size, i;
8871   unsigned reg;
8872   dw_loc_descr_ref loc_result = NULL;
8873
8874   reg = REGNO (rtl);
8875 #ifdef LEAF_REG_REMAP
8876   if (current_function_uses_only_leaf_regs)
8877     {
8878       int leaf_reg = LEAF_REG_REMAP (reg);
8879       if (leaf_reg != -1)
8880         reg = (unsigned) leaf_reg;
8881     }
8882 #endif
8883   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8884   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8885
8886   /* Simple, contiguous registers.  */
8887   if (regs == NULL_RTX)
8888     {
8889       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8890
8891       loc_result = NULL;
8892       while (nregs--)
8893         {
8894           dw_loc_descr_ref t;
8895
8896           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8897                                       VAR_INIT_STATUS_INITIALIZED);
8898           add_loc_descr (&loc_result, t);
8899           add_loc_descr_op_piece (&loc_result, size);
8900           ++reg;
8901         }
8902       return loc_result;
8903     }
8904
8905   /* Now onto stupid register sets in non contiguous locations.  */
8906
8907   gcc_assert (GET_CODE (regs) == PARALLEL);
8908
8909   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8910   loc_result = NULL;
8911
8912   for (i = 0; i < XVECLEN (regs, 0); ++i)
8913     {
8914       dw_loc_descr_ref t;
8915
8916       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8917                                   VAR_INIT_STATUS_INITIALIZED);
8918       add_loc_descr (&loc_result, t);
8919       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8920       add_loc_descr_op_piece (&loc_result, size);
8921     }
8922
8923   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8924     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8925   return loc_result;
8926 }
8927
8928 /* Return a location descriptor that designates a constant.  */
8929
8930 static dw_loc_descr_ref
8931 int_loc_descriptor (HOST_WIDE_INT i)
8932 {
8933   enum dwarf_location_atom op;
8934
8935   /* Pick the smallest representation of a constant, rather than just
8936      defaulting to the LEB encoding.  */
8937   if (i >= 0)
8938     {
8939       if (i <= 31)
8940         op = DW_OP_lit0 + i;
8941       else if (i <= 0xff)
8942         op = DW_OP_const1u;
8943       else if (i <= 0xffff)
8944         op = DW_OP_const2u;
8945       else if (HOST_BITS_PER_WIDE_INT == 32
8946                || i <= 0xffffffff)
8947         op = DW_OP_const4u;
8948       else
8949         op = DW_OP_constu;
8950     }
8951   else
8952     {
8953       if (i >= -0x80)
8954         op = DW_OP_const1s;
8955       else if (i >= -0x8000)
8956         op = DW_OP_const2s;
8957       else if (HOST_BITS_PER_WIDE_INT == 32
8958                || i >= -0x80000000)
8959         op = DW_OP_const4s;
8960       else
8961         op = DW_OP_consts;
8962     }
8963
8964   return new_loc_descr (op, i, 0);
8965 }
8966
8967 /* Return a location descriptor that designates a base+offset location.  */
8968
8969 static dw_loc_descr_ref
8970 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
8971                  enum var_init_status initialized)
8972 {
8973   unsigned int regno;
8974   dw_loc_descr_ref result;
8975
8976   /* We only use "frame base" when we're sure we're talking about the
8977      post-prologue local stack frame.  We do this by *not* running
8978      register elimination until this point, and recognizing the special
8979      argument pointer and soft frame pointer rtx's.  */
8980   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
8981     {
8982       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
8983
8984       if (elim != reg)
8985         {
8986           if (GET_CODE (elim) == PLUS)
8987             {
8988               offset += INTVAL (XEXP (elim, 1));
8989               elim = XEXP (elim, 0);
8990             }
8991           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
8992                       : stack_pointer_rtx));
8993           offset += frame_pointer_fb_offset;
8994
8995           return new_loc_descr (DW_OP_fbreg, offset, 0);
8996         }
8997     }
8998
8999   regno = dbx_reg_number (reg);
9000   if (regno <= 31)
9001     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9002   else
9003     result = new_loc_descr (DW_OP_bregx, regno, offset);
9004
9005   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9006     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9007
9008   return result;
9009 }
9010
9011 /* Return true if this RTL expression describes a base+offset calculation.  */
9012
9013 static inline int
9014 is_based_loc (const_rtx rtl)
9015 {
9016   return (GET_CODE (rtl) == PLUS
9017           && ((REG_P (XEXP (rtl, 0))
9018                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9019                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9020 }
9021
9022 /* Return a descriptor that describes the concatenation of N locations
9023    used to form the address of a memory location.  */
9024
9025 static dw_loc_descr_ref
9026 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9027                             enum var_init_status initialized)
9028 {
9029   unsigned int i;
9030   dw_loc_descr_ref cc_loc_result = NULL;
9031   unsigned int n = XVECLEN (concatn, 0);
9032
9033   for (i = 0; i < n; ++i)
9034     {
9035       dw_loc_descr_ref ref;
9036       rtx x = XVECEXP (concatn, 0, i);
9037
9038       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9039       if (ref == NULL)
9040         return NULL;
9041
9042       add_loc_descr (&cc_loc_result, ref);
9043       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9044     }
9045
9046   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9047     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9048
9049   return cc_loc_result;
9050 }
9051
9052 /* The following routine converts the RTL for a variable or parameter
9053    (resident in memory) into an equivalent Dwarf representation of a
9054    mechanism for getting the address of that same variable onto the top of a
9055    hypothetical "address evaluation" stack.
9056
9057    When creating memory location descriptors, we are effectively transforming
9058    the RTL for a memory-resident object into its Dwarf postfix expression
9059    equivalent.  This routine recursively descends an RTL tree, turning
9060    it into Dwarf postfix code as it goes.
9061
9062    MODE is the mode of the memory reference, needed to handle some
9063    autoincrement addressing modes.
9064
9065    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9066    location list for RTL.
9067
9068    Return 0 if we can't represent the location.  */
9069
9070 static dw_loc_descr_ref
9071 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9072                     enum var_init_status initialized)
9073 {
9074   dw_loc_descr_ref mem_loc_result = NULL;
9075   enum dwarf_location_atom op;
9076
9077   /* Note that for a dynamically sized array, the location we will generate a
9078      description of here will be the lowest numbered location which is
9079      actually within the array.  That's *not* necessarily the same as the
9080      zeroth element of the array.  */
9081
9082   rtl = targetm.delegitimize_address (rtl);
9083
9084   switch (GET_CODE (rtl))
9085     {
9086     case POST_INC:
9087     case POST_DEC:
9088     case POST_MODIFY:
9089       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9090          just fall into the SUBREG code.  */
9091
9092       /* ... fall through ...  */
9093
9094     case SUBREG:
9095       /* The case of a subreg may arise when we have a local (register)
9096          variable or a formal (register) parameter which doesn't quite fill
9097          up an entire register.  For now, just assume that it is
9098          legitimate to make the Dwarf info refer to the whole register which
9099          contains the given subreg.  */
9100       rtl = XEXP (rtl, 0);
9101
9102       /* ... fall through ...  */
9103
9104     case REG:
9105       /* Whenever a register number forms a part of the description of the
9106          method for calculating the (dynamic) address of a memory resident
9107          object, DWARF rules require the register number be referred to as
9108          a "base register".  This distinction is not based in any way upon
9109          what category of register the hardware believes the given register
9110          belongs to.  This is strictly DWARF terminology we're dealing with
9111          here. Note that in cases where the location of a memory-resident
9112          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9113          OP_CONST (0)) the actual DWARF location descriptor that we generate
9114          may just be OP_BASEREG (basereg).  This may look deceptively like
9115          the object in question was allocated to a register (rather than in
9116          memory) so DWARF consumers need to be aware of the subtle
9117          distinction between OP_REG and OP_BASEREG.  */
9118       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9119         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9120       break;
9121
9122     case MEM:
9123       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9124                                            VAR_INIT_STATUS_INITIALIZED);
9125       if (mem_loc_result != 0)
9126         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9127       break;
9128
9129     case LO_SUM:
9130          rtl = XEXP (rtl, 1);
9131
9132       /* ... fall through ...  */
9133
9134     case LABEL_REF:
9135       /* Some ports can transform a symbol ref into a label ref, because
9136          the symbol ref is too far away and has to be dumped into a constant
9137          pool.  */
9138     case CONST:
9139     case SYMBOL_REF:
9140       /* Alternatively, the symbol in the constant pool might be referenced
9141          by a different symbol.  */
9142       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9143         {
9144           bool marked;
9145           rtx tmp = get_pool_constant_mark (rtl, &marked);
9146
9147           if (GET_CODE (tmp) == SYMBOL_REF)
9148             {
9149               rtl = tmp;
9150               if (CONSTANT_POOL_ADDRESS_P (tmp))
9151                 get_pool_constant_mark (tmp, &marked);
9152               else
9153                 marked = true;
9154             }
9155
9156           /* If all references to this pool constant were optimized away,
9157              it was not output and thus we can't represent it.
9158              FIXME: might try to use DW_OP_const_value here, though
9159              DW_OP_piece complicates it.  */
9160           if (!marked)
9161             return 0;
9162         }
9163
9164       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9165       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9166       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9167       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9168       break;
9169
9170     case PRE_MODIFY:
9171       /* Extract the PLUS expression nested inside and fall into
9172          PLUS code below.  */
9173       rtl = XEXP (rtl, 1);
9174       goto plus;
9175
9176     case PRE_INC:
9177     case PRE_DEC:
9178       /* Turn these into a PLUS expression and fall into the PLUS code
9179          below.  */
9180       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9181                           GEN_INT (GET_CODE (rtl) == PRE_INC
9182                                    ? GET_MODE_UNIT_SIZE (mode)
9183                                    : -GET_MODE_UNIT_SIZE (mode)));
9184
9185       /* ... fall through ...  */
9186
9187     case PLUS:
9188     plus:
9189       if (is_based_loc (rtl))
9190         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9191                                           INTVAL (XEXP (rtl, 1)),
9192                                           VAR_INIT_STATUS_INITIALIZED);
9193       else
9194         {
9195           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9196                                                VAR_INIT_STATUS_INITIALIZED);
9197           if (mem_loc_result == 0)
9198             break;
9199
9200           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9201               && INTVAL (XEXP (rtl, 1)) >= 0)
9202             add_loc_descr (&mem_loc_result,
9203                            new_loc_descr (DW_OP_plus_uconst,
9204                                           INTVAL (XEXP (rtl, 1)), 0));
9205           else
9206             {
9207               add_loc_descr (&mem_loc_result,
9208                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9209                                                  VAR_INIT_STATUS_INITIALIZED));
9210               add_loc_descr (&mem_loc_result,
9211                              new_loc_descr (DW_OP_plus, 0, 0));
9212             }
9213         }
9214       break;
9215
9216     /* If a pseudo-reg is optimized away, it is possible for it to
9217        be replaced with a MEM containing a multiply or shift.  */
9218     case MULT:
9219       op = DW_OP_mul;
9220       goto do_binop;
9221
9222     case ASHIFT:
9223       op = DW_OP_shl;
9224       goto do_binop;
9225
9226     case ASHIFTRT:
9227       op = DW_OP_shra;
9228       goto do_binop;
9229
9230     case LSHIFTRT:
9231       op = DW_OP_shr;
9232       goto do_binop;
9233
9234     do_binop:
9235       {
9236         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9237                                                    VAR_INIT_STATUS_INITIALIZED);
9238         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9239                                                    VAR_INIT_STATUS_INITIALIZED);
9240
9241         if (op0 == 0 || op1 == 0)
9242           break;
9243
9244         mem_loc_result = op0;
9245         add_loc_descr (&mem_loc_result, op1);
9246         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9247         break;
9248       }
9249
9250     case CONST_INT:
9251       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9252       break;
9253
9254     case CONCATN:
9255       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode, 
9256                                                    VAR_INIT_STATUS_INITIALIZED);
9257       break;
9258
9259     default:
9260       gcc_unreachable ();
9261     }
9262
9263   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9264     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9265
9266   return mem_loc_result;
9267 }
9268
9269 /* Return a descriptor that describes the concatenation of two locations.
9270    This is typically a complex variable.  */
9271
9272 static dw_loc_descr_ref
9273 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9274 {
9275   dw_loc_descr_ref cc_loc_result = NULL;
9276   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9277   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9278
9279   if (x0_ref == 0 || x1_ref == 0)
9280     return 0;
9281
9282   cc_loc_result = x0_ref;
9283   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9284
9285   add_loc_descr (&cc_loc_result, x1_ref);
9286   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9287
9288   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9289     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9290
9291   return cc_loc_result;
9292 }
9293
9294 /* Return a descriptor that describes the concatenation of N
9295    locations.  */
9296
9297 static dw_loc_descr_ref
9298 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9299 {
9300   unsigned int i;
9301   dw_loc_descr_ref cc_loc_result = NULL;
9302   unsigned int n = XVECLEN (concatn, 0);
9303
9304   for (i = 0; i < n; ++i)
9305     {
9306       dw_loc_descr_ref ref;
9307       rtx x = XVECEXP (concatn, 0, i);
9308
9309       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9310       if (ref == NULL)
9311         return NULL;
9312
9313       add_loc_descr (&cc_loc_result, ref);
9314       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9315     }
9316
9317   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9318     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9319
9320   return cc_loc_result;
9321 }
9322
9323 /* Output a proper Dwarf location descriptor for a variable or parameter
9324    which is either allocated in a register or in a memory location.  For a
9325    register, we just generate an OP_REG and the register number.  For a
9326    memory location we provide a Dwarf postfix expression describing how to
9327    generate the (dynamic) address of the object onto the address stack.
9328
9329    If we don't know how to describe it, return 0.  */
9330
9331 static dw_loc_descr_ref
9332 loc_descriptor (rtx rtl, enum var_init_status initialized)
9333 {
9334   dw_loc_descr_ref loc_result = NULL;
9335
9336   switch (GET_CODE (rtl))
9337     {
9338     case SUBREG:
9339       /* The case of a subreg may arise when we have a local (register)
9340          variable or a formal (register) parameter which doesn't quite fill
9341          up an entire register.  For now, just assume that it is
9342          legitimate to make the Dwarf info refer to the whole register which
9343          contains the given subreg.  */
9344       rtl = SUBREG_REG (rtl);
9345
9346       /* ... fall through ...  */
9347
9348     case REG:
9349       loc_result = reg_loc_descriptor (rtl, initialized);
9350       break;
9351
9352     case MEM:
9353       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9354                                        initialized);
9355       break;
9356
9357     case CONCAT:
9358       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9359                                           initialized);
9360       break;
9361
9362     case CONCATN:
9363       loc_result = concatn_loc_descriptor (rtl, initialized);
9364       break;
9365
9366     case VAR_LOCATION:
9367       /* Single part.  */
9368       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9369         {
9370           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9371           break;
9372         }
9373
9374       rtl = XEXP (rtl, 1);
9375       /* FALLTHRU */
9376
9377     case PARALLEL:
9378       {
9379         rtvec par_elems = XVEC (rtl, 0);
9380         int num_elem = GET_NUM_ELEM (par_elems);
9381         enum machine_mode mode;
9382         int i;
9383
9384         /* Create the first one, so we have something to add to.  */
9385         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9386                                      initialized);
9387         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9388         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9389         for (i = 1; i < num_elem; i++)
9390           {
9391             dw_loc_descr_ref temp;
9392
9393             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9394                                    initialized);
9395             add_loc_descr (&loc_result, temp);
9396             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9397             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9398           }
9399       }
9400       break;
9401
9402     default:
9403       gcc_unreachable ();
9404     }
9405
9406   return loc_result;
9407 }
9408
9409 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9410    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9411    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9412    top-level invocation, and we require the address of LOC; is 0 if we require
9413    the value of LOC.  */
9414
9415 static dw_loc_descr_ref
9416 loc_descriptor_from_tree_1 (tree loc, int want_address)
9417 {
9418   dw_loc_descr_ref ret, ret1;
9419   int have_address = 0;
9420   enum dwarf_location_atom op;
9421
9422   /* ??? Most of the time we do not take proper care for sign/zero
9423      extending the values properly.  Hopefully this won't be a real
9424      problem...  */
9425
9426   switch (TREE_CODE (loc))
9427     {
9428     case ERROR_MARK:
9429       return 0;
9430
9431     case PLACEHOLDER_EXPR:
9432       /* This case involves extracting fields from an object to determine the
9433          position of other fields.  We don't try to encode this here.  The
9434          only user of this is Ada, which encodes the needed information using
9435          the names of types.  */
9436       return 0;
9437
9438     case CALL_EXPR:
9439       return 0;
9440
9441     case PREINCREMENT_EXPR:
9442     case PREDECREMENT_EXPR:
9443     case POSTINCREMENT_EXPR:
9444     case POSTDECREMENT_EXPR:
9445       /* There are no opcodes for these operations.  */
9446       return 0;
9447
9448     case ADDR_EXPR:
9449       /* If we already want an address, there's nothing we can do.  */
9450       if (want_address)
9451         return 0;
9452
9453       /* Otherwise, process the argument and look for the address.  */
9454       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9455
9456     case VAR_DECL:
9457       if (DECL_THREAD_LOCAL_P (loc))
9458         {
9459           rtx rtl;
9460
9461           /* If this is not defined, we have no way to emit the data.  */
9462           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9463             return 0;
9464
9465           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9466              look up addresses of objects in the current module.  */
9467           if (DECL_EXTERNAL (loc))
9468             return 0;
9469
9470           rtl = rtl_for_decl_location (loc);
9471           if (rtl == NULL_RTX)
9472             return 0;
9473
9474           if (!MEM_P (rtl))
9475             return 0;
9476           rtl = XEXP (rtl, 0);
9477           if (! CONSTANT_P (rtl))
9478             return 0;
9479
9480           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9481           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9482           ret->dw_loc_oprnd1.v.val_addr = rtl;
9483
9484           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9485           add_loc_descr (&ret, ret1);
9486
9487           have_address = 1;
9488           break;
9489         }
9490       /* FALLTHRU */
9491
9492     case PARM_DECL:
9493       if (DECL_HAS_VALUE_EXPR_P (loc))
9494         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9495                                            want_address);
9496       /* FALLTHRU */
9497
9498     case RESULT_DECL:
9499     case FUNCTION_DECL:
9500       {
9501         rtx rtl = rtl_for_decl_location (loc);
9502
9503         if (rtl == NULL_RTX)
9504           return 0;
9505         else if (GET_CODE (rtl) == CONST_INT)
9506           {
9507             HOST_WIDE_INT val = INTVAL (rtl);
9508             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9509               val &= GET_MODE_MASK (DECL_MODE (loc));
9510             ret = int_loc_descriptor (val);
9511           }
9512         else if (GET_CODE (rtl) == CONST_STRING)
9513           return 0;
9514         else if (CONSTANT_P (rtl))
9515           {
9516             ret = new_loc_descr (DW_OP_addr, 0, 0);
9517             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9518             ret->dw_loc_oprnd1.v.val_addr = rtl;
9519           }
9520         else
9521           {
9522             enum machine_mode mode;
9523
9524             /* Certain constructs can only be represented at top-level.  */
9525             if (want_address == 2)
9526               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9527
9528             mode = GET_MODE (rtl);
9529             if (MEM_P (rtl))
9530               {
9531                 rtl = XEXP (rtl, 0);
9532                 have_address = 1;
9533               }
9534             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9535           }
9536       }
9537       break;
9538
9539     case INDIRECT_REF:
9540       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9541       have_address = 1;
9542       break;
9543
9544     case COMPOUND_EXPR:
9545       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9546
9547     case NOP_EXPR:
9548     case CONVERT_EXPR:
9549     case NON_LVALUE_EXPR:
9550     case VIEW_CONVERT_EXPR:
9551     case SAVE_EXPR:
9552     case GIMPLE_MODIFY_STMT:
9553       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9554                                          want_address);
9555
9556     case COMPONENT_REF:
9557     case BIT_FIELD_REF:
9558     case ARRAY_REF:
9559     case ARRAY_RANGE_REF:
9560       {
9561         tree obj, offset;
9562         HOST_WIDE_INT bitsize, bitpos, bytepos;
9563         enum machine_mode mode;
9564         int volatilep;
9565         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9566
9567         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9568                                    &unsignedp, &volatilep, false);
9569
9570         if (obj == loc)
9571           return 0;
9572
9573         ret = loc_descriptor_from_tree_1 (obj, 1);
9574         if (ret == 0
9575             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9576           return 0;
9577
9578         if (offset != NULL_TREE)
9579           {
9580             /* Variable offset.  */
9581             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9582             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9583           }
9584
9585         bytepos = bitpos / BITS_PER_UNIT;
9586         if (bytepos > 0)
9587           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9588         else if (bytepos < 0)
9589           {
9590             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9591             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9592           }
9593
9594         have_address = 1;
9595         break;
9596       }
9597
9598     case INTEGER_CST:
9599       if (host_integerp (loc, 0))
9600         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9601       else
9602         return 0;
9603       break;
9604
9605     case CONSTRUCTOR:
9606       {
9607         /* Get an RTL for this, if something has been emitted.  */
9608         rtx rtl = lookup_constant_def (loc);
9609         enum machine_mode mode;
9610
9611         if (!rtl || !MEM_P (rtl))
9612           return 0;
9613         mode = GET_MODE (rtl);
9614         rtl = XEXP (rtl, 0);
9615         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9616         have_address = 1;
9617         break;
9618       }
9619
9620     case TRUTH_AND_EXPR:
9621     case TRUTH_ANDIF_EXPR:
9622     case BIT_AND_EXPR:
9623       op = DW_OP_and;
9624       goto do_binop;
9625
9626     case TRUTH_XOR_EXPR:
9627     case BIT_XOR_EXPR:
9628       op = DW_OP_xor;
9629       goto do_binop;
9630
9631     case TRUTH_OR_EXPR:
9632     case TRUTH_ORIF_EXPR:
9633     case BIT_IOR_EXPR:
9634       op = DW_OP_or;
9635       goto do_binop;
9636
9637     case FLOOR_DIV_EXPR:
9638     case CEIL_DIV_EXPR:
9639     case ROUND_DIV_EXPR:
9640     case TRUNC_DIV_EXPR:
9641       op = DW_OP_div;
9642       goto do_binop;
9643
9644     case MINUS_EXPR:
9645       op = DW_OP_minus;
9646       goto do_binop;
9647
9648     case FLOOR_MOD_EXPR:
9649     case CEIL_MOD_EXPR:
9650     case ROUND_MOD_EXPR:
9651     case TRUNC_MOD_EXPR:
9652       op = DW_OP_mod;
9653       goto do_binop;
9654
9655     case MULT_EXPR:
9656       op = DW_OP_mul;
9657       goto do_binop;
9658
9659     case LSHIFT_EXPR:
9660       op = DW_OP_shl;
9661       goto do_binop;
9662
9663     case RSHIFT_EXPR:
9664       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9665       goto do_binop;
9666
9667     case POINTER_PLUS_EXPR:
9668     case PLUS_EXPR:
9669       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9670           && host_integerp (TREE_OPERAND (loc, 1), 0))
9671         {
9672           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9673           if (ret == 0)
9674             return 0;
9675
9676           add_loc_descr (&ret,
9677                          new_loc_descr (DW_OP_plus_uconst,
9678                                         tree_low_cst (TREE_OPERAND (loc, 1),
9679                                                       0),
9680                                         0));
9681           break;
9682         }
9683
9684       op = DW_OP_plus;
9685       goto do_binop;
9686
9687     case LE_EXPR:
9688       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9689         return 0;
9690
9691       op = DW_OP_le;
9692       goto do_binop;
9693
9694     case GE_EXPR:
9695       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9696         return 0;
9697
9698       op = DW_OP_ge;
9699       goto do_binop;
9700
9701     case LT_EXPR:
9702       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9703         return 0;
9704
9705       op = DW_OP_lt;
9706       goto do_binop;
9707
9708     case GT_EXPR:
9709       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9710         return 0;
9711
9712       op = DW_OP_gt;
9713       goto do_binop;
9714
9715     case EQ_EXPR:
9716       op = DW_OP_eq;
9717       goto do_binop;
9718
9719     case NE_EXPR:
9720       op = DW_OP_ne;
9721       goto do_binop;
9722
9723     do_binop:
9724       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9725       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9726       if (ret == 0 || ret1 == 0)
9727         return 0;
9728
9729       add_loc_descr (&ret, ret1);
9730       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9731       break;
9732
9733     case TRUTH_NOT_EXPR:
9734     case BIT_NOT_EXPR:
9735       op = DW_OP_not;
9736       goto do_unop;
9737
9738     case ABS_EXPR:
9739       op = DW_OP_abs;
9740       goto do_unop;
9741
9742     case NEGATE_EXPR:
9743       op = DW_OP_neg;
9744       goto do_unop;
9745
9746     do_unop:
9747       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9748       if (ret == 0)
9749         return 0;
9750
9751       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9752       break;
9753
9754     case MIN_EXPR:
9755     case MAX_EXPR:
9756       {
9757         const enum tree_code code =
9758           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9759
9760         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9761                       build2 (code, integer_type_node,
9762                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9763                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9764       }
9765
9766       /* ... fall through ...  */
9767
9768     case COND_EXPR:
9769       {
9770         dw_loc_descr_ref lhs
9771           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9772         dw_loc_descr_ref rhs
9773           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9774         dw_loc_descr_ref bra_node, jump_node, tmp;
9775
9776         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9777         if (ret == 0 || lhs == 0 || rhs == 0)
9778           return 0;
9779
9780         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9781         add_loc_descr (&ret, bra_node);
9782
9783         add_loc_descr (&ret, rhs);
9784         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9785         add_loc_descr (&ret, jump_node);
9786
9787         add_loc_descr (&ret, lhs);
9788         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9789         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9790
9791         /* ??? Need a node to point the skip at.  Use a nop.  */
9792         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9793         add_loc_descr (&ret, tmp);
9794         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9795         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9796       }
9797       break;
9798
9799     case FIX_TRUNC_EXPR:
9800       return 0;
9801
9802     default:
9803       /* Leave front-end specific codes as simply unknown.  This comes
9804          up, for instance, with the C STMT_EXPR.  */
9805       if ((unsigned int) TREE_CODE (loc)
9806           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9807         return 0;
9808
9809 #ifdef ENABLE_CHECKING
9810       /* Otherwise this is a generic code; we should just lists all of
9811          these explicitly.  We forgot one.  */
9812       gcc_unreachable ();
9813 #else
9814       /* In a release build, we want to degrade gracefully: better to
9815          generate incomplete debugging information than to crash.  */
9816       return NULL;
9817 #endif
9818     }
9819
9820   /* Show if we can't fill the request for an address.  */
9821   if (want_address && !have_address)
9822     return 0;
9823
9824   /* If we've got an address and don't want one, dereference.  */
9825   if (!want_address && have_address && ret)
9826     {
9827       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9828
9829       if (size > DWARF2_ADDR_SIZE || size == -1)
9830         return 0;
9831       else if (size == DWARF2_ADDR_SIZE)
9832         op = DW_OP_deref;
9833       else
9834         op = DW_OP_deref_size;
9835
9836       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9837     }
9838
9839   return ret;
9840 }
9841
9842 static inline dw_loc_descr_ref
9843 loc_descriptor_from_tree (tree loc)
9844 {
9845   return loc_descriptor_from_tree_1 (loc, 2);
9846 }
9847
9848 /* Given a value, round it up to the lowest multiple of `boundary'
9849    which is not less than the value itself.  */
9850
9851 static inline HOST_WIDE_INT
9852 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9853 {
9854   return (((value + boundary - 1) / boundary) * boundary);
9855 }
9856
9857 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9858    pointer to the declared type for the relevant field variable, or return
9859    `integer_type_node' if the given node turns out to be an
9860    ERROR_MARK node.  */
9861
9862 static inline tree
9863 field_type (const_tree decl)
9864 {
9865   tree type;
9866
9867   if (TREE_CODE (decl) == ERROR_MARK)
9868     return integer_type_node;
9869
9870   type = DECL_BIT_FIELD_TYPE (decl);
9871   if (type == NULL_TREE)
9872     type = TREE_TYPE (decl);
9873
9874   return type;
9875 }
9876
9877 /* Given a pointer to a tree node, return the alignment in bits for
9878    it, or else return BITS_PER_WORD if the node actually turns out to
9879    be an ERROR_MARK node.  */
9880
9881 static inline unsigned
9882 simple_type_align_in_bits (const_tree type)
9883 {
9884   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9885 }
9886
9887 static inline unsigned
9888 simple_decl_align_in_bits (const_tree decl)
9889 {
9890   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9891 }
9892
9893 /* Return the result of rounding T up to ALIGN.  */
9894
9895 static inline HOST_WIDE_INT
9896 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9897 {
9898   /* We must be careful if T is negative because HOST_WIDE_INT can be
9899      either "above" or "below" unsigned int as per the C promotion
9900      rules, depending on the host, thus making the signedness of the
9901      direct multiplication and division unpredictable.  */
9902   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9903
9904   u += align - 1;
9905   u /= align;
9906   u *= align;
9907
9908   return (HOST_WIDE_INT) u;
9909 }
9910
9911 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9912    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9913    or return 0 if we are unable to determine what that offset is, either
9914    because the argument turns out to be a pointer to an ERROR_MARK node, or
9915    because the offset is actually variable.  (We can't handle the latter case
9916    just yet).  */
9917
9918 static HOST_WIDE_INT
9919 field_byte_offset (const_tree decl)
9920 {
9921   HOST_WIDE_INT object_offset_in_bits;
9922   HOST_WIDE_INT bitpos_int;
9923
9924   if (TREE_CODE (decl) == ERROR_MARK)
9925     return 0;
9926
9927   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9928
9929   /* We cannot yet cope with fields whose positions are variable, so
9930      for now, when we see such things, we simply return 0.  Someday, we may
9931      be able to handle such cases, but it will be damn difficult.  */
9932   if (! host_integerp (bit_position (decl), 0))
9933     return 0;
9934
9935   bitpos_int = int_bit_position (decl);
9936
9937 #ifdef PCC_BITFIELD_TYPE_MATTERS
9938   if (PCC_BITFIELD_TYPE_MATTERS)
9939     {
9940       tree type;
9941       tree field_size_tree;
9942       HOST_WIDE_INT deepest_bitpos;
9943       unsigned HOST_WIDE_INT field_size_in_bits;
9944       unsigned int type_align_in_bits;
9945       unsigned int decl_align_in_bits;
9946       unsigned HOST_WIDE_INT type_size_in_bits;
9947
9948       type = field_type (decl);
9949       field_size_tree = DECL_SIZE (decl);
9950
9951       /* The size could be unspecified if there was an error, or for
9952          a flexible array member.  */
9953       if (! field_size_tree)
9954         field_size_tree = bitsize_zero_node;
9955
9956       /* If we don't know the size of the field, pretend it's a full word.  */
9957       if (host_integerp (field_size_tree, 1))
9958         field_size_in_bits = tree_low_cst (field_size_tree, 1);
9959       else
9960         field_size_in_bits = BITS_PER_WORD;
9961
9962       type_size_in_bits = simple_type_size_in_bits (type);
9963       type_align_in_bits = simple_type_align_in_bits (type);
9964       decl_align_in_bits = simple_decl_align_in_bits (decl);
9965
9966       /* The GCC front-end doesn't make any attempt to keep track of the
9967          starting bit offset (relative to the start of the containing
9968          structure type) of the hypothetical "containing object" for a
9969          bit-field.  Thus, when computing the byte offset value for the
9970          start of the "containing object" of a bit-field, we must deduce
9971          this information on our own. This can be rather tricky to do in
9972          some cases.  For example, handling the following structure type
9973          definition when compiling for an i386/i486 target (which only
9974          aligns long long's to 32-bit boundaries) can be very tricky:
9975
9976          struct S { int field1; long long field2:31; };
9977
9978          Fortunately, there is a simple rule-of-thumb which can be used
9979          in such cases.  When compiling for an i386/i486, GCC will
9980          allocate 8 bytes for the structure shown above.  It decides to
9981          do this based upon one simple rule for bit-field allocation.
9982          GCC allocates each "containing object" for each bit-field at
9983          the first (i.e. lowest addressed) legitimate alignment boundary
9984          (based upon the required minimum alignment for the declared
9985          type of the field) which it can possibly use, subject to the
9986          condition that there is still enough available space remaining
9987          in the containing object (when allocated at the selected point)
9988          to fully accommodate all of the bits of the bit-field itself.
9989
9990          This simple rule makes it obvious why GCC allocates 8 bytes for
9991          each object of the structure type shown above.  When looking
9992          for a place to allocate the "containing object" for `field2',
9993          the compiler simply tries to allocate a 64-bit "containing
9994          object" at each successive 32-bit boundary (starting at zero)
9995          until it finds a place to allocate that 64- bit field such that
9996          at least 31 contiguous (and previously unallocated) bits remain
9997          within that selected 64 bit field.  (As it turns out, for the
9998          example above, the compiler finds it is OK to allocate the
9999          "containing object" 64-bit field at bit-offset zero within the
10000          structure type.)
10001
10002          Here we attempt to work backwards from the limited set of facts
10003          we're given, and we try to deduce from those facts, where GCC
10004          must have believed that the containing object started (within
10005          the structure type). The value we deduce is then used (by the
10006          callers of this routine) to generate DW_AT_location and
10007          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10008          the case of DW_AT_location, regular fields as well).  */
10009
10010       /* Figure out the bit-distance from the start of the structure to
10011          the "deepest" bit of the bit-field.  */
10012       deepest_bitpos = bitpos_int + field_size_in_bits;
10013
10014       /* This is the tricky part.  Use some fancy footwork to deduce
10015          where the lowest addressed bit of the containing object must
10016          be.  */
10017       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10018
10019       /* Round up to type_align by default.  This works best for
10020          bitfields.  */
10021       object_offset_in_bits
10022         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10023
10024       if (object_offset_in_bits > bitpos_int)
10025         {
10026           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10027
10028           /* Round up to decl_align instead.  */
10029           object_offset_in_bits
10030             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10031         }
10032     }
10033   else
10034 #endif
10035     object_offset_in_bits = bitpos_int;
10036
10037   return object_offset_in_bits / BITS_PER_UNIT;
10038 }
10039 \f
10040 /* The following routines define various Dwarf attributes and any data
10041    associated with them.  */
10042
10043 /* Add a location description attribute value to a DIE.
10044
10045    This emits location attributes suitable for whole variables and
10046    whole parameters.  Note that the location attributes for struct fields are
10047    generated by the routine `data_member_location_attribute' below.  */
10048
10049 static inline void
10050 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10051                              dw_loc_descr_ref descr)
10052 {
10053   if (descr != 0)
10054     add_AT_loc (die, attr_kind, descr);
10055 }
10056
10057 /* Attach the specialized form of location attribute used for data members of
10058    struct and union types.  In the special case of a FIELD_DECL node which
10059    represents a bit-field, the "offset" part of this special location
10060    descriptor must indicate the distance in bytes from the lowest-addressed
10061    byte of the containing struct or union type to the lowest-addressed byte of
10062    the "containing object" for the bit-field.  (See the `field_byte_offset'
10063    function above).
10064
10065    For any given bit-field, the "containing object" is a hypothetical object
10066    (of some integral or enum type) within which the given bit-field lives.  The
10067    type of this hypothetical "containing object" is always the same as the
10068    declared type of the individual bit-field itself (for GCC anyway... the
10069    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10070    bytes) of the hypothetical "containing object" which will be given in the
10071    DW_AT_byte_size attribute for this bit-field.  (See the
10072    `byte_size_attribute' function below.)  It is also used when calculating the
10073    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10074    function below.)  */
10075
10076 static void
10077 add_data_member_location_attribute (dw_die_ref die, tree decl)
10078 {
10079   HOST_WIDE_INT offset;
10080   dw_loc_descr_ref loc_descr = 0;
10081
10082   if (TREE_CODE (decl) == TREE_BINFO)
10083     {
10084       /* We're working on the TAG_inheritance for a base class.  */
10085       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10086         {
10087           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10088              aren't at a fixed offset from all (sub)objects of the same
10089              type.  We need to extract the appropriate offset from our
10090              vtable.  The following dwarf expression means
10091
10092                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10093
10094              This is specific to the V3 ABI, of course.  */
10095
10096           dw_loc_descr_ref tmp;
10097
10098           /* Make a copy of the object address.  */
10099           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10100           add_loc_descr (&loc_descr, tmp);
10101
10102           /* Extract the vtable address.  */
10103           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10104           add_loc_descr (&loc_descr, tmp);
10105
10106           /* Calculate the address of the offset.  */
10107           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10108           gcc_assert (offset < 0);
10109
10110           tmp = int_loc_descriptor (-offset);
10111           add_loc_descr (&loc_descr, tmp);
10112           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10113           add_loc_descr (&loc_descr, tmp);
10114
10115           /* Extract the offset.  */
10116           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10117           add_loc_descr (&loc_descr, tmp);
10118
10119           /* Add it to the object address.  */
10120           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10121           add_loc_descr (&loc_descr, tmp);
10122         }
10123       else
10124         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10125     }
10126   else
10127     offset = field_byte_offset (decl);
10128
10129   if (! loc_descr)
10130     {
10131       enum dwarf_location_atom op;
10132
10133       /* The DWARF2 standard says that we should assume that the structure
10134          address is already on the stack, so we can specify a structure field
10135          address by using DW_OP_plus_uconst.  */
10136
10137 #ifdef MIPS_DEBUGGING_INFO
10138       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10139          operator correctly.  It works only if we leave the offset on the
10140          stack.  */
10141       op = DW_OP_constu;
10142 #else
10143       op = DW_OP_plus_uconst;
10144 #endif
10145
10146       loc_descr = new_loc_descr (op, offset, 0);
10147     }
10148
10149   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10150 }
10151
10152 /* Writes integer values to dw_vec_const array.  */
10153
10154 static void
10155 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10156 {
10157   while (size != 0)
10158     {
10159       *dest++ = val & 0xff;
10160       val >>= 8;
10161       --size;
10162     }
10163 }
10164
10165 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10166
10167 static HOST_WIDE_INT
10168 extract_int (const unsigned char *src, unsigned int size)
10169 {
10170   HOST_WIDE_INT val = 0;
10171
10172   src += size;
10173   while (size != 0)
10174     {
10175       val <<= 8;
10176       val |= *--src & 0xff;
10177       --size;
10178     }
10179   return val;
10180 }
10181
10182 /* Writes floating point values to dw_vec_const array.  */
10183
10184 static void
10185 insert_float (const_rtx rtl, unsigned char *array)
10186 {
10187   REAL_VALUE_TYPE rv;
10188   long val[4];
10189   int i;
10190
10191   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10192   real_to_target (val, &rv, GET_MODE (rtl));
10193
10194   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10195   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10196     {
10197       insert_int (val[i], 4, array);
10198       array += 4;
10199     }
10200 }
10201
10202 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10203    does not have a "location" either in memory or in a register.  These
10204    things can arise in GNU C when a constant is passed as an actual parameter
10205    to an inlined function.  They can also arise in C++ where declared
10206    constants do not necessarily get memory "homes".  */
10207
10208 static void
10209 add_const_value_attribute (dw_die_ref die, rtx rtl)
10210 {
10211   switch (GET_CODE (rtl))
10212     {
10213     case CONST_INT:
10214       {
10215         HOST_WIDE_INT val = INTVAL (rtl);
10216
10217         if (val < 0)
10218           add_AT_int (die, DW_AT_const_value, val);
10219         else
10220           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10221       }
10222       break;
10223
10224     case CONST_DOUBLE:
10225       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10226          floating-point constant.  A CONST_DOUBLE is used whenever the
10227          constant requires more than one word in order to be adequately
10228          represented.  We output CONST_DOUBLEs as blocks.  */
10229       {
10230         enum machine_mode mode = GET_MODE (rtl);
10231
10232         if (SCALAR_FLOAT_MODE_P (mode))
10233           {
10234             unsigned int length = GET_MODE_SIZE (mode);
10235             unsigned char *array = ggc_alloc (length);
10236
10237             insert_float (rtl, array);
10238             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10239           }
10240         else
10241           {
10242             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10243             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10244
10245             add_AT_long_long (die, DW_AT_const_value,
10246                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10247           }
10248       }
10249       break;
10250
10251     case CONST_VECTOR:
10252       {
10253         enum machine_mode mode = GET_MODE (rtl);
10254         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10255         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10256         unsigned char *array = ggc_alloc (length * elt_size);
10257         unsigned int i;
10258         unsigned char *p;
10259
10260         switch (GET_MODE_CLASS (mode))
10261           {
10262           case MODE_VECTOR_INT:
10263             for (i = 0, p = array; i < length; i++, p += elt_size)
10264               {
10265                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10266                 HOST_WIDE_INT lo, hi;
10267
10268                 switch (GET_CODE (elt))
10269                   {
10270                   case CONST_INT:
10271                     lo = INTVAL (elt);
10272                     hi = -(lo < 0);
10273                     break;
10274
10275                   case CONST_DOUBLE:
10276                     lo = CONST_DOUBLE_LOW (elt);
10277                     hi = CONST_DOUBLE_HIGH (elt);
10278                     break;
10279
10280                   default:
10281                     gcc_unreachable ();
10282                   }
10283
10284                 if (elt_size <= sizeof (HOST_WIDE_INT))
10285                   insert_int (lo, elt_size, p);
10286                 else
10287                   {
10288                     unsigned char *p0 = p;
10289                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10290
10291                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10292                     if (WORDS_BIG_ENDIAN)
10293                       {
10294                         p0 = p1;
10295                         p1 = p;
10296                       }
10297                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10298                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10299                   }
10300               }
10301             break;
10302
10303           case MODE_VECTOR_FLOAT:
10304             for (i = 0, p = array; i < length; i++, p += elt_size)
10305               {
10306                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10307                 insert_float (elt, p);
10308               }
10309             break;
10310
10311           default:
10312             gcc_unreachable ();
10313           }
10314
10315         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10316       }
10317       break;
10318
10319     case CONST_STRING:
10320       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10321       break;
10322
10323     case SYMBOL_REF:
10324     case LABEL_REF:
10325     case CONST:
10326       add_AT_addr (die, DW_AT_const_value, rtl);
10327       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10328       break;
10329
10330     case PLUS:
10331       /* In cases where an inlined instance of an inline function is passed
10332          the address of an `auto' variable (which is local to the caller) we
10333          can get a situation where the DECL_RTL of the artificial local
10334          variable (for the inlining) which acts as a stand-in for the
10335          corresponding formal parameter (of the inline function) will look
10336          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10337          exactly a compile-time constant expression, but it isn't the address
10338          of the (artificial) local variable either.  Rather, it represents the
10339          *value* which the artificial local variable always has during its
10340          lifetime.  We currently have no way to represent such quasi-constant
10341          values in Dwarf, so for now we just punt and generate nothing.  */
10342       break;
10343
10344     default:
10345       /* No other kinds of rtx should be possible here.  */
10346       gcc_unreachable ();
10347     }
10348
10349 }
10350
10351 /* Determine whether the evaluation of EXPR references any variables
10352    or functions which aren't otherwise used (and therefore may not be
10353    output).  */
10354 static tree
10355 reference_to_unused (tree * tp, int * walk_subtrees,
10356                      void * data ATTRIBUTE_UNUSED)
10357 {
10358   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10359     *walk_subtrees = 0;
10360
10361   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10362       && ! TREE_ASM_WRITTEN (*tp))
10363     return *tp;
10364   else if (!flag_unit_at_a_time)
10365     return NULL_TREE;
10366   /* ???  The C++ FE emits debug information for using decls, so
10367      putting gcc_unreachable here falls over.  See PR31899.  For now
10368      be conservative.  */
10369   else if (!cgraph_global_info_ready
10370            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10371     return *tp;
10372   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10373     {
10374       struct varpool_node *node = varpool_node (*tp);
10375       if (!node->needed)
10376         return *tp;
10377     }
10378   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10379            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10380     {
10381       struct cgraph_node *node = cgraph_node (*tp);
10382       if (!node->output)
10383         return *tp;
10384     }
10385   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10386     return *tp;
10387
10388   return NULL_TREE;
10389 }
10390
10391 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10392    for use in a later add_const_value_attribute call.  */
10393
10394 static rtx
10395 rtl_for_decl_init (tree init, tree type)
10396 {
10397   rtx rtl = NULL_RTX;
10398
10399   /* If a variable is initialized with a string constant without embedded
10400      zeros, build CONST_STRING.  */
10401   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10402     {
10403       tree enttype = TREE_TYPE (type);
10404       tree domain = TYPE_DOMAIN (type);
10405       enum machine_mode mode = TYPE_MODE (enttype);
10406
10407       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10408           && domain
10409           && integer_zerop (TYPE_MIN_VALUE (domain))
10410           && compare_tree_int (TYPE_MAX_VALUE (domain),
10411                                TREE_STRING_LENGTH (init) - 1) == 0
10412           && ((size_t) TREE_STRING_LENGTH (init)
10413               == strlen (TREE_STRING_POINTER (init)) + 1))
10414         rtl = gen_rtx_CONST_STRING (VOIDmode,
10415                                     ggc_strdup (TREE_STRING_POINTER (init)));
10416     }
10417   /* Other aggregates, and complex values, could be represented using
10418      CONCAT: FIXME!  */
10419   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10420     ;
10421   /* Vectors only work if their mode is supported by the target.
10422      FIXME: generic vectors ought to work too.  */
10423   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10424     ;
10425   /* If the initializer is something that we know will expand into an
10426      immediate RTL constant, expand it now.  We must be careful not to
10427      reference variables which won't be output.  */
10428   else if (initializer_constant_valid_p (init, type)
10429            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10430     {
10431       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10432          possible.  */
10433       if (TREE_CODE (type) == VECTOR_TYPE)
10434         switch (TREE_CODE (init))
10435           {
10436           case VECTOR_CST:
10437             break;
10438           case CONSTRUCTOR:
10439             if (TREE_CONSTANT (init))
10440               {
10441                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10442                 bool constant_p = true;
10443                 tree value;
10444                 unsigned HOST_WIDE_INT ix;
10445
10446                 /* Even when ctor is constant, it might contain non-*_CST
10447                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10448                    belong into VECTOR_CST nodes.  */
10449                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10450                   if (!CONSTANT_CLASS_P (value))
10451                     {
10452                       constant_p = false;
10453                       break;
10454                     }
10455
10456                 if (constant_p)
10457                   {
10458                     init = build_vector_from_ctor (type, elts);
10459                     break;
10460                   }
10461               }
10462             /* FALLTHRU */
10463
10464           default:
10465             return NULL;
10466           }
10467
10468       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10469
10470       /* If expand_expr returns a MEM, it wasn't immediate.  */
10471       gcc_assert (!rtl || !MEM_P (rtl));
10472     }
10473
10474   return rtl;
10475 }
10476
10477 /* Generate RTL for the variable DECL to represent its location.  */
10478
10479 static rtx
10480 rtl_for_decl_location (tree decl)
10481 {
10482   rtx rtl;
10483
10484   /* Here we have to decide where we are going to say the parameter "lives"
10485      (as far as the debugger is concerned).  We only have a couple of
10486      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10487
10488      DECL_RTL normally indicates where the parameter lives during most of the
10489      activation of the function.  If optimization is enabled however, this
10490      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10491      that the parameter doesn't really live anywhere (as far as the code
10492      generation parts of GCC are concerned) during most of the function's
10493      activation.  That will happen (for example) if the parameter is never
10494      referenced within the function.
10495
10496      We could just generate a location descriptor here for all non-NULL
10497      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10498      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10499      where DECL_RTL is NULL or is a pseudo-reg.
10500
10501      Note however that we can only get away with using DECL_INCOMING_RTL as
10502      a backup substitute for DECL_RTL in certain limited cases.  In cases
10503      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10504      we can be sure that the parameter was passed using the same type as it is
10505      declared to have within the function, and that its DECL_INCOMING_RTL
10506      points us to a place where a value of that type is passed.
10507
10508      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10509      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10510      because in these cases DECL_INCOMING_RTL points us to a value of some
10511      type which is *different* from the type of the parameter itself.  Thus,
10512      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10513      such cases, the debugger would end up (for example) trying to fetch a
10514      `float' from a place which actually contains the first part of a
10515      `double'.  That would lead to really incorrect and confusing
10516      output at debug-time.
10517
10518      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10519      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10520      are a couple of exceptions however.  On little-endian machines we can
10521      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10522      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10523      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10524      when (on a little-endian machine) a non-prototyped function has a
10525      parameter declared to be of type `short' or `char'.  In such cases,
10526      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10527      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10528      passed `int' value.  If the debugger then uses that address to fetch
10529      a `short' or a `char' (on a little-endian machine) the result will be
10530      the correct data, so we allow for such exceptional cases below.
10531
10532      Note that our goal here is to describe the place where the given formal
10533      parameter lives during most of the function's activation (i.e. between the
10534      end of the prologue and the start of the epilogue).  We'll do that as best
10535      as we can. Note however that if the given formal parameter is modified
10536      sometime during the execution of the function, then a stack backtrace (at
10537      debug-time) will show the function as having been called with the *new*
10538      value rather than the value which was originally passed in.  This happens
10539      rarely enough that it is not a major problem, but it *is* a problem, and
10540      I'd like to fix it.
10541
10542      A future version of dwarf2out.c may generate two additional attributes for
10543      any given DW_TAG_formal_parameter DIE which will describe the "passed
10544      type" and the "passed location" for the given formal parameter in addition
10545      to the attributes we now generate to indicate the "declared type" and the
10546      "active location" for each parameter.  This additional set of attributes
10547      could be used by debuggers for stack backtraces. Separately, note that
10548      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10549      This happens (for example) for inlined-instances of inline function formal
10550      parameters which are never referenced.  This really shouldn't be
10551      happening.  All PARM_DECL nodes should get valid non-NULL
10552      DECL_INCOMING_RTL values.  FIXME.  */
10553
10554   /* Use DECL_RTL as the "location" unless we find something better.  */
10555   rtl = DECL_RTL_IF_SET (decl);
10556
10557   /* When generating abstract instances, ignore everything except
10558      constants, symbols living in memory, and symbols living in
10559      fixed registers.  */
10560   if (! reload_completed)
10561     {
10562       if (rtl
10563           && (CONSTANT_P (rtl)
10564               || (MEM_P (rtl)
10565                   && CONSTANT_P (XEXP (rtl, 0)))
10566               || (REG_P (rtl)
10567                   && TREE_CODE (decl) == VAR_DECL
10568                   && TREE_STATIC (decl))))
10569         {
10570           rtl = targetm.delegitimize_address (rtl);
10571           return rtl;
10572         }
10573       rtl = NULL_RTX;
10574     }
10575   else if (TREE_CODE (decl) == PARM_DECL)
10576     {
10577       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10578         {
10579           tree declared_type = TREE_TYPE (decl);
10580           tree passed_type = DECL_ARG_TYPE (decl);
10581           enum machine_mode dmode = TYPE_MODE (declared_type);
10582           enum machine_mode pmode = TYPE_MODE (passed_type);
10583
10584           /* This decl represents a formal parameter which was optimized out.
10585              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10586              all cases where (rtl == NULL_RTX) just below.  */
10587           if (dmode == pmode)
10588             rtl = DECL_INCOMING_RTL (decl);
10589           else if (SCALAR_INT_MODE_P (dmode)
10590                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10591                    && DECL_INCOMING_RTL (decl))
10592             {
10593               rtx inc = DECL_INCOMING_RTL (decl);
10594               if (REG_P (inc))
10595                 rtl = inc;
10596               else if (MEM_P (inc))
10597                 {
10598                   if (BYTES_BIG_ENDIAN)
10599                     rtl = adjust_address_nv (inc, dmode,
10600                                              GET_MODE_SIZE (pmode)
10601                                              - GET_MODE_SIZE (dmode));
10602                   else
10603                     rtl = inc;
10604                 }
10605             }
10606         }
10607
10608       /* If the parm was passed in registers, but lives on the stack, then
10609          make a big endian correction if the mode of the type of the
10610          parameter is not the same as the mode of the rtl.  */
10611       /* ??? This is the same series of checks that are made in dbxout.c before
10612          we reach the big endian correction code there.  It isn't clear if all
10613          of these checks are necessary here, but keeping them all is the safe
10614          thing to do.  */
10615       else if (MEM_P (rtl)
10616                && XEXP (rtl, 0) != const0_rtx
10617                && ! CONSTANT_P (XEXP (rtl, 0))
10618                /* Not passed in memory.  */
10619                && !MEM_P (DECL_INCOMING_RTL (decl))
10620                /* Not passed by invisible reference.  */
10621                && (!REG_P (XEXP (rtl, 0))
10622                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10623                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10624 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10625                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10626 #endif
10627                      )
10628                /* Big endian correction check.  */
10629                && BYTES_BIG_ENDIAN
10630                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10631                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10632                    < UNITS_PER_WORD))
10633         {
10634           int offset = (UNITS_PER_WORD
10635                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10636
10637           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10638                              plus_constant (XEXP (rtl, 0), offset));
10639         }
10640     }
10641   else if (TREE_CODE (decl) == VAR_DECL
10642            && rtl
10643            && MEM_P (rtl)
10644            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10645            && BYTES_BIG_ENDIAN)
10646     {
10647       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10648       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10649
10650       /* If a variable is declared "register" yet is smaller than
10651          a register, then if we store the variable to memory, it
10652          looks like we're storing a register-sized value, when in
10653          fact we are not.  We need to adjust the offset of the
10654          storage location to reflect the actual value's bytes,
10655          else gdb will not be able to display it.  */
10656       if (rsize > dsize)
10657         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10658                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10659     }
10660
10661   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10662      and will have been substituted directly into all expressions that use it.
10663      C does not have such a concept, but C++ and other languages do.  */
10664   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10665     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10666
10667   if (rtl)
10668     rtl = targetm.delegitimize_address (rtl);
10669
10670   /* If we don't look past the constant pool, we risk emitting a
10671      reference to a constant pool entry that isn't referenced from
10672      code, and thus is not emitted.  */
10673   if (rtl)
10674     rtl = avoid_constant_pool_reference (rtl);
10675
10676   return rtl;
10677 }
10678
10679 /* We need to figure out what section we should use as the base for the
10680    address ranges where a given location is valid.
10681    1. If this particular DECL has a section associated with it, use that.
10682    2. If this function has a section associated with it, use that.
10683    3. Otherwise, use the text section.
10684    XXX: If you split a variable across multiple sections, we won't notice.  */
10685
10686 static const char *
10687 secname_for_decl (const_tree decl)
10688 {
10689   const char *secname;
10690
10691   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10692     {
10693       tree sectree = DECL_SECTION_NAME (decl);
10694       secname = TREE_STRING_POINTER (sectree);
10695     }
10696   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10697     {
10698       tree sectree = DECL_SECTION_NAME (current_function_decl);
10699       secname = TREE_STRING_POINTER (sectree);
10700     }
10701   else if (cfun && in_cold_section_p)
10702     secname = cfun->cold_section_label;
10703   else
10704     secname = text_section_label;
10705
10706   return secname;
10707 }
10708
10709 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10710    data attribute for a variable or a parameter.  We generate the
10711    DW_AT_const_value attribute only in those cases where the given variable
10712    or parameter does not have a true "location" either in memory or in a
10713    register.  This can happen (for example) when a constant is passed as an
10714    actual argument in a call to an inline function.  (It's possible that
10715    these things can crop up in other ways also.)  Note that one type of
10716    constant value which can be passed into an inlined function is a constant
10717    pointer.  This can happen for example if an actual argument in an inlined
10718    function call evaluates to a compile-time constant address.  */
10719
10720 static void
10721 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10722                                        enum dwarf_attribute attr)
10723 {
10724   rtx rtl;
10725   dw_loc_descr_ref descr;
10726   var_loc_list *loc_list;
10727   struct var_loc_node *node;
10728   if (TREE_CODE (decl) == ERROR_MARK)
10729     return;
10730
10731   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10732               || TREE_CODE (decl) == RESULT_DECL);
10733
10734   /* See if we possibly have multiple locations for this variable.  */
10735   loc_list = lookup_decl_loc (decl);
10736
10737   /* If it truly has multiple locations, the first and last node will
10738      differ.  */
10739   if (loc_list && loc_list->first != loc_list->last)
10740     {
10741       const char *endname, *secname;
10742       dw_loc_list_ref list;
10743       rtx varloc;
10744       enum var_init_status initialized;
10745
10746       /* Now that we know what section we are using for a base,
10747          actually construct the list of locations.
10748          The first location information is what is passed to the
10749          function that creates the location list, and the remaining
10750          locations just get added on to that list.
10751          Note that we only know the start address for a location
10752          (IE location changes), so to build the range, we use
10753          the range [current location start, next location start].
10754          This means we have to special case the last node, and generate
10755          a range of [last location start, end of function label].  */
10756
10757       node = loc_list->first;
10758       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10759       secname = secname_for_decl (decl);
10760
10761       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10762         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10763       else
10764         initialized = VAR_INIT_STATUS_INITIALIZED;
10765
10766       list = new_loc_list (loc_descriptor (varloc, initialized),
10767                            node->label, node->next->label, secname, 1);
10768       node = node->next;
10769
10770       for (; node->next; node = node->next)
10771         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10772           {
10773             /* The variable has a location between NODE->LABEL and
10774                NODE->NEXT->LABEL.  */
10775             enum var_init_status initialized =
10776               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10777             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10778             add_loc_descr_to_loc_list (&list, 
10779                                        loc_descriptor (varloc, initialized),
10780                                        node->label, node->next->label, secname);
10781           }
10782
10783       /* If the variable has a location at the last label
10784          it keeps its location until the end of function.  */
10785       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10786         {
10787           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10788           enum var_init_status initialized =
10789             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10790
10791           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10792           if (!current_function_decl)
10793             endname = text_end_label;
10794           else
10795             {
10796               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10797                                            current_function_funcdef_no);
10798               endname = ggc_strdup (label_id);
10799             }
10800           add_loc_descr_to_loc_list (&list, 
10801                                      loc_descriptor (varloc, initialized),
10802                                      node->label, endname, secname);
10803         }
10804
10805       /* Finally, add the location list to the DIE, and we are done.  */
10806       add_AT_loc_list (die, attr, list);
10807       return;
10808     }
10809
10810   /* Try to get some constant RTL for this decl, and use that as the value of
10811      the location.  */
10812
10813   rtl = rtl_for_decl_location (decl);
10814   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10815     {
10816       add_const_value_attribute (die, rtl);
10817       return;
10818     }
10819
10820   /* If we have tried to generate the location otherwise, and it
10821      didn't work out (we wouldn't be here if we did), and we have a one entry
10822      location list, try generating a location from that.  */
10823   if (loc_list && loc_list->first)
10824     {
10825       enum var_init_status status;
10826       node = loc_list->first;
10827       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10828       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10829       if (descr)
10830         {
10831           add_AT_location_description (die, attr, descr);
10832           return;
10833         }
10834     }
10835
10836   /* We couldn't get any rtl, so try directly generating the location
10837      description from the tree.  */
10838   descr = loc_descriptor_from_tree (decl);
10839   if (descr)
10840     {
10841       add_AT_location_description (die, attr, descr);
10842       return;
10843     }
10844   /* None of that worked, so it must not really have a location;
10845      try adding a constant value attribute from the DECL_INITIAL.  */
10846   tree_add_const_value_attribute (die, decl);
10847 }
10848
10849 /* If we don't have a copy of this variable in memory for some reason (such
10850    as a C++ member constant that doesn't have an out-of-line definition),
10851    we should tell the debugger about the constant value.  */
10852
10853 static void
10854 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10855 {
10856   tree init = DECL_INITIAL (decl);
10857   tree type = TREE_TYPE (decl);
10858   rtx rtl;
10859
10860   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10861     /* OK */;
10862   else
10863     return;
10864
10865   rtl = rtl_for_decl_init (init, type);
10866   if (rtl)
10867     add_const_value_attribute (var_die, rtl);
10868 }
10869
10870 /* Convert the CFI instructions for the current function into a
10871    location list.  This is used for DW_AT_frame_base when we targeting
10872    a dwarf2 consumer that does not support the dwarf3
10873    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10874    expressions.  */
10875
10876 static dw_loc_list_ref
10877 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10878 {
10879   dw_fde_ref fde;
10880   dw_loc_list_ref list, *list_tail;
10881   dw_cfi_ref cfi;
10882   dw_cfa_location last_cfa, next_cfa;
10883   const char *start_label, *last_label, *section;
10884
10885   fde = &fde_table[fde_table_in_use - 1];
10886
10887   section = secname_for_decl (current_function_decl);
10888   list_tail = &list;
10889   list = NULL;
10890
10891   next_cfa.reg = INVALID_REGNUM;
10892   next_cfa.offset = 0;
10893   next_cfa.indirect = 0;
10894   next_cfa.base_offset = 0;
10895
10896   start_label = fde->dw_fde_begin;
10897
10898   /* ??? Bald assumption that the CIE opcode list does not contain
10899      advance opcodes.  */
10900   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10901     lookup_cfa_1 (cfi, &next_cfa);
10902
10903   last_cfa = next_cfa;
10904   last_label = start_label;
10905
10906   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10907     switch (cfi->dw_cfi_opc)
10908       {
10909       case DW_CFA_set_loc:
10910       case DW_CFA_advance_loc1:
10911       case DW_CFA_advance_loc2:
10912       case DW_CFA_advance_loc4:
10913         if (!cfa_equal_p (&last_cfa, &next_cfa))
10914           {
10915             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10916                                        start_label, last_label, section,
10917                                        list == NULL);
10918
10919             list_tail = &(*list_tail)->dw_loc_next;
10920             last_cfa = next_cfa;
10921             start_label = last_label;
10922           }
10923         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10924         break;
10925
10926       case DW_CFA_advance_loc:
10927         /* The encoding is complex enough that we should never emit this.  */
10928       case DW_CFA_remember_state:
10929       case DW_CFA_restore_state:
10930         /* We don't handle these two in this function.  It would be possible
10931            if it were to be required.  */
10932         gcc_unreachable ();
10933
10934       default:
10935         lookup_cfa_1 (cfi, &next_cfa);
10936         break;
10937       }
10938
10939   if (!cfa_equal_p (&last_cfa, &next_cfa))
10940     {
10941       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10942                                  start_label, last_label, section,
10943                                  list == NULL);
10944       list_tail = &(*list_tail)->dw_loc_next;
10945       start_label = last_label;
10946     }
10947   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10948                              start_label, fde->dw_fde_end, section,
10949                              list == NULL);
10950
10951   return list;
10952 }
10953
10954 /* Compute a displacement from the "steady-state frame pointer" to the
10955    frame base (often the same as the CFA), and store it in
10956    frame_pointer_fb_offset.  OFFSET is added to the displacement
10957    before the latter is negated.  */
10958
10959 static void
10960 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10961 {
10962   rtx reg, elim;
10963
10964 #ifdef FRAME_POINTER_CFA_OFFSET
10965   reg = frame_pointer_rtx;
10966   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10967 #else
10968   reg = arg_pointer_rtx;
10969   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
10970 #endif
10971
10972   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10973   if (GET_CODE (elim) == PLUS)
10974     {
10975       offset += INTVAL (XEXP (elim, 1));
10976       elim = XEXP (elim, 0);
10977     }
10978   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
10979                        : stack_pointer_rtx));
10980
10981   frame_pointer_fb_offset = -offset;
10982 }
10983
10984 /* Generate a DW_AT_name attribute given some string value to be included as
10985    the value of the attribute.  */
10986
10987 static void
10988 add_name_attribute (dw_die_ref die, const char *name_string)
10989 {
10990   if (name_string != NULL && *name_string != 0)
10991     {
10992       if (demangle_name_func)
10993         name_string = (*demangle_name_func) (name_string);
10994
10995       add_AT_string (die, DW_AT_name, name_string);
10996     }
10997 }
10998
10999 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11000
11001 static void
11002 add_comp_dir_attribute (dw_die_ref die)
11003 {
11004   const char *wd = get_src_pwd ();
11005   if (wd != NULL)
11006     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11007 }
11008
11009 /* Given a tree node describing an array bound (either lower or upper) output
11010    a representation for that bound.  */
11011
11012 static void
11013 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11014 {
11015   switch (TREE_CODE (bound))
11016     {
11017     case ERROR_MARK:
11018       return;
11019
11020     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11021     case INTEGER_CST:
11022       if (! host_integerp (bound, 0)
11023           || (bound_attr == DW_AT_lower_bound
11024               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11025                   || (is_fortran () && integer_onep (bound)))))
11026         /* Use the default.  */
11027         ;
11028       else
11029         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11030       break;
11031
11032     case CONVERT_EXPR:
11033     case NOP_EXPR:
11034     case NON_LVALUE_EXPR:
11035     case VIEW_CONVERT_EXPR:
11036       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11037       break;
11038
11039     case SAVE_EXPR:
11040       break;
11041
11042     case VAR_DECL:
11043     case PARM_DECL:
11044     case RESULT_DECL:
11045       {
11046         dw_die_ref decl_die = lookup_decl_die (bound);
11047
11048         /* ??? Can this happen, or should the variable have been bound
11049            first?  Probably it can, since I imagine that we try to create
11050            the types of parameters in the order in which they exist in
11051            the list, and won't have created a forward reference to a
11052            later parameter.  */
11053         if (decl_die != NULL)
11054           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11055         break;
11056       }
11057
11058     default:
11059       {
11060         /* Otherwise try to create a stack operation procedure to
11061            evaluate the value of the array bound.  */
11062
11063         dw_die_ref ctx, decl_die;
11064         dw_loc_descr_ref loc;
11065
11066         loc = loc_descriptor_from_tree (bound);
11067         if (loc == NULL)
11068           break;
11069
11070         if (current_function_decl == 0)
11071           ctx = comp_unit_die;
11072         else
11073           ctx = lookup_decl_die (current_function_decl);
11074
11075         decl_die = new_die (DW_TAG_variable, ctx, bound);
11076         add_AT_flag (decl_die, DW_AT_artificial, 1);
11077         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11078         add_AT_loc (decl_die, DW_AT_location, loc);
11079
11080         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11081         break;
11082       }
11083     }
11084 }
11085
11086 /* Note that the block of subscript information for an array type also
11087    includes information about the element type of type given array type.  */
11088
11089 static void
11090 add_subscript_info (dw_die_ref type_die, tree type)
11091 {
11092 #ifndef MIPS_DEBUGGING_INFO
11093   unsigned dimension_number;
11094 #endif
11095   tree lower, upper;
11096   dw_die_ref subrange_die;
11097
11098   /* The GNU compilers represent multidimensional array types as sequences of
11099      one dimensional array types whose element types are themselves array
11100      types.  Here we squish that down, so that each multidimensional array
11101      type gets only one array_type DIE in the Dwarf debugging info. The draft
11102      Dwarf specification say that we are allowed to do this kind of
11103      compression in C (because there is no difference between an array or
11104      arrays and a multidimensional array in C) but for other source languages
11105      (e.g. Ada) we probably shouldn't do this.  */
11106
11107   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11108      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11109      We work around this by disabling this feature.  See also
11110      gen_array_type_die.  */
11111 #ifndef MIPS_DEBUGGING_INFO
11112   for (dimension_number = 0;
11113        TREE_CODE (type) == ARRAY_TYPE;
11114        type = TREE_TYPE (type), dimension_number++)
11115 #endif
11116     {
11117       tree domain = TYPE_DOMAIN (type);
11118
11119       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11120          and (in GNU C only) variable bounds.  Handle all three forms
11121          here.  */
11122       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11123       if (domain)
11124         {
11125           /* We have an array type with specified bounds.  */
11126           lower = TYPE_MIN_VALUE (domain);
11127           upper = TYPE_MAX_VALUE (domain);
11128
11129           /* Define the index type.  */
11130           if (TREE_TYPE (domain))
11131             {
11132               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11133                  TREE_TYPE field.  We can't emit debug info for this
11134                  because it is an unnamed integral type.  */
11135               if (TREE_CODE (domain) == INTEGER_TYPE
11136                   && TYPE_NAME (domain) == NULL_TREE
11137                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11138                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11139                 ;
11140               else
11141                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11142                                     type_die);
11143             }
11144
11145           /* ??? If upper is NULL, the array has unspecified length,
11146              but it does have a lower bound.  This happens with Fortran
11147                dimension arr(N:*)
11148              Since the debugger is definitely going to need to know N
11149              to produce useful results, go ahead and output the lower
11150              bound solo, and hope the debugger can cope.  */
11151
11152           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11153           if (upper)
11154             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11155         }
11156
11157       /* Otherwise we have an array type with an unspecified length.  The
11158          DWARF-2 spec does not say how to handle this; let's just leave out the
11159          bounds.  */
11160     }
11161 }
11162
11163 static void
11164 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11165 {
11166   unsigned size;
11167
11168   switch (TREE_CODE (tree_node))
11169     {
11170     case ERROR_MARK:
11171       size = 0;
11172       break;
11173     case ENUMERAL_TYPE:
11174     case RECORD_TYPE:
11175     case UNION_TYPE:
11176     case QUAL_UNION_TYPE:
11177       size = int_size_in_bytes (tree_node);
11178       break;
11179     case FIELD_DECL:
11180       /* For a data member of a struct or union, the DW_AT_byte_size is
11181          generally given as the number of bytes normally allocated for an
11182          object of the *declared* type of the member itself.  This is true
11183          even for bit-fields.  */
11184       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11185       break;
11186     default:
11187       gcc_unreachable ();
11188     }
11189
11190   /* Note that `size' might be -1 when we get to this point.  If it is, that
11191      indicates that the byte size of the entity in question is variable.  We
11192      have no good way of expressing this fact in Dwarf at the present time,
11193      so just let the -1 pass on through.  */
11194   add_AT_unsigned (die, DW_AT_byte_size, size);
11195 }
11196
11197 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11198    which specifies the distance in bits from the highest order bit of the
11199    "containing object" for the bit-field to the highest order bit of the
11200    bit-field itself.
11201
11202    For any given bit-field, the "containing object" is a hypothetical object
11203    (of some integral or enum type) within which the given bit-field lives.  The
11204    type of this hypothetical "containing object" is always the same as the
11205    declared type of the individual bit-field itself.  The determination of the
11206    exact location of the "containing object" for a bit-field is rather
11207    complicated.  It's handled by the `field_byte_offset' function (above).
11208
11209    Note that it is the size (in bytes) of the hypothetical "containing object"
11210    which will be given in the DW_AT_byte_size attribute for this bit-field.
11211    (See `byte_size_attribute' above).  */
11212
11213 static inline void
11214 add_bit_offset_attribute (dw_die_ref die, tree decl)
11215 {
11216   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11217   tree type = DECL_BIT_FIELD_TYPE (decl);
11218   HOST_WIDE_INT bitpos_int;
11219   HOST_WIDE_INT highest_order_object_bit_offset;
11220   HOST_WIDE_INT highest_order_field_bit_offset;
11221   HOST_WIDE_INT unsigned bit_offset;
11222
11223   /* Must be a field and a bit field.  */
11224   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11225
11226   /* We can't yet handle bit-fields whose offsets are variable, so if we
11227      encounter such things, just return without generating any attribute
11228      whatsoever.  Likewise for variable or too large size.  */
11229   if (! host_integerp (bit_position (decl), 0)
11230       || ! host_integerp (DECL_SIZE (decl), 1))
11231     return;
11232
11233   bitpos_int = int_bit_position (decl);
11234
11235   /* Note that the bit offset is always the distance (in bits) from the
11236      highest-order bit of the "containing object" to the highest-order bit of
11237      the bit-field itself.  Since the "high-order end" of any object or field
11238      is different on big-endian and little-endian machines, the computation
11239      below must take account of these differences.  */
11240   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11241   highest_order_field_bit_offset = bitpos_int;
11242
11243   if (! BYTES_BIG_ENDIAN)
11244     {
11245       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11246       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11247     }
11248
11249   bit_offset
11250     = (! BYTES_BIG_ENDIAN
11251        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11252        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11253
11254   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11255 }
11256
11257 /* For a FIELD_DECL node which represents a bit field, output an attribute
11258    which specifies the length in bits of the given field.  */
11259
11260 static inline void
11261 add_bit_size_attribute (dw_die_ref die, tree decl)
11262 {
11263   /* Must be a field and a bit field.  */
11264   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11265               && DECL_BIT_FIELD_TYPE (decl));
11266
11267   if (host_integerp (DECL_SIZE (decl), 1))
11268     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11269 }
11270
11271 /* If the compiled language is ANSI C, then add a 'prototyped'
11272    attribute, if arg types are given for the parameters of a function.  */
11273
11274 static inline void
11275 add_prototyped_attribute (dw_die_ref die, tree func_type)
11276 {
11277   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11278       && TYPE_ARG_TYPES (func_type) != NULL)
11279     add_AT_flag (die, DW_AT_prototyped, 1);
11280 }
11281
11282 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11283    by looking in either the type declaration or object declaration
11284    equate table.  */
11285
11286 static inline void
11287 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11288 {
11289   dw_die_ref origin_die = NULL;
11290
11291   if (TREE_CODE (origin) != FUNCTION_DECL)
11292     {
11293       /* We may have gotten separated from the block for the inlined
11294          function, if we're in an exception handler or some such; make
11295          sure that the abstract function has been written out.
11296
11297          Doing this for nested functions is wrong, however; functions are
11298          distinct units, and our context might not even be inline.  */
11299       tree fn = origin;
11300
11301       if (TYPE_P (fn))
11302         fn = TYPE_STUB_DECL (fn);
11303
11304       fn = decl_function_context (fn);
11305       if (fn)
11306         dwarf2out_abstract_function (fn);
11307     }
11308
11309   if (DECL_P (origin))
11310     origin_die = lookup_decl_die (origin);
11311   else if (TYPE_P (origin))
11312     origin_die = lookup_type_die (origin);
11313
11314   /* XXX: Functions that are never lowered don't always have correct block
11315      trees (in the case of java, they simply have no block tree, in some other
11316      languages).  For these functions, there is nothing we can really do to
11317      output correct debug info for inlined functions in all cases.  Rather
11318      than die, we'll just produce deficient debug info now, in that we will
11319      have variables without a proper abstract origin.  In the future, when all
11320      functions are lowered, we should re-add a gcc_assert (origin_die)
11321      here.  */
11322
11323   if (origin_die)
11324       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11325 }
11326
11327 /* We do not currently support the pure_virtual attribute.  */
11328
11329 static inline void
11330 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11331 {
11332   if (DECL_VINDEX (func_decl))
11333     {
11334       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11335
11336       if (host_integerp (DECL_VINDEX (func_decl), 0))
11337         add_AT_loc (die, DW_AT_vtable_elem_location,
11338                     new_loc_descr (DW_OP_constu,
11339                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11340                                    0));
11341
11342       /* GNU extension: Record what type this method came from originally.  */
11343       if (debug_info_level > DINFO_LEVEL_TERSE)
11344         add_AT_die_ref (die, DW_AT_containing_type,
11345                         lookup_type_die (DECL_CONTEXT (func_decl)));
11346     }
11347 }
11348 \f
11349 /* Add source coordinate attributes for the given decl.  */
11350
11351 static void
11352 add_src_coords_attributes (dw_die_ref die, tree decl)
11353 {
11354   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11355
11356   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11357   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11358 }
11359
11360 /* Add a DW_AT_name attribute and source coordinate attribute for the
11361    given decl, but only if it actually has a name.  */
11362
11363 static void
11364 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11365 {
11366   tree decl_name;
11367
11368   decl_name = DECL_NAME (decl);
11369   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11370     {
11371       add_name_attribute (die, dwarf2_name (decl, 0));
11372       if (! DECL_ARTIFICIAL (decl))
11373         add_src_coords_attributes (die, decl);
11374
11375       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11376           && TREE_PUBLIC (decl)
11377           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11378           && !DECL_ABSTRACT (decl)
11379           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11380           && !is_fortran ())
11381         add_AT_string (die, DW_AT_MIPS_linkage_name,
11382                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11383     }
11384
11385 #ifdef VMS_DEBUGGING_INFO
11386   /* Get the function's name, as described by its RTL.  This may be different
11387      from the DECL_NAME name used in the source file.  */
11388   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11389     {
11390       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11391                    XEXP (DECL_RTL (decl), 0));
11392       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11393     }
11394 #endif
11395 }
11396
11397 /* Push a new declaration scope.  */
11398
11399 static void
11400 push_decl_scope (tree scope)
11401 {
11402   VEC_safe_push (tree, gc, decl_scope_table, scope);
11403 }
11404
11405 /* Pop a declaration scope.  */
11406
11407 static inline void
11408 pop_decl_scope (void)
11409 {
11410   VEC_pop (tree, decl_scope_table);
11411 }
11412
11413 /* Return the DIE for the scope that immediately contains this type.
11414    Non-named types get global scope.  Named types nested in other
11415    types get their containing scope if it's open, or global scope
11416    otherwise.  All other types (i.e. function-local named types) get
11417    the current active scope.  */
11418
11419 static dw_die_ref
11420 scope_die_for (tree t, dw_die_ref context_die)
11421 {
11422   dw_die_ref scope_die = NULL;
11423   tree containing_scope;
11424   int i;
11425
11426   /* Non-types always go in the current scope.  */
11427   gcc_assert (TYPE_P (t));
11428
11429   containing_scope = TYPE_CONTEXT (t);
11430
11431   /* Use the containing namespace if it was passed in (for a declaration).  */
11432   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11433     {
11434       if (context_die == lookup_decl_die (containing_scope))
11435         /* OK */;
11436       else
11437         containing_scope = NULL_TREE;
11438     }
11439
11440   /* Ignore function type "scopes" from the C frontend.  They mean that
11441      a tagged type is local to a parmlist of a function declarator, but
11442      that isn't useful to DWARF.  */
11443   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11444     containing_scope = NULL_TREE;
11445
11446   if (containing_scope == NULL_TREE)
11447     scope_die = comp_unit_die;
11448   else if (TYPE_P (containing_scope))
11449     {
11450       /* For types, we can just look up the appropriate DIE.  But
11451          first we check to see if we're in the middle of emitting it
11452          so we know where the new DIE should go.  */
11453       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11454         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11455           break;
11456
11457       if (i < 0)
11458         {
11459           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11460                       || TREE_ASM_WRITTEN (containing_scope));
11461
11462           /* If none of the current dies are suitable, we get file scope.  */
11463           scope_die = comp_unit_die;
11464         }
11465       else
11466         scope_die = lookup_type_die (containing_scope);
11467     }
11468   else
11469     scope_die = context_die;
11470
11471   return scope_die;
11472 }
11473
11474 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11475
11476 static inline int
11477 local_scope_p (dw_die_ref context_die)
11478 {
11479   for (; context_die; context_die = context_die->die_parent)
11480     if (context_die->die_tag == DW_TAG_inlined_subroutine
11481         || context_die->die_tag == DW_TAG_subprogram)
11482       return 1;
11483
11484   return 0;
11485 }
11486
11487 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11488    whether or not to treat a DIE in this context as a declaration.  */
11489
11490 static inline int
11491 class_or_namespace_scope_p (dw_die_ref context_die)
11492 {
11493   return (context_die
11494           && (context_die->die_tag == DW_TAG_structure_type
11495               || context_die->die_tag == DW_TAG_class_type
11496               || context_die->die_tag == DW_TAG_interface_type
11497               || context_die->die_tag == DW_TAG_union_type
11498               || context_die->die_tag == DW_TAG_namespace));
11499 }
11500
11501 /* Many forms of DIEs require a "type description" attribute.  This
11502    routine locates the proper "type descriptor" die for the type given
11503    by 'type', and adds a DW_AT_type attribute below the given die.  */
11504
11505 static void
11506 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11507                     int decl_volatile, dw_die_ref context_die)
11508 {
11509   enum tree_code code  = TREE_CODE (type);
11510   dw_die_ref type_die  = NULL;
11511
11512   /* ??? If this type is an unnamed subrange type of an integral, floating-point
11513      or fixed-point type, use the inner type.  This is because we have no
11514      support for unnamed types in base_type_die.  This can happen if this is
11515      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11516   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11517       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11518     type = TREE_TYPE (type), code = TREE_CODE (type);
11519
11520   if (code == ERROR_MARK
11521       /* Handle a special case.  For functions whose return type is void, we
11522          generate *no* type attribute.  (Note that no object may have type
11523          `void', so this only applies to function return types).  */
11524       || code == VOID_TYPE)
11525     return;
11526
11527   type_die = modified_type_die (type,
11528                                 decl_const || TYPE_READONLY (type),
11529                                 decl_volatile || TYPE_VOLATILE (type),
11530                                 context_die);
11531
11532   if (type_die != NULL)
11533     add_AT_die_ref (object_die, DW_AT_type, type_die);
11534 }
11535
11536 /* Given an object die, add the calling convention attribute for the
11537    function call type.  */
11538 static void
11539 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11540 {
11541   enum dwarf_calling_convention value = DW_CC_normal;
11542
11543   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11544
11545   /* DWARF doesn't provide a way to identify a program's source-level
11546      entry point.  DW_AT_calling_convention attributes are only meant
11547      to describe functions' calling conventions.  However, lacking a
11548      better way to signal the Fortran main program, we use this for the
11549      time being, following existing custom.  */
11550   if (is_fortran ()
11551       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11552     value = DW_CC_program;
11553
11554   /* Only add the attribute if the backend requests it, and
11555      is not DW_CC_normal.  */
11556   if (value && (value != DW_CC_normal))
11557     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11558 }
11559
11560 /* Given a tree pointer to a struct, class, union, or enum type node, return
11561    a pointer to the (string) tag name for the given type, or zero if the type
11562    was declared without a tag.  */
11563
11564 static const char *
11565 type_tag (const_tree type)
11566 {
11567   const char *name = 0;
11568
11569   if (TYPE_NAME (type) != 0)
11570     {
11571       tree t = 0;
11572
11573       /* Find the IDENTIFIER_NODE for the type name.  */
11574       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11575         t = TYPE_NAME (type);
11576
11577       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11578          a TYPE_DECL node, regardless of whether or not a `typedef' was
11579          involved.  */
11580       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11581                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11582         {
11583           /* We want to be extra verbose.  Don't call dwarf_name if
11584              DECL_NAME isn't set.  The default hook for decl_printable_name
11585              doesn't like that, and in this context it's correct to return
11586              0, instead of "<anonymous>" or the like.  */
11587           if (DECL_NAME (TYPE_NAME (type)))
11588             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11589         }
11590
11591       /* Now get the name as a string, or invent one.  */
11592       if (!name && t != 0)
11593         name = IDENTIFIER_POINTER (t);
11594     }
11595
11596   return (name == 0 || *name == '\0') ? 0 : name;
11597 }
11598
11599 /* Return the type associated with a data member, make a special check
11600    for bit field types.  */
11601
11602 static inline tree
11603 member_declared_type (const_tree member)
11604 {
11605   return (DECL_BIT_FIELD_TYPE (member)
11606           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11607 }
11608
11609 /* Get the decl's label, as described by its RTL. This may be different
11610    from the DECL_NAME name used in the source file.  */
11611
11612 #if 0
11613 static const char *
11614 decl_start_label (tree decl)
11615 {
11616   rtx x;
11617   const char *fnname;
11618
11619   x = DECL_RTL (decl);
11620   gcc_assert (MEM_P (x));
11621
11622   x = XEXP (x, 0);
11623   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11624
11625   fnname = XSTR (x, 0);
11626   return fnname;
11627 }
11628 #endif
11629 \f
11630 /* These routines generate the internal representation of the DIE's for
11631    the compilation unit.  Debugging information is collected by walking
11632    the declaration trees passed in from dwarf2out_decl().  */
11633
11634 static void
11635 gen_array_type_die (tree type, dw_die_ref context_die)
11636 {
11637   dw_die_ref scope_die = scope_die_for (type, context_die);
11638   dw_die_ref array_die;
11639   tree element_type;
11640
11641   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11642      the inner array type comes before the outer array type.  Thus we must
11643      call gen_type_die before we call new_die.  See below also.  */
11644 #ifdef MIPS_DEBUGGING_INFO
11645   gen_type_die (TREE_TYPE (type), context_die);
11646 #endif
11647
11648   array_die = new_die (DW_TAG_array_type, scope_die, type);
11649   add_name_attribute (array_die, type_tag (type));
11650   equate_type_number_to_die (type, array_die);
11651
11652   if (TREE_CODE (type) == VECTOR_TYPE)
11653     {
11654       /* The frontend feeds us a representation for the vector as a struct
11655          containing an array.  Pull out the array type.  */
11656       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11657       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11658     }
11659
11660   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11661   if (is_fortran ()
11662       && TREE_CODE (type) == ARRAY_TYPE
11663       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11664     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11665
11666 #if 0
11667   /* We default the array ordering.  SDB will probably do
11668      the right things even if DW_AT_ordering is not present.  It's not even
11669      an issue until we start to get into multidimensional arrays anyway.  If
11670      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11671      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11672      and when we find out that we need to put these in, we will only do so
11673      for multidimensional arrays.  */
11674   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11675 #endif
11676
11677 #ifdef MIPS_DEBUGGING_INFO
11678   /* The SGI compilers handle arrays of unknown bound by setting
11679      AT_declaration and not emitting any subrange DIEs.  */
11680   if (! TYPE_DOMAIN (type))
11681     add_AT_flag (array_die, DW_AT_declaration, 1);
11682   else
11683 #endif
11684     add_subscript_info (array_die, type);
11685
11686   /* Add representation of the type of the elements of this array type.  */
11687   element_type = TREE_TYPE (type);
11688
11689   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11690      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11691      We work around this by disabling this feature.  See also
11692      add_subscript_info.  */
11693 #ifndef MIPS_DEBUGGING_INFO
11694   while (TREE_CODE (element_type) == ARRAY_TYPE)
11695     element_type = TREE_TYPE (element_type);
11696
11697   gen_type_die (element_type, context_die);
11698 #endif
11699
11700   add_type_attribute (array_die, element_type, 0, 0, context_die);
11701
11702   if (get_AT (array_die, DW_AT_name))
11703     add_pubtype (type, array_die);
11704 }
11705
11706 static dw_loc_descr_ref
11707 descr_info_loc (tree val, tree base_decl)
11708 {
11709   HOST_WIDE_INT size;
11710   dw_loc_descr_ref loc, loc2;
11711   enum dwarf_location_atom op;
11712
11713   if (val == base_decl)
11714     return new_loc_descr (DW_OP_push_object_address, 0, 0);
11715
11716   switch (TREE_CODE (val))
11717     {
11718     case NOP_EXPR:
11719     case CONVERT_EXPR:
11720       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11721     case INTEGER_CST:
11722       if (host_integerp (val, 0))
11723         return int_loc_descriptor (tree_low_cst (val, 0));
11724       break;
11725     case INDIRECT_REF:
11726       size = int_size_in_bytes (TREE_TYPE (val));
11727       if (size < 0)
11728         break;
11729       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11730       if (!loc)
11731         break;
11732       if (size == DWARF2_ADDR_SIZE)
11733         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11734       else
11735         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11736       return loc;
11737     case POINTER_PLUS_EXPR:
11738     case PLUS_EXPR:
11739       if (host_integerp (TREE_OPERAND (val, 1), 1)
11740           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11741              < 16384)
11742         {
11743           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11744           if (!loc)
11745             break;
11746           add_loc_descr (&loc,
11747                          new_loc_descr (DW_OP_plus_uconst,
11748                                         tree_low_cst (TREE_OPERAND (val, 1),
11749                                                       1), 0));
11750         }
11751       else
11752         {
11753           op = DW_OP_plus;
11754         do_binop:
11755           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11756           if (!loc)
11757             break;
11758           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11759           if (!loc2)
11760             break;
11761           add_loc_descr (&loc, loc2);
11762           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11763         }
11764       return loc;
11765     case MINUS_EXPR:
11766       op = DW_OP_minus;
11767       goto do_binop;
11768     case MULT_EXPR:
11769       op = DW_OP_mul;
11770       goto do_binop;
11771     case EQ_EXPR:
11772       op = DW_OP_eq;
11773       goto do_binop;
11774     case NE_EXPR:
11775       op = DW_OP_ne;
11776       goto do_binop;
11777     default:
11778       break;
11779     }
11780   return NULL;
11781 }
11782
11783 static void
11784 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11785                       tree val, tree base_decl)
11786 {
11787   dw_loc_descr_ref loc;
11788
11789   if (host_integerp (val, 0))
11790     {
11791       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11792       return;
11793     }
11794
11795   loc = descr_info_loc (val, base_decl);
11796   if (!loc)
11797     return;
11798
11799   add_AT_loc (die, attr, loc);
11800 }
11801
11802 /* This routine generates DIE for array with hidden descriptor, details
11803    are filled into *info by a langhook.  */
11804
11805 static void
11806 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11807                           dw_die_ref context_die)
11808 {
11809   dw_die_ref scope_die = scope_die_for (type, context_die);
11810   dw_die_ref array_die;
11811   int dim;
11812
11813   array_die = new_die (DW_TAG_array_type, scope_die, type);
11814   add_name_attribute (array_die, type_tag (type));
11815   equate_type_number_to_die (type, array_die);
11816
11817   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11818   if (is_fortran ()
11819       && info->ndimensions >= 2)
11820     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11821
11822   if (info->data_location)
11823     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11824                           info->base_decl);
11825   if (info->associated)
11826     add_descr_info_field (array_die, DW_AT_associated, info->associated,
11827                           info->base_decl);
11828   if (info->allocated)
11829     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11830                           info->base_decl);
11831
11832   for (dim = 0; dim < info->ndimensions; dim++)
11833     {
11834       dw_die_ref subrange_die
11835         = new_die (DW_TAG_subrange_type, array_die, NULL);
11836
11837       if (info->dimen[dim].lower_bound)
11838         {
11839           /* If it is the default value, omit it.  */
11840           if ((is_c_family () || is_java ())
11841               && integer_zerop (info->dimen[dim].lower_bound))
11842             ;
11843           else if (is_fortran ()
11844                    && integer_onep (info->dimen[dim].lower_bound))
11845             ;
11846           else
11847             add_descr_info_field (subrange_die, DW_AT_lower_bound,
11848                                   info->dimen[dim].lower_bound,
11849                                   info->base_decl);
11850         }
11851       if (info->dimen[dim].upper_bound)
11852         add_descr_info_field (subrange_die, DW_AT_upper_bound,
11853                               info->dimen[dim].upper_bound,
11854                               info->base_decl);
11855       if (info->dimen[dim].stride)
11856         add_descr_info_field (subrange_die, DW_AT_byte_stride,
11857                               info->dimen[dim].stride,
11858                               info->base_decl);
11859     }
11860
11861   gen_type_die (info->element_type, context_die);
11862   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11863
11864   if (get_AT (array_die, DW_AT_name))
11865     add_pubtype (type, array_die);
11866 }
11867
11868 #if 0
11869 static void
11870 gen_entry_point_die (tree decl, dw_die_ref context_die)
11871 {
11872   tree origin = decl_ultimate_origin (decl);
11873   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11874
11875   if (origin != NULL)
11876     add_abstract_origin_attribute (decl_die, origin);
11877   else
11878     {
11879       add_name_and_src_coords_attributes (decl_die, decl);
11880       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11881                           0, 0, context_die);
11882     }
11883
11884   if (DECL_ABSTRACT (decl))
11885     equate_decl_number_to_die (decl, decl_die);
11886   else
11887     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11888 }
11889 #endif
11890
11891 /* Walk through the list of incomplete types again, trying once more to
11892    emit full debugging info for them.  */
11893
11894 static void
11895 retry_incomplete_types (void)
11896 {
11897   int i;
11898
11899   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11900     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11901 }
11902
11903 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11904
11905 static void
11906 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11907 {
11908   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11909
11910   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11911      be incomplete and such types are not marked.  */
11912   add_abstract_origin_attribute (type_die, type);
11913 }
11914
11915 /* Determine what tag to use for a record type.  */
11916
11917 static enum dwarf_tag
11918 record_type_tag (tree type)
11919 {
11920   if (! lang_hooks.types.classify_record)
11921     return DW_TAG_structure_type;
11922
11923   switch (lang_hooks.types.classify_record (type))
11924     {
11925     case RECORD_IS_STRUCT:
11926       return DW_TAG_structure_type;
11927
11928     case RECORD_IS_CLASS:
11929       return DW_TAG_class_type;
11930
11931     case RECORD_IS_INTERFACE:
11932       return DW_TAG_interface_type;
11933
11934     default:
11935       gcc_unreachable ();
11936     }
11937 }
11938
11939 /* Generate a DIE to represent an inlined instance of a structure type.  */
11940
11941 static void
11942 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11943 {
11944   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
11945
11946   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11947      be incomplete and such types are not marked.  */
11948   add_abstract_origin_attribute (type_die, type);
11949 }
11950
11951 /* Generate a DIE to represent an inlined instance of a union type.  */
11952
11953 static void
11954 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11955 {
11956   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11957
11958   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11959      be incomplete and such types are not marked.  */
11960   add_abstract_origin_attribute (type_die, type);
11961 }
11962
11963 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11964    include all of the information about the enumeration values also. Each
11965    enumerated type name/value is listed as a child of the enumerated type
11966    DIE.  */
11967
11968 static dw_die_ref
11969 gen_enumeration_type_die (tree type, dw_die_ref context_die)
11970 {
11971   dw_die_ref type_die = lookup_type_die (type);
11972
11973   if (type_die == NULL)
11974     {
11975       type_die = new_die (DW_TAG_enumeration_type,
11976                           scope_die_for (type, context_die), type);
11977       equate_type_number_to_die (type, type_die);
11978       add_name_attribute (type_die, type_tag (type));
11979     }
11980   else if (! TYPE_SIZE (type))
11981     return type_die;
11982   else
11983     remove_AT (type_die, DW_AT_declaration);
11984
11985   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
11986      given enum type is incomplete, do not generate the DW_AT_byte_size
11987      attribute or the DW_AT_element_list attribute.  */
11988   if (TYPE_SIZE (type))
11989     {
11990       tree link;
11991
11992       TREE_ASM_WRITTEN (type) = 1;
11993       add_byte_size_attribute (type_die, type);
11994       if (TYPE_STUB_DECL (type) != NULL_TREE)
11995         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
11996
11997       /* If the first reference to this type was as the return type of an
11998          inline function, then it may not have a parent.  Fix this now.  */
11999       if (type_die->die_parent == NULL)
12000         add_child_die (scope_die_for (type, context_die), type_die);
12001
12002       for (link = TYPE_VALUES (type);
12003            link != NULL; link = TREE_CHAIN (link))
12004         {
12005           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12006           tree value = TREE_VALUE (link);
12007
12008           add_name_attribute (enum_die,
12009                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12010
12011           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12012             /* DWARF2 does not provide a way of indicating whether or
12013                not enumeration constants are signed or unsigned.  GDB
12014                always assumes the values are signed, so we output all
12015                values as if they were signed.  That means that
12016                enumeration constants with very large unsigned values
12017                will appear to have negative values in the debugger.  */
12018             add_AT_int (enum_die, DW_AT_const_value,
12019                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12020         }
12021     }
12022   else
12023     add_AT_flag (type_die, DW_AT_declaration, 1);
12024
12025   if (get_AT (type_die, DW_AT_name))
12026     add_pubtype (type, type_die);
12027
12028   return type_die;
12029 }
12030
12031 /* Generate a DIE to represent either a real live formal parameter decl or to
12032    represent just the type of some formal parameter position in some function
12033    type.
12034
12035    Note that this routine is a bit unusual because its argument may be a
12036    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12037    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12038    node.  If it's the former then this function is being called to output a
12039    DIE to represent a formal parameter object (or some inlining thereof).  If
12040    it's the latter, then this function is only being called to output a
12041    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12042    argument type of some subprogram type.  */
12043
12044 static dw_die_ref
12045 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12046 {
12047   dw_die_ref parm_die
12048     = new_die (DW_TAG_formal_parameter, context_die, node);
12049   tree origin;
12050
12051   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12052     {
12053     case tcc_declaration:
12054       origin = decl_ultimate_origin (node);
12055       if (origin != NULL)
12056         add_abstract_origin_attribute (parm_die, origin);
12057       else
12058         {
12059           tree type = TREE_TYPE (node);
12060           add_name_and_src_coords_attributes (parm_die, node);
12061           if (DECL_BY_REFERENCE (node))
12062             type = TREE_TYPE (type);
12063           add_type_attribute (parm_die, type,
12064                               TREE_READONLY (node),
12065                               TREE_THIS_VOLATILE (node),
12066                               context_die);
12067           if (DECL_ARTIFICIAL (node))
12068             add_AT_flag (parm_die, DW_AT_artificial, 1);
12069         }
12070
12071       equate_decl_number_to_die (node, parm_die);
12072       if (! DECL_ABSTRACT (node))
12073         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12074
12075       break;
12076
12077     case tcc_type:
12078       /* We were called with some kind of a ..._TYPE node.  */
12079       add_type_attribute (parm_die, node, 0, 0, context_die);
12080       break;
12081
12082     default:
12083       gcc_unreachable ();
12084     }
12085
12086   return parm_die;
12087 }
12088
12089 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12090    at the end of an (ANSI prototyped) formal parameters list.  */
12091
12092 static void
12093 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12094 {
12095   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12096 }
12097
12098 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12099    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12100    parameters as specified in some function type specification (except for
12101    those which appear as part of a function *definition*).  */
12102
12103 static void
12104 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12105 {
12106   tree link;
12107   tree formal_type = NULL;
12108   tree first_parm_type;
12109   tree arg;
12110
12111   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12112     {
12113       arg = DECL_ARGUMENTS (function_or_method_type);
12114       function_or_method_type = TREE_TYPE (function_or_method_type);
12115     }
12116   else
12117     arg = NULL_TREE;
12118
12119   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12120
12121   /* Make our first pass over the list of formal parameter types and output a
12122      DW_TAG_formal_parameter DIE for each one.  */
12123   for (link = first_parm_type; link; )
12124     {
12125       dw_die_ref parm_die;
12126
12127       formal_type = TREE_VALUE (link);
12128       if (formal_type == void_type_node)
12129         break;
12130
12131       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12132       parm_die = gen_formal_parameter_die (formal_type, context_die);
12133       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12134            && link == first_parm_type)
12135           || (arg && DECL_ARTIFICIAL (arg)))
12136         add_AT_flag (parm_die, DW_AT_artificial, 1);
12137
12138       link = TREE_CHAIN (link);
12139       if (arg)
12140         arg = TREE_CHAIN (arg);
12141     }
12142
12143   /* If this function type has an ellipsis, add a
12144      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12145   if (formal_type != void_type_node)
12146     gen_unspecified_parameters_die (function_or_method_type, context_die);
12147
12148   /* Make our second (and final) pass over the list of formal parameter types
12149      and output DIEs to represent those types (as necessary).  */
12150   for (link = TYPE_ARG_TYPES (function_or_method_type);
12151        link && TREE_VALUE (link);
12152        link = TREE_CHAIN (link))
12153     gen_type_die (TREE_VALUE (link), context_die);
12154 }
12155
12156 /* We want to generate the DIE for TYPE so that we can generate the
12157    die for MEMBER, which has been defined; we will need to refer back
12158    to the member declaration nested within TYPE.  If we're trying to
12159    generate minimal debug info for TYPE, processing TYPE won't do the
12160    trick; we need to attach the member declaration by hand.  */
12161
12162 static void
12163 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12164 {
12165   gen_type_die (type, context_die);
12166
12167   /* If we're trying to avoid duplicate debug info, we may not have
12168      emitted the member decl for this function.  Emit it now.  */
12169   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12170       && ! lookup_decl_die (member))
12171     {
12172       dw_die_ref type_die;
12173       gcc_assert (!decl_ultimate_origin (member));
12174
12175       push_decl_scope (type);
12176       type_die = lookup_type_die (type);
12177       if (TREE_CODE (member) == FUNCTION_DECL)
12178         gen_subprogram_die (member, type_die);
12179       else if (TREE_CODE (member) == FIELD_DECL)
12180         {
12181           /* Ignore the nameless fields that are used to skip bits but handle
12182              C++ anonymous unions and structs.  */
12183           if (DECL_NAME (member) != NULL_TREE
12184               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12185               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12186             {
12187               gen_type_die (member_declared_type (member), type_die);
12188               gen_field_die (member, type_die);
12189             }
12190         }
12191       else
12192         gen_variable_die (member, type_die);
12193
12194       pop_decl_scope ();
12195     }
12196 }
12197
12198 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12199    may later generate inlined and/or out-of-line instances of.  */
12200
12201 static void
12202 dwarf2out_abstract_function (tree decl)
12203 {
12204   dw_die_ref old_die;
12205   tree save_fn;
12206   tree context;
12207   int was_abstract = DECL_ABSTRACT (decl);
12208
12209   /* Make sure we have the actual abstract inline, not a clone.  */
12210   decl = DECL_ORIGIN (decl);
12211
12212   old_die = lookup_decl_die (decl);
12213   if (old_die && get_AT (old_die, DW_AT_inline))
12214     /* We've already generated the abstract instance.  */
12215     return;
12216
12217   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12218      we don't get confused by DECL_ABSTRACT.  */
12219   if (debug_info_level > DINFO_LEVEL_TERSE)
12220     {
12221       context = decl_class_context (decl);
12222       if (context)
12223         gen_type_die_for_member
12224           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12225     }
12226
12227   /* Pretend we've just finished compiling this function.  */
12228   save_fn = current_function_decl;
12229   current_function_decl = decl;
12230   push_cfun (DECL_STRUCT_FUNCTION (decl));
12231
12232   set_decl_abstract_flags (decl, 1);
12233   dwarf2out_decl (decl);
12234   if (! was_abstract)
12235     set_decl_abstract_flags (decl, 0);
12236
12237   current_function_decl = save_fn;
12238   pop_cfun ();
12239 }
12240
12241 /* Helper function of premark_used_types() which gets called through
12242    htab_traverse_resize().
12243
12244    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12245    marked as unused by prune_unused_types.  */
12246 static int
12247 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12248 {
12249   tree type;
12250   dw_die_ref die;
12251
12252   type = *slot;
12253   die = lookup_type_die (type);
12254   if (die != NULL)
12255     die->die_perennial_p = 1;
12256   return 1;
12257 }
12258
12259 /* Mark all members of used_types_hash as perennial.  */
12260 static void
12261 premark_used_types (void)
12262 {
12263   if (cfun && cfun->used_types_hash)
12264     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12265 }
12266
12267 /* Generate a DIE to represent a declared function (either file-scope or
12268    block-local).  */
12269
12270 static void
12271 gen_subprogram_die (tree decl, dw_die_ref context_die)
12272 {
12273   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12274   tree origin = decl_ultimate_origin (decl);
12275   dw_die_ref subr_die;
12276   tree fn_arg_types;
12277   tree outer_scope;
12278   dw_die_ref old_die = lookup_decl_die (decl);
12279   int declaration = (current_function_decl != decl
12280                      || class_or_namespace_scope_p (context_die));
12281
12282   premark_used_types ();
12283
12284   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12285      started to generate the abstract instance of an inline, decided to output
12286      its containing class, and proceeded to emit the declaration of the inline
12287      from the member list for the class.  If so, DECLARATION takes priority;
12288      we'll get back to the abstract instance when done with the class.  */
12289
12290   /* The class-scope declaration DIE must be the primary DIE.  */
12291   if (origin && declaration && class_or_namespace_scope_p (context_die))
12292     {
12293       origin = NULL;
12294       gcc_assert (!old_die);
12295     }
12296
12297   /* Now that the C++ front end lazily declares artificial member fns, we
12298      might need to retrofit the declaration into its class.  */
12299   if (!declaration && !origin && !old_die
12300       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12301       && !class_or_namespace_scope_p (context_die)
12302       && debug_info_level > DINFO_LEVEL_TERSE)
12303     old_die = force_decl_die (decl);
12304
12305   if (origin != NULL)
12306     {
12307       gcc_assert (!declaration || local_scope_p (context_die));
12308
12309       /* Fixup die_parent for the abstract instance of a nested
12310          inline function.  */
12311       if (old_die && old_die->die_parent == NULL)
12312         add_child_die (context_die, old_die);
12313
12314       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12315       add_abstract_origin_attribute (subr_die, origin);
12316     }
12317   else if (old_die)
12318     {
12319       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12320       struct dwarf_file_data * file_index = lookup_filename (s.file);
12321
12322       if (!get_AT_flag (old_die, DW_AT_declaration)
12323           /* We can have a normal definition following an inline one in the
12324              case of redefinition of GNU C extern inlines.
12325              It seems reasonable to use AT_specification in this case.  */
12326           && !get_AT (old_die, DW_AT_inline))
12327         {
12328           /* Detect and ignore this case, where we are trying to output
12329              something we have already output.  */
12330           return;
12331         }
12332
12333       /* If the definition comes from the same place as the declaration,
12334          maybe use the old DIE.  We always want the DIE for this function
12335          that has the *_pc attributes to be under comp_unit_die so the
12336          debugger can find it.  We also need to do this for abstract
12337          instances of inlines, since the spec requires the out-of-line copy
12338          to have the same parent.  For local class methods, this doesn't
12339          apply; we just use the old DIE.  */
12340       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12341           && (DECL_ARTIFICIAL (decl)
12342               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12343                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12344                       == (unsigned) s.line))))
12345         {
12346           subr_die = old_die;
12347
12348           /* Clear out the declaration attribute and the formal parameters.
12349              Do not remove all children, because it is possible that this
12350              declaration die was forced using force_decl_die(). In such
12351              cases die that forced declaration die (e.g. TAG_imported_module)
12352              is one of the children that we do not want to remove.  */
12353           remove_AT (subr_die, DW_AT_declaration);
12354           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12355         }
12356       else
12357         {
12358           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12359           add_AT_specification (subr_die, old_die);
12360           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12361             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12362           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12363             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12364         }
12365     }
12366   else
12367     {
12368       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12369
12370       if (TREE_PUBLIC (decl))
12371         add_AT_flag (subr_die, DW_AT_external, 1);
12372
12373       add_name_and_src_coords_attributes (subr_die, decl);
12374       if (debug_info_level > DINFO_LEVEL_TERSE)
12375         {
12376           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12377           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12378                               0, 0, context_die);
12379         }
12380
12381       add_pure_or_virtual_attribute (subr_die, decl);
12382       if (DECL_ARTIFICIAL (decl))
12383         add_AT_flag (subr_die, DW_AT_artificial, 1);
12384
12385       if (TREE_PROTECTED (decl))
12386         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12387       else if (TREE_PRIVATE (decl))
12388         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12389     }
12390
12391   if (declaration)
12392     {
12393       if (!old_die || !get_AT (old_die, DW_AT_inline))
12394         {
12395           add_AT_flag (subr_die, DW_AT_declaration, 1);
12396
12397           /* The first time we see a member function, it is in the context of
12398              the class to which it belongs.  We make sure of this by emitting
12399              the class first.  The next time is the definition, which is
12400              handled above.  The two may come from the same source text.
12401
12402              Note that force_decl_die() forces function declaration die. It is
12403              later reused to represent definition.  */
12404           equate_decl_number_to_die (decl, subr_die);
12405         }
12406     }
12407   else if (DECL_ABSTRACT (decl))
12408     {
12409       if (DECL_DECLARED_INLINE_P (decl))
12410         {
12411           if (cgraph_function_possibly_inlined_p (decl))
12412             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12413           else
12414             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12415         }
12416       else
12417         {
12418           if (cgraph_function_possibly_inlined_p (decl))
12419             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12420           else
12421             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12422         }
12423
12424       if (DECL_DECLARED_INLINE_P (decl)
12425           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12426         add_AT_flag (subr_die, DW_AT_artificial, 1);
12427
12428       equate_decl_number_to_die (decl, subr_die);
12429     }
12430   else if (!DECL_EXTERNAL (decl))
12431     {
12432       HOST_WIDE_INT cfa_fb_offset;
12433
12434       if (!old_die || !get_AT (old_die, DW_AT_inline))
12435         equate_decl_number_to_die (decl, subr_die);
12436
12437       if (!flag_reorder_blocks_and_partition)
12438         {
12439           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12440                                        current_function_funcdef_no);
12441           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12442           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12443                                        current_function_funcdef_no);
12444           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12445
12446           add_pubname (decl, subr_die);
12447           add_arange (decl, subr_die);
12448         }
12449       else
12450         {  /* Do nothing for now; maybe need to duplicate die, one for
12451               hot section and ond for cold section, then use the hot/cold
12452               section begin/end labels to generate the aranges...  */
12453           /*
12454             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12455             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12456             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12457             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12458
12459             add_pubname (decl, subr_die);
12460             add_arange (decl, subr_die);
12461             add_arange (decl, subr_die);
12462            */
12463         }
12464
12465 #ifdef MIPS_DEBUGGING_INFO
12466       /* Add a reference to the FDE for this routine.  */
12467       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12468 #endif
12469
12470       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12471
12472       /* We define the "frame base" as the function's CFA.  This is more
12473          convenient for several reasons: (1) It's stable across the prologue
12474          and epilogue, which makes it better than just a frame pointer,
12475          (2) With dwarf3, there exists a one-byte encoding that allows us
12476          to reference the .debug_frame data by proxy, but failing that,
12477          (3) We can at least reuse the code inspection and interpretation
12478          code that determines the CFA position at various points in the
12479          function.  */
12480       /* ??? Use some command-line or configury switch to enable the use
12481          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
12482          consumers that understand it; fall back to "pure" dwarf2 and
12483          convert the CFA data into a location list.  */
12484       {
12485         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12486         if (list->dw_loc_next)
12487           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12488         else
12489           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12490       }
12491
12492       /* Compute a displacement from the "steady-state frame pointer" to
12493          the CFA.  The former is what all stack slots and argument slots
12494          will reference in the rtl; the later is what we've told the
12495          debugger about.  We'll need to adjust all frame_base references
12496          by this displacement.  */
12497       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12498
12499       if (cfun->static_chain_decl)
12500         add_AT_location_description (subr_die, DW_AT_static_link,
12501                  loc_descriptor_from_tree (cfun->static_chain_decl));
12502     }
12503
12504   /* Now output descriptions of the arguments for this function. This gets
12505      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12506      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12507      `...' at the end of the formal parameter list.  In order to find out if
12508      there was a trailing ellipsis or not, we must instead look at the type
12509      associated with the FUNCTION_DECL.  This will be a node of type
12510      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12511      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12512      an ellipsis at the end.  */
12513
12514   /* In the case where we are describing a mere function declaration, all we
12515      need to do here (and all we *can* do here) is to describe the *types* of
12516      its formal parameters.  */
12517   if (debug_info_level <= DINFO_LEVEL_TERSE)
12518     ;
12519   else if (declaration)
12520     gen_formal_types_die (decl, subr_die);
12521   else
12522     {
12523       /* Generate DIEs to represent all known formal parameters.  */
12524       tree arg_decls = DECL_ARGUMENTS (decl);
12525       tree parm;
12526
12527       /* When generating DIEs, generate the unspecified_parameters DIE
12528          instead if we come across the arg "__builtin_va_alist" */
12529       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12530         if (TREE_CODE (parm) == PARM_DECL)
12531           {
12532             if (DECL_NAME (parm)
12533                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12534                             "__builtin_va_alist"))
12535               gen_unspecified_parameters_die (parm, subr_die);
12536             else
12537               gen_decl_die (parm, subr_die);
12538           }
12539
12540       /* Decide whether we need an unspecified_parameters DIE at the end.
12541          There are 2 more cases to do this for: 1) the ansi ... declaration -
12542          this is detectable when the end of the arg list is not a
12543          void_type_node 2) an unprototyped function declaration (not a
12544          definition).  This just means that we have no info about the
12545          parameters at all.  */
12546       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12547       if (fn_arg_types != NULL)
12548         {
12549           /* This is the prototyped case, check for....  */
12550           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12551             gen_unspecified_parameters_die (decl, subr_die);
12552         }
12553       else if (DECL_INITIAL (decl) == NULL_TREE)
12554         gen_unspecified_parameters_die (decl, subr_die);
12555     }
12556
12557   /* Output Dwarf info for all of the stuff within the body of the function
12558      (if it has one - it may be just a declaration).  */
12559   outer_scope = DECL_INITIAL (decl);
12560
12561   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12562      a function.  This BLOCK actually represents the outermost binding contour
12563      for the function, i.e. the contour in which the function's formal
12564      parameters and labels get declared. Curiously, it appears that the front
12565      end doesn't actually put the PARM_DECL nodes for the current function onto
12566      the BLOCK_VARS list for this outer scope, but are strung off of the
12567      DECL_ARGUMENTS list for the function instead.
12568
12569      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12570      the LABEL_DECL nodes for the function however, and we output DWARF info
12571      for those in decls_for_scope.  Just within the `outer_scope' there will be
12572      a BLOCK node representing the function's outermost pair of curly braces,
12573      and any blocks used for the base and member initializers of a C++
12574      constructor function.  */
12575   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12576     {
12577       /* Emit a DW_TAG_variable DIE for a named return value.  */
12578       if (DECL_NAME (DECL_RESULT (decl)))
12579         gen_decl_die (DECL_RESULT (decl), subr_die);
12580
12581       current_function_has_inlines = 0;
12582       decls_for_scope (outer_scope, subr_die, 0);
12583
12584 #if 0 && defined (MIPS_DEBUGGING_INFO)
12585       if (current_function_has_inlines)
12586         {
12587           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12588           if (! comp_unit_has_inlines)
12589             {
12590               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12591               comp_unit_has_inlines = 1;
12592             }
12593         }
12594 #endif
12595     }
12596   /* Add the calling convention attribute if requested.  */
12597   add_calling_convention_attribute (subr_die, decl);
12598
12599 }
12600
12601 /* Generate a DIE to represent a declared data object.  */
12602
12603 static void
12604 gen_variable_die (tree decl, dw_die_ref context_die)
12605 {
12606   tree origin = decl_ultimate_origin (decl);
12607   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12608
12609   dw_die_ref old_die = lookup_decl_die (decl);
12610   int declaration = (DECL_EXTERNAL (decl)
12611                      /* If DECL is COMDAT and has not actually been
12612                         emitted, we cannot take its address; there
12613                         might end up being no definition anywhere in
12614                         the program.  For example, consider the C++
12615                         test case:
12616
12617                           template <class T>
12618                           struct S { static const int i = 7; };
12619
12620                           template <class T>
12621                           const int S<T>::i;
12622
12623                           int f() { return S<int>::i; }
12624
12625                         Here, S<int>::i is not DECL_EXTERNAL, but no
12626                         definition is required, so the compiler will
12627                         not emit a definition.  */
12628                      || (TREE_CODE (decl) == VAR_DECL
12629                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12630                      || class_or_namespace_scope_p (context_die));
12631
12632   if (origin != NULL)
12633     add_abstract_origin_attribute (var_die, origin);
12634
12635   /* Loop unrolling can create multiple blocks that refer to the same
12636      static variable, so we must test for the DW_AT_declaration flag.
12637
12638      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12639      copy decls and set the DECL_ABSTRACT flag on them instead of
12640      sharing them.
12641
12642      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12643
12644      ??? The declare_in_namespace support causes us to get two DIEs for one
12645      variable, both of which are declarations.  We want to avoid considering
12646      one to be a specification, so we must test that this DIE is not a
12647      declaration.  */
12648   else if (old_die && TREE_STATIC (decl) && ! declaration
12649            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12650     {
12651       /* This is a definition of a C++ class level static.  */
12652       add_AT_specification (var_die, old_die);
12653       if (DECL_NAME (decl))
12654         {
12655           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12656           struct dwarf_file_data * file_index = lookup_filename (s.file);
12657
12658           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12659             add_AT_file (var_die, DW_AT_decl_file, file_index);
12660
12661           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12662             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12663         }
12664     }
12665   else
12666     {
12667       tree type = TREE_TYPE (decl);
12668       if ((TREE_CODE (decl) == PARM_DECL
12669            || TREE_CODE (decl) == RESULT_DECL)
12670           && DECL_BY_REFERENCE (decl))
12671         type = TREE_TYPE (type);
12672
12673       add_name_and_src_coords_attributes (var_die, decl);
12674       add_type_attribute (var_die, type, TREE_READONLY (decl),
12675                           TREE_THIS_VOLATILE (decl), context_die);
12676
12677       if (TREE_PUBLIC (decl))
12678         add_AT_flag (var_die, DW_AT_external, 1);
12679
12680       if (DECL_ARTIFICIAL (decl))
12681         add_AT_flag (var_die, DW_AT_artificial, 1);
12682
12683       if (TREE_PROTECTED (decl))
12684         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12685       else if (TREE_PRIVATE (decl))
12686         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12687     }
12688
12689   if (declaration)
12690     add_AT_flag (var_die, DW_AT_declaration, 1);
12691
12692   if (DECL_ABSTRACT (decl) || declaration)
12693     equate_decl_number_to_die (decl, var_die);
12694
12695   if (! declaration && ! DECL_ABSTRACT (decl))
12696     {
12697       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12698       add_pubname (decl, var_die);
12699     }
12700   else
12701     tree_add_const_value_attribute (var_die, decl);
12702 }
12703
12704 /* Generate a DIE to represent a label identifier.  */
12705
12706 static void
12707 gen_label_die (tree decl, dw_die_ref context_die)
12708 {
12709   tree origin = decl_ultimate_origin (decl);
12710   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12711   rtx insn;
12712   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12713
12714   if (origin != NULL)
12715     add_abstract_origin_attribute (lbl_die, origin);
12716   else
12717     add_name_and_src_coords_attributes (lbl_die, decl);
12718
12719   if (DECL_ABSTRACT (decl))
12720     equate_decl_number_to_die (decl, lbl_die);
12721   else
12722     {
12723       insn = DECL_RTL_IF_SET (decl);
12724
12725       /* Deleted labels are programmer specified labels which have been
12726          eliminated because of various optimizations.  We still emit them
12727          here so that it is possible to put breakpoints on them.  */
12728       if (insn
12729           && (LABEL_P (insn)
12730               || ((NOTE_P (insn)
12731                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12732         {
12733           /* When optimization is enabled (via -O) some parts of the compiler
12734              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12735              represent source-level labels which were explicitly declared by
12736              the user.  This really shouldn't be happening though, so catch
12737              it if it ever does happen.  */
12738           gcc_assert (!INSN_DELETED_P (insn));
12739
12740           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12741           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12742         }
12743     }
12744 }
12745
12746 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12747    attributes to the DIE for a block STMT, to describe where the inlined
12748    function was called from.  This is similar to add_src_coords_attributes.  */
12749
12750 static inline void
12751 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12752 {
12753   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12754
12755   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12756   add_AT_unsigned (die, DW_AT_call_line, s.line);
12757 }
12758
12759
12760 /* If STMT's abstract origin is a function declaration and STMT's
12761    first subblock's abstract origin is the function's outermost block,
12762    then we're looking at the main entry point.  */
12763 static bool
12764 is_inlined_entry_point (const_tree stmt)
12765 {
12766   tree decl, block;
12767
12768   if (!stmt || TREE_CODE (stmt) != BLOCK)
12769     return false;
12770
12771   decl = block_ultimate_origin (stmt);
12772
12773   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12774     return false;
12775
12776   block = BLOCK_SUBBLOCKS (stmt);
12777
12778   if (block)
12779     {
12780       if (TREE_CODE (block) != BLOCK)
12781         return false;
12782
12783       block = block_ultimate_origin (block);
12784     }
12785
12786   return block == DECL_INITIAL (decl);
12787 }
12788
12789 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12790    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12791
12792 static inline void
12793 add_high_low_attributes (tree stmt, dw_die_ref die)
12794 {
12795   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12796
12797   if (BLOCK_FRAGMENT_CHAIN (stmt))
12798     {
12799       tree chain;
12800
12801       if (is_inlined_entry_point (stmt))
12802         {
12803           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12804                                        BLOCK_NUMBER (stmt));
12805           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12806         }
12807
12808       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12809
12810       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12811       do
12812         {
12813           add_ranges (chain);
12814           chain = BLOCK_FRAGMENT_CHAIN (chain);
12815         }
12816       while (chain);
12817       add_ranges (NULL);
12818     }
12819   else
12820     {
12821       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12822                                    BLOCK_NUMBER (stmt));
12823       add_AT_lbl_id (die, DW_AT_low_pc, label);
12824       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12825                                    BLOCK_NUMBER (stmt));
12826       add_AT_lbl_id (die, DW_AT_high_pc, label);
12827     }
12828 }
12829
12830 /* Generate a DIE for a lexical block.  */
12831
12832 static void
12833 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12834 {
12835   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12836
12837   if (! BLOCK_ABSTRACT (stmt))
12838     add_high_low_attributes (stmt, stmt_die);
12839
12840   decls_for_scope (stmt, stmt_die, depth);
12841 }
12842
12843 /* Generate a DIE for an inlined subprogram.  */
12844
12845 static void
12846 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12847 {
12848   tree decl = block_ultimate_origin (stmt);
12849
12850   /* Emit info for the abstract instance first, if we haven't yet.  We
12851      must emit this even if the block is abstract, otherwise when we
12852      emit the block below (or elsewhere), we may end up trying to emit
12853      a die whose origin die hasn't been emitted, and crashing.  */
12854   dwarf2out_abstract_function (decl);
12855
12856   if (! BLOCK_ABSTRACT (stmt))
12857     {
12858       dw_die_ref subr_die
12859         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12860
12861       add_abstract_origin_attribute (subr_die, decl);
12862       add_high_low_attributes (stmt, subr_die);
12863       add_call_src_coords_attributes (stmt, subr_die);
12864
12865       decls_for_scope (stmt, subr_die, depth);
12866       current_function_has_inlines = 1;
12867     }
12868   else
12869     /* We may get here if we're the outer block of function A that was
12870        inlined into function B that was inlined into function C.  When
12871        generating debugging info for C, dwarf2out_abstract_function(B)
12872        would mark all inlined blocks as abstract, including this one.
12873        So, we wouldn't (and shouldn't) expect labels to be generated
12874        for this one.  Instead, just emit debugging info for
12875        declarations within the block.  This is particularly important
12876        in the case of initializers of arguments passed from B to us:
12877        if they're statement expressions containing declarations, we
12878        wouldn't generate dies for their abstract variables, and then,
12879        when generating dies for the real variables, we'd die (pun
12880        intended :-)  */
12881     gen_lexical_block_die (stmt, context_die, depth);
12882 }
12883
12884 /* Generate a DIE for a field in a record, or structure.  */
12885
12886 static void
12887 gen_field_die (tree decl, dw_die_ref context_die)
12888 {
12889   dw_die_ref decl_die;
12890
12891   if (TREE_TYPE (decl) == error_mark_node)
12892     return;
12893
12894   decl_die = new_die (DW_TAG_member, context_die, decl);
12895   add_name_and_src_coords_attributes (decl_die, decl);
12896   add_type_attribute (decl_die, member_declared_type (decl),
12897                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12898                       context_die);
12899
12900   if (DECL_BIT_FIELD_TYPE (decl))
12901     {
12902       add_byte_size_attribute (decl_die, decl);
12903       add_bit_size_attribute (decl_die, decl);
12904       add_bit_offset_attribute (decl_die, decl);
12905     }
12906
12907   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12908     add_data_member_location_attribute (decl_die, decl);
12909
12910   if (DECL_ARTIFICIAL (decl))
12911     add_AT_flag (decl_die, DW_AT_artificial, 1);
12912
12913   if (TREE_PROTECTED (decl))
12914     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12915   else if (TREE_PRIVATE (decl))
12916     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12917
12918   /* Equate decl number to die, so that we can look up this decl later on.  */
12919   equate_decl_number_to_die (decl, decl_die);
12920 }
12921
12922 #if 0
12923 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12924    Use modified_type_die instead.
12925    We keep this code here just in case these types of DIEs may be needed to
12926    represent certain things in other languages (e.g. Pascal) someday.  */
12927
12928 static void
12929 gen_pointer_type_die (tree type, dw_die_ref context_die)
12930 {
12931   dw_die_ref ptr_die
12932     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12933
12934   equate_type_number_to_die (type, ptr_die);
12935   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12936   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12937 }
12938
12939 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12940    Use modified_type_die instead.
12941    We keep this code here just in case these types of DIEs may be needed to
12942    represent certain things in other languages (e.g. Pascal) someday.  */
12943
12944 static void
12945 gen_reference_type_die (tree type, dw_die_ref context_die)
12946 {
12947   dw_die_ref ref_die
12948     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12949
12950   equate_type_number_to_die (type, ref_die);
12951   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12952   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12953 }
12954 #endif
12955
12956 /* Generate a DIE for a pointer to a member type.  */
12957
12958 static void
12959 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12960 {
12961   dw_die_ref ptr_die
12962     = new_die (DW_TAG_ptr_to_member_type,
12963                scope_die_for (type, context_die), type);
12964
12965   equate_type_number_to_die (type, ptr_die);
12966   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12967                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12968   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12969 }
12970
12971 /* Generate the DIE for the compilation unit.  */
12972
12973 static dw_die_ref
12974 gen_compile_unit_die (const char *filename)
12975 {
12976   dw_die_ref die;
12977   char producer[250];
12978   const char *language_string = lang_hooks.name;
12979   int language;
12980
12981   die = new_die (DW_TAG_compile_unit, NULL, NULL);
12982
12983   if (filename)
12984     {
12985       add_name_attribute (die, filename);
12986       /* Don't add cwd for <built-in>.  */
12987       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
12988         add_comp_dir_attribute (die);
12989     }
12990
12991   sprintf (producer, "%s %s", language_string, version_string);
12992
12993 #ifdef MIPS_DEBUGGING_INFO
12994   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
12995      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
12996      not appear in the producer string, the debugger reaches the conclusion
12997      that the object file is stripped and has no debugging information.
12998      To get the MIPS/SGI debugger to believe that there is debugging
12999      information in the object file, we add a -g to the producer string.  */
13000   if (debug_info_level > DINFO_LEVEL_TERSE)
13001     strcat (producer, " -g");
13002 #endif
13003
13004   add_AT_string (die, DW_AT_producer, producer);
13005
13006   if (strcmp (language_string, "GNU C++") == 0)
13007     language = DW_LANG_C_plus_plus;
13008   else if (strcmp (language_string, "GNU Ada") == 0)
13009     language = DW_LANG_Ada95;
13010   else if (strcmp (language_string, "GNU F77") == 0)
13011     language = DW_LANG_Fortran77;
13012   else if (strcmp (language_string, "GNU F95") == 0)
13013     language = DW_LANG_Fortran95;
13014   else if (strcmp (language_string, "GNU Pascal") == 0)
13015     language = DW_LANG_Pascal83;
13016   else if (strcmp (language_string, "GNU Java") == 0)
13017     language = DW_LANG_Java;
13018   else if (strcmp (language_string, "GNU Objective-C") == 0)
13019     language = DW_LANG_ObjC;
13020   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13021     language = DW_LANG_ObjC_plus_plus;
13022   else
13023     language = DW_LANG_C89;
13024
13025   add_AT_unsigned (die, DW_AT_language, language);
13026   return die;
13027 }
13028
13029 /* Generate the DIE for a base class.  */
13030
13031 static void
13032 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13033 {
13034   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13035
13036   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13037   add_data_member_location_attribute (die, binfo);
13038
13039   if (BINFO_VIRTUAL_P (binfo))
13040     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13041
13042   if (access == access_public_node)
13043     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13044   else if (access == access_protected_node)
13045     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13046 }
13047
13048 /* Generate a DIE for a class member.  */
13049
13050 static void
13051 gen_member_die (tree type, dw_die_ref context_die)
13052 {
13053   tree member;
13054   tree binfo = TYPE_BINFO (type);
13055   dw_die_ref child;
13056
13057   /* If this is not an incomplete type, output descriptions of each of its
13058      members. Note that as we output the DIEs necessary to represent the
13059      members of this record or union type, we will also be trying to output
13060      DIEs to represent the *types* of those members. However the `type'
13061      function (above) will specifically avoid generating type DIEs for member
13062      types *within* the list of member DIEs for this (containing) type except
13063      for those types (of members) which are explicitly marked as also being
13064      members of this (containing) type themselves.  The g++ front- end can
13065      force any given type to be treated as a member of some other (containing)
13066      type by setting the TYPE_CONTEXT of the given (member) type to point to
13067      the TREE node representing the appropriate (containing) type.  */
13068
13069   /* First output info about the base classes.  */
13070   if (binfo)
13071     {
13072       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13073       int i;
13074       tree base;
13075
13076       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13077         gen_inheritance_die (base,
13078                              (accesses ? VEC_index (tree, accesses, i)
13079                               : access_public_node), context_die);
13080     }
13081
13082   /* Now output info about the data members and type members.  */
13083   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13084     {
13085       /* If we thought we were generating minimal debug info for TYPE
13086          and then changed our minds, some of the member declarations
13087          may have already been defined.  Don't define them again, but
13088          do put them in the right order.  */
13089
13090       child = lookup_decl_die (member);
13091       if (child)
13092         splice_child_die (context_die, child);
13093       else
13094         gen_decl_die (member, context_die);
13095     }
13096
13097   /* Now output info about the function members (if any).  */
13098   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13099     {
13100       /* Don't include clones in the member list.  */
13101       if (DECL_ABSTRACT_ORIGIN (member))
13102         continue;
13103
13104       child = lookup_decl_die (member);
13105       if (child)
13106         splice_child_die (context_die, child);
13107       else
13108         gen_decl_die (member, context_die);
13109     }
13110 }
13111
13112 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13113    is set, we pretend that the type was never defined, so we only get the
13114    member DIEs needed by later specification DIEs.  */
13115
13116 static void
13117 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13118                                 enum debug_info_usage usage)
13119 {
13120   dw_die_ref type_die = lookup_type_die (type);
13121   dw_die_ref scope_die = 0;
13122   int nested = 0;
13123   int complete = (TYPE_SIZE (type)
13124                   && (! TYPE_STUB_DECL (type)
13125                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13126   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13127   complete = complete && should_emit_struct_debug (type, usage);
13128
13129   if (type_die && ! complete)
13130     return;
13131
13132   if (TYPE_CONTEXT (type) != NULL_TREE
13133       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13134           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13135     nested = 1;
13136
13137   scope_die = scope_die_for (type, context_die);
13138
13139   if (! type_die || (nested && scope_die == comp_unit_die))
13140     /* First occurrence of type or toplevel definition of nested class.  */
13141     {
13142       dw_die_ref old_die = type_die;
13143
13144       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13145                           ? record_type_tag (type) : DW_TAG_union_type,
13146                           scope_die, type);
13147       equate_type_number_to_die (type, type_die);
13148       if (old_die)
13149         add_AT_specification (type_die, old_die);
13150       else
13151         add_name_attribute (type_die, type_tag (type));
13152     }
13153   else
13154     remove_AT (type_die, DW_AT_declaration);
13155
13156   /* If this type has been completed, then give it a byte_size attribute and
13157      then give a list of members.  */
13158   if (complete && !ns_decl)
13159     {
13160       /* Prevent infinite recursion in cases where the type of some member of
13161          this type is expressed in terms of this type itself.  */
13162       TREE_ASM_WRITTEN (type) = 1;
13163       add_byte_size_attribute (type_die, type);
13164       if (TYPE_STUB_DECL (type) != NULL_TREE)
13165         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13166
13167       /* If the first reference to this type was as the return type of an
13168          inline function, then it may not have a parent.  Fix this now.  */
13169       if (type_die->die_parent == NULL)
13170         add_child_die (scope_die, type_die);
13171
13172       push_decl_scope (type);
13173       gen_member_die (type, type_die);
13174       pop_decl_scope ();
13175
13176       /* GNU extension: Record what type our vtable lives in.  */
13177       if (TYPE_VFIELD (type))
13178         {
13179           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13180
13181           gen_type_die (vtype, context_die);
13182           add_AT_die_ref (type_die, DW_AT_containing_type,
13183                           lookup_type_die (vtype));
13184         }
13185     }
13186   else
13187     {
13188       add_AT_flag (type_die, DW_AT_declaration, 1);
13189
13190       /* We don't need to do this for function-local types.  */
13191       if (TYPE_STUB_DECL (type)
13192           && ! decl_function_context (TYPE_STUB_DECL (type)))
13193         VEC_safe_push (tree, gc, incomplete_types, type);
13194     }
13195
13196   if (get_AT (type_die, DW_AT_name))
13197     add_pubtype (type, type_die);
13198 }
13199
13200 /* Generate a DIE for a subroutine _type_.  */
13201
13202 static void
13203 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13204 {
13205   tree return_type = TREE_TYPE (type);
13206   dw_die_ref subr_die
13207     = new_die (DW_TAG_subroutine_type,
13208                scope_die_for (type, context_die), type);
13209
13210   equate_type_number_to_die (type, subr_die);
13211   add_prototyped_attribute (subr_die, type);
13212   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13213   gen_formal_types_die (type, subr_die);
13214
13215   if (get_AT (subr_die, DW_AT_name))
13216     add_pubtype (type, subr_die);
13217 }
13218
13219 /* Generate a DIE for a type definition.  */
13220
13221 static void
13222 gen_typedef_die (tree decl, dw_die_ref context_die)
13223 {
13224   dw_die_ref type_die;
13225   tree origin;
13226
13227   if (TREE_ASM_WRITTEN (decl))
13228     return;
13229
13230   TREE_ASM_WRITTEN (decl) = 1;
13231   type_die = new_die (DW_TAG_typedef, context_die, decl);
13232   origin = decl_ultimate_origin (decl);
13233   if (origin != NULL)
13234     add_abstract_origin_attribute (type_die, origin);
13235   else
13236     {
13237       tree type;
13238
13239       add_name_and_src_coords_attributes (type_die, decl);
13240       if (DECL_ORIGINAL_TYPE (decl))
13241         {
13242           type = DECL_ORIGINAL_TYPE (decl);
13243
13244           gcc_assert (type != TREE_TYPE (decl));
13245           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13246         }
13247       else
13248         type = TREE_TYPE (decl);
13249
13250       add_type_attribute (type_die, type, TREE_READONLY (decl),
13251                           TREE_THIS_VOLATILE (decl), context_die);
13252     }
13253
13254   if (DECL_ABSTRACT (decl))
13255     equate_decl_number_to_die (decl, type_die);
13256
13257   if (get_AT (type_die, DW_AT_name))
13258     add_pubtype (decl, type_die);
13259 }
13260
13261 /* Generate a type description DIE.  */
13262
13263 static void
13264 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13265                                 enum debug_info_usage usage)
13266 {
13267   int need_pop;
13268   struct array_descr_info info;
13269
13270   if (type == NULL_TREE || type == error_mark_node)
13271     return;
13272
13273   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13274       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13275     {
13276       if (TREE_ASM_WRITTEN (type))
13277         return;
13278
13279       /* Prevent broken recursion; we can't hand off to the same type.  */
13280       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13281
13282       TREE_ASM_WRITTEN (type) = 1;
13283       gen_decl_die (TYPE_NAME (type), context_die);
13284       return;
13285     }
13286
13287   /* If this is an array type with hidden descriptor, handle it first.  */
13288   if (!TREE_ASM_WRITTEN (type)
13289       && lang_hooks.types.get_array_descr_info
13290       && lang_hooks.types.get_array_descr_info (type, &info))
13291     {
13292       gen_descr_array_type_die (type, &info, context_die);
13293       TREE_ASM_WRITTEN (type) = 1;
13294       return;
13295     }
13296
13297   /* We are going to output a DIE to represent the unqualified version
13298      of this type (i.e. without any const or volatile qualifiers) so
13299      get the main variant (i.e. the unqualified version) of this type
13300      now.  (Vectors are special because the debugging info is in the
13301      cloned type itself).  */
13302   if (TREE_CODE (type) != VECTOR_TYPE)
13303     type = type_main_variant (type);
13304
13305   if (TREE_ASM_WRITTEN (type))
13306     return;
13307
13308   switch (TREE_CODE (type))
13309     {
13310     case ERROR_MARK:
13311       break;
13312
13313     case POINTER_TYPE:
13314     case REFERENCE_TYPE:
13315       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13316          ensures that the gen_type_die recursion will terminate even if the
13317          type is recursive.  Recursive types are possible in Ada.  */
13318       /* ??? We could perhaps do this for all types before the switch
13319          statement.  */
13320       TREE_ASM_WRITTEN (type) = 1;
13321
13322       /* For these types, all that is required is that we output a DIE (or a
13323          set of DIEs) to represent the "basis" type.  */
13324       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13325                                 DINFO_USAGE_IND_USE);
13326       break;
13327
13328     case OFFSET_TYPE:
13329       /* This code is used for C++ pointer-to-data-member types.
13330          Output a description of the relevant class type.  */
13331       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13332                                         DINFO_USAGE_IND_USE);
13333
13334       /* Output a description of the type of the object pointed to.  */
13335       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13336                                         DINFO_USAGE_IND_USE);
13337
13338       /* Now output a DIE to represent this pointer-to-data-member type
13339          itself.  */
13340       gen_ptr_to_mbr_type_die (type, context_die);
13341       break;
13342
13343     case FUNCTION_TYPE:
13344       /* Force out return type (in case it wasn't forced out already).  */
13345       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13346                                         DINFO_USAGE_DIR_USE);
13347       gen_subroutine_type_die (type, context_die);
13348       break;
13349
13350     case METHOD_TYPE:
13351       /* Force out return type (in case it wasn't forced out already).  */
13352       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13353                                         DINFO_USAGE_DIR_USE);
13354       gen_subroutine_type_die (type, context_die);
13355       break;
13356
13357     case ARRAY_TYPE:
13358       gen_array_type_die (type, context_die);
13359       break;
13360
13361     case VECTOR_TYPE:
13362       gen_array_type_die (type, context_die);
13363       break;
13364
13365     case ENUMERAL_TYPE:
13366     case RECORD_TYPE:
13367     case UNION_TYPE:
13368     case QUAL_UNION_TYPE:
13369       /* If this is a nested type whose containing class hasn't been written
13370          out yet, writing it out will cover this one, too.  This does not apply
13371          to instantiations of member class templates; they need to be added to
13372          the containing class as they are generated.  FIXME: This hurts the
13373          idea of combining type decls from multiple TUs, since we can't predict
13374          what set of template instantiations we'll get.  */
13375       if (TYPE_CONTEXT (type)
13376           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13377           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13378         {
13379           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13380
13381           if (TREE_ASM_WRITTEN (type))
13382             return;
13383
13384           /* If that failed, attach ourselves to the stub.  */
13385           push_decl_scope (TYPE_CONTEXT (type));
13386           context_die = lookup_type_die (TYPE_CONTEXT (type));
13387           need_pop = 1;
13388         }
13389       else
13390         {
13391           declare_in_namespace (type, context_die);
13392           need_pop = 0;
13393         }
13394
13395       if (TREE_CODE (type) == ENUMERAL_TYPE)
13396         {
13397           /* This might have been written out by the call to
13398              declare_in_namespace.  */
13399           if (!TREE_ASM_WRITTEN (type))
13400             gen_enumeration_type_die (type, context_die);
13401         }
13402       else
13403         gen_struct_or_union_type_die (type, context_die, usage);
13404
13405       if (need_pop)
13406         pop_decl_scope ();
13407
13408       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13409          it up if it is ever completed.  gen_*_type_die will set it for us
13410          when appropriate.  */
13411       return;
13412
13413     case VOID_TYPE:
13414     case INTEGER_TYPE:
13415     case REAL_TYPE:
13416     case FIXED_POINT_TYPE:
13417     case COMPLEX_TYPE:
13418     case BOOLEAN_TYPE:
13419       /* No DIEs needed for fundamental types.  */
13420       break;
13421
13422     case LANG_TYPE:
13423       /* No Dwarf representation currently defined.  */
13424       break;
13425
13426     default:
13427       gcc_unreachable ();
13428     }
13429
13430   TREE_ASM_WRITTEN (type) = 1;
13431 }
13432
13433 static void
13434 gen_type_die (tree type, dw_die_ref context_die)
13435 {
13436   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13437 }
13438
13439 /* Generate a DIE for a tagged type instantiation.  */
13440
13441 static void
13442 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13443 {
13444   if (type == NULL_TREE || type == error_mark_node)
13445     return;
13446
13447   /* We are going to output a DIE to represent the unqualified version of
13448      this type (i.e. without any const or volatile qualifiers) so make sure
13449      that we have the main variant (i.e. the unqualified version) of this
13450      type now.  */
13451   gcc_assert (type == type_main_variant (type));
13452
13453   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13454      an instance of an unresolved type.  */
13455
13456   switch (TREE_CODE (type))
13457     {
13458     case ERROR_MARK:
13459       break;
13460
13461     case ENUMERAL_TYPE:
13462       gen_inlined_enumeration_type_die (type, context_die);
13463       break;
13464
13465     case RECORD_TYPE:
13466       gen_inlined_structure_type_die (type, context_die);
13467       break;
13468
13469     case UNION_TYPE:
13470     case QUAL_UNION_TYPE:
13471       gen_inlined_union_type_die (type, context_die);
13472       break;
13473
13474     default:
13475       gcc_unreachable ();
13476     }
13477 }
13478
13479 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13480    things which are local to the given block.  */
13481
13482 static void
13483 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13484 {
13485   int must_output_die = 0;
13486   tree origin;
13487   tree decl;
13488   enum tree_code origin_code;
13489
13490   /* Ignore blocks that are NULL.  */
13491   if (stmt == NULL_TREE)
13492     return;
13493
13494   /* If the block is one fragment of a non-contiguous block, do not
13495      process the variables, since they will have been done by the
13496      origin block.  Do process subblocks.  */
13497   if (BLOCK_FRAGMENT_ORIGIN (stmt))
13498     {
13499       tree sub;
13500
13501       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13502         gen_block_die (sub, context_die, depth + 1);
13503
13504       return;
13505     }
13506
13507   /* Determine the "ultimate origin" of this block.  This block may be an
13508      inlined instance of an inlined instance of inline function, so we have
13509      to trace all of the way back through the origin chain to find out what
13510      sort of node actually served as the original seed for the creation of
13511      the current block.  */
13512   origin = block_ultimate_origin (stmt);
13513   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13514
13515   /* Determine if we need to output any Dwarf DIEs at all to represent this
13516      block.  */
13517   if (origin_code == FUNCTION_DECL)
13518     /* The outer scopes for inlinings *must* always be represented.  We
13519        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13520     must_output_die = 1;
13521   else
13522     {
13523       /* In the case where the current block represents an inlining of the
13524          "body block" of an inline function, we must *NOT* output any DIE for
13525          this block because we have already output a DIE to represent the whole
13526          inlined function scope and the "body block" of any function doesn't
13527          really represent a different scope according to ANSI C rules.  So we
13528          check here to make sure that this block does not represent a "body
13529          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13530       if (! is_body_block (origin ? origin : stmt))
13531         {
13532           /* Determine if this block directly contains any "significant"
13533              local declarations which we will need to output DIEs for.  */
13534           if (debug_info_level > DINFO_LEVEL_TERSE)
13535             /* We are not in terse mode so *any* local declaration counts
13536                as being a "significant" one.  */
13537             must_output_die = (BLOCK_VARS (stmt) != NULL
13538                                && (TREE_USED (stmt)
13539                                    || TREE_ASM_WRITTEN (stmt)
13540                                    || BLOCK_ABSTRACT (stmt)));
13541           else
13542             /* We are in terse mode, so only local (nested) function
13543                definitions count as "significant" local declarations.  */
13544             for (decl = BLOCK_VARS (stmt);
13545                  decl != NULL; decl = TREE_CHAIN (decl))
13546               if (TREE_CODE (decl) == FUNCTION_DECL
13547                   && DECL_INITIAL (decl))
13548                 {
13549                   must_output_die = 1;
13550                   break;
13551                 }
13552         }
13553     }
13554
13555   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13556      DIE for any block which contains no significant local declarations at
13557      all.  Rather, in such cases we just call `decls_for_scope' so that any
13558      needed Dwarf info for any sub-blocks will get properly generated. Note
13559      that in terse mode, our definition of what constitutes a "significant"
13560      local declaration gets restricted to include only inlined function
13561      instances and local (nested) function definitions.  */
13562   if (must_output_die)
13563     {
13564       if (origin_code == FUNCTION_DECL)
13565         gen_inlined_subroutine_die (stmt, context_die, depth);
13566       else
13567         gen_lexical_block_die (stmt, context_die, depth);
13568     }
13569   else
13570     decls_for_scope (stmt, context_die, depth);
13571 }
13572
13573 /* Generate all of the decls declared within a given scope and (recursively)
13574    all of its sub-blocks.  */
13575
13576 static void
13577 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13578 {
13579   tree decl;
13580   tree subblocks;
13581
13582   /* Ignore NULL blocks.  */
13583   if (stmt == NULL_TREE)
13584     return;
13585
13586   if (TREE_USED (stmt))
13587     {
13588       /* Output the DIEs to represent all of the data objects and typedefs
13589          declared directly within this block but not within any nested
13590          sub-blocks.  Also, nested function and tag DIEs have been
13591          generated with a parent of NULL; fix that up now.  */
13592       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13593         {
13594           dw_die_ref die;
13595
13596           if (TREE_CODE (decl) == FUNCTION_DECL)
13597             die = lookup_decl_die (decl);
13598           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13599             die = lookup_type_die (TREE_TYPE (decl));
13600           else
13601             die = NULL;
13602
13603           if (die != NULL && die->die_parent == NULL)
13604             add_child_die (context_die, die);
13605           /* Do not produce debug information for static variables since
13606              these might be optimized out.  We are called for these later
13607              in varpool_analyze_pending_decls. */
13608           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13609             ;
13610           else
13611             gen_decl_die (decl, context_die);
13612         }
13613     }
13614
13615   /* If we're at -g1, we're not interested in subblocks.  */
13616   if (debug_info_level <= DINFO_LEVEL_TERSE)
13617     return;
13618
13619   /* Output the DIEs to represent all sub-blocks (and the items declared
13620      therein) of this block.  */
13621   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13622        subblocks != NULL;
13623        subblocks = BLOCK_CHAIN (subblocks))
13624     gen_block_die (subblocks, context_die, depth + 1);
13625 }
13626
13627 /* Is this a typedef we can avoid emitting?  */
13628
13629 static inline int
13630 is_redundant_typedef (const_tree decl)
13631 {
13632   if (TYPE_DECL_IS_STUB (decl))
13633     return 1;
13634
13635   if (DECL_ARTIFICIAL (decl)
13636       && DECL_CONTEXT (decl)
13637       && is_tagged_type (DECL_CONTEXT (decl))
13638       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13639       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13640     /* Also ignore the artificial member typedef for the class name.  */
13641     return 1;
13642
13643   return 0;
13644 }
13645
13646 /* Returns the DIE for decl.  A DIE will always be returned.  */
13647
13648 static dw_die_ref
13649 force_decl_die (tree decl)
13650 {
13651   dw_die_ref decl_die;
13652   unsigned saved_external_flag;
13653   tree save_fn = NULL_TREE;
13654   decl_die = lookup_decl_die (decl);
13655   if (!decl_die)
13656     {
13657       dw_die_ref context_die;
13658       tree decl_context = DECL_CONTEXT (decl);
13659       if (decl_context)
13660         {
13661           /* Find die that represents this context.  */
13662           if (TYPE_P (decl_context))
13663             context_die = force_type_die (decl_context);
13664           else
13665             context_die = force_decl_die (decl_context);
13666         }
13667       else
13668         context_die = comp_unit_die;
13669
13670       decl_die = lookup_decl_die (decl);
13671       if (decl_die)
13672         return decl_die;
13673
13674       switch (TREE_CODE (decl))
13675         {
13676         case FUNCTION_DECL:
13677           /* Clear current_function_decl, so that gen_subprogram_die thinks
13678              that this is a declaration. At this point, we just want to force
13679              declaration die.  */
13680           save_fn = current_function_decl;
13681           current_function_decl = NULL_TREE;
13682           gen_subprogram_die (decl, context_die);
13683           current_function_decl = save_fn;
13684           break;
13685
13686         case VAR_DECL:
13687           /* Set external flag to force declaration die. Restore it after
13688            gen_decl_die() call.  */
13689           saved_external_flag = DECL_EXTERNAL (decl);
13690           DECL_EXTERNAL (decl) = 1;
13691           gen_decl_die (decl, context_die);
13692           DECL_EXTERNAL (decl) = saved_external_flag;
13693           break;
13694
13695         case NAMESPACE_DECL:
13696           dwarf2out_decl (decl);
13697           break;
13698
13699         default:
13700           gcc_unreachable ();
13701         }
13702
13703       /* We should be able to find the DIE now.  */
13704       if (!decl_die)
13705         decl_die = lookup_decl_die (decl);
13706       gcc_assert (decl_die);
13707     }
13708
13709   return decl_die;
13710 }
13711
13712 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13713    always returned.  */
13714
13715 static dw_die_ref
13716 force_type_die (tree type)
13717 {
13718   dw_die_ref type_die;
13719
13720   type_die = lookup_type_die (type);
13721   if (!type_die)
13722     {
13723       dw_die_ref context_die;
13724       if (TYPE_CONTEXT (type))
13725         {
13726           if (TYPE_P (TYPE_CONTEXT (type)))
13727             context_die = force_type_die (TYPE_CONTEXT (type));
13728           else
13729             context_die = force_decl_die (TYPE_CONTEXT (type));
13730         }
13731       else
13732         context_die = comp_unit_die;
13733
13734       type_die = lookup_type_die (type);
13735       if (type_die)
13736         return type_die;
13737       gen_type_die (type, context_die);
13738       type_die = lookup_type_die (type);
13739       gcc_assert (type_die);
13740     }
13741   return type_die;
13742 }
13743
13744 /* Force out any required namespaces to be able to output DECL,
13745    and return the new context_die for it, if it's changed.  */
13746
13747 static dw_die_ref
13748 setup_namespace_context (tree thing, dw_die_ref context_die)
13749 {
13750   tree context = (DECL_P (thing)
13751                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13752   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13753     /* Force out the namespace.  */
13754     context_die = force_decl_die (context);
13755
13756   return context_die;
13757 }
13758
13759 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13760    type) within its namespace, if appropriate.
13761
13762    For compatibility with older debuggers, namespace DIEs only contain
13763    declarations; all definitions are emitted at CU scope.  */
13764
13765 static void
13766 declare_in_namespace (tree thing, dw_die_ref context_die)
13767 {
13768   dw_die_ref ns_context;
13769
13770   if (debug_info_level <= DINFO_LEVEL_TERSE)
13771     return;
13772
13773   /* If this decl is from an inlined function, then don't try to emit it in its
13774      namespace, as we will get confused.  It would have already been emitted
13775      when the abstract instance of the inline function was emitted anyways.  */
13776   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13777     return;
13778
13779   ns_context = setup_namespace_context (thing, context_die);
13780
13781   if (ns_context != context_die)
13782     {
13783       if (DECL_P (thing))
13784         gen_decl_die (thing, ns_context);
13785       else
13786         gen_type_die (thing, ns_context);
13787     }
13788 }
13789
13790 /* Generate a DIE for a namespace or namespace alias.  */
13791
13792 static void
13793 gen_namespace_die (tree decl)
13794 {
13795   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13796
13797   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13798      they are an alias of.  */
13799   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13800     {
13801       /* Output a real namespace.  */
13802       dw_die_ref namespace_die
13803         = new_die (DW_TAG_namespace, context_die, decl);
13804       add_name_and_src_coords_attributes (namespace_die, decl);
13805       equate_decl_number_to_die (decl, namespace_die);
13806     }
13807   else
13808     {
13809       /* Output a namespace alias.  */
13810
13811       /* Force out the namespace we are an alias of, if necessary.  */
13812       dw_die_ref origin_die
13813         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13814
13815       /* Now create the namespace alias DIE.  */
13816       dw_die_ref namespace_die
13817         = new_die (DW_TAG_imported_declaration, context_die, decl);
13818       add_name_and_src_coords_attributes (namespace_die, decl);
13819       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13820       equate_decl_number_to_die (decl, namespace_die);
13821     }
13822 }
13823
13824 /* Generate Dwarf debug information for a decl described by DECL.  */
13825
13826 static void
13827 gen_decl_die (tree decl, dw_die_ref context_die)
13828 {
13829   tree origin;
13830
13831   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13832     return;
13833
13834   switch (TREE_CODE (decl))
13835     {
13836     case ERROR_MARK:
13837       break;
13838
13839     case CONST_DECL:
13840       /* The individual enumerators of an enum type get output when we output
13841          the Dwarf representation of the relevant enum type itself.  */
13842       break;
13843
13844     case FUNCTION_DECL:
13845       /* Don't output any DIEs to represent mere function declarations,
13846          unless they are class members or explicit block externs.  */
13847       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13848           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13849         break;
13850
13851 #if 0
13852       /* FIXME */
13853       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13854          on local redeclarations of global functions.  That seems broken.  */
13855       if (current_function_decl != decl)
13856         /* This is only a declaration.  */;
13857 #endif
13858
13859       /* If we're emitting a clone, emit info for the abstract instance.  */
13860       if (DECL_ORIGIN (decl) != decl)
13861         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13862
13863       /* If we're emitting an out-of-line copy of an inline function,
13864          emit info for the abstract instance and set up to refer to it.  */
13865       else if (cgraph_function_possibly_inlined_p (decl)
13866                && ! DECL_ABSTRACT (decl)
13867                && ! class_or_namespace_scope_p (context_die)
13868                /* dwarf2out_abstract_function won't emit a die if this is just
13869                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13870                   that case, because that works only if we have a die.  */
13871                && DECL_INITIAL (decl) != NULL_TREE)
13872         {
13873           dwarf2out_abstract_function (decl);
13874           set_decl_origin_self (decl);
13875         }
13876
13877       /* Otherwise we're emitting the primary DIE for this decl.  */
13878       else if (debug_info_level > DINFO_LEVEL_TERSE)
13879         {
13880           /* Before we describe the FUNCTION_DECL itself, make sure that we
13881              have described its return type.  */
13882           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13883
13884           /* And its virtual context.  */
13885           if (DECL_VINDEX (decl) != NULL_TREE)
13886             gen_type_die (DECL_CONTEXT (decl), context_die);
13887
13888           /* And its containing type.  */
13889           origin = decl_class_context (decl);
13890           if (origin != NULL_TREE)
13891             gen_type_die_for_member (origin, decl, context_die);
13892
13893           /* And its containing namespace.  */
13894           declare_in_namespace (decl, context_die);
13895         }
13896
13897       /* Now output a DIE to represent the function itself.  */
13898       gen_subprogram_die (decl, context_die);
13899       break;
13900
13901     case TYPE_DECL:
13902       /* If we are in terse mode, don't generate any DIEs to represent any
13903          actual typedefs.  */
13904       if (debug_info_level <= DINFO_LEVEL_TERSE)
13905         break;
13906
13907       /* In the special case of a TYPE_DECL node representing the declaration
13908          of some type tag, if the given TYPE_DECL is marked as having been
13909          instantiated from some other (original) TYPE_DECL node (e.g. one which
13910          was generated within the original definition of an inline function) we
13911          have to generate a special (abbreviated) DW_TAG_structure_type,
13912          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13913       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13914           && is_tagged_type (TREE_TYPE (decl)))
13915         {
13916           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13917           break;
13918         }
13919
13920       if (is_redundant_typedef (decl))
13921         gen_type_die (TREE_TYPE (decl), context_die);
13922       else
13923         /* Output a DIE to represent the typedef itself.  */
13924         gen_typedef_die (decl, context_die);
13925       break;
13926
13927     case LABEL_DECL:
13928       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13929         gen_label_die (decl, context_die);
13930       break;
13931
13932     case VAR_DECL:
13933     case RESULT_DECL:
13934       /* If we are in terse mode, don't generate any DIEs to represent any
13935          variable declarations or definitions.  */
13936       if (debug_info_level <= DINFO_LEVEL_TERSE)
13937         break;
13938
13939       /* Output any DIEs that are needed to specify the type of this data
13940          object.  */
13941       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
13942         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13943       else
13944         gen_type_die (TREE_TYPE (decl), context_die);
13945
13946       /* And its containing type.  */
13947       origin = decl_class_context (decl);
13948       if (origin != NULL_TREE)
13949         gen_type_die_for_member (origin, decl, context_die);
13950
13951       /* And its containing namespace.  */
13952       declare_in_namespace (decl, context_die);
13953
13954       /* Now output the DIE to represent the data object itself.  This gets
13955          complicated because of the possibility that the VAR_DECL really
13956          represents an inlined instance of a formal parameter for an inline
13957          function.  */
13958       origin = decl_ultimate_origin (decl);
13959       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13960         gen_formal_parameter_die (decl, context_die);
13961       else
13962         gen_variable_die (decl, context_die);
13963       break;
13964
13965     case FIELD_DECL:
13966       /* Ignore the nameless fields that are used to skip bits but handle C++
13967          anonymous unions and structs.  */
13968       if (DECL_NAME (decl) != NULL_TREE
13969           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13970           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13971         {
13972           gen_type_die (member_declared_type (decl), context_die);
13973           gen_field_die (decl, context_die);
13974         }
13975       break;
13976
13977     case PARM_DECL:
13978       if (DECL_BY_REFERENCE (decl))
13979         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13980       else
13981         gen_type_die (TREE_TYPE (decl), context_die);
13982       gen_formal_parameter_die (decl, context_die);
13983       break;
13984
13985     case NAMESPACE_DECL:
13986       gen_namespace_die (decl);
13987       break;
13988
13989     default:
13990       /* Probably some frontend-internal decl.  Assume we don't care.  */
13991       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
13992       break;
13993     }
13994 }
13995 \f
13996 /* Output debug information for global decl DECL.  Called from toplev.c after
13997    compilation proper has finished.  */
13998
13999 static void
14000 dwarf2out_global_decl (tree decl)
14001 {
14002   /* Output DWARF2 information for file-scope tentative data object
14003      declarations, file-scope (extern) function declarations (which had no
14004      corresponding body) and file-scope tagged type declarations and
14005      definitions which have not yet been forced out.  */
14006   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14007     dwarf2out_decl (decl);
14008 }
14009
14010 /* Output debug information for type decl DECL.  Called from toplev.c
14011    and from language front ends (to record built-in types).  */
14012 static void
14013 dwarf2out_type_decl (tree decl, int local)
14014 {
14015   if (!local)
14016     dwarf2out_decl (decl);
14017 }
14018
14019 /* Output debug information for imported module or decl.  */
14020
14021 static void
14022 dwarf2out_imported_module_or_decl (tree decl, tree context)
14023 {
14024   dw_die_ref imported_die, at_import_die;
14025   dw_die_ref scope_die;
14026   expanded_location xloc;
14027
14028   if (debug_info_level <= DINFO_LEVEL_TERSE)
14029     return;
14030
14031   gcc_assert (decl);
14032
14033   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14034      We need decl DIE for reference and scope die. First, get DIE for the decl
14035      itself.  */
14036
14037   /* Get the scope die for decl context. Use comp_unit_die for global module
14038      or decl. If die is not found for non globals, force new die.  */
14039   if (!context)
14040     scope_die = comp_unit_die;
14041   else if (TYPE_P (context))
14042     {
14043       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14044         return;
14045     scope_die = force_type_die (context);
14046     }
14047   else
14048     scope_die = force_decl_die (context);
14049
14050   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14051   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14052     {
14053       if (is_base_type (TREE_TYPE (decl)))
14054         at_import_die = base_type_die (TREE_TYPE (decl));
14055       else
14056         at_import_die = force_type_die (TREE_TYPE (decl));
14057     }
14058   else
14059     {
14060       at_import_die = lookup_decl_die (decl);
14061       if (!at_import_die)
14062         {
14063           /* If we're trying to avoid duplicate debug info, we may not have
14064              emitted the member decl for this field.  Emit it now.  */
14065           if (TREE_CODE (decl) == FIELD_DECL)
14066             {
14067               tree type = DECL_CONTEXT (decl);
14068               dw_die_ref type_context_die;
14069
14070               if (TYPE_CONTEXT (type))
14071                 if (TYPE_P (TYPE_CONTEXT (type)))
14072                   {
14073                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14074                                                    DINFO_USAGE_DIR_USE))
14075                       return;
14076                   type_context_die = force_type_die (TYPE_CONTEXT (type));
14077                   }
14078               else
14079                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14080               else
14081                 type_context_die = comp_unit_die;
14082               gen_type_die_for_member (type, decl, type_context_die);
14083             }
14084           at_import_die = force_decl_die (decl);
14085         }
14086     }
14087
14088   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14089   if (TREE_CODE (decl) == NAMESPACE_DECL)
14090     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14091   else
14092     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14093
14094   xloc = expand_location (input_location);
14095   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14096   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14097   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14098 }
14099
14100 /* Write the debugging output for DECL.  */
14101
14102 void
14103 dwarf2out_decl (tree decl)
14104 {
14105   dw_die_ref context_die = comp_unit_die;
14106
14107   switch (TREE_CODE (decl))
14108     {
14109     case ERROR_MARK:
14110       return;
14111
14112     case FUNCTION_DECL:
14113       /* What we would really like to do here is to filter out all mere
14114          file-scope declarations of file-scope functions which are never
14115          referenced later within this translation unit (and keep all of ones
14116          that *are* referenced later on) but we aren't clairvoyant, so we have
14117          no idea which functions will be referenced in the future (i.e. later
14118          on within the current translation unit). So here we just ignore all
14119          file-scope function declarations which are not also definitions.  If
14120          and when the debugger needs to know something about these functions,
14121          it will have to hunt around and find the DWARF information associated
14122          with the definition of the function.
14123
14124          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14125          nodes represent definitions and which ones represent mere
14126          declarations.  We have to check DECL_INITIAL instead. That's because
14127          the C front-end supports some weird semantics for "extern inline"
14128          function definitions.  These can get inlined within the current
14129          translation unit (and thus, we need to generate Dwarf info for their
14130          abstract instances so that the Dwarf info for the concrete inlined
14131          instances can have something to refer to) but the compiler never
14132          generates any out-of-lines instances of such things (despite the fact
14133          that they *are* definitions).
14134
14135          The important point is that the C front-end marks these "extern
14136          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14137          them anyway. Note that the C++ front-end also plays some similar games
14138          for inline function definitions appearing within include files which
14139          also contain `#pragma interface' pragmas.  */
14140       if (DECL_INITIAL (decl) == NULL_TREE)
14141         return;
14142
14143       /* If we're a nested function, initially use a parent of NULL; if we're
14144          a plain function, this will be fixed up in decls_for_scope.  If
14145          we're a method, it will be ignored, since we already have a DIE.  */
14146       if (decl_function_context (decl)
14147           /* But if we're in terse mode, we don't care about scope.  */
14148           && debug_info_level > DINFO_LEVEL_TERSE)
14149         context_die = NULL;
14150       break;
14151
14152     case VAR_DECL:
14153       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14154          declaration and if the declaration was never even referenced from
14155          within this entire compilation unit.  We suppress these DIEs in
14156          order to save space in the .debug section (by eliminating entries
14157          which are probably useless).  Note that we must not suppress
14158          block-local extern declarations (whether used or not) because that
14159          would screw-up the debugger's name lookup mechanism and cause it to
14160          miss things which really ought to be in scope at a given point.  */
14161       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14162         return;
14163
14164       /* For local statics lookup proper context die.  */
14165       if (TREE_STATIC (decl) && decl_function_context (decl))
14166         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14167
14168       /* If we are in terse mode, don't generate any DIEs to represent any
14169          variable declarations or definitions.  */
14170       if (debug_info_level <= DINFO_LEVEL_TERSE)
14171         return;
14172       break;
14173
14174     case NAMESPACE_DECL:
14175       if (debug_info_level <= DINFO_LEVEL_TERSE)
14176         return;
14177       if (lookup_decl_die (decl) != NULL)
14178         return;
14179       break;
14180
14181     case TYPE_DECL:
14182       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14183       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14184         return;
14185
14186       /* Don't bother trying to generate any DIEs to represent any of the
14187          normal built-in types for the language we are compiling.  */
14188       if (DECL_IS_BUILTIN (decl))
14189         {
14190           /* OK, we need to generate one for `bool' so GDB knows what type
14191              comparisons have.  */
14192           if (is_cxx ()
14193               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14194               && ! DECL_IGNORED_P (decl))
14195             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14196
14197           return;
14198         }
14199
14200       /* If we are in terse mode, don't generate any DIEs for types.  */
14201       if (debug_info_level <= DINFO_LEVEL_TERSE)
14202         return;
14203
14204       /* If we're a function-scope tag, initially use a parent of NULL;
14205          this will be fixed up in decls_for_scope.  */
14206       if (decl_function_context (decl))
14207         context_die = NULL;
14208
14209       break;
14210
14211     default:
14212       return;
14213     }
14214
14215   gen_decl_die (decl, context_die);
14216 }
14217
14218 /* Output a marker (i.e. a label) for the beginning of the generated code for
14219    a lexical block.  */
14220
14221 static void
14222 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14223                        unsigned int blocknum)
14224 {
14225   switch_to_section (current_function_section ());
14226   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14227 }
14228
14229 /* Output a marker (i.e. a label) for the end of the generated code for a
14230    lexical block.  */
14231
14232 static void
14233 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14234 {
14235   switch_to_section (current_function_section ());
14236   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14237 }
14238
14239 /* Returns nonzero if it is appropriate not to emit any debugging
14240    information for BLOCK, because it doesn't contain any instructions.
14241
14242    Don't allow this for blocks with nested functions or local classes
14243    as we would end up with orphans, and in the presence of scheduling
14244    we may end up calling them anyway.  */
14245
14246 static bool
14247 dwarf2out_ignore_block (const_tree block)
14248 {
14249   tree decl;
14250
14251   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14252     if (TREE_CODE (decl) == FUNCTION_DECL
14253         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14254       return 0;
14255
14256   return 1;
14257 }
14258
14259 /* Hash table routines for file_hash.  */
14260
14261 static int
14262 file_table_eq (const void *p1_p, const void *p2_p)
14263 {
14264   const struct dwarf_file_data * p1 = p1_p;
14265   const char * p2 = p2_p;
14266   return strcmp (p1->filename, p2) == 0;
14267 }
14268
14269 static hashval_t
14270 file_table_hash (const void *p_p)
14271 {
14272   const struct dwarf_file_data * p = p_p;
14273   return htab_hash_string (p->filename);
14274 }
14275
14276 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14277    dwarf2out.c) and return its "index".  The index of each (known) filename is
14278    just a unique number which is associated with only that one filename.  We
14279    need such numbers for the sake of generating labels (in the .debug_sfnames
14280    section) and references to those files numbers (in the .debug_srcinfo
14281    and.debug_macinfo sections).  If the filename given as an argument is not
14282    found in our current list, add it to the list and assign it the next
14283    available unique index number.  In order to speed up searches, we remember
14284    the index of the filename was looked up last.  This handles the majority of
14285    all searches.  */
14286
14287 static struct dwarf_file_data *
14288 lookup_filename (const char *file_name)
14289 {
14290   void ** slot;
14291   struct dwarf_file_data * created;
14292
14293   /* Check to see if the file name that was searched on the previous
14294      call matches this file name.  If so, return the index.  */
14295   if (file_table_last_lookup
14296       && (file_name == file_table_last_lookup->filename
14297           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14298     return file_table_last_lookup;
14299
14300   /* Didn't match the previous lookup, search the table.  */
14301   slot = htab_find_slot_with_hash (file_table, file_name,
14302                                    htab_hash_string (file_name), INSERT);
14303   if (*slot)
14304     return *slot;
14305
14306   created = ggc_alloc (sizeof (struct dwarf_file_data));
14307   created->filename = file_name;
14308   created->emitted_number = 0;
14309   *slot = created;
14310   return created;
14311 }
14312
14313 /* If the assembler will construct the file table, then translate the compiler
14314    internal file table number into the assembler file table number, and emit
14315    a .file directive if we haven't already emitted one yet.  The file table
14316    numbers are different because we prune debug info for unused variables and
14317    types, which may include filenames.  */
14318
14319 static int
14320 maybe_emit_file (struct dwarf_file_data * fd)
14321 {
14322   if (! fd->emitted_number)
14323     {
14324       if (last_emitted_file)
14325         fd->emitted_number = last_emitted_file->emitted_number + 1;
14326       else
14327         fd->emitted_number = 1;
14328       last_emitted_file = fd;
14329
14330       if (DWARF2_ASM_LINE_DEBUG_INFO)
14331         {
14332           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14333           output_quoted_string (asm_out_file,
14334                                 remap_debug_filename (fd->filename));
14335           fputc ('\n', asm_out_file);
14336         }
14337     }
14338
14339   return fd->emitted_number;
14340 }
14341
14342 /* Called by the final INSN scan whenever we see a var location.  We
14343    use it to drop labels in the right places, and throw the location in
14344    our lookup table.  */
14345
14346 static void
14347 dwarf2out_var_location (rtx loc_note)
14348 {
14349   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14350   struct var_loc_node *newloc;
14351   rtx prev_insn;
14352   static rtx last_insn;
14353   static const char *last_label;
14354   tree decl;
14355
14356   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14357     return;
14358   prev_insn = PREV_INSN (loc_note);
14359
14360   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14361   /* If the insn we processed last time is the previous insn
14362      and it is also a var location note, use the label we emitted
14363      last time.  */
14364   if (last_insn != NULL_RTX
14365       && last_insn == prev_insn
14366       && NOTE_P (prev_insn)
14367       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14368     {
14369       newloc->label = last_label;
14370     }
14371   else
14372     {
14373       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14374       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14375       loclabel_num++;
14376       newloc->label = ggc_strdup (loclabel);
14377     }
14378   newloc->var_loc_note = loc_note;
14379   newloc->next = NULL;
14380
14381   if (cfun && in_cold_section_p)
14382     newloc->section_label = cfun->cold_section_label;
14383   else
14384     newloc->section_label = text_section_label;
14385
14386   last_insn = loc_note;
14387   last_label = newloc->label;
14388   decl = NOTE_VAR_LOCATION_DECL (loc_note);
14389   add_var_loc_to_decl (decl, newloc);
14390 }
14391
14392 /* We need to reset the locations at the beginning of each
14393    function. We can't do this in the end_function hook, because the
14394    declarations that use the locations won't have been output when
14395    that hook is called.  Also compute have_multiple_function_sections here.  */
14396
14397 static void
14398 dwarf2out_begin_function (tree fun)
14399 {
14400   htab_empty (decl_loc_table);
14401
14402   if (function_section (fun) != text_section)
14403     have_multiple_function_sections = true;
14404
14405   dwarf2out_note_section_used ();
14406 }
14407
14408 /* Output a label to mark the beginning of a source code line entry
14409    and record information relating to this source line, in
14410    'line_info_table' for later output of the .debug_line section.  */
14411
14412 static void
14413 dwarf2out_source_line (unsigned int line, const char *filename)
14414 {
14415   if (debug_info_level >= DINFO_LEVEL_NORMAL
14416       && line != 0)
14417     {
14418       int file_num = maybe_emit_file (lookup_filename (filename));
14419
14420       switch_to_section (current_function_section ());
14421
14422       /* If requested, emit something human-readable.  */
14423       if (flag_debug_asm)
14424         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14425                  filename, line);
14426
14427       if (DWARF2_ASM_LINE_DEBUG_INFO)
14428         {
14429           /* Emit the .loc directive understood by GNU as.  */
14430           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14431
14432           /* Indicate that line number info exists.  */
14433           line_info_table_in_use++;
14434         }
14435       else if (function_section (current_function_decl) != text_section)
14436         {
14437           dw_separate_line_info_ref line_info;
14438           targetm.asm_out.internal_label (asm_out_file,
14439                                           SEPARATE_LINE_CODE_LABEL,
14440                                           separate_line_info_table_in_use);
14441
14442           /* Expand the line info table if necessary.  */
14443           if (separate_line_info_table_in_use
14444               == separate_line_info_table_allocated)
14445             {
14446               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14447               separate_line_info_table
14448                 = ggc_realloc (separate_line_info_table,
14449                                separate_line_info_table_allocated
14450                                * sizeof (dw_separate_line_info_entry));
14451               memset (separate_line_info_table
14452                        + separate_line_info_table_in_use,
14453                       0,
14454                       (LINE_INFO_TABLE_INCREMENT
14455                        * sizeof (dw_separate_line_info_entry)));
14456             }
14457
14458           /* Add the new entry at the end of the line_info_table.  */
14459           line_info
14460             = &separate_line_info_table[separate_line_info_table_in_use++];
14461           line_info->dw_file_num = file_num;
14462           line_info->dw_line_num = line;
14463           line_info->function = current_function_funcdef_no;
14464         }
14465       else
14466         {
14467           dw_line_info_ref line_info;
14468
14469           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14470                                      line_info_table_in_use);
14471
14472           /* Expand the line info table if necessary.  */
14473           if (line_info_table_in_use == line_info_table_allocated)
14474             {
14475               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14476               line_info_table
14477                 = ggc_realloc (line_info_table,
14478                                (line_info_table_allocated
14479                                 * sizeof (dw_line_info_entry)));
14480               memset (line_info_table + line_info_table_in_use, 0,
14481                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14482             }
14483
14484           /* Add the new entry at the end of the line_info_table.  */
14485           line_info = &line_info_table[line_info_table_in_use++];
14486           line_info->dw_file_num = file_num;
14487           line_info->dw_line_num = line;
14488         }
14489     }
14490 }
14491
14492 /* Record the beginning of a new source file.  */
14493
14494 static void
14495 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14496 {
14497   if (flag_eliminate_dwarf2_dups)
14498     {
14499       /* Record the beginning of the file for break_out_includes.  */
14500       dw_die_ref bincl_die;
14501
14502       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14503       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14504     }
14505
14506   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14507     {
14508       int file_num = maybe_emit_file (lookup_filename (filename));
14509
14510       switch_to_section (debug_macinfo_section);
14511       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14512       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14513                                    lineno);
14514
14515       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14516     }
14517 }
14518
14519 /* Record the end of a source file.  */
14520
14521 static void
14522 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14523 {
14524   if (flag_eliminate_dwarf2_dups)
14525     /* Record the end of the file for break_out_includes.  */
14526     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14527
14528   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14529     {
14530       switch_to_section (debug_macinfo_section);
14531       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14532     }
14533 }
14534
14535 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14536    the tail part of the directive line, i.e. the part which is past the
14537    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14538
14539 static void
14540 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14541                   const char *buffer ATTRIBUTE_UNUSED)
14542 {
14543   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14544     {
14545       switch_to_section (debug_macinfo_section);
14546       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14547       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14548       dw2_asm_output_nstring (buffer, -1, "The macro");
14549     }
14550 }
14551
14552 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14553    the tail part of the directive line, i.e. the part which is past the
14554    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14555
14556 static void
14557 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14558                  const char *buffer ATTRIBUTE_UNUSED)
14559 {
14560   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14561     {
14562       switch_to_section (debug_macinfo_section);
14563       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14564       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14565       dw2_asm_output_nstring (buffer, -1, "The macro");
14566     }
14567 }
14568
14569 /* Set up for Dwarf output at the start of compilation.  */
14570
14571 static void
14572 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14573 {
14574   /* Allocate the file_table.  */
14575   file_table = htab_create_ggc (50, file_table_hash,
14576                                 file_table_eq, NULL);
14577
14578   /* Allocate the decl_die_table.  */
14579   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14580                                     decl_die_table_eq, NULL);
14581
14582   /* Allocate the decl_loc_table.  */
14583   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14584                                     decl_loc_table_eq, NULL);
14585
14586   /* Allocate the initial hunk of the decl_scope_table.  */
14587   decl_scope_table = VEC_alloc (tree, gc, 256);
14588
14589   /* Allocate the initial hunk of the abbrev_die_table.  */
14590   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14591                                         * sizeof (dw_die_ref));
14592   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14593   /* Zero-th entry is allocated, but unused.  */
14594   abbrev_die_table_in_use = 1;
14595
14596   /* Allocate the initial hunk of the line_info_table.  */
14597   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14598                                        * sizeof (dw_line_info_entry));
14599   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14600
14601   /* Zero-th entry is allocated, but unused.  */
14602   line_info_table_in_use = 1;
14603
14604   /* Allocate the pubtypes and pubnames vectors.  */
14605   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14606   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14607
14608   /* Generate the initial DIE for the .debug section.  Note that the (string)
14609      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14610      will (typically) be a relative pathname and that this pathname should be
14611      taken as being relative to the directory from which the compiler was
14612      invoked when the given (base) source file was compiled.  We will fill
14613      in this value in dwarf2out_finish.  */
14614   comp_unit_die = gen_compile_unit_die (NULL);
14615
14616   incomplete_types = VEC_alloc (tree, gc, 64);
14617
14618   used_rtx_array = VEC_alloc (rtx, gc, 32);
14619
14620   debug_info_section = get_section (DEBUG_INFO_SECTION,
14621                                     SECTION_DEBUG, NULL);
14622   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14623                                       SECTION_DEBUG, NULL);
14624   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14625                                        SECTION_DEBUG, NULL);
14626   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14627                                        SECTION_DEBUG, NULL);
14628   debug_line_section = get_section (DEBUG_LINE_SECTION,
14629                                     SECTION_DEBUG, NULL);
14630   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14631                                    SECTION_DEBUG, NULL);
14632   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14633                                         SECTION_DEBUG, NULL);
14634 #ifdef DEBUG_PUBTYPES_SECTION
14635   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14636                                         SECTION_DEBUG, NULL);
14637 #endif
14638   debug_str_section = get_section (DEBUG_STR_SECTION,
14639                                    DEBUG_STR_SECTION_FLAGS, NULL);
14640   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14641                                       SECTION_DEBUG, NULL);
14642   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14643                                      SECTION_DEBUG, NULL);
14644
14645   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14646   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14647                                DEBUG_ABBREV_SECTION_LABEL, 0);
14648   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14649   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14650                                COLD_TEXT_SECTION_LABEL, 0);
14651   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14652
14653   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14654                                DEBUG_INFO_SECTION_LABEL, 0);
14655   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14656                                DEBUG_LINE_SECTION_LABEL, 0);
14657   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14658                                DEBUG_RANGES_SECTION_LABEL, 0);
14659   switch_to_section (debug_abbrev_section);
14660   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14661   switch_to_section (debug_info_section);
14662   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14663   switch_to_section (debug_line_section);
14664   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14665
14666   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14667     {
14668       switch_to_section (debug_macinfo_section);
14669       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14670                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14671       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14672     }
14673
14674   switch_to_section (text_section);
14675   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14676   if (flag_reorder_blocks_and_partition)
14677     {
14678       cold_text_section = unlikely_text_section ();
14679       switch_to_section (cold_text_section);
14680       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14681     }
14682 }
14683
14684 /* A helper function for dwarf2out_finish called through
14685    ht_forall.  Emit one queued .debug_str string.  */
14686
14687 static int
14688 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14689 {
14690   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14691
14692   if (node->form == DW_FORM_strp)
14693     {
14694       switch_to_section (debug_str_section);
14695       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14696       assemble_string (node->str, strlen (node->str) + 1);
14697     }
14698
14699   return 1;
14700 }
14701
14702 #if ENABLE_ASSERT_CHECKING
14703 /* Verify that all marks are clear.  */
14704
14705 static void
14706 verify_marks_clear (dw_die_ref die)
14707 {
14708   dw_die_ref c;
14709
14710   gcc_assert (! die->die_mark);
14711   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14712 }
14713 #endif /* ENABLE_ASSERT_CHECKING */
14714
14715 /* Clear the marks for a die and its children.
14716    Be cool if the mark isn't set.  */
14717
14718 static void
14719 prune_unmark_dies (dw_die_ref die)
14720 {
14721   dw_die_ref c;
14722
14723   if (die->die_mark)
14724     die->die_mark = 0;
14725   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14726 }
14727
14728 /* Given DIE that we're marking as used, find any other dies
14729    it references as attributes and mark them as used.  */
14730
14731 static void
14732 prune_unused_types_walk_attribs (dw_die_ref die)
14733 {
14734   dw_attr_ref a;
14735   unsigned ix;
14736
14737   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14738     {
14739       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14740         {
14741           /* A reference to another DIE.
14742              Make sure that it will get emitted.  */
14743           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14744         }
14745       /* Set the string's refcount to 0 so that prune_unused_types_mark
14746          accounts properly for it.  */
14747       if (AT_class (a) == dw_val_class_str)
14748         a->dw_attr_val.v.val_str->refcount = 0;
14749     }
14750 }
14751
14752
14753 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14754    to DIE's children.  */
14755
14756 static void
14757 prune_unused_types_mark (dw_die_ref die, int dokids)
14758 {
14759   dw_die_ref c;
14760
14761   if (die->die_mark == 0)
14762     {
14763       /* We haven't done this node yet.  Mark it as used.  */
14764       die->die_mark = 1;
14765
14766       /* We also have to mark its parents as used.
14767          (But we don't want to mark our parents' kids due to this.)  */
14768       if (die->die_parent)
14769         prune_unused_types_mark (die->die_parent, 0);
14770
14771       /* Mark any referenced nodes.  */
14772       prune_unused_types_walk_attribs (die);
14773
14774       /* If this node is a specification,
14775          also mark the definition, if it exists.  */
14776       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14777         prune_unused_types_mark (die->die_definition, 1);
14778     }
14779
14780   if (dokids && die->die_mark != 2)
14781     {
14782       /* We need to walk the children, but haven't done so yet.
14783          Remember that we've walked the kids.  */
14784       die->die_mark = 2;
14785
14786       /* If this is an array type, we need to make sure our
14787          kids get marked, even if they're types.  */
14788       if (die->die_tag == DW_TAG_array_type)
14789         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14790       else
14791         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14792     }
14793 }
14794
14795
14796 /* Walk the tree DIE and mark types that we actually use.  */
14797
14798 static void
14799 prune_unused_types_walk (dw_die_ref die)
14800 {
14801   dw_die_ref c;
14802
14803   /* Don't do anything if this node is already marked.  */
14804   if (die->die_mark)
14805     return;
14806
14807   switch (die->die_tag)
14808     {
14809     case DW_TAG_const_type:
14810     case DW_TAG_packed_type:
14811     case DW_TAG_pointer_type:
14812     case DW_TAG_reference_type:
14813     case DW_TAG_volatile_type:
14814     case DW_TAG_typedef:
14815     case DW_TAG_array_type:
14816     case DW_TAG_structure_type:
14817     case DW_TAG_union_type:
14818     case DW_TAG_class_type:
14819     case DW_TAG_interface_type:
14820     case DW_TAG_friend:
14821     case DW_TAG_variant_part:
14822     case DW_TAG_enumeration_type:
14823     case DW_TAG_subroutine_type:
14824     case DW_TAG_string_type:
14825     case DW_TAG_set_type:
14826     case DW_TAG_subrange_type:
14827     case DW_TAG_ptr_to_member_type:
14828     case DW_TAG_file_type:
14829       if (die->die_perennial_p)
14830         break;
14831
14832       /* It's a type node --- don't mark it.  */
14833       return;
14834
14835     default:
14836       /* Mark everything else.  */
14837       break;
14838   }
14839
14840   die->die_mark = 1;
14841
14842   /* Now, mark any dies referenced from here.  */
14843   prune_unused_types_walk_attribs (die);
14844
14845   /* Mark children.  */
14846   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14847 }
14848
14849 /* Increment the string counts on strings referred to from DIE's
14850    attributes.  */
14851
14852 static void
14853 prune_unused_types_update_strings (dw_die_ref die)
14854 {
14855   dw_attr_ref a;
14856   unsigned ix;
14857
14858   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14859     if (AT_class (a) == dw_val_class_str)
14860       {
14861         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14862         s->refcount++;
14863         /* Avoid unnecessarily putting strings that are used less than
14864            twice in the hash table.  */
14865         if (s->refcount
14866             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14867           {
14868             void ** slot;
14869             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14870                                              htab_hash_string (s->str),
14871                                              INSERT);
14872             gcc_assert (*slot == NULL);
14873             *slot = s;
14874           }
14875       }
14876 }
14877
14878 /* Remove from the tree DIE any dies that aren't marked.  */
14879
14880 static void
14881 prune_unused_types_prune (dw_die_ref die)
14882 {
14883   dw_die_ref c;
14884
14885   gcc_assert (die->die_mark);
14886   prune_unused_types_update_strings (die);
14887
14888   if (! die->die_child)
14889     return;
14890
14891   c = die->die_child;
14892   do {
14893     dw_die_ref prev = c;
14894     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14895       if (c == die->die_child)
14896         {
14897           /* No marked children between 'prev' and the end of the list.  */
14898           if (prev == c)
14899             /* No marked children at all.  */
14900             die->die_child = NULL;
14901           else
14902             {
14903               prev->die_sib = c->die_sib;
14904               die->die_child = prev;
14905             }
14906           return;
14907         }
14908
14909     if (c != prev->die_sib)
14910       prev->die_sib = c;
14911     prune_unused_types_prune (c);
14912   } while (c != die->die_child);
14913 }
14914
14915
14916 /* Remove dies representing declarations that we never use.  */
14917
14918 static void
14919 prune_unused_types (void)
14920 {
14921   unsigned int i;
14922   limbo_die_node *node;
14923   pubname_ref pub;
14924
14925 #if ENABLE_ASSERT_CHECKING
14926   /* All the marks should already be clear.  */
14927   verify_marks_clear (comp_unit_die);
14928   for (node = limbo_die_list; node; node = node->next)
14929     verify_marks_clear (node->die);
14930 #endif /* ENABLE_ASSERT_CHECKING */
14931
14932   /* Set the mark on nodes that are actually used.  */
14933   prune_unused_types_walk (comp_unit_die);
14934   for (node = limbo_die_list; node; node = node->next)
14935     prune_unused_types_walk (node->die);
14936
14937   /* Also set the mark on nodes referenced from the
14938      pubname_table or arange_table.  */
14939   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14940     prune_unused_types_mark (pub->die, 1);
14941   for (i = 0; i < arange_table_in_use; i++)
14942     prune_unused_types_mark (arange_table[i], 1);
14943
14944   /* Get rid of nodes that aren't marked; and update the string counts.  */
14945   if (debug_str_hash)
14946     htab_empty (debug_str_hash);
14947   prune_unused_types_prune (comp_unit_die);
14948   for (node = limbo_die_list; node; node = node->next)
14949     prune_unused_types_prune (node->die);
14950
14951   /* Leave the marks clear.  */
14952   prune_unmark_dies (comp_unit_die);
14953   for (node = limbo_die_list; node; node = node->next)
14954     prune_unmark_dies (node->die);
14955 }
14956
14957 /* Set the parameter to true if there are any relative pathnames in
14958    the file table.  */
14959 static int
14960 file_table_relative_p (void ** slot, void *param)
14961 {
14962   bool *p = param;
14963   struct dwarf_file_data *d = *slot;
14964   if (!IS_ABSOLUTE_PATH (d->filename))
14965     {
14966       *p = true;
14967       return 0;
14968     }
14969   return 1;
14970 }
14971
14972 /* Output stuff that dwarf requires at the end of every file,
14973    and generate the DWARF-2 debugging info.  */
14974
14975 static void
14976 dwarf2out_finish (const char *filename)
14977 {
14978   limbo_die_node *node, *next_node;
14979   dw_die_ref die = 0;
14980
14981   /* Add the name for the main input file now.  We delayed this from
14982      dwarf2out_init to avoid complications with PCH.  */
14983   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
14984   if (!IS_ABSOLUTE_PATH (filename))
14985     add_comp_dir_attribute (comp_unit_die);
14986   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
14987     {
14988       bool p = false;
14989       htab_traverse (file_table, file_table_relative_p, &p);
14990       if (p)
14991         add_comp_dir_attribute (comp_unit_die);
14992     }
14993
14994   /* Traverse the limbo die list, and add parent/child links.  The only
14995      dies without parents that should be here are concrete instances of
14996      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
14997      For concrete instances, we can get the parent die from the abstract
14998      instance.  */
14999   for (node = limbo_die_list; node; node = next_node)
15000     {
15001       next_node = node->next;
15002       die = node->die;
15003
15004       if (die->die_parent == NULL)
15005         {
15006           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15007
15008           if (origin)
15009             add_child_die (origin->die_parent, die);
15010           else if (die == comp_unit_die)
15011             ;
15012           else if (errorcount > 0 || sorrycount > 0)
15013             /* It's OK to be confused by errors in the input.  */
15014             add_child_die (comp_unit_die, die);
15015           else
15016             {
15017               /* In certain situations, the lexical block containing a
15018                  nested function can be optimized away, which results
15019                  in the nested function die being orphaned.  Likewise
15020                  with the return type of that nested function.  Force
15021                  this to be a child of the containing function.
15022
15023                  It may happen that even the containing function got fully
15024                  inlined and optimized out.  In that case we are lost and
15025                  assign the empty child.  This should not be big issue as
15026                  the function is likely unreachable too.  */
15027               tree context = NULL_TREE;
15028
15029               gcc_assert (node->created_for);
15030
15031               if (DECL_P (node->created_for))
15032                 context = DECL_CONTEXT (node->created_for);
15033               else if (TYPE_P (node->created_for))
15034                 context = TYPE_CONTEXT (node->created_for);
15035
15036               gcc_assert (context
15037                           && (TREE_CODE (context) == FUNCTION_DECL
15038                               || TREE_CODE (context) == NAMESPACE_DECL));
15039
15040               origin = lookup_decl_die (context);
15041               if (origin)
15042                 add_child_die (origin, die);
15043               else
15044                 add_child_die (comp_unit_die, die);
15045             }
15046         }
15047     }
15048
15049   limbo_die_list = NULL;
15050
15051   /* Walk through the list of incomplete types again, trying once more to
15052      emit full debugging info for them.  */
15053   retry_incomplete_types ();
15054
15055   if (flag_eliminate_unused_debug_types)
15056     prune_unused_types ();
15057
15058   /* Generate separate CUs for each of the include files we've seen.
15059      They will go into limbo_die_list.  */
15060   if (flag_eliminate_dwarf2_dups)
15061     break_out_includes (comp_unit_die);
15062
15063   /* Traverse the DIE's and add add sibling attributes to those DIE's
15064      that have children.  */
15065   add_sibling_attributes (comp_unit_die);
15066   for (node = limbo_die_list; node; node = node->next)
15067     add_sibling_attributes (node->die);
15068
15069   /* Output a terminator label for the .text section.  */
15070   switch_to_section (text_section);
15071   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15072   if (flag_reorder_blocks_and_partition)
15073     {
15074       switch_to_section (unlikely_text_section ());
15075       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15076     }
15077
15078   /* We can only use the low/high_pc attributes if all of the code was
15079      in .text.  */
15080   if (!have_multiple_function_sections)
15081     {
15082       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15083       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15084     }
15085
15086   else
15087     {
15088       unsigned fde_idx = 0;
15089
15090       /* We need to give .debug_loc and .debug_ranges an appropriate
15091          "base address".  Use zero so that these addresses become
15092          absolute.  Historically, we've emitted the unexpected
15093          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15094          Emit both to give time for other tools to adapt.  */
15095       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15096       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15097
15098       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15099                          add_ranges_by_labels (text_section_label,
15100                                                text_end_label));
15101       if (flag_reorder_blocks_and_partition)
15102         add_ranges_by_labels (cold_text_section_label,
15103                               cold_end_label);
15104
15105       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15106         {
15107           dw_fde_ref fde = &fde_table[fde_idx];
15108
15109           if (fde->dw_fde_switched_sections)
15110             {
15111               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15112                                     fde->dw_fde_hot_section_end_label);
15113               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15114                                     fde->dw_fde_unlikely_section_end_label);
15115             }
15116           else
15117             add_ranges_by_labels (fde->dw_fde_begin,
15118                                   fde->dw_fde_end);
15119         }
15120
15121       add_ranges (NULL);
15122     }
15123
15124   /* Output location list section if necessary.  */
15125   if (have_location_lists)
15126     {
15127       /* Output the location lists info.  */
15128       switch_to_section (debug_loc_section);
15129       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15130                                    DEBUG_LOC_SECTION_LABEL, 0);
15131       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15132       output_location_lists (die);
15133     }
15134
15135   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15136     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15137                     debug_line_section_label);
15138
15139   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15140     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15141
15142   /* Output all of the compilation units.  We put the main one last so that
15143      the offsets are available to output_pubnames.  */
15144   for (node = limbo_die_list; node; node = node->next)
15145     output_comp_unit (node->die, 0);
15146
15147   output_comp_unit (comp_unit_die, 0);
15148
15149   /* Output the abbreviation table.  */
15150   switch_to_section (debug_abbrev_section);
15151   output_abbrev_section ();
15152
15153   /* Output public names table if necessary.  */
15154   if (!VEC_empty (pubname_entry, pubname_table))
15155     {
15156       switch_to_section (debug_pubnames_section);
15157       output_pubnames (pubname_table);
15158     }
15159
15160 #ifdef DEBUG_PUBTYPES_SECTION
15161   /* Output public types table if necessary.  */
15162   if (!VEC_empty (pubname_entry, pubtype_table))
15163     {
15164       switch_to_section (debug_pubtypes_section);
15165       output_pubnames (pubtype_table);
15166     }
15167 #endif
15168
15169   /* Output the address range information.  We only put functions in the arange
15170      table, so don't write it out if we don't have any.  */
15171   if (fde_table_in_use)
15172     {
15173       switch_to_section (debug_aranges_section);
15174       output_aranges ();
15175     }
15176
15177   /* Output ranges section if necessary.  */
15178   if (ranges_table_in_use)
15179     {
15180       switch_to_section (debug_ranges_section);
15181       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15182       output_ranges ();
15183     }
15184
15185   /* Output the source line correspondence table.  We must do this
15186      even if there is no line information.  Otherwise, on an empty
15187      translation unit, we will generate a present, but empty,
15188      .debug_info section.  IRIX 6.5 `nm' will then complain when
15189      examining the file.  This is done late so that any filenames
15190      used by the debug_info section are marked as 'used'.  */
15191   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15192     {
15193       switch_to_section (debug_line_section);
15194       output_line_info ();
15195     }
15196
15197   /* Have to end the macro section.  */
15198   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15199     {
15200       switch_to_section (debug_macinfo_section);
15201       dw2_asm_output_data (1, 0, "End compilation unit");
15202     }
15203
15204   /* If we emitted any DW_FORM_strp form attribute, output the string
15205      table too.  */
15206   if (debug_str_hash)
15207     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15208 }
15209 #else
15210
15211 /* This should never be used, but its address is needed for comparisons.  */
15212 const struct gcc_debug_hooks dwarf2_debug_hooks;
15213
15214 #endif /* DWARF2_DEBUGGING_INFO */
15215
15216 #include "gt-dwarf2out.h"