OSDN Git Service

./:
[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 typedef struct dw_val_struct *dw_val_ref;
3762 typedef struct die_struct *dw_die_ref;
3763 typedef const struct die_struct *const_dw_die_ref;
3764 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3765 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3766
3767 typedef struct GTY(()) deferred_locations_struct
3768 {
3769   tree variable;
3770   dw_die_ref die;
3771 } deferred_locations;
3772
3773 DEF_VEC_O(deferred_locations);
3774 DEF_VEC_ALLOC_O(deferred_locations,gc);
3775
3776 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3777
3778 /* Each DIE may have a series of attribute/value pairs.  Values
3779    can take on several forms.  The forms that are used in this
3780    implementation are listed below.  */
3781
3782 enum dw_val_class
3783 {
3784   dw_val_class_addr,
3785   dw_val_class_offset,
3786   dw_val_class_loc,
3787   dw_val_class_loc_list,
3788   dw_val_class_range_list,
3789   dw_val_class_const,
3790   dw_val_class_unsigned_const,
3791   dw_val_class_long_long,
3792   dw_val_class_vec,
3793   dw_val_class_flag,
3794   dw_val_class_die_ref,
3795   dw_val_class_fde_ref,
3796   dw_val_class_lbl_id,
3797   dw_val_class_lineptr,
3798   dw_val_class_str,
3799   dw_val_class_macptr,
3800   dw_val_class_file
3801 };
3802
3803 /* Describe a double word constant value.  */
3804 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3805
3806 typedef struct GTY(()) dw_long_long_struct {
3807   unsigned long hi;
3808   unsigned long low;
3809 }
3810 dw_long_long_const;
3811
3812 /* Describe a floating point constant value, or a vector constant value.  */
3813
3814 typedef struct GTY(()) dw_vec_struct {
3815   unsigned char * GTY((length ("%h.length"))) array;
3816   unsigned length;
3817   unsigned elt_size;
3818 }
3819 dw_vec_const;
3820
3821 /* The dw_val_node describes an attribute's value, as it is
3822    represented internally.  */
3823
3824 typedef struct GTY(()) dw_val_struct {
3825   enum dw_val_class val_class;
3826   union dw_val_struct_union
3827     {
3828       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3829       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3830       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3831       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3832       HOST_WIDE_INT GTY ((default)) val_int;
3833       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3834       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3835       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3836       struct dw_val_die_union
3837         {
3838           dw_die_ref die;
3839           int external;
3840         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3841       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3842       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3843       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3844       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3845       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3846     }
3847   GTY ((desc ("%1.val_class"))) v;
3848 }
3849 dw_val_node;
3850
3851 /* Locations in memory are described using a sequence of stack machine
3852    operations.  */
3853
3854 typedef struct GTY(()) dw_loc_descr_struct {
3855   dw_loc_descr_ref dw_loc_next;
3856   enum dwarf_location_atom dw_loc_opc;
3857   int dw_loc_addr;
3858   dw_val_node dw_loc_oprnd1;
3859   dw_val_node dw_loc_oprnd2;
3860 }
3861 dw_loc_descr_node;
3862
3863 /* Location lists are ranges + location descriptions for that range,
3864    so you can track variables that are in different places over
3865    their entire life.  */
3866 typedef struct GTY(()) dw_loc_list_struct {
3867   dw_loc_list_ref dw_loc_next;
3868   const char *begin; /* Label for begin address of range */
3869   const char *end;  /* Label for end address of range */
3870   char *ll_symbol; /* Label for beginning of location list.
3871                       Only on head of list */
3872   const char *section; /* Section this loclist is relative to */
3873   dw_loc_descr_ref expr;
3874 } dw_loc_list_node;
3875
3876 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3877
3878 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3879
3880 /* Convert a DWARF stack opcode into its string name.  */
3881
3882 static const char *
3883 dwarf_stack_op_name (unsigned int op)
3884 {
3885   switch (op)
3886     {
3887     case DW_OP_addr:
3888     case INTERNAL_DW_OP_tls_addr:
3889       return "DW_OP_addr";
3890     case DW_OP_deref:
3891       return "DW_OP_deref";
3892     case DW_OP_const1u:
3893       return "DW_OP_const1u";
3894     case DW_OP_const1s:
3895       return "DW_OP_const1s";
3896     case DW_OP_const2u:
3897       return "DW_OP_const2u";
3898     case DW_OP_const2s:
3899       return "DW_OP_const2s";
3900     case DW_OP_const4u:
3901       return "DW_OP_const4u";
3902     case DW_OP_const4s:
3903       return "DW_OP_const4s";
3904     case DW_OP_const8u:
3905       return "DW_OP_const8u";
3906     case DW_OP_const8s:
3907       return "DW_OP_const8s";
3908     case DW_OP_constu:
3909       return "DW_OP_constu";
3910     case DW_OP_consts:
3911       return "DW_OP_consts";
3912     case DW_OP_dup:
3913       return "DW_OP_dup";
3914     case DW_OP_drop:
3915       return "DW_OP_drop";
3916     case DW_OP_over:
3917       return "DW_OP_over";
3918     case DW_OP_pick:
3919       return "DW_OP_pick";
3920     case DW_OP_swap:
3921       return "DW_OP_swap";
3922     case DW_OP_rot:
3923       return "DW_OP_rot";
3924     case DW_OP_xderef:
3925       return "DW_OP_xderef";
3926     case DW_OP_abs:
3927       return "DW_OP_abs";
3928     case DW_OP_and:
3929       return "DW_OP_and";
3930     case DW_OP_div:
3931       return "DW_OP_div";
3932     case DW_OP_minus:
3933       return "DW_OP_minus";
3934     case DW_OP_mod:
3935       return "DW_OP_mod";
3936     case DW_OP_mul:
3937       return "DW_OP_mul";
3938     case DW_OP_neg:
3939       return "DW_OP_neg";
3940     case DW_OP_not:
3941       return "DW_OP_not";
3942     case DW_OP_or:
3943       return "DW_OP_or";
3944     case DW_OP_plus:
3945       return "DW_OP_plus";
3946     case DW_OP_plus_uconst:
3947       return "DW_OP_plus_uconst";
3948     case DW_OP_shl:
3949       return "DW_OP_shl";
3950     case DW_OP_shr:
3951       return "DW_OP_shr";
3952     case DW_OP_shra:
3953       return "DW_OP_shra";
3954     case DW_OP_xor:
3955       return "DW_OP_xor";
3956     case DW_OP_bra:
3957       return "DW_OP_bra";
3958     case DW_OP_eq:
3959       return "DW_OP_eq";
3960     case DW_OP_ge:
3961       return "DW_OP_ge";
3962     case DW_OP_gt:
3963       return "DW_OP_gt";
3964     case DW_OP_le:
3965       return "DW_OP_le";
3966     case DW_OP_lt:
3967       return "DW_OP_lt";
3968     case DW_OP_ne:
3969       return "DW_OP_ne";
3970     case DW_OP_skip:
3971       return "DW_OP_skip";
3972     case DW_OP_lit0:
3973       return "DW_OP_lit0";
3974     case DW_OP_lit1:
3975       return "DW_OP_lit1";
3976     case DW_OP_lit2:
3977       return "DW_OP_lit2";
3978     case DW_OP_lit3:
3979       return "DW_OP_lit3";
3980     case DW_OP_lit4:
3981       return "DW_OP_lit4";
3982     case DW_OP_lit5:
3983       return "DW_OP_lit5";
3984     case DW_OP_lit6:
3985       return "DW_OP_lit6";
3986     case DW_OP_lit7:
3987       return "DW_OP_lit7";
3988     case DW_OP_lit8:
3989       return "DW_OP_lit8";
3990     case DW_OP_lit9:
3991       return "DW_OP_lit9";
3992     case DW_OP_lit10:
3993       return "DW_OP_lit10";
3994     case DW_OP_lit11:
3995       return "DW_OP_lit11";
3996     case DW_OP_lit12:
3997       return "DW_OP_lit12";
3998     case DW_OP_lit13:
3999       return "DW_OP_lit13";
4000     case DW_OP_lit14:
4001       return "DW_OP_lit14";
4002     case DW_OP_lit15:
4003       return "DW_OP_lit15";
4004     case DW_OP_lit16:
4005       return "DW_OP_lit16";
4006     case DW_OP_lit17:
4007       return "DW_OP_lit17";
4008     case DW_OP_lit18:
4009       return "DW_OP_lit18";
4010     case DW_OP_lit19:
4011       return "DW_OP_lit19";
4012     case DW_OP_lit20:
4013       return "DW_OP_lit20";
4014     case DW_OP_lit21:
4015       return "DW_OP_lit21";
4016     case DW_OP_lit22:
4017       return "DW_OP_lit22";
4018     case DW_OP_lit23:
4019       return "DW_OP_lit23";
4020     case DW_OP_lit24:
4021       return "DW_OP_lit24";
4022     case DW_OP_lit25:
4023       return "DW_OP_lit25";
4024     case DW_OP_lit26:
4025       return "DW_OP_lit26";
4026     case DW_OP_lit27:
4027       return "DW_OP_lit27";
4028     case DW_OP_lit28:
4029       return "DW_OP_lit28";
4030     case DW_OP_lit29:
4031       return "DW_OP_lit29";
4032     case DW_OP_lit30:
4033       return "DW_OP_lit30";
4034     case DW_OP_lit31:
4035       return "DW_OP_lit31";
4036     case DW_OP_reg0:
4037       return "DW_OP_reg0";
4038     case DW_OP_reg1:
4039       return "DW_OP_reg1";
4040     case DW_OP_reg2:
4041       return "DW_OP_reg2";
4042     case DW_OP_reg3:
4043       return "DW_OP_reg3";
4044     case DW_OP_reg4:
4045       return "DW_OP_reg4";
4046     case DW_OP_reg5:
4047       return "DW_OP_reg5";
4048     case DW_OP_reg6:
4049       return "DW_OP_reg6";
4050     case DW_OP_reg7:
4051       return "DW_OP_reg7";
4052     case DW_OP_reg8:
4053       return "DW_OP_reg8";
4054     case DW_OP_reg9:
4055       return "DW_OP_reg9";
4056     case DW_OP_reg10:
4057       return "DW_OP_reg10";
4058     case DW_OP_reg11:
4059       return "DW_OP_reg11";
4060     case DW_OP_reg12:
4061       return "DW_OP_reg12";
4062     case DW_OP_reg13:
4063       return "DW_OP_reg13";
4064     case DW_OP_reg14:
4065       return "DW_OP_reg14";
4066     case DW_OP_reg15:
4067       return "DW_OP_reg15";
4068     case DW_OP_reg16:
4069       return "DW_OP_reg16";
4070     case DW_OP_reg17:
4071       return "DW_OP_reg17";
4072     case DW_OP_reg18:
4073       return "DW_OP_reg18";
4074     case DW_OP_reg19:
4075       return "DW_OP_reg19";
4076     case DW_OP_reg20:
4077       return "DW_OP_reg20";
4078     case DW_OP_reg21:
4079       return "DW_OP_reg21";
4080     case DW_OP_reg22:
4081       return "DW_OP_reg22";
4082     case DW_OP_reg23:
4083       return "DW_OP_reg23";
4084     case DW_OP_reg24:
4085       return "DW_OP_reg24";
4086     case DW_OP_reg25:
4087       return "DW_OP_reg25";
4088     case DW_OP_reg26:
4089       return "DW_OP_reg26";
4090     case DW_OP_reg27:
4091       return "DW_OP_reg27";
4092     case DW_OP_reg28:
4093       return "DW_OP_reg28";
4094     case DW_OP_reg29:
4095       return "DW_OP_reg29";
4096     case DW_OP_reg30:
4097       return "DW_OP_reg30";
4098     case DW_OP_reg31:
4099       return "DW_OP_reg31";
4100     case DW_OP_breg0:
4101       return "DW_OP_breg0";
4102     case DW_OP_breg1:
4103       return "DW_OP_breg1";
4104     case DW_OP_breg2:
4105       return "DW_OP_breg2";
4106     case DW_OP_breg3:
4107       return "DW_OP_breg3";
4108     case DW_OP_breg4:
4109       return "DW_OP_breg4";
4110     case DW_OP_breg5:
4111       return "DW_OP_breg5";
4112     case DW_OP_breg6:
4113       return "DW_OP_breg6";
4114     case DW_OP_breg7:
4115       return "DW_OP_breg7";
4116     case DW_OP_breg8:
4117       return "DW_OP_breg8";
4118     case DW_OP_breg9:
4119       return "DW_OP_breg9";
4120     case DW_OP_breg10:
4121       return "DW_OP_breg10";
4122     case DW_OP_breg11:
4123       return "DW_OP_breg11";
4124     case DW_OP_breg12:
4125       return "DW_OP_breg12";
4126     case DW_OP_breg13:
4127       return "DW_OP_breg13";
4128     case DW_OP_breg14:
4129       return "DW_OP_breg14";
4130     case DW_OP_breg15:
4131       return "DW_OP_breg15";
4132     case DW_OP_breg16:
4133       return "DW_OP_breg16";
4134     case DW_OP_breg17:
4135       return "DW_OP_breg17";
4136     case DW_OP_breg18:
4137       return "DW_OP_breg18";
4138     case DW_OP_breg19:
4139       return "DW_OP_breg19";
4140     case DW_OP_breg20:
4141       return "DW_OP_breg20";
4142     case DW_OP_breg21:
4143       return "DW_OP_breg21";
4144     case DW_OP_breg22:
4145       return "DW_OP_breg22";
4146     case DW_OP_breg23:
4147       return "DW_OP_breg23";
4148     case DW_OP_breg24:
4149       return "DW_OP_breg24";
4150     case DW_OP_breg25:
4151       return "DW_OP_breg25";
4152     case DW_OP_breg26:
4153       return "DW_OP_breg26";
4154     case DW_OP_breg27:
4155       return "DW_OP_breg27";
4156     case DW_OP_breg28:
4157       return "DW_OP_breg28";
4158     case DW_OP_breg29:
4159       return "DW_OP_breg29";
4160     case DW_OP_breg30:
4161       return "DW_OP_breg30";
4162     case DW_OP_breg31:
4163       return "DW_OP_breg31";
4164     case DW_OP_regx:
4165       return "DW_OP_regx";
4166     case DW_OP_fbreg:
4167       return "DW_OP_fbreg";
4168     case DW_OP_bregx:
4169       return "DW_OP_bregx";
4170     case DW_OP_piece:
4171       return "DW_OP_piece";
4172     case DW_OP_deref_size:
4173       return "DW_OP_deref_size";
4174     case DW_OP_xderef_size:
4175       return "DW_OP_xderef_size";
4176     case DW_OP_nop:
4177       return "DW_OP_nop";
4178     case DW_OP_push_object_address:
4179       return "DW_OP_push_object_address";
4180     case DW_OP_call2:
4181       return "DW_OP_call2";
4182     case DW_OP_call4:
4183       return "DW_OP_call4";
4184     case DW_OP_call_ref:
4185       return "DW_OP_call_ref";
4186     case DW_OP_GNU_push_tls_address:
4187       return "DW_OP_GNU_push_tls_address";
4188     case DW_OP_GNU_uninit:
4189       return "DW_OP_GNU_uninit";
4190     default:
4191       return "OP_<unknown>";
4192     }
4193 }
4194
4195 /* Return a pointer to a newly allocated location description.  Location
4196    descriptions are simple expression terms that can be strung
4197    together to form more complicated location (address) descriptions.  */
4198
4199 static inline dw_loc_descr_ref
4200 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4201                unsigned HOST_WIDE_INT oprnd2)
4202 {
4203   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4204
4205   descr->dw_loc_opc = op;
4206   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4207   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4208   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4209   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4210
4211   return descr;
4212 }
4213
4214 /* Return a pointer to a newly allocated location description for
4215    REG and OFFSET.  */
4216
4217 static inline dw_loc_descr_ref
4218 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4219 {
4220   if (reg <= 31)
4221     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4222                           offset, 0);
4223   else
4224     return new_loc_descr (DW_OP_bregx, reg, offset);
4225 }
4226
4227 /* Add a location description term to a location description expression.  */
4228
4229 static inline void
4230 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4231 {
4232   dw_loc_descr_ref *d;
4233
4234   /* Find the end of the chain.  */
4235   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4236     ;
4237
4238   *d = descr;
4239 }
4240
4241 /* Add a constant OFFSET to a location expression.  */
4242
4243 static void
4244 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4245 {
4246   dw_loc_descr_ref loc;
4247   HOST_WIDE_INT *p;
4248
4249   gcc_assert (*list_head != NULL);
4250
4251   if (!offset)
4252     return;
4253
4254   /* Find the end of the chain.  */
4255   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4256     ;
4257
4258   p = NULL;
4259   if (loc->dw_loc_opc == DW_OP_fbreg
4260       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4261     p = &loc->dw_loc_oprnd1.v.val_int;
4262   else if (loc->dw_loc_opc == DW_OP_bregx)
4263     p = &loc->dw_loc_oprnd2.v.val_int;
4264
4265   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4266      offset.  Don't optimize if an signed integer overflow would happen.  */
4267   if (p != NULL
4268       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4269           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4270     *p += offset;
4271
4272   else if (offset > 0)
4273     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4274
4275   else
4276     {
4277       loc->dw_loc_next = int_loc_descriptor (offset);
4278       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4279     }
4280 }
4281
4282 /* Return the size of a location descriptor.  */
4283
4284 static unsigned long
4285 size_of_loc_descr (dw_loc_descr_ref loc)
4286 {
4287   unsigned long size = 1;
4288
4289   switch (loc->dw_loc_opc)
4290     {
4291     case DW_OP_addr:
4292     case INTERNAL_DW_OP_tls_addr:
4293       size += DWARF2_ADDR_SIZE;
4294       break;
4295     case DW_OP_const1u:
4296     case DW_OP_const1s:
4297       size += 1;
4298       break;
4299     case DW_OP_const2u:
4300     case DW_OP_const2s:
4301       size += 2;
4302       break;
4303     case DW_OP_const4u:
4304     case DW_OP_const4s:
4305       size += 4;
4306       break;
4307     case DW_OP_const8u:
4308     case DW_OP_const8s:
4309       size += 8;
4310       break;
4311     case DW_OP_constu:
4312       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4313       break;
4314     case DW_OP_consts:
4315       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4316       break;
4317     case DW_OP_pick:
4318       size += 1;
4319       break;
4320     case DW_OP_plus_uconst:
4321       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4322       break;
4323     case DW_OP_skip:
4324     case DW_OP_bra:
4325       size += 2;
4326       break;
4327     case DW_OP_breg0:
4328     case DW_OP_breg1:
4329     case DW_OP_breg2:
4330     case DW_OP_breg3:
4331     case DW_OP_breg4:
4332     case DW_OP_breg5:
4333     case DW_OP_breg6:
4334     case DW_OP_breg7:
4335     case DW_OP_breg8:
4336     case DW_OP_breg9:
4337     case DW_OP_breg10:
4338     case DW_OP_breg11:
4339     case DW_OP_breg12:
4340     case DW_OP_breg13:
4341     case DW_OP_breg14:
4342     case DW_OP_breg15:
4343     case DW_OP_breg16:
4344     case DW_OP_breg17:
4345     case DW_OP_breg18:
4346     case DW_OP_breg19:
4347     case DW_OP_breg20:
4348     case DW_OP_breg21:
4349     case DW_OP_breg22:
4350     case DW_OP_breg23:
4351     case DW_OP_breg24:
4352     case DW_OP_breg25:
4353     case DW_OP_breg26:
4354     case DW_OP_breg27:
4355     case DW_OP_breg28:
4356     case DW_OP_breg29:
4357     case DW_OP_breg30:
4358     case DW_OP_breg31:
4359       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4360       break;
4361     case DW_OP_regx:
4362       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4363       break;
4364     case DW_OP_fbreg:
4365       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4366       break;
4367     case DW_OP_bregx:
4368       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4369       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4370       break;
4371     case DW_OP_piece:
4372       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4373       break;
4374     case DW_OP_deref_size:
4375     case DW_OP_xderef_size:
4376       size += 1;
4377       break;
4378     case DW_OP_call2:
4379       size += 2;
4380       break;
4381     case DW_OP_call4:
4382       size += 4;
4383       break;
4384     case DW_OP_call_ref:
4385       size += DWARF2_ADDR_SIZE;
4386       break;
4387     default:
4388       break;
4389     }
4390
4391   return size;
4392 }
4393
4394 /* Return the size of a series of location descriptors.  */
4395
4396 static unsigned long
4397 size_of_locs (dw_loc_descr_ref loc)
4398 {
4399   dw_loc_descr_ref l;
4400   unsigned long size;
4401
4402   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4403      field, to avoid writing to a PCH file.  */
4404   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4405     {
4406       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4407         break;
4408       size += size_of_loc_descr (l);
4409     }
4410   if (! l)
4411     return size;
4412
4413   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4414     {
4415       l->dw_loc_addr = size;
4416       size += size_of_loc_descr (l);
4417     }
4418
4419   return size;
4420 }
4421
4422 /* Output location description stack opcode's operands (if any).  */
4423
4424 static void
4425 output_loc_operands (dw_loc_descr_ref loc)
4426 {
4427   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4428   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4429
4430   switch (loc->dw_loc_opc)
4431     {
4432 #ifdef DWARF2_DEBUGGING_INFO
4433     case DW_OP_addr:
4434       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4435       break;
4436     case DW_OP_const2u:
4437     case DW_OP_const2s:
4438       dw2_asm_output_data (2, val1->v.val_int, NULL);
4439       break;
4440     case DW_OP_const4u:
4441     case DW_OP_const4s:
4442       dw2_asm_output_data (4, val1->v.val_int, NULL);
4443       break;
4444     case DW_OP_const8u:
4445     case DW_OP_const8s:
4446       gcc_assert (HOST_BITS_PER_LONG >= 64);
4447       dw2_asm_output_data (8, val1->v.val_int, NULL);
4448       break;
4449     case DW_OP_skip:
4450     case DW_OP_bra:
4451       {
4452         int offset;
4453
4454         gcc_assert (val1->val_class == dw_val_class_loc);
4455         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4456
4457         dw2_asm_output_data (2, offset, NULL);
4458       }
4459       break;
4460 #else
4461     case DW_OP_addr:
4462     case DW_OP_const2u:
4463     case DW_OP_const2s:
4464     case DW_OP_const4u:
4465     case DW_OP_const4s:
4466     case DW_OP_const8u:
4467     case DW_OP_const8s:
4468     case DW_OP_skip:
4469     case DW_OP_bra:
4470       /* We currently don't make any attempt to make sure these are
4471          aligned properly like we do for the main unwind info, so
4472          don't support emitting things larger than a byte if we're
4473          only doing unwinding.  */
4474       gcc_unreachable ();
4475 #endif
4476     case DW_OP_const1u:
4477     case DW_OP_const1s:
4478       dw2_asm_output_data (1, val1->v.val_int, NULL);
4479       break;
4480     case DW_OP_constu:
4481       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4482       break;
4483     case DW_OP_consts:
4484       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4485       break;
4486     case DW_OP_pick:
4487       dw2_asm_output_data (1, val1->v.val_int, NULL);
4488       break;
4489     case DW_OP_plus_uconst:
4490       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4491       break;
4492     case DW_OP_breg0:
4493     case DW_OP_breg1:
4494     case DW_OP_breg2:
4495     case DW_OP_breg3:
4496     case DW_OP_breg4:
4497     case DW_OP_breg5:
4498     case DW_OP_breg6:
4499     case DW_OP_breg7:
4500     case DW_OP_breg8:
4501     case DW_OP_breg9:
4502     case DW_OP_breg10:
4503     case DW_OP_breg11:
4504     case DW_OP_breg12:
4505     case DW_OP_breg13:
4506     case DW_OP_breg14:
4507     case DW_OP_breg15:
4508     case DW_OP_breg16:
4509     case DW_OP_breg17:
4510     case DW_OP_breg18:
4511     case DW_OP_breg19:
4512     case DW_OP_breg20:
4513     case DW_OP_breg21:
4514     case DW_OP_breg22:
4515     case DW_OP_breg23:
4516     case DW_OP_breg24:
4517     case DW_OP_breg25:
4518     case DW_OP_breg26:
4519     case DW_OP_breg27:
4520     case DW_OP_breg28:
4521     case DW_OP_breg29:
4522     case DW_OP_breg30:
4523     case DW_OP_breg31:
4524       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4525       break;
4526     case DW_OP_regx:
4527       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4528       break;
4529     case DW_OP_fbreg:
4530       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4531       break;
4532     case DW_OP_bregx:
4533       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4534       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4535       break;
4536     case DW_OP_piece:
4537       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4538       break;
4539     case DW_OP_deref_size:
4540     case DW_OP_xderef_size:
4541       dw2_asm_output_data (1, val1->v.val_int, NULL);
4542       break;
4543
4544     case INTERNAL_DW_OP_tls_addr:
4545       if (targetm.asm_out.output_dwarf_dtprel)
4546         {
4547           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4548                                                DWARF2_ADDR_SIZE,
4549                                                val1->v.val_addr);
4550           fputc ('\n', asm_out_file);
4551         }
4552       else
4553         gcc_unreachable ();
4554       break;
4555
4556     default:
4557       /* Other codes have no operands.  */
4558       break;
4559     }
4560 }
4561
4562 /* Output a sequence of location operations.  */
4563
4564 static void
4565 output_loc_sequence (dw_loc_descr_ref loc)
4566 {
4567   for (; loc != NULL; loc = loc->dw_loc_next)
4568     {
4569       /* Output the opcode.  */
4570       dw2_asm_output_data (1, loc->dw_loc_opc,
4571                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4572
4573       /* Output the operand(s) (if any).  */
4574       output_loc_operands (loc);
4575     }
4576 }
4577
4578 /* Output location description stack opcode's operands (if any).
4579    The output is single bytes on a line, suitable for .cfi_escape.  */
4580
4581 static void
4582 output_loc_operands_raw (dw_loc_descr_ref loc)
4583 {
4584   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4585   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4586
4587   switch (loc->dw_loc_opc)
4588     {
4589     case DW_OP_addr:
4590       /* We cannot output addresses in .cfi_escape, only bytes.  */
4591       gcc_unreachable ();
4592
4593     case DW_OP_const1u:
4594     case DW_OP_const1s:
4595     case DW_OP_pick:
4596     case DW_OP_deref_size:
4597     case DW_OP_xderef_size:
4598       fputc (',', asm_out_file);
4599       dw2_asm_output_data_raw (1, val1->v.val_int);
4600       break;
4601
4602     case DW_OP_const2u:
4603     case DW_OP_const2s:
4604       fputc (',', asm_out_file);
4605       dw2_asm_output_data_raw (2, val1->v.val_int);
4606       break;
4607
4608     case DW_OP_const4u:
4609     case DW_OP_const4s:
4610       fputc (',', asm_out_file);
4611       dw2_asm_output_data_raw (4, val1->v.val_int);
4612       break;
4613
4614     case DW_OP_const8u:
4615     case DW_OP_const8s:
4616       gcc_assert (HOST_BITS_PER_LONG >= 64);
4617       fputc (',', asm_out_file);
4618       dw2_asm_output_data_raw (8, val1->v.val_int);
4619       break;
4620
4621     case DW_OP_skip:
4622     case DW_OP_bra:
4623       {
4624         int offset;
4625
4626         gcc_assert (val1->val_class == dw_val_class_loc);
4627         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4628
4629         fputc (',', asm_out_file);
4630         dw2_asm_output_data_raw (2, offset);
4631       }
4632       break;
4633
4634     case DW_OP_constu:
4635     case DW_OP_plus_uconst:
4636     case DW_OP_regx:
4637     case DW_OP_piece:
4638       fputc (',', asm_out_file);
4639       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4640       break;
4641
4642     case DW_OP_consts:
4643     case DW_OP_breg0:
4644     case DW_OP_breg1:
4645     case DW_OP_breg2:
4646     case DW_OP_breg3:
4647     case DW_OP_breg4:
4648     case DW_OP_breg5:
4649     case DW_OP_breg6:
4650     case DW_OP_breg7:
4651     case DW_OP_breg8:
4652     case DW_OP_breg9:
4653     case DW_OP_breg10:
4654     case DW_OP_breg11:
4655     case DW_OP_breg12:
4656     case DW_OP_breg13:
4657     case DW_OP_breg14:
4658     case DW_OP_breg15:
4659     case DW_OP_breg16:
4660     case DW_OP_breg17:
4661     case DW_OP_breg18:
4662     case DW_OP_breg19:
4663     case DW_OP_breg20:
4664     case DW_OP_breg21:
4665     case DW_OP_breg22:
4666     case DW_OP_breg23:
4667     case DW_OP_breg24:
4668     case DW_OP_breg25:
4669     case DW_OP_breg26:
4670     case DW_OP_breg27:
4671     case DW_OP_breg28:
4672     case DW_OP_breg29:
4673     case DW_OP_breg30:
4674     case DW_OP_breg31:
4675     case DW_OP_fbreg:
4676       fputc (',', asm_out_file);
4677       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4678       break;
4679
4680     case DW_OP_bregx:
4681       fputc (',', asm_out_file);
4682       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4683       fputc (',', asm_out_file);
4684       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4685       break;
4686
4687     case INTERNAL_DW_OP_tls_addr:
4688       gcc_unreachable ();
4689
4690     default:
4691       /* Other codes have no operands.  */
4692       break;
4693     }
4694 }
4695
4696 static void
4697 output_loc_sequence_raw (dw_loc_descr_ref loc)
4698 {
4699   while (1)
4700     {
4701       /* Output the opcode.  */
4702       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4703       output_loc_operands_raw (loc);
4704
4705       if (!loc->dw_loc_next)
4706         break;
4707       loc = loc->dw_loc_next;
4708
4709       fputc (',', asm_out_file);
4710     }
4711 }
4712
4713 /* This routine will generate the correct assembly data for a location
4714    description based on a cfi entry with a complex address.  */
4715
4716 static void
4717 output_cfa_loc (dw_cfi_ref cfi)
4718 {
4719   dw_loc_descr_ref loc;
4720   unsigned long size;
4721
4722   if (cfi->dw_cfi_opc == DW_CFA_expression)
4723     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4724
4725   /* Output the size of the block.  */
4726   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4727   size = size_of_locs (loc);
4728   dw2_asm_output_data_uleb128 (size, NULL);
4729
4730   /* Now output the operations themselves.  */
4731   output_loc_sequence (loc);
4732 }
4733
4734 /* Similar, but used for .cfi_escape.  */
4735
4736 static void
4737 output_cfa_loc_raw (dw_cfi_ref cfi)
4738 {
4739   dw_loc_descr_ref loc;
4740   unsigned long size;
4741
4742   if (cfi->dw_cfi_opc == DW_CFA_expression)
4743     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4744
4745   /* Output the size of the block.  */
4746   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4747   size = size_of_locs (loc);
4748   dw2_asm_output_data_uleb128_raw (size);
4749   fputc (',', asm_out_file);
4750
4751   /* Now output the operations themselves.  */
4752   output_loc_sequence_raw (loc);
4753 }
4754
4755 /* This function builds a dwarf location descriptor sequence from a
4756    dw_cfa_location, adding the given OFFSET to the result of the
4757    expression.  */
4758
4759 static struct dw_loc_descr_struct *
4760 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4761 {
4762   struct dw_loc_descr_struct *head, *tmp;
4763
4764   offset += cfa->offset;
4765
4766   if (cfa->indirect)
4767     {
4768       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4769       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4770       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4771       add_loc_descr (&head, tmp);
4772       if (offset != 0)
4773         {
4774           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4775           add_loc_descr (&head, tmp);
4776         }
4777     }
4778   else
4779     head = new_reg_loc_descr (cfa->reg, offset);
4780
4781   return head;
4782 }
4783
4784 /* This function builds a dwarf location descriptor sequence for
4785    the address at OFFSET from the CFA when stack is aligned to
4786    ALIGNMENT byte.  */
4787
4788 static struct dw_loc_descr_struct *
4789 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4790 {
4791   struct dw_loc_descr_struct *head;
4792   unsigned int dwarf_fp
4793     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4794
4795  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4796   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4797     {
4798       head = new_reg_loc_descr (dwarf_fp, 0);
4799       add_loc_descr (&head, int_loc_descriptor (alignment));
4800       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4801       loc_descr_plus_const (&head, offset);
4802     }
4803   else
4804     head = new_reg_loc_descr (dwarf_fp, offset);
4805   return head;
4806 }
4807
4808 /* This function fills in aa dw_cfa_location structure from a dwarf location
4809    descriptor sequence.  */
4810
4811 static void
4812 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4813 {
4814   struct dw_loc_descr_struct *ptr;
4815   cfa->offset = 0;
4816   cfa->base_offset = 0;
4817   cfa->indirect = 0;
4818   cfa->reg = -1;
4819
4820   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4821     {
4822       enum dwarf_location_atom op = ptr->dw_loc_opc;
4823
4824       switch (op)
4825         {
4826         case DW_OP_reg0:
4827         case DW_OP_reg1:
4828         case DW_OP_reg2:
4829         case DW_OP_reg3:
4830         case DW_OP_reg4:
4831         case DW_OP_reg5:
4832         case DW_OP_reg6:
4833         case DW_OP_reg7:
4834         case DW_OP_reg8:
4835         case DW_OP_reg9:
4836         case DW_OP_reg10:
4837         case DW_OP_reg11:
4838         case DW_OP_reg12:
4839         case DW_OP_reg13:
4840         case DW_OP_reg14:
4841         case DW_OP_reg15:
4842         case DW_OP_reg16:
4843         case DW_OP_reg17:
4844         case DW_OP_reg18:
4845         case DW_OP_reg19:
4846         case DW_OP_reg20:
4847         case DW_OP_reg21:
4848         case DW_OP_reg22:
4849         case DW_OP_reg23:
4850         case DW_OP_reg24:
4851         case DW_OP_reg25:
4852         case DW_OP_reg26:
4853         case DW_OP_reg27:
4854         case DW_OP_reg28:
4855         case DW_OP_reg29:
4856         case DW_OP_reg30:
4857         case DW_OP_reg31:
4858           cfa->reg = op - DW_OP_reg0;
4859           break;
4860         case DW_OP_regx:
4861           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4862           break;
4863         case DW_OP_breg0:
4864         case DW_OP_breg1:
4865         case DW_OP_breg2:
4866         case DW_OP_breg3:
4867         case DW_OP_breg4:
4868         case DW_OP_breg5:
4869         case DW_OP_breg6:
4870         case DW_OP_breg7:
4871         case DW_OP_breg8:
4872         case DW_OP_breg9:
4873         case DW_OP_breg10:
4874         case DW_OP_breg11:
4875         case DW_OP_breg12:
4876         case DW_OP_breg13:
4877         case DW_OP_breg14:
4878         case DW_OP_breg15:
4879         case DW_OP_breg16:
4880         case DW_OP_breg17:
4881         case DW_OP_breg18:
4882         case DW_OP_breg19:
4883         case DW_OP_breg20:
4884         case DW_OP_breg21:
4885         case DW_OP_breg22:
4886         case DW_OP_breg23:
4887         case DW_OP_breg24:
4888         case DW_OP_breg25:
4889         case DW_OP_breg26:
4890         case DW_OP_breg27:
4891         case DW_OP_breg28:
4892         case DW_OP_breg29:
4893         case DW_OP_breg30:
4894         case DW_OP_breg31:
4895           cfa->reg = op - DW_OP_breg0;
4896           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4897           break;
4898         case DW_OP_bregx:
4899           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4900           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4901           break;
4902         case DW_OP_deref:
4903           cfa->indirect = 1;
4904           break;
4905         case DW_OP_plus_uconst:
4906           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4907           break;
4908         default:
4909           internal_error ("DW_LOC_OP %s not implemented",
4910                           dwarf_stack_op_name (ptr->dw_loc_opc));
4911         }
4912     }
4913 }
4914 #endif /* .debug_frame support */
4915 \f
4916 /* And now, the support for symbolic debugging information.  */
4917 #ifdef DWARF2_DEBUGGING_INFO
4918
4919 /* .debug_str support.  */
4920 static int output_indirect_string (void **, void *);
4921
4922 static void dwarf2out_init (const char *);
4923 static void dwarf2out_finish (const char *);
4924 static void dwarf2out_define (unsigned int, const char *);
4925 static void dwarf2out_undef (unsigned int, const char *);
4926 static void dwarf2out_start_source_file (unsigned, const char *);
4927 static void dwarf2out_end_source_file (unsigned);
4928 static void dwarf2out_begin_block (unsigned, unsigned);
4929 static void dwarf2out_end_block (unsigned, unsigned);
4930 static bool dwarf2out_ignore_block (const_tree);
4931 static void dwarf2out_global_decl (tree);
4932 static void dwarf2out_type_decl (tree, int);
4933 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4934 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4935                                                  dw_die_ref);
4936 static void dwarf2out_abstract_function (tree);
4937 static void dwarf2out_var_location (rtx);
4938 static void dwarf2out_begin_function (tree);
4939 static void dwarf2out_set_name (tree, tree);
4940
4941 /* The debug hooks structure.  */
4942
4943 const struct gcc_debug_hooks dwarf2_debug_hooks =
4944 {
4945   dwarf2out_init,
4946   dwarf2out_finish,
4947   dwarf2out_define,
4948   dwarf2out_undef,
4949   dwarf2out_start_source_file,
4950   dwarf2out_end_source_file,
4951   dwarf2out_begin_block,
4952   dwarf2out_end_block,
4953   dwarf2out_ignore_block,
4954   dwarf2out_source_line,
4955   dwarf2out_begin_prologue,
4956   debug_nothing_int_charstar,   /* end_prologue */
4957   dwarf2out_end_epilogue,
4958   dwarf2out_begin_function,
4959   debug_nothing_int,            /* end_function */
4960   dwarf2out_decl,               /* function_decl */
4961   dwarf2out_global_decl,
4962   dwarf2out_type_decl,          /* type_decl */
4963   dwarf2out_imported_module_or_decl,
4964   debug_nothing_tree,           /* deferred_inline_function */
4965   /* The DWARF 2 backend tries to reduce debugging bloat by not
4966      emitting the abstract description of inline functions until
4967      something tries to reference them.  */
4968   dwarf2out_abstract_function,  /* outlining_inline_function */
4969   debug_nothing_rtx,            /* label */
4970   debug_nothing_int,            /* handle_pch */
4971   dwarf2out_var_location,
4972   dwarf2out_switch_text_section,
4973   dwarf2out_set_name,
4974   1                             /* start_end_main_source_file */
4975 };
4976 #endif
4977 \f
4978 /* NOTE: In the comments in this file, many references are made to
4979    "Debugging Information Entries".  This term is abbreviated as `DIE'
4980    throughout the remainder of this file.  */
4981
4982 /* An internal representation of the DWARF output is built, and then
4983    walked to generate the DWARF debugging info.  The walk of the internal
4984    representation is done after the entire program has been compiled.
4985    The types below are used to describe the internal representation.  */
4986
4987 /* Various DIE's use offsets relative to the beginning of the
4988    .debug_info section to refer to each other.  */
4989
4990 typedef long int dw_offset;
4991
4992 /* Define typedefs here to avoid circular dependencies.  */
4993
4994 typedef struct dw_attr_struct *dw_attr_ref;
4995 typedef struct dw_line_info_struct *dw_line_info_ref;
4996 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4997 typedef struct pubname_struct *pubname_ref;
4998 typedef struct dw_ranges_struct *dw_ranges_ref;
4999 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5000
5001 /* Each entry in the line_info_table maintains the file and
5002    line number associated with the label generated for that
5003    entry.  The label gives the PC value associated with
5004    the line number entry.  */
5005
5006 typedef struct GTY(()) dw_line_info_struct {
5007   unsigned long dw_file_num;
5008   unsigned long dw_line_num;
5009 }
5010 dw_line_info_entry;
5011
5012 /* Line information for functions in separate sections; each one gets its
5013    own sequence.  */
5014 typedef struct GTY(()) dw_separate_line_info_struct {
5015   unsigned long dw_file_num;
5016   unsigned long dw_line_num;
5017   unsigned long function;
5018 }
5019 dw_separate_line_info_entry;
5020
5021 /* Each DIE attribute has a field specifying the attribute kind,
5022    a link to the next attribute in the chain, and an attribute value.
5023    Attributes are typically linked below the DIE they modify.  */
5024
5025 typedef struct GTY(()) dw_attr_struct {
5026   enum dwarf_attribute dw_attr;
5027   dw_val_node dw_attr_val;
5028 }
5029 dw_attr_node;
5030
5031 DEF_VEC_O(dw_attr_node);
5032 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5033
5034 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5035    The children of each node form a circular list linked by
5036    die_sib.  die_child points to the node *before* the "first" child node.  */
5037
5038 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5039   enum dwarf_tag die_tag;
5040   char *die_symbol;
5041   VEC(dw_attr_node,gc) * die_attr;
5042   dw_die_ref die_parent;
5043   dw_die_ref die_child;
5044   dw_die_ref die_sib;
5045   dw_die_ref die_definition; /* ref from a specification to its definition */
5046   dw_offset die_offset;
5047   unsigned long die_abbrev;
5048   int die_mark;
5049   /* Die is used and must not be pruned as unused.  */
5050   int die_perennial_p;
5051   unsigned int decl_id;
5052 }
5053 die_node;
5054
5055 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5056 #define FOR_EACH_CHILD(die, c, expr) do {       \
5057   c = die->die_child;                           \
5058   if (c) do {                                   \
5059     c = c->die_sib;                             \
5060     expr;                                       \
5061   } while (c != die->die_child);                \
5062 } while (0)
5063
5064 /* The pubname structure */
5065
5066 typedef struct GTY(()) pubname_struct {
5067   dw_die_ref die;
5068   const char *name;
5069 }
5070 pubname_entry;
5071
5072 DEF_VEC_O(pubname_entry);
5073 DEF_VEC_ALLOC_O(pubname_entry, gc);
5074
5075 struct GTY(()) dw_ranges_struct {
5076   /* If this is positive, it's a block number, otherwise it's a
5077      bitwise-negated index into dw_ranges_by_label.  */
5078   int num;
5079 };
5080
5081 struct GTY(()) dw_ranges_by_label_struct {
5082   const char *begin;
5083   const char *end;
5084 };
5085
5086 /* The limbo die list structure.  */
5087 typedef struct GTY(()) limbo_die_struct {
5088   dw_die_ref die;
5089   tree created_for;
5090   struct limbo_die_struct *next;
5091 }
5092 limbo_die_node;
5093
5094 /* How to start an assembler comment.  */
5095 #ifndef ASM_COMMENT_START
5096 #define ASM_COMMENT_START ";#"
5097 #endif
5098
5099 /* Define a macro which returns nonzero for a TYPE_DECL which was
5100    implicitly generated for a tagged type.
5101
5102    Note that unlike the gcc front end (which generates a NULL named
5103    TYPE_DECL node for each complete tagged type, each array type, and
5104    each function type node created) the g++ front end generates a
5105    _named_ TYPE_DECL node for each tagged type node created.
5106    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5107    generate a DW_TAG_typedef DIE for them.  */
5108
5109 #define TYPE_DECL_IS_STUB(decl)                         \
5110   (DECL_NAME (decl) == NULL_TREE                        \
5111    || (DECL_ARTIFICIAL (decl)                           \
5112        && is_tagged_type (TREE_TYPE (decl))             \
5113        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5114            /* This is necessary for stub decls that     \
5115               appear in nested inline functions.  */    \
5116            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5117                && (decl_ultimate_origin (decl)          \
5118                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5119
5120 /* Information concerning the compilation unit's programming
5121    language, and compiler version.  */
5122
5123 /* Fixed size portion of the DWARF compilation unit header.  */
5124 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5125   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5126
5127 /* Fixed size portion of public names info.  */
5128 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5129
5130 /* Fixed size portion of the address range info.  */
5131 #define DWARF_ARANGES_HEADER_SIZE                                       \
5132   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5133                 DWARF2_ADDR_SIZE * 2)                                   \
5134    - DWARF_INITIAL_LENGTH_SIZE)
5135
5136 /* Size of padding portion in the address range info.  It must be
5137    aligned to twice the pointer size.  */
5138 #define DWARF_ARANGES_PAD_SIZE \
5139   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5140                 DWARF2_ADDR_SIZE * 2)                              \
5141    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5142
5143 /* Use assembler line directives if available.  */
5144 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5145 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5146 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5147 #else
5148 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5149 #endif
5150 #endif
5151
5152 /* Minimum line offset in a special line info. opcode.
5153    This value was chosen to give a reasonable range of values.  */
5154 #define DWARF_LINE_BASE  -10
5155
5156 /* First special line opcode - leave room for the standard opcodes.  */
5157 #define DWARF_LINE_OPCODE_BASE  10
5158
5159 /* Range of line offsets in a special line info. opcode.  */
5160 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5161
5162 /* Flag that indicates the initial value of the is_stmt_start flag.
5163    In the present implementation, we do not mark any lines as
5164    the beginning of a source statement, because that information
5165    is not made available by the GCC front-end.  */
5166 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5167
5168 #ifdef DWARF2_DEBUGGING_INFO
5169 /* This location is used by calc_die_sizes() to keep track
5170    the offset of each DIE within the .debug_info section.  */
5171 static unsigned long next_die_offset;
5172 #endif
5173
5174 /* Record the root of the DIE's built for the current compilation unit.  */
5175 static GTY(()) dw_die_ref comp_unit_die;
5176
5177 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5178 static GTY(()) limbo_die_node *limbo_die_list;
5179
5180 /* A list of DIEs for which we may have to generate
5181    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5182    set.  */
5183 static GTY(()) limbo_die_node *deferred_asm_name;
5184
5185 /* Filenames referenced by this compilation unit.  */
5186 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5187
5188 /* A hash table of references to DIE's that describe declarations.
5189    The key is a DECL_UID() which is a unique number identifying each decl.  */
5190 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5191
5192 /* A hash table of references to DIE's that describe COMMON blocks.
5193    The key is DECL_UID() ^ die_parent.  */
5194 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5195
5196 /* Node of the variable location list.  */
5197 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5198   rtx GTY (()) var_loc_note;
5199   const char * GTY (()) label;
5200   const char * GTY (()) section_label;
5201   struct var_loc_node * GTY (()) next;
5202 };
5203
5204 /* Variable location list.  */
5205 struct GTY (()) var_loc_list_def {
5206   struct var_loc_node * GTY (()) first;
5207
5208   /* Do not mark the last element of the chained list because
5209      it is marked through the chain.  */
5210   struct var_loc_node * GTY ((skip ("%h"))) last;
5211
5212   /* DECL_UID of the variable decl.  */
5213   unsigned int decl_id;
5214 };
5215 typedef struct var_loc_list_def var_loc_list;
5216
5217
5218 /* Table of decl location linked lists.  */
5219 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5220
5221 /* A pointer to the base of a list of references to DIE's that
5222    are uniquely identified by their tag, presence/absence of
5223    children DIE's, and list of attribute/value pairs.  */
5224 static GTY((length ("abbrev_die_table_allocated")))
5225   dw_die_ref *abbrev_die_table;
5226
5227 /* Number of elements currently allocated for abbrev_die_table.  */
5228 static GTY(()) unsigned abbrev_die_table_allocated;
5229
5230 /* Number of elements in type_die_table currently in use.  */
5231 static GTY(()) unsigned abbrev_die_table_in_use;
5232
5233 /* Size (in elements) of increments by which we may expand the
5234    abbrev_die_table.  */
5235 #define ABBREV_DIE_TABLE_INCREMENT 256
5236
5237 /* A pointer to the base of a table that contains line information
5238    for each source code line in .text in the compilation unit.  */
5239 static GTY((length ("line_info_table_allocated")))
5240      dw_line_info_ref line_info_table;
5241
5242 /* Number of elements currently allocated for line_info_table.  */
5243 static GTY(()) unsigned line_info_table_allocated;
5244
5245 /* Number of elements in line_info_table currently in use.  */
5246 static GTY(()) unsigned line_info_table_in_use;
5247
5248 /* A pointer to the base of a table that contains line information
5249    for each source code line outside of .text in the compilation unit.  */
5250 static GTY ((length ("separate_line_info_table_allocated")))
5251      dw_separate_line_info_ref separate_line_info_table;
5252
5253 /* Number of elements currently allocated for separate_line_info_table.  */
5254 static GTY(()) unsigned separate_line_info_table_allocated;
5255
5256 /* Number of elements in separate_line_info_table currently in use.  */
5257 static GTY(()) unsigned separate_line_info_table_in_use;
5258
5259 /* Size (in elements) of increments by which we may expand the
5260    line_info_table.  */
5261 #define LINE_INFO_TABLE_INCREMENT 1024
5262
5263 /* A pointer to the base of a table that contains a list of publicly
5264    accessible names.  */
5265 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5266
5267 /* A pointer to the base of a table that contains a list of publicly
5268    accessible types.  */
5269 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5270
5271 /* Array of dies for which we should generate .debug_arange info.  */
5272 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5273
5274 /* Number of elements currently allocated for arange_table.  */
5275 static GTY(()) unsigned arange_table_allocated;
5276
5277 /* Number of elements in arange_table currently in use.  */
5278 static GTY(()) unsigned arange_table_in_use;
5279
5280 /* Size (in elements) of increments by which we may expand the
5281    arange_table.  */
5282 #define ARANGE_TABLE_INCREMENT 64
5283
5284 /* Array of dies for which we should generate .debug_ranges info.  */
5285 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5286
5287 /* Number of elements currently allocated for ranges_table.  */
5288 static GTY(()) unsigned ranges_table_allocated;
5289
5290 /* Number of elements in ranges_table currently in use.  */
5291 static GTY(()) unsigned ranges_table_in_use;
5292
5293 /* Array of pairs of labels referenced in ranges_table.  */
5294 static GTY ((length ("ranges_by_label_allocated")))
5295      dw_ranges_by_label_ref ranges_by_label;
5296
5297 /* Number of elements currently allocated for ranges_by_label.  */
5298 static GTY(()) unsigned ranges_by_label_allocated;
5299
5300 /* Number of elements in ranges_by_label currently in use.  */
5301 static GTY(()) unsigned ranges_by_label_in_use;
5302
5303 /* Size (in elements) of increments by which we may expand the
5304    ranges_table.  */
5305 #define RANGES_TABLE_INCREMENT 64
5306
5307 /* Whether we have location lists that need outputting */
5308 static GTY(()) bool have_location_lists;
5309
5310 /* Unique label counter.  */
5311 static GTY(()) unsigned int loclabel_num;
5312
5313 #ifdef DWARF2_DEBUGGING_INFO
5314 /* Record whether the function being analyzed contains inlined functions.  */
5315 static int current_function_has_inlines;
5316 #endif
5317 #if 0 && defined (MIPS_DEBUGGING_INFO)
5318 static int comp_unit_has_inlines;
5319 #endif
5320
5321 /* The last file entry emitted by maybe_emit_file().  */
5322 static GTY(()) struct dwarf_file_data * last_emitted_file;
5323
5324 /* Number of internal labels generated by gen_internal_sym().  */
5325 static GTY(()) int label_num;
5326
5327 /* Cached result of previous call to lookup_filename.  */
5328 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5329
5330 #ifdef DWARF2_DEBUGGING_INFO
5331
5332 /* Offset from the "steady-state frame pointer" to the frame base,
5333    within the current function.  */
5334 static HOST_WIDE_INT frame_pointer_fb_offset;
5335
5336 /* Forward declarations for functions defined in this file.  */
5337
5338 static int is_pseudo_reg (const_rtx);
5339 static tree type_main_variant (tree);
5340 static int is_tagged_type (const_tree);
5341 static const char *dwarf_tag_name (unsigned);
5342 static const char *dwarf_attr_name (unsigned);
5343 static const char *dwarf_form_name (unsigned);
5344 static tree decl_ultimate_origin (const_tree);
5345 static tree decl_class_context (tree);
5346 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5347 static inline enum dw_val_class AT_class (dw_attr_ref);
5348 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5349 static inline unsigned AT_flag (dw_attr_ref);
5350 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5351 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5352 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5353 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5354 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5355                               unsigned long);
5356 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5357                                unsigned int, unsigned char *);
5358 static hashval_t debug_str_do_hash (const void *);
5359 static int debug_str_eq (const void *, const void *);
5360 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5361 static inline const char *AT_string (dw_attr_ref);
5362 static enum dwarf_form AT_string_form (dw_attr_ref);
5363 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5364 static void add_AT_specification (dw_die_ref, dw_die_ref);
5365 static inline dw_die_ref AT_ref (dw_attr_ref);
5366 static inline int AT_ref_external (dw_attr_ref);
5367 static inline void set_AT_ref_external (dw_attr_ref, int);
5368 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5369 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5370 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5371 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5372                              dw_loc_list_ref);
5373 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5374 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5375 static inline rtx AT_addr (dw_attr_ref);
5376 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5377 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5378 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5379 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5380                            unsigned HOST_WIDE_INT);
5381 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5382                                unsigned long);
5383 static inline const char *AT_lbl (dw_attr_ref);
5384 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5385 static const char *get_AT_low_pc (dw_die_ref);
5386 static const char *get_AT_hi_pc (dw_die_ref);
5387 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5388 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5389 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5390 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5391 static bool is_c_family (void);
5392 static bool is_cxx (void);
5393 static bool is_java (void);
5394 static bool is_fortran (void);
5395 static bool is_ada (void);
5396 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5397 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5398 static void add_child_die (dw_die_ref, dw_die_ref);
5399 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5400 static dw_die_ref lookup_type_die (tree);
5401 static void equate_type_number_to_die (tree, dw_die_ref);
5402 static hashval_t decl_die_table_hash (const void *);
5403 static int decl_die_table_eq (const void *, const void *);
5404 static dw_die_ref lookup_decl_die (tree);
5405 static hashval_t common_block_die_table_hash (const void *);
5406 static int common_block_die_table_eq (const void *, const void *);
5407 static hashval_t decl_loc_table_hash (const void *);
5408 static int decl_loc_table_eq (const void *, const void *);
5409 static var_loc_list *lookup_decl_loc (const_tree);
5410 static void equate_decl_number_to_die (tree, dw_die_ref);
5411 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5412 static void print_spaces (FILE *);
5413 static void print_die (dw_die_ref, FILE *);
5414 static void print_dwarf_line_table (FILE *);
5415 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5416 static dw_die_ref pop_compile_unit (dw_die_ref);
5417 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5418 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5419 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5420 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5421 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5422 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5423 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5424 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5425 static void compute_section_prefix (dw_die_ref);
5426 static int is_type_die (dw_die_ref);
5427 static int is_comdat_die (dw_die_ref);
5428 static int is_symbol_die (dw_die_ref);
5429 static void assign_symbol_names (dw_die_ref);
5430 static void break_out_includes (dw_die_ref);
5431 static hashval_t htab_cu_hash (const void *);
5432 static int htab_cu_eq (const void *, const void *);
5433 static void htab_cu_del (void *);
5434 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5435 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5436 static void add_sibling_attributes (dw_die_ref);
5437 static void build_abbrev_table (dw_die_ref);
5438 static void output_location_lists (dw_die_ref);
5439 static int constant_size (unsigned HOST_WIDE_INT);
5440 static unsigned long size_of_die (dw_die_ref);
5441 static void calc_die_sizes (dw_die_ref);
5442 static void mark_dies (dw_die_ref);
5443 static void unmark_dies (dw_die_ref);
5444 static void unmark_all_dies (dw_die_ref);
5445 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5446 static unsigned long size_of_aranges (void);
5447 static enum dwarf_form value_format (dw_attr_ref);
5448 static void output_value_format (dw_attr_ref);
5449 static void output_abbrev_section (void);
5450 static void output_die_symbol (dw_die_ref);
5451 static void output_die (dw_die_ref);
5452 static void output_compilation_unit_header (void);
5453 static void output_comp_unit (dw_die_ref, int);
5454 static const char *dwarf2_name (tree, int);
5455 static void add_pubname (tree, dw_die_ref);
5456 static void add_pubname_string (const char *, dw_die_ref);
5457 static void add_pubtype (tree, dw_die_ref);
5458 static void output_pubnames (VEC (pubname_entry,gc) *);
5459 static void add_arange (tree, dw_die_ref);
5460 static void output_aranges (void);
5461 static unsigned int add_ranges_num (int);
5462 static unsigned int add_ranges (const_tree);
5463 static unsigned int add_ranges_by_labels (const char *, const char *);
5464 static void output_ranges (void);
5465 static void output_line_info (void);
5466 static void output_file_names (void);
5467 static dw_die_ref base_type_die (tree);
5468 static int is_base_type (tree);
5469 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5470 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5471 static int type_is_enum (const_tree);
5472 static unsigned int dbx_reg_number (const_rtx);
5473 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5474 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5475 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5476                                                 enum var_init_status);
5477 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5478                                                      enum var_init_status);
5479 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5480                                          enum var_init_status);
5481 static int is_based_loc (const_rtx);
5482 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5483                                             enum var_init_status);
5484 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5485                                                enum var_init_status);
5486 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5487 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5488 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5489 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5490 static tree field_type (const_tree);
5491 static unsigned int simple_type_align_in_bits (const_tree);
5492 static unsigned int simple_decl_align_in_bits (const_tree);
5493 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5494 static HOST_WIDE_INT field_byte_offset (const_tree);
5495 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5496                                          dw_loc_descr_ref);
5497 static void add_data_member_location_attribute (dw_die_ref, tree);
5498 static void add_const_value_attribute (dw_die_ref, rtx);
5499 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5500 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5501 static void insert_float (const_rtx, unsigned char *);
5502 static rtx rtl_for_decl_location (tree);
5503 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5504                                                    enum dwarf_attribute);
5505 static void tree_add_const_value_attribute (dw_die_ref, tree);
5506 static void add_name_attribute (dw_die_ref, const char *);
5507 static void add_comp_dir_attribute (dw_die_ref);
5508 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5509 static void add_subscript_info (dw_die_ref, tree, bool);
5510 static void add_byte_size_attribute (dw_die_ref, tree);
5511 static void add_bit_offset_attribute (dw_die_ref, tree);
5512 static void add_bit_size_attribute (dw_die_ref, tree);
5513 static void add_prototyped_attribute (dw_die_ref, tree);
5514 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5515 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5516 static void add_src_coords_attributes (dw_die_ref, tree);
5517 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5518 static void push_decl_scope (tree);
5519 static void pop_decl_scope (void);
5520 static dw_die_ref scope_die_for (tree, dw_die_ref);
5521 static inline int local_scope_p (dw_die_ref);
5522 static inline int class_scope_p (dw_die_ref);
5523 static inline int class_or_namespace_scope_p (dw_die_ref);
5524 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5525 static void add_calling_convention_attribute (dw_die_ref, tree);
5526 static const char *type_tag (const_tree);
5527 static tree member_declared_type (const_tree);
5528 #if 0
5529 static const char *decl_start_label (tree);
5530 #endif
5531 static void gen_array_type_die (tree, dw_die_ref);
5532 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5533 #if 0
5534 static void gen_entry_point_die (tree, dw_die_ref);
5535 #endif
5536 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5537 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5538 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5539 static void gen_formal_types_die (tree, dw_die_ref);
5540 static void gen_subprogram_die (tree, dw_die_ref);
5541 static void gen_variable_die (tree, tree, dw_die_ref);
5542 static void gen_const_die (tree, dw_die_ref);
5543 static void gen_label_die (tree, dw_die_ref);
5544 static void gen_lexical_block_die (tree, dw_die_ref, int);
5545 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5546 static void gen_field_die (tree, dw_die_ref);
5547 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5548 static dw_die_ref gen_compile_unit_die (const char *);
5549 static void gen_inheritance_die (tree, tree, dw_die_ref);
5550 static void gen_member_die (tree, dw_die_ref);
5551 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5552                                                 enum debug_info_usage);
5553 static void gen_subroutine_type_die (tree, dw_die_ref);
5554 static void gen_typedef_die (tree, dw_die_ref);
5555 static void gen_type_die (tree, dw_die_ref);
5556 static void gen_block_die (tree, dw_die_ref, int);
5557 static void decls_for_scope (tree, dw_die_ref, int);
5558 static int is_redundant_typedef (const_tree);
5559 static void gen_namespace_die (tree, dw_die_ref);
5560 static void gen_decl_die (tree, tree, dw_die_ref);
5561 static dw_die_ref force_decl_die (tree);
5562 static dw_die_ref force_type_die (tree);
5563 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5564 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5565 static struct dwarf_file_data * lookup_filename (const char *);
5566 static void retry_incomplete_types (void);
5567 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5568 static void splice_child_die (dw_die_ref, dw_die_ref);
5569 static int file_info_cmp (const void *, const void *);
5570 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5571                                      const char *, const char *, unsigned);
5572 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5573                                        const char *, const char *,
5574                                        const char *);
5575 static void output_loc_list (dw_loc_list_ref);
5576 static char *gen_internal_sym (const char *);
5577
5578 static void prune_unmark_dies (dw_die_ref);
5579 static void prune_unused_types_mark (dw_die_ref, int);
5580 static void prune_unused_types_walk (dw_die_ref);
5581 static void prune_unused_types_walk_attribs (dw_die_ref);
5582 static void prune_unused_types_prune (dw_die_ref);
5583 static void prune_unused_types (void);
5584 static int maybe_emit_file (struct dwarf_file_data *fd);
5585
5586 /* Section names used to hold DWARF debugging information.  */
5587 #ifndef DEBUG_INFO_SECTION
5588 #define DEBUG_INFO_SECTION      ".debug_info"
5589 #endif
5590 #ifndef DEBUG_ABBREV_SECTION
5591 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5592 #endif
5593 #ifndef DEBUG_ARANGES_SECTION
5594 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5595 #endif
5596 #ifndef DEBUG_MACINFO_SECTION
5597 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5598 #endif
5599 #ifndef DEBUG_LINE_SECTION
5600 #define DEBUG_LINE_SECTION      ".debug_line"
5601 #endif
5602 #ifndef DEBUG_LOC_SECTION
5603 #define DEBUG_LOC_SECTION       ".debug_loc"
5604 #endif
5605 #ifndef DEBUG_PUBNAMES_SECTION
5606 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5607 #endif
5608 #ifndef DEBUG_STR_SECTION
5609 #define DEBUG_STR_SECTION       ".debug_str"
5610 #endif
5611 #ifndef DEBUG_RANGES_SECTION
5612 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5613 #endif
5614
5615 /* Standard ELF section names for compiled code and data.  */
5616 #ifndef TEXT_SECTION_NAME
5617 #define TEXT_SECTION_NAME       ".text"
5618 #endif
5619
5620 /* Section flags for .debug_str section.  */
5621 #define DEBUG_STR_SECTION_FLAGS \
5622   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5623    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5624    : SECTION_DEBUG)
5625
5626 /* Labels we insert at beginning sections we can reference instead of
5627    the section names themselves.  */
5628
5629 #ifndef TEXT_SECTION_LABEL
5630 #define TEXT_SECTION_LABEL              "Ltext"
5631 #endif
5632 #ifndef COLD_TEXT_SECTION_LABEL
5633 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5634 #endif
5635 #ifndef DEBUG_LINE_SECTION_LABEL
5636 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5637 #endif
5638 #ifndef DEBUG_INFO_SECTION_LABEL
5639 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5640 #endif
5641 #ifndef DEBUG_ABBREV_SECTION_LABEL
5642 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5643 #endif
5644 #ifndef DEBUG_LOC_SECTION_LABEL
5645 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5646 #endif
5647 #ifndef DEBUG_RANGES_SECTION_LABEL
5648 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5649 #endif
5650 #ifndef DEBUG_MACINFO_SECTION_LABEL
5651 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5652 #endif
5653
5654 /* Definitions of defaults for formats and names of various special
5655    (artificial) labels which may be generated within this file (when the -g
5656    options is used and DWARF2_DEBUGGING_INFO is in effect.
5657    If necessary, these may be overridden from within the tm.h file, but
5658    typically, overriding these defaults is unnecessary.  */
5659
5660 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5661 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5662 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5663 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5664 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5665 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5666 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5667 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5668 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5669 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5670
5671 #ifndef TEXT_END_LABEL
5672 #define TEXT_END_LABEL          "Letext"
5673 #endif
5674 #ifndef COLD_END_LABEL
5675 #define COLD_END_LABEL          "Letext_cold"
5676 #endif
5677 #ifndef BLOCK_BEGIN_LABEL
5678 #define BLOCK_BEGIN_LABEL       "LBB"
5679 #endif
5680 #ifndef BLOCK_END_LABEL
5681 #define BLOCK_END_LABEL         "LBE"
5682 #endif
5683 #ifndef LINE_CODE_LABEL
5684 #define LINE_CODE_LABEL         "LM"
5685 #endif
5686 #ifndef SEPARATE_LINE_CODE_LABEL
5687 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5688 #endif
5689
5690 \f
5691 /* We allow a language front-end to designate a function that is to be
5692    called to "demangle" any name before it is put into a DIE.  */
5693
5694 static const char *(*demangle_name_func) (const char *);
5695
5696 void
5697 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5698 {
5699   demangle_name_func = func;
5700 }
5701
5702 /* Test if rtl node points to a pseudo register.  */
5703
5704 static inline int
5705 is_pseudo_reg (const_rtx rtl)
5706 {
5707   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5708           || (GET_CODE (rtl) == SUBREG
5709               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5710 }
5711
5712 /* Return a reference to a type, with its const and volatile qualifiers
5713    removed.  */
5714
5715 static inline tree
5716 type_main_variant (tree type)
5717 {
5718   type = TYPE_MAIN_VARIANT (type);
5719
5720   /* ??? There really should be only one main variant among any group of
5721      variants of a given type (and all of the MAIN_VARIANT values for all
5722      members of the group should point to that one type) but sometimes the C
5723      front-end messes this up for array types, so we work around that bug
5724      here.  */
5725   if (TREE_CODE (type) == ARRAY_TYPE)
5726     while (type != TYPE_MAIN_VARIANT (type))
5727       type = TYPE_MAIN_VARIANT (type);
5728
5729   return type;
5730 }
5731
5732 /* Return nonzero if the given type node represents a tagged type.  */
5733
5734 static inline int
5735 is_tagged_type (const_tree type)
5736 {
5737   enum tree_code code = TREE_CODE (type);
5738
5739   return (code == RECORD_TYPE || code == UNION_TYPE
5740           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5741 }
5742
5743 /* Convert a DIE tag into its string name.  */
5744
5745 static const char *
5746 dwarf_tag_name (unsigned int tag)
5747 {
5748   switch (tag)
5749     {
5750     case DW_TAG_padding:
5751       return "DW_TAG_padding";
5752     case DW_TAG_array_type:
5753       return "DW_TAG_array_type";
5754     case DW_TAG_class_type:
5755       return "DW_TAG_class_type";
5756     case DW_TAG_entry_point:
5757       return "DW_TAG_entry_point";
5758     case DW_TAG_enumeration_type:
5759       return "DW_TAG_enumeration_type";
5760     case DW_TAG_formal_parameter:
5761       return "DW_TAG_formal_parameter";
5762     case DW_TAG_imported_declaration:
5763       return "DW_TAG_imported_declaration";
5764     case DW_TAG_label:
5765       return "DW_TAG_label";
5766     case DW_TAG_lexical_block:
5767       return "DW_TAG_lexical_block";
5768     case DW_TAG_member:
5769       return "DW_TAG_member";
5770     case DW_TAG_pointer_type:
5771       return "DW_TAG_pointer_type";
5772     case DW_TAG_reference_type:
5773       return "DW_TAG_reference_type";
5774     case DW_TAG_compile_unit:
5775       return "DW_TAG_compile_unit";
5776     case DW_TAG_string_type:
5777       return "DW_TAG_string_type";
5778     case DW_TAG_structure_type:
5779       return "DW_TAG_structure_type";
5780     case DW_TAG_subroutine_type:
5781       return "DW_TAG_subroutine_type";
5782     case DW_TAG_typedef:
5783       return "DW_TAG_typedef";
5784     case DW_TAG_union_type:
5785       return "DW_TAG_union_type";
5786     case DW_TAG_unspecified_parameters:
5787       return "DW_TAG_unspecified_parameters";
5788     case DW_TAG_variant:
5789       return "DW_TAG_variant";
5790     case DW_TAG_common_block:
5791       return "DW_TAG_common_block";
5792     case DW_TAG_common_inclusion:
5793       return "DW_TAG_common_inclusion";
5794     case DW_TAG_inheritance:
5795       return "DW_TAG_inheritance";
5796     case DW_TAG_inlined_subroutine:
5797       return "DW_TAG_inlined_subroutine";
5798     case DW_TAG_module:
5799       return "DW_TAG_module";
5800     case DW_TAG_ptr_to_member_type:
5801       return "DW_TAG_ptr_to_member_type";
5802     case DW_TAG_set_type:
5803       return "DW_TAG_set_type";
5804     case DW_TAG_subrange_type:
5805       return "DW_TAG_subrange_type";
5806     case DW_TAG_with_stmt:
5807       return "DW_TAG_with_stmt";
5808     case DW_TAG_access_declaration:
5809       return "DW_TAG_access_declaration";
5810     case DW_TAG_base_type:
5811       return "DW_TAG_base_type";
5812     case DW_TAG_catch_block:
5813       return "DW_TAG_catch_block";
5814     case DW_TAG_const_type:
5815       return "DW_TAG_const_type";
5816     case DW_TAG_constant:
5817       return "DW_TAG_constant";
5818     case DW_TAG_enumerator:
5819       return "DW_TAG_enumerator";
5820     case DW_TAG_file_type:
5821       return "DW_TAG_file_type";
5822     case DW_TAG_friend:
5823       return "DW_TAG_friend";
5824     case DW_TAG_namelist:
5825       return "DW_TAG_namelist";
5826     case DW_TAG_namelist_item:
5827       return "DW_TAG_namelist_item";
5828     case DW_TAG_packed_type:
5829       return "DW_TAG_packed_type";
5830     case DW_TAG_subprogram:
5831       return "DW_TAG_subprogram";
5832     case DW_TAG_template_type_param:
5833       return "DW_TAG_template_type_param";
5834     case DW_TAG_template_value_param:
5835       return "DW_TAG_template_value_param";
5836     case DW_TAG_thrown_type:
5837       return "DW_TAG_thrown_type";
5838     case DW_TAG_try_block:
5839       return "DW_TAG_try_block";
5840     case DW_TAG_variant_part:
5841       return "DW_TAG_variant_part";
5842     case DW_TAG_variable:
5843       return "DW_TAG_variable";
5844     case DW_TAG_volatile_type:
5845       return "DW_TAG_volatile_type";
5846     case DW_TAG_dwarf_procedure:
5847       return "DW_TAG_dwarf_procedure";
5848     case DW_TAG_restrict_type:
5849       return "DW_TAG_restrict_type";
5850     case DW_TAG_interface_type:
5851       return "DW_TAG_interface_type";
5852     case DW_TAG_namespace:
5853       return "DW_TAG_namespace";
5854     case DW_TAG_imported_module:
5855       return "DW_TAG_imported_module";
5856     case DW_TAG_unspecified_type:
5857       return "DW_TAG_unspecified_type";
5858     case DW_TAG_partial_unit:
5859       return "DW_TAG_partial_unit";
5860     case DW_TAG_imported_unit:
5861       return "DW_TAG_imported_unit";
5862     case DW_TAG_condition:
5863       return "DW_TAG_condition";
5864     case DW_TAG_shared_type:
5865       return "DW_TAG_shared_type";
5866     case DW_TAG_MIPS_loop:
5867       return "DW_TAG_MIPS_loop";
5868     case DW_TAG_format_label:
5869       return "DW_TAG_format_label";
5870     case DW_TAG_function_template:
5871       return "DW_TAG_function_template";
5872     case DW_TAG_class_template:
5873       return "DW_TAG_class_template";
5874     case DW_TAG_GNU_BINCL:
5875       return "DW_TAG_GNU_BINCL";
5876     case DW_TAG_GNU_EINCL:
5877       return "DW_TAG_GNU_EINCL";
5878     default:
5879       return "DW_TAG_<unknown>";
5880     }
5881 }
5882
5883 /* Convert a DWARF attribute code into its string name.  */
5884
5885 static const char *
5886 dwarf_attr_name (unsigned int attr)
5887 {
5888   switch (attr)
5889     {
5890     case DW_AT_sibling:
5891       return "DW_AT_sibling";
5892     case DW_AT_location:
5893       return "DW_AT_location";
5894     case DW_AT_name:
5895       return "DW_AT_name";
5896     case DW_AT_ordering:
5897       return "DW_AT_ordering";
5898     case DW_AT_subscr_data:
5899       return "DW_AT_subscr_data";
5900     case DW_AT_byte_size:
5901       return "DW_AT_byte_size";
5902     case DW_AT_bit_offset:
5903       return "DW_AT_bit_offset";
5904     case DW_AT_bit_size:
5905       return "DW_AT_bit_size";
5906     case DW_AT_element_list:
5907       return "DW_AT_element_list";
5908     case DW_AT_stmt_list:
5909       return "DW_AT_stmt_list";
5910     case DW_AT_low_pc:
5911       return "DW_AT_low_pc";
5912     case DW_AT_high_pc:
5913       return "DW_AT_high_pc";
5914     case DW_AT_language:
5915       return "DW_AT_language";
5916     case DW_AT_member:
5917       return "DW_AT_member";
5918     case DW_AT_discr:
5919       return "DW_AT_discr";
5920     case DW_AT_discr_value:
5921       return "DW_AT_discr_value";
5922     case DW_AT_visibility:
5923       return "DW_AT_visibility";
5924     case DW_AT_import:
5925       return "DW_AT_import";
5926     case DW_AT_string_length:
5927       return "DW_AT_string_length";
5928     case DW_AT_common_reference:
5929       return "DW_AT_common_reference";
5930     case DW_AT_comp_dir:
5931       return "DW_AT_comp_dir";
5932     case DW_AT_const_value:
5933       return "DW_AT_const_value";
5934     case DW_AT_containing_type:
5935       return "DW_AT_containing_type";
5936     case DW_AT_default_value:
5937       return "DW_AT_default_value";
5938     case DW_AT_inline:
5939       return "DW_AT_inline";
5940     case DW_AT_is_optional:
5941       return "DW_AT_is_optional";
5942     case DW_AT_lower_bound:
5943       return "DW_AT_lower_bound";
5944     case DW_AT_producer:
5945       return "DW_AT_producer";
5946     case DW_AT_prototyped:
5947       return "DW_AT_prototyped";
5948     case DW_AT_return_addr:
5949       return "DW_AT_return_addr";
5950     case DW_AT_start_scope:
5951       return "DW_AT_start_scope";
5952     case DW_AT_bit_stride:
5953       return "DW_AT_bit_stride";
5954     case DW_AT_upper_bound:
5955       return "DW_AT_upper_bound";
5956     case DW_AT_abstract_origin:
5957       return "DW_AT_abstract_origin";
5958     case DW_AT_accessibility:
5959       return "DW_AT_accessibility";
5960     case DW_AT_address_class:
5961       return "DW_AT_address_class";
5962     case DW_AT_artificial:
5963       return "DW_AT_artificial";
5964     case DW_AT_base_types:
5965       return "DW_AT_base_types";
5966     case DW_AT_calling_convention:
5967       return "DW_AT_calling_convention";
5968     case DW_AT_count:
5969       return "DW_AT_count";
5970     case DW_AT_data_member_location:
5971       return "DW_AT_data_member_location";
5972     case DW_AT_decl_column:
5973       return "DW_AT_decl_column";
5974     case DW_AT_decl_file:
5975       return "DW_AT_decl_file";
5976     case DW_AT_decl_line:
5977       return "DW_AT_decl_line";
5978     case DW_AT_declaration:
5979       return "DW_AT_declaration";
5980     case DW_AT_discr_list:
5981       return "DW_AT_discr_list";
5982     case DW_AT_encoding:
5983       return "DW_AT_encoding";
5984     case DW_AT_external:
5985       return "DW_AT_external";
5986     case DW_AT_explicit:
5987       return "DW_AT_explicit";
5988     case DW_AT_frame_base:
5989       return "DW_AT_frame_base";
5990     case DW_AT_friend:
5991       return "DW_AT_friend";
5992     case DW_AT_identifier_case:
5993       return "DW_AT_identifier_case";
5994     case DW_AT_macro_info:
5995       return "DW_AT_macro_info";
5996     case DW_AT_namelist_items:
5997       return "DW_AT_namelist_items";
5998     case DW_AT_priority:
5999       return "DW_AT_priority";
6000     case DW_AT_segment:
6001       return "DW_AT_segment";
6002     case DW_AT_specification:
6003       return "DW_AT_specification";
6004     case DW_AT_static_link:
6005       return "DW_AT_static_link";
6006     case DW_AT_type:
6007       return "DW_AT_type";
6008     case DW_AT_use_location:
6009       return "DW_AT_use_location";
6010     case DW_AT_variable_parameter:
6011       return "DW_AT_variable_parameter";
6012     case DW_AT_virtuality:
6013       return "DW_AT_virtuality";
6014     case DW_AT_vtable_elem_location:
6015       return "DW_AT_vtable_elem_location";
6016
6017     case DW_AT_allocated:
6018       return "DW_AT_allocated";
6019     case DW_AT_associated:
6020       return "DW_AT_associated";
6021     case DW_AT_data_location:
6022       return "DW_AT_data_location";
6023     case DW_AT_byte_stride:
6024       return "DW_AT_byte_stride";
6025     case DW_AT_entry_pc:
6026       return "DW_AT_entry_pc";
6027     case DW_AT_use_UTF8:
6028       return "DW_AT_use_UTF8";
6029     case DW_AT_extension:
6030       return "DW_AT_extension";
6031     case DW_AT_ranges:
6032       return "DW_AT_ranges";
6033     case DW_AT_trampoline:
6034       return "DW_AT_trampoline";
6035     case DW_AT_call_column:
6036       return "DW_AT_call_column";
6037     case DW_AT_call_file:
6038       return "DW_AT_call_file";
6039     case DW_AT_call_line:
6040       return "DW_AT_call_line";
6041
6042     case DW_AT_MIPS_fde:
6043       return "DW_AT_MIPS_fde";
6044     case DW_AT_MIPS_loop_begin:
6045       return "DW_AT_MIPS_loop_begin";
6046     case DW_AT_MIPS_tail_loop_begin:
6047       return "DW_AT_MIPS_tail_loop_begin";
6048     case DW_AT_MIPS_epilog_begin:
6049       return "DW_AT_MIPS_epilog_begin";
6050     case DW_AT_MIPS_loop_unroll_factor:
6051       return "DW_AT_MIPS_loop_unroll_factor";
6052     case DW_AT_MIPS_software_pipeline_depth:
6053       return "DW_AT_MIPS_software_pipeline_depth";
6054     case DW_AT_MIPS_linkage_name:
6055       return "DW_AT_MIPS_linkage_name";
6056     case DW_AT_MIPS_stride:
6057       return "DW_AT_MIPS_stride";
6058     case DW_AT_MIPS_abstract_name:
6059       return "DW_AT_MIPS_abstract_name";
6060     case DW_AT_MIPS_clone_origin:
6061       return "DW_AT_MIPS_clone_origin";
6062     case DW_AT_MIPS_has_inlines:
6063       return "DW_AT_MIPS_has_inlines";
6064
6065     case DW_AT_sf_names:
6066       return "DW_AT_sf_names";
6067     case DW_AT_src_info:
6068       return "DW_AT_src_info";
6069     case DW_AT_mac_info:
6070       return "DW_AT_mac_info";
6071     case DW_AT_src_coords:
6072       return "DW_AT_src_coords";
6073     case DW_AT_body_begin:
6074       return "DW_AT_body_begin";
6075     case DW_AT_body_end:
6076       return "DW_AT_body_end";
6077     case DW_AT_GNU_vector:
6078       return "DW_AT_GNU_vector";
6079
6080     case DW_AT_VMS_rtnbeg_pd_address:
6081       return "DW_AT_VMS_rtnbeg_pd_address";
6082
6083     default:
6084       return "DW_AT_<unknown>";
6085     }
6086 }
6087
6088 /* Convert a DWARF value form code into its string name.  */
6089
6090 static const char *
6091 dwarf_form_name (unsigned int form)
6092 {
6093   switch (form)
6094     {
6095     case DW_FORM_addr:
6096       return "DW_FORM_addr";
6097     case DW_FORM_block2:
6098       return "DW_FORM_block2";
6099     case DW_FORM_block4:
6100       return "DW_FORM_block4";
6101     case DW_FORM_data2:
6102       return "DW_FORM_data2";
6103     case DW_FORM_data4:
6104       return "DW_FORM_data4";
6105     case DW_FORM_data8:
6106       return "DW_FORM_data8";
6107     case DW_FORM_string:
6108       return "DW_FORM_string";
6109     case DW_FORM_block:
6110       return "DW_FORM_block";
6111     case DW_FORM_block1:
6112       return "DW_FORM_block1";
6113     case DW_FORM_data1:
6114       return "DW_FORM_data1";
6115     case DW_FORM_flag:
6116       return "DW_FORM_flag";
6117     case DW_FORM_sdata:
6118       return "DW_FORM_sdata";
6119     case DW_FORM_strp:
6120       return "DW_FORM_strp";
6121     case DW_FORM_udata:
6122       return "DW_FORM_udata";
6123     case DW_FORM_ref_addr:
6124       return "DW_FORM_ref_addr";
6125     case DW_FORM_ref1:
6126       return "DW_FORM_ref1";
6127     case DW_FORM_ref2:
6128       return "DW_FORM_ref2";
6129     case DW_FORM_ref4:
6130       return "DW_FORM_ref4";
6131     case DW_FORM_ref8:
6132       return "DW_FORM_ref8";
6133     case DW_FORM_ref_udata:
6134       return "DW_FORM_ref_udata";
6135     case DW_FORM_indirect:
6136       return "DW_FORM_indirect";
6137     default:
6138       return "DW_FORM_<unknown>";
6139     }
6140 }
6141 \f
6142 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6143    instance of an inlined instance of a decl which is local to an inline
6144    function, so we have to trace all of the way back through the origin chain
6145    to find out what sort of node actually served as the original seed for the
6146    given block.  */
6147
6148 static tree
6149 decl_ultimate_origin (const_tree decl)
6150 {
6151   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6152     return NULL_TREE;
6153
6154   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6155      nodes in the function to point to themselves; ignore that if
6156      we're trying to output the abstract instance of this function.  */
6157   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6158     return NULL_TREE;
6159
6160   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6161      most distant ancestor, this should never happen.  */
6162   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6163
6164   return DECL_ABSTRACT_ORIGIN (decl);
6165 }
6166
6167 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6168    of a virtual function may refer to a base class, so we check the 'this'
6169    parameter.  */
6170
6171 static tree
6172 decl_class_context (tree decl)
6173 {
6174   tree context = NULL_TREE;
6175
6176   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6177     context = DECL_CONTEXT (decl);
6178   else
6179     context = TYPE_MAIN_VARIANT
6180       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6181
6182   if (context && !TYPE_P (context))
6183     context = NULL_TREE;
6184
6185   return context;
6186 }
6187 \f
6188 /* Add an attribute/value pair to a DIE.  */
6189
6190 static inline void
6191 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6192 {
6193   /* Maybe this should be an assert?  */
6194   if (die == NULL)
6195     return;
6196
6197   if (die->die_attr == NULL)
6198     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6199   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6200 }
6201
6202 static inline enum dw_val_class
6203 AT_class (dw_attr_ref a)
6204 {
6205   return a->dw_attr_val.val_class;
6206 }
6207
6208 /* Add a flag value attribute to a DIE.  */
6209
6210 static inline void
6211 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6212 {
6213   dw_attr_node attr;
6214
6215   attr.dw_attr = attr_kind;
6216   attr.dw_attr_val.val_class = dw_val_class_flag;
6217   attr.dw_attr_val.v.val_flag = flag;
6218   add_dwarf_attr (die, &attr);
6219 }
6220
6221 static inline unsigned
6222 AT_flag (dw_attr_ref a)
6223 {
6224   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6225   return a->dw_attr_val.v.val_flag;
6226 }
6227
6228 /* Add a signed integer attribute value to a DIE.  */
6229
6230 static inline void
6231 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6232 {
6233   dw_attr_node attr;
6234
6235   attr.dw_attr = attr_kind;
6236   attr.dw_attr_val.val_class = dw_val_class_const;
6237   attr.dw_attr_val.v.val_int = int_val;
6238   add_dwarf_attr (die, &attr);
6239 }
6240
6241 static inline HOST_WIDE_INT
6242 AT_int (dw_attr_ref a)
6243 {
6244   gcc_assert (a && AT_class (a) == dw_val_class_const);
6245   return a->dw_attr_val.v.val_int;
6246 }
6247
6248 /* Add an unsigned integer attribute value to a DIE.  */
6249
6250 static inline void
6251 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6252                  unsigned HOST_WIDE_INT unsigned_val)
6253 {
6254   dw_attr_node attr;
6255
6256   attr.dw_attr = attr_kind;
6257   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6258   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6259   add_dwarf_attr (die, &attr);
6260 }
6261
6262 static inline unsigned HOST_WIDE_INT
6263 AT_unsigned (dw_attr_ref a)
6264 {
6265   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6266   return a->dw_attr_val.v.val_unsigned;
6267 }
6268
6269 /* Add an unsigned double integer attribute value to a DIE.  */
6270
6271 static inline void
6272 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6273                   long unsigned int val_hi, long unsigned int val_low)
6274 {
6275   dw_attr_node attr;
6276
6277   attr.dw_attr = attr_kind;
6278   attr.dw_attr_val.val_class = dw_val_class_long_long;
6279   attr.dw_attr_val.v.val_long_long.hi = val_hi;
6280   attr.dw_attr_val.v.val_long_long.low = val_low;
6281   add_dwarf_attr (die, &attr);
6282 }
6283
6284 /* Add a floating point attribute value to a DIE and return it.  */
6285
6286 static inline void
6287 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6288             unsigned int length, unsigned int elt_size, unsigned char *array)
6289 {
6290   dw_attr_node attr;
6291
6292   attr.dw_attr = attr_kind;
6293   attr.dw_attr_val.val_class = dw_val_class_vec;
6294   attr.dw_attr_val.v.val_vec.length = length;
6295   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6296   attr.dw_attr_val.v.val_vec.array = array;
6297   add_dwarf_attr (die, &attr);
6298 }
6299
6300 /* Hash and equality functions for debug_str_hash.  */
6301
6302 static hashval_t
6303 debug_str_do_hash (const void *x)
6304 {
6305   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6306 }
6307
6308 static int
6309 debug_str_eq (const void *x1, const void *x2)
6310 {
6311   return strcmp ((((const struct indirect_string_node *)x1)->str),
6312                  (const char *)x2) == 0;
6313 }
6314
6315 static struct indirect_string_node *
6316 find_AT_string (const char *str)
6317 {
6318   struct indirect_string_node *node;
6319   void **slot;
6320
6321   if (! debug_str_hash)
6322     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6323                                       debug_str_eq, NULL);
6324
6325   slot = htab_find_slot_with_hash (debug_str_hash, str,
6326                                    htab_hash_string (str), INSERT);
6327   if (*slot == NULL)
6328     {
6329       node = (struct indirect_string_node *)
6330                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6331       node->str = ggc_strdup (str);
6332       *slot = node;
6333     }
6334   else
6335     node = (struct indirect_string_node *) *slot;
6336
6337   node->refcount++;
6338   return node;
6339 }
6340
6341 /* Add a string attribute value to a DIE.  */
6342
6343 static inline void
6344 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6345 {
6346   dw_attr_node attr;
6347   struct indirect_string_node *node;
6348
6349   node = find_AT_string (str);
6350
6351   attr.dw_attr = attr_kind;
6352   attr.dw_attr_val.val_class = dw_val_class_str;
6353   attr.dw_attr_val.v.val_str = node;
6354   add_dwarf_attr (die, &attr);
6355 }
6356
6357 static inline const char *
6358 AT_string (dw_attr_ref a)
6359 {
6360   gcc_assert (a && AT_class (a) == dw_val_class_str);
6361   return a->dw_attr_val.v.val_str->str;
6362 }
6363
6364 /* Find out whether a string should be output inline in DIE
6365    or out-of-line in .debug_str section.  */
6366
6367 static enum dwarf_form
6368 AT_string_form (dw_attr_ref a)
6369 {
6370   struct indirect_string_node *node;
6371   unsigned int len;
6372   char label[32];
6373
6374   gcc_assert (a && AT_class (a) == dw_val_class_str);
6375
6376   node = a->dw_attr_val.v.val_str;
6377   if (node->form)
6378     return node->form;
6379
6380   len = strlen (node->str) + 1;
6381
6382   /* If the string is shorter or equal to the size of the reference, it is
6383      always better to put it inline.  */
6384   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6385     return node->form = DW_FORM_string;
6386
6387   /* If we cannot expect the linker to merge strings in .debug_str
6388      section, only put it into .debug_str if it is worth even in this
6389      single module.  */
6390   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6391       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6392     return node->form = DW_FORM_string;
6393
6394   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6395   ++dw2_string_counter;
6396   node->label = xstrdup (label);
6397
6398   return node->form = DW_FORM_strp;
6399 }
6400
6401 /* Add a DIE reference attribute value to a DIE.  */
6402
6403 static inline void
6404 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6405 {
6406   dw_attr_node attr;
6407
6408   attr.dw_attr = attr_kind;
6409   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6410   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6411   attr.dw_attr_val.v.val_die_ref.external = 0;
6412   add_dwarf_attr (die, &attr);
6413 }
6414
6415 /* Add an AT_specification attribute to a DIE, and also make the back
6416    pointer from the specification to the definition.  */
6417
6418 static inline void
6419 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6420 {
6421   add_AT_die_ref (die, DW_AT_specification, targ_die);
6422   gcc_assert (!targ_die->die_definition);
6423   targ_die->die_definition = die;
6424 }
6425
6426 static inline dw_die_ref
6427 AT_ref (dw_attr_ref a)
6428 {
6429   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6430   return a->dw_attr_val.v.val_die_ref.die;
6431 }
6432
6433 static inline int
6434 AT_ref_external (dw_attr_ref a)
6435 {
6436   if (a && AT_class (a) == dw_val_class_die_ref)
6437     return a->dw_attr_val.v.val_die_ref.external;
6438
6439   return 0;
6440 }
6441
6442 static inline void
6443 set_AT_ref_external (dw_attr_ref a, int i)
6444 {
6445   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6446   a->dw_attr_val.v.val_die_ref.external = i;
6447 }
6448
6449 /* Add an FDE reference attribute value to a DIE.  */
6450
6451 static inline void
6452 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6453 {
6454   dw_attr_node attr;
6455
6456   attr.dw_attr = attr_kind;
6457   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6458   attr.dw_attr_val.v.val_fde_index = targ_fde;
6459   add_dwarf_attr (die, &attr);
6460 }
6461
6462 /* Add a location description attribute value to a DIE.  */
6463
6464 static inline void
6465 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6466 {
6467   dw_attr_node attr;
6468
6469   attr.dw_attr = attr_kind;
6470   attr.dw_attr_val.val_class = dw_val_class_loc;
6471   attr.dw_attr_val.v.val_loc = loc;
6472   add_dwarf_attr (die, &attr);
6473 }
6474
6475 static inline dw_loc_descr_ref
6476 AT_loc (dw_attr_ref a)
6477 {
6478   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6479   return a->dw_attr_val.v.val_loc;
6480 }
6481
6482 static inline void
6483 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6484 {
6485   dw_attr_node attr;
6486
6487   attr.dw_attr = attr_kind;
6488   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6489   attr.dw_attr_val.v.val_loc_list = loc_list;
6490   add_dwarf_attr (die, &attr);
6491   have_location_lists = true;
6492 }
6493
6494 static inline dw_loc_list_ref
6495 AT_loc_list (dw_attr_ref a)
6496 {
6497   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6498   return a->dw_attr_val.v.val_loc_list;
6499 }
6500
6501 /* Add an address constant attribute value to a DIE.  */
6502
6503 static inline void
6504 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6505 {
6506   dw_attr_node attr;
6507
6508   attr.dw_attr = attr_kind;
6509   attr.dw_attr_val.val_class = dw_val_class_addr;
6510   attr.dw_attr_val.v.val_addr = addr;
6511   add_dwarf_attr (die, &attr);
6512 }
6513
6514 /* Get the RTX from to an address DIE attribute.  */
6515
6516 static inline rtx
6517 AT_addr (dw_attr_ref a)
6518 {
6519   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6520   return a->dw_attr_val.v.val_addr;
6521 }
6522
6523 /* Add a file attribute value to a DIE.  */
6524
6525 static inline void
6526 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6527              struct dwarf_file_data *fd)
6528 {
6529   dw_attr_node attr;
6530
6531   attr.dw_attr = attr_kind;
6532   attr.dw_attr_val.val_class = dw_val_class_file;
6533   attr.dw_attr_val.v.val_file = fd;
6534   add_dwarf_attr (die, &attr);
6535 }
6536
6537 /* Get the dwarf_file_data from a file DIE attribute.  */
6538
6539 static inline struct dwarf_file_data *
6540 AT_file (dw_attr_ref a)
6541 {
6542   gcc_assert (a && AT_class (a) == dw_val_class_file);
6543   return a->dw_attr_val.v.val_file;
6544 }
6545
6546 /* Add a label identifier attribute value to a DIE.  */
6547
6548 static inline void
6549 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6550 {
6551   dw_attr_node attr;
6552
6553   attr.dw_attr = attr_kind;
6554   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6555   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6556   add_dwarf_attr (die, &attr);
6557 }
6558
6559 /* Add a section offset attribute value to a DIE, an offset into the
6560    debug_line section.  */
6561
6562 static inline void
6563 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6564                 const char *label)
6565 {
6566   dw_attr_node attr;
6567
6568   attr.dw_attr = attr_kind;
6569   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6570   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6571   add_dwarf_attr (die, &attr);
6572 }
6573
6574 /* Add a section offset attribute value to a DIE, an offset into the
6575    debug_macinfo section.  */
6576
6577 static inline void
6578 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6579                const char *label)
6580 {
6581   dw_attr_node attr;
6582
6583   attr.dw_attr = attr_kind;
6584   attr.dw_attr_val.val_class = dw_val_class_macptr;
6585   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6586   add_dwarf_attr (die, &attr);
6587 }
6588
6589 /* Add an offset attribute value to a DIE.  */
6590
6591 static inline void
6592 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6593                unsigned HOST_WIDE_INT offset)
6594 {
6595   dw_attr_node attr;
6596
6597   attr.dw_attr = attr_kind;
6598   attr.dw_attr_val.val_class = dw_val_class_offset;
6599   attr.dw_attr_val.v.val_offset = offset;
6600   add_dwarf_attr (die, &attr);
6601 }
6602
6603 /* Add an range_list attribute value to a DIE.  */
6604
6605 static void
6606 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6607                    long unsigned int offset)
6608 {
6609   dw_attr_node attr;
6610
6611   attr.dw_attr = attr_kind;
6612   attr.dw_attr_val.val_class = dw_val_class_range_list;
6613   attr.dw_attr_val.v.val_offset = offset;
6614   add_dwarf_attr (die, &attr);
6615 }
6616
6617 static inline const char *
6618 AT_lbl (dw_attr_ref a)
6619 {
6620   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6621                     || AT_class (a) == dw_val_class_lineptr
6622                     || AT_class (a) == dw_val_class_macptr));
6623   return a->dw_attr_val.v.val_lbl_id;
6624 }
6625
6626 /* Get the attribute of type attr_kind.  */
6627
6628 static dw_attr_ref
6629 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6630 {
6631   dw_attr_ref a;
6632   unsigned ix;
6633   dw_die_ref spec = NULL;
6634
6635   if (! die)
6636     return NULL;
6637
6638   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6639     if (a->dw_attr == attr_kind)
6640       return a;
6641     else if (a->dw_attr == DW_AT_specification
6642              || a->dw_attr == DW_AT_abstract_origin)
6643       spec = AT_ref (a);
6644
6645   if (spec)
6646     return get_AT (spec, attr_kind);
6647
6648   return NULL;
6649 }
6650
6651 /* Return the "low pc" attribute value, typically associated with a subprogram
6652    DIE.  Return null if the "low pc" attribute is either not present, or if it
6653    cannot be represented as an assembler label identifier.  */
6654
6655 static inline const char *
6656 get_AT_low_pc (dw_die_ref die)
6657 {
6658   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6659
6660   return a ? AT_lbl (a) : NULL;
6661 }
6662
6663 /* Return the "high pc" attribute value, typically associated with a subprogram
6664    DIE.  Return null if the "high pc" attribute is either not present, or if it
6665    cannot be represented as an assembler label identifier.  */
6666
6667 static inline const char *
6668 get_AT_hi_pc (dw_die_ref die)
6669 {
6670   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6671
6672   return a ? AT_lbl (a) : NULL;
6673 }
6674
6675 /* Return the value of the string attribute designated by ATTR_KIND, or
6676    NULL if it is not present.  */
6677
6678 static inline const char *
6679 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6680 {
6681   dw_attr_ref a = get_AT (die, attr_kind);
6682
6683   return a ? AT_string (a) : NULL;
6684 }
6685
6686 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6687    if it is not present.  */
6688
6689 static inline int
6690 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6691 {
6692   dw_attr_ref a = get_AT (die, attr_kind);
6693
6694   return a ? AT_flag (a) : 0;
6695 }
6696
6697 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6698    if it is not present.  */
6699
6700 static inline unsigned
6701 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6702 {
6703   dw_attr_ref a = get_AT (die, attr_kind);
6704
6705   return a ? AT_unsigned (a) : 0;
6706 }
6707
6708 static inline dw_die_ref
6709 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6710 {
6711   dw_attr_ref a = get_AT (die, attr_kind);
6712
6713   return a ? AT_ref (a) : NULL;
6714 }
6715
6716 static inline struct dwarf_file_data *
6717 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6718 {
6719   dw_attr_ref a = get_AT (die, attr_kind);
6720
6721   return a ? AT_file (a) : NULL;
6722 }
6723
6724 /* Return TRUE if the language is C or C++.  */
6725
6726 static inline bool
6727 is_c_family (void)
6728 {
6729   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6730
6731   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6732           || lang == DW_LANG_C99
6733           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6734 }
6735
6736 /* Return TRUE if the language is C++.  */
6737
6738 static inline bool
6739 is_cxx (void)
6740 {
6741   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6742
6743   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6744 }
6745
6746 /* Return TRUE if the language is Fortran.  */
6747
6748 static inline bool
6749 is_fortran (void)
6750 {
6751   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6752
6753   return (lang == DW_LANG_Fortran77
6754           || lang == DW_LANG_Fortran90
6755           || lang == DW_LANG_Fortran95);
6756 }
6757
6758 /* Return TRUE if the language is Java.  */
6759
6760 static inline bool
6761 is_java (void)
6762 {
6763   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6764
6765   return lang == DW_LANG_Java;
6766 }
6767
6768 /* Return TRUE if the language is Ada.  */
6769
6770 static inline bool
6771 is_ada (void)
6772 {
6773   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6774
6775   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6776 }
6777
6778 /* Remove the specified attribute if present.  */
6779
6780 static void
6781 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6782 {
6783   dw_attr_ref a;
6784   unsigned ix;
6785
6786   if (! die)
6787     return;
6788
6789   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6790     if (a->dw_attr == attr_kind)
6791       {
6792         if (AT_class (a) == dw_val_class_str)
6793           if (a->dw_attr_val.v.val_str->refcount)
6794             a->dw_attr_val.v.val_str->refcount--;
6795
6796         /* VEC_ordered_remove should help reduce the number of abbrevs
6797            that are needed.  */
6798         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6799         return;
6800       }
6801 }
6802
6803 /* Remove CHILD from its parent.  PREV must have the property that
6804    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6805
6806 static void
6807 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6808 {
6809   gcc_assert (child->die_parent == prev->die_parent);
6810   gcc_assert (prev->die_sib == child);
6811   if (prev == child)
6812     {
6813       gcc_assert (child->die_parent->die_child == child);
6814       prev = NULL;
6815     }
6816   else
6817     prev->die_sib = child->die_sib;
6818   if (child->die_parent->die_child == child)
6819     child->die_parent->die_child = prev;
6820 }
6821
6822 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6823    matches TAG.  */
6824
6825 static void
6826 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6827 {
6828   dw_die_ref c;
6829
6830   c = die->die_child;
6831   if (c) do {
6832     dw_die_ref prev = c;
6833     c = c->die_sib;
6834     while (c->die_tag == tag)
6835       {
6836         remove_child_with_prev (c, prev);
6837         /* Might have removed every child.  */
6838         if (c == c->die_sib)
6839           return;
6840         c = c->die_sib;
6841       }
6842   } while (c != die->die_child);
6843 }
6844
6845 /* Add a CHILD_DIE as the last child of DIE.  */
6846
6847 static void
6848 add_child_die (dw_die_ref die, dw_die_ref child_die)
6849 {
6850   /* FIXME this should probably be an assert.  */
6851   if (! die || ! child_die)
6852     return;
6853   gcc_assert (die != child_die);
6854
6855   child_die->die_parent = die;
6856   if (die->die_child)
6857     {
6858       child_die->die_sib = die->die_child->die_sib;
6859       die->die_child->die_sib = child_die;
6860     }
6861   else
6862     child_die->die_sib = child_die;
6863   die->die_child = child_die;
6864 }
6865
6866 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6867    is the specification, to the end of PARENT's list of children.
6868    This is done by removing and re-adding it.  */
6869
6870 static void
6871 splice_child_die (dw_die_ref parent, dw_die_ref child)
6872 {
6873   dw_die_ref p;
6874
6875   /* We want the declaration DIE from inside the class, not the
6876      specification DIE at toplevel.  */
6877   if (child->die_parent != parent)
6878     {
6879       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6880
6881       if (tmp)
6882         child = tmp;
6883     }
6884
6885   gcc_assert (child->die_parent == parent
6886               || (child->die_parent
6887                   == get_AT_ref (parent, DW_AT_specification)));
6888
6889   for (p = child->die_parent->die_child; ; p = p->die_sib)
6890     if (p->die_sib == child)
6891       {
6892         remove_child_with_prev (child, p);
6893         break;
6894       }
6895
6896   add_child_die (parent, child);
6897 }
6898
6899 /* Return a pointer to a newly created DIE node.  */
6900
6901 static inline dw_die_ref
6902 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6903 {
6904   dw_die_ref die = GGC_CNEW (die_node);
6905
6906   die->die_tag = tag_value;
6907
6908   if (parent_die != NULL)
6909     add_child_die (parent_die, die);
6910   else
6911     {
6912       limbo_die_node *limbo_node;
6913
6914       limbo_node = GGC_CNEW (limbo_die_node);
6915       limbo_node->die = die;
6916       limbo_node->created_for = t;
6917       limbo_node->next = limbo_die_list;
6918       limbo_die_list = limbo_node;
6919     }
6920
6921   return die;
6922 }
6923
6924 /* Return the DIE associated with the given type specifier.  */
6925
6926 static inline dw_die_ref
6927 lookup_type_die (tree type)
6928 {
6929   return TYPE_SYMTAB_DIE (type);
6930 }
6931
6932 /* Equate a DIE to a given type specifier.  */
6933
6934 static inline void
6935 equate_type_number_to_die (tree type, dw_die_ref type_die)
6936 {
6937   TYPE_SYMTAB_DIE (type) = type_die;
6938 }
6939
6940 /* Returns a hash value for X (which really is a die_struct).  */
6941
6942 static hashval_t
6943 decl_die_table_hash (const void *x)
6944 {
6945   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6946 }
6947
6948 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6949
6950 static int
6951 decl_die_table_eq (const void *x, const void *y)
6952 {
6953   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6954 }
6955
6956 /* Return the DIE associated with a given declaration.  */
6957
6958 static inline dw_die_ref
6959 lookup_decl_die (tree decl)
6960 {
6961   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6962 }
6963
6964 /* Returns a hash value for X (which really is a var_loc_list).  */
6965
6966 static hashval_t
6967 decl_loc_table_hash (const void *x)
6968 {
6969   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6970 }
6971
6972 /* Return nonzero if decl_id of var_loc_list X is the same as
6973    UID of decl *Y.  */
6974
6975 static int
6976 decl_loc_table_eq (const void *x, const void *y)
6977 {
6978   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6979 }
6980
6981 /* Return the var_loc list associated with a given declaration.  */
6982
6983 static inline var_loc_list *
6984 lookup_decl_loc (const_tree decl)
6985 {
6986   return (var_loc_list *)
6987     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6988 }
6989
6990 /* Equate a DIE to a particular declaration.  */
6991
6992 static void
6993 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6994 {
6995   unsigned int decl_id = DECL_UID (decl);
6996   void **slot;
6997
6998   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6999   *slot = decl_die;
7000   decl_die->decl_id = decl_id;
7001 }
7002
7003 /* Add a variable location node to the linked list for DECL.  */
7004
7005 static void
7006 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7007 {
7008   unsigned int decl_id = DECL_UID (decl);
7009   var_loc_list *temp;
7010   void **slot;
7011
7012   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7013   if (*slot == NULL)
7014     {
7015       temp = GGC_CNEW (var_loc_list);
7016       temp->decl_id = decl_id;
7017       *slot = temp;
7018     }
7019   else
7020     temp = (var_loc_list *) *slot;
7021
7022   if (temp->last)
7023     {
7024       /* If the current location is the same as the end of the list,
7025          and either both or neither of the locations is uninitialized,
7026          we have nothing to do.  */
7027       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7028                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7029           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7030                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7031               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7032                    == VAR_INIT_STATUS_UNINITIALIZED)
7033                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7034                       == VAR_INIT_STATUS_UNINITIALIZED))))
7035         {
7036           /* Add LOC to the end of list and update LAST.  */
7037           temp->last->next = loc;
7038           temp->last = loc;
7039         }
7040     }
7041   /* Do not add empty location to the beginning of the list.  */
7042   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7043     {
7044       temp->first = loc;
7045       temp->last = loc;
7046     }
7047 }
7048 \f
7049 /* Keep track of the number of spaces used to indent the
7050    output of the debugging routines that print the structure of
7051    the DIE internal representation.  */
7052 static int print_indent;
7053
7054 /* Indent the line the number of spaces given by print_indent.  */
7055
7056 static inline void
7057 print_spaces (FILE *outfile)
7058 {
7059   fprintf (outfile, "%*s", print_indent, "");
7060 }
7061
7062 /* Print the information associated with a given DIE, and its children.
7063    This routine is a debugging aid only.  */
7064
7065 static void
7066 print_die (dw_die_ref die, FILE *outfile)
7067 {
7068   dw_attr_ref a;
7069   dw_die_ref c;
7070   unsigned ix;
7071
7072   print_spaces (outfile);
7073   fprintf (outfile, "DIE %4ld: %s\n",
7074            die->die_offset, dwarf_tag_name (die->die_tag));
7075   print_spaces (outfile);
7076   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7077   fprintf (outfile, " offset: %ld\n", die->die_offset);
7078
7079   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7080     {
7081       print_spaces (outfile);
7082       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7083
7084       switch (AT_class (a))
7085         {
7086         case dw_val_class_addr:
7087           fprintf (outfile, "address");
7088           break;
7089         case dw_val_class_offset:
7090           fprintf (outfile, "offset");
7091           break;
7092         case dw_val_class_loc:
7093           fprintf (outfile, "location descriptor");
7094           break;
7095         case dw_val_class_loc_list:
7096           fprintf (outfile, "location list -> label:%s",
7097                    AT_loc_list (a)->ll_symbol);
7098           break;
7099         case dw_val_class_range_list:
7100           fprintf (outfile, "range list");
7101           break;
7102         case dw_val_class_const:
7103           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7104           break;
7105         case dw_val_class_unsigned_const:
7106           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7107           break;
7108         case dw_val_class_long_long:
7109           fprintf (outfile, "constant (%lu,%lu)",
7110                    a->dw_attr_val.v.val_long_long.hi,
7111                    a->dw_attr_val.v.val_long_long.low);
7112           break;
7113         case dw_val_class_vec:
7114           fprintf (outfile, "floating-point or vector constant");
7115           break;
7116         case dw_val_class_flag:
7117           fprintf (outfile, "%u", AT_flag (a));
7118           break;
7119         case dw_val_class_die_ref:
7120           if (AT_ref (a) != NULL)
7121             {
7122               if (AT_ref (a)->die_symbol)
7123                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7124               else
7125                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7126             }
7127           else
7128             fprintf (outfile, "die -> <null>");
7129           break;
7130         case dw_val_class_lbl_id:
7131         case dw_val_class_lineptr:
7132         case dw_val_class_macptr:
7133           fprintf (outfile, "label: %s", AT_lbl (a));
7134           break;
7135         case dw_val_class_str:
7136           if (AT_string (a) != NULL)
7137             fprintf (outfile, "\"%s\"", AT_string (a));
7138           else
7139             fprintf (outfile, "<null>");
7140           break;
7141         case dw_val_class_file:
7142           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7143                    AT_file (a)->emitted_number);
7144           break;
7145         default:
7146           break;
7147         }
7148
7149       fprintf (outfile, "\n");
7150     }
7151
7152   if (die->die_child != NULL)
7153     {
7154       print_indent += 4;
7155       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7156       print_indent -= 4;
7157     }
7158   if (print_indent == 0)
7159     fprintf (outfile, "\n");
7160 }
7161
7162 /* Print the contents of the source code line number correspondence table.
7163    This routine is a debugging aid only.  */
7164
7165 static void
7166 print_dwarf_line_table (FILE *outfile)
7167 {
7168   unsigned i;
7169   dw_line_info_ref line_info;
7170
7171   fprintf (outfile, "\n\nDWARF source line information\n");
7172   for (i = 1; i < line_info_table_in_use; i++)
7173     {
7174       line_info = &line_info_table[i];
7175       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7176                line_info->dw_file_num,
7177                line_info->dw_line_num);
7178     }
7179
7180   fprintf (outfile, "\n\n");
7181 }
7182
7183 /* Print the information collected for a given DIE.  */
7184
7185 void
7186 debug_dwarf_die (dw_die_ref die)
7187 {
7188   print_die (die, stderr);
7189 }
7190
7191 /* Print all DWARF information collected for the compilation unit.
7192    This routine is a debugging aid only.  */
7193
7194 void
7195 debug_dwarf (void)
7196 {
7197   print_indent = 0;
7198   print_die (comp_unit_die, stderr);
7199   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7200     print_dwarf_line_table (stderr);
7201 }
7202 \f
7203 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7204    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7205    DIE that marks the start of the DIEs for this include file.  */
7206
7207 static dw_die_ref
7208 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7209 {
7210   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7211   dw_die_ref new_unit = gen_compile_unit_die (filename);
7212
7213   new_unit->die_sib = old_unit;
7214   return new_unit;
7215 }
7216
7217 /* Close an include-file CU and reopen the enclosing one.  */
7218
7219 static dw_die_ref
7220 pop_compile_unit (dw_die_ref old_unit)
7221 {
7222   dw_die_ref new_unit = old_unit->die_sib;
7223
7224   old_unit->die_sib = NULL;
7225   return new_unit;
7226 }
7227
7228 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7229 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7230
7231 /* Calculate the checksum of a location expression.  */
7232
7233 static inline void
7234 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7235 {
7236   CHECKSUM (loc->dw_loc_opc);
7237   CHECKSUM (loc->dw_loc_oprnd1);
7238   CHECKSUM (loc->dw_loc_oprnd2);
7239 }
7240
7241 /* Calculate the checksum of an attribute.  */
7242
7243 static void
7244 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7245 {
7246   dw_loc_descr_ref loc;
7247   rtx r;
7248
7249   CHECKSUM (at->dw_attr);
7250
7251   /* We don't care that this was compiled with a different compiler
7252      snapshot; if the output is the same, that's what matters.  */
7253   if (at->dw_attr == DW_AT_producer)
7254     return;
7255
7256   switch (AT_class (at))
7257     {
7258     case dw_val_class_const:
7259       CHECKSUM (at->dw_attr_val.v.val_int);
7260       break;
7261     case dw_val_class_unsigned_const:
7262       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7263       break;
7264     case dw_val_class_long_long:
7265       CHECKSUM (at->dw_attr_val.v.val_long_long);
7266       break;
7267     case dw_val_class_vec:
7268       CHECKSUM (at->dw_attr_val.v.val_vec);
7269       break;
7270     case dw_val_class_flag:
7271       CHECKSUM (at->dw_attr_val.v.val_flag);
7272       break;
7273     case dw_val_class_str:
7274       CHECKSUM_STRING (AT_string (at));
7275       break;
7276
7277     case dw_val_class_addr:
7278       r = AT_addr (at);
7279       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7280       CHECKSUM_STRING (XSTR (r, 0));
7281       break;
7282
7283     case dw_val_class_offset:
7284       CHECKSUM (at->dw_attr_val.v.val_offset);
7285       break;
7286
7287     case dw_val_class_loc:
7288       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7289         loc_checksum (loc, ctx);
7290       break;
7291
7292     case dw_val_class_die_ref:
7293       die_checksum (AT_ref (at), ctx, mark);
7294       break;
7295
7296     case dw_val_class_fde_ref:
7297     case dw_val_class_lbl_id:
7298     case dw_val_class_lineptr:
7299     case dw_val_class_macptr:
7300       break;
7301
7302     case dw_val_class_file:
7303       CHECKSUM_STRING (AT_file (at)->filename);
7304       break;
7305
7306     default:
7307       break;
7308     }
7309 }
7310
7311 /* Calculate the checksum of a DIE.  */
7312
7313 static void
7314 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7315 {
7316   dw_die_ref c;
7317   dw_attr_ref a;
7318   unsigned ix;
7319
7320   /* To avoid infinite recursion.  */
7321   if (die->die_mark)
7322     {
7323       CHECKSUM (die->die_mark);
7324       return;
7325     }
7326   die->die_mark = ++(*mark);
7327
7328   CHECKSUM (die->die_tag);
7329
7330   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7331     attr_checksum (a, ctx, mark);
7332
7333   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7334 }
7335
7336 #undef CHECKSUM
7337 #undef CHECKSUM_STRING
7338
7339 /* Do the location expressions look same?  */
7340 static inline int
7341 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7342 {
7343   return loc1->dw_loc_opc == loc2->dw_loc_opc
7344          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7345          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7346 }
7347
7348 /* Do the values look the same?  */
7349 static int
7350 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7351 {
7352   dw_loc_descr_ref loc1, loc2;
7353   rtx r1, r2;
7354
7355   if (v1->val_class != v2->val_class)
7356     return 0;
7357
7358   switch (v1->val_class)
7359     {
7360     case dw_val_class_const:
7361       return v1->v.val_int == v2->v.val_int;
7362     case dw_val_class_unsigned_const:
7363       return v1->v.val_unsigned == v2->v.val_unsigned;
7364     case dw_val_class_long_long:
7365       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7366              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7367     case dw_val_class_vec:
7368       if (v1->v.val_vec.length != v2->v.val_vec.length
7369           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7370         return 0;
7371       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7372                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7373         return 0;
7374       return 1;
7375     case dw_val_class_flag:
7376       return v1->v.val_flag == v2->v.val_flag;
7377     case dw_val_class_str:
7378       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7379
7380     case dw_val_class_addr:
7381       r1 = v1->v.val_addr;
7382       r2 = v2->v.val_addr;
7383       if (GET_CODE (r1) != GET_CODE (r2))
7384         return 0;
7385       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7386       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7387
7388     case dw_val_class_offset:
7389       return v1->v.val_offset == v2->v.val_offset;
7390
7391     case dw_val_class_loc:
7392       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7393            loc1 && loc2;
7394            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7395         if (!same_loc_p (loc1, loc2, mark))
7396           return 0;
7397       return !loc1 && !loc2;
7398
7399     case dw_val_class_die_ref:
7400       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7401
7402     case dw_val_class_fde_ref:
7403     case dw_val_class_lbl_id:
7404     case dw_val_class_lineptr:
7405     case dw_val_class_macptr:
7406       return 1;
7407
7408     case dw_val_class_file:
7409       return v1->v.val_file == v2->v.val_file;
7410
7411     default:
7412       return 1;
7413     }
7414 }
7415
7416 /* Do the attributes look the same?  */
7417
7418 static int
7419 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7420 {
7421   if (at1->dw_attr != at2->dw_attr)
7422     return 0;
7423
7424   /* We don't care that this was compiled with a different compiler
7425      snapshot; if the output is the same, that's what matters. */
7426   if (at1->dw_attr == DW_AT_producer)
7427     return 1;
7428
7429   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7430 }
7431
7432 /* Do the dies look the same?  */
7433
7434 static int
7435 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7436 {
7437   dw_die_ref c1, c2;
7438   dw_attr_ref a1;
7439   unsigned ix;
7440
7441   /* To avoid infinite recursion.  */
7442   if (die1->die_mark)
7443     return die1->die_mark == die2->die_mark;
7444   die1->die_mark = die2->die_mark = ++(*mark);
7445
7446   if (die1->die_tag != die2->die_tag)
7447     return 0;
7448
7449   if (VEC_length (dw_attr_node, die1->die_attr)
7450       != VEC_length (dw_attr_node, die2->die_attr))
7451     return 0;
7452
7453   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7454     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7455       return 0;
7456
7457   c1 = die1->die_child;
7458   c2 = die2->die_child;
7459   if (! c1)
7460     {
7461       if (c2)
7462         return 0;
7463     }
7464   else
7465     for (;;)
7466       {
7467         if (!same_die_p (c1, c2, mark))
7468           return 0;
7469         c1 = c1->die_sib;
7470         c2 = c2->die_sib;
7471         if (c1 == die1->die_child)
7472           {
7473             if (c2 == die2->die_child)
7474               break;
7475             else
7476               return 0;
7477           }
7478     }
7479
7480   return 1;
7481 }
7482
7483 /* Do the dies look the same?  Wrapper around same_die_p.  */
7484
7485 static int
7486 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7487 {
7488   int mark = 0;
7489   int ret = same_die_p (die1, die2, &mark);
7490
7491   unmark_all_dies (die1);
7492   unmark_all_dies (die2);
7493
7494   return ret;
7495 }
7496
7497 /* The prefix to attach to symbols on DIEs in the current comdat debug
7498    info section.  */
7499 static char *comdat_symbol_id;
7500
7501 /* The index of the current symbol within the current comdat CU.  */
7502 static unsigned int comdat_symbol_number;
7503
7504 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7505    children, and set comdat_symbol_id accordingly.  */
7506
7507 static void
7508 compute_section_prefix (dw_die_ref unit_die)
7509 {
7510   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7511   const char *base = die_name ? lbasename (die_name) : "anonymous";
7512   char *name = XALLOCAVEC (char, strlen (base) + 64);
7513   char *p;
7514   int i, mark;
7515   unsigned char checksum[16];
7516   struct md5_ctx ctx;
7517
7518   /* Compute the checksum of the DIE, then append part of it as hex digits to
7519      the name filename of the unit.  */
7520
7521   md5_init_ctx (&ctx);
7522   mark = 0;
7523   die_checksum (unit_die, &ctx, &mark);
7524   unmark_all_dies (unit_die);
7525   md5_finish_ctx (&ctx, checksum);
7526
7527   sprintf (name, "%s.", base);
7528   clean_symbol_name (name);
7529
7530   p = name + strlen (name);
7531   for (i = 0; i < 4; i++)
7532     {
7533       sprintf (p, "%.2x", checksum[i]);
7534       p += 2;
7535     }
7536
7537   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7538   comdat_symbol_number = 0;
7539 }
7540
7541 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7542
7543 static int
7544 is_type_die (dw_die_ref die)
7545 {
7546   switch (die->die_tag)
7547     {
7548     case DW_TAG_array_type:
7549     case DW_TAG_class_type:
7550     case DW_TAG_interface_type:
7551     case DW_TAG_enumeration_type:
7552     case DW_TAG_pointer_type:
7553     case DW_TAG_reference_type:
7554     case DW_TAG_string_type:
7555     case DW_TAG_structure_type:
7556     case DW_TAG_subroutine_type:
7557     case DW_TAG_union_type:
7558     case DW_TAG_ptr_to_member_type:
7559     case DW_TAG_set_type:
7560     case DW_TAG_subrange_type:
7561     case DW_TAG_base_type:
7562     case DW_TAG_const_type:
7563     case DW_TAG_file_type:
7564     case DW_TAG_packed_type:
7565     case DW_TAG_volatile_type:
7566     case DW_TAG_typedef:
7567       return 1;
7568     default:
7569       return 0;
7570     }
7571 }
7572
7573 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7574    Basically, we want to choose the bits that are likely to be shared between
7575    compilations (types) and leave out the bits that are specific to individual
7576    compilations (functions).  */
7577
7578 static int
7579 is_comdat_die (dw_die_ref c)
7580 {
7581   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7582      we do for stabs.  The advantage is a greater likelihood of sharing between
7583      objects that don't include headers in the same order (and therefore would
7584      put the base types in a different comdat).  jason 8/28/00 */
7585
7586   if (c->die_tag == DW_TAG_base_type)
7587     return 0;
7588
7589   if (c->die_tag == DW_TAG_pointer_type
7590       || c->die_tag == DW_TAG_reference_type
7591       || c->die_tag == DW_TAG_const_type
7592       || c->die_tag == DW_TAG_volatile_type)
7593     {
7594       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7595
7596       return t ? is_comdat_die (t) : 0;
7597     }
7598
7599   return is_type_die (c);
7600 }
7601
7602 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7603    compilation unit.  */
7604
7605 static int
7606 is_symbol_die (dw_die_ref c)
7607 {
7608   return (is_type_die (c)
7609           || (get_AT (c, DW_AT_declaration)
7610               && !get_AT (c, DW_AT_specification))
7611           || c->die_tag == DW_TAG_namespace
7612           || c->die_tag == DW_TAG_module);
7613 }
7614
7615 static char *
7616 gen_internal_sym (const char *prefix)
7617 {
7618   char buf[256];
7619
7620   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7621   return xstrdup (buf);
7622 }
7623
7624 /* Assign symbols to all worthy DIEs under DIE.  */
7625
7626 static void
7627 assign_symbol_names (dw_die_ref die)
7628 {
7629   dw_die_ref c;
7630
7631   if (is_symbol_die (die))
7632     {
7633       if (comdat_symbol_id)
7634         {
7635           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7636
7637           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7638                    comdat_symbol_id, comdat_symbol_number++);
7639           die->die_symbol = xstrdup (p);
7640         }
7641       else
7642         die->die_symbol = gen_internal_sym ("LDIE");
7643     }
7644
7645   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7646 }
7647
7648 struct cu_hash_table_entry
7649 {
7650   dw_die_ref cu;
7651   unsigned min_comdat_num, max_comdat_num;
7652   struct cu_hash_table_entry *next;
7653 };
7654
7655 /* Routines to manipulate hash table of CUs.  */
7656 static hashval_t
7657 htab_cu_hash (const void *of)
7658 {
7659   const struct cu_hash_table_entry *const entry =
7660     (const struct cu_hash_table_entry *) of;
7661
7662   return htab_hash_string (entry->cu->die_symbol);
7663 }
7664
7665 static int
7666 htab_cu_eq (const void *of1, const void *of2)
7667 {
7668   const struct cu_hash_table_entry *const entry1 =
7669     (const struct cu_hash_table_entry *) of1;
7670   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7671
7672   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7673 }
7674
7675 static void
7676 htab_cu_del (void *what)
7677 {
7678   struct cu_hash_table_entry *next,
7679     *entry = (struct cu_hash_table_entry *) what;
7680
7681   while (entry)
7682     {
7683       next = entry->next;
7684       free (entry);
7685       entry = next;
7686     }
7687 }
7688
7689 /* Check whether we have already seen this CU and set up SYM_NUM
7690    accordingly.  */
7691 static int
7692 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7693 {
7694   struct cu_hash_table_entry dummy;
7695   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7696
7697   dummy.max_comdat_num = 0;
7698
7699   slot = (struct cu_hash_table_entry **)
7700     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7701         INSERT);
7702   entry = *slot;
7703
7704   for (; entry; last = entry, entry = entry->next)
7705     {
7706       if (same_die_p_wrap (cu, entry->cu))
7707         break;
7708     }
7709
7710   if (entry)
7711     {
7712       *sym_num = entry->min_comdat_num;
7713       return 1;
7714     }
7715
7716   entry = XCNEW (struct cu_hash_table_entry);
7717   entry->cu = cu;
7718   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7719   entry->next = *slot;
7720   *slot = entry;
7721
7722   return 0;
7723 }
7724
7725 /* Record SYM_NUM to record of CU in HTABLE.  */
7726 static void
7727 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7728 {
7729   struct cu_hash_table_entry **slot, *entry;
7730
7731   slot = (struct cu_hash_table_entry **)
7732     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7733         NO_INSERT);
7734   entry = *slot;
7735
7736   entry->max_comdat_num = sym_num;
7737 }
7738
7739 /* Traverse the DIE (which is always comp_unit_die), and set up
7740    additional compilation units for each of the include files we see
7741    bracketed by BINCL/EINCL.  */
7742
7743 static void
7744 break_out_includes (dw_die_ref die)
7745 {
7746   dw_die_ref c;
7747   dw_die_ref unit = NULL;
7748   limbo_die_node *node, **pnode;
7749   htab_t cu_hash_table;
7750
7751   c = die->die_child;
7752   if (c) do {
7753     dw_die_ref prev = c;
7754     c = c->die_sib;
7755     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7756            || (unit && is_comdat_die (c)))
7757       {
7758         dw_die_ref next = c->die_sib;
7759
7760         /* This DIE is for a secondary CU; remove it from the main one.  */
7761         remove_child_with_prev (c, prev);
7762
7763         if (c->die_tag == DW_TAG_GNU_BINCL)
7764           unit = push_new_compile_unit (unit, c);
7765         else if (c->die_tag == DW_TAG_GNU_EINCL)
7766           unit = pop_compile_unit (unit);
7767         else
7768           add_child_die (unit, c);
7769         c = next;
7770         if (c == die->die_child)
7771           break;
7772       }
7773   } while (c != die->die_child);
7774
7775 #if 0
7776   /* We can only use this in debugging, since the frontend doesn't check
7777      to make sure that we leave every include file we enter.  */
7778   gcc_assert (!unit);
7779 #endif
7780
7781   assign_symbol_names (die);
7782   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7783   for (node = limbo_die_list, pnode = &limbo_die_list;
7784        node;
7785        node = node->next)
7786     {
7787       int is_dupl;
7788
7789       compute_section_prefix (node->die);
7790       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7791                         &comdat_symbol_number);
7792       assign_symbol_names (node->die);
7793       if (is_dupl)
7794         *pnode = node->next;
7795       else
7796         {
7797           pnode = &node->next;
7798           record_comdat_symbol_number (node->die, cu_hash_table,
7799                 comdat_symbol_number);
7800         }
7801     }
7802   htab_delete (cu_hash_table);
7803 }
7804
7805 /* Traverse the DIE and add a sibling attribute if it may have the
7806    effect of speeding up access to siblings.  To save some space,
7807    avoid generating sibling attributes for DIE's without children.  */
7808
7809 static void
7810 add_sibling_attributes (dw_die_ref die)
7811 {
7812   dw_die_ref c;
7813
7814   if (! die->die_child)
7815     return;
7816
7817   if (die->die_parent && die != die->die_parent->die_child)
7818     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7819
7820   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7821 }
7822
7823 /* Output all location lists for the DIE and its children.  */
7824
7825 static void
7826 output_location_lists (dw_die_ref die)
7827 {
7828   dw_die_ref c;
7829   dw_attr_ref a;
7830   unsigned ix;
7831
7832   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7833     if (AT_class (a) == dw_val_class_loc_list)
7834       output_loc_list (AT_loc_list (a));
7835
7836   FOR_EACH_CHILD (die, c, output_location_lists (c));
7837 }
7838
7839 /* The format of each DIE (and its attribute value pairs) is encoded in an
7840    abbreviation table.  This routine builds the abbreviation table and assigns
7841    a unique abbreviation id for each abbreviation entry.  The children of each
7842    die are visited recursively.  */
7843
7844 static void
7845 build_abbrev_table (dw_die_ref die)
7846 {
7847   unsigned long abbrev_id;
7848   unsigned int n_alloc;
7849   dw_die_ref c;
7850   dw_attr_ref a;
7851   unsigned ix;
7852
7853   /* Scan the DIE references, and mark as external any that refer to
7854      DIEs from other CUs (i.e. those which are not marked).  */
7855   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7856     if (AT_class (a) == dw_val_class_die_ref
7857         && AT_ref (a)->die_mark == 0)
7858       {
7859         gcc_assert (AT_ref (a)->die_symbol);
7860
7861         set_AT_ref_external (a, 1);
7862       }
7863
7864   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7865     {
7866       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7867       dw_attr_ref die_a, abbrev_a;
7868       unsigned ix;
7869       bool ok = true;
7870
7871       if (abbrev->die_tag != die->die_tag)
7872         continue;
7873       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7874         continue;
7875
7876       if (VEC_length (dw_attr_node, abbrev->die_attr)
7877           != VEC_length (dw_attr_node, die->die_attr))
7878         continue;
7879
7880       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7881         {
7882           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7883           if ((abbrev_a->dw_attr != die_a->dw_attr)
7884               || (value_format (abbrev_a) != value_format (die_a)))
7885             {
7886               ok = false;
7887               break;
7888             }
7889         }
7890       if (ok)
7891         break;
7892     }
7893
7894   if (abbrev_id >= abbrev_die_table_in_use)
7895     {
7896       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7897         {
7898           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7899           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7900                                             n_alloc);
7901
7902           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7903                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7904           abbrev_die_table_allocated = n_alloc;
7905         }
7906
7907       ++abbrev_die_table_in_use;
7908       abbrev_die_table[abbrev_id] = die;
7909     }
7910
7911   die->die_abbrev = abbrev_id;
7912   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7913 }
7914 \f
7915 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7916
7917 static int
7918 constant_size (unsigned HOST_WIDE_INT value)
7919 {
7920   int log;
7921
7922   if (value == 0)
7923     log = 0;
7924   else
7925     log = floor_log2 (value);
7926
7927   log = log / 8;
7928   log = 1 << (floor_log2 (log) + 1);
7929
7930   return log;
7931 }
7932
7933 /* Return the size of a DIE as it is represented in the
7934    .debug_info section.  */
7935
7936 static unsigned long
7937 size_of_die (dw_die_ref die)
7938 {
7939   unsigned long size = 0;
7940   dw_attr_ref a;
7941   unsigned ix;
7942
7943   size += size_of_uleb128 (die->die_abbrev);
7944   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7945     {
7946       switch (AT_class (a))
7947         {
7948         case dw_val_class_addr:
7949           size += DWARF2_ADDR_SIZE;
7950           break;
7951         case dw_val_class_offset:
7952           size += DWARF_OFFSET_SIZE;
7953           break;
7954         case dw_val_class_loc:
7955           {
7956             unsigned long lsize = size_of_locs (AT_loc (a));
7957
7958             /* Block length.  */
7959             size += constant_size (lsize);
7960             size += lsize;
7961           }
7962           break;
7963         case dw_val_class_loc_list:
7964           size += DWARF_OFFSET_SIZE;
7965           break;
7966         case dw_val_class_range_list:
7967           size += DWARF_OFFSET_SIZE;
7968           break;
7969         case dw_val_class_const:
7970           size += size_of_sleb128 (AT_int (a));
7971           break;
7972         case dw_val_class_unsigned_const:
7973           size += constant_size (AT_unsigned (a));
7974           break;
7975         case dw_val_class_long_long:
7976           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7977           break;
7978         case dw_val_class_vec:
7979           size += constant_size (a->dw_attr_val.v.val_vec.length
7980                                  * a->dw_attr_val.v.val_vec.elt_size)
7981                   + a->dw_attr_val.v.val_vec.length
7982                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7983           break;
7984         case dw_val_class_flag:
7985           size += 1;
7986           break;
7987         case dw_val_class_die_ref:
7988           if (AT_ref_external (a))
7989             size += DWARF2_ADDR_SIZE;
7990           else
7991             size += DWARF_OFFSET_SIZE;
7992           break;
7993         case dw_val_class_fde_ref:
7994           size += DWARF_OFFSET_SIZE;
7995           break;
7996         case dw_val_class_lbl_id:
7997           size += DWARF2_ADDR_SIZE;
7998           break;
7999         case dw_val_class_lineptr:
8000         case dw_val_class_macptr:
8001           size += DWARF_OFFSET_SIZE;
8002           break;
8003         case dw_val_class_str:
8004           if (AT_string_form (a) == DW_FORM_strp)
8005             size += DWARF_OFFSET_SIZE;
8006           else
8007             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8008           break;
8009         case dw_val_class_file:
8010           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8011           break;
8012         default:
8013           gcc_unreachable ();
8014         }
8015     }
8016
8017   return size;
8018 }
8019
8020 /* Size the debugging information associated with a given DIE.  Visits the
8021    DIE's children recursively.  Updates the global variable next_die_offset, on
8022    each time through.  Uses the current value of next_die_offset to update the
8023    die_offset field in each DIE.  */
8024
8025 static void
8026 calc_die_sizes (dw_die_ref die)
8027 {
8028   dw_die_ref c;
8029
8030   die->die_offset = next_die_offset;
8031   next_die_offset += size_of_die (die);
8032
8033   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8034
8035   if (die->die_child != NULL)
8036     /* Count the null byte used to terminate sibling lists.  */
8037     next_die_offset += 1;
8038 }
8039
8040 /* Set the marks for a die and its children.  We do this so
8041    that we know whether or not a reference needs to use FORM_ref_addr; only
8042    DIEs in the same CU will be marked.  We used to clear out the offset
8043    and use that as the flag, but ran into ordering problems.  */
8044
8045 static void
8046 mark_dies (dw_die_ref die)
8047 {
8048   dw_die_ref c;
8049
8050   gcc_assert (!die->die_mark);
8051
8052   die->die_mark = 1;
8053   FOR_EACH_CHILD (die, c, mark_dies (c));
8054 }
8055
8056 /* Clear the marks for a die and its children.  */
8057
8058 static void
8059 unmark_dies (dw_die_ref die)
8060 {
8061   dw_die_ref c;
8062
8063   gcc_assert (die->die_mark);
8064
8065   die->die_mark = 0;
8066   FOR_EACH_CHILD (die, c, unmark_dies (c));
8067 }
8068
8069 /* Clear the marks for a die, its children and referred dies.  */
8070
8071 static void
8072 unmark_all_dies (dw_die_ref die)
8073 {
8074   dw_die_ref c;
8075   dw_attr_ref a;
8076   unsigned ix;
8077
8078   if (!die->die_mark)
8079     return;
8080   die->die_mark = 0;
8081
8082   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8083
8084   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8085     if (AT_class (a) == dw_val_class_die_ref)
8086       unmark_all_dies (AT_ref (a));
8087 }
8088
8089 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8090    generated for the compilation unit.  */
8091
8092 static unsigned long
8093 size_of_pubnames (VEC (pubname_entry, gc) * names)
8094 {
8095   unsigned long size;
8096   unsigned i;
8097   pubname_ref p;
8098
8099   size = DWARF_PUBNAMES_HEADER_SIZE;
8100   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8101     if (names != pubtype_table
8102         || p->die->die_offset != 0
8103         || !flag_eliminate_unused_debug_types)
8104       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8105
8106   size += DWARF_OFFSET_SIZE;
8107   return size;
8108 }
8109
8110 /* Return the size of the information in the .debug_aranges section.  */
8111
8112 static unsigned long
8113 size_of_aranges (void)
8114 {
8115   unsigned long size;
8116
8117   size = DWARF_ARANGES_HEADER_SIZE;
8118
8119   /* Count the address/length pair for this compilation unit.  */
8120   if (text_section_used)
8121     size += 2 * DWARF2_ADDR_SIZE;
8122   if (cold_text_section_used)
8123     size += 2 * DWARF2_ADDR_SIZE;
8124   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8125
8126   /* Count the two zero words used to terminated the address range table.  */
8127   size += 2 * DWARF2_ADDR_SIZE;
8128   return size;
8129 }
8130 \f
8131 /* Select the encoding of an attribute value.  */
8132
8133 static enum dwarf_form
8134 value_format (dw_attr_ref a)
8135 {
8136   switch (a->dw_attr_val.val_class)
8137     {
8138     case dw_val_class_addr:
8139       return DW_FORM_addr;
8140     case dw_val_class_range_list:
8141     case dw_val_class_offset:
8142     case dw_val_class_loc_list:
8143       switch (DWARF_OFFSET_SIZE)
8144         {
8145         case 4:
8146           return DW_FORM_data4;
8147         case 8:
8148           return DW_FORM_data8;
8149         default:
8150           gcc_unreachable ();
8151         }
8152     case dw_val_class_loc:
8153       switch (constant_size (size_of_locs (AT_loc (a))))
8154         {
8155         case 1:
8156           return DW_FORM_block1;
8157         case 2:
8158           return DW_FORM_block2;
8159         default:
8160           gcc_unreachable ();
8161         }
8162     case dw_val_class_const:
8163       return DW_FORM_sdata;
8164     case dw_val_class_unsigned_const:
8165       switch (constant_size (AT_unsigned (a)))
8166         {
8167         case 1:
8168           return DW_FORM_data1;
8169         case 2:
8170           return DW_FORM_data2;
8171         case 4:
8172           return DW_FORM_data4;
8173         case 8:
8174           return DW_FORM_data8;
8175         default:
8176           gcc_unreachable ();
8177         }
8178     case dw_val_class_long_long:
8179       return DW_FORM_block1;
8180     case dw_val_class_vec:
8181       switch (constant_size (a->dw_attr_val.v.val_vec.length
8182                              * a->dw_attr_val.v.val_vec.elt_size))
8183         {
8184         case 1:
8185           return DW_FORM_block1;
8186         case 2:
8187           return DW_FORM_block2;
8188         case 4:
8189           return DW_FORM_block4;
8190         default:
8191           gcc_unreachable ();
8192         }
8193     case dw_val_class_flag:
8194       return DW_FORM_flag;
8195     case dw_val_class_die_ref:
8196       if (AT_ref_external (a))
8197         return DW_FORM_ref_addr;
8198       else
8199         return DW_FORM_ref;
8200     case dw_val_class_fde_ref:
8201       return DW_FORM_data;
8202     case dw_val_class_lbl_id:
8203       return DW_FORM_addr;
8204     case dw_val_class_lineptr:
8205     case dw_val_class_macptr:
8206       return DW_FORM_data;
8207     case dw_val_class_str:
8208       return AT_string_form (a);
8209     case dw_val_class_file:
8210       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8211         {
8212         case 1:
8213           return DW_FORM_data1;
8214         case 2:
8215           return DW_FORM_data2;
8216         case 4:
8217           return DW_FORM_data4;
8218         default:
8219           gcc_unreachable ();
8220         }
8221
8222     default:
8223       gcc_unreachable ();
8224     }
8225 }
8226
8227 /* Output the encoding of an attribute value.  */
8228
8229 static void
8230 output_value_format (dw_attr_ref a)
8231 {
8232   enum dwarf_form form = value_format (a);
8233
8234   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8235 }
8236
8237 /* Output the .debug_abbrev section which defines the DIE abbreviation
8238    table.  */
8239
8240 static void
8241 output_abbrev_section (void)
8242 {
8243   unsigned long abbrev_id;
8244
8245   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8246     {
8247       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8248       unsigned ix;
8249       dw_attr_ref a_attr;
8250
8251       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8252       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8253                                    dwarf_tag_name (abbrev->die_tag));
8254
8255       if (abbrev->die_child != NULL)
8256         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8257       else
8258         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8259
8260       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8261            ix++)
8262         {
8263           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8264                                        dwarf_attr_name (a_attr->dw_attr));
8265           output_value_format (a_attr);
8266         }
8267
8268       dw2_asm_output_data (1, 0, NULL);
8269       dw2_asm_output_data (1, 0, NULL);
8270     }
8271
8272   /* Terminate the table.  */
8273   dw2_asm_output_data (1, 0, NULL);
8274 }
8275
8276 /* Output a symbol we can use to refer to this DIE from another CU.  */
8277
8278 static inline void
8279 output_die_symbol (dw_die_ref die)
8280 {
8281   char *sym = die->die_symbol;
8282
8283   if (sym == 0)
8284     return;
8285
8286   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8287     /* We make these global, not weak; if the target doesn't support
8288        .linkonce, it doesn't support combining the sections, so debugging
8289        will break.  */
8290     targetm.asm_out.globalize_label (asm_out_file, sym);
8291
8292   ASM_OUTPUT_LABEL (asm_out_file, sym);
8293 }
8294
8295 /* Return a new location list, given the begin and end range, and the
8296    expression. gensym tells us whether to generate a new internal symbol for
8297    this location list node, which is done for the head of the list only.  */
8298
8299 static inline dw_loc_list_ref
8300 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8301               const char *section, unsigned int gensym)
8302 {
8303   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8304
8305   retlist->begin = begin;
8306   retlist->end = end;
8307   retlist->expr = expr;
8308   retlist->section = section;
8309   if (gensym)
8310     retlist->ll_symbol = gen_internal_sym ("LLST");
8311
8312   return retlist;
8313 }
8314
8315 /* Add a location description expression to a location list.  */
8316
8317 static inline void
8318 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8319                            const char *begin, const char *end,
8320                            const char *section)
8321 {
8322   dw_loc_list_ref *d;
8323
8324   /* Find the end of the chain.  */
8325   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8326     ;
8327
8328   /* Add a new location list node to the list.  */
8329   *d = new_loc_list (descr, begin, end, section, 0);
8330 }
8331
8332 /* Output the location list given to us.  */
8333
8334 static void
8335 output_loc_list (dw_loc_list_ref list_head)
8336 {
8337   dw_loc_list_ref curr = list_head;
8338
8339   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8340
8341   /* Walk the location list, and output each range + expression.  */
8342   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8343     {
8344       unsigned long size;
8345       /* Don't output an entry that starts and ends at the same address.  */
8346       if (strcmp (curr->begin, curr->end) == 0)
8347         continue;
8348       if (!have_multiple_function_sections)
8349         {
8350           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8351                                 "Location list begin address (%s)",
8352                                 list_head->ll_symbol);
8353           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8354                                 "Location list end address (%s)",
8355                                 list_head->ll_symbol);
8356         }
8357       else
8358         {
8359           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8360                                "Location list begin address (%s)",
8361                                list_head->ll_symbol);
8362           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8363                                "Location list end address (%s)",
8364                                list_head->ll_symbol);
8365         }
8366       size = size_of_locs (curr->expr);
8367
8368       /* Output the block length for this list of location operations.  */
8369       gcc_assert (size <= 0xffff);
8370       dw2_asm_output_data (2, size, "%s", "Location expression size");
8371
8372       output_loc_sequence (curr->expr);
8373     }
8374
8375   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8376                        "Location list terminator begin (%s)",
8377                        list_head->ll_symbol);
8378   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8379                        "Location list terminator end (%s)",
8380                        list_head->ll_symbol);
8381 }
8382
8383 /* Output the DIE and its attributes.  Called recursively to generate
8384    the definitions of each child DIE.  */
8385
8386 static void
8387 output_die (dw_die_ref die)
8388 {
8389   dw_attr_ref a;
8390   dw_die_ref c;
8391   unsigned long size;
8392   unsigned ix;
8393
8394   /* If someone in another CU might refer to us, set up a symbol for
8395      them to point to.  */
8396   if (die->die_symbol)
8397     output_die_symbol (die);
8398
8399   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8400                                (unsigned long)die->die_offset,
8401                                dwarf_tag_name (die->die_tag));
8402
8403   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8404     {
8405       const char *name = dwarf_attr_name (a->dw_attr);
8406
8407       switch (AT_class (a))
8408         {
8409         case dw_val_class_addr:
8410           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8411           break;
8412
8413         case dw_val_class_offset:
8414           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8415                                "%s", name);
8416           break;
8417
8418         case dw_val_class_range_list:
8419           {
8420             char *p = strchr (ranges_section_label, '\0');
8421
8422             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8423                      a->dw_attr_val.v.val_offset);
8424             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8425                                    debug_ranges_section, "%s", name);
8426             *p = '\0';
8427           }
8428           break;
8429
8430         case dw_val_class_loc:
8431           size = size_of_locs (AT_loc (a));
8432
8433           /* Output the block length for this list of location operations.  */
8434           dw2_asm_output_data (constant_size (size), size, "%s", name);
8435
8436           output_loc_sequence (AT_loc (a));
8437           break;
8438
8439         case dw_val_class_const:
8440           /* ??? It would be slightly more efficient to use a scheme like is
8441              used for unsigned constants below, but gdb 4.x does not sign
8442              extend.  Gdb 5.x does sign extend.  */
8443           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8444           break;
8445
8446         case dw_val_class_unsigned_const:
8447           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8448                                AT_unsigned (a), "%s", name);
8449           break;
8450
8451         case dw_val_class_long_long:
8452           {
8453             unsigned HOST_WIDE_INT first, second;
8454
8455             dw2_asm_output_data (1,
8456                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8457                                  "%s", name);
8458
8459             if (WORDS_BIG_ENDIAN)
8460               {
8461                 first = a->dw_attr_val.v.val_long_long.hi;
8462                 second = a->dw_attr_val.v.val_long_long.low;
8463               }
8464             else
8465               {
8466                 first = a->dw_attr_val.v.val_long_long.low;
8467                 second = a->dw_attr_val.v.val_long_long.hi;
8468               }
8469
8470             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8471                                  first, "long long constant");
8472             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8473                                  second, NULL);
8474           }
8475           break;
8476
8477         case dw_val_class_vec:
8478           {
8479             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8480             unsigned int len = a->dw_attr_val.v.val_vec.length;
8481             unsigned int i;
8482             unsigned char *p;
8483
8484             dw2_asm_output_data (constant_size (len * elt_size),
8485                                  len * elt_size, "%s", name);
8486             if (elt_size > sizeof (HOST_WIDE_INT))
8487               {
8488                 elt_size /= 2;
8489                 len *= 2;
8490               }
8491             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8492                  i < len;
8493                  i++, p += elt_size)
8494               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8495                                    "fp or vector constant word %u", i);
8496             break;
8497           }
8498
8499         case dw_val_class_flag:
8500           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8501           break;
8502
8503         case dw_val_class_loc_list:
8504           {
8505             char *sym = AT_loc_list (a)->ll_symbol;
8506
8507             gcc_assert (sym);
8508             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8509                                    "%s", name);
8510           }
8511           break;
8512
8513         case dw_val_class_die_ref:
8514           if (AT_ref_external (a))
8515             {
8516               char *sym = AT_ref (a)->die_symbol;
8517
8518               gcc_assert (sym);
8519               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8520                                      "%s", name);
8521             }
8522           else
8523             {
8524               gcc_assert (AT_ref (a)->die_offset);
8525               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8526                                    "%s", name);
8527             }
8528           break;
8529
8530         case dw_val_class_fde_ref:
8531           {
8532             char l1[20];
8533
8534             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8535                                          a->dw_attr_val.v.val_fde_index * 2);
8536             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8537                                    "%s", name);
8538           }
8539           break;
8540
8541         case dw_val_class_lbl_id:
8542           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8543           break;
8544
8545         case dw_val_class_lineptr:
8546           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8547                                  debug_line_section, "%s", name);
8548           break;
8549
8550         case dw_val_class_macptr:
8551           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8552                                  debug_macinfo_section, "%s", name);
8553           break;
8554
8555         case dw_val_class_str:
8556           if (AT_string_form (a) == DW_FORM_strp)
8557             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8558                                    a->dw_attr_val.v.val_str->label,
8559                                    debug_str_section,
8560                                    "%s: \"%s\"", name, AT_string (a));
8561           else
8562             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8563           break;
8564
8565         case dw_val_class_file:
8566           {
8567             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8568
8569             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8570                                  a->dw_attr_val.v.val_file->filename);
8571             break;
8572           }
8573
8574         default:
8575           gcc_unreachable ();
8576         }
8577     }
8578
8579   FOR_EACH_CHILD (die, c, output_die (c));
8580
8581   /* Add null byte to terminate sibling list.  */
8582   if (die->die_child != NULL)
8583     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8584                          (unsigned long) die->die_offset);
8585 }
8586
8587 /* Output the compilation unit that appears at the beginning of the
8588    .debug_info section, and precedes the DIE descriptions.  */
8589
8590 static void
8591 output_compilation_unit_header (void)
8592 {
8593   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8594     dw2_asm_output_data (4, 0xffffffff,
8595       "Initial length escape value indicating 64-bit DWARF extension");
8596   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8597                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8598                        "Length of Compilation Unit Info");
8599   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8600   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8601                          debug_abbrev_section,
8602                          "Offset Into Abbrev. Section");
8603   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8604 }
8605
8606 /* Output the compilation unit DIE and its children.  */
8607
8608 static void
8609 output_comp_unit (dw_die_ref die, int output_if_empty)
8610 {
8611   const char *secname;
8612   char *oldsym, *tmp;
8613
8614   /* Unless we are outputting main CU, we may throw away empty ones.  */
8615   if (!output_if_empty && die->die_child == NULL)
8616     return;
8617
8618   /* Even if there are no children of this DIE, we must output the information
8619      about the compilation unit.  Otherwise, on an empty translation unit, we
8620      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8621      will then complain when examining the file.  First mark all the DIEs in
8622      this CU so we know which get local refs.  */
8623   mark_dies (die);
8624
8625   build_abbrev_table (die);
8626
8627   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8628   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8629   calc_die_sizes (die);
8630
8631   oldsym = die->die_symbol;
8632   if (oldsym)
8633     {
8634       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8635
8636       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8637       secname = tmp;
8638       die->die_symbol = NULL;
8639       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8640     }
8641   else
8642     switch_to_section (debug_info_section);
8643
8644   /* Output debugging information.  */
8645   output_compilation_unit_header ();
8646   output_die (die);
8647
8648   /* Leave the marks on the main CU, so we can check them in
8649      output_pubnames.  */
8650   if (oldsym)
8651     {
8652       unmark_dies (die);
8653       die->die_symbol = oldsym;
8654     }
8655 }
8656
8657 /* Return the DWARF2/3 pubname associated with a decl.  */
8658
8659 static const char *
8660 dwarf2_name (tree decl, int scope)
8661 {
8662   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8663 }
8664
8665 /* Add a new entry to .debug_pubnames if appropriate.  */
8666
8667 static void
8668 add_pubname_string (const char *str, dw_die_ref die)
8669 {
8670   pubname_entry e;
8671
8672   e.die = die;
8673   e.name = xstrdup (str);
8674   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8675 }
8676
8677 static void
8678 add_pubname (tree decl, dw_die_ref die)
8679 {
8680
8681   if (TREE_PUBLIC (decl))
8682     add_pubname_string (dwarf2_name (decl, 1), die);
8683 }
8684
8685 /* Add a new entry to .debug_pubtypes if appropriate.  */
8686
8687 static void
8688 add_pubtype (tree decl, dw_die_ref die)
8689 {
8690   pubname_entry e;
8691
8692   e.name = NULL;
8693   if ((TREE_PUBLIC (decl)
8694        || die->die_parent == comp_unit_die)
8695       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8696     {
8697       e.die = die;
8698       if (TYPE_P (decl))
8699         {
8700           if (TYPE_NAME (decl))
8701             {
8702               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8703                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8704               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8705                        && DECL_NAME (TYPE_NAME (decl)))
8706                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8707               else
8708                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8709             }
8710         }
8711       else
8712         e.name = xstrdup (dwarf2_name (decl, 1));
8713
8714       /* If we don't have a name for the type, there's no point in adding
8715          it to the table.  */
8716       if (e.name && e.name[0] != '\0')
8717         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8718     }
8719 }
8720
8721 /* Output the public names table used to speed up access to externally
8722    visible names; or the public types table used to find type definitions.  */
8723
8724 static void
8725 output_pubnames (VEC (pubname_entry, gc) * names)
8726 {
8727   unsigned i;
8728   unsigned long pubnames_length = size_of_pubnames (names);
8729   pubname_ref pub;
8730
8731   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8732     dw2_asm_output_data (4, 0xffffffff,
8733       "Initial length escape value indicating 64-bit DWARF extension");
8734   if (names == pubname_table)
8735     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8736                          "Length of Public Names Info");
8737   else
8738     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8739                          "Length of Public Type Names Info");
8740   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8741   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8742                          debug_info_section,
8743                          "Offset of Compilation Unit Info");
8744   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8745                        "Compilation Unit Length");
8746
8747   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8748     {
8749       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8750       if (names == pubname_table)
8751         gcc_assert (pub->die->die_mark);
8752
8753       if (names != pubtype_table
8754           || pub->die->die_offset != 0
8755           || !flag_eliminate_unused_debug_types)
8756         {
8757           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8758                                "DIE offset");
8759
8760           dw2_asm_output_nstring (pub->name, -1, "external name");
8761         }
8762     }
8763
8764   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8765 }
8766
8767 /* Add a new entry to .debug_aranges if appropriate.  */
8768
8769 static void
8770 add_arange (tree decl, dw_die_ref die)
8771 {
8772   if (! DECL_SECTION_NAME (decl))
8773     return;
8774
8775   if (arange_table_in_use == arange_table_allocated)
8776     {
8777       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8778       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8779                                     arange_table_allocated);
8780       memset (arange_table + arange_table_in_use, 0,
8781               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8782     }
8783
8784   arange_table[arange_table_in_use++] = die;
8785 }
8786
8787 /* Output the information that goes into the .debug_aranges table.
8788    Namely, define the beginning and ending address range of the
8789    text section generated for this compilation unit.  */
8790
8791 static void
8792 output_aranges (void)
8793 {
8794   unsigned i;
8795   unsigned long aranges_length = size_of_aranges ();
8796
8797   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8798     dw2_asm_output_data (4, 0xffffffff,
8799       "Initial length escape value indicating 64-bit DWARF extension");
8800   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8801                        "Length of Address Ranges Info");
8802   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8803   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8804                          debug_info_section,
8805                          "Offset of Compilation Unit Info");
8806   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8807   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8808
8809   /* We need to align to twice the pointer size here.  */
8810   if (DWARF_ARANGES_PAD_SIZE)
8811     {
8812       /* Pad using a 2 byte words so that padding is correct for any
8813          pointer size.  */
8814       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8815                            2 * DWARF2_ADDR_SIZE);
8816       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8817         dw2_asm_output_data (2, 0, NULL);
8818     }
8819
8820   /* It is necessary not to output these entries if the sections were
8821      not used; if the sections were not used, the length will be 0 and
8822      the address may end up as 0 if the section is discarded by ld
8823      --gc-sections, leaving an invalid (0, 0) entry that can be
8824      confused with the terminator.  */
8825   if (text_section_used)
8826     {
8827       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8828       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8829                             text_section_label, "Length");
8830     }
8831   if (cold_text_section_used)
8832     {
8833       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8834                            "Address");
8835       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8836                             cold_text_section_label, "Length");
8837     }
8838
8839   for (i = 0; i < arange_table_in_use; i++)
8840     {
8841       dw_die_ref die = arange_table[i];
8842
8843       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8844       gcc_assert (die->die_mark);
8845
8846       if (die->die_tag == DW_TAG_subprogram)
8847         {
8848           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8849                                "Address");
8850           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8851                                 get_AT_low_pc (die), "Length");
8852         }
8853       else
8854         {
8855           /* A static variable; extract the symbol from DW_AT_location.
8856              Note that this code isn't currently hit, as we only emit
8857              aranges for functions (jason 9/23/99).  */
8858           dw_attr_ref a = get_AT (die, DW_AT_location);
8859           dw_loc_descr_ref loc;
8860
8861           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8862
8863           loc = AT_loc (a);
8864           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8865
8866           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8867                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8868           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8869                                get_AT_unsigned (die, DW_AT_byte_size),
8870                                "Length");
8871         }
8872     }
8873
8874   /* Output the terminator words.  */
8875   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8876   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8877 }
8878
8879 /* Add a new entry to .debug_ranges.  Return the offset at which it
8880    was placed.  */
8881
8882 static unsigned int
8883 add_ranges_num (int num)
8884 {
8885   unsigned int in_use = ranges_table_in_use;
8886
8887   if (in_use == ranges_table_allocated)
8888     {
8889       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8890       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8891                                     ranges_table_allocated);
8892       memset (ranges_table + ranges_table_in_use, 0,
8893               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8894     }
8895
8896   ranges_table[in_use].num = num;
8897   ranges_table_in_use = in_use + 1;
8898
8899   return in_use * 2 * DWARF2_ADDR_SIZE;
8900 }
8901
8902 /* Add a new entry to .debug_ranges corresponding to a block, or a
8903    range terminator if BLOCK is NULL.  */
8904
8905 static unsigned int
8906 add_ranges (const_tree block)
8907 {
8908   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8909 }
8910
8911 /* Add a new entry to .debug_ranges corresponding to a pair of
8912    labels.  */
8913
8914 static unsigned int
8915 add_ranges_by_labels (const char *begin, const char *end)
8916 {
8917   unsigned int in_use = ranges_by_label_in_use;
8918
8919   if (in_use == ranges_by_label_allocated)
8920     {
8921       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8922       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8923                                        ranges_by_label,
8924                                        ranges_by_label_allocated);
8925       memset (ranges_by_label + ranges_by_label_in_use, 0,
8926               RANGES_TABLE_INCREMENT
8927               * sizeof (struct dw_ranges_by_label_struct));
8928     }
8929
8930   ranges_by_label[in_use].begin = begin;
8931   ranges_by_label[in_use].end = end;
8932   ranges_by_label_in_use = in_use + 1;
8933
8934   return add_ranges_num (-(int)in_use - 1);
8935 }
8936
8937 static void
8938 output_ranges (void)
8939 {
8940   unsigned i;
8941   static const char *const start_fmt = "Offset 0x%x";
8942   const char *fmt = start_fmt;
8943
8944   for (i = 0; i < ranges_table_in_use; i++)
8945     {
8946       int block_num = ranges_table[i].num;
8947
8948       if (block_num > 0)
8949         {
8950           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8951           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8952
8953           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8954           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8955
8956           /* If all code is in the text section, then the compilation
8957              unit base address defaults to DW_AT_low_pc, which is the
8958              base of the text section.  */
8959           if (!have_multiple_function_sections)
8960             {
8961               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8962                                     text_section_label,
8963                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8964               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8965                                     text_section_label, NULL);
8966             }
8967
8968           /* Otherwise, the compilation unit base address is zero,
8969              which allows us to use absolute addresses, and not worry
8970              about whether the target supports cross-section
8971              arithmetic.  */
8972           else
8973             {
8974               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8975                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8976               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8977             }
8978
8979           fmt = NULL;
8980         }
8981
8982       /* Negative block_num stands for an index into ranges_by_label.  */
8983       else if (block_num < 0)
8984         {
8985           int lab_idx = - block_num - 1;
8986
8987           if (!have_multiple_function_sections)
8988             {
8989               gcc_unreachable ();
8990 #if 0
8991               /* If we ever use add_ranges_by_labels () for a single
8992                  function section, all we have to do is to take out
8993                  the #if 0 above.  */
8994               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8995                                     ranges_by_label[lab_idx].begin,
8996                                     text_section_label,
8997                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8998               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8999                                     ranges_by_label[lab_idx].end,
9000                                     text_section_label, NULL);
9001 #endif
9002             }
9003           else
9004             {
9005               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9006                                    ranges_by_label[lab_idx].begin,
9007                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9008               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9009                                    ranges_by_label[lab_idx].end,
9010                                    NULL);
9011             }
9012         }
9013       else
9014         {
9015           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9016           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9017           fmt = start_fmt;
9018         }
9019     }
9020 }
9021
9022 /* Data structure containing information about input files.  */
9023 struct file_info
9024 {
9025   const char *path;     /* Complete file name.  */
9026   const char *fname;    /* File name part.  */
9027   int length;           /* Length of entire string.  */
9028   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9029   int dir_idx;          /* Index in directory table.  */
9030 };
9031
9032 /* Data structure containing information about directories with source
9033    files.  */
9034 struct dir_info
9035 {
9036   const char *path;     /* Path including directory name.  */
9037   int length;           /* Path length.  */
9038   int prefix;           /* Index of directory entry which is a prefix.  */
9039   int count;            /* Number of files in this directory.  */
9040   int dir_idx;          /* Index of directory used as base.  */
9041 };
9042
9043 /* Callback function for file_info comparison.  We sort by looking at
9044    the directories in the path.  */
9045
9046 static int
9047 file_info_cmp (const void *p1, const void *p2)
9048 {
9049   const struct file_info *const s1 = (const struct file_info *) p1;
9050   const struct file_info *const s2 = (const struct file_info *) p2;
9051   const unsigned char *cp1;
9052   const unsigned char *cp2;
9053
9054   /* Take care of file names without directories.  We need to make sure that
9055      we return consistent values to qsort since some will get confused if
9056      we return the same value when identical operands are passed in opposite
9057      orders.  So if neither has a directory, return 0 and otherwise return
9058      1 or -1 depending on which one has the directory.  */
9059   if ((s1->path == s1->fname || s2->path == s2->fname))
9060     return (s2->path == s2->fname) - (s1->path == s1->fname);
9061
9062   cp1 = (const unsigned char *) s1->path;
9063   cp2 = (const unsigned char *) s2->path;
9064
9065   while (1)
9066     {
9067       ++cp1;
9068       ++cp2;
9069       /* Reached the end of the first path?  If so, handle like above.  */
9070       if ((cp1 == (const unsigned char *) s1->fname)
9071           || (cp2 == (const unsigned char *) s2->fname))
9072         return ((cp2 == (const unsigned char *) s2->fname)
9073                 - (cp1 == (const unsigned char *) s1->fname));
9074
9075       /* Character of current path component the same?  */
9076       else if (*cp1 != *cp2)
9077         return *cp1 - *cp2;
9078     }
9079 }
9080
9081 struct file_name_acquire_data
9082 {
9083   struct file_info *files;
9084   int used_files;
9085   int max_files;
9086 };
9087
9088 /* Traversal function for the hash table.  */
9089
9090 static int
9091 file_name_acquire (void ** slot, void *data)
9092 {
9093   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9094   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9095   struct file_info *fi;
9096   const char *f;
9097
9098   gcc_assert (fnad->max_files >= d->emitted_number);
9099
9100   if (! d->emitted_number)
9101     return 1;
9102
9103   gcc_assert (fnad->max_files != fnad->used_files);
9104
9105   fi = fnad->files + fnad->used_files++;
9106
9107   /* Skip all leading "./".  */
9108   f = d->filename;
9109   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9110     f += 2;
9111
9112   /* Create a new array entry.  */
9113   fi->path = f;
9114   fi->length = strlen (f);
9115   fi->file_idx = d;
9116
9117   /* Search for the file name part.  */
9118   f = strrchr (f, DIR_SEPARATOR);
9119 #if defined (DIR_SEPARATOR_2)
9120   {
9121     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9122
9123     if (g != NULL)
9124       {
9125         if (f == NULL || f < g)
9126           f = g;
9127       }
9128   }
9129 #endif
9130
9131   fi->fname = f == NULL ? fi->path : f + 1;
9132   return 1;
9133 }
9134
9135 /* Output the directory table and the file name table.  We try to minimize
9136    the total amount of memory needed.  A heuristic is used to avoid large
9137    slowdowns with many input files.  */
9138
9139 static void
9140 output_file_names (void)
9141 {
9142   struct file_name_acquire_data fnad;
9143   int numfiles;
9144   struct file_info *files;
9145   struct dir_info *dirs;
9146   int *saved;
9147   int *savehere;
9148   int *backmap;
9149   int ndirs;
9150   int idx_offset;
9151   int i;
9152   int idx;
9153
9154   if (!last_emitted_file)
9155     {
9156       dw2_asm_output_data (1, 0, "End directory table");
9157       dw2_asm_output_data (1, 0, "End file name table");
9158       return;
9159     }
9160
9161   numfiles = last_emitted_file->emitted_number;
9162
9163   /* Allocate the various arrays we need.  */
9164   files = XALLOCAVEC (struct file_info, numfiles);
9165   dirs = XALLOCAVEC (struct dir_info, numfiles);
9166
9167   fnad.files = files;
9168   fnad.used_files = 0;
9169   fnad.max_files = numfiles;
9170   htab_traverse (file_table, file_name_acquire, &fnad);
9171   gcc_assert (fnad.used_files == fnad.max_files);
9172
9173   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9174
9175   /* Find all the different directories used.  */
9176   dirs[0].path = files[0].path;
9177   dirs[0].length = files[0].fname - files[0].path;
9178   dirs[0].prefix = -1;
9179   dirs[0].count = 1;
9180   dirs[0].dir_idx = 0;
9181   files[0].dir_idx = 0;
9182   ndirs = 1;
9183
9184   for (i = 1; i < numfiles; i++)
9185     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9186         && memcmp (dirs[ndirs - 1].path, files[i].path,
9187                    dirs[ndirs - 1].length) == 0)
9188       {
9189         /* Same directory as last entry.  */
9190         files[i].dir_idx = ndirs - 1;
9191         ++dirs[ndirs - 1].count;
9192       }
9193     else
9194       {
9195         int j;
9196
9197         /* This is a new directory.  */
9198         dirs[ndirs].path = files[i].path;
9199         dirs[ndirs].length = files[i].fname - files[i].path;
9200         dirs[ndirs].count = 1;
9201         dirs[ndirs].dir_idx = ndirs;
9202         files[i].dir_idx = ndirs;
9203
9204         /* Search for a prefix.  */
9205         dirs[ndirs].prefix = -1;
9206         for (j = 0; j < ndirs; j++)
9207           if (dirs[j].length < dirs[ndirs].length
9208               && dirs[j].length > 1
9209               && (dirs[ndirs].prefix == -1
9210                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9211               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9212             dirs[ndirs].prefix = j;
9213
9214         ++ndirs;
9215       }
9216
9217   /* Now to the actual work.  We have to find a subset of the directories which
9218      allow expressing the file name using references to the directory table
9219      with the least amount of characters.  We do not do an exhaustive search
9220      where we would have to check out every combination of every single
9221      possible prefix.  Instead we use a heuristic which provides nearly optimal
9222      results in most cases and never is much off.  */
9223   saved = XALLOCAVEC (int, ndirs);
9224   savehere = XALLOCAVEC (int, ndirs);
9225
9226   memset (saved, '\0', ndirs * sizeof (saved[0]));
9227   for (i = 0; i < ndirs; i++)
9228     {
9229       int j;
9230       int total;
9231
9232       /* We can always save some space for the current directory.  But this
9233          does not mean it will be enough to justify adding the directory.  */
9234       savehere[i] = dirs[i].length;
9235       total = (savehere[i] - saved[i]) * dirs[i].count;
9236
9237       for (j = i + 1; j < ndirs; j++)
9238         {
9239           savehere[j] = 0;
9240           if (saved[j] < dirs[i].length)
9241             {
9242               /* Determine whether the dirs[i] path is a prefix of the
9243                  dirs[j] path.  */
9244               int k;
9245
9246               k = dirs[j].prefix;
9247               while (k != -1 && k != (int) i)
9248                 k = dirs[k].prefix;
9249
9250               if (k == (int) i)
9251                 {
9252                   /* Yes it is.  We can possibly save some memory by
9253                      writing the filenames in dirs[j] relative to
9254                      dirs[i].  */
9255                   savehere[j] = dirs[i].length;
9256                   total += (savehere[j] - saved[j]) * dirs[j].count;
9257                 }
9258             }
9259         }
9260
9261       /* Check whether we can save enough to justify adding the dirs[i]
9262          directory.  */
9263       if (total > dirs[i].length + 1)
9264         {
9265           /* It's worthwhile adding.  */
9266           for (j = i; j < ndirs; j++)
9267             if (savehere[j] > 0)
9268               {
9269                 /* Remember how much we saved for this directory so far.  */
9270                 saved[j] = savehere[j];
9271
9272                 /* Remember the prefix directory.  */
9273                 dirs[j].dir_idx = i;
9274               }
9275         }
9276     }
9277
9278   /* Emit the directory name table.  */
9279   idx = 1;
9280   idx_offset = dirs[0].length > 0 ? 1 : 0;
9281   for (i = 1 - idx_offset; i < ndirs; i++)
9282     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
9283                             "Directory Entry: 0x%x", i + idx_offset);
9284
9285   dw2_asm_output_data (1, 0, "End directory table");
9286
9287   /* We have to emit them in the order of emitted_number since that's
9288      used in the debug info generation.  To do this efficiently we
9289      generate a back-mapping of the indices first.  */
9290   backmap = XALLOCAVEC (int, numfiles);
9291   for (i = 0; i < numfiles; i++)
9292     backmap[files[i].file_idx->emitted_number - 1] = i;
9293
9294   /* Now write all the file names.  */
9295   for (i = 0; i < numfiles; i++)
9296     {
9297       int file_idx = backmap[i];
9298       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9299
9300       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9301                               "File Entry: 0x%x", (unsigned) i + 1);
9302
9303       /* Include directory index.  */
9304       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9305
9306       /* Modification time.  */
9307       dw2_asm_output_data_uleb128 (0, NULL);
9308
9309       /* File length in bytes.  */
9310       dw2_asm_output_data_uleb128 (0, NULL);
9311     }
9312
9313   dw2_asm_output_data (1, 0, "End file name table");
9314 }
9315
9316
9317 /* Output the source line number correspondence information.  This
9318    information goes into the .debug_line section.  */
9319
9320 static void
9321 output_line_info (void)
9322 {
9323   char l1[20], l2[20], p1[20], p2[20];
9324   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9325   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9326   unsigned opc;
9327   unsigned n_op_args;
9328   unsigned long lt_index;
9329   unsigned long current_line;
9330   long line_offset;
9331   long line_delta;
9332   unsigned long current_file;
9333   unsigned long function;
9334
9335   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9336   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9337   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9338   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9339
9340   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9341     dw2_asm_output_data (4, 0xffffffff,
9342       "Initial length escape value indicating 64-bit DWARF extension");
9343   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9344                         "Length of Source Line Info");
9345   ASM_OUTPUT_LABEL (asm_out_file, l1);
9346
9347   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
9348   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9349   ASM_OUTPUT_LABEL (asm_out_file, p1);
9350
9351   /* Define the architecture-dependent minimum instruction length (in
9352    bytes).  In this implementation of DWARF, this field is used for
9353    information purposes only.  Since GCC generates assembly language,
9354    we have no a priori knowledge of how many instruction bytes are
9355    generated for each source line, and therefore can use only the
9356    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9357    commands.  Accordingly, we fix this as `1', which is "correct
9358    enough" for all architectures, and don't let the target override.  */
9359   dw2_asm_output_data (1, 1,
9360                        "Minimum Instruction Length");
9361
9362   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9363                        "Default is_stmt_start flag");
9364   dw2_asm_output_data (1, DWARF_LINE_BASE,
9365                        "Line Base Value (Special Opcodes)");
9366   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9367                        "Line Range Value (Special Opcodes)");
9368   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9369                        "Special Opcode Base");
9370
9371   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9372     {
9373       switch (opc)
9374         {
9375         case DW_LNS_advance_pc:
9376         case DW_LNS_advance_line:
9377         case DW_LNS_set_file:
9378         case DW_LNS_set_column:
9379         case DW_LNS_fixed_advance_pc:
9380           n_op_args = 1;
9381           break;
9382         default:
9383           n_op_args = 0;
9384           break;
9385         }
9386
9387       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9388                            opc, n_op_args);
9389     }
9390
9391   /* Write out the information about the files we use.  */
9392   output_file_names ();
9393   ASM_OUTPUT_LABEL (asm_out_file, p2);
9394
9395   /* We used to set the address register to the first location in the text
9396      section here, but that didn't accomplish anything since we already
9397      have a line note for the opening brace of the first function.  */
9398
9399   /* Generate the line number to PC correspondence table, encoded as
9400      a series of state machine operations.  */
9401   current_file = 1;
9402   current_line = 1;
9403
9404   if (cfun && in_cold_section_p)
9405     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9406   else
9407     strcpy (prev_line_label, text_section_label);
9408   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9409     {
9410       dw_line_info_ref line_info = &line_info_table[lt_index];
9411
9412 #if 0
9413       /* Disable this optimization for now; GDB wants to see two line notes
9414          at the beginning of a function so it can find the end of the
9415          prologue.  */
9416
9417       /* Don't emit anything for redundant notes.  Just updating the
9418          address doesn't accomplish anything, because we already assume
9419          that anything after the last address is this line.  */
9420       if (line_info->dw_line_num == current_line
9421           && line_info->dw_file_num == current_file)
9422         continue;
9423 #endif
9424
9425       /* Emit debug info for the address of the current line.
9426
9427          Unfortunately, we have little choice here currently, and must always
9428          use the most general form.  GCC does not know the address delta
9429          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9430          attributes which will give an upper bound on the address range.  We
9431          could perhaps use length attributes to determine when it is safe to
9432          use DW_LNS_fixed_advance_pc.  */
9433
9434       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9435       if (0)
9436         {
9437           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9438           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9439                                "DW_LNS_fixed_advance_pc");
9440           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9441         }
9442       else
9443         {
9444           /* This can handle any delta.  This takes
9445              4+DWARF2_ADDR_SIZE bytes.  */
9446           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9447           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9448           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9449           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9450         }
9451
9452       strcpy (prev_line_label, line_label);
9453
9454       /* Emit debug info for the source file of the current line, if
9455          different from the previous line.  */
9456       if (line_info->dw_file_num != current_file)
9457         {
9458           current_file = line_info->dw_file_num;
9459           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9460           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9461         }
9462
9463       /* Emit debug info for the current line number, choosing the encoding
9464          that uses the least amount of space.  */
9465       if (line_info->dw_line_num != current_line)
9466         {
9467           line_offset = line_info->dw_line_num - current_line;
9468           line_delta = line_offset - DWARF_LINE_BASE;
9469           current_line = line_info->dw_line_num;
9470           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9471             /* This can handle deltas from -10 to 234, using the current
9472                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9473                takes 1 byte.  */
9474             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9475                                  "line %lu", current_line);
9476           else
9477             {
9478               /* This can handle any delta.  This takes at least 4 bytes,
9479                  depending on the value being encoded.  */
9480               dw2_asm_output_data (1, DW_LNS_advance_line,
9481                                    "advance to line %lu", current_line);
9482               dw2_asm_output_data_sleb128 (line_offset, NULL);
9483               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9484             }
9485         }
9486       else
9487         /* We still need to start a new row, so output a copy insn.  */
9488         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9489     }
9490
9491   /* Emit debug info for the address of the end of the function.  */
9492   if (0)
9493     {
9494       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9495                            "DW_LNS_fixed_advance_pc");
9496       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9497     }
9498   else
9499     {
9500       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9501       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9502       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9503       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9504     }
9505
9506   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9507   dw2_asm_output_data_uleb128 (1, NULL);
9508   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9509
9510   function = 0;
9511   current_file = 1;
9512   current_line = 1;
9513   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9514     {
9515       dw_separate_line_info_ref line_info
9516         = &separate_line_info_table[lt_index];
9517
9518 #if 0
9519       /* Don't emit anything for redundant notes.  */
9520       if (line_info->dw_line_num == current_line
9521           && line_info->dw_file_num == current_file
9522           && line_info->function == function)
9523         goto cont;
9524 #endif
9525
9526       /* Emit debug info for the address of the current line.  If this is
9527          a new function, or the first line of a function, then we need
9528          to handle it differently.  */
9529       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9530                                    lt_index);
9531       if (function != line_info->function)
9532         {
9533           function = line_info->function;
9534
9535           /* Set the address register to the first line in the function.  */
9536           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9537           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9538           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9539           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9540         }
9541       else
9542         {
9543           /* ??? See the DW_LNS_advance_pc comment above.  */
9544           if (0)
9545             {
9546               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9547                                    "DW_LNS_fixed_advance_pc");
9548               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9549             }
9550           else
9551             {
9552               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9553               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9554               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9555               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9556             }
9557         }
9558
9559       strcpy (prev_line_label, line_label);
9560
9561       /* Emit debug info for the source file of the current line, if
9562          different from the previous line.  */
9563       if (line_info->dw_file_num != current_file)
9564         {
9565           current_file = line_info->dw_file_num;
9566           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9567           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9568         }
9569
9570       /* Emit debug info for the current line number, choosing the encoding
9571          that uses the least amount of space.  */
9572       if (line_info->dw_line_num != current_line)
9573         {
9574           line_offset = line_info->dw_line_num - current_line;
9575           line_delta = line_offset - DWARF_LINE_BASE;
9576           current_line = line_info->dw_line_num;
9577           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9578             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9579                                  "line %lu", current_line);
9580           else
9581             {
9582               dw2_asm_output_data (1, DW_LNS_advance_line,
9583                                    "advance to line %lu", current_line);
9584               dw2_asm_output_data_sleb128 (line_offset, NULL);
9585               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9586             }
9587         }
9588       else
9589         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9590
9591 #if 0
9592     cont:
9593 #endif
9594
9595       lt_index++;
9596
9597       /* If we're done with a function, end its sequence.  */
9598       if (lt_index == separate_line_info_table_in_use
9599           || separate_line_info_table[lt_index].function != function)
9600         {
9601           current_file = 1;
9602           current_line = 1;
9603
9604           /* Emit debug info for the address of the end of the function.  */
9605           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9606           if (0)
9607             {
9608               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9609                                    "DW_LNS_fixed_advance_pc");
9610               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9611             }
9612           else
9613             {
9614               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9615               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9616               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9617               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9618             }
9619
9620           /* Output the marker for the end of this sequence.  */
9621           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9622           dw2_asm_output_data_uleb128 (1, NULL);
9623           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9624         }
9625     }
9626
9627   /* Output the marker for the end of the line number info.  */
9628   ASM_OUTPUT_LABEL (asm_out_file, l2);
9629 }
9630 \f
9631 /* Given a pointer to a tree node for some base type, return a pointer to
9632    a DIE that describes the given type.
9633
9634    This routine must only be called for GCC type nodes that correspond to
9635    Dwarf base (fundamental) types.  */
9636
9637 static dw_die_ref
9638 base_type_die (tree type)
9639 {
9640   dw_die_ref base_type_result;
9641   enum dwarf_type encoding;
9642
9643   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9644     return 0;
9645
9646   /* If this is a subtype that should not be emitted as a subrange type,
9647      use the base type.  See subrange_type_for_debug_p.  */
9648   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9649     type = TREE_TYPE (type);
9650
9651   switch (TREE_CODE (type))
9652     {
9653     case INTEGER_TYPE:
9654       if (TYPE_STRING_FLAG (type))
9655         {
9656           if (TYPE_UNSIGNED (type))
9657             encoding = DW_ATE_unsigned_char;
9658           else
9659             encoding = DW_ATE_signed_char;
9660         }
9661       else if (TYPE_UNSIGNED (type))
9662         encoding = DW_ATE_unsigned;
9663       else
9664         encoding = DW_ATE_signed;
9665       break;
9666
9667     case REAL_TYPE:
9668       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9669         encoding = DW_ATE_decimal_float;
9670       else
9671         encoding = DW_ATE_float;
9672       break;
9673
9674     case FIXED_POINT_TYPE:
9675       if (TYPE_UNSIGNED (type))
9676         encoding = DW_ATE_unsigned_fixed;
9677       else
9678         encoding = DW_ATE_signed_fixed;
9679       break;
9680
9681       /* Dwarf2 doesn't know anything about complex ints, so use
9682          a user defined type for it.  */
9683     case COMPLEX_TYPE:
9684       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9685         encoding = DW_ATE_complex_float;
9686       else
9687         encoding = DW_ATE_lo_user;
9688       break;
9689
9690     case BOOLEAN_TYPE:
9691       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9692       encoding = DW_ATE_boolean;
9693       break;
9694
9695     default:
9696       /* No other TREE_CODEs are Dwarf fundamental types.  */
9697       gcc_unreachable ();
9698     }
9699
9700   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9701
9702   /* This probably indicates a bug.  */
9703   if (! TYPE_NAME (type))
9704     add_name_attribute (base_type_result, "__unknown__");
9705
9706   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9707                    int_size_in_bytes (type));
9708   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9709
9710   return base_type_result;
9711 }
9712
9713 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9714    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9715
9716 static inline int
9717 is_base_type (tree type)
9718 {
9719   switch (TREE_CODE (type))
9720     {
9721     case ERROR_MARK:
9722     case VOID_TYPE:
9723     case INTEGER_TYPE:
9724     case REAL_TYPE:
9725     case FIXED_POINT_TYPE:
9726     case COMPLEX_TYPE:
9727     case BOOLEAN_TYPE:
9728       return 1;
9729
9730     case ARRAY_TYPE:
9731     case RECORD_TYPE:
9732     case UNION_TYPE:
9733     case QUAL_UNION_TYPE:
9734     case ENUMERAL_TYPE:
9735     case FUNCTION_TYPE:
9736     case METHOD_TYPE:
9737     case POINTER_TYPE:
9738     case REFERENCE_TYPE:
9739     case OFFSET_TYPE:
9740     case LANG_TYPE:
9741     case VECTOR_TYPE:
9742       return 0;
9743
9744     default:
9745       gcc_unreachable ();
9746     }
9747
9748   return 0;
9749 }
9750
9751 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9752    node, return the size in bits for the type if it is a constant, or else
9753    return the alignment for the type if the type's size is not constant, or
9754    else return BITS_PER_WORD if the type actually turns out to be an
9755    ERROR_MARK node.  */
9756
9757 static inline unsigned HOST_WIDE_INT
9758 simple_type_size_in_bits (const_tree type)
9759 {
9760   if (TREE_CODE (type) == ERROR_MARK)
9761     return BITS_PER_WORD;
9762   else if (TYPE_SIZE (type) == NULL_TREE)
9763     return 0;
9764   else if (host_integerp (TYPE_SIZE (type), 1))
9765     return tree_low_cst (TYPE_SIZE (type), 1);
9766   else
9767     return TYPE_ALIGN (type);
9768 }
9769
9770 /*  Given a pointer to a tree node for a subrange type, return a pointer
9771     to a DIE that describes the given type.  */
9772
9773 static dw_die_ref
9774 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9775 {
9776   dw_die_ref subrange_die;
9777   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9778
9779   if (context_die == NULL)
9780     context_die = comp_unit_die;
9781
9782   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9783
9784   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9785     {
9786       /* The size of the subrange type and its base type do not match,
9787          so we need to generate a size attribute for the subrange type.  */
9788       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9789     }
9790
9791   if (low)
9792     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9793   if (high)
9794     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9795
9796   return subrange_die;
9797 }
9798
9799 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9800    entry that chains various modifiers in front of the given type.  */
9801
9802 static dw_die_ref
9803 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9804                    dw_die_ref context_die)
9805 {
9806   enum tree_code code = TREE_CODE (type);
9807   dw_die_ref mod_type_die;
9808   dw_die_ref sub_die = NULL;
9809   tree item_type = NULL;
9810   tree qualified_type;
9811   tree name, low, high;
9812
9813   if (code == ERROR_MARK)
9814     return NULL;
9815
9816   /* See if we already have the appropriately qualified variant of
9817      this type.  */
9818   qualified_type
9819     = get_qualified_type (type,
9820                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9821                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9822
9823   /* If we do, then we can just use its DIE, if it exists.  */
9824   if (qualified_type)
9825     {
9826       mod_type_die = lookup_type_die (qualified_type);
9827       if (mod_type_die)
9828         return mod_type_die;
9829     }
9830
9831   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9832
9833   /* Handle C typedef types.  */
9834   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9835     {
9836       tree dtype = TREE_TYPE (name);
9837
9838       if (qualified_type == dtype)
9839         {
9840           /* For a named type, use the typedef.  */
9841           gen_type_die (qualified_type, context_die);
9842           return lookup_type_die (qualified_type);
9843         }
9844       else if (is_const_type < TYPE_READONLY (dtype)
9845                || is_volatile_type < TYPE_VOLATILE (dtype)
9846                || (is_const_type <= TYPE_READONLY (dtype)
9847                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9848                    && DECL_ORIGINAL_TYPE (name) != type))
9849         /* cv-unqualified version of named type.  Just use the unnamed
9850            type to which it refers.  */
9851         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9852                                   is_const_type, is_volatile_type,
9853                                   context_die);
9854       /* Else cv-qualified version of named type; fall through.  */
9855     }
9856
9857   if (is_const_type)
9858     {
9859       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9860       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9861     }
9862   else if (is_volatile_type)
9863     {
9864       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9865       sub_die = modified_type_die (type, 0, 0, context_die);
9866     }
9867   else if (code == POINTER_TYPE)
9868     {
9869       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9870       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9871                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9872       item_type = TREE_TYPE (type);
9873     }
9874   else if (code == REFERENCE_TYPE)
9875     {
9876       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9877       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9878                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9879       item_type = TREE_TYPE (type);
9880     }
9881   else if (code == INTEGER_TYPE
9882            && TREE_TYPE (type) != NULL_TREE
9883            && subrange_type_for_debug_p (type, &low, &high))
9884     {
9885       mod_type_die = subrange_type_die (type, low, high, context_die);
9886       item_type = TREE_TYPE (type);
9887     }
9888   else if (is_base_type (type))
9889     mod_type_die = base_type_die (type);
9890   else
9891     {
9892       gen_type_die (type, context_die);
9893
9894       /* We have to get the type_main_variant here (and pass that to the
9895          `lookup_type_die' routine) because the ..._TYPE node we have
9896          might simply be a *copy* of some original type node (where the
9897          copy was created to help us keep track of typedef names) and
9898          that copy might have a different TYPE_UID from the original
9899          ..._TYPE node.  */
9900       if (TREE_CODE (type) != VECTOR_TYPE)
9901         return lookup_type_die (type_main_variant (type));
9902       else
9903         /* Vectors have the debugging information in the type,
9904            not the main variant.  */
9905         return lookup_type_die (type);
9906     }
9907
9908   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9909      don't output a DW_TAG_typedef, since there isn't one in the
9910      user's program; just attach a DW_AT_name to the type.  */
9911   if (name
9912       && (TREE_CODE (name) != TYPE_DECL
9913           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9914     {
9915       if (TREE_CODE (name) == TYPE_DECL)
9916         /* Could just call add_name_and_src_coords_attributes here,
9917            but since this is a builtin type it doesn't have any
9918            useful source coordinates anyway.  */
9919         name = DECL_NAME (name);
9920       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9921     }
9922
9923   if (qualified_type)
9924     equate_type_number_to_die (qualified_type, mod_type_die);
9925
9926   if (item_type)
9927     /* We must do this after the equate_type_number_to_die call, in case
9928        this is a recursive type.  This ensures that the modified_type_die
9929        recursion will terminate even if the type is recursive.  Recursive
9930        types are possible in Ada.  */
9931     sub_die = modified_type_die (item_type,
9932                                  TYPE_READONLY (item_type),
9933                                  TYPE_VOLATILE (item_type),
9934                                  context_die);
9935
9936   if (sub_die != NULL)
9937     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9938
9939   return mod_type_die;
9940 }
9941
9942 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9943    an enumerated type.  */
9944
9945 static inline int
9946 type_is_enum (const_tree type)
9947 {
9948   return TREE_CODE (type) == ENUMERAL_TYPE;
9949 }
9950
9951 /* Return the DBX register number described by a given RTL node.  */
9952
9953 static unsigned int
9954 dbx_reg_number (const_rtx rtl)
9955 {
9956   unsigned regno = REGNO (rtl);
9957
9958   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9959
9960 #ifdef LEAF_REG_REMAP
9961   if (current_function_uses_only_leaf_regs)
9962     {
9963       int leaf_reg = LEAF_REG_REMAP (regno);
9964       if (leaf_reg != -1)
9965         regno = (unsigned) leaf_reg;
9966     }
9967 #endif
9968
9969   return DBX_REGISTER_NUMBER (regno);
9970 }
9971
9972 /* Optionally add a DW_OP_piece term to a location description expression.
9973    DW_OP_piece is only added if the location description expression already
9974    doesn't end with DW_OP_piece.  */
9975
9976 static void
9977 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9978 {
9979   dw_loc_descr_ref loc;
9980
9981   if (*list_head != NULL)
9982     {
9983       /* Find the end of the chain.  */
9984       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9985         ;
9986
9987       if (loc->dw_loc_opc != DW_OP_piece)
9988         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9989     }
9990 }
9991
9992 /* Return a location descriptor that designates a machine register or
9993    zero if there is none.  */
9994
9995 static dw_loc_descr_ref
9996 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9997 {
9998   rtx regs;
9999
10000   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10001     return 0;
10002
10003   regs = targetm.dwarf_register_span (rtl);
10004
10005   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10006     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10007   else
10008     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10009 }
10010
10011 /* Return a location descriptor that designates a machine register for
10012    a given hard register number.  */
10013
10014 static dw_loc_descr_ref
10015 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10016 {
10017   dw_loc_descr_ref reg_loc_descr;
10018
10019   if (regno <= 31)
10020     reg_loc_descr
10021       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10022   else
10023     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10024
10025   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10026     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10027
10028   return reg_loc_descr;
10029 }
10030
10031 /* Given an RTL of a register, return a location descriptor that
10032    designates a value that spans more than one register.  */
10033
10034 static dw_loc_descr_ref
10035 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10036                              enum var_init_status initialized)
10037 {
10038   int nregs, size, i;
10039   unsigned reg;
10040   dw_loc_descr_ref loc_result = NULL;
10041
10042   reg = REGNO (rtl);
10043 #ifdef LEAF_REG_REMAP
10044   if (current_function_uses_only_leaf_regs)
10045     {
10046       int leaf_reg = LEAF_REG_REMAP (reg);
10047       if (leaf_reg != -1)
10048         reg = (unsigned) leaf_reg;
10049     }
10050 #endif
10051   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10052   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10053
10054   /* Simple, contiguous registers.  */
10055   if (regs == NULL_RTX)
10056     {
10057       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10058
10059       loc_result = NULL;
10060       while (nregs--)
10061         {
10062           dw_loc_descr_ref t;
10063
10064           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10065                                       VAR_INIT_STATUS_INITIALIZED);
10066           add_loc_descr (&loc_result, t);
10067           add_loc_descr_op_piece (&loc_result, size);
10068           ++reg;
10069         }
10070       return loc_result;
10071     }
10072
10073   /* Now onto stupid register sets in non contiguous locations.  */
10074
10075   gcc_assert (GET_CODE (regs) == PARALLEL);
10076
10077   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10078   loc_result = NULL;
10079
10080   for (i = 0; i < XVECLEN (regs, 0); ++i)
10081     {
10082       dw_loc_descr_ref t;
10083
10084       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10085                                   VAR_INIT_STATUS_INITIALIZED);
10086       add_loc_descr (&loc_result, t);
10087       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10088       add_loc_descr_op_piece (&loc_result, size);
10089     }
10090
10091   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10092     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10093   return loc_result;
10094 }
10095
10096 #endif /* DWARF2_DEBUGGING_INFO */
10097
10098 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10099
10100 /* Return a location descriptor that designates a constant.  */
10101
10102 static dw_loc_descr_ref
10103 int_loc_descriptor (HOST_WIDE_INT i)
10104 {
10105   enum dwarf_location_atom op;
10106
10107   /* Pick the smallest representation of a constant, rather than just
10108      defaulting to the LEB encoding.  */
10109   if (i >= 0)
10110     {
10111       if (i <= 31)
10112         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10113       else if (i <= 0xff)
10114         op = DW_OP_const1u;
10115       else if (i <= 0xffff)
10116         op = DW_OP_const2u;
10117       else if (HOST_BITS_PER_WIDE_INT == 32
10118                || i <= 0xffffffff)
10119         op = DW_OP_const4u;
10120       else
10121         op = DW_OP_constu;
10122     }
10123   else
10124     {
10125       if (i >= -0x80)
10126         op = DW_OP_const1s;
10127       else if (i >= -0x8000)
10128         op = DW_OP_const2s;
10129       else if (HOST_BITS_PER_WIDE_INT == 32
10130                || i >= -0x80000000)
10131         op = DW_OP_const4s;
10132       else
10133         op = DW_OP_consts;
10134     }
10135
10136   return new_loc_descr (op, i, 0);
10137 }
10138 #endif
10139
10140 #ifdef DWARF2_DEBUGGING_INFO
10141
10142 /* Return a location descriptor that designates a base+offset location.  */
10143
10144 static dw_loc_descr_ref
10145 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10146                  enum var_init_status initialized)
10147 {
10148   unsigned int regno;
10149   dw_loc_descr_ref result;
10150   dw_fde_ref fde = current_fde ();
10151
10152   /* We only use "frame base" when we're sure we're talking about the
10153      post-prologue local stack frame.  We do this by *not* running
10154      register elimination until this point, and recognizing the special
10155      argument pointer and soft frame pointer rtx's.  */
10156   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10157     {
10158       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10159
10160       if (elim != reg)
10161         {
10162           if (GET_CODE (elim) == PLUS)
10163             {
10164               offset += INTVAL (XEXP (elim, 1));
10165               elim = XEXP (elim, 0);
10166             }
10167           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10168                        && (elim == hard_frame_pointer_rtx
10169                            || elim == stack_pointer_rtx))
10170                       || elim == (frame_pointer_needed
10171                                   ? hard_frame_pointer_rtx
10172                                   : stack_pointer_rtx));
10173
10174           /* If drap register is used to align stack, use frame
10175              pointer + offset to access stack variables.  If stack
10176              is aligned without drap, use stack pointer + offset to
10177              access stack variables.  */
10178           if (crtl->stack_realign_tried
10179               && cfa.reg == HARD_FRAME_POINTER_REGNUM
10180               && reg == frame_pointer_rtx)
10181             {
10182               int base_reg
10183                 = DWARF_FRAME_REGNUM (cfa.indirect
10184                                       ? HARD_FRAME_POINTER_REGNUM
10185                                       : STACK_POINTER_REGNUM);
10186               return new_reg_loc_descr (base_reg, offset);
10187             }
10188
10189           offset += frame_pointer_fb_offset;
10190           return new_loc_descr (DW_OP_fbreg, offset, 0);
10191         }
10192     }
10193   else if (fde
10194            && fde->drap_reg != INVALID_REGNUM
10195            && (fde->drap_reg == REGNO (reg)
10196                || fde->vdrap_reg == REGNO (reg)))
10197     {
10198       /* Use cfa+offset to represent the location of arguments passed
10199          on stack when drap is used to align stack.  */
10200       return new_loc_descr (DW_OP_fbreg, offset, 0);
10201     }
10202
10203   regno = dbx_reg_number (reg);
10204   if (regno <= 31)
10205     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10206                             offset, 0);
10207   else
10208     result = new_loc_descr (DW_OP_bregx, regno, offset);
10209
10210   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10211     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10212
10213   return result;
10214 }
10215
10216 /* Return true if this RTL expression describes a base+offset calculation.  */
10217
10218 static inline int
10219 is_based_loc (const_rtx rtl)
10220 {
10221   return (GET_CODE (rtl) == PLUS
10222           && ((REG_P (XEXP (rtl, 0))
10223                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10224                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
10225 }
10226
10227 /* Return a descriptor that describes the concatenation of N locations
10228    used to form the address of a memory location.  */
10229
10230 static dw_loc_descr_ref
10231 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10232                             enum var_init_status initialized)
10233 {
10234   unsigned int i;
10235   dw_loc_descr_ref cc_loc_result = NULL;
10236   unsigned int n = XVECLEN (concatn, 0);
10237
10238   for (i = 0; i < n; ++i)
10239     {
10240       dw_loc_descr_ref ref;
10241       rtx x = XVECEXP (concatn, 0, i);
10242
10243       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10244       if (ref == NULL)
10245         return NULL;
10246
10247       add_loc_descr (&cc_loc_result, ref);
10248       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10249     }
10250
10251   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10252     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10253
10254   return cc_loc_result;
10255 }
10256
10257 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10258    failed.  */
10259
10260 static dw_loc_descr_ref
10261 tls_mem_loc_descriptor (rtx mem)
10262 {
10263   tree base;
10264   dw_loc_descr_ref loc_result;
10265
10266   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10267     return NULL;
10268
10269   base = get_base_address (MEM_EXPR (mem));
10270   if (base == NULL
10271       || TREE_CODE (base) != VAR_DECL
10272       || !DECL_THREAD_LOCAL_P (base))
10273     return NULL;
10274
10275   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10276   if (loc_result == NULL)
10277     return NULL;
10278
10279   if (INTVAL (MEM_OFFSET (mem)))
10280     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10281
10282   return loc_result;
10283 }
10284
10285 /* The following routine converts the RTL for a variable or parameter
10286    (resident in memory) into an equivalent Dwarf representation of a
10287    mechanism for getting the address of that same variable onto the top of a
10288    hypothetical "address evaluation" stack.
10289
10290    When creating memory location descriptors, we are effectively transforming
10291    the RTL for a memory-resident object into its Dwarf postfix expression
10292    equivalent.  This routine recursively descends an RTL tree, turning
10293    it into Dwarf postfix code as it goes.
10294
10295    MODE is the mode of the memory reference, needed to handle some
10296    autoincrement addressing modes.
10297
10298    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10299    location list for RTL.
10300
10301    Return 0 if we can't represent the location.  */
10302
10303 static dw_loc_descr_ref
10304 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10305                     enum var_init_status initialized)
10306 {
10307   dw_loc_descr_ref mem_loc_result = NULL;
10308   enum dwarf_location_atom op;
10309
10310   /* Note that for a dynamically sized array, the location we will generate a
10311      description of here will be the lowest numbered location which is
10312      actually within the array.  That's *not* necessarily the same as the
10313      zeroth element of the array.  */
10314
10315   rtl = targetm.delegitimize_address (rtl);
10316
10317   switch (GET_CODE (rtl))
10318     {
10319     case POST_INC:
10320     case POST_DEC:
10321     case POST_MODIFY:
10322       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10323          just fall into the SUBREG code.  */
10324
10325       /* ... fall through ...  */
10326
10327     case SUBREG:
10328       /* The case of a subreg may arise when we have a local (register)
10329          variable or a formal (register) parameter which doesn't quite fill
10330          up an entire register.  For now, just assume that it is
10331          legitimate to make the Dwarf info refer to the whole register which
10332          contains the given subreg.  */
10333       rtl = XEXP (rtl, 0);
10334
10335       /* ... fall through ...  */
10336
10337     case REG:
10338       /* Whenever a register number forms a part of the description of the
10339          method for calculating the (dynamic) address of a memory resident
10340          object, DWARF rules require the register number be referred to as
10341          a "base register".  This distinction is not based in any way upon
10342          what category of register the hardware believes the given register
10343          belongs to.  This is strictly DWARF terminology we're dealing with
10344          here. Note that in cases where the location of a memory-resident
10345          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10346          OP_CONST (0)) the actual DWARF location descriptor that we generate
10347          may just be OP_BASEREG (basereg).  This may look deceptively like
10348          the object in question was allocated to a register (rather than in
10349          memory) so DWARF consumers need to be aware of the subtle
10350          distinction between OP_REG and OP_BASEREG.  */
10351       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10352         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10353       else if (stack_realign_drap
10354                && crtl->drap_reg
10355                && crtl->args.internal_arg_pointer == rtl
10356                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10357         {
10358           /* If RTL is internal_arg_pointer, which has been optimized
10359              out, use DRAP instead.  */
10360           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10361                                             VAR_INIT_STATUS_INITIALIZED);
10362         }
10363       break;
10364
10365     case MEM:
10366       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10367                                            VAR_INIT_STATUS_INITIALIZED);
10368       if (mem_loc_result == NULL)
10369         mem_loc_result = tls_mem_loc_descriptor (rtl);
10370       if (mem_loc_result != 0)
10371         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10372       break;
10373
10374     case LO_SUM:
10375          rtl = XEXP (rtl, 1);
10376
10377       /* ... fall through ...  */
10378
10379     case LABEL_REF:
10380       /* Some ports can transform a symbol ref into a label ref, because
10381          the symbol ref is too far away and has to be dumped into a constant
10382          pool.  */
10383     case CONST:
10384     case SYMBOL_REF:
10385       /* Alternatively, the symbol in the constant pool might be referenced
10386          by a different symbol.  */
10387       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10388         {
10389           bool marked;
10390           rtx tmp = get_pool_constant_mark (rtl, &marked);
10391
10392           if (GET_CODE (tmp) == SYMBOL_REF)
10393             {
10394               rtl = tmp;
10395               if (CONSTANT_POOL_ADDRESS_P (tmp))
10396                 get_pool_constant_mark (tmp, &marked);
10397               else
10398                 marked = true;
10399             }
10400
10401           /* If all references to this pool constant were optimized away,
10402              it was not output and thus we can't represent it.
10403              FIXME: might try to use DW_OP_const_value here, though
10404              DW_OP_piece complicates it.  */
10405           if (!marked)
10406             return 0;
10407         }
10408
10409       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10410       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10411       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10412       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10413       break;
10414
10415     case PRE_MODIFY:
10416       /* Extract the PLUS expression nested inside and fall into
10417          PLUS code below.  */
10418       rtl = XEXP (rtl, 1);
10419       goto plus;
10420
10421     case PRE_INC:
10422     case PRE_DEC:
10423       /* Turn these into a PLUS expression and fall into the PLUS code
10424          below.  */
10425       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10426                           GEN_INT (GET_CODE (rtl) == PRE_INC
10427                                    ? GET_MODE_UNIT_SIZE (mode)
10428                                    : -GET_MODE_UNIT_SIZE (mode)));
10429
10430       /* ... fall through ...  */
10431
10432     case PLUS:
10433     plus:
10434       if (is_based_loc (rtl))
10435         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10436                                           INTVAL (XEXP (rtl, 1)),
10437                                           VAR_INIT_STATUS_INITIALIZED);
10438       else
10439         {
10440           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10441                                                VAR_INIT_STATUS_INITIALIZED);
10442           if (mem_loc_result == 0)
10443             break;
10444
10445           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT)
10446             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
10447           else
10448             {
10449               dw_loc_descr_ref mem_loc_result2
10450                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10451                                       VAR_INIT_STATUS_INITIALIZED);
10452               if (mem_loc_result2 == 0)
10453                 break;
10454               add_loc_descr (&mem_loc_result, mem_loc_result2);
10455               add_loc_descr (&mem_loc_result,
10456                              new_loc_descr (DW_OP_plus, 0, 0));
10457             }
10458         }
10459       break;
10460
10461     /* If a pseudo-reg is optimized away, it is possible for it to
10462        be replaced with a MEM containing a multiply or shift.  */
10463     case MULT:
10464       op = DW_OP_mul;
10465       goto do_binop;
10466
10467     case ASHIFT:
10468       op = DW_OP_shl;
10469       goto do_binop;
10470
10471     case ASHIFTRT:
10472       op = DW_OP_shra;
10473       goto do_binop;
10474
10475     case LSHIFTRT:
10476       op = DW_OP_shr;
10477       goto do_binop;
10478
10479     do_binop:
10480       {
10481         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10482                                                    VAR_INIT_STATUS_INITIALIZED);
10483         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10484                                                    VAR_INIT_STATUS_INITIALIZED);
10485
10486         if (op0 == 0 || op1 == 0)
10487           break;
10488
10489         mem_loc_result = op0;
10490         add_loc_descr (&mem_loc_result, op1);
10491         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10492         break;
10493       }
10494
10495     case CONST_INT:
10496       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10497       break;
10498
10499     case CONCATN:
10500       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10501                                                    VAR_INIT_STATUS_INITIALIZED);
10502       break;
10503
10504     case UNSPEC:
10505       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10506          can't express it in the debug info.  This can happen e.g. with some
10507          TLS UNSPECs.  */
10508       break;
10509
10510     default:
10511       gcc_unreachable ();
10512     }
10513
10514   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10515     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10516
10517   return mem_loc_result;
10518 }
10519
10520 /* Return a descriptor that describes the concatenation of two locations.
10521    This is typically a complex variable.  */
10522
10523 static dw_loc_descr_ref
10524 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10525 {
10526   dw_loc_descr_ref cc_loc_result = NULL;
10527   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10528   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10529
10530   if (x0_ref == 0 || x1_ref == 0)
10531     return 0;
10532
10533   cc_loc_result = x0_ref;
10534   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10535
10536   add_loc_descr (&cc_loc_result, x1_ref);
10537   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10538
10539   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10540     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10541
10542   return cc_loc_result;
10543 }
10544
10545 /* Return a descriptor that describes the concatenation of N
10546    locations.  */
10547
10548 static dw_loc_descr_ref
10549 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10550 {
10551   unsigned int i;
10552   dw_loc_descr_ref cc_loc_result = NULL;
10553   unsigned int n = XVECLEN (concatn, 0);
10554
10555   for (i = 0; i < n; ++i)
10556     {
10557       dw_loc_descr_ref ref;
10558       rtx x = XVECEXP (concatn, 0, i);
10559
10560       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10561       if (ref == NULL)
10562         return NULL;
10563
10564       add_loc_descr (&cc_loc_result, ref);
10565       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10566     }
10567
10568   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10569     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10570
10571   return cc_loc_result;
10572 }
10573
10574 /* Output a proper Dwarf location descriptor for a variable or parameter
10575    which is either allocated in a register or in a memory location.  For a
10576    register, we just generate an OP_REG and the register number.  For a
10577    memory location we provide a Dwarf postfix expression describing how to
10578    generate the (dynamic) address of the object onto the address stack.
10579
10580    If we don't know how to describe it, return 0.  */
10581
10582 static dw_loc_descr_ref
10583 loc_descriptor (rtx rtl, enum var_init_status initialized)
10584 {
10585   dw_loc_descr_ref loc_result = NULL;
10586
10587   switch (GET_CODE (rtl))
10588     {
10589     case SUBREG:
10590       /* The case of a subreg may arise when we have a local (register)
10591          variable or a formal (register) parameter which doesn't quite fill
10592          up an entire register.  For now, just assume that it is
10593          legitimate to make the Dwarf info refer to the whole register which
10594          contains the given subreg.  */
10595       rtl = SUBREG_REG (rtl);
10596
10597       /* ... fall through ...  */
10598
10599     case REG:
10600       loc_result = reg_loc_descriptor (rtl, initialized);
10601       break;
10602
10603     case MEM:
10604       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10605                                        initialized);
10606       if (loc_result == NULL)
10607         loc_result = tls_mem_loc_descriptor (rtl);
10608       break;
10609
10610     case CONCAT:
10611       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10612                                           initialized);
10613       break;
10614
10615     case CONCATN:
10616       loc_result = concatn_loc_descriptor (rtl, initialized);
10617       break;
10618
10619     case VAR_LOCATION:
10620       /* Single part.  */
10621       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10622         {
10623           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10624           break;
10625         }
10626
10627       rtl = XEXP (rtl, 1);
10628       /* FALLTHRU */
10629
10630     case PARALLEL:
10631       {
10632         rtvec par_elems = XVEC (rtl, 0);
10633         int num_elem = GET_NUM_ELEM (par_elems);
10634         enum machine_mode mode;
10635         int i;
10636
10637         /* Create the first one, so we have something to add to.  */
10638         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10639                                      initialized);
10640         if (loc_result == NULL)
10641           return NULL;
10642         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10643         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10644         for (i = 1; i < num_elem; i++)
10645           {
10646             dw_loc_descr_ref temp;
10647
10648             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10649                                    initialized);
10650             if (temp == NULL)
10651               return NULL;
10652             add_loc_descr (&loc_result, temp);
10653             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10654             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10655           }
10656       }
10657       break;
10658
10659     default:
10660       gcc_unreachable ();
10661     }
10662
10663   return loc_result;
10664 }
10665
10666 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10667    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10668    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10669    top-level invocation, and we require the address of LOC; is 0 if we require
10670    the value of LOC.  */
10671
10672 static dw_loc_descr_ref
10673 loc_descriptor_from_tree_1 (tree loc, int want_address)
10674 {
10675   dw_loc_descr_ref ret, ret1;
10676   int have_address = 0;
10677   enum dwarf_location_atom op;
10678
10679   /* ??? Most of the time we do not take proper care for sign/zero
10680      extending the values properly.  Hopefully this won't be a real
10681      problem...  */
10682
10683   switch (TREE_CODE (loc))
10684     {
10685     case ERROR_MARK:
10686       return 0;
10687
10688     case PLACEHOLDER_EXPR:
10689       /* This case involves extracting fields from an object to determine the
10690          position of other fields.  We don't try to encode this here.  The
10691          only user of this is Ada, which encodes the needed information using
10692          the names of types.  */
10693       return 0;
10694
10695     case CALL_EXPR:
10696       return 0;
10697
10698     case PREINCREMENT_EXPR:
10699     case PREDECREMENT_EXPR:
10700     case POSTINCREMENT_EXPR:
10701     case POSTDECREMENT_EXPR:
10702       /* There are no opcodes for these operations.  */
10703       return 0;
10704
10705     case ADDR_EXPR:
10706       /* If we already want an address, there's nothing we can do.  */
10707       if (want_address)
10708         return 0;
10709
10710       /* Otherwise, process the argument and look for the address.  */
10711       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10712
10713     case VAR_DECL:
10714       if (DECL_THREAD_LOCAL_P (loc))
10715         {
10716           rtx rtl;
10717           enum dwarf_location_atom first_op;
10718           enum dwarf_location_atom second_op;
10719
10720           if (targetm.have_tls)
10721             {
10722               /* If this is not defined, we have no way to emit the
10723                  data.  */
10724               if (!targetm.asm_out.output_dwarf_dtprel)
10725                 return 0;
10726
10727                /* The way DW_OP_GNU_push_tls_address is specified, we
10728                   can only look up addresses of objects in the current
10729                   module.  */
10730               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10731                 return 0;
10732               first_op = (enum dwarf_location_atom) INTERNAL_DW_OP_tls_addr;
10733               second_op = DW_OP_GNU_push_tls_address;
10734             }
10735           else
10736             {
10737               if (!targetm.emutls.debug_form_tls_address)
10738                 return 0;
10739               loc = emutls_decl (loc);
10740               first_op = DW_OP_addr;
10741               second_op = DW_OP_form_tls_address;
10742             }
10743
10744           rtl = rtl_for_decl_location (loc);
10745           if (rtl == NULL_RTX)
10746             return 0;
10747
10748           if (!MEM_P (rtl))
10749             return 0;
10750           rtl = XEXP (rtl, 0);
10751           if (! CONSTANT_P (rtl))
10752             return 0;
10753
10754           ret = new_loc_descr (first_op, 0, 0);
10755           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10756           ret->dw_loc_oprnd1.v.val_addr = rtl;
10757
10758           ret1 = new_loc_descr (second_op, 0, 0);
10759           add_loc_descr (&ret, ret1);
10760
10761           have_address = 1;
10762           break;
10763         }
10764       /* FALLTHRU */
10765
10766     case PARM_DECL:
10767       if (DECL_HAS_VALUE_EXPR_P (loc))
10768         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10769                                            want_address);
10770       /* FALLTHRU */
10771
10772     case RESULT_DECL:
10773     case FUNCTION_DECL:
10774       {
10775         rtx rtl = rtl_for_decl_location (loc);
10776
10777         if (rtl == NULL_RTX)
10778           return 0;
10779         else if (GET_CODE (rtl) == CONST_INT)
10780           {
10781             HOST_WIDE_INT val = INTVAL (rtl);
10782             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10783               val &= GET_MODE_MASK (DECL_MODE (loc));
10784             ret = int_loc_descriptor (val);
10785           }
10786         else if (GET_CODE (rtl) == CONST_STRING)
10787           return 0;
10788         else if (CONSTANT_P (rtl))
10789           {
10790             ret = new_loc_descr (DW_OP_addr, 0, 0);
10791             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10792             ret->dw_loc_oprnd1.v.val_addr = rtl;
10793           }
10794         else
10795           {
10796             enum machine_mode mode;
10797
10798             /* Certain constructs can only be represented at top-level.  */
10799             if (want_address == 2)
10800               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10801
10802             mode = GET_MODE (rtl);
10803             if (MEM_P (rtl))
10804               {
10805                 rtl = XEXP (rtl, 0);
10806                 have_address = 1;
10807               }
10808             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10809           }
10810       }
10811       break;
10812
10813     case INDIRECT_REF:
10814       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10815       have_address = 1;
10816       break;
10817
10818     case COMPOUND_EXPR:
10819       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10820
10821     CASE_CONVERT:
10822     case VIEW_CONVERT_EXPR:
10823     case SAVE_EXPR:
10824     case MODIFY_EXPR:
10825       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10826
10827     case COMPONENT_REF:
10828     case BIT_FIELD_REF:
10829     case ARRAY_REF:
10830     case ARRAY_RANGE_REF:
10831       {
10832         tree obj, offset;
10833         HOST_WIDE_INT bitsize, bitpos, bytepos;
10834         enum machine_mode mode;
10835         int volatilep;
10836         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10837
10838         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10839                                    &unsignedp, &volatilep, false);
10840
10841         if (obj == loc)
10842           return 0;
10843
10844         ret = loc_descriptor_from_tree_1 (obj, 1);
10845         if (ret == 0
10846             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10847           return 0;
10848
10849         if (offset != NULL_TREE)
10850           {
10851             /* Variable offset.  */
10852             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10853             if (ret1 == 0)
10854               return 0;
10855             add_loc_descr (&ret, ret1);
10856             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10857           }
10858
10859         bytepos = bitpos / BITS_PER_UNIT;
10860         loc_descr_plus_const (&ret, bytepos);
10861
10862         have_address = 1;
10863         break;
10864       }
10865
10866     case INTEGER_CST:
10867       if (host_integerp (loc, 0))
10868         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10869       else
10870         return 0;
10871       break;
10872
10873     case CONSTRUCTOR:
10874       {
10875         /* Get an RTL for this, if something has been emitted.  */
10876         rtx rtl = lookup_constant_def (loc);
10877         enum machine_mode mode;
10878
10879         if (!rtl || !MEM_P (rtl))
10880           return 0;
10881         mode = GET_MODE (rtl);
10882         rtl = XEXP (rtl, 0);
10883         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10884         have_address = 1;
10885         break;
10886       }
10887
10888     case TRUTH_AND_EXPR:
10889     case TRUTH_ANDIF_EXPR:
10890     case BIT_AND_EXPR:
10891       op = DW_OP_and;
10892       goto do_binop;
10893
10894     case TRUTH_XOR_EXPR:
10895     case BIT_XOR_EXPR:
10896       op = DW_OP_xor;
10897       goto do_binop;
10898
10899     case TRUTH_OR_EXPR:
10900     case TRUTH_ORIF_EXPR:
10901     case BIT_IOR_EXPR:
10902       op = DW_OP_or;
10903       goto do_binop;
10904
10905     case FLOOR_DIV_EXPR:
10906     case CEIL_DIV_EXPR:
10907     case ROUND_DIV_EXPR:
10908     case TRUNC_DIV_EXPR:
10909       op = DW_OP_div;
10910       goto do_binop;
10911
10912     case MINUS_EXPR:
10913       op = DW_OP_minus;
10914       goto do_binop;
10915
10916     case FLOOR_MOD_EXPR:
10917     case CEIL_MOD_EXPR:
10918     case ROUND_MOD_EXPR:
10919     case TRUNC_MOD_EXPR:
10920       op = DW_OP_mod;
10921       goto do_binop;
10922
10923     case MULT_EXPR:
10924       op = DW_OP_mul;
10925       goto do_binop;
10926
10927     case LSHIFT_EXPR:
10928       op = DW_OP_shl;
10929       goto do_binop;
10930
10931     case RSHIFT_EXPR:
10932       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10933       goto do_binop;
10934
10935     case POINTER_PLUS_EXPR:
10936     case PLUS_EXPR:
10937       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10938           && host_integerp (TREE_OPERAND (loc, 1), 0))
10939         {
10940           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10941           if (ret == 0)
10942             return 0;
10943
10944           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
10945           break;
10946         }
10947
10948       op = DW_OP_plus;
10949       goto do_binop;
10950
10951     case LE_EXPR:
10952       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10953         return 0;
10954
10955       op = DW_OP_le;
10956       goto do_binop;
10957
10958     case GE_EXPR:
10959       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10960         return 0;
10961
10962       op = DW_OP_ge;
10963       goto do_binop;
10964
10965     case LT_EXPR:
10966       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10967         return 0;
10968
10969       op = DW_OP_lt;
10970       goto do_binop;
10971
10972     case GT_EXPR:
10973       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10974         return 0;
10975
10976       op = DW_OP_gt;
10977       goto do_binop;
10978
10979     case EQ_EXPR:
10980       op = DW_OP_eq;
10981       goto do_binop;
10982
10983     case NE_EXPR:
10984       op = DW_OP_ne;
10985       goto do_binop;
10986
10987     do_binop:
10988       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10989       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10990       if (ret == 0 || ret1 == 0)
10991         return 0;
10992
10993       add_loc_descr (&ret, ret1);
10994       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10995       break;
10996
10997     case TRUTH_NOT_EXPR:
10998     case BIT_NOT_EXPR:
10999       op = DW_OP_not;
11000       goto do_unop;
11001
11002     case ABS_EXPR:
11003       op = DW_OP_abs;
11004       goto do_unop;
11005
11006     case NEGATE_EXPR:
11007       op = DW_OP_neg;
11008       goto do_unop;
11009
11010     do_unop:
11011       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11012       if (ret == 0)
11013         return 0;
11014
11015       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11016       break;
11017
11018     case MIN_EXPR:
11019     case MAX_EXPR:
11020       {
11021         const enum tree_code code =
11022           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
11023
11024         loc = build3 (COND_EXPR, TREE_TYPE (loc),
11025                       build2 (code, integer_type_node,
11026                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
11027                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
11028       }
11029
11030       /* ... fall through ...  */
11031
11032     case COND_EXPR:
11033       {
11034         dw_loc_descr_ref lhs
11035           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
11036         dw_loc_descr_ref rhs
11037           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
11038         dw_loc_descr_ref bra_node, jump_node, tmp;
11039
11040         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
11041         if (ret == 0 || lhs == 0 || rhs == 0)
11042           return 0;
11043
11044         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11045         add_loc_descr (&ret, bra_node);
11046
11047         add_loc_descr (&ret, rhs);
11048         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
11049         add_loc_descr (&ret, jump_node);
11050
11051         add_loc_descr (&ret, lhs);
11052         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11053         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
11054
11055         /* ??? Need a node to point the skip at.  Use a nop.  */
11056         tmp = new_loc_descr (DW_OP_nop, 0, 0);
11057         add_loc_descr (&ret, tmp);
11058         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11059         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
11060       }
11061       break;
11062
11063     case FIX_TRUNC_EXPR:
11064       return 0;
11065
11066     default:
11067       /* Leave front-end specific codes as simply unknown.  This comes
11068          up, for instance, with the C STMT_EXPR.  */
11069       if ((unsigned int) TREE_CODE (loc)
11070           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
11071         return 0;
11072
11073 #ifdef ENABLE_CHECKING
11074       /* Otherwise this is a generic code; we should just lists all of
11075          these explicitly.  We forgot one.  */
11076       gcc_unreachable ();
11077 #else
11078       /* In a release build, we want to degrade gracefully: better to
11079          generate incomplete debugging information than to crash.  */
11080       return NULL;
11081 #endif
11082     }
11083
11084   /* Show if we can't fill the request for an address.  */
11085   if (want_address && !have_address)
11086     return 0;
11087
11088   /* If we've got an address and don't want one, dereference.  */
11089   if (!want_address && have_address && ret)
11090     {
11091       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
11092
11093       if (size > DWARF2_ADDR_SIZE || size == -1)
11094         return 0;
11095       else if (size == DWARF2_ADDR_SIZE)
11096         op = DW_OP_deref;
11097       else
11098         op = DW_OP_deref_size;
11099
11100       add_loc_descr (&ret, new_loc_descr (op, size, 0));
11101     }
11102
11103   return ret;
11104 }
11105
11106 static inline dw_loc_descr_ref
11107 loc_descriptor_from_tree (tree loc)
11108 {
11109   return loc_descriptor_from_tree_1 (loc, 2);
11110 }
11111
11112 /* Given a value, round it up to the lowest multiple of `boundary'
11113    which is not less than the value itself.  */
11114
11115 static inline HOST_WIDE_INT
11116 ceiling (HOST_WIDE_INT value, unsigned int boundary)
11117 {
11118   return (((value + boundary - 1) / boundary) * boundary);
11119 }
11120
11121 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
11122    pointer to the declared type for the relevant field variable, or return
11123    `integer_type_node' if the given node turns out to be an
11124    ERROR_MARK node.  */
11125
11126 static inline tree
11127 field_type (const_tree decl)
11128 {
11129   tree type;
11130
11131   if (TREE_CODE (decl) == ERROR_MARK)
11132     return integer_type_node;
11133
11134   type = DECL_BIT_FIELD_TYPE (decl);
11135   if (type == NULL_TREE)
11136     type = TREE_TYPE (decl);
11137
11138   return type;
11139 }
11140
11141 /* Given a pointer to a tree node, return the alignment in bits for
11142    it, or else return BITS_PER_WORD if the node actually turns out to
11143    be an ERROR_MARK node.  */
11144
11145 static inline unsigned
11146 simple_type_align_in_bits (const_tree type)
11147 {
11148   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
11149 }
11150
11151 static inline unsigned
11152 simple_decl_align_in_bits (const_tree decl)
11153 {
11154   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
11155 }
11156
11157 /* Return the result of rounding T up to ALIGN.  */
11158
11159 static inline HOST_WIDE_INT
11160 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
11161 {
11162   /* We must be careful if T is negative because HOST_WIDE_INT can be
11163      either "above" or "below" unsigned int as per the C promotion
11164      rules, depending on the host, thus making the signedness of the
11165      direct multiplication and division unpredictable.  */
11166   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
11167
11168   u += align - 1;
11169   u /= align;
11170   u *= align;
11171
11172   return (HOST_WIDE_INT) u;
11173 }
11174
11175 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
11176    lowest addressed byte of the "containing object" for the given FIELD_DECL,
11177    or return 0 if we are unable to determine what that offset is, either
11178    because the argument turns out to be a pointer to an ERROR_MARK node, or
11179    because the offset is actually variable.  (We can't handle the latter case
11180    just yet).  */
11181
11182 static HOST_WIDE_INT
11183 field_byte_offset (const_tree decl)
11184 {
11185   HOST_WIDE_INT object_offset_in_bits;
11186   HOST_WIDE_INT bitpos_int;
11187
11188   if (TREE_CODE (decl) == ERROR_MARK)
11189     return 0;
11190
11191   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
11192
11193   /* We cannot yet cope with fields whose positions are variable, so
11194      for now, when we see such things, we simply return 0.  Someday, we may
11195      be able to handle such cases, but it will be damn difficult.  */
11196   if (! host_integerp (bit_position (decl), 0))
11197     return 0;
11198
11199   bitpos_int = int_bit_position (decl);
11200
11201 #ifdef PCC_BITFIELD_TYPE_MATTERS
11202   if (PCC_BITFIELD_TYPE_MATTERS)
11203     {
11204       tree type;
11205       tree field_size_tree;
11206       HOST_WIDE_INT deepest_bitpos;
11207       unsigned HOST_WIDE_INT field_size_in_bits;
11208       unsigned int type_align_in_bits;
11209       unsigned int decl_align_in_bits;
11210       unsigned HOST_WIDE_INT type_size_in_bits;
11211
11212       type = field_type (decl);
11213       type_size_in_bits = simple_type_size_in_bits (type);
11214       type_align_in_bits = simple_type_align_in_bits (type);
11215
11216       field_size_tree = DECL_SIZE (decl);
11217
11218       /* The size could be unspecified if there was an error, or for
11219          a flexible array member.  */
11220       if (!field_size_tree)
11221         field_size_tree = bitsize_zero_node;
11222
11223       /* If the size of the field is not constant, use the type size.  */
11224       if (host_integerp (field_size_tree, 1))
11225         field_size_in_bits = tree_low_cst (field_size_tree, 1);
11226       else
11227         field_size_in_bits = type_size_in_bits;
11228
11229       decl_align_in_bits = simple_decl_align_in_bits (decl);
11230
11231       /* The GCC front-end doesn't make any attempt to keep track of the
11232          starting bit offset (relative to the start of the containing
11233          structure type) of the hypothetical "containing object" for a
11234          bit-field.  Thus, when computing the byte offset value for the
11235          start of the "containing object" of a bit-field, we must deduce
11236          this information on our own. This can be rather tricky to do in
11237          some cases.  For example, handling the following structure type
11238          definition when compiling for an i386/i486 target (which only
11239          aligns long long's to 32-bit boundaries) can be very tricky:
11240
11241          struct S { int field1; long long field2:31; };
11242
11243          Fortunately, there is a simple rule-of-thumb which can be used
11244          in such cases.  When compiling for an i386/i486, GCC will
11245          allocate 8 bytes for the structure shown above.  It decides to
11246          do this based upon one simple rule for bit-field allocation.
11247          GCC allocates each "containing object" for each bit-field at
11248          the first (i.e. lowest addressed) legitimate alignment boundary
11249          (based upon the required minimum alignment for the declared
11250          type of the field) which it can possibly use, subject to the
11251          condition that there is still enough available space remaining
11252          in the containing object (when allocated at the selected point)
11253          to fully accommodate all of the bits of the bit-field itself.
11254
11255          This simple rule makes it obvious why GCC allocates 8 bytes for
11256          each object of the structure type shown above.  When looking
11257          for a place to allocate the "containing object" for `field2',
11258          the compiler simply tries to allocate a 64-bit "containing
11259          object" at each successive 32-bit boundary (starting at zero)
11260          until it finds a place to allocate that 64- bit field such that
11261          at least 31 contiguous (and previously unallocated) bits remain
11262          within that selected 64 bit field.  (As it turns out, for the
11263          example above, the compiler finds it is OK to allocate the
11264          "containing object" 64-bit field at bit-offset zero within the
11265          structure type.)
11266
11267          Here we attempt to work backwards from the limited set of facts
11268          we're given, and we try to deduce from those facts, where GCC
11269          must have believed that the containing object started (within
11270          the structure type). The value we deduce is then used (by the
11271          callers of this routine) to generate DW_AT_location and
11272          DW_AT_bit_offset attributes for fields (both bit-fields and, in
11273          the case of DW_AT_location, regular fields as well).  */
11274
11275       /* Figure out the bit-distance from the start of the structure to
11276          the "deepest" bit of the bit-field.  */
11277       deepest_bitpos = bitpos_int + field_size_in_bits;
11278
11279       /* This is the tricky part.  Use some fancy footwork to deduce
11280          where the lowest addressed bit of the containing object must
11281          be.  */
11282       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11283
11284       /* Round up to type_align by default.  This works best for
11285          bitfields.  */
11286       object_offset_in_bits
11287         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11288
11289       if (object_offset_in_bits > bitpos_int)
11290         {
11291           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11292
11293           /* Round up to decl_align instead.  */
11294           object_offset_in_bits
11295             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11296         }
11297     }
11298   else
11299 #endif
11300     object_offset_in_bits = bitpos_int;
11301
11302   return object_offset_in_bits / BITS_PER_UNIT;
11303 }
11304 \f
11305 /* The following routines define various Dwarf attributes and any data
11306    associated with them.  */
11307
11308 /* Add a location description attribute value to a DIE.
11309
11310    This emits location attributes suitable for whole variables and
11311    whole parameters.  Note that the location attributes for struct fields are
11312    generated by the routine `data_member_location_attribute' below.  */
11313
11314 static inline void
11315 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11316                              dw_loc_descr_ref descr)
11317 {
11318   if (descr != 0)
11319     add_AT_loc (die, attr_kind, descr);
11320 }
11321
11322 /* Attach the specialized form of location attribute used for data members of
11323    struct and union types.  In the special case of a FIELD_DECL node which
11324    represents a bit-field, the "offset" part of this special location
11325    descriptor must indicate the distance in bytes from the lowest-addressed
11326    byte of the containing struct or union type to the lowest-addressed byte of
11327    the "containing object" for the bit-field.  (See the `field_byte_offset'
11328    function above).
11329
11330    For any given bit-field, the "containing object" is a hypothetical object
11331    (of some integral or enum type) within which the given bit-field lives.  The
11332    type of this hypothetical "containing object" is always the same as the
11333    declared type of the individual bit-field itself (for GCC anyway... the
11334    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11335    bytes) of the hypothetical "containing object" which will be given in the
11336    DW_AT_byte_size attribute for this bit-field.  (See the
11337    `byte_size_attribute' function below.)  It is also used when calculating the
11338    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11339    function below.)  */
11340
11341 static void
11342 add_data_member_location_attribute (dw_die_ref die, tree decl)
11343 {
11344   HOST_WIDE_INT offset;
11345   dw_loc_descr_ref loc_descr = 0;
11346
11347   if (TREE_CODE (decl) == TREE_BINFO)
11348     {
11349       /* We're working on the TAG_inheritance for a base class.  */
11350       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11351         {
11352           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11353              aren't at a fixed offset from all (sub)objects of the same
11354              type.  We need to extract the appropriate offset from our
11355              vtable.  The following dwarf expression means
11356
11357                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11358
11359              This is specific to the V3 ABI, of course.  */
11360
11361           dw_loc_descr_ref tmp;
11362
11363           /* Make a copy of the object address.  */
11364           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11365           add_loc_descr (&loc_descr, tmp);
11366
11367           /* Extract the vtable address.  */
11368           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11369           add_loc_descr (&loc_descr, tmp);
11370
11371           /* Calculate the address of the offset.  */
11372           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11373           gcc_assert (offset < 0);
11374
11375           tmp = int_loc_descriptor (-offset);
11376           add_loc_descr (&loc_descr, tmp);
11377           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11378           add_loc_descr (&loc_descr, tmp);
11379
11380           /* Extract the offset.  */
11381           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11382           add_loc_descr (&loc_descr, tmp);
11383
11384           /* Add it to the object address.  */
11385           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11386           add_loc_descr (&loc_descr, tmp);
11387         }
11388       else
11389         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11390     }
11391   else
11392     offset = field_byte_offset (decl);
11393
11394   if (! loc_descr)
11395     {
11396       enum dwarf_location_atom op;
11397
11398       /* The DWARF2 standard says that we should assume that the structure
11399          address is already on the stack, so we can specify a structure field
11400          address by using DW_OP_plus_uconst.  */
11401
11402 #ifdef MIPS_DEBUGGING_INFO
11403       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11404          operator correctly.  It works only if we leave the offset on the
11405          stack.  */
11406       op = DW_OP_constu;
11407 #else
11408       op = DW_OP_plus_uconst;
11409 #endif
11410
11411       loc_descr = new_loc_descr (op, offset, 0);
11412     }
11413
11414   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11415 }
11416
11417 /* Writes integer values to dw_vec_const array.  */
11418
11419 static void
11420 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11421 {
11422   while (size != 0)
11423     {
11424       *dest++ = val & 0xff;
11425       val >>= 8;
11426       --size;
11427     }
11428 }
11429
11430 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11431
11432 static HOST_WIDE_INT
11433 extract_int (const unsigned char *src, unsigned int size)
11434 {
11435   HOST_WIDE_INT val = 0;
11436
11437   src += size;
11438   while (size != 0)
11439     {
11440       val <<= 8;
11441       val |= *--src & 0xff;
11442       --size;
11443     }
11444   return val;
11445 }
11446
11447 /* Writes floating point values to dw_vec_const array.  */
11448
11449 static void
11450 insert_float (const_rtx rtl, unsigned char *array)
11451 {
11452   REAL_VALUE_TYPE rv;
11453   long val[4];
11454   int i;
11455
11456   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11457   real_to_target (val, &rv, GET_MODE (rtl));
11458
11459   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11460   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11461     {
11462       insert_int (val[i], 4, array);
11463       array += 4;
11464     }
11465 }
11466
11467 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11468    does not have a "location" either in memory or in a register.  These
11469    things can arise in GNU C when a constant is passed as an actual parameter
11470    to an inlined function.  They can also arise in C++ where declared
11471    constants do not necessarily get memory "homes".  */
11472
11473 static void
11474 add_const_value_attribute (dw_die_ref die, rtx rtl)
11475 {
11476   switch (GET_CODE (rtl))
11477     {
11478     case CONST_INT:
11479       {
11480         HOST_WIDE_INT val = INTVAL (rtl);
11481
11482         if (val < 0)
11483           add_AT_int (die, DW_AT_const_value, val);
11484         else
11485           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11486       }
11487       break;
11488
11489     case CONST_DOUBLE:
11490       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11491          floating-point constant.  A CONST_DOUBLE is used whenever the
11492          constant requires more than one word in order to be adequately
11493          represented.  We output CONST_DOUBLEs as blocks.  */
11494       {
11495         enum machine_mode mode = GET_MODE (rtl);
11496
11497         if (SCALAR_FLOAT_MODE_P (mode))
11498           {
11499             unsigned int length = GET_MODE_SIZE (mode);
11500             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11501
11502             insert_float (rtl, array);
11503             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11504           }
11505         else
11506           {
11507             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11508             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11509
11510             add_AT_long_long (die, DW_AT_const_value,
11511                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11512           }
11513       }
11514       break;
11515
11516     case CONST_VECTOR:
11517       {
11518         enum machine_mode mode = GET_MODE (rtl);
11519         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11520         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11521         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11522         unsigned int i;
11523         unsigned char *p;
11524
11525         switch (GET_MODE_CLASS (mode))
11526           {
11527           case MODE_VECTOR_INT:
11528             for (i = 0, p = array; i < length; i++, p += elt_size)
11529               {
11530                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11531                 HOST_WIDE_INT lo, hi;
11532
11533                 switch (GET_CODE (elt))
11534                   {
11535                   case CONST_INT:
11536                     lo = INTVAL (elt);
11537                     hi = -(lo < 0);
11538                     break;
11539
11540                   case CONST_DOUBLE:
11541                     lo = CONST_DOUBLE_LOW (elt);
11542                     hi = CONST_DOUBLE_HIGH (elt);
11543                     break;
11544
11545                   default:
11546                     gcc_unreachable ();
11547                   }
11548
11549                 if (elt_size <= sizeof (HOST_WIDE_INT))
11550                   insert_int (lo, elt_size, p);
11551                 else
11552                   {
11553                     unsigned char *p0 = p;
11554                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11555
11556                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11557                     if (WORDS_BIG_ENDIAN)
11558                       {
11559                         p0 = p1;
11560                         p1 = p;
11561                       }
11562                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11563                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11564                   }
11565               }
11566             break;
11567
11568           case MODE_VECTOR_FLOAT:
11569             for (i = 0, p = array; i < length; i++, p += elt_size)
11570               {
11571                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11572                 insert_float (elt, p);
11573               }
11574             break;
11575
11576           default:
11577             gcc_unreachable ();
11578           }
11579
11580         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11581       }
11582       break;
11583
11584     case CONST_STRING:
11585       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11586       break;
11587
11588     case SYMBOL_REF:
11589     case LABEL_REF:
11590     case CONST:
11591       add_AT_addr (die, DW_AT_const_value, rtl);
11592       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11593       break;
11594
11595     case PLUS:
11596       /* In cases where an inlined instance of an inline function is passed
11597          the address of an `auto' variable (which is local to the caller) we
11598          can get a situation where the DECL_RTL of the artificial local
11599          variable (for the inlining) which acts as a stand-in for the
11600          corresponding formal parameter (of the inline function) will look
11601          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11602          exactly a compile-time constant expression, but it isn't the address
11603          of the (artificial) local variable either.  Rather, it represents the
11604          *value* which the artificial local variable always has during its
11605          lifetime.  We currently have no way to represent such quasi-constant
11606          values in Dwarf, so for now we just punt and generate nothing.  */
11607       break;
11608
11609     default:
11610       /* No other kinds of rtx should be possible here.  */
11611       gcc_unreachable ();
11612     }
11613
11614 }
11615
11616 /* Determine whether the evaluation of EXPR references any variables
11617    or functions which aren't otherwise used (and therefore may not be
11618    output).  */
11619 static tree
11620 reference_to_unused (tree * tp, int * walk_subtrees,
11621                      void * data ATTRIBUTE_UNUSED)
11622 {
11623   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11624     *walk_subtrees = 0;
11625
11626   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11627       && ! TREE_ASM_WRITTEN (*tp))
11628     return *tp;
11629   /* ???  The C++ FE emits debug information for using decls, so
11630      putting gcc_unreachable here falls over.  See PR31899.  For now
11631      be conservative.  */
11632   else if (!cgraph_global_info_ready
11633            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11634     return *tp;
11635   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11636     {
11637       struct varpool_node *node = varpool_node (*tp);
11638       if (!node->needed)
11639         return *tp;
11640     }
11641   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11642            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11643     {
11644       struct cgraph_node *node = cgraph_node (*tp);
11645       if (node->process || TREE_ASM_WRITTEN (*tp))
11646         return *tp;
11647     }
11648   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11649     return *tp;
11650
11651   return NULL_TREE;
11652 }
11653
11654 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11655    for use in a later add_const_value_attribute call.  */
11656
11657 static rtx
11658 rtl_for_decl_init (tree init, tree type)
11659 {
11660   rtx rtl = NULL_RTX;
11661
11662   /* If a variable is initialized with a string constant without embedded
11663      zeros, build CONST_STRING.  */
11664   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11665     {
11666       tree enttype = TREE_TYPE (type);
11667       tree domain = TYPE_DOMAIN (type);
11668       enum machine_mode mode = TYPE_MODE (enttype);
11669
11670       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11671           && domain
11672           && integer_zerop (TYPE_MIN_VALUE (domain))
11673           && compare_tree_int (TYPE_MAX_VALUE (domain),
11674                                TREE_STRING_LENGTH (init) - 1) == 0
11675           && ((size_t) TREE_STRING_LENGTH (init)
11676               == strlen (TREE_STRING_POINTER (init)) + 1))
11677         rtl = gen_rtx_CONST_STRING (VOIDmode,
11678                                     ggc_strdup (TREE_STRING_POINTER (init)));
11679     }
11680   /* Other aggregates, and complex values, could be represented using
11681      CONCAT: FIXME!  */
11682   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11683     ;
11684   /* Vectors only work if their mode is supported by the target.
11685      FIXME: generic vectors ought to work too.  */
11686   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11687     ;
11688   /* If the initializer is something that we know will expand into an
11689      immediate RTL constant, expand it now.  We must be careful not to
11690      reference variables which won't be output.  */
11691   else if (initializer_constant_valid_p (init, type)
11692            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11693     {
11694       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11695          possible.  */
11696       if (TREE_CODE (type) == VECTOR_TYPE)
11697         switch (TREE_CODE (init))
11698           {
11699           case VECTOR_CST:
11700             break;
11701           case CONSTRUCTOR:
11702             if (TREE_CONSTANT (init))
11703               {
11704                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11705                 bool constant_p = true;
11706                 tree value;
11707                 unsigned HOST_WIDE_INT ix;
11708
11709                 /* Even when ctor is constant, it might contain non-*_CST
11710                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11711                    belong into VECTOR_CST nodes.  */
11712                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11713                   if (!CONSTANT_CLASS_P (value))
11714                     {
11715                       constant_p = false;
11716                       break;
11717                     }
11718
11719                 if (constant_p)
11720                   {
11721                     init = build_vector_from_ctor (type, elts);
11722                     break;
11723                   }
11724               }
11725             /* FALLTHRU */
11726
11727           default:
11728             return NULL;
11729           }
11730
11731       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11732
11733       /* If expand_expr returns a MEM, it wasn't immediate.  */
11734       gcc_assert (!rtl || !MEM_P (rtl));
11735     }
11736
11737   return rtl;
11738 }
11739
11740 /* Generate RTL for the variable DECL to represent its location.  */
11741
11742 static rtx
11743 rtl_for_decl_location (tree decl)
11744 {
11745   rtx rtl;
11746
11747   /* Here we have to decide where we are going to say the parameter "lives"
11748      (as far as the debugger is concerned).  We only have a couple of
11749      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11750
11751      DECL_RTL normally indicates where the parameter lives during most of the
11752      activation of the function.  If optimization is enabled however, this
11753      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11754      that the parameter doesn't really live anywhere (as far as the code
11755      generation parts of GCC are concerned) during most of the function's
11756      activation.  That will happen (for example) if the parameter is never
11757      referenced within the function.
11758
11759      We could just generate a location descriptor here for all non-NULL
11760      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11761      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11762      where DECL_RTL is NULL or is a pseudo-reg.
11763
11764      Note however that we can only get away with using DECL_INCOMING_RTL as
11765      a backup substitute for DECL_RTL in certain limited cases.  In cases
11766      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11767      we can be sure that the parameter was passed using the same type as it is
11768      declared to have within the function, and that its DECL_INCOMING_RTL
11769      points us to a place where a value of that type is passed.
11770
11771      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11772      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11773      because in these cases DECL_INCOMING_RTL points us to a value of some
11774      type which is *different* from the type of the parameter itself.  Thus,
11775      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11776      such cases, the debugger would end up (for example) trying to fetch a
11777      `float' from a place which actually contains the first part of a
11778      `double'.  That would lead to really incorrect and confusing
11779      output at debug-time.
11780
11781      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11782      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11783      are a couple of exceptions however.  On little-endian machines we can
11784      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11785      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11786      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11787      when (on a little-endian machine) a non-prototyped function has a
11788      parameter declared to be of type `short' or `char'.  In such cases,
11789      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11790      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11791      passed `int' value.  If the debugger then uses that address to fetch
11792      a `short' or a `char' (on a little-endian machine) the result will be
11793      the correct data, so we allow for such exceptional cases below.
11794
11795      Note that our goal here is to describe the place where the given formal
11796      parameter lives during most of the function's activation (i.e. between the
11797      end of the prologue and the start of the epilogue).  We'll do that as best
11798      as we can. Note however that if the given formal parameter is modified
11799      sometime during the execution of the function, then a stack backtrace (at
11800      debug-time) will show the function as having been called with the *new*
11801      value rather than the value which was originally passed in.  This happens
11802      rarely enough that it is not a major problem, but it *is* a problem, and
11803      I'd like to fix it.
11804
11805      A future version of dwarf2out.c may generate two additional attributes for
11806      any given DW_TAG_formal_parameter DIE which will describe the "passed
11807      type" and the "passed location" for the given formal parameter in addition
11808      to the attributes we now generate to indicate the "declared type" and the
11809      "active location" for each parameter.  This additional set of attributes
11810      could be used by debuggers for stack backtraces. Separately, note that
11811      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11812      This happens (for example) for inlined-instances of inline function formal
11813      parameters which are never referenced.  This really shouldn't be
11814      happening.  All PARM_DECL nodes should get valid non-NULL
11815      DECL_INCOMING_RTL values.  FIXME.  */
11816
11817   /* Use DECL_RTL as the "location" unless we find something better.  */
11818   rtl = DECL_RTL_IF_SET (decl);
11819
11820   /* When generating abstract instances, ignore everything except
11821      constants, symbols living in memory, and symbols living in
11822      fixed registers.  */
11823   if (! reload_completed)
11824     {
11825       if (rtl
11826           && (CONSTANT_P (rtl)
11827               || (MEM_P (rtl)
11828                   && CONSTANT_P (XEXP (rtl, 0)))
11829               || (REG_P (rtl)
11830                   && TREE_CODE (decl) == VAR_DECL
11831                   && TREE_STATIC (decl))))
11832         {
11833           rtl = targetm.delegitimize_address (rtl);
11834           return rtl;
11835         }
11836       rtl = NULL_RTX;
11837     }
11838   else if (TREE_CODE (decl) == PARM_DECL)
11839     {
11840       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11841         {
11842           tree declared_type = TREE_TYPE (decl);
11843           tree passed_type = DECL_ARG_TYPE (decl);
11844           enum machine_mode dmode = TYPE_MODE (declared_type);
11845           enum machine_mode pmode = TYPE_MODE (passed_type);
11846
11847           /* This decl represents a formal parameter which was optimized out.
11848              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11849              all cases where (rtl == NULL_RTX) just below.  */
11850           if (dmode == pmode)
11851             rtl = DECL_INCOMING_RTL (decl);
11852           else if (SCALAR_INT_MODE_P (dmode)
11853                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11854                    && DECL_INCOMING_RTL (decl))
11855             {
11856               rtx inc = DECL_INCOMING_RTL (decl);
11857               if (REG_P (inc))
11858                 rtl = inc;
11859               else if (MEM_P (inc))
11860                 {
11861                   if (BYTES_BIG_ENDIAN)
11862                     rtl = adjust_address_nv (inc, dmode,
11863                                              GET_MODE_SIZE (pmode)
11864                                              - GET_MODE_SIZE (dmode));
11865                   else
11866                     rtl = inc;
11867                 }
11868             }
11869         }
11870
11871       /* If the parm was passed in registers, but lives on the stack, then
11872          make a big endian correction if the mode of the type of the
11873          parameter is not the same as the mode of the rtl.  */
11874       /* ??? This is the same series of checks that are made in dbxout.c before
11875          we reach the big endian correction code there.  It isn't clear if all
11876          of these checks are necessary here, but keeping them all is the safe
11877          thing to do.  */
11878       else if (MEM_P (rtl)
11879                && XEXP (rtl, 0) != const0_rtx
11880                && ! CONSTANT_P (XEXP (rtl, 0))
11881                /* Not passed in memory.  */
11882                && !MEM_P (DECL_INCOMING_RTL (decl))
11883                /* Not passed by invisible reference.  */
11884                && (!REG_P (XEXP (rtl, 0))
11885                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11886                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11887 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11888                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11889 #endif
11890                      )
11891                /* Big endian correction check.  */
11892                && BYTES_BIG_ENDIAN
11893                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11894                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11895                    < UNITS_PER_WORD))
11896         {
11897           int offset = (UNITS_PER_WORD
11898                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11899
11900           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11901                              plus_constant (XEXP (rtl, 0), offset));
11902         }
11903     }
11904   else if (TREE_CODE (decl) == VAR_DECL
11905            && rtl
11906            && MEM_P (rtl)
11907            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11908            && BYTES_BIG_ENDIAN)
11909     {
11910       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11911       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11912
11913       /* If a variable is declared "register" yet is smaller than
11914          a register, then if we store the variable to memory, it
11915          looks like we're storing a register-sized value, when in
11916          fact we are not.  We need to adjust the offset of the
11917          storage location to reflect the actual value's bytes,
11918          else gdb will not be able to display it.  */
11919       if (rsize > dsize)
11920         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11921                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11922     }
11923
11924   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11925      and will have been substituted directly into all expressions that use it.
11926      C does not have such a concept, but C++ and other languages do.  */
11927   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11928     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11929
11930   if (rtl)
11931     rtl = targetm.delegitimize_address (rtl);
11932
11933   /* If we don't look past the constant pool, we risk emitting a
11934      reference to a constant pool entry that isn't referenced from
11935      code, and thus is not emitted.  */
11936   if (rtl)
11937     rtl = avoid_constant_pool_reference (rtl);
11938
11939   return rtl;
11940 }
11941
11942 /* We need to figure out what section we should use as the base for the
11943    address ranges where a given location is valid.
11944    1. If this particular DECL has a section associated with it, use that.
11945    2. If this function has a section associated with it, use that.
11946    3. Otherwise, use the text section.
11947    XXX: If you split a variable across multiple sections, we won't notice.  */
11948
11949 static const char *
11950 secname_for_decl (const_tree decl)
11951 {
11952   const char *secname;
11953
11954   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11955     {
11956       tree sectree = DECL_SECTION_NAME (decl);
11957       secname = TREE_STRING_POINTER (sectree);
11958     }
11959   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11960     {
11961       tree sectree = DECL_SECTION_NAME (current_function_decl);
11962       secname = TREE_STRING_POINTER (sectree);
11963     }
11964   else if (cfun && in_cold_section_p)
11965     secname = crtl->subsections.cold_section_label;
11966   else
11967     secname = text_section_label;
11968
11969   return secname;
11970 }
11971
11972 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11973    returned.  If so, the decl for the COMMON block is returned, and the
11974    value is the offset into the common block for the symbol.  */
11975
11976 static tree
11977 fortran_common (tree decl, HOST_WIDE_INT *value)
11978 {
11979   tree val_expr, cvar;
11980   enum machine_mode mode;
11981   HOST_WIDE_INT bitsize, bitpos;
11982   tree offset;
11983   int volatilep = 0, unsignedp = 0;
11984
11985   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11986      it does not have a value (the offset into the common area), or if it
11987      is thread local (as opposed to global) then it isn't common, and shouldn't
11988      be handled as such.  */
11989   if (TREE_CODE (decl) != VAR_DECL
11990       || !TREE_PUBLIC (decl)
11991       || !TREE_STATIC (decl)
11992       || !DECL_HAS_VALUE_EXPR_P (decl)
11993       || !is_fortran ())
11994     return NULL_TREE;
11995
11996   val_expr = DECL_VALUE_EXPR (decl);
11997   if (TREE_CODE (val_expr) != COMPONENT_REF)
11998     return NULL_TREE;
11999
12000   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
12001                               &mode, &unsignedp, &volatilep, true);
12002
12003   if (cvar == NULL_TREE
12004       || TREE_CODE (cvar) != VAR_DECL
12005       || DECL_ARTIFICIAL (cvar)
12006       || !TREE_PUBLIC (cvar))
12007     return NULL_TREE;
12008
12009   *value = 0;
12010   if (offset != NULL)
12011     {
12012       if (!host_integerp (offset, 0))
12013         return NULL_TREE;
12014       *value = tree_low_cst (offset, 0);
12015     }
12016   if (bitpos != 0)
12017     *value += bitpos / BITS_PER_UNIT;
12018
12019   return cvar;
12020 }
12021
12022 /* Dereference a location expression LOC if DECL is passed by invisible
12023    reference.  */
12024
12025 static dw_loc_descr_ref
12026 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12027 {
12028   HOST_WIDE_INT size;
12029   enum dwarf_location_atom op;
12030
12031   if (loc == NULL)
12032     return NULL;
12033
12034   if ((TREE_CODE (decl) != PARM_DECL
12035        && TREE_CODE (decl) != RESULT_DECL
12036        && TREE_CODE (decl) != VAR_DECL)
12037       || !DECL_BY_REFERENCE (decl))
12038     return loc;
12039
12040   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12041      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12042      location expression is considered to be address of a memory location,
12043      rather than the register itself.  */
12044   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12045        || loc->dw_loc_opc == DW_OP_regx)
12046       && (loc->dw_loc_next == NULL
12047           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12048               && loc->dw_loc_next->dw_loc_next == NULL)))
12049     {
12050       if (loc->dw_loc_opc == DW_OP_regx)
12051         {
12052           loc->dw_loc_opc = DW_OP_bregx;
12053           loc->dw_loc_oprnd2.v.val_int = 0;
12054         }
12055       else
12056         {
12057           loc->dw_loc_opc
12058             = (enum dwarf_location_atom)
12059               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12060           loc->dw_loc_oprnd1.v.val_int = 0;
12061         }
12062       return loc;
12063     }
12064
12065   size = int_size_in_bytes (TREE_TYPE (decl));
12066   if (size > DWARF2_ADDR_SIZE || size == -1)
12067     return 0;
12068   else if (size == DWARF2_ADDR_SIZE)
12069     op = DW_OP_deref;
12070   else
12071     op = DW_OP_deref_size;
12072   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12073   return loc;
12074 }
12075
12076 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
12077    data attribute for a variable or a parameter.  We generate the
12078    DW_AT_const_value attribute only in those cases where the given variable
12079    or parameter does not have a true "location" either in memory or in a
12080    register.  This can happen (for example) when a constant is passed as an
12081    actual argument in a call to an inline function.  (It's possible that
12082    these things can crop up in other ways also.)  Note that one type of
12083    constant value which can be passed into an inlined function is a constant
12084    pointer.  This can happen for example if an actual argument in an inlined
12085    function call evaluates to a compile-time constant address.  */
12086
12087 static void
12088 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
12089                                        enum dwarf_attribute attr)
12090 {
12091   rtx rtl;
12092   dw_loc_descr_ref descr;
12093   var_loc_list *loc_list;
12094   struct var_loc_node *node;
12095   if (TREE_CODE (decl) == ERROR_MARK)
12096     return;
12097
12098   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
12099               || TREE_CODE (decl) == RESULT_DECL);
12100
12101   /* See if we possibly have multiple locations for this variable.  */
12102   loc_list = lookup_decl_loc (decl);
12103
12104   /* If it truly has multiple locations, the first and last node will
12105      differ.  */
12106   if (loc_list && loc_list->first != loc_list->last)
12107     {
12108       const char *endname, *secname;
12109       dw_loc_list_ref list;
12110       rtx varloc;
12111       enum var_init_status initialized;
12112
12113       /* Now that we know what section we are using for a base,
12114          actually construct the list of locations.
12115          The first location information is what is passed to the
12116          function that creates the location list, and the remaining
12117          locations just get added on to that list.
12118          Note that we only know the start address for a location
12119          (IE location changes), so to build the range, we use
12120          the range [current location start, next location start].
12121          This means we have to special case the last node, and generate
12122          a range of [last location start, end of function label].  */
12123
12124       node = loc_list->first;
12125       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12126       secname = secname_for_decl (decl);
12127
12128       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12129         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12130       else
12131         initialized = VAR_INIT_STATUS_INITIALIZED;
12132
12133       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
12134       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12135       node = node->next;
12136
12137       for (; node->next; node = node->next)
12138         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12139           {
12140             /* The variable has a location between NODE->LABEL and
12141                NODE->NEXT->LABEL.  */
12142             enum var_init_status initialized =
12143               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12144             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12145             descr = loc_by_reference (loc_descriptor (varloc, initialized),
12146                                       decl);
12147             add_loc_descr_to_loc_list (&list, descr,
12148                                        node->label, node->next->label, secname);
12149           }
12150
12151       /* If the variable has a location at the last label
12152          it keeps its location until the end of function.  */
12153       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12154         {
12155           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12156           enum var_init_status initialized =
12157             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12158
12159           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12160           if (!current_function_decl)
12161             endname = text_end_label;
12162           else
12163             {
12164               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12165                                            current_function_funcdef_no);
12166               endname = ggc_strdup (label_id);
12167             }
12168           descr = loc_by_reference (loc_descriptor (varloc, initialized),
12169                                     decl);
12170           add_loc_descr_to_loc_list (&list, descr,
12171                                      node->label, endname, secname);
12172         }
12173
12174       /* Finally, add the location list to the DIE, and we are done.  */
12175       add_AT_loc_list (die, attr, list);
12176       return;
12177     }
12178
12179   /* Try to get some constant RTL for this decl, and use that as the value of
12180      the location.  */
12181
12182   rtl = rtl_for_decl_location (decl);
12183   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
12184     {
12185       add_const_value_attribute (die, rtl);
12186       return;
12187     }
12188
12189   /* If we have tried to generate the location otherwise, and it
12190      didn't work out (we wouldn't be here if we did), and we have a one entry
12191      location list, try generating a location from that.  */
12192   if (loc_list && loc_list->first)
12193     {
12194       enum var_init_status status;
12195       node = loc_list->first;
12196       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12197       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
12198       if (descr)
12199         {
12200           descr = loc_by_reference (descr, decl);
12201           add_AT_location_description (die, attr, descr);
12202           return;
12203         }
12204     }
12205
12206   /* We couldn't get any rtl, so try directly generating the location
12207      description from the tree.  */
12208   descr = loc_descriptor_from_tree (decl);
12209   if (descr)
12210     {
12211       descr = loc_by_reference (descr, decl);
12212       add_AT_location_description (die, attr, descr);
12213       return;
12214     }
12215   /* None of that worked, so it must not really have a location;
12216      try adding a constant value attribute from the DECL_INITIAL.  */
12217   tree_add_const_value_attribute (die, decl);
12218 }
12219
12220 /* Add VARIABLE and DIE into deferred locations list.  */
12221
12222 static void
12223 defer_location (tree variable, dw_die_ref die)
12224 {
12225   deferred_locations entry;
12226   entry.variable = variable;
12227   entry.die = die;
12228   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
12229 }
12230
12231 /* Helper function for tree_add_const_value_attribute.  Natively encode
12232    initializer INIT into an array.  Return true if successful.  */
12233
12234 static bool
12235 native_encode_initializer (tree init, unsigned char *array, int size)
12236 {
12237   tree type;
12238
12239   if (init == NULL_TREE)
12240     return false;
12241
12242   STRIP_NOPS (init);
12243   switch (TREE_CODE (init))
12244     {
12245     case STRING_CST:
12246       type = TREE_TYPE (init);
12247       if (TREE_CODE (type) == ARRAY_TYPE)
12248         {
12249           tree enttype = TREE_TYPE (type);
12250           enum machine_mode mode = TYPE_MODE (enttype);
12251
12252           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
12253             return false;
12254           if (int_size_in_bytes (type) != size)
12255             return false;
12256           if (size > TREE_STRING_LENGTH (init))
12257             {
12258               memcpy (array, TREE_STRING_POINTER (init),
12259                       TREE_STRING_LENGTH (init));
12260               memset (array + TREE_STRING_LENGTH (init),
12261                       '\0', size - TREE_STRING_LENGTH (init));
12262             }
12263           else
12264             memcpy (array, TREE_STRING_POINTER (init), size);
12265           return true;
12266         }
12267       return false;
12268     case CONSTRUCTOR:
12269       type = TREE_TYPE (init);
12270       if (int_size_in_bytes (type) != size)
12271         return false;
12272       if (TREE_CODE (type) == ARRAY_TYPE)
12273         {
12274           HOST_WIDE_INT min_index;
12275           unsigned HOST_WIDE_INT cnt;
12276           int curpos = 0, fieldsize;
12277           constructor_elt *ce;
12278
12279           if (TYPE_DOMAIN (type) == NULL_TREE
12280               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12281             return false;
12282
12283           fieldsize = int_size_in_bytes (TREE_TYPE (type));
12284           if (fieldsize <= 0)
12285             return false;
12286
12287           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12288           memset (array, '\0', size);
12289           for (cnt = 0;
12290                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12291                cnt++)
12292             {
12293               tree val = ce->value;
12294               tree index = ce->index;
12295               int pos = curpos;
12296               if (index && TREE_CODE (index) == RANGE_EXPR)
12297                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12298                       * fieldsize;
12299               else if (index)
12300                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12301
12302               if (val)
12303                 {
12304                   STRIP_NOPS (val);
12305                   if (!native_encode_initializer (val, array + pos, fieldsize))
12306                     return false;
12307                 }
12308               curpos = pos + fieldsize;
12309               if (index && TREE_CODE (index) == RANGE_EXPR)
12310                 {
12311                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12312                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12313                   while (count > 0)
12314                     {
12315                       if (val)
12316                         memcpy (array + curpos, array + pos, fieldsize);
12317                       curpos += fieldsize;
12318                     }
12319                 }
12320               gcc_assert (curpos <= size);
12321             }
12322           return true;
12323         }
12324       else if (TREE_CODE (type) == RECORD_TYPE
12325                || TREE_CODE (type) == UNION_TYPE)
12326         {
12327           tree field = NULL_TREE;
12328           unsigned HOST_WIDE_INT cnt;
12329           constructor_elt *ce;
12330
12331           if (int_size_in_bytes (type) != size)
12332             return false;
12333
12334           if (TREE_CODE (type) == RECORD_TYPE)
12335             field = TYPE_FIELDS (type);
12336
12337           for (cnt = 0;
12338                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12339                cnt++, field = field ? TREE_CHAIN (field) : 0)
12340             {
12341               tree val = ce->value;
12342               int pos, fieldsize;
12343
12344               if (ce->index != 0)
12345                 field = ce->index;
12346
12347               if (val)
12348                 STRIP_NOPS (val);
12349
12350               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12351                 return false;
12352
12353               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12354                   && TYPE_DOMAIN (TREE_TYPE (field))
12355                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12356                 return false;
12357               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12358                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12359                 return false;
12360               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12361               pos = int_byte_position (field);
12362               gcc_assert (pos + fieldsize <= size);
12363               if (val
12364                   && !native_encode_initializer (val, array + pos, fieldsize))
12365                 return false;
12366             }
12367           return true;
12368         }
12369       return false;
12370     case VIEW_CONVERT_EXPR:
12371     case NON_LVALUE_EXPR:
12372       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12373     default:
12374       return native_encode_expr (init, array, size) == size;
12375     }
12376 }
12377
12378 /* If we don't have a copy of this variable in memory for some reason (such
12379    as a C++ member constant that doesn't have an out-of-line definition),
12380    we should tell the debugger about the constant value.  */
12381
12382 static void
12383 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12384 {
12385   tree init;
12386   tree type = TREE_TYPE (decl);
12387   rtx rtl;
12388
12389   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12390     return;
12391
12392   init = DECL_INITIAL (decl);
12393   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12394     /* OK */;
12395   else
12396     return;
12397
12398   rtl = rtl_for_decl_init (init, type);
12399   if (rtl)
12400     add_const_value_attribute (var_die, rtl);
12401   /* If the host and target are sane, try harder.  */
12402   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12403            && initializer_constant_valid_p (init, type))
12404     {
12405       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12406       if (size > 0 && (int) size == size)
12407         {
12408           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12409
12410           if (native_encode_initializer (init, array, size))
12411             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12412         }
12413     }
12414 }
12415
12416 /* Convert the CFI instructions for the current function into a
12417    location list.  This is used for DW_AT_frame_base when we targeting
12418    a dwarf2 consumer that does not support the dwarf3
12419    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12420    expressions.  */
12421
12422 static dw_loc_list_ref
12423 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12424 {
12425   dw_fde_ref fde;
12426   dw_loc_list_ref list, *list_tail;
12427   dw_cfi_ref cfi;
12428   dw_cfa_location last_cfa, next_cfa;
12429   const char *start_label, *last_label, *section;
12430   dw_cfa_location remember;
12431
12432   fde = current_fde ();
12433   gcc_assert (fde != NULL);
12434
12435   section = secname_for_decl (current_function_decl);
12436   list_tail = &list;
12437   list = NULL;
12438
12439   memset (&next_cfa, 0, sizeof (next_cfa));
12440   next_cfa.reg = INVALID_REGNUM;
12441   remember = next_cfa;
12442
12443   start_label = fde->dw_fde_begin;
12444
12445   /* ??? Bald assumption that the CIE opcode list does not contain
12446      advance opcodes.  */
12447   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12448     lookup_cfa_1 (cfi, &next_cfa, &remember);
12449
12450   last_cfa = next_cfa;
12451   last_label = start_label;
12452
12453   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12454     switch (cfi->dw_cfi_opc)
12455       {
12456       case DW_CFA_set_loc:
12457       case DW_CFA_advance_loc1:
12458       case DW_CFA_advance_loc2:
12459       case DW_CFA_advance_loc4:
12460         if (!cfa_equal_p (&last_cfa, &next_cfa))
12461           {
12462             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12463                                        start_label, last_label, section,
12464                                        list == NULL);
12465
12466             list_tail = &(*list_tail)->dw_loc_next;
12467             last_cfa = next_cfa;
12468             start_label = last_label;
12469           }
12470         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12471         break;
12472
12473       case DW_CFA_advance_loc:
12474         /* The encoding is complex enough that we should never emit this.  */
12475         gcc_unreachable ();
12476
12477       default:
12478         lookup_cfa_1 (cfi, &next_cfa, &remember);
12479         break;
12480       }
12481
12482   if (!cfa_equal_p (&last_cfa, &next_cfa))
12483     {
12484       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12485                                  start_label, last_label, section,
12486                                  list == NULL);
12487       list_tail = &(*list_tail)->dw_loc_next;
12488       start_label = last_label;
12489     }
12490   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12491                              start_label, fde->dw_fde_end, section,
12492                              list == NULL);
12493
12494   return list;
12495 }
12496
12497 /* Compute a displacement from the "steady-state frame pointer" to the
12498    frame base (often the same as the CFA), and store it in
12499    frame_pointer_fb_offset.  OFFSET is added to the displacement
12500    before the latter is negated.  */
12501
12502 static void
12503 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12504 {
12505   rtx reg, elim;
12506
12507 #ifdef FRAME_POINTER_CFA_OFFSET
12508   reg = frame_pointer_rtx;
12509   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12510 #else
12511   reg = arg_pointer_rtx;
12512   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12513 #endif
12514
12515   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12516   if (GET_CODE (elim) == PLUS)
12517     {
12518       offset += INTVAL (XEXP (elim, 1));
12519       elim = XEXP (elim, 0);
12520     }
12521
12522   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12523                && (elim == hard_frame_pointer_rtx
12524                    || elim == stack_pointer_rtx))
12525               || elim == (frame_pointer_needed
12526                           ? hard_frame_pointer_rtx
12527                           : stack_pointer_rtx));
12528
12529   frame_pointer_fb_offset = -offset;
12530 }
12531
12532 /* Generate a DW_AT_name attribute given some string value to be included as
12533    the value of the attribute.  */
12534
12535 static void
12536 add_name_attribute (dw_die_ref die, const char *name_string)
12537 {
12538   if (name_string != NULL && *name_string != 0)
12539     {
12540       if (demangle_name_func)
12541         name_string = (*demangle_name_func) (name_string);
12542
12543       add_AT_string (die, DW_AT_name, name_string);
12544     }
12545 }
12546
12547 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12548
12549 static void
12550 add_comp_dir_attribute (dw_die_ref die)
12551 {
12552   const char *wd = get_src_pwd ();
12553   if (wd != NULL)
12554     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12555 }
12556
12557 /* Given a tree node describing an array bound (either lower or upper) output
12558    a representation for that bound.  */
12559
12560 static void
12561 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12562 {
12563   switch (TREE_CODE (bound))
12564     {
12565     case ERROR_MARK:
12566       return;
12567
12568     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12569     case INTEGER_CST:
12570       if (! host_integerp (bound, 0)
12571           || (bound_attr == DW_AT_lower_bound
12572               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12573                   || (is_fortran () && integer_onep (bound)))))
12574         /* Use the default.  */
12575         ;
12576       else
12577         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12578       break;
12579
12580     CASE_CONVERT:
12581     case VIEW_CONVERT_EXPR:
12582       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12583       break;
12584
12585     case SAVE_EXPR:
12586       break;
12587
12588     case VAR_DECL:
12589     case PARM_DECL:
12590     case RESULT_DECL:
12591       {
12592         dw_die_ref decl_die = lookup_decl_die (bound);
12593         dw_loc_descr_ref loc;
12594
12595         /* ??? Can this happen, or should the variable have been bound
12596            first?  Probably it can, since I imagine that we try to create
12597            the types of parameters in the order in which they exist in
12598            the list, and won't have created a forward reference to a
12599            later parameter.  */
12600         if (decl_die != NULL)
12601           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12602         else
12603           {
12604             loc = loc_descriptor_from_tree_1 (bound, 0);
12605             add_AT_location_description (subrange_die, bound_attr, loc);
12606           }
12607         break;
12608       }
12609
12610     default:
12611       {
12612         /* Otherwise try to create a stack operation procedure to
12613            evaluate the value of the array bound.  */
12614
12615         dw_die_ref ctx, decl_die;
12616         dw_loc_descr_ref loc;
12617
12618         loc = loc_descriptor_from_tree (bound);
12619         if (loc == NULL)
12620           break;
12621
12622         if (current_function_decl == 0)
12623           ctx = comp_unit_die;
12624         else
12625           ctx = lookup_decl_die (current_function_decl);
12626
12627         decl_die = new_die (DW_TAG_variable, ctx, bound);
12628         add_AT_flag (decl_die, DW_AT_artificial, 1);
12629         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12630         add_AT_loc (decl_die, DW_AT_location, loc);
12631
12632         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12633         break;
12634       }
12635     }
12636 }
12637
12638 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12639    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12640    Note that the block of subscript information for an array type also
12641    includes information about the element type of the given array type.  */
12642
12643 static void
12644 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12645 {
12646   unsigned dimension_number;
12647   tree lower, upper;
12648   dw_die_ref subrange_die;
12649
12650   for (dimension_number = 0;
12651        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12652        type = TREE_TYPE (type), dimension_number++)
12653     {
12654       tree domain = TYPE_DOMAIN (type);
12655
12656       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12657         break;
12658
12659       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12660          and (in GNU C only) variable bounds.  Handle all three forms
12661          here.  */
12662       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12663       if (domain)
12664         {
12665           /* We have an array type with specified bounds.  */
12666           lower = TYPE_MIN_VALUE (domain);
12667           upper = TYPE_MAX_VALUE (domain);
12668
12669           /* Define the index type.  */
12670           if (TREE_TYPE (domain))
12671             {
12672               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12673                  TREE_TYPE field.  We can't emit debug info for this
12674                  because it is an unnamed integral type.  */
12675               if (TREE_CODE (domain) == INTEGER_TYPE
12676                   && TYPE_NAME (domain) == NULL_TREE
12677                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12678                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12679                 ;
12680               else
12681                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12682                                     type_die);
12683             }
12684
12685           /* ??? If upper is NULL, the array has unspecified length,
12686              but it does have a lower bound.  This happens with Fortran
12687                dimension arr(N:*)
12688              Since the debugger is definitely going to need to know N
12689              to produce useful results, go ahead and output the lower
12690              bound solo, and hope the debugger can cope.  */
12691
12692           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12693           if (upper)
12694             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12695         }
12696
12697       /* Otherwise we have an array type with an unspecified length.  The
12698          DWARF-2 spec does not say how to handle this; let's just leave out the
12699          bounds.  */
12700     }
12701 }
12702
12703 static void
12704 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12705 {
12706   unsigned size;
12707
12708   switch (TREE_CODE (tree_node))
12709     {
12710     case ERROR_MARK:
12711       size = 0;
12712       break;
12713     case ENUMERAL_TYPE:
12714     case RECORD_TYPE:
12715     case UNION_TYPE:
12716     case QUAL_UNION_TYPE:
12717       size = int_size_in_bytes (tree_node);
12718       break;
12719     case FIELD_DECL:
12720       /* For a data member of a struct or union, the DW_AT_byte_size is
12721          generally given as the number of bytes normally allocated for an
12722          object of the *declared* type of the member itself.  This is true
12723          even for bit-fields.  */
12724       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12725       break;
12726     default:
12727       gcc_unreachable ();
12728     }
12729
12730   /* Note that `size' might be -1 when we get to this point.  If it is, that
12731      indicates that the byte size of the entity in question is variable.  We
12732      have no good way of expressing this fact in Dwarf at the present time,
12733      so just let the -1 pass on through.  */
12734   add_AT_unsigned (die, DW_AT_byte_size, size);
12735 }
12736
12737 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12738    which specifies the distance in bits from the highest order bit of the
12739    "containing object" for the bit-field to the highest order bit of the
12740    bit-field itself.
12741
12742    For any given bit-field, the "containing object" is a hypothetical object
12743    (of some integral or enum type) within which the given bit-field lives.  The
12744    type of this hypothetical "containing object" is always the same as the
12745    declared type of the individual bit-field itself.  The determination of the
12746    exact location of the "containing object" for a bit-field is rather
12747    complicated.  It's handled by the `field_byte_offset' function (above).
12748
12749    Note that it is the size (in bytes) of the hypothetical "containing object"
12750    which will be given in the DW_AT_byte_size attribute for this bit-field.
12751    (See `byte_size_attribute' above).  */
12752
12753 static inline void
12754 add_bit_offset_attribute (dw_die_ref die, tree decl)
12755 {
12756   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12757   tree type = DECL_BIT_FIELD_TYPE (decl);
12758   HOST_WIDE_INT bitpos_int;
12759   HOST_WIDE_INT highest_order_object_bit_offset;
12760   HOST_WIDE_INT highest_order_field_bit_offset;
12761   HOST_WIDE_INT unsigned bit_offset;
12762
12763   /* Must be a field and a bit field.  */
12764   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12765
12766   /* We can't yet handle bit-fields whose offsets are variable, so if we
12767      encounter such things, just return without generating any attribute
12768      whatsoever.  Likewise for variable or too large size.  */
12769   if (! host_integerp (bit_position (decl), 0)
12770       || ! host_integerp (DECL_SIZE (decl), 1))
12771     return;
12772
12773   bitpos_int = int_bit_position (decl);
12774
12775   /* Note that the bit offset is always the distance (in bits) from the
12776      highest-order bit of the "containing object" to the highest-order bit of
12777      the bit-field itself.  Since the "high-order end" of any object or field
12778      is different on big-endian and little-endian machines, the computation
12779      below must take account of these differences.  */
12780   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12781   highest_order_field_bit_offset = bitpos_int;
12782
12783   if (! BYTES_BIG_ENDIAN)
12784     {
12785       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12786       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12787     }
12788
12789   bit_offset
12790     = (! BYTES_BIG_ENDIAN
12791        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12792        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12793
12794   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12795 }
12796
12797 /* For a FIELD_DECL node which represents a bit field, output an attribute
12798    which specifies the length in bits of the given field.  */
12799
12800 static inline void
12801 add_bit_size_attribute (dw_die_ref die, tree decl)
12802 {
12803   /* Must be a field and a bit field.  */
12804   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12805               && DECL_BIT_FIELD_TYPE (decl));
12806
12807   if (host_integerp (DECL_SIZE (decl), 1))
12808     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12809 }
12810
12811 /* If the compiled language is ANSI C, then add a 'prototyped'
12812    attribute, if arg types are given for the parameters of a function.  */
12813
12814 static inline void
12815 add_prototyped_attribute (dw_die_ref die, tree func_type)
12816 {
12817   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12818       && TYPE_ARG_TYPES (func_type) != NULL)
12819     add_AT_flag (die, DW_AT_prototyped, 1);
12820 }
12821
12822 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12823    by looking in either the type declaration or object declaration
12824    equate table.  */
12825
12826 static inline dw_die_ref
12827 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12828 {
12829   dw_die_ref origin_die = NULL;
12830
12831   if (TREE_CODE (origin) != FUNCTION_DECL)
12832     {
12833       /* We may have gotten separated from the block for the inlined
12834          function, if we're in an exception handler or some such; make
12835          sure that the abstract function has been written out.
12836
12837          Doing this for nested functions is wrong, however; functions are
12838          distinct units, and our context might not even be inline.  */
12839       tree fn = origin;
12840
12841       if (TYPE_P (fn))
12842         fn = TYPE_STUB_DECL (fn);
12843
12844       fn = decl_function_context (fn);
12845       if (fn)
12846         dwarf2out_abstract_function (fn);
12847     }
12848
12849   if (DECL_P (origin))
12850     origin_die = lookup_decl_die (origin);
12851   else if (TYPE_P (origin))
12852     origin_die = lookup_type_die (origin);
12853
12854   /* XXX: Functions that are never lowered don't always have correct block
12855      trees (in the case of java, they simply have no block tree, in some other
12856      languages).  For these functions, there is nothing we can really do to
12857      output correct debug info for inlined functions in all cases.  Rather
12858      than die, we'll just produce deficient debug info now, in that we will
12859      have variables without a proper abstract origin.  In the future, when all
12860      functions are lowered, we should re-add a gcc_assert (origin_die)
12861      here.  */
12862
12863   if (origin_die)
12864     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12865   return origin_die;
12866 }
12867
12868 /* We do not currently support the pure_virtual attribute.  */
12869
12870 static inline void
12871 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12872 {
12873   if (DECL_VINDEX (func_decl))
12874     {
12875       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12876
12877       if (host_integerp (DECL_VINDEX (func_decl), 0))
12878         add_AT_loc (die, DW_AT_vtable_elem_location,
12879                     new_loc_descr (DW_OP_constu,
12880                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12881                                    0));
12882
12883       /* GNU extension: Record what type this method came from originally.  */
12884       if (debug_info_level > DINFO_LEVEL_TERSE)
12885         add_AT_die_ref (die, DW_AT_containing_type,
12886                         lookup_type_die (DECL_CONTEXT (func_decl)));
12887     }
12888 }
12889 \f
12890 /* Add source coordinate attributes for the given decl.  */
12891
12892 static void
12893 add_src_coords_attributes (dw_die_ref die, tree decl)
12894 {
12895   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12896
12897   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12898   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12899 }
12900
12901 /* Add a DW_AT_name attribute and source coordinate attribute for the
12902    given decl, but only if it actually has a name.  */
12903
12904 static void
12905 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12906 {
12907   tree decl_name;
12908
12909   decl_name = DECL_NAME (decl);
12910   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12911     {
12912       add_name_attribute (die, dwarf2_name (decl, 0));
12913       if (! DECL_ARTIFICIAL (decl))
12914         add_src_coords_attributes (die, decl);
12915
12916       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12917           && TREE_PUBLIC (decl)
12918           && !DECL_ABSTRACT (decl)
12919           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12920           && !is_fortran ())
12921         {
12922           /* Defer until we have an assembler name set.  */
12923           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
12924             {
12925               limbo_die_node *asm_name;
12926
12927               asm_name = GGC_CNEW (limbo_die_node);
12928               asm_name->die = die;
12929               asm_name->created_for = decl;
12930               asm_name->next = deferred_asm_name;
12931               deferred_asm_name = asm_name;
12932             }
12933           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
12934             add_AT_string (die, DW_AT_MIPS_linkage_name,
12935                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12936         }
12937     }
12938
12939 #ifdef VMS_DEBUGGING_INFO
12940   /* Get the function's name, as described by its RTL.  This may be different
12941      from the DECL_NAME name used in the source file.  */
12942   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12943     {
12944       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12945                    XEXP (DECL_RTL (decl), 0));
12946       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12947     }
12948 #endif
12949 }
12950
12951 /* Push a new declaration scope.  */
12952
12953 static void
12954 push_decl_scope (tree scope)
12955 {
12956   VEC_safe_push (tree, gc, decl_scope_table, scope);
12957 }
12958
12959 /* Pop a declaration scope.  */
12960
12961 static inline void
12962 pop_decl_scope (void)
12963 {
12964   VEC_pop (tree, decl_scope_table);
12965 }
12966
12967 /* Return the DIE for the scope that immediately contains this type.
12968    Non-named types get global scope.  Named types nested in other
12969    types get their containing scope if it's open, or global scope
12970    otherwise.  All other types (i.e. function-local named types) get
12971    the current active scope.  */
12972
12973 static dw_die_ref
12974 scope_die_for (tree t, dw_die_ref context_die)
12975 {
12976   dw_die_ref scope_die = NULL;
12977   tree containing_scope;
12978   int i;
12979
12980   /* Non-types always go in the current scope.  */
12981   gcc_assert (TYPE_P (t));
12982
12983   containing_scope = TYPE_CONTEXT (t);
12984
12985   /* Use the containing namespace if it was passed in (for a declaration).  */
12986   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12987     {
12988       if (context_die == lookup_decl_die (containing_scope))
12989         /* OK */;
12990       else
12991         containing_scope = NULL_TREE;
12992     }
12993
12994   /* Ignore function type "scopes" from the C frontend.  They mean that
12995      a tagged type is local to a parmlist of a function declarator, but
12996      that isn't useful to DWARF.  */
12997   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12998     containing_scope = NULL_TREE;
12999
13000   if (containing_scope == NULL_TREE)
13001     scope_die = comp_unit_die;
13002   else if (TYPE_P (containing_scope))
13003     {
13004       /* For types, we can just look up the appropriate DIE.  But
13005          first we check to see if we're in the middle of emitting it
13006          so we know where the new DIE should go.  */
13007       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
13008         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
13009           break;
13010
13011       if (i < 0)
13012         {
13013           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
13014                       || TREE_ASM_WRITTEN (containing_scope));
13015
13016           /* If none of the current dies are suitable, we get file scope.  */
13017           scope_die = comp_unit_die;
13018         }
13019       else
13020         scope_die = lookup_type_die (containing_scope);
13021     }
13022   else
13023     scope_die = context_die;
13024
13025   return scope_die;
13026 }
13027
13028 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
13029
13030 static inline int
13031 local_scope_p (dw_die_ref context_die)
13032 {
13033   for (; context_die; context_die = context_die->die_parent)
13034     if (context_die->die_tag == DW_TAG_inlined_subroutine
13035         || context_die->die_tag == DW_TAG_subprogram)
13036       return 1;
13037
13038   return 0;
13039 }
13040
13041 /* Returns nonzero if CONTEXT_DIE is a class.  */
13042
13043 static inline int
13044 class_scope_p (dw_die_ref context_die)
13045 {
13046   return (context_die
13047           && (context_die->die_tag == DW_TAG_structure_type
13048               || context_die->die_tag == DW_TAG_class_type
13049               || context_die->die_tag == DW_TAG_interface_type
13050               || context_die->die_tag == DW_TAG_union_type));
13051 }
13052
13053 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
13054    whether or not to treat a DIE in this context as a declaration.  */
13055
13056 static inline int
13057 class_or_namespace_scope_p (dw_die_ref context_die)
13058 {
13059   return (class_scope_p (context_die)
13060           || (context_die && context_die->die_tag == DW_TAG_namespace));
13061 }
13062
13063 /* Many forms of DIEs require a "type description" attribute.  This
13064    routine locates the proper "type descriptor" die for the type given
13065    by 'type', and adds a DW_AT_type attribute below the given die.  */
13066
13067 static void
13068 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
13069                     int decl_volatile, dw_die_ref context_die)
13070 {
13071   enum tree_code code  = TREE_CODE (type);
13072   dw_die_ref type_die  = NULL;
13073
13074   /* ??? If this type is an unnamed subrange type of an integral, floating-point
13075      or fixed-point type, use the inner type.  This is because we have no
13076      support for unnamed types in base_type_die.  This can happen if this is
13077      an Ada subrange type.  Correct solution is emit a subrange type die.  */
13078   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
13079       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
13080     type = TREE_TYPE (type), code = TREE_CODE (type);
13081
13082   if (code == ERROR_MARK
13083       /* Handle a special case.  For functions whose return type is void, we
13084          generate *no* type attribute.  (Note that no object may have type
13085          `void', so this only applies to function return types).  */
13086       || code == VOID_TYPE)
13087     return;
13088
13089   type_die = modified_type_die (type,
13090                                 decl_const || TYPE_READONLY (type),
13091                                 decl_volatile || TYPE_VOLATILE (type),
13092                                 context_die);
13093
13094   if (type_die != NULL)
13095     add_AT_die_ref (object_die, DW_AT_type, type_die);
13096 }
13097
13098 /* Given an object die, add the calling convention attribute for the
13099    function call type.  */
13100 static void
13101 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
13102 {
13103   enum dwarf_calling_convention value = DW_CC_normal;
13104
13105   value = ((enum dwarf_calling_convention)
13106            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
13107
13108   /* DWARF doesn't provide a way to identify a program's source-level
13109      entry point.  DW_AT_calling_convention attributes are only meant
13110      to describe functions' calling conventions.  However, lacking a
13111      better way to signal the Fortran main program, we use this for the
13112      time being, following existing custom.  */
13113   if (is_fortran ()
13114       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
13115     value = DW_CC_program;
13116
13117   /* Only add the attribute if the backend requests it, and
13118      is not DW_CC_normal.  */
13119   if (value && (value != DW_CC_normal))
13120     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
13121 }
13122
13123 /* Given a tree pointer to a struct, class, union, or enum type node, return
13124    a pointer to the (string) tag name for the given type, or zero if the type
13125    was declared without a tag.  */
13126
13127 static const char *
13128 type_tag (const_tree type)
13129 {
13130   const char *name = 0;
13131
13132   if (TYPE_NAME (type) != 0)
13133     {
13134       tree t = 0;
13135
13136       /* Find the IDENTIFIER_NODE for the type name.  */
13137       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
13138         t = TYPE_NAME (type);
13139
13140       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
13141          a TYPE_DECL node, regardless of whether or not a `typedef' was
13142          involved.  */
13143       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13144                && ! DECL_IGNORED_P (TYPE_NAME (type)))
13145         {
13146           /* We want to be extra verbose.  Don't call dwarf_name if
13147              DECL_NAME isn't set.  The default hook for decl_printable_name
13148              doesn't like that, and in this context it's correct to return
13149              0, instead of "<anonymous>" or the like.  */
13150           if (DECL_NAME (TYPE_NAME (type)))
13151             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
13152         }
13153
13154       /* Now get the name as a string, or invent one.  */
13155       if (!name && t != 0)
13156         name = IDENTIFIER_POINTER (t);
13157     }
13158
13159   return (name == 0 || *name == '\0') ? 0 : name;
13160 }
13161
13162 /* Return the type associated with a data member, make a special check
13163    for bit field types.  */
13164
13165 static inline tree
13166 member_declared_type (const_tree member)
13167 {
13168   return (DECL_BIT_FIELD_TYPE (member)
13169           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
13170 }
13171
13172 /* Get the decl's label, as described by its RTL. This may be different
13173    from the DECL_NAME name used in the source file.  */
13174
13175 #if 0
13176 static const char *
13177 decl_start_label (tree decl)
13178 {
13179   rtx x;
13180   const char *fnname;
13181
13182   x = DECL_RTL (decl);
13183   gcc_assert (MEM_P (x));
13184
13185   x = XEXP (x, 0);
13186   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13187
13188   fnname = XSTR (x, 0);
13189   return fnname;
13190 }
13191 #endif
13192 \f
13193 /* These routines generate the internal representation of the DIE's for
13194    the compilation unit.  Debugging information is collected by walking
13195    the declaration trees passed in from dwarf2out_decl().  */
13196
13197 static void
13198 gen_array_type_die (tree type, dw_die_ref context_die)
13199 {
13200   dw_die_ref scope_die = scope_die_for (type, context_die);
13201   dw_die_ref array_die;
13202
13203   /* GNU compilers represent multidimensional array types as sequences of one
13204      dimensional array types whose element types are themselves array types.
13205      We sometimes squish that down to a single array_type DIE with multiple
13206      subscripts in the Dwarf debugging info.  The draft Dwarf specification
13207      say that we are allowed to do this kind of compression in C, because
13208      there is no difference between an array of arrays and a multidimensional
13209      array.  We don't do this for Ada to remain as close as possible to the
13210      actual representation, which is especially important against the language
13211      flexibilty wrt arrays of variable size.  */
13212
13213   bool collapse_nested_arrays = !is_ada ();
13214   tree element_type;
13215
13216   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
13217      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
13218   if (TYPE_STRING_FLAG (type)
13219       && TREE_CODE (type) == ARRAY_TYPE
13220       && is_fortran ()
13221       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
13222     {
13223       HOST_WIDE_INT size;
13224
13225       array_die = new_die (DW_TAG_string_type, scope_die, type);
13226       add_name_attribute (array_die, type_tag (type));
13227       equate_type_number_to_die (type, array_die);
13228       size = int_size_in_bytes (type);
13229       if (size >= 0)
13230         add_AT_unsigned (array_die, DW_AT_byte_size, size);
13231       else if (TYPE_DOMAIN (type) != NULL_TREE
13232                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
13233                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
13234         {
13235           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13236           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
13237
13238           size = int_size_in_bytes (TREE_TYPE (szdecl));
13239           if (loc && size > 0)
13240             {
13241               add_AT_loc (array_die, DW_AT_string_length, loc);
13242               if (size != DWARF2_ADDR_SIZE)
13243                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13244             }
13245         }
13246       return;
13247     }
13248
13249   /* ??? The SGI dwarf reader fails for array of array of enum types
13250      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
13251      array type comes before the outer array type.  We thus call gen_type_die
13252      before we new_die and must prevent nested array types collapsing for this
13253      target.  */
13254
13255 #ifdef MIPS_DEBUGGING_INFO
13256   gen_type_die (TREE_TYPE (type), context_die);
13257   collapse_nested_arrays = false;
13258 #endif
13259
13260   array_die = new_die (DW_TAG_array_type, scope_die, type);
13261   add_name_attribute (array_die, type_tag (type));
13262   equate_type_number_to_die (type, array_die);
13263
13264   if (TREE_CODE (type) == VECTOR_TYPE)
13265     {
13266       /* The frontend feeds us a representation for the vector as a struct
13267          containing an array.  Pull out the array type.  */
13268       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
13269       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
13270     }
13271
13272   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13273   if (is_fortran ()
13274       && TREE_CODE (type) == ARRAY_TYPE
13275       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
13276       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
13277     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13278
13279 #if 0
13280   /* We default the array ordering.  SDB will probably do
13281      the right things even if DW_AT_ordering is not present.  It's not even
13282      an issue until we start to get into multidimensional arrays anyway.  If
13283      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
13284      then we'll have to put the DW_AT_ordering attribute back in.  (But if
13285      and when we find out that we need to put these in, we will only do so
13286      for multidimensional arrays.  */
13287   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
13288 #endif
13289
13290 #ifdef MIPS_DEBUGGING_INFO
13291   /* The SGI compilers handle arrays of unknown bound by setting
13292      AT_declaration and not emitting any subrange DIEs.  */
13293   if (! TYPE_DOMAIN (type))
13294     add_AT_flag (array_die, DW_AT_declaration, 1);
13295   else
13296 #endif
13297     add_subscript_info (array_die, type, collapse_nested_arrays);
13298
13299   /* Add representation of the type of the elements of this array type and
13300      emit the corresponding DIE if we haven't done it already.  */  
13301   element_type = TREE_TYPE (type);
13302   if (collapse_nested_arrays)
13303     while (TREE_CODE (element_type) == ARRAY_TYPE)
13304       {
13305         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13306           break;
13307         element_type = TREE_TYPE (element_type);
13308       }
13309
13310 #ifndef MIPS_DEBUGGING_INFO
13311   gen_type_die (element_type, context_die);
13312 #endif
13313
13314   add_type_attribute (array_die, element_type, 0, 0, context_die);
13315
13316   if (get_AT (array_die, DW_AT_name))
13317     add_pubtype (type, array_die);
13318 }
13319
13320 static dw_loc_descr_ref
13321 descr_info_loc (tree val, tree base_decl)
13322 {
13323   HOST_WIDE_INT size;
13324   dw_loc_descr_ref loc, loc2;
13325   enum dwarf_location_atom op;
13326
13327   if (val == base_decl)
13328     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13329
13330   switch (TREE_CODE (val))
13331     {
13332     CASE_CONVERT:
13333       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13334     case VAR_DECL:
13335       return loc_descriptor_from_tree_1 (val, 0);
13336     case INTEGER_CST:
13337       if (host_integerp (val, 0))
13338         return int_loc_descriptor (tree_low_cst (val, 0));
13339       break;
13340     case INDIRECT_REF:
13341       size = int_size_in_bytes (TREE_TYPE (val));
13342       if (size < 0)
13343         break;
13344       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13345       if (!loc)
13346         break;
13347       if (size == DWARF2_ADDR_SIZE)
13348         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13349       else
13350         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13351       return loc;
13352     case POINTER_PLUS_EXPR:
13353     case PLUS_EXPR:
13354       if (host_integerp (TREE_OPERAND (val, 1), 1)
13355           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13356              < 16384)
13357         {
13358           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13359           if (!loc)
13360             break;
13361           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
13362         }
13363       else
13364         {
13365           op = DW_OP_plus;
13366         do_binop:
13367           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13368           if (!loc)
13369             break;
13370           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13371           if (!loc2)
13372             break;
13373           add_loc_descr (&loc, loc2);
13374           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13375         }
13376       return loc;
13377     case MINUS_EXPR:
13378       op = DW_OP_minus;
13379       goto do_binop;
13380     case MULT_EXPR:
13381       op = DW_OP_mul;
13382       goto do_binop;
13383     case EQ_EXPR:
13384       op = DW_OP_eq;
13385       goto do_binop;
13386     case NE_EXPR:
13387       op = DW_OP_ne;
13388       goto do_binop;
13389     default:
13390       break;
13391     }
13392   return NULL;
13393 }
13394
13395 static void
13396 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13397                       tree val, tree base_decl)
13398 {
13399   dw_loc_descr_ref loc;
13400
13401   if (host_integerp (val, 0))
13402     {
13403       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13404       return;
13405     }
13406
13407   loc = descr_info_loc (val, base_decl);
13408   if (!loc)
13409     return;
13410
13411   add_AT_loc (die, attr, loc);
13412 }
13413
13414 /* This routine generates DIE for array with hidden descriptor, details
13415    are filled into *info by a langhook.  */
13416
13417 static void
13418 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13419                           dw_die_ref context_die)
13420 {
13421   dw_die_ref scope_die = scope_die_for (type, context_die);
13422   dw_die_ref array_die;
13423   int dim;
13424
13425   array_die = new_die (DW_TAG_array_type, scope_die, type);
13426   add_name_attribute (array_die, type_tag (type));
13427   equate_type_number_to_die (type, array_die);
13428
13429   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13430   if (is_fortran ()
13431       && info->ndimensions >= 2)
13432     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13433
13434   if (info->data_location)
13435     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13436                           info->base_decl);
13437   if (info->associated)
13438     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13439                           info->base_decl);
13440   if (info->allocated)
13441     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13442                           info->base_decl);
13443
13444   for (dim = 0; dim < info->ndimensions; dim++)
13445     {
13446       dw_die_ref subrange_die
13447         = new_die (DW_TAG_subrange_type, array_die, NULL);
13448
13449       if (info->dimen[dim].lower_bound)
13450         {
13451           /* If it is the default value, omit it.  */
13452           if ((is_c_family () || is_java ())
13453               && integer_zerop (info->dimen[dim].lower_bound))
13454             ;
13455           else if (is_fortran ()
13456                    && integer_onep (info->dimen[dim].lower_bound))
13457             ;
13458           else
13459             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13460                                   info->dimen[dim].lower_bound,
13461                                   info->base_decl);
13462         }
13463       if (info->dimen[dim].upper_bound)
13464         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13465                               info->dimen[dim].upper_bound,
13466                               info->base_decl);
13467       if (info->dimen[dim].stride)
13468         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13469                               info->dimen[dim].stride,
13470                               info->base_decl);
13471     }
13472
13473   gen_type_die (info->element_type, context_die);
13474   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13475
13476   if (get_AT (array_die, DW_AT_name))
13477     add_pubtype (type, array_die);
13478 }
13479
13480 #if 0
13481 static void
13482 gen_entry_point_die (tree decl, dw_die_ref context_die)
13483 {
13484   tree origin = decl_ultimate_origin (decl);
13485   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13486
13487   if (origin != NULL)
13488     add_abstract_origin_attribute (decl_die, origin);
13489   else
13490     {
13491       add_name_and_src_coords_attributes (decl_die, decl);
13492       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13493                           0, 0, context_die);
13494     }
13495
13496   if (DECL_ABSTRACT (decl))
13497     equate_decl_number_to_die (decl, decl_die);
13498   else
13499     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13500 }
13501 #endif
13502
13503 /* Walk through the list of incomplete types again, trying once more to
13504    emit full debugging info for them.  */
13505
13506 static void
13507 retry_incomplete_types (void)
13508 {
13509   int i;
13510
13511   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13512     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13513 }
13514
13515 /* Determine what tag to use for a record type.  */
13516
13517 static enum dwarf_tag
13518 record_type_tag (tree type)
13519 {
13520   if (! lang_hooks.types.classify_record)
13521     return DW_TAG_structure_type;
13522
13523   switch (lang_hooks.types.classify_record (type))
13524     {
13525     case RECORD_IS_STRUCT:
13526       return DW_TAG_structure_type;
13527
13528     case RECORD_IS_CLASS:
13529       return DW_TAG_class_type;
13530
13531     case RECORD_IS_INTERFACE:
13532       return DW_TAG_interface_type;
13533
13534     default:
13535       gcc_unreachable ();
13536     }
13537 }
13538
13539 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13540    include all of the information about the enumeration values also. Each
13541    enumerated type name/value is listed as a child of the enumerated type
13542    DIE.  */
13543
13544 static dw_die_ref
13545 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13546 {
13547   dw_die_ref type_die = lookup_type_die (type);
13548
13549   if (type_die == NULL)
13550     {
13551       type_die = new_die (DW_TAG_enumeration_type,
13552                           scope_die_for (type, context_die), type);
13553       equate_type_number_to_die (type, type_die);
13554       add_name_attribute (type_die, type_tag (type));
13555     }
13556   else if (! TYPE_SIZE (type))
13557     return type_die;
13558   else
13559     remove_AT (type_die, DW_AT_declaration);
13560
13561   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13562      given enum type is incomplete, do not generate the DW_AT_byte_size
13563      attribute or the DW_AT_element_list attribute.  */
13564   if (TYPE_SIZE (type))
13565     {
13566       tree link;
13567
13568       TREE_ASM_WRITTEN (type) = 1;
13569       add_byte_size_attribute (type_die, type);
13570       if (TYPE_STUB_DECL (type) != NULL_TREE)
13571         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13572
13573       /* If the first reference to this type was as the return type of an
13574          inline function, then it may not have a parent.  Fix this now.  */
13575       if (type_die->die_parent == NULL)
13576         add_child_die (scope_die_for (type, context_die), type_die);
13577
13578       for (link = TYPE_VALUES (type);
13579            link != NULL; link = TREE_CHAIN (link))
13580         {
13581           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13582           tree value = TREE_VALUE (link);
13583
13584           add_name_attribute (enum_die,
13585                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13586
13587           if (TREE_CODE (value) == CONST_DECL)
13588             value = DECL_INITIAL (value);
13589
13590           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13591             /* DWARF2 does not provide a way of indicating whether or
13592                not enumeration constants are signed or unsigned.  GDB
13593                always assumes the values are signed, so we output all
13594                values as if they were signed.  That means that
13595                enumeration constants with very large unsigned values
13596                will appear to have negative values in the debugger.  */
13597             add_AT_int (enum_die, DW_AT_const_value,
13598                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13599         }
13600     }
13601   else
13602     add_AT_flag (type_die, DW_AT_declaration, 1);
13603
13604   if (get_AT (type_die, DW_AT_name))
13605     add_pubtype (type, type_die);
13606
13607   return type_die;
13608 }
13609
13610 /* Generate a DIE to represent either a real live formal parameter decl or to
13611    represent just the type of some formal parameter position in some function
13612    type.
13613
13614    Note that this routine is a bit unusual because its argument may be a
13615    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13616    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13617    node.  If it's the former then this function is being called to output a
13618    DIE to represent a formal parameter object (or some inlining thereof).  If
13619    it's the latter, then this function is only being called to output a
13620    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13621    argument type of some subprogram type.  */
13622
13623 static dw_die_ref
13624 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13625 {
13626   tree node_or_origin = node ? node : origin;
13627   dw_die_ref parm_die
13628     = new_die (DW_TAG_formal_parameter, context_die, node);
13629
13630   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13631     {
13632     case tcc_declaration:
13633       if (!origin)
13634         origin = decl_ultimate_origin (node);
13635       if (origin != NULL)
13636         add_abstract_origin_attribute (parm_die, origin);
13637       else
13638         {
13639           tree type = TREE_TYPE (node);
13640           add_name_and_src_coords_attributes (parm_die, node);
13641           if (DECL_BY_REFERENCE (node))
13642             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13643                                 context_die);
13644           else
13645             add_type_attribute (parm_die, type,
13646                                 TREE_READONLY (node),
13647                                 TREE_THIS_VOLATILE (node),
13648                                 context_die);
13649           if (DECL_ARTIFICIAL (node))
13650             add_AT_flag (parm_die, DW_AT_artificial, 1);
13651         }
13652
13653       if (node)
13654         equate_decl_number_to_die (node, parm_die);
13655       if (! DECL_ABSTRACT (node_or_origin))
13656         add_location_or_const_value_attribute (parm_die, node_or_origin,
13657                                                DW_AT_location);
13658
13659       break;
13660
13661     case tcc_type:
13662       /* We were called with some kind of a ..._TYPE node.  */
13663       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13664       break;
13665
13666     default:
13667       gcc_unreachable ();
13668     }
13669
13670   return parm_die;
13671 }
13672
13673 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13674    at the end of an (ANSI prototyped) formal parameters list.  */
13675
13676 static void
13677 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13678 {
13679   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13680 }
13681
13682 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13683    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13684    parameters as specified in some function type specification (except for
13685    those which appear as part of a function *definition*).  */
13686
13687 static void
13688 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13689 {
13690   tree link;
13691   tree formal_type = NULL;
13692   tree first_parm_type;
13693   tree arg;
13694
13695   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13696     {
13697       arg = DECL_ARGUMENTS (function_or_method_type);
13698       function_or_method_type = TREE_TYPE (function_or_method_type);
13699     }
13700   else
13701     arg = NULL_TREE;
13702
13703   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13704
13705   /* Make our first pass over the list of formal parameter types and output a
13706      DW_TAG_formal_parameter DIE for each one.  */
13707   for (link = first_parm_type; link; )
13708     {
13709       dw_die_ref parm_die;
13710
13711       formal_type = TREE_VALUE (link);
13712       if (formal_type == void_type_node)
13713         break;
13714
13715       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13716       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13717       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13718            && link == first_parm_type)
13719           || (arg && DECL_ARTIFICIAL (arg)))
13720         add_AT_flag (parm_die, DW_AT_artificial, 1);
13721
13722       link = TREE_CHAIN (link);
13723       if (arg)
13724         arg = TREE_CHAIN (arg);
13725     }
13726
13727   /* If this function type has an ellipsis, add a
13728      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13729   if (formal_type != void_type_node)
13730     gen_unspecified_parameters_die (function_or_method_type, context_die);
13731
13732   /* Make our second (and final) pass over the list of formal parameter types
13733      and output DIEs to represent those types (as necessary).  */
13734   for (link = TYPE_ARG_TYPES (function_or_method_type);
13735        link && TREE_VALUE (link);
13736        link = TREE_CHAIN (link))
13737     gen_type_die (TREE_VALUE (link), context_die);
13738 }
13739
13740 /* We want to generate the DIE for TYPE so that we can generate the
13741    die for MEMBER, which has been defined; we will need to refer back
13742    to the member declaration nested within TYPE.  If we're trying to
13743    generate minimal debug info for TYPE, processing TYPE won't do the
13744    trick; we need to attach the member declaration by hand.  */
13745
13746 static void
13747 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13748 {
13749   gen_type_die (type, context_die);
13750
13751   /* If we're trying to avoid duplicate debug info, we may not have
13752      emitted the member decl for this function.  Emit it now.  */
13753   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13754       && ! lookup_decl_die (member))
13755     {
13756       dw_die_ref type_die;
13757       gcc_assert (!decl_ultimate_origin (member));
13758
13759       push_decl_scope (type);
13760       type_die = lookup_type_die (type);
13761       if (TREE_CODE (member) == FUNCTION_DECL)
13762         gen_subprogram_die (member, type_die);
13763       else if (TREE_CODE (member) == FIELD_DECL)
13764         {
13765           /* Ignore the nameless fields that are used to skip bits but handle
13766              C++ anonymous unions and structs.  */
13767           if (DECL_NAME (member) != NULL_TREE
13768               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13769               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13770             {
13771               gen_type_die (member_declared_type (member), type_die);
13772               gen_field_die (member, type_die);
13773             }
13774         }
13775       else
13776         gen_variable_die (member, NULL_TREE, type_die);
13777
13778       pop_decl_scope ();
13779     }
13780 }
13781
13782 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13783    may later generate inlined and/or out-of-line instances of.  */
13784
13785 static void
13786 dwarf2out_abstract_function (tree decl)
13787 {
13788   dw_die_ref old_die;
13789   tree save_fn;
13790   tree context;
13791   int was_abstract = DECL_ABSTRACT (decl);
13792
13793   /* Make sure we have the actual abstract inline, not a clone.  */
13794   decl = DECL_ORIGIN (decl);
13795
13796   old_die = lookup_decl_die (decl);
13797   if (old_die && get_AT (old_die, DW_AT_inline))
13798     /* We've already generated the abstract instance.  */
13799     return;
13800
13801   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13802      we don't get confused by DECL_ABSTRACT.  */
13803   if (debug_info_level > DINFO_LEVEL_TERSE)
13804     {
13805       context = decl_class_context (decl);
13806       if (context)
13807         gen_type_die_for_member
13808           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13809     }
13810
13811   /* Pretend we've just finished compiling this function.  */
13812   save_fn = current_function_decl;
13813   current_function_decl = decl;
13814   push_cfun (DECL_STRUCT_FUNCTION (decl));
13815
13816   set_decl_abstract_flags (decl, 1);
13817   dwarf2out_decl (decl);
13818   if (! was_abstract)
13819     set_decl_abstract_flags (decl, 0);
13820
13821   current_function_decl = save_fn;
13822   pop_cfun ();
13823 }
13824
13825 /* Helper function of premark_used_types() which gets called through
13826    htab_traverse_resize().
13827
13828    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13829    marked as unused by prune_unused_types.  */
13830 static int
13831 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13832 {
13833   tree type;
13834   dw_die_ref die;
13835
13836   type = (tree) *slot;
13837   die = lookup_type_die (type);
13838   if (die != NULL)
13839     die->die_perennial_p = 1;
13840   return 1;
13841 }
13842
13843 /* Mark all members of used_types_hash as perennial.  */
13844 static void
13845 premark_used_types (void)
13846 {
13847   if (cfun && cfun->used_types_hash)
13848     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13849 }
13850
13851 /* Generate a DIE to represent a declared function (either file-scope or
13852    block-local).  */
13853
13854 static void
13855 gen_subprogram_die (tree decl, dw_die_ref context_die)
13856 {
13857   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13858   tree origin = decl_ultimate_origin (decl);
13859   dw_die_ref subr_die;
13860   tree fn_arg_types;
13861   tree outer_scope;
13862   dw_die_ref old_die = lookup_decl_die (decl);
13863   int declaration = (current_function_decl != decl
13864                      || class_or_namespace_scope_p (context_die));
13865
13866   premark_used_types ();
13867
13868   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13869      started to generate the abstract instance of an inline, decided to output
13870      its containing class, and proceeded to emit the declaration of the inline
13871      from the member list for the class.  If so, DECLARATION takes priority;
13872      we'll get back to the abstract instance when done with the class.  */
13873
13874   /* The class-scope declaration DIE must be the primary DIE.  */
13875   if (origin && declaration && class_or_namespace_scope_p (context_die))
13876     {
13877       origin = NULL;
13878       gcc_assert (!old_die);
13879     }
13880
13881   /* Now that the C++ front end lazily declares artificial member fns, we
13882      might need to retrofit the declaration into its class.  */
13883   if (!declaration && !origin && !old_die
13884       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13885       && !class_or_namespace_scope_p (context_die)
13886       && debug_info_level > DINFO_LEVEL_TERSE)
13887     old_die = force_decl_die (decl);
13888
13889   if (origin != NULL)
13890     {
13891       gcc_assert (!declaration || local_scope_p (context_die));
13892
13893       /* Fixup die_parent for the abstract instance of a nested
13894          inline function.  */
13895       if (old_die && old_die->die_parent == NULL)
13896         add_child_die (context_die, old_die);
13897
13898       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13899       add_abstract_origin_attribute (subr_die, origin);
13900     }
13901   else if (old_die)
13902     {
13903       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13904       struct dwarf_file_data * file_index = lookup_filename (s.file);
13905
13906       if (!get_AT_flag (old_die, DW_AT_declaration)
13907           /* We can have a normal definition following an inline one in the
13908              case of redefinition of GNU C extern inlines.
13909              It seems reasonable to use AT_specification in this case.  */
13910           && !get_AT (old_die, DW_AT_inline))
13911         {
13912           /* Detect and ignore this case, where we are trying to output
13913              something we have already output.  */
13914           return;
13915         }
13916
13917       /* If the definition comes from the same place as the declaration,
13918          maybe use the old DIE.  We always want the DIE for this function
13919          that has the *_pc attributes to be under comp_unit_die so the
13920          debugger can find it.  We also need to do this for abstract
13921          instances of inlines, since the spec requires the out-of-line copy
13922          to have the same parent.  For local class methods, this doesn't
13923          apply; we just use the old DIE.  */
13924       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13925           && (DECL_ARTIFICIAL (decl)
13926               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13927                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13928                       == (unsigned) s.line))))
13929         {
13930           subr_die = old_die;
13931
13932           /* Clear out the declaration attribute and the formal parameters.
13933              Do not remove all children, because it is possible that this
13934              declaration die was forced using force_decl_die(). In such
13935              cases die that forced declaration die (e.g. TAG_imported_module)
13936              is one of the children that we do not want to remove.  */
13937           remove_AT (subr_die, DW_AT_declaration);
13938           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13939         }
13940       else
13941         {
13942           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13943           add_AT_specification (subr_die, old_die);
13944           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13945             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13946           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13947             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13948         }
13949     }
13950   else
13951     {
13952       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13953
13954       if (TREE_PUBLIC (decl))
13955         add_AT_flag (subr_die, DW_AT_external, 1);
13956
13957       add_name_and_src_coords_attributes (subr_die, decl);
13958       if (debug_info_level > DINFO_LEVEL_TERSE)
13959         {
13960           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13961           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13962                               0, 0, context_die);
13963         }
13964
13965       add_pure_or_virtual_attribute (subr_die, decl);
13966       if (DECL_ARTIFICIAL (decl))
13967         add_AT_flag (subr_die, DW_AT_artificial, 1);
13968
13969       if (TREE_PROTECTED (decl))
13970         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13971       else if (TREE_PRIVATE (decl))
13972         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13973     }
13974
13975   if (declaration)
13976     {
13977       if (!old_die || !get_AT (old_die, DW_AT_inline))
13978         {
13979           add_AT_flag (subr_die, DW_AT_declaration, 1);
13980
13981           /* If this is an explicit function declaration then generate
13982              a DW_AT_explicit attribute.  */
13983           if (lang_hooks.decls.function_decl_explicit_p (decl))
13984             add_AT_flag (subr_die, DW_AT_explicit, 1);
13985
13986           /* The first time we see a member function, it is in the context of
13987              the class to which it belongs.  We make sure of this by emitting
13988              the class first.  The next time is the definition, which is
13989              handled above.  The two may come from the same source text.
13990
13991              Note that force_decl_die() forces function declaration die. It is
13992              later reused to represent definition.  */
13993           equate_decl_number_to_die (decl, subr_die);
13994         }
13995     }
13996   else if (DECL_ABSTRACT (decl))
13997     {
13998       if (DECL_DECLARED_INLINE_P (decl))
13999         {
14000           if (cgraph_function_possibly_inlined_p (decl))
14001             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
14002           else
14003             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
14004         }
14005       else
14006         {
14007           if (cgraph_function_possibly_inlined_p (decl))
14008             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
14009           else
14010             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
14011         }
14012
14013       if (DECL_DECLARED_INLINE_P (decl)
14014           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
14015         add_AT_flag (subr_die, DW_AT_artificial, 1);
14016
14017       equate_decl_number_to_die (decl, subr_die);
14018     }
14019   else if (!DECL_EXTERNAL (decl))
14020     {
14021       HOST_WIDE_INT cfa_fb_offset;
14022
14023       if (!old_die || !get_AT (old_die, DW_AT_inline))
14024         equate_decl_number_to_die (decl, subr_die);
14025
14026       if (!flag_reorder_blocks_and_partition)
14027         {
14028           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
14029                                        current_function_funcdef_no);
14030           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
14031           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14032                                        current_function_funcdef_no);
14033           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
14034
14035           add_pubname (decl, subr_die);
14036           add_arange (decl, subr_die);
14037         }
14038       else
14039         {  /* Do nothing for now; maybe need to duplicate die, one for
14040               hot section and one for cold section, then use the hot/cold
14041               section begin/end labels to generate the aranges...  */
14042           /*
14043             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
14044             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
14045             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
14046             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
14047
14048             add_pubname (decl, subr_die);
14049             add_arange (decl, subr_die);
14050             add_arange (decl, subr_die);
14051            */
14052         }
14053
14054 #ifdef MIPS_DEBUGGING_INFO
14055       /* Add a reference to the FDE for this routine.  */
14056       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
14057 #endif
14058
14059       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
14060
14061       /* We define the "frame base" as the function's CFA.  This is more
14062          convenient for several reasons: (1) It's stable across the prologue
14063          and epilogue, which makes it better than just a frame pointer,
14064          (2) With dwarf3, there exists a one-byte encoding that allows us
14065          to reference the .debug_frame data by proxy, but failing that,
14066          (3) We can at least reuse the code inspection and interpretation
14067          code that determines the CFA position at various points in the
14068          function.  */
14069       /* ??? Use some command-line or configury switch to enable the use
14070          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
14071          consumers that understand it; fall back to "pure" dwarf2 and
14072          convert the CFA data into a location list.  */
14073       {
14074         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
14075         if (list->dw_loc_next)
14076           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
14077         else
14078           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
14079       }
14080
14081       /* Compute a displacement from the "steady-state frame pointer" to
14082          the CFA.  The former is what all stack slots and argument slots
14083          will reference in the rtl; the later is what we've told the
14084          debugger about.  We'll need to adjust all frame_base references
14085          by this displacement.  */
14086       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
14087
14088       if (cfun->static_chain_decl)
14089         add_AT_location_description (subr_die, DW_AT_static_link,
14090                  loc_descriptor_from_tree (cfun->static_chain_decl));
14091     }
14092
14093   /* Now output descriptions of the arguments for this function. This gets
14094      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
14095      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
14096      `...' at the end of the formal parameter list.  In order to find out if
14097      there was a trailing ellipsis or not, we must instead look at the type
14098      associated with the FUNCTION_DECL.  This will be a node of type
14099      FUNCTION_TYPE. If the chain of type nodes hanging off of this
14100      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
14101      an ellipsis at the end.  */
14102
14103   /* In the case where we are describing a mere function declaration, all we
14104      need to do here (and all we *can* do here) is to describe the *types* of
14105      its formal parameters.  */
14106   if (debug_info_level <= DINFO_LEVEL_TERSE)
14107     ;
14108   else if (declaration)
14109     gen_formal_types_die (decl, subr_die);
14110   else
14111     {
14112       /* Generate DIEs to represent all known formal parameters.  */
14113       tree arg_decls = DECL_ARGUMENTS (decl);
14114       tree parm;
14115
14116       /* When generating DIEs, generate the unspecified_parameters DIE
14117          instead if we come across the arg "__builtin_va_alist" */
14118       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
14119         if (TREE_CODE (parm) == PARM_DECL)
14120           {
14121             if (DECL_NAME (parm)
14122                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
14123                             "__builtin_va_alist"))
14124               gen_unspecified_parameters_die (parm, subr_die);
14125             else
14126               gen_decl_die (parm, NULL, subr_die);
14127           }
14128
14129       /* Decide whether we need an unspecified_parameters DIE at the end.
14130          There are 2 more cases to do this for: 1) the ansi ... declaration -
14131          this is detectable when the end of the arg list is not a
14132          void_type_node 2) an unprototyped function declaration (not a
14133          definition).  This just means that we have no info about the
14134          parameters at all.  */
14135       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
14136       if (fn_arg_types != NULL)
14137         {
14138           /* This is the prototyped case, check for....  */
14139           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
14140             gen_unspecified_parameters_die (decl, subr_die);
14141         }
14142       else if (DECL_INITIAL (decl) == NULL_TREE)
14143         gen_unspecified_parameters_die (decl, subr_die);
14144     }
14145
14146   /* Output Dwarf info for all of the stuff within the body of the function
14147      (if it has one - it may be just a declaration).  */
14148   outer_scope = DECL_INITIAL (decl);
14149
14150   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
14151      a function.  This BLOCK actually represents the outermost binding contour
14152      for the function, i.e. the contour in which the function's formal
14153      parameters and labels get declared. Curiously, it appears that the front
14154      end doesn't actually put the PARM_DECL nodes for the current function onto
14155      the BLOCK_VARS list for this outer scope, but are strung off of the
14156      DECL_ARGUMENTS list for the function instead.
14157
14158      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
14159      the LABEL_DECL nodes for the function however, and we output DWARF info
14160      for those in decls_for_scope.  Just within the `outer_scope' there will be
14161      a BLOCK node representing the function's outermost pair of curly braces,
14162      and any blocks used for the base and member initializers of a C++
14163      constructor function.  */
14164   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
14165     {
14166       /* Emit a DW_TAG_variable DIE for a named return value.  */
14167       if (DECL_NAME (DECL_RESULT (decl)))
14168         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
14169
14170       current_function_has_inlines = 0;
14171       decls_for_scope (outer_scope, subr_die, 0);
14172
14173 #if 0 && defined (MIPS_DEBUGGING_INFO)
14174       if (current_function_has_inlines)
14175         {
14176           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
14177           if (! comp_unit_has_inlines)
14178             {
14179               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
14180               comp_unit_has_inlines = 1;
14181             }
14182         }
14183 #endif
14184     }
14185   /* Add the calling convention attribute if requested.  */
14186   add_calling_convention_attribute (subr_die, decl);
14187
14188 }
14189
14190 /* Returns a hash value for X (which really is a die_struct).  */
14191
14192 static hashval_t
14193 common_block_die_table_hash (const void *x)
14194 {
14195   const_dw_die_ref d = (const_dw_die_ref) x;
14196   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
14197 }
14198
14199 /* Return nonzero if decl_id and die_parent of die_struct X is the same
14200    as decl_id and die_parent of die_struct Y.  */
14201
14202 static int
14203 common_block_die_table_eq (const void *x, const void *y)
14204 {
14205   const_dw_die_ref d = (const_dw_die_ref) x;
14206   const_dw_die_ref e = (const_dw_die_ref) y;
14207   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
14208 }
14209
14210 /* Generate a DIE to represent a declared data object.
14211    Either DECL or ORIGIN must be non-null.  */
14212
14213 static void
14214 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
14215 {
14216   HOST_WIDE_INT off;
14217   tree com_decl;
14218   tree decl_or_origin = decl ? decl : origin;
14219   dw_die_ref var_die;
14220   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
14221   dw_die_ref origin_die;
14222   int declaration = (DECL_EXTERNAL (decl_or_origin)
14223                      /* If DECL is COMDAT and has not actually been
14224                         emitted, we cannot take its address; there
14225                         might end up being no definition anywhere in
14226                         the program.  For example, consider the C++
14227                         test case:
14228
14229                           template <class T>
14230                           struct S { static const int i = 7; };
14231
14232                           template <class T>
14233                           const int S<T>::i;
14234
14235                           int f() { return S<int>::i; }
14236
14237                         Here, S<int>::i is not DECL_EXTERNAL, but no
14238                         definition is required, so the compiler will
14239                         not emit a definition.  */
14240                      || (TREE_CODE (decl_or_origin) == VAR_DECL
14241                          && DECL_COMDAT (decl_or_origin)
14242                          && !TREE_ASM_WRITTEN (decl_or_origin))
14243                      || class_or_namespace_scope_p (context_die));
14244
14245   if (!origin)
14246     origin = decl_ultimate_origin (decl);
14247
14248   com_decl = fortran_common (decl_or_origin, &off);
14249
14250   /* Symbol in common gets emitted as a child of the common block, in the form
14251      of a data member.  */
14252   if (com_decl)
14253     {
14254       tree field;
14255       dw_die_ref com_die;
14256       dw_loc_descr_ref loc;
14257       die_node com_die_arg;
14258
14259       var_die = lookup_decl_die (decl_or_origin);
14260       if (var_die)
14261         {
14262           if (get_AT (var_die, DW_AT_location) == NULL)
14263             {
14264               loc = loc_descriptor_from_tree (com_decl);
14265               if (loc)
14266                 {
14267                   if (off)
14268                     {
14269                       /* Optimize the common case.  */
14270                       if (loc->dw_loc_opc == DW_OP_addr
14271                           && loc->dw_loc_next == NULL
14272                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14273                              == SYMBOL_REF)
14274                         loc->dw_loc_oprnd1.v.val_addr
14275                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14276                         else
14277                           loc_descr_plus_const (&loc, off);
14278                     }
14279                   add_AT_loc (var_die, DW_AT_location, loc);
14280                   remove_AT (var_die, DW_AT_declaration);
14281                 }
14282             }
14283           return;
14284         }
14285
14286       if (common_block_die_table == NULL)
14287         common_block_die_table
14288           = htab_create_ggc (10, common_block_die_table_hash,
14289                              common_block_die_table_eq, NULL);
14290
14291       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14292       com_die_arg.decl_id = DECL_UID (com_decl);
14293       com_die_arg.die_parent = context_die;
14294       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14295       loc = loc_descriptor_from_tree (com_decl);
14296       if (com_die == NULL)
14297         {
14298           const char *cnam
14299             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14300           void **slot;
14301
14302           com_die = new_die (DW_TAG_common_block, context_die, decl);
14303           add_name_and_src_coords_attributes (com_die, com_decl);
14304           if (loc)
14305             {
14306               add_AT_loc (com_die, DW_AT_location, loc);
14307               /* Avoid sharing the same loc descriptor between
14308                  DW_TAG_common_block and DW_TAG_variable.  */
14309               loc = loc_descriptor_from_tree (com_decl);
14310             }
14311           else if (DECL_EXTERNAL (decl))
14312             add_AT_flag (com_die, DW_AT_declaration, 1);
14313           add_pubname_string (cnam, com_die); /* ??? needed? */
14314           com_die->decl_id = DECL_UID (com_decl);
14315           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14316           *slot = (void *) com_die;
14317         }
14318       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14319         {
14320           add_AT_loc (com_die, DW_AT_location, loc);
14321           loc = loc_descriptor_from_tree (com_decl);
14322           remove_AT (com_die, DW_AT_declaration);
14323         }
14324       var_die = new_die (DW_TAG_variable, com_die, decl);
14325       add_name_and_src_coords_attributes (var_die, decl);
14326       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14327                           TREE_THIS_VOLATILE (decl), context_die);
14328       add_AT_flag (var_die, DW_AT_external, 1);
14329       if (loc)
14330         {
14331           if (off)
14332             {
14333               /* Optimize the common case.  */
14334               if (loc->dw_loc_opc == DW_OP_addr
14335                   && loc->dw_loc_next == NULL
14336                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14337                 loc->dw_loc_oprnd1.v.val_addr
14338                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14339               else
14340                 loc_descr_plus_const (&loc, off);
14341             }
14342           add_AT_loc (var_die, DW_AT_location, loc);
14343         }
14344       else if (DECL_EXTERNAL (decl))
14345         add_AT_flag (var_die, DW_AT_declaration, 1);
14346       equate_decl_number_to_die (decl, var_die);
14347       return;
14348     }
14349
14350   /* If the compiler emitted a definition for the DECL declaration
14351      and if we already emitted a DIE for it, don't emit a second
14352      DIE for it again.  */
14353   if (old_die
14354       && declaration
14355       && old_die->die_parent == context_die)
14356     return;
14357
14358   /* For static data members, the declaration in the class is supposed
14359      to have DW_TAG_member tag; the specification should still be
14360      DW_TAG_variable referencing the DW_TAG_member DIE.  */
14361   if (declaration && class_scope_p (context_die))
14362     var_die = new_die (DW_TAG_member, context_die, decl);
14363   else
14364     var_die = new_die (DW_TAG_variable, context_die, decl);
14365
14366   origin_die = NULL;
14367   if (origin != NULL)
14368     origin_die = add_abstract_origin_attribute (var_die, origin);
14369
14370   /* Loop unrolling can create multiple blocks that refer to the same
14371      static variable, so we must test for the DW_AT_declaration flag.
14372
14373      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14374      copy decls and set the DECL_ABSTRACT flag on them instead of
14375      sharing them.
14376
14377      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14378
14379      ??? The declare_in_namespace support causes us to get two DIEs for one
14380      variable, both of which are declarations.  We want to avoid considering
14381      one to be a specification, so we must test that this DIE is not a
14382      declaration.  */
14383   else if (old_die && TREE_STATIC (decl) && ! declaration
14384            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14385     {
14386       /* This is a definition of a C++ class level static.  */
14387       add_AT_specification (var_die, old_die);
14388       if (DECL_NAME (decl))
14389         {
14390           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14391           struct dwarf_file_data * file_index = lookup_filename (s.file);
14392
14393           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14394             add_AT_file (var_die, DW_AT_decl_file, file_index);
14395
14396           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14397             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14398         }
14399     }
14400   else
14401     {
14402       tree type = TREE_TYPE (decl);
14403
14404       add_name_and_src_coords_attributes (var_die, decl);
14405       if ((TREE_CODE (decl) == PARM_DECL
14406            || TREE_CODE (decl) == RESULT_DECL
14407            || TREE_CODE (decl) == VAR_DECL)
14408           && DECL_BY_REFERENCE (decl))
14409         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14410       else
14411         add_type_attribute (var_die, type, TREE_READONLY (decl),
14412                             TREE_THIS_VOLATILE (decl), context_die);
14413
14414       if (TREE_PUBLIC (decl))
14415         add_AT_flag (var_die, DW_AT_external, 1);
14416
14417       if (DECL_ARTIFICIAL (decl))
14418         add_AT_flag (var_die, DW_AT_artificial, 1);
14419
14420       if (TREE_PROTECTED (decl))
14421         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14422       else if (TREE_PRIVATE (decl))
14423         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14424     }
14425
14426   if (declaration)
14427     add_AT_flag (var_die, DW_AT_declaration, 1);
14428
14429   if (decl && (DECL_ABSTRACT (decl) || declaration))
14430     equate_decl_number_to_die (decl, var_die);
14431
14432   if (! declaration
14433       && (! DECL_ABSTRACT (decl_or_origin)
14434           /* Local static vars are shared between all clones/inlines,
14435              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14436              already set.  */
14437           || (TREE_CODE (decl_or_origin) == VAR_DECL
14438               && TREE_STATIC (decl_or_origin)
14439               && DECL_RTL_SET_P (decl_or_origin)))
14440       /* When abstract origin already has DW_AT_location attribute, no need
14441          to add it again.  */
14442       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14443     {
14444       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14445           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14446         defer_location (decl_or_origin, var_die);
14447       else
14448         add_location_or_const_value_attribute (var_die,
14449                                                decl_or_origin,
14450                                                DW_AT_location);
14451       add_pubname (decl_or_origin, var_die);
14452     }
14453   else
14454     tree_add_const_value_attribute (var_die, decl_or_origin);
14455 }
14456
14457 /* Generate a DIE to represent a named constant.  */
14458
14459 static void
14460 gen_const_die (tree decl, dw_die_ref context_die)
14461 {
14462   dw_die_ref const_die;
14463   tree type = TREE_TYPE (decl);
14464
14465   const_die = new_die (DW_TAG_constant, context_die, decl);
14466   add_name_and_src_coords_attributes (const_die, decl);
14467   add_type_attribute (const_die, type, 1, 0, context_die);
14468   if (TREE_PUBLIC (decl))
14469     add_AT_flag (const_die, DW_AT_external, 1);
14470   if (DECL_ARTIFICIAL (decl))
14471     add_AT_flag (const_die, DW_AT_artificial, 1);
14472   tree_add_const_value_attribute (const_die, decl);
14473 }
14474
14475 /* Generate a DIE to represent a label identifier.  */
14476
14477 static void
14478 gen_label_die (tree decl, dw_die_ref context_die)
14479 {
14480   tree origin = decl_ultimate_origin (decl);
14481   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14482   rtx insn;
14483   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14484
14485   if (origin != NULL)
14486     add_abstract_origin_attribute (lbl_die, origin);
14487   else
14488     add_name_and_src_coords_attributes (lbl_die, decl);
14489
14490   if (DECL_ABSTRACT (decl))
14491     equate_decl_number_to_die (decl, lbl_die);
14492   else
14493     {
14494       insn = DECL_RTL_IF_SET (decl);
14495
14496       /* Deleted labels are programmer specified labels which have been
14497          eliminated because of various optimizations.  We still emit them
14498          here so that it is possible to put breakpoints on them.  */
14499       if (insn
14500           && (LABEL_P (insn)
14501               || ((NOTE_P (insn)
14502                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14503         {
14504           /* When optimization is enabled (via -O) some parts of the compiler
14505              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14506              represent source-level labels which were explicitly declared by
14507              the user.  This really shouldn't be happening though, so catch
14508              it if it ever does happen.  */
14509           gcc_assert (!INSN_DELETED_P (insn));
14510
14511           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14512           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14513         }
14514     }
14515 }
14516
14517 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14518    attributes to the DIE for a block STMT, to describe where the inlined
14519    function was called from.  This is similar to add_src_coords_attributes.  */
14520
14521 static inline void
14522 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14523 {
14524   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14525
14526   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14527   add_AT_unsigned (die, DW_AT_call_line, s.line);
14528 }
14529
14530
14531 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14532    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14533
14534 static inline void
14535 add_high_low_attributes (tree stmt, dw_die_ref die)
14536 {
14537   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14538
14539   if (BLOCK_FRAGMENT_CHAIN (stmt))
14540     {
14541       tree chain;
14542
14543       if (inlined_function_outer_scope_p (stmt))
14544         {
14545           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14546                                        BLOCK_NUMBER (stmt));
14547           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14548         }
14549
14550       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14551
14552       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14553       do
14554         {
14555           add_ranges (chain);
14556           chain = BLOCK_FRAGMENT_CHAIN (chain);
14557         }
14558       while (chain);
14559       add_ranges (NULL);
14560     }
14561   else
14562     {
14563       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14564                                    BLOCK_NUMBER (stmt));
14565       add_AT_lbl_id (die, DW_AT_low_pc, label);
14566       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14567                                    BLOCK_NUMBER (stmt));
14568       add_AT_lbl_id (die, DW_AT_high_pc, label);
14569     }
14570 }
14571
14572 /* Generate a DIE for a lexical block.  */
14573
14574 static void
14575 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14576 {
14577   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14578
14579   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14580     add_high_low_attributes (stmt, stmt_die);
14581
14582   decls_for_scope (stmt, stmt_die, depth);
14583 }
14584
14585 /* Generate a DIE for an inlined subprogram.  */
14586
14587 static void
14588 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14589 {
14590   tree decl = block_ultimate_origin (stmt);
14591
14592   /* Emit info for the abstract instance first, if we haven't yet.  We
14593      must emit this even if the block is abstract, otherwise when we
14594      emit the block below (or elsewhere), we may end up trying to emit
14595      a die whose origin die hasn't been emitted, and crashing.  */
14596   dwarf2out_abstract_function (decl);
14597
14598   if (! BLOCK_ABSTRACT (stmt))
14599     {
14600       dw_die_ref subr_die
14601         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14602
14603       add_abstract_origin_attribute (subr_die, decl);
14604       if (TREE_ASM_WRITTEN (stmt))
14605         add_high_low_attributes (stmt, subr_die);
14606       add_call_src_coords_attributes (stmt, subr_die);
14607
14608       decls_for_scope (stmt, subr_die, depth);
14609       current_function_has_inlines = 1;
14610     }
14611   else
14612     /* We may get here if we're the outer block of function A that was
14613        inlined into function B that was inlined into function C.  When
14614        generating debugging info for C, dwarf2out_abstract_function(B)
14615        would mark all inlined blocks as abstract, including this one.
14616        So, we wouldn't (and shouldn't) expect labels to be generated
14617        for this one.  Instead, just emit debugging info for
14618        declarations within the block.  This is particularly important
14619        in the case of initializers of arguments passed from B to us:
14620        if they're statement expressions containing declarations, we
14621        wouldn't generate dies for their abstract variables, and then,
14622        when generating dies for the real variables, we'd die (pun
14623        intended :-)  */
14624     gen_lexical_block_die (stmt, context_die, depth);
14625 }
14626
14627 /* Generate a DIE for a field in a record, or structure.  */
14628
14629 static void
14630 gen_field_die (tree decl, dw_die_ref context_die)
14631 {
14632   dw_die_ref decl_die;
14633
14634   if (TREE_TYPE (decl) == error_mark_node)
14635     return;
14636
14637   decl_die = new_die (DW_TAG_member, context_die, decl);
14638   add_name_and_src_coords_attributes (decl_die, decl);
14639   add_type_attribute (decl_die, member_declared_type (decl),
14640                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14641                       context_die);
14642
14643   if (DECL_BIT_FIELD_TYPE (decl))
14644     {
14645       add_byte_size_attribute (decl_die, decl);
14646       add_bit_size_attribute (decl_die, decl);
14647       add_bit_offset_attribute (decl_die, decl);
14648     }
14649
14650   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14651     add_data_member_location_attribute (decl_die, decl);
14652
14653   if (DECL_ARTIFICIAL (decl))
14654     add_AT_flag (decl_die, DW_AT_artificial, 1);
14655
14656   if (TREE_PROTECTED (decl))
14657     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14658   else if (TREE_PRIVATE (decl))
14659     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14660
14661   /* Equate decl number to die, so that we can look up this decl later on.  */
14662   equate_decl_number_to_die (decl, decl_die);
14663 }
14664
14665 #if 0
14666 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14667    Use modified_type_die instead.
14668    We keep this code here just in case these types of DIEs may be needed to
14669    represent certain things in other languages (e.g. Pascal) someday.  */
14670
14671 static void
14672 gen_pointer_type_die (tree type, dw_die_ref context_die)
14673 {
14674   dw_die_ref ptr_die
14675     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14676
14677   equate_type_number_to_die (type, ptr_die);
14678   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14679   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14680 }
14681
14682 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14683    Use modified_type_die instead.
14684    We keep this code here just in case these types of DIEs may be needed to
14685    represent certain things in other languages (e.g. Pascal) someday.  */
14686
14687 static void
14688 gen_reference_type_die (tree type, dw_die_ref context_die)
14689 {
14690   dw_die_ref ref_die
14691     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14692
14693   equate_type_number_to_die (type, ref_die);
14694   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14695   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14696 }
14697 #endif
14698
14699 /* Generate a DIE for a pointer to a member type.  */
14700
14701 static void
14702 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14703 {
14704   dw_die_ref ptr_die
14705     = new_die (DW_TAG_ptr_to_member_type,
14706                scope_die_for (type, context_die), type);
14707
14708   equate_type_number_to_die (type, ptr_die);
14709   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14710                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14711   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14712 }
14713
14714 /* Generate the DIE for the compilation unit.  */
14715
14716 static dw_die_ref
14717 gen_compile_unit_die (const char *filename)
14718 {
14719   dw_die_ref die;
14720   char producer[250];
14721   const char *language_string = lang_hooks.name;
14722   int language;
14723
14724   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14725
14726   if (filename)
14727     {
14728       add_name_attribute (die, filename);
14729       /* Don't add cwd for <built-in>.  */
14730       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14731         add_comp_dir_attribute (die);
14732     }
14733
14734   sprintf (producer, "%s %s", language_string, version_string);
14735
14736 #ifdef MIPS_DEBUGGING_INFO
14737   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14738      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14739      not appear in the producer string, the debugger reaches the conclusion
14740      that the object file is stripped and has no debugging information.
14741      To get the MIPS/SGI debugger to believe that there is debugging
14742      information in the object file, we add a -g to the producer string.  */
14743   if (debug_info_level > DINFO_LEVEL_TERSE)
14744     strcat (producer, " -g");
14745 #endif
14746
14747   add_AT_string (die, DW_AT_producer, producer);
14748
14749   if (strcmp (language_string, "GNU C++") == 0)
14750     language = DW_LANG_C_plus_plus;
14751   else if (strcmp (language_string, "GNU Ada") == 0)
14752     language = DW_LANG_Ada95;
14753   else if (strcmp (language_string, "GNU F77") == 0)
14754     language = DW_LANG_Fortran77;
14755   else if (strcmp (language_string, "GNU Fortran") == 0)
14756     language = DW_LANG_Fortran95;
14757   else if (strcmp (language_string, "GNU Pascal") == 0)
14758     language = DW_LANG_Pascal83;
14759   else if (strcmp (language_string, "GNU Java") == 0)
14760     language = DW_LANG_Java;
14761   else if (strcmp (language_string, "GNU Objective-C") == 0)
14762     language = DW_LANG_ObjC;
14763   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14764     language = DW_LANG_ObjC_plus_plus;
14765   else
14766     language = DW_LANG_C89;
14767
14768   add_AT_unsigned (die, DW_AT_language, language);
14769   return die;
14770 }
14771
14772 /* Generate the DIE for a base class.  */
14773
14774 static void
14775 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14776 {
14777   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14778
14779   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14780   add_data_member_location_attribute (die, binfo);
14781
14782   if (BINFO_VIRTUAL_P (binfo))
14783     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14784
14785   if (access == access_public_node)
14786     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14787   else if (access == access_protected_node)
14788     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14789 }
14790
14791 /* Generate a DIE for a class member.  */
14792
14793 static void
14794 gen_member_die (tree type, dw_die_ref context_die)
14795 {
14796   tree member;
14797   tree binfo = TYPE_BINFO (type);
14798   dw_die_ref child;
14799
14800   /* If this is not an incomplete type, output descriptions of each of its
14801      members. Note that as we output the DIEs necessary to represent the
14802      members of this record or union type, we will also be trying to output
14803      DIEs to represent the *types* of those members. However the `type'
14804      function (above) will specifically avoid generating type DIEs for member
14805      types *within* the list of member DIEs for this (containing) type except
14806      for those types (of members) which are explicitly marked as also being
14807      members of this (containing) type themselves.  The g++ front- end can
14808      force any given type to be treated as a member of some other (containing)
14809      type by setting the TYPE_CONTEXT of the given (member) type to point to
14810      the TREE node representing the appropriate (containing) type.  */
14811
14812   /* First output info about the base classes.  */
14813   if (binfo)
14814     {
14815       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14816       int i;
14817       tree base;
14818
14819       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14820         gen_inheritance_die (base,
14821                              (accesses ? VEC_index (tree, accesses, i)
14822                               : access_public_node), context_die);
14823     }
14824
14825   /* Now output info about the data members and type members.  */
14826   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14827     {
14828       /* If we thought we were generating minimal debug info for TYPE
14829          and then changed our minds, some of the member declarations
14830          may have already been defined.  Don't define them again, but
14831          do put them in the right order.  */
14832
14833       child = lookup_decl_die (member);
14834       if (child)
14835         splice_child_die (context_die, child);
14836       else
14837         gen_decl_die (member, NULL, context_die);
14838     }
14839
14840   /* Now output info about the function members (if any).  */
14841   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14842     {
14843       /* Don't include clones in the member list.  */
14844       if (DECL_ABSTRACT_ORIGIN (member))
14845         continue;
14846
14847       child = lookup_decl_die (member);
14848       if (child)
14849         splice_child_die (context_die, child);
14850       else
14851         gen_decl_die (member, NULL, context_die);
14852     }
14853 }
14854
14855 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14856    is set, we pretend that the type was never defined, so we only get the
14857    member DIEs needed by later specification DIEs.  */
14858
14859 static void
14860 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14861                                 enum debug_info_usage usage)
14862 {
14863   dw_die_ref type_die = lookup_type_die (type);
14864   dw_die_ref scope_die = 0;
14865   int nested = 0;
14866   int complete = (TYPE_SIZE (type)
14867                   && (! TYPE_STUB_DECL (type)
14868                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14869   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14870   complete = complete && should_emit_struct_debug (type, usage);
14871
14872   if (type_die && ! complete)
14873     return;
14874
14875   if (TYPE_CONTEXT (type) != NULL_TREE
14876       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14877           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14878     nested = 1;
14879
14880   scope_die = scope_die_for (type, context_die);
14881
14882   if (! type_die || (nested && scope_die == comp_unit_die))
14883     /* First occurrence of type or toplevel definition of nested class.  */
14884     {
14885       dw_die_ref old_die = type_die;
14886
14887       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14888                           ? record_type_tag (type) : DW_TAG_union_type,
14889                           scope_die, type);
14890       equate_type_number_to_die (type, type_die);
14891       if (old_die)
14892         add_AT_specification (type_die, old_die);
14893       else
14894         add_name_attribute (type_die, type_tag (type));
14895     }
14896   else
14897     remove_AT (type_die, DW_AT_declaration);
14898
14899   /* If this type has been completed, then give it a byte_size attribute and
14900      then give a list of members.  */
14901   if (complete && !ns_decl)
14902     {
14903       /* Prevent infinite recursion in cases where the type of some member of
14904          this type is expressed in terms of this type itself.  */
14905       TREE_ASM_WRITTEN (type) = 1;
14906       add_byte_size_attribute (type_die, type);
14907       if (TYPE_STUB_DECL (type) != NULL_TREE)
14908         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14909
14910       /* If the first reference to this type was as the return type of an
14911          inline function, then it may not have a parent.  Fix this now.  */
14912       if (type_die->die_parent == NULL)
14913         add_child_die (scope_die, type_die);
14914
14915       push_decl_scope (type);
14916       gen_member_die (type, type_die);
14917       pop_decl_scope ();
14918
14919       /* GNU extension: Record what type our vtable lives in.  */
14920       if (TYPE_VFIELD (type))
14921         {
14922           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14923
14924           gen_type_die (vtype, context_die);
14925           add_AT_die_ref (type_die, DW_AT_containing_type,
14926                           lookup_type_die (vtype));
14927         }
14928     }
14929   else
14930     {
14931       add_AT_flag (type_die, DW_AT_declaration, 1);
14932
14933       /* We don't need to do this for function-local types.  */
14934       if (TYPE_STUB_DECL (type)
14935           && ! decl_function_context (TYPE_STUB_DECL (type)))
14936         VEC_safe_push (tree, gc, incomplete_types, type);
14937     }
14938
14939   if (get_AT (type_die, DW_AT_name))
14940     add_pubtype (type, type_die);
14941 }
14942
14943 /* Generate a DIE for a subroutine _type_.  */
14944
14945 static void
14946 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14947 {
14948   tree return_type = TREE_TYPE (type);
14949   dw_die_ref subr_die
14950     = new_die (DW_TAG_subroutine_type,
14951                scope_die_for (type, context_die), type);
14952
14953   equate_type_number_to_die (type, subr_die);
14954   add_prototyped_attribute (subr_die, type);
14955   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14956   gen_formal_types_die (type, subr_die);
14957
14958   if (get_AT (subr_die, DW_AT_name))
14959     add_pubtype (type, subr_die);
14960 }
14961
14962 /* Generate a DIE for a type definition.  */
14963
14964 static void
14965 gen_typedef_die (tree decl, dw_die_ref context_die)
14966 {
14967   dw_die_ref type_die;
14968   tree origin;
14969
14970   if (TREE_ASM_WRITTEN (decl))
14971     return;
14972
14973   TREE_ASM_WRITTEN (decl) = 1;
14974   type_die = new_die (DW_TAG_typedef, context_die, decl);
14975   origin = decl_ultimate_origin (decl);
14976   if (origin != NULL)
14977     add_abstract_origin_attribute (type_die, origin);
14978   else
14979     {
14980       tree type;
14981
14982       add_name_and_src_coords_attributes (type_die, decl);
14983       if (DECL_ORIGINAL_TYPE (decl))
14984         {
14985           type = DECL_ORIGINAL_TYPE (decl);
14986
14987           gcc_assert (type != TREE_TYPE (decl));
14988           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14989         }
14990       else
14991         type = TREE_TYPE (decl);
14992
14993       add_type_attribute (type_die, type, TREE_READONLY (decl),
14994                           TREE_THIS_VOLATILE (decl), context_die);
14995     }
14996
14997   if (DECL_ABSTRACT (decl))
14998     equate_decl_number_to_die (decl, type_die);
14999
15000   if (get_AT (type_die, DW_AT_name))
15001     add_pubtype (decl, type_die);
15002 }
15003
15004 /* Generate a type description DIE.  */
15005
15006 static void
15007 gen_type_die_with_usage (tree type, dw_die_ref context_die,
15008                                 enum debug_info_usage usage)
15009 {
15010   int need_pop;
15011   struct array_descr_info info;
15012
15013   if (type == NULL_TREE || type == error_mark_node)
15014     return;
15015
15016   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15017       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
15018     {
15019       if (TREE_ASM_WRITTEN (type))
15020         return;
15021
15022       /* Prevent broken recursion; we can't hand off to the same type.  */
15023       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
15024
15025       /* Use the DIE of the containing namespace as the parent DIE of
15026          the type description DIE we want to generate.  */
15027       if (DECL_CONTEXT (TYPE_NAME (type))
15028           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
15029         context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
15030
15031       TREE_ASM_WRITTEN (type) = 1;
15032       gen_decl_die (TYPE_NAME (type), NULL, context_die);
15033       return;
15034     }
15035
15036   /* If this is an array type with hidden descriptor, handle it first.  */
15037   if (!TREE_ASM_WRITTEN (type)
15038       && lang_hooks.types.get_array_descr_info
15039       && lang_hooks.types.get_array_descr_info (type, &info))
15040     {
15041       gen_descr_array_type_die (type, &info, context_die);
15042       TREE_ASM_WRITTEN (type) = 1;
15043       return;
15044     }
15045
15046   /* We are going to output a DIE to represent the unqualified version
15047      of this type (i.e. without any const or volatile qualifiers) so
15048      get the main variant (i.e. the unqualified version) of this type
15049      now.  (Vectors are special because the debugging info is in the
15050      cloned type itself).  */
15051   if (TREE_CODE (type) != VECTOR_TYPE)
15052     type = type_main_variant (type);
15053
15054   if (TREE_ASM_WRITTEN (type))
15055     return;
15056
15057   switch (TREE_CODE (type))
15058     {
15059     case ERROR_MARK:
15060       break;
15061
15062     case POINTER_TYPE:
15063     case REFERENCE_TYPE:
15064       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
15065          ensures that the gen_type_die recursion will terminate even if the
15066          type is recursive.  Recursive types are possible in Ada.  */
15067       /* ??? We could perhaps do this for all types before the switch
15068          statement.  */
15069       TREE_ASM_WRITTEN (type) = 1;
15070
15071       /* For these types, all that is required is that we output a DIE (or a
15072          set of DIEs) to represent the "basis" type.  */
15073       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15074                                 DINFO_USAGE_IND_USE);
15075       break;
15076
15077     case OFFSET_TYPE:
15078       /* This code is used for C++ pointer-to-data-member types.
15079          Output a description of the relevant class type.  */
15080       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
15081                                         DINFO_USAGE_IND_USE);
15082
15083       /* Output a description of the type of the object pointed to.  */
15084       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15085                                         DINFO_USAGE_IND_USE);
15086
15087       /* Now output a DIE to represent this pointer-to-data-member type
15088          itself.  */
15089       gen_ptr_to_mbr_type_die (type, context_die);
15090       break;
15091
15092     case FUNCTION_TYPE:
15093       /* Force out return type (in case it wasn't forced out already).  */
15094       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15095                                         DINFO_USAGE_DIR_USE);
15096       gen_subroutine_type_die (type, context_die);
15097       break;
15098
15099     case METHOD_TYPE:
15100       /* Force out return type (in case it wasn't forced out already).  */
15101       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15102                                         DINFO_USAGE_DIR_USE);
15103       gen_subroutine_type_die (type, context_die);
15104       break;
15105
15106     case ARRAY_TYPE:
15107       gen_array_type_die (type, context_die);
15108       break;
15109
15110     case VECTOR_TYPE:
15111       gen_array_type_die (type, context_die);
15112       break;
15113
15114     case ENUMERAL_TYPE:
15115     case RECORD_TYPE:
15116     case UNION_TYPE:
15117     case QUAL_UNION_TYPE:
15118       /* If this is a nested type whose containing class hasn't been written
15119          out yet, writing it out will cover this one, too.  This does not apply
15120          to instantiations of member class templates; they need to be added to
15121          the containing class as they are generated.  FIXME: This hurts the
15122          idea of combining type decls from multiple TUs, since we can't predict
15123          what set of template instantiations we'll get.  */
15124       if (TYPE_CONTEXT (type)
15125           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15126           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
15127         {
15128           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
15129
15130           if (TREE_ASM_WRITTEN (type))
15131             return;
15132
15133           /* If that failed, attach ourselves to the stub.  */
15134           push_decl_scope (TYPE_CONTEXT (type));
15135           context_die = lookup_type_die (TYPE_CONTEXT (type));
15136           need_pop = 1;
15137         }
15138       else
15139         {
15140           context_die = declare_in_namespace (type, context_die);
15141           need_pop = 0;
15142         }
15143
15144       if (TREE_CODE (type) == ENUMERAL_TYPE)
15145         {
15146           /* This might have been written out by the call to
15147              declare_in_namespace.  */
15148           if (!TREE_ASM_WRITTEN (type))
15149             gen_enumeration_type_die (type, context_die);
15150         }
15151       else
15152         gen_struct_or_union_type_die (type, context_die, usage);
15153
15154       if (need_pop)
15155         pop_decl_scope ();
15156
15157       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
15158          it up if it is ever completed.  gen_*_type_die will set it for us
15159          when appropriate.  */
15160       return;
15161
15162     case VOID_TYPE:
15163     case INTEGER_TYPE:
15164     case REAL_TYPE:
15165     case FIXED_POINT_TYPE:
15166     case COMPLEX_TYPE:
15167     case BOOLEAN_TYPE:
15168       /* No DIEs needed for fundamental types.  */
15169       break;
15170
15171     case LANG_TYPE:
15172       /* No Dwarf representation currently defined.  */
15173       break;
15174
15175     default:
15176       gcc_unreachable ();
15177     }
15178
15179   TREE_ASM_WRITTEN (type) = 1;
15180 }
15181
15182 static void
15183 gen_type_die (tree type, dw_die_ref context_die)
15184 {
15185   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
15186 }
15187
15188 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
15189    things which are local to the given block.  */
15190
15191 static void
15192 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
15193 {
15194   int must_output_die = 0;
15195   bool inlined_func;
15196
15197   /* Ignore blocks that are NULL.  */
15198   if (stmt == NULL_TREE)
15199     return;
15200
15201   inlined_func = inlined_function_outer_scope_p (stmt);
15202
15203   /* If the block is one fragment of a non-contiguous block, do not
15204      process the variables, since they will have been done by the
15205      origin block.  Do process subblocks.  */
15206   if (BLOCK_FRAGMENT_ORIGIN (stmt))
15207     {
15208       tree sub;
15209
15210       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
15211         gen_block_die (sub, context_die, depth + 1);
15212
15213       return;
15214     }
15215
15216   /* Determine if we need to output any Dwarf DIEs at all to represent this
15217      block.  */
15218   if (inlined_func)
15219     /* The outer scopes for inlinings *must* always be represented.  We
15220        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
15221     must_output_die = 1;
15222   else
15223     {
15224       /* Determine if this block directly contains any "significant"
15225          local declarations which we will need to output DIEs for.  */
15226       if (debug_info_level > DINFO_LEVEL_TERSE)
15227         /* We are not in terse mode so *any* local declaration counts
15228            as being a "significant" one.  */
15229         must_output_die = ((BLOCK_VARS (stmt) != NULL
15230                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
15231                            && (TREE_USED (stmt)
15232                                || TREE_ASM_WRITTEN (stmt)
15233                                || BLOCK_ABSTRACT (stmt)));
15234       else if ((TREE_USED (stmt)
15235                 || TREE_ASM_WRITTEN (stmt)
15236                 || BLOCK_ABSTRACT (stmt))
15237                && !dwarf2out_ignore_block (stmt))
15238         must_output_die = 1;
15239     }
15240
15241   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
15242      DIE for any block which contains no significant local declarations at
15243      all.  Rather, in such cases we just call `decls_for_scope' so that any
15244      needed Dwarf info for any sub-blocks will get properly generated. Note
15245      that in terse mode, our definition of what constitutes a "significant"
15246      local declaration gets restricted to include only inlined function
15247      instances and local (nested) function definitions.  */
15248   if (must_output_die)
15249     {
15250       if (inlined_func)
15251         gen_inlined_subroutine_die (stmt, context_die, depth);
15252       else
15253         gen_lexical_block_die (stmt, context_die, depth);
15254     }
15255   else
15256     decls_for_scope (stmt, context_die, depth);
15257 }
15258
15259 /* Process variable DECL (or variable with origin ORIGIN) within
15260    block STMT and add it to CONTEXT_DIE.  */
15261 static void
15262 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15263 {
15264   dw_die_ref die;
15265   tree decl_or_origin = decl ? decl : origin;
15266   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15267
15268   if (ultimate_origin)
15269     origin = ultimate_origin;
15270
15271   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15272     die = lookup_decl_die (decl_or_origin);
15273   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15274            && TYPE_DECL_IS_STUB (decl_or_origin))
15275     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15276   else
15277     die = NULL;
15278
15279   if (die != NULL && die->die_parent == NULL)
15280     add_child_die (context_die, die);
15281   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15282     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15283                                          stmt, context_die);
15284   else
15285     gen_decl_die (decl, origin, context_die);
15286 }
15287
15288 /* Generate all of the decls declared within a given scope and (recursively)
15289    all of its sub-blocks.  */
15290
15291 static void
15292 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15293 {
15294   tree decl;
15295   unsigned int i;
15296   tree subblocks;
15297
15298   /* Ignore NULL blocks.  */
15299   if (stmt == NULL_TREE)
15300     return;
15301
15302   /* Output the DIEs to represent all of the data objects and typedefs
15303      declared directly within this block but not within any nested
15304      sub-blocks.  Also, nested function and tag DIEs have been
15305      generated with a parent of NULL; fix that up now.  */
15306   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15307     process_scope_var (stmt, decl, NULL_TREE, context_die);
15308   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15309     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15310                        context_die);
15311
15312   /* If we're at -g1, we're not interested in subblocks.  */
15313   if (debug_info_level <= DINFO_LEVEL_TERSE)
15314     return;
15315
15316   /* Output the DIEs to represent all sub-blocks (and the items declared
15317      therein) of this block.  */
15318   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15319        subblocks != NULL;
15320        subblocks = BLOCK_CHAIN (subblocks))
15321     gen_block_die (subblocks, context_die, depth + 1);
15322 }
15323
15324 /* Is this a typedef we can avoid emitting?  */
15325
15326 static inline int
15327 is_redundant_typedef (const_tree decl)
15328 {
15329   if (TYPE_DECL_IS_STUB (decl))
15330     return 1;
15331
15332   if (DECL_ARTIFICIAL (decl)
15333       && DECL_CONTEXT (decl)
15334       && is_tagged_type (DECL_CONTEXT (decl))
15335       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15336       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15337     /* Also ignore the artificial member typedef for the class name.  */
15338     return 1;
15339
15340   return 0;
15341 }
15342
15343 /* Returns the DIE for a context.  */
15344
15345 static inline dw_die_ref
15346 get_context_die (tree context)
15347 {
15348   if (context)
15349     {
15350       /* Find die that represents this context.  */
15351       if (TYPE_P (context))
15352         return force_type_die (context);
15353       else
15354         return force_decl_die (context);
15355     }
15356   return comp_unit_die;
15357 }
15358
15359 /* Returns the DIE for decl.  A DIE will always be returned.  */
15360
15361 static dw_die_ref
15362 force_decl_die (tree decl)
15363 {
15364   dw_die_ref decl_die;
15365   unsigned saved_external_flag;
15366   tree save_fn = NULL_TREE;
15367   decl_die = lookup_decl_die (decl);
15368   if (!decl_die)
15369     {
15370       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15371
15372       decl_die = lookup_decl_die (decl);
15373       if (decl_die)
15374         return decl_die;
15375
15376       switch (TREE_CODE (decl))
15377         {
15378         case FUNCTION_DECL:
15379           /* Clear current_function_decl, so that gen_subprogram_die thinks
15380              that this is a declaration. At this point, we just want to force
15381              declaration die.  */
15382           save_fn = current_function_decl;
15383           current_function_decl = NULL_TREE;
15384           gen_subprogram_die (decl, context_die);
15385           current_function_decl = save_fn;
15386           break;
15387
15388         case VAR_DECL:
15389           /* Set external flag to force declaration die. Restore it after
15390            gen_decl_die() call.  */
15391           saved_external_flag = DECL_EXTERNAL (decl);
15392           DECL_EXTERNAL (decl) = 1;
15393           gen_decl_die (decl, NULL, context_die);
15394           DECL_EXTERNAL (decl) = saved_external_flag;
15395           break;
15396
15397         case NAMESPACE_DECL:
15398           dwarf2out_decl (decl);
15399           break;
15400
15401         default:
15402           gcc_unreachable ();
15403         }
15404
15405       /* We should be able to find the DIE now.  */
15406       if (!decl_die)
15407         decl_die = lookup_decl_die (decl);
15408       gcc_assert (decl_die);
15409     }
15410
15411   return decl_die;
15412 }
15413
15414 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15415    always returned.  */
15416
15417 static dw_die_ref
15418 force_type_die (tree type)
15419 {
15420   dw_die_ref type_die;
15421
15422   type_die = lookup_type_die (type);
15423   if (!type_die)
15424     {
15425       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15426
15427       type_die = modified_type_die (type, TYPE_READONLY (type),
15428                                     TYPE_VOLATILE (type), context_die);
15429       gcc_assert (type_die);
15430     }
15431   return type_die;
15432 }
15433
15434 /* Force out any required namespaces to be able to output DECL,
15435    and return the new context_die for it, if it's changed.  */
15436
15437 static dw_die_ref
15438 setup_namespace_context (tree thing, dw_die_ref context_die)
15439 {
15440   tree context = (DECL_P (thing)
15441                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15442   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15443     /* Force out the namespace.  */
15444     context_die = force_decl_die (context);
15445
15446   return context_die;
15447 }
15448
15449 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15450    type) within its namespace, if appropriate.
15451
15452    For compatibility with older debuggers, namespace DIEs only contain
15453    declarations; all definitions are emitted at CU scope.  */
15454
15455 static dw_die_ref
15456 declare_in_namespace (tree thing, dw_die_ref context_die)
15457 {
15458   dw_die_ref ns_context;
15459
15460   if (debug_info_level <= DINFO_LEVEL_TERSE)
15461     return context_die;
15462
15463   /* If this decl is from an inlined function, then don't try to emit it in its
15464      namespace, as we will get confused.  It would have already been emitted
15465      when the abstract instance of the inline function was emitted anyways.  */
15466   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15467     return context_die;
15468
15469   ns_context = setup_namespace_context (thing, context_die);
15470
15471   if (ns_context != context_die)
15472     {
15473       if (is_fortran ())
15474         return ns_context;
15475       if (DECL_P (thing))
15476         gen_decl_die (thing, NULL, ns_context);
15477       else
15478         gen_type_die (thing, ns_context);
15479     }
15480   return context_die;
15481 }
15482
15483 /* Generate a DIE for a namespace or namespace alias.  */
15484
15485 static void
15486 gen_namespace_die (tree decl, dw_die_ref context_die)
15487 {
15488   dw_die_ref namespace_die;
15489
15490   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15491      they are an alias of.  */
15492   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15493     {
15494       /* Output a real namespace or module.  */
15495       context_die = setup_namespace_context (decl, comp_unit_die);
15496       namespace_die = new_die (is_fortran ()
15497                                ? DW_TAG_module : DW_TAG_namespace,
15498                                context_die, decl);
15499       /* For Fortran modules defined in different CU don't add src coords.  */
15500       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15501         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15502       else
15503         add_name_and_src_coords_attributes (namespace_die, decl);
15504       if (DECL_EXTERNAL (decl))
15505         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15506       equate_decl_number_to_die (decl, namespace_die);
15507     }
15508   else
15509     {
15510       /* Output a namespace alias.  */
15511
15512       /* Force out the namespace we are an alias of, if necessary.  */
15513       dw_die_ref origin_die
15514         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15515
15516       if (DECL_CONTEXT (decl) == NULL_TREE
15517           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15518         context_die = setup_namespace_context (decl, comp_unit_die);
15519       /* Now create the namespace alias DIE.  */
15520       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15521       add_name_and_src_coords_attributes (namespace_die, decl);
15522       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15523       equate_decl_number_to_die (decl, namespace_die);
15524     }
15525 }
15526
15527 /* Generate Dwarf debug information for a decl described by DECL.  */
15528
15529 static void
15530 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15531 {
15532   tree decl_or_origin = decl ? decl : origin;
15533   tree class_origin = NULL;
15534
15535   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15536     return;
15537
15538   switch (TREE_CODE (decl_or_origin))
15539     {
15540     case ERROR_MARK:
15541       break;
15542
15543     case CONST_DECL:
15544       if (!is_fortran ())
15545         {
15546           /* The individual enumerators of an enum type get output when we output
15547              the Dwarf representation of the relevant enum type itself.  */
15548           break;
15549         }
15550
15551       /* Emit its type.  */
15552       gen_type_die (TREE_TYPE (decl), context_die);
15553
15554       /* And its containing namespace.  */
15555       context_die = declare_in_namespace (decl, context_die);
15556
15557       gen_const_die (decl, context_die);
15558       break;
15559
15560     case FUNCTION_DECL:
15561       /* Don't output any DIEs to represent mere function declarations,
15562          unless they are class members or explicit block externs.  */
15563       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15564           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15565           && (current_function_decl == NULL_TREE
15566               || DECL_ARTIFICIAL (decl_or_origin)))
15567         break;
15568
15569 #if 0
15570       /* FIXME */
15571       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15572          on local redeclarations of global functions.  That seems broken.  */
15573       if (current_function_decl != decl)
15574         /* This is only a declaration.  */;
15575 #endif
15576
15577       /* If we're emitting a clone, emit info for the abstract instance.  */
15578       if (origin || DECL_ORIGIN (decl) != decl)
15579         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15580
15581       /* If we're emitting an out-of-line copy of an inline function,
15582          emit info for the abstract instance and set up to refer to it.  */
15583       else if (cgraph_function_possibly_inlined_p (decl)
15584                && ! DECL_ABSTRACT (decl)
15585                && ! class_or_namespace_scope_p (context_die)
15586                /* dwarf2out_abstract_function won't emit a die if this is just
15587                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15588                   that case, because that works only if we have a die.  */
15589                && DECL_INITIAL (decl) != NULL_TREE)
15590         {
15591           dwarf2out_abstract_function (decl);
15592           set_decl_origin_self (decl);
15593         }
15594
15595       /* Otherwise we're emitting the primary DIE for this decl.  */
15596       else if (debug_info_level > DINFO_LEVEL_TERSE)
15597         {
15598           /* Before we describe the FUNCTION_DECL itself, make sure that we
15599              have described its return type.  */
15600           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15601
15602           /* And its virtual context.  */
15603           if (DECL_VINDEX (decl) != NULL_TREE)
15604             gen_type_die (DECL_CONTEXT (decl), context_die);
15605
15606           /* And its containing type.  */
15607           if (!origin)
15608             origin = decl_class_context (decl);
15609           if (origin != NULL_TREE)
15610             gen_type_die_for_member (origin, decl, context_die);
15611
15612           /* And its containing namespace.  */
15613           context_die = declare_in_namespace (decl, context_die);
15614         }
15615
15616       /* Now output a DIE to represent the function itself.  */
15617       if (decl)
15618         gen_subprogram_die (decl, context_die);
15619       break;
15620
15621     case TYPE_DECL:
15622       /* If we are in terse mode, don't generate any DIEs to represent any
15623          actual typedefs.  */
15624       if (debug_info_level <= DINFO_LEVEL_TERSE)
15625         break;
15626
15627       /* In the special case of a TYPE_DECL node representing the declaration
15628          of some type tag, if the given TYPE_DECL is marked as having been
15629          instantiated from some other (original) TYPE_DECL node (e.g. one which
15630          was generated within the original definition of an inline function) we
15631          used to generate a special (abbreviated) DW_TAG_structure_type,
15632          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15633          should be actually referencing those DIEs, as variable DIEs with that
15634          type would be emitted already in the abstract origin, so it was always
15635          removed during unused type prunning.  Don't add anything in this
15636          case.  */
15637       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15638         break;
15639
15640       if (is_redundant_typedef (decl))
15641         gen_type_die (TREE_TYPE (decl), context_die);
15642       else
15643         /* Output a DIE to represent the typedef itself.  */
15644         gen_typedef_die (decl, context_die);
15645       break;
15646
15647     case LABEL_DECL:
15648       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15649         gen_label_die (decl, context_die);
15650       break;
15651
15652     case VAR_DECL:
15653     case RESULT_DECL:
15654       /* If we are in terse mode, don't generate any DIEs to represent any
15655          variable declarations or definitions.  */
15656       if (debug_info_level <= DINFO_LEVEL_TERSE)
15657         break;
15658
15659       /* Output any DIEs that are needed to specify the type of this data
15660          object.  */
15661       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
15662            || TREE_CODE (decl_or_origin) == VAR_DECL)
15663           && DECL_BY_REFERENCE (decl_or_origin))
15664         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15665       else
15666         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15667
15668       /* And its containing type.  */
15669       class_origin = decl_class_context (decl_or_origin);
15670       if (class_origin != NULL_TREE)
15671         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15672
15673       /* And its containing namespace.  */
15674       context_die = declare_in_namespace (decl_or_origin, context_die);
15675
15676       /* Now output the DIE to represent the data object itself.  This gets
15677          complicated because of the possibility that the VAR_DECL really
15678          represents an inlined instance of a formal parameter for an inline
15679          function.  */
15680       if (!origin)
15681         origin = decl_ultimate_origin (decl);
15682       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15683         gen_formal_parameter_die (decl, origin, context_die);
15684       else
15685         gen_variable_die (decl, origin, context_die);
15686       break;
15687
15688     case FIELD_DECL:
15689       /* Ignore the nameless fields that are used to skip bits but handle C++
15690          anonymous unions and structs.  */
15691       if (DECL_NAME (decl) != NULL_TREE
15692           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15693           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15694         {
15695           gen_type_die (member_declared_type (decl), context_die);
15696           gen_field_die (decl, context_die);
15697         }
15698       break;
15699
15700     case PARM_DECL:
15701       if (DECL_BY_REFERENCE (decl_or_origin))
15702         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15703       else
15704         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15705       gen_formal_parameter_die (decl, origin, context_die);
15706       break;
15707
15708     case NAMESPACE_DECL:
15709     case IMPORTED_DECL:
15710       gen_namespace_die (decl, context_die);
15711       break;
15712
15713     default:
15714       /* Probably some frontend-internal decl.  Assume we don't care.  */
15715       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15716       break;
15717     }
15718 }
15719 \f
15720 /* Output debug information for global decl DECL.  Called from toplev.c after
15721    compilation proper has finished.  */
15722
15723 static void
15724 dwarf2out_global_decl (tree decl)
15725 {
15726   /* Output DWARF2 information for file-scope tentative data object
15727      declarations, file-scope (extern) function declarations (which
15728      had no corresponding body) and file-scope tagged type declarations
15729      and definitions which have not yet been forced out.  */
15730   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15731     dwarf2out_decl (decl);
15732 }
15733
15734 /* Output debug information for type decl DECL.  Called from toplev.c
15735    and from language front ends (to record built-in types).  */
15736 static void
15737 dwarf2out_type_decl (tree decl, int local)
15738 {
15739   if (!local)
15740     dwarf2out_decl (decl);
15741 }
15742
15743 /* Output debug information for imported module or decl DECL.
15744    NAME is non-NULL name in the lexical block if the decl has been renamed.
15745    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15746    that DECL belongs to.
15747    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15748 static void
15749 dwarf2out_imported_module_or_decl_1 (tree decl,
15750                                      tree name,
15751                                      tree lexical_block,
15752                                      dw_die_ref lexical_block_die)
15753 {
15754   expanded_location xloc;
15755   dw_die_ref imported_die = NULL;
15756   dw_die_ref at_import_die;
15757
15758   if (TREE_CODE (decl) == IMPORTED_DECL)
15759     {
15760       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15761       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15762       gcc_assert (decl);
15763     }
15764   else
15765     xloc = expand_location (input_location);
15766
15767   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15768     {
15769       if (is_base_type (TREE_TYPE (decl)))
15770         at_import_die = base_type_die (TREE_TYPE (decl));
15771       else
15772         at_import_die = force_type_die (TREE_TYPE (decl));
15773       /* For namespace N { typedef void T; } using N::T; base_type_die
15774          returns NULL, but DW_TAG_imported_declaration requires
15775          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15776       if (!at_import_die)
15777         {
15778           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15779           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15780           at_import_die = lookup_type_die (TREE_TYPE (decl));
15781           gcc_assert (at_import_die);
15782         }
15783     }
15784   else
15785     {
15786       at_import_die = lookup_decl_die (decl);
15787       if (!at_import_die)
15788         {
15789           /* If we're trying to avoid duplicate debug info, we may not have
15790              emitted the member decl for this field.  Emit it now.  */
15791           if (TREE_CODE (decl) == FIELD_DECL)
15792             {
15793               tree type = DECL_CONTEXT (decl);
15794
15795               if (TYPE_CONTEXT (type)
15796                   && TYPE_P (TYPE_CONTEXT (type))
15797                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15798                                                 DINFO_USAGE_DIR_USE))
15799                 return;
15800               gen_type_die_for_member (type, decl,
15801                                        get_context_die (TYPE_CONTEXT (type)));
15802             }
15803           at_import_die = force_decl_die (decl);
15804         }
15805     }
15806
15807   if (TREE_CODE (decl) == NAMESPACE_DECL)
15808     imported_die = new_die (DW_TAG_imported_module,
15809                             lexical_block_die,
15810                             lexical_block);
15811   else
15812     imported_die = new_die (DW_TAG_imported_declaration,
15813                             lexical_block_die,
15814                             lexical_block);
15815
15816   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15817   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15818   if (name)
15819     add_AT_string (imported_die, DW_AT_name,
15820                    IDENTIFIER_POINTER (name));
15821   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15822 }
15823
15824 /* Output debug information for imported module or decl DECL.
15825    NAME is non-NULL name in context if the decl has been renamed.
15826    CHILD is true if decl is one of the renamed decls as part of
15827    importing whole module.  */
15828
15829 static void
15830 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15831                                    bool child)
15832 {
15833   /* dw_die_ref at_import_die;  */
15834   dw_die_ref scope_die;
15835
15836   if (debug_info_level <= DINFO_LEVEL_TERSE)
15837     return;
15838
15839   gcc_assert (decl);
15840
15841   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15842      We need decl DIE for reference and scope die. First, get DIE for the decl
15843      itself.  */
15844
15845   /* Get the scope die for decl context. Use comp_unit_die for global module
15846      or decl. If die is not found for non globals, force new die.  */
15847   if (context
15848       && TYPE_P (context)
15849       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15850     return;
15851   scope_die = get_context_die (context);
15852
15853   if (child)
15854     {
15855       gcc_assert (scope_die->die_child);
15856       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15857       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15858       scope_die = scope_die->die_child;
15859     }
15860
15861   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15862   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15863
15864 }
15865
15866 /* Write the debugging output for DECL.  */
15867
15868 void
15869 dwarf2out_decl (tree decl)
15870 {
15871   dw_die_ref context_die = comp_unit_die;
15872
15873   switch (TREE_CODE (decl))
15874     {
15875     case ERROR_MARK:
15876       return;
15877
15878     case FUNCTION_DECL:
15879       /* What we would really like to do here is to filter out all mere
15880          file-scope declarations of file-scope functions which are never
15881          referenced later within this translation unit (and keep all of ones
15882          that *are* referenced later on) but we aren't clairvoyant, so we have
15883          no idea which functions will be referenced in the future (i.e. later
15884          on within the current translation unit). So here we just ignore all
15885          file-scope function declarations which are not also definitions.  If
15886          and when the debugger needs to know something about these functions,
15887          it will have to hunt around and find the DWARF information associated
15888          with the definition of the function.
15889
15890          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15891          nodes represent definitions and which ones represent mere
15892          declarations.  We have to check DECL_INITIAL instead. That's because
15893          the C front-end supports some weird semantics for "extern inline"
15894          function definitions.  These can get inlined within the current
15895          translation unit (and thus, we need to generate Dwarf info for their
15896          abstract instances so that the Dwarf info for the concrete inlined
15897          instances can have something to refer to) but the compiler never
15898          generates any out-of-lines instances of such things (despite the fact
15899          that they *are* definitions).
15900
15901          The important point is that the C front-end marks these "extern
15902          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15903          them anyway. Note that the C++ front-end also plays some similar games
15904          for inline function definitions appearing within include files which
15905          also contain `#pragma interface' pragmas.  */
15906       if (DECL_INITIAL (decl) == NULL_TREE)
15907         return;
15908
15909       /* If we're a nested function, initially use a parent of NULL; if we're
15910          a plain function, this will be fixed up in decls_for_scope.  If
15911          we're a method, it will be ignored, since we already have a DIE.  */
15912       if (decl_function_context (decl)
15913           /* But if we're in terse mode, we don't care about scope.  */
15914           && debug_info_level > DINFO_LEVEL_TERSE)
15915         context_die = NULL;
15916       break;
15917
15918     case VAR_DECL:
15919       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15920          declaration and if the declaration was never even referenced from
15921          within this entire compilation unit.  We suppress these DIEs in
15922          order to save space in the .debug section (by eliminating entries
15923          which are probably useless).  Note that we must not suppress
15924          block-local extern declarations (whether used or not) because that
15925          would screw-up the debugger's name lookup mechanism and cause it to
15926          miss things which really ought to be in scope at a given point.  */
15927       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15928         return;
15929
15930       /* For local statics lookup proper context die.  */
15931       if (TREE_STATIC (decl) && decl_function_context (decl))
15932         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15933
15934       /* If we are in terse mode, don't generate any DIEs to represent any
15935          variable declarations or definitions.  */
15936       if (debug_info_level <= DINFO_LEVEL_TERSE)
15937         return;
15938       break;
15939
15940     case CONST_DECL:
15941       if (debug_info_level <= DINFO_LEVEL_TERSE)
15942         return;
15943       if (!is_fortran ())
15944         return;
15945       if (TREE_STATIC (decl) && decl_function_context (decl))
15946         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15947       break;
15948
15949     case NAMESPACE_DECL:
15950     case IMPORTED_DECL:
15951       if (debug_info_level <= DINFO_LEVEL_TERSE)
15952         return;
15953       if (lookup_decl_die (decl) != NULL)
15954         return;
15955       break;
15956
15957     case TYPE_DECL:
15958       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15959       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15960         return;
15961
15962       /* Don't bother trying to generate any DIEs to represent any of the
15963          normal built-in types for the language we are compiling.  */
15964       if (DECL_IS_BUILTIN (decl))
15965         {
15966           /* OK, we need to generate one for `bool' so GDB knows what type
15967              comparisons have.  */
15968           if (is_cxx ()
15969               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15970               && ! DECL_IGNORED_P (decl))
15971             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15972
15973           return;
15974         }
15975
15976       /* If we are in terse mode, don't generate any DIEs for types.  */
15977       if (debug_info_level <= DINFO_LEVEL_TERSE)
15978         return;
15979
15980       /* If we're a function-scope tag, initially use a parent of NULL;
15981          this will be fixed up in decls_for_scope.  */
15982       if (decl_function_context (decl))
15983         context_die = NULL;
15984
15985       break;
15986
15987     default:
15988       return;
15989     }
15990
15991   gen_decl_die (decl, NULL, context_die);
15992 }
15993
15994 /* Output a marker (i.e. a label) for the beginning of the generated code for
15995    a lexical block.  */
15996
15997 static void
15998 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15999                        unsigned int blocknum)
16000 {
16001   switch_to_section (current_function_section ());
16002   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
16003 }
16004
16005 /* Output a marker (i.e. a label) for the end of the generated code for a
16006    lexical block.  */
16007
16008 static void
16009 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
16010 {
16011   switch_to_section (current_function_section ());
16012   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
16013 }
16014
16015 /* Returns nonzero if it is appropriate not to emit any debugging
16016    information for BLOCK, because it doesn't contain any instructions.
16017
16018    Don't allow this for blocks with nested functions or local classes
16019    as we would end up with orphans, and in the presence of scheduling
16020    we may end up calling them anyway.  */
16021
16022 static bool
16023 dwarf2out_ignore_block (const_tree block)
16024 {
16025   tree decl;
16026   unsigned int i;
16027
16028   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
16029     if (TREE_CODE (decl) == FUNCTION_DECL
16030         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16031       return 0;
16032   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
16033     {
16034       decl = BLOCK_NONLOCALIZED_VAR (block, i);
16035       if (TREE_CODE (decl) == FUNCTION_DECL
16036           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
16037       return 0;
16038     }
16039
16040   return 1;
16041 }
16042
16043 /* Hash table routines for file_hash.  */
16044
16045 static int
16046 file_table_eq (const void *p1_p, const void *p2_p)
16047 {
16048   const struct dwarf_file_data *const p1 =
16049     (const struct dwarf_file_data *) p1_p;
16050   const char *const p2 = (const char *) p2_p;
16051   return strcmp (p1->filename, p2) == 0;
16052 }
16053
16054 static hashval_t
16055 file_table_hash (const void *p_p)
16056 {
16057   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
16058   return htab_hash_string (p->filename);
16059 }
16060
16061 /* Lookup FILE_NAME (in the list of filenames that we know about here in
16062    dwarf2out.c) and return its "index".  The index of each (known) filename is
16063    just a unique number which is associated with only that one filename.  We
16064    need such numbers for the sake of generating labels (in the .debug_sfnames
16065    section) and references to those files numbers (in the .debug_srcinfo
16066    and.debug_macinfo sections).  If the filename given as an argument is not
16067    found in our current list, add it to the list and assign it the next
16068    available unique index number.  In order to speed up searches, we remember
16069    the index of the filename was looked up last.  This handles the majority of
16070    all searches.  */
16071
16072 static struct dwarf_file_data *
16073 lookup_filename (const char *file_name)
16074 {
16075   void ** slot;
16076   struct dwarf_file_data * created;
16077
16078   /* Check to see if the file name that was searched on the previous
16079      call matches this file name.  If so, return the index.  */
16080   if (file_table_last_lookup
16081       && (file_name == file_table_last_lookup->filename
16082           || strcmp (file_table_last_lookup->filename, file_name) == 0))
16083     return file_table_last_lookup;
16084
16085   /* Didn't match the previous lookup, search the table.  */
16086   slot = htab_find_slot_with_hash (file_table, file_name,
16087                                    htab_hash_string (file_name), INSERT);
16088   if (*slot)
16089     return (struct dwarf_file_data *) *slot;
16090
16091   created = GGC_NEW (struct dwarf_file_data);
16092   created->filename = file_name;
16093   created->emitted_number = 0;
16094   *slot = created;
16095   return created;
16096 }
16097
16098 /* If the assembler will construct the file table, then translate the compiler
16099    internal file table number into the assembler file table number, and emit
16100    a .file directive if we haven't already emitted one yet.  The file table
16101    numbers are different because we prune debug info for unused variables and
16102    types, which may include filenames.  */
16103
16104 static int
16105 maybe_emit_file (struct dwarf_file_data * fd)
16106 {
16107   if (! fd->emitted_number)
16108     {
16109       if (last_emitted_file)
16110         fd->emitted_number = last_emitted_file->emitted_number + 1;
16111       else
16112         fd->emitted_number = 1;
16113       last_emitted_file = fd;
16114
16115       if (DWARF2_ASM_LINE_DEBUG_INFO)
16116         {
16117           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
16118           output_quoted_string (asm_out_file,
16119                                 remap_debug_filename (fd->filename));
16120           fputc ('\n', asm_out_file);
16121         }
16122     }
16123
16124   return fd->emitted_number;
16125 }
16126
16127 /* Replace DW_AT_name for the decl with name.  */
16128  
16129 static void
16130 dwarf2out_set_name (tree decl, tree name)
16131 {
16132   dw_die_ref die;
16133   dw_attr_ref attr;
16134
16135   die = TYPE_SYMTAB_DIE (decl);
16136   if (!die)
16137     return;
16138
16139   attr = get_AT (die, DW_AT_name);
16140   if (attr)
16141     {
16142       struct indirect_string_node *node;
16143
16144       node = find_AT_string (dwarf2_name (name, 0));
16145       /* replace the string.  */
16146       attr->dw_attr_val.v.val_str = node;
16147     }
16148
16149   else
16150     add_name_attribute (die, dwarf2_name (name, 0));
16151 }
16152 /* Called by the final INSN scan whenever we see a var location.  We
16153    use it to drop labels in the right places, and throw the location in
16154    our lookup table.  */
16155
16156 static void
16157 dwarf2out_var_location (rtx loc_note)
16158 {
16159   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
16160   struct var_loc_node *newloc;
16161   rtx prev_insn;
16162   static rtx last_insn;
16163   static const char *last_label;
16164   tree decl;
16165
16166   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
16167     return;
16168   prev_insn = PREV_INSN (loc_note);
16169
16170   newloc = GGC_CNEW (struct var_loc_node);
16171   /* If the insn we processed last time is the previous insn
16172      and it is also a var location note, use the label we emitted
16173      last time.  */
16174   if (last_insn != NULL_RTX
16175       && last_insn == prev_insn
16176       && NOTE_P (prev_insn)
16177       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
16178     {
16179       newloc->label = last_label;
16180     }
16181   else
16182     {
16183       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
16184       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
16185       loclabel_num++;
16186       newloc->label = ggc_strdup (loclabel);
16187     }
16188   newloc->var_loc_note = loc_note;
16189   newloc->next = NULL;
16190
16191   if (cfun && in_cold_section_p)
16192     newloc->section_label = crtl->subsections.cold_section_label;
16193   else
16194     newloc->section_label = text_section_label;
16195
16196   last_insn = loc_note;
16197   last_label = newloc->label;
16198   decl = NOTE_VAR_LOCATION_DECL (loc_note);
16199   add_var_loc_to_decl (decl, newloc);
16200 }
16201
16202 /* We need to reset the locations at the beginning of each
16203    function. We can't do this in the end_function hook, because the
16204    declarations that use the locations won't have been output when
16205    that hook is called.  Also compute have_multiple_function_sections here.  */
16206
16207 static void
16208 dwarf2out_begin_function (tree fun)
16209 {
16210   htab_empty (decl_loc_table);
16211
16212   if (function_section (fun) != text_section)
16213     have_multiple_function_sections = true;
16214
16215   dwarf2out_note_section_used ();
16216 }
16217
16218 /* Output a label to mark the beginning of a source code line entry
16219    and record information relating to this source line, in
16220    'line_info_table' for later output of the .debug_line section.  */
16221
16222 static void
16223 dwarf2out_source_line (unsigned int line, const char *filename,
16224                        int discriminator ATTRIBUTE_UNUSED)
16225 {
16226   if (debug_info_level >= DINFO_LEVEL_NORMAL
16227       && line != 0)
16228     {
16229       int file_num = maybe_emit_file (lookup_filename (filename));
16230
16231       switch_to_section (current_function_section ());
16232
16233       /* If requested, emit something human-readable.  */
16234       if (flag_debug_asm)
16235         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
16236                  filename, line);
16237
16238       if (DWARF2_ASM_LINE_DEBUG_INFO)
16239         {
16240           /* Emit the .loc directive understood by GNU as.  */
16241           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
16242 #ifdef HAVE_GAS_DISCRIMINATOR
16243           if (discriminator != 0)
16244             fprintf (asm_out_file, " discriminator %d", discriminator);
16245 #endif /* HAVE_GAS_DISCRIMINATOR */
16246           fputc ('\n', asm_out_file);
16247
16248           /* Indicate that line number info exists.  */
16249           line_info_table_in_use++;
16250         }
16251       else if (function_section (current_function_decl) != text_section)
16252         {
16253           dw_separate_line_info_ref line_info;
16254           targetm.asm_out.internal_label (asm_out_file,
16255                                           SEPARATE_LINE_CODE_LABEL,
16256                                           separate_line_info_table_in_use);
16257
16258           /* Expand the line info table if necessary.  */
16259           if (separate_line_info_table_in_use
16260               == separate_line_info_table_allocated)
16261             {
16262               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16263               separate_line_info_table
16264                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
16265                                  separate_line_info_table,
16266                                  separate_line_info_table_allocated);
16267               memset (separate_line_info_table
16268                        + separate_line_info_table_in_use,
16269                       0,
16270                       (LINE_INFO_TABLE_INCREMENT
16271                        * sizeof (dw_separate_line_info_entry)));
16272             }
16273
16274           /* Add the new entry at the end of the line_info_table.  */
16275           line_info
16276             = &separate_line_info_table[separate_line_info_table_in_use++];
16277           line_info->dw_file_num = file_num;
16278           line_info->dw_line_num = line;
16279           line_info->function = current_function_funcdef_no;
16280         }
16281       else
16282         {
16283           dw_line_info_ref line_info;
16284
16285           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
16286                                      line_info_table_in_use);
16287
16288           /* Expand the line info table if necessary.  */
16289           if (line_info_table_in_use == line_info_table_allocated)
16290             {
16291               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16292               line_info_table
16293                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
16294                                  line_info_table_allocated);
16295               memset (line_info_table + line_info_table_in_use, 0,
16296                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
16297             }
16298
16299           /* Add the new entry at the end of the line_info_table.  */
16300           line_info = &line_info_table[line_info_table_in_use++];
16301           line_info->dw_file_num = file_num;
16302           line_info->dw_line_num = line;
16303         }
16304     }
16305 }
16306
16307 /* Record the beginning of a new source file.  */
16308
16309 static void
16310 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16311 {
16312   if (flag_eliminate_dwarf2_dups)
16313     {
16314       /* Record the beginning of the file for break_out_includes.  */
16315       dw_die_ref bincl_die;
16316
16317       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16318       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16319     }
16320
16321   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16322     {
16323       int file_num = maybe_emit_file (lookup_filename (filename));
16324
16325       switch_to_section (debug_macinfo_section);
16326       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16327       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16328                                    lineno);
16329
16330       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16331     }
16332 }
16333
16334 /* Record the end of a source file.  */
16335
16336 static void
16337 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16338 {
16339   if (flag_eliminate_dwarf2_dups)
16340     /* Record the end of the file for break_out_includes.  */
16341     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16342
16343   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16344     {
16345       switch_to_section (debug_macinfo_section);
16346       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16347     }
16348 }
16349
16350 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16351    the tail part of the directive line, i.e. the part which is past the
16352    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16353
16354 static void
16355 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16356                   const char *buffer ATTRIBUTE_UNUSED)
16357 {
16358   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16359     {
16360       switch_to_section (debug_macinfo_section);
16361       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16362       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16363       dw2_asm_output_nstring (buffer, -1, "The macro");
16364     }
16365 }
16366
16367 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16368    the tail part of the directive line, i.e. the part which is past the
16369    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16370
16371 static void
16372 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16373                  const char *buffer ATTRIBUTE_UNUSED)
16374 {
16375   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16376     {
16377       switch_to_section (debug_macinfo_section);
16378       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16379       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16380       dw2_asm_output_nstring (buffer, -1, "The macro");
16381     }
16382 }
16383
16384 /* Set up for Dwarf output at the start of compilation.  */
16385
16386 static void
16387 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16388 {
16389   /* Allocate the file_table.  */
16390   file_table = htab_create_ggc (50, file_table_hash,
16391                                 file_table_eq, NULL);
16392
16393   /* Allocate the decl_die_table.  */
16394   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16395                                     decl_die_table_eq, NULL);
16396
16397   /* Allocate the decl_loc_table.  */
16398   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16399                                     decl_loc_table_eq, NULL);
16400
16401   /* Allocate the initial hunk of the decl_scope_table.  */
16402   decl_scope_table = VEC_alloc (tree, gc, 256);
16403
16404   /* Allocate the initial hunk of the abbrev_die_table.  */
16405   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16406   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16407   /* Zero-th entry is allocated, but unused.  */
16408   abbrev_die_table_in_use = 1;
16409
16410   /* Allocate the initial hunk of the line_info_table.  */
16411   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16412   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16413
16414   /* Zero-th entry is allocated, but unused.  */
16415   line_info_table_in_use = 1;
16416
16417   /* Allocate the pubtypes and pubnames vectors.  */
16418   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16419   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16420
16421   /* Generate the initial DIE for the .debug section.  Note that the (string)
16422      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16423      will (typically) be a relative pathname and that this pathname should be
16424      taken as being relative to the directory from which the compiler was
16425      invoked when the given (base) source file was compiled.  We will fill
16426      in this value in dwarf2out_finish.  */
16427   comp_unit_die = gen_compile_unit_die (NULL);
16428
16429   incomplete_types = VEC_alloc (tree, gc, 64);
16430
16431   used_rtx_array = VEC_alloc (rtx, gc, 32);
16432
16433   debug_info_section = get_section (DEBUG_INFO_SECTION,
16434                                     SECTION_DEBUG, NULL);
16435   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16436                                       SECTION_DEBUG, NULL);
16437   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16438                                        SECTION_DEBUG, NULL);
16439   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16440                                        SECTION_DEBUG, NULL);
16441   debug_line_section = get_section (DEBUG_LINE_SECTION,
16442                                     SECTION_DEBUG, NULL);
16443   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16444                                    SECTION_DEBUG, NULL);
16445   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16446                                         SECTION_DEBUG, NULL);
16447 #ifdef DEBUG_PUBTYPES_SECTION
16448   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16449                                         SECTION_DEBUG, NULL);
16450 #endif
16451   debug_str_section = get_section (DEBUG_STR_SECTION,
16452                                    DEBUG_STR_SECTION_FLAGS, NULL);
16453   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16454                                       SECTION_DEBUG, NULL);
16455   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16456                                      SECTION_DEBUG, NULL);
16457
16458   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16459   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16460                                DEBUG_ABBREV_SECTION_LABEL, 0);
16461   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16462   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16463                                COLD_TEXT_SECTION_LABEL, 0);
16464   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16465
16466   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16467                                DEBUG_INFO_SECTION_LABEL, 0);
16468   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16469                                DEBUG_LINE_SECTION_LABEL, 0);
16470   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16471                                DEBUG_RANGES_SECTION_LABEL, 0);
16472   switch_to_section (debug_abbrev_section);
16473   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16474   switch_to_section (debug_info_section);
16475   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16476   switch_to_section (debug_line_section);
16477   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16478
16479   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16480     {
16481       switch_to_section (debug_macinfo_section);
16482       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16483                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16484       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16485     }
16486
16487   switch_to_section (text_section);
16488   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16489   if (flag_reorder_blocks_and_partition)
16490     {
16491       cold_text_section = unlikely_text_section ();
16492       switch_to_section (cold_text_section);
16493       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16494     }
16495 }
16496
16497 /* A helper function for dwarf2out_finish called through
16498    ht_forall.  Emit one queued .debug_str string.  */
16499
16500 static int
16501 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16502 {
16503   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16504
16505   if (node->form == DW_FORM_strp)
16506     {
16507       switch_to_section (debug_str_section);
16508       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16509       assemble_string (node->str, strlen (node->str) + 1);
16510     }
16511
16512   return 1;
16513 }
16514
16515 #if ENABLE_ASSERT_CHECKING
16516 /* Verify that all marks are clear.  */
16517
16518 static void
16519 verify_marks_clear (dw_die_ref die)
16520 {
16521   dw_die_ref c;
16522
16523   gcc_assert (! die->die_mark);
16524   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16525 }
16526 #endif /* ENABLE_ASSERT_CHECKING */
16527
16528 /* Clear the marks for a die and its children.
16529    Be cool if the mark isn't set.  */
16530
16531 static void
16532 prune_unmark_dies (dw_die_ref die)
16533 {
16534   dw_die_ref c;
16535
16536   if (die->die_mark)
16537     die->die_mark = 0;
16538   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16539 }
16540
16541 /* Given DIE that we're marking as used, find any other dies
16542    it references as attributes and mark them as used.  */
16543
16544 static void
16545 prune_unused_types_walk_attribs (dw_die_ref die)
16546 {
16547   dw_attr_ref a;
16548   unsigned ix;
16549
16550   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16551     {
16552       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16553         {
16554           /* A reference to another DIE.
16555              Make sure that it will get emitted.  */
16556           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16557         }
16558       /* Set the string's refcount to 0 so that prune_unused_types_mark
16559          accounts properly for it.  */
16560       if (AT_class (a) == dw_val_class_str)
16561         a->dw_attr_val.v.val_str->refcount = 0;
16562     }
16563 }
16564
16565
16566 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16567    to DIE's children.  */
16568
16569 static void
16570 prune_unused_types_mark (dw_die_ref die, int dokids)
16571 {
16572   dw_die_ref c;
16573
16574   if (die->die_mark == 0)
16575     {
16576       /* We haven't done this node yet.  Mark it as used.  */
16577       die->die_mark = 1;
16578
16579       /* We also have to mark its parents as used.
16580          (But we don't want to mark our parents' kids due to this.)  */
16581       if (die->die_parent)
16582         prune_unused_types_mark (die->die_parent, 0);
16583
16584       /* Mark any referenced nodes.  */
16585       prune_unused_types_walk_attribs (die);
16586
16587       /* If this node is a specification,
16588          also mark the definition, if it exists.  */
16589       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16590         prune_unused_types_mark (die->die_definition, 1);
16591     }
16592
16593   if (dokids && die->die_mark != 2)
16594     {
16595       /* We need to walk the children, but haven't done so yet.
16596          Remember that we've walked the kids.  */
16597       die->die_mark = 2;
16598
16599       /* If this is an array type, we need to make sure our
16600          kids get marked, even if they're types.  */
16601       if (die->die_tag == DW_TAG_array_type)
16602         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16603       else
16604         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16605     }
16606 }
16607
16608 /* For local classes, look if any static member functions were emitted
16609    and if so, mark them.  */
16610
16611 static void
16612 prune_unused_types_walk_local_classes (dw_die_ref die)
16613 {
16614   dw_die_ref c;
16615
16616   if (die->die_mark == 2)
16617     return;
16618
16619   switch (die->die_tag)
16620     {
16621     case DW_TAG_structure_type:
16622     case DW_TAG_union_type:
16623     case DW_TAG_class_type:
16624       break;
16625
16626     case DW_TAG_subprogram:
16627       if (!get_AT_flag (die, DW_AT_declaration)
16628           || die->die_definition != NULL)
16629         prune_unused_types_mark (die, 1);
16630       return;
16631
16632     default:
16633       return;
16634     }
16635
16636   /* Mark children.  */
16637   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16638 }
16639
16640 /* Walk the tree DIE and mark types that we actually use.  */
16641
16642 static void
16643 prune_unused_types_walk (dw_die_ref die)
16644 {
16645   dw_die_ref c;
16646
16647   /* Don't do anything if this node is already marked and
16648      children have been marked as well.  */
16649   if (die->die_mark == 2)
16650     return;
16651
16652   switch (die->die_tag)
16653     {
16654     case DW_TAG_structure_type:
16655     case DW_TAG_union_type:
16656     case DW_TAG_class_type:
16657       if (die->die_perennial_p)
16658         break;
16659
16660       for (c = die->die_parent; c; c = c->die_parent)
16661         if (c->die_tag == DW_TAG_subprogram)
16662           break;
16663
16664       /* Finding used static member functions inside of classes
16665          is needed just for local classes, because for other classes
16666          static member function DIEs with DW_AT_specification
16667          are emitted outside of the DW_TAG_*_type.  If we ever change
16668          it, we'd need to call this even for non-local classes.  */
16669       if (c)
16670         prune_unused_types_walk_local_classes (die);
16671
16672       /* It's a type node --- don't mark it.  */
16673       return;
16674
16675     case DW_TAG_const_type:
16676     case DW_TAG_packed_type:
16677     case DW_TAG_pointer_type:
16678     case DW_TAG_reference_type:
16679     case DW_TAG_volatile_type:
16680     case DW_TAG_typedef:
16681     case DW_TAG_array_type:
16682     case DW_TAG_interface_type:
16683     case DW_TAG_friend:
16684     case DW_TAG_variant_part:
16685     case DW_TAG_enumeration_type:
16686     case DW_TAG_subroutine_type:
16687     case DW_TAG_string_type:
16688     case DW_TAG_set_type:
16689     case DW_TAG_subrange_type:
16690     case DW_TAG_ptr_to_member_type:
16691     case DW_TAG_file_type:
16692       if (die->die_perennial_p)
16693         break;
16694
16695       /* It's a type node --- don't mark it.  */
16696       return;
16697
16698     default:
16699       /* Mark everything else.  */
16700       break;
16701   }
16702
16703   if (die->die_mark == 0)
16704     {
16705       die->die_mark = 1;
16706
16707       /* Now, mark any dies referenced from here.  */
16708       prune_unused_types_walk_attribs (die);
16709     }
16710
16711   die->die_mark = 2;
16712
16713   /* Mark children.  */
16714   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16715 }
16716
16717 /* Increment the string counts on strings referred to from DIE's
16718    attributes.  */
16719
16720 static void
16721 prune_unused_types_update_strings (dw_die_ref die)
16722 {
16723   dw_attr_ref a;
16724   unsigned ix;
16725
16726   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16727     if (AT_class (a) == dw_val_class_str)
16728       {
16729         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16730         s->refcount++;
16731         /* Avoid unnecessarily putting strings that are used less than
16732            twice in the hash table.  */
16733         if (s->refcount
16734             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16735           {
16736             void ** slot;
16737             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16738                                              htab_hash_string (s->str),
16739                                              INSERT);
16740             gcc_assert (*slot == NULL);
16741             *slot = s;
16742           }
16743       }
16744 }
16745
16746 /* Remove from the tree DIE any dies that aren't marked.  */
16747
16748 static void
16749 prune_unused_types_prune (dw_die_ref die)
16750 {
16751   dw_die_ref c;
16752
16753   gcc_assert (die->die_mark);
16754   prune_unused_types_update_strings (die);
16755
16756   if (! die->die_child)
16757     return;
16758
16759   c = die->die_child;
16760   do {
16761     dw_die_ref prev = c;
16762     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16763       if (c == die->die_child)
16764         {
16765           /* No marked children between 'prev' and the end of the list.  */
16766           if (prev == c)
16767             /* No marked children at all.  */
16768             die->die_child = NULL;
16769           else
16770             {
16771               prev->die_sib = c->die_sib;
16772               die->die_child = prev;
16773             }
16774           return;
16775         }
16776
16777     if (c != prev->die_sib)
16778       prev->die_sib = c;
16779     prune_unused_types_prune (c);
16780   } while (c != die->die_child);
16781 }
16782
16783
16784 /* Remove dies representing declarations that we never use.  */
16785
16786 static void
16787 prune_unused_types (void)
16788 {
16789   unsigned int i;
16790   limbo_die_node *node;
16791   pubname_ref pub;
16792
16793 #if ENABLE_ASSERT_CHECKING
16794   /* All the marks should already be clear.  */
16795   verify_marks_clear (comp_unit_die);
16796   for (node = limbo_die_list; node; node = node->next)
16797     verify_marks_clear (node->die);
16798 #endif /* ENABLE_ASSERT_CHECKING */
16799
16800   /* Set the mark on nodes that are actually used.  */
16801   prune_unused_types_walk (comp_unit_die);
16802   for (node = limbo_die_list; node; node = node->next)
16803     prune_unused_types_walk (node->die);
16804
16805   /* Also set the mark on nodes referenced from the
16806      pubname_table or arange_table.  */
16807   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16808     prune_unused_types_mark (pub->die, 1);
16809   for (i = 0; i < arange_table_in_use; i++)
16810     prune_unused_types_mark (arange_table[i], 1);
16811
16812   /* Get rid of nodes that aren't marked; and update the string counts.  */
16813   if (debug_str_hash)
16814     htab_empty (debug_str_hash);
16815   prune_unused_types_prune (comp_unit_die);
16816   for (node = limbo_die_list; node; node = node->next)
16817     prune_unused_types_prune (node->die);
16818
16819   /* Leave the marks clear.  */
16820   prune_unmark_dies (comp_unit_die);
16821   for (node = limbo_die_list; node; node = node->next)
16822     prune_unmark_dies (node->die);
16823 }
16824
16825 /* Set the parameter to true if there are any relative pathnames in
16826    the file table.  */
16827 static int
16828 file_table_relative_p (void ** slot, void *param)
16829 {
16830   bool *p = (bool *) param;
16831   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16832   if (!IS_ABSOLUTE_PATH (d->filename))
16833     {
16834       *p = true;
16835       return 0;
16836     }
16837   return 1;
16838 }
16839
16840 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
16841    to the location it would have been added, should we know its
16842    DECL_ASSEMBLER_NAME when we added other attributes.  This will
16843    probably improve compactness of debug info, removing equivalent
16844    abbrevs, and hide any differences caused by deferring the
16845    computation of the assembler name, triggered by e.g. PCH.  */
16846
16847 static inline void
16848 move_linkage_attr (dw_die_ref die)
16849 {
16850   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
16851   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
16852
16853   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
16854
16855   while (--ix > 0)
16856     {
16857       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
16858
16859       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
16860         break;
16861     }
16862
16863   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
16864     {
16865       VEC_pop (dw_attr_node, die->die_attr);
16866       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
16867     }
16868 }
16869
16870 /* Output stuff that dwarf requires at the end of every file,
16871    and generate the DWARF-2 debugging info.  */
16872
16873 static void
16874 dwarf2out_finish (const char *filename)
16875 {
16876   limbo_die_node *node, *next_node;
16877   dw_die_ref die = 0;
16878   unsigned int i;
16879
16880   /* Add the name for the main input file now.  We delayed this from
16881      dwarf2out_init to avoid complications with PCH.  */
16882   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16883   if (!IS_ABSOLUTE_PATH (filename))
16884     add_comp_dir_attribute (comp_unit_die);
16885   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16886     {
16887       bool p = false;
16888       htab_traverse (file_table, file_table_relative_p, &p);
16889       if (p)
16890         add_comp_dir_attribute (comp_unit_die);
16891     }
16892
16893   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16894     {
16895       add_location_or_const_value_attribute (
16896         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16897         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16898         DW_AT_location);
16899     }
16900
16901   /* Traverse the limbo die list, and add parent/child links.  The only
16902      dies without parents that should be here are concrete instances of
16903      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16904      For concrete instances, we can get the parent die from the abstract
16905      instance.  */
16906   for (node = limbo_die_list; node; node = next_node)
16907     {
16908       next_node = node->next;
16909       die = node->die;
16910
16911       if (die->die_parent == NULL)
16912         {
16913           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16914
16915           if (origin)
16916             add_child_die (origin->die_parent, die);
16917           else if (die == comp_unit_die)
16918             ;
16919           else if (errorcount > 0 || sorrycount > 0)
16920             /* It's OK to be confused by errors in the input.  */
16921             add_child_die (comp_unit_die, die);
16922           else
16923             {
16924               /* In certain situations, the lexical block containing a
16925                  nested function can be optimized away, which results
16926                  in the nested function die being orphaned.  Likewise
16927                  with the return type of that nested function.  Force
16928                  this to be a child of the containing function.
16929
16930                  It may happen that even the containing function got fully
16931                  inlined and optimized out.  In that case we are lost and
16932                  assign the empty child.  This should not be big issue as
16933                  the function is likely unreachable too.  */
16934               tree context = NULL_TREE;
16935
16936               gcc_assert (node->created_for);
16937
16938               if (DECL_P (node->created_for))
16939                 context = DECL_CONTEXT (node->created_for);
16940               else if (TYPE_P (node->created_for))
16941                 context = TYPE_CONTEXT (node->created_for);
16942
16943               gcc_assert (context
16944                           && (TREE_CODE (context) == FUNCTION_DECL
16945                               || TREE_CODE (context) == NAMESPACE_DECL));
16946
16947               origin = lookup_decl_die (context);
16948               if (origin)
16949                 add_child_die (origin, die);
16950               else
16951                 add_child_die (comp_unit_die, die);
16952             }
16953         }
16954     }
16955
16956   limbo_die_list = NULL;
16957
16958   for (node = deferred_asm_name; node; node = node->next)
16959     {
16960       tree decl = node->created_for;
16961       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16962         {
16963           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
16964                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
16965           move_linkage_attr (node->die);
16966         }
16967     }
16968
16969   deferred_asm_name = NULL;
16970
16971   /* Walk through the list of incomplete types again, trying once more to
16972      emit full debugging info for them.  */
16973   retry_incomplete_types ();
16974
16975   if (flag_eliminate_unused_debug_types)
16976     prune_unused_types ();
16977
16978   /* Generate separate CUs for each of the include files we've seen.
16979      They will go into limbo_die_list.  */
16980   if (flag_eliminate_dwarf2_dups)
16981     break_out_includes (comp_unit_die);
16982
16983   /* Traverse the DIE's and add add sibling attributes to those DIE's
16984      that have children.  */
16985   add_sibling_attributes (comp_unit_die);
16986   for (node = limbo_die_list; node; node = node->next)
16987     add_sibling_attributes (node->die);
16988
16989   /* Output a terminator label for the .text section.  */
16990   switch_to_section (text_section);
16991   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16992   if (flag_reorder_blocks_and_partition)
16993     {
16994       switch_to_section (unlikely_text_section ());
16995       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16996     }
16997
16998   /* We can only use the low/high_pc attributes if all of the code was
16999      in .text.  */
17000   if (!have_multiple_function_sections)
17001     {
17002       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
17003       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
17004     }
17005
17006   else
17007     {
17008       unsigned fde_idx = 0;
17009
17010       /* We need to give .debug_loc and .debug_ranges an appropriate
17011          "base address".  Use zero so that these addresses become
17012          absolute.  Historically, we've emitted the unexpected
17013          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
17014          Emit both to give time for other tools to adapt.  */
17015       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
17016       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
17017
17018       add_AT_range_list (comp_unit_die, DW_AT_ranges,
17019                          add_ranges_by_labels (text_section_label,
17020                                                text_end_label));
17021       if (flag_reorder_blocks_and_partition)
17022         add_ranges_by_labels (cold_text_section_label,
17023                               cold_end_label);
17024
17025       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
17026         {
17027           dw_fde_ref fde = &fde_table[fde_idx];
17028
17029           if (fde->dw_fde_switched_sections)
17030             {
17031               add_ranges_by_labels (fde->dw_fde_hot_section_label,
17032                                     fde->dw_fde_hot_section_end_label);
17033               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
17034                                     fde->dw_fde_unlikely_section_end_label);
17035             }
17036           else
17037             add_ranges_by_labels (fde->dw_fde_begin,
17038                                   fde->dw_fde_end);
17039         }
17040
17041       add_ranges (NULL);
17042     }
17043
17044   /* Output location list section if necessary.  */
17045   if (have_location_lists)
17046     {
17047       /* Output the location lists info.  */
17048       switch_to_section (debug_loc_section);
17049       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
17050                                    DEBUG_LOC_SECTION_LABEL, 0);
17051       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
17052       output_location_lists (die);
17053     }
17054
17055   if (debug_info_level >= DINFO_LEVEL_NORMAL)
17056     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
17057                     debug_line_section_label);
17058
17059   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17060     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
17061
17062   /* Output all of the compilation units.  We put the main one last so that
17063      the offsets are available to output_pubnames.  */
17064   for (node = limbo_die_list; node; node = node->next)
17065     output_comp_unit (node->die, 0);
17066
17067   /* Output the main compilation unit if non-empty or if .debug_macinfo
17068      has been emitted.  */
17069   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
17070
17071   /* Output the abbreviation table.  */
17072   switch_to_section (debug_abbrev_section);
17073   output_abbrev_section ();
17074
17075   /* Output public names table if necessary.  */
17076   if (!VEC_empty (pubname_entry, pubname_table))
17077     {
17078       switch_to_section (debug_pubnames_section);
17079       output_pubnames (pubname_table);
17080     }
17081
17082 #ifdef DEBUG_PUBTYPES_SECTION
17083   /* Output public types table if necessary.  */
17084   if (!VEC_empty (pubname_entry, pubtype_table))
17085     {
17086       switch_to_section (debug_pubtypes_section);
17087       output_pubnames (pubtype_table);
17088     }
17089 #endif
17090
17091   /* Output the address range information.  We only put functions in the arange
17092      table, so don't write it out if we don't have any.  */
17093   if (fde_table_in_use)
17094     {
17095       switch_to_section (debug_aranges_section);
17096       output_aranges ();
17097     }
17098
17099   /* Output ranges section if necessary.  */
17100   if (ranges_table_in_use)
17101     {
17102       switch_to_section (debug_ranges_section);
17103       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
17104       output_ranges ();
17105     }
17106
17107   /* Output the source line correspondence table.  We must do this
17108      even if there is no line information.  Otherwise, on an empty
17109      translation unit, we will generate a present, but empty,
17110      .debug_info section.  IRIX 6.5 `nm' will then complain when
17111      examining the file.  This is done late so that any filenames
17112      used by the debug_info section are marked as 'used'.  */
17113   if (! DWARF2_ASM_LINE_DEBUG_INFO)
17114     {
17115       switch_to_section (debug_line_section);
17116       output_line_info ();
17117     }
17118
17119   /* Have to end the macro section.  */
17120   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17121     {
17122       switch_to_section (debug_macinfo_section);
17123       dw2_asm_output_data (1, 0, "End compilation unit");
17124     }
17125
17126   /* If we emitted any DW_FORM_strp form attribute, output the string
17127      table too.  */
17128   if (debug_str_hash)
17129     htab_traverse (debug_str_hash, output_indirect_string, NULL);
17130 }
17131 #else
17132
17133 /* This should never be used, but its address is needed for comparisons.  */
17134 const struct gcc_debug_hooks dwarf2_debug_hooks;
17135
17136 #endif /* DWARF2_DEBUGGING_INFO */
17137
17138 #include "gt-dwarf2out.h"