OSDN Git Service

2009-06-05 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009 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 *, int);
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 /* Save the result of dwarf2out_do_frame across PCH.  */
114 static GTY(()) bool saved_do_cfi_asm = 0;
115
116 /* Decide whether we want to emit frame unwind information for the current
117    translation unit.  */
118
119 int
120 dwarf2out_do_frame (void)
121 {
122   /* We want to emit correct CFA location expressions or lists, so we
123      have to return true if we're going to output debug info, even if
124      we're not going to output frame or unwind info.  */
125   return (write_symbols == DWARF2_DEBUG
126           || write_symbols == VMS_AND_DWARF2_DEBUG
127           || DWARF2_FRAME_INFO || saved_do_cfi_asm
128 #ifdef DWARF2_UNWIND_INFO
129           || (DWARF2_UNWIND_INFO
130               && (flag_unwind_tables
131                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
132 #endif
133           );
134 }
135
136 /* Decide whether to emit frame unwind via assembler directives.  */
137
138 int
139 dwarf2out_do_cfi_asm (void)
140 {
141   int enc;
142
143 #ifdef MIPS_DEBUGGING_INFO
144   return false;
145 #endif
146   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
147     return false;
148   if (saved_do_cfi_asm || !eh_personality_libfunc)
149     return true;
150   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
151     return false;
152
153   /* Make sure the personality encoding is one the assembler can support.
154      In particular, aligned addresses can't be handled.  */
155   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
156   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
157     return false;
158   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
159   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
160     return false;
161
162   saved_do_cfi_asm = true;
163   return true;
164 }
165
166 /* The size of the target's pointer type.  */
167 #ifndef PTR_SIZE
168 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
169 #endif
170
171 /* Array of RTXes referenced by the debugging information, which therefore
172    must be kept around forever.  */
173 static GTY(()) VEC(rtx,gc) *used_rtx_array;
174
175 /* A pointer to the base of a list of incomplete types which might be
176    completed at some later time.  incomplete_types_list needs to be a
177    VEC(tree,gc) because we want to tell the garbage collector about
178    it.  */
179 static GTY(()) VEC(tree,gc) *incomplete_types;
180
181 /* A pointer to the base of a table of references to declaration
182    scopes.  This table is a display which tracks the nesting
183    of declaration scopes at the current scope and containing
184    scopes.  This table is used to find the proper place to
185    define type declaration DIE's.  */
186 static GTY(()) VEC(tree,gc) *decl_scope_table;
187
188 /* Pointers to various DWARF2 sections.  */
189 static GTY(()) section *debug_info_section;
190 static GTY(()) section *debug_abbrev_section;
191 static GTY(()) section *debug_aranges_section;
192 static GTY(()) section *debug_macinfo_section;
193 static GTY(()) section *debug_line_section;
194 static GTY(()) section *debug_loc_section;
195 static GTY(()) section *debug_pubnames_section;
196 static GTY(()) section *debug_pubtypes_section;
197 static GTY(()) section *debug_str_section;
198 static GTY(()) section *debug_ranges_section;
199 static GTY(()) section *debug_frame_section;
200
201 /* How to start an assembler comment.  */
202 #ifndef ASM_COMMENT_START
203 #define ASM_COMMENT_START ";#"
204 #endif
205
206 typedef struct dw_cfi_struct *dw_cfi_ref;
207 typedef struct dw_fde_struct *dw_fde_ref;
208 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
209
210 /* Call frames are described using a sequence of Call Frame
211    Information instructions.  The register number, offset
212    and address fields are provided as possible operands;
213    their use is selected by the opcode field.  */
214
215 enum dw_cfi_oprnd_type {
216   dw_cfi_oprnd_unused,
217   dw_cfi_oprnd_reg_num,
218   dw_cfi_oprnd_offset,
219   dw_cfi_oprnd_addr,
220   dw_cfi_oprnd_loc
221 };
222
223 typedef union GTY(()) dw_cfi_oprnd_struct {
224   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
225   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
226   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
227   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
228 }
229 dw_cfi_oprnd;
230
231 typedef struct GTY(()) dw_cfi_struct {
232   dw_cfi_ref dw_cfi_next;
233   enum dwarf_call_frame_info dw_cfi_opc;
234   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
235     dw_cfi_oprnd1;
236   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
237     dw_cfi_oprnd2;
238 }
239 dw_cfi_node;
240
241 /* This is how we define the location of the CFA. We use to handle it
242    as REG + OFFSET all the time,  but now it can be more complex.
243    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
244    Instead of passing around REG and OFFSET, we pass a copy
245    of this structure.  */
246 typedef struct GTY(()) cfa_loc {
247   HOST_WIDE_INT offset;
248   HOST_WIDE_INT base_offset;
249   unsigned int reg;
250   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
251   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
252 } dw_cfa_location;
253
254 /* All call frame descriptions (FDE's) in the GCC generated DWARF
255    refer to a single Common Information Entry (CIE), defined at
256    the beginning of the .debug_frame section.  This use of a single
257    CIE obviates the need to keep track of multiple CIE's
258    in the DWARF generation routines below.  */
259
260 typedef struct GTY(()) dw_fde_struct {
261   tree decl;
262   const char *dw_fde_begin;
263   const char *dw_fde_current_label;
264   const char *dw_fde_end;
265   const char *dw_fde_hot_section_label;
266   const char *dw_fde_hot_section_end_label;
267   const char *dw_fde_unlikely_section_label;
268   const char *dw_fde_unlikely_section_end_label;
269   bool dw_fde_switched_sections;
270   dw_cfi_ref dw_fde_cfi;
271   unsigned funcdef_number;
272   HOST_WIDE_INT stack_realignment;
273   /* Dynamic realign argument pointer register.  */
274   unsigned int drap_reg;
275   /* Virtual dynamic realign argument pointer register.  */
276   unsigned int vdrap_reg;
277   unsigned all_throwers_are_sibcalls : 1;
278   unsigned nothrow : 1;
279   unsigned uses_eh_lsda : 1;
280   /* Whether we did stack realign in this call frame.  */
281   unsigned stack_realign : 1;
282   /* Whether dynamic realign argument pointer register has been saved.  */
283   unsigned drap_reg_saved: 1;
284 }
285 dw_fde_node;
286
287 /* Maximum size (in bytes) of an artificially generated label.  */
288 #define MAX_ARTIFICIAL_LABEL_BYTES      30
289
290 /* The size of addresses as they appear in the Dwarf 2 data.
291    Some architectures use word addresses to refer to code locations,
292    but Dwarf 2 info always uses byte addresses.  On such machines,
293    Dwarf 2 addresses need to be larger than the architecture's
294    pointers.  */
295 #ifndef DWARF2_ADDR_SIZE
296 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
297 #endif
298
299 /* The size in bytes of a DWARF field indicating an offset or length
300    relative to a debug info section, specified to be 4 bytes in the
301    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
302    as PTR_SIZE.  */
303
304 #ifndef DWARF_OFFSET_SIZE
305 #define DWARF_OFFSET_SIZE 4
306 #endif
307
308 /* According to the (draft) DWARF 3 specification, the initial length
309    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
310    bytes are 0xffffffff, followed by the length stored in the next 8
311    bytes.
312
313    However, the SGI/MIPS ABI uses an initial length which is equal to
314    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
315
316 #ifndef DWARF_INITIAL_LENGTH_SIZE
317 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
318 #endif
319
320 #define DWARF_VERSION 2
321
322 /* Round SIZE up to the nearest BOUNDARY.  */
323 #define DWARF_ROUND(SIZE,BOUNDARY) \
324   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
325
326 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
327 #ifndef DWARF_CIE_DATA_ALIGNMENT
328 #ifdef STACK_GROWS_DOWNWARD
329 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
330 #else
331 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
332 #endif
333 #endif
334
335 /* CIE identifier.  */
336 #if HOST_BITS_PER_WIDE_INT >= 64
337 #define DWARF_CIE_ID \
338   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
339 #else
340 #define DWARF_CIE_ID DW_CIE_ID
341 #endif
342
343 /* A pointer to the base of a table that contains frame description
344    information for each routine.  */
345 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
346
347 /* Number of elements currently allocated for fde_table.  */
348 static GTY(()) unsigned fde_table_allocated;
349
350 /* Number of elements in fde_table currently in use.  */
351 static GTY(()) unsigned fde_table_in_use;
352
353 /* Size (in elements) of increments by which we may expand the
354    fde_table.  */
355 #define FDE_TABLE_INCREMENT 256
356
357 /* Get the current fde_table entry we should use.  */
358
359 static inline dw_fde_ref
360 current_fde (void)
361 {
362   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
363 }
364
365 /* A list of call frame insns for the CIE.  */
366 static GTY(()) dw_cfi_ref cie_cfi_head;
367
368 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
369 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
370    attribute that accelerates the lookup of the FDE associated
371    with the subprogram.  This variable holds the table index of the FDE
372    associated with the current function (body) definition.  */
373 static unsigned current_funcdef_fde;
374 #endif
375
376 struct GTY(()) indirect_string_node {
377   const char *str;
378   unsigned int refcount;
379   enum dwarf_form form;
380   char *label;
381 };
382
383 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
384
385 static GTY(()) int dw2_string_counter;
386 static GTY(()) unsigned long dwarf2out_cfi_label_num;
387
388 /* True if the compilation unit places functions in more than one section.  */
389 static GTY(()) bool have_multiple_function_sections = false;
390
391 /* Whether the default text and cold text sections have been used at all.  */
392
393 static GTY(()) bool text_section_used = false;
394 static GTY(()) bool cold_text_section_used = false;
395
396 /* The default cold text section.  */
397 static GTY(()) section *cold_text_section;
398
399 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
400
401 /* Forward declarations for functions defined in this file.  */
402
403 static char *stripattributes (const char *);
404 static const char *dwarf_cfi_name (unsigned);
405 static dw_cfi_ref new_cfi (void);
406 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
407 static void add_fde_cfi (const char *, dw_cfi_ref);
408 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
409 static void lookup_cfa (dw_cfa_location *);
410 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
411 #ifdef DWARF2_UNWIND_INFO
412 static void initial_return_save (rtx);
413 #endif
414 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
415                                           HOST_WIDE_INT);
416 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
417 static void output_cfi_directive (dw_cfi_ref);
418 static void output_call_frame_info (int);
419 static void dwarf2out_note_section_used (void);
420 static void dwarf2out_stack_adjust (rtx, bool);
421 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
422 static void flush_queued_reg_saves (void);
423 static bool clobbers_queued_reg_save (const_rtx);
424 static void dwarf2out_frame_debug_expr (rtx, const char *);
425
426 /* Support for complex CFA locations.  */
427 static void output_cfa_loc (dw_cfi_ref);
428 static void output_cfa_loc_raw (dw_cfi_ref);
429 static void get_cfa_from_loc_descr (dw_cfa_location *,
430                                     struct dw_loc_descr_struct *);
431 static struct dw_loc_descr_struct *build_cfa_loc
432   (dw_cfa_location *, HOST_WIDE_INT);
433 static struct dw_loc_descr_struct *build_cfa_aligned_loc
434   (HOST_WIDE_INT, HOST_WIDE_INT);
435 static void def_cfa_1 (const char *, dw_cfa_location *);
436
437 /* How to start an assembler comment.  */
438 #ifndef ASM_COMMENT_START
439 #define ASM_COMMENT_START ";#"
440 #endif
441
442 /* Data and reference forms for relocatable data.  */
443 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
444 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
445
446 #ifndef DEBUG_FRAME_SECTION
447 #define DEBUG_FRAME_SECTION     ".debug_frame"
448 #endif
449
450 #ifndef FUNC_BEGIN_LABEL
451 #define FUNC_BEGIN_LABEL        "LFB"
452 #endif
453
454 #ifndef FUNC_END_LABEL
455 #define FUNC_END_LABEL          "LFE"
456 #endif
457
458 #ifndef FRAME_BEGIN_LABEL
459 #define FRAME_BEGIN_LABEL       "Lframe"
460 #endif
461 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
462 #define CIE_END_LABEL           "LECIE"
463 #define FDE_LABEL               "LSFDE"
464 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
465 #define FDE_END_LABEL           "LEFDE"
466 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
467 #define LINE_NUMBER_END_LABEL   "LELT"
468 #define LN_PROLOG_AS_LABEL      "LASLTP"
469 #define LN_PROLOG_END_LABEL     "LELTP"
470 #define DIE_LABEL_PREFIX        "DW"
471
472 /* The DWARF 2 CFA column which tracks the return address.  Normally this
473    is the column for PC, or the first column after all of the hard
474    registers.  */
475 #ifndef DWARF_FRAME_RETURN_COLUMN
476 #ifdef PC_REGNUM
477 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
478 #else
479 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
480 #endif
481 #endif
482
483 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
484    default, we just provide columns for all registers.  */
485 #ifndef DWARF_FRAME_REGNUM
486 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
487 #endif
488 \f
489 /* Hook used by __throw.  */
490
491 rtx
492 expand_builtin_dwarf_sp_column (void)
493 {
494   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
495   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
496 }
497
498 /* Return a pointer to a copy of the section string name S with all
499    attributes stripped off, and an asterisk prepended (for assemble_name).  */
500
501 static inline char *
502 stripattributes (const char *s)
503 {
504   char *stripped = XNEWVEC (char, strlen (s) + 2);
505   char *p = stripped;
506
507   *p++ = '*';
508
509   while (*s && *s != ',')
510     *p++ = *s++;
511
512   *p = '\0';
513   return stripped;
514 }
515
516 /* MEM is a memory reference for the register size table, each element of
517    which has mode MODE.  Initialize column C as a return address column.  */
518
519 static void
520 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
521 {
522   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
523   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
524   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
525 }
526
527 /* Generate code to initialize the register size table.  */
528
529 void
530 expand_builtin_init_dwarf_reg_sizes (tree address)
531 {
532   unsigned int i;
533   enum machine_mode mode = TYPE_MODE (char_type_node);
534   rtx addr = expand_normal (address);
535   rtx mem = gen_rtx_MEM (BLKmode, addr);
536   bool wrote_return_column = false;
537
538   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
539     {
540       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
541
542       if (rnum < DWARF_FRAME_REGISTERS)
543         {
544           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
545           enum machine_mode save_mode = reg_raw_mode[i];
546           HOST_WIDE_INT size;
547
548           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
549             save_mode = choose_hard_reg_mode (i, 1, true);
550           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
551             {
552               if (save_mode == VOIDmode)
553                 continue;
554               wrote_return_column = true;
555             }
556           size = GET_MODE_SIZE (save_mode);
557           if (offset < 0)
558             continue;
559
560           emit_move_insn (adjust_address (mem, mode, offset),
561                           gen_int_mode (size, mode));
562         }
563     }
564
565   if (!wrote_return_column)
566     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
567
568 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
569   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
570 #endif
571
572   targetm.init_dwarf_reg_sizes_extra (address);
573 }
574
575 /* Convert a DWARF call frame info. operation to its string name */
576
577 static const char *
578 dwarf_cfi_name (unsigned int cfi_opc)
579 {
580   switch (cfi_opc)
581     {
582     case DW_CFA_advance_loc:
583       return "DW_CFA_advance_loc";
584     case DW_CFA_offset:
585       return "DW_CFA_offset";
586     case DW_CFA_restore:
587       return "DW_CFA_restore";
588     case DW_CFA_nop:
589       return "DW_CFA_nop";
590     case DW_CFA_set_loc:
591       return "DW_CFA_set_loc";
592     case DW_CFA_advance_loc1:
593       return "DW_CFA_advance_loc1";
594     case DW_CFA_advance_loc2:
595       return "DW_CFA_advance_loc2";
596     case DW_CFA_advance_loc4:
597       return "DW_CFA_advance_loc4";
598     case DW_CFA_offset_extended:
599       return "DW_CFA_offset_extended";
600     case DW_CFA_restore_extended:
601       return "DW_CFA_restore_extended";
602     case DW_CFA_undefined:
603       return "DW_CFA_undefined";
604     case DW_CFA_same_value:
605       return "DW_CFA_same_value";
606     case DW_CFA_register:
607       return "DW_CFA_register";
608     case DW_CFA_remember_state:
609       return "DW_CFA_remember_state";
610     case DW_CFA_restore_state:
611       return "DW_CFA_restore_state";
612     case DW_CFA_def_cfa:
613       return "DW_CFA_def_cfa";
614     case DW_CFA_def_cfa_register:
615       return "DW_CFA_def_cfa_register";
616     case DW_CFA_def_cfa_offset:
617       return "DW_CFA_def_cfa_offset";
618
619     /* DWARF 3 */
620     case DW_CFA_def_cfa_expression:
621       return "DW_CFA_def_cfa_expression";
622     case DW_CFA_expression:
623       return "DW_CFA_expression";
624     case DW_CFA_offset_extended_sf:
625       return "DW_CFA_offset_extended_sf";
626     case DW_CFA_def_cfa_sf:
627       return "DW_CFA_def_cfa_sf";
628     case DW_CFA_def_cfa_offset_sf:
629       return "DW_CFA_def_cfa_offset_sf";
630
631     /* SGI/MIPS specific */
632     case DW_CFA_MIPS_advance_loc8:
633       return "DW_CFA_MIPS_advance_loc8";
634
635     /* GNU extensions */
636     case DW_CFA_GNU_window_save:
637       return "DW_CFA_GNU_window_save";
638     case DW_CFA_GNU_args_size:
639       return "DW_CFA_GNU_args_size";
640     case DW_CFA_GNU_negative_offset_extended:
641       return "DW_CFA_GNU_negative_offset_extended";
642
643     default:
644       return "DW_CFA_<unknown>";
645     }
646 }
647
648 /* Return a pointer to a newly allocated Call Frame Instruction.  */
649
650 static inline dw_cfi_ref
651 new_cfi (void)
652 {
653   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
654
655   cfi->dw_cfi_next = NULL;
656   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
657   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
658
659   return cfi;
660 }
661
662 /* Add a Call Frame Instruction to list of instructions.  */
663
664 static inline void
665 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
666 {
667   dw_cfi_ref *p;
668   dw_fde_ref fde = current_fde ();
669
670   /* When DRAP is used, CFA is defined with an expression.  Redefine
671      CFA may lead to a different CFA value.   */
672   /* ??? Of course, this heuristic fails when we're annotating epilogues,
673      because of course we'll always want to redefine the CFA back to the
674      stack pointer on the way out.  Where should we move this check?  */
675   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
676     switch (cfi->dw_cfi_opc)
677       {
678         case DW_CFA_def_cfa_register:
679         case DW_CFA_def_cfa_offset:
680         case DW_CFA_def_cfa_offset_sf:
681         case DW_CFA_def_cfa:
682         case DW_CFA_def_cfa_sf:
683           gcc_unreachable ();
684
685         default:
686           break;
687       }
688
689   /* Find the end of the chain.  */
690   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
691     ;
692
693   *p = cfi;
694 }
695
696 /* Generate a new label for the CFI info to refer to.  FORCE is true
697    if a label needs to be output even when using .cfi_* directives.  */
698
699 char *
700 dwarf2out_cfi_label (bool force)
701 {
702   static char label[20];
703
704   if (!force && dwarf2out_do_cfi_asm ())
705     {
706       /* In this case, we will be emitting the asm directive instead of
707          the label, so just return a placeholder to keep the rest of the
708          interfaces happy.  */
709       strcpy (label, "<do not output>");
710     }
711   else
712     {
713       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
714       ASM_OUTPUT_LABEL (asm_out_file, label);
715     }
716
717   return label;
718 }
719
720 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
721    or to the CIE if LABEL is NULL.  */
722
723 static void
724 add_fde_cfi (const char *label, dw_cfi_ref cfi)
725 {
726   dw_cfi_ref *list_head = &cie_cfi_head;
727
728   if (dwarf2out_do_cfi_asm ())
729     {
730       if (label)
731         {
732           dw_fde_ref fde = current_fde ();
733
734           gcc_assert (fde != NULL);
735
736           /* We still have to add the cfi to the list so that
737              lookup_cfa works later on.  When -g2 and above we
738              even need to force emitting of CFI labels and
739              add to list a DW_CFA_set_loc for convert_cfa_to_fb_loc_list
740              purposes.  */
741           switch (cfi->dw_cfi_opc)
742             {
743             case DW_CFA_def_cfa_offset:
744             case DW_CFA_def_cfa_offset_sf:
745             case DW_CFA_def_cfa_register:
746             case DW_CFA_def_cfa:
747             case DW_CFA_def_cfa_sf:
748             case DW_CFA_def_cfa_expression:
749             case DW_CFA_restore_state:
750               if (write_symbols != DWARF2_DEBUG
751                   && write_symbols != VMS_AND_DWARF2_DEBUG)
752                 break;
753               if (debug_info_level <= DINFO_LEVEL_TERSE)
754                 break;
755
756               if (*label == 0 || strcmp (label, "<do not output>") == 0)
757                 label = dwarf2out_cfi_label (true);
758
759               if (fde->dw_fde_current_label == NULL
760                   || strcmp (label, fde->dw_fde_current_label) != 0)
761                 {
762                   dw_cfi_ref xcfi;
763
764                   label = xstrdup (label);
765
766                   /* Set the location counter to the new label.  */
767                   xcfi = new_cfi ();
768                   /* It doesn't metter whether DW_CFA_set_loc
769                      or DW_CFA_advance_loc4 is added here, those aren't
770                      emitted into assembly, only looked up by
771                      convert_cfa_to_fb_loc_list.  */
772                   xcfi->dw_cfi_opc = DW_CFA_set_loc;
773                   xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
774                   add_cfi (&fde->dw_fde_cfi, xcfi);
775                   fde->dw_fde_current_label = label;
776                 }
777               break;
778             default:
779               break;
780             }
781
782           output_cfi_directive (cfi);
783
784           list_head = &fde->dw_fde_cfi;
785         }
786       /* ??? If this is a CFI for the CIE, we don't emit.  This
787          assumes that the standard CIE contents that the assembler
788          uses matches the standard CIE contents that the compiler
789          uses.  This is probably a bad assumption.  I'm not quite
790          sure how to address this for now.  */
791     }
792   else if (label)
793     {
794       dw_fde_ref fde = current_fde ();
795
796       gcc_assert (fde != NULL);
797
798       if (*label == 0)
799         label = dwarf2out_cfi_label (false);
800
801       if (fde->dw_fde_current_label == NULL
802           || strcmp (label, fde->dw_fde_current_label) != 0)
803         {
804           dw_cfi_ref xcfi;
805
806           label = xstrdup (label);
807
808           /* Set the location counter to the new label.  */
809           xcfi = new_cfi ();
810           /* If we have a current label, advance from there, otherwise
811              set the location directly using set_loc.  */
812           xcfi->dw_cfi_opc = fde->dw_fde_current_label
813                              ? DW_CFA_advance_loc4
814                              : DW_CFA_set_loc;
815           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
816           add_cfi (&fde->dw_fde_cfi, xcfi);
817
818           fde->dw_fde_current_label = label;
819         }
820
821       list_head = &fde->dw_fde_cfi;
822     }
823
824   add_cfi (list_head, cfi);
825 }
826
827 /* Subroutine of lookup_cfa.  */
828
829 static void
830 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
831 {
832   switch (cfi->dw_cfi_opc)
833     {
834     case DW_CFA_def_cfa_offset:
835     case DW_CFA_def_cfa_offset_sf:
836       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
837       break;
838     case DW_CFA_def_cfa_register:
839       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
840       break;
841     case DW_CFA_def_cfa:
842     case DW_CFA_def_cfa_sf:
843       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
844       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
845       break;
846     case DW_CFA_def_cfa_expression:
847       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
848       break;
849
850     case DW_CFA_remember_state:
851       gcc_assert (!remember->in_use);
852       *remember = *loc;
853       remember->in_use = 1;
854       break;
855     case DW_CFA_restore_state:
856       gcc_assert (remember->in_use);
857       *loc = *remember;
858       remember->in_use = 0;
859       break;
860
861     default:
862       break;
863     }
864 }
865
866 /* Find the previous value for the CFA.  */
867
868 static void
869 lookup_cfa (dw_cfa_location *loc)
870 {
871   dw_cfi_ref cfi;
872   dw_fde_ref fde;
873   dw_cfa_location remember;
874
875   memset (loc, 0, sizeof (*loc));
876   loc->reg = INVALID_REGNUM;
877   remember = *loc;
878
879   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
880     lookup_cfa_1 (cfi, loc, &remember);
881
882   fde = current_fde ();
883   if (fde)
884     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
885       lookup_cfa_1 (cfi, loc, &remember);
886 }
887
888 /* The current rule for calculating the DWARF2 canonical frame address.  */
889 static dw_cfa_location cfa;
890
891 /* The register used for saving registers to the stack, and its offset
892    from the CFA.  */
893 static dw_cfa_location cfa_store;
894
895 /* The current save location around an epilogue.  */
896 static dw_cfa_location cfa_remember;
897
898 /* The running total of the size of arguments pushed onto the stack.  */
899 static HOST_WIDE_INT args_size;
900
901 /* The last args_size we actually output.  */
902 static HOST_WIDE_INT old_args_size;
903
904 /* Entry point to update the canonical frame address (CFA).
905    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
906    calculated from REG+OFFSET.  */
907
908 void
909 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
910 {
911   dw_cfa_location loc;
912   loc.indirect = 0;
913   loc.base_offset = 0;
914   loc.reg = reg;
915   loc.offset = offset;
916   def_cfa_1 (label, &loc);
917 }
918
919 /* Determine if two dw_cfa_location structures define the same data.  */
920
921 static bool
922 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
923 {
924   return (loc1->reg == loc2->reg
925           && loc1->offset == loc2->offset
926           && loc1->indirect == loc2->indirect
927           && (loc1->indirect == 0
928               || loc1->base_offset == loc2->base_offset));
929 }
930
931 /* This routine does the actual work.  The CFA is now calculated from
932    the dw_cfa_location structure.  */
933
934 static void
935 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
936 {
937   dw_cfi_ref cfi;
938   dw_cfa_location old_cfa, loc;
939
940   cfa = *loc_p;
941   loc = *loc_p;
942
943   if (cfa_store.reg == loc.reg && loc.indirect == 0)
944     cfa_store.offset = loc.offset;
945
946   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
947   lookup_cfa (&old_cfa);
948
949   /* If nothing changed, no need to issue any call frame instructions.  */
950   if (cfa_equal_p (&loc, &old_cfa))
951     return;
952
953   cfi = new_cfi ();
954
955   if (loc.reg == old_cfa.reg && !loc.indirect)
956     {
957       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
958          the CFA register did not change but the offset did.  The data 
959          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
960          in the assembler via the .cfi_def_cfa_offset directive.  */
961       if (loc.offset < 0)
962         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
963       else
964         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
965       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
966     }
967
968 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
969   else if (loc.offset == old_cfa.offset
970            && old_cfa.reg != INVALID_REGNUM
971            && !loc.indirect)
972     {
973       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
974          indicating the CFA register has changed to <register> but the
975          offset has not changed.  */
976       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
977       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
978     }
979 #endif
980
981   else if (loc.indirect == 0)
982     {
983       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
984          indicating the CFA register has changed to <register> with
985          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
986          happens in output_cfi, or in the assembler via the .cfi_def_cfa
987          directive.  */
988       if (loc.offset < 0)
989         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
990       else
991         cfi->dw_cfi_opc = DW_CFA_def_cfa;
992       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
993       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
994     }
995   else
996     {
997       /* Construct a DW_CFA_def_cfa_expression instruction to
998          calculate the CFA using a full location expression since no
999          register-offset pair is available.  */
1000       struct dw_loc_descr_struct *loc_list;
1001
1002       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1003       loc_list = build_cfa_loc (&loc, 0);
1004       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1005     }
1006
1007   add_fde_cfi (label, cfi);
1008 }
1009
1010 /* Add the CFI for saving a register.  REG is the CFA column number.
1011    LABEL is passed to add_fde_cfi.
1012    If SREG is -1, the register is saved at OFFSET from the CFA;
1013    otherwise it is saved in SREG.  */
1014
1015 static void
1016 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1017 {
1018   dw_cfi_ref cfi = new_cfi ();
1019   dw_fde_ref fde = current_fde ();
1020
1021   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1022
1023   /* When stack is aligned, store REG using DW_CFA_expression with
1024      FP.  */
1025   if (fde
1026       && fde->stack_realign
1027       && sreg == INVALID_REGNUM)
1028     {
1029       cfi->dw_cfi_opc = DW_CFA_expression;
1030       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1031       cfi->dw_cfi_oprnd1.dw_cfi_loc
1032         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1033     }
1034   else if (sreg == INVALID_REGNUM)
1035     {
1036       if (offset < 0)
1037         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1038       else if (reg & ~0x3f)
1039         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1040       else
1041         cfi->dw_cfi_opc = DW_CFA_offset;
1042       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1043     }
1044   else if (sreg == reg)
1045     cfi->dw_cfi_opc = DW_CFA_same_value;
1046   else
1047     {
1048       cfi->dw_cfi_opc = DW_CFA_register;
1049       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1050     }
1051
1052   add_fde_cfi (label, cfi);
1053 }
1054
1055 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1056    This CFI tells the unwinder that it needs to restore the window registers
1057    from the previous frame's window save area.
1058
1059    ??? Perhaps we should note in the CIE where windows are saved (instead of
1060    assuming 0(cfa)) and what registers are in the window.  */
1061
1062 void
1063 dwarf2out_window_save (const char *label)
1064 {
1065   dw_cfi_ref cfi = new_cfi ();
1066
1067   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1068   add_fde_cfi (label, cfi);
1069 }
1070
1071 /* Add a CFI to update the running total of the size of arguments
1072    pushed onto the stack.  */
1073
1074 void
1075 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1076 {
1077   dw_cfi_ref cfi;
1078
1079   if (size == old_args_size)
1080     return;
1081
1082   old_args_size = size;
1083
1084   cfi = new_cfi ();
1085   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1086   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1087   add_fde_cfi (label, cfi);
1088 }
1089
1090 /* Entry point for saving a register to the stack.  REG is the GCC register
1091    number.  LABEL and OFFSET are passed to reg_save.  */
1092
1093 void
1094 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1095 {
1096   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1097 }
1098
1099 /* Entry point for saving the return address in the stack.
1100    LABEL and OFFSET are passed to reg_save.  */
1101
1102 void
1103 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1104 {
1105   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1106 }
1107
1108 /* Entry point for saving the return address in a register.
1109    LABEL and SREG are passed to reg_save.  */
1110
1111 void
1112 dwarf2out_return_reg (const char *label, unsigned int sreg)
1113 {
1114   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1115 }
1116
1117 #ifdef DWARF2_UNWIND_INFO
1118 /* Record the initial position of the return address.  RTL is
1119    INCOMING_RETURN_ADDR_RTX.  */
1120
1121 static void
1122 initial_return_save (rtx rtl)
1123 {
1124   unsigned int reg = INVALID_REGNUM;
1125   HOST_WIDE_INT offset = 0;
1126
1127   switch (GET_CODE (rtl))
1128     {
1129     case REG:
1130       /* RA is in a register.  */
1131       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1132       break;
1133
1134     case MEM:
1135       /* RA is on the stack.  */
1136       rtl = XEXP (rtl, 0);
1137       switch (GET_CODE (rtl))
1138         {
1139         case REG:
1140           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1141           offset = 0;
1142           break;
1143
1144         case PLUS:
1145           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1146           offset = INTVAL (XEXP (rtl, 1));
1147           break;
1148
1149         case MINUS:
1150           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1151           offset = -INTVAL (XEXP (rtl, 1));
1152           break;
1153
1154         default:
1155           gcc_unreachable ();
1156         }
1157
1158       break;
1159
1160     case PLUS:
1161       /* The return address is at some offset from any value we can
1162          actually load.  For instance, on the SPARC it is in %i7+8. Just
1163          ignore the offset for now; it doesn't matter for unwinding frames.  */
1164       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1165       initial_return_save (XEXP (rtl, 0));
1166       return;
1167
1168     default:
1169       gcc_unreachable ();
1170     }
1171
1172   if (reg != DWARF_FRAME_RETURN_COLUMN)
1173     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1174 }
1175 #endif
1176
1177 /* Given a SET, calculate the amount of stack adjustment it
1178    contains.  */
1179
1180 static HOST_WIDE_INT
1181 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1182                      HOST_WIDE_INT cur_offset)
1183 {
1184   const_rtx src = SET_SRC (pattern);
1185   const_rtx dest = SET_DEST (pattern);
1186   HOST_WIDE_INT offset = 0;
1187   enum rtx_code code;
1188
1189   if (dest == stack_pointer_rtx)
1190     {
1191       code = GET_CODE (src);
1192
1193       /* Assume (set (reg sp) (reg whatever)) sets args_size
1194          level to 0.  */
1195       if (code == REG && src != stack_pointer_rtx)
1196         {
1197           offset = -cur_args_size;
1198 #ifndef STACK_GROWS_DOWNWARD
1199           offset = -offset;
1200 #endif
1201           return offset - cur_offset;
1202         }
1203
1204       if (! (code == PLUS || code == MINUS)
1205           || XEXP (src, 0) != stack_pointer_rtx
1206           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1207         return 0;
1208
1209       /* (set (reg sp) (plus (reg sp) (const_int))) */
1210       offset = INTVAL (XEXP (src, 1));
1211       if (code == PLUS)
1212         offset = -offset;
1213       return offset;
1214     }
1215
1216   if (MEM_P (src) && !MEM_P (dest))
1217     dest = src;
1218   if (MEM_P (dest))
1219     {
1220       /* (set (mem (pre_dec (reg sp))) (foo)) */
1221       src = XEXP (dest, 0);
1222       code = GET_CODE (src);
1223
1224       switch (code)
1225         {
1226         case PRE_MODIFY:
1227         case POST_MODIFY:
1228           if (XEXP (src, 0) == stack_pointer_rtx)
1229             {
1230               rtx val = XEXP (XEXP (src, 1), 1);
1231               /* We handle only adjustments by constant amount.  */
1232               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1233                           && GET_CODE (val) == CONST_INT);
1234               offset = -INTVAL (val);
1235               break;
1236             }
1237           return 0;
1238
1239         case PRE_DEC:
1240         case POST_DEC:
1241           if (XEXP (src, 0) == stack_pointer_rtx)
1242             {
1243               offset = GET_MODE_SIZE (GET_MODE (dest));
1244               break;
1245             }
1246           return 0;
1247
1248         case PRE_INC:
1249         case POST_INC:
1250           if (XEXP (src, 0) == stack_pointer_rtx)
1251             {
1252               offset = -GET_MODE_SIZE (GET_MODE (dest));
1253               break;
1254             }
1255           return 0;
1256
1257         default:
1258           return 0;
1259         }
1260     }
1261   else
1262     return 0;
1263
1264   return offset;
1265 }
1266
1267 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1268    indexed by INSN_UID.  */
1269
1270 static HOST_WIDE_INT *barrier_args_size;
1271
1272 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1273
1274 static HOST_WIDE_INT
1275 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1276                              VEC (rtx, heap) **next)
1277 {
1278   HOST_WIDE_INT offset = 0;
1279   int i;
1280
1281   if (! RTX_FRAME_RELATED_P (insn))
1282     {
1283       if (prologue_epilogue_contains (insn))
1284         /* Nothing */;
1285       else if (GET_CODE (PATTERN (insn)) == SET)
1286         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1287       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1288                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1289         {
1290           /* There may be stack adjustments inside compound insns.  Search
1291              for them.  */
1292           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1293             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1294               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1295                                              cur_args_size, offset);
1296         }
1297     }
1298   else
1299     {
1300       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1301
1302       if (expr)
1303         {
1304           expr = XEXP (expr, 0);
1305           if (GET_CODE (expr) == PARALLEL
1306               || GET_CODE (expr) == SEQUENCE)
1307             for (i = 1; i < XVECLEN (expr, 0); i++)
1308               {
1309                 rtx elem = XVECEXP (expr, 0, i);
1310
1311                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1312                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1313               }
1314         }
1315     }
1316
1317 #ifndef STACK_GROWS_DOWNWARD
1318   offset = -offset;
1319 #endif
1320
1321   cur_args_size += offset;
1322   if (cur_args_size < 0)
1323     cur_args_size = 0;
1324
1325   if (JUMP_P (insn))
1326     {
1327       rtx dest = JUMP_LABEL (insn);
1328
1329       if (dest)
1330         {
1331           if (barrier_args_size [INSN_UID (dest)] < 0)
1332             {
1333               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1334               VEC_safe_push (rtx, heap, *next, dest);
1335             }
1336         }
1337     }
1338
1339   return cur_args_size;
1340 }
1341
1342 /* Walk the whole function and compute args_size on BARRIERs.  */
1343
1344 static void
1345 compute_barrier_args_size (void)
1346 {
1347   int max_uid = get_max_uid (), i;
1348   rtx insn;
1349   VEC (rtx, heap) *worklist, *next, *tmp;
1350
1351   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1352   for (i = 0; i < max_uid; i++)
1353     barrier_args_size[i] = -1;
1354
1355   worklist = VEC_alloc (rtx, heap, 20);
1356   next = VEC_alloc (rtx, heap, 20);
1357   insn = get_insns ();
1358   barrier_args_size[INSN_UID (insn)] = 0;
1359   VEC_quick_push (rtx, worklist, insn);
1360   for (;;)
1361     {
1362       while (!VEC_empty (rtx, worklist))
1363         {
1364           rtx prev, body, first_insn;
1365           HOST_WIDE_INT cur_args_size;
1366
1367           first_insn = insn = VEC_pop (rtx, worklist);
1368           cur_args_size = barrier_args_size[INSN_UID (insn)];
1369           prev = prev_nonnote_insn (insn);
1370           if (prev && BARRIER_P (prev))
1371             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1372
1373           for (; insn; insn = NEXT_INSN (insn))
1374             {
1375               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1376                 continue;
1377               if (BARRIER_P (insn))
1378                 break;
1379
1380               if (LABEL_P (insn))
1381                 {
1382                   if (insn == first_insn)
1383                     continue;
1384                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1385                     {
1386                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1387                       continue;
1388                     }
1389                   else
1390                     {
1391                       /* The insns starting with this label have been
1392                          already scanned or are in the worklist.  */
1393                       break;
1394                     }
1395                 }
1396
1397               body = PATTERN (insn);
1398               if (GET_CODE (body) == SEQUENCE)
1399                 {
1400                   HOST_WIDE_INT dest_args_size = cur_args_size;
1401                   for (i = 1; i < XVECLEN (body, 0); i++)
1402                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1403                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1404                       dest_args_size
1405                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1406                                                        dest_args_size, &next);
1407                     else
1408                       cur_args_size
1409                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1410                                                        cur_args_size, &next);
1411
1412                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1413                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1414                                                  dest_args_size, &next);
1415                   else
1416                     cur_args_size
1417                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1418                                                      cur_args_size, &next);
1419                 }
1420               else
1421                 cur_args_size
1422                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1423             }
1424         }
1425
1426       if (VEC_empty (rtx, next))
1427         break;
1428
1429       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1430       tmp = next;
1431       next = worklist;
1432       worklist = tmp;
1433       VEC_truncate (rtx, next, 0);
1434     }
1435
1436   VEC_free (rtx, heap, worklist);
1437   VEC_free (rtx, heap, next);
1438 }
1439
1440
1441 /* Check INSN to see if it looks like a push or a stack adjustment, and
1442    make a note of it if it does.  EH uses this information to find out how
1443    much extra space it needs to pop off the stack.  */
1444
1445 static void
1446 dwarf2out_stack_adjust (rtx insn, bool after_p)
1447 {
1448   HOST_WIDE_INT offset;
1449   const char *label;
1450   int i;
1451
1452   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1453      with this function.  Proper support would require all frame-related
1454      insns to be marked, and to be able to handle saving state around
1455      epilogues textually in the middle of the function.  */
1456   if (prologue_epilogue_contains (insn))
1457     return;
1458
1459   /* If INSN is an instruction from target of an annulled branch, the
1460      effects are for the target only and so current argument size
1461      shouldn't change at all.  */
1462   if (final_sequence
1463       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1464       && INSN_FROM_TARGET_P (insn))
1465     return;
1466
1467   /* If only calls can throw, and we have a frame pointer,
1468      save up adjustments until we see the CALL_INSN.  */
1469   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1470     {
1471       if (CALL_P (insn) && !after_p)
1472         {
1473           /* Extract the size of the args from the CALL rtx itself.  */
1474           insn = PATTERN (insn);
1475           if (GET_CODE (insn) == PARALLEL)
1476             insn = XVECEXP (insn, 0, 0);
1477           if (GET_CODE (insn) == SET)
1478             insn = SET_SRC (insn);
1479           gcc_assert (GET_CODE (insn) == CALL);
1480           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1481         }
1482       return;
1483     }
1484
1485   if (CALL_P (insn) && !after_p)
1486     {
1487       if (!flag_asynchronous_unwind_tables)
1488         dwarf2out_args_size ("", args_size);
1489       return;
1490     }
1491   else if (BARRIER_P (insn))
1492     {
1493       /* Don't call compute_barrier_args_size () if the only
1494          BARRIER is at the end of function.  */
1495       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1496         compute_barrier_args_size ();
1497       if (barrier_args_size == NULL)
1498         offset = 0;
1499       else
1500         {
1501           offset = barrier_args_size[INSN_UID (insn)];
1502           if (offset < 0)
1503             offset = 0;
1504         }
1505
1506       offset -= args_size;
1507 #ifndef STACK_GROWS_DOWNWARD
1508       offset = -offset;
1509 #endif
1510     }
1511   else if (GET_CODE (PATTERN (insn)) == SET)
1512     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1513   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1514            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1515     {
1516       /* There may be stack adjustments inside compound insns.  Search
1517          for them.  */
1518       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1519         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1520           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1521                                          args_size, offset);
1522     }
1523   else
1524     return;
1525
1526   if (offset == 0)
1527     return;
1528
1529   label = dwarf2out_cfi_label (false);
1530   dwarf2out_args_size_adjust (offset, label);
1531 }
1532
1533 /* Adjust args_size based on stack adjustment OFFSET.  */
1534
1535 static void
1536 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1537 {
1538   if (cfa.reg == STACK_POINTER_REGNUM)
1539     cfa.offset += offset;
1540
1541   if (cfa_store.reg == STACK_POINTER_REGNUM)
1542     cfa_store.offset += offset;
1543
1544 #ifndef STACK_GROWS_DOWNWARD
1545   offset = -offset;
1546 #endif
1547
1548   args_size += offset;
1549   if (args_size < 0)
1550     args_size = 0;
1551
1552   def_cfa_1 (label, &cfa);
1553   if (flag_asynchronous_unwind_tables)
1554     dwarf2out_args_size (label, args_size);
1555 }
1556
1557 #endif
1558
1559 /* We delay emitting a register save until either (a) we reach the end
1560    of the prologue or (b) the register is clobbered.  This clusters
1561    register saves so that there are fewer pc advances.  */
1562
1563 struct GTY(()) queued_reg_save {
1564   struct queued_reg_save *next;
1565   rtx reg;
1566   HOST_WIDE_INT cfa_offset;
1567   rtx saved_reg;
1568 };
1569
1570 static GTY(()) struct queued_reg_save *queued_reg_saves;
1571
1572 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1573 struct GTY(()) reg_saved_in_data {
1574   rtx orig_reg;
1575   rtx saved_in_reg;
1576 };
1577
1578 /* A list of registers saved in other registers.
1579    The list intentionally has a small maximum capacity of 4; if your
1580    port needs more than that, you might consider implementing a
1581    more efficient data structure.  */
1582 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1583 static GTY(()) size_t num_regs_saved_in_regs;
1584
1585 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1586 static const char *last_reg_save_label;
1587
1588 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1589    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1590
1591 static void
1592 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1593 {
1594   struct queued_reg_save *q;
1595
1596   /* Duplicates waste space, but it's also necessary to remove them
1597      for correctness, since the queue gets output in reverse
1598      order.  */
1599   for (q = queued_reg_saves; q != NULL; q = q->next)
1600     if (REGNO (q->reg) == REGNO (reg))
1601       break;
1602
1603   if (q == NULL)
1604     {
1605       q = GGC_NEW (struct queued_reg_save);
1606       q->next = queued_reg_saves;
1607       queued_reg_saves = q;
1608     }
1609
1610   q->reg = reg;
1611   q->cfa_offset = offset;
1612   q->saved_reg = sreg;
1613
1614   last_reg_save_label = label;
1615 }
1616
1617 /* Output all the entries in QUEUED_REG_SAVES.  */
1618
1619 static void
1620 flush_queued_reg_saves (void)
1621 {
1622   struct queued_reg_save *q;
1623
1624   for (q = queued_reg_saves; q; q = q->next)
1625     {
1626       size_t i;
1627       unsigned int reg, sreg;
1628
1629       for (i = 0; i < num_regs_saved_in_regs; i++)
1630         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1631           break;
1632       if (q->saved_reg && i == num_regs_saved_in_regs)
1633         {
1634           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1635           num_regs_saved_in_regs++;
1636         }
1637       if (i != num_regs_saved_in_regs)
1638         {
1639           regs_saved_in_regs[i].orig_reg = q->reg;
1640           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1641         }
1642
1643       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1644       if (q->saved_reg)
1645         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1646       else
1647         sreg = INVALID_REGNUM;
1648       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1649     }
1650
1651   queued_reg_saves = NULL;
1652   last_reg_save_label = NULL;
1653 }
1654
1655 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1656    location for?  Or, does it clobber a register which we've previously
1657    said that some other register is saved in, and for which we now
1658    have a new location for?  */
1659
1660 static bool
1661 clobbers_queued_reg_save (const_rtx insn)
1662 {
1663   struct queued_reg_save *q;
1664
1665   for (q = queued_reg_saves; q; q = q->next)
1666     {
1667       size_t i;
1668       if (modified_in_p (q->reg, insn))
1669         return true;
1670       for (i = 0; i < num_regs_saved_in_regs; i++)
1671         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1672             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1673           return true;
1674     }
1675
1676   return false;
1677 }
1678
1679 /* Entry point for saving the first register into the second.  */
1680
1681 void
1682 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1683 {
1684   size_t i;
1685   unsigned int regno, sregno;
1686
1687   for (i = 0; i < num_regs_saved_in_regs; i++)
1688     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1689       break;
1690   if (i == num_regs_saved_in_regs)
1691     {
1692       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1693       num_regs_saved_in_regs++;
1694     }
1695   regs_saved_in_regs[i].orig_reg = reg;
1696   regs_saved_in_regs[i].saved_in_reg = sreg;
1697
1698   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1699   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1700   reg_save (label, regno, sregno, 0);
1701 }
1702
1703 /* What register, if any, is currently saved in REG?  */
1704
1705 static rtx
1706 reg_saved_in (rtx reg)
1707 {
1708   unsigned int regn = REGNO (reg);
1709   size_t i;
1710   struct queued_reg_save *q;
1711
1712   for (q = queued_reg_saves; q; q = q->next)
1713     if (q->saved_reg && regn == REGNO (q->saved_reg))
1714       return q->reg;
1715
1716   for (i = 0; i < num_regs_saved_in_regs; i++)
1717     if (regs_saved_in_regs[i].saved_in_reg
1718         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1719       return regs_saved_in_regs[i].orig_reg;
1720
1721   return NULL_RTX;
1722 }
1723
1724
1725 /* A temporary register holding an integral value used in adjusting SP
1726    or setting up the store_reg.  The "offset" field holds the integer
1727    value, not an offset.  */
1728 static dw_cfa_location cfa_temp;
1729
1730 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1731
1732 static void
1733 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1734 {
1735   memset (&cfa, 0, sizeof (cfa));
1736
1737   switch (GET_CODE (pat))
1738     {
1739     case PLUS:
1740       cfa.reg = REGNO (XEXP (pat, 0));
1741       cfa.offset = INTVAL (XEXP (pat, 1));
1742       break;
1743
1744     case REG:
1745       cfa.reg = REGNO (pat);
1746       break;
1747
1748     default:
1749       /* Recurse and define an expression.  */
1750       gcc_unreachable ();
1751     }
1752
1753   def_cfa_1 (label, &cfa);
1754 }
1755
1756 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1757
1758 static void
1759 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1760 {
1761   rtx src, dest;
1762
1763   gcc_assert (GET_CODE (pat) == SET);
1764   dest = XEXP (pat, 0);
1765   src = XEXP (pat, 1);
1766
1767   switch (GET_CODE (src))
1768     {
1769     case PLUS:
1770       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1771       cfa.offset -= INTVAL (XEXP (src, 1));
1772       break;
1773
1774     case REG:
1775         break;
1776
1777     default:
1778         gcc_unreachable ();
1779     }
1780
1781   cfa.reg = REGNO (dest);
1782   gcc_assert (cfa.indirect == 0);
1783
1784   def_cfa_1 (label, &cfa);
1785 }
1786
1787 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1788
1789 static void
1790 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1791 {
1792   HOST_WIDE_INT offset;
1793   rtx src, addr, span;
1794
1795   src = XEXP (set, 1);
1796   addr = XEXP (set, 0);
1797   gcc_assert (MEM_P (addr));
1798   addr = XEXP (addr, 0);
1799   
1800   /* As documented, only consider extremely simple addresses.  */
1801   switch (GET_CODE (addr))
1802     {
1803     case REG:
1804       gcc_assert (REGNO (addr) == cfa.reg);
1805       offset = -cfa.offset;
1806       break;
1807     case PLUS:
1808       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1809       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1810       break;
1811     default:
1812       gcc_unreachable ();
1813     }
1814
1815   span = targetm.dwarf_register_span (src);
1816
1817   /* ??? We'd like to use queue_reg_save, but we need to come up with
1818      a different flushing heuristic for epilogues.  */
1819   if (!span)
1820     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1821   else
1822     {
1823       /* We have a PARALLEL describing where the contents of SRC live.
1824          Queue register saves for each piece of the PARALLEL.  */
1825       int par_index;
1826       int limit;
1827       HOST_WIDE_INT span_offset = offset;
1828
1829       gcc_assert (GET_CODE (span) == PARALLEL);
1830
1831       limit = XVECLEN (span, 0);
1832       for (par_index = 0; par_index < limit; par_index++)
1833         {
1834           rtx elem = XVECEXP (span, 0, par_index);
1835
1836           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1837                     INVALID_REGNUM, span_offset);
1838           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1839         }
1840     }
1841 }
1842
1843 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1844
1845 static void
1846 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1847 {
1848   rtx src, dest;
1849   unsigned sregno, dregno;
1850
1851   src = XEXP (set, 1);
1852   dest = XEXP (set, 0);
1853
1854   if (src == pc_rtx)
1855     sregno = DWARF_FRAME_RETURN_COLUMN;
1856   else
1857     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1858
1859   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1860
1861   /* ??? We'd like to use queue_reg_save, but we need to come up with
1862      a different flushing heuristic for epilogues.  */
1863   reg_save (label, sregno, dregno, 0);
1864 }
1865
1866 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1867
1868 static void
1869 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1870 {
1871   dw_cfi_ref cfi = new_cfi ();
1872   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1873
1874   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1875   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1876
1877   add_fde_cfi (label, cfi);
1878 }
1879
1880 /* Record call frame debugging information for an expression EXPR,
1881    which either sets SP or FP (adjusting how we calculate the frame
1882    address) or saves a register to the stack or another register.
1883    LABEL indicates the address of EXPR.
1884
1885    This function encodes a state machine mapping rtxes to actions on
1886    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1887    users need not read the source code.
1888
1889   The High-Level Picture
1890
1891   Changes in the register we use to calculate the CFA: Currently we
1892   assume that if you copy the CFA register into another register, we
1893   should take the other one as the new CFA register; this seems to
1894   work pretty well.  If it's wrong for some target, it's simple
1895   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1896
1897   Changes in the register we use for saving registers to the stack:
1898   This is usually SP, but not always.  Again, we deduce that if you
1899   copy SP into another register (and SP is not the CFA register),
1900   then the new register is the one we will be using for register
1901   saves.  This also seems to work.
1902
1903   Register saves: There's not much guesswork about this one; if
1904   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1905   register save, and the register used to calculate the destination
1906   had better be the one we think we're using for this purpose.
1907   It's also assumed that a copy from a call-saved register to another
1908   register is saving that register if RTX_FRAME_RELATED_P is set on
1909   that instruction.  If the copy is from a call-saved register to
1910   the *same* register, that means that the register is now the same
1911   value as in the caller.
1912
1913   Except: If the register being saved is the CFA register, and the
1914   offset is nonzero, we are saving the CFA, so we assume we have to
1915   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1916   the intent is to save the value of SP from the previous frame.
1917
1918   In addition, if a register has previously been saved to a different
1919   register,
1920
1921   Invariants / Summaries of Rules
1922
1923   cfa          current rule for calculating the CFA.  It usually
1924                consists of a register and an offset.
1925   cfa_store    register used by prologue code to save things to the stack
1926                cfa_store.offset is the offset from the value of
1927                cfa_store.reg to the actual CFA
1928   cfa_temp     register holding an integral value.  cfa_temp.offset
1929                stores the value, which will be used to adjust the
1930                stack pointer.  cfa_temp is also used like cfa_store,
1931                to track stores to the stack via fp or a temp reg.
1932
1933   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1934                with cfa.reg as the first operand changes the cfa.reg and its
1935                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1936                cfa_temp.offset.
1937
1938   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1939                expression yielding a constant.  This sets cfa_temp.reg
1940                and cfa_temp.offset.
1941
1942   Rule 5:      Create a new register cfa_store used to save items to the
1943                stack.
1944
1945   Rules 10-14: Save a register to the stack.  Define offset as the
1946                difference of the original location and cfa_store's
1947                location (or cfa_temp's location if cfa_temp is used).
1948
1949   Rules 16-20: If AND operation happens on sp in prologue, we assume
1950                stack is realigned.  We will use a group of DW_OP_XXX
1951                expressions to represent the location of the stored
1952                register instead of CFA+offset.
1953
1954   The Rules
1955
1956   "{a,b}" indicates a choice of a xor b.
1957   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1958
1959   Rule 1:
1960   (set <reg1> <reg2>:cfa.reg)
1961   effects: cfa.reg = <reg1>
1962            cfa.offset unchanged
1963            cfa_temp.reg = <reg1>
1964            cfa_temp.offset = cfa.offset
1965
1966   Rule 2:
1967   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1968                               {<const_int>,<reg>:cfa_temp.reg}))
1969   effects: cfa.reg = sp if fp used
1970            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1971            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1972              if cfa_store.reg==sp
1973
1974   Rule 3:
1975   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1976   effects: cfa.reg = fp
1977            cfa_offset += +/- <const_int>
1978
1979   Rule 4:
1980   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1981   constraints: <reg1> != fp
1982                <reg1> != sp
1983   effects: cfa.reg = <reg1>
1984            cfa_temp.reg = <reg1>
1985            cfa_temp.offset = cfa.offset
1986
1987   Rule 5:
1988   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1989   constraints: <reg1> != fp
1990                <reg1> != sp
1991   effects: cfa_store.reg = <reg1>
1992            cfa_store.offset = cfa.offset - cfa_temp.offset
1993
1994   Rule 6:
1995   (set <reg> <const_int>)
1996   effects: cfa_temp.reg = <reg>
1997            cfa_temp.offset = <const_int>
1998
1999   Rule 7:
2000   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2001   effects: cfa_temp.reg = <reg1>
2002            cfa_temp.offset |= <const_int>
2003
2004   Rule 8:
2005   (set <reg> (high <exp>))
2006   effects: none
2007
2008   Rule 9:
2009   (set <reg> (lo_sum <exp> <const_int>))
2010   effects: cfa_temp.reg = <reg>
2011            cfa_temp.offset = <const_int>
2012
2013   Rule 10:
2014   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2015   effects: cfa_store.offset -= <const_int>
2016            cfa.offset = cfa_store.offset if cfa.reg == sp
2017            cfa.reg = sp
2018            cfa.base_offset = -cfa_store.offset
2019
2020   Rule 11:
2021   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2022   effects: cfa_store.offset += -/+ mode_size(mem)
2023            cfa.offset = cfa_store.offset if cfa.reg == sp
2024            cfa.reg = sp
2025            cfa.base_offset = -cfa_store.offset
2026
2027   Rule 12:
2028   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2029
2030        <reg2>)
2031   effects: cfa.reg = <reg1>
2032            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2033
2034   Rule 13:
2035   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2036   effects: cfa.reg = <reg1>
2037            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2038
2039   Rule 14:
2040   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2041   effects: cfa.reg = <reg1>
2042            cfa.base_offset = -cfa_temp.offset
2043            cfa_temp.offset -= mode_size(mem)
2044
2045   Rule 15:
2046   (set <reg> {unspec, unspec_volatile})
2047   effects: target-dependent
2048
2049   Rule 16:
2050   (set sp (and: sp <const_int>))
2051   constraints: cfa_store.reg == sp
2052   effects: current_fde.stack_realign = 1
2053            cfa_store.offset = 0
2054            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2055
2056   Rule 17:
2057   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2058   effects: cfa_store.offset += -/+ mode_size(mem)
2059
2060   Rule 18:
2061   (set (mem ({pre_inc, pre_dec} sp)) fp)
2062   constraints: fde->stack_realign == 1
2063   effects: cfa_store.offset = 0
2064            cfa.reg != HARD_FRAME_POINTER_REGNUM
2065
2066   Rule 19:
2067   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2068   constraints: fde->stack_realign == 1
2069                && cfa.offset == 0
2070                && cfa.indirect == 0
2071                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2072   effects: Use DW_CFA_def_cfa_expression to define cfa
2073            cfa.reg == fde->drap_reg
2074
2075   Rule 20:
2076   (set reg fde->drap_reg)
2077   constraints: fde->vdrap_reg == INVALID_REGNUM
2078   effects: fde->vdrap_reg = reg.
2079   (set mem fde->drap_reg)
2080   constraints: fde->drap_reg_saved == 1
2081   effects: none.  */
2082
2083 static void
2084 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2085 {
2086   rtx src, dest, span;
2087   HOST_WIDE_INT offset;
2088   dw_fde_ref fde;
2089
2090   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2091      the PARALLEL independently. The first element is always processed if
2092      it is a SET. This is for backward compatibility.   Other elements
2093      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2094      flag is set in them.  */
2095   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2096     {
2097       int par_index;
2098       int limit = XVECLEN (expr, 0);
2099       rtx elem;
2100
2101       /* PARALLELs have strict read-modify-write semantics, so we
2102          ought to evaluate every rvalue before changing any lvalue.
2103          It's cumbersome to do that in general, but there's an
2104          easy approximation that is enough for all current users:
2105          handle register saves before register assignments.  */
2106       if (GET_CODE (expr) == PARALLEL)
2107         for (par_index = 0; par_index < limit; par_index++)
2108           {
2109             elem = XVECEXP (expr, 0, par_index);
2110             if (GET_CODE (elem) == SET
2111                 && MEM_P (SET_DEST (elem))
2112                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2113               dwarf2out_frame_debug_expr (elem, label);
2114           }
2115
2116       for (par_index = 0; par_index < limit; par_index++)
2117         {
2118           elem = XVECEXP (expr, 0, par_index);
2119           if (GET_CODE (elem) == SET
2120               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2121               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2122             dwarf2out_frame_debug_expr (elem, label);
2123           else if (GET_CODE (elem) == SET
2124                    && par_index != 0
2125                    && !RTX_FRAME_RELATED_P (elem))
2126             {
2127               /* Stack adjustment combining might combine some post-prologue
2128                  stack adjustment into a prologue stack adjustment.  */
2129               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2130
2131               if (offset != 0)
2132                 dwarf2out_args_size_adjust (offset, label);
2133             }
2134         }
2135       return;
2136     }
2137
2138   gcc_assert (GET_CODE (expr) == SET);
2139
2140   src = SET_SRC (expr);
2141   dest = SET_DEST (expr);
2142
2143   if (REG_P (src))
2144     {
2145       rtx rsi = reg_saved_in (src);
2146       if (rsi)
2147         src = rsi;
2148     }
2149
2150   fde = current_fde ();
2151
2152   if (GET_CODE (src) == REG
2153       && fde
2154       && fde->drap_reg == REGNO (src)
2155       && (fde->drap_reg_saved
2156           || GET_CODE (dest) == REG))
2157     {
2158       /* Rule 20 */
2159       /* If we are saving dynamic realign argument pointer to a
2160          register, the destination is virtual dynamic realign
2161          argument pointer.  It may be used to access argument.  */
2162       if (GET_CODE (dest) == REG)
2163         {
2164           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2165           fde->vdrap_reg = REGNO (dest);
2166         }
2167       return;
2168     }
2169
2170   switch (GET_CODE (dest))
2171     {
2172     case REG:
2173       switch (GET_CODE (src))
2174         {
2175           /* Setting FP from SP.  */
2176         case REG:
2177           if (cfa.reg == (unsigned) REGNO (src))
2178             {
2179               /* Rule 1 */
2180               /* Update the CFA rule wrt SP or FP.  Make sure src is
2181                  relative to the current CFA register.
2182
2183                  We used to require that dest be either SP or FP, but the
2184                  ARM copies SP to a temporary register, and from there to
2185                  FP.  So we just rely on the backends to only set
2186                  RTX_FRAME_RELATED_P on appropriate insns.  */
2187               cfa.reg = REGNO (dest);
2188               cfa_temp.reg = cfa.reg;
2189               cfa_temp.offset = cfa.offset;
2190             }
2191           else
2192             {
2193               /* Saving a register in a register.  */
2194               gcc_assert (!fixed_regs [REGNO (dest)]
2195                           /* For the SPARC and its register window.  */
2196                           || (DWARF_FRAME_REGNUM (REGNO (src))
2197                               == DWARF_FRAME_RETURN_COLUMN));
2198
2199               /* After stack is aligned, we can only save SP in FP
2200                  if drap register is used.  In this case, we have
2201                  to restore stack pointer with the CFA value and we
2202                  don't generate this DWARF information.  */
2203               if (fde
2204                   && fde->stack_realign
2205                   && REGNO (src) == STACK_POINTER_REGNUM)
2206                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2207                             && fde->drap_reg != INVALID_REGNUM
2208                             && cfa.reg != REGNO (src));
2209               else
2210                 queue_reg_save (label, src, dest, 0);
2211             }
2212           break;
2213
2214         case PLUS:
2215         case MINUS:
2216         case LO_SUM:
2217           if (dest == stack_pointer_rtx)
2218             {
2219               /* Rule 2 */
2220               /* Adjusting SP.  */
2221               switch (GET_CODE (XEXP (src, 1)))
2222                 {
2223                 case CONST_INT:
2224                   offset = INTVAL (XEXP (src, 1));
2225                   break;
2226                 case REG:
2227                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2228                               == cfa_temp.reg);
2229                   offset = cfa_temp.offset;
2230                   break;
2231                 default:
2232                   gcc_unreachable ();
2233                 }
2234
2235               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2236                 {
2237                   /* Restoring SP from FP in the epilogue.  */
2238                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2239                   cfa.reg = STACK_POINTER_REGNUM;
2240                 }
2241               else if (GET_CODE (src) == LO_SUM)
2242                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2243                 ;
2244               else
2245                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2246
2247               if (GET_CODE (src) != MINUS)
2248                 offset = -offset;
2249               if (cfa.reg == STACK_POINTER_REGNUM)
2250                 cfa.offset += offset;
2251               if (cfa_store.reg == STACK_POINTER_REGNUM)
2252                 cfa_store.offset += offset;
2253             }
2254           else if (dest == hard_frame_pointer_rtx)
2255             {
2256               /* Rule 3 */
2257               /* Either setting the FP from an offset of the SP,
2258                  or adjusting the FP */
2259               gcc_assert (frame_pointer_needed);
2260
2261               gcc_assert (REG_P (XEXP (src, 0))
2262                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2263                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2264               offset = INTVAL (XEXP (src, 1));
2265               if (GET_CODE (src) != MINUS)
2266                 offset = -offset;
2267               cfa.offset += offset;
2268               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2269             }
2270           else
2271             {
2272               gcc_assert (GET_CODE (src) != MINUS);
2273
2274               /* Rule 4 */
2275               if (REG_P (XEXP (src, 0))
2276                   && REGNO (XEXP (src, 0)) == cfa.reg
2277                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2278                 {
2279                   /* Setting a temporary CFA register that will be copied
2280                      into the FP later on.  */
2281                   offset = - INTVAL (XEXP (src, 1));
2282                   cfa.offset += offset;
2283                   cfa.reg = REGNO (dest);
2284                   /* Or used to save regs to the stack.  */
2285                   cfa_temp.reg = cfa.reg;
2286                   cfa_temp.offset = cfa.offset;
2287                 }
2288
2289               /* Rule 5 */
2290               else if (REG_P (XEXP (src, 0))
2291                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2292                        && XEXP (src, 1) == stack_pointer_rtx)
2293                 {
2294                   /* Setting a scratch register that we will use instead
2295                      of SP for saving registers to the stack.  */
2296                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2297                   cfa_store.reg = REGNO (dest);
2298                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2299                 }
2300
2301               /* Rule 9 */
2302               else if (GET_CODE (src) == LO_SUM
2303                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2304                 {
2305                   cfa_temp.reg = REGNO (dest);
2306                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2307                 }
2308               else
2309                 gcc_unreachable ();
2310             }
2311           break;
2312
2313           /* Rule 6 */
2314         case CONST_INT:
2315           cfa_temp.reg = REGNO (dest);
2316           cfa_temp.offset = INTVAL (src);
2317           break;
2318
2319           /* Rule 7 */
2320         case IOR:
2321           gcc_assert (REG_P (XEXP (src, 0))
2322                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2323                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2324
2325           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2326             cfa_temp.reg = REGNO (dest);
2327           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2328           break;
2329
2330           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2331              which will fill in all of the bits.  */
2332           /* Rule 8 */
2333         case HIGH:
2334           break;
2335
2336           /* Rule 15 */
2337         case UNSPEC:
2338         case UNSPEC_VOLATILE:
2339           gcc_assert (targetm.dwarf_handle_frame_unspec);
2340           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2341           return;
2342
2343           /* Rule 16 */
2344         case AND:
2345           /* If this AND operation happens on stack pointer in prologue,
2346              we assume the stack is realigned and we extract the
2347              alignment.  */
2348           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2349             {
2350               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2351               fde->stack_realign = 1;
2352               fde->stack_realignment = INTVAL (XEXP (src, 1));
2353               cfa_store.offset = 0;
2354
2355               if (cfa.reg != STACK_POINTER_REGNUM
2356                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2357                 fde->drap_reg = cfa.reg;
2358             }
2359           return;
2360
2361         default:
2362           gcc_unreachable ();
2363         }
2364
2365       def_cfa_1 (label, &cfa);
2366       break;
2367
2368     case MEM:
2369
2370       /* Saving a register to the stack.  Make sure dest is relative to the
2371          CFA register.  */
2372       switch (GET_CODE (XEXP (dest, 0)))
2373         {
2374           /* Rule 10 */
2375           /* With a push.  */
2376         case PRE_MODIFY:
2377           /* We can't handle variable size modifications.  */
2378           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2379                       == CONST_INT);
2380           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2381
2382           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2383                       && cfa_store.reg == STACK_POINTER_REGNUM);
2384
2385           cfa_store.offset += offset;
2386           if (cfa.reg == STACK_POINTER_REGNUM)
2387             cfa.offset = cfa_store.offset;
2388
2389           offset = -cfa_store.offset;
2390           break;
2391
2392           /* Rule 11 */
2393         case PRE_INC:
2394         case PRE_DEC:
2395           offset = GET_MODE_SIZE (GET_MODE (dest));
2396           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2397             offset = -offset;
2398
2399           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2400                        == STACK_POINTER_REGNUM)
2401                       && cfa_store.reg == STACK_POINTER_REGNUM);
2402
2403           cfa_store.offset += offset;
2404
2405           /* Rule 18: If stack is aligned, we will use FP as a
2406              reference to represent the address of the stored
2407              regiser.  */
2408           if (fde
2409               && fde->stack_realign
2410               && src == hard_frame_pointer_rtx)
2411             {
2412               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2413               cfa_store.offset = 0;
2414             }
2415
2416           if (cfa.reg == STACK_POINTER_REGNUM)
2417             cfa.offset = cfa_store.offset;
2418
2419           offset = -cfa_store.offset;
2420           break;
2421
2422           /* Rule 12 */
2423           /* With an offset.  */
2424         case PLUS:
2425         case MINUS:
2426         case LO_SUM:
2427           {
2428             int regno;
2429
2430             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2431                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2432             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2433             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2434               offset = -offset;
2435
2436             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2437
2438             if (cfa_store.reg == (unsigned) regno)
2439               offset -= cfa_store.offset;
2440             else
2441               {
2442                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2443                 offset -= cfa_temp.offset;
2444               }
2445           }
2446           break;
2447
2448           /* Rule 13 */
2449           /* Without an offset.  */
2450         case REG:
2451           {
2452             int regno = REGNO (XEXP (dest, 0));
2453
2454             if (cfa_store.reg == (unsigned) regno)
2455               offset = -cfa_store.offset;
2456             else
2457               {
2458                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2459                 offset = -cfa_temp.offset;
2460               }
2461           }
2462           break;
2463
2464           /* Rule 14 */
2465         case POST_INC:
2466           gcc_assert (cfa_temp.reg
2467                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2468           offset = -cfa_temp.offset;
2469           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2470           break;
2471
2472         default:
2473           gcc_unreachable ();
2474         }
2475
2476         /* Rule 17 */
2477         /* If the source operand of this MEM operation is not a
2478            register, basically the source is return address.  Here
2479            we only care how much stack grew and we don't save it.  */
2480       if (!REG_P (src))
2481         break;
2482
2483       if (REGNO (src) != STACK_POINTER_REGNUM
2484           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2485           && (unsigned) REGNO (src) == cfa.reg)
2486         {
2487           /* We're storing the current CFA reg into the stack.  */
2488
2489           if (cfa.offset == 0)
2490             {
2491               /* Rule 19 */
2492               /* If stack is aligned, putting CFA reg into stack means
2493                  we can no longer use reg + offset to represent CFA.
2494                  Here we use DW_CFA_def_cfa_expression instead.  The
2495                  result of this expression equals to the original CFA
2496                  value.  */
2497               if (fde
2498                   && fde->stack_realign
2499                   && cfa.indirect == 0
2500                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2501                 {
2502                   dw_cfa_location cfa_exp;
2503
2504                   gcc_assert (fde->drap_reg == cfa.reg);
2505
2506                   cfa_exp.indirect = 1;
2507                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2508                   cfa_exp.base_offset = offset;
2509                   cfa_exp.offset = 0;
2510
2511                   fde->drap_reg_saved = 1;
2512
2513                   def_cfa_1 (label, &cfa_exp);
2514                   break;
2515                 }
2516
2517               /* If the source register is exactly the CFA, assume
2518                  we're saving SP like any other register; this happens
2519                  on the ARM.  */
2520               def_cfa_1 (label, &cfa);
2521               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2522               break;
2523             }
2524           else
2525             {
2526               /* Otherwise, we'll need to look in the stack to
2527                  calculate the CFA.  */
2528               rtx x = XEXP (dest, 0);
2529
2530               if (!REG_P (x))
2531                 x = XEXP (x, 0);
2532               gcc_assert (REG_P (x));
2533
2534               cfa.reg = REGNO (x);
2535               cfa.base_offset = offset;
2536               cfa.indirect = 1;
2537               def_cfa_1 (label, &cfa);
2538               break;
2539             }
2540         }
2541
2542       def_cfa_1 (label, &cfa);
2543       {
2544         span = targetm.dwarf_register_span (src);
2545
2546         if (!span)
2547           queue_reg_save (label, src, NULL_RTX, offset);
2548         else
2549           {
2550             /* We have a PARALLEL describing where the contents of SRC
2551                live.  Queue register saves for each piece of the
2552                PARALLEL.  */
2553             int par_index;
2554             int limit;
2555             HOST_WIDE_INT span_offset = offset;
2556
2557             gcc_assert (GET_CODE (span) == PARALLEL);
2558
2559             limit = XVECLEN (span, 0);
2560             for (par_index = 0; par_index < limit; par_index++)
2561               {
2562                 rtx elem = XVECEXP (span, 0, par_index);
2563
2564                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2565                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2566               }
2567           }
2568       }
2569       break;
2570
2571     default:
2572       gcc_unreachable ();
2573     }
2574 }
2575
2576 /* Record call frame debugging information for INSN, which either
2577    sets SP or FP (adjusting how we calculate the frame address) or saves a
2578    register to the stack.  If INSN is NULL_RTX, initialize our state.
2579
2580    If AFTER_P is false, we're being called before the insn is emitted,
2581    otherwise after.  Call instructions get invoked twice.  */
2582
2583 void
2584 dwarf2out_frame_debug (rtx insn, bool after_p)
2585 {
2586   const char *label;
2587   rtx note, n;
2588   bool handled_one = false;
2589
2590   if (insn == NULL_RTX)
2591     {
2592       size_t i;
2593
2594       /* Flush any queued register saves.  */
2595       flush_queued_reg_saves ();
2596
2597       /* Set up state for generating call frame debug info.  */
2598       lookup_cfa (&cfa);
2599       gcc_assert (cfa.reg
2600                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2601
2602       cfa.reg = STACK_POINTER_REGNUM;
2603       cfa_store = cfa;
2604       cfa_temp.reg = -1;
2605       cfa_temp.offset = 0;
2606
2607       for (i = 0; i < num_regs_saved_in_regs; i++)
2608         {
2609           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2610           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2611         }
2612       num_regs_saved_in_regs = 0;
2613
2614       if (barrier_args_size)
2615         {
2616           XDELETEVEC (barrier_args_size);
2617           barrier_args_size = NULL;
2618         }
2619       return;
2620     }
2621
2622   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2623     flush_queued_reg_saves ();
2624
2625   if (! RTX_FRAME_RELATED_P (insn))
2626     {
2627       if (!ACCUMULATE_OUTGOING_ARGS)
2628         dwarf2out_stack_adjust (insn, after_p);
2629       return;
2630     }
2631
2632   label = dwarf2out_cfi_label (false);
2633
2634   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2635     switch (REG_NOTE_KIND (note))
2636       {
2637       case REG_FRAME_RELATED_EXPR:
2638         insn = XEXP (note, 0);
2639         goto found;
2640
2641       case REG_CFA_DEF_CFA:
2642         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2643         handled_one = true;
2644         break;
2645
2646       case REG_CFA_ADJUST_CFA:
2647         n = XEXP (note, 0);
2648         if (n == NULL)
2649           {
2650             n = PATTERN (insn);
2651             if (GET_CODE (n) == PARALLEL)
2652               n = XVECEXP (n, 0, 0);
2653           }
2654         dwarf2out_frame_debug_adjust_cfa (n, label);
2655         handled_one = true;
2656         break;
2657
2658       case REG_CFA_OFFSET:
2659         n = XEXP (note, 0);
2660         if (n == NULL)
2661           n = single_set (insn);
2662         dwarf2out_frame_debug_cfa_offset (n, label);
2663         handled_one = true;
2664         break;
2665
2666       case REG_CFA_REGISTER:
2667         n = XEXP (note, 0);
2668         if (n == NULL)
2669           {
2670             n = PATTERN (insn);
2671             if (GET_CODE (n) == PARALLEL)
2672               n = XVECEXP (n, 0, 0);
2673           }
2674         dwarf2out_frame_debug_cfa_register (n, label);
2675         handled_one = true;
2676         break;
2677
2678       case REG_CFA_RESTORE:
2679         n = XEXP (note, 0);
2680         if (n == NULL)
2681           {
2682             n = PATTERN (insn);
2683             if (GET_CODE (n) == PARALLEL)
2684               n = XVECEXP (n, 0, 0);
2685             n = XEXP (n, 0);
2686           }
2687         dwarf2out_frame_debug_cfa_restore (n, label);
2688         handled_one = true;
2689         break;
2690
2691       default:
2692         break;
2693       }
2694   if (handled_one)
2695     return;
2696
2697   insn = PATTERN (insn);
2698  found:
2699   dwarf2out_frame_debug_expr (insn, label);
2700 }
2701
2702 /* Determine if we need to save and restore CFI information around this
2703    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2704    we do need to save/restore, then emit the save now, and insert a
2705    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2706
2707 void
2708 dwarf2out_begin_epilogue (rtx insn)
2709 {
2710   bool saw_frp = false;
2711   rtx i;
2712   dw_cfi_ref cfi;
2713
2714   /* Scan forward to the return insn, noticing if there are possible
2715      frame related insns.  */
2716   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2717     {
2718       if (!INSN_P (i))
2719         continue;
2720
2721       /* Look for both regular and sibcalls to end the block.  */
2722       if (returnjump_p (i))
2723         break;
2724       if (CALL_P (i) && SIBLING_CALL_P (i))
2725         break;
2726
2727       if (RTX_FRAME_RELATED_P (i))
2728         saw_frp = true;
2729     }
2730
2731   /* If the port doesn't emit epilogue unwind info, we don't need a
2732      save/restore pair.  */
2733   if (!saw_frp)
2734     return;
2735
2736   /* Otherwise, search forward to see if the return insn was the last
2737      basic block of the function.  If so, we don't need save/restore.  */
2738   gcc_assert (i != NULL);
2739   i = next_real_insn (i);
2740   if (i == NULL)
2741     return;
2742
2743   /* Insert the restore before that next real insn in the stream, and before
2744      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2745      properly nested.  This should be after any label or alignment.  This
2746      will be pushed into the CFI stream by the function below.  */
2747   while (1)
2748     {
2749       rtx p = PREV_INSN (i);
2750       if (!NOTE_P (p))
2751         break;
2752       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2753         break;
2754       i = p;
2755     }
2756   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2757
2758   /* Emit the state save.  */
2759   cfi = new_cfi (); 
2760   cfi->dw_cfi_opc = DW_CFA_remember_state;
2761   add_fde_cfi (dwarf2out_cfi_label (false), cfi);
2762
2763   /* And emulate the state save.  */
2764   gcc_assert (!cfa_remember.in_use);
2765   cfa_remember = cfa;
2766   cfa_remember.in_use = 1;
2767 }
2768
2769 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2770
2771 void
2772 dwarf2out_frame_debug_restore_state (void)
2773 {
2774   dw_cfi_ref cfi = new_cfi (); 
2775   const char *label = dwarf2out_cfi_label (false);
2776
2777   cfi->dw_cfi_opc = DW_CFA_restore_state;
2778   add_fde_cfi (label, cfi);
2779
2780   gcc_assert (cfa_remember.in_use);
2781   cfa = cfa_remember;
2782   cfa_remember.in_use = 0;
2783 }
2784
2785 #endif
2786
2787 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2788 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2789  (enum dwarf_call_frame_info cfi);
2790
2791 static enum dw_cfi_oprnd_type
2792 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2793 {
2794   switch (cfi)
2795     {
2796     case DW_CFA_nop:
2797     case DW_CFA_GNU_window_save:
2798     case DW_CFA_remember_state:
2799     case DW_CFA_restore_state:
2800       return dw_cfi_oprnd_unused;
2801
2802     case DW_CFA_set_loc:
2803     case DW_CFA_advance_loc1:
2804     case DW_CFA_advance_loc2:
2805     case DW_CFA_advance_loc4:
2806     case DW_CFA_MIPS_advance_loc8:
2807       return dw_cfi_oprnd_addr;
2808
2809     case DW_CFA_offset:
2810     case DW_CFA_offset_extended:
2811     case DW_CFA_def_cfa:
2812     case DW_CFA_offset_extended_sf:
2813     case DW_CFA_def_cfa_sf:
2814     case DW_CFA_restore:
2815     case DW_CFA_restore_extended:
2816     case DW_CFA_undefined:
2817     case DW_CFA_same_value:
2818     case DW_CFA_def_cfa_register:
2819     case DW_CFA_register:
2820       return dw_cfi_oprnd_reg_num;
2821
2822     case DW_CFA_def_cfa_offset:
2823     case DW_CFA_GNU_args_size:
2824     case DW_CFA_def_cfa_offset_sf:
2825       return dw_cfi_oprnd_offset;
2826
2827     case DW_CFA_def_cfa_expression:
2828     case DW_CFA_expression:
2829       return dw_cfi_oprnd_loc;
2830
2831     default:
2832       gcc_unreachable ();
2833     }
2834 }
2835
2836 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2837 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2838  (enum dwarf_call_frame_info cfi);
2839
2840 static enum dw_cfi_oprnd_type
2841 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2842 {
2843   switch (cfi)
2844     {
2845     case DW_CFA_def_cfa:
2846     case DW_CFA_def_cfa_sf:
2847     case DW_CFA_offset:
2848     case DW_CFA_offset_extended_sf:
2849     case DW_CFA_offset_extended:
2850       return dw_cfi_oprnd_offset;
2851
2852     case DW_CFA_register:
2853       return dw_cfi_oprnd_reg_num;
2854
2855     default:
2856       return dw_cfi_oprnd_unused;
2857     }
2858 }
2859
2860 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2861
2862 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2863    switch to the data section instead, and write out a synthetic label
2864    for collect2.  */
2865
2866 static void
2867 switch_to_eh_frame_section (void)
2868 {
2869   tree label;
2870
2871 #ifdef EH_FRAME_SECTION_NAME
2872   if (eh_frame_section == 0)
2873     {
2874       int flags;
2875
2876       if (EH_TABLES_CAN_BE_READ_ONLY)
2877         {
2878           int fde_encoding;
2879           int per_encoding;
2880           int lsda_encoding;
2881
2882           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2883                                                        /*global=*/0);
2884           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2885                                                        /*global=*/1);
2886           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2887                                                         /*global=*/0);
2888           flags = ((! flag_pic
2889                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2890                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2891                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2892                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2893                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2894                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2895                    ? 0 : SECTION_WRITE);
2896         }
2897       else
2898         flags = SECTION_WRITE;
2899       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2900     }
2901 #endif
2902
2903   if (eh_frame_section)
2904     switch_to_section (eh_frame_section);
2905   else
2906     {
2907       /* We have no special eh_frame section.  Put the information in
2908          the data section and emit special labels to guide collect2.  */
2909       switch_to_section (data_section);
2910       label = get_file_function_name ("F");
2911       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2912       targetm.asm_out.globalize_label (asm_out_file,
2913                                        IDENTIFIER_POINTER (label));
2914       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2915     }
2916 }
2917
2918 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2919
2920 static HOST_WIDE_INT
2921 div_data_align (HOST_WIDE_INT off)
2922 {
2923   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2924   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2925   return r;
2926 }
2927
2928 /* Output a Call Frame Information opcode and its operand(s).  */
2929
2930 static void
2931 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2932 {
2933   unsigned long r;
2934   HOST_WIDE_INT off;
2935
2936   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2937     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2938                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2939                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2940                          ((unsigned HOST_WIDE_INT)
2941                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2942   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2943     {
2944       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2945       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2946                            "DW_CFA_offset, column 0x%lx", r);
2947       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2948       dw2_asm_output_data_uleb128 (off, NULL);
2949     }
2950   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2951     {
2952       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2953       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2954                            "DW_CFA_restore, column 0x%lx", r);
2955     }
2956   else
2957     {
2958       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2959                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2960
2961       switch (cfi->dw_cfi_opc)
2962         {
2963         case DW_CFA_set_loc:
2964           if (for_eh)
2965             dw2_asm_output_encoded_addr_rtx (
2966                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2967                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2968                 false, NULL);
2969           else
2970             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2971                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2972           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2973           break;
2974
2975         case DW_CFA_advance_loc1:
2976           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2977                                 fde->dw_fde_current_label, NULL);
2978           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2979           break;
2980
2981         case DW_CFA_advance_loc2:
2982           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2983                                 fde->dw_fde_current_label, NULL);
2984           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2985           break;
2986
2987         case DW_CFA_advance_loc4:
2988           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2989                                 fde->dw_fde_current_label, NULL);
2990           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2991           break;
2992
2993         case DW_CFA_MIPS_advance_loc8:
2994           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2995                                 fde->dw_fde_current_label, NULL);
2996           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2997           break;
2998
2999         case DW_CFA_offset_extended:
3000           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3001           dw2_asm_output_data_uleb128 (r, NULL);
3002           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3003           dw2_asm_output_data_uleb128 (off, NULL);
3004           break;
3005
3006         case DW_CFA_def_cfa:
3007           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3008           dw2_asm_output_data_uleb128 (r, NULL);
3009           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3010           break;
3011
3012         case DW_CFA_offset_extended_sf:
3013           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3014           dw2_asm_output_data_uleb128 (r, NULL);
3015           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3016           dw2_asm_output_data_sleb128 (off, NULL);
3017           break;
3018
3019         case DW_CFA_def_cfa_sf:
3020           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3021           dw2_asm_output_data_uleb128 (r, NULL);
3022           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3023           dw2_asm_output_data_sleb128 (off, NULL);
3024           break;
3025
3026         case DW_CFA_restore_extended:
3027         case DW_CFA_undefined:
3028         case DW_CFA_same_value:
3029         case DW_CFA_def_cfa_register:
3030           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3031           dw2_asm_output_data_uleb128 (r, NULL);
3032           break;
3033
3034         case DW_CFA_register:
3035           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3036           dw2_asm_output_data_uleb128 (r, NULL);
3037           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3038           dw2_asm_output_data_uleb128 (r, NULL);
3039           break;
3040
3041         case DW_CFA_def_cfa_offset:
3042         case DW_CFA_GNU_args_size:
3043           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3044           break;
3045
3046         case DW_CFA_def_cfa_offset_sf:
3047           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3048           dw2_asm_output_data_sleb128 (off, NULL);
3049           break;
3050
3051         case DW_CFA_GNU_window_save:
3052           break;
3053
3054         case DW_CFA_def_cfa_expression:
3055         case DW_CFA_expression:
3056           output_cfa_loc (cfi);
3057           break;
3058
3059         case DW_CFA_GNU_negative_offset_extended:
3060           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3061           gcc_unreachable ();
3062
3063         default:
3064           break;
3065         }
3066     }
3067 }
3068
3069 /* Similar, but do it via assembler directives instead.  */
3070
3071 static void
3072 output_cfi_directive (dw_cfi_ref cfi)
3073 {
3074   unsigned long r, r2;
3075
3076   switch (cfi->dw_cfi_opc)
3077     {
3078     case DW_CFA_advance_loc:
3079     case DW_CFA_advance_loc1:
3080     case DW_CFA_advance_loc2:
3081     case DW_CFA_advance_loc4:
3082     case DW_CFA_MIPS_advance_loc8:
3083     case DW_CFA_set_loc:
3084       /* Should only be created by add_fde_cfi in a code path not
3085          followed when emitting via directives.  The assembler is
3086          going to take care of this for us.  */
3087       gcc_unreachable ();
3088
3089     case DW_CFA_offset:
3090     case DW_CFA_offset_extended:
3091     case DW_CFA_offset_extended_sf:
3092       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3093       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3094                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3095       break;
3096
3097     case DW_CFA_restore:
3098     case DW_CFA_restore_extended:
3099       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3100       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3101       break;
3102
3103     case DW_CFA_undefined:
3104       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3105       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3106       break;
3107
3108     case DW_CFA_same_value:
3109       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3110       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3111       break;
3112
3113     case DW_CFA_def_cfa:
3114     case DW_CFA_def_cfa_sf:
3115       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3116       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3117                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3118       break;
3119
3120     case DW_CFA_def_cfa_register:
3121       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3122       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3123       break;
3124
3125     case DW_CFA_register:
3126       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3127       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3128       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3129       break;
3130
3131     case DW_CFA_def_cfa_offset:
3132     case DW_CFA_def_cfa_offset_sf:
3133       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3134                HOST_WIDE_INT_PRINT_DEC"\n",
3135                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3136       break;
3137
3138     case DW_CFA_remember_state:
3139       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3140       break;
3141     case DW_CFA_restore_state:
3142       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3143       break;
3144
3145     case DW_CFA_GNU_args_size:
3146       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3147       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3148       if (flag_debug_asm)
3149         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3150                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3151       fputc ('\n', asm_out_file);
3152       break;
3153
3154     case DW_CFA_GNU_window_save:
3155       fprintf (asm_out_file, "\t.cfi_window_save\n");
3156       break;
3157
3158     case DW_CFA_def_cfa_expression:
3159     case DW_CFA_expression:
3160       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3161       output_cfa_loc_raw (cfi);
3162       fputc ('\n', asm_out_file);
3163       break;
3164
3165     default:
3166       gcc_unreachable ();
3167     }
3168 }
3169
3170 /* Output the call frame information used to record information
3171    that relates to calculating the frame pointer, and records the
3172    location of saved registers.  */
3173
3174 static void
3175 output_call_frame_info (int for_eh)
3176 {
3177   unsigned int i;
3178   dw_fde_ref fde;
3179   dw_cfi_ref cfi;
3180   char l1[20], l2[20], section_start_label[20];
3181   bool any_lsda_needed = false;
3182   char augmentation[6];
3183   int augmentation_size;
3184   int fde_encoding = DW_EH_PE_absptr;
3185   int per_encoding = DW_EH_PE_absptr;
3186   int lsda_encoding = DW_EH_PE_absptr;
3187   int return_reg;
3188
3189   /* Don't emit a CIE if there won't be any FDEs.  */
3190   if (fde_table_in_use == 0)
3191     return;
3192
3193   /* Nothing to do if the assembler's doing it all.  */
3194   if (dwarf2out_do_cfi_asm ())
3195     return;
3196
3197   /* If we make FDEs linkonce, we may have to emit an empty label for
3198      an FDE that wouldn't otherwise be emitted.  We want to avoid
3199      having an FDE kept around when the function it refers to is
3200      discarded.  Example where this matters: a primary function
3201      template in C++ requires EH information, but an explicit
3202      specialization doesn't.  */
3203   if (TARGET_USES_WEAK_UNWIND_INFO
3204       && ! flag_asynchronous_unwind_tables
3205       && flag_exceptions
3206       && for_eh)
3207     for (i = 0; i < fde_table_in_use; i++)
3208       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3209           && !fde_table[i].uses_eh_lsda
3210           && ! DECL_WEAK (fde_table[i].decl))
3211         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3212                                       for_eh, /* empty */ 1);
3213
3214   /* If we don't have any functions we'll want to unwind out of, don't
3215      emit any EH unwind information.  Note that if exceptions aren't
3216      enabled, we won't have collected nothrow information, and if we
3217      asked for asynchronous tables, we always want this info.  */
3218   if (for_eh)
3219     {
3220       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3221
3222       for (i = 0; i < fde_table_in_use; i++)
3223         if (fde_table[i].uses_eh_lsda)
3224           any_eh_needed = any_lsda_needed = true;
3225         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3226           any_eh_needed = true;
3227         else if (! fde_table[i].nothrow
3228                  && ! fde_table[i].all_throwers_are_sibcalls)
3229           any_eh_needed = true;
3230
3231       if (! any_eh_needed)
3232         return;
3233     }
3234
3235   /* We're going to be generating comments, so turn on app.  */
3236   if (flag_debug_asm)
3237     app_enable ();
3238
3239   if (for_eh)
3240     switch_to_eh_frame_section ();
3241   else
3242     {
3243       if (!debug_frame_section)
3244         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3245                                            SECTION_DEBUG, NULL);
3246       switch_to_section (debug_frame_section);
3247     }
3248
3249   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3250   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3251
3252   /* Output the CIE.  */
3253   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3254   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3255   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3256     dw2_asm_output_data (4, 0xffffffff,
3257       "Initial length escape value indicating 64-bit DWARF extension");
3258   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3259                         "Length of Common Information Entry");
3260   ASM_OUTPUT_LABEL (asm_out_file, l1);
3261
3262   /* Now that the CIE pointer is PC-relative for EH,
3263      use 0 to identify the CIE.  */
3264   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3265                        (for_eh ? 0 : DWARF_CIE_ID),
3266                        "CIE Identifier Tag");
3267
3268   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
3269
3270   augmentation[0] = 0;
3271   augmentation_size = 0;
3272   if (for_eh)
3273     {
3274       char *p;
3275
3276       /* Augmentation:
3277          z      Indicates that a uleb128 is present to size the
3278                 augmentation section.
3279          L      Indicates the encoding (and thus presence) of
3280                 an LSDA pointer in the FDE augmentation.
3281          R      Indicates a non-default pointer encoding for
3282                 FDE code pointers.
3283          P      Indicates the presence of an encoding + language
3284                 personality routine in the CIE augmentation.  */
3285
3286       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3287       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3288       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3289
3290       p = augmentation + 1;
3291       if (eh_personality_libfunc)
3292         {
3293           *p++ = 'P';
3294           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3295           assemble_external_libcall (eh_personality_libfunc);
3296         }
3297       if (any_lsda_needed)
3298         {
3299           *p++ = 'L';
3300           augmentation_size += 1;
3301         }
3302       if (fde_encoding != DW_EH_PE_absptr)
3303         {
3304           *p++ = 'R';
3305           augmentation_size += 1;
3306         }
3307       if (p > augmentation + 1)
3308         {
3309           augmentation[0] = 'z';
3310           *p = '\0';
3311         }
3312
3313       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3314       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3315         {
3316           int offset = (  4             /* Length */
3317                         + 4             /* CIE Id */
3318                         + 1             /* CIE version */
3319                         + strlen (augmentation) + 1     /* Augmentation */
3320                         + size_of_uleb128 (1)           /* Code alignment */
3321                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3322                         + 1             /* RA column */
3323                         + 1             /* Augmentation size */
3324                         + 1             /* Personality encoding */ );
3325           int pad = -offset & (PTR_SIZE - 1);
3326
3327           augmentation_size += pad;
3328
3329           /* Augmentations should be small, so there's scarce need to
3330              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3331           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3332         }
3333     }
3334
3335   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3336   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3337   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3338                                "CIE Data Alignment Factor");
3339
3340   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3341   if (DW_CIE_VERSION == 1)
3342     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3343   else
3344     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3345
3346   if (augmentation[0])
3347     {
3348       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3349       if (eh_personality_libfunc)
3350         {
3351           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3352                                eh_data_format_name (per_encoding));
3353           dw2_asm_output_encoded_addr_rtx (per_encoding,
3354                                            eh_personality_libfunc,
3355                                            true, NULL);
3356         }
3357
3358       if (any_lsda_needed)
3359         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3360                              eh_data_format_name (lsda_encoding));
3361
3362       if (fde_encoding != DW_EH_PE_absptr)
3363         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3364                              eh_data_format_name (fde_encoding));
3365     }
3366
3367   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3368     output_cfi (cfi, NULL, for_eh);
3369
3370   /* Pad the CIE out to an address sized boundary.  */
3371   ASM_OUTPUT_ALIGN (asm_out_file,
3372                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3373   ASM_OUTPUT_LABEL (asm_out_file, l2);
3374
3375   /* Loop through all of the FDE's.  */
3376   for (i = 0; i < fde_table_in_use; i++)
3377     {
3378       fde = &fde_table[i];
3379
3380       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3381       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3382           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3383           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3384           && !fde->uses_eh_lsda)
3385         continue;
3386
3387       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3388       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3389       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3390       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3391       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3392         dw2_asm_output_data (4, 0xffffffff,
3393                              "Initial length escape value indicating 64-bit DWARF extension");
3394       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3395                             "FDE Length");
3396       ASM_OUTPUT_LABEL (asm_out_file, l1);
3397
3398       if (for_eh)
3399         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3400       else
3401         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3402                                debug_frame_section, "FDE CIE offset");
3403
3404       if (for_eh)
3405         {
3406           if (fde->dw_fde_switched_sections)
3407             {
3408               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3409                                       fde->dw_fde_unlikely_section_label);
3410               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3411                                       fde->dw_fde_hot_section_label);
3412               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3413               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3414               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3415                                                "FDE initial location");
3416               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3417                                     fde->dw_fde_hot_section_end_label,
3418                                     fde->dw_fde_hot_section_label,
3419                                     "FDE address range");
3420               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3421                                                "FDE initial location");
3422               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3423                                     fde->dw_fde_unlikely_section_end_label,
3424                                     fde->dw_fde_unlikely_section_label,
3425                                     "FDE address range");
3426             }
3427           else
3428             {
3429               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3430               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3431               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3432                                                sym_ref,
3433                                                false,
3434                                                "FDE initial location");
3435               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3436                                     fde->dw_fde_end, fde->dw_fde_begin,
3437                                     "FDE address range");
3438             }
3439         }
3440       else
3441         {
3442           if (fde->dw_fde_switched_sections)
3443             {
3444               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3445                                    fde->dw_fde_hot_section_label,
3446                                    "FDE initial location");
3447               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3448                                     fde->dw_fde_hot_section_end_label,
3449                                     fde->dw_fde_hot_section_label,
3450                                     "FDE address range");
3451               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3452                                    fde->dw_fde_unlikely_section_label,
3453                                    "FDE initial location");
3454               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3455                                     fde->dw_fde_unlikely_section_end_label,
3456                                     fde->dw_fde_unlikely_section_label,
3457                                     "FDE address range");
3458             }
3459           else
3460             {
3461               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3462                                    "FDE initial location");
3463               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3464                                     fde->dw_fde_end, fde->dw_fde_begin,
3465                                     "FDE address range");
3466             }
3467         }
3468
3469       if (augmentation[0])
3470         {
3471           if (any_lsda_needed)
3472             {
3473               int size = size_of_encoded_value (lsda_encoding);
3474
3475               if (lsda_encoding == DW_EH_PE_aligned)
3476                 {
3477                   int offset = (  4             /* Length */
3478                                 + 4             /* CIE offset */
3479                                 + 2 * size_of_encoded_value (fde_encoding)
3480                                 + 1             /* Augmentation size */ );
3481                   int pad = -offset & (PTR_SIZE - 1);
3482
3483                   size += pad;
3484                   gcc_assert (size_of_uleb128 (size) == 1);
3485                 }
3486
3487               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3488
3489               if (fde->uses_eh_lsda)
3490                 {
3491                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3492                                                fde->funcdef_number);
3493                   dw2_asm_output_encoded_addr_rtx (
3494                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3495                         false, "Language Specific Data Area");
3496                 }
3497               else
3498                 {
3499                   if (lsda_encoding == DW_EH_PE_aligned)
3500                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3501                   dw2_asm_output_data
3502                     (size_of_encoded_value (lsda_encoding), 0,
3503                      "Language Specific Data Area (none)");
3504                 }
3505             }
3506           else
3507             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3508         }
3509
3510       /* Loop through the Call Frame Instructions associated with
3511          this FDE.  */
3512       fde->dw_fde_current_label = fde->dw_fde_begin;
3513       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3514         output_cfi (cfi, fde, for_eh);
3515
3516       /* Pad the FDE out to an address sized boundary.  */
3517       ASM_OUTPUT_ALIGN (asm_out_file,
3518                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3519       ASM_OUTPUT_LABEL (asm_out_file, l2);
3520     }
3521
3522   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3523     dw2_asm_output_data (4, 0, "End of Table");
3524 #ifdef MIPS_DEBUGGING_INFO
3525   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3526      get a value of 0.  Putting .align 0 after the label fixes it.  */
3527   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3528 #endif
3529
3530   /* Turn off app to make assembly quicker.  */
3531   if (flag_debug_asm)
3532     app_disable ();
3533 }
3534
3535 /* Output a marker (i.e. a label) for the beginning of a function, before
3536    the prologue.  */
3537
3538 void
3539 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3540                           const char *file ATTRIBUTE_UNUSED)
3541 {
3542   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3543   char * dup_label;
3544   dw_fde_ref fde;
3545
3546   current_function_func_begin_label = NULL;
3547
3548 #ifdef TARGET_UNWIND_INFO
3549   /* ??? current_function_func_begin_label is also used by except.c
3550      for call-site information.  We must emit this label if it might
3551      be used.  */
3552   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3553       && ! dwarf2out_do_frame ())
3554     return;
3555 #else
3556   if (! dwarf2out_do_frame ())
3557     return;
3558 #endif
3559
3560   switch_to_section (function_section (current_function_decl));
3561   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3562                                current_function_funcdef_no);
3563   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3564                           current_function_funcdef_no);
3565   dup_label = xstrdup (label);
3566   current_function_func_begin_label = dup_label;
3567
3568 #ifdef TARGET_UNWIND_INFO
3569   /* We can elide the fde allocation if we're not emitting debug info.  */
3570   if (! dwarf2out_do_frame ())
3571     return;
3572 #endif
3573
3574   /* Expand the fde table if necessary.  */
3575   if (fde_table_in_use == fde_table_allocated)
3576     {
3577       fde_table_allocated += FDE_TABLE_INCREMENT;
3578       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3579       memset (fde_table + fde_table_in_use, 0,
3580               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3581     }
3582
3583   /* Record the FDE associated with this function.  */
3584   current_funcdef_fde = fde_table_in_use;
3585
3586   /* Add the new FDE at the end of the fde_table.  */
3587   fde = &fde_table[fde_table_in_use++];
3588   fde->decl = current_function_decl;
3589   fde->dw_fde_begin = dup_label;
3590   fde->dw_fde_current_label = dup_label;
3591   fde->dw_fde_hot_section_label = NULL;
3592   fde->dw_fde_hot_section_end_label = NULL;
3593   fde->dw_fde_unlikely_section_label = NULL;
3594   fde->dw_fde_unlikely_section_end_label = NULL;
3595   fde->dw_fde_switched_sections = false;
3596   fde->dw_fde_end = NULL;
3597   fde->dw_fde_cfi = NULL;
3598   fde->funcdef_number = current_function_funcdef_no;
3599   fde->nothrow = crtl->nothrow;
3600   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3601   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3602   fde->drap_reg = INVALID_REGNUM;
3603   fde->vdrap_reg = INVALID_REGNUM;
3604
3605   args_size = old_args_size = 0;
3606
3607   /* We only want to output line number information for the genuine dwarf2
3608      prologue case, not the eh frame case.  */
3609 #ifdef DWARF2_DEBUGGING_INFO
3610   if (file)
3611     dwarf2out_source_line (line, file, 0);
3612 #endif
3613
3614   if (dwarf2out_do_cfi_asm ())
3615     {
3616       int enc;
3617       rtx ref;
3618
3619       fprintf (asm_out_file, "\t.cfi_startproc\n");
3620
3621       if (eh_personality_libfunc)
3622         {
3623           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3624           ref = eh_personality_libfunc;
3625
3626           /* ??? The GAS support isn't entirely consistent.  We have to
3627              handle indirect support ourselves, but PC-relative is done
3628              in the assembler.  Further, the assembler can't handle any
3629              of the weirder relocation types.  */
3630           if (enc & DW_EH_PE_indirect)
3631             ref = dw2_force_const_mem (ref, true);
3632
3633           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3634           output_addr_const (asm_out_file, ref);
3635           fputc ('\n', asm_out_file);
3636         }
3637
3638       if (crtl->uses_eh_lsda)
3639         {
3640           char lab[20];
3641
3642           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3643           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3644                                        current_function_funcdef_no);
3645           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3646           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3647
3648           if (enc & DW_EH_PE_indirect)
3649             ref = dw2_force_const_mem (ref, true);
3650
3651           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3652           output_addr_const (asm_out_file, ref);
3653           fputc ('\n', asm_out_file);
3654         }
3655     }
3656 }
3657
3658 /* Output a marker (i.e. a label) for the absolute end of the generated code
3659    for a function definition.  This gets called *after* the epilogue code has
3660    been generated.  */
3661
3662 void
3663 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3664                         const char *file ATTRIBUTE_UNUSED)
3665 {
3666   dw_fde_ref fde;
3667   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3668
3669   if (dwarf2out_do_cfi_asm ())
3670     fprintf (asm_out_file, "\t.cfi_endproc\n");
3671
3672   /* Output a label to mark the endpoint of the code generated for this
3673      function.  */
3674   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3675                                current_function_funcdef_no);
3676   ASM_OUTPUT_LABEL (asm_out_file, label);
3677   fde = current_fde ();
3678   gcc_assert (fde != NULL);
3679   fde->dw_fde_end = xstrdup (label);
3680 }
3681
3682 void
3683 dwarf2out_frame_init (void)
3684 {
3685   /* Allocate the initial hunk of the fde_table.  */
3686   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3687   fde_table_allocated = FDE_TABLE_INCREMENT;
3688   fde_table_in_use = 0;
3689
3690   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3691      sake of lookup_cfa.  */
3692
3693   /* On entry, the Canonical Frame Address is at SP.  */
3694   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3695
3696 #ifdef DWARF2_UNWIND_INFO
3697   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3698     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3699 #endif
3700 }
3701
3702 void
3703 dwarf2out_frame_finish (void)
3704 {
3705   /* Output call frame information.  */
3706   if (DWARF2_FRAME_INFO)
3707     output_call_frame_info (0);
3708
3709 #ifndef TARGET_UNWIND_INFO
3710   /* Output another copy for the unwinder.  */
3711   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3712     output_call_frame_info (1);
3713 #endif
3714 }
3715
3716 /* Note that the current function section is being used for code.  */
3717
3718 static void
3719 dwarf2out_note_section_used (void)
3720 {
3721   section *sec = current_function_section ();
3722   if (sec == text_section)
3723     text_section_used = true;
3724   else if (sec == cold_text_section)
3725     cold_text_section_used = true;
3726 }
3727
3728 void
3729 dwarf2out_switch_text_section (void)
3730 {
3731   dw_fde_ref fde = current_fde ();
3732
3733   gcc_assert (cfun && fde);
3734
3735   fde->dw_fde_switched_sections = true;
3736   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3737   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3738   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3739   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3740   have_multiple_function_sections = true;
3741
3742   /* Reset the current label on switching text sections, so that we
3743      don't attempt to advance_loc4 between labels in different sections.  */
3744   fde->dw_fde_current_label = NULL;
3745
3746   /* There is no need to mark used sections when not debugging.  */
3747   if (cold_text_section != NULL)
3748     dwarf2out_note_section_used ();
3749 }
3750 #endif
3751 \f
3752 /* And now, the subset of the debugging information support code necessary
3753    for emitting location expressions.  */
3754
3755 /* Data about a single source file.  */
3756 struct GTY(()) dwarf_file_data {
3757   const char * filename;
3758   int emitted_number;
3759 };
3760
3761 /* We need some way to distinguish DW_OP_addr with a direct symbol
3762    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3763 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3764
3765
3766 typedef struct dw_val_struct *dw_val_ref;
3767 typedef struct die_struct *dw_die_ref;
3768 typedef const struct die_struct *const_dw_die_ref;
3769 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3770 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3771
3772 typedef struct GTY(()) deferred_locations_struct
3773 {
3774   tree variable;
3775   dw_die_ref die;
3776 } deferred_locations;
3777
3778 DEF_VEC_O(deferred_locations);
3779 DEF_VEC_ALLOC_O(deferred_locations,gc);
3780
3781 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3782
3783 /* Each DIE may have a series of attribute/value pairs.  Values
3784    can take on several forms.  The forms that are used in this
3785    implementation are listed below.  */
3786
3787 enum dw_val_class
3788 {
3789   dw_val_class_addr,
3790   dw_val_class_offset,
3791   dw_val_class_loc,
3792   dw_val_class_loc_list,
3793   dw_val_class_range_list,
3794   dw_val_class_const,
3795   dw_val_class_unsigned_const,
3796   dw_val_class_long_long,
3797   dw_val_class_vec,
3798   dw_val_class_flag,
3799   dw_val_class_die_ref,
3800   dw_val_class_fde_ref,
3801   dw_val_class_lbl_id,
3802   dw_val_class_lineptr,
3803   dw_val_class_str,
3804   dw_val_class_macptr,
3805   dw_val_class_file
3806 };
3807
3808 /* Describe a double word constant value.  */
3809 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3810
3811 typedef struct GTY(()) dw_long_long_struct {
3812   unsigned long hi;
3813   unsigned long low;
3814 }
3815 dw_long_long_const;
3816
3817 /* Describe a floating point constant value, or a vector constant value.  */
3818
3819 typedef struct GTY(()) dw_vec_struct {
3820   unsigned char * GTY((length ("%h.length"))) array;
3821   unsigned length;
3822   unsigned elt_size;
3823 }
3824 dw_vec_const;
3825
3826 /* The dw_val_node describes an attribute's value, as it is
3827    represented internally.  */
3828
3829 typedef struct GTY(()) dw_val_struct {
3830   enum dw_val_class val_class;
3831   union dw_val_struct_union
3832     {
3833       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3834       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3835       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3836       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3837       HOST_WIDE_INT GTY ((default)) val_int;
3838       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3839       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3840       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3841       struct dw_val_die_union
3842         {
3843           dw_die_ref die;
3844           int external;
3845         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3846       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3847       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3848       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3849       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3850       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3851     }
3852   GTY ((desc ("%1.val_class"))) v;
3853 }
3854 dw_val_node;
3855
3856 /* Locations in memory are described using a sequence of stack machine
3857    operations.  */
3858
3859 typedef struct GTY(()) dw_loc_descr_struct {
3860   dw_loc_descr_ref dw_loc_next;
3861   enum dwarf_location_atom dw_loc_opc;
3862   int dw_loc_addr;
3863   dw_val_node dw_loc_oprnd1;
3864   dw_val_node dw_loc_oprnd2;
3865 }
3866 dw_loc_descr_node;
3867
3868 /* Location lists are ranges + location descriptions for that range,
3869    so you can track variables that are in different places over
3870    their entire life.  */
3871 typedef struct GTY(()) dw_loc_list_struct {
3872   dw_loc_list_ref dw_loc_next;
3873   const char *begin; /* Label for begin address of range */
3874   const char *end;  /* Label for end address of range */
3875   char *ll_symbol; /* Label for beginning of location list.
3876                       Only on head of list */
3877   const char *section; /* Section this loclist is relative to */
3878   dw_loc_descr_ref expr;
3879 } dw_loc_list_node;
3880
3881 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3882
3883 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3884
3885 /* Convert a DWARF stack opcode into its string name.  */
3886
3887 static const char *
3888 dwarf_stack_op_name (unsigned int op)
3889 {
3890   switch (op)
3891     {
3892     case DW_OP_addr:
3893     case INTERNAL_DW_OP_tls_addr:
3894       return "DW_OP_addr";
3895     case DW_OP_deref:
3896       return "DW_OP_deref";
3897     case DW_OP_const1u:
3898       return "DW_OP_const1u";
3899     case DW_OP_const1s:
3900       return "DW_OP_const1s";
3901     case DW_OP_const2u:
3902       return "DW_OP_const2u";
3903     case DW_OP_const2s:
3904       return "DW_OP_const2s";
3905     case DW_OP_const4u:
3906       return "DW_OP_const4u";
3907     case DW_OP_const4s:
3908       return "DW_OP_const4s";
3909     case DW_OP_const8u:
3910       return "DW_OP_const8u";
3911     case DW_OP_const8s:
3912       return "DW_OP_const8s";
3913     case DW_OP_constu:
3914       return "DW_OP_constu";
3915     case DW_OP_consts:
3916       return "DW_OP_consts";
3917     case DW_OP_dup:
3918       return "DW_OP_dup";
3919     case DW_OP_drop:
3920       return "DW_OP_drop";
3921     case DW_OP_over:
3922       return "DW_OP_over";
3923     case DW_OP_pick:
3924       return "DW_OP_pick";
3925     case DW_OP_swap:
3926       return "DW_OP_swap";
3927     case DW_OP_rot:
3928       return "DW_OP_rot";
3929     case DW_OP_xderef:
3930       return "DW_OP_xderef";
3931     case DW_OP_abs:
3932       return "DW_OP_abs";
3933     case DW_OP_and:
3934       return "DW_OP_and";
3935     case DW_OP_div:
3936       return "DW_OP_div";
3937     case DW_OP_minus:
3938       return "DW_OP_minus";
3939     case DW_OP_mod:
3940       return "DW_OP_mod";
3941     case DW_OP_mul:
3942       return "DW_OP_mul";
3943     case DW_OP_neg:
3944       return "DW_OP_neg";
3945     case DW_OP_not:
3946       return "DW_OP_not";
3947     case DW_OP_or:
3948       return "DW_OP_or";
3949     case DW_OP_plus:
3950       return "DW_OP_plus";
3951     case DW_OP_plus_uconst:
3952       return "DW_OP_plus_uconst";
3953     case DW_OP_shl:
3954       return "DW_OP_shl";
3955     case DW_OP_shr:
3956       return "DW_OP_shr";
3957     case DW_OP_shra:
3958       return "DW_OP_shra";
3959     case DW_OP_xor:
3960       return "DW_OP_xor";
3961     case DW_OP_bra:
3962       return "DW_OP_bra";
3963     case DW_OP_eq:
3964       return "DW_OP_eq";
3965     case DW_OP_ge:
3966       return "DW_OP_ge";
3967     case DW_OP_gt:
3968       return "DW_OP_gt";
3969     case DW_OP_le:
3970       return "DW_OP_le";
3971     case DW_OP_lt:
3972       return "DW_OP_lt";
3973     case DW_OP_ne:
3974       return "DW_OP_ne";
3975     case DW_OP_skip:
3976       return "DW_OP_skip";
3977     case DW_OP_lit0:
3978       return "DW_OP_lit0";
3979     case DW_OP_lit1:
3980       return "DW_OP_lit1";
3981     case DW_OP_lit2:
3982       return "DW_OP_lit2";
3983     case DW_OP_lit3:
3984       return "DW_OP_lit3";
3985     case DW_OP_lit4:
3986       return "DW_OP_lit4";
3987     case DW_OP_lit5:
3988       return "DW_OP_lit5";
3989     case DW_OP_lit6:
3990       return "DW_OP_lit6";
3991     case DW_OP_lit7:
3992       return "DW_OP_lit7";
3993     case DW_OP_lit8:
3994       return "DW_OP_lit8";
3995     case DW_OP_lit9:
3996       return "DW_OP_lit9";
3997     case DW_OP_lit10:
3998       return "DW_OP_lit10";
3999     case DW_OP_lit11:
4000       return "DW_OP_lit11";
4001     case DW_OP_lit12:
4002       return "DW_OP_lit12";
4003     case DW_OP_lit13:
4004       return "DW_OP_lit13";
4005     case DW_OP_lit14:
4006       return "DW_OP_lit14";
4007     case DW_OP_lit15:
4008       return "DW_OP_lit15";
4009     case DW_OP_lit16:
4010       return "DW_OP_lit16";
4011     case DW_OP_lit17:
4012       return "DW_OP_lit17";
4013     case DW_OP_lit18:
4014       return "DW_OP_lit18";
4015     case DW_OP_lit19:
4016       return "DW_OP_lit19";
4017     case DW_OP_lit20:
4018       return "DW_OP_lit20";
4019     case DW_OP_lit21:
4020       return "DW_OP_lit21";
4021     case DW_OP_lit22:
4022       return "DW_OP_lit22";
4023     case DW_OP_lit23:
4024       return "DW_OP_lit23";
4025     case DW_OP_lit24:
4026       return "DW_OP_lit24";
4027     case DW_OP_lit25:
4028       return "DW_OP_lit25";
4029     case DW_OP_lit26:
4030       return "DW_OP_lit26";
4031     case DW_OP_lit27:
4032       return "DW_OP_lit27";
4033     case DW_OP_lit28:
4034       return "DW_OP_lit28";
4035     case DW_OP_lit29:
4036       return "DW_OP_lit29";
4037     case DW_OP_lit30:
4038       return "DW_OP_lit30";
4039     case DW_OP_lit31:
4040       return "DW_OP_lit31";
4041     case DW_OP_reg0:
4042       return "DW_OP_reg0";
4043     case DW_OP_reg1:
4044       return "DW_OP_reg1";
4045     case DW_OP_reg2:
4046       return "DW_OP_reg2";
4047     case DW_OP_reg3:
4048       return "DW_OP_reg3";
4049     case DW_OP_reg4:
4050       return "DW_OP_reg4";
4051     case DW_OP_reg5:
4052       return "DW_OP_reg5";
4053     case DW_OP_reg6:
4054       return "DW_OP_reg6";
4055     case DW_OP_reg7:
4056       return "DW_OP_reg7";
4057     case DW_OP_reg8:
4058       return "DW_OP_reg8";
4059     case DW_OP_reg9:
4060       return "DW_OP_reg9";
4061     case DW_OP_reg10:
4062       return "DW_OP_reg10";
4063     case DW_OP_reg11:
4064       return "DW_OP_reg11";
4065     case DW_OP_reg12:
4066       return "DW_OP_reg12";
4067     case DW_OP_reg13:
4068       return "DW_OP_reg13";
4069     case DW_OP_reg14:
4070       return "DW_OP_reg14";
4071     case DW_OP_reg15:
4072       return "DW_OP_reg15";
4073     case DW_OP_reg16:
4074       return "DW_OP_reg16";
4075     case DW_OP_reg17:
4076       return "DW_OP_reg17";
4077     case DW_OP_reg18:
4078       return "DW_OP_reg18";
4079     case DW_OP_reg19:
4080       return "DW_OP_reg19";
4081     case DW_OP_reg20:
4082       return "DW_OP_reg20";
4083     case DW_OP_reg21:
4084       return "DW_OP_reg21";
4085     case DW_OP_reg22:
4086       return "DW_OP_reg22";
4087     case DW_OP_reg23:
4088       return "DW_OP_reg23";
4089     case DW_OP_reg24:
4090       return "DW_OP_reg24";
4091     case DW_OP_reg25:
4092       return "DW_OP_reg25";
4093     case DW_OP_reg26:
4094       return "DW_OP_reg26";
4095     case DW_OP_reg27:
4096       return "DW_OP_reg27";
4097     case DW_OP_reg28:
4098       return "DW_OP_reg28";
4099     case DW_OP_reg29:
4100       return "DW_OP_reg29";
4101     case DW_OP_reg30:
4102       return "DW_OP_reg30";
4103     case DW_OP_reg31:
4104       return "DW_OP_reg31";
4105     case DW_OP_breg0:
4106       return "DW_OP_breg0";
4107     case DW_OP_breg1:
4108       return "DW_OP_breg1";
4109     case DW_OP_breg2:
4110       return "DW_OP_breg2";
4111     case DW_OP_breg3:
4112       return "DW_OP_breg3";
4113     case DW_OP_breg4:
4114       return "DW_OP_breg4";
4115     case DW_OP_breg5:
4116       return "DW_OP_breg5";
4117     case DW_OP_breg6:
4118       return "DW_OP_breg6";
4119     case DW_OP_breg7:
4120       return "DW_OP_breg7";
4121     case DW_OP_breg8:
4122       return "DW_OP_breg8";
4123     case DW_OP_breg9:
4124       return "DW_OP_breg9";
4125     case DW_OP_breg10:
4126       return "DW_OP_breg10";
4127     case DW_OP_breg11:
4128       return "DW_OP_breg11";
4129     case DW_OP_breg12:
4130       return "DW_OP_breg12";
4131     case DW_OP_breg13:
4132       return "DW_OP_breg13";
4133     case DW_OP_breg14:
4134       return "DW_OP_breg14";
4135     case DW_OP_breg15:
4136       return "DW_OP_breg15";
4137     case DW_OP_breg16:
4138       return "DW_OP_breg16";
4139     case DW_OP_breg17:
4140       return "DW_OP_breg17";
4141     case DW_OP_breg18:
4142       return "DW_OP_breg18";
4143     case DW_OP_breg19:
4144       return "DW_OP_breg19";
4145     case DW_OP_breg20:
4146       return "DW_OP_breg20";
4147     case DW_OP_breg21:
4148       return "DW_OP_breg21";
4149     case DW_OP_breg22:
4150       return "DW_OP_breg22";
4151     case DW_OP_breg23:
4152       return "DW_OP_breg23";
4153     case DW_OP_breg24:
4154       return "DW_OP_breg24";
4155     case DW_OP_breg25:
4156       return "DW_OP_breg25";
4157     case DW_OP_breg26:
4158       return "DW_OP_breg26";
4159     case DW_OP_breg27:
4160       return "DW_OP_breg27";
4161     case DW_OP_breg28:
4162       return "DW_OP_breg28";
4163     case DW_OP_breg29:
4164       return "DW_OP_breg29";
4165     case DW_OP_breg30:
4166       return "DW_OP_breg30";
4167     case DW_OP_breg31:
4168       return "DW_OP_breg31";
4169     case DW_OP_regx:
4170       return "DW_OP_regx";
4171     case DW_OP_fbreg:
4172       return "DW_OP_fbreg";
4173     case DW_OP_bregx:
4174       return "DW_OP_bregx";
4175     case DW_OP_piece:
4176       return "DW_OP_piece";
4177     case DW_OP_deref_size:
4178       return "DW_OP_deref_size";
4179     case DW_OP_xderef_size:
4180       return "DW_OP_xderef_size";
4181     case DW_OP_nop:
4182       return "DW_OP_nop";
4183     case DW_OP_push_object_address:
4184       return "DW_OP_push_object_address";
4185     case DW_OP_call2:
4186       return "DW_OP_call2";
4187     case DW_OP_call4:
4188       return "DW_OP_call4";
4189     case DW_OP_call_ref:
4190       return "DW_OP_call_ref";
4191     case DW_OP_GNU_push_tls_address:
4192       return "DW_OP_GNU_push_tls_address";
4193     case DW_OP_GNU_uninit:
4194       return "DW_OP_GNU_uninit";
4195     default:
4196       return "OP_<unknown>";
4197     }
4198 }
4199
4200 /* Return a pointer to a newly allocated location description.  Location
4201    descriptions are simple expression terms that can be strung
4202    together to form more complicated location (address) descriptions.  */
4203
4204 static inline dw_loc_descr_ref
4205 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4206                unsigned HOST_WIDE_INT oprnd2)
4207 {
4208   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4209
4210   descr->dw_loc_opc = op;
4211   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4212   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4213   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4214   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4215
4216   return descr;
4217 }
4218
4219 /* Return a pointer to a newly allocated location description for
4220    REG and OFFSET.  */
4221
4222 static inline dw_loc_descr_ref
4223 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4224 {
4225   if (reg <= 31)
4226     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4227                           offset, 0);
4228   else
4229     return new_loc_descr (DW_OP_bregx, reg, offset);
4230 }
4231
4232 /* Add a location description term to a location description expression.  */
4233
4234 static inline void
4235 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4236 {
4237   dw_loc_descr_ref *d;
4238
4239   /* Find the end of the chain.  */
4240   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4241     ;
4242
4243   *d = descr;
4244 }
4245
4246 /* Add a constant OFFSET to a location expression.  */
4247
4248 static void
4249 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4250 {
4251   dw_loc_descr_ref loc;
4252   HOST_WIDE_INT *p;
4253
4254   gcc_assert (*list_head != NULL);
4255
4256   if (!offset)
4257     return;
4258
4259   /* Find the end of the chain.  */
4260   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4261     ;
4262
4263   p = NULL;
4264   if (loc->dw_loc_opc == DW_OP_fbreg
4265       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4266     p = &loc->dw_loc_oprnd1.v.val_int;
4267   else if (loc->dw_loc_opc == DW_OP_bregx)
4268     p = &loc->dw_loc_oprnd2.v.val_int;
4269
4270   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4271      offset.  Don't optimize if an signed integer overflow would happen.  */
4272   if (p != NULL
4273       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4274           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4275     *p += offset;
4276
4277   else if (offset > 0)
4278     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4279
4280   else
4281     {
4282       loc->dw_loc_next = int_loc_descriptor (offset);
4283       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4284     }
4285 }
4286
4287 /* Return the size of a location descriptor.  */
4288
4289 static unsigned long
4290 size_of_loc_descr (dw_loc_descr_ref loc)
4291 {
4292   unsigned long size = 1;
4293
4294   switch (loc->dw_loc_opc)
4295     {
4296     case DW_OP_addr:
4297     case INTERNAL_DW_OP_tls_addr:
4298       size += DWARF2_ADDR_SIZE;
4299       break;
4300     case DW_OP_const1u:
4301     case DW_OP_const1s:
4302       size += 1;
4303       break;
4304     case DW_OP_const2u:
4305     case DW_OP_const2s:
4306       size += 2;
4307       break;
4308     case DW_OP_const4u:
4309     case DW_OP_const4s:
4310       size += 4;
4311       break;
4312     case DW_OP_const8u:
4313     case DW_OP_const8s:
4314       size += 8;
4315       break;
4316     case DW_OP_constu:
4317       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4318       break;
4319     case DW_OP_consts:
4320       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4321       break;
4322     case DW_OP_pick:
4323       size += 1;
4324       break;
4325     case DW_OP_plus_uconst:
4326       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4327       break;
4328     case DW_OP_skip:
4329     case DW_OP_bra:
4330       size += 2;
4331       break;
4332     case DW_OP_breg0:
4333     case DW_OP_breg1:
4334     case DW_OP_breg2:
4335     case DW_OP_breg3:
4336     case DW_OP_breg4:
4337     case DW_OP_breg5:
4338     case DW_OP_breg6:
4339     case DW_OP_breg7:
4340     case DW_OP_breg8:
4341     case DW_OP_breg9:
4342     case DW_OP_breg10:
4343     case DW_OP_breg11:
4344     case DW_OP_breg12:
4345     case DW_OP_breg13:
4346     case DW_OP_breg14:
4347     case DW_OP_breg15:
4348     case DW_OP_breg16:
4349     case DW_OP_breg17:
4350     case DW_OP_breg18:
4351     case DW_OP_breg19:
4352     case DW_OP_breg20:
4353     case DW_OP_breg21:
4354     case DW_OP_breg22:
4355     case DW_OP_breg23:
4356     case DW_OP_breg24:
4357     case DW_OP_breg25:
4358     case DW_OP_breg26:
4359     case DW_OP_breg27:
4360     case DW_OP_breg28:
4361     case DW_OP_breg29:
4362     case DW_OP_breg30:
4363     case DW_OP_breg31:
4364       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4365       break;
4366     case DW_OP_regx:
4367       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4368       break;
4369     case DW_OP_fbreg:
4370       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4371       break;
4372     case DW_OP_bregx:
4373       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4374       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4375       break;
4376     case DW_OP_piece:
4377       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4378       break;
4379     case DW_OP_deref_size:
4380     case DW_OP_xderef_size:
4381       size += 1;
4382       break;
4383     case DW_OP_call2:
4384       size += 2;
4385       break;
4386     case DW_OP_call4:
4387       size += 4;
4388       break;
4389     case DW_OP_call_ref:
4390       size += DWARF2_ADDR_SIZE;
4391       break;
4392     default:
4393       break;
4394     }
4395
4396   return size;
4397 }
4398
4399 /* Return the size of a series of location descriptors.  */
4400
4401 static unsigned long
4402 size_of_locs (dw_loc_descr_ref loc)
4403 {
4404   dw_loc_descr_ref l;
4405   unsigned long size;
4406
4407   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4408      field, to avoid writing to a PCH file.  */
4409   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4410     {
4411       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4412         break;
4413       size += size_of_loc_descr (l);
4414     }
4415   if (! l)
4416     return size;
4417
4418   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4419     {
4420       l->dw_loc_addr = size;
4421       size += size_of_loc_descr (l);
4422     }
4423
4424   return size;
4425 }
4426
4427 /* Output location description stack opcode's operands (if any).  */
4428
4429 static void
4430 output_loc_operands (dw_loc_descr_ref loc)
4431 {
4432   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4433   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4434
4435   switch (loc->dw_loc_opc)
4436     {
4437 #ifdef DWARF2_DEBUGGING_INFO
4438     case DW_OP_addr:
4439       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4440       break;
4441     case DW_OP_const2u:
4442     case DW_OP_const2s:
4443       dw2_asm_output_data (2, val1->v.val_int, NULL);
4444       break;
4445     case DW_OP_const4u:
4446     case DW_OP_const4s:
4447       dw2_asm_output_data (4, val1->v.val_int, NULL);
4448       break;
4449     case DW_OP_const8u:
4450     case DW_OP_const8s:
4451       gcc_assert (HOST_BITS_PER_LONG >= 64);
4452       dw2_asm_output_data (8, val1->v.val_int, NULL);
4453       break;
4454     case DW_OP_skip:
4455     case DW_OP_bra:
4456       {
4457         int offset;
4458
4459         gcc_assert (val1->val_class == dw_val_class_loc);
4460         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4461
4462         dw2_asm_output_data (2, offset, NULL);
4463       }
4464       break;
4465 #else
4466     case DW_OP_addr:
4467     case DW_OP_const2u:
4468     case DW_OP_const2s:
4469     case DW_OP_const4u:
4470     case DW_OP_const4s:
4471     case DW_OP_const8u:
4472     case DW_OP_const8s:
4473     case DW_OP_skip:
4474     case DW_OP_bra:
4475       /* We currently don't make any attempt to make sure these are
4476          aligned properly like we do for the main unwind info, so
4477          don't support emitting things larger than a byte if we're
4478          only doing unwinding.  */
4479       gcc_unreachable ();
4480 #endif
4481     case DW_OP_const1u:
4482     case DW_OP_const1s:
4483       dw2_asm_output_data (1, val1->v.val_int, NULL);
4484       break;
4485     case DW_OP_constu:
4486       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4487       break;
4488     case DW_OP_consts:
4489       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4490       break;
4491     case DW_OP_pick:
4492       dw2_asm_output_data (1, val1->v.val_int, NULL);
4493       break;
4494     case DW_OP_plus_uconst:
4495       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4496       break;
4497     case DW_OP_breg0:
4498     case DW_OP_breg1:
4499     case DW_OP_breg2:
4500     case DW_OP_breg3:
4501     case DW_OP_breg4:
4502     case DW_OP_breg5:
4503     case DW_OP_breg6:
4504     case DW_OP_breg7:
4505     case DW_OP_breg8:
4506     case DW_OP_breg9:
4507     case DW_OP_breg10:
4508     case DW_OP_breg11:
4509     case DW_OP_breg12:
4510     case DW_OP_breg13:
4511     case DW_OP_breg14:
4512     case DW_OP_breg15:
4513     case DW_OP_breg16:
4514     case DW_OP_breg17:
4515     case DW_OP_breg18:
4516     case DW_OP_breg19:
4517     case DW_OP_breg20:
4518     case DW_OP_breg21:
4519     case DW_OP_breg22:
4520     case DW_OP_breg23:
4521     case DW_OP_breg24:
4522     case DW_OP_breg25:
4523     case DW_OP_breg26:
4524     case DW_OP_breg27:
4525     case DW_OP_breg28:
4526     case DW_OP_breg29:
4527     case DW_OP_breg30:
4528     case DW_OP_breg31:
4529       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4530       break;
4531     case DW_OP_regx:
4532       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4533       break;
4534     case DW_OP_fbreg:
4535       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4536       break;
4537     case DW_OP_bregx:
4538       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4539       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4540       break;
4541     case DW_OP_piece:
4542       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4543       break;
4544     case DW_OP_deref_size:
4545     case DW_OP_xderef_size:
4546       dw2_asm_output_data (1, val1->v.val_int, NULL);
4547       break;
4548
4549     case INTERNAL_DW_OP_tls_addr:
4550       if (targetm.asm_out.output_dwarf_dtprel)
4551         {
4552           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4553                                                DWARF2_ADDR_SIZE,
4554                                                val1->v.val_addr);
4555           fputc ('\n', asm_out_file);
4556         }
4557       else
4558         gcc_unreachable ();
4559       break;
4560
4561     default:
4562       /* Other codes have no operands.  */
4563       break;
4564     }
4565 }
4566
4567 /* Output a sequence of location operations.  */
4568
4569 static void
4570 output_loc_sequence (dw_loc_descr_ref loc)
4571 {
4572   for (; loc != NULL; loc = loc->dw_loc_next)
4573     {
4574       /* Output the opcode.  */
4575       dw2_asm_output_data (1, loc->dw_loc_opc,
4576                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4577
4578       /* Output the operand(s) (if any).  */
4579       output_loc_operands (loc);
4580     }
4581 }
4582
4583 /* Output location description stack opcode's operands (if any).
4584    The output is single bytes on a line, suitable for .cfi_escape.  */
4585
4586 static void
4587 output_loc_operands_raw (dw_loc_descr_ref loc)
4588 {
4589   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4590   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4591
4592   switch (loc->dw_loc_opc)
4593     {
4594     case DW_OP_addr:
4595       /* We cannot output addresses in .cfi_escape, only bytes.  */
4596       gcc_unreachable ();
4597
4598     case DW_OP_const1u:
4599     case DW_OP_const1s:
4600     case DW_OP_pick:
4601     case DW_OP_deref_size:
4602     case DW_OP_xderef_size:
4603       fputc (',', asm_out_file);
4604       dw2_asm_output_data_raw (1, val1->v.val_int);
4605       break;
4606
4607     case DW_OP_const2u:
4608     case DW_OP_const2s:
4609       fputc (',', asm_out_file);
4610       dw2_asm_output_data_raw (2, val1->v.val_int);
4611       break;
4612
4613     case DW_OP_const4u:
4614     case DW_OP_const4s:
4615       fputc (',', asm_out_file);
4616       dw2_asm_output_data_raw (4, val1->v.val_int);
4617       break;
4618
4619     case DW_OP_const8u:
4620     case DW_OP_const8s:
4621       gcc_assert (HOST_BITS_PER_LONG >= 64);
4622       fputc (',', asm_out_file);
4623       dw2_asm_output_data_raw (8, val1->v.val_int);
4624       break;
4625
4626     case DW_OP_skip:
4627     case DW_OP_bra:
4628       {
4629         int offset;
4630
4631         gcc_assert (val1->val_class == dw_val_class_loc);
4632         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4633
4634         fputc (',', asm_out_file);
4635         dw2_asm_output_data_raw (2, offset);
4636       }
4637       break;
4638
4639     case DW_OP_constu:
4640     case DW_OP_plus_uconst:
4641     case DW_OP_regx:
4642     case DW_OP_piece:
4643       fputc (',', asm_out_file);
4644       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4645       break;
4646
4647     case DW_OP_consts:
4648     case DW_OP_breg0:
4649     case DW_OP_breg1:
4650     case DW_OP_breg2:
4651     case DW_OP_breg3:
4652     case DW_OP_breg4:
4653     case DW_OP_breg5:
4654     case DW_OP_breg6:
4655     case DW_OP_breg7:
4656     case DW_OP_breg8:
4657     case DW_OP_breg9:
4658     case DW_OP_breg10:
4659     case DW_OP_breg11:
4660     case DW_OP_breg12:
4661     case DW_OP_breg13:
4662     case DW_OP_breg14:
4663     case DW_OP_breg15:
4664     case DW_OP_breg16:
4665     case DW_OP_breg17:
4666     case DW_OP_breg18:
4667     case DW_OP_breg19:
4668     case DW_OP_breg20:
4669     case DW_OP_breg21:
4670     case DW_OP_breg22:
4671     case DW_OP_breg23:
4672     case DW_OP_breg24:
4673     case DW_OP_breg25:
4674     case DW_OP_breg26:
4675     case DW_OP_breg27:
4676     case DW_OP_breg28:
4677     case DW_OP_breg29:
4678     case DW_OP_breg30:
4679     case DW_OP_breg31:
4680     case DW_OP_fbreg:
4681       fputc (',', asm_out_file);
4682       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4683       break;
4684
4685     case DW_OP_bregx:
4686       fputc (',', asm_out_file);
4687       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4688       fputc (',', asm_out_file);
4689       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4690       break;
4691
4692     case INTERNAL_DW_OP_tls_addr:
4693       gcc_unreachable ();
4694
4695     default:
4696       /* Other codes have no operands.  */
4697       break;
4698     }
4699 }
4700
4701 static void
4702 output_loc_sequence_raw (dw_loc_descr_ref loc)
4703 {
4704   while (1)
4705     {
4706       /* Output the opcode.  */
4707       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4708       output_loc_operands_raw (loc);
4709
4710       if (!loc->dw_loc_next)
4711         break;
4712       loc = loc->dw_loc_next;
4713
4714       fputc (',', asm_out_file);
4715     }
4716 }
4717
4718 /* This routine will generate the correct assembly data for a location
4719    description based on a cfi entry with a complex address.  */
4720
4721 static void
4722 output_cfa_loc (dw_cfi_ref cfi)
4723 {
4724   dw_loc_descr_ref loc;
4725   unsigned long size;
4726
4727   if (cfi->dw_cfi_opc == DW_CFA_expression)
4728     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4729
4730   /* Output the size of the block.  */
4731   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4732   size = size_of_locs (loc);
4733   dw2_asm_output_data_uleb128 (size, NULL);
4734
4735   /* Now output the operations themselves.  */
4736   output_loc_sequence (loc);
4737 }
4738
4739 /* Similar, but used for .cfi_escape.  */
4740
4741 static void
4742 output_cfa_loc_raw (dw_cfi_ref cfi)
4743 {
4744   dw_loc_descr_ref loc;
4745   unsigned long size;
4746
4747   if (cfi->dw_cfi_opc == DW_CFA_expression)
4748     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4749
4750   /* Output the size of the block.  */
4751   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4752   size = size_of_locs (loc);
4753   dw2_asm_output_data_uleb128_raw (size);
4754   fputc (',', asm_out_file);
4755
4756   /* Now output the operations themselves.  */
4757   output_loc_sequence_raw (loc);
4758 }
4759
4760 /* This function builds a dwarf location descriptor sequence from a
4761    dw_cfa_location, adding the given OFFSET to the result of the
4762    expression.  */
4763
4764 static struct dw_loc_descr_struct *
4765 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4766 {
4767   struct dw_loc_descr_struct *head, *tmp;
4768
4769   offset += cfa->offset;
4770
4771   if (cfa->indirect)
4772     {
4773       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4774       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4775       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4776       add_loc_descr (&head, tmp);
4777       if (offset != 0)
4778         {
4779           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4780           add_loc_descr (&head, tmp);
4781         }
4782     }
4783   else
4784     head = new_reg_loc_descr (cfa->reg, offset);
4785
4786   return head;
4787 }
4788
4789 /* This function builds a dwarf location descriptor sequence for
4790    the address at OFFSET from the CFA when stack is aligned to
4791    ALIGNMENT byte.  */
4792
4793 static struct dw_loc_descr_struct *
4794 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4795 {
4796   struct dw_loc_descr_struct *head;
4797   unsigned int dwarf_fp
4798     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4799
4800  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4801   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4802     {
4803       head = new_reg_loc_descr (dwarf_fp, 0);
4804       add_loc_descr (&head, int_loc_descriptor (alignment));
4805       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4806       loc_descr_plus_const (&head, offset);
4807     }
4808   else
4809     head = new_reg_loc_descr (dwarf_fp, offset);
4810   return head;
4811 }
4812
4813 /* This function fills in aa dw_cfa_location structure from a dwarf location
4814    descriptor sequence.  */
4815
4816 static void
4817 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4818 {
4819   struct dw_loc_descr_struct *ptr;
4820   cfa->offset = 0;
4821   cfa->base_offset = 0;
4822   cfa->indirect = 0;
4823   cfa->reg = -1;
4824
4825   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4826     {
4827       enum dwarf_location_atom op = ptr->dw_loc_opc;
4828
4829       switch (op)
4830         {
4831         case DW_OP_reg0:
4832         case DW_OP_reg1:
4833         case DW_OP_reg2:
4834         case DW_OP_reg3:
4835         case DW_OP_reg4:
4836         case DW_OP_reg5:
4837         case DW_OP_reg6:
4838         case DW_OP_reg7:
4839         case DW_OP_reg8:
4840         case DW_OP_reg9:
4841         case DW_OP_reg10:
4842         case DW_OP_reg11:
4843         case DW_OP_reg12:
4844         case DW_OP_reg13:
4845         case DW_OP_reg14:
4846         case DW_OP_reg15:
4847         case DW_OP_reg16:
4848         case DW_OP_reg17:
4849         case DW_OP_reg18:
4850         case DW_OP_reg19:
4851         case DW_OP_reg20:
4852         case DW_OP_reg21:
4853         case DW_OP_reg22:
4854         case DW_OP_reg23:
4855         case DW_OP_reg24:
4856         case DW_OP_reg25:
4857         case DW_OP_reg26:
4858         case DW_OP_reg27:
4859         case DW_OP_reg28:
4860         case DW_OP_reg29:
4861         case DW_OP_reg30:
4862         case DW_OP_reg31:
4863           cfa->reg = op - DW_OP_reg0;
4864           break;
4865         case DW_OP_regx:
4866           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4867           break;
4868         case DW_OP_breg0:
4869         case DW_OP_breg1:
4870         case DW_OP_breg2:
4871         case DW_OP_breg3:
4872         case DW_OP_breg4:
4873         case DW_OP_breg5:
4874         case DW_OP_breg6:
4875         case DW_OP_breg7:
4876         case DW_OP_breg8:
4877         case DW_OP_breg9:
4878         case DW_OP_breg10:
4879         case DW_OP_breg11:
4880         case DW_OP_breg12:
4881         case DW_OP_breg13:
4882         case DW_OP_breg14:
4883         case DW_OP_breg15:
4884         case DW_OP_breg16:
4885         case DW_OP_breg17:
4886         case DW_OP_breg18:
4887         case DW_OP_breg19:
4888         case DW_OP_breg20:
4889         case DW_OP_breg21:
4890         case DW_OP_breg22:
4891         case DW_OP_breg23:
4892         case DW_OP_breg24:
4893         case DW_OP_breg25:
4894         case DW_OP_breg26:
4895         case DW_OP_breg27:
4896         case DW_OP_breg28:
4897         case DW_OP_breg29:
4898         case DW_OP_breg30:
4899         case DW_OP_breg31:
4900           cfa->reg = op - DW_OP_breg0;
4901           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4902           break;
4903         case DW_OP_bregx:
4904           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4905           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4906           break;
4907         case DW_OP_deref:
4908           cfa->indirect = 1;
4909           break;
4910         case DW_OP_plus_uconst:
4911           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4912           break;
4913         default:
4914           internal_error ("DW_LOC_OP %s not implemented",
4915                           dwarf_stack_op_name (ptr->dw_loc_opc));
4916         }
4917     }
4918 }
4919 #endif /* .debug_frame support */
4920 \f
4921 /* And now, the support for symbolic debugging information.  */
4922 #ifdef DWARF2_DEBUGGING_INFO
4923
4924 /* .debug_str support.  */
4925 static int output_indirect_string (void **, void *);
4926
4927 static void dwarf2out_init (const char *);
4928 static void dwarf2out_finish (const char *);
4929 static void dwarf2out_define (unsigned int, const char *);
4930 static void dwarf2out_undef (unsigned int, const char *);
4931 static void dwarf2out_start_source_file (unsigned, const char *);
4932 static void dwarf2out_end_source_file (unsigned);
4933 static void dwarf2out_begin_block (unsigned, unsigned);
4934 static void dwarf2out_end_block (unsigned, unsigned);
4935 static bool dwarf2out_ignore_block (const_tree);
4936 static void dwarf2out_global_decl (tree);
4937 static void dwarf2out_type_decl (tree, int);
4938 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4939 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4940                                                  dw_die_ref);
4941 static void dwarf2out_abstract_function (tree);
4942 static void dwarf2out_var_location (rtx);
4943 static void dwarf2out_begin_function (tree);
4944 static void dwarf2out_set_name (tree, tree);
4945
4946 /* The debug hooks structure.  */
4947
4948 const struct gcc_debug_hooks dwarf2_debug_hooks =
4949 {
4950   dwarf2out_init,
4951   dwarf2out_finish,
4952   dwarf2out_define,
4953   dwarf2out_undef,
4954   dwarf2out_start_source_file,
4955   dwarf2out_end_source_file,
4956   dwarf2out_begin_block,
4957   dwarf2out_end_block,
4958   dwarf2out_ignore_block,
4959   dwarf2out_source_line,
4960   dwarf2out_begin_prologue,
4961   debug_nothing_int_charstar,   /* end_prologue */
4962   dwarf2out_end_epilogue,
4963   dwarf2out_begin_function,
4964   debug_nothing_int,            /* end_function */
4965   dwarf2out_decl,               /* function_decl */
4966   dwarf2out_global_decl,
4967   dwarf2out_type_decl,          /* type_decl */
4968   dwarf2out_imported_module_or_decl,
4969   debug_nothing_tree,           /* deferred_inline_function */
4970   /* The DWARF 2 backend tries to reduce debugging bloat by not
4971      emitting the abstract description of inline functions until
4972      something tries to reference them.  */
4973   dwarf2out_abstract_function,  /* outlining_inline_function */
4974   debug_nothing_rtx,            /* label */
4975   debug_nothing_int,            /* handle_pch */
4976   dwarf2out_var_location,
4977   dwarf2out_switch_text_section,
4978   dwarf2out_set_name,
4979   1                             /* start_end_main_source_file */
4980 };
4981 #endif
4982 \f
4983 /* NOTE: In the comments in this file, many references are made to
4984    "Debugging Information Entries".  This term is abbreviated as `DIE'
4985    throughout the remainder of this file.  */
4986
4987 /* An internal representation of the DWARF output is built, and then
4988    walked to generate the DWARF debugging info.  The walk of the internal
4989    representation is done after the entire program has been compiled.
4990    The types below are used to describe the internal representation.  */
4991
4992 /* Various DIE's use offsets relative to the beginning of the
4993    .debug_info section to refer to each other.  */
4994
4995 typedef long int dw_offset;
4996
4997 /* Define typedefs here to avoid circular dependencies.  */
4998
4999 typedef struct dw_attr_struct *dw_attr_ref;
5000 typedef struct dw_line_info_struct *dw_line_info_ref;
5001 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5002 typedef struct pubname_struct *pubname_ref;
5003 typedef struct dw_ranges_struct *dw_ranges_ref;
5004 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5005
5006 /* Each entry in the line_info_table maintains the file and
5007    line number associated with the label generated for that
5008    entry.  The label gives the PC value associated with
5009    the line number entry.  */
5010
5011 typedef struct GTY(()) dw_line_info_struct {
5012   unsigned long dw_file_num;
5013   unsigned long dw_line_num;
5014 }
5015 dw_line_info_entry;
5016
5017 /* Line information for functions in separate sections; each one gets its
5018    own sequence.  */
5019 typedef struct GTY(()) dw_separate_line_info_struct {
5020   unsigned long dw_file_num;
5021   unsigned long dw_line_num;
5022   unsigned long function;
5023 }
5024 dw_separate_line_info_entry;
5025
5026 /* Each DIE attribute has a field specifying the attribute kind,
5027    a link to the next attribute in the chain, and an attribute value.
5028    Attributes are typically linked below the DIE they modify.  */
5029
5030 typedef struct GTY(()) dw_attr_struct {
5031   enum dwarf_attribute dw_attr;
5032   dw_val_node dw_attr_val;
5033 }
5034 dw_attr_node;
5035
5036 DEF_VEC_O(dw_attr_node);
5037 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5038
5039 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5040    The children of each node form a circular list linked by
5041    die_sib.  die_child points to the node *before* the "first" child node.  */
5042
5043 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5044   enum dwarf_tag die_tag;
5045   char *die_symbol;
5046   VEC(dw_attr_node,gc) * die_attr;
5047   dw_die_ref die_parent;
5048   dw_die_ref die_child;
5049   dw_die_ref die_sib;
5050   dw_die_ref die_definition; /* ref from a specification to its definition */
5051   dw_offset die_offset;
5052   unsigned long die_abbrev;
5053   int die_mark;
5054   /* Die is used and must not be pruned as unused.  */
5055   int die_perennial_p;
5056   unsigned int decl_id;
5057 }
5058 die_node;
5059
5060 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5061 #define FOR_EACH_CHILD(die, c, expr) do {       \
5062   c = die->die_child;                           \
5063   if (c) do {                                   \
5064     c = c->die_sib;                             \
5065     expr;                                       \
5066   } while (c != die->die_child);                \
5067 } while (0)
5068
5069 /* The pubname structure */
5070
5071 typedef struct GTY(()) pubname_struct {
5072   dw_die_ref die;
5073   const char *name;
5074 }
5075 pubname_entry;
5076
5077 DEF_VEC_O(pubname_entry);
5078 DEF_VEC_ALLOC_O(pubname_entry, gc);
5079
5080 struct GTY(()) dw_ranges_struct {
5081   /* If this is positive, it's a block number, otherwise it's a
5082      bitwise-negated index into dw_ranges_by_label.  */
5083   int num;
5084 };
5085
5086 struct GTY(()) dw_ranges_by_label_struct {
5087   const char *begin;
5088   const char *end;
5089 };
5090
5091 /* The limbo die list structure.  */
5092 typedef struct GTY(()) limbo_die_struct {
5093   dw_die_ref die;
5094   tree created_for;
5095   struct limbo_die_struct *next;
5096 }
5097 limbo_die_node;
5098
5099 /* How to start an assembler comment.  */
5100 #ifndef ASM_COMMENT_START
5101 #define ASM_COMMENT_START ";#"
5102 #endif
5103
5104 /* Define a macro which returns nonzero for a TYPE_DECL which was
5105    implicitly generated for a tagged type.
5106
5107    Note that unlike the gcc front end (which generates a NULL named
5108    TYPE_DECL node for each complete tagged type, each array type, and
5109    each function type node created) the g++ front end generates a
5110    _named_ TYPE_DECL node for each tagged type node created.
5111    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5112    generate a DW_TAG_typedef DIE for them.  */
5113
5114 #define TYPE_DECL_IS_STUB(decl)                         \
5115   (DECL_NAME (decl) == NULL_TREE                        \
5116    || (DECL_ARTIFICIAL (decl)                           \
5117        && is_tagged_type (TREE_TYPE (decl))             \
5118        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5119            /* This is necessary for stub decls that     \
5120               appear in nested inline functions.  */    \
5121            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5122                && (decl_ultimate_origin (decl)          \
5123                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5124
5125 /* Information concerning the compilation unit's programming
5126    language, and compiler version.  */
5127
5128 /* Fixed size portion of the DWARF compilation unit header.  */
5129 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5130   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5131
5132 /* Fixed size portion of public names info.  */
5133 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5134
5135 /* Fixed size portion of the address range info.  */
5136 #define DWARF_ARANGES_HEADER_SIZE                                       \
5137   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5138                 DWARF2_ADDR_SIZE * 2)                                   \
5139    - DWARF_INITIAL_LENGTH_SIZE)
5140
5141 /* Size of padding portion in the address range info.  It must be
5142    aligned to twice the pointer size.  */
5143 #define DWARF_ARANGES_PAD_SIZE \
5144   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5145                 DWARF2_ADDR_SIZE * 2)                              \
5146    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5147
5148 /* Use assembler line directives if available.  */
5149 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5150 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5151 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5152 #else
5153 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5154 #endif
5155 #endif
5156
5157 /* Minimum line offset in a special line info. opcode.
5158    This value was chosen to give a reasonable range of values.  */
5159 #define DWARF_LINE_BASE  -10
5160
5161 /* First special line opcode - leave room for the standard opcodes.  */
5162 #define DWARF_LINE_OPCODE_BASE  10
5163
5164 /* Range of line offsets in a special line info. opcode.  */
5165 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5166
5167 /* Flag that indicates the initial value of the is_stmt_start flag.
5168    In the present implementation, we do not mark any lines as
5169    the beginning of a source statement, because that information
5170    is not made available by the GCC front-end.  */
5171 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5172
5173 #ifdef DWARF2_DEBUGGING_INFO
5174 /* This location is used by calc_die_sizes() to keep track
5175    the offset of each DIE within the .debug_info section.  */
5176 static unsigned long next_die_offset;
5177 #endif
5178
5179 /* Record the root of the DIE's built for the current compilation unit.  */
5180 static GTY(()) dw_die_ref comp_unit_die;
5181
5182 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5183 static GTY(()) limbo_die_node *limbo_die_list;
5184
5185 /* A list of DIEs for which we may have to generate
5186    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5187    set.  */
5188 static GTY(()) limbo_die_node *deferred_asm_name;
5189
5190 /* Filenames referenced by this compilation unit.  */
5191 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5192
5193 /* A hash table of references to DIE's that describe declarations.
5194    The key is a DECL_UID() which is a unique number identifying each decl.  */
5195 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5196
5197 /* A hash table of references to DIE's that describe COMMON blocks.
5198    The key is DECL_UID() ^ die_parent.  */
5199 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5200
5201 /* Node of the variable location list.  */
5202 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5203   rtx GTY (()) var_loc_note;
5204   const char * GTY (()) label;
5205   const char * GTY (()) section_label;
5206   struct var_loc_node * GTY (()) next;
5207 };
5208
5209 /* Variable location list.  */
5210 struct GTY (()) var_loc_list_def {
5211   struct var_loc_node * GTY (()) first;
5212
5213   /* Do not mark the last element of the chained list because
5214      it is marked through the chain.  */
5215   struct var_loc_node * GTY ((skip ("%h"))) last;
5216
5217   /* DECL_UID of the variable decl.  */
5218   unsigned int decl_id;
5219 };
5220 typedef struct var_loc_list_def var_loc_list;
5221
5222
5223 /* Table of decl location linked lists.  */
5224 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5225
5226 /* A pointer to the base of a list of references to DIE's that
5227    are uniquely identified by their tag, presence/absence of
5228    children DIE's, and list of attribute/value pairs.  */
5229 static GTY((length ("abbrev_die_table_allocated")))
5230   dw_die_ref *abbrev_die_table;
5231
5232 /* Number of elements currently allocated for abbrev_die_table.  */
5233 static GTY(()) unsigned abbrev_die_table_allocated;
5234
5235 /* Number of elements in type_die_table currently in use.  */
5236 static GTY(()) unsigned abbrev_die_table_in_use;
5237
5238 /* Size (in elements) of increments by which we may expand the
5239    abbrev_die_table.  */
5240 #define ABBREV_DIE_TABLE_INCREMENT 256
5241
5242 /* A pointer to the base of a table that contains line information
5243    for each source code line in .text in the compilation unit.  */
5244 static GTY((length ("line_info_table_allocated")))
5245      dw_line_info_ref line_info_table;
5246
5247 /* Number of elements currently allocated for line_info_table.  */
5248 static GTY(()) unsigned line_info_table_allocated;
5249
5250 /* Number of elements in line_info_table currently in use.  */
5251 static GTY(()) unsigned line_info_table_in_use;
5252
5253 /* A pointer to the base of a table that contains line information
5254    for each source code line outside of .text in the compilation unit.  */
5255 static GTY ((length ("separate_line_info_table_allocated")))
5256      dw_separate_line_info_ref separate_line_info_table;
5257
5258 /* Number of elements currently allocated for separate_line_info_table.  */
5259 static GTY(()) unsigned separate_line_info_table_allocated;
5260
5261 /* Number of elements in separate_line_info_table currently in use.  */
5262 static GTY(()) unsigned separate_line_info_table_in_use;
5263
5264 /* Size (in elements) of increments by which we may expand the
5265    line_info_table.  */
5266 #define LINE_INFO_TABLE_INCREMENT 1024
5267
5268 /* A pointer to the base of a table that contains a list of publicly
5269    accessible names.  */
5270 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5271
5272 /* A pointer to the base of a table that contains a list of publicly
5273    accessible types.  */
5274 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5275
5276 /* Array of dies for which we should generate .debug_arange info.  */
5277 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5278
5279 /* Number of elements currently allocated for arange_table.  */
5280 static GTY(()) unsigned arange_table_allocated;
5281
5282 /* Number of elements in arange_table currently in use.  */
5283 static GTY(()) unsigned arange_table_in_use;
5284
5285 /* Size (in elements) of increments by which we may expand the
5286    arange_table.  */
5287 #define ARANGE_TABLE_INCREMENT 64
5288
5289 /* Array of dies for which we should generate .debug_ranges info.  */
5290 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5291
5292 /* Number of elements currently allocated for ranges_table.  */
5293 static GTY(()) unsigned ranges_table_allocated;
5294
5295 /* Number of elements in ranges_table currently in use.  */
5296 static GTY(()) unsigned ranges_table_in_use;
5297
5298 /* Array of pairs of labels referenced in ranges_table.  */
5299 static GTY ((length ("ranges_by_label_allocated")))
5300      dw_ranges_by_label_ref ranges_by_label;
5301
5302 /* Number of elements currently allocated for ranges_by_label.  */
5303 static GTY(()) unsigned ranges_by_label_allocated;
5304
5305 /* Number of elements in ranges_by_label currently in use.  */
5306 static GTY(()) unsigned ranges_by_label_in_use;
5307
5308 /* Size (in elements) of increments by which we may expand the
5309    ranges_table.  */
5310 #define RANGES_TABLE_INCREMENT 64
5311
5312 /* Whether we have location lists that need outputting */
5313 static GTY(()) bool have_location_lists;
5314
5315 /* Unique label counter.  */
5316 static GTY(()) unsigned int loclabel_num;
5317
5318 #ifdef DWARF2_DEBUGGING_INFO
5319 /* Record whether the function being analyzed contains inlined functions.  */
5320 static int current_function_has_inlines;
5321 #endif
5322 #if 0 && defined (MIPS_DEBUGGING_INFO)
5323 static int comp_unit_has_inlines;
5324 #endif
5325
5326 /* The last file entry emitted by maybe_emit_file().  */
5327 static GTY(()) struct dwarf_file_data * last_emitted_file;
5328
5329 /* Number of internal labels generated by gen_internal_sym().  */
5330 static GTY(()) int label_num;
5331
5332 /* Cached result of previous call to lookup_filename.  */
5333 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5334
5335 #ifdef DWARF2_DEBUGGING_INFO
5336
5337 /* Offset from the "steady-state frame pointer" to the frame base,
5338    within the current function.  */
5339 static HOST_WIDE_INT frame_pointer_fb_offset;
5340
5341 /* Forward declarations for functions defined in this file.  */
5342
5343 static int is_pseudo_reg (const_rtx);
5344 static tree type_main_variant (tree);
5345 static int is_tagged_type (const_tree);
5346 static const char *dwarf_tag_name (unsigned);
5347 static const char *dwarf_attr_name (unsigned);
5348 static const char *dwarf_form_name (unsigned);
5349 static tree decl_ultimate_origin (const_tree);
5350 static tree decl_class_context (tree);
5351 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5352 static inline enum dw_val_class AT_class (dw_attr_ref);
5353 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5354 static inline unsigned AT_flag (dw_attr_ref);
5355 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5356 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5357 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5358 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5359 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5360                               unsigned long);
5361 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5362                                unsigned int, unsigned char *);
5363 static hashval_t debug_str_do_hash (const void *);
5364 static int debug_str_eq (const void *, const void *);
5365 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5366 static inline const char *AT_string (dw_attr_ref);
5367 static enum dwarf_form AT_string_form (dw_attr_ref);
5368 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5369 static void add_AT_specification (dw_die_ref, dw_die_ref);
5370 static inline dw_die_ref AT_ref (dw_attr_ref);
5371 static inline int AT_ref_external (dw_attr_ref);
5372 static inline void set_AT_ref_external (dw_attr_ref, int);
5373 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5374 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5375 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5376 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5377                              dw_loc_list_ref);
5378 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5379 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5380 static inline rtx AT_addr (dw_attr_ref);
5381 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5382 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5383 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5384 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5385                            unsigned HOST_WIDE_INT);
5386 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5387                                unsigned long);
5388 static inline const char *AT_lbl (dw_attr_ref);
5389 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5390 static const char *get_AT_low_pc (dw_die_ref);
5391 static const char *get_AT_hi_pc (dw_die_ref);
5392 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5393 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5394 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5395 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5396 static bool is_c_family (void);
5397 static bool is_cxx (void);
5398 static bool is_java (void);
5399 static bool is_fortran (void);
5400 static bool is_ada (void);
5401 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5402 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5403 static void add_child_die (dw_die_ref, dw_die_ref);
5404 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5405 static dw_die_ref lookup_type_die (tree);
5406 static void equate_type_number_to_die (tree, dw_die_ref);
5407 static hashval_t decl_die_table_hash (const void *);
5408 static int decl_die_table_eq (const void *, const void *);
5409 static dw_die_ref lookup_decl_die (tree);
5410 static hashval_t common_block_die_table_hash (const void *);
5411 static int common_block_die_table_eq (const void *, const void *);
5412 static hashval_t decl_loc_table_hash (const void *);
5413 static int decl_loc_table_eq (const void *, const void *);
5414 static var_loc_list *lookup_decl_loc (const_tree);
5415 static void equate_decl_number_to_die (tree, dw_die_ref);
5416 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5417 static void print_spaces (FILE *);
5418 static void print_die (dw_die_ref, FILE *);
5419 static void print_dwarf_line_table (FILE *);
5420 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5421 static dw_die_ref pop_compile_unit (dw_die_ref);
5422 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5423 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5424 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5425 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5426 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5427 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5428 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5429 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5430 static void compute_section_prefix (dw_die_ref);
5431 static int is_type_die (dw_die_ref);
5432 static int is_comdat_die (dw_die_ref);
5433 static int is_symbol_die (dw_die_ref);
5434 static void assign_symbol_names (dw_die_ref);
5435 static void break_out_includes (dw_die_ref);
5436 static hashval_t htab_cu_hash (const void *);
5437 static int htab_cu_eq (const void *, const void *);
5438 static void htab_cu_del (void *);
5439 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5440 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5441 static void add_sibling_attributes (dw_die_ref);
5442 static void build_abbrev_table (dw_die_ref);
5443 static void output_location_lists (dw_die_ref);
5444 static int constant_size (unsigned HOST_WIDE_INT);
5445 static unsigned long size_of_die (dw_die_ref);
5446 static void calc_die_sizes (dw_die_ref);
5447 static void mark_dies (dw_die_ref);
5448 static void unmark_dies (dw_die_ref);
5449 static void unmark_all_dies (dw_die_ref);
5450 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5451 static unsigned long size_of_aranges (void);
5452 static enum dwarf_form value_format (dw_attr_ref);
5453 static void output_value_format (dw_attr_ref);
5454 static void output_abbrev_section (void);
5455 static void output_die_symbol (dw_die_ref);
5456 static void output_die (dw_die_ref);
5457 static void output_compilation_unit_header (void);
5458 static void output_comp_unit (dw_die_ref, int);
5459 static const char *dwarf2_name (tree, int);
5460 static void add_pubname (tree, dw_die_ref);
5461 static void add_pubname_string (const char *, dw_die_ref);
5462 static void add_pubtype (tree, dw_die_ref);
5463 static void output_pubnames (VEC (pubname_entry,gc) *);
5464 static void add_arange (tree, dw_die_ref);
5465 static void output_aranges (void);
5466 static unsigned int add_ranges_num (int);
5467 static unsigned int add_ranges (const_tree);
5468 static unsigned int add_ranges_by_labels (const char *, const char *);
5469 static void output_ranges (void);
5470 static void output_line_info (void);
5471 static void output_file_names (void);
5472 static dw_die_ref base_type_die (tree);
5473 static int is_base_type (tree);
5474 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5475 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5476 static int type_is_enum (const_tree);
5477 static unsigned int dbx_reg_number (const_rtx);
5478 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5479 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5480 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5481                                                 enum var_init_status);
5482 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5483                                                      enum var_init_status);
5484 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5485                                          enum var_init_status);
5486 static int is_based_loc (const_rtx);
5487 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5488                                             enum var_init_status);
5489 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5490                                                enum var_init_status);
5491 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5492 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5493 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5494 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5495 static tree field_type (const_tree);
5496 static unsigned int simple_type_align_in_bits (const_tree);
5497 static unsigned int simple_decl_align_in_bits (const_tree);
5498 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5499 static HOST_WIDE_INT field_byte_offset (const_tree);
5500 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5501                                          dw_loc_descr_ref);
5502 static void add_data_member_location_attribute (dw_die_ref, tree);
5503 static void add_const_value_attribute (dw_die_ref, rtx);
5504 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5505 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5506 static void insert_float (const_rtx, unsigned char *);
5507 static rtx rtl_for_decl_location (tree);
5508 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5509                                                    enum dwarf_attribute);
5510 static void tree_add_const_value_attribute (dw_die_ref, tree);
5511 static void add_name_attribute (dw_die_ref, const char *);
5512 static void add_comp_dir_attribute (dw_die_ref);
5513 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5514 static void add_subscript_info (dw_die_ref, tree, bool);
5515 static void add_byte_size_attribute (dw_die_ref, tree);
5516 static void add_bit_offset_attribute (dw_die_ref, tree);
5517 static void add_bit_size_attribute (dw_die_ref, tree);
5518 static void add_prototyped_attribute (dw_die_ref, tree);
5519 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5520 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5521 static void add_src_coords_attributes (dw_die_ref, tree);
5522 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5523 static void push_decl_scope (tree);
5524 static void pop_decl_scope (void);
5525 static dw_die_ref scope_die_for (tree, dw_die_ref);
5526 static inline int local_scope_p (dw_die_ref);
5527 static inline int class_scope_p (dw_die_ref);
5528 static inline int class_or_namespace_scope_p (dw_die_ref);
5529 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5530 static void add_calling_convention_attribute (dw_die_ref, tree);
5531 static const char *type_tag (const_tree);
5532 static tree member_declared_type (const_tree);
5533 #if 0
5534 static const char *decl_start_label (tree);
5535 #endif
5536 static void gen_array_type_die (tree, dw_die_ref);
5537 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5538 #if 0
5539 static void gen_entry_point_die (tree, dw_die_ref);
5540 #endif
5541 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5542 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5543 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5544 static void gen_formal_types_die (tree, dw_die_ref);
5545 static void gen_subprogram_die (tree, dw_die_ref);
5546 static void gen_variable_die (tree, tree, dw_die_ref);
5547 static void gen_const_die (tree, dw_die_ref);
5548 static void gen_label_die (tree, dw_die_ref);
5549 static void gen_lexical_block_die (tree, dw_die_ref, int);
5550 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5551 static void gen_field_die (tree, dw_die_ref);
5552 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5553 static dw_die_ref gen_compile_unit_die (const char *);
5554 static void gen_inheritance_die (tree, tree, dw_die_ref);
5555 static void gen_member_die (tree, dw_die_ref);
5556 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5557                                                 enum debug_info_usage);
5558 static void gen_subroutine_type_die (tree, dw_die_ref);
5559 static void gen_typedef_die (tree, dw_die_ref);
5560 static void gen_type_die (tree, dw_die_ref);
5561 static void gen_block_die (tree, dw_die_ref, int);
5562 static void decls_for_scope (tree, dw_die_ref, int);
5563 static int is_redundant_typedef (const_tree);
5564 static void gen_namespace_die (tree, dw_die_ref);
5565 static void gen_decl_die (tree, tree, dw_die_ref);
5566 static dw_die_ref force_decl_die (tree);
5567 static dw_die_ref force_type_die (tree);
5568 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5569 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5570 static struct dwarf_file_data * lookup_filename (const char *);
5571 static void retry_incomplete_types (void);
5572 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5573 static void splice_child_die (dw_die_ref, dw_die_ref);
5574 static int file_info_cmp (const void *, const void *);
5575 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5576                                      const char *, const char *, unsigned);
5577 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5578                                        const char *, const char *,
5579                                        const char *);
5580 static void output_loc_list (dw_loc_list_ref);
5581 static char *gen_internal_sym (const char *);
5582
5583 static void prune_unmark_dies (dw_die_ref);
5584 static void prune_unused_types_mark (dw_die_ref, int);
5585 static void prune_unused_types_walk (dw_die_ref);
5586 static void prune_unused_types_walk_attribs (dw_die_ref);
5587 static void prune_unused_types_prune (dw_die_ref);
5588 static void prune_unused_types (void);
5589 static int maybe_emit_file (struct dwarf_file_data *fd);
5590
5591 /* Section names used to hold DWARF debugging information.  */
5592 #ifndef DEBUG_INFO_SECTION
5593 #define DEBUG_INFO_SECTION      ".debug_info"
5594 #endif
5595 #ifndef DEBUG_ABBREV_SECTION
5596 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5597 #endif
5598 #ifndef DEBUG_ARANGES_SECTION
5599 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5600 #endif
5601 #ifndef DEBUG_MACINFO_SECTION
5602 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5603 #endif
5604 #ifndef DEBUG_LINE_SECTION
5605 #define DEBUG_LINE_SECTION      ".debug_line"
5606 #endif
5607 #ifndef DEBUG_LOC_SECTION
5608 #define DEBUG_LOC_SECTION       ".debug_loc"
5609 #endif
5610 #ifndef DEBUG_PUBNAMES_SECTION
5611 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5612 #endif
5613 #ifndef DEBUG_STR_SECTION
5614 #define DEBUG_STR_SECTION       ".debug_str"
5615 #endif
5616 #ifndef DEBUG_RANGES_SECTION
5617 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5618 #endif
5619
5620 /* Standard ELF section names for compiled code and data.  */
5621 #ifndef TEXT_SECTION_NAME
5622 #define TEXT_SECTION_NAME       ".text"
5623 #endif
5624
5625 /* Section flags for .debug_str section.  */
5626 #define DEBUG_STR_SECTION_FLAGS \
5627   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5628    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5629    : SECTION_DEBUG)
5630
5631 /* Labels we insert at beginning sections we can reference instead of
5632    the section names themselves.  */
5633
5634 #ifndef TEXT_SECTION_LABEL
5635 #define TEXT_SECTION_LABEL              "Ltext"
5636 #endif
5637 #ifndef COLD_TEXT_SECTION_LABEL
5638 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5639 #endif
5640 #ifndef DEBUG_LINE_SECTION_LABEL
5641 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5642 #endif
5643 #ifndef DEBUG_INFO_SECTION_LABEL
5644 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5645 #endif
5646 #ifndef DEBUG_ABBREV_SECTION_LABEL
5647 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5648 #endif
5649 #ifndef DEBUG_LOC_SECTION_LABEL
5650 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5651 #endif
5652 #ifndef DEBUG_RANGES_SECTION_LABEL
5653 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5654 #endif
5655 #ifndef DEBUG_MACINFO_SECTION_LABEL
5656 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5657 #endif
5658
5659 /* Definitions of defaults for formats and names of various special
5660    (artificial) labels which may be generated within this file (when the -g
5661    options is used and DWARF2_DEBUGGING_INFO is in effect.
5662    If necessary, these may be overridden from within the tm.h file, but
5663    typically, overriding these defaults is unnecessary.  */
5664
5665 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5666 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5667 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5668 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5669 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5670 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5671 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5672 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5673 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5674 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5675
5676 #ifndef TEXT_END_LABEL
5677 #define TEXT_END_LABEL          "Letext"
5678 #endif
5679 #ifndef COLD_END_LABEL
5680 #define COLD_END_LABEL          "Letext_cold"
5681 #endif
5682 #ifndef BLOCK_BEGIN_LABEL
5683 #define BLOCK_BEGIN_LABEL       "LBB"
5684 #endif
5685 #ifndef BLOCK_END_LABEL
5686 #define BLOCK_END_LABEL         "LBE"
5687 #endif
5688 #ifndef LINE_CODE_LABEL
5689 #define LINE_CODE_LABEL         "LM"
5690 #endif
5691 #ifndef SEPARATE_LINE_CODE_LABEL
5692 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5693 #endif
5694
5695 \f
5696 /* We allow a language front-end to designate a function that is to be
5697    called to "demangle" any name before it is put into a DIE.  */
5698
5699 static const char *(*demangle_name_func) (const char *);
5700
5701 void
5702 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5703 {
5704   demangle_name_func = func;
5705 }
5706
5707 /* Test if rtl node points to a pseudo register.  */
5708
5709 static inline int
5710 is_pseudo_reg (const_rtx rtl)
5711 {
5712   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5713           || (GET_CODE (rtl) == SUBREG
5714               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5715 }
5716
5717 /* Return a reference to a type, with its const and volatile qualifiers
5718    removed.  */
5719
5720 static inline tree
5721 type_main_variant (tree type)
5722 {
5723   type = TYPE_MAIN_VARIANT (type);
5724
5725   /* ??? There really should be only one main variant among any group of
5726      variants of a given type (and all of the MAIN_VARIANT values for all
5727      members of the group should point to that one type) but sometimes the C
5728      front-end messes this up for array types, so we work around that bug
5729      here.  */
5730   if (TREE_CODE (type) == ARRAY_TYPE)
5731     while (type != TYPE_MAIN_VARIANT (type))
5732       type = TYPE_MAIN_VARIANT (type);
5733
5734   return type;
5735 }
5736
5737 /* Return nonzero if the given type node represents a tagged type.  */
5738
5739 static inline int
5740 is_tagged_type (const_tree type)
5741 {
5742   enum tree_code code = TREE_CODE (type);
5743
5744   return (code == RECORD_TYPE || code == UNION_TYPE
5745           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5746 }
5747
5748 /* Convert a DIE tag into its string name.  */
5749
5750 static const char *
5751 dwarf_tag_name (unsigned int tag)
5752 {
5753   switch (tag)
5754     {
5755     case DW_TAG_padding:
5756       return "DW_TAG_padding";
5757     case DW_TAG_array_type:
5758       return "DW_TAG_array_type";
5759     case DW_TAG_class_type:
5760       return "DW_TAG_class_type";
5761     case DW_TAG_entry_point:
5762       return "DW_TAG_entry_point";
5763     case DW_TAG_enumeration_type:
5764       return "DW_TAG_enumeration_type";
5765     case DW_TAG_formal_parameter:
5766       return "DW_TAG_formal_parameter";
5767     case DW_TAG_imported_declaration:
5768       return "DW_TAG_imported_declaration";
5769     case DW_TAG_label:
5770       return "DW_TAG_label";
5771     case DW_TAG_lexical_block:
5772       return "DW_TAG_lexical_block";
5773     case DW_TAG_member:
5774       return "DW_TAG_member";
5775     case DW_TAG_pointer_type:
5776       return "DW_TAG_pointer_type";
5777     case DW_TAG_reference_type:
5778       return "DW_TAG_reference_type";
5779     case DW_TAG_compile_unit:
5780       return "DW_TAG_compile_unit";
5781     case DW_TAG_string_type:
5782       return "DW_TAG_string_type";
5783     case DW_TAG_structure_type:
5784       return "DW_TAG_structure_type";
5785     case DW_TAG_subroutine_type:
5786       return "DW_TAG_subroutine_type";
5787     case DW_TAG_typedef:
5788       return "DW_TAG_typedef";
5789     case DW_TAG_union_type:
5790       return "DW_TAG_union_type";
5791     case DW_TAG_unspecified_parameters:
5792       return "DW_TAG_unspecified_parameters";
5793     case DW_TAG_variant:
5794       return "DW_TAG_variant";
5795     case DW_TAG_common_block:
5796       return "DW_TAG_common_block";
5797     case DW_TAG_common_inclusion:
5798       return "DW_TAG_common_inclusion";
5799     case DW_TAG_inheritance:
5800       return "DW_TAG_inheritance";
5801     case DW_TAG_inlined_subroutine:
5802       return "DW_TAG_inlined_subroutine";
5803     case DW_TAG_module:
5804       return "DW_TAG_module";
5805     case DW_TAG_ptr_to_member_type:
5806       return "DW_TAG_ptr_to_member_type";
5807     case DW_TAG_set_type:
5808       return "DW_TAG_set_type";
5809     case DW_TAG_subrange_type:
5810       return "DW_TAG_subrange_type";
5811     case DW_TAG_with_stmt:
5812       return "DW_TAG_with_stmt";
5813     case DW_TAG_access_declaration:
5814       return "DW_TAG_access_declaration";
5815     case DW_TAG_base_type:
5816       return "DW_TAG_base_type";
5817     case DW_TAG_catch_block:
5818       return "DW_TAG_catch_block";
5819     case DW_TAG_const_type:
5820       return "DW_TAG_const_type";
5821     case DW_TAG_constant:
5822       return "DW_TAG_constant";
5823     case DW_TAG_enumerator:
5824       return "DW_TAG_enumerator";
5825     case DW_TAG_file_type:
5826       return "DW_TAG_file_type";
5827     case DW_TAG_friend:
5828       return "DW_TAG_friend";
5829     case DW_TAG_namelist:
5830       return "DW_TAG_namelist";
5831     case DW_TAG_namelist_item:
5832       return "DW_TAG_namelist_item";
5833     case DW_TAG_packed_type:
5834       return "DW_TAG_packed_type";
5835     case DW_TAG_subprogram:
5836       return "DW_TAG_subprogram";
5837     case DW_TAG_template_type_param:
5838       return "DW_TAG_template_type_param";
5839     case DW_TAG_template_value_param:
5840       return "DW_TAG_template_value_param";
5841     case DW_TAG_thrown_type:
5842       return "DW_TAG_thrown_type";
5843     case DW_TAG_try_block:
5844       return "DW_TAG_try_block";
5845     case DW_TAG_variant_part:
5846       return "DW_TAG_variant_part";
5847     case DW_TAG_variable:
5848       return "DW_TAG_variable";
5849     case DW_TAG_volatile_type:
5850       return "DW_TAG_volatile_type";
5851     case DW_TAG_dwarf_procedure:
5852       return "DW_TAG_dwarf_procedure";
5853     case DW_TAG_restrict_type:
5854       return "DW_TAG_restrict_type";
5855     case DW_TAG_interface_type:
5856       return "DW_TAG_interface_type";
5857     case DW_TAG_namespace:
5858       return "DW_TAG_namespace";
5859     case DW_TAG_imported_module:
5860       return "DW_TAG_imported_module";
5861     case DW_TAG_unspecified_type:
5862       return "DW_TAG_unspecified_type";
5863     case DW_TAG_partial_unit:
5864       return "DW_TAG_partial_unit";
5865     case DW_TAG_imported_unit:
5866       return "DW_TAG_imported_unit";
5867     case DW_TAG_condition:
5868       return "DW_TAG_condition";
5869     case DW_TAG_shared_type:
5870       return "DW_TAG_shared_type";
5871     case DW_TAG_MIPS_loop:
5872       return "DW_TAG_MIPS_loop";
5873     case DW_TAG_format_label:
5874       return "DW_TAG_format_label";
5875     case DW_TAG_function_template:
5876       return "DW_TAG_function_template";
5877     case DW_TAG_class_template:
5878       return "DW_TAG_class_template";
5879     case DW_TAG_GNU_BINCL:
5880       return "DW_TAG_GNU_BINCL";
5881     case DW_TAG_GNU_EINCL:
5882       return "DW_TAG_GNU_EINCL";
5883     default:
5884       return "DW_TAG_<unknown>";
5885     }
5886 }
5887
5888 /* Convert a DWARF attribute code into its string name.  */
5889
5890 static const char *
5891 dwarf_attr_name (unsigned int attr)
5892 {
5893   switch (attr)
5894     {
5895     case DW_AT_sibling:
5896       return "DW_AT_sibling";
5897     case DW_AT_location:
5898       return "DW_AT_location";
5899     case DW_AT_name:
5900       return "DW_AT_name";
5901     case DW_AT_ordering:
5902       return "DW_AT_ordering";
5903     case DW_AT_subscr_data:
5904       return "DW_AT_subscr_data";
5905     case DW_AT_byte_size:
5906       return "DW_AT_byte_size";
5907     case DW_AT_bit_offset:
5908       return "DW_AT_bit_offset";
5909     case DW_AT_bit_size:
5910       return "DW_AT_bit_size";
5911     case DW_AT_element_list:
5912       return "DW_AT_element_list";
5913     case DW_AT_stmt_list:
5914       return "DW_AT_stmt_list";
5915     case DW_AT_low_pc:
5916       return "DW_AT_low_pc";
5917     case DW_AT_high_pc:
5918       return "DW_AT_high_pc";
5919     case DW_AT_language:
5920       return "DW_AT_language";
5921     case DW_AT_member:
5922       return "DW_AT_member";
5923     case DW_AT_discr:
5924       return "DW_AT_discr";
5925     case DW_AT_discr_value:
5926       return "DW_AT_discr_value";
5927     case DW_AT_visibility:
5928       return "DW_AT_visibility";
5929     case DW_AT_import:
5930       return "DW_AT_import";
5931     case DW_AT_string_length:
5932       return "DW_AT_string_length";
5933     case DW_AT_common_reference:
5934       return "DW_AT_common_reference";
5935     case DW_AT_comp_dir:
5936       return "DW_AT_comp_dir";
5937     case DW_AT_const_value:
5938       return "DW_AT_const_value";
5939     case DW_AT_containing_type:
5940       return "DW_AT_containing_type";
5941     case DW_AT_default_value:
5942       return "DW_AT_default_value";
5943     case DW_AT_inline:
5944       return "DW_AT_inline";
5945     case DW_AT_is_optional:
5946       return "DW_AT_is_optional";
5947     case DW_AT_lower_bound:
5948       return "DW_AT_lower_bound";
5949     case DW_AT_producer:
5950       return "DW_AT_producer";
5951     case DW_AT_prototyped:
5952       return "DW_AT_prototyped";
5953     case DW_AT_return_addr:
5954       return "DW_AT_return_addr";
5955     case DW_AT_start_scope:
5956       return "DW_AT_start_scope";
5957     case DW_AT_bit_stride:
5958       return "DW_AT_bit_stride";
5959     case DW_AT_upper_bound:
5960       return "DW_AT_upper_bound";
5961     case DW_AT_abstract_origin:
5962       return "DW_AT_abstract_origin";
5963     case DW_AT_accessibility:
5964       return "DW_AT_accessibility";
5965     case DW_AT_address_class:
5966       return "DW_AT_address_class";
5967     case DW_AT_artificial:
5968       return "DW_AT_artificial";
5969     case DW_AT_base_types:
5970       return "DW_AT_base_types";
5971     case DW_AT_calling_convention:
5972       return "DW_AT_calling_convention";
5973     case DW_AT_count:
5974       return "DW_AT_count";
5975     case DW_AT_data_member_location:
5976       return "DW_AT_data_member_location";
5977     case DW_AT_decl_column:
5978       return "DW_AT_decl_column";
5979     case DW_AT_decl_file:
5980       return "DW_AT_decl_file";
5981     case DW_AT_decl_line:
5982       return "DW_AT_decl_line";
5983     case DW_AT_declaration:
5984       return "DW_AT_declaration";
5985     case DW_AT_discr_list:
5986       return "DW_AT_discr_list";
5987     case DW_AT_encoding:
5988       return "DW_AT_encoding";
5989     case DW_AT_external:
5990       return "DW_AT_external";
5991     case DW_AT_explicit:
5992       return "DW_AT_explicit";
5993     case DW_AT_frame_base:
5994       return "DW_AT_frame_base";
5995     case DW_AT_friend:
5996       return "DW_AT_friend";
5997     case DW_AT_identifier_case:
5998       return "DW_AT_identifier_case";
5999     case DW_AT_macro_info:
6000       return "DW_AT_macro_info";
6001     case DW_AT_namelist_items:
6002       return "DW_AT_namelist_items";
6003     case DW_AT_priority:
6004       return "DW_AT_priority";
6005     case DW_AT_segment:
6006       return "DW_AT_segment";
6007     case DW_AT_specification:
6008       return "DW_AT_specification";
6009     case DW_AT_static_link:
6010       return "DW_AT_static_link";
6011     case DW_AT_type:
6012       return "DW_AT_type";
6013     case DW_AT_use_location:
6014       return "DW_AT_use_location";
6015     case DW_AT_variable_parameter:
6016       return "DW_AT_variable_parameter";
6017     case DW_AT_virtuality:
6018       return "DW_AT_virtuality";
6019     case DW_AT_vtable_elem_location:
6020       return "DW_AT_vtable_elem_location";
6021
6022     case DW_AT_allocated:
6023       return "DW_AT_allocated";
6024     case DW_AT_associated:
6025       return "DW_AT_associated";
6026     case DW_AT_data_location:
6027       return "DW_AT_data_location";
6028     case DW_AT_byte_stride:
6029       return "DW_AT_byte_stride";
6030     case DW_AT_entry_pc:
6031       return "DW_AT_entry_pc";
6032     case DW_AT_use_UTF8:
6033       return "DW_AT_use_UTF8";
6034     case DW_AT_extension:
6035       return "DW_AT_extension";
6036     case DW_AT_ranges:
6037       return "DW_AT_ranges";
6038     case DW_AT_trampoline:
6039       return "DW_AT_trampoline";
6040     case DW_AT_call_column:
6041       return "DW_AT_call_column";
6042     case DW_AT_call_file:
6043       return "DW_AT_call_file";
6044     case DW_AT_call_line:
6045       return "DW_AT_call_line";
6046
6047     case DW_AT_MIPS_fde:
6048       return "DW_AT_MIPS_fde";
6049     case DW_AT_MIPS_loop_begin:
6050       return "DW_AT_MIPS_loop_begin";
6051     case DW_AT_MIPS_tail_loop_begin:
6052       return "DW_AT_MIPS_tail_loop_begin";
6053     case DW_AT_MIPS_epilog_begin:
6054       return "DW_AT_MIPS_epilog_begin";
6055     case DW_AT_MIPS_loop_unroll_factor:
6056       return "DW_AT_MIPS_loop_unroll_factor";
6057     case DW_AT_MIPS_software_pipeline_depth:
6058       return "DW_AT_MIPS_software_pipeline_depth";
6059     case DW_AT_MIPS_linkage_name:
6060       return "DW_AT_MIPS_linkage_name";
6061     case DW_AT_MIPS_stride:
6062       return "DW_AT_MIPS_stride";
6063     case DW_AT_MIPS_abstract_name:
6064       return "DW_AT_MIPS_abstract_name";
6065     case DW_AT_MIPS_clone_origin:
6066       return "DW_AT_MIPS_clone_origin";
6067     case DW_AT_MIPS_has_inlines:
6068       return "DW_AT_MIPS_has_inlines";
6069
6070     case DW_AT_sf_names:
6071       return "DW_AT_sf_names";
6072     case DW_AT_src_info:
6073       return "DW_AT_src_info";
6074     case DW_AT_mac_info:
6075       return "DW_AT_mac_info";
6076     case DW_AT_src_coords:
6077       return "DW_AT_src_coords";
6078     case DW_AT_body_begin:
6079       return "DW_AT_body_begin";
6080     case DW_AT_body_end:
6081       return "DW_AT_body_end";
6082     case DW_AT_GNU_vector:
6083       return "DW_AT_GNU_vector";
6084
6085     case DW_AT_VMS_rtnbeg_pd_address:
6086       return "DW_AT_VMS_rtnbeg_pd_address";
6087
6088     default:
6089       return "DW_AT_<unknown>";
6090     }
6091 }
6092
6093 /* Convert a DWARF value form code into its string name.  */
6094
6095 static const char *
6096 dwarf_form_name (unsigned int form)
6097 {
6098   switch (form)
6099     {
6100     case DW_FORM_addr:
6101       return "DW_FORM_addr";
6102     case DW_FORM_block2:
6103       return "DW_FORM_block2";
6104     case DW_FORM_block4:
6105       return "DW_FORM_block4";
6106     case DW_FORM_data2:
6107       return "DW_FORM_data2";
6108     case DW_FORM_data4:
6109       return "DW_FORM_data4";
6110     case DW_FORM_data8:
6111       return "DW_FORM_data8";
6112     case DW_FORM_string:
6113       return "DW_FORM_string";
6114     case DW_FORM_block:
6115       return "DW_FORM_block";
6116     case DW_FORM_block1:
6117       return "DW_FORM_block1";
6118     case DW_FORM_data1:
6119       return "DW_FORM_data1";
6120     case DW_FORM_flag:
6121       return "DW_FORM_flag";
6122     case DW_FORM_sdata:
6123       return "DW_FORM_sdata";
6124     case DW_FORM_strp:
6125       return "DW_FORM_strp";
6126     case DW_FORM_udata:
6127       return "DW_FORM_udata";
6128     case DW_FORM_ref_addr:
6129       return "DW_FORM_ref_addr";
6130     case DW_FORM_ref1:
6131       return "DW_FORM_ref1";
6132     case DW_FORM_ref2:
6133       return "DW_FORM_ref2";
6134     case DW_FORM_ref4:
6135       return "DW_FORM_ref4";
6136     case DW_FORM_ref8:
6137       return "DW_FORM_ref8";
6138     case DW_FORM_ref_udata:
6139       return "DW_FORM_ref_udata";
6140     case DW_FORM_indirect:
6141       return "DW_FORM_indirect";
6142     default:
6143       return "DW_FORM_<unknown>";
6144     }
6145 }
6146 \f
6147 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6148    instance of an inlined instance of a decl which is local to an inline
6149    function, so we have to trace all of the way back through the origin chain
6150    to find out what sort of node actually served as the original seed for the
6151    given block.  */
6152
6153 static tree
6154 decl_ultimate_origin (const_tree decl)
6155 {
6156   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6157     return NULL_TREE;
6158
6159   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6160      nodes in the function to point to themselves; ignore that if
6161      we're trying to output the abstract instance of this function.  */
6162   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6163     return NULL_TREE;
6164
6165   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6166      most distant ancestor, this should never happen.  */
6167   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6168
6169   return DECL_ABSTRACT_ORIGIN (decl);
6170 }
6171
6172 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6173    of a virtual function may refer to a base class, so we check the 'this'
6174    parameter.  */
6175
6176 static tree
6177 decl_class_context (tree decl)
6178 {
6179   tree context = NULL_TREE;
6180
6181   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6182     context = DECL_CONTEXT (decl);
6183   else
6184     context = TYPE_MAIN_VARIANT
6185       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6186
6187   if (context && !TYPE_P (context))
6188     context = NULL_TREE;
6189
6190   return context;
6191 }
6192 \f
6193 /* Add an attribute/value pair to a DIE.  */
6194
6195 static inline void
6196 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6197 {
6198   /* Maybe this should be an assert?  */
6199   if (die == NULL)
6200     return;
6201
6202   if (die->die_attr == NULL)
6203     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6204   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6205 }
6206
6207 static inline enum dw_val_class
6208 AT_class (dw_attr_ref a)
6209 {
6210   return a->dw_attr_val.val_class;
6211 }
6212
6213 /* Add a flag value attribute to a DIE.  */
6214
6215 static inline void
6216 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6217 {
6218   dw_attr_node attr;
6219
6220   attr.dw_attr = attr_kind;
6221   attr.dw_attr_val.val_class = dw_val_class_flag;
6222   attr.dw_attr_val.v.val_flag = flag;
6223   add_dwarf_attr (die, &attr);
6224 }
6225
6226 static inline unsigned
6227 AT_flag (dw_attr_ref a)
6228 {
6229   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6230   return a->dw_attr_val.v.val_flag;
6231 }
6232
6233 /* Add a signed integer attribute value to a DIE.  */
6234
6235 static inline void
6236 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6237 {
6238   dw_attr_node attr;
6239
6240   attr.dw_attr = attr_kind;
6241   attr.dw_attr_val.val_class = dw_val_class_const;
6242   attr.dw_attr_val.v.val_int = int_val;
6243   add_dwarf_attr (die, &attr);
6244 }
6245
6246 static inline HOST_WIDE_INT
6247 AT_int (dw_attr_ref a)
6248 {
6249   gcc_assert (a && AT_class (a) == dw_val_class_const);
6250   return a->dw_attr_val.v.val_int;
6251 }
6252
6253 /* Add an unsigned integer attribute value to a DIE.  */
6254
6255 static inline void
6256 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6257                  unsigned HOST_WIDE_INT unsigned_val)
6258 {
6259   dw_attr_node attr;
6260
6261   attr.dw_attr = attr_kind;
6262   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6263   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6264   add_dwarf_attr (die, &attr);
6265 }
6266
6267 static inline unsigned HOST_WIDE_INT
6268 AT_unsigned (dw_attr_ref a)
6269 {
6270   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6271   return a->dw_attr_val.v.val_unsigned;
6272 }
6273
6274 /* Add an unsigned double integer attribute value to a DIE.  */
6275
6276 static inline void
6277 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6278                   long unsigned int val_hi, long unsigned int val_low)
6279 {
6280   dw_attr_node attr;
6281
6282   attr.dw_attr = attr_kind;
6283   attr.dw_attr_val.val_class = dw_val_class_long_long;
6284   attr.dw_attr_val.v.val_long_long.hi = val_hi;
6285   attr.dw_attr_val.v.val_long_long.low = val_low;
6286   add_dwarf_attr (die, &attr);
6287 }
6288
6289 /* Add a floating point attribute value to a DIE and return it.  */
6290
6291 static inline void
6292 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6293             unsigned int length, unsigned int elt_size, unsigned char *array)
6294 {
6295   dw_attr_node attr;
6296
6297   attr.dw_attr = attr_kind;
6298   attr.dw_attr_val.val_class = dw_val_class_vec;
6299   attr.dw_attr_val.v.val_vec.length = length;
6300   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6301   attr.dw_attr_val.v.val_vec.array = array;
6302   add_dwarf_attr (die, &attr);
6303 }
6304
6305 /* Hash and equality functions for debug_str_hash.  */
6306
6307 static hashval_t
6308 debug_str_do_hash (const void *x)
6309 {
6310   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6311 }
6312
6313 static int
6314 debug_str_eq (const void *x1, const void *x2)
6315 {
6316   return strcmp ((((const struct indirect_string_node *)x1)->str),
6317                  (const char *)x2) == 0;
6318 }
6319
6320 static struct indirect_string_node *
6321 find_AT_string (const char *str)
6322 {
6323   struct indirect_string_node *node;
6324   void **slot;
6325
6326   if (! debug_str_hash)
6327     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6328                                       debug_str_eq, NULL);
6329
6330   slot = htab_find_slot_with_hash (debug_str_hash, str,
6331                                    htab_hash_string (str), INSERT);
6332   if (*slot == NULL)
6333     {
6334       node = (struct indirect_string_node *)
6335                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6336       node->str = ggc_strdup (str);
6337       *slot = node;
6338     }
6339   else
6340     node = (struct indirect_string_node *) *slot;
6341
6342   node->refcount++;
6343   return node;
6344 }
6345
6346 /* Add a string attribute value to a DIE.  */
6347
6348 static inline void
6349 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6350 {
6351   dw_attr_node attr;
6352   struct indirect_string_node *node;
6353
6354   node = find_AT_string (str);
6355
6356   attr.dw_attr = attr_kind;
6357   attr.dw_attr_val.val_class = dw_val_class_str;
6358   attr.dw_attr_val.v.val_str = node;
6359   add_dwarf_attr (die, &attr);
6360 }
6361
6362 static inline const char *
6363 AT_string (dw_attr_ref a)
6364 {
6365   gcc_assert (a && AT_class (a) == dw_val_class_str);
6366   return a->dw_attr_val.v.val_str->str;
6367 }
6368
6369 /* Find out whether a string should be output inline in DIE
6370    or out-of-line in .debug_str section.  */
6371
6372 static enum dwarf_form
6373 AT_string_form (dw_attr_ref a)
6374 {
6375   struct indirect_string_node *node;
6376   unsigned int len;
6377   char label[32];
6378
6379   gcc_assert (a && AT_class (a) == dw_val_class_str);
6380
6381   node = a->dw_attr_val.v.val_str;
6382   if (node->form)
6383     return node->form;
6384
6385   len = strlen (node->str) + 1;
6386
6387   /* If the string is shorter or equal to the size of the reference, it is
6388      always better to put it inline.  */
6389   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6390     return node->form = DW_FORM_string;
6391
6392   /* If we cannot expect the linker to merge strings in .debug_str
6393      section, only put it into .debug_str if it is worth even in this
6394      single module.  */
6395   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6396       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6397     return node->form = DW_FORM_string;
6398
6399   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6400   ++dw2_string_counter;
6401   node->label = xstrdup (label);
6402
6403   return node->form = DW_FORM_strp;
6404 }
6405
6406 /* Add a DIE reference attribute value to a DIE.  */
6407
6408 static inline void
6409 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6410 {
6411   dw_attr_node attr;
6412
6413   attr.dw_attr = attr_kind;
6414   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6415   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6416   attr.dw_attr_val.v.val_die_ref.external = 0;
6417   add_dwarf_attr (die, &attr);
6418 }
6419
6420 /* Add an AT_specification attribute to a DIE, and also make the back
6421    pointer from the specification to the definition.  */
6422
6423 static inline void
6424 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6425 {
6426   add_AT_die_ref (die, DW_AT_specification, targ_die);
6427   gcc_assert (!targ_die->die_definition);
6428   targ_die->die_definition = die;
6429 }
6430
6431 static inline dw_die_ref
6432 AT_ref (dw_attr_ref a)
6433 {
6434   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6435   return a->dw_attr_val.v.val_die_ref.die;
6436 }
6437
6438 static inline int
6439 AT_ref_external (dw_attr_ref a)
6440 {
6441   if (a && AT_class (a) == dw_val_class_die_ref)
6442     return a->dw_attr_val.v.val_die_ref.external;
6443
6444   return 0;
6445 }
6446
6447 static inline void
6448 set_AT_ref_external (dw_attr_ref a, int i)
6449 {
6450   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6451   a->dw_attr_val.v.val_die_ref.external = i;
6452 }
6453
6454 /* Add an FDE reference attribute value to a DIE.  */
6455
6456 static inline void
6457 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6458 {
6459   dw_attr_node attr;
6460
6461   attr.dw_attr = attr_kind;
6462   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6463   attr.dw_attr_val.v.val_fde_index = targ_fde;
6464   add_dwarf_attr (die, &attr);
6465 }
6466
6467 /* Add a location description attribute value to a DIE.  */
6468
6469 static inline void
6470 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6471 {
6472   dw_attr_node attr;
6473
6474   attr.dw_attr = attr_kind;
6475   attr.dw_attr_val.val_class = dw_val_class_loc;
6476   attr.dw_attr_val.v.val_loc = loc;
6477   add_dwarf_attr (die, &attr);
6478 }
6479
6480 static inline dw_loc_descr_ref
6481 AT_loc (dw_attr_ref a)
6482 {
6483   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6484   return a->dw_attr_val.v.val_loc;
6485 }
6486
6487 static inline void
6488 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6489 {
6490   dw_attr_node attr;
6491
6492   attr.dw_attr = attr_kind;
6493   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6494   attr.dw_attr_val.v.val_loc_list = loc_list;
6495   add_dwarf_attr (die, &attr);
6496   have_location_lists = true;
6497 }
6498
6499 static inline dw_loc_list_ref
6500 AT_loc_list (dw_attr_ref a)
6501 {
6502   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6503   return a->dw_attr_val.v.val_loc_list;
6504 }
6505
6506 /* Add an address constant attribute value to a DIE.  */
6507
6508 static inline void
6509 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6510 {
6511   dw_attr_node attr;
6512
6513   attr.dw_attr = attr_kind;
6514   attr.dw_attr_val.val_class = dw_val_class_addr;
6515   attr.dw_attr_val.v.val_addr = addr;
6516   add_dwarf_attr (die, &attr);
6517 }
6518
6519 /* Get the RTX from to an address DIE attribute.  */
6520
6521 static inline rtx
6522 AT_addr (dw_attr_ref a)
6523 {
6524   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6525   return a->dw_attr_val.v.val_addr;
6526 }
6527
6528 /* Add a file attribute value to a DIE.  */
6529
6530 static inline void
6531 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6532              struct dwarf_file_data *fd)
6533 {
6534   dw_attr_node attr;
6535
6536   attr.dw_attr = attr_kind;
6537   attr.dw_attr_val.val_class = dw_val_class_file;
6538   attr.dw_attr_val.v.val_file = fd;
6539   add_dwarf_attr (die, &attr);
6540 }
6541
6542 /* Get the dwarf_file_data from a file DIE attribute.  */
6543
6544 static inline struct dwarf_file_data *
6545 AT_file (dw_attr_ref a)
6546 {
6547   gcc_assert (a && AT_class (a) == dw_val_class_file);
6548   return a->dw_attr_val.v.val_file;
6549 }
6550
6551 /* Add a label identifier attribute value to a DIE.  */
6552
6553 static inline void
6554 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6555 {
6556   dw_attr_node attr;
6557
6558   attr.dw_attr = attr_kind;
6559   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6560   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6561   add_dwarf_attr (die, &attr);
6562 }
6563
6564 /* Add a section offset attribute value to a DIE, an offset into the
6565    debug_line section.  */
6566
6567 static inline void
6568 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6569                 const char *label)
6570 {
6571   dw_attr_node attr;
6572
6573   attr.dw_attr = attr_kind;
6574   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6575   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6576   add_dwarf_attr (die, &attr);
6577 }
6578
6579 /* Add a section offset attribute value to a DIE, an offset into the
6580    debug_macinfo section.  */
6581
6582 static inline void
6583 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6584                const char *label)
6585 {
6586   dw_attr_node attr;
6587
6588   attr.dw_attr = attr_kind;
6589   attr.dw_attr_val.val_class = dw_val_class_macptr;
6590   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6591   add_dwarf_attr (die, &attr);
6592 }
6593
6594 /* Add an offset attribute value to a DIE.  */
6595
6596 static inline void
6597 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6598                unsigned HOST_WIDE_INT offset)
6599 {
6600   dw_attr_node attr;
6601
6602   attr.dw_attr = attr_kind;
6603   attr.dw_attr_val.val_class = dw_val_class_offset;
6604   attr.dw_attr_val.v.val_offset = offset;
6605   add_dwarf_attr (die, &attr);
6606 }
6607
6608 /* Add an range_list attribute value to a DIE.  */
6609
6610 static void
6611 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6612                    long unsigned int offset)
6613 {
6614   dw_attr_node attr;
6615
6616   attr.dw_attr = attr_kind;
6617   attr.dw_attr_val.val_class = dw_val_class_range_list;
6618   attr.dw_attr_val.v.val_offset = offset;
6619   add_dwarf_attr (die, &attr);
6620 }
6621
6622 static inline const char *
6623 AT_lbl (dw_attr_ref a)
6624 {
6625   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6626                     || AT_class (a) == dw_val_class_lineptr
6627                     || AT_class (a) == dw_val_class_macptr));
6628   return a->dw_attr_val.v.val_lbl_id;
6629 }
6630
6631 /* Get the attribute of type attr_kind.  */
6632
6633 static dw_attr_ref
6634 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6635 {
6636   dw_attr_ref a;
6637   unsigned ix;
6638   dw_die_ref spec = NULL;
6639
6640   if (! die)
6641     return NULL;
6642
6643   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6644     if (a->dw_attr == attr_kind)
6645       return a;
6646     else if (a->dw_attr == DW_AT_specification
6647              || a->dw_attr == DW_AT_abstract_origin)
6648       spec = AT_ref (a);
6649
6650   if (spec)
6651     return get_AT (spec, attr_kind);
6652
6653   return NULL;
6654 }
6655
6656 /* Return the "low pc" attribute value, typically associated with a subprogram
6657    DIE.  Return null if the "low pc" attribute is either not present, or if it
6658    cannot be represented as an assembler label identifier.  */
6659
6660 static inline const char *
6661 get_AT_low_pc (dw_die_ref die)
6662 {
6663   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6664
6665   return a ? AT_lbl (a) : NULL;
6666 }
6667
6668 /* Return the "high pc" attribute value, typically associated with a subprogram
6669    DIE.  Return null if the "high pc" attribute is either not present, or if it
6670    cannot be represented as an assembler label identifier.  */
6671
6672 static inline const char *
6673 get_AT_hi_pc (dw_die_ref die)
6674 {
6675   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6676
6677   return a ? AT_lbl (a) : NULL;
6678 }
6679
6680 /* Return the value of the string attribute designated by ATTR_KIND, or
6681    NULL if it is not present.  */
6682
6683 static inline const char *
6684 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6685 {
6686   dw_attr_ref a = get_AT (die, attr_kind);
6687
6688   return a ? AT_string (a) : NULL;
6689 }
6690
6691 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6692    if it is not present.  */
6693
6694 static inline int
6695 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6696 {
6697   dw_attr_ref a = get_AT (die, attr_kind);
6698
6699   return a ? AT_flag (a) : 0;
6700 }
6701
6702 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6703    if it is not present.  */
6704
6705 static inline unsigned
6706 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6707 {
6708   dw_attr_ref a = get_AT (die, attr_kind);
6709
6710   return a ? AT_unsigned (a) : 0;
6711 }
6712
6713 static inline dw_die_ref
6714 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6715 {
6716   dw_attr_ref a = get_AT (die, attr_kind);
6717
6718   return a ? AT_ref (a) : NULL;
6719 }
6720
6721 static inline struct dwarf_file_data *
6722 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6723 {
6724   dw_attr_ref a = get_AT (die, attr_kind);
6725
6726   return a ? AT_file (a) : NULL;
6727 }
6728
6729 /* Return TRUE if the language is C or C++.  */
6730
6731 static inline bool
6732 is_c_family (void)
6733 {
6734   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6735
6736   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6737           || lang == DW_LANG_C99
6738           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6739 }
6740
6741 /* Return TRUE if the language is C++.  */
6742
6743 static inline bool
6744 is_cxx (void)
6745 {
6746   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6747
6748   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6749 }
6750
6751 /* Return TRUE if the language is Fortran.  */
6752
6753 static inline bool
6754 is_fortran (void)
6755 {
6756   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6757
6758   return (lang == DW_LANG_Fortran77
6759           || lang == DW_LANG_Fortran90
6760           || lang == DW_LANG_Fortran95);
6761 }
6762
6763 /* Return TRUE if the language is Java.  */
6764
6765 static inline bool
6766 is_java (void)
6767 {
6768   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6769
6770   return lang == DW_LANG_Java;
6771 }
6772
6773 /* Return TRUE if the language is Ada.  */
6774
6775 static inline bool
6776 is_ada (void)
6777 {
6778   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6779
6780   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6781 }
6782
6783 /* Remove the specified attribute if present.  */
6784
6785 static void
6786 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6787 {
6788   dw_attr_ref a;
6789   unsigned ix;
6790
6791   if (! die)
6792     return;
6793
6794   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6795     if (a->dw_attr == attr_kind)
6796       {
6797         if (AT_class (a) == dw_val_class_str)
6798           if (a->dw_attr_val.v.val_str->refcount)
6799             a->dw_attr_val.v.val_str->refcount--;
6800
6801         /* VEC_ordered_remove should help reduce the number of abbrevs
6802            that are needed.  */
6803         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6804         return;
6805       }
6806 }
6807
6808 /* Remove CHILD from its parent.  PREV must have the property that
6809    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6810
6811 static void
6812 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6813 {
6814   gcc_assert (child->die_parent == prev->die_parent);
6815   gcc_assert (prev->die_sib == child);
6816   if (prev == child)
6817     {
6818       gcc_assert (child->die_parent->die_child == child);
6819       prev = NULL;
6820     }
6821   else
6822     prev->die_sib = child->die_sib;
6823   if (child->die_parent->die_child == child)
6824     child->die_parent->die_child = prev;
6825 }
6826
6827 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6828    matches TAG.  */
6829
6830 static void
6831 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6832 {
6833   dw_die_ref c;
6834
6835   c = die->die_child;
6836   if (c) do {
6837     dw_die_ref prev = c;
6838     c = c->die_sib;
6839     while (c->die_tag == tag)
6840       {
6841         remove_child_with_prev (c, prev);
6842         /* Might have removed every child.  */
6843         if (c == c->die_sib)
6844           return;
6845         c = c->die_sib;
6846       }
6847   } while (c != die->die_child);
6848 }
6849
6850 /* Add a CHILD_DIE as the last child of DIE.  */
6851
6852 static void
6853 add_child_die (dw_die_ref die, dw_die_ref child_die)
6854 {
6855   /* FIXME this should probably be an assert.  */
6856   if (! die || ! child_die)
6857     return;
6858   gcc_assert (die != child_die);
6859
6860   child_die->die_parent = die;
6861   if (die->die_child)
6862     {
6863       child_die->die_sib = die->die_child->die_sib;
6864       die->die_child->die_sib = child_die;
6865     }
6866   else
6867     child_die->die_sib = child_die;
6868   die->die_child = child_die;
6869 }
6870
6871 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6872    is the specification, to the end of PARENT's list of children.
6873    This is done by removing and re-adding it.  */
6874
6875 static void
6876 splice_child_die (dw_die_ref parent, dw_die_ref child)
6877 {
6878   dw_die_ref p;
6879
6880   /* We want the declaration DIE from inside the class, not the
6881      specification DIE at toplevel.  */
6882   if (child->die_parent != parent)
6883     {
6884       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6885
6886       if (tmp)
6887         child = tmp;
6888     }
6889
6890   gcc_assert (child->die_parent == parent
6891               || (child->die_parent
6892                   == get_AT_ref (parent, DW_AT_specification)));
6893
6894   for (p = child->die_parent->die_child; ; p = p->die_sib)
6895     if (p->die_sib == child)
6896       {
6897         remove_child_with_prev (child, p);
6898         break;
6899       }
6900
6901   add_child_die (parent, child);
6902 }
6903
6904 /* Return a pointer to a newly created DIE node.  */
6905
6906 static inline dw_die_ref
6907 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6908 {
6909   dw_die_ref die = GGC_CNEW (die_node);
6910
6911   die->die_tag = tag_value;
6912
6913   if (parent_die != NULL)
6914     add_child_die (parent_die, die);
6915   else
6916     {
6917       limbo_die_node *limbo_node;
6918
6919       limbo_node = GGC_CNEW (limbo_die_node);
6920       limbo_node->die = die;
6921       limbo_node->created_for = t;
6922       limbo_node->next = limbo_die_list;
6923       limbo_die_list = limbo_node;
6924     }
6925
6926   return die;
6927 }
6928
6929 /* Return the DIE associated with the given type specifier.  */
6930
6931 static inline dw_die_ref
6932 lookup_type_die (tree type)
6933 {
6934   return TYPE_SYMTAB_DIE (type);
6935 }
6936
6937 /* Equate a DIE to a given type specifier.  */
6938
6939 static inline void
6940 equate_type_number_to_die (tree type, dw_die_ref type_die)
6941 {
6942   TYPE_SYMTAB_DIE (type) = type_die;
6943 }
6944
6945 /* Returns a hash value for X (which really is a die_struct).  */
6946
6947 static hashval_t
6948 decl_die_table_hash (const void *x)
6949 {
6950   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6951 }
6952
6953 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6954
6955 static int
6956 decl_die_table_eq (const void *x, const void *y)
6957 {
6958   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6959 }
6960
6961 /* Return the DIE associated with a given declaration.  */
6962
6963 static inline dw_die_ref
6964 lookup_decl_die (tree decl)
6965 {
6966   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6967 }
6968
6969 /* Returns a hash value for X (which really is a var_loc_list).  */
6970
6971 static hashval_t
6972 decl_loc_table_hash (const void *x)
6973 {
6974   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6975 }
6976
6977 /* Return nonzero if decl_id of var_loc_list X is the same as
6978    UID of decl *Y.  */
6979
6980 static int
6981 decl_loc_table_eq (const void *x, const void *y)
6982 {
6983   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6984 }
6985
6986 /* Return the var_loc list associated with a given declaration.  */
6987
6988 static inline var_loc_list *
6989 lookup_decl_loc (const_tree decl)
6990 {
6991   return (var_loc_list *)
6992     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6993 }
6994
6995 /* Equate a DIE to a particular declaration.  */
6996
6997 static void
6998 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6999 {
7000   unsigned int decl_id = DECL_UID (decl);
7001   void **slot;
7002
7003   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7004   *slot = decl_die;
7005   decl_die->decl_id = decl_id;
7006 }
7007
7008 /* Add a variable location node to the linked list for DECL.  */
7009
7010 static void
7011 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7012 {
7013   unsigned int decl_id = DECL_UID (decl);
7014   var_loc_list *temp;
7015   void **slot;
7016
7017   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7018   if (*slot == NULL)
7019     {
7020       temp = GGC_CNEW (var_loc_list);
7021       temp->decl_id = decl_id;
7022       *slot = temp;
7023     }
7024   else
7025     temp = (var_loc_list *) *slot;
7026
7027   if (temp->last)
7028     {
7029       /* If the current location is the same as the end of the list,
7030          and either both or neither of the locations is uninitialized,
7031          we have nothing to do.  */
7032       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7033                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7034           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7035                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7036               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7037                    == VAR_INIT_STATUS_UNINITIALIZED)
7038                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7039                       == VAR_INIT_STATUS_UNINITIALIZED))))
7040         {
7041           /* Add LOC to the end of list and update LAST.  */
7042           temp->last->next = loc;
7043           temp->last = loc;
7044         }
7045     }
7046   /* Do not add empty location to the beginning of the list.  */
7047   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7048     {
7049       temp->first = loc;
7050       temp->last = loc;
7051     }
7052 }
7053 \f
7054 /* Keep track of the number of spaces used to indent the
7055    output of the debugging routines that print the structure of
7056    the DIE internal representation.  */
7057 static int print_indent;
7058
7059 /* Indent the line the number of spaces given by print_indent.  */
7060
7061 static inline void
7062 print_spaces (FILE *outfile)
7063 {
7064   fprintf (outfile, "%*s", print_indent, "");
7065 }
7066
7067 /* Print the information associated with a given DIE, and its children.
7068    This routine is a debugging aid only.  */
7069
7070 static void
7071 print_die (dw_die_ref die, FILE *outfile)
7072 {
7073   dw_attr_ref a;
7074   dw_die_ref c;
7075   unsigned ix;
7076
7077   print_spaces (outfile);
7078   fprintf (outfile, "DIE %4ld: %s\n",
7079            die->die_offset, dwarf_tag_name (die->die_tag));
7080   print_spaces (outfile);
7081   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7082   fprintf (outfile, " offset: %ld\n", die->die_offset);
7083
7084   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7085     {
7086       print_spaces (outfile);
7087       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7088
7089       switch (AT_class (a))
7090         {
7091         case dw_val_class_addr:
7092           fprintf (outfile, "address");
7093           break;
7094         case dw_val_class_offset:
7095           fprintf (outfile, "offset");
7096           break;
7097         case dw_val_class_loc:
7098           fprintf (outfile, "location descriptor");
7099           break;
7100         case dw_val_class_loc_list:
7101           fprintf (outfile, "location list -> label:%s",
7102                    AT_loc_list (a)->ll_symbol);
7103           break;
7104         case dw_val_class_range_list:
7105           fprintf (outfile, "range list");
7106           break;
7107         case dw_val_class_const:
7108           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7109           break;
7110         case dw_val_class_unsigned_const:
7111           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7112           break;
7113         case dw_val_class_long_long:
7114           fprintf (outfile, "constant (%lu,%lu)",
7115                    a->dw_attr_val.v.val_long_long.hi,
7116                    a->dw_attr_val.v.val_long_long.low);
7117           break;
7118         case dw_val_class_vec:
7119           fprintf (outfile, "floating-point or vector constant");
7120           break;
7121         case dw_val_class_flag:
7122           fprintf (outfile, "%u", AT_flag (a));
7123           break;
7124         case dw_val_class_die_ref:
7125           if (AT_ref (a) != NULL)
7126             {
7127               if (AT_ref (a)->die_symbol)
7128                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7129               else
7130                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7131             }
7132           else
7133             fprintf (outfile, "die -> <null>");
7134           break;
7135         case dw_val_class_lbl_id:
7136         case dw_val_class_lineptr:
7137         case dw_val_class_macptr:
7138           fprintf (outfile, "label: %s", AT_lbl (a));
7139           break;
7140         case dw_val_class_str:
7141           if (AT_string (a) != NULL)
7142             fprintf (outfile, "\"%s\"", AT_string (a));
7143           else
7144             fprintf (outfile, "<null>");
7145           break;
7146         case dw_val_class_file:
7147           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7148                    AT_file (a)->emitted_number);
7149           break;
7150         default:
7151           break;
7152         }
7153
7154       fprintf (outfile, "\n");
7155     }
7156
7157   if (die->die_child != NULL)
7158     {
7159       print_indent += 4;
7160       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7161       print_indent -= 4;
7162     }
7163   if (print_indent == 0)
7164     fprintf (outfile, "\n");
7165 }
7166
7167 /* Print the contents of the source code line number correspondence table.
7168    This routine is a debugging aid only.  */
7169
7170 static void
7171 print_dwarf_line_table (FILE *outfile)
7172 {
7173   unsigned i;
7174   dw_line_info_ref line_info;
7175
7176   fprintf (outfile, "\n\nDWARF source line information\n");
7177   for (i = 1; i < line_info_table_in_use; i++)
7178     {
7179       line_info = &line_info_table[i];
7180       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7181                line_info->dw_file_num,
7182                line_info->dw_line_num);
7183     }
7184
7185   fprintf (outfile, "\n\n");
7186 }
7187
7188 /* Print the information collected for a given DIE.  */
7189
7190 void
7191 debug_dwarf_die (dw_die_ref die)
7192 {
7193   print_die (die, stderr);
7194 }
7195
7196 /* Print all DWARF information collected for the compilation unit.
7197    This routine is a debugging aid only.  */
7198
7199 void
7200 debug_dwarf (void)
7201 {
7202   print_indent = 0;
7203   print_die (comp_unit_die, stderr);
7204   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7205     print_dwarf_line_table (stderr);
7206 }
7207 \f
7208 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7209    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7210    DIE that marks the start of the DIEs for this include file.  */
7211
7212 static dw_die_ref
7213 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7214 {
7215   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7216   dw_die_ref new_unit = gen_compile_unit_die (filename);
7217
7218   new_unit->die_sib = old_unit;
7219   return new_unit;
7220 }
7221
7222 /* Close an include-file CU and reopen the enclosing one.  */
7223
7224 static dw_die_ref
7225 pop_compile_unit (dw_die_ref old_unit)
7226 {
7227   dw_die_ref new_unit = old_unit->die_sib;
7228
7229   old_unit->die_sib = NULL;
7230   return new_unit;
7231 }
7232
7233 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7234 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7235
7236 /* Calculate the checksum of a location expression.  */
7237
7238 static inline void
7239 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7240 {
7241   CHECKSUM (loc->dw_loc_opc);
7242   CHECKSUM (loc->dw_loc_oprnd1);
7243   CHECKSUM (loc->dw_loc_oprnd2);
7244 }
7245
7246 /* Calculate the checksum of an attribute.  */
7247
7248 static void
7249 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7250 {
7251   dw_loc_descr_ref loc;
7252   rtx r;
7253
7254   CHECKSUM (at->dw_attr);
7255
7256   /* We don't care that this was compiled with a different compiler
7257      snapshot; if the output is the same, that's what matters.  */
7258   if (at->dw_attr == DW_AT_producer)
7259     return;
7260
7261   switch (AT_class (at))
7262     {
7263     case dw_val_class_const:
7264       CHECKSUM (at->dw_attr_val.v.val_int);
7265       break;
7266     case dw_val_class_unsigned_const:
7267       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7268       break;
7269     case dw_val_class_long_long:
7270       CHECKSUM (at->dw_attr_val.v.val_long_long);
7271       break;
7272     case dw_val_class_vec:
7273       CHECKSUM (at->dw_attr_val.v.val_vec);
7274       break;
7275     case dw_val_class_flag:
7276       CHECKSUM (at->dw_attr_val.v.val_flag);
7277       break;
7278     case dw_val_class_str:
7279       CHECKSUM_STRING (AT_string (at));
7280       break;
7281
7282     case dw_val_class_addr:
7283       r = AT_addr (at);
7284       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7285       CHECKSUM_STRING (XSTR (r, 0));
7286       break;
7287
7288     case dw_val_class_offset:
7289       CHECKSUM (at->dw_attr_val.v.val_offset);
7290       break;
7291
7292     case dw_val_class_loc:
7293       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7294         loc_checksum (loc, ctx);
7295       break;
7296
7297     case dw_val_class_die_ref:
7298       die_checksum (AT_ref (at), ctx, mark);
7299       break;
7300
7301     case dw_val_class_fde_ref:
7302     case dw_val_class_lbl_id:
7303     case dw_val_class_lineptr:
7304     case dw_val_class_macptr:
7305       break;
7306
7307     case dw_val_class_file:
7308       CHECKSUM_STRING (AT_file (at)->filename);
7309       break;
7310
7311     default:
7312       break;
7313     }
7314 }
7315
7316 /* Calculate the checksum of a DIE.  */
7317
7318 static void
7319 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7320 {
7321   dw_die_ref c;
7322   dw_attr_ref a;
7323   unsigned ix;
7324
7325   /* To avoid infinite recursion.  */
7326   if (die->die_mark)
7327     {
7328       CHECKSUM (die->die_mark);
7329       return;
7330     }
7331   die->die_mark = ++(*mark);
7332
7333   CHECKSUM (die->die_tag);
7334
7335   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7336     attr_checksum (a, ctx, mark);
7337
7338   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7339 }
7340
7341 #undef CHECKSUM
7342 #undef CHECKSUM_STRING
7343
7344 /* Do the location expressions look same?  */
7345 static inline int
7346 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7347 {
7348   return loc1->dw_loc_opc == loc2->dw_loc_opc
7349          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7350          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7351 }
7352
7353 /* Do the values look the same?  */
7354 static int
7355 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7356 {
7357   dw_loc_descr_ref loc1, loc2;
7358   rtx r1, r2;
7359
7360   if (v1->val_class != v2->val_class)
7361     return 0;
7362
7363   switch (v1->val_class)
7364     {
7365     case dw_val_class_const:
7366       return v1->v.val_int == v2->v.val_int;
7367     case dw_val_class_unsigned_const:
7368       return v1->v.val_unsigned == v2->v.val_unsigned;
7369     case dw_val_class_long_long:
7370       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7371              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7372     case dw_val_class_vec:
7373       if (v1->v.val_vec.length != v2->v.val_vec.length
7374           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7375         return 0;
7376       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7377                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7378         return 0;
7379       return 1;
7380     case dw_val_class_flag:
7381       return v1->v.val_flag == v2->v.val_flag;
7382     case dw_val_class_str:
7383       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7384
7385     case dw_val_class_addr:
7386       r1 = v1->v.val_addr;
7387       r2 = v2->v.val_addr;
7388       if (GET_CODE (r1) != GET_CODE (r2))
7389         return 0;
7390       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7391       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7392
7393     case dw_val_class_offset:
7394       return v1->v.val_offset == v2->v.val_offset;
7395
7396     case dw_val_class_loc:
7397       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7398            loc1 && loc2;
7399            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7400         if (!same_loc_p (loc1, loc2, mark))
7401           return 0;
7402       return !loc1 && !loc2;
7403
7404     case dw_val_class_die_ref:
7405       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7406
7407     case dw_val_class_fde_ref:
7408     case dw_val_class_lbl_id:
7409     case dw_val_class_lineptr:
7410     case dw_val_class_macptr:
7411       return 1;
7412
7413     case dw_val_class_file:
7414       return v1->v.val_file == v2->v.val_file;
7415
7416     default:
7417       return 1;
7418     }
7419 }
7420
7421 /* Do the attributes look the same?  */
7422
7423 static int
7424 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7425 {
7426   if (at1->dw_attr != at2->dw_attr)
7427     return 0;
7428
7429   /* We don't care that this was compiled with a different compiler
7430      snapshot; if the output is the same, that's what matters. */
7431   if (at1->dw_attr == DW_AT_producer)
7432     return 1;
7433
7434   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7435 }
7436
7437 /* Do the dies look the same?  */
7438
7439 static int
7440 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7441 {
7442   dw_die_ref c1, c2;
7443   dw_attr_ref a1;
7444   unsigned ix;
7445
7446   /* To avoid infinite recursion.  */
7447   if (die1->die_mark)
7448     return die1->die_mark == die2->die_mark;
7449   die1->die_mark = die2->die_mark = ++(*mark);
7450
7451   if (die1->die_tag != die2->die_tag)
7452     return 0;
7453
7454   if (VEC_length (dw_attr_node, die1->die_attr)
7455       != VEC_length (dw_attr_node, die2->die_attr))
7456     return 0;
7457
7458   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7459     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7460       return 0;
7461
7462   c1 = die1->die_child;
7463   c2 = die2->die_child;
7464   if (! c1)
7465     {
7466       if (c2)
7467         return 0;
7468     }
7469   else
7470     for (;;)
7471       {
7472         if (!same_die_p (c1, c2, mark))
7473           return 0;
7474         c1 = c1->die_sib;
7475         c2 = c2->die_sib;
7476         if (c1 == die1->die_child)
7477           {
7478             if (c2 == die2->die_child)
7479               break;
7480             else
7481               return 0;
7482           }
7483     }
7484
7485   return 1;
7486 }
7487
7488 /* Do the dies look the same?  Wrapper around same_die_p.  */
7489
7490 static int
7491 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7492 {
7493   int mark = 0;
7494   int ret = same_die_p (die1, die2, &mark);
7495
7496   unmark_all_dies (die1);
7497   unmark_all_dies (die2);
7498
7499   return ret;
7500 }
7501
7502 /* The prefix to attach to symbols on DIEs in the current comdat debug
7503    info section.  */
7504 static char *comdat_symbol_id;
7505
7506 /* The index of the current symbol within the current comdat CU.  */
7507 static unsigned int comdat_symbol_number;
7508
7509 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7510    children, and set comdat_symbol_id accordingly.  */
7511
7512 static void
7513 compute_section_prefix (dw_die_ref unit_die)
7514 {
7515   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7516   const char *base = die_name ? lbasename (die_name) : "anonymous";
7517   char *name = XALLOCAVEC (char, strlen (base) + 64);
7518   char *p;
7519   int i, mark;
7520   unsigned char checksum[16];
7521   struct md5_ctx ctx;
7522
7523   /* Compute the checksum of the DIE, then append part of it as hex digits to
7524      the name filename of the unit.  */
7525
7526   md5_init_ctx (&ctx);
7527   mark = 0;
7528   die_checksum (unit_die, &ctx, &mark);
7529   unmark_all_dies (unit_die);
7530   md5_finish_ctx (&ctx, checksum);
7531
7532   sprintf (name, "%s.", base);
7533   clean_symbol_name (name);
7534
7535   p = name + strlen (name);
7536   for (i = 0; i < 4; i++)
7537     {
7538       sprintf (p, "%.2x", checksum[i]);
7539       p += 2;
7540     }
7541
7542   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7543   comdat_symbol_number = 0;
7544 }
7545
7546 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7547
7548 static int
7549 is_type_die (dw_die_ref die)
7550 {
7551   switch (die->die_tag)
7552     {
7553     case DW_TAG_array_type:
7554     case DW_TAG_class_type:
7555     case DW_TAG_interface_type:
7556     case DW_TAG_enumeration_type:
7557     case DW_TAG_pointer_type:
7558     case DW_TAG_reference_type:
7559     case DW_TAG_string_type:
7560     case DW_TAG_structure_type:
7561     case DW_TAG_subroutine_type:
7562     case DW_TAG_union_type:
7563     case DW_TAG_ptr_to_member_type:
7564     case DW_TAG_set_type:
7565     case DW_TAG_subrange_type:
7566     case DW_TAG_base_type:
7567     case DW_TAG_const_type:
7568     case DW_TAG_file_type:
7569     case DW_TAG_packed_type:
7570     case DW_TAG_volatile_type:
7571     case DW_TAG_typedef:
7572       return 1;
7573     default:
7574       return 0;
7575     }
7576 }
7577
7578 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7579    Basically, we want to choose the bits that are likely to be shared between
7580    compilations (types) and leave out the bits that are specific to individual
7581    compilations (functions).  */
7582
7583 static int
7584 is_comdat_die (dw_die_ref c)
7585 {
7586   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7587      we do for stabs.  The advantage is a greater likelihood of sharing between
7588      objects that don't include headers in the same order (and therefore would
7589      put the base types in a different comdat).  jason 8/28/00 */
7590
7591   if (c->die_tag == DW_TAG_base_type)
7592     return 0;
7593
7594   if (c->die_tag == DW_TAG_pointer_type
7595       || c->die_tag == DW_TAG_reference_type
7596       || c->die_tag == DW_TAG_const_type
7597       || c->die_tag == DW_TAG_volatile_type)
7598     {
7599       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7600
7601       return t ? is_comdat_die (t) : 0;
7602     }
7603
7604   return is_type_die (c);
7605 }
7606
7607 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7608    compilation unit.  */
7609
7610 static int
7611 is_symbol_die (dw_die_ref c)
7612 {
7613   return (is_type_die (c)
7614           || (get_AT (c, DW_AT_declaration)
7615               && !get_AT (c, DW_AT_specification))
7616           || c->die_tag == DW_TAG_namespace
7617           || c->die_tag == DW_TAG_module);
7618 }
7619
7620 static char *
7621 gen_internal_sym (const char *prefix)
7622 {
7623   char buf[256];
7624
7625   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7626   return xstrdup (buf);
7627 }
7628
7629 /* Assign symbols to all worthy DIEs under DIE.  */
7630
7631 static void
7632 assign_symbol_names (dw_die_ref die)
7633 {
7634   dw_die_ref c;
7635
7636   if (is_symbol_die (die))
7637     {
7638       if (comdat_symbol_id)
7639         {
7640           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7641
7642           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7643                    comdat_symbol_id, comdat_symbol_number++);
7644           die->die_symbol = xstrdup (p);
7645         }
7646       else
7647         die->die_symbol = gen_internal_sym ("LDIE");
7648     }
7649
7650   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7651 }
7652
7653 struct cu_hash_table_entry
7654 {
7655   dw_die_ref cu;
7656   unsigned min_comdat_num, max_comdat_num;
7657   struct cu_hash_table_entry *next;
7658 };
7659
7660 /* Routines to manipulate hash table of CUs.  */
7661 static hashval_t
7662 htab_cu_hash (const void *of)
7663 {
7664   const struct cu_hash_table_entry *const entry =
7665     (const struct cu_hash_table_entry *) of;
7666
7667   return htab_hash_string (entry->cu->die_symbol);
7668 }
7669
7670 static int
7671 htab_cu_eq (const void *of1, const void *of2)
7672 {
7673   const struct cu_hash_table_entry *const entry1 =
7674     (const struct cu_hash_table_entry *) of1;
7675   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7676
7677   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7678 }
7679
7680 static void
7681 htab_cu_del (void *what)
7682 {
7683   struct cu_hash_table_entry *next,
7684     *entry = (struct cu_hash_table_entry *) what;
7685
7686   while (entry)
7687     {
7688       next = entry->next;
7689       free (entry);
7690       entry = next;
7691     }
7692 }
7693
7694 /* Check whether we have already seen this CU and set up SYM_NUM
7695    accordingly.  */
7696 static int
7697 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7698 {
7699   struct cu_hash_table_entry dummy;
7700   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7701
7702   dummy.max_comdat_num = 0;
7703
7704   slot = (struct cu_hash_table_entry **)
7705     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7706         INSERT);
7707   entry = *slot;
7708
7709   for (; entry; last = entry, entry = entry->next)
7710     {
7711       if (same_die_p_wrap (cu, entry->cu))
7712         break;
7713     }
7714
7715   if (entry)
7716     {
7717       *sym_num = entry->min_comdat_num;
7718       return 1;
7719     }
7720
7721   entry = XCNEW (struct cu_hash_table_entry);
7722   entry->cu = cu;
7723   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7724   entry->next = *slot;
7725   *slot = entry;
7726
7727   return 0;
7728 }
7729
7730 /* Record SYM_NUM to record of CU in HTABLE.  */
7731 static void
7732 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7733 {
7734   struct cu_hash_table_entry **slot, *entry;
7735
7736   slot = (struct cu_hash_table_entry **)
7737     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7738         NO_INSERT);
7739   entry = *slot;
7740
7741   entry->max_comdat_num = sym_num;
7742 }
7743
7744 /* Traverse the DIE (which is always comp_unit_die), and set up
7745    additional compilation units for each of the include files we see
7746    bracketed by BINCL/EINCL.  */
7747
7748 static void
7749 break_out_includes (dw_die_ref die)
7750 {
7751   dw_die_ref c;
7752   dw_die_ref unit = NULL;
7753   limbo_die_node *node, **pnode;
7754   htab_t cu_hash_table;
7755
7756   c = die->die_child;
7757   if (c) do {
7758     dw_die_ref prev = c;
7759     c = c->die_sib;
7760     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7761            || (unit && is_comdat_die (c)))
7762       {
7763         dw_die_ref next = c->die_sib;
7764
7765         /* This DIE is for a secondary CU; remove it from the main one.  */
7766         remove_child_with_prev (c, prev);
7767
7768         if (c->die_tag == DW_TAG_GNU_BINCL)
7769           unit = push_new_compile_unit (unit, c);
7770         else if (c->die_tag == DW_TAG_GNU_EINCL)
7771           unit = pop_compile_unit (unit);
7772         else
7773           add_child_die (unit, c);
7774         c = next;
7775         if (c == die->die_child)
7776           break;
7777       }
7778   } while (c != die->die_child);
7779
7780 #if 0
7781   /* We can only use this in debugging, since the frontend doesn't check
7782      to make sure that we leave every include file we enter.  */
7783   gcc_assert (!unit);
7784 #endif
7785
7786   assign_symbol_names (die);
7787   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7788   for (node = limbo_die_list, pnode = &limbo_die_list;
7789        node;
7790        node = node->next)
7791     {
7792       int is_dupl;
7793
7794       compute_section_prefix (node->die);
7795       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7796                         &comdat_symbol_number);
7797       assign_symbol_names (node->die);
7798       if (is_dupl)
7799         *pnode = node->next;
7800       else
7801         {
7802           pnode = &node->next;
7803           record_comdat_symbol_number (node->die, cu_hash_table,
7804                 comdat_symbol_number);
7805         }
7806     }
7807   htab_delete (cu_hash_table);
7808 }
7809
7810 /* Traverse the DIE and add a sibling attribute if it may have the
7811    effect of speeding up access to siblings.  To save some space,
7812    avoid generating sibling attributes for DIE's without children.  */
7813
7814 static void
7815 add_sibling_attributes (dw_die_ref die)
7816 {
7817   dw_die_ref c;
7818
7819   if (! die->die_child)
7820     return;
7821
7822   if (die->die_parent && die != die->die_parent->die_child)
7823     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7824
7825   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7826 }
7827
7828 /* Output all location lists for the DIE and its children.  */
7829
7830 static void
7831 output_location_lists (dw_die_ref die)
7832 {
7833   dw_die_ref c;
7834   dw_attr_ref a;
7835   unsigned ix;
7836
7837   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7838     if (AT_class (a) == dw_val_class_loc_list)
7839       output_loc_list (AT_loc_list (a));
7840
7841   FOR_EACH_CHILD (die, c, output_location_lists (c));
7842 }
7843
7844 /* The format of each DIE (and its attribute value pairs) is encoded in an
7845    abbreviation table.  This routine builds the abbreviation table and assigns
7846    a unique abbreviation id for each abbreviation entry.  The children of each
7847    die are visited recursively.  */
7848
7849 static void
7850 build_abbrev_table (dw_die_ref die)
7851 {
7852   unsigned long abbrev_id;
7853   unsigned int n_alloc;
7854   dw_die_ref c;
7855   dw_attr_ref a;
7856   unsigned ix;
7857
7858   /* Scan the DIE references, and mark as external any that refer to
7859      DIEs from other CUs (i.e. those which are not marked).  */
7860   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7861     if (AT_class (a) == dw_val_class_die_ref
7862         && AT_ref (a)->die_mark == 0)
7863       {
7864         gcc_assert (AT_ref (a)->die_symbol);
7865
7866         set_AT_ref_external (a, 1);
7867       }
7868
7869   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7870     {
7871       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7872       dw_attr_ref die_a, abbrev_a;
7873       unsigned ix;
7874       bool ok = true;
7875
7876       if (abbrev->die_tag != die->die_tag)
7877         continue;
7878       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7879         continue;
7880
7881       if (VEC_length (dw_attr_node, abbrev->die_attr)
7882           != VEC_length (dw_attr_node, die->die_attr))
7883         continue;
7884
7885       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7886         {
7887           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7888           if ((abbrev_a->dw_attr != die_a->dw_attr)
7889               || (value_format (abbrev_a) != value_format (die_a)))
7890             {
7891               ok = false;
7892               break;
7893             }
7894         }
7895       if (ok)
7896         break;
7897     }
7898
7899   if (abbrev_id >= abbrev_die_table_in_use)
7900     {
7901       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7902         {
7903           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7904           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7905                                             n_alloc);
7906
7907           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7908                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7909           abbrev_die_table_allocated = n_alloc;
7910         }
7911
7912       ++abbrev_die_table_in_use;
7913       abbrev_die_table[abbrev_id] = die;
7914     }
7915
7916   die->die_abbrev = abbrev_id;
7917   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7918 }
7919 \f
7920 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7921
7922 static int
7923 constant_size (unsigned HOST_WIDE_INT value)
7924 {
7925   int log;
7926
7927   if (value == 0)
7928     log = 0;
7929   else
7930     log = floor_log2 (value);
7931
7932   log = log / 8;
7933   log = 1 << (floor_log2 (log) + 1);
7934
7935   return log;
7936 }
7937
7938 /* Return the size of a DIE as it is represented in the
7939    .debug_info section.  */
7940
7941 static unsigned long
7942 size_of_die (dw_die_ref die)
7943 {
7944   unsigned long size = 0;
7945   dw_attr_ref a;
7946   unsigned ix;
7947
7948   size += size_of_uleb128 (die->die_abbrev);
7949   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7950     {
7951       switch (AT_class (a))
7952         {
7953         case dw_val_class_addr:
7954           size += DWARF2_ADDR_SIZE;
7955           break;
7956         case dw_val_class_offset:
7957           size += DWARF_OFFSET_SIZE;
7958           break;
7959         case dw_val_class_loc:
7960           {
7961             unsigned long lsize = size_of_locs (AT_loc (a));
7962
7963             /* Block length.  */
7964             size += constant_size (lsize);
7965             size += lsize;
7966           }
7967           break;
7968         case dw_val_class_loc_list:
7969           size += DWARF_OFFSET_SIZE;
7970           break;
7971         case dw_val_class_range_list:
7972           size += DWARF_OFFSET_SIZE;
7973           break;
7974         case dw_val_class_const:
7975           size += size_of_sleb128 (AT_int (a));
7976           break;
7977         case dw_val_class_unsigned_const:
7978           size += constant_size (AT_unsigned (a));
7979           break;
7980         case dw_val_class_long_long:
7981           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7982           break;
7983         case dw_val_class_vec:
7984           size += constant_size (a->dw_attr_val.v.val_vec.length
7985                                  * a->dw_attr_val.v.val_vec.elt_size)
7986                   + a->dw_attr_val.v.val_vec.length
7987                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7988           break;
7989         case dw_val_class_flag:
7990           size += 1;
7991           break;
7992         case dw_val_class_die_ref:
7993           if (AT_ref_external (a))
7994             size += DWARF2_ADDR_SIZE;
7995           else
7996             size += DWARF_OFFSET_SIZE;
7997           break;
7998         case dw_val_class_fde_ref:
7999           size += DWARF_OFFSET_SIZE;
8000           break;
8001         case dw_val_class_lbl_id:
8002           size += DWARF2_ADDR_SIZE;
8003           break;
8004         case dw_val_class_lineptr:
8005         case dw_val_class_macptr:
8006           size += DWARF_OFFSET_SIZE;
8007           break;
8008         case dw_val_class_str:
8009           if (AT_string_form (a) == DW_FORM_strp)
8010             size += DWARF_OFFSET_SIZE;
8011           else
8012             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8013           break;
8014         case dw_val_class_file:
8015           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8016           break;
8017         default:
8018           gcc_unreachable ();
8019         }
8020     }
8021
8022   return size;
8023 }
8024
8025 /* Size the debugging information associated with a given DIE.  Visits the
8026    DIE's children recursively.  Updates the global variable next_die_offset, on
8027    each time through.  Uses the current value of next_die_offset to update the
8028    die_offset field in each DIE.  */
8029
8030 static void
8031 calc_die_sizes (dw_die_ref die)
8032 {
8033   dw_die_ref c;
8034
8035   die->die_offset = next_die_offset;
8036   next_die_offset += size_of_die (die);
8037
8038   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8039
8040   if (die->die_child != NULL)
8041     /* Count the null byte used to terminate sibling lists.  */
8042     next_die_offset += 1;
8043 }
8044
8045 /* Set the marks for a die and its children.  We do this so
8046    that we know whether or not a reference needs to use FORM_ref_addr; only
8047    DIEs in the same CU will be marked.  We used to clear out the offset
8048    and use that as the flag, but ran into ordering problems.  */
8049
8050 static void
8051 mark_dies (dw_die_ref die)
8052 {
8053   dw_die_ref c;
8054
8055   gcc_assert (!die->die_mark);
8056
8057   die->die_mark = 1;
8058   FOR_EACH_CHILD (die, c, mark_dies (c));
8059 }
8060
8061 /* Clear the marks for a die and its children.  */
8062
8063 static void
8064 unmark_dies (dw_die_ref die)
8065 {
8066   dw_die_ref c;
8067
8068   gcc_assert (die->die_mark);
8069
8070   die->die_mark = 0;
8071   FOR_EACH_CHILD (die, c, unmark_dies (c));
8072 }
8073
8074 /* Clear the marks for a die, its children and referred dies.  */
8075
8076 static void
8077 unmark_all_dies (dw_die_ref die)
8078 {
8079   dw_die_ref c;
8080   dw_attr_ref a;
8081   unsigned ix;
8082
8083   if (!die->die_mark)
8084     return;
8085   die->die_mark = 0;
8086
8087   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8088
8089   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8090     if (AT_class (a) == dw_val_class_die_ref)
8091       unmark_all_dies (AT_ref (a));
8092 }
8093
8094 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8095    generated for the compilation unit.  */
8096
8097 static unsigned long
8098 size_of_pubnames (VEC (pubname_entry, gc) * names)
8099 {
8100   unsigned long size;
8101   unsigned i;
8102   pubname_ref p;
8103
8104   size = DWARF_PUBNAMES_HEADER_SIZE;
8105   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8106     if (names != pubtype_table
8107         || p->die->die_offset != 0
8108         || !flag_eliminate_unused_debug_types)
8109       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8110
8111   size += DWARF_OFFSET_SIZE;
8112   return size;
8113 }
8114
8115 /* Return the size of the information in the .debug_aranges section.  */
8116
8117 static unsigned long
8118 size_of_aranges (void)
8119 {
8120   unsigned long size;
8121
8122   size = DWARF_ARANGES_HEADER_SIZE;
8123
8124   /* Count the address/length pair for this compilation unit.  */
8125   if (text_section_used)
8126     size += 2 * DWARF2_ADDR_SIZE;
8127   if (cold_text_section_used)
8128     size += 2 * DWARF2_ADDR_SIZE;
8129   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8130
8131   /* Count the two zero words used to terminated the address range table.  */
8132   size += 2 * DWARF2_ADDR_SIZE;
8133   return size;
8134 }
8135 \f
8136 /* Select the encoding of an attribute value.  */
8137
8138 static enum dwarf_form
8139 value_format (dw_attr_ref a)
8140 {
8141   switch (a->dw_attr_val.val_class)
8142     {
8143     case dw_val_class_addr:
8144       return DW_FORM_addr;
8145     case dw_val_class_range_list:
8146     case dw_val_class_offset:
8147     case dw_val_class_loc_list:
8148       switch (DWARF_OFFSET_SIZE)
8149         {
8150         case 4:
8151           return DW_FORM_data4;
8152         case 8:
8153           return DW_FORM_data8;
8154         default:
8155           gcc_unreachable ();
8156         }
8157     case dw_val_class_loc:
8158       switch (constant_size (size_of_locs (AT_loc (a))))
8159         {
8160         case 1:
8161           return DW_FORM_block1;
8162         case 2:
8163           return DW_FORM_block2;
8164         default:
8165           gcc_unreachable ();
8166         }
8167     case dw_val_class_const:
8168       return DW_FORM_sdata;
8169     case dw_val_class_unsigned_const:
8170       switch (constant_size (AT_unsigned (a)))
8171         {
8172         case 1:
8173           return DW_FORM_data1;
8174         case 2:
8175           return DW_FORM_data2;
8176         case 4:
8177           return DW_FORM_data4;
8178         case 8:
8179           return DW_FORM_data8;
8180         default:
8181           gcc_unreachable ();
8182         }
8183     case dw_val_class_long_long:
8184       return DW_FORM_block1;
8185     case dw_val_class_vec:
8186       switch (constant_size (a->dw_attr_val.v.val_vec.length
8187                              * a->dw_attr_val.v.val_vec.elt_size))
8188         {
8189         case 1:
8190           return DW_FORM_block1;
8191         case 2:
8192           return DW_FORM_block2;
8193         case 4:
8194           return DW_FORM_block4;
8195         default:
8196           gcc_unreachable ();
8197         }
8198     case dw_val_class_flag:
8199       return DW_FORM_flag;
8200     case dw_val_class_die_ref:
8201       if (AT_ref_external (a))
8202         return DW_FORM_ref_addr;
8203       else
8204         return DW_FORM_ref;
8205     case dw_val_class_fde_ref:
8206       return DW_FORM_data;
8207     case dw_val_class_lbl_id:
8208       return DW_FORM_addr;
8209     case dw_val_class_lineptr:
8210     case dw_val_class_macptr:
8211       return DW_FORM_data;
8212     case dw_val_class_str:
8213       return AT_string_form (a);
8214     case dw_val_class_file:
8215       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8216         {
8217         case 1:
8218           return DW_FORM_data1;
8219         case 2:
8220           return DW_FORM_data2;
8221         case 4:
8222           return DW_FORM_data4;
8223         default:
8224           gcc_unreachable ();
8225         }
8226
8227     default:
8228       gcc_unreachable ();
8229     }
8230 }
8231
8232 /* Output the encoding of an attribute value.  */
8233
8234 static void
8235 output_value_format (dw_attr_ref a)
8236 {
8237   enum dwarf_form form = value_format (a);
8238
8239   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8240 }
8241
8242 /* Output the .debug_abbrev section which defines the DIE abbreviation
8243    table.  */
8244
8245 static void
8246 output_abbrev_section (void)
8247 {
8248   unsigned long abbrev_id;
8249
8250   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8251     {
8252       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8253       unsigned ix;
8254       dw_attr_ref a_attr;
8255
8256       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8257       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8258                                    dwarf_tag_name (abbrev->die_tag));
8259
8260       if (abbrev->die_child != NULL)
8261         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8262       else
8263         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8264
8265       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8266            ix++)
8267         {
8268           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8269                                        dwarf_attr_name (a_attr->dw_attr));
8270           output_value_format (a_attr);
8271         }
8272
8273       dw2_asm_output_data (1, 0, NULL);
8274       dw2_asm_output_data (1, 0, NULL);
8275     }
8276
8277   /* Terminate the table.  */
8278   dw2_asm_output_data (1, 0, NULL);
8279 }
8280
8281 /* Output a symbol we can use to refer to this DIE from another CU.  */
8282
8283 static inline void
8284 output_die_symbol (dw_die_ref die)
8285 {
8286   char *sym = die->die_symbol;
8287
8288   if (sym == 0)
8289     return;
8290
8291   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8292     /* We make these global, not weak; if the target doesn't support
8293        .linkonce, it doesn't support combining the sections, so debugging
8294        will break.  */
8295     targetm.asm_out.globalize_label (asm_out_file, sym);
8296
8297   ASM_OUTPUT_LABEL (asm_out_file, sym);
8298 }
8299
8300 /* Return a new location list, given the begin and end range, and the
8301    expression. gensym tells us whether to generate a new internal symbol for
8302    this location list node, which is done for the head of the list only.  */
8303
8304 static inline dw_loc_list_ref
8305 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8306               const char *section, unsigned int gensym)
8307 {
8308   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8309
8310   retlist->begin = begin;
8311   retlist->end = end;
8312   retlist->expr = expr;
8313   retlist->section = section;
8314   if (gensym)
8315     retlist->ll_symbol = gen_internal_sym ("LLST");
8316
8317   return retlist;
8318 }
8319
8320 /* Add a location description expression to a location list.  */
8321
8322 static inline void
8323 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8324                            const char *begin, const char *end,
8325                            const char *section)
8326 {
8327   dw_loc_list_ref *d;
8328
8329   /* Find the end of the chain.  */
8330   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8331     ;
8332
8333   /* Add a new location list node to the list.  */
8334   *d = new_loc_list (descr, begin, end, section, 0);
8335 }
8336
8337 /* Output the location list given to us.  */
8338
8339 static void
8340 output_loc_list (dw_loc_list_ref list_head)
8341 {
8342   dw_loc_list_ref curr = list_head;
8343
8344   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8345
8346   /* Walk the location list, and output each range + expression.  */
8347   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8348     {
8349       unsigned long size;
8350       /* Don't output an entry that starts and ends at the same address.  */
8351       if (strcmp (curr->begin, curr->end) == 0)
8352         continue;
8353       if (!have_multiple_function_sections)
8354         {
8355           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8356                                 "Location list begin address (%s)",
8357                                 list_head->ll_symbol);
8358           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8359                                 "Location list end address (%s)",
8360                                 list_head->ll_symbol);
8361         }
8362       else
8363         {
8364           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8365                                "Location list begin address (%s)",
8366                                list_head->ll_symbol);
8367           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8368                                "Location list end address (%s)",
8369                                list_head->ll_symbol);
8370         }
8371       size = size_of_locs (curr->expr);
8372
8373       /* Output the block length for this list of location operations.  */
8374       gcc_assert (size <= 0xffff);
8375       dw2_asm_output_data (2, size, "%s", "Location expression size");
8376
8377       output_loc_sequence (curr->expr);
8378     }
8379
8380   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8381                        "Location list terminator begin (%s)",
8382                        list_head->ll_symbol);
8383   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8384                        "Location list terminator end (%s)",
8385                        list_head->ll_symbol);
8386 }
8387
8388 /* Output the DIE and its attributes.  Called recursively to generate
8389    the definitions of each child DIE.  */
8390
8391 static void
8392 output_die (dw_die_ref die)
8393 {
8394   dw_attr_ref a;
8395   dw_die_ref c;
8396   unsigned long size;
8397   unsigned ix;
8398
8399   /* If someone in another CU might refer to us, set up a symbol for
8400      them to point to.  */
8401   if (die->die_symbol)
8402     output_die_symbol (die);
8403
8404   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8405                                (unsigned long)die->die_offset,
8406                                dwarf_tag_name (die->die_tag));
8407
8408   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8409     {
8410       const char *name = dwarf_attr_name (a->dw_attr);
8411
8412       switch (AT_class (a))
8413         {
8414         case dw_val_class_addr:
8415           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8416           break;
8417
8418         case dw_val_class_offset:
8419           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8420                                "%s", name);
8421           break;
8422
8423         case dw_val_class_range_list:
8424           {
8425             char *p = strchr (ranges_section_label, '\0');
8426
8427             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8428                      a->dw_attr_val.v.val_offset);
8429             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8430                                    debug_ranges_section, "%s", name);
8431             *p = '\0';
8432           }
8433           break;
8434
8435         case dw_val_class_loc:
8436           size = size_of_locs (AT_loc (a));
8437
8438           /* Output the block length for this list of location operations.  */
8439           dw2_asm_output_data (constant_size (size), size, "%s", name);
8440
8441           output_loc_sequence (AT_loc (a));
8442           break;
8443
8444         case dw_val_class_const:
8445           /* ??? It would be slightly more efficient to use a scheme like is
8446              used for unsigned constants below, but gdb 4.x does not sign
8447              extend.  Gdb 5.x does sign extend.  */
8448           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8449           break;
8450
8451         case dw_val_class_unsigned_const:
8452           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8453                                AT_unsigned (a), "%s", name);
8454           break;
8455
8456         case dw_val_class_long_long:
8457           {
8458             unsigned HOST_WIDE_INT first, second;
8459
8460             dw2_asm_output_data (1,
8461                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8462                                  "%s", name);
8463
8464             if (WORDS_BIG_ENDIAN)
8465               {
8466                 first = a->dw_attr_val.v.val_long_long.hi;
8467                 second = a->dw_attr_val.v.val_long_long.low;
8468               }
8469             else
8470               {
8471                 first = a->dw_attr_val.v.val_long_long.low;
8472                 second = a->dw_attr_val.v.val_long_long.hi;
8473               }
8474
8475             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8476                                  first, "long long constant");
8477             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8478                                  second, NULL);
8479           }
8480           break;
8481
8482         case dw_val_class_vec:
8483           {
8484             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8485             unsigned int len = a->dw_attr_val.v.val_vec.length;
8486             unsigned int i;
8487             unsigned char *p;
8488
8489             dw2_asm_output_data (constant_size (len * elt_size),
8490                                  len * elt_size, "%s", name);
8491             if (elt_size > sizeof (HOST_WIDE_INT))
8492               {
8493                 elt_size /= 2;
8494                 len *= 2;
8495               }
8496             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8497                  i < len;
8498                  i++, p += elt_size)
8499               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8500                                    "fp or vector constant word %u", i);
8501             break;
8502           }
8503
8504         case dw_val_class_flag:
8505           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8506           break;
8507
8508         case dw_val_class_loc_list:
8509           {
8510             char *sym = AT_loc_list (a)->ll_symbol;
8511
8512             gcc_assert (sym);
8513             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8514                                    "%s", name);
8515           }
8516           break;
8517
8518         case dw_val_class_die_ref:
8519           if (AT_ref_external (a))
8520             {
8521               char *sym = AT_ref (a)->die_symbol;
8522
8523               gcc_assert (sym);
8524               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8525                                      "%s", name);
8526             }
8527           else
8528             {
8529               gcc_assert (AT_ref (a)->die_offset);
8530               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8531                                    "%s", name);
8532             }
8533           break;
8534
8535         case dw_val_class_fde_ref:
8536           {
8537             char l1[20];
8538
8539             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8540                                          a->dw_attr_val.v.val_fde_index * 2);
8541             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8542                                    "%s", name);
8543           }
8544           break;
8545
8546         case dw_val_class_lbl_id:
8547           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8548           break;
8549
8550         case dw_val_class_lineptr:
8551           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8552                                  debug_line_section, "%s", name);
8553           break;
8554
8555         case dw_val_class_macptr:
8556           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8557                                  debug_macinfo_section, "%s", name);
8558           break;
8559
8560         case dw_val_class_str:
8561           if (AT_string_form (a) == DW_FORM_strp)
8562             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8563                                    a->dw_attr_val.v.val_str->label,
8564                                    debug_str_section,
8565                                    "%s: \"%s\"", name, AT_string (a));
8566           else
8567             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8568           break;
8569
8570         case dw_val_class_file:
8571           {
8572             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8573
8574             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8575                                  a->dw_attr_val.v.val_file->filename);
8576             break;
8577           }
8578
8579         default:
8580           gcc_unreachable ();
8581         }
8582     }
8583
8584   FOR_EACH_CHILD (die, c, output_die (c));
8585
8586   /* Add null byte to terminate sibling list.  */
8587   if (die->die_child != NULL)
8588     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8589                          (unsigned long) die->die_offset);
8590 }
8591
8592 /* Output the compilation unit that appears at the beginning of the
8593    .debug_info section, and precedes the DIE descriptions.  */
8594
8595 static void
8596 output_compilation_unit_header (void)
8597 {
8598   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8599     dw2_asm_output_data (4, 0xffffffff,
8600       "Initial length escape value indicating 64-bit DWARF extension");
8601   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8602                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8603                        "Length of Compilation Unit Info");
8604   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8605   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8606                          debug_abbrev_section,
8607                          "Offset Into Abbrev. Section");
8608   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8609 }
8610
8611 /* Output the compilation unit DIE and its children.  */
8612
8613 static void
8614 output_comp_unit (dw_die_ref die, int output_if_empty)
8615 {
8616   const char *secname;
8617   char *oldsym, *tmp;
8618
8619   /* Unless we are outputting main CU, we may throw away empty ones.  */
8620   if (!output_if_empty && die->die_child == NULL)
8621     return;
8622
8623   /* Even if there are no children of this DIE, we must output the information
8624      about the compilation unit.  Otherwise, on an empty translation unit, we
8625      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8626      will then complain when examining the file.  First mark all the DIEs in
8627      this CU so we know which get local refs.  */
8628   mark_dies (die);
8629
8630   build_abbrev_table (die);
8631
8632   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8633   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8634   calc_die_sizes (die);
8635
8636   oldsym = die->die_symbol;
8637   if (oldsym)
8638     {
8639       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8640
8641       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8642       secname = tmp;
8643       die->die_symbol = NULL;
8644       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8645     }
8646   else
8647     switch_to_section (debug_info_section);
8648
8649   /* Output debugging information.  */
8650   output_compilation_unit_header ();
8651   output_die (die);
8652
8653   /* Leave the marks on the main CU, so we can check them in
8654      output_pubnames.  */
8655   if (oldsym)
8656     {
8657       unmark_dies (die);
8658       die->die_symbol = oldsym;
8659     }
8660 }
8661
8662 /* Return the DWARF2/3 pubname associated with a decl.  */
8663
8664 static const char *
8665 dwarf2_name (tree decl, int scope)
8666 {
8667   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8668 }
8669
8670 /* Add a new entry to .debug_pubnames if appropriate.  */
8671
8672 static void
8673 add_pubname_string (const char *str, dw_die_ref die)
8674 {
8675   pubname_entry e;
8676
8677   e.die = die;
8678   e.name = xstrdup (str);
8679   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8680 }
8681
8682 static void
8683 add_pubname (tree decl, dw_die_ref die)
8684 {
8685
8686   if (TREE_PUBLIC (decl))
8687     add_pubname_string (dwarf2_name (decl, 1), die);
8688 }
8689
8690 /* Add a new entry to .debug_pubtypes if appropriate.  */
8691
8692 static void
8693 add_pubtype (tree decl, dw_die_ref die)
8694 {
8695   pubname_entry e;
8696
8697   e.name = NULL;
8698   if ((TREE_PUBLIC (decl)
8699        || die->die_parent == comp_unit_die)
8700       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8701     {
8702       e.die = die;
8703       if (TYPE_P (decl))
8704         {
8705           if (TYPE_NAME (decl))
8706             {
8707               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8708                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8709               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8710                        && DECL_NAME (TYPE_NAME (decl)))
8711                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8712               else
8713                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8714             }
8715         }
8716       else
8717         e.name = xstrdup (dwarf2_name (decl, 1));
8718
8719       /* If we don't have a name for the type, there's no point in adding
8720          it to the table.  */
8721       if (e.name && e.name[0] != '\0')
8722         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8723     }
8724 }
8725
8726 /* Output the public names table used to speed up access to externally
8727    visible names; or the public types table used to find type definitions.  */
8728
8729 static void
8730 output_pubnames (VEC (pubname_entry, gc) * names)
8731 {
8732   unsigned i;
8733   unsigned long pubnames_length = size_of_pubnames (names);
8734   pubname_ref pub;
8735
8736   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8737     dw2_asm_output_data (4, 0xffffffff,
8738       "Initial length escape value indicating 64-bit DWARF extension");
8739   if (names == pubname_table)
8740     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8741                          "Length of Public Names Info");
8742   else
8743     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8744                          "Length of Public Type Names Info");
8745   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8746   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8747                          debug_info_section,
8748                          "Offset of Compilation Unit Info");
8749   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8750                        "Compilation Unit Length");
8751
8752   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8753     {
8754       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8755       if (names == pubname_table)
8756         gcc_assert (pub->die->die_mark);
8757
8758       if (names != pubtype_table
8759           || pub->die->die_offset != 0
8760           || !flag_eliminate_unused_debug_types)
8761         {
8762           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8763                                "DIE offset");
8764
8765           dw2_asm_output_nstring (pub->name, -1, "external name");
8766         }
8767     }
8768
8769   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8770 }
8771
8772 /* Add a new entry to .debug_aranges if appropriate.  */
8773
8774 static void
8775 add_arange (tree decl, dw_die_ref die)
8776 {
8777   if (! DECL_SECTION_NAME (decl))
8778     return;
8779
8780   if (arange_table_in_use == arange_table_allocated)
8781     {
8782       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8783       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8784                                     arange_table_allocated);
8785       memset (arange_table + arange_table_in_use, 0,
8786               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8787     }
8788
8789   arange_table[arange_table_in_use++] = die;
8790 }
8791
8792 /* Output the information that goes into the .debug_aranges table.
8793    Namely, define the beginning and ending address range of the
8794    text section generated for this compilation unit.  */
8795
8796 static void
8797 output_aranges (void)
8798 {
8799   unsigned i;
8800   unsigned long aranges_length = size_of_aranges ();
8801
8802   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8803     dw2_asm_output_data (4, 0xffffffff,
8804       "Initial length escape value indicating 64-bit DWARF extension");
8805   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8806                        "Length of Address Ranges Info");
8807   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8808   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8809                          debug_info_section,
8810                          "Offset of Compilation Unit Info");
8811   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8812   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8813
8814   /* We need to align to twice the pointer size here.  */
8815   if (DWARF_ARANGES_PAD_SIZE)
8816     {
8817       /* Pad using a 2 byte words so that padding is correct for any
8818          pointer size.  */
8819       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8820                            2 * DWARF2_ADDR_SIZE);
8821       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8822         dw2_asm_output_data (2, 0, NULL);
8823     }
8824
8825   /* It is necessary not to output these entries if the sections were
8826      not used; if the sections were not used, the length will be 0 and
8827      the address may end up as 0 if the section is discarded by ld
8828      --gc-sections, leaving an invalid (0, 0) entry that can be
8829      confused with the terminator.  */
8830   if (text_section_used)
8831     {
8832       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8833       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8834                             text_section_label, "Length");
8835     }
8836   if (cold_text_section_used)
8837     {
8838       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8839                            "Address");
8840       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8841                             cold_text_section_label, "Length");
8842     }
8843
8844   for (i = 0; i < arange_table_in_use; i++)
8845     {
8846       dw_die_ref die = arange_table[i];
8847
8848       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8849       gcc_assert (die->die_mark);
8850
8851       if (die->die_tag == DW_TAG_subprogram)
8852         {
8853           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8854                                "Address");
8855           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8856                                 get_AT_low_pc (die), "Length");
8857         }
8858       else
8859         {
8860           /* A static variable; extract the symbol from DW_AT_location.
8861              Note that this code isn't currently hit, as we only emit
8862              aranges for functions (jason 9/23/99).  */
8863           dw_attr_ref a = get_AT (die, DW_AT_location);
8864           dw_loc_descr_ref loc;
8865
8866           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8867
8868           loc = AT_loc (a);
8869           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8870
8871           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8872                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8873           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8874                                get_AT_unsigned (die, DW_AT_byte_size),
8875                                "Length");
8876         }
8877     }
8878
8879   /* Output the terminator words.  */
8880   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8881   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8882 }
8883
8884 /* Add a new entry to .debug_ranges.  Return the offset at which it
8885    was placed.  */
8886
8887 static unsigned int
8888 add_ranges_num (int num)
8889 {
8890   unsigned int in_use = ranges_table_in_use;
8891
8892   if (in_use == ranges_table_allocated)
8893     {
8894       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8895       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8896                                     ranges_table_allocated);
8897       memset (ranges_table + ranges_table_in_use, 0,
8898               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8899     }
8900
8901   ranges_table[in_use].num = num;
8902   ranges_table_in_use = in_use + 1;
8903
8904   return in_use * 2 * DWARF2_ADDR_SIZE;
8905 }
8906
8907 /* Add a new entry to .debug_ranges corresponding to a block, or a
8908    range terminator if BLOCK is NULL.  */
8909
8910 static unsigned int
8911 add_ranges (const_tree block)
8912 {
8913   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8914 }
8915
8916 /* Add a new entry to .debug_ranges corresponding to a pair of
8917    labels.  */
8918
8919 static unsigned int
8920 add_ranges_by_labels (const char *begin, const char *end)
8921 {
8922   unsigned int in_use = ranges_by_label_in_use;
8923
8924   if (in_use == ranges_by_label_allocated)
8925     {
8926       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8927       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8928                                        ranges_by_label,
8929                                        ranges_by_label_allocated);
8930       memset (ranges_by_label + ranges_by_label_in_use, 0,
8931               RANGES_TABLE_INCREMENT
8932               * sizeof (struct dw_ranges_by_label_struct));
8933     }
8934
8935   ranges_by_label[in_use].begin = begin;
8936   ranges_by_label[in_use].end = end;
8937   ranges_by_label_in_use = in_use + 1;
8938
8939   return add_ranges_num (-(int)in_use - 1);
8940 }
8941
8942 static void
8943 output_ranges (void)
8944 {
8945   unsigned i;
8946   static const char *const start_fmt = "Offset 0x%x";
8947   const char *fmt = start_fmt;
8948
8949   for (i = 0; i < ranges_table_in_use; i++)
8950     {
8951       int block_num = ranges_table[i].num;
8952
8953       if (block_num > 0)
8954         {
8955           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8956           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8957
8958           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8959           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8960
8961           /* If all code is in the text section, then the compilation
8962              unit base address defaults to DW_AT_low_pc, which is the
8963              base of the text section.  */
8964           if (!have_multiple_function_sections)
8965             {
8966               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8967                                     text_section_label,
8968                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8969               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8970                                     text_section_label, NULL);
8971             }
8972
8973           /* Otherwise, the compilation unit base address is zero,
8974              which allows us to use absolute addresses, and not worry
8975              about whether the target supports cross-section
8976              arithmetic.  */
8977           else
8978             {
8979               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8980                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8981               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8982             }
8983
8984           fmt = NULL;
8985         }
8986
8987       /* Negative block_num stands for an index into ranges_by_label.  */
8988       else if (block_num < 0)
8989         {
8990           int lab_idx = - block_num - 1;
8991
8992           if (!have_multiple_function_sections)
8993             {
8994               gcc_unreachable ();
8995 #if 0
8996               /* If we ever use add_ranges_by_labels () for a single
8997                  function section, all we have to do is to take out
8998                  the #if 0 above.  */
8999               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9000                                     ranges_by_label[lab_idx].begin,
9001                                     text_section_label,
9002                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9003               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9004                                     ranges_by_label[lab_idx].end,
9005                                     text_section_label, NULL);
9006 #endif
9007             }
9008           else
9009             {
9010               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9011                                    ranges_by_label[lab_idx].begin,
9012                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9013               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9014                                    ranges_by_label[lab_idx].end,
9015                                    NULL);
9016             }
9017         }
9018       else
9019         {
9020           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9021           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9022           fmt = start_fmt;
9023         }
9024     }
9025 }
9026
9027 /* Data structure containing information about input files.  */
9028 struct file_info
9029 {
9030   const char *path;     /* Complete file name.  */
9031   const char *fname;    /* File name part.  */
9032   int length;           /* Length of entire string.  */
9033   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9034   int dir_idx;          /* Index in directory table.  */
9035 };
9036
9037 /* Data structure containing information about directories with source
9038    files.  */
9039 struct dir_info
9040 {
9041   const char *path;     /* Path including directory name.  */
9042   int length;           /* Path length.  */
9043   int prefix;           /* Index of directory entry which is a prefix.  */
9044   int count;            /* Number of files in this directory.  */
9045   int dir_idx;          /* Index of directory used as base.  */
9046 };
9047
9048 /* Callback function for file_info comparison.  We sort by looking at
9049    the directories in the path.  */
9050
9051 static int
9052 file_info_cmp (const void *p1, const void *p2)
9053 {
9054   const struct file_info *const s1 = (const struct file_info *) p1;
9055   const struct file_info *const s2 = (const struct file_info *) p2;
9056   const unsigned char *cp1;
9057   const unsigned char *cp2;
9058
9059   /* Take care of file names without directories.  We need to make sure that
9060      we return consistent values to qsort since some will get confused if
9061      we return the same value when identical operands are passed in opposite
9062      orders.  So if neither has a directory, return 0 and otherwise return
9063      1 or -1 depending on which one has the directory.  */
9064   if ((s1->path == s1->fname || s2->path == s2->fname))
9065     return (s2->path == s2->fname) - (s1->path == s1->fname);
9066
9067   cp1 = (const unsigned char *) s1->path;
9068   cp2 = (const unsigned char *) s2->path;
9069
9070   while (1)
9071     {
9072       ++cp1;
9073       ++cp2;
9074       /* Reached the end of the first path?  If so, handle like above.  */
9075       if ((cp1 == (const unsigned char *) s1->fname)
9076           || (cp2 == (const unsigned char *) s2->fname))
9077         return ((cp2 == (const unsigned char *) s2->fname)
9078                 - (cp1 == (const unsigned char *) s1->fname));
9079
9080       /* Character of current path component the same?  */
9081       else if (*cp1 != *cp2)
9082         return *cp1 - *cp2;
9083     }
9084 }
9085
9086 struct file_name_acquire_data
9087 {
9088   struct file_info *files;
9089   int used_files;
9090   int max_files;
9091 };
9092
9093 /* Traversal function for the hash table.  */
9094
9095 static int
9096 file_name_acquire (void ** slot, void *data)
9097 {
9098   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9099   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9100   struct file_info *fi;
9101   const char *f;
9102
9103   gcc_assert (fnad->max_files >= d->emitted_number);
9104
9105   if (! d->emitted_number)
9106     return 1;
9107
9108   gcc_assert (fnad->max_files != fnad->used_files);
9109
9110   fi = fnad->files + fnad->used_files++;
9111
9112   /* Skip all leading "./".  */
9113   f = d->filename;
9114   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9115     f += 2;
9116
9117   /* Create a new array entry.  */
9118   fi->path = f;
9119   fi->length = strlen (f);
9120   fi->file_idx = d;
9121
9122   /* Search for the file name part.  */
9123   f = strrchr (f, DIR_SEPARATOR);
9124 #if defined (DIR_SEPARATOR_2)
9125   {
9126     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9127
9128     if (g != NULL)
9129       {
9130         if (f == NULL || f < g)
9131           f = g;
9132       }
9133   }
9134 #endif
9135
9136   fi->fname = f == NULL ? fi->path : f + 1;
9137   return 1;
9138 }
9139
9140 /* Output the directory table and the file name table.  We try to minimize
9141    the total amount of memory needed.  A heuristic is used to avoid large
9142    slowdowns with many input files.  */
9143
9144 static void
9145 output_file_names (void)
9146 {
9147   struct file_name_acquire_data fnad;
9148   int numfiles;
9149   struct file_info *files;
9150   struct dir_info *dirs;
9151   int *saved;
9152   int *savehere;
9153   int *backmap;
9154   int ndirs;
9155   int idx_offset;
9156   int i;
9157   int idx;
9158
9159   if (!last_emitted_file)
9160     {
9161       dw2_asm_output_data (1, 0, "End directory table");
9162       dw2_asm_output_data (1, 0, "End file name table");
9163       return;
9164     }
9165
9166   numfiles = last_emitted_file->emitted_number;
9167
9168   /* Allocate the various arrays we need.  */
9169   files = XALLOCAVEC (struct file_info, numfiles);
9170   dirs = XALLOCAVEC (struct dir_info, numfiles);
9171
9172   fnad.files = files;
9173   fnad.used_files = 0;
9174   fnad.max_files = numfiles;
9175   htab_traverse (file_table, file_name_acquire, &fnad);
9176   gcc_assert (fnad.used_files == fnad.max_files);
9177
9178   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9179
9180   /* Find all the different directories used.  */
9181   dirs[0].path = files[0].path;
9182   dirs[0].length = files[0].fname - files[0].path;
9183   dirs[0].prefix = -1;
9184   dirs[0].count = 1;
9185   dirs[0].dir_idx = 0;
9186   files[0].dir_idx = 0;
9187   ndirs = 1;
9188
9189   for (i = 1; i < numfiles; i++)
9190     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9191         && memcmp (dirs[ndirs - 1].path, files[i].path,
9192                    dirs[ndirs - 1].length) == 0)
9193       {
9194         /* Same directory as last entry.  */
9195         files[i].dir_idx = ndirs - 1;
9196         ++dirs[ndirs - 1].count;
9197       }
9198     else
9199       {
9200         int j;
9201
9202         /* This is a new directory.  */
9203         dirs[ndirs].path = files[i].path;
9204         dirs[ndirs].length = files[i].fname - files[i].path;
9205         dirs[ndirs].count = 1;
9206         dirs[ndirs].dir_idx = ndirs;
9207         files[i].dir_idx = ndirs;
9208
9209         /* Search for a prefix.  */
9210         dirs[ndirs].prefix = -1;
9211         for (j = 0; j < ndirs; j++)
9212           if (dirs[j].length < dirs[ndirs].length
9213               && dirs[j].length > 1
9214               && (dirs[ndirs].prefix == -1
9215                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9216               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9217             dirs[ndirs].prefix = j;
9218
9219         ++ndirs;
9220       }
9221
9222   /* Now to the actual work.  We have to find a subset of the directories which
9223      allow expressing the file name using references to the directory table
9224      with the least amount of characters.  We do not do an exhaustive search
9225      where we would have to check out every combination of every single
9226      possible prefix.  Instead we use a heuristic which provides nearly optimal
9227      results in most cases and never is much off.  */
9228   saved = XALLOCAVEC (int, ndirs);
9229   savehere = XALLOCAVEC (int, ndirs);
9230
9231   memset (saved, '\0', ndirs * sizeof (saved[0]));
9232   for (i = 0; i < ndirs; i++)
9233     {
9234       int j;
9235       int total;
9236
9237       /* We can always save some space for the current directory.  But this
9238          does not mean it will be enough to justify adding the directory.  */
9239       savehere[i] = dirs[i].length;
9240       total = (savehere[i] - saved[i]) * dirs[i].count;
9241
9242       for (j = i + 1; j < ndirs; j++)
9243         {
9244           savehere[j] = 0;
9245           if (saved[j] < dirs[i].length)
9246             {
9247               /* Determine whether the dirs[i] path is a prefix of the
9248                  dirs[j] path.  */
9249               int k;
9250
9251               k = dirs[j].prefix;
9252               while (k != -1 && k != (int) i)
9253                 k = dirs[k].prefix;
9254
9255               if (k == (int) i)
9256                 {
9257                   /* Yes it is.  We can possibly save some memory by
9258                      writing the filenames in dirs[j] relative to
9259                      dirs[i].  */
9260                   savehere[j] = dirs[i].length;
9261                   total += (savehere[j] - saved[j]) * dirs[j].count;
9262                 }
9263             }
9264         }
9265
9266       /* Check whether we can save enough to justify adding the dirs[i]
9267          directory.  */
9268       if (total > dirs[i].length + 1)
9269         {
9270           /* It's worthwhile adding.  */
9271           for (j = i; j < ndirs; j++)
9272             if (savehere[j] > 0)
9273               {
9274                 /* Remember how much we saved for this directory so far.  */
9275                 saved[j] = savehere[j];
9276
9277                 /* Remember the prefix directory.  */
9278                 dirs[j].dir_idx = i;
9279               }
9280         }
9281     }
9282
9283   /* Emit the directory name table.  */
9284   idx = 1;
9285   idx_offset = dirs[0].length > 0 ? 1 : 0;
9286   for (i = 1 - idx_offset; i < ndirs; i++)
9287     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
9288                             "Directory Entry: 0x%x", i + idx_offset);
9289
9290   dw2_asm_output_data (1, 0, "End directory table");
9291
9292   /* We have to emit them in the order of emitted_number since that's
9293      used in the debug info generation.  To do this efficiently we
9294      generate a back-mapping of the indices first.  */
9295   backmap = XALLOCAVEC (int, numfiles);
9296   for (i = 0; i < numfiles; i++)
9297     backmap[files[i].file_idx->emitted_number - 1] = i;
9298
9299   /* Now write all the file names.  */
9300   for (i = 0; i < numfiles; i++)
9301     {
9302       int file_idx = backmap[i];
9303       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9304
9305       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9306                               "File Entry: 0x%x", (unsigned) i + 1);
9307
9308       /* Include directory index.  */
9309       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9310
9311       /* Modification time.  */
9312       dw2_asm_output_data_uleb128 (0, NULL);
9313
9314       /* File length in bytes.  */
9315       dw2_asm_output_data_uleb128 (0, NULL);
9316     }
9317
9318   dw2_asm_output_data (1, 0, "End file name table");
9319 }
9320
9321
9322 /* Output the source line number correspondence information.  This
9323    information goes into the .debug_line section.  */
9324
9325 static void
9326 output_line_info (void)
9327 {
9328   char l1[20], l2[20], p1[20], p2[20];
9329   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9330   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9331   unsigned opc;
9332   unsigned n_op_args;
9333   unsigned long lt_index;
9334   unsigned long current_line;
9335   long line_offset;
9336   long line_delta;
9337   unsigned long current_file;
9338   unsigned long function;
9339
9340   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9341   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9342   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9343   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9344
9345   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9346     dw2_asm_output_data (4, 0xffffffff,
9347       "Initial length escape value indicating 64-bit DWARF extension");
9348   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9349                         "Length of Source Line Info");
9350   ASM_OUTPUT_LABEL (asm_out_file, l1);
9351
9352   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
9353   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9354   ASM_OUTPUT_LABEL (asm_out_file, p1);
9355
9356   /* Define the architecture-dependent minimum instruction length (in
9357    bytes).  In this implementation of DWARF, this field is used for
9358    information purposes only.  Since GCC generates assembly language,
9359    we have no a priori knowledge of how many instruction bytes are
9360    generated for each source line, and therefore can use only the
9361    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9362    commands.  Accordingly, we fix this as `1', which is "correct
9363    enough" for all architectures, and don't let the target override.  */
9364   dw2_asm_output_data (1, 1,
9365                        "Minimum Instruction Length");
9366
9367   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9368                        "Default is_stmt_start flag");
9369   dw2_asm_output_data (1, DWARF_LINE_BASE,
9370                        "Line Base Value (Special Opcodes)");
9371   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9372                        "Line Range Value (Special Opcodes)");
9373   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9374                        "Special Opcode Base");
9375
9376   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9377     {
9378       switch (opc)
9379         {
9380         case DW_LNS_advance_pc:
9381         case DW_LNS_advance_line:
9382         case DW_LNS_set_file:
9383         case DW_LNS_set_column:
9384         case DW_LNS_fixed_advance_pc:
9385           n_op_args = 1;
9386           break;
9387         default:
9388           n_op_args = 0;
9389           break;
9390         }
9391
9392       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9393                            opc, n_op_args);
9394     }
9395
9396   /* Write out the information about the files we use.  */
9397   output_file_names ();
9398   ASM_OUTPUT_LABEL (asm_out_file, p2);
9399
9400   /* We used to set the address register to the first location in the text
9401      section here, but that didn't accomplish anything since we already
9402      have a line note for the opening brace of the first function.  */
9403
9404   /* Generate the line number to PC correspondence table, encoded as
9405      a series of state machine operations.  */
9406   current_file = 1;
9407   current_line = 1;
9408
9409   if (cfun && in_cold_section_p)
9410     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9411   else
9412     strcpy (prev_line_label, text_section_label);
9413   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9414     {
9415       dw_line_info_ref line_info = &line_info_table[lt_index];
9416
9417 #if 0
9418       /* Disable this optimization for now; GDB wants to see two line notes
9419          at the beginning of a function so it can find the end of the
9420          prologue.  */
9421
9422       /* Don't emit anything for redundant notes.  Just updating the
9423          address doesn't accomplish anything, because we already assume
9424          that anything after the last address is this line.  */
9425       if (line_info->dw_line_num == current_line
9426           && line_info->dw_file_num == current_file)
9427         continue;
9428 #endif
9429
9430       /* Emit debug info for the address of the current line.
9431
9432          Unfortunately, we have little choice here currently, and must always
9433          use the most general form.  GCC does not know the address delta
9434          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9435          attributes which will give an upper bound on the address range.  We
9436          could perhaps use length attributes to determine when it is safe to
9437          use DW_LNS_fixed_advance_pc.  */
9438
9439       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9440       if (0)
9441         {
9442           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9443           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9444                                "DW_LNS_fixed_advance_pc");
9445           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9446         }
9447       else
9448         {
9449           /* This can handle any delta.  This takes
9450              4+DWARF2_ADDR_SIZE bytes.  */
9451           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9452           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9453           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9454           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9455         }
9456
9457       strcpy (prev_line_label, line_label);
9458
9459       /* Emit debug info for the source file of the current line, if
9460          different from the previous line.  */
9461       if (line_info->dw_file_num != current_file)
9462         {
9463           current_file = line_info->dw_file_num;
9464           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9465           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9466         }
9467
9468       /* Emit debug info for the current line number, choosing the encoding
9469          that uses the least amount of space.  */
9470       if (line_info->dw_line_num != current_line)
9471         {
9472           line_offset = line_info->dw_line_num - current_line;
9473           line_delta = line_offset - DWARF_LINE_BASE;
9474           current_line = line_info->dw_line_num;
9475           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9476             /* This can handle deltas from -10 to 234, using the current
9477                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9478                takes 1 byte.  */
9479             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9480                                  "line %lu", current_line);
9481           else
9482             {
9483               /* This can handle any delta.  This takes at least 4 bytes,
9484                  depending on the value being encoded.  */
9485               dw2_asm_output_data (1, DW_LNS_advance_line,
9486                                    "advance to line %lu", current_line);
9487               dw2_asm_output_data_sleb128 (line_offset, NULL);
9488               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9489             }
9490         }
9491       else
9492         /* We still need to start a new row, so output a copy insn.  */
9493         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9494     }
9495
9496   /* Emit debug info for the address of the end of the function.  */
9497   if (0)
9498     {
9499       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9500                            "DW_LNS_fixed_advance_pc");
9501       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9502     }
9503   else
9504     {
9505       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9506       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9507       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9508       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9509     }
9510
9511   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9512   dw2_asm_output_data_uleb128 (1, NULL);
9513   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9514
9515   function = 0;
9516   current_file = 1;
9517   current_line = 1;
9518   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9519     {
9520       dw_separate_line_info_ref line_info
9521         = &separate_line_info_table[lt_index];
9522
9523 #if 0
9524       /* Don't emit anything for redundant notes.  */
9525       if (line_info->dw_line_num == current_line
9526           && line_info->dw_file_num == current_file
9527           && line_info->function == function)
9528         goto cont;
9529 #endif
9530
9531       /* Emit debug info for the address of the current line.  If this is
9532          a new function, or the first line of a function, then we need
9533          to handle it differently.  */
9534       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9535                                    lt_index);
9536       if (function != line_info->function)
9537         {
9538           function = line_info->function;
9539
9540           /* Set the address register to the first line in the function.  */
9541           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9542           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9543           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9544           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9545         }
9546       else
9547         {
9548           /* ??? See the DW_LNS_advance_pc comment above.  */
9549           if (0)
9550             {
9551               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9552                                    "DW_LNS_fixed_advance_pc");
9553               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9554             }
9555           else
9556             {
9557               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9558               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9559               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9560               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9561             }
9562         }
9563
9564       strcpy (prev_line_label, line_label);
9565
9566       /* Emit debug info for the source file of the current line, if
9567          different from the previous line.  */
9568       if (line_info->dw_file_num != current_file)
9569         {
9570           current_file = line_info->dw_file_num;
9571           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9572           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9573         }
9574
9575       /* Emit debug info for the current line number, choosing the encoding
9576          that uses the least amount of space.  */
9577       if (line_info->dw_line_num != current_line)
9578         {
9579           line_offset = line_info->dw_line_num - current_line;
9580           line_delta = line_offset - DWARF_LINE_BASE;
9581           current_line = line_info->dw_line_num;
9582           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9583             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9584                                  "line %lu", current_line);
9585           else
9586             {
9587               dw2_asm_output_data (1, DW_LNS_advance_line,
9588                                    "advance to line %lu", current_line);
9589               dw2_asm_output_data_sleb128 (line_offset, NULL);
9590               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9591             }
9592         }
9593       else
9594         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9595
9596 #if 0
9597     cont:
9598 #endif
9599
9600       lt_index++;
9601
9602       /* If we're done with a function, end its sequence.  */
9603       if (lt_index == separate_line_info_table_in_use
9604           || separate_line_info_table[lt_index].function != function)
9605         {
9606           current_file = 1;
9607           current_line = 1;
9608
9609           /* Emit debug info for the address of the end of the function.  */
9610           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9611           if (0)
9612             {
9613               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9614                                    "DW_LNS_fixed_advance_pc");
9615               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9616             }
9617           else
9618             {
9619               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9620               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9621               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9622               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9623             }
9624
9625           /* Output the marker for the end of this sequence.  */
9626           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9627           dw2_asm_output_data_uleb128 (1, NULL);
9628           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9629         }
9630     }
9631
9632   /* Output the marker for the end of the line number info.  */
9633   ASM_OUTPUT_LABEL (asm_out_file, l2);
9634 }
9635 \f
9636 /* Given a pointer to a tree node for some base type, return a pointer to
9637    a DIE that describes the given type.
9638
9639    This routine must only be called for GCC type nodes that correspond to
9640    Dwarf base (fundamental) types.  */
9641
9642 static dw_die_ref
9643 base_type_die (tree type)
9644 {
9645   dw_die_ref base_type_result;
9646   enum dwarf_type encoding;
9647
9648   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9649     return 0;
9650
9651   /* If this is a subtype that should not be emitted as a subrange type,
9652      use the base type.  See subrange_type_for_debug_p.  */
9653   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9654     type = TREE_TYPE (type);
9655
9656   switch (TREE_CODE (type))
9657     {
9658     case INTEGER_TYPE:
9659       if (TYPE_STRING_FLAG (type))
9660         {
9661           if (TYPE_UNSIGNED (type))
9662             encoding = DW_ATE_unsigned_char;
9663           else
9664             encoding = DW_ATE_signed_char;
9665         }
9666       else if (TYPE_UNSIGNED (type))
9667         encoding = DW_ATE_unsigned;
9668       else
9669         encoding = DW_ATE_signed;
9670       break;
9671
9672     case REAL_TYPE:
9673       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9674         encoding = DW_ATE_decimal_float;
9675       else
9676         encoding = DW_ATE_float;
9677       break;
9678
9679     case FIXED_POINT_TYPE:
9680       if (TYPE_UNSIGNED (type))
9681         encoding = DW_ATE_unsigned_fixed;
9682       else
9683         encoding = DW_ATE_signed_fixed;
9684       break;
9685
9686       /* Dwarf2 doesn't know anything about complex ints, so use
9687          a user defined type for it.  */
9688     case COMPLEX_TYPE:
9689       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9690         encoding = DW_ATE_complex_float;
9691       else
9692         encoding = DW_ATE_lo_user;
9693       break;
9694
9695     case BOOLEAN_TYPE:
9696       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9697       encoding = DW_ATE_boolean;
9698       break;
9699
9700     default:
9701       /* No other TREE_CODEs are Dwarf fundamental types.  */
9702       gcc_unreachable ();
9703     }
9704
9705   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9706
9707   /* This probably indicates a bug.  */
9708   if (! TYPE_NAME (type))
9709     add_name_attribute (base_type_result, "__unknown__");
9710
9711   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9712                    int_size_in_bytes (type));
9713   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9714
9715   return base_type_result;
9716 }
9717
9718 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9719    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9720
9721 static inline int
9722 is_base_type (tree type)
9723 {
9724   switch (TREE_CODE (type))
9725     {
9726     case ERROR_MARK:
9727     case VOID_TYPE:
9728     case INTEGER_TYPE:
9729     case REAL_TYPE:
9730     case FIXED_POINT_TYPE:
9731     case COMPLEX_TYPE:
9732     case BOOLEAN_TYPE:
9733       return 1;
9734
9735     case ARRAY_TYPE:
9736     case RECORD_TYPE:
9737     case UNION_TYPE:
9738     case QUAL_UNION_TYPE:
9739     case ENUMERAL_TYPE:
9740     case FUNCTION_TYPE:
9741     case METHOD_TYPE:
9742     case POINTER_TYPE:
9743     case REFERENCE_TYPE:
9744     case OFFSET_TYPE:
9745     case LANG_TYPE:
9746     case VECTOR_TYPE:
9747       return 0;
9748
9749     default:
9750       gcc_unreachable ();
9751     }
9752
9753   return 0;
9754 }
9755
9756 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9757    node, return the size in bits for the type if it is a constant, or else
9758    return the alignment for the type if the type's size is not constant, or
9759    else return BITS_PER_WORD if the type actually turns out to be an
9760    ERROR_MARK node.  */
9761
9762 static inline unsigned HOST_WIDE_INT
9763 simple_type_size_in_bits (const_tree type)
9764 {
9765   if (TREE_CODE (type) == ERROR_MARK)
9766     return BITS_PER_WORD;
9767   else if (TYPE_SIZE (type) == NULL_TREE)
9768     return 0;
9769   else if (host_integerp (TYPE_SIZE (type), 1))
9770     return tree_low_cst (TYPE_SIZE (type), 1);
9771   else
9772     return TYPE_ALIGN (type);
9773 }
9774
9775 /*  Given a pointer to a tree node for a subrange type, return a pointer
9776     to a DIE that describes the given type.  */
9777
9778 static dw_die_ref
9779 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9780 {
9781   dw_die_ref subrange_die;
9782   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9783
9784   if (context_die == NULL)
9785     context_die = comp_unit_die;
9786
9787   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9788
9789   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9790     {
9791       /* The size of the subrange type and its base type do not match,
9792          so we need to generate a size attribute for the subrange type.  */
9793       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9794     }
9795
9796   if (low)
9797     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9798   if (high)
9799     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9800
9801   return subrange_die;
9802 }
9803
9804 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9805    entry that chains various modifiers in front of the given type.  */
9806
9807 static dw_die_ref
9808 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9809                    dw_die_ref context_die)
9810 {
9811   enum tree_code code = TREE_CODE (type);
9812   dw_die_ref mod_type_die;
9813   dw_die_ref sub_die = NULL;
9814   tree item_type = NULL;
9815   tree qualified_type;
9816   tree name, low, high;
9817
9818   if (code == ERROR_MARK)
9819     return NULL;
9820
9821   /* See if we already have the appropriately qualified variant of
9822      this type.  */
9823   qualified_type
9824     = get_qualified_type (type,
9825                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9826                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9827
9828   /* If we do, then we can just use its DIE, if it exists.  */
9829   if (qualified_type)
9830     {
9831       mod_type_die = lookup_type_die (qualified_type);
9832       if (mod_type_die)
9833         return mod_type_die;
9834     }
9835
9836   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9837
9838   /* Handle C typedef types.  */
9839   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9840     {
9841       tree dtype = TREE_TYPE (name);
9842
9843       if (qualified_type == dtype)
9844         {
9845           /* For a named type, use the typedef.  */
9846           gen_type_die (qualified_type, context_die);
9847           return lookup_type_die (qualified_type);
9848         }
9849       else if (is_const_type < TYPE_READONLY (dtype)
9850                || is_volatile_type < TYPE_VOLATILE (dtype)
9851                || (is_const_type <= TYPE_READONLY (dtype)
9852                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9853                    && DECL_ORIGINAL_TYPE (name) != type))
9854         /* cv-unqualified version of named type.  Just use the unnamed
9855            type to which it refers.  */
9856         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9857                                   is_const_type, is_volatile_type,
9858                                   context_die);
9859       /* Else cv-qualified version of named type; fall through.  */
9860     }
9861
9862   if (is_const_type)
9863     {
9864       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9865       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9866     }
9867   else if (is_volatile_type)
9868     {
9869       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9870       sub_die = modified_type_die (type, 0, 0, context_die);
9871     }
9872   else if (code == POINTER_TYPE)
9873     {
9874       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9875       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9876                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9877       item_type = TREE_TYPE (type);
9878     }
9879   else if (code == REFERENCE_TYPE)
9880     {
9881       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9882       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9883                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9884       item_type = TREE_TYPE (type);
9885     }
9886   else if (code == INTEGER_TYPE
9887            && TREE_TYPE (type) != NULL_TREE
9888            && subrange_type_for_debug_p (type, &low, &high))
9889     {
9890       mod_type_die = subrange_type_die (type, low, high, context_die);
9891       item_type = TREE_TYPE (type);
9892     }
9893   else if (is_base_type (type))
9894     mod_type_die = base_type_die (type);
9895   else
9896     {
9897       gen_type_die (type, context_die);
9898
9899       /* We have to get the type_main_variant here (and pass that to the
9900          `lookup_type_die' routine) because the ..._TYPE node we have
9901          might simply be a *copy* of some original type node (where the
9902          copy was created to help us keep track of typedef names) and
9903          that copy might have a different TYPE_UID from the original
9904          ..._TYPE node.  */
9905       if (TREE_CODE (type) != VECTOR_TYPE)
9906         return lookup_type_die (type_main_variant (type));
9907       else
9908         /* Vectors have the debugging information in the type,
9909            not the main variant.  */
9910         return lookup_type_die (type);
9911     }
9912
9913   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9914      don't output a DW_TAG_typedef, since there isn't one in the
9915      user's program; just attach a DW_AT_name to the type.  */
9916   if (name
9917       && (TREE_CODE (name) != TYPE_DECL
9918           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9919     {
9920       if (TREE_CODE (name) == TYPE_DECL)
9921         /* Could just call add_name_and_src_coords_attributes here,
9922            but since this is a builtin type it doesn't have any
9923            useful source coordinates anyway.  */
9924         name = DECL_NAME (name);
9925       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9926     }
9927
9928   if (qualified_type)
9929     equate_type_number_to_die (qualified_type, mod_type_die);
9930
9931   if (item_type)
9932     /* We must do this after the equate_type_number_to_die call, in case
9933        this is a recursive type.  This ensures that the modified_type_die
9934        recursion will terminate even if the type is recursive.  Recursive
9935        types are possible in Ada.  */
9936     sub_die = modified_type_die (item_type,
9937                                  TYPE_READONLY (item_type),
9938                                  TYPE_VOLATILE (item_type),
9939                                  context_die);
9940
9941   if (sub_die != NULL)
9942     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9943
9944   return mod_type_die;
9945 }
9946
9947 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9948    an enumerated type.  */
9949
9950 static inline int
9951 type_is_enum (const_tree type)
9952 {
9953   return TREE_CODE (type) == ENUMERAL_TYPE;
9954 }
9955
9956 /* Return the DBX register number described by a given RTL node.  */
9957
9958 static unsigned int
9959 dbx_reg_number (const_rtx rtl)
9960 {
9961   unsigned regno = REGNO (rtl);
9962
9963   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9964
9965 #ifdef LEAF_REG_REMAP
9966   if (current_function_uses_only_leaf_regs)
9967     {
9968       int leaf_reg = LEAF_REG_REMAP (regno);
9969       if (leaf_reg != -1)
9970         regno = (unsigned) leaf_reg;
9971     }
9972 #endif
9973
9974   return DBX_REGISTER_NUMBER (regno);
9975 }
9976
9977 /* Optionally add a DW_OP_piece term to a location description expression.
9978    DW_OP_piece is only added if the location description expression already
9979    doesn't end with DW_OP_piece.  */
9980
9981 static void
9982 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9983 {
9984   dw_loc_descr_ref loc;
9985
9986   if (*list_head != NULL)
9987     {
9988       /* Find the end of the chain.  */
9989       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9990         ;
9991
9992       if (loc->dw_loc_opc != DW_OP_piece)
9993         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9994     }
9995 }
9996
9997 /* Return a location descriptor that designates a machine register or
9998    zero if there is none.  */
9999
10000 static dw_loc_descr_ref
10001 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10002 {
10003   rtx regs;
10004
10005   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10006     return 0;
10007
10008   regs = targetm.dwarf_register_span (rtl);
10009
10010   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10011     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10012   else
10013     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10014 }
10015
10016 /* Return a location descriptor that designates a machine register for
10017    a given hard register number.  */
10018
10019 static dw_loc_descr_ref
10020 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10021 {
10022   dw_loc_descr_ref reg_loc_descr;
10023
10024   if (regno <= 31)
10025     reg_loc_descr
10026       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10027   else
10028     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10029
10030   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10031     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10032
10033   return reg_loc_descr;
10034 }
10035
10036 /* Given an RTL of a register, return a location descriptor that
10037    designates a value that spans more than one register.  */
10038
10039 static dw_loc_descr_ref
10040 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10041                              enum var_init_status initialized)
10042 {
10043   int nregs, size, i;
10044   unsigned reg;
10045   dw_loc_descr_ref loc_result = NULL;
10046
10047   reg = REGNO (rtl);
10048 #ifdef LEAF_REG_REMAP
10049   if (current_function_uses_only_leaf_regs)
10050     {
10051       int leaf_reg = LEAF_REG_REMAP (reg);
10052       if (leaf_reg != -1)
10053         reg = (unsigned) leaf_reg;
10054     }
10055 #endif
10056   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10057   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10058
10059   /* Simple, contiguous registers.  */
10060   if (regs == NULL_RTX)
10061     {
10062       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10063
10064       loc_result = NULL;
10065       while (nregs--)
10066         {
10067           dw_loc_descr_ref t;
10068
10069           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10070                                       VAR_INIT_STATUS_INITIALIZED);
10071           add_loc_descr (&loc_result, t);
10072           add_loc_descr_op_piece (&loc_result, size);
10073           ++reg;
10074         }
10075       return loc_result;
10076     }
10077
10078   /* Now onto stupid register sets in non contiguous locations.  */
10079
10080   gcc_assert (GET_CODE (regs) == PARALLEL);
10081
10082   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10083   loc_result = NULL;
10084
10085   for (i = 0; i < XVECLEN (regs, 0); ++i)
10086     {
10087       dw_loc_descr_ref t;
10088
10089       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10090                                   VAR_INIT_STATUS_INITIALIZED);
10091       add_loc_descr (&loc_result, t);
10092       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10093       add_loc_descr_op_piece (&loc_result, size);
10094     }
10095
10096   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10097     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10098   return loc_result;
10099 }
10100
10101 #endif /* DWARF2_DEBUGGING_INFO */
10102
10103 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10104
10105 /* Return a location descriptor that designates a constant.  */
10106
10107 static dw_loc_descr_ref
10108 int_loc_descriptor (HOST_WIDE_INT i)
10109 {
10110   enum dwarf_location_atom op;
10111
10112   /* Pick the smallest representation of a constant, rather than just
10113      defaulting to the LEB encoding.  */
10114   if (i >= 0)
10115     {
10116       if (i <= 31)
10117         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10118       else if (i <= 0xff)
10119         op = DW_OP_const1u;
10120       else if (i <= 0xffff)
10121         op = DW_OP_const2u;
10122       else if (HOST_BITS_PER_WIDE_INT == 32
10123                || i <= 0xffffffff)
10124         op = DW_OP_const4u;
10125       else
10126         op = DW_OP_constu;
10127     }
10128   else
10129     {
10130       if (i >= -0x80)
10131         op = DW_OP_const1s;
10132       else if (i >= -0x8000)
10133         op = DW_OP_const2s;
10134       else if (HOST_BITS_PER_WIDE_INT == 32
10135                || i >= -0x80000000)
10136         op = DW_OP_const4s;
10137       else
10138         op = DW_OP_consts;
10139     }
10140
10141   return new_loc_descr (op, i, 0);
10142 }
10143 #endif
10144
10145 #ifdef DWARF2_DEBUGGING_INFO
10146
10147 /* Return a location descriptor that designates a base+offset location.  */
10148
10149 static dw_loc_descr_ref
10150 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10151                  enum var_init_status initialized)
10152 {
10153   unsigned int regno;
10154   dw_loc_descr_ref result;
10155   dw_fde_ref fde = current_fde ();
10156
10157   /* We only use "frame base" when we're sure we're talking about the
10158      post-prologue local stack frame.  We do this by *not* running
10159      register elimination until this point, and recognizing the special
10160      argument pointer and soft frame pointer rtx's.  */
10161   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10162     {
10163       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10164
10165       if (elim != reg)
10166         {
10167           if (GET_CODE (elim) == PLUS)
10168             {
10169               offset += INTVAL (XEXP (elim, 1));
10170               elim = XEXP (elim, 0);
10171             }
10172           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10173                        && (elim == hard_frame_pointer_rtx
10174                            || elim == stack_pointer_rtx))
10175                       || elim == (frame_pointer_needed
10176                                   ? hard_frame_pointer_rtx
10177                                   : stack_pointer_rtx));
10178
10179           /* If drap register is used to align stack, use frame
10180              pointer + offset to access stack variables.  If stack
10181              is aligned without drap, use stack pointer + offset to
10182              access stack variables.  */
10183           if (crtl->stack_realign_tried
10184               && cfa.reg == HARD_FRAME_POINTER_REGNUM
10185               && reg == frame_pointer_rtx)
10186             {
10187               int base_reg
10188                 = DWARF_FRAME_REGNUM (cfa.indirect
10189                                       ? HARD_FRAME_POINTER_REGNUM
10190                                       : STACK_POINTER_REGNUM);
10191               return new_reg_loc_descr (base_reg, offset);
10192             }
10193
10194           offset += frame_pointer_fb_offset;
10195           return new_loc_descr (DW_OP_fbreg, offset, 0);
10196         }
10197     }
10198   else if (fde
10199            && fde->drap_reg != INVALID_REGNUM
10200            && (fde->drap_reg == REGNO (reg)
10201                || fde->vdrap_reg == REGNO (reg)))
10202     {
10203       /* Use cfa+offset to represent the location of arguments passed
10204          on stack when drap is used to align stack.  */
10205       return new_loc_descr (DW_OP_fbreg, offset, 0);
10206     }
10207
10208   regno = dbx_reg_number (reg);
10209   if (regno <= 31)
10210     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10211                             offset, 0);
10212   else
10213     result = new_loc_descr (DW_OP_bregx, regno, offset);
10214
10215   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10216     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10217
10218   return result;
10219 }
10220
10221 /* Return true if this RTL expression describes a base+offset calculation.  */
10222
10223 static inline int
10224 is_based_loc (const_rtx rtl)
10225 {
10226   return (GET_CODE (rtl) == PLUS
10227           && ((REG_P (XEXP (rtl, 0))
10228                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10229                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
10230 }
10231
10232 /* Return a descriptor that describes the concatenation of N locations
10233    used to form the address of a memory location.  */
10234
10235 static dw_loc_descr_ref
10236 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10237                             enum var_init_status initialized)
10238 {
10239   unsigned int i;
10240   dw_loc_descr_ref cc_loc_result = NULL;
10241   unsigned int n = XVECLEN (concatn, 0);
10242
10243   for (i = 0; i < n; ++i)
10244     {
10245       dw_loc_descr_ref ref;
10246       rtx x = XVECEXP (concatn, 0, i);
10247
10248       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10249       if (ref == NULL)
10250         return NULL;
10251
10252       add_loc_descr (&cc_loc_result, ref);
10253       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10254     }
10255
10256   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10257     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10258
10259   return cc_loc_result;
10260 }
10261
10262 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10263    failed.  */
10264
10265 static dw_loc_descr_ref
10266 tls_mem_loc_descriptor (rtx mem)
10267 {
10268   tree base;
10269   dw_loc_descr_ref loc_result;
10270
10271   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10272     return NULL;
10273
10274   base = get_base_address (MEM_EXPR (mem));
10275   if (base == NULL
10276       || TREE_CODE (base) != VAR_DECL
10277       || !DECL_THREAD_LOCAL_P (base))
10278     return NULL;
10279
10280   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10281   if (loc_result == NULL)
10282     return NULL;
10283
10284   if (INTVAL (MEM_OFFSET (mem)))
10285     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10286
10287   return loc_result;
10288 }
10289
10290 /* The following routine converts the RTL for a variable or parameter
10291    (resident in memory) into an equivalent Dwarf representation of a
10292    mechanism for getting the address of that same variable onto the top of a
10293    hypothetical "address evaluation" stack.
10294
10295    When creating memory location descriptors, we are effectively transforming
10296    the RTL for a memory-resident object into its Dwarf postfix expression
10297    equivalent.  This routine recursively descends an RTL tree, turning
10298    it into Dwarf postfix code as it goes.
10299
10300    MODE is the mode of the memory reference, needed to handle some
10301    autoincrement addressing modes.
10302
10303    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10304    location list for RTL.
10305
10306    Return 0 if we can't represent the location.  */
10307
10308 static dw_loc_descr_ref
10309 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10310                     enum var_init_status initialized)
10311 {
10312   dw_loc_descr_ref mem_loc_result = NULL;
10313   enum dwarf_location_atom op;
10314
10315   /* Note that for a dynamically sized array, the location we will generate a
10316      description of here will be the lowest numbered location which is
10317      actually within the array.  That's *not* necessarily the same as the
10318      zeroth element of the array.  */
10319
10320   rtl = targetm.delegitimize_address (rtl);
10321
10322   switch (GET_CODE (rtl))
10323     {
10324     case POST_INC:
10325     case POST_DEC:
10326     case POST_MODIFY:
10327       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10328          just fall into the SUBREG code.  */
10329
10330       /* ... fall through ...  */
10331
10332     case SUBREG:
10333       /* The case of a subreg may arise when we have a local (register)
10334          variable or a formal (register) parameter which doesn't quite fill
10335          up an entire register.  For now, just assume that it is
10336          legitimate to make the Dwarf info refer to the whole register which
10337          contains the given subreg.  */
10338       rtl = XEXP (rtl, 0);
10339
10340       /* ... fall through ...  */
10341
10342     case REG:
10343       /* Whenever a register number forms a part of the description of the
10344          method for calculating the (dynamic) address of a memory resident
10345          object, DWARF rules require the register number be referred to as
10346          a "base register".  This distinction is not based in any way upon
10347          what category of register the hardware believes the given register
10348          belongs to.  This is strictly DWARF terminology we're dealing with
10349          here. Note that in cases where the location of a memory-resident
10350          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10351          OP_CONST (0)) the actual DWARF location descriptor that we generate
10352          may just be OP_BASEREG (basereg).  This may look deceptively like
10353          the object in question was allocated to a register (rather than in
10354          memory) so DWARF consumers need to be aware of the subtle
10355          distinction between OP_REG and OP_BASEREG.  */
10356       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10357         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10358       else if (stack_realign_drap
10359                && crtl->drap_reg
10360                && crtl->args.internal_arg_pointer == rtl
10361                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10362         {
10363           /* If RTL is internal_arg_pointer, which has been optimized
10364              out, use DRAP instead.  */
10365           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10366                                             VAR_INIT_STATUS_INITIALIZED);
10367         }
10368       break;
10369
10370     case MEM:
10371       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10372                                            VAR_INIT_STATUS_INITIALIZED);
10373       if (mem_loc_result == NULL)
10374         mem_loc_result = tls_mem_loc_descriptor (rtl);
10375       if (mem_loc_result != 0)
10376         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10377       break;
10378
10379     case LO_SUM:
10380          rtl = XEXP (rtl, 1);
10381
10382       /* ... fall through ...  */
10383
10384     case LABEL_REF:
10385       /* Some ports can transform a symbol ref into a label ref, because
10386          the symbol ref is too far away and has to be dumped into a constant
10387          pool.  */
10388     case CONST:
10389     case SYMBOL_REF:
10390       /* Alternatively, the symbol in the constant pool might be referenced
10391          by a different symbol.  */
10392       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10393         {
10394           bool marked;
10395           rtx tmp = get_pool_constant_mark (rtl, &marked);
10396
10397           if (GET_CODE (tmp) == SYMBOL_REF)
10398             {
10399               rtl = tmp;
10400               if (CONSTANT_POOL_ADDRESS_P (tmp))
10401                 get_pool_constant_mark (tmp, &marked);
10402               else
10403                 marked = true;
10404             }
10405
10406           /* If all references to this pool constant were optimized away,
10407              it was not output and thus we can't represent it.
10408              FIXME: might try to use DW_OP_const_value here, though
10409              DW_OP_piece complicates it.  */
10410           if (!marked)
10411             return 0;
10412         }
10413
10414       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10415       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10416       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10417       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10418       break;
10419
10420     case PRE_MODIFY:
10421       /* Extract the PLUS expression nested inside and fall into
10422          PLUS code below.  */
10423       rtl = XEXP (rtl, 1);
10424       goto plus;
10425
10426     case PRE_INC:
10427     case PRE_DEC:
10428       /* Turn these into a PLUS expression and fall into the PLUS code
10429          below.  */
10430       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10431                           GEN_INT (GET_CODE (rtl) == PRE_INC
10432                                    ? GET_MODE_UNIT_SIZE (mode)
10433                                    : -GET_MODE_UNIT_SIZE (mode)));
10434
10435       /* ... fall through ...  */
10436
10437     case PLUS:
10438     plus:
10439       if (is_based_loc (rtl))
10440         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10441                                           INTVAL (XEXP (rtl, 1)),
10442                                           VAR_INIT_STATUS_INITIALIZED);
10443       else
10444         {
10445           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10446                                                VAR_INIT_STATUS_INITIALIZED);
10447           if (mem_loc_result == 0)
10448             break;
10449
10450           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT)
10451             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
10452           else
10453             {
10454               dw_loc_descr_ref mem_loc_result2
10455                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10456                                       VAR_INIT_STATUS_INITIALIZED);
10457               if (mem_loc_result2 == 0)
10458                 break;
10459               add_loc_descr (&mem_loc_result, mem_loc_result2);
10460               add_loc_descr (&mem_loc_result,
10461                              new_loc_descr (DW_OP_plus, 0, 0));
10462             }
10463         }
10464       break;
10465
10466     /* If a pseudo-reg is optimized away, it is possible for it to
10467        be replaced with a MEM containing a multiply or shift.  */
10468     case MULT:
10469       op = DW_OP_mul;
10470       goto do_binop;
10471
10472     case ASHIFT:
10473       op = DW_OP_shl;
10474       goto do_binop;
10475
10476     case ASHIFTRT:
10477       op = DW_OP_shra;
10478       goto do_binop;
10479
10480     case LSHIFTRT:
10481       op = DW_OP_shr;
10482       goto do_binop;
10483
10484     do_binop:
10485       {
10486         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10487                                                    VAR_INIT_STATUS_INITIALIZED);
10488         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10489                                                    VAR_INIT_STATUS_INITIALIZED);
10490
10491         if (op0 == 0 || op1 == 0)
10492           break;
10493
10494         mem_loc_result = op0;
10495         add_loc_descr (&mem_loc_result, op1);
10496         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10497         break;
10498       }
10499
10500     case CONST_INT:
10501       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10502       break;
10503
10504     case CONCATN:
10505       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10506                                                    VAR_INIT_STATUS_INITIALIZED);
10507       break;
10508
10509     case UNSPEC:
10510       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10511          can't express it in the debug info.  This can happen e.g. with some
10512          TLS UNSPECs.  */
10513       break;
10514
10515     default:
10516       gcc_unreachable ();
10517     }
10518
10519   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10520     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10521
10522   return mem_loc_result;
10523 }
10524
10525 /* Return a descriptor that describes the concatenation of two locations.
10526    This is typically a complex variable.  */
10527
10528 static dw_loc_descr_ref
10529 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10530 {
10531   dw_loc_descr_ref cc_loc_result = NULL;
10532   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10533   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10534
10535   if (x0_ref == 0 || x1_ref == 0)
10536     return 0;
10537
10538   cc_loc_result = x0_ref;
10539   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10540
10541   add_loc_descr (&cc_loc_result, x1_ref);
10542   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10543
10544   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10545     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10546
10547   return cc_loc_result;
10548 }
10549
10550 /* Return a descriptor that describes the concatenation of N
10551    locations.  */
10552
10553 static dw_loc_descr_ref
10554 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10555 {
10556   unsigned int i;
10557   dw_loc_descr_ref cc_loc_result = NULL;
10558   unsigned int n = XVECLEN (concatn, 0);
10559
10560   for (i = 0; i < n; ++i)
10561     {
10562       dw_loc_descr_ref ref;
10563       rtx x = XVECEXP (concatn, 0, i);
10564
10565       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10566       if (ref == NULL)
10567         return NULL;
10568
10569       add_loc_descr (&cc_loc_result, ref);
10570       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10571     }
10572
10573   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10574     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10575
10576   return cc_loc_result;
10577 }
10578
10579 /* Output a proper Dwarf location descriptor for a variable or parameter
10580    which is either allocated in a register or in a memory location.  For a
10581    register, we just generate an OP_REG and the register number.  For a
10582    memory location we provide a Dwarf postfix expression describing how to
10583    generate the (dynamic) address of the object onto the address stack.
10584
10585    If we don't know how to describe it, return 0.  */
10586
10587 static dw_loc_descr_ref
10588 loc_descriptor (rtx rtl, enum var_init_status initialized)
10589 {
10590   dw_loc_descr_ref loc_result = NULL;
10591
10592   switch (GET_CODE (rtl))
10593     {
10594     case SUBREG:
10595       /* The case of a subreg may arise when we have a local (register)
10596          variable or a formal (register) parameter which doesn't quite fill
10597          up an entire register.  For now, just assume that it is
10598          legitimate to make the Dwarf info refer to the whole register which
10599          contains the given subreg.  */
10600       rtl = SUBREG_REG (rtl);
10601
10602       /* ... fall through ...  */
10603
10604     case REG:
10605       loc_result = reg_loc_descriptor (rtl, initialized);
10606       break;
10607
10608     case MEM:
10609       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10610                                        initialized);
10611       if (loc_result == NULL)
10612         loc_result = tls_mem_loc_descriptor (rtl);
10613       break;
10614
10615     case CONCAT:
10616       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10617                                           initialized);
10618       break;
10619
10620     case CONCATN:
10621       loc_result = concatn_loc_descriptor (rtl, initialized);
10622       break;
10623
10624     case VAR_LOCATION:
10625       /* Single part.  */
10626       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10627         {
10628           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10629           break;
10630         }
10631
10632       rtl = XEXP (rtl, 1);
10633       /* FALLTHRU */
10634
10635     case PARALLEL:
10636       {
10637         rtvec par_elems = XVEC (rtl, 0);
10638         int num_elem = GET_NUM_ELEM (par_elems);
10639         enum machine_mode mode;
10640         int i;
10641
10642         /* Create the first one, so we have something to add to.  */
10643         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10644                                      initialized);
10645         if (loc_result == NULL)
10646           return NULL;
10647         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10648         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10649         for (i = 1; i < num_elem; i++)
10650           {
10651             dw_loc_descr_ref temp;
10652
10653             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10654                                    initialized);
10655             if (temp == NULL)
10656               return NULL;
10657             add_loc_descr (&loc_result, temp);
10658             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10659             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10660           }
10661       }
10662       break;
10663
10664     default:
10665       gcc_unreachable ();
10666     }
10667
10668   return loc_result;
10669 }
10670
10671 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10672    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10673    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10674    top-level invocation, and we require the address of LOC; is 0 if we require
10675    the value of LOC.  */
10676
10677 static dw_loc_descr_ref
10678 loc_descriptor_from_tree_1 (tree loc, int want_address)
10679 {
10680   dw_loc_descr_ref ret, ret1;
10681   int have_address = 0;
10682   enum dwarf_location_atom op;
10683
10684   /* ??? Most of the time we do not take proper care for sign/zero
10685      extending the values properly.  Hopefully this won't be a real
10686      problem...  */
10687
10688   switch (TREE_CODE (loc))
10689     {
10690     case ERROR_MARK:
10691       return 0;
10692
10693     case PLACEHOLDER_EXPR:
10694       /* This case involves extracting fields from an object to determine the
10695          position of other fields.  We don't try to encode this here.  The
10696          only user of this is Ada, which encodes the needed information using
10697          the names of types.  */
10698       return 0;
10699
10700     case CALL_EXPR:
10701       return 0;
10702
10703     case PREINCREMENT_EXPR:
10704     case PREDECREMENT_EXPR:
10705     case POSTINCREMENT_EXPR:
10706     case POSTDECREMENT_EXPR:
10707       /* There are no opcodes for these operations.  */
10708       return 0;
10709
10710     case ADDR_EXPR:
10711       /* If we already want an address, there's nothing we can do.  */
10712       if (want_address)
10713         return 0;
10714
10715       /* Otherwise, process the argument and look for the address.  */
10716       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10717
10718     case VAR_DECL:
10719       if (DECL_THREAD_LOCAL_P (loc))
10720         {
10721           rtx rtl;
10722           enum dwarf_location_atom first_op;
10723           enum dwarf_location_atom second_op;
10724
10725           if (targetm.have_tls)
10726             {
10727               /* If this is not defined, we have no way to emit the
10728                  data.  */
10729               if (!targetm.asm_out.output_dwarf_dtprel)
10730                 return 0;
10731
10732                /* The way DW_OP_GNU_push_tls_address is specified, we
10733                   can only look up addresses of objects in the current
10734                   module.  */
10735               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10736                 return 0;
10737               first_op = (enum dwarf_location_atom) INTERNAL_DW_OP_tls_addr;
10738               second_op = DW_OP_GNU_push_tls_address;
10739             }
10740           else
10741             {
10742               if (!targetm.emutls.debug_form_tls_address)
10743                 return 0;
10744               loc = emutls_decl (loc);
10745               first_op = DW_OP_addr;
10746               second_op = DW_OP_form_tls_address;
10747             }
10748
10749           rtl = rtl_for_decl_location (loc);
10750           if (rtl == NULL_RTX)
10751             return 0;
10752
10753           if (!MEM_P (rtl))
10754             return 0;
10755           rtl = XEXP (rtl, 0);
10756           if (! CONSTANT_P (rtl))
10757             return 0;
10758
10759           ret = new_loc_descr (first_op, 0, 0);
10760           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10761           ret->dw_loc_oprnd1.v.val_addr = rtl;
10762
10763           ret1 = new_loc_descr (second_op, 0, 0);
10764           add_loc_descr (&ret, ret1);
10765
10766           have_address = 1;
10767           break;
10768         }
10769       /* FALLTHRU */
10770
10771     case PARM_DECL:
10772       if (DECL_HAS_VALUE_EXPR_P (loc))
10773         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10774                                            want_address);
10775       /* FALLTHRU */
10776
10777     case RESULT_DECL:
10778     case FUNCTION_DECL:
10779       {
10780         rtx rtl = rtl_for_decl_location (loc);
10781
10782         if (rtl == NULL_RTX)
10783           return 0;
10784         else if (GET_CODE (rtl) == CONST_INT)
10785           {
10786             HOST_WIDE_INT val = INTVAL (rtl);
10787             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10788               val &= GET_MODE_MASK (DECL_MODE (loc));
10789             ret = int_loc_descriptor (val);
10790           }
10791         else if (GET_CODE (rtl) == CONST_STRING)
10792           return 0;
10793         else if (CONSTANT_P (rtl))
10794           {
10795             ret = new_loc_descr (DW_OP_addr, 0, 0);
10796             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10797             ret->dw_loc_oprnd1.v.val_addr = rtl;
10798           }
10799         else
10800           {
10801             enum machine_mode mode;
10802
10803             /* Certain constructs can only be represented at top-level.  */
10804             if (want_address == 2)
10805               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10806
10807             mode = GET_MODE (rtl);
10808             if (MEM_P (rtl))
10809               {
10810                 rtl = XEXP (rtl, 0);
10811                 have_address = 1;
10812               }
10813             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10814           }
10815       }
10816       break;
10817
10818     case INDIRECT_REF:
10819       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10820       have_address = 1;
10821       break;
10822
10823     case COMPOUND_EXPR:
10824       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10825
10826     CASE_CONVERT:
10827     case VIEW_CONVERT_EXPR:
10828     case SAVE_EXPR:
10829     case MODIFY_EXPR:
10830       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10831
10832     case COMPONENT_REF:
10833     case BIT_FIELD_REF:
10834     case ARRAY_REF:
10835     case ARRAY_RANGE_REF:
10836       {
10837         tree obj, offset;
10838         HOST_WIDE_INT bitsize, bitpos, bytepos;
10839         enum machine_mode mode;
10840         int volatilep;
10841         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10842
10843         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10844                                    &unsignedp, &volatilep, false);
10845
10846         if (obj == loc)
10847           return 0;
10848
10849         ret = loc_descriptor_from_tree_1 (obj, 1);
10850         if (ret == 0
10851             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10852           return 0;
10853
10854         if (offset != NULL_TREE)
10855           {
10856             /* Variable offset.  */
10857             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10858             if (ret1 == 0)
10859               return 0;
10860             add_loc_descr (&ret, ret1);
10861             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10862           }
10863
10864         bytepos = bitpos / BITS_PER_UNIT;
10865         loc_descr_plus_const (&ret, bytepos);
10866
10867         have_address = 1;
10868         break;
10869       }
10870
10871     case INTEGER_CST:
10872       if (host_integerp (loc, 0))
10873         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10874       else
10875         return 0;
10876       break;
10877
10878     case CONSTRUCTOR:
10879       {
10880         /* Get an RTL for this, if something has been emitted.  */
10881         rtx rtl = lookup_constant_def (loc);
10882         enum machine_mode mode;
10883
10884         if (!rtl || !MEM_P (rtl))
10885           return 0;
10886         mode = GET_MODE (rtl);
10887         rtl = XEXP (rtl, 0);
10888         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10889         have_address = 1;
10890         break;
10891       }
10892
10893     case TRUTH_AND_EXPR:
10894     case TRUTH_ANDIF_EXPR:
10895     case BIT_AND_EXPR:
10896       op = DW_OP_and;
10897       goto do_binop;
10898
10899     case TRUTH_XOR_EXPR:
10900     case BIT_XOR_EXPR:
10901       op = DW_OP_xor;
10902       goto do_binop;
10903
10904     case TRUTH_OR_EXPR:
10905     case TRUTH_ORIF_EXPR:
10906     case BIT_IOR_EXPR:
10907       op = DW_OP_or;
10908       goto do_binop;
10909
10910     case FLOOR_DIV_EXPR:
10911     case CEIL_DIV_EXPR:
10912     case ROUND_DIV_EXPR:
10913     case TRUNC_DIV_EXPR:
10914       op = DW_OP_div;
10915       goto do_binop;
10916
10917     case MINUS_EXPR:
10918       op = DW_OP_minus;
10919       goto do_binop;
10920
10921     case FLOOR_MOD_EXPR:
10922     case CEIL_MOD_EXPR:
10923     case ROUND_MOD_EXPR:
10924     case TRUNC_MOD_EXPR:
10925       op = DW_OP_mod;
10926       goto do_binop;
10927
10928     case MULT_EXPR:
10929       op = DW_OP_mul;
10930       goto do_binop;
10931
10932     case LSHIFT_EXPR:
10933       op = DW_OP_shl;
10934       goto do_binop;
10935
10936     case RSHIFT_EXPR:
10937       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10938       goto do_binop;
10939
10940     case POINTER_PLUS_EXPR:
10941     case PLUS_EXPR:
10942       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10943           && host_integerp (TREE_OPERAND (loc, 1), 0))
10944         {
10945           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10946           if (ret == 0)
10947             return 0;
10948
10949           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
10950           break;
10951         }
10952
10953       op = DW_OP_plus;
10954       goto do_binop;
10955
10956     case LE_EXPR:
10957       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10958         return 0;
10959
10960       op = DW_OP_le;
10961       goto do_binop;
10962
10963     case GE_EXPR:
10964       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10965         return 0;
10966
10967       op = DW_OP_ge;
10968       goto do_binop;
10969
10970     case LT_EXPR:
10971       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10972         return 0;
10973
10974       op = DW_OP_lt;
10975       goto do_binop;
10976
10977     case GT_EXPR:
10978       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10979         return 0;
10980
10981       op = DW_OP_gt;
10982       goto do_binop;
10983
10984     case EQ_EXPR:
10985       op = DW_OP_eq;
10986       goto do_binop;
10987
10988     case NE_EXPR:
10989       op = DW_OP_ne;
10990       goto do_binop;
10991
10992     do_binop:
10993       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10994       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10995       if (ret == 0 || ret1 == 0)
10996         return 0;
10997
10998       add_loc_descr (&ret, ret1);
10999       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11000       break;
11001
11002     case TRUTH_NOT_EXPR:
11003     case BIT_NOT_EXPR:
11004       op = DW_OP_not;
11005       goto do_unop;
11006
11007     case ABS_EXPR:
11008       op = DW_OP_abs;
11009       goto do_unop;
11010
11011     case NEGATE_EXPR:
11012       op = DW_OP_neg;
11013       goto do_unop;
11014
11015     do_unop:
11016       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11017       if (ret == 0)
11018         return 0;
11019
11020       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11021       break;
11022
11023     case MIN_EXPR:
11024     case MAX_EXPR:
11025       {
11026         const enum tree_code code =
11027           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
11028
11029         loc = build3 (COND_EXPR, TREE_TYPE (loc),
11030                       build2 (code, integer_type_node,
11031                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
11032                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
11033       }
11034
11035       /* ... fall through ...  */
11036
11037     case COND_EXPR:
11038       {
11039         dw_loc_descr_ref lhs
11040           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11041         dw_loc_descr_ref rhs
11042           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
11043         dw_loc_descr_ref bra_node, jump_node, tmp;
11044
11045         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11046         if (ret == 0 || lhs == 0 || rhs == 0)
11047           return 0;
11048
11049         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11050         add_loc_descr (&ret, bra_node);
11051
11052         add_loc_descr (&ret, rhs);
11053         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
11054         add_loc_descr (&ret, jump_node);
11055
11056         add_loc_descr (&ret, lhs);
11057         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11058         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
11059
11060         /* ??? Need a node to point the skip at.  Use a nop.  */
11061         tmp = new_loc_descr (DW_OP_nop, 0, 0);
11062         add_loc_descr (&ret, tmp);
11063         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11064         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
11065       }
11066       break;
11067
11068     case FIX_TRUNC_EXPR:
11069       return 0;
11070
11071     default:
11072       /* Leave front-end specific codes as simply unknown.  This comes
11073          up, for instance, with the C STMT_EXPR.  */
11074       if ((unsigned int) TREE_CODE (loc)
11075           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
11076         return 0;
11077
11078 #ifdef ENABLE_CHECKING
11079       /* Otherwise this is a generic code; we should just lists all of
11080          these explicitly.  We forgot one.  */
11081       gcc_unreachable ();
11082 #else
11083       /* In a release build, we want to degrade gracefully: better to
11084          generate incomplete debugging information than to crash.  */
11085       return NULL;
11086 #endif
11087     }
11088
11089   /* Show if we can't fill the request for an address.  */
11090   if (want_address && !have_address)
11091     return 0;
11092
11093   /* If we've got an address and don't want one, dereference.  */
11094   if (!want_address && have_address && ret)
11095     {
11096       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
11097
11098       if (size > DWARF2_ADDR_SIZE || size == -1)
11099         return 0;
11100       else if (size == DWARF2_ADDR_SIZE)
11101         op = DW_OP_deref;
11102       else
11103         op = DW_OP_deref_size;
11104
11105       add_loc_descr (&ret, new_loc_descr (op, size, 0));
11106     }
11107
11108   return ret;
11109 }
11110
11111 static inline dw_loc_descr_ref
11112 loc_descriptor_from_tree (tree loc)
11113 {
11114   return loc_descriptor_from_tree_1 (loc, 2);
11115 }
11116
11117 /* Given a value, round it up to the lowest multiple of `boundary'
11118    which is not less than the value itself.  */
11119
11120 static inline HOST_WIDE_INT
11121 ceiling (HOST_WIDE_INT value, unsigned int boundary)
11122 {
11123   return (((value + boundary - 1) / boundary) * boundary);
11124 }
11125
11126 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
11127    pointer to the declared type for the relevant field variable, or return
11128    `integer_type_node' if the given node turns out to be an
11129    ERROR_MARK node.  */
11130
11131 static inline tree
11132 field_type (const_tree decl)
11133 {
11134   tree type;
11135
11136   if (TREE_CODE (decl) == ERROR_MARK)
11137     return integer_type_node;
11138
11139   type = DECL_BIT_FIELD_TYPE (decl);
11140   if (type == NULL_TREE)
11141     type = TREE_TYPE (decl);
11142
11143   return type;
11144 }
11145
11146 /* Given a pointer to a tree node, return the alignment in bits for
11147    it, or else return BITS_PER_WORD if the node actually turns out to
11148    be an ERROR_MARK node.  */
11149
11150 static inline unsigned
11151 simple_type_align_in_bits (const_tree type)
11152 {
11153   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
11154 }
11155
11156 static inline unsigned
11157 simple_decl_align_in_bits (const_tree decl)
11158 {
11159   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
11160 }
11161
11162 /* Return the result of rounding T up to ALIGN.  */
11163
11164 static inline HOST_WIDE_INT
11165 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
11166 {
11167   /* We must be careful if T is negative because HOST_WIDE_INT can be
11168      either "above" or "below" unsigned int as per the C promotion
11169      rules, depending on the host, thus making the signedness of the
11170      direct multiplication and division unpredictable.  */
11171   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
11172
11173   u += align - 1;
11174   u /= align;
11175   u *= align;
11176
11177   return (HOST_WIDE_INT) u;
11178 }
11179
11180 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
11181    lowest addressed byte of the "containing object" for the given FIELD_DECL,
11182    or return 0 if we are unable to determine what that offset is, either
11183    because the argument turns out to be a pointer to an ERROR_MARK node, or
11184    because the offset is actually variable.  (We can't handle the latter case
11185    just yet).  */
11186
11187 static HOST_WIDE_INT
11188 field_byte_offset (const_tree decl)
11189 {
11190   HOST_WIDE_INT object_offset_in_bits;
11191   HOST_WIDE_INT bitpos_int;
11192
11193   if (TREE_CODE (decl) == ERROR_MARK)
11194     return 0;
11195
11196   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
11197
11198   /* We cannot yet cope with fields whose positions are variable, so
11199      for now, when we see such things, we simply return 0.  Someday, we may
11200      be able to handle such cases, but it will be damn difficult.  */
11201   if (! host_integerp (bit_position (decl), 0))
11202     return 0;
11203
11204   bitpos_int = int_bit_position (decl);
11205
11206 #ifdef PCC_BITFIELD_TYPE_MATTERS
11207   if (PCC_BITFIELD_TYPE_MATTERS)
11208     {
11209       tree type;
11210       tree field_size_tree;
11211       HOST_WIDE_INT deepest_bitpos;
11212       unsigned HOST_WIDE_INT field_size_in_bits;
11213       unsigned int type_align_in_bits;
11214       unsigned int decl_align_in_bits;
11215       unsigned HOST_WIDE_INT type_size_in_bits;
11216
11217       type = field_type (decl);
11218       type_size_in_bits = simple_type_size_in_bits (type);
11219       type_align_in_bits = simple_type_align_in_bits (type);
11220
11221       field_size_tree = DECL_SIZE (decl);
11222
11223       /* The size could be unspecified if there was an error, or for
11224          a flexible array member.  */
11225       if (!field_size_tree)
11226         field_size_tree = bitsize_zero_node;
11227
11228       /* If the size of the field is not constant, use the type size.  */
11229       if (host_integerp (field_size_tree, 1))
11230         field_size_in_bits = tree_low_cst (field_size_tree, 1);
11231       else
11232         field_size_in_bits = type_size_in_bits;
11233
11234       decl_align_in_bits = simple_decl_align_in_bits (decl);
11235
11236       /* The GCC front-end doesn't make any attempt to keep track of the
11237          starting bit offset (relative to the start of the containing
11238          structure type) of the hypothetical "containing object" for a
11239          bit-field.  Thus, when computing the byte offset value for the
11240          start of the "containing object" of a bit-field, we must deduce
11241          this information on our own. This can be rather tricky to do in
11242          some cases.  For example, handling the following structure type
11243          definition when compiling for an i386/i486 target (which only
11244          aligns long long's to 32-bit boundaries) can be very tricky:
11245
11246          struct S { int field1; long long field2:31; };
11247
11248          Fortunately, there is a simple rule-of-thumb which can be used
11249          in such cases.  When compiling for an i386/i486, GCC will
11250          allocate 8 bytes for the structure shown above.  It decides to
11251          do this based upon one simple rule for bit-field allocation.
11252          GCC allocates each "containing object" for each bit-field at
11253          the first (i.e. lowest addressed) legitimate alignment boundary
11254          (based upon the required minimum alignment for the declared
11255          type of the field) which it can possibly use, subject to the
11256          condition that there is still enough available space remaining
11257          in the containing object (when allocated at the selected point)
11258          to fully accommodate all of the bits of the bit-field itself.
11259
11260          This simple rule makes it obvious why GCC allocates 8 bytes for
11261          each object of the structure type shown above.  When looking
11262          for a place to allocate the "containing object" for `field2',
11263          the compiler simply tries to allocate a 64-bit "containing
11264          object" at each successive 32-bit boundary (starting at zero)
11265          until it finds a place to allocate that 64- bit field such that
11266          at least 31 contiguous (and previously unallocated) bits remain
11267          within that selected 64 bit field.  (As it turns out, for the
11268          example above, the compiler finds it is OK to allocate the
11269          "containing object" 64-bit field at bit-offset zero within the
11270          structure type.)
11271
11272          Here we attempt to work backwards from the limited set of facts
11273          we're given, and we try to deduce from those facts, where GCC
11274          must have believed that the containing object started (within
11275          the structure type). The value we deduce is then used (by the
11276          callers of this routine) to generate DW_AT_location and
11277          DW_AT_bit_offset attributes for fields (both bit-fields and, in
11278          the case of DW_AT_location, regular fields as well).  */
11279
11280       /* Figure out the bit-distance from the start of the structure to
11281          the "deepest" bit of the bit-field.  */
11282       deepest_bitpos = bitpos_int + field_size_in_bits;
11283
11284       /* This is the tricky part.  Use some fancy footwork to deduce
11285          where the lowest addressed bit of the containing object must
11286          be.  */
11287       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11288
11289       /* Round up to type_align by default.  This works best for
11290          bitfields.  */
11291       object_offset_in_bits
11292         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11293
11294       if (object_offset_in_bits > bitpos_int)
11295         {
11296           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11297
11298           /* Round up to decl_align instead.  */
11299           object_offset_in_bits
11300             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11301         }
11302     }
11303   else
11304 #endif
11305     object_offset_in_bits = bitpos_int;
11306
11307   return object_offset_in_bits / BITS_PER_UNIT;
11308 }
11309 \f
11310 /* The following routines define various Dwarf attributes and any data
11311    associated with them.  */
11312
11313 /* Add a location description attribute value to a DIE.
11314
11315    This emits location attributes suitable for whole variables and
11316    whole parameters.  Note that the location attributes for struct fields are
11317    generated by the routine `data_member_location_attribute' below.  */
11318
11319 static inline void
11320 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11321                              dw_loc_descr_ref descr)
11322 {
11323   if (descr != 0)
11324     add_AT_loc (die, attr_kind, descr);
11325 }
11326
11327 /* Attach the specialized form of location attribute used for data members of
11328    struct and union types.  In the special case of a FIELD_DECL node which
11329    represents a bit-field, the "offset" part of this special location
11330    descriptor must indicate the distance in bytes from the lowest-addressed
11331    byte of the containing struct or union type to the lowest-addressed byte of
11332    the "containing object" for the bit-field.  (See the `field_byte_offset'
11333    function above).
11334
11335    For any given bit-field, the "containing object" is a hypothetical object
11336    (of some integral or enum type) within which the given bit-field lives.  The
11337    type of this hypothetical "containing object" is always the same as the
11338    declared type of the individual bit-field itself (for GCC anyway... the
11339    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11340    bytes) of the hypothetical "containing object" which will be given in the
11341    DW_AT_byte_size attribute for this bit-field.  (See the
11342    `byte_size_attribute' function below.)  It is also used when calculating the
11343    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11344    function below.)  */
11345
11346 static void
11347 add_data_member_location_attribute (dw_die_ref die, tree decl)
11348 {
11349   HOST_WIDE_INT offset;
11350   dw_loc_descr_ref loc_descr = 0;
11351
11352   if (TREE_CODE (decl) == TREE_BINFO)
11353     {
11354       /* We're working on the TAG_inheritance for a base class.  */
11355       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11356         {
11357           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11358              aren't at a fixed offset from all (sub)objects of the same
11359              type.  We need to extract the appropriate offset from our
11360              vtable.  The following dwarf expression means
11361
11362                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11363
11364              This is specific to the V3 ABI, of course.  */
11365
11366           dw_loc_descr_ref tmp;
11367
11368           /* Make a copy of the object address.  */
11369           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11370           add_loc_descr (&loc_descr, tmp);
11371
11372           /* Extract the vtable address.  */
11373           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11374           add_loc_descr (&loc_descr, tmp);
11375
11376           /* Calculate the address of the offset.  */
11377           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11378           gcc_assert (offset < 0);
11379
11380           tmp = int_loc_descriptor (-offset);
11381           add_loc_descr (&loc_descr, tmp);
11382           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11383           add_loc_descr (&loc_descr, tmp);
11384
11385           /* Extract the offset.  */
11386           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11387           add_loc_descr (&loc_descr, tmp);
11388
11389           /* Add it to the object address.  */
11390           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11391           add_loc_descr (&loc_descr, tmp);
11392         }
11393       else
11394         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11395     }
11396   else
11397     offset = field_byte_offset (decl);
11398
11399   if (! loc_descr)
11400     {
11401       enum dwarf_location_atom op;
11402
11403       /* The DWARF2 standard says that we should assume that the structure
11404          address is already on the stack, so we can specify a structure field
11405          address by using DW_OP_plus_uconst.  */
11406
11407 #ifdef MIPS_DEBUGGING_INFO
11408       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11409          operator correctly.  It works only if we leave the offset on the
11410          stack.  */
11411       op = DW_OP_constu;
11412 #else
11413       op = DW_OP_plus_uconst;
11414 #endif
11415
11416       loc_descr = new_loc_descr (op, offset, 0);
11417     }
11418
11419   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11420 }
11421
11422 /* Writes integer values to dw_vec_const array.  */
11423
11424 static void
11425 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11426 {
11427   while (size != 0)
11428     {
11429       *dest++ = val & 0xff;
11430       val >>= 8;
11431       --size;
11432     }
11433 }
11434
11435 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11436
11437 static HOST_WIDE_INT
11438 extract_int (const unsigned char *src, unsigned int size)
11439 {
11440   HOST_WIDE_INT val = 0;
11441
11442   src += size;
11443   while (size != 0)
11444     {
11445       val <<= 8;
11446       val |= *--src & 0xff;
11447       --size;
11448     }
11449   return val;
11450 }
11451
11452 /* Writes floating point values to dw_vec_const array.  */
11453
11454 static void
11455 insert_float (const_rtx rtl, unsigned char *array)
11456 {
11457   REAL_VALUE_TYPE rv;
11458   long val[4];
11459   int i;
11460
11461   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11462   real_to_target (val, &rv, GET_MODE (rtl));
11463
11464   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11465   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11466     {
11467       insert_int (val[i], 4, array);
11468       array += 4;
11469     }
11470 }
11471
11472 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11473    does not have a "location" either in memory or in a register.  These
11474    things can arise in GNU C when a constant is passed as an actual parameter
11475    to an inlined function.  They can also arise in C++ where declared
11476    constants do not necessarily get memory "homes".  */
11477
11478 static void
11479 add_const_value_attribute (dw_die_ref die, rtx rtl)
11480 {
11481   switch (GET_CODE (rtl))
11482     {
11483     case CONST_INT:
11484       {
11485         HOST_WIDE_INT val = INTVAL (rtl);
11486
11487         if (val < 0)
11488           add_AT_int (die, DW_AT_const_value, val);
11489         else
11490           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11491       }
11492       break;
11493
11494     case CONST_DOUBLE:
11495       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11496          floating-point constant.  A CONST_DOUBLE is used whenever the
11497          constant requires more than one word in order to be adequately
11498          represented.  We output CONST_DOUBLEs as blocks.  */
11499       {
11500         enum machine_mode mode = GET_MODE (rtl);
11501
11502         if (SCALAR_FLOAT_MODE_P (mode))
11503           {
11504             unsigned int length = GET_MODE_SIZE (mode);
11505             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11506
11507             insert_float (rtl, array);
11508             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11509           }
11510         else
11511           {
11512             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11513             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11514
11515             add_AT_long_long (die, DW_AT_const_value,
11516                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11517           }
11518       }
11519       break;
11520
11521     case CONST_VECTOR:
11522       {
11523         enum machine_mode mode = GET_MODE (rtl);
11524         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11525         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11526         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11527         unsigned int i;
11528         unsigned char *p;
11529
11530         switch (GET_MODE_CLASS (mode))
11531           {
11532           case MODE_VECTOR_INT:
11533             for (i = 0, p = array; i < length; i++, p += elt_size)
11534               {
11535                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11536                 HOST_WIDE_INT lo, hi;
11537
11538                 switch (GET_CODE (elt))
11539                   {
11540                   case CONST_INT:
11541                     lo = INTVAL (elt);
11542                     hi = -(lo < 0);
11543                     break;
11544
11545                   case CONST_DOUBLE:
11546                     lo = CONST_DOUBLE_LOW (elt);
11547                     hi = CONST_DOUBLE_HIGH (elt);
11548                     break;
11549
11550                   default:
11551                     gcc_unreachable ();
11552                   }
11553
11554                 if (elt_size <= sizeof (HOST_WIDE_INT))
11555                   insert_int (lo, elt_size, p);
11556                 else
11557                   {
11558                     unsigned char *p0 = p;
11559                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11560
11561                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11562                     if (WORDS_BIG_ENDIAN)
11563                       {
11564                         p0 = p1;
11565                         p1 = p;
11566                       }
11567                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11568                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11569                   }
11570               }
11571             break;
11572
11573           case MODE_VECTOR_FLOAT:
11574             for (i = 0, p = array; i < length; i++, p += elt_size)
11575               {
11576                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11577                 insert_float (elt, p);
11578               }
11579             break;
11580
11581           default:
11582             gcc_unreachable ();
11583           }
11584
11585         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11586       }
11587       break;
11588
11589     case CONST_STRING:
11590       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11591       break;
11592
11593     case SYMBOL_REF:
11594     case LABEL_REF:
11595     case CONST:
11596       add_AT_addr (die, DW_AT_const_value, rtl);
11597       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11598       break;
11599
11600     case PLUS:
11601       /* In cases where an inlined instance of an inline function is passed
11602          the address of an `auto' variable (which is local to the caller) we
11603          can get a situation where the DECL_RTL of the artificial local
11604          variable (for the inlining) which acts as a stand-in for the
11605          corresponding formal parameter (of the inline function) will look
11606          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11607          exactly a compile-time constant expression, but it isn't the address
11608          of the (artificial) local variable either.  Rather, it represents the
11609          *value* which the artificial local variable always has during its
11610          lifetime.  We currently have no way to represent such quasi-constant
11611          values in Dwarf, so for now we just punt and generate nothing.  */
11612       break;
11613
11614     default:
11615       /* No other kinds of rtx should be possible here.  */
11616       gcc_unreachable ();
11617     }
11618
11619 }
11620
11621 /* Determine whether the evaluation of EXPR references any variables
11622    or functions which aren't otherwise used (and therefore may not be
11623    output).  */
11624 static tree
11625 reference_to_unused (tree * tp, int * walk_subtrees,
11626                      void * data ATTRIBUTE_UNUSED)
11627 {
11628   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11629     *walk_subtrees = 0;
11630
11631   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11632       && ! TREE_ASM_WRITTEN (*tp))
11633     return *tp;
11634   /* ???  The C++ FE emits debug information for using decls, so
11635      putting gcc_unreachable here falls over.  See PR31899.  For now
11636      be conservative.  */
11637   else if (!cgraph_global_info_ready
11638            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11639     return *tp;
11640   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11641     {
11642       struct varpool_node *node = varpool_node (*tp);
11643       if (!node->needed)
11644         return *tp;
11645     }
11646   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11647            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11648     {
11649       struct cgraph_node *node = cgraph_node (*tp);
11650       if (node->process || TREE_ASM_WRITTEN (*tp))
11651         return *tp;
11652     }
11653   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11654     return *tp;
11655
11656   return NULL_TREE;
11657 }
11658
11659 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11660    for use in a later add_const_value_attribute call.  */
11661
11662 static rtx
11663 rtl_for_decl_init (tree init, tree type)
11664 {
11665   rtx rtl = NULL_RTX;
11666
11667   /* If a variable is initialized with a string constant without embedded
11668      zeros, build CONST_STRING.  */
11669   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11670     {
11671       tree enttype = TREE_TYPE (type);
11672       tree domain = TYPE_DOMAIN (type);
11673       enum machine_mode mode = TYPE_MODE (enttype);
11674
11675       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11676           && domain
11677           && integer_zerop (TYPE_MIN_VALUE (domain))
11678           && compare_tree_int (TYPE_MAX_VALUE (domain),
11679                                TREE_STRING_LENGTH (init) - 1) == 0
11680           && ((size_t) TREE_STRING_LENGTH (init)
11681               == strlen (TREE_STRING_POINTER (init)) + 1))
11682         rtl = gen_rtx_CONST_STRING (VOIDmode,
11683                                     ggc_strdup (TREE_STRING_POINTER (init)));
11684     }
11685   /* Other aggregates, and complex values, could be represented using
11686      CONCAT: FIXME!  */
11687   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11688     ;
11689   /* Vectors only work if their mode is supported by the target.
11690      FIXME: generic vectors ought to work too.  */
11691   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11692     ;
11693   /* If the initializer is something that we know will expand into an
11694      immediate RTL constant, expand it now.  We must be careful not to
11695      reference variables which won't be output.  */
11696   else if (initializer_constant_valid_p (init, type)
11697            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11698     {
11699       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11700          possible.  */
11701       if (TREE_CODE (type) == VECTOR_TYPE)
11702         switch (TREE_CODE (init))
11703           {
11704           case VECTOR_CST:
11705             break;
11706           case CONSTRUCTOR:
11707             if (TREE_CONSTANT (init))
11708               {
11709                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11710                 bool constant_p = true;
11711                 tree value;
11712                 unsigned HOST_WIDE_INT ix;
11713
11714                 /* Even when ctor is constant, it might contain non-*_CST
11715                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11716                    belong into VECTOR_CST nodes.  */
11717                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11718                   if (!CONSTANT_CLASS_P (value))
11719                     {
11720                       constant_p = false;
11721                       break;
11722                     }
11723
11724                 if (constant_p)
11725                   {
11726                     init = build_vector_from_ctor (type, elts);
11727                     break;
11728                   }
11729               }
11730             /* FALLTHRU */
11731
11732           default:
11733             return NULL;
11734           }
11735
11736       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11737
11738       /* If expand_expr returns a MEM, it wasn't immediate.  */
11739       gcc_assert (!rtl || !MEM_P (rtl));
11740     }
11741
11742   return rtl;
11743 }
11744
11745 /* Generate RTL for the variable DECL to represent its location.  */
11746
11747 static rtx
11748 rtl_for_decl_location (tree decl)
11749 {
11750   rtx rtl;
11751
11752   /* Here we have to decide where we are going to say the parameter "lives"
11753      (as far as the debugger is concerned).  We only have a couple of
11754      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11755
11756      DECL_RTL normally indicates where the parameter lives during most of the
11757      activation of the function.  If optimization is enabled however, this
11758      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11759      that the parameter doesn't really live anywhere (as far as the code
11760      generation parts of GCC are concerned) during most of the function's
11761      activation.  That will happen (for example) if the parameter is never
11762      referenced within the function.
11763
11764      We could just generate a location descriptor here for all non-NULL
11765      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11766      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11767      where DECL_RTL is NULL or is a pseudo-reg.
11768
11769      Note however that we can only get away with using DECL_INCOMING_RTL as
11770      a backup substitute for DECL_RTL in certain limited cases.  In cases
11771      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11772      we can be sure that the parameter was passed using the same type as it is
11773      declared to have within the function, and that its DECL_INCOMING_RTL
11774      points us to a place where a value of that type is passed.
11775
11776      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11777      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11778      because in these cases DECL_INCOMING_RTL points us to a value of some
11779      type which is *different* from the type of the parameter itself.  Thus,
11780      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11781      such cases, the debugger would end up (for example) trying to fetch a
11782      `float' from a place which actually contains the first part of a
11783      `double'.  That would lead to really incorrect and confusing
11784      output at debug-time.
11785
11786      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11787      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11788      are a couple of exceptions however.  On little-endian machines we can
11789      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11790      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11791      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11792      when (on a little-endian machine) a non-prototyped function has a
11793      parameter declared to be of type `short' or `char'.  In such cases,
11794      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11795      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11796      passed `int' value.  If the debugger then uses that address to fetch
11797      a `short' or a `char' (on a little-endian machine) the result will be
11798      the correct data, so we allow for such exceptional cases below.
11799
11800      Note that our goal here is to describe the place where the given formal
11801      parameter lives during most of the function's activation (i.e. between the
11802      end of the prologue and the start of the epilogue).  We'll do that as best
11803      as we can. Note however that if the given formal parameter is modified
11804      sometime during the execution of the function, then a stack backtrace (at
11805      debug-time) will show the function as having been called with the *new*
11806      value rather than the value which was originally passed in.  This happens
11807      rarely enough that it is not a major problem, but it *is* a problem, and
11808      I'd like to fix it.
11809
11810      A future version of dwarf2out.c may generate two additional attributes for
11811      any given DW_TAG_formal_parameter DIE which will describe the "passed
11812      type" and the "passed location" for the given formal parameter in addition
11813      to the attributes we now generate to indicate the "declared type" and the
11814      "active location" for each parameter.  This additional set of attributes
11815      could be used by debuggers for stack backtraces. Separately, note that
11816      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11817      This happens (for example) for inlined-instances of inline function formal
11818      parameters which are never referenced.  This really shouldn't be
11819      happening.  All PARM_DECL nodes should get valid non-NULL
11820      DECL_INCOMING_RTL values.  FIXME.  */
11821
11822   /* Use DECL_RTL as the "location" unless we find something better.  */
11823   rtl = DECL_RTL_IF_SET (decl);
11824
11825   /* When generating abstract instances, ignore everything except
11826      constants, symbols living in memory, and symbols living in
11827      fixed registers.  */
11828   if (! reload_completed)
11829     {
11830       if (rtl
11831           && (CONSTANT_P (rtl)
11832               || (MEM_P (rtl)
11833                   && CONSTANT_P (XEXP (rtl, 0)))
11834               || (REG_P (rtl)
11835                   && TREE_CODE (decl) == VAR_DECL
11836                   && TREE_STATIC (decl))))
11837         {
11838           rtl = targetm.delegitimize_address (rtl);
11839           return rtl;
11840         }
11841       rtl = NULL_RTX;
11842     }
11843   else if (TREE_CODE (decl) == PARM_DECL)
11844     {
11845       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11846         {
11847           tree declared_type = TREE_TYPE (decl);
11848           tree passed_type = DECL_ARG_TYPE (decl);
11849           enum machine_mode dmode = TYPE_MODE (declared_type);
11850           enum machine_mode pmode = TYPE_MODE (passed_type);
11851
11852           /* This decl represents a formal parameter which was optimized out.
11853              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11854              all cases where (rtl == NULL_RTX) just below.  */
11855           if (dmode == pmode)
11856             rtl = DECL_INCOMING_RTL (decl);
11857           else if (SCALAR_INT_MODE_P (dmode)
11858                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11859                    && DECL_INCOMING_RTL (decl))
11860             {
11861               rtx inc = DECL_INCOMING_RTL (decl);
11862               if (REG_P (inc))
11863                 rtl = inc;
11864               else if (MEM_P (inc))
11865                 {
11866                   if (BYTES_BIG_ENDIAN)
11867                     rtl = adjust_address_nv (inc, dmode,
11868                                              GET_MODE_SIZE (pmode)
11869                                              - GET_MODE_SIZE (dmode));
11870                   else
11871                     rtl = inc;
11872                 }
11873             }
11874         }
11875
11876       /* If the parm was passed in registers, but lives on the stack, then
11877          make a big endian correction if the mode of the type of the
11878          parameter is not the same as the mode of the rtl.  */
11879       /* ??? This is the same series of checks that are made in dbxout.c before
11880          we reach the big endian correction code there.  It isn't clear if all
11881          of these checks are necessary here, but keeping them all is the safe
11882          thing to do.  */
11883       else if (MEM_P (rtl)
11884                && XEXP (rtl, 0) != const0_rtx
11885                && ! CONSTANT_P (XEXP (rtl, 0))
11886                /* Not passed in memory.  */
11887                && !MEM_P (DECL_INCOMING_RTL (decl))
11888                /* Not passed by invisible reference.  */
11889                && (!REG_P (XEXP (rtl, 0))
11890                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11891                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11892 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11893                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11894 #endif
11895                      )
11896                /* Big endian correction check.  */
11897                && BYTES_BIG_ENDIAN
11898                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11899                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11900                    < UNITS_PER_WORD))
11901         {
11902           int offset = (UNITS_PER_WORD
11903                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11904
11905           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11906                              plus_constant (XEXP (rtl, 0), offset));
11907         }
11908     }
11909   else if (TREE_CODE (decl) == VAR_DECL
11910            && rtl
11911            && MEM_P (rtl)
11912            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11913            && BYTES_BIG_ENDIAN)
11914     {
11915       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11916       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11917
11918       /* If a variable is declared "register" yet is smaller than
11919          a register, then if we store the variable to memory, it
11920          looks like we're storing a register-sized value, when in
11921          fact we are not.  We need to adjust the offset of the
11922          storage location to reflect the actual value's bytes,
11923          else gdb will not be able to display it.  */
11924       if (rsize > dsize)
11925         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11926                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11927     }
11928
11929   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11930      and will have been substituted directly into all expressions that use it.
11931      C does not have such a concept, but C++ and other languages do.  */
11932   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11933     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11934
11935   if (rtl)
11936     rtl = targetm.delegitimize_address (rtl);
11937
11938   /* If we don't look past the constant pool, we risk emitting a
11939      reference to a constant pool entry that isn't referenced from
11940      code, and thus is not emitted.  */
11941   if (rtl)
11942     rtl = avoid_constant_pool_reference (rtl);
11943
11944   return rtl;
11945 }
11946
11947 /* We need to figure out what section we should use as the base for the
11948    address ranges where a given location is valid.
11949    1. If this particular DECL has a section associated with it, use that.
11950    2. If this function has a section associated with it, use that.
11951    3. Otherwise, use the text section.
11952    XXX: If you split a variable across multiple sections, we won't notice.  */
11953
11954 static const char *
11955 secname_for_decl (const_tree decl)
11956 {
11957   const char *secname;
11958
11959   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11960     {
11961       tree sectree = DECL_SECTION_NAME (decl);
11962       secname = TREE_STRING_POINTER (sectree);
11963     }
11964   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11965     {
11966       tree sectree = DECL_SECTION_NAME (current_function_decl);
11967       secname = TREE_STRING_POINTER (sectree);
11968     }
11969   else if (cfun && in_cold_section_p)
11970     secname = crtl->subsections.cold_section_label;
11971   else
11972     secname = text_section_label;
11973
11974   return secname;
11975 }
11976
11977 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11978    returned.  If so, the decl for the COMMON block is returned, and the
11979    value is the offset into the common block for the symbol.  */
11980
11981 static tree
11982 fortran_common (tree decl, HOST_WIDE_INT *value)
11983 {
11984   tree val_expr, cvar;
11985   enum machine_mode mode;
11986   HOST_WIDE_INT bitsize, bitpos;
11987   tree offset;
11988   int volatilep = 0, unsignedp = 0;
11989
11990   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11991      it does not have a value (the offset into the common area), or if it
11992      is thread local (as opposed to global) then it isn't common, and shouldn't
11993      be handled as such.  */
11994   if (TREE_CODE (decl) != VAR_DECL
11995       || !TREE_PUBLIC (decl)
11996       || !TREE_STATIC (decl)
11997       || !DECL_HAS_VALUE_EXPR_P (decl)
11998       || !is_fortran ())
11999     return NULL_TREE;
12000
12001   val_expr = DECL_VALUE_EXPR (decl);
12002   if (TREE_CODE (val_expr) != COMPONENT_REF)
12003     return NULL_TREE;
12004
12005   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
12006                               &mode, &unsignedp, &volatilep, true);
12007
12008   if (cvar == NULL_TREE
12009       || TREE_CODE (cvar) != VAR_DECL
12010       || DECL_ARTIFICIAL (cvar)
12011       || !TREE_PUBLIC (cvar))
12012     return NULL_TREE;
12013
12014   *value = 0;
12015   if (offset != NULL)
12016     {
12017       if (!host_integerp (offset, 0))
12018         return NULL_TREE;
12019       *value = tree_low_cst (offset, 0);
12020     }
12021   if (bitpos != 0)
12022     *value += bitpos / BITS_PER_UNIT;
12023
12024   return cvar;
12025 }
12026
12027 /* Dereference a location expression LOC if DECL is passed by invisible
12028    reference.  */
12029
12030 static dw_loc_descr_ref
12031 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12032 {
12033   HOST_WIDE_INT size;
12034   enum dwarf_location_atom op;
12035
12036   if (loc == NULL)
12037     return NULL;
12038
12039   if ((TREE_CODE (decl) != PARM_DECL
12040        && TREE_CODE (decl) != RESULT_DECL
12041        && TREE_CODE (decl) != VAR_DECL)
12042       || !DECL_BY_REFERENCE (decl))
12043     return loc;
12044
12045   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12046      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12047      location expression is considered to be address of a memory location,
12048      rather than the register itself.  */
12049   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12050        || loc->dw_loc_opc == DW_OP_regx)
12051       && (loc->dw_loc_next == NULL
12052           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12053               && loc->dw_loc_next->dw_loc_next == NULL)))
12054     {
12055       if (loc->dw_loc_opc == DW_OP_regx)
12056         {
12057           loc->dw_loc_opc = DW_OP_bregx;
12058           loc->dw_loc_oprnd2.v.val_int = 0;
12059         }
12060       else
12061         {
12062           loc->dw_loc_opc
12063             = (enum dwarf_location_atom)
12064               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12065           loc->dw_loc_oprnd1.v.val_int = 0;
12066         }
12067       return loc;
12068     }
12069
12070   size = int_size_in_bytes (TREE_TYPE (decl));
12071   if (size > DWARF2_ADDR_SIZE || size == -1)
12072     return 0;
12073   else if (size == DWARF2_ADDR_SIZE)
12074     op = DW_OP_deref;
12075   else
12076     op = DW_OP_deref_size;
12077   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12078   return loc;
12079 }
12080
12081 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
12082    data attribute for a variable or a parameter.  We generate the
12083    DW_AT_const_value attribute only in those cases where the given variable
12084    or parameter does not have a true "location" either in memory or in a
12085    register.  This can happen (for example) when a constant is passed as an
12086    actual argument in a call to an inline function.  (It's possible that
12087    these things can crop up in other ways also.)  Note that one type of
12088    constant value which can be passed into an inlined function is a constant
12089    pointer.  This can happen for example if an actual argument in an inlined
12090    function call evaluates to a compile-time constant address.  */
12091
12092 static void
12093 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
12094                                        enum dwarf_attribute attr)
12095 {
12096   rtx rtl;
12097   dw_loc_descr_ref descr;
12098   var_loc_list *loc_list;
12099   struct var_loc_node *node;
12100   if (TREE_CODE (decl) == ERROR_MARK)
12101     return;
12102
12103   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
12104               || TREE_CODE (decl) == RESULT_DECL);
12105
12106   /* See if we possibly have multiple locations for this variable.  */
12107   loc_list = lookup_decl_loc (decl);
12108
12109   /* If it truly has multiple locations, the first and last node will
12110      differ.  */
12111   if (loc_list && loc_list->first != loc_list->last)
12112     {
12113       const char *endname, *secname;
12114       dw_loc_list_ref list;
12115       rtx varloc;
12116       enum var_init_status initialized;
12117
12118       /* Now that we know what section we are using for a base,
12119          actually construct the list of locations.
12120          The first location information is what is passed to the
12121          function that creates the location list, and the remaining
12122          locations just get added on to that list.
12123          Note that we only know the start address for a location
12124          (IE location changes), so to build the range, we use
12125          the range [current location start, next location start].
12126          This means we have to special case the last node, and generate
12127          a range of [last location start, end of function label].  */
12128
12129       node = loc_list->first;
12130       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12131       secname = secname_for_decl (decl);
12132
12133       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12134         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12135       else
12136         initialized = VAR_INIT_STATUS_INITIALIZED;
12137
12138       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
12139       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12140       node = node->next;
12141
12142       for (; node->next; node = node->next)
12143         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12144           {
12145             /* The variable has a location between NODE->LABEL and
12146                NODE->NEXT->LABEL.  */
12147             enum var_init_status initialized =
12148               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12149             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12150             descr = loc_by_reference (loc_descriptor (varloc, initialized),
12151                                       decl);
12152             add_loc_descr_to_loc_list (&list, descr,
12153                                        node->label, node->next->label, secname);
12154           }
12155
12156       /* If the variable has a location at the last label
12157          it keeps its location until the end of function.  */
12158       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12159         {
12160           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12161           enum var_init_status initialized =
12162             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12163
12164           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12165           if (!current_function_decl)
12166             endname = text_end_label;
12167           else
12168             {
12169               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12170                                            current_function_funcdef_no);
12171               endname = ggc_strdup (label_id);
12172             }
12173           descr = loc_by_reference (loc_descriptor (varloc, initialized),
12174                                     decl);
12175           add_loc_descr_to_loc_list (&list, descr,
12176                                      node->label, endname, secname);
12177         }
12178
12179       /* Finally, add the location list to the DIE, and we are done.  */
12180       add_AT_loc_list (die, attr, list);
12181       return;
12182     }
12183
12184   /* Try to get some constant RTL for this decl, and use that as the value of
12185      the location.  */
12186
12187   rtl = rtl_for_decl_location (decl);
12188   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
12189     {
12190       add_const_value_attribute (die, rtl);
12191       return;
12192     }
12193
12194   /* If we have tried to generate the location otherwise, and it
12195      didn't work out (we wouldn't be here if we did), and we have a one entry
12196      location list, try generating a location from that.  */
12197   if (loc_list && loc_list->first)
12198     {
12199       enum var_init_status status;
12200       node = loc_list->first;
12201       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12202       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
12203       if (descr)
12204         {
12205           descr = loc_by_reference (descr, decl);
12206           add_AT_location_description (die, attr, descr);
12207           return;
12208         }
12209     }
12210
12211   /* We couldn't get any rtl, so try directly generating the location
12212      description from the tree.  */
12213   descr = loc_descriptor_from_tree (decl);
12214   if (descr)
12215     {
12216       descr = loc_by_reference (descr, decl);
12217       add_AT_location_description (die, attr, descr);
12218       return;
12219     }
12220   /* None of that worked, so it must not really have a location;
12221      try adding a constant value attribute from the DECL_INITIAL.  */
12222   tree_add_const_value_attribute (die, decl);
12223 }
12224
12225 /* Add VARIABLE and DIE into deferred locations list.  */
12226
12227 static void
12228 defer_location (tree variable, dw_die_ref die)
12229 {
12230   deferred_locations entry;
12231   entry.variable = variable;
12232   entry.die = die;
12233   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
12234 }
12235
12236 /* Helper function for tree_add_const_value_attribute.  Natively encode
12237    initializer INIT into an array.  Return true if successful.  */
12238
12239 static bool
12240 native_encode_initializer (tree init, unsigned char *array, int size)
12241 {
12242   tree type;
12243
12244   if (init == NULL_TREE)
12245     return false;
12246
12247   STRIP_NOPS (init);
12248   switch (TREE_CODE (init))
12249     {
12250     case STRING_CST:
12251       type = TREE_TYPE (init);
12252       if (TREE_CODE (type) == ARRAY_TYPE)
12253         {
12254           tree enttype = TREE_TYPE (type);
12255           enum machine_mode mode = TYPE_MODE (enttype);
12256
12257           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
12258             return false;
12259           if (int_size_in_bytes (type) != size)
12260             return false;
12261           if (size > TREE_STRING_LENGTH (init))
12262             {
12263               memcpy (array, TREE_STRING_POINTER (init),
12264                       TREE_STRING_LENGTH (init));
12265               memset (array + TREE_STRING_LENGTH (init),
12266                       '\0', size - TREE_STRING_LENGTH (init));
12267             }
12268           else
12269             memcpy (array, TREE_STRING_POINTER (init), size);
12270           return true;
12271         }
12272       return false;
12273     case CONSTRUCTOR:
12274       type = TREE_TYPE (init);
12275       if (int_size_in_bytes (type) != size)
12276         return false;
12277       if (TREE_CODE (type) == ARRAY_TYPE)
12278         {
12279           HOST_WIDE_INT min_index;
12280           unsigned HOST_WIDE_INT cnt;
12281           int curpos = 0, fieldsize;
12282           constructor_elt *ce;
12283
12284           if (TYPE_DOMAIN (type) == NULL_TREE
12285               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12286             return false;
12287
12288           fieldsize = int_size_in_bytes (TREE_TYPE (type));
12289           if (fieldsize <= 0)
12290             return false;
12291
12292           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12293           memset (array, '\0', size);
12294           for (cnt = 0;
12295                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12296                cnt++)
12297             {
12298               tree val = ce->value;
12299               tree index = ce->index;
12300               int pos = curpos;
12301               if (index && TREE_CODE (index) == RANGE_EXPR)
12302                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12303                       * fieldsize;
12304               else if (index)
12305                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12306
12307               if (val)
12308                 {
12309                   STRIP_NOPS (val);
12310                   if (!native_encode_initializer (val, array + pos, fieldsize))
12311                     return false;
12312                 }
12313               curpos = pos + fieldsize;
12314               if (index && TREE_CODE (index) == RANGE_EXPR)
12315                 {
12316                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12317                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12318                   while (count > 0)
12319                     {
12320                       if (val)
12321                         memcpy (array + curpos, array + pos, fieldsize);
12322                       curpos += fieldsize;
12323                     }
12324                 }
12325               gcc_assert (curpos <= size);
12326             }
12327           return true;
12328         }
12329       else if (TREE_CODE (type) == RECORD_TYPE
12330                || TREE_CODE (type) == UNION_TYPE)
12331         {
12332           tree field = NULL_TREE;
12333           unsigned HOST_WIDE_INT cnt;
12334           constructor_elt *ce;
12335
12336           if (int_size_in_bytes (type) != size)
12337             return false;
12338
12339           if (TREE_CODE (type) == RECORD_TYPE)
12340             field = TYPE_FIELDS (type);
12341
12342           for (cnt = 0;
12343                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12344                cnt++, field = field ? TREE_CHAIN (field) : 0)
12345             {
12346               tree val = ce->value;
12347               int pos, fieldsize;
12348
12349               if (ce->index != 0)
12350                 field = ce->index;
12351
12352               if (val)
12353                 STRIP_NOPS (val);
12354
12355               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12356                 return false;
12357
12358               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12359                   && TYPE_DOMAIN (TREE_TYPE (field))
12360                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12361                 return false;
12362               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12363                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12364                 return false;
12365               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12366               pos = int_byte_position (field);
12367               gcc_assert (pos + fieldsize <= size);
12368               if (val
12369                   && !native_encode_initializer (val, array + pos, fieldsize))
12370                 return false;
12371             }
12372           return true;
12373         }
12374       return false;
12375     case VIEW_CONVERT_EXPR:
12376     case NON_LVALUE_EXPR:
12377       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12378     default:
12379       return native_encode_expr (init, array, size) == size;
12380     }
12381 }
12382
12383 /* If we don't have a copy of this variable in memory for some reason (such
12384    as a C++ member constant that doesn't have an out-of-line definition),
12385    we should tell the debugger about the constant value.  */
12386
12387 static void
12388 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12389 {
12390   tree init;
12391   tree type = TREE_TYPE (decl);
12392   rtx rtl;
12393
12394   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12395     return;
12396
12397   init = DECL_INITIAL (decl);
12398   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12399     /* OK */;
12400   else
12401     return;
12402
12403   rtl = rtl_for_decl_init (init, type);
12404   if (rtl)
12405     add_const_value_attribute (var_die, rtl);
12406   /* If the host and target are sane, try harder.  */
12407   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12408            && initializer_constant_valid_p (init, type))
12409     {
12410       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12411       if (size > 0 && (int) size == size)
12412         {
12413           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12414
12415           if (native_encode_initializer (init, array, size))
12416             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12417         }
12418     }
12419 }
12420
12421 /* Convert the CFI instructions for the current function into a
12422    location list.  This is used for DW_AT_frame_base when we targeting
12423    a dwarf2 consumer that does not support the dwarf3
12424    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12425    expressions.  */
12426
12427 static dw_loc_list_ref
12428 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12429 {
12430   dw_fde_ref fde;
12431   dw_loc_list_ref list, *list_tail;
12432   dw_cfi_ref cfi;
12433   dw_cfa_location last_cfa, next_cfa;
12434   const char *start_label, *last_label, *section;
12435   dw_cfa_location remember;
12436
12437   fde = current_fde ();
12438   gcc_assert (fde != NULL);
12439
12440   section = secname_for_decl (current_function_decl);
12441   list_tail = &list;
12442   list = NULL;
12443
12444   memset (&next_cfa, 0, sizeof (next_cfa));
12445   next_cfa.reg = INVALID_REGNUM;
12446   remember = next_cfa;
12447
12448   start_label = fde->dw_fde_begin;
12449
12450   /* ??? Bald assumption that the CIE opcode list does not contain
12451      advance opcodes.  */
12452   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12453     lookup_cfa_1 (cfi, &next_cfa, &remember);
12454
12455   last_cfa = next_cfa;
12456   last_label = start_label;
12457
12458   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12459     switch (cfi->dw_cfi_opc)
12460       {
12461       case DW_CFA_set_loc:
12462       case DW_CFA_advance_loc1:
12463       case DW_CFA_advance_loc2:
12464       case DW_CFA_advance_loc4:
12465         if (!cfa_equal_p (&last_cfa, &next_cfa))
12466           {
12467             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12468                                        start_label, last_label, section,
12469                                        list == NULL);
12470
12471             list_tail = &(*list_tail)->dw_loc_next;
12472             last_cfa = next_cfa;
12473             start_label = last_label;
12474           }
12475         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12476         break;
12477
12478       case DW_CFA_advance_loc:
12479         /* The encoding is complex enough that we should never emit this.  */
12480         gcc_unreachable ();
12481
12482       default:
12483         lookup_cfa_1 (cfi, &next_cfa, &remember);
12484         break;
12485       }
12486
12487   if (!cfa_equal_p (&last_cfa, &next_cfa))
12488     {
12489       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12490                                  start_label, last_label, section,
12491                                  list == NULL);
12492       list_tail = &(*list_tail)->dw_loc_next;
12493       start_label = last_label;
12494     }
12495   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12496                              start_label, fde->dw_fde_end, section,
12497                              list == NULL);
12498
12499   return list;
12500 }
12501
12502 /* Compute a displacement from the "steady-state frame pointer" to the
12503    frame base (often the same as the CFA), and store it in
12504    frame_pointer_fb_offset.  OFFSET is added to the displacement
12505    before the latter is negated.  */
12506
12507 static void
12508 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12509 {
12510   rtx reg, elim;
12511
12512 #ifdef FRAME_POINTER_CFA_OFFSET
12513   reg = frame_pointer_rtx;
12514   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12515 #else
12516   reg = arg_pointer_rtx;
12517   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12518 #endif
12519
12520   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12521   if (GET_CODE (elim) == PLUS)
12522     {
12523       offset += INTVAL (XEXP (elim, 1));
12524       elim = XEXP (elim, 0);
12525     }
12526
12527   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12528                && (elim == hard_frame_pointer_rtx
12529                    || elim == stack_pointer_rtx))
12530               || elim == (frame_pointer_needed
12531                           ? hard_frame_pointer_rtx
12532                           : stack_pointer_rtx));
12533
12534   frame_pointer_fb_offset = -offset;
12535 }
12536
12537 /* Generate a DW_AT_name attribute given some string value to be included as
12538    the value of the attribute.  */
12539
12540 static void
12541 add_name_attribute (dw_die_ref die, const char *name_string)
12542 {
12543   if (name_string != NULL && *name_string != 0)
12544     {
12545       if (demangle_name_func)
12546         name_string = (*demangle_name_func) (name_string);
12547
12548       add_AT_string (die, DW_AT_name, name_string);
12549     }
12550 }
12551
12552 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12553
12554 static void
12555 add_comp_dir_attribute (dw_die_ref die)
12556 {
12557   const char *wd = get_src_pwd ();
12558   if (wd != NULL)
12559     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12560 }
12561
12562 /* Given a tree node describing an array bound (either lower or upper) output
12563    a representation for that bound.  */
12564
12565 static void
12566 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12567 {
12568   switch (TREE_CODE (bound))
12569     {
12570     case ERROR_MARK:
12571       return;
12572
12573     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12574     case INTEGER_CST:
12575       if (! host_integerp (bound, 0)
12576           || (bound_attr == DW_AT_lower_bound
12577               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12578                   || (is_fortran () && integer_onep (bound)))))
12579         /* Use the default.  */
12580         ;
12581       else
12582         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12583       break;
12584
12585     CASE_CONVERT:
12586     case VIEW_CONVERT_EXPR:
12587       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12588       break;
12589
12590     case SAVE_EXPR:
12591       break;
12592
12593     case VAR_DECL:
12594     case PARM_DECL:
12595     case RESULT_DECL:
12596       {
12597         dw_die_ref decl_die = lookup_decl_die (bound);
12598         dw_loc_descr_ref loc;
12599
12600         /* ??? Can this happen, or should the variable have been bound
12601            first?  Probably it can, since I imagine that we try to create
12602            the types of parameters in the order in which they exist in
12603            the list, and won't have created a forward reference to a
12604            later parameter.  */
12605         if (decl_die != NULL)
12606           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12607         else
12608           {
12609             loc = loc_descriptor_from_tree_1 (bound, 0);
12610             add_AT_location_description (subrange_die, bound_attr, loc);
12611           }
12612         break;
12613       }
12614
12615     default:
12616       {
12617         /* Otherwise try to create a stack operation procedure to
12618            evaluate the value of the array bound.  */
12619
12620         dw_die_ref ctx, decl_die;
12621         dw_loc_descr_ref loc;
12622
12623         loc = loc_descriptor_from_tree (bound);
12624         if (loc == NULL)
12625           break;
12626
12627         if (current_function_decl == 0)
12628           ctx = comp_unit_die;
12629         else
12630           ctx = lookup_decl_die (current_function_decl);
12631
12632         decl_die = new_die (DW_TAG_variable, ctx, bound);
12633         add_AT_flag (decl_die, DW_AT_artificial, 1);
12634         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12635         add_AT_loc (decl_die, DW_AT_location, loc);
12636
12637         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12638         break;
12639       }
12640     }
12641 }
12642
12643 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12644    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12645    Note that the block of subscript information for an array type also
12646    includes information about the element type of the given array type.  */
12647
12648 static void
12649 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12650 {
12651   unsigned dimension_number;
12652   tree lower, upper;
12653   dw_die_ref subrange_die;
12654
12655   for (dimension_number = 0;
12656        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12657        type = TREE_TYPE (type), dimension_number++)
12658     {
12659       tree domain = TYPE_DOMAIN (type);
12660
12661       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12662         break;
12663
12664       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12665          and (in GNU C only) variable bounds.  Handle all three forms
12666          here.  */
12667       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12668       if (domain)
12669         {
12670           /* We have an array type with specified bounds.  */
12671           lower = TYPE_MIN_VALUE (domain);
12672           upper = TYPE_MAX_VALUE (domain);
12673
12674           /* Define the index type.  */
12675           if (TREE_TYPE (domain))
12676             {
12677               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12678                  TREE_TYPE field.  We can't emit debug info for this
12679                  because it is an unnamed integral type.  */
12680               if (TREE_CODE (domain) == INTEGER_TYPE
12681                   && TYPE_NAME (domain) == NULL_TREE
12682                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12683                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12684                 ;
12685               else
12686                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12687                                     type_die);
12688             }
12689
12690           /* ??? If upper is NULL, the array has unspecified length,
12691              but it does have a lower bound.  This happens with Fortran
12692                dimension arr(N:*)
12693              Since the debugger is definitely going to need to know N
12694              to produce useful results, go ahead and output the lower
12695              bound solo, and hope the debugger can cope.  */
12696
12697           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12698           if (upper)
12699             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12700         }
12701
12702       /* Otherwise we have an array type with an unspecified length.  The
12703          DWARF-2 spec does not say how to handle this; let's just leave out the
12704          bounds.  */
12705     }
12706 }
12707
12708 static void
12709 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12710 {
12711   unsigned size;
12712
12713   switch (TREE_CODE (tree_node))
12714     {
12715     case ERROR_MARK:
12716       size = 0;
12717       break;
12718     case ENUMERAL_TYPE:
12719     case RECORD_TYPE:
12720     case UNION_TYPE:
12721     case QUAL_UNION_TYPE:
12722       size = int_size_in_bytes (tree_node);
12723       break;
12724     case FIELD_DECL:
12725       /* For a data member of a struct or union, the DW_AT_byte_size is
12726          generally given as the number of bytes normally allocated for an
12727          object of the *declared* type of the member itself.  This is true
12728          even for bit-fields.  */
12729       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12730       break;
12731     default:
12732       gcc_unreachable ();
12733     }
12734
12735   /* Note that `size' might be -1 when we get to this point.  If it is, that
12736      indicates that the byte size of the entity in question is variable.  We
12737      have no good way of expressing this fact in Dwarf at the present time,
12738      so just let the -1 pass on through.  */
12739   add_AT_unsigned (die, DW_AT_byte_size, size);
12740 }
12741
12742 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12743    which specifies the distance in bits from the highest order bit of the
12744    "containing object" for the bit-field to the highest order bit of the
12745    bit-field itself.
12746
12747    For any given bit-field, the "containing object" is a hypothetical object
12748    (of some integral or enum type) within which the given bit-field lives.  The
12749    type of this hypothetical "containing object" is always the same as the
12750    declared type of the individual bit-field itself.  The determination of the
12751    exact location of the "containing object" for a bit-field is rather
12752    complicated.  It's handled by the `field_byte_offset' function (above).
12753
12754    Note that it is the size (in bytes) of the hypothetical "containing object"
12755    which will be given in the DW_AT_byte_size attribute for this bit-field.
12756    (See `byte_size_attribute' above).  */
12757
12758 static inline void
12759 add_bit_offset_attribute (dw_die_ref die, tree decl)
12760 {
12761   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12762   tree type = DECL_BIT_FIELD_TYPE (decl);
12763   HOST_WIDE_INT bitpos_int;
12764   HOST_WIDE_INT highest_order_object_bit_offset;
12765   HOST_WIDE_INT highest_order_field_bit_offset;
12766   HOST_WIDE_INT unsigned bit_offset;
12767
12768   /* Must be a field and a bit field.  */
12769   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12770
12771   /* We can't yet handle bit-fields whose offsets are variable, so if we
12772      encounter such things, just return without generating any attribute
12773      whatsoever.  Likewise for variable or too large size.  */
12774   if (! host_integerp (bit_position (decl), 0)
12775       || ! host_integerp (DECL_SIZE (decl), 1))
12776     return;
12777
12778   bitpos_int = int_bit_position (decl);
12779
12780   /* Note that the bit offset is always the distance (in bits) from the
12781      highest-order bit of the "containing object" to the highest-order bit of
12782      the bit-field itself.  Since the "high-order end" of any object or field
12783      is different on big-endian and little-endian machines, the computation
12784      below must take account of these differences.  */
12785   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12786   highest_order_field_bit_offset = bitpos_int;
12787
12788   if (! BYTES_BIG_ENDIAN)
12789     {
12790       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12791       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12792     }
12793
12794   bit_offset
12795     = (! BYTES_BIG_ENDIAN
12796        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12797        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12798
12799   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12800 }
12801
12802 /* For a FIELD_DECL node which represents a bit field, output an attribute
12803    which specifies the length in bits of the given field.  */
12804
12805 static inline void
12806 add_bit_size_attribute (dw_die_ref die, tree decl)
12807 {
12808   /* Must be a field and a bit field.  */
12809   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12810               && DECL_BIT_FIELD_TYPE (decl));
12811
12812   if (host_integerp (DECL_SIZE (decl), 1))
12813     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12814 }
12815
12816 /* If the compiled language is ANSI C, then add a 'prototyped'
12817    attribute, if arg types are given for the parameters of a function.  */
12818
12819 static inline void
12820 add_prototyped_attribute (dw_die_ref die, tree func_type)
12821 {
12822   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12823       && TYPE_ARG_TYPES (func_type) != NULL)
12824     add_AT_flag (die, DW_AT_prototyped, 1);
12825 }
12826
12827 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12828    by looking in either the type declaration or object declaration
12829    equate table.  */
12830
12831 static inline dw_die_ref
12832 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12833 {
12834   dw_die_ref origin_die = NULL;
12835
12836   if (TREE_CODE (origin) != FUNCTION_DECL)
12837     {
12838       /* We may have gotten separated from the block for the inlined
12839          function, if we're in an exception handler or some such; make
12840          sure that the abstract function has been written out.
12841
12842          Doing this for nested functions is wrong, however; functions are
12843          distinct units, and our context might not even be inline.  */
12844       tree fn = origin;
12845
12846       if (TYPE_P (fn))
12847         fn = TYPE_STUB_DECL (fn);
12848
12849       fn = decl_function_context (fn);
12850       if (fn)
12851         dwarf2out_abstract_function (fn);
12852     }
12853
12854   if (DECL_P (origin))
12855     origin_die = lookup_decl_die (origin);
12856   else if (TYPE_P (origin))
12857     origin_die = lookup_type_die (origin);
12858
12859   /* XXX: Functions that are never lowered don't always have correct block
12860      trees (in the case of java, they simply have no block tree, in some other
12861      languages).  For these functions, there is nothing we can really do to
12862      output correct debug info for inlined functions in all cases.  Rather
12863      than die, we'll just produce deficient debug info now, in that we will
12864      have variables without a proper abstract origin.  In the future, when all
12865      functions are lowered, we should re-add a gcc_assert (origin_die)
12866      here.  */
12867
12868   if (origin_die)
12869     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12870   return origin_die;
12871 }
12872
12873 /* We do not currently support the pure_virtual attribute.  */
12874
12875 static inline void
12876 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12877 {
12878   if (DECL_VINDEX (func_decl))
12879     {
12880       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12881
12882       if (host_integerp (DECL_VINDEX (func_decl), 0))
12883         add_AT_loc (die, DW_AT_vtable_elem_location,
12884                     new_loc_descr (DW_OP_constu,
12885                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12886                                    0));
12887
12888       /* GNU extension: Record what type this method came from originally.  */
12889       if (debug_info_level > DINFO_LEVEL_TERSE)
12890         add_AT_die_ref (die, DW_AT_containing_type,
12891                         lookup_type_die (DECL_CONTEXT (func_decl)));
12892     }
12893 }
12894 \f
12895 /* Add source coordinate attributes for the given decl.  */
12896
12897 static void
12898 add_src_coords_attributes (dw_die_ref die, tree decl)
12899 {
12900   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12901
12902   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12903   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12904 }
12905
12906 /* Add a DW_AT_name attribute and source coordinate attribute for the
12907    given decl, but only if it actually has a name.  */
12908
12909 static void
12910 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12911 {
12912   tree decl_name;
12913
12914   decl_name = DECL_NAME (decl);
12915   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12916     {
12917       add_name_attribute (die, dwarf2_name (decl, 0));
12918       if (! DECL_ARTIFICIAL (decl))
12919         add_src_coords_attributes (die, decl);
12920
12921       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12922           && TREE_PUBLIC (decl)
12923           && !DECL_ABSTRACT (decl)
12924           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12925           && !is_fortran ())
12926         {
12927           /* Defer until we have an assembler name set.  */
12928           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
12929             {
12930               limbo_die_node *asm_name;
12931
12932               asm_name = GGC_CNEW (limbo_die_node);
12933               asm_name->die = die;
12934               asm_name->created_for = decl;
12935               asm_name->next = deferred_asm_name;
12936               deferred_asm_name = asm_name;
12937             }
12938           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
12939             add_AT_string (die, DW_AT_MIPS_linkage_name,
12940                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12941         }
12942     }
12943
12944 #ifdef VMS_DEBUGGING_INFO
12945   /* Get the function's name, as described by its RTL.  This may be different
12946      from the DECL_NAME name used in the source file.  */
12947   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12948     {
12949       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12950                    XEXP (DECL_RTL (decl), 0));
12951       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12952     }
12953 #endif
12954 }
12955
12956 /* Push a new declaration scope.  */
12957
12958 static void
12959 push_decl_scope (tree scope)
12960 {
12961   VEC_safe_push (tree, gc, decl_scope_table, scope);
12962 }
12963
12964 /* Pop a declaration scope.  */
12965
12966 static inline void
12967 pop_decl_scope (void)
12968 {
12969   VEC_pop (tree, decl_scope_table);
12970 }
12971
12972 /* Return the DIE for the scope that immediately contains this type.
12973    Non-named types get global scope.  Named types nested in other
12974    types get their containing scope if it's open, or global scope
12975    otherwise.  All other types (i.e. function-local named types) get
12976    the current active scope.  */
12977
12978 static dw_die_ref
12979 scope_die_for (tree t, dw_die_ref context_die)
12980 {
12981   dw_die_ref scope_die = NULL;
12982   tree containing_scope;
12983   int i;
12984
12985   /* Non-types always go in the current scope.  */
12986   gcc_assert (TYPE_P (t));
12987
12988   containing_scope = TYPE_CONTEXT (t);
12989
12990   /* Use the containing namespace if it was passed in (for a declaration).  */
12991   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12992     {
12993       if (context_die == lookup_decl_die (containing_scope))
12994         /* OK */;
12995       else
12996         containing_scope = NULL_TREE;
12997     }
12998
12999   /* Ignore function type "scopes" from the C frontend.  They mean that
13000      a tagged type is local to a parmlist of a function declarator, but
13001      that isn't useful to DWARF.  */
13002   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
13003     containing_scope = NULL_TREE;
13004
13005   if (containing_scope == NULL_TREE)
13006     scope_die = comp_unit_die;
13007   else if (TYPE_P (containing_scope))
13008     {
13009       /* For types, we can just look up the appropriate DIE.  But
13010          first we check to see if we're in the middle of emitting it
13011          so we know where the new DIE should go.  */
13012       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
13013         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
13014           break;
13015
13016       if (i < 0)
13017         {
13018           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
13019                       || TREE_ASM_WRITTEN (containing_scope));
13020
13021           /* If none of the current dies are suitable, we get file scope.  */
13022           scope_die = comp_unit_die;
13023         }
13024       else
13025         scope_die = lookup_type_die (containing_scope);
13026     }
13027   else
13028     scope_die = context_die;
13029
13030   return scope_die;
13031 }
13032
13033 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
13034
13035 static inline int
13036 local_scope_p (dw_die_ref context_die)
13037 {
13038   for (; context_die; context_die = context_die->die_parent)
13039     if (context_die->die_tag == DW_TAG_inlined_subroutine
13040         || context_die->die_tag == DW_TAG_subprogram)
13041       return 1;
13042
13043   return 0;
13044 }
13045
13046 /* Returns nonzero if CONTEXT_DIE is a class.  */
13047
13048 static inline int
13049 class_scope_p (dw_die_ref context_die)
13050 {
13051   return (context_die
13052           && (context_die->die_tag == DW_TAG_structure_type
13053               || context_die->die_tag == DW_TAG_class_type
13054               || context_die->die_tag == DW_TAG_interface_type
13055               || context_die->die_tag == DW_TAG_union_type));
13056 }
13057
13058 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
13059    whether or not to treat a DIE in this context as a declaration.  */
13060
13061 static inline int
13062 class_or_namespace_scope_p (dw_die_ref context_die)
13063 {
13064   return (class_scope_p (context_die)
13065           || (context_die && context_die->die_tag == DW_TAG_namespace));
13066 }
13067
13068 /* Many forms of DIEs require a "type description" attribute.  This
13069    routine locates the proper "type descriptor" die for the type given
13070    by 'type', and adds a DW_AT_type attribute below the given die.  */
13071
13072 static void
13073 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
13074                     int decl_volatile, dw_die_ref context_die)
13075 {
13076   enum tree_code code  = TREE_CODE (type);
13077   dw_die_ref type_die  = NULL;
13078
13079   /* ??? If this type is an unnamed subrange type of an integral, floating-point
13080      or fixed-point type, use the inner type.  This is because we have no
13081      support for unnamed types in base_type_die.  This can happen if this is
13082      an Ada subrange type.  Correct solution is emit a subrange type die.  */
13083   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
13084       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
13085     type = TREE_TYPE (type), code = TREE_CODE (type);
13086
13087   if (code == ERROR_MARK
13088       /* Handle a special case.  For functions whose return type is void, we
13089          generate *no* type attribute.  (Note that no object may have type
13090          `void', so this only applies to function return types).  */
13091       || code == VOID_TYPE)
13092     return;
13093
13094   type_die = modified_type_die (type,
13095                                 decl_const || TYPE_READONLY (type),
13096                                 decl_volatile || TYPE_VOLATILE (type),
13097                                 context_die);
13098
13099   if (type_die != NULL)
13100     add_AT_die_ref (object_die, DW_AT_type, type_die);
13101 }
13102
13103 /* Given an object die, add the calling convention attribute for the
13104    function call type.  */
13105 static void
13106 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
13107 {
13108   enum dwarf_calling_convention value = DW_CC_normal;
13109
13110   value = ((enum dwarf_calling_convention)
13111            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
13112
13113   /* DWARF doesn't provide a way to identify a program's source-level
13114      entry point.  DW_AT_calling_convention attributes are only meant
13115      to describe functions' calling conventions.  However, lacking a
13116      better way to signal the Fortran main program, we use this for the
13117      time being, following existing custom.  */
13118   if (is_fortran ()
13119       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
13120     value = DW_CC_program;
13121
13122   /* Only add the attribute if the backend requests it, and
13123      is not DW_CC_normal.  */
13124   if (value && (value != DW_CC_normal))
13125     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
13126 }
13127
13128 /* Given a tree pointer to a struct, class, union, or enum type node, return
13129    a pointer to the (string) tag name for the given type, or zero if the type
13130    was declared without a tag.  */
13131
13132 static const char *
13133 type_tag (const_tree type)
13134 {
13135   const char *name = 0;
13136
13137   if (TYPE_NAME (type) != 0)
13138     {
13139       tree t = 0;
13140
13141       /* Find the IDENTIFIER_NODE for the type name.  */
13142       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
13143         t = TYPE_NAME (type);
13144
13145       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
13146          a TYPE_DECL node, regardless of whether or not a `typedef' was
13147          involved.  */
13148       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13149                && ! DECL_IGNORED_P (TYPE_NAME (type)))
13150         {
13151           /* We want to be extra verbose.  Don't call dwarf_name if
13152              DECL_NAME isn't set.  The default hook for decl_printable_name
13153              doesn't like that, and in this context it's correct to return
13154              0, instead of "<anonymous>" or the like.  */
13155           if (DECL_NAME (TYPE_NAME (type)))
13156             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
13157         }
13158
13159       /* Now get the name as a string, or invent one.  */
13160       if (!name && t != 0)
13161         name = IDENTIFIER_POINTER (t);
13162     }
13163
13164   return (name == 0 || *name == '\0') ? 0 : name;
13165 }
13166
13167 /* Return the type associated with a data member, make a special check
13168    for bit field types.  */
13169
13170 static inline tree
13171 member_declared_type (const_tree member)
13172 {
13173   return (DECL_BIT_FIELD_TYPE (member)
13174           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
13175 }
13176
13177 /* Get the decl's label, as described by its RTL. This may be different
13178    from the DECL_NAME name used in the source file.  */
13179
13180 #if 0
13181 static const char *
13182 decl_start_label (tree decl)
13183 {
13184   rtx x;
13185   const char *fnname;
13186
13187   x = DECL_RTL (decl);
13188   gcc_assert (MEM_P (x));
13189
13190   x = XEXP (x, 0);
13191   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13192
13193   fnname = XSTR (x, 0);
13194   return fnname;
13195 }
13196 #endif
13197 \f
13198 /* These routines generate the internal representation of the DIE's for
13199    the compilation unit.  Debugging information is collected by walking
13200    the declaration trees passed in from dwarf2out_decl().  */
13201
13202 static void
13203 gen_array_type_die (tree type, dw_die_ref context_die)
13204 {
13205   dw_die_ref scope_die = scope_die_for (type, context_die);
13206   dw_die_ref array_die;
13207
13208   /* GNU compilers represent multidimensional array types as sequences of one
13209      dimensional array types whose element types are themselves array types.
13210      We sometimes squish that down to a single array_type DIE with multiple
13211      subscripts in the Dwarf debugging info.  The draft Dwarf specification
13212      say that we are allowed to do this kind of compression in C, because
13213      there is no difference between an array of arrays and a multidimensional
13214      array.  We don't do this for Ada to remain as close as possible to the
13215      actual representation, which is especially important against the language
13216      flexibilty wrt arrays of variable size.  */
13217
13218   bool collapse_nested_arrays = !is_ada ();
13219   tree element_type;
13220
13221   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
13222      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
13223   if (TYPE_STRING_FLAG (type)
13224       && TREE_CODE (type) == ARRAY_TYPE
13225       && is_fortran ()
13226       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
13227     {
13228       HOST_WIDE_INT size;
13229
13230       array_die = new_die (DW_TAG_string_type, scope_die, type);
13231       add_name_attribute (array_die, type_tag (type));
13232       equate_type_number_to_die (type, array_die);
13233       size = int_size_in_bytes (type);
13234       if (size >= 0)
13235         add_AT_unsigned (array_die, DW_AT_byte_size, size);
13236       else if (TYPE_DOMAIN (type) != NULL_TREE
13237                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
13238                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
13239         {
13240           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13241           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
13242
13243           size = int_size_in_bytes (TREE_TYPE (szdecl));
13244           if (loc && size > 0)
13245             {
13246               add_AT_loc (array_die, DW_AT_string_length, loc);
13247               if (size != DWARF2_ADDR_SIZE)
13248                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13249             }
13250         }
13251       return;
13252     }
13253
13254   /* ??? The SGI dwarf reader fails for array of array of enum types
13255      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
13256      array type comes before the outer array type.  We thus call gen_type_die
13257      before we new_die and must prevent nested array types collapsing for this
13258      target.  */
13259
13260 #ifdef MIPS_DEBUGGING_INFO
13261   gen_type_die (TREE_TYPE (type), context_die);
13262   collapse_nested_arrays = false;
13263 #endif
13264
13265   array_die = new_die (DW_TAG_array_type, scope_die, type);
13266   add_name_attribute (array_die, type_tag (type));
13267   equate_type_number_to_die (type, array_die);
13268
13269   if (TREE_CODE (type) == VECTOR_TYPE)
13270     {
13271       /* The frontend feeds us a representation for the vector as a struct
13272          containing an array.  Pull out the array type.  */
13273       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
13274       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
13275     }
13276
13277   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13278   if (is_fortran ()
13279       && TREE_CODE (type) == ARRAY_TYPE
13280       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
13281       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
13282     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13283
13284 #if 0
13285   /* We default the array ordering.  SDB will probably do
13286      the right things even if DW_AT_ordering is not present.  It's not even
13287      an issue until we start to get into multidimensional arrays anyway.  If
13288      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
13289      then we'll have to put the DW_AT_ordering attribute back in.  (But if
13290      and when we find out that we need to put these in, we will only do so
13291      for multidimensional arrays.  */
13292   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
13293 #endif
13294
13295 #ifdef MIPS_DEBUGGING_INFO
13296   /* The SGI compilers handle arrays of unknown bound by setting
13297      AT_declaration and not emitting any subrange DIEs.  */
13298   if (! TYPE_DOMAIN (type))
13299     add_AT_flag (array_die, DW_AT_declaration, 1);
13300   else
13301 #endif
13302     add_subscript_info (array_die, type, collapse_nested_arrays);
13303
13304   /* Add representation of the type of the elements of this array type and
13305      emit the corresponding DIE if we haven't done it already.  */  
13306   element_type = TREE_TYPE (type);
13307   if (collapse_nested_arrays)
13308     while (TREE_CODE (element_type) == ARRAY_TYPE)
13309       {
13310         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13311           break;
13312         element_type = TREE_TYPE (element_type);
13313       }
13314
13315 #ifndef MIPS_DEBUGGING_INFO
13316   gen_type_die (element_type, context_die);
13317 #endif
13318
13319   add_type_attribute (array_die, element_type, 0, 0, context_die);
13320
13321   if (get_AT (array_die, DW_AT_name))
13322     add_pubtype (type, array_die);
13323 }
13324
13325 static dw_loc_descr_ref
13326 descr_info_loc (tree val, tree base_decl)
13327 {
13328   HOST_WIDE_INT size;
13329   dw_loc_descr_ref loc, loc2;
13330   enum dwarf_location_atom op;
13331
13332   if (val == base_decl)
13333     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13334
13335   switch (TREE_CODE (val))
13336     {
13337     CASE_CONVERT:
13338       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13339     case VAR_DECL:
13340       return loc_descriptor_from_tree_1 (val, 0);
13341     case INTEGER_CST:
13342       if (host_integerp (val, 0))
13343         return int_loc_descriptor (tree_low_cst (val, 0));
13344       break;
13345     case INDIRECT_REF:
13346       size = int_size_in_bytes (TREE_TYPE (val));
13347       if (size < 0)
13348         break;
13349       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13350       if (!loc)
13351         break;
13352       if (size == DWARF2_ADDR_SIZE)
13353         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13354       else
13355         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13356       return loc;
13357     case POINTER_PLUS_EXPR:
13358     case PLUS_EXPR:
13359       if (host_integerp (TREE_OPERAND (val, 1), 1)
13360           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13361              < 16384)
13362         {
13363           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13364           if (!loc)
13365             break;
13366           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
13367         }
13368       else
13369         {
13370           op = DW_OP_plus;
13371         do_binop:
13372           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13373           if (!loc)
13374             break;
13375           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13376           if (!loc2)
13377             break;
13378           add_loc_descr (&loc, loc2);
13379           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13380         }
13381       return loc;
13382     case MINUS_EXPR:
13383       op = DW_OP_minus;
13384       goto do_binop;
13385     case MULT_EXPR:
13386       op = DW_OP_mul;
13387       goto do_binop;
13388     case EQ_EXPR:
13389       op = DW_OP_eq;
13390       goto do_binop;
13391     case NE_EXPR:
13392       op = DW_OP_ne;
13393       goto do_binop;
13394     default:
13395       break;
13396     }
13397   return NULL;
13398 }
13399
13400 static void
13401 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13402                       tree val, tree base_decl)
13403 {
13404   dw_loc_descr_ref loc;
13405
13406   if (host_integerp (val, 0))
13407     {
13408       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13409       return;
13410     }
13411
13412   loc = descr_info_loc (val, base_decl);
13413   if (!loc)
13414     return;
13415
13416   add_AT_loc (die, attr, loc);
13417 }
13418
13419 /* This routine generates DIE for array with hidden descriptor, details
13420    are filled into *info by a langhook.  */
13421
13422 static void
13423 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13424                           dw_die_ref context_die)
13425 {
13426   dw_die_ref scope_die = scope_die_for (type, context_die);
13427   dw_die_ref array_die;
13428   int dim;
13429
13430   array_die = new_die (DW_TAG_array_type, scope_die, type);
13431   add_name_attribute (array_die, type_tag (type));
13432   equate_type_number_to_die (type, array_die);
13433
13434   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13435   if (is_fortran ()
13436       && info->ndimensions >= 2)
13437     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13438
13439   if (info->data_location)
13440     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13441                           info->base_decl);
13442   if (info->associated)
13443     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13444                           info->base_decl);
13445   if (info->allocated)
13446     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13447                           info->base_decl);
13448
13449   for (dim = 0; dim < info->ndimensions; dim++)
13450     {
13451       dw_die_ref subrange_die
13452         = new_die (DW_TAG_subrange_type, array_die, NULL);
13453
13454       if (info->dimen[dim].lower_bound)
13455         {
13456           /* If it is the default value, omit it.  */
13457           if ((is_c_family () || is_java ())
13458               && integer_zerop (info->dimen[dim].lower_bound))
13459             ;
13460           else if (is_fortran ()
13461                    && integer_onep (info->dimen[dim].lower_bound))
13462             ;
13463           else
13464             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13465                                   info->dimen[dim].lower_bound,
13466                                   info->base_decl);
13467         }
13468       if (info->dimen[dim].upper_bound)
13469         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13470                               info->dimen[dim].upper_bound,
13471                               info->base_decl);
13472       if (info->dimen[dim].stride)
13473         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13474                               info->dimen[dim].stride,
13475                               info->base_decl);
13476     }
13477
13478   gen_type_die (info->element_type, context_die);
13479   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13480
13481   if (get_AT (array_die, DW_AT_name))
13482     add_pubtype (type, array_die);
13483 }
13484
13485 #if 0
13486 static void
13487 gen_entry_point_die (tree decl, dw_die_ref context_die)
13488 {
13489   tree origin = decl_ultimate_origin (decl);
13490   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13491
13492   if (origin != NULL)
13493     add_abstract_origin_attribute (decl_die, origin);
13494   else
13495     {
13496       add_name_and_src_coords_attributes (decl_die, decl);
13497       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13498                           0, 0, context_die);
13499     }
13500
13501   if (DECL_ABSTRACT (decl))
13502     equate_decl_number_to_die (decl, decl_die);
13503   else
13504     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13505 }
13506 #endif
13507
13508 /* Walk through the list of incomplete types again, trying once more to
13509    emit full debugging info for them.  */
13510
13511 static void
13512 retry_incomplete_types (void)
13513 {
13514   int i;
13515
13516   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13517     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13518 }
13519
13520 /* Determine what tag to use for a record type.  */
13521
13522 static enum dwarf_tag
13523 record_type_tag (tree type)
13524 {
13525   if (! lang_hooks.types.classify_record)
13526     return DW_TAG_structure_type;
13527
13528   switch (lang_hooks.types.classify_record (type))
13529     {
13530     case RECORD_IS_STRUCT:
13531       return DW_TAG_structure_type;
13532
13533     case RECORD_IS_CLASS:
13534       return DW_TAG_class_type;
13535
13536     case RECORD_IS_INTERFACE:
13537       return DW_TAG_interface_type;
13538
13539     default:
13540       gcc_unreachable ();
13541     }
13542 }
13543
13544 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13545    include all of the information about the enumeration values also. Each
13546    enumerated type name/value is listed as a child of the enumerated type
13547    DIE.  */
13548
13549 static dw_die_ref
13550 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13551 {
13552   dw_die_ref type_die = lookup_type_die (type);
13553
13554   if (type_die == NULL)
13555     {
13556       type_die = new_die (DW_TAG_enumeration_type,
13557                           scope_die_for (type, context_die), type);
13558       equate_type_number_to_die (type, type_die);
13559       add_name_attribute (type_die, type_tag (type));
13560     }
13561   else if (! TYPE_SIZE (type))
13562     return type_die;
13563   else
13564     remove_AT (type_die, DW_AT_declaration);
13565
13566   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13567      given enum type is incomplete, do not generate the DW_AT_byte_size
13568      attribute or the DW_AT_element_list attribute.  */
13569   if (TYPE_SIZE (type))
13570     {
13571       tree link;
13572
13573       TREE_ASM_WRITTEN (type) = 1;
13574       add_byte_size_attribute (type_die, type);
13575       if (TYPE_STUB_DECL (type) != NULL_TREE)
13576         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13577
13578       /* If the first reference to this type was as the return type of an
13579          inline function, then it may not have a parent.  Fix this now.  */
13580       if (type_die->die_parent == NULL)
13581         add_child_die (scope_die_for (type, context_die), type_die);
13582
13583       for (link = TYPE_VALUES (type);
13584            link != NULL; link = TREE_CHAIN (link))
13585         {
13586           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13587           tree value = TREE_VALUE (link);
13588
13589           add_name_attribute (enum_die,
13590                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13591
13592           if (TREE_CODE (value) == CONST_DECL)
13593             value = DECL_INITIAL (value);
13594
13595           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13596             /* DWARF2 does not provide a way of indicating whether or
13597                not enumeration constants are signed or unsigned.  GDB
13598                always assumes the values are signed, so we output all
13599                values as if they were signed.  That means that
13600                enumeration constants with very large unsigned values
13601                will appear to have negative values in the debugger.  */
13602             add_AT_int (enum_die, DW_AT_const_value,
13603                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13604         }
13605     }
13606   else
13607     add_AT_flag (type_die, DW_AT_declaration, 1);
13608
13609   if (get_AT (type_die, DW_AT_name))
13610     add_pubtype (type, type_die);
13611
13612   return type_die;
13613 }
13614
13615 /* Generate a DIE to represent either a real live formal parameter decl or to
13616    represent just the type of some formal parameter position in some function
13617    type.
13618
13619    Note that this routine is a bit unusual because its argument may be a
13620    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13621    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13622    node.  If it's the former then this function is being called to output a
13623    DIE to represent a formal parameter object (or some inlining thereof).  If
13624    it's the latter, then this function is only being called to output a
13625    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13626    argument type of some subprogram type.  */
13627
13628 static dw_die_ref
13629 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13630 {
13631   tree node_or_origin = node ? node : origin;
13632   dw_die_ref parm_die
13633     = new_die (DW_TAG_formal_parameter, context_die, node);
13634
13635   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13636     {
13637     case tcc_declaration:
13638       if (!origin)
13639         origin = decl_ultimate_origin (node);
13640       if (origin != NULL)
13641         add_abstract_origin_attribute (parm_die, origin);
13642       else
13643         {
13644           tree type = TREE_TYPE (node);
13645           add_name_and_src_coords_attributes (parm_die, node);
13646           if (DECL_BY_REFERENCE (node))
13647             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13648                                 context_die);
13649           else
13650             add_type_attribute (parm_die, type,
13651                                 TREE_READONLY (node),
13652                                 TREE_THIS_VOLATILE (node),
13653                                 context_die);
13654           if (DECL_ARTIFICIAL (node))
13655             add_AT_flag (parm_die, DW_AT_artificial, 1);
13656         }
13657
13658       if (node)
13659         equate_decl_number_to_die (node, parm_die);
13660       if (! DECL_ABSTRACT (node_or_origin))
13661         add_location_or_const_value_attribute (parm_die, node_or_origin,
13662                                                DW_AT_location);
13663
13664       break;
13665
13666     case tcc_type:
13667       /* We were called with some kind of a ..._TYPE node.  */
13668       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13669       break;
13670
13671     default:
13672       gcc_unreachable ();
13673     }
13674
13675   return parm_die;
13676 }
13677
13678 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13679    at the end of an (ANSI prototyped) formal parameters list.  */
13680
13681 static void
13682 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13683 {
13684   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13685 }
13686
13687 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13688    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13689    parameters as specified in some function type specification (except for
13690    those which appear as part of a function *definition*).  */
13691
13692 static void
13693 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13694 {
13695   tree link;
13696   tree formal_type = NULL;
13697   tree first_parm_type;
13698   tree arg;
13699
13700   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13701     {
13702       arg = DECL_ARGUMENTS (function_or_method_type);
13703       function_or_method_type = TREE_TYPE (function_or_method_type);
13704     }
13705   else
13706     arg = NULL_TREE;
13707
13708   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13709
13710   /* Make our first pass over the list of formal parameter types and output a
13711      DW_TAG_formal_parameter DIE for each one.  */
13712   for (link = first_parm_type; link; )
13713     {
13714       dw_die_ref parm_die;
13715
13716       formal_type = TREE_VALUE (link);
13717       if (formal_type == void_type_node)
13718         break;
13719
13720       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13721       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13722       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13723            && link == first_parm_type)
13724           || (arg && DECL_ARTIFICIAL (arg)))
13725         add_AT_flag (parm_die, DW_AT_artificial, 1);
13726
13727       link = TREE_CHAIN (link);
13728       if (arg)
13729         arg = TREE_CHAIN (arg);
13730     }
13731
13732   /* If this function type has an ellipsis, add a
13733      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13734   if (formal_type != void_type_node)
13735     gen_unspecified_parameters_die (function_or_method_type, context_die);
13736
13737   /* Make our second (and final) pass over the list of formal parameter types
13738      and output DIEs to represent those types (as necessary).  */
13739   for (link = TYPE_ARG_TYPES (function_or_method_type);
13740        link && TREE_VALUE (link);
13741        link = TREE_CHAIN (link))
13742     gen_type_die (TREE_VALUE (link), context_die);
13743 }
13744
13745 /* We want to generate the DIE for TYPE so that we can generate the
13746    die for MEMBER, which has been defined; we will need to refer back
13747    to the member declaration nested within TYPE.  If we're trying to
13748    generate minimal debug info for TYPE, processing TYPE won't do the
13749    trick; we need to attach the member declaration by hand.  */
13750
13751 static void
13752 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13753 {
13754   gen_type_die (type, context_die);
13755
13756   /* If we're trying to avoid duplicate debug info, we may not have
13757      emitted the member decl for this function.  Emit it now.  */
13758   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13759       && ! lookup_decl_die (member))
13760     {
13761       dw_die_ref type_die;
13762       gcc_assert (!decl_ultimate_origin (member));
13763
13764       push_decl_scope (type);
13765       type_die = lookup_type_die (type);
13766       if (TREE_CODE (member) == FUNCTION_DECL)
13767         gen_subprogram_die (member, type_die);
13768       else if (TREE_CODE (member) == FIELD_DECL)
13769         {
13770           /* Ignore the nameless fields that are used to skip bits but handle
13771              C++ anonymous unions and structs.  */
13772           if (DECL_NAME (member) != NULL_TREE
13773               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13774               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13775             {
13776               gen_type_die (member_declared_type (member), type_die);
13777               gen_field_die (member, type_die);
13778             }
13779         }
13780       else
13781         gen_variable_die (member, NULL_TREE, type_die);
13782
13783       pop_decl_scope ();
13784     }
13785 }
13786
13787 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13788    may later generate inlined and/or out-of-line instances of.  */
13789
13790 static void
13791 dwarf2out_abstract_function (tree decl)
13792 {
13793   dw_die_ref old_die;
13794   tree save_fn;
13795   tree context;
13796   int was_abstract = DECL_ABSTRACT (decl);
13797
13798   /* Make sure we have the actual abstract inline, not a clone.  */
13799   decl = DECL_ORIGIN (decl);
13800
13801   old_die = lookup_decl_die (decl);
13802   if (old_die && get_AT (old_die, DW_AT_inline))
13803     /* We've already generated the abstract instance.  */
13804     return;
13805
13806   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13807      we don't get confused by DECL_ABSTRACT.  */
13808   if (debug_info_level > DINFO_LEVEL_TERSE)
13809     {
13810       context = decl_class_context (decl);
13811       if (context)
13812         gen_type_die_for_member
13813           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13814     }
13815
13816   /* Pretend we've just finished compiling this function.  */
13817   save_fn = current_function_decl;
13818   current_function_decl = decl;
13819   push_cfun (DECL_STRUCT_FUNCTION (decl));
13820
13821   set_decl_abstract_flags (decl, 1);
13822   dwarf2out_decl (decl);
13823   if (! was_abstract)
13824     set_decl_abstract_flags (decl, 0);
13825
13826   current_function_decl = save_fn;
13827   pop_cfun ();
13828 }
13829
13830 /* Helper function of premark_used_types() which gets called through
13831    htab_traverse_resize().
13832
13833    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13834    marked as unused by prune_unused_types.  */
13835 static int
13836 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13837 {
13838   tree type;
13839   dw_die_ref die;
13840
13841   type = (tree) *slot;
13842   die = lookup_type_die (type);
13843   if (die != NULL)
13844     die->die_perennial_p = 1;
13845   return 1;
13846 }
13847
13848 /* Mark all members of used_types_hash as perennial.  */
13849 static void
13850 premark_used_types (void)
13851 {
13852   if (cfun && cfun->used_types_hash)
13853     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13854 }
13855
13856 /* Generate a DIE to represent a declared function (either file-scope or
13857    block-local).  */
13858
13859 static void
13860 gen_subprogram_die (tree decl, dw_die_ref context_die)
13861 {
13862   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13863   tree origin = decl_ultimate_origin (decl);
13864   dw_die_ref subr_die;
13865   tree fn_arg_types;
13866   tree outer_scope;
13867   dw_die_ref old_die = lookup_decl_die (decl);
13868   int declaration = (current_function_decl != decl
13869                      || class_or_namespace_scope_p (context_die));
13870
13871   premark_used_types ();
13872
13873   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13874      started to generate the abstract instance of an inline, decided to output
13875      its containing class, and proceeded to emit the declaration of the inline
13876      from the member list for the class.  If so, DECLARATION takes priority;
13877      we'll get back to the abstract instance when done with the class.  */
13878
13879   /* The class-scope declaration DIE must be the primary DIE.  */
13880   if (origin && declaration && class_or_namespace_scope_p (context_die))
13881     {
13882       origin = NULL;
13883       gcc_assert (!old_die);
13884     }
13885
13886   /* Now that the C++ front end lazily declares artificial member fns, we
13887      might need to retrofit the declaration into its class.  */
13888   if (!declaration && !origin && !old_die
13889       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13890       && !class_or_namespace_scope_p (context_die)
13891       && debug_info_level > DINFO_LEVEL_TERSE)
13892     old_die = force_decl_die (decl);
13893
13894   if (origin != NULL)
13895     {
13896       gcc_assert (!declaration || local_scope_p (context_die));
13897
13898       /* Fixup die_parent for the abstract instance of a nested
13899          inline function.  */
13900       if (old_die && old_die->die_parent == NULL)
13901         add_child_die (context_die, old_die);
13902
13903       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13904       add_abstract_origin_attribute (subr_die, origin);
13905     }
13906   else if (old_die)
13907     {
13908       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13909       struct dwarf_file_data * file_index = lookup_filename (s.file);
13910
13911       if (!get_AT_flag (old_die, DW_AT_declaration)
13912           /* We can have a normal definition following an inline one in the
13913              case of redefinition of GNU C extern inlines.
13914              It seems reasonable to use AT_specification in this case.  */
13915           && !get_AT (old_die, DW_AT_inline))
13916         {
13917           /* Detect and ignore this case, where we are trying to output
13918              something we have already output.  */
13919           return;
13920         }
13921
13922       /* If the definition comes from the same place as the declaration,
13923          maybe use the old DIE.  We always want the DIE for this function
13924          that has the *_pc attributes to be under comp_unit_die so the
13925          debugger can find it.  We also need to do this for abstract
13926          instances of inlines, since the spec requires the out-of-line copy
13927          to have the same parent.  For local class methods, this doesn't
13928          apply; we just use the old DIE.  */
13929       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13930           && (DECL_ARTIFICIAL (decl)
13931               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13932                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13933                       == (unsigned) s.line))))
13934         {
13935           subr_die = old_die;
13936
13937           /* Clear out the declaration attribute and the formal parameters.
13938              Do not remove all children, because it is possible that this
13939              declaration die was forced using force_decl_die(). In such
13940              cases die that forced declaration die (e.g. TAG_imported_module)
13941              is one of the children that we do not want to remove.  */
13942           remove_AT (subr_die, DW_AT_declaration);
13943           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13944         }
13945       else
13946         {
13947           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13948           add_AT_specification (subr_die, old_die);
13949           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13950             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13951           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13952             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13953         }
13954     }
13955   else
13956     {
13957       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13958
13959       if (TREE_PUBLIC (decl))
13960         add_AT_flag (subr_die, DW_AT_external, 1);
13961
13962       add_name_and_src_coords_attributes (subr_die, decl);
13963       if (debug_info_level > DINFO_LEVEL_TERSE)
13964         {
13965           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13966           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13967                               0, 0, context_die);
13968         }
13969
13970       add_pure_or_virtual_attribute (subr_die, decl);
13971       if (DECL_ARTIFICIAL (decl))
13972         add_AT_flag (subr_die, DW_AT_artificial, 1);
13973
13974       if (TREE_PROTECTED (decl))
13975         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13976       else if (TREE_PRIVATE (decl))
13977         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13978     }
13979
13980   if (declaration)
13981     {
13982       if (!old_die || !get_AT (old_die, DW_AT_inline))
13983         {
13984           add_AT_flag (subr_die, DW_AT_declaration, 1);
13985
13986           /* If this is an explicit function declaration then generate
13987              a DW_AT_explicit attribute.  */
13988           if (lang_hooks.decls.function_decl_explicit_p (decl))
13989             add_AT_flag (subr_die, DW_AT_explicit, 1);
13990
13991           /* The first time we see a member function, it is in the context of
13992              the class to which it belongs.  We make sure of this by emitting
13993              the class first.  The next time is the definition, which is
13994              handled above.  The two may come from the same source text.
13995
13996              Note that force_decl_die() forces function declaration die. It is
13997              later reused to represent definition.  */
13998           equate_decl_number_to_die (decl, subr_die);
13999         }
14000     }
14001   else if (DECL_ABSTRACT (decl))
14002     {
14003       if (DECL_DECLARED_INLINE_P (decl))
14004         {
14005           if (cgraph_function_possibly_inlined_p (decl))
14006             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
14007           else
14008             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
14009         }
14010       else
14011         {
14012           if (cgraph_function_possibly_inlined_p (decl))
14013             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
14014           else
14015             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
14016         }
14017
14018       if (DECL_DECLARED_INLINE_P (decl)
14019           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
14020         add_AT_flag (subr_die, DW_AT_artificial, 1);
14021
14022       equate_decl_number_to_die (decl, subr_die);
14023     }
14024   else if (!DECL_EXTERNAL (decl))
14025     {
14026       HOST_WIDE_INT cfa_fb_offset;
14027
14028       if (!old_die || !get_AT (old_die, DW_AT_inline))
14029         equate_decl_number_to_die (decl, subr_die);
14030
14031       if (!flag_reorder_blocks_and_partition)
14032         {
14033           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
14034                                        current_function_funcdef_no);
14035           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
14036           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14037                                        current_function_funcdef_no);
14038           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
14039
14040           add_pubname (decl, subr_die);
14041           add_arange (decl, subr_die);
14042         }
14043       else
14044         {  /* Do nothing for now; maybe need to duplicate die, one for
14045               hot section and one for cold section, then use the hot/cold
14046               section begin/end labels to generate the aranges...  */
14047           /*
14048             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
14049             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
14050             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
14051             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
14052
14053             add_pubname (decl, subr_die);
14054             add_arange (decl, subr_die);
14055             add_arange (decl, subr_die);
14056            */
14057         }
14058
14059 #ifdef MIPS_DEBUGGING_INFO
14060       /* Add a reference to the FDE for this routine.  */
14061       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
14062 #endif
14063
14064       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
14065
14066       /* We define the "frame base" as the function's CFA.  This is more
14067          convenient for several reasons: (1) It's stable across the prologue
14068          and epilogue, which makes it better than just a frame pointer,
14069          (2) With dwarf3, there exists a one-byte encoding that allows us
14070          to reference the .debug_frame data by proxy, but failing that,
14071          (3) We can at least reuse the code inspection and interpretation
14072          code that determines the CFA position at various points in the
14073          function.  */
14074       /* ??? Use some command-line or configury switch to enable the use
14075          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
14076          consumers that understand it; fall back to "pure" dwarf2 and
14077          convert the CFA data into a location list.  */
14078       {
14079         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
14080         if (list->dw_loc_next)
14081           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
14082         else
14083           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
14084       }
14085
14086       /* Compute a displacement from the "steady-state frame pointer" to
14087          the CFA.  The former is what all stack slots and argument slots
14088          will reference in the rtl; the later is what we've told the
14089          debugger about.  We'll need to adjust all frame_base references
14090          by this displacement.  */
14091       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
14092
14093       if (cfun->static_chain_decl)
14094         add_AT_location_description (subr_die, DW_AT_static_link,
14095                  loc_descriptor_from_tree (cfun->static_chain_decl));
14096     }
14097
14098   /* Now output descriptions of the arguments for this function. This gets
14099      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
14100      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
14101      `...' at the end of the formal parameter list.  In order to find out if
14102      there was a trailing ellipsis or not, we must instead look at the type
14103      associated with the FUNCTION_DECL.  This will be a node of type
14104      FUNCTION_TYPE. If the chain of type nodes hanging off of this
14105      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
14106      an ellipsis at the end.  */
14107
14108   /* In the case where we are describing a mere function declaration, all we
14109      need to do here (and all we *can* do here) is to describe the *types* of
14110      its formal parameters.  */
14111   if (debug_info_level <= DINFO_LEVEL_TERSE)
14112     ;
14113   else if (declaration)
14114     gen_formal_types_die (decl, subr_die);
14115   else
14116     {
14117       /* Generate DIEs to represent all known formal parameters.  */
14118       tree arg_decls = DECL_ARGUMENTS (decl);
14119       tree parm;
14120
14121       /* When generating DIEs, generate the unspecified_parameters DIE
14122          instead if we come across the arg "__builtin_va_alist" */
14123       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
14124         if (TREE_CODE (parm) == PARM_DECL)
14125           {
14126             if (DECL_NAME (parm)
14127                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
14128                             "__builtin_va_alist"))
14129               gen_unspecified_parameters_die (parm, subr_die);
14130             else
14131               gen_decl_die (parm, NULL, subr_die);
14132           }
14133
14134       /* Decide whether we need an unspecified_parameters DIE at the end.
14135          There are 2 more cases to do this for: 1) the ansi ... declaration -
14136          this is detectable when the end of the arg list is not a
14137          void_type_node 2) an unprototyped function declaration (not a
14138          definition).  This just means that we have no info about the
14139          parameters at all.  */
14140       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
14141       if (fn_arg_types != NULL)
14142         {
14143           /* This is the prototyped case, check for....  */
14144           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
14145             gen_unspecified_parameters_die (decl, subr_die);
14146         }
14147       else if (DECL_INITIAL (decl) == NULL_TREE)
14148         gen_unspecified_parameters_die (decl, subr_die);
14149     }
14150
14151   /* Output Dwarf info for all of the stuff within the body of the function
14152      (if it has one - it may be just a declaration).  */
14153   outer_scope = DECL_INITIAL (decl);
14154
14155   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
14156      a function.  This BLOCK actually represents the outermost binding contour
14157      for the function, i.e. the contour in which the function's formal
14158      parameters and labels get declared. Curiously, it appears that the front
14159      end doesn't actually put the PARM_DECL nodes for the current function onto
14160      the BLOCK_VARS list for this outer scope, but are strung off of the
14161      DECL_ARGUMENTS list for the function instead.
14162
14163      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
14164      the LABEL_DECL nodes for the function however, and we output DWARF info
14165      for those in decls_for_scope.  Just within the `outer_scope' there will be
14166      a BLOCK node representing the function's outermost pair of curly braces,
14167      and any blocks used for the base and member initializers of a C++
14168      constructor function.  */
14169   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
14170     {
14171       /* Emit a DW_TAG_variable DIE for a named return value.  */
14172       if (DECL_NAME (DECL_RESULT (decl)))
14173         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
14174
14175       current_function_has_inlines = 0;
14176       decls_for_scope (outer_scope, subr_die, 0);
14177
14178 #if 0 && defined (MIPS_DEBUGGING_INFO)
14179       if (current_function_has_inlines)
14180         {
14181           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
14182           if (! comp_unit_has_inlines)
14183             {
14184               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
14185               comp_unit_has_inlines = 1;
14186             }
14187         }
14188 #endif
14189     }
14190   /* Add the calling convention attribute if requested.  */
14191   add_calling_convention_attribute (subr_die, decl);
14192
14193 }
14194
14195 /* Returns a hash value for X (which really is a die_struct).  */
14196
14197 static hashval_t
14198 common_block_die_table_hash (const void *x)
14199 {
14200   const_dw_die_ref d = (const_dw_die_ref) x;
14201   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
14202 }
14203
14204 /* Return nonzero if decl_id and die_parent of die_struct X is the same
14205    as decl_id and die_parent of die_struct Y.  */
14206
14207 static int
14208 common_block_die_table_eq (const void *x, const void *y)
14209 {
14210   const_dw_die_ref d = (const_dw_die_ref) x;
14211   const_dw_die_ref e = (const_dw_die_ref) y;
14212   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
14213 }
14214
14215 /* Generate a DIE to represent a declared data object.
14216    Either DECL or ORIGIN must be non-null.  */
14217
14218 static void
14219 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
14220 {
14221   HOST_WIDE_INT off;
14222   tree com_decl;
14223   tree decl_or_origin = decl ? decl : origin;
14224   dw_die_ref var_die;
14225   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
14226   dw_die_ref origin_die;
14227   int declaration = (DECL_EXTERNAL (decl_or_origin)
14228                      /* If DECL is COMDAT and has not actually been
14229                         emitted, we cannot take its address; there
14230                         might end up being no definition anywhere in
14231                         the program.  For example, consider the C++
14232                         test case:
14233
14234                           template <class T>
14235                           struct S { static const int i = 7; };
14236
14237                           template <class T>
14238                           const int S<T>::i;
14239
14240                           int f() { return S<int>::i; }
14241
14242                         Here, S<int>::i is not DECL_EXTERNAL, but no
14243                         definition is required, so the compiler will
14244                         not emit a definition.  */
14245                      || (TREE_CODE (decl_or_origin) == VAR_DECL
14246                          && DECL_COMDAT (decl_or_origin)
14247                          && !TREE_ASM_WRITTEN (decl_or_origin))
14248                      || class_or_namespace_scope_p (context_die));
14249
14250   if (!origin)
14251     origin = decl_ultimate_origin (decl);
14252
14253   com_decl = fortran_common (decl_or_origin, &off);
14254
14255   /* Symbol in common gets emitted as a child of the common block, in the form
14256      of a data member.  */
14257   if (com_decl)
14258     {
14259       tree field;
14260       dw_die_ref com_die;
14261       dw_loc_descr_ref loc;
14262       die_node com_die_arg;
14263
14264       var_die = lookup_decl_die (decl_or_origin);
14265       if (var_die)
14266         {
14267           if (get_AT (var_die, DW_AT_location) == NULL)
14268             {
14269               loc = loc_descriptor_from_tree (com_decl);
14270               if (loc)
14271                 {
14272                   if (off)
14273                     {
14274                       /* Optimize the common case.  */
14275                       if (loc->dw_loc_opc == DW_OP_addr
14276                           && loc->dw_loc_next == NULL
14277                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14278                              == SYMBOL_REF)
14279                         loc->dw_loc_oprnd1.v.val_addr
14280                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14281                         else
14282                           loc_descr_plus_const (&loc, off);
14283                     }
14284                   add_AT_loc (var_die, DW_AT_location, loc);
14285                   remove_AT (var_die, DW_AT_declaration);
14286                 }
14287             }
14288           return;
14289         }
14290
14291       if (common_block_die_table == NULL)
14292         common_block_die_table
14293           = htab_create_ggc (10, common_block_die_table_hash,
14294                              common_block_die_table_eq, NULL);
14295
14296       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14297       com_die_arg.decl_id = DECL_UID (com_decl);
14298       com_die_arg.die_parent = context_die;
14299       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14300       loc = loc_descriptor_from_tree (com_decl);
14301       if (com_die == NULL)
14302         {
14303           const char *cnam
14304             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14305           void **slot;
14306
14307           com_die = new_die (DW_TAG_common_block, context_die, decl);
14308           add_name_and_src_coords_attributes (com_die, com_decl);
14309           if (loc)
14310             {
14311               add_AT_loc (com_die, DW_AT_location, loc);
14312               /* Avoid sharing the same loc descriptor between
14313                  DW_TAG_common_block and DW_TAG_variable.  */
14314               loc = loc_descriptor_from_tree (com_decl);
14315             }
14316           else if (DECL_EXTERNAL (decl))
14317             add_AT_flag (com_die, DW_AT_declaration, 1);
14318           add_pubname_string (cnam, com_die); /* ??? needed? */
14319           com_die->decl_id = DECL_UID (com_decl);
14320           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14321           *slot = (void *) com_die;
14322         }
14323       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14324         {
14325           add_AT_loc (com_die, DW_AT_location, loc);
14326           loc = loc_descriptor_from_tree (com_decl);
14327           remove_AT (com_die, DW_AT_declaration);
14328         }
14329       var_die = new_die (DW_TAG_variable, com_die, decl);
14330       add_name_and_src_coords_attributes (var_die, decl);
14331       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14332                           TREE_THIS_VOLATILE (decl), context_die);
14333       add_AT_flag (var_die, DW_AT_external, 1);
14334       if (loc)
14335         {
14336           if (off)
14337             {
14338               /* Optimize the common case.  */
14339               if (loc->dw_loc_opc == DW_OP_addr
14340                   && loc->dw_loc_next == NULL
14341                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14342                 loc->dw_loc_oprnd1.v.val_addr
14343                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14344               else
14345                 loc_descr_plus_const (&loc, off);
14346             }
14347           add_AT_loc (var_die, DW_AT_location, loc);
14348         }
14349       else if (DECL_EXTERNAL (decl))
14350         add_AT_flag (var_die, DW_AT_declaration, 1);
14351       equate_decl_number_to_die (decl, var_die);
14352       return;
14353     }
14354
14355   /* If the compiler emitted a definition for the DECL declaration
14356      and if we already emitted a DIE for it, don't emit a second
14357      DIE for it again.  */
14358   if (old_die
14359       && declaration
14360       && old_die->die_parent == context_die)
14361     return;
14362
14363   /* For static data members, the declaration in the class is supposed
14364      to have DW_TAG_member tag; the specification should still be
14365      DW_TAG_variable referencing the DW_TAG_member DIE.  */
14366   if (declaration && class_scope_p (context_die))
14367     var_die = new_die (DW_TAG_member, context_die, decl);
14368   else
14369     var_die = new_die (DW_TAG_variable, context_die, decl);
14370
14371   origin_die = NULL;
14372   if (origin != NULL)
14373     origin_die = add_abstract_origin_attribute (var_die, origin);
14374
14375   /* Loop unrolling can create multiple blocks that refer to the same
14376      static variable, so we must test for the DW_AT_declaration flag.
14377
14378      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14379      copy decls and set the DECL_ABSTRACT flag on them instead of
14380      sharing them.
14381
14382      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14383
14384      ??? The declare_in_namespace support causes us to get two DIEs for one
14385      variable, both of which are declarations.  We want to avoid considering
14386      one to be a specification, so we must test that this DIE is not a
14387      declaration.  */
14388   else if (old_die && TREE_STATIC (decl) && ! declaration
14389            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14390     {
14391       /* This is a definition of a C++ class level static.  */
14392       add_AT_specification (var_die, old_die);
14393       if (DECL_NAME (decl))
14394         {
14395           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14396           struct dwarf_file_data * file_index = lookup_filename (s.file);
14397
14398           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14399             add_AT_file (var_die, DW_AT_decl_file, file_index);
14400
14401           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14402             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14403         }
14404     }
14405   else
14406     {
14407       tree type = TREE_TYPE (decl);
14408
14409       add_name_and_src_coords_attributes (var_die, decl);
14410       if ((TREE_CODE (decl) == PARM_DECL
14411            || TREE_CODE (decl) == RESULT_DECL
14412            || TREE_CODE (decl) == VAR_DECL)
14413           && DECL_BY_REFERENCE (decl))
14414         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14415       else
14416         add_type_attribute (var_die, type, TREE_READONLY (decl),
14417                             TREE_THIS_VOLATILE (decl), context_die);
14418
14419       if (TREE_PUBLIC (decl))
14420         add_AT_flag (var_die, DW_AT_external, 1);
14421
14422       if (DECL_ARTIFICIAL (decl))
14423         add_AT_flag (var_die, DW_AT_artificial, 1);
14424
14425       if (TREE_PROTECTED (decl))
14426         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14427       else if (TREE_PRIVATE (decl))
14428         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14429     }
14430
14431   if (declaration)
14432     add_AT_flag (var_die, DW_AT_declaration, 1);
14433
14434   if (decl && (DECL_ABSTRACT (decl) || declaration))
14435     equate_decl_number_to_die (decl, var_die);
14436
14437   if (! declaration
14438       && (! DECL_ABSTRACT (decl_or_origin)
14439           /* Local static vars are shared between all clones/inlines,
14440              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14441              already set.  */
14442           || (TREE_CODE (decl_or_origin) == VAR_DECL
14443               && TREE_STATIC (decl_or_origin)
14444               && DECL_RTL_SET_P (decl_or_origin)))
14445       /* When abstract origin already has DW_AT_location attribute, no need
14446          to add it again.  */
14447       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14448     {
14449       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14450           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14451         defer_location (decl_or_origin, var_die);
14452       else
14453         add_location_or_const_value_attribute (var_die,
14454                                                decl_or_origin,
14455                                                DW_AT_location);
14456       add_pubname (decl_or_origin, var_die);
14457     }
14458   else
14459     tree_add_const_value_attribute (var_die, decl_or_origin);
14460 }
14461
14462 /* Generate a DIE to represent a named constant.  */
14463
14464 static void
14465 gen_const_die (tree decl, dw_die_ref context_die)
14466 {
14467   dw_die_ref const_die;
14468   tree type = TREE_TYPE (decl);
14469
14470   const_die = new_die (DW_TAG_constant, context_die, decl);
14471   add_name_and_src_coords_attributes (const_die, decl);
14472   add_type_attribute (const_die, type, 1, 0, context_die);
14473   if (TREE_PUBLIC (decl))
14474     add_AT_flag (const_die, DW_AT_external, 1);
14475   if (DECL_ARTIFICIAL (decl))
14476     add_AT_flag (const_die, DW_AT_artificial, 1);
14477   tree_add_const_value_attribute (const_die, decl);
14478 }
14479
14480 /* Generate a DIE to represent a label identifier.  */
14481
14482 static void
14483 gen_label_die (tree decl, dw_die_ref context_die)
14484 {
14485   tree origin = decl_ultimate_origin (decl);
14486   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14487   rtx insn;
14488   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14489
14490   if (origin != NULL)
14491     add_abstract_origin_attribute (lbl_die, origin);
14492   else
14493     add_name_and_src_coords_attributes (lbl_die, decl);
14494
14495   if (DECL_ABSTRACT (decl))
14496     equate_decl_number_to_die (decl, lbl_die);
14497   else
14498     {
14499       insn = DECL_RTL_IF_SET (decl);
14500
14501       /* Deleted labels are programmer specified labels which have been
14502          eliminated because of various optimizations.  We still emit them
14503          here so that it is possible to put breakpoints on them.  */
14504       if (insn
14505           && (LABEL_P (insn)
14506               || ((NOTE_P (insn)
14507                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14508         {
14509           /* When optimization is enabled (via -O) some parts of the compiler
14510              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14511              represent source-level labels which were explicitly declared by
14512              the user.  This really shouldn't be happening though, so catch
14513              it if it ever does happen.  */
14514           gcc_assert (!INSN_DELETED_P (insn));
14515
14516           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14517           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14518         }
14519     }
14520 }
14521
14522 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14523    attributes to the DIE for a block STMT, to describe where the inlined
14524    function was called from.  This is similar to add_src_coords_attributes.  */
14525
14526 static inline void
14527 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14528 {
14529   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14530
14531   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14532   add_AT_unsigned (die, DW_AT_call_line, s.line);
14533 }
14534
14535
14536 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14537    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14538
14539 static inline void
14540 add_high_low_attributes (tree stmt, dw_die_ref die)
14541 {
14542   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14543
14544   if (BLOCK_FRAGMENT_CHAIN (stmt))
14545     {
14546       tree chain;
14547
14548       if (inlined_function_outer_scope_p (stmt))
14549         {
14550           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14551                                        BLOCK_NUMBER (stmt));
14552           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14553         }
14554
14555       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14556
14557       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14558       do
14559         {
14560           add_ranges (chain);
14561           chain = BLOCK_FRAGMENT_CHAIN (chain);
14562         }
14563       while (chain);
14564       add_ranges (NULL);
14565     }
14566   else
14567     {
14568       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14569                                    BLOCK_NUMBER (stmt));
14570       add_AT_lbl_id (die, DW_AT_low_pc, label);
14571       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14572                                    BLOCK_NUMBER (stmt));
14573       add_AT_lbl_id (die, DW_AT_high_pc, label);
14574     }
14575 }
14576
14577 /* Generate a DIE for a lexical block.  */
14578
14579 static void
14580 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14581 {
14582   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14583
14584   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14585     add_high_low_attributes (stmt, stmt_die);
14586
14587   decls_for_scope (stmt, stmt_die, depth);
14588 }
14589
14590 /* Generate a DIE for an inlined subprogram.  */
14591
14592 static void
14593 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14594 {
14595   tree decl = block_ultimate_origin (stmt);
14596
14597   /* Emit info for the abstract instance first, if we haven't yet.  We
14598      must emit this even if the block is abstract, otherwise when we
14599      emit the block below (or elsewhere), we may end up trying to emit
14600      a die whose origin die hasn't been emitted, and crashing.  */
14601   dwarf2out_abstract_function (decl);
14602
14603   if (! BLOCK_ABSTRACT (stmt))
14604     {
14605       dw_die_ref subr_die
14606         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14607
14608       add_abstract_origin_attribute (subr_die, decl);
14609       if (TREE_ASM_WRITTEN (stmt))
14610         add_high_low_attributes (stmt, subr_die);
14611       add_call_src_coords_attributes (stmt, subr_die);
14612
14613       decls_for_scope (stmt, subr_die, depth);
14614       current_function_has_inlines = 1;
14615     }
14616   else
14617     /* We may get here if we're the outer block of function A that was
14618        inlined into function B that was inlined into function C.  When
14619        generating debugging info for C, dwarf2out_abstract_function(B)
14620        would mark all inlined blocks as abstract, including this one.
14621        So, we wouldn't (and shouldn't) expect labels to be generated
14622        for this one.  Instead, just emit debugging info for
14623        declarations within the block.  This is particularly important
14624        in the case of initializers of arguments passed from B to us:
14625        if they're statement expressions containing declarations, we
14626        wouldn't generate dies for their abstract variables, and then,
14627        when generating dies for the real variables, we'd die (pun
14628        intended :-)  */
14629     gen_lexical_block_die (stmt, context_die, depth);
14630 }
14631
14632 /* Generate a DIE for a field in a record, or structure.  */
14633
14634 static void
14635 gen_field_die (tree decl, dw_die_ref context_die)
14636 {
14637   dw_die_ref decl_die;
14638
14639   if (TREE_TYPE (decl) == error_mark_node)
14640     return;
14641
14642   decl_die = new_die (DW_TAG_member, context_die, decl);
14643   add_name_and_src_coords_attributes (decl_die, decl);
14644   add_type_attribute (decl_die, member_declared_type (decl),
14645                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14646                       context_die);
14647
14648   if (DECL_BIT_FIELD_TYPE (decl))
14649     {
14650       add_byte_size_attribute (decl_die, decl);
14651       add_bit_size_attribute (decl_die, decl);
14652       add_bit_offset_attribute (decl_die, decl);
14653     }
14654
14655   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14656     add_data_member_location_attribute (decl_die, decl);
14657
14658   if (DECL_ARTIFICIAL (decl))
14659     add_AT_flag (decl_die, DW_AT_artificial, 1);
14660
14661   if (TREE_PROTECTED (decl))
14662     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14663   else if (TREE_PRIVATE (decl))
14664     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14665
14666   /* Equate decl number to die, so that we can look up this decl later on.  */
14667   equate_decl_number_to_die (decl, decl_die);
14668 }
14669
14670 #if 0
14671 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14672    Use modified_type_die instead.
14673    We keep this code here just in case these types of DIEs may be needed to
14674    represent certain things in other languages (e.g. Pascal) someday.  */
14675
14676 static void
14677 gen_pointer_type_die (tree type, dw_die_ref context_die)
14678 {
14679   dw_die_ref ptr_die
14680     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14681
14682   equate_type_number_to_die (type, ptr_die);
14683   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14684   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14685 }
14686
14687 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14688    Use modified_type_die instead.
14689    We keep this code here just in case these types of DIEs may be needed to
14690    represent certain things in other languages (e.g. Pascal) someday.  */
14691
14692 static void
14693 gen_reference_type_die (tree type, dw_die_ref context_die)
14694 {
14695   dw_die_ref ref_die
14696     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14697
14698   equate_type_number_to_die (type, ref_die);
14699   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14700   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14701 }
14702 #endif
14703
14704 /* Generate a DIE for a pointer to a member type.  */
14705
14706 static void
14707 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14708 {
14709   dw_die_ref ptr_die
14710     = new_die (DW_TAG_ptr_to_member_type,
14711                scope_die_for (type, context_die), type);
14712
14713   equate_type_number_to_die (type, ptr_die);
14714   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14715                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14716   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14717 }
14718
14719 /* Generate the DIE for the compilation unit.  */
14720
14721 static dw_die_ref
14722 gen_compile_unit_die (const char *filename)
14723 {
14724   dw_die_ref die;
14725   char producer[250];
14726   const char *language_string = lang_hooks.name;
14727   int language;
14728
14729   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14730
14731   if (filename)
14732     {
14733       add_name_attribute (die, filename);
14734       /* Don't add cwd for <built-in>.  */
14735       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14736         add_comp_dir_attribute (die);
14737     }
14738
14739   sprintf (producer, "%s %s", language_string, version_string);
14740
14741 #ifdef MIPS_DEBUGGING_INFO
14742   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14743      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14744      not appear in the producer string, the debugger reaches the conclusion
14745      that the object file is stripped and has no debugging information.
14746      To get the MIPS/SGI debugger to believe that there is debugging
14747      information in the object file, we add a -g to the producer string.  */
14748   if (debug_info_level > DINFO_LEVEL_TERSE)
14749     strcat (producer, " -g");
14750 #endif
14751
14752   add_AT_string (die, DW_AT_producer, producer);
14753
14754   if (strcmp (language_string, "GNU C++") == 0)
14755     language = DW_LANG_C_plus_plus;
14756   else if (strcmp (language_string, "GNU Ada") == 0)
14757     language = DW_LANG_Ada95;
14758   else if (strcmp (language_string, "GNU F77") == 0)
14759     language = DW_LANG_Fortran77;
14760   else if (strcmp (language_string, "GNU Fortran") == 0)
14761     language = DW_LANG_Fortran95;
14762   else if (strcmp (language_string, "GNU Pascal") == 0)
14763     language = DW_LANG_Pascal83;
14764   else if (strcmp (language_string, "GNU Java") == 0)
14765     language = DW_LANG_Java;
14766   else if (strcmp (language_string, "GNU Objective-C") == 0)
14767     language = DW_LANG_ObjC;
14768   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14769     language = DW_LANG_ObjC_plus_plus;
14770   else
14771     language = DW_LANG_C89;
14772
14773   add_AT_unsigned (die, DW_AT_language, language);
14774   return die;
14775 }
14776
14777 /* Generate the DIE for a base class.  */
14778
14779 static void
14780 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14781 {
14782   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14783
14784   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14785   add_data_member_location_attribute (die, binfo);
14786
14787   if (BINFO_VIRTUAL_P (binfo))
14788     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14789
14790   if (access == access_public_node)
14791     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14792   else if (access == access_protected_node)
14793     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14794 }
14795
14796 /* Generate a DIE for a class member.  */
14797
14798 static void
14799 gen_member_die (tree type, dw_die_ref context_die)
14800 {
14801   tree member;
14802   tree binfo = TYPE_BINFO (type);
14803   dw_die_ref child;
14804
14805   /* If this is not an incomplete type, output descriptions of each of its
14806      members. Note that as we output the DIEs necessary to represent the
14807      members of this record or union type, we will also be trying to output
14808      DIEs to represent the *types* of those members. However the `type'
14809      function (above) will specifically avoid generating type DIEs for member
14810      types *within* the list of member DIEs for this (containing) type except
14811      for those types (of members) which are explicitly marked as also being
14812      members of this (containing) type themselves.  The g++ front- end can
14813      force any given type to be treated as a member of some other (containing)
14814      type by setting the TYPE_CONTEXT of the given (member) type to point to
14815      the TREE node representing the appropriate (containing) type.  */
14816
14817   /* First output info about the base classes.  */
14818   if (binfo)
14819     {
14820       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14821       int i;
14822       tree base;
14823
14824       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14825         gen_inheritance_die (base,
14826                              (accesses ? VEC_index (tree, accesses, i)
14827                               : access_public_node), context_die);
14828     }
14829
14830   /* Now output info about the data members and type members.  */
14831   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14832     {
14833       /* If we thought we were generating minimal debug info for TYPE
14834          and then changed our minds, some of the member declarations
14835          may have already been defined.  Don't define them again, but
14836          do put them in the right order.  */
14837
14838       child = lookup_decl_die (member);
14839       if (child)
14840         splice_child_die (context_die, child);
14841       else
14842         gen_decl_die (member, NULL, context_die);
14843     }
14844
14845   /* Now output info about the function members (if any).  */
14846   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14847     {
14848       /* Don't include clones in the member list.  */
14849       if (DECL_ABSTRACT_ORIGIN (member))
14850         continue;
14851
14852       child = lookup_decl_die (member);
14853       if (child)
14854         splice_child_die (context_die, child);
14855       else
14856         gen_decl_die (member, NULL, context_die);
14857     }
14858 }
14859
14860 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14861    is set, we pretend that the type was never defined, so we only get the
14862    member DIEs needed by later specification DIEs.  */
14863
14864 static void
14865 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14866                                 enum debug_info_usage usage)
14867 {
14868   dw_die_ref type_die = lookup_type_die (type);
14869   dw_die_ref scope_die = 0;
14870   int nested = 0;
14871   int complete = (TYPE_SIZE (type)
14872                   && (! TYPE_STUB_DECL (type)
14873                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14874   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14875   complete = complete && should_emit_struct_debug (type, usage);
14876
14877   if (type_die && ! complete)
14878     return;
14879
14880   if (TYPE_CONTEXT (type) != NULL_TREE
14881       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14882           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14883     nested = 1;
14884
14885   scope_die = scope_die_for (type, context_die);
14886
14887   if (! type_die || (nested && scope_die == comp_unit_die))
14888     /* First occurrence of type or toplevel definition of nested class.  */
14889     {
14890       dw_die_ref old_die = type_die;
14891
14892       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14893                           ? record_type_tag (type) : DW_TAG_union_type,
14894                           scope_die, type);
14895       equate_type_number_to_die (type, type_die);
14896       if (old_die)
14897         add_AT_specification (type_die, old_die);
14898       else
14899         add_name_attribute (type_die, type_tag (type));
14900     }
14901   else
14902     remove_AT (type_die, DW_AT_declaration);
14903
14904   /* If this type has been completed, then give it a byte_size attribute and
14905      then give a list of members.  */
14906   if (complete && !ns_decl)
14907     {
14908       /* Prevent infinite recursion in cases where the type of some member of
14909          this type is expressed in terms of this type itself.  */
14910       TREE_ASM_WRITTEN (type) = 1;
14911       add_byte_size_attribute (type_die, type);
14912       if (TYPE_STUB_DECL (type) != NULL_TREE)
14913         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14914
14915       /* If the first reference to this type was as the return type of an
14916          inline function, then it may not have a parent.  Fix this now.  */
14917       if (type_die->die_parent == NULL)
14918         add_child_die (scope_die, type_die);
14919
14920       push_decl_scope (type);
14921       gen_member_die (type, type_die);
14922       pop_decl_scope ();
14923
14924       /* GNU extension: Record what type our vtable lives in.  */
14925       if (TYPE_VFIELD (type))
14926         {
14927           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14928
14929           gen_type_die (vtype, context_die);
14930           add_AT_die_ref (type_die, DW_AT_containing_type,
14931                           lookup_type_die (vtype));
14932         }
14933     }
14934   else
14935     {
14936       add_AT_flag (type_die, DW_AT_declaration, 1);
14937
14938       /* We don't need to do this for function-local types.  */
14939       if (TYPE_STUB_DECL (type)
14940           && ! decl_function_context (TYPE_STUB_DECL (type)))
14941         VEC_safe_push (tree, gc, incomplete_types, type);
14942     }
14943
14944   if (get_AT (type_die, DW_AT_name))
14945     add_pubtype (type, type_die);
14946 }
14947
14948 /* Generate a DIE for a subroutine _type_.  */
14949
14950 static void
14951 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14952 {
14953   tree return_type = TREE_TYPE (type);
14954   dw_die_ref subr_die
14955     = new_die (DW_TAG_subroutine_type,
14956                scope_die_for (type, context_die), type);
14957
14958   equate_type_number_to_die (type, subr_die);
14959   add_prototyped_attribute (subr_die, type);
14960   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14961   gen_formal_types_die (type, subr_die);
14962
14963   if (get_AT (subr_die, DW_AT_name))
14964     add_pubtype (type, subr_die);
14965 }
14966
14967 /* Generate a DIE for a type definition.  */
14968
14969 static void
14970 gen_typedef_die (tree decl, dw_die_ref context_die)
14971 {
14972   dw_die_ref type_die;
14973   tree origin;
14974
14975   if (TREE_ASM_WRITTEN (decl))
14976     return;
14977
14978   TREE_ASM_WRITTEN (decl) = 1;
14979   type_die = new_die (DW_TAG_typedef, context_die, decl);
14980   origin = decl_ultimate_origin (decl);
14981   if (origin != NULL)
14982     add_abstract_origin_attribute (type_die, origin);
14983   else
14984     {
14985       tree type;
14986
14987       add_name_and_src_coords_attributes (type_die, decl);
14988       if (DECL_ORIGINAL_TYPE (decl))
14989         {
14990           type = DECL_ORIGINAL_TYPE (decl);
14991
14992           gcc_assert (type != TREE_TYPE (decl));
14993           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14994         }
14995       else
14996         type = TREE_TYPE (decl);
14997
14998       add_type_attribute (type_die, type, TREE_READONLY (decl),
14999                           TREE_THIS_VOLATILE (decl), context_die);
15000     }
15001
15002   if (DECL_ABSTRACT (decl))
15003     equate_decl_number_to_die (decl, type_die);
15004
15005   if (get_AT (type_die, DW_AT_name))
15006     add_pubtype (decl, type_die);
15007 }
15008
15009 /* Generate a type description DIE.  */
15010
15011 static void
15012 gen_type_die_with_usage (tree type, dw_die_ref context_die,
15013                                 enum debug_info_usage usage)
15014 {
15015   int need_pop;
15016   struct array_descr_info info;
15017
15018   if (type == NULL_TREE || type == error_mark_node)
15019     return;
15020
15021   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15022       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
15023     {
15024       if (TREE_ASM_WRITTEN (type))
15025         return;
15026
15027       /* Prevent broken recursion; we can't hand off to the same type.  */
15028       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
15029
15030       /* Use the DIE of the containing namespace as the parent DIE of
15031          the type description DIE we want to generate.  */
15032       if (DECL_CONTEXT (TYPE_NAME (type))
15033           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
15034         context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
15035
15036       TREE_ASM_WRITTEN (type) = 1;
15037       gen_decl_die (TYPE_NAME (type), NULL, context_die);
15038       return;
15039     }
15040
15041   /* If this is an array type with hidden descriptor, handle it first.  */
15042   if (!TREE_ASM_WRITTEN (type)
15043       && lang_hooks.types.get_array_descr_info
15044       && lang_hooks.types.get_array_descr_info (type, &info))
15045     {
15046       gen_descr_array_type_die (type, &info, context_die);
15047       TREE_ASM_WRITTEN (type) = 1;
15048       return;
15049     }
15050
15051   /* We are going to output a DIE to represent the unqualified version
15052      of this type (i.e. without any const or volatile qualifiers) so
15053      get the main variant (i.e. the unqualified version) of this type
15054      now.  (Vectors are special because the debugging info is in the
15055      cloned type itself).  */
15056   if (TREE_CODE (type) != VECTOR_TYPE)
15057     type = type_main_variant (type);
15058
15059   if (TREE_ASM_WRITTEN (type))
15060     return;
15061
15062   switch (TREE_CODE (type))
15063     {
15064     case ERROR_MARK:
15065       break;
15066
15067     case POINTER_TYPE:
15068     case REFERENCE_TYPE:
15069       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
15070          ensures that the gen_type_die recursion will terminate even if the
15071          type is recursive.  Recursive types are possible in Ada.  */
15072       /* ??? We could perhaps do this for all types before the switch
15073          statement.  */
15074       TREE_ASM_WRITTEN (type) = 1;
15075
15076       /* For these types, all that is required is that we output a DIE (or a
15077          set of DIEs) to represent the "basis" type.  */
15078       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15079                                 DINFO_USAGE_IND_USE);
15080       break;
15081
15082     case OFFSET_TYPE:
15083       /* This code is used for C++ pointer-to-data-member types.
15084          Output a description of the relevant class type.  */
15085       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
15086                                         DINFO_USAGE_IND_USE);
15087
15088       /* Output a description of the type of the object pointed to.  */
15089       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15090                                         DINFO_USAGE_IND_USE);
15091
15092       /* Now output a DIE to represent this pointer-to-data-member type
15093          itself.  */
15094       gen_ptr_to_mbr_type_die (type, context_die);
15095       break;
15096
15097     case FUNCTION_TYPE:
15098       /* Force out return type (in case it wasn't forced out already).  */
15099       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15100                                         DINFO_USAGE_DIR_USE);
15101       gen_subroutine_type_die (type, context_die);
15102       break;
15103
15104     case METHOD_TYPE:
15105       /* Force out return type (in case it wasn't forced out already).  */
15106       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15107                                         DINFO_USAGE_DIR_USE);
15108       gen_subroutine_type_die (type, context_die);
15109       break;
15110
15111     case ARRAY_TYPE:
15112       gen_array_type_die (type, context_die);
15113       break;
15114
15115     case VECTOR_TYPE:
15116       gen_array_type_die (type, context_die);
15117       break;
15118
15119     case ENUMERAL_TYPE:
15120     case RECORD_TYPE:
15121     case UNION_TYPE:
15122     case QUAL_UNION_TYPE:
15123       /* If this is a nested type whose containing class hasn't been written
15124          out yet, writing it out will cover this one, too.  This does not apply
15125          to instantiations of member class templates; they need to be added to
15126          the containing class as they are generated.  FIXME: This hurts the
15127          idea of combining type decls from multiple TUs, since we can't predict
15128          what set of template instantiations we'll get.  */
15129       if (TYPE_CONTEXT (type)
15130           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15131           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
15132         {
15133           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
15134
15135           if (TREE_ASM_WRITTEN (type))
15136             return;
15137
15138           /* If that failed, attach ourselves to the stub.  */
15139           push_decl_scope (TYPE_CONTEXT (type));
15140           context_die = lookup_type_die (TYPE_CONTEXT (type));
15141           need_pop = 1;
15142         }
15143       else
15144         {
15145           context_die = declare_in_namespace (type, context_die);
15146           need_pop = 0;
15147         }
15148
15149       if (TREE_CODE (type) == ENUMERAL_TYPE)
15150         {
15151           /* This might have been written out by the call to
15152              declare_in_namespace.  */
15153           if (!TREE_ASM_WRITTEN (type))
15154             gen_enumeration_type_die (type, context_die);
15155         }
15156       else
15157         gen_struct_or_union_type_die (type, context_die, usage);
15158
15159       if (need_pop)
15160         pop_decl_scope ();
15161
15162       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
15163          it up if it is ever completed.  gen_*_type_die will set it for us
15164          when appropriate.  */
15165       return;
15166
15167     case VOID_TYPE:
15168     case INTEGER_TYPE:
15169     case REAL_TYPE:
15170     case FIXED_POINT_TYPE:
15171     case COMPLEX_TYPE:
15172     case BOOLEAN_TYPE:
15173       /* No DIEs needed for fundamental types.  */
15174       break;
15175
15176     case LANG_TYPE:
15177       /* No Dwarf representation currently defined.  */
15178       break;
15179
15180     default:
15181       gcc_unreachable ();
15182     }
15183
15184   TREE_ASM_WRITTEN (type) = 1;
15185 }
15186
15187 static void
15188 gen_type_die (tree type, dw_die_ref context_die)
15189 {
15190   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
15191 }
15192
15193 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
15194    things which are local to the given block.  */
15195
15196 static void
15197 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
15198 {
15199   int must_output_die = 0;
15200   bool inlined_func;
15201
15202   /* Ignore blocks that are NULL.  */
15203   if (stmt == NULL_TREE)
15204     return;
15205
15206   inlined_func = inlined_function_outer_scope_p (stmt);
15207
15208   /* If the block is one fragment of a non-contiguous block, do not
15209      process the variables, since they will have been done by the
15210      origin block.  Do process subblocks.  */
15211   if (BLOCK_FRAGMENT_ORIGIN (stmt))
15212     {
15213       tree sub;
15214
15215       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
15216         gen_block_die (sub, context_die, depth + 1);
15217
15218       return;
15219     }
15220
15221   /* Determine if we need to output any Dwarf DIEs at all to represent this
15222      block.  */
15223   if (inlined_func)
15224     /* The outer scopes for inlinings *must* always be represented.  We
15225        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
15226     must_output_die = 1;
15227   else
15228     {
15229       /* Determine if this block directly contains any "significant"
15230          local declarations which we will need to output DIEs for.  */
15231       if (debug_info_level > DINFO_LEVEL_TERSE)
15232         /* We are not in terse mode so *any* local declaration counts
15233            as being a "significant" one.  */
15234         must_output_die = ((BLOCK_VARS (stmt) != NULL
15235                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
15236                            && (TREE_USED (stmt)
15237                                || TREE_ASM_WRITTEN (stmt)
15238                                || BLOCK_ABSTRACT (stmt)));
15239       else if ((TREE_USED (stmt)
15240                 || TREE_ASM_WRITTEN (stmt)
15241                 || BLOCK_ABSTRACT (stmt))
15242                && !dwarf2out_ignore_block (stmt))
15243         must_output_die = 1;
15244     }
15245
15246   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
15247      DIE for any block which contains no significant local declarations at
15248      all.  Rather, in such cases we just call `decls_for_scope' so that any
15249      needed Dwarf info for any sub-blocks will get properly generated. Note
15250      that in terse mode, our definition of what constitutes a "significant"
15251      local declaration gets restricted to include only inlined function
15252      instances and local (nested) function definitions.  */
15253   if (must_output_die)
15254     {
15255       if (inlined_func)
15256         gen_inlined_subroutine_die (stmt, context_die, depth);
15257       else
15258         gen_lexical_block_die (stmt, context_die, depth);
15259     }
15260   else
15261     decls_for_scope (stmt, context_die, depth);
15262 }
15263
15264 /* Process variable DECL (or variable with origin ORIGIN) within
15265    block STMT and add it to CONTEXT_DIE.  */
15266 static void
15267 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15268 {
15269   dw_die_ref die;
15270   tree decl_or_origin = decl ? decl : origin;
15271   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15272
15273   if (ultimate_origin)
15274     origin = ultimate_origin;
15275
15276   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15277     die = lookup_decl_die (decl_or_origin);
15278   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15279            && TYPE_DECL_IS_STUB (decl_or_origin))
15280     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15281   else
15282     die = NULL;
15283
15284   if (die != NULL && die->die_parent == NULL)
15285     add_child_die (context_die, die);
15286   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15287     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15288                                          stmt, context_die);
15289   else
15290     gen_decl_die (decl, origin, context_die);
15291 }
15292
15293 /* Generate all of the decls declared within a given scope and (recursively)
15294    all of its sub-blocks.  */
15295
15296 static void
15297 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15298 {
15299   tree decl;
15300   unsigned int i;
15301   tree subblocks;
15302
15303   /* Ignore NULL blocks.  */
15304   if (stmt == NULL_TREE)
15305     return;
15306
15307   /* Output the DIEs to represent all of the data objects and typedefs
15308      declared directly within this block but not within any nested
15309      sub-blocks.  Also, nested function and tag DIEs have been
15310      generated with a parent of NULL; fix that up now.  */
15311   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15312     process_scope_var (stmt, decl, NULL_TREE, context_die);
15313   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15314     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15315                        context_die);
15316
15317   /* If we're at -g1, we're not interested in subblocks.  */
15318   if (debug_info_level <= DINFO_LEVEL_TERSE)
15319     return;
15320
15321   /* Output the DIEs to represent all sub-blocks (and the items declared
15322      therein) of this block.  */
15323   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15324        subblocks != NULL;
15325        subblocks = BLOCK_CHAIN (subblocks))
15326     gen_block_die (subblocks, context_die, depth + 1);
15327 }
15328
15329 /* Is this a typedef we can avoid emitting?  */
15330
15331 static inline int
15332 is_redundant_typedef (const_tree decl)
15333 {
15334   if (TYPE_DECL_IS_STUB (decl))
15335     return 1;
15336
15337   if (DECL_ARTIFICIAL (decl)
15338       && DECL_CONTEXT (decl)
15339       && is_tagged_type (DECL_CONTEXT (decl))
15340       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15341       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15342     /* Also ignore the artificial member typedef for the class name.  */
15343     return 1;
15344
15345   return 0;
15346 }
15347
15348 /* Returns the DIE for a context.  */
15349
15350 static inline dw_die_ref
15351 get_context_die (tree context)
15352 {
15353   if (context)
15354     {
15355       /* Find die that represents this context.  */
15356       if (TYPE_P (context))
15357         return force_type_die (context);
15358       else
15359         return force_decl_die (context);
15360     }
15361   return comp_unit_die;
15362 }
15363
15364 /* Returns the DIE for decl.  A DIE will always be returned.  */
15365
15366 static dw_die_ref
15367 force_decl_die (tree decl)
15368 {
15369   dw_die_ref decl_die;
15370   unsigned saved_external_flag;
15371   tree save_fn = NULL_TREE;
15372   decl_die = lookup_decl_die (decl);
15373   if (!decl_die)
15374     {
15375       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15376
15377       decl_die = lookup_decl_die (decl);
15378       if (decl_die)
15379         return decl_die;
15380
15381       switch (TREE_CODE (decl))
15382         {
15383         case FUNCTION_DECL:
15384           /* Clear current_function_decl, so that gen_subprogram_die thinks
15385              that this is a declaration. At this point, we just want to force
15386              declaration die.  */
15387           save_fn = current_function_decl;
15388           current_function_decl = NULL_TREE;
15389           gen_subprogram_die (decl, context_die);
15390           current_function_decl = save_fn;
15391           break;
15392
15393         case VAR_DECL:
15394           /* Set external flag to force declaration die. Restore it after
15395            gen_decl_die() call.  */
15396           saved_external_flag = DECL_EXTERNAL (decl);
15397           DECL_EXTERNAL (decl) = 1;
15398           gen_decl_die (decl, NULL, context_die);
15399           DECL_EXTERNAL (decl) = saved_external_flag;
15400           break;
15401
15402         case NAMESPACE_DECL:
15403           dwarf2out_decl (decl);
15404           break;
15405
15406         default:
15407           gcc_unreachable ();
15408         }
15409
15410       /* We should be able to find the DIE now.  */
15411       if (!decl_die)
15412         decl_die = lookup_decl_die (decl);
15413       gcc_assert (decl_die);
15414     }
15415
15416   return decl_die;
15417 }
15418
15419 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15420    always returned.  */
15421
15422 static dw_die_ref
15423 force_type_die (tree type)
15424 {
15425   dw_die_ref type_die;
15426
15427   type_die = lookup_type_die (type);
15428   if (!type_die)
15429     {
15430       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15431
15432       type_die = modified_type_die (type, TYPE_READONLY (type),
15433                                     TYPE_VOLATILE (type), context_die);
15434       gcc_assert (type_die);
15435     }
15436   return type_die;
15437 }
15438
15439 /* Force out any required namespaces to be able to output DECL,
15440    and return the new context_die for it, if it's changed.  */
15441
15442 static dw_die_ref
15443 setup_namespace_context (tree thing, dw_die_ref context_die)
15444 {
15445   tree context = (DECL_P (thing)
15446                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15447   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15448     /* Force out the namespace.  */
15449     context_die = force_decl_die (context);
15450
15451   return context_die;
15452 }
15453
15454 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15455    type) within its namespace, if appropriate.
15456
15457    For compatibility with older debuggers, namespace DIEs only contain
15458    declarations; all definitions are emitted at CU scope.  */
15459
15460 static dw_die_ref
15461 declare_in_namespace (tree thing, dw_die_ref context_die)
15462 {
15463   dw_die_ref ns_context;
15464
15465   if (debug_info_level <= DINFO_LEVEL_TERSE)
15466     return context_die;
15467
15468   /* If this decl is from an inlined function, then don't try to emit it in its
15469      namespace, as we will get confused.  It would have already been emitted
15470      when the abstract instance of the inline function was emitted anyways.  */
15471   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15472     return context_die;
15473
15474   ns_context = setup_namespace_context (thing, context_die);
15475
15476   if (ns_context != context_die)
15477     {
15478       if (is_fortran ())
15479         return ns_context;
15480       if (DECL_P (thing))
15481         gen_decl_die (thing, NULL, ns_context);
15482       else
15483         gen_type_die (thing, ns_context);
15484     }
15485   return context_die;
15486 }
15487
15488 /* Generate a DIE for a namespace or namespace alias.  */
15489
15490 static void
15491 gen_namespace_die (tree decl, dw_die_ref context_die)
15492 {
15493   dw_die_ref namespace_die;
15494
15495   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15496      they are an alias of.  */
15497   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15498     {
15499       /* Output a real namespace or module.  */
15500       context_die = setup_namespace_context (decl, comp_unit_die);
15501       namespace_die = new_die (is_fortran ()
15502                                ? DW_TAG_module : DW_TAG_namespace,
15503                                context_die, decl);
15504       /* For Fortran modules defined in different CU don't add src coords.  */
15505       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15506         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15507       else
15508         add_name_and_src_coords_attributes (namespace_die, decl);
15509       if (DECL_EXTERNAL (decl))
15510         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15511       equate_decl_number_to_die (decl, namespace_die);
15512     }
15513   else
15514     {
15515       /* Output a namespace alias.  */
15516
15517       /* Force out the namespace we are an alias of, if necessary.  */
15518       dw_die_ref origin_die
15519         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15520
15521       if (DECL_CONTEXT (decl) == NULL_TREE
15522           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15523         context_die = setup_namespace_context (decl, comp_unit_die);
15524       /* Now create the namespace alias DIE.  */
15525       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15526       add_name_and_src_coords_attributes (namespace_die, decl);
15527       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15528       equate_decl_number_to_die (decl, namespace_die);
15529     }
15530 }
15531
15532 /* Generate Dwarf debug information for a decl described by DECL.  */
15533
15534 static void
15535 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15536 {
15537   tree decl_or_origin = decl ? decl : origin;
15538   tree class_origin = NULL;
15539
15540   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15541     return;
15542
15543   switch (TREE_CODE (decl_or_origin))
15544     {
15545     case ERROR_MARK:
15546       break;
15547
15548     case CONST_DECL:
15549       if (!is_fortran ())
15550         {
15551           /* The individual enumerators of an enum type get output when we output
15552              the Dwarf representation of the relevant enum type itself.  */
15553           break;
15554         }
15555
15556       /* Emit its type.  */
15557       gen_type_die (TREE_TYPE (decl), context_die);
15558
15559       /* And its containing namespace.  */
15560       context_die = declare_in_namespace (decl, context_die);
15561
15562       gen_const_die (decl, context_die);
15563       break;
15564
15565     case FUNCTION_DECL:
15566       /* Don't output any DIEs to represent mere function declarations,
15567          unless they are class members or explicit block externs.  */
15568       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15569           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15570           && (current_function_decl == NULL_TREE
15571               || DECL_ARTIFICIAL (decl_or_origin)))
15572         break;
15573
15574 #if 0
15575       /* FIXME */
15576       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15577          on local redeclarations of global functions.  That seems broken.  */
15578       if (current_function_decl != decl)
15579         /* This is only a declaration.  */;
15580 #endif
15581
15582       /* If we're emitting a clone, emit info for the abstract instance.  */
15583       if (origin || DECL_ORIGIN (decl) != decl)
15584         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15585
15586       /* If we're emitting an out-of-line copy of an inline function,
15587          emit info for the abstract instance and set up to refer to it.  */
15588       else if (cgraph_function_possibly_inlined_p (decl)
15589                && ! DECL_ABSTRACT (decl)
15590                && ! class_or_namespace_scope_p (context_die)
15591                /* dwarf2out_abstract_function won't emit a die if this is just
15592                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15593                   that case, because that works only if we have a die.  */
15594                && DECL_INITIAL (decl) != NULL_TREE)
15595         {
15596           dwarf2out_abstract_function (decl);
15597           set_decl_origin_self (decl);
15598         }
15599
15600       /* Otherwise we're emitting the primary DIE for this decl.  */
15601       else if (debug_info_level > DINFO_LEVEL_TERSE)
15602         {
15603           /* Before we describe the FUNCTION_DECL itself, make sure that we
15604              have described its return type.  */
15605           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15606
15607           /* And its virtual context.  */
15608           if (DECL_VINDEX (decl) != NULL_TREE)
15609             gen_type_die (DECL_CONTEXT (decl), context_die);
15610
15611           /* And its containing type.  */
15612           if (!origin)
15613             origin = decl_class_context (decl);
15614           if (origin != NULL_TREE)
15615             gen_type_die_for_member (origin, decl, context_die);
15616
15617           /* And its containing namespace.  */
15618           context_die = declare_in_namespace (decl, context_die);
15619         }
15620
15621       /* Now output a DIE to represent the function itself.  */
15622       if (decl)
15623         gen_subprogram_die (decl, context_die);
15624       break;
15625
15626     case TYPE_DECL:
15627       /* If we are in terse mode, don't generate any DIEs to represent any
15628          actual typedefs.  */
15629       if (debug_info_level <= DINFO_LEVEL_TERSE)
15630         break;
15631
15632       /* In the special case of a TYPE_DECL node representing the declaration
15633          of some type tag, if the given TYPE_DECL is marked as having been
15634          instantiated from some other (original) TYPE_DECL node (e.g. one which
15635          was generated within the original definition of an inline function) we
15636          used to generate a special (abbreviated) DW_TAG_structure_type,
15637          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15638          should be actually referencing those DIEs, as variable DIEs with that
15639          type would be emitted already in the abstract origin, so it was always
15640          removed during unused type prunning.  Don't add anything in this
15641          case.  */
15642       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15643         break;
15644
15645       if (is_redundant_typedef (decl))
15646         gen_type_die (TREE_TYPE (decl), context_die);
15647       else
15648         /* Output a DIE to represent the typedef itself.  */
15649         gen_typedef_die (decl, context_die);
15650       break;
15651
15652     case LABEL_DECL:
15653       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15654         gen_label_die (decl, context_die);
15655       break;
15656
15657     case VAR_DECL:
15658     case RESULT_DECL:
15659       /* If we are in terse mode, don't generate any DIEs to represent any
15660          variable declarations or definitions.  */
15661       if (debug_info_level <= DINFO_LEVEL_TERSE)
15662         break;
15663
15664       /* Output any DIEs that are needed to specify the type of this data
15665          object.  */
15666       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
15667            || TREE_CODE (decl_or_origin) == VAR_DECL)
15668           && DECL_BY_REFERENCE (decl_or_origin))
15669         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15670       else
15671         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15672
15673       /* And its containing type.  */
15674       class_origin = decl_class_context (decl_or_origin);
15675       if (class_origin != NULL_TREE)
15676         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15677
15678       /* And its containing namespace.  */
15679       context_die = declare_in_namespace (decl_or_origin, context_die);
15680
15681       /* Now output the DIE to represent the data object itself.  This gets
15682          complicated because of the possibility that the VAR_DECL really
15683          represents an inlined instance of a formal parameter for an inline
15684          function.  */
15685       if (!origin)
15686         origin = decl_ultimate_origin (decl);
15687       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15688         gen_formal_parameter_die (decl, origin, context_die);
15689       else
15690         gen_variable_die (decl, origin, context_die);
15691       break;
15692
15693     case FIELD_DECL:
15694       /* Ignore the nameless fields that are used to skip bits but handle C++
15695          anonymous unions and structs.  */
15696       if (DECL_NAME (decl) != NULL_TREE
15697           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15698           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15699         {
15700           gen_type_die (member_declared_type (decl), context_die);
15701           gen_field_die (decl, context_die);
15702         }
15703       break;
15704
15705     case PARM_DECL:
15706       if (DECL_BY_REFERENCE (decl_or_origin))
15707         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15708       else
15709         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15710       gen_formal_parameter_die (decl, origin, context_die);
15711       break;
15712
15713     case NAMESPACE_DECL:
15714     case IMPORTED_DECL:
15715       gen_namespace_die (decl, context_die);
15716       break;
15717
15718     default:
15719       /* Probably some frontend-internal decl.  Assume we don't care.  */
15720       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15721       break;
15722     }
15723 }
15724 \f
15725 /* Output debug information for global decl DECL.  Called from toplev.c after
15726    compilation proper has finished.  */
15727
15728 static void
15729 dwarf2out_global_decl (tree decl)
15730 {
15731   /* Output DWARF2 information for file-scope tentative data object
15732      declarations, file-scope (extern) function declarations (which
15733      had no corresponding body) and file-scope tagged type declarations
15734      and definitions which have not yet been forced out.  */
15735   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15736     dwarf2out_decl (decl);
15737 }
15738
15739 /* Output debug information for type decl DECL.  Called from toplev.c
15740    and from language front ends (to record built-in types).  */
15741 static void
15742 dwarf2out_type_decl (tree decl, int local)
15743 {
15744   if (!local)
15745     dwarf2out_decl (decl);
15746 }
15747
15748 /* Output debug information for imported module or decl DECL.
15749    NAME is non-NULL name in the lexical block if the decl has been renamed.
15750    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15751    that DECL belongs to.
15752    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15753 static void
15754 dwarf2out_imported_module_or_decl_1 (tree decl,
15755                                      tree name,
15756                                      tree lexical_block,
15757                                      dw_die_ref lexical_block_die)
15758 {
15759   expanded_location xloc;
15760   dw_die_ref imported_die = NULL;
15761   dw_die_ref at_import_die;
15762
15763   if (TREE_CODE (decl) == IMPORTED_DECL)
15764     {
15765       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15766       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15767       gcc_assert (decl);
15768     }
15769   else
15770     xloc = expand_location (input_location);
15771
15772   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15773     {
15774       if (is_base_type (TREE_TYPE (decl)))
15775         at_import_die = base_type_die (TREE_TYPE (decl));
15776       else
15777         at_import_die = force_type_die (TREE_TYPE (decl));
15778       /* For namespace N { typedef void T; } using N::T; base_type_die
15779          returns NULL, but DW_TAG_imported_declaration requires
15780          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15781       if (!at_import_die)
15782         {
15783           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15784           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15785           at_import_die = lookup_type_die (TREE_TYPE (decl));
15786           gcc_assert (at_import_die);
15787         }
15788     }
15789   else
15790     {
15791       at_import_die = lookup_decl_die (decl);
15792       if (!at_import_die)
15793         {
15794           /* If we're trying to avoid duplicate debug info, we may not have
15795              emitted the member decl for this field.  Emit it now.  */
15796           if (TREE_CODE (decl) == FIELD_DECL)
15797             {
15798               tree type = DECL_CONTEXT (decl);
15799
15800               if (TYPE_CONTEXT (type)
15801                   && TYPE_P (TYPE_CONTEXT (type))
15802                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15803                                                 DINFO_USAGE_DIR_USE))
15804                 return;
15805               gen_type_die_for_member (type, decl,
15806                                        get_context_die (TYPE_CONTEXT (type)));
15807             }
15808           at_import_die = force_decl_die (decl);
15809         }
15810     }
15811
15812   if (TREE_CODE (decl) == NAMESPACE_DECL)
15813     imported_die = new_die (DW_TAG_imported_module,
15814                             lexical_block_die,
15815                             lexical_block);
15816   else
15817     imported_die = new_die (DW_TAG_imported_declaration,
15818                             lexical_block_die,
15819                             lexical_block);
15820
15821   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15822   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15823   if (name)
15824     add_AT_string (imported_die, DW_AT_name,
15825                    IDENTIFIER_POINTER (name));
15826   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15827 }
15828
15829 /* Output debug information for imported module or decl DECL.
15830    NAME is non-NULL name in context if the decl has been renamed.
15831    CHILD is true if decl is one of the renamed decls as part of
15832    importing whole module.  */
15833
15834 static void
15835 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15836                                    bool child)
15837 {
15838   /* dw_die_ref at_import_die;  */
15839   dw_die_ref scope_die;
15840
15841   if (debug_info_level <= DINFO_LEVEL_TERSE)
15842     return;
15843
15844   gcc_assert (decl);
15845
15846   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15847      We need decl DIE for reference and scope die. First, get DIE for the decl
15848      itself.  */
15849
15850   /* Get the scope die for decl context. Use comp_unit_die for global module
15851      or decl. If die is not found for non globals, force new die.  */
15852   if (context
15853       && TYPE_P (context)
15854       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15855     return;
15856   scope_die = get_context_die (context);
15857
15858   if (child)
15859     {
15860       gcc_assert (scope_die->die_child);
15861       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15862       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15863       scope_die = scope_die->die_child;
15864     }
15865
15866   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15867   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15868
15869 }
15870
15871 /* Write the debugging output for DECL.  */
15872
15873 void
15874 dwarf2out_decl (tree decl)
15875 {
15876   dw_die_ref context_die = comp_unit_die;
15877
15878   switch (TREE_CODE (decl))
15879     {
15880     case ERROR_MARK:
15881       return;
15882
15883     case FUNCTION_DECL:
15884       /* What we would really like to do here is to filter out all mere
15885          file-scope declarations of file-scope functions which are never
15886          referenced later within this translation unit (and keep all of ones
15887          that *are* referenced later on) but we aren't clairvoyant, so we have
15888          no idea which functions will be referenced in the future (i.e. later
15889          on within the current translation unit). So here we just ignore all
15890          file-scope function declarations which are not also definitions.  If
15891          and when the debugger needs to know something about these functions,
15892          it will have to hunt around and find the DWARF information associated
15893          with the definition of the function.
15894
15895          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15896          nodes represent definitions and which ones represent mere
15897          declarations.  We have to check DECL_INITIAL instead. That's because
15898          the C front-end supports some weird semantics for "extern inline"
15899          function definitions.  These can get inlined within the current
15900          translation unit (and thus, we need to generate Dwarf info for their
15901          abstract instances so that the Dwarf info for the concrete inlined
15902          instances can have something to refer to) but the compiler never
15903          generates any out-of-lines instances of such things (despite the fact
15904          that they *are* definitions).
15905
15906          The important point is that the C front-end marks these "extern
15907          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15908          them anyway. Note that the C++ front-end also plays some similar games
15909          for inline function definitions appearing within include files which
15910          also contain `#pragma interface' pragmas.  */
15911       if (DECL_INITIAL (decl) == NULL_TREE)
15912         return;
15913
15914       /* If we're a nested function, initially use a parent of NULL; if we're
15915          a plain function, this will be fixed up in decls_for_scope.  If
15916          we're a method, it will be ignored, since we already have a DIE.  */
15917       if (decl_function_context (decl)
15918           /* But if we're in terse mode, we don't care about scope.  */
15919           && debug_info_level > DINFO_LEVEL_TERSE)
15920         context_die = NULL;
15921       break;
15922
15923     case VAR_DECL:
15924       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15925          declaration and if the declaration was never even referenced from
15926          within this entire compilation unit.  We suppress these DIEs in
15927          order to save space in the .debug section (by eliminating entries
15928          which are probably useless).  Note that we must not suppress
15929          block-local extern declarations (whether used or not) because that
15930          would screw-up the debugger's name lookup mechanism and cause it to
15931          miss things which really ought to be in scope at a given point.  */
15932       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15933         return;
15934
15935       /* For local statics lookup proper context die.  */
15936       if (TREE_STATIC (decl) && decl_function_context (decl))
15937         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15938
15939       /* If we are in terse mode, don't generate any DIEs to represent any
15940          variable declarations or definitions.  */
15941       if (debug_info_level <= DINFO_LEVEL_TERSE)
15942         return;
15943       break;
15944
15945     case CONST_DECL:
15946       if (debug_info_level <= DINFO_LEVEL_TERSE)
15947         return;
15948       if (!is_fortran ())
15949         return;
15950       if (TREE_STATIC (decl) && decl_function_context (decl))
15951         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15952       break;
15953
15954     case NAMESPACE_DECL:
15955     case IMPORTED_DECL:
15956       if (debug_info_level <= DINFO_LEVEL_TERSE)
15957         return;
15958       if (lookup_decl_die (decl) != NULL)
15959         return;
15960       break;
15961
15962     case TYPE_DECL:
15963       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15964       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15965         return;
15966
15967       /* Don't bother trying to generate any DIEs to represent any of the
15968          normal built-in types for the language we are compiling.  */
15969       if (DECL_IS_BUILTIN (decl))
15970         {
15971           /* OK, we need to generate one for `bool' so GDB knows what type
15972              comparisons have.  */
15973           if (is_cxx ()
15974               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15975               && ! DECL_IGNORED_P (decl))
15976             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15977
15978           return;
15979         }
15980
15981       /* If we are in terse mode, don't generate any DIEs for types.  */
15982       if (debug_info_level <= DINFO_LEVEL_TERSE)
15983         return;
15984
15985       /* If we're a function-scope tag, initially use a parent of NULL;
15986          this will be fixed up in decls_for_scope.  */
15987       if (decl_function_context (decl))
15988         context_die = NULL;
15989
15990       break;
15991
15992     default:
15993       return;
15994     }
15995
15996   gen_decl_die (decl, NULL, context_die);
15997 }
15998
15999 /* Output a marker (i.e. a label) for the beginning of the generated code for
16000    a lexical block.  */
16001
16002 static void
16003 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
16004                        unsigned int blocknum)
16005 {
16006   switch_to_section (current_function_section ());
16007   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
16008 }
16009
16010 /* Output a marker (i.e. a label) for the end of the generated code for a
16011    lexical block.  */
16012
16013 static void
16014 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
16015 {
16016   switch_to_section (current_function_section ());
16017   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
16018 }
16019
16020 /* Returns nonzero if it is appropriate not to emit any debugging
16021    information for BLOCK, because it doesn't contain any instructions.
16022
16023    Don't allow this for blocks with nested functions or local classes
16024    as we would end up with orphans, and in the presence of scheduling
16025    we may end up calling them anyway.  */
16026
16027 static bool
16028 dwarf2out_ignore_block (const_tree block)
16029 {
16030   tree decl;
16031   unsigned int i;
16032
16033   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
16034     if (TREE_CODE (decl) == FUNCTION_DECL
16035         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16036       return 0;
16037   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
16038     {
16039       decl = BLOCK_NONLOCALIZED_VAR (block, i);
16040       if (TREE_CODE (decl) == FUNCTION_DECL
16041           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16042       return 0;
16043     }
16044
16045   return 1;
16046 }
16047
16048 /* Hash table routines for file_hash.  */
16049
16050 static int
16051 file_table_eq (const void *p1_p, const void *p2_p)
16052 {
16053   const struct dwarf_file_data *const p1 =
16054     (const struct dwarf_file_data *) p1_p;
16055   const char *const p2 = (const char *) p2_p;
16056   return strcmp (p1->filename, p2) == 0;
16057 }
16058
16059 static hashval_t
16060 file_table_hash (const void *p_p)
16061 {
16062   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
16063   return htab_hash_string (p->filename);
16064 }
16065
16066 /* Lookup FILE_NAME (in the list of filenames that we know about here in
16067    dwarf2out.c) and return its "index".  The index of each (known) filename is
16068    just a unique number which is associated with only that one filename.  We
16069    need such numbers for the sake of generating labels (in the .debug_sfnames
16070    section) and references to those files numbers (in the .debug_srcinfo
16071    and.debug_macinfo sections).  If the filename given as an argument is not
16072    found in our current list, add it to the list and assign it the next
16073    available unique index number.  In order to speed up searches, we remember
16074    the index of the filename was looked up last.  This handles the majority of
16075    all searches.  */
16076
16077 static struct dwarf_file_data *
16078 lookup_filename (const char *file_name)
16079 {
16080   void ** slot;
16081   struct dwarf_file_data * created;
16082
16083   /* Check to see if the file name that was searched on the previous
16084      call matches this file name.  If so, return the index.  */
16085   if (file_table_last_lookup
16086       && (file_name == file_table_last_lookup->filename
16087           || strcmp (file_table_last_lookup->filename, file_name) == 0))
16088     return file_table_last_lookup;
16089
16090   /* Didn't match the previous lookup, search the table.  */
16091   slot = htab_find_slot_with_hash (file_table, file_name,
16092                                    htab_hash_string (file_name), INSERT);
16093   if (*slot)
16094     return (struct dwarf_file_data *) *slot;
16095
16096   created = GGC_NEW (struct dwarf_file_data);
16097   created->filename = file_name;
16098   created->emitted_number = 0;
16099   *slot = created;
16100   return created;
16101 }
16102
16103 /* If the assembler will construct the file table, then translate the compiler
16104    internal file table number into the assembler file table number, and emit
16105    a .file directive if we haven't already emitted one yet.  The file table
16106    numbers are different because we prune debug info for unused variables and
16107    types, which may include filenames.  */
16108
16109 static int
16110 maybe_emit_file (struct dwarf_file_data * fd)
16111 {
16112   if (! fd->emitted_number)
16113     {
16114       if (last_emitted_file)
16115         fd->emitted_number = last_emitted_file->emitted_number + 1;
16116       else
16117         fd->emitted_number = 1;
16118       last_emitted_file = fd;
16119
16120       if (DWARF2_ASM_LINE_DEBUG_INFO)
16121         {
16122           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
16123           output_quoted_string (asm_out_file,
16124                                 remap_debug_filename (fd->filename));
16125           fputc ('\n', asm_out_file);
16126         }
16127     }
16128
16129   return fd->emitted_number;
16130 }
16131
16132 /* Replace DW_AT_name for the decl with name.  */
16133  
16134 static void
16135 dwarf2out_set_name (tree decl, tree name)
16136 {
16137   dw_die_ref die;
16138   dw_attr_ref attr;
16139
16140   die = TYPE_SYMTAB_DIE (decl);
16141   if (!die)
16142     return;
16143
16144   attr = get_AT (die, DW_AT_name);
16145   if (attr)
16146     {
16147       struct indirect_string_node *node;
16148
16149       node = find_AT_string (dwarf2_name (name, 0));
16150       /* replace the string.  */
16151       attr->dw_attr_val.v.val_str = node;
16152     }
16153
16154   else
16155     add_name_attribute (die, dwarf2_name (name, 0));
16156 }
16157 /* Called by the final INSN scan whenever we see a var location.  We
16158    use it to drop labels in the right places, and throw the location in
16159    our lookup table.  */
16160
16161 static void
16162 dwarf2out_var_location (rtx loc_note)
16163 {
16164   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
16165   struct var_loc_node *newloc;
16166   rtx prev_insn;
16167   static rtx last_insn;
16168   static const char *last_label;
16169   tree decl;
16170
16171   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
16172     return;
16173   prev_insn = PREV_INSN (loc_note);
16174
16175   newloc = GGC_CNEW (struct var_loc_node);
16176   /* If the insn we processed last time is the previous insn
16177      and it is also a var location note, use the label we emitted
16178      last time.  */
16179   if (last_insn != NULL_RTX
16180       && last_insn == prev_insn
16181       && NOTE_P (prev_insn)
16182       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
16183     {
16184       newloc->label = last_label;
16185     }
16186   else
16187     {
16188       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
16189       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
16190       loclabel_num++;
16191       newloc->label = ggc_strdup (loclabel);
16192     }
16193   newloc->var_loc_note = loc_note;
16194   newloc->next = NULL;
16195
16196   if (cfun && in_cold_section_p)
16197     newloc->section_label = crtl->subsections.cold_section_label;
16198   else
16199     newloc->section_label = text_section_label;
16200
16201   last_insn = loc_note;
16202   last_label = newloc->label;
16203   decl = NOTE_VAR_LOCATION_DECL (loc_note);
16204   add_var_loc_to_decl (decl, newloc);
16205 }
16206
16207 /* We need to reset the locations at the beginning of each
16208    function. We can't do this in the end_function hook, because the
16209    declarations that use the locations won't have been output when
16210    that hook is called.  Also compute have_multiple_function_sections here.  */
16211
16212 static void
16213 dwarf2out_begin_function (tree fun)
16214 {
16215   htab_empty (decl_loc_table);
16216
16217   if (function_section (fun) != text_section)
16218     have_multiple_function_sections = true;
16219
16220   dwarf2out_note_section_used ();
16221 }
16222
16223 /* Output a label to mark the beginning of a source code line entry
16224    and record information relating to this source line, in
16225    'line_info_table' for later output of the .debug_line section.  */
16226
16227 static void
16228 dwarf2out_source_line (unsigned int line, const char *filename,
16229                        int discriminator ATTRIBUTE_UNUSED)
16230 {
16231   if (debug_info_level >= DINFO_LEVEL_NORMAL
16232       && line != 0)
16233     {
16234       int file_num = maybe_emit_file (lookup_filename (filename));
16235
16236       switch_to_section (current_function_section ());
16237
16238       /* If requested, emit something human-readable.  */
16239       if (flag_debug_asm)
16240         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
16241                  filename, line);
16242
16243       if (DWARF2_ASM_LINE_DEBUG_INFO)
16244         {
16245           /* Emit the .loc directive understood by GNU as.  */
16246           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
16247 #ifdef HAVE_GAS_DISCRIMINATOR
16248           if (discriminator != 0)
16249             fprintf (asm_out_file, " discriminator %d", discriminator);
16250 #endif /* HAVE_GAS_DISCRIMINATOR */
16251           fputc ('\n', asm_out_file);
16252
16253           /* Indicate that line number info exists.  */
16254           line_info_table_in_use++;
16255         }
16256       else if (function_section (current_function_decl) != text_section)
16257         {
16258           dw_separate_line_info_ref line_info;
16259           targetm.asm_out.internal_label (asm_out_file,
16260                                           SEPARATE_LINE_CODE_LABEL,
16261                                           separate_line_info_table_in_use);
16262
16263           /* Expand the line info table if necessary.  */
16264           if (separate_line_info_table_in_use
16265               == separate_line_info_table_allocated)
16266             {
16267               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16268               separate_line_info_table
16269                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
16270                                  separate_line_info_table,
16271                                  separate_line_info_table_allocated);
16272               memset (separate_line_info_table
16273                        + separate_line_info_table_in_use,
16274                       0,
16275                       (LINE_INFO_TABLE_INCREMENT
16276                        * sizeof (dw_separate_line_info_entry)));
16277             }
16278
16279           /* Add the new entry at the end of the line_info_table.  */
16280           line_info
16281             = &separate_line_info_table[separate_line_info_table_in_use++];
16282           line_info->dw_file_num = file_num;
16283           line_info->dw_line_num = line;
16284           line_info->function = current_function_funcdef_no;
16285         }
16286       else
16287         {
16288           dw_line_info_ref line_info;
16289
16290           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
16291                                      line_info_table_in_use);
16292
16293           /* Expand the line info table if necessary.  */
16294           if (line_info_table_in_use == line_info_table_allocated)
16295             {
16296               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16297               line_info_table
16298                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
16299                                  line_info_table_allocated);
16300               memset (line_info_table + line_info_table_in_use, 0,
16301                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
16302             }
16303
16304           /* Add the new entry at the end of the line_info_table.  */
16305           line_info = &line_info_table[line_info_table_in_use++];
16306           line_info->dw_file_num = file_num;
16307           line_info->dw_line_num = line;
16308         }
16309     }
16310 }
16311
16312 /* Record the beginning of a new source file.  */
16313
16314 static void
16315 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16316 {
16317   if (flag_eliminate_dwarf2_dups)
16318     {
16319       /* Record the beginning of the file for break_out_includes.  */
16320       dw_die_ref bincl_die;
16321
16322       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16323       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16324     }
16325
16326   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16327     {
16328       int file_num = maybe_emit_file (lookup_filename (filename));
16329
16330       switch_to_section (debug_macinfo_section);
16331       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16332       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16333                                    lineno);
16334
16335       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16336     }
16337 }
16338
16339 /* Record the end of a source file.  */
16340
16341 static void
16342 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16343 {
16344   if (flag_eliminate_dwarf2_dups)
16345     /* Record the end of the file for break_out_includes.  */
16346     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16347
16348   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16349     {
16350       switch_to_section (debug_macinfo_section);
16351       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16352     }
16353 }
16354
16355 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16356    the tail part of the directive line, i.e. the part which is past the
16357    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16358
16359 static void
16360 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16361                   const char *buffer ATTRIBUTE_UNUSED)
16362 {
16363   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16364     {
16365       switch_to_section (debug_macinfo_section);
16366       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16367       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16368       dw2_asm_output_nstring (buffer, -1, "The macro");
16369     }
16370 }
16371
16372 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16373    the tail part of the directive line, i.e. the part which is past the
16374    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16375
16376 static void
16377 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16378                  const char *buffer ATTRIBUTE_UNUSED)
16379 {
16380   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16381     {
16382       switch_to_section (debug_macinfo_section);
16383       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16384       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16385       dw2_asm_output_nstring (buffer, -1, "The macro");
16386     }
16387 }
16388
16389 /* Set up for Dwarf output at the start of compilation.  */
16390
16391 static void
16392 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16393 {
16394   /* Allocate the file_table.  */
16395   file_table = htab_create_ggc (50, file_table_hash,
16396                                 file_table_eq, NULL);
16397
16398   /* Allocate the decl_die_table.  */
16399   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16400                                     decl_die_table_eq, NULL);
16401
16402   /* Allocate the decl_loc_table.  */
16403   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16404                                     decl_loc_table_eq, NULL);
16405
16406   /* Allocate the initial hunk of the decl_scope_table.  */
16407   decl_scope_table = VEC_alloc (tree, gc, 256);
16408
16409   /* Allocate the initial hunk of the abbrev_die_table.  */
16410   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16411   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16412   /* Zero-th entry is allocated, but unused.  */
16413   abbrev_die_table_in_use = 1;
16414
16415   /* Allocate the initial hunk of the line_info_table.  */
16416   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16417   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16418
16419   /* Zero-th entry is allocated, but unused.  */
16420   line_info_table_in_use = 1;
16421
16422   /* Allocate the pubtypes and pubnames vectors.  */
16423   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16424   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16425
16426   /* Generate the initial DIE for the .debug section.  Note that the (string)
16427      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16428      will (typically) be a relative pathname and that this pathname should be
16429      taken as being relative to the directory from which the compiler was
16430      invoked when the given (base) source file was compiled.  We will fill
16431      in this value in dwarf2out_finish.  */
16432   comp_unit_die = gen_compile_unit_die (NULL);
16433
16434   incomplete_types = VEC_alloc (tree, gc, 64);
16435
16436   used_rtx_array = VEC_alloc (rtx, gc, 32);
16437
16438   debug_info_section = get_section (DEBUG_INFO_SECTION,
16439                                     SECTION_DEBUG, NULL);
16440   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16441                                       SECTION_DEBUG, NULL);
16442   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16443                                        SECTION_DEBUG, NULL);
16444   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16445                                        SECTION_DEBUG, NULL);
16446   debug_line_section = get_section (DEBUG_LINE_SECTION,
16447                                     SECTION_DEBUG, NULL);
16448   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16449                                    SECTION_DEBUG, NULL);
16450   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16451                                         SECTION_DEBUG, NULL);
16452 #ifdef DEBUG_PUBTYPES_SECTION
16453   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16454                                         SECTION_DEBUG, NULL);
16455 #endif
16456   debug_str_section = get_section (DEBUG_STR_SECTION,
16457                                    DEBUG_STR_SECTION_FLAGS, NULL);
16458   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16459                                       SECTION_DEBUG, NULL);
16460   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16461                                      SECTION_DEBUG, NULL);
16462
16463   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16464   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16465                                DEBUG_ABBREV_SECTION_LABEL, 0);
16466   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16467   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16468                                COLD_TEXT_SECTION_LABEL, 0);
16469   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16470
16471   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16472                                DEBUG_INFO_SECTION_LABEL, 0);
16473   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16474                                DEBUG_LINE_SECTION_LABEL, 0);
16475   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16476                                DEBUG_RANGES_SECTION_LABEL, 0);
16477   switch_to_section (debug_abbrev_section);
16478   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16479   switch_to_section (debug_info_section);
16480   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16481   switch_to_section (debug_line_section);
16482   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16483
16484   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16485     {
16486       switch_to_section (debug_macinfo_section);
16487       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16488                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16489       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16490     }
16491
16492   switch_to_section (text_section);
16493   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16494   if (flag_reorder_blocks_and_partition)
16495     {
16496       cold_text_section = unlikely_text_section ();
16497       switch_to_section (cold_text_section);
16498       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16499     }
16500 }
16501
16502 /* A helper function for dwarf2out_finish called through
16503    ht_forall.  Emit one queued .debug_str string.  */
16504
16505 static int
16506 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16507 {
16508   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16509
16510   if (node->form == DW_FORM_strp)
16511     {
16512       switch_to_section (debug_str_section);
16513       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16514       assemble_string (node->str, strlen (node->str) + 1);
16515     }
16516
16517   return 1;
16518 }
16519
16520 #if ENABLE_ASSERT_CHECKING
16521 /* Verify that all marks are clear.  */
16522
16523 static void
16524 verify_marks_clear (dw_die_ref die)
16525 {
16526   dw_die_ref c;
16527
16528   gcc_assert (! die->die_mark);
16529   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16530 }
16531 #endif /* ENABLE_ASSERT_CHECKING */
16532
16533 /* Clear the marks for a die and its children.
16534    Be cool if the mark isn't set.  */
16535
16536 static void
16537 prune_unmark_dies (dw_die_ref die)
16538 {
16539   dw_die_ref c;
16540
16541   if (die->die_mark)
16542     die->die_mark = 0;
16543   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16544 }
16545
16546 /* Given DIE that we're marking as used, find any other dies
16547    it references as attributes and mark them as used.  */
16548
16549 static void
16550 prune_unused_types_walk_attribs (dw_die_ref die)
16551 {
16552   dw_attr_ref a;
16553   unsigned ix;
16554
16555   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16556     {
16557       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16558         {
16559           /* A reference to another DIE.
16560              Make sure that it will get emitted.  */
16561           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16562         }
16563       /* Set the string's refcount to 0 so that prune_unused_types_mark
16564          accounts properly for it.  */
16565       if (AT_class (a) == dw_val_class_str)
16566         a->dw_attr_val.v.val_str->refcount = 0;
16567     }
16568 }
16569
16570
16571 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16572    to DIE's children.  */
16573
16574 static void
16575 prune_unused_types_mark (dw_die_ref die, int dokids)
16576 {
16577   dw_die_ref c;
16578
16579   if (die->die_mark == 0)
16580     {
16581       /* We haven't done this node yet.  Mark it as used.  */
16582       die->die_mark = 1;
16583
16584       /* We also have to mark its parents as used.
16585          (But we don't want to mark our parents' kids due to this.)  */
16586       if (die->die_parent)
16587         prune_unused_types_mark (die->die_parent, 0);
16588
16589       /* Mark any referenced nodes.  */
16590       prune_unused_types_walk_attribs (die);
16591
16592       /* If this node is a specification,
16593          also mark the definition, if it exists.  */
16594       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16595         prune_unused_types_mark (die->die_definition, 1);
16596     }
16597
16598   if (dokids && die->die_mark != 2)
16599     {
16600       /* We need to walk the children, but haven't done so yet.
16601          Remember that we've walked the kids.  */
16602       die->die_mark = 2;
16603
16604       /* If this is an array type, we need to make sure our
16605          kids get marked, even if they're types.  */
16606       if (die->die_tag == DW_TAG_array_type)
16607         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16608       else
16609         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16610     }
16611 }
16612
16613 /* For local classes, look if any static member functions were emitted
16614    and if so, mark them.  */
16615
16616 static void
16617 prune_unused_types_walk_local_classes (dw_die_ref die)
16618 {
16619   dw_die_ref c;
16620
16621   if (die->die_mark == 2)
16622     return;
16623
16624   switch (die->die_tag)
16625     {
16626     case DW_TAG_structure_type:
16627     case DW_TAG_union_type:
16628     case DW_TAG_class_type:
16629       break;
16630
16631     case DW_TAG_subprogram:
16632       if (!get_AT_flag (die, DW_AT_declaration)
16633           || die->die_definition != NULL)
16634         prune_unused_types_mark (die, 1);
16635       return;
16636
16637     default:
16638       return;
16639     }
16640
16641   /* Mark children.  */
16642   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16643 }
16644
16645 /* Walk the tree DIE and mark types that we actually use.  */
16646
16647 static void
16648 prune_unused_types_walk (dw_die_ref die)
16649 {
16650   dw_die_ref c;
16651
16652   /* Don't do anything if this node is already marked and
16653      children have been marked as well.  */
16654   if (die->die_mark == 2)
16655     return;
16656
16657   switch (die->die_tag)
16658     {
16659     case DW_TAG_structure_type:
16660     case DW_TAG_union_type:
16661     case DW_TAG_class_type:
16662       if (die->die_perennial_p)
16663         break;
16664
16665       for (c = die->die_parent; c; c = c->die_parent)
16666         if (c->die_tag == DW_TAG_subprogram)
16667           break;
16668
16669       /* Finding used static member functions inside of classes
16670          is needed just for local classes, because for other classes
16671          static member function DIEs with DW_AT_specification
16672          are emitted outside of the DW_TAG_*_type.  If we ever change
16673          it, we'd need to call this even for non-local classes.  */
16674       if (c)
16675         prune_unused_types_walk_local_classes (die);
16676
16677       /* It's a type node --- don't mark it.  */
16678       return;
16679
16680     case DW_TAG_const_type:
16681     case DW_TAG_packed_type:
16682     case DW_TAG_pointer_type:
16683     case DW_TAG_reference_type:
16684     case DW_TAG_volatile_type:
16685     case DW_TAG_typedef:
16686     case DW_TAG_array_type:
16687     case DW_TAG_interface_type:
16688     case DW_TAG_friend:
16689     case DW_TAG_variant_part:
16690     case DW_TAG_enumeration_type:
16691     case DW_TAG_subroutine_type:
16692     case DW_TAG_string_type:
16693     case DW_TAG_set_type:
16694     case DW_TAG_subrange_type:
16695     case DW_TAG_ptr_to_member_type:
16696     case DW_TAG_file_type:
16697       if (die->die_perennial_p)
16698         break;
16699
16700       /* It's a type node --- don't mark it.  */
16701       return;
16702
16703     default:
16704       /* Mark everything else.  */
16705       break;
16706   }
16707
16708   if (die->die_mark == 0)
16709     {
16710       die->die_mark = 1;
16711
16712       /* Now, mark any dies referenced from here.  */
16713       prune_unused_types_walk_attribs (die);
16714     }
16715
16716   die->die_mark = 2;
16717
16718   /* Mark children.  */
16719   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16720 }
16721
16722 /* Increment the string counts on strings referred to from DIE's
16723    attributes.  */
16724
16725 static void
16726 prune_unused_types_update_strings (dw_die_ref die)
16727 {
16728   dw_attr_ref a;
16729   unsigned ix;
16730
16731   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16732     if (AT_class (a) == dw_val_class_str)
16733       {
16734         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16735         s->refcount++;
16736         /* Avoid unnecessarily putting strings that are used less than
16737            twice in the hash table.  */
16738         if (s->refcount
16739             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16740           {
16741             void ** slot;
16742             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16743                                              htab_hash_string (s->str),
16744                                              INSERT);
16745             gcc_assert (*slot == NULL);
16746             *slot = s;
16747           }
16748       }
16749 }
16750
16751 /* Remove from the tree DIE any dies that aren't marked.  */
16752
16753 static void
16754 prune_unused_types_prune (dw_die_ref die)
16755 {
16756   dw_die_ref c;
16757
16758   gcc_assert (die->die_mark);
16759   prune_unused_types_update_strings (die);
16760
16761   if (! die->die_child)
16762     return;
16763
16764   c = die->die_child;
16765   do {
16766     dw_die_ref prev = c;
16767     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16768       if (c == die->die_child)
16769         {
16770           /* No marked children between 'prev' and the end of the list.  */
16771           if (prev == c)
16772             /* No marked children at all.  */
16773             die->die_child = NULL;
16774           else
16775             {
16776               prev->die_sib = c->die_sib;
16777               die->die_child = prev;
16778             }
16779           return;
16780         }
16781
16782     if (c != prev->die_sib)
16783       prev->die_sib = c;
16784     prune_unused_types_prune (c);
16785   } while (c != die->die_child);
16786 }
16787
16788
16789 /* Remove dies representing declarations that we never use.  */
16790
16791 static void
16792 prune_unused_types (void)
16793 {
16794   unsigned int i;
16795   limbo_die_node *node;
16796   pubname_ref pub;
16797
16798 #if ENABLE_ASSERT_CHECKING
16799   /* All the marks should already be clear.  */
16800   verify_marks_clear (comp_unit_die);
16801   for (node = limbo_die_list; node; node = node->next)
16802     verify_marks_clear (node->die);
16803 #endif /* ENABLE_ASSERT_CHECKING */
16804
16805   /* Set the mark on nodes that are actually used.  */
16806   prune_unused_types_walk (comp_unit_die);
16807   for (node = limbo_die_list; node; node = node->next)
16808     prune_unused_types_walk (node->die);
16809
16810   /* Also set the mark on nodes referenced from the
16811      pubname_table or arange_table.  */
16812   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16813     prune_unused_types_mark (pub->die, 1);
16814   for (i = 0; i < arange_table_in_use; i++)
16815     prune_unused_types_mark (arange_table[i], 1);
16816
16817   /* Get rid of nodes that aren't marked; and update the string counts.  */
16818   if (debug_str_hash)
16819     htab_empty (debug_str_hash);
16820   prune_unused_types_prune (comp_unit_die);
16821   for (node = limbo_die_list; node; node = node->next)
16822     prune_unused_types_prune (node->die);
16823
16824   /* Leave the marks clear.  */
16825   prune_unmark_dies (comp_unit_die);
16826   for (node = limbo_die_list; node; node = node->next)
16827     prune_unmark_dies (node->die);
16828 }
16829
16830 /* Set the parameter to true if there are any relative pathnames in
16831    the file table.  */
16832 static int
16833 file_table_relative_p (void ** slot, void *param)
16834 {
16835   bool *p = (bool *) param;
16836   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16837   if (!IS_ABSOLUTE_PATH (d->filename))
16838     {
16839       *p = true;
16840       return 0;
16841     }
16842   return 1;
16843 }
16844
16845 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
16846    to the location it would have been added, should we know its
16847    DECL_ASSEMBLER_NAME when we added other attributes.  This will
16848    probably improve compactness of debug info, removing equivalent
16849    abbrevs, and hide any differences caused by deferring the
16850    computation of the assembler name, triggered by e.g. PCH.  */
16851
16852 static inline void
16853 move_linkage_attr (dw_die_ref die)
16854 {
16855   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
16856   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
16857
16858   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
16859
16860   while (--ix > 0)
16861     {
16862       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
16863
16864       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
16865         break;
16866     }
16867
16868   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
16869     {
16870       VEC_pop (dw_attr_node, die->die_attr);
16871       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
16872     }
16873 }
16874
16875 /* Output stuff that dwarf requires at the end of every file,
16876    and generate the DWARF-2 debugging info.  */
16877
16878 static void
16879 dwarf2out_finish (const char *filename)
16880 {
16881   limbo_die_node *node, *next_node;
16882   dw_die_ref die = 0;
16883   unsigned int i;
16884
16885   /* Add the name for the main input file now.  We delayed this from
16886      dwarf2out_init to avoid complications with PCH.  */
16887   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16888   if (!IS_ABSOLUTE_PATH (filename))
16889     add_comp_dir_attribute (comp_unit_die);
16890   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16891     {
16892       bool p = false;
16893       htab_traverse (file_table, file_table_relative_p, &p);
16894       if (p)
16895         add_comp_dir_attribute (comp_unit_die);
16896     }
16897
16898   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16899     {
16900       add_location_or_const_value_attribute (
16901         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16902         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16903         DW_AT_location);
16904     }
16905
16906   /* Traverse the limbo die list, and add parent/child links.  The only
16907      dies without parents that should be here are concrete instances of
16908      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16909      For concrete instances, we can get the parent die from the abstract
16910      instance.  */
16911   for (node = limbo_die_list; node; node = next_node)
16912     {
16913       next_node = node->next;
16914       die = node->die;
16915
16916       if (die->die_parent == NULL)
16917         {
16918           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16919
16920           if (origin)
16921             add_child_die (origin->die_parent, die);
16922           else if (die == comp_unit_die)
16923             ;
16924           else if (errorcount > 0 || sorrycount > 0)
16925             /* It's OK to be confused by errors in the input.  */
16926             add_child_die (comp_unit_die, die);
16927           else
16928             {
16929               /* In certain situations, the lexical block containing a
16930                  nested function can be optimized away, which results
16931                  in the nested function die being orphaned.  Likewise
16932                  with the return type of that nested function.  Force
16933                  this to be a child of the containing function.
16934
16935                  It may happen that even the containing function got fully
16936                  inlined and optimized out.  In that case we are lost and
16937                  assign the empty child.  This should not be big issue as
16938                  the function is likely unreachable too.  */
16939               tree context = NULL_TREE;
16940
16941               gcc_assert (node->created_for);
16942
16943               if (DECL_P (node->created_for))
16944                 context = DECL_CONTEXT (node->created_for);
16945               else if (TYPE_P (node->created_for))
16946                 context = TYPE_CONTEXT (node->created_for);
16947
16948               gcc_assert (context
16949                           && (TREE_CODE (context) == FUNCTION_DECL
16950                               || TREE_CODE (context) == NAMESPACE_DECL));
16951
16952               origin = lookup_decl_die (context);
16953               if (origin)
16954                 add_child_die (origin, die);
16955               else
16956                 add_child_die (comp_unit_die, die);
16957             }
16958         }
16959     }
16960
16961   limbo_die_list = NULL;
16962
16963   for (node = deferred_asm_name; node; node = node->next)
16964     {
16965       tree decl = node->created_for;
16966       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16967         {
16968           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
16969                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
16970           move_linkage_attr (node->die);
16971         }
16972     }
16973
16974   deferred_asm_name = NULL;
16975
16976   /* Walk through the list of incomplete types again, trying once more to
16977      emit full debugging info for them.  */
16978   retry_incomplete_types ();
16979
16980   if (flag_eliminate_unused_debug_types)
16981     prune_unused_types ();
16982
16983   /* Generate separate CUs for each of the include files we've seen.
16984      They will go into limbo_die_list.  */
16985   if (flag_eliminate_dwarf2_dups)
16986     break_out_includes (comp_unit_die);
16987
16988   /* Traverse the DIE's and add add sibling attributes to those DIE's
16989      that have children.  */
16990   add_sibling_attributes (comp_unit_die);
16991   for (node = limbo_die_list; node; node = node->next)
16992     add_sibling_attributes (node->die);
16993
16994   /* Output a terminator label for the .text section.  */
16995   switch_to_section (text_section);
16996   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16997   if (flag_reorder_blocks_and_partition)
16998     {
16999       switch_to_section (unlikely_text_section ());
17000       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
17001     }
17002
17003   /* We can only use the low/high_pc attributes if all of the code was
17004      in .text.  */
17005   if (!have_multiple_function_sections)
17006     {
17007       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
17008       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
17009     }
17010
17011   else
17012     {
17013       unsigned fde_idx = 0;
17014
17015       /* We need to give .debug_loc and .debug_ranges an appropriate
17016          "base address".  Use zero so that these addresses become
17017          absolute.  Historically, we've emitted the unexpected
17018          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
17019          Emit both to give time for other tools to adapt.  */
17020       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
17021       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
17022
17023       add_AT_range_list (comp_unit_die, DW_AT_ranges,
17024                          add_ranges_by_labels (text_section_label,
17025                                                text_end_label));
17026       if (flag_reorder_blocks_and_partition)
17027         add_ranges_by_labels (cold_text_section_label,
17028                               cold_end_label);
17029
17030       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
17031         {
17032           dw_fde_ref fde = &fde_table[fde_idx];
17033
17034           if (fde->dw_fde_switched_sections)
17035             {
17036               add_ranges_by_labels (fde->dw_fde_hot_section_label,
17037                                     fde->dw_fde_hot_section_end_label);
17038               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
17039                                     fde->dw_fde_unlikely_section_end_label);
17040             }
17041           else
17042             add_ranges_by_labels (fde->dw_fde_begin,
17043                                   fde->dw_fde_end);
17044         }
17045
17046       add_ranges (NULL);
17047     }
17048
17049   /* Output location list section if necessary.  */
17050   if (have_location_lists)
17051     {
17052       /* Output the location lists info.  */
17053       switch_to_section (debug_loc_section);
17054       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
17055                                    DEBUG_LOC_SECTION_LABEL, 0);
17056       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
17057       output_location_lists (die);
17058     }
17059
17060   if (debug_info_level >= DINFO_LEVEL_NORMAL)
17061     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
17062                     debug_line_section_label);
17063
17064   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17065     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
17066
17067   /* Output all of the compilation units.  We put the main one last so that
17068      the offsets are available to output_pubnames.  */
17069   for (node = limbo_die_list; node; node = node->next)
17070     output_comp_unit (node->die, 0);
17071
17072   /* Output the main compilation unit if non-empty or if .debug_macinfo
17073      has been emitted.  */
17074   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
17075
17076   /* Output the abbreviation table.  */
17077   switch_to_section (debug_abbrev_section);
17078   output_abbrev_section ();
17079
17080   /* Output public names table if necessary.  */
17081   if (!VEC_empty (pubname_entry, pubname_table))
17082     {
17083       switch_to_section (debug_pubnames_section);
17084       output_pubnames (pubname_table);
17085     }
17086
17087 #ifdef DEBUG_PUBTYPES_SECTION
17088   /* Output public types table if necessary.  */
17089   if (!VEC_empty (pubname_entry, pubtype_table))
17090     {
17091       switch_to_section (debug_pubtypes_section);
17092       output_pubnames (pubtype_table);
17093     }
17094 #endif
17095
17096   /* Output the address range information.  We only put functions in the arange
17097      table, so don't write it out if we don't have any.  */
17098   if (fde_table_in_use)
17099     {
17100       switch_to_section (debug_aranges_section);
17101       output_aranges ();
17102     }
17103
17104   /* Output ranges section if necessary.  */
17105   if (ranges_table_in_use)
17106     {
17107       switch_to_section (debug_ranges_section);
17108       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
17109       output_ranges ();
17110     }
17111
17112   /* Output the source line correspondence table.  We must do this
17113      even if there is no line information.  Otherwise, on an empty
17114      translation unit, we will generate a present, but empty,
17115      .debug_info section.  IRIX 6.5 `nm' will then complain when
17116      examining the file.  This is done late so that any filenames
17117      used by the debug_info section are marked as 'used'.  */
17118   if (! DWARF2_ASM_LINE_DEBUG_INFO)
17119     {
17120       switch_to_section (debug_line_section);
17121       output_line_info ();
17122     }
17123
17124   /* Have to end the macro section.  */
17125   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17126     {
17127       switch_to_section (debug_macinfo_section);
17128       dw2_asm_output_data (1, 0, "End compilation unit");
17129     }
17130
17131   /* If we emitted any DW_FORM_strp form attribute, output the string
17132      table too.  */
17133   if (debug_str_hash)
17134     htab_traverse (debug_str_hash, output_indirect_string, NULL);
17135 }
17136 #else
17137
17138 /* This should never be used, but its address is needed for comparisons.  */
17139 const struct gcc_debug_hooks dwarf2_debug_hooks;
17140
17141 #endif /* DWARF2_DEBUGGING_INFO */
17142
17143 #include "gt-dwarf2out.h"