OSDN Git Service

* Makefile.def: Add MPC support and dependencies.
[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 *);
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.  */
697
698 char *
699 dwarf2out_cfi_label (void)
700 {
701   static char label[20];
702
703   if (dwarf2out_do_cfi_asm ())
704     {
705       /* In this case, we will be emitting the asm directive instead of
706          the label, so just return a placeholder to keep the rest of the
707          interfaces happy.  */
708       strcpy (label, "<do not output>");
709     }
710   else
711     {
712       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
713       ASM_OUTPUT_LABEL (asm_out_file, label);
714     }
715
716   return label;
717 }
718
719 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
720    or to the CIE if LABEL is NULL.  */
721
722 static void
723 add_fde_cfi (const char *label, dw_cfi_ref cfi)
724 {
725   dw_cfi_ref *list_head = &cie_cfi_head;
726
727   if (dwarf2out_do_cfi_asm ())
728     {
729       if (label)
730         {
731           output_cfi_directive (cfi);
732
733           /* We still have to add the cfi to the list so that
734              lookup_cfa works later on.  */
735           list_head = &current_fde ()->dw_fde_cfi;
736         }
737       /* ??? If this is a CFI for the CIE, we don't emit.  This
738          assumes that the standard CIE contents that the assembler
739          uses matches the standard CIE contents that the compiler
740          uses.  This is probably a bad assumption.  I'm not quite
741          sure how to address this for now.  */
742     }
743   else if (label)
744     {
745       dw_fde_ref fde = current_fde ();
746
747       gcc_assert (fde != NULL);
748
749       if (*label == 0)
750         label = dwarf2out_cfi_label ();
751
752       if (fde->dw_fde_current_label == NULL
753           || strcmp (label, fde->dw_fde_current_label) != 0)
754         {
755           dw_cfi_ref xcfi;
756
757           label = xstrdup (label);
758
759           /* Set the location counter to the new label.  */
760           xcfi = new_cfi ();
761           /* If we have a current label, advance from there, otherwise
762              set the location directly using set_loc.  */
763           xcfi->dw_cfi_opc = fde->dw_fde_current_label
764                              ? DW_CFA_advance_loc4
765                              : DW_CFA_set_loc;
766           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
767           add_cfi (&fde->dw_fde_cfi, xcfi);
768
769           fde->dw_fde_current_label = label;
770         }
771
772       list_head = &fde->dw_fde_cfi;
773     }
774
775   add_cfi (list_head, cfi);
776 }
777
778 /* Subroutine of lookup_cfa.  */
779
780 static void
781 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
782 {
783   switch (cfi->dw_cfi_opc)
784     {
785     case DW_CFA_def_cfa_offset:
786     case DW_CFA_def_cfa_offset_sf:
787       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
788       break;
789     case DW_CFA_def_cfa_register:
790       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
791       break;
792     case DW_CFA_def_cfa:
793     case DW_CFA_def_cfa_sf:
794       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
795       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
796       break;
797     case DW_CFA_def_cfa_expression:
798       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
799       break;
800
801     case DW_CFA_remember_state:
802       gcc_assert (!remember->in_use);
803       *remember = *loc;
804       remember->in_use = 1;
805       break;
806     case DW_CFA_restore_state:
807       gcc_assert (remember->in_use);
808       *loc = *remember;
809       remember->in_use = 0;
810       break;
811
812     default:
813       break;
814     }
815 }
816
817 /* Find the previous value for the CFA.  */
818
819 static void
820 lookup_cfa (dw_cfa_location *loc)
821 {
822   dw_cfi_ref cfi;
823   dw_fde_ref fde;
824   dw_cfa_location remember;
825
826   memset (loc, 0, sizeof (*loc));
827   loc->reg = INVALID_REGNUM;
828   remember = *loc;
829
830   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
831     lookup_cfa_1 (cfi, loc, &remember);
832
833   fde = current_fde ();
834   if (fde)
835     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
836       lookup_cfa_1 (cfi, loc, &remember);
837 }
838
839 /* The current rule for calculating the DWARF2 canonical frame address.  */
840 static dw_cfa_location cfa;
841
842 /* The register used for saving registers to the stack, and its offset
843    from the CFA.  */
844 static dw_cfa_location cfa_store;
845
846 /* The current save location around an epilogue.  */
847 static dw_cfa_location cfa_remember;
848
849 /* The running total of the size of arguments pushed onto the stack.  */
850 static HOST_WIDE_INT args_size;
851
852 /* The last args_size we actually output.  */
853 static HOST_WIDE_INT old_args_size;
854
855 /* Entry point to update the canonical frame address (CFA).
856    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
857    calculated from REG+OFFSET.  */
858
859 void
860 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
861 {
862   dw_cfa_location loc;
863   loc.indirect = 0;
864   loc.base_offset = 0;
865   loc.reg = reg;
866   loc.offset = offset;
867   def_cfa_1 (label, &loc);
868 }
869
870 /* Determine if two dw_cfa_location structures define the same data.  */
871
872 static bool
873 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
874 {
875   return (loc1->reg == loc2->reg
876           && loc1->offset == loc2->offset
877           && loc1->indirect == loc2->indirect
878           && (loc1->indirect == 0
879               || loc1->base_offset == loc2->base_offset));
880 }
881
882 /* This routine does the actual work.  The CFA is now calculated from
883    the dw_cfa_location structure.  */
884
885 static void
886 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
887 {
888   dw_cfi_ref cfi;
889   dw_cfa_location old_cfa, loc;
890
891   cfa = *loc_p;
892   loc = *loc_p;
893
894   if (cfa_store.reg == loc.reg && loc.indirect == 0)
895     cfa_store.offset = loc.offset;
896
897   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
898   lookup_cfa (&old_cfa);
899
900   /* If nothing changed, no need to issue any call frame instructions.  */
901   if (cfa_equal_p (&loc, &old_cfa))
902     return;
903
904   cfi = new_cfi ();
905
906   if (loc.reg == old_cfa.reg && !loc.indirect)
907     {
908       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
909          the CFA register did not change but the offset did.  The data 
910          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
911          in the assembler via the .cfi_def_cfa_offset directive.  */
912       if (loc.offset < 0)
913         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
914       else
915         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
916       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
917     }
918
919 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
920   else if (loc.offset == old_cfa.offset
921            && old_cfa.reg != INVALID_REGNUM
922            && !loc.indirect)
923     {
924       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
925          indicating the CFA register has changed to <register> but the
926          offset has not changed.  */
927       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
928       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
929     }
930 #endif
931
932   else if (loc.indirect == 0)
933     {
934       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
935          indicating the CFA register has changed to <register> with
936          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
937          happens in output_cfi, or in the assembler via the .cfi_def_cfa
938          directive.  */
939       if (loc.offset < 0)
940         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
941       else
942         cfi->dw_cfi_opc = DW_CFA_def_cfa;
943       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
944       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
945     }
946   else
947     {
948       /* Construct a DW_CFA_def_cfa_expression instruction to
949          calculate the CFA using a full location expression since no
950          register-offset pair is available.  */
951       struct dw_loc_descr_struct *loc_list;
952
953       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
954       loc_list = build_cfa_loc (&loc, 0);
955       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
956     }
957
958   add_fde_cfi (label, cfi);
959 }
960
961 /* Add the CFI for saving a register.  REG is the CFA column number.
962    LABEL is passed to add_fde_cfi.
963    If SREG is -1, the register is saved at OFFSET from the CFA;
964    otherwise it is saved in SREG.  */
965
966 static void
967 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
968 {
969   dw_cfi_ref cfi = new_cfi ();
970   dw_fde_ref fde = current_fde ();
971
972   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
973
974   /* When stack is aligned, store REG using DW_CFA_expression with
975      FP.  */
976   if (fde
977       && fde->stack_realign
978       && sreg == INVALID_REGNUM)
979     {
980       cfi->dw_cfi_opc = DW_CFA_expression;
981       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
982       cfi->dw_cfi_oprnd1.dw_cfi_loc
983         = build_cfa_aligned_loc (offset, fde->stack_realignment);
984     }
985   else if (sreg == INVALID_REGNUM)
986     {
987       if (offset < 0)
988         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
989       else if (reg & ~0x3f)
990         cfi->dw_cfi_opc = DW_CFA_offset_extended;
991       else
992         cfi->dw_cfi_opc = DW_CFA_offset;
993       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
994     }
995   else if (sreg == reg)
996     cfi->dw_cfi_opc = DW_CFA_same_value;
997   else
998     {
999       cfi->dw_cfi_opc = DW_CFA_register;
1000       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1001     }
1002
1003   add_fde_cfi (label, cfi);
1004 }
1005
1006 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1007    This CFI tells the unwinder that it needs to restore the window registers
1008    from the previous frame's window save area.
1009
1010    ??? Perhaps we should note in the CIE where windows are saved (instead of
1011    assuming 0(cfa)) and what registers are in the window.  */
1012
1013 void
1014 dwarf2out_window_save (const char *label)
1015 {
1016   dw_cfi_ref cfi = new_cfi ();
1017
1018   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1019   add_fde_cfi (label, cfi);
1020 }
1021
1022 /* Add a CFI to update the running total of the size of arguments
1023    pushed onto the stack.  */
1024
1025 void
1026 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1027 {
1028   dw_cfi_ref cfi;
1029
1030   if (size == old_args_size)
1031     return;
1032
1033   old_args_size = size;
1034
1035   cfi = new_cfi ();
1036   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1037   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1038   add_fde_cfi (label, cfi);
1039 }
1040
1041 /* Entry point for saving a register to the stack.  REG is the GCC register
1042    number.  LABEL and OFFSET are passed to reg_save.  */
1043
1044 void
1045 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1046 {
1047   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1048 }
1049
1050 /* Entry point for saving the return address in the stack.
1051    LABEL and OFFSET are passed to reg_save.  */
1052
1053 void
1054 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1055 {
1056   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1057 }
1058
1059 /* Entry point for saving the return address in a register.
1060    LABEL and SREG are passed to reg_save.  */
1061
1062 void
1063 dwarf2out_return_reg (const char *label, unsigned int sreg)
1064 {
1065   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1066 }
1067
1068 #ifdef DWARF2_UNWIND_INFO
1069 /* Record the initial position of the return address.  RTL is
1070    INCOMING_RETURN_ADDR_RTX.  */
1071
1072 static void
1073 initial_return_save (rtx rtl)
1074 {
1075   unsigned int reg = INVALID_REGNUM;
1076   HOST_WIDE_INT offset = 0;
1077
1078   switch (GET_CODE (rtl))
1079     {
1080     case REG:
1081       /* RA is in a register.  */
1082       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1083       break;
1084
1085     case MEM:
1086       /* RA is on the stack.  */
1087       rtl = XEXP (rtl, 0);
1088       switch (GET_CODE (rtl))
1089         {
1090         case REG:
1091           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1092           offset = 0;
1093           break;
1094
1095         case PLUS:
1096           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1097           offset = INTVAL (XEXP (rtl, 1));
1098           break;
1099
1100         case MINUS:
1101           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1102           offset = -INTVAL (XEXP (rtl, 1));
1103           break;
1104
1105         default:
1106           gcc_unreachable ();
1107         }
1108
1109       break;
1110
1111     case PLUS:
1112       /* The return address is at some offset from any value we can
1113          actually load.  For instance, on the SPARC it is in %i7+8. Just
1114          ignore the offset for now; it doesn't matter for unwinding frames.  */
1115       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1116       initial_return_save (XEXP (rtl, 0));
1117       return;
1118
1119     default:
1120       gcc_unreachable ();
1121     }
1122
1123   if (reg != DWARF_FRAME_RETURN_COLUMN)
1124     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1125 }
1126 #endif
1127
1128 /* Given a SET, calculate the amount of stack adjustment it
1129    contains.  */
1130
1131 static HOST_WIDE_INT
1132 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1133                      HOST_WIDE_INT cur_offset)
1134 {
1135   const_rtx src = SET_SRC (pattern);
1136   const_rtx dest = SET_DEST (pattern);
1137   HOST_WIDE_INT offset = 0;
1138   enum rtx_code code;
1139
1140   if (dest == stack_pointer_rtx)
1141     {
1142       code = GET_CODE (src);
1143
1144       /* Assume (set (reg sp) (reg whatever)) sets args_size
1145          level to 0.  */
1146       if (code == REG && src != stack_pointer_rtx)
1147         {
1148           offset = -cur_args_size;
1149 #ifndef STACK_GROWS_DOWNWARD
1150           offset = -offset;
1151 #endif
1152           return offset - cur_offset;
1153         }
1154
1155       if (! (code == PLUS || code == MINUS)
1156           || XEXP (src, 0) != stack_pointer_rtx
1157           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1158         return 0;
1159
1160       /* (set (reg sp) (plus (reg sp) (const_int))) */
1161       offset = INTVAL (XEXP (src, 1));
1162       if (code == PLUS)
1163         offset = -offset;
1164       return offset;
1165     }
1166
1167   if (MEM_P (src) && !MEM_P (dest))
1168     dest = src;
1169   if (MEM_P (dest))
1170     {
1171       /* (set (mem (pre_dec (reg sp))) (foo)) */
1172       src = XEXP (dest, 0);
1173       code = GET_CODE (src);
1174
1175       switch (code)
1176         {
1177         case PRE_MODIFY:
1178         case POST_MODIFY:
1179           if (XEXP (src, 0) == stack_pointer_rtx)
1180             {
1181               rtx val = XEXP (XEXP (src, 1), 1);
1182               /* We handle only adjustments by constant amount.  */
1183               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1184                           && GET_CODE (val) == CONST_INT);
1185               offset = -INTVAL (val);
1186               break;
1187             }
1188           return 0;
1189
1190         case PRE_DEC:
1191         case POST_DEC:
1192           if (XEXP (src, 0) == stack_pointer_rtx)
1193             {
1194               offset = GET_MODE_SIZE (GET_MODE (dest));
1195               break;
1196             }
1197           return 0;
1198
1199         case PRE_INC:
1200         case POST_INC:
1201           if (XEXP (src, 0) == stack_pointer_rtx)
1202             {
1203               offset = -GET_MODE_SIZE (GET_MODE (dest));
1204               break;
1205             }
1206           return 0;
1207
1208         default:
1209           return 0;
1210         }
1211     }
1212   else
1213     return 0;
1214
1215   return offset;
1216 }
1217
1218 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1219    indexed by INSN_UID.  */
1220
1221 static HOST_WIDE_INT *barrier_args_size;
1222
1223 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1224
1225 static HOST_WIDE_INT
1226 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1227                              VEC (rtx, heap) **next)
1228 {
1229   HOST_WIDE_INT offset = 0;
1230   int i;
1231
1232   if (! RTX_FRAME_RELATED_P (insn))
1233     {
1234       if (prologue_epilogue_contains (insn))
1235         /* Nothing */;
1236       else if (GET_CODE (PATTERN (insn)) == SET)
1237         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1238       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1239                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1240         {
1241           /* There may be stack adjustments inside compound insns.  Search
1242              for them.  */
1243           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1244             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1245               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1246                                              cur_args_size, offset);
1247         }
1248     }
1249   else
1250     {
1251       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1252
1253       if (expr)
1254         {
1255           expr = XEXP (expr, 0);
1256           if (GET_CODE (expr) == PARALLEL
1257               || GET_CODE (expr) == SEQUENCE)
1258             for (i = 1; i < XVECLEN (expr, 0); i++)
1259               {
1260                 rtx elem = XVECEXP (expr, 0, i);
1261
1262                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1263                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1264               }
1265         }
1266     }
1267
1268 #ifndef STACK_GROWS_DOWNWARD
1269   offset = -offset;
1270 #endif
1271
1272   cur_args_size += offset;
1273   if (cur_args_size < 0)
1274     cur_args_size = 0;
1275
1276   if (JUMP_P (insn))
1277     {
1278       rtx dest = JUMP_LABEL (insn);
1279
1280       if (dest)
1281         {
1282           if (barrier_args_size [INSN_UID (dest)] < 0)
1283             {
1284               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1285               VEC_safe_push (rtx, heap, *next, dest);
1286             }
1287         }
1288     }
1289
1290   return cur_args_size;
1291 }
1292
1293 /* Walk the whole function and compute args_size on BARRIERs.  */
1294
1295 static void
1296 compute_barrier_args_size (void)
1297 {
1298   int max_uid = get_max_uid (), i;
1299   rtx insn;
1300   VEC (rtx, heap) *worklist, *next, *tmp;
1301
1302   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1303   for (i = 0; i < max_uid; i++)
1304     barrier_args_size[i] = -1;
1305
1306   worklist = VEC_alloc (rtx, heap, 20);
1307   next = VEC_alloc (rtx, heap, 20);
1308   insn = get_insns ();
1309   barrier_args_size[INSN_UID (insn)] = 0;
1310   VEC_quick_push (rtx, worklist, insn);
1311   for (;;)
1312     {
1313       while (!VEC_empty (rtx, worklist))
1314         {
1315           rtx prev, body, first_insn;
1316           HOST_WIDE_INT cur_args_size;
1317
1318           first_insn = insn = VEC_pop (rtx, worklist);
1319           cur_args_size = barrier_args_size[INSN_UID (insn)];
1320           prev = prev_nonnote_insn (insn);
1321           if (prev && BARRIER_P (prev))
1322             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1323
1324           for (; insn; insn = NEXT_INSN (insn))
1325             {
1326               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1327                 continue;
1328               if (BARRIER_P (insn))
1329                 break;
1330
1331               if (LABEL_P (insn))
1332                 {
1333                   if (insn == first_insn)
1334                     continue;
1335                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1336                     {
1337                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1338                       continue;
1339                     }
1340                   else
1341                     {
1342                       /* The insns starting with this label have been
1343                          already scanned or are in the worklist.  */
1344                       break;
1345                     }
1346                 }
1347
1348               body = PATTERN (insn);
1349               if (GET_CODE (body) == SEQUENCE)
1350                 {
1351                   HOST_WIDE_INT dest_args_size = cur_args_size;
1352                   for (i = 1; i < XVECLEN (body, 0); i++)
1353                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1354                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1355                       dest_args_size
1356                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1357                                                        dest_args_size, &next);
1358                     else
1359                       cur_args_size
1360                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1361                                                        cur_args_size, &next);
1362
1363                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1364                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1365                                                  dest_args_size, &next);
1366                   else
1367                     cur_args_size
1368                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1369                                                      cur_args_size, &next);
1370                 }
1371               else
1372                 cur_args_size
1373                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1374             }
1375         }
1376
1377       if (VEC_empty (rtx, next))
1378         break;
1379
1380       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1381       tmp = next;
1382       next = worklist;
1383       worklist = tmp;
1384       VEC_truncate (rtx, next, 0);
1385     }
1386
1387   VEC_free (rtx, heap, worklist);
1388   VEC_free (rtx, heap, next);
1389 }
1390
1391
1392 /* Check INSN to see if it looks like a push or a stack adjustment, and
1393    make a note of it if it does.  EH uses this information to find out how
1394    much extra space it needs to pop off the stack.  */
1395
1396 static void
1397 dwarf2out_stack_adjust (rtx insn, bool after_p)
1398 {
1399   HOST_WIDE_INT offset;
1400   const char *label;
1401   int i;
1402
1403   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1404      with this function.  Proper support would require all frame-related
1405      insns to be marked, and to be able to handle saving state around
1406      epilogues textually in the middle of the function.  */
1407   if (prologue_epilogue_contains (insn))
1408     return;
1409
1410   /* If INSN is an instruction from target of an annulled branch, the
1411      effects are for the target only and so current argument size
1412      shouldn't change at all.  */
1413   if (final_sequence
1414       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1415       && INSN_FROM_TARGET_P (insn))
1416     return;
1417
1418   /* If only calls can throw, and we have a frame pointer,
1419      save up adjustments until we see the CALL_INSN.  */
1420   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1421     {
1422       if (CALL_P (insn) && !after_p)
1423         {
1424           /* Extract the size of the args from the CALL rtx itself.  */
1425           insn = PATTERN (insn);
1426           if (GET_CODE (insn) == PARALLEL)
1427             insn = XVECEXP (insn, 0, 0);
1428           if (GET_CODE (insn) == SET)
1429             insn = SET_SRC (insn);
1430           gcc_assert (GET_CODE (insn) == CALL);
1431           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1432         }
1433       return;
1434     }
1435
1436   if (CALL_P (insn) && !after_p)
1437     {
1438       if (!flag_asynchronous_unwind_tables)
1439         dwarf2out_args_size ("", args_size);
1440       return;
1441     }
1442   else if (BARRIER_P (insn))
1443     {
1444       /* Don't call compute_barrier_args_size () if the only
1445          BARRIER is at the end of function.  */
1446       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1447         compute_barrier_args_size ();
1448       if (barrier_args_size == NULL)
1449         offset = 0;
1450       else
1451         {
1452           offset = barrier_args_size[INSN_UID (insn)];
1453           if (offset < 0)
1454             offset = 0;
1455         }
1456
1457       offset -= args_size;
1458 #ifndef STACK_GROWS_DOWNWARD
1459       offset = -offset;
1460 #endif
1461     }
1462   else if (GET_CODE (PATTERN (insn)) == SET)
1463     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1464   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1465            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1466     {
1467       /* There may be stack adjustments inside compound insns.  Search
1468          for them.  */
1469       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1470         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1471           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1472                                          args_size, offset);
1473     }
1474   else
1475     return;
1476
1477   if (offset == 0)
1478     return;
1479
1480   label = dwarf2out_cfi_label ();
1481   dwarf2out_args_size_adjust (offset, label);
1482 }
1483
1484 /* Adjust args_size based on stack adjustment OFFSET.  */
1485
1486 static void
1487 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1488 {
1489   if (cfa.reg == STACK_POINTER_REGNUM)
1490     cfa.offset += offset;
1491
1492   if (cfa_store.reg == STACK_POINTER_REGNUM)
1493     cfa_store.offset += offset;
1494
1495 #ifndef STACK_GROWS_DOWNWARD
1496   offset = -offset;
1497 #endif
1498
1499   args_size += offset;
1500   if (args_size < 0)
1501     args_size = 0;
1502
1503   def_cfa_1 (label, &cfa);
1504   if (flag_asynchronous_unwind_tables)
1505     dwarf2out_args_size (label, args_size);
1506 }
1507
1508 #endif
1509
1510 /* We delay emitting a register save until either (a) we reach the end
1511    of the prologue or (b) the register is clobbered.  This clusters
1512    register saves so that there are fewer pc advances.  */
1513
1514 struct GTY(()) queued_reg_save {
1515   struct queued_reg_save *next;
1516   rtx reg;
1517   HOST_WIDE_INT cfa_offset;
1518   rtx saved_reg;
1519 };
1520
1521 static GTY(()) struct queued_reg_save *queued_reg_saves;
1522
1523 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1524 struct GTY(()) reg_saved_in_data {
1525   rtx orig_reg;
1526   rtx saved_in_reg;
1527 };
1528
1529 /* A list of registers saved in other registers.
1530    The list intentionally has a small maximum capacity of 4; if your
1531    port needs more than that, you might consider implementing a
1532    more efficient data structure.  */
1533 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1534 static GTY(()) size_t num_regs_saved_in_regs;
1535
1536 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1537 static const char *last_reg_save_label;
1538
1539 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1540    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1541
1542 static void
1543 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1544 {
1545   struct queued_reg_save *q;
1546
1547   /* Duplicates waste space, but it's also necessary to remove them
1548      for correctness, since the queue gets output in reverse
1549      order.  */
1550   for (q = queued_reg_saves; q != NULL; q = q->next)
1551     if (REGNO (q->reg) == REGNO (reg))
1552       break;
1553
1554   if (q == NULL)
1555     {
1556       q = GGC_NEW (struct queued_reg_save);
1557       q->next = queued_reg_saves;
1558       queued_reg_saves = q;
1559     }
1560
1561   q->reg = reg;
1562   q->cfa_offset = offset;
1563   q->saved_reg = sreg;
1564
1565   last_reg_save_label = label;
1566 }
1567
1568 /* Output all the entries in QUEUED_REG_SAVES.  */
1569
1570 static void
1571 flush_queued_reg_saves (void)
1572 {
1573   struct queued_reg_save *q;
1574
1575   for (q = queued_reg_saves; q; q = q->next)
1576     {
1577       size_t i;
1578       unsigned int reg, sreg;
1579
1580       for (i = 0; i < num_regs_saved_in_regs; i++)
1581         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1582           break;
1583       if (q->saved_reg && i == num_regs_saved_in_regs)
1584         {
1585           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1586           num_regs_saved_in_regs++;
1587         }
1588       if (i != num_regs_saved_in_regs)
1589         {
1590           regs_saved_in_regs[i].orig_reg = q->reg;
1591           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1592         }
1593
1594       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1595       if (q->saved_reg)
1596         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1597       else
1598         sreg = INVALID_REGNUM;
1599       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1600     }
1601
1602   queued_reg_saves = NULL;
1603   last_reg_save_label = NULL;
1604 }
1605
1606 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1607    location for?  Or, does it clobber a register which we've previously
1608    said that some other register is saved in, and for which we now
1609    have a new location for?  */
1610
1611 static bool
1612 clobbers_queued_reg_save (const_rtx insn)
1613 {
1614   struct queued_reg_save *q;
1615
1616   for (q = queued_reg_saves; q; q = q->next)
1617     {
1618       size_t i;
1619       if (modified_in_p (q->reg, insn))
1620         return true;
1621       for (i = 0; i < num_regs_saved_in_regs; i++)
1622         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1623             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1624           return true;
1625     }
1626
1627   return false;
1628 }
1629
1630 /* Entry point for saving the first register into the second.  */
1631
1632 void
1633 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1634 {
1635   size_t i;
1636   unsigned int regno, sregno;
1637
1638   for (i = 0; i < num_regs_saved_in_regs; i++)
1639     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1640       break;
1641   if (i == num_regs_saved_in_regs)
1642     {
1643       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1644       num_regs_saved_in_regs++;
1645     }
1646   regs_saved_in_regs[i].orig_reg = reg;
1647   regs_saved_in_regs[i].saved_in_reg = sreg;
1648
1649   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1650   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1651   reg_save (label, regno, sregno, 0);
1652 }
1653
1654 /* What register, if any, is currently saved in REG?  */
1655
1656 static rtx
1657 reg_saved_in (rtx reg)
1658 {
1659   unsigned int regn = REGNO (reg);
1660   size_t i;
1661   struct queued_reg_save *q;
1662
1663   for (q = queued_reg_saves; q; q = q->next)
1664     if (q->saved_reg && regn == REGNO (q->saved_reg))
1665       return q->reg;
1666
1667   for (i = 0; i < num_regs_saved_in_regs; i++)
1668     if (regs_saved_in_regs[i].saved_in_reg
1669         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1670       return regs_saved_in_regs[i].orig_reg;
1671
1672   return NULL_RTX;
1673 }
1674
1675
1676 /* A temporary register holding an integral value used in adjusting SP
1677    or setting up the store_reg.  The "offset" field holds the integer
1678    value, not an offset.  */
1679 static dw_cfa_location cfa_temp;
1680
1681 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1682
1683 static void
1684 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1685 {
1686   memset (&cfa, 0, sizeof (cfa));
1687
1688   switch (GET_CODE (pat))
1689     {
1690     case PLUS:
1691       cfa.reg = REGNO (XEXP (pat, 0));
1692       cfa.offset = INTVAL (XEXP (pat, 1));
1693       break;
1694
1695     case REG:
1696       cfa.reg = REGNO (pat);
1697       break;
1698
1699     default:
1700       /* Recurse and define an expression.  */
1701       gcc_unreachable ();
1702     }
1703
1704   def_cfa_1 (label, &cfa);
1705 }
1706
1707 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1708
1709 static void
1710 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1711 {
1712   rtx src, dest;
1713
1714   gcc_assert (GET_CODE (pat) == SET);
1715   dest = XEXP (pat, 0);
1716   src = XEXP (pat, 1);
1717
1718   switch (GET_CODE (src))
1719     {
1720     case PLUS:
1721       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1722       cfa.offset -= INTVAL (XEXP (src, 1));
1723       break;
1724
1725     case REG:
1726         break;
1727
1728     default:
1729         gcc_unreachable ();
1730     }
1731
1732   cfa.reg = REGNO (dest);
1733   gcc_assert (cfa.indirect == 0);
1734
1735   def_cfa_1 (label, &cfa);
1736 }
1737
1738 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1739
1740 static void
1741 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1742 {
1743   HOST_WIDE_INT offset;
1744   rtx src, addr, span;
1745
1746   src = XEXP (set, 1);
1747   addr = XEXP (set, 0);
1748   gcc_assert (MEM_P (addr));
1749   addr = XEXP (addr, 0);
1750   
1751   /* As documented, only consider extremely simple addresses.  */
1752   switch (GET_CODE (addr))
1753     {
1754     case REG:
1755       gcc_assert (REGNO (addr) == cfa.reg);
1756       offset = -cfa.offset;
1757       break;
1758     case PLUS:
1759       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1760       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1761       break;
1762     default:
1763       gcc_unreachable ();
1764     }
1765
1766   span = targetm.dwarf_register_span (src);
1767
1768   /* ??? We'd like to use queue_reg_save, but we need to come up with
1769      a different flushing heuristic for epilogues.  */
1770   if (!span)
1771     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1772   else
1773     {
1774       /* We have a PARALLEL describing where the contents of SRC live.
1775          Queue register saves for each piece of the PARALLEL.  */
1776       int par_index;
1777       int limit;
1778       HOST_WIDE_INT span_offset = offset;
1779
1780       gcc_assert (GET_CODE (span) == PARALLEL);
1781
1782       limit = XVECLEN (span, 0);
1783       for (par_index = 0; par_index < limit; par_index++)
1784         {
1785           rtx elem = XVECEXP (span, 0, par_index);
1786
1787           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1788                     INVALID_REGNUM, span_offset);
1789           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1790         }
1791     }
1792 }
1793
1794 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1795
1796 static void
1797 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1798 {
1799   rtx src, dest;
1800   unsigned sregno, dregno;
1801
1802   src = XEXP (set, 1);
1803   dest = XEXP (set, 0);
1804
1805   if (src == pc_rtx)
1806     sregno = DWARF_FRAME_RETURN_COLUMN;
1807   else
1808     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1809
1810   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1811
1812   /* ??? We'd like to use queue_reg_save, but we need to come up with
1813      a different flushing heuristic for epilogues.  */
1814   reg_save (label, sregno, dregno, 0);
1815 }
1816
1817 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1818
1819 static void
1820 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1821 {
1822   dw_cfi_ref cfi = new_cfi ();
1823   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1824
1825   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1826   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1827
1828   add_fde_cfi (label, cfi);
1829 }
1830
1831 /* Record call frame debugging information for an expression EXPR,
1832    which either sets SP or FP (adjusting how we calculate the frame
1833    address) or saves a register to the stack or another register.
1834    LABEL indicates the address of EXPR.
1835
1836    This function encodes a state machine mapping rtxes to actions on
1837    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1838    users need not read the source code.
1839
1840   The High-Level Picture
1841
1842   Changes in the register we use to calculate the CFA: Currently we
1843   assume that if you copy the CFA register into another register, we
1844   should take the other one as the new CFA register; this seems to
1845   work pretty well.  If it's wrong for some target, it's simple
1846   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1847
1848   Changes in the register we use for saving registers to the stack:
1849   This is usually SP, but not always.  Again, we deduce that if you
1850   copy SP into another register (and SP is not the CFA register),
1851   then the new register is the one we will be using for register
1852   saves.  This also seems to work.
1853
1854   Register saves: There's not much guesswork about this one; if
1855   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1856   register save, and the register used to calculate the destination
1857   had better be the one we think we're using for this purpose.
1858   It's also assumed that a copy from a call-saved register to another
1859   register is saving that register if RTX_FRAME_RELATED_P is set on
1860   that instruction.  If the copy is from a call-saved register to
1861   the *same* register, that means that the register is now the same
1862   value as in the caller.
1863
1864   Except: If the register being saved is the CFA register, and the
1865   offset is nonzero, we are saving the CFA, so we assume we have to
1866   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1867   the intent is to save the value of SP from the previous frame.
1868
1869   In addition, if a register has previously been saved to a different
1870   register,
1871
1872   Invariants / Summaries of Rules
1873
1874   cfa          current rule for calculating the CFA.  It usually
1875                consists of a register and an offset.
1876   cfa_store    register used by prologue code to save things to the stack
1877                cfa_store.offset is the offset from the value of
1878                cfa_store.reg to the actual CFA
1879   cfa_temp     register holding an integral value.  cfa_temp.offset
1880                stores the value, which will be used to adjust the
1881                stack pointer.  cfa_temp is also used like cfa_store,
1882                to track stores to the stack via fp or a temp reg.
1883
1884   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1885                with cfa.reg as the first operand changes the cfa.reg and its
1886                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1887                cfa_temp.offset.
1888
1889   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1890                expression yielding a constant.  This sets cfa_temp.reg
1891                and cfa_temp.offset.
1892
1893   Rule 5:      Create a new register cfa_store used to save items to the
1894                stack.
1895
1896   Rules 10-14: Save a register to the stack.  Define offset as the
1897                difference of the original location and cfa_store's
1898                location (or cfa_temp's location if cfa_temp is used).
1899
1900   Rules 16-20: If AND operation happens on sp in prologue, we assume
1901                stack is realigned.  We will use a group of DW_OP_XXX
1902                expressions to represent the location of the stored
1903                register instead of CFA+offset.
1904
1905   The Rules
1906
1907   "{a,b}" indicates a choice of a xor b.
1908   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1909
1910   Rule 1:
1911   (set <reg1> <reg2>:cfa.reg)
1912   effects: cfa.reg = <reg1>
1913            cfa.offset unchanged
1914            cfa_temp.reg = <reg1>
1915            cfa_temp.offset = cfa.offset
1916
1917   Rule 2:
1918   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1919                               {<const_int>,<reg>:cfa_temp.reg}))
1920   effects: cfa.reg = sp if fp used
1921            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1922            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1923              if cfa_store.reg==sp
1924
1925   Rule 3:
1926   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1927   effects: cfa.reg = fp
1928            cfa_offset += +/- <const_int>
1929
1930   Rule 4:
1931   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1932   constraints: <reg1> != fp
1933                <reg1> != sp
1934   effects: cfa.reg = <reg1>
1935            cfa_temp.reg = <reg1>
1936            cfa_temp.offset = cfa.offset
1937
1938   Rule 5:
1939   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1940   constraints: <reg1> != fp
1941                <reg1> != sp
1942   effects: cfa_store.reg = <reg1>
1943            cfa_store.offset = cfa.offset - cfa_temp.offset
1944
1945   Rule 6:
1946   (set <reg> <const_int>)
1947   effects: cfa_temp.reg = <reg>
1948            cfa_temp.offset = <const_int>
1949
1950   Rule 7:
1951   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1952   effects: cfa_temp.reg = <reg1>
1953            cfa_temp.offset |= <const_int>
1954
1955   Rule 8:
1956   (set <reg> (high <exp>))
1957   effects: none
1958
1959   Rule 9:
1960   (set <reg> (lo_sum <exp> <const_int>))
1961   effects: cfa_temp.reg = <reg>
1962            cfa_temp.offset = <const_int>
1963
1964   Rule 10:
1965   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1966   effects: cfa_store.offset -= <const_int>
1967            cfa.offset = cfa_store.offset if cfa.reg == sp
1968            cfa.reg = sp
1969            cfa.base_offset = -cfa_store.offset
1970
1971   Rule 11:
1972   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1973   effects: cfa_store.offset += -/+ mode_size(mem)
1974            cfa.offset = cfa_store.offset if cfa.reg == sp
1975            cfa.reg = sp
1976            cfa.base_offset = -cfa_store.offset
1977
1978   Rule 12:
1979   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1980
1981        <reg2>)
1982   effects: cfa.reg = <reg1>
1983            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1984
1985   Rule 13:
1986   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1987   effects: cfa.reg = <reg1>
1988            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1989
1990   Rule 14:
1991   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1992   effects: cfa.reg = <reg1>
1993            cfa.base_offset = -cfa_temp.offset
1994            cfa_temp.offset -= mode_size(mem)
1995
1996   Rule 15:
1997   (set <reg> {unspec, unspec_volatile})
1998   effects: target-dependent
1999
2000   Rule 16:
2001   (set sp (and: sp <const_int>))
2002   constraints: cfa_store.reg == sp
2003   effects: current_fde.stack_realign = 1
2004            cfa_store.offset = 0
2005            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2006
2007   Rule 17:
2008   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2009   effects: cfa_store.offset += -/+ mode_size(mem)
2010
2011   Rule 18:
2012   (set (mem ({pre_inc, pre_dec} sp)) fp)
2013   constraints: fde->stack_realign == 1
2014   effects: cfa_store.offset = 0
2015            cfa.reg != HARD_FRAME_POINTER_REGNUM
2016
2017   Rule 19:
2018   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2019   constraints: fde->stack_realign == 1
2020                && cfa.offset == 0
2021                && cfa.indirect == 0
2022                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2023   effects: Use DW_CFA_def_cfa_expression to define cfa
2024            cfa.reg == fde->drap_reg
2025
2026   Rule 20:
2027   (set reg fde->drap_reg)
2028   constraints: fde->vdrap_reg == INVALID_REGNUM
2029   effects: fde->vdrap_reg = reg.
2030   (set mem fde->drap_reg)
2031   constraints: fde->drap_reg_saved == 1
2032   effects: none.  */
2033
2034 static void
2035 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2036 {
2037   rtx src, dest, span;
2038   HOST_WIDE_INT offset;
2039   dw_fde_ref fde;
2040
2041   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2042      the PARALLEL independently. The first element is always processed if
2043      it is a SET. This is for backward compatibility.   Other elements
2044      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2045      flag is set in them.  */
2046   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2047     {
2048       int par_index;
2049       int limit = XVECLEN (expr, 0);
2050       rtx elem;
2051
2052       /* PARALLELs have strict read-modify-write semantics, so we
2053          ought to evaluate every rvalue before changing any lvalue.
2054          It's cumbersome to do that in general, but there's an
2055          easy approximation that is enough for all current users:
2056          handle register saves before register assignments.  */
2057       if (GET_CODE (expr) == PARALLEL)
2058         for (par_index = 0; par_index < limit; par_index++)
2059           {
2060             elem = XVECEXP (expr, 0, par_index);
2061             if (GET_CODE (elem) == SET
2062                 && MEM_P (SET_DEST (elem))
2063                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2064               dwarf2out_frame_debug_expr (elem, label);
2065           }
2066
2067       for (par_index = 0; par_index < limit; par_index++)
2068         {
2069           elem = XVECEXP (expr, 0, par_index);
2070           if (GET_CODE (elem) == SET
2071               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2072               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2073             dwarf2out_frame_debug_expr (elem, label);
2074           else if (GET_CODE (elem) == SET
2075                    && par_index != 0
2076                    && !RTX_FRAME_RELATED_P (elem))
2077             {
2078               /* Stack adjustment combining might combine some post-prologue
2079                  stack adjustment into a prologue stack adjustment.  */
2080               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2081
2082               if (offset != 0)
2083                 dwarf2out_args_size_adjust (offset, label);
2084             }
2085         }
2086       return;
2087     }
2088
2089   gcc_assert (GET_CODE (expr) == SET);
2090
2091   src = SET_SRC (expr);
2092   dest = SET_DEST (expr);
2093
2094   if (REG_P (src))
2095     {
2096       rtx rsi = reg_saved_in (src);
2097       if (rsi)
2098         src = rsi;
2099     }
2100
2101   fde = current_fde ();
2102
2103   if (GET_CODE (src) == REG
2104       && fde
2105       && fde->drap_reg == REGNO (src)
2106       && (fde->drap_reg_saved
2107           || GET_CODE (dest) == REG))
2108     {
2109       /* Rule 20 */
2110       /* If we are saving dynamic realign argument pointer to a
2111          register, the destination is virtual dynamic realign
2112          argument pointer.  It may be used to access argument.  */
2113       if (GET_CODE (dest) == REG)
2114         {
2115           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2116           fde->vdrap_reg = REGNO (dest);
2117         }
2118       return;
2119     }
2120
2121   switch (GET_CODE (dest))
2122     {
2123     case REG:
2124       switch (GET_CODE (src))
2125         {
2126           /* Setting FP from SP.  */
2127         case REG:
2128           if (cfa.reg == (unsigned) REGNO (src))
2129             {
2130               /* Rule 1 */
2131               /* Update the CFA rule wrt SP or FP.  Make sure src is
2132                  relative to the current CFA register.
2133
2134                  We used to require that dest be either SP or FP, but the
2135                  ARM copies SP to a temporary register, and from there to
2136                  FP.  So we just rely on the backends to only set
2137                  RTX_FRAME_RELATED_P on appropriate insns.  */
2138               cfa.reg = REGNO (dest);
2139               cfa_temp.reg = cfa.reg;
2140               cfa_temp.offset = cfa.offset;
2141             }
2142           else
2143             {
2144               /* Saving a register in a register.  */
2145               gcc_assert (!fixed_regs [REGNO (dest)]
2146                           /* For the SPARC and its register window.  */
2147                           || (DWARF_FRAME_REGNUM (REGNO (src))
2148                               == DWARF_FRAME_RETURN_COLUMN));
2149
2150               /* After stack is aligned, we can only save SP in FP
2151                  if drap register is used.  In this case, we have
2152                  to restore stack pointer with the CFA value and we
2153                  don't generate this DWARF information.  */
2154               if (fde
2155                   && fde->stack_realign
2156                   && REGNO (src) == STACK_POINTER_REGNUM)
2157                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2158                             && fde->drap_reg != INVALID_REGNUM
2159                             && cfa.reg != REGNO (src));
2160               else
2161                 queue_reg_save (label, src, dest, 0);
2162             }
2163           break;
2164
2165         case PLUS:
2166         case MINUS:
2167         case LO_SUM:
2168           if (dest == stack_pointer_rtx)
2169             {
2170               /* Rule 2 */
2171               /* Adjusting SP.  */
2172               switch (GET_CODE (XEXP (src, 1)))
2173                 {
2174                 case CONST_INT:
2175                   offset = INTVAL (XEXP (src, 1));
2176                   break;
2177                 case REG:
2178                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2179                               == cfa_temp.reg);
2180                   offset = cfa_temp.offset;
2181                   break;
2182                 default:
2183                   gcc_unreachable ();
2184                 }
2185
2186               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2187                 {
2188                   /* Restoring SP from FP in the epilogue.  */
2189                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2190                   cfa.reg = STACK_POINTER_REGNUM;
2191                 }
2192               else if (GET_CODE (src) == LO_SUM)
2193                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2194                 ;
2195               else
2196                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2197
2198               if (GET_CODE (src) != MINUS)
2199                 offset = -offset;
2200               if (cfa.reg == STACK_POINTER_REGNUM)
2201                 cfa.offset += offset;
2202               if (cfa_store.reg == STACK_POINTER_REGNUM)
2203                 cfa_store.offset += offset;
2204             }
2205           else if (dest == hard_frame_pointer_rtx)
2206             {
2207               /* Rule 3 */
2208               /* Either setting the FP from an offset of the SP,
2209                  or adjusting the FP */
2210               gcc_assert (frame_pointer_needed);
2211
2212               gcc_assert (REG_P (XEXP (src, 0))
2213                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2214                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
2215               offset = INTVAL (XEXP (src, 1));
2216               if (GET_CODE (src) != MINUS)
2217                 offset = -offset;
2218               cfa.offset += offset;
2219               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2220             }
2221           else
2222             {
2223               gcc_assert (GET_CODE (src) != MINUS);
2224
2225               /* Rule 4 */
2226               if (REG_P (XEXP (src, 0))
2227                   && REGNO (XEXP (src, 0)) == cfa.reg
2228                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
2229                 {
2230                   /* Setting a temporary CFA register that will be copied
2231                      into the FP later on.  */
2232                   offset = - INTVAL (XEXP (src, 1));
2233                   cfa.offset += offset;
2234                   cfa.reg = REGNO (dest);
2235                   /* Or used to save regs to the stack.  */
2236                   cfa_temp.reg = cfa.reg;
2237                   cfa_temp.offset = cfa.offset;
2238                 }
2239
2240               /* Rule 5 */
2241               else if (REG_P (XEXP (src, 0))
2242                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2243                        && XEXP (src, 1) == stack_pointer_rtx)
2244                 {
2245                   /* Setting a scratch register that we will use instead
2246                      of SP for saving registers to the stack.  */
2247                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2248                   cfa_store.reg = REGNO (dest);
2249                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2250                 }
2251
2252               /* Rule 9 */
2253               else if (GET_CODE (src) == LO_SUM
2254                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
2255                 {
2256                   cfa_temp.reg = REGNO (dest);
2257                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2258                 }
2259               else
2260                 gcc_unreachable ();
2261             }
2262           break;
2263
2264           /* Rule 6 */
2265         case CONST_INT:
2266           cfa_temp.reg = REGNO (dest);
2267           cfa_temp.offset = INTVAL (src);
2268           break;
2269
2270           /* Rule 7 */
2271         case IOR:
2272           gcc_assert (REG_P (XEXP (src, 0))
2273                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2274                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
2275
2276           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2277             cfa_temp.reg = REGNO (dest);
2278           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2279           break;
2280
2281           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2282              which will fill in all of the bits.  */
2283           /* Rule 8 */
2284         case HIGH:
2285           break;
2286
2287           /* Rule 15 */
2288         case UNSPEC:
2289         case UNSPEC_VOLATILE:
2290           gcc_assert (targetm.dwarf_handle_frame_unspec);
2291           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2292           return;
2293
2294           /* Rule 16 */
2295         case AND:
2296           /* If this AND operation happens on stack pointer in prologue,
2297              we assume the stack is realigned and we extract the
2298              alignment.  */
2299           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2300             {
2301               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2302               fde->stack_realign = 1;
2303               fde->stack_realignment = INTVAL (XEXP (src, 1));
2304               cfa_store.offset = 0;
2305
2306               if (cfa.reg != STACK_POINTER_REGNUM
2307                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2308                 fde->drap_reg = cfa.reg;
2309             }
2310           return;
2311
2312         default:
2313           gcc_unreachable ();
2314         }
2315
2316       def_cfa_1 (label, &cfa);
2317       break;
2318
2319     case MEM:
2320
2321       /* Saving a register to the stack.  Make sure dest is relative to the
2322          CFA register.  */
2323       switch (GET_CODE (XEXP (dest, 0)))
2324         {
2325           /* Rule 10 */
2326           /* With a push.  */
2327         case PRE_MODIFY:
2328           /* We can't handle variable size modifications.  */
2329           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2330                       == CONST_INT);
2331           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2332
2333           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2334                       && cfa_store.reg == STACK_POINTER_REGNUM);
2335
2336           cfa_store.offset += offset;
2337           if (cfa.reg == STACK_POINTER_REGNUM)
2338             cfa.offset = cfa_store.offset;
2339
2340           offset = -cfa_store.offset;
2341           break;
2342
2343           /* Rule 11 */
2344         case PRE_INC:
2345         case PRE_DEC:
2346           offset = GET_MODE_SIZE (GET_MODE (dest));
2347           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2348             offset = -offset;
2349
2350           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2351                        == STACK_POINTER_REGNUM)
2352                       && cfa_store.reg == STACK_POINTER_REGNUM);
2353
2354           cfa_store.offset += offset;
2355
2356           /* Rule 18: If stack is aligned, we will use FP as a
2357              reference to represent the address of the stored
2358              regiser.  */
2359           if (fde
2360               && fde->stack_realign
2361               && src == hard_frame_pointer_rtx)
2362             {
2363               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2364               cfa_store.offset = 0;
2365             }
2366
2367           if (cfa.reg == STACK_POINTER_REGNUM)
2368             cfa.offset = cfa_store.offset;
2369
2370           offset = -cfa_store.offset;
2371           break;
2372
2373           /* Rule 12 */
2374           /* With an offset.  */
2375         case PLUS:
2376         case MINUS:
2377         case LO_SUM:
2378           {
2379             int regno;
2380
2381             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
2382                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2383             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2384             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2385               offset = -offset;
2386
2387             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2388
2389             if (cfa_store.reg == (unsigned) regno)
2390               offset -= cfa_store.offset;
2391             else
2392               {
2393                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2394                 offset -= cfa_temp.offset;
2395               }
2396           }
2397           break;
2398
2399           /* Rule 13 */
2400           /* Without an offset.  */
2401         case REG:
2402           {
2403             int regno = REGNO (XEXP (dest, 0));
2404
2405             if (cfa_store.reg == (unsigned) regno)
2406               offset = -cfa_store.offset;
2407             else
2408               {
2409                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2410                 offset = -cfa_temp.offset;
2411               }
2412           }
2413           break;
2414
2415           /* Rule 14 */
2416         case POST_INC:
2417           gcc_assert (cfa_temp.reg
2418                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2419           offset = -cfa_temp.offset;
2420           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2421           break;
2422
2423         default:
2424           gcc_unreachable ();
2425         }
2426
2427         /* Rule 17 */
2428         /* If the source operand of this MEM operation is not a
2429            register, basically the source is return address.  Here
2430            we only care how much stack grew and we don't save it.  */
2431       if (!REG_P (src))
2432         break;
2433
2434       if (REGNO (src) != STACK_POINTER_REGNUM
2435           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2436           && (unsigned) REGNO (src) == cfa.reg)
2437         {
2438           /* We're storing the current CFA reg into the stack.  */
2439
2440           if (cfa.offset == 0)
2441             {
2442               /* Rule 19 */
2443               /* If stack is aligned, putting CFA reg into stack means
2444                  we can no longer use reg + offset to represent CFA.
2445                  Here we use DW_CFA_def_cfa_expression instead.  The
2446                  result of this expression equals to the original CFA
2447                  value.  */
2448               if (fde
2449                   && fde->stack_realign
2450                   && cfa.indirect == 0
2451                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2452                 {
2453                   dw_cfa_location cfa_exp;
2454
2455                   gcc_assert (fde->drap_reg == cfa.reg);
2456
2457                   cfa_exp.indirect = 1;
2458                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2459                   cfa_exp.base_offset = offset;
2460                   cfa_exp.offset = 0;
2461
2462                   fde->drap_reg_saved = 1;
2463
2464                   def_cfa_1 (label, &cfa_exp);
2465                   break;
2466                 }
2467
2468               /* If the source register is exactly the CFA, assume
2469                  we're saving SP like any other register; this happens
2470                  on the ARM.  */
2471               def_cfa_1 (label, &cfa);
2472               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2473               break;
2474             }
2475           else
2476             {
2477               /* Otherwise, we'll need to look in the stack to
2478                  calculate the CFA.  */
2479               rtx x = XEXP (dest, 0);
2480
2481               if (!REG_P (x))
2482                 x = XEXP (x, 0);
2483               gcc_assert (REG_P (x));
2484
2485               cfa.reg = REGNO (x);
2486               cfa.base_offset = offset;
2487               cfa.indirect = 1;
2488               def_cfa_1 (label, &cfa);
2489               break;
2490             }
2491         }
2492
2493       def_cfa_1 (label, &cfa);
2494       {
2495         span = targetm.dwarf_register_span (src);
2496
2497         if (!span)
2498           queue_reg_save (label, src, NULL_RTX, offset);
2499         else
2500           {
2501             /* We have a PARALLEL describing where the contents of SRC
2502                live.  Queue register saves for each piece of the
2503                PARALLEL.  */
2504             int par_index;
2505             int limit;
2506             HOST_WIDE_INT span_offset = offset;
2507
2508             gcc_assert (GET_CODE (span) == PARALLEL);
2509
2510             limit = XVECLEN (span, 0);
2511             for (par_index = 0; par_index < limit; par_index++)
2512               {
2513                 rtx elem = XVECEXP (span, 0, par_index);
2514
2515                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2516                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2517               }
2518           }
2519       }
2520       break;
2521
2522     default:
2523       gcc_unreachable ();
2524     }
2525 }
2526
2527 /* Record call frame debugging information for INSN, which either
2528    sets SP or FP (adjusting how we calculate the frame address) or saves a
2529    register to the stack.  If INSN is NULL_RTX, initialize our state.
2530
2531    If AFTER_P is false, we're being called before the insn is emitted,
2532    otherwise after.  Call instructions get invoked twice.  */
2533
2534 void
2535 dwarf2out_frame_debug (rtx insn, bool after_p)
2536 {
2537   const char *label;
2538   rtx note, n;
2539   bool handled_one = false;
2540
2541   if (insn == NULL_RTX)
2542     {
2543       size_t i;
2544
2545       /* Flush any queued register saves.  */
2546       flush_queued_reg_saves ();
2547
2548       /* Set up state for generating call frame debug info.  */
2549       lookup_cfa (&cfa);
2550       gcc_assert (cfa.reg
2551                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2552
2553       cfa.reg = STACK_POINTER_REGNUM;
2554       cfa_store = cfa;
2555       cfa_temp.reg = -1;
2556       cfa_temp.offset = 0;
2557
2558       for (i = 0; i < num_regs_saved_in_regs; i++)
2559         {
2560           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2561           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2562         }
2563       num_regs_saved_in_regs = 0;
2564
2565       if (barrier_args_size)
2566         {
2567           XDELETEVEC (barrier_args_size);
2568           barrier_args_size = NULL;
2569         }
2570       return;
2571     }
2572
2573   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2574     flush_queued_reg_saves ();
2575
2576   if (! RTX_FRAME_RELATED_P (insn))
2577     {
2578       if (!ACCUMULATE_OUTGOING_ARGS)
2579         dwarf2out_stack_adjust (insn, after_p);
2580       return;
2581     }
2582
2583   label = dwarf2out_cfi_label ();
2584
2585   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2586     switch (REG_NOTE_KIND (note))
2587       {
2588       case REG_FRAME_RELATED_EXPR:
2589         insn = XEXP (note, 0);
2590         goto found;
2591
2592       case REG_CFA_DEF_CFA:
2593         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2594         handled_one = true;
2595         break;
2596
2597       case REG_CFA_ADJUST_CFA:
2598         n = XEXP (note, 0);
2599         if (n == NULL)
2600           {
2601             n = PATTERN (insn);
2602             if (GET_CODE (n) == PARALLEL)
2603               n = XVECEXP (n, 0, 0);
2604           }
2605         dwarf2out_frame_debug_adjust_cfa (n, label);
2606         handled_one = true;
2607         break;
2608
2609       case REG_CFA_OFFSET:
2610         n = XEXP (note, 0);
2611         if (n == NULL)
2612           n = single_set (insn);
2613         dwarf2out_frame_debug_cfa_offset (n, label);
2614         handled_one = true;
2615         break;
2616
2617       case REG_CFA_REGISTER:
2618         n = XEXP (note, 0);
2619         if (n == NULL)
2620           {
2621             n = PATTERN (insn);
2622             if (GET_CODE (n) == PARALLEL)
2623               n = XVECEXP (n, 0, 0);
2624           }
2625         dwarf2out_frame_debug_cfa_register (n, label);
2626         handled_one = true;
2627         break;
2628
2629       case REG_CFA_RESTORE:
2630         n = XEXP (note, 0);
2631         if (n == NULL)
2632           {
2633             n = PATTERN (insn);
2634             if (GET_CODE (n) == PARALLEL)
2635               n = XVECEXP (n, 0, 0);
2636             n = XEXP (n, 0);
2637           }
2638         dwarf2out_frame_debug_cfa_restore (n, label);
2639         handled_one = true;
2640         break;
2641
2642       default:
2643         break;
2644       }
2645   if (handled_one)
2646     return;
2647
2648   insn = PATTERN (insn);
2649  found:
2650   dwarf2out_frame_debug_expr (insn, label);
2651 }
2652
2653 /* Determine if we need to save and restore CFI information around this
2654    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2655    we do need to save/restore, then emit the save now, and insert a
2656    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2657
2658 void
2659 dwarf2out_begin_epilogue (rtx insn)
2660 {
2661   bool saw_frp = false;
2662   rtx i;
2663   dw_cfi_ref cfi;
2664
2665   /* Scan forward to the return insn, noticing if there are possible
2666      frame related insns.  */
2667   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2668     {
2669       if (!INSN_P (i))
2670         continue;
2671
2672       /* Look for both regular and sibcalls to end the block.  */
2673       if (returnjump_p (i))
2674         break;
2675       if (CALL_P (i) && SIBLING_CALL_P (i))
2676         break;
2677
2678       if (RTX_FRAME_RELATED_P (i))
2679         saw_frp = true;
2680     }
2681
2682   /* If the port doesn't emit epilogue unwind info, we don't need a
2683      save/restore pair.  */
2684   if (!saw_frp)
2685     return;
2686
2687   /* Otherwise, search forward to see if the return insn was the last
2688      basic block of the function.  If so, we don't need save/restore.  */
2689   gcc_assert (i != NULL);
2690   i = next_real_insn (i);
2691   if (i == NULL)
2692     return;
2693
2694   /* Insert the restore before that next real insn in the stream, and before
2695      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2696      properly nested.  This should be after any label or alignment.  This
2697      will be pushed into the CFI stream by the function below.  */
2698   while (1)
2699     {
2700       rtx p = PREV_INSN (i);
2701       if (!NOTE_P (p))
2702         break;
2703       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2704         break;
2705       i = p;
2706     }
2707   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2708
2709   /* Emit the state save.  */
2710   cfi = new_cfi (); 
2711   cfi->dw_cfi_opc = DW_CFA_remember_state;
2712   add_fde_cfi (dwarf2out_cfi_label (), cfi);
2713
2714   /* And emulate the state save.  */
2715   gcc_assert (!cfa_remember.in_use);
2716   cfa_remember = cfa;
2717   cfa_remember.in_use = 1;
2718 }
2719
2720 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2721
2722 void
2723 dwarf2out_frame_debug_restore_state (void)
2724 {
2725   dw_cfi_ref cfi = new_cfi (); 
2726   const char *label = dwarf2out_cfi_label ();
2727
2728   cfi->dw_cfi_opc = DW_CFA_restore_state;
2729   add_fde_cfi (label, cfi);
2730
2731   gcc_assert (cfa_remember.in_use);
2732   cfa = cfa_remember;
2733   cfa_remember.in_use = 0;
2734 }
2735
2736 #endif
2737
2738 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2739 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2740  (enum dwarf_call_frame_info cfi);
2741
2742 static enum dw_cfi_oprnd_type
2743 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2744 {
2745   switch (cfi)
2746     {
2747     case DW_CFA_nop:
2748     case DW_CFA_GNU_window_save:
2749     case DW_CFA_remember_state:
2750     case DW_CFA_restore_state:
2751       return dw_cfi_oprnd_unused;
2752
2753     case DW_CFA_set_loc:
2754     case DW_CFA_advance_loc1:
2755     case DW_CFA_advance_loc2:
2756     case DW_CFA_advance_loc4:
2757     case DW_CFA_MIPS_advance_loc8:
2758       return dw_cfi_oprnd_addr;
2759
2760     case DW_CFA_offset:
2761     case DW_CFA_offset_extended:
2762     case DW_CFA_def_cfa:
2763     case DW_CFA_offset_extended_sf:
2764     case DW_CFA_def_cfa_sf:
2765     case DW_CFA_restore:
2766     case DW_CFA_restore_extended:
2767     case DW_CFA_undefined:
2768     case DW_CFA_same_value:
2769     case DW_CFA_def_cfa_register:
2770     case DW_CFA_register:
2771       return dw_cfi_oprnd_reg_num;
2772
2773     case DW_CFA_def_cfa_offset:
2774     case DW_CFA_GNU_args_size:
2775     case DW_CFA_def_cfa_offset_sf:
2776       return dw_cfi_oprnd_offset;
2777
2778     case DW_CFA_def_cfa_expression:
2779     case DW_CFA_expression:
2780       return dw_cfi_oprnd_loc;
2781
2782     default:
2783       gcc_unreachable ();
2784     }
2785 }
2786
2787 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2788 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2789  (enum dwarf_call_frame_info cfi);
2790
2791 static enum dw_cfi_oprnd_type
2792 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2793 {
2794   switch (cfi)
2795     {
2796     case DW_CFA_def_cfa:
2797     case DW_CFA_def_cfa_sf:
2798     case DW_CFA_offset:
2799     case DW_CFA_offset_extended_sf:
2800     case DW_CFA_offset_extended:
2801       return dw_cfi_oprnd_offset;
2802
2803     case DW_CFA_register:
2804       return dw_cfi_oprnd_reg_num;
2805
2806     default:
2807       return dw_cfi_oprnd_unused;
2808     }
2809 }
2810
2811 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2812
2813 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2814    switch to the data section instead, and write out a synthetic label
2815    for collect2.  */
2816
2817 static void
2818 switch_to_eh_frame_section (void)
2819 {
2820   tree label;
2821
2822 #ifdef EH_FRAME_SECTION_NAME
2823   if (eh_frame_section == 0)
2824     {
2825       int flags;
2826
2827       if (EH_TABLES_CAN_BE_READ_ONLY)
2828         {
2829           int fde_encoding;
2830           int per_encoding;
2831           int lsda_encoding;
2832
2833           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2834                                                        /*global=*/0);
2835           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2836                                                        /*global=*/1);
2837           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2838                                                         /*global=*/0);
2839           flags = ((! flag_pic
2840                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2841                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2842                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2843                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2844                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2845                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2846                    ? 0 : SECTION_WRITE);
2847         }
2848       else
2849         flags = SECTION_WRITE;
2850       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2851     }
2852 #endif
2853
2854   if (eh_frame_section)
2855     switch_to_section (eh_frame_section);
2856   else
2857     {
2858       /* We have no special eh_frame section.  Put the information in
2859          the data section and emit special labels to guide collect2.  */
2860       switch_to_section (data_section);
2861       label = get_file_function_name ("F");
2862       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2863       targetm.asm_out.globalize_label (asm_out_file,
2864                                        IDENTIFIER_POINTER (label));
2865       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2866     }
2867 }
2868
2869 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
2870
2871 static HOST_WIDE_INT
2872 div_data_align (HOST_WIDE_INT off)
2873 {
2874   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
2875   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
2876   return r;
2877 }
2878
2879 /* Output a Call Frame Information opcode and its operand(s).  */
2880
2881 static void
2882 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2883 {
2884   unsigned long r;
2885   HOST_WIDE_INT off;
2886
2887   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2888     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2889                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2890                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2891                          ((unsigned HOST_WIDE_INT)
2892                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
2893   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2894     {
2895       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2896       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2897                            "DW_CFA_offset, column 0x%lx", r);
2898       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2899       dw2_asm_output_data_uleb128 (off, NULL);
2900     }
2901   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2902     {
2903       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2904       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2905                            "DW_CFA_restore, column 0x%lx", r);
2906     }
2907   else
2908     {
2909       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2910                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2911
2912       switch (cfi->dw_cfi_opc)
2913         {
2914         case DW_CFA_set_loc:
2915           if (for_eh)
2916             dw2_asm_output_encoded_addr_rtx (
2917                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2918                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2919                 false, NULL);
2920           else
2921             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2922                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2923           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2924           break;
2925
2926         case DW_CFA_advance_loc1:
2927           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2928                                 fde->dw_fde_current_label, NULL);
2929           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2930           break;
2931
2932         case DW_CFA_advance_loc2:
2933           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2934                                 fde->dw_fde_current_label, NULL);
2935           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2936           break;
2937
2938         case DW_CFA_advance_loc4:
2939           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2940                                 fde->dw_fde_current_label, NULL);
2941           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2942           break;
2943
2944         case DW_CFA_MIPS_advance_loc8:
2945           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2946                                 fde->dw_fde_current_label, NULL);
2947           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2948           break;
2949
2950         case DW_CFA_offset_extended:
2951           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2952           dw2_asm_output_data_uleb128 (r, NULL);
2953           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2954           dw2_asm_output_data_uleb128 (off, NULL);
2955           break;
2956
2957         case DW_CFA_def_cfa:
2958           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2959           dw2_asm_output_data_uleb128 (r, NULL);
2960           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2961           break;
2962
2963         case DW_CFA_offset_extended_sf:
2964           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2965           dw2_asm_output_data_uleb128 (r, NULL);
2966           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2967           dw2_asm_output_data_sleb128 (off, NULL);
2968           break;
2969
2970         case DW_CFA_def_cfa_sf:
2971           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2972           dw2_asm_output_data_uleb128 (r, NULL);
2973           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
2974           dw2_asm_output_data_sleb128 (off, NULL);
2975           break;
2976
2977         case DW_CFA_restore_extended:
2978         case DW_CFA_undefined:
2979         case DW_CFA_same_value:
2980         case DW_CFA_def_cfa_register:
2981           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2982           dw2_asm_output_data_uleb128 (r, NULL);
2983           break;
2984
2985         case DW_CFA_register:
2986           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2987           dw2_asm_output_data_uleb128 (r, NULL);
2988           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2989           dw2_asm_output_data_uleb128 (r, NULL);
2990           break;
2991
2992         case DW_CFA_def_cfa_offset:
2993         case DW_CFA_GNU_args_size:
2994           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2995           break;
2996
2997         case DW_CFA_def_cfa_offset_sf:
2998           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
2999           dw2_asm_output_data_sleb128 (off, NULL);
3000           break;
3001
3002         case DW_CFA_GNU_window_save:
3003           break;
3004
3005         case DW_CFA_def_cfa_expression:
3006         case DW_CFA_expression:
3007           output_cfa_loc (cfi);
3008           break;
3009
3010         case DW_CFA_GNU_negative_offset_extended:
3011           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3012           gcc_unreachable ();
3013
3014         default:
3015           break;
3016         }
3017     }
3018 }
3019
3020 /* Similar, but do it via assembler directives instead.  */
3021
3022 static void
3023 output_cfi_directive (dw_cfi_ref cfi)
3024 {
3025   unsigned long r, r2;
3026
3027   switch (cfi->dw_cfi_opc)
3028     {
3029     case DW_CFA_advance_loc:
3030     case DW_CFA_advance_loc1:
3031     case DW_CFA_advance_loc2:
3032     case DW_CFA_advance_loc4:
3033     case DW_CFA_MIPS_advance_loc8:
3034     case DW_CFA_set_loc:
3035       /* Should only be created by add_fde_cfi in a code path not
3036          followed when emitting via directives.  The assembler is
3037          going to take care of this for us.  */
3038       gcc_unreachable ();
3039
3040     case DW_CFA_offset:
3041     case DW_CFA_offset_extended:
3042     case DW_CFA_offset_extended_sf:
3043       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
3044       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3045                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3046       break;
3047
3048     case DW_CFA_restore:
3049     case DW_CFA_restore_extended:
3050       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
3051       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3052       break;
3053
3054     case DW_CFA_undefined:
3055       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
3056       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3057       break;
3058
3059     case DW_CFA_same_value:
3060       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
3061       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3062       break;
3063
3064     case DW_CFA_def_cfa:
3065     case DW_CFA_def_cfa_sf:
3066       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
3067       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3068                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3069       break;
3070
3071     case DW_CFA_def_cfa_register:
3072       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
3073       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3074       break;
3075
3076     case DW_CFA_register:
3077       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 0);
3078       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 0);
3079       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3080       break;
3081
3082     case DW_CFA_def_cfa_offset:
3083     case DW_CFA_def_cfa_offset_sf:
3084       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3085                HOST_WIDE_INT_PRINT_DEC"\n",
3086                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3087       break;
3088
3089     case DW_CFA_remember_state:
3090       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3091       break;
3092     case DW_CFA_restore_state:
3093       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3094       break;
3095
3096     case DW_CFA_GNU_args_size:
3097       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3098       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3099       if (flag_debug_asm)
3100         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3101                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3102       fputc ('\n', asm_out_file);
3103       break;
3104
3105     case DW_CFA_GNU_window_save:
3106       fprintf (asm_out_file, "\t.cfi_window_save\n");
3107       break;
3108
3109     case DW_CFA_def_cfa_expression:
3110     case DW_CFA_expression:
3111       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3112       output_cfa_loc_raw (cfi);
3113       fputc ('\n', asm_out_file);
3114       break;
3115
3116     default:
3117       gcc_unreachable ();
3118     }
3119 }
3120
3121 /* Output the call frame information used to record information
3122    that relates to calculating the frame pointer, and records the
3123    location of saved registers.  */
3124
3125 static void
3126 output_call_frame_info (int for_eh)
3127 {
3128   unsigned int i;
3129   dw_fde_ref fde;
3130   dw_cfi_ref cfi;
3131   char l1[20], l2[20], section_start_label[20];
3132   bool any_lsda_needed = false;
3133   char augmentation[6];
3134   int augmentation_size;
3135   int fde_encoding = DW_EH_PE_absptr;
3136   int per_encoding = DW_EH_PE_absptr;
3137   int lsda_encoding = DW_EH_PE_absptr;
3138   int return_reg;
3139
3140   /* Don't emit a CIE if there won't be any FDEs.  */
3141   if (fde_table_in_use == 0)
3142     return;
3143
3144   /* Nothing to do if the assembler's doing it all.  */
3145   if (dwarf2out_do_cfi_asm ())
3146     return;
3147
3148   /* If we make FDEs linkonce, we may have to emit an empty label for
3149      an FDE that wouldn't otherwise be emitted.  We want to avoid
3150      having an FDE kept around when the function it refers to is
3151      discarded.  Example where this matters: a primary function
3152      template in C++ requires EH information, but an explicit
3153      specialization doesn't.  */
3154   if (TARGET_USES_WEAK_UNWIND_INFO
3155       && ! flag_asynchronous_unwind_tables
3156       && flag_exceptions
3157       && for_eh)
3158     for (i = 0; i < fde_table_in_use; i++)
3159       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3160           && !fde_table[i].uses_eh_lsda
3161           && ! DECL_WEAK (fde_table[i].decl))
3162         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3163                                       for_eh, /* empty */ 1);
3164
3165   /* If we don't have any functions we'll want to unwind out of, don't
3166      emit any EH unwind information.  Note that if exceptions aren't
3167      enabled, we won't have collected nothrow information, and if we
3168      asked for asynchronous tables, we always want this info.  */
3169   if (for_eh)
3170     {
3171       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3172
3173       for (i = 0; i < fde_table_in_use; i++)
3174         if (fde_table[i].uses_eh_lsda)
3175           any_eh_needed = any_lsda_needed = true;
3176         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3177           any_eh_needed = true;
3178         else if (! fde_table[i].nothrow
3179                  && ! fde_table[i].all_throwers_are_sibcalls)
3180           any_eh_needed = true;
3181
3182       if (! any_eh_needed)
3183         return;
3184     }
3185
3186   /* We're going to be generating comments, so turn on app.  */
3187   if (flag_debug_asm)
3188     app_enable ();
3189
3190   if (for_eh)
3191     switch_to_eh_frame_section ();
3192   else
3193     {
3194       if (!debug_frame_section)
3195         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3196                                            SECTION_DEBUG, NULL);
3197       switch_to_section (debug_frame_section);
3198     }
3199
3200   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3201   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3202
3203   /* Output the CIE.  */
3204   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3205   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3206   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3207     dw2_asm_output_data (4, 0xffffffff,
3208       "Initial length escape value indicating 64-bit DWARF extension");
3209   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3210                         "Length of Common Information Entry");
3211   ASM_OUTPUT_LABEL (asm_out_file, l1);
3212
3213   /* Now that the CIE pointer is PC-relative for EH,
3214      use 0 to identify the CIE.  */
3215   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3216                        (for_eh ? 0 : DWARF_CIE_ID),
3217                        "CIE Identifier Tag");
3218
3219   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
3220
3221   augmentation[0] = 0;
3222   augmentation_size = 0;
3223   if (for_eh)
3224     {
3225       char *p;
3226
3227       /* Augmentation:
3228          z      Indicates that a uleb128 is present to size the
3229                 augmentation section.
3230          L      Indicates the encoding (and thus presence) of
3231                 an LSDA pointer in the FDE augmentation.
3232          R      Indicates a non-default pointer encoding for
3233                 FDE code pointers.
3234          P      Indicates the presence of an encoding + language
3235                 personality routine in the CIE augmentation.  */
3236
3237       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3238       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3239       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3240
3241       p = augmentation + 1;
3242       if (eh_personality_libfunc)
3243         {
3244           *p++ = 'P';
3245           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3246           assemble_external_libcall (eh_personality_libfunc);
3247         }
3248       if (any_lsda_needed)
3249         {
3250           *p++ = 'L';
3251           augmentation_size += 1;
3252         }
3253       if (fde_encoding != DW_EH_PE_absptr)
3254         {
3255           *p++ = 'R';
3256           augmentation_size += 1;
3257         }
3258       if (p > augmentation + 1)
3259         {
3260           augmentation[0] = 'z';
3261           *p = '\0';
3262         }
3263
3264       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3265       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3266         {
3267           int offset = (  4             /* Length */
3268                         + 4             /* CIE Id */
3269                         + 1             /* CIE version */
3270                         + strlen (augmentation) + 1     /* Augmentation */
3271                         + size_of_uleb128 (1)           /* Code alignment */
3272                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3273                         + 1             /* RA column */
3274                         + 1             /* Augmentation size */
3275                         + 1             /* Personality encoding */ );
3276           int pad = -offset & (PTR_SIZE - 1);
3277
3278           augmentation_size += pad;
3279
3280           /* Augmentations should be small, so there's scarce need to
3281              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3282           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3283         }
3284     }
3285
3286   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3287   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3288   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3289                                "CIE Data Alignment Factor");
3290
3291   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3292   if (DW_CIE_VERSION == 1)
3293     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3294   else
3295     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3296
3297   if (augmentation[0])
3298     {
3299       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3300       if (eh_personality_libfunc)
3301         {
3302           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3303                                eh_data_format_name (per_encoding));
3304           dw2_asm_output_encoded_addr_rtx (per_encoding,
3305                                            eh_personality_libfunc,
3306                                            true, NULL);
3307         }
3308
3309       if (any_lsda_needed)
3310         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3311                              eh_data_format_name (lsda_encoding));
3312
3313       if (fde_encoding != DW_EH_PE_absptr)
3314         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3315                              eh_data_format_name (fde_encoding));
3316     }
3317
3318   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3319     output_cfi (cfi, NULL, for_eh);
3320
3321   /* Pad the CIE out to an address sized boundary.  */
3322   ASM_OUTPUT_ALIGN (asm_out_file,
3323                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3324   ASM_OUTPUT_LABEL (asm_out_file, l2);
3325
3326   /* Loop through all of the FDE's.  */
3327   for (i = 0; i < fde_table_in_use; i++)
3328     {
3329       fde = &fde_table[i];
3330
3331       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3332       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3333           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3334           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3335           && !fde->uses_eh_lsda)
3336         continue;
3337
3338       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
3339       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
3340       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
3341       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
3342       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3343         dw2_asm_output_data (4, 0xffffffff,
3344                              "Initial length escape value indicating 64-bit DWARF extension");
3345       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3346                             "FDE Length");
3347       ASM_OUTPUT_LABEL (asm_out_file, l1);
3348
3349       if (for_eh)
3350         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3351       else
3352         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3353                                debug_frame_section, "FDE CIE offset");
3354
3355       if (for_eh)
3356         {
3357           if (fde->dw_fde_switched_sections)
3358             {
3359               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
3360                                       fde->dw_fde_unlikely_section_label);
3361               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
3362                                       fde->dw_fde_hot_section_label);
3363               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
3364               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
3365               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
3366                                                "FDE initial location");
3367               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3368                                     fde->dw_fde_hot_section_end_label,
3369                                     fde->dw_fde_hot_section_label,
3370                                     "FDE address range");
3371               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
3372                                                "FDE initial location");
3373               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3374                                     fde->dw_fde_unlikely_section_end_label,
3375                                     fde->dw_fde_unlikely_section_label,
3376                                     "FDE address range");
3377             }
3378           else
3379             {
3380               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
3381               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3382               dw2_asm_output_encoded_addr_rtx (fde_encoding,
3383                                                sym_ref,
3384                                                false,
3385                                                "FDE initial location");
3386               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3387                                     fde->dw_fde_end, fde->dw_fde_begin,
3388                                     "FDE address range");
3389             }
3390         }
3391       else
3392         {
3393           if (fde->dw_fde_switched_sections)
3394             {
3395               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3396                                    fde->dw_fde_hot_section_label,
3397                                    "FDE initial location");
3398               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3399                                     fde->dw_fde_hot_section_end_label,
3400                                     fde->dw_fde_hot_section_label,
3401                                     "FDE address range");
3402               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3403                                    fde->dw_fde_unlikely_section_label,
3404                                    "FDE initial location");
3405               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3406                                     fde->dw_fde_unlikely_section_end_label,
3407                                     fde->dw_fde_unlikely_section_label,
3408                                     "FDE address range");
3409             }
3410           else
3411             {
3412               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
3413                                    "FDE initial location");
3414               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
3415                                     fde->dw_fde_end, fde->dw_fde_begin,
3416                                     "FDE address range");
3417             }
3418         }
3419
3420       if (augmentation[0])
3421         {
3422           if (any_lsda_needed)
3423             {
3424               int size = size_of_encoded_value (lsda_encoding);
3425
3426               if (lsda_encoding == DW_EH_PE_aligned)
3427                 {
3428                   int offset = (  4             /* Length */
3429                                 + 4             /* CIE offset */
3430                                 + 2 * size_of_encoded_value (fde_encoding)
3431                                 + 1             /* Augmentation size */ );
3432                   int pad = -offset & (PTR_SIZE - 1);
3433
3434                   size += pad;
3435                   gcc_assert (size_of_uleb128 (size) == 1);
3436                 }
3437
3438               dw2_asm_output_data_uleb128 (size, "Augmentation size");
3439
3440               if (fde->uses_eh_lsda)
3441                 {
3442                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
3443                                                fde->funcdef_number);
3444                   dw2_asm_output_encoded_addr_rtx (
3445                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
3446                         false, "Language Specific Data Area");
3447                 }
3448               else
3449                 {
3450                   if (lsda_encoding == DW_EH_PE_aligned)
3451                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3452                   dw2_asm_output_data
3453                     (size_of_encoded_value (lsda_encoding), 0,
3454                      "Language Specific Data Area (none)");
3455                 }
3456             }
3457           else
3458             dw2_asm_output_data_uleb128 (0, "Augmentation size");
3459         }
3460
3461       /* Loop through the Call Frame Instructions associated with
3462          this FDE.  */
3463       fde->dw_fde_current_label = fde->dw_fde_begin;
3464       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3465         output_cfi (cfi, fde, for_eh);
3466
3467       /* Pad the FDE out to an address sized boundary.  */
3468       ASM_OUTPUT_ALIGN (asm_out_file,
3469                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3470       ASM_OUTPUT_LABEL (asm_out_file, l2);
3471     }
3472
3473   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3474     dw2_asm_output_data (4, 0, "End of Table");
3475 #ifdef MIPS_DEBUGGING_INFO
3476   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3477      get a value of 0.  Putting .align 0 after the label fixes it.  */
3478   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3479 #endif
3480
3481   /* Turn off app to make assembly quicker.  */
3482   if (flag_debug_asm)
3483     app_disable ();
3484 }
3485
3486 /* Output a marker (i.e. a label) for the beginning of a function, before
3487    the prologue.  */
3488
3489 void
3490 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3491                           const char *file ATTRIBUTE_UNUSED)
3492 {
3493   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3494   char * dup_label;
3495   dw_fde_ref fde;
3496
3497   current_function_func_begin_label = NULL;
3498
3499 #ifdef TARGET_UNWIND_INFO
3500   /* ??? current_function_func_begin_label is also used by except.c
3501      for call-site information.  We must emit this label if it might
3502      be used.  */
3503   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3504       && ! dwarf2out_do_frame ())
3505     return;
3506 #else
3507   if (! dwarf2out_do_frame ())
3508     return;
3509 #endif
3510
3511   switch_to_section (function_section (current_function_decl));
3512   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3513                                current_function_funcdef_no);
3514   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3515                           current_function_funcdef_no);
3516   dup_label = xstrdup (label);
3517   current_function_func_begin_label = dup_label;
3518
3519 #ifdef TARGET_UNWIND_INFO
3520   /* We can elide the fde allocation if we're not emitting debug info.  */
3521   if (! dwarf2out_do_frame ())
3522     return;
3523 #endif
3524
3525   /* Expand the fde table if necessary.  */
3526   if (fde_table_in_use == fde_table_allocated)
3527     {
3528       fde_table_allocated += FDE_TABLE_INCREMENT;
3529       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3530       memset (fde_table + fde_table_in_use, 0,
3531               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3532     }
3533
3534   /* Record the FDE associated with this function.  */
3535   current_funcdef_fde = fde_table_in_use;
3536
3537   /* Add the new FDE at the end of the fde_table.  */
3538   fde = &fde_table[fde_table_in_use++];
3539   fde->decl = current_function_decl;
3540   fde->dw_fde_begin = dup_label;
3541   fde->dw_fde_current_label = dup_label;
3542   fde->dw_fde_hot_section_label = NULL;
3543   fde->dw_fde_hot_section_end_label = NULL;
3544   fde->dw_fde_unlikely_section_label = NULL;
3545   fde->dw_fde_unlikely_section_end_label = NULL;
3546   fde->dw_fde_switched_sections = false;
3547   fde->dw_fde_end = NULL;
3548   fde->dw_fde_cfi = NULL;
3549   fde->funcdef_number = current_function_funcdef_no;
3550   fde->nothrow = crtl->nothrow;
3551   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3552   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3553   fde->drap_reg = INVALID_REGNUM;
3554   fde->vdrap_reg = INVALID_REGNUM;
3555
3556   args_size = old_args_size = 0;
3557
3558   /* We only want to output line number information for the genuine dwarf2
3559      prologue case, not the eh frame case.  */
3560 #ifdef DWARF2_DEBUGGING_INFO
3561   if (file)
3562     dwarf2out_source_line (line, file);
3563 #endif
3564
3565   if (dwarf2out_do_cfi_asm ())
3566     {
3567       int enc;
3568       rtx ref;
3569
3570       fprintf (asm_out_file, "\t.cfi_startproc\n");
3571
3572       if (eh_personality_libfunc)
3573         {
3574           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1); 
3575           ref = eh_personality_libfunc;
3576
3577           /* ??? The GAS support isn't entirely consistent.  We have to
3578              handle indirect support ourselves, but PC-relative is done
3579              in the assembler.  Further, the assembler can't handle any
3580              of the weirder relocation types.  */
3581           if (enc & DW_EH_PE_indirect)
3582             ref = dw2_force_const_mem (ref, true);
3583
3584           fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3585           output_addr_const (asm_out_file, ref);
3586           fputc ('\n', asm_out_file);
3587         }
3588
3589       if (crtl->uses_eh_lsda)
3590         {
3591           char lab[20];
3592
3593           enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3594           ASM_GENERATE_INTERNAL_LABEL (lab, "LLSDA",
3595                                        current_function_funcdef_no);
3596           ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3597           SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3598
3599           if (enc & DW_EH_PE_indirect)
3600             ref = dw2_force_const_mem (ref, true);
3601
3602           fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3603           output_addr_const (asm_out_file, ref);
3604           fputc ('\n', asm_out_file);
3605         }
3606     }
3607 }
3608
3609 /* Output a marker (i.e. a label) for the absolute end of the generated code
3610    for a function definition.  This gets called *after* the epilogue code has
3611    been generated.  */
3612
3613 void
3614 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3615                         const char *file ATTRIBUTE_UNUSED)
3616 {
3617   dw_fde_ref fde;
3618   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3619
3620   if (dwarf2out_do_cfi_asm ())
3621     fprintf (asm_out_file, "\t.cfi_endproc\n");
3622
3623   /* Output a label to mark the endpoint of the code generated for this
3624      function.  */
3625   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3626                                current_function_funcdef_no);
3627   ASM_OUTPUT_LABEL (asm_out_file, label);
3628   fde = current_fde ();
3629   gcc_assert (fde != NULL);
3630   fde->dw_fde_end = xstrdup (label);
3631 }
3632
3633 void
3634 dwarf2out_frame_init (void)
3635 {
3636   /* Allocate the initial hunk of the fde_table.  */
3637   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
3638   fde_table_allocated = FDE_TABLE_INCREMENT;
3639   fde_table_in_use = 0;
3640
3641   /* Generate the CFA instructions common to all FDE's.  Do it now for the
3642      sake of lookup_cfa.  */
3643
3644   /* On entry, the Canonical Frame Address is at SP.  */
3645   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
3646
3647 #ifdef DWARF2_UNWIND_INFO
3648   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
3649     initial_return_save (INCOMING_RETURN_ADDR_RTX);
3650 #endif
3651 }
3652
3653 void
3654 dwarf2out_frame_finish (void)
3655 {
3656   /* Output call frame information.  */
3657   if (DWARF2_FRAME_INFO)
3658     output_call_frame_info (0);
3659
3660 #ifndef TARGET_UNWIND_INFO
3661   /* Output another copy for the unwinder.  */
3662   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
3663     output_call_frame_info (1);
3664 #endif
3665 }
3666
3667 /* Note that the current function section is being used for code.  */
3668
3669 static void
3670 dwarf2out_note_section_used (void)
3671 {
3672   section *sec = current_function_section ();
3673   if (sec == text_section)
3674     text_section_used = true;
3675   else if (sec == cold_text_section)
3676     cold_text_section_used = true;
3677 }
3678
3679 void
3680 dwarf2out_switch_text_section (void)
3681 {
3682   dw_fde_ref fde = current_fde ();
3683
3684   gcc_assert (cfun && fde);
3685
3686   fde->dw_fde_switched_sections = true;
3687   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
3688   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
3689   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
3690   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
3691   have_multiple_function_sections = true;
3692
3693   /* Reset the current label on switching text sections, so that we
3694      don't attempt to advance_loc4 between labels in different sections.  */
3695   fde->dw_fde_current_label = NULL;
3696
3697   /* There is no need to mark used sections when not debugging.  */
3698   if (cold_text_section != NULL)
3699     dwarf2out_note_section_used ();
3700 }
3701 #endif
3702 \f
3703 /* And now, the subset of the debugging information support code necessary
3704    for emitting location expressions.  */
3705
3706 /* Data about a single source file.  */
3707 struct GTY(()) dwarf_file_data {
3708   const char * filename;
3709   int emitted_number;
3710 };
3711
3712 /* We need some way to distinguish DW_OP_addr with a direct symbol
3713    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
3714 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
3715
3716
3717 typedef struct dw_val_struct *dw_val_ref;
3718 typedef struct die_struct *dw_die_ref;
3719 typedef const struct die_struct *const_dw_die_ref;
3720 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
3721 typedef struct dw_loc_list_struct *dw_loc_list_ref;
3722
3723 typedef struct GTY(()) deferred_locations_struct
3724 {
3725   tree variable;
3726   dw_die_ref die;
3727 } deferred_locations;
3728
3729 DEF_VEC_O(deferred_locations);
3730 DEF_VEC_ALLOC_O(deferred_locations,gc);
3731
3732 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
3733
3734 /* Each DIE may have a series of attribute/value pairs.  Values
3735    can take on several forms.  The forms that are used in this
3736    implementation are listed below.  */
3737
3738 enum dw_val_class
3739 {
3740   dw_val_class_addr,
3741   dw_val_class_offset,
3742   dw_val_class_loc,
3743   dw_val_class_loc_list,
3744   dw_val_class_range_list,
3745   dw_val_class_const,
3746   dw_val_class_unsigned_const,
3747   dw_val_class_long_long,
3748   dw_val_class_vec,
3749   dw_val_class_flag,
3750   dw_val_class_die_ref,
3751   dw_val_class_fde_ref,
3752   dw_val_class_lbl_id,
3753   dw_val_class_lineptr,
3754   dw_val_class_str,
3755   dw_val_class_macptr,
3756   dw_val_class_file
3757 };
3758
3759 /* Describe a double word constant value.  */
3760 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
3761
3762 typedef struct GTY(()) dw_long_long_struct {
3763   unsigned long hi;
3764   unsigned long low;
3765 }
3766 dw_long_long_const;
3767
3768 /* Describe a floating point constant value, or a vector constant value.  */
3769
3770 typedef struct GTY(()) dw_vec_struct {
3771   unsigned char * GTY((length ("%h.length"))) array;
3772   unsigned length;
3773   unsigned elt_size;
3774 }
3775 dw_vec_const;
3776
3777 /* The dw_val_node describes an attribute's value, as it is
3778    represented internally.  */
3779
3780 typedef struct GTY(()) dw_val_struct {
3781   enum dw_val_class val_class;
3782   union dw_val_struct_union
3783     {
3784       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
3785       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
3786       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
3787       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
3788       HOST_WIDE_INT GTY ((default)) val_int;
3789       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
3790       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
3791       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
3792       struct dw_val_die_union
3793         {
3794           dw_die_ref die;
3795           int external;
3796         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
3797       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
3798       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
3799       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
3800       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
3801       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
3802     }
3803   GTY ((desc ("%1.val_class"))) v;
3804 }
3805 dw_val_node;
3806
3807 /* Locations in memory are described using a sequence of stack machine
3808    operations.  */
3809
3810 typedef struct GTY(()) dw_loc_descr_struct {
3811   dw_loc_descr_ref dw_loc_next;
3812   enum dwarf_location_atom dw_loc_opc;
3813   int dw_loc_addr;
3814   dw_val_node dw_loc_oprnd1;
3815   dw_val_node dw_loc_oprnd2;
3816 }
3817 dw_loc_descr_node;
3818
3819 /* Location lists are ranges + location descriptions for that range,
3820    so you can track variables that are in different places over
3821    their entire life.  */
3822 typedef struct GTY(()) dw_loc_list_struct {
3823   dw_loc_list_ref dw_loc_next;
3824   const char *begin; /* Label for begin address of range */
3825   const char *end;  /* Label for end address of range */
3826   char *ll_symbol; /* Label for beginning of location list.
3827                       Only on head of list */
3828   const char *section; /* Section this loclist is relative to */
3829   dw_loc_descr_ref expr;
3830 } dw_loc_list_node;
3831
3832 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
3833
3834 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
3835
3836 /* Convert a DWARF stack opcode into its string name.  */
3837
3838 static const char *
3839 dwarf_stack_op_name (unsigned int op)
3840 {
3841   switch (op)
3842     {
3843     case DW_OP_addr:
3844     case INTERNAL_DW_OP_tls_addr:
3845       return "DW_OP_addr";
3846     case DW_OP_deref:
3847       return "DW_OP_deref";
3848     case DW_OP_const1u:
3849       return "DW_OP_const1u";
3850     case DW_OP_const1s:
3851       return "DW_OP_const1s";
3852     case DW_OP_const2u:
3853       return "DW_OP_const2u";
3854     case DW_OP_const2s:
3855       return "DW_OP_const2s";
3856     case DW_OP_const4u:
3857       return "DW_OP_const4u";
3858     case DW_OP_const4s:
3859       return "DW_OP_const4s";
3860     case DW_OP_const8u:
3861       return "DW_OP_const8u";
3862     case DW_OP_const8s:
3863       return "DW_OP_const8s";
3864     case DW_OP_constu:
3865       return "DW_OP_constu";
3866     case DW_OP_consts:
3867       return "DW_OP_consts";
3868     case DW_OP_dup:
3869       return "DW_OP_dup";
3870     case DW_OP_drop:
3871       return "DW_OP_drop";
3872     case DW_OP_over:
3873       return "DW_OP_over";
3874     case DW_OP_pick:
3875       return "DW_OP_pick";
3876     case DW_OP_swap:
3877       return "DW_OP_swap";
3878     case DW_OP_rot:
3879       return "DW_OP_rot";
3880     case DW_OP_xderef:
3881       return "DW_OP_xderef";
3882     case DW_OP_abs:
3883       return "DW_OP_abs";
3884     case DW_OP_and:
3885       return "DW_OP_and";
3886     case DW_OP_div:
3887       return "DW_OP_div";
3888     case DW_OP_minus:
3889       return "DW_OP_minus";
3890     case DW_OP_mod:
3891       return "DW_OP_mod";
3892     case DW_OP_mul:
3893       return "DW_OP_mul";
3894     case DW_OP_neg:
3895       return "DW_OP_neg";
3896     case DW_OP_not:
3897       return "DW_OP_not";
3898     case DW_OP_or:
3899       return "DW_OP_or";
3900     case DW_OP_plus:
3901       return "DW_OP_plus";
3902     case DW_OP_plus_uconst:
3903       return "DW_OP_plus_uconst";
3904     case DW_OP_shl:
3905       return "DW_OP_shl";
3906     case DW_OP_shr:
3907       return "DW_OP_shr";
3908     case DW_OP_shra:
3909       return "DW_OP_shra";
3910     case DW_OP_xor:
3911       return "DW_OP_xor";
3912     case DW_OP_bra:
3913       return "DW_OP_bra";
3914     case DW_OP_eq:
3915       return "DW_OP_eq";
3916     case DW_OP_ge:
3917       return "DW_OP_ge";
3918     case DW_OP_gt:
3919       return "DW_OP_gt";
3920     case DW_OP_le:
3921       return "DW_OP_le";
3922     case DW_OP_lt:
3923       return "DW_OP_lt";
3924     case DW_OP_ne:
3925       return "DW_OP_ne";
3926     case DW_OP_skip:
3927       return "DW_OP_skip";
3928     case DW_OP_lit0:
3929       return "DW_OP_lit0";
3930     case DW_OP_lit1:
3931       return "DW_OP_lit1";
3932     case DW_OP_lit2:
3933       return "DW_OP_lit2";
3934     case DW_OP_lit3:
3935       return "DW_OP_lit3";
3936     case DW_OP_lit4:
3937       return "DW_OP_lit4";
3938     case DW_OP_lit5:
3939       return "DW_OP_lit5";
3940     case DW_OP_lit6:
3941       return "DW_OP_lit6";
3942     case DW_OP_lit7:
3943       return "DW_OP_lit7";
3944     case DW_OP_lit8:
3945       return "DW_OP_lit8";
3946     case DW_OP_lit9:
3947       return "DW_OP_lit9";
3948     case DW_OP_lit10:
3949       return "DW_OP_lit10";
3950     case DW_OP_lit11:
3951       return "DW_OP_lit11";
3952     case DW_OP_lit12:
3953       return "DW_OP_lit12";
3954     case DW_OP_lit13:
3955       return "DW_OP_lit13";
3956     case DW_OP_lit14:
3957       return "DW_OP_lit14";
3958     case DW_OP_lit15:
3959       return "DW_OP_lit15";
3960     case DW_OP_lit16:
3961       return "DW_OP_lit16";
3962     case DW_OP_lit17:
3963       return "DW_OP_lit17";
3964     case DW_OP_lit18:
3965       return "DW_OP_lit18";
3966     case DW_OP_lit19:
3967       return "DW_OP_lit19";
3968     case DW_OP_lit20:
3969       return "DW_OP_lit20";
3970     case DW_OP_lit21:
3971       return "DW_OP_lit21";
3972     case DW_OP_lit22:
3973       return "DW_OP_lit22";
3974     case DW_OP_lit23:
3975       return "DW_OP_lit23";
3976     case DW_OP_lit24:
3977       return "DW_OP_lit24";
3978     case DW_OP_lit25:
3979       return "DW_OP_lit25";
3980     case DW_OP_lit26:
3981       return "DW_OP_lit26";
3982     case DW_OP_lit27:
3983       return "DW_OP_lit27";
3984     case DW_OP_lit28:
3985       return "DW_OP_lit28";
3986     case DW_OP_lit29:
3987       return "DW_OP_lit29";
3988     case DW_OP_lit30:
3989       return "DW_OP_lit30";
3990     case DW_OP_lit31:
3991       return "DW_OP_lit31";
3992     case DW_OP_reg0:
3993       return "DW_OP_reg0";
3994     case DW_OP_reg1:
3995       return "DW_OP_reg1";
3996     case DW_OP_reg2:
3997       return "DW_OP_reg2";
3998     case DW_OP_reg3:
3999       return "DW_OP_reg3";
4000     case DW_OP_reg4:
4001       return "DW_OP_reg4";
4002     case DW_OP_reg5:
4003       return "DW_OP_reg5";
4004     case DW_OP_reg6:
4005       return "DW_OP_reg6";
4006     case DW_OP_reg7:
4007       return "DW_OP_reg7";
4008     case DW_OP_reg8:
4009       return "DW_OP_reg8";
4010     case DW_OP_reg9:
4011       return "DW_OP_reg9";
4012     case DW_OP_reg10:
4013       return "DW_OP_reg10";
4014     case DW_OP_reg11:
4015       return "DW_OP_reg11";
4016     case DW_OP_reg12:
4017       return "DW_OP_reg12";
4018     case DW_OP_reg13:
4019       return "DW_OP_reg13";
4020     case DW_OP_reg14:
4021       return "DW_OP_reg14";
4022     case DW_OP_reg15:
4023       return "DW_OP_reg15";
4024     case DW_OP_reg16:
4025       return "DW_OP_reg16";
4026     case DW_OP_reg17:
4027       return "DW_OP_reg17";
4028     case DW_OP_reg18:
4029       return "DW_OP_reg18";
4030     case DW_OP_reg19:
4031       return "DW_OP_reg19";
4032     case DW_OP_reg20:
4033       return "DW_OP_reg20";
4034     case DW_OP_reg21:
4035       return "DW_OP_reg21";
4036     case DW_OP_reg22:
4037       return "DW_OP_reg22";
4038     case DW_OP_reg23:
4039       return "DW_OP_reg23";
4040     case DW_OP_reg24:
4041       return "DW_OP_reg24";
4042     case DW_OP_reg25:
4043       return "DW_OP_reg25";
4044     case DW_OP_reg26:
4045       return "DW_OP_reg26";
4046     case DW_OP_reg27:
4047       return "DW_OP_reg27";
4048     case DW_OP_reg28:
4049       return "DW_OP_reg28";
4050     case DW_OP_reg29:
4051       return "DW_OP_reg29";
4052     case DW_OP_reg30:
4053       return "DW_OP_reg30";
4054     case DW_OP_reg31:
4055       return "DW_OP_reg31";
4056     case DW_OP_breg0:
4057       return "DW_OP_breg0";
4058     case DW_OP_breg1:
4059       return "DW_OP_breg1";
4060     case DW_OP_breg2:
4061       return "DW_OP_breg2";
4062     case DW_OP_breg3:
4063       return "DW_OP_breg3";
4064     case DW_OP_breg4:
4065       return "DW_OP_breg4";
4066     case DW_OP_breg5:
4067       return "DW_OP_breg5";
4068     case DW_OP_breg6:
4069       return "DW_OP_breg6";
4070     case DW_OP_breg7:
4071       return "DW_OP_breg7";
4072     case DW_OP_breg8:
4073       return "DW_OP_breg8";
4074     case DW_OP_breg9:
4075       return "DW_OP_breg9";
4076     case DW_OP_breg10:
4077       return "DW_OP_breg10";
4078     case DW_OP_breg11:
4079       return "DW_OP_breg11";
4080     case DW_OP_breg12:
4081       return "DW_OP_breg12";
4082     case DW_OP_breg13:
4083       return "DW_OP_breg13";
4084     case DW_OP_breg14:
4085       return "DW_OP_breg14";
4086     case DW_OP_breg15:
4087       return "DW_OP_breg15";
4088     case DW_OP_breg16:
4089       return "DW_OP_breg16";
4090     case DW_OP_breg17:
4091       return "DW_OP_breg17";
4092     case DW_OP_breg18:
4093       return "DW_OP_breg18";
4094     case DW_OP_breg19:
4095       return "DW_OP_breg19";
4096     case DW_OP_breg20:
4097       return "DW_OP_breg20";
4098     case DW_OP_breg21:
4099       return "DW_OP_breg21";
4100     case DW_OP_breg22:
4101       return "DW_OP_breg22";
4102     case DW_OP_breg23:
4103       return "DW_OP_breg23";
4104     case DW_OP_breg24:
4105       return "DW_OP_breg24";
4106     case DW_OP_breg25:
4107       return "DW_OP_breg25";
4108     case DW_OP_breg26:
4109       return "DW_OP_breg26";
4110     case DW_OP_breg27:
4111       return "DW_OP_breg27";
4112     case DW_OP_breg28:
4113       return "DW_OP_breg28";
4114     case DW_OP_breg29:
4115       return "DW_OP_breg29";
4116     case DW_OP_breg30:
4117       return "DW_OP_breg30";
4118     case DW_OP_breg31:
4119       return "DW_OP_breg31";
4120     case DW_OP_regx:
4121       return "DW_OP_regx";
4122     case DW_OP_fbreg:
4123       return "DW_OP_fbreg";
4124     case DW_OP_bregx:
4125       return "DW_OP_bregx";
4126     case DW_OP_piece:
4127       return "DW_OP_piece";
4128     case DW_OP_deref_size:
4129       return "DW_OP_deref_size";
4130     case DW_OP_xderef_size:
4131       return "DW_OP_xderef_size";
4132     case DW_OP_nop:
4133       return "DW_OP_nop";
4134     case DW_OP_push_object_address:
4135       return "DW_OP_push_object_address";
4136     case DW_OP_call2:
4137       return "DW_OP_call2";
4138     case DW_OP_call4:
4139       return "DW_OP_call4";
4140     case DW_OP_call_ref:
4141       return "DW_OP_call_ref";
4142     case DW_OP_GNU_push_tls_address:
4143       return "DW_OP_GNU_push_tls_address";
4144     case DW_OP_GNU_uninit:
4145       return "DW_OP_GNU_uninit";
4146     default:
4147       return "OP_<unknown>";
4148     }
4149 }
4150
4151 /* Return a pointer to a newly allocated location description.  Location
4152    descriptions are simple expression terms that can be strung
4153    together to form more complicated location (address) descriptions.  */
4154
4155 static inline dw_loc_descr_ref
4156 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4157                unsigned HOST_WIDE_INT oprnd2)
4158 {
4159   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4160
4161   descr->dw_loc_opc = op;
4162   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4163   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4164   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4165   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4166
4167   return descr;
4168 }
4169
4170 /* Return a pointer to a newly allocated location description for
4171    REG and OFFSET.  */
4172
4173 static inline dw_loc_descr_ref
4174 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4175 {
4176   if (reg <= 31)
4177     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4178                           offset, 0);
4179   else
4180     return new_loc_descr (DW_OP_bregx, reg, offset);
4181 }
4182
4183 /* Add a location description term to a location description expression.  */
4184
4185 static inline void
4186 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4187 {
4188   dw_loc_descr_ref *d;
4189
4190   /* Find the end of the chain.  */
4191   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4192     ;
4193
4194   *d = descr;
4195 }
4196
4197 /* Add a constant OFFSET to a location expression.  */
4198
4199 static void
4200 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4201 {
4202   dw_loc_descr_ref loc;
4203   HOST_WIDE_INT *p;
4204
4205   gcc_assert (*list_head != NULL);
4206
4207   if (!offset)
4208     return;
4209
4210   /* Find the end of the chain.  */
4211   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4212     ;
4213
4214   p = NULL;
4215   if (loc->dw_loc_opc == DW_OP_fbreg
4216       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4217     p = &loc->dw_loc_oprnd1.v.val_int;
4218   else if (loc->dw_loc_opc == DW_OP_bregx)
4219     p = &loc->dw_loc_oprnd2.v.val_int;
4220
4221   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4222      offset.  Don't optimize if an signed integer overflow would happen.  */
4223   if (p != NULL
4224       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4225           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4226     *p += offset;
4227
4228   else if (offset > 0)
4229     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4230
4231   else
4232     {
4233       loc->dw_loc_next = int_loc_descriptor (offset);
4234       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4235     }
4236 }
4237
4238 /* Return the size of a location descriptor.  */
4239
4240 static unsigned long
4241 size_of_loc_descr (dw_loc_descr_ref loc)
4242 {
4243   unsigned long size = 1;
4244
4245   switch (loc->dw_loc_opc)
4246     {
4247     case DW_OP_addr:
4248     case INTERNAL_DW_OP_tls_addr:
4249       size += DWARF2_ADDR_SIZE;
4250       break;
4251     case DW_OP_const1u:
4252     case DW_OP_const1s:
4253       size += 1;
4254       break;
4255     case DW_OP_const2u:
4256     case DW_OP_const2s:
4257       size += 2;
4258       break;
4259     case DW_OP_const4u:
4260     case DW_OP_const4s:
4261       size += 4;
4262       break;
4263     case DW_OP_const8u:
4264     case DW_OP_const8s:
4265       size += 8;
4266       break;
4267     case DW_OP_constu:
4268       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4269       break;
4270     case DW_OP_consts:
4271       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4272       break;
4273     case DW_OP_pick:
4274       size += 1;
4275       break;
4276     case DW_OP_plus_uconst:
4277       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4278       break;
4279     case DW_OP_skip:
4280     case DW_OP_bra:
4281       size += 2;
4282       break;
4283     case DW_OP_breg0:
4284     case DW_OP_breg1:
4285     case DW_OP_breg2:
4286     case DW_OP_breg3:
4287     case DW_OP_breg4:
4288     case DW_OP_breg5:
4289     case DW_OP_breg6:
4290     case DW_OP_breg7:
4291     case DW_OP_breg8:
4292     case DW_OP_breg9:
4293     case DW_OP_breg10:
4294     case DW_OP_breg11:
4295     case DW_OP_breg12:
4296     case DW_OP_breg13:
4297     case DW_OP_breg14:
4298     case DW_OP_breg15:
4299     case DW_OP_breg16:
4300     case DW_OP_breg17:
4301     case DW_OP_breg18:
4302     case DW_OP_breg19:
4303     case DW_OP_breg20:
4304     case DW_OP_breg21:
4305     case DW_OP_breg22:
4306     case DW_OP_breg23:
4307     case DW_OP_breg24:
4308     case DW_OP_breg25:
4309     case DW_OP_breg26:
4310     case DW_OP_breg27:
4311     case DW_OP_breg28:
4312     case DW_OP_breg29:
4313     case DW_OP_breg30:
4314     case DW_OP_breg31:
4315       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4316       break;
4317     case DW_OP_regx:
4318       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4319       break;
4320     case DW_OP_fbreg:
4321       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4322       break;
4323     case DW_OP_bregx:
4324       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4325       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4326       break;
4327     case DW_OP_piece:
4328       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4329       break;
4330     case DW_OP_deref_size:
4331     case DW_OP_xderef_size:
4332       size += 1;
4333       break;
4334     case DW_OP_call2:
4335       size += 2;
4336       break;
4337     case DW_OP_call4:
4338       size += 4;
4339       break;
4340     case DW_OP_call_ref:
4341       size += DWARF2_ADDR_SIZE;
4342       break;
4343     default:
4344       break;
4345     }
4346
4347   return size;
4348 }
4349
4350 /* Return the size of a series of location descriptors.  */
4351
4352 static unsigned long
4353 size_of_locs (dw_loc_descr_ref loc)
4354 {
4355   dw_loc_descr_ref l;
4356   unsigned long size;
4357
4358   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4359      field, to avoid writing to a PCH file.  */
4360   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4361     {
4362       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4363         break;
4364       size += size_of_loc_descr (l);
4365     }
4366   if (! l)
4367     return size;
4368
4369   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4370     {
4371       l->dw_loc_addr = size;
4372       size += size_of_loc_descr (l);
4373     }
4374
4375   return size;
4376 }
4377
4378 /* Output location description stack opcode's operands (if any).  */
4379
4380 static void
4381 output_loc_operands (dw_loc_descr_ref loc)
4382 {
4383   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4384   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4385
4386   switch (loc->dw_loc_opc)
4387     {
4388 #ifdef DWARF2_DEBUGGING_INFO
4389     case DW_OP_addr:
4390       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4391       break;
4392     case DW_OP_const2u:
4393     case DW_OP_const2s:
4394       dw2_asm_output_data (2, val1->v.val_int, NULL);
4395       break;
4396     case DW_OP_const4u:
4397     case DW_OP_const4s:
4398       dw2_asm_output_data (4, val1->v.val_int, NULL);
4399       break;
4400     case DW_OP_const8u:
4401     case DW_OP_const8s:
4402       gcc_assert (HOST_BITS_PER_LONG >= 64);
4403       dw2_asm_output_data (8, val1->v.val_int, NULL);
4404       break;
4405     case DW_OP_skip:
4406     case DW_OP_bra:
4407       {
4408         int offset;
4409
4410         gcc_assert (val1->val_class == dw_val_class_loc);
4411         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4412
4413         dw2_asm_output_data (2, offset, NULL);
4414       }
4415       break;
4416 #else
4417     case DW_OP_addr:
4418     case DW_OP_const2u:
4419     case DW_OP_const2s:
4420     case DW_OP_const4u:
4421     case DW_OP_const4s:
4422     case DW_OP_const8u:
4423     case DW_OP_const8s:
4424     case DW_OP_skip:
4425     case DW_OP_bra:
4426       /* We currently don't make any attempt to make sure these are
4427          aligned properly like we do for the main unwind info, so
4428          don't support emitting things larger than a byte if we're
4429          only doing unwinding.  */
4430       gcc_unreachable ();
4431 #endif
4432     case DW_OP_const1u:
4433     case DW_OP_const1s:
4434       dw2_asm_output_data (1, val1->v.val_int, NULL);
4435       break;
4436     case DW_OP_constu:
4437       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4438       break;
4439     case DW_OP_consts:
4440       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4441       break;
4442     case DW_OP_pick:
4443       dw2_asm_output_data (1, val1->v.val_int, NULL);
4444       break;
4445     case DW_OP_plus_uconst:
4446       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4447       break;
4448     case DW_OP_breg0:
4449     case DW_OP_breg1:
4450     case DW_OP_breg2:
4451     case DW_OP_breg3:
4452     case DW_OP_breg4:
4453     case DW_OP_breg5:
4454     case DW_OP_breg6:
4455     case DW_OP_breg7:
4456     case DW_OP_breg8:
4457     case DW_OP_breg9:
4458     case DW_OP_breg10:
4459     case DW_OP_breg11:
4460     case DW_OP_breg12:
4461     case DW_OP_breg13:
4462     case DW_OP_breg14:
4463     case DW_OP_breg15:
4464     case DW_OP_breg16:
4465     case DW_OP_breg17:
4466     case DW_OP_breg18:
4467     case DW_OP_breg19:
4468     case DW_OP_breg20:
4469     case DW_OP_breg21:
4470     case DW_OP_breg22:
4471     case DW_OP_breg23:
4472     case DW_OP_breg24:
4473     case DW_OP_breg25:
4474     case DW_OP_breg26:
4475     case DW_OP_breg27:
4476     case DW_OP_breg28:
4477     case DW_OP_breg29:
4478     case DW_OP_breg30:
4479     case DW_OP_breg31:
4480       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4481       break;
4482     case DW_OP_regx:
4483       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4484       break;
4485     case DW_OP_fbreg:
4486       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4487       break;
4488     case DW_OP_bregx:
4489       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4490       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4491       break;
4492     case DW_OP_piece:
4493       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4494       break;
4495     case DW_OP_deref_size:
4496     case DW_OP_xderef_size:
4497       dw2_asm_output_data (1, val1->v.val_int, NULL);
4498       break;
4499
4500     case INTERNAL_DW_OP_tls_addr:
4501       if (targetm.asm_out.output_dwarf_dtprel)
4502         {
4503           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4504                                                DWARF2_ADDR_SIZE,
4505                                                val1->v.val_addr);
4506           fputc ('\n', asm_out_file);
4507         }
4508       else
4509         gcc_unreachable ();
4510       break;
4511
4512     default:
4513       /* Other codes have no operands.  */
4514       break;
4515     }
4516 }
4517
4518 /* Output a sequence of location operations.  */
4519
4520 static void
4521 output_loc_sequence (dw_loc_descr_ref loc)
4522 {
4523   for (; loc != NULL; loc = loc->dw_loc_next)
4524     {
4525       /* Output the opcode.  */
4526       dw2_asm_output_data (1, loc->dw_loc_opc,
4527                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4528
4529       /* Output the operand(s) (if any).  */
4530       output_loc_operands (loc);
4531     }
4532 }
4533
4534 /* Output location description stack opcode's operands (if any).
4535    The output is single bytes on a line, suitable for .cfi_escape.  */
4536
4537 static void
4538 output_loc_operands_raw (dw_loc_descr_ref loc)
4539 {
4540   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4541   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4542
4543   switch (loc->dw_loc_opc)
4544     {
4545     case DW_OP_addr:
4546       /* We cannot output addresses in .cfi_escape, only bytes.  */
4547       gcc_unreachable ();
4548
4549     case DW_OP_const1u:
4550     case DW_OP_const1s:
4551     case DW_OP_pick:
4552     case DW_OP_deref_size:
4553     case DW_OP_xderef_size:
4554       fputc (',', asm_out_file);
4555       dw2_asm_output_data_raw (1, val1->v.val_int);
4556       break;
4557
4558     case DW_OP_const2u:
4559     case DW_OP_const2s:
4560       fputc (',', asm_out_file);
4561       dw2_asm_output_data_raw (2, val1->v.val_int);
4562       break;
4563
4564     case DW_OP_const4u:
4565     case DW_OP_const4s:
4566       fputc (',', asm_out_file);
4567       dw2_asm_output_data_raw (4, val1->v.val_int);
4568       break;
4569
4570     case DW_OP_const8u:
4571     case DW_OP_const8s:
4572       gcc_assert (HOST_BITS_PER_LONG >= 64);
4573       fputc (',', asm_out_file);
4574       dw2_asm_output_data_raw (8, val1->v.val_int);
4575       break;
4576
4577     case DW_OP_skip:
4578     case DW_OP_bra:
4579       {
4580         int offset;
4581
4582         gcc_assert (val1->val_class == dw_val_class_loc);
4583         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4584
4585         fputc (',', asm_out_file);
4586         dw2_asm_output_data_raw (2, offset);
4587       }
4588       break;
4589
4590     case DW_OP_constu:
4591     case DW_OP_plus_uconst:
4592     case DW_OP_regx:
4593     case DW_OP_piece:
4594       fputc (',', asm_out_file);
4595       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4596       break;
4597
4598     case DW_OP_consts:
4599     case DW_OP_breg0:
4600     case DW_OP_breg1:
4601     case DW_OP_breg2:
4602     case DW_OP_breg3:
4603     case DW_OP_breg4:
4604     case DW_OP_breg5:
4605     case DW_OP_breg6:
4606     case DW_OP_breg7:
4607     case DW_OP_breg8:
4608     case DW_OP_breg9:
4609     case DW_OP_breg10:
4610     case DW_OP_breg11:
4611     case DW_OP_breg12:
4612     case DW_OP_breg13:
4613     case DW_OP_breg14:
4614     case DW_OP_breg15:
4615     case DW_OP_breg16:
4616     case DW_OP_breg17:
4617     case DW_OP_breg18:
4618     case DW_OP_breg19:
4619     case DW_OP_breg20:
4620     case DW_OP_breg21:
4621     case DW_OP_breg22:
4622     case DW_OP_breg23:
4623     case DW_OP_breg24:
4624     case DW_OP_breg25:
4625     case DW_OP_breg26:
4626     case DW_OP_breg27:
4627     case DW_OP_breg28:
4628     case DW_OP_breg29:
4629     case DW_OP_breg30:
4630     case DW_OP_breg31:
4631     case DW_OP_fbreg:
4632       fputc (',', asm_out_file);
4633       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
4634       break;
4635
4636     case DW_OP_bregx:
4637       fputc (',', asm_out_file);
4638       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
4639       fputc (',', asm_out_file);
4640       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
4641       break;
4642
4643     case INTERNAL_DW_OP_tls_addr:
4644       gcc_unreachable ();
4645
4646     default:
4647       /* Other codes have no operands.  */
4648       break;
4649     }
4650 }
4651
4652 static void
4653 output_loc_sequence_raw (dw_loc_descr_ref loc)
4654 {
4655   while (1)
4656     {
4657       /* Output the opcode.  */
4658       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
4659       output_loc_operands_raw (loc);
4660
4661       if (!loc->dw_loc_next)
4662         break;
4663       loc = loc->dw_loc_next;
4664
4665       fputc (',', asm_out_file);
4666     }
4667 }
4668
4669 /* This routine will generate the correct assembly data for a location
4670    description based on a cfi entry with a complex address.  */
4671
4672 static void
4673 output_cfa_loc (dw_cfi_ref cfi)
4674 {
4675   dw_loc_descr_ref loc;
4676   unsigned long size;
4677
4678   if (cfi->dw_cfi_opc == DW_CFA_expression)
4679     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
4680
4681   /* Output the size of the block.  */
4682   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4683   size = size_of_locs (loc);
4684   dw2_asm_output_data_uleb128 (size, NULL);
4685
4686   /* Now output the operations themselves.  */
4687   output_loc_sequence (loc);
4688 }
4689
4690 /* Similar, but used for .cfi_escape.  */
4691
4692 static void
4693 output_cfa_loc_raw (dw_cfi_ref cfi)
4694 {
4695   dw_loc_descr_ref loc;
4696   unsigned long size;
4697
4698   if (cfi->dw_cfi_opc == DW_CFA_expression)
4699     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
4700
4701   /* Output the size of the block.  */
4702   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
4703   size = size_of_locs (loc);
4704   dw2_asm_output_data_uleb128_raw (size);
4705   fputc (',', asm_out_file);
4706
4707   /* Now output the operations themselves.  */
4708   output_loc_sequence_raw (loc);
4709 }
4710
4711 /* This function builds a dwarf location descriptor sequence from a
4712    dw_cfa_location, adding the given OFFSET to the result of the
4713    expression.  */
4714
4715 static struct dw_loc_descr_struct *
4716 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
4717 {
4718   struct dw_loc_descr_struct *head, *tmp;
4719
4720   offset += cfa->offset;
4721
4722   if (cfa->indirect)
4723     {
4724       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
4725       head->dw_loc_oprnd1.val_class = dw_val_class_const;
4726       tmp = new_loc_descr (DW_OP_deref, 0, 0);
4727       add_loc_descr (&head, tmp);
4728       if (offset != 0)
4729         {
4730           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4731           add_loc_descr (&head, tmp);
4732         }
4733     }
4734   else
4735     head = new_reg_loc_descr (cfa->reg, offset);
4736
4737   return head;
4738 }
4739
4740 /* This function builds a dwarf location descriptor sequence for
4741    the address at OFFSET from the CFA when stack is aligned to
4742    ALIGNMENT byte.  */
4743
4744 static struct dw_loc_descr_struct *
4745 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
4746 {
4747   struct dw_loc_descr_struct *head;
4748   unsigned int dwarf_fp
4749     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
4750
4751  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
4752   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
4753     {
4754       head = new_reg_loc_descr (dwarf_fp, 0);
4755       add_loc_descr (&head, int_loc_descriptor (alignment));
4756       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
4757       loc_descr_plus_const (&head, offset);
4758     }
4759   else
4760     head = new_reg_loc_descr (dwarf_fp, offset);
4761   return head;
4762 }
4763
4764 /* This function fills in aa dw_cfa_location structure from a dwarf location
4765    descriptor sequence.  */
4766
4767 static void
4768 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
4769 {
4770   struct dw_loc_descr_struct *ptr;
4771   cfa->offset = 0;
4772   cfa->base_offset = 0;
4773   cfa->indirect = 0;
4774   cfa->reg = -1;
4775
4776   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
4777     {
4778       enum dwarf_location_atom op = ptr->dw_loc_opc;
4779
4780       switch (op)
4781         {
4782         case DW_OP_reg0:
4783         case DW_OP_reg1:
4784         case DW_OP_reg2:
4785         case DW_OP_reg3:
4786         case DW_OP_reg4:
4787         case DW_OP_reg5:
4788         case DW_OP_reg6:
4789         case DW_OP_reg7:
4790         case DW_OP_reg8:
4791         case DW_OP_reg9:
4792         case DW_OP_reg10:
4793         case DW_OP_reg11:
4794         case DW_OP_reg12:
4795         case DW_OP_reg13:
4796         case DW_OP_reg14:
4797         case DW_OP_reg15:
4798         case DW_OP_reg16:
4799         case DW_OP_reg17:
4800         case DW_OP_reg18:
4801         case DW_OP_reg19:
4802         case DW_OP_reg20:
4803         case DW_OP_reg21:
4804         case DW_OP_reg22:
4805         case DW_OP_reg23:
4806         case DW_OP_reg24:
4807         case DW_OP_reg25:
4808         case DW_OP_reg26:
4809         case DW_OP_reg27:
4810         case DW_OP_reg28:
4811         case DW_OP_reg29:
4812         case DW_OP_reg30:
4813         case DW_OP_reg31:
4814           cfa->reg = op - DW_OP_reg0;
4815           break;
4816         case DW_OP_regx:
4817           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4818           break;
4819         case DW_OP_breg0:
4820         case DW_OP_breg1:
4821         case DW_OP_breg2:
4822         case DW_OP_breg3:
4823         case DW_OP_breg4:
4824         case DW_OP_breg5:
4825         case DW_OP_breg6:
4826         case DW_OP_breg7:
4827         case DW_OP_breg8:
4828         case DW_OP_breg9:
4829         case DW_OP_breg10:
4830         case DW_OP_breg11:
4831         case DW_OP_breg12:
4832         case DW_OP_breg13:
4833         case DW_OP_breg14:
4834         case DW_OP_breg15:
4835         case DW_OP_breg16:
4836         case DW_OP_breg17:
4837         case DW_OP_breg18:
4838         case DW_OP_breg19:
4839         case DW_OP_breg20:
4840         case DW_OP_breg21:
4841         case DW_OP_breg22:
4842         case DW_OP_breg23:
4843         case DW_OP_breg24:
4844         case DW_OP_breg25:
4845         case DW_OP_breg26:
4846         case DW_OP_breg27:
4847         case DW_OP_breg28:
4848         case DW_OP_breg29:
4849         case DW_OP_breg30:
4850         case DW_OP_breg31:
4851           cfa->reg = op - DW_OP_breg0;
4852           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
4853           break;
4854         case DW_OP_bregx:
4855           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
4856           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
4857           break;
4858         case DW_OP_deref:
4859           cfa->indirect = 1;
4860           break;
4861         case DW_OP_plus_uconst:
4862           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
4863           break;
4864         default:
4865           internal_error ("DW_LOC_OP %s not implemented",
4866                           dwarf_stack_op_name (ptr->dw_loc_opc));
4867         }
4868     }
4869 }
4870 #endif /* .debug_frame support */
4871 \f
4872 /* And now, the support for symbolic debugging information.  */
4873 #ifdef DWARF2_DEBUGGING_INFO
4874
4875 /* .debug_str support.  */
4876 static int output_indirect_string (void **, void *);
4877
4878 static void dwarf2out_init (const char *);
4879 static void dwarf2out_finish (const char *);
4880 static void dwarf2out_define (unsigned int, const char *);
4881 static void dwarf2out_undef (unsigned int, const char *);
4882 static void dwarf2out_start_source_file (unsigned, const char *);
4883 static void dwarf2out_end_source_file (unsigned);
4884 static void dwarf2out_begin_block (unsigned, unsigned);
4885 static void dwarf2out_end_block (unsigned, unsigned);
4886 static bool dwarf2out_ignore_block (const_tree);
4887 static void dwarf2out_global_decl (tree);
4888 static void dwarf2out_type_decl (tree, int);
4889 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
4890 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
4891                                                  dw_die_ref);
4892 static void dwarf2out_abstract_function (tree);
4893 static void dwarf2out_var_location (rtx);
4894 static void dwarf2out_begin_function (tree);
4895 static void dwarf2out_set_name (tree, tree);
4896
4897 /* The debug hooks structure.  */
4898
4899 const struct gcc_debug_hooks dwarf2_debug_hooks =
4900 {
4901   dwarf2out_init,
4902   dwarf2out_finish,
4903   dwarf2out_define,
4904   dwarf2out_undef,
4905   dwarf2out_start_source_file,
4906   dwarf2out_end_source_file,
4907   dwarf2out_begin_block,
4908   dwarf2out_end_block,
4909   dwarf2out_ignore_block,
4910   dwarf2out_source_line,
4911   dwarf2out_begin_prologue,
4912   debug_nothing_int_charstar,   /* end_prologue */
4913   dwarf2out_end_epilogue,
4914   dwarf2out_begin_function,
4915   debug_nothing_int,            /* end_function */
4916   dwarf2out_decl,               /* function_decl */
4917   dwarf2out_global_decl,
4918   dwarf2out_type_decl,          /* type_decl */
4919   dwarf2out_imported_module_or_decl,
4920   debug_nothing_tree,           /* deferred_inline_function */
4921   /* The DWARF 2 backend tries to reduce debugging bloat by not
4922      emitting the abstract description of inline functions until
4923      something tries to reference them.  */
4924   dwarf2out_abstract_function,  /* outlining_inline_function */
4925   debug_nothing_rtx,            /* label */
4926   debug_nothing_int,            /* handle_pch */
4927   dwarf2out_var_location,
4928   dwarf2out_switch_text_section,
4929   dwarf2out_set_name,
4930   1                             /* start_end_main_source_file */
4931 };
4932 #endif
4933 \f
4934 /* NOTE: In the comments in this file, many references are made to
4935    "Debugging Information Entries".  This term is abbreviated as `DIE'
4936    throughout the remainder of this file.  */
4937
4938 /* An internal representation of the DWARF output is built, and then
4939    walked to generate the DWARF debugging info.  The walk of the internal
4940    representation is done after the entire program has been compiled.
4941    The types below are used to describe the internal representation.  */
4942
4943 /* Various DIE's use offsets relative to the beginning of the
4944    .debug_info section to refer to each other.  */
4945
4946 typedef long int dw_offset;
4947
4948 /* Define typedefs here to avoid circular dependencies.  */
4949
4950 typedef struct dw_attr_struct *dw_attr_ref;
4951 typedef struct dw_line_info_struct *dw_line_info_ref;
4952 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
4953 typedef struct pubname_struct *pubname_ref;
4954 typedef struct dw_ranges_struct *dw_ranges_ref;
4955 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
4956
4957 /* Each entry in the line_info_table maintains the file and
4958    line number associated with the label generated for that
4959    entry.  The label gives the PC value associated with
4960    the line number entry.  */
4961
4962 typedef struct GTY(()) dw_line_info_struct {
4963   unsigned long dw_file_num;
4964   unsigned long dw_line_num;
4965 }
4966 dw_line_info_entry;
4967
4968 /* Line information for functions in separate sections; each one gets its
4969    own sequence.  */
4970 typedef struct GTY(()) dw_separate_line_info_struct {
4971   unsigned long dw_file_num;
4972   unsigned long dw_line_num;
4973   unsigned long function;
4974 }
4975 dw_separate_line_info_entry;
4976
4977 /* Each DIE attribute has a field specifying the attribute kind,
4978    a link to the next attribute in the chain, and an attribute value.
4979    Attributes are typically linked below the DIE they modify.  */
4980
4981 typedef struct GTY(()) dw_attr_struct {
4982   enum dwarf_attribute dw_attr;
4983   dw_val_node dw_attr_val;
4984 }
4985 dw_attr_node;
4986
4987 DEF_VEC_O(dw_attr_node);
4988 DEF_VEC_ALLOC_O(dw_attr_node,gc);
4989
4990 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
4991    The children of each node form a circular list linked by
4992    die_sib.  die_child points to the node *before* the "first" child node.  */
4993
4994 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
4995   enum dwarf_tag die_tag;
4996   char *die_symbol;
4997   VEC(dw_attr_node,gc) * die_attr;
4998   dw_die_ref die_parent;
4999   dw_die_ref die_child;
5000   dw_die_ref die_sib;
5001   dw_die_ref die_definition; /* ref from a specification to its definition */
5002   dw_offset die_offset;
5003   unsigned long die_abbrev;
5004   int die_mark;
5005   /* Die is used and must not be pruned as unused.  */
5006   int die_perennial_p;
5007   unsigned int decl_id;
5008 }
5009 die_node;
5010
5011 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5012 #define FOR_EACH_CHILD(die, c, expr) do {       \
5013   c = die->die_child;                           \
5014   if (c) do {                                   \
5015     c = c->die_sib;                             \
5016     expr;                                       \
5017   } while (c != die->die_child);                \
5018 } while (0)
5019
5020 /* The pubname structure */
5021
5022 typedef struct GTY(()) pubname_struct {
5023   dw_die_ref die;
5024   const char *name;
5025 }
5026 pubname_entry;
5027
5028 DEF_VEC_O(pubname_entry);
5029 DEF_VEC_ALLOC_O(pubname_entry, gc);
5030
5031 struct GTY(()) dw_ranges_struct {
5032   /* If this is positive, it's a block number, otherwise it's a
5033      bitwise-negated index into dw_ranges_by_label.  */
5034   int num;
5035 };
5036
5037 struct GTY(()) dw_ranges_by_label_struct {
5038   const char *begin;
5039   const char *end;
5040 };
5041
5042 /* The limbo die list structure.  */
5043 typedef struct GTY(()) limbo_die_struct {
5044   dw_die_ref die;
5045   tree created_for;
5046   struct limbo_die_struct *next;
5047 }
5048 limbo_die_node;
5049
5050 /* How to start an assembler comment.  */
5051 #ifndef ASM_COMMENT_START
5052 #define ASM_COMMENT_START ";#"
5053 #endif
5054
5055 /* Define a macro which returns nonzero for a TYPE_DECL which was
5056    implicitly generated for a tagged type.
5057
5058    Note that unlike the gcc front end (which generates a NULL named
5059    TYPE_DECL node for each complete tagged type, each array type, and
5060    each function type node created) the g++ front end generates a
5061    _named_ TYPE_DECL node for each tagged type node created.
5062    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5063    generate a DW_TAG_typedef DIE for them.  */
5064
5065 #define TYPE_DECL_IS_STUB(decl)                         \
5066   (DECL_NAME (decl) == NULL_TREE                        \
5067    || (DECL_ARTIFICIAL (decl)                           \
5068        && is_tagged_type (TREE_TYPE (decl))             \
5069        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5070            /* This is necessary for stub decls that     \
5071               appear in nested inline functions.  */    \
5072            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5073                && (decl_ultimate_origin (decl)          \
5074                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5075
5076 /* Information concerning the compilation unit's programming
5077    language, and compiler version.  */
5078
5079 /* Fixed size portion of the DWARF compilation unit header.  */
5080 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5081   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5082
5083 /* Fixed size portion of public names info.  */
5084 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5085
5086 /* Fixed size portion of the address range info.  */
5087 #define DWARF_ARANGES_HEADER_SIZE                                       \
5088   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5089                 DWARF2_ADDR_SIZE * 2)                                   \
5090    - DWARF_INITIAL_LENGTH_SIZE)
5091
5092 /* Size of padding portion in the address range info.  It must be
5093    aligned to twice the pointer size.  */
5094 #define DWARF_ARANGES_PAD_SIZE \
5095   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5096                 DWARF2_ADDR_SIZE * 2)                              \
5097    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5098
5099 /* Use assembler line directives if available.  */
5100 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5101 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5102 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5103 #else
5104 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5105 #endif
5106 #endif
5107
5108 /* Minimum line offset in a special line info. opcode.
5109    This value was chosen to give a reasonable range of values.  */
5110 #define DWARF_LINE_BASE  -10
5111
5112 /* First special line opcode - leave room for the standard opcodes.  */
5113 #define DWARF_LINE_OPCODE_BASE  10
5114
5115 /* Range of line offsets in a special line info. opcode.  */
5116 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5117
5118 /* Flag that indicates the initial value of the is_stmt_start flag.
5119    In the present implementation, we do not mark any lines as
5120    the beginning of a source statement, because that information
5121    is not made available by the GCC front-end.  */
5122 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5123
5124 #ifdef DWARF2_DEBUGGING_INFO
5125 /* This location is used by calc_die_sizes() to keep track
5126    the offset of each DIE within the .debug_info section.  */
5127 static unsigned long next_die_offset;
5128 #endif
5129
5130 /* Record the root of the DIE's built for the current compilation unit.  */
5131 static GTY(()) dw_die_ref comp_unit_die;
5132
5133 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5134 static GTY(()) limbo_die_node *limbo_die_list;
5135
5136 /* Filenames referenced by this compilation unit.  */
5137 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5138
5139 /* A hash table of references to DIE's that describe declarations.
5140    The key is a DECL_UID() which is a unique number identifying each decl.  */
5141 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5142
5143 /* A hash table of references to DIE's that describe COMMON blocks.
5144    The key is DECL_UID() ^ die_parent.  */
5145 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5146
5147 /* Node of the variable location list.  */
5148 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5149   rtx GTY (()) var_loc_note;
5150   const char * GTY (()) label;
5151   const char * GTY (()) section_label;
5152   struct var_loc_node * GTY (()) next;
5153 };
5154
5155 /* Variable location list.  */
5156 struct GTY (()) var_loc_list_def {
5157   struct var_loc_node * GTY (()) first;
5158
5159   /* Do not mark the last element of the chained list because
5160      it is marked through the chain.  */
5161   struct var_loc_node * GTY ((skip ("%h"))) last;
5162
5163   /* DECL_UID of the variable decl.  */
5164   unsigned int decl_id;
5165 };
5166 typedef struct var_loc_list_def var_loc_list;
5167
5168
5169 /* Table of decl location linked lists.  */
5170 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5171
5172 /* A pointer to the base of a list of references to DIE's that
5173    are uniquely identified by their tag, presence/absence of
5174    children DIE's, and list of attribute/value pairs.  */
5175 static GTY((length ("abbrev_die_table_allocated")))
5176   dw_die_ref *abbrev_die_table;
5177
5178 /* Number of elements currently allocated for abbrev_die_table.  */
5179 static GTY(()) unsigned abbrev_die_table_allocated;
5180
5181 /* Number of elements in type_die_table currently in use.  */
5182 static GTY(()) unsigned abbrev_die_table_in_use;
5183
5184 /* Size (in elements) of increments by which we may expand the
5185    abbrev_die_table.  */
5186 #define ABBREV_DIE_TABLE_INCREMENT 256
5187
5188 /* A pointer to the base of a table that contains line information
5189    for each source code line in .text in the compilation unit.  */
5190 static GTY((length ("line_info_table_allocated")))
5191      dw_line_info_ref line_info_table;
5192
5193 /* Number of elements currently allocated for line_info_table.  */
5194 static GTY(()) unsigned line_info_table_allocated;
5195
5196 /* Number of elements in line_info_table currently in use.  */
5197 static GTY(()) unsigned line_info_table_in_use;
5198
5199 /* A pointer to the base of a table that contains line information
5200    for each source code line outside of .text in the compilation unit.  */
5201 static GTY ((length ("separate_line_info_table_allocated")))
5202      dw_separate_line_info_ref separate_line_info_table;
5203
5204 /* Number of elements currently allocated for separate_line_info_table.  */
5205 static GTY(()) unsigned separate_line_info_table_allocated;
5206
5207 /* Number of elements in separate_line_info_table currently in use.  */
5208 static GTY(()) unsigned separate_line_info_table_in_use;
5209
5210 /* Size (in elements) of increments by which we may expand the
5211    line_info_table.  */
5212 #define LINE_INFO_TABLE_INCREMENT 1024
5213
5214 /* A pointer to the base of a table that contains a list of publicly
5215    accessible names.  */
5216 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5217
5218 /* A pointer to the base of a table that contains a list of publicly
5219    accessible types.  */
5220 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5221
5222 /* Array of dies for which we should generate .debug_arange info.  */
5223 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5224
5225 /* Number of elements currently allocated for arange_table.  */
5226 static GTY(()) unsigned arange_table_allocated;
5227
5228 /* Number of elements in arange_table currently in use.  */
5229 static GTY(()) unsigned arange_table_in_use;
5230
5231 /* Size (in elements) of increments by which we may expand the
5232    arange_table.  */
5233 #define ARANGE_TABLE_INCREMENT 64
5234
5235 /* Array of dies for which we should generate .debug_ranges info.  */
5236 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5237
5238 /* Number of elements currently allocated for ranges_table.  */
5239 static GTY(()) unsigned ranges_table_allocated;
5240
5241 /* Number of elements in ranges_table currently in use.  */
5242 static GTY(()) unsigned ranges_table_in_use;
5243
5244 /* Array of pairs of labels referenced in ranges_table.  */
5245 static GTY ((length ("ranges_by_label_allocated")))
5246      dw_ranges_by_label_ref ranges_by_label;
5247
5248 /* Number of elements currently allocated for ranges_by_label.  */
5249 static GTY(()) unsigned ranges_by_label_allocated;
5250
5251 /* Number of elements in ranges_by_label currently in use.  */
5252 static GTY(()) unsigned ranges_by_label_in_use;
5253
5254 /* Size (in elements) of increments by which we may expand the
5255    ranges_table.  */
5256 #define RANGES_TABLE_INCREMENT 64
5257
5258 /* Whether we have location lists that need outputting */
5259 static GTY(()) bool have_location_lists;
5260
5261 /* Unique label counter.  */
5262 static GTY(()) unsigned int loclabel_num;
5263
5264 #ifdef DWARF2_DEBUGGING_INFO
5265 /* Record whether the function being analyzed contains inlined functions.  */
5266 static int current_function_has_inlines;
5267 #endif
5268 #if 0 && defined (MIPS_DEBUGGING_INFO)
5269 static int comp_unit_has_inlines;
5270 #endif
5271
5272 /* The last file entry emitted by maybe_emit_file().  */
5273 static GTY(()) struct dwarf_file_data * last_emitted_file;
5274
5275 /* Number of internal labels generated by gen_internal_sym().  */
5276 static GTY(()) int label_num;
5277
5278 /* Cached result of previous call to lookup_filename.  */
5279 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5280
5281 #ifdef DWARF2_DEBUGGING_INFO
5282
5283 /* Offset from the "steady-state frame pointer" to the frame base,
5284    within the current function.  */
5285 static HOST_WIDE_INT frame_pointer_fb_offset;
5286
5287 /* Forward declarations for functions defined in this file.  */
5288
5289 static int is_pseudo_reg (const_rtx);
5290 static tree type_main_variant (tree);
5291 static int is_tagged_type (const_tree);
5292 static const char *dwarf_tag_name (unsigned);
5293 static const char *dwarf_attr_name (unsigned);
5294 static const char *dwarf_form_name (unsigned);
5295 static tree decl_ultimate_origin (const_tree);
5296 static tree decl_class_context (tree);
5297 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5298 static inline enum dw_val_class AT_class (dw_attr_ref);
5299 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5300 static inline unsigned AT_flag (dw_attr_ref);
5301 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5302 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5303 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5304 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5305 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
5306                               unsigned long);
5307 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5308                                unsigned int, unsigned char *);
5309 static hashval_t debug_str_do_hash (const void *);
5310 static int debug_str_eq (const void *, const void *);
5311 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5312 static inline const char *AT_string (dw_attr_ref);
5313 static enum dwarf_form AT_string_form (dw_attr_ref);
5314 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5315 static void add_AT_specification (dw_die_ref, dw_die_ref);
5316 static inline dw_die_ref AT_ref (dw_attr_ref);
5317 static inline int AT_ref_external (dw_attr_ref);
5318 static inline void set_AT_ref_external (dw_attr_ref, int);
5319 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5320 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5321 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5322 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5323                              dw_loc_list_ref);
5324 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5325 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5326 static inline rtx AT_addr (dw_attr_ref);
5327 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5328 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5329 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5330 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5331                            unsigned HOST_WIDE_INT);
5332 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5333                                unsigned long);
5334 static inline const char *AT_lbl (dw_attr_ref);
5335 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5336 static const char *get_AT_low_pc (dw_die_ref);
5337 static const char *get_AT_hi_pc (dw_die_ref);
5338 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5339 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5340 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5341 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5342 static bool is_c_family (void);
5343 static bool is_cxx (void);
5344 static bool is_java (void);
5345 static bool is_fortran (void);
5346 static bool is_ada (void);
5347 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5348 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5349 static void add_child_die (dw_die_ref, dw_die_ref);
5350 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5351 static dw_die_ref lookup_type_die (tree);
5352 static void equate_type_number_to_die (tree, dw_die_ref);
5353 static hashval_t decl_die_table_hash (const void *);
5354 static int decl_die_table_eq (const void *, const void *);
5355 static dw_die_ref lookup_decl_die (tree);
5356 static hashval_t common_block_die_table_hash (const void *);
5357 static int common_block_die_table_eq (const void *, const void *);
5358 static hashval_t decl_loc_table_hash (const void *);
5359 static int decl_loc_table_eq (const void *, const void *);
5360 static var_loc_list *lookup_decl_loc (const_tree);
5361 static void equate_decl_number_to_die (tree, dw_die_ref);
5362 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5363 static void print_spaces (FILE *);
5364 static void print_die (dw_die_ref, FILE *);
5365 static void print_dwarf_line_table (FILE *);
5366 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5367 static dw_die_ref pop_compile_unit (dw_die_ref);
5368 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5369 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5370 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5371 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5372 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5373 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5374 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5375 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5376 static void compute_section_prefix (dw_die_ref);
5377 static int is_type_die (dw_die_ref);
5378 static int is_comdat_die (dw_die_ref);
5379 static int is_symbol_die (dw_die_ref);
5380 static void assign_symbol_names (dw_die_ref);
5381 static void break_out_includes (dw_die_ref);
5382 static hashval_t htab_cu_hash (const void *);
5383 static int htab_cu_eq (const void *, const void *);
5384 static void htab_cu_del (void *);
5385 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5386 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5387 static void add_sibling_attributes (dw_die_ref);
5388 static void build_abbrev_table (dw_die_ref);
5389 static void output_location_lists (dw_die_ref);
5390 static int constant_size (unsigned HOST_WIDE_INT);
5391 static unsigned long size_of_die (dw_die_ref);
5392 static void calc_die_sizes (dw_die_ref);
5393 static void mark_dies (dw_die_ref);
5394 static void unmark_dies (dw_die_ref);
5395 static void unmark_all_dies (dw_die_ref);
5396 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5397 static unsigned long size_of_aranges (void);
5398 static enum dwarf_form value_format (dw_attr_ref);
5399 static void output_value_format (dw_attr_ref);
5400 static void output_abbrev_section (void);
5401 static void output_die_symbol (dw_die_ref);
5402 static void output_die (dw_die_ref);
5403 static void output_compilation_unit_header (void);
5404 static void output_comp_unit (dw_die_ref, int);
5405 static const char *dwarf2_name (tree, int);
5406 static void add_pubname (tree, dw_die_ref);
5407 static void add_pubname_string (const char *, dw_die_ref);
5408 static void add_pubtype (tree, dw_die_ref);
5409 static void output_pubnames (VEC (pubname_entry,gc) *);
5410 static void add_arange (tree, dw_die_ref);
5411 static void output_aranges (void);
5412 static unsigned int add_ranges_num (int);
5413 static unsigned int add_ranges (const_tree);
5414 static unsigned int add_ranges_by_labels (const char *, const char *);
5415 static void output_ranges (void);
5416 static void output_line_info (void);
5417 static void output_file_names (void);
5418 static dw_die_ref base_type_die (tree);
5419 static int is_base_type (tree);
5420 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5421 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5422 static int type_is_enum (const_tree);
5423 static unsigned int dbx_reg_number (const_rtx);
5424 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5425 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5426 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5427                                                 enum var_init_status);
5428 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5429                                                      enum var_init_status);
5430 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5431                                          enum var_init_status);
5432 static int is_based_loc (const_rtx);
5433 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5434                                             enum var_init_status);
5435 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5436                                                enum var_init_status);
5437 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
5438 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5439 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5440 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5441 static tree field_type (const_tree);
5442 static unsigned int simple_type_align_in_bits (const_tree);
5443 static unsigned int simple_decl_align_in_bits (const_tree);
5444 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5445 static HOST_WIDE_INT field_byte_offset (const_tree);
5446 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5447                                          dw_loc_descr_ref);
5448 static void add_data_member_location_attribute (dw_die_ref, tree);
5449 static void add_const_value_attribute (dw_die_ref, rtx);
5450 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5451 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5452 static void insert_float (const_rtx, unsigned char *);
5453 static rtx rtl_for_decl_location (tree);
5454 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5455                                                    enum dwarf_attribute);
5456 static void tree_add_const_value_attribute (dw_die_ref, tree);
5457 static void add_name_attribute (dw_die_ref, const char *);
5458 static void add_comp_dir_attribute (dw_die_ref);
5459 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5460 static void add_subscript_info (dw_die_ref, tree, bool);
5461 static void add_byte_size_attribute (dw_die_ref, tree);
5462 static void add_bit_offset_attribute (dw_die_ref, tree);
5463 static void add_bit_size_attribute (dw_die_ref, tree);
5464 static void add_prototyped_attribute (dw_die_ref, tree);
5465 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5466 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5467 static void add_src_coords_attributes (dw_die_ref, tree);
5468 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5469 static void push_decl_scope (tree);
5470 static void pop_decl_scope (void);
5471 static dw_die_ref scope_die_for (tree, dw_die_ref);
5472 static inline int local_scope_p (dw_die_ref);
5473 static inline int class_scope_p (dw_die_ref);
5474 static inline int class_or_namespace_scope_p (dw_die_ref);
5475 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5476 static void add_calling_convention_attribute (dw_die_ref, tree);
5477 static const char *type_tag (const_tree);
5478 static tree member_declared_type (const_tree);
5479 #if 0
5480 static const char *decl_start_label (tree);
5481 #endif
5482 static void gen_array_type_die (tree, dw_die_ref);
5483 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5484 #if 0
5485 static void gen_entry_point_die (tree, dw_die_ref);
5486 #endif
5487 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5488 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5489 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5490 static void gen_formal_types_die (tree, dw_die_ref);
5491 static void gen_subprogram_die (tree, dw_die_ref);
5492 static void gen_variable_die (tree, tree, dw_die_ref);
5493 static void gen_const_die (tree, dw_die_ref);
5494 static void gen_label_die (tree, dw_die_ref);
5495 static void gen_lexical_block_die (tree, dw_die_ref, int);
5496 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5497 static void gen_field_die (tree, dw_die_ref);
5498 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5499 static dw_die_ref gen_compile_unit_die (const char *);
5500 static void gen_inheritance_die (tree, tree, dw_die_ref);
5501 static void gen_member_die (tree, dw_die_ref);
5502 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5503                                                 enum debug_info_usage);
5504 static void gen_subroutine_type_die (tree, dw_die_ref);
5505 static void gen_typedef_die (tree, dw_die_ref);
5506 static void gen_type_die (tree, dw_die_ref);
5507 static void gen_block_die (tree, dw_die_ref, int);
5508 static void decls_for_scope (tree, dw_die_ref, int);
5509 static int is_redundant_typedef (const_tree);
5510 static void gen_namespace_die (tree, dw_die_ref);
5511 static void gen_decl_die (tree, tree, dw_die_ref);
5512 static dw_die_ref force_decl_die (tree);
5513 static dw_die_ref force_type_die (tree);
5514 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5515 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5516 static struct dwarf_file_data * lookup_filename (const char *);
5517 static void retry_incomplete_types (void);
5518 static void gen_type_die_for_member (tree, tree, dw_die_ref);
5519 static void splice_child_die (dw_die_ref, dw_die_ref);
5520 static int file_info_cmp (const void *, const void *);
5521 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
5522                                      const char *, const char *, unsigned);
5523 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
5524                                        const char *, const char *,
5525                                        const char *);
5526 static void output_loc_list (dw_loc_list_ref);
5527 static char *gen_internal_sym (const char *);
5528
5529 static void prune_unmark_dies (dw_die_ref);
5530 static void prune_unused_types_mark (dw_die_ref, int);
5531 static void prune_unused_types_walk (dw_die_ref);
5532 static void prune_unused_types_walk_attribs (dw_die_ref);
5533 static void prune_unused_types_prune (dw_die_ref);
5534 static void prune_unused_types (void);
5535 static int maybe_emit_file (struct dwarf_file_data *fd);
5536
5537 /* Section names used to hold DWARF debugging information.  */
5538 #ifndef DEBUG_INFO_SECTION
5539 #define DEBUG_INFO_SECTION      ".debug_info"
5540 #endif
5541 #ifndef DEBUG_ABBREV_SECTION
5542 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
5543 #endif
5544 #ifndef DEBUG_ARANGES_SECTION
5545 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
5546 #endif
5547 #ifndef DEBUG_MACINFO_SECTION
5548 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
5549 #endif
5550 #ifndef DEBUG_LINE_SECTION
5551 #define DEBUG_LINE_SECTION      ".debug_line"
5552 #endif
5553 #ifndef DEBUG_LOC_SECTION
5554 #define DEBUG_LOC_SECTION       ".debug_loc"
5555 #endif
5556 #ifndef DEBUG_PUBNAMES_SECTION
5557 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
5558 #endif
5559 #ifndef DEBUG_STR_SECTION
5560 #define DEBUG_STR_SECTION       ".debug_str"
5561 #endif
5562 #ifndef DEBUG_RANGES_SECTION
5563 #define DEBUG_RANGES_SECTION    ".debug_ranges"
5564 #endif
5565
5566 /* Standard ELF section names for compiled code and data.  */
5567 #ifndef TEXT_SECTION_NAME
5568 #define TEXT_SECTION_NAME       ".text"
5569 #endif
5570
5571 /* Section flags for .debug_str section.  */
5572 #define DEBUG_STR_SECTION_FLAGS \
5573   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
5574    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
5575    : SECTION_DEBUG)
5576
5577 /* Labels we insert at beginning sections we can reference instead of
5578    the section names themselves.  */
5579
5580 #ifndef TEXT_SECTION_LABEL
5581 #define TEXT_SECTION_LABEL              "Ltext"
5582 #endif
5583 #ifndef COLD_TEXT_SECTION_LABEL
5584 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
5585 #endif
5586 #ifndef DEBUG_LINE_SECTION_LABEL
5587 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
5588 #endif
5589 #ifndef DEBUG_INFO_SECTION_LABEL
5590 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
5591 #endif
5592 #ifndef DEBUG_ABBREV_SECTION_LABEL
5593 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
5594 #endif
5595 #ifndef DEBUG_LOC_SECTION_LABEL
5596 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
5597 #endif
5598 #ifndef DEBUG_RANGES_SECTION_LABEL
5599 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
5600 #endif
5601 #ifndef DEBUG_MACINFO_SECTION_LABEL
5602 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
5603 #endif
5604
5605 /* Definitions of defaults for formats and names of various special
5606    (artificial) labels which may be generated within this file (when the -g
5607    options is used and DWARF2_DEBUGGING_INFO is in effect.
5608    If necessary, these may be overridden from within the tm.h file, but
5609    typically, overriding these defaults is unnecessary.  */
5610
5611 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5612 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5613 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5614 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
5615 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5616 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5617 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5618 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5619 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
5620 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
5621
5622 #ifndef TEXT_END_LABEL
5623 #define TEXT_END_LABEL          "Letext"
5624 #endif
5625 #ifndef COLD_END_LABEL
5626 #define COLD_END_LABEL          "Letext_cold"
5627 #endif
5628 #ifndef BLOCK_BEGIN_LABEL
5629 #define BLOCK_BEGIN_LABEL       "LBB"
5630 #endif
5631 #ifndef BLOCK_END_LABEL
5632 #define BLOCK_END_LABEL         "LBE"
5633 #endif
5634 #ifndef LINE_CODE_LABEL
5635 #define LINE_CODE_LABEL         "LM"
5636 #endif
5637 #ifndef SEPARATE_LINE_CODE_LABEL
5638 #define SEPARATE_LINE_CODE_LABEL        "LSM"
5639 #endif
5640
5641 \f
5642 /* We allow a language front-end to designate a function that is to be
5643    called to "demangle" any name before it is put into a DIE.  */
5644
5645 static const char *(*demangle_name_func) (const char *);
5646
5647 void
5648 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
5649 {
5650   demangle_name_func = func;
5651 }
5652
5653 /* Test if rtl node points to a pseudo register.  */
5654
5655 static inline int
5656 is_pseudo_reg (const_rtx rtl)
5657 {
5658   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
5659           || (GET_CODE (rtl) == SUBREG
5660               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
5661 }
5662
5663 /* Return a reference to a type, with its const and volatile qualifiers
5664    removed.  */
5665
5666 static inline tree
5667 type_main_variant (tree type)
5668 {
5669   type = TYPE_MAIN_VARIANT (type);
5670
5671   /* ??? There really should be only one main variant among any group of
5672      variants of a given type (and all of the MAIN_VARIANT values for all
5673      members of the group should point to that one type) but sometimes the C
5674      front-end messes this up for array types, so we work around that bug
5675      here.  */
5676   if (TREE_CODE (type) == ARRAY_TYPE)
5677     while (type != TYPE_MAIN_VARIANT (type))
5678       type = TYPE_MAIN_VARIANT (type);
5679
5680   return type;
5681 }
5682
5683 /* Return nonzero if the given type node represents a tagged type.  */
5684
5685 static inline int
5686 is_tagged_type (const_tree type)
5687 {
5688   enum tree_code code = TREE_CODE (type);
5689
5690   return (code == RECORD_TYPE || code == UNION_TYPE
5691           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
5692 }
5693
5694 /* Convert a DIE tag into its string name.  */
5695
5696 static const char *
5697 dwarf_tag_name (unsigned int tag)
5698 {
5699   switch (tag)
5700     {
5701     case DW_TAG_padding:
5702       return "DW_TAG_padding";
5703     case DW_TAG_array_type:
5704       return "DW_TAG_array_type";
5705     case DW_TAG_class_type:
5706       return "DW_TAG_class_type";
5707     case DW_TAG_entry_point:
5708       return "DW_TAG_entry_point";
5709     case DW_TAG_enumeration_type:
5710       return "DW_TAG_enumeration_type";
5711     case DW_TAG_formal_parameter:
5712       return "DW_TAG_formal_parameter";
5713     case DW_TAG_imported_declaration:
5714       return "DW_TAG_imported_declaration";
5715     case DW_TAG_label:
5716       return "DW_TAG_label";
5717     case DW_TAG_lexical_block:
5718       return "DW_TAG_lexical_block";
5719     case DW_TAG_member:
5720       return "DW_TAG_member";
5721     case DW_TAG_pointer_type:
5722       return "DW_TAG_pointer_type";
5723     case DW_TAG_reference_type:
5724       return "DW_TAG_reference_type";
5725     case DW_TAG_compile_unit:
5726       return "DW_TAG_compile_unit";
5727     case DW_TAG_string_type:
5728       return "DW_TAG_string_type";
5729     case DW_TAG_structure_type:
5730       return "DW_TAG_structure_type";
5731     case DW_TAG_subroutine_type:
5732       return "DW_TAG_subroutine_type";
5733     case DW_TAG_typedef:
5734       return "DW_TAG_typedef";
5735     case DW_TAG_union_type:
5736       return "DW_TAG_union_type";
5737     case DW_TAG_unspecified_parameters:
5738       return "DW_TAG_unspecified_parameters";
5739     case DW_TAG_variant:
5740       return "DW_TAG_variant";
5741     case DW_TAG_common_block:
5742       return "DW_TAG_common_block";
5743     case DW_TAG_common_inclusion:
5744       return "DW_TAG_common_inclusion";
5745     case DW_TAG_inheritance:
5746       return "DW_TAG_inheritance";
5747     case DW_TAG_inlined_subroutine:
5748       return "DW_TAG_inlined_subroutine";
5749     case DW_TAG_module:
5750       return "DW_TAG_module";
5751     case DW_TAG_ptr_to_member_type:
5752       return "DW_TAG_ptr_to_member_type";
5753     case DW_TAG_set_type:
5754       return "DW_TAG_set_type";
5755     case DW_TAG_subrange_type:
5756       return "DW_TAG_subrange_type";
5757     case DW_TAG_with_stmt:
5758       return "DW_TAG_with_stmt";
5759     case DW_TAG_access_declaration:
5760       return "DW_TAG_access_declaration";
5761     case DW_TAG_base_type:
5762       return "DW_TAG_base_type";
5763     case DW_TAG_catch_block:
5764       return "DW_TAG_catch_block";
5765     case DW_TAG_const_type:
5766       return "DW_TAG_const_type";
5767     case DW_TAG_constant:
5768       return "DW_TAG_constant";
5769     case DW_TAG_enumerator:
5770       return "DW_TAG_enumerator";
5771     case DW_TAG_file_type:
5772       return "DW_TAG_file_type";
5773     case DW_TAG_friend:
5774       return "DW_TAG_friend";
5775     case DW_TAG_namelist:
5776       return "DW_TAG_namelist";
5777     case DW_TAG_namelist_item:
5778       return "DW_TAG_namelist_item";
5779     case DW_TAG_packed_type:
5780       return "DW_TAG_packed_type";
5781     case DW_TAG_subprogram:
5782       return "DW_TAG_subprogram";
5783     case DW_TAG_template_type_param:
5784       return "DW_TAG_template_type_param";
5785     case DW_TAG_template_value_param:
5786       return "DW_TAG_template_value_param";
5787     case DW_TAG_thrown_type:
5788       return "DW_TAG_thrown_type";
5789     case DW_TAG_try_block:
5790       return "DW_TAG_try_block";
5791     case DW_TAG_variant_part:
5792       return "DW_TAG_variant_part";
5793     case DW_TAG_variable:
5794       return "DW_TAG_variable";
5795     case DW_TAG_volatile_type:
5796       return "DW_TAG_volatile_type";
5797     case DW_TAG_dwarf_procedure:
5798       return "DW_TAG_dwarf_procedure";
5799     case DW_TAG_restrict_type:
5800       return "DW_TAG_restrict_type";
5801     case DW_TAG_interface_type:
5802       return "DW_TAG_interface_type";
5803     case DW_TAG_namespace:
5804       return "DW_TAG_namespace";
5805     case DW_TAG_imported_module:
5806       return "DW_TAG_imported_module";
5807     case DW_TAG_unspecified_type:
5808       return "DW_TAG_unspecified_type";
5809     case DW_TAG_partial_unit:
5810       return "DW_TAG_partial_unit";
5811     case DW_TAG_imported_unit:
5812       return "DW_TAG_imported_unit";
5813     case DW_TAG_condition:
5814       return "DW_TAG_condition";
5815     case DW_TAG_shared_type:
5816       return "DW_TAG_shared_type";
5817     case DW_TAG_MIPS_loop:
5818       return "DW_TAG_MIPS_loop";
5819     case DW_TAG_format_label:
5820       return "DW_TAG_format_label";
5821     case DW_TAG_function_template:
5822       return "DW_TAG_function_template";
5823     case DW_TAG_class_template:
5824       return "DW_TAG_class_template";
5825     case DW_TAG_GNU_BINCL:
5826       return "DW_TAG_GNU_BINCL";
5827     case DW_TAG_GNU_EINCL:
5828       return "DW_TAG_GNU_EINCL";
5829     default:
5830       return "DW_TAG_<unknown>";
5831     }
5832 }
5833
5834 /* Convert a DWARF attribute code into its string name.  */
5835
5836 static const char *
5837 dwarf_attr_name (unsigned int attr)
5838 {
5839   switch (attr)
5840     {
5841     case DW_AT_sibling:
5842       return "DW_AT_sibling";
5843     case DW_AT_location:
5844       return "DW_AT_location";
5845     case DW_AT_name:
5846       return "DW_AT_name";
5847     case DW_AT_ordering:
5848       return "DW_AT_ordering";
5849     case DW_AT_subscr_data:
5850       return "DW_AT_subscr_data";
5851     case DW_AT_byte_size:
5852       return "DW_AT_byte_size";
5853     case DW_AT_bit_offset:
5854       return "DW_AT_bit_offset";
5855     case DW_AT_bit_size:
5856       return "DW_AT_bit_size";
5857     case DW_AT_element_list:
5858       return "DW_AT_element_list";
5859     case DW_AT_stmt_list:
5860       return "DW_AT_stmt_list";
5861     case DW_AT_low_pc:
5862       return "DW_AT_low_pc";
5863     case DW_AT_high_pc:
5864       return "DW_AT_high_pc";
5865     case DW_AT_language:
5866       return "DW_AT_language";
5867     case DW_AT_member:
5868       return "DW_AT_member";
5869     case DW_AT_discr:
5870       return "DW_AT_discr";
5871     case DW_AT_discr_value:
5872       return "DW_AT_discr_value";
5873     case DW_AT_visibility:
5874       return "DW_AT_visibility";
5875     case DW_AT_import:
5876       return "DW_AT_import";
5877     case DW_AT_string_length:
5878       return "DW_AT_string_length";
5879     case DW_AT_common_reference:
5880       return "DW_AT_common_reference";
5881     case DW_AT_comp_dir:
5882       return "DW_AT_comp_dir";
5883     case DW_AT_const_value:
5884       return "DW_AT_const_value";
5885     case DW_AT_containing_type:
5886       return "DW_AT_containing_type";
5887     case DW_AT_default_value:
5888       return "DW_AT_default_value";
5889     case DW_AT_inline:
5890       return "DW_AT_inline";
5891     case DW_AT_is_optional:
5892       return "DW_AT_is_optional";
5893     case DW_AT_lower_bound:
5894       return "DW_AT_lower_bound";
5895     case DW_AT_producer:
5896       return "DW_AT_producer";
5897     case DW_AT_prototyped:
5898       return "DW_AT_prototyped";
5899     case DW_AT_return_addr:
5900       return "DW_AT_return_addr";
5901     case DW_AT_start_scope:
5902       return "DW_AT_start_scope";
5903     case DW_AT_bit_stride:
5904       return "DW_AT_bit_stride";
5905     case DW_AT_upper_bound:
5906       return "DW_AT_upper_bound";
5907     case DW_AT_abstract_origin:
5908       return "DW_AT_abstract_origin";
5909     case DW_AT_accessibility:
5910       return "DW_AT_accessibility";
5911     case DW_AT_address_class:
5912       return "DW_AT_address_class";
5913     case DW_AT_artificial:
5914       return "DW_AT_artificial";
5915     case DW_AT_base_types:
5916       return "DW_AT_base_types";
5917     case DW_AT_calling_convention:
5918       return "DW_AT_calling_convention";
5919     case DW_AT_count:
5920       return "DW_AT_count";
5921     case DW_AT_data_member_location:
5922       return "DW_AT_data_member_location";
5923     case DW_AT_decl_column:
5924       return "DW_AT_decl_column";
5925     case DW_AT_decl_file:
5926       return "DW_AT_decl_file";
5927     case DW_AT_decl_line:
5928       return "DW_AT_decl_line";
5929     case DW_AT_declaration:
5930       return "DW_AT_declaration";
5931     case DW_AT_discr_list:
5932       return "DW_AT_discr_list";
5933     case DW_AT_encoding:
5934       return "DW_AT_encoding";
5935     case DW_AT_external:
5936       return "DW_AT_external";
5937     case DW_AT_explicit:
5938       return "DW_AT_explicit";
5939     case DW_AT_frame_base:
5940       return "DW_AT_frame_base";
5941     case DW_AT_friend:
5942       return "DW_AT_friend";
5943     case DW_AT_identifier_case:
5944       return "DW_AT_identifier_case";
5945     case DW_AT_macro_info:
5946       return "DW_AT_macro_info";
5947     case DW_AT_namelist_items:
5948       return "DW_AT_namelist_items";
5949     case DW_AT_priority:
5950       return "DW_AT_priority";
5951     case DW_AT_segment:
5952       return "DW_AT_segment";
5953     case DW_AT_specification:
5954       return "DW_AT_specification";
5955     case DW_AT_static_link:
5956       return "DW_AT_static_link";
5957     case DW_AT_type:
5958       return "DW_AT_type";
5959     case DW_AT_use_location:
5960       return "DW_AT_use_location";
5961     case DW_AT_variable_parameter:
5962       return "DW_AT_variable_parameter";
5963     case DW_AT_virtuality:
5964       return "DW_AT_virtuality";
5965     case DW_AT_vtable_elem_location:
5966       return "DW_AT_vtable_elem_location";
5967
5968     case DW_AT_allocated:
5969       return "DW_AT_allocated";
5970     case DW_AT_associated:
5971       return "DW_AT_associated";
5972     case DW_AT_data_location:
5973       return "DW_AT_data_location";
5974     case DW_AT_byte_stride:
5975       return "DW_AT_byte_stride";
5976     case DW_AT_entry_pc:
5977       return "DW_AT_entry_pc";
5978     case DW_AT_use_UTF8:
5979       return "DW_AT_use_UTF8";
5980     case DW_AT_extension:
5981       return "DW_AT_extension";
5982     case DW_AT_ranges:
5983       return "DW_AT_ranges";
5984     case DW_AT_trampoline:
5985       return "DW_AT_trampoline";
5986     case DW_AT_call_column:
5987       return "DW_AT_call_column";
5988     case DW_AT_call_file:
5989       return "DW_AT_call_file";
5990     case DW_AT_call_line:
5991       return "DW_AT_call_line";
5992
5993     case DW_AT_MIPS_fde:
5994       return "DW_AT_MIPS_fde";
5995     case DW_AT_MIPS_loop_begin:
5996       return "DW_AT_MIPS_loop_begin";
5997     case DW_AT_MIPS_tail_loop_begin:
5998       return "DW_AT_MIPS_tail_loop_begin";
5999     case DW_AT_MIPS_epilog_begin:
6000       return "DW_AT_MIPS_epilog_begin";
6001     case DW_AT_MIPS_loop_unroll_factor:
6002       return "DW_AT_MIPS_loop_unroll_factor";
6003     case DW_AT_MIPS_software_pipeline_depth:
6004       return "DW_AT_MIPS_software_pipeline_depth";
6005     case DW_AT_MIPS_linkage_name:
6006       return "DW_AT_MIPS_linkage_name";
6007     case DW_AT_MIPS_stride:
6008       return "DW_AT_MIPS_stride";
6009     case DW_AT_MIPS_abstract_name:
6010       return "DW_AT_MIPS_abstract_name";
6011     case DW_AT_MIPS_clone_origin:
6012       return "DW_AT_MIPS_clone_origin";
6013     case DW_AT_MIPS_has_inlines:
6014       return "DW_AT_MIPS_has_inlines";
6015
6016     case DW_AT_sf_names:
6017       return "DW_AT_sf_names";
6018     case DW_AT_src_info:
6019       return "DW_AT_src_info";
6020     case DW_AT_mac_info:
6021       return "DW_AT_mac_info";
6022     case DW_AT_src_coords:
6023       return "DW_AT_src_coords";
6024     case DW_AT_body_begin:
6025       return "DW_AT_body_begin";
6026     case DW_AT_body_end:
6027       return "DW_AT_body_end";
6028     case DW_AT_GNU_vector:
6029       return "DW_AT_GNU_vector";
6030
6031     case DW_AT_VMS_rtnbeg_pd_address:
6032       return "DW_AT_VMS_rtnbeg_pd_address";
6033
6034     default:
6035       return "DW_AT_<unknown>";
6036     }
6037 }
6038
6039 /* Convert a DWARF value form code into its string name.  */
6040
6041 static const char *
6042 dwarf_form_name (unsigned int form)
6043 {
6044   switch (form)
6045     {
6046     case DW_FORM_addr:
6047       return "DW_FORM_addr";
6048     case DW_FORM_block2:
6049       return "DW_FORM_block2";
6050     case DW_FORM_block4:
6051       return "DW_FORM_block4";
6052     case DW_FORM_data2:
6053       return "DW_FORM_data2";
6054     case DW_FORM_data4:
6055       return "DW_FORM_data4";
6056     case DW_FORM_data8:
6057       return "DW_FORM_data8";
6058     case DW_FORM_string:
6059       return "DW_FORM_string";
6060     case DW_FORM_block:
6061       return "DW_FORM_block";
6062     case DW_FORM_block1:
6063       return "DW_FORM_block1";
6064     case DW_FORM_data1:
6065       return "DW_FORM_data1";
6066     case DW_FORM_flag:
6067       return "DW_FORM_flag";
6068     case DW_FORM_sdata:
6069       return "DW_FORM_sdata";
6070     case DW_FORM_strp:
6071       return "DW_FORM_strp";
6072     case DW_FORM_udata:
6073       return "DW_FORM_udata";
6074     case DW_FORM_ref_addr:
6075       return "DW_FORM_ref_addr";
6076     case DW_FORM_ref1:
6077       return "DW_FORM_ref1";
6078     case DW_FORM_ref2:
6079       return "DW_FORM_ref2";
6080     case DW_FORM_ref4:
6081       return "DW_FORM_ref4";
6082     case DW_FORM_ref8:
6083       return "DW_FORM_ref8";
6084     case DW_FORM_ref_udata:
6085       return "DW_FORM_ref_udata";
6086     case DW_FORM_indirect:
6087       return "DW_FORM_indirect";
6088     default:
6089       return "DW_FORM_<unknown>";
6090     }
6091 }
6092 \f
6093 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6094    instance of an inlined instance of a decl which is local to an inline
6095    function, so we have to trace all of the way back through the origin chain
6096    to find out what sort of node actually served as the original seed for the
6097    given block.  */
6098
6099 static tree
6100 decl_ultimate_origin (const_tree decl)
6101 {
6102   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6103     return NULL_TREE;
6104
6105   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6106      nodes in the function to point to themselves; ignore that if
6107      we're trying to output the abstract instance of this function.  */
6108   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6109     return NULL_TREE;
6110
6111   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6112      most distant ancestor, this should never happen.  */
6113   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6114
6115   return DECL_ABSTRACT_ORIGIN (decl);
6116 }
6117
6118 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6119    of a virtual function may refer to a base class, so we check the 'this'
6120    parameter.  */
6121
6122 static tree
6123 decl_class_context (tree decl)
6124 {
6125   tree context = NULL_TREE;
6126
6127   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6128     context = DECL_CONTEXT (decl);
6129   else
6130     context = TYPE_MAIN_VARIANT
6131       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6132
6133   if (context && !TYPE_P (context))
6134     context = NULL_TREE;
6135
6136   return context;
6137 }
6138 \f
6139 /* Add an attribute/value pair to a DIE.  */
6140
6141 static inline void
6142 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6143 {
6144   /* Maybe this should be an assert?  */
6145   if (die == NULL)
6146     return;
6147
6148   if (die->die_attr == NULL)
6149     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6150   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6151 }
6152
6153 static inline enum dw_val_class
6154 AT_class (dw_attr_ref a)
6155 {
6156   return a->dw_attr_val.val_class;
6157 }
6158
6159 /* Add a flag value attribute to a DIE.  */
6160
6161 static inline void
6162 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6163 {
6164   dw_attr_node attr;
6165
6166   attr.dw_attr = attr_kind;
6167   attr.dw_attr_val.val_class = dw_val_class_flag;
6168   attr.dw_attr_val.v.val_flag = flag;
6169   add_dwarf_attr (die, &attr);
6170 }
6171
6172 static inline unsigned
6173 AT_flag (dw_attr_ref a)
6174 {
6175   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6176   return a->dw_attr_val.v.val_flag;
6177 }
6178
6179 /* Add a signed integer attribute value to a DIE.  */
6180
6181 static inline void
6182 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6183 {
6184   dw_attr_node attr;
6185
6186   attr.dw_attr = attr_kind;
6187   attr.dw_attr_val.val_class = dw_val_class_const;
6188   attr.dw_attr_val.v.val_int = int_val;
6189   add_dwarf_attr (die, &attr);
6190 }
6191
6192 static inline HOST_WIDE_INT
6193 AT_int (dw_attr_ref a)
6194 {
6195   gcc_assert (a && AT_class (a) == dw_val_class_const);
6196   return a->dw_attr_val.v.val_int;
6197 }
6198
6199 /* Add an unsigned integer attribute value to a DIE.  */
6200
6201 static inline void
6202 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6203                  unsigned HOST_WIDE_INT unsigned_val)
6204 {
6205   dw_attr_node attr;
6206
6207   attr.dw_attr = attr_kind;
6208   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6209   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6210   add_dwarf_attr (die, &attr);
6211 }
6212
6213 static inline unsigned HOST_WIDE_INT
6214 AT_unsigned (dw_attr_ref a)
6215 {
6216   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6217   return a->dw_attr_val.v.val_unsigned;
6218 }
6219
6220 /* Add an unsigned double integer attribute value to a DIE.  */
6221
6222 static inline void
6223 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6224                   long unsigned int val_hi, long unsigned int val_low)
6225 {
6226   dw_attr_node attr;
6227
6228   attr.dw_attr = attr_kind;
6229   attr.dw_attr_val.val_class = dw_val_class_long_long;
6230   attr.dw_attr_val.v.val_long_long.hi = val_hi;
6231   attr.dw_attr_val.v.val_long_long.low = val_low;
6232   add_dwarf_attr (die, &attr);
6233 }
6234
6235 /* Add a floating point attribute value to a DIE and return it.  */
6236
6237 static inline void
6238 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6239             unsigned int length, unsigned int elt_size, unsigned char *array)
6240 {
6241   dw_attr_node attr;
6242
6243   attr.dw_attr = attr_kind;
6244   attr.dw_attr_val.val_class = dw_val_class_vec;
6245   attr.dw_attr_val.v.val_vec.length = length;
6246   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6247   attr.dw_attr_val.v.val_vec.array = array;
6248   add_dwarf_attr (die, &attr);
6249 }
6250
6251 /* Hash and equality functions for debug_str_hash.  */
6252
6253 static hashval_t
6254 debug_str_do_hash (const void *x)
6255 {
6256   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6257 }
6258
6259 static int
6260 debug_str_eq (const void *x1, const void *x2)
6261 {
6262   return strcmp ((((const struct indirect_string_node *)x1)->str),
6263                  (const char *)x2) == 0;
6264 }
6265
6266 static struct indirect_string_node *
6267 find_AT_string (const char *str)
6268 {
6269   struct indirect_string_node *node;
6270   void **slot;
6271
6272   if (! debug_str_hash)
6273     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6274                                       debug_str_eq, NULL);
6275
6276   slot = htab_find_slot_with_hash (debug_str_hash, str,
6277                                    htab_hash_string (str), INSERT);
6278   if (*slot == NULL)
6279     {
6280       node = (struct indirect_string_node *)
6281                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6282       node->str = ggc_strdup (str);
6283       *slot = node;
6284     }
6285   else
6286     node = (struct indirect_string_node *) *slot;
6287
6288   node->refcount++;
6289   return node;
6290 }
6291
6292 /* Add a string attribute value to a DIE.  */
6293
6294 static inline void
6295 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6296 {
6297   dw_attr_node attr;
6298   struct indirect_string_node *node;
6299
6300   node = find_AT_string (str);
6301
6302   attr.dw_attr = attr_kind;
6303   attr.dw_attr_val.val_class = dw_val_class_str;
6304   attr.dw_attr_val.v.val_str = node;
6305   add_dwarf_attr (die, &attr);
6306 }
6307
6308 static inline const char *
6309 AT_string (dw_attr_ref a)
6310 {
6311   gcc_assert (a && AT_class (a) == dw_val_class_str);
6312   return a->dw_attr_val.v.val_str->str;
6313 }
6314
6315 /* Find out whether a string should be output inline in DIE
6316    or out-of-line in .debug_str section.  */
6317
6318 static enum dwarf_form
6319 AT_string_form (dw_attr_ref a)
6320 {
6321   struct indirect_string_node *node;
6322   unsigned int len;
6323   char label[32];
6324
6325   gcc_assert (a && AT_class (a) == dw_val_class_str);
6326
6327   node = a->dw_attr_val.v.val_str;
6328   if (node->form)
6329     return node->form;
6330
6331   len = strlen (node->str) + 1;
6332
6333   /* If the string is shorter or equal to the size of the reference, it is
6334      always better to put it inline.  */
6335   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6336     return node->form = DW_FORM_string;
6337
6338   /* If we cannot expect the linker to merge strings in .debug_str
6339      section, only put it into .debug_str if it is worth even in this
6340      single module.  */
6341   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
6342       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
6343     return node->form = DW_FORM_string;
6344
6345   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6346   ++dw2_string_counter;
6347   node->label = xstrdup (label);
6348
6349   return node->form = DW_FORM_strp;
6350 }
6351
6352 /* Add a DIE reference attribute value to a DIE.  */
6353
6354 static inline void
6355 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6356 {
6357   dw_attr_node attr;
6358
6359   attr.dw_attr = attr_kind;
6360   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6361   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6362   attr.dw_attr_val.v.val_die_ref.external = 0;
6363   add_dwarf_attr (die, &attr);
6364 }
6365
6366 /* Add an AT_specification attribute to a DIE, and also make the back
6367    pointer from the specification to the definition.  */
6368
6369 static inline void
6370 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6371 {
6372   add_AT_die_ref (die, DW_AT_specification, targ_die);
6373   gcc_assert (!targ_die->die_definition);
6374   targ_die->die_definition = die;
6375 }
6376
6377 static inline dw_die_ref
6378 AT_ref (dw_attr_ref a)
6379 {
6380   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6381   return a->dw_attr_val.v.val_die_ref.die;
6382 }
6383
6384 static inline int
6385 AT_ref_external (dw_attr_ref a)
6386 {
6387   if (a && AT_class (a) == dw_val_class_die_ref)
6388     return a->dw_attr_val.v.val_die_ref.external;
6389
6390   return 0;
6391 }
6392
6393 static inline void
6394 set_AT_ref_external (dw_attr_ref a, int i)
6395 {
6396   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6397   a->dw_attr_val.v.val_die_ref.external = i;
6398 }
6399
6400 /* Add an FDE reference attribute value to a DIE.  */
6401
6402 static inline void
6403 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6404 {
6405   dw_attr_node attr;
6406
6407   attr.dw_attr = attr_kind;
6408   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6409   attr.dw_attr_val.v.val_fde_index = targ_fde;
6410   add_dwarf_attr (die, &attr);
6411 }
6412
6413 /* Add a location description attribute value to a DIE.  */
6414
6415 static inline void
6416 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6417 {
6418   dw_attr_node attr;
6419
6420   attr.dw_attr = attr_kind;
6421   attr.dw_attr_val.val_class = dw_val_class_loc;
6422   attr.dw_attr_val.v.val_loc = loc;
6423   add_dwarf_attr (die, &attr);
6424 }
6425
6426 static inline dw_loc_descr_ref
6427 AT_loc (dw_attr_ref a)
6428 {
6429   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6430   return a->dw_attr_val.v.val_loc;
6431 }
6432
6433 static inline void
6434 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6435 {
6436   dw_attr_node attr;
6437
6438   attr.dw_attr = attr_kind;
6439   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6440   attr.dw_attr_val.v.val_loc_list = loc_list;
6441   add_dwarf_attr (die, &attr);
6442   have_location_lists = true;
6443 }
6444
6445 static inline dw_loc_list_ref
6446 AT_loc_list (dw_attr_ref a)
6447 {
6448   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6449   return a->dw_attr_val.v.val_loc_list;
6450 }
6451
6452 /* Add an address constant attribute value to a DIE.  */
6453
6454 static inline void
6455 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6456 {
6457   dw_attr_node attr;
6458
6459   attr.dw_attr = attr_kind;
6460   attr.dw_attr_val.val_class = dw_val_class_addr;
6461   attr.dw_attr_val.v.val_addr = addr;
6462   add_dwarf_attr (die, &attr);
6463 }
6464
6465 /* Get the RTX from to an address DIE attribute.  */
6466
6467 static inline rtx
6468 AT_addr (dw_attr_ref a)
6469 {
6470   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6471   return a->dw_attr_val.v.val_addr;
6472 }
6473
6474 /* Add a file attribute value to a DIE.  */
6475
6476 static inline void
6477 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
6478              struct dwarf_file_data *fd)
6479 {
6480   dw_attr_node attr;
6481
6482   attr.dw_attr = attr_kind;
6483   attr.dw_attr_val.val_class = dw_val_class_file;
6484   attr.dw_attr_val.v.val_file = fd;
6485   add_dwarf_attr (die, &attr);
6486 }
6487
6488 /* Get the dwarf_file_data from a file DIE attribute.  */
6489
6490 static inline struct dwarf_file_data *
6491 AT_file (dw_attr_ref a)
6492 {
6493   gcc_assert (a && AT_class (a) == dw_val_class_file);
6494   return a->dw_attr_val.v.val_file;
6495 }
6496
6497 /* Add a label identifier attribute value to a DIE.  */
6498
6499 static inline void
6500 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
6501 {
6502   dw_attr_node attr;
6503
6504   attr.dw_attr = attr_kind;
6505   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
6506   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
6507   add_dwarf_attr (die, &attr);
6508 }
6509
6510 /* Add a section offset attribute value to a DIE, an offset into the
6511    debug_line section.  */
6512
6513 static inline void
6514 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6515                 const char *label)
6516 {
6517   dw_attr_node attr;
6518
6519   attr.dw_attr = attr_kind;
6520   attr.dw_attr_val.val_class = dw_val_class_lineptr;
6521   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6522   add_dwarf_attr (die, &attr);
6523 }
6524
6525 /* Add a section offset attribute value to a DIE, an offset into the
6526    debug_macinfo section.  */
6527
6528 static inline void
6529 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
6530                const char *label)
6531 {
6532   dw_attr_node attr;
6533
6534   attr.dw_attr = attr_kind;
6535   attr.dw_attr_val.val_class = dw_val_class_macptr;
6536   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
6537   add_dwarf_attr (die, &attr);
6538 }
6539
6540 /* Add an offset attribute value to a DIE.  */
6541
6542 static inline void
6543 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
6544                unsigned HOST_WIDE_INT offset)
6545 {
6546   dw_attr_node attr;
6547
6548   attr.dw_attr = attr_kind;
6549   attr.dw_attr_val.val_class = dw_val_class_offset;
6550   attr.dw_attr_val.v.val_offset = offset;
6551   add_dwarf_attr (die, &attr);
6552 }
6553
6554 /* Add an range_list attribute value to a DIE.  */
6555
6556 static void
6557 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
6558                    long unsigned int offset)
6559 {
6560   dw_attr_node attr;
6561
6562   attr.dw_attr = attr_kind;
6563   attr.dw_attr_val.val_class = dw_val_class_range_list;
6564   attr.dw_attr_val.v.val_offset = offset;
6565   add_dwarf_attr (die, &attr);
6566 }
6567
6568 static inline const char *
6569 AT_lbl (dw_attr_ref a)
6570 {
6571   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
6572                     || AT_class (a) == dw_val_class_lineptr
6573                     || AT_class (a) == dw_val_class_macptr));
6574   return a->dw_attr_val.v.val_lbl_id;
6575 }
6576
6577 /* Get the attribute of type attr_kind.  */
6578
6579 static dw_attr_ref
6580 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6581 {
6582   dw_attr_ref a;
6583   unsigned ix;
6584   dw_die_ref spec = NULL;
6585
6586   if (! die)
6587     return NULL;
6588
6589   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6590     if (a->dw_attr == attr_kind)
6591       return a;
6592     else if (a->dw_attr == DW_AT_specification
6593              || a->dw_attr == DW_AT_abstract_origin)
6594       spec = AT_ref (a);
6595
6596   if (spec)
6597     return get_AT (spec, attr_kind);
6598
6599   return NULL;
6600 }
6601
6602 /* Return the "low pc" attribute value, typically associated with a subprogram
6603    DIE.  Return null if the "low pc" attribute is either not present, or if it
6604    cannot be represented as an assembler label identifier.  */
6605
6606 static inline const char *
6607 get_AT_low_pc (dw_die_ref die)
6608 {
6609   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
6610
6611   return a ? AT_lbl (a) : NULL;
6612 }
6613
6614 /* Return the "high pc" attribute value, typically associated with a subprogram
6615    DIE.  Return null if the "high pc" attribute is either not present, or if it
6616    cannot be represented as an assembler label identifier.  */
6617
6618 static inline const char *
6619 get_AT_hi_pc (dw_die_ref die)
6620 {
6621   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
6622
6623   return a ? AT_lbl (a) : NULL;
6624 }
6625
6626 /* Return the value of the string attribute designated by ATTR_KIND, or
6627    NULL if it is not present.  */
6628
6629 static inline const char *
6630 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
6631 {
6632   dw_attr_ref a = get_AT (die, attr_kind);
6633
6634   return a ? AT_string (a) : NULL;
6635 }
6636
6637 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
6638    if it is not present.  */
6639
6640 static inline int
6641 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
6642 {
6643   dw_attr_ref a = get_AT (die, attr_kind);
6644
6645   return a ? AT_flag (a) : 0;
6646 }
6647
6648 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
6649    if it is not present.  */
6650
6651 static inline unsigned
6652 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
6653 {
6654   dw_attr_ref a = get_AT (die, attr_kind);
6655
6656   return a ? AT_unsigned (a) : 0;
6657 }
6658
6659 static inline dw_die_ref
6660 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
6661 {
6662   dw_attr_ref a = get_AT (die, attr_kind);
6663
6664   return a ? AT_ref (a) : NULL;
6665 }
6666
6667 static inline struct dwarf_file_data *
6668 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
6669 {
6670   dw_attr_ref a = get_AT (die, attr_kind);
6671
6672   return a ? AT_file (a) : NULL;
6673 }
6674
6675 /* Return TRUE if the language is C or C++.  */
6676
6677 static inline bool
6678 is_c_family (void)
6679 {
6680   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6681
6682   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
6683           || lang == DW_LANG_C99
6684           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
6685 }
6686
6687 /* Return TRUE if the language is C++.  */
6688
6689 static inline bool
6690 is_cxx (void)
6691 {
6692   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6693
6694   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
6695 }
6696
6697 /* Return TRUE if the language is Fortran.  */
6698
6699 static inline bool
6700 is_fortran (void)
6701 {
6702   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6703
6704   return (lang == DW_LANG_Fortran77
6705           || lang == DW_LANG_Fortran90
6706           || lang == DW_LANG_Fortran95);
6707 }
6708
6709 /* Return TRUE if the language is Java.  */
6710
6711 static inline bool
6712 is_java (void)
6713 {
6714   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6715
6716   return lang == DW_LANG_Java;
6717 }
6718
6719 /* Return TRUE if the language is Ada.  */
6720
6721 static inline bool
6722 is_ada (void)
6723 {
6724   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
6725
6726   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
6727 }
6728
6729 /* Remove the specified attribute if present.  */
6730
6731 static void
6732 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
6733 {
6734   dw_attr_ref a;
6735   unsigned ix;
6736
6737   if (! die)
6738     return;
6739
6740   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6741     if (a->dw_attr == attr_kind)
6742       {
6743         if (AT_class (a) == dw_val_class_str)
6744           if (a->dw_attr_val.v.val_str->refcount)
6745             a->dw_attr_val.v.val_str->refcount--;
6746
6747         /* VEC_ordered_remove should help reduce the number of abbrevs
6748            that are needed.  */
6749         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
6750         return;
6751       }
6752 }
6753
6754 /* Remove CHILD from its parent.  PREV must have the property that
6755    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
6756
6757 static void
6758 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
6759 {
6760   gcc_assert (child->die_parent == prev->die_parent);
6761   gcc_assert (prev->die_sib == child);
6762   if (prev == child)
6763     {
6764       gcc_assert (child->die_parent->die_child == child);
6765       prev = NULL;
6766     }
6767   else
6768     prev->die_sib = child->die_sib;
6769   if (child->die_parent->die_child == child)
6770     child->die_parent->die_child = prev;
6771 }
6772
6773 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
6774    matches TAG.  */
6775
6776 static void
6777 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
6778 {
6779   dw_die_ref c;
6780
6781   c = die->die_child;
6782   if (c) do {
6783     dw_die_ref prev = c;
6784     c = c->die_sib;
6785     while (c->die_tag == tag)
6786       {
6787         remove_child_with_prev (c, prev);
6788         /* Might have removed every child.  */
6789         if (c == c->die_sib)
6790           return;
6791         c = c->die_sib;
6792       }
6793   } while (c != die->die_child);
6794 }
6795
6796 /* Add a CHILD_DIE as the last child of DIE.  */
6797
6798 static void
6799 add_child_die (dw_die_ref die, dw_die_ref child_die)
6800 {
6801   /* FIXME this should probably be an assert.  */
6802   if (! die || ! child_die)
6803     return;
6804   gcc_assert (die != child_die);
6805
6806   child_die->die_parent = die;
6807   if (die->die_child)
6808     {
6809       child_die->die_sib = die->die_child->die_sib;
6810       die->die_child->die_sib = child_die;
6811     }
6812   else
6813     child_die->die_sib = child_die;
6814   die->die_child = child_die;
6815 }
6816
6817 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
6818    is the specification, to the end of PARENT's list of children.
6819    This is done by removing and re-adding it.  */
6820
6821 static void
6822 splice_child_die (dw_die_ref parent, dw_die_ref child)
6823 {
6824   dw_die_ref p;
6825
6826   /* We want the declaration DIE from inside the class, not the
6827      specification DIE at toplevel.  */
6828   if (child->die_parent != parent)
6829     {
6830       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
6831
6832       if (tmp)
6833         child = tmp;
6834     }
6835
6836   gcc_assert (child->die_parent == parent
6837               || (child->die_parent
6838                   == get_AT_ref (parent, DW_AT_specification)));
6839
6840   for (p = child->die_parent->die_child; ; p = p->die_sib)
6841     if (p->die_sib == child)
6842       {
6843         remove_child_with_prev (child, p);
6844         break;
6845       }
6846
6847   add_child_die (parent, child);
6848 }
6849
6850 /* Return a pointer to a newly created DIE node.  */
6851
6852 static inline dw_die_ref
6853 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
6854 {
6855   dw_die_ref die = GGC_CNEW (die_node);
6856
6857   die->die_tag = tag_value;
6858
6859   if (parent_die != NULL)
6860     add_child_die (parent_die, die);
6861   else
6862     {
6863       limbo_die_node *limbo_node;
6864
6865       limbo_node = GGC_CNEW (limbo_die_node);
6866       limbo_node->die = die;
6867       limbo_node->created_for = t;
6868       limbo_node->next = limbo_die_list;
6869       limbo_die_list = limbo_node;
6870     }
6871
6872   return die;
6873 }
6874
6875 /* Return the DIE associated with the given type specifier.  */
6876
6877 static inline dw_die_ref
6878 lookup_type_die (tree type)
6879 {
6880   return TYPE_SYMTAB_DIE (type);
6881 }
6882
6883 /* Equate a DIE to a given type specifier.  */
6884
6885 static inline void
6886 equate_type_number_to_die (tree type, dw_die_ref type_die)
6887 {
6888   TYPE_SYMTAB_DIE (type) = type_die;
6889 }
6890
6891 /* Returns a hash value for X (which really is a die_struct).  */
6892
6893 static hashval_t
6894 decl_die_table_hash (const void *x)
6895 {
6896   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
6897 }
6898
6899 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
6900
6901 static int
6902 decl_die_table_eq (const void *x, const void *y)
6903 {
6904   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
6905 }
6906
6907 /* Return the DIE associated with a given declaration.  */
6908
6909 static inline dw_die_ref
6910 lookup_decl_die (tree decl)
6911 {
6912   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
6913 }
6914
6915 /* Returns a hash value for X (which really is a var_loc_list).  */
6916
6917 static hashval_t
6918 decl_loc_table_hash (const void *x)
6919 {
6920   return (hashval_t) ((const var_loc_list *) x)->decl_id;
6921 }
6922
6923 /* Return nonzero if decl_id of var_loc_list X is the same as
6924    UID of decl *Y.  */
6925
6926 static int
6927 decl_loc_table_eq (const void *x, const void *y)
6928 {
6929   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
6930 }
6931
6932 /* Return the var_loc list associated with a given declaration.  */
6933
6934 static inline var_loc_list *
6935 lookup_decl_loc (const_tree decl)
6936 {
6937   return (var_loc_list *)
6938     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
6939 }
6940
6941 /* Equate a DIE to a particular declaration.  */
6942
6943 static void
6944 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
6945 {
6946   unsigned int decl_id = DECL_UID (decl);
6947   void **slot;
6948
6949   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
6950   *slot = decl_die;
6951   decl_die->decl_id = decl_id;
6952 }
6953
6954 /* Add a variable location node to the linked list for DECL.  */
6955
6956 static void
6957 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
6958 {
6959   unsigned int decl_id = DECL_UID (decl);
6960   var_loc_list *temp;
6961   void **slot;
6962
6963   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
6964   if (*slot == NULL)
6965     {
6966       temp = GGC_CNEW (var_loc_list);
6967       temp->decl_id = decl_id;
6968       *slot = temp;
6969     }
6970   else
6971     temp = (var_loc_list *) *slot;
6972
6973   if (temp->last)
6974     {
6975       /* If the current location is the same as the end of the list,
6976          and either both or neither of the locations is uninitialized,
6977          we have nothing to do.  */
6978       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
6979                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
6980           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6981                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
6982               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
6983                    == VAR_INIT_STATUS_UNINITIALIZED)
6984                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
6985                       == VAR_INIT_STATUS_UNINITIALIZED))))
6986         {
6987           /* Add LOC to the end of list and update LAST.  */
6988           temp->last->next = loc;
6989           temp->last = loc;
6990         }
6991     }
6992   /* Do not add empty location to the beginning of the list.  */
6993   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
6994     {
6995       temp->first = loc;
6996       temp->last = loc;
6997     }
6998 }
6999 \f
7000 /* Keep track of the number of spaces used to indent the
7001    output of the debugging routines that print the structure of
7002    the DIE internal representation.  */
7003 static int print_indent;
7004
7005 /* Indent the line the number of spaces given by print_indent.  */
7006
7007 static inline void
7008 print_spaces (FILE *outfile)
7009 {
7010   fprintf (outfile, "%*s", print_indent, "");
7011 }
7012
7013 /* Print the information associated with a given DIE, and its children.
7014    This routine is a debugging aid only.  */
7015
7016 static void
7017 print_die (dw_die_ref die, FILE *outfile)
7018 {
7019   dw_attr_ref a;
7020   dw_die_ref c;
7021   unsigned ix;
7022
7023   print_spaces (outfile);
7024   fprintf (outfile, "DIE %4ld: %s\n",
7025            die->die_offset, dwarf_tag_name (die->die_tag));
7026   print_spaces (outfile);
7027   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7028   fprintf (outfile, " offset: %ld\n", die->die_offset);
7029
7030   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7031     {
7032       print_spaces (outfile);
7033       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7034
7035       switch (AT_class (a))
7036         {
7037         case dw_val_class_addr:
7038           fprintf (outfile, "address");
7039           break;
7040         case dw_val_class_offset:
7041           fprintf (outfile, "offset");
7042           break;
7043         case dw_val_class_loc:
7044           fprintf (outfile, "location descriptor");
7045           break;
7046         case dw_val_class_loc_list:
7047           fprintf (outfile, "location list -> label:%s",
7048                    AT_loc_list (a)->ll_symbol);
7049           break;
7050         case dw_val_class_range_list:
7051           fprintf (outfile, "range list");
7052           break;
7053         case dw_val_class_const:
7054           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7055           break;
7056         case dw_val_class_unsigned_const:
7057           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7058           break;
7059         case dw_val_class_long_long:
7060           fprintf (outfile, "constant (%lu,%lu)",
7061                    a->dw_attr_val.v.val_long_long.hi,
7062                    a->dw_attr_val.v.val_long_long.low);
7063           break;
7064         case dw_val_class_vec:
7065           fprintf (outfile, "floating-point or vector constant");
7066           break;
7067         case dw_val_class_flag:
7068           fprintf (outfile, "%u", AT_flag (a));
7069           break;
7070         case dw_val_class_die_ref:
7071           if (AT_ref (a) != NULL)
7072             {
7073               if (AT_ref (a)->die_symbol)
7074                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7075               else
7076                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7077             }
7078           else
7079             fprintf (outfile, "die -> <null>");
7080           break;
7081         case dw_val_class_lbl_id:
7082         case dw_val_class_lineptr:
7083         case dw_val_class_macptr:
7084           fprintf (outfile, "label: %s", AT_lbl (a));
7085           break;
7086         case dw_val_class_str:
7087           if (AT_string (a) != NULL)
7088             fprintf (outfile, "\"%s\"", AT_string (a));
7089           else
7090             fprintf (outfile, "<null>");
7091           break;
7092         case dw_val_class_file:
7093           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7094                    AT_file (a)->emitted_number);
7095           break;
7096         default:
7097           break;
7098         }
7099
7100       fprintf (outfile, "\n");
7101     }
7102
7103   if (die->die_child != NULL)
7104     {
7105       print_indent += 4;
7106       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7107       print_indent -= 4;
7108     }
7109   if (print_indent == 0)
7110     fprintf (outfile, "\n");
7111 }
7112
7113 /* Print the contents of the source code line number correspondence table.
7114    This routine is a debugging aid only.  */
7115
7116 static void
7117 print_dwarf_line_table (FILE *outfile)
7118 {
7119   unsigned i;
7120   dw_line_info_ref line_info;
7121
7122   fprintf (outfile, "\n\nDWARF source line information\n");
7123   for (i = 1; i < line_info_table_in_use; i++)
7124     {
7125       line_info = &line_info_table[i];
7126       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7127                line_info->dw_file_num,
7128                line_info->dw_line_num);
7129     }
7130
7131   fprintf (outfile, "\n\n");
7132 }
7133
7134 /* Print the information collected for a given DIE.  */
7135
7136 void
7137 debug_dwarf_die (dw_die_ref die)
7138 {
7139   print_die (die, stderr);
7140 }
7141
7142 /* Print all DWARF information collected for the compilation unit.
7143    This routine is a debugging aid only.  */
7144
7145 void
7146 debug_dwarf (void)
7147 {
7148   print_indent = 0;
7149   print_die (comp_unit_die, stderr);
7150   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7151     print_dwarf_line_table (stderr);
7152 }
7153 \f
7154 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7155    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7156    DIE that marks the start of the DIEs for this include file.  */
7157
7158 static dw_die_ref
7159 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7160 {
7161   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7162   dw_die_ref new_unit = gen_compile_unit_die (filename);
7163
7164   new_unit->die_sib = old_unit;
7165   return new_unit;
7166 }
7167
7168 /* Close an include-file CU and reopen the enclosing one.  */
7169
7170 static dw_die_ref
7171 pop_compile_unit (dw_die_ref old_unit)
7172 {
7173   dw_die_ref new_unit = old_unit->die_sib;
7174
7175   old_unit->die_sib = NULL;
7176   return new_unit;
7177 }
7178
7179 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7180 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7181
7182 /* Calculate the checksum of a location expression.  */
7183
7184 static inline void
7185 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7186 {
7187   CHECKSUM (loc->dw_loc_opc);
7188   CHECKSUM (loc->dw_loc_oprnd1);
7189   CHECKSUM (loc->dw_loc_oprnd2);
7190 }
7191
7192 /* Calculate the checksum of an attribute.  */
7193
7194 static void
7195 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7196 {
7197   dw_loc_descr_ref loc;
7198   rtx r;
7199
7200   CHECKSUM (at->dw_attr);
7201
7202   /* We don't care that this was compiled with a different compiler
7203      snapshot; if the output is the same, that's what matters.  */
7204   if (at->dw_attr == DW_AT_producer)
7205     return;
7206
7207   switch (AT_class (at))
7208     {
7209     case dw_val_class_const:
7210       CHECKSUM (at->dw_attr_val.v.val_int);
7211       break;
7212     case dw_val_class_unsigned_const:
7213       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7214       break;
7215     case dw_val_class_long_long:
7216       CHECKSUM (at->dw_attr_val.v.val_long_long);
7217       break;
7218     case dw_val_class_vec:
7219       CHECKSUM (at->dw_attr_val.v.val_vec);
7220       break;
7221     case dw_val_class_flag:
7222       CHECKSUM (at->dw_attr_val.v.val_flag);
7223       break;
7224     case dw_val_class_str:
7225       CHECKSUM_STRING (AT_string (at));
7226       break;
7227
7228     case dw_val_class_addr:
7229       r = AT_addr (at);
7230       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7231       CHECKSUM_STRING (XSTR (r, 0));
7232       break;
7233
7234     case dw_val_class_offset:
7235       CHECKSUM (at->dw_attr_val.v.val_offset);
7236       break;
7237
7238     case dw_val_class_loc:
7239       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7240         loc_checksum (loc, ctx);
7241       break;
7242
7243     case dw_val_class_die_ref:
7244       die_checksum (AT_ref (at), ctx, mark);
7245       break;
7246
7247     case dw_val_class_fde_ref:
7248     case dw_val_class_lbl_id:
7249     case dw_val_class_lineptr:
7250     case dw_val_class_macptr:
7251       break;
7252
7253     case dw_val_class_file:
7254       CHECKSUM_STRING (AT_file (at)->filename);
7255       break;
7256
7257     default:
7258       break;
7259     }
7260 }
7261
7262 /* Calculate the checksum of a DIE.  */
7263
7264 static void
7265 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7266 {
7267   dw_die_ref c;
7268   dw_attr_ref a;
7269   unsigned ix;
7270
7271   /* To avoid infinite recursion.  */
7272   if (die->die_mark)
7273     {
7274       CHECKSUM (die->die_mark);
7275       return;
7276     }
7277   die->die_mark = ++(*mark);
7278
7279   CHECKSUM (die->die_tag);
7280
7281   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7282     attr_checksum (a, ctx, mark);
7283
7284   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7285 }
7286
7287 #undef CHECKSUM
7288 #undef CHECKSUM_STRING
7289
7290 /* Do the location expressions look same?  */
7291 static inline int
7292 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7293 {
7294   return loc1->dw_loc_opc == loc2->dw_loc_opc
7295          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7296          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7297 }
7298
7299 /* Do the values look the same?  */
7300 static int
7301 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7302 {
7303   dw_loc_descr_ref loc1, loc2;
7304   rtx r1, r2;
7305
7306   if (v1->val_class != v2->val_class)
7307     return 0;
7308
7309   switch (v1->val_class)
7310     {
7311     case dw_val_class_const:
7312       return v1->v.val_int == v2->v.val_int;
7313     case dw_val_class_unsigned_const:
7314       return v1->v.val_unsigned == v2->v.val_unsigned;
7315     case dw_val_class_long_long:
7316       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
7317              && v1->v.val_long_long.low == v2->v.val_long_long.low;
7318     case dw_val_class_vec:
7319       if (v1->v.val_vec.length != v2->v.val_vec.length
7320           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7321         return 0;
7322       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7323                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7324         return 0;
7325       return 1;
7326     case dw_val_class_flag:
7327       return v1->v.val_flag == v2->v.val_flag;
7328     case dw_val_class_str:
7329       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7330
7331     case dw_val_class_addr:
7332       r1 = v1->v.val_addr;
7333       r2 = v2->v.val_addr;
7334       if (GET_CODE (r1) != GET_CODE (r2))
7335         return 0;
7336       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7337       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7338
7339     case dw_val_class_offset:
7340       return v1->v.val_offset == v2->v.val_offset;
7341
7342     case dw_val_class_loc:
7343       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7344            loc1 && loc2;
7345            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7346         if (!same_loc_p (loc1, loc2, mark))
7347           return 0;
7348       return !loc1 && !loc2;
7349
7350     case dw_val_class_die_ref:
7351       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7352
7353     case dw_val_class_fde_ref:
7354     case dw_val_class_lbl_id:
7355     case dw_val_class_lineptr:
7356     case dw_val_class_macptr:
7357       return 1;
7358
7359     case dw_val_class_file:
7360       return v1->v.val_file == v2->v.val_file;
7361
7362     default:
7363       return 1;
7364     }
7365 }
7366
7367 /* Do the attributes look the same?  */
7368
7369 static int
7370 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7371 {
7372   if (at1->dw_attr != at2->dw_attr)
7373     return 0;
7374
7375   /* We don't care that this was compiled with a different compiler
7376      snapshot; if the output is the same, that's what matters. */
7377   if (at1->dw_attr == DW_AT_producer)
7378     return 1;
7379
7380   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7381 }
7382
7383 /* Do the dies look the same?  */
7384
7385 static int
7386 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7387 {
7388   dw_die_ref c1, c2;
7389   dw_attr_ref a1;
7390   unsigned ix;
7391
7392   /* To avoid infinite recursion.  */
7393   if (die1->die_mark)
7394     return die1->die_mark == die2->die_mark;
7395   die1->die_mark = die2->die_mark = ++(*mark);
7396
7397   if (die1->die_tag != die2->die_tag)
7398     return 0;
7399
7400   if (VEC_length (dw_attr_node, die1->die_attr)
7401       != VEC_length (dw_attr_node, die2->die_attr))
7402     return 0;
7403
7404   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7405     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7406       return 0;
7407
7408   c1 = die1->die_child;
7409   c2 = die2->die_child;
7410   if (! c1)
7411     {
7412       if (c2)
7413         return 0;
7414     }
7415   else
7416     for (;;)
7417       {
7418         if (!same_die_p (c1, c2, mark))
7419           return 0;
7420         c1 = c1->die_sib;
7421         c2 = c2->die_sib;
7422         if (c1 == die1->die_child)
7423           {
7424             if (c2 == die2->die_child)
7425               break;
7426             else
7427               return 0;
7428           }
7429     }
7430
7431   return 1;
7432 }
7433
7434 /* Do the dies look the same?  Wrapper around same_die_p.  */
7435
7436 static int
7437 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7438 {
7439   int mark = 0;
7440   int ret = same_die_p (die1, die2, &mark);
7441
7442   unmark_all_dies (die1);
7443   unmark_all_dies (die2);
7444
7445   return ret;
7446 }
7447
7448 /* The prefix to attach to symbols on DIEs in the current comdat debug
7449    info section.  */
7450 static char *comdat_symbol_id;
7451
7452 /* The index of the current symbol within the current comdat CU.  */
7453 static unsigned int comdat_symbol_number;
7454
7455 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7456    children, and set comdat_symbol_id accordingly.  */
7457
7458 static void
7459 compute_section_prefix (dw_die_ref unit_die)
7460 {
7461   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7462   const char *base = die_name ? lbasename (die_name) : "anonymous";
7463   char *name = XALLOCAVEC (char, strlen (base) + 64);
7464   char *p;
7465   int i, mark;
7466   unsigned char checksum[16];
7467   struct md5_ctx ctx;
7468
7469   /* Compute the checksum of the DIE, then append part of it as hex digits to
7470      the name filename of the unit.  */
7471
7472   md5_init_ctx (&ctx);
7473   mark = 0;
7474   die_checksum (unit_die, &ctx, &mark);
7475   unmark_all_dies (unit_die);
7476   md5_finish_ctx (&ctx, checksum);
7477
7478   sprintf (name, "%s.", base);
7479   clean_symbol_name (name);
7480
7481   p = name + strlen (name);
7482   for (i = 0; i < 4; i++)
7483     {
7484       sprintf (p, "%.2x", checksum[i]);
7485       p += 2;
7486     }
7487
7488   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
7489   comdat_symbol_number = 0;
7490 }
7491
7492 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7493
7494 static int
7495 is_type_die (dw_die_ref die)
7496 {
7497   switch (die->die_tag)
7498     {
7499     case DW_TAG_array_type:
7500     case DW_TAG_class_type:
7501     case DW_TAG_interface_type:
7502     case DW_TAG_enumeration_type:
7503     case DW_TAG_pointer_type:
7504     case DW_TAG_reference_type:
7505     case DW_TAG_string_type:
7506     case DW_TAG_structure_type:
7507     case DW_TAG_subroutine_type:
7508     case DW_TAG_union_type:
7509     case DW_TAG_ptr_to_member_type:
7510     case DW_TAG_set_type:
7511     case DW_TAG_subrange_type:
7512     case DW_TAG_base_type:
7513     case DW_TAG_const_type:
7514     case DW_TAG_file_type:
7515     case DW_TAG_packed_type:
7516     case DW_TAG_volatile_type:
7517     case DW_TAG_typedef:
7518       return 1;
7519     default:
7520       return 0;
7521     }
7522 }
7523
7524 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7525    Basically, we want to choose the bits that are likely to be shared between
7526    compilations (types) and leave out the bits that are specific to individual
7527    compilations (functions).  */
7528
7529 static int
7530 is_comdat_die (dw_die_ref c)
7531 {
7532   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7533      we do for stabs.  The advantage is a greater likelihood of sharing between
7534      objects that don't include headers in the same order (and therefore would
7535      put the base types in a different comdat).  jason 8/28/00 */
7536
7537   if (c->die_tag == DW_TAG_base_type)
7538     return 0;
7539
7540   if (c->die_tag == DW_TAG_pointer_type
7541       || c->die_tag == DW_TAG_reference_type
7542       || c->die_tag == DW_TAG_const_type
7543       || c->die_tag == DW_TAG_volatile_type)
7544     {
7545       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7546
7547       return t ? is_comdat_die (t) : 0;
7548     }
7549
7550   return is_type_die (c);
7551 }
7552
7553 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7554    compilation unit.  */
7555
7556 static int
7557 is_symbol_die (dw_die_ref c)
7558 {
7559   return (is_type_die (c)
7560           || (get_AT (c, DW_AT_declaration)
7561               && !get_AT (c, DW_AT_specification))
7562           || c->die_tag == DW_TAG_namespace
7563           || c->die_tag == DW_TAG_module);
7564 }
7565
7566 static char *
7567 gen_internal_sym (const char *prefix)
7568 {
7569   char buf[256];
7570
7571   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7572   return xstrdup (buf);
7573 }
7574
7575 /* Assign symbols to all worthy DIEs under DIE.  */
7576
7577 static void
7578 assign_symbol_names (dw_die_ref die)
7579 {
7580   dw_die_ref c;
7581
7582   if (is_symbol_die (die))
7583     {
7584       if (comdat_symbol_id)
7585         {
7586           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7587
7588           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7589                    comdat_symbol_id, comdat_symbol_number++);
7590           die->die_symbol = xstrdup (p);
7591         }
7592       else
7593         die->die_symbol = gen_internal_sym ("LDIE");
7594     }
7595
7596   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7597 }
7598
7599 struct cu_hash_table_entry
7600 {
7601   dw_die_ref cu;
7602   unsigned min_comdat_num, max_comdat_num;
7603   struct cu_hash_table_entry *next;
7604 };
7605
7606 /* Routines to manipulate hash table of CUs.  */
7607 static hashval_t
7608 htab_cu_hash (const void *of)
7609 {
7610   const struct cu_hash_table_entry *const entry =
7611     (const struct cu_hash_table_entry *) of;
7612
7613   return htab_hash_string (entry->cu->die_symbol);
7614 }
7615
7616 static int
7617 htab_cu_eq (const void *of1, const void *of2)
7618 {
7619   const struct cu_hash_table_entry *const entry1 =
7620     (const struct cu_hash_table_entry *) of1;
7621   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7622
7623   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
7624 }
7625
7626 static void
7627 htab_cu_del (void *what)
7628 {
7629   struct cu_hash_table_entry *next,
7630     *entry = (struct cu_hash_table_entry *) what;
7631
7632   while (entry)
7633     {
7634       next = entry->next;
7635       free (entry);
7636       entry = next;
7637     }
7638 }
7639
7640 /* Check whether we have already seen this CU and set up SYM_NUM
7641    accordingly.  */
7642 static int
7643 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7644 {
7645   struct cu_hash_table_entry dummy;
7646   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7647
7648   dummy.max_comdat_num = 0;
7649
7650   slot = (struct cu_hash_table_entry **)
7651     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7652         INSERT);
7653   entry = *slot;
7654
7655   for (; entry; last = entry, entry = entry->next)
7656     {
7657       if (same_die_p_wrap (cu, entry->cu))
7658         break;
7659     }
7660
7661   if (entry)
7662     {
7663       *sym_num = entry->min_comdat_num;
7664       return 1;
7665     }
7666
7667   entry = XCNEW (struct cu_hash_table_entry);
7668   entry->cu = cu;
7669   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7670   entry->next = *slot;
7671   *slot = entry;
7672
7673   return 0;
7674 }
7675
7676 /* Record SYM_NUM to record of CU in HTABLE.  */
7677 static void
7678 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7679 {
7680   struct cu_hash_table_entry **slot, *entry;
7681
7682   slot = (struct cu_hash_table_entry **)
7683     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
7684         NO_INSERT);
7685   entry = *slot;
7686
7687   entry->max_comdat_num = sym_num;
7688 }
7689
7690 /* Traverse the DIE (which is always comp_unit_die), and set up
7691    additional compilation units for each of the include files we see
7692    bracketed by BINCL/EINCL.  */
7693
7694 static void
7695 break_out_includes (dw_die_ref die)
7696 {
7697   dw_die_ref c;
7698   dw_die_ref unit = NULL;
7699   limbo_die_node *node, **pnode;
7700   htab_t cu_hash_table;
7701
7702   c = die->die_child;
7703   if (c) do {
7704     dw_die_ref prev = c;
7705     c = c->die_sib;
7706     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7707            || (unit && is_comdat_die (c)))
7708       {
7709         dw_die_ref next = c->die_sib;
7710
7711         /* This DIE is for a secondary CU; remove it from the main one.  */
7712         remove_child_with_prev (c, prev);
7713
7714         if (c->die_tag == DW_TAG_GNU_BINCL)
7715           unit = push_new_compile_unit (unit, c);
7716         else if (c->die_tag == DW_TAG_GNU_EINCL)
7717           unit = pop_compile_unit (unit);
7718         else
7719           add_child_die (unit, c);
7720         c = next;
7721         if (c == die->die_child)
7722           break;
7723       }
7724   } while (c != die->die_child);
7725
7726 #if 0
7727   /* We can only use this in debugging, since the frontend doesn't check
7728      to make sure that we leave every include file we enter.  */
7729   gcc_assert (!unit);
7730 #endif
7731
7732   assign_symbol_names (die);
7733   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7734   for (node = limbo_die_list, pnode = &limbo_die_list;
7735        node;
7736        node = node->next)
7737     {
7738       int is_dupl;
7739
7740       compute_section_prefix (node->die);
7741       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7742                         &comdat_symbol_number);
7743       assign_symbol_names (node->die);
7744       if (is_dupl)
7745         *pnode = node->next;
7746       else
7747         {
7748           pnode = &node->next;
7749           record_comdat_symbol_number (node->die, cu_hash_table,
7750                 comdat_symbol_number);
7751         }
7752     }
7753   htab_delete (cu_hash_table);
7754 }
7755
7756 /* Traverse the DIE and add a sibling attribute if it may have the
7757    effect of speeding up access to siblings.  To save some space,
7758    avoid generating sibling attributes for DIE's without children.  */
7759
7760 static void
7761 add_sibling_attributes (dw_die_ref die)
7762 {
7763   dw_die_ref c;
7764
7765   if (! die->die_child)
7766     return;
7767
7768   if (die->die_parent && die != die->die_parent->die_child)
7769     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7770
7771   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7772 }
7773
7774 /* Output all location lists for the DIE and its children.  */
7775
7776 static void
7777 output_location_lists (dw_die_ref die)
7778 {
7779   dw_die_ref c;
7780   dw_attr_ref a;
7781   unsigned ix;
7782
7783   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7784     if (AT_class (a) == dw_val_class_loc_list)
7785       output_loc_list (AT_loc_list (a));
7786
7787   FOR_EACH_CHILD (die, c, output_location_lists (c));
7788 }
7789
7790 /* The format of each DIE (and its attribute value pairs) is encoded in an
7791    abbreviation table.  This routine builds the abbreviation table and assigns
7792    a unique abbreviation id for each abbreviation entry.  The children of each
7793    die are visited recursively.  */
7794
7795 static void
7796 build_abbrev_table (dw_die_ref die)
7797 {
7798   unsigned long abbrev_id;
7799   unsigned int n_alloc;
7800   dw_die_ref c;
7801   dw_attr_ref a;
7802   unsigned ix;
7803
7804   /* Scan the DIE references, and mark as external any that refer to
7805      DIEs from other CUs (i.e. those which are not marked).  */
7806   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7807     if (AT_class (a) == dw_val_class_die_ref
7808         && AT_ref (a)->die_mark == 0)
7809       {
7810         gcc_assert (AT_ref (a)->die_symbol);
7811
7812         set_AT_ref_external (a, 1);
7813       }
7814
7815   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7816     {
7817       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7818       dw_attr_ref die_a, abbrev_a;
7819       unsigned ix;
7820       bool ok = true;
7821
7822       if (abbrev->die_tag != die->die_tag)
7823         continue;
7824       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7825         continue;
7826
7827       if (VEC_length (dw_attr_node, abbrev->die_attr)
7828           != VEC_length (dw_attr_node, die->die_attr))
7829         continue;
7830
7831       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
7832         {
7833           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7834           if ((abbrev_a->dw_attr != die_a->dw_attr)
7835               || (value_format (abbrev_a) != value_format (die_a)))
7836             {
7837               ok = false;
7838               break;
7839             }
7840         }
7841       if (ok)
7842         break;
7843     }
7844
7845   if (abbrev_id >= abbrev_die_table_in_use)
7846     {
7847       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7848         {
7849           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7850           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7851                                             n_alloc);
7852
7853           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7854                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7855           abbrev_die_table_allocated = n_alloc;
7856         }
7857
7858       ++abbrev_die_table_in_use;
7859       abbrev_die_table[abbrev_id] = die;
7860     }
7861
7862   die->die_abbrev = abbrev_id;
7863   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7864 }
7865 \f
7866 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7867
7868 static int
7869 constant_size (unsigned HOST_WIDE_INT value)
7870 {
7871   int log;
7872
7873   if (value == 0)
7874     log = 0;
7875   else
7876     log = floor_log2 (value);
7877
7878   log = log / 8;
7879   log = 1 << (floor_log2 (log) + 1);
7880
7881   return log;
7882 }
7883
7884 /* Return the size of a DIE as it is represented in the
7885    .debug_info section.  */
7886
7887 static unsigned long
7888 size_of_die (dw_die_ref die)
7889 {
7890   unsigned long size = 0;
7891   dw_attr_ref a;
7892   unsigned ix;
7893
7894   size += size_of_uleb128 (die->die_abbrev);
7895   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7896     {
7897       switch (AT_class (a))
7898         {
7899         case dw_val_class_addr:
7900           size += DWARF2_ADDR_SIZE;
7901           break;
7902         case dw_val_class_offset:
7903           size += DWARF_OFFSET_SIZE;
7904           break;
7905         case dw_val_class_loc:
7906           {
7907             unsigned long lsize = size_of_locs (AT_loc (a));
7908
7909             /* Block length.  */
7910             size += constant_size (lsize);
7911             size += lsize;
7912           }
7913           break;
7914         case dw_val_class_loc_list:
7915           size += DWARF_OFFSET_SIZE;
7916           break;
7917         case dw_val_class_range_list:
7918           size += DWARF_OFFSET_SIZE;
7919           break;
7920         case dw_val_class_const:
7921           size += size_of_sleb128 (AT_int (a));
7922           break;
7923         case dw_val_class_unsigned_const:
7924           size += constant_size (AT_unsigned (a));
7925           break;
7926         case dw_val_class_long_long:
7927           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
7928           break;
7929         case dw_val_class_vec:
7930           size += constant_size (a->dw_attr_val.v.val_vec.length
7931                                  * a->dw_attr_val.v.val_vec.elt_size)
7932                   + a->dw_attr_val.v.val_vec.length
7933                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7934           break;
7935         case dw_val_class_flag:
7936           size += 1;
7937           break;
7938         case dw_val_class_die_ref:
7939           if (AT_ref_external (a))
7940             size += DWARF2_ADDR_SIZE;
7941           else
7942             size += DWARF_OFFSET_SIZE;
7943           break;
7944         case dw_val_class_fde_ref:
7945           size += DWARF_OFFSET_SIZE;
7946           break;
7947         case dw_val_class_lbl_id:
7948           size += DWARF2_ADDR_SIZE;
7949           break;
7950         case dw_val_class_lineptr:
7951         case dw_val_class_macptr:
7952           size += DWARF_OFFSET_SIZE;
7953           break;
7954         case dw_val_class_str:
7955           if (AT_string_form (a) == DW_FORM_strp)
7956             size += DWARF_OFFSET_SIZE;
7957           else
7958             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7959           break;
7960         case dw_val_class_file:
7961           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7962           break;
7963         default:
7964           gcc_unreachable ();
7965         }
7966     }
7967
7968   return size;
7969 }
7970
7971 /* Size the debugging information associated with a given DIE.  Visits the
7972    DIE's children recursively.  Updates the global variable next_die_offset, on
7973    each time through.  Uses the current value of next_die_offset to update the
7974    die_offset field in each DIE.  */
7975
7976 static void
7977 calc_die_sizes (dw_die_ref die)
7978 {
7979   dw_die_ref c;
7980
7981   die->die_offset = next_die_offset;
7982   next_die_offset += size_of_die (die);
7983
7984   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7985
7986   if (die->die_child != NULL)
7987     /* Count the null byte used to terminate sibling lists.  */
7988     next_die_offset += 1;
7989 }
7990
7991 /* Set the marks for a die and its children.  We do this so
7992    that we know whether or not a reference needs to use FORM_ref_addr; only
7993    DIEs in the same CU will be marked.  We used to clear out the offset
7994    and use that as the flag, but ran into ordering problems.  */
7995
7996 static void
7997 mark_dies (dw_die_ref die)
7998 {
7999   dw_die_ref c;
8000
8001   gcc_assert (!die->die_mark);
8002
8003   die->die_mark = 1;
8004   FOR_EACH_CHILD (die, c, mark_dies (c));
8005 }
8006
8007 /* Clear the marks for a die and its children.  */
8008
8009 static void
8010 unmark_dies (dw_die_ref die)
8011 {
8012   dw_die_ref c;
8013
8014   gcc_assert (die->die_mark);
8015
8016   die->die_mark = 0;
8017   FOR_EACH_CHILD (die, c, unmark_dies (c));
8018 }
8019
8020 /* Clear the marks for a die, its children and referred dies.  */
8021
8022 static void
8023 unmark_all_dies (dw_die_ref die)
8024 {
8025   dw_die_ref c;
8026   dw_attr_ref a;
8027   unsigned ix;
8028
8029   if (!die->die_mark)
8030     return;
8031   die->die_mark = 0;
8032
8033   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8034
8035   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8036     if (AT_class (a) == dw_val_class_die_ref)
8037       unmark_all_dies (AT_ref (a));
8038 }
8039
8040 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8041    generated for the compilation unit.  */
8042
8043 static unsigned long
8044 size_of_pubnames (VEC (pubname_entry, gc) * names)
8045 {
8046   unsigned long size;
8047   unsigned i;
8048   pubname_ref p;
8049
8050   size = DWARF_PUBNAMES_HEADER_SIZE;
8051   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8052     if (names != pubtype_table
8053         || p->die->die_offset != 0
8054         || !flag_eliminate_unused_debug_types)
8055       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8056
8057   size += DWARF_OFFSET_SIZE;
8058   return size;
8059 }
8060
8061 /* Return the size of the information in the .debug_aranges section.  */
8062
8063 static unsigned long
8064 size_of_aranges (void)
8065 {
8066   unsigned long size;
8067
8068   size = DWARF_ARANGES_HEADER_SIZE;
8069
8070   /* Count the address/length pair for this compilation unit.  */
8071   if (text_section_used)
8072     size += 2 * DWARF2_ADDR_SIZE;
8073   if (cold_text_section_used)
8074     size += 2 * DWARF2_ADDR_SIZE;
8075   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8076
8077   /* Count the two zero words used to terminated the address range table.  */
8078   size += 2 * DWARF2_ADDR_SIZE;
8079   return size;
8080 }
8081 \f
8082 /* Select the encoding of an attribute value.  */
8083
8084 static enum dwarf_form
8085 value_format (dw_attr_ref a)
8086 {
8087   switch (a->dw_attr_val.val_class)
8088     {
8089     case dw_val_class_addr:
8090       return DW_FORM_addr;
8091     case dw_val_class_range_list:
8092     case dw_val_class_offset:
8093     case dw_val_class_loc_list:
8094       switch (DWARF_OFFSET_SIZE)
8095         {
8096         case 4:
8097           return DW_FORM_data4;
8098         case 8:
8099           return DW_FORM_data8;
8100         default:
8101           gcc_unreachable ();
8102         }
8103     case dw_val_class_loc:
8104       switch (constant_size (size_of_locs (AT_loc (a))))
8105         {
8106         case 1:
8107           return DW_FORM_block1;
8108         case 2:
8109           return DW_FORM_block2;
8110         default:
8111           gcc_unreachable ();
8112         }
8113     case dw_val_class_const:
8114       return DW_FORM_sdata;
8115     case dw_val_class_unsigned_const:
8116       switch (constant_size (AT_unsigned (a)))
8117         {
8118         case 1:
8119           return DW_FORM_data1;
8120         case 2:
8121           return DW_FORM_data2;
8122         case 4:
8123           return DW_FORM_data4;
8124         case 8:
8125           return DW_FORM_data8;
8126         default:
8127           gcc_unreachable ();
8128         }
8129     case dw_val_class_long_long:
8130       return DW_FORM_block1;
8131     case dw_val_class_vec:
8132       switch (constant_size (a->dw_attr_val.v.val_vec.length
8133                              * a->dw_attr_val.v.val_vec.elt_size))
8134         {
8135         case 1:
8136           return DW_FORM_block1;
8137         case 2:
8138           return DW_FORM_block2;
8139         case 4:
8140           return DW_FORM_block4;
8141         default:
8142           gcc_unreachable ();
8143         }
8144     case dw_val_class_flag:
8145       return DW_FORM_flag;
8146     case dw_val_class_die_ref:
8147       if (AT_ref_external (a))
8148         return DW_FORM_ref_addr;
8149       else
8150         return DW_FORM_ref;
8151     case dw_val_class_fde_ref:
8152       return DW_FORM_data;
8153     case dw_val_class_lbl_id:
8154       return DW_FORM_addr;
8155     case dw_val_class_lineptr:
8156     case dw_val_class_macptr:
8157       return DW_FORM_data;
8158     case dw_val_class_str:
8159       return AT_string_form (a);
8160     case dw_val_class_file:
8161       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8162         {
8163         case 1:
8164           return DW_FORM_data1;
8165         case 2:
8166           return DW_FORM_data2;
8167         case 4:
8168           return DW_FORM_data4;
8169         default:
8170           gcc_unreachable ();
8171         }
8172
8173     default:
8174       gcc_unreachable ();
8175     }
8176 }
8177
8178 /* Output the encoding of an attribute value.  */
8179
8180 static void
8181 output_value_format (dw_attr_ref a)
8182 {
8183   enum dwarf_form form = value_format (a);
8184
8185   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8186 }
8187
8188 /* Output the .debug_abbrev section which defines the DIE abbreviation
8189    table.  */
8190
8191 static void
8192 output_abbrev_section (void)
8193 {
8194   unsigned long abbrev_id;
8195
8196   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8197     {
8198       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8199       unsigned ix;
8200       dw_attr_ref a_attr;
8201
8202       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8203       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8204                                    dwarf_tag_name (abbrev->die_tag));
8205
8206       if (abbrev->die_child != NULL)
8207         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8208       else
8209         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8210
8211       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8212            ix++)
8213         {
8214           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8215                                        dwarf_attr_name (a_attr->dw_attr));
8216           output_value_format (a_attr);
8217         }
8218
8219       dw2_asm_output_data (1, 0, NULL);
8220       dw2_asm_output_data (1, 0, NULL);
8221     }
8222
8223   /* Terminate the table.  */
8224   dw2_asm_output_data (1, 0, NULL);
8225 }
8226
8227 /* Output a symbol we can use to refer to this DIE from another CU.  */
8228
8229 static inline void
8230 output_die_symbol (dw_die_ref die)
8231 {
8232   char *sym = die->die_symbol;
8233
8234   if (sym == 0)
8235     return;
8236
8237   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8238     /* We make these global, not weak; if the target doesn't support
8239        .linkonce, it doesn't support combining the sections, so debugging
8240        will break.  */
8241     targetm.asm_out.globalize_label (asm_out_file, sym);
8242
8243   ASM_OUTPUT_LABEL (asm_out_file, sym);
8244 }
8245
8246 /* Return a new location list, given the begin and end range, and the
8247    expression. gensym tells us whether to generate a new internal symbol for
8248    this location list node, which is done for the head of the list only.  */
8249
8250 static inline dw_loc_list_ref
8251 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8252               const char *section, unsigned int gensym)
8253 {
8254   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8255
8256   retlist->begin = begin;
8257   retlist->end = end;
8258   retlist->expr = expr;
8259   retlist->section = section;
8260   if (gensym)
8261     retlist->ll_symbol = gen_internal_sym ("LLST");
8262
8263   return retlist;
8264 }
8265
8266 /* Add a location description expression to a location list.  */
8267
8268 static inline void
8269 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8270                            const char *begin, const char *end,
8271                            const char *section)
8272 {
8273   dw_loc_list_ref *d;
8274
8275   /* Find the end of the chain.  */
8276   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8277     ;
8278
8279   /* Add a new location list node to the list.  */
8280   *d = new_loc_list (descr, begin, end, section, 0);
8281 }
8282
8283 /* Output the location list given to us.  */
8284
8285 static void
8286 output_loc_list (dw_loc_list_ref list_head)
8287 {
8288   dw_loc_list_ref curr = list_head;
8289
8290   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8291
8292   /* Walk the location list, and output each range + expression.  */
8293   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8294     {
8295       unsigned long size;
8296       /* Don't output an entry that starts and ends at the same address.  */
8297       if (strcmp (curr->begin, curr->end) == 0)
8298         continue;
8299       if (!have_multiple_function_sections)
8300         {
8301           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8302                                 "Location list begin address (%s)",
8303                                 list_head->ll_symbol);
8304           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8305                                 "Location list end address (%s)",
8306                                 list_head->ll_symbol);
8307         }
8308       else
8309         {
8310           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8311                                "Location list begin address (%s)",
8312                                list_head->ll_symbol);
8313           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8314                                "Location list end address (%s)",
8315                                list_head->ll_symbol);
8316         }
8317       size = size_of_locs (curr->expr);
8318
8319       /* Output the block length for this list of location operations.  */
8320       gcc_assert (size <= 0xffff);
8321       dw2_asm_output_data (2, size, "%s", "Location expression size");
8322
8323       output_loc_sequence (curr->expr);
8324     }
8325
8326   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8327                        "Location list terminator begin (%s)",
8328                        list_head->ll_symbol);
8329   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8330                        "Location list terminator end (%s)",
8331                        list_head->ll_symbol);
8332 }
8333
8334 /* Output the DIE and its attributes.  Called recursively to generate
8335    the definitions of each child DIE.  */
8336
8337 static void
8338 output_die (dw_die_ref die)
8339 {
8340   dw_attr_ref a;
8341   dw_die_ref c;
8342   unsigned long size;
8343   unsigned ix;
8344
8345   /* If someone in another CU might refer to us, set up a symbol for
8346      them to point to.  */
8347   if (die->die_symbol)
8348     output_die_symbol (die);
8349
8350   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8351                                (unsigned long)die->die_offset,
8352                                dwarf_tag_name (die->die_tag));
8353
8354   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8355     {
8356       const char *name = dwarf_attr_name (a->dw_attr);
8357
8358       switch (AT_class (a))
8359         {
8360         case dw_val_class_addr:
8361           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8362           break;
8363
8364         case dw_val_class_offset:
8365           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8366                                "%s", name);
8367           break;
8368
8369         case dw_val_class_range_list:
8370           {
8371             char *p = strchr (ranges_section_label, '\0');
8372
8373             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8374                      a->dw_attr_val.v.val_offset);
8375             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8376                                    debug_ranges_section, "%s", name);
8377             *p = '\0';
8378           }
8379           break;
8380
8381         case dw_val_class_loc:
8382           size = size_of_locs (AT_loc (a));
8383
8384           /* Output the block length for this list of location operations.  */
8385           dw2_asm_output_data (constant_size (size), size, "%s", name);
8386
8387           output_loc_sequence (AT_loc (a));
8388           break;
8389
8390         case dw_val_class_const:
8391           /* ??? It would be slightly more efficient to use a scheme like is
8392              used for unsigned constants below, but gdb 4.x does not sign
8393              extend.  Gdb 5.x does sign extend.  */
8394           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8395           break;
8396
8397         case dw_val_class_unsigned_const:
8398           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8399                                AT_unsigned (a), "%s", name);
8400           break;
8401
8402         case dw_val_class_long_long:
8403           {
8404             unsigned HOST_WIDE_INT first, second;
8405
8406             dw2_asm_output_data (1,
8407                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8408                                  "%s", name);
8409
8410             if (WORDS_BIG_ENDIAN)
8411               {
8412                 first = a->dw_attr_val.v.val_long_long.hi;
8413                 second = a->dw_attr_val.v.val_long_long.low;
8414               }
8415             else
8416               {
8417                 first = a->dw_attr_val.v.val_long_long.low;
8418                 second = a->dw_attr_val.v.val_long_long.hi;
8419               }
8420
8421             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8422                                  first, "long long constant");
8423             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
8424                                  second, NULL);
8425           }
8426           break;
8427
8428         case dw_val_class_vec:
8429           {
8430             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8431             unsigned int len = a->dw_attr_val.v.val_vec.length;
8432             unsigned int i;
8433             unsigned char *p;
8434
8435             dw2_asm_output_data (constant_size (len * elt_size),
8436                                  len * elt_size, "%s", name);
8437             if (elt_size > sizeof (HOST_WIDE_INT))
8438               {
8439                 elt_size /= 2;
8440                 len *= 2;
8441               }
8442             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8443                  i < len;
8444                  i++, p += elt_size)
8445               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8446                                    "fp or vector constant word %u", i);
8447             break;
8448           }
8449
8450         case dw_val_class_flag:
8451           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8452           break;
8453
8454         case dw_val_class_loc_list:
8455           {
8456             char *sym = AT_loc_list (a)->ll_symbol;
8457
8458             gcc_assert (sym);
8459             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8460                                    "%s", name);
8461           }
8462           break;
8463
8464         case dw_val_class_die_ref:
8465           if (AT_ref_external (a))
8466             {
8467               char *sym = AT_ref (a)->die_symbol;
8468
8469               gcc_assert (sym);
8470               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
8471                                      "%s", name);
8472             }
8473           else
8474             {
8475               gcc_assert (AT_ref (a)->die_offset);
8476               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8477                                    "%s", name);
8478             }
8479           break;
8480
8481         case dw_val_class_fde_ref:
8482           {
8483             char l1[20];
8484
8485             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8486                                          a->dw_attr_val.v.val_fde_index * 2);
8487             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8488                                    "%s", name);
8489           }
8490           break;
8491
8492         case dw_val_class_lbl_id:
8493           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8494           break;
8495
8496         case dw_val_class_lineptr:
8497           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8498                                  debug_line_section, "%s", name);
8499           break;
8500
8501         case dw_val_class_macptr:
8502           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8503                                  debug_macinfo_section, "%s", name);
8504           break;
8505
8506         case dw_val_class_str:
8507           if (AT_string_form (a) == DW_FORM_strp)
8508             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8509                                    a->dw_attr_val.v.val_str->label,
8510                                    debug_str_section,
8511                                    "%s: \"%s\"", name, AT_string (a));
8512           else
8513             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8514           break;
8515
8516         case dw_val_class_file:
8517           {
8518             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8519
8520             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8521                                  a->dw_attr_val.v.val_file->filename);
8522             break;
8523           }
8524
8525         default:
8526           gcc_unreachable ();
8527         }
8528     }
8529
8530   FOR_EACH_CHILD (die, c, output_die (c));
8531
8532   /* Add null byte to terminate sibling list.  */
8533   if (die->die_child != NULL)
8534     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
8535                          (unsigned long) die->die_offset);
8536 }
8537
8538 /* Output the compilation unit that appears at the beginning of the
8539    .debug_info section, and precedes the DIE descriptions.  */
8540
8541 static void
8542 output_compilation_unit_header (void)
8543 {
8544   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8545     dw2_asm_output_data (4, 0xffffffff,
8546       "Initial length escape value indicating 64-bit DWARF extension");
8547   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8548                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8549                        "Length of Compilation Unit Info");
8550   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
8551   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8552                          debug_abbrev_section,
8553                          "Offset Into Abbrev. Section");
8554   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8555 }
8556
8557 /* Output the compilation unit DIE and its children.  */
8558
8559 static void
8560 output_comp_unit (dw_die_ref die, int output_if_empty)
8561 {
8562   const char *secname;
8563   char *oldsym, *tmp;
8564
8565   /* Unless we are outputting main CU, we may throw away empty ones.  */
8566   if (!output_if_empty && die->die_child == NULL)
8567     return;
8568
8569   /* Even if there are no children of this DIE, we must output the information
8570      about the compilation unit.  Otherwise, on an empty translation unit, we
8571      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8572      will then complain when examining the file.  First mark all the DIEs in
8573      this CU so we know which get local refs.  */
8574   mark_dies (die);
8575
8576   build_abbrev_table (die);
8577
8578   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8579   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8580   calc_die_sizes (die);
8581
8582   oldsym = die->die_symbol;
8583   if (oldsym)
8584     {
8585       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8586
8587       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8588       secname = tmp;
8589       die->die_symbol = NULL;
8590       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8591     }
8592   else
8593     switch_to_section (debug_info_section);
8594
8595   /* Output debugging information.  */
8596   output_compilation_unit_header ();
8597   output_die (die);
8598
8599   /* Leave the marks on the main CU, so we can check them in
8600      output_pubnames.  */
8601   if (oldsym)
8602     {
8603       unmark_dies (die);
8604       die->die_symbol = oldsym;
8605     }
8606 }
8607
8608 /* Return the DWARF2/3 pubname associated with a decl.  */
8609
8610 static const char *
8611 dwarf2_name (tree decl, int scope)
8612 {
8613   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8614 }
8615
8616 /* Add a new entry to .debug_pubnames if appropriate.  */
8617
8618 static void
8619 add_pubname_string (const char *str, dw_die_ref die)
8620 {
8621   pubname_entry e;
8622
8623   e.die = die;
8624   e.name = xstrdup (str);
8625   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8626 }
8627
8628 static void
8629 add_pubname (tree decl, dw_die_ref die)
8630 {
8631
8632   if (TREE_PUBLIC (decl))
8633     add_pubname_string (dwarf2_name (decl, 1), die);
8634 }
8635
8636 /* Add a new entry to .debug_pubtypes if appropriate.  */
8637
8638 static void
8639 add_pubtype (tree decl, dw_die_ref die)
8640 {
8641   pubname_entry e;
8642
8643   e.name = NULL;
8644   if ((TREE_PUBLIC (decl)
8645        || die->die_parent == comp_unit_die)
8646       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8647     {
8648       e.die = die;
8649       if (TYPE_P (decl))
8650         {
8651           if (TYPE_NAME (decl))
8652             {
8653               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8654                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8655               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8656                        && DECL_NAME (TYPE_NAME (decl)))
8657                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8658               else
8659                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8660             }
8661         }
8662       else
8663         e.name = xstrdup (dwarf2_name (decl, 1));
8664
8665       /* If we don't have a name for the type, there's no point in adding
8666          it to the table.  */
8667       if (e.name && e.name[0] != '\0')
8668         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8669     }
8670 }
8671
8672 /* Output the public names table used to speed up access to externally
8673    visible names; or the public types table used to find type definitions.  */
8674
8675 static void
8676 output_pubnames (VEC (pubname_entry, gc) * names)
8677 {
8678   unsigned i;
8679   unsigned long pubnames_length = size_of_pubnames (names);
8680   pubname_ref pub;
8681
8682   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8683     dw2_asm_output_data (4, 0xffffffff,
8684       "Initial length escape value indicating 64-bit DWARF extension");
8685   if (names == pubname_table)
8686     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8687                          "Length of Public Names Info");
8688   else
8689     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8690                          "Length of Public Type Names Info");
8691   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8692   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8693                          debug_info_section,
8694                          "Offset of Compilation Unit Info");
8695   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8696                        "Compilation Unit Length");
8697
8698   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
8699     {
8700       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8701       if (names == pubname_table)
8702         gcc_assert (pub->die->die_mark);
8703
8704       if (names != pubtype_table
8705           || pub->die->die_offset != 0
8706           || !flag_eliminate_unused_debug_types)
8707         {
8708           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8709                                "DIE offset");
8710
8711           dw2_asm_output_nstring (pub->name, -1, "external name");
8712         }
8713     }
8714
8715   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8716 }
8717
8718 /* Add a new entry to .debug_aranges if appropriate.  */
8719
8720 static void
8721 add_arange (tree decl, dw_die_ref die)
8722 {
8723   if (! DECL_SECTION_NAME (decl))
8724     return;
8725
8726   if (arange_table_in_use == arange_table_allocated)
8727     {
8728       arange_table_allocated += ARANGE_TABLE_INCREMENT;
8729       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
8730                                     arange_table_allocated);
8731       memset (arange_table + arange_table_in_use, 0,
8732               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
8733     }
8734
8735   arange_table[arange_table_in_use++] = die;
8736 }
8737
8738 /* Output the information that goes into the .debug_aranges table.
8739    Namely, define the beginning and ending address range of the
8740    text section generated for this compilation unit.  */
8741
8742 static void
8743 output_aranges (void)
8744 {
8745   unsigned i;
8746   unsigned long aranges_length = size_of_aranges ();
8747
8748   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8749     dw2_asm_output_data (4, 0xffffffff,
8750       "Initial length escape value indicating 64-bit DWARF extension");
8751   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8752                        "Length of Address Ranges Info");
8753   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8754   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8755                          debug_info_section,
8756                          "Offset of Compilation Unit Info");
8757   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8758   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8759
8760   /* We need to align to twice the pointer size here.  */
8761   if (DWARF_ARANGES_PAD_SIZE)
8762     {
8763       /* Pad using a 2 byte words so that padding is correct for any
8764          pointer size.  */
8765       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8766                            2 * DWARF2_ADDR_SIZE);
8767       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8768         dw2_asm_output_data (2, 0, NULL);
8769     }
8770
8771   /* It is necessary not to output these entries if the sections were
8772      not used; if the sections were not used, the length will be 0 and
8773      the address may end up as 0 if the section is discarded by ld
8774      --gc-sections, leaving an invalid (0, 0) entry that can be
8775      confused with the terminator.  */
8776   if (text_section_used)
8777     {
8778       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8779       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8780                             text_section_label, "Length");
8781     }
8782   if (cold_text_section_used)
8783     {
8784       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8785                            "Address");
8786       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8787                             cold_text_section_label, "Length");
8788     }
8789
8790   for (i = 0; i < arange_table_in_use; i++)
8791     {
8792       dw_die_ref die = arange_table[i];
8793
8794       /* We shouldn't see aranges for DIEs outside of the main CU.  */
8795       gcc_assert (die->die_mark);
8796
8797       if (die->die_tag == DW_TAG_subprogram)
8798         {
8799           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
8800                                "Address");
8801           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
8802                                 get_AT_low_pc (die), "Length");
8803         }
8804       else
8805         {
8806           /* A static variable; extract the symbol from DW_AT_location.
8807              Note that this code isn't currently hit, as we only emit
8808              aranges for functions (jason 9/23/99).  */
8809           dw_attr_ref a = get_AT (die, DW_AT_location);
8810           dw_loc_descr_ref loc;
8811
8812           gcc_assert (a && AT_class (a) == dw_val_class_loc);
8813
8814           loc = AT_loc (a);
8815           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
8816
8817           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
8818                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
8819           dw2_asm_output_data (DWARF2_ADDR_SIZE,
8820                                get_AT_unsigned (die, DW_AT_byte_size),
8821                                "Length");
8822         }
8823     }
8824
8825   /* Output the terminator words.  */
8826   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8827   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8828 }
8829
8830 /* Add a new entry to .debug_ranges.  Return the offset at which it
8831    was placed.  */
8832
8833 static unsigned int
8834 add_ranges_num (int num)
8835 {
8836   unsigned int in_use = ranges_table_in_use;
8837
8838   if (in_use == ranges_table_allocated)
8839     {
8840       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8841       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8842                                     ranges_table_allocated);
8843       memset (ranges_table + ranges_table_in_use, 0,
8844               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8845     }
8846
8847   ranges_table[in_use].num = num;
8848   ranges_table_in_use = in_use + 1;
8849
8850   return in_use * 2 * DWARF2_ADDR_SIZE;
8851 }
8852
8853 /* Add a new entry to .debug_ranges corresponding to a block, or a
8854    range terminator if BLOCK is NULL.  */
8855
8856 static unsigned int
8857 add_ranges (const_tree block)
8858 {
8859   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8860 }
8861
8862 /* Add a new entry to .debug_ranges corresponding to a pair of
8863    labels.  */
8864
8865 static unsigned int
8866 add_ranges_by_labels (const char *begin, const char *end)
8867 {
8868   unsigned int in_use = ranges_by_label_in_use;
8869
8870   if (in_use == ranges_by_label_allocated)
8871     {
8872       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8873       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8874                                        ranges_by_label,
8875                                        ranges_by_label_allocated);
8876       memset (ranges_by_label + ranges_by_label_in_use, 0,
8877               RANGES_TABLE_INCREMENT
8878               * sizeof (struct dw_ranges_by_label_struct));
8879     }
8880
8881   ranges_by_label[in_use].begin = begin;
8882   ranges_by_label[in_use].end = end;
8883   ranges_by_label_in_use = in_use + 1;
8884
8885   return add_ranges_num (-(int)in_use - 1);
8886 }
8887
8888 static void
8889 output_ranges (void)
8890 {
8891   unsigned i;
8892   static const char *const start_fmt = "Offset 0x%x";
8893   const char *fmt = start_fmt;
8894
8895   for (i = 0; i < ranges_table_in_use; i++)
8896     {
8897       int block_num = ranges_table[i].num;
8898
8899       if (block_num > 0)
8900         {
8901           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8902           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8903
8904           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8905           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8906
8907           /* If all code is in the text section, then the compilation
8908              unit base address defaults to DW_AT_low_pc, which is the
8909              base of the text section.  */
8910           if (!have_multiple_function_sections)
8911             {
8912               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8913                                     text_section_label,
8914                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8915               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8916                                     text_section_label, NULL);
8917             }
8918
8919           /* Otherwise, the compilation unit base address is zero,
8920              which allows us to use absolute addresses, and not worry
8921              about whether the target supports cross-section
8922              arithmetic.  */
8923           else
8924             {
8925               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8926                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8927               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8928             }
8929
8930           fmt = NULL;
8931         }
8932
8933       /* Negative block_num stands for an index into ranges_by_label.  */
8934       else if (block_num < 0)
8935         {
8936           int lab_idx = - block_num - 1;
8937
8938           if (!have_multiple_function_sections)
8939             {
8940               gcc_unreachable ();
8941 #if 0
8942               /* If we ever use add_ranges_by_labels () for a single
8943                  function section, all we have to do is to take out
8944                  the #if 0 above.  */
8945               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8946                                     ranges_by_label[lab_idx].begin,
8947                                     text_section_label,
8948                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8949               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8950                                     ranges_by_label[lab_idx].end,
8951                                     text_section_label, NULL);
8952 #endif
8953             }
8954           else
8955             {
8956               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8957                                    ranges_by_label[lab_idx].begin,
8958                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8959               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8960                                    ranges_by_label[lab_idx].end,
8961                                    NULL);
8962             }
8963         }
8964       else
8965         {
8966           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8967           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8968           fmt = start_fmt;
8969         }
8970     }
8971 }
8972
8973 /* Data structure containing information about input files.  */
8974 struct file_info
8975 {
8976   const char *path;     /* Complete file name.  */
8977   const char *fname;    /* File name part.  */
8978   int length;           /* Length of entire string.  */
8979   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8980   int dir_idx;          /* Index in directory table.  */
8981 };
8982
8983 /* Data structure containing information about directories with source
8984    files.  */
8985 struct dir_info
8986 {
8987   const char *path;     /* Path including directory name.  */
8988   int length;           /* Path length.  */
8989   int prefix;           /* Index of directory entry which is a prefix.  */
8990   int count;            /* Number of files in this directory.  */
8991   int dir_idx;          /* Index of directory used as base.  */
8992 };
8993
8994 /* Callback function for file_info comparison.  We sort by looking at
8995    the directories in the path.  */
8996
8997 static int
8998 file_info_cmp (const void *p1, const void *p2)
8999 {
9000   const struct file_info *const s1 = (const struct file_info *) p1;
9001   const struct file_info *const s2 = (const struct file_info *) p2;
9002   const unsigned char *cp1;
9003   const unsigned char *cp2;
9004
9005   /* Take care of file names without directories.  We need to make sure that
9006      we return consistent values to qsort since some will get confused if
9007      we return the same value when identical operands are passed in opposite
9008      orders.  So if neither has a directory, return 0 and otherwise return
9009      1 or -1 depending on which one has the directory.  */
9010   if ((s1->path == s1->fname || s2->path == s2->fname))
9011     return (s2->path == s2->fname) - (s1->path == s1->fname);
9012
9013   cp1 = (const unsigned char *) s1->path;
9014   cp2 = (const unsigned char *) s2->path;
9015
9016   while (1)
9017     {
9018       ++cp1;
9019       ++cp2;
9020       /* Reached the end of the first path?  If so, handle like above.  */
9021       if ((cp1 == (const unsigned char *) s1->fname)
9022           || (cp2 == (const unsigned char *) s2->fname))
9023         return ((cp2 == (const unsigned char *) s2->fname)
9024                 - (cp1 == (const unsigned char *) s1->fname));
9025
9026       /* Character of current path component the same?  */
9027       else if (*cp1 != *cp2)
9028         return *cp1 - *cp2;
9029     }
9030 }
9031
9032 struct file_name_acquire_data
9033 {
9034   struct file_info *files;
9035   int used_files;
9036   int max_files;
9037 };
9038
9039 /* Traversal function for the hash table.  */
9040
9041 static int
9042 file_name_acquire (void ** slot, void *data)
9043 {
9044   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9045   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9046   struct file_info *fi;
9047   const char *f;
9048
9049   gcc_assert (fnad->max_files >= d->emitted_number);
9050
9051   if (! d->emitted_number)
9052     return 1;
9053
9054   gcc_assert (fnad->max_files != fnad->used_files);
9055
9056   fi = fnad->files + fnad->used_files++;
9057
9058   /* Skip all leading "./".  */
9059   f = d->filename;
9060   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9061     f += 2;
9062
9063   /* Create a new array entry.  */
9064   fi->path = f;
9065   fi->length = strlen (f);
9066   fi->file_idx = d;
9067
9068   /* Search for the file name part.  */
9069   f = strrchr (f, DIR_SEPARATOR);
9070 #if defined (DIR_SEPARATOR_2)
9071   {
9072     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9073
9074     if (g != NULL)
9075       {
9076         if (f == NULL || f < g)
9077           f = g;
9078       }
9079   }
9080 #endif
9081
9082   fi->fname = f == NULL ? fi->path : f + 1;
9083   return 1;
9084 }
9085
9086 /* Output the directory table and the file name table.  We try to minimize
9087    the total amount of memory needed.  A heuristic is used to avoid large
9088    slowdowns with many input files.  */
9089
9090 static void
9091 output_file_names (void)
9092 {
9093   struct file_name_acquire_data fnad;
9094   int numfiles;
9095   struct file_info *files;
9096   struct dir_info *dirs;
9097   int *saved;
9098   int *savehere;
9099   int *backmap;
9100   int ndirs;
9101   int idx_offset;
9102   int i;
9103   int idx;
9104
9105   if (!last_emitted_file)
9106     {
9107       dw2_asm_output_data (1, 0, "End directory table");
9108       dw2_asm_output_data (1, 0, "End file name table");
9109       return;
9110     }
9111
9112   numfiles = last_emitted_file->emitted_number;
9113
9114   /* Allocate the various arrays we need.  */
9115   files = XALLOCAVEC (struct file_info, numfiles);
9116   dirs = XALLOCAVEC (struct dir_info, numfiles);
9117
9118   fnad.files = files;
9119   fnad.used_files = 0;
9120   fnad.max_files = numfiles;
9121   htab_traverse (file_table, file_name_acquire, &fnad);
9122   gcc_assert (fnad.used_files == fnad.max_files);
9123
9124   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9125
9126   /* Find all the different directories used.  */
9127   dirs[0].path = files[0].path;
9128   dirs[0].length = files[0].fname - files[0].path;
9129   dirs[0].prefix = -1;
9130   dirs[0].count = 1;
9131   dirs[0].dir_idx = 0;
9132   files[0].dir_idx = 0;
9133   ndirs = 1;
9134
9135   for (i = 1; i < numfiles; i++)
9136     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9137         && memcmp (dirs[ndirs - 1].path, files[i].path,
9138                    dirs[ndirs - 1].length) == 0)
9139       {
9140         /* Same directory as last entry.  */
9141         files[i].dir_idx = ndirs - 1;
9142         ++dirs[ndirs - 1].count;
9143       }
9144     else
9145       {
9146         int j;
9147
9148         /* This is a new directory.  */
9149         dirs[ndirs].path = files[i].path;
9150         dirs[ndirs].length = files[i].fname - files[i].path;
9151         dirs[ndirs].count = 1;
9152         dirs[ndirs].dir_idx = ndirs;
9153         files[i].dir_idx = ndirs;
9154
9155         /* Search for a prefix.  */
9156         dirs[ndirs].prefix = -1;
9157         for (j = 0; j < ndirs; j++)
9158           if (dirs[j].length < dirs[ndirs].length
9159               && dirs[j].length > 1
9160               && (dirs[ndirs].prefix == -1
9161                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9162               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9163             dirs[ndirs].prefix = j;
9164
9165         ++ndirs;
9166       }
9167
9168   /* Now to the actual work.  We have to find a subset of the directories which
9169      allow expressing the file name using references to the directory table
9170      with the least amount of characters.  We do not do an exhaustive search
9171      where we would have to check out every combination of every single
9172      possible prefix.  Instead we use a heuristic which provides nearly optimal
9173      results in most cases and never is much off.  */
9174   saved = XALLOCAVEC (int, ndirs);
9175   savehere = XALLOCAVEC (int, ndirs);
9176
9177   memset (saved, '\0', ndirs * sizeof (saved[0]));
9178   for (i = 0; i < ndirs; i++)
9179     {
9180       int j;
9181       int total;
9182
9183       /* We can always save some space for the current directory.  But this
9184          does not mean it will be enough to justify adding the directory.  */
9185       savehere[i] = dirs[i].length;
9186       total = (savehere[i] - saved[i]) * dirs[i].count;
9187
9188       for (j = i + 1; j < ndirs; j++)
9189         {
9190           savehere[j] = 0;
9191           if (saved[j] < dirs[i].length)
9192             {
9193               /* Determine whether the dirs[i] path is a prefix of the
9194                  dirs[j] path.  */
9195               int k;
9196
9197               k = dirs[j].prefix;
9198               while (k != -1 && k != (int) i)
9199                 k = dirs[k].prefix;
9200
9201               if (k == (int) i)
9202                 {
9203                   /* Yes it is.  We can possibly save some memory by
9204                      writing the filenames in dirs[j] relative to
9205                      dirs[i].  */
9206                   savehere[j] = dirs[i].length;
9207                   total += (savehere[j] - saved[j]) * dirs[j].count;
9208                 }
9209             }
9210         }
9211
9212       /* Check whether we can save enough to justify adding the dirs[i]
9213          directory.  */
9214       if (total > dirs[i].length + 1)
9215         {
9216           /* It's worthwhile adding.  */
9217           for (j = i; j < ndirs; j++)
9218             if (savehere[j] > 0)
9219               {
9220                 /* Remember how much we saved for this directory so far.  */
9221                 saved[j] = savehere[j];
9222
9223                 /* Remember the prefix directory.  */
9224                 dirs[j].dir_idx = i;
9225               }
9226         }
9227     }
9228
9229   /* Emit the directory name table.  */
9230   idx = 1;
9231   idx_offset = dirs[0].length > 0 ? 1 : 0;
9232   for (i = 1 - idx_offset; i < ndirs; i++)
9233     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
9234                             "Directory Entry: 0x%x", i + idx_offset);
9235
9236   dw2_asm_output_data (1, 0, "End directory table");
9237
9238   /* We have to emit them in the order of emitted_number since that's
9239      used in the debug info generation.  To do this efficiently we
9240      generate a back-mapping of the indices first.  */
9241   backmap = XALLOCAVEC (int, numfiles);
9242   for (i = 0; i < numfiles; i++)
9243     backmap[files[i].file_idx->emitted_number - 1] = i;
9244
9245   /* Now write all the file names.  */
9246   for (i = 0; i < numfiles; i++)
9247     {
9248       int file_idx = backmap[i];
9249       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9250
9251       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9252                               "File Entry: 0x%x", (unsigned) i + 1);
9253
9254       /* Include directory index.  */
9255       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9256
9257       /* Modification time.  */
9258       dw2_asm_output_data_uleb128 (0, NULL);
9259
9260       /* File length in bytes.  */
9261       dw2_asm_output_data_uleb128 (0, NULL);
9262     }
9263
9264   dw2_asm_output_data (1, 0, "End file name table");
9265 }
9266
9267
9268 /* Output the source line number correspondence information.  This
9269    information goes into the .debug_line section.  */
9270
9271 static void
9272 output_line_info (void)
9273 {
9274   char l1[20], l2[20], p1[20], p2[20];
9275   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9276   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9277   unsigned opc;
9278   unsigned n_op_args;
9279   unsigned long lt_index;
9280   unsigned long current_line;
9281   long line_offset;
9282   long line_delta;
9283   unsigned long current_file;
9284   unsigned long function;
9285
9286   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9287   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9288   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9289   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9290
9291   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9292     dw2_asm_output_data (4, 0xffffffff,
9293       "Initial length escape value indicating 64-bit DWARF extension");
9294   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9295                         "Length of Source Line Info");
9296   ASM_OUTPUT_LABEL (asm_out_file, l1);
9297
9298   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
9299   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9300   ASM_OUTPUT_LABEL (asm_out_file, p1);
9301
9302   /* Define the architecture-dependent minimum instruction length (in
9303    bytes).  In this implementation of DWARF, this field is used for
9304    information purposes only.  Since GCC generates assembly language,
9305    we have no a priori knowledge of how many instruction bytes are
9306    generated for each source line, and therefore can use only the
9307    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9308    commands.  Accordingly, we fix this as `1', which is "correct
9309    enough" for all architectures, and don't let the target override.  */
9310   dw2_asm_output_data (1, 1,
9311                        "Minimum Instruction Length");
9312
9313   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9314                        "Default is_stmt_start flag");
9315   dw2_asm_output_data (1, DWARF_LINE_BASE,
9316                        "Line Base Value (Special Opcodes)");
9317   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9318                        "Line Range Value (Special Opcodes)");
9319   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9320                        "Special Opcode Base");
9321
9322   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9323     {
9324       switch (opc)
9325         {
9326         case DW_LNS_advance_pc:
9327         case DW_LNS_advance_line:
9328         case DW_LNS_set_file:
9329         case DW_LNS_set_column:
9330         case DW_LNS_fixed_advance_pc:
9331           n_op_args = 1;
9332           break;
9333         default:
9334           n_op_args = 0;
9335           break;
9336         }
9337
9338       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9339                            opc, n_op_args);
9340     }
9341
9342   /* Write out the information about the files we use.  */
9343   output_file_names ();
9344   ASM_OUTPUT_LABEL (asm_out_file, p2);
9345
9346   /* We used to set the address register to the first location in the text
9347      section here, but that didn't accomplish anything since we already
9348      have a line note for the opening brace of the first function.  */
9349
9350   /* Generate the line number to PC correspondence table, encoded as
9351      a series of state machine operations.  */
9352   current_file = 1;
9353   current_line = 1;
9354
9355   if (cfun && in_cold_section_p)
9356     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9357   else
9358     strcpy (prev_line_label, text_section_label);
9359   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9360     {
9361       dw_line_info_ref line_info = &line_info_table[lt_index];
9362
9363 #if 0
9364       /* Disable this optimization for now; GDB wants to see two line notes
9365          at the beginning of a function so it can find the end of the
9366          prologue.  */
9367
9368       /* Don't emit anything for redundant notes.  Just updating the
9369          address doesn't accomplish anything, because we already assume
9370          that anything after the last address is this line.  */
9371       if (line_info->dw_line_num == current_line
9372           && line_info->dw_file_num == current_file)
9373         continue;
9374 #endif
9375
9376       /* Emit debug info for the address of the current line.
9377
9378          Unfortunately, we have little choice here currently, and must always
9379          use the most general form.  GCC does not know the address delta
9380          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9381          attributes which will give an upper bound on the address range.  We
9382          could perhaps use length attributes to determine when it is safe to
9383          use DW_LNS_fixed_advance_pc.  */
9384
9385       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9386       if (0)
9387         {
9388           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9389           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9390                                "DW_LNS_fixed_advance_pc");
9391           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9392         }
9393       else
9394         {
9395           /* This can handle any delta.  This takes
9396              4+DWARF2_ADDR_SIZE bytes.  */
9397           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9398           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9399           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9400           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9401         }
9402
9403       strcpy (prev_line_label, line_label);
9404
9405       /* Emit debug info for the source file of the current line, if
9406          different from the previous line.  */
9407       if (line_info->dw_file_num != current_file)
9408         {
9409           current_file = line_info->dw_file_num;
9410           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9411           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9412         }
9413
9414       /* Emit debug info for the current line number, choosing the encoding
9415          that uses the least amount of space.  */
9416       if (line_info->dw_line_num != current_line)
9417         {
9418           line_offset = line_info->dw_line_num - current_line;
9419           line_delta = line_offset - DWARF_LINE_BASE;
9420           current_line = line_info->dw_line_num;
9421           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9422             /* This can handle deltas from -10 to 234, using the current
9423                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
9424                takes 1 byte.  */
9425             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9426                                  "line %lu", current_line);
9427           else
9428             {
9429               /* This can handle any delta.  This takes at least 4 bytes,
9430                  depending on the value being encoded.  */
9431               dw2_asm_output_data (1, DW_LNS_advance_line,
9432                                    "advance to line %lu", current_line);
9433               dw2_asm_output_data_sleb128 (line_offset, NULL);
9434               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9435             }
9436         }
9437       else
9438         /* We still need to start a new row, so output a copy insn.  */
9439         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9440     }
9441
9442   /* Emit debug info for the address of the end of the function.  */
9443   if (0)
9444     {
9445       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9446                            "DW_LNS_fixed_advance_pc");
9447       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
9448     }
9449   else
9450     {
9451       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9452       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9453       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9454       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
9455     }
9456
9457   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9458   dw2_asm_output_data_uleb128 (1, NULL);
9459   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9460
9461   function = 0;
9462   current_file = 1;
9463   current_line = 1;
9464   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
9465     {
9466       dw_separate_line_info_ref line_info
9467         = &separate_line_info_table[lt_index];
9468
9469 #if 0
9470       /* Don't emit anything for redundant notes.  */
9471       if (line_info->dw_line_num == current_line
9472           && line_info->dw_file_num == current_file
9473           && line_info->function == function)
9474         goto cont;
9475 #endif
9476
9477       /* Emit debug info for the address of the current line.  If this is
9478          a new function, or the first line of a function, then we need
9479          to handle it differently.  */
9480       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
9481                                    lt_index);
9482       if (function != line_info->function)
9483         {
9484           function = line_info->function;
9485
9486           /* Set the address register to the first line in the function.  */
9487           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9488           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9489           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9490           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9491         }
9492       else
9493         {
9494           /* ??? See the DW_LNS_advance_pc comment above.  */
9495           if (0)
9496             {
9497               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9498                                    "DW_LNS_fixed_advance_pc");
9499               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9500             }
9501           else
9502             {
9503               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9504               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9505               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9506               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9507             }
9508         }
9509
9510       strcpy (prev_line_label, line_label);
9511
9512       /* Emit debug info for the source file of the current line, if
9513          different from the previous line.  */
9514       if (line_info->dw_file_num != current_file)
9515         {
9516           current_file = line_info->dw_file_num;
9517           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9518           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9519         }
9520
9521       /* Emit debug info for the current line number, choosing the encoding
9522          that uses the least amount of space.  */
9523       if (line_info->dw_line_num != current_line)
9524         {
9525           line_offset = line_info->dw_line_num - current_line;
9526           line_delta = line_offset - DWARF_LINE_BASE;
9527           current_line = line_info->dw_line_num;
9528           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9529             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9530                                  "line %lu", current_line);
9531           else
9532             {
9533               dw2_asm_output_data (1, DW_LNS_advance_line,
9534                                    "advance to line %lu", current_line);
9535               dw2_asm_output_data_sleb128 (line_offset, NULL);
9536               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9537             }
9538         }
9539       else
9540         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
9541
9542 #if 0
9543     cont:
9544 #endif
9545
9546       lt_index++;
9547
9548       /* If we're done with a function, end its sequence.  */
9549       if (lt_index == separate_line_info_table_in_use
9550           || separate_line_info_table[lt_index].function != function)
9551         {
9552           current_file = 1;
9553           current_line = 1;
9554
9555           /* Emit debug info for the address of the end of the function.  */
9556           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
9557           if (0)
9558             {
9559               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9560                                    "DW_LNS_fixed_advance_pc");
9561               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9562             }
9563           else
9564             {
9565               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9566               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9567               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9568               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9569             }
9570
9571           /* Output the marker for the end of this sequence.  */
9572           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
9573           dw2_asm_output_data_uleb128 (1, NULL);
9574           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9575         }
9576     }
9577
9578   /* Output the marker for the end of the line number info.  */
9579   ASM_OUTPUT_LABEL (asm_out_file, l2);
9580 }
9581 \f
9582 /* Given a pointer to a tree node for some base type, return a pointer to
9583    a DIE that describes the given type.
9584
9585    This routine must only be called for GCC type nodes that correspond to
9586    Dwarf base (fundamental) types.  */
9587
9588 static dw_die_ref
9589 base_type_die (tree type)
9590 {
9591   dw_die_ref base_type_result;
9592   enum dwarf_type encoding;
9593
9594   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9595     return 0;
9596
9597   /* If this is a subtype that should not be emitted as a subrange type,
9598      use the base type.  See subrange_type_for_debug_p.  */
9599   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9600     type = TREE_TYPE (type);
9601
9602   switch (TREE_CODE (type))
9603     {
9604     case INTEGER_TYPE:
9605       if (TYPE_STRING_FLAG (type))
9606         {
9607           if (TYPE_UNSIGNED (type))
9608             encoding = DW_ATE_unsigned_char;
9609           else
9610             encoding = DW_ATE_signed_char;
9611         }
9612       else if (TYPE_UNSIGNED (type))
9613         encoding = DW_ATE_unsigned;
9614       else
9615         encoding = DW_ATE_signed;
9616       break;
9617
9618     case REAL_TYPE:
9619       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9620         encoding = DW_ATE_decimal_float;
9621       else
9622         encoding = DW_ATE_float;
9623       break;
9624
9625     case FIXED_POINT_TYPE:
9626       if (TYPE_UNSIGNED (type))
9627         encoding = DW_ATE_unsigned_fixed;
9628       else
9629         encoding = DW_ATE_signed_fixed;
9630       break;
9631
9632       /* Dwarf2 doesn't know anything about complex ints, so use
9633          a user defined type for it.  */
9634     case COMPLEX_TYPE:
9635       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9636         encoding = DW_ATE_complex_float;
9637       else
9638         encoding = DW_ATE_lo_user;
9639       break;
9640
9641     case BOOLEAN_TYPE:
9642       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9643       encoding = DW_ATE_boolean;
9644       break;
9645
9646     default:
9647       /* No other TREE_CODEs are Dwarf fundamental types.  */
9648       gcc_unreachable ();
9649     }
9650
9651   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
9652
9653   /* This probably indicates a bug.  */
9654   if (! TYPE_NAME (type))
9655     add_name_attribute (base_type_result, "__unknown__");
9656
9657   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9658                    int_size_in_bytes (type));
9659   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9660
9661   return base_type_result;
9662 }
9663
9664 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9665    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9666
9667 static inline int
9668 is_base_type (tree type)
9669 {
9670   switch (TREE_CODE (type))
9671     {
9672     case ERROR_MARK:
9673     case VOID_TYPE:
9674     case INTEGER_TYPE:
9675     case REAL_TYPE:
9676     case FIXED_POINT_TYPE:
9677     case COMPLEX_TYPE:
9678     case BOOLEAN_TYPE:
9679       return 1;
9680
9681     case ARRAY_TYPE:
9682     case RECORD_TYPE:
9683     case UNION_TYPE:
9684     case QUAL_UNION_TYPE:
9685     case ENUMERAL_TYPE:
9686     case FUNCTION_TYPE:
9687     case METHOD_TYPE:
9688     case POINTER_TYPE:
9689     case REFERENCE_TYPE:
9690     case OFFSET_TYPE:
9691     case LANG_TYPE:
9692     case VECTOR_TYPE:
9693       return 0;
9694
9695     default:
9696       gcc_unreachable ();
9697     }
9698
9699   return 0;
9700 }
9701
9702 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9703    node, return the size in bits for the type if it is a constant, or else
9704    return the alignment for the type if the type's size is not constant, or
9705    else return BITS_PER_WORD if the type actually turns out to be an
9706    ERROR_MARK node.  */
9707
9708 static inline unsigned HOST_WIDE_INT
9709 simple_type_size_in_bits (const_tree type)
9710 {
9711   if (TREE_CODE (type) == ERROR_MARK)
9712     return BITS_PER_WORD;
9713   else if (TYPE_SIZE (type) == NULL_TREE)
9714     return 0;
9715   else if (host_integerp (TYPE_SIZE (type), 1))
9716     return tree_low_cst (TYPE_SIZE (type), 1);
9717   else
9718     return TYPE_ALIGN (type);
9719 }
9720
9721 /*  Given a pointer to a tree node for a subrange type, return a pointer
9722     to a DIE that describes the given type.  */
9723
9724 static dw_die_ref
9725 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9726 {
9727   dw_die_ref subrange_die;
9728   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9729
9730   if (context_die == NULL)
9731     context_die = comp_unit_die;
9732
9733   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9734
9735   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9736     {
9737       /* The size of the subrange type and its base type do not match,
9738          so we need to generate a size attribute for the subrange type.  */
9739       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9740     }
9741
9742   if (low)
9743     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9744   if (high)
9745     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9746
9747   return subrange_die;
9748 }
9749
9750 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9751    entry that chains various modifiers in front of the given type.  */
9752
9753 static dw_die_ref
9754 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9755                    dw_die_ref context_die)
9756 {
9757   enum tree_code code = TREE_CODE (type);
9758   dw_die_ref mod_type_die;
9759   dw_die_ref sub_die = NULL;
9760   tree item_type = NULL;
9761   tree qualified_type;
9762   tree name, low, high;
9763
9764   if (code == ERROR_MARK)
9765     return NULL;
9766
9767   /* See if we already have the appropriately qualified variant of
9768      this type.  */
9769   qualified_type
9770     = get_qualified_type (type,
9771                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9772                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9773
9774   /* If we do, then we can just use its DIE, if it exists.  */
9775   if (qualified_type)
9776     {
9777       mod_type_die = lookup_type_die (qualified_type);
9778       if (mod_type_die)
9779         return mod_type_die;
9780     }
9781
9782   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9783
9784   /* Handle C typedef types.  */
9785   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
9786     {
9787       tree dtype = TREE_TYPE (name);
9788
9789       if (qualified_type == dtype)
9790         {
9791           /* For a named type, use the typedef.  */
9792           gen_type_die (qualified_type, context_die);
9793           return lookup_type_die (qualified_type);
9794         }
9795       else if (is_const_type < TYPE_READONLY (dtype)
9796                || is_volatile_type < TYPE_VOLATILE (dtype)
9797                || (is_const_type <= TYPE_READONLY (dtype)
9798                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9799                    && DECL_ORIGINAL_TYPE (name) != type))
9800         /* cv-unqualified version of named type.  Just use the unnamed
9801            type to which it refers.  */
9802         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9803                                   is_const_type, is_volatile_type,
9804                                   context_die);
9805       /* Else cv-qualified version of named type; fall through.  */
9806     }
9807
9808   if (is_const_type)
9809     {
9810       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
9811       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9812     }
9813   else if (is_volatile_type)
9814     {
9815       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
9816       sub_die = modified_type_die (type, 0, 0, context_die);
9817     }
9818   else if (code == POINTER_TYPE)
9819     {
9820       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
9821       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9822                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9823       item_type = TREE_TYPE (type);
9824     }
9825   else if (code == REFERENCE_TYPE)
9826     {
9827       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
9828       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9829                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9830       item_type = TREE_TYPE (type);
9831     }
9832   else if (code == INTEGER_TYPE
9833            && TREE_TYPE (type) != NULL_TREE
9834            && subrange_type_for_debug_p (type, &low, &high))
9835     {
9836       mod_type_die = subrange_type_die (type, low, high, context_die);
9837       item_type = TREE_TYPE (type);
9838     }
9839   else if (is_base_type (type))
9840     mod_type_die = base_type_die (type);
9841   else
9842     {
9843       gen_type_die (type, context_die);
9844
9845       /* We have to get the type_main_variant here (and pass that to the
9846          `lookup_type_die' routine) because the ..._TYPE node we have
9847          might simply be a *copy* of some original type node (where the
9848          copy was created to help us keep track of typedef names) and
9849          that copy might have a different TYPE_UID from the original
9850          ..._TYPE node.  */
9851       if (TREE_CODE (type) != VECTOR_TYPE)
9852         return lookup_type_die (type_main_variant (type));
9853       else
9854         /* Vectors have the debugging information in the type,
9855            not the main variant.  */
9856         return lookup_type_die (type);
9857     }
9858
9859   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9860      don't output a DW_TAG_typedef, since there isn't one in the
9861      user's program; just attach a DW_AT_name to the type.  */
9862   if (name
9863       && (TREE_CODE (name) != TYPE_DECL
9864           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
9865     {
9866       if (TREE_CODE (name) == TYPE_DECL)
9867         /* Could just call add_name_and_src_coords_attributes here,
9868            but since this is a builtin type it doesn't have any
9869            useful source coordinates anyway.  */
9870         name = DECL_NAME (name);
9871       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9872     }
9873
9874   if (qualified_type)
9875     equate_type_number_to_die (qualified_type, mod_type_die);
9876
9877   if (item_type)
9878     /* We must do this after the equate_type_number_to_die call, in case
9879        this is a recursive type.  This ensures that the modified_type_die
9880        recursion will terminate even if the type is recursive.  Recursive
9881        types are possible in Ada.  */
9882     sub_die = modified_type_die (item_type,
9883                                  TYPE_READONLY (item_type),
9884                                  TYPE_VOLATILE (item_type),
9885                                  context_die);
9886
9887   if (sub_die != NULL)
9888     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9889
9890   return mod_type_die;
9891 }
9892
9893 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9894    an enumerated type.  */
9895
9896 static inline int
9897 type_is_enum (const_tree type)
9898 {
9899   return TREE_CODE (type) == ENUMERAL_TYPE;
9900 }
9901
9902 /* Return the DBX register number described by a given RTL node.  */
9903
9904 static unsigned int
9905 dbx_reg_number (const_rtx rtl)
9906 {
9907   unsigned regno = REGNO (rtl);
9908
9909   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9910
9911 #ifdef LEAF_REG_REMAP
9912   if (current_function_uses_only_leaf_regs)
9913     {
9914       int leaf_reg = LEAF_REG_REMAP (regno);
9915       if (leaf_reg != -1)
9916         regno = (unsigned) leaf_reg;
9917     }
9918 #endif
9919
9920   return DBX_REGISTER_NUMBER (regno);
9921 }
9922
9923 /* Optionally add a DW_OP_piece term to a location description expression.
9924    DW_OP_piece is only added if the location description expression already
9925    doesn't end with DW_OP_piece.  */
9926
9927 static void
9928 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9929 {
9930   dw_loc_descr_ref loc;
9931
9932   if (*list_head != NULL)
9933     {
9934       /* Find the end of the chain.  */
9935       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9936         ;
9937
9938       if (loc->dw_loc_opc != DW_OP_piece)
9939         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9940     }
9941 }
9942
9943 /* Return a location descriptor that designates a machine register or
9944    zero if there is none.  */
9945
9946 static dw_loc_descr_ref
9947 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9948 {
9949   rtx regs;
9950
9951   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9952     return 0;
9953
9954   regs = targetm.dwarf_register_span (rtl);
9955
9956   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9957     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9958   else
9959     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9960 }
9961
9962 /* Return a location descriptor that designates a machine register for
9963    a given hard register number.  */
9964
9965 static dw_loc_descr_ref
9966 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9967 {
9968   dw_loc_descr_ref reg_loc_descr;
9969
9970   if (regno <= 31)
9971     reg_loc_descr
9972       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
9973   else
9974     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
9975
9976   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9977     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9978
9979   return reg_loc_descr;
9980 }
9981
9982 /* Given an RTL of a register, return a location descriptor that
9983    designates a value that spans more than one register.  */
9984
9985 static dw_loc_descr_ref
9986 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9987                              enum var_init_status initialized)
9988 {
9989   int nregs, size, i;
9990   unsigned reg;
9991   dw_loc_descr_ref loc_result = NULL;
9992
9993   reg = REGNO (rtl);
9994 #ifdef LEAF_REG_REMAP
9995   if (current_function_uses_only_leaf_regs)
9996     {
9997       int leaf_reg = LEAF_REG_REMAP (reg);
9998       if (leaf_reg != -1)
9999         reg = (unsigned) leaf_reg;
10000     }
10001 #endif
10002   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10003   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10004
10005   /* Simple, contiguous registers.  */
10006   if (regs == NULL_RTX)
10007     {
10008       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10009
10010       loc_result = NULL;
10011       while (nregs--)
10012         {
10013           dw_loc_descr_ref t;
10014
10015           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10016                                       VAR_INIT_STATUS_INITIALIZED);
10017           add_loc_descr (&loc_result, t);
10018           add_loc_descr_op_piece (&loc_result, size);
10019           ++reg;
10020         }
10021       return loc_result;
10022     }
10023
10024   /* Now onto stupid register sets in non contiguous locations.  */
10025
10026   gcc_assert (GET_CODE (regs) == PARALLEL);
10027
10028   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10029   loc_result = NULL;
10030
10031   for (i = 0; i < XVECLEN (regs, 0); ++i)
10032     {
10033       dw_loc_descr_ref t;
10034
10035       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10036                                   VAR_INIT_STATUS_INITIALIZED);
10037       add_loc_descr (&loc_result, t);
10038       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10039       add_loc_descr_op_piece (&loc_result, size);
10040     }
10041
10042   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10043     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10044   return loc_result;
10045 }
10046
10047 #endif /* DWARF2_DEBUGGING_INFO */
10048
10049 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10050
10051 /* Return a location descriptor that designates a constant.  */
10052
10053 static dw_loc_descr_ref
10054 int_loc_descriptor (HOST_WIDE_INT i)
10055 {
10056   enum dwarf_location_atom op;
10057
10058   /* Pick the smallest representation of a constant, rather than just
10059      defaulting to the LEB encoding.  */
10060   if (i >= 0)
10061     {
10062       if (i <= 31)
10063         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10064       else if (i <= 0xff)
10065         op = DW_OP_const1u;
10066       else if (i <= 0xffff)
10067         op = DW_OP_const2u;
10068       else if (HOST_BITS_PER_WIDE_INT == 32
10069                || i <= 0xffffffff)
10070         op = DW_OP_const4u;
10071       else
10072         op = DW_OP_constu;
10073     }
10074   else
10075     {
10076       if (i >= -0x80)
10077         op = DW_OP_const1s;
10078       else if (i >= -0x8000)
10079         op = DW_OP_const2s;
10080       else if (HOST_BITS_PER_WIDE_INT == 32
10081                || i >= -0x80000000)
10082         op = DW_OP_const4s;
10083       else
10084         op = DW_OP_consts;
10085     }
10086
10087   return new_loc_descr (op, i, 0);
10088 }
10089 #endif
10090
10091 #ifdef DWARF2_DEBUGGING_INFO
10092
10093 /* Return a location descriptor that designates a base+offset location.  */
10094
10095 static dw_loc_descr_ref
10096 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10097                  enum var_init_status initialized)
10098 {
10099   unsigned int regno;
10100   dw_loc_descr_ref result;
10101   dw_fde_ref fde = current_fde ();
10102
10103   /* We only use "frame base" when we're sure we're talking about the
10104      post-prologue local stack frame.  We do this by *not* running
10105      register elimination until this point, and recognizing the special
10106      argument pointer and soft frame pointer rtx's.  */
10107   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10108     {
10109       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10110
10111       if (elim != reg)
10112         {
10113           if (GET_CODE (elim) == PLUS)
10114             {
10115               offset += INTVAL (XEXP (elim, 1));
10116               elim = XEXP (elim, 0);
10117             }
10118           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10119                        && (elim == hard_frame_pointer_rtx
10120                            || elim == stack_pointer_rtx))
10121                       || elim == (frame_pointer_needed
10122                                   ? hard_frame_pointer_rtx
10123                                   : stack_pointer_rtx));
10124
10125           /* If drap register is used to align stack, use frame
10126              pointer + offset to access stack variables.  If stack
10127              is aligned without drap, use stack pointer + offset to
10128              access stack variables.  */
10129           if (crtl->stack_realign_tried
10130               && cfa.reg == HARD_FRAME_POINTER_REGNUM
10131               && reg == frame_pointer_rtx)
10132             {
10133               int base_reg
10134                 = DWARF_FRAME_REGNUM (cfa.indirect
10135                                       ? HARD_FRAME_POINTER_REGNUM
10136                                       : STACK_POINTER_REGNUM);
10137               return new_reg_loc_descr (base_reg, offset);
10138             }
10139
10140           offset += frame_pointer_fb_offset;
10141           return new_loc_descr (DW_OP_fbreg, offset, 0);
10142         }
10143     }
10144   else if (fde
10145            && fde->drap_reg != INVALID_REGNUM
10146            && (fde->drap_reg == REGNO (reg)
10147                || fde->vdrap_reg == REGNO (reg)))
10148     {
10149       /* Use cfa+offset to represent the location of arguments passed
10150          on stack when drap is used to align stack.  */
10151       return new_loc_descr (DW_OP_fbreg, offset, 0);
10152     }
10153
10154   regno = dbx_reg_number (reg);
10155   if (regno <= 31)
10156     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10157                             offset, 0);
10158   else
10159     result = new_loc_descr (DW_OP_bregx, regno, offset);
10160
10161   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10162     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10163
10164   return result;
10165 }
10166
10167 /* Return true if this RTL expression describes a base+offset calculation.  */
10168
10169 static inline int
10170 is_based_loc (const_rtx rtl)
10171 {
10172   return (GET_CODE (rtl) == PLUS
10173           && ((REG_P (XEXP (rtl, 0))
10174                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10175                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
10176 }
10177
10178 /* Return a descriptor that describes the concatenation of N locations
10179    used to form the address of a memory location.  */
10180
10181 static dw_loc_descr_ref
10182 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10183                             enum var_init_status initialized)
10184 {
10185   unsigned int i;
10186   dw_loc_descr_ref cc_loc_result = NULL;
10187   unsigned int n = XVECLEN (concatn, 0);
10188
10189   for (i = 0; i < n; ++i)
10190     {
10191       dw_loc_descr_ref ref;
10192       rtx x = XVECEXP (concatn, 0, i);
10193
10194       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10195       if (ref == NULL)
10196         return NULL;
10197
10198       add_loc_descr (&cc_loc_result, ref);
10199       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10200     }
10201
10202   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10203     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10204
10205   return cc_loc_result;
10206 }
10207
10208 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10209    failed.  */
10210
10211 static dw_loc_descr_ref
10212 tls_mem_loc_descriptor (rtx mem)
10213 {
10214   tree base;
10215   dw_loc_descr_ref loc_result;
10216
10217   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10218     return NULL;
10219
10220   base = get_base_address (MEM_EXPR (mem));
10221   if (base == NULL
10222       || TREE_CODE (base) != VAR_DECL
10223       || !DECL_THREAD_LOCAL_P (base))
10224     return NULL;
10225
10226   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10227   if (loc_result == NULL)
10228     return NULL;
10229
10230   if (INTVAL (MEM_OFFSET (mem)))
10231     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10232
10233   return loc_result;
10234 }
10235
10236 /* The following routine converts the RTL for a variable or parameter
10237    (resident in memory) into an equivalent Dwarf representation of a
10238    mechanism for getting the address of that same variable onto the top of a
10239    hypothetical "address evaluation" stack.
10240
10241    When creating memory location descriptors, we are effectively transforming
10242    the RTL for a memory-resident object into its Dwarf postfix expression
10243    equivalent.  This routine recursively descends an RTL tree, turning
10244    it into Dwarf postfix code as it goes.
10245
10246    MODE is the mode of the memory reference, needed to handle some
10247    autoincrement addressing modes.
10248
10249    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
10250    location list for RTL.
10251
10252    Return 0 if we can't represent the location.  */
10253
10254 static dw_loc_descr_ref
10255 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
10256                     enum var_init_status initialized)
10257 {
10258   dw_loc_descr_ref mem_loc_result = NULL;
10259   enum dwarf_location_atom op;
10260
10261   /* Note that for a dynamically sized array, the location we will generate a
10262      description of here will be the lowest numbered location which is
10263      actually within the array.  That's *not* necessarily the same as the
10264      zeroth element of the array.  */
10265
10266   rtl = targetm.delegitimize_address (rtl);
10267
10268   switch (GET_CODE (rtl))
10269     {
10270     case POST_INC:
10271     case POST_DEC:
10272     case POST_MODIFY:
10273       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
10274          just fall into the SUBREG code.  */
10275
10276       /* ... fall through ...  */
10277
10278     case SUBREG:
10279       /* The case of a subreg may arise when we have a local (register)
10280          variable or a formal (register) parameter which doesn't quite fill
10281          up an entire register.  For now, just assume that it is
10282          legitimate to make the Dwarf info refer to the whole register which
10283          contains the given subreg.  */
10284       rtl = XEXP (rtl, 0);
10285
10286       /* ... fall through ...  */
10287
10288     case REG:
10289       /* Whenever a register number forms a part of the description of the
10290          method for calculating the (dynamic) address of a memory resident
10291          object, DWARF rules require the register number be referred to as
10292          a "base register".  This distinction is not based in any way upon
10293          what category of register the hardware believes the given register
10294          belongs to.  This is strictly DWARF terminology we're dealing with
10295          here. Note that in cases where the location of a memory-resident
10296          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
10297          OP_CONST (0)) the actual DWARF location descriptor that we generate
10298          may just be OP_BASEREG (basereg).  This may look deceptively like
10299          the object in question was allocated to a register (rather than in
10300          memory) so DWARF consumers need to be aware of the subtle
10301          distinction between OP_REG and OP_BASEREG.  */
10302       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
10303         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
10304       else if (stack_realign_drap
10305                && crtl->drap_reg
10306                && crtl->args.internal_arg_pointer == rtl
10307                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
10308         {
10309           /* If RTL is internal_arg_pointer, which has been optimized
10310              out, use DRAP instead.  */
10311           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
10312                                             VAR_INIT_STATUS_INITIALIZED);
10313         }
10314       break;
10315
10316     case MEM:
10317       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10318                                            VAR_INIT_STATUS_INITIALIZED);
10319       if (mem_loc_result == NULL)
10320         mem_loc_result = tls_mem_loc_descriptor (rtl);
10321       if (mem_loc_result != 0)
10322         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
10323       break;
10324
10325     case LO_SUM:
10326          rtl = XEXP (rtl, 1);
10327
10328       /* ... fall through ...  */
10329
10330     case LABEL_REF:
10331       /* Some ports can transform a symbol ref into a label ref, because
10332          the symbol ref is too far away and has to be dumped into a constant
10333          pool.  */
10334     case CONST:
10335     case SYMBOL_REF:
10336       /* Alternatively, the symbol in the constant pool might be referenced
10337          by a different symbol.  */
10338       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
10339         {
10340           bool marked;
10341           rtx tmp = get_pool_constant_mark (rtl, &marked);
10342
10343           if (GET_CODE (tmp) == SYMBOL_REF)
10344             {
10345               rtl = tmp;
10346               if (CONSTANT_POOL_ADDRESS_P (tmp))
10347                 get_pool_constant_mark (tmp, &marked);
10348               else
10349                 marked = true;
10350             }
10351
10352           /* If all references to this pool constant were optimized away,
10353              it was not output and thus we can't represent it.
10354              FIXME: might try to use DW_OP_const_value here, though
10355              DW_OP_piece complicates it.  */
10356           if (!marked)
10357             return 0;
10358         }
10359
10360       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
10361       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
10362       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
10363       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10364       break;
10365
10366     case PRE_MODIFY:
10367       /* Extract the PLUS expression nested inside and fall into
10368          PLUS code below.  */
10369       rtl = XEXP (rtl, 1);
10370       goto plus;
10371
10372     case PRE_INC:
10373     case PRE_DEC:
10374       /* Turn these into a PLUS expression and fall into the PLUS code
10375          below.  */
10376       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
10377                           GEN_INT (GET_CODE (rtl) == PRE_INC
10378                                    ? GET_MODE_UNIT_SIZE (mode)
10379                                    : -GET_MODE_UNIT_SIZE (mode)));
10380
10381       /* ... fall through ...  */
10382
10383     case PLUS:
10384     plus:
10385       if (is_based_loc (rtl))
10386         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
10387                                           INTVAL (XEXP (rtl, 1)),
10388                                           VAR_INIT_STATUS_INITIALIZED);
10389       else
10390         {
10391           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
10392                                                VAR_INIT_STATUS_INITIALIZED);
10393           if (mem_loc_result == 0)
10394             break;
10395
10396           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT)
10397             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
10398           else
10399             {
10400               dw_loc_descr_ref mem_loc_result2
10401                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10402                                       VAR_INIT_STATUS_INITIALIZED);
10403               if (mem_loc_result2 == 0)
10404                 break;
10405               add_loc_descr (&mem_loc_result, mem_loc_result2);
10406               add_loc_descr (&mem_loc_result,
10407                              new_loc_descr (DW_OP_plus, 0, 0));
10408             }
10409         }
10410       break;
10411
10412     /* If a pseudo-reg is optimized away, it is possible for it to
10413        be replaced with a MEM containing a multiply or shift.  */
10414     case MULT:
10415       op = DW_OP_mul;
10416       goto do_binop;
10417
10418     case ASHIFT:
10419       op = DW_OP_shl;
10420       goto do_binop;
10421
10422     case ASHIFTRT:
10423       op = DW_OP_shra;
10424       goto do_binop;
10425
10426     case LSHIFTRT:
10427       op = DW_OP_shr;
10428       goto do_binop;
10429
10430     do_binop:
10431       {
10432         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
10433                                                    VAR_INIT_STATUS_INITIALIZED);
10434         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
10435                                                    VAR_INIT_STATUS_INITIALIZED);
10436
10437         if (op0 == 0 || op1 == 0)
10438           break;
10439
10440         mem_loc_result = op0;
10441         add_loc_descr (&mem_loc_result, op1);
10442         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
10443         break;
10444       }
10445
10446     case CONST_INT:
10447       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
10448       break;
10449
10450     case CONCATN:
10451       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
10452                                                    VAR_INIT_STATUS_INITIALIZED);
10453       break;
10454
10455     case UNSPEC:
10456       /* If delegitimize_address couldn't do anything with the UNSPEC, we
10457          can't express it in the debug info.  This can happen e.g. with some
10458          TLS UNSPECs.  */
10459       break;
10460
10461     default:
10462       gcc_unreachable ();
10463     }
10464
10465   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10466     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10467
10468   return mem_loc_result;
10469 }
10470
10471 /* Return a descriptor that describes the concatenation of two locations.
10472    This is typically a complex variable.  */
10473
10474 static dw_loc_descr_ref
10475 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
10476 {
10477   dw_loc_descr_ref cc_loc_result = NULL;
10478   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
10479   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
10480
10481   if (x0_ref == 0 || x1_ref == 0)
10482     return 0;
10483
10484   cc_loc_result = x0_ref;
10485   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
10486
10487   add_loc_descr (&cc_loc_result, x1_ref);
10488   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
10489
10490   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10491     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10492
10493   return cc_loc_result;
10494 }
10495
10496 /* Return a descriptor that describes the concatenation of N
10497    locations.  */
10498
10499 static dw_loc_descr_ref
10500 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
10501 {
10502   unsigned int i;
10503   dw_loc_descr_ref cc_loc_result = NULL;
10504   unsigned int n = XVECLEN (concatn, 0);
10505
10506   for (i = 0; i < n; ++i)
10507     {
10508       dw_loc_descr_ref ref;
10509       rtx x = XVECEXP (concatn, 0, i);
10510
10511       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
10512       if (ref == NULL)
10513         return NULL;
10514
10515       add_loc_descr (&cc_loc_result, ref);
10516       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10517     }
10518
10519   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10520     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10521
10522   return cc_loc_result;
10523 }
10524
10525 /* Output a proper Dwarf location descriptor for a variable or parameter
10526    which is either allocated in a register or in a memory location.  For a
10527    register, we just generate an OP_REG and the register number.  For a
10528    memory location we provide a Dwarf postfix expression describing how to
10529    generate the (dynamic) address of the object onto the address stack.
10530
10531    If we don't know how to describe it, return 0.  */
10532
10533 static dw_loc_descr_ref
10534 loc_descriptor (rtx rtl, enum var_init_status initialized)
10535 {
10536   dw_loc_descr_ref loc_result = NULL;
10537
10538   switch (GET_CODE (rtl))
10539     {
10540     case SUBREG:
10541       /* The case of a subreg may arise when we have a local (register)
10542          variable or a formal (register) parameter which doesn't quite fill
10543          up an entire register.  For now, just assume that it is
10544          legitimate to make the Dwarf info refer to the whole register which
10545          contains the given subreg.  */
10546       rtl = SUBREG_REG (rtl);
10547
10548       /* ... fall through ...  */
10549
10550     case REG:
10551       loc_result = reg_loc_descriptor (rtl, initialized);
10552       break;
10553
10554     case MEM:
10555       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
10556                                        initialized);
10557       if (loc_result == NULL)
10558         loc_result = tls_mem_loc_descriptor (rtl);
10559       break;
10560
10561     case CONCAT:
10562       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
10563                                           initialized);
10564       break;
10565
10566     case CONCATN:
10567       loc_result = concatn_loc_descriptor (rtl, initialized);
10568       break;
10569
10570     case VAR_LOCATION:
10571       /* Single part.  */
10572       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
10573         {
10574           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
10575           break;
10576         }
10577
10578       rtl = XEXP (rtl, 1);
10579       /* FALLTHRU */
10580
10581     case PARALLEL:
10582       {
10583         rtvec par_elems = XVEC (rtl, 0);
10584         int num_elem = GET_NUM_ELEM (par_elems);
10585         enum machine_mode mode;
10586         int i;
10587
10588         /* Create the first one, so we have something to add to.  */
10589         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
10590                                      initialized);
10591         if (loc_result == NULL)
10592           return NULL;
10593         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
10594         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10595         for (i = 1; i < num_elem; i++)
10596           {
10597             dw_loc_descr_ref temp;
10598
10599             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
10600                                    initialized);
10601             if (temp == NULL)
10602               return NULL;
10603             add_loc_descr (&loc_result, temp);
10604             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
10605             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
10606           }
10607       }
10608       break;
10609
10610     default:
10611       gcc_unreachable ();
10612     }
10613
10614   return loc_result;
10615 }
10616
10617 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
10618    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
10619    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
10620    top-level invocation, and we require the address of LOC; is 0 if we require
10621    the value of LOC.  */
10622
10623 static dw_loc_descr_ref
10624 loc_descriptor_from_tree_1 (tree loc, int want_address)
10625 {
10626   dw_loc_descr_ref ret, ret1;
10627   int have_address = 0;
10628   enum dwarf_location_atom op;
10629
10630   /* ??? Most of the time we do not take proper care for sign/zero
10631      extending the values properly.  Hopefully this won't be a real
10632      problem...  */
10633
10634   switch (TREE_CODE (loc))
10635     {
10636     case ERROR_MARK:
10637       return 0;
10638
10639     case PLACEHOLDER_EXPR:
10640       /* This case involves extracting fields from an object to determine the
10641          position of other fields.  We don't try to encode this here.  The
10642          only user of this is Ada, which encodes the needed information using
10643          the names of types.  */
10644       return 0;
10645
10646     case CALL_EXPR:
10647       return 0;
10648
10649     case PREINCREMENT_EXPR:
10650     case PREDECREMENT_EXPR:
10651     case POSTINCREMENT_EXPR:
10652     case POSTDECREMENT_EXPR:
10653       /* There are no opcodes for these operations.  */
10654       return 0;
10655
10656     case ADDR_EXPR:
10657       /* If we already want an address, there's nothing we can do.  */
10658       if (want_address)
10659         return 0;
10660
10661       /* Otherwise, process the argument and look for the address.  */
10662       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
10663
10664     case VAR_DECL:
10665       if (DECL_THREAD_LOCAL_P (loc))
10666         {
10667           rtx rtl;
10668           enum dwarf_location_atom first_op;
10669           enum dwarf_location_atom second_op;
10670
10671           if (targetm.have_tls)
10672             {
10673               /* If this is not defined, we have no way to emit the
10674                  data.  */
10675               if (!targetm.asm_out.output_dwarf_dtprel)
10676                 return 0;
10677
10678                /* The way DW_OP_GNU_push_tls_address is specified, we
10679                   can only look up addresses of objects in the current
10680                   module.  */
10681               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
10682                 return 0;
10683               first_op = (enum dwarf_location_atom) INTERNAL_DW_OP_tls_addr;
10684               second_op = DW_OP_GNU_push_tls_address;
10685             }
10686           else
10687             {
10688               if (!targetm.emutls.debug_form_tls_address)
10689                 return 0;
10690               loc = emutls_decl (loc);
10691               first_op = DW_OP_addr;
10692               second_op = DW_OP_form_tls_address;
10693             }
10694
10695           rtl = rtl_for_decl_location (loc);
10696           if (rtl == NULL_RTX)
10697             return 0;
10698
10699           if (!MEM_P (rtl))
10700             return 0;
10701           rtl = XEXP (rtl, 0);
10702           if (! CONSTANT_P (rtl))
10703             return 0;
10704
10705           ret = new_loc_descr (first_op, 0, 0);
10706           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10707           ret->dw_loc_oprnd1.v.val_addr = rtl;
10708
10709           ret1 = new_loc_descr (second_op, 0, 0);
10710           add_loc_descr (&ret, ret1);
10711
10712           have_address = 1;
10713           break;
10714         }
10715       /* FALLTHRU */
10716
10717     case PARM_DECL:
10718       if (DECL_HAS_VALUE_EXPR_P (loc))
10719         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
10720                                            want_address);
10721       /* FALLTHRU */
10722
10723     case RESULT_DECL:
10724     case FUNCTION_DECL:
10725       {
10726         rtx rtl = rtl_for_decl_location (loc);
10727
10728         if (rtl == NULL_RTX)
10729           return 0;
10730         else if (GET_CODE (rtl) == CONST_INT)
10731           {
10732             HOST_WIDE_INT val = INTVAL (rtl);
10733             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
10734               val &= GET_MODE_MASK (DECL_MODE (loc));
10735             ret = int_loc_descriptor (val);
10736           }
10737         else if (GET_CODE (rtl) == CONST_STRING)
10738           return 0;
10739         else if (CONSTANT_P (rtl))
10740           {
10741             ret = new_loc_descr (DW_OP_addr, 0, 0);
10742             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
10743             ret->dw_loc_oprnd1.v.val_addr = rtl;
10744           }
10745         else
10746           {
10747             enum machine_mode mode;
10748
10749             /* Certain constructs can only be represented at top-level.  */
10750             if (want_address == 2)
10751               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
10752
10753             mode = GET_MODE (rtl);
10754             if (MEM_P (rtl))
10755               {
10756                 rtl = XEXP (rtl, 0);
10757                 have_address = 1;
10758               }
10759             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10760           }
10761       }
10762       break;
10763
10764     case INDIRECT_REF:
10765       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10766       have_address = 1;
10767       break;
10768
10769     case COMPOUND_EXPR:
10770       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
10771
10772     CASE_CONVERT:
10773     case VIEW_CONVERT_EXPR:
10774     case SAVE_EXPR:
10775     case MODIFY_EXPR:
10776       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
10777
10778     case COMPONENT_REF:
10779     case BIT_FIELD_REF:
10780     case ARRAY_REF:
10781     case ARRAY_RANGE_REF:
10782       {
10783         tree obj, offset;
10784         HOST_WIDE_INT bitsize, bitpos, bytepos;
10785         enum machine_mode mode;
10786         int volatilep;
10787         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
10788
10789         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
10790                                    &unsignedp, &volatilep, false);
10791
10792         if (obj == loc)
10793           return 0;
10794
10795         ret = loc_descriptor_from_tree_1 (obj, 1);
10796         if (ret == 0
10797             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
10798           return 0;
10799
10800         if (offset != NULL_TREE)
10801           {
10802             /* Variable offset.  */
10803             ret1 = loc_descriptor_from_tree_1 (offset, 0);
10804             if (ret1 == 0)
10805               return 0;
10806             add_loc_descr (&ret, ret1);
10807             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
10808           }
10809
10810         bytepos = bitpos / BITS_PER_UNIT;
10811         loc_descr_plus_const (&ret, bytepos);
10812
10813         have_address = 1;
10814         break;
10815       }
10816
10817     case INTEGER_CST:
10818       if (host_integerp (loc, 0))
10819         ret = int_loc_descriptor (tree_low_cst (loc, 0));
10820       else
10821         return 0;
10822       break;
10823
10824     case CONSTRUCTOR:
10825       {
10826         /* Get an RTL for this, if something has been emitted.  */
10827         rtx rtl = lookup_constant_def (loc);
10828         enum machine_mode mode;
10829
10830         if (!rtl || !MEM_P (rtl))
10831           return 0;
10832         mode = GET_MODE (rtl);
10833         rtl = XEXP (rtl, 0);
10834         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
10835         have_address = 1;
10836         break;
10837       }
10838
10839     case TRUTH_AND_EXPR:
10840     case TRUTH_ANDIF_EXPR:
10841     case BIT_AND_EXPR:
10842       op = DW_OP_and;
10843       goto do_binop;
10844
10845     case TRUTH_XOR_EXPR:
10846     case BIT_XOR_EXPR:
10847       op = DW_OP_xor;
10848       goto do_binop;
10849
10850     case TRUTH_OR_EXPR:
10851     case TRUTH_ORIF_EXPR:
10852     case BIT_IOR_EXPR:
10853       op = DW_OP_or;
10854       goto do_binop;
10855
10856     case FLOOR_DIV_EXPR:
10857     case CEIL_DIV_EXPR:
10858     case ROUND_DIV_EXPR:
10859     case TRUNC_DIV_EXPR:
10860       op = DW_OP_div;
10861       goto do_binop;
10862
10863     case MINUS_EXPR:
10864       op = DW_OP_minus;
10865       goto do_binop;
10866
10867     case FLOOR_MOD_EXPR:
10868     case CEIL_MOD_EXPR:
10869     case ROUND_MOD_EXPR:
10870     case TRUNC_MOD_EXPR:
10871       op = DW_OP_mod;
10872       goto do_binop;
10873
10874     case MULT_EXPR:
10875       op = DW_OP_mul;
10876       goto do_binop;
10877
10878     case LSHIFT_EXPR:
10879       op = DW_OP_shl;
10880       goto do_binop;
10881
10882     case RSHIFT_EXPR:
10883       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
10884       goto do_binop;
10885
10886     case POINTER_PLUS_EXPR:
10887     case PLUS_EXPR:
10888       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
10889           && host_integerp (TREE_OPERAND (loc, 1), 0))
10890         {
10891           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10892           if (ret == 0)
10893             return 0;
10894
10895           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
10896           break;
10897         }
10898
10899       op = DW_OP_plus;
10900       goto do_binop;
10901
10902     case LE_EXPR:
10903       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10904         return 0;
10905
10906       op = DW_OP_le;
10907       goto do_binop;
10908
10909     case GE_EXPR:
10910       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10911         return 0;
10912
10913       op = DW_OP_ge;
10914       goto do_binop;
10915
10916     case LT_EXPR:
10917       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10918         return 0;
10919
10920       op = DW_OP_lt;
10921       goto do_binop;
10922
10923     case GT_EXPR:
10924       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
10925         return 0;
10926
10927       op = DW_OP_gt;
10928       goto do_binop;
10929
10930     case EQ_EXPR:
10931       op = DW_OP_eq;
10932       goto do_binop;
10933
10934     case NE_EXPR:
10935       op = DW_OP_ne;
10936       goto do_binop;
10937
10938     do_binop:
10939       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10940       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10941       if (ret == 0 || ret1 == 0)
10942         return 0;
10943
10944       add_loc_descr (&ret, ret1);
10945       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10946       break;
10947
10948     case TRUTH_NOT_EXPR:
10949     case BIT_NOT_EXPR:
10950       op = DW_OP_not;
10951       goto do_unop;
10952
10953     case ABS_EXPR:
10954       op = DW_OP_abs;
10955       goto do_unop;
10956
10957     case NEGATE_EXPR:
10958       op = DW_OP_neg;
10959       goto do_unop;
10960
10961     do_unop:
10962       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10963       if (ret == 0)
10964         return 0;
10965
10966       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10967       break;
10968
10969     case MIN_EXPR:
10970     case MAX_EXPR:
10971       {
10972         const enum tree_code code =
10973           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
10974
10975         loc = build3 (COND_EXPR, TREE_TYPE (loc),
10976                       build2 (code, integer_type_node,
10977                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
10978                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
10979       }
10980
10981       /* ... fall through ...  */
10982
10983     case COND_EXPR:
10984       {
10985         dw_loc_descr_ref lhs
10986           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
10987         dw_loc_descr_ref rhs
10988           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
10989         dw_loc_descr_ref bra_node, jump_node, tmp;
10990
10991         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
10992         if (ret == 0 || lhs == 0 || rhs == 0)
10993           return 0;
10994
10995         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10996         add_loc_descr (&ret, bra_node);
10997
10998         add_loc_descr (&ret, rhs);
10999         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
11000         add_loc_descr (&ret, jump_node);
11001
11002         add_loc_descr (&ret, lhs);
11003         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11004         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
11005
11006         /* ??? Need a node to point the skip at.  Use a nop.  */
11007         tmp = new_loc_descr (DW_OP_nop, 0, 0);
11008         add_loc_descr (&ret, tmp);
11009         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11010         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
11011       }
11012       break;
11013
11014     case FIX_TRUNC_EXPR:
11015       return 0;
11016
11017     default:
11018       /* Leave front-end specific codes as simply unknown.  This comes
11019          up, for instance, with the C STMT_EXPR.  */
11020       if ((unsigned int) TREE_CODE (loc)
11021           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
11022         return 0;
11023
11024 #ifdef ENABLE_CHECKING
11025       /* Otherwise this is a generic code; we should just lists all of
11026          these explicitly.  We forgot one.  */
11027       gcc_unreachable ();
11028 #else
11029       /* In a release build, we want to degrade gracefully: better to
11030          generate incomplete debugging information than to crash.  */
11031       return NULL;
11032 #endif
11033     }
11034
11035   /* Show if we can't fill the request for an address.  */
11036   if (want_address && !have_address)
11037     return 0;
11038
11039   /* If we've got an address and don't want one, dereference.  */
11040   if (!want_address && have_address && ret)
11041     {
11042       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
11043
11044       if (size > DWARF2_ADDR_SIZE || size == -1)
11045         return 0;
11046       else if (size == DWARF2_ADDR_SIZE)
11047         op = DW_OP_deref;
11048       else
11049         op = DW_OP_deref_size;
11050
11051       add_loc_descr (&ret, new_loc_descr (op, size, 0));
11052     }
11053
11054   return ret;
11055 }
11056
11057 static inline dw_loc_descr_ref
11058 loc_descriptor_from_tree (tree loc)
11059 {
11060   return loc_descriptor_from_tree_1 (loc, 2);
11061 }
11062
11063 /* Given a value, round it up to the lowest multiple of `boundary'
11064    which is not less than the value itself.  */
11065
11066 static inline HOST_WIDE_INT
11067 ceiling (HOST_WIDE_INT value, unsigned int boundary)
11068 {
11069   return (((value + boundary - 1) / boundary) * boundary);
11070 }
11071
11072 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
11073    pointer to the declared type for the relevant field variable, or return
11074    `integer_type_node' if the given node turns out to be an
11075    ERROR_MARK node.  */
11076
11077 static inline tree
11078 field_type (const_tree decl)
11079 {
11080   tree type;
11081
11082   if (TREE_CODE (decl) == ERROR_MARK)
11083     return integer_type_node;
11084
11085   type = DECL_BIT_FIELD_TYPE (decl);
11086   if (type == NULL_TREE)
11087     type = TREE_TYPE (decl);
11088
11089   return type;
11090 }
11091
11092 /* Given a pointer to a tree node, return the alignment in bits for
11093    it, or else return BITS_PER_WORD if the node actually turns out to
11094    be an ERROR_MARK node.  */
11095
11096 static inline unsigned
11097 simple_type_align_in_bits (const_tree type)
11098 {
11099   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
11100 }
11101
11102 static inline unsigned
11103 simple_decl_align_in_bits (const_tree decl)
11104 {
11105   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
11106 }
11107
11108 /* Return the result of rounding T up to ALIGN.  */
11109
11110 static inline HOST_WIDE_INT
11111 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
11112 {
11113   /* We must be careful if T is negative because HOST_WIDE_INT can be
11114      either "above" or "below" unsigned int as per the C promotion
11115      rules, depending on the host, thus making the signedness of the
11116      direct multiplication and division unpredictable.  */
11117   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
11118
11119   u += align - 1;
11120   u /= align;
11121   u *= align;
11122
11123   return (HOST_WIDE_INT) u;
11124 }
11125
11126 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
11127    lowest addressed byte of the "containing object" for the given FIELD_DECL,
11128    or return 0 if we are unable to determine what that offset is, either
11129    because the argument turns out to be a pointer to an ERROR_MARK node, or
11130    because the offset is actually variable.  (We can't handle the latter case
11131    just yet).  */
11132
11133 static HOST_WIDE_INT
11134 field_byte_offset (const_tree decl)
11135 {
11136   HOST_WIDE_INT object_offset_in_bits;
11137   HOST_WIDE_INT bitpos_int;
11138
11139   if (TREE_CODE (decl) == ERROR_MARK)
11140     return 0;
11141
11142   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
11143
11144   /* We cannot yet cope with fields whose positions are variable, so
11145      for now, when we see such things, we simply return 0.  Someday, we may
11146      be able to handle such cases, but it will be damn difficult.  */
11147   if (! host_integerp (bit_position (decl), 0))
11148     return 0;
11149
11150   bitpos_int = int_bit_position (decl);
11151
11152 #ifdef PCC_BITFIELD_TYPE_MATTERS
11153   if (PCC_BITFIELD_TYPE_MATTERS)
11154     {
11155       tree type;
11156       tree field_size_tree;
11157       HOST_WIDE_INT deepest_bitpos;
11158       unsigned HOST_WIDE_INT field_size_in_bits;
11159       unsigned int type_align_in_bits;
11160       unsigned int decl_align_in_bits;
11161       unsigned HOST_WIDE_INT type_size_in_bits;
11162
11163       type = field_type (decl);
11164       type_size_in_bits = simple_type_size_in_bits (type);
11165       type_align_in_bits = simple_type_align_in_bits (type);
11166
11167       field_size_tree = DECL_SIZE (decl);
11168
11169       /* The size could be unspecified if there was an error, or for
11170          a flexible array member.  */
11171       if (!field_size_tree)
11172         field_size_tree = bitsize_zero_node;
11173
11174       /* If the size of the field is not constant, use the type size.  */
11175       if (host_integerp (field_size_tree, 1))
11176         field_size_in_bits = tree_low_cst (field_size_tree, 1);
11177       else
11178         field_size_in_bits = type_size_in_bits;
11179
11180       decl_align_in_bits = simple_decl_align_in_bits (decl);
11181
11182       /* The GCC front-end doesn't make any attempt to keep track of the
11183          starting bit offset (relative to the start of the containing
11184          structure type) of the hypothetical "containing object" for a
11185          bit-field.  Thus, when computing the byte offset value for the
11186          start of the "containing object" of a bit-field, we must deduce
11187          this information on our own. This can be rather tricky to do in
11188          some cases.  For example, handling the following structure type
11189          definition when compiling for an i386/i486 target (which only
11190          aligns long long's to 32-bit boundaries) can be very tricky:
11191
11192          struct S { int field1; long long field2:31; };
11193
11194          Fortunately, there is a simple rule-of-thumb which can be used
11195          in such cases.  When compiling for an i386/i486, GCC will
11196          allocate 8 bytes for the structure shown above.  It decides to
11197          do this based upon one simple rule for bit-field allocation.
11198          GCC allocates each "containing object" for each bit-field at
11199          the first (i.e. lowest addressed) legitimate alignment boundary
11200          (based upon the required minimum alignment for the declared
11201          type of the field) which it can possibly use, subject to the
11202          condition that there is still enough available space remaining
11203          in the containing object (when allocated at the selected point)
11204          to fully accommodate all of the bits of the bit-field itself.
11205
11206          This simple rule makes it obvious why GCC allocates 8 bytes for
11207          each object of the structure type shown above.  When looking
11208          for a place to allocate the "containing object" for `field2',
11209          the compiler simply tries to allocate a 64-bit "containing
11210          object" at each successive 32-bit boundary (starting at zero)
11211          until it finds a place to allocate that 64- bit field such that
11212          at least 31 contiguous (and previously unallocated) bits remain
11213          within that selected 64 bit field.  (As it turns out, for the
11214          example above, the compiler finds it is OK to allocate the
11215          "containing object" 64-bit field at bit-offset zero within the
11216          structure type.)
11217
11218          Here we attempt to work backwards from the limited set of facts
11219          we're given, and we try to deduce from those facts, where GCC
11220          must have believed that the containing object started (within
11221          the structure type). The value we deduce is then used (by the
11222          callers of this routine) to generate DW_AT_location and
11223          DW_AT_bit_offset attributes for fields (both bit-fields and, in
11224          the case of DW_AT_location, regular fields as well).  */
11225
11226       /* Figure out the bit-distance from the start of the structure to
11227          the "deepest" bit of the bit-field.  */
11228       deepest_bitpos = bitpos_int + field_size_in_bits;
11229
11230       /* This is the tricky part.  Use some fancy footwork to deduce
11231          where the lowest addressed bit of the containing object must
11232          be.  */
11233       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11234
11235       /* Round up to type_align by default.  This works best for
11236          bitfields.  */
11237       object_offset_in_bits
11238         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
11239
11240       if (object_offset_in_bits > bitpos_int)
11241         {
11242           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
11243
11244           /* Round up to decl_align instead.  */
11245           object_offset_in_bits
11246             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
11247         }
11248     }
11249   else
11250 #endif
11251     object_offset_in_bits = bitpos_int;
11252
11253   return object_offset_in_bits / BITS_PER_UNIT;
11254 }
11255 \f
11256 /* The following routines define various Dwarf attributes and any data
11257    associated with them.  */
11258
11259 /* Add a location description attribute value to a DIE.
11260
11261    This emits location attributes suitable for whole variables and
11262    whole parameters.  Note that the location attributes for struct fields are
11263    generated by the routine `data_member_location_attribute' below.  */
11264
11265 static inline void
11266 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
11267                              dw_loc_descr_ref descr)
11268 {
11269   if (descr != 0)
11270     add_AT_loc (die, attr_kind, descr);
11271 }
11272
11273 /* Attach the specialized form of location attribute used for data members of
11274    struct and union types.  In the special case of a FIELD_DECL node which
11275    represents a bit-field, the "offset" part of this special location
11276    descriptor must indicate the distance in bytes from the lowest-addressed
11277    byte of the containing struct or union type to the lowest-addressed byte of
11278    the "containing object" for the bit-field.  (See the `field_byte_offset'
11279    function above).
11280
11281    For any given bit-field, the "containing object" is a hypothetical object
11282    (of some integral or enum type) within which the given bit-field lives.  The
11283    type of this hypothetical "containing object" is always the same as the
11284    declared type of the individual bit-field itself (for GCC anyway... the
11285    DWARF spec doesn't actually mandate this).  Note that it is the size (in
11286    bytes) of the hypothetical "containing object" which will be given in the
11287    DW_AT_byte_size attribute for this bit-field.  (See the
11288    `byte_size_attribute' function below.)  It is also used when calculating the
11289    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
11290    function below.)  */
11291
11292 static void
11293 add_data_member_location_attribute (dw_die_ref die, tree decl)
11294 {
11295   HOST_WIDE_INT offset;
11296   dw_loc_descr_ref loc_descr = 0;
11297
11298   if (TREE_CODE (decl) == TREE_BINFO)
11299     {
11300       /* We're working on the TAG_inheritance for a base class.  */
11301       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
11302         {
11303           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
11304              aren't at a fixed offset from all (sub)objects of the same
11305              type.  We need to extract the appropriate offset from our
11306              vtable.  The following dwarf expression means
11307
11308                BaseAddr = ObAddr + *((*ObAddr) - Offset)
11309
11310              This is specific to the V3 ABI, of course.  */
11311
11312           dw_loc_descr_ref tmp;
11313
11314           /* Make a copy of the object address.  */
11315           tmp = new_loc_descr (DW_OP_dup, 0, 0);
11316           add_loc_descr (&loc_descr, tmp);
11317
11318           /* Extract the vtable address.  */
11319           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11320           add_loc_descr (&loc_descr, tmp);
11321
11322           /* Calculate the address of the offset.  */
11323           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
11324           gcc_assert (offset < 0);
11325
11326           tmp = int_loc_descriptor (-offset);
11327           add_loc_descr (&loc_descr, tmp);
11328           tmp = new_loc_descr (DW_OP_minus, 0, 0);
11329           add_loc_descr (&loc_descr, tmp);
11330
11331           /* Extract the offset.  */
11332           tmp = new_loc_descr (DW_OP_deref, 0, 0);
11333           add_loc_descr (&loc_descr, tmp);
11334
11335           /* Add it to the object address.  */
11336           tmp = new_loc_descr (DW_OP_plus, 0, 0);
11337           add_loc_descr (&loc_descr, tmp);
11338         }
11339       else
11340         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
11341     }
11342   else
11343     offset = field_byte_offset (decl);
11344
11345   if (! loc_descr)
11346     {
11347       enum dwarf_location_atom op;
11348
11349       /* The DWARF2 standard says that we should assume that the structure
11350          address is already on the stack, so we can specify a structure field
11351          address by using DW_OP_plus_uconst.  */
11352
11353 #ifdef MIPS_DEBUGGING_INFO
11354       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
11355          operator correctly.  It works only if we leave the offset on the
11356          stack.  */
11357       op = DW_OP_constu;
11358 #else
11359       op = DW_OP_plus_uconst;
11360 #endif
11361
11362       loc_descr = new_loc_descr (op, offset, 0);
11363     }
11364
11365   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
11366 }
11367
11368 /* Writes integer values to dw_vec_const array.  */
11369
11370 static void
11371 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
11372 {
11373   while (size != 0)
11374     {
11375       *dest++ = val & 0xff;
11376       val >>= 8;
11377       --size;
11378     }
11379 }
11380
11381 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
11382
11383 static HOST_WIDE_INT
11384 extract_int (const unsigned char *src, unsigned int size)
11385 {
11386   HOST_WIDE_INT val = 0;
11387
11388   src += size;
11389   while (size != 0)
11390     {
11391       val <<= 8;
11392       val |= *--src & 0xff;
11393       --size;
11394     }
11395   return val;
11396 }
11397
11398 /* Writes floating point values to dw_vec_const array.  */
11399
11400 static void
11401 insert_float (const_rtx rtl, unsigned char *array)
11402 {
11403   REAL_VALUE_TYPE rv;
11404   long val[4];
11405   int i;
11406
11407   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
11408   real_to_target (val, &rv, GET_MODE (rtl));
11409
11410   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
11411   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
11412     {
11413       insert_int (val[i], 4, array);
11414       array += 4;
11415     }
11416 }
11417
11418 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
11419    does not have a "location" either in memory or in a register.  These
11420    things can arise in GNU C when a constant is passed as an actual parameter
11421    to an inlined function.  They can also arise in C++ where declared
11422    constants do not necessarily get memory "homes".  */
11423
11424 static void
11425 add_const_value_attribute (dw_die_ref die, rtx rtl)
11426 {
11427   switch (GET_CODE (rtl))
11428     {
11429     case CONST_INT:
11430       {
11431         HOST_WIDE_INT val = INTVAL (rtl);
11432
11433         if (val < 0)
11434           add_AT_int (die, DW_AT_const_value, val);
11435         else
11436           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
11437       }
11438       break;
11439
11440     case CONST_DOUBLE:
11441       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
11442          floating-point constant.  A CONST_DOUBLE is used whenever the
11443          constant requires more than one word in order to be adequately
11444          represented.  We output CONST_DOUBLEs as blocks.  */
11445       {
11446         enum machine_mode mode = GET_MODE (rtl);
11447
11448         if (SCALAR_FLOAT_MODE_P (mode))
11449           {
11450             unsigned int length = GET_MODE_SIZE (mode);
11451             unsigned char *array = GGC_NEWVEC (unsigned char, length);
11452
11453             insert_float (rtl, array);
11454             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
11455           }
11456         else
11457           {
11458             /* ??? We really should be using HOST_WIDE_INT throughout.  */
11459             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
11460
11461             add_AT_long_long (die, DW_AT_const_value,
11462                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
11463           }
11464       }
11465       break;
11466
11467     case CONST_VECTOR:
11468       {
11469         enum machine_mode mode = GET_MODE (rtl);
11470         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
11471         unsigned int length = CONST_VECTOR_NUNITS (rtl);
11472         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11473         unsigned int i;
11474         unsigned char *p;
11475
11476         switch (GET_MODE_CLASS (mode))
11477           {
11478           case MODE_VECTOR_INT:
11479             for (i = 0, p = array; i < length; i++, p += elt_size)
11480               {
11481                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11482                 HOST_WIDE_INT lo, hi;
11483
11484                 switch (GET_CODE (elt))
11485                   {
11486                   case CONST_INT:
11487                     lo = INTVAL (elt);
11488                     hi = -(lo < 0);
11489                     break;
11490
11491                   case CONST_DOUBLE:
11492                     lo = CONST_DOUBLE_LOW (elt);
11493                     hi = CONST_DOUBLE_HIGH (elt);
11494                     break;
11495
11496                   default:
11497                     gcc_unreachable ();
11498                   }
11499
11500                 if (elt_size <= sizeof (HOST_WIDE_INT))
11501                   insert_int (lo, elt_size, p);
11502                 else
11503                   {
11504                     unsigned char *p0 = p;
11505                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11506
11507                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11508                     if (WORDS_BIG_ENDIAN)
11509                       {
11510                         p0 = p1;
11511                         p1 = p;
11512                       }
11513                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11514                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11515                   }
11516               }
11517             break;
11518
11519           case MODE_VECTOR_FLOAT:
11520             for (i = 0, p = array; i < length; i++, p += elt_size)
11521               {
11522                 rtx elt = CONST_VECTOR_ELT (rtl, i);
11523                 insert_float (elt, p);
11524               }
11525             break;
11526
11527           default:
11528             gcc_unreachable ();
11529           }
11530
11531         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
11532       }
11533       break;
11534
11535     case CONST_STRING:
11536       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
11537       break;
11538
11539     case SYMBOL_REF:
11540     case LABEL_REF:
11541     case CONST:
11542       add_AT_addr (die, DW_AT_const_value, rtl);
11543       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11544       break;
11545
11546     case PLUS:
11547       /* In cases where an inlined instance of an inline function is passed
11548          the address of an `auto' variable (which is local to the caller) we
11549          can get a situation where the DECL_RTL of the artificial local
11550          variable (for the inlining) which acts as a stand-in for the
11551          corresponding formal parameter (of the inline function) will look
11552          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
11553          exactly a compile-time constant expression, but it isn't the address
11554          of the (artificial) local variable either.  Rather, it represents the
11555          *value* which the artificial local variable always has during its
11556          lifetime.  We currently have no way to represent such quasi-constant
11557          values in Dwarf, so for now we just punt and generate nothing.  */
11558       break;
11559
11560     default:
11561       /* No other kinds of rtx should be possible here.  */
11562       gcc_unreachable ();
11563     }
11564
11565 }
11566
11567 /* Determine whether the evaluation of EXPR references any variables
11568    or functions which aren't otherwise used (and therefore may not be
11569    output).  */
11570 static tree
11571 reference_to_unused (tree * tp, int * walk_subtrees,
11572                      void * data ATTRIBUTE_UNUSED)
11573 {
11574   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
11575     *walk_subtrees = 0;
11576
11577   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
11578       && ! TREE_ASM_WRITTEN (*tp))
11579     return *tp;
11580   /* ???  The C++ FE emits debug information for using decls, so
11581      putting gcc_unreachable here falls over.  See PR31899.  For now
11582      be conservative.  */
11583   else if (!cgraph_global_info_ready
11584            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
11585     return *tp;
11586   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
11587     {
11588       struct varpool_node *node = varpool_node (*tp);
11589       if (!node->needed)
11590         return *tp;
11591     }
11592   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
11593            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
11594     {
11595       struct cgraph_node *node = cgraph_node (*tp);
11596       if (node->process || TREE_ASM_WRITTEN (*tp))
11597         return *tp;
11598     }
11599   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
11600     return *tp;
11601
11602   return NULL_TREE;
11603 }
11604
11605 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
11606    for use in a later add_const_value_attribute call.  */
11607
11608 static rtx
11609 rtl_for_decl_init (tree init, tree type)
11610 {
11611   rtx rtl = NULL_RTX;
11612
11613   /* If a variable is initialized with a string constant without embedded
11614      zeros, build CONST_STRING.  */
11615   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
11616     {
11617       tree enttype = TREE_TYPE (type);
11618       tree domain = TYPE_DOMAIN (type);
11619       enum machine_mode mode = TYPE_MODE (enttype);
11620
11621       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
11622           && domain
11623           && integer_zerop (TYPE_MIN_VALUE (domain))
11624           && compare_tree_int (TYPE_MAX_VALUE (domain),
11625                                TREE_STRING_LENGTH (init) - 1) == 0
11626           && ((size_t) TREE_STRING_LENGTH (init)
11627               == strlen (TREE_STRING_POINTER (init)) + 1))
11628         rtl = gen_rtx_CONST_STRING (VOIDmode,
11629                                     ggc_strdup (TREE_STRING_POINTER (init)));
11630     }
11631   /* Other aggregates, and complex values, could be represented using
11632      CONCAT: FIXME!  */
11633   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
11634     ;
11635   /* Vectors only work if their mode is supported by the target.
11636      FIXME: generic vectors ought to work too.  */
11637   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
11638     ;
11639   /* If the initializer is something that we know will expand into an
11640      immediate RTL constant, expand it now.  We must be careful not to
11641      reference variables which won't be output.  */
11642   else if (initializer_constant_valid_p (init, type)
11643            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
11644     {
11645       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
11646          possible.  */
11647       if (TREE_CODE (type) == VECTOR_TYPE)
11648         switch (TREE_CODE (init))
11649           {
11650           case VECTOR_CST:
11651             break;
11652           case CONSTRUCTOR:
11653             if (TREE_CONSTANT (init))
11654               {
11655                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
11656                 bool constant_p = true;
11657                 tree value;
11658                 unsigned HOST_WIDE_INT ix;
11659
11660                 /* Even when ctor is constant, it might contain non-*_CST
11661                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
11662                    belong into VECTOR_CST nodes.  */
11663                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
11664                   if (!CONSTANT_CLASS_P (value))
11665                     {
11666                       constant_p = false;
11667                       break;
11668                     }
11669
11670                 if (constant_p)
11671                   {
11672                     init = build_vector_from_ctor (type, elts);
11673                     break;
11674                   }
11675               }
11676             /* FALLTHRU */
11677
11678           default:
11679             return NULL;
11680           }
11681
11682       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
11683
11684       /* If expand_expr returns a MEM, it wasn't immediate.  */
11685       gcc_assert (!rtl || !MEM_P (rtl));
11686     }
11687
11688   return rtl;
11689 }
11690
11691 /* Generate RTL for the variable DECL to represent its location.  */
11692
11693 static rtx
11694 rtl_for_decl_location (tree decl)
11695 {
11696   rtx rtl;
11697
11698   /* Here we have to decide where we are going to say the parameter "lives"
11699      (as far as the debugger is concerned).  We only have a couple of
11700      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
11701
11702      DECL_RTL normally indicates where the parameter lives during most of the
11703      activation of the function.  If optimization is enabled however, this
11704      could be either NULL or else a pseudo-reg.  Both of those cases indicate
11705      that the parameter doesn't really live anywhere (as far as the code
11706      generation parts of GCC are concerned) during most of the function's
11707      activation.  That will happen (for example) if the parameter is never
11708      referenced within the function.
11709
11710      We could just generate a location descriptor here for all non-NULL
11711      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
11712      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
11713      where DECL_RTL is NULL or is a pseudo-reg.
11714
11715      Note however that we can only get away with using DECL_INCOMING_RTL as
11716      a backup substitute for DECL_RTL in certain limited cases.  In cases
11717      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
11718      we can be sure that the parameter was passed using the same type as it is
11719      declared to have within the function, and that its DECL_INCOMING_RTL
11720      points us to a place where a value of that type is passed.
11721
11722      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
11723      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
11724      because in these cases DECL_INCOMING_RTL points us to a value of some
11725      type which is *different* from the type of the parameter itself.  Thus,
11726      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
11727      such cases, the debugger would end up (for example) trying to fetch a
11728      `float' from a place which actually contains the first part of a
11729      `double'.  That would lead to really incorrect and confusing
11730      output at debug-time.
11731
11732      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
11733      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
11734      are a couple of exceptions however.  On little-endian machines we can
11735      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
11736      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
11737      an integral type that is smaller than TREE_TYPE (decl). These cases arise
11738      when (on a little-endian machine) a non-prototyped function has a
11739      parameter declared to be of type `short' or `char'.  In such cases,
11740      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
11741      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
11742      passed `int' value.  If the debugger then uses that address to fetch
11743      a `short' or a `char' (on a little-endian machine) the result will be
11744      the correct data, so we allow for such exceptional cases below.
11745
11746      Note that our goal here is to describe the place where the given formal
11747      parameter lives during most of the function's activation (i.e. between the
11748      end of the prologue and the start of the epilogue).  We'll do that as best
11749      as we can. Note however that if the given formal parameter is modified
11750      sometime during the execution of the function, then a stack backtrace (at
11751      debug-time) will show the function as having been called with the *new*
11752      value rather than the value which was originally passed in.  This happens
11753      rarely enough that it is not a major problem, but it *is* a problem, and
11754      I'd like to fix it.
11755
11756      A future version of dwarf2out.c may generate two additional attributes for
11757      any given DW_TAG_formal_parameter DIE which will describe the "passed
11758      type" and the "passed location" for the given formal parameter in addition
11759      to the attributes we now generate to indicate the "declared type" and the
11760      "active location" for each parameter.  This additional set of attributes
11761      could be used by debuggers for stack backtraces. Separately, note that
11762      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
11763      This happens (for example) for inlined-instances of inline function formal
11764      parameters which are never referenced.  This really shouldn't be
11765      happening.  All PARM_DECL nodes should get valid non-NULL
11766      DECL_INCOMING_RTL values.  FIXME.  */
11767
11768   /* Use DECL_RTL as the "location" unless we find something better.  */
11769   rtl = DECL_RTL_IF_SET (decl);
11770
11771   /* When generating abstract instances, ignore everything except
11772      constants, symbols living in memory, and symbols living in
11773      fixed registers.  */
11774   if (! reload_completed)
11775     {
11776       if (rtl
11777           && (CONSTANT_P (rtl)
11778               || (MEM_P (rtl)
11779                   && CONSTANT_P (XEXP (rtl, 0)))
11780               || (REG_P (rtl)
11781                   && TREE_CODE (decl) == VAR_DECL
11782                   && TREE_STATIC (decl))))
11783         {
11784           rtl = targetm.delegitimize_address (rtl);
11785           return rtl;
11786         }
11787       rtl = NULL_RTX;
11788     }
11789   else if (TREE_CODE (decl) == PARM_DECL)
11790     {
11791       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
11792         {
11793           tree declared_type = TREE_TYPE (decl);
11794           tree passed_type = DECL_ARG_TYPE (decl);
11795           enum machine_mode dmode = TYPE_MODE (declared_type);
11796           enum machine_mode pmode = TYPE_MODE (passed_type);
11797
11798           /* This decl represents a formal parameter which was optimized out.
11799              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
11800              all cases where (rtl == NULL_RTX) just below.  */
11801           if (dmode == pmode)
11802             rtl = DECL_INCOMING_RTL (decl);
11803           else if (SCALAR_INT_MODE_P (dmode)
11804                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
11805                    && DECL_INCOMING_RTL (decl))
11806             {
11807               rtx inc = DECL_INCOMING_RTL (decl);
11808               if (REG_P (inc))
11809                 rtl = inc;
11810               else if (MEM_P (inc))
11811                 {
11812                   if (BYTES_BIG_ENDIAN)
11813                     rtl = adjust_address_nv (inc, dmode,
11814                                              GET_MODE_SIZE (pmode)
11815                                              - GET_MODE_SIZE (dmode));
11816                   else
11817                     rtl = inc;
11818                 }
11819             }
11820         }
11821
11822       /* If the parm was passed in registers, but lives on the stack, then
11823          make a big endian correction if the mode of the type of the
11824          parameter is not the same as the mode of the rtl.  */
11825       /* ??? This is the same series of checks that are made in dbxout.c before
11826          we reach the big endian correction code there.  It isn't clear if all
11827          of these checks are necessary here, but keeping them all is the safe
11828          thing to do.  */
11829       else if (MEM_P (rtl)
11830                && XEXP (rtl, 0) != const0_rtx
11831                && ! CONSTANT_P (XEXP (rtl, 0))
11832                /* Not passed in memory.  */
11833                && !MEM_P (DECL_INCOMING_RTL (decl))
11834                /* Not passed by invisible reference.  */
11835                && (!REG_P (XEXP (rtl, 0))
11836                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
11837                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
11838 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11839                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
11840 #endif
11841                      )
11842                /* Big endian correction check.  */
11843                && BYTES_BIG_ENDIAN
11844                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
11845                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
11846                    < UNITS_PER_WORD))
11847         {
11848           int offset = (UNITS_PER_WORD
11849                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
11850
11851           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11852                              plus_constant (XEXP (rtl, 0), offset));
11853         }
11854     }
11855   else if (TREE_CODE (decl) == VAR_DECL
11856            && rtl
11857            && MEM_P (rtl)
11858            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
11859            && BYTES_BIG_ENDIAN)
11860     {
11861       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
11862       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
11863
11864       /* If a variable is declared "register" yet is smaller than
11865          a register, then if we store the variable to memory, it
11866          looks like we're storing a register-sized value, when in
11867          fact we are not.  We need to adjust the offset of the
11868          storage location to reflect the actual value's bytes,
11869          else gdb will not be able to display it.  */
11870       if (rsize > dsize)
11871         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
11872                            plus_constant (XEXP (rtl, 0), rsize-dsize));
11873     }
11874
11875   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
11876      and will have been substituted directly into all expressions that use it.
11877      C does not have such a concept, but C++ and other languages do.  */
11878   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
11879     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
11880
11881   if (rtl)
11882     rtl = targetm.delegitimize_address (rtl);
11883
11884   /* If we don't look past the constant pool, we risk emitting a
11885      reference to a constant pool entry that isn't referenced from
11886      code, and thus is not emitted.  */
11887   if (rtl)
11888     rtl = avoid_constant_pool_reference (rtl);
11889
11890   return rtl;
11891 }
11892
11893 /* We need to figure out what section we should use as the base for the
11894    address ranges where a given location is valid.
11895    1. If this particular DECL has a section associated with it, use that.
11896    2. If this function has a section associated with it, use that.
11897    3. Otherwise, use the text section.
11898    XXX: If you split a variable across multiple sections, we won't notice.  */
11899
11900 static const char *
11901 secname_for_decl (const_tree decl)
11902 {
11903   const char *secname;
11904
11905   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
11906     {
11907       tree sectree = DECL_SECTION_NAME (decl);
11908       secname = TREE_STRING_POINTER (sectree);
11909     }
11910   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
11911     {
11912       tree sectree = DECL_SECTION_NAME (current_function_decl);
11913       secname = TREE_STRING_POINTER (sectree);
11914     }
11915   else if (cfun && in_cold_section_p)
11916     secname = crtl->subsections.cold_section_label;
11917   else
11918     secname = text_section_label;
11919
11920   return secname;
11921 }
11922
11923 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
11924    returned.  If so, the decl for the COMMON block is returned, and the
11925    value is the offset into the common block for the symbol.  */
11926
11927 static tree
11928 fortran_common (tree decl, HOST_WIDE_INT *value)
11929 {
11930   tree val_expr, cvar;
11931   enum machine_mode mode;
11932   HOST_WIDE_INT bitsize, bitpos;
11933   tree offset;
11934   int volatilep = 0, unsignedp = 0;
11935
11936   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
11937      it does not have a value (the offset into the common area), or if it
11938      is thread local (as opposed to global) then it isn't common, and shouldn't
11939      be handled as such.  */
11940   if (TREE_CODE (decl) != VAR_DECL
11941       || !TREE_PUBLIC (decl)
11942       || !TREE_STATIC (decl)
11943       || !DECL_HAS_VALUE_EXPR_P (decl)
11944       || !is_fortran ())
11945     return NULL_TREE;
11946
11947   val_expr = DECL_VALUE_EXPR (decl);
11948   if (TREE_CODE (val_expr) != COMPONENT_REF)
11949     return NULL_TREE;
11950
11951   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
11952                               &mode, &unsignedp, &volatilep, true);
11953
11954   if (cvar == NULL_TREE
11955       || TREE_CODE (cvar) != VAR_DECL
11956       || DECL_ARTIFICIAL (cvar)
11957       || !TREE_PUBLIC (cvar))
11958     return NULL_TREE;
11959
11960   *value = 0;
11961   if (offset != NULL)
11962     {
11963       if (!host_integerp (offset, 0))
11964         return NULL_TREE;
11965       *value = tree_low_cst (offset, 0);
11966     }
11967   if (bitpos != 0)
11968     *value += bitpos / BITS_PER_UNIT;
11969
11970   return cvar;
11971 }
11972
11973 /* Dereference a location expression LOC if DECL is passed by invisible
11974    reference.  */
11975
11976 static dw_loc_descr_ref
11977 loc_by_reference (dw_loc_descr_ref loc, tree decl)
11978 {
11979   HOST_WIDE_INT size;
11980   enum dwarf_location_atom op;
11981
11982   if (loc == NULL)
11983     return NULL;
11984
11985   if ((TREE_CODE (decl) != PARM_DECL
11986        && TREE_CODE (decl) != RESULT_DECL
11987        && TREE_CODE (decl) != VAR_DECL)
11988       || !DECL_BY_REFERENCE (decl))
11989     return loc;
11990
11991   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
11992      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
11993      location expression is considered to be address of a memory location,
11994      rather than the register itself.  */
11995   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
11996        || loc->dw_loc_opc == DW_OP_regx)
11997       && (loc->dw_loc_next == NULL
11998           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
11999               && loc->dw_loc_next->dw_loc_next == NULL)))
12000     {
12001       if (loc->dw_loc_opc == DW_OP_regx)
12002         {
12003           loc->dw_loc_opc = DW_OP_bregx;
12004           loc->dw_loc_oprnd2.v.val_int = 0;
12005         }
12006       else
12007         {
12008           loc->dw_loc_opc
12009             = (enum dwarf_location_atom)
12010               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12011           loc->dw_loc_oprnd1.v.val_int = 0;
12012         }
12013       return loc;
12014     }
12015
12016   size = int_size_in_bytes (TREE_TYPE (decl));
12017   if (size > DWARF2_ADDR_SIZE || size == -1)
12018     return 0;
12019   else if (size == DWARF2_ADDR_SIZE)
12020     op = DW_OP_deref;
12021   else
12022     op = DW_OP_deref_size;
12023   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12024   return loc;
12025 }
12026
12027 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
12028    data attribute for a variable or a parameter.  We generate the
12029    DW_AT_const_value attribute only in those cases where the given variable
12030    or parameter does not have a true "location" either in memory or in a
12031    register.  This can happen (for example) when a constant is passed as an
12032    actual argument in a call to an inline function.  (It's possible that
12033    these things can crop up in other ways also.)  Note that one type of
12034    constant value which can be passed into an inlined function is a constant
12035    pointer.  This can happen for example if an actual argument in an inlined
12036    function call evaluates to a compile-time constant address.  */
12037
12038 static void
12039 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
12040                                        enum dwarf_attribute attr)
12041 {
12042   rtx rtl;
12043   dw_loc_descr_ref descr;
12044   var_loc_list *loc_list;
12045   struct var_loc_node *node;
12046   if (TREE_CODE (decl) == ERROR_MARK)
12047     return;
12048
12049   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
12050               || TREE_CODE (decl) == RESULT_DECL);
12051
12052   /* See if we possibly have multiple locations for this variable.  */
12053   loc_list = lookup_decl_loc (decl);
12054
12055   /* If it truly has multiple locations, the first and last node will
12056      differ.  */
12057   if (loc_list && loc_list->first != loc_list->last)
12058     {
12059       const char *endname, *secname;
12060       dw_loc_list_ref list;
12061       rtx varloc;
12062       enum var_init_status initialized;
12063
12064       /* Now that we know what section we are using for a base,
12065          actually construct the list of locations.
12066          The first location information is what is passed to the
12067          function that creates the location list, and the remaining
12068          locations just get added on to that list.
12069          Note that we only know the start address for a location
12070          (IE location changes), so to build the range, we use
12071          the range [current location start, next location start].
12072          This means we have to special case the last node, and generate
12073          a range of [last location start, end of function label].  */
12074
12075       node = loc_list->first;
12076       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12077       secname = secname_for_decl (decl);
12078
12079       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12080         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12081       else
12082         initialized = VAR_INIT_STATUS_INITIALIZED;
12083
12084       descr = loc_by_reference (loc_descriptor (varloc, initialized), decl);
12085       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12086       node = node->next;
12087
12088       for (; node->next; node = node->next)
12089         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12090           {
12091             /* The variable has a location between NODE->LABEL and
12092                NODE->NEXT->LABEL.  */
12093             enum var_init_status initialized =
12094               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12095             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12096             descr = loc_by_reference (loc_descriptor (varloc, initialized),
12097                                       decl);
12098             add_loc_descr_to_loc_list (&list, descr,
12099                                        node->label, node->next->label, secname);
12100           }
12101
12102       /* If the variable has a location at the last label
12103          it keeps its location until the end of function.  */
12104       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12105         {
12106           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12107           enum var_init_status initialized =
12108             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12109
12110           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12111           if (!current_function_decl)
12112             endname = text_end_label;
12113           else
12114             {
12115               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12116                                            current_function_funcdef_no);
12117               endname = ggc_strdup (label_id);
12118             }
12119           descr = loc_by_reference (loc_descriptor (varloc, initialized),
12120                                     decl);
12121           add_loc_descr_to_loc_list (&list, descr,
12122                                      node->label, endname, secname);
12123         }
12124
12125       /* Finally, add the location list to the DIE, and we are done.  */
12126       add_AT_loc_list (die, attr, list);
12127       return;
12128     }
12129
12130   /* Try to get some constant RTL for this decl, and use that as the value of
12131      the location.  */
12132
12133   rtl = rtl_for_decl_location (decl);
12134   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
12135     {
12136       add_const_value_attribute (die, rtl);
12137       return;
12138     }
12139
12140   /* If we have tried to generate the location otherwise, and it
12141      didn't work out (we wouldn't be here if we did), and we have a one entry
12142      location list, try generating a location from that.  */
12143   if (loc_list && loc_list->first)
12144     {
12145       enum var_init_status status;
12146       node = loc_list->first;
12147       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12148       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
12149       if (descr)
12150         {
12151           descr = loc_by_reference (descr, decl);
12152           add_AT_location_description (die, attr, descr);
12153           return;
12154         }
12155     }
12156
12157   /* We couldn't get any rtl, so try directly generating the location
12158      description from the tree.  */
12159   descr = loc_descriptor_from_tree (decl);
12160   if (descr)
12161     {
12162       descr = loc_by_reference (descr, decl);
12163       add_AT_location_description (die, attr, descr);
12164       return;
12165     }
12166   /* None of that worked, so it must not really have a location;
12167      try adding a constant value attribute from the DECL_INITIAL.  */
12168   tree_add_const_value_attribute (die, decl);
12169 }
12170
12171 /* Add VARIABLE and DIE into deferred locations list.  */
12172
12173 static void
12174 defer_location (tree variable, dw_die_ref die)
12175 {
12176   deferred_locations entry;
12177   entry.variable = variable;
12178   entry.die = die;
12179   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
12180 }
12181
12182 /* Helper function for tree_add_const_value_attribute.  Natively encode
12183    initializer INIT into an array.  Return true if successful.  */
12184
12185 static bool
12186 native_encode_initializer (tree init, unsigned char *array, int size)
12187 {
12188   tree type;
12189
12190   if (init == NULL_TREE)
12191     return false;
12192
12193   STRIP_NOPS (init);
12194   switch (TREE_CODE (init))
12195     {
12196     case STRING_CST:
12197       type = TREE_TYPE (init);
12198       if (TREE_CODE (type) == ARRAY_TYPE)
12199         {
12200           tree enttype = TREE_TYPE (type);
12201           enum machine_mode mode = TYPE_MODE (enttype);
12202
12203           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
12204             return false;
12205           if (int_size_in_bytes (type) != size)
12206             return false;
12207           if (size > TREE_STRING_LENGTH (init))
12208             {
12209               memcpy (array, TREE_STRING_POINTER (init),
12210                       TREE_STRING_LENGTH (init));
12211               memset (array + TREE_STRING_LENGTH (init),
12212                       '\0', size - TREE_STRING_LENGTH (init));
12213             }
12214           else
12215             memcpy (array, TREE_STRING_POINTER (init), size);
12216           return true;
12217         }
12218       return false;
12219     case CONSTRUCTOR:
12220       type = TREE_TYPE (init);
12221       if (int_size_in_bytes (type) != size)
12222         return false;
12223       if (TREE_CODE (type) == ARRAY_TYPE)
12224         {
12225           HOST_WIDE_INT min_index;
12226           unsigned HOST_WIDE_INT cnt;
12227           int curpos = 0, fieldsize;
12228           constructor_elt *ce;
12229
12230           if (TYPE_DOMAIN (type) == NULL_TREE
12231               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
12232             return false;
12233
12234           fieldsize = int_size_in_bytes (TREE_TYPE (type));
12235           if (fieldsize <= 0)
12236             return false;
12237
12238           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
12239           memset (array, '\0', size);
12240           for (cnt = 0;
12241                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12242                cnt++)
12243             {
12244               tree val = ce->value;
12245               tree index = ce->index;
12246               int pos = curpos;
12247               if (index && TREE_CODE (index) == RANGE_EXPR)
12248                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
12249                       * fieldsize;
12250               else if (index)
12251                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
12252
12253               if (val)
12254                 {
12255                   STRIP_NOPS (val);
12256                   if (!native_encode_initializer (val, array + pos, fieldsize))
12257                     return false;
12258                 }
12259               curpos = pos + fieldsize;
12260               if (index && TREE_CODE (index) == RANGE_EXPR)
12261                 {
12262                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
12263                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
12264                   while (count > 0)
12265                     {
12266                       if (val)
12267                         memcpy (array + curpos, array + pos, fieldsize);
12268                       curpos += fieldsize;
12269                     }
12270                 }
12271               gcc_assert (curpos <= size);
12272             }
12273           return true;
12274         }
12275       else if (TREE_CODE (type) == RECORD_TYPE
12276                || TREE_CODE (type) == UNION_TYPE)
12277         {
12278           tree field = NULL_TREE;
12279           unsigned HOST_WIDE_INT cnt;
12280           constructor_elt *ce;
12281
12282           if (int_size_in_bytes (type) != size)
12283             return false;
12284
12285           if (TREE_CODE (type) == RECORD_TYPE)
12286             field = TYPE_FIELDS (type);
12287
12288           for (cnt = 0;
12289                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
12290                cnt++, field = field ? TREE_CHAIN (field) : 0)
12291             {
12292               tree val = ce->value;
12293               int pos, fieldsize;
12294
12295               if (ce->index != 0)
12296                 field = ce->index;
12297
12298               if (val)
12299                 STRIP_NOPS (val);
12300
12301               if (field == NULL_TREE || DECL_BIT_FIELD (field))
12302                 return false;
12303
12304               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
12305                   && TYPE_DOMAIN (TREE_TYPE (field))
12306                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
12307                 return false;
12308               else if (DECL_SIZE_UNIT (field) == NULL_TREE
12309                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
12310                 return false;
12311               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
12312               pos = int_byte_position (field);
12313               gcc_assert (pos + fieldsize <= size);
12314               if (val
12315                   && !native_encode_initializer (val, array + pos, fieldsize))
12316                 return false;
12317             }
12318           return true;
12319         }
12320       return false;
12321     case VIEW_CONVERT_EXPR:
12322     case NON_LVALUE_EXPR:
12323       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
12324     default:
12325       return native_encode_expr (init, array, size) == size;
12326     }
12327 }
12328
12329 /* If we don't have a copy of this variable in memory for some reason (such
12330    as a C++ member constant that doesn't have an out-of-line definition),
12331    we should tell the debugger about the constant value.  */
12332
12333 static void
12334 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
12335 {
12336   tree init;
12337   tree type = TREE_TYPE (decl);
12338   rtx rtl;
12339
12340   if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
12341     return;
12342
12343   init = DECL_INITIAL (decl);
12344   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
12345     /* OK */;
12346   else
12347     return;
12348
12349   rtl = rtl_for_decl_init (init, type);
12350   if (rtl)
12351     add_const_value_attribute (var_die, rtl);
12352   /* If the host and target are sane, try harder.  */
12353   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
12354            && initializer_constant_valid_p (init, type))
12355     {
12356       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
12357       if (size > 0 && (int) size == size)
12358         {
12359           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
12360
12361           if (native_encode_initializer (init, array, size))
12362             add_AT_vec (var_die, DW_AT_const_value, size, 1, array);
12363         }
12364     }
12365 }
12366
12367 /* Convert the CFI instructions for the current function into a
12368    location list.  This is used for DW_AT_frame_base when we targeting
12369    a dwarf2 consumer that does not support the dwarf3
12370    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
12371    expressions.  */
12372
12373 static dw_loc_list_ref
12374 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
12375 {
12376   dw_fde_ref fde;
12377   dw_loc_list_ref list, *list_tail;
12378   dw_cfi_ref cfi;
12379   dw_cfa_location last_cfa, next_cfa;
12380   const char *start_label, *last_label, *section;
12381   dw_cfa_location remember;
12382
12383   fde = current_fde ();
12384   gcc_assert (fde != NULL);
12385
12386   section = secname_for_decl (current_function_decl);
12387   list_tail = &list;
12388   list = NULL;
12389
12390   memset (&next_cfa, 0, sizeof (next_cfa));
12391   next_cfa.reg = INVALID_REGNUM;
12392   remember = next_cfa;
12393
12394   start_label = fde->dw_fde_begin;
12395
12396   /* ??? Bald assumption that the CIE opcode list does not contain
12397      advance opcodes.  */
12398   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
12399     lookup_cfa_1 (cfi, &next_cfa, &remember);
12400
12401   last_cfa = next_cfa;
12402   last_label = start_label;
12403
12404   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
12405     switch (cfi->dw_cfi_opc)
12406       {
12407       case DW_CFA_set_loc:
12408       case DW_CFA_advance_loc1:
12409       case DW_CFA_advance_loc2:
12410       case DW_CFA_advance_loc4:
12411         if (!cfa_equal_p (&last_cfa, &next_cfa))
12412           {
12413             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12414                                        start_label, last_label, section,
12415                                        list == NULL);
12416
12417             list_tail = &(*list_tail)->dw_loc_next;
12418             last_cfa = next_cfa;
12419             start_label = last_label;
12420           }
12421         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
12422         break;
12423
12424       case DW_CFA_advance_loc:
12425         /* The encoding is complex enough that we should never emit this.  */
12426         gcc_unreachable ();
12427
12428       default:
12429         lookup_cfa_1 (cfi, &next_cfa, &remember);
12430         break;
12431       }
12432
12433   if (!cfa_equal_p (&last_cfa, &next_cfa))
12434     {
12435       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
12436                                  start_label, last_label, section,
12437                                  list == NULL);
12438       list_tail = &(*list_tail)->dw_loc_next;
12439       start_label = last_label;
12440     }
12441   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
12442                              start_label, fde->dw_fde_end, section,
12443                              list == NULL);
12444
12445   return list;
12446 }
12447
12448 /* Compute a displacement from the "steady-state frame pointer" to the
12449    frame base (often the same as the CFA), and store it in
12450    frame_pointer_fb_offset.  OFFSET is added to the displacement
12451    before the latter is negated.  */
12452
12453 static void
12454 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
12455 {
12456   rtx reg, elim;
12457
12458 #ifdef FRAME_POINTER_CFA_OFFSET
12459   reg = frame_pointer_rtx;
12460   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
12461 #else
12462   reg = arg_pointer_rtx;
12463   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
12464 #endif
12465
12466   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12467   if (GET_CODE (elim) == PLUS)
12468     {
12469       offset += INTVAL (XEXP (elim, 1));
12470       elim = XEXP (elim, 0);
12471     }
12472
12473   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12474                && (elim == hard_frame_pointer_rtx
12475                    || elim == stack_pointer_rtx))
12476               || elim == (frame_pointer_needed
12477                           ? hard_frame_pointer_rtx
12478                           : stack_pointer_rtx));
12479
12480   frame_pointer_fb_offset = -offset;
12481 }
12482
12483 /* Generate a DW_AT_name attribute given some string value to be included as
12484    the value of the attribute.  */
12485
12486 static void
12487 add_name_attribute (dw_die_ref die, const char *name_string)
12488 {
12489   if (name_string != NULL && *name_string != 0)
12490     {
12491       if (demangle_name_func)
12492         name_string = (*demangle_name_func) (name_string);
12493
12494       add_AT_string (die, DW_AT_name, name_string);
12495     }
12496 }
12497
12498 /* Generate a DW_AT_comp_dir attribute for DIE.  */
12499
12500 static void
12501 add_comp_dir_attribute (dw_die_ref die)
12502 {
12503   const char *wd = get_src_pwd ();
12504   if (wd != NULL)
12505     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
12506 }
12507
12508 /* Given a tree node describing an array bound (either lower or upper) output
12509    a representation for that bound.  */
12510
12511 static void
12512 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
12513 {
12514   switch (TREE_CODE (bound))
12515     {
12516     case ERROR_MARK:
12517       return;
12518
12519     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
12520     case INTEGER_CST:
12521       if (! host_integerp (bound, 0)
12522           || (bound_attr == DW_AT_lower_bound
12523               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
12524                   || (is_fortran () && integer_onep (bound)))))
12525         /* Use the default.  */
12526         ;
12527       else
12528         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
12529       break;
12530
12531     CASE_CONVERT:
12532     case VIEW_CONVERT_EXPR:
12533       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
12534       break;
12535
12536     case SAVE_EXPR:
12537       break;
12538
12539     case VAR_DECL:
12540     case PARM_DECL:
12541     case RESULT_DECL:
12542       {
12543         dw_die_ref decl_die = lookup_decl_die (bound);
12544         dw_loc_descr_ref loc;
12545
12546         /* ??? Can this happen, or should the variable have been bound
12547            first?  Probably it can, since I imagine that we try to create
12548            the types of parameters in the order in which they exist in
12549            the list, and won't have created a forward reference to a
12550            later parameter.  */
12551         if (decl_die != NULL)
12552           add_AT_die_ref (subrange_die, bound_attr, decl_die);
12553         else
12554           {
12555             loc = loc_descriptor_from_tree_1 (bound, 0);
12556             add_AT_location_description (subrange_die, bound_attr, loc);
12557           }
12558         break;
12559       }
12560
12561     default:
12562       {
12563         /* Otherwise try to create a stack operation procedure to
12564            evaluate the value of the array bound.  */
12565
12566         dw_die_ref ctx, decl_die;
12567         dw_loc_descr_ref loc;
12568
12569         loc = loc_descriptor_from_tree (bound);
12570         if (loc == NULL)
12571           break;
12572
12573         if (current_function_decl == 0)
12574           ctx = comp_unit_die;
12575         else
12576           ctx = lookup_decl_die (current_function_decl);
12577
12578         decl_die = new_die (DW_TAG_variable, ctx, bound);
12579         add_AT_flag (decl_die, DW_AT_artificial, 1);
12580         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
12581         add_AT_loc (decl_die, DW_AT_location, loc);
12582
12583         add_AT_die_ref (subrange_die, bound_attr, decl_die);
12584         break;
12585       }
12586     }
12587 }
12588
12589 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
12590    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
12591    Note that the block of subscript information for an array type also
12592    includes information about the element type of the given array type.  */
12593
12594 static void
12595 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
12596 {
12597   unsigned dimension_number;
12598   tree lower, upper;
12599   dw_die_ref subrange_die;
12600
12601   for (dimension_number = 0;
12602        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
12603        type = TREE_TYPE (type), dimension_number++)
12604     {
12605       tree domain = TYPE_DOMAIN (type);
12606
12607       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
12608         break;
12609
12610       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
12611          and (in GNU C only) variable bounds.  Handle all three forms
12612          here.  */
12613       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
12614       if (domain)
12615         {
12616           /* We have an array type with specified bounds.  */
12617           lower = TYPE_MIN_VALUE (domain);
12618           upper = TYPE_MAX_VALUE (domain);
12619
12620           /* Define the index type.  */
12621           if (TREE_TYPE (domain))
12622             {
12623               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
12624                  TREE_TYPE field.  We can't emit debug info for this
12625                  because it is an unnamed integral type.  */
12626               if (TREE_CODE (domain) == INTEGER_TYPE
12627                   && TYPE_NAME (domain) == NULL_TREE
12628                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
12629                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
12630                 ;
12631               else
12632                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
12633                                     type_die);
12634             }
12635
12636           /* ??? If upper is NULL, the array has unspecified length,
12637              but it does have a lower bound.  This happens with Fortran
12638                dimension arr(N:*)
12639              Since the debugger is definitely going to need to know N
12640              to produce useful results, go ahead and output the lower
12641              bound solo, and hope the debugger can cope.  */
12642
12643           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
12644           if (upper)
12645             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
12646         }
12647
12648       /* Otherwise we have an array type with an unspecified length.  The
12649          DWARF-2 spec does not say how to handle this; let's just leave out the
12650          bounds.  */
12651     }
12652 }
12653
12654 static void
12655 add_byte_size_attribute (dw_die_ref die, tree tree_node)
12656 {
12657   unsigned size;
12658
12659   switch (TREE_CODE (tree_node))
12660     {
12661     case ERROR_MARK:
12662       size = 0;
12663       break;
12664     case ENUMERAL_TYPE:
12665     case RECORD_TYPE:
12666     case UNION_TYPE:
12667     case QUAL_UNION_TYPE:
12668       size = int_size_in_bytes (tree_node);
12669       break;
12670     case FIELD_DECL:
12671       /* For a data member of a struct or union, the DW_AT_byte_size is
12672          generally given as the number of bytes normally allocated for an
12673          object of the *declared* type of the member itself.  This is true
12674          even for bit-fields.  */
12675       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
12676       break;
12677     default:
12678       gcc_unreachable ();
12679     }
12680
12681   /* Note that `size' might be -1 when we get to this point.  If it is, that
12682      indicates that the byte size of the entity in question is variable.  We
12683      have no good way of expressing this fact in Dwarf at the present time,
12684      so just let the -1 pass on through.  */
12685   add_AT_unsigned (die, DW_AT_byte_size, size);
12686 }
12687
12688 /* For a FIELD_DECL node which represents a bit-field, output an attribute
12689    which specifies the distance in bits from the highest order bit of the
12690    "containing object" for the bit-field to the highest order bit of the
12691    bit-field itself.
12692
12693    For any given bit-field, the "containing object" is a hypothetical object
12694    (of some integral or enum type) within which the given bit-field lives.  The
12695    type of this hypothetical "containing object" is always the same as the
12696    declared type of the individual bit-field itself.  The determination of the
12697    exact location of the "containing object" for a bit-field is rather
12698    complicated.  It's handled by the `field_byte_offset' function (above).
12699
12700    Note that it is the size (in bytes) of the hypothetical "containing object"
12701    which will be given in the DW_AT_byte_size attribute for this bit-field.
12702    (See `byte_size_attribute' above).  */
12703
12704 static inline void
12705 add_bit_offset_attribute (dw_die_ref die, tree decl)
12706 {
12707   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
12708   tree type = DECL_BIT_FIELD_TYPE (decl);
12709   HOST_WIDE_INT bitpos_int;
12710   HOST_WIDE_INT highest_order_object_bit_offset;
12711   HOST_WIDE_INT highest_order_field_bit_offset;
12712   HOST_WIDE_INT unsigned bit_offset;
12713
12714   /* Must be a field and a bit field.  */
12715   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
12716
12717   /* We can't yet handle bit-fields whose offsets are variable, so if we
12718      encounter such things, just return without generating any attribute
12719      whatsoever.  Likewise for variable or too large size.  */
12720   if (! host_integerp (bit_position (decl), 0)
12721       || ! host_integerp (DECL_SIZE (decl), 1))
12722     return;
12723
12724   bitpos_int = int_bit_position (decl);
12725
12726   /* Note that the bit offset is always the distance (in bits) from the
12727      highest-order bit of the "containing object" to the highest-order bit of
12728      the bit-field itself.  Since the "high-order end" of any object or field
12729      is different on big-endian and little-endian machines, the computation
12730      below must take account of these differences.  */
12731   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
12732   highest_order_field_bit_offset = bitpos_int;
12733
12734   if (! BYTES_BIG_ENDIAN)
12735     {
12736       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
12737       highest_order_object_bit_offset += simple_type_size_in_bits (type);
12738     }
12739
12740   bit_offset
12741     = (! BYTES_BIG_ENDIAN
12742        ? highest_order_object_bit_offset - highest_order_field_bit_offset
12743        : highest_order_field_bit_offset - highest_order_object_bit_offset);
12744
12745   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
12746 }
12747
12748 /* For a FIELD_DECL node which represents a bit field, output an attribute
12749    which specifies the length in bits of the given field.  */
12750
12751 static inline void
12752 add_bit_size_attribute (dw_die_ref die, tree decl)
12753 {
12754   /* Must be a field and a bit field.  */
12755   gcc_assert (TREE_CODE (decl) == FIELD_DECL
12756               && DECL_BIT_FIELD_TYPE (decl));
12757
12758   if (host_integerp (DECL_SIZE (decl), 1))
12759     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
12760 }
12761
12762 /* If the compiled language is ANSI C, then add a 'prototyped'
12763    attribute, if arg types are given for the parameters of a function.  */
12764
12765 static inline void
12766 add_prototyped_attribute (dw_die_ref die, tree func_type)
12767 {
12768   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
12769       && TYPE_ARG_TYPES (func_type) != NULL)
12770     add_AT_flag (die, DW_AT_prototyped, 1);
12771 }
12772
12773 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
12774    by looking in either the type declaration or object declaration
12775    equate table.  */
12776
12777 static inline dw_die_ref
12778 add_abstract_origin_attribute (dw_die_ref die, tree origin)
12779 {
12780   dw_die_ref origin_die = NULL;
12781
12782   if (TREE_CODE (origin) != FUNCTION_DECL)
12783     {
12784       /* We may have gotten separated from the block for the inlined
12785          function, if we're in an exception handler or some such; make
12786          sure that the abstract function has been written out.
12787
12788          Doing this for nested functions is wrong, however; functions are
12789          distinct units, and our context might not even be inline.  */
12790       tree fn = origin;
12791
12792       if (TYPE_P (fn))
12793         fn = TYPE_STUB_DECL (fn);
12794
12795       fn = decl_function_context (fn);
12796       if (fn)
12797         dwarf2out_abstract_function (fn);
12798     }
12799
12800   if (DECL_P (origin))
12801     origin_die = lookup_decl_die (origin);
12802   else if (TYPE_P (origin))
12803     origin_die = lookup_type_die (origin);
12804
12805   /* XXX: Functions that are never lowered don't always have correct block
12806      trees (in the case of java, they simply have no block tree, in some other
12807      languages).  For these functions, there is nothing we can really do to
12808      output correct debug info for inlined functions in all cases.  Rather
12809      than die, we'll just produce deficient debug info now, in that we will
12810      have variables without a proper abstract origin.  In the future, when all
12811      functions are lowered, we should re-add a gcc_assert (origin_die)
12812      here.  */
12813
12814   if (origin_die)
12815     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
12816   return origin_die;
12817 }
12818
12819 /* We do not currently support the pure_virtual attribute.  */
12820
12821 static inline void
12822 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
12823 {
12824   if (DECL_VINDEX (func_decl))
12825     {
12826       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
12827
12828       if (host_integerp (DECL_VINDEX (func_decl), 0))
12829         add_AT_loc (die, DW_AT_vtable_elem_location,
12830                     new_loc_descr (DW_OP_constu,
12831                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
12832                                    0));
12833
12834       /* GNU extension: Record what type this method came from originally.  */
12835       if (debug_info_level > DINFO_LEVEL_TERSE)
12836         add_AT_die_ref (die, DW_AT_containing_type,
12837                         lookup_type_die (DECL_CONTEXT (func_decl)));
12838     }
12839 }
12840 \f
12841 /* Add source coordinate attributes for the given decl.  */
12842
12843 static void
12844 add_src_coords_attributes (dw_die_ref die, tree decl)
12845 {
12846   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12847
12848   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
12849   add_AT_unsigned (die, DW_AT_decl_line, s.line);
12850 }
12851
12852 /* Add a DW_AT_name attribute and source coordinate attribute for the
12853    given decl, but only if it actually has a name.  */
12854
12855 static void
12856 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
12857 {
12858   tree decl_name;
12859
12860   decl_name = DECL_NAME (decl);
12861   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
12862     {
12863       add_name_attribute (die, dwarf2_name (decl, 0));
12864       if (! DECL_ARTIFICIAL (decl))
12865         add_src_coords_attributes (die, decl);
12866
12867       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
12868           && TREE_PUBLIC (decl)
12869           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
12870           && !DECL_ABSTRACT (decl)
12871           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
12872           && !is_fortran ())
12873         add_AT_string (die, DW_AT_MIPS_linkage_name,
12874                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
12875     }
12876
12877 #ifdef VMS_DEBUGGING_INFO
12878   /* Get the function's name, as described by its RTL.  This may be different
12879      from the DECL_NAME name used in the source file.  */
12880   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
12881     {
12882       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
12883                    XEXP (DECL_RTL (decl), 0));
12884       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
12885     }
12886 #endif
12887 }
12888
12889 /* Push a new declaration scope.  */
12890
12891 static void
12892 push_decl_scope (tree scope)
12893 {
12894   VEC_safe_push (tree, gc, decl_scope_table, scope);
12895 }
12896
12897 /* Pop a declaration scope.  */
12898
12899 static inline void
12900 pop_decl_scope (void)
12901 {
12902   VEC_pop (tree, decl_scope_table);
12903 }
12904
12905 /* Return the DIE for the scope that immediately contains this type.
12906    Non-named types get global scope.  Named types nested in other
12907    types get their containing scope if it's open, or global scope
12908    otherwise.  All other types (i.e. function-local named types) get
12909    the current active scope.  */
12910
12911 static dw_die_ref
12912 scope_die_for (tree t, dw_die_ref context_die)
12913 {
12914   dw_die_ref scope_die = NULL;
12915   tree containing_scope;
12916   int i;
12917
12918   /* Non-types always go in the current scope.  */
12919   gcc_assert (TYPE_P (t));
12920
12921   containing_scope = TYPE_CONTEXT (t);
12922
12923   /* Use the containing namespace if it was passed in (for a declaration).  */
12924   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
12925     {
12926       if (context_die == lookup_decl_die (containing_scope))
12927         /* OK */;
12928       else
12929         containing_scope = NULL_TREE;
12930     }
12931
12932   /* Ignore function type "scopes" from the C frontend.  They mean that
12933      a tagged type is local to a parmlist of a function declarator, but
12934      that isn't useful to DWARF.  */
12935   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
12936     containing_scope = NULL_TREE;
12937
12938   if (containing_scope == NULL_TREE)
12939     scope_die = comp_unit_die;
12940   else if (TYPE_P (containing_scope))
12941     {
12942       /* For types, we can just look up the appropriate DIE.  But
12943          first we check to see if we're in the middle of emitting it
12944          so we know where the new DIE should go.  */
12945       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
12946         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
12947           break;
12948
12949       if (i < 0)
12950         {
12951           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
12952                       || TREE_ASM_WRITTEN (containing_scope));
12953
12954           /* If none of the current dies are suitable, we get file scope.  */
12955           scope_die = comp_unit_die;
12956         }
12957       else
12958         scope_die = lookup_type_die (containing_scope);
12959     }
12960   else
12961     scope_die = context_die;
12962
12963   return scope_die;
12964 }
12965
12966 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
12967
12968 static inline int
12969 local_scope_p (dw_die_ref context_die)
12970 {
12971   for (; context_die; context_die = context_die->die_parent)
12972     if (context_die->die_tag == DW_TAG_inlined_subroutine
12973         || context_die->die_tag == DW_TAG_subprogram)
12974       return 1;
12975
12976   return 0;
12977 }
12978
12979 /* Returns nonzero if CONTEXT_DIE is a class.  */
12980
12981 static inline int
12982 class_scope_p (dw_die_ref context_die)
12983 {
12984   return (context_die
12985           && (context_die->die_tag == DW_TAG_structure_type
12986               || context_die->die_tag == DW_TAG_class_type
12987               || context_die->die_tag == DW_TAG_interface_type
12988               || context_die->die_tag == DW_TAG_union_type));
12989 }
12990
12991 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
12992    whether or not to treat a DIE in this context as a declaration.  */
12993
12994 static inline int
12995 class_or_namespace_scope_p (dw_die_ref context_die)
12996 {
12997   return (class_scope_p (context_die)
12998           || (context_die && context_die->die_tag == DW_TAG_namespace));
12999 }
13000
13001 /* Many forms of DIEs require a "type description" attribute.  This
13002    routine locates the proper "type descriptor" die for the type given
13003    by 'type', and adds a DW_AT_type attribute below the given die.  */
13004
13005 static void
13006 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
13007                     int decl_volatile, dw_die_ref context_die)
13008 {
13009   enum tree_code code  = TREE_CODE (type);
13010   dw_die_ref type_die  = NULL;
13011
13012   /* ??? If this type is an unnamed subrange type of an integral, floating-point
13013      or fixed-point type, use the inner type.  This is because we have no
13014      support for unnamed types in base_type_die.  This can happen if this is
13015      an Ada subrange type.  Correct solution is emit a subrange type die.  */
13016   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
13017       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
13018     type = TREE_TYPE (type), code = TREE_CODE (type);
13019
13020   if (code == ERROR_MARK
13021       /* Handle a special case.  For functions whose return type is void, we
13022          generate *no* type attribute.  (Note that no object may have type
13023          `void', so this only applies to function return types).  */
13024       || code == VOID_TYPE)
13025     return;
13026
13027   type_die = modified_type_die (type,
13028                                 decl_const || TYPE_READONLY (type),
13029                                 decl_volatile || TYPE_VOLATILE (type),
13030                                 context_die);
13031
13032   if (type_die != NULL)
13033     add_AT_die_ref (object_die, DW_AT_type, type_die);
13034 }
13035
13036 /* Given an object die, add the calling convention attribute for the
13037    function call type.  */
13038 static void
13039 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
13040 {
13041   enum dwarf_calling_convention value = DW_CC_normal;
13042
13043   value = ((enum dwarf_calling_convention)
13044            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
13045
13046   /* DWARF doesn't provide a way to identify a program's source-level
13047      entry point.  DW_AT_calling_convention attributes are only meant
13048      to describe functions' calling conventions.  However, lacking a
13049      better way to signal the Fortran main program, we use this for the
13050      time being, following existing custom.  */
13051   if (is_fortran ()
13052       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
13053     value = DW_CC_program;
13054
13055   /* Only add the attribute if the backend requests it, and
13056      is not DW_CC_normal.  */
13057   if (value && (value != DW_CC_normal))
13058     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
13059 }
13060
13061 /* Given a tree pointer to a struct, class, union, or enum type node, return
13062    a pointer to the (string) tag name for the given type, or zero if the type
13063    was declared without a tag.  */
13064
13065 static const char *
13066 type_tag (const_tree type)
13067 {
13068   const char *name = 0;
13069
13070   if (TYPE_NAME (type) != 0)
13071     {
13072       tree t = 0;
13073
13074       /* Find the IDENTIFIER_NODE for the type name.  */
13075       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
13076         t = TYPE_NAME (type);
13077
13078       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
13079          a TYPE_DECL node, regardless of whether or not a `typedef' was
13080          involved.  */
13081       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13082                && ! DECL_IGNORED_P (TYPE_NAME (type)))
13083         {
13084           /* We want to be extra verbose.  Don't call dwarf_name if
13085              DECL_NAME isn't set.  The default hook for decl_printable_name
13086              doesn't like that, and in this context it's correct to return
13087              0, instead of "<anonymous>" or the like.  */
13088           if (DECL_NAME (TYPE_NAME (type)))
13089             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
13090         }
13091
13092       /* Now get the name as a string, or invent one.  */
13093       if (!name && t != 0)
13094         name = IDENTIFIER_POINTER (t);
13095     }
13096
13097   return (name == 0 || *name == '\0') ? 0 : name;
13098 }
13099
13100 /* Return the type associated with a data member, make a special check
13101    for bit field types.  */
13102
13103 static inline tree
13104 member_declared_type (const_tree member)
13105 {
13106   return (DECL_BIT_FIELD_TYPE (member)
13107           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
13108 }
13109
13110 /* Get the decl's label, as described by its RTL. This may be different
13111    from the DECL_NAME name used in the source file.  */
13112
13113 #if 0
13114 static const char *
13115 decl_start_label (tree decl)
13116 {
13117   rtx x;
13118   const char *fnname;
13119
13120   x = DECL_RTL (decl);
13121   gcc_assert (MEM_P (x));
13122
13123   x = XEXP (x, 0);
13124   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13125
13126   fnname = XSTR (x, 0);
13127   return fnname;
13128 }
13129 #endif
13130 \f
13131 /* These routines generate the internal representation of the DIE's for
13132    the compilation unit.  Debugging information is collected by walking
13133    the declaration trees passed in from dwarf2out_decl().  */
13134
13135 static void
13136 gen_array_type_die (tree type, dw_die_ref context_die)
13137 {
13138   dw_die_ref scope_die = scope_die_for (type, context_die);
13139   dw_die_ref array_die;
13140
13141   /* GNU compilers represent multidimensional array types as sequences of one
13142      dimensional array types whose element types are themselves array types.
13143      We sometimes squish that down to a single array_type DIE with multiple
13144      subscripts in the Dwarf debugging info.  The draft Dwarf specification
13145      say that we are allowed to do this kind of compression in C, because
13146      there is no difference between an array of arrays and a multidimensional
13147      array.  We don't do this for Ada to remain as close as possible to the
13148      actual representation, which is especially important against the language
13149      flexibilty wrt arrays of variable size.  */
13150
13151   bool collapse_nested_arrays = !is_ada ();
13152   tree element_type;
13153
13154   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
13155      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
13156   if (TYPE_STRING_FLAG (type)
13157       && TREE_CODE (type) == ARRAY_TYPE
13158       && is_fortran ()
13159       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
13160     {
13161       HOST_WIDE_INT size;
13162
13163       array_die = new_die (DW_TAG_string_type, scope_die, type);
13164       add_name_attribute (array_die, type_tag (type));
13165       equate_type_number_to_die (type, array_die);
13166       size = int_size_in_bytes (type);
13167       if (size >= 0)
13168         add_AT_unsigned (array_die, DW_AT_byte_size, size);
13169       else if (TYPE_DOMAIN (type) != NULL_TREE
13170                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
13171                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
13172         {
13173           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
13174           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
13175
13176           size = int_size_in_bytes (TREE_TYPE (szdecl));
13177           if (loc && size > 0)
13178             {
13179               add_AT_loc (array_die, DW_AT_string_length, loc);
13180               if (size != DWARF2_ADDR_SIZE)
13181                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
13182             }
13183         }
13184       return;
13185     }
13186
13187   /* ??? The SGI dwarf reader fails for array of array of enum types
13188      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
13189      array type comes before the outer array type.  We thus call gen_type_die
13190      before we new_die and must prevent nested array types collapsing for this
13191      target.  */
13192
13193 #ifdef MIPS_DEBUGGING_INFO
13194   gen_type_die (TREE_TYPE (type), context_die);
13195   collapse_nested_arrays = false;
13196 #endif
13197
13198   array_die = new_die (DW_TAG_array_type, scope_die, type);
13199   add_name_attribute (array_die, type_tag (type));
13200   equate_type_number_to_die (type, array_die);
13201
13202   if (TREE_CODE (type) == VECTOR_TYPE)
13203     {
13204       /* The frontend feeds us a representation for the vector as a struct
13205          containing an array.  Pull out the array type.  */
13206       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
13207       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
13208     }
13209
13210   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13211   if (is_fortran ()
13212       && TREE_CODE (type) == ARRAY_TYPE
13213       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
13214       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
13215     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13216
13217 #if 0
13218   /* We default the array ordering.  SDB will probably do
13219      the right things even if DW_AT_ordering is not present.  It's not even
13220      an issue until we start to get into multidimensional arrays anyway.  If
13221      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
13222      then we'll have to put the DW_AT_ordering attribute back in.  (But if
13223      and when we find out that we need to put these in, we will only do so
13224      for multidimensional arrays.  */
13225   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
13226 #endif
13227
13228 #ifdef MIPS_DEBUGGING_INFO
13229   /* The SGI compilers handle arrays of unknown bound by setting
13230      AT_declaration and not emitting any subrange DIEs.  */
13231   if (! TYPE_DOMAIN (type))
13232     add_AT_flag (array_die, DW_AT_declaration, 1);
13233   else
13234 #endif
13235     add_subscript_info (array_die, type, collapse_nested_arrays);
13236
13237   /* Add representation of the type of the elements of this array type and
13238      emit the corresponding DIE if we haven't done it already.  */  
13239   element_type = TREE_TYPE (type);
13240   if (collapse_nested_arrays)
13241     while (TREE_CODE (element_type) == ARRAY_TYPE)
13242       {
13243         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
13244           break;
13245         element_type = TREE_TYPE (element_type);
13246       }
13247
13248 #ifndef MIPS_DEBUGGING_INFO
13249   gen_type_die (element_type, context_die);
13250 #endif
13251
13252   add_type_attribute (array_die, element_type, 0, 0, context_die);
13253
13254   if (get_AT (array_die, DW_AT_name))
13255     add_pubtype (type, array_die);
13256 }
13257
13258 static dw_loc_descr_ref
13259 descr_info_loc (tree val, tree base_decl)
13260 {
13261   HOST_WIDE_INT size;
13262   dw_loc_descr_ref loc, loc2;
13263   enum dwarf_location_atom op;
13264
13265   if (val == base_decl)
13266     return new_loc_descr (DW_OP_push_object_address, 0, 0);
13267
13268   switch (TREE_CODE (val))
13269     {
13270     CASE_CONVERT:
13271       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13272     case VAR_DECL:
13273       return loc_descriptor_from_tree_1 (val, 0);
13274     case INTEGER_CST:
13275       if (host_integerp (val, 0))
13276         return int_loc_descriptor (tree_low_cst (val, 0));
13277       break;
13278     case INDIRECT_REF:
13279       size = int_size_in_bytes (TREE_TYPE (val));
13280       if (size < 0)
13281         break;
13282       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13283       if (!loc)
13284         break;
13285       if (size == DWARF2_ADDR_SIZE)
13286         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
13287       else
13288         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
13289       return loc;
13290     case POINTER_PLUS_EXPR:
13291     case PLUS_EXPR:
13292       if (host_integerp (TREE_OPERAND (val, 1), 1)
13293           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
13294              < 16384)
13295         {
13296           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13297           if (!loc)
13298             break;
13299           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
13300         }
13301       else
13302         {
13303           op = DW_OP_plus;
13304         do_binop:
13305           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
13306           if (!loc)
13307             break;
13308           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
13309           if (!loc2)
13310             break;
13311           add_loc_descr (&loc, loc2);
13312           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
13313         }
13314       return loc;
13315     case MINUS_EXPR:
13316       op = DW_OP_minus;
13317       goto do_binop;
13318     case MULT_EXPR:
13319       op = DW_OP_mul;
13320       goto do_binop;
13321     case EQ_EXPR:
13322       op = DW_OP_eq;
13323       goto do_binop;
13324     case NE_EXPR:
13325       op = DW_OP_ne;
13326       goto do_binop;
13327     default:
13328       break;
13329     }
13330   return NULL;
13331 }
13332
13333 static void
13334 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
13335                       tree val, tree base_decl)
13336 {
13337   dw_loc_descr_ref loc;
13338
13339   if (host_integerp (val, 0))
13340     {
13341       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
13342       return;
13343     }
13344
13345   loc = descr_info_loc (val, base_decl);
13346   if (!loc)
13347     return;
13348
13349   add_AT_loc (die, attr, loc);
13350 }
13351
13352 /* This routine generates DIE for array with hidden descriptor, details
13353    are filled into *info by a langhook.  */
13354
13355 static void
13356 gen_descr_array_type_die (tree type, struct array_descr_info *info,
13357                           dw_die_ref context_die)
13358 {
13359   dw_die_ref scope_die = scope_die_for (type, context_die);
13360   dw_die_ref array_die;
13361   int dim;
13362
13363   array_die = new_die (DW_TAG_array_type, scope_die, type);
13364   add_name_attribute (array_die, type_tag (type));
13365   equate_type_number_to_die (type, array_die);
13366
13367   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
13368   if (is_fortran ()
13369       && info->ndimensions >= 2)
13370     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
13371
13372   if (info->data_location)
13373     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
13374                           info->base_decl);
13375   if (info->associated)
13376     add_descr_info_field (array_die, DW_AT_associated, info->associated,
13377                           info->base_decl);
13378   if (info->allocated)
13379     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
13380                           info->base_decl);
13381
13382   for (dim = 0; dim < info->ndimensions; dim++)
13383     {
13384       dw_die_ref subrange_die
13385         = new_die (DW_TAG_subrange_type, array_die, NULL);
13386
13387       if (info->dimen[dim].lower_bound)
13388         {
13389           /* If it is the default value, omit it.  */
13390           if ((is_c_family () || is_java ())
13391               && integer_zerop (info->dimen[dim].lower_bound))
13392             ;
13393           else if (is_fortran ()
13394                    && integer_onep (info->dimen[dim].lower_bound))
13395             ;
13396           else
13397             add_descr_info_field (subrange_die, DW_AT_lower_bound,
13398                                   info->dimen[dim].lower_bound,
13399                                   info->base_decl);
13400         }
13401       if (info->dimen[dim].upper_bound)
13402         add_descr_info_field (subrange_die, DW_AT_upper_bound,
13403                               info->dimen[dim].upper_bound,
13404                               info->base_decl);
13405       if (info->dimen[dim].stride)
13406         add_descr_info_field (subrange_die, DW_AT_byte_stride,
13407                               info->dimen[dim].stride,
13408                               info->base_decl);
13409     }
13410
13411   gen_type_die (info->element_type, context_die);
13412   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
13413
13414   if (get_AT (array_die, DW_AT_name))
13415     add_pubtype (type, array_die);
13416 }
13417
13418 #if 0
13419 static void
13420 gen_entry_point_die (tree decl, dw_die_ref context_die)
13421 {
13422   tree origin = decl_ultimate_origin (decl);
13423   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
13424
13425   if (origin != NULL)
13426     add_abstract_origin_attribute (decl_die, origin);
13427   else
13428     {
13429       add_name_and_src_coords_attributes (decl_die, decl);
13430       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
13431                           0, 0, context_die);
13432     }
13433
13434   if (DECL_ABSTRACT (decl))
13435     equate_decl_number_to_die (decl, decl_die);
13436   else
13437     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
13438 }
13439 #endif
13440
13441 /* Walk through the list of incomplete types again, trying once more to
13442    emit full debugging info for them.  */
13443
13444 static void
13445 retry_incomplete_types (void)
13446 {
13447   int i;
13448
13449   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
13450     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
13451 }
13452
13453 /* Determine what tag to use for a record type.  */
13454
13455 static enum dwarf_tag
13456 record_type_tag (tree type)
13457 {
13458   if (! lang_hooks.types.classify_record)
13459     return DW_TAG_structure_type;
13460
13461   switch (lang_hooks.types.classify_record (type))
13462     {
13463     case RECORD_IS_STRUCT:
13464       return DW_TAG_structure_type;
13465
13466     case RECORD_IS_CLASS:
13467       return DW_TAG_class_type;
13468
13469     case RECORD_IS_INTERFACE:
13470       return DW_TAG_interface_type;
13471
13472     default:
13473       gcc_unreachable ();
13474     }
13475 }
13476
13477 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
13478    include all of the information about the enumeration values also. Each
13479    enumerated type name/value is listed as a child of the enumerated type
13480    DIE.  */
13481
13482 static dw_die_ref
13483 gen_enumeration_type_die (tree type, dw_die_ref context_die)
13484 {
13485   dw_die_ref type_die = lookup_type_die (type);
13486
13487   if (type_die == NULL)
13488     {
13489       type_die = new_die (DW_TAG_enumeration_type,
13490                           scope_die_for (type, context_die), type);
13491       equate_type_number_to_die (type, type_die);
13492       add_name_attribute (type_die, type_tag (type));
13493     }
13494   else if (! TYPE_SIZE (type))
13495     return type_die;
13496   else
13497     remove_AT (type_die, DW_AT_declaration);
13498
13499   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
13500      given enum type is incomplete, do not generate the DW_AT_byte_size
13501      attribute or the DW_AT_element_list attribute.  */
13502   if (TYPE_SIZE (type))
13503     {
13504       tree link;
13505
13506       TREE_ASM_WRITTEN (type) = 1;
13507       add_byte_size_attribute (type_die, type);
13508       if (TYPE_STUB_DECL (type) != NULL_TREE)
13509         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13510
13511       /* If the first reference to this type was as the return type of an
13512          inline function, then it may not have a parent.  Fix this now.  */
13513       if (type_die->die_parent == NULL)
13514         add_child_die (scope_die_for (type, context_die), type_die);
13515
13516       for (link = TYPE_VALUES (type);
13517            link != NULL; link = TREE_CHAIN (link))
13518         {
13519           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
13520           tree value = TREE_VALUE (link);
13521
13522           add_name_attribute (enum_die,
13523                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
13524
13525           if (TREE_CODE (value) == CONST_DECL)
13526             value = DECL_INITIAL (value);
13527
13528           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
13529             /* DWARF2 does not provide a way of indicating whether or
13530                not enumeration constants are signed or unsigned.  GDB
13531                always assumes the values are signed, so we output all
13532                values as if they were signed.  That means that
13533                enumeration constants with very large unsigned values
13534                will appear to have negative values in the debugger.  */
13535             add_AT_int (enum_die, DW_AT_const_value,
13536                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
13537         }
13538     }
13539   else
13540     add_AT_flag (type_die, DW_AT_declaration, 1);
13541
13542   if (get_AT (type_die, DW_AT_name))
13543     add_pubtype (type, type_die);
13544
13545   return type_die;
13546 }
13547
13548 /* Generate a DIE to represent either a real live formal parameter decl or to
13549    represent just the type of some formal parameter position in some function
13550    type.
13551
13552    Note that this routine is a bit unusual because its argument may be a
13553    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
13554    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
13555    node.  If it's the former then this function is being called to output a
13556    DIE to represent a formal parameter object (or some inlining thereof).  If
13557    it's the latter, then this function is only being called to output a
13558    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
13559    argument type of some subprogram type.  */
13560
13561 static dw_die_ref
13562 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
13563 {
13564   tree node_or_origin = node ? node : origin;
13565   dw_die_ref parm_die
13566     = new_die (DW_TAG_formal_parameter, context_die, node);
13567
13568   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
13569     {
13570     case tcc_declaration:
13571       if (!origin)
13572         origin = decl_ultimate_origin (node);
13573       if (origin != NULL)
13574         add_abstract_origin_attribute (parm_die, origin);
13575       else
13576         {
13577           tree type = TREE_TYPE (node);
13578           add_name_and_src_coords_attributes (parm_die, node);
13579           if (DECL_BY_REFERENCE (node))
13580             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
13581                                 context_die);
13582           else
13583             add_type_attribute (parm_die, type,
13584                                 TREE_READONLY (node),
13585                                 TREE_THIS_VOLATILE (node),
13586                                 context_die);
13587           if (DECL_ARTIFICIAL (node))
13588             add_AT_flag (parm_die, DW_AT_artificial, 1);
13589         }
13590
13591       if (node)
13592         equate_decl_number_to_die (node, parm_die);
13593       if (! DECL_ABSTRACT (node_or_origin))
13594         add_location_or_const_value_attribute (parm_die, node_or_origin,
13595                                                DW_AT_location);
13596
13597       break;
13598
13599     case tcc_type:
13600       /* We were called with some kind of a ..._TYPE node.  */
13601       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
13602       break;
13603
13604     default:
13605       gcc_unreachable ();
13606     }
13607
13608   return parm_die;
13609 }
13610
13611 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
13612    at the end of an (ANSI prototyped) formal parameters list.  */
13613
13614 static void
13615 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
13616 {
13617   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
13618 }
13619
13620 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
13621    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
13622    parameters as specified in some function type specification (except for
13623    those which appear as part of a function *definition*).  */
13624
13625 static void
13626 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
13627 {
13628   tree link;
13629   tree formal_type = NULL;
13630   tree first_parm_type;
13631   tree arg;
13632
13633   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
13634     {
13635       arg = DECL_ARGUMENTS (function_or_method_type);
13636       function_or_method_type = TREE_TYPE (function_or_method_type);
13637     }
13638   else
13639     arg = NULL_TREE;
13640
13641   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
13642
13643   /* Make our first pass over the list of formal parameter types and output a
13644      DW_TAG_formal_parameter DIE for each one.  */
13645   for (link = first_parm_type; link; )
13646     {
13647       dw_die_ref parm_die;
13648
13649       formal_type = TREE_VALUE (link);
13650       if (formal_type == void_type_node)
13651         break;
13652
13653       /* Output a (nameless) DIE to represent the formal parameter itself.  */
13654       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
13655       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
13656            && link == first_parm_type)
13657           || (arg && DECL_ARTIFICIAL (arg)))
13658         add_AT_flag (parm_die, DW_AT_artificial, 1);
13659
13660       link = TREE_CHAIN (link);
13661       if (arg)
13662         arg = TREE_CHAIN (arg);
13663     }
13664
13665   /* If this function type has an ellipsis, add a
13666      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
13667   if (formal_type != void_type_node)
13668     gen_unspecified_parameters_die (function_or_method_type, context_die);
13669
13670   /* Make our second (and final) pass over the list of formal parameter types
13671      and output DIEs to represent those types (as necessary).  */
13672   for (link = TYPE_ARG_TYPES (function_or_method_type);
13673        link && TREE_VALUE (link);
13674        link = TREE_CHAIN (link))
13675     gen_type_die (TREE_VALUE (link), context_die);
13676 }
13677
13678 /* We want to generate the DIE for TYPE so that we can generate the
13679    die for MEMBER, which has been defined; we will need to refer back
13680    to the member declaration nested within TYPE.  If we're trying to
13681    generate minimal debug info for TYPE, processing TYPE won't do the
13682    trick; we need to attach the member declaration by hand.  */
13683
13684 static void
13685 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
13686 {
13687   gen_type_die (type, context_die);
13688
13689   /* If we're trying to avoid duplicate debug info, we may not have
13690      emitted the member decl for this function.  Emit it now.  */
13691   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
13692       && ! lookup_decl_die (member))
13693     {
13694       dw_die_ref type_die;
13695       gcc_assert (!decl_ultimate_origin (member));
13696
13697       push_decl_scope (type);
13698       type_die = lookup_type_die (type);
13699       if (TREE_CODE (member) == FUNCTION_DECL)
13700         gen_subprogram_die (member, type_die);
13701       else if (TREE_CODE (member) == FIELD_DECL)
13702         {
13703           /* Ignore the nameless fields that are used to skip bits but handle
13704              C++ anonymous unions and structs.  */
13705           if (DECL_NAME (member) != NULL_TREE
13706               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
13707               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
13708             {
13709               gen_type_die (member_declared_type (member), type_die);
13710               gen_field_die (member, type_die);
13711             }
13712         }
13713       else
13714         gen_variable_die (member, NULL_TREE, type_die);
13715
13716       pop_decl_scope ();
13717     }
13718 }
13719
13720 /* Generate the DWARF2 info for the "abstract" instance of a function which we
13721    may later generate inlined and/or out-of-line instances of.  */
13722
13723 static void
13724 dwarf2out_abstract_function (tree decl)
13725 {
13726   dw_die_ref old_die;
13727   tree save_fn;
13728   tree context;
13729   int was_abstract = DECL_ABSTRACT (decl);
13730
13731   /* Make sure we have the actual abstract inline, not a clone.  */
13732   decl = DECL_ORIGIN (decl);
13733
13734   old_die = lookup_decl_die (decl);
13735   if (old_die && get_AT (old_die, DW_AT_inline))
13736     /* We've already generated the abstract instance.  */
13737     return;
13738
13739   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
13740      we don't get confused by DECL_ABSTRACT.  */
13741   if (debug_info_level > DINFO_LEVEL_TERSE)
13742     {
13743       context = decl_class_context (decl);
13744       if (context)
13745         gen_type_die_for_member
13746           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
13747     }
13748
13749   /* Pretend we've just finished compiling this function.  */
13750   save_fn = current_function_decl;
13751   current_function_decl = decl;
13752   push_cfun (DECL_STRUCT_FUNCTION (decl));
13753
13754   set_decl_abstract_flags (decl, 1);
13755   dwarf2out_decl (decl);
13756   if (! was_abstract)
13757     set_decl_abstract_flags (decl, 0);
13758
13759   current_function_decl = save_fn;
13760   pop_cfun ();
13761 }
13762
13763 /* Helper function of premark_used_types() which gets called through
13764    htab_traverse_resize().
13765
13766    Marks the DIE of a given type in *SLOT as perennial, so it never gets
13767    marked as unused by prune_unused_types.  */
13768 static int
13769 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
13770 {
13771   tree type;
13772   dw_die_ref die;
13773
13774   type = (tree) *slot;
13775   die = lookup_type_die (type);
13776   if (die != NULL)
13777     die->die_perennial_p = 1;
13778   return 1;
13779 }
13780
13781 /* Mark all members of used_types_hash as perennial.  */
13782 static void
13783 premark_used_types (void)
13784 {
13785   if (cfun && cfun->used_types_hash)
13786     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
13787 }
13788
13789 /* Generate a DIE to represent a declared function (either file-scope or
13790    block-local).  */
13791
13792 static void
13793 gen_subprogram_die (tree decl, dw_die_ref context_die)
13794 {
13795   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13796   tree origin = decl_ultimate_origin (decl);
13797   dw_die_ref subr_die;
13798   tree fn_arg_types;
13799   tree outer_scope;
13800   dw_die_ref old_die = lookup_decl_die (decl);
13801   int declaration = (current_function_decl != decl
13802                      || class_or_namespace_scope_p (context_die));
13803
13804   premark_used_types ();
13805
13806   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
13807      started to generate the abstract instance of an inline, decided to output
13808      its containing class, and proceeded to emit the declaration of the inline
13809      from the member list for the class.  If so, DECLARATION takes priority;
13810      we'll get back to the abstract instance when done with the class.  */
13811
13812   /* The class-scope declaration DIE must be the primary DIE.  */
13813   if (origin && declaration && class_or_namespace_scope_p (context_die))
13814     {
13815       origin = NULL;
13816       gcc_assert (!old_die);
13817     }
13818
13819   /* Now that the C++ front end lazily declares artificial member fns, we
13820      might need to retrofit the declaration into its class.  */
13821   if (!declaration && !origin && !old_die
13822       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
13823       && !class_or_namespace_scope_p (context_die)
13824       && debug_info_level > DINFO_LEVEL_TERSE)
13825     old_die = force_decl_die (decl);
13826
13827   if (origin != NULL)
13828     {
13829       gcc_assert (!declaration || local_scope_p (context_die));
13830
13831       /* Fixup die_parent for the abstract instance of a nested
13832          inline function.  */
13833       if (old_die && old_die->die_parent == NULL)
13834         add_child_die (context_die, old_die);
13835
13836       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13837       add_abstract_origin_attribute (subr_die, origin);
13838     }
13839   else if (old_die)
13840     {
13841       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
13842       struct dwarf_file_data * file_index = lookup_filename (s.file);
13843
13844       if (!get_AT_flag (old_die, DW_AT_declaration)
13845           /* We can have a normal definition following an inline one in the
13846              case of redefinition of GNU C extern inlines.
13847              It seems reasonable to use AT_specification in this case.  */
13848           && !get_AT (old_die, DW_AT_inline))
13849         {
13850           /* Detect and ignore this case, where we are trying to output
13851              something we have already output.  */
13852           return;
13853         }
13854
13855       /* If the definition comes from the same place as the declaration,
13856          maybe use the old DIE.  We always want the DIE for this function
13857          that has the *_pc attributes to be under comp_unit_die so the
13858          debugger can find it.  We also need to do this for abstract
13859          instances of inlines, since the spec requires the out-of-line copy
13860          to have the same parent.  For local class methods, this doesn't
13861          apply; we just use the old DIE.  */
13862       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
13863           && (DECL_ARTIFICIAL (decl)
13864               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
13865                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
13866                       == (unsigned) s.line))))
13867         {
13868           subr_die = old_die;
13869
13870           /* Clear out the declaration attribute and the formal parameters.
13871              Do not remove all children, because it is possible that this
13872              declaration die was forced using force_decl_die(). In such
13873              cases die that forced declaration die (e.g. TAG_imported_module)
13874              is one of the children that we do not want to remove.  */
13875           remove_AT (subr_die, DW_AT_declaration);
13876           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
13877         }
13878       else
13879         {
13880           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13881           add_AT_specification (subr_die, old_die);
13882           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
13883             add_AT_file (subr_die, DW_AT_decl_file, file_index);
13884           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
13885             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
13886         }
13887     }
13888   else
13889     {
13890       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
13891
13892       if (TREE_PUBLIC (decl))
13893         add_AT_flag (subr_die, DW_AT_external, 1);
13894
13895       add_name_and_src_coords_attributes (subr_die, decl);
13896       if (debug_info_level > DINFO_LEVEL_TERSE)
13897         {
13898           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
13899           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
13900                               0, 0, context_die);
13901         }
13902
13903       add_pure_or_virtual_attribute (subr_die, decl);
13904       if (DECL_ARTIFICIAL (decl))
13905         add_AT_flag (subr_die, DW_AT_artificial, 1);
13906
13907       if (TREE_PROTECTED (decl))
13908         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
13909       else if (TREE_PRIVATE (decl))
13910         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
13911     }
13912
13913   if (declaration)
13914     {
13915       if (!old_die || !get_AT (old_die, DW_AT_inline))
13916         {
13917           add_AT_flag (subr_die, DW_AT_declaration, 1);
13918
13919           /* If this is an explicit function declaration then generate
13920              a DW_AT_explicit attribute.  */
13921           if (lang_hooks.decls.function_decl_explicit_p (decl))
13922             add_AT_flag (subr_die, DW_AT_explicit, 1);
13923
13924           /* The first time we see a member function, it is in the context of
13925              the class to which it belongs.  We make sure of this by emitting
13926              the class first.  The next time is the definition, which is
13927              handled above.  The two may come from the same source text.
13928
13929              Note that force_decl_die() forces function declaration die. It is
13930              later reused to represent definition.  */
13931           equate_decl_number_to_die (decl, subr_die);
13932         }
13933     }
13934   else if (DECL_ABSTRACT (decl))
13935     {
13936       if (DECL_DECLARED_INLINE_P (decl))
13937         {
13938           if (cgraph_function_possibly_inlined_p (decl))
13939             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
13940           else
13941             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
13942         }
13943       else
13944         {
13945           if (cgraph_function_possibly_inlined_p (decl))
13946             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
13947           else
13948             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
13949         }
13950
13951       if (DECL_DECLARED_INLINE_P (decl)
13952           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
13953         add_AT_flag (subr_die, DW_AT_artificial, 1);
13954
13955       equate_decl_number_to_die (decl, subr_die);
13956     }
13957   else if (!DECL_EXTERNAL (decl))
13958     {
13959       HOST_WIDE_INT cfa_fb_offset;
13960
13961       if (!old_die || !get_AT (old_die, DW_AT_inline))
13962         equate_decl_number_to_die (decl, subr_die);
13963
13964       if (!flag_reorder_blocks_and_partition)
13965         {
13966           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
13967                                        current_function_funcdef_no);
13968           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
13969           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13970                                        current_function_funcdef_no);
13971           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
13972
13973           add_pubname (decl, subr_die);
13974           add_arange (decl, subr_die);
13975         }
13976       else
13977         {  /* Do nothing for now; maybe need to duplicate die, one for
13978               hot section and one for cold section, then use the hot/cold
13979               section begin/end labels to generate the aranges...  */
13980           /*
13981             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
13982             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
13983             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
13984             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
13985
13986             add_pubname (decl, subr_die);
13987             add_arange (decl, subr_die);
13988             add_arange (decl, subr_die);
13989            */
13990         }
13991
13992 #ifdef MIPS_DEBUGGING_INFO
13993       /* Add a reference to the FDE for this routine.  */
13994       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
13995 #endif
13996
13997       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
13998
13999       /* We define the "frame base" as the function's CFA.  This is more
14000          convenient for several reasons: (1) It's stable across the prologue
14001          and epilogue, which makes it better than just a frame pointer,
14002          (2) With dwarf3, there exists a one-byte encoding that allows us
14003          to reference the .debug_frame data by proxy, but failing that,
14004          (3) We can at least reuse the code inspection and interpretation
14005          code that determines the CFA position at various points in the
14006          function.  */
14007       /* ??? Use some command-line or configury switch to enable the use
14008          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
14009          consumers that understand it; fall back to "pure" dwarf2 and
14010          convert the CFA data into a location list.  */
14011       {
14012         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
14013         if (list->dw_loc_next)
14014           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
14015         else
14016           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
14017       }
14018
14019       /* Compute a displacement from the "steady-state frame pointer" to
14020          the CFA.  The former is what all stack slots and argument slots
14021          will reference in the rtl; the later is what we've told the
14022          debugger about.  We'll need to adjust all frame_base references
14023          by this displacement.  */
14024       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
14025
14026       if (cfun->static_chain_decl)
14027         add_AT_location_description (subr_die, DW_AT_static_link,
14028                  loc_descriptor_from_tree (cfun->static_chain_decl));
14029     }
14030
14031   /* Now output descriptions of the arguments for this function. This gets
14032      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
14033      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
14034      `...' at the end of the formal parameter list.  In order to find out if
14035      there was a trailing ellipsis or not, we must instead look at the type
14036      associated with the FUNCTION_DECL.  This will be a node of type
14037      FUNCTION_TYPE. If the chain of type nodes hanging off of this
14038      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
14039      an ellipsis at the end.  */
14040
14041   /* In the case where we are describing a mere function declaration, all we
14042      need to do here (and all we *can* do here) is to describe the *types* of
14043      its formal parameters.  */
14044   if (debug_info_level <= DINFO_LEVEL_TERSE)
14045     ;
14046   else if (declaration)
14047     gen_formal_types_die (decl, subr_die);
14048   else
14049     {
14050       /* Generate DIEs to represent all known formal parameters.  */
14051       tree arg_decls = DECL_ARGUMENTS (decl);
14052       tree parm;
14053
14054       /* When generating DIEs, generate the unspecified_parameters DIE
14055          instead if we come across the arg "__builtin_va_alist" */
14056       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
14057         if (TREE_CODE (parm) == PARM_DECL)
14058           {
14059             if (DECL_NAME (parm)
14060                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
14061                             "__builtin_va_alist"))
14062               gen_unspecified_parameters_die (parm, subr_die);
14063             else
14064               gen_decl_die (parm, NULL, subr_die);
14065           }
14066
14067       /* Decide whether we need an unspecified_parameters DIE at the end.
14068          There are 2 more cases to do this for: 1) the ansi ... declaration -
14069          this is detectable when the end of the arg list is not a
14070          void_type_node 2) an unprototyped function declaration (not a
14071          definition).  This just means that we have no info about the
14072          parameters at all.  */
14073       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
14074       if (fn_arg_types != NULL)
14075         {
14076           /* This is the prototyped case, check for....  */
14077           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
14078             gen_unspecified_parameters_die (decl, subr_die);
14079         }
14080       else if (DECL_INITIAL (decl) == NULL_TREE)
14081         gen_unspecified_parameters_die (decl, subr_die);
14082     }
14083
14084   /* Output Dwarf info for all of the stuff within the body of the function
14085      (if it has one - it may be just a declaration).  */
14086   outer_scope = DECL_INITIAL (decl);
14087
14088   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
14089      a function.  This BLOCK actually represents the outermost binding contour
14090      for the function, i.e. the contour in which the function's formal
14091      parameters and labels get declared. Curiously, it appears that the front
14092      end doesn't actually put the PARM_DECL nodes for the current function onto
14093      the BLOCK_VARS list for this outer scope, but are strung off of the
14094      DECL_ARGUMENTS list for the function instead.
14095
14096      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
14097      the LABEL_DECL nodes for the function however, and we output DWARF info
14098      for those in decls_for_scope.  Just within the `outer_scope' there will be
14099      a BLOCK node representing the function's outermost pair of curly braces,
14100      and any blocks used for the base and member initializers of a C++
14101      constructor function.  */
14102   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
14103     {
14104       /* Emit a DW_TAG_variable DIE for a named return value.  */
14105       if (DECL_NAME (DECL_RESULT (decl)))
14106         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
14107
14108       current_function_has_inlines = 0;
14109       decls_for_scope (outer_scope, subr_die, 0);
14110
14111 #if 0 && defined (MIPS_DEBUGGING_INFO)
14112       if (current_function_has_inlines)
14113         {
14114           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
14115           if (! comp_unit_has_inlines)
14116             {
14117               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
14118               comp_unit_has_inlines = 1;
14119             }
14120         }
14121 #endif
14122     }
14123   /* Add the calling convention attribute if requested.  */
14124   add_calling_convention_attribute (subr_die, decl);
14125
14126 }
14127
14128 /* Returns a hash value for X (which really is a die_struct).  */
14129
14130 static hashval_t
14131 common_block_die_table_hash (const void *x)
14132 {
14133   const_dw_die_ref d = (const_dw_die_ref) x;
14134   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
14135 }
14136
14137 /* Return nonzero if decl_id and die_parent of die_struct X is the same
14138    as decl_id and die_parent of die_struct Y.  */
14139
14140 static int
14141 common_block_die_table_eq (const void *x, const void *y)
14142 {
14143   const_dw_die_ref d = (const_dw_die_ref) x;
14144   const_dw_die_ref e = (const_dw_die_ref) y;
14145   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
14146 }
14147
14148 /* Generate a DIE to represent a declared data object.
14149    Either DECL or ORIGIN must be non-null.  */
14150
14151 static void
14152 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
14153 {
14154   HOST_WIDE_INT off;
14155   tree com_decl;
14156   tree decl_or_origin = decl ? decl : origin;
14157   dw_die_ref var_die;
14158   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
14159   dw_die_ref origin_die;
14160   int declaration = (DECL_EXTERNAL (decl_or_origin)
14161                      /* If DECL is COMDAT and has not actually been
14162                         emitted, we cannot take its address; there
14163                         might end up being no definition anywhere in
14164                         the program.  For example, consider the C++
14165                         test case:
14166
14167                           template <class T>
14168                           struct S { static const int i = 7; };
14169
14170                           template <class T>
14171                           const int S<T>::i;
14172
14173                           int f() { return S<int>::i; }
14174
14175                         Here, S<int>::i is not DECL_EXTERNAL, but no
14176                         definition is required, so the compiler will
14177                         not emit a definition.  */
14178                      || (TREE_CODE (decl_or_origin) == VAR_DECL
14179                          && DECL_COMDAT (decl_or_origin)
14180                          && !TREE_ASM_WRITTEN (decl_or_origin))
14181                      || class_or_namespace_scope_p (context_die));
14182
14183   if (!origin)
14184     origin = decl_ultimate_origin (decl);
14185
14186   com_decl = fortran_common (decl_or_origin, &off);
14187
14188   /* Symbol in common gets emitted as a child of the common block, in the form
14189      of a data member.  */
14190   if (com_decl)
14191     {
14192       tree field;
14193       dw_die_ref com_die;
14194       dw_loc_descr_ref loc;
14195       die_node com_die_arg;
14196
14197       var_die = lookup_decl_die (decl_or_origin);
14198       if (var_die)
14199         {
14200           if (get_AT (var_die, DW_AT_location) == NULL)
14201             {
14202               loc = loc_descriptor_from_tree (com_decl);
14203               if (loc)
14204                 {
14205                   if (off)
14206                     {
14207                       /* Optimize the common case.  */
14208                       if (loc->dw_loc_opc == DW_OP_addr
14209                           && loc->dw_loc_next == NULL
14210                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
14211                              == SYMBOL_REF)
14212                         loc->dw_loc_oprnd1.v.val_addr
14213                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14214                         else
14215                           loc_descr_plus_const (&loc, off);
14216                     }
14217                   add_AT_loc (var_die, DW_AT_location, loc);
14218                   remove_AT (var_die, DW_AT_declaration);
14219                 }
14220             }
14221           return;
14222         }
14223
14224       if (common_block_die_table == NULL)
14225         common_block_die_table
14226           = htab_create_ggc (10, common_block_die_table_hash,
14227                              common_block_die_table_eq, NULL);
14228
14229       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
14230       com_die_arg.decl_id = DECL_UID (com_decl);
14231       com_die_arg.die_parent = context_die;
14232       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
14233       loc = loc_descriptor_from_tree (com_decl);
14234       if (com_die == NULL)
14235         {
14236           const char *cnam
14237             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
14238           void **slot;
14239
14240           com_die = new_die (DW_TAG_common_block, context_die, decl);
14241           add_name_and_src_coords_attributes (com_die, com_decl);
14242           if (loc)
14243             {
14244               add_AT_loc (com_die, DW_AT_location, loc);
14245               /* Avoid sharing the same loc descriptor between
14246                  DW_TAG_common_block and DW_TAG_variable.  */
14247               loc = loc_descriptor_from_tree (com_decl);
14248             }
14249           else if (DECL_EXTERNAL (decl))
14250             add_AT_flag (com_die, DW_AT_declaration, 1);
14251           add_pubname_string (cnam, com_die); /* ??? needed? */
14252           com_die->decl_id = DECL_UID (com_decl);
14253           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
14254           *slot = (void *) com_die;
14255         }
14256       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
14257         {
14258           add_AT_loc (com_die, DW_AT_location, loc);
14259           loc = loc_descriptor_from_tree (com_decl);
14260           remove_AT (com_die, DW_AT_declaration);
14261         }
14262       var_die = new_die (DW_TAG_variable, com_die, decl);
14263       add_name_and_src_coords_attributes (var_die, decl);
14264       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
14265                           TREE_THIS_VOLATILE (decl), context_die);
14266       add_AT_flag (var_die, DW_AT_external, 1);
14267       if (loc)
14268         {
14269           if (off)
14270             {
14271               /* Optimize the common case.  */
14272               if (loc->dw_loc_opc == DW_OP_addr
14273                   && loc->dw_loc_next == NULL
14274                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
14275                 loc->dw_loc_oprnd1.v.val_addr
14276                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
14277               else
14278                 loc_descr_plus_const (&loc, off);
14279             }
14280           add_AT_loc (var_die, DW_AT_location, loc);
14281         }
14282       else if (DECL_EXTERNAL (decl))
14283         add_AT_flag (var_die, DW_AT_declaration, 1);
14284       equate_decl_number_to_die (decl, var_die);
14285       return;
14286     }
14287
14288   /* If the compiler emitted a definition for the DECL declaration
14289      and if we already emitted a DIE for it, don't emit a second
14290      DIE for it again.  */
14291   if (old_die
14292       && declaration
14293       && old_die->die_parent == context_die)
14294     return;
14295
14296   /* For static data members, the declaration in the class is supposed
14297      to have DW_TAG_member tag; the specification should still be
14298      DW_TAG_variable referencing the DW_TAG_member DIE.  */
14299   if (declaration && class_scope_p (context_die))
14300     var_die = new_die (DW_TAG_member, context_die, decl);
14301   else
14302     var_die = new_die (DW_TAG_variable, context_die, decl);
14303
14304   origin_die = NULL;
14305   if (origin != NULL)
14306     origin_die = add_abstract_origin_attribute (var_die, origin);
14307
14308   /* Loop unrolling can create multiple blocks that refer to the same
14309      static variable, so we must test for the DW_AT_declaration flag.
14310
14311      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
14312      copy decls and set the DECL_ABSTRACT flag on them instead of
14313      sharing them.
14314
14315      ??? Duplicated blocks have been rewritten to use .debug_ranges.
14316
14317      ??? The declare_in_namespace support causes us to get two DIEs for one
14318      variable, both of which are declarations.  We want to avoid considering
14319      one to be a specification, so we must test that this DIE is not a
14320      declaration.  */
14321   else if (old_die && TREE_STATIC (decl) && ! declaration
14322            && get_AT_flag (old_die, DW_AT_declaration) == 1)
14323     {
14324       /* This is a definition of a C++ class level static.  */
14325       add_AT_specification (var_die, old_die);
14326       if (DECL_NAME (decl))
14327         {
14328           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14329           struct dwarf_file_data * file_index = lookup_filename (s.file);
14330
14331           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
14332             add_AT_file (var_die, DW_AT_decl_file, file_index);
14333
14334           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
14335             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
14336         }
14337     }
14338   else
14339     {
14340       tree type = TREE_TYPE (decl);
14341
14342       add_name_and_src_coords_attributes (var_die, decl);
14343       if ((TREE_CODE (decl) == PARM_DECL
14344            || TREE_CODE (decl) == RESULT_DECL
14345            || TREE_CODE (decl) == VAR_DECL)
14346           && DECL_BY_REFERENCE (decl))
14347         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
14348       else
14349         add_type_attribute (var_die, type, TREE_READONLY (decl),
14350                             TREE_THIS_VOLATILE (decl), context_die);
14351
14352       if (TREE_PUBLIC (decl))
14353         add_AT_flag (var_die, DW_AT_external, 1);
14354
14355       if (DECL_ARTIFICIAL (decl))
14356         add_AT_flag (var_die, DW_AT_artificial, 1);
14357
14358       if (TREE_PROTECTED (decl))
14359         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
14360       else if (TREE_PRIVATE (decl))
14361         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
14362     }
14363
14364   if (declaration)
14365     add_AT_flag (var_die, DW_AT_declaration, 1);
14366
14367   if (decl && (DECL_ABSTRACT (decl) || declaration))
14368     equate_decl_number_to_die (decl, var_die);
14369
14370   if (! declaration
14371       && (! DECL_ABSTRACT (decl_or_origin)
14372           /* Local static vars are shared between all clones/inlines,
14373              so emit DW_AT_location on the abstract DIE if DECL_RTL is
14374              already set.  */
14375           || (TREE_CODE (decl_or_origin) == VAR_DECL
14376               && TREE_STATIC (decl_or_origin)
14377               && DECL_RTL_SET_P (decl_or_origin)))
14378       /* When abstract origin already has DW_AT_location attribute, no need
14379          to add it again.  */
14380       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
14381     {
14382       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
14383           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
14384         defer_location (decl_or_origin, var_die);
14385       else
14386         add_location_or_const_value_attribute (var_die,
14387                                                decl_or_origin,
14388                                                DW_AT_location);
14389       add_pubname (decl_or_origin, var_die);
14390     }
14391   else
14392     tree_add_const_value_attribute (var_die, decl_or_origin);
14393 }
14394
14395 /* Generate a DIE to represent a named constant.  */
14396
14397 static void
14398 gen_const_die (tree decl, dw_die_ref context_die)
14399 {
14400   dw_die_ref const_die;
14401   tree type = TREE_TYPE (decl);
14402
14403   const_die = new_die (DW_TAG_constant, context_die, decl);
14404   add_name_and_src_coords_attributes (const_die, decl);
14405   add_type_attribute (const_die, type, 1, 0, context_die);
14406   if (TREE_PUBLIC (decl))
14407     add_AT_flag (const_die, DW_AT_external, 1);
14408   if (DECL_ARTIFICIAL (decl))
14409     add_AT_flag (const_die, DW_AT_artificial, 1);
14410   tree_add_const_value_attribute (const_die, decl);
14411 }
14412
14413 /* Generate a DIE to represent a label identifier.  */
14414
14415 static void
14416 gen_label_die (tree decl, dw_die_ref context_die)
14417 {
14418   tree origin = decl_ultimate_origin (decl);
14419   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
14420   rtx insn;
14421   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14422
14423   if (origin != NULL)
14424     add_abstract_origin_attribute (lbl_die, origin);
14425   else
14426     add_name_and_src_coords_attributes (lbl_die, decl);
14427
14428   if (DECL_ABSTRACT (decl))
14429     equate_decl_number_to_die (decl, lbl_die);
14430   else
14431     {
14432       insn = DECL_RTL_IF_SET (decl);
14433
14434       /* Deleted labels are programmer specified labels which have been
14435          eliminated because of various optimizations.  We still emit them
14436          here so that it is possible to put breakpoints on them.  */
14437       if (insn
14438           && (LABEL_P (insn)
14439               || ((NOTE_P (insn)
14440                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
14441         {
14442           /* When optimization is enabled (via -O) some parts of the compiler
14443              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
14444              represent source-level labels which were explicitly declared by
14445              the user.  This really shouldn't be happening though, so catch
14446              it if it ever does happen.  */
14447           gcc_assert (!INSN_DELETED_P (insn));
14448
14449           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
14450           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
14451         }
14452     }
14453 }
14454
14455 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
14456    attributes to the DIE for a block STMT, to describe where the inlined
14457    function was called from.  This is similar to add_src_coords_attributes.  */
14458
14459 static inline void
14460 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
14461 {
14462   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
14463
14464   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
14465   add_AT_unsigned (die, DW_AT_call_line, s.line);
14466 }
14467
14468
14469 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
14470    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
14471
14472 static inline void
14473 add_high_low_attributes (tree stmt, dw_die_ref die)
14474 {
14475   char label[MAX_ARTIFICIAL_LABEL_BYTES];
14476
14477   if (BLOCK_FRAGMENT_CHAIN (stmt))
14478     {
14479       tree chain;
14480
14481       if (inlined_function_outer_scope_p (stmt))
14482         {
14483           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14484                                        BLOCK_NUMBER (stmt));
14485           add_AT_lbl_id (die, DW_AT_entry_pc, label);
14486         }
14487
14488       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
14489
14490       chain = BLOCK_FRAGMENT_CHAIN (stmt);
14491       do
14492         {
14493           add_ranges (chain);
14494           chain = BLOCK_FRAGMENT_CHAIN (chain);
14495         }
14496       while (chain);
14497       add_ranges (NULL);
14498     }
14499   else
14500     {
14501       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
14502                                    BLOCK_NUMBER (stmt));
14503       add_AT_lbl_id (die, DW_AT_low_pc, label);
14504       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
14505                                    BLOCK_NUMBER (stmt));
14506       add_AT_lbl_id (die, DW_AT_high_pc, label);
14507     }
14508 }
14509
14510 /* Generate a DIE for a lexical block.  */
14511
14512 static void
14513 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
14514 {
14515   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
14516
14517   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
14518     add_high_low_attributes (stmt, stmt_die);
14519
14520   decls_for_scope (stmt, stmt_die, depth);
14521 }
14522
14523 /* Generate a DIE for an inlined subprogram.  */
14524
14525 static void
14526 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
14527 {
14528   tree decl = block_ultimate_origin (stmt);
14529
14530   /* Emit info for the abstract instance first, if we haven't yet.  We
14531      must emit this even if the block is abstract, otherwise when we
14532      emit the block below (or elsewhere), we may end up trying to emit
14533      a die whose origin die hasn't been emitted, and crashing.  */
14534   dwarf2out_abstract_function (decl);
14535
14536   if (! BLOCK_ABSTRACT (stmt))
14537     {
14538       dw_die_ref subr_die
14539         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
14540
14541       add_abstract_origin_attribute (subr_die, decl);
14542       if (TREE_ASM_WRITTEN (stmt))
14543         add_high_low_attributes (stmt, subr_die);
14544       add_call_src_coords_attributes (stmt, subr_die);
14545
14546       decls_for_scope (stmt, subr_die, depth);
14547       current_function_has_inlines = 1;
14548     }
14549   else
14550     /* We may get here if we're the outer block of function A that was
14551        inlined into function B that was inlined into function C.  When
14552        generating debugging info for C, dwarf2out_abstract_function(B)
14553        would mark all inlined blocks as abstract, including this one.
14554        So, we wouldn't (and shouldn't) expect labels to be generated
14555        for this one.  Instead, just emit debugging info for
14556        declarations within the block.  This is particularly important
14557        in the case of initializers of arguments passed from B to us:
14558        if they're statement expressions containing declarations, we
14559        wouldn't generate dies for their abstract variables, and then,
14560        when generating dies for the real variables, we'd die (pun
14561        intended :-)  */
14562     gen_lexical_block_die (stmt, context_die, depth);
14563 }
14564
14565 /* Generate a DIE for a field in a record, or structure.  */
14566
14567 static void
14568 gen_field_die (tree decl, dw_die_ref context_die)
14569 {
14570   dw_die_ref decl_die;
14571
14572   if (TREE_TYPE (decl) == error_mark_node)
14573     return;
14574
14575   decl_die = new_die (DW_TAG_member, context_die, decl);
14576   add_name_and_src_coords_attributes (decl_die, decl);
14577   add_type_attribute (decl_die, member_declared_type (decl),
14578                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
14579                       context_die);
14580
14581   if (DECL_BIT_FIELD_TYPE (decl))
14582     {
14583       add_byte_size_attribute (decl_die, decl);
14584       add_bit_size_attribute (decl_die, decl);
14585       add_bit_offset_attribute (decl_die, decl);
14586     }
14587
14588   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
14589     add_data_member_location_attribute (decl_die, decl);
14590
14591   if (DECL_ARTIFICIAL (decl))
14592     add_AT_flag (decl_die, DW_AT_artificial, 1);
14593
14594   if (TREE_PROTECTED (decl))
14595     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
14596   else if (TREE_PRIVATE (decl))
14597     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
14598
14599   /* Equate decl number to die, so that we can look up this decl later on.  */
14600   equate_decl_number_to_die (decl, decl_die);
14601 }
14602
14603 #if 0
14604 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14605    Use modified_type_die instead.
14606    We keep this code here just in case these types of DIEs may be needed to
14607    represent certain things in other languages (e.g. Pascal) someday.  */
14608
14609 static void
14610 gen_pointer_type_die (tree type, dw_die_ref context_die)
14611 {
14612   dw_die_ref ptr_die
14613     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
14614
14615   equate_type_number_to_die (type, ptr_die);
14616   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14617   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14618 }
14619
14620 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
14621    Use modified_type_die instead.
14622    We keep this code here just in case these types of DIEs may be needed to
14623    represent certain things in other languages (e.g. Pascal) someday.  */
14624
14625 static void
14626 gen_reference_type_die (tree type, dw_die_ref context_die)
14627 {
14628   dw_die_ref ref_die
14629     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
14630
14631   equate_type_number_to_die (type, ref_die);
14632   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
14633   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
14634 }
14635 #endif
14636
14637 /* Generate a DIE for a pointer to a member type.  */
14638
14639 static void
14640 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
14641 {
14642   dw_die_ref ptr_die
14643     = new_die (DW_TAG_ptr_to_member_type,
14644                scope_die_for (type, context_die), type);
14645
14646   equate_type_number_to_die (type, ptr_die);
14647   add_AT_die_ref (ptr_die, DW_AT_containing_type,
14648                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
14649   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
14650 }
14651
14652 /* Generate the DIE for the compilation unit.  */
14653
14654 static dw_die_ref
14655 gen_compile_unit_die (const char *filename)
14656 {
14657   dw_die_ref die;
14658   char producer[250];
14659   const char *language_string = lang_hooks.name;
14660   int language;
14661
14662   die = new_die (DW_TAG_compile_unit, NULL, NULL);
14663
14664   if (filename)
14665     {
14666       add_name_attribute (die, filename);
14667       /* Don't add cwd for <built-in>.  */
14668       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
14669         add_comp_dir_attribute (die);
14670     }
14671
14672   sprintf (producer, "%s %s", language_string, version_string);
14673
14674 #ifdef MIPS_DEBUGGING_INFO
14675   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
14676      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
14677      not appear in the producer string, the debugger reaches the conclusion
14678      that the object file is stripped and has no debugging information.
14679      To get the MIPS/SGI debugger to believe that there is debugging
14680      information in the object file, we add a -g to the producer string.  */
14681   if (debug_info_level > DINFO_LEVEL_TERSE)
14682     strcat (producer, " -g");
14683 #endif
14684
14685   add_AT_string (die, DW_AT_producer, producer);
14686
14687   if (strcmp (language_string, "GNU C++") == 0)
14688     language = DW_LANG_C_plus_plus;
14689   else if (strcmp (language_string, "GNU Ada") == 0)
14690     language = DW_LANG_Ada95;
14691   else if (strcmp (language_string, "GNU F77") == 0)
14692     language = DW_LANG_Fortran77;
14693   else if (strcmp (language_string, "GNU Fortran") == 0)
14694     language = DW_LANG_Fortran95;
14695   else if (strcmp (language_string, "GNU Pascal") == 0)
14696     language = DW_LANG_Pascal83;
14697   else if (strcmp (language_string, "GNU Java") == 0)
14698     language = DW_LANG_Java;
14699   else if (strcmp (language_string, "GNU Objective-C") == 0)
14700     language = DW_LANG_ObjC;
14701   else if (strcmp (language_string, "GNU Objective-C++") == 0)
14702     language = DW_LANG_ObjC_plus_plus;
14703   else
14704     language = DW_LANG_C89;
14705
14706   add_AT_unsigned (die, DW_AT_language, language);
14707   return die;
14708 }
14709
14710 /* Generate the DIE for a base class.  */
14711
14712 static void
14713 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
14714 {
14715   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
14716
14717   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
14718   add_data_member_location_attribute (die, binfo);
14719
14720   if (BINFO_VIRTUAL_P (binfo))
14721     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14722
14723   if (access == access_public_node)
14724     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14725   else if (access == access_protected_node)
14726     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14727 }
14728
14729 /* Generate a DIE for a class member.  */
14730
14731 static void
14732 gen_member_die (tree type, dw_die_ref context_die)
14733 {
14734   tree member;
14735   tree binfo = TYPE_BINFO (type);
14736   dw_die_ref child;
14737
14738   /* If this is not an incomplete type, output descriptions of each of its
14739      members. Note that as we output the DIEs necessary to represent the
14740      members of this record or union type, we will also be trying to output
14741      DIEs to represent the *types* of those members. However the `type'
14742      function (above) will specifically avoid generating type DIEs for member
14743      types *within* the list of member DIEs for this (containing) type except
14744      for those types (of members) which are explicitly marked as also being
14745      members of this (containing) type themselves.  The g++ front- end can
14746      force any given type to be treated as a member of some other (containing)
14747      type by setting the TYPE_CONTEXT of the given (member) type to point to
14748      the TREE node representing the appropriate (containing) type.  */
14749
14750   /* First output info about the base classes.  */
14751   if (binfo)
14752     {
14753       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
14754       int i;
14755       tree base;
14756
14757       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
14758         gen_inheritance_die (base,
14759                              (accesses ? VEC_index (tree, accesses, i)
14760                               : access_public_node), context_die);
14761     }
14762
14763   /* Now output info about the data members and type members.  */
14764   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
14765     {
14766       /* If we thought we were generating minimal debug info for TYPE
14767          and then changed our minds, some of the member declarations
14768          may have already been defined.  Don't define them again, but
14769          do put them in the right order.  */
14770
14771       child = lookup_decl_die (member);
14772       if (child)
14773         splice_child_die (context_die, child);
14774       else
14775         gen_decl_die (member, NULL, context_die);
14776     }
14777
14778   /* Now output info about the function members (if any).  */
14779   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
14780     {
14781       /* Don't include clones in the member list.  */
14782       if (DECL_ABSTRACT_ORIGIN (member))
14783         continue;
14784
14785       child = lookup_decl_die (member);
14786       if (child)
14787         splice_child_die (context_die, child);
14788       else
14789         gen_decl_die (member, NULL, context_die);
14790     }
14791 }
14792
14793 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
14794    is set, we pretend that the type was never defined, so we only get the
14795    member DIEs needed by later specification DIEs.  */
14796
14797 static void
14798 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
14799                                 enum debug_info_usage usage)
14800 {
14801   dw_die_ref type_die = lookup_type_die (type);
14802   dw_die_ref scope_die = 0;
14803   int nested = 0;
14804   int complete = (TYPE_SIZE (type)
14805                   && (! TYPE_STUB_DECL (type)
14806                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
14807   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
14808   complete = complete && should_emit_struct_debug (type, usage);
14809
14810   if (type_die && ! complete)
14811     return;
14812
14813   if (TYPE_CONTEXT (type) != NULL_TREE
14814       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
14815           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
14816     nested = 1;
14817
14818   scope_die = scope_die_for (type, context_die);
14819
14820   if (! type_die || (nested && scope_die == comp_unit_die))
14821     /* First occurrence of type or toplevel definition of nested class.  */
14822     {
14823       dw_die_ref old_die = type_die;
14824
14825       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
14826                           ? record_type_tag (type) : DW_TAG_union_type,
14827                           scope_die, type);
14828       equate_type_number_to_die (type, type_die);
14829       if (old_die)
14830         add_AT_specification (type_die, old_die);
14831       else
14832         add_name_attribute (type_die, type_tag (type));
14833     }
14834   else
14835     remove_AT (type_die, DW_AT_declaration);
14836
14837   /* If this type has been completed, then give it a byte_size attribute and
14838      then give a list of members.  */
14839   if (complete && !ns_decl)
14840     {
14841       /* Prevent infinite recursion in cases where the type of some member of
14842          this type is expressed in terms of this type itself.  */
14843       TREE_ASM_WRITTEN (type) = 1;
14844       add_byte_size_attribute (type_die, type);
14845       if (TYPE_STUB_DECL (type) != NULL_TREE)
14846         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14847
14848       /* If the first reference to this type was as the return type of an
14849          inline function, then it may not have a parent.  Fix this now.  */
14850       if (type_die->die_parent == NULL)
14851         add_child_die (scope_die, type_die);
14852
14853       push_decl_scope (type);
14854       gen_member_die (type, type_die);
14855       pop_decl_scope ();
14856
14857       /* GNU extension: Record what type our vtable lives in.  */
14858       if (TYPE_VFIELD (type))
14859         {
14860           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
14861
14862           gen_type_die (vtype, context_die);
14863           add_AT_die_ref (type_die, DW_AT_containing_type,
14864                           lookup_type_die (vtype));
14865         }
14866     }
14867   else
14868     {
14869       add_AT_flag (type_die, DW_AT_declaration, 1);
14870
14871       /* We don't need to do this for function-local types.  */
14872       if (TYPE_STUB_DECL (type)
14873           && ! decl_function_context (TYPE_STUB_DECL (type)))
14874         VEC_safe_push (tree, gc, incomplete_types, type);
14875     }
14876
14877   if (get_AT (type_die, DW_AT_name))
14878     add_pubtype (type, type_die);
14879 }
14880
14881 /* Generate a DIE for a subroutine _type_.  */
14882
14883 static void
14884 gen_subroutine_type_die (tree type, dw_die_ref context_die)
14885 {
14886   tree return_type = TREE_TYPE (type);
14887   dw_die_ref subr_die
14888     = new_die (DW_TAG_subroutine_type,
14889                scope_die_for (type, context_die), type);
14890
14891   equate_type_number_to_die (type, subr_die);
14892   add_prototyped_attribute (subr_die, type);
14893   add_type_attribute (subr_die, return_type, 0, 0, context_die);
14894   gen_formal_types_die (type, subr_die);
14895
14896   if (get_AT (subr_die, DW_AT_name))
14897     add_pubtype (type, subr_die);
14898 }
14899
14900 /* Generate a DIE for a type definition.  */
14901
14902 static void
14903 gen_typedef_die (tree decl, dw_die_ref context_die)
14904 {
14905   dw_die_ref type_die;
14906   tree origin;
14907
14908   if (TREE_ASM_WRITTEN (decl))
14909     return;
14910
14911   TREE_ASM_WRITTEN (decl) = 1;
14912   type_die = new_die (DW_TAG_typedef, context_die, decl);
14913   origin = decl_ultimate_origin (decl);
14914   if (origin != NULL)
14915     add_abstract_origin_attribute (type_die, origin);
14916   else
14917     {
14918       tree type;
14919
14920       add_name_and_src_coords_attributes (type_die, decl);
14921       if (DECL_ORIGINAL_TYPE (decl))
14922         {
14923           type = DECL_ORIGINAL_TYPE (decl);
14924
14925           gcc_assert (type != TREE_TYPE (decl));
14926           equate_type_number_to_die (TREE_TYPE (decl), type_die);
14927         }
14928       else
14929         type = TREE_TYPE (decl);
14930
14931       add_type_attribute (type_die, type, TREE_READONLY (decl),
14932                           TREE_THIS_VOLATILE (decl), context_die);
14933     }
14934
14935   if (DECL_ABSTRACT (decl))
14936     equate_decl_number_to_die (decl, type_die);
14937
14938   if (get_AT (type_die, DW_AT_name))
14939     add_pubtype (decl, type_die);
14940 }
14941
14942 /* Generate a type description DIE.  */
14943
14944 static void
14945 gen_type_die_with_usage (tree type, dw_die_ref context_die,
14946                                 enum debug_info_usage usage)
14947 {
14948   int need_pop;
14949   struct array_descr_info info;
14950
14951   if (type == NULL_TREE || type == error_mark_node)
14952     return;
14953
14954   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14955       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
14956     {
14957       if (TREE_ASM_WRITTEN (type))
14958         return;
14959
14960       /* Prevent broken recursion; we can't hand off to the same type.  */
14961       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
14962
14963       /* Use the DIE of the containing namespace as the parent DIE of
14964          the type description DIE we want to generate.  */
14965       if (DECL_CONTEXT (TYPE_NAME (type))
14966           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
14967         context_die = lookup_decl_die (DECL_CONTEXT (TYPE_NAME (type)));
14968
14969       TREE_ASM_WRITTEN (type) = 1;
14970       gen_decl_die (TYPE_NAME (type), NULL, context_die);
14971       return;
14972     }
14973
14974   /* If this is an array type with hidden descriptor, handle it first.  */
14975   if (!TREE_ASM_WRITTEN (type)
14976       && lang_hooks.types.get_array_descr_info
14977       && lang_hooks.types.get_array_descr_info (type, &info))
14978     {
14979       gen_descr_array_type_die (type, &info, context_die);
14980       TREE_ASM_WRITTEN (type) = 1;
14981       return;
14982     }
14983
14984   /* We are going to output a DIE to represent the unqualified version
14985      of this type (i.e. without any const or volatile qualifiers) so
14986      get the main variant (i.e. the unqualified version) of this type
14987      now.  (Vectors are special because the debugging info is in the
14988      cloned type itself).  */
14989   if (TREE_CODE (type) != VECTOR_TYPE)
14990     type = type_main_variant (type);
14991
14992   if (TREE_ASM_WRITTEN (type))
14993     return;
14994
14995   switch (TREE_CODE (type))
14996     {
14997     case ERROR_MARK:
14998       break;
14999
15000     case POINTER_TYPE:
15001     case REFERENCE_TYPE:
15002       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
15003          ensures that the gen_type_die recursion will terminate even if the
15004          type is recursive.  Recursive types are possible in Ada.  */
15005       /* ??? We could perhaps do this for all types before the switch
15006          statement.  */
15007       TREE_ASM_WRITTEN (type) = 1;
15008
15009       /* For these types, all that is required is that we output a DIE (or a
15010          set of DIEs) to represent the "basis" type.  */
15011       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15012                                 DINFO_USAGE_IND_USE);
15013       break;
15014
15015     case OFFSET_TYPE:
15016       /* This code is used for C++ pointer-to-data-member types.
15017          Output a description of the relevant class type.  */
15018       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
15019                                         DINFO_USAGE_IND_USE);
15020
15021       /* Output a description of the type of the object pointed to.  */
15022       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15023                                         DINFO_USAGE_IND_USE);
15024
15025       /* Now output a DIE to represent this pointer-to-data-member type
15026          itself.  */
15027       gen_ptr_to_mbr_type_die (type, context_die);
15028       break;
15029
15030     case FUNCTION_TYPE:
15031       /* Force out return type (in case it wasn't forced out already).  */
15032       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15033                                         DINFO_USAGE_DIR_USE);
15034       gen_subroutine_type_die (type, context_die);
15035       break;
15036
15037     case METHOD_TYPE:
15038       /* Force out return type (in case it wasn't forced out already).  */
15039       gen_type_die_with_usage (TREE_TYPE (type), context_die,
15040                                         DINFO_USAGE_DIR_USE);
15041       gen_subroutine_type_die (type, context_die);
15042       break;
15043
15044     case ARRAY_TYPE:
15045       gen_array_type_die (type, context_die);
15046       break;
15047
15048     case VECTOR_TYPE:
15049       gen_array_type_die (type, context_die);
15050       break;
15051
15052     case ENUMERAL_TYPE:
15053     case RECORD_TYPE:
15054     case UNION_TYPE:
15055     case QUAL_UNION_TYPE:
15056       /* If this is a nested type whose containing class hasn't been written
15057          out yet, writing it out will cover this one, too.  This does not apply
15058          to instantiations of member class templates; they need to be added to
15059          the containing class as they are generated.  FIXME: This hurts the
15060          idea of combining type decls from multiple TUs, since we can't predict
15061          what set of template instantiations we'll get.  */
15062       if (TYPE_CONTEXT (type)
15063           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
15064           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
15065         {
15066           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
15067
15068           if (TREE_ASM_WRITTEN (type))
15069             return;
15070
15071           /* If that failed, attach ourselves to the stub.  */
15072           push_decl_scope (TYPE_CONTEXT (type));
15073           context_die = lookup_type_die (TYPE_CONTEXT (type));
15074           need_pop = 1;
15075         }
15076       else
15077         {
15078           context_die = declare_in_namespace (type, context_die);
15079           need_pop = 0;
15080         }
15081
15082       if (TREE_CODE (type) == ENUMERAL_TYPE)
15083         {
15084           /* This might have been written out by the call to
15085              declare_in_namespace.  */
15086           if (!TREE_ASM_WRITTEN (type))
15087             gen_enumeration_type_die (type, context_die);
15088         }
15089       else
15090         gen_struct_or_union_type_die (type, context_die, usage);
15091
15092       if (need_pop)
15093         pop_decl_scope ();
15094
15095       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
15096          it up if it is ever completed.  gen_*_type_die will set it for us
15097          when appropriate.  */
15098       return;
15099
15100     case VOID_TYPE:
15101     case INTEGER_TYPE:
15102     case REAL_TYPE:
15103     case FIXED_POINT_TYPE:
15104     case COMPLEX_TYPE:
15105     case BOOLEAN_TYPE:
15106       /* No DIEs needed for fundamental types.  */
15107       break;
15108
15109     case LANG_TYPE:
15110       /* No Dwarf representation currently defined.  */
15111       break;
15112
15113     default:
15114       gcc_unreachable ();
15115     }
15116
15117   TREE_ASM_WRITTEN (type) = 1;
15118 }
15119
15120 static void
15121 gen_type_die (tree type, dw_die_ref context_die)
15122 {
15123   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
15124 }
15125
15126 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
15127    things which are local to the given block.  */
15128
15129 static void
15130 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
15131 {
15132   int must_output_die = 0;
15133   bool inlined_func;
15134
15135   /* Ignore blocks that are NULL.  */
15136   if (stmt == NULL_TREE)
15137     return;
15138
15139   inlined_func = inlined_function_outer_scope_p (stmt);
15140
15141   /* If the block is one fragment of a non-contiguous block, do not
15142      process the variables, since they will have been done by the
15143      origin block.  Do process subblocks.  */
15144   if (BLOCK_FRAGMENT_ORIGIN (stmt))
15145     {
15146       tree sub;
15147
15148       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
15149         gen_block_die (sub, context_die, depth + 1);
15150
15151       return;
15152     }
15153
15154   /* Determine if we need to output any Dwarf DIEs at all to represent this
15155      block.  */
15156   if (inlined_func)
15157     /* The outer scopes for inlinings *must* always be represented.  We
15158        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
15159     must_output_die = 1;
15160   else
15161     {
15162       /* Determine if this block directly contains any "significant"
15163          local declarations which we will need to output DIEs for.  */
15164       if (debug_info_level > DINFO_LEVEL_TERSE)
15165         /* We are not in terse mode so *any* local declaration counts
15166            as being a "significant" one.  */
15167         must_output_die = ((BLOCK_VARS (stmt) != NULL
15168                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
15169                            && (TREE_USED (stmt)
15170                                || TREE_ASM_WRITTEN (stmt)
15171                                || BLOCK_ABSTRACT (stmt)));
15172       else if ((TREE_USED (stmt)
15173                 || TREE_ASM_WRITTEN (stmt)
15174                 || BLOCK_ABSTRACT (stmt))
15175                && !dwarf2out_ignore_block (stmt))
15176         must_output_die = 1;
15177     }
15178
15179   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
15180      DIE for any block which contains no significant local declarations at
15181      all.  Rather, in such cases we just call `decls_for_scope' so that any
15182      needed Dwarf info for any sub-blocks will get properly generated. Note
15183      that in terse mode, our definition of what constitutes a "significant"
15184      local declaration gets restricted to include only inlined function
15185      instances and local (nested) function definitions.  */
15186   if (must_output_die)
15187     {
15188       if (inlined_func)
15189         gen_inlined_subroutine_die (stmt, context_die, depth);
15190       else
15191         gen_lexical_block_die (stmt, context_die, depth);
15192     }
15193   else
15194     decls_for_scope (stmt, context_die, depth);
15195 }
15196
15197 /* Process variable DECL (or variable with origin ORIGIN) within
15198    block STMT and add it to CONTEXT_DIE.  */
15199 static void
15200 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
15201 {
15202   dw_die_ref die;
15203   tree decl_or_origin = decl ? decl : origin;
15204   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
15205
15206   if (ultimate_origin)
15207     origin = ultimate_origin;
15208
15209   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
15210     die = lookup_decl_die (decl_or_origin);
15211   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
15212            && TYPE_DECL_IS_STUB (decl_or_origin))
15213     die = lookup_type_die (TREE_TYPE (decl_or_origin));
15214   else
15215     die = NULL;
15216
15217   if (die != NULL && die->die_parent == NULL)
15218     add_child_die (context_die, die);
15219   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
15220     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
15221                                          stmt, context_die);
15222   else
15223     gen_decl_die (decl, origin, context_die);
15224 }
15225
15226 /* Generate all of the decls declared within a given scope and (recursively)
15227    all of its sub-blocks.  */
15228
15229 static void
15230 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
15231 {
15232   tree decl;
15233   unsigned int i;
15234   tree subblocks;
15235
15236   /* Ignore NULL blocks.  */
15237   if (stmt == NULL_TREE)
15238     return;
15239
15240   /* Output the DIEs to represent all of the data objects and typedefs
15241      declared directly within this block but not within any nested
15242      sub-blocks.  Also, nested function and tag DIEs have been
15243      generated with a parent of NULL; fix that up now.  */
15244   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
15245     process_scope_var (stmt, decl, NULL_TREE, context_die);
15246   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
15247     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
15248                        context_die);
15249
15250   /* If we're at -g1, we're not interested in subblocks.  */
15251   if (debug_info_level <= DINFO_LEVEL_TERSE)
15252     return;
15253
15254   /* Output the DIEs to represent all sub-blocks (and the items declared
15255      therein) of this block.  */
15256   for (subblocks = BLOCK_SUBBLOCKS (stmt);
15257        subblocks != NULL;
15258        subblocks = BLOCK_CHAIN (subblocks))
15259     gen_block_die (subblocks, context_die, depth + 1);
15260 }
15261
15262 /* Is this a typedef we can avoid emitting?  */
15263
15264 static inline int
15265 is_redundant_typedef (const_tree decl)
15266 {
15267   if (TYPE_DECL_IS_STUB (decl))
15268     return 1;
15269
15270   if (DECL_ARTIFICIAL (decl)
15271       && DECL_CONTEXT (decl)
15272       && is_tagged_type (DECL_CONTEXT (decl))
15273       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
15274       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
15275     /* Also ignore the artificial member typedef for the class name.  */
15276     return 1;
15277
15278   return 0;
15279 }
15280
15281 /* Returns the DIE for a context.  */
15282
15283 static inline dw_die_ref
15284 get_context_die (tree context)
15285 {
15286   if (context)
15287     {
15288       /* Find die that represents this context.  */
15289       if (TYPE_P (context))
15290         return force_type_die (context);
15291       else
15292         return force_decl_die (context);
15293     }
15294   return comp_unit_die;
15295 }
15296
15297 /* Returns the DIE for decl.  A DIE will always be returned.  */
15298
15299 static dw_die_ref
15300 force_decl_die (tree decl)
15301 {
15302   dw_die_ref decl_die;
15303   unsigned saved_external_flag;
15304   tree save_fn = NULL_TREE;
15305   decl_die = lookup_decl_die (decl);
15306   if (!decl_die)
15307     {
15308       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
15309
15310       decl_die = lookup_decl_die (decl);
15311       if (decl_die)
15312         return decl_die;
15313
15314       switch (TREE_CODE (decl))
15315         {
15316         case FUNCTION_DECL:
15317           /* Clear current_function_decl, so that gen_subprogram_die thinks
15318              that this is a declaration. At this point, we just want to force
15319              declaration die.  */
15320           save_fn = current_function_decl;
15321           current_function_decl = NULL_TREE;
15322           gen_subprogram_die (decl, context_die);
15323           current_function_decl = save_fn;
15324           break;
15325
15326         case VAR_DECL:
15327           /* Set external flag to force declaration die. Restore it after
15328            gen_decl_die() call.  */
15329           saved_external_flag = DECL_EXTERNAL (decl);
15330           DECL_EXTERNAL (decl) = 1;
15331           gen_decl_die (decl, NULL, context_die);
15332           DECL_EXTERNAL (decl) = saved_external_flag;
15333           break;
15334
15335         case NAMESPACE_DECL:
15336           dwarf2out_decl (decl);
15337           break;
15338
15339         default:
15340           gcc_unreachable ();
15341         }
15342
15343       /* We should be able to find the DIE now.  */
15344       if (!decl_die)
15345         decl_die = lookup_decl_die (decl);
15346       gcc_assert (decl_die);
15347     }
15348
15349   return decl_die;
15350 }
15351
15352 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
15353    always returned.  */
15354
15355 static dw_die_ref
15356 force_type_die (tree type)
15357 {
15358   dw_die_ref type_die;
15359
15360   type_die = lookup_type_die (type);
15361   if (!type_die)
15362     {
15363       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
15364
15365       type_die = modified_type_die (type, TYPE_READONLY (type),
15366                                     TYPE_VOLATILE (type), context_die);
15367       gcc_assert (type_die);
15368     }
15369   return type_die;
15370 }
15371
15372 /* Force out any required namespaces to be able to output DECL,
15373    and return the new context_die for it, if it's changed.  */
15374
15375 static dw_die_ref
15376 setup_namespace_context (tree thing, dw_die_ref context_die)
15377 {
15378   tree context = (DECL_P (thing)
15379                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
15380   if (context && TREE_CODE (context) == NAMESPACE_DECL)
15381     /* Force out the namespace.  */
15382     context_die = force_decl_die (context);
15383
15384   return context_die;
15385 }
15386
15387 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
15388    type) within its namespace, if appropriate.
15389
15390    For compatibility with older debuggers, namespace DIEs only contain
15391    declarations; all definitions are emitted at CU scope.  */
15392
15393 static dw_die_ref
15394 declare_in_namespace (tree thing, dw_die_ref context_die)
15395 {
15396   dw_die_ref ns_context;
15397
15398   if (debug_info_level <= DINFO_LEVEL_TERSE)
15399     return context_die;
15400
15401   /* If this decl is from an inlined function, then don't try to emit it in its
15402      namespace, as we will get confused.  It would have already been emitted
15403      when the abstract instance of the inline function was emitted anyways.  */
15404   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
15405     return context_die;
15406
15407   ns_context = setup_namespace_context (thing, context_die);
15408
15409   if (ns_context != context_die)
15410     {
15411       if (is_fortran ())
15412         return ns_context;
15413       if (DECL_P (thing))
15414         gen_decl_die (thing, NULL, ns_context);
15415       else
15416         gen_type_die (thing, ns_context);
15417     }
15418   return context_die;
15419 }
15420
15421 /* Generate a DIE for a namespace or namespace alias.  */
15422
15423 static void
15424 gen_namespace_die (tree decl, dw_die_ref context_die)
15425 {
15426   dw_die_ref namespace_die;
15427
15428   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
15429      they are an alias of.  */
15430   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
15431     {
15432       /* Output a real namespace or module.  */
15433       context_die = setup_namespace_context (decl, comp_unit_die);
15434       namespace_die = new_die (is_fortran ()
15435                                ? DW_TAG_module : DW_TAG_namespace,
15436                                context_die, decl);
15437       /* For Fortran modules defined in different CU don't add src coords.  */
15438       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
15439         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
15440       else
15441         add_name_and_src_coords_attributes (namespace_die, decl);
15442       if (DECL_EXTERNAL (decl))
15443         add_AT_flag (namespace_die, DW_AT_declaration, 1);
15444       equate_decl_number_to_die (decl, namespace_die);
15445     }
15446   else
15447     {
15448       /* Output a namespace alias.  */
15449
15450       /* Force out the namespace we are an alias of, if necessary.  */
15451       dw_die_ref origin_die
15452         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
15453
15454       if (DECL_CONTEXT (decl) == NULL_TREE
15455           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
15456         context_die = setup_namespace_context (decl, comp_unit_die);
15457       /* Now create the namespace alias DIE.  */
15458       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
15459       add_name_and_src_coords_attributes (namespace_die, decl);
15460       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
15461       equate_decl_number_to_die (decl, namespace_die);
15462     }
15463 }
15464
15465 /* Generate Dwarf debug information for a decl described by DECL.  */
15466
15467 static void
15468 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
15469 {
15470   tree decl_or_origin = decl ? decl : origin;
15471   tree class_origin = NULL;
15472
15473   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
15474     return;
15475
15476   switch (TREE_CODE (decl_or_origin))
15477     {
15478     case ERROR_MARK:
15479       break;
15480
15481     case CONST_DECL:
15482       if (!is_fortran ())
15483         {
15484           /* The individual enumerators of an enum type get output when we output
15485              the Dwarf representation of the relevant enum type itself.  */
15486           break;
15487         }
15488
15489       /* Emit its type.  */
15490       gen_type_die (TREE_TYPE (decl), context_die);
15491
15492       /* And its containing namespace.  */
15493       context_die = declare_in_namespace (decl, context_die);
15494
15495       gen_const_die (decl, context_die);
15496       break;
15497
15498     case FUNCTION_DECL:
15499       /* Don't output any DIEs to represent mere function declarations,
15500          unless they are class members or explicit block externs.  */
15501       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
15502           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
15503           && (current_function_decl == NULL_TREE
15504               || DECL_ARTIFICIAL (decl_or_origin)))
15505         break;
15506
15507 #if 0
15508       /* FIXME */
15509       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
15510          on local redeclarations of global functions.  That seems broken.  */
15511       if (current_function_decl != decl)
15512         /* This is only a declaration.  */;
15513 #endif
15514
15515       /* If we're emitting a clone, emit info for the abstract instance.  */
15516       if (origin || DECL_ORIGIN (decl) != decl)
15517         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
15518
15519       /* If we're emitting an out-of-line copy of an inline function,
15520          emit info for the abstract instance and set up to refer to it.  */
15521       else if (cgraph_function_possibly_inlined_p (decl)
15522                && ! DECL_ABSTRACT (decl)
15523                && ! class_or_namespace_scope_p (context_die)
15524                /* dwarf2out_abstract_function won't emit a die if this is just
15525                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
15526                   that case, because that works only if we have a die.  */
15527                && DECL_INITIAL (decl) != NULL_TREE)
15528         {
15529           dwarf2out_abstract_function (decl);
15530           set_decl_origin_self (decl);
15531         }
15532
15533       /* Otherwise we're emitting the primary DIE for this decl.  */
15534       else if (debug_info_level > DINFO_LEVEL_TERSE)
15535         {
15536           /* Before we describe the FUNCTION_DECL itself, make sure that we
15537              have described its return type.  */
15538           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
15539
15540           /* And its virtual context.  */
15541           if (DECL_VINDEX (decl) != NULL_TREE)
15542             gen_type_die (DECL_CONTEXT (decl), context_die);
15543
15544           /* And its containing type.  */
15545           if (!origin)
15546             origin = decl_class_context (decl);
15547           if (origin != NULL_TREE)
15548             gen_type_die_for_member (origin, decl, context_die);
15549
15550           /* And its containing namespace.  */
15551           context_die = declare_in_namespace (decl, context_die);
15552         }
15553
15554       /* Now output a DIE to represent the function itself.  */
15555       if (decl)
15556         gen_subprogram_die (decl, context_die);
15557       break;
15558
15559     case TYPE_DECL:
15560       /* If we are in terse mode, don't generate any DIEs to represent any
15561          actual typedefs.  */
15562       if (debug_info_level <= DINFO_LEVEL_TERSE)
15563         break;
15564
15565       /* In the special case of a TYPE_DECL node representing the declaration
15566          of some type tag, if the given TYPE_DECL is marked as having been
15567          instantiated from some other (original) TYPE_DECL node (e.g. one which
15568          was generated within the original definition of an inline function) we
15569          used to generate a special (abbreviated) DW_TAG_structure_type,
15570          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
15571          should be actually referencing those DIEs, as variable DIEs with that
15572          type would be emitted already in the abstract origin, so it was always
15573          removed during unused type prunning.  Don't add anything in this
15574          case.  */
15575       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
15576         break;
15577
15578       if (is_redundant_typedef (decl))
15579         gen_type_die (TREE_TYPE (decl), context_die);
15580       else
15581         /* Output a DIE to represent the typedef itself.  */
15582         gen_typedef_die (decl, context_die);
15583       break;
15584
15585     case LABEL_DECL:
15586       if (debug_info_level >= DINFO_LEVEL_NORMAL)
15587         gen_label_die (decl, context_die);
15588       break;
15589
15590     case VAR_DECL:
15591     case RESULT_DECL:
15592       /* If we are in terse mode, don't generate any DIEs to represent any
15593          variable declarations or definitions.  */
15594       if (debug_info_level <= DINFO_LEVEL_TERSE)
15595         break;
15596
15597       /* Output any DIEs that are needed to specify the type of this data
15598          object.  */
15599       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
15600            || TREE_CODE (decl_or_origin) == VAR_DECL)
15601           && DECL_BY_REFERENCE (decl_or_origin))
15602         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15603       else
15604         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15605
15606       /* And its containing type.  */
15607       class_origin = decl_class_context (decl_or_origin);
15608       if (class_origin != NULL_TREE)
15609         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
15610
15611       /* And its containing namespace.  */
15612       context_die = declare_in_namespace (decl_or_origin, context_die);
15613
15614       /* Now output the DIE to represent the data object itself.  This gets
15615          complicated because of the possibility that the VAR_DECL really
15616          represents an inlined instance of a formal parameter for an inline
15617          function.  */
15618       if (!origin)
15619         origin = decl_ultimate_origin (decl);
15620       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
15621         gen_formal_parameter_die (decl, origin, context_die);
15622       else
15623         gen_variable_die (decl, origin, context_die);
15624       break;
15625
15626     case FIELD_DECL:
15627       /* Ignore the nameless fields that are used to skip bits but handle C++
15628          anonymous unions and structs.  */
15629       if (DECL_NAME (decl) != NULL_TREE
15630           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
15631           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
15632         {
15633           gen_type_die (member_declared_type (decl), context_die);
15634           gen_field_die (decl, context_die);
15635         }
15636       break;
15637
15638     case PARM_DECL:
15639       if (DECL_BY_REFERENCE (decl_or_origin))
15640         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
15641       else
15642         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
15643       gen_formal_parameter_die (decl, origin, context_die);
15644       break;
15645
15646     case NAMESPACE_DECL:
15647     case IMPORTED_DECL:
15648       gen_namespace_die (decl, context_die);
15649       break;
15650
15651     default:
15652       /* Probably some frontend-internal decl.  Assume we don't care.  */
15653       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
15654       break;
15655     }
15656 }
15657 \f
15658 /* Output debug information for global decl DECL.  Called from toplev.c after
15659    compilation proper has finished.  */
15660
15661 static void
15662 dwarf2out_global_decl (tree decl)
15663 {
15664   /* Output DWARF2 information for file-scope tentative data object
15665      declarations, file-scope (extern) function declarations (which
15666      had no corresponding body) and file-scope tagged type declarations
15667      and definitions which have not yet been forced out.  */
15668   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
15669     dwarf2out_decl (decl);
15670 }
15671
15672 /* Output debug information for type decl DECL.  Called from toplev.c
15673    and from language front ends (to record built-in types).  */
15674 static void
15675 dwarf2out_type_decl (tree decl, int local)
15676 {
15677   if (!local)
15678     dwarf2out_decl (decl);
15679 }
15680
15681 /* Output debug information for imported module or decl DECL.
15682    NAME is non-NULL name in the lexical block if the decl has been renamed.
15683    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
15684    that DECL belongs to.
15685    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
15686 static void
15687 dwarf2out_imported_module_or_decl_1 (tree decl,
15688                                      tree name,
15689                                      tree lexical_block,
15690                                      dw_die_ref lexical_block_die)
15691 {
15692   expanded_location xloc;
15693   dw_die_ref imported_die = NULL;
15694   dw_die_ref at_import_die;
15695
15696   if (TREE_CODE (decl) == IMPORTED_DECL)
15697     {
15698       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
15699       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
15700       gcc_assert (decl);
15701     }
15702   else
15703     xloc = expand_location (input_location);
15704
15705   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
15706     {
15707       if (is_base_type (TREE_TYPE (decl)))
15708         at_import_die = base_type_die (TREE_TYPE (decl));
15709       else
15710         at_import_die = force_type_die (TREE_TYPE (decl));
15711       /* For namespace N { typedef void T; } using N::T; base_type_die
15712          returns NULL, but DW_TAG_imported_declaration requires
15713          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
15714       if (!at_import_die)
15715         {
15716           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
15717           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
15718           at_import_die = lookup_type_die (TREE_TYPE (decl));
15719           gcc_assert (at_import_die);
15720         }
15721     }
15722   else
15723     {
15724       at_import_die = lookup_decl_die (decl);
15725       if (!at_import_die)
15726         {
15727           /* If we're trying to avoid duplicate debug info, we may not have
15728              emitted the member decl for this field.  Emit it now.  */
15729           if (TREE_CODE (decl) == FIELD_DECL)
15730             {
15731               tree type = DECL_CONTEXT (decl);
15732
15733               if (TYPE_CONTEXT (type)
15734                   && TYPE_P (TYPE_CONTEXT (type))
15735                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
15736                                                 DINFO_USAGE_DIR_USE))
15737                 return;
15738               gen_type_die_for_member (type, decl,
15739                                        get_context_die (TYPE_CONTEXT (type)));
15740             }
15741           at_import_die = force_decl_die (decl);
15742         }
15743     }
15744
15745   if (TREE_CODE (decl) == NAMESPACE_DECL)
15746     imported_die = new_die (DW_TAG_imported_module,
15747                             lexical_block_die,
15748                             lexical_block);
15749   else
15750     imported_die = new_die (DW_TAG_imported_declaration,
15751                             lexical_block_die,
15752                             lexical_block);
15753
15754   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
15755   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
15756   if (name)
15757     add_AT_string (imported_die, DW_AT_name,
15758                    IDENTIFIER_POINTER (name));
15759   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
15760 }
15761
15762 /* Output debug information for imported module or decl DECL.
15763    NAME is non-NULL name in context if the decl has been renamed.
15764    CHILD is true if decl is one of the renamed decls as part of
15765    importing whole module.  */
15766
15767 static void
15768 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
15769                                    bool child)
15770 {
15771   /* dw_die_ref at_import_die;  */
15772   dw_die_ref scope_die;
15773
15774   if (debug_info_level <= DINFO_LEVEL_TERSE)
15775     return;
15776
15777   gcc_assert (decl);
15778
15779   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
15780      We need decl DIE for reference and scope die. First, get DIE for the decl
15781      itself.  */
15782
15783   /* Get the scope die for decl context. Use comp_unit_die for global module
15784      or decl. If die is not found for non globals, force new die.  */
15785   if (context
15786       && TYPE_P (context)
15787       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
15788     return;
15789   scope_die = get_context_die (context);
15790
15791   if (child)
15792     {
15793       gcc_assert (scope_die->die_child);
15794       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
15795       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
15796       scope_die = scope_die->die_child;
15797     }
15798
15799   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
15800   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
15801
15802 }
15803
15804 /* Write the debugging output for DECL.  */
15805
15806 void
15807 dwarf2out_decl (tree decl)
15808 {
15809   dw_die_ref context_die = comp_unit_die;
15810
15811   switch (TREE_CODE (decl))
15812     {
15813     case ERROR_MARK:
15814       return;
15815
15816     case FUNCTION_DECL:
15817       /* What we would really like to do here is to filter out all mere
15818          file-scope declarations of file-scope functions which are never
15819          referenced later within this translation unit (and keep all of ones
15820          that *are* referenced later on) but we aren't clairvoyant, so we have
15821          no idea which functions will be referenced in the future (i.e. later
15822          on within the current translation unit). So here we just ignore all
15823          file-scope function declarations which are not also definitions.  If
15824          and when the debugger needs to know something about these functions,
15825          it will have to hunt around and find the DWARF information associated
15826          with the definition of the function.
15827
15828          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
15829          nodes represent definitions and which ones represent mere
15830          declarations.  We have to check DECL_INITIAL instead. That's because
15831          the C front-end supports some weird semantics for "extern inline"
15832          function definitions.  These can get inlined within the current
15833          translation unit (and thus, we need to generate Dwarf info for their
15834          abstract instances so that the Dwarf info for the concrete inlined
15835          instances can have something to refer to) but the compiler never
15836          generates any out-of-lines instances of such things (despite the fact
15837          that they *are* definitions).
15838
15839          The important point is that the C front-end marks these "extern
15840          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
15841          them anyway. Note that the C++ front-end also plays some similar games
15842          for inline function definitions appearing within include files which
15843          also contain `#pragma interface' pragmas.  */
15844       if (DECL_INITIAL (decl) == NULL_TREE)
15845         return;
15846
15847       /* If we're a nested function, initially use a parent of NULL; if we're
15848          a plain function, this will be fixed up in decls_for_scope.  If
15849          we're a method, it will be ignored, since we already have a DIE.  */
15850       if (decl_function_context (decl)
15851           /* But if we're in terse mode, we don't care about scope.  */
15852           && debug_info_level > DINFO_LEVEL_TERSE)
15853         context_die = NULL;
15854       break;
15855
15856     case VAR_DECL:
15857       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
15858          declaration and if the declaration was never even referenced from
15859          within this entire compilation unit.  We suppress these DIEs in
15860          order to save space in the .debug section (by eliminating entries
15861          which are probably useless).  Note that we must not suppress
15862          block-local extern declarations (whether used or not) because that
15863          would screw-up the debugger's name lookup mechanism and cause it to
15864          miss things which really ought to be in scope at a given point.  */
15865       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
15866         return;
15867
15868       /* For local statics lookup proper context die.  */
15869       if (TREE_STATIC (decl) && decl_function_context (decl))
15870         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15871
15872       /* If we are in terse mode, don't generate any DIEs to represent any
15873          variable declarations or definitions.  */
15874       if (debug_info_level <= DINFO_LEVEL_TERSE)
15875         return;
15876       break;
15877
15878     case CONST_DECL:
15879       if (debug_info_level <= DINFO_LEVEL_TERSE)
15880         return;
15881       if (!is_fortran ())
15882         return;
15883       if (TREE_STATIC (decl) && decl_function_context (decl))
15884         context_die = lookup_decl_die (DECL_CONTEXT (decl));
15885       break;
15886
15887     case NAMESPACE_DECL:
15888     case IMPORTED_DECL:
15889       if (debug_info_level <= DINFO_LEVEL_TERSE)
15890         return;
15891       if (lookup_decl_die (decl) != NULL)
15892         return;
15893       break;
15894
15895     case TYPE_DECL:
15896       /* Don't emit stubs for types unless they are needed by other DIEs.  */
15897       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
15898         return;
15899
15900       /* Don't bother trying to generate any DIEs to represent any of the
15901          normal built-in types for the language we are compiling.  */
15902       if (DECL_IS_BUILTIN (decl))
15903         {
15904           /* OK, we need to generate one for `bool' so GDB knows what type
15905              comparisons have.  */
15906           if (is_cxx ()
15907               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
15908               && ! DECL_IGNORED_P (decl))
15909             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
15910
15911           return;
15912         }
15913
15914       /* If we are in terse mode, don't generate any DIEs for types.  */
15915       if (debug_info_level <= DINFO_LEVEL_TERSE)
15916         return;
15917
15918       /* If we're a function-scope tag, initially use a parent of NULL;
15919          this will be fixed up in decls_for_scope.  */
15920       if (decl_function_context (decl))
15921         context_die = NULL;
15922
15923       break;
15924
15925     default:
15926       return;
15927     }
15928
15929   gen_decl_die (decl, NULL, context_die);
15930 }
15931
15932 /* Output a marker (i.e. a label) for the beginning of the generated code for
15933    a lexical block.  */
15934
15935 static void
15936 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
15937                        unsigned int blocknum)
15938 {
15939   switch_to_section (current_function_section ());
15940   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
15941 }
15942
15943 /* Output a marker (i.e. a label) for the end of the generated code for a
15944    lexical block.  */
15945
15946 static void
15947 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
15948 {
15949   switch_to_section (current_function_section ());
15950   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
15951 }
15952
15953 /* Returns nonzero if it is appropriate not to emit any debugging
15954    information for BLOCK, because it doesn't contain any instructions.
15955
15956    Don't allow this for blocks with nested functions or local classes
15957    as we would end up with orphans, and in the presence of scheduling
15958    we may end up calling them anyway.  */
15959
15960 static bool
15961 dwarf2out_ignore_block (const_tree block)
15962 {
15963   tree decl;
15964   unsigned int i;
15965
15966   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
15967     if (TREE_CODE (decl) == FUNCTION_DECL
15968         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15969       return 0;
15970   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
15971     {
15972       decl = BLOCK_NONLOCALIZED_VAR (block, i);
15973       if (TREE_CODE (decl) == FUNCTION_DECL
15974           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
15975       return 0;
15976     }
15977
15978   return 1;
15979 }
15980
15981 /* Hash table routines for file_hash.  */
15982
15983 static int
15984 file_table_eq (const void *p1_p, const void *p2_p)
15985 {
15986   const struct dwarf_file_data *const p1 =
15987     (const struct dwarf_file_data *) p1_p;
15988   const char *const p2 = (const char *) p2_p;
15989   return strcmp (p1->filename, p2) == 0;
15990 }
15991
15992 static hashval_t
15993 file_table_hash (const void *p_p)
15994 {
15995   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
15996   return htab_hash_string (p->filename);
15997 }
15998
15999 /* Lookup FILE_NAME (in the list of filenames that we know about here in
16000    dwarf2out.c) and return its "index".  The index of each (known) filename is
16001    just a unique number which is associated with only that one filename.  We
16002    need such numbers for the sake of generating labels (in the .debug_sfnames
16003    section) and references to those files numbers (in the .debug_srcinfo
16004    and.debug_macinfo sections).  If the filename given as an argument is not
16005    found in our current list, add it to the list and assign it the next
16006    available unique index number.  In order to speed up searches, we remember
16007    the index of the filename was looked up last.  This handles the majority of
16008    all searches.  */
16009
16010 static struct dwarf_file_data *
16011 lookup_filename (const char *file_name)
16012 {
16013   void ** slot;
16014   struct dwarf_file_data * created;
16015
16016   /* Check to see if the file name that was searched on the previous
16017      call matches this file name.  If so, return the index.  */
16018   if (file_table_last_lookup
16019       && (file_name == file_table_last_lookup->filename
16020           || strcmp (file_table_last_lookup->filename, file_name) == 0))
16021     return file_table_last_lookup;
16022
16023   /* Didn't match the previous lookup, search the table.  */
16024   slot = htab_find_slot_with_hash (file_table, file_name,
16025                                    htab_hash_string (file_name), INSERT);
16026   if (*slot)
16027     return (struct dwarf_file_data *) *slot;
16028
16029   created = GGC_NEW (struct dwarf_file_data);
16030   created->filename = file_name;
16031   created->emitted_number = 0;
16032   *slot = created;
16033   return created;
16034 }
16035
16036 /* If the assembler will construct the file table, then translate the compiler
16037    internal file table number into the assembler file table number, and emit
16038    a .file directive if we haven't already emitted one yet.  The file table
16039    numbers are different because we prune debug info for unused variables and
16040    types, which may include filenames.  */
16041
16042 static int
16043 maybe_emit_file (struct dwarf_file_data * fd)
16044 {
16045   if (! fd->emitted_number)
16046     {
16047       if (last_emitted_file)
16048         fd->emitted_number = last_emitted_file->emitted_number + 1;
16049       else
16050         fd->emitted_number = 1;
16051       last_emitted_file = fd;
16052
16053       if (DWARF2_ASM_LINE_DEBUG_INFO)
16054         {
16055           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
16056           output_quoted_string (asm_out_file,
16057                                 remap_debug_filename (fd->filename));
16058           fputc ('\n', asm_out_file);
16059         }
16060     }
16061
16062   return fd->emitted_number;
16063 }
16064
16065 /* Replace DW_AT_name for the decl with name.  */
16066  
16067 static void
16068 dwarf2out_set_name (tree decl, tree name)
16069 {
16070   dw_die_ref die;
16071   dw_attr_ref attr;
16072
16073   die = TYPE_SYMTAB_DIE (decl);
16074   if (!die)
16075     return;
16076
16077   attr = get_AT (die, DW_AT_name);
16078   if (attr)
16079     {
16080       struct indirect_string_node *node;
16081
16082       node = find_AT_string (dwarf2_name (name, 0));
16083       /* replace the string.  */
16084       attr->dw_attr_val.v.val_str = node;
16085     }
16086
16087   else
16088     add_name_attribute (die, dwarf2_name (name, 0));
16089 }
16090 /* Called by the final INSN scan whenever we see a var location.  We
16091    use it to drop labels in the right places, and throw the location in
16092    our lookup table.  */
16093
16094 static void
16095 dwarf2out_var_location (rtx loc_note)
16096 {
16097   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
16098   struct var_loc_node *newloc;
16099   rtx prev_insn;
16100   static rtx last_insn;
16101   static const char *last_label;
16102   tree decl;
16103
16104   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
16105     return;
16106   prev_insn = PREV_INSN (loc_note);
16107
16108   newloc = GGC_CNEW (struct var_loc_node);
16109   /* If the insn we processed last time is the previous insn
16110      and it is also a var location note, use the label we emitted
16111      last time.  */
16112   if (last_insn != NULL_RTX
16113       && last_insn == prev_insn
16114       && NOTE_P (prev_insn)
16115       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
16116     {
16117       newloc->label = last_label;
16118     }
16119   else
16120     {
16121       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
16122       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
16123       loclabel_num++;
16124       newloc->label = ggc_strdup (loclabel);
16125     }
16126   newloc->var_loc_note = loc_note;
16127   newloc->next = NULL;
16128
16129   if (cfun && in_cold_section_p)
16130     newloc->section_label = crtl->subsections.cold_section_label;
16131   else
16132     newloc->section_label = text_section_label;
16133
16134   last_insn = loc_note;
16135   last_label = newloc->label;
16136   decl = NOTE_VAR_LOCATION_DECL (loc_note);
16137   add_var_loc_to_decl (decl, newloc);
16138 }
16139
16140 /* We need to reset the locations at the beginning of each
16141    function. We can't do this in the end_function hook, because the
16142    declarations that use the locations won't have been output when
16143    that hook is called.  Also compute have_multiple_function_sections here.  */
16144
16145 static void
16146 dwarf2out_begin_function (tree fun)
16147 {
16148   htab_empty (decl_loc_table);
16149
16150   if (function_section (fun) != text_section)
16151     have_multiple_function_sections = true;
16152
16153   dwarf2out_note_section_used ();
16154 }
16155
16156 /* Output a label to mark the beginning of a source code line entry
16157    and record information relating to this source line, in
16158    'line_info_table' for later output of the .debug_line section.  */
16159
16160 static void
16161 dwarf2out_source_line (unsigned int line, const char *filename)
16162 {
16163   if (debug_info_level >= DINFO_LEVEL_NORMAL
16164       && line != 0)
16165     {
16166       int file_num = maybe_emit_file (lookup_filename (filename));
16167
16168       switch_to_section (current_function_section ());
16169
16170       /* If requested, emit something human-readable.  */
16171       if (flag_debug_asm)
16172         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
16173                  filename, line);
16174
16175       if (DWARF2_ASM_LINE_DEBUG_INFO)
16176         {
16177           /* Emit the .loc directive understood by GNU as.  */
16178           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
16179
16180           /* Indicate that line number info exists.  */
16181           line_info_table_in_use++;
16182         }
16183       else if (function_section (current_function_decl) != text_section)
16184         {
16185           dw_separate_line_info_ref line_info;
16186           targetm.asm_out.internal_label (asm_out_file,
16187                                           SEPARATE_LINE_CODE_LABEL,
16188                                           separate_line_info_table_in_use);
16189
16190           /* Expand the line info table if necessary.  */
16191           if (separate_line_info_table_in_use
16192               == separate_line_info_table_allocated)
16193             {
16194               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16195               separate_line_info_table
16196                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
16197                                  separate_line_info_table,
16198                                  separate_line_info_table_allocated);
16199               memset (separate_line_info_table
16200                        + separate_line_info_table_in_use,
16201                       0,
16202                       (LINE_INFO_TABLE_INCREMENT
16203                        * sizeof (dw_separate_line_info_entry)));
16204             }
16205
16206           /* Add the new entry at the end of the line_info_table.  */
16207           line_info
16208             = &separate_line_info_table[separate_line_info_table_in_use++];
16209           line_info->dw_file_num = file_num;
16210           line_info->dw_line_num = line;
16211           line_info->function = current_function_funcdef_no;
16212         }
16213       else
16214         {
16215           dw_line_info_ref line_info;
16216
16217           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
16218                                      line_info_table_in_use);
16219
16220           /* Expand the line info table if necessary.  */
16221           if (line_info_table_in_use == line_info_table_allocated)
16222             {
16223               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
16224               line_info_table
16225                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
16226                                  line_info_table_allocated);
16227               memset (line_info_table + line_info_table_in_use, 0,
16228                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
16229             }
16230
16231           /* Add the new entry at the end of the line_info_table.  */
16232           line_info = &line_info_table[line_info_table_in_use++];
16233           line_info->dw_file_num = file_num;
16234           line_info->dw_line_num = line;
16235         }
16236     }
16237 }
16238
16239 /* Record the beginning of a new source file.  */
16240
16241 static void
16242 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
16243 {
16244   if (flag_eliminate_dwarf2_dups)
16245     {
16246       /* Record the beginning of the file for break_out_includes.  */
16247       dw_die_ref bincl_die;
16248
16249       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
16250       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
16251     }
16252
16253   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16254     {
16255       int file_num = maybe_emit_file (lookup_filename (filename));
16256
16257       switch_to_section (debug_macinfo_section);
16258       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
16259       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
16260                                    lineno);
16261
16262       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
16263     }
16264 }
16265
16266 /* Record the end of a source file.  */
16267
16268 static void
16269 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
16270 {
16271   if (flag_eliminate_dwarf2_dups)
16272     /* Record the end of the file for break_out_includes.  */
16273     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
16274
16275   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16276     {
16277       switch_to_section (debug_macinfo_section);
16278       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
16279     }
16280 }
16281
16282 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
16283    the tail part of the directive line, i.e. the part which is past the
16284    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16285
16286 static void
16287 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
16288                   const char *buffer ATTRIBUTE_UNUSED)
16289 {
16290   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16291     {
16292       switch_to_section (debug_macinfo_section);
16293       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
16294       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16295       dw2_asm_output_nstring (buffer, -1, "The macro");
16296     }
16297 }
16298
16299 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
16300    the tail part of the directive line, i.e. the part which is past the
16301    initial whitespace, #, whitespace, directive-name, whitespace part.  */
16302
16303 static void
16304 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
16305                  const char *buffer ATTRIBUTE_UNUSED)
16306 {
16307   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16308     {
16309       switch_to_section (debug_macinfo_section);
16310       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
16311       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
16312       dw2_asm_output_nstring (buffer, -1, "The macro");
16313     }
16314 }
16315
16316 /* Set up for Dwarf output at the start of compilation.  */
16317
16318 static void
16319 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
16320 {
16321   /* Allocate the file_table.  */
16322   file_table = htab_create_ggc (50, file_table_hash,
16323                                 file_table_eq, NULL);
16324
16325   /* Allocate the decl_die_table.  */
16326   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
16327                                     decl_die_table_eq, NULL);
16328
16329   /* Allocate the decl_loc_table.  */
16330   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
16331                                     decl_loc_table_eq, NULL);
16332
16333   /* Allocate the initial hunk of the decl_scope_table.  */
16334   decl_scope_table = VEC_alloc (tree, gc, 256);
16335
16336   /* Allocate the initial hunk of the abbrev_die_table.  */
16337   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
16338   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
16339   /* Zero-th entry is allocated, but unused.  */
16340   abbrev_die_table_in_use = 1;
16341
16342   /* Allocate the initial hunk of the line_info_table.  */
16343   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
16344   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
16345
16346   /* Zero-th entry is allocated, but unused.  */
16347   line_info_table_in_use = 1;
16348
16349   /* Allocate the pubtypes and pubnames vectors.  */
16350   pubname_table = VEC_alloc (pubname_entry, gc, 32);
16351   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
16352
16353   /* Generate the initial DIE for the .debug section.  Note that the (string)
16354      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
16355      will (typically) be a relative pathname and that this pathname should be
16356      taken as being relative to the directory from which the compiler was
16357      invoked when the given (base) source file was compiled.  We will fill
16358      in this value in dwarf2out_finish.  */
16359   comp_unit_die = gen_compile_unit_die (NULL);
16360
16361   incomplete_types = VEC_alloc (tree, gc, 64);
16362
16363   used_rtx_array = VEC_alloc (rtx, gc, 32);
16364
16365   debug_info_section = get_section (DEBUG_INFO_SECTION,
16366                                     SECTION_DEBUG, NULL);
16367   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
16368                                       SECTION_DEBUG, NULL);
16369   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
16370                                        SECTION_DEBUG, NULL);
16371   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
16372                                        SECTION_DEBUG, NULL);
16373   debug_line_section = get_section (DEBUG_LINE_SECTION,
16374                                     SECTION_DEBUG, NULL);
16375   debug_loc_section = get_section (DEBUG_LOC_SECTION,
16376                                    SECTION_DEBUG, NULL);
16377   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
16378                                         SECTION_DEBUG, NULL);
16379 #ifdef DEBUG_PUBTYPES_SECTION
16380   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
16381                                         SECTION_DEBUG, NULL);
16382 #endif
16383   debug_str_section = get_section (DEBUG_STR_SECTION,
16384                                    DEBUG_STR_SECTION_FLAGS, NULL);
16385   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
16386                                       SECTION_DEBUG, NULL);
16387   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
16388                                      SECTION_DEBUG, NULL);
16389
16390   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
16391   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
16392                                DEBUG_ABBREV_SECTION_LABEL, 0);
16393   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
16394   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
16395                                COLD_TEXT_SECTION_LABEL, 0);
16396   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
16397
16398   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
16399                                DEBUG_INFO_SECTION_LABEL, 0);
16400   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
16401                                DEBUG_LINE_SECTION_LABEL, 0);
16402   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
16403                                DEBUG_RANGES_SECTION_LABEL, 0);
16404   switch_to_section (debug_abbrev_section);
16405   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
16406   switch_to_section (debug_info_section);
16407   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
16408   switch_to_section (debug_line_section);
16409   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
16410
16411   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16412     {
16413       switch_to_section (debug_macinfo_section);
16414       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
16415                                    DEBUG_MACINFO_SECTION_LABEL, 0);
16416       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
16417     }
16418
16419   switch_to_section (text_section);
16420   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
16421   if (flag_reorder_blocks_and_partition)
16422     {
16423       cold_text_section = unlikely_text_section ();
16424       switch_to_section (cold_text_section);
16425       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
16426     }
16427 }
16428
16429 /* A helper function for dwarf2out_finish called through
16430    ht_forall.  Emit one queued .debug_str string.  */
16431
16432 static int
16433 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
16434 {
16435   struct indirect_string_node *node = (struct indirect_string_node *) *h;
16436
16437   if (node->form == DW_FORM_strp)
16438     {
16439       switch_to_section (debug_str_section);
16440       ASM_OUTPUT_LABEL (asm_out_file, node->label);
16441       assemble_string (node->str, strlen (node->str) + 1);
16442     }
16443
16444   return 1;
16445 }
16446
16447 #if ENABLE_ASSERT_CHECKING
16448 /* Verify that all marks are clear.  */
16449
16450 static void
16451 verify_marks_clear (dw_die_ref die)
16452 {
16453   dw_die_ref c;
16454
16455   gcc_assert (! die->die_mark);
16456   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
16457 }
16458 #endif /* ENABLE_ASSERT_CHECKING */
16459
16460 /* Clear the marks for a die and its children.
16461    Be cool if the mark isn't set.  */
16462
16463 static void
16464 prune_unmark_dies (dw_die_ref die)
16465 {
16466   dw_die_ref c;
16467
16468   if (die->die_mark)
16469     die->die_mark = 0;
16470   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
16471 }
16472
16473 /* Given DIE that we're marking as used, find any other dies
16474    it references as attributes and mark them as used.  */
16475
16476 static void
16477 prune_unused_types_walk_attribs (dw_die_ref die)
16478 {
16479   dw_attr_ref a;
16480   unsigned ix;
16481
16482   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16483     {
16484       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
16485         {
16486           /* A reference to another DIE.
16487              Make sure that it will get emitted.  */
16488           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
16489         }
16490       /* Set the string's refcount to 0 so that prune_unused_types_mark
16491          accounts properly for it.  */
16492       if (AT_class (a) == dw_val_class_str)
16493         a->dw_attr_val.v.val_str->refcount = 0;
16494     }
16495 }
16496
16497
16498 /* Mark DIE as being used.  If DOKIDS is true, then walk down
16499    to DIE's children.  */
16500
16501 static void
16502 prune_unused_types_mark (dw_die_ref die, int dokids)
16503 {
16504   dw_die_ref c;
16505
16506   if (die->die_mark == 0)
16507     {
16508       /* We haven't done this node yet.  Mark it as used.  */
16509       die->die_mark = 1;
16510
16511       /* We also have to mark its parents as used.
16512          (But we don't want to mark our parents' kids due to this.)  */
16513       if (die->die_parent)
16514         prune_unused_types_mark (die->die_parent, 0);
16515
16516       /* Mark any referenced nodes.  */
16517       prune_unused_types_walk_attribs (die);
16518
16519       /* If this node is a specification,
16520          also mark the definition, if it exists.  */
16521       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
16522         prune_unused_types_mark (die->die_definition, 1);
16523     }
16524
16525   if (dokids && die->die_mark != 2)
16526     {
16527       /* We need to walk the children, but haven't done so yet.
16528          Remember that we've walked the kids.  */
16529       die->die_mark = 2;
16530
16531       /* If this is an array type, we need to make sure our
16532          kids get marked, even if they're types.  */
16533       if (die->die_tag == DW_TAG_array_type)
16534         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
16535       else
16536         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16537     }
16538 }
16539
16540 /* For local classes, look if any static member functions were emitted
16541    and if so, mark them.  */
16542
16543 static void
16544 prune_unused_types_walk_local_classes (dw_die_ref die)
16545 {
16546   dw_die_ref c;
16547
16548   if (die->die_mark == 2)
16549     return;
16550
16551   switch (die->die_tag)
16552     {
16553     case DW_TAG_structure_type:
16554     case DW_TAG_union_type:
16555     case DW_TAG_class_type:
16556       break;
16557
16558     case DW_TAG_subprogram:
16559       if (!get_AT_flag (die, DW_AT_declaration)
16560           || die->die_definition != NULL)
16561         prune_unused_types_mark (die, 1);
16562       return;
16563
16564     default:
16565       return;
16566     }
16567
16568   /* Mark children.  */
16569   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
16570 }
16571
16572 /* Walk the tree DIE and mark types that we actually use.  */
16573
16574 static void
16575 prune_unused_types_walk (dw_die_ref die)
16576 {
16577   dw_die_ref c;
16578
16579   /* Don't do anything if this node is already marked and
16580      children have been marked as well.  */
16581   if (die->die_mark == 2)
16582     return;
16583
16584   switch (die->die_tag)
16585     {
16586     case DW_TAG_structure_type:
16587     case DW_TAG_union_type:
16588     case DW_TAG_class_type:
16589       if (die->die_perennial_p)
16590         break;
16591
16592       for (c = die->die_parent; c; c = c->die_parent)
16593         if (c->die_tag == DW_TAG_subprogram)
16594           break;
16595
16596       /* Finding used static member functions inside of classes
16597          is needed just for local classes, because for other classes
16598          static member function DIEs with DW_AT_specification
16599          are emitted outside of the DW_TAG_*_type.  If we ever change
16600          it, we'd need to call this even for non-local classes.  */
16601       if (c)
16602         prune_unused_types_walk_local_classes (die);
16603
16604       /* It's a type node --- don't mark it.  */
16605       return;
16606
16607     case DW_TAG_const_type:
16608     case DW_TAG_packed_type:
16609     case DW_TAG_pointer_type:
16610     case DW_TAG_reference_type:
16611     case DW_TAG_volatile_type:
16612     case DW_TAG_typedef:
16613     case DW_TAG_array_type:
16614     case DW_TAG_interface_type:
16615     case DW_TAG_friend:
16616     case DW_TAG_variant_part:
16617     case DW_TAG_enumeration_type:
16618     case DW_TAG_subroutine_type:
16619     case DW_TAG_string_type:
16620     case DW_TAG_set_type:
16621     case DW_TAG_subrange_type:
16622     case DW_TAG_ptr_to_member_type:
16623     case DW_TAG_file_type:
16624       if (die->die_perennial_p)
16625         break;
16626
16627       /* It's a type node --- don't mark it.  */
16628       return;
16629
16630     default:
16631       /* Mark everything else.  */
16632       break;
16633   }
16634
16635   if (die->die_mark == 0)
16636     {
16637       die->die_mark = 1;
16638
16639       /* Now, mark any dies referenced from here.  */
16640       prune_unused_types_walk_attribs (die);
16641     }
16642
16643   die->die_mark = 2;
16644
16645   /* Mark children.  */
16646   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
16647 }
16648
16649 /* Increment the string counts on strings referred to from DIE's
16650    attributes.  */
16651
16652 static void
16653 prune_unused_types_update_strings (dw_die_ref die)
16654 {
16655   dw_attr_ref a;
16656   unsigned ix;
16657
16658   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
16659     if (AT_class (a) == dw_val_class_str)
16660       {
16661         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
16662         s->refcount++;
16663         /* Avoid unnecessarily putting strings that are used less than
16664            twice in the hash table.  */
16665         if (s->refcount
16666             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
16667           {
16668             void ** slot;
16669             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
16670                                              htab_hash_string (s->str),
16671                                              INSERT);
16672             gcc_assert (*slot == NULL);
16673             *slot = s;
16674           }
16675       }
16676 }
16677
16678 /* Remove from the tree DIE any dies that aren't marked.  */
16679
16680 static void
16681 prune_unused_types_prune (dw_die_ref die)
16682 {
16683   dw_die_ref c;
16684
16685   gcc_assert (die->die_mark);
16686   prune_unused_types_update_strings (die);
16687
16688   if (! die->die_child)
16689     return;
16690
16691   c = die->die_child;
16692   do {
16693     dw_die_ref prev = c;
16694     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
16695       if (c == die->die_child)
16696         {
16697           /* No marked children between 'prev' and the end of the list.  */
16698           if (prev == c)
16699             /* No marked children at all.  */
16700             die->die_child = NULL;
16701           else
16702             {
16703               prev->die_sib = c->die_sib;
16704               die->die_child = prev;
16705             }
16706           return;
16707         }
16708
16709     if (c != prev->die_sib)
16710       prev->die_sib = c;
16711     prune_unused_types_prune (c);
16712   } while (c != die->die_child);
16713 }
16714
16715
16716 /* Remove dies representing declarations that we never use.  */
16717
16718 static void
16719 prune_unused_types (void)
16720 {
16721   unsigned int i;
16722   limbo_die_node *node;
16723   pubname_ref pub;
16724
16725 #if ENABLE_ASSERT_CHECKING
16726   /* All the marks should already be clear.  */
16727   verify_marks_clear (comp_unit_die);
16728   for (node = limbo_die_list; node; node = node->next)
16729     verify_marks_clear (node->die);
16730 #endif /* ENABLE_ASSERT_CHECKING */
16731
16732   /* Set the mark on nodes that are actually used.  */
16733   prune_unused_types_walk (comp_unit_die);
16734   for (node = limbo_die_list; node; node = node->next)
16735     prune_unused_types_walk (node->die);
16736
16737   /* Also set the mark on nodes referenced from the
16738      pubname_table or arange_table.  */
16739   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
16740     prune_unused_types_mark (pub->die, 1);
16741   for (i = 0; i < arange_table_in_use; i++)
16742     prune_unused_types_mark (arange_table[i], 1);
16743
16744   /* Get rid of nodes that aren't marked; and update the string counts.  */
16745   if (debug_str_hash)
16746     htab_empty (debug_str_hash);
16747   prune_unused_types_prune (comp_unit_die);
16748   for (node = limbo_die_list; node; node = node->next)
16749     prune_unused_types_prune (node->die);
16750
16751   /* Leave the marks clear.  */
16752   prune_unmark_dies (comp_unit_die);
16753   for (node = limbo_die_list; node; node = node->next)
16754     prune_unmark_dies (node->die);
16755 }
16756
16757 /* Set the parameter to true if there are any relative pathnames in
16758    the file table.  */
16759 static int
16760 file_table_relative_p (void ** slot, void *param)
16761 {
16762   bool *p = (bool *) param;
16763   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
16764   if (!IS_ABSOLUTE_PATH (d->filename))
16765     {
16766       *p = true;
16767       return 0;
16768     }
16769   return 1;
16770 }
16771
16772 /* Output stuff that dwarf requires at the end of every file,
16773    and generate the DWARF-2 debugging info.  */
16774
16775 static void
16776 dwarf2out_finish (const char *filename)
16777 {
16778   limbo_die_node *node, *next_node;
16779   dw_die_ref die = 0;
16780   unsigned int i;
16781
16782   /* Add the name for the main input file now.  We delayed this from
16783      dwarf2out_init to avoid complications with PCH.  */
16784   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
16785   if (!IS_ABSOLUTE_PATH (filename))
16786     add_comp_dir_attribute (comp_unit_die);
16787   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
16788     {
16789       bool p = false;
16790       htab_traverse (file_table, file_table_relative_p, &p);
16791       if (p)
16792         add_comp_dir_attribute (comp_unit_die);
16793     }
16794
16795   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
16796     {
16797       add_location_or_const_value_attribute (
16798         VEC_index (deferred_locations, deferred_locations_list, i)->die,
16799         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
16800         DW_AT_location);
16801     }
16802
16803   /* Traverse the limbo die list, and add parent/child links.  The only
16804      dies without parents that should be here are concrete instances of
16805      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
16806      For concrete instances, we can get the parent die from the abstract
16807      instance.  */
16808   for (node = limbo_die_list; node; node = next_node)
16809     {
16810       next_node = node->next;
16811       die = node->die;
16812
16813       if (die->die_parent == NULL)
16814         {
16815           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
16816
16817           if (origin)
16818             add_child_die (origin->die_parent, die);
16819           else if (die == comp_unit_die)
16820             ;
16821           else if (errorcount > 0 || sorrycount > 0)
16822             /* It's OK to be confused by errors in the input.  */
16823             add_child_die (comp_unit_die, die);
16824           else
16825             {
16826               /* In certain situations, the lexical block containing a
16827                  nested function can be optimized away, which results
16828                  in the nested function die being orphaned.  Likewise
16829                  with the return type of that nested function.  Force
16830                  this to be a child of the containing function.
16831
16832                  It may happen that even the containing function got fully
16833                  inlined and optimized out.  In that case we are lost and
16834                  assign the empty child.  This should not be big issue as
16835                  the function is likely unreachable too.  */
16836               tree context = NULL_TREE;
16837
16838               gcc_assert (node->created_for);
16839
16840               if (DECL_P (node->created_for))
16841                 context = DECL_CONTEXT (node->created_for);
16842               else if (TYPE_P (node->created_for))
16843                 context = TYPE_CONTEXT (node->created_for);
16844
16845               gcc_assert (context
16846                           && (TREE_CODE (context) == FUNCTION_DECL
16847                               || TREE_CODE (context) == NAMESPACE_DECL));
16848
16849               origin = lookup_decl_die (context);
16850               if (origin)
16851                 add_child_die (origin, die);
16852               else
16853                 add_child_die (comp_unit_die, die);
16854             }
16855         }
16856     }
16857
16858   limbo_die_list = NULL;
16859
16860   /* Walk through the list of incomplete types again, trying once more to
16861      emit full debugging info for them.  */
16862   retry_incomplete_types ();
16863
16864   if (flag_eliminate_unused_debug_types)
16865     prune_unused_types ();
16866
16867   /* Generate separate CUs for each of the include files we've seen.
16868      They will go into limbo_die_list.  */
16869   if (flag_eliminate_dwarf2_dups)
16870     break_out_includes (comp_unit_die);
16871
16872   /* Traverse the DIE's and add add sibling attributes to those DIE's
16873      that have children.  */
16874   add_sibling_attributes (comp_unit_die);
16875   for (node = limbo_die_list; node; node = node->next)
16876     add_sibling_attributes (node->die);
16877
16878   /* Output a terminator label for the .text section.  */
16879   switch_to_section (text_section);
16880   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
16881   if (flag_reorder_blocks_and_partition)
16882     {
16883       switch_to_section (unlikely_text_section ());
16884       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
16885     }
16886
16887   /* We can only use the low/high_pc attributes if all of the code was
16888      in .text.  */
16889   if (!have_multiple_function_sections)
16890     {
16891       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
16892       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
16893     }
16894
16895   else
16896     {
16897       unsigned fde_idx = 0;
16898
16899       /* We need to give .debug_loc and .debug_ranges an appropriate
16900          "base address".  Use zero so that these addresses become
16901          absolute.  Historically, we've emitted the unexpected
16902          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
16903          Emit both to give time for other tools to adapt.  */
16904       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
16905       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
16906
16907       add_AT_range_list (comp_unit_die, DW_AT_ranges,
16908                          add_ranges_by_labels (text_section_label,
16909                                                text_end_label));
16910       if (flag_reorder_blocks_and_partition)
16911         add_ranges_by_labels (cold_text_section_label,
16912                               cold_end_label);
16913
16914       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
16915         {
16916           dw_fde_ref fde = &fde_table[fde_idx];
16917
16918           if (fde->dw_fde_switched_sections)
16919             {
16920               add_ranges_by_labels (fde->dw_fde_hot_section_label,
16921                                     fde->dw_fde_hot_section_end_label);
16922               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
16923                                     fde->dw_fde_unlikely_section_end_label);
16924             }
16925           else
16926             add_ranges_by_labels (fde->dw_fde_begin,
16927                                   fde->dw_fde_end);
16928         }
16929
16930       add_ranges (NULL);
16931     }
16932
16933   /* Output location list section if necessary.  */
16934   if (have_location_lists)
16935     {
16936       /* Output the location lists info.  */
16937       switch_to_section (debug_loc_section);
16938       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
16939                                    DEBUG_LOC_SECTION_LABEL, 0);
16940       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
16941       output_location_lists (die);
16942     }
16943
16944   if (debug_info_level >= DINFO_LEVEL_NORMAL)
16945     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
16946                     debug_line_section_label);
16947
16948   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
16949     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
16950
16951   /* Output all of the compilation units.  We put the main one last so that
16952      the offsets are available to output_pubnames.  */
16953   for (node = limbo_die_list; node; node = node->next)
16954     output_comp_unit (node->die, 0);
16955
16956   /* Output the main compilation unit if non-empty or if .debug_macinfo
16957      has been emitted.  */
16958   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
16959
16960   /* Output the abbreviation table.  */
16961   switch_to_section (debug_abbrev_section);
16962   output_abbrev_section ();
16963
16964   /* Output public names table if necessary.  */
16965   if (!VEC_empty (pubname_entry, pubname_table))
16966     {
16967       switch_to_section (debug_pubnames_section);
16968       output_pubnames (pubname_table);
16969     }
16970
16971 #ifdef DEBUG_PUBTYPES_SECTION
16972   /* Output public types table if necessary.  */
16973   if (!VEC_empty (pubname_entry, pubtype_table))
16974     {
16975       switch_to_section (debug_pubtypes_section);
16976       output_pubnames (pubtype_table);
16977     }
16978 #endif
16979
16980   /* Output the address range information.  We only put functions in the arange
16981      table, so don't write it out if we don't have any.  */
16982   if (fde_table_in_use)
16983     {
16984       switch_to_section (debug_aranges_section);
16985       output_aranges ();
16986     }
16987
16988   /* Output ranges section if necessary.  */
16989   if (ranges_table_in_use)
16990     {
16991       switch_to_section (debug_ranges_section);
16992       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
16993       output_ranges ();
16994     }
16995
16996   /* Output the source line correspondence table.  We must do this
16997      even if there is no line information.  Otherwise, on an empty
16998      translation unit, we will generate a present, but empty,
16999      .debug_info section.  IRIX 6.5 `nm' will then complain when
17000      examining the file.  This is done late so that any filenames
17001      used by the debug_info section are marked as 'used'.  */
17002   if (! DWARF2_ASM_LINE_DEBUG_INFO)
17003     {
17004       switch_to_section (debug_line_section);
17005       output_line_info ();
17006     }
17007
17008   /* Have to end the macro section.  */
17009   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17010     {
17011       switch_to_section (debug_macinfo_section);
17012       dw2_asm_output_data (1, 0, "End compilation unit");
17013     }
17014
17015   /* If we emitted any DW_FORM_strp form attribute, output the string
17016      table too.  */
17017   if (debug_str_hash)
17018     htab_traverse (debug_str_hash, output_indirect_string, NULL);
17019 }
17020 #else
17021
17022 /* This should never be used, but its address is needed for comparisons.  */
17023 const struct gcc_debug_hooks dwarf2_debug_hooks;
17024
17025 #endif /* DWARF2_DEBUGGING_INFO */
17026
17027 #include "gt-dwarf2out.h"