OSDN Git Service

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